Skip to content

Commit

Permalink
clean up variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Sanchez committed Feb 23, 2022
1 parent 99d7fe4 commit 79056c6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
14 changes: 7 additions & 7 deletions pypums/surveys.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def __post_init__(self):
self._survey = _clean_survey(self.survey, self._year)
self._sample_unit = self.sample_unit[0].lower()
self._state_abbr = us.states.lookup(self.state).abbr.lower()
self._SURVEY_URL = build_acs_url(
self._survey_url = build_acs_url(
self._year, self._survey, self._sample_unit, self._state_abbr
)
self.NAME = "ACS"
self.name = "ACS"
self._data_dir = None
self._extracted = None
self._extract_folder = None
Expand Down Expand Up @@ -61,8 +61,8 @@ def download(
).exists():
if overwrite:
_download_data(
url=self._SURVEY_URL,
name=self.NAME.lower(),
url=self._survey_url,
name=self.name.lower(),
data_directory=data_directory,
extract=extract,
)
Expand All @@ -72,8 +72,8 @@ def download(
)
else:
_download_data(
url=self._SURVEY_URL,
name=self.NAME.lower(),
url=self._survey_url,
name=self.name.lower(),
data_directory=data_directory,
extract=extract,
)
Expand All @@ -86,4 +86,4 @@ def as_dataframe(self):
extracted_file = list(self._extract_folder.glob("*.csv"))[0]
return read_csv(extracted_file)
else:
return _download_as_dataframe(self._SURVEY_URL)
return _download_as_dataframe(self._survey_url)
28 changes: 14 additions & 14 deletions pypums/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def _extract_data(downloaded_file: Path, extract_dir: Path):
full_extract_path = full_extract_dir_path.joinpath(state)
if not full_extract_path.exists():
full_extract_path.mkdir()
CONTENT_FILE = ZipFile(downloaded_file)
content_file = ZipFile(downloaded_file)

for file in rich.progress.track(
CONTENT_FILE.filelist,
content_file.filelist,
description="Extracting...",
):
CONTENT_FILE.extract(file, str(full_extract_path))
content_file.extract(file, str(full_extract_path))

print(f"Files extracted successfully at [magenta]{full_extract_path}[/magenta]")

Expand Down Expand Up @@ -149,9 +149,9 @@ def _download_as_dataframe(URL: str) -> pd.DataFrame:
pd.DataFrame
DataFrame containing data from CSV
"""
_GET_DATA_REQUEST = httpx.get(URL, timeout=None)
response = httpx.get(URL, timeout=None)

with ZipFile(io.BytesIO(_GET_DATA_REQUEST.content)) as thezip:
with ZipFile(io.BytesIO(response.content)) as thezip:
csv_files = [
file for file in thezip.infolist() if file.filename.endswith(".csv")
]
Expand Down Expand Up @@ -187,19 +187,19 @@ def build_acs_url(
URL to retrieve data from.
"""

UNIT = sample_unit[0].lower()
STATE_ABBR = us.states.lookup(state).abbr.lower()
unit = sample_unit[0].lower()
state_abbr = us.states.lookup(state).abbr.lower()

if "5" in survey:
SURVEY = "5-Year"
survey = "5-Year"
elif "3" in survey:
SURVEY = "3-Year"
survey = "3-Year"
else:
SURVEY = "1-Year"
survey = "1-Year"

YEAR = _clean_year(year)
year = _clean_year(year)

SURVEY = f"{_clean_survey(SURVEY, YEAR)}"
survey = f"{_clean_survey(survey, year)}"

SURVEY_URL = f"{ACS_PUMS_URL}{YEAR}/{SURVEY}csv_{UNIT}{STATE_ABBR}.zip"
return SURVEY_URL
survey_url = f"{ACS_PUMS_URL}{year}/{survey}csv_{unit}{state_abbr}.zip"
return survey_url
8 changes: 4 additions & 4 deletions tests/test_pypums.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def test_build_acs_url():

def test_acs_class_urls():
assert (
ACS()._SURVEY_URL
ACS()._survey_url
== "https://www2.census.gov/programs-surveys/acs/data/pums/2018/1-Year/csv_pca.zip"
)
assert (
ACS(2005, "arkansas", "1-year", "household")._SURVEY_URL
ACS(2005, "arkansas", "1-year", "household")._survey_url
== "https://www2.census.gov/programs-surveys/acs/data/pums/2005/csv_har.zip"
)
assert (
ACS(2012, "Delaware", "3-year", "person")._SURVEY_URL
ACS(2012, "Delaware", "3-year", "person")._survey_url
== "https://www2.census.gov/programs-surveys/acs/data/pums/2012/3-Year/csv_pde.zip"
)
assert (
ACS(2018, "colorado", "3-year", "household")._SURVEY_URL
ACS(2018, "colorado", "3-year", "household")._survey_url
== "https://www2.census.gov/programs-surveys/acs/data/pums/2018/5-Year/csv_hco.zip"
)

Expand Down

0 comments on commit 79056c6

Please sign in to comment.