diff --git a/fastkml/enums.py b/fastkml/enums.py index e1ef370a..df403e09 100644 --- a/fastkml/enums.py +++ b/fastkml/enums.py @@ -118,7 +118,7 @@ class DataType(REnum): def convert(self, value: str) -> Union[str, int, float, bool]: """Convert the data type to a python type.""" if self == DataType.string: - return str(value) + return value if self in {DataType.int_, DataType.uint, DataType.short, DataType.ushort}: return int(value) if self in {DataType.float_, DataType.double}: diff --git a/fastkml/kml.py b/fastkml/kml.py index 2d3b02b9..a3267d35 100644 --- a/fastkml/kml.py +++ b/fastkml/kml.py @@ -210,9 +210,7 @@ def style_url(self) -> Optional[str]: Returns the url only, not a full StyleUrl object. if you need the full StyleUrl object use _style_url. """ - if isinstance(self._style_url, StyleUrl): - return self._style_url.url - return None + return self._style_url.url if isinstance(self._style_url, StyleUrl) else None @style_url.setter def style_url(self, styleurl: Union[str, StyleUrl, None]) -> None: @@ -262,9 +260,7 @@ def link(self, url) -> None: @property def author(self) -> None: - if self._atom_author: - return self._atom_author.name - return None + return self._atom_author.name if self._atom_author else None @author.setter def author(self, name) -> None: @@ -300,8 +296,7 @@ def snippet(self) -> Optional[Dict[str, Any]]: if not self._snippet: return None if isinstance(self._snippet, dict): - text = self._snippet.get("text") - if text: + if text := self._snippet.get("text"): assert isinstance(text, str) max_lines = self._snippet.get("maxLines", None) if max_lines is None: @@ -339,9 +334,7 @@ def snippet(self, snip=None) -> None: @property def address(self) -> None: - if self._address: - return self._address - return None + return self._address if self._address else None @address.setter def address(self, address) -> None: @@ -354,9 +347,7 @@ def address(self, address) -> None: @property def phone_number(self) -> None: - if self._phone_number: - return self._phone_number - return None + return self._phone_number if self._phone_number else None @phone_number.setter def phone_number(self, phone_number) -> None: @@ -1647,10 +1638,7 @@ def etree_element( def get_style_by_url(self, style_url: str) -> Union[Style, StyleMap]: id = urlparse.urlparse(style_url).fragment - for style in self.styles(): - if style.id == id: - return style - return None + return next((style for style in self.styles() if style.id == id), None) class Folder(_Container): @@ -1717,9 +1705,7 @@ def __init__( @property def geometry(self) -> Optional[AnyGeometryType]: - if self._geometry is not None: - return self._geometry.geometry - return None + return self._geometry.geometry if self._geometry is not None else None def from_element(self, element: Element, strict=False) -> None: super().from_element(element) diff --git a/fastkml/styles.py b/fastkml/styles.py index 5ac99055..7e99b4e8 100644 --- a/fastkml/styles.py +++ b/fastkml/styles.py @@ -46,9 +46,7 @@ def strtobool(val: str) -> int: val = val.lower() if val == "false": return 0 - if val == "true": - return 1 - return int(float(val)) + return 1 if val == "true" else int(float(val)) class StyleUrl(_BaseObject): diff --git a/fastkml/times.py b/fastkml/times.py index b71063eb..cec717c0 100644 --- a/fastkml/times.py +++ b/fastkml/times.py @@ -125,8 +125,7 @@ def parse(cls, datestr: str) -> Optional["KmlDateTime"]: """Parse a KML DateTime string into a KmlDateTime object.""" resolution = None dt = None - year_month_day_match = year_month_day.match(datestr) - if year_month_day_match: + if year_month_day_match := year_month_day.match(datestr): year = int(year_month_day_match.group("year")) month = int(year_month_day_match.group("month") or 1) day = int(year_month_day_match.group("day") or 1) @@ -226,16 +225,14 @@ def etree_element( ) -> Element: element = super().etree_element(precision=precision, verbosity=verbosity) if self.begin is not None: - text = str(self.begin) - if text: + if text := str(self.begin): begin = config.etree.SubElement( # type: ignore[attr-defined] element, f"{self.ns}begin", ) begin.text = text if self.end is not None: - text = str(self.end) - if text: + if text := str(self.end): end = config.etree.SubElement( # type: ignore[attr-defined] element, f"{self.ns}end", diff --git a/tests/base_test.py b/tests/base_test.py index 0ab1b2fa..69002c62 100644 --- a/tests/base_test.py +++ b/tests/base_test.py @@ -76,10 +76,7 @@ class Test(base._BaseObject): def test_base_from_element_raises(self) -> None: be = base._BaseObject() - element = cast( - types.Element, - config.etree.Element(config.KMLNS + "Base"), # type: ignore[attr-defined] - ) + element = cast(types.Element, config.etree.Element(f"{config.KMLNS}Base")) with pytest.raises(TypeError): be.from_element(element=element)