Dataset Viewer
Auto-converted to Parquet Duplicate
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
sequencelengths
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>&gt;&gt;&gt; def foo(): ... print "foo" ... &gt;&gt;&gt; class A: ... def bar( self ): ... print "bar" ... &gt;&gt;&gt; a = A() &gt;&gt;&gt; foo &lt;function foo at 0x00A98D70&gt; &gt;&gt;&gt; a.bar &lt;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> &gt;&gt;&gt; 0x12AF 4783 &gt;&gt;&gt; 0x100 256 </code></pre> <p>and octal:</p> <pre><code> &gt;&gt;&gt; 01267 695 &gt;&gt;&gt; 0100 64 </code>...
<p>For reference&mdash;<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>&gt;&gt;&gt; 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>&gt;&gt;&gt; import os &gt;&gt;&gt; print os.name posix &gt;&gt;&gt; import platform &gt;&gt;&gt; platform.system() 'Linux' &gt;&gt;&gt; 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>&gt;&gt;&gt; import struct &gt;&gt;&gt; struct.pack('f', 3.141592654) b'\xdb\x0fI@' &gt;&gt;&gt; struct.unpack('f', b'\xdb\x0fI@') (3.1415927410125732,) &gt;&gt;&gt; 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&hellip; 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 &amp; 0xff exitstatus = (status &amp; 0xff00) &gt;&gt; 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&lt;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> &gt;&gt;&gt; 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 &gt; 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 &lt; intArray.Length; ++i) { if(intArray[i] &gt; 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>&gt;&gt;&gt; import re &gt;&gt;&gt; mystring = r"This is \n a test \r" &gt;&gt;&gt; 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>&gt;&gt;&gt; mystring = r"This is \n a test \r" &gt;&gt;&gt; mystring.decode('string-escape') 'This is \n a test \r' &gt;&gt;&gt; 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("""&lt;!ELEMENT foo EMPTY&gt;""")...
[ { "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>&gt;&gt;&gt; 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>&gt;&gt;&gt; import time &gt;&gt;&gt; time.strptime("01-31-2009", "%m-%d-%Y") (2009, 1, 31, 0, 0, 0, 5, 31, -1) &gt;&gt;&gt; time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1)) 1233378000.0 &gt;&gt;&gt; 60*60*24 # seconds in a day 86400 &gt;&gt;&gt; 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>&gt;&gt;&gt; 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>&gt;&gt;&gt; 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>&gt;&gt;&gt; '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'=&gt;'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
{}
End of preview. Expand in Data Studio

Dataset Card for stackoverflow_feedback_demo

This dataset has been created with Argilla.

As shown in the sections below, this dataset can be loaded into Argilla as explained in Load with Argilla, or used directly with the datasets library in Load with datasets.

Dataset Summary

This dataset contains:

  • A dataset configuration file conforming to the Argilla dataset format named argilla.yaml. This configuration file will be used to configure the dataset when using the FeedbackDataset.from_huggingface method in Argilla.

  • Dataset records in a format compatible with HuggingFace datasets. These records will be loaded automatically when using FeedbackDataset.from_huggingface and can be loaded independently using the datasets library via load_dataset.

  • The annotation guidelines that have been used for building and curating the dataset, if they've been defined in Argilla.

Load with Argilla

To load with Argilla, you'll just need to install Argilla as pip install argilla --upgrade and then use the following code:

import argilla as rg

ds = rg.FeedbackDataset.from_huggingface("argilla/stackoverflow_feedback_demo")

Load with datasets

To load this dataset with datasets, you'll just need to install datasets as pip install datasets --upgrade and then use the following code:

from datasets import load_dataset

ds = load_dataset("argilla/stackoverflow_feedback_demo")

Supported Tasks and Leaderboards

This dataset can contain multiple fields, questions and responses so it can be used for different NLP tasks, depending on the configuration. The dataset structure is described in the Dataset Structure section.

There are no leaderboards associated with this dataset.

Languages

[More Information Needed]

Dataset Structure

Data in Argilla

The dataset is created in Argilla with: fields, questions, suggestions, and guidelines.

The fields are the dataset records themselves, for the moment just text fields are suppported. These are the ones that will be used to provide responses to the questions.

Field Name Title Type Required Markdown
title Title FieldTypes.text True False
question Question FieldTypes.text True True
answer Answer FieldTypes.text True True

The questions are the questions that will be asked to the annotators. They can be of different types, such as rating, text, label_selection, multi_label_selection, or ranking.

Question Name Title Type Required Description Values/Labels
title_question_fit Does the title match the question? QuestionTypes.label_selection True N/A ['yes', 'no']
tags What are the topics mentioned in this question? QuestionTypes.multi_label_selection True Select all that apply. ['python', 'django', 'python-2.7', 'list', 'python-3.x', 'numpy', 'pandas', 'regex', 'dictionary', 'string', 'matplotlib', 'arrays', 'google-app-engine', 'csv', 'tkinter', 'flask', 'json', 'linux', 'mysql', 'html', 'function', 'file', 'class', 'algorithm', 'windows', 'scipy', 'loops', 'multithreading', 'beautifulsoup', 'django-models', 'for-loop', 'javascript', 'xml', 'sqlalchemy', 'parsing', 'performance', 'datetime', 'osx', 'sorting', 'unicode', 'c++', 'dataframe', 'selenium', 'subprocess', 'pygame', 'java', 'pyqt', 'pip', 'tuples', 'scrapy']
answer_quality Rate the quality of the answer: QuestionTypes.rating True N/A [1, 2, 3, 4, 5]
new_answer If needed, correct the answer QuestionTypes.text False If the rating is below 4, please provide a corrected answer N/A

✨ NEW Additionally, we also have suggestions, which are linked to the existing questions, and so on, named appending "-suggestion" and "-suggestion-metadata" to those, containing the value/s of the suggestion and its metadata, respectively. So on, the possible values are the same as in the table above.

Finally, the guidelines are just a plain string that can be used to provide instructions to the annotators. Find those in the annotation guidelines section.

Data Instances

An example of a dataset instance in Argilla looks as follows:

{
    "external_id": null,
    "fields": {
        "answer": "\u003cp\u003eUnfortunately the only API that isn\u0027t deprecated is located in the ApplicationServices framework, which doesn\u0027t have a bridge support file, and thus isn\u0027t available in the bridge. If you\u0027re wanting to use ctypes, you can use ATSFontGetFileReference after looking up the ATSFontRef.\u003c/p\u003e\r\n\r\n\u003cp\u003eCocoa doesn\u0027t have any native support, at least as of 10.5, for getting the location of a font.\u003c/p\u003e",
        "question": "\u003cp\u003eI am using the Photoshop\u0027s javascript API to find the fonts in a given PSD.\u003c/p\u003e\n\n\u003cp\u003eGiven 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.\u003c/p\u003e\n\n\u003cp\u003eThis is all happening in a python program running on OSX so I guess I\u0027m looking for one of:\u003c/p\u003e\n\n\u003cul\u003e\n\u003cli\u003eSome Photoshop javascript\u003c/li\u003e\n\u003cli\u003eA Python function\u003c/li\u003e\n\u003cli\u003eAn OSX API that I can call from python\u003c/li\u003e\n\u003c/ul\u003e\n",
        "title": "How can I find the full path to a font from its display name on a Mac?"
    },
    "metadata": {},
    "responses": [
        {
            "status": "submitted",
            "user_id": null,
            "values": {
                "answer_quality": {
                    "value": 1
                },
                "new_answer": {
                    "value": "Sample answer"
                },
                "tags": {
                    "value": [
                        "tkinter"
                    ]
                },
                "title_question_fit": {
                    "value": "yes"
                }
            }
        }
    ],
    "suggestions": []
}

While the same record in HuggingFace datasets looks as follows:

{
    "answer": "\u003cp\u003eUnfortunately the only API that isn\u0027t deprecated is located in the ApplicationServices framework, which doesn\u0027t have a bridge support file, and thus isn\u0027t available in the bridge. If you\u0027re wanting to use ctypes, you can use ATSFontGetFileReference after looking up the ATSFontRef.\u003c/p\u003e\r\n\r\n\u003cp\u003eCocoa doesn\u0027t have any native support, at least as of 10.5, for getting the location of a font.\u003c/p\u003e",
    "answer_quality": [
        {
            "status": "submitted",
            "user_id": null,
            "value": 1
        }
    ],
    "answer_quality-suggestion": null,
    "answer_quality-suggestion-metadata": {
        "agent": null,
        "score": null,
        "type": null
    },
    "external_id": null,
    "metadata": "{}",
    "new_answer": [
        {
            "status": "submitted",
            "user_id": null,
            "value": "Sample answer"
        }
    ],
    "new_answer-suggestion": null,
    "new_answer-suggestion-metadata": {
        "agent": null,
        "score": null,
        "type": null
    },
    "question": "\u003cp\u003eI am using the Photoshop\u0027s javascript API to find the fonts in a given PSD.\u003c/p\u003e\n\n\u003cp\u003eGiven 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.\u003c/p\u003e\n\n\u003cp\u003eThis is all happening in a python program running on OSX so I guess I\u0027m looking for one of:\u003c/p\u003e\n\n\u003cul\u003e\n\u003cli\u003eSome Photoshop javascript\u003c/li\u003e\n\u003cli\u003eA Python function\u003c/li\u003e\n\u003cli\u003eAn OSX API that I can call from python\u003c/li\u003e\n\u003c/ul\u003e\n",
    "tags": [
        {
            "status": "submitted",
            "user_id": null,
            "value": [
                "tkinter"
            ]
        }
    ],
    "tags-suggestion": null,
    "tags-suggestion-metadata": {
        "agent": null,
        "score": null,
        "type": null
    },
    "title": "How can I find the full path to a font from its display name on a Mac?",
    "title_question_fit": [
        {
            "status": "submitted",
            "user_id": null,
            "value": "yes"
        }
    ],
    "title_question_fit-suggestion": null,
    "title_question_fit-suggestion-metadata": {
        "agent": null,
        "score": null,
        "type": null
    }
}

Data Fields

Among the dataset fields, we differentiate between the following:

  • Fields: These are the dataset records themselves, for the moment just text fields are suppported. These are the ones that will be used to provide responses to the questions.

    • title is of type FieldTypes.text.
    • question is of type FieldTypes.text.
    • answer is of type FieldTypes.text.
  • Questions: These are the questions that will be asked to the annotators. They can be of different types, such as RatingQuestion, TextQuestion, LabelQuestion, MultiLabelQuestion, and RankingQuestion.

    • title_question_fit is of type QuestionTypes.label_selection with the following allowed values ['yes', 'no'].
    • tags is of type QuestionTypes.multi_label_selection with the following allowed values ['python', 'django', 'python-2.7', 'list', 'python-3.x', 'numpy', 'pandas', 'regex', 'dictionary', 'string', 'matplotlib', 'arrays', 'google-app-engine', 'csv', 'tkinter', 'flask', 'json', 'linux', 'mysql', 'html', 'function', 'file', 'class', 'algorithm', 'windows', 'scipy', 'loops', 'multithreading', 'beautifulsoup', 'django-models', 'for-loop', 'javascript', 'xml', 'sqlalchemy', 'parsing', 'performance', 'datetime', 'osx', 'sorting', 'unicode', 'c++', 'dataframe', 'selenium', 'subprocess', 'pygame', 'java', 'pyqt', 'pip', 'tuples', 'scrapy'], and description "Select all that apply.".
    • answer_quality is of type QuestionTypes.rating with the following allowed values [1, 2, 3, 4, 5].
    • (optional) new_answer is of type QuestionTypes.text, and description "If the rating is below 4, please provide a corrected answer".
  • ✨ NEW Suggestions: As of Argilla 1.13.0, the suggestions have been included to provide the annotators with suggestions to ease or assist during the annotation process. Suggestions are linked to the existing questions, are always optional, and contain not just the suggestion itself, but also the metadata linked to it, if applicable.

    • (optional) title_question_fit-suggestion is of type QuestionTypes.label_selection with the following allowed values ['yes', 'no'].
    • (optional) tags-suggestion is of type QuestionTypes.multi_label_selection with the following allowed values ['python', 'django', 'python-2.7', 'list', 'python-3.x', 'numpy', 'pandas', 'regex', 'dictionary', 'string', 'matplotlib', 'arrays', 'google-app-engine', 'csv', 'tkinter', 'flask', 'json', 'linux', 'mysql', 'html', 'function', 'file', 'class', 'algorithm', 'windows', 'scipy', 'loops', 'multithreading', 'beautifulsoup', 'django-models', 'for-loop', 'javascript', 'xml', 'sqlalchemy', 'parsing', 'performance', 'datetime', 'osx', 'sorting', 'unicode', 'c++', 'dataframe', 'selenium', 'subprocess', 'pygame', 'java', 'pyqt', 'pip', 'tuples', 'scrapy'].
    • (optional) answer_quality-suggestion is of type QuestionTypes.rating with the following allowed values [1, 2, 3, 4, 5].
    • (optional) new_answer-suggestion is of type QuestionTypes.text.

Additionally, we also have one more field which is optional and is the following:

  • external_id: This is an optional field that can be used to provide an external ID for the dataset record. This can be useful if you want to link the dataset record to an external resource, such as a database or a file.

Data Splits

The dataset contains a single split, which is train.

Dataset Creation

Curation Rationale

[More Information Needed]

Source Data

Initial Data Collection and Normalization

[More Information Needed]

Who are the source language producers?

[More Information Needed]

Annotations

Annotation guidelines

[More Information Needed]

Annotation process

[More Information Needed]

Who are the annotators?

[More Information Needed]

Personal and Sensitive Information

[More Information Needed]

Considerations for Using the Data

Social Impact of Dataset

[More Information Needed]

Discussion of Biases

[More Information Needed]

Other Known Limitations

[More Information Needed]

Additional Information

Dataset Curators

[More Information Needed]

Licensing Information

[More Information Needed]

Citation Information

[More Information Needed]

Contributions

[More Information Needed]

Downloads last month
23