Skip to main content

A functional utility box with various functions

Project description

FuncBox

FuncBox is a streamlined Python utility library designed to provide essential utilities.

Installation

To install FuncBox, use pip:

pip install -U funcbox

Usage

Import FuncBox into your Python project to access its functions:

from funcbox import *

Available Functions

is_prime(n: int) -> bool

Efficiently check if a number is prime. Only checking potential divisors of form 6k±1 up to sqrt(n)

  • Parameters:
    • n: The number to check for primality
  • Returns:
    • bool: True if the number is prime, False otherwise
  • Examples:
    print(is_prime(7))  # Output: True
    print(is_prime(10))  # Output: False
    

fibonacci(n: int, type="int") -> Union[int, List[int]]

Calculate Fibonacci numbers efficiently.

  • Parameters:
    • n: The index of Fibonacci number to calculate (0-indexed) or count of numbers for list
    • type: Output format - 'int' for single value or 'list' for sequence. Defaults to "int".
  • Returns:
    • Union[int, List[int]]: Either the nth Fibonacci number or a list of n Fibonacci numbers
  • Raises:
    • ValueError: If n is negative or type is not 'int' or 'list'
  • Examples:
    print(fibonacci(0))  # Output: 0
    print(fibonacci(5))  # Output: 5
    print(fibonacci(5, "list"))  # Output: [0, 1, 1, 2, 3]
    

get_factors(num: int) -> List[int]

Get all factors of a number, excluding the number itself.

  • Parameters:
    • num: The number to find factors for.
  • Returns:
    • List[int]: A sorted list of all factors of the number (excluding the number itself).
  • Examples:
    print(get_factors(12))  # Output: [1, 2, 3, 4, 6]
    print(get_factors(7))   # Output: [1]
    

dijkstra(graph: dict, start_node: Any, end_node: Any = None) -> dict

Compute Dijkstra's shortest path algorithm to find the shortest paths from a start node to all other nodes in a graph, or to a specific end node if specified.

  • Parameters:
    • graph: A graph represented as an adjacency list, where keys are nodes and values are dictionaries mapping neighbors to edge weights.
    • start_node: The node to start the pathfinding from.
    • end_node: (Optional) If specified, the algorithm will terminate early once the shortest path to this node is found. Defaults to None.
  • Returns:
    • dict: A dictionary containing two dictionaries:
      • 'distances': Shortest distances from the start node to each node.
      • 'paths': Shortest paths from the start node to each node. Nodes not reachable from the start node will have a distance of infinity and path as None.
  • Examples:
    graph = {
        'A': {'B': 4, 'C': 2},
        'B': {'D': 5, 'E': 1},
        'C': {'B': 1, 'E': 3},
        'D': {'F': 2},
        'E': {'D': 1, 'F': 4},
        'F': {}
    }
    result = dijkstra(graph, 'A')
    print(result['distances'])  # Output distances from A to all nodes
    print(result['paths'])      # Output paths from A to all nodes
    
    # Using end_node parameter
    result = dijkstra(graph, 'A', 'F')
    print(result['distances'])  # Output distances for nodes processed
    print(result['paths'])      # Output paths for nodes processed
    

primes(start: int = 2, limit: int = None) -> List[int]

Efficiently generate all prime numbers between start and limit using the Sieve of Eratosthenes algorithm.

  • Parameters:
    • start: The lower bound for finding prime numbers (inclusive). Defaults to 2.
    • limit: The upper bound for finding prime numbers (inclusive).
  • Returns:
    • List[int]: A list of all prime numbers from start to the given limit.
  • Raises:
    • ValueError: If limit is less than 2 or start is less than 2.
  • Examples:
    print(primes(limit=10))  # Output: [2, 3, 5, 7]
    print(primes(start=10, limit=20))  # Output: [11, 13, 17, 19]
    

Disclaimer

FuncBox provides utility functions for general use. The developer is not responsible for any issues caused by improper use or abuse of the library.

Contributing

Contributions are welcome! Feel free to fork the repository and submit a pull request with your improvements.

License

This project is licensed under the MIT License. See the LICENSE file for more 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

funcbox-0.3.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

funcbox-0.3.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file funcbox-0.3.1.tar.gz.

File metadata

  • Download URL: funcbox-0.3.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for funcbox-0.3.1.tar.gz
Algorithm Hash digest
SHA256 1e9b6ea2da1a0f432e2cbc31a049825584ca79cd56a41e9bd5e772a802e7a6cb
MD5 2be4d2a45852a88e551d42e18aef45bc
BLAKE2b-256 7b10c77a68ceab98609c72e6521d1f23329930b75d68420bc4f25b6464c82650

See more details on using hashes here.

File details

Details for the file funcbox-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: funcbox-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for funcbox-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6842c72730ff366d8709b3ae7e2a465d92b48831524201705ad87b29da0d7960
MD5 c9a429bf036305ecb5825a586594a39d
BLAKE2b-256 1410cd4bf671886739c473c2ff84fcc6d280b7b29ea605b7f63d1d86574955ec

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