Paste Arfk5

Submitted on 1 June 2016 at 12:33 UTC
2637 views

Section 1 (python)

from collections import namedtuple

def literalstruct(name, **kv):
    o = type(name, (), {})()
    o.__dict__.update(kv)
    return o

Animal = namedtuple('Animal',
    [ 'species', 'name', 'call' ])

Facility = namedtuple('Facility', [
    'director',
    'phone',
    'animals'
])

def bark(): return "barks and jumps"

def meow(): return "meow"

City = literalstruct('City',
        zoo = Facility(
            'Vasiliy Pupkin',
            '111-22-33', [
                Animal('dog', 'Spotty', bark),
                Animal('dog', 'Fido', bark)
            ]
        ),
        circus = Facility(
            'John Jackson',
            '111-37-13', [
                Animal('cat', 'Mittens', meow)
            ]
        )
)

def everyone(group):
    for a in group:
        print("%s: %s" % (a.name, a.call()))

everyone(City.zoo.animals)
everyone(City.circus.animals)

Raw · Download

Pastee