Skip to content

Instantly share code, notes, and snippets.

@rubik
Created March 18, 2011 15:10
Show Gist options
  • Save rubik/876227 to your computer and use it in GitHub Desktop.
Save rubik/876227 to your computer and use it in GitHub Desktop.
from __future__ import division
import sys
class Person(object):
attrs = ('name', 'surname', 'birth', 'birth_place', 'gender', 'marital_status', 'address', 'job')
def __init__(self, name, surname, birth, birth_place, gender, marital_status, address, job):
kw = locals()
for attr in self.attrs:
setattr(self, attr, kw[attr])
def __str__(self):
return '<{self.name} {self.surname}>'.format(self=self)
def __eq__(self, other):
return all(self.a == other.a for a in self.attrs)
class RegistryOffice(object):
def __init__(self, *args):
self.people = set(args)
def __len__(self):
return len(self.people)
def _calc(self, n):
return n * 100 / len(self)
def add(self, p):
self.persone.add(p)
def print_(self):
return sys.stdout.write('\n'.join(str(p) for p in self.people) + '\n')
def search(self, name, surname):
return [p for p in self.people if p.name == name and p.surname == surname]
def mstats(self):
free = conjugates = widowers = 0
for p in self.people:
if p.marital_status == 'free':
free += 1
elif p.marital_status == 'conjugate':
conjugates += 1
elif p.marital_status == 'widower':
widowers += 1
else:
raise ValueError('Unknown marital status')
return sys.stdout.write('Free: {0}\nConjugates: {1}\nWidowers: {2}\n'.format(self._calc(free),
self._calc(conjugates),
self._calc(widowers)))
def gstats(self):
males = females = 0
for p in self.people:
if p.gender == 'male':
males += 1
elif p.gender == 'female':
females += 1
return sys.stdout.write('Males {0}\nFemales {1}'.format(self._calc(males), self._calc(females)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment