Represent A Table As Object
Solution 1:
As an alternative to my other answer, you can use namedtuple
as suggested by @jamylak:
from collections import namedtuple
classVerb(namedtuple("_Verb", # arbitrary class name/tag
["singular1", "singular2", "singular3",
"plural1", "plural2", "plural3"])):
@propertydefsingular(self):
return (self.singular1, self.singular2, self.singular3)
# similarly for plural @propertydeffirst_person(self):
return (self.singular1, self.plural1)
# similarly for 2nd and 3rd person
Now "make" can be represented as
Verb("make", "make", "makes", "make", "make", "make")
And again, this can be optimized by leveraging the simplicity of English conjugations.
The downside of this solution is that it doesn't allow changing individual fields in the table, because namedtuple
is immutable. If you want to make changes, use an ordinary class
with __slots__
.
Solution 2:
You can get rid of the redundancy by representing each verb as a flat tuple:
("make", "make", "makes", "make", "make", "make")
Then create a dict
mapping keys to indices:
ind_from_key = {'1st': (0, 3),
...,
'singular': (0, 1, 2),
...,
'1st singular': (0,)}
Of course, lookups get a bit more complicated because you'll have to do an indirect lookup:
def conjugation(verb, conj):
indices = ind_from_key[conj]
return [CONJUGATION[verb][i] for i in indices]
Note that the conjugation of English verbs is simple enough to allow further optimizations; the plural forms are always the same across grammatical persons.
As for the original question: no, there's no single standard way of representing relational data in Python. If your relations get more complicated than verbal conjugations, and you have a lot of data, then you might want to look into SQLite or other database solutions, perhaps in conjunction with SQLAlchemy.
Post a Comment for "Represent A Table As Object"