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
Python .sort() not working as expected
1,513,727
<p>Tackling a few puzzle problems on a quiet Saturday night (wooohoo... not) and am struggling with sort(). The results aren't quite what I expect. The program iterates through every combination from 100 - 999 and checks if the product is a palindome. If it is, append to the list. I need the list sorted :D Here's my pr...
3
2009-10-03T13:22:03Z
1,513,745
<p>Your list contains strings so it is sorting them alphabetically - try converting the list to integers and then do the sort.</p>
2
2009-10-03T13:28:08Z
[ "python", "list", "sorting" ]
Python .sort() not working as expected
1,513,727
<p>Tackling a few puzzle problems on a quiet Saturday night (wooohoo... not) and am struggling with sort(). The results aren't quite what I expect. The program iterates through every combination from 100 - 999 and checks if the product is a palindome. If it is, append to the list. I need the list sorted :D Here's my pr...
3
2009-10-03T13:22:03Z
1,513,749
<p>The comparator operator is treating your input as strings instead of integers. In string comparsion 2 as the 3rd letter is lexically greater than 1. <code> reversed = str(mult)[::-1] </code></p>
0
2009-10-03T13:28:52Z
[ "python", "list", "sorting" ]
Python .sort() not working as expected
1,513,727
<p>Tackling a few puzzle problems on a quiet Saturday night (wooohoo... not) and am struggling with sort(). The results aren't quite what I expect. The program iterates through every combination from 100 - 999 and checks if the product is a palindome. If it is, append to the list. I need the list sorted :D Here's my pr...
3
2009-10-03T13:22:03Z
1,513,790
<p>Sort is doing its job. If you intended to store integers in the list, take Lukáš advice. You can also tell sort how to sort, for example by making ints:</p> <pre><code>list.sort(key=int) </code></pre> <p>the key parameter takes a function that calculates an item to take the list object's place in all comparisons...
8
2009-10-03T13:45:35Z
[ "python", "list", "sorting" ]
Python .sort() not working as expected
1,513,727
<p>Tackling a few puzzle problems on a quiet Saturday night (wooohoo... not) and am struggling with sort(). The results aren't quite what I expect. The program iterates through every combination from 100 - 999 and checks if the product is a palindome. If it is, append to the list. I need the list sorted :D Here's my pr...
3
2009-10-03T13:22:03Z
1,721,461
<p>No need to convert to int. mult already is an int and as you have checked it is a palindrome it will look the same as reversed, so just:</p> <pre><code>list.append(mult) </code></pre>
1
2009-11-12T11:04:10Z
[ "python", "list", "sorting" ]
How to get a http page using mechanize cookies?
1,513,823
<p>There is a Python mechanize object with a form with almost all values set, but not yet submitted. Now I want to fetch another page using cookies from mechanize instance, but without resetting the page, forms and so on, e.g. so that the values remain set (I just need to get body string of another page, nothing else)....
3
2009-10-03T14:08:10Z
1,513,870
<p>No idea whether this will work, but why don't you try deepcopying the mechanize instance, eg</p> <pre><code>from copy import deepcopy br = Browser() br.open("http://www.example.com/") # Make a copy for doing other stuff with br2 = deepcopy(br) # Do stuff with br2 # Now do stuff with br </code></pre>
2
2009-10-03T14:29:37Z
[ "python", "mechanize" ]
How to get a http page using mechanize cookies?
1,513,823
<p>There is a Python mechanize object with a form with almost all values set, but not yet submitted. Now I want to fetch another page using cookies from mechanize instance, but without resetting the page, forms and so on, e.g. so that the values remain set (I just need to get body string of another page, nothing else)....
3
2009-10-03T14:08:10Z
1,513,899
<p>Some wild ideas:</p> <ul> <li>Fetch the second page before filling in the form? </li> <li>Or fetch the new page and then goBack()? Although maybe that will reset the values.</li> </ul>
2
2009-10-03T14:39:21Z
[ "python", "mechanize" ]
How to get a http page using mechanize cookies?
1,513,823
<p>There is a Python mechanize object with a form with almost all values set, but not yet submitted. Now I want to fetch another page using cookies from mechanize instance, but without resetting the page, forms and so on, e.g. so that the values remain set (I just need to get body string of another page, nothing else)....
3
2009-10-03T14:08:10Z
1,514,850
<p>And the correct answer:</p> <pre><code>opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.br._ua_handlers["_cookies"].cookiejar)) opener.open(imgurl) </code></pre>
5
2009-10-03T21:03:46Z
[ "python", "mechanize" ]
Python Implementation of the Object Pool Design Pattern
1,514,120
<p>I need an <a href="http://en.wikipedia.org/wiki/Object_pool">Object Pool</a>, and rather than implement it myself, I thought I would look around for a ready-made and tested Python library.</p> <p>What I found was plenty of other people <a href="http://mail.python.org/pipermail/python-list/2009-July/719988.html">loo...
18
2009-10-03T15:55:44Z
1,514,269
<p>It seems to me, from your description, that what you need is a pool of <em>connections</em>, not of objects. For simple thread-safety, just keep the reusable connections in a <code>Queue.Queue</code> instance, call it <code>pool</code>. When a thread instantiates a connection-wrapping object, the object gets its co...
20
2009-10-03T16:57:21Z
[ "python", "design-patterns", "data-structures" ]
Python Implementation of the Object Pool Design Pattern
1,514,120
<p>I need an <a href="http://en.wikipedia.org/wiki/Object_pool">Object Pool</a>, and rather than implement it myself, I thought I would look around for a ready-made and tested Python library.</p> <p>What I found was plenty of other people <a href="http://mail.python.org/pipermail/python-list/2009-July/719988.html">loo...
18
2009-10-03T15:55:44Z
5,020,415
<p>I had a similar problem and I must say Queue.Queue is quite good, however there is a missing piece of the puzzle. The following class helps deal with ensuring the object taken gets returned to the pool. Example is included.</p> <p>I've allowed 2 ways to use this class, with keyword or encapsulating object with dest...
3
2011-02-16T18:16:32Z
[ "python", "design-patterns", "data-structures" ]
Is it reasonable to save data as python modules?
1,514,228
<p>This is what I've done for a project. I have a few data structures that are bascially dictionaries with some methods that operate on the data. When I save them to disk, I write them out to .py files as code that when imported as a module will load the same data into such a data structure.</p> <p>Is this reasonable?...
3
2009-10-03T16:40:01Z
1,514,234
<p>The biggest drawback is that it's a potential security problem since it's hard to guarantee that the files won't contains arbitrary code, which could be really bad. So don't use this approach if anyone else than you have write-access to the files.</p>
3
2009-10-03T16:42:14Z
[ "python", "data-persistence", "dynamic-import" ]
Is it reasonable to save data as python modules?
1,514,228
<p>This is what I've done for a project. I have a few data structures that are bascially dictionaries with some methods that operate on the data. When I save them to disk, I write them out to .py files as code that when imported as a module will load the same data into such a data structure.</p> <p>Is this reasonable?...
3
2009-10-03T16:40:01Z
1,514,240
<p>A reasonable option might be to use the <a href="http://docs.python.org/library/pickle.html" rel="nofollow">Pickle</a> module, which is specifically designed to save and restore python structures to disk.</p>
3
2009-10-03T16:45:48Z
[ "python", "data-persistence", "dynamic-import" ]
Is it reasonable to save data as python modules?
1,514,228
<p>This is what I've done for a project. I have a few data structures that are bascially dictionaries with some methods that operate on the data. When I save them to disk, I write them out to .py files as code that when imported as a module will load the same data into such a data structure.</p> <p>Is this reasonable?...
3
2009-10-03T16:40:01Z
1,514,248
<p>It's reasonable, and I do it all the time. Obviously it's not a format you use to exchange data, so it's not a good format for anything like a save file. </p> <p>But for example, when I do migrations of websites to Plone, I often get data about the site (such as a list of which pages should be migrated, or a list o...
3
2009-10-03T16:48:05Z
[ "python", "data-persistence", "dynamic-import" ]
Is it reasonable to save data as python modules?
1,514,228
<p>This is what I've done for a project. I have a few data structures that are bascially dictionaries with some methods that operate on the data. When I save them to disk, I write them out to .py files as code that when imported as a module will load the same data into such a data structure.</p> <p>Is this reasonable?...
3
2009-10-03T16:40:01Z
1,514,250
<p>By operating this way, you may gain some modicum of convenience, but you pay many kinds of price for that. The space it takes to save your data, and the time it takes to both save and reload it, go up substantially; and your security exposure is unbounded -- you must ferociously guard the paths from which you reload...
7
2009-10-03T16:48:31Z
[ "python", "data-persistence", "dynamic-import" ]
Is it reasonable to save data as python modules?
1,514,228
<p>This is what I've done for a project. I have a few data structures that are bascially dictionaries with some methods that operate on the data. When I save them to disk, I write them out to .py files as code that when imported as a module will load the same data into such a data structure.</p> <p>Is this reasonable?...
3
2009-10-03T16:40:01Z
1,515,466
<p>Alex Martelli's answer is absolutely insightful and I agree with him. However, I'll go one step further and make a specific recommendation: use JSON.</p> <p>JSON is simple, and Python's data structures map well into it; and there are several standard libraries and tools for working with JSON. The <code>json</code...
3
2009-10-04T02:44:48Z
[ "python", "data-persistence", "dynamic-import" ]
Installing python module
1,514,371
<p>I'm new to python and is trying to install the pyimage module. <a href="http://code.google.com/p/pyimage/" rel="nofollow">http://code.google.com/p/pyimage/</a></p> <p>I'm on Windows, and have downloaded and installed 2.6 and 3.1.</p> <p>I downloaded pyimage, and used cmd and cd to get to its dir.</p> <p>I then go...
0
2009-10-03T17:37:47Z
1,514,395
<p>It is giving you that error because Python is not in your path. By default, the Python executable is not added to the path. You will have to do it manually. An in-depth tutorial endorsed by the Python website may be found <a href="http://showmedo.com/videos/video?name=960000&amp;fromSeriesID=96" rel="nofollow">here....
3
2009-10-03T17:42:51Z
[ "python", "module" ]
Installing python module
1,514,371
<p>I'm new to python and is trying to install the pyimage module. <a href="http://code.google.com/p/pyimage/" rel="nofollow">http://code.google.com/p/pyimage/</a></p> <p>I'm on Windows, and have downloaded and installed 2.6 and 3.1.</p> <p>I downloaded pyimage, and used cmd and cd to get to its dir.</p> <p>I then go...
0
2009-10-03T17:37:47Z
1,514,455
<p>To put Python on your system's environment PATH variable, so that running python at a command prompt will work, follow the instructions in <a href="http://showmedo.com/videos/video?name=960000&amp;fromSeriesID=96" rel="nofollow">this video</a>.</p>
1
2009-10-03T18:08:10Z
[ "python", "module" ]
GqlQuery OrderBy a property in a ReferenceProperty
1,514,450
<p>I don't know how to make a GqlQuery that order by a property of the ReferenceProperty.</p> <p>In this case, I want to get all the Seat but order by the id of the Room</p> <p>The GqlQuery reference does not allow a join, so how do you approach in this case?</p> <pre><code>class Room(db.Model): id = db.IntegerP...
1
2009-10-03T18:05:44Z
1,514,466
<p>In general, to work around non-relational DBs lack of JOIN functionality, you denormalize; specifically, you redundantly put data pieces in more than one place, so they can be effectively accessed in your queries (this does make it more cumbersome to update your store, but then, non-relational DBs in general are hea...
0
2009-10-03T18:14:55Z
[ "python", "django", "google-app-engine" ]
GqlQuery OrderBy a property in a ReferenceProperty
1,514,450
<p>I don't know how to make a GqlQuery that order by a property of the ReferenceProperty.</p> <p>In this case, I want to get all the Seat but order by the id of the Room</p> <p>The GqlQuery reference does not allow a join, so how do you approach in this case?</p> <pre><code>class Room(db.Model): id = db.IntegerP...
1
2009-10-03T18:05:44Z
6,353,840
<p>I think in you case it will work as you want:</p> <pre><code>class Seat(db.Model): id = db.IntegerProperty() roomId = db.ReferenceProperty(Room,collection_name='seats') </code></pre> <p>And after that you may use something like this:</p> <pre><code>rooms = Room.all().order('id') for room in rooms: sea...
0
2011-06-15T06:22:33Z
[ "python", "django", "google-app-engine" ]
C#: Does a dictionary in C# have something similar to the Python setdefault?
1,514,457
<p>Trying to translate <a href="http://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-number/118712#118712">some methods</a> written in Python over into C#. The line looks like this:</p> <pre><code>d[p] = d.setdefault(p, 0) + 1 </code></pre> <p>What exactly does setdefault...
3
2009-10-03T18:10:29Z
1,514,470
<p>From the <a href="http://docs.python.org/library/stdtypes.html" rel="nofollow">Python docs</a>:</p> <blockquote> <p><code>setdefault(key[, default])</code></p> <p>If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.</p> </bloc...
12
2009-10-03T18:16:12Z
[ "c#", "python", "dictionary" ]
C#: Does a dictionary in C# have something similar to the Python setdefault?
1,514,457
<p>Trying to translate <a href="http://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-number/118712#118712">some methods</a> written in Python over into C#. The line looks like this:</p> <pre><code>d[p] = d.setdefault(p, 0) + 1 </code></pre> <p>What exactly does setdefault...
3
2009-10-03T18:10:29Z
1,514,475
<p>d.setdefault(p, 0) will return the value of the entry with key p if it exists and if it does not then it will set the value for the key p to 0.</p>
0
2009-10-03T18:21:39Z
[ "c#", "python", "dictionary" ]
C#: Does a dictionary in C# have something similar to the Python setdefault?
1,514,457
<p>Trying to translate <a href="http://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-number/118712#118712">some methods</a> written in Python over into C#. The line looks like this:</p> <pre><code>d[p] = d.setdefault(p, 0) + 1 </code></pre> <p>What exactly does setdefault...
3
2009-10-03T18:10:29Z
1,514,477
<p><strong>Edit</strong> -- warning: the following works only for this <em>specific</em> case, <strong>not</strong> for the general case -- see below.</p> <pre><code>int value = 0; d.TryGetValue(p, out value); d[p] = value + 1; </code></pre> <p>this is equivalent to the following Python snippet (which is better than ...
6
2009-10-03T18:23:16Z
[ "c#", "python", "dictionary" ]
C#: Does a dictionary in C# have something similar to the Python setdefault?
1,514,457
<p>Trying to translate <a href="http://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-number/118712#118712">some methods</a> written in Python over into C#. The line looks like this:</p> <pre><code>d[p] = d.setdefault(p, 0) + 1 </code></pre> <p>What exactly does setdefault...
3
2009-10-03T18:10:29Z
3,427,050
<p>If you want to have the item default to the default instance of object, you might want to consider this (from <a href="http://neofight.wordpress.com/2010/08/06/setdefault-for-c-dictionary-idictionary/" rel="nofollow">here</a>)</p> <pre><code>public static TValue SetDefault&lt;TKey, TValue&gt;(this IDictionary&lt;TK...
1
2010-08-06T19:24:50Z
[ "c#", "python", "dictionary" ]
C#: Does a dictionary in C# have something similar to the Python setdefault?
1,514,457
<p>Trying to translate <a href="http://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-number/118712#118712">some methods</a> written in Python over into C#. The line looks like this:</p> <pre><code>d[p] = d.setdefault(p, 0) + 1 </code></pre> <p>What exactly does setdefault...
3
2009-10-03T18:10:29Z
10,455,782
<p>I know I'm 3 years late to the party, but a variation that works and is useful:</p> <pre><code>public static TV SetDefault&lt;TK, TV&gt;(this IDictionary&lt;TK, TV&gt; dict, TK key) where TV: new() { TV value; if (!dict.TryGetValue(key, out value)) dict.Add(key, value = new TV()); return value; } </code...
0
2012-05-04T20:34:09Z
[ "c#", "python", "dictionary" ]
Fitting a step function
1,514,484
<p>I am trying to fit a step function using scipy.optimize.leastsq. Consider the following example:</p> <pre><code>import numpy as np from scipy.optimize import leastsq def fitfunc(p, x): y = np.zeros(x.shape) y[x &lt; p[0]] = p[1] y[p[0] &lt; x] = p[2] return y errfunc = lambda p, x, y: fitfunc(p, x...
3
2009-10-03T18:28:22Z
1,514,670
<p>Most probably your optimization is stuck in a <em>local minima</em>. I don't know what leastsq really works like, but if you give it an initial estimate of (0, 0, 0), it gets stuck there, too.</p> <p>You can check the gradient at the initial estimate numerically (evaluate at +/- epsilon for a very small epsilon and...
0
2009-10-03T19:53:35Z
[ "python", "scipy", "curve-fitting", "best-fit-curve" ]
Fitting a step function
1,514,484
<p>I am trying to fit a step function using scipy.optimize.leastsq. Consider the following example:</p> <pre><code>import numpy as np from scipy.optimize import leastsq def fitfunc(p, x): y = np.zeros(x.shape) y[x &lt; p[0]] = p[1] y[p[0] &lt; x] = p[2] return y errfunc = lambda p, x, y: fitfunc(p, x...
3
2009-10-03T18:28:22Z
1,514,730
<p>It turns out that the fitting is much better if I add the epsfcn= argument to leastsq:</p> <pre><code>p1, success = leastsq(errfunc, p0, args=(x, y), epsfcn=10.) </code></pre> <p>and the result is</p> <pre><code>[ 248.00000146 -8.8273455 0.40818216] </code></pre> <p>My basic understanding is that the first...
1
2009-10-03T20:15:59Z
[ "python", "scipy", "curve-fitting", "best-fit-curve" ]
Fitting a step function
1,514,484
<p>I am trying to fit a step function using scipy.optimize.leastsq. Consider the following example:</p> <pre><code>import numpy as np from scipy.optimize import leastsq def fitfunc(p, x): y = np.zeros(x.shape) y[x &lt; p[0]] = p[1] y[p[0] &lt; x] = p[2] return y errfunc = lambda p, x, y: fitfunc(p, x...
3
2009-10-03T18:28:22Z
1,514,752
<p>I don't think that least squares fitting is the way to go about coming up with an approximation for a step. I don't believe it will give you a satisfactory description of the discontinuity. Least squares would not be my first thought when attacking this problem. </p> <p>Why wouldn't you use a Fourier series appro...
1
2009-10-03T20:25:57Z
[ "python", "scipy", "curve-fitting", "best-fit-curve" ]
Fitting a step function
1,514,484
<p>I am trying to fit a step function using scipy.optimize.leastsq. Consider the following example:</p> <pre><code>import numpy as np from scipy.optimize import leastsq def fitfunc(p, x): y = np.zeros(x.shape) y[x &lt; p[0]] = p[1] y[p[0] &lt; x] = p[2] return y errfunc = lambda p, x, y: fitfunc(p, x...
3
2009-10-03T18:28:22Z
1,515,267
<p>I propose approximating the step function. Instead of inifinite slope at the "change point" make it linear over one x distance (1.0 in the example). E.g. if the x parameter, xp, for the function is defined as the midpoint on this line then the value at xp-0.5 is the lower y value and the value at xp+0.5 is the highe...
1
2009-10-04T00:43:15Z
[ "python", "scipy", "curve-fitting", "best-fit-curve" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
1,514,557
<pre><code>variable = [] </code></pre> <p>Now <code>variable</code> refers to an empty list<sup>*</sup>.</p> <p>Of course this is an assignment, not a declaration. There's no way to say in Python "this variable should never refer to anything other than a list", since Python is dynamically typed.</p> <hr> <p><sup>*<...
168
2009-10-03T19:07:12Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
1,514,559
<p>This is how:</p> <pre><code>my_array = [1, 'rebecca', 'allard', 15] </code></pre>
12
2009-10-03T19:07:49Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
1,514,561
<p>You don't declare anything in Python. You just use it. I recommend you start out with something like <a href="http://diveintopython.net">http://diveintopython.net</a>.</p>
15
2009-10-03T19:08:05Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
1,514,564
<p>I would normally just do <code>a = [1,2,3]</code> which is actually a <code>list</code> but for <code>arrays</code> look at this formal <a href="http://docs.python.org/library/array.html">definition</a></p>
9
2009-10-03T19:09:13Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
1,514,649
<p>You don't actually declare things, but this is how you create an array in Python:</p> <pre><code>from array import array intarray = array('i') </code></pre> <p>For more info see the array module: <a href="http://docs.python.org/library/array.html">http://docs.python.org/library/array.html</a></p> <p>Now possible ...
56
2009-10-03T19:44:18Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
1,515,282
<p>Following on from Lennart, there's also <a href="http://numpy.scipy.org/">numpy</a> which implements homogeneous multi-dimensional arrays.</p>
6
2009-10-04T00:50:24Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
4,476,624
<p>I think you (meant)want an list with the first 30 cells already filled. So</p> <pre><code> f = [] for i in range(30): f.append(0) </code></pre> <p>An example to where this could be used is in Fibonacci sequence. See problem 2 in <a href="http://projecteuler.net/">Project Euler</a></p>
33
2010-12-18T04:25:20Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
6,768,818
<p>for calculations, use <a href="http://numpy.scipy.org/">numpy</a> arrays like this:</p> <pre><code>import numpy as np a = np.ones((3,2)) # a 2D array with 3 rows, 2 columns, filled with ones b = np.array([1,2,3]) # a 1D array initialised using a list [1,2,3] c = np.linspace(2,3,100) # an array with 100...
9
2011-07-20T21:31:58Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
13,300,517
<p>How about this...</p> <pre><code>&gt;&gt;&gt; a = range(12) &gt;&gt;&gt; a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] &gt;&gt;&gt; a[7] 6 </code></pre>
5
2012-11-09T00:51:43Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
15,822,462
<p>Python calls them <a href="http://docs.python.org/2/tutorial/introduction.html#lists" rel="nofollow">lists</a>. You can write a list literal with square brackets and commas:</p> <pre><code>&gt;&gt;&gt; [6,28,496,8128] [6, 28, 496, 8128] </code></pre>
4
2013-04-04T21:55:40Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
21,780,274
<p>I had an array of strings and needed an array of the same length of booleans initiated to True. This is what I did</p> <pre><code>strs = ["Hi","Bye"] bools = [ True for s in strs ] </code></pre>
2
2014-02-14T13:07:38Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
36,042,565
<p>This is surprisingly complex topic in Python. </p> <h1>Practical answer</h1> <p>Arrays are represented by class <code>list</code> (see <a href="https://docs.python.org/2/tutorial/datastructures.html">reference</a> and do not mix them with <a href="https://wiki.python.org/moin/Generators">generators</a>).</p> <p>C...
7
2016-03-16T17:16:04Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
38,303,109
<p>To add to Lennart's answer, an array may be created like this:</p> <pre><code>from array import array float_array = array("f",values) </code></pre> <p>where <em>values</em> can take the form of a tuple, list, or np.array, but not array:</p> <pre><code>values = [1,2,3] values = (1,2,3) values = np.array([1,2,3],'f...
4
2016-07-11T09:09:04Z
[ "python", "arrays" ]
How to declare an array in Python?
1,514,553
<p>How do I declare an array in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a>?</p> <p>I can't find any reference to arrays in the documentation. </p>
198
2009-10-03T19:03:40Z
39,003,823
<p>A couple of contributions suggested that arrays in python are represented by lists. Perhaps theoretically/under the hood that is correct however a major distinction between the two is the fact that lists accept mixed data types and mixed numeric types, on the other hand array requires a type-code restricting all ele...
3
2016-08-17T18:24:28Z
[ "python", "arrays" ]
Neural Network Example Source-code (preferably Python)
1,514,573
<p>I wonder if anyone has some example code of a Neural network in python. If someone know of some sort of tutorial with a complete walkthrough that would be awesome, but just example source would be great as well!</p> <p>Thanks</p>
11
2009-10-03T19:13:20Z
1,514,631
<p>Here is a simple example by Armin Rigo: <a href="http://codespeak.net/pypy/dist/demo/bpnn.py" rel="nofollow">http://codespeak.net/pypy/dist/demo/bpnn.py</a>. If you want to use more sophisticated stuff, there is also <a href="http://pybrain.org" rel="nofollow">http://pybrain.org</a>.</p> <p><strong>Edit:</strong> L...
7
2009-10-03T19:35:38Z
[ "python", "neural-network" ]
Neural Network Example Source-code (preferably Python)
1,514,573
<p>I wonder if anyone has some example code of a Neural network in python. If someone know of some sort of tutorial with a complete walkthrough that would be awesome, but just example source would be great as well!</p> <p>Thanks</p>
11
2009-10-03T19:13:20Z
1,514,671
<p>Take a look at <a href="http://my.safaribooksonline.com/9780596529321/whats%5Fin%5Fa%5Fsearch%5Fengine#X2ludGVybmFsX1NlY3Rpb25Db250ZW50P3htbGlkPTk3ODA1OTY1MjkzMjEvbGVhcm5pbmdfZnJvbV9jbGlja3M=">Learning from Clicks</a> from the book <a href="http://my.safaribooksonline.com/9780596529321">Programming Collective Intell...
4
2009-10-03T19:53:43Z
[ "python", "neural-network" ]
Neural Network Example Source-code (preferably Python)
1,514,573
<p>I wonder if anyone has some example code of a Neural network in python. If someone know of some sort of tutorial with a complete walkthrough that would be awesome, but just example source would be great as well!</p> <p>Thanks</p>
11
2009-10-03T19:13:20Z
1,515,871
<p>You might want to take a look at <a href="http://montepython.sourceforge.net/" rel="nofollow"><strong>Monte</strong></a>:</p> <blockquote> <p>Monte (python) is a Python framework for building gradient based learning machines, like neural networks, conditional random fields, logistic regression, etc. Monte...
2
2009-10-04T07:59:46Z
[ "python", "neural-network" ]
Neural Network Example Source-code (preferably Python)
1,514,573
<p>I wonder if anyone has some example code of a Neural network in python. If someone know of some sort of tutorial with a complete walkthrough that would be awesome, but just example source would be great as well!</p> <p>Thanks</p>
11
2009-10-03T19:13:20Z
12,436,399
<p>Found this interresting discusion on ubuntu forums <a href="http://ubuntuforums.org/showthread.php?t=320257" rel="nofollow">http://ubuntuforums.org/showthread.php?t=320257</a></p> <pre><code>import time import random # Learning rate: # Lower = slower # Higher = less precise rate=.2 # Create random weights inWeig...
4
2012-09-15T10:00:25Z
[ "python", "neural-network" ]
Neural Network Example Source-code (preferably Python)
1,514,573
<p>I wonder if anyone has some example code of a Neural network in python. If someone know of some sort of tutorial with a complete walkthrough that would be awesome, but just example source would be great as well!</p> <p>Thanks</p>
11
2009-10-03T19:13:20Z
18,524,373
<p>Here is a probabilistic neural network tutorial :<a href="http://www.youtube.com/watch?v=uAKu4g7lBxU" rel="nofollow">http://www.youtube.com/watch?v=uAKu4g7lBxU</a></p> <p>And my Python Implementation:</p> <pre><code>import math data = {'o' : [(0.2, 0.5), (0.5, 0.7)], 'x' : [(0.8, 0.8), (0.4, 0.5)], ...
2
2013-08-30T03:19:19Z
[ "python", "neural-network" ]
Writing Cocoa applications in Python 3
1,514,638
<p>It looks like PyObjC is not ported to Python 3 yet.</p> <p>Meanwhile is there a way to write Cocoa applications using Python 3?</p> <p>I am intending to start a new MacOSX GUI application project and though5 would want to use Python 3.x instead of Python 2.x.</p>
1
2009-10-03T19:39:32Z
1,514,697
<p>For full-blown Cocoa, I think PyObjC is pretty much the only game in town. If you are coming to Cocoa from a Python background rather than to Python from an Obj-C Cocoa background, surely the learning curve of the Cocoa APIs is <em>much</em> steeper than the differences between Python 2.x and Python 3.x. So I thin...
3
2009-10-03T20:04:32Z
[ "python", "cocoa", "python-3.x", "pyobjc" ]
Display Pretty Code in Django
1,514,874
<p>I'm looking for something I can use within django to display preformatted code. Ideally this would include out-of-the-box syntax highlighting for various programming languages, although just starting with something that displayed html and xml well would be a good starting point.</p> <p>Does something like this exi...
3
2009-10-03T21:11:56Z
1,514,879
<p>You could use <a href="http://pygments.org" rel="nofollow">Pygments</a> to do the syntax highlighting and get HTML to display.</p> <p>Example code :</p> <pre><code>from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter highlighted = highlight('# Some P...
8
2009-10-03T21:13:49Z
[ "python", "html", "django" ]
Display Pretty Code in Django
1,514,874
<p>I'm looking for something I can use within django to display preformatted code. Ideally this would include out-of-the-box syntax highlighting for various programming languages, although just starting with something that displayed html and xml well would be a good starting point.</p> <p>Does something like this exi...
3
2009-10-03T21:11:56Z
1,516,227
<p>I have found SyntaxHighlighter (<a href="http://alexgorbatchev.com" rel="nofollow">http://alexgorbatchev.com</a>) to work well within the Django part of my site.</p>
2
2009-10-04T11:58:42Z
[ "python", "html", "django" ]
Why my code is getting NZEC run time error?
1,515,019
<p><a href="https://www.spoj.pl/problems/ORDERS/" rel="nofollow">Question source: SPOJ.. ORDERS</a></p> <pre><code>def swap(ary,idx1,idx2): tmp = ary[idx1] ary[idx1] = ary[idx2] ary[idx2] = tmp def mkranks(size): tmp = [] for i in range(1, size + 1): tmp = tmp + [i] return tmp def per...
0
2009-10-03T22:28:02Z
1,515,063
<p>Having made your code a bit more Pythonic (but without altering its flow/algorithm):</p> <pre><code>def swap(ary, idx1, idx2): ary[idx1], ary[idx2] = [ary[i] for i in (idx2, idx1)] def permutations(ordered, movements): size = len(ordered) for i in range(1, len(ordered)): for j in range(movement...
1
2009-10-03T22:50:24Z
[ "python" ]
Entity groups in Google App Engine Datastore
1,515,135
<p>So I have an app that if I'm honest doesn't really need transactional integrity (lots of updates, none of them critical). So I was planning on simply leaving entity groups by the wayside for now. But I'd still like to understand it (coming from a relational background).</p> <p>The way I see it, all queries for my...
6
2009-10-03T23:27:57Z
1,652,322
<p>You are essentially correct. You need to group them if you want transactional capability. However, you can group several entities together without creating an actual root entity, in the sense of an entity in the datastore. You instead create a sort of virtual root entity. One important use case of this feature i...
3
2009-10-30T21:05:25Z
[ "python", "google-app-engine", "schemaless" ]
Entity groups in Google App Engine Datastore
1,515,135
<p>So I have an app that if I'm honest doesn't really need transactional integrity (lots of updates, none of them critical). So I was planning on simply leaving entity groups by the wayside for now. But I'd still like to understand it (coming from a relational background).</p> <p>The way I see it, all queries for my...
6
2009-10-03T23:27:57Z
2,016,369
<blockquote> <p>The way I see it, if I want transactions (on a per-user basis), I will need some kind of root user entity as the parent of all entities that are part of the hierarchy of her data, no matter how thin this entity would actually be i.e. basically no properties.</p> </blockquote> <p>I wouldn't just creat...
11
2010-01-06T21:26:32Z
[ "python", "google-app-engine", "schemaless" ]
Simple example of how to use ast.NodeVisitor?
1,515,357
<p>Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree in Python 2.6? The difference between visit and generic_visit is unclear to me, and I cannot find any example using google codesearch or plain google.</p>
31
2009-10-04T01:38:14Z
1,515,380
<p><code>generic_visit</code> is called when a custom visitor (ie visit_Name) can't be found. Here's a piece of code I wrote recently with ast.NodeVisitor: <a href="https://github.com/pypy/pypy/blob/master/py/_code/_assertionnew.py" rel="nofollow">https://github.com/pypy/pypy/blob/master/py/_code/_assertionnew.py</a> I...
5
2009-10-04T01:52:15Z
[ "python", "python-2.6", "abstract-syntax-tree" ]
Simple example of how to use ast.NodeVisitor?
1,515,357
<p>Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree in Python 2.6? The difference between visit and generic_visit is unclear to me, and I cannot find any example using google codesearch or plain google.</p>
31
2009-10-04T01:38:14Z
1,515,403
<p><code>ast.visit</code> -- unless you override it in a subclass, of course -- when called to visit an <code>ast.Node</code> of class <code>foo</code>, calls <code>self.visit_foo</code> if that method exists, otherwise <code>self.generic_visit</code>. The latter, again in its implementation in class <code>ast</code> i...
59
2009-10-04T02:05:23Z
[ "python", "python-2.6", "abstract-syntax-tree" ]
Simple example of how to use ast.NodeVisitor?
1,515,357
<p>Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree in Python 2.6? The difference between visit and generic_visit is unclear to me, and I cannot find any example using google codesearch or plain google.</p>
31
2009-10-04T01:38:14Z
26,374,977
<p>Looking at the code in <a href="https://github.com/python/cpython/blob/master/Lib/ast.py">ast.py</a> it's not that hard to copy paste and roll your own walker. E.g.</p> <pre><code>import ast def str_node(node): if isinstance(node, ast.AST): fields = [(name, str_node(val)) for name, val in ast.iter_field...
7
2014-10-15T05:17:50Z
[ "python", "python-2.6", "abstract-syntax-tree" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,418
<p>You don't need to declare variable types in python; a variable has the type of whatever's assigned to it. </p> <p>EDIT:</p> <p>To solve the re-stated problem, try this:</p> <pre><code>def multiplyItemsByFour(argsList): output = argsList.pop(0) * 4 for arg in argsList: output += arg * 4 return output <...
1
2009-10-04T02:12:44Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,424
<p>No need to test the objects, just multiply away!</p> <pre><code>'this is a string' * 6 14 * 6 [1,2,3] * 6 </code></pre> <p>all just work</p>
0
2009-10-04T02:17:24Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,425
<p>Python is dynamically typed, you don't need to declare the type of a variable, because a variable doesn't have a type, only values do. (Any variable can store any value, a value never changes its type during its lifetime.)</p> <pre><code>def do_something(x): return x * 5 </code></pre> <p>This will work for any...
1
2009-10-04T02:17:38Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,435
<pre><code>def fivetimes(anylist): return anylist * 5 </code></pre> <p>As you see, if you're given a list argument, there's no need for any assignment whatsoever in order to "multiply it a given number of times and return the output". You talk about a <em>given</em> list; how is it <em>given</em> to you, if not (th...
6
2009-10-04T02:26:46Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,493
<p>My guess is that the purpose of your homework is to expose you to "duck typing". The basic idea is that you don't worry about the types too much, you just worry about whether the <em>behaviors</em> work correctly. A classic example:</p> <pre><code>def add_two(a, b): return a + b print add_two(1, 2) # prints...
2
2009-10-04T03:04:56Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,497
<p>Try this:</p> <pre><code>def timesfourlist(list): nextstep = map(times_four, list) sum(nextstep) </code></pre> <p>map performs the function passed in on each element of the list(returning a new list) and then sum does the += on the list.</p>
0
2009-10-04T03:08:21Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,624
<p>Are you sure this is for Python beginners? To me, the cleanest way to do this is with reduce() and lambda, both of which are not typical beginner tools, and sometimes discouraged even for experienced Python programmers:</p> <pre><code>def multiplyItemsByFour(argsList): if not argsList: return None ...
2
2009-10-04T04:48:36Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,738
<p>If you just want to fill in the blank in your code, you could try setting <code>object=arglist[0].__class__()</code> to give it the zero equivalent value of that class.</p> <pre><code>&gt;&gt;&gt; def multiplyItemsByFour(argsList): output = argsList[0].__class__() for arg in argsList: output += ...
0
2009-10-04T06:18:49Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,784
<p>You gave these sample inputs and outputs:</p> <p>Input: ('a','b') Output: 'aaaabbbb' Input: (2,3,4) Output: 36</p> <p>I don't want to write the solution to your homework for you, but I do want to steer you in the correct direction. But I'm still not sure I understand what your problem is, because the problem as I...
1
2009-10-04T06:53:59Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,515,808
<p>Very easy in Python. You need to get the type of the data in your list - use the type() function on the first item - type(argsList[0]). Then to initialize output (where you now have ????) you need the 'zero' or nul value for that type. So just as int() or float() or str() returns the zero or nul for their type so ...
2
2009-10-04T07:09:05Z
[ "python", "types" ]
Declaring Unknown Type Variable in Python?
1,515,412
<p>I have a situation in Python(cough, homework) where I need to multiply EACH ELEMENT in a given list of objects a specified number of times and return the output of the elements. The problem is that the sample inputs given are of different types. For example, one case may input a list of strings whose elements I ne...
2
2009-10-04T02:10:00Z
1,518,443
<p>Thanks to Alex Martelli, you have the best possible solution:</p> <pre><code>def theFinalAndTrulyRealProblemAsPosed(argsList): items = iter(argsList) output = next(items, []) * 4 for item in items: output += item * 4 return output </code></pre> <p>This is beautiful and elegant. First we cr...
0
2009-10-05T05:55:38Z
[ "python", "types" ]
Django 1.0 Testing: how do I get a session to persist between test code and view being tested?
1,515,446
<p>I am trying to test how a view behaves when there is certain data stored in the session. To do so I have created the session in the test method and invoked an interactive shell at the very beginning of the view:</p> <p><strong>Test Method:</strong></p> <pre><code>def test_user_with_unused_tests(self): "User is...
2
2009-10-04T02:31:58Z
8,656,609
<p>Looks like you need a <code>setUp</code> method, please lookup <a href="http://docs.python.org/library/unittest.html" rel="nofollow">http://docs.python.org/library/unittest.html</a> for <code>setUp</code> and <code>tearDown</code> method documentation. The setUp will be run for all tests, the code for each individua...
0
2011-12-28T13:36:23Z
[ "python", "django", "testing" ]
Using Django as a Backend for Cappuccino
1,515,578
<p>I'm new to both Django and Cappuccino. I have a Django site setup and running through Apache via mod_wsgi. I want to use Django as the backend for a Cappuccino application, but a VirtualHost setup in Apache and mod_wsgi to serve a Django application serves static files out of a different location than the normal web...
2
2009-10-04T04:18:21Z
1,515,700
<p>Have you read:</p> <p><a href="http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines" rel="nofollow">http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines</a></p> <p>This goes into various aspects of using WSGIScriptAlias for mod_wsgi and Alias directives for static files.</p> <p>I'd suggest you e...
1
2009-10-04T05:44:47Z
[ "python", "django", "apache", "mod-wsgi", "cappuccino" ]
Using Django as a Backend for Cappuccino
1,515,578
<p>I'm new to both Django and Cappuccino. I have a Django site setup and running through Apache via mod_wsgi. I want to use Django as the backend for a Cappuccino application, but a VirtualHost setup in Apache and mod_wsgi to serve a Django application serves static files out of a different location than the normal web...
2
2009-10-04T04:18:21Z
1,516,924
<p>Here is the configuration I came up with that works:</p> <p>Django Media Settings:</p> <pre><code>MEDIA_ROOT = '/Users/Me/Development/Web Projects/mysite/mysite/public_html' MEDIA_URL = 'http:/mysite.local/' ADMIN_MEDIA_PREFIX = '/' </code></pre> <p>Apache VirtualHost Configuration:</p> <pre><code>&lt;VirtualHos...
0
2009-10-04T17:24:12Z
[ "python", "django", "apache", "mod-wsgi", "cappuccino" ]
How do you convert a string to a function in python?
1,515,601
<p>How do you convert a string (for example, input from a textbox) into a proper function?</p>
1
2009-10-04T04:36:16Z
1,515,603
<p>You can use <code>eval</code>. But be very careful! This opens up lots of security holes.</p> <pre><code>&gt;&gt;&gt; s = '3+4' &gt;&gt;&gt; eval(s) 7 </code></pre> <p>If you want it callable:</p> <pre><code>&gt;&gt;&gt; s = '3+4' &gt;&gt;&gt; f = eval('lambda: ' + s) &gt;&gt;&gt; f() 7 </code></pre> <p>More inf...
6
2009-10-04T04:37:44Z
[ "python" ]
How do you convert a string to a function in python?
1,515,601
<p>How do you convert a string (for example, input from a textbox) into a proper function?</p>
1
2009-10-04T04:36:16Z
1,515,612
<pre><code>def func(): print "hello" # just eval it eval(raw_input()) # if you just want to ask for name fName=raw_input() if fName in globals(): globals()[fName]() </code></pre> <p>And there could be various other ways depending on what is the objective?</p>
0
2009-10-04T04:42:33Z
[ "python" ]
How do you convert a string to a function in python?
1,515,601
<p>How do you convert a string (for example, input from a textbox) into a proper function?</p>
1
2009-10-04T04:36:16Z
5,253,997
<p>try this </p> <pre><code>func = ___import___('func_name') </code></pre> <p>or</p> <pre><code>if hasattr(your_module, func_name): func = getattr(your_module, func_name) </code></pre> <p>or</p> <pre><code>if func_name in globals(): func = globals[func_name] </code></pre> <p>or something etc</p>
1
2011-03-10T00:15:18Z
[ "python" ]
is there any latest version of pyUIQ?
1,515,630
<p>i have tried to install python for symbian uiq 3..i have tried signed and unsigned version,but both installation failed..anyone faced same problem?so,do you have any solution? i am using sony ericsson G900..</p>
1
2009-10-04T04:52:25Z
1,515,667
<p>The latest release version of PyUIQ is 0.2, from YEARS ago, available <a href="http://sourceforge.net/projects/pyuiq/" rel="nofollow">here</a>. No sources in the CVS repo on sourceforge, either... looks pretty much like a dead project to me:-(. What do you need exactly?</p>
1
2009-10-04T05:19:27Z
[ "python", "symbian" ]
is there any latest version of pyUIQ?
1,515,630
<p>i have tried to install python for symbian uiq 3..i have tried signed and unsigned version,but both installation failed..anyone faced same problem?so,do you have any solution? i am using sony ericsson G900..</p>
1
2009-10-04T04:52:25Z
1,516,008
<p>Even though the pyUIQ version available is very old, you should be able to install it if you sign it properly. <a href="https://www.symbiansigned.com/app/page/public/openSignedOnline.do" rel="nofollow">Open Signed Online</a> should do it.</p>
0
2009-10-04T09:23:08Z
[ "python", "symbian" ]
Case insensitive urls for Django?
1,515,634
<p>It seems by default django's url solver perform case sensitive search for solving url and differentiate between '/Login' and 'login'. My url patterns are as follows.</p> <pre><code>urlpatterns = patterns('', (r'^admin/(.*)', admin.site.root), (r'^static/(?P&lt;path&gt;.*)$', 'django.views.static.serve', ...
28
2009-10-04T04:56:10Z
1,515,657
<p>Just put <code>(?i)</code> at the start of every <code>r'...'</code> string, i.e.:</p> <pre><code>urlpatterns = patterns('', (r'^(?i)admin/(.*)', admin.site.root), (r'^(?i)static/(?P&lt;path&gt;.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_DOC_ROOT, 'show_indexes': True}), (r'^(?i)login/...
71
2009-10-04T05:12:44Z
[ "python", "django" ]
Non Blocking Server in Python
1,515,686
<p>Can someone please tell how to write a Non-Blocking server code using the socket library alone.Thanks</p>
0
2009-10-04T05:35:02Z
1,515,694
<p>Not sure what you mean by "socket library alone" - you surely will need other modules from the standard Python library.</p> <p>The lowest level of non-blocking code is the <a href="http://docs.python.org/library/select.html" rel="nofollow">select module</a>. This allows you to have many simultaneous client connecti...
0
2009-10-04T05:42:05Z
[ "python", "sockets", "nonblocking" ]
Non Blocking Server in Python
1,515,686
<p>Can someone please tell how to write a Non-Blocking server code using the socket library alone.Thanks</p>
0
2009-10-04T05:35:02Z
1,515,698
<p>Why <code>socket</code> alone? It's <em>so</em> much simpler to use another standard library module, <code>asyncore</code> -- and if you can't, at the <strong>very</strong> least <code>select</code>!</p> <p>If you're constrained by your homework's condition to only use <code>socket</code>, then I hope you can at le...
2
2009-10-04T05:44:01Z
[ "python", "sockets", "nonblocking" ]
Non Blocking Server in Python
1,515,686
<p>Can someone please tell how to write a Non-Blocking server code using the socket library alone.Thanks</p>
0
2009-10-04T05:35:02Z
1,515,708
<p>Frankly, just don't (unless it's for an exercise). The <a href="http://twistedmatrix.com/">Twisted Framework</a> will do everything network-related for you, so you have to write only your protocol without caring about the transport layer. Writing socket code is not easy, so why not use code somebody else wrote and t...
5
2009-10-04T05:48:39Z
[ "python", "sockets", "nonblocking" ]
Non Blocking Server in Python
1,515,686
<p>Can someone please tell how to write a Non-Blocking server code using the socket library alone.Thanks</p>
0
2009-10-04T05:35:02Z
5,049,206
<p>Use eventlets or gevent. It monkey patches existing libraries. socket module can be used without any changes. Though code appears synchronous, it executes asynchronously.</p> <p>Example: <a href="http://eventlet.net/doc/examples.html#socket-connect" rel="nofollow">http://eventlet.net/doc/examples.html#socket-conne...
0
2011-02-19T06:10:10Z
[ "python", "sockets", "nonblocking" ]
How to remove existing background color of text when highlighting?
1,515,809
<p>I'm writing a small utility in Python that does some pattern matching of text. Text that matches the pattern the user has entered gets highlighted yellow. </p> <p>I'm achieving this using a Tkinter <code>Text</code> widget, and setting up a tag on the Text widget named <code>"match"</code> that gives any text wit...
1
2009-10-04T07:09:10Z
1,516,840
<p>I think you need to set <code>selectbackground</code>, not <code>highlightbackground</code> which means something completely different (the bg color for the "highlight rectangle" drawn around a widget when it gets focus). However, I believe the <code>sel</code> pseudo-tag (representing the selection, which is what I...
0
2009-10-04T16:42:59Z
[ "python", "colors", "background", "tkinter", "highlight" ]
How to remove existing background color of text when highlighting?
1,515,809
<p>I'm writing a small utility in Python that does some pattern matching of text. Text that matches the pattern the user has entered gets highlighted yellow. </p> <p>I'm achieving this using a Tkinter <code>Text</code> widget, and setting up a tag on the Text widget named <code>"match"</code> that gives any text wit...
1
2009-10-04T07:09:10Z
1,516,883
<p>Tags have priority. Tags with a high priority have preference over those that have a lower priority. When you select a range of text it is given the tag "sel". You simply need to raise the priority of the "sel" tag to be above the priority of your "match" tag:</p> <pre><code>w.tag_raise("sel") </code></pre> <p>Ale...
3
2009-10-04T17:04:59Z
[ "python", "colors", "background", "tkinter", "highlight" ]
how to run both python 2.6 and 3.0 on the same windows XP box?
1,515,850
<p>What kind of setup do people use to run both python 2.6 and python 3.0 on the same windows machine?</p>
3
2009-10-04T07:45:12Z
1,515,858
<p>Why do you want to do it? Everything that's in 3.0 is in 2.6, you can use the new features in 3.0 with <code>from __future__ import</code>. Run 2.6 until you're ready to upgrade to 3.0 for everything, then upgrade. You're not intended to have multiple versions of Python on the same machine. If you really want to, yo...
0
2009-10-04T07:51:15Z
[ "python", "windows-xp" ]
how to run both python 2.6 and 3.0 on the same windows XP box?
1,515,850
<p>What kind of setup do people use to run both python 2.6 and python 3.0 on the same windows machine?</p>
3
2009-10-04T07:45:12Z
1,515,859
<p>No problem, each version is installed in its own directory. On my Windows box, I have <code>C:\Python26\</code> and <code>C:\Python31\</code>. The <em>Start Menu</em> items are also distinct. Just use the standard installers from the Python Programming Language <a href="http://www.python.org/download/" rel="nofollow...
9
2009-10-04T07:51:59Z
[ "python", "windows-xp" ]
how to run both python 2.6 and 3.0 on the same windows XP box?
1,515,850
<p>What kind of setup do people use to run both python 2.6 and python 3.0 on the same windows machine?</p>
3
2009-10-04T07:45:12Z
1,515,971
<p>-You could use a batch file to launch with the appropriate version.</p> <p>-Eclipse with pydev allows you to choose which version to launch with.</p> <p>-Re-associate the .py/.pyw extension with your preferred version. </p>
1
2009-10-04T09:00:29Z
[ "python", "windows-xp" ]
how to run both python 2.6 and 3.0 on the same windows XP box?
1,515,850
<p>What kind of setup do people use to run both python 2.6 and python 3.0 on the same windows machine?</p>
3
2009-10-04T07:45:12Z
1,517,045
<p><a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">Virtualenv</a> is the solution of choice on Unix and Mac platforms. </p> <blockquote> <p>virtualenv is a tool to create isolated Python environments.</p> <p>The basic problem being addressed is one of dependencies and versions, and indirec...
1
2009-10-04T18:25:45Z
[ "python", "windows-xp" ]
how to run both python 2.6 and 3.0 on the same windows XP box?
1,515,850
<p>What kind of setup do people use to run both python 2.6 and python 3.0 on the same windows machine?</p>
3
2009-10-04T07:45:12Z
1,517,110
<blockquote> <p>Interesting, but I want to be able to learn the 3.0 syntax (print()) etc but still need to maintain some 2.5 and 2.6 code..</p> </blockquote> <p>Python has <a href="http://docs.python.org/library/%5F%5Ffuture%5F%5F.html" rel="nofollow"><code>__future__</code> "Future statement definitions"</a>, which...
1
2009-10-04T18:55:08Z
[ "python", "windows-xp" ]
Does C++ have an equivilent to Python's __setitem__
1,515,899
<p>Just as the title asks, does C++ have the equivalent of Python's <strong>setitem</strong> and <strong>getitem</strong> for classes?</p> <p>Basically it allows you to do something like the following.</p> <pre><code>MyClass anObject; anObject[0] = 1; anObject[1] = "foo"; </code></pre>
2
2009-10-04T08:18:08Z
1,515,903
<p>basically, you overload the subscript operator (<code>operator[]</code>), and it returns a reference (so it can be read as well as written to)</p>
6
2009-10-04T08:19:58Z
[ "c++", "python" ]
Does C++ have an equivilent to Python's __setitem__
1,515,899
<p>Just as the title asks, does C++ have the equivalent of Python's <strong>setitem</strong> and <strong>getitem</strong> for classes?</p> <p>Basically it allows you to do something like the following.</p> <pre><code>MyClass anObject; anObject[0] = 1; anObject[1] = "foo"; </code></pre>
2
2009-10-04T08:18:08Z
1,515,911
<p>You can overload the [] operator, but it's not quite the same as a separate getitem/setitem method pair, in that you don't get to specify different handling for getting and setting.</p> <p>But you can get close by returning a temporary object that overrides the assignment operator.</p>
6
2009-10-04T08:24:03Z
[ "c++", "python" ]
Does C++ have an equivilent to Python's __setitem__
1,515,899
<p>Just as the title asks, does C++ have the equivalent of Python's <strong>setitem</strong> and <strong>getitem</strong> for classes?</p> <p>Basically it allows you to do something like the following.</p> <pre><code>MyClass anObject; anObject[0] = 1; anObject[1] = "foo"; </code></pre>
2
2009-10-04T08:18:08Z
1,516,083
<p>There's an interesting discussion about what's possible (and what's not) in C++ with subscript operators in <a href="http://www.ddj.com/cpp/184406037" rel="nofollow">this article</a>.</p>
3
2009-10-04T10:10:00Z
[ "c++", "python" ]
Does C++ have an equivilent to Python's __setitem__
1,515,899
<p>Just as the title asks, does C++ have the equivalent of Python's <strong>setitem</strong> and <strong>getitem</strong> for classes?</p> <p>Basically it allows you to do something like the following.</p> <pre><code>MyClass anObject; anObject[0] = 1; anObject[1] = "foo"; </code></pre>
2
2009-10-04T08:18:08Z
1,516,248
<p>To expand on Earwicker post:</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; template &lt;typename Type&gt; class Vector { public: template &lt;typename Element&gt; class ReferenceWrapper { public: explicit ReferenceWrapper(Element&amp; elem) : elem_(elem) ...
1
2009-10-04T12:10:04Z
[ "c++", "python" ]
Does C++ have an equivilent to Python's __setitem__
1,515,899
<p>Just as the title asks, does C++ have the equivalent of Python's <strong>setitem</strong> and <strong>getitem</strong> for classes?</p> <p>Basically it allows you to do something like the following.</p> <pre><code>MyClass anObject; anObject[0] = 1; anObject[1] = "foo"; </code></pre>
2
2009-10-04T08:18:08Z
1,516,272
<p>It's not portable, but MSVC has <a href="http://msdn.microsoft.com/en-us/library/yhfk0thd%28VS.80%29.aspx" rel="nofollow">__declspec(property)</a>, which also allows indexers:</p> <pre><code>struct Foo { void SetFoo(int index, int value) { ... } int GetFoo(int index) { ... } __declspec(property(propget=Ge...
1
2009-10-04T12:19:00Z
[ "c++", "python" ]
How can google app engine report progress back on file upload
1,515,978
<p>I have been trying to use an ajax-style file upload component (like the dojox file upload component) with google app engine, and have a progress bar. </p> <p>The javascript side of things is fine, but i am wondering how to organise things on the server side? </p> <p>Is is possible to access the data as it is being...
0
2009-10-04T09:04:13Z
1,516,097
<p>You can limit file size on a client site using <a href="http://code.google.com/p/swfupload/" rel="nofollow">swfupload</a>. </p> <p>It's a flash component with javascript api that exposes flash uploading capabilities. The check of a file size is done in the client browser. (in function <a href="http://code.google.co...
1
2009-10-04T10:20:48Z
[ "python", "google-app-engine", "progress-bar" ]
How can google app engine report progress back on file upload
1,515,978
<p>I have been trying to use an ajax-style file upload component (like the dojox file upload component) with google app engine, and have a progress bar. </p> <p>The javascript side of things is fine, but i am wondering how to organise things on the server side? </p> <p>Is is possible to access the data as it is being...
0
2009-10-04T09:04:13Z
1,516,405
<p>App Engine won't return any data from the request handler until the after the request is completed, so any progress checking would have to be done client-side. IIRC your request won't even be loaded into the application server until the upload is complete.</p>
1
2009-10-04T13:28:02Z
[ "python", "google-app-engine", "progress-bar" ]
How can google app engine report progress back on file upload
1,515,978
<p>I have been trying to use an ajax-style file upload component (like the dojox file upload component) with google app engine, and have a progress bar. </p> <p>The javascript side of things is fine, but i am wondering how to organise things on the server side? </p> <p>Is is possible to access the data as it is being...
0
2009-10-04T09:04:13Z
4,482,642
<p>This won't work for every browser, but it's a nice way of doing multiple file uploads via AJAX:</p> <pre><code>function upload_files(entityKey, files, url, progress_callback) { var xhr = new XMLHttpRequest(), formData = new FormData(); xhr.upload['onprogress'] = progress_callback; formData.append('entityKey'...
1
2010-12-19T11:07:47Z
[ "python", "google-app-engine", "progress-bar" ]
How to set explicitly the terminal size when using pexpect
1,516,025
<p>I have a ncurses app that checks terminal size at startup and exits immediately if it doesn't fit.</p> <p>In Linux, the default size is 80x24, this app requires at least 25. The fix is easy, I'm just resizing the terminal emulation window (in X) before running the ncurses app.</p> <p>I would like to automate the n...
4
2009-10-04T09:35:43Z
1,516,182
<p>Try setting the LINES and COLUMNS environment variables before you use pexpect.</p> <p>Pexpect will pass on the environment to the subtask, and some (all?) curses programs read these environment variables before starting.</p> <p>eg</p> <pre><code>import os os.environ['LINES'] = "25" os.environ['COLUMNS'] = "80" #...
4
2009-10-04T11:20:19Z
[ "python", "linux", "pexpect" ]
How to set explicitly the terminal size when using pexpect
1,516,025
<p>I have a ncurses app that checks terminal size at startup and exits immediately if it doesn't fit.</p> <p>In Linux, the default size is 80x24, this app requires at least 25. The fix is easy, I'm just resizing the terminal emulation window (in X) before running the ncurses app.</p> <p>I would like to automate the n...
4
2009-10-04T09:35:43Z
10,946,444
<p>You can also use :</p> <pre><code>import pexpect child = pexpect.spawn(cmd) child.setwinsize(400,400) </code></pre>
3
2012-06-08T09:41:01Z
[ "python", "linux", "pexpect" ]
chess AI for GAE
1,516,223
<p>I am looking for a Chess AI that can be run on Google App Engine. Most chess AI's seem to be written in C and so can not be run on the GAE. It needs to be strong enough to beat a casual player, but efficient enough that it can calculate a move within a single request (less than 10 secs).</p> <p>Ideally it would be ...
5
2009-10-04T11:57:20Z
1,516,512
<p>This problem is a poor match for the GAE architecture, which is designed for efficient CRUD operations, and not CPU-intensive tasks. In practice, anything that takes more than a few tens of milliseconds per request will blow out your CPU quota pretty quickly.</p>
1
2009-10-04T14:14:28Z
[ "python", "google-app-engine", "artificial-intelligence", "chess" ]
chess AI for GAE
1,516,223
<p>I am looking for a Chess AI that can be run on Google App Engine. Most chess AI's seem to be written in C and so can not be run on the GAE. It needs to be strong enough to beat a casual player, but efficient enough that it can calculate a move within a single request (less than 10 secs).</p> <p>Ideally it would be ...
5
2009-10-04T11:57:20Z
1,516,519
<p>What's wrong with <a href="http://pychess.googlepages.com/" rel="nofollow">PyChess</a>? It's pure Python, fairly mature, and will certainly be able to beat a casual player.</p> <p>It's been a while since I've used PyChess, but a quick glance through <a href="http://code.google.com/p/pychess/source/browse/trunk/lib...
5
2009-10-04T14:18:27Z
[ "python", "google-app-engine", "artificial-intelligence", "chess" ]
Python: List Sorting with Multiple Attributes and Mixed Order
1,516,249
<p>I have to sort a python list, with multiple attributes. I can do that in ascending order for ALL attributes easily with</p> <pre><code>L.sort(key=operator.attrgetter(attribute)).... </code></pre> <p>but the problem is, that I have use mixed configurations for ascending/descending... I have to "imitate" a bit the S...
12
2009-10-04T12:10:07Z
1,516,265
<p>You can't, but writing the compare function is easy:</p> <pre><code>def my_cmp(a, b): return cmp(a.foo, b.foo) or cmp(b.bar, a.bar) L.sort(my_cmp) </code></pre>
4
2009-10-04T12:16:59Z
[ "python", "list", "sorting" ]
Python: List Sorting with Multiple Attributes and Mixed Order
1,516,249
<p>I have to sort a python list, with multiple attributes. I can do that in ascending order for ALL attributes easily with</p> <pre><code>L.sort(key=operator.attrgetter(attribute)).... </code></pre> <p>but the problem is, that I have use mixed configurations for ascending/descending... I have to "imitate" a bit the S...
12
2009-10-04T12:10:07Z
1,516,290
<p>A custom function will render your code more readable. If you have many sorting operations and you don't want to create those functions though, you can use lambda's:</p> <pre><code>L.sort(lambda x, y: cmp(x.name, y.name) or -cmp(x.year, y.year)) </code></pre>
6
2009-10-04T12:26:50Z
[ "python", "list", "sorting" ]