Tod Naturlich/script/family generator.py

From All The Fallen Stories
Revision as of 03:03, 19 October 2017 by Fivesteaks (talk | contribs) (Formatting to make it readable on the page, not just in source.)
Jump to navigation Jump to search
#!/usr/bin/env python
'''Random family generator

Needs the packages "requests" and "termcolor" to work.'''

import random
import requests
from termcolor import colored, cprint

GENDERS = ['male', 'female']
BIRTHDAYS = ['January', 'February', 'March', 'April', 'May', 'June',' July', 'August', 'September', 'October', 'November', 'December']
TITLES = ['Parent', 'Sibling']


def generate_age(min_age, max_age, use_gaussian):
    age = 0
    if use_gaussian:
        mean = (max_age + min_age) / 2
        dev = (max_age - min_age) / 4
        age = int(random.gauss(mean, dev))
    else:
        age = random.randint(min_age, max_age)
    return age


def get_school_grade(age):
    if age < 4 or age > 17:
        return None
    if age <= 5:
        return 'Kindergarten'
    if age <= 6:
        return "1st Grade"
    if age <= 7:
        return "2nd Grade"
    if age <= 8:
        return "3rd Grade"
    if age <= 13:
        return "{}th Grade".format(int(age-5))
    if age <= 14:
        return "High School Freshman"
    if age <= 15:
        return "High School Sophomore"
    if age <= 16:
        return "High School Junior"
    return "High School Senior"


def get_names(count=10, gender=None, surname=False, frequency='all'):
    name_params = {
        'count': count,
        'with_surname': 'true' if surname else 'false',
        'frequency': frequency
    }
    if gender and gender in GENDERS:
        name_params['type'] = gender

    response = requests.get('ht' + 'tp://namey.muffinlabs.com/name.json',params=name_params)
    return response.json()


class Character:
    def __init__(self, gender=None, age=None, age_range=None, age_gaussian=False, birthday=None, title=None, alive=True):
        self.alive = alive

        if gender and gender in GENDERS:
            self.gender = gender
        else:
            self.gender = random.choice(GENDERS)

        if age:
            self.age = int(age)
        else:
            if age_range is None:
                age_range = [4, 17]
            self.age = generate_age(age_range[0], age_range[1], age_gaussian)

        if birthday and birthday in BIRTHDAYS:
            self.birthday = birthday
        else:
            self.birthday = random.choice(BIRTHDAYS)

        if title and title in TITLES:
            self.title = title
        else:
            self.title = TITLES[1]

        if title is None or title not in TITLES:
            title = TITLES[1]

        self.grade = get_school_grade(self.age)

    def print_title(self):
        if self.gender == GENDERS[0]:
            if self.title == TITLES[0]:
                return 'Father'
            else:
                return 'Son'
        else:
            if self.title == TITLES[0]:
                return 'Mother'
            else:
                return 'Daughter'

    def print(self, with_names=False):
        names = None
        if with_names:
            print(colored('Getting name list', 'red', attrs=['bold']), end='\r')
            args = {
                'gender': self.gender,
                'surname': True if self.title == TITLES[0] else False,
            }
            names = get_names(**args)
            print(' ' * 80, end='\r')
        description = '{} ({}, {})'.format(self.print_title(), self.age, self.birthday)
        if self.grade:
            description += ' - {}'.format(self.grade)
        if not self.alive:
            description += ' (Deceased)'
        cprint(description, 'blue', attrs=['bold'])
        if with_names:
            cprint(', '.join(names), 'green')


class Family:
    def __init__(self):
        self.no_children = int(random.gauss(3,1))

        self.mom_alive = random.randrange(100) < 90
        self.dad_alive = random.randrange(100) < 95

        self.first_pregnancy_age = int(random.gauss(17, 3))

        self.children = [Character() for i in range(self.no_children)]
        self.children.sort(key=lambda child: child.age, reverse=True)

        self.mom_age = self.first_pregnancy_age + self.children[0].age
        self.dad_age = self.mom_age + int(random.gauss(6, 10))

        self.mom = Character(age=self.mom_age, gender=GENDERS[1], title=TITLES[0], alive=self.mom_alive)
        self.dad = Character(age=self.dad_age, gender=GENDERS[0], title=TITLES[0], alive=self.dad_alive)

    def print(self, with_names=False):
        self.dad.print(with_names)
        self.mom.print(with_names)
        for child in self.children:
            child.print(with_names)



if __name__ == '__main__':
    family = Family()
    family.print(True)