title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
Python: inserting double or single quotes around a string
2,439,027
<p>Im using python to access a MySQL database and im getting a unknown column in field due to quotes not being around the variable.</p> <p>code below:</p> <pre><code>cur = x.cnx.cursor() cur.execute('insert into tempPDBcode (PDBcode) values (%s);' % (s)) rows = cur.fetchall() </code></pre> <p>How do i manually inse...
3
2010-03-13T16:20:20Z
2,439,047
<p>If this were purely a string-handling question, the answer would be tojust put them in the string:</p> <pre><code>cur.execute('insert into tempPDBcode (PDBcode) values ("%s");' % (s)) </code></pre> <p>That's the classic use case for why Python supports both kinds of quotes.</p> <p>However as other answers &amp; ...
-4
2010-03-13T16:24:25Z
[ "python", "sql", "quotes" ]
Python: inserting double or single quotes around a string
2,439,027
<p>Im using python to access a MySQL database and im getting a unknown column in field due to quotes not being around the variable.</p> <p>code below:</p> <pre><code>cur = x.cnx.cursor() cur.execute('insert into tempPDBcode (PDBcode) values (%s);' % (s)) rows = cur.fetchall() </code></pre> <p>How do i manually inse...
3
2010-03-13T16:20:20Z
2,439,048
<p>You shouldn't use Python's string functions to build the SQL statement. You run the risk of leaving an SQL injection vulnerability. You should do this instead:</p> <pre><code>cur.execute('insert into tempPDBcode (PDBcode) values (%s);', s) </code></pre> <p>Note the comma.</p>
10
2010-03-13T16:24:25Z
[ "python", "sql", "quotes" ]
Python: inserting double or single quotes around a string
2,439,027
<p>Im using python to access a MySQL database and im getting a unknown column in field due to quotes not being around the variable.</p> <p>code below:</p> <pre><code>cur = x.cnx.cursor() cur.execute('insert into tempPDBcode (PDBcode) values (%s);' % (s)) rows = cur.fetchall() </code></pre> <p>How do i manually inse...
3
2010-03-13T16:20:20Z
2,439,054
<p>Python will do this for you automatically, if you use the database API:</p> <pre><code>cur = x.cnx.cursor() cur.execute('insert into tempPDBcode (PDBcode) values (%s)',s) </code></pre> <p>Using the DB API means that python will figure out whether to use quotes or not, and also means that you don't have to worry a...
5
2010-03-13T16:25:01Z
[ "python", "sql", "quotes" ]
how to embed a webpage using wx?
2,439,039
<p>I need to show a webpage (a complex page with script and stuff, no static html) in a frame or something. It's for a desktop application, I'm using python 2.6 + wxPython 2.8.10.1. I need to catch some events too (mostly about changing page). I've found some samples using the webview module in a gtk application, but I...
0
2010-03-13T16:22:59Z
2,439,208
<p>You can embed IE, but I think that's about it. wxWebKit is working on a wx add-on to use WebKit as an embedded browser in wx, but I think it's still a work in progress.</p>
1
2010-03-13T17:15:28Z
[ "python", "wx" ]
how to embed a webpage using wx?
2,439,039
<p>I need to show a webpage (a complex page with script and stuff, no static html) in a frame or something. It's for a desktop application, I'm using python 2.6 + wxPython 2.8.10.1. I need to catch some events too (mostly about changing page). I've found some samples using the webview module in a gtk application, but I...
0
2010-03-13T16:22:59Z
2,439,387
<p>There is a commercial solution for this called <a href="http://www.kirix.com/labs/wxwebconnect.html" rel="nofollow">wxWebConnect</a> that uses Gecko (the Mozilla engine). I've never used it myself because i'm waiting for the wxWebKit project to be ready to use but it looks pretty good although perhaps a little overk...
1
2010-03-13T18:02:50Z
[ "python", "wx" ]
Is it possible to use re2 from Python?
2,439,345
<p>i just discovered <a href="http://code.google.com/p/re2">http://code.google.com/p/re2</a>, a promising library that uses a long-neglected way (<a href="http://swtch.com/~rsc/regexp/regexp1.html">Thompson NFA</a>) to implement a regular expression engine that can be orders of magnitudes faster than the available engi...
20
2010-03-13T17:51:26Z
2,439,399
<p>You could try to build re2 into its own DLL/so and use ctypes to call functions from that DLL/so. You will probably need to define your own entry points in the DLL/so.</p>
0
2010-03-13T18:06:16Z
[ "python", "regex", "re2" ]
Is it possible to use re2 from Python?
2,439,345
<p>i just discovered <a href="http://code.google.com/p/re2">http://code.google.com/p/re2</a>, a promising library that uses a long-neglected way (<a href="http://swtch.com/~rsc/regexp/regexp1.html">Thompson NFA</a>) to implement a regular expression engine that can be orders of magnitudes faster than the available engi...
20
2010-03-13T17:51:26Z
2,439,545
<p>Possible yes, easy no. Looking at the re2.h, this is a C++ library exposed as a class. There are two ways you could use it from Python. </p> <p>1.) As Tuomas says, compile it as a DLL/so and use ctypes. In order to use it from python, though, you would need to wrap the object init and methods into c style extern...
5
2010-03-13T18:46:36Z
[ "python", "regex", "re2" ]
Is it possible to use re2 from Python?
2,439,345
<p>i just discovered <a href="http://code.google.com/p/re2">http://code.google.com/p/re2</a>, a promising library that uses a long-neglected way (<a href="http://swtch.com/~rsc/regexp/regexp1.html">Thompson NFA</a>) to implement a regular expression engine that can be orders of magnitudes faster than the available engi...
20
2010-03-13T17:51:26Z
2,439,553
<p><a href="http://www.swig.org/" rel="nofollow">SWIG</a> handles C++ (unlike ctypes), so it may be more straightforward to use it.</p>
3
2010-03-13T18:50:03Z
[ "python", "regex", "re2" ]
Is it possible to use re2 from Python?
2,439,345
<p>i just discovered <a href="http://code.google.com/p/re2">http://code.google.com/p/re2</a>, a promising library that uses a long-neglected way (<a href="http://swtch.com/~rsc/regexp/regexp1.html">Thompson NFA</a>) to implement a regular expression engine that can be orders of magnitudes faster than the available engi...
20
2010-03-13T17:51:26Z
2,457,569
<p>David Reiss has put together a Python wrapper for re2. It doesn't have all of the functionality of Python's re module, but it's a start. It's available here: <a href="http://github.com/facebook/pyre2">http://github.com/facebook/pyre2</a>.</p>
12
2010-03-16T19:47:36Z
[ "python", "regex", "re2" ]
Where to find a list of all the possible HTML tags in Python?
2,439,374
<p>Is there a standard module in Python that lists all the HTML tags? </p> <p>For example, I would like to do things like:</p> <pre><code>if is_valid_html_tag('div'): print 'div is a valid tag' if is_not_valid_html_tag('boda'): print 'boda is not a valid tag' </code></pre> <p>To do this I need a list of all t...
1
2010-03-13T18:01:03Z
2,439,851
<p>I don't know whether there is a built in module that does just that. I'd suggest finding a list of tags, then writing a function like this...</p> <pre><code>def is_valid_html_tag(tag_name): tags=["a","abbr","acronym","address","area","b","base","bdo","big","blockquote","body","br","button","caption","cite","cod...
4
2010-03-13T20:07:03Z
[ "python", "html", "tags" ]
Where to find a list of all the possible HTML tags in Python?
2,439,374
<p>Is there a standard module in Python that lists all the HTML tags? </p> <p>For example, I would like to do things like:</p> <pre><code>if is_valid_html_tag('div'): print 'div is a valid tag' if is_not_valid_html_tag('boda'): print 'boda is not a valid tag' </code></pre> <p>To do this I need a list of all t...
1
2010-03-13T18:01:03Z
2,441,108
<p>Since python has all kinds of modules - smtp, xml, etc, the question asks if there is a module like htmltags that would list all the valid html tags.</p> <p>When you search the <a href="http://docs.python.org" rel="nofollow">http://docs.python.org</a> site, you didn't find any such thing, did you?</p> <p>When you ...
-1
2010-03-14T03:48:18Z
[ "python", "html", "tags" ]
Where to find a list of all the possible HTML tags in Python?
2,439,374
<p>Is there a standard module in Python that lists all the HTML tags? </p> <p>For example, I would like to do things like:</p> <pre><code>if is_valid_html_tag('div'): print 'div is a valid tag' if is_not_valid_html_tag('boda'): print 'boda is not a valid tag' </code></pre> <p>To do this I need a list of all t...
1
2010-03-13T18:01:03Z
20,855,962
<p>Use this: <a href="https://github.com/html5lib/html5lib-python/blob/master/html5lib/sanitizer.py" rel="nofollow">https://github.com/html5lib/html5lib-python/blob/master/html5lib/sanitizer.py</a></p> <pre><code>from html5lib.sanitizer import HTMLSanitizerMixin print(HTMLSanitizerMixin.acceptable_elements) </code></p...
2
2013-12-31T10:38:39Z
[ "python", "html", "tags" ]
Parsing/Tokenizing a String Containing a SQL Command
2,439,618
<p>Are there any open source libraries (any language, python/PHP preferred) that will tokenize/parse an ANSI SQL string into its various components? </p> <p>That is, if I had the following string</p> <pre><code> SELECT a.foo, b.baz, a.bar FROM TABLE_A a LEFT JOIN TABLE_B b ON a.id = b.id WHERE baz = 'snafu'; </c...
6
2010-03-13T19:04:08Z
2,439,710
<p>SQLite source has a file named <code>parse.y</code> that contains grammar for SQL. You can pass that file to <a href="http://www.hwaci.com/sw/lemon/" rel="nofollow">lemon parser generator</a> to generate C code that executes the grammar. </p>
2
2010-03-13T19:31:09Z
[ "php", "python", "sql", "parsing", "tokenize" ]
Can I override a query in DJango?
2,439,631
<p>I know you can override delete and save methods in DJango models, but can you override a select query somehow to intercept and change a parameter slightly. I have a hashed value I want to check for, and would like to keep the hashing internal to the model.</p>
0
2010-03-13T19:07:56Z
2,439,676
<p>You don't make it absolutely clear what you want to do, but I think there are two possibilities here.</p> <p>The general way to override the database query is to define a <a href="http://docs.djangoproject.com/en/1.1/topics/db/managers/" rel="nofollow">custom Manager</a>, and override <code>get_query_set</code> met...
3
2010-03-13T19:21:35Z
[ "python", "django" ]
Can I override a query in DJango?
2,439,631
<p>I know you can override delete and save methods in DJango models, but can you override a select query somehow to intercept and change a parameter slightly. I have a hashed value I want to check for, and would like to keep the hashing internal to the model.</p>
0
2010-03-13T19:07:56Z
2,444,043
<p>if you're using 1.2, you can try <a href="http://docs.djangoproject.com/en/dev/topics/db/sql/" rel="nofollow">raw()</a>, which seems like exactly what you're looking for.</p>
1
2010-03-14T22:25:55Z
[ "python", "django" ]
Is it possible to put a wx.window (frame/panel) over a wx.MenuBar?
2,439,677
<p>I want to know if it's possible to put a frame or a panel over a menubar using wxpython?</p> <p>Thanks in advance!</p>
0
2010-03-13T19:21:39Z
2,463,869
<p>Your only chance is to write some Custom Menu, which could be bit difficult but doable. So basically instead of using system menu etc, you create windows inside your main frame which look like menu.</p>
1
2010-03-17T16:09:39Z
[ "python", "wxpython" ]
How can we change a wx.MenuBar background and foreground colour using wxpython?
2,439,708
<p>I want to change the wxpython menubar colours.</p> <p>How can I do it?</p>
-2
2010-03-13T19:30:17Z
2,440,868
<p>don't think it's doable -- it defaults to the platform's native look</p>
0
2010-03-14T01:41:07Z
[ "python", "wxpython" ]
How can we change a wx.MenuBar background and foreground colour using wxpython?
2,439,708
<p>I want to change the wxpython menubar colours.</p> <p>How can I do it?</p>
-2
2010-03-13T19:30:17Z
2,463,861
<p>There are two ways</p> <ol> <li><p>Hard way , write your own custom menu bar from PyControl, position it at top of window, and create popup windows for submenus.</p></li> <li><p>Easier way: you can use win32api to change system menu colors for your app. Here is an example showing that <a href="http://code.activesta...
2
2010-03-17T16:08:14Z
[ "python", "wxpython" ]
How to accept localized date format (e.g dd/mm/yy) in a DateField on an admin form?
2,439,801
<p>Is it possible to customize a django application to have accept localized date format (e.g dd/mm/yy) in a DateField on an admin form ?</p> <p>I have a model class :</p> <pre><code>class MyModel(models.Model): date = models.DateField("Date") </code></pre> <p>And associated admin class</p> <pre><code>...
3
2010-03-13T19:54:28Z
2,439,847
<p>The admin system uses a default <code>ModelForm</code> for editing the objects. You'll need to provide a custom form so that you can begin overriding field behaviour.</p> <p>Inside your modelform, override the field using a <code>DateField</code>, and use the <a href="http://docs.djangoproject.com/en/1.1/ref/forms/...
3
2010-03-13T20:05:32Z
[ "python", "django", "django-admin" ]
How to accept localized date format (e.g dd/mm/yy) in a DateField on an admin form?
2,439,801
<p>Is it possible to customize a django application to have accept localized date format (e.g dd/mm/yy) in a DateField on an admin form ?</p> <p>I have a model class :</p> <pre><code>class MyModel(models.Model): date = models.DateField("Date") </code></pre> <p>And associated admin class</p> <pre><code>...
3
2010-03-13T19:54:28Z
3,014,991
<p>The fix proposed by Ben is working but the calendar on the right is not shown anymore.</p> <p>Here is an improvement. The calendar will still be shown and the date will be displayed with the %d%%M/%Y format.</p> <pre><code>from django.forms.models import ModelForm from django.contrib.admin.widgets import AdminDate...
2
2010-06-10T13:50:04Z
[ "python", "django", "django-admin" ]
Check if a MediaWiki page exists (Python)
2,439,824
<p>I'm working on a Python script that transforms this:</p> <pre><code>foo bar </code></pre> <p>Into this:</p> <pre><code>[[Component foo]] [[bar]] </code></pre> <p>The script checks (per input line) if the page "Component foo" exists. If it exists then a link to that page is created, if it doesn't exist then a dir...
2
2010-03-13T19:59:31Z
2,439,853
<p>If you have local access to the wiki database, it might be easiest to do a query against the database to see whether each page exists.</p> <p>If you only have HTTP access, you might try the <a href="http://wwwsearch.sourceforge.net/mechanize/" rel="nofollow">mechanize</a> library which lets you programmatically aut...
2
2010-03-13T20:08:00Z
[ "python", "mediawiki" ]
Check if a MediaWiki page exists (Python)
2,439,824
<p>I'm working on a Python script that transforms this:</p> <pre><code>foo bar </code></pre> <p>Into this:</p> <pre><code>[[Component foo]] [[bar]] </code></pre> <p>The script checks (per input line) if the page "Component foo" exists. If it exists then a link to that page is created, if it doesn't exist then a dir...
2
2010-03-13T19:59:31Z
2,495,054
<p>You should be able to use the MediaWiki API. <a href="http://www.mediawiki.org/wiki/API" rel="nofollow">http://www.mediawiki.org/wiki/API</a> (maybe under Queries or Creating/Editing)</p> <p>I'm not too familiar with it, but for example, you could compare the output of an existing page with a nonexistent page.</p>...
1
2010-03-22T19:19:17Z
[ "python", "mediawiki" ]
Check if a MediaWiki page exists (Python)
2,439,824
<p>I'm working on a Python script that transforms this:</p> <pre><code>foo bar </code></pre> <p>Into this:</p> <pre><code>[[Component foo]] [[bar]] </code></pre> <p>The script checks (per input line) if the page "Component foo" exists. If it exists then a link to that page is created, if it doesn't exist then a dir...
2
2010-03-13T19:59:31Z
2,495,799
<p>You can definitely use the API to check if a page exists:</p> <pre><code>#Assuming words is a list of words you wish to query for import urllib # replace en.wikipedia.org with the address of the wiki you want to access query = "http://en.wikipedia.org/w/api?action=query&amp;titles=%s&amp;format=xml" % "|".join(wor...
7
2010-03-22T21:13:21Z
[ "python", "mediawiki" ]
Check if a MediaWiki page exists (Python)
2,439,824
<p>I'm working on a Python script that transforms this:</p> <pre><code>foo bar </code></pre> <p>Into this:</p> <pre><code>[[Component foo]] [[bar]] </code></pre> <p>The script checks (per input line) if the page "Component foo" exists. If it exists then a link to that page is created, if it doesn't exist then a dir...
2
2010-03-13T19:59:31Z
2,495,937
<p>Since the pages are stored in the database, you will have to access that one way or another. Since you don't have local access the API, as suggested, is probably it - but there may be alternatives. </p> <p><a href="http://www.mwusers.com/forums/forum.php" rel="nofollow">http://www.mwusers.com/forums/forum.php</a></...
0
2010-03-22T21:34:27Z
[ "python", "mediawiki" ]
Check if a MediaWiki page exists (Python)
2,439,824
<p>I'm working on a Python script that transforms this:</p> <pre><code>foo bar </code></pre> <p>Into this:</p> <pre><code>[[Component foo]] [[bar]] </code></pre> <p>The script checks (per input line) if the page "Component foo" exists. If it exists then a link to that page is created, if it doesn't exist then a dir...
2
2010-03-13T19:59:31Z
2,495,956
<p>Use <a href="https://www.mediawiki.org/wiki/Manual:Pywikibot" rel="nofollow">Pywikibot</a> to interact with the MediaWiki software. It's probably the most powerful bot framework available.</p> <blockquote> <p>The <strong>Python Wikipediabot Framework</strong> (<strong>pywikipedia</strong> or <strong>PyWikipediaBo...
5
2010-03-22T21:37:26Z
[ "python", "mediawiki" ]
How to print a range with decimal points in Python?
2,439,837
<p>I can print a range of numbers easily using range, but is is possible to print a range with 1 decimal place from -10 to 10?</p> <p>e.g</p> <p><code>-10.0, -9.9, -9.8</code> all they way through to +10?</p>
1
2010-03-13T20:01:55Z
2,439,844
<pre><code>[i/10.0 for i in range(-100,101)] </code></pre> <p>(The <code>.0</code> is not needed in Python 3.x)</p>
7
2010-03-13T20:03:59Z
[ "python", "range" ]
How to print a range with decimal points in Python?
2,439,837
<p>I can print a range of numbers easily using range, but is is possible to print a range with 1 decimal place from -10 to 10?</p> <p>e.g</p> <p><code>-10.0, -9.9, -9.8</code> all they way through to +10?</p>
1
2010-03-13T20:01:55Z
2,439,859
<p><code>numpy.arange</code> might serve your purpose well.</p>
0
2010-03-13T20:09:26Z
[ "python", "range" ]
How to print a range with decimal points in Python?
2,439,837
<p>I can print a range of numbers easily using range, but is is possible to print a range with 1 decimal place from -10 to 10?</p> <p>e.g</p> <p><code>-10.0, -9.9, -9.8</code> all they way through to +10?</p>
1
2010-03-13T20:01:55Z
2,439,912
<p>There's <a href="http://code.activestate.com/recipes/66472-frange-a-range-function-with-float-increments/" rel="nofollow">a recipe on ActiveState</a> that implements a floating-point range. In your example, you can use it like</p> <pre><code>frange(-10, 10.01, 0.1) </code></pre> <p>Note that this won't generate 1 ...
2
2010-03-13T20:29:00Z
[ "python", "range" ]
How to print a range with decimal points in Python?
2,439,837
<p>I can print a range of numbers easily using range, but is is possible to print a range with 1 decimal place from -10 to 10?</p> <p>e.g</p> <p><code>-10.0, -9.9, -9.8</code> all they way through to +10?</p>
1
2010-03-13T20:01:55Z
2,440,638
<pre><code>print(', '.join('%.1f' % x/10.0 for x in range(-100, 101))) </code></pre> <p>should do exactly what you ask (the printing part, that is!) in any level of Python (a pretty long line of course -- a few hundred characters!-). You could omit the outer parenthese in Python 2, could omit the <code>.0</code> bit ...
0
2010-03-14T00:09:14Z
[ "python", "range" ]
How to print a range with decimal points in Python?
2,439,837
<p>I can print a range of numbers easily using range, but is is possible to print a range with 1 decimal place from -10 to 10?</p> <p>e.g</p> <p><code>-10.0, -9.9, -9.8</code> all they way through to +10?</p>
1
2010-03-13T20:01:55Z
2,441,838
<p>Define a simple function, like:</p> <pre><code>def frange(fmin, fmax, fleap): cur=fmin while cur&lt;fmax: yield cur cur+=fleap </code></pre> <p>Call it using <code>generators</code>, e.g.: </p> <pre><code>my_float_range=[i for i in frange(-10,10,.1)] </code></pre> <p>Some sanity...
0
2010-03-14T10:33:17Z
[ "python", "range" ]
How can I use Python with Mechanize for posting multipart/form-data?
2,439,900
<p>I am using <a href="http://pypi.python.org/pypi/mechanize/0.1.11" rel="nofollow">http://pypi.python.org/pypi/mechanize/0.1.11</a> for programmatic web browsing, I want to be able to upload files to servers the same way the browser does (by sending the content as multipart/form-data, defined in RFC2388)</p> <p>Is th...
2
2010-03-13T20:23:03Z
2,440,625
<p>There's a couple of good answer on <a href="http://stackoverflow.com/questions/1299855/upload-file-with-python-mechanize">this SO question</a>, one with bare mechanize and one with twill on top of it, and I believe they both end up sending multipart/form-data as you want.</p>
2
2010-03-14T00:05:55Z
[ "python", "mechanize", "multipartform-data" ]
How to remove lowercase sentence fragments from text?
2,439,968
<p>I'm tyring to remove lowercase sentence fragments from standard text files using regular expresions or a simple Perl oneliner. </p> <p>These are commonly referred to as speech or attribution tags, for example - he said, she said, etc. </p> <p>This example shows before and after using manual deletion:</p> <ol> ...
2
2010-03-13T20:48:09Z
2,440,031
<p>Here's a Python snippet that should do:</p> <pre><code> thetext="""triple quoted paste of your sample text""" y=thetext.split('\n') for line in y: m=re.findall('(".*?")',line) if m: print ' '.join(m) else: print line </code></pre>
3
2010-03-13T21:07:30Z
[ "python", "regex", "perl", "awk" ]
How to remove lowercase sentence fragments from text?
2,439,968
<p>I'm tyring to remove lowercase sentence fragments from standard text files using regular expresions or a simple Perl oneliner. </p> <p>These are commonly referred to as speech or attribution tags, for example - he said, she said, etc. </p> <p>This example shows before and after using manual deletion:</p> <ol> ...
2
2010-03-13T20:48:09Z
2,440,037
<p>The <a href="http://search.cpan.org/dist/Text-Balanced/lib/Text/Balanced.pm" rel="nofollow"><code>Text::Balanced</code></a> module is what you seem to be after if you're looking to use Perl. The following should be able to extract all the quoted speech in your example (not pretty, but gets the job done).</p> <p>It ...
0
2010-03-13T21:09:25Z
[ "python", "regex", "perl", "awk" ]
How to remove lowercase sentence fragments from text?
2,439,968
<p>I'm tyring to remove lowercase sentence fragments from standard text files using regular expresions or a simple Perl oneliner. </p> <p>These are commonly referred to as speech or attribution tags, for example - he said, she said, etc. </p> <p>This example shows before and after using manual deletion:</p> <ol> ...
2
2010-03-13T20:48:09Z
2,440,170
<p>I am not entirely sure which editor are you using, if you are using something editor that supports atomic grouping (e.g. EditorPad Pro) You can use the regular expression below to do the search and replace:</p> <p>Search for </p> <pre><code>(".+?"|^[A-Z].+\r\n)(.(?!"))* Note: you should replace \r\n with \n or \r...
0
2010-03-13T21:49:23Z
[ "python", "regex", "perl", "awk" ]
How to remove lowercase sentence fragments from text?
2,439,968
<p>I'm tyring to remove lowercase sentence fragments from standard text files using regular expresions or a simple Perl oneliner. </p> <p>These are commonly referred to as speech or attribution tags, for example - he said, she said, etc. </p> <p>This example shows before and after using manual deletion:</p> <ol> ...
2
2010-03-13T20:48:09Z
2,440,859
<p>This works for all cases shown in the question:</p> <pre><code>sed -n '/"/!{p;b}; s/\(.*\)"[^"]*/\1" /;s/\(.*"\)\([^"]*\)\(".*"\)/\1 \3/;p' textfile </code></pre> <p>It fails for cases such as these:</p> <pre><code>He said, "It doesn't always work." "Secondly," I said, "it fails for three quoted phrases..." He c...
0
2010-03-14T01:37:05Z
[ "python", "regex", "perl", "awk" ]
How to remove lowercase sentence fragments from text?
2,439,968
<p>I'm tyring to remove lowercase sentence fragments from standard text files using regular expresions or a simple Perl oneliner. </p> <p>These are commonly referred to as speech or attribution tags, for example - he said, she said, etc. </p> <p>This example shows before and after using manual deletion:</p> <ol> ...
2
2010-03-13T20:48:09Z
2,440,964
<p>If I understand what you are after... passing each line through a regex like this should work...</p> <p>You can use the perl debugger to play around with this. Hop into the perl debugger with just a <code>perl -de 42</code> on the command line in linux/mac. (The "42" is just a valid expression - it could be anyt...
0
2010-03-14T03:24:41Z
[ "python", "regex", "perl", "awk" ]
Python 3: Most efficient way to create a [func(i) for i in range(N)] list comprehension
2,439,986
<p>Say I have a function func(i) that creates an object for an integer i, and N is some nonnegative integer. Then what's the fastest way to create a list (not a range) equal to this list</p> <pre><code>mylist = [func(i) for i in range(N)] </code></pre> <p>without resorting to advanced methods like creating a function...
7
2010-03-13T20:54:43Z
2,440,045
<p>Using list comprehension to accomplish what you're trying to do would be more pythonic way to do it. Despite performance penalty:).</p>
0
2010-03-13T21:11:55Z
[ "python", "list-comprehension" ]
Python 3: Most efficient way to create a [func(i) for i in range(N)] list comprehension
2,439,986
<p>Say I have a function func(i) that creates an object for an integer i, and N is some nonnegative integer. Then what's the fastest way to create a list (not a range) equal to this list</p> <pre><code>mylist = [func(i) for i in range(N)] </code></pre> <p>without resorting to advanced methods like creating a function...
7
2010-03-13T20:54:43Z
2,440,058
<p>There is no difference in computational complexity between using an autoresizing array and preallocating an array. At worst, it costs about O(2N). See here: </p> <p><a href="http://stackoverflow.com/questions/200384/constant-amortized-time/249695#249695">http://stackoverflow.com/questions/200384/constant-amortize...
2
2010-03-13T21:14:54Z
[ "python", "list-comprehension" ]
Python 3: Most efficient way to create a [func(i) for i in range(N)] list comprehension
2,439,986
<p>Say I have a function func(i) that creates an object for an integer i, and N is some nonnegative integer. Then what's the fastest way to create a list (not a range) equal to this list</p> <pre><code>mylist = [func(i) for i in range(N)] </code></pre> <p>without resorting to advanced methods like creating a function...
7
2010-03-13T20:54:43Z
2,440,097
<p>Going to have to disagree with Javier here...</p> <p>With the following code:</p> <pre><code>print '%5s | %6s | %6s' % ('N', 'l.comp', 'manual') print '------+--------+-------' for N in 10, 100, 1000, 10000: num_iter = 1000000 / N # Time for list comprehension. t1 = timeit.timeit('[func(i) for i in ra...
1
2010-03-13T21:29:04Z
[ "python", "list-comprehension" ]
Python 3: Most efficient way to create a [func(i) for i in range(N)] list comprehension
2,439,986
<p>Say I have a function func(i) that creates an object for an integer i, and N is some nonnegative integer. Then what's the fastest way to create a list (not a range) equal to this list</p> <pre><code>mylist = [func(i) for i in range(N)] </code></pre> <p>without resorting to advanced methods like creating a function...
7
2010-03-13T20:54:43Z
2,440,098
<p>If you use the <code>timeit</code> module, you may come to the opposite conclusion: list comprehension is faster than preallocation:</p> <pre><code>f=lambda x: x+1 N=1000000 def lc(): return [f(i) for i in range(N)] def prealloc(): mylist = [None]*N for i in range(N): mylist[i]=f(i) return mylist de...
2
2010-03-13T21:29:21Z
[ "python", "list-comprehension" ]
Python 3: Most efficient way to create a [func(i) for i in range(N)] list comprehension
2,439,986
<p>Say I have a function func(i) that creates an object for an integer i, and N is some nonnegative integer. Then what's the fastest way to create a list (not a range) equal to this list</p> <pre><code>mylist = [func(i) for i in range(N)] </code></pre> <p>without resorting to advanced methods like creating a function...
7
2010-03-13T20:54:43Z
2,440,107
<p>Interesting question. As of the following test, it seems that preallocation does not improve performance in the current CPython implementation (Python 2 code but result ranking is the same, except that there's no xrange in Python 3):</p> <pre><code>N = 5000000 def func(x): return x**2 def timeit(fn): impo...
1
2010-03-13T21:32:22Z
[ "python", "list-comprehension" ]
Python 3: Most efficient way to create a [func(i) for i in range(N)] list comprehension
2,439,986
<p>Say I have a function func(i) that creates an object for an integer i, and N is some nonnegative integer. Then what's the fastest way to create a list (not a range) equal to this list</p> <pre><code>mylist = [func(i) for i in range(N)] </code></pre> <p>without resorting to advanced methods like creating a function...
7
2010-03-13T20:54:43Z
2,440,583
<p>Somebody wrote: """Python is smart enough. As long as the object you're iterating over has a <code>__len__</code> or <code>__length_hint__</code> method, Python will call it to determine the size and preallocate the array."""</p> <p>As far as I can tell, <strong>there is no preallocation in a list comprehension</st...
7
2010-03-13T23:54:40Z
[ "python", "list-comprehension" ]
Disable browser caching in pylons
2,439,987
<p>I'm have an action /json that returns json from the server.</p> <p>Unfortunately in IE, the browser likes to cache this json.</p> <p>How can I make it so that this action doesn't cache?</p>
2
2010-03-13T20:54:45Z
2,440,152
<p>This is a common problem -- IE caches all ajax/json requests on the client side. Other browsers do not.</p> <p>To work around it, generate a random number and append it to your request url as a variable. This fools IE into thinking it's a new request.</p> <p>Here's an example in javascript, you can do something si...
1
2010-03-13T21:44:56Z
[ "python", "internet-explorer", "caching", "pylons" ]
Disable browser caching in pylons
2,439,987
<p>I'm have an action /json that returns json from the server.</p> <p>Unfortunately in IE, the browser likes to cache this json.</p> <p>How can I make it so that this action doesn't cache?</p>
2
2010-03-13T20:54:45Z
2,440,183
<p>Make sure your responses are not telling the browser that the content expires in the future. There are two HTTP headers the control this.</p> <ol> <li>Expires</li> <li>Cache-Control - There are many possible values for this header, but the one that controls expiration is max-age=foo.</li> </ol> <p>In addition, IE ...
2
2010-03-13T21:53:29Z
[ "python", "internet-explorer", "caching", "pylons" ]
Disable browser caching in pylons
2,439,987
<p>I'm have an action /json that returns json from the server.</p> <p>Unfortunately in IE, the browser likes to cache this json.</p> <p>How can I make it so that this action doesn't cache?</p>
2
2010-03-13T20:54:45Z
2,440,305
<p>Make sure your response headers have:</p> <pre><code>Cache-Control: no-cache Pragma: no-cache Expires=-1 </code></pre>
3
2010-03-13T22:34:22Z
[ "python", "internet-explorer", "caching", "pylons" ]
Disable browser caching in pylons
2,439,987
<p>I'm have an action /json that returns json from the server.</p> <p>Unfortunately in IE, the browser likes to cache this json.</p> <p>How can I make it so that this action doesn't cache?</p>
2
2010-03-13T20:54:45Z
3,907,139
<p>The jQuery library has pretty nice ajax functions, and settings to control them. One of them is is called "cache" and it will automatically append a random number to the query that essentially forces the browser to not cache the page. This can be set along with the parameter "dataType", which can be set to "json" to...
1
2010-10-11T14:31:16Z
[ "python", "internet-explorer", "caching", "pylons" ]
How to check the existence of a row in SQLite with Python?
2,440,147
<p>I have the cursor with the query statement as follows:</p> <pre><code>cursor.execute("select rowid from components where name = ?", (name,)) </code></pre> <p>I want to check for the existence of the components: name and return to a python variable. How do I do that?</p>
24
2010-03-13T21:42:37Z
2,440,179
<p>Since the <code>name</code>s are unique, I really favor your (the OP's) method of using <code>fetchone</code> or Alex Martelli's method of using <code>SELECT count(*)</code> over my initial suggestion of using <code>fetchall</code>.</p> <p><code>fetchall</code> wraps the results (typically multiple rows of data) in...
51
2010-03-13T21:51:46Z
[ "python", "sql", "sqlite", "sqlite3" ]
How to check the existence of a row in SQLite with Python?
2,440,147
<p>I have the cursor with the query statement as follows:</p> <pre><code>cursor.execute("select rowid from components where name = ?", (name,)) </code></pre> <p>I want to check for the existence of the components: name and return to a python variable. How do I do that?</p>
24
2010-03-13T21:42:37Z
2,440,185
<p>Ooops, I have found the answer</p> <pre><code>exist = cursor.fetchone() if exist is None: #not exists else: #exists </code></pre>
10
2010-03-13T21:54:26Z
[ "python", "sql", "sqlite", "sqlite3" ]
How to check the existence of a row in SQLite with Python?
2,440,147
<p>I have the cursor with the query statement as follows:</p> <pre><code>cursor.execute("select rowid from components where name = ?", (name,)) </code></pre> <p>I want to check for the existence of the components: name and return to a python variable. How do I do that?</p>
24
2010-03-13T21:42:37Z
2,440,571
<p>As both existing answers (your own and @unutbu's) point out, the trick is that you do need to do <em>some</em> sort of fetching, after executing the <code>SELECT</code>, to check whether there have been any results to the select or not (whether you do it with a single fetch and check for none, or a fetch-all and che...
5
2010-03-13T23:49:15Z
[ "python", "sql", "sqlite", "sqlite3" ]
How to check the existence of a row in SQLite with Python?
2,440,147
<p>I have the cursor with the query statement as follows:</p> <pre><code>cursor.execute("select rowid from components where name = ?", (name,)) </code></pre> <p>I want to check for the existence of the components: name and return to a python variable. How do I do that?</p>
24
2010-03-13T21:42:37Z
39,767,236
<p>To make it even shorter...:</p> <pre><code>row = cursor.fetchone() if row: print "row is present" else: print "row is not present" </code></pre>
0
2016-09-29T10:01:52Z
[ "python", "sql", "sqlite", "sqlite3" ]
Printing Stdout In Command Line App Without Overwriting Pending User Input
2,440,387
<p>In a basic Unix-shell app, how would you print to stdout without disturbing any pending user input.</p> <p>e.g. Below is a simple Python app that echos user input. A thread running in the background prints a counter every 1 second.</p> <pre><code>import threading, time class MyThread( threading.Thread ): runn...
1
2010-03-13T23:01:46Z
2,440,553
<p>You have to port your code to <em>some</em> way of controlling the terminal as slightly better than a teletype -- e.g. with the <a href="http://docs.python.org/library/curses.html?highlight=curses#module-curses" rel="nofollow">curses</a> module in Python's standard library, or other ways to move the cursor away befo...
2
2010-03-13T23:44:17Z
[ "python", "bash", "shell", "scripting", "stdout" ]
Printing Stdout In Command Line App Without Overwriting Pending User Input
2,440,387
<p>In a basic Unix-shell app, how would you print to stdout without disturbing any pending user input.</p> <p>e.g. Below is a simple Python app that echos user input. A thread running in the background prints a counter every 1 second.</p> <pre><code>import threading, time class MyThread( threading.Thread ): runn...
1
2010-03-13T23:01:46Z
2,441,515
<p>You could defer writing output until just after you receive some input. For anything more advanced you'll have to use Alex's answer</p> <pre><code>import threading, time output=[] class MyThread( threading.Thread ): running = False def run(self): self.running = True i = 0 while self....
0
2010-03-14T07:38:21Z
[ "python", "bash", "shell", "scripting", "stdout" ]
Save memory in Python. How to iterate over the lines and save them efficiently with a 2million line file?
2,440,495
<p>I have a tab-separated data file with a little over 2 million lines and 19 columns. You can find it, in US.zip: <a href="http://download.geonames.org/export/dump/" rel="nofollow">http://download.geonames.org/export/dump/</a>.</p> <p>I started to run the following but with <code>for l in f.readlines()</code>. I und...
3
2010-03-13T23:30:18Z
2,440,527
<p>This looks perfectly fine to me. Iterating over the file like that or using <code>xreadlines()</code> will read each line as needed (with sane buffering behind the scenes). Memory usage should not grow as you read in more and more data.</p> <p>As for performance, you should profile your app. Most likely the bottlen...
2
2010-03-13T23:37:58Z
[ "python", "django", "file", "memory-management" ]
Save memory in Python. How to iterate over the lines and save them efficiently with a 2million line file?
2,440,495
<p>I have a tab-separated data file with a little over 2 million lines and 19 columns. You can find it, in US.zip: <a href="http://download.geonames.org/export/dump/" rel="nofollow">http://download.geonames.org/export/dump/</a>.</p> <p>I started to run the following but with <code>for l in f.readlines()</code>. I und...
3
2010-03-13T23:30:18Z
2,440,530
<p>There's no reason to worry in the data you've given us: is memory consumption going UP as you read more and more lines? Now <em>that</em> would be cause for worry -- but there's no indication that this would happen in the code you've shown, assuming that <code>p.save()</code> saves the object to some database or fi...
2
2010-03-13T23:38:56Z
[ "python", "django", "file", "memory-management" ]
Save memory in Python. How to iterate over the lines and save them efficiently with a 2million line file?
2,440,495
<p>I have a tab-separated data file with a little over 2 million lines and 19 columns. You can find it, in US.zip: <a href="http://download.geonames.org/export/dump/" rel="nofollow">http://download.geonames.org/export/dump/</a>.</p> <p>I started to run the following but with <code>for l in f.readlines()</code>. I und...
3
2010-03-13T23:30:18Z
2,440,580
<p>Make sure that Django's DEBUG setting is set to False</p>
5
2010-03-13T23:53:31Z
[ "python", "django", "file", "memory-management" ]
Getting CPU temperature using Python?
2,440,511
<p>How do I retrieve the temperature of my CPU using Python? (Assuming I'm on Linux)</p>
20
2010-03-13T23:35:19Z
2,440,539
<p><a href="http://sourceforge.net/projects/py-cputemp/" rel="nofollow">Py-cputemp</a> seems to do the job.</p>
5
2010-03-13T23:40:49Z
[ "python", "cpu", "temperature" ]
Getting CPU temperature using Python?
2,440,511
<p>How do I retrieve the temperature of my CPU using Python? (Assuming I'm on Linux)</p>
20
2010-03-13T23:35:19Z
2,440,540
<p>Depending on your Linux distro, you may find a file under <code>/proc</code> that contains this information. For example, <a href="http://www.togaware.com/linux/survivor/CPU_Temperature.html" rel="nofollow">this page</a> suggests <code>/proc/acpi/thermal_zone/THM/temperature</code>.</p>
2
2010-03-13T23:41:20Z
[ "python", "cpu", "temperature" ]
Getting CPU temperature using Python?
2,440,511
<p>How do I retrieve the temperature of my CPU using Python? (Assuming I'm on Linux)</p>
20
2010-03-13T23:35:19Z
2,440,543
<p>You could try the <a href="http://pyi2c.sourceforge.net/" rel="nofollow">PyI2C</a> module, it can read directly from the kernel.</p>
1
2010-03-13T23:41:52Z
[ "python", "cpu", "temperature" ]
Getting CPU temperature using Python?
2,440,511
<p>How do I retrieve the temperature of my CPU using Python? (Assuming I'm on Linux)</p>
20
2010-03-13T23:35:19Z
2,440,544
<p>If your Linux supports ACPI, reading pseudo-file <code>/proc/acpi/thermal_zone/THM0/temperature</code> (the path may differ, I know it's <code>/proc/acpi/thermal_zone/THRM/temperature</code> in some systems) should do it. But I don't think there's a way that works in <em>every</em> Linux system in the world, so you...
8
2010-03-13T23:42:04Z
[ "python", "cpu", "temperature" ]
Getting CPU temperature using Python?
2,440,511
<p>How do I retrieve the temperature of my CPU using Python? (Assuming I'm on Linux)</p>
20
2010-03-13T23:35:19Z
12,332,754
<p>As an alternative you can install the lm-sensors package, then install <a href="https://bitbucket.org/blackjack/pysensors/overview" rel="nofollow">PySensors</a> (a python binding for libsensors).</p>
1
2012-09-08T17:04:42Z
[ "python", "cpu", "temperature" ]
Getting CPU temperature using Python?
2,440,511
<p>How do I retrieve the temperature of my CPU using Python? (Assuming I'm on Linux)</p>
20
2010-03-13T23:35:19Z
15,213,255
<p>There is a <a href="http://shallowsky.com/blog/linux/kernel/sysfs-thermal-zone.html">newer API</a> (see also <a href="http://lwn.net/Articles/268958/">LWN article</a> and <a href="http://www.mjmwired.net/kernel/Documentation/thermal/sysfs-api.txt">Linux kernel doc</a>) showing temperatures under e.g.</p> <pre><code...
9
2013-03-04T23:40:35Z
[ "python", "cpu", "temperature" ]
Getting CPU temperature using Python?
2,440,511
<p>How do I retrieve the temperature of my CPU using Python? (Assuming I'm on Linux)</p>
20
2010-03-13T23:35:19Z
16,256,136
<p>Reading files in <strong>/sys/class/hwmon/hwmon*/temp1_*</strong> worked for me but AFAIK there are no standards for doing this cleanly. Anyway, you can try this and make sure it provides the same number of CPUs shown by "<strong>sensors</strong>" cmdline utility, in which case you can assume it's reliable.</p> <pr...
3
2013-04-27T19:38:31Z
[ "python", "cpu", "temperature" ]
Getting CPU temperature using Python?
2,440,511
<p>How do I retrieve the temperature of my CPU using Python? (Assuming I'm on Linux)</p>
20
2010-03-13T23:35:19Z
30,465,124
<p>Sysmon works nice. Nicely made, it does much more than measure CPU temperature. It is a command line program, and logs all the data it measured to a file. Also, it is open-source and written in python 2.7. </p> <p>Sysmon: <a href="https://github.com/calthecoder/sysmon-1.0.1" rel="nofollow">https://github.com/calthe...
0
2015-05-26T17:28:44Z
[ "python", "cpu", "temperature" ]
How do I find the length of media with gstreamer?
2,440,554
<p>How do I find the playback time of media with gstreamer?</p>
12
2010-03-13T23:44:29Z
2,441,373
<p>Here's a simple Python script to get the duration of anything gstreamer can decode. Note that all times in gstreamer are in nanoseconds.</p> <h1>duration.py:</h1> <pre><code>#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import division import sys import gobject gobject.threads_init() import pygst ...
17
2010-03-14T06:16:05Z
[ "python", "video", "media", "gstreamer" ]
How do I find the length of media with gstreamer?
2,440,554
<p>How do I find the playback time of media with gstreamer?</p>
12
2010-03-13T23:44:29Z
2,562,747
<p>See Section 6.5. </p> <p><a href="http://majorsilence.com/pygtk_audio_and_video_playback_gstreamer" rel="nofollow">http://majorsilence.com/pygtk_audio_and_video_playback_gstreamer</a></p> <p>It avoids need to create a pipeline and run it manually.</p>
3
2010-04-01T18:38:38Z
[ "python", "video", "media", "gstreamer" ]
How do I find the length of media with gstreamer?
2,440,554
<p>How do I find the playback time of media with gstreamer?</p>
12
2010-03-13T23:44:29Z
31,616,242
<p>Why re-invent the wheel?<br> Use:<br> gst-discoverer-1.0 filename<br> or<br> gst-discoverer-0.10 filename </p> <p>Depending on the file type you may want to add " | grep Duration" to avoid the tags which can be lengthy.<br> For the ridding of extraneous tags for video,flac and mp3 files this should do th...
0
2015-07-24T17:15:50Z
[ "python", "video", "media", "gstreamer" ]
Compiler options wrong with python setup.py
2,440,579
<p>I'm trying to install matplotlib on my mac setup. I find that setup.py has inaccurate flags, in particular the isysroot points to an earlier SDK.</p> <p>Where does setup.py get its info and how can i fix it?</p> <p>I'm on MacOS 10.5.8, XCode 3.1.2 and Python 2.6 (default config was 2.5)</p>
2
2010-03-13T23:53:07Z
2,440,611
<p><code>setup.py</code> gets its info from your installation of Python, specifically the <a href="http://docs.python.org/library/distutils.html?highlight=distutils#module-distutils" rel="nofollow">distutils</a> package of the standard library, from which it imports at least some functionality.</p> <p><a href="http://...
1
2010-03-14T00:02:10Z
[ "python", "osx", "gcc", "distutils", "setup.py" ]
Compiler options wrong with python setup.py
2,440,579
<p>I'm trying to install matplotlib on my mac setup. I find that setup.py has inaccurate flags, in particular the isysroot points to an earlier SDK.</p> <p>Where does setup.py get its info and how can i fix it?</p> <p>I'm on MacOS 10.5.8, XCode 3.1.2 and Python 2.6 (default config was 2.5)</p>
2
2010-03-13T23:53:07Z
2,440,981
<p>I'm guessing you've installed 2.6 on 10.5 using the python.org OS X installer. In that case, the flags are accurate and you should not try to change them. The python.org installers are built using the so-called 10.4u SDK and with a deployment target of 10.3, allowing one installer image to work on Mac OS X systems...
3
2010-03-14T03:32:13Z
[ "python", "osx", "gcc", "distutils", "setup.py" ]
Formatting floats in Python without superfluous zeros
2,440,692
<p>How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?</p> <p>Like:</p> <pre><code>3 -&gt; "3" 3. -&gt; "3" 3.0 -&gt; "3" 3.1 -&gt; "3.1" 3.14 -&gt; "3.14" 3.140 -&gt; "3.14" </code></pre>
57
2010-03-14T00:27:39Z
2,440,708
<p>You could use <code>%g</code> to achieve this:</p> <pre><code>'%g'%(3.140) </code></pre> <p>or, for Python 2.6 or better:</p> <pre><code>'{0:g}'.format(3.140) </code></pre> <p>From the <a href="http://docs.python.org/library/string.html#format-specification-mini-language">docs for <code>format</code></a>: <code>...
67
2010-03-14T00:34:31Z
[ "python", "formatting", "floating-point", "pretty-print" ]
Formatting floats in Python without superfluous zeros
2,440,692
<p>How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?</p> <p>Like:</p> <pre><code>3 -&gt; "3" 3. -&gt; "3" 3.0 -&gt; "3" 3.1 -&gt; "3.1" 3.14 -&gt; "3.14" 3.140 -&gt; "3.14" </code></pre>
57
2010-03-14T00:27:39Z
2,440,786
<p>Me, I'd do <code>('%f' % x).rstrip('0').rstrip('.')</code> -- guarantees fixed-point formatting rather than scientific notation, etc etc. Yeah, not as slick and elegant as <code>%g</code>, but, it works (and I don't know how to force <code>%g</code> to never use scientific notation;-).</p>
72
2010-03-14T01:11:24Z
[ "python", "formatting", "floating-point", "pretty-print" ]
Formatting floats in Python without superfluous zeros
2,440,692
<p>How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?</p> <p>Like:</p> <pre><code>3 -&gt; "3" 3. -&gt; "3" 3.0 -&gt; "3" 3.1 -&gt; "3.1" 3.14 -&gt; "3.14" 3.140 -&gt; "3.14" </code></pre>
57
2010-03-14T00:27:39Z
7,172,838
<p>Use %g with big enough width, for example '%.99g'. It will print in fixed-point notation for any reasonably big number.</p> <p><strong>EDIT: it doesn't work</strong></p> <pre><code>&gt;&gt;&gt; '%.99g' % 0.0000001 '9.99999999999999954748111825886258685613938723690807819366455078125e-08' </code></pre>
0
2011-08-24T08:55:44Z
[ "python", "formatting", "floating-point", "pretty-print" ]
Formatting floats in Python without superfluous zeros
2,440,692
<p>How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?</p> <p>Like:</p> <pre><code>3 -&gt; "3" 3. -&gt; "3" 3.0 -&gt; "3" 3.1 -&gt; "3.1" 3.14 -&gt; "3.14" 3.140 -&gt; "3.14" </code></pre>
57
2010-03-14T00:27:39Z
20,933,939
<p>Formatting <code>"%.f"%num</code> will be create string without zeros</p>
-1
2014-01-05T13:11:40Z
[ "python", "formatting", "floating-point", "pretty-print" ]
Formatting floats in Python without superfluous zeros
2,440,692
<p>How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?</p> <p>Like:</p> <pre><code>3 -&gt; "3" 3. -&gt; "3" 3.0 -&gt; "3" 3.1 -&gt; "3.1" 3.14 -&gt; "3.14" 3.140 -&gt; "3.14" </code></pre>
57
2010-03-14T00:27:39Z
34,332,708
<p>It is possible using <code>format</code> function: </p> <pre><code>{}.format(float) </code></pre>
0
2015-12-17T10:50:09Z
[ "python", "formatting", "floating-point", "pretty-print" ]
Formatting floats in Python without superfluous zeros
2,440,692
<p>How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?</p> <p>Like:</p> <pre><code>3 -&gt; "3" 3. -&gt; "3" 3.0 -&gt; "3" 3.1 -&gt; "3.1" 3.14 -&gt; "3.14" 3.140 -&gt; "3.14" </code></pre>
57
2010-03-14T00:27:39Z
36,981,428
<p>What about trying the easiest and probably most effective approach? The method <strong>normalize()</strong> removes all the rightmost trailing zeros.</p> <pre><code>from decimal import Decimal print (Decimal('0.001000').normalize()) # Result: 0.001 </code></pre> <p>Works in <strong>Python 2</strong> and <strong>P...
3
2016-05-02T11:32:39Z
[ "python", "formatting", "floating-point", "pretty-print" ]
Formatting floats in Python without superfluous zeros
2,440,692
<p>How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?</p> <p>Like:</p> <pre><code>3 -&gt; "3" 3. -&gt; "3" 3.0 -&gt; "3" 3.1 -&gt; "3.1" 3.14 -&gt; "3.14" 3.140 -&gt; "3.14" </code></pre>
57
2010-03-14T00:27:39Z
37,736,333
<p>After looking over answers to several similar questions, this seems to be the best solution for me:</p> <pre><code>def floatToString(inputValue): return ('%.15f' % inputValue).rstrip('0').rstrip('.') </code></pre> <p>My reasoning:</p> <p><code>%g</code> doesn't get rid of scientific notation.</p> <pre><code>...
2
2016-06-09T21:17:09Z
[ "python", "formatting", "floating-point", "pretty-print" ]
Formatting floats in Python without superfluous zeros
2,440,692
<p>How to format a float so it does not containt the remaing zeros? In other words, I want the resulting string to be as short as possible..?</p> <p>Like:</p> <pre><code>3 -&gt; "3" 3. -&gt; "3" 3.0 -&gt; "3" 3.1 -&gt; "3.1" 3.14 -&gt; "3.14" 3.140 -&gt; "3.14" </code></pre>
57
2010-03-14T00:27:39Z
37,754,880
<p>I think there's an easier way to do this with the new <code>str.format</code> functionality. If you just convert it with str (e.g. <code>str(.0300)</code> -> <code>0.3</code>) it does what is desired, mostly. If the number is small or large enough, it will resort to exponential notation, but mostly it will just pr...
0
2016-06-10T18:21:20Z
[ "python", "formatting", "floating-point", "pretty-print" ]
How to bind an ip address to telnetlib in Python
2,440,781
<p>The code below binds an ip address to urllib, urllib2, etc.</p> <pre><code>import socket true_socket = socket.socket def bound_socket(*a, **k): sock = true_socket(*a, **k) sock.bind((sourceIP, 0)) return sock socket.socket = bound_socket </code></pre> <p>Is it also able to bind an ip address to telnetl...
1
2010-03-14T01:10:35Z
2,440,802
<p><code>telnetlib</code> at least in recent Python releases uses <code>socket.create_connection</code> (see telnetlib's sources <a href="http://svn.python.org/view/python/trunk/Lib/telnetlib.py?revision=63788&amp;view=markup" rel="nofollow">here</a>) but that should also be caught by your monkeypatch (sources <a href=...
2
2010-03-14T01:17:34Z
[ "python", "ip-address", "telnetlib" ]
warning in python with MySQLdb
2,440,799
<p>when I use MySQLdb get this message: </p> <pre><code>/var/lib/python-support/python2.6/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import ImmutableSet </code></pre> <p>I try filter the warning with </p> <pre><code>import warnings warnings.filterwarnings("ignore", messa...
1
2010-03-14T01:16:31Z
2,440,814
<p>What release of MySQLdb are you using? I think the current one (1.2.3c1) should have it fixed see <a href="http://sourceforge.net/tracker/index.php?func=detail&amp;aid=2156977&amp;group_id=22307&amp;atid=374932" rel="nofollow">this bug</a> (marked as fixed as of Oct 2008, 1.2 branch).</p>
1
2010-03-14T01:21:50Z
[ "python", "warnings", "mysql" ]
warning in python with MySQLdb
2,440,799
<p>when I use MySQLdb get this message: </p> <pre><code>/var/lib/python-support/python2.6/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import ImmutableSet </code></pre> <p>I try filter the warning with </p> <pre><code>import warnings warnings.filterwarnings("ignore", messa...
1
2010-03-14T01:16:31Z
2,441,030
<p>From <a href="http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings" rel="nofollow">python documentation</a>: you could filter your warning this way, so that if other warnings are caused by an other part of your code, there would still be displayed:</p> <pre><code>import warnings with warnin...
4
2010-03-14T03:56:24Z
[ "python", "warnings", "mysql" ]
Python Least-Squares Natural Splines
2,441,058
<p>I am trying to find a numerical package which will fit a natural spline which minimizes weighted least squares.</p> <p>There is a package in scipy which does what I want for unnatural splines. </p> <pre><code>import numpy as np import matplotlib.pyplot as plt from scipy import interpolate, randn x = np.arange(0,5...
7
2010-03-14T03:09:58Z
2,443,386
<p>The spline.py file inside of this <a href="http://cars.uchicago.edu/software/pub/python_epics.tar">tar file</a> from <a href="http://cars9.uchicago.edu/software/python/">this page</a> does a natural spline fit by default. There is also some code on <a href="http://python.pastebin.com/phbBLJ4K">this page</a> that cla...
5
2010-03-14T19:14:35Z
[ "python", "scipy", "spline" ]
What is the relationship between docutils and Sphinx?
2,441,078
<p>There seems to be a plethora of documentation tools for Python. Another one that I've run across is epydoc. It seems like Sphinx is the de facto standard, because it's used to generate the official Python docs. Can someone please sort out the current state of Python's documentation tools for me?</p>
16
2010-03-14T03:24:05Z
2,441,159
<p><a href="http://epydoc.sourceforge.net/">epydoc</a> and <a href="http://sphinx.pocoo.org/">Sphinx</a> are different types of tools. </p> <p>They are the same in that they:</p> <ul> <li>Both use <a href="http://en.wikipedia.org/wiki/ReStructuredText">ReST</a> via <a href="http://docutils.sourceforge.net/">docutils<...
20
2010-03-14T04:15:48Z
[ "python", "documentation-generation", "python-sphinx", "epydoc" ]
What is the relationship between docutils and Sphinx?
2,441,078
<p>There seems to be a plethora of documentation tools for Python. Another one that I've run across is epydoc. It seems like Sphinx is the de facto standard, because it's used to generate the official Python docs. Can someone please sort out the current state of Python's documentation tools for me?</p>
16
2010-03-14T03:24:05Z
16,447,694
<p>It seems like <a href="http://epydoc.sourceforge.net/" rel="nofollow">Epydoc</a> is, if not dead, at least pining for the fjords.</p> <p>Sphynx with the <a href="http://sphinx-doc.org/man/sphinx-apidoc.html" rel="nofollow">sphinx-apidoc</a> command and the <a href="http://sphinx-doc.org/ext/autodoc.html" rel="nofol...
2
2013-05-08T18:28:52Z
[ "python", "documentation-generation", "python-sphinx", "epydoc" ]
What is the relationship between docutils and Sphinx?
2,441,078
<p>There seems to be a plethora of documentation tools for Python. Another one that I've run across is epydoc. It seems like Sphinx is the de facto standard, because it's used to generate the official Python docs. Can someone please sort out the current state of Python's documentation tools for me?</p>
16
2010-03-14T03:24:05Z
21,603,879
<p>I recently changed from Python2 to Python3 and found that there was no Epydoc package for Python3. So it seems with Python3 there is a clear focus on using Sphinx as API documentation.</p>
1
2014-02-06T13:06:49Z
[ "python", "documentation-generation", "python-sphinx", "epydoc" ]
Embed python interpreter in a python application
2,441,172
<p>i'm looking for a way to ship the python interpreter with my application (also written in python), so that it doesn't need to have python installed on the machine.</p> <p>I searched google and found a bunch of results about how to embed the python interpreter in applications written in various languages, but nothin...
7
2010-03-14T04:24:12Z
2,441,182
<p>You need <em>some</em> sort of executable in order to start Python. May as well be the one your app has been frozen into.</p> <p>The alternative is to copy the executable, library, and pieces of the stdlib that you need into a private directory and invoke that against your app.</p>
2
2010-03-14T04:28:05Z
[ "python", "interpreter", "embedding" ]
Embed python interpreter in a python application
2,441,172
<p>i'm looking for a way to ship the python interpreter with my application (also written in python), so that it doesn't need to have python installed on the machine.</p> <p>I searched google and found a bunch of results about how to embed the python interpreter in applications written in various languages, but nothin...
7
2010-03-14T04:24:12Z
2,441,184
<p>For distribution on Windows machines, look into <a href="http://www.py2exe.org/"><strong>py2exe</strong></a><br></p> <pre><code>py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation </code></pre> <p>For the MacIn...
9
2010-03-14T04:28:23Z
[ "python", "interpreter", "embedding" ]
Embed python interpreter in a python application
2,441,172
<p>i'm looking for a way to ship the python interpreter with my application (also written in python), so that it doesn't need to have python installed on the machine.</p> <p>I searched google and found a bunch of results about how to embed the python interpreter in applications written in various languages, but nothin...
7
2010-03-14T04:24:12Z
2,441,185
<p>Making a frozen binary using a utility like cx_freeze or py2exe is probably the easiest way to do this. That way you only need to distribute the executable. I know that you might prefer not to distribute a binary, but if that is a concern you could always give users the option to download the source and run from an ...
2
2010-03-14T04:29:48Z
[ "python", "interpreter", "embedding" ]
Embed python interpreter in a python application
2,441,172
<p>i'm looking for a way to ship the python interpreter with my application (also written in python), so that it doesn't need to have python installed on the machine.</p> <p>I searched google and found a bunch of results about how to embed the python interpreter in applications written in various languages, but nothin...
7
2010-03-14T04:24:12Z
2,441,206
<p>For Windows: <a href="http://www.py2exe.org/index.cgi/FrontPage" rel="nofollow">py2exe</a></p> <p>For Linux: <a href="http://wiki.python.org/moin/Freeze" rel="nofollow">Freeze</a></p> <p>Full disclosure: I've only read about these, never used them. Perhaps some who has can comment?</p>
0
2010-03-14T04:36:51Z
[ "python", "interpreter", "embedding" ]
Embed python interpreter in a python application
2,441,172
<p>i'm looking for a way to ship the python interpreter with my application (also written in python), so that it doesn't need to have python installed on the machine.</p> <p>I searched google and found a bunch of results about how to embed the python interpreter in applications written in various languages, but nothin...
7
2010-03-14T04:24:12Z
2,441,499
<p>Have a look at <a href="http://www.python-packager.com" rel="nofollow">http://www.python-packager.com</a>, it is a free webservice for building redistrutable python binaries based on pyinstaller. I've used it to build apps for Windows and it is very easy to use and also works with GUI apps.</p>
0
2010-03-14T07:32:02Z
[ "python", "interpreter", "embedding" ]
How to implement Comet server side with Python?
2,441,533
<p>I once tried to implement Comet in PHP. Soon, I found that PHP is not suitable for Comet, since each HTTP request will occupy one process/thread. As a result, it doesn't scale well. </p> <p>I just installed mod_python in my XAMPP. I thought it would be easy to implement Comet with Python asynchronous programming. B...
1
2010-03-14T07:48:50Z
2,441,559
<p>First of all, I'm not async expert at all, I just investigated the topic once. IMHO if you're using XAMPP then you're loosing the posibility of doing long polling because Apache uses thread/processes (depending on configuration) for each request.</p> <p>What you need, is non-blocking web server, like <a href="http...
7
2010-03-14T08:05:36Z
[ "python", "comet" ]
How to implement Comet server side with Python?
2,441,533
<p>I once tried to implement Comet in PHP. Soon, I found that PHP is not suitable for Comet, since each HTTP request will occupy one process/thread. As a result, it doesn't scale well. </p> <p>I just installed mod_python in my XAMPP. I thought it would be easy to implement Comet with Python asynchronous programming. B...
1
2010-03-14T07:48:50Z
2,441,606
<p>I'm not sure if you came across <a href="http://stackoverflow.com/questions/960969/python-comet-server">this question</a>, but the question asked is pretty similar and there seem to be some good answers there. HTH.</p>
0
2010-03-14T08:28:49Z
[ "python", "comet" ]
What are good Python and/or Django deployment solutions?
2,441,661
<p>For now I use some mix between <a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">virtual_env</a>, <a href="http://pypi.python.org/pypi/pip" rel="nofollow">pip</a> and <a href="http://docs.fabfile.org/0.9.0/" rel="nofollow">Fabric</a>.</p> <p>This allows to:</p> <ul> <li>install required libs;</li> <l...
0
2010-03-14T08:59:16Z
2,469,365
<p>Fabric is the best solution for you. You can do everything you need using Fabric.</p>
1
2010-03-18T11:23:03Z
[ "python", "deployment" ]
Weird characters in exported csv files when converting
2,441,682
<p>Hey guys, I came across a problem I cannot solve on my own concerning the downloadable csv formatted trends data files from Google Insights for Search. </p> <p>I'm to lazy to reformat the files I4S gives me manually what means: Extracting the section with the actual trends data and reformatting the columns so that...
1
2010-03-14T09:11:49Z
2,442,037
<p><code>repr()</code> is your friend (except on Python 3.X; use <code>ascii()</code> instead).</p> <pre><code>prompt&gt;\python26\python -c "print repr(open('report.csv','rb').read()[:300])" '\xff\xfeW\x00e\x00b\x00 \x00S\x00e\x00a\x00r\x00c\x00h\x00 \x00I\x00n\x00t\x00e \x00r\x00e\x00s\x00t\x00:\x00 \x00f\x00o\x00o\...
3
2010-03-14T11:45:34Z
[ "python", "csv" ]
Weird characters in exported csv files when converting
2,441,682
<p>Hey guys, I came across a problem I cannot solve on my own concerning the downloadable csv formatted trends data files from Google Insights for Search. </p> <p>I'm to lazy to reformat the files I4S gives me manually what means: Extracting the section with the actual trends data and reformatting the columns so that...
1
2010-03-14T09:11:49Z
2,442,043
<p>Found the solution:</p> <p>It was a character encoding problem. Depending on the editor you use other character set encodings are shown: </p> <p>Notepad++: ucs-2 little endian PSPad: utf-16le</p> <p>Decoding the contents with ucs-2 didn't work so I tried utf-16le and it went well. extraneons answer was wrong, but...
2
2010-03-14T11:46:47Z
[ "python", "csv" ]
Weird characters in exported csv files when converting
2,441,682
<p>Hey guys, I came across a problem I cannot solve on my own concerning the downloadable csv formatted trends data files from Google Insights for Search. </p> <p>I'm to lazy to reformat the files I4S gives me manually what means: Extracting the section with the actual trends data and reformatting the columns so that...
1
2010-03-14T09:11:49Z
2,442,087
<p>The problem is character encoding, possibly in combination with universal line ending support of Python. As you mentioned, the source file is in UCS-2 LE, with a Byte Order Mark (BOM). You need to do something like:</p> <pre><code>import codecs input_file = codecs.open("Downloads/report.csv", "r", encoding="utf_16...
2
2010-03-14T12:11:24Z
[ "python", "csv" ]
How do you use pip, virtualenv and Fabric to handle deployment?
2,441,704
<p>What are your settings, your tricks, and above all, your workflow?</p> <p>These tools are great but there are still no best practices attached to their usage, so I don't know what is the most efficient way to use them.</p> <ul> <li>Do you use <a href="http://pypi.python.org/pypi/pip">pip</a> bundles or always down...
66
2010-03-14T09:23:20Z
2,448,744
<p>"Best practices" are very context-dependent, so I won't claim my practices are best, just that they work for me. I work on mostly small sites, so no multiple-server deployments, CDNs etc. I do need to support Webfaction shared hosting deployment, as some clients need the cheapest hosting they can find. I do often ha...
78
2010-03-15T16:31:48Z
[ "python", "deployment", "virtualenv", "fabric", "pip" ]
How do you use pip, virtualenv and Fabric to handle deployment?
2,441,704
<p>What are your settings, your tricks, and above all, your workflow?</p> <p>These tools are great but there are still no best practices attached to their usage, so I don't know what is the most efficient way to use them.</p> <ul> <li>Do you use <a href="http://pypi.python.org/pypi/pip">pip</a> bundles or always down...
66
2010-03-14T09:23:20Z
3,310,647
<p>I use fabric to build and deploy my code and assume a system already set up for that. I think that a tool like <a href="http://www.puppetlabs.com/" rel="nofollow">puppet</a> is more appropriate to automate the installation of things like apache and mysql, though I have yet to really include it in my workflow.</p> <...
9
2010-07-22T15:38:30Z
[ "python", "deployment", "virtualenv", "fabric", "pip" ]
Django database - how to add this column in raw SQL
2,441,771
<p>Suppose I have my models set up already.</p> <pre><code>class books(models.Model): title = models.CharField... ISBN = models.Integer... </code></pre> <p>What if I want to add this column to my table?</p> <pre><code>user = models.ForeignKey(User, unique=True) </code></pre> <p>How would I write the raw SQL...
2
2010-03-14T09:57:58Z
2,441,783
<p>You should investigate a tool like <a href="http://south.aeracode.org">South</a>, which does all this for you.</p> <p>However the SQL would be something like (assuming you're using MySQL):</p> <pre><code>ALTER TABLE `appname_books` ADD COLUMN `user_id` INTEGER NOT NULL UNIQUE; ALTER TABLE `appname_books` ADD CONST...
5
2010-03-14T10:04:44Z
[ "python", "mysql", "database", "django" ]
How to discover table properties from SQLAlchemy mapped object
2,441,796
<p>I have a class mapped with a table, in my case in a declarative way, and I want to "discover" table properties, columns, names, relations, from this class:</p> <pre><code>engine = create_engine('sqlite:///' + databasePath, echo=True) # setting up root class for declarative declaration Base = declarative_base(bind=...
14
2010-03-14T10:11:10Z
2,448,930
<p>Information you need you can get from <a href="http://www.sqlalchemy.org/docs/reference/sqlalchemy/schema.html?highlight=table#sqlalchemy.schema.Table">Table</a> object:</p> <ul> <li><code>Ship.__table__.columns</code> will provide you with columns information</li> <li><code>Ship.__table__.foreign_keys</code> will ...
34
2010-03-15T16:58:44Z
[ "python", "sqlite", "sqlalchemy", "instrumentation" ]
Python: puzzling behaviour inside httplib
2,441,798
<p>I have added one line ( <code>import pdb; pdb.set_trace()</code> ) to httplib's HTTPConnection.putheader, so I can see what's going on inside. </p> <p><code>Python26\Lib\httplib.py</code>, line 489:</p> <pre><code>def putheader(self, header, value): """Send a request header line to the server. For example...
0
2010-03-14T10:12:55Z
2,441,892
<blockquote> <p>If the self.__state doesn't exist python would have to raise an exception as it did when I entered the expression.</p> </blockquote> <p>In Python, you don't have to declare variables explicitly. They are "born" when you assign to them.</p> <p>Some code validators like pylint warn about these situati...
0
2010-03-14T10:54:58Z
[ "python", "debugging", "httplib" ]
Python: puzzling behaviour inside httplib
2,441,798
<p>I have added one line ( <code>import pdb; pdb.set_trace()</code> ) to httplib's HTTPConnection.putheader, so I can see what's going on inside. </p> <p><code>Python26\Lib\httplib.py</code>, line 489:</p> <pre><code>def putheader(self, header, value): """Send a request header line to the server. For example...
0
2010-03-14T10:12:55Z
2,442,095
<p>Answering my own question:</p> <p><a href="http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python" rel="nofollow">http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Python</a></p> <p><code>__state</code> is a private name inside the object, it gets mangled as <code>_HTTPConnection__state</code...
1
2010-03-14T12:14:03Z
[ "python", "debugging", "httplib" ]