Skip to content

Commit

Permalink
Replace Cython with pure Python (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmouchet authored Oct 27, 2023
1 parent 2496322 commit 3892e56
Show file tree
Hide file tree
Showing 16 changed files with 903 additions and 958 deletions.
6 changes: 3 additions & 3 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ commit = True
tag = True
message = release(project): {current_version} → {new_version}

[bumpversion:file:setup.py]
search = version="{current_version}"
replace = version="{new_version}"
[bumpversion:file:pyproject.toml]
search = version = "{current_version}"
replace = version = "{new_version}"

[bumpversion:file:diamond_miner/__init__.py]
37 changes: 9 additions & 28 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: "3.x"
- uses: dioptra-io/setup-poetry-action@v1
- name: Install package
run: |
python -m venv venv
./venv/bin/pip install -e .[dev]
run: poetry install
- name: Insert test data
run: ./venv/bin/python tests/data/insert.py
run: poetry run python tests/data/insert.py
- name: Run tests
run: ./venv/bin/pytest --cov=diamond_miner --cov-report=xml
run: poetry run pytest --cov=diamond_miner --cov-report=xml
- uses: codecov/codecov-action@v3

mkdocs:
Expand All @@ -31,42 +30,24 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: "3.x"
- uses: dioptra-io/setup-poetry-action@v1
- name: Install package
run: |
python -m venv venv
./venv/bin/pip install -e .[dev]
run: poetry install
- name: Build documentation
run: ./venv/bin/mkdocs build --strict
run: poetry run mkdocs build --strict
- name: Publish documentation
run: ./venv/bin/mkdocs gh-deploy --force --no-history --strict
run: poetry run mkdocs gh-deploy --force --no-history --strict
if: ${{ startsWith(github.ref, 'refs/tags/v') }}

pypi:
needs: [test]
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.x"
- name: Set up QEMU
if: runner.os == 'Linux'
uses: docker/setup-qemu-action@v2
with:
platforms: all
- name: Build wheels
uses: pypa/[email protected]
with:
output-dir: dist
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
path: dist/*.whl
- uses: dioptra-io/publish-python-action@v1
with:
password: ${{ secrets.PYPI_TOKEN }}
upload: ${{ startsWith(github.ref, 'refs/tags/v') }}
wheel: false
2 changes: 0 additions & 2 deletions MANIFEST.in

This file was deleted.

24 changes: 24 additions & 0 deletions diamond_miner/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from ipaddress import IPv6Address


def format_probe(
dst_addr_v6: int, src_port: int, dst_port: int, ttl: int, protocol: str
) -> str:
"""
Create a Caracal probe string.
Examples:
>>> from diamond_miner.format import format_probe
>>> format_probe(281470816487432, 24000, 33434, 1, "icmp")
'::ffff:808:808,24000,33434,1,icmp'
"""
return f"{format_ipv6(dst_addr_v6)},{src_port},{dst_port},{ttl},{protocol}"


def format_ipv6(addr: int) -> str:
"""
Convert an IPv6 UInt128 to a string.
>>> from diamond_miner.format import format_ipv6
>>> format_ipv6(281470816487432)
'::ffff:808:808'
"""
return str(IPv6Address(addr))
4 changes: 0 additions & 4 deletions diamond_miner/format.pyi

This file was deleted.

48 changes: 0 additions & 48 deletions diamond_miner/format.pyx

This file was deleted.

28 changes: 14 additions & 14 deletions diamond_miner/grid.pyx → diamond_miner/grid.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
from collections.abc import Iterator, Sequence
from random import randint
from typing import Any

from pygfc import Permutation


# The Cython version is approx. 2x faster on a M1 CPU.
cdef class ParameterGrid:
cdef tuple parameters
cdef tuple size_
cdef len

def __init__(self, *parameters):
class ParameterGrid:
def __init__(self, *parameters: Sequence):
self.parameters = parameters
self.size_ = tuple(len(p) for p in self.parameters)
self.len = 1
for dim in self.size_:
self.len *= dim

def __getitem__(self, index):
def __getitem__(self, index: Sequence[int] | int) -> Sequence[Any]:
if isinstance(index, int):
if index >= len(self):
raise IndexError("index out of range")
index = self.linear_to_subscript(index)
return [p[i] for p, i in zip(self.parameters, index)]

def __iter__(self):
def __iter__(self) -> Iterator[Sequence[Any]]:
return (self[index] for index in range(len(self)))

def __len__(self):
def __len__(self) -> int:
return self.len

@property
def size(self):
def size(self) -> Sequence[int]:
return self.size_

def shuffled(self, int rounds = 6, seed = None):
seed = seed or randint(0, 2 ** 64 - 1)
def shuffled(
self, rounds: int = 6, seed: int | None = None
) -> Iterator[Sequence[Any]]:
seed = seed or randint(0, 2**64 - 1)
perm = Permutation(len(self), rounds, seed)
return (self[index] for index in perm)

cdef list linear_to_subscript(self, index):
cdef list coordinates = []
def linear_to_subscript(self, index: int) -> Sequence[int]:
coordinates = []
for dim in self.size_:
index, coordinate = divmod(index, dim)
coordinates.append(coordinate)
Expand Down
13 changes: 0 additions & 13 deletions diamond_miner/grid.pyi

This file was deleted.

Loading

0 comments on commit 3892e56

Please sign in to comment.