PyDoc on a shared object - python

Small simple question, but I cannot find the answer!
Is it possible to use PyDoc on a shared object used as a module in python ? Let say that I declare all the help in that *.so and I'm able to use them with help(module or class name). But I'd like to use PyDoc to create an HTML from these. So this way, I won't have to write the doc two times!
Let me know if it's possible, or if there's a small hack :)
Also, in the C files, I'm able to put the description of each function! But when I type help(my_class) I don't have a description of the class and then the list of function. What am I doing wrong ?
So, what I want is when I do this in the C files:
return PyObject_NEW(my_class, &my_class_type);
Later, in Python, after I loaded the module, I can do:
help(my_class)
and see the help, like it would do for help(int)!!
Thanks!

So, I partially solved my problem.
I still can't use pydoc on an *.so file. But now, I can add modules, which is nice. Everything is available here: http://docs.python.org/extending/newtypes.html
But I'm still looking for the pydoc solution!

Related

Is possibile to get the AST from a python code object?

I know that's possible from a snippet of code in python to get its AST or its code object via compile().
I was wondering if it is possible to do the opposite: I have the code object(extracted by a .pyc file) and I was looking for the its AST.
No, unfortunately it is not possible to do by conventional methods unless the file that the code object created is still available in the path. There are tools like uncompyle6, which might help though.

How to print all available python standard library methods in the terminal

I am trying to figure out how to list out all the standard library functions/methods (still learning the difference, I'm a noob). So I get how to import a module and use dir() and help(). These have been a great help and when I try to rewrite a program I learned to write and I get lost I try to use the dir() and help() to spark my memory.
However, for example, I was re-looking through some code and I had .replace() and I was scouring through my imported modules to find it and then found out it's in the standard library and a sub-part of the str() function. Is there something I can type into dir() that will spit out str()?
How can I print out all top level built in function/methods like I would when I do dir(re)?
I've tried re-wording this question in search engines several ways and I can't find anything and the results get muddied with "python list _____".
Thanks so much!
Is there something I can type into dir() that will spit out str()?
dir() on a value of that type.
dir('hello') # string methods
dir(5) # integer methods
dir([]) # list methods
Ah, I never tried typing just "dir()" that listed I could put in dir(__builtins__) and that is what I was looking for. I'll leave it here in case someone else is a noob too and knows where they can start. Just start with dir() and go down the rabbit hole!

How can I call a python function from an advanced scripting voice command in Dragon NaturallySpeaking?

How can I call a python function from an advanced scripting voice command in Dragon NaturallySpeaking?
I don't want to use a third-party application such as dragonfly or NatLink (paper).
So, one way is to compile it. You can put a bunch of functions that do different things all into the same program and pass along appropriate arguments to select the function you want, and pass the parameters along. Returning the result can be tricky, though, but I usually use the Clipboard (so copy the py output to clip and read from clip in Dragon). Multi-word params need to have spaces escaped (%20) and process it inside your py.
Something like this:
ShellExecute "path\program.exe myFunc myPar1, my%20Par%202", 6 ' 6 runs minimized
Wait 1
myVar = Clipboard
Hth,
Warning: This is not an answer. I am not a programmer. I don't know any Python and have no way of testing it.
This is just a suggestion on how to solve this problem. I don't know where else to put this. I'd put it in a comment, but it allows no screenshots. Please edit and suggest as you wish.
There is answer on SO that deals with calling Python from Excel, which is a similar concept: https://stackoverflow.com/a/3569988/2101890. I am trying to use that here but don't know how.
When using commands in another programming language, you can sometimes add them by adding a reference in the MyCommands Editor. You can reference DLLs and other "stuff". Some references to libraries appear automatically. I've installed Python and hoped to find Python in the References, but no such luck:
There is no Python entry here that I can find. You may have better luck. If you do find something, check the box and see if you can add python commands without causing an error when saving the command.
Maybe you can browse to %localappdata%\Programs\Python\Python36\ and add some of the DLLs from there and call Python commands from there. Or try getting it to work in the way described under 1.

is there a way to import a Python egg from memory, but not the disk?

Suppose I have a binary string which contains a Python egg, all zipped up. Is there a way to execute and "import" that egg from memory, without ever writing it to disk?
eggs are simply zip files under another name so they are imported using zipimport.zipimporter which (unfortunately) is a module written in C.
I think your options are:
rewrite a modified zipimporter to accept a file like object
instead of a filename
re-implement zipimporter in Python and then
modify to accept a file-like object
or write a temporary file to
disk, import that and then delete the file.
The last of these is probably the easiest.
Some time ago I had a look at pypiserver and might be something similar to what you need:
http://pypi.python.org/pypi/pypiserver/0.5.0
The standalone version
https://raw.github.com/schmir/pypiserver/standalone/pypi-server-standalone.py
does some interesting magic with a DictImporter and the zlib library, maybe you can do something similar..
Take a look at the imp module: http://docs.python.org/library/imp.html
You may have some luck with byte-compiled modules or dynamically formed modules.
Also this recipe can probably help (not as is, I guess):
http://code.activestate.com/recipes/82234-importing-a-dynamically-generated-module/
As mentioned, in a comment to the latter, works with single modules only.
The question is similar to one asked some time ago: Python, import string of Python code as module
I am not familiar with Python eggs, but try using a StringIO object. Because Python uses duck typing, you can treat it just like a file if you don't call any file specific methods on it.

how do you statically find dynamically loaded modules

How does one get (finds the location of) the dynamically imported modules from a python script ?
so, python from my understanding can dynamically (at run time) load modules.
Be it using _import_(module_name), or using the exec "from x import y", either using imp.find_module("module_name") and then imp.load_module(param1, param2, param3, param4) .
Knowing that I want to get all the dependencies for a python file. This would include getting (or at least I tried to) the dynamically loaded modules, those loaded either by using hard coded string objects or those returned by a function/method.
For normal import module_name and from x import y you can do either a manual scanning of the code or use module_finder.
So if I want to copy one python script and all its dependencies (including the custom dynamically loaded modules) how should I do that ?
You can't; the very nature of programming (in any language) means that you cannot predict what code will be executed without actually executing it. So you have no way of telling which modules could be included.
This is further confused by user-input, consider: __import__(sys.argv[1]).
There's a lot of theoretical information about the first problem, which is normally described as the Halting problem, the second just obviously can't be done.
From a theoretical perspective, you can never know exactly what/where modules are being imported. From a practical perspective, if you simply want to know where the modules are, check the module.__file__ attribute or run the script under python -v to find files when modules are loaded. This won't give you every module that could possibly be loaded, but will get most modules with mostly sane code.
See also: How do I find the location of Python module sources?
This is not possible to do 100% accurately. I answered a similar question here: Dependency Testing with Python
Just an idea and I'm not sure that it will work:
You could write a module that contains a wrapper for __builtin__.__import__. This wrapper would save a reference to the old __import__and then assign a function to __builtin__.__import__ that does the following:
whenever called, get the current stacktrace and work out the calling function. Maybe the information in the globals parameter to __import__ is enough.
get the module of that calling functions and store the name of this module and what will get imported
redirect the call the real __import__
After you have done this you can call your application with python -m magic_module yourapp.py. The magic module must store the information somewhere where you can retrieve it later.
That's quite of a question.
Static analysis is about predicting all possible run-time execution paths and making sure the program halts for specific input at all.
Which is equivalent to Halting Problem and unfortunately there is no generic solution.
The only way to resolve dynamic dependencies is to run the code.

Categories