コード例 #1
0
def main():
    # Create a list of dcids for Santa Clara and Montgomery County.
    sc, mc = 'geoId/06085', 'geoId/24031'
    dcids = [sc, mc]

    # Get all CensusTracts in these two counties.
    print('Get Census Tracts')
    tracts = dc.get_places_in(dcids, 'CensusTract')
    if sc in tracts:
        print('> 10 CensusTracts in Santa Clara County')
        for dcid in tracts[sc][:10]:
            print('  - {}'.format(dcid))
    if mc in tracts:
        print('> 10 CensusTracts in Montgomery County')
        for dcid in tracts[mc][:10]:
            print('  - {}'.format(dcid))

    # Get place stats.
    print('Get place stats -- all')
    stats = dc.get_stats(['geoId/05', 'geoId/06', 'dc/madDcid'],
                         'dc/0hyp6tkn18vcb',
                         obs_dates='all')
    print(stats)

    print('Get place stats -- latest')
    stats = dc.get_stats(['geoId/05', 'geoId/06', 'dc/madDcid'],
                         'dc/0hyp6tkn18vcb')
    print(stats)

    print('Get place stats -- 2014')
    stats = dc.get_stats(['geoId/05', 'geoId/06', 'dc/madDcid'],
                         'dc/0hyp6tkn18vcb',
                         obs_dates=['2014'])
    print(stats)

    print('Get place stats -- 2014 badly formatted')
    stats = dc.get_stats(['geoId/05', 'geoId/06', 'dc/madDcid'],
                         'dc/0hyp6tkn18vcb',
                         obs_dates='2014')
    print(stats)

    print('Get place stats -- 2015-2016')
    stats = dc.get_stats(['geoId/05', 'geoId/06', 'dc/madDcid'],
                         'dc/0hyp6tkn18vcb',
                         obs_dates=['2015', '2016'])
    print(stats)
コード例 #2
0
    def test_bad_dcids(self, urlopen):
        """ Calling get_stats with dcids that do not exist returns empty
      results.
    """
        # Call get_stats with one dcid that does not exist
        bad_dcids_1 = dc.get_stats(['geoId/05', 'dc/MadDcid'],
                                   'dc/0hyp6tkn18vcb')
        self.assertDictEqual(
            bad_dcids_1,
            {'geoId/05': {
                'data': {
                    '2018': 18003
                },
                'place_name': 'Arkansas'
            }})

        # Call get_stats when both dcids do not exist
        bad_dcids_2 = dc.get_stats(['dc/MadDcid', 'dc/MadderDcid'],
                                   'dc/0hyp6tkn18vcb')
        self.assertDictEqual({}, bad_dcids_2)
コード例 #3
0
    def test_batch_request(self, mock_urlopen):
        """ Make multiple calls to REST API when number of geos exceeds the batch size. """
        save_batch_size = dc.utils._QUERY_BATCH_SIZE
        dc.utils._QUERY_BATCH_SIZE = 1

        self.assertEqual(0, mock_urlopen.call_count)
        stats = dc.get_stats(['geoId/05'], 'dc/0hyp6tkn18vcb', 'latest')
        self.assertDictEqual(stats, {
            'geoId/05': {
                'data': {
                    '2018': 18003
                },
                'place_name': 'Arkansas'
            },
        })
        self.assertEqual(1, mock_urlopen.call_count)

        stats = dc.get_stats(['geoId/05', 'geoId/06'], 'dc/0hyp6tkn18vcb',
                             'latest')
        self.assertDictEqual(
            stats, {
                'geoId/05': {
                    'data': {
                        '2018': 18003
                    },
                    'place_name': 'Arkansas'
                },
                'geoId/06': {
                    'data': {
                        '2018': 366331
                    },
                    'place_name': 'California'
                }
            })
        self.assertEqual(3, mock_urlopen.call_count)

        dc.utils._QUERY_BATCH_SIZE = save_batch_size
コード例 #4
0
    def test_opt_args(self, urlopen):
        """ Calling get_stats with mmethod, unit, and obs period returns specific data.
    """
        # Set the API key
        dc.set_api_key('TEST-API-KEY')

        # Call get_stats with all optional args
        stats = dc.get_stats(['geoId/48'], 'dc/0hyp6tkn18vcb', 'latest', 'MM1',
                             'Inch', 'P1Y')
        self.assertDictEqual(
            stats, {'geoId/48': {
                'data': {
                    '2016': 1
                },
                'place_name': 'Texas'
            }})

        # Call get_stats with mmethod specified
        stats = dc.get_stats(['geoId/48'], 'dc/0hyp6tkn18vcb', 'latest', 'MM1')
        self.assertDictEqual(
            stats, {'geoId/48': {
                'data': {
                    '2016': 2
                },
                'place_name': 'Texas'
            }})

        # Call get_stats without optional args
        stats = dc.get_stats(['geoId/48'], 'dc/0hyp6tkn18vcb', 'latest')
        self.assertDictEqual(
            stats, {'geoId/48': {
                'data': {
                    '2016': 3
                },
                'place_name': 'Texas'
            }})
コード例 #5
0
 def test_no_data(self, urlopen):
     """ Calling get_stats with for None data. """
     # Call get_stats with no dcids.
     result = dc.get_stats(['geoId/00'], 'dc/0hyp6tkn18vcb')
     self.assertDictEqual({}, result)
コード例 #6
0
 def test_no_dcids(self, urlopen):
     """ Calling get_stats with no dcids returns empty results. """
     # Call get_stats with no dcids.
     no_dcids = dc.get_stats([], 'dc/0hyp6tkn18vcb')
     self.assertDictEqual({}, no_dcids)
コード例 #7
0
    def test_multiple_dcids(self, urlopen):
        """ Calling get_stats with proper dcids returns valid results. """
        # Call get_stats
        stats = dc.get_stats(['geoId/05', 'geoId/06'], 'dc/0hyp6tkn18vcb',
                             'all')
        self.assertDictEqual(
            stats, {
                'geoId/05': {
                    'data': {
                        '2011': 18136,
                        '2012': 17279,
                        '2013': 17459,
                        '2014': 16966,
                        '2015': 17173,
                        '2016': 17041,
                        '2017': 17783,
                        '2018': 18003
                    },
                    'place_name': 'Arkansas'
                },
                'geoId/06': {
                    'data': {
                        '2011': 316667,
                        '2012': 324116,
                        '2013': 331853,
                        '2014': 342818,
                        '2015': 348979,
                        '2016': 354806,
                        '2017': 360645,
                        '2018': 366331
                    },
                    'place_name': 'California'
                }
            })

        # Call get_stats for latest obs
        stats = dc.get_stats(['geoId/05', 'geoId/06'], 'dc/0hyp6tkn18vcb',
                             'latest')
        self.assertDictEqual(
            stats, {
                'geoId/05': {
                    'data': {
                        '2018': 18003
                    },
                    'place_name': 'Arkansas'
                },
                'geoId/06': {
                    'data': {
                        '2018': 366331
                    },
                    'place_name': 'California'
                }
            })

        # Call get_stats for specific obs
        stats = dc.get_stats(['geoId/05', 'geoId/06'], 'dc/0hyp6tkn18vcb',
                             ['2013', '2018'])
        self.assertDictEqual(
            stats, {
                'geoId/05': {
                    'data': {
                        '2013': 17459,
                        '2018': 18003
                    },
                    'place_name': 'Arkansas'
                },
                'geoId/06': {
                    'data': {
                        '2013': 331853,
                        '2018': 366331
                    },
                    'place_name': 'California'
                }
            })

        # Call get_stats -- dates must be in interable
        stats = dc.get_stats(['geoId/05', 'geoId/06'], 'dc/0hyp6tkn18vcb',
                             '2018')
        self.assertDictEqual(
            stats, {
                'geoId/05': {
                    'data': {},
                    'place_name': 'Arkansas'
                },
                'geoId/06': {
                    'data': {},
                    'place_name': 'California'
                }
            })