Skip to main content

Monic Framework

Project description

Monic Framework

codecov

Monic Framework is a powerful expression evaluation and code execution framework that provides a safe and flexible way to parse and interpret Python-style code. It offers a robust expression parser and interpreter designed for dynamic code evaluation, scripting, and embedded programming scenarios.

Key Features

  • Python-style syntax support
  • Secure code execution environment
  • Expression parsing and interpretation
  • Function definition and call support
  • Built-in type checking and validation
  • Seamless integration with existing Python projects

Supported Language Features

Core Python Features

  • Variables and basic data types (int, float, str, bool, etc.)
  • Control flow statements (if/else, for, while, break, continue)
  • Functions with full parameter support:
    • Positional and keyword arguments
    • Default values
    • Variable arguments (*args)
    • Keyword arguments (**kwargs)
    • Keyword-only arguments
  • Lambda expressions
  • List, set, and dictionary comprehensions
  • Context managers (with statements)
  • Exception handling (try/except/finally)
  • Classes and object-oriented programming
  • Pattern matching (match/case statements)

Expression Support

  • Arithmetic operations (+, -, *, /, //, %, **)
  • Comparison operations (==, !=, <, >, <=, >=)
  • Logical operations (and, or, not)
  • Bitwise operations (&, |, ^, <<, >>)
  • Assignment operations (=, +=, -=, *=, etc.)
  • Attribute access (obj.attr)
  • Subscript operations (obj[key])
  • Function and method calls
  • String formatting (f-strings)
  • Tuple and list unpacking
  • Named expressions (walrus operator :=)

Built-in Functions and Types

  • Essential built-ins (print, len, range, etc.)
  • Type conversion functions (int, float, str, bool)
  • Collection operations (min, max, sum, sorted)
  • Iteration helpers (enumerate, zip, filter, map)
  • Type checking (isinstance, issubclass)

Security Features

  • Sandboxed execution environment
  • Restricted access to system resources
  • Prevention of malicious code execution
  • Execution timeout protection:
    • Configurable maximum execution time
    • Automatic termination of long-running code
    • Protection against infinite loops
    • Time-based resource control
  • Forbidden operations disallowed:
    • File system operations
    • System command execution
    • Module imports
    • Access to sensitive attributes
    • Dangerous built-in functions

Installation

Basic installation:

pip install -r requirements.txt

For development and testing:

pip install -r requirements-tests.txt

Quick Start

Basic Usage

from monic.expressions import ExpressionsParser, ExpressionsInterpreter


# Initialize parser and interpreter
parser = ExpressionsParser()
interpreter = ExpressionsInterpreter()

# Execute simple expressions
code = """
# Variable assignment
x = 10
y = 20

# Conditional statement
if x < y:
    result = "y is greater"
else:
    result = "x is greater"

# Return result
result
"""

tree = parser.parse(code)
result = interpreter.execute(tree)
print(result)  # Output: "y is greater"

Function Definition and Calls

from monic.expressions import ExpressionsParser, ExpressionsInterpreter


code = """
def calculate_sum(a, b):
    return a + b

def calculate_average(numbers):
    total = 0
    for num in numbers:
        total += num
    return total / len(numbers)

# Using functions
sum_result = calculate_sum(10, 20)
avg_result = calculate_average([1, 2, 3, 4, 5])

[sum_result, avg_result]
"""

tree = parser.parse(code)
result = interpreter.execute(tree)
print(result)  # Output: [30, 3.0]

Timeout Control

from monic.expressions import ExpressionsParser, ExpressionsContext, ExpressionsInterpreter


parser = ExpressionsParser()
# Initialize with timeout context
context = ExpressionsContext(timeout=5.0)  # Set 5 seconds timeout
interpreter = ExpressionsInterpreter(context=context)

# This will be terminated after 5 seconds
code = """
while True:
    pass  # Infinite loop
"""

try:
    tree = parser.parse(code)
    interpreter.execute(tree)
except TimeoutError:
    print("Code execution timed out")  # Output: Code execution timed out

License

This project is licensed under the terms specified in the LICENSE file.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Issues and Support

For bug reports or feature requests, please submit them through GitHub Issues.

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

monic_framework-0.1.0.tar.gz (36.4 kB view details)

Uploaded Source

Built Distribution

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

monic_framework-0.1.0-py3-none-any.whl (37.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: monic_framework-0.1.0.tar.gz
  • Upload date:
  • Size: 36.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for monic_framework-0.1.0.tar.gz
Algorithm Hash digest
SHA256 83e1c7b3fcdcb9dcd80841479c7cb5e6acef4218b3b7fcb8b3616268f84de2ba
MD5 1f421aee096f224c0c044f26f94e05dc
BLAKE2b-256 9abf4a8353fd8240099378a1f7d279406a8883a9f4e160083fab4581388dcc65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for monic_framework-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7718c0a5a2e6183ab3c908e7cc62edde61f82acd431812f7104eeadefcfa59bb
MD5 f3ab1f0deee1db15806afa0f99c153a6
BLAKE2b-256 53f6c63459018a2af79ecce098df4f4f6bdde73a6d97a240412a8bf1ed3b5009

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