def test_appurl(self):
        from publicdata.census.util import sub_geoids, sub_summarylevel

        from rowgenerators import parse_app_url
        from publicdata.census.exceptions import CensusParsingException

        #self.assertEqual(245,list(parse_app_url('census://2016/5/RI/140/B17001').generator))

        #self.assertEqual(245, list(parse_app_url('census://RI/140/B17001').generator))

        with self.assertRaises(ValueError):
            sub_geoids('foobar')

        u = parse_app_url('census://RI/140/B17001')
        self.assertEqual('B17001', u.tableid)
        self.assertEqual('04000US44', u.geoid)

        u = parse_app_url('census://B17001/140/RI')
        self.assertEqual('B17001', u.tableid)
        self.assertEqual('04000US44', u.geoid)

        u = parse_app_url('census://140/RI/B17001')
        self.assertEqual('B17001', u.tableid)
        self.assertEqual('04000US44', u.geoid)

        with self.assertRaises(CensusParsingException):
            parse_app_url('census://B17001/Frop/140')

        with self.assertRaises(CensusParsingException):
            parse_app_url('census://BINGO/RI/140')
Beispiel #2
0
    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
Beispiel #3
0
    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
Beispiel #4
0
 def state(self):
     '''Return the cstate'''
     return sub_geoids(self.state)
Beispiel #5
0
 def geoid(self):
     '''Return the containment Geoid'''
     return sub_geoids(self._geoid)
Beispiel #6
0
 def geoid(self):
     return sub_geoids(self._parts[2])
Beispiel #7
0
 def tableid(self):
     return sub_geoids(self._parts[4])
Beispiel #8
0
    def test_sub_geoid(self):

        self.assertEqual(sub_geoids('ca'), '04000US06')
        self.assertEqual(sub_geoids('US'), '01000US')
        self.assertEqual(sub_geoids('x'), 'x')