Skip to content

Commit

Permalink
Cyclic imports (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
kavigupta authored Oct 27, 2024
1 parent 85e101f commit 2128ec4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
1 change: 0 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ disable=raw-checker-failed,
## incompatible with black
wrong-import-position,
## these need to be fixed at some point. here to make the build pass
cyclic-import,import-outside-toplevel,
fixme,

# Enable the message, report, category or checker with the given id(s). You can
Expand Down
5 changes: 2 additions & 3 deletions urbanstats/data/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from urbanstats.data.gpw import load_full_ghs
from urbanstats.data.population_overlays import direct_population_overlay, relevant_regions
from urbanstats.geometry.shapefiles.shapefile import Shapefile
from urbanstats.geometry.shapefiles.shapefiles.urban_centers import URBAN_CENTERS


class MapDataset:
Expand Down Expand Up @@ -429,8 +431,6 @@ def to_basic_geopandas_frame(map_shape, circles):


def attach_urban_centers_to_frame(frame):
from urbanstats.geometry.shapefiles.shapefiles.urban_centers import URBAN_CENTERS

urban_center_shapefile = URBAN_CENTERS.load_file()
urban_center_shapefile.index = urban_center_shapefile.longname
overlays = direct_population_overlay(frame, urban_center_shapefile)
Expand Down Expand Up @@ -756,7 +756,6 @@ def produce_image(population):


def circle_shapefile_object(country_shapefile, population, just_usa):
from urbanstats.geometry.shapefiles.shapefile import Shapefile

name = named_populations[population] + " Person Circle"
if just_usa:
Expand Down
14 changes: 12 additions & 2 deletions urbanstats/geometry/shapefiles/shapefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import geopandas as gpd
import pandas as pd

from urbanstats.special_cases.deduplicate_longnames import drop_duplicate


@attr.s
class Shapefile:
Expand All @@ -22,6 +20,18 @@ class Shapefile:
tolerate_no_state = attr.ib(default=False)

def load_file(self):
"""
Load the shapefile and apply the filters and extractors.
This has a circular dependency with the deduplicate_longnames module, which
needs to load other shapefiles to figure out how to disambiguate duplicated
longnames.
This should be the only circular dependency related to shapefiles.
"""
# pylint: disable=import-outside-toplevel,cyclic-import
from urbanstats.special_cases.deduplicate_longnames import drop_duplicate

if isinstance(self.path, list):
s = gpd.GeoDataFrame(pd.concat([gpd.read_file(p) for p in self.path]))
s = s.reset_index(drop=True)
Expand Down
5 changes: 1 addition & 4 deletions urbanstats/special_cases/deduplicate_longnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from permacache import permacache, stable_hash

from urbanstats.data.circle import naive_directions_for_rows_with_names
from urbanstats.geometry.shapefiles.shapefiles_list import shapefiles


def strip_suffix(name):
Expand Down Expand Up @@ -101,14 +102,10 @@ def drop_duplicate(s, duplicates, drop_dup_shapefile_key):

@lru_cache(None)
def load_shapefile_cached(drop_dup_shapefile_key):
from urbanstats.geometry.shapefiles.shapefiles_list import shapefiles

return shapefiles[drop_dup_shapefile_key].load_file()


def shapefile_hash_key(sf_key):
from urbanstats.geometry.shapefiles.shapefiles_list import shapefiles

return shapefiles[sf_key].hash_key


Expand Down

0 comments on commit 2128ec4

Please sign in to comment.