What does the Python error “name ‘self’ is not defined” mean?

The following should explain the problem. Maybe you will want to try this? The name self is only defined inside methods that explicitly declare a parameter called self. It is not defined at the class scope. The class scope is executed only once, at class definition time. “Calling” the class with A() calls it’s constructor __init__() instead. So maybe you actually want … Read more

Selecting with complex criteria from pandas.DataFrame

Sure! Setup: We can apply column operations and get boolean Series objects: [Update, to switch to new-style .loc]: And then we can use these to index into the object. For read access, you can chain indices: but you can get yourself into trouble because of the difference between a view and a copy doing this for … Read more

Using Queue in python

You do This imports all the classes from the queue module already. Change that line to CAREFUL: “Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools”. (Python PEP-8) As an alternative, one could use:

Error: nodename nor servname provided, or not known (python sockets)

From the docs of socket.gethostname: Return a string containing the hostname of the machine where the Python interpreter is currently executing. Note: gethostname() doesn’t always return the fully qualified domain name; use getfqdn() for that. The host IP is not the same as the hostname. You have a couple of options: You can either manually assign host to 0.0.0.0 or localhost You can also query socket.gethostbyname:host … Read more

In Python, when to use a Dictionary, List or Set?

A list keeps order, dict and set don’t: when you care about order, therefore, you must use list (if your choice of containers is limited to these three, of course 😉 ). dict associates each key with a value, while list and set just contain values: very different use cases, obviously. set requires items to be hashable, list doesn’t: if you have non-hashable items, therefore, you cannot use set and must instead use list. … Read more

How to convert IPython notebooks to PDF and HTML?

If you have LaTeX installed you can download as PDF directly from Jupyter notebook with File -> Download as -> PDF via LaTeX (.pdf). Otherwise follow these two steps. For HTML output, you should now use Jupyter in place of IPython and select File -> Download as -> HTML (.html) or run the following command:jupyter nbconvert –to html notebook.ipynb This will convert the Jupyter document file … Read more

Decoding UTF-8 strings in Python

You need to properly decode the source text. Most likely the source text is in UTF-8 format, not ASCII. Because you do not provide any context or code for your question it is not possible to give a direct answer. I suggest you study how unicode and character encoding is done in Python: http://docs.python.org/2/howto/unicode.html