Sunday, 1 September 2013

global name 'rank' is not defined

global name 'rank' is not defined

I'm new to Python programming and have problems with this code. rank and
suit does not work in method shuffle. My "simple" (?) Question is, why?
from Tkinter import *
from Canvas import Rectangle, CanvasText, Group, Window
from PIL import Image
import ImageTk
win = Tk()
text = Text(win, width=65, height=15, font=("Arial", 10))
win.title("Play High or Low Card")
win.geometry("700x600")
class Card(object):
RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K"]
SUIT = ["Python/projekt/bilder/hearts.png",
"Python/projekt/bilder/spades.png",
"Python/projekt/bilder/diamond.png",
"Python/projekt/bilder/clubs.png"]
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
rep = self.rank + self.suit
return rep
def draw(self,suit,rank):
bg = ImageTk.PhotoImage(Image.open(self.suit).resize((10, 10)))
cardGraph = Canvas(win, width=70, height=100, bg="White", bd=1,
relief='solid', highlightthickness=2)
cardGraph.photo=bg
cardGraph.create_image(10,10, image=bg, anchor=CENTER) #left/up
cardGraph.create_image(53,93, image=bg, anchor=CENTER) #right/down
cardGraph.create_text(20, 10, text=self.rank, font=("Helvetica",
8, "bold")) #left/up
cardGraph.create_text(63, 93, text=self.rank, font=("Helvetica",
8, "bold")) #right/down
cardGraph.create_text(36, 50, text=self.rank, font=("Helvetica",
12, "bold")) #middle
cardGraph.pack(side = "left", anchor=NW)
class Hand(object):
def __init__(self):
self.cards = []
def __str__ (self):
if self.cards:
rep = ""
for card in self.cards:
rep += str(card) + " "
else:
rep = "<empty>"
return rep
def clear(self):
self.cards = []
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)
class Deck(Hand, Card):
def populate(self):
for suit in Card.SUIT:
for rank in Card.RANKS:
self.add(Card(rank, suit))
DrawCard = Card(rank,suit)
DrawCard.draw(self,rank)
def shuffle(self):
import random
random.shuffle(self.cards)
DrawCard = Card(rank,suit)
DrawCard.draw(self,rank)
def deal(self, hands, per_hand = 1):
for rounds in range(per_hand):
for hand in hands:
if self.cards:
top_card = self.cards[1]
self.give(top_card, hand)
else:
print("Cant continue deck. Out of cards!!")
deck1 = Deck()
deck1.populate()
deck1.shuffle()
my_hand = Hand()
your_hand = Hand()
hands = [my_hand, your_hand]
deck1.deal(hands, per_hand = 5)
print(my_hand)
shuffleBtn = Button(win, text="Turn", command=lambda: deck1.shuffle())
shuffleBtn.pack()
mainloop()
The traceback is:
Traceback (most recent call last): File "C:/Users/RaJ/Desktop/Cards", line
95, in deck1.shuffle() File "C:/Users/RaJ/Desktop/Cards", line 77, in
shuffle DrawCard = Card(rank,suit) NameError: global name 'rank' is not
defined

Saturday, 31 August 2013

JAVA dynamic threading

JAVA dynamic threading

i'm a begginer in java, and i have this code i made, but that t thread
gets only executed once, how can i make the thread be dynamic?
//main program - creating 2 threads, unfortuantely at this point only 1
it's running
public static void main(String[] args){
timeThread ttm = new timeThread();
ttm.name = "map";
ttm.min = 1000;
ttm.max = 5000;
ttm.start();
timeThread tta = new timeThread();
tta.name = "arena";
tta.min = 6000;
tta.max = 10000;
tta.start();
}
//the timethread i'm calling in the program
static class timeThread{
static String name;
static int min;
static int max;
static int random;
static Thread t = new Thread () {
public void run () {
while (true){
random = genRandomInteger(min,max);
System.out.println("Thread named: "
+ name + " running for: "
+ random + " secconds...");
try {
Thread.sleep(random);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
};
void start(){
t.start();
}
}
//the random function generator
private static int genRandomInteger(int aStart, int aEnd){
int returnValue = aStart + (int)(Math.random()
* ((aEnd - aStart) + 1));
return returnValue;
}

Is this dependency injection?

Is this dependency injection?

A simple question that is perhaps a bit silly. Is this dependency
injection? I do not mean a DI container.
Foo depends on an instance of Bar. Foo gets an instance of Bar passed by
constructor and does not care even for the initialization.
public class Foo
{
public Foo(IBar bar)
{
DoSomething(bar);
}
private void DoSomething(IBar bar)
{
// ...
}
}
public class Bar : IBar
{
// ...
}
Is passing an instance of Bar to the constructor a dependency injection?
Especially constructor injection.

DB_DataObject and PDO

DB_DataObject and PDO

I was told that using PDO is recommended to make my code more secure from
mysql injections.
I am currently using DB_DataObject that I read that cleans the input from
injections as well
(http://pear.php.net/manual/en/package.database.db-dataobject.php) Do I
still need to use PDO or DB_Dataobject should be ok ? Also can I combine
them together and if yes how.
Example part of my DB statement
$password=encryptpass($_REQUEST['password']);
$user->query("select username from {$user->__table} where
(username = '$username' or email='$username') AND password =
'$password' ");
Thanks

How to screengrab current page using JavaScript?

How to screengrab current page using JavaScript?

When you send feedback using the new Google Maps Beta, you have the option
to send a screengrab of the page you're on. Given that no Java alerts
appear, I can only assume this is using JavaScript.
How would I take a screengrab of the page I'm on using JavaScript, like
how Google Maps Feedback Service does?

CUDA: static global member functions (kernels)

CUDA: static global member functions (kernels)

When compiling the code below using nvcc (CUDA 5.0), the error "illegal
combination of memory qualifiers" appears, as it apparently is impossible
to have global kernels in a class.
class A
{
public:
__global__ static void kernel();
};
__global__ void A::kernel()
{}
I can understand this restriction when dealing with non-static members,
but why does the error still occur when the kernel is declared static? The
calling of such members would be no different from calling the function
when it is declared in a namespace (A in this case).
A::kernel <<< 1, 1 >>> ();
Is there a reason I'm missing as to why this hasn't been implemented (yet)?

Add new number and comma at the end of value [on hold]

Add new number and comma at the end of value [on hold]

Suppose I have a number like 1234. I want to add new number 00 and , and €
at the end. Now number will be like 1234,00€ I can not figure out.