0

I am working as a summer intern at a fairly large company, me and another intern has been tasked with writing a background service from scratch in python(with given specifications). I can't say that I am a Python expert but I try to write as good code as possible and follow good practice. To the describe the service in a easy way it receives messages from a buss (amqp atm) and matches them against a patterns and preform actions depending on the pattern, all this is configurable from a json file.

To get down to the point, the service is suppose to be general and therefore a developer should be able to add different inputs and outputs classes. This is the structure I have right now.

  • service.py
  • input
    • __init__.py (Includes all .py files in the __all__ variable)
    • base.py
    • amqp.py

service.py

from input import *
import input
#use input.base.sources

input\base.py

sources = {}
def register_input(cls):
   sources[cls.input_name] = cls
   return cls

class InputBase(object):
    action(self, params):
...

input\amqp.py

@register_input
class AmqpInterface(InputBase):
    input_name = "amqp"
    action(self, params):
...

This works, but it doesn't look very nice and it also creates problems with include orders. Neither I nor the senior developer could think of a more pythonic way of solving this. Do you have any ideas how to solve this problem in a more elegant way?

xobust
  • 1
  • 1
  • 5
    Maybe try [codereview.se]. – Peter Wood Jul 08 '15 at 22:07
  • As I said, the senior developer working closes to me couldn't think of a better way at the moment, that's why I'm asking. I could ask some dev with python knowledge from another part of the company though. – xobust Jul 08 '15 at 22:11
  • 1
    It's another site, more suited to critiquing working code. – Peter Wood Jul 08 '15 at 22:16
  • how are you able to call `action(self, params):` ? Is it just a placeholder? – Pynchia Jul 08 '15 at 22:24
  • You can add code to the `__init__.py` file to automatically import thes other modules and optionally their contents in the directory. See this [answer](http://stackoverflow.com/a/5135444/355230) and this [other one](http://stackoverflow.com/a/14428820/355230) of mine. – martineau Jul 08 '15 at 23:43
  • @PeterWood Sorry, didn't realize that, will try it :) – xobust Jul 09 '15 at 05:25
  • @Pynchia I wrote down the code from memory so its just a placeholder – xobust Jul 09 '15 at 05:25
  • @martineau Thanks for pointing me in the right direction. – xobust Jul 09 '15 at 05:25

0 Answers0