} for text in filter(None, re.split(r" *\d+\) *", cells[0]))] elif cells[0].lower().startswith("{dream "): dreams = [{ "text": text } for text in filter( None, re.split(r" *{dream ?\d+\} *", cells[0], flags=re.I))] elif cells[0].lower().startswith("{1"): dreams = [{ "text": text } for text in filter(None, re.split(r" *{\d+\} *", cells[0]))] else: dreams = [{"text": cells[0]}] dream_data = {"dreams": dreams} if cells[1] != "not in use": if cells[1] == "unknown": dream_data["context"] = None else: dream_data["context"] = cells[1] if cells[2] != "not in use": if cells[1] == "unknown": dream_data["feeling"] = None else: dream_data["feeling"] = cells[2] if cells[3] not in ["not in use", "none"]: dream_data["disorientation"] = int(cells[3]) if cells[4] not in ["not in use", "none"]: dream_data["lewdness"] = int(cells[4]) data_source.update_entry_for_stat_on_date(stat_name, entry_date, dream_data, source) print(json.dumps(dream_data, indent=2))
def add_tz_if_missing(dt_str: str) -> str: dt = dateutil.parser.parse(dt_str) if dt.tzinfo: return dt_str return dt.replace(tzinfo=tzutc()).isoformat() if __name__ == "__main__": data_source = DataSource() start_date = datetime.datetime(2016, 4, 11) end_date = datetime.datetime(2020, 10, 25) sleep_data_resp = data_source.get_entries_for_stat_over_range( "sleep", start_date, end_date) for sleep_stat in sleep_data_resp: sleep_datum = sleep_stat["data"] print(sleep_stat) sleep_datum['sleep_time'] = add_tz_if_missing( sleep_datum['sleep_time']) sleep_datum['wake_time'] = add_tz_if_missing(sleep_datum['wake_time']) for i in sleep_datum.get('interruptions', []): if "sleep_time" in i: i['sleep_time'] = add_tz_if_missing(i["sleep_time"]) if "wake_time" in i: i['wake_time'] = add_tz_if_missing(i['wake_time']) print(sleep_stat) data_source.update_entry_for_stat_on_date("sleep", sleep_stat["date"], sleep_datum, sleep_stat["source"])
from data_source import DataSource migration_dict = {"spreadsheet": "parsed from manual spreadsheet entry"} data_source = DataSource() for entry in data_source.get_entries_over_range("earliest", "latest"): if entry["source"] in migration_dict: data_source.update_entry_for_stat_on_date( entry["stat_name"], entry["date"], entry["data"], migration_dict[entry["source"]])