Skip to content

Commit

Permalink
Run ruff's auto-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Jan 29, 2025
1 parent ea11c45 commit 62c7084
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 97 deletions.
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# maxminddb documentation build configuration file, created by
# sphinx-quickstart on Tue Apr 9 13:34:57 2013.
Expand All @@ -12,8 +11,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os
import sys

sys.path.insert(0, os.path.abspath(".."))
import maxminddb
Expand Down
6 changes: 3 additions & 3 deletions examples/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import argparse
import maxminddb
import random
import socket
import struct
import timeit

import maxminddb

parser = argparse.ArgumentParser(description="Benchmark maxminddb.")
parser.add_argument("--count", default=250000, type=int, help="number of lookups")
parser.add_argument("--mode", default=0, type=int, help="reader mode to use")
Expand All @@ -30,4 +30,4 @@ def lookup_ip_address():
number=args.count,
)

print("{:,}".format(int(args.count / elapsed)), "lookups per second")
print(f"{int(args.count / elapsed):,}", "lookups per second")
5 changes: 3 additions & 2 deletions maxminddb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@


__all__ = [
"InvalidDatabaseError",
"MODE_AUTO",
"MODE_FD",
"MODE_FILE",
"MODE_MEMORY",
"MODE_MMAP",
"MODE_MMAP_EXT",
"InvalidDatabaseError",
"Reader",
"open_database",
]
Expand All @@ -51,6 +51,7 @@ def open_database(
a path. This mode implies MODE_MEMORY.
* MODE_AUTO - tries MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that
order. Default mode.
"""
if mode not in (
MODE_AUTO,
Expand All @@ -70,7 +71,7 @@ def open_database(

if not has_extension:
raise ValueError(
"MODE_MMAP_EXT requires the maxminddb.extension module to be available"
"MODE_MMAP_EXT requires the maxminddb.extension module to be available",
)

# The C type exposes the same API as the Python Reader, so for type
Expand Down
15 changes: 10 additions & 5 deletions maxminddb/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

import struct
from typing import cast, Dict, List, Tuple, Union
from typing import Dict, List, Tuple, Union, cast

try:
# pylint: disable=unused-import
Expand Down Expand Up @@ -37,6 +37,7 @@ def __init__(
database_buffer -- an mmap'd MaxMind DB file.
pointer_base -- the base number to use when decoding a pointer
pointer_test -- used for internal unit testing of pointer code
"""
self._pointer_test = pointer_test
self._buffer = database_buffer
Expand Down Expand Up @@ -142,6 +143,7 @@ def decode(self, offset: int) -> Tuple[Record, int]:
Arguments:
offset -- the location of the data structure to decode
"""
new_offset = offset + 1
ctrl_byte = self._buffer[offset]
Expand All @@ -154,7 +156,7 @@ def decode(self, offset: int) -> Tuple[Record, int]:
decoder = self._type_decoder[type_num]
except KeyError as ex:
raise InvalidDatabaseError(
f"Unexpected type number ({type_num}) encountered"
f"Unexpected type number ({type_num}) encountered",
) from ex

(size, new_offset) = self._size_from_ctrl_byte(ctrl_byte, new_offset, type_num)
Expand All @@ -166,7 +168,7 @@ def _read_extended(self, offset: int) -> Tuple[int, int]:
if type_num < 7:
raise InvalidDatabaseError(
"Something went horribly wrong in the decoder. An "
f"extended type resolved to a type number < 8 ({type_num})"
f"extended type resolved to a type number < 8 ({type_num})",
)
return type_num, offset + 1

Expand All @@ -175,11 +177,14 @@ def _verify_size(expected: int, actual: int) -> None:
if expected != actual:
raise InvalidDatabaseError(
"The MaxMind DB file's data section contains bad data "
"(unknown data type or corrupt data)"
"(unknown data type or corrupt data)",
)

def _size_from_ctrl_byte(
self, ctrl_byte: int, offset: int, type_num: int
self,
ctrl_byte: int,
offset: int,
type_num: int,
) -> Tuple[int, int]:
size = ctrl_byte & 0x1F
if type_num == 1 or size < 29:
Expand Down
24 changes: 14 additions & 10 deletions maxminddb/extension.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ This module contains the C extension database reader and related classes.
"""

# pylint: disable=E0601,E0602
from ipaddress import IPv4Address, IPv6Address
from os import PathLike
from typing import Any, AnyStr, Dict, IO, List, Optional, Tuple, Union
from maxminddb import MODE_AUTO
from typing import IO, Any, AnyStr, Dict, List, Optional, Tuple, Union

from maxminddb.types import Record

class Reader:
"""
A C extension implementation of a reader for the MaxMind DB format. IP
"""A C extension implementation of a reader for the MaxMind DB format. IP
addresses can be looked up using the ``get`` method.
"""

closed: bool = ...

def __init__(
self, database: Union[AnyStr, int, PathLike, IO], mode: int = MODE_AUTO
self,
database: Union[AnyStr, int, PathLike, IO],
mode: int = ...,
) -> None:
"""Reader for the MaxMind DB file format
Expand All @@ -30,6 +32,7 @@ class Reader:
file, or a file descriptor in the case of MODE_FD.
mode -- mode to open the database with. The only supported modes are
MODE_AUTO and MODE_MMAP_EXT.
"""

def close(self) -> None:
Expand All @@ -38,25 +41,26 @@ class Reader:
def get(self, ip_address: Union[str, IPv6Address, IPv4Address]) -> Optional[Record]:
"""Return the record for the ip_address in the MaxMind DB
Arguments:
ip_address -- an IP address in the standard string notation
"""

def get_with_prefix_len(
self, ip_address: Union[str, IPv6Address, IPv4Address]
self,
ip_address: Union[str, IPv6Address, IPv4Address],
) -> Tuple[Optional[Record], int]:
"""Return a tuple with the record and the associated prefix length
Arguments:
ip_address -- an IP address in the standard string notation
"""

def metadata(self) -> "Metadata":
def metadata(self) -> Metadata:
"""Return the metadata associated with the MaxMind DB file"""

def __enter__(self) -> "Reader": ...
def __enter__(self) -> Reader: ...
def __exit__(self, *args) -> None: ...

# pylint: disable=too-few-public-methods
Expand Down
4 changes: 2 additions & 2 deletions maxminddb/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def close(self) -> None:
if hasattr(os, "pread"):

def _read(self, buffersize: int, offset: int) -> bytes:
"""read that uses pread"""
"""Read that uses pread"""
# pylint: disable=no-member
return os.pread(self._handle.fileno(), buffersize, offset) # type: ignore

else:

def _read(self, buffersize: int, offset: int) -> bytes:
"""read with a lock
"""Read with a lock
This lock is necessary as after a fork, the different processes
will share the same file table entry, even if we dup the fd, and
Expand Down
29 changes: 17 additions & 12 deletions maxminddb/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import struct
from ipaddress import IPv4Address, IPv6Address
from os import PathLike
from typing import Any, AnyStr, Dict, IO, List, Optional, Tuple, Union
from typing import IO, Any, AnyStr, Dict, List, Optional, Tuple, Union

from maxminddb.const import MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY, MODE_FD
from maxminddb.const import MODE_AUTO, MODE_FD, MODE_FILE, MODE_MEMORY, MODE_MMAP
from maxminddb.decoder import Decoder
from maxminddb.errors import InvalidDatabaseError
from maxminddb.file import FileBuffer
Expand All @@ -44,7 +44,9 @@ class Reader:
_ipv4_start: int

def __init__(
self, database: Union[AnyStr, int, PathLike, IO], mode: int = MODE_AUTO
self,
database: Union[AnyStr, int, PathLike, IO],
mode: int = MODE_AUTO,
) -> None:
"""Reader for the MaxMind DB file format
Expand All @@ -58,6 +60,7 @@ def __init__(
* MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default.
* MODE_FD - the param passed via database is a file descriptor, not
a path. This mode implies MODE_MEMORY.
"""
filename: Any
if (mode == MODE_AUTO and mmap) or mode == MODE_MMAP:
Expand All @@ -83,18 +86,19 @@ def __init__(
raise ValueError(
f"Unsupported open mode ({mode}). Only MODE_AUTO, MODE_FILE, "
"MODE_MEMORY and MODE_FD are supported by the pure Python "
"Reader"
"Reader",
)

metadata_start = self._buffer.rfind(
self._METADATA_START_MARKER, max(0, self._buffer_size - 128 * 1024)
self._METADATA_START_MARKER,
max(0, self._buffer_size - 128 * 1024),
)

if metadata_start == -1:
self.close()
raise InvalidDatabaseError(
f"Error opening database file ({filename}). "
"Is this a valid MaxMind DB file?"
"Is this a valid MaxMind DB file?",
)

metadata_start += len(self._METADATA_START_MARKER)
Expand All @@ -103,7 +107,7 @@ def __init__(

if not isinstance(metadata, dict):
raise InvalidDatabaseError(
f"Error reading metadata in database file ({filename})."
f"Error reading metadata in database file ({filename}).",
)

self._metadata = Metadata(**metadata) # pylint: disable=bad-option-value
Expand Down Expand Up @@ -134,21 +138,22 @@ def metadata(self) -> "Metadata":
def get(self, ip_address: Union[str, IPv6Address, IPv4Address]) -> Optional[Record]:
"""Return the record for the ip_address in the MaxMind DB
Arguments:
ip_address -- an IP address in the standard string notation
"""
(record, _) = self.get_with_prefix_len(ip_address)
return record

def get_with_prefix_len(
self, ip_address: Union[str, IPv6Address, IPv4Address]
self,
ip_address: Union[str, IPv6Address, IPv4Address],
) -> Tuple[Optional[Record], int]:
"""Return a tuple with the record and the associated prefix length
Arguments:
ip_address -- an IP address in the standard string notation
"""
if isinstance(ip_address, str):
address = ipaddress.ip_address(ip_address)
Expand All @@ -163,7 +168,7 @@ def get_with_prefix_len(
if address.version == 6 and self._metadata.ip_version == 4:
raise ValueError(
f"Error looking up {ip_address}. You attempted to look up "
"an IPv6 address in an IPv4-only database."
"an IPv6 address in an IPv4-only database.",
)

(pointer, prefix_len) = self._find_address_in_tree(packed_address)
Expand All @@ -187,7 +192,7 @@ def _generate_children(self, node, depth, ip_acc):
if ip_acc <= _IPV4_MAX_NUM and bits == 128:
depth -= 96
yield ipaddress.ip_network((ip_acc, depth)), self._resolve_data_pointer(
node
node,
)
elif node < node_count:
left = self._read_node(node, 0)
Expand Down
8 changes: 2 additions & 6 deletions maxminddb/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@


class RecordList(List[Record]): # pylint: disable=too-few-public-methods
"""
RecordList is a type for lists in a database record.
"""
"""RecordList is a type for lists in a database record."""


class RecordDict(Dict[str, Record]): # pylint: disable=too-few-public-methods
"""
RecordDict is a type for dicts in a database record.
"""
"""RecordDict is a type for dicts in a database record."""
Loading

0 comments on commit 62c7084

Please sign in to comment.