Skip to main content

A versatile Python library for utility functions similar to Underscore.js

Project description

UnicoreFW-py - Unicore Framework in Python


Overview

UnicoreFW-PY, a part of UnicoreFW.org is a Python-based framework based on UnderscoreJS, designed to offer a comprehensive set of utilities and functional programming tools. This framework is equipped with command-line capabilities that allow users to execute example scripts, parse custom command-line arguments, and integrate powerful utility methods for various use cases. The goal of UnicoreFW is to provide security, performance, and ease of use for developers looking to build and maintain Python applications.

Features

  • Flexible Test Execution: Run example scripts through the command line for rapid prototyping and testing.
  • Utility Functions: Includes a robust set of utility methods for functional programming, string manipulation, and more.
  • Secure Execution: Built-in security measures to safely execute code and handle user inputs.

Installation

  1. Clone the repository:

    git clone https://github.com/unicorefw-org/unicorefw-py.git
    cd unicore-py
    
  2. Ensure Python 3.x is installed on your system.

  3. Install any required dependencies (if applicable):

    pip install -r requirements.txt
    

Directory Structure

project_root_dir/
├── src/
│   └── unicorefw.py
├── examples/
│   └── sets/            # list of examples
│   └── functions.py     # show examples of function usage
│   └── task_manager.py  # Sample implementations of UniCoreFW functions
│   └── underscore.py    # examples on how to use UniCoreFW as _
└── README.md

Quick Start Guide

from unicorefw import UniCoreFW, UniCoreFWWrapper

def _(collection):
    return UniCoreFWWrapper(collection)

# Attach functions from 'Unicore' directly to '_'
for func_name in dir(UniCoreFW):
    if callable(getattr(UniCoreFW, func_name)) and not func_name.startswith("_"):
        setattr(_, func_name, getattr(UniCoreFW, func_name))

# Example usage:
result = _([1, 2, 3, 4, 5]).map(lambda x: x * 2).filter(lambda x: x > 5).value()
print(result)  # Expected output: [6, 8, 10]

# Using a static function call
template = "Name: <%= name %>, Age: <%= age %>"
context = {"name": "Alice", "age": 25}
result = _.template(template, context)
print(result)  # Expected output: "Name: Alice, Age: 25"

Documention

Please see docs/guide.md for more information.

PYTHON IMPLEMENTATION


This class, unicore, provides a wide range of utility functions for working with arrays, objects, and strings. Here's a summary of what each method does:

Array Functions:

  • map(array, func): Applies a function to each element of an array and returns a new array.
  • filter(array, func): Filters elements in an array based on a predicate function.
  • reduce(array, func, initial=None): Reduces the array to a single value using the provided function.
  • find(array, func): Finds the first element in the array that matches a predicate function.
  • uniq(array): Removes duplicates from an array.
  • flatten(array, depth=float('inf')): Flattens a nested array into a one-dimensional array.
  • first(array, n=1): Returns the first n elements of an array.
  • last(array, n=1): Returns the last n elements of an array.
  • compact(array): Removes falsey values from an array.
  • without(array, *values): Returns an array excluding all provided values.
  • range(start, stop=None, step=1): Generates a range of numbers.
  • intersection(*arrays): Returns an array of values common to all arrays.
  • difference(array, *others): Returns values from the first array not present in others.
  • groupBy(array, key_func): Groups array elements by the result of a function.
  • sortBy(array, key_func): Sorts an array by a function or key.
  • sample(array, n=1): Returns a random sample from an array.
  • zip(*arrays): Combines multiple arrays into an array of tuples.
  • unzip(array_of_tuples): Separates tuples into arrays.

Object Functions:

  • keys(obj): Returns the keys of a dictionary.
  • values(obj): Returns the values of a dictionary.
  • extend(obj, *sources): Extends an object by copying properties from sources.
  • clone(obj): Creates a shallow copy of an object.
  • has(obj, key): Checks if an object has a given property key.
  • defaults(obj, defaults): Assigns default properties to an object if they are missing.
  • invert(obj): Inverts an object's keys and values.

Type Checking Functions:

  • isString(obj): Checks if an object is a string.
  • isNumber(obj): Checks if an object is a number.
  • isArray(obj): Checks if an object is a list.
  • isObject(obj): Checks if an object is a dictionary.
  • isFunction(obj): Checks if an object is callable.
  • isBoolean(obj): Checks if an object is a boolean.
  • isDate(obj): Checks if an object is a date object.
  • isRegExp(obj): Checks if an object is a regular expression.
  • isError(obj): Checks if an object is an error instance.
  • isNull(obj): Checks if an object is None.
  • isUndefined(obj): Checks if an object is undefined.
  • isFinite(obj): Checks if an object is a finite number.
  • isNaN(obj): Checks if an object is NaN.
  • isMap(obj): Checks if an object is a map.
  • isSet(obj): Checks if an object is a set.

Utility Functions:

  • identity(value): Returns the given value unchanged.
  • times(n, func): Calls a function n times.
  • uniqueId(prefix=""): Generates a unique identifier with an optional prefix.
  • escape(string): Escapes HTML characters in a string.
  • unescape(string): Unescapes HTML characters in a string.
  • now(): Returns the current timestamp.
  • memoize(func): Caches the results of function calls.
  • bind(func, context, *args): Binds a function to a context, optionally pre-filling arguments.
  • partial(func, *partial_args): Partially applies arguments to a function.
  • throttle(func, wait): Throttles a function to be called at most once every wait milliseconds.
  • debounce(func, wait): Debounces a function to only be called after wait milliseconds.
  • once(func): Ensures a function is only called once.
  • after(times, func): Returns a function that will only run after it's been called a specified number of times.
  • compose(*funcs): Composes multiple functions to execute in sequence.
  • invoke(array, func_name, *args): Calls a method on each item in an array.
  • matches(attrs): Returns a function that checks if an object matches key-value pairs.
  • allKeys(obj): Returns all keys of an object, including inherited ones

Security Considerations

  • Safe Execution: The framework ensures that code execution is sandboxed and limited in scope to avoid unwanted side effects.
  • Input Validation: Command-line inputs are validated to prevent invalid or malicious commands.

Contributing

We welcome contributions to UnicoreFW! Please follow these steps:

  1. Fork the repository.
  2. Create a feature branch.
  3. Submit a pull request with a detailed description of your changes.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

unicorefw-0.1.2-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file unicorefw-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: unicorefw-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for unicorefw-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 25fa0e1af5fd3b7268fde14a07c1583e3b9070048d329caee45280d04dadb1b5
MD5 d163e9836c5e010a44478d558f8143b0
BLAKE2b-256 96dc618565b8c5b7120102586e50c2afe1341bccbf84d7941a076e021835845b

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