Skip to content

Commit

Permalink
Add ruff (#25)
Browse files Browse the repository at this point in the history
* add ruff

* code quality improvements

* compatibility fix

* compatibility fix
  • Loading branch information
exxamalte committed Jan 31, 2024
1 parent 67d2d4a commit 6b50096
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.11
hooks:
- id: ruff
args:
- --fix
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
Expand Down
4 changes: 2 additions & 2 deletions georss_ingv_centro_nazionale_terremoti_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""INGV Centro Nazionale Terremoti (Earthquakes) library."""
from .feed import IngvCentroNazionaleTerremotiFeed # noqa
from .feed_manager import IngvCentroNazionaleTerremotiFeedManager # noqa
from .feed import IngvCentroNazionaleTerremotiFeed # noqa: F401
from .feed_manager import IngvCentroNazionaleTerremotiFeedManager # noqa: F401
6 changes: 2 additions & 4 deletions georss_ingv_centro_nazionale_terremoti_client/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
"http://shakemap.rm.ingv.it/shake4/data/{}/current/products/intensity.jpg"
)

REGEXP_ATTR_MAGNITUDE = r"Magnitude\(M.{{0,3}}\) (?P<{}>[^ ]+) ".format(
CUSTOM_ATTRIBUTE
)
REGEXP_ATTR_MAGNITUDE = rf"Magnitude\(M.{{0,3}}\) (?P<{CUSTOM_ATTRIBUTE}>[^ ]+) "
REGEXP_ATTR_REGION = r"Magnitude\(M.{{0,3}}\) [^ ]+[ ]+-[ ]+(?P<{}>.+)$".format(
CUSTOM_ATTRIBUTE
)
REGEXP_ATTR_EVENT_ID = r"eventId=(?P<{}>\d+)$".format(CUSTOM_ATTRIBUTE)
REGEXP_ATTR_EVENT_ID = rf"eventId=(?P<{CUSTOM_ATTRIBUTE}>\d+)$"

URL = "http://cnt.rm.ingv.it/feed/atom/all_week"
4 changes: 2 additions & 2 deletions georss_ingv_centro_nazionale_terremoti_client/feed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""INGV Centro Nazionale Terremoti (Earthquakes) feed."""
from typing import Tuple
from __future__ import annotations

from georss_client import ATTR_ATTRIBUTION, GeoRssFeed

Expand All @@ -12,7 +12,7 @@ class IngvCentroNazionaleTerremotiFeed(GeoRssFeed):

def __init__(
self,
home_coordinates: Tuple[float, float],
home_coordinates: tuple[float, float],
filter_radius: float = None,
filter_minimum_magnitude: float = None,
):
Expand Down
12 changes: 6 additions & 6 deletions georss_ingv_centro_nazionale_terremoti_client/feed_entry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""INGV Centro Nazionale Terremoti (Earthquakes) feed entry."""
from typing import Optional, Tuple
from __future__ import annotations

from georss_client import FeedEntry

Expand All @@ -14,7 +14,7 @@
class IngvCentroNazionaleTerremotiFeedEntry(FeedEntry):
"""INGV Centro Nazionale Terremoti feed entry."""

def __init__(self, home_coordinates: Tuple[float, float], rss_entry, attribution):
def __init__(self, home_coordinates: tuple[float, float], rss_entry, attribution):
"""Initialise this service."""
super().__init__(home_coordinates, rss_entry)
self._attribution = attribution
Expand All @@ -25,28 +25,28 @@ def attribution(self) -> str:
return self._attribution

@property
def magnitude(self) -> Optional[float]:
def magnitude(self) -> float | None:
"""Return the magnitude of this entry."""
magnitude = self._search_in_title(REGEXP_ATTR_MAGNITUDE)
if magnitude:
magnitude = float(magnitude)
return magnitude

@property
def region(self) -> Optional[float]:
def region(self) -> float | None:
"""Return the region of this entry."""
return self._search_in_title(REGEXP_ATTR_REGION)

@property
def event_id(self) -> Optional[int]:
def event_id(self) -> int | None:
"""Return the event id of this entry."""
event_id = self._search_in_external_id(REGEXP_ATTR_EVENT_ID)
if event_id:
return int(event_id)
return None

@property
def image_url(self) -> Optional[str]:
def image_url(self) -> str | None:
"""Return the image url of this entry."""
if self.event_id and self.magnitude >= 3:
return IMAGE_URL_PATTERN.format(self.event_id)
Expand Down
4 changes: 2 additions & 2 deletions georss_ingv_centro_nazionale_terremoti_client/feed_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""INGV Centro Nazionale Terremoti (Earthquakes) feed manager."""
from typing import Tuple
from __future__ import annotations

from georss_client.feed_manager import FeedManagerBase

Expand All @@ -14,7 +14,7 @@ def __init__(
generate_callback,
update_callback,
remove_callback,
coordinates: Tuple[float, float],
coordinates: tuple[float, float],
filter_radius: float = None,
filter_minimum_magnitude: float = None,
):
Expand Down
39 changes: 39 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,42 @@ target-version = ['py38', 'py39', 'py310']
[tool.isort]
profile = "black"
src_paths = ["georss_ingv_centro_nazionale_terremoti_client", "tests"]

[tool.ruff]
target-version = "py38"

select = [
"B007", # Loop control variable {name} not used within loop body
"B014", # Exception handler with duplicate exception
"C", # complexity
"D", # docstrings
"E", # pycodestyle
"F", # pyflakes/autoflake
"ICN001", # import concentions; {name} should be imported as {asname}
"PGH004", # Use specific rule codes when using noqa
"PLC0414", # Useless import alias. Import alias does not rename original package.
"SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass
"SIM117", # Merge with-statements that use the same scope
"SIM118", # Use {key} in {dict} instead of {key} in {dict}.keys()
"SIM201", # Use {left} != {right} instead of not {left} == {right}
"SIM212", # Use {a} if {a} else {b} instead of {b} if not {a} else {a}
"SIM300", # Yoda conditions. Use 'age == 42' instead of '42 == age'.
"SIM401", # Use get from dict with default instead of an if block
"T20", # flake8-print
"TRY004", # Prefer TypeError exception for invalid type
"RUF006", # Store a reference to the return value of asyncio.create_task
"UP", # pyupgrade
"W", # pycodestyle
]

ignore = [
"D202", # No blank lines allowed after function docstring
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the second line
"D406", # Section name should end with a newline
"D407", # Section name underlining
"E501", # line too long
"E731", # do not assign a lambda expression, use a def
# Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
]

0 comments on commit 6b50096

Please sign in to comment.