Skip to main content

A collection of utility functions

Project description

Rustils

Tests GitHub release (latest by date) Publish to PyPI

A collection of utility functions implemented in Rust with Python bindings, focusing on performance and reliability.

Features

String to Boolean Conversion

  • str_to_bool(s: str) -> bool: Convert string to boolean (Rust implementation)
  • py_str_to_bool(s: str) -> bool: Convert string to boolean (Python reference implementation)

Supported values:

  • True: "true", "1", "yes", "y", "on" (case-insensitive)
  • False: "false", "0", "no", "n", "off" (case-insensitive)

3D Point Distance Calculation

  • point3d_distance(p1: Tuple[float, float, float], p2: Tuple[float, float, float]) -> float: Calculate Euclidean distance between two 3D points (Rust implementation)
  • py_point3d_distance(p1: Tuple[float, float, float], p2: Tuple[float, float, float]) -> float: Calculate Euclidean distance between two 3D points (Python reference implementation)

Example:

from rustils import point3d_distance

# Calculate distance between two 3D points
distance = point3d_distance((0, 0, 0), (1, 1, 1))  # Returns sqrt(3)

Installation

Requirements

  • Python >= 3.9
  • Rust toolchain (for development)
  • Poetry (for dependency management)

Installing from Source

# Clone the repository
git clone https://github.com/tsonglew/rustils.git
cd rustils

# Install with poetry
poetry install

Usage

Python Interface

from rustils import str_to_bool, py_str_to_bool

# Using Rust implementation
result = str_to_bool("yes")  # Returns True
result = str_to_bool("0")    # Returns False

# Using Python implementation
result = py_str_to_bool("true")  # Returns True
result = py_str_to_bool("n")     # Returns False

# Invalid inputs raise ValueError
try:
    str_to_bool("invalid")
except ValueError as e:
    print(e)  # Cannot convert 'invalid' to bool

Performance Comparison

Benchmark results comparing Rust and Python implementations:

Single Value Operation

Name (time in ns)                Min      Max     Mean    StdDev   Median   OPS (Kops/s)
----------------------------------------------------------------------------------------
test_benchmark_single_python    123.75   11,469   138.41   77.46   135.00   7,224.79
test_benchmark_single_rust      500.00  631,292   635.66  2,730.91  625.00   1,573.17

Multiple Values Operation

Name (time in ns)            Min       Max      Mean     StdDev    Median   OPS (Kops/s)
----------------------------------------------------------------------------------------
test_benchmark_python       1,597.00  334,764  1,732.36  1,699.67  1,653.00    577.25
test_benchmark_rust         6,541.00  107,125  7,141.03  1,862.39  6,959.00    140.04

Note: For this simple operation, the Python implementation shows better performance due to the overhead of crossing the Python/Rust boundary. For more complex operations or larger datasets, the Rust implementation would likely show better performance.

Development

Common Commands

The project includes a Makefile for common development tasks:

# Install dependencies
make install

# Run tests
make test

# Run benchmarks
make benchmark

# Format code
make format

# Run linting
make lint

# Build package
make build

# Publish to PyPI
make publish

# Show all available commands
make help

Setting up Development Environment

# Install development dependencies
poetry install

# Activate virtual environment
poetry shell

Running Tests

# Run all tests
poetry run pytest

# Run only benchmark tests
poetry run pytest tests/test_benchmark.py -v --benchmark-only

Project Structure

rustils/
├── Cargo.toml          # Rust package configuration
├── pyproject.toml      # Python package configuration
├── rustils/
│   ├── __init__.py    # Python package initialization
│   └── py_utils.py    # Python implementation
├── src/
│   └── lib.rs         # Rust implementation
└── tests/
    ├── test_basic.py     # Basic functionality tests
    └── test_benchmark.py # Performance benchmark tests

License

MIT License - see the LICENSE file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rustils-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rustils-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file rustils-0.1.0.tar.gz.

File metadata

  • Download URL: rustils-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for rustils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5bbf878b4cf062633dec430c917dd4b01810a5594e8e3356b60d42b0808416da
MD5 4c84a968c0fe71ae191612b18bd2fead
BLAKE2b-256 2a6a06192feeef9d79dd78044a76f44f9e1075e4804024318ac3541a8ec04f47

See more details on using hashes here.

File details

Details for the file rustils-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: rustils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for rustils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f289cbb03d5cf80e815567ca675cacf819a74ffd60efb8a898616c207e765b6c
MD5 dc8785d68499c744b6a2091b0208db99
BLAKE2b-256 d27ad8a193c16533aafb3e73da711811c2cec4941f63eb32a0a5e36a576255dd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page