Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to abstract season/show/episode data
    text
    copied!<p>Basically, I've written an API to www.thetvdb.com in Python. The current code can be found <a href="http://github.com/dbr/tvdb_api/tree/master/tvdb_api.py" rel="nofollow noreferrer">here</a>.</p> <p>It grabs data from the API as requested, and has to store the data somehow, and make it available by doing:</p> <pre><code>print tvdbinstance[1][23]['episodename'] # get the name of episode 23 of season 1 </code></pre> <p>What is the "best" way to abstract this data within the <code>Tvdb()</code> class?</p> <p>I originally used a extended <code>Dict()</code> that automatically created sub-dicts (so you could do <code>x[1][2][3][4] = "something"</code> without having to do <code>if x[1].has_key(2): x[1][2] = []</code> and so on)</p> <p>Then I just stored the data by doing <code>self.data[show_id][season_number][episode_number][attribute_name] = "something"</code></p> <p>This worked okay, but there was no easy way of checking if <code>x[3][24]</code> was supposed to exist or not (so I couldn't raise the season_not_found exception).</p> <p>Currently it's using four classes: <code>ShowContainer</code>, <code>Show</code>, <code>Season</code> and <code>Episode</code>. Each one is a very basic dict, which I can easily add extra functionality in (the <code>search()</code> function on <code>Show()</code> for example). Each has a <code>__setitem__</code>, <code>__getitem_</code> and <code>has_key</code>.</p> <p>This works mostly fine, I can check in Shows if it has that season in it's <code>self.data</code> dict, if not, <code>raise season_not_found</code>. I can also check in <code>Season()</code> if it has that episode and so on.</p> <p>The problem now is it's presenting itself as a dict, but doesn't have all the functionality, and because I'm overriding the <code>__getitem__</code> and <code>__setitem__</code> functions, it's easy to accidentally recursively call <code>__getitem__</code> (so I'm not sure if extending the <code>Dict</code> class will cause problems).</p> <p>The other slight problem is adding data into the dict is a lot more work than the old <code>Dict</code> method (which was <code>self.data[seas_no][ep_no]['attribute'] = 'something'</code>). See <code>_setItem</code> and <code>_setData</code>. It's not too bad, since it's currently only a read-only API interface (so the users of the API should only ever retrieve data, not add more), but it's hardly... Elegant.</p> <p>I think the series-of-classes system is probably the best way, but does anyone have a better idea for storing the data? And would extending the <code>ShowContainer</code>/etc classes with <code>Dict</code> cause problems?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload