Refactor habit list collection in repetition migration

This commit is contained in:
Marty Oehme 2021-08-31 18:05:59 +02:00
parent 189d1bb8af
commit a09f248113
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
1 changed files with 27 additions and 38 deletions

View File

@ -4,7 +4,7 @@ import re
def migrate(db, habitlist, events):
c = db.cursor()
habits = simple_habits_list(c, habitlist)
habits = habit_list_add_ids(c, habitlist)
repetitions = get_all_repetitions(habits, events)
for rep in repetitions:
add_to_database(
@ -34,6 +34,23 @@ def get_all_repetitions(habits, events):
return repetitions
def extract_tags(text, tagmarker="#"):
"""Return lists of tuples of all event tags found in text.
Parameters:
text (str): The text to search through.
tagmarker (str): Optional character marking beginning of tag, defaults to '#'.
Returns:
tags (list): List of tuples in the form [('tag', '3'), ('anothertag', '')].
"""
string_tags = re.findall(rf"{tagmarker}(\w+)(?:\((\d+)\))?", text)
tags_with_int_counters = []
for tag in string_tags:
tags_with_int_counters.append((tag[0], None if tag[1] == "" else int(tag[1])))
return tags_with_int_counters
# does not do:
# non-range habits but still #habit(3) number included, adding multiple?
def tags_to_repetitions(habit, tags, timestamp):
"""Return a list of all repetitions generated from the tags and habits passed in.
Parameters:
@ -56,43 +73,21 @@ def tags_to_repetitions(habit, tags, timestamp):
return reps
def extract_tags(text, tagmarker="#"):
"""Return lists of tuples of all event tags found in text.
Parameters:
text (str): The text to search through.
tagmarker (str): Optional character marking beginning of tag, defaults to '#'.
Returns:
tags (list): List of tuples in the form [('tag', '3'), ('anothertag', '')].
"""
string_tags = re.findall(rf"{tagmarker}(\w+)(?:\((\d+)\))?", text)
tags_with_int_counters = []
for tag in string_tags:
tags_with_int_counters.append((tag[0], None if tag[1] == "" else int(tag[1])))
return tags_with_int_counters
# TODO possibly just get rid of this entirely
def simple_habits_list(c, habitlist):
"""Return simplified collection of habits, only carrying name, tag, id.
def habit_list_add_ids(c, habitlist):
"""Return the collection of habits with their sqlite id added.
Parameters:
c (sqlite.db.cursor): SQL cursor of database to query.
habitlist (list): Full habit collection to return a simplified view of.
Returns:
habitlist (list): Simple habitlist, habit name, tag, and id.
Copy of original, changes do not affect original.
habitlist (list): The full habit collection passed in unchanged,
except for an id added to each habit corresponding
to their id in sqlite database and Loop repetitions.
"""
simple = []
for h in habitlist:
simple.append(
{
"name": h["name"],
"description": h["description"],
"id": fetch_habit_id(c, h["uuid"]),
}
)
if "type" in h:
simple[-1]["type"] = h["type"]
return simple
with_id = habitlist.copy()
for h in with_id:
h["id"] = fetch_habit_id(c, h["uuid"])
return with_id
def fetch_habit_id(c, uuid):
@ -109,12 +104,6 @@ def fetch_habit_id(c, uuid):
return id[0]
# does not do:
# ranges in habits (should check for habit being range, then taking #habit(3) number into account)
# also, range trackers: multiply value by 1,000
# non-range habits but still #habit(3) number included, adding multiple?
def add_to_database(cursor, habit_id, timestamp, value=2):
try:
cursor.execute(