organizing your scripts, with plenty of re-use

Ethan Furman ethan at stoneleaf.us
Mon Oct 12 19:36:58 EDT 2009


Stef Mientki wrote:
> Gabriel Genellina wrote:

[snip]

>>
>> That's what I meant to say. It IS a zero-installation schema, and it 
>> also works if you properly install the package. Quoting Steven 
>> D'Aprano (changing names slightly):
>>
>> """You would benefit greatly from separating the interface from
>> the backend. You should arrange matters so that the users see something
>> like this:
>>
>> project/
>> +-- animal
>> +-- mammal
>> +-- reptile
>> +-- somepackagename/
>>     +-- __init__.py
>>     +-- animals.py
>>     +-- mammals/
>>         +-- __init__.py
>>         +-- horse.py
>>         +-- otter.py
>>     +-- reptiles/
>>         +-- __init__.py
>>         +-- gator.py
>>         +-- newt.py
>>     +-- misc/
>>         +-- __init__.py
>>         +-- lungs.py
>>         +-- swimming.py
>>

[snip]

>> The key is to put all the core functionality into a package, and place 
>> the package where Python can find it. Also, it's a good idea to use 
>> relative imports from inside the package. There is no need to juggle 
>> with sys.path nor even set PYTHONPATH nor import __main__ nor play any 
>> strange games; it Just Works (tm).
>>
> please don't get angry,
> I'm not a programmer, I'm just a human ;-)
> 
> Hierarchical choices are done on todays knowledge, tomorrow we might 
> have different views and want/need to arrange things in another way.
> An otter may become a reptile ;-)
> So from the human viewpoint the following should be possible (and is for 
> example possible in Delphi)

> - I can move the complete project anywhere I like and it should still 
> work without any modifications (when I move my desk I can still do my work)

Move a complete package anywhere along the PYTHONPATH and it will still 
work.  Check.

> - I can move any file in he project to any other place in the project 
> and again everything should work without any modifications ( when I 
> rearrange my books, I can still find a specific book)

Move any file in any directory to any other spot in that same directory 
and it will still work.  Check.  ;-)

Humans are a lot smarter than computers.  Even 'just humans'.  ;-)  If 
you move your book, then can't find it on the last shelf it used to be 
on, you look on other shelves, you look on your desk, you look on the 
coffe table, you look in your car, etc, etc, and so forth.  If you move 
a file in a package to somewhere else, and you don't tell the package 
where it's at, it's not going to start looking all over the hard-drive 
for it.  If that were the case you would have to be extra careful to 
have every module's name be distinct, and then what's the point of 
having packages?

~Ethan~

> In my humble opinion if these actions are not possible, there must be 
> redundant information in the collection. The only valid reason for 
> redundant information is to perform self healing (or call it error 
> correction), and here we have a catch-22.
> 
> cheers,
> Stef Mientki




More information about the Python-list mailing list