Skip to content

Commit

Permalink
Change default for clean_shapes and clean_routes to false
Browse files Browse the repository at this point in the history
- linted
  • Loading branch information
e-lo committed Sep 4, 2024
1 parent 3a5bc64 commit 530cebc
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion projectcard/examples/transit-service_deletion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ transit_service_deletion:
timespans:
- ['06:00:00', '09:00:00']
clean_shapes: false
clean_routes: false
clean_routes: true
2 changes: 1 addition & 1 deletion projectcard/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def dict_to_yaml_with_comments(d):

for line in yaml_lines:
if "#" in line:
final_yaml_lines.append(f"#{line}")
final_yaml_lines.append(f"#{line}")
else:
final_yaml_lines.append(line)

Expand Down
3 changes: 1 addition & 2 deletions projectcard/projectcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def __init__(self, sp_dictionary: dict, parent_project: ProjectCard):
sp_dictionary (dict): dictionary of sub-project attributes contained within "changes"
list of parent projet card
parent_project (ProjectCard): ProjectCard object for parent project card
"""
self._parent_project = parent_project

Expand Down Expand Up @@ -255,4 +255,3 @@ def facility(self) -> dict:
def valid(self) -> bool:
"""Check if subproject is valid."""
return self._parent_project.valid

8 changes: 4 additions & 4 deletions projectcard/schema/changes/transit_service_deletion.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
"service": {"$ref": "../transit/selection/select_trips.json"},
"clean_shapes": {
"type": "boolean",
"description": "If true, unused shapes for these trips will be removed. Defaults to true.",
"default": true
"description": "If true, unused shapes for these trips will be removed. Defaults to false.",
"default": false
},
"clean_routes": {
"type": "boolean",
"description": "If true, unused routes for these trips will be removed. Defaults to true.",
"default": true
"description": "If true, unused routes for these trips will be removed. Defaults to false.",
"default": false
}
}
}
8 changes: 7 additions & 1 deletion projectcard/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ def _update_roadway_addition(change, default_roadway_values: dict = DEFAULT_ROAD
for field, default_value in default_roadway_values[p].items():
if field not in item or item[field] is None or item[field] == "":
item[field] = default_value
if field in ["walk_access","bike_access","drive_access","bus_only","rail_only"]:
if field in [
"walk_access",
"bike_access",
"drive_access",
"bus_only",
"rail_only",
]:
item[field] = int(item[field])
if p == "links":
item["roadway"] = item["roadway"].lower()
Expand Down
18 changes: 9 additions & 9 deletions projectcard/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,34 @@ def update_dict_with_schema_defaults(
if isinstance(schema, str) or isinstance(schema, Path):
schema = _load_schema(schema)

if 'properties' in schema:
for prop_name, schema_part in schema['properties'].items():
if "properties" in schema:
for prop_name, schema_part in schema["properties"].items():
# Only update if the property is required, has a default, and is not already there
if (
prop_name not in data
and 'default' in schema_part
and prop_name in schema.get('required', [])
and "default" in schema_part
and prop_name in schema.get("required", [])
):
CardLogger.debug(f"Adding default value for {prop_name}: {schema_part['default']}")
data[prop_name] = schema_part['default']
data[prop_name] = schema_part["default"]
elif (
prop_name in data
and isinstance(data[prop_name], dict)
and "properties" in schema_part
):
data[prop_name] = update_dict_with_schema_defaults(data[prop_name], schema_part)
elif (
prop_name in data
and isinstance(data[prop_name], list)
and "items" in schema_part
prop_name in data and isinstance(data[prop_name], list) and "items" in schema_part
):
for item in data[prop_name]:
if isinstance(item, dict):
update_dict_with_schema_defaults(item, schema_part["items"])
return data


def validate_card(jsondata: dict, schema_path: Union[str, Path] = PROJECTCARD_SCHEMA, parse_defaults: bool = True) -> bool:
def validate_card(
jsondata: dict, schema_path: Union[str, Path] = PROJECTCARD_SCHEMA, parse_defaults: bool = True
) -> bool:
"""Validates json-like data to specified schema.
If `pycode` key exists, will evaluate it for basic runtime errors using Flake8.
Expand Down
12 changes: 4 additions & 8 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

def test_update_dict_with_schema_defaults():
from projectcard.validate import update_dict_with_schema_defaults

# Test case 1: Missing required property with default value
data = {}
schema = {
Expand Down Expand Up @@ -125,16 +126,11 @@ def test_update_dict_with_schema_defaults():

def test_update_project_card_with_defaults():
from projectcard.projectcard import ProjectCard

project_card_data = {
"project": "Delete Transit",
"transit_service_deletion": {
"service": {
"trip_properties": {
"trip_id": ["trip_1"]
}
}
}
"transit_service_deletion": {"service": {"trip_properties": {"trip_id":["trip_1"]}}},
}
card = ProjectCard(project_card_data)
CardLogger.debug(f"card:\n{card}")
assert card.transit_service_deletion["clean_shapes"] is True
assert card.transit_service_deletion["clean_shapes"] is False

0 comments on commit 530cebc

Please sign in to comment.