Skip to main content

Add your description here

Project description

LuxTools

A collection of tools and utilities that I often use in my work.

Features

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.

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

luxtools-0.1.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

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

luxtools-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: luxtools-0.1.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.5

File hashes

Hashes for luxtools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1c8b1efb494dc55b39ed732737014fd2321400838611dbfce2bfe4d7afbdd0b2
MD5 7f3bb9dfd50251f90e06f6d9b90b16a7
BLAKE2b-256 368e2917eccaf7fa53d805a2c98066e50a6ece0500ec67c40fa6e330373001b3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: luxtools-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.5

File hashes

Hashes for luxtools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 520833d9470773f460ad80a4605ff75e51b5efe68f38cc9c09d10031860c9275
MD5 0061847d2a1a2b1fd29af17d8c32a9a1
BLAKE2b-256 8a0a5583d3854e71c512f61eb44c0b2000ce06ebbbc89fa9773bcac9a589ce17

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