Skip to content

Commit

Permalink
better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Morris committed Oct 3, 2024
1 parent 16dda0e commit ab1addd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions poems/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def apply_context(self, context: Context, forced=[], verbose=False):
if keyword == context_dict[category]:
if keyword in forced:
if verbose:
print(f"FORCING CONTEXT: {category}='{keyword}'")
logger.info(f"FORCING CONTEXT: {category}='{keyword}'")
multiplier = 1e18
else:
multiplier = weight
Expand Down Expand Up @@ -155,7 +155,7 @@ def apply_history(self, history: DataFrame, cooldown: int = 30 * 86400, manage_a
treated_authors.append(entry.author)

if verbose:
print(f"Dropped authors {dropped_authors}.")
logger.info(f"Dropped authors {dropped_authors}.")


self.df.loc[indices_to_drop, "likelihood"] = 0
Expand Down
9 changes: 6 additions & 3 deletions poems/curator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from .data import authors

import pandas as pd
import logging

logger = logging.getLogger("poems")

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

Expand Down Expand Up @@ -76,19 +79,19 @@ def get_poem(

author_summary = pd.DataFrame(author_summary_entries).T.sort_values(["probability"], ascending=[False])

print(f"choosing from {len(self.catalog.df)} poems; the most likely are:")
logger.info(f"choosing from {len(self.catalog.df)} poems; the most likely are:")
print(catalog_summary.iloc[:20].to_string())
print()

print(f"choosing from {len(author_summary)} authors; the most likely are:")
logger.info(f"choosing from {len(author_summary)} authors; the most likely are:")
print(author_summary.iloc[:20].to_string())
print()

chosen_loc = np.random.choice(self.catalog.df.index, p=self.catalog.df.probability)
chosen_author, chosen_title = self.catalog.df.loc[chosen_loc, ["author", "title"]]

if verbose:
print(f"chose poem '{chosen_title}' by {chosen_author}")
logger.info(f"chose poem '{chosen_title}' by {chosen_author}")

self.catalog.reset()

Expand Down
2 changes: 0 additions & 2 deletions poems/tests/test_curator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from poems import Curator



def test_curator():
curator = Curator()
p = curator.get_poem(very_verbose=True)
Expand Down
6 changes: 3 additions & 3 deletions poems/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def email_thread(username: str,
content=content,
recipient=recipient)
a, b = recipient.split("@")
print(f"{datetime.now().isoformat()} | {a:>24} @ {b:<20}")
logger.info(f"Set email to {a:>24} @ {b:<20}")
done = True
except Exception as error:
logger.warning(error)
print(f"Encountered error for recipient {recipient}. Trying again in 60 seconds...")
logger.warning(f"Encountered error for recipient {recipient}. Trying again in 60 seconds...")
fails += 1
time.sleep(60)

Expand Down Expand Up @@ -102,7 +102,7 @@ def write_to_repo(repo, items, branch="master", verbose=False):

blob = repo.create_git_blob(content, "utf-8")
elements.append(gh.InputGitTreeElement(path=filename, mode="100644", type="blob", sha=blob.sha))
if verbose: print(f"writing to {filename}")
if verbose: logger.info(f"writing to {filename}")

head_sha = repo.get_branch(branch).commit.sha
base_tree = repo.get_git_tree(sha=head_sha)
Expand Down
6 changes: 3 additions & 3 deletions scripts/send-poem.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
when = ttime.time() if args.mode == "daily" else ttime.time() + random.uniform(low=0, high=365 * 86400)
context = Context(timestamp=when)

print(f"using context {context.to_dict()}")
logger.info(f"using context {context.to_dict()}")

curator.catalog.apply_context(context, forced=["holy_thursday", "good_friday", "holy_saturday", "easter_sunday", "christmas_eve", "christmas_day"])
curator.catalog.apply_history(history, verbose=True)
Expand All @@ -57,7 +57,7 @@

wait_seconds = (start_time - now).seconds
if wait_seconds < 600:
logger.info(f"Waiting {wait_seconds:.03f} seconds.")
logger.info(f"Waiting {int(wait_seconds)} seconds.")
ttime.sleep(wait_seconds)

now = Context.now()
Expand Down Expand Up @@ -96,7 +96,7 @@
daily_poems[str(index)] = packet

except Exception as e:
logger.warning(f"Could not find poem for entry {entry}")
logger.warning(f"Could not find poem for entry {entry.to_dict()}")

utils.write_to_repo(repo,
items={
Expand Down

0 comments on commit ab1addd

Please sign in to comment.