-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityDict.py
29 lines (23 loc) · 1015 Bytes
/
priorityDict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import hashlib
import random
class PriorityDictionary:
'''
A PriorityDictionary P allows for the mapping of keys to priority values,
which are consistent from call to call. That is,
P.getPriority(x) returns a priority for a hashable object x
where the same priority is returned if x's priority is re-queried.
When debugging is on, priority is computed via md5.
When debugging is off, priority is assigned truly randomly.
'''
def __init__(self, debug=True, seed=None):
self.priorityDictionary = {}
if debug:
self.priorityCalculator = lambda x : hashlib.md5(x.encode('utf-8')).hexdigest()
else:
self.priorityCalculator = lambda _ : random.random()
if (seed):
random.seed(seed)
def getPriority(self, s):
if s not in self.priorityDictionary:
self.priorityDictionary[s] = self.priorityCalculator(s)
return self.priorityDictionary[s]