endolith / elsim

Election Simulator 3000: Simulates a variety of elections and voting methods
https://endolith.github.io/elsim
MIT License
7 stars 4 forks source link

Fix package structure, absolute imports #9

Open endolith opened 2 years ago

endolith commented 2 years ago

Currently the overall package structure is what I want:

import elsim

elsim.__dir__()
Out[3]: 
[...
 'strategies',
 'elections',
 'methods']

As is the methods sub-package:

elsim.methods.__dir__()
Out[4]: 
[...
 '_common',
 'approval',
 'combined_approval',
 'condorcet',
 'borda',
 'black',
 'condorcet_from_matrix',
 'ranked_election_to_matrix',
 'coombs',
 'fptp',
 'irv',
 'runoff',
 'score',
 'star',
 'utility_winner']

But the other two include cruft that shouldn't be exposed to the user, such as np and cdist, and elections includes itself:

elsim.elections.__dir__()
Out[5]: 
[...
 'elections',
 'numbers',
 'np',
 'cdist',
 'honest_rankings',
 'elections_rng',
 'check_random_state',
 'random_utilities',
 'impartial_culture',
 'normal_electorate',
 'normed_dist_utilities']

Also the __init__.py files have relative imports, despite this being mildly recommended against by PEP8:

Absolute imports are recommended, as they are usually more readable and tend to be better behaved (or at least give better error messages) if the import system is incorrectly configured (such as when a directory inside a package ends up on sys.path):

from . import elections, strategies, methods

Though there are a lot of other packages that do it this way.

I've always been confused about the best way to do this stuff.