Last active
November 14, 2018 00:08
-
-
Save claudijd/15ccfe5f72841ff4bc2143728ae2ffd6 to your computer and use it in GitHub Desktop.
A demo of pickle usage vs. random usage respective to existing and proposed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import os.path | |
import random | |
# Old pickle queue logic (summarized) | |
pickle_cache_file = "pickle.queue" | |
reset_list_count = 0 | |
assignment_rounds = 50 | |
assignee_list_rotation_events = 0 | |
assignment_counts_pickle = { | |
"user1@mozilla.com": 0, | |
"user2@mozilla.com": 0, | |
"user3@mozilla.com": 0 | |
} | |
# If a queue is not initialized, create one | |
if not os.path.isfile(pickle_cache_file): | |
assign_hash = ['user1@mozilla.com', | |
'user2@mozilla.com', 'user3@mozilla.com'] | |
assign_list = assign_hash[:] | |
with open(pickle_cache_file, 'wb') as f: | |
pickle.dump((assign_list, assign_hash), f) | |
# Emulate a specific round | |
for iteration in range(1, assignment_rounds): | |
# Incremeent reset list counter | |
reset_list_count += 1 | |
with open(pickle_cache_file, 'rb') as f: | |
assign_list, assign_hash = pickle.load(f) | |
# Emulate a list reset at arbitrary times, to simulate an assignee edit | |
if reset_list_count % random.randint(1, 28) == 0: | |
assignee_list_rotation_events += 1 | |
assign_hash = ['user1@mozilla.com', | |
'user2@mozilla.com', 'user3@mozilla.com'] | |
assign_list = assign_hash[:] | |
assignee = assign_list.pop() | |
assign_list.insert(0, assignee) | |
# Add a count to whomever was assigned | |
assignment_counts_pickle[assignee] += 1 | |
with open(pickle_cache_file, 'wb') as f: | |
pickle.dump((assign_list, assign_hash), f) | |
print("Pickle Strategy... (with {} rotation edits)".format( | |
str(assignee_list_rotation_events))) | |
print(assignment_counts_pickle) | |
# New random.choice logic (summarized) | |
assign_hash = ['user1@mozilla.com', | |
'user2@mozilla.com', 'user3@mozilla.com'] | |
assignment_counts_random = { | |
"user1@mozilla.com": 0, | |
"user2@mozilla.com": 0, | |
"user3@mozilla.com": 0 | |
} | |
for iteration in range(1, assignment_rounds): | |
assignee = random.choice(assign_hash) | |
assignment_counts_random[assignee] += 1 | |
print("Random Strategy...") | |
print(assignment_counts_random) |
Note that results are not deterministic, so you will see differences depending on exactly when the list rotation is reset.
TLDR: I don't think either are as deterministic as we want in 50 assignment sample.
I ran a case just on pickle itself:
import pickle
try:
with open('test', 'rb') as f:
u = pickle.load(f)
except:
u = ['z', 'a', 'b', 'c']
print ('current', u)
with open ('test', 'wb') as f:
pickle.dump(u, f)
/tmp % for i in $(seq 1 10); do python ./a.py; done :(
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
current ['z', 'a', 'b', 'c']
ie it seems to keep the serialization the same (even without assign_hash)
maybe you're just testing https://gist.github.com/claudijd/15ccfe5f72841ff4bc2143728ae2ffd6#file-pickle_random_testing-py-L37 in this case
with rotation logic:
import pickle
try:
with open('test', 'rb') as f:
u = pickle.load(f)
except:
u = ['z', 'a', 'b', 'c']
print ('current', u)
# rotate
a = u.pop()
u.insert(0, a)
with open ('test', 'wb') as f:
pickle.dump(u, f)
/tmp % for i in $(seq 1 10); do python ./a.py; done
current ['z', 'a', 'b', 'c']
current ['c', 'z', 'a', 'b']
current ['b', 'c', 'z', 'a']
current ['a', 'b', 'c', 'z']
current ['z', 'a', 'b', 'c']
current ['c', 'z', 'a', 'b']
current ['b', 'c', 'z', 'a']
current ['a', 'b', 'c', 'z']
current ['z', 'a', 'b', 'c']
current ['c', 'z', 'a', 'b']
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ python ~/Desktop/pickle_test.py
Pickle Strategy... (with 10 rotation edits)
{'user1@mozilla.com': 13, 'user2@mozilla.com': 16, 'user3@mozilla.com': 20}
Random Strategy...
{'user1@mozilla.com': 20, 'user2@mozilla.com': 13, 'user3@mozilla.com': 16}