The best way to combine and match components in a python application.

I have a component that uses a simple pub / add-on module that I wrote as a message queue. I would like to try other implementations like RabbitMQ. However, I want to reconfigure this backend so that I can switch between my version and third-party modules for cleanliness and testing.

The obvious answer is:

  • Read the configuration file
  • Create a mutable settings object / dict
  • Change the target component to lazily load the specified implementation.

sort of:

# component.py from test.queues import Queue class Component: def __init__(self, Queue=Queue): self.queue = Queue() def publish(self, message): self.queue.publish(message) # queues.py import test.settings as settings def Queue(*args, **kwargs): klass = settings.get('queue') return klass(*args, **kwargs) 

Not sure if init should take the Queue class, I suppose this will help to easily determine the queue used during testing.

Another thought that I had was something like http://www.voidspace.org.uk/python/mock/patch.html , although it looks like it will be messy. The surface would be that I would not have to change the code to support the swap component.

Any other ideas or jokes would be appreciated.

EDIT: Fixed indentation.

+4
source share
1 answer

One thing I did before is to create a common class that each concrete implementation inherits from. Then there is a specification that can be easily followed, and each implementation can avoid repeating certain code that they will all use.

This is a bad example, but you can see how you could force the splash object to use any of the specified classes, and the rest of your code would not care.

 class SaverTemplate(object): def __init__(self, name, obj): self.name = name self.obj = obj def save(self): raise NotImplementedError import json class JsonSaver(SaverTemplate): def save(self): file = open(self.name + '.json', 'wb') json.dump(self.object, file) file.close() import cPickle class PickleSaver(SaverTemplate): def save(self): file = open(self.name + '.pickle', 'wb') cPickle.dump(self.object, file, protocol=cPickle.HIGHEST_PROTOCOL) file.close() import yaml class PickleSaver(SaverTemplate): def save(self): file = open(self.name + '.yaml', 'wb') yaml.dump(self.object, file) file.close() saver = PickleSaver('whatever', foo) saver.save() 
+2
source

Source: https://habr.com/ru/post/1338768/


All Articles