-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrinomialsplit.py
178 lines (132 loc) · 4.55 KB
/
trinomialsplit.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Name: trinomialsplit.py
# Description: class that stores an ST and its split-up version
# Attributes:
# Variables:
# trinomial - string containing an instance of an ST.
# statenumber - contains the state code. 1 - 50, alphabetical except for Alaska and Hawaii
# countycode - contains alphabetical county code string.
# sitenumber - contains actual site number.#
class TrinomialSplit:
trinomial = ""
statenumber = ""
countycode = ""
sitenumber = ""
count = 0
countycount = 0
def __init__(self, trinomialinstance):
self.trinomial = trinomialinstance
self.split_trinomial()
def has_dashes(self):
if self.trinomial.find("-") != -1:
return True
else:
return False
def has_spaces(self):
if self.trinomial.find(" ") != -1:
return True
else:
return False
def has_state_no(self):
if self.statenumber == "00":
return False
else:
return True
# strip dashes
def strip_dashes(self):
self.trinomial = self.trinomial.replace("-", "")
#strip spaces
def strip_spaces(self):
self.trinomial = self.trinomial.replace(" ", "")
def has_parens(self):
if ((self.trinomial.find("(") != -1) or (self.trinomial.find(")") != -1)):
return True
else:
return False
def has_brackets(self):
if ((self.trinomial.find("[") != -1) or (self.trinomial.find("]") != -1)):
return True
else:
return False
# Strip trinomial of brackets or parens
def strip_parens(self):
self.trinomial = self.trinomial.replace("(", "")
self.trinomial = self.trinomial.replace(")", "")
def strip_brackets(self):
self.trinomial = self.trinomial.replace("]", "")
self.trinomial = self.trinomial.replace("[", "")
#Grabs the state code.
def get_state_code(self):
self.statenumber = self.trinomial[self.count:2]
self.count += 2
#Grabs the county code
def get_county_code(self):
# If there is a state number, the starting off point for the loop should be its length.
# else, it should be 0.
#keep track of what the length of the county code is.
countylen = 0
for char in self.trinomial:
if char.isalpha():
countylen += 1
#county code spans from the current count to the end of the code
self.countycode = self.trinomial[self.count:self.count + countylen]
self.count += countylen
#grabs the site number
def get_site_number(self):
self.sitenumber = self.trinomial[self.count:]
# see if the site number has leading zeros
def has_leading(self):
if self.sitenumber[0] == "0":
return True
else:
return False
#count and return the number of leading zeros
def leadingzeros(self):
leading = 0
for n in self.sitenumber:
if n == "0":
leading += 1
else:
return leading
# remove the leading zeroes
def remove_leading(self):
self.sitenumber = self.sitenumber[self.leadingzeros():]
def split_trinomial(self):
if self.has_parens():
self.strip_parens()
if self.has_brackets():
self.strip_brackets()
if self.has_dashes():
self.strip_dashes()
if self.has_spaces():
self.strip_spaces()
if self.is_lowercase():
self.change_toupper()
if self.has_state_no():
self.get_state_code()
else:
self.statenumber = "00"
self.get_county_code()
self.get_site_number()
if self.has_leading():
self.remove_leading()
# Test function to print out the trinomial elements.
def print_elements(self):
print(self.statenumber + " " + self.countycode + " " + self.sitenumber)
# test function
def print_trinomial(self):
print(self.trinomial)
# checks if any of the letters are lower case, and, if so, returns true.
def is_lowercase(self):
for char in self.trinomial:
if char.islower():
return True
return False
#changes any lowercase letters to uppercase.
def change_toupper(self):
temp = ""
for char in self.trinomial:
if char.islower():
temp += char.upper()
else:
temp += char
self.trinomial = temp