Skip to content

Commit

Permalink
Cython optional (#29)
Browse files Browse the repository at this point in the history
* optional Cython dependency
* fixes for a workflows
* small import fixes
  • Loading branch information
o-murphy authored Oct 18, 2023
1 parent fc01f86 commit 0ec57a3
Show file tree
Hide file tree
Showing 36 changed files with 601 additions and 168 deletions.
25 changes: 19 additions & 6 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,32 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install build-essential -y
python -m pip install --upgrade pip
# python -m pip install flake8
python -m pip install cython
python -m pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install setuptools pytest cython
- name: Run unittest tests in pure python mode
run: |
if pytest tests --no-header --no-summary -v; then
echo "Pytest succeeded."
else
echo "Pytest failed, running without capture"
# Add your additional commands here
pytest tests --no-header --no-summary -v -s
fi
- name: Build cython modules
run: |
cd py_ballisticcalc_exts
python setup.py build_ext --inplace
cd ..
# python setup.py build_ext --inplace
- name: Run unittest tests
- name: Run unittest tests in binary mode
run: |
if pytest tests --no-header --no-summary -v; then
echo "Pytest succeeded."
Expand Down
17 changes: 6 additions & 11 deletions .github/workflows/python-publish-test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Upload Python Package to Test PyPI

on:
# release:
# types: [created, draft, edited]
workflow_dispatch:

permissions:
Expand Down Expand Up @@ -30,22 +28,19 @@ jobs:
- name: Build package
run: python -m build

- name: Build extensions package
run: |
cd py_ballisticcalc_exts
python -m build --outdir ../dist
cd ..
- name: Publish package to Test PyPI
run: |
python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/* --skip-existing --verbose --non-interactive
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}

# - name: Publish package to Test PyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# repository-url: https://test.pypi.org/legacy/
# user: __token__
# password: ${{ secrets.TEST_PYPI_API_TOKEN }}
# skip-existing: true
# verbose: true

- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ jobs:
- name: Build package
run: python -m build

- name: Build extensions package
run: |
cd py_ballisticcalc_exts
python -m build --outdir ../dist
cd ..
- name: Publish package to PyPI
run: |
python -m twine upload dist/* --skip-existing --verbose --non-interactive
Expand Down
5 changes: 2 additions & 3 deletions Manifest.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
recursive-include py_ballisticcalc *.pyx
recursive-include py_ballisticcalc *.pyi
include requirements*.txt
prune py_ballisticcalc_exts
prune tests
include py.typed
12 changes: 3 additions & 9 deletions py_ballisticcalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
__copyright__ = ("",)

__credits__ = ["o-murphy"]
__version__ = "1.1.0b9"
__version__ = "1.1.0"

import logging
try:
from .drag_model import * # pylint: disable=import-error
from .trajectory_calc import * # pylint: disable=import-error
except ImportError:
logging.warning("Package installed in --no-binary (pure python) mode, "
"use .whl packages to get better performance")
from .pure import *

from .backend import *
from .drag_tables import *
from .settings import *
from .multiple_bc import *
Expand Down
15 changes: 15 additions & 0 deletions py_ballisticcalc/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Searching for an available backends"""

from .logger import logger

# trying to use cython based backend
try:
from py_ballisticcalc_exts import * # pylint: disable=wildcard-import

logger.info("Binary modules found, running in binary mode")
except ImportError as error:
from .drag_model import *
from .trajectory_calc import *

logger.warning("Library running in pure python mode. "
"For better performance install 'py_ballisticcalc.exts' package")
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""definitions for a bullet's drag model calculation"""

import typing
from dataclasses import dataclass

import math

from ..settings import Settings as Set
from ..unit import Weight, Distance
from ..drag_tables import DragTablesSet
from .settings import Settings as Set
from .unit import Weight, Distance
from .drag_tables import DragTablesSet

__all__ = ('DragModel', )
__all__ = ('DragModel', 'make_data_points')


@dataclass
Expand Down
7 changes: 2 additions & 5 deletions py_ballisticcalc/example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"""Example of library usage"""


from py_ballisticcalc import Velocity, Temperature, Distance
from py_ballisticcalc import DragModel, TableG7
from py_ballisticcalc import Ammo, Atmo, Wind
from py_ballisticcalc import Weapon, Shot, Calculator
from py_ballisticcalc import *
from py_ballisticcalc import Settings as Set


Expand All @@ -14,7 +11,7 @@
# Set.Units.distance = Distance.Meter
Set.Units.sight_height = Distance.Centimeter

Set.set_max_calc_step_size(Distance.Foot(1))
# Set.set_max_calc_step_size(Distance.Foot(1))
Set.USE_POWDER_SENSITIVITY = True # enable muzzle velocity correction my powder temperature

# define params with default units
Expand Down
5 changes: 1 addition & 4 deletions py_ballisticcalc/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
from .conditions import Atmo, Shot
from .munition import Weapon, Ammo
# pylint: disable=import-error,no-name-in-module
try:
from .trajectory_calc import * # pylint: disable=import-error
except ImportError:
from .pure.py_trajectory_calc import *
from .backend import *
from .trajectory_data import HitResult
from .unit import Angular, Distance
from .settings import Settings
Expand Down
13 changes: 13 additions & 0 deletions py_ballisticcalc/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Default logger for py_ballisticcalc library"""

import logging

__all__ = ('logger',)

formatter = logging.Formatter("%(levelname)s:%(name)s:%(message)s")
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)

logger = logging.getLogger('py_balcalc')
logger.addHandler(console_handler)
logger.setLevel(logging.INFO)
5 changes: 1 addition & 4 deletions py_ballisticcalc/multiple_bc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

from .conditions import Atmo
# pylint: disable=import-error,no-name-in-module
try:
from .drag_model import make_data_points
except ImportError:
from .pure.py_drag_model import make_data_points
from .backend import make_data_points
from .settings import Settings as Set
from .unit import Distance, Weight, Velocity

Expand Down
8 changes: 1 addition & 7 deletions py_ballisticcalc/munition.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
"""Module for Weapon and Ammo properties definitions"""

import math
from dataclasses import dataclass, field

# pylint: disable=import-error,no-name-in-module
try:
from .drag_model import DragModel
except ImportError:
from .pure.py_drag_model import DragModel
from .settings import Settings as Set
from .unit import TypedUnits, Velocity, Temperature, Distance, Angular

Expand Down Expand Up @@ -44,7 +38,7 @@ def __post_init__(self):
class Ammo(TypedUnits):
"""Creates Ammo and Projectile properties"""

dm: DragModel
dm: 'DragModel'
length: [float, Distance] = field(default_factory=lambda: Set.Units.length)
mv: [float, Velocity] = field(default_factory=lambda: Set.Units.velocity)
temp_modifier: float = field(default=0)
Expand Down
4 changes: 0 additions & 4 deletions py_ballisticcalc/pure/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""pure python trajectory calculation backend"""

from dataclasses import dataclass
import math
from typing import NamedTuple

from ..conditions import Atmo, Shot, Wind
from ..munition import Ammo, Weapon
from ..settings import Settings
from ..trajectory_data import TrajectoryData, TrajFlag
from ..unit import Distance, Angular, Velocity, Weight, Energy, Pressure, Temperature
from .conditions import Atmo, Shot, Wind
from .munition import Ammo, Weapon
from .settings import Settings
from .trajectory_data import TrajectoryData, TrajFlag
from .unit import Distance, Angular, Velocity, Weight, Energy, Pressure, Temperature

__all__ = ('TrajectoryCalc', )

Expand Down Expand Up @@ -441,7 +443,10 @@ def calculate_curve(data_points):
curve.append(curve_point)
return curve


def calculate_by_curve(data: list, curve: list, mach: float):
"""returning the calculated drag for a
specified mach based on previously calculated data"""
# num_points, mlo, mhi, mid
# cdef CurvePoint curve_m

Expand Down
4 changes: 1 addition & 3 deletions py_ballisticcalc/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

__all__ = ('Unit', 'AbstractUnit', 'UnitPropsDict', 'Distance',
'Velocity', 'Angular', 'Temperature', 'Pressure',
'Energy', 'Weight',
# 'is_unit',
'TypedUnits')
'Energy', 'Weight', 'TypedUnits')


class Unit(IntEnum):
Expand Down
Loading

0 comments on commit 0ec57a3

Please sign in to comment.