Skip to content

Commit

Permalink
Replace triple nested for loops with itertools.product
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHall2020 committed Dec 17, 2024
1 parent 470d14e commit ef4bd0b
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 147 deletions.
94 changes: 47 additions & 47 deletions archeryutils/classifications/agb_field_classifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
agb_field_classification_scores
"""

import itertools
from typing import Any, TypedDict

import numpy as np
Expand Down Expand Up @@ -76,55 +77,54 @@ def _make_agb_field_classification_dict() -> dict[str, GroupData]:
agb_classes_field = agb_classes_info_field["classes"]
agb_classes_field_long = agb_classes_info_field["classes_long"]


# Generate dict of classifications
# loop over bowstyles
# loop over genders
# loop over ages
# loop over all bowstyles, genders, ages
classification_dict = {}
for bowstyle in agb_bowstyles:
for gender in agb_genders:
for age in agb_ages:
groupname = cls_funcs.get_groupname(
bowstyle["bowstyle"], gender, age["age_group"]
)

# Get max dists for category from json file data
# Use metres as corresponding yards >= metric
dists = _assign_dists(bowstyle["bowstyle"], age)

# set step from datum based on age and gender steps required
delta_hc_age_gender = cls_funcs.get_age_gender_step(
gender,
age["step"],
bowstyle["ageStep_field"],
bowstyle["genderStep_field"],
)

classifications_count = len(agb_classes_field)

class_hc = np.empty(classifications_count)

min_dists = np.empty(classifications_count)
min_dists[0:6] = dists[0]
min_dists[6:9] = [max(dists[0] - 10 * i, 30) for i in range(1, 4)]

for i in range(classifications_count):
# Assign handicap for this classification
class_hc[i] = (
bowstyle["datum_field"]
+ delta_hc_age_gender
+ (i - 2) * bowstyle["classStep_field"]
)

groupdata: GroupData = {
"classes": agb_classes_field,
"classes_long": agb_classes_field_long,
"class_HC": class_hc,
"max_distance": dists[1],
"min_dists": min_dists,
}

classification_dict[groupname] = groupdata
for bowstyle, gender, age in itertools.product(
agb_bowstyles, agb_genders, agb_ages
):
groupname = cls_funcs.get_groupname(
bowstyle["bowstyle"], gender, age["age_group"]
)

# Get max dists for category from json file data
# Use metres as corresponding yards >= metric
dists = _assign_dists(bowstyle["bowstyle"], age)

# set step from datum based on age and gender steps required
delta_hc_age_gender = cls_funcs.get_age_gender_step(
gender,
age["step"],
bowstyle["ageStep_field"],
bowstyle["genderStep_field"],
)

classifications_count = len(agb_classes_field)

class_hc = np.empty(classifications_count)

min_dists = np.empty(classifications_count)
min_dists[0:6] = dists[0]
min_dists[6:9] = [max(dists[0] - 10 * i, 30) for i in range(1, 4)]

for i in range(classifications_count):
# Assign handicap for this classification
class_hc[i] = (
bowstyle["datum_field"]
+ delta_hc_age_gender
+ (i - 2) * bowstyle["classStep_field"]
)

groupdata: GroupData = {
"classes": agb_classes_field,
"classes_long": agb_classes_field_long,
"class_HC": class_hc,
"max_distance": dists[1],
"min_dists": min_dists,
}

classification_dict[groupname] = groupdata

return classification_dict

Expand Down
73 changes: 36 additions & 37 deletions archeryutils/classifications/agb_indoor_classifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
agb_indoor_classification_scores
"""

import itertools
from typing import TypedDict

import numpy as np
Expand Down Expand Up @@ -73,44 +74,42 @@ def _make_agb_indoor_classification_dict() -> dict[str, GroupData]:
agb_classes_in_long = agb_classes_info_in["classes_long"]

# Generate dict of classifications
# loop over bowstyles
# loop over ages
# loop over genders
# loop over all bowstyles, genders, ages
classification_dict = {}
for bowstyle in agb_bowstyles:
for gender in agb_genders:
for age in agb_ages:
groupname = cls_funcs.get_groupname(
bowstyle["bowstyle"],
gender,
age["age_group"],
)

# set step from datum based on age and gender steps required
delta_hc_age_gender = cls_funcs.get_age_gender_step(
gender,
age["step"],
bowstyle["ageStep_in"],
bowstyle["genderStep_in"],
)

classifications_count = len(agb_classes_in)

class_hc = np.empty(classifications_count)
for i in range(classifications_count):
# Assign handicap for this classification
class_hc[i] = (
bowstyle["datum_in"]
+ delta_hc_age_gender
+ (i - 1) * bowstyle["classStep_in"]
)

groupdata: GroupData = {
"classes": agb_classes_in,
"classes_long": agb_classes_in_long,
"class_HC": class_hc,
}
classification_dict[groupname] = groupdata
for bowstyle, gender, age in itertools.product(
agb_bowstyles, agb_genders, agb_ages
):
groupname = cls_funcs.get_groupname(
bowstyle["bowstyle"],
gender,
age["age_group"],
)

# set step from datum based on age and gender steps required
delta_hc_age_gender = cls_funcs.get_age_gender_step(
gender,
age["step"],
bowstyle["ageStep_in"],
bowstyle["genderStep_in"],
)

classifications_count = len(agb_classes_in)

class_hc = np.empty(classifications_count)
for i in range(classifications_count):
# Assign handicap for this classification
class_hc[i] = (
bowstyle["datum_in"]
+ delta_hc_age_gender
+ (i - 1) * bowstyle["classStep_in"]
)

groupdata: GroupData = {
"classes": agb_classes_in,
"classes_long": agb_classes_in_long,
"class_HC": class_hc,
}
classification_dict[groupname] = groupdata

return classification_dict

Expand Down
125 changes: 62 additions & 63 deletions archeryutils/classifications/agb_outdoor_classifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
agb_outdoor_classification_scores
"""

import itertools
from typing import Any, Literal, TypedDict, cast

import numpy as np
Expand Down Expand Up @@ -73,70 +74,68 @@ def _make_agb_outdoor_classification_dict() -> dict[str, GroupData]:
agb_classes_out_long = agb_classes_info_out["classes_long"]

# Generate dict of classifications
# loop over bowstyles
# loop over genders
# loop over ages
# loop over all bowstyles, genders, ages
classification_dict = {}
for bowstyle in agb_bowstyles:
for gender in agb_genders:
for age in agb_ages:
groupname = cls_funcs.get_groupname(
bowstyle["bowstyle"],
gender,
age["age_group"],
)

# Get max dists for category from json file data
# Use metres as corresponding yards >= metric
gender_key = cast(Literal["male", "female"], gender.lower())
max_dist = age[gender_key]

# set step from datum based on age and gender steps required
delta_hc_age_gender = cls_funcs.get_age_gender_step(
gender,
age["step"],
bowstyle["ageStep_out"],
bowstyle["genderStep_out"],
)
classifications_count = len(agb_classes_out)

class_hc = np.empty(classifications_count)
min_dists = np.empty(classifications_count)

for i in range(classifications_count):
# Assign handicap for this classification
class_hc[i] = (
bowstyle["datum_out"]
+ delta_hc_age_gender
+ (i - 2) * bowstyle["classStep_out"]
)

# Get minimum distance that must be shot for this classification
min_dists[i] = _assign_min_dist(
n_class=i,
gender=gender,
age_group=age["age_group"],
max_dists=max_dist,
)

# Assign prestige rounds for the category
prestige_rounds = _assign_outdoor_prestige(
bowstyle=bowstyle["bowstyle"],
age=age["age_group"],
gender=gender,
max_dist=max_dist,
)

groupdata: GroupData = {
"classes": agb_classes_out,
"max_distance": max_dist,
"classes_long": agb_classes_out_long,
"class_HC": class_hc,
"min_dists": min_dists,
"prestige_rounds": prestige_rounds,
}

classification_dict[groupname] = groupdata
for bowstyle, gender, age in itertools.product(
agb_bowstyles, agb_genders, agb_ages
):
groupname = cls_funcs.get_groupname(
bowstyle["bowstyle"],
gender,
age["age_group"],
)

# Get max dists for category from json file data
# Use metres as corresponding yards >= metric
gender_key = cast(Literal["male", "female"], gender.lower())
max_dist = age[gender_key]

# set step from datum based on age and gender steps required
delta_hc_age_gender = cls_funcs.get_age_gender_step(
gender,
age["step"],
bowstyle["ageStep_out"],
bowstyle["genderStep_out"],
)
classifications_count = len(agb_classes_out)

class_hc = np.empty(classifications_count)
min_dists = np.empty(classifications_count)

for i in range(classifications_count):
# Assign handicap for this classification
class_hc[i] = (
bowstyle["datum_out"]
+ delta_hc_age_gender
+ (i - 2) * bowstyle["classStep_out"]
)

# Get minimum distance that must be shot for this classification
min_dists[i] = _assign_min_dist(
n_class=i,
gender=gender,
age_group=age["age_group"],
max_dists=max_dist,
)

# Assign prestige rounds for the category
prestige_rounds = _assign_outdoor_prestige(
bowstyle=bowstyle["bowstyle"],
age=age["age_group"],
gender=gender,
max_dist=max_dist,
)

groupdata: GroupData = {
"classes": agb_classes_out,
"max_distance": max_dist,
"classes_long": agb_classes_out_long,
"class_HC": class_hc,
"min_dists": min_dists,
"prestige_rounds": prestige_rounds,
}

classification_dict[groupname] = groupdata

return classification_dict

Expand Down

0 comments on commit ef4bd0b

Please sign in to comment.