Skip to main content

A simple scripting language that blends Python and JavaScript syntax

Project description

EasyScript

A simple scripting language interpreter that blends Python and JavaScript syntax, designed for easy expression evaluation, business rule processing, and LDAP/user object manipulation.

Features

  • Hybrid Syntax: Combines the best of Python and JavaScript syntax
  • JavaScript-style String Concatenation: Automatic string conversion (e.g., "hello" + 5"hello5")
  • Python-style Boolean Operators: Supports and, or, and not operators
  • Object Property Access: Dot notation for accessing object properties (user.cn, user.mail)
  • Built-in Variables: Pre-defined variables (day, month, year) with support for object injection
  • Built-in Functions: Essential functions like len(), log()
  • Regex Matching: Pattern matching with ~ operator (string ~ pattern)
  • Conditional Logic: Support for if statements with optional return keyword
  • Mixed Boolean Values: Supports both True/False and true/false
  • Comments: Python-style comments using # character (everything after # is ignored)

Quick Start

from easyscript import EasyScriptEvaluator

# Create an evaluator instance
evaluator = EasyScriptEvaluator()

# Basic arithmetic
result = evaluator.evaluate("3 + 5")  # Returns: 8

# String concatenation
result = evaluator.evaluate('"Hello " + "World"')  # Returns: "Hello World"

# JavaScript-style string + number
result = evaluator.evaluate('"Count: " + 42')  # Returns: "Count: 42"

# Built-in variables
result = evaluator.evaluate("year")  # Returns: current year

# Object injection
class User:
    def __init__(self):
        self.cn = "John Doe"
        self.mail = "john@example.com"

user = User()
result = evaluator.evaluate("user.cn", {"user": user})  # Returns: "John Doe"

# Log function (prints and returns value)
result = evaluator.evaluate('log("Debug info")')  # Prints: Debug info, Returns: "Debug info"

# Regex matching
result = evaluator.evaluate('user.mail ~ ".*@.*"', {"user": user})  # Returns: True

# Conditional logic
result = evaluator.evaluate('if len(user.cn) > 3: return true', {"user": user})  # Returns: True

# Comments in expressions
result = evaluator.evaluate('5 + 3 # This is a comment')  # Returns: 8
result = evaluator.evaluate('len("hello") # Get string length')  # Returns: 5

Syntax Examples

Basic Operations

// Arithmetic
3 + 5 * 2        // 13
10 / 2 - 1       // 4.0

// String operations
"Hello " + "World"           // "Hello World"
"Value: " + 42               // "Value: 42"
len("Hello")                 // 5
log("Debug: " + 42)          // Prints: Debug: 42, Returns: "Debug: 42"

// Comparisons
5 > 3                        // True
"abc" == "abc"               // True
len("test") >= 4             // True

// Regex matching
"hello" ~ "h.*o"             // True
"test123" ~ "[0-9]+"         // True
"email@domain.com" ~ ".*@.*" // True

Boolean Logic

// Python-style boolean operators
true and false               // False
true or false                // True
not true                     // False
not false                    // True

// Operator precedence (not has higher precedence)
not true and false           // False (equivalent to: (not true) and false)
not (true and false)         // True

// Mixed case support
True and False               // False
true and False               // False
not True                     // False

Comments

// Python-style comments using #
5 + 3 # This is a comment
"hello" + "world" # Comments can appear at end of line

# Full line comments are supported
5 + 3  # Result: 8

# Multiple comments work fine
# This is comment line 1
# This is comment line 2
5 * 2  # Result: 10

// Comments in multi-line expressions
5 # first number
+ 3 # second number
* 2 # multiply result

Object Injection and Access

// Create and inject custom objects
from easyscript import EasyScriptEvaluator

class User:
    def __init__(self):
        self.cn = "John Doe"
        self.mail = "john.doe@company.com"
        self.department = "Engineering"

user = User()
evaluator = EasyScriptEvaluator()

// Access injected object properties
evaluator.evaluate("user.cn", {"user": user})                      // "John Doe"
evaluator.evaluate("user.mail", {"user": user})                    // "john.doe@company.com"

// Use in expressions
evaluator.evaluate('"Hello " + user.cn', {"user": user})           // "Hello John Doe"
evaluator.evaluate('len(user.mail) > 10', {"user": user})          // True
evaluator.evaluate('user.mail ~ ".*@.*"', {"user": user})          // True (email validation)

Conditional Statements

// Basic conditional
if 5 > 3: true               // True

// With return keyword (optional)
if len(user.cn) > 3: return "Long name"    // "Long name"

// Complex conditions
if user.department == "Engineering" and len(user.cn) > 3: true

Built-in Variables

EasyScript provides several built-in variables:

  • day: Current day of the month
  • month: Current month (1-12)
  • year: Current year

Additional objects can be injected using the variables parameter.

Use Cases

EasyScript is particularly useful for:

  • Business Rule Processing: Evaluate complex business logic with simple syntax
  • LDAP/Directory Services: Process user attributes and directory data
  • Configuration Management: Dynamic configuration based on conditions
  • Data Validation: Validate data with readable expressions
  • Template Processing: Generate dynamic content based on user data

Installation

Simply install the package or download the easyscript.py file:

pip install easyscript
from easyscript import EasyScriptEvaluator

Requirements

  • Python 3.6+
  • No external dependencies

Running Tests

Run the included test suite to verify functionality:

# Basic functionality tests
python tests/test_easyscript.py

# Object injection examples (includes LDAPUser example class)
python tests/test_easyscript_user.py

# Or run all tests
python -m pytest tests/

Note: The LDAPUser class used in tests is just an example of object injection and is located in tests/test_helpers.py. It's not part of the main library.

Object Injection

EasyScript's power comes from injecting your own objects and data:

from easyscript import EasyScriptEvaluator

evaluator = EasyScriptEvaluator()

# Inject any Python object
class Config:
    def __init__(self):
        self.debug = True
        self.api_url = "https://api.example.com"

class User:
    def __init__(self, name, email):
        self.name = name
        self.email = email
        self.active = True

# Create instances
config = Config()
user = User("Alice", "alice@company.com")

# Inject multiple objects
variables = {
    'config': config,
    'user': user,
    'version': '1.0'
}

# Use in expressions
result = evaluator.evaluate('user.name + " - " + config.api_url', variables)
# Returns: "Alice - https://api.example.com"

result = evaluator.evaluate('if user.active and config.debug: "Debug mode"', variables)
# Returns: "Debug mode"

result = evaluator.evaluate('user.email ~ ".*@company\.com"', variables)
# Returns: True

Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

License

This project is released under the MIT License. See the LICENSE file for details.

Examples

Real-world Usage Scenarios

User Access Control:

if user.department == "IT" and len(user.cn) > 0: return true

Dynamic Greetings:

if month >= 6 and month <= 8: return "Summer greetings, " + user.givenName

Data Validation:

# Inject user object
user = User("john", "john@company.com")
variables = {"user": user}

result = evaluator.evaluate('if len(user.mail) > 5 and user.mail ~ ".*@.*": return true', variables)

Pattern Matching:

result = evaluator.evaluate('if user.username ~ "^[a-z]+$": return "Valid username"', variables)
result = evaluator.evaluate('if user.mail ~ ".*@company\.com$": return "Company email"', variables)

Configuration Logic:

if user.title == "Manager" or user.department == "Executive": return "admin"

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

easyscript-0.6.2.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

easyscript-0.6.2-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file easyscript-0.6.2.tar.gz.

File metadata

  • Download URL: easyscript-0.6.2.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for easyscript-0.6.2.tar.gz
Algorithm Hash digest
SHA256 3b3509c7aad67fcf46314f260eb78e5743490d48da17e8dc4c834a648ec9b1e5
MD5 5ca1ea8c632b64466851f97118b1eca8
BLAKE2b-256 c6dd49ba079bcb8be3791db403fc7ad22cce40447a28a7001a80e585daf36ad0

See more details on using hashes here.

File details

Details for the file easyscript-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: easyscript-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for easyscript-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c14ad84d01ad830f3bdb38bb948a0083ec4d7577eaab770ae1b9e7517af277b3
MD5 aff3c01c5793b468c649a2f8b73a46d1
BLAKE2b-256 c4f23e1a97c201bf2e9a6d9a3cb683df3d2c930ab1acfae0e280547c303498d3

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