Created
June 15, 2023 20:47
-
-
Save fulcrum6378/2b61bf8264e6506f5dba7a285d9b7080 to your computer and use it in GitHub Desktop.
Statistics creator for Fortuna application using Matplotlib which takes the exported Vita file from Fortuna which must be in Iranian calendar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from calendar import monthrange | |
from datetime import date | |
from typing import Dict, List, Optional | |
import matplotlib.pyplot as plt | |
class Luna: | |
diebus: List[Optional[float]] | |
emojis: List[Optional[str]] | |
verba: List[Optional[str]] | |
default: Optional[float] | |
emoji: Optional[str] | |
verbum: Optional[str] | |
def __init__(self, calendar: date, default: Optional[float], | |
emoji: Optional[str], verbum: Optional[str]): | |
max_dies = monthrange(calendar.year, calendar.month)[1] | |
self.diebus = [None] * max_dies | |
self.emojis = [None] * max_dies | |
self.verba = [None] * max_dies | |
self.default = default | |
self.emoji = emoji | |
self.verbum = verbum | |
def __getitem__(self, item): | |
return self.diebus[item] | |
file = "fortuna.vita" | |
args = sys.argv[1:] | |
if len(args) > 0: file = args[0] | |
with open(file, "r", encoding='utf-8') as f: text = f.read() | |
vita: Dict[str, Luna] = dict() | |
key: Optional[str] = None | |
loadVitaText = lambda st: st.replace("\\n", "\n") | |
dies = 0 | |
for ln in text.split("\n"): | |
if key is None: | |
if not ln.startswith('@'): continue | |
sn = ln.split(";", 3) | |
s = sn[0].split("~") | |
key = s[0][1:] | |
splitKey = key.split(".") | |
vita[key] = Luna(date(int(splitKey[0]), int(splitKey[1]), 1), | |
float(s[1]) if len(s) > 1 else None, | |
sn[1] if len(sn) > 1 else None, | |
loadVitaText(sn[2]) if len(sn) > 2 else None) | |
dies = 0 | |
else: | |
if len(ln) == 0: | |
key = None | |
continue | |
sn = ln.split(";", 3) | |
s = sn[0].split(":") | |
if len(s) == 2: | |
dies = int(s[0]) - 1 | |
vita[key].diebus[dies] = float(s[1] if len(s) > 1 else s[0]) | |
vita[key].emojis[dies] = sn[1] if len(sn) > 1 else None | |
vita[key].verba[dies] = loadVitaText(sn[2]) if len(sn) > 2 else None | |
dies += 1 | |
dates = list[date]() | |
scores = list[float]() | |
for k, luna in vita.items(): | |
key_split = k.split(".") | |
for day in range(len(luna.diebus)): | |
try: | |
dat = date(int(key_split[0]), int(key_split[1]), day + 1) | |
except Exception as e: | |
raise Exception(k, day, e.args) | |
if luna[day] is not None: | |
dates.append(dat) | |
scores.append(luna[day]) | |
elif luna.default is not None: | |
dates.append(dat) | |
scores.append(luna.default) | |
plt.plot(dates, scores) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment