How to create python Ply lex passing a module - python

I'm trying to use a Logo Language Compiler that uses Ply into the Unity3D environment for an Open Source project https://github.com/ssouzawallace/blocks-programming.
To do so I am using IronPython that is a Python interpreter running in .NET (I need this to run in Uinty3D). There is a bug in IronPython and i found others with the same issue related to the traceback of python script execution.
In resume if I run the Logo Compiler using the official Python interpreter everything goes OK. But in IronPython, when the code pass trough the get_caller_module_dic method it cannot find my pyLex stuff because it cannot reach the second frame level.
In order to resolve the problem I am wondering to pass the proper object or module to the method:
def lex(module=None,object=None,debug=0,optimize=0,lextab="lextab",reflags=0,nowarn=0,outputdir="", debuglog=None, errorlog=None):
But I don't know how to do this.
Someone know what can I do?
Thank you very much in advance

Found solution here Python: How do I get a reference to a module inside the module itself?
Now I pass the entire script as a module instead hoping the lex and yacc script find my module using the traceback.
Using
import sys
current_module = sys.modules[__name__]

Related

PyCharm can't find queue.SimpleQueue

Using pycharm with python 3.7. I am using queue.SimpleQueue. The code runs fine, and PyCharm is pointed at the correct interpreter and all that. But with this code:
import queue
Q = queue.SimpleQueue()
I get a warning "Cannot find reference 'SimpleQueue' in 'queue.pyi'".
I do some searching. I hit ctrl-B on the "import queue" statement and it takes me to a file called queue.pyi in the folder helpers/typeshed/stdlib/3/ under the pycharm installation. So apparently instead of the queue.py file in lib/python3.7/ under the python venv, it thinks I'm trying to import this queue.pyi file instead, which I didn't even know existed.
Like I said, the code runs fine, and I can simply add # noinspection PyUnresolvedReferences and the warning goes away, but then the type inferencing and code hints on the variable Q don't work.
Another fix is to instead import _queue and use _queue.SimpleQueue, because apparently in python 3.7 queue.SimpleQueue is implemented in cython and is imported from a cython package _queue. But importing _queue seems hackish and implementation-dependent.
Is there a way to tell PyCharm that import queue means the actual lib/python3.7/queue.py as opposed to whatever helpers/typeshed/stdlib/3/queue.pyi is?
It was fixed in PyCharm 2019.3 https://youtrack.jetbrains.com/issue/PY-31437, could you please try to update?

calling precompiled module from another file

Primarirly I am c++ developer trying to use python for certain tasks for me.
I have made a python module in python 3.6 and got it pre-compiled in windows 7 using the following command
python -m py_compile myfile.py
using information from this link. I get "myfile.pyc" created
Now I want to use this in a python file which is using python 2.7
so, I use information from this and this & write
mod=imp.load_source('myfile.func', 'c:/folder1/folder2/myfile.pyc')
But the above statement gives me exception
[name 'o' is not defined]
Is this because I am using pre-compiled in 3.6 & using in 2.7?
What is that am I missing here
First python 3.6 is not backwards compatible with python 2.7. Secondly its usually better to import the module as normal and let the compiler handle caching library code as compiled byte code. Also the function load_source is meant for loading uncompiled source files, the function you want is load_compiled. Check here
https://docs.python.org/2/library/imp.html
Lastly, if you are looking for performance improvements this will only help reduce compile time, and only on the first compile or when the imported file changes.
What is __pycache__?
This is the complete solution of my problem. ( If you do not want to go through all the comment & discussion & figuring out the solution )
As Mr. Garrigan Stafford aptly pointed out that I am using the wrong API for loading the module.
The API for loading a compiled module is load_compiled & not load_source.
When I started using this API, ran in to the error of magic number: Bad magic number.
This happens because while creating the file, the compiler inserts certain values to basically identify what file is it. ( more info : can be found here.).
In my case, compiled my lib is 3.6 & used in 2.7 which was causing the problem.
To overcome, I basically went back to the original code & compiled my lib in 2.7 & then used it in the client code.
Volla !!!!
All works fine now.
Thanks to stackoverflow community as whole & Mr. Stafford in particular for helping out.

Can't access pyFirmata from Processing.py

The following code produces an error message in the Processing IDE:
from pyfirmata import Arduino,util
"No module named pyfirmata"
I have no problem running the code directly in the python 2.7 interpreter.
But, I can't access the Processing API from the interpreter.
You'll have to place the library in question inside your sketch folder. Python Mode doesn't use your system python, and cannot see any of the modules installed there.

Create documentation using pydoc and unknown modules

I'm afraid this will a question for a very particular case. At university we have been told to generate documentation by using pydoc. The problem is that we need to create it for a Maya script, and pydoc yells when it finds import maya.cmds as cmds
So, I tried to comment this line but I keep getting errors:
python C:\Python27\Lib\pydoc.py Script.py
problem in Script - <type 'exceptions.NameError'>: global name 'cmds' is not defined
I also tried Script, without the extension .py but it's silly doing that, we still running around the same issue.
Does anybody know how to generate documentation for Maya scripts, where import maya only works in the maya interpreter?
maya.commands is an stub module until it's run inside a working maya environment; if you just import it and inspect it outside of Maya you'll see that it's basically a placeholder.
If you want to inspect the contents, you can import the maya.standalone module and initialize it before running other commands (in this case it means you won't be able to run pydoc standalone.
You can get the documentation using the write command:
import maya.standalone
maya.standalone.initialize()
import pydoc
import mymodule
pydoc.write(mymodule) # writes the mymodule.html to current directory
Be warned, however, that the documentation for all maya built in functions will be unhelful:
'built-in function ls'
however you can at least document your own stuff without the maya parts crashing.
Pydoc, ironically, does not have a lot of external documentation. However you can see the code here:http://hg.python.org/cpython/file/2.7/Lib/pydoc.py (i'm not sure about the delta between this and the 2.6 version for maya pre-2014 but it works as above in maya 2011)

Compiling module using Notepad++ and IDLE in Windows

I have a simple module and a basic def. Module name is example315.py and the def is
def right_justify(s)
print(s)
This works fine when I import example315 and then call example315.right_justify("hello world")
If I change my def to not return anything (in fact I can change it in any way) and then run the function again (AFTER saving my module of course) iit still does the print.
Short of exiting IDLE and starting over I can't seem to get it to work.
Any help appreciated
The module is loaded once per session, you have to re-load it when you change it.
From the Python tutorial on modules:
For efficiency reasons, each module is only imported once per
interpreter session. Therefore, if you change your modules, you must
restart the interpreter – or, if it’s just one module you want to test
interactively, use reload(), e.g. reload(modulename).
The problem you're facing is the fact that IDLE has already imported and built its internal representation of your module. Editing the file on disk won't reflect on the now imported memory-resident version in IDLE. You should be able to get the behavior you're looking for with:
example315 = reload(example315)
And here's some source: Python Docs Source

Categories

Resources