Skip to content

Commit

Permalink
try to add type hints, not done
Browse files Browse the repository at this point in the history
  • Loading branch information
joshinils committed Mar 3, 2024
1 parent ca8c65f commit e43bc4d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 117 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ repos:
hooks:
- id: isort
name: isort
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: 'v0.931' # Use the sha / tag you want to point at
# hooks:
# - id: mypy
# args: [--disallow-untyped-defs, --disallow-incomplete-defs]
# # args: [--disallow-untyped-defs, --disallow-incomplete-defs, --disallow-untyped-calls]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.931' # Use the sha / tag you want to point at
hooks:
- id: mypy
args: [--disallow-untyped-defs, --disallow-incomplete-defs]
# args: [--disallow-untyped-defs, --disallow-incomplete-defs, --disallow-untyped-calls]
1 change: 0 additions & 1 deletion cycling_quality_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
version/date: 2024-03-02
"""

import imp
import imp
import pathlib

Expand Down
27 changes: 13 additions & 14 deletions helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
functions used in multiple files to be imported
"""

from typing import Any, List, Optional

from qgis import *
from qgis.core import *
from qgis.core import NULL, QgsFeature
from qgis.PyQt.QtCore import *
from qgis.core import QgsFeature
from qgis.core import NULL
from qgis.PyQt.QtCore import QVariant # type: ignore

debug_warning_counter__derive_attribute = 0
debug_warning_counter__cast_to_float = 0
Expand Down Expand Up @@ -98,7 +100,7 @@ def get_access(feature, access_key):
return access_value


def cast_to_float(value):
def cast_to_float(value: Any) -> QVariant | float:
# return a value as a float
try:
return float(value)
Expand All @@ -112,12 +114,12 @@ def cast_to_float(value):
return NULL


def get_weakest_surface_value(value_list):
def get_weakest_surface_value(values: List[str]) -> Optional[str]:
"""
from a list of surface values, choose the weakest one
"""

# surface values in descent order
# surface values in descending order
surface_value_list = [
'asphalt',
'paved',
Expand Down Expand Up @@ -150,15 +152,12 @@ def get_weakest_surface_value(value_list):
'rock'
]

value = NULL
for i in range(len(value_list)):
if value_list[i] in surface_value_list:
if not value:
value = value_list[i]
else:
if surface_value_list.index(value_list[i]) > surface_value_list.index(value):
value = value_list[i]
return value
return_value = None
for value in values:
if value in surface_value_list:
if surface_value_list.index(value) > surface_value_list.index(value):
return_value = value
return return_value


def add_delimited_value(var, value):
Expand Down
11 changes: 4 additions & 7 deletions script_main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import imp
import time
from os.path import exists
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path
from pathlib import Path

import qgis.processing as processing # type: ignore
from qgis.core import ( # type: ignore
Expand Down Expand Up @@ -37,7 +34,7 @@ def main(dir_input: Path, dir_output: Path) -> None:
layer_way_input = QgsVectorLayer(f"{dir_input}|geometrytype=LineString", 'way input', 'ogr')

print(time.strftime('%H:%M:%S', time.localtime()), 'Reproject data...')
layer = processing.run(
layer_1: QgsVectorLayer = processing.run(
'native:reprojectlayer',
{
'INPUT': layer_way_input,
Expand Down Expand Up @@ -244,10 +241,10 @@ def main(dir_input: Path, dir_output: Path) -> None:
'crossing',
'crossing:markings'
]
layer = processing.run(
layer: QgsVectorLayer = processing.run(
'native:retainfields',
{
'INPUT': layer,
'INPUT': layer_1,
'FIELDS': attributes_list,
'OUTPUT': 'memory:'
}
Expand Down
21 changes: 12 additions & 9 deletions script_step_01.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import imp
import time
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from qgis.core import QgsFeature, QgsVectorLayer

import qgis.processing as processing
from qgis.core import (
NULL, QgsProcessingFeatureSourceDefinition, QgsProject, edit
NULL, QgsFeature, QgsProcessingFeatureSourceDefinition, QgsProject,
QgsVectorLayer, edit
)
from qgis.PyQt.QtCore import QVariant # type: ignore

import helper_functions
import vars_settings
Expand All @@ -17,7 +15,12 @@
imp.reload(helper_functions)


def step_01(layer, id_proc_highway, id_proc_maxspeed, id_proc_sidepath):
def step_01(
layer: QgsVectorLayer,
id_proc_highway: int,
id_proc_maxspeed: int,
id_proc_sidepath: int,
):
"""
Check paths whether they are sidepath (a path along a road)
"""
Expand Down Expand Up @@ -161,7 +164,7 @@ def step_01(layer, id_proc_highway, id_proc_maxspeed, id_proc_sidepath):
with edit(layer):
for feature in layer.getFeatures():
hw = feature.attribute('highway')
maxspeed = feature.attribute('maxspeed')
maxspeed: QVariant = feature.attribute('maxspeed')
if maxspeed == 'walk':
maxspeed = 10
else:
Expand All @@ -171,10 +174,10 @@ def step_01(layer, id_proc_highway, id_proc_maxspeed, id_proc_sidepath):
layer.changeAttributeValue(feature.id(), id_proc_maxspeed, maxspeed)
continue
id = feature.attribute('id')
is_sidepath = feature.attribute('is_sidepath')
is_sidepath: QVariant = feature.attribute('is_sidepath')
if feature.attribute('footway') == 'sidewalk':
is_sidepath = 'yes'
is_sidepath_of = feature.attribute('is_sidepath:of')
is_sidepath_of: QVariant = feature.attribute('is_sidepath:of')
checks = sidepath_dict[id]['checks']

if not is_sidepath:
Expand Down
5 changes: 3 additions & 2 deletions script_step_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from qgis.core import (
NULL, QgsProcessingFeatureSourceDefinition, QgsProperty, edit
)
from qgis.PyQt.QtCore import QVariant # type: ignore

import helper_functions
import vars_settings
Expand Down Expand Up @@ -36,8 +37,8 @@ def step_02(
with edit(layer):
for feature in layer.getFeatures():
highway = feature.attribute('highway')
cycleway = feature.attribute('cycleway')
cycleway_both = feature.attribute('cycleway:both')
cycleway: QVariant = feature.attribute('cycleway')
cycleway_both: QVariant = feature.attribute('cycleway:both')
cycleway_left = feature.attribute('cycleway:left')
cycleway_right = feature.attribute('cycleway:right')
sidewalk_bicycle = feature.attribute('sidewalk:bicycle')
Expand Down
Loading

0 comments on commit e43bc4d

Please sign in to comment.