Skip to content

Commit

Permalink
Create github workflow python.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
vimt committed Jun 1, 2024
1 parent 6f3c73c commit c8b5e65
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 30 deletions.
40 changes: 40 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Which issue does this PR close?

<!--
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.
-->

Closes #.

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->

## What changes are included in this PR?

<!--
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.
-->

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
51 changes: 51 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup Go environment
uses: actions/[email protected]
with:
go-version: '1.22'
- name: Setup Java JDK
uses: actions/[email protected]
with:
distribution: 'temurin'
java-version: '22'
cache: maven
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
sudo add-apt-repository ppa:maxmind/ppa
sudo apt update
sudo apt install libmaxminddb0 libmaxminddb-dev
python -m pip install --upgrade pip
pip install ".[test]" ruff pytest-cov
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -V
- name: Check with ruff
run: |
ruff check
ruff format --check
- name: Test with pytest
run: |
pytest tests/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ When generating an MMDB file for use with the Java client, you must specify the
```python
from mmdb_writer import MMDBWriter

writer = MMDBWriter(int_type='int32')
writer = MMDBWriter(int_type='i32')
```

Alternatively, you can explicitly specify data types using the [Type Enforcement](#type-enforcement) section.
Expand Down
44 changes: 30 additions & 14 deletions mmdb_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,26 @@ def __repr__(self):


IntType = Union[
Literal["auto", "u16", "u32", "u64", "u128", "i32"]
Literal[
"auto",
"u16",
"u32",
"u64",
"u128",
"i32",
"uint16",
"uint32",
"uint64",
"uint128",
"int32",
]
| MmdbU16
| MmdbU32
| MmdbU64
| MmdbU128
| MmdbI32
]
FloatType = Union[Literal["f32", "f64"] | MmdbF32 | MmdbF64]
FloatType = Union[Literal["f32", "f64", "float32", "float64"] | MmdbF32 | MmdbF64]


class Encoder(object):
Expand Down Expand Up @@ -320,21 +332,25 @@ def python_type_id(self, value):
return MMDBTypeID.INT32
else:
return MMDBTypeID.UINT16
elif self.int_type in ("u16", MmdbU16):
elif self.int_type in ("u16", "uint16", MmdbU16):
return MMDBTypeID.UINT16
elif self.int_type in ("u32", MmdbU32):
elif self.int_type in ("u32", "uint32", MmdbU32):
return MMDBTypeID.UINT32
elif self.int_type in ("u64", MmdbU64):
elif self.int_type in ("u64", "uint64", MmdbU64):
return MMDBTypeID.UINT64
elif self.int_type in ("u128", MmdbU128):
elif self.int_type in ("u128", "uint128", MmdbU128):
return MMDBTypeID.UINT128
elif self.int_type in ("i32", MmdbI32):
elif self.int_type in ("i32", "int32", MmdbI32):
return MMDBTypeID.INT32
else:
raise ValueError(f"unknown int_type={self.int_type}")
elif value_type is float:
if self.float_type in ("f32", MmdbF32):
if self.float_type in ("f32", "float32", MmdbF32):
return MMDBTypeID.FLOAT
else:
elif self.float_type in ("f64", "float64", MmdbF64):
return MMDBTypeID.DOUBLE
else:
raise ValueError(f"unknown float_type={self.float_type}")
elif value_type is Decimal:
return MMDBTypeID.DOUBLE
raise TypeError("unknown type {value_type}".format(value_type=value_type))
Expand Down Expand Up @@ -394,11 +410,11 @@ class TreeWriter(object):
encoder_cls = Encoder

def __init__(
self,
tree: "SearchTreeNode",
meta: dict,
int_type: IntType = "auto",
float_type: FloatType = "f64",
self,
tree: "SearchTreeNode",
meta: dict,
int_type: IntType = "auto",
float_type: FloatType = "f64",
):
self._node_idx = {}
self._leaf_offset = {}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_version(file):
zip_safe=False,
python_requires=">=3.6",
install_requires=["netaddr>=0.7"],
tests_require=["maxminddb>=1.5", "numpy"],
tests_require=["maxminddb>=1.5", "numpy", "pytest"],
classifiers=[
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python",
Expand Down
1 change: 0 additions & 1 deletion tests/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
MmdbU32,
MmdbU64,
MmdbU128,
MmdbBaseType,
)


Expand Down
29 changes: 16 additions & 13 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import maxminddb
from netaddr import IPSet

from mmdb_writer import MMDBWriter
from tests.record import Record
from mmdb_writer import MMDBWriter, MmdbI32, MmdbU16, MmdbU32, MmdbU64, MmdbU128

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

def test_int_type(self):
value_range_map = {
"i32": (-(2 ** 31), 2 ** 31 - 1),
"u16": (0, 2 ** 16 - 1),
"u32": (0, 2 ** 32 - 1),
"u64": (0, 2 ** 64 - 1),
"u128": (0, 2 ** 128 - 1),
}
for int_type in ("i32", "u16", "u32", "u64", "u128"):
value_range_map = {}
value_range_map.update(
{k: (-(2**31), 2**31 - 1) for k in ("i32", "int32", MmdbI32)}
)
value_range_map.update({k: (0, 2**16 - 1) for k in ("u16", "uint16", MmdbU16)})
value_range_map.update({k: (0, 2**32 - 1) for k in ("u32", "uint32", MmdbU32)})
value_range_map.update({k: (0, 2**64 - 1) for k in ("u64", "uint64", MmdbU64)})
value_range_map.update(
{k: (0, 2**128 - 1) for k in ("u128", "uint128", MmdbU128)}
)

for int_type, value_range in value_range_map.items():
writer = MMDBWriter(int_type=int_type)

(start, end) = value_range_map[int_type]
(start, end) = value_range
ok_value = random.randint(start, end)
bad_value1 = random.randint(end + 1, end + 2 ** 16)
bad_value2 = random.randint(start - 2 ** 16, start - 1)
bad_value1 = random.randint(end + 1, end + 2**16)
bad_value2 = random.randint(start - 2**16, start - 1)
writer.insert_network(IPSet(["1.0.0.0/8"]), {"value": ok_value})
writer.to_db_file(self.filename)
for bad_value in (bad_value1, bad_value2):
Expand Down

0 comments on commit c8b5e65

Please sign in to comment.