1

I have a class (content.MyClass), which stores lots of facts about bacteria. I call it many times, to define many types of bacteria. It isn't highly elegant, but it is reasonably fast, readable, and modular (easy to add more bacteria).

Question: is there a better way I should do this?

import content
def myMethod():
    bacteria = {} #A dictionary I fill with 'Bacteria Name':object

    bacteria['Staph Aureus'] = content.MyClass(
        bug_type = ['gram+'],
        virulence = ['Protein A', 'TSST-1', 'exfoiative toxin', 'enterotoxin'],
        labs = ['catalase+', 'coagulase+']
    )
    bacteria['Staph Epidermidis'] = content.MyClass(
        bug_type = ['gram+'],
        sx = ['infects prosthetic devices']
    )
    #Etc. about 25 more times.
    return bacteria

(Footnote: I know PEP 8 says I should indent everything to line up with "MyClass(", but that wouldn't work here as some of the lists are very long. Also, there are a lot more variables in each class; I trimmed them for the example here.)

2 Answers2

2

Question: is there a better way I should do this?

You're thinking about solving the wrong problem.

Make your class data-driven: separate the code from the data. Load the definitions from a data source; something as simple as a JSON or YAML file would work just fine.

When doing data-driven programming, one clearly distinguishes code from the data structures on which it acts, and designs both so that one can make changes to the logic of the program by editing not the code but the data structure.

http://www.faqs.org/docs/artu/ch09s01.html

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Personally I think using Python as a literal syntax for configuration is pleasant and useful. So much so I wrote a library to help you use this technique in C++ programs too! https://github.com/jzwinck/pccl I'm all for data-driven stuff, but it's not clear to me that this isn't already it. – John Zwinck Mar 07 '13 at 04:41
  • 1
    Interesting (and I don't mean that sarcastically or anything). Coming primarily from a JavaScript/web/JSON API background, of course I see things through that lens – so I'm curious, care to divulge a small example of such a config file? In my head I imagine it looks a lot like JSON, though maybe with fewer double quotes `:)` – Matt Ball Mar 07 '13 at 04:45
  • 1
    There's a basic one here, in the unit tests for pccl: https://github.com/jzwinck/pccl/blob/master/test/PyConfig.test.py - one of the things I like about "code as configuration syntax" is that if you wake up one day (as I have) and realize that the only way your program will fully know how to configure itself is by consulting a web service or network protocol or whatever, it can. Oh, and as you can see from the example, you can put functions in the config! – John Zwinck Mar 07 '13 at 04:47
  • I like the data-driven idea, and spent a long time considering something similar, earlier. Like John Zwinck said, though, I figured the Python syntax was pleasant enough that I didn't need to reinvent the wheel there. I like all these suggestions, thanks a bunch! – ancientcampus Mar 07 '13 at 05:06
  • I'll admit I previously didn't understand what YAML or JSON were. Upon further thought, I realized you're absolutely right - they're perfect for my needs here. – ancientcampus Mar 08 '13 at 22:32
1

I would do it this way:

def myMethod():
  from content import MyClass
  return {
    'Staph Aureus': MyClass(
      bug_type = ['gram+'],
      virulence = ['Protein A', 'TSST-1', 'exfoiative toxin', 'enterotoxin'],
      labs = ['catalase+', 'coagulase+']
    ),
    'Staph Epidermidis': MyClass(
      bug_type = ['gram+'],
      sx = ['infects prosthetic devices']
    ),
    #Etc. about 25 more times.
  }
John Zwinck
  • 239,568
  • 38
  • 324
  • 436