Search is not available for this dataset
id
int64
469
72M
question
stringlengths
11
150
body
stringlengths
22
48.7k
answer
stringlengths
20
59.4k
embedding
listlengths
384
384
535
Continuous Integration System for a Python Codebase
<p>I am starting to work on a hobby project with a <strong>Python</strong> codebase and I 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 <strong>CruiseControl</stro...
<p>We run <a href="http://buildbot.net/trac" rel="noreferrer">Buildbot - Trac</a> at work. I haven't used it too much since my codebase isn't part of the release cycle yet. But we run the tests on different environments (OSX/Linux/Win) and it sends emails — and it's written in Python.</p>
[ -0.05872442573308945, -0.04423551261425018, -0.055181585252285004, 0.0013319650897756219, -0.05609998479485512, -0.12781426310539246, -0.07270757108926773, 0.05584420636296272, -0.09619002044200897, 0.011250924319028854, -0.02283351495862007, 0.006450799759477377, 0.08161547780036926, -0.0...
594
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...
[ -0.021542774513363838, 0.10579828917980194, -0.0492791086435318, -0.002201908500865102, -0.01329115778207779, 0.04330158978700638, 0.01789134182035923, 0.10232941061258316, 0.04561940208077431, 0.010781007818877697, 0.02089501917362213, 0.024002138525247574, 0.06050004065036774, -0.0717330...
766
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 tr...
[ -0.005056446418166161, -0.0617218017578125, -0.029078692197799683, 0.0380852073431015, -0.10221108794212341, -0.057689134031534195, -0.0004748091159854084, 0.025314636528491974, -0.028782596811652184, -0.010911412537097931, -0.042986128479242325, 0.04879327863454819, 0.08302947878837585, -...
1,829
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 Windows:</strong></p> <pre><code>import msvcrt answer=msvcrt.getch() </code></pre>
[ -0.028170861303806305, -0.03516143932938576, -0.00807343702763319, 0.014501090161502361, -0.06017032265663147, -0.010309271514415741, 0.05218518152832985, 0.03913488984107971, -0.0010847625089809299, -0.018306534737348557, 0.013658195734024048, -0.034183043986558914, 0.10658743232488632, -...
2,311
File size differences after copying a file to a server vía FTP
<p>I have created a PHP-script to update a web server 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 ...
<p>Do you need to open the locfile in binary using <code>rb</code>?</p> <pre><code>f = open (locfile, "rb") </code></pre>
[ 0.008465638384222984, -0.012878390960395336, -0.0498034842312336, 0.025510508567094803, 0.07123596221208572, -0.055958181619644165, -0.04330196604132652, 0.04214857891201973, 0.0306306891143322, 0.07248230278491974, 0.02467171847820282, 0.05719351768493652, -0.022725826129317284, -0.044472...
972
Adding a method to an existing object instance in Python
<p>How do I add a method to an existing object (i.e., not in the class definition) in Python?</p> <p>I understand that it's not generally considered good practice to do so, except in some cases.</p>
<p>In Python, there is a difference between functions and bound methods.</p> <pre><code>&gt;&gt;&gt; def foo(): ... print &quot;foo&quot; ... &gt;&gt;&gt; class A: ... def bar( self ): ... print &quot;bar&quot; ... &gt;&gt;&gt; a = A() &gt;&gt;&gt; foo &lt;function foo at 0x00A98D70&gt; &gt;&gt;&gt; a.b...
[ -0.05934213474392891, 0.022202536463737488, -0.04230020195245743, 0.024714544415473938, -0.09349798411130905, -0.05669301748275757, -0.001670153229497373, 0.02238517627120018, -0.0454307496547699, 0.022595416754484177, 0.03989506512880325, 0.07576527446508408, 0.03222396597266197, -0.01146...
1,854
How to identify which OS Python is running on
<p>What do I need to look at to see whether I'm on Windows or Unix, etc.?</p>
<pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; 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>The output of <a href="https://docs.python.org/library/platform.html#platform.system" rel="noreferrer"><code>platfor...
[ 0.03942202404141426, -0.049274418503046036, -0.047646664083004, -0.04202395677566528, 0.06858862936496735, -0.020487040281295776, 0.02741522341966629, 0.0028701985720545053, -0.001189762493595481, -0.024163609370589256, 0.021461039781570435, -0.010219909250736237, -0.02200360596179962, -0....
469
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 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> <...
<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 nati...
[ -0.11462126672267914, 0.027748119086027145, -0.036889661103487015, 0.025442061945796013, -0.004515254870057106, -0.08879219740629196, 0.010381417348980904, 0.05948428809642792, -0.006306905299425125, -0.07225438207387924, 0.05664337798953056, 0.020010288804769516, 0.07603701949119568, -0.0...
4,942
How to sell Python to a client/boss/person
<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 ...
[ -0.075514055788517, 0.007503089029341936, -0.09369491040706635, -0.03715220093727112, 0.018824536353349686, -0.0822109803557396, 0.0032141939736902714, 0.06648333370685577, -0.07544343918561935, 0.05732763558626175, 0.015158533118665218, 0.06176063418388367, 0.07255249470472336, -0.0374175...
5,419
Python, Unicode, and the Windows console
<p>When I try to <code>print</code> a string in a Windows console, sometimes I get an error that says <code>UnicodeEncodeError: 'charmap' codec can't encode character ....</code>. I assume this is because the Windows console cannot handle all Unicode characters.</p> <p>How can I work around this? For example, how can I...
<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" rel...
[ 0.0025160349905490875, -0.05243263766169548, -0.025348931550979614, 0.028634754940867424, -0.10519483685493469, 0.018387198448181152, -0.042757805436849594, -0.007403934840112925, -0.013411211781203747, 0.0029275782871991396, 0.04941261187195778, -0.011644736863672733, 0.05396382138133049, ...
5,909
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 link = "http://python.org" print "opening url:", link site = urllib.urlopen(link) meta = site.info() print "Content-Length:", meta.getheaders("Content-Length")[0] f = open("out.txt", "r") print "File on disk:",len(f.read()) f.close() f = ope...
[ 0.0069312299601733685, 0.04520424082875252, -0.03435501828789711, 0.08599794656038284, 0.04702037572860718, -0.087661974132061, -0.017674246802926064, 0.05503243952989578, 0.010930675081908703, 0.05142443999648094, 0.024842802435159683, 0.07179050147533417, -0.03398341313004494, -0.0749770...
773
How do I use 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><strong>IMPORTANT NOTE:</strong> You <em>may</em> have to <strong>sort your data</strong> first.</p> <hr /> <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 uniq...
[ -0.09724272042512894, 0.02429025061428547, 0.027550822123885155, 0.002642358187586069, 0.03149470314383507, -0.0014979798579588532, 0.04510776698589325, -0.01710405945777893, -0.06999397277832031, -0.03494247421622276, 0.036102768033742905, -0.017169157043099403, 0.03113550692796707, -0.07...
502
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://web.archive.org/web/20090309234215/http://developer.apple.com:80/documentation/Darwin/Reference/ManPages/man1/sips.1.html" rel="noreferrer">sips</a>. Is th...
<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...
[ -0.07626603543758392, -0.016132481396198273, -0.03482050076127052, 0.004131009802222252, 0.061187129467725754, -0.08954198658466339, -0.08742201328277588, -0.00883434060961008, -0.024882376194000244, -0.09493515640497208, -0.006579338572919369, -0.004245436284691095, 0.06389964371919632, 0...
5,966
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" rel="noreferrer">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>pr...
<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...
[ -0.1041533499956131, 0.020315570756793022, -0.02648266777396202, -0.0024649298284202814, 0.02521594427525997, -0.0781412199139595, -0.031732868403196335, 0.0019574542529881, -0.04468442499637604, -0.03972099348902702, 0.006215882487595081, 0.00046770970220677555, 0.08524303883314133, -0.01...
1,476
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></pre> <p><strong>How do you us...
<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 repre...
[ 0.03389197215437889, 0.03416256979107857, -0.016776518896222115, 0.000031027364457258955, -0.03931425139307976, -0.09027628600597382, -0.00674758804962039, 0.06575716286897659, -0.06032461300492287, -0.019955884665250778, -0.05503786727786064, -0.033547669649124146, 0.11389853060245514, 0....
13,941
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 &quot;Process complete&quot;. I much rather it be a simple &quot;bell.&quot;</p> <p>I know that there's a function that can be...
<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...
[ -0.04550822451710701, -0.01719401590526104, 0.028451239690184593, -0.02007727324962616, -0.014286400750279427, -0.041157033294439316, 0.07316304743289948, -0.017142491415143013, -0.06113475561141968, -0.06838344782590866, 0.004882259760051966, -0.06785082817077637, 0.01793815940618515, 0.0...
12,592
Can you check that an exception is thrown with doctest in Python?
<p>Is it possible to write a <code>doctest</code> 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 <code>doctest</code> for that?</p>
<p>Yes. You can do it. The <a href="https://docs.python.org/3/library/doctest.html" rel="noreferrer">doctest module documentation</a> and Wikipedia has an <a href="http://en.wikipedia.org/wiki/Doctest#Example_2:_doctests_embedded_in_a_README.txt_file" rel="noreferrer">example</a> of it.</p> <pre><code> &gt;&gt;&gt; ...
[ -0.03350399434566498, 0.04097212851047516, -0.003313262714073062, 0.0022288637701421976, -0.02345469780266285, 0.013247179798781872, -0.08950527757406235, 0.05788271874189377, -0.04340958967804909, 0.0556173138320446, 0.018063656985759735, -0.013497394509613514, 0.13403058052062988, -0.043...
18,418
Elegant way to remove items from sequence in Python?
<p>When I am writing code in Python, I often need to remove items from a list or other sequence type based on some criteria. I haven't found a solution that is elegant and efficient, as removing items from a list you are currently iterating through is bad. For example, you can't do this:</p> <pre><code>for name in na...
<p>Two easy ways to accomplish just the filtering are:</p> <ol> <li><p>Using <code>filter</code>:</p> <p><code>names = filter(lambda name: name[-5:] != "Smith", names)</code></p></li> <li><p>Using list comprehensions:</p> <p><code>names = [name for name in names if name[-5:] != "Smith"]</code></p></li> </ol> <p>Not...
[ -0.08196520805358887, 0.13107861578464508, 0.06869945675134659, -0.02503133751451969, 0.02326507307589054, -0.11552789062261581, 0.06952786445617676, -0.07347093522548676, -0.002908298745751381, 0.007006646599620581, 0.03249647095799446, 0.08862211555242538, 0.021367622539401054, -0.017244...
14,389
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 <code>[\u0000-\uFFFF]</code> for what you want.</p> <p>You can also use the <code>re.UNICODE</code> compile flag. <a href="http://docs.python.org/lib/re-syntax.html" rel="noreferrer">The docs</a> say that if <code>UNICODE</code> is set, <code>\w</code> will match the characters <code>[0-9_]</code>...
[ -0.05794582515954971, -0.03245515376329422, 0.05186701938509941, -0.061324771493673325, -0.02776331640779972, 0.05187924578785896, 0.055461663752794266, -0.031919337809085846, 0.0037047092337161303, -0.053434379398822784, -0.01686592772603035, -0.037114232778549194, 0.019623342901468277, 0...
15,798
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="noreferrer">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;""...
[ -0.043945878744125366, 0.05896557867527008, 0.008077316917479038, -0.061039675027132034, -0.016048654913902283, -0.09339183568954468, 0.0023063323460519314, 0.03164196386933327, -0.037777822464704514, -0.03116982989013195, 0.019609900191426277, -0.025718150660395622, 0.08333678543567657, -...
22,617
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>Starting with Python 3.6, formatting in Python can be done using <a href="https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498" rel="noreferrer">formatted string literals</a> or <em>f-strings</em>:</p> <pre><code>hours, minutes, seconds = 6, 56, 33 f'{hours:02}:{minutes:02}:{seconds:02} {"pm" if hours &gt;...
[ 0.05189000815153122, 0.1085330918431282, -0.03778597339987755, -0.01943555474281311, -0.0644630566239357, 0.030648307874798775, -0.02257019653916359, 0.06181538105010986, 0.05146080628037453, -0.024594517424702644, -0.05511118099093437, -0.048676151782274246, 0.026338638737797737, -0.01475...
19,339
Transpose/Unzip Function (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', 'b'...
<p>In 2.x, <a href="http://docs.python.org/library/functions.html#zip" rel="noreferrer"><code>zip</code></a> is its own inverse! Provided you <a href="https://stackoverflow.com/questions/36901">use the special * operator</a>.</p> <pre><code>&gt;&gt;&gt; zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) [('a', 'b', 'c', 'd...
[ -0.03523669391870499, -0.02227628417313099, 0.05448676273226738, -0.004867011681199074, 0.09266361594200134, -0.0735432505607605, -0.007819337770342827, -0.05660487711429596, -0.06158820539712906, 0.04762514680624008, -0.06721542030572891, 0.024984538555145264, 0.070529043674469, -0.056848...
20,927
Updating an auto_now DateTimeField in a parent model in 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>
[ -0.02526787295937538, -0.034467607736587524, 0.03497156500816345, 0.026964930817484856, 0.016669854521751404, 0.030995920300483704, 0.001369971432723105, 0.04230477288365364, 0.03677951544523239, 0.012236762791872025, 0.05253929644823074, 0.02700844034552574, -0.020544903352856636, 0.08220...
19,151
How to build a basic iterator?
<p>How can I create an <a href="https://stackoverflow.com/questions/9884132/what-are-iterator-iterable-and-iteration">iterator</a> in Python?</p> <p>For example, suppose I have a class whose instances logically &quot;contain&quot; some values:</p> <pre><code>class Example: def __init__(self, values): self.v...
<p>Iterator objects in python conform to the iterator protocol, which basically means they provide two methods: <code>__iter__()</code> and <code>__next__()</code>. </p> <ul> <li><p>The <code>__iter__</code> returns the iterator object and is implicitly called at the start of loops.</p></li> <li><p>The <code>__next...
[ -0.064704030752182, 0.0736808031797409, -0.019699621945619583, 0.014402265660464764, 0.010487074963748455, -0.04911937937140465, 0.04187510907649994, 0.011005954816937447, -0.054333385080099106, -0.03356345742940903, -0.0018979825545102358, -0.0009804266737774014, 0.08774052560329437, -0.0...
19,030
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 ...
[ -0.07572110742330551, 0.00503461342304945, 0.035964805632829666, -0.05080338194966316, 0.08149328082799911, -0.09865860641002655, 0.07054558396339417, -0.10662500560283661, 0.02194800041615963, 0.01415892131626606, 0.032378751784563065, 0.018681827932596207, 0.052583228796720505, 0.0052160...
20,063
What's the best way to parse command line arguments?
<p>What's the <strong>easiest</strong>, <strong>tersest</strong>, and most <strong>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="https://stackoverflow.com/questions/3217673/why-use-argparse-rather-than-optparse">this answer</a> for more information.</str...
[ -0.04358280077576637, 0.013795214705169201, -0.053431082516908646, 0.009641041047871113, -0.030445801094174385, -0.11744970083236694, 0.030305063351988792, 0.09597950428724289, -0.07580836117267609, -0.025383692234754562, -0.03068649210035801, 0.025745423510670662, 0.037021324038505554, 0....
29,243
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...
[ 0.013707324862480164, 0.05107378214597702, 0.058169540017843246, 0.006869847886264324, 0.021203262731432915, -0.03335323557257652, 0.005636942572891712, 0.04850420728325844, -0.005609928630292416, -0.0046206144616007805, 0.09325511008501053, -0.05856006219983101, 0.025303315371274948, -0.1...
22,676
How to download a file over HTTP?
<p>I have a small utility that I use to download an MP3 file from a website on a schedule and then builds/updates a podcast XML file which I've added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. However, I use wget inside a Windows <code>.bat</code> file to download the...
<p>Use <a href="https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen" rel="noreferrer"><code>urllib.request.urlopen()</code></a>:</p> <pre><code>import urllib.request with urllib.request.urlopen('http://www.example.com/') as f: html = f.read().decode('utf-8') </code></pre> <p>This is the mos...
[ -0.03771563246846199, 0.08431442826986313, -0.08561576157808304, 0.04507279768586159, -0.010044940747320652, -0.09775909781455994, -0.0002638197911437601, 0.018900861963629723, 0.0007192266639322042, 0.0037024144548922777, 0.02217920869588852, 0.06379421055316925, 0.0024824978318065405, -0...
28,165
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'...
[ -0.00558116240426898, 0.03783649206161499, 0.04201367869973183, -0.04684629291296005, -0.02523941732943058, -0.055951595306396484, -0.003697863547131419, -0.04982016980648041, -0.020703818649053574, 0.03679511323571205, 0.028276707977056503, 0.002625510562211275, 0.05304345116019249, -0.04...
20,794
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...
[ -0.034941863268613815, -0.05945397913455963, 0.03410620242357254, 0.0521346740424633, 0.09410245716571808, -0.08936364948749542, -0.07440459728240967, 0.07625456899404526, -0.04768043011426926, 0.004766152705997229, 0.04112225025892258, 0.04641781374812126, 0.07263795286417007, -0.04193261...
31,412
Proprietary plug-ins for GPL programs: what about interpreted languages?
<p>I am developing a GPL-licensed application in Python and need to know if the GPL allows my program to use proprietary plug-ins. This is <a href="http://www.gnu.org/licenses/gpl-faq.html" rel="noreferrer">what the FSF has to say</a> on the issue:</p> <blockquote> <p><strong>If a program released under the GPL uses pl...
<blockquote> <p>he distinction between fork/exec and dynamic linking, besides being kind of artificial,</p> </blockquote> <p>I don't think its artificial at all. Basically they are just making the division based upon the level of integration. If the program has "plugins" which are essentially fire and forget with ...
[ -0.11515491455793381, -0.053461361676454544, -0.010014837607741356, -0.031171895563602448, 0.012803701683878899, -0.041150957345962524, -0.011643609032034874, 0.055856261402368546, 0.0008518705726601183, -0.04270659014582634, -0.01578928530216217, 0.003574685426428914, 0.01987743191421032, ...
1,171
What is the most efficient graph data structure in Python?
<p>I need to be able to manipulate a large (10^7 nodes) graph in python. The data corresponding to each node/edge is minimal, say, a small number of strings. What is the most efficient, in terms of <strong><em>memory and speed</em></strong>, way of doing this? </p> <p>A dict of dicts is more flexible and simpler to im...
<p>I would strongly advocate you look at <a href="http://networkx.github.com/" rel="noreferrer">NetworkX</a>. It's a battle-tested war horse and the first tool most 'research' types reach for when they need to do analysis of network based data. I have manipulated graphs with 100s of thousands of edges without problem o...
[ -0.020180514082312584, 0.11789878457784653, -0.023141706362366676, 0.02623654529452324, -0.021552875638008118, -0.11831121891736984, -0.002549532800912857, 0.00686525646597147, -0.030238645151257515, 0.044691942632198334, -0.010378916747868061, 0.07440962642431259, 0.009373883716762066, -0...
29,856
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 ...
<p>Personally, I wouldn't worry about it until you see a problem. Messing with the default python install on a *Nix system can cause more trouble than it's worth. I can say from personal experience that you never truly understand what python has done for the nix world until you have a problem with it.</p> <p>You can...
[ 0.04180387780070305, -0.03372282534837723, 0.04034030809998512, -0.016015274450182915, 0.04140457883477211, -0.0926174446940422, -0.06904306262731552, -0.022384807467460632, -0.11977662146091461, -0.045645374804735184, 0.0065717436373233795, 0.03927876427769661, 0.0032438263297080994, 0.03...
26,025
Cleanest & Fastest server setup for Django
<p>I'm about to deploy a mediumsized site powered by Django. I have a dedicated Ubuntu Server. </p> <p>I'm really confused over which serversoftware to use. So i thought to myself: why not ask stackoverflow.</p> <p>What i'm looking for is:</p> <ul> <li>Easy to set up</li> <li>Fast and easy on resources</li> <li>Can ...
<p>Since I was looking for some more in-depth answers, I decided to research the issue myself in depth. Please let me know if I've misunderstood anything.</p> <p>Some general recommendation are to use a separate webserver for handling media. By separate, I mean a webserver which is not running Django. This server can ...
[ -0.037272583693265915, -0.08415956795215607, 0.006653950084000826, -0.012246401980519295, 0.024992093443870544, -0.047878626734018326, 0.014175738207995892, 0.05415532365441322, -0.03576162829995155, -0.021252574399113655, 0.04373884201049805, 0.07200927287340164, -0.029298199340701103, 0....
28,369
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 noreferrer">rexec module</a>) has been removed from Python since 2.3 due to apparently unfixable secur...
<p>Depends on your definition of safe I suppose. A lot of the security depends on what you pass in and what you are allowed to pass in the context. For instance, if a file is passed in, I can open arbitrary files:</p> <pre><code>&gt;&gt;&gt; names['f'] = open('foo', 'w+') &gt;&gt;&gt; safe_eval.safe_eval("baz = type(f...
[ -0.14644236862659454, 0.033271823078393936, -0.08796624094247818, 0.06480761617422104, 0.03363379091024399, -0.07872824370861053, -0.07024279981851578, 0.024331456050276756, -0.003541763173416257, -0.03666391968727112, -0.030064169317483902, -0.020796334370970726, 0.07789795100688934, -0.0...
33,534
Extending base classes in Python
<p>I'm trying to extend some "base" classes in Python:</p> <pre><code>class xlist (list): def len(self): return len(self) def add(self, *args): self.extend(args) return None class xint (int): def add(self, value): self += value return self x = xlist([1,2,3]) pri...
<p><code>int</code> is a value type, so each time you do an assignment, (e.g. both instances of <code>+=</code> above), it doesn't modify the object you have on the heap, but replaces the reference with one of the result of the right hand side of the assignment (i.e. an <code>int</code>)</p> <p><code>list</code> isn't ...
[ -0.042368147522211075, 0.0395507775247097, 0.03963562846183777, -0.03512752801179886, -0.00014139653649181128, -0.03190705552697182, 0.02878306806087494, 0.031219778582453728, -0.08959485590457916, -0.06376836448907852, 0.01549333892762661, 0.035661064088344574, -0.0011877916986122727, 0.0...
34,611
Toolkit Options for 2D Python Game Programming
<p>What are some toolkits for developing 2D games in Python? An option that I have heard of is Pygame, but is there anything that has more range to do more things? What are the good and bad parts about the modules?</p>
<p>I use <a href="http://www.pygame.org/news.html" rel="nofollow noreferrer">pygame</a> myself and it is very good. It has good documentation and tutorials, and is quite well designed. I've also heard wonderful reviews of <a href="http://www.pyglet.org/" rel="nofollow noreferrer">pyglet</a>.</p>
[ -0.027809372171759605, -0.021617908030748367, 0.01206993218511343, -0.04917336627840996, -0.0388568639755249, -0.07675137370824814, -0.07445808500051498, 0.0182623490691185, -0.06700687855482101, 0.0318378321826458, -0.02464457042515278, -0.005869025830179453, -0.01029899250715971, 0.01213...
35,538
Validate (X)HTML in Python
<p>What's the best way to go about validating that a document follows some version of HTML (prefereably that I can specify)? I'd like to be able to know where the failures occur, as in a web-based validator, except in a native Python app.</p>
<p>XHTML is easy, use <a href="http://lxml.de/validation.html" rel="noreferrer">lxml</a>.</p> <pre><code>from lxml import etree from StringIO import StringIO etree.parse(StringIO(html), etree.HTMLParser(recover=False)) </code></pre> <p>HTML is harder, since there's traditionally not been as much interest in validatio...
[ -0.040864668786525726, 0.04620368406176567, 0.049428608268499374, -0.005342376884073019, 0.08747825771570206, -0.07217288017272949, -0.12116775661706924, -0.008091851137578487, -0.020069118589162827, -0.0017425674013793468, -0.02380259893834591, 0.02699507400393486, 0.06843122839927673, 0....
35,817
How to escape os.system() calls?
<p>When using os.system() it's often necessary to escape filenames and other arguments passed as parameters to commands. How can I do this? Preferably something that would work on multiple operating systems/shells but in particular for bash.</p> <p>I'm currently doing the following, but am sure there must be a librar...
<p>This is what I use:</p> <pre><code>def shellquote(s): return "'" + s.replace("'", "'\\''") + "'" </code></pre> <p>The shell will always accept a quoted filename and remove the surrounding quotes before passing it to the program in question. Notably, this avoids problems with filenames that contain spaces or an...
[ -0.03978975862264633, 0.07033293694257736, 0.011219337582588196, 0.017830152064561844, -0.03516687825322151, -0.05015837773680687, 0.022013729438185692, 0.08540692925453186, -0.030310524627566338, -0.008198966272175312, -0.04253747686743736, 0.01676998659968376, 0.09131009876728058, -0.031...
742
Class views in Django
<p><a href="http://www.djangoproject.com/" rel="noreferrer">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>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...
[ -0.08878220617771149, -0.009375695139169693, -0.024288233369588852, 0.004894650541245937, 0.002408438129350543, 0.00807324517518282, -0.040021490305662155, 0.0035938273649662733, -0.04492460563778877, -0.0013053733855485916, 0.008701435290277004, 0.039982154965400696, 0.043339554220438004, ...
36,812
How do I add data to an existing model in Django?
<p>Currently, I am writing up a bit of a product-based CMS as my first project.</p> <p>Here is my question. How can I add additional data (products) to my Product model?</p> <p>I have added '/admin/products/add' to my urls.py, but I don't really know where to go from there. How would i build both my view and my templ...
<p>You will want to wire your URL to the Django <a href="https://docs.djangoproject.com/en/1.4/ref/generic-views/#django-views-generic-create-update-create-object" rel="nofollow noreferrer">create_object generic view</a>, and pass it either "model" (the model you want to create) or "form_class" (a customized <a href="h...
[ -0.008554786443710327, -0.0812787339091301, -0.016919348388910294, -0.013255452737212181, -0.009400858543813229, 0.056924786418676376, 0.008994747884571552, 0.11296448856592178, -0.09117192029953003, -0.039470966905355453, 0.005589600186794996, 0.0020583688747137785, 0.019859962165355682, ...
36,806
Setup django with WSGI and apache
<p>I have been sold on mod_wsgi and apache rather than mod_python. I have all the parts installed (django, apache, mod_wsgi) but have run into a problem deploying.</p> <p>I am on osx 10.5 with apache 2.2 and django 1.0b2, mod_wsgi-2.3</p> <p>My application is called tred.</p> <p>Here are the relevant files: httpd-vh...
<p>What happens if you remove the <code>Alias /</code> directive?</p>
[ 0.0005907349986955523, -0.0021148277446627617, 0.04189914092421532, -0.11177040636539459, 0.016665272414684296, -0.14588837325572968, -0.010341632179915905, 0.006010791752487421, -0.001321394694969058, 0.011946441605687141, 0.06842003762722015, 0.03401673585176468, -0.01956169866025448, 0....
36,324
"The system cannot find the file specified" when invoking subprocess.Popen in python
<p>I'm trying to use <code>svnmerge.py</code> to merge some files. Under the hood it uses python, and when I use it I get an error - &quot;The system cannot find the file specified&quot;. Colleagues at work are running the same version of <code>svnmerge.py</code>, and of python (2.5.2, specifically r252:60911) without ...
<p>It's a bug, see the <a href="http://docs.python.org/lib/node528.html" rel="noreferrer">documentation of <code>subprocess.Popen</code></a>. There either needs to be a <code>"shell=True</code>" option, or the first argument needs to be a sequence <code>['svn', '--version']</code>. As it is now, <code>Popen</code> is l...
[ -0.12116311490535736, -0.030173588544130325, -0.051975466310977936, 0.028213253244757652, 0.03615006059408188, -0.09643734246492386, -0.03852655738592148, -0.014567789621651173, 0.013074434362351894, -0.08588198572397232, 0.05093879625201225, 0.08871384710073471, -0.006966868415474892, 0.0...
34,243
Python descriptor protocol analog in other languages?
<p>Is there something like the Python descriptor protocol implemented in other languages? It seems like a nice way to increase modularity/encapsulation without bloating your containing class' implementation, but I've never heard of a similar thing in any other languages. Is it likely absent from other languages because...
<p>I've not heard of a direct equivalent either. You could probably achieve the same effect with macros, especially in a language like Lisp which has extremely powerful macros.</p> <p>I wouldn't be at all surprised if other languages start to incorporate something similar because it is so powerful.</p>
[ -0.14835594594478607, 0.03831722214818001, -0.006379055790603161, -0.02515295334160328, -0.036289043724536896, -0.07578569650650024, -0.02008681558072567, 0.0019606626592576504, -0.0024298792704939842, -0.06664952635765076, 0.01981460116803646, 0.06872590631246567, 0.034635256975889206, 0....
39,663
What is the best way to do Bit Field manipulation in Python?
<p>I'm reading some MPEG Transport Stream protocol over UDP and it has some funky bitfields in it (length 13 for example). I'm using the "struct" library to do the broad unpacking, but is there a simple way to say "Grab the next 13 bits" rather than have to hand-tweak the bit manipulation? I'd like something like the w...
<p>It's an often-asked question. There's an <a href="http://code.activestate.com/recipes/113799/" rel="noreferrer">ASPN Cookbook</a> entry on it that has served me in the past.</p> <p>And there is an <a href="http://wiki.python.org/moin/BitManipulation" rel="noreferrer">extensive page of requirements one person would...
[ 0.012796835042536259, 0.05439642444252968, -0.05225708335638046, -0.07166211307048798, -0.007091374136507511, -0.038453828543424606, 0.02639312483370304, -0.02162882499396801, -0.03474274277687073, -0.05707709863781929, 0.0014647230273112655, -0.006655710283666849, -0.1058361604809761, 0.0...
33,475
ssh hangs when command invoked directly, but exits cleanly when run interactive
<p>I need to launch a server on the remote machine and retrieve the port number that the server process is lsitening on. When invoked, the server will listen on a random port and output the port number on stderr.</p> <p>I want to automate the process of logging on to the remote machine, launching the process, and ret...
<blockquote> <pre><code>s = p.stderr.readline() </code></pre> </blockquote> <p>I suspect it's the above line. When you invoke a command directly through ssh, you don't get your full pty (assuming Linux), and thus no stderr to read from.</p> <p>When you log in interactively, stdin, stdout, and stderr are set up for y...
[ -0.05905088037252426, -0.0001402584312018007, -0.05849384516477585, -0.031718090176582336, -0.07522273808717728, -0.04918968677520752, 0.00034112512366846204, 0.05774484947323799, -0.032618481665849686, 0.014181424863636494, -0.06544779986143112, 0.02214009314775467, -0.021893836557865143, ...
34,020
Are Python threads buggy?
<p>A reliable coder friend told me that Python's current multi-threading implementation is seriously buggy - enough to avoid using altogether. What can said about this rumor?</p>
<p>Python threads are good for <strong>concurrent I/O programming</strong>. Threads are swapped out of the CPU as soon as they block waiting for input from file, network, etc. This allows other Python threads to use the CPU while others wait. This would allow you to write a multi-threaded web server or web crawler, for...
[ -0.1617678999900818, -0.044625766575336456, 0.013840722851455212, 0.02568381279706955, -0.018115082755684853, -0.11744923889636993, -0.07672510296106339, -0.00879976898431778, -0.09294047206640244, -0.037761323153972626, -0.0064162518829107285, 0.08423799276351929, 0.013572906143963337, -0...
27,567
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="noreferrer">http://codespeak.net/pypy/dist/pypy/doc/translation.html</a></li> </ul> <p>The gener...
[ -0.043910540640354156, -0.01812119409441948, -0.05946660786867142, -0.04675889387726784, 0.005769993178546429, -0.048435382544994354, -0.026119554415345192, 0.0470019094645977, -0.0037498269230127335, 0.004249689634889364, 0.007818976417183876, 0.05210237205028534, 0.011252544820308685, 0....
33,813
What are some useful TextMate features?
<p>I noticed that many people here use <a href="http://macromates.com/" rel="nofollow noreferrer">TextMate</a> for coding on OS X. I've recently started using it, and although I like its minimalistic interface, it makes it harder to stumble upon cool features if you don't know what you're looking for.</p> <p>So, what ...
<p>Don't neglect the 'mate' command line tool. You can use it to pipe output into TextMate, so if you do the following...</p> <pre><code>diff file1.py file2.py | mate </code></pre> <p>...it will not only open in TextMate, but it is smart enough to know that you're looking at a diff and highlight lines on screen.</p> ...
[ -0.123097725212574, -0.02071540802717209, 0.011716472916305065, 0.05226931348443031, 0.06200146675109863, -0.02708805911242962, -0.010389965958893299, 0.037132784724235535, -0.07683435827493668, -0.035447731614112854, 0.04897246137261391, 0.04350581020116806, 0.07032828032970428, -0.012010...
40,586
cx_Oracle: how do I get the ORA-xxxxx error number?
<p>In a try/except block, how do I extract the Oracle error number?</p>
<pre><code>try: cursor.execute("select 1 / 0 from dual") except cx_Oracle.DatabaseError, e: error, = e print "Code:", error.code print "Message:", error.message </code></pre> <p>This results in the following output:</p> <pre><code>Code: 1476 Message: ORA-01476: divisor is equal to zero </code></pre>
[ -0.04706430807709694, 0.0827237218618393, 0.0011591663351282477, -0.018537940457463264, 0.058498580008745193, 0.0034956540912389755, -0.010221929289400578, 0.06722761690616608, -0.03601833060383797, 0.033396948128938675, 0.018187018111348152, -0.03269224241375923, 0.03763449564576149, -0.0...
44,109
What's the best way to extend the User model in Django?
<p>What's the best way to extend the User model (bundled with Django's authentication app) with custom fields? I would also possibly like to use the email as the username (for authentication purposes).</p> <p>I've already seen a <a href="http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inhe...
<p>The least painful and indeed Django-recommended way of doing this is through a <code>OneToOneField(User)</code> property.</p> <blockquote> <h2><a href="https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model" rel="noreferrer">Extending the existing User model</a></h2> <p>…</p...
[ -0.09877458214759827, 0.038392070680856705, 0.03656093031167984, -0.04897655174136162, -0.030517738312482834, -0.02623158134520054, 0.00742869870737195, 0.09166055172681808, -0.006252865307033062, -0.04541029408574104, 0.05343906208872795, 0.009939911775290966, -0.018810490146279335, 0.015...
26,706
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...
[ -0.061557963490486145, -0.009309932589530945, -0.053174059838056564, 0.07054819911718369, 0.05188896879553795, -0.030030643567442894, 0.1211748868227005, 0.02920372597873211, -0.0021282231900840998, -0.03574105724692345, 0.08306660503149033, 0.02504023164510727, -0.0030455433297902346, 0.0...
42,581
Python re.sub with a flag does not replace all occurrences
<p>The Python docs say:</p> <blockquote> <p>re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...</p> </blockquote> <p>So what's going on ...
<p>Look at the definition of <a href="http://docs.python.org/library/re.html#re.sub" rel="noreferrer"><code>re.sub</code></a>:</p> <pre><code>re.sub(pattern, repl, string[, count, flags]) </code></pre> <p>The 4th argument is the count, you are using <code>re.MULTILINE</code> (which is 8) as the count, not as a flag.<...
[ -0.09429867565631866, 0.007114107254892588, 0.07884229719638824, 0.016624005511403084, -0.06125058978796005, -0.025868847966194153, 0.03270035609602928, 0.0027442832943052053, 0.0549129955470562, 0.04452493041753769, 0.043947942554950714, 0.02827748842537403, 0.07818517088890076, -0.042294...
8,154
Pylons error - 'MySQL server has gone away'
<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, and saw that this was because the connections to MySQL were not being renewed. This shouldn't ...
<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...
[ -0.03374609351158142, -0.06902164965867996, -0.04892341420054436, 0.010264997370541096, -0.05656425654888153, -0.0913444235920906, 0.017204780131578445, -0.003474114229902625, -0.060187023133039474, -0.007789032068103552, 0.033938512206077576, 0.08708906173706055, 0.1000271588563919, 0.003...
47,789
Generator expressions vs. list comprehensions
<p>When should you use generator expressions and when should you use list comprehensions in Python?</p> <pre><code># Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)] </code></pre>
<p><a href="https://stackoverflow.com/a/47792/4518341">John's answer</a> is good (that list comprehensions are better when you want to iterate over something multiple times). However, it's also worth noting that you should use a list if you want to use any of the list methods. For example, the following code won't wo...
[ -0.026234732940793037, 0.0459696501493454, -0.005278796888887882, 0.01066101435571909, 0.004674459341913462, -0.04817984253168106, -0.01624693162739277, 0.021422509104013443, -0.06392783671617508, -0.026286311447620392, 0.0256073996424675, -0.015972275286912918, 0.09494183957576752, -0.056...
36,139
How to sort a list of strings?
<p>What is the best way of creating an alphabetically sorted list in Python?</p>
<p>Basic answer:</p> <pre><code>mylist = ["b", "C", "A"] mylist.sort() </code></pre> <p>This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the <a href="http://docs.python.org/library/functions.html#sorted" rel="noreferrer"><code>sorted()</code>...
[ -0.03653275966644287, 0.009643889032304287, 0.012446030043065548, -0.01549745537340641, -0.04097909852862358, -0.01950502023100853, 0.0003725218412000686, 0.018096234649419785, -0.02324705198407173, 0.07159656286239624, 0.05967964604496956, 0.15887126326560974, 0.10204865038394928, 0.01446...
47,207
Django: Print url of view without hardcoding the url
<p>Can i print out a url <code>/admin/manage/products/add</code> of a certain view in a template?</p> <p>Here is the rule i want to create a link for</p> <pre><code>(r'^manage/products/add/$', create_object, {'model': Product, 'post_save_redirect': ''}), </code></pre> <p>I would like to have /manage/products/add in ...
<p>You can use <code>get_absolute_url</code>, but that will only work for a particular object. Since your object hasn't been created yet, it won't work in this case.</p> <p>You want to use <a href="https://docs.djangoproject.com/en/1.2/topics/http/urls/#naming-url-patterns" rel="nofollow noreferrer">named URL pattern...
[ -0.033634454011917114, -0.013350959867238998, -0.06012512370944023, 0.051062967628240585, 0.03176046162843704, 0.03629542887210846, -0.04493705555796623, 0.045443568378686905, -0.0781211331486702, 0.019953692331910133, -0.004231207072734833, 0.052680887281894684, 0.028632119297981262, -0.0...
43,099
How can I get a commit message from a bzr post-commit hook?
<p>I'm trying to write a <code>bzr post-commit</code> hook for my private bugtracker, but I'm stuck at the function signature of </p> <pre><code>post_commit(local, master, old_revno, old_revid, new_revno, mew_revid) </code></pre> <p>How can I extract the commit message for the branch from this with <code>bzrlib</code...
<p>And the answer is like so:</p> <pre><code>def check_commit_msg(local, master, old_revno, old_revid, new_revno, new_revid): branch = local or master revision = branch.repository.get_revision(new_revid) print revision.message </code></pre> <p>local and master are Branch objects, so once you have a revisi...
[ -0.015258203260600567, 0.07295691967010498, -0.026566157117486, 0.021045371890068054, 0.013405282981693745, -0.009585779160261154, -0.0046507008373737335, 0.046981628984212875, -0.03853578492999077, -0.004454751033335924, 0.007500175386667252, -0.0334821380674839, 0.03645482659339905, -0.0...
42,093
Pure Python library to generate Identicons?
<p>Does anyone know of a FOSS Python lib for generating <a href="http://en.wikipedia.org/wiki/Identicon" rel="noreferrer">Identicons</a>? I've looked, but so far I haven't had much luck.</p>
<p>I've found two implementations:<br> <a href="http://coderepos.org/share/browser/lang/python/misc/identicon.py" rel="noreferrer">http://coderepos.org/share/browser/lang/python/misc/identicon.py</a><br> <a href="http://code.google.com/p/visicon/" rel="noreferrer">http://code.google.com/p/visicon/</a></p>
[ -0.13823412358760834, 0.017240911722183228, -0.017062054947018623, 0.013951875269412994, 0.01638232171535492, -0.09929237514734268, -0.04721513018012047, -0.03954504802823067, -0.05491088330745697, -0.03406602516770363, -0.020837413147091866, 0.0011325329542160034, 0.11008011549711227, -0....
41,547
Always including the user in the django template context
<p>I am working on a small intranet site for a small company, where user should be able to post. I have imagined a very simple authentication mechanism where people just enter their email address, and gets sent a unique login url, that sets a cookie that will always identify them for future requests.</p> <p>In my temp...
<p>In a more general sense of not having to explicitly set variables in each view, it sounds like you want to look at writing your own <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors" rel="noreferrer">context processor</a>.</p> <p>From the docs:</p> <blockquote> ...
[ -0.010005800984799862, -0.032192785292863846, -0.05300267040729523, -0.029155142605304718, 0.010417923331260681, 0.0013318097917363048, 0.11487547308206558, 0.014098692685365677, -0.012234638445079327, -0.03856033831834793, -0.015591846778988838, -0.05629201605916023, 0.03662649914622307, ...
39,929
PGP signatures from Python?
<p>What is the easiest way to create and verify PGP/GPG signatures from within a Python application? </p> <p>I can call pgp or gpg using subprocess and parse the output, but I was looking for a way that didn't require an external program to be installed (my application is cross-platform mac/windows/unix).</p>
<p>I think <a href="http://www.gnupg.org/related_software/gpgme/" rel="nofollow noreferrer">GPGME</a> and the <a href="http://pyme.sourceforge.net/" rel="nofollow noreferrer">PyMe Python wrapper</a> should do what you need.</p>
[ -0.0477927103638649, -0.02142438478767872, -0.029919220134615898, -0.0312776044011116, -0.020668134093284607, -0.14461322128772736, 0.0158892422914505, 0.0984327644109726, -0.04386848956346512, -0.07386891543865204, -0.05836515128612518, 0.007710226811468601, 0.043420758098363876, -0.02300...
47,953
What are the advantages of packaging your python library/application as an .egg file?
<p>I've read some about .egg files and I've noticed them in my lib directory but what are the advantages/disadvantages of using then as a developer?</p>
<p>From the <a href="http://peak.telecommunity.com/DevCenter/PythonEggs" rel="noreferrer">Python Enterprise Application Kit community</a>:</p> <blockquote> <p><em>"Eggs are to Pythons as Jars are to Java..."</em></p> <p>Python eggs are a way of bundling additional information with a Python project, that all...
[ -0.07315482944250107, 0.015156136825680733, -0.004507548175752163, -0.028498847037553787, 0.07990432530641556, 0.04018767550587654, 0.01051248051226139, 0.05427621304988861, 0.0773422122001648, -0.010708111338317394, 0.06932438164949417, 0.12530307471752167, -0.083174929022789, -0.02796911...
43,290
How to generate urls in django
<p>In Django's template language, you can use <code>{% url [viewname] [args] %}</code> to generate a URL to a specific view with parameters. How can you programatically do the same in Python code?</p> <p>What I need is to create a list of menu items where each item has name, URL, and an active flag (whether it's the ...
<p>If you need to use something similar to the <code>{% url %}</code> template tag in your code, Django provides the <code>django.core.urlresolvers.reverse()</code>. The <code>reverse</code> function has the following signature:</p> <pre><code>reverse(viewname, urlconf=None, args=None, kwargs=None) </code></pre> <p><...
[ -0.036679837852716446, 0.02530910260975361, -0.059589918702840805, 0.029808714985847473, -0.03018897958099842, -0.01907290332019329, 0.061399076133966446, 0.015645362436771393, -0.0830388069152832, -0.02886221557855606, -0.028538934886455536, 0.005444041918963194, 0.041968271136283875, 0.0...
37,535
What's the easiest way to read a FoxPro DBF file from Python?
<p>I've got a bunch of FoxPro (VFP9) DBF files on my Ubuntu system, is there a library to open these in Python? I only need to read them, and would preferably have access to the memo fields too.</p> <p><strong>Update</strong>: Thanks @cnu, I used Yusdi Santoso's <a href="http://www.physics.ox.ac.uk/users/santoso/dbf....
<p>You can try this <a href="http://code.activestate.com/recipes/362715/" rel="nofollow noreferrer">recipe on Active State</a>. </p> <p>There is also a <a href="http://code.google.com/p/lino/source/browse/lino/utils/dbfreader.py" rel="nofollow noreferrer">DBFReader module</a> which you can try.</p> <p>For support for...
[ -0.05507487431168556, -0.06669413298368454, -0.1340329945087433, -0.03770534694194794, 0.029068000614643097, -0.006632247474044561, -0.015282451175153255, 0.09441045671701431, -0.07251988351345062, -0.021958893164992332, 0.022130466997623444, 0.07986073195934296, 0.012306643649935722, 0.00...
38,056
How to check if a process is still running using Python on Linux?
<p>The only <em>nice</em> way I've found is:</p> <pre><code>import sys import os try: os.kill(int(sys.argv[1]), 0) print "Running" except: print "Not running" </code></pre> <p>(<a href="http://www.unix.com/unix-advanced-expert-users/79267-trick-bash-scripters-check-if-process-running.html" re...
<p>Mark's answer is the way to go, after all, that's why the /proc file system is there. For something a little more copy/pasteable:</p> <pre><code> &gt;&gt;&gt; import os.path &gt;&gt;&gt; os.path.exists("/proc/0") False &gt;&gt;&gt; os.path.exists("/proc/12") True </code></pre>
[ -0.05512288585305214, -0.018195688724517822, -0.07057405263185501, -0.04118405655026436, 0.10514383763074875, -0.13222892582416534, -0.04260670021176338, -0.01623293198645115, 0.00734939007088542, 0.016846049576997757, 0.014248814433813095, 0.06021406874060631, 0.05285295844078064, -0.0589...
38,197
Why is the subprocess.Popen class not named Subprocess?
<p>The primary class in the <code>subprocess</code> module is name <code>Popen</code>, and represents a subprocess. <code>Popen</code> sounds like someone was trying to force the name to follow some function naming format, rather than chosing a name that actually represents what the object is. Does anyone know why it ...
<p>Now, I'm not saying that this is the greatest name in the world, but here was the idea as I understand it.</p> <p>Originally, the popen family was in the os module and was an implementation of the venerable posix popen. The movement to the subprocess module would have been an opportune time to rename them, but I g...
[ -0.14106318354606628, -0.017061414197087288, -0.05073335021734238, 0.006756182294338942, -0.031006576493382454, -0.04550406336784363, -0.03374416008591652, 0.08197307586669922, 0.07045701891183853, -0.06563829630613327, -0.05297427996993065, 0.036520615220069885, -0.0016701486892998219, -0...
32,044
How can I render a tree structure (recursive) using a django template?
<p>I have a tree structure in memory that I would like to render in HTML using a Django template. </p> <pre><code>class Node(): name = "node name" children = [] </code></pre> <p>There will be some object <code>root</code> that is a <code>Node</code>, and <code>children</code> is a list of <code>Node</code>s. <cod...
<p>I think the canonical answer is: "Don't".</p> <p>What you should probably do instead is unravel the thing in your <em>view</em> code, so it's just a matter of iterating over (in|de)dents in the template. I think I'd do it by appending indents and dedents to a list while recursing through the tree and then sending t...
[ -0.04315483570098877, 0.07645642757415771, 0.06130516901612282, 0.03792601078748703, 0.06942597031593323, -0.021482333540916443, -0.07149586081504822, 0.03075176477432251, -0.024136262014508247, 0.009797638282179832, -0.042747266590595245, 0.005239155609160662, 0.011185792274773121, 0.0061...
41,562
How do I do monkeypatching in python?
<p>I've had to do some introspection in python and it wasn't pretty:</p> <pre><code>name = sys._getframe(1).f_code name = "%s:%d %s()" %(os.path.split(name.co_filename)[1],name.co_firstlineno,name.co_name) </code></pre> <p>To get something like</p> <pre><code>foo.py:22 bar() blah blah </code></pre> <p>In our debugg...
<p>A print statement does its IO through "sys.stdout.write" so you can override sys.stdout if you want to manipulate the print stream.</p>
[ -0.05957398563623428, 0.04879479482769966, -0.07944516092538834, 0.07340554147958755, -0.0007824538042768836, -0.06969178467988968, 0.007496287114918232, 0.03814701735973358, -0.09674936532974243, -0.016058417037129402, 0.04594612494111061, 0.07781361788511276, 0.03120512329041958, -0.0398...
47,253
How do you set up a Python WSGI server under IIS?
<p>I work in a Windows environment and would prefer to deploy code to IIS. At the same time I would like to code in Python.</p> <p>Having read that IIS can run fastCGI application, I went to the IIS site where it describes in detail how to get PHP up and running but not much about anything else.</p> <p>Does anyone ha...
<p>There shouldn't be any need to use FastCGI. There exists a <a href="https://github.com/hexdump42/isapi-wsgi" rel="noreferrer">ISAPI extension for WSGI</a>.</p>
[ -0.08084718137979507, -0.03780287504196167, -0.08721895515918732, 0.026345763355493546, -0.025483885779976845, -0.0505572110414505, -0.0011235031997784972, 0.014259492047131062, -0.07150597870349884, 0.023762570694088936, -0.028977883979678154, 0.020906472578644753, 0.019656704738736153, 0...
48,777
Python: No module named core.exceptions
<p>I'm trying to get Google AppEngine to work on my Debian box and am getting the following error when I try to access my page:</p> <pre><code>&lt;type 'exceptions.ImportError'&gt;: No module named core.exceptions </code></pre> <p>The same app works fine for me when I run it on my other Ubuntu box, so I know it's no...
<p><code>core.exceptions</code> is part of django; what version of django do you have installed? The AppEngine comes with the appropriate version for whatever release you've downloaded (in the lib/django directory). It can be installed by going to that directory and running <strong><code>python setup.py install</code><...
[ -0.08210963755846024, -0.09915390610694885, 0.08662653714418411, -0.08242237567901611, 0.043282266706228256, -0.10493291169404984, -0.0633879005908966, 0.011069796979427338, -0.08608081936836243, 0.0023747486993670464, 0.047040704637765884, -0.06851871311664581, 0.04830053076148033, -0.058...
45,385
Good python library for generating audio files?
<p>Can anyone recommend a good library for generating an audio file, such as mp3, wav, or even midi, from python?</p> <p>I've seen recommendations for working with the id tags (song name, artist, etc) in mp3 files, but this is not my goal.</p>
<p>See <a href="http://wiki.python.org/moin/Audio/" rel="noreferrer">http://wiki.python.org/moin/Audio/</a> and <a href="http://wiki.python.org/moin/PythonInMusic" rel="noreferrer">http://wiki.python.org/moin/PythonInMusic</a>, maybe some of the projects listed there can be of help.</p> <p>Also, <a href="http://www.go...
[ -0.10423041880130768, -0.038425784558057785, -0.056452903896570206, -0.0038097163196653128, 0.028519313782453537, -0.03490934148430824, -0.026397980749607086, -0.03573779761791229, -0.04215674474835396, -0.03821722790598869, -0.012448228895664215, -0.009527625516057014, 0.048334140330553055,...
35,948
Django templates and variable attributes
<p>I'm using Google App Engine and Django templates.<br> I have a table that I want to display the objects look something like:<br></p> <pre><code>Object Result: Items = [item1,item2] Users = [{name='username',item1=3,item2=4},..] </code></pre> <p>The Django template is:</p> <pre><code>&lt;table&gt; &lt;tr a...
<p>I found a "nicer"/"better" solution for getting variables inside Its not the nicest way, but it works.</p> <p>You install a custom filter into django which gets the key of your dict as a parameter</p> <p>To make it work in google app-engine you need to add a file to your main directory, I called mine <em>django_ha...
[ 0.031371429562568665, 0.08652838319540024, 0.038031063973903656, 0.014353198930621147, 0.0029599806293845177, 0.0170748233795166, 0.000980681274086237, 0.04205334931612015, -0.04299350827932358, -0.012813822366297245, -0.02550036460161209, -0.027593670412898064, -0.028477150946855545, -0.0...
38,435
Retrieving an Oracle timestamp using Python's Win32 ODBC module
<p>Given an Oracle table created using the following:</p> <pre><code>CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME ZONE); </code></pre> <p>Using the Python ODBC module from its <a href="http://www.python.org/download/windows/" rel="nofollow noreferrer">Win32 extensions</a> (from the win32all package), I tried the...
<p>I believe this is a bug in the Oracle ODBC driver. Basically, the Oracle ODBC driver does not support the <code>TIMESTAMP WITH (LOCAL) TIME ZONE</code> data types, only the <code>TIMESTAMP</code> data type. As you have discovered, one workaround is in fact to use the <code>TO_CHAR</code> method.</p> <p>In your exam...
[ -0.05040934309363365, -0.012369594536721706, -0.08751030266284943, 0.055792804807424545, -0.000519119726959616, -0.12968695163726807, 0.04938832297921181, 0.04122565686702728, 0.00851831678301096, -0.009041019715368748, 0.007664879783987999, -0.04547891393303871, 0.0287239421159029, -0.084...
51,412
Passing on named variable arguments in python
<p>Say I have the following methods:</p> <pre><code>def methodA(arg, **kwargs): pass def methodB(arg, *args, **kwargs): pass </code></pre> <p>In methodA I wish to call methodB, passing on the kwargs. However, it seems that if I define <code>methodA</code> as follows, the second argument will be passed on as ...
<p>Put the asterisks before the kwargs variable. This makes Python pass the variable (which is assumed to be a dictionary) as keyword arguments.</p> <pre><code>methodB("argvalue", **kwargs) </code></pre>
[ -0.020525217056274414, -0.026631832122802734, -0.0515967458486557, -0.01668471284210682, -0.13203276693820953, -0.02684742398560047, 0.06668910384178162, -0.002352637704461813, 0.027313124388456345, -0.02432178519666195, 0.004769861698150635, 0.033738039433956146, 0.03542559966444969, 0.01...
43,315
Can I write native iPhone apps using Python?
<p>Using <a href="http://pyobjc.sourceforge.net/" rel="noreferrer">PyObjC</a>, you can use Python to write Cocoa applications for OS X. Can I write native iPhone apps using Python and if so, how?</p>
<p>Not currently, currently the only languages available to access the iPhone SDK are C/C++, Objective C and Swift.</p> <p>There is no technical reason why this could not change in the future but I wouldn't hold your breath for this happening in the short term.</p> <p>That said, Objective-C and Swift really are not t...
[ -0.049277953803539276, -0.03993437439203262, 0.02264869585633278, 0.009517216123640537, -0.05590963363647461, -0.08560357987880707, -0.04559410363435745, 0.034343402832746506, -0.039475373923778534, -0.042731545865535736, 0.018620679154992104, -0.026684394106268883, 0.03949613869190216, 0....
45,228
Where can I find the time and space complexity of the built-in sequence types in Python
<p>I've been unable to find a source for this information, short of looking through the Python source code myself to determine how the objects work. Does anyone know where I could find this online?</p>
<p>Checkout the <a href="http://wiki.python.org/moin/TimeComplexity" rel="noreferrer">TimeComplexity</a> page on the py dot org wiki. It covers set/dicts/lists/etc at least as far as time complexity goes.</p>
[ -0.06397207081317902, -0.020655330270528793, -0.07598710060119629, 0.026620302349328995, -0.046599969267845154, -0.11983393132686615, -0.016841843724250793, 0.0161147341132164, -0.02042584866285324, 0.003019120544195175, 0.012810449115931988, 0.05693482607603073, -0.010589759796857834, -0....
51,502
Improving Python readability?
<p>I've been really enjoying Python programming lately. I come from a background of a strong love for C-based coding, where everything is perhaps more complicated than it should be (but puts hair on your chest, at least). So switching from C to Python for more complex things that don't require tons of speed has been mo...
<p>Part of learning a new programming language is learning to read code in that language. A crutch like this may make it easier to read your own code, but it's going to impede the process of learning how to read anyone else's Python code. I really think you'd be better off getting rid of the end of block comments and...
[ -0.08032397925853729, 0.05119059607386589, 0.017343437299132347, 0.008665992878377438, -0.042331576347351074, -0.10171838849782944, -0.0033284821547567844, 0.06169784069061279, 0.015884937718510628, -0.029089758172631264, -0.015909746289253235, 0.024057600647211075, 0.03136743977665901, 0....
49,824
Java -> Python?
<p>Besides the dynamic nature of Python (and the syntax), what are some of the major features of the Python language that Java doesn't have, and vice versa?</p>
<ol> <li><p>List comprehensions. I often find myself filtering/mapping lists, and being able to say <code>[line.replace("spam","eggs") for line in open("somefile.txt") if line.startswith("nee")]</code> is really nice.</p></li> <li><p>Functions are first class objects. They can be passed as parameters to other functio...
[ -0.056290000677108765, 0.012306516990065575, 0.0367911234498024, -0.00491240993142128, -0.012714806012809277, -0.07597431540489197, -0.06191956624388695, 0.034188710153102875, -0.04761166125535965, -0.06392975151538849, -0.014347060583531857, 0.03600352257490158, 0.04439390078186989, 0.019...
50,394
What Python way would you suggest to check whois database records?
<p>I'm trying to get a webservice up and running that actually requires to check whois databases. What I'm doing right now is ugly and I'd like to avoid it as much as I can: I call gwhois command and parse its output. Ugly.</p> <p>I did some search to try to find a pythonic way to do this task. Generally I got quite m...
<p>There's nothing wrong with using a command line utility to do what you want. If you put a nice wrapper around the service, you can implement the internals however you want! For example:</p> <pre><code>class Whois(object): _whois_by_query_cache = {} def __init__(self, query): """Initializes the inst...
[ -0.09168802201747894, -0.01087432075291872, -0.06526840478181839, -0.021000932902097702, -0.0030034296214580536, -0.1723869889974594, -0.016871586441993713, -0.07291833311319351, -0.04225079342722893, -0.013049096800386906, -0.007029874715954065, -0.00893461238592863, 0.06374334543943405, ...
42,482
Best way to extract text from a Word doc without using COM/automation?
<p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation? (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p> <p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</...
<p>I use catdoc or antiword for this, whatever gives the result that is the easiest to parse. I have embedded this in python functions, so it is easy to use from the parsing system (which is written in python).</p> <pre><code>import os def doc_to_text_catdoc(filename): (fi, fo, fe) = os.popen3('catdoc -w "%s"' % ...
[ -0.052552707493305206, 0.07274659723043442, 0.008742831647396088, -0.002258186461403966, 0.028338855132460594, -0.07236592471599579, 0.015364625491201878, 0.0035835644230246544, -0.03129107877612114, 0.04731981083750725, 0.015186067670583725, 0.06482655555009842, 0.07056497782468796, -0.00...
54,867
What is the difference between old style and new style classes in Python?
<p>What is the difference between old style and new style classes in Python? When should I use one or the other?</p>
<p>From <em><a href="http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes" rel="noreferrer">New-style and classic classes</a></em>:</p> <blockquote> <p><strong>Up to Python 2.1, old-style classes were the only flavour available to the user.</strong></p> <p>The concept of (old-style) c...
[ -0.09438801556825638, 0.014827737584710121, -0.009524845518171787, 0.001137187471613288, 0.012264304794371128, -0.05800681933760643, -0.05326839163899422, -0.004509952384978533, 0.0037825598847121, -0.003696591593325138, 0.023303644731640816, 0.13260237872600555, 0.043513551354408264, -0.0...
49,137
Calling python from a c++ program for distribution
<p>I would like to call python script files from my c++ program.</p> <p>I am not sure that the people I will distribute to will have python installed.</p>
<p>Boost has a python interface library which could help you.</p> <p><a href="http://www.boost.org/doc/libs/release/libs/python/doc/index.html" rel="noreferrer">Boost.Python</a></p>
[ -0.09286084771156311, 0.03597336262464523, -0.04395756870508194, -0.027455829083919525, -0.08159905672073364, -0.06557243317365646, -0.039917878806591034, 0.1166759729385376, -0.012468698434531689, -0.0329909473657608, -0.042841166257858276, 0.005960825830698013, 0.05251641571521759, -0.00...
53,162
How can I do a line break (line continuation) in Python (split up a long line of source code)?
<p>Given:</p> <pre><code>e = 'a' + 'b' + 'c' + 'd' </code></pre> <p>How do I write the above in two lines?</p> <pre><code>e = 'a' + 'b' + 'c' + 'd' </code></pre> <hr /> <p><sub>See also: <a href="https://stackoverflow.com/questions/10660435">How do I split the definition of a long string over multiple lines?</a> if...
<p>What is the line? You can just have arguments on the next line without any problems:</p> <pre><code>a = dostuff(blahblah1, blahblah2, blahblah3, blahblah4, blahblah5, blahblah6, blahblah7) </code></pre> <p>Otherwise you can do something like this:</p> <pre><code>if (a == True and b == False): </cod...
[ -0.06274475902318954, 0.060942113399505615, 0.07189210504293442, -0.004957450088113546, -0.027546757832169533, 0.00616863090544939, -0.04264643415808678, 0.01693325862288475, -0.07081107795238495, 0.08999628573656082, -0.01882629282772541, -0.024519264698028564, 0.061048902571201324, -0.09...
55,391
Python regular expression for HTML parsing
<p>I want to grab the value of a hidden input field in HTML.</p> <pre><code>&lt;input type="hidden" name="fooId" value="12-3456789-1111111111" /&gt; </code></pre> <p>I want to write a regular expression in Python that will return the value of <code>fooId</code>, given that I know the line in the HTML follows the form...
<p>For this particular case, BeautifulSoup is harder to write than a regex, but it is much more robust... I'm just contributing with the BeautifulSoup example, given that you already know which regexp to use :-)</p> <pre><code>from BeautifulSoup import BeautifulSoup #Or retrieve it from the web, etc. html_data = ope...
[ -0.08580997586250305, 0.10170421004295349, -0.020767152309417725, 0.03316383436322212, 0.01999962143599987, -0.0007445714436471462, 0.033822160214185715, -0.01460929773747921, 0.011695126071572304, -0.07428406178951263, -0.04724059998989105, 0.03284857049584389, 0.07089828699827194, -0.115...
49,146
How can I make an EXE file from a Python program?
<p>I've used several modules to make EXEs for Python, but I'm not sure if I'm doing it right.</p> <p>How should I go about this, and why? Please base your answers on personal experience, and provide references where necessary.</p>
<p><a href="https://pypi.org/project/auto-py-to-exe/" rel="noreferrer">Auto PY to EXE</a> - A .py to .exe converter using a simple graphical interface built using Eel and PyInstaller in Python.</p> <hr> <p><a href="http://www.py2exe.org/" rel="noreferrer">py2exe</a> is probably what you want, but it only works on Wi...
[ -0.04400380328297615, 0.00427217036485672, -0.08750447630882263, 0.02492089942097664, -0.03494716063141823, -0.11705994606018066, 0.023931393399834633, 0.12851330637931824, -0.043470557779073715, 0.045101799070835114, -0.004472735337913036, -0.017399627715349197, 0.08688957244157791, 0.031...
50,499
How do I get the path and name of the python file that is currently executing?
<p>I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process. </p> <p>For example, let's say I have three files. Using <a href="http://docs.python.org/library/functions.html#execfile" rel="noreferrer">execfile</a>:</p> <ul> <li><code>script_1.py<...
<p>p1.py:</p> <pre><code>execfile(&quot;p2.py&quot;) </code></pre> <p>p2.py:</p> <pre><code>import inspect, os print (inspect.getfile(inspect.currentframe())) # script filename (usually with path) print (os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))) # script directory </code></pre>
[ -0.07059342414140701, -0.01056270394474268, -0.0685221403837204, 0.012119761668145657, 0.026713822036981583, -0.08532724529504776, 0.02605609782040119, 0.12566174566745758, 0.007834229618310928, -0.028217485174536705, -0.02784985676407814, 0.02674536220729351, 0.04685824364423752, -0.05804...
53,997
Any good AJAX framework for Google App Engine apps?
<p>I am trying to implement AJAX in my Google App Engine application, and so I am looking for a good AJAX framework that will help me. Anyone has any idea?</p> <p>I am thinking about Google Web Toolkit, how good it is in terms of creating AJAX for Google App Engine?</p>
<p>As Google Web Toolkit is a <a href="http://code.google.com/support/bin/answer.py?answer=54830&amp;topic=10208" rel="nofollow noreferrer">subset of Java</a> it works best when you Java at the backend too. Since Google App Engine is currently <a href="http://code.google.com/appengine/kb/general.html#language" rel="no...
[ -0.1058589443564415, -0.0769752562046051, 0.03954669088125229, -0.056060463190078735, -0.029492110013961792, -0.005393982864916325, -0.04798611253499985, 0.010337522253394127, -0.04733110964298248, -0.07816512137651443, 0.04083164036273956, 0.0275725144892931, 0.0020579148549586535, -0.103...
51,212
How to write a download progress indicator in Python?
<p>I am writing a little application to download files over http (as, for example, described <a href="https://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python#22776">here</a>).</p> <p>I also want to include a little download progress indicator showing the percentage of the download pro...
<p>There's a text progress bar library for python at <a href="http://pypi.python.org/pypi/progressbar/2.2" rel="noreferrer">http://pypi.python.org/pypi/progressbar/2.2</a> that you might find useful:</p> <blockquote> <p>This library provides a text mode progressbar. This is tipically used to display the progress of a ...
[ 0.004160316661000252, 0.05245286598801613, -0.08158635348081589, 0.02751067280769348, -0.007872180081903934, -0.019362010061740875, 0.00169979443307966, 0.07902782410383224, 0.01338866725564003, 0.015156928449869156, -0.018183784559369087, -0.021447602659463882, 0.020617444068193436, -0.05...
51,233
How can I retrieve the page title of a webpage using Python?
<p>How can I retrieve the page title of a webpage (title html tag) using Python?</p>
<p>I'll always use <a href="http://lxml.de/" rel="noreferrer">lxml</a> for such tasks. You could use <a href="http://www.crummy.com/software/BeautifulSoup/" rel="noreferrer">beautifulsoup</a> as well.</p> <pre><code>import lxml.html t = lxml.html.parse(url) print(t.find(&quot;.//title&quot;).text) </code></pre> <p>EDIT...
[ -0.04964863508939743, 0.08168575167655945, 0.006995017174631357, -0.022467106580734253, 0.00990329496562481, -0.10339127480983734, -0.030408384278416634, -0.031001826748251915, -0.027650298550724983, -0.059610966593027115, -0.012186004780232906, 0.04086167365312576, 0.08711044490337372, -0...
51,553
Why are SQL aggregate functions so much slower than Python and Java (or Poor Man's OLAP)
<p>I need a real DBA's opinion. Postgres 8.3 takes 200 ms to execute this query on my Macbook Pro while Java and Python perform the same calculation in under 20 ms (350,000 rows):</p> <pre><code>SELECT count(id), avg(a), avg(b), avg(c), avg(d) FROM tuples; </code></pre> <p>Is this normal behaviour when using a SQL da...
<p>Postgres is doing a lot more than it looks like (maintaining data consistency for a start!)</p> <p>If the values don't have to be 100% spot on, or if the table is updated rarely, but you are running this calculation often, you might want to look into Materialized Views to speed it up.</p> <p>(Note, I have not used...
[ -0.007262014783918858, -0.016210203990340233, -0.054100193083286285, 0.05534093454480171, -0.06742404401302338, -0.11211137473583221, 0.012417330406606197, 0.017672570422291756, -0.019421938806772232, 0.03962826728820801, -0.003307173727080226, -0.0077131870202720165, 0.06690012663602829, ...
47,198
Which Version of Python to Use for Maximum Compatibility
<p>If I was going to start an open source project using Python what version should I use to ensure that the vast majority of users can use it on their system?</p> <p>I'm the kind of person who quickly jumps to the next version (which I'll do when Python 3 comes out) but many people may be more conservative if their cu...
<p>As python is in kind of an transition phase towards python 3 with breaking backward compatibility I don't think it is a good idea to go python 3 only. Based on the <a href="http://www.python.org/dev/peps/pep-3000/#timeline" rel="nofollow noreferrer">time line</a> there will be at least one or two following releases ...
[ 0.005622227676212788, -0.058310531079769135, 0.017044562846422195, -0.051755793392658234, 0.06362499296665192, -0.06466296315193176, -0.0687021017074585, 0.07412717491388321, -0.04313012957572937, -0.02895960584282875, -0.02105502411723137, 0.07091362774372101, -0.04202645644545555, -0.006...
55,365
How can I get Emacs' key bindings in Python's IDLE?
<p>I use Emacs primarily for coding Python but sometimes I use IDLE. Is there a way to change the key bindings easily in IDLE to match Emacs?</p>
<p>IDLE provides Emacs keybindings without having to install other software. </p> <ol> <li>Open up the menu item Options -> Configure IDLE...</li> <li>Go to Keys tab</li> <li>In the drop down menu on the right side of the dialog change the select to "IDLE Classic Unix"</li> </ol> <p>It's not the true emacs key bindin...
[ -0.060368623584508896, -0.03785698860883713, -0.0036729632411152124, 0.016572030261158943, -0.05572668835520744, -0.03166419267654419, 0.07403872162103653, 0.004854056052863598, 0.012230461463332176, 0.039733920246362686, 0.0437445305287838, 0.030660074204206467, 0.0729290321469307, -0.035...
47,701
Is there a way to attach a debugger to a multi-threaded Python process?
<p>I'm trying to debug a deadlock in a multi-threaded Python application after it has locked up. Is there a way to attach a debugger to inspect the state of the process? </p> <p>Edit: I'm attempting this on Linux, but it would be great if there were a cross-platform solution. It's Python after all :)</p>
<p>Yeah, gdb is good for lower level debugging.</p> <p>You can change threads with the <em>thread</em> command.</p> <p>e.g</p> <pre><code>(gdb) thr 2 [Switching to thread 2 (process 6159 thread 0x3f1b)] (gdb) backtrace .... </code></pre> <p>You could also check out Python specific debuggers like <a href="http://win...
[ -0.06818687170743942, -0.030599158257246017, -0.06799229234457016, -0.02503141388297081, 0.056995030492544174, -0.07843033969402313, -0.0258384607732296, 0.006040121428668499, -0.021777942776679993, 0.01693788170814514, 0.006786308251321316, -0.02854223921895027, -0.039864055812358856, -0....
56,229
XML writing tools for Python
<p>I'm currently trying ElementTree and it looks fine, it escapes HTML entities and so on and so forth. Am I missing something truly wonderful I haven't heard of?</p> <p>This is similar to what I'm actually doing:</p> <pre><code>import xml.etree.ElementTree as ET root = ET.Element('html') head = ET.SubElement(root,'h...
<p>I assume that you're actually creating an XML DOM tree, because you want to validate that what goes into this file is valid XML, since otherwise you'd just write a static string to a file. If validating your output is indeed your goal, then I'd suggest</p> <pre><code>from xml.dom.minidom import parseString doc = ...
[ -0.015493927523493767, 0.05219889059662819, 0.10271042585372925, 0.02170456387102604, 0.06610007584095001, -0.09667807817459106, 0.014315524138510227, -0.007275910582393408, 0.03439237177371979, -0.029466399922966957, 0.0063993437215685844, -0.0012767850421369076, 0.06012025102972984, -0.1...
53,225
How do you check whether a python method is bound or not?
<p>Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?</p>
<pre class="lang-py prettyprint-override"><code>def isbound(method): return method.im_self is not None def instance(bounded_method): return bounded_method.im_self </code></pre> <p><a href="https://docs.python.org/2.7/reference/datamodel.html#index-40" rel="noreferrer">User-defined methods:</a></p> <blockqu...
[ -0.011344351805746555, 0.03390579670667648, -0.047213561832904816, -0.010492763482034206, -0.0034064941573888063, -0.015077440068125725, -0.00786731205880642, 0.017938632518053055, -0.005828008987009525, -0.017724471166729927, -0.042186930775642395, 0.03671547397971153, -0.007133760489523411...
53,513
How do I check if a list is empty?
<p>For example, if passed the following:</p> <pre><code>a = [] </code></pre> <p>How do I check to see if <code>a</code> is empty?</p>
<pre><code>if not a: print(&quot;List is empty&quot;) </code></pre> <p>Using the <a href="https://docs.python.org/library/stdtypes.html#truth-value-testing" rel="noreferrer">implicit booleanness</a> of the empty <code>list</code> is quite Pythonic.</p>
[ 0.05589934438467026, 0.021429363638162613, -0.029277455061674118, 0.008179305121302605, 0.01315333228558302, -0.03008153662085533, -0.0207864698022604, -0.030901586636900902, 0.06672541797161102, -0.01411449071019888, 0.007845212705433369, -0.036256637424230576, 0.06740175187587738, -0.092...
53,786
How do i generate a histogram for a given probability distribution (for functional testing a server)?
<p>I am trying to automate functional testing of a server using a realistic frequency distribution of requests. (sort of load testing, sort of simulation)</p> <p>I've chosen the <a href="http://en.wikipedia.org/wiki/Weibull_distribution" rel="nofollow noreferrer">Weibull</a> distribution as it "sort of" matches the di...
<p>This is quick and probably not that accurate, but if you calculate the PDF yourself, then at least you make it easier to lay several smaller/larger ones on a single timeline. <code>dev</code> is the std deviation in the Guassian noise, which controls the roughness. Note that this is <em>not</em> the 'right' way to g...
[ -0.05531952902674675, 0.018909040838479996, -0.013744914904236794, 0.11096581816673279, -0.08146288245916367, -0.11467885226011276, -0.03799008950591087, -0.02883908525109291, 0.011360201984643936, -0.05651763826608658, 0.018974820151925087, -0.04889703914523125, 0.0493534691631794, -0.019...
58,294
How do I get the external IP of a socket in Python?
<p>When I call <code>socket.getsockname()</code> on a socket object, it returns a tuple of my machine's internal IP and the port. However, I would like to retrieve my external IP. What's the cheapest, most efficient manner of doing this?</p>
<p>This isn't possible without cooperation from an external server, because there could be any number of NATs between you and the other computer. If it's a custom protocol, you could ask the other system to report what address it's connected to.</p>
[ -0.06582589447498322, 0.01385213527828455, -0.013671905733644962, 0.0000881590458448045, -0.05117848142981529, -0.06211218237876892, 0.011334612965583801, 0.0442052036523819, 0.019667671993374825, -0.020242230966687202, 0.018025768920779228, 0.03145527094602585, -0.07157954573631287, -0.08...
58,119
Does re.compile() or any given Python library call throw an exception?
<p>I can't tell from the Python documentation whether the re.compile(x) function may throw an exception (assuming you pass in a string). I imagine there is something that could be considered an invalid regular expression. The larger question is, where do I go to find if a given Python library call may throw exception(s...
<p>Well, <code>re.compile</code> certainly may:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.compile('he(lo') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "C:\Python25\lib\re.py", line 180, in compile return _compile(pattern, flags) File "C:\Python25\lib\re...
[ 0.0027510973159223795, 0.02832808345556259, 0.019700082018971443, -0.02197287231683731, 0.023122139275074005, -0.013267775997519493, -0.01859671249985695, 0.014407690614461899, -0.07480119913816452, -0.04226841777563095, -0.047784171998500824, 0.03471652418375015, 0.061703700572252274, -0....
683
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...
[ -0.07712186127901077, 0.059465307742357254, -0.04389176517724991, 0.08273030817508698, -0.004728115629404783, -0.010398566722869873, 0.014070849865674973, 0.0016059493646025658, -0.04266352206468582, -0.012626700103282928, -0.03559539094567299, 0.04803227633237839, 0.08881088346242905, 0.0...