-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Prescod
committed
Dec 23, 2022
1 parent
c0b561d
commit 82e7a4d
Showing
10 changed files
with
414 additions
and
6 deletions.
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
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,17 @@ | ||
- plugin: snowfakery.standard_plugins.Math | ||
- object: ParentObject__c | ||
count: 2 | ||
fields: | ||
TotalAmount__c: | ||
random_number: | ||
min: 30 | ||
max: 90 | ||
friends: | ||
- object: ChildObject__c | ||
for_each: | ||
var: child_value | ||
value: | ||
Math.random_partition: | ||
total: ${{ParentObject__c.TotalAmount__c}} | ||
fields: | ||
Amount__c: ${{child_value}} |
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,13 @@ | ||
- plugin: snowfakery.standard_plugins.Math | ||
|
||
- object: Values | ||
for_each: | ||
var: current_value | ||
value: | ||
Math.random_partition: | ||
total: 100 | ||
min: 10 | ||
max: 50 | ||
step: 0.1 | ||
fields: | ||
Amount: ${{current_value}} |
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,15 @@ | ||
- plugin: snowfakery.standard_plugins.Math | ||
- option: step | ||
default: 0.01 | ||
|
||
- object: Values | ||
for_each: | ||
var: current_value | ||
value: | ||
Math.random_partition: | ||
total: 100 | ||
min: 10 | ||
max: 50 | ||
step: ${{step}} | ||
fields: | ||
Amount: ${{current_value}} |
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,25 @@ | ||
# This shows how you could create a plugin or feature where | ||
# a parent object generates child objects which sum up | ||
# to any particular value. | ||
|
||
- plugin: examples.sum_totals.SummationPlugin | ||
- var: summation_helper | ||
value: | ||
SummationPlugin.summer: | ||
total: 100 | ||
step: 10 | ||
|
||
- object: ParentObject__c | ||
count: 10 | ||
fields: | ||
MinimumChildObjectAmount__c: 10 | ||
MinimumStep: 5 | ||
TotalAmount__c: ${{summation_helper.total}} | ||
friends: | ||
- object: ChildObject__c | ||
count: ${{summation_helper.count}} | ||
fields: | ||
Parent__c: | ||
reference: ParentObject__c | ||
Amount__c: ${{summation_helper.next_amount}} | ||
RunningTotal__c: ${{summation_helper.running_total}} |
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,13 @@ | ||
- plugin: snowfakery.standard_plugins.Math | ||
|
||
- object: Values | ||
for_each: | ||
var: current_value | ||
value: | ||
Math.random_partition: | ||
total: 100 | ||
min: 10 | ||
max: 50 | ||
step: 5 | ||
fields: | ||
Amount: ${{current_value}} |
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 |
---|---|---|
@@ -1,20 +1,105 @@ | ||
import math | ||
from snowfakery.plugins import SnowfakeryPlugin | ||
from random import randint, shuffle | ||
from types import SimpleNamespace | ||
from typing import List, Optional, Union | ||
from snowfakery.plugins import SnowfakeryPlugin, memorable, PluginResultIterator | ||
|
||
|
||
class Math(SnowfakeryPlugin): | ||
def custom_functions(self, *args, **kwargs): | ||
"Expose math functions to Snowfakery" | ||
|
||
class MathNamespace: | ||
pass | ||
class MathNamespace(SimpleNamespace): | ||
@memorable | ||
def random_partition( | ||
self, | ||
total: int, | ||
*, | ||
min: int = 1, | ||
max: Optional[int] = None, | ||
step: int = 1, | ||
): | ||
return GenericPluginResultIterator(False, parts(total, min, max, step)) | ||
|
||
mathns = MathNamespace() | ||
mathns.__dict__ = math.__dict__.copy() | ||
mathns.__dict__.update(math.__dict__.copy()) | ||
|
||
mathns.pi = math.pi | ||
mathns.round = round | ||
mathns.min = min | ||
mathns.max = max | ||
|
||
mathns.context = self.context | ||
return mathns | ||
|
||
|
||
class GenericPluginResultIterator(PluginResultIterator): | ||
def __init__(self, repeat, iterable): | ||
super().__init__(repeat) | ||
self.next = iter(iterable).__next__ | ||
|
||
|
||
def parts(total: int, min_: int = 1, max_=None, step=1) -> List[Union[int, float]]: | ||
"""Split a number into a randomized set of 'pieces'. | ||
The pieces add up to the `total`. E.g. | ||
parts(12) -> [3, 6, 3] | ||
parts(16) -> [8, 4, 2, 2] | ||
The numbers generated will never be less than `min_`, if provided. | ||
The numbers generated will never be less than `max_`, if provided. | ||
The numbers generated will always be a multiple of `step`, if provided. | ||
But...if you provide inconsistent constraints then your values | ||
will be inconsistent with them. e.g. if `total` is not a multiple | ||
of `step`. | ||
""" | ||
max_ = max_ or total | ||
factor = 0 | ||
|
||
if step < 1: | ||
assert step in [0.01, 0.5, 0.1, 0.20, 0.25, 0.50], step | ||
factor = step | ||
total = int(total / factor) | ||
step = int(total / factor) | ||
min_ = int(total / factor) | ||
max_ = int(total / factor) | ||
|
||
pieces = [] | ||
|
||
while sum(pieces) < total: | ||
remaining = total - sum(pieces) | ||
smallest = max(min_, step) | ||
if remaining < smallest: | ||
# try to add it to a random other piece | ||
for i, val in enumerate(pieces): | ||
if val + remaining <= max_: | ||
pieces[i] += remaining | ||
remaining = 0 | ||
break | ||
|
||
# just tack it on the end despite | ||
# it being too small...our | ||
# constraints must have been impossible | ||
# to fulfil | ||
if remaining: | ||
pieces.append(remaining) | ||
|
||
else: | ||
part = randint(smallest, min(remaining, max_)) | ||
round_up = part + step - (part % step) | ||
if round_up <= min(remaining, max_) and randint(0, 1): | ||
part = round_up | ||
else: | ||
part -= part % step | ||
|
||
pieces.append(part) | ||
|
||
assert sum(pieces) == total, pieces | ||
assert 0 not in pieces, pieces | ||
|
||
shuffle(pieces) | ||
if factor: | ||
pieces = [round(p * factor, 2) for p in pieces] | ||
return pieces |
Oops, something went wrong.