[Tutor] Baccarat code check.

col speed ajarncolin at gmail.com
Sat Dec 17 11:01:56 CET 2011


On 17 December 2011 16:49, Peter Otten <__peter__ at web.de> wrote:
> col speed wrote:
>
>> If anyone has the time, please have a look at the attached text file
>> and let me know any comments on how to improve it.
>
> At first glance the code looks good. I think you can move to the next level
> now ;)
>
> Are you using a version control system? If not, have a look at mercurial.
> Here's an introduction: http://hginit.com/
>
> Once you've checked in the code you can start writing tests that verify the
> correctness of parts of it, see http://docs.python.org/library/unittest.html
> Don't be afraid to change your code to make it easier to test. That's called
> "refactoring" and usually improves the code quality. If something goes wrong
> you always have the previous version in the version control.
>
> How do you know you're done writing tests? There's a great tool that helps
> you measure what portion of your code is exercised by your tests:
>
> http://pypi.python.org/pypi/coverage
>
> Do you know the style guide for Python code
> (http://www.python.org/dev/peps/pep-0008/)? You should at least consider to
> follow it.
>
> Now to your actual code: I don't know Baccarat and haven't looked closely at
> your script. I think you should base your Card class on data rather than
> calculations. That will simplify it significantly:
>
> _cards = [
> ("1", 1),
> ("2", 2),
> ("3", 3),
> ("4", 4),
> ("5", 5),
> ("6", 6),
> ("7", 7),
> ("8", 8),
> ("9", 9),
> ("10", 0),
> ("J", 0),
> ("Q", 0),
> ("K", 0),
> ("A", 1),
> ]
>
> class Card(object):
>    def __init__(self, name, value):
>        self.name = name
>        self.value = value
>
> cards = [Card(n, v) for n, v in _cards]
>
> class Shoe(list):
>    """A collection of 8 decks of cards"""
>    def __init__(self):
>        self[:] = cards*(4*8)
>        random.shuffle(self)
>    #...
>
> Once you have unittests in place you can try and replace your Card
> implementation with mine and see if your script continues to work correctly.
>
> Last and least:
>
> - I ran your script and got an UnboundLocalError (I'm sorry I didn't keep
> the traceback); there must be a code path in main() where last_card is
> undefined. Try to separate the program logic from the user interface. This
> makes it easier to identify and reproduce problems in the program logic and
> to write tests to prevent them in the future.
>
> - random.choice() takes a list argument
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Fantastic! I will check all those out.
Thanks again


More information about the Tutor mailing list