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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How can I find the full path to a font from its display name on a Mac? | <p>I am using the Photoshop's javascript API to find the fonts in a given PSD.</p>
<p>Given a font name returned by the API, I want to find the actual physical font file that that font name corresponds to on the disc.</p>
<p>This is all happening in a python program running on OSX so I guess I'm looking for one of:</... | <p>Unfortunately the only API that isn't deprecated is located in the ApplicationServices framework, which doesn't have a bridge support file, and thus isn't available in the bridge. If you're wanting to use ctypes, you can use ATSFontGetFileReference after looking up the ATSFontRef.</p>
<p>Cocoa doesn't have any na... | [
{
"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 | {} |
Get a preview JPEG of a PDF on Windows? | <p>I have a cross-platform (Python) application which needs to generate a JPEG preview of the first page of a PDF.</p>
<p>On the Mac I am spawning <a href="http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/sips.1.html">sips</a>. Is there something similarly simple I can do on Windows?</p>
| <p>ImageMagick delegates the PDF->bitmap conversion to GhostScript anyway, so here's a command you can use (it's based on the actual command listed by the <code>ps:alpha</code> delegate in ImageMagick, just adjusted to use JPEG as output):</p>
<pre><code>gs -q -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT \
-dM... | [
{
"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": 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 | {} |
Continuous Integration System for a Python Codebase | <p>I'm starting work on a hobby project with a python codebase and would like to set up some form of continuous integration (i.e. running a battery of test-cases each time a check-in is made and sending nag e-mails to responsible persons when the tests fail) similar to CruiseControl or TeamCity.</p>
<p>I realize I cou... | <p>One possibility is Hudson. It's written in Java, but there's integration with Python projects:</p>
<blockquote>
<p><a href="http://redsolo.blogspot.com/2007/11/hudson-embraces-python.html" rel="nofollow">Hudson embraces Python</a></p>
</blockquote>
<p>I've never tried it myself, however.</p>
<p>(<strong>Update... | [
{
"user_id": null,
"value": "no",
"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 | {} |
cx_Oracle: How do I iterate over a result set? | <p>There are several ways to iterate over a result set. What are the tradeoff of each?</p>
| <p>The canonical way is to use the built-in cursor iterator.</p>
<pre><code>curs.execute('select * from people')
for row in curs:
print row
</code></pre>
<hr>
<p>You can use <code>fetchall()</code> to get all rows at once.</p>
<pre><code>for row in curs.fetchall():
print row
</code></pre>
<p>It can be conv... | [
{
"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 | {} |
Using 'in' to match an attribute of Python objects in an array | <p>I don't remember whether I was dreaming or not but I seem to recall there being a function which allowed something like,</p>
<pre><code>foo in iter_attr(array of python objects, attribute name)</code></pre>
<p>I've looked over the docs but this kind of thing doesn't fall under any obvious listed headers</p> | <p>Using a list comprehension would build a temporary list, which could eat all your memory if the sequence being searched is large. Even if the sequence is not large, building the list means iterating over the whole of the sequence before <code>in</code> could start its search.</p>
<p>The temporary list can be avoidi... | [
{
"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 | {} |
Class views in Django | <p><a href="http://www.djangoproject.com/">Django</a> view points to a function, which can be a problem if you want to change only a bit of functionality. Yes, I could have million keyword arguments and even more if statements in the function, but I was thinking more of an object oriented approach.</p>
<p>For exampl... | <p>I've created and used my own generic view classes, defining <strong><code>__call__</code></strong> so an instance of the class is callable. I really like it; while Django's generic views allow some customization through keyword arguments, OO generic views (if their behavior is split into a number of separate method... | [
{
"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 | {} |
Python and MySQL | <p>I can get Python to work with Postgresql but I cannot get it to work with MySQL. The main problem is that on the shared hosting account I have I do not have the ability to install things such as Django or PySQL, I generally fail when installing them on my computer so maybe it's good I can't install on the host.</p>
... | <p>MySQLdb is what I have used before.</p>
<p>If you host is using Python version 2.5 or higher, support for sqlite3 databases is built in (sqlite allows you to have a relational database that is simply a file in your filesystem). But buyer beware, sqlite is not suited for production, so it may depend what you are ... | [
{
"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 do I use Python's itertools.groupby()? | <p>I haven't been able to find an understandable explanation of how to actually use Python's <code>itertools.groupby()</code> function. What I'm trying to do is this:</p>
<ul>
<li>Take a list - in this case, the children of an objectified <code>lxml</code> element</li>
<li>Divide it into groups based on some criteria... | <p>As Sebastjan said, <strong>you first have to sort your data. This is important.</strong></p>
<p>The part I didn't get is that in the example construction</p>
<pre><code>groups = []
uniquekeys = []
for k, g in groupby(data, keyfunc):
groups.append(list(g)) # Store group iterator as a list
uniquekeys.append... | [
{
"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": 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 | {} |
Adding a Method to an Existing Object Instance | <p>I've read that it is possible to add a method to an existing object (e.g. not in the class definition) in <strong>Python</strong>, I think this is called <em>Monkey Patching</em> (or in some cases <em>Duck Punching</em>). I understand that it's not always a good decision to do so. But, how might one do this?</p>
<p... | <p>In Python, there is a difference between functions and bound methods.</p>
<pre><code>>>> def foo():
... print "foo"
...
>>> class A:
... def bar( self ):
... print "bar"
...
>>> a = A()
>>> foo
<function foo at 0x00A98D70>
>>> a.bar
<bound method... | [
{
"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 do you express binary literals in Python? | <p>How do you express an integer as a binary number with Python literals?</p>
<p>I was easily able to find the answer for hex:</p>
<pre><code> >>> 0x12AF
4783
>>> 0x100
256
</code></pre>
<p>and octal:</p>
<pre><code> >>> 01267
695
>>> 0100
64
</code>... | <p>For reference—<em>future</em> Python possibilities:<br />
Starting with Python 2.6 you can express binary literals using the prefix <strong>0b</strong> or <strong>0B</strong>:</p>
<pre><code>>>> 0b101111
47
</code></pre>
<p>You can also use the new <strong>bin</strong> function to get the binary rep... | [
{
"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": 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 | {} |
Any experiences with Protocol Buffers? | <p>I was just looking through some information about Google's <a href="http://code.google.com/apis/protocolbuffers/">protocol buffers</a> data interchange format. Has anyone played around with the code or even created a project around it?</p>
<p>I'm currently using XML in a Python project for structured content cre... | <p>Protocol buffers are intended to optimize communications between machines. They are really not intended for human interaction. Also, the format is binary, so it could not replace XML in that use case. </p>
<p>I would also recommend <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> as being the most compact tex... | [
{
"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": 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 make a menu that does not require the user to press [enter] to make a selection? | <p>I've got a menu in Python. That part was easy. I'm using <code>raw_input()</code> to get the selection from the user. </p>
<p>The problem is that <code>raw_input</code> (and input) require the user to press <kbd>Enter</kbd> after they make a selection. Is there any way to make the program act immediately upon a key... | <p><strong>On Linux:</strong></p>
<ul>
<li>set raw mode</li>
<li>select and read the keystroke</li>
<li>restore normal settings</li>
</ul>
<pre>
import sys
import select
import termios
import tty
def getkey():
old_settings = termios.tcgetattr(sys.stdin)
tty.setraw(sys.stdin.fileno())
select.select([sys.s... | [
{
"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 to check what OS am I running on in Python? | <p>What do I need to look at to see if I'm on Windows, Unix, etc?</p>
| <pre><code>>>> import os
>>> print os.name
posix
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'
</code></pre>
<p>See: <a href="https://docs.python.org/2/library/platform.html">platform â Access to underlying platformâs ident... | [
{
"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": 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: what is the difference between (1,2,3) and [1,2,3], and when should I use each? | <p>In many places, <code>(1,2,3)</code> and <code>[1,2,3]</code> can be used interchangeably.</p>
<p>When should I use one or the other, and why?</p>
| <p>From the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">Python FAQ</a>:</p>
<blockquote>
<p>Lists and tuples, while similar in many respects, are generally used in fundamentally different ways. Tuples can be thought of as being similar to Pascal records or C s... | [
{
"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": 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 | {} |
File size differences after copying a file to a server vÃa FTP | <p>I have created a PHP-script to update a webserver that is live inside a local directory.
I'm migrating the script into Python. It works fine for the most part, but after a PUT command the size of the file appears to change. Thus, the size of the file is different from that of the file on the server. </p>
<p>Once I ... | <p>Do you need to open the locfile in binary using <code>rb</code>?</p>
<pre><code>f = open (locfile, "rb")
</code></pre>
| [
{
"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": 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 create a directly-executable cross-platform GUI app using Python? | <p>Python works on multiple platforms and can be used for desktop and web applications, thus I conclude that there is some way to compile it into an executable for Mac, Windows and Linux.</p>
<p>The problem being I have no idea where to start or how to write a GUI with it, can anybody shed some light on this and poi... | <p>First you will need some GUI library with Python bindings and then (if you want) some program that will convert your python scripts into standalone executables.</p>
<p><strong>Cross-platform GUI libraries with Python bindings (Windows, Linux, Mac)</strong></p>
<p>Of course, there are many, but the most popular tha... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"numpy",
"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 | {} |
Calling a function of a module from a string with the function's name in Python | <p>What is the best way to go about calling a function given a string with the function's name in a Python program. For example, let's say that I have a module <code>foo</code>, and I have a string whose contents are <code>"bar"</code>. What is the best way to go about calling <code>foo.bar()</code>?</p>
<p>I need to... | <p>Assuming module <code>foo</code> with method <code>bar</code>:</p>
<pre><code>import foo
methodToCall = getattr(foo, 'bar')
result = methodToCall()
</code></pre>
<p>As far as that goes, lines 2 and 3 can be compressed to:</p>
<pre><code>result = getattr(foo, 'bar')()
</code></pre>
<p>if that makes more sense for... | [
{
"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": 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 | {} |
Programmatically talking to a Serial Port in OS X or Linux | <p>I have a Prolite LED sign that I like to set up to show scrolling search queries from a apache logs and other fun statistics. The problem is, my G5 does not have a serial port, so I have to use a usb to serial dongle. It shows up as /dev/cu.usbserial and /dev/tty.usbserial . </p>
<p>When i do this everything seems ... | <p><code>/dev/cu.xxxxx</code> is the "callout" device, it's what you use when you establish a connection to the serial device and start talking to it. <code>/dev/tty.xxxxx</code> is the "dialin" device, used for monitoring a port for incoming calls for e.g. a fax listener.</p> | [
{
"user_id": null,
"value": "no",
"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 to sell Python to a client/boss/person with lots of cash | <p>When asked to create system XYZ and you ask to do it in Python over PHP or Ruby, what are the main features you can mention when they require you to explain it?</p> | <p>This is one of those cases that really boil down to personal preference or situational details. If you're more comfortable and experienced with Python, then say so. Are they asking you to justify it because they're more comfortable with one of the other environments? After you're done, will the system be passed off ... | [
{
"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": 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 set up Python scripts to work in Apache 2.0? | <p>I tried to follow a couple of googled up tutorials on setting up mod_python, but failed every time. Do you have a good, step-by step, rock-solid howto?</p>
<p>My dev box is OS X, production - Centos.</p> | <p>There are two main ways of running Python on Apache. The simplest would be to use CGI and write normal Python scripts while the second is using a web framework like Django or Pylons.</p>
<p>Using CGI is straightforward. Make sure your Apache config file has a cgi-bin set up. If not, follow their documentation (<a h... | [
{
"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": 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 | {} |
Does anyone have experience creating a shared library in MATLAB? | <p>A researcher has created a small simulation in MATLAB, and we want to make it accessible to others. My plan is to take the simulation, clean up a few things, and turn it into a set of functions. Then, I plan to compile it into a C library and use SWIG to create a Python wrapper. At that point, I should be able to ca... | <p>One thing to remember is that the Matlab compiler does not actually compile the Matlab code into native machine instructions. It simply wraps it into a standalone executable or a library with its own runtime engine that runs it. You would be able to run your code without Matlab installed, and you would be able to ... | [
{
"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 | {} |
Cross Platform, Language Agnostic GUI Markup Language? | <p>I learned Swing back in the day but now I've moved to Python and want to make some apps with GUIs. I haven't had the time to learn a new GUI API so I've been using Jython, but I would prefer to use CPython. </p>
<p>It would be great if I can have one simple markup that allows me to switch GUI libraries. It would ... | <p>erm.. HTML? (trying to be funny here... while we wait for real answers..)</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": 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 | {} |
Convert Bytes to Floating Point Numbers in Python | <p>I have a binary file that I have to parse and I'm using Python. Is there a way to take 4 bytes and convert it to a single precision floating point number?</p> | <pre><code>>>> import struct
>>> struct.pack('f', 3.141592654)
b'\xdb\x0fI@'
>>> struct.unpack('f', b'\xdb\x0fI@')
(3.1415927410125732,)
>>> struct.pack('4f', 1.0, 2.0, 3.0, 4.0)
'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'
</code></pre>
| [
{
"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": 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, Unicode, and the Windows console | <p>When I try to print a Unicode string in a Windows console, I get a <code>UnicodeEncodeError: 'charmap' codec can't encode character ....</code> error. I assume this is because the Windows console does not accept Unicode-only characters. What's the best way around this? Is there any way I can make Python automatical... | <p><strong>Note:</strong> This answer is sort of outdated (from 2008). Please use the solution below with care!!</p>
<hr>
<p>Here is a page that details the problem and a solution (search the page for the text <em>Wrapping sys.stdout into an instance</em>):</p>
<p><a href="http://wiki.python.org/moin/PrintFails">Pri... | [
{
"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": 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 | {} |
Get size of a file before downloading in Python | <p>I'm downloading an entire directory from a web server. It works OK, but I can't figure how to get the file size before download to compare if it was updated on the server or not. Can this be done as if I was downloading the file from a FTP server?</p>
<pre><code>import urllib
import re
url = "http://www.someurl.co... | <p>I have reproduced what you are seeing:</p>
<pre><code>import urllib, os<br>link = "http://python.org"<br>print "opening url:", link<br>site = urllib.urlopen(link)<br>meta = site.info()<br>print "Content-Length:", meta.getheaders("Content-Length")[0]<br><br>f = open("out.txt", "r")<br>print "File on disk:",len(f.r... | [
{
"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": 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 | {} |
Best way to abstract season/show/episode data | <p>Basically, I've written an API to www.thetvdb.com in Python. The current code can be found <a href="http://github.com/dbr/tvdb_api/tree/master/tvdb_api.py">here</a>.</p>
<p>It grabs data from the API as requested, and has to store the data somehow, and make it available by doing:</p>
<pre><code>print tvdbinsta... | <p>OK, what you need is <code>classobj</code> from new module. That would allow you to construct exception classes dynamically (<code>classobj</code> takes a string as an argument for the class name). </p>
<pre><code>import new
myexc=new.classobj("ExcName",(Exception,),{})
i=myexc("This is the exc msg!")
raise i
</cod... | [
{
"user_id": null,
"value": "no",
"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 | {} |
Pylons error - 'MySQL server has gone away' | <p>[I hope this isn't too obscure… I'll ask the newsgroup if nobody knows here]</p>
<p>I'm using Pylons (a python framework) to serve a simple web application, but it seems to die from time to time, with this in the error log: <code>(2006, 'MySQL server has gone away')</code></p>
<p>I did a bit of checking, an... | <p>I think I fixed it. It's turns out I had a simple config error. My ini file read:</p>
<pre><code>sqlalchemy.default.url = [connection string here]
sqlalchemy.pool_recycle = 1800
</code></pre>
<p>The problem is that my <code>environment.py</code> file declared that the engine would only map keys with the prefix: <c... | [
{
"user_id": null,
"value": "no",
"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": 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 use Xpath in Python? | <p>What is the library? Is there a full implementation? How is the library used? Where is its website?</p>
| <p><a href="http://xmlsoft.org/python.html">libxml2</a> has a number of advantages:</p>
<ol>
<li>Compliance to the <a href="http://www.w3.org/TR/xpath">spec</a></li>
<li>Active development and a community participation </li>
<li>Speed. This is really a python wrapper around a C implementation. </li>
<li>Ubiquity. The ... | [
{
"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": 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 | {} |
Accessing mp3 Meta-Data with Python | <p>What is the best way to retrieve mp3 metadata in python? I've seen a couple frameworks out there, but I'm unsure as to which would be the best to use.... Any ideas?</p>
| <p>I used <a href="http://eyed3.nicfit.net/">eyeD3</a> the other day with a lot of success. I found that it could add artwork to the ID3 tag which the other modules I looked at couldn't. You'll have to download the tar and execute <code>python setup.py install</code> from the source folder. </p>
<p>Relevant example... | [
{
"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": 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 do I treat an integer as an array of bytes in Python? | <p>I'm trying to decode the result of the Python os.wait() function. This returns, according to the Python docs:</p>
<blockquote>
<p>a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the sign... | <p>This will do what you want:</p>
<pre><code>signum = status & 0xff
exitstatus = (status & 0xff00) >> 8
</code></pre>
| [
{
"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 should I unit test a code-generator? | <p>This is a difficult and open-ended question I know, but I thought I'd throw it to the floor and see if anyone had any interesting suggestions.</p>
<p>I have developed a code-generator that takes our python interface to our C++ code (generated via SWIG) and generates code needed to expose this as WebServices. When ... | <p>I started writing up a summary of my experience with my own code generator, then went back and re-read your question and found you had already touched upon the same issues yourself, focus on the execution results instead of the code layout/look.</p>
<p>Problem is, this is hard to test, the generated code might not ... | [
{
"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 | {} |
Using an XML catalog with Python's lxml? | <p>Is there a way, when I parse an XML document using lxml, to validate that document against its DTD using an external catalog file? I need to be able to work the fixed attributes defined in a documentâs DTD.</p>
| <p>You can add the catalog to the <code>XML_CATALOG_FILES</code> environment variable:</p>
<pre><code>os.environ['XML_CATALOG_FILES'] = 'file:///to/my/catalog.xml'
</code></pre>
<p>See <a href="http://thread.gmane.org/gmane.comp.python.lxml.devel/5907" rel="nofollow">this thread</a>. Note that entries in <code>XML_CA... | [
{
"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 | {} |
Can you check that an exception is thrown with doctest in Python? | <p>Is it possible to write a doctest unit test that will check that an exception is raised?<br>
For example, if I have a function <code>foo(x)</code> that is supposed to raise an exception if <code>x<0</code>, how would I write the doctest for that? </p>
| <p>Yes. You can do it. The <a href="https://docs.python.org/3/library/doctest.html">doctest module documentation</a> and Wikipedia has an <a href="http://en.wikipedia.org/wiki/Doctest#Example_2%3a_doctests_embedded_in_a_README.txt_file">example</a> of it.</p>
<pre><code> >>> x
Traceback (most recent call... | [
{
"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": 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 | {} |
Replacement for for... if array iteration | <p>I love list comprehensions in Python, because they concisely represent a transformation of a list.</p>
<p>However, in other languages, I frequently find myself writing something along the lines of:</p>
<pre><code>foreach (int x in intArray)
if (x > 3) //generic condition on x
x++
//do other processin... | <p>The increment in the original <code>foreach</code> loop will not affect the contents of the array, the only way to do this remains a <code>for</code> loop:</p>
<pre><code>for(int i = 0; i < intArray.Length; ++i)
{
if(intArray[i] > 3) ++intArray[i];
}
</code></pre>
<p>Linq is not intended to modify existi... | [
{
"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 | {} |
Python version of PHP's stripslashes | <p>I wrote a piece of code to convert PHP's striplashes into valid Python [backslash] escapes:</p>
<pre><code>cleaned = stringwithslashes
cleaned = cleaned.replace('\\n', '\n')
cleaned = cleaned.replace('\\r', '\n')
cleaned = cleaned.replace('\\', '')
</code></pre>
<p>How can I condense it? </p>
| <p>Not totally sure this is what you want, but..</p>
<pre><code>cleaned = stringwithslashes.decode('string_escape')
</code></pre>
| [
{
"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": 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 Expressions to implement string unescaping | <p>I'm trying to implement string unescaping with Python regex and backreferences, and it doesn't seem to want to work very well. I'm sure it's something I'm doing wrong but I can't figure out what...</p>
<pre><code>>>> import re
>>> mystring = r"This is \n a test \r"
>>> p = re.compile( "\\... | <p>Isn't that what <a href="#13844">Anders' second example</a> does?</p>
<p>In 2.5 there's also a <code>string-escape</code> encoding you can apply:</p>
<pre><code>>>> mystring = r"This is \n a test \r"
>>> mystring.decode('string-escape')
'This is \n a test \r'
>>> print mystring.decode('s... | [
{
"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 | {} |
Can you explain closures (as they relate to Python)? | <p>I've been reading a lot about closures and I think I understand them, but without clouding the picture for myself and others, I am hoping someone can explain closures as succinctly and clearly as possible. I'm looking for a simple explanation that might help me understand where and why I would want to use them.</p>... | <p><a href="http://mrevelle.blogspot.com/2006/10/closure-on-closures.html">Closure on closures</a></p>
<blockquote>
<p>Objects are data with methods
attached, closures are functions with
data attached.</p>
</blockquote>
<pre><code>def make_counter():
i = 0
def counter(): # counter() is a closure
... | [
{
"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": 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 Sound ("Bell") | <p>I'd like to have a python program alert me when it has completed its task by making a beep noise. Currently, I use <code>import os</code> and then use a command line speech program to say "Process complete." I much rather it be a simple "bell."</p>
<p>I know that there's a function that can be used in Cocoa apps... | <p>Have you tried :</p>
<pre><code>import sys
sys.stdout.write('\a')
sys.stdout.flush()
</code></pre>
<p>That works for me here on Mac OS 10.5</p>
<p>Actually, I think your original attempt works also with a little modification:</p>
<pre><code>print('\a')
</code></pre>
<p>(You just need the single quotes around th... | [
{
"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": 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 | {} |
Is there a python module for regex matching in zip files | <p>I have over a million text files compressed into 40 zip files. I also have a list of about 500 model names of phones. I want to find out the number of times a particular model was mentioned in the text files. </p>
<p>Is there any python module which can do a regex match on the files without unzipping it. Is there a... | <p>There's nothing that will automatically do what you want.</p>
<p>However, there is a python zipfile module that will make this easy to do. Here's how to iterate over the lines in the file.</p>
<pre><code>#!/usr/bin/python
import zipfile
f = zipfile.ZipFile('myfile.zip')
for subfile in f.namelist():
print su... | [
{
"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": 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 | {} |
Regex and unicode | <p>I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example), grabs the episode name (from the www.thetvdb.com API) and automatically renames them into something nicer (Show Name - [01x02].avi)</p>
<p>The script works fine, that is until you try and use it on files that have Unicode s... | <p>Use a subrange of [\u0000-\uFFFF] for what you want.</p>
<p>You can also use the re.UNICODE compile flag. <a href="http://docs.python.org/lib/re-syntax.html">The docs</a> say that if UNICODE is set, \w will match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties d... | [
{
"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": 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 validate xml against a DTD file in Python | <p>I need to validate an XML string (and not a file)
against a DTD description file. </p>
<p>How can that be done in <code>python</code>?</p>
| <p>Another good option is <a href="http://lxml.de/validation.html" rel="nofollow">lxml's validation</a> which I find quite pleasant to use.</p>
<p>A simple example taken from the lxml site:</p>
<pre><code>from StringIO import StringIO
from lxml import etree
dtd = etree.DTD(StringIO("""<!ELEMENT foo EMPTY>""")... | [
{
"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": 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 | {} |
Prototyping with Python code before compiling | <p>I have been mulling over writing a peak fitting library for a while. I know Python fairly well and plan on implementing everything in Python to begin with but envisage that I may have to re-implement some core routines in a compiled language eventually.</p>
<p>IIRC, one of Python's original remits was as a prototyp... | <p>Finally a question that I can really put a value answer to :). </p>
<p>I have investigated f2py, boost.python, swig, cython and pyrex for my work (PhD in optical measurement techniques). I used swig extensively, boost.python some and pyrex and cython a lot. I also used ctypes. This is my breakdown:</p>
<p><strong>... | [
{
"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": 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 | {} |
Sanitising user input using Python | <p>What's the best way to sanitise user input for a Python-based web application? Is there a single function to remove HTML characters and any other necessary characters combinations to prevent an <a href="http://en.wikipedia.org/wiki/Cross-site_scripting" rel="nofollow">XSS</a> or SQL injection attack?</p>
| <p>Here is a snippet that will remove all tags not on the white list, and all tag attributes not on the attribues whitelist (so you can't use <code>onclick</code>).</p>
<p>It is a modified version of <a href="http://www.djangosnippets.org/snippets/205/">http://www.djangosnippets.org/snippets/205/</a>, with the regex o... | [
{
"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": 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 | {} |
Create an encrypted ZIP file in Python | <p>I'm creating an ZIP file with ZipFile in Python 2.5, it works ok so far:</p>
<pre><code>import zipfile, os
locfile = "test.txt"
loczip = os.path.splitext (locfile)[0] + ".zip"
zip = zipfile.ZipFile (loczip, "w")
zip.write (locfile)
zip.close()
</code></pre>
<p>but I couldn't find how to encrypt the files in the Z... | <p>I created a simple library to create a password encrypted zip file in python. - <a href="https://github.com/smihica/pyminizip"><strong>here</strong></a></p>
<pre><code>import pyminizip
compression_level = 5 # 1-9
pyminizip.compress("src.txt", "dst.zip", "password", compression_level)
</code></pre>
<p><strong>The ... | [
{
"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 | {} |
What's the best way to distribute python command-line tools? | <p>My current setup.py script works okay, but it installs tvnamer.py (the tool) as "tvnamer.py" into site-packages or somewhere similar..</p>
<p>Can I make setup.py install tvnamer.py as tvnamer, and/or is there a better way of installing command-line applications?</p>
| <p>Try the <code>entry_points.console_scripts</code> parameter in the setup() call. As described in the <a href="http://peak.telecommunity.com/DevCenter/setuptools#automatic-script-creation">setuptools docs</a>, this should do what I think you want.</p>
<p>To reproduce here:</p>
<pre><code>from setuptools import setu... | [
{
"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": 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 check set of files conform to a naming scheme | <p>I have a bunch of files (TV episodes, although that is fairly arbitrary) that I want to check match a specific naming/organisation scheme..</p>
<p>Currently: I have three arrays of regex, one for valid filenames, one for files missing an episode name, and one for valid paths.</p>
<p>Then, I loop though each valid-... | <blockquote>
<p>I want to add a rule that checks for
the presence of a folder.jpg file in
each directory, but to add this would
make the code substantially more messy
in it's current state..</p>
</blockquote>
<p>This doesn't look bad. In fact your current code does it very nicely, and Sven mentioned a good ... | [
{
"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": 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 | {} |
Build a Basic Python Iterator | <p>How would one create an iterative function (or iterator object) in python?</p>
| <p>Iterator objects in python conform to the iterator protocol, which basically means they provide two methods: <code>__iter__()</code> and <code>next()</code>. The <code>__iter__</code> returns the iterator object and is implicitly called at the start of loops. The <code>next()</code> method returns the next value... | [
{
"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 | {} |
A Transpose/Unzip Function in Python (inverse of zip) | <p>I have a list of 2-item tuples and I'd like to convert them to 2 lists where the first contains the first item in each tuple and the second list holds the second item.</p>
<p><strong>For example:</strong></p>
<pre><code>original = [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
# and I want to become...
result = (['a', '... | <p><a href="http://docs.python.org/library/functions.html#zip"><code>zip</code></a> is its own inverse! Provided you use the special * operator.</p>
<pre><code>>>> zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)])
[('a', 'b', 'c', 'd'), (1, 2, 3, 4)]
</code></pre>
<p>The way this works is by calling <code>zip</... | [
{
"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 | {} |
Introducing Python | <p>The company I used to work with has two developers working fulltime, and a handful of freelancers. They're in the process of hiring a new lead developer to try to bring order and management to the development.</p>
<p>But, currently, one of the developers has seen the light of Django (the company has only developed ... | <p>I recently introduced Python to my company, which does consulting work for the Post Office. I did this by waiting until there was a project for which I would be the only programmer, then getting permission to do this new project in Python. I then did another small project in Python with similarly impressive result... | [
{
"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 | {} |
What's the best way to grab/parse command line arguments passed to a Python script? | <p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
| <p><strong>This answer suggests <code>optparse</code> which is appropriate for older Python versions. For Python 2.7 and above, <code>argparse</code> replaces <code>optparse</code>. See <a href="http://stackoverflow.com/questions/3217673/why-use-argparse-rather-than-optparse">this answer</a> for more information.</stro... | [
{
"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 | {} |
Find broken symlinks with Python | <p>If I call <code>os.stat()</code> on a broken <code>symlink</code>, python throws an <code>OSError</code> exception. This makes it useful for finding them. However, there are a few other reasons that <code>os.stat()</code> might throw a similar exception. Is there a more precise way of detecting broken <code>symlinks... | <p>A common Python saying is that it's easier to ask forgiveness than permission. While I'm not a fan of this statement in real life, it does apply in a lot of cases. Usually you want to avoid code that chains two system calls on the same file, because you never know what will happen to the file in between your two c... | [
{
"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": 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 | {} |
updating an auto_now DateTimeField in a parent model w/ Django | <p>I've got two models: Message and Attachment. Each attachment is attached to a specific message, using a ForeignKey on the Attachment model. Both models have an auto_now DateTimeField called updated. I'm trying to make it so that when any attachment is saved, it also sets the updated field on the associated message t... | <p>You would also need to then save the message. Then it that should work.</p>
| [
{
"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 | {} |
Specifying a mySQL ENUM in a Django model | <p>How do I go about specifying and using an ENUM in a Django model?</p>
| <p>From the <a href="https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.choices" rel="nofollow">Django documentation</a>:</p>
<pre><code>MAYBECHOICE = (
('y', 'Yes'),
('n', 'No'),
('u', 'Unknown'),
)
</code></pre>
<p>And you define a charfield in your model :</p>
<pre><code>m... | [
{
"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 | {} |
Why Java and Python garbage collection methods are different? | <p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p>
<p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p>
<p>Why does Java choose this strategy and what is the benefit from thi... | <p>There are drawbacks of using reference counting. One of the most mentioned is circular references: Suppose A references B, B references C and C references B. If A were to drop its reference to B, both B and C will still have a reference count of 1 and won't be deleted with traditional reference counting. CPython (re... | [
{
"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 | {} |
Why does this python date/time conversion seem wrong? | <pre><code>>>> import time
>>> time.strptime("01-31-2009", "%m-%d-%Y")
(2009, 1, 31, 0, 0, 0, 5, 31, -1)
>>> time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1))
1233378000.0
>>> 60*60*24 # seconds in a day
86400
>>> 1233378000.0 / 86400
14275.208333333334
</code></pre>
<p>time.m... | <p>Short answer: Because of timezones.</p>
<p>The Epoch is in UTC.</p>
<p>For example, I'm on IST (Irish Stsandard Time) or GMT+1. time.mktime() is relative to my timezone, so on my system this refers to</p>
<pre><code>>>> time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1))
1233360000.0
</code></pre>
<p>Because... | [
{
"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": 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 content discovery engines, like Zemanta and Open Calais work? | <p>I was wondering how as semantic service like Open Calais figures out the names of companies, or people, tech concepts, keywords, etc. from a piece of text. Is it because they have a large database that they match the text against? </p>
<p>How would a service like Zemanta know what images to suggest to a piece of te... | <p>Michal Finkelstein from OpenCalais here.</p>
<p>First, thanks for your interest. I'll reply here but I also encourage you to read more on OpenCalais forums; there's a lot of information there including - but not limited to:
<a href="http://opencalais.com/tagging-information">http://opencalais.com/tagging-informatio... | [
{
"user_id": null,
"value": "no",
"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 | {} |
Unicode vs UTF-8 confusion in Python / Django? | <p>I stumbled over this passage in the <a href="http://www.djangoproject.com/documentation/tutorial01/">Django tutorial</a>:</p>
<blockquote>
<p>Django models have a default <strong>str</strong>() method that calls <strong>unicode</strong>() and converts the result to a UTF-8 bytestring. This means that unicode(p) w... | <blockquote>
<p>what is a "Unicode string" in Python? Does that mean UCS-2?</p>
</blockquote>
<p>Unicode strings in Python are stored internally either as UCS-2 (fixed-length 16-bit representation, almost the same as UTF-16) or UCS-4/UTF-32 (fixed-length 32-bit representation). It's a compile-time option; on Windows... | [
{
"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": 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 | {} |
Format numbers to strings in Python | <p>I need to find out how to format numbers as strings. My code is here:</p>
<pre><code>return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm
</code></pre>
<p>Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of ... | <p>Formatting in Python is done via the <a href="http://docs.python.org/2/library/stdtypes.html#string-formatting">string formatting (<code>%</code>) operator</a>:</p>
<pre><code>"%02d:%02d:%02d" % (hours, minutes, seconds)
</code></pre>
<p>/Edit: There's also <a href="https://docs.python.org/2/library/time.html#time... | [
{
"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 do I download a file over HTTP using Python? | <p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p>
<p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu... | <p>One more, using <a href="http://docs.python.org/2/library/urllib.html#urllib.urlretrieve"><code>urlretrieve</code></a>:</p>
<pre><code>import urllib
urllib.urlretrieve ("http://www.example.com/songs/mp3.mp3", "mp3.mp3")
</code></pre>
<p>(for Python 3+ use 'import urllib.request' and urllib.request.urlretrieve)</p>... | [
{
"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": 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 best way to duplicate fork() in windows? | <p>How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the <code>fork()</code> system call, using Python?</p>
<p>I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking o... | <p>Use the python <a href="http://docs.python.org/library/multiprocessing.html">multiprocessing module</a> which will work everywhere.</p>
<p>Here is a <a href="http://www.ibm.com/developerworks/aix/library/au-multiprocessing/">IBM developerWords article</a> that shows how to convert from os.fork() to the multiprocess... | [
{
"user_id": null,
"value": "no",
"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 | {} |
How can I graph the Lines of Code history for git repo? | <p>Basically I want to get the number of lines-of-code in the repository after each commit.</p>
<p>The only (really crappy) ways I have found is to use git filter-branch to run "wc -l *", and a script that run git reset --hard on each commit, then ran wc -l</p>
<p>To make it a bit clearer, when the tool is run, it wo... | <p>You may get both added and removed lines with git log, like:</p>
<pre><code>git log --shortstat --reverse --pretty=oneline
</code></pre>
<p>From this, you can write a similar script to the one you did using this info. In python:</p>
<pre><code>#!/usr/bin/python
"""
Display the per-commit size of the current git ... | [
{
"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 | {} |
Python code generator for Visual Studio? | <p>I had an idea, if I add a python .py file to my C# project, and tag the file with a custom generator that would execute the python file, and treat the output as the result of the code generation, ie. put it into a C# file, that would allow me to do quite a lot of code generation as part of the build process.</p>
<p... | <p>I think <a href="http://nedbatchelder.com/code/cog/" rel="nofollow">Cog</a> does what you want.</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": 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 to capture Python interpreter's and/or CMD.EXE's output from a Python script? | <ol>
<li>Is it possible to capture Python interpreter's output from a Python script?</li>
<li>Is it possible to capture Windows CMD's output from a Python script?</li>
</ol>
<p>If so, which librar(y|ies) should I look into?</p>
| <p>If you are talking about the python interpreter or CMD.exe that is the 'parent' of your script then no, it isn't possible. In every POSIX-like system (now you're running Windows, it seems, and that might have some quirk I don't know about, YMMV) each process has three streams, standard input, standard output and sta... | [
{
"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": 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 | {} |
Binary buffer in Python | <p>In Python you can use <a href="https://docs.python.org/library/struct.html">StringIO</a> for a file-like buffer for character data. <a href="https://docs.python.org/library/mmap.html">Memory-mapped file</a> basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python ha... | <p>You are probably looking for <a href="http://docs.python.org/release/3.1.3/library/io.html#binary-i-o">io.BytesIO</a> class. It works exactly like StringIO except that it supports binary data:</p>
<pre><code>from io import BytesIO
bio = BytesIO(b"some initial binary data: \x00\x01")
</code></pre>
<p>StringIO will ... | [
{
"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": 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 | {} |
pyGame within a pyGTK application | <p>What is the best way to use PyGame (SDL) within a PyGTK application?</p>
<p>I'm searching for a method that allows me to have a drawing area in the GTK window and at the same time being able to manage both GTK and SDL events.</p>
| <p>I've never attempted it myself, but hearing plenty about other people who've tried, it's not a road you want to go down.</p>
<p>There is the alternative of putting the gui in pygame itself. There are plenty of gui toolkits built specifically for pygame that you could use. Most of them are rather unfinished, but the... | [
{
"user_id": null,
"value": "no",
"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 | {} |
Python super class reflection | <p>If I have Python code</p>
<pre><code>class A():
pass
class B():
pass
class C(A, B):
pass
</code></pre>
<p>and I have class <code>C</code>, is there a way to iterate through it's super classed (<code>A</code> and <code>B</code>)? Something like pseudocode:</p>
<pre><code>>>> magicGetSuperClass... | <p><code>C.__bases__</code> is an array of the super classes, so you could implement your hypothetical function like so:</p>
<pre><code>def magicGetSuperClasses(cls):
return cls.__bases__
</code></pre>
<p>But I imagine it would be easier to just reference <code>cls.__bases__</code> directly in most cases.</p>
| [
{
"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 | {} |
Is there any difference between "foo is None" and "foo == None"? | <p>Is there any difference between:</p>
<pre><code>if foo is None: pass
</code></pre>
<p>and</p>
<pre><code>if foo == None: pass
</code></pre>
<p>The convention that I've seen in most Python code (and the code I myself write) is the former, but I recently came across code which uses the latter. None is an instance... | <p><code>is</code> always returns <code>True</code> if it compares the same object instance</p>
<p>Whereas <code>==</code> is ultimately determined by the <code>__eq__()</code> method</p>
<p>i.e.</p>
<pre><code>
>>> class foo(object):
def __eq__(self, other):
return True
>>> f = foo()
>>> f == Non... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"pandas",
"python-3.x"
],
"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 | {} |
wxpython: How do I examine dragged data in OnDragOver? | <p>I'm a bit perplexed by drag and drop in wxPython (but perhaps this questions pertains to drag and drop in other GUI frameworks as well). The frameworks provides a couple of callbacks (OnEnter and OnDragOver) that purportedly allow me to inform the system whether the current mouse position is a valid place to drop wh... | <p>One solution, which is a hack of limited usefulness, is when a drag is initiated, store the dragged data in a global or static reference somewhere. This way, in the OnEnter and OnDragOver handlers, it is possible to get a reference to the data being dragged. This is of course only useful for drags within the same ap... | [
{
"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 | {} |
Where can I learn more about PyPy's translation function? | <p>I've been having a hard time trying to understand PyPy's translation. It looks like something absolutely revolutionary from simply reading the description, however I'm hard-pressed to find good documentation on actually translating a real world piece of code to something such as LLVM. Does such a thing exist? The... | <p>This document seems to go into quite a bit of detail (and I think a complete description is out of scope for a stackoverflow answer):</p>
<ul>
<li><a href="http://codespeak.net/pypy/dist/pypy/doc/translation.html" rel="nofollow">http://codespeak.net/pypy/dist/pypy/doc/translation.html</a></li>
</ul>
<p>The general... | [
{
"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 | {} |
Does PHP have an equivalent to this type of Python string substitution? | <p>Python has this wonderful way of handling string substitutions using dictionaries:</p>
<pre><code>>>> 'The %(site)s site %(adj)s because it %(adj)s' % {'site':'Stackoverflow', 'adj':'rocks'}
'The Stackoverflow site rocks because it rocks'
</code></pre>
<p>I love this because you can specify a value once i... | <pre><code>function subst($str, $dict){
return preg_replace(array_map(create_function('$a', 'return "/%\\($a\\)s/";'), array_keys($dict)), array_values($dict), $str);
}
</code></pre>
<p>You call it like so:</p>
<pre><code>echo subst('The %(site)s site %(adj)s because it %(adj)s', array('site'=>'Stackoverflow'... | [
{
"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": 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 "safe_eval" really safe? | <p>I'm looking for a "safe" eval function, to implement spreadsheet-like calculations (using numpy/scipy).</p>
<p>The functionality to do this (the <a href="http://docs.python.org/lib/module-rexec.html" rel="nofollow">rexec module</a>) has been removed from Python since 2.3 due to apparently unfixable security problem... | <p>Writing your own parser could be fun! It might be a better option because people are expecting to use the familiar spreadsheet syntax (Excel, etc) and not Python when they're entering formulas. I'm not familiar with safe_eval but I would imagine that anything like this certainly has the potential for exploitation.... | [
{
"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 | {} |
Most Pythonic way equivalent for: while ((x = next()) != END) | <p>What's the best Python idiom for this C construct?</p>
<pre><code>while ((x = next()) != END) {
....
}
</code></pre>
<p>I don't have the ability to recode next().</p>
<p>update: and the answer from seems to be:</p>
<pre><code>for x in iter(next, END):
....
</code></pre>
| <p>@Mark Harrison's answer:</p>
<pre><code>for x in iter(next_, END):
....
</code></pre>
<p>Here's an excerpt from <a href="http://docs.python.org/library/functions.html">Python's documentation</a>:</p>
<pre><code>iter(o[, sentinel])
</code></pre>
<blockquote>
<p>Return an iterator object.
<em>...(snip)...<... | [
{
"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 | {} |
Best way to extract data from a FileMaker Pro database in a script? | <p>My job would be easier, or at least less tedious if I could come up with an automated way (preferably in a Python script) to extract useful information from a FileMaker Pro database. I am working on Linux machine and the FileMaker database is on the same LAN running on an OS X machine. I can log into the webby inter... | <p>It has been a <strong>really</strong> long time since I did anything with FileMaker Pro, but I know that it does have capabilities for an ODBC (and JDBC) connection to be made to it (however, I don't know how, or if, that translates to the linux/perl/python world though). </p>
<p>This article shows how to share/ex... | [
{
"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": 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 refactoring tools do you use for Python? | <p>I have a bunch of classes I want to rename. Some of them have names that are small and that name is reused in other class names, where I don't want that name changed. Most of this lives in Python code, but we also have some XML code that references class names.</p>
<p>Simple search and replace only gets me so far... | <p>In the meantime, I've tried it two tools that have some sort of integration with vim.</p>
<p>The first is <a href="http://rope.sourceforge.net/">Rope</a>, a python refactoring library that comes with a Vim (and emacs) plug-in. I tried it for a few renames, and that definitely worked as expected. It allowed me to ... | [
{
"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": 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 best way to use web services in python? | <p>I have a medium sized application that runs as a .net web-service which I do not control,
and I want to create a loose pythonic API above it to enable easy scripting.</p>
<p>I wanted to know what is the best/most practical solution for using web-services in python.</p>
<p>Edit:
I need to consume a complex soap WS
... | <p>If I have to expose APIs, I prefer doing it as JSON. Python has excellent support for JSON objects (JSON Objects are infact python dictionaries)</p>
| [
{
"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 | {} |
How do I create an xml document in python | <p>Here is my sample code:</p>
<pre><code>from xml.dom.minidom import *
def make_xml():
doc = Document()
node = doc.createElement('foo')
node.innerText = 'bar'
doc.appendChild(node)
return doc
if __name__ == '__main__':
make_xml().writexml(sys.stdout)
</code></pre>
<p>when I run the above code... | <p>Setting an attribute on an object won't give a compile-time or a run-time error, it will just do nothing useful if the object doesn't access it (i.e. "<code>node.noSuchAttr = 'bar'</code>" would also not give an error).</p>
<p>Unless you need a specific feature of <code>minidom</code>, I would look at <code>Element... | [
{
"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 | {} |
Python distutils - does anyone know how to use it? | <p>I wrote a quick program in python to add a gtk GUI to a cli program. I was wondering how I can create an installer using distutils. Since it's just a GUI frontend for a command line app it only works in *nix anyway so I'm not worried about it being cross platform.</p>
<p>my main goal is to create a .deb package for... | <p>See the <a href="http://docs.python.org/dist/simple-example.html" rel="nofollow">distutils simple example</a>. That's basically what it is like, except real install scripts usually contain a bit more information. I have not seen any that are fundamentally more complicated, though. In essence, you just give it a list... | [
{
"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": 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 | {} |
Install Python to match directory layout in OS X 10.5 | <p>The default Python install on OS X 10.5 is 2.5.1 with a fat 32 bit (Intel and PPC) client. I want to setup apache and mysql to run django. In the past I have run apache and mysql to match this install in 32 bit mode (even stripping out the 64 bit stuff from apache to make it work).</p>
<p>I want to upgrade Python t... | <p>Not sure I entirely understand your question, but can't you simply build and install a 64 bit version and then create symbolic links so that /Library/Python/2.5 and below point to your freshly built version of python?</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": 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 threads work in Python, and what are common Python-threading specific pitfalls? | <p>I've been trying to wrap my head around how threads work in Python, and it's hard to find good information on how they operate. I may just be missing a link or something, but it seems like the official documentation isn't very thorough on the subject, and I haven't been able to find a good write-up.</p>
<p>From wha... | <p>Yes, because of the Global Interpreter Lock (GIL) there can only run one thread at a time. Here are some links with some insights about this:</p>
<ul>
<li><a href="http://www.artima.com/weblogs/viewpost.jsp?thread=214235">http://www.artima.com/weblogs/viewpost.jsp?thread=214235</a></li>
<li><a href="http://smoothsp... | [
{
"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 render a tree structure (recursive) using a django template? | <p>I have a tree structure in memory that I would like to render in HTML using a Django template. </p>
<pre><code>class Node():
name = "node name"
children = []
</code></pre>
<p>There will be some object <code>root</code> that is a <code>Node</code>, and <code>children</code> is a list of <code>Node</code>s. <cod... | <p>Using <code>with</code> template tag, I could do tree/recursive list.</p>
<p>Sample code:</p>
<p>main template: assuming 'all_root_elems' is list of one or more root of tree</p>
<pre><code><ul>
{%for node in all_root_elems %}
{%include "tree_view_template.html" %}
{%endfor%}
</ul>
</code></pre>
... | [
{
"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 | {} |
Programmatically editing Python source | <p>This is something that I think would be very useful. Basically, I'd like there to be a way to edit Python source programmatically without requiring human intervention. There are a couple of things I would like to do with this:</p>
<ol>
<li><p>Edit the configuration of Python apps that use source modules for confi... | <p>Python's standard library provides pretty good facilities for working with Python source; note the <a href="https://docs.python.org/2/library/tokenize.html" rel="nofollow">tokenize</a> and <a href="https://docs.python.org/2/library/parser.html" rel="nofollow">parser</a> modules.</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 | {} |
Is it possible to run a Python script as a service in Windows? If possible, how? | <p>I am sketching the architecture for a set of programs that share various interrelated objects stored in a database. I want one of the programs to act as a service which provides a higher level interface for operations on these objects, and the other programs to access the objects through that service.</p>
<p>I am c... | <p>Yes you can. I do it using the pythoncom libraries that come included with <a href="http://www.activestate.com/Products/activepython/index.mhtml" rel="nofollow">ActivePython</a> or can be installed with <a href="https://sourceforge.net/projects/pywin32/" rel="nofollow">pywin32</a> (Python for Windows extensions).</p... | [
{
"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": 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 generate dynamic (parametrized) unit tests in python? | <p>I have some kind of test data and want to create an unit test for each item. My first idea was to do it like this:</p>
<pre><code>import unittest
l = [["foo", "a", "a",], ["bar", "a", "b"], ["lee", "b", "b"]]
class TestSequence(unittest.TestCase):
def testsample(self):
for name, a,b in l:
... | <p>i use something like this:</p>
<pre><code>import unittest
l = [["foo", "a", "a",], ["bar", "a", "b"], ["lee", "b", "b"]]
class TestSequense(unittest.TestCase):
pass
def test_generator(a, b):
def test(self):
self.assertEqual(a,b)
return test
if __name__ == '__main__':
for t in l:
... | [
{
"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": 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 | {} |
ssh hangs when command invoked directly, but exits cleanly when run interactive | <p>I need to launch a server on the remote machine and retrieve the port number that the server process is lsitening on. When invoked, the server will listen on a random port and output the port number on stderr.</p>
<p>I want to automate the process of logging on to the remote machine, launching the process, and ret... | <blockquote>
<pre><code>s = p.stderr.readline()
</code></pre>
</blockquote>
<p>I suspect it's the above line. When you invoke a command directly through ssh, you don't get your full pty (assuming Linux), and thus no stderr to read from.</p>
<p>When you log in interactively, stdin, stdout, and stderr are set up for y... | [
{
"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 | {} |
Extending base classes in Python | <p>I'm trying to extend some "base" classes in Python:</p>
<pre><code>class xlist (list):
def len(self):
return len(self)
def add(self, *args):
self.extend(args)
return None
class xint (int):
def add(self, value):
self += value
return self
x = xlist([1,2,3])
pri... | <p>Your two <code>xint</code> examples don't work for two different reasons.</p>
<p>The first doesn't work because <code>self += value</code> is equivalent to <code>self = self + value</code> which just reassigns the local variable <code>self</code> to a different object (an integer) but doesn't change the original ob... | [
{
"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 | {} |
Find out how much memory is being used by an object in Python | <p>How would you go about finding out how much memory is being used by an object? I know it is possible to find out how much is used by a block of code, but not by an instantiated object (anytime during its life), which is what I want. </p>
| <p><strong>There's no easy way to find out the memory size of a python object</strong>. One of the problems you may find is that Python objects - like lists and dicts - may have references to other python objects (in this case, what would your size be? The size containing the size of each object or not?). There are som... | [
{
"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 | {} |
Are Python threads buggy? | <p>A reliable coder friend told me that Python's current multi-threading implementation is seriously buggy - enough to avoid using altogether. What can said about this rumor?</p>
| <p>Python threads are good for <strong>concurrent I/O programming</strong>. Threads are swapped out of the CPU as soon as they block waiting for input from file, network, etc. This allows other Python threads to use the CPU while others wait. This would allow you to write a multi-threaded web server or web crawler, for... | [
{
"user_id": null,
"value": "yes",
"status": "submitted"
}
] | null | {
"type": null,
"score": null,
"agent": null
} | [
{
"user_id": null,
"value": [
"pandas",
"numpy"
],
"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 specify an authenticated proxy for a python http connection? | <p>What's the best way to specify a proxy with username and password for an http connection in python?</p>
| <p>This works for me: </p>
<pre><code>import urllib2
proxy = urllib2.ProxyHandler({'http': 'http://
username:password@proxyurl:proxyport'})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://python.org')... | [
{
"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": 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 | {} |
Django ImageField core=False in newforms admin | <p>In the transition to newforms admin I'm having difficulty figuring out how specify core=False for ImageFields.</p>
<p>I get the following error:</p>
<pre><code>TypeError: __init__() got an unexpected keyword argument 'core'
</code></pre>
<p>[Edit] However, by just removing the core argument I get a "This field is... | <p>The <code>core</code> attribute isn't used anymore.</p>
<p>From <a href="http://oebfare.com/blog/2008/jul/20/newforms-admin-migration-and-screencast/" rel="nofollow">Brian Rosner's Blog</a>:</p>
<blockquote>
<p>You can safely just remove any and all <code>core</code> arguments. They are no longer used. <em>newfo... | [
{
"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 descriptor protocol analog in other languages? | <p>Is there something like the Python descriptor protocol implemented in other languages? It seems like a nice way to increase modularity/encapsulation without bloating your containing class' implementation, but I've never heard of a similar thing in any other languages. Is it likely absent from other languages because... | <p>I've not heard of a direct equivalent either. You could probably achieve the same effect with macros, especially in a language like Lisp which has extremely powerful macros.</p>
<p>I wouldn't be at all surprised if other languages start to incorporate something similar because it is so powerful.</p>
| [
{
"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": 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 do I make Windows aware of a service I have written in Python? | <p>In <a href="http://stackoverflow.com/questions/32404/can-i-run-a-python-script-as-a-service-in-windows-how" rel="nofollow" title="Python scripts as Windows service">another question</a> I posted yesterday, I got very good advice on how a Python script could be run as a service in Windows. What I'm left wondering is:... | <p>Here is code to install a python-script as a service, written in python :)</p>
<p><a href="http://code.activestate.com/recipes/551780/">http://code.activestate.com/recipes/551780/</a></p>
<p>This post could also help you out:</p>
<p><a href="http://essiene.blogspot.com/2005/04/python-windows-services.html">http:/... | [
{
"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": 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 | {} |
Finding what methods an object has | <p>Given a Python object of any kind, is there an easy way to get a list of all methods that this object has?<br>
Or,</p>
<p>if this is not possible, is there at least an easy way to check if it has a particular method other than simply checking if an error occurs when the method is called?</p>
| <p>It appears you can use this code, replacing 'object' with the object you're interested in:-</p>
<pre><code>[method for method in dir(object) if callable(getattr(object, method))]
</code></pre>
<p>I discovered it at <a href="http://www.diveintopython.net/power_of_introspection/index.html">this site</a>, hopefully t... | [
{
"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": 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 | {} |
Scaffolding in pylons | <p>Is there anything similar to rails' scaffolding fo pylons? I've been poking around google, but fofund only this thing caled dbsprockets, which is fine, although probably way to much for my needs. What i really need is a basic CRUD thas is based on the SQLAlchemy model.</p>
| <p>The question is super old, but hell: <a href="http://code.google.com/p/formalchemy/" rel="nofollow">http://code.google.com/p/formalchemy/</a></p>
<p>Gives you basic crud out of the box, customizable to do even relatively complex things easily, and gives you a drop-in Pylons admin app too (written and customizable w... | [
{
"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 | {} |
Validate (X)HTML in Python | <p>What's the best way to go about validating that a document follows some version of HTML (prefereably that I can specify)? I'd like to be able to know where the failures occur, as in a web-based validator, except in a native Python app.</p>
| <p><a href="http://countergram.com/software/pytidylib">http://countergram.com/software/pytidylib</a> is a nice python binding for HTML Tidy. Their example:</p>
<pre><code>from tidylib import tidy_document
document, errors = tidy_document('''<p>f&otilde;o <img src="bar.jpg">''',
options={'numeric-e... | [
{
"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": 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 does Python's iter() on a mapping return iterkeys() instead of iteritems()? | <p>It seems like if you want to get the keys of a mapping, you ask for them; otherwise, give me the <em>whole</em> mapping (constituted by a set of key-value pairs). Is there a historical reason for this?</p>
| <p>Check out <a href="http://mail.python.org/pipermail/python-3000/2007-September/010209.html" rel="nofollow">this thread</a> for a discussion on the reasons behind this behavior (including that Guido likes it, and it's <a href="http://mail.python.org/pipermail/python-3000/2007-September/010222.html" rel="nofollow">not... | [
{
"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": 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 | {} |
Ruby "is" equivalent | <p>Is there a Ruby equivalent for Python's "is"? It tests whether two objects are identical (i.e. have the same memory location).</p>
| <p>Use <code>a.equal? b</code></p>
<p><a href="http://www.ruby-doc.org/core/classes/Object.html">http://www.ruby-doc.org/core/classes/Object.html</a></p>
<blockquote>
<p>Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) iff a is the ... | [
{
"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 | {} |
Is Python good for big software projects (not web based)? | <p>Right now I'm developing mostly in C/C++, but I wrote some small utilities in Python to automatize some tasks and I really love it as language (especially the productivity). </p>
<p>Except for the performances (a problem that could be sometimes solved thanks to the ease of interfacing Python with C modules), do you... | <p>We've used IronPython to build our flagship spreadsheet application (40kloc production code - and it's Python, which IMO means loc per feature is low) at <a href="http://www.resolversystems.com/">Resolver Systems</a>, so I'd definitely say it's ready for production use of complex apps.</p>
<p>There are two ways in ... | [
{
"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 | {} |
Why is my instance variable not in __dict__? | <p>If I create a class <code>A</code> as follows:</p>
<pre><code>class A:
def __init__(self):
self.name = 'A'
</code></pre>
<p>Inspecting the <code>__dict__</code> member looks like <code>{'name': 'A'}</code></p>
<p>If however I create a class <code>B</code>:</p>
<pre><code>class B:
name = 'B'
</cod... | <p><code>B.name</code> is a class attribute, not an instance attribute. It shows up in <code>B.__dict__</code>, but not in <code>b = B(); b.__dict__</code>.</p>
<p>The distinction is obscured somewhat because when you access an attribute on an instance, the class dict is a fallback. So in the above example, <code>b.... | [
{
"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 escape os.system() calls in Python? | <p>When using os.system() it's often necessary to escape filenames and other arguments passed as parameters to commands. How can I do this? Preferably something that would work on multiple operating systems/shells but in particular for bash.</p>
<p>I'm currently doing the following, but am sure there must be a libra... | <p><a href="https://docs.python.org/3/library/shlex.html#shlex.quote"><code>shlex.quote()</code></a> does what you want since python 3.</p>
<p>(Use <a href="https://docs.python.org/2/library/pipes.html#pipes.quote"><code>pipes.quote</code></a> to support both python 2 and python 3)</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": 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 templates and variable attributes | <p>I'm using Google App Engine and Django templates.<br>
I have a table that I want to display the objects look something like:<br></p>
<pre><code>Object Result:
Items = [item1,item2]
Users = [{name='username',item1=3,item2=4},..]
</code></pre>
<p>The Django template is:</p>
<pre><code><table>
<tr a... | <p>I found a "nicer"/"better" solution for getting variables inside
Its not the nicest way, but it works.</p>
<p>You install a custom filter into django which gets the key of your dict as a parameter</p>
<p>To make it work in google app-engine you need to add a file to your main directory,
I called mine *django_hack.... | [
{
"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": 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.