Skip to content

Commit

Permalink
WIP field definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
GwynHannay committed Dec 15, 2021
1 parent 35df176 commit d0d9602
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
10 changes: 5 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
import json
from utils import csv_parser as cps, data_functions as df
from datetime import datetime
from collections import defaultdict


def conversion(csv_file: str):
"""[summary]
"""Converts and transforms a CSV file from the Sleep as Android app into a JSON file.
Parameters
----------
csv_file : str
[description]
Name / location of the CSV file to be processed.
"""
first_pass = []

Expand All @@ -34,6 +33,7 @@ def conversion(csv_file: str):
# let's identify each part and convert it into
# something much more useable
i = 0
id = 0
json_array = []
for record in first_pass:
headers = []
Expand All @@ -43,10 +43,10 @@ def conversion(csv_file: str):

for key in record:
val = record[key]
id = 0

header = df.process_header(key)

# these headers contain datetimes in various forms, so we want to standardise them
if header in ('id', 'tracking_start', 'tracking_end', 'alarm_scheduled'):
datetime_value = df.process_dates(header, val)

Expand All @@ -56,7 +56,7 @@ def conversion(csv_file: str):
val = datetime.strftime(datetime_value, '%Y-%m-%d %H:%M')

elif header in ('tracking_hours'):
val = df.process_numbers(header, val)
val = df.process_numbers(val)

elif header.startswith('event'):
event = df.process_event(val)
Expand Down
34 changes: 34 additions & 0 deletions utils/csv_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@

saa_fields = {
'Id': {
'name': 'id',
'type': 'unix timestamp'
},
'Tz': {
'name': 'timezone',
'type': 'string'
},
'From': {
'name': 'tracking_start',
'type': 'datetime'
},
'To': {
'name': 'tracking_end',
'type': 'datetime'
},
'Sched': {
'name': 'alarm_scheduled',
'type': 'datetime'
},
}


def csv_headers(headers):
"""[summary]
Expand Down Expand Up @@ -43,3 +67,13 @@ def combine_record(headers, row):
record = dict(zip_it)

return record


def get_instructions(header):
instruction = saa_fields[header]

return instruction


def saa_field_parser(header, value):
data_type = get_instructions(header)
23 changes: 11 additions & 12 deletions utils/data_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def process_header(header: str) -> str:
"""[summary]
"""Converts all headers to lowercase and makes them a bit more descriptive.
Parameters
----------
Expand All @@ -19,10 +19,9 @@ def process_header(header: str) -> str:
Exception
[description]
"""
# convert all headers to lowercase

header = header.lower()

# rename some of the headings to be a bit more descriptive
try:
if header == 'tz':
header = 'timezone'
Expand All @@ -33,29 +32,29 @@ def process_header(header: str) -> str:
elif header == 'sched':
header = 'alarm_scheduled'
elif header == 'hours':
header = 'tracking_hours'
header = 'hours_tracked'
except Exception as e:
raise Exception(
"An error occurred processing header '{}': {}".format(header, e))

return header


def process_dates(header, detail):
def process_dates(header: str, detail: str) -> datetime:
"""[summary]
Parameters
----------
header : [type]
header : str
[description]
detail : [type]
detail : str
[description]
Returns
-------
[type]
datetime
[description]
"""
"""
if header == 'id':
datetime_value = datetime.fromtimestamp(int(detail)/1000)
else:
Expand All @@ -64,7 +63,7 @@ def process_dates(header, detail):
return datetime_value


def process_numbers(header, detail):
def process_numbers(detail: str) -> float:
"""[summary]
Parameters
Expand All @@ -79,9 +78,9 @@ def process_numbers(header, detail):
[type]
[description]
"""
detail = float(detail)
value = float(detail)

return detail
return value


def process_event(event):
Expand Down

0 comments on commit d0d9602

Please sign in to comment.