1
0
Fork 0

Refactor nested function out of generate_post for better readability.

This commit is contained in:
Casper V. Kristensen 2019-03-15 01:17:45 +01:00
parent abfbc34259
commit 3a06f9c56f
Signed by: caspervk
GPG key ID: 289CA03790535054

View file

@ -14,19 +14,6 @@ from .parsing import ParsedReleases, Release, ReleaseType
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def generate_post(parsed_releases: ParsedReleases) -> str:
post = []
for platform, platform_releases in parsed_releases.items():
if sum(len(pr) for pr in platform_releases.values()) == 0:
continue
post.append(f"# {platform}")
for release_type, releases in platform_releases.items():
if not releases:
continue
# Releases in the tables are grouped by release group, and the groups are ordered according to the most
# popular game within the group. Games are sorted by popularity internally in the groups as well.
def popularity(release: Release): def popularity(release: Release):
# The popularity of a game is defined by the number of reviews it has on Steam, however: # The popularity of a game is defined by the number of reviews it has on Steam, however:
# - We rank RIPs lower than non-RIPs so the same game released as both will sort the non-RIP first. # - We rank RIPs lower than non-RIPs so the same game released as both will sort the non-RIP first.
@ -34,19 +21,11 @@ def generate_post(parsed_releases: ParsedReleases) -> str:
is_rip = "RIP" in [tag.upper() for tag in release.tags] is_rip = "RIP" in [tag.upper() for tag in release.tags]
return len(release.highlights), release.num_reviews, not is_rip return len(release.highlights), release.num_reviews, not is_rip
group_order = defaultdict(lambda: (-1, False))
for release in releases:
group_order[release.group] = max(group_order[release.group], popularity(release))
def order(release: Release):
return (group_order[release.group],
release.group, # ensure grouping if two groups share group_order
popularity(release))
def row(release: Release): def row(release: Release):
# The rows in the table containing updates will use the full rls_name as the name, while tables # The rows in the table containing updates will use the full rls_name as the name, while tables
# containing game and DLC releases will show tags and highlights, as well as the stylized game_name. # containing game and DLC releases will show tags and highlights, as well as the stylized game_name.
if release_type == ReleaseType.UPDATE: if release.type == ReleaseType.UPDATE:
name = f"[{release.rls_name}]({release.nfo_link})" name = f"[{release.rls_name}]({release.nfo_link})"
else: else:
tags = " ({})".format(" ".join(release.tags)) if release.tags else "" tags = " ({})".format(" ".join(release.tags)) if release.tags else ""
@ -56,19 +35,45 @@ def generate_post(parsed_releases: ParsedReleases) -> str:
release.nfo_link, release.nfo_link,
highlights) highlights)
stores = ", ".join(f"[{name}]({link})" for name, link in release.store_links.items())
if release.score == -1: if release.score == -1:
reviews = "-" reviews = "-"
else: else:
num_reviews_humanized = util.humanize(release.num_reviews, precision=1, prefix="dec", suffix="") num_reviews_humanized = util.humanize(release.num_reviews, precision=1, prefix="dec", suffix="")
reviews = f"{release.score:.0%} ({num_reviews_humanized})" reviews = f"{release.score:.0%} ({num_reviews_humanized})"
stores = ", ".join(f"[{name}]({link})" for name, link in release.store_links.items())
return name, release.group, stores, reviews return name, release.group, stores, reviews
post.append(f"| {release_type} | Group | Store | Score (Reviews) |")
def generate_post(releases: ParsedReleases) -> str:
post = []
for platform, platform_releases in releases.items():
# Skip if platform doesn't contain any releases in any of the three release type categories
if sum(len(pr) for pr in platform_releases.values()) == 0:
continue
post.append(f"# {platform}")
for type, type_releases in platform_releases.items():
if not type_releases:
continue
# Releases in the tables are grouped by release group, and the groups are ordered according to the most
# popular game within the group. Games are sorted by popularity internally in the groups as well.
group_order = defaultdict(lambda: (0, -1, False))
for release in type_releases:
group_order[release.group] = max(group_order[release.group], popularity(release))
def order(release: Release):
return (group_order[release.group],
release.group, # ensure grouping if two groups share group_order
popularity(release))
type_releases.sort(key=order, reverse=True)
post.append(f"| {type} | Group | Store | Score (Reviews) |")
post.append("|:-|:-|:-|:-|") post.append("|:-|:-|:-|:-|")
post.extend("| {} | {} | {} | {} |".format(*row(rls)) for rls in sorted(releases, key=order, reverse=True)) post.extend("| {} | {} | {} | {} |".format(*row(rls)) for rls in type_releases)
post.append("") post.append("")
post.append(" ") post.append(" ")