Skip to content

Instantly share code, notes, and snippets.

@mgardne8
Created June 7, 2017 19:41
Show Gist options
  • Save mgardne8/5edacf1d689f35762b6b9457a377caa6 to your computer and use it in GitHub Desktop.
Save mgardne8/5edacf1d689f35762b6b9457a377caa6 to your computer and use it in GitHub Desktop.
Why am i doing this?
"""
Card class for Cards Cards Cards Cards......
"""
from .exceptions import CompareError
from .pile import Pile
from .enums import Values as VALUES
from enum import Enum
class Card(object):
"""Todo: Docstrings
"""
__slots__ = ["suit", "value", "_enum"]
def __init__(self, suit: str=None, *, value: int, _value_enum: Enum = VALUES):
"""Todo: Docstrings
"""
self.suit = suit
self.value = value
self._enum = _value_enum
def __str__(self):
res = "{}".format(self._enum(self.value).name)
if suit is not None:
res += " of {}".format(self.suit)
return res
def __int__(self):
return self.value
def __bool__(self):
return True # :shrug:
def __lt__(self, other):
if isinstance(other,(int,float,Card)):
return self.value < int(other)
raise CompareError(self, other)
def __le__(self, other):
if isinstance(other,(int,float,Card)):
return self.value <= int(other)
raise CompareError(self, other)
def __eq__(self, other):
if isinstance(other,(int,float,Card)):
return self.value == int(other)
raise CompareError(self, other)
def __ne__(self, other):
if isinstance(other,(int,float,Card)):
return self.value != int(other)
raise CompareError(self, other)
def __ge__(self, other):
if isinstance(other,(int,float,Card)):
return self.value >= int(other)
raise CompareError(self, other)
def __gt__(self, other):
if isinstance(other,(int,float,Card)):
return self.value > int(other)
raise CompareError(self, other)
def __len__(self):
return 1 # :shrug: again
def __iter__(self):
raise StopIteration('cannot iterate Object with type: {}'.format(type(self)))
def __sub__(self, other):
raise ArithmaticError('cannot subtract from an object of type: {}'.format(type(self)))
def __add__(self, other):
if isinstance(other, Pile): # if given a Pile-like-object, combind them
return other + self
elif isinstance(other, Card): # if given another card, create a Pile
return Pile(cards=(self,other))
else:
raise TypeError('cannot combind objects of types: {} and {}'.format(type(self),type(other)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment