class ClassWithDict(js.JsonSaveable): x = js.Int() d = js.Dict() def __init__(self, x, d): self.x = x self.d = d
class OtherSaveable(js.JsonSaveable): foo = js.String() bar = js.Int() def __init__(self, foo, bar): self.foo = foo self.bar = bar
class ClassWithList(js.JsonSaveable): x = js.Int() lst = js.List() def __init__(self, x, lst): self.x = x self.lst = lst
class MyClass(js.JsonSaveable): x = js.Int() y = js.Float() lst = js.List() def __init__(self, x, lst): self.x = x self.lst = lst
class SimpleClass(js.JsonSaveable): a = js.Int() b = js.Float() def __init__(self, a=None, b=None): if a is not None: self.a = a if b is not None: self.b = b
class Donation(js.JsonSaveable): id = js.Int() amount = js.Float() date = js.DateTime() def __init__(self, id: int, amount: float, date: datetime=datetime.datetime.utcnow()): self.id = id self.amount = amount self.date = date def __repr__(self): return str(self.to_json_compat())
class Donor(js.JsonSaveable): """donor giving to organization""" id = js.Int() firstname = js.String() lastname = js.String() email = js.String() _donations = js.List() _donation_id = js.Int() def __init__(self, id, firstname=None, lastname=None, email=None): """args: id (int): identification for donor. Will try to force to int when initiated or raise error. firstname (str, optional): string representing given name lastnamt (str, optional): string representing surname _donations (list): contains Donation objects from donor _donation_id (int): tracks indentification for donations""" try: self.id = int(id) except ValueError: raise ValueError('id input should be interpreted as integer') self.firstname = firstname self.lastname = lastname self.email = email self._donations = [] self._donation_id = 1 def donation_total(self): """returns the total amount the donor has donated""" if self.donations: print(self.donations) return sum([i.amount for i in self.donations]) else: return 0 def add_donation(self, amount, date=datetime.datetime.utcnow()): """adds donation for user""" self._donations.append(Donation(amount=amount, date=date, id=self._donation_id)) self._donation_id += 1 def donation_count(self): """returns count of donations""" return len(self._donations) @property def donations(self): """returns donations""" return self._donations @property def fullname(self): """returns combine first and last name""" return " ".join([self.firstname, self.lastname]) def summarize_donor(self): """provides summary tuple of donor""" return (self.id, self.fullname, self.donation_total(), self.donation_count(), self.donation_total()/self.donation_count()) def __repr__(self): return str(self.to_json_compat())
class NoInit(js.JsonSaveable): x = js.Int() y = js.String()
class Donor(js.JsonSaveable): _name = js.String() _list_donations = js.List() _donation_count = js.Int() _amount = js.Float() def __init__(self, name, list_donations): self._name = name self._list_donations = list_donations self._donation_count = len(list_donations) self._amount = sum(list_donations) @property def name(self): return self._name @property def amount(self): return self._amount @amount.setter def amount(self, amount): self._amount = amount def add(self, donation_amount): self._amount += donation_amount self._donation_count += 1 self._list_donations.append(donation_amount) @property def donation_count(self): return self._donation_count @property def average(self): return self._amount / self._donation_count def get_letter_text(self, name, amount): msg = [] msg.append('Dear {},'.format(name)) msg.append( '\n\n\tThank you for your very kind donation of ${:.2f}.'.format( amount)) msg.append('\n\n\tIt will be put to very good use.') msg.append('\n\n\t\t\t\tSincerely,') msg.append('\n\t\t\t\t-The Team\n') return "".join(msg) def challenge(self, factor, min_donation=None, max_donation=None): """Get the sum of projection donation from a donor""" new_list = [] if min_donation is not None and max_donation is not None: #filter minimum & maximum new_list = list( filter(lambda donation: donation >= min_donation, self._list_donations)) new_list = list( filter(lambda donation_amount: donation_amount <= max_donation, new_list)) new_list = list(map(lambda x: x * factor, new_list)) elif min_donation is None and max_donation is not None: #filter maximum new_list = list( filter(lambda donation: donation <= max_donation, self._list_donations)) new_list = list(map(lambda x: x * factor, new_list)) elif min_donation is not None and max_donation is None: # filter minimum new_list = list( filter(lambda donation: donation >= min_donation, self._list_donations)) new_list = list(map(lambda x: x * factor, new_list)) else: # no minimum and maximum new_list = list(map(lambda x: x * factor, self._list_donations)) return sum(new_list) def __lt__(self, other): return self._amount < other._amount def __gt__(self, other): return self._amount > other._amount def __eq__(self, other): return self._amount == other._amount