-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique References #684
Merged
Merged
Unique References #684
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
9e9a945
WIP: deep references using SQLAlchemy
a8431da
WIP: deep references using SQL Lite
384ef26
Start of support for nicknames
cbdbdff
Tests of corner cases
29c9875
Lazy loading
091f343
Lookup in nicknames
be8f726
Recreate just_once objects after continuation
2c91ae9
Dump defaultdict
ad35da8
One table per sobject and lazy-loaded references to sobjects
c4982d1
Indexes
9461f40
Cleanups in memory feature
2c74746
Docs and tests
a0d9772
Add nickname_id for looking up by nickname
f69955e
Start roughing in unique feature
66751ad
Merge remote-tracking branch 'origin/main' into feature/deep-references
95388a9
Add tests and remove comments
83a82fe
Minor refactorings
48d72b7
Minor cleanups
bd45550
Support serializing LazyLoadedObjectReference's.
1cff911
Lazy load stuff even Nicknames
6c6be64
Fix bug & benchmark
997d957
Missing file
dfd9dd5
Unique random_references
ef63376
Merge remote-tracking branch 'origin/main' into feature/unique-refere…
98d9b04
Fix tests and error messages.
0384dd0
Merge remote-tracking branch 'origin/main' into feature/unique-refere…
fe34a8a
Fix pragma
9d408bf
Add a feature for scoping to parent
4fe0097
Docs
d6a8e84
Docs
cd3508d
Fix minor bugs
64b30bb
Update terminology
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- object: Campaign | ||
count: 5 | ||
fields: | ||
Name: Campaign ${{child_index}} | ||
- object: Contact | ||
count: 3 | ||
fields: | ||
FirstName: | ||
fake: FirstName | ||
LastName: | ||
fake: LastName | ||
friends: | ||
- object: CampaignMember | ||
count: 5 | ||
fields: | ||
ContactId: | ||
reference: Contact | ||
CampaignId: | ||
random_reference: | ||
to: Campaign | ||
parent: Contact | ||
unique: True |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import typing as T | ||
import random | ||
import math | ||
|
||
|
||
class UpdatableRandomRange: | ||
def __init__(self, start: int, stop: int = None): | ||
assert stop > start | ||
self.start = start | ||
self._set_new_range_immediately(start, stop) | ||
|
||
def set_new_top(self, new_top: int): | ||
# do not replace RNG until old one is exhausted | ||
assert new_top >= self.cur_stop | ||
self.cur_stop = new_top | ||
|
||
def set_new_range(self, new_bottom: int, new_top: int): | ||
"""Update the range subject to constraints | ||
|
||
There are two modes: | ||
|
||
If you update the range by changing only the top value, | ||
the generator will finish generating the first list before | ||
expanding its scope. | ||
|
||
So if you configured it with range(0,10) and then | ||
range(0,20) you would get | ||
|
||
shuffle(list(range(0,10)) + shuffle(list(range(10,20)) | ||
|
||
Not: | ||
|
||
shuffle(list(range(0,10) + list(range(10,20)) | ||
|
||
If you update the range by changing both values, the previous | ||
generator is just discarded, because you presumably don't | ||
want those values anymore. The new bottom must be higher | ||
than the old top. This preserves the rule that no value is | ||
ever produced twice. | ||
""" | ||
if new_bottom == self.start: | ||
self.set_new_top(new_top) | ||
else: | ||
assert new_bottom >= self.orig_stop, (new_bottom, self.orig_stop) | ||
self._set_new_range_immediately(new_bottom, new_top) | ||
|
||
def _set_new_range_immediately(self, new_bottom: int, new_top: int): | ||
assert new_top > new_bottom | ||
self.start = new_bottom | ||
self.orig_stop = self.cur_stop = new_top | ||
self.num_generator = random_range(self.start, self.orig_stop) | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
rv = next(self.num_generator, None) | ||
|
||
if rv is not None: | ||
return rv | ||
|
||
if self.cur_stop <= self.orig_stop: | ||
raise StopIteration() | ||
|
||
self.start = self.orig_stop | ||
self.num_generator = random_range(self.start, self.cur_stop) | ||
self.orig_stop = self.cur_stop | ||
return next(self.num_generator) | ||
|
||
|
||
def random_range(start: int, stop: int) -> T.Generator[int, None, None]: | ||
""" | ||
Return a randomized "range" using a Linear Congruential Generator | ||
to produce the number sequence. Parameters are the same as for | ||
python builtin "range". | ||
Memory -- storage for 8 integers, regardless of parameters. | ||
Compute -- at most 2*"maximum" steps required to generate sequence. | ||
Based on https://stackoverflow.com/a/53551417/113477 | ||
|
||
# Set a default values the same way "range" does. | ||
""" | ||
step = 1 # step is hard-coded to "1" because it seemed to be buggy | ||
# and not important for our use-case | ||
|
||
# Use a mapping to convert a standard range into the desired range. | ||
def mapping(i): | ||
return (i * step) + start | ||
|
||
# Compute the number of numbers in this range. | ||
maximum = (stop - start) // step | ||
|
||
# Seed range with a random integer. | ||
value = random.randint(0, maximum) | ||
# | ||
# Construct an offset, multiplier, and modulus for a linear | ||
# congruential generator. These generators are cyclic and | ||
# non-repeating when they maintain the properties: | ||
# | ||
# 1) "modulus" and "offset" are relatively prime. | ||
# 2) ["multiplier" - 1] is divisible by all prime factors of "modulus". | ||
# 3) ["multiplier" - 1] is divisible by 4 if "modulus" is divisible by 4. | ||
# | ||
offset = random.randint(0, maximum) * 2 + 1 # Pick a random odd-valued offset. | ||
multiplier = ( | ||
4 * (maximum // 4) + 1 | ||
) # Pick a multiplier 1 greater than a multiple of 4. | ||
modulus = int( | ||
2 ** math.ceil(math.log2(maximum)) | ||
) # Pick a modulus just big enough to generate all numbers (power of 2). | ||
# Track how many random numbers have been returned. | ||
found = 0 | ||
while found < maximum: | ||
# If this is a valid value, yield it in generator fashion. | ||
if value < maximum: | ||
found += 1 | ||
yield mapping(value) | ||
# Calculate the next value in the sequence. | ||
value = (value * multiplier + offset) % modulus |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to get cause up in semantics but lower limit and upper limit are the mathematical terms should we stay true to this here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jofsky Changed in 64b30bb