Re: RapydScript Python Modules

136 views
Skip to first unread message

Alexander Tsepkov

unread,
Jan 8, 2015, 3:18:58 PM1/8/15
to Muhammed Fatih İnanç, RapydScript
Adding RapydScript mailing list to the conversation since this is something other users may want to be aware of as well.

Short answer:
You can only import modules translated from Python to RapydScript, although translation will often be straightforward.

Long answer:
RapydScript is not Python, it's JavaScript variant that shares 90% of the syntax with Python (I should also mention that it's closer to Python 3 rather than Python 2). As a result, Python logic that happens to fall within than 90% can be used in RapydScript verbatim, the 10% that can't will require some porting on your end.

If the module is simple enough and doesn't require operator overloading, use of generators, or other complex Python logic, you can usually just wrap the code in "module" declaration and import it same way as Python (the wrapping is necessary since RapydScript uses the same namespace by default, whereas Python automatically creates a new namespace for each imported file with the same name as the file - this was mostly for developer convenience (to make it easier to divide logic into smaller files/chunks) rather than a technical limitation). And if I remember correctly, I added a --pythonic-import flag that automatically wraps each file in its own namespace, like native Python (although I don't use it myself so I haven't tested it recently).

If the module uses other low-level modules (that use os.system, platform-specific GUI, hook into C/C++, etc.), or complex features unique to Python (typically for optimization purposes, things like "yield", operator overloading, etc.), you will need to rewrite that logic manually to be more consistent with what's allowed in JavaScript world. This may vary from simple (see how I reimplemented Python's "re", "math", and "unittest" modules in src/lib directory) to very difficult (think reimplementing numpy/scipy). Often times, when the porting is very difficult, there is often already a JavaScript library that does something very similar, which I would suggest using instead (for example, instead of matplotlib, you could use D3 or something else from this chart: https://en.wikipedia.org/wiki/Comparison_of_JavaScript_charting_frameworks). In fact, when it comes to GUI and visual interaction (or web-scraping), JavaScript typically blows any Python solution out of the water - which is why I prefer RapydScript to native Python.

You will also need to make sure that any other Python modules being imported by the Python module you port to RapydScript are ported to RapydScript too. This is why I use .pyj extension rather than .py, I'd rather be told that the module doesn't exist than try to import an incompatible one without being aware of it. Lastly, there will be a tiny subset of Python logic you will not be able to use/port into RapydScript at all. This will usually be low-level system stuff that has no purpose in JavaScript world (unless you use node.js, in which case again, there is probably a JavaScript-compatible alternative), or stuff written in C/C++ with a Python wrapper around it (again, more and more of this is now implemented in JavaScript as well, even things like FFT algorithms for image and sound processing: https://github.com/dntj/jsfft).



On Thu, Jan 8, 2015 at 8:29 AM, Muhammed Fatih İnanç <mfati...@gmail.com> wrote:
Hello,

I bothered you from Facebook, a week ago. I am M. Fatih İNANÇ from Istanbul Technical University.

As I told you before, I am working on RapydScript to my graduation project. I posted something to Google Groups, but I am kind of a hurry I am again bothering you, I hope you will understand.

I read as much as I can documentation and forums and I still have a question about RapydScript. Can we import Python modules to RapydScript just like we can import Haskell Modules in Faylang script automatically? Or can we only import the translated Python to RapydScript modules to our code? Or simply can not we import Python modules to our RapydScript code? If so, what is the reason that you do not apply this feature? Is it because of some technical limitations, or is it because of some priority things, or is it because you can not handle it without a help?

I really will be appreciated if you help me.

Thank you for your time anyway.

--
Muhammed Fatih İNANÇ

Reply all
Reply to author
Forward
0 new messages