Pregunta

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?

¿Fue útil?

Solución

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...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top