Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Using objc_util and ctypes to convert Python code to Obj-C code
-
I want to program a game and then run it through a converter to convert the python code into objective c.
Example:
Library/example.py
print 'hello world'
You know what that does when it is run.
Library/converter.py
#This is the code where I need help
When run, converter.py prints this
>>>What file should I convert? example.py >>>Converting... >>>Parsing... >>> >>> #Converted output in objective c
Can anybody help me with this?
Maybe some example code? -
Why? Translating code between languages doesn't always work out too well.
Have you looked at the app template?
-
I looked at the template, I like it. Just for future reference, I want know how to convert. I often use a Windows computer; I can't use Xcode there. Is there any way?
-
As @Webmaster4o said, automatic code converters produce horrible code most of the time. How horrible the code is depends on how different the languages are.
For example, there is
lib2to3
, which is Python's standard Python 2 to Python 3 converter. The two versions of Python have many differences, but in the end they are still very similar, solib2to3
's output is generally quite readable. (Keep in mind thatlib2to3
does not attempt to convert every bit of code. Anything that it can't handle is simply ignored.)A more serious example, there's a Java-to-Python converter that I came across a while back (don't remember the name off the top of my head, but it's on PyPI somewhere). Java and Python have some major differences besides syntax, most importantly the standard library. This converter actually included the (most important) standard Java classes rewritten as Python modules, that way all method calls remained the same. As you can probably guess, this will produce code that works (if you're lucky), but nothing that looks like normal Python code. There are also some Java language constructs that cannot be translated directly to Python, such as assignments or increment/decrement operators in expressions.
Converting between Python and C is even worse than that. The two lanugages have few things in common besides basic constructs like if/else. They also have very different concepts of what is a "basic language feature". For example, this is fairly basic Python code:
mylist = [] mylist.append("hi") mylist.append(-42) print(mylist[-1])
However this would be quite hard to convert as C has no standard equivalent to Python's
list
. There are arrays, however they are fixed-size and their contents must all be of the same type.The other way around (C to Python) isn't much easier, and sometimes basically impossible. For example, this is valid C:
#include <stdio.h> int main(int argc, char **argv) { printf("%s\n", argv[argc]); return 0; }
You should never write this in a real program - what this does is get the data located in memory directly behind the
argv
array, pretend that it's achar *
, and print the text data that it points to. How would you translate this to Python? (Actually I think this falls under "undefined behavior", which means that if you give this to a C-to-Python translator, it would be allowed to crash, format your hard drive and/or summon Richard Stallman armed with a katana.)Point is, writing a code translator is hard, and there will always be cases where it will not work perfectly. If you want to run Python code from C, you should embed CPython. That way you have a full Python environment that is guaranteed to work.
-
https://github.com/alehander42/pseudo is on GitHub Trending Python these daze. It focuses more on algorithm porting rather than porting an entire script.
-
I figured that because I heard that Python compiles into C and then into Assembly when run, somebody had invented a program that could print your Python code in c, halfway compiled.
-
No, Python is not compiled into C. Python source code is internally compiled into Python bytecode, which is then run by the Python runtime. The runtime (CPython) is written in C, but that doesn't mean that Python code is compiled to C.
There is a project called Cython (not to be confused with CPython, the "standard" Python implementation from http://python.org/) which is based on Python's syntax, but has some extra features for working with C libraries. Cython source code is "compiled" to C, but only so it can be compiled to native code by a normal C compiler. Most Python code is also valid Cython, so you can probably use that to compile Python scripts to native code.