Python, Webkit: how to get the DOM after the page has loaded? - python

In my code I've connected to the WebView's load-finished event. The particular callback function takes a webview object and frame object as arguments. Then I tried executing get_dom_document() on the frame & the webview objects respectively. It seems this method doesn't exist for those objects...
PS: i started with the tips i got here http://www.gnu.org/software/pythonwebkit/
UPDATE (11-Sep-2010): I think the link I shared relates to a new & different project. Its not a solution per se. My bad!

it's definitely there.
and you can't just "take the tips from http://www.gnu.org/software/pythonwebkit/" you actually have to COMPILE THE CODE (reason: standard pywebkitgtk DOES NOT have W3C DOM accessor functions).
then take a look in pythonwebkit/pywebkitgtk/examples and run browser.py and you'll see what to do.
l.

i forgot to mention (and it wasn't on the documentation, which i've now updated): you specifically need to check out the "python_codegen" branch, otherwise you just end up with plain vanilla webkit. which is of absolutely no use to you.

Related

How to switch context in Blender python?

Good day,
I'm very new to the Blender API.
Ultimately, I am trying to get the currently selected object. I know this is available bpy.context.selected_objects. However, I'm running my script from a python file and the context object is different. in my current context the selected_objects does not exist.
I've searched around and at most I've been able to find that you can edit in which context some operators are run by overriding their settings. But bpy.context.selected_objects is not an operator.
Perhaps there is a method or operator inside bpy.data which returns the selected object? I can't seem to find the part of the documentation which shows what attributes are exposed inside bpy.data https://docs.blender.org/api/current/search.html?q=bpy.data.&check_keywords=yes&area=default#
Thanks in advance.
As your running this from blender scripting possibly using bpy.context.active_object rather then bpy.context.selected_objects might work better.
There can be an active object when no objects are selected, here's more info on them: https://blender.stackexchange.com/questions/27396/whats-the-difference-among-object-active-object-and-selected-objects
It's also quite hard to tell what could be going on without more details on the internal functionings, so attaching code would be usefull.

Execution Code Tracking - How to know which code has been executed in project?

Let say that I have open source project from which I would like to borrow some functionality. Can I get some sort of report generated during execution and/or interaction of this project?
Report should contain e.g.:
which functions has been called,
in which order,
which classes has been instantiated etc.?
Would be nice to have some graphic output for that... you know, if else tree and highlighted the executed branch etc.
I am mostly interested in python and C (perl would be fine too) but if there is any universal tool that cover multiple languages (or one tool per language) for that, it would be very nice.
PS: I am familiar with debuggers but I do not want to step every singe line of code and check if this is the correct instruction. I'm assuming that if functions/methods/classes etc. are properly named then one can get some hints about where to find desired piece of code. But only naming is not enough because you do not know (from brief overview of code) if hopefully looking function foo() does not require some data that was generated by obscure function bar() etc. For that reason I am looking for something that can visualize relations between running code.
PS: Do not know if this is question for SO or programmers.stackexchange. Feel free to move if you wish. PS: I've noticed that tags that I've used are not recommended but execution flow tracking is the best phrase to describe this process
Check out Ned Batchelder's coverage and perhaps the graphviz/dot library called pycallgraph. May not be exactly what you need and also (python-only) but in the ballpark.
Pycallgraph is actually likelier to be of interest because it shows the execution path, not just what codelines got executed. It only renders to PDF normally, but it wasn't too difficult to get it to do SVG instead (dot/graphviz supports svg and other formats, pycallgraph was hardcoding pdf rendering).
Neither will do exactly what you want but they are a start.

How to clear cache in SUDS before initialization?

The SUDS module has built in method for cache clear like client.options.cache.clear(). The main problem is that i need to create client first and only then clear the cache... Is there some method to clear it before the initialization ?
Take a look at the following option from the docs:
client.set_options(cache=None)
It is not exactly what you asked but may be useful.

How do I display function arguments in ipython qtconsole?

I'm using IPython qtconsole under windows 7, and when I first write a method name and type the bracket, a popup shows method parameters.
What is the way to display that popup explicitly, once it has disappeared? This is pretty common 'show method parameters' shortcut that I'm talking about, but I've failed to find the shortcut to it after an embarrassing amount of google searches.
In Spyder, try View - Panes - Object inspector. Then type the full name of the function.
I would highly recommend relying on the Python Library Reference rather than any in-IDE tools, at least for functions and classes that are in the standard library. For objects outside those libraries however... it looks like you can type object_name followed by a question mark, that is, object_name?, to get a list of informative details about the object. (Since everything is an object, this presumably includes functions.)
For your specific question, it looks, from the iPython docs, like the TAB key is what you're looking for, but somehow I doubt you haven't already tried that.

learning python 3, the docs seem bad

How do you figure out what methods are available within a class?
Example :
I am trying to learn about urllib.request. I found urlopen() in the docs :
http://docs.python.org/3.0/library/urllib.request.html
So I have :
response = urllib.request.urlopen(url)
What does this return? I know it will probably be an object, but what are this objects methods? Eventually I get to the examples (stuck at the bottom of the page far from where the idea was introduced) and discover read(), but I had to look outside the docs to find .decode(), which finally lets you do what the whole purpose of this library is.
Im having this type of problem with much of the docs.python.org pages. Is there a better documentation somewhere else, or am I going about learning this all the wrong way?
The real problem here is that you're using a very old version of the documentation. I have no idea how you found it, but it should be pretty clear from the URL http://docs.python.org/3.0/library/urllib.request.html, and the header on the top of that page ("Python v3.0.1 documentation"), and so on that you're not looking at the documentation for your version.*
If you were looking at the 3.3 documentation, you would have seen this:
For http and https urls, this function returns a http.client.HTTPResponse object which has the following HTTPResponse Objects methods.
The first link takes you to the reference for the exact class, the second to the reference for the abstract type. Which gives you exactly what you were looking for—the read method, and everything else.
In older versions of Python, the term "file-like object" was thrown around loosely. This was always a vague term (sometimes it means "an iterable of lines", "has a read() and/or write() as appropriate", "has a fileno()", …), and became much more so in Python 3 (because you have to distinguish a binary file from a text file). So, over the years, they've phased this out in favor of more specific documentation. But if you're looking at the very earliest Python 3 documentation, you're not getting the benefit.
* Note that if you visit any recent-ish version of the docs, like the 3.3 linked above, there's a pulldown menu in the header that lets you switch to a different version if you've found the wrong one. And, the default will always be 2.7 or the latest stable 3.x, and those are also the most common search results, and the easiest things to link to, so you will usually be on one of those unless you're really trying to make things hard for yourself. If you do find yourself on ancient docs like 3.0 or 2.4 or something, you can often just edit the URL to 3.3 or 2.7, or just 3 or 2; if not, a quick search should work.
The key phrase in the documentation of urlopen is:
This function returns a file-like object with two additional methods from the urllib.response module
The fact that this is a file-like object indicates that all the normal file operation functions (read, close) apply to the returned object also.
The document you linked to says this:
This function returns a file-like object with two additional methods
from the urllib.response module
geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed
info() — return the meta-information of the page, such as headers, in the form of an http.client.HTTPMessage instance (see Quick Reference to HTTP Headers)
Emphasis mine. A "file-like object" is defined as follows:
file object
An object exposing a file-oriented API (with methods such as read() or write()) to an underlying resource. Depending on the way it was created, a file object can mediate access to a real on-disk file or to another type of storage or communication device (for example standard input/output, in-memory buffers, sockets, pipes, etc.). File objects are also called file-like objects or streams.
As #abarnert points out, you're probably just better off using the new docs, which explicitly link to the methods of the returned object.
If you aren't using Ipython already you should be. Then you can just type urllib. and press tab to see all the available options and do the same for whatever it returns. If you type a question mark after the function it will bring up the documentation, two question marks often brings up the source.
For what its worth though I have always found the online documentation to be very helpful. It does say that urllib.request.urlopen is going to return a file like object early in the explanation

Categories