0

Let's say, I have the following code:

class SomeChoices(object):
    CHOICE_A = "Sub"
    CHOICE_B = "First Team"

    @classmethod
    def choices(cls):
        return cls.CHOICE_A, cls.CHOICE_B

The comparing string for CHOICE_B can be both First Team or First-Team. What is the best practice to compare them so that both First Team and First-Team would be considered as CHOICE_B?

martineau
  • 119,623
  • 25
  • 170
  • 301
Priom
  • 53
  • 7
  • 2
    The query lacks information. Could you specify the input you pass and the output you require in your question? – Roxy Aug 31 '21 at 17:54
  • The data will come from a csv file and I will compare the data on column level. So, for the column, I am talking about, will make sure if the value is from a choice field and the choice field can have either `CHOICE_A` and `CHOICE_B`. – Priom Aug 31 '21 at 18:03
  • Can you post a [mre]? I don't see how that class is relevant for your question... You're basically asking how to compare `First Team` and `First-Team` as equal. And what is CONST in the title? – Tomerikoo Aug 31 '21 at 18:04
  • Just remove the character(s) you want to ignore. – martineau Aug 31 '21 at 18:23

2 Answers2

0
s in {'First Team', 'First-Team'}

This checks if the string exists in the set efficiently.

bwdm
  • 793
  • 7
  • 17
0

It's a matter of preference. If this is just a static check that you know you won't expand on, you can just implement something quick like bwdm's answer.

If you want to make it expandable, so that you can dynamically add more words to compare with, you're best off keeping multiple sets, each containing viable words.

class SomeChoices(object):
    choices = {
        "A":{"Sub"}, "B":{"First Team", "First-Team"}
    }

Now you can easily check if something is part of a choice, and also expand it if you want.

"First-Team" in SomeChoices.choices["B"]    # True
"First Team" in SomeChoices.choices["B"]    # True
"First_Team" in SomeChoices.choices["B"]    # False

SomeChoices.choices["B"].add("First_Team")
"First_Team" in SomeChoices.choices["B"]    # True

SomeChoises.choices["C"] = {"Second Team"}
"Second Team" in SomeChoices.choices["C"]   # True

This follows the open/close principle, where a module is easy to expand upon without needing to change the code for it.