How can I change Python syntax in Pythonista? I wish to create some sort of module that changes the syntax when imported and changes it back with a module.quit() function. I specifically want to create bind(obj1, obj2) where obj1.value =obj2 and vice versa. Any ideas?
Python's syntax cannot be changed by Python code. If it could, then it wouldn't be syntax. (It is possible to process Python code from a string in advanced ways using the
astmodules and the
compilefunction. That's probably not what you're looking for though.)
Can you give an example of how you'd use the
bindfunction and what you want it to do? I don't quite understand what you mean with "obj1.value = obj2 and vice versa". It's possible that it could be written as a normal Python function.
print obj1.value #prints obj2
print obj2.value #prints obj1
Syntax would be:
I tried writing a module that defined the functions. This way, you pass a txt file with changed Python script through the module, and it would convert the changed syntax to normal syntax. Then exec it.
Am I on the right path? I aim overall to create a Python split-off language.
def bind(a, b): a.value = b b.value = a
Or am I missing something?
You're basically, as I understand it, talking about writing your own language, but still interpreting it with the Python interpreter. Either your language would have to manually parse the input string to 'translate' it to Python, or you'd have to write your own language. You can't merely reassign variables and constants to create a whole new language.
In any case, this isn't really a relevant Pythonista question, why don't you ask it on stackoverflow?
I figured that manually parsing the language through Pythonista would be the best way to go. I wanted to use the custom modules too.
If you want to write your own parser from the ground up (instead of using parts of Python's parser), then you should look into the
plymodules. As far as I know,
pyparsingis included with Pythonista.
plyisn't, but it's a pure Python module, so you can easily install it with
stashor by hand.
Neither is included
@jbap Are you sure?
>>> import pyparsing >>> pyparsing <module 'pyparsing' from '/var/mobile/Containers/Bundle/Application/E4982298-24E8-43A9-BE41-1B030744FB63/Pythonista.app/Frameworks/PythonistaKit.framework/pylib/site-packages/pyparsing.py'>
pyparsing.pyis located inside
PythonistaKit.framework, so it's not something that I had to install by hand.
pyparsingfor a long time too, even before the 2.0 release, so it can't be a very recent addition.
(The documentation for
pyparsingisn't included with Pythonista though, aside from docstrings that you can view with