Guest User

Untitled

a guest
Apr 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. >>> from baralho import Baralho
  5. >>> b = Baralho()
  6. >>> b[0]
  7. <A de copas>
  8. >>> b[:3]
  9. [<A de copas>, <2 de copas>, <3 de copas>]
  10. >>> b[-3:]
  11. [<J de paus>, <Q de paus>, <K de paus>]
  12. >>> for carta in b: # doctest:+ELLIPSIS
  13. ... print carta
  14. <A de copas>
  15. <2 de copas>
  16. <3 de copas>
  17. <4 de copas>
  18. <5 de copas>
  19. ...
  20. >>> for carta in reversed(b): # doctest:+ELLIPSIS
  21. ... print carta
  22. <K de paus>
  23. <Q de paus>
  24. <J de paus>
  25. <10 de paus>
  26. ...
  27. >>>
  28.  
  29. '''
  30.  
  31. from random import shuffle
  32.  
  33. class Carta(object):
  34.     def __init__(self, valor, naipe):
  35.         self.valor = valor
  36.         self.naipe = naipe
  37.  
  38.     def __repr__(self):
  39.         return '<%s de %s>' % (self.valor, self.naipe)
  40.  
  41. class Baralho(object):
  42.     naipes = 'copas ouros espadas paus'.split()
  43.     valores = 'A 2 3 4 5 6 7 8 9 10 J Q K'.split()
  44.  
  45.     def __init__(self):
  46.         self.cartas = [Carta(v, n)
  47.                         for n in self.naipes
  48.                         for v in self.valores]
  49.  
  50.     def __getitem__(self, pos):
  51.         return self.cartas[pos]
  52.  
  53.     def __len__(self):
  54.         return len(self.cartas)
  55.        
  56. class Baralho_Inverso(Baralho):
  57.  
  58.    def __init__(self):
  59.       super(Baralho_Inverso, self).__init__()
  60.       self.cartas = [x for x in reversed(self.cartas)]
  61.  
  62.    def __iter__(self):
  63.       '''yield self.cartas #achei que deveria ser assim'''
  64.       return (carta for carta in self.cartas) #mas encontrei assim no código do Luciano Ramalho
  65.  
  66. baralho_inverso = Baralho_Inverso()
  67. for carta in baralho_inverso.cartas:
  68.     print carta
Add Comment
Please, Sign In to add comment