Funchacks - Fun module which is a small set of utilities

Overview

funchacks

PyPI version Supported python versions
CI status Mypy badge

👋 Introduction

Funchacks is a fun module that provides a small package of utilities.

Dynamic signature change without compile, eval and exec? That was the main idea of the project! But this path is a little dangerous, so the part could not be implemented, but if possible it will be implemented in the next versions!

So is it worth using funchacks signature utilities? More likely no than yes. If you want a really optimized and safe implementation of this idea, it's better to look into makefun (this was another reason why I wanted to do a dynamic signature change without compile, eval and exec).

⚙️ Installation

pip install funchacks

🚀 Quick start

  • 🔎 Function locals

from funchacks import inspections


def foo() -> None:
    some_local_var = 1
    other_var = 2

>>> dict(inspections.getlocals(foo.__code__))
{"some_local_var": 1, "other_var": 2}
  • 🔗 Dynamic function signature

(!) Note: if you add positional only or positional arguments, then there must be *args in the function signature. Accordingly, if you add keyword only or keyword arguments - **kwargs.

import inspect
from typing import Any

from funchacks import sig
@sig.change_args(
    sig.posonly("first"),
    sig.arg("second"),
)
def foo(*args: Any) -> None:
    """
    !!! Note:
        Temporarily positional only arguments are available only for
        the signature, there may be errors when calling the function.
    """

>>> inspect.Signature.from_callable(foo)
(first, /, second, *args)
@sig.change_args(
    sig.kwarg("first", None),
    sig.kwonly("second"),
)
def bar(**kwargs: Any) -> None:
    """
    !!! Note:
        Temporarily keyword only arguments are available only for
        the signature, there may be errors when calling the function.
    """

>>> inspect.Signature.from_callable(bar)
(first=None, *, second, **kwargs)
@sig.change_args(
    sig.arg("first"),
    sig.kwarg("second", None)
)
def baz(*args: Any, **kwargs: Any) -> None:
    """This should work.

    But how to access the arguments? locals?...
    """
    # All wrapped function has __sig__ attribute
    # that contains function signature.
    lvars = sig.Bind.from_locals(locals(), in_=baz)

    assert lvars.args() == ["first"]
    assert lvars.kwargs() == ["second"]

    return lvars.get("first") + lvars.get("second")

>>> inspect.Signature.from_callable(baz)
(first, second=None, *args, **kwargs)

>>> baz(1, 2)
3

Signature from function.

def spam(a, /, b, c=None, *, d) -> None:
    pass


@sig.from_function(spam)
def eggs(*args: Any, **kwargs: Any) -> None:
    pass

>>> inspect.Signature.from_callable(eggs)
(a, /, b, c=None, *, d)

Making small wrap.

from types import CodeType

from funchacks import make_wrap, WrapFunction


def foo() -> None:
    some_local = 1
    other_local = 2

wrap = make_wrap(foo)
wrap_from_func = WrapFunction.from_function(foo)

>>> dict(wrap.flocals)
{'some_local': 1, 'other_local': 2}
>>> isinstance(wrap, WrapFunction)
True
>>> dict(wrap.flocals) == dict(wrap_from_func.flocals)
True
>>> isinstance(wrap.code, CodeType)
True
You might also like...
Make creating Excel XLSX files fun again
Make creating Excel XLSX files fun again

Poi: Make creating Excel XLSX files fun again. Poi helps you write Excel sheet in a declarative way, ensuring you have a better Excel writing experien

Wordle is fun, so let's ruin it with computers.

ruin-wordle Wordle is fun, so let's ruin it with computers. Metrics This repository assesses two metrics about each algorithm: Success: how many of th

Moji sends text and fun facts from different APIs wit da use of a notification deamon
Moji sends text and fun facts from different APIs wit da use of a notification deamon

Moji sends text and fun facts from different APIs wit da use of a notification deamon. Can be runned via dmenu or rofi.

Just messing around with AI for fun coding 😂

Python-AI Projects 🤖 World Clock ⏰ ⚙︎ Steps to run world-clock.py file Download and open the file in your Python IDE. Run the file a type the name of

Its a simple and fun to use application. You can make your own quizes and send the lik of the quiz to your friends.
Its a simple and fun to use application. You can make your own quizes and send the lik of the quiz to your friends.

Quiz Application Its a simple and fun to use application. You can make your own quizes and send the lik of the quiz to your friends. When they would a

Cross-platform config and manager for click console utilities.

climan Help the project financially: Donate: https://smartlegion.github.io/donate/ Yandex Money: https://yoomoney.ru/to/4100115206129186 PayPal: https

python DroneCAN code generation, interface and utilities

UAVCAN v0 stack in Python Python implementation of the UAVCAN v0 protocol stack. UAVCAN is a lightweight protocol designed for reliable communication

Aerospace utilities: flight conditions package, standard atmosphere model, and more.

Aerospace Utilities About Module that contains commonly-used aerospace utilities for problem solving. Flight Condition: input altitude to compute comm

LinuxHelper - A collection of utilities for non-technical Linux users accessible via a GUI

Linux Helper A collection of utilities for non-technical Linux users accessible via a GUI This app is still in very early development, expect bugs and

Releases(funchacks)
Owner
DenyS
My discord: DenyS#1469
DenyS
OnTime is a small python that you set a time and on that time, app will send you notification and also play an alarm.

OnTime Always be OnTime! What is OnTime? OnTime is a small python that you set a time and on that time, app will send you notification and also play a

AmirHossein Mohammadi 11 Jan 16, 2022
TB Set color display - Add-on for Blender to set multiple objects and material Display Color at once.

TB_Set_color_display Add-on for Blender with operations to transfer name between object, data, materials and action names Set groups of object's or ma

null 1 Jun 1, 2022
A small Python library which gives you the IEEE-754 representation of a floating point number.

ieee754 ieee754 is small Python library which gives you the IEEE-754 representation of a floating point number. You can specify a precision given in t

Bora Canbula 5 Dec 20, 2022
Module for remote in-memory Python package/module loading through HTTP/S

httpimport Python's missing feature! The feature has been suggested in Python Mailing List Remote, in-memory Python package/module importing through H

John Torakis 220 Dec 17, 2022
A Python simple Dice Simulator just for fun

Dice Simulator ?? A Simple Python Dice Simulator ?? ?? ?? Description: That program make your RPG session more easy and simple. Roll the dice never be

Lauro Brant 17 May 14, 2022
a simple thing that i made for fun :trollface:

we-do-a-little-trolling about a simple thing that i made for fun. requirements and instructions first you need to install obs , then start the virtual

ranon rat 6 Jul 15, 2022
1cak - An Indonesian web that provide lot of fun.

An unofficial API of 1cak.com 1cak - An Indonesian web that provide lot of fun. Endpoint Lol -> 10 Recent stored posts on database Example: https://on

Dicky Mulia Fiqri 5 Sep 27, 2022
Secret santa is a fun and easy way to get together with your friends and/or family with a gift for them.

Secret Santa What is Secret Santa? Secret santa is a fun and easy way to get together with your friends and/or family with a gift for them. The idea i

null 2 Dec 6, 2021
A simple bot that will help you in your learning and make it more fun.

hyperskill-SimpleChattyBot-python A simple bot that will help you in your learning and make it more fun. Syntax bot.py Stages Stage #1: Zuhura Bot we

null 1 Nov 9, 2021
Really bad lisp implementation. Fun with pattern matching.

Lisp-py This is a horrible, ugly interpreter for a trivial lisp. Don't use it. It was written as an excuse to mess around with the new pattern matchin

Erik Derohanian 1 Nov 23, 2021