I have a function that pulls data from a file or string literal in my case and returns them in either a list
or a dict
.
My concerns
I'm not sure if a function should have two different return types.
I feel like I'm repeating myself a lot.
If it is ok to return two different datatypes, how do you document that in the docstring?
def pull_csv(data, delimiter=',', field_names=None, perserve_line_nums=False):
"""
Pulls data out of a CSV file and returns as either a list or dict.
:param data:
:param delimiter: str
:param field_names: bool
:param perserve_line_nums: bool
:return:
"""
if field_names:
if perserve_line_nums:
lines = {}
for line_num, line in enumerate(data.split('\n')):
lines.update({line_num: {}})
for index, value in enumerate(line.split(delimiter)):
lines[line_num].update({field_names[index]: value})
else:
lines = []
for line_num, line in enumerate(data.split('\n')):
lines.append({})
for index, value in enumerate(line.split(delimiter)):
lines[line_num].update({field_names[index]: value})
else:
if perserve_line_nums:
lines = {}
for line_num, line in enumerate(data.split('\n')):
lines.update({line_num: []})
for value in line.split(delimiter):
lines[line_num].append(value)
else:
lines = []
for line_num, line in enumerate(data.split('\n')):
lines.append([])
for value in line.split(delimiter):
lines[line_num].append(value)
return lines
Example
data = """ \
Bill, 1, Jan, 28, 93
Joe, 7, Dec, 02, 67 \
"""
print(pull_csv(data, ',', ['Name', 'Fav_num', 'Birth_month', 'Birth_date', 'Birth_year'], True))
Output
[{'Birth_date': ' 28', 'Birth_year': ' 03', 'Name': ' Jason', 'Birth_month': ' Jan', 'Fav_num': ' 1'}, {'Birth_date': ' 02', 'Birth_year': ' 67 ', 'Name': 'George', 'Birth_month': ' Dec', 'Fav_num': ' 7'}]