Skip to content

Commit

Permalink
Format to PEP8
Browse files Browse the repository at this point in the history
+  add dev requirements
  • Loading branch information
e-lo committed Sep 18, 2019
1 parent 19cedb8 commit c12616d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 98 deletions.
24 changes: 12 additions & 12 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

# -- Project information -----------------------------------------------------

project = 'Network Wrangler'
copyright = '2019, Metropolitan Council'
author = 'Metropolitan Council'
project = "Network Wrangler"
copyright = "2019, Metropolitan Council"
author = "Metropolitan Council"


# -- General configuration ---------------------------------------------------
Expand All @@ -28,30 +28,30 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx_autodoc_typehints',
'sphinx.ext.inheritance_diagram',
'sphinx.ext.todo',
'sphinx.ext.coverage',
"sphinx.ext.autodoc",
"sphinx_autodoc_typehints",
"sphinx.ext.inheritance_diagram",
"sphinx.ext.todo",
"sphinx.ext.coverage",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]
5 changes: 2 additions & 3 deletions network_wrangler/ProjectCard.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def validate_project_card_schema(
"""
if not os.path.exists(card_schema_file):
base_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"schemas",
os.path.dirname(os.path.realpath(__file__)), "schemas"
)
card_schema_file = os.path.join(base_path, card_schema_file)

Expand Down Expand Up @@ -112,7 +111,7 @@ def build_link_selection_query(selection: dict, mode="isDriveLink", ignore=[]):
# return sel_query

for l in selection["link"]:
for key,value in l.items():
for key, value in l.items():
if key in ignore:
continue
if isinstance(value, list):
Expand Down
27 changes: 12 additions & 15 deletions network_wrangler/RoadwayNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ def validate_node_schema(
"""
if not os.path.exists(schema_location):
base_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"schemas"
os.path.dirname(os.path.realpath(__file__)), "schemas"
)
schema_location = os.path.join(base_path, schema_location)

Expand Down Expand Up @@ -248,8 +247,7 @@ def validate_link_schema(

if not os.path.exists(schema_location):
base_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"schemas"
os.path.dirname(os.path.realpath(__file__)), "schemas"
)
schema_location = os.path.join(base_path, schema_location)

Expand Down Expand Up @@ -286,8 +284,7 @@ def validate_shape_schema(

if not os.path.exists(schema_location):
base_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"schemas"
os.path.dirname(os.path.realpath(__file__)), "schemas"
)
schema_location = os.path.join(base_path, schema_location)

Expand Down Expand Up @@ -339,22 +336,22 @@ def validate_selection(self, selection: dict) -> Bool:

err = []
for l in selection["link"]:
for k,v in l.items():
if k not in self.links_df.columns:
err.append(
"{} specified in link selection but not an attribute in network\n".format(
k
for k, v in l.items():
if k not in self.links_df.columns:
err.append(
"{} specified in link selection but not an attribute in network\n".format(
k
)
)
)
for k,v in selection["A"].items():
for k, v in selection["A"].items():
if k not in self.nodes_df.columns and k != RoadwayNetwork.NODE_FOREIGN_KEY:
err.append(
"{} specified in A node selection but not an attribute in network\n".format(
k
)
)
for k,v in selection["B"].items():
print("l3",k,v)
for k, v in selection["B"].items():
print("l3", k, v)
if k not in self.nodes_df.columns and k != RoadwayNetwork.NODE_FOREIGN_KEY:
err.append(
"{} specified in B node selection but not an attribute in network\n".format(
Expand Down
10 changes: 8 additions & 2 deletions network_wrangler/Scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def __init__(self, base_scenario: dict, project_cards: [ProjectCard] = None):
self.prerequisites_sorted = False

for card in self.project_cards:
self.prerequisites.update({card.project: card.dependencies["prerequisites"]})
self.prerequisites.update(
{card.project: card.dependencies["prerequisites"]}
)
self.corequisites.update({card.project: card.dependencies["corequisites"]})

@staticmethod
Expand Down Expand Up @@ -107,7 +109,11 @@ def add_project_cards(self, folder: str, tags: [str] = []):
}
)
self.conflicts.update(
{project_card.project: project_card.dependencies["conflicts"]}
{
project_card.project: project_card.dependencies[
"conflicts"
]
}
)

def __str__(self):
Expand Down
72 changes: 34 additions & 38 deletions network_wrangler/TransitNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .Logger import WranglerLogger
from .Utils import parse_time_spans


class TransitNetwork(object):
"""
Representation of a Transit Network.
Expand Down Expand Up @@ -121,60 +122,55 @@ def select_transit_features(self, selection: dict) -> pd.Series:
freq = self.feed.frequencies

# Turn selection's values into lists if they are not already
for key in ['trip_id', 'route_id', 'short_name', 'long_name']:
if (selection.get(key) is not None and
type(selection.get(key)) != list):
for key in ["trip_id", "route_id", "short_name", "long_name"]:
if selection.get(key) is not None and type(selection.get(key)) != list:
selection[key] = [selection[key]]

# Based on the key in selection, filter trips
if 'trip_id' in selection:
trips = trips[trips.trip_id.isin(selection['trip_id'])]
if "trip_id" in selection:
trips = trips[trips.trip_id.isin(selection["trip_id"])]

elif 'route_id' in selection:
trips = trips[trips.route_id.isin(selection['route_id'])]
elif "route_id" in selection:
trips = trips[trips.route_id.isin(selection["route_id"])]

elif 'short_name' in selection:
routes = routes[
routes.route_short_name.isin(selection['short_name'])
]
trips = trips[trips.route_id.isin(routes['route_id'])]
elif "short_name" in selection:
routes = routes[routes.route_short_name.isin(selection["short_name"])]
trips = trips[trips.route_id.isin(routes["route_id"])]

elif 'long_name' in selection:
elif "long_name" in selection:
matches = []
for sel in selection['long_name']:
for long_name in routes['route_long_name']:
for sel in selection["long_name"]:
for long_name in routes["route_long_name"]:
x = re.search(sel, long_name)
if x is not None:
matches.append(long_name)

routes = routes[routes.route_long_name.isin(matches)]
trips = trips[trips.route_id.isin(routes['route_id'])]
trips = trips[trips.route_id.isin(routes["route_id"])]

else:
WranglerLogger.error(
'Selection not supported %s', selection.keys()
)
WranglerLogger.error("Selection not supported %s", selection.keys())
raise ValueError

# If a time key exists, filter trips using frequency table
if selection.get('time') is not None:
selection['time'] = parse_time_spans(selection['time'])
if selection.get("time") is not None:
selection["time"] = parse_time_spans(selection["time"])

# Filter freq to trips in selection
freq = freq[freq.trip_id.isin(trips['trip_id'])]
freq = freq[freq.start_time == selection['time'][0]]
freq = freq[freq.end_time == selection['time'][1]]
freq = freq[freq.trip_id.isin(trips["trip_id"])]
freq = freq[freq.start_time == selection["time"][0]]
freq = freq[freq.end_time == selection["time"][1]]

# Filter trips table to those still in freq table
trips = trips[trips.trip_id.isin(freq['trip_id'])]
trips = trips[trips.trip_id.isin(freq["trip_id"])]

# Check that there is at least one trip in trips table or raise error
if len(trips) < 1:
WranglerLogger.error('Selection returned zero trips')
WranglerLogger.error("Selection returned zero trips")
raise ValueError

# Return pandas.Series of trip_ids
return trips['trip_id']
return trips["trip_id"]

def apply_transit_feature_change(
self, trip_ids: pd.Series, properties: dict
Expand All @@ -195,7 +191,7 @@ def apply_transit_feature_change(
None
"""
for i in properties:
if i['property'] in ['headway_secs']:
if i["property"] in ["headway_secs"]:
self.apply_transit_feature_change_frequency(trip_ids, i)

# elif i['property'] in ['stops']:
Expand All @@ -213,25 +209,25 @@ def apply_transit_feature_change_frequency(
freq = freq[freq.trip_id.isin(trip_ids)]

# Check all `existing` properties if given
if properties.get('existing') is not None:
if not all(freq.headway_secs == properties['existing']):
if properties.get("existing") is not None:
if not all(freq.headway_secs == properties["existing"]):
WranglerLogger.error(
'Existing does not match for at least '
'1 trip in:\n {}'.format(trip_ids.to_string())
"Existing does not match for at least "
"1 trip in:\n {}".format(trip_ids.to_string())
)
raise ValueError

# Calculate build value
if properties.get('set') is not None:
build_value = properties['set']
if properties.get("set") is not None:
build_value = properties["set"]
else:
build_value = [i + properties['change'] for i in freq.headway_secs]
build_value = [i + properties["change"] for i in freq.headway_secs]

# Update self or return a new object
q = self.feed.frequencies.trip_id.isin(freq['trip_id'])
q = self.feed.frequencies.trip_id.isin(freq["trip_id"])
if in_place:
self.feed.frequencies.loc[q, properties['property']] = build_value
self.feed.frequencies.loc[q, properties["property"]] = build_value
else:
updated_network = copy.deepcopy(self)
updated_network.loc[q, properties['property']] = build_value
updated_network.loc[q, properties["property"]] = build_value
return updated_network
14 changes: 7 additions & 7 deletions network_wrangler/Utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pandas as pd
from .Logger import WranglerLogger


def point_df_to_geojson(df: pd.DataFrame, properties: list):
"""
Author: Geoff Boeing:
Expand Down Expand Up @@ -67,6 +68,7 @@ def make_slug(text, delimiter: str = "_"):
text = re.sub("[,.;@#?!&$']+", "", text.lower())
return re.sub("[\ ]+", delimiter, text)


def parse_time_spans(times):
"""
parse time spans into tuples of seconds from midnight
Expand All @@ -83,19 +85,17 @@ def parse_time_spans(times):
try:
start_time, end_time = times
except:
WranglerLogger.error(
"ERROR: times should be a tuple or list of two strings"
)
WranglerLogger.error("ERROR: times should be a tuple or list of two strings")
raise ValueError()

start_time=start_time.strip()
end_time=end_time.strip()
start_time = start_time.strip()
end_time = end_time.strip()

# If time is given without seconds, add 00
if len(start_time) <= 5:
start_time += ':00'
start_time += ":00"
if len(end_time) <= 5:
end_time += ':00'
end_time += ":00"

# Convert times to seconds from midnight (Partride's time storage)
h0, m0, s0 = start_time.split(":")
Expand Down
50 changes: 29 additions & 21 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
from setuptools import setup

classifiers = ['Development Status :: 1 - Planning',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7']
classifiers = [
"Development Status :: 1 - Planning",
"License :: OSI Approved :: Apache Software License",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
]

with open('README.md') as f:
with open("README.md") as f:
long_description = f.read()

with open('requirements.txt') as f:
with open("requirements.txt") as f:
requirements = f.readlines()

with open("dev-requirements.txt") as f:
requirements += f.readlines()

install_requires = [r.strip() for r in requirements]

setup(name='network_wrangler',
version='0.0.0',
description='',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://github.com/wsp-sag/network_wrangler',
license='Apache 2',
platforms='any',
packages=['network_wrangler'],
include_package_data=True,
install_requires=install_requires)
setup(
name="network_wrangler",
version="0.0.0",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/wsp-sag/network_wrangler",
license="Apache 2",
platforms="any",
packages=["network_wrangler"],
include_package_data=True,
install_requires=install_requires,
)

0 comments on commit c12616d

Please sign in to comment.