LuxTools
A collection of tools and utilities that I often use in my work.
Features
- Scientific
- Error propagation for numerical calculations
- Pretty printing of numerical results with uncertainties
- Functional
- Function composition utilities
- Partial function application
- Overload function definitions
Installation
pip install luxtools
Usage
Scientific
Error Propagation
import torch
from luxtools import get_error
# Create tensors with uncertainties
x = torch.tensor([1.0, 3.0], dtype=torch.float32, requires_grad=True)
x.sigma = torch.tensor([0.1, 0.2], dtype=torch.float32)
y = torch.tensor([2.0, 4.0], dtype=torch.float32, requires_grad=True)
y = Variable(y, torch.tensor([0.2, 0.3]))
# Perform calculations
f = x * y
# Get propagated error
error = get_error(f)
# tensor([0.2828, 1.2042])
Numeric Result Formatting
from luxtools import NumericResult
# Create a measurement with uncertainty
result = NumericResult(1.234, 0.193)
print(result)
# (1.2 ± 0.2)
# Scientific notation
result = NumericResult(234.23424, 10)
print(result)
# (2.3 ± 0.1)*10^(2)
# LaTeX output
print(result.latex())
# (2.3 \pm 0.1)\cdot 10^{2}
Functional
Function Composition
from luxtools import chain
# Compose functions
f = lambda x: x + 1
g = lambda x: x * 2
h = lambda x: x ** 2
# Create a new function that applies f, then g, then h
composed = chain(f, g, h)
result = composed(3) # ((3 + 1) * 2) ** 2 = 64
Partial Application
Allows you to partially apply arguments to a function, creating a new function with fewer arguments. See article for discussion.
from luxtools import partial
@partial
def greet(greeting, name):
return f"{greeting}, {name}!"
# Create a new function with 'Hello' fixed as the greeting
say_hello = greet("Hello")
result = say_hello("World") # "Hello, World!"
Overload function definitions
Allows you to have multiple function definitions for the same function name. It uses typehints to determine which function to call. See article for discussion.
from luxtools import overload
class Email:
def __init__(self, email: str):
self.email = email
def __str__(self) -> str:
return self.email
class PhoneNumber:
def __init__(self, phone_number: str):
self.phone_number = phone_number
def __str__(self) -> str:
return self.phone_number
@overload
def get_user(email: Email):
print("Email:", email)
return email
@overload
def get_user(phone_number: PhoneNumber):
print("Phone:", phone_number)
return phone_number
get_user(Email("test@example.com")) # prints: Email: test@example.com
get_user(PhoneNumber("123-456-789")) # prints: Phone: 123-456-789
Caveat, if the function is defined in a non-global scope such as a class, or inside a function, then you need to pass the local scope to the decorator.
def local_scope():
@overload(scope=locals())
def get_user(email: Email):
print("Email:", email)
@overload(scope=locals())
def get_user(phone_number: PhoneNumber):
print("Phone:", phone_number)
This is necessary because the parent stack frame doesn't exist inside the overload function, so it has to be passed explicitly. See tests.
Metadata
Release files for luxtools 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| luxtools-0.1.1.tar.gz | 19.5 kB | Without provenance | Details for luxtools-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| luxtools-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for luxtools-0.1.1-py3-none-any.whl |
Total release size: 28.3 kB
Release files / luxtools-0.1.1.tar.gz
| Download URL | luxtools-0.1.1.tar.gz |
|---|---|
| Size | 19.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6716cb2106e7fe58e6f6c33ed72ed98fb1138077c808f975726f012f694c47af
|
|
BLAKE2b-256 checksum How to use checksums |
ed923fa60675434708c65442107180376aed70531af7c32d3c8631b2f470a116
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.5
|
Release files / luxtools-0.1.1-py3-none-any.whl
| Download URL | luxtools-0.1.1-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
30fd8d869098739a165943dd63eb5f668d9f05e516f7cf9cdf8d63a9745472c8
|
|
BLAKE2b-256 checksum How to use checksums |
a82514e2e13ab36d0427562165af393ec12e232118aea44f5d237426bfd91a93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.5
|
Security for luxtools 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.