On Thu, Sep 1, 2011 at 12:08 PM, eukreign <[email protected]> wrote: > Hello, > > I've spent a few evenings working on a project to be able to write > Android applications in Python by compiling Python source into Java > source. I thought I'd share what I have in case there is interest in > this (maybe even outside of Android development). I see this project > having two parts, a Python to Java converter and a very basic > simulated/stubbed out Java/Android API (so that one could write unit > tests in python and run them in python, before compiling everything > into Java). > > Of course 100% Python to Java is not possible or practical to > implement in a reasonable amount of time but I think enough could be > implemented so that we could have 100% Python syntax with maybe 50% > Python semantics and 50% Java semantics. I think just getting to that > point would be a huge productivity gain on working with any java > frameworks/APIs. Over time I think there can be work arounds for > things that Java can't do like multiple inheritance, dynamic class > creation, duck typing, etc. > > So, how does it work? I use Pythons ast and symtable modules to walk > the source code and try to infer the possible types that a variable > can have. This gives me the type information that Java will need. Then > another pass generates Java source using this type information. There > is nothing particularly magical especially since duck typing isn't > implemented and you can't change the type of a variable after you've > assigned to it the first time. So you have to be very careful with the > Python code you write, think of it as statically typed Python where > you don't have to declare types since they are inferred. I haven't > gotten to lists and dictionaries yet but the principles will be the > same, you can't have heterogeneous lists so the first thing you put in > a list will provide that lists type. > > Why not use PyPy? It's possible to achieve some of my goals with > PyPy and in some cases it will achieve a lot more than I will ever > attempt to implement. One advantage of PyPy is that it supports a lot > more of Pythons dynamism (for one, PyPy generates code from a loaded > Python module while py2j uses static python source files). There are > two major problems with PyPy for my purposes: 1) it generates > specialized jvm byte code that the dalvik vm cannot convert 2) because > PyPy is more pythonic it will be much harder to compile code in a way > that will just plugin into the Android APIs, with PyPy you can't > import Java packages or inherit from Java classes as you would have to > do to write Android apps. > > Code I have so far is hosted here: https://bitbucket.org/eukreign/py2j/src > > I would say it's still more of a prototype than even alpha level > code. You can see an example of input file and output in the samples > directory. > > Also, the bottom of both analyze.py and compiler.py have a ton of > unit tests you can look at to see what is possible so far. > > If you have questions or are interested in helping me please send me > an email. > > - lex > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en
Lex, Sounds like a fun hobby project, but also a lot of work if you *actually* want it to work well ;-). If you want to just get a quick and dirty solution, perhaps you should look here: http://code.google.com/p/android-scripting/ Neat idea otherwise, though. Kris -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

