Skip to content

Commit 36cf50f

Browse files
committed
Create github workflow python.yml
1 parent 6f3c73c commit 36cf50f

File tree

6 files changed

+138
-29
lines changed

6 files changed

+138
-29
lines changed

.github/pull_request_template.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Which issue does this PR close?
2+
3+
<!--
4+
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
5+
-->
6+
7+
Closes #.
8+
9+
## Rationale for this change
10+
11+
<!--
12+
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
13+
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
14+
-->
15+
16+
## What changes are included in this PR?
17+
18+
<!--
19+
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
20+
-->
21+
22+
## Are these changes tested?
23+
24+
<!--
25+
We typically require tests for all PRs in order to:
26+
1. Prevent the code from being accidentally broken by subsequent changes
27+
2. Serve as another way to document the expected behavior of the code
28+
29+
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
30+
-->
31+
32+
## Are there any user-facing changes?
33+
34+
<!--
35+
If there are user-facing changes then we may require documentation to be updated before approving the PR.
36+
-->
37+
38+
<!--
39+
If there are any breaking changes to public APIs, please add the `api change` label.
40+
-->

.github/workflows/python.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Python application
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
pull_request:
10+
branches: [ "master" ]
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
build:
17+
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v4
22+
- name: Setup Go environment
23+
uses: actions/[email protected]
24+
with:
25+
go-version: '1.22'
26+
- name: Setup Java JDK
27+
uses: actions/[email protected]
28+
with:
29+
distribution: 'temurin'
30+
java-version: '22'
31+
cache: maven
32+
- name: Set up Python 3.10
33+
uses: actions/setup-python@v3
34+
with:
35+
python-version: "3.10"
36+
- name: Install dependencies
37+
run: |
38+
sudo add-apt-repository ppa:maxmind/ppa
39+
sudo apt update
40+
sudo apt install libmaxminddb0 libmaxminddb-dev
41+
python -m pip install --upgrade pip
42+
pip install netaddr maxminddb ruff pytest pytest-cov
43+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
44+
python -V
45+
- name: Check with ruff
46+
run: |
47+
ruff check
48+
ruff format --check
49+
- name: Test with pytest
50+
run: |
51+
pytest tests/*

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ When generating an MMDB file for use with the Java client, you must specify the
3737
```python
3838
from mmdb_writer import MMDBWriter
3939

40-
writer = MMDBWriter(int_type='int32')
40+
writer = MMDBWriter(int_type='i32')
4141
```
4242

4343
Alternatively, you can explicitly specify data types using the [Type Enforcement](#type-enforcement) section.

mmdb_writer.py

+30-14
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,26 @@ def __repr__(self):
134134

135135

136136
IntType = Union[
137-
Literal["auto", "u16", "u32", "u64", "u128", "i32"]
137+
Literal[
138+
"auto",
139+
"u16",
140+
"u32",
141+
"u64",
142+
"u128",
143+
"i32",
144+
"uint16",
145+
"uint32",
146+
"uint64",
147+
"uint128",
148+
"int32",
149+
]
138150
| MmdbU16
139151
| MmdbU32
140152
| MmdbU64
141153
| MmdbU128
142154
| MmdbI32
143155
]
144-
FloatType = Union[Literal["f32", "f64"] | MmdbF32 | MmdbF64]
156+
FloatType = Union[Literal["f32", "f64", "float32", "float64"] | MmdbF32 | MmdbF64]
145157

146158

147159
class Encoder(object):
@@ -320,21 +332,25 @@ def python_type_id(self, value):
320332
return MMDBTypeID.INT32
321333
else:
322334
return MMDBTypeID.UINT16
323-
elif self.int_type in ("u16", MmdbU16):
335+
elif self.int_type in ("u16", "uint16", MmdbU16):
324336
return MMDBTypeID.UINT16
325-
elif self.int_type in ("u32", MmdbU32):
337+
elif self.int_type in ("u32", "uint32", MmdbU32):
326338
return MMDBTypeID.UINT32
327-
elif self.int_type in ("u64", MmdbU64):
339+
elif self.int_type in ("u64", "uint64", MmdbU64):
328340
return MMDBTypeID.UINT64
329-
elif self.int_type in ("u128", MmdbU128):
341+
elif self.int_type in ("u128", "uint128", MmdbU128):
330342
return MMDBTypeID.UINT128
331-
elif self.int_type in ("i32", MmdbI32):
343+
elif self.int_type in ("i32", "int32", MmdbI32):
332344
return MMDBTypeID.INT32
345+
else:
346+
raise ValueError(f"unknown int_type={self.int_type}")
333347
elif value_type is float:
334-
if self.float_type in ("f32", MmdbF32):
348+
if self.float_type in ("f32", "float32", MmdbF32):
335349
return MMDBTypeID.FLOAT
336-
else:
350+
elif self.float_type in ("f64", "float64", MmdbF64):
337351
return MMDBTypeID.DOUBLE
352+
else:
353+
raise ValueError(f"unknown float_type={self.float_type}")
338354
elif value_type is Decimal:
339355
return MMDBTypeID.DOUBLE
340356
raise TypeError("unknown type {value_type}".format(value_type=value_type))
@@ -394,11 +410,11 @@ class TreeWriter(object):
394410
encoder_cls = Encoder
395411

396412
def __init__(
397-
self,
398-
tree: "SearchTreeNode",
399-
meta: dict,
400-
int_type: IntType = "auto",
401-
float_type: FloatType = "f64",
413+
self,
414+
tree: "SearchTreeNode",
415+
meta: dict,
416+
int_type: IntType = "auto",
417+
float_type: FloatType = "f64",
402418
):
403419
self._node_idx = {}
404420
self._leaf_offset = {}

tests/record.py

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
MmdbU32,
1212
MmdbU64,
1313
MmdbU128,
14-
MmdbBaseType,
1514
)
1615

1716

tests/test.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import maxminddb
99
from netaddr import IPSet
1010

11-
from mmdb_writer import MMDBWriter
12-
from tests.record import Record
11+
from mmdb_writer import MMDBWriter, MmdbI32, MmdbU16, MmdbU32, MmdbU64, MmdbU128
1312

1413
logging.basicConfig(
1514
format="[%(asctime)s: %(levelname)s] %(message)s", level=logging.INFO
@@ -71,20 +70,24 @@ def test_insert_subnet(self):
7170
m.close()
7271

7372
def test_int_type(self):
74-
value_range_map = {
75-
"i32": (-(2 ** 31), 2 ** 31 - 1),
76-
"u16": (0, 2 ** 16 - 1),
77-
"u32": (0, 2 ** 32 - 1),
78-
"u64": (0, 2 ** 64 - 1),
79-
"u128": (0, 2 ** 128 - 1),
80-
}
81-
for int_type in ("i32", "u16", "u32", "u64", "u128"):
73+
value_range_map = {}
74+
value_range_map.update(
75+
{k: (-(2**31), 2**31 - 1) for k in ("i32", "int32", MmdbI32)}
76+
)
77+
value_range_map.update({k: (0, 2**16 - 1) for k in ("u16", "uint16", MmdbU16)})
78+
value_range_map.update({k: (0, 2**32 - 1) for k in ("u32", "uint32", MmdbU32)})
79+
value_range_map.update({k: (0, 2**64 - 1) for k in ("u64", "uint64", MmdbU64)})
80+
value_range_map.update(
81+
{k: (0, 2**128 - 1) for k in ("u128", "uint128", MmdbU128)}
82+
)
83+
84+
for int_type, value_range in value_range_map.items():
8285
writer = MMDBWriter(int_type=int_type)
8386

84-
(start, end) = value_range_map[int_type]
87+
(start, end) = value_range
8588
ok_value = random.randint(start, end)
86-
bad_value1 = random.randint(end + 1, end + 2 ** 16)
87-
bad_value2 = random.randint(start - 2 ** 16, start - 1)
89+
bad_value1 = random.randint(end + 1, end + 2**16)
90+
bad_value2 = random.randint(start - 2**16, start - 1)
8891
writer.insert_network(IPSet(["1.0.0.0/8"]), {"value": ok_value})
8992
writer.to_db_file(self.filename)
9093
for bad_value in (bad_value1, bad_value2):

0 commit comments

Comments
 (0)