Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do <strong>not</strong> walk the filesystem (!) and scan the Python source code of the backends! That's an ugly hack at the best of times, and even worse here because you don't need anything like it at all! Registering all the classes on import is perfectly OK. </p> <hr> <p>Store the backends in a class attribute instead of an instance attribute; that way, all <code>Storage</code> instances will look at the same set of backends:</p> <pre><code>&gt;&gt;&gt; class Storage(object): ... backends = set() ... ... def register(self, backend): ... self.backends.add(backend) ... </code></pre> <p>Every backend can register itself by instantiating its own <code>Storage</code>, which has access to the class-level <code>backends</code> attribute:</p> <pre><code>&gt;&gt;&gt; foo = Storage() &gt;&gt;&gt; foo.register("text") &gt;&gt;&gt; bar = Storage() &gt;&gt;&gt; bar.register("xml") </code></pre> <p>You can read this attribute by instantiating another <code>Storage</code>, which will read the same variable:</p> <pre><code>&gt;&gt;&gt; baz = Storage() &gt;&gt;&gt; baz.backends {'xml', 'text'} </code></pre> <hr> <p>You could even store the backend instances in a class attribute of <code>Connection</code>, and register each backend upon instantiation:</p> <pre><code>&gt;&gt;&gt; class Connection(object,metaclass=abc.ABCMeta): ... @abc.abstractmethod ... def register(self, backend): ... pass ... ... backends = set() ... &gt;&gt;&gt; class TextBackend(Connection): ... def register(self): ... super().backends.add(self) ... ... def __init__(self): ... self.register() ... &gt;&gt;&gt; class XMLBackend(Connection): ... def register(self): ... super().backends.add(self) ... ... def __init__(self): ... self.register() ... &gt;&gt;&gt; foo = TextBackend() &gt;&gt;&gt; bar = XMLBackend() &gt;&gt;&gt; Connection.backends {&lt;__main__.XMLBackend object at 0x027ADAB0&gt;, \ &lt;__main__.TextBackend object at 0x027ADA50&gt;} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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