-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrinomialsplit.py
186 lines (147 loc) · 5.72 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
179
180
181
182
183
184
185
186
# 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()
# see if the trinomial has dashes or spaces
def has_dashes_or_spaces(self):
for i in range(0, len(self.trinomial)):
if (self.trinomial[i] == " ") or (self.trinomial[i] == "-"):
return True
return False
def has_state_no(self):
if self.statenumber == "00":
return False
else:
return True
# strip dashes or spaces
def strip_dashes(self):
strippedtrinomial = ""
for i in range(0, len(self.trinomial)):
# if the character is not a space or dash, copy it over.
if (self.trinomial[i] != " ") and (self.trinomial[i] != "-"):
strippedtrinomial += self.trinomial[i]
self.trinomial = strippedtrinomial
# see if the trinomial has parentheses or brackets, return true if the case.
def has_parens_or_brackets(self):
if (self.trinomial[0] == "(") and (self.trinomial[len(self.trinomial) - 1] == ")"):
return True
elif (self.trinomial[0] == "[") and (self.trinomial[len(self.trinomial) - 1] == "]"):
return True
else:
return False
# Strip trinomial of brackets or parens
def strip_parens(self):
strippedtrinomial = ""
for i in range(0, len(self.trinomial)):
# If not the first or last character (the spaces, in other words), copy over.
if (i != 0) and (i != len(self.trinomial) - 1):
strippedtrinomial += self.trinomial[i]
# store the stripped version back into the original.
self.trinomial = strippedtrinomial
#Grabs the state code
def get_state_code(self):
for i in range(0, len(self.trinomial)):
if self.trinomial[i].isdigit():
self.statenumber += self.trinomial[i]
self.count += 1
elif self.count > 2:
print("WARNING: state numbers are not more than 2 digits long. Have you checked if your data is "
"cleaned?")
return
else:
return
#Grabs the county code
def get_county_code(self):
# If tere is a state number, the starting off point for the loop should be its length.
# else, it should be 0.
if (self.has_state_no):
startcount = len(self.statenumber)
else:
startcount = 0
for i in range(startcount, len(self.trinomial)):
if self.trinomial[i].isalpha():
self.countycode += self.trinomial[i]
self.count += 1
self.countycount += 1
elif self.countycount > 3:
print("WARNING: county codes are typically not more than 3 digits long. Have you checked if your data "
"is cleaned?")
return
else:
return
#grabs the site number
def get_site_number(self):
for i in range(self.count, len(self.trinomial)):
self.sitenumber += self.trinomial[i]
# 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 i in range(0, len(self.sitenumber)):
if self.sitenumber[i] == "0":
leading += 1
else:
return leading
# remove the leading zeroes
def remove_leading(self):
temp = ""
# start counting at the point where there are no leading zeroes.
for i in range(self.leadingzeros(), len(self.sitenumber)):
temp += self.sitenumber[i]
self.sitenumber = temp
def split_trinomial(self):
if self.has_parens_or_brackets():
self.strip_parens()
if self.has_dashes_or_spaces():
self.strip_dashes()
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 i in range(0, len(self.trinomial)):
if self.trinomial[i].isalpha():
if self.trinomial[i].islower():
return True
return False
#changes any lowercase letters to uppercase.
def change_toupper(self):
temp = ""
for i in range(0, len(self.trinomial)):
if self.trinomial[i].isalpha() and self.trinomial[i].islower():
temp += self.trinomial[i].upper()
else:
temp += self.trinomial[i]
self.trinomial = temp