Skip to main content

A Python module for calling functions by string with argument support

Project description

Callixir

A small and convenient tool for implementing command execution in any application. Suitable for a console application or a social media bot.

Its principle of operation is simple: the user sends a string containing the command name and its arguments, if required. The next step is to execute the corresponding method based on this string.


Example

from callixir import SimpleShell

shell = SimpleShell()

@shell.reg("echo")
def cmdEcho(arg: str):
	return f"Echo {arg}"

@shell.reg("sum")
def сmdSum(a: int, b: int):
	return a + b

@shell.reg("log")
def cmdLog(*args: str):
	return ", ".join(args)

result = shell.execute("echo Hello_World!")
print(result.result) # Echo Hello_World!

result = shell.execute("sum 1 2")
print(result.result) # 3

result = shell.execute("log 1 2 Hi 10 J 100 \"many words in one\" 999 71")
print(result.result) # 1, 2, Hi, 10, J, 100, many words in one, 999, 71

Installing

Install Callixir via pip:

pip install callixir

Features

  • Convenient handling of arguments, they correspond to the signature of the arguments in the method;
  • Support for asynchronous commands;
  • Support for named arguments.

Async example

from callixir import AsyncSimpleShell
import asyncio

shell = AsyncSimpleShell()

@shell.reg("echo")
async def cmdEcho(arg: str):
	return f"Echo {arg}"

@shell.reg("sum")
async def сmdSum(a: int, b: int):
	return a + b

@shell.reg("log")
async def cmdLog(*args: str):
	return ", ".join(args)

async def main():
	result = await shell.execute("echo Hello_World!")
	print(result.result) # Echo Hello_World!

	result = await shell.execute("sum 1 4")
	print(result.result) # 5

	result = await shell.execute("log nova prospekt \"Async cmd\"")
	print(result.result) # nova, prospekt, Async cmd

asyncio.run(main())

Named arguments

The handler can work with function's named arguments (**kwargs). Furthermore, the same arguments undergo data type conversion for example, if the annotation is int, then all kwargs arguments will be converted to int.

A named argument can be specified in the string in two ways: --argument_name=value, --argument_name value. Values can also be enclosed in double quotes: --argument_name "multiple values".

Example

from callixir import SimpleShell

shell = SimpleShell()

@shell.reg("info")
def cmdInfo(**kwargs: str):
	data = []

	for key, value in kwargs.items():
		data.append(f"key = {key}\tvalue = {value}")

	return "\n".join(data)

@shell.reg("calc")
def cmdCalc(a: float, b: float, **kwargs: str):
	action = kwargs.get("action", "+")

	if action == "+": return a+b
	elif action == "-": return a-b
	elif action == "*": return a * b
	elif action == "/": return a / b
	else: return "Incorrect type of action"

def main():
	result = shell.execute("info --arg1=value1 --arg2 value2 --apple red --datetime=\"03.11.2025 15:31\"")
	print(result.result)
	'''
	key = arg1	value = value1
	key = arg2	value = value2
	key = apple	value = red
	key = datetime	value = 03.11.2025 15:31
	'''

	result = shell.execute("calc 1 2")
	print(result.result) # 3.0

	result = shell.execute("calc 1 2 --action +")
	print(result.result) # 3.0

	result = shell.execute("calc 1 2 --action *")
	print(result.result) # 2.0

	result = shell.execute("calc 1 2 --action pow")
	print(result.result) # Incorrect type of action

main()

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

callixir-0.0.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

callixir-0.0.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file callixir-0.0.2.tar.gz.

File metadata

  • Download URL: callixir-0.0.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.9 Windows/11

File hashes

Hashes for callixir-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3ebd6fe5c6f42cb44cc47ad8eae3e9e38d3d05a76380bbe87b4614911f462d09
MD5 4b00489536c412a2c2ce556ec75c3b19
BLAKE2b-256 a885ebbdda04e2e0d5cd56abd00892bedf516db2973f931ee6ef5881df85faf8

See more details on using hashes here.

File details

Details for the file callixir-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: callixir-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.9 Windows/11

File hashes

Hashes for callixir-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 db8d380d53c8183aacb2468c86b72d0962d2524f01515cfd0bb8ce82174f10e4
MD5 980893181345ec55102efb5338a0607f
BLAKE2b-256 06a4bb4d7688f2ef36f59015f71b3f93cfa74ef483728fd04917645aa91834b0

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