2

While writing code, I often run into the problem of addressing trivial dependancies. For example, let's take the layout of a memoizer object I've recently written. My directory structure looks like this:

memoizer/
  memoizer.py
  tests.py
  strategies/
    __init__.py
    perfect.py
    mru.py
    queue.py
README

The problem is specifically, with mru.py. mru.py contains a MRU class which uses a Queue defined in queue.py. Clearly though, a queue isn't a strategy for a memoizer, and it doesn't make any sense to put it under strategies.

One idea I had was to set up my code to look something like this:

memoizer/
  memoizer.py
  tests.py
  strategies/
    __init__.py
    perfect.py
    mru/
      __init__.py
      mru.py
      queue.py
README

The problem with this set up though is that now the user has to know that mru is in a subpackage.

Another idea was to arrange the structure like this:

memoizer/
  memoizer.py
  tests.py
  strategies/
    __init__.py
    perfect.py
    mru.py
    dependencies/
      __init__.py
      queue.py
README

This would solve my problem, but it intuitively seems like the wrong way to do it.

What is the right way to organize my files to account for this inconsistency?

brc
  • 5,281
  • 2
  • 29
  • 30
Ceasar
  • 22,185
  • 15
  • 64
  • 83

1 Answers1

3

I would suggest a variation of the second structure:

memoizer/
  memoizer.py
  tests.py
  strategies/
    __init__.py
    perfect.py
    mru/
      __init__.py <- This is what used to be mru.py
      queue.py
README

Then the user can just import memoizer.strategies.mru.

That's the quick and easy way of doing it. However, you could keep the original mru.py file, and expose items from it in your __init__.py like so:

import mru as _mru

SomeClass = _mru.SomeClass
# etc...
rossipedia
  • 56,800
  • 10
  • 90
  • 93
  • Can you elaborate on using __init__.py to expose mru? I can't seem to figure out from the documentation how that works exactly, except in the case of from x import *. – Ceasar Nov 03 '11 at 06:14
  • Basically, python treats the `module/__init__.py` file the same way that it would treat the file `module.py`. The easiest way to do that would be to just rename `mru.py` to `__init__.py` inside the `mru` folder. – rossipedia Nov 03 '11 at 06:18
  • So I get the following when using the interpreter from within /memoizer. `>>> from strategies.mru import MRU Traceback (most recent call last): File "", line 1, in ImportError: cannot import name MRU >>> from strategies.mru.mru import MRU` Am I misunderstanding something? EDIT: Nevermind- your edits cleared my confusion. – Ceasar Nov 03 '11 at 06:24
  • `from strategies.mru.mru import MRU` tells python to assume there's a `strategies/mru/mru.py` or `strategies/mru/mru/__init__.py` file with the name `MRU` defined in it. If you renamed your `mru.py` file to `__init__.py`, then you should just use `from strategies.mru import MRU` – rossipedia Nov 03 '11 at 06:33