def test_hydrate_with_non_lists(self): data = ("US",) result = LocationDatabase.hydrate(data, StreetSuffixEntity) self.assertIsInstance(result, StreetSuffixEntity) with self.assertRaises(TypeError): LocationDatabase.hydrate("foo", StreetSuffixEntity)
def test_hydrate_with_non_lists(self): data = ('US', ) result = LocationDatabase.hydrate(data, StreetSuffixEntity) self.assertIsInstance(result, StreetSuffixEntity) with self.assertRaises(TypeError): LocationDatabase.hydrate('foo', StreetSuffixEntity)
def get_postal_code_data(cls, data): """ If this looks like a postal code, we try to get its info :param data: a possibly detected zip code :type data: str :return: possible locations for this zip code :rtype: list """ plus_postal_code = '-' in data if plus_postal_code: prefix = data.split('-')[0] database = LocationDatabase.get_database() cursor = database.cursor() try: rows = cursor.execute( 'SELECT * FROM city WHERE postal_code = ?', (data,) ).fetchall() entities = LocationDatabase.hydrate(rows, CityEntity) except sqlite3.Error: database.close() return None if plus_postal_code: try: rows = cursor.execute( 'SELECT * FROM city WHERE postal_code = ?', (prefix,) ).fetchall() prefix_entities = LocationDatabase.hydrate(rows, CityEntity) entities.extend(prefix_entities) except sqlite3.Error: database.close() return None if not entities: database.close() return None entities = LocationDatabase.substitute_country_data(entities, cursor) entities = [vars(x) for x in entities] database.close() return entities
def get_street_suffix(cls, data_set): """ Looks at our data for a street suffix and returns the entity :param data_set: the list of parts of address data :type data_set: list :return: the street suffix if one was found :rtype: list """ sql_items = [] for _ in data_set: sql_items.append('suffix_name = ?') database = LocationDatabase.get_database() cursor = database.cursor() try: row = cursor.execute( 'SELECT * FROM street_suffix WHERE ' + " or ".join(sql_items), tuple(data_set) ).fetchone() database.close() except sqlite3.Error: database.close() return None if row is not None: return LocationDatabase.hydrate(row, StreetSuffixEntity)
def get_street_suffix(cls, data_set): """ Looks at our data for a street suffix and returns the entity :param data_set: the list of parts of address data :type data_set: list :return: the street suffix if one was found :rtype: list """ sql_items = [] for _ in data_set: sql_items.append('suffix_name = ?') database = LocationDatabase.get_database() cursor = database.cursor() try: row = cursor.execute( 'SELECT * FROM street_suffix WHERE ' + " or ".join(sql_items), tuple(data_set)).fetchone() database.close() except sqlite3.Error: database.close() return None if row is not None: return LocationDatabase.hydrate(row, StreetSuffixEntity)
def find_matching_landmarks(cls, data): """ Looks in the database for landmarks matching datastring :param data: string we want to check against landmarks :type data: str :return: list of landmarks :rtype: list """ database = LocationDatabase.get_database() cursor = database.cursor() try: rows = cursor.execute( 'SELECT * FROM landmark WHERE resource like ?', (data + '%',) ).fetchall() entities = LocationDatabase.hydrate(rows, LandmarkEntity) entities = \ LocationDatabase.substitute_country_data(entities, cursor) entities = [vars(x) for x in entities] database.close() except sqlite3.Error: database.close() return return entities
def get_postal_code_data(cls, data): """If this looks like a postal code, we try to get its info""" plus_postal_code = '-' in data if plus_postal_code: prefix = data.split('-')[0] database = LocationDatabase.get_database() cursor = database.cursor() try: rows = cursor.execute('SELECT * FROM city WHERE postal_code = ?', (data, )).fetchall() entities = LocationDatabase.hydrate(rows, CityEntity) except sqlite3.Error: database.close() return None if plus_postal_code: try: rows = cursor.execute( 'SELECT * FROM city WHERE postal_code = ?', (prefix, )).fetchall() prefix_entities = LocationDatabase.hydrate(rows, CityEntity) entities.extend(prefix_entities) except sqlite3.Error: database.close() return None if not entities: database.close() return None entities = LocationDatabase.substitute_country_data(entities, cursor) entities = [vars(x) for x in entities] database.close() return entities
def find_matching_landmarks(cls, data): """Looks in the database for landmarks matching datastring""" database = LocationDatabase.get_database() cursor = database.cursor() try: rows = cursor.execute( 'SELECT * FROM landmark WHERE resource like ?', (data + '%', )).fetchall() entities = LocationDatabase.hydrate(rows, LandmarkEntity) entities = \ LocationDatabase.substitute_country_data(entities, cursor) entities = [vars(x) for x in entities] database.close() except sqlite3.Error: database.close() return return entities