Skip to content

Latest commit

 

History

History
56 lines (38 loc) · 2.24 KB

README.md

File metadata and controls

56 lines (38 loc) · 2.24 KB

PythonSyntax

Build Status

Coverage Status

codecov.io

This package is under development and anything might change at any time. It also currently doesn't work on any release version of Julia, except for 0.5.2.

PythonSyntax.jl is a little like LispSyntax.jl, where this package gets its inspiration. But this isn't lisp syntax, it's Python syntax.

You need to use Python 3 with, e.g. by (and then restarting Julia after):

ENV["PYTHON"] = "/usr/bin/python3"
Pkg.build("PyCall")

The easiest way to use this package is to define modules:

using PythonSyntax

pymodule"""
FizzBuzz

def fizzbuzz(n):
    # this is still Julia, even though it looks like Python!
    # so range includes 1 and has length n — very different from Python.
    for i in range(1, n):
        if i % 15 == 0:
            println("FizzBuzz")
        elif i % 3 == 0:
            println("Fizz")
        elif i % 5 == 0:
            println("Buzz")
        else:
            println(i)
"""

FizzBuzz.fizzbuzz(10)

Remember: this is Julia, not Python. The syntax is Pythonic but the semantics are Julian.

Rewriting

Some identifiers are rewritten. Currently, the only rewriting is that _b suffixes in Python get mapped to ! suffixes in Julia.

Magic Syntax

PythonSyntax introduces some magic syntax that is unlike anything else in the Python language.

  • __jl__("2:2:10") escapes to Julia syntax. This can be useful if something has no clean way of being expressed pythonically. Note that this is not a runtime method: only string literals, and not strings computed at runtime, can be used.
  • __mc__(time, [i**2 for i in range(1, 10)]) allows calling Julia macros. Any number of arguments can be provided; they are given to the macro as expressions and are not evaluated, exactly as in Julia.