title stringlengths 15 101 | question stringlengths 57 2.99k | answer stringlengths 59 4.06k | title_question_fit listlengths 1 1 | title_question_fit-suggestion stringclasses 0
values | title_question_fit-suggestion-metadata dict | tags listlengths 1 1 | tags-suggestion listlengths | tags-suggestion-metadata dict | answer_quality listlengths 1 1 | answer_quality-suggestion int32 | answer_quality-suggestion-metadata dict | new_answer listlengths 1 1 | new_answer-suggestion stringclasses 0
values | new_answer-suggestion-metadata dict | external_id stringclasses 0
values | metadata stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
C-like structures in Python | <p>Is there a way to conveniently define a C-like structure in Python? I'm tired of writing stuff like:</p>
<pre><code>class MyStruct():
def __init__(self, field1, field2, field3):
self.field1 = field1
self.field2 = field2
self.field3 = field3
</code></pre>
| <p>Use a <a href="https://docs.python.org/2/library/collections.html#collections.namedtuple">named tuple</a>, which was added to the <a href="http://docs.python.org/library/collections.html">collections module</a> in the standard library in Python 2.6. It's also possible to use Raymond Hettinger's <a href="http://code.... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"flask"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do I sort a list of strings in Python? | <p>What is the best way of creating an alphabetically sorted list in Python?</p>
| <p>Basic answer:</p>
<pre><code>mylist = ["b", "C", "A"]
mylist.sort()
</code></pre>
<p>This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the <a href="http://docs.python.org/library/functions.html#sorted"><code>sorted()</code></a> function:</p... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"tkinter"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
"The system cannot find the file specified" when invoking subprocess.Popen in python | <p>I'm trying to use svnmerge.py to merge some files. Under the hood it uses python, and when I use it I get an error - "The system cannot find the file specified". Colleagues at work are running the same version of svnmerge.py, and of python (2.5.2, specifically r252:60911) without an issue.</p>
<p>I found <a href=... | <p>It's a bug, see the <a href="http://docs.python.org/lib/node528.html" rel="nofollow">documentation of <code>subprocess.Popen</code></a>. There either needs to be a <code>"shell=True</code>" option, or the first argument needs to be a sequence <code>['svn', '--version']</code>. As it is now, <code>Popen</code> is loo... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"regex"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Setup django with WSGI and apache | <p>I have been sold on mod_wsgi and apache rather than mod_python.
I have all the parts installed (django, apache, mod_wsgi) but have run into a problem deploying.</p>
<p>I am on osx 10.5 with apache 2.2 and django 1.0b2, mod_wsgi-2.3</p>
<p>My application is called tred.</p>
<p>Here are the relevant files:
httpd-vh... | <p>Note that Alias and WSGIScriptAlias directives do not have the same precedence. Thus, they will not be processed in file order as written. Instead, all Alias directives get precedence over WSGIScriptAlias directives. Thus, it wouldn't have mattered if the Alias for '/' appeared after WSGIScriptAlias, it would still ... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-3.x",
"pandas"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do I add data to an existing model in Django? | <p>Currently, I am writing up a bit of a product-based CMS as my first project.</p>
<p>Here is my question. How can I add additional data (products) to my Product model?</p>
<p>I have added '/admin/products/add' to my urls.py, but I don't really know where to go from there. How would i build both my view and my templ... | <p>You will want to wire your URL to the Django <a href="https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-create-update-create-object" rel="nofollow">create_object generic view</a>, and pass it either "model" (the model you want to create) or "form_class" (a customized <a href="https://docs... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-3.x"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What does ** (double star) and * (star) do for Python parameters? | <p>In the following method definitions, what does the <code>*</code> and <code>**</code> do for <code>param2</code>?</p>
<pre><code>def foo(param1, *param2):
def bar(param1, **param2):
</code></pre>
| <p>The <code>*args</code> and <code>**kwargs</code> is a common idiom to allow arbitrary number of arguments to functions as described in the section <a href="http://docs.python.org/dev/tutorial/controlflow.html#more-on-defining-functions">more on defining functions</a> in the Python documentation.</p>
<p>The <code>*a... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"mysql"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How can I represent an 'Enum' in Python? | <p>I'm mainly a C# developer, but I'm currently working on a project in Python.</p>
<p>How can I represent the equivalent of an Enum in Python? </p>
| <p>Enums have been added to Python 3.4 as described in <a href="http://www.python.org/dev/peps/pep-0435/" rel="nofollow">PEP 435</a>. It has also been <a href="https://pypi.python.org/pypi/enum34" rel="nofollow">backported to 3.3, 3.2, 3.1, 2.7, 2.6, 2.5, and 2.4</a> on pypi. </p>
<p>For more advanced Enum technique... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Resources for lexing, tokenising and parsing in python | <p>Can people point me to resources on lexing, parsing and tokenising with Python?</p>
<p>I'm doing a little hacking on an open source project (<a href="http://www.hotwire-shell.org/">hotwire</a>) and wanted to do a few changes to the code that <a href="http://code.google.com/p/hotwire-shell/source/browse/trunk/hotwir... | <p>I'm a happy user of <a href="http://www.dabeaz.com/ply/">PLY</a>. It is a pure-Python implementation of Lex & Yacc, with lots of small niceties that make it quite Pythonic and easy to use. Since Lex & Yacc are the most popular lexing & parsing tools and are used for the most projects, PLY has the advanta... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"django"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How to make Ruby or Python web sites to use multiple cores? | <p>Even though <a href="http://twistedmatrix.com/pipermail/twisted-python/2004-May/007896.html">Python</a> and <a href="http://www.reddit.com/comments/6wmum/thread_safe_ruby_on_rails_in_22_release/">Ruby</a> have one kernel thread per interpreter thread, they have a global interpreter lock (GIL) that is used to protect... | <p>I'm not totally sure which problem you want so solve, but if you deploy your python/django application via an apache prefork MPM using mod_python apache will start several worker processes for handling different requests.</p>
<p>If one request needs so much resources, that you want to use multiple cores have a look... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How can I simply inherit methods from an existing instance? | <p>below I have a very simple example of what I'm trying to do. I want to be able to use HTMLDecorator with any other class. Ignore the fact it's called decorator, it's just a name.</p>
<pre><code>import cgi
class ClassX(object):
pass # ... with own __repr__
class ClassY(object):
pass # ... with own __repr__
... | <blockquote>
<p>Very close, but then I lose everything from ClassX. Below is something a collegue gave me that does do the trick, but it's hideous. There has to be a better way.</p>
</blockquote>
<p>Looks like you're trying to set up some sort of proxy object scheme. That's doable, and there are better solutions tha... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-3.x"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Filter out HTML tags and resolve entities in python | <p>Because regular expressions scare me, I'm trying to find a way to remove all HTML tags and resolve HTML entities from a string in Python.</p>
| <p>Use <a href="http://codespeak.net/lxml/lxmlhtml.html">lxml</a> which is the best xml/html library for python.</p>
<pre><code>import lxml.html
t = lxml.html.fromstring("...")
t.text_content()
</code></pre>
<p>And if you just want to sanitize the html look at the lxml.html.clean <a href="http://codespeak.net/lxml/lx... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What's the easiest way to read a FoxPro DBF file from Python? | <p>I've got a bunch of FoxPro (VFP9) DBF files on my Ubuntu system, is there a library to open these in Python? I only need to read them, and would preferably have access to the memo fields too.</p>
<p><strong>Update</strong>: Thanks @cnu, I used Yusdi Santoso's <a href="http://www.physics.ox.ac.uk/users/santoso/dbf.... | <p>I prefer <a href="http://sourceforge.net/projects/dbfpy/">dbfpy</a>. It supports both reading and writing of <code>.DBF</code> files and can cope with most variations of the format. It's the only implementation I have found that could both read and write the legacy DBF files of some older systems I have worked with.... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What are Class methods in Python for? | <p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p>
<p>I now realise that I don't need to use Class methods for what I would done with <co... | <p>Class methods are for when you need to have methods that aren't specific to any particular instance, but still involve the class in some way. The most interesting thing about them is that they can be overridden by subclasses, something that's simply not possible in Java's static methods or Python's module-level func... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"mysql"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Retrieving an Oracle timestamp using Python's Win32 ODBC module | <p>Given an Oracle table created using the following:</p>
<pre><code>CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME ZONE);
</code></pre>
<p>Using the Python ODBC module from its <a href="http://www.python.org/download/windows/" rel="nofollow">Win32 extensions</a> (from the win32all package), I tried the following:... | <p>I believe this is a bug in the Oracle ODBC driver. Basically, the Oracle ODBC driver does not support the <code>TIMESTAMP WITH (LOCAL) TIME ZONE</code> data types, only the <code>TIMESTAMP</code> data type. As you have discovered, one workaround is in fact to use the <code>TO_CHAR</code> method.</p>
<p>In your exam... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"google-app-engine"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What's the best way to return multiple values from a function in Python? | <p>I have a function where I need to do something to a string. I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string. </p>
<p>In C#, I would use an out parameter for the string, but there is no equivalent in Python. I'm still very ne... | <pre><code>def f(in_str):
out_str = in_str.upper()
return True, out_str # Creates tuple automatically
succeeded, b = f("a") # Automatic tuple unpacking
</code></pre>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"tkinter"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Using Django time/date widgets in custom form | <p>How can I use the nifty JavaScript date and time widgets that the default admin uses with my custom view?</p>
<p>I have looked through <a href="https://docs.djangoproject.com/en/dev/topics/forms/">the Django forms documentation</a>, and it briefly mentions django.contrib.admin.widgets, but I don't know how to use i... | <p>The growing complexity of this answer over time, and the many hacks required, probably ought to caution you against doing this at all. It's relying on undocumented internal implementation details of the admin, is likely to break again in future versions of Django, and is no easier to implement than just finding anot... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-2.7"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How to merge two Python dictionaries in a single expression? | <p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p>
<pre><code>>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': ... | <p>In your case, what you can do is:</p>
<pre><code>z = dict(x.items() + y.items())
</code></pre>
<p>This will, as you want it, put the final dict in <code>z</code>, and make the value for key <code>b</code> be properly overridden by the second (<code>y</code>) dict's value:</p>
<pre><code>>>> x = {'a':1, '... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"google-app-engine"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Search and replace a line in a file in Python | <p>I want to loop over the contents of a text file and do a search and replace on some lines and write the result back to the file. I could first load the whole file in memory and then write it back, but that probably is not the best way to do it.</p>
<p>What is the best way to do this, within the following code?</p>
... | <p>The shortest way would probably be to use the <a href="http://docs.python.org/2/library/fileinput.html?highlight=fileinput#fileinput">fileinput module</a>. For example, the following adds line numbers to a file, in-place:</p>
<pre><code>import fileinput
for line in fileinput.input("test.txt", inplace=True):
pr... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"tkinter"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Finding a file in a Python module distribution | <p>I've written a Python package that includes a bsddb database of pre-computed values for one of the more time-consuming computations. For simplicity, my setup script installs the database file in the same directory as the code which accesses the database (on Unix, something like /usr/lib/python2.5/site-packages/mypa... | <p>Try using pkg_resources, which is part of setuptools (and available on all of the pythons I have access to right now):</p>
<pre><code>>>> import pkg_resources
>>> pkg_resources.resource_ filename(__name__, "foo.config")
'foo.config'
>>> pkg_resources.resource_filename('tempfile', "foo.con... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"regex"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What is the best way to do Bit Field manipulation in Python? | <p>I'm reading some MPEG Transport Stream protocol over UDP and it has some funky bitfields in it (length 13 for example). I'm using the "struct" library to do the broad unpacking, but is there a simple way to say "Grab the next 13 bits" rather than have to hand-tweak the bit manipulation? I'd like something like the w... | <p>The <a href="http://python-bitstring.googlecode.com">bitstring</a> module is designed to address just this problem. It will let you read, modify and construct data using bits as the basic building blocks. The latest versions are for Python 2.6 or later (including Python 3) but version 1.0 supported Python 2.4 and 2.... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"google-app-engine"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Using C in a shared multi-platform POSIX environment | <p>I write tools that are used in a shared workspace. Since there are multiple OS's working in this space, we generally use Python and standardize the version that is installed across machines. However, if I wanted to write some things in C, I was wondering if maybe I could have the application wrapped in a Python scri... | <p>Launching a Python interpreter instance just to select the right binary to run would be much heavier than you need. I'd distribute a shell .rc file which provides aliases.</p>
<p>In /shared/bin, you put the various binaries: /shared/bin/toolname-mac, /shared/bin/toolname-debian-x86, /shared/bin/toolname-netbsd-dre... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"django"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
PGP signatures from Python? | <p>What is the easiest way to create and verify PGP/GPG signatures from within a Python application? </p>
<p>I can call pgp or gpg using subprocess and parse the output, but I was looking for a way that didn't require an external program to be installed (my application is cross-platform mac/windows/unix).</p>
| <p>I think <a href="http://www.gnupg.org/related_software/gpgme/" rel="nofollow">GPGME</a> and the <a href="http://pyme.sourceforge.net/" rel="nofollow">PyMe Python wrapper</a> should do what you need.</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"google-app-engine"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
javascript locals()? | <p>In python one can get a dictionary of all local and global variables in the current scope with the built-in functions locals() and globals(). Is there some equivalent way of doing this in javascript? For instance, I would like to do something like the following:</p>
<pre><code>var foo = function(){ alert('foo'); ... | <ul>
<li><p>locals() - No. </p></li>
<li><p>globals() - Yes.</p></li>
</ul>
<p><code>window</code> is a reference to the global scope, like <code>globals()</code> in python.</p>
<pre><code>globals()["foo"]
</code></pre>
<p>is the same as:</p>
<pre><code>window["foo"]
</code></pre>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"mysql"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
cx_Oracle: how do I get the ORA-xxxxx error number? | <p>In a try/except block, how do I extract the Oracle error number?</p>
| <pre><code>try:
cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
error, = exc
print "Code:", error.code
print "Message:", error.message
</code></pre>
<p>This results in the following output:</p>
<pre><code>Code: 1476
Message: ORA-01476: divisor is equal to zero
</code></pre>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-2.7"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Python deployment and /usr/bin/env portability | <p>At the beginning of all my executable Python scripts I put the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> line:</p>
<pre><code>#!/usr/bin/env python
</code></pre>
<p>I'm running these scripts on a system where <code>env python</code> yields a Python 2.2 environment. My scripts quickly fail b... | <p>"env" simply executes the first thing it finds in the PATH env var. To switch to different python, prepend the directory for that python's executable to the path before invoking your script.</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"dictionary"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Always including the user in the django template context | <p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p>
<p>In my temp... | <p>There is <strong>no need</strong> to write a context processor for the user object if you already have the <a href="https://docs.djangoproject.com/en/dev/topics/auth/default/#authentication-data-in-templates"><code>"django.core.context_processors.auth"</code></a> in <a href="https://docs.djangoproject.com/en/dev/ref... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do I do monkeypatching in python? | <p>I've had to do some introspection in python and it wasn't pretty:</p>
<pre><code>name = sys._getframe(1).f_code
name = "%s:%d %s()" %(os.path.split(name.co_filename)[1],name.co_firstlineno,name.co_name)
</code></pre>
<p>To get something like</p>
<pre><code>foo.py:22 bar() blah blah
</code></pre>
<p>In our debugg... | <p>A print statement does its IO through "sys.stdout.write" so you can override sys.stdout if you want to manipulate the print stream.</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"pandas"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Splitting tuples in Python - best practice? | <p>I have a method in my Python code that returns a tuple - a row from a SQL query. Let's say it has three fields: (jobId, label, username)</p>
<p>For ease of passing it around between functions, I've been passing the entire tuple as a variable called 'job'. Eventually, however, I want to get at the bits, so I've been... | <p>I'd say that a dictionary is definitely the best way to do it. It's easily extensible, allows you to give each value a sensible name, and Python has a lot of built-in language features for using and manipulating dictionaries. If you need to add more fields later, all you need to change is the code that converts th... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"arrays"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Standard way to open a folder window in linux? | <p>I want to open a folder window, in the appropriate file manager, from within a cross-platform (windows/mac/linux) Python application.</p>
<p>On OSX, I can open a window in the finder with</p>
<pre><code>os.system('open "%s"' % foldername)
</code></pre>
<p>and on Windows with</p>
<pre><code>os.startfile(foldernam... | <pre><code>os.system('xdg-open "%s"' % foldername)
</code></pre>
<p><code>xdg-open</code> can be used for files/urls also</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"tkinter"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What is a tuple useful for? | <p>I am learning Python for a class now, and we just covered tuples as one of the data types. I read the Wikipedia page on it, but, I could not figure out where such a data type would be useful in practice. Can I have some examples, perhaps in Python, where an immutable set of numbers would be needed? How is this diffe... | <ul>
<li>Tuples are used whenever you want to return multiple results from a function.</li>
<li>Since they're immutable, they can be used as keys for a dictionary (lists can't).</li>
</ul>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"pandas"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Best way to extract text from a Word doc without using COM/automation? | <p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>
<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</... | <p>(Same answer as <a href="http://stackoverflow.com/questions/125222/extracting-text-from-ms-word-files-in-python">extracting text from MS word files in python</a>)</p>
<p>Use the native Python docx module which I made this week. Here's how to extract all the text from a doc:</p>
<pre><code>document = opendocx('Hell... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Python re.sub MULTILINE caret match | <p>The Python docs say:</p>
<blockquote>
<p>re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...</p>
</blockquote>
<p>So what's going on ... | <p>Look at the definition of <a href="http://docs.python.org/library/re.html#re.sub"><code>re.sub</code></a>:</p>
<pre><code>sub(pattern, repl, string[, count])
</code></pre>
<p>The 4th argument is the count, you are using <code>re.MULTILINE</code> (which is 8) as the count, not as a flag.</p>
<p>You have to compile... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"string"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Get Last Day of the Month in Python | <p>Is there a way using Python's standard library to easily determine (i.e. one function call) the last day of a given month?</p>
<p>If the standard library doesn't support that, does the dateutil package support this?</p>
| <p>I didn't notice this earlier when I was looking at the <a href="https://docs.python.org/2/library/calendar.html">documentation for the calendar module</a>, but a method called <a href="http://docs.python.org/library/calendar.html#calendar.monthrange">monthrange</a> provides this information:</p>
<blockquote>
<p><... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-3.x",
"pandas"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How can I get a commit message from a bzr post-commit hook? | <p>I'm trying to write a bzr post-commit hook for my private bugtracker, but I'm stuck at the function signature of post_commit(local, master, old_revno, old_revid, new_revno, mew_revid). How can I extract the commit message for the branch from this with bzrlib in Python?</p>
| <p>And the answer is like so:</p>
<pre><code>def check_commit_msg(local, master, old_revno, old_revid, new_revno, new_revid):
branch = local or master
revision = branch.repository.get_revision(new_revid)
print revision.message
</code></pre>
<p>local and master are Branch objects, so once you have a revisi... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"list"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How to generate urls in django | <p>In Django's template language, you can use <code>{% url [viewname] [args] %}</code> to generate a URL to a specific view with parameters. How can you programatically do the same in Python code?</p>
<p>What I need is to create a list of menu items where each item has name, URL, and an active flag (whether it's the ... | <p>If you need to use something similar to the <code>{% url %}</code> template tag in your code, Django provides the <code>django.core.urlresolvers.reverse()</code>. The <code>reverse</code> function has the following signature:</p>
<pre><code>reverse(viewname, urlconf=None, args=None, kwargs=None)
</code></pre>
<p><... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"string"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Can I write native iPhone apps using Python | <p>Using <a href="http://pyobjc.sourceforge.net/">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
| <p>You can use PyObjC on the iPhone as well, due to the excellent work by Jay Freeman (saurik). See <a href="http://www.saurik.com/id/5">iPhone Applications in Python</a>.</p>
<p>Note that this requires a jailbroken iPhone at the moment.</p>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
A python web application framework for tight DB/GUI coupling? | <p>I'm a firm believer of the heretic thought of tight coupling between the backend and frontend: I want existing, implied knowledge about a backend to be automatically made use of when generating user interfaces. E.g., if a VARCHAR column has a maximum with of 20 characters, there GUIs should automatically constrain t... | <p>web2py does most of what you ask:</p>
<p>Based on a field type and its validators it will render the field with the appropriate widget. You can override with</p>
<pre><code>db.table.field.widget=...
</code></pre>
<p>and use a third party widget.</p>
<p>web2py has js to blocks the user from entering a non-integer... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"google-app-engine"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How to find the mime type of a file in python? | <p>Let's say you want to save a bunch of files somewhere, for instance in BLOBs. Let's say you want to dish these files out via a web page and have the client automatically open the correct application/viewer.</p>
<p>Assumption: The browser figures out which application/viewer to use by the mime-type (content-type?) h... | <p>The python-magic method suggested by toivotuo is outdated. <a href="http://github.com/ahupp/python-magic">Python-magic's</a> current trunk is at Github and based on the readme there, finding the MIME-type, is done like this.</p>
<pre><code># For MIME types
>>> import magic
>>> mime = magic.Magic(m... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"arrays"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Pros and Cons of different approaches to web programming in Python | <p>I'd like to do some server-side scripting using Python. But I'm kind of lost with the number of ways to do that.</p>
<p>It starts with the do-it-yourself CGI approach and it seems to end with some pretty robust frameworks that would basically do all the job themselves. And a huge lot of stuff in between, like <a hr... | <p>CGI is great for low-traffic websites, but it has some performance problems for anything else. This is because every time a request comes in, the server starts the CGI application in its own process. This is bad for two reasons: 1) Starting and stopping a process can take time and 2) you can't cache anything in ... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"flask"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Modulus operation with negatives values - weird thing? | <p>Can you please tell me how much is <code>(-2) % 5</code>?
According to my Python interpreter is 3, but do you have a wise explanation for this?</p>
<p>I've read that in some languages the result can be machine-dependent, but I'm not sure though.</p>
| <p>By the way: most programming languages would disagree with Python and give the result <code>-2</code>. Depending on the interpretation of modulus this is correct. However, the most agreed-upon mathematical definition states that the modulus of <em>a</em> and <em>b</em> is the (strictly positive) rest <em>r</em> of t... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"list"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do I document a module in Python? | <p>That's it. If you want to document a function or a class, you put a string just after the definition. For instance:</p>
<pre><code>def foo():
"""This function does nothing."""
pass
</code></pre>
<p>But what about a module? How can I document what a <em>file.py</em> does?</p>
| <p>For the packages, you can document it in <code>__init__.py</code>.
For the modules, you can add a docstring simply in the module file.</p>
<p>All the information is here: <a href="http://www.python.org/dev/peps/pep-0257/">http://www.python.org/dev/peps/pep-0257/</a></p>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"arrays"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Iterate over subclasses of a given class in a given module | <p>In Python, given a module X and a class Y, how can I iterate or generate a list of all subclasses of Y that exist in module X?</p>
| <p>Although Quamrana's suggestion works fine, there are a couple of possible improvements I'd like to suggest to make it more pythonic. They rely on using the inspect module from the standard library.</p>
<ol>
<li>You can avoid the getattr call by using <code>inspect.getmembers()</code></li>
<li>The try/catch can be a... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"json"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How would you make a comma-separated string from a list? | <p>What would be your preferred way to concatenate strings from a sequence such that between each two consecutive pair a comma is added. That is, how do you map, for instance, <code>[ 'a', 'b', 'c' ]</code> to <code>'a,b,c'</code>? (The cases <code>[ s ]</code> and <code>[]</code> should be mapped to <code>s</code> and... | <pre><code>myList = ['a','b','c','d']
myString = ",".join(myList )
</code></pre>
<p>This won't work if the list contains numbers.</p>
<hr>
<p>As <a href="http://stackoverflow.com/users/3399/ricardo-reyes">Ricardo Reyes</a> suggested, if it contains non-string types (such as integers, floats, bools, None) then do:</p... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Can someone explain __all__ in Python? | <p>I have been using Python more and more, and I keep seeing the variable <code>__all__</code> set in different <code>__init__.py</code> files. Can someone explain what this does?</p>
| <p>Linked to, but not explicitly mentioned here, is exactly when <code>__all__</code> is used. It is a list of strings defining what symbols in a module will be exported when <code>from <module> import *</code> is used on the module.</p>
<p>For example, the following code in a <code>foo.py</code> explicitly expo... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"list"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Can the HTTP version or headers affect the visual appearance of a web page? | <p>I know, I would have thought the answer was obviously "no" as well, but I am experiencing a strange situation where when I view my site from our staging server it appears slightly larger than when I view it from my local dev server. I have used Charles to confirm that all of the content -- the HTML, the images, the... | <p>Have you tried View -> Zoom -> Reset on both sites?</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Python packages - import by class, not file | <p>Say I have the following file structure:</p>
<pre><code>app/
app.py
controllers/
__init__.py
project.py
plugin.py
</code></pre>
<p>If app/controllers/project.py defines a class Project, app.py would import it like this:</p>
<pre><code>from app.controllers.project import Project
</code></pre>
<p>I... | <p>You need to put</p>
<pre><code>from project import Project
</code></pre>
<p>in <code>controllers/__init__.py</code>.</p>
<p>Note that when <a href="http://www.python.org/dev/peps/pep-0328/" rel="nofollow">Absolute imports</a> become the default (Python 2.7?), you will want to add a dot before the module name (to ... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-2.7"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Where can I find the time and space complexity of the built-in sequence types in Python | <p>I've been unable to find a source for this information, short of looking through the Python source code myself to determine how the objects work. Does anyone know where I could find this online?</p>
| <p>Checkout the <a href="http://wiki.python.org/moin/TimeComplexity">TimeComplexity</a> page on the py dot org wiki. It covers set/dicts/lists/etc at least as far as time complexity goes.</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"dictionary"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Wacom tablet Python interface | <p>If possible I want to catch pressure sensitive input from a Wacom tablet in Python. Are there any Python libraries available that can do this?</p>
| <p>You could perhaps take a look at the <a href="http://www.alexmac.cc/tablet-apps/tablet-apps-0.3.1.tar.bz2" rel="nofollow">software</a> described <a href="http://www.alexmac.cc/tablet-apps/" rel="nofollow">here</a>. It is a gnome applet, written in Python.</p>
<p>From the web site:</p>
<p>"The gnome wacom applet is... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"flask"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Is there a Python library for generating .ico files? | <p>I'm looking to create <code>favicon.ico</code> files programatically from Python, but PIL only has support for reading <code>ico</code> files.</p>
| <p>Perhaps the following would work:</p>
<ul>
<li>Generate your icon image using PIL</li>
<li>Convert the image to .ico format using the python interface to ImageMagick, <a href="http://www.imagemagick.org/download/python/">PythonMagick</a></li>
</ul>
<p>I have not tried this approach. The ImageMagick convert command... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"flask"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How to know whether a window with a given title is already open in Tk? | <p>Iâve writen a little python script that just pops up a message box containing the text passed on the command line. I want to pop it up only when the window âresulting from a previous callâ is not open.</p>
<pre><code>from Tkinter import *
import tkMessageBox
root = Tk()
root.withdraw()
# TODO not if a wind... | <p>I believe you want:</p>
<pre><code>if 'normal' != root.state():
tkMessageBox.showinfo("Key you!", " ".join(sys.argv[1:]))
</code></pre>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Analizing MIPS binaries: is there a Python library for parsing binary data? | <p>I'm working on a utility which needs to resolve hex addresses to a symbolic function name and source code line number within a binary. The utility will run on Linux on x86, though the binaries it analyzes will be for a MIPS-based embedded system. The MIPS binaries are in ELF format, using DWARF for the symbolic debu... | <p>You might be interested in the DWARF library from <a href="http://code.google.com/p/pydevtools/" rel="nofollow">pydevtools</a>:</p>
<pre><code>>>> from bintools.dwarf import DWARF
>>> dwarf = DWARF('test/test')
>>> dwarf.get_loc_by_addr(0x8048475)
('/home/emilmont/Workspace/dbg/test/main.... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"arrays"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Which Version of Python to Use for Maximum Compatibility | <p>If I was going to start an open source project using Python what version should I use to ensure that the vast majority of users can use it on their system?</p>
<p>I'm the kind of person who quickly jumps to the next version (which I'll do when Python 3 comes out) but many people may be more conservative if their cu... | <p>As python is in kind of an transition phase towards python 3 with breaking backward compatibility I don't think it is a good idea to go python 3 only. Based on the <a href="http://www.python.org/dev/peps/pep-3000/#timeline" rel="nofollow">time line</a> there will be at least one or two following releases of the 2.x ... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"regex"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Django: Print url of view without hardcoding the url | <p>Can i print out a url <code>/admin/manage/products/add</code> of a certain view in a template?</p>
<p>Here is the rule i want to create a link for</p>
<pre><code>(r'^manage/products/add/$', create_object, {'model': Product, 'post_save_redirect': ''}),
</code></pre>
<p>I would like to have /manage/products/add in ... | <p>You can use <code>get_absolute_url</code>, but that will only work for a particular object. Since your object hasn't been created yet, it won't work in this case.</p>
<p>You want to use <a href="https://docs.djangoproject.com/en/1.2/topics/http/urls/#naming-url-patterns" rel="nofollow">named URL patterns</a>. Her... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do you set up a python wsgi server under IIS? | <p>I work in a windows environment and would prefer to deploy code to IIS. At the same time I would like to code in python.</p>
<p>Having read that IIS can run fastCGI application, I went to the IIS site where it describes in detail how to get PHP up and running but not much about anything else.</p>
<p>Does anyone ha... | <p>There shouldn't be any need to use FastCGI. There exists a <a href="https://github.com/hexdump42/isapi-wsgi">ISAPI extension for WSGI</a>.</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"google-app-engine"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Is there a way to attach a debugger to a multi-threaded Python process? | <p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p>
<p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
| <p>Use <a href="http://winpdb.org/">Winpdb</a>. It is a <strong>platform independent</strong> graphical GPL Python debugger with support for remote debugging over a network, multiple threads, namespace modification, embedded debugging, encrypted communication and is up to 20 times faster than pdb.</p>
<p>Features:</p>... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Generator Expressions vs. List Comprehension | <p>When should you use generator expressions and when should you use list comprehensions in Python?</p>
<pre><code># Generator expression
(x*2 for x in range(256))
# List comprehension
[x*2 for x in range(256)]
</code></pre>
| <p>John's answer is good (that list comprehensions are better when you want to iterate over something multiple times). However, it's also worth noting that you should use a list if you want to use any of the list methods. For example, the following code won't work:</p>
<pre><code>def gen():
return (something for... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"google-app-engine"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
User Authentication in Pylons + AuthKit | <p>I am trying to create a web application using Pylons and the resources on the web point to the <a href="http://pylonsbook.com/alpha1/authentication_and_authorization">PylonsBook</a> page which isn't of much help. I want authentication and authorisation and is there anyway to setup Authkit to work easily with Pylons?... | <p>I gave up on authkit and rolled my own:
<a href="http://tonylandis.com/openid-db-authentication-in-pylons-is-easy-with-rpx/" rel="nofollow">http://tonylandis.com/openid-db-authentication-in-pylons-is-easy-with-rpx/</a></p>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"linux"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What are the advantages of packaging your python library/application as an .egg file? | <p>I've read some about .egg files and I've noticed them in my lib directory but what are the advantages/disadvantages of using then as a developer?</p>
| <p>From the <a href="http://peak.telecommunity.com/DevCenter/PythonEggs">Python Enterprise Application Kit community</a>:</p>
<blockquote>
<p><em>"Eggs are to Pythons as Jars are to Java..."</em></p>
<p>Python eggs are a way of bundling
additional information with a Python
project, that allows the project's... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Glade or no glade: What is the best way to use PyGtk? | <p>I've been learning python for a while now with some success. I even managed to create one or two (simple) programs using PyGtk + Glade.</p>
<p>The thing is: I am not sure if the best way to use GTK with python is by building the interfaces using Glade.</p>
<p>I was wondering if the more experienced ones among us (... | <p>I would say that it depends: if you find that using Glade you can build the apps you want or need to make than that's absolutely fine. If however you actually want to learn how GTK works or you have some non-standard UI requirements you will <strong>have</strong> to dig into GTK internals (which are not that complic... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"regex"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Embedding a remote Python shell in an application | <p>You can embed the <a href="http://ipython.scipy.org/">IPython</a> shell inside of your application so that it launches the shell in the foreground. Is there a way to embed a telnet server in a python app so that you can telnet to a certain port and launch a remote IPython shell? </p>
<p>Any tips for redirecting t... | <p>Python includes a <a href="http://www.python.org/doc/lib/module-telnetlib.html" rel="nofollow">telnet client</a>, but not a telnet server. You can implement a telnet server using <a href="http://twistedmatrix.com" rel="nofollow">Twisted</a>. <a href="http://twistedmatrix.com/pipermail/twisted-python/2004-August/00... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Project structure for Google App Engine | <p>I started an application in Google App Engine right when it came out, to play with the technology and work on a pet project that I had been thinking about for a long time but never gotten around to starting. The result is <a href="http://www.bowlsk.com" rel="nofollow">BowlSK</a>. However, as it has grown, and feat... | <p>First, I would suggest you have a look at "<a href="http://sites.google.com/site/io/rapid-development-with-python-django-and-google-app-engine">Rapid Development with Python, Django, and Google App Engine</a>"</p>
<p>GvR describes a general/standard project layout on page 10 of his <a href="http://sites.google.com/... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"list"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do I implement a pre-commit hook script in SVN that calls dos2unix to validate checked-in file | <p>I was wondering if anyone here had some experience writing this type of script and if they could give me some pointers.</p>
<p>I would like to modify this <a href="http://wordaligned.org/articles/a-subversion-pre-commit-hook" rel="nofollow">script</a> to validate that the check-in file does not have a Carriage Retu... | <p>I think you can avoid a commit hook script in this case by using the <code>svn:eol-style</code> property as described in the SVNBook:</p>
<ul>
<li><p><a href="http://svnbook.red-bean.com/en/1.7/svn.advanced.props.file-portability.html#svn.advanced.props.special.eol-style" rel="nofollow">End-of-Line Character Sequen... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"linux"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Python: No module named core.exceptions | <p>I'm trying to get Google AppEngine to work on my Debian box and am getting the following error when I try to access my page:</p>
<pre><code><type 'exceptions.ImportError'>: No module named core.exceptions
</code></pre>
<p>The same app works fine for me when I run it on my other Ubuntu box, so I know it's not... | <p><code>core.exceptions</code> is part of django; what version of django do you have installed? The AppEngine comes with the appropriate version for whatever release you've downloaded (in the lib/django directory). It can be installed by going to that directory and running <strong><code>python setup.py install</code><... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"regex"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Calling python from a c++ program for distribution | <p>I would like to call python script files from my c++ program. </p>
<p>I am not sure that the people I will distribute to will have python installed.</p>
<p>Basically I'm looking for a .lib file that I can use that has an Apache like distribution license.</p>
| <blockquote>
<p>I would like to call python script files from my c++ program.</p>
</blockquote>
<p>This means that you want to embed Python in your C++ application. As mentioned in <a href="http://docs.python.org/extending/embedding.html">Embedding Python in Another Application</a>:</p>
<blockquote>
<p>Embedding ... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do I turn a python program into an .egg file? | <p>How do I turn a python program into an .egg file?</p>
| <p><a href="http://peak.telecommunity.com/DevCenter/setuptools">Setuptools</a> is the software that creates <a href="http://peak.telecommunity.com/DevCenter/PythonEggs">.egg files</a>. It's an extension of the <a href="http://docs.python.org/lib/module-distutils.html"><code>distutils</code></a> package in the standard ... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Can parallel traversals be done in MATLAB just as in Python? | <p>Using the <code>zip</code> function, Python allows for loops to traverse multiple sequences in parallel. </p>
<p><code>for (x,y) in zip(List1, List2):</code></p>
<p>Does MATLAB have an equivalent syntax? If not, what is the best way to iterate over two parallel arrays at the same time using MATLAB?</p>
| <p>If x and y are column vectors, you can do:</p>
<pre><code>for i=[x';y']
# do stuff with i(1) and i(2)
end
</code></pre>
<p>(with row vectors, just use <code>x</code> and <code>y</code>).</p>
<p>Here is an example run:</p>
<pre class="lang-matlab prettyprint-override"><code>>> x=[1 ; 2; 3;]
x =
1
... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How does one decrypt a PDF with an owner password, but no user password? | <p>Although the <a href="http://www.adobe.com/devnet/pdf/pdf_reference.html" rel="nofollow">PDF specification</a> is available from Adobe, it's not exactly the simplest document to read through. PDF allows documents to be encrypted so that either a user password and/or an owner password is required to do various thing... | <p>A plugin for GSview for viewing encrypted PDFs is <a href="http://www.cs.cmu.edu/~dst/Adobe/Gallery/Keating/" rel="nofollow">here</a>.</p>
<p>If this works for you, you may be able to look at the source.</p>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Java -> Python? | <p>Besides the dynamic nature of Python (and the syntax), what are some of the major features of the Python language that Java doesn't have, and vice versa?</p>
| <ol>
<li><p>List comprehensions. I often find myself filtering/mapping lists, and being able to say <code>[line.replace("spam","eggs") for line in open("somefile.txt") if line.startswith("nee")]</code> is really nice.</p></li>
<li><p>Functions are first class objects. They can be passed as parameters to other functio... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"matplotlib"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Open source alternative to MATLAB's fmincon function? | <p>Is there an open-source alternative to MATLAB's <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/optim/index.html?/access/helpdesk/help/toolbox/optim/ug/fmincon.html"><code>fmincon</code></a> function for constrained linear optimization? I'm rewriting a MATLAB program to use Python / <a href="http://nu... | <p>Is your problem convex? Linear? Non-linear? I agree that SciPy.optimize will probably do the job, but fmincon is a sort of bazooka for solving optimization problems, and you'll be better off if you can confine it to one of the categories below (in increasing level of difficulty to solve efficiently)</p>
<p>Linear... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"tkinter"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What Python way would you suggest to check whois database records? | <p>I'm trying to get a webservice up and running that actually requires to check whois databases. What I'm doing right now is ugly and I'd like to avoid it as much as I can: I call gwhois command and parse its output. Ugly.</p>
<p>I did some search to try to find a pythonic way to do this task. Generally I got quite m... | <p>Look at this:
<a href="http://code.google.com/p/pywhois/">http://code.google.com/p/pywhois/</a></p>
<p>pywhois - Python module for retrieving WHOIS information of domains</p>
<p>Goal:
- Create a simple importable Python module which will produce parsed WHOIS data for a given domain.
- Able to extract data for all ... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"matplotlib"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do I get the path and name of the file that is currently executing? | <p>I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process. </p>
<p>For example, let's say I have three files. Using <a href="http://docs.python.org/library/functions.html#execfile">execfile</a>:</p>
<ul>
<li><code>script_1.py</code> calls <cod... | <pre><code>__file__
</code></pre>
<p>as others have said. You may want to use: </p>
<pre><code>os.path.realpath(__file__)
</code></pre>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-2.7"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Django Sessions | <p>I'm looking at sessions in Django, and by default they are stored in the database. What are the benefits of filesystem and cache sessions and when should I use them?</p>
| <p>The filesystem backend is only worth looking at if you're not going to use a database for any other part of your system. If you are using a database then the filesystem backend has nothing to recommend it.</p>
<p>The memcache backend is much quicker than the database backend, but you run the risk of a session being... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do you create a weak reference to an object in Python? | <p>How do you create a weak reference to an object in Python? </p>
| <pre><code>>>> import weakref
>>> class Object:
... pass
...
>>> o = Object()
>>> r = weakref.ref(o)
>>> # if the reference is still active, r() will be o, otherwise None
>>> do_something_with_o(r())
</code></pre>
<p>See the <a href="http://docs.python.org/lib/... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"string"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What is the simplest way to find the difference between 2 times in python? | <p>I have 2 time values which have the type <code>datetime.time</code>. I want to find their difference. The obvious thing to do is t1 - t2, but this doesn't work. It works for objects of type <code>datetime.datetime</code> but not for <code>datetime.time</code>. So what is the best way to do this?</p>
| <p>Also a little silly, but you could try picking an arbitrary day and embedding each time in it, using <code>datetime.datetime.combine</code>, then subtracting:</p>
<pre><code>>>> import datetime
>>> t1 = datetime.time(2,3,4)
>>> t2 = datetime.time(18,20,59)
>>> dummydate = datetim... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"linux"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How to write a download progress indicator in Python? | <p>I am writing a little application to download files over http (as, for example, described <a href="http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776" rel="nofollow">here</a>).</p>
<p>I also want to include a little download progress indicator showing the percentage of th... | <p>There's a text progress bar library for python at <a href="http://pypi.python.org/pypi/progressbar/2.2">http://pypi.python.org/pypi/progressbar/2.2</a> that you might find useful: </p>
<blockquote>
<p>This library provides a text mode progressbar. This is tipically used to display the progress of a long running ... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"regex"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How can I retrieve the page title of a webpage using Python? | <p>How can I retrieve the page title of a webpage (title html tag) using Python?</p>
| <p>Here's a simplified version of <a href="http://stackoverflow.com/a/51242/4279">@Vinko Vrsalovic's answer</a>:</p>
<pre><code>import urllib2
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen("https://www.google.com"))
print soup.title.string
</code></pre>
<p>NOTE:</p>
<ul>
<li><p><em>so... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"arrays"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Passing on named variable arguments in python | <p>Say I have the following methods:</p>
<pre><code>def methodA(arg, **kwargs):
pass
def methodB(arg, *args, **kwargs):
pass
</code></pre>
<p>In methodA I wish to call methodB, passing on the kwargs. However, it seems that if I define <code>methodA</code> as follows, the second argument will be passed on as ... | <p>Put the asterisks before the kwargs variable. This makes Python pass the variable (which is assumed to be a dictionary) as keyword arguments.</p>
<pre><code>methodB("argvalue", **kwargs)
</code></pre>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"pandas"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Improving Python readability? | <p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo... | <p>Part of learning a new programming language is learning to read code in that language. A crutch like this may make it easier to read your own code, but it's going to impede the process of learning how to read anyone else's Python code. I really think you'd be better off getting rid of the end of block comments and... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"google-app-engine"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How to get an absolute file path in Python | <p>Given a path such as <code>"mydir/myfile.txt"</code>, how do I find the absolute filepath relative to the current working directory in Python? E.g. on Windows, I might end up with:</p>
<pre><code>"C:/example/cwd/mydir/myfile.txt"
</code></pre>
| <pre><code>>>> import os
>>> os.path.abspath("mydir/myfile.txt")
</code></pre>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"list"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP) | <p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p>
<pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples;
</code></pre>
<p>Is this normal behaviour when using a SQL da... | <p>I would say your test scheme is not really useful. To fulfill the db query, the db server goes through several steps:</p>
<ol>
<li>parse the SQL</li>
<li>work up a query plan, i. e. decide on which indices to use (if any), optimize etc.</li>
<li>if an index is used, search it for the pointers to the actual data, th... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Cross-platform space remaining on volume using python | <p>I need a way to determine the space remaining on a disk volume using python on linux, Windows and OS X. I'm currently parsing the output of the various system calls (df, dir) to accomplish this - is there a better way?</p>
| <pre><code>import ctypes
import os
import platform
import sys
def get_free_space_mb(dirname):
"""Return folder/drive free space (in megabytes)."""
if platform.system() == 'Windows':
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dirname), None, No... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"json"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Large Python Includes | <p>I have a file that I want to include in Python but the included file is fairly long and it'd be much neater to be able to split them into several files but then I have to use several include statements.</p>
<p>Is there some way to group together several files and include them all at once?</p>
| <ol>
<li>Put files in one folder. </li>
<li>Add __init__.py file to the folder. Do necessary imports in __init__.py</li>
<li><p>Replace multiple imports by one:</p>
<p>import folder_name </p></li>
</ol>
<p>See <a href="http://docs.python.org/tut/node8.html#SECTION008400000000000000000... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"list"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How can I do a line break (line continuation) in Python? | <p>I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?</p>
<p>For example, adding a bunch of strings,</p>
<pre><code>e = 'a' + 'b' + 'c' + 'd'
</code></pre>
<p>and have it like this:</p>
<pre><code>e = 'a' + 'b' +
'c' + 'd'
</code></pre>
| <p>What is the line? You can just have arguments on the next line without any problems:</p>
<pre><code>a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5,
blahblah6, blahblah7)
</code></pre>
<p>Otherwise you can do something like this:</p>
<pre><code>if a == True and \
b == False
</co... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do you check whether a python method is bound or not? | <p>Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?</p>
| <pre><code>def isbound(method):
return method.im_self is not None
def instance(bounded_method):
return bounded_method.im_self
</code></pre>
<p><a href="http://docs.python.org/ref/types.html">User-defined methods:</a></p>
<blockquote>
<p>When a user-defined method object is
created by retrieving a user-de... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"tkinter"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Best way to check if a list is empty | <p>For example, if passed the following:</p>
<pre><code>a = []
</code></pre>
<p>How do I check to see if <code>a</code> is empty?</p>
| <pre><code>if not a:
print("List is empty")
</code></pre>
<p>Using the implicit booleanness of the empty list <code>a</code> is quite pythonic.</p>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-2.7"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What are some strategies to write python code that works in CPython, Jython and IronPython | <p>Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?</p>
| <p>If you <em>do</em> find you need to write unique code for an environment, use pythons </p>
<pre><code>import mymodule_jython as mymodule
import mymodule_cpython as mymodule
</code></pre>
<p>have this stuff in a simple module (''module_importer''?) and write your code like this:</p>
<pre><code>from module_importe... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"linux"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do i generate a histogram for a given probability distribution (for functional testing a server)? | <p>I am trying to automate functional testing of a server using a realistic frequency distribution of requests. (sort of load testing, sort of simulation)</p>
<p>I've chosen the <a href="http://en.wikipedia.org/wiki/Weibull_distribution" rel="nofollow">Weibull</a> distribution as it "sort of" matches the distribution ... | <p>Why don't you try <a href="http://grinder.sourceforge.net" rel="nofollow">The Grinder 3</a> to load test your server, it comes with all this and more prebuilt, and it supports python as a scripting language</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"json"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Any good AJAX framework for Google App Engine apps? | <p>I am trying to implement AJAX in my Google App Engine application, and so I am looking for a good AJAX framework that will help me. Anyone has any idea?</p>
<p>I am thinking about Google Web Toolkit, how good it is in terms of creating AJAX for Google App Engine?</p>
| <p>As Google Web Toolkit is a <a href="http://code.google.com/support/bin/answer.py?answer=54830&topic=10208" rel="nofollow">subset of Java</a> it works best when you Java at the backend too. Since Google App Engine is currently <a href="http://code.google.com/appengine/kb/general.html#language" rel="nofollow">Pyt... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"html"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
What is the difference between old style and new style classes in Python? | <p>What is the difference between old style and new style classes in Python? Is there ever a reason to use old-style classes these days?</p>
| <p>From <a href="http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes">http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes</a> :</p>
<blockquote>
<p>Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
In Python, how can you easily retrieve sorted items from a dictionary? | <p>Dictionaries unlike lists are not ordered (and do not have the 'sort' attribute). Therefore, you can not rely on getting the items in the same order when first added. </p>
<p>What is the easiest way to loop through a dictionary containing strings as the key value and retrieving them in ascending order by key?</p>
... | <p>Do you mean that you need the values sorted by the value of the key?
In that case, this should do it:</p>
<pre><code>for key in sorted(d):
print d[key]
</code></pre>
<p><strong>EDIT:</strong> changed to use sorted(d) instead of sorted(d.keys()), thanks <a href="http://stackoverflow.com/users/1694/eli-courtwrig... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 1,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How can I get Emacs' key bindings in Python's IDLE? | <p>I use Emacs primarily for coding Python but sometimes I use IDLE. Is there a way to change the key bindings easily in IDLE to match Emacs?</p>
| <p>IDLE provides Emacs keybindings without having to install other software. </p>
<ol>
<li>Open up the menu item Options -> Configure IDLE...</li>
<li>Go to Keys tab</li>
<li>In the drop down menu on the right
side of the dialog change the select
to "IDLE Classic Unix"</li>
</ol>
<p>It's not the true emacs key bindin... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"numpy"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Python regular expression for HTML parsing (BeautifulSoup) | <p>I want to grab the value of a hidden input field in HTML.</p>
<pre><code><input type="hidden" name="fooId" value="12-3456789-1111111111" />
</code></pre>
<p>I want to write a regular expression in Python that will return the value of fooId, given that I know the line in the HTML follows the format</p>
<pre>... | <p>For this particular case, BeautifulSoup is harder to write than a regex, but it is much more robust... I'm just contributing with the BeautifulSoup example, given that you already know which regexp to use :-)</p>
<pre><code>from BeautifulSoup import BeautifulSoup
#Or retrieve it from the web, etc.
html_data = ope... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-2.7"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
XML writing tools for Python | <p>I'm currently trying ElementTree and it looks fine, it escapes HTML entities and so on and so forth. Am I missing something truly wonderful I haven't heard of?</p>
<p>This is similar to what I'm actually doing:</p>
<pre><code>import xml.etree.ElementTree as ET
root = ET.Element('html')
head = ET.SubElement(root,'h... | <p>Another way is using the <a href="http://codespeak.net/lxml/tutorial.html#the-e-factory" rel="nofollow">E Factory</a> builder from lxml (available in <a href="http://effbot.org/zone/element-builder.htm" rel="nofollow">Elementtree</a> too)</p>
<pre><code>>>> from lxml import etree
>>> from lxml.bu... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"arrays"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Anyone used Dabo for a medium-big project? | <p>We're at the beginning of a new ERP-ish client-server application, developed as a Python rich client. We're currently evaluating Dabo as our main framework and it looks quite nice and easy to use, but I was wondering, has anyone used it for medium-to-big sized projects?<br />
Thanks for your time!</p>
| <p>I'm one of the authors of the Dabo framework. One of our users pointed out to me the extremely negative answer you received, and so I thought I had better chime in and clear up some of the incorrect assumptions in the first reply.</p>
<p>Dabo is indeed well-known in the Python community. I have presented it at 3 of... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"csv"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
round() in Python doesn't seem to be rounding properly | <p>The documentation for the <a href="http://docs.python.org/lib/built-in-funcs.html">round()</a> function states that you pass it a number, and the positions past the decimal to round. Thus it <em>should</em> do this:</p>
<pre><code>n = 5.59
round(n, 1) # 5.6
</code></pre>
<p>But, in actuality, good old floating poi... | <p>Formatting works correctly even without having to round:</p>
<pre><code>"%.1f" % n
</code></pre>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"pandas"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 4,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Convert XML/HTML Entities into Unicode String in Python | <p>I'm doing some web scraping and sites frequently use HTML entities to represent non ascii characters. Does Python have a utility that takes a string with HTML entities and returns a unicode type?</p>
<p>For example:</p>
<p>I get back:</p>
<pre><code>&#x01ce;
</code></pre>
<p>which represents an "Ç" with a t... | <p>Python has the <a href="https://docs.python.org/2/library/htmllib.html#module-htmlentitydefs">htmlentitydefs</a> module, but this doesn't include a function to unescape HTML entities.</p>
<p>Python developer Fredrik Lundh (author of elementtree, among other things) has such a function <a href="http://effbot.org/zo... | [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"regex"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 5,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Does re.compile() or any given Python library call throw an exception? | <p>I can't tell from the Python documentation whether the re.compile(x) function may throw an exception (assuming you pass in a string). I imagine there is something that could be considered an invalid regular expression. The larger question is, where do I go to find if a given Python library call may throw exception(s... | <p>Well, <code>re.compile</code> certainly may:</p>
<pre><code>>>> import re
>>> re.compile('he(lo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\lib\re.py", line 180, in compile
return _compile(pattern, flags)
File "C:\Python25\lib\re... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How do I get the external IP of a socket in Python? | <p>When I call <code>socket.getsockname()</code> on a socket object, it returns a tuple of my machine's internal IP and the port. However, I would like to retrieve my external IP. What's the cheapest, most efficient manner of doing this?</p>
| <p>This isn't possible without cooperation from an external server, because there could be any number of NATs between you and the other computer. If it's a custom protocol, you could ask the other system to report what address it's connected to.</p>
| [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"arrays"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 2,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Can I document Python code with doxygen (and does it make sense)? | <p>I like doxygen to create documentation of C or PHP code. I have an upcoming Python project and I think I remember that Python doesn't have /* .. */ comments and also has its own self-documentation facility which seems to be the pythonic way to document.</p>
<p>Can I just use doxygen? Anything particular to be aware... | <p>The <a href="https://pypi.python.org/pypi/doxypy/">doxypy</a> input filter allows you to use pretty much all of Doxygen's formatting tags in a standard Python docstring format. I use it to document a large mixed C++ and Python game application framework, and it's working well.</p>
| [
{
"user_id": null,
"value": "no",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"flask"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
How would you design a very "Pythonic" UI framework? | <p>I have been playing with the Ruby library "shoes". Basically you can write a GUI application in the following way:</p>
<pre><code>Shoes.app do
t = para "Not clicked!"
button "The Label" do
alert "You clicked the button!" # when clicked, make an alert
t.replace "Clicked!" # ..and replace the label's text... | <p>You could actually pull this off, but it would require using metaclasses, which are <em>deep</em> magic (there be dragons). If you want an intro to metaclasses, there's a series of <a href="http://www.ibm.com/developerworks/linux/library/l-pymeta.html" rel="nofollow">articles from IBM</a> which manage to introduce t... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"python-2.7"
],
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": 3,
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": "Sample answer",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | null | {} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.