Card.JSONEncoder added
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from random import shuffle
|
from random import shuffle
|
||||||
|
from json import JSONEncoder
|
||||||
|
|
||||||
class BridzikException(Exception):
|
class BridzikException(Exception):
|
||||||
pass
|
pass
|
||||||
@@ -51,7 +52,7 @@ class Card_values(Enum):
|
|||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
|
||||||
|
|
||||||
class Card():
|
class Card(JSONEncoder):
|
||||||
def __init__(self, color: Card_colors, value: Card_values):
|
def __init__(self, color: Card_colors, value: Card_values):
|
||||||
self.color = color
|
self.color = color
|
||||||
self.value = value
|
self.value = value
|
||||||
@@ -68,6 +69,16 @@ class Card():
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Card {}_{}>'.format(self.color.name, self.value.name)
|
return '<Card {}_{}>'.format(self.color.name, self.value.name)
|
||||||
|
|
||||||
|
class JSONEncoder(JSONEncoder):
|
||||||
|
def default(self, obj):
|
||||||
|
if isinstance(obj, Card):
|
||||||
|
return {
|
||||||
|
'color': obj.color.name,
|
||||||
|
'value': obj.value.name
|
||||||
|
}
|
||||||
|
return JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
|
|
||||||
cards = [Card(color, value) for value in Card_values for color in Card_colors]
|
cards = [Card(color, value) for value in Card_values for color in Card_colors]
|
||||||
|
|
||||||
# for series_number in range(4): # 4 hry
|
# for series_number in range(4): # 4 hry
|
||||||
|
|||||||
Reference in New Issue
Block a user