Last active
May 31, 2016 15:07
-
-
Save my8bird/f640a2d4cb008cf36800404be7422eee to your computer and use it in GitHub Desktop.
Interview Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from os.path import join as pj | |
import functools.partial | |
import logging | |
logger = logging.getLogger('my.module') | |
def getAlerts(ids, default = []): | |
""" | |
Downloads the alerts with `ids`. If an error is generated then the default is returned | |
@type ids: `list` of `number` | |
@param ids: List of `Alert` ids to find in the database | |
""" | |
db = getDB() | |
try: | |
alerts = db.find(Alert.id.in(ids)) | |
except Exception as ex: | |
print ex | |
return default | |
if len(alerts) != len(ids): | |
logger.warn('Not all alerts were found') | |
return alerts | |
class Item(object): | |
""" | |
Holds details for an item. | |
@type data: object | |
@attr data: Object holding the data blob | |
""" | |
def __init__(self, data): | |
self.data = data | |
def getData(self): | |
return self.data | |
def setData(self, newData): | |
self.data = newData | |
class Collection(object): | |
def __init__(self): | |
self._items = [] | |
def add(self, item, logger = None): | |
if logger: | |
logger.info('Adding: ', item) | |
self._items.append(item) | |
def remove(self, item): | |
self._items.remove(item) | |
def loop(self): | |
return iter(self._items) | |
class AlertCollection(Collection): | |
def __init__(self): | |
Collection.__init__(self) | |
def loop(self): | |
return self._items | |
if __name__ == '__main__': | |
# Get Alerts from database | |
alerts = getAlerts([1, 2, 3, 4]) | |
# Add them to a collection | |
collection = AlertCollection() | |
adder = functools.partial(collection.add, logger = logger) | |
[adder(alert) for alert in alerts] | |
# Print out the alerts in the collection | |
print (alert for alert in collection.loop()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment