Skip to content

Commit 62c7084

Browse files
committed
Run ruff's auto-fixer
1 parent ea11c45 commit 62c7084

File tree

11 files changed

+133
-97
lines changed

11 files changed

+133
-97
lines changed

docs/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
#
43
# maxminddb documentation build configuration file, created by
54
# sphinx-quickstart on Tue Apr 9 13:34:57 2013.
@@ -12,8 +11,8 @@
1211
# All configuration values have a default; values that are commented out
1312
# serve to show the default.
1413

15-
import sys
1614
import os
15+
import sys
1716

1817
sys.path.insert(0, os.path.abspath(".."))
1918
import maxminddb

examples/benchmark.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/python
2-
# -*- coding: utf-8 -*-
32

43
import argparse
5-
import maxminddb
64
import random
75
import socket
86
import struct
97
import timeit
108

9+
import maxminddb
10+
1111
parser = argparse.ArgumentParser(description="Benchmark maxminddb.")
1212
parser.add_argument("--count", default=250000, type=int, help="number of lookups")
1313
parser.add_argument("--mode", default=0, type=int, help="reader mode to use")
@@ -30,4 +30,4 @@ def lookup_ip_address():
3030
number=args.count,
3131
)
3232

33-
print("{:,}".format(int(args.count / elapsed)), "lookups per second")
33+
print(f"{int(args.count / elapsed):,}", "lookups per second")

maxminddb/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222

2323
__all__ = [
24-
"InvalidDatabaseError",
2524
"MODE_AUTO",
2625
"MODE_FD",
2726
"MODE_FILE",
2827
"MODE_MEMORY",
2928
"MODE_MMAP",
3029
"MODE_MMAP_EXT",
30+
"InvalidDatabaseError",
3131
"Reader",
3232
"open_database",
3333
]
@@ -51,6 +51,7 @@ def open_database(
5151
a path. This mode implies MODE_MEMORY.
5252
* MODE_AUTO - tries MODE_MMAP_EXT, MODE_MMAP, MODE_FILE in that
5353
order. Default mode.
54+
5455
"""
5556
if mode not in (
5657
MODE_AUTO,
@@ -70,7 +71,7 @@ def open_database(
7071

7172
if not has_extension:
7273
raise ValueError(
73-
"MODE_MMAP_EXT requires the maxminddb.extension module to be available"
74+
"MODE_MMAP_EXT requires the maxminddb.extension module to be available",
7475
)
7576

7677
# The C type exposes the same API as the Python Reader, so for type

maxminddb/decoder.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
import struct
10-
from typing import cast, Dict, List, Tuple, Union
10+
from typing import Dict, List, Tuple, Union, cast
1111

1212
try:
1313
# pylint: disable=unused-import
@@ -37,6 +37,7 @@ def __init__(
3737
database_buffer -- an mmap'd MaxMind DB file.
3838
pointer_base -- the base number to use when decoding a pointer
3939
pointer_test -- used for internal unit testing of pointer code
40+
4041
"""
4142
self._pointer_test = pointer_test
4243
self._buffer = database_buffer
@@ -142,6 +143,7 @@ def decode(self, offset: int) -> Tuple[Record, int]:
142143
143144
Arguments:
144145
offset -- the location of the data structure to decode
146+
145147
"""
146148
new_offset = offset + 1
147149
ctrl_byte = self._buffer[offset]
@@ -154,7 +156,7 @@ def decode(self, offset: int) -> Tuple[Record, int]:
154156
decoder = self._type_decoder[type_num]
155157
except KeyError as ex:
156158
raise InvalidDatabaseError(
157-
f"Unexpected type number ({type_num}) encountered"
159+
f"Unexpected type number ({type_num}) encountered",
158160
) from ex
159161

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

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

181183
def _size_from_ctrl_byte(
182-
self, ctrl_byte: int, offset: int, type_num: int
184+
self,
185+
ctrl_byte: int,
186+
offset: int,
187+
type_num: int,
183188
) -> Tuple[int, int]:
184189
size = ctrl_byte & 0x1F
185190
if type_num == 1 or size < 29:

maxminddb/extension.pyi

+14-10
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@ This module contains the C extension database reader and related classes.
66
77
"""
88

9+
# pylint: disable=E0601,E0602
910
from ipaddress import IPv4Address, IPv6Address
1011
from os import PathLike
11-
from typing import Any, AnyStr, Dict, IO, List, Optional, Tuple, Union
12-
from maxminddb import MODE_AUTO
12+
from typing import IO, Any, AnyStr, Dict, List, Optional, Tuple, Union
13+
1314
from maxminddb.types import Record
1415

1516
class Reader:
16-
"""
17-
A C extension implementation of a reader for the MaxMind DB format. IP
17+
"""A C extension implementation of a reader for the MaxMind DB format. IP
1818
addresses can be looked up using the ``get`` method.
1919
"""
2020

2121
closed: bool = ...
2222

2323
def __init__(
24-
self, database: Union[AnyStr, int, PathLike, IO], mode: int = MODE_AUTO
24+
self,
25+
database: Union[AnyStr, int, PathLike, IO],
26+
mode: int = ...,
2527
) -> None:
2628
"""Reader for the MaxMind DB file format
2729
@@ -30,6 +32,7 @@ class Reader:
3032
file, or a file descriptor in the case of MODE_FD.
3133
mode -- mode to open the database with. The only supported modes are
3234
MODE_AUTO and MODE_MMAP_EXT.
35+
3336
"""
3437

3538
def close(self) -> None:
@@ -38,25 +41,26 @@ class Reader:
3841
def get(self, ip_address: Union[str, IPv6Address, IPv4Address]) -> Optional[Record]:
3942
"""Return the record for the ip_address in the MaxMind DB
4043
41-
4244
Arguments:
4345
ip_address -- an IP address in the standard string notation
46+
4447
"""
4548

4649
def get_with_prefix_len(
47-
self, ip_address: Union[str, IPv6Address, IPv4Address]
50+
self,
51+
ip_address: Union[str, IPv6Address, IPv4Address],
4852
) -> Tuple[Optional[Record], int]:
4953
"""Return a tuple with the record and the associated prefix length
5054
51-
5255
Arguments:
5356
ip_address -- an IP address in the standard string notation
57+
5458
"""
5559

56-
def metadata(self) -> "Metadata":
60+
def metadata(self) -> Metadata:
5761
"""Return the metadata associated with the MaxMind DB file"""
5862

59-
def __enter__(self) -> "Reader": ...
63+
def __enter__(self) -> Reader: ...
6064
def __exit__(self, *args) -> None: ...
6165

6266
# pylint: disable=too-few-public-methods

maxminddb/file.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ def close(self) -> None:
4545
if hasattr(os, "pread"):
4646

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

5252
else:
5353

5454
def _read(self, buffersize: int, offset: int) -> bytes:
55-
"""read with a lock
55+
"""Read with a lock
5656
5757
This lock is necessary as after a fork, the different processes
5858
will share the same file table entry, even if we dup the fd, and

maxminddb/reader.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
import struct
1717
from ipaddress import IPv4Address, IPv6Address
1818
from os import PathLike
19-
from typing import Any, AnyStr, Dict, IO, List, Optional, Tuple, Union
19+
from typing import IO, Any, AnyStr, Dict, List, Optional, Tuple, Union
2020

21-
from maxminddb.const import MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY, MODE_FD
21+
from maxminddb.const import MODE_AUTO, MODE_FD, MODE_FILE, MODE_MEMORY, MODE_MMAP
2222
from maxminddb.decoder import Decoder
2323
from maxminddb.errors import InvalidDatabaseError
2424
from maxminddb.file import FileBuffer
@@ -44,7 +44,9 @@ class Reader:
4444
_ipv4_start: int
4545

4646
def __init__(
47-
self, database: Union[AnyStr, int, PathLike, IO], mode: int = MODE_AUTO
47+
self,
48+
database: Union[AnyStr, int, PathLike, IO],
49+
mode: int = MODE_AUTO,
4850
) -> None:
4951
"""Reader for the MaxMind DB file format
5052
@@ -58,6 +60,7 @@ def __init__(
5860
* MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default.
5961
* MODE_FD - the param passed via database is a file descriptor, not
6062
a path. This mode implies MODE_MEMORY.
63+
6164
"""
6265
filename: Any
6366
if (mode == MODE_AUTO and mmap) or mode == MODE_MMAP:
@@ -83,18 +86,19 @@ def __init__(
8386
raise ValueError(
8487
f"Unsupported open mode ({mode}). Only MODE_AUTO, MODE_FILE, "
8588
"MODE_MEMORY and MODE_FD are supported by the pure Python "
86-
"Reader"
89+
"Reader",
8790
)
8891

8992
metadata_start = self._buffer.rfind(
90-
self._METADATA_START_MARKER, max(0, self._buffer_size - 128 * 1024)
93+
self._METADATA_START_MARKER,
94+
max(0, self._buffer_size - 128 * 1024),
9195
)
9296

9397
if metadata_start == -1:
9498
self.close()
9599
raise InvalidDatabaseError(
96100
f"Error opening database file ({filename}). "
97-
"Is this a valid MaxMind DB file?"
101+
"Is this a valid MaxMind DB file?",
98102
)
99103

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

104108
if not isinstance(metadata, dict):
105109
raise InvalidDatabaseError(
106-
f"Error reading metadata in database file ({filename})."
110+
f"Error reading metadata in database file ({filename}).",
107111
)
108112

109113
self._metadata = Metadata(**metadata) # pylint: disable=bad-option-value
@@ -134,21 +138,22 @@ def metadata(self) -> "Metadata":
134138
def get(self, ip_address: Union[str, IPv6Address, IPv4Address]) -> Optional[Record]:
135139
"""Return the record for the ip_address in the MaxMind DB
136140
137-
138141
Arguments:
139142
ip_address -- an IP address in the standard string notation
143+
140144
"""
141145
(record, _) = self.get_with_prefix_len(ip_address)
142146
return record
143147

144148
def get_with_prefix_len(
145-
self, ip_address: Union[str, IPv6Address, IPv4Address]
149+
self,
150+
ip_address: Union[str, IPv6Address, IPv4Address],
146151
) -> Tuple[Optional[Record], int]:
147152
"""Return a tuple with the record and the associated prefix length
148153
149-
150154
Arguments:
151155
ip_address -- an IP address in the standard string notation
156+
152157
"""
153158
if isinstance(ip_address, str):
154159
address = ipaddress.ip_address(ip_address)
@@ -163,7 +168,7 @@ def get_with_prefix_len(
163168
if address.version == 6 and self._metadata.ip_version == 4:
164169
raise ValueError(
165170
f"Error looking up {ip_address}. You attempted to look up "
166-
"an IPv6 address in an IPv4-only database."
171+
"an IPv6 address in an IPv4-only database.",
167172
)
168173

169174
(pointer, prefix_len) = self._find_address_in_tree(packed_address)
@@ -187,7 +192,7 @@ def _generate_children(self, node, depth, ip_acc):
187192
if ip_acc <= _IPV4_MAX_NUM and bits == 128:
188193
depth -= 96
189194
yield ipaddress.ip_network((ip_acc, depth)), self._resolve_data_pointer(
190-
node
195+
node,
191196
)
192197
elif node < node_count:
193198
left = self._read_node(node, 0)

maxminddb/types.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@
1212

1313

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

1917

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

0 commit comments

Comments
 (0)