# Qython Programming Language
**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.
## What is Qython?
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
### Language Philosophy
1. **Familiarity**: Leverage Python's intuitive syntax and structure
2. **Efficiency**: Generate optimized q/kdb+ code for high-performance computing
3. **Expressiveness**: Add domain-specific constructs for common q patterns
4. **Safety**: Automatic closure analysis and error checking
## Qython Language Features
### 1. Core Python Compatibility
Qython supports standard Python constructs:
```python
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_sum
```
### 2. Extended Constructs
#### `do` Statement - Declarative Iteration
Replace imperative loops with declarative iteration counts:
```python
def repeat_operation(n):
counter = 0
do n times:
counter = counter + 1
process_step(counter)
return counter
```
#### `converge()` Function - Functional Convergence
Express iterative algorithms that converge to a solution:
```python
def newton_sqrt(x):
def step(guess):
new_guess = (guess + x / guess) / 2
return new_guess
result = converge(step, starting_from=x/2)
return result
```
The `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
### 3. Function-Based Convergence
Qython provides a `converge()` function for iterative algorithms:
```python
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_result
```
Generates efficient q code:
```q
step/[a]
```
## File Types and Project Structure
### File Extensions
- **`.py`** - Pure Python files (standard Python syntax)
- **`.qy`** - Qython files (extended syntax with `do` and `converge`)
### Project Structure
```
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 Translation
### Language Mappings
| 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 |
### Real-World Example: Newton's Method
**Qython Source** (`newton.qy`):
```python
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 result
```
**Generated Q Code**:
```q
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
q)sqrt_newton[16]
4f
q)sqrt_newton[10]
3.162278f
q)\t:10000 sqrt_newton[100]
2 // 2 milliseconds for 10,000 iterations
```
## Qython Compiler
### Installation and Usage
```python
from translate import translate_file
# Compile Qython to Q
q_code = translate_file('myproject.qy')
# Execute in q environment
q(q_code)
```
### Advanced Features
#### Closure Analysis
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
#### Error Handling
```python
# 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"
```
## Language Design Principles
### 1. **Readability First**
Qython maintains Python's philosophy that code is read more often than written:
```python
# Clear intent and flow
def step(solution):
return improve_solution(solution, data, parameters)
final_solution = converge(step, starting_from=initial_guess)
```
### 2. **Performance Through Translation**
Generate optimized q code while maintaining high-level abstractions:
```python
# High-level Qython
do 1000000 times:
process_data()
# Efficient q translation
do[1000000; process_data[]]
```
### 3. **Functional-Imperative Bridge**
Combine the best of both paradigms:
- **Imperative clarity** for business logic
- **Functional efficiency** for mathematical operations
- **Automatic translation** between paradigms
### 4. **Safety and Correctness**
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
## Future Qython Extensions
### Planned Language Features
- **List comprehensions** → q's `each`, `over` operators
- **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
### Example Future Syntax
```python
# 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)
```
## Why Qython?
### For Python Developers
- **Familiar syntax** with powerful new constructs
- **Automatic optimization** to high-performance q code
- **Functional programming** concepts without the learning curve
### For Q/KDB+ Developers
- **Higher-level abstractions** for complex algorithms
- **Readable code** for team collaboration
- **Faster development** with automatic closure management
### For Data Scientists
- **Expressive algorithms** for mathematical computing
- **High performance** execution on large datasets
- **Seamless integration** with existing Python workflows
### For Financial Technologists
- **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.
Raw data
{
"_id": null,
"home_page": "https://github.com/gabiteodoru/qython",
"name": "gabiteodoru-qython",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "q kdb qython compiler translator functional-programming",
"author": "Gabi Teodoru",
"author_email": "gabiteodoru@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ac/b4/1c3c156171f3bc91aad0527b314d73d6372244d8bad1f3c15d419bbf7626/gabiteodoru_qython-0.1.1.tar.gz",
"platform": null,
"description": "# Qython Programming Language\n\n**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.\n\n## What is Qython?\n\nQython is designed for developers who want to:\n- **Write readable code** using Python-like syntax\n- **Generate efficient q code** automatically\n- **Express functional concepts** declaratively\n- **Bridge paradigms** between imperative and functional programming\n\n### Language Philosophy\n\n1. **Familiarity**: Leverage Python's intuitive syntax and structure\n2. **Efficiency**: Generate optimized q/kdb+ code for high-performance computing\n3. **Expressiveness**: Add domain-specific constructs for common q patterns\n4. **Safety**: Automatic closure analysis and error checking\n\n## Qython Language Features\n\n### 1. Core Python Compatibility\nQython supports standard Python constructs:\n\n```python\ndef calculate_mean(values, weights):\n if len(values) == 0:\n raise ValueError(\"Cannot compute mean of empty list\")\n \n total = 0\n weight_sum = 0\n \n # Standard Python logic works\n for i in range(len(values)):\n total = total + values[i] * weights[i]\n weight_sum = weight_sum + weights[i]\n \n return total / weight_sum\n```\n\n### 2. Extended Constructs\n\n#### `do` Statement - Declarative Iteration\nReplace imperative loops with declarative iteration counts:\n\n```python\ndef repeat_operation(n):\n counter = 0\n do n times:\n counter = counter + 1\n process_step(counter)\n return counter\n```\n\n#### `converge()` Function - Functional Convergence\nExpress iterative algorithms that converge to a solution:\n\n```python\ndef newton_sqrt(x):\n def step(guess):\n new_guess = (guess + x / guess) / 2\n return new_guess\n \n result = converge(step, starting_from=x/2)\n return result\n```\n\nThe `converge()` function:\n- **Takes a step function** that defines the iteration logic\n- **Detects convergence** using q's built-in convergence operator\n- **Eliminates boilerplate** tolerance checking and loop management\n\n### 3. Function-Based Convergence\n\nQython provides a `converge()` function for iterative algorithms:\n\n```python\ndef complex_convergence(a, b, tolerance):\n def step(result):\n next_val = some_function(result, a, b)\n return next_val\n \n final_result = converge(step, starting_from=a)\n return final_result\n```\n\nGenerates efficient q code:\n```q\nstep/[a]\n```\n\n## File Types and Project Structure\n\n### File Extensions\n- **`.py`** - Pure Python files (standard Python syntax)\n- **`.qy`** - Qython files (extended syntax with `do` and `converge`)\n\n### Project Structure\n```\nqython-project/\n\u251c\u2500\u2500 src/\n\u2502 \u251c\u2500\u2500 algorithms.qy # Qython source files\n\u2502 \u251c\u2500\u2500 utilities.py # Pure Python utilities\n\u2502 \u2514\u2500\u2500 converge_examples.qy # Convergence algorithms\n\u251c\u2500\u2500 build/\n\u2502 \u2514\u2500\u2500 generated.q # Compiled q code\n\u251c\u2500\u2500 translator/\n\u2502 \u251c\u2500\u2500 translate.py # Qython \u2192 Q compiler\n\u2502 \u251c\u2500\u2500 custom_grammar.txt # Extended grammar definition\n\u2502 \u2514\u2500\u2500 demo.py # Usage examples\n\u2514\u2500\u2500 README.md\n```\n\n## Qython \u2192 Q Translation\n\n### Language Mappings\n\n| Qython | Q | Purpose |\n|--------|---|---------|\n| `def func(x, y):` | `func:{[x;y] ...}` | Function definition |\n| `if condition:` | `if[condition; ...]` | Conditionals |\n| `while condition:` | `while[condition; ...]` | Traditional loops |\n| `do n times:` | `do[n; ...]` | Fixed iteration |\n| `converge(step_func, starting_from=val)` | `step_func/[val]` | Convergence iteration |\n| `x = y` | `x:y` | Assignment |\n| `x / y` | `x%y` | Division |\n| `raise ValueError(\"msg\")` | `` `$\"msg\" `` | Error handling |\n\n### Real-World Example: Newton's Method\n\n**Qython Source** (`newton.qy`):\n```python\ndef sqrt_newton(x):\n \"\"\"Calculate square root using Newton's method in Qython.\"\"\"\n if x < 0:\n raise ValueError(\"Cannot compute sqrt of negative number\")\n if x == 0:\n return 0\n \n def step(guess):\n new_guess = (guess + x / guess) / 2\n return new_guess\n \n result = converge(step, starting_from=x/2)\n return result\n```\n\n**Generated Q Code**:\n```q\nsqrt_newton:{[x]\n if[x<0; `$\"Error\"];\n if[x=0; :0];\n step:{[guess]\n new_guess:(guess+x%guess)%2;\n :new_guess\n };\n result:step/[x%2];\n :result\n}\n```\n\n**Performance**:\n```q\nq)sqrt_newton[16]\n4f\nq)sqrt_newton[10] \n3.162278f\nq)\\t:10000 sqrt_newton[100]\n2 // 2 milliseconds for 10,000 iterations\n```\n\n## Qython Compiler\n\n### Installation and Usage\n\n```python\nfrom translate import translate_file\n\n# Compile Qython to Q\nq_code = translate_file('myproject.qy')\n\n# Execute in q environment\nq(q_code)\n```\n\n### Advanced Features\n\n#### Closure Analysis\nThe compiler automatically:\n- **Detects** variables referenced from outer scope\n- **Generates** proper parameter lists and function calls\n- **Validates** q's 8-parameter limit\n- **Optimizes** parameter ordering for performance\n\n#### Error Handling\n```python\n# Qython enforces safety constraints\ndef too_many_vars():\n converge on result starting from 0:\n # Error: More than 8 variables would be captured\n complex_expression(a, b, c, d, e, f, g, h, i)\n return result\n# Compiler error: \"too many variables (9), q supports max 8 parameters\"\n```\n\n## Language Design Principles\n\n### 1. **Readability First**\nQython maintains Python's philosophy that code is read more often than written:\n\n```python\n# Clear intent and flow\ndef step(solution):\n return improve_solution(solution, data, parameters)\n\nfinal_solution = converge(step, starting_from=initial_guess)\n```\n\n### 2. **Performance Through Translation**\nGenerate optimized q code while maintaining high-level abstractions:\n\n```python\n# High-level Qython\ndo 1000000 times:\n process_data()\n\n# Efficient q translation\ndo[1000000; process_data[]]\n```\n\n### 3. **Functional-Imperative Bridge**\nCombine the best of both paradigms:\n\n- **Imperative clarity** for business logic\n- **Functional efficiency** for mathematical operations\n- **Automatic translation** between paradigms\n\n### 4. **Safety and Correctness**\nCompile-time guarantees for common q pitfalls:\n\n- **Closure capture** prevents undefined variable errors\n- **Parameter limits** enforced at compile time\n- **Type-aware** translation for q-specific constructs\n\n## Future Qython Extensions\n\n### Planned Language Features\n- **List comprehensions** \u2192 q's `each`, `over` operators\n- **Pattern matching** \u2192 q's conditional expressions\n- **Async/await** \u2192 q's deferred execution model\n- **Classes** \u2192 q's namespace system\n- **Type annotations** \u2192 q's type system integration\n\n### Example Future Syntax\n```python\n# List comprehensions\nresult = [x * 2 for x in data if x > threshold]\n# \u2192 {x*2} each data where data>threshold\n\n# Pattern matching \nmatch algorithm:\n case \"newton\": converge on x starting from initial: ...\n case \"bisection\": while abs(high - low) > tolerance: ...\n case _: raise ValueError(\"Unknown algorithm\")\n\n# Async operations\nasync def parallel_compute(datasets):\n results = await [process(d) for d in datasets]\n return combine(results)\n```\n\n## Why Qython?\n\n### For Python Developers\n- **Familiar syntax** with powerful new constructs\n- **Automatic optimization** to high-performance q code\n- **Functional programming** concepts without the learning curve\n\n### For Q/KDB+ Developers \n- **Higher-level abstractions** for complex algorithms\n- **Readable code** for team collaboration\n- **Faster development** with automatic closure management\n\n### For Data Scientists\n- **Expressive algorithms** for mathematical computing\n- **High performance** execution on large datasets\n- **Seamless integration** with existing Python workflows\n\n### For Financial Technologists\n- **Domain-specific** constructs for iterative algorithms\n- **Real-time performance** through q compilation\n- **Risk management** through compile-time safety checks\n\n---\n\n**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.\n",
"bugtrack_url": null,
"license": null,
"summary": "Qython: Python-like syntax with q-functional constructs translator",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/gabiteodoru/qython"
},
"split_keywords": [
"q",
"kdb",
"qython",
"compiler",
"translator",
"functional-programming"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "7f1bc23d323191892e5566deb7346d400bf64bb9c2154eafc09ab4b659612854",
"md5": "d88ab2190c18f9bc2d0ce2151d5e1627",
"sha256": "44d1bec111c816e3b1feda990ded0c34e7a205dcf84abb9fb723e5ca10ab6c30"
},
"downloads": -1,
"filename": "gabiteodoru_qython-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d88ab2190c18f9bc2d0ce2151d5e1627",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 15470,
"upload_time": "2025-07-16T05:22:37",
"upload_time_iso_8601": "2025-07-16T05:22:37.487130Z",
"url": "https://files.pythonhosted.org/packages/7f/1b/c23d323191892e5566deb7346d400bf64bb9c2154eafc09ab4b659612854/gabiteodoru_qython-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "acb41c3c156171f3bc91aad0527b314d73d6372244d8bad1f3c15d419bbf7626",
"md5": "6bb8740981e25c6e3f83ae678c804dfb",
"sha256": "786649a82b8ae91b3c46f9d1310a1c0287479c84739efff4c6a10bae04a11017"
},
"downloads": -1,
"filename": "gabiteodoru_qython-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "6bb8740981e25c6e3f83ae678c804dfb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 15298,
"upload_time": "2025-07-16T05:22:38",
"upload_time_iso_8601": "2025-07-16T05:22:38.711924Z",
"url": "https://files.pythonhosted.org/packages/ac/b4/1c3c156171f3bc91aad0527b314d73d6372244d8bad1f3c15d419bbf7626/gabiteodoru_qython-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-16 05:22:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gabiteodoru",
"github_project": "qython",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "gabiteodoru-qython"
}