Multi-lang support over multiple classes/enums inside self.__str__
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
Context:
Coming from C# I am building a python 3.6 texas hold'em to get acquainted with different python features.
I am currently buidling enums
for card-colors and -faces and classes for cards and DeckOfCards etc.
DeckOfCards -> 52 * Card -> (CardColor, CardFace)
I want the cards to print themself using different ("en","de", ..) languages. Each class has it's own dict of languages and some setLang/getLang method to change it. Each class __str__()
uses the dict to output itself differently depending on the __lang__
set for each class.
setLang(..)
propagates down from DeckOfCards
->Card
->CardColor
->CardFace
- this is tedious.
I was considering a "global" Translater for all classes that can be used inside each classâÂÂs self.__str__
method.
How would one provide multi-lang support 'usually' in python?
remarks:
- I glanced through PEP 8 but I am not going 100% conform with it - using my C# style mostly
- I haven't found a suitable tag for multi-lang, globalisation or translation
CardColor
from enum import Enum,unique
@unique
class CardColor(Enum):
Pikes = 4
Hearts = 3
Tiles = 2
Clovers = 1
__lang__ = "de"
__nameDict__ = "de" : Pikes: "Pik", Hearst: "Herz", Tiles: "Karo", Clovers: "Kreuz",
"en" : Pikes: "Pikes", Hearst: "Hearts", Tiles:"Tiles", Clovers: "Clovers"
def getLang(self):
'''returns available languages'''
return self.__nameDict__.keys()[:]
def setLang(self, lang):
'''sets the language of this instance'''
if lang in self.__nameDict__:
self.__lang__ = lang
def __str__(self):
'''returns the DisplayName'''
return '0'.format(self.__nameDict__[self.__lang__][self.value])
CardFace
from enum import Enum,unique
@unique
class CardFace(Enum):
Two = 2
Three = 3
Four = 4
Five = 5
Six = 6
Seven = 7
Eight = 8
Nine = 9
Ten = 10
Jack = 11
Queen = 12
King = 13
Ace = 14
__lang__ = ""
__nameDict__ = "de" : Two : "Zwei", Three : "Drei", Four : "Vier", Five : "Fünf", Six : "Sechs", Seven : "Sieben", Eight : "Acht",
Nine : "Neun", Ten : "Zehn", Jack : "Bube", Queen : "Dame", King : "König", Ace : "As" ,
"en" : Two : "Two", Three : "Three", Four : "Four", Five : "Five", Six : "Six", Seven : "Seven", Eight : "Eight",
Nine : "Nine", Ten : "Ten", Jack : "Jack",Queen : "Queen",King : "King", Ace : "Ace"
def getLang(self):
'''returns available languages'''
return self.__nameDict__.keys()
def setLang(self, lang):
'''sets the language of this instance'''
if lang in self.__nameDict__:
self.__lang__ = lang
def __str__(self):
'''returns the DisplayName'''
return '0'.format(self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en'))[self.value])
Card
from enum import Enum,unique
from cardcolor import CardColor
from cardface import CardFace
class Card:
__lang__ = "de"
__nameDict__ = "de" : "1 0", "en" : "0 of 1"
def __init__(self, cardColor, cardValue):
self.c = cardColor
self.v = cardValue
def __str__(self):
'''returns the cards DisplayName'''
return self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en')).format(self.v, self.c)
def setLang(self, lang):
if (lang in self.__nameDict__) and lang in self.v.getLang() and lang in self.c.getLang() :
self.__lang__ = lang
self.v.setLang(lang)
self.c.setLang(lang)
DeckOfCards
from card import Card
from cardcolor import CardColor
from cardface import CardFace
from random import shuffle
class DeckOfCards:
__lang__ = ""
__nameDict__ = "de" : "Kartenspiel", "en" : "Deck of Cards"
__deck__ = None
def __init__(self):
self.__deck__= self.__newDeck__()
self.__shuffle__()
def __shuffle__(self):
shuffle(self.__deck__)
def __newDeck__(self):
return [Card(c,v) for c in CardColor for v in CardFace]
def __str__(self):
'''returns the cards DisplayName'''
return "0:n 12".format(self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en')),
''.join([', '.join([str(c) for c in self.__deck__[x:x+6]])+',n ' for x in range(0,len(self.__deck__), 6) ]).strip().strip(",n"),
"n")
def Cards(self):
return self.__deck__[:]
def setLang(self, lang):
if (lang in self.__nameDict__): # Todo: check if all support lang
self.__lang__ = lang
for c in self.__deck__:
c.setLang(lang)
@staticmethod
def printOneShuffledDeck(lang=None):
d = DeckOfCards()
if lang:
d.setLang(lang)
print(d)
Testprintt names
from deckofcards import DeckOfCards
DeckOfCards.printOneShuffledDeck("en")
DeckOfCards.printOneShuffledDeck("de")
Output:
Deck of Cards:
Jack of Hearts, Nine of Hearts, King of Diamonds, Eight of Hearts, Seven of Diamonds, Eight of Clovers,
Five of Hearts, Six of Clovers, Seven of Pikes, Nine of Clovers, Six of Hearts, Three of Diamonds,
Five of Clovers, Eight of Diamonds, Three of Pikes, Six of Diamonds, Three of Clovers, Ten of Pikes,
Three of Hearts, Jack of Pikes, Five of Diamonds, Eight of Pikes, Queen of Diamonds, Two of Pikes,
Queen of Hearts, Ace of Hearts, Two of Diamonds, Five of Pikes, Four of Diamonds, Four of Clovers,
Seven of Clovers, Two of Clovers, Nine of Pikes, Ten of Hearts, Queen of Clovers, Six of Pikes,
King of Hearts, King of Clovers, Ten of Diamonds, Four of Hearts, Ace of Clovers, Two of Hearts,
Jack of Clovers, Queen of Pikes, Ace of Diamonds, King of Pikes, Nine of Diamonds, Jack of Diamonds,
Ace of Pikes, Four of Pikes, Ten of Clovers, Seven of Hearts
Kartenspiel:
Karo König, Pik Sieben, Herz Vier, Pik As, Kreuz Bube, Herz Bube,
Herz Fünf, Herz Sieben, Pik Sechs, Pik Acht, Herz Dame, Karo Neun,
Kreuz Drei, Karo Zwei, Karo Zehn, Kreuz König, Karo Sieben, Kreuz Neun,
Kreuz As, Pik König, Karo Acht, Herz Sechs, Karo Bube, Pik Zehn,
Kreuz Sieben, Kreuz Zwei, Herz As, Karo Drei, Karo As, Pik Drei,
Herz König, Pik Dame, Kreuz Fünf, Pik Vier, Herz Zwei, Pik Fünf,
Kreuz Acht, Herz Neun, Pik Zwei, Kreuz Zehn, Kreuz Dame, Karo Fünf,
Herz Zehn, Kreuz Sechs, Kreuz Vier, Herz Drei, Karo Sechs, Karo Dame,
Herz Acht, Pik Neun, Pik Bube, Karo Vier
python python-3.x
 |Â
show 7 more comments
up vote
4
down vote
favorite
Context:
Coming from C# I am building a python 3.6 texas hold'em to get acquainted with different python features.
I am currently buidling enums
for card-colors and -faces and classes for cards and DeckOfCards etc.
DeckOfCards -> 52 * Card -> (CardColor, CardFace)
I want the cards to print themself using different ("en","de", ..) languages. Each class has it's own dict of languages and some setLang/getLang method to change it. Each class __str__()
uses the dict to output itself differently depending on the __lang__
set for each class.
setLang(..)
propagates down from DeckOfCards
->Card
->CardColor
->CardFace
- this is tedious.
I was considering a "global" Translater for all classes that can be used inside each classâÂÂs self.__str__
method.
How would one provide multi-lang support 'usually' in python?
remarks:
- I glanced through PEP 8 but I am not going 100% conform with it - using my C# style mostly
- I haven't found a suitable tag for multi-lang, globalisation or translation
CardColor
from enum import Enum,unique
@unique
class CardColor(Enum):
Pikes = 4
Hearts = 3
Tiles = 2
Clovers = 1
__lang__ = "de"
__nameDict__ = "de" : Pikes: "Pik", Hearst: "Herz", Tiles: "Karo", Clovers: "Kreuz",
"en" : Pikes: "Pikes", Hearst: "Hearts", Tiles:"Tiles", Clovers: "Clovers"
def getLang(self):
'''returns available languages'''
return self.__nameDict__.keys()[:]
def setLang(self, lang):
'''sets the language of this instance'''
if lang in self.__nameDict__:
self.__lang__ = lang
def __str__(self):
'''returns the DisplayName'''
return '0'.format(self.__nameDict__[self.__lang__][self.value])
CardFace
from enum import Enum,unique
@unique
class CardFace(Enum):
Two = 2
Three = 3
Four = 4
Five = 5
Six = 6
Seven = 7
Eight = 8
Nine = 9
Ten = 10
Jack = 11
Queen = 12
King = 13
Ace = 14
__lang__ = ""
__nameDict__ = "de" : Two : "Zwei", Three : "Drei", Four : "Vier", Five : "Fünf", Six : "Sechs", Seven : "Sieben", Eight : "Acht",
Nine : "Neun", Ten : "Zehn", Jack : "Bube", Queen : "Dame", King : "König", Ace : "As" ,
"en" : Two : "Two", Three : "Three", Four : "Four", Five : "Five", Six : "Six", Seven : "Seven", Eight : "Eight",
Nine : "Nine", Ten : "Ten", Jack : "Jack",Queen : "Queen",King : "King", Ace : "Ace"
def getLang(self):
'''returns available languages'''
return self.__nameDict__.keys()
def setLang(self, lang):
'''sets the language of this instance'''
if lang in self.__nameDict__:
self.__lang__ = lang
def __str__(self):
'''returns the DisplayName'''
return '0'.format(self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en'))[self.value])
Card
from enum import Enum,unique
from cardcolor import CardColor
from cardface import CardFace
class Card:
__lang__ = "de"
__nameDict__ = "de" : "1 0", "en" : "0 of 1"
def __init__(self, cardColor, cardValue):
self.c = cardColor
self.v = cardValue
def __str__(self):
'''returns the cards DisplayName'''
return self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en')).format(self.v, self.c)
def setLang(self, lang):
if (lang in self.__nameDict__) and lang in self.v.getLang() and lang in self.c.getLang() :
self.__lang__ = lang
self.v.setLang(lang)
self.c.setLang(lang)
DeckOfCards
from card import Card
from cardcolor import CardColor
from cardface import CardFace
from random import shuffle
class DeckOfCards:
__lang__ = ""
__nameDict__ = "de" : "Kartenspiel", "en" : "Deck of Cards"
__deck__ = None
def __init__(self):
self.__deck__= self.__newDeck__()
self.__shuffle__()
def __shuffle__(self):
shuffle(self.__deck__)
def __newDeck__(self):
return [Card(c,v) for c in CardColor for v in CardFace]
def __str__(self):
'''returns the cards DisplayName'''
return "0:n 12".format(self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en')),
''.join([', '.join([str(c) for c in self.__deck__[x:x+6]])+',n ' for x in range(0,len(self.__deck__), 6) ]).strip().strip(",n"),
"n")
def Cards(self):
return self.__deck__[:]
def setLang(self, lang):
if (lang in self.__nameDict__): # Todo: check if all support lang
self.__lang__ = lang
for c in self.__deck__:
c.setLang(lang)
@staticmethod
def printOneShuffledDeck(lang=None):
d = DeckOfCards()
if lang:
d.setLang(lang)
print(d)
Testprintt names
from deckofcards import DeckOfCards
DeckOfCards.printOneShuffledDeck("en")
DeckOfCards.printOneShuffledDeck("de")
Output:
Deck of Cards:
Jack of Hearts, Nine of Hearts, King of Diamonds, Eight of Hearts, Seven of Diamonds, Eight of Clovers,
Five of Hearts, Six of Clovers, Seven of Pikes, Nine of Clovers, Six of Hearts, Three of Diamonds,
Five of Clovers, Eight of Diamonds, Three of Pikes, Six of Diamonds, Three of Clovers, Ten of Pikes,
Three of Hearts, Jack of Pikes, Five of Diamonds, Eight of Pikes, Queen of Diamonds, Two of Pikes,
Queen of Hearts, Ace of Hearts, Two of Diamonds, Five of Pikes, Four of Diamonds, Four of Clovers,
Seven of Clovers, Two of Clovers, Nine of Pikes, Ten of Hearts, Queen of Clovers, Six of Pikes,
King of Hearts, King of Clovers, Ten of Diamonds, Four of Hearts, Ace of Clovers, Two of Hearts,
Jack of Clovers, Queen of Pikes, Ace of Diamonds, King of Pikes, Nine of Diamonds, Jack of Diamonds,
Ace of Pikes, Four of Pikes, Ten of Clovers, Seven of Hearts
Kartenspiel:
Karo König, Pik Sieben, Herz Vier, Pik As, Kreuz Bube, Herz Bube,
Herz Fünf, Herz Sieben, Pik Sechs, Pik Acht, Herz Dame, Karo Neun,
Kreuz Drei, Karo Zwei, Karo Zehn, Kreuz König, Karo Sieben, Kreuz Neun,
Kreuz As, Pik König, Karo Acht, Herz Sechs, Karo Bube, Pik Zehn,
Kreuz Sieben, Kreuz Zwei, Herz As, Karo Drei, Karo As, Pik Drei,
Herz König, Pik Dame, Kreuz Fünf, Pik Vier, Herz Zwei, Pik Fünf,
Kreuz Acht, Herz Neun, Pik Zwei, Kreuz Zehn, Kreuz Dame, Karo Fünf,
Herz Zehn, Kreuz Sechs, Kreuz Vier, Herz Drei, Karo Sechs, Karo Dame,
Herz Acht, Pik Neun, Pik Bube, Karo Vier
python python-3.x
1
This question is off-topic ( asking about code not yet written ). Also: don't create your own magic methods!
â Daniel
Jan 7 at 10:47
glanced through PEP 8
check whether your IDE supports PEP 8 hinting (much the same way the spelling checker you don't seem to be using would). Have a look at docstring/PEP 257, too.
â greybeard
Jan 7 at 10:49
1
@Coal_ What I have works, it is just tedious - and there may be smarter ways to do it - what I thought that codereview is about...
â Patrick Artner
Jan 7 at 11:20
1
I once asked a question about localization and got good insights.
â Mathias Ettinger
Jan 7 at 11:26
2
spelling checker
: have your computer warn you aboutaquainted
,buidling
,themselfs
,Translater
,eachs
,throuhg
,stlye
,havent
,mulit
, evencolor
guessing you learned English closer to Merry Old than to God Bless.
â greybeard
Jan 7 at 11:53
 |Â
show 7 more comments
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Context:
Coming from C# I am building a python 3.6 texas hold'em to get acquainted with different python features.
I am currently buidling enums
for card-colors and -faces and classes for cards and DeckOfCards etc.
DeckOfCards -> 52 * Card -> (CardColor, CardFace)
I want the cards to print themself using different ("en","de", ..) languages. Each class has it's own dict of languages and some setLang/getLang method to change it. Each class __str__()
uses the dict to output itself differently depending on the __lang__
set for each class.
setLang(..)
propagates down from DeckOfCards
->Card
->CardColor
->CardFace
- this is tedious.
I was considering a "global" Translater for all classes that can be used inside each classâÂÂs self.__str__
method.
How would one provide multi-lang support 'usually' in python?
remarks:
- I glanced through PEP 8 but I am not going 100% conform with it - using my C# style mostly
- I haven't found a suitable tag for multi-lang, globalisation or translation
CardColor
from enum import Enum,unique
@unique
class CardColor(Enum):
Pikes = 4
Hearts = 3
Tiles = 2
Clovers = 1
__lang__ = "de"
__nameDict__ = "de" : Pikes: "Pik", Hearst: "Herz", Tiles: "Karo", Clovers: "Kreuz",
"en" : Pikes: "Pikes", Hearst: "Hearts", Tiles:"Tiles", Clovers: "Clovers"
def getLang(self):
'''returns available languages'''
return self.__nameDict__.keys()[:]
def setLang(self, lang):
'''sets the language of this instance'''
if lang in self.__nameDict__:
self.__lang__ = lang
def __str__(self):
'''returns the DisplayName'''
return '0'.format(self.__nameDict__[self.__lang__][self.value])
CardFace
from enum import Enum,unique
@unique
class CardFace(Enum):
Two = 2
Three = 3
Four = 4
Five = 5
Six = 6
Seven = 7
Eight = 8
Nine = 9
Ten = 10
Jack = 11
Queen = 12
King = 13
Ace = 14
__lang__ = ""
__nameDict__ = "de" : Two : "Zwei", Three : "Drei", Four : "Vier", Five : "Fünf", Six : "Sechs", Seven : "Sieben", Eight : "Acht",
Nine : "Neun", Ten : "Zehn", Jack : "Bube", Queen : "Dame", King : "König", Ace : "As" ,
"en" : Two : "Two", Three : "Three", Four : "Four", Five : "Five", Six : "Six", Seven : "Seven", Eight : "Eight",
Nine : "Nine", Ten : "Ten", Jack : "Jack",Queen : "Queen",King : "King", Ace : "Ace"
def getLang(self):
'''returns available languages'''
return self.__nameDict__.keys()
def setLang(self, lang):
'''sets the language of this instance'''
if lang in self.__nameDict__:
self.__lang__ = lang
def __str__(self):
'''returns the DisplayName'''
return '0'.format(self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en'))[self.value])
Card
from enum import Enum,unique
from cardcolor import CardColor
from cardface import CardFace
class Card:
__lang__ = "de"
__nameDict__ = "de" : "1 0", "en" : "0 of 1"
def __init__(self, cardColor, cardValue):
self.c = cardColor
self.v = cardValue
def __str__(self):
'''returns the cards DisplayName'''
return self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en')).format(self.v, self.c)
def setLang(self, lang):
if (lang in self.__nameDict__) and lang in self.v.getLang() and lang in self.c.getLang() :
self.__lang__ = lang
self.v.setLang(lang)
self.c.setLang(lang)
DeckOfCards
from card import Card
from cardcolor import CardColor
from cardface import CardFace
from random import shuffle
class DeckOfCards:
__lang__ = ""
__nameDict__ = "de" : "Kartenspiel", "en" : "Deck of Cards"
__deck__ = None
def __init__(self):
self.__deck__= self.__newDeck__()
self.__shuffle__()
def __shuffle__(self):
shuffle(self.__deck__)
def __newDeck__(self):
return [Card(c,v) for c in CardColor for v in CardFace]
def __str__(self):
'''returns the cards DisplayName'''
return "0:n 12".format(self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en')),
''.join([', '.join([str(c) for c in self.__deck__[x:x+6]])+',n ' for x in range(0,len(self.__deck__), 6) ]).strip().strip(",n"),
"n")
def Cards(self):
return self.__deck__[:]
def setLang(self, lang):
if (lang in self.__nameDict__): # Todo: check if all support lang
self.__lang__ = lang
for c in self.__deck__:
c.setLang(lang)
@staticmethod
def printOneShuffledDeck(lang=None):
d = DeckOfCards()
if lang:
d.setLang(lang)
print(d)
Testprintt names
from deckofcards import DeckOfCards
DeckOfCards.printOneShuffledDeck("en")
DeckOfCards.printOneShuffledDeck("de")
Output:
Deck of Cards:
Jack of Hearts, Nine of Hearts, King of Diamonds, Eight of Hearts, Seven of Diamonds, Eight of Clovers,
Five of Hearts, Six of Clovers, Seven of Pikes, Nine of Clovers, Six of Hearts, Three of Diamonds,
Five of Clovers, Eight of Diamonds, Three of Pikes, Six of Diamonds, Three of Clovers, Ten of Pikes,
Three of Hearts, Jack of Pikes, Five of Diamonds, Eight of Pikes, Queen of Diamonds, Two of Pikes,
Queen of Hearts, Ace of Hearts, Two of Diamonds, Five of Pikes, Four of Diamonds, Four of Clovers,
Seven of Clovers, Two of Clovers, Nine of Pikes, Ten of Hearts, Queen of Clovers, Six of Pikes,
King of Hearts, King of Clovers, Ten of Diamonds, Four of Hearts, Ace of Clovers, Two of Hearts,
Jack of Clovers, Queen of Pikes, Ace of Diamonds, King of Pikes, Nine of Diamonds, Jack of Diamonds,
Ace of Pikes, Four of Pikes, Ten of Clovers, Seven of Hearts
Kartenspiel:
Karo König, Pik Sieben, Herz Vier, Pik As, Kreuz Bube, Herz Bube,
Herz Fünf, Herz Sieben, Pik Sechs, Pik Acht, Herz Dame, Karo Neun,
Kreuz Drei, Karo Zwei, Karo Zehn, Kreuz König, Karo Sieben, Kreuz Neun,
Kreuz As, Pik König, Karo Acht, Herz Sechs, Karo Bube, Pik Zehn,
Kreuz Sieben, Kreuz Zwei, Herz As, Karo Drei, Karo As, Pik Drei,
Herz König, Pik Dame, Kreuz Fünf, Pik Vier, Herz Zwei, Pik Fünf,
Kreuz Acht, Herz Neun, Pik Zwei, Kreuz Zehn, Kreuz Dame, Karo Fünf,
Herz Zehn, Kreuz Sechs, Kreuz Vier, Herz Drei, Karo Sechs, Karo Dame,
Herz Acht, Pik Neun, Pik Bube, Karo Vier
python python-3.x
Context:
Coming from C# I am building a python 3.6 texas hold'em to get acquainted with different python features.
I am currently buidling enums
for card-colors and -faces and classes for cards and DeckOfCards etc.
DeckOfCards -> 52 * Card -> (CardColor, CardFace)
I want the cards to print themself using different ("en","de", ..) languages. Each class has it's own dict of languages and some setLang/getLang method to change it. Each class __str__()
uses the dict to output itself differently depending on the __lang__
set for each class.
setLang(..)
propagates down from DeckOfCards
->Card
->CardColor
->CardFace
- this is tedious.
I was considering a "global" Translater for all classes that can be used inside each classâÂÂs self.__str__
method.
How would one provide multi-lang support 'usually' in python?
remarks:
- I glanced through PEP 8 but I am not going 100% conform with it - using my C# style mostly
- I haven't found a suitable tag for multi-lang, globalisation or translation
CardColor
from enum import Enum,unique
@unique
class CardColor(Enum):
Pikes = 4
Hearts = 3
Tiles = 2
Clovers = 1
__lang__ = "de"
__nameDict__ = "de" : Pikes: "Pik", Hearst: "Herz", Tiles: "Karo", Clovers: "Kreuz",
"en" : Pikes: "Pikes", Hearst: "Hearts", Tiles:"Tiles", Clovers: "Clovers"
def getLang(self):
'''returns available languages'''
return self.__nameDict__.keys()[:]
def setLang(self, lang):
'''sets the language of this instance'''
if lang in self.__nameDict__:
self.__lang__ = lang
def __str__(self):
'''returns the DisplayName'''
return '0'.format(self.__nameDict__[self.__lang__][self.value])
CardFace
from enum import Enum,unique
@unique
class CardFace(Enum):
Two = 2
Three = 3
Four = 4
Five = 5
Six = 6
Seven = 7
Eight = 8
Nine = 9
Ten = 10
Jack = 11
Queen = 12
King = 13
Ace = 14
__lang__ = ""
__nameDict__ = "de" : Two : "Zwei", Three : "Drei", Four : "Vier", Five : "Fünf", Six : "Sechs", Seven : "Sieben", Eight : "Acht",
Nine : "Neun", Ten : "Zehn", Jack : "Bube", Queen : "Dame", King : "König", Ace : "As" ,
"en" : Two : "Two", Three : "Three", Four : "Four", Five : "Five", Six : "Six", Seven : "Seven", Eight : "Eight",
Nine : "Nine", Ten : "Ten", Jack : "Jack",Queen : "Queen",King : "King", Ace : "Ace"
def getLang(self):
'''returns available languages'''
return self.__nameDict__.keys()
def setLang(self, lang):
'''sets the language of this instance'''
if lang in self.__nameDict__:
self.__lang__ = lang
def __str__(self):
'''returns the DisplayName'''
return '0'.format(self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en'))[self.value])
Card
from enum import Enum,unique
from cardcolor import CardColor
from cardface import CardFace
class Card:
__lang__ = "de"
__nameDict__ = "de" : "1 0", "en" : "0 of 1"
def __init__(self, cardColor, cardValue):
self.c = cardColor
self.v = cardValue
def __str__(self):
'''returns the cards DisplayName'''
return self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en')).format(self.v, self.c)
def setLang(self, lang):
if (lang in self.__nameDict__) and lang in self.v.getLang() and lang in self.c.getLang() :
self.__lang__ = lang
self.v.setLang(lang)
self.c.setLang(lang)
DeckOfCards
from card import Card
from cardcolor import CardColor
from cardface import CardFace
from random import shuffle
class DeckOfCards:
__lang__ = ""
__nameDict__ = "de" : "Kartenspiel", "en" : "Deck of Cards"
__deck__ = None
def __init__(self):
self.__deck__= self.__newDeck__()
self.__shuffle__()
def __shuffle__(self):
shuffle(self.__deck__)
def __newDeck__(self):
return [Card(c,v) for c in CardColor for v in CardFace]
def __str__(self):
'''returns the cards DisplayName'''
return "0:n 12".format(self.__nameDict__.get(self.__lang__, self.__nameDict__.get('en')),
''.join([', '.join([str(c) for c in self.__deck__[x:x+6]])+',n ' for x in range(0,len(self.__deck__), 6) ]).strip().strip(",n"),
"n")
def Cards(self):
return self.__deck__[:]
def setLang(self, lang):
if (lang in self.__nameDict__): # Todo: check if all support lang
self.__lang__ = lang
for c in self.__deck__:
c.setLang(lang)
@staticmethod
def printOneShuffledDeck(lang=None):
d = DeckOfCards()
if lang:
d.setLang(lang)
print(d)
Testprintt names
from deckofcards import DeckOfCards
DeckOfCards.printOneShuffledDeck("en")
DeckOfCards.printOneShuffledDeck("de")
Output:
Deck of Cards:
Jack of Hearts, Nine of Hearts, King of Diamonds, Eight of Hearts, Seven of Diamonds, Eight of Clovers,
Five of Hearts, Six of Clovers, Seven of Pikes, Nine of Clovers, Six of Hearts, Three of Diamonds,
Five of Clovers, Eight of Diamonds, Three of Pikes, Six of Diamonds, Three of Clovers, Ten of Pikes,
Three of Hearts, Jack of Pikes, Five of Diamonds, Eight of Pikes, Queen of Diamonds, Two of Pikes,
Queen of Hearts, Ace of Hearts, Two of Diamonds, Five of Pikes, Four of Diamonds, Four of Clovers,
Seven of Clovers, Two of Clovers, Nine of Pikes, Ten of Hearts, Queen of Clovers, Six of Pikes,
King of Hearts, King of Clovers, Ten of Diamonds, Four of Hearts, Ace of Clovers, Two of Hearts,
Jack of Clovers, Queen of Pikes, Ace of Diamonds, King of Pikes, Nine of Diamonds, Jack of Diamonds,
Ace of Pikes, Four of Pikes, Ten of Clovers, Seven of Hearts
Kartenspiel:
Karo König, Pik Sieben, Herz Vier, Pik As, Kreuz Bube, Herz Bube,
Herz Fünf, Herz Sieben, Pik Sechs, Pik Acht, Herz Dame, Karo Neun,
Kreuz Drei, Karo Zwei, Karo Zehn, Kreuz König, Karo Sieben, Kreuz Neun,
Kreuz As, Pik König, Karo Acht, Herz Sechs, Karo Bube, Pik Zehn,
Kreuz Sieben, Kreuz Zwei, Herz As, Karo Drei, Karo As, Pik Drei,
Herz König, Pik Dame, Kreuz Fünf, Pik Vier, Herz Zwei, Pik Fünf,
Kreuz Acht, Herz Neun, Pik Zwei, Kreuz Zehn, Kreuz Dame, Karo Fünf,
Herz Zehn, Kreuz Sechs, Kreuz Vier, Herz Drei, Karo Sechs, Karo Dame,
Herz Acht, Pik Neun, Pik Bube, Karo Vier
python python-3.x
edited Jan 7 at 13:13
asked Jan 7 at 10:24
Patrick Artner
1214
1214
1
This question is off-topic ( asking about code not yet written ). Also: don't create your own magic methods!
â Daniel
Jan 7 at 10:47
glanced through PEP 8
check whether your IDE supports PEP 8 hinting (much the same way the spelling checker you don't seem to be using would). Have a look at docstring/PEP 257, too.
â greybeard
Jan 7 at 10:49
1
@Coal_ What I have works, it is just tedious - and there may be smarter ways to do it - what I thought that codereview is about...
â Patrick Artner
Jan 7 at 11:20
1
I once asked a question about localization and got good insights.
â Mathias Ettinger
Jan 7 at 11:26
2
spelling checker
: have your computer warn you aboutaquainted
,buidling
,themselfs
,Translater
,eachs
,throuhg
,stlye
,havent
,mulit
, evencolor
guessing you learned English closer to Merry Old than to God Bless.
â greybeard
Jan 7 at 11:53
 |Â
show 7 more comments
1
This question is off-topic ( asking about code not yet written ). Also: don't create your own magic methods!
â Daniel
Jan 7 at 10:47
glanced through PEP 8
check whether your IDE supports PEP 8 hinting (much the same way the spelling checker you don't seem to be using would). Have a look at docstring/PEP 257, too.
â greybeard
Jan 7 at 10:49
1
@Coal_ What I have works, it is just tedious - and there may be smarter ways to do it - what I thought that codereview is about...
â Patrick Artner
Jan 7 at 11:20
1
I once asked a question about localization and got good insights.
â Mathias Ettinger
Jan 7 at 11:26
2
spelling checker
: have your computer warn you aboutaquainted
,buidling
,themselfs
,Translater
,eachs
,throuhg
,stlye
,havent
,mulit
, evencolor
guessing you learned English closer to Merry Old than to God Bless.
â greybeard
Jan 7 at 11:53
1
1
This question is off-topic ( asking about code not yet written ). Also: don't create your own magic methods!
â Daniel
Jan 7 at 10:47
This question is off-topic ( asking about code not yet written ). Also: don't create your own magic methods!
â Daniel
Jan 7 at 10:47
glanced through PEP 8
check whether your IDE supports PEP 8 hinting (much the same way the spelling checker you don't seem to be using would). Have a look at docstring/PEP 257, too.â greybeard
Jan 7 at 10:49
glanced through PEP 8
check whether your IDE supports PEP 8 hinting (much the same way the spelling checker you don't seem to be using would). Have a look at docstring/PEP 257, too.â greybeard
Jan 7 at 10:49
1
1
@Coal_ What I have works, it is just tedious - and there may be smarter ways to do it - what I thought that codereview is about...
â Patrick Artner
Jan 7 at 11:20
@Coal_ What I have works, it is just tedious - and there may be smarter ways to do it - what I thought that codereview is about...
â Patrick Artner
Jan 7 at 11:20
1
1
I once asked a question about localization and got good insights.
â Mathias Ettinger
Jan 7 at 11:26
I once asked a question about localization and got good insights.
â Mathias Ettinger
Jan 7 at 11:26
2
2
spelling checker
: have your computer warn you about aquainted
, buidling
, themselfs
, Translater
, eachs
, throuhg
, stlye
, havent
, mulit
, even color
guessing you learned English closer to Merry Old than to God Bless.â greybeard
Jan 7 at 11:53
spelling checker
: have your computer warn you about aquainted
, buidling
, themselfs
, Translater
, eachs
, throuhg
, stlye
, havent
, mulit
, even color
guessing you learned English closer to Merry Old than to God Bless.â greybeard
Jan 7 at 11:53
 |Â
show 7 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184499%2fmulti-lang-support-over-multiple-classes-enums-inside-self-str%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
This question is off-topic ( asking about code not yet written ). Also: don't create your own magic methods!
â Daniel
Jan 7 at 10:47
glanced through PEP 8
check whether your IDE supports PEP 8 hinting (much the same way the spelling checker you don't seem to be using would). Have a look at docstring/PEP 257, too.â greybeard
Jan 7 at 10:49
1
@Coal_ What I have works, it is just tedious - and there may be smarter ways to do it - what I thought that codereview is about...
â Patrick Artner
Jan 7 at 11:20
1
I once asked a question about localization and got good insights.
â Mathias Ettinger
Jan 7 at 11:26
2
spelling checker
: have your computer warn you aboutaquainted
,buidling
,themselfs
,Translater
,eachs
,throuhg
,stlye
,havent
,mulit
, evencolor
guessing you learned English closer to Merry Old than to God Bless.â greybeard
Jan 7 at 11:53