Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
topydo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
topydo
Commits
1d333708
Commit
1d333708
authored
Jul 22, 2017
by
Bram Schoenmakers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update docstrings
parent
e22bfbde
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
18 deletions
+26
-18
topydo/lib/HashListValues.py
topydo/lib/HashListValues.py
+26
-18
No files found.
topydo/lib/HashListValues.py
View file @
1d333708
...
@@ -63,25 +63,19 @@ _TABLE_SIZES = {
...
@@ -63,25 +63,19 @@ _TABLE_SIZES = {
}
}
def
_to_base
(
p_alphabet
,
p_value
):
"""
Converts integer to text ID with characters from the given alphabet.
Based on answer on
https://stackoverflow.com/questions/1181919/python-base-36-encoding
"""
result
=
''
while
p_value
:
p_value
,
i
=
divmod
(
p_value
,
len
(
p_alphabet
))
result
=
p_alphabet
[
i
]
+
result
return
result
or
p_alphabet
[
0
]
class
_TableSizeException
(
Exception
):
class
_TableSizeException
(
Exception
):
pass
pass
def
_get_table_size
(
p_alphabet
,
p_num
):
def
_get_table_size
(
p_alphabet
,
p_num
):
# choose a larger key size if there's >1% chance of collision
"""
Returns a prime number that is suitable for the hash table size. The size
is dependent on the alphabet used, and the number of items that need to be
hashed. The table size is at least 100 times larger than the number of
items to be hashed, to avoid collisions.
When the alphabet is too little or too large, then _TableSizeException is
raised. Currently an alphabet of 10 to 40 characters is supported.
"""
try
:
try
:
for
width
,
size
in
sorted
(
_TABLE_SIZES
[
len
(
p_alphabet
)].
items
()):
for
width
,
size
in
sorted
(
_TABLE_SIZES
[
len
(
p_alphabet
)].
items
()):
if
p_num
<
size
*
0.01
:
if
p_num
<
size
*
0.01
:
...
@@ -96,13 +90,27 @@ def hash_list_values(p_list, p_key=lambda i: i): # pragma: no branch
...
@@ -96,13 +90,27 @@ def hash_list_values(p_list, p_key=lambda i: i): # pragma: no branch
Calculates a unique value for each item in the list, these can be used as
Calculates a unique value for each item in the list, these can be used as
identifiers.
identifiers.
The value is based on hashing an item using the p_
hash
function.
The value is based on hashing an item using the p_
key
function.
Suitable for lists not larger than approx. 16K items.
Suitable for lists not larger than approx. 16K items.
Returns a tuple with the status and a list of tuples where each item is
Returns a tuple with the status and a list of tuples where each item is
combined with the ID.
combined with the ID.
"""
"""
def
to_base
(
p_alphabet
,
p_value
):
"""
Converts integer to text ID with characters from the given alphabet.
Based on answer at
https://stackoverflow.com/questions/1181919/python-base-36-encoding
"""
result
=
''
while
p_value
:
p_value
,
i
=
divmod
(
p_value
,
len
(
p_alphabet
))
result
=
p_alphabet
[
i
]
+
result
return
result
or
p_alphabet
[
0
]
result
=
[]
result
=
[]
used
=
set
()
used
=
set
()
alphabet
=
config
().
identifier_alphabet
()
alphabet
=
config
().
identifier_alphabet
()
...
@@ -127,14 +135,14 @@ def hash_list_values(p_list, p_key=lambda i: i): # pragma: no branch
...
@@ -127,14 +135,14 @@ def hash_list_values(p_list, p_key=lambda i: i): # pragma: no branch
hash_value
=
(
hash_value
+
1
)
%
size
hash_value
=
(
hash_value
+
1
)
%
size
used
.
add
(
hash_value
)
used
.
add
(
hash_value
)
result
.
append
((
item
,
_
to_base
(
alphabet
,
hash_value
)))
result
.
append
((
item
,
to_base
(
alphabet
,
hash_value
)))
return
result
return
result
def
max_id_length
(
p_num
):
def
max_id_length
(
p_num
):
"""
"""
Returns the length of the IDs used, given the number of items that are
Returns the length of the IDs used, given the number of items that are
assigned an ID.
assigned an ID.
Used for padding in lists.
"""
"""
try
:
try
:
alphabet
=
config
().
identifier_alphabet
()
alphabet
=
config
().
identifier_alphabet
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment