-
Notifications
You must be signed in to change notification settings - Fork 0
/
regions.py
139 lines (99 loc) · 3.9 KB
/
regions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Region:
region_name = None
region_dict = {}
def __init__(self, code, name, upper_region_code=None):
self.name = name
self.code = code
self.upper_region_code = upper_region_code
self.inner_regions = []
Region.region_dict[code] = self # adds region to all region dictionary - region_dict in Region class
@classmethod
def get_region_list(cls):
"""Returns list with region instances made from the class region dict."""
return cls.region_dict_to_list()
@classmethod
def add_region(cls, code, name, upper_region_code=None):
"""Makes new region instance and adds it to self class region dict:
index - region code, value - region instance"""
if code not in cls.region_dict:
new_region = cls(code, name, upper_region_code)
cls.region_dict[code] = new_region
Region.region_dict[code] = new_region
@classmethod
def get_region_by_number(cls, number):
return cls.region_dict[number]
@classmethod
def region_dict_to_list(cls):
return [region for index, region in cls.region_dict.items()]
@classmethod
def region_names_list(cls):
return [region.name for region in cls.region_dict_to_list()]
@classmethod
def region_codes_list(cls):
return [region.code[:-1] for region in cls.region_dict_to_list()]
@classmethod
def get_region_type_name(cls):
return cls.region_name
@property
def subregions(self):
"""Property with list with region subregions instances (for example list of gminas in powiat)."""
return list(filter(lambda x: x.upper_region == self, Region.get_region_list()))
@property
def number_of_subregions(self):
"""Property with numbers of subregions in region (for example numbers of gminas in powiat)"""
return len(self.subregions)
@property
def upper_region(self):
"""Property that stores instance of upper region in hierarchy (for example powiat that gmina belongs to).
It is searched in main region dict in Region class by self.upper_region_code attribute.
For voivodeship, that don't have an upper region returns None"""
return Region.region_dict[self.upper_region_code] if self.upper_region_code else None
def __str__(self):
return self.name
def add_inner_region(self, region):
self.inner_regions.append(region)
def get_subregions(self):
return self.subregions
def get_number_of_subregions(self):
return self.number_of_subregions
def get_name(self):
return self.name
def get_class_name(self):
return self.__class__.__name__
class Voivodeship(Region): # Województwo
region_name = 'Województwo'
region_dict = {}
pass
class Powiat(Region): # Powiat
region_name = 'Powiat'
region_dict = {}
def __init__(self, code, name, upper_region_code):
super().__init__(code, name, upper_region_code)
Powiat.region_dict[code] = self
class PlainPowiat(Powiat):
region_name = 'Powiat'
region_dict = {}
class CityPowiat(Powiat): # miasto na prawach powiatu (city with powiat status)
region_name = 'Miasto na prawach powiatu'
region_dict = {}
class Gmina(Region):
region_name = 'Gmina'
region_dict = {}
class UrbanGmina(Gmina): # Gmina miejska
region_name = 'Gmina miejska'
region_dict = {}
class Representation(UrbanGmina): # Delegatura
region_name = 'Delegatura'
region_dict = {}
class RuralGmina(Gmina): # Gmina wiejska
region_name = 'Gmina wiejska'
region_dict = {}
class UrbanRuralGmina(Gmina): # Gmina miejsko-wiejska
region_name = 'Gmina miejsko-wiejska'
region_dict = {}
class City(UrbanRuralGmina): # Miasto
region_name = 'Miasto'
region_dict = {}
class RuralArea(UrbanRuralGmina): # Obszar wiejski
region_name = 'Obszar wiejski'
region_dict = {}