Note that there are some explanatory texts on larger screens.

plurals
  1. POChoose adapter dynamically depending on librarie(s) installed
    primarykey
    data
    text
    <p>I am designing a library that has adapters that supports a wide-range of libraries. I want the library to dynamically choose which ever adapter that has the library it uses installed on the machine when importing specific classes.</p> <p>The goal is to be able to change the library that the program depends on without having to make modifications to the code. This particular feature is for handling RabbitMQ connections, as we have had a lot of problems with <a href="https://github.com/pika/pika" rel="nofollow">pika</a>, we want to be able to change to a different library e.g. <a href="https://github.com/celery/py-amqp" rel="nofollow">pyAMPQ</a> or <a href="https://github.com/gmr/rabbitpy/" rel="nofollow">rabbitpy</a> without having to change the underlying code. </p> <p>I was thinking of implementing something like this in the <code>__init__.py</code> file of <code>servicelibrary.simple</code>.</p> <pre><code>try: #import pika # Is pika installed? from servicelibrary.simple.synchronous import Publisher from servicelibrary.simple.synchronous import Consumer except ImportError: #import ampq # Is ampq installed? from servicelibrary.simple.alternative import Publisher from servicelibrary.simple.alternative import Consumer </code></pre> <p>Then when the user imports the library </p> <pre><code>from servicelibrary.simple import Publisher </code></pre> <p>The underlying layer looks something like this</p> <p><strong>alternative.py</strong></p> <pre><code>import amqp class Publisher(object): ...... class Consumer(object): ...... </code></pre> <p><strong>synchronous.py</strong></p> <pre><code>import pika class Publisher(object): ...... class Consumer(object): ...... </code></pre> <p>This would automatically pick the second one when the first one is not installed.</p> <p>Is there a better way of implementing something like this? If anyone could link a library/adapter with a similar implementation that would be helpful as well.</p> <p><strong>[Edit]</strong> </p> <p>What would be the cleanest way to implement something like this? In the future I would also like to be able to change the default preference. Ultimately I may just settle for using the library installed, as I can control that, but it would be a nice feature to have. </p> <p>Alexanders suggestion is interesting, but I would like to know if there is a cleaner way.</p> <p><strong>[Edit2]</strong></p> <p>The original example was simplified. Each module may contain multiple types of imports, e.g. Consumer and Publisher.</p>
    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.
 

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