class NameGenerator(object): """Person name generator Generates random full names. Names can recur. """ def __init__(self, seed): self.random = PortableRandom(seed) self.first_names = self._readLines('first_names.txt') self.last_names = self._readLines('last_names.txt') def _readLines(self, filename): """Read in lines from file Filename is relative to the module. Returned lines are stripped. """ fullpath = os.path.join(os.path.dirname(__file__), filename) lines = file(fullpath).readlines() return [line.strip() for line in lines] def generate(self): """Generates name information, returns tuple of first name, last name, full name """ first_name = self.random.choice(self.first_names) last_name = self.random.choice(self.last_names) full_name = "%s %s" % (first_name, last_name) return first_name, last_name, full_name
class SamplePersonalEvents(object): """Sample data plugin class that generates personal random events.""" implements(ISampleDataPlugin) name = 'personal_events' dependencies = ('students', 'teachers', 'terms') probability = 2 # probability of having an event on any day def _readLines(self, filename): """Read in lines from file Filename is relative to the module. Returned lines are stripped. """ fullpath = os.path.join(os.path.dirname(__file__), filename) lines = file(fullpath).readlines() return [line.strip() for line in lines] def generate(self, app, seed=None): self.random = PortableRandom(seed) event_titles = self._readLines('event_titles.txt') person_ids = [person for person in app['persons'].keys() if person.startswith('student') or person.startswith('teacher')] dates = [] for term in ITermContainer(app).values(): dates.append(term.first) dates.append(term.last) first = min(dates) last = max(dates) days = DateRange(first, last) for person_id in person_ids: person = app['persons'][person_id] calendar = ISchoolToolCalendar(person) for day in days: if self.random.randrange(0, 100) < self.probability: event_title = self.random.choice(event_titles) time_hour = self.random.randint(6, 23) time_min = self.random.choice((0, 30)) event_time = datetime.datetime(day.year, day.month, day.day, time_hour, time_min, tzinfo=utc) event_duration = datetime.timedelta( minutes=self.random.randint(1, 12)*30) event = CalendarEvent(event_time, event_duration, event_title) calendar.addEvent(event)
class ChoiceGenerator(object): def __init__(self, seed, choices): self.random = PortableRandom(seed) self.choices = choices def generate(self): return self.random.choice(self.choices)