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
listlengths
1
5
Populating a list/array by index in Python?
869,778
<p>Is this possible:</p> <pre><code>myList = [] myList[12] = 'a' myList[22] = 'b' myList[32] = 'c' myList[42] = 'd' </code></pre> <p>When I try, I get:</p> <pre><code># IndexError: list assignment index out of range # </code></pre>
20
2009-05-15T16:57:25Z
869,819
<p>Not without populating the other locations in the list with something (like <code>None</code> or an empty string). Trying to insert an element into a list using the code you wrote would result in an <code>IndexError</code>.</p> <p>There's also <code>mylist.insert</code>, but this code:</p> <pre><code>myList.inser...
2
2009-05-15T17:03:32Z
[ "python", "list" ]
Populating a list/array by index in Python?
869,778
<p>Is this possible:</p> <pre><code>myList = [] myList[12] = 'a' myList[22] = 'b' myList[32] = 'c' myList[42] = 'd' </code></pre> <p>When I try, I get:</p> <pre><code># IndexError: list assignment index out of range # </code></pre>
20
2009-05-15T16:57:25Z
869,894
<p>If you don't know the size of the list ahead of time, you could use try/except and then Extend the list in the except:</p> <pre><code>L = [] def add(i, s): try: L[i] = s except IndexError: L.extend([None]*(i-len(L)+1)) L[i] = s add(12, 'a') add(22, 'b') </code></pre> <p><strong>---...
2
2009-05-15T17:19:36Z
[ "python", "list" ]
Populating a list/array by index in Python?
869,778
<p>Is this possible:</p> <pre><code>myList = [] myList[12] = 'a' myList[22] = 'b' myList[32] = 'c' myList[42] = 'd' </code></pre> <p>When I try, I get:</p> <pre><code># IndexError: list assignment index out of range # </code></pre>
20
2009-05-15T16:57:25Z
869,901
<p>Here's a quick list wrapper that will auto-expand your list with zeros if you attempt to assign a value to a index past it's length.</p> <pre><code>class defaultlist(list): def __setitem__(self, index, value): size = len(self) if index &gt;= size: self.extend(0 for _ in range(size, index + ...
13
2009-05-15T17:20:46Z
[ "python", "list" ]
Populating a list/array by index in Python?
869,778
<p>Is this possible:</p> <pre><code>myList = [] myList[12] = 'a' myList[22] = 'b' myList[32] = 'c' myList[42] = 'd' </code></pre> <p>When I try, I get:</p> <pre><code># IndexError: list assignment index out of range # </code></pre>
20
2009-05-15T16:57:25Z
20,031,992
<p>Just in case someone needs, I figured out a soluction for my problem, I needed to <strong>calc a lot of factorials</strong>, some of them <strong>could be repeated</strong>, so here is my solution:</p> <pre><code>factorials = {} def calcFact(v): try: return factorials[v] except KeyError: fa...
1
2013-11-17T14:42:52Z
[ "python", "list" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
869,902
<p>Try <code>range(100,-1,-1)</code>, the 3rd argument being the increment to use (documented <a href="https://docs.python.org/library/functions.html#range">here</a>).</p>
146
2009-05-15T17:20:46Z
[ "python", "loops" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
869,905
<pre><code>for i in range(100, -1, -1) </code></pre> <p>and some slightly longer (and slower) solution:</p> <pre><code>for i in reversed(range(101)) for i in range(101)[::-1] </code></pre>
13
2009-05-15T17:21:39Z
[ "python", "loops" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
869,914
<p>In my opinion, this is the most readable:</p> <pre><code>for i in reversed(xrange(101)): print i, </code></pre>
103
2009-05-15T17:24:09Z
[ "python", "loops" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
1,750,432
<p>Generally in Python, you can use negative indices to start from the back:</p> <pre><code>numbers = [10, 20, 30, 40, 50] for i in xrange(len(numbers)): print numbers[-i - 1] </code></pre> <p>Result:</p> <pre><code>50 40 30 20 10 </code></pre>
8
2009-11-17T17:26:02Z
[ "python", "loops" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
6,807,504
<p>Another solution:</p> <pre><code>z = 10 for x in range (z): y = z-x print y </code></pre> <p>Result:</p> <pre><code>10 9 8 7 6 5 4 3 2 1 </code></pre> <p>Tip: If you are using this method to count back indices in a list, you will want to -1 from the 'y' value, as your list indices will begin at 0.</p>
3
2011-07-24T14:48:06Z
[ "python", "loops" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
24,374,584
<p><code>for var in range(10,-1,-1)</code> works</p>
1
2014-06-23T20:15:58Z
[ "python", "loops" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
24,617,809
<p>I tried this in one of the codeacademy exercises (reversing chars in a string without using reversed nor :: -1)</p> <pre><code>def reverse(text): chars= [] l = len(text) last = l-1 for i in range (l): chars.append(text[last]) last-=1 result= "" for c in chars: res...
0
2014-07-07T18:55:05Z
[ "python", "loops" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
30,311,352
<p>Short and sweet. This was my solution when doing codeAcademy course. Prints a string in rev order. </p> <pre><code>def reverse(text): string = "" for i in range(len(text)-1,-1,-1): string += text[i] return string </code></pre>
2
2015-05-18T19:27:33Z
[ "python", "loops" ]
Loop backwards using indices in Python?
869,885
<p>I am trying to loop from 100 to 0. How do I do this in Python?</p> <p><code>for i in range (100,0)</code> doesn't work.</p>
92
2009-05-15T17:17:06Z
32,716,115
<pre><code>a = 10 for i in sorted(range(a), reverse=True): print i </code></pre>
0
2015-09-22T11:50:33Z
[ "python", "loops" ]
BWSplitView and PyObjc
869,912
<p>I'm trying to use Brandon Walkin's BWSplitView from BWToolkit in a Cocoa PyObjc project. When I run the project I get the following error message:</p> <pre><code>NSInvalidUnarchiveOperationException - *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (BWSplitView) </code></pre> <p>Does th...
3
2009-05-15T17:24:04Z
869,993
<p>I've fixed this using the following steps:<br /> 1. Download and install <a href="http://github.com/jrydberg/pyobjc-bwtoolkitframework/tree/master" rel="nofollow">http://github.com/jrydberg/pyobjc-bwtoolkitframework/tree/master</a><br /> 2. Ensure you have BWToolkit.framework installed in /System/Library/Framework...
0
2009-05-15T17:42:51Z
[ "python", "cocoa", "pyobjc", "bwtoolkit" ]
BWSplitView and PyObjc
869,912
<p>I'm trying to use Brandon Walkin's BWSplitView from BWToolkit in a Cocoa PyObjc project. When I run the project I get the following error message:</p> <pre><code>NSInvalidUnarchiveOperationException - *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (BWSplitView) </code></pre> <p>Does th...
3
2009-05-15T17:24:04Z
870,039
<p>I suspect that you got that error because you had a BWSplitView in a nib/xib file that you were attempting to load. In order to unarchive the objects in a nib file, the runtime needs to be able to create instances of the archived classes (e.g. BWSplitView). The exception that's being thrown is because BWSplitView is...
5
2009-05-15T17:51:02Z
[ "python", "cocoa", "pyobjc", "bwtoolkit" ]
BWSplitView and PyObjc
869,912
<p>I'm trying to use Brandon Walkin's BWSplitView from BWToolkit in a Cocoa PyObjc project. When I run the project I get the following error message:</p> <pre><code>NSInvalidUnarchiveOperationException - *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (BWSplitView) </code></pre> <p>Does th...
3
2009-05-15T17:24:04Z
1,768,125
<p>We had a similar message:</p> <pre><code>*** Terminating app due to uncaught exception ‘NSInvalidUnarchiveOperationException’, reason: ‘*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (MKMapView)’ </code></pre> <p>In our case it was due to a stray entry in the "Framework Search ...
0
2009-11-20T03:20:31Z
[ "python", "cocoa", "pyobjc", "bwtoolkit" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
870,003
<p>In the Idle editor, you can just select the lines you want to indent and hit Tab.</p> <p>I should note that this doesn't actually insert any tabs into your source, just spaces.</p>
3
2009-05-15T17:43:58Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
870,011
<p>In IDLE I just use ctrl+] and ctrl+[ on a block of code.</p>
2
2009-05-15T17:46:27Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
870,030
<p>Use VI and never program the same again. :^)</p>
1
2009-05-15T17:49:46Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
870,038
<p>In TextMate, just highlight the lines you want to indent and use:</p> <p><br />⌘ + [ <br />or <br />⌘ + ] </p> <p>To move the text in the appropriate direction.</p>
0
2009-05-15T17:50:59Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
870,910
<p>PyDev, which you can find at <a href="http://pydev.sourceforge.net/" rel="nofollow">http://pydev.sourceforge.net/</a> has a "Code Formatter". It also has autoindent feature. It is a plugin for Eclipse which is freely available for Mac too.</p> <p>Another option would be <a href="http://code.google.com/p/macvim/" re...
0
2009-05-15T21:15:21Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
871,192
<p>[Funny ;-)] Dude, I told you that you would need one developer less if you had this new keyboard model <img src="http://img22.imageshack.us/img22/7318/pythonkeyboard.jpg" alt="Pythonic keyboard" /></p>
1
2009-05-15T22:53:41Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
871,231
<p>With emacs there's Python mode. In that mode you highlight and do:</p> <pre><code>ctrl-c &gt; ctrl-c &lt; </code></pre>
2
2009-05-15T23:10:04Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
871,269
<p>Vim: switch to visual mode, select the block, use > to indent (or &lt; to unindent).</p> <p>See also: <a href="http://stackoverflow.com/questions/235839/how-do-i-indent-multiple-lines-quickly-in-vi">http://stackoverflow.com/questions/235839/how-do-i-indent-multiple-lines-quickly-in-vi</a></p>
1
2009-05-15T23:35:32Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
871,294
<p>If you are using vim there is a plugin specifically for this: <a href="http://vim.sourceforge.net/scripts/script.php?script%5Fid=30" rel="nofollow">Python_fn.vim</a> </p> <p>It provides useful python functions (and menu equivalents):</p> <pre><code>]t -- Jump to beginning of block ]e -- Jump to end of bl...
1
2009-05-15T23:46:59Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
871,329
<p>I don't know what wacky planets everyone is coming from, but in most editors that don't date back to the stone age, indenting blocks of code typically only requires that a block of text be selected and Tab be pressed. On the flip side, Shift+Tab usually UNdents the block.</p> <p>This is true for Visual Studio, Not...
5
2009-05-16T00:18:29Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
871,482
<p>In Komodo the Tab and Shift Tab both work as expected to indent and unindent large blocks of code.</p>
0
2009-05-16T01:52:52Z
[ "python", "user-interface", "indentation" ]
Indentation in python GUI
869,975
<p>As i write code in python and suddenly feel like adding a new block in front of the code i have already written.... the indentation of the complete code is affected.. it is very tedious process to move to each line and change the indentation...is there any way to do auto indent or something...</p> <p>eg:</p> <pre>...
2
2009-05-15T17:39:27Z
871,562
<p>In vim, you can enter:</p> <p><code>&gt;&gt;</code></p> <p>to indent a line. If you enter:</p> <p><code>5&gt;&gt;</code></p> <p>you indent the 5 lines at and below the cursor. <code>5&lt;&lt;</code> does the reverse.</p>
0
2009-05-16T02:54:09Z
[ "python", "user-interface", "indentation" ]
Help translation PYTHON to VB.NET
870,116
<p>I am coding an application in VB.NET that sends sms.</p> <p>Would you please post <strong>PYTHON->VB.NET</strong> translation of this code and/or guidelines?</p> <p>Thanks in advance!!!</p> <pre><code>import threading class MessageThread(threading.Thread): def __init__(self,msg,no): threading.Thread.__i...
0
2009-05-15T18:13:27Z
870,175
<p>This is IronPython-code ("Python for .NET"), hence the source code uses the .NET-Framework just as VB and all classes (even <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx" rel="nofollow">System.Threading.Thread</a>) can be used in the same way shown.</p> <p>Some tips:</p> <p><code>Me...
0
2009-05-15T18:31:12Z
[ "python", "vb.net", "multithreading", "translation" ]
Help translation PYTHON to VB.NET
870,116
<p>I am coding an application in VB.NET that sends sms.</p> <p>Would you please post <strong>PYTHON->VB.NET</strong> translation of this code and/or guidelines?</p> <p>Thanks in advance!!!</p> <pre><code>import threading class MessageThread(threading.Thread): def __init__(self,msg,no): threading.Thread.__i...
0
2009-05-15T18:13:27Z
870,303
<p>This code creates a thread for every msg/no tuple and calls sendmsg. The first "for each ... each.start()" starts the thread (which only calls sendmsg) and the second "for each ... each.join()" waits for each thread to complete. Depending on the number of records, this could create a significant number of threads (w...
1
2009-05-15T19:00:01Z
[ "python", "vb.net", "multithreading", "translation" ]
Scheduling a JasperServer Report via SOAP using Python
870,188
<p>I was able to figure out how to run reports, download files, list folders, etc. on a JasperServer using Python with SOAPpy and xml.dom minidom.</p> <p>Here's an example execute report request, which works:</p> <pre><code>repositoryURL = 'http://user@pass:myjasperserver:8080/jasperserver/services/repository' reposi...
6
2009-05-15T18:36:02Z
1,898,176
<p>I've had a lot of bad experiences with minidom. I recommend you use <a href="http://codespeak.net/lxml/" rel="nofollow">lxml</a>. I haven't had any experience with soap itself, so I can't speak to the rest of the issue. </p>
1
2009-12-13T23:22:34Z
[ "python", "soap", "jasper-reports" ]
Scheduling a JasperServer Report via SOAP using Python
870,188
<p>I was able to figure out how to run reports, download files, list folders, etc. on a JasperServer using Python with SOAPpy and xml.dom minidom.</p> <p>Here's an example execute report request, which works:</p> <pre><code>repositoryURL = 'http://user@pass:myjasperserver:8080/jasperserver/services/repository' reposi...
6
2009-05-15T18:36:02Z
2,450,435
<p>Without knowing anything at all about Jasper, I can guarantee you that you'll do better to replace your hardcoded SOAP requests with a simple client based on <a href="https://fedorahosted.org/suds/" rel="nofollow">the excellent suds library</a>. It abstracts away the SOAP and leaves you with squeaky-clean API acces...
1
2010-03-15T20:58:53Z
[ "python", "soap", "jasper-reports" ]
AuthSub with Text_db in google app engine
870,192
<p>I am trying to read a spreadsheet from app engine using text_db and authsub.</p> <p>I read <a href="http://code.google.com/appengine/articles/gdata.html" rel="nofollow">http://code.google.com/appengine/articles/gdata.html</a> and got it to work. Then I read <a href="http://code.google.com/p/gdata-python-client/wiki...
1
2009-05-15T18:36:34Z
870,343
<p>As always, I figure out the answer only after giving up and asking for help.</p> <p>we need to add two more calls to run_on_appengine (to register the two clients that the text_db client has):</p> <pre><code>gdata.alt.appengine.run_on_appengine(client) gdata.alt.appengine.run_on_appengine(client._GetDocsClient()) ...
1
2009-05-15T19:10:42Z
[ "python", "google-app-engine", "authentication", "gdata-api" ]
SOAPpy - reserved word in named parameter list
870,455
<p>I'm using SOAPpy to access a SOAP Webservice. This call to the function findPathwaysByText works just fine:</p> <pre><code>server.findPathwaysByText (query= 'WP619', species = 'Mus musculus') </code></pre> <p>However, this call to the function login does not:</p> <pre><code>server.login (user='amarillion', pass='...
3
2009-05-15T19:37:54Z
870,473
<p>You can say</p> <pre><code>server.login(user='amarillion', **{'pass': '*****'}) </code></pre> <p>The double-asterix syntax here applies keyword arguments. Here's a simple example that shows what's happening:</p> <pre><code>def f(a, b): return a + b kwargs = {"a": 5, "b": 6} return f(**kwargs) # same ...
1
2009-05-15T19:42:09Z
[ "python", "soap", "soappy", "reserved-words" ]
SOAPpy - reserved word in named parameter list
870,455
<p>I'm using SOAPpy to access a SOAP Webservice. This call to the function findPathwaysByText works just fine:</p> <pre><code>server.findPathwaysByText (query= 'WP619', species = 'Mus musculus') </code></pre> <p>However, this call to the function login does not:</p> <pre><code>server.login (user='amarillion', pass='...
3
2009-05-15T19:37:54Z
870,482
<p>You could try:</p> <pre><code>d = {'user':'amarillion', 'pass':'*****' } server.login(**d) </code></pre> <p>This passes in the given dictionary as though they were keyword arguments (the **)</p>
5
2009-05-15T19:43:14Z
[ "python", "soap", "soappy", "reserved-words" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
870,558
<pre><code>s = 'ASDjifjASFJ7364' s_lowercase = ''.join(filter(lambda c: c.islower(), s)) print s_lowercase #print 'jifj' </code></pre>
4
2009-05-15T19:53:32Z
[ "python", "filter", "whitelist" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
870,559
<pre><code>s = 'Agh#$%#%2341- -!zdrkfd' print ''.join(c for c in s if c.islower()) </code></pre> <p>String objects are iterable; there is no need to "explode" the string into a list. You can put whatever condition you want in the list comprehension, and it will filter characters accordingly.</p> <p>You could also i...
17
2009-05-15T19:53:36Z
[ "python", "filter", "whitelist" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
870,565
<p>I'd use a regex. For lowercase match [a-z].</p>
1
2009-05-15T19:54:27Z
[ "python", "filter", "whitelist" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
870,582
<pre><code>&gt;&gt;&gt; s = 'Agh#$%#%2341- -!zdrkfd' &gt;&gt;&gt; ''.join(i for i in s if i in 'qwertyuiopasdfghjklzxcvbnm') 'ghzdrkfd' </code></pre>
4
2009-05-15T19:57:34Z
[ "python", "filter", "whitelist" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
870,588
<p>Using a regular expression is easy enough, especially for this scenario:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; s = 'ASDjifjASFJ7364' &gt;&gt;&gt; re.sub(r'[^a-z]+', '', s) 'jifj' </code></pre> <p>If you plan on doing this many times, it is best to compile the regular expression before hand:</p> <pre>...
5
2009-05-15T19:58:27Z
[ "python", "filter", "whitelist" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
870,624
<pre><code>import string print "".join([c for c in "Agh#$%#%2341- -!zdrkfd" if c in string.lowercase]) </code></pre>
0
2009-05-15T20:04:29Z
[ "python", "filter", "whitelist" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
870,748
<p>If you are looking for efficiency. Using the <a href="http://docs.python.org/library/stdtypes.html#str.translate">translate</a> function is the fastest you can get.</p> <p>It can be used to quickly replace characters and/or delete them.</p> <pre><code>import string delete_table = string.maketrans( string.asci...
30
2009-05-15T20:35:58Z
[ "python", "filter", "whitelist" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
870,847
<p>Here's one solution if you are specifically interested in working on strings:</p> <pre><code> s = 'Agh#$%#%2341- -!zdrkfd' lowercase_chars = [chr(i) for i in xrange(ord('a'), ord('z') + 1)] whitelist = set(lowercase_chars) filtered_list = [c for c in s if c in whitelist] </code></pre> <p>The whitelist is actual...
1
2009-05-15T20:59:53Z
[ "python", "filter", "whitelist" ]
In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?
870,520
<p>Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'.</p> <p>How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through ...
20
2009-05-15T19:48:02Z
29,925,445
<pre><code>import string print filter(string.lowercase.__contains__, "lowerUPPER") print filter("123".__contains__, "a1b2c3") </code></pre>
0
2015-04-28T16:54:47Z
[ "python", "filter", "whitelist" ]
Pythonic way to split comma separated numbers into pairs
870,652
<p>I'd like to split a comma separated value into pairs:</p> <pre><code>&gt;&gt;&gt; s = '0,1,2,3,4,5,6,7,8,9' &gt;&gt;&gt; pairs = # something pythonic &gt;&gt;&gt; pairs [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] </code></pre> <p>What would <em># something pythonic</em> look like?</p> <p>How would you detect and han...
17
2009-05-15T20:10:32Z
870,677
<p>Something like:</p> <pre><code>zip(t[::2], t[1::2]) </code></pre> <p>Full example:</p> <pre><code>&gt;&gt;&gt; s = ','.join(str(i) for i in range(10)) &gt;&gt;&gt; s '0,1,2,3,4,5,6,7,8,9' &gt;&gt;&gt; t = [int(i) for i in s.split(',')] &gt;&gt;&gt; t [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] &gt;&gt;&gt; p = zip(t[::2], t[1...
44
2009-05-15T20:15:55Z
[ "python", "tuples" ]
Pythonic way to split comma separated numbers into pairs
870,652
<p>I'd like to split a comma separated value into pairs:</p> <pre><code>&gt;&gt;&gt; s = '0,1,2,3,4,5,6,7,8,9' &gt;&gt;&gt; pairs = # something pythonic &gt;&gt;&gt; pairs [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] </code></pre> <p>What would <em># something pythonic</em> look like?</p> <p>How would you detect and han...
17
2009-05-15T20:10:32Z
870,682
<p>How about this:</p> <pre><code>&gt;&gt;&gt; x = '0,1,2,3,4,5,6,7,8,9'.split(',') &gt;&gt;&gt; def chunker(seq, size): ... return (tuple(seq[pos:pos + size]) for pos in xrange(0, len(seq), size)) ... &gt;&gt;&gt; list(chunker(x, 2)) [('0', '1'), ('2', '3'), ('4', '5'), ('6', '7'), ('8', '9')] </code></pre> <p>T...
8
2009-05-15T20:16:32Z
[ "python", "tuples" ]
Pythonic way to split comma separated numbers into pairs
870,652
<p>I'd like to split a comma separated value into pairs:</p> <pre><code>&gt;&gt;&gt; s = '0,1,2,3,4,5,6,7,8,9' &gt;&gt;&gt; pairs = # something pythonic &gt;&gt;&gt; pairs [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] </code></pre> <p>What would <em># something pythonic</em> look like?</p> <p>How would you detect and han...
17
2009-05-15T20:10:32Z
870,692
<p>This will ignore the last number in an odd list:</p> <pre><code>n = [int(x) for x in s.split(',')] print zip(n[::2], n[1::2]) </code></pre> <p>This will pad the shorter list by 0 in an odd list:</p> <pre><code>import itertools n = [int(x) for x in s.split(',')] print list(itertools.izip_longest(n[::2], n[1::2], f...
2
2009-05-15T20:20:54Z
[ "python", "tuples" ]
Pythonic way to split comma separated numbers into pairs
870,652
<p>I'd like to split a comma separated value into pairs:</p> <pre><code>&gt;&gt;&gt; s = '0,1,2,3,4,5,6,7,8,9' &gt;&gt;&gt; pairs = # something pythonic &gt;&gt;&gt; pairs [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] </code></pre> <p>What would <em># something pythonic</em> look like?</p> <p>How would you detect and han...
17
2009-05-15T20:10:32Z
870,724
<p>A more general option, that also works on iterators and allows for combining any number of items:</p> <pre><code> def n_wise(seq, n): return zip(*([iter(seq)]*n)) </code></pre> <p>Replace zip with itertools.izip if you want to get a lazy iterator instead of a list.</p>
8
2009-05-15T20:29:28Z
[ "python", "tuples" ]
Pythonic way to split comma separated numbers into pairs
870,652
<p>I'd like to split a comma separated value into pairs:</p> <pre><code>&gt;&gt;&gt; s = '0,1,2,3,4,5,6,7,8,9' &gt;&gt;&gt; pairs = # something pythonic &gt;&gt;&gt; pairs [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] </code></pre> <p>What would <em># something pythonic</em> look like?</p> <p>How would you detect and han...
17
2009-05-15T20:10:32Z
1,429,822
<p>A solution much like FogleBirds, but using an iterator (a generator expression) instead of list comprehension.</p> <pre><code>s = '0,1,2,3,4,5,6,7,8,9' # generator expression creating an iterator yielding numbers iterator = (int(i) for i in s.split(',')) # use zip to create pairs # (will ignore last item if odd nu...
4
2009-09-15T21:53:29Z
[ "python", "tuples" ]
How to generate a file with DDL in the engine's SQL dialect in SQLAlchemy?
870,925
<p>Suppose I have an <code>engine</code> pointing at MySQL database:</p> <pre><code>engine = create_engine('mysql://arthurdent:answer42@localhost/dtdb', echo=True) </code></pre> <p>I can populate <code>dtdb</code> with tables, FKs, etc by:</p> <pre><code>metadata.create_all(engine) </code></pre> <p>Is there an easy...
7
2009-05-15T21:19:13Z
870,958
<p>The quick answer is in the <a href="http://docs.sqlalchemy.org/en/rel_0_8/faq.html#how-can-i-get-the-create-table-drop-table-output-as-a-string">SQLAlchemy 0.8 FAQ</a>.</p> <p>In SQLAlchemy 0.8 you need to do</p> <pre><code>engine = create_engine( 'mssql+pyodbc://./MyDb', strategy='mock', executor= lambda sql, *mu...
13
2009-05-15T21:33:37Z
[ "python", "sqlalchemy" ]
Django: Overriding __init__ for Custom Forms
871,037
<p>I am making a custom form object in Django which has an overrided __init__ method. The purpose of overriding the method is to dynamically generate drop-down boxes based on the new parameters.</p> <p>For example,</p> <pre><code>class TicketForm(forms.Form): Type = Type.GetTicketTypeField() def __init__(sel...
14
2009-05-15T22:00:04Z
871,082
<p>You can dynamically modify your form by using the <code>self.fields</code> dict. Something like this may work for you:</p> <pre><code>class TicketForm(forms.Form): Type = Type.GetTicketTypeField() def __init__(self, ticket, *args, **kwargs): super(TicketForm, self).__init__(*args, **kwargs) self.field...
25
2009-05-15T22:13:03Z
[ "python", "django" ]
Django: Overriding __init__ for Custom Forms
871,037
<p>I am making a custom form object in Django which has an overrided __init__ method. The purpose of overriding the method is to dynamically generate drop-down boxes based on the new parameters.</p> <p>For example,</p> <pre><code>class TicketForm(forms.Form): Type = Type.GetTicketTypeField() def __init__(sel...
14
2009-05-15T22:00:04Z
871,084
<p>I found a solution <a href="http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/" rel="nofollow">here</a>. If there is a better solution, please post a reply.</p> <pre><code>class TicketForm(forms.Form): Type = Type.GetTicketTypeField() def __init__(self, ticket=None, *args, **kwargs): super(...
2
2009-05-15T22:14:05Z
[ "python", "django" ]
escape problem in django templates
871,163
<p>Let's say that I have this string:</p> <pre><code>s = '&lt;p&gt;Hello!&lt;/p&gt;' </code></pre> <p>When I pass this variable to a template, I want it to be rendered as raw html. Looking at the docs I see that I can either use the safe filter:</p> <pre><code>{{s|safe}} </code></pre> <p>or disable autoescape:</p> ...
8
2009-05-15T22:43:45Z
871,197
<p>You pretty much covered it, those are indeed all the ways to disable autoescaping.</p> <p>Are you sure the value you are talking about is actually <code>s = '&lt;p&gt;Hello!&lt;/p&gt;'</code>?</p> <p>My hunch is that you have additional escaping somewhere in that string...</p>
2
2009-05-15T22:56:45Z
[ "python", "html", "django", "django-templates", "rendering" ]
escape problem in django templates
871,163
<p>Let's say that I have this string:</p> <pre><code>s = '&lt;p&gt;Hello!&lt;/p&gt;' </code></pre> <p>When I pass this variable to a template, I want it to be rendered as raw html. Looking at the docs I see that I can either use the safe filter:</p> <pre><code>{{s|safe}} </code></pre> <p>or disable autoescape:</p> ...
8
2009-05-15T22:43:45Z
2,109,545
<p>Look at the HTML source code, is it escaped!??? I don't think so. It should be printing letter by letter, just like this:</p> <pre><code>&lt; p &gt; H E L L O &lt; / &gt; </code></pre>
-3
2010-01-21T13:37:43Z
[ "python", "html", "django", "django-templates", "rendering" ]
escape problem in django templates
871,163
<p>Let's say that I have this string:</p> <pre><code>s = '&lt;p&gt;Hello!&lt;/p&gt;' </code></pre> <p>When I pass this variable to a template, I want it to be rendered as raw html. Looking at the docs I see that I can either use the safe filter:</p> <pre><code>{{s|safe}} </code></pre> <p>or disable autoescape:</p> ...
8
2009-05-15T22:43:45Z
4,405,279
<p>I think you should write as follows</p> <pre><code>{{s|escape|safe}} </code></pre> <p>it is ok for me</p>
5
2010-12-10T03:04:01Z
[ "python", "html", "django", "django-templates", "rendering" ]
XPath - How can I query for a parent node satisfying an attribute presence condition?
871,188
<p>I need to query a node to determine if it has a parent node that contains a specified attribute. For instance:</p> <pre><code>&lt;a b="value"&gt; &lt;b/&gt; &lt;/a&gt; </code></pre> <p>From b as my focus element, I'd like to execute an XPath query:</p> <pre><code>..[@b] </code></pre> <p>that would return ele...
1
2009-05-15T22:52:42Z
871,209
<p>I don't know about the lxml.etree library but <code>..[@b]</code> is <strike>fully valid XPath</strike> (<strong>Update</strong>: see Ben Blank's comment). Identical to <code>parent::a[@b]</code>, it will return context at the <code>a</code> element.</p>
1
2009-05-15T23:00:38Z
[ "python", "xml", "xpath" ]
XPath - How can I query for a parent node satisfying an attribute presence condition?
871,188
<p>I need to query a node to determine if it has a parent node that contains a specified attribute. For instance:</p> <pre><code>&lt;a b="value"&gt; &lt;b/&gt; &lt;/a&gt; </code></pre> <p>From b as my focus element, I'd like to execute an XPath query:</p> <pre><code>..[@b] </code></pre> <p>that would return ele...
1
2009-05-15T22:52:42Z
871,222
<p>You can't combine the <code>.</code> or <code>..</code> shorthands with a predicate. Instead, you'll need to use the full <code>parent::</code> axis. The following should work for you:</p> <pre><code>parent::*[@b] </code></pre> <p>This will select the parent node (regardless of its local name), IFF it has a "b" ...
4
2009-05-15T23:05:54Z
[ "python", "xml", "xpath" ]
Python program using os.pipe and os.fork() issue
871,447
<p>I've recently needed to write a script that performs an <strong>os.fork()</strong> to split into two processes. The child process becomes a server process and passes data back to the parent process using a pipe created with <strong>os.pipe()</strong>. The child closes the <code>'r'</code> end of the pipe and the par...
10
2009-05-16T01:36:53Z
871,503
<p>The "parent" vs. "child" part of fork in a Python application is silly. It's a legacy from 16-bit unix days. It's an affectation from a day when fork/exec and exec were Important Things to make the most of a tiny little processor.</p> <p>Break your Python code into two separate parts: parent and child.</p> <p>Th...
-8
2009-05-16T02:05:01Z
[ "python", "pipe", "fork" ]
Python program using os.pipe and os.fork() issue
871,447
<p>I've recently needed to write a script that performs an <strong>os.fork()</strong> to split into two processes. The child process becomes a server process and passes data back to the parent process using a pipe created with <strong>os.pipe()</strong>. The child closes the <code>'r'</code> end of the pipe and the par...
10
2009-05-16T01:36:53Z
871,509
<p><a href="http://www.myelin.co.nz/post/2003/3/13/" rel="nofollow">Here's</a> some example code for doing just this.</p>
2
2009-05-16T02:09:02Z
[ "python", "pipe", "fork" ]
Python program using os.pipe and os.fork() issue
871,447
<p>I've recently needed to write a script that performs an <strong>os.fork()</strong> to split into two processes. The child process becomes a server process and passes data back to the parent process using a pipe created with <strong>os.pipe()</strong>. The child closes the <code>'r'</code> end of the pipe and the par...
10
2009-05-16T01:36:53Z
871,515
<p>Using </p> <p><code>fcntl.fcntl(readPipe, fcntl.F_SETFL, os.O_NONBLOCK)</code></p> <p>Before invoking the read() solved both problems. The read() call is no longer blocking and the data is appearing after just a flush() on the writing end.</p>
5
2009-05-16T02:14:38Z
[ "python", "pipe", "fork" ]
Python program using os.pipe and os.fork() issue
871,447
<p>I've recently needed to write a script that performs an <strong>os.fork()</strong> to split into two processes. The child process becomes a server process and passes data back to the parent process using a pipe created with <strong>os.pipe()</strong>. The child closes the <code>'r'</code> end of the pipe and the par...
10
2009-05-16T01:36:53Z
871,565
<p>I see you have solved the problem of blocking i/o and buffering.</p> <p>A note if you decide to try a different approach: subprocess is the equivalent / a replacement for the fork/exec idiom. It seems like that's not what you're doing: you have just a fork (not an exec) and exchanging data between the two processes...
4
2009-05-16T02:56:06Z
[ "python", "pipe", "fork" ]
Python program using os.pipe and os.fork() issue
871,447
<p>I've recently needed to write a script that performs an <strong>os.fork()</strong> to split into two processes. The child process becomes a server process and passes data back to the parent process using a pipe created with <strong>os.pipe()</strong>. The child closes the <code>'r'</code> end of the pipe and the par...
10
2009-05-16T01:36:53Z
872,637
<p>Are you using read() without specifying a size, or treating the pipe as an iterator (<code>for line in f</code>)? If so, that's probably the source of your problem - read() is defined to read until the end of the file before returning, rather than just read what is available for reading. That will mean it will blo...
10
2009-05-16T15:30:19Z
[ "python", "pipe", "fork" ]
Simple List of All Java Standard Classes and Methods?
871,812
<p>I'm building a very simple Java parser, to look for some specific usage models. This is in no way lex/yacc or any other form of interpreter/compiler for puposes of running the code. </p> <p>When I encounter a word or a set of two words separated by a dot ("word.word"), I would like to know if that's a standard Java...
6
2009-05-16T05:58:52Z
871,818
<p>What's wrong with the <a href="http://java.sun.com/javase/6/docs/api/allclasses-frame.html" rel="nofollow">javadoc</a>? The <a href="http://java.sun.com/javase/6/docs/api/allclasses-frame.html" rel="nofollow">index</a> lists all classes, methods, and static variables. You can probably grep for parenthesis.</p>
1
2009-05-16T06:03:45Z
[ "java", "python", "parsing" ]
Simple List of All Java Standard Classes and Methods?
871,812
<p>I'm building a very simple Java parser, to look for some specific usage models. This is in no way lex/yacc or any other form of interpreter/compiler for puposes of running the code. </p> <p>When I encounter a word or a set of two words separated by a dot ("word.word"), I would like to know if that's a standard Java...
6
2009-05-16T05:58:52Z
871,820
<p>To get all classes and methods you can look at the index on <a href="http://java.sun.com/javase/6/docs/api/index-files/index-1.html" rel="nofollow">http://java.sun.com/javase/6/docs/api/index-files/index-1.html</a></p> <p>This will be 10's of thousands classes and method which can be overwhelming.</p> <p>I suggest...
1
2009-05-16T06:06:25Z
[ "java", "python", "parsing" ]
Simple List of All Java Standard Classes and Methods?
871,812
<p>I'm building a very simple Java parser, to look for some specific usage models. This is in no way lex/yacc or any other form of interpreter/compiler for puposes of running the code. </p> <p>When I encounter a word or a set of two words separated by a dot ("word.word"), I would like to know if that's a standard Java...
6
2009-05-16T05:58:52Z
871,879
<blockquote> <p>When I encounter a word or a set of two words separated by a dot ("word.word"), I would like to know if that's a standard Java class (and method), e.g. "Integer", or some user defined name.</p> </blockquote> <p>If thats what you're after, you could do without a (limited) list of Java Classes by using...
0
2009-05-16T07:16:18Z
[ "java", "python", "parsing" ]
Simple List of All Java Standard Classes and Methods?
871,812
<p>I'm building a very simple Java parser, to look for some specific usage models. This is in no way lex/yacc or any other form of interpreter/compiler for puposes of running the code. </p> <p>When I encounter a word or a set of two words separated by a dot ("word.word"), I would like to know if that's a standard Java...
6
2009-05-16T05:58:52Z
871,896
<p>If you just want to create a list of all classes in Java and their methods (so that you can populate a database or an XML file), you may want to write an Eclipse-plugin that looks at the entire JavaCore model, and scans all of its classes (e.g., by searching all subtypes of Object). Then enumerate all the methods. Y...
1
2009-05-16T07:25:01Z
[ "java", "python", "parsing" ]
Simple List of All Java Standard Classes and Methods?
871,812
<p>I'm building a very simple Java parser, to look for some specific usage models. This is in no way lex/yacc or any other form of interpreter/compiler for puposes of running the code. </p> <p>When I encounter a word or a set of two words separated by a dot ("word.word"), I would like to know if that's a standard Java...
6
2009-05-16T05:58:52Z
871,901
<p>IBM had a tool for creating XML from JavaDocs, if I am not mistaken: <a href="http://www.ibm.com/developerworks/xml/library/x-tipjdoc/index.html" rel="nofollow">http://www.ibm.com/developerworks/xml/library/x-tipjdoc/index.html</a></p>
1
2009-05-16T07:26:22Z
[ "java", "python", "parsing" ]
Simple List of All Java Standard Classes and Methods?
871,812
<p>I'm building a very simple Java parser, to look for some specific usage models. This is in no way lex/yacc or any other form of interpreter/compiler for puposes of running the code. </p> <p>When I encounter a word or a set of two words separated by a dot ("word.word"), I would like to know if that's a standard Java...
6
2009-05-16T05:58:52Z
871,924
<p>There's also an option to either parse <code>classlist</code> file from jre/lib folder or open the <code>jsse.jar</code> file, list all classes there and make a list of them in dot-separated form by yourself.</p>
1
2009-05-16T07:41:11Z
[ "java", "python", "parsing" ]
Using exec() with recursive functions
871,887
<p>I want to execute some Python code, typed at runtime, so I get the string and call</p> <blockquote> <p>exec(pp, globals(), locals())</p> </blockquote> <p>where <strong>pp</strong> is the string. It works fine, except for recursive calls, e. g., for example, this code is OK:</p> <pre><code>def horse(): robot...
5
2009-05-16T07:21:25Z
871,906
<p>It works for me:</p> <pre><code>a = """\ def rec(n): if n &gt; 10: return print n return rec(n+1) rec(5)""" exec(a) 5 6 7 8 9 10 </code></pre> <p>All I can say is that there is probably a bug in your code.</p> <p><strong>Edit</strong></p> <p>Here you go</p> <pre><code>def fn1(): glob =...
4
2009-05-16T07:30:48Z
[ "python", "recursion", "exec" ]
Using exec() with recursive functions
871,887
<p>I want to execute some Python code, typed at runtime, so I get the string and call</p> <blockquote> <p>exec(pp, globals(), locals())</p> </blockquote> <p>where <strong>pp</strong> is the string. It works fine, except for recursive calls, e. g., for example, this code is OK:</p> <pre><code>def horse(): robot...
5
2009-05-16T07:21:25Z
871,956
<p>"NameError: global name 'rec' is not defined" means it's looking for rec in the global scope, not the local scope. Looks like it's defining rec in the local scope but then attempting to execute in the global. Try printing locals() and globals() in side the string you're executing.</p> <p><a href="http://www.python....
0
2009-05-16T08:05:53Z
[ "python", "recursion", "exec" ]
Using exec() with recursive functions
871,887
<p>I want to execute some Python code, typed at runtime, so I get the string and call</p> <blockquote> <p>exec(pp, globals(), locals())</p> </blockquote> <p>where <strong>pp</strong> is the string. It works fine, except for recursive calls, e. g., for example, this code is OK:</p> <pre><code>def horse(): robot...
5
2009-05-16T07:21:25Z
871,983
<p>This works for me (added <code>global rec</code>). <code>rec(5)</code> calls the local <code>rec</code>, but <code>rec(n+1)</code> calls a global rec (which doesn't exist) without it.</p> <pre><code>def fn1(): a = """global rec def rec(n): if n &gt; 10: return print n return rec(n+1) rec(5)...
3
2009-05-16T08:30:27Z
[ "python", "recursion", "exec" ]
Using exec() with recursive functions
871,887
<p>I want to execute some Python code, typed at runtime, so I get the string and call</p> <blockquote> <p>exec(pp, globals(), locals())</p> </blockquote> <p>where <strong>pp</strong> is the string. It works fine, except for recursive calls, e. g., for example, this code is OK:</p> <pre><code>def horse(): robot...
5
2009-05-16T07:21:25Z
872,082
<p>This surprised me too at first, and seems to be an odd corner case where exec is acting neither quite like a top-level definition, or a definition within an enclosing function. It looks like what is happening is that the function definition is being executed in the locals() dict you pass in. However, the defined f...
4
2009-05-16T09:45:06Z
[ "python", "recursion", "exec" ]
Python vs. C# Twitter API libraries
872,054
<p>I have experience with both .NET(5yrs) and Python(1yr) and I want to create a simple web project with Twitter as the backbone. I have experience with AppEngine, and have always wanted to try Azure. I'm going to make extensive use of sending and parsing tweets from lots of users at a time, and since I've set a short ...
12
2009-05-16T09:27:26Z
872,066
<p>The best advice is to use whatever language you are most comfortable with.</p> <p>Myself and a colleague have recently re-written our Twitter web-app's entire back-end with a C# service, and the decision for us came down to which library best suited the purpose. A number of the libraries have varying 'features', s...
4
2009-05-16T09:35:35Z
[ "c#", "python", "api", "twitter" ]
Python vs. C# Twitter API libraries
872,054
<p>I have experience with both .NET(5yrs) and Python(1yr) and I want to create a simple web project with Twitter as the backbone. I have experience with AppEngine, and have always wanted to try Azure. I'm going to make extensive use of sending and parsing tweets from lots of users at a time, and since I've set a short ...
12
2009-05-16T09:27:26Z
872,219
<p>I would put my vote in for this twitter library; <a href="http://code.google.com/p/python-twitter/" rel="nofollow">http://code.google.com/p/python-twitter/</a></p> <p>I've used it in 10+ projects that I can think of and its been very good. I've actually been using the dev version in a number of projects too and fou...
4
2009-05-16T11:17:24Z
[ "c#", "python", "api", "twitter" ]
Python vs. C# Twitter API libraries
872,054
<p>I have experience with both .NET(5yrs) and Python(1yr) and I want to create a simple web project with Twitter as the backbone. I have experience with AppEngine, and have always wanted to try Azure. I'm going to make extensive use of sending and parsing tweets from lots of users at a time, and since I've set a short ...
12
2009-05-16T09:27:26Z
872,603
<p>You can use both .NET and Python ... IronPython. IronPython will work with Yedda. [1]</p>
0
2009-05-16T15:10:35Z
[ "c#", "python", "api", "twitter" ]
Python vs. C# Twitter API libraries
872,054
<p>I have experience with both .NET(5yrs) and Python(1yr) and I want to create a simple web project with Twitter as the backbone. I have experience with AppEngine, and have always wanted to try Azure. I'm going to make extensive use of sending and parsing tweets from lots of users at a time, and since I've set a short ...
12
2009-05-16T09:27:26Z
872,611
<p>I am using <a href="http://mike.verdone.ca/twitter/" rel="nofollow">this python library</a> for one of my project.</p> <p>It's really easy to use and yet very powerful.</p>
0
2009-05-16T15:16:07Z
[ "c#", "python", "api", "twitter" ]
Python vs. C# Twitter API libraries
872,054
<p>I have experience with both .NET(5yrs) and Python(1yr) and I want to create a simple web project with Twitter as the backbone. I have experience with AppEngine, and have always wanted to try Azure. I'm going to make extensive use of sending and parsing tweets from lots of users at a time, and since I've set a short ...
12
2009-05-16T09:27:26Z
926,485
<p><a href="http://linqtotwitter.codeplex.com/" rel="nofollow">LINQ to Twitter</a> is available too, covers the entire Twitter API, and works with VB, C#, and Delphi Prism.</p> <p>Joe</p>
3
2009-05-29T15:07:59Z
[ "c#", "python", "api", "twitter" ]
Python vs. C# Twitter API libraries
872,054
<p>I have experience with both .NET(5yrs) and Python(1yr) and I want to create a simple web project with Twitter as the backbone. I have experience with AppEngine, and have always wanted to try Azure. I'm going to make extensive use of sending and parsing tweets from lots of users at a time, and since I've set a short ...
12
2009-05-16T09:27:26Z
1,100,307
<p>python-twyt by Andrew Price. BSD licensed Twitter API interface library and command line client.</p> <p>is my python library of choice. it's fairly straightforward. </p>
0
2009-07-08T20:15:04Z
[ "c#", "python", "api", "twitter" ]
Python vs. C# Twitter API libraries
872,054
<p>I have experience with both .NET(5yrs) and Python(1yr) and I want to create a simple web project with Twitter as the backbone. I have experience with AppEngine, and have always wanted to try Azure. I'm going to make extensive use of sending and parsing tweets from lots of users at a time, and since I've set a short ...
12
2009-05-16T09:27:26Z
1,288,970
<p>I have a bit of experience with the Twitter API (I'm Digitallyborn, author of Twitterizer).</p> <p>I would say go with what is easiest to you. There are a lot of great libraries out there for every language.</p>
0
2009-08-17T16:21:37Z
[ "c#", "python", "api", "twitter" ]
Visual Editor for Django Templates?
872,065
<p>Is there a tool out there for visually building Django templates?</p> <p>Thanks</p>
16
2009-05-16T09:34:04Z
873,728
<p>I haven't tried it personally, but a co-worker uses the free <a href="http://www.activestate.com/komodo%5Fedit/features/" rel="nofollow">ActiveState Komodo Edit</a> to edit Django templates, and the page I linked claims support for Django template editing.</p> <p>There's also <a href="http://code.google.com/p/netbe...
6
2009-05-17T01:51:48Z
[ "python", "django", "django-templates" ]
Visual Editor for Django Templates?
872,065
<p>Is there a tool out there for visually building Django templates?</p> <p>Thanks</p>
16
2009-05-16T09:34:04Z
874,222
<p>There's no WYSIWYG tool like Dreamweaver. But highligting is possible. I am using Kate to edit my templates.</p> <p>For instance when you comment in Django template it inserts <code>{% comment %}</code>.</p>
2
2009-05-17T09:19:03Z
[ "python", "django", "django-templates" ]
Visual Editor for Django Templates?
872,065
<p>Is there a tool out there for visually building Django templates?</p> <p>Thanks</p>
16
2009-05-16T09:34:04Z
875,985
<p>Kind of an oblique answer, but if being able to use tools like Dreamweaver on your templates is important to you, you may find you like Genshi better than Django Templates, and it's easy enough to switch your template engine. </p> <p>Genshi is an XML templating language that is one of the inheritors of the Zope TAL...
1
2009-05-18T02:04:43Z
[ "python", "django", "django-templates" ]
Visual Editor for Django Templates?
872,065
<p>Is there a tool out there for visually building Django templates?</p> <p>Thanks</p>
16
2009-05-16T09:34:04Z
3,940,622
<p>I'm really liking Eclipse with Pydev and the Aptana Studio 3 Eclipse plugin. More info here: <a href="http://pydev.blogspot.com/2010/08/django-templates-editor.html" rel="nofollow">http://pydev.blogspot.com/2010/08/django-templates-editor.html</a></p> <p>(The first thing I did was change the theme. I quite like the...
1
2010-10-15T08:27:59Z
[ "python", "django", "django-templates" ]
Visual Editor for Django Templates?
872,065
<p>Is there a tool out there for visually building Django templates?</p> <p>Thanks</p>
16
2009-05-16T09:34:04Z
4,040,196
<p>There is also <a href="http://www.jetbrains.com/pycharm/">PyCharm</a>.</p>
6
2010-10-28T05:59:17Z
[ "python", "django", "django-templates" ]
Visual Editor for Django Templates?
872,065
<p>Is there a tool out there for visually building Django templates?</p> <p>Thanks</p>
16
2009-05-16T09:34:04Z
7,990,708
<p>Don't forget for the <a href="https://code.djangoproject.com/wiki/Emacs" rel="nofollow">Emacs</a> Users there is django template assistance. The Emacs link will take you to more helpful documentation in Emacs.</p>
0
2011-11-03T05:40:42Z
[ "python", "django", "django-templates" ]
No reverse error in Google App Engine Django patch?
872,100
<p>I am using <a href="http://en.wikipedia.org/wiki/Google%5FApp%5FEngine" rel="nofollow">Google App Engine</a> patch Django.</p> <p>When I try to go to the admin site,</p> <blockquote> <p><a href="http://127.0.0.1:8080/admin/" rel="nofollow">http://127.0.0.1:8080/admin/</a></p> </blockquote> <p>, I keep getting t...
2
2009-05-16T10:02:28Z
889,946
<p>I could not find a proper answer to my question. Anyways I solved the problem temporarily by reinstalling the Django framework and the app engine SDK.</p>
1
2009-05-20T20:01:37Z
[ "python", "django", "google-app-engine", "django-urls" ]
What's wrong with this Python code?
872,119
<p>I'm very new so just learning, so go easy please!</p> <pre><code>start = int(input('How much did you start with?:' )) if start &lt; 0: print("That's impossible! Try again.") print(start = int(input('How much did you start with:' ))) if start &gt;= 0: print(inorout = raw_input('Cool! Now have you put m...
-1
2009-05-16T10:09:06Z
872,125
<ul> <li>You can't assign to variables in expressions in Python, like in C: print (start=int(input('blah'))) isn't correct. Do the assignment first in a separate statement.</li> <li>The first line musn't be indented, but that might just be a copy and paste error.</li> <li>The word <code>in</code> is a reserved word so ...
7
2009-05-16T10:14:00Z
[ "python" ]
What's wrong with this Python code?
872,119
<p>I'm very new so just learning, so go easy please!</p> <pre><code>start = int(input('How much did you start with?:' )) if start &lt; 0: print("That's impossible! Try again.") print(start = int(input('How much did you start with:' ))) if start &gt;= 0: print(inorout = raw_input('Cool! Now have you put m...
-1
2009-05-16T10:09:06Z
872,130
<p>Assigning in statements is your problem. Move the assignments out of print statements</p>
3
2009-05-16T10:19:05Z
[ "python" ]
What's wrong with this Python code?
872,119
<p>I'm very new so just learning, so go easy please!</p> <pre><code>start = int(input('How much did you start with?:' )) if start &lt; 0: print("That's impossible! Try again.") print(start = int(input('How much did you start with:' ))) if start &gt;= 0: print(inorout = raw_input('Cool! Now have you put m...
-1
2009-05-16T10:09:06Z
872,465
<ul> <li>Consider prompting for input using a function wrapping a loop.</li> <li>Don't use <a href="http://docs.python.org/library/functions.html#input" rel="nofollow">input</a> for general user input, use <a href="http://docs.python.org/library/functions.html#raw_input" rel="nofollow">raw_input</a> instead</li> <li>Wr...
0
2009-05-16T13:49:35Z
[ "python" ]
Sorting a list of objects by attribute
872,181
<p>I am trying to sort a list of objects in python, however this code will not work:</p> <pre><code>import datetime class Day: def __init__(self, date, text): self.date = date self.text = text def __cmp__(self, other): return cmp(self.date, other.date) mylist = [Day(datetime.date(200...
0
2009-05-16T10:53:44Z
872,190
<p>mylist.sort() returns nothing, it sorts the list in place. Change it to </p> <pre><code>mylist.sort() print mylist </code></pre> <p>to see the correct result. </p> <p>See <a href="http://docs.python.org/library/stdtypes.html#mutable-sequence-types" rel="nofollow">http://docs.python.org/library/stdtypes.html#mutab...
5
2009-05-16T11:00:58Z
[ "python" ]
Sorting a list of objects by attribute
872,181
<p>I am trying to sort a list of objects in python, however this code will not work:</p> <pre><code>import datetime class Day: def __init__(self, date, text): self.date = date self.text = text def __cmp__(self, other): return cmp(self.date, other.date) mylist = [Day(datetime.date(200...
0
2009-05-16T10:53:44Z
872,213
<p>See <a href="http://docs.python.org/library/functions.html#sorted" rel="nofollow"><code>sorted</code></a> for a function that will return a sorted copy of any iterable.</p>
2
2009-05-16T11:13:45Z
[ "python" ]
How to convert python date format to 10-digit date format for mysql?
872,255
<p>how to convert python date format to 10 digit date format for mysql</p> <p>example: date in python -> 11-05-09</p> <p>to something like 1239992972 (10 digit)</p> <p>Thanks</p>
0
2009-05-16T11:41:22Z
872,263
<p>You can use the time module's strptime - you pass in a string time (e.g. 11-05-09) and a format and it will return a struct_time, which you can get the numerical value from (by calling time.mktime on the returned struct_time). See the <a href="http://docs.python.org/library/time.html#time.strptime" rel="nofollow">do...
4
2009-05-16T11:46:14Z
[ "python" ]
How to convert python date format to 10-digit date format for mysql?
872,255
<p>how to convert python date format to 10 digit date format for mysql</p> <p>example: date in python -> 11-05-09</p> <p>to something like 1239992972 (10 digit)</p> <p>Thanks</p>
0
2009-05-16T11:41:22Z
872,264
<p>If it is a datetime obj, you can do:</p> <pre><code>import time time.mktime(datetime_obj.timetuple()) </code></pre> <p>If not:</p> <pre><code>time.mktime(time.strptime("11-05-09", "%d-%m-%y")) </code></pre>
2
2009-05-16T11:46:41Z
[ "python" ]
How to convert python date format to 10-digit date format for mysql?
872,255
<p>how to convert python date format to 10 digit date format for mysql</p> <p>example: date in python -> 11-05-09</p> <p>to something like 1239992972 (10 digit)</p> <p>Thanks</p>
0
2009-05-16T11:41:22Z
872,271
<p>Use time.strptime() to convert the date into a time tuple, and then time.mktime() (or calendar.timegm()) to convert that into a floating point time. You'll probably need to truncate it to an integer after.</p> <pre><code>tm = time.strptime('11-05-09', '%d-%m-%y') time = time.mktime(tm) time_int = int(time) </code><...
0
2009-05-16T11:50:15Z
[ "python" ]
How to convert python date format to 10-digit date format for mysql?
872,255
<p>how to convert python date format to 10 digit date format for mysql</p> <p>example: date in python -> 11-05-09</p> <p>to something like 1239992972 (10 digit)</p> <p>Thanks</p>
0
2009-05-16T11:41:22Z
30,144,025
<p>You can use <a href="https://github.com/ralphavalon/easy_date" rel="nofollow">easy_date</a> to make it easy:</p> <pre><code>import date_converter timestamp = date_converter.string_to_timestamp('11-05-09','%d-%m-%y') </code></pre>
0
2015-05-09T19:15:39Z
[ "python" ]
Most Efficient Way to Find Whether a Large List Contains a Specific String (Python)
872,290
<p>I have a file containing roughly all the words in English (~60k words, ~500k characters). I want to test whether a certain word I receive as input is "in English" (i.e. if this exact word is in the list).</p> <p>What would be the most efficient way to do this in Python?</p> <p>The trivial solution is to load the f...
6
2009-05-16T12:01:37Z
872,295
<p>You're basically testing whether a member is in a set or not, right?</p> <p>If so, and because you said you have lots of memory, why not just load all the words as keys in memcache, and then for every word, just check if it is present in memcache or not.</p> <p>Or use that data structure that is used by bash to au...
1
2009-05-16T12:04:42Z
[ "python", "string" ]
Most Efficient Way to Find Whether a Large List Contains a Specific String (Python)
872,290
<p>I have a file containing roughly all the words in English (~60k words, ~500k characters). I want to test whether a certain word I receive as input is "in English" (i.e. if this exact word is in the list).</p> <p>What would be the most efficient way to do this in Python?</p> <p>The trivial solution is to load the f...
6
2009-05-16T12:01:37Z
872,297
<p>The python <a href="http://docs.python.org/library/stdtypes.html#set-types-set-frozenset" rel="nofollow">Set</a> is what you should try.</p> <blockquote> <p>A set object is an unordered collection of distinct hashable objects. Common uses include <strong>membership testing</strong>, removing duplicates from a seq...
13
2009-05-16T12:05:17Z
[ "python", "string" ]
Most Efficient Way to Find Whether a Large List Contains a Specific String (Python)
872,290
<p>I have a file containing roughly all the words in English (~60k words, ~500k characters). I want to test whether a certain word I receive as input is "in English" (i.e. if this exact word is in the list).</p> <p>What would be the most efficient way to do this in Python?</p> <p>The trivial solution is to load the f...
6
2009-05-16T12:01:37Z
872,299
<p>A <a href="http://en.wikipedia.org/wiki/Trie" rel="nofollow">Trie</a> structure would suit your purposes. There are undoubtedly Python implementations to be found out there...</p>
3
2009-05-16T12:05:58Z
[ "python", "string" ]
Most Efficient Way to Find Whether a Large List Contains a Specific String (Python)
872,290
<p>I have a file containing roughly all the words in English (~60k words, ~500k characters). I want to test whether a certain word I receive as input is "in English" (i.e. if this exact word is in the list).</p> <p>What would be the most efficient way to do this in Python?</p> <p>The trivial solution is to load the f...
6
2009-05-16T12:01:37Z
872,300
<p>If memory consumption isn't an issue and the words won't change, the fastest way to do this is put everything in a hash and search that way. In Python, this is the <a href="http://www.python.org/doc/2.5.2/tut/node7.html#SECTION007400000000000000000" rel="nofollow"><strong><code>Set</code></strong></a>. You'll have c...
1
2009-05-16T12:06:07Z
[ "python", "string" ]
Most Efficient Way to Find Whether a Large List Contains a Specific String (Python)
872,290
<p>I have a file containing roughly all the words in English (~60k words, ~500k characters). I want to test whether a certain word I receive as input is "in English" (i.e. if this exact word is in the list).</p> <p>What would be the most efficient way to do this in Python?</p> <p>The trivial solution is to load the f...
6
2009-05-16T12:01:37Z
872,301
<p>500k character is not a large list. if items in your list are unique and you need to do this search repeatedly use <a href="http://docs.python.org/library/stdtypes.html#set-types-set-frozenset" rel="nofollow"><code>set</code></a> which would lower the complexity to <code>O(1)</code> in the best case.</p>
1
2009-05-16T12:06:27Z
[ "python", "string" ]