Skip to content

Commit

Permalink
refactor time
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Morris committed Oct 3, 2024
1 parent bddf59d commit 42b9a4e
Show file tree
Hide file tree
Showing 19 changed files with 9,028 additions and 15,189 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/daily-poem.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Poem of the day
on:
schedule:
- cron: "55 10 * * *" # daily at 10:55 UTC, so that it sends at 11 UTC (usually 06:00 AM EST)
- cron: "50 10 * * *" # daily at 10:50 UTC, so that it sends at 11 UTC (usually 06:00 AM EST)
workflow_dispatch:

jobs:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/hourly-test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: Hourly test
on:
schedule:
- cron: "0 0-10,12-23 * * *" # hourly, except at 12:00 UTC (07:00 EST)
# - cron: "0 */3 * * *" # three-hourly
- cron: "50 0-9,11-23 * * *" # hourly, except at 12:00 UTC (07:00 EST)

jobs:
run_tests:
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
All of the poems in here are good, or interesting. There are currently 8,967 poems in 44 (3.602) languages by 592 (255.198) authors from 54 (9.280) countries.
All of the poems in here are good, or interesting. There are currently 8,982 poems in 43 (3.575) languages by 592 (254.896) authors from 74 (11.037) countries.

(The quantites in the parentheses are the effective counts, based on the entropy of the distribution over the poems.)
9 changes: 8 additions & 1 deletion poems/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from .context import Context # noqa
from .curator import Curator # noqa
from .poem import Poem, Author # noqa
from .poem import Poem # noqa
from .objects import Author, Time # noqa

import logging

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s.%(msecs)03d %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
8 changes: 5 additions & 3 deletions poems/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import json
import os
import pathlib
import warnings
import yaml

from pandas import DataFrame
from .context import Context
from .poem import Poem, Author
from .utils import make_author_stats

import logging
logger = logging.getLogger("poems")

here, this_file = os.path.split(__file__)

CONTEXT_WEIGHTS = yaml.safe_load(pathlib.Path(f"{here}/data/weights.yml").read_text())
Expand Down Expand Up @@ -136,7 +138,7 @@ def apply_history(self, history: DataFrame, cooldown: int = 30 * 86400, manage_a
res = self.df.loc[author_mask & (self.df.title==entry.title)]

if not len(res):
warnings.warn(f"Could not remove poem '{entry.title}' by '{entry.author}'.")
logger.warning(f"Could not remove poem '{entry.title}' by '{entry.author}'.")

indices_to_drop.extend(res.index)

Expand Down Expand Up @@ -165,4 +167,4 @@ def _repr_html_(self):
return self.df._repr_html_()

def construct_poem(self, author, title):
return Poem(key=title, author=Author(key=author, **self.data[author]["metadata"]), context=self.context, **self.data[author]["poems"][title])
return Poem(key=title, author=Author(**self.data[author]["metadata"]), context=self.context, **self.data[author]["poems"][title])
6 changes: 3 additions & 3 deletions poems/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ def get_season(t=None):
winter_start_yday = datetime(year, 12, 1).timetuple().tm_yday
winter_end_yday = datetime(year, 3, 1).timetuple().tm_yday

spring_start_yday = datetime(year, 3, 21).timetuple().tm_yday
spring_start_yday = datetime(year, 4, 1).timetuple().tm_yday
spring_end_yday = datetime(year, 6, 1).timetuple().tm_yday

summer_start_yday = datetime(year, 6, 1).timetuple().tm_yday
summer_end_yday = datetime(year, 9, 1).timetuple().tm_yday

autumn_start_yday = datetime(year, 9, 21).timetuple().tm_yday
autumn_start_yday = datetime(year, 10, 1).timetuple().tm_yday
autumn_end_yday = datetime(year, 12, 1).timetuple().tm_yday

if (year_day < winter_end_yday) or (year_day > winter_start_yday):
if (year_day < winter_end_yday) or (year_day >= winter_start_yday):
return "winter"
if spring_start_yday <= year_day < spring_end_yday:
return "spring"
Expand Down
4 changes: 2 additions & 2 deletions poems/curator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .errors import AuthorNotFoundError, PoemNotFoundError
from .catalog import Catalog
from .poem import Author, Poem
from .data import authors
# from .data import authors

import pandas as pd

Expand All @@ -22,7 +22,7 @@ def get_author(self, author=None) -> Author:
author = np.random.choice(all_authors)

if author in all_authors:
return Author(key=author, **self.catalog.data[author]["metadata"])
return Author(**self.catalog.data[author]["metadata"])

raise ValueError(f"No author '{author}'.")

Expand Down
24 changes: 12 additions & 12 deletions poems/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
from ..objects import Author

here, this_filename = os.path.split(__file__)
flags = pd.read_csv(f"{here}/flags.csv", index_col=0)
countries = pd.read_csv(f"{here}/countries.csv", index_col=0).fillna("")


with open(f"{here}/poems.json", "r+") as f:
poems = json.load(f)

author_list = []
author_entries = {}
for author_key, author_data in poems.items():
author = Author(key=author_key, **author_data["metadata"])
author_list.append(author)
# author_list = []
# author_entries = {}
# for author_key, author_data in poems.items():
# author = Author(key=author_key, **author_data["metadata"])
# author_list.append(author)

author_entries[author_key] = {
"name": author.name,
"demonym": author.demonym.lower(),
# "dates": author.dates()[1:-1],
}
# author_entries[author_key] = {
# "name": author.name,
# "demonym": author.demonym.lower(),
# # "dates": author.dates()[1:-1],
# }

authors = pd.DataFrame(author_entries).T
# authors = pd.DataFrame(author_entries).T
2 changes: 2 additions & 0 deletions poems/data/flags.csv → poems/data/countries.csv
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,5 @@ zimbabwe,🇿🇼,Zimbabwe,Zimbabwean,:flag-zw:,1f1ff-1f1fc,&#127487;&#127484;,F
england,🏴󠁧󠁢󠁥󠁮󠁧󠁿,England,English,:England:,1F3F4 E0067 E0062 E0065 E006E E0067 E007F,&#x1F3F4;&#xE0067;&#xE0062;&#xE0065;&#xE006E;&#xE0067;&#xE007F;,Flags (subdivision-flag),,england
scotland,🏴󠁧󠁢󠁳󠁣󠁴󠁿,Scotland,Scottish,:Scotland:,1F3F4 E0067 E0062 E0073 E0063 E0074 E007F,&#x1F3F4;&#xE0067;&#xE0062;&#xE0073;&#xE0063;&#xE0074;&#xE007F;,Flags (subdivision-flag),,scotland
wales,🏴󠁧󠁢󠁷󠁬󠁳󠁿,Wales,Welsh,:wales:,1F3F4 E0067 E0062 E0077 E006C E0073 E007F,&#x1F3F4;&#xE0067;&#xE0062;&#xE0077;&#xE006C;&#xE0073;&#xE007F;,(subdivision-flag),,wales
roman-empire,,Roman Empire,Roman,,,,,,
persia,,Persia,Persian,,,,,,
Loading

0 comments on commit 42b9a4e

Please sign in to comment.