I wrote a quick script to scrape various data about mixed martial arts fights and their associated odds.
Originally, the data was a tuple with the first entry being the name of a fighter (string) and the second being their odds (float). The script later accessed this data, and I defined two constants, FIGHTER = 0
and ODDS = 1
so that I later use fight_data[FIGHTER]
or fight_data[ODDS]
.
Since the data is immutable, a tuple made sense, and by defining constants my reasoning was that my IDE/Editor could catch typos as opposed to using a string index for a dictionary.
FIGHTER = 0
ODDS = 1
fight_data = get_data()
def process_data(fight_data):
do_something(fight_data[FIGHTER])
do_something(fight_data[ODDS])
What are the other alternatives? I thought of making a FightData
class, but the data is strictly a value object with two small elements.
class FightData(object):
fighter = None
odds = None
def __init__(self, fighter, odds):
self.fighter = fighter
self.odds = odds
fight_data = get_data()
def process_data(data):
do_something(fight_data.fighter)
do_something(fight_data.odds)
In addition, I realized I could use a dictionary, and have fight_data["fighter"]
but that seems both ugly and unnecessary to me.
Which one of these alternatives is the best?