Qython is a hybrid programming language that combines Python's readable syntax with q/kdb+'s functional programming paradigms. It extends Python with domain-specific constructs that map directly to q's efficient operations, creating a bridge between imperative and functional programming.
Qython is designed for developers who want to:
- Write readable code using Python-like syntax
- Generate efficient q code automatically
- Express functional concepts declaratively
- Bridge paradigms between imperative and functional programming
- Familiarity: Leverage Python's intuitive syntax and structure
- Efficiency: Generate optimized q/kdb+ code for high-performance computing
- Expressiveness: Add domain-specific constructs for common q patterns
- Safety: Automatic closure analysis and error checking
Qython supports standard Python constructs:
def calculate_mean(values, weights):
if len(values) == 0:
raise ValueError("Cannot compute mean of empty list")
total = 0
weight_sum = 0
# Standard Python logic works
for i in range(len(values)):
total = total + values[i] * weights[i]
weight_sum = weight_sum + weights[i]
return total / weight_sumReplace imperative loops with declarative iteration counts:
def repeat_operation(n):
counter = 0
do n times:
counter = counter + 1
process_step(counter)
return counterExpress iterative algorithms that converge to a solution:
def newton_sqrt(x):
def step(guess):
new_guess = (guess + x / guess) / 2
return new_guess
result = converge(step, starting_from=x/2)
return resultThe converge() function:
- Takes a step function that defines the iteration logic
- Detects convergence using q's built-in convergence operator
- Eliminates boilerplate tolerance checking and loop management
Qython provides a converge() function for iterative algorithms:
def complex_convergence(a, b, tolerance):
def step(result):
next_val = some_function(result, a, b)
return next_val
final_result = converge(step, starting_from=a)
return final_resultGenerates efficient q code:
step/[a].py- Pure Python files (standard Python syntax).qy- Qython files (extended syntax withdoandconverge)
qython-project/
├── src/
│ ├── algorithms.qy # Qython source files
│ ├── utilities.py # Pure Python utilities
│ └── converge_examples.qy # Convergence algorithms
├── build/
│ └── generated.q # Compiled q code
├── translator/
│ ├── translate.py # Qython → Q compiler
│ ├── custom_grammar.txt # Extended grammar definition
│ └── demo.py # Usage examples
└── README.md
| Qython | Q | Purpose |
|---|---|---|
def func(x, y): |
func:{[x;y] ...} |
Function definition |
if condition: |
if[condition; ...] |
Conditionals |
while condition: |
while[condition; ...] |
Traditional loops |
do n times: |
do[n; ...] |
Fixed iteration |
converge(step_func, starting_from=val) |
step_func/[val] |
Convergence iteration |
x = y |
x:y |
Assignment |
x / y |
x%y |
Division |
raise ValueError("msg") |
`$"msg" |
Error handling |
Qython Source (newton.qy):
def sqrt_newton(x):
"""Calculate square root using Newton's method in Qython."""
if x < 0:
raise ValueError("Cannot compute sqrt of negative number")
if x == 0:
return 0
def step(guess):
new_guess = (guess + x / guess) / 2
return new_guess
result = converge(step, starting_from=x/2)
return resultGenerated Q Code:
sqrt_newton:{[x]
if[x<0; `$"Error"];
if[x=0; :0];
step:{[guess]
new_guess:(guess+x%guess)%2;
:new_guess
};
result:step/[x%2];
:result
}Performance:
q)sqrt_newton[16]
4f
q)sqrt_newton[10]
3.162278f
q)\t:10000 sqrt_newton[100]
2 // 2 milliseconds for 10,000 iterationsfrom translate import translate_file
# Compile Qython to Q
q_code = translate_file('myproject.qy')
# Execute in q environment
q(q_code)The compiler automatically:
- Detects variables referenced from outer scope
- Generates proper parameter lists and function calls
- Validates q's 8-parameter limit
- Optimizes parameter ordering for performance
# Qython enforces safety constraints
def too_many_vars():
converge on result starting from 0:
# Error: More than 8 variables would be captured
complex_expression(a, b, c, d, e, f, g, h, i)
return result
# Compiler error: "too many variables (9), q supports max 8 parameters"Qython maintains Python's philosophy that code is read more often than written:
# Clear intent and flow
def step(solution):
return improve_solution(solution, data, parameters)
final_solution = converge(step, starting_from=initial_guess)Generate optimized q code while maintaining high-level abstractions:
# High-level Qython
do 1000000 times:
process_data()
# Efficient q translation
do[1000000; process_data[]]Combine the best of both paradigms:
- Imperative clarity for business logic
- Functional efficiency for mathematical operations
- Automatic translation between paradigms
Compile-time guarantees for common q pitfalls:
- Closure capture prevents undefined variable errors
- Parameter limits enforced at compile time
- Type-aware translation for q-specific constructs
- List comprehensions → q's
each,overoperators - Pattern matching → q's conditional expressions
- Async/await → q's deferred execution model
- Classes → q's namespace system
- Type annotations → q's type system integration
# List comprehensions
result = [x * 2 for x in data if x > threshold]
# → {x*2} each data where data>threshold
# Pattern matching
match algorithm:
case "newton": converge on x starting from initial: ...
case "bisection": while abs(high - low) > tolerance: ...
case _: raise ValueError("Unknown algorithm")
# Async operations
async def parallel_compute(datasets):
results = await [process(d) for d in datasets]
return combine(results)- Familiar syntax with powerful new constructs
- Automatic optimization to high-performance q code
- Functional programming concepts without the learning curve
- Higher-level abstractions for complex algorithms
- Readable code for team collaboration
- Faster development with automatic closure management
- Expressive algorithms for mathematical computing
- High performance execution on large datasets
- Seamless integration with existing Python workflows
- Domain-specific constructs for iterative algorithms
- Real-time performance through q compilation
- Risk management through compile-time safety checks
Qython represents the next evolution in domain-specific languages, combining the readability of Python with the performance of q/kdb+ to create a powerful tool for mathematical computing, financial modeling, and high-performance data analysis.