Skip to content

Commit 4490b9d

Browse files
committed
Update util.py
Added four simplistic functions to help deal with numbers.
1 parent c1ca844 commit 4490b9d

1 file changed

Lines changed: 48 additions & 6 deletions

File tree

util.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ def normalize_tainted_string(questionablestring):
111111
return re.sub('\s+', ' ', re.sub('[\W_]+', ' ', questionablestring.lower()))
112112

113113

114-
# Ref: https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
114+
# Ref: Started with https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
115115
def read_file_line_by_line_to_list(text_file) -> list:
116116
"""
117117
Read file into an array
118118
:param file: input file
119119
:return: file lines in an array
120120
"""
121121
if not os.path.exists(text_file):
122-
raise Exception('file path: {0} not exists'.format(text_file))
122+
raise Exception('file path: {0} does not exist'.format(text_file))
123123

124124
file_array = []
125125
with open(text_file, "r") as text_file_input:
@@ -129,19 +129,19 @@ def read_file_line_by_line_to_list(text_file) -> list:
129129
return file_array
130130

131131

132-
# Ref: https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
132+
# Ref: Started with https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
133133
def read_file_line_by_line_to_set(file) -> set:
134134
"""
135135
Read file into a set
136136
:param file: input file
137137
:return: file lines in a set
138138
"""
139139
if not os.path.exists(file):
140-
raise BuildError('file path: {0} not exists'.format(file))
140+
raise Exception('file path: {0} does not exist'.format(file))
141141

142142
file_set = set()
143-
with open(file, "r") as ins:
144-
for line in ins:
143+
with open(file, "r") as text_file:
144+
for line in text_file:
145145
file_set.add(line.strip())
146146

147147
return file_set
@@ -164,6 +164,47 @@ def read_file_line_by_line_to_set(file) -> set:
164164
line = sys.stdin.readline()
165165

166166

167+
168+
def remove_commas(string_input: str) -> str:
169+
import re
170+
# Input: A collection of "strings" that may be comma separated.
171+
# Output: A collection of "strings" that are now space separated.
172+
no_commas = re.sub(r",", " ", string_input)
173+
return no_commas
174+
175+
176+
def convert_strings_to_floats(strings: str) -> list:
177+
import statistics
178+
# Input: The "strings" must be a list of "numbers" in string format,
179+
# Output: A Python `list` of floats composed of the numbers in the Input.
180+
#
181+
# Depends upon: remove_commas(string_input: str)
182+
#
183+
# Remove commas - because it receives lists...
184+
strings_no_commas: str = remove_commas(strings)
185+
# Convert string input to a list of floats.
186+
# My original use case was receiving a list on the command line.
187+
# This approach was outlined at:
188+
# https://www.geeksforgeeks.org/python/python-converting-all-strings-in-list-to-integers/
189+
list_integers: list = list(map(float, strings_no_commas.split()))
190+
return list_integers
191+
192+
193+
def get_median(list_items: list) -> float:
194+
import statistics
195+
# Input: A Python list of numbers ("strings").
196+
# Output: A float.
197+
median_number = statistics.median(list_items)
198+
return median_number
199+
200+
def get_mean(list_items: list) -> float:
201+
import statistics
202+
# Input: A Python list of numbers ("strings").
203+
# Output: A float.
204+
mean_number = statistics.mean(list_items)
205+
return mean_number
206+
207+
167208
# Ref: https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
168209
# TODO: Work on this download_file() function - make it more general & support proxies
169210
from urllib.request import urlopen
@@ -335,3 +376,4 @@ def is_role_based_email(email: str) -> bool:
335376
return True
336377

337378
return False
379+

0 commit comments

Comments
 (0)