Re: [Tutor] Blackjack Betting

2011-07-03 Thread Alan Gauld


"David Merrick"  wrote

class BJ_Player(BJ_Hand):
   """ A Blackjack Player. """

   def betting(stash):


You forgot self so stash will take on the value of
the instance.



   try:
   if stash > 0:
   wager = int(input("\nHow much do you want to wager?: 
"))

   if wager > bet.stash:
   int(input("\n You can only wager what you have. 
How

much?: "))


and you don't assign the result here to any variables



class BJ_Game(object):
   """ A Blackjack Game. """
   def __init__(self, names):
   self.players = []
   for name in names:
   player = BJ_Player(name)
   bet = BJ_Player(name).betting(stash = 10)


Here you call the method and python assigns the new
instance value to stash, but you are simultaneously
assigning 10 to stash. Pyton is confused...

You need a self in your method definition.

 File "I:/Python/Python Source Code/chapter09/blackjackBetting.py", 
line

120, in __init__
   bet = BJ_Player(name).betting(stash = 10)
TypeError: betting() got multiple values for keyword argument 
'stash'




HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Cython question

2011-07-03 Thread Albert-Jan Roskam
Hi Stefan, Alan,

Thanks for your useful advice. The first thing I will try is take the call to 
the spssio dll out of the Python method (ie, 'unwrap' it) and put it inside the 
loop. I didn't think wrapping the code inside a method/function would create so 
much overhead.

Cheers!!

Albert-Jan



~~

All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the Romans ever done for us?

~~

--- On Sat, 7/2/11, Stefan Behnel  wrote:

From: Stefan Behnel 
Subject: Re: [Tutor] Cython question
To: tutor@python.org
Date: Saturday, July 2, 2011, 4:52 PM

Alan Gauld, 02.07.2011 15:28:
> "Albert-Jan Roskam" wrote
>> I used cProfile to find the bottlenecks, the two Python functions
>> getValueChar and getValueNum. These two Python functions simply call two
>> equivalent C functions in a .dll (using ctypes).

The code is currently declared as Windows-only and I don't know any good 
C-level profiling code for that platform. Under Linux, once I'm sure I have a 
CPU bound problem below the Python level, I'd use valgrind and KCacheGrind to 
analyse the performance. That will include all C function calls (and even CPU 
instructions, if you want) in the call trace. Makes it a bit less obvious to 
see what Python is doing, but leads to much more detailed results at the C 
level.

It's also worth keeping in mind that all profiling attempts *always* interfere 
with the normal program execution. The results you get during a profiling run 
may not be what you'd get with profiling disabled. So, profiling is nice, but 
it doesn't replace proper benchmarking.


> In that case cythin will speed up the calling loops but it can't do
> anything to speed up the DLL calls, you have effectively already optimised
> those functions by calling the DLL.
> 
>> The problem is that these functions are called as many times as there are
>> VALUES in a file
> 
> It might be worth a try if you have very big data sets
> because a C loop is faster than a Python loop. But don't expect order of
> magnitude improvements.

Looking at the code now, it's actually worse than that. The C function call 
does not only go through ctypes, but is additionally wrapped in a method call. 
So the OP is paying the call overhead twice for each field, plus the method 
lookup and some other operations. These things can add up quite easily.

So, iff the conversion code is really a CPU bottleneck, and depending on how 
much work the C functions actually do, the current call overhead, 100 times per 
record, may be a substantial part of the game. It's worth seeing if it can be 
dropped at the Python level by removing method lookup and call levels (i.e. by 
inlining the method), but if that's not enough, Cython may still be worth it. 
For one, Cython's call overhead is lower than that of ctypes, and if the call 
is only done once, and the loop is moved into Cython (i.e. C) entirely, the 
overhead will also drop substantially.

It might also be worth running the code in PyPy instead of CPython. PyPy will 
optimise a lot of the overhead away that this code contains.


>> So if I understand you correctly, this is not Cpu bound

I don't have enough information to comment on that.


> It may still be CPU bound in that the CPU is doing all the work, but if
> the CPU time is in the DLL functions rather than in the loop cython
> won't help much.
> 
> CPU bound refers to the type of processing - is it lots of logic, math,
> control flows etc? Or is it I/O bound - reading network, disk, or user
> input? Or it might be memory bound - creating lots of in memory objects
> (especially if that results in paging to disk, when it becomes I/O
> bound  too!)
> 
> Knowing what is causing the bottleneck will determine how to improve
> things. Use tools like TaskManager in Windows or top in *nix to see
> where the time is going and what resources are being consumed. Fast code
> is not always the answer.

That is very good advice. As a rule of thumb, a process monitor like top will 
tell you how much time is spent in I/O and CPU. If, during a test run (with 
profiling disabled, as that eats time, too!), your CPU usage stays close to 
100%, your program is CPU bound. If, however, it stays lower, and the monitor 
reports a high I/O waiting time, it's I/O bound. In this case, I/O bound is 
what you want to achieve, because it means that your code is running faster 
than your hard drive can deliver the data.

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Algorithm for sequence matching

2011-07-03 Thread Christopher King
I know a way to do that
set1 = set(list1)
set2 = set(list2)
combined = set1&set2

On Sat, Jul 2, 2011 at 5:16 PM, Walter Prins  wrote:

> Hi Ankur,
>
> On 2 July 2011 21:30, ANKUR AGGARWAL  wrote:
>
>> Hey
>> I am looking for an algo for the largest sequence search in the two list.
>>
>> Example : list a accepts some say 'm' numbers. list b accept says 'n'
>> numbers. I want to look for the largest same sequence between the two list
>> and then display it. I tried out but failed to do so.
>> Say A=[11,23,45,21,63,56,78,32]
>> B=[56,78,11,23,45,21,111,234,56543]
>>
>> There are two  similar sequence matching over here [11,23] and [23,45,21]
>> i want to display second sequence because its larger in number. Plz help
>> Thanks in Advance :)
>>
>
> Umm, what about [11,23,45,21]?  That seems to be longer still so should be
> the best one to display, or?
>
> Walter
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Algorithm for sequence matching

2011-07-03 Thread Albert-Jan Roskam
Hi,

Are you looking for a Longest Common Subsequence (LCS) algorithm?
http://code.activestate.com/recipes/576869-longest-common-subsequence-problem-solver/

Cheers!!

Albert-Jan



~~

All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the Romans ever done for us?

~~

--- On Sun, 7/3/11, Christopher King  wrote:

From: Christopher King 
Subject: Re: [Tutor] Algorithm for sequence matching
To: "Walter Prins" 
Cc: tutor@python.org
Date: Sunday, July 3, 2011, 5:48 PM

I know a way to do thatset1 = set(list1)set2 = set(list2)combined = set1&set2

On Sat, Jul 2, 2011 at 5:16 PM, Walter Prins  wrote:

Hi Ankur,

On 2 July 2011 21:30, ANKUR AGGARWAL  wrote:


HeyI am looking for an algo for the largest sequence search in the two list.
Example : list a accepts some say 'm' numbers. list b accept says 'n' numbers. 
I want to look for the largest same sequence between the two list and then 
display it. I tried out but failed to do so. 



Say A=[11,23,45,21,63,56,78,32]B=[56,78,11,23,45,21,111,234,56543]
There are two  similar sequence matching over here [11,23] and [23,45,21] i 
want to display second sequence because its larger in number. Plz help



Thanks in Advance :)
Umm, what about [11,23,45,21]?  That seems to be longer still so should be the 
best one to display, or?

Walter 



___

Tutor maillist  -  Tutor@python.org

To unsubscribe or change subscription options:

http://mail.python.org/mailman/listinfo/tutor





-Inline Attachment Follows-

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Blackjackbetting

2011-07-03 Thread David Merrick
HI. I feel I'm starting to go round in circles solving this problem. I feel
I made significant progress.
Can someone help me iron out the bugs please

# Blackjack
# From 1 to 7 players compete against a dealer

import cards, games

class BJ_Card(cards.Card):
""" A Blackjack Card. """
ACE_VALUE = 1

@property
def value(self):
if self.is_face_up:
v = BJ_Card.RANKS.index(self.rank) + 1
if v > 10:
v = 10
else:
v = None
return v

class BJ_Deck(cards.Deck):
""" A Blackjack Deck. """
def populate(self):
for suit in BJ_Card.SUITS:
for rank in BJ_Card.RANKS:
self.cards.append(BJ_Card(rank, suit))


class BJ_Hand(cards.Hand):
""" A Blackjack Hand. """
def __init__(self, name):
super(BJ_Hand, self).__init__()
self.name = name

def __str__(self):
rep = self.name + ":\t" + super(BJ_Hand, self).__str__()
if self.total:
rep += "(" + str(self.total) + ")"
return rep

@property
def total(self):
# if a card in the hand has value of None, then total is None
for card in self.cards:
if not card.value:
return None

# add up card values, treat each Ace as 1
t = 0
for card in self.cards:
  t += card.value

# determine if hand contains an Ace
contains_ace = False
for card in self.cards:
if card.value == BJ_Card.ACE_VALUE:
contains_ace = True

# if hand contains Ace and total is low enough, treat Ace as 11
if contains_ace and t <= 11:
# add only 10 since we've already added 1 for the Ace
t += 10

return t

def is_busted(self):
return self.total > 21

class Bet(object):
""" A Blackjack Gamble. """
# Values
def __init__(bet, money = 10):
stash  = money

# Betting options
def betting(bet,stash):
try:
if stash > 0:
wager = int(input("\nHow much do you want to wager?: "))
if wager > stash:
int(input("\n You can only wager what you have. How
much?: "))
elif wager < 0:
int(input("\n You can only wager what you have. How
much?: "))
except ValueError:
int(input("\n That's not valid! Choose a number: "))


# Money Conditions
def gamble(bet):
if bet.stash <= 0:
print("\nYou are out of money! You're out of the game!")



class BJ_Player(BJ_Hand):
""" A Blackjack Player. """
stash = 10
if stash <= 0:
 print("\nYou are out of money! You're out of the game!")

def is_hitting(self):
response = games.ask_yes_no("\n" + self.name + ", do you want a hit?
(Y/N): ")
return response == "y"

def bust(self,stash,wager):
print(self.name, "busts.")
self.lose(self,stash,wager)

def lose(self,stash,wager):
print(self.name, "loses.")
stash = stash - wager
print("Your stash is: ",stash)
return stash

def win(self,stash,wager):
print(self.name, "wins.")
stash = stash + wager
print("Your stash is: ",stash)
return stash

def push(self):
print(self.name, "pushes.")


class BJ_Dealer(BJ_Hand):
""" A Blackjack Dealer. """
def is_hitting(self):
return self.total < 17

def bust(self):
print(self.name, "busts.")

def flip_first_card(self):
first_card = self.cards[0]
first_card.flip()


class BJ_Game(object):
""" A Blackjack Game. """
def __init__(self, names):
self.players = []
for name in names:
stash = 100
player = BJ_Player(name)
playerbet = Bet(stash).betting(stash)
self.players.append(player)

self.dealer = BJ_Dealer("Dealer")

self.deck = BJ_Deck()
self.deck.populate()
self.deck.shuffle()

@property
def still_playing(self):
sp = []
for player in self.players:
if not player.is_busted():
sp.append(player)
return sp

def __additional_cards(self, player,stash,wager):
while not player.is_busted() and player.is_hitting():
self.deck.deal([player])
print(player)
if player.is_busted():
player.bust(self,stash,wager)

def play(self,stash,wager):
# deal initial 2 cards to everyone
self.deck.deal(self.players + [self.dealer], per_hand = 2)
self.dealer.flip_first_card()# hide dealer's first card
for player in self.players:
print(player)
print(self.dealer)

# deal additional cards to players
for player in self.players:
self.__additional_cards(player,stash,wager)

self.dealer.flip_first_card() 

Re: [Tutor] Blackjackbetting

2011-07-03 Thread Alan Gauld

"David Merrick"  wrote


I feel I'm starting to go round in circles solving this problem.
I feel I made significant progress.


That sounds like a contradiction.
Did you make progress or are you going in circles?

Do you understand what self is?

 File "I:/Python/Python Source Code/chapter09/blackjackBettingB.py", 
line

166, in __additional_cards
   player.bust(self,stash,wager)
TypeError: bust() takes exactly 1 positional argument (4 given)


OK, It looks a bit confusing but here are some observations.

player.bust() takes 1 argument. You have provided 4
It looks like 3, but remember that Python fills in the 'self'
argument using the current instance to make 4 in total.

Now why are you explicitly passing self as the first argument?
It looks like you don't really understand how self is used?

But where does the 1 argument come from, BJ Player
takes 3 arguments but BJ Hand.bust only takes one.

Could you have managed to create a BJHand instance
where you intended a BJPlayer?

I don't know, and I'm not going to try to unravel that
mass of code for you! But it's something to check.
(print is your friend)

But remember, we said away back at the beginning:
"don't try to debug a load of code at once.
Build it up slowly, testing as you go" ?

It's a lot easier to find bugs that way. If you continue
to pursue the "built it big and then ask for help" approach
you may run out of sympathetic readers before your code
ever works...

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor