Using Enums in a Class
$begingroup$
I wrote a card class a while back in this post here: Previous Question
I know it has been a long time but I recently came back to the project and wrote it using an ENUM as suggested in the answer to increase readability and to make input less error prone. I've never really used enums in python so the question is did I do it correctly or is there a better way to do it?
from enum import Enum
class Suit(Enum):
CLUB, HEART, DIAMOND, SPADE = range(1, 5)
class Rank(Enum):
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)
JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'
class Card(object):
"""Models a playing card, each Card object will have a suit, rank, and weight associated with each.
possible_suits -- List of possible suits a card object can have
possible_ranks -- List of possible ranks a card object can have
Suit and rank weights are initialized by position in list.
If card parameters are outside of expected values, card becomes joker with zero weight
"""
def __init__(self, suit, rank, in_deck = False):
if suit in Suit and rank in Rank:
self.suit = suit
self.rank = rank
self.suit_weight = suit.value
self.rank_weight = rank.value
else:
self.suit = "Joker"
self.rank = "J"
self.suit_weight = 0
self.rank_weight = 0
self.in_deck = in_deck
def __str__(self):
"""Returns abbreviated name of card
Example: str(Card('Spades', 'A') outputs 'AS'
"""
return str(self.rank.value) + str(self.suit.name[0])
def __eq__(self, other):
"""Return True if cards are equal by suit and rank weight"""
return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight
def __gt__(self, other):
"""Returns true if first card is greater than second card by weight"""
if self.suit_weight > other.suit_weight:
return True
if self.suit_weight == other.suit_weight:
if self.rank_weight > other.rank_weight:
return True
return False
def modify_weight(self, new_suit_weight = None, new_rank_weight = None):
"""Modifies weight of card object"""
if new_suit_weight:
self.suit_weight = new_suit_weight
if new_rank_weight:
self.rank_weight = new_rank_weight
def is_in_deck(self):
"""Return True if card is in a deck, else false"""
return self.in_deck
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def get_suit_weight(self):
return self.suit_weight
def get_rank_weight(self):
return self.rank_weight
object-oriented enum
$endgroup$
add a comment |
$begingroup$
I wrote a card class a while back in this post here: Previous Question
I know it has been a long time but I recently came back to the project and wrote it using an ENUM as suggested in the answer to increase readability and to make input less error prone. I've never really used enums in python so the question is did I do it correctly or is there a better way to do it?
from enum import Enum
class Suit(Enum):
CLUB, HEART, DIAMOND, SPADE = range(1, 5)
class Rank(Enum):
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)
JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'
class Card(object):
"""Models a playing card, each Card object will have a suit, rank, and weight associated with each.
possible_suits -- List of possible suits a card object can have
possible_ranks -- List of possible ranks a card object can have
Suit and rank weights are initialized by position in list.
If card parameters are outside of expected values, card becomes joker with zero weight
"""
def __init__(self, suit, rank, in_deck = False):
if suit in Suit and rank in Rank:
self.suit = suit
self.rank = rank
self.suit_weight = suit.value
self.rank_weight = rank.value
else:
self.suit = "Joker"
self.rank = "J"
self.suit_weight = 0
self.rank_weight = 0
self.in_deck = in_deck
def __str__(self):
"""Returns abbreviated name of card
Example: str(Card('Spades', 'A') outputs 'AS'
"""
return str(self.rank.value) + str(self.suit.name[0])
def __eq__(self, other):
"""Return True if cards are equal by suit and rank weight"""
return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight
def __gt__(self, other):
"""Returns true if first card is greater than second card by weight"""
if self.suit_weight > other.suit_weight:
return True
if self.suit_weight == other.suit_weight:
if self.rank_weight > other.rank_weight:
return True
return False
def modify_weight(self, new_suit_weight = None, new_rank_weight = None):
"""Modifies weight of card object"""
if new_suit_weight:
self.suit_weight = new_suit_weight
if new_rank_weight:
self.rank_weight = new_rank_weight
def is_in_deck(self):
"""Return True if card is in a deck, else false"""
return self.in_deck
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def get_suit_weight(self):
return self.suit_weight
def get_rank_weight(self):
return self.rank_weight
object-oriented enum
$endgroup$
add a comment |
$begingroup$
I wrote a card class a while back in this post here: Previous Question
I know it has been a long time but I recently came back to the project and wrote it using an ENUM as suggested in the answer to increase readability and to make input less error prone. I've never really used enums in python so the question is did I do it correctly or is there a better way to do it?
from enum import Enum
class Suit(Enum):
CLUB, HEART, DIAMOND, SPADE = range(1, 5)
class Rank(Enum):
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)
JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'
class Card(object):
"""Models a playing card, each Card object will have a suit, rank, and weight associated with each.
possible_suits -- List of possible suits a card object can have
possible_ranks -- List of possible ranks a card object can have
Suit and rank weights are initialized by position in list.
If card parameters are outside of expected values, card becomes joker with zero weight
"""
def __init__(self, suit, rank, in_deck = False):
if suit in Suit and rank in Rank:
self.suit = suit
self.rank = rank
self.suit_weight = suit.value
self.rank_weight = rank.value
else:
self.suit = "Joker"
self.rank = "J"
self.suit_weight = 0
self.rank_weight = 0
self.in_deck = in_deck
def __str__(self):
"""Returns abbreviated name of card
Example: str(Card('Spades', 'A') outputs 'AS'
"""
return str(self.rank.value) + str(self.suit.name[0])
def __eq__(self, other):
"""Return True if cards are equal by suit and rank weight"""
return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight
def __gt__(self, other):
"""Returns true if first card is greater than second card by weight"""
if self.suit_weight > other.suit_weight:
return True
if self.suit_weight == other.suit_weight:
if self.rank_weight > other.rank_weight:
return True
return False
def modify_weight(self, new_suit_weight = None, new_rank_weight = None):
"""Modifies weight of card object"""
if new_suit_weight:
self.suit_weight = new_suit_weight
if new_rank_weight:
self.rank_weight = new_rank_weight
def is_in_deck(self):
"""Return True if card is in a deck, else false"""
return self.in_deck
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def get_suit_weight(self):
return self.suit_weight
def get_rank_weight(self):
return self.rank_weight
object-oriented enum
$endgroup$
I wrote a card class a while back in this post here: Previous Question
I know it has been a long time but I recently came back to the project and wrote it using an ENUM as suggested in the answer to increase readability and to make input less error prone. I've never really used enums in python so the question is did I do it correctly or is there a better way to do it?
from enum import Enum
class Suit(Enum):
CLUB, HEART, DIAMOND, SPADE = range(1, 5)
class Rank(Enum):
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)
JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'
class Card(object):
"""Models a playing card, each Card object will have a suit, rank, and weight associated with each.
possible_suits -- List of possible suits a card object can have
possible_ranks -- List of possible ranks a card object can have
Suit and rank weights are initialized by position in list.
If card parameters are outside of expected values, card becomes joker with zero weight
"""
def __init__(self, suit, rank, in_deck = False):
if suit in Suit and rank in Rank:
self.suit = suit
self.rank = rank
self.suit_weight = suit.value
self.rank_weight = rank.value
else:
self.suit = "Joker"
self.rank = "J"
self.suit_weight = 0
self.rank_weight = 0
self.in_deck = in_deck
def __str__(self):
"""Returns abbreviated name of card
Example: str(Card('Spades', 'A') outputs 'AS'
"""
return str(self.rank.value) + str(self.suit.name[0])
def __eq__(self, other):
"""Return True if cards are equal by suit and rank weight"""
return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight
def __gt__(self, other):
"""Returns true if first card is greater than second card by weight"""
if self.suit_weight > other.suit_weight:
return True
if self.suit_weight == other.suit_weight:
if self.rank_weight > other.rank_weight:
return True
return False
def modify_weight(self, new_suit_weight = None, new_rank_weight = None):
"""Modifies weight of card object"""
if new_suit_weight:
self.suit_weight = new_suit_weight
if new_rank_weight:
self.rank_weight = new_rank_weight
def is_in_deck(self):
"""Return True if card is in a deck, else false"""
return self.in_deck
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def get_suit_weight(self):
return self.suit_weight
def get_rank_weight(self):
return self.rank_weight
object-oriented enum
object-oriented enum
asked 21 mins ago
CE3601CE3601
235
235
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214582%2fusing-enums-in-a-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214582%2fusing-enums-in-a-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown