Skip to content

Instantly share code, notes, and snippets.

@metaperl
Created Feb 23, 2022
Embed
What would you like to do?
import itertools
from param import Parameterized, Number, List, ClassSelector
marketer_id = itertools.count(1)
class Marketer(Parameterized):
id = Number()
front_line_target = Number(5)
def __init__(self, **params):
super().__init__(**params)
self.id = next(marketer_id)
@property
def sponsoring_goals_met(self):
return len(self.front_line) < self.front_line_target
def recruit(self):
if self.sponsoring_goals_met:
return list()
new_marketer = Marketer()
self.front_line.append(new_marketer)
return new_marketer
def status(self):
print(f"Marketer {self.id} has sponsored {len(self.front_line)} marketers.")
return self.front_line
Marketer.param.add_parameter('sponsor', ClassSelector(class_=Marketer))
Marketer.param.add_parameter('front_line', List([], item_type=Marketer))
class Network(Parameterized):
marketers = List([], item_type=Marketer)
def recruit(self):
for marketer in self.marketers:
new_marketers = marketer.recruit()
self.marketers.extend(new_marketers)
def status(self):
total = 0
for marketer in self.marketers:
total += len(marketer.status())
print(f"Total marketers {total}.")
def main(months=5):
network = Network()
network.marketers.append(Marketer())
for month in range(months):
network.recruit()
print(f"Month {month} recruiting done. Status:")
network.status()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment