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 0951c03 commit cdd3922
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
29 changes: 29 additions & 0 deletions django_ltree/guess_label_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import math


def guess_the_label_size(path_size: int, combination_size: int):
if path_size == 0:
return 1

# 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:
break

label_size += 1

return label_size
29 changes: 2 additions & 27 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
import math
from django_ltree.guess_label_size import guess_the_label_size


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=self.guess_the_label_size(
repeat=guess_the_label_size(
path_size=len(self.skip_paths), combination_size=len(combinations)
),
)
Expand All @@ -31,28 +31,3 @@ def __next__(self):
return path

next = __next__

@staticmethod
def guess_the_label_size(path_size: int, combination_size: int):
if path_size == 0:
return 1

# 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 calculated_path_size < path_size:
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
label_size += 1

return label_size

0 comments on commit cdd3922

Please sign in to comment.