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
How to convert rational and decimal number strings to floats in python?
575,925
<p>How can I convert strings which can denote decimal or rational numbers to floats</p> <pre><code>&gt;&gt;&gt; ["0.1234", "1/2"] ['0.1234', '1/2'] </code></pre> <p>I'd want [0.1234, 0.5].</p> <p>eval is what I was thinking but no luck:</p> <pre><code>&gt;&gt;&gt; eval("1/2") 0 </code></pre>
9
2009-02-22T22:08:47Z
575,937
<p>Use <code>from __future__ import division</code> to get the behavior you want. Then, in a pinch, you can do something like </p> <pre><code>from __future__ import division strings = ["0.1234", "1/2", "2/3"] numbers = map(eval, strings) </code></pre> <p>to get a list of floats out of your strings. If you want to do ...
4
2009-02-22T22:16:05Z
[ "python", "rational-numbers" ]
How to convert rational and decimal number strings to floats in python?
575,925
<p>How can I convert strings which can denote decimal or rational numbers to floats</p> <pre><code>&gt;&gt;&gt; ["0.1234", "1/2"] ['0.1234', '1/2'] </code></pre> <p>I'd want [0.1234, 0.5].</p> <p>eval is what I was thinking but no luck:</p> <pre><code>&gt;&gt;&gt; eval("1/2") 0 </code></pre>
9
2009-02-22T22:08:47Z
575,939
<p>The problem with eval is that, as in python, the quotient of integers is an integer. So, you have several choices.</p> <p>The first is simply to make integer division return floats:</p> <pre><code>from __future__ import division </code></pre> <p>The other is to split the rational number:</p> <pre><code>reduce(la...
2
2009-02-22T22:17:37Z
[ "python", "rational-numbers" ]
How to convert rational and decimal number strings to floats in python?
575,925
<p>How can I convert strings which can denote decimal or rational numbers to floats</p> <pre><code>&gt;&gt;&gt; ["0.1234", "1/2"] ['0.1234', '1/2'] </code></pre> <p>I'd want [0.1234, 0.5].</p> <p>eval is what I was thinking but no luck:</p> <pre><code>&gt;&gt;&gt; eval("1/2") 0 </code></pre>
9
2009-02-22T22:08:47Z
575,944
<p>I'd parse the string if conversion fails:</p> <pre><code>&gt;&gt;&gt; def convert(s): try: return float(s) except ValueError: num, denom = s.split('/') return float(num) / float(denom) ... &gt;&gt;&gt; convert("0.1234") 0.1234 &gt;&gt;&gt; convert("1/2") 0.5 </code></pre> <p>Gener...
16
2009-02-22T22:18:47Z
[ "python", "rational-numbers" ]
How to convert rational and decimal number strings to floats in python?
575,925
<p>How can I convert strings which can denote decimal or rational numbers to floats</p> <pre><code>&gt;&gt;&gt; ["0.1234", "1/2"] ['0.1234', '1/2'] </code></pre> <p>I'd want [0.1234, 0.5].</p> <p>eval is what I was thinking but no luck:</p> <pre><code>&gt;&gt;&gt; eval("1/2") 0 </code></pre>
9
2009-02-22T22:08:47Z
575,967
<p>The suggestions with <code>from __future__ import division</code> combined with <code>eval</code> will certainly work.</p> <p>It's probably worth pointing out that the suggestions that don't use <code>eval</code> but rather parse the string do so because <code>eval</code> is dangerous: if there is some way for an a...
0
2009-02-22T22:29:11Z
[ "python", "rational-numbers" ]
How to convert rational and decimal number strings to floats in python?
575,925
<p>How can I convert strings which can denote decimal or rational numbers to floats</p> <pre><code>&gt;&gt;&gt; ["0.1234", "1/2"] ['0.1234', '1/2'] </code></pre> <p>I'd want [0.1234, 0.5].</p> <p>eval is what I was thinking but no luck:</p> <pre><code>&gt;&gt;&gt; eval("1/2") 0 </code></pre>
9
2009-02-22T22:08:47Z
575,976
<p>As others have pointed out, using <code>eval</code> is potentially a security risk, and certainly a bad habit to get into. (if you don't think it's as risky as <code>exec</code>, imagine <code>eval</code>ing something like: <code>__import__('os').system('rm -rf /')</code>)</p> <p>However, if you have python 2.6 or ...
7
2009-02-22T22:34:55Z
[ "python", "rational-numbers" ]
How to convert rational and decimal number strings to floats in python?
575,925
<p>How can I convert strings which can denote decimal or rational numbers to floats</p> <pre><code>&gt;&gt;&gt; ["0.1234", "1/2"] ['0.1234', '1/2'] </code></pre> <p>I'd want [0.1234, 0.5].</p> <p>eval is what I was thinking but no luck:</p> <pre><code>&gt;&gt;&gt; eval("1/2") 0 </code></pre>
9
2009-02-22T22:08:47Z
576,025
<p>Another option (also only for 2.6 and up) is the <code>fractions</code> module.</p> <pre><code>&gt;&gt;&gt; from fractions import Fraction &gt;&gt;&gt; Fraction("0.1234") Fraction(617, 5000) &gt;&gt;&gt; Fraction("1/2") Fraction(1, 2) &gt;&gt;&gt; float(Fraction("0.1234")) 0.1234 &gt;&gt;&gt; float(Fraction("1/2"))...
6
2009-02-22T23:04:44Z
[ "python", "rational-numbers" ]
How to convert rational and decimal number strings to floats in python?
575,925
<p>How can I convert strings which can denote decimal or rational numbers to floats</p> <pre><code>&gt;&gt;&gt; ["0.1234", "1/2"] ['0.1234', '1/2'] </code></pre> <p>I'd want [0.1234, 0.5].</p> <p>eval is what I was thinking but no luck:</p> <pre><code>&gt;&gt;&gt; eval("1/2") 0 </code></pre>
9
2009-02-22T22:08:47Z
576,156
<p>In Python 3, this should work.</p> <pre><code>&gt;&gt;&gt; x = ["0.1234", "1/2"] &gt;&gt;&gt; [eval(i) for i in x] [0.1234, 0.5] </code></pre>
1
2009-02-23T00:18:53Z
[ "python", "rational-numbers" ]
How to convert rational and decimal number strings to floats in python?
575,925
<p>How can I convert strings which can denote decimal or rational numbers to floats</p> <pre><code>&gt;&gt;&gt; ["0.1234", "1/2"] ['0.1234', '1/2'] </code></pre> <p>I'd want [0.1234, 0.5].</p> <p>eval is what I was thinking but no luck:</p> <pre><code>&gt;&gt;&gt; eval("1/2") 0 </code></pre>
9
2009-02-22T22:08:47Z
576,395
<p><a href="http://code.google.com/p/sympy/" rel="nofollow">sympy</a> can help you out here:</p> <pre><code>import sympy half = sympy.Rational('1/2') p1234 = sympy.Rational('0.1234') print '%f, %f" % (half, p1234) </code></pre>
1
2009-02-23T02:54:20Z
[ "python", "rational-numbers" ]
Is there a special trick to downloading a zip file and writing it to disk with Python?
576,238
<p>I am FTPing a zip file from a remote FTP site using Python's ftplib. I then attempt to write it to disk. The file write works, however most attempts to open the zip using WinZip or WinRar fail; both apps claim the file is corrupted. Oddly however, when right clicking and attempting to extract the file using WinRar, ...
1
2009-02-23T01:00:37Z
576,254
<p>I've never used that library, but urllib2 works fine, and is more straightforward. Curl is even better.</p> <p>Looking at your code, I can see a couple of things wrong. Your exception catching only prints the exception, then continues. For fatal errors like not getting an FTP connection, they need to print the m...
1
2009-02-23T01:12:00Z
[ "python", "ftp", "ftplib" ]
Is there a special trick to downloading a zip file and writing it to disk with Python?
576,238
<p>I am FTPing a zip file from a remote FTP site using Python's ftplib. I then attempt to write it to disk. The file write works, however most attempts to open the zip using WinZip or WinRar fail; both apps claim the file is corrupted. Oddly however, when right clicking and attempting to extract the file using WinRar, ...
1
2009-02-23T01:00:37Z
576,258
<p>Pass file.write directly inside the retrbinary function instead of passing appender. This will work and it will also not use that much RAM when you are downloading a big file. </p> <p>If you'd like the data stored inside a variable though, you can also have a variable named: </p> <pre><code>blocks = [] </code></p...
2
2009-02-23T01:13:59Z
[ "python", "ftp", "ftplib" ]
Django Project structure, recommended structure to share an extended auth "User" model across apps?
576,345
<p>I'm wondering what the common project/application structure is when the user model extended/sub-classed and this Resulting User model is shared and used across multiple apps. </p> <p>I'd like to reference the same user model in multiple apps. I haven't built the login interface yet, so I'm not sure how it should f...
1
2009-02-23T02:17:32Z
576,353
<p>You should check first if the contrib.auth module satisfies your needs, so you don't have to reinvent the wheel:</p> <p><a href="http://docs.djangoproject.com/en/dev/topics/auth/#topics-auth" rel="nofollow">http://docs.djangoproject.com/en/dev/topics/auth/#topics-auth</a></p> <p>edit:</p> <p>Check this snippet th...
3
2009-02-23T02:22:59Z
[ "python", "django", "django-models", "django-project-architect" ]
Django Project structure, recommended structure to share an extended auth "User" model across apps?
576,345
<p>I'm wondering what the common project/application structure is when the user model extended/sub-classed and this Resulting User model is shared and used across multiple apps. </p> <p>I'd like to reference the same user model in multiple apps. I haven't built the login interface yet, so I'm not sure how it should f...
1
2009-02-23T02:17:32Z
576,359
<p>i think the 'project/app' names are badly chosen. it's more like 'site/module'. an app can be very useful without having views, for example.</p> <p>check the <a href="http://www.youtube.com/view_play_list?p=D415FAF806EC47A1" rel="nofollow">2008 DjangoCon talks</a> on YouTube, especially the one about <a href="htt...
2
2009-02-23T02:25:52Z
[ "python", "django", "django-models", "django-project-architect" ]
Django Project structure, recommended structure to share an extended auth "User" model across apps?
576,345
<p>I'm wondering what the common project/application structure is when the user model extended/sub-classed and this Resulting User model is shared and used across multiple apps. </p> <p>I'd like to reference the same user model in multiple apps. I haven't built the login interface yet, so I'm not sure how it should f...
1
2009-02-23T02:17:32Z
576,362
<p>Why are you extending User? Please clarify.</p> <p>If you're adding more information about the users, you don't need to roll your own user and auth system. Django's version of that is quite solid. The user management is located in django.contrib.auth.</p> <p>If you need to customize the information stored with u...
6
2009-02-23T02:27:09Z
[ "python", "django", "django-models", "django-project-architect" ]
Is it required to learn Python 2.6 along with Python 3.0?
576,557
<p>If I learn python 3.0 and code in it, will my code be still compatible with Python 2.6 (or 2.5 too!)?</p> <p><hr></p> <p>Remarkably similar to:</p> <p><a href="http://stackoverflow.com/questions/410609/if-im-going-to-learn-python-should-i-learn-2-x-or-just-jump-into-3-0/410626">If I'm Going to Learn Python, Shoul...
0
2009-02-23T04:43:32Z
576,565
<p>No, 3.x is largely incompatible with 2.x (that was actually a major motivation for doing it). In fact, you probably shouldn't be using 3.0 at all-- it's rather unusable at the moment, and is still mostly intended for library developers to port to it so that it can be usable.</p>
5
2009-02-23T04:46:55Z
[ "python" ]
Is it required to learn Python 2.6 along with Python 3.0?
576,557
<p>If I learn python 3.0 and code in it, will my code be still compatible with Python 2.6 (or 2.5 too!)?</p> <p><hr></p> <p>Remarkably similar to:</p> <p><a href="http://stackoverflow.com/questions/410609/if-im-going-to-learn-python-should-i-learn-2-x-or-just-jump-into-3-0/410626">If I'm Going to Learn Python, Shoul...
0
2009-02-23T04:43:32Z
576,567
<p>NO. Python 3 code is backwards incompatible with 2.6. I recommend to begin with 2.6, because your code will be more <strong>useful</strong>.</p>
1
2009-02-23T04:47:38Z
[ "python" ]
Is it required to learn Python 2.6 along with Python 3.0?
576,557
<p>If I learn python 3.0 and code in it, will my code be still compatible with Python 2.6 (or 2.5 too!)?</p> <p><hr></p> <p>Remarkably similar to:</p> <p><a href="http://stackoverflow.com/questions/410609/if-im-going-to-learn-python-should-i-learn-2-x-or-just-jump-into-3-0/410626">If I'm Going to Learn Python, Shoul...
0
2009-02-23T04:43:32Z
576,583
<p>Python 2.6 and Python 3.0 are <em>very</em> compatible with each other. There honestly aren't very many differences between the two. At this point, third-party library support is far better for the 2.x series (last I checked, a few libraries I use hadn't been updated from 2.5, but going from 2.5 to 2.6 is just a rec...
4
2009-02-23T05:01:46Z
[ "python" ]
Is it required to learn Python 2.6 along with Python 3.0?
576,557
<p>If I learn python 3.0 and code in it, will my code be still compatible with Python 2.6 (or 2.5 too!)?</p> <p><hr></p> <p>Remarkably similar to:</p> <p><a href="http://stackoverflow.com/questions/410609/if-im-going-to-learn-python-should-i-learn-2-x-or-just-jump-into-3-0/410626">If I'm Going to Learn Python, Shoul...
0
2009-02-23T04:43:32Z
576,666
<p>It would be easier to use 2.6 right now because most external libraries are not compatible with 3 yet. </p>
2
2009-02-23T06:14:30Z
[ "python" ]
PyGame not receiving events when 3+ keys are pressed at the same time
576,634
<p><em>I am developing a simple game in <a href="http://www.pygame.org/" rel="nofollow">PyGame</a>... A rocket ship flying around and shooting stuff.</em> </p> <hr> <p><strong>Question:</strong> Why does pygame stop emitting keyboard events when too may keys are pressed at once?</p> <p><strong>About the Key Handli...
3
2009-02-23T05:55:34Z
576,643
<p>This sounds like a input problem, not a code problem - are you sure the problem isn't the keyboard itself? Most keyboards have limitations on the number of keys that can be pressed at the same time. Often times you can't press more than a few keys that are close together at a time.</p> <p>To test it out, just sta...
10
2009-02-23T06:02:08Z
[ "python", "pygame", "keyboard-events" ]
PyGame not receiving events when 3+ keys are pressed at the same time
576,634
<p><em>I am developing a simple game in <a href="http://www.pygame.org/" rel="nofollow">PyGame</a>... A rocket ship flying around and shooting stuff.</em> </p> <hr> <p><strong>Question:</strong> Why does pygame stop emitting keyboard events when too may keys are pressed at once?</p> <p><strong>About the Key Handli...
3
2009-02-23T05:55:34Z
576,646
<p>It may very well depend on the keyboard. My current no-name keyboard only supports two keys pressed at the same time, often a pain in games.</p>
1
2009-02-23T06:02:59Z
[ "python", "pygame", "keyboard-events" ]
PyGame not receiving events when 3+ keys are pressed at the same time
576,634
<p><em>I am developing a simple game in <a href="http://www.pygame.org/" rel="nofollow">PyGame</a>... A rocket ship flying around and shooting stuff.</em> </p> <hr> <p><strong>Question:</strong> Why does pygame stop emitting keyboard events when too may keys are pressed at once?</p> <p><strong>About the Key Handli...
3
2009-02-23T05:55:34Z
576,647
<p>Some keyboards cannot send certain keys together. Often this limit is reached with 3 keys.</p>
2
2009-02-23T06:03:05Z
[ "python", "pygame", "keyboard-events" ]
PyGame not receiving events when 3+ keys are pressed at the same time
576,634
<p><em>I am developing a simple game in <a href="http://www.pygame.org/" rel="nofollow">PyGame</a>... A rocket ship flying around and shooting stuff.</em> </p> <hr> <p><strong>Question:</strong> Why does pygame stop emitting keyboard events when too may keys are pressed at once?</p> <p><strong>About the Key Handli...
3
2009-02-23T05:55:34Z
579,243
<p>As others have eluded to already, certain (especially cheaper, lower-end) keyboards have a low quality <a href="http://en.wikipedia.org/wiki/Keyboard_technology#Keyboard_switch_matrix" rel="nofollow">keyboard matrix</a>. With these keyboards, certain key combinations will lead to the behavior you're experiencing. An...
5
2009-02-23T20:42:15Z
[ "python", "pygame", "keyboard-events" ]
Writing a TTL decorator in Python
576,776
<p>I'm trying to write a TTL decorator in python. Basically I give it raise an exception if the function doesn't answer in the selected time.</p> <p>You can find the thead2 snippets on <a href="http://sebulba.wikispaces.com/recipe+thread2" rel="nofollow">http://sebulba.wikispaces.com/recipe+thread2</a></p> <pre><code...
3
2009-02-23T07:16:49Z
576,801
<p>The code provided is a bit tough to follow -- how is it going to raise an exception in the right place at the right time in the right thread?</p> <p><strong>Consider this rough flow:</strong></p> <p>Decorator function called with target function. Return a function which:</p> <ol> <li>Starts thread, calling target...
5
2009-02-23T07:33:17Z
[ "python", "decorator", "ttl" ]
Writing a TTL decorator in Python
576,776
<p>I'm trying to write a TTL decorator in python. Basically I give it raise an exception if the function doesn't answer in the selected time.</p> <p>You can find the thead2 snippets on <a href="http://sebulba.wikispaces.com/recipe+thread2" rel="nofollow">http://sebulba.wikispaces.com/recipe+thread2</a></p> <pre><code...
3
2009-02-23T07:16:49Z
5,914,987
<p>If you want the function's execution to be terminated after the timeout has been exceeded, you might want to try code that has that capability. To use the module, all that needs to be done is for your function to be called as an argument to <code>add_timeout</code>, and the returned value can run. Once called, the o...
0
2011-05-06T17:37:34Z
[ "python", "decorator", "ttl" ]
Is there a known Win32 Tkinter bug with respect to displaying photos on a canvas?
576,843
<p>I'm noticing a pretty strange bug with tkinter, and I am wondering if it's because there's something in how the python interacts with the tcl, at least in Win32.</p> <p>Here I have a super simple program that displays a gif image. It works perfectly.</p> <pre><code>from Tkinter import * canvas = Canvas(width=300...
3
2009-02-23T07:55:51Z
579,364
<p>Do that as a quick solution, and I'll try to explain:</p> <pre><code>def set_canvas(cv): global photo # here! photo=PhotoImage(file=sys.argv[1]) cv.create_image(0, 0, image=photo, anchor=NW) # embed a photo print cv print photo </code></pre> <p>A PhotoImage needs to have at least one reference...
6
2009-02-23T21:08:36Z
[ "python", "user-interface", "winapi", "tkinter" ]
How can I reference columns by their names in python calling SQLite?
576,933
<p>I have some code which I've been using to query MySQL, and I'm hoping to use it with SQLite. My real hope is that this will not involve making too many changes to the code. Unfortunately, the following code doesn't work with SQLite:</p> <pre><code>cursor.execute(query) rows = cursor.fetchall() data = [] for row i...
10
2009-02-23T08:50:12Z
576,954
<p>I'm not sure if this is the best approach, but here's what I typically do to retrieve a record set using a DB-API 2 compliant module:</p> <pre><code>cursor.execute("""SELECT foo, bar, baz, quux FROM table WHERE id = %s;""", (interesting_record_id,)) for foo, bar, baz, quux in cursor.fetchall(): ...
9
2009-02-23T09:01:05Z
[ "python", "sqlite" ]
How can I reference columns by their names in python calling SQLite?
576,933
<p>I have some code which I've been using to query MySQL, and I'm hoping to use it with SQLite. My real hope is that this will not involve making too many changes to the code. Unfortunately, the following code doesn't work with SQLite:</p> <pre><code>cursor.execute(query) rows = cursor.fetchall() data = [] for row i...
10
2009-02-23T08:50:12Z
577,004
<p>To access columns by name, use the <a href="http://oss.itsystementwicklung.de/download/pysqlite/doc/sqlite3.html#sqlite3.Connection.row_factory"><code>row_factory</code></a> attribute of the Connection instance. It lets you set a function that takes the arguments <code>cursor</code> and <code>row</code>, and return...
13
2009-02-23T09:25:44Z
[ "python", "sqlite" ]
How can I reference columns by their names in python calling SQLite?
576,933
<p>I have some code which I've been using to query MySQL, and I'm hoping to use it with SQLite. My real hope is that this will not involve making too many changes to the code. Unfortunately, the following code doesn't work with SQLite:</p> <pre><code>cursor.execute(query) rows = cursor.fetchall() data = [] for row i...
10
2009-02-23T08:50:12Z
577,916
<p>The SQLite API supports cursor.description so you can easily do it like this</p> <pre><code>headers = {} for record in cursor.fetchall(): if not headers: headers = dict((desc[0], idx) for idx,desc in cursor.description)) data.append(record[headers['column_name']]) </code></pre> <p>A little long win...
1
2009-02-23T15:06:27Z
[ "python", "sqlite" ]
How can I reference columns by their names in python calling SQLite?
576,933
<p>I have some code which I've been using to query MySQL, and I'm hoping to use it with SQLite. My real hope is that this will not involve making too many changes to the code. Unfortunately, the following code doesn't work with SQLite:</p> <pre><code>cursor.execute(query) rows = cursor.fetchall() data = [] for row i...
10
2009-02-23T08:50:12Z
7,099,412
<p>This can be done by adding a single line after the "connect" statment:</p> <pre><code>conn.row_factory = sqlite3.Row </code></pre> <p>Check the documentation here: <a href="http://docs.python.org/library/sqlite3.html#accessing-columns-by-name-instead-of-by-index">http://docs.python.org/library/sqlite3.html#accessi...
8
2011-08-17T21:00:19Z
[ "python", "sqlite" ]
How can I reference columns by their names in python calling SQLite?
576,933
<p>I have some code which I've been using to query MySQL, and I'm hoping to use it with SQLite. My real hope is that this will not involve making too many changes to the code. Unfortunately, the following code doesn't work with SQLite:</p> <pre><code>cursor.execute(query) rows = cursor.fetchall() data = [] for row i...
10
2009-02-23T08:50:12Z
11,664,048
<p>kushal's answer to <a href="http://www.gossamer-threads.com/lists/python/python/755969" rel="nofollow">this forum</a> works fine:</p> <blockquote> <p>Use a DictCursor:</p> </blockquote> <pre><code>import MySQLdb.cursors . . . cursor = db.cursor (MySQLdb.cursors.DictCursor) cursor.execute (query) rows = cursor.f...
1
2012-07-26T06:59:46Z
[ "python", "sqlite" ]
How can I reference columns by their names in python calling SQLite?
576,933
<p>I have some code which I've been using to query MySQL, and I'm hoping to use it with SQLite. My real hope is that this will not involve making too many changes to the code. Unfortunately, the following code doesn't work with SQLite:</p> <pre><code>cursor.execute(query) rows = cursor.fetchall() data = [] for row i...
10
2009-02-23T08:50:12Z
20,042,292
<p>In the five years since the question was asked and then answered, a very simple solution has arisen. Any new code can simply wrap the connection object with a row factory. Code example:</p> <pre><code>import sqlite3 conn = sqlite3.connect('./someFile') conn.row_factory = sqlite3.Row // Here's the magic! cu...
8
2013-11-18T07:27:11Z
[ "python", "sqlite" ]
How do I design sms service?
576,940
<p>I want to design a website that can send and receive sms.</p> <ol> <li>How should I approach the problem ?</li> <li>What are the resources available ?</li> <li>I know php,python what else do I need or are the better options available?</li> <li>How can experiment using my pc only?[somthing like localhost]</li> <li>W...
1
2009-02-23T08:53:45Z
576,947
<p>You need a SMS server. <a href="http://www.ozeki.hu/" rel="nofollow">This</a> should get you started.</p>
0
2009-02-23T08:55:54Z
[ "php", "python", "sms", "mobile-phones", "bulksms" ]
How do I design sms service?
576,940
<p>I want to design a website that can send and receive sms.</p> <ol> <li>How should I approach the problem ?</li> <li>What are the resources available ?</li> <li>I know php,python what else do I need or are the better options available?</li> <li>How can experiment using my pc only?[somthing like localhost]</li> <li>W...
1
2009-02-23T08:53:45Z
576,959
<p>You can take a look at <a href="http://www.kannel.org" rel="nofollow">Kannel</a>. It's so simple to create SMS services using it. Just define a keyword, then put in the URL to which the incoming SMS request will be routed (you'll get the info such as mobile number and SMS text in query string parameters), then whate...
3
2009-02-23T09:04:35Z
[ "php", "python", "sms", "mobile-phones", "bulksms" ]
How do I design sms service?
576,940
<p>I want to design a website that can send and receive sms.</p> <ol> <li>How should I approach the problem ?</li> <li>What are the resources available ?</li> <li>I know php,python what else do I need or are the better options available?</li> <li>How can experiment using my pc only?[somthing like localhost]</li> <li>W...
1
2009-02-23T08:53:45Z
577,001
<p>First thing, You need to sign up for an account (SMS gateway), most of them also give you example code how to send and receive sms using their API. Then you will wrap the the sms functionality around your sites logic. </p> <p>e.g <a href="http://www.clickatell.com/developers/php.php" rel="nofollow">http://www.clic...
2
2009-02-23T09:25:06Z
[ "php", "python", "sms", "mobile-phones", "bulksms" ]
How do I design sms service?
576,940
<p>I want to design a website that can send and receive sms.</p> <ol> <li>How should I approach the problem ?</li> <li>What are the resources available ?</li> <li>I know php,python what else do I need or are the better options available?</li> <li>How can experiment using my pc only?[somthing like localhost]</li> <li>W...
1
2009-02-23T08:53:45Z
577,345
<p>Since my company does this sometimes (text promotions etc, though our main focus is much much lower level stuff), I figured I should pitch in.</p> <p>By far the simplest way is to use a service such as <a href="http://www.clickatell.com" rel="nofollow">Clickatell</a>, which provides a HTTP API, as well as FTP and <...
0
2009-02-23T11:32:17Z
[ "php", "python", "sms", "mobile-phones", "bulksms" ]
How do I design sms service?
576,940
<p>I want to design a website that can send and receive sms.</p> <ol> <li>How should I approach the problem ?</li> <li>What are the resources available ?</li> <li>I know php,python what else do I need or are the better options available?</li> <li>How can experiment using my pc only?[somthing like localhost]</li> <li>W...
1
2009-02-23T08:53:45Z
577,357
<p>I've copied this from an <a href="http://stackoverflow.com/questions/432944/sms-from-web-application/433020#433020">answer I gave</a> in relation to <a href="http://stackoverflow.com/questions/432944/sms-from-web-application/">this question</a>. However, in addition to the text below, take a look at <a href="http:/...
2
2009-02-23T11:38:17Z
[ "php", "python", "sms", "mobile-phones", "bulksms" ]
How can I change a huge file into csv in python
576,967
<p>I'm a beginner in python. I have a huge text file (hundreds of GB) and I want to convert the file into csv file. In my text file, I know the row delimiter is a string "&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>". If a line contains that string, I want to replace it with ". Is there a way to do it without having to read the...
2
2009-02-23T09:10:01Z
576,976
<p>It's only wasteful if you don't have disk to spare. That is, fix it when it's a problem. Your solution looks ok as a first attempt.</p> <p>It's not wasteful of memory because a file handler is a stream.</p>
1
2009-02-23T09:15:35Z
[ "python", "file", "csv" ]
How can I change a huge file into csv in python
576,967
<p>I'm a beginner in python. I have a huge text file (hundreds of GB) and I want to convert the file into csv file. In my text file, I know the row delimiter is a string "&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>". If a line contains that string, I want to replace it with ". Is there a way to do it without having to read the...
2
2009-02-23T09:10:01Z
576,984
<p>Yes, open() treats the file as a stream, as does readline(). It'll only read the next line. If you call read(), however, it'll read everything into memory.</p> <p>Your example code looks ok at first glance. Almost every solution will require you to copy the file elsewhere. Its not exactly easy to modify the con...
4
2009-02-23T09:18:55Z
[ "python", "file", "csv" ]
How can I change a huge file into csv in python
576,967
<p>I'm a beginner in python. I have a huge text file (hundreds of GB) and I want to convert the file into csv file. In my text file, I know the row delimiter is a string "&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>". If a line contains that string, I want to replace it with ". Is there a way to do it without having to read the...
2
2009-02-23T09:10:01Z
576,995
<p>Reading lines is simply done using a <a href="http://docs.python.org/library/stdtypes.html#file.next" rel="nofollow">file iterator</a>:</p> <pre><code>for line in fin: if line.contains("&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;"): fout.writeline("\"") </code></pre> <p>Also consider...
1
2009-02-23T09:23:05Z
[ "python", "file", "csv" ]
How can I change a huge file into csv in python
576,967
<p>I'm a beginner in python. I have a huge text file (hundreds of GB) and I want to convert the file into csv file. In my text file, I know the row delimiter is a string "&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>". If a line contains that string, I want to replace it with ". Is there a way to do it without having to read the...
2
2009-02-23T09:10:01Z
577,047
<p>[For the problem exactly as stated] There's no way that this can be done without copying the data, in python or any other language. If your processing always replaced substrings with new substrings of <em>equal length</em>, maybe you could do it in-place. But whenever you replace <code>&lt;>&lt;>&lt;>&lt;>&lt;>&lt...
0
2009-02-23T09:44:55Z
[ "python", "file", "csv" ]
How can I change a huge file into csv in python
576,967
<p>I'm a beginner in python. I have a huge text file (hundreds of GB) and I want to convert the file into csv file. In my text file, I know the row delimiter is a string "&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>". If a line contains that string, I want to replace it with ". Is there a way to do it without having to read the...
2
2009-02-23T09:10:01Z
577,068
<p>If you're delimiting fields with double quotes, it looks like you need to escape the double quotes you have occurring in your elements (for example <code>1231214 ""</code> will need to be <code>\n1231214 \"\"</code>).</p> <p>Something like</p> <pre><code>fin = open("input", "r") fout = open("output", "w") for line...
0
2009-02-23T09:55:01Z
[ "python", "file", "csv" ]
How can I change a huge file into csv in python
576,967
<p>I'm a beginner in python. I have a huge text file (hundreds of GB) and I want to convert the file into csv file. In my text file, I know the row delimiter is a string "&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>". If a line contains that string, I want to replace it with ". Is there a way to do it without having to read the...
2
2009-02-23T09:10:01Z
577,158
<p>@richard-levasseur</p> <p>I agree, <code>sed</code> seems like the right way to go. Here's a rough cut at what the OP describes:</p> <pre><code> sed -i -e's/&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;/"/g' foo.txt </code></pre> <p>This will do the replacement in-place in the existing <code>foo.txt</c...
5
2009-02-23T10:27:42Z
[ "python", "file", "csv" ]
How can I change a huge file into csv in python
576,967
<p>I'm a beginner in python. I have a huge text file (hundreds of GB) and I want to convert the file into csv file. In my text file, I know the row delimiter is a string "&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>". If a line contains that string, I want to replace it with ". Is there a way to do it without having to read the...
2
2009-02-23T09:10:01Z
577,867
<p>With python you will have to create a new file for safety sake, it will cause alot less headaches than trying to write in place.</p> <p>The below listed reads your input 1 line at a time and buffers the columns (from what I understood of your test input file was 1 row) and then once the end of row delimiter is hit ...
1
2009-02-23T14:53:51Z
[ "python", "file", "csv" ]
How can I change a huge file into csv in python
576,967
<p>I'm a beginner in python. I have a huge text file (hundreds of GB) and I want to convert the file into csv file. In my text file, I know the row delimiter is a string "&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>". If a line contains that string, I want to replace it with ". Is there a way to do it without having to read the...
2
2009-02-23T09:10:01Z
580,169
<p>@Constatin suggests that if you would be satisfied with replacing <pre><code>'&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>&lt;>\n'</code></pre> by <pre><code>'" \n'</code></pre> then the replacement string is the same length, and in that case you can craft a solution to in-place editing with <code>mmap</code>. You wil...
1
2009-02-24T01:59:12Z
[ "python", "file", "csv" ]
How to exit a module before it has finished parsing?
577,119
<p>I have a module that imports a module, but in some cases the module being imported may not exist. After the module is imported there is a class inherits from a class the imported module. If I was to catch the <code>ImportError</code> exception in the case the module doesn't exist, how can I stop Python from parsing ...
4
2009-02-23T10:15:09Z
577,172
<p>You could use:</p> <pre><code>try: from skynet import SkyNet inherit_from = SkyNet except ImportError: inherit_from = object class SelfAwareSkyeNet(inherit_from): pass </code></pre> <p>This works only if the implementation do not differ.</p> <p><strong>Edit:</strong> New solution after comment.</p>
2
2009-02-23T10:34:43Z
[ "python", "import", "module" ]
How to exit a module before it has finished parsing?
577,119
<p>I have a module that imports a module, but in some cases the module being imported may not exist. After the module is imported there is a class inherits from a class the imported module. If I was to catch the <code>ImportError</code> exception in the case the module doesn't exist, how can I stop Python from parsing ...
4
2009-02-23T10:15:09Z
577,211
<p>try: supports an else: clause</p> <pre><code>try: from skynet import SkyNet except ImportError: class SelfAwareSkyNet(): pass else: class SelfAwareSkyNet(SkyNet): pass </code></pre>
8
2009-02-23T10:51:16Z
[ "python", "import", "module" ]
Python "extend" for a dictionary
577,234
<p>Which is the best way to extend a dictionary with another one? For instance:</p> <pre><code>&gt;&gt;&gt; a = { "a" : 1, "b" : 2 } &gt;&gt;&gt; b = { "c" : 3, "d" : 4 } &gt;&gt;&gt; a {'a': 1, 'b': 2} &gt;&gt;&gt; b {'c': 3, 'd': 4} </code></pre> <p>I'm looking for any operation to obtain this avoiding <code>for</c...
211
2009-02-23T10:59:32Z
577,241
<pre><code>a.update(b) </code></pre> <p><a href="http://docs.python.org/2/library/stdtypes.html#dict.update">Python Standard Library Documentation</a></p>
352
2009-02-23T11:01:49Z
[ "python", "dictionary" ]
Python "extend" for a dictionary
577,234
<p>Which is the best way to extend a dictionary with another one? For instance:</p> <pre><code>&gt;&gt;&gt; a = { "a" : 1, "b" : 2 } &gt;&gt;&gt; b = { "c" : 3, "d" : 4 } &gt;&gt;&gt; a {'a': 1, 'b': 2} &gt;&gt;&gt; b {'c': 3, 'd': 4} </code></pre> <p>I'm looking for any operation to obtain this avoiding <code>for</c...
211
2009-02-23T10:59:32Z
577,245
<pre><code>a.update(b) </code></pre> <p>Will add keys and values from <em>b</em> to <em>a</em>, overwriting if there's already a value for a key.</p>
18
2009-02-23T11:04:22Z
[ "python", "dictionary" ]
Python "extend" for a dictionary
577,234
<p>Which is the best way to extend a dictionary with another one? For instance:</p> <pre><code>&gt;&gt;&gt; a = { "a" : 1, "b" : 2 } &gt;&gt;&gt; b = { "c" : 3, "d" : 4 } &gt;&gt;&gt; a {'a': 1, 'b': 2} &gt;&gt;&gt; b {'c': 3, 'd': 4} </code></pre> <p>I'm looking for any operation to obtain this avoiding <code>for</c...
211
2009-02-23T10:59:32Z
1,552,420
<p>A beautiful gem in <a href="http://stackoverflow.com/questions/1551666/how-can-2-python-dictionaries-become-1/1551878#1551878">this closed question</a>:</p> <p>The "oneliner way", altering neither of the input dicts, is</p> <pre><code>basket = dict(basket_one, **basket_two) </code></pre> <p>Learn what <a href="ht...
109
2009-10-12T02:27:52Z
[ "python", "dictionary" ]
Python "extend" for a dictionary
577,234
<p>Which is the best way to extend a dictionary with another one? For instance:</p> <pre><code>&gt;&gt;&gt; a = { "a" : 1, "b" : 2 } &gt;&gt;&gt; b = { "c" : 3, "d" : 4 } &gt;&gt;&gt; a {'a': 1, 'b': 2} &gt;&gt;&gt; b {'c': 3, 'd': 4} </code></pre> <p>I'm looking for any operation to obtain this avoiding <code>for</c...
211
2009-02-23T10:59:32Z
12,697,215
<p>As others have mentioned, <code>a.update(b)</code> for some dicts <code>a</code> and <code>b</code> will achieve the result you've asked for in your question. However, I want to point out that many times I have seen the <code>extend</code> method of mapping/set objects desire that in the syntax <code>a.extend(b)</co...
12
2012-10-02T19:48:15Z
[ "python", "dictionary" ]
gtk TextView widget doesn't update during function
577,302
<p>I'm new to GUI programming with python and gtk, so this is a bit of a beginners question. I have a function that is called when a button is pressed which does various tasks, and a TextView widget which I write to after each task is completed. The problem is that the TextView widget doesn't update until the entire f...
0
2009-02-23T11:18:37Z
577,461
<p>After each update to the TextView call</p> <pre><code>while gtk.events_pending(): gtk.main_iteration() </code></pre> <p>You can do your update through a custom function:</p> <pre><code>def my_insert(self, widget, report, text): report.insert_at_cursor(text) while gtk.events_pending(): gtk.main_iteratio...
4
2009-02-23T12:17:22Z
[ "python", "gtk", "pygtk" ]
Django - how do I _not_ dispatch a signal?
577,376
<p>I wrote some smart generic counters and managers for my models (to avoid <code>select count</code> queries etc.). Therefore I got some heavy logic going on for post_save. </p> <p>I would like to prevent handling the signal when there's no need to. I guess the perfect interface would be:</p> <pre><code>instance.s...
4
2009-02-23T11:45:25Z
577,432
<p>A quick and dirty solution would be:</p> <pre><code>from django.db.models.signals import post_save from somewhere_in_my_app import my_post_save_handler post_save.disconnect(my_post_save_handler) instance.save() post_save.connect(my_post_save_handler) </code></pre> <p>But otherwise i strongly recommend moving your...
11
2009-02-23T12:05:29Z
[ "python", "django", "django-signals" ]
Django - how do I _not_ dispatch a signal?
577,376
<p>I wrote some smart generic counters and managers for my models (to avoid <code>select count</code> queries etc.). Therefore I got some heavy logic going on for post_save. </p> <p>I would like to prevent handling the signal when there's no need to. I guess the perfect interface would be:</p> <pre><code>instance.s...
4
2009-02-23T11:45:25Z
1,420,789
<p>You can also call <code>instance.save_base(raw=True)</code> and check for the <code>raw</code> argument in your <code>pre_save</code> or <code>post_save</code> signal handler:</p> <pre><code>def my_post_save_handler(instance, raw, **kwargs): if not raw: heavy_logic() </code></pre> <p>You can add some s...
2
2009-09-14T10:39:02Z
[ "python", "django", "django-signals" ]
Django - how do I _not_ dispatch a signal?
577,376
<p>I wrote some smart generic counters and managers for my models (to avoid <code>select count</code> queries etc.). Therefore I got some heavy logic going on for post_save. </p> <p>I would like to prevent handling the signal when there's no need to. I guess the perfect interface would be:</p> <pre><code>instance.s...
4
2009-02-23T11:45:25Z
10,881,618
<p>You can disconnect and reconnect the signal. Try using a <code>with:</code> statement with this utility class:</p> <pre><code>class SignalBlocker(object): def __init__(self, signal, receiver, **kwargs): self.signal = signal self.receiver = receiver self.kwargs = kwargs def __enter__...
11
2012-06-04T12:49:48Z
[ "python", "django", "django-signals" ]
Django - how do I _not_ dispatch a signal?
577,376
<p>I wrote some smart generic counters and managers for my models (to avoid <code>select count</code> queries etc.). Therefore I got some heavy logic going on for post_save. </p> <p>I would like to prevent handling the signal when there's no need to. I guess the perfect interface would be:</p> <pre><code>instance.s...
4
2009-02-23T11:45:25Z
16,556,300
<p>I found simple and easy solution:</p> <pre><code>MyModel.objects.filter(pk=instance.id).update(**data) </code></pre> <p>It is due to (<a href="https://docs.djangoproject.com/en/1.5/ref/models/querysets/#update">https://docs.djangoproject.com/en/1.5/ref/models/querysets/#update</a>):</p> <blockquote> <p>Finally,...
11
2013-05-15T03:25:55Z
[ "python", "django", "django-signals" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
577,486
<p>In Windows, you can use the <a href="http://docs.python.org/library/msvcrt.html#msvcrt-console" rel="nofollow"><code>msvcrt</code></a> module. </p> <blockquote> <p><strong>msvcrt.kbhit()</strong> Return true if a keypress is waiting to be read.</p> <p><strong>msvcrt.getch()</strong> Read a keypre...
3
2009-02-23T12:28:33Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
577,487
<p>There's no need to wait for input before closing, just change your command like so:</p> <pre><code>cmd /K python &lt;script&gt; </code></pre> <p>The <code>/K</code> switch will execute the command that follows, but leave the command interpreter window open, in contrast to <code>/C</code>, which executes and then c...
12
2009-02-23T12:29:39Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
577,488
<p>One way is to leave a <code>raw_input()</code> at the end so the script waits for you to press Enter before it terminates.</p>
43
2009-02-23T12:30:06Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
577,499
<p>An external WConio module can help here: <a href="http://newcenturycomputers.net/projects/wconio.html" rel="nofollow">http://newcenturycomputers.net/projects/wconio.html</a></p> <pre><code>import WConio WConio.getch() </code></pre>
0
2009-02-23T12:35:08Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
577,529
<blockquote> <p>One way is to leave a raw_input() at the end so the script waits for you to press enter before it terminates.</p> </blockquote> <p>The advantage of using raw_input() instead of msvcrt.* stuff is that the former is a part of standard Python (i.e. absolutely cross-platform). This also means that the sc...
7
2009-02-23T12:51:41Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
578,751
<pre><code>import pdb pdb.debug() </code></pre> <p>This is used to debug the script. Should be useful to break also.</p>
0
2009-02-23T18:35:12Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
4,130,571
<p>Try <code>os.system("pause")</code>I used it and it worked for me :)</p>
26
2010-11-09T04:41:40Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
4,130,603
<p>Getting python to read a single character from the terminal in an unbuffered manner is a little bit tricky, but here's a recipe that'll do it: </p> <p><a href="http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/" rel="nofollow">Recipe 134892: getch()-like unbuffered charac...
0
2010-11-09T04:49:09Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
15,254,312
<p>The best option: <code>os.system('pause')</code> &lt;-- this will actually display a message saying 'press any key to continue' whereas adding just <code>raw_input('')</code> will print no message, just the cursor will be available. </p> <p>not related to answer:</p> <p><code>os.system("some cmd command")</code> i...
6
2013-03-06T17:44:46Z
[ "python", "command-line" ]
Pause in Python
577,467
<p>I am running command-line Python scripts from the Windows taskbar by having a shortcut pointing to the Python interpreter with the actual script as a parameter.</p> <p>After the script has been processed, the interpreter terminates and the output window is closed which makes it impossible to read script output.</p>...
38
2009-02-23T12:20:26Z
22,947,778
<p>If you type </p> <pre><code>input("") </code></pre> <p>It will wait for them to press any button then it will continue. Also you can put text between the quotes.</p>
-1
2014-04-08T20:44:16Z
[ "python", "command-line" ]
Rotating a glViewport?
577,639
<p>In a "multitouch" environement, any application showed on a surface can be rotated/scaled to the direction of an user. Actual solution is to drawing the application on a FBO, and draw a rotated/scaled rectangle with the texture on it. I don't think it's good for performance, and all graphics cards don't provide FBO....
2
2009-02-23T13:33:48Z
577,672
<p>If you already have the code set up to render your scene, try adding a <code>glRotate()</code> call to the viewmodel matrix setup, to "rotate the camera" before rendering the scene.</p>
2
2009-02-23T13:42:21Z
[ "python", "math", "opengl" ]
Rotating a glViewport?
577,639
<p>In a "multitouch" environement, any application showed on a surface can be rotated/scaled to the direction of an user. Actual solution is to drawing the application on a FBO, and draw a rotated/scaled rectangle with the texture on it. I don't think it's good for performance, and all graphics cards don't provide FBO....
2
2009-02-23T13:33:48Z
593,825
<p>There's no way to have a rotated viewport in OpenGL, you have to handle it manually. I see the following possible solutions :</p> <ul> <li><p>Keep on using textures, perhaps using glCopyTexSubImage instead of FBOs, as this is basic OpenGL feature. If your target platforms are hardware accelerated, performance shoul...
2
2009-02-27T07:20:43Z
[ "python", "math", "opengl" ]
How do I prevent Python's os.walk from walking across mount points?
577,761
<p>In Unix all disks are exposed as paths in the main filesystem, so <code>os.walk('/')</code> would traverse, for example, <code>/media/cdrom</code> as well as the primary hard disk, and that is undesirable for some applications.</p> <p>How do I get an <code>os.walk</code> that stays on a single device?</p> <p>Relat...
7
2009-02-23T14:16:59Z
577,807
<p><code>os.walk()</code> can't tell (as far as I know) that it is browsing a different drive. You will need to check that yourself.</p> <p>Try using <code>os.stat()</code>, or checking that the root variable from <code>os.walk()</code> is not <code>/media</code></p>
1
2009-02-23T14:31:05Z
[ "python", "linux", "unix" ]
How do I prevent Python's os.walk from walking across mount points?
577,761
<p>In Unix all disks are exposed as paths in the main filesystem, so <code>os.walk('/')</code> would traverse, for example, <code>/media/cdrom</code> as well as the primary hard disk, and that is undesirable for some applications.</p> <p>How do I get an <code>os.walk</code> that stays on a single device?</p> <p>Relat...
7
2009-02-23T14:16:59Z
577,830
<p>From <code>os.walk</code> docs:</p> <blockquote> <p>When topdown is true, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search</p> </blockquote...
15
2009-02-23T14:39:56Z
[ "python", "linux", "unix" ]
How do I prevent Python's os.walk from walking across mount points?
577,761
<p>In Unix all disks are exposed as paths in the main filesystem, so <code>os.walk('/')</code> would traverse, for example, <code>/media/cdrom</code> as well as the primary hard disk, and that is undesirable for some applications.</p> <p>How do I get an <code>os.walk</code> that stays on a single device?</p> <p>Relat...
7
2009-02-23T14:16:59Z
577,835
<p>I think <a href="http://docs.python.org/library/os.path.html#os.path.ismount" rel="nofollow">os.path.ismount</a> might work for you. You code might look something like this:</p> <pre><code>import os import os.path for root, dirs, files in os.walk('/'): # Handle files. dirs[:] = filter(lambda dir: not os.pa...
3
2009-02-23T14:40:49Z
[ "python", "linux", "unix" ]
Python Socket help (Syntax error)
577,779
<pre><code>import socket HOST = "swemach.se" PORT = 21 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT) data = s.recv(1024) s.close() print "%s" % data </code></pre> <p>Gives me error</p> <pre><code>File "main.txt", line 7 data = s.recv(1024) ^ SyntaxError: invaild syntax </code></pre> ...
0
2009-02-23T14:22:21Z
577,784
<p>You've forgot parenthesis.</p> <pre><code>s.connect((HOST, PORT)) </code></pre>
4
2009-02-23T14:24:33Z
[ "python" ]
Python Socket help (Syntax error)
577,779
<pre><code>import socket HOST = "swemach.se" PORT = 21 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT) data = s.recv(1024) s.close() print "%s" % data </code></pre> <p>Gives me error</p> <pre><code>File "main.txt", line 7 data = s.recv(1024) ^ SyntaxError: invaild syntax </code></pre> ...
0
2009-02-23T14:22:21Z
577,789
<pre><code>(( HOST, PORT) </code></pre> <p>^ there you go</p>
4
2009-02-23T14:24:58Z
[ "python" ]
PyQt: No such slot
577,824
<p>I am starting to learn Qt4 and Python, following along some tutorial i found on the interwebs. I have the following two files:</p> <p>lcdrange.py:</p> <pre><code>from PyQt4 import QtGui, QtCore class LCDRange(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init...
1
2009-02-23T14:37:06Z
577,882
<p>Try this:</p> <pre><code>self.connect(lcdRange, QtCore.SIGNAL('valueChanged'), previousRange.setValue) </code></pre> <h1>What's the difference?</h1> <p><a href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#signal-and-slot-support" rel="nofollow">The PyQt documentation</a> has a section abou...
7
2009-02-23T14:57:53Z
[ "python", "ruby", "qt4", "pyqt" ]
PyQt: No such slot
577,824
<p>I am starting to learn Qt4 and Python, following along some tutorial i found on the interwebs. I have the following two files:</p> <p>lcdrange.py:</p> <pre><code>from PyQt4 import QtGui, QtCore class LCDRange(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init...
1
2009-02-23T14:37:06Z
958,995
<p>NOTE</p> <p>The "text" within the SIGNAL must match the c++ API documentation. </p> <pre><code># This will work - its IDENTICAL to the documentation QtCore.SIGNAL('customContextMenuRequested(const QPoint&amp;)') # this wont QtCore.SIGNAL('customContextMenuRequested(QPoint&amp;)') # and this wont QtCore.SIGNAL('...
0
2009-06-06T04:29:47Z
[ "python", "ruby", "qt4", "pyqt" ]
PyQt: No such slot
577,824
<p>I am starting to learn Qt4 and Python, following along some tutorial i found on the interwebs. I have the following two files:</p> <p>lcdrange.py:</p> <pre><code>from PyQt4 import QtGui, QtCore class LCDRange(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init...
1
2009-02-23T14:37:06Z
1,750,986
<p><br /> you forgot to put </p> <pre><code> @Qt.pyqtSlot() </code></pre> <p>above method you are using as slot. <br /></p> <p>For example your code should look like this</p> <pre><code> @Qt.pyqtSlot('const QPoint&') def setValue(self,value): self.slider.setValue(value) </code></pre> <p>Here is one good pa...
1
2009-11-17T18:55:25Z
[ "python", "ruby", "qt4", "pyqt" ]
How can I make this Python recursive function return a flat list?
577,940
<p>Look at this simple function</p> <pre><code>def prime_factors(n): for i in range(2,n): if n % i == 0: return i, prime_factors(n / i) return n </code></pre> <p>Here's the result of <code>prime_factors(120)</code></p> <pre><code>(2, (2, (2, (3, 5)))) </code></pre> <p>Instead of nested tuples,...
9
2009-02-23T15:12:37Z
577,955
<pre><code>def prime_factors(n): for i in range(2,n): if n % i == 0: yield i for p in prime_factors(n / i): yield p return yield n </code></pre> <p>Example:</p> <pre><code>&gt;&gt;&gt; tuple(prime_factors(100)) (2, 2, 5, 5) </code></pre>
10
2009-02-23T15:16:14Z
[ "python", "recursion" ]
How can I make this Python recursive function return a flat list?
577,940
<p>Look at this simple function</p> <pre><code>def prime_factors(n): for i in range(2,n): if n % i == 0: return i, prime_factors(n / i) return n </code></pre> <p>Here's the result of <code>prime_factors(120)</code></p> <pre><code>(2, (2, (2, (3, 5)))) </code></pre> <p>Instead of nested tuples,...
9
2009-02-23T15:12:37Z
577,961
<pre><code>def prime_factors(n): for i in range(2,n): if n % i == 0: return [i] + prime_factors(n / i) return [n] </code></pre>
18
2009-02-23T15:17:03Z
[ "python", "recursion" ]
How can I make this Python recursive function return a flat list?
577,940
<p>Look at this simple function</p> <pre><code>def prime_factors(n): for i in range(2,n): if n % i == 0: return i, prime_factors(n / i) return n </code></pre> <p>Here's the result of <code>prime_factors(120)</code></p> <pre><code>(2, (2, (2, (3, 5)))) </code></pre> <p>Instead of nested tuples,...
9
2009-02-23T15:12:37Z
577,971
<p>Without changing the original function, from <a href="http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks" rel="nofollow">Python Tricks</a>:</p> <pre><code>def flatten(x): """flatten(sequence) -&gt; list Returns a single, flat list which contains all elements retrieved from the sequence and ...
7
2009-02-23T15:19:23Z
[ "python", "recursion" ]
How can I make this Python recursive function return a flat list?
577,940
<p>Look at this simple function</p> <pre><code>def prime_factors(n): for i in range(2,n): if n % i == 0: return i, prime_factors(n / i) return n </code></pre> <p>Here's the result of <code>prime_factors(120)</code></p> <pre><code>(2, (2, (2, (3, 5)))) </code></pre> <p>Instead of nested tuples,...
9
2009-02-23T15:12:37Z
578,236
<p>liw.fi suggested in a <a href="http://stackoverflow.com/questions/577940/how-can-i-make-this-python-recursive-function-return-a-flat-list/577961#577961">comment</a>:</p> <blockquote> <p>Instead of creating a new list for each return value, you could pass the list as an argument and append to it. If the list ge...
2
2009-02-23T16:18:41Z
[ "python", "recursion" ]
Easiest way to create a scrollable area using wxPython?
578,200
<p>Okay, so I want to display a series of windows within windows and have the whole lot scrollable. I've been hunting through <a href="http://docs.wxwidgets.org/stable/wx_wxscrolledwindow.html#wxscrolledwindow" rel="nofollow">the wxWidgets documentation</a> and a load of examples from various sources on t'internet. Mos...
0
2009-02-23T16:12:36Z
580,006
<p>Oops.. turns out I was creating my child windows badly:</p> <pre><code>wind = MyCustomWindow(self) </code></pre> <p>should be:</p> <pre><code>wind = MyCustomWindow(self.scrolling_window) </code></pre> <p>..which meant the child windows were waiting for the top-level window (the frame) to be re-drawn instead of l...
1
2009-02-24T00:32:25Z
[ "python", "scroll", "wxwidgets", "scrolledwindow" ]
Python 2.5 to Python 2.2 converter
578,262
<p>I am working on a PyS60 application for S60 2nd Edition devices. I have coded my application logic in Python 2.5.</p> <p>Is there any tool that automates th conversion from Python 2.5 to Python 2.2 or do I need to do in manually?</p>
1
2009-02-23T16:24:09Z
578,337
<p>I don't know of any tool that would go from 2.5 to 2.2 automatically; but there was one a while ago that did 2.3 to 2.2 by <a href="http://www.radlogic.com.au/downloads.htm" rel="nofollow">RADLogic</a>.</p> <p>Depending on how many recent features your code uses, it may be trivial to convert it manually. </p> <p>I...
1
2009-02-23T16:42:25Z
[ "python", "pys60" ]
Python 2.5 to Python 2.2 converter
578,262
<p>I am working on a PyS60 application for S60 2nd Edition devices. I have coded my application logic in Python 2.5.</p> <p>Is there any tool that automates th conversion from Python 2.5 to Python 2.2 or do I need to do in manually?</p>
1
2009-02-23T16:24:09Z
578,966
<p>The latest Python for S60, <a href="http://discussion.forum.nokia.com/forum/showthread.php?t=154215" rel="nofollow">1.9.0</a>, actually includes Python 2.5.1. So maybe you don't need to convert.</p>
3
2009-02-23T19:23:54Z
[ "python", "pys60" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
578,393
<p>For one thing, I would suggest summing up the terms as you calculate them rather than storing them in an array and summing the array afterwards, since you don't need to do anything with the individual terms other than adding them up. (That's just good computational sense in any language)</p>
4
2009-02-23T16:56:56Z
[ "python" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
578,417
<p>I would make the following changes:</p> <ul> <li>Use iteration instead of recursion</li> <li>Just keep a running total instead of keeping a list of all Fibonacci numbers and then finding the sum of the even ones a posterior</li> </ul> <p>Other than that, it's <em>reasonably</em> Pythonic.</p> <pre><code>def even_...
2
2009-02-23T17:00:56Z
[ "python" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
578,424
<p>Using generators is a Pythonic way to generate long sequences while preserving memory:</p> <pre><code>def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b import itertools upto_4000000 = itertools.takewhile(lambda x: x &lt;= 4000000, fibonacci()) print(sum(x for x in upto_4000000 if x % 2 =...
16
2009-02-23T17:02:01Z
[ "python" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
578,426
<p>First I'd do fibo() as a generator:</p> <pre><code>def fibo(a=-1,b=1,upto=4000000): while a+b&lt;upto: a,b = b,a+b yield b </code></pre> <p>Then I'd also select for evenness as a generator rather than a list comprehension.</p> <pre><code>print sum(i for i in fibo() if not i%2) </code></pre>
12
2009-02-23T17:02:33Z
[ "python" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
578,491
<p>I'd use the fibonacci generator as in <a href="http://stackoverflow.com/questions/578379/python-program-to-find-fibonacci-series-more-pythonic-way/578424#578424">@constantin' answer</a> but generator expressions could be replaced by a plain <code>for</code> loop:</p> <pre><code>def fibonacci(a=0, b=1): while Tr...
2
2009-02-23T17:19:28Z
[ "python" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
3,277,816
<p>Here's an alternate direct method It relies on a few properties:</p> <ol> <li>Each Fibonacci number can be calculated directly as floor( pow( phi, n ) + 0.5 ) (See Computation by Rounding in <a href="http://en.wikipedia.org/wiki/Fibonacci_number" rel="nofollow">http://en.wikipedia.org/wiki/Fibonacci_number</a> ). C...
1
2010-07-19T00:37:09Z
[ "python" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
7,895,823
<p>In Python 3 at least if you give a generator to the <code>sum</code> function it will lazily evaluate it so there is no need to reinvent the wheel.</p> <p>This is what @Constantin did and is correct.</p> <p>Tested by comparing the memory usage of using generators:</p> <p><code>sum(range(9999999))</code></p> <p>c...
0
2011-10-25T21:00:26Z
[ "python" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
17,396,950
<pre><code>print ("Fibonacci Series\n") a = input ("Enter a nth Term: ") b = 0 x = 0 y = 1 print x,("\n"), y while b &lt;=a-2: b = b+1 z = x + y print z x = y y = z </code></pre>
0
2013-07-01T03:10:48Z
[ "python" ]
Python program to find fibonacci series. More Pythonic way
578,379
<p>There is another thread to discuss Fibo series in Python. This is to tweak code into more pythonic. <a href="http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python">How to write the Fibonacci Sequence in Python</a></p> <p>I am in love with this program I wrote to solve Project Euler...
6
2009-02-23T16:53:23Z
24,467,465
<pre><code>def main(): a = 1 b = 2 num = 2 while b &lt; 4000000: a, b = b, a+b num += b if b % 2 == 0 else 0 print num if __name__ == '__main__': main() </code></pre>
0
2014-06-28T13:49:48Z
[ "python" ]
How to pass values by ref in Python?
578,635
<p>Basically I am using the C++ API of an app, but there is no reference for its python access. One variable is passed by ref, like so:</p> <pre><code>GetPoint ( Point &amp;p, Object obj ) </code></pre> <p>so how can I translate to Python? Is there a pass by ref symbol?</p>
3
2009-02-23T17:58:20Z
578,645
<p>There is no pass by reference symbol in Python.</p> <p>Just modify the passed in point, your modifications will be visible from the calling function.</p> <pre><code>&gt;&gt;&gt; def change(obj): ... obj.x = 10 ... &gt;&gt;&gt; class Point(object): x,y = 0,0 ... &gt;&gt;&gt; p = Point() &gt;&gt;&gt; p.x 0 &gt;&...
4
2009-02-23T18:00:41Z
[ "python", "variables", "pass-by-reference" ]
How to pass values by ref in Python?
578,635
<p>Basically I am using the C++ API of an app, but there is no reference for its python access. One variable is passed by ref, like so:</p> <pre><code>GetPoint ( Point &amp;p, Object obj ) </code></pre> <p>so how can I translate to Python? Is there a pass by ref symbol?</p>
3
2009-02-23T17:58:20Z
578,647
<p>I'm pretty sure Python passes the value of the reference to a variable. <a href="http://rg03.wordpress.com/2007/04/21/semantics-of-python-variable-names-from-a-c-perspective/" rel="nofollow">This article</a> can probably explain it better than I.</p>
2
2009-02-23T18:01:43Z
[ "python", "variables", "pass-by-reference" ]
How to pass values by ref in Python?
578,635
<p>Basically I am using the C++ API of an app, but there is no reference for its python access. One variable is passed by ref, like so:</p> <pre><code>GetPoint ( Point &amp;p, Object obj ) </code></pre> <p>so how can I translate to Python? Is there a pass by ref symbol?</p>
3
2009-02-23T17:58:20Z
578,649
<p>Objects are always passed as reference in Python. So wrapping up in object produces similar effect.</p>
1
2009-02-23T18:02:10Z
[ "python", "variables", "pass-by-reference" ]
How to pass values by ref in Python?
578,635
<p>Basically I am using the C++ API of an app, but there is no reference for its python access. One variable is passed by ref, like so:</p> <pre><code>GetPoint ( Point &amp;p, Object obj ) </code></pre> <p>so how can I translate to Python? Is there a pass by ref symbol?</p>
3
2009-02-23T17:58:20Z
578,651
<p>It's likely that your Python bindings have turned that signature into:</p> <pre><code>Point GetPoint(Object obj) </code></pre> <p>or even:</p> <pre><code>Point Object::GetPoint() </code></pre> <p>So look into the bindings' documentation or sources.</p>
3
2009-02-23T18:02:48Z
[ "python", "variables", "pass-by-reference" ]
How to pass values by ref in Python?
578,635
<p>Basically I am using the C++ API of an app, but there is no reference for its python access. One variable is passed by ref, like so:</p> <pre><code>GetPoint ( Point &amp;p, Object obj ) </code></pre> <p>so how can I translate to Python? Is there a pass by ref symbol?</p>
3
2009-02-23T17:58:20Z
578,655
<p>There is not ref by symbol in python - the right thing to do depends on your API. The question to ask yourself is who owns the object passed from C++ to python. Sometimes, the easiest ting it just to copy the object into a python object, but that may not always be the best thing to do.</p> <p>You may be interested ...
1
2009-02-23T18:04:21Z
[ "python", "variables", "pass-by-reference" ]
Alternative to 'for i in xrange(len(x))'
578,677
<p>So I see in <a href="http://stackoverflow.com/questions/576988/python-specific-antipatterns">another post</a> the following "bad" snippet, but the only alternatives I have seen involve patching Python.</p> <pre><code>for i in xrange(len(something)): workwith = something[i] # do things with workwith... </code></...
9
2009-02-23T18:10:52Z
578,685
<p>See <a href="http://docs.python.org/glossary.html#term-pythonic">Pythonic</a></p> <pre><code>for workwith in something: # do things with workwith </code></pre>
22
2009-02-23T18:12:36Z
[ "python", "for-loop", "anti-patterns" ]
Alternative to 'for i in xrange(len(x))'
578,677
<p>So I see in <a href="http://stackoverflow.com/questions/576988/python-specific-antipatterns">another post</a> the following "bad" snippet, but the only alternatives I have seen involve patching Python.</p> <pre><code>for i in xrange(len(something)): workwith = something[i] # do things with workwith... </code></...
9
2009-02-23T18:10:52Z
578,689
<p>for example:</p> <pre><code>[workwith(i) for i in something] </code></pre>
0
2009-02-23T18:13:05Z
[ "python", "for-loop", "anti-patterns" ]
Alternative to 'for i in xrange(len(x))'
578,677
<p>So I see in <a href="http://stackoverflow.com/questions/576988/python-specific-antipatterns">another post</a> the following "bad" snippet, but the only alternatives I have seen involve patching Python.</p> <pre><code>for i in xrange(len(something)): workwith = something[i] # do things with workwith... </code></...
9
2009-02-23T18:10:52Z
578,692
<p>What is <code>x</code>? If its a sequence or iterator or string then </p> <pre><code>for i in x: workwith = i </code></pre> <p>will work fine.</p>
-3
2009-02-23T18:14:03Z
[ "python", "for-loop", "anti-patterns" ]
Alternative to 'for i in xrange(len(x))'
578,677
<p>So I see in <a href="http://stackoverflow.com/questions/576988/python-specific-antipatterns">another post</a> the following "bad" snippet, but the only alternatives I have seen involve patching Python.</p> <pre><code>for i in xrange(len(something)): workwith = something[i] # do things with workwith... </code></...
9
2009-02-23T18:10:52Z
578,694
<p>If you need to know the index in the loop body:</p> <pre><code>for index, workwith in enumerate(something): print "element", index, "is", workwith </code></pre>
23
2009-02-23T18:14:59Z
[ "python", "for-loop", "anti-patterns" ]
Alternative to 'for i in xrange(len(x))'
578,677
<p>So I see in <a href="http://stackoverflow.com/questions/576988/python-specific-antipatterns">another post</a> the following "bad" snippet, but the only alternatives I have seen involve patching Python.</p> <pre><code>for i in xrange(len(something)): workwith = something[i] # do things with workwith... </code></...
9
2009-02-23T18:10:52Z
582,541
<p>As there are <a href="http://stackoverflow.com/questions/578677/alternative-to-for-i-in-xrangelenx/578694#578694">two</a> <a href="http://stackoverflow.com/questions/578677/alternative-to-for-i-in-xrangelenx/578685#578685">answers</a> to question that are perfectly valid (with an assumption each) and author of the q...
11
2009-02-24T16:54:18Z
[ "python", "for-loop", "anti-patterns" ]