Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autogenerated docstrings summarizing each function #1408

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 bin/get_profile.py 100755 → 100644
@@ -37,21 +37,42 @@
TIMED_ENTRIES = ['carbratio', 'sens', 'basal', 'target_low', 'target_high']



def get_profiles(nightscout, token):
"""Get profiles available in nightscout

Gets profiles from nightscout, and returns them as a list of dictionaries.

Args:
nightscout (str): Nightscout URL (required)
token (str): Nightscout token (optional)

Returns:
list: A list of dictionaries, each containing a profile.
"""
Get profiles available in nightscout
"""

r_url = nightscout + "/api/v1/profile.json"
if token is not None:
r_url = r_url + "?" + token
r = requests.get(r_url)
return r.json()



def get_current_profile(nightscout, token, profile_name):
"""Try to get the active profile

Gets the active profile from nightscout, and then returns it.

Args:
nightscout (str): Nightscout URL (required)
token (str): Nightscout token (optional)
profile_name (str): Profile name (optional)

Returns:
dict: Profile
"""
Try to get the active profile
"""

r_url = nightscout + "/api/v1/profile.json"
if token is not None:
r_url = r_url + "?" + token
@@ -103,10 +124,21 @@ def get_current_profile(nightscout, token, profile_name):
return p_list[0]["store"][profile_name]



def profiles(nightscout, token):
"""Print list of profiles available in nightscout

Gets profiles from nightscout, prints the default profile, and then
prints a list of all available profiles.

Args:
nightscout (str): Nightscout URL (required)
token (str): Nightscout token (optional)

Returns:
None
"""
print list of profiles available in nightscout
"""

p_list = get_profiles(nightscout, token)
default_profile = p_list[0]["defaultProfile"]
profile_list = p_list[0]["store"].keys()
@@ -116,10 +148,22 @@ def profiles(nightscout, token):
print("\t" + profile)



def display(nightscout, token, profile_name, profile_format):
"""Display contents of a profile, in requested format

Gets the profile from nightscout, and then displays it in the requested format.

Args:
nightscout (str): Nightscout URL (required)
token (str): Nightscout token (optional)
profile_name (str): Profile name (optional)
profile_format (str): Profile format (optional)

Returns:
None
"""
Display contents of a profile, in requested format
"""

profile = get_current_profile(nightscout, token, profile_name)
if profile_format == "nightscout":
# display_nightscout(p_list, profile_name)
@@ -131,10 +175,22 @@ def display(nightscout, token, profile_name, profile_format):
print(json.dumps(ns_to_oaps(profile), indent=4))



def write(nightscout, token, profile_name, directory):
"""Write profile in OpenAPS format to a directory

Gets the profile from nightscout, and then writes it to the requested directory.

Args:
nightscout (str): Nightscout URL (required)
token (str): Nightscout token (optional)
profile_name (str): Profile name (optional)
directory (str): Directory to write profile files to (required)

Returns:
None
"""
Write profile in OpenAPS format to a directory
"""

profile = ns_to_oaps(get_current_profile(nightscout, token, profile_name))
logging.debug("Checking for directory: %s", directory)
if not os.path.isdir(directory):
@@ -161,10 +217,17 @@ def write(nightscout, token, profile_name, directory):
f.write(json.dumps(profile, indent=4))



def normalize_entry(entry):
"""Clean up an entry before further processing

Args:
entry (dict): A single profile entry

Returns:
None
"""
Clean up an entry before further processing
"""

try:
if entry["timeAsSeconds"]:
pass
@@ -186,10 +249,19 @@ def normalize_entry(entry):
return entry



def ns_to_oaps(ns_profile):
"""Convert nightscout profile to OpenAPS format

Converts a nightscout profile to an OpenAPS profile.

Args:
ns_profile (dict): Nightscout profile (required)

Returns:
dict: OpenAPS profile
"""
Convert nightscout profile to OpenAPS format
"""

oaps_profile = {}
# XXX If addint any new entries, make sure to update PROFILE_KEYS at the top
# Not represented in nightscout
@@ -314,18 +386,33 @@ def ns_to_oaps(ns_profile):
return sorted_profile



def display_nightscout(profile_data, profile_name):
"""Display profile the way it comes from nightscout

Args:
profile_data (dict): Profile data (required)
profile_name (str): Profile name (required)

Returns:
None
"""
Display profile the way it comes from nightscout
"""

print("Displaying profile {}".format(profile_name))
print(json.dumps(profile_data[0]["store"][profile_name], indent=4))



def display_text(p_data):
"""Display profile in text format

Args:
p_data (dict): Profile data (required)

Returns:
None
"""
Display profile in text format
"""

# p_data = profile_data[0]["store"][profile_name]
logging.debug("Data keys: %s", p_data.keys())

@@ -28,20 +28,57 @@
import argparse
import re



def parseDateAndRun(filename):
"""Parse date and run number from filename

Parses the date and run number from a filename.

Args:
filename (str): Filename (required)

Returns:
tuple: (date, run)
"""

m=re.match( r'.*profile.(?P<run>[0-9]*).(?P<date>20[0-9][0-9]-[01][0-9]-[0-3][0-9]).json', filename)
if m:
return (m.group('date'), m.group('run'))
else: # not found
return ('0','0')



def calc_minutes(timestr):
"""Returns the number of minutes from midnight. Seconds are ignored

Args:
timestr (str): Time string in format HH:MM

Returns:
int: Number of minutes from midnight
"""

# returns the number of minutes from midnight. seconds are ignored
# based on http://stackoverflow.com/questions/10663720/converting-a-time-string-to-seconds-in-python
ftr = [60,1,0] # ignore seconds, count minutes, and use 60 minutes per hour
return sum([a*b for a,b in zip(ftr, map(int,timestr.split(':')))])



def expandProfile(l, valueField, offsetField):
"""Expand a profile to cover the whole day

Args:
l (list): Profile list (required)
valueField (str): Value field (required)
offsetField (str): Offset field (required)

Returns:
list: Expanded profile
"""

r=[]
minutes=0
value=l[0][valueField]
@@ -63,7 +100,22 @@ def expandProfile(l, valueField, offsetField):
# return the expanded profile
return r



def writeExcelHeader(ws, date_format, headerFormat):
"""Write header to Excel worksheet

Writes the header to the Excel worksheet.

Args:
ws (xlsxwriter.worksheet.Worksheet): Excel worksheet (required)
date_format (xlsxwriter.format.Format): Excel date format (required)
headerFormat (xlsxwriter.format.Format): Excel header format (required)

Returns:
None
"""

ws.write_string(0,0, 'Filename', headerFormat)
ws.write_string(0,1, 'Date', headerFormat)
ws.write_string(0,2, 'Run', headerFormat)
@@ -74,7 +126,23 @@ def writeExcelHeader(ws, date_format, headerFormat):
ws.write_datetime(0, col, dt, date_format)
col=col+1



def write_profile(worksheet, row, json, excel_number_format):
"""Write profile to worksheet

Writes a profile to a worksheet.

Args:
worksheet (xlsxwriter.worksheet.Worksheet): Worksheet to write to
row (int): Row to write to
json (dict): Profile to write
excel_number_format (xlsxwriter.format.Format): Format to use for numbers

Returns:
None
"""

worksheet.write_string(row, 0, filename)
date, run = parseDateAndRun(filename)
worksheet.write_string(row, 1, date)
@@ -96,7 +164,20 @@ def write_timebased_profile(worksheet, row, expandedList, excel_number_format):
worksheet.write_number(row, col, expandedList[i], excel_number_format)
col=col+1



def excel_init_workbook(workbook):
"""Initialize Excel workbook

Initializes the Excel workbook with the formats that will be used in the rest of the program.

Args:
workbook (Excel workbook): Excel workbook (required)

Returns:
None
"""

#see http://xlsxwriter.readthedocs.io/format.html#format for documentation on the Excel format's
excel_hour_format = workbook.add_format({'num_format': 'hh:mm', 'bold': True, 'font_color': 'black'})
excel_2decimals_format = workbook.add_format({'num_format': '0.00', 'font_size': '16'})