Exemple #1
0
    def __init__(self, cache_dir):
        """Constructor.
    Args:
        cache_dir: cache directory
    Returns:
        an instance.
    """
        # checks exists and is writable, creates if necessary
        self.cache_dir = utils.init_cache_dir(cache_dir)

        self.offline_mode = not utils.check_online(
            "https://www.scotlandscensus.gov.uk/ods-web/data-warehouse.html")
        if self.offline_mode:
            print(
                "Unable to contact %s, operating in offline mode - pre-cached data only"
                % self.URL)

        # download the lookup if not present
        lookup_file = self.cache_dir / "sc_lookup.csv"
        if not os.path.isfile(str(lookup_file)):
            lookup_url = "https://www2.gov.scot/Resource/0046/00462936.csv"
            response = requests.get(lookup_url)
            response.raise_for_status()
            with open(str(lookup_file), 'wb') as fd:
                for chunk in response.iter_content(chunk_size=1024):
                    fd.write(chunk)

        self.area_lookup = pd.read_csv(str(self.cache_dir / "sc_lookup.csv"))

        # TODO use a map (just in case col order changes)
        self.area_lookup.columns = ["OA11", "LSOA11", "MSOA11", "LAD"]
Exemple #2
0
    def __init__(self, cache_dir, verbose=False):
        """Constructor.
    Args:
        cache_dir: cache directory
    Returns:
        an instance.
    """
        self.cache_dir = utils.init_cache_dir(cache_dir)
        self.verbose = verbose
        self.offline_mode = True

        # how best to deal with site unavailable...
        self.offline_mode = not utils.check_online(self.URL, Nomisweb.Timeout)
        if self.offline_mode:
            print(
                "Unable to contact %s, operating in offline mode - pre-cached data only"
                % self.URL)

        self.key = _get_api_key(self.cache_dir)
        if not self.offline_mode and self.key is None:
            raise RuntimeError(
                "No API key found. Whilst downloads still work, they may be truncated,\n"
                "causing potentially unforseen problems in any modelling/analysis.\n"
                "Set the key value in the environment variable NOMIS_API_KEY.\n"
                "Register at www.nomisweb.co.uk to obtain a key")

        if self.verbose: print("Cache directory: ", self.cache_dir)

        # static member
        Nomisweb.cached_lad_codes = self.__cache_lad_codes()
Exemple #3
0
    def __init__(self, cache_dir):
        """Constructor.
    Args:
        cache_dir: cache directory
    Returns:
        an instance.
    """
        # checks exists and is writable, creates if necessary
        self.cache_dir = utils.init_cache_dir(cache_dir)

        self.offline_mode = not utils.check_online(self.URL)
        if self.offline_mode:
            print(
                "Unable to contact %s, operating in offline mode - pre-cached data only"
                % self.URL)

        # download the lookup if not present
        lookup_file = self.cache_dir / "ni_lookup.csv"
        if not os.path.isfile(str(lookup_file)):
            z = zipfile.ZipFile(
                str(self.__source_to_zip(NISRA.data_sources[2])))
            pd.read_csv(z.open("All_Geographies_Code_Files/NI_HIERARCHY.csv")) \
                .drop(["NUTS3", "HSCT", "ELB", "COUNTRY"], axis=1) \
                .to_csv(str(lookup_file), index=False)

        # load the area lookup
        self.area_lookup = pd.read_csv(str(lookup_file))