Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/PI-631] Generate Product ids #429

Merged
merged 6 commits into from
Dec 11, 2024

Conversation

jameslinnell
Copy link
Contributor

No description provided.

@jameslinnell jameslinnell requested a review from a team as a code owner November 29, 2024 14:00
@jameslinnell jameslinnell force-pushed the feature/PI-631-generate_product_ids branch from 3519854 to 861e56e Compare December 2, 2024 10:43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super minor but probably call this file product_id_generator.py just so we don't get confused with other ids :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would if we wanted in the future be able to generate other ids, it's just that by default it generates product_ids. So it is generic

Comment on lines 44 to 49
for _ in range(id_count):
saved = False
while not saved:
generated_id = generator()
saved = save_to_set(generated_id, ids)
ids = saved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe to make more concise:

Suggested change
for _ in range(id_count):
saved = False
while not saved:
generated_id = generator()
saved = save_to_set(generated_id, ids)
ids = saved
initial_len = len(ids)
while len(ids) - initial_len < id_count:
ids.add(generator())

Comment on lines 10 to 15
generated_product_ids = set()


@pytest.fixture(scope="module", autouse=True)
def _get_generated_ids():
global generated_product_ids
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
generated_product_ids = set()
@pytest.fixture(scope="module", autouse=True)
def _get_generated_ids():
global generated_product_ids
generated_product_ids = set()
@pytest.fixture(scope="module")
def generated_product_ids():

and then use the generated_product_ids fixture in the arg of the required test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way the fixture is called once, but ok.

Comment on lines 113 to 123
def create(cls):
"""No current_id needed, key is generated randomly."""
rng = random.Random(datetime.now().timestamp())
product_id = "-".join(
"".join(rng.choices(PRODUCT_ID_VALID_CHARS, k=PRODUCT_ID_PART_LENGTH))
for _ in range(PRODUCT_ID_NUMBER_OF_PARTS)
)
return cls(__root__=f"P.{product_id}")
existing_ids = cls.load_existing_ids()
while True:
"""No current_id needed, key is generated randomly."""
rng = random.Random(datetime.now().timestamp())
product_id = "-".join(
"".join(rng.choices(PRODUCT_ID_VALID_CHARS, k=PRODUCT_ID_PART_LENGTH))
for _ in range(PRODUCT_ID_NUMBER_OF_PARTS)
)
if f"P.{product_id}" not in existing_ids:
return cls(__root__=f"P.{product_id}")
Copy link
Contributor

@jaklinger jaklinger Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to minimise the change, perhaps could leave the entire method unchanged instead could just change the return line of the function to:

        if f"P.{product_id}" in cls.load_existing_ids():
            return cls.create()
        return cls(__root__=f"P.{product_id}")

Copy link
Contributor Author

@jameslinnell jameslinnell Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it this way so that the file is called once. In your suggestion the file would get called on each recursive flow. Your way seems less efficient

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd cache the load under the hood to avoid loading the file twice, so e.g.

@cache
def _load_existing_ids():
   # load the ids

...

class CantRememberTheNameOfTheClass:
   ...

   @classmethod
   def load_existing_ids(cls):
       return _load_existing_ids()

   ...

@jameslinnell jameslinnell force-pushed the feature/PI-631-generate_product_ids branch from adb10ab to 7c0e54f Compare December 5, 2024 12:19
@megan-bower4 megan-bower4 merged commit 1099bde into main Dec 11, 2024
21 of 22 checks passed
@megan-bower4 megan-bower4 deleted the feature/PI-631-generate_product_ids branch December 11, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants