|
| 1 | +Splitting Modules |
| 2 | +################# |
| 3 | + |
| 4 | +Sometimes a module might contain functionality which is dependent upon certain |
| 5 | +behavior which cannot be analyzed - either it truly has external side effects, |
| 6 | +it is dependent upon another module which cannot yet be strictified and needs |
| 7 | +to be used at the top-level, or it is dependent upon something which strict |
| 8 | +modules have not yet been able to analyze. |
| 9 | + |
| 10 | +In these cases one possible solution, although generally a last resort, |
| 11 | +is to break the module into two modules. The first module will only contain |
| 12 | +the code which cannot be safely strictified. The second module will contain |
| 13 | +all of the code that can be safely treated as strict. A better way to do this |
| 14 | +is to not have the unverifable code happen at startup, but if that's not |
| 15 | +possible then splitting is an acceptable option. |
| 16 | + |
| 17 | +Because strict modules can still import non-strict modules the strict module |
| 18 | +can continue to expose the same interface as it previously did, and no other |
| 19 | +code needs to be updated. The only limitation to this is that it requires |
| 20 | +that the module being strictified doesn't need to interact with the non-strict |
| 21 | +elements at the top level. For example classes could still create instances |
| 22 | +of them, but the strict module couldn't call functions in the non-strict |
| 23 | +module at the top level. |
| 24 | + |
| 25 | + |
| 26 | +.. code-block:: python |
| 27 | +
|
| 28 | + import csv |
| 29 | + from random import choice |
| 30 | +
|
| 31 | + FAMOUS_PEOPLE = list(csv.reader(open('famous_people.txt').readlines())) |
| 32 | +
|
| 33 | + class FamousPerson: |
| 34 | + def __init__(self, name, age, height): |
| 35 | + self.name = name |
| 36 | + self.age = int(age) |
| 37 | + self.height = float(height) |
| 38 | +
|
| 39 | + def get_random_person(): |
| 40 | + return FamousPerson(*choice(FAMOUS_PEOPLE)) |
| 41 | +
|
| 42 | +
|
| 43 | +We can split this into two modules, one which does the unverifable read of our |
| 44 | +sample data from disk and another which returns the random piece of sample data: |
| 45 | + |
| 46 | + |
| 47 | +.. code-block:: python |
| 48 | +
|
| 49 | + import csv |
| 50 | +
|
| 51 | + FAMOUS_PEOPLE = list(csv.reader(open('famous_people.txt').readlines())) |
| 52 | +
|
| 53 | +
|
| 54 | +And we can have another module which exports our FamousPerson class along with |
| 55 | +the API to return a random famous person: |
| 56 | + |
| 57 | +.. code-block:: python |
| 58 | +
|
| 59 | + from random import choice |
| 60 | + from famous_people_data import FAMOUS_PEOPLE |
| 61 | +
|
| 62 | + class FamousPerson: |
| 63 | + def __init__(self, name, age, height): |
| 64 | + self.name = name |
| 65 | + self.age = int(age) |
| 66 | + self.height = float(height) |
| 67 | +
|
| 68 | + def get_random_person(): |
| 69 | + return FamousPerson(*choice(FAMOUS_PEOPLE)) |
0 commit comments