Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
baseplate-admin committed Feb 23, 2024
1 parent 6aa4940 commit bf6cbb8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
26 changes: 0 additions & 26 deletions django_ltree/guess_label_size.py

This file was deleted.

28 changes: 26 additions & 2 deletions django_ltree/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from itertools import product

from django_ltree.fields import PathValue
from django_ltree.guess_label_size import guess_the_label_size
import math


class PathGenerator(object):
Expand All @@ -15,7 +15,7 @@ def __init__(self, prefix=None, skip=None):
self.path_prefix = prefix if prefix else []
self.product_iterator = product(
combinations,
repeat=guess_the_label_size(
repeat=self.guess_the_label_size(
path_size=len(self.skip_paths), combination_size=len(combinations)
),
)
Expand All @@ -31,3 +31,27 @@ def __next__(self):
return path

next = __next__

def guess_the_label_size(self, path_size: int, combination_size: int) -> int:
# The theoritical limit for this at the time of writing is 2_538_557_185_841_324_496 (python 3.12.2)
calculated_path_size = 0
# The theoritical limit for this at the time of writing is 32 (python 3.12.2)
label_size = 0

# THIS IS AN VERY IMPORTANT CHECK
last = 0

while True:
possible_cominations = math.comb(combination_size, label_size)
if last > possible_cominations:
raise ValueError("We approached the limit of `math.comb`")

last = possible_cominations
calculated_path_size += possible_cominations

if calculated_path_size > path_size and label_size != 0:
break

label_size += 1

return label_size
2 changes: 0 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,3 @@ def test_slicing(db):
create_test_data()
qs = Taxonomy.objects.all()
assert qs[:3].count() == 3


0 comments on commit bf6cbb8

Please sign in to comment.