def _guess(self, parts): """Guess at what the URL ought to be""" messages = [] year = release = geoid = summary_level = tableid = None for part in parts: try: int(str(part)[1]) if part.upper()[0] in ['B', 'C']: tableid = part continue except (IndexError, ValueError, AttributeError): pass try: sub_geoids(part) geoid = part continue except (ValueError, TypeError): pass try: sub_summarylevel(part) summary_level = part continue except (ValueError, KeyError): pass try: if int(part) in [1, 3, 5]: release = int(part) continue except ValueError: pass try: if 2004 < int(part) < 2050: year = int(part) continue except ValueError: pass messages.append("Failed to parse '{}' ".format(part)) year = int(year or self.default_year) release = int(release or self.default_release) messages += self._test_parts( [year, release, geoid, summary_level, tableid]) if messages: raise CensusParsingException( "Failed to parse census url '{}' : {}".format( '/'.join(str(e) for e in parts), '; '.join(messages))) return year, release, geoid, summary_level, tableid
def _test_parts(self, parts, raise_exception = False): """Check if the URL is formatted properly""" year, release, geoid, summary_level, tableid = parts message = [] if year is None: message.append("No year") else: try: int(year) except: message.append("Bad year {}".format(year)) if not release: message.append("No release") else: try: assert (int(release) in [1, 3, 5]) except: message.append("Bad release {}".format(release)) if not geoid: message.append("No geoid") else: try: sub_geoids(geoid) except: message.append("Bad geoid {}".format(geoid)) if not summary_level: message.append("No summary_level") else: try: sub_summarylevel(summary_level) except: message.append("Bad summarylevel {}".format(summary_level)) if not tableid: message.append("No tableid") else: try: assert(tableid.upper()[0] in ['B','C']) except: message.append("Bad tableid {}".format(tableid)) return message
def year(self): '''Return the year''' return sub_summarylevel(self.year)
def summary_level(self): '''Return the sumary level code''' return sub_summarylevel(self._summary_level)
def summary_level(self): return sub_summarylevel(self._parts[3])