Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0.4rc1 typing annotations #85

Merged
merged 10 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# These are supported funding model platforms

github: [o-murphy] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
#github: [o-murphy] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
#patreon: o-murphy
open_collective: o-murphy # Replace with a single Open Collective username
ko_fi: o_murphy
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Mypy typing check

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -e .[dev,charts]
python -m pip install pandas-stubs matplotlib-stubs

- name: Analysing the code with mypy
run: |
mypy ./py_ballisticcalc
4 changes: 2 additions & 2 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
python -m pip install -e .[dev]

- name: Analysing the code with pylint
run: |
#pylint $(git ls-files '*.py')
pylint ./py_ballisticcalc
3 changes: 2 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
sudo apt-get install build-essential -y

python -m pip install --upgrade pip
python -m pip install setuptools pytest cython
python -m pip install setuptools cython
python -m pip install -e .[dev]

- name: Check Python version and install tomli if less than 3.11
run: |
Expand Down
21 changes: 12 additions & 9 deletions .github/workflows/python-publish-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ jobs:
sudo apt-get install build-essential -y

python -m pip install --upgrade pip
python -m pip install setuptools pytest cython
python -m pip install setuptools cython
python -m pip install -e .[dev]

- name: Run unittest tests in pure python mode
run: |
Expand Down Expand Up @@ -71,17 +72,18 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
python -m pip install -e .[dev]
python -m pip install twine

- name: Build package
run: python -m build

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

deploy-exts:
needs: build
Expand All @@ -103,17 +105,18 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
python -m pip install -e .[dev]
python -m pip install twine

- name: Build extensions package
run: |
cd py_ballisticcalc_exts
python -m build --outdir ../dist
cd ..

- name: Publish package to Test PyPI
- name: Publish package to PyPI
run: |
python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/* --skip-existing --verbose --non-interactive
python -m twine upload dist/* --skip-existing --verbose --non-interactive
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }}
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
31 changes: 16 additions & 15 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ jobs:
sudo apt-get install build-essential -y

python -m pip install --upgrade pip
python -m pip install setuptools pytest cython
python -m pip install setuptools cython
python -m pip install -e .[dev]

- name: Run unittest tests in pure python mode
run: |
Expand Down Expand Up @@ -82,17 +83,17 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
python -m pip install -e .[dev]
python -m pip install twine

- name: Build package
run: python -m build

- name: Publish package to PyPI
run: |
python -m twine upload dist/* --skip-existing --verbose --non-interactive
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
- name: Publish package to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}

deploy-exts:
needs: [build, deploy]
Expand All @@ -114,17 +115,17 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
python -m pip install -e .[dev]
python -m pip install twine

- 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
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
- name: Publish package to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
2 changes: 2 additions & 0 deletions Manifest.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ prune py_ballisticcalc_exts
prune tests
include py.typed
include Example.ipynb
include LICENSE
include README.md
92 changes: 92 additions & 0 deletions examples/kanik_50bmg_aid_shooting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Example of library usage"""
import math

from py_ballisticcalc import *


# set global library settings
PreferredUnits.velocity = Velocity.MPS
PreferredUnits.adjustment = Angular.Mil
PreferredUnits.temperature = Temperature.Celsius
PreferredUnits.distance = Distance.Meter
PreferredUnits.sight_height = Distance.Centimeter
PreferredUnits.drop = Distance.Centimeter


# set_global_use_powder_sensitivity(True) # enable muzzle velocity correction my powder temperature

# define params with default prefer_units
length = Distance.Inch(2.3)

weapon = Weapon(sight_height=Unit.Centimeter(9.5), twist=15)
dm = DragModel(0.62, TableG1, 661, 0.51)
ammo = Ammo(dm, 900)

zero_atmo = Atmo(
altitude=Unit.Meter(150),
pressure=Unit.hPa(1000),
temperature=Unit.Celsius(15),
humidity=50
)
zero = Shot(weapon=weapon, ammo=ammo, atmo=zero_atmo)
zero_distance = Distance.Meter(500)

calc = Calculator()
calc.set_weapon_zero(zero, zero_distance)

shot = Shot(look_angle=Unit.Degree(20), weapon=weapon, ammo=ammo, atmo=zero_atmo)
shot_result = calc.fire(shot, Distance.Meter(1000), Distance.Meter(100))

from pprint import pprint
fieldsss = TrajectoryData._fields

# for p in shot_result:
#
# # table = [{fieldsss[i]: it} for i, it in enumerate(p.in_def_units())]
# #
# # pprint(table)
# print(p.in_def_units(), ",")


def calculate_lead_angle(target_speed, target_size, bullet_speed, initial_distance, time_of_flight):
"""
Розрахунок кута упередження маючи швидкість і розмір цілі, швидкість кулі, початкову відстань до цілі.

:param target_speed: Швидкість цілі (м/с)
:param target_size: Довжина цілі (м)
:param bullet_speed: Швидкість кулі (м/с)
:param initial_distance: Початкова відстань до цілі (м)
:return: Кут упередження в градусах
"""
# Час польоту кулі
# time_of_flight = initial_distance / bullet_speed

# Відстань, яку пройде ніс цілі
# distance_nose_travel = target_speed * time_of_flight + target_size
distance_nose_travel = target_speed * time_of_flight + target_size

# Обчислення кута упередження
lead_angle = math.atan(distance_nose_travel / initial_distance)

# Перетворення кута в градуси
lead_angle_degrees = math.degrees(lead_angle)

return lead_angle_degrees

target_speed = 50
target_size = 3
max_lead_angle = 6.21


for p in shot_result:

table = [{fieldsss[i]: it} for i, it in enumerate(p.formatted())]

pprint(table)

# for p in shot_result[1:]:
# values = p.in_def_units()
# a = calculate_lead_angle(target_speed, target_size, values[2], values[1], values[0])
# # if a > max_lead_angle:
# # continue
# print(f"{a:.2f}°, {values[1]:.0f}m, {values[2]:.0f}m/s")
33 changes: 20 additions & 13 deletions py_ballisticcalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
__credits__ = ["o-murphy", "dbookstaber"]

import os
import sys
from typing import Dict, Union, Optional

from .backend import *
from .drag_tables import *
from .conditions import *
from .drag_model import *
from .drag_tables import *
from .interface import *
from .logger import logger
from .trajectory_data import *
from .conditions import *
from .munition import *
from .trajectory_data import *
from .unit import *
from typing import Dict, Union, Optional

try:
import tomllib
except ImportError:
if sys.version_info[:2] < (3, 11):
import tomli as tomllib
else:
import tomllib


def _load_config(filepath=None):

def find_pybc_toml(start_dir=os.getcwd()):
"""
Search for the pyproject.toml file starting from the specified directory.
Expand Down Expand Up @@ -94,7 +94,6 @@ def _basic_config(filename=None,
max_calc_step_size: Optional[Union[float, Distance]] = None,
use_powder_sensitivity: bool = False,
preferred_units: Optional[Dict[str, Unit]] = None):

"""
Method to load preferred units from file or Mapping
"""
Expand Down Expand Up @@ -140,7 +139,6 @@ def _basic_config(filename=None,
'Ammo',
'Sight',
'Unit',
'UnitType',
'UnitAliases',
'UnitAliasError',
'UnitTypeError',
Expand All @@ -156,9 +154,18 @@ def _basic_config(filename=None,
'Pressure',
'Energy',
'Weight',
'Dimension',
'PreferredUnits',
'get_drag_tables_names'
'get_drag_tables_names',
]

__all__ += ["TableG%s" % n for n in (1, 7, 2, 5, 6, 8, 'I', 'S')]
# __all__ += ["TableG%s" % n for n in (1, 7, 2, 5, 6, 8, 'I', 'S')]
__all__ += [
'TableG1',
'TableG7',
'TableG2',
'TableG5',
'TableG6',
'TableG8',
'TableGI',
'TableGS'
]
4 changes: 2 additions & 2 deletions py_ballisticcalc/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# try to use cython based backend
try:
from py_ballisticcalc_exts import (TrajectoryCalc,
from py_ballisticcalc_exts import (TrajectoryCalc, # type: ignore
get_global_max_calc_step_size,
get_global_use_powder_sensitivity,
set_global_max_calc_step_size,
Expand All @@ -14,7 +14,7 @@

logger.debug("Binary modules found, running in binary mode")
except ImportError as error:
from .trajectory_calc import (TrajectoryCalc,
from .trajectory_calc import (TrajectoryCalc, # type: ignore
get_global_max_calc_step_size,
get_global_use_powder_sensitivity,
set_global_max_calc_step_size,
Expand Down
Loading