Skip to content

Commit

Permalink
**Refactor and modernize codebase for improved clarity and functional…
Browse files Browse the repository at this point in the history
…ity**

Refactored several modules and test cases to use type annotations, f-strings, and other Python 3 modernizations. Consolidated imports for better maintainability and readability. Enhanced context management and error handling, while replacing redundant or obsolete patterns like `list()` comprehensions and legacy formatting.
  • Loading branch information
gmr committed Feb 9, 2025
1 parent 6ad9fbc commit 11b0b8e
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 406 deletions.
25 changes: 7 additions & 18 deletions pgdumplib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
unless you are hacking on the library itself.
"""
import typing

K_VERSION_MAP = {
((9, 0, 0), (10, 2)): (1, 12, 0),
Expand All @@ -24,14 +23,7 @@
FORMAT_NULL: int = 4
FORMAT_DIRECTORY: int = 5

FORMATS: typing.List[str] = [
'Unknown',
'Custom',
'Files',
'Tar',
'Null',
'Directory'
]
FORMATS: list[str] = ['Unknown', 'Custom', 'Files', 'Tar', 'Null', 'Directory']

K_OFFSET_POS_NOT_SET: int = 1
"""Specifies the entry has data but no offset"""
Expand All @@ -42,10 +34,10 @@

MAGIC: bytes = b'PGDMP'

MIN_VER: typing.Tuple[int, int, int] = (1, 12, 0)
MIN_VER: tuple[int, int, int] = (1, 12, 0)
"""The minumum supported version of pg_dump files ot support"""

MAX_VER: typing.Tuple[int, int, int] = (1, 14, 0)
MAX_VER: tuple[int, int, int] = (1, 14, 0)
"""The maximum supported version of pg_dump files ot support"""

PGDUMP_STRFTIME_FMT: str = '%Y-%m-%d %H:%M:%S %Z'
Expand All @@ -62,14 +54,11 @@
SECTION_POST_DATA: str = 'Post-Data'
"""Post-data section for an entry in a dump's table of contents"""

SECTIONS: typing.List[str] = [
SECTION_NONE,
SECTION_PRE_DATA,
SECTION_DATA,
SECTION_POST_DATA
SECTIONS: list[str] = [
SECTION_NONE, SECTION_PRE_DATA, SECTION_DATA, SECTION_POST_DATA
]

VERSION: typing.Tuple[int, int, int] = (1, 12, 0)
VERSION: tuple[int, int, int] = (1, 12, 0)
"""pg_dump file format version to create by default"""

ZLIB_OUT_SIZE: int = 4096
Expand Down Expand Up @@ -144,7 +133,7 @@
USER_MAPPING: str = 'USER MAPPING'
VIEW: str = 'VIEW'

SECTION_MAPPING: typing.Dict[str, str] = {
SECTION_MAPPING: dict[str, str] = {
ACCESS_METHOD: SECTION_PRE_DATA,
ACL: SECTION_NONE,
AGGREGATE: SECTION_PRE_DATA,
Expand Down
23 changes: 7 additions & 16 deletions pgdumplib/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import datetime
import decimal
import ipaddress
import typing
import uuid

import pendulum
Expand All @@ -33,7 +32,7 @@ class DataConverter:
"""
@staticmethod
def convert(row: str) -> typing.Tuple[typing.Optional[str], ...]:
def convert(row: str) -> tuple[str | None, ...]:
"""Convert the string based row into a tuple of columns.
:param str row: The row to convert
Expand All @@ -56,17 +55,9 @@ def convert(row: str) -> str:
return row


SmartColumn = typing.Union[
None,
str,
int,
datetime.datetime,
decimal.Decimal,
ipaddress.IPv4Address,
ipaddress.IPv4Network,
ipaddress.IPv6Address,
ipaddress.IPv6Network,
uuid.UUID]
SmartColumn = (None | str | int | datetime.datetime | decimal.Decimal
| ipaddress.IPv4Address | ipaddress.IPv4Network
| ipaddress.IPv6Address | ipaddress.IPv6Network | uuid.UUID)


class SmartDataConverter(DataConverter):
Expand All @@ -89,7 +80,7 @@ class SmartDataConverter(DataConverter):
- :py:class:`uuid.UUID`
"""
def convert(self, row: str) -> typing.Tuple[SmartColumn, ...]:
def convert(self, row: str) -> tuple[SmartColumn, ...]:
"""Convert the string based row into a tuple of columns"""
return tuple(self._convert_column(c) for c in row.split('\t'))

Expand Down Expand Up @@ -119,8 +110,8 @@ def _convert_column(column: str) -> SmartColumn:
pass
for tz_fmt in {'Z', 'ZZ', 'z', 'zz'}:
try:
return pendulum.from_format(
column, 'YYYY-MM-DD HH:mm:ss {}'.format(tz_fmt))
return pendulum.from_format(column,
f'YYYY-MM-DD HH:mm:ss {tz_fmt}')
except ValueError:
pass
return column
Loading

0 comments on commit 11b0b8e

Please sign in to comment.