Playing card class, written using enumsBasic playing card objects for gamesPlaying Card Class - is this...

Why do neural networks need so many training examples to perform?

What's a good word to describe a public place that looks like it wouldn't be rough?

Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?

Isn't using the Extrusion Multiplier like cheating?

If I sold a PS4 game I owned the disc for, can I reinstall it digitally?

What is the most triangles you can make from a capital "H" and 3 straight lines?

Chess tournament winning streaks

Can a dragon be stuck looking like a human?

Notes in a lick that don't fit in the scale associated with the chord

Does Improved Divine Smite trigger when a paladin makes an unarmed strike?

Cat is tipping over bed-side lamps during the night

How do I say "Brexit" in Latin?

Can an insurance company drop you after receiving a bill and refusing to pay?

How should I handle players who ignore the session zero agreement?

Explain the objections to these measures against human trafficking

What makes the Forgotten Realms "forgotten"?

Cryptic with missing capitals

Is a debit card dangerous for an account with low balance and no overdraft protection?

Can a hotel cancel a confirmed reservation?

How would a Dictatorship make a country more successful?

What to do when being responsible for data protection in your lab, yet advice is ignored?

Why zero tolerance on nudity in space?

Do authors have to be politically correct in article-writing?

A universal method for left-hand alignment of a sequence of equalities



Playing card class, written using enums


Basic playing card objects for gamesPlaying Card Class - is this right?Weekend Challenge - Poker Hand EvaluationOptimizing “Poker hands” challenge solutionDetermining winner and winning hand in poker (holdem)Class to represent playing cardDetermine playing cardBeginnings of a Poker hand classifierUsing enums in a card/deck classPlaying card objectBasic playing card objects for games













0












$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









share|improve this question











$endgroup$

















    0












    $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









    share|improve this question











    $endgroup$















      0












      0








      0





      $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









      share|improve this question











      $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






      python object-oriented playing-cards enum






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 5 hours ago









      200_success

      130k16153417




      130k16153417










      asked 6 hours ago









      CE3601CE3601

      235




      235






















          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214582%2fplaying-card-class-written-using-enums%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
















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214582%2fplaying-card-class-written-using-enums%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

          Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...