michaelmeyer
michaelmeyer

Reputation: 8215

Represent a table as object

Is there a standard way to represent a table which contains some relational data in Python ? I mean, something like this:

      Singular   Plural
1st.  I make     we make
2nd.  you make   you make
3d.   he makes   they make

I would like the data to be accessible both by rows and by columns, like this:

1st. Singular -> I make
1st.          -> I make, we make
Plural 3d.    -> they make
Plural        -> we make, you make, they make

I can't see any way to store the data efficiently, without redundancy. The better I can think of is to use multiple dictionaries (one per row and one per column), which would each contain as many keys as there are rows or columns associated to the dictionary itself, plus one special key which would contain all the associated values.

I guess something like this has already been addressed, that's why I ask.

Upvotes: 3

Views: 2778

Answers (2)

Fred Foo
Fred Foo

Reputation: 363818

As an alternative to my other answer, you can use namedtuple as suggested by @jamylak:

from collections import namedtuple

class Verb(namedtuple("_Verb",  # arbitrary class name/tag
                      ["singular1", "singular2", "singular3",
                       "plural1", "plural2", "plural3"])):
    @property
    def singular(self):
        return (self.singular1, self.singular2, self.singular3)

    # similarly for plural

    @property
    def first_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__.

Upvotes: 4

Fred Foo
Fred Foo

Reputation: 363818

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.

Upvotes: 2

Related Questions