def test_should_add_getters_to_the_given_class_for_the_given_var_names(self): class MyClass(object): def __init__(self, foo, bar): self.foo = foo self.bar = bar with_getters_for(MyClass, 'foo', 'bar') foo = 'foo_var' bar = 'bar_var' clazz = MyClass(foo, bar) self.assertEquals(foo, clazz.get_foo(), "Should have a getter for foo") self.assertEquals(bar, clazz.get_bar(), "Should have a getter for bar")
""" Class defining the values in the users shelf """ def __init__(self, hashed_password, salt, stickies_json): """ Constructor for UserData Args: hashed_password the password after being hashed. salt the salt used to hash the password. stickies_json the JSON string for their stickies. """ assign_injectables(self, locals()) def update_stickies(self, new_stickies_json): """ Returns a new UserData with the updated stickies """ return UserData(self.hashed_password, self.salt, new_stickies_json) with_getters_for(UserData, 'hashed_password', 'salt', 'stickies_json') def confirmed_password_valid(password, confirmation): """ Tell if a password was confirmed properly Args: password the password to check. confirmation the confirmation of the password. Returns: True if the password and confirmation are the same (no typos) """ return password == confirmation def generate_salt(length):
return [PageVisit(visit_row[1], visit_row[2], id=visit_row[0]) for \ visit_row in self.database] def close(self): self.database.close() """ Immutable data objects representing rows in the databases. They differ from ORMs in that they do not contain pointers for relational fields, they are just thin wrappers around the values in the table. """ class User(object): def __init__(self, username, hashed_password, salt, id=None): assign_injectables(self, locals()) with_getters_for(User, 'id', 'username', 'hashed_password', 'salt') def time_comparisons(visits): hour_ago = datetime.timedelta(hours=1) day_ago = datetime.timedelta(days=1) week_ago = datetime.timedelta(weeks=1) approximately_month_ago = datetime.timedelta(days=30) year_ago = datetime.timedelta(days=365) def visit_was_since_delta(delta, visit): visit_time = visit.get_time_visited() visit_date = datetime.date.fromtimestamp(visit_time) now = datetime.date.fromtimestamp(time.time()) earliest = now - delta return earliest < visit_date descriptions = ['sinceLastHour', 'sinceLastDay', 'sinceLastWeek', 'sinceLastMonth', 'sinceLastYear']
class DatabaseObject(object): def __init__(self): self.get_id = partial(get_id_if_not_none, self) super(DatabaseObject, self).__init__() class User(DatabaseObject): def __init__(self, username, hashed_password, salt, id=None): assign_injectables(self, locals()) super(User, self).__init__() @classmethod def from_row(clazz, row): (id, username, hashed_password, salt) = row return clazz(username, hashed_password, salt, id=id) with_getters_for(User, 'username', 'hashed_password', 'salt') class Player(DatabaseObject): def __init__(self, created_by_user, currently_in_room, id=None): assign_injectables(self, locals()) super(Player, self).__init__() @classmethod def from_row(clazz, row): (id, created_by_user, currently_in_room) = row return clazz(created_by_user, currently_in_room, id=id) with_getters_for(Player, 'created_by_user', 'currently_in_room') class GameEntity(DatabaseObject): """ Abstract base class for objects in the game with names and descriptions