Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Is this usage appealing to you?</p> <p>Easy look-up and usage of single entries:</p> <pre><code>&gt;&gt;&gt; left("10000") Entry({'parent': '10001', 'key': '10000', 'fkey': 'asdf'}) &gt;&gt;&gt; left("10000")['key'] '10000' &gt;&gt;&gt; left("10000")['parent'] '10001' </code></pre> <p>Easy look-up of parents:</p> <pre><code>&gt;&gt;&gt; left("10000").parent() Entry({'parent': '', 'key': '10001', 'fkey': 'qwer'}) &gt;&gt;&gt; left("10000").parent().parent() &gt;&gt;&gt; left("10001") Entry({'parent': '', 'key': '10001', 'fkey': 'qwer'}) &gt;&gt;&gt; left("10001") is left("10000").parent() True </code></pre> <p>Easy look-up of related entries:</p> <pre><code>&gt;&gt;&gt; left("10001").related() Entry({'parent': '', 'key': 'qwer', 'fkey': '10001'}) &gt;&gt;&gt; right("qwer") Entry({'parent': '', 'key': 'qwer', 'fkey': '10001'}) &gt;&gt;&gt; right(left("10001").related()['key']) Entry({'parent': '', 'key': 'qwer', 'fkey': '10001'}) &gt;&gt;&gt; right("qwer") is left("10001").related() True </code></pre> <p>Particularly here is the example in your question: parent's foreign key:</p> <pre><code>&gt;&gt;&gt; left("10000").parent()['fkey'] 'qwer' </code></pre> <p>If so, then here is the code! Classes:</p> <pre><code>class Entry(object): def __init__(self, dataset, d): self.dataset = dataset self.d = d def parent(self): return self.dataset.parent_of(self) def related(self): if not self.dataset.related_dataset: raise ValueError("no related dataset specified") return self.dataset.related_dataset(self['fkey']) def __getitem__(self, k): return self.d.__getitem__(k) def __repr__(self): return "Entry(%s)" % repr(self.d) def __str__(self): return str(self.d) class Dataset(object): def __init__(self, data): self.data = dict((k, Entry(self, v)) for (k,v) in data.items()) self.related_dataset = None def set_related_dataset(self, dataset): self.related_dataset = dataset def entry(self, key): if isinstance(key, Entry): return key return self.data[key] def __call__(self, key): return self.entry(key) def parent_of(self, entry): entry = self.entry(entry) if not entry['parent']: return None return self.data[entry['parent']] </code></pre> <p>And the usage for the data you've provided:</p> <pre><code>leftdata = {'10000': { 'key': '10000', 'fkey':'asdf', 'parent':'10001'}, '10001': { 'key': '10001', 'fkey':'qwer', 'parent':''},} rightdata = {'asdf': { 'key': 'asdf', 'fkey':'10000', 'parent':'qwer'}, 'qwer': { 'key': 'qwer', 'fkey':'10001', 'parent':''}} left = Dataset(leftdata) right = Dataset(rightdata) left.set_related_dataset(right) right.set_related_dataset(left) </code></pre> <p>Explanation: Wrap each dict value in an <code>Entry</code> class with <code>__getitem__</code> defined to make it usable as a dict (more or less). Have a <code>Dataset</code> class that maps primary keys to these <code>Entry</code>s. Provide the <code>Entry</code> access to this dataset and provide convenient methods <code>.parent()</code> and <code>.related()</code>. In order for <code>.related()</code> to work, set which dataset the "related" one should be with <code>set_related_dataset</code> and it all ties together.</p> <p>Now you can even just pass <code>Entry</code>s and you'll be able to access the related entries without needing to pass both datasets in.</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