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 |
|---|---|---|---|---|---|---|---|---|---|
WxPython differences between Windows and Linux | 916,987 | <p>The tutorials I've found on WxPython all use examples from Linux, but there seem to be differences in some details.</p>
<p>For example, in Windows a Panel behind the widgets is mandatory to show the background properly. Additionally, some examples that look fine in the tutorials don't work in my computer.</p>
<p>S... | 1 | 2009-05-27T17:16:34Z | 935,519 | <p>I find a number of small differences, but don't remember all of them. Here are two:</p>
<p>1) The layout can be slightly different, for example, causing things to not completely fit in the window in one OS when the do in the other. I haven't investigated the reasons for this, but it happens most often when I use ... | 0 | 2009-06-01T16:11:54Z | [
"python",
"windows",
"linux",
"user-interface",
"wxpython"
] |
Sorting a tuple that contains lists | 917,202 | <p>I have a similar question to <a href="http://stackoverflow.com/questions/222752/sorting-a-tuple-that-contains-tuples">this one</a> but instead my tuple contains lists, as follows:</p>
<pre><code>mytuple = (
["tomato", 3],
["say", 2],
["say", 5],
["I", 4],
["you", 1],
["tomato", 6],
)
</code></pre>
<p>What's ... | 0 | 2009-05-27T18:09:44Z | 917,225 | <p>You will have to instantiate a new tuple, unfortunately: something like</p>
<pre><code>mytuple = sorted(mytuple)
</code></pre>
<p>should do the trick. <code>sorted</code> won't return a tuple, though. wrap the call in <code>tuple()</code> if you need that. This could potentially be costly if the data set is lon... | 1 | 2009-05-27T18:13:59Z | [
"python",
"list",
"sorting",
"tuples"
] |
Sorting a tuple that contains lists | 917,202 | <p>I have a similar question to <a href="http://stackoverflow.com/questions/222752/sorting-a-tuple-that-contains-tuples">this one</a> but instead my tuple contains lists, as follows:</p>
<pre><code>mytuple = (
["tomato", 3],
["say", 2],
["say", 5],
["I", 4],
["you", 1],
["tomato", 6],
)
</code></pre>
<p>What's ... | 0 | 2009-05-27T18:09:44Z | 917,226 | <p>The technique used in the accepted answer to that question (<code>sorted(..., key=itemgetter(...))</code>) should work with any iterable of this kind. Based on the data you present here, I think the exact solution presented there is what you want.</p>
| 1 | 2009-05-27T18:14:09Z | [
"python",
"list",
"sorting",
"tuples"
] |
Sorting a tuple that contains lists | 917,202 | <p>I have a similar question to <a href="http://stackoverflow.com/questions/222752/sorting-a-tuple-that-contains-tuples">this one</a> but instead my tuple contains lists, as follows:</p>
<pre><code>mytuple = (
["tomato", 3],
["say", 2],
["say", 5],
["I", 4],
["you", 1],
["tomato", 6],
)
</code></pre>
<p>What's ... | 0 | 2009-05-27T18:09:44Z | 917,238 | <p>You can get a sorted tuple easy enough:</p>
<pre><code>>>> sorted(mytuple)
[['I', 4], ['say', 2], ['say', 5], ['tomato', 3], ['tomato', 6], ['you', 1]]
</code></pre>
<p>This will sort based on the items in the list. If the first two match, it compares the second, etc.</p>
<p>If you have a different crite... | 7 | 2009-05-27T18:16:04Z | [
"python",
"list",
"sorting",
"tuples"
] |
Sorting a tuple that contains lists | 917,202 | <p>I have a similar question to <a href="http://stackoverflow.com/questions/222752/sorting-a-tuple-that-contains-tuples">this one</a> but instead my tuple contains lists, as follows:</p>
<pre><code>mytuple = (
["tomato", 3],
["say", 2],
["say", 5],
["I", 4],
["you", 1],
["tomato", 6],
)
</code></pre>
<p>What's ... | 0 | 2009-05-27T18:09:44Z | 917,615 | <p>You cannot sort a tuple. </p>
<p>What you can do is use <a href="http://docs.python.org/library/functions.html?highlight=sorted#sorted">sorted()</a> which will not sort the tuple, but will create a sorted list from your tuple. If you really need a sorted tuple, you can then cast the return from sorted as a tuple:</... | 5 | 2009-05-27T19:38:07Z | [
"python",
"list",
"sorting",
"tuples"
] |
Does Ruby have an equivalent of Python's twisted framework as a networking abstraction layer? | 917,369 | <p>From my understanding, Python's twisted framework provides a higher-level abstraction for networking communications (?). </p>
<p>I am looking for a Ruby equivalent of twisted to use in a Rails application.</p>
| 6 | 2009-05-27T18:44:57Z | 917,538 | <p>Take a look at <a href="http://eventmachine.rubyforge.org/">EventMachine</a>. It's not as extensive as Twisted but it's built around the same concepts of event-driven network programming.</p>
| 7 | 2009-05-27T19:17:45Z | [
"python",
"ruby-on-rails",
"ruby",
"twisted"
] |
how to write nslookup programmatically? | 917,619 | <p>instead of using exec in our script to do an nslookup, is there an easy way to write it programmatically in PHP, Python, or Ruby?</p>
| 2 | 2009-05-27T19:39:03Z | 917,650 | <p>For Python see <a href="http://small-code.blogspot.com/2008/05/nslookup-in-python.html" rel="nofollow">http://small-code.blogspot.com/2008/05/nslookup-in-python.html</a> . For much richer functionality, also in Python, see <a href="http://www.dnspython.org/" rel="nofollow">http://www.dnspython.org/</a> .</p>
| 1 | 2009-05-27T19:44:07Z | [
"php",
"python",
"ruby",
"networking",
"nslookup"
] |
how to write nslookup programmatically? | 917,619 | <p>instead of using exec in our script to do an nslookup, is there an easy way to write it programmatically in PHP, Python, or Ruby?</p>
| 2 | 2009-05-27T19:39:03Z | 917,651 | <p><a href="http://www.ruby-doc.org/stdlib/libdoc/socket/rdoc/classes/Socket.html#M004531" rel="nofollow">Socket Class</a> in Ruby. See this <a href="http://stackoverflow.com/questions/2993/reverse-dns-in-ruby/3012#3012">answer</a>.</p>
| 1 | 2009-05-27T19:44:15Z | [
"php",
"python",
"ruby",
"networking",
"nslookup"
] |
how to write nslookup programmatically? | 917,619 | <p>instead of using exec in our script to do an nslookup, is there an easy way to write it programmatically in PHP, Python, or Ruby?</p>
| 2 | 2009-05-27T19:39:03Z | 917,658 | <p>Yes, although the function names might not be what you expect.</p>
<p>Since the Python and Ruby answers are already posted, here is an PHP example:</p>
<pre><code>$ip = gethostbyname('www.example.com');
$hostname = gethostbyaddr('127.0.0.1');
</code></pre>
| 4 | 2009-05-27T19:45:41Z | [
"php",
"python",
"ruby",
"networking",
"nslookup"
] |
how to write nslookup programmatically? | 917,619 | <p>instead of using exec in our script to do an nslookup, is there an easy way to write it programmatically in PHP, Python, or Ruby?</p>
| 2 | 2009-05-27T19:39:03Z | 917,691 | <p>For PHP, you can use <a href="http://php.net/gethostbyname" rel="nofollow">gethostbyname</a> and <a href="http://php.net/gethostbyaddr" rel="nofollow">gethostbyaddr</a>.</p>
<p>For Python, import the socket module and again use <a href="http://docs.python.org/library/socket.html#socket.gethostbyname" rel="nofollow"... | 1 | 2009-05-27T19:51:04Z | [
"php",
"python",
"ruby",
"networking",
"nslookup"
] |
how to write nslookup programmatically? | 917,619 | <p>instead of using exec in our script to do an nslookup, is there an easy way to write it programmatically in PHP, Python, or Ruby?</p>
| 2 | 2009-05-27T19:39:03Z | 36,620,700 | <pre><code>$ip = gethostbyname('www.example.com');
</code></pre>
<p>This will work but please be aware that it will be affected if the user changes their hosts file. You can't rely on it. </p>
| 0 | 2016-04-14T10:42:27Z | [
"php",
"python",
"ruby",
"networking",
"nslookup"
] |
Reinstall /Library/Python on OS X Leopard | 917,876 | <p>I accidentally removed /Library/Python on OS X Leopard. How can I reinstall that?</p>
| 0 | 2009-05-27T20:31:50Z | 917,890 | <p>If you'd like, I'll create a tarball from a pristine installation. I'm using MacOSX 10.5.7, and only 12K.</p>
| 1 | 2009-05-27T20:34:52Z | [
"python",
"osx",
"osx-leopard",
"reinstall"
] |
Reinstall /Library/Python on OS X Leopard | 917,876 | <p>I accidentally removed /Library/Python on OS X Leopard. How can I reinstall that?</p>
| 0 | 2009-05-27T20:31:50Z | 917,896 | <p>I'm using 10.4, but unless the installation changed dramatically in 10.5, <code>/Library/Python</code> is just a place to install local (user-installed) packages; the actual Python install is under <code>/System</code>. On 10.4, I have the following structure:</p>
<pre><code>/Library/
Python/
2.3/
... | 1 | 2009-05-27T20:36:08Z | [
"python",
"osx",
"osx-leopard",
"reinstall"
] |
Reinstall /Library/Python on OS X Leopard | 917,876 | <p>I accidentally removed /Library/Python on OS X Leopard. How can I reinstall that?</p>
| 0 | 2009-05-27T20:31:50Z | 917,897 | <p><code>/Library/Python</code> contains your python site-packages, which is the local software you've installed using commands like <code>python setup.py install</code>. The pieces here are third-party packages, not items installed by Apple - your actual Python installation is still safe in /System/Library/etc... </p>... | 1 | 2009-05-27T20:36:51Z | [
"python",
"osx",
"osx-leopard",
"reinstall"
] |
How do I filter by time in a date time field? | 917,996 | <p>In the model 'entered' is a datetime field. I want to query the data to find all entry's that where made between noon(start_time) and 5:00pm (end_time).</p>
<pre><code>selected = Entry.objects.filter(entered__gte=start_time, entered__lte=end_time)
</code></pre>
<p>(as I expected)I get an error of:</p>
<pre><code>... | 5 | 2009-05-27T21:03:17Z | 918,071 | <p>I don't believe there's built-in support for this, but you can pass <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#extra-select-none-where-none-params-none-tables-none-order-by-none-select-params-none">extra where-clause</a> parameters (warning: some possibility of introducing DB-dependent behav... | 5 | 2009-05-27T21:21:34Z | [
"python",
"django"
] |
How do I filter by time in a date time field? | 917,996 | <p>In the model 'entered' is a datetime field. I want to query the data to find all entry's that where made between noon(start_time) and 5:00pm (end_time).</p>
<pre><code>selected = Entry.objects.filter(entered__gte=start_time, entered__lte=end_time)
</code></pre>
<p>(as I expected)I get an error of:</p>
<pre><code>... | 5 | 2009-05-27T21:03:17Z | 918,322 | <p>Did you provide <a href="http://docs.python.org/library/datetime.html#datetime-objects" rel="nofollow">datetime objects</a> for <code>start_time</code> and <code>end_time</code>?</p>
<p>A quick try-out:</p>
<pre><code>class Entry(models.Model):
entered = models.DateTimeField()
>>> from datetime impor... | 0 | 2009-05-27T22:31:01Z | [
"python",
"django"
] |
How do I filter by time in a date time field? | 917,996 | <p>In the model 'entered' is a datetime field. I want to query the data to find all entry's that where made between noon(start_time) and 5:00pm (end_time).</p>
<pre><code>selected = Entry.objects.filter(entered__gte=start_time, entered__lte=end_time)
</code></pre>
<p>(as I expected)I get an error of:</p>
<pre><code>... | 5 | 2009-05-27T21:03:17Z | 921,083 | <p>Using SQLite as an example, a relatively clean and generic solution would be:</p>
<pre><code>Entry.objects.extra(where=["time(entered) between '%s' and '%s'"],
params=[start_time.strftime("%H:%M"), end_time.strftime("%H:%M")])
</code></pre>
| 3 | 2009-05-28T14:19:16Z | [
"python",
"django"
] |
How do I filter by time in a date time field? | 917,996 | <p>In the model 'entered' is a datetime field. I want to query the data to find all entry's that where made between noon(start_time) and 5:00pm (end_time).</p>
<pre><code>selected = Entry.objects.filter(entered__gte=start_time, entered__lte=end_time)
</code></pre>
<p>(as I expected)I get an error of:</p>
<pre><code>... | 5 | 2009-05-27T21:03:17Z | 921,139 | <p>You could filter in python instead using the db's mechanisms:</p>
<pre><code>for e in Entry.objects.all():
if i.entered.hour>= 9 and i.entered.hour < 17 :# or break down to minutes/seconds
list.append(e)
</code></pre>
<p>but both solutions are ugly, i think.</p>
<p>Steve, you have to decide, what... | 2 | 2009-05-28T14:28:59Z | [
"python",
"django"
] |
How do I filter by time in a date time field? | 917,996 | <p>In the model 'entered' is a datetime field. I want to query the data to find all entry's that where made between noon(start_time) and 5:00pm (end_time).</p>
<pre><code>selected = Entry.objects.filter(entered__gte=start_time, entered__lte=end_time)
</code></pre>
<p>(as I expected)I get an error of:</p>
<pre><code>... | 5 | 2009-05-27T21:03:17Z | 13,894,806 | <p>I suggest filter <code>x<=arg</code> and exclude <code>x>=arg2</code>.</p>
| 0 | 2012-12-15T18:10:45Z | [
"python",
"django"
] |
How do I filter by time in a date time field? | 917,996 | <p>In the model 'entered' is a datetime field. I want to query the data to find all entry's that where made between noon(start_time) and 5:00pm (end_time).</p>
<pre><code>selected = Entry.objects.filter(entered__gte=start_time, entered__lte=end_time)
</code></pre>
<p>(as I expected)I get an error of:</p>
<pre><code>... | 5 | 2009-05-27T21:03:17Z | 21,847,795 | <p>You can use range to filter in between. I found this the best way to filter since SQL does filtering better than Django. </p>
<pre><code>fromRange=datetime.datetime.strptime(request.GET.get('from'),'%Y-%m-%dT%H:%M:%S.%fZ')
toRange=datetime.datetime.strptime(request.GET.get('to'),'%Y-%m-%dT%H:%M:%S.%fZ')
entry = En... | -1 | 2014-02-18T08:17:43Z | [
"python",
"django"
] |
How do I filter by time in a date time field? | 917,996 | <p>In the model 'entered' is a datetime field. I want to query the data to find all entry's that where made between noon(start_time) and 5:00pm (end_time).</p>
<pre><code>selected = Entry.objects.filter(entered__gte=start_time, entered__lte=end_time)
</code></pre>
<p>(as I expected)I get an error of:</p>
<pre><code>... | 5 | 2009-05-27T21:03:17Z | 35,946,485 | <p>I just figured out a solution for a similar use case â perhaps someone finds this useful:</p>
<p>In my case, I have Availability and Constraint models like so (simplified):</p>
<pre><code>class Availability(...):
start = models.DateTimeField()
end = models.DateTimeField()
class Constraint(...):
fro... | 0 | 2016-03-11T17:36:07Z | [
"python",
"django"
] |
Is there a way in Python to index a list of containers (tuples, lists, dictionaries) by an element of a container? | 918,076 | <p>I have been poking around for a recipe / example to index a list of tuples without taking a modification of the decorate, sort, undecorate approach. </p>
<p>For example:</p>
<pre><code>l=[(a,b,c),(x,c,b),(z,c,b),(z,c,d),(a,d,d),(x,d,c) . . .]
</code></pre>
<p>The approach I have been using is to build a dictiona... | 1 | 2009-05-27T21:22:43Z | 918,115 | <p>If i think this is what you're asking...</p>
<pre><code>l = ['asd', 'asdxzc']
d = {}
for i, x in enumerate(l):
d[x] = {'index': i}
</code></pre>
| 0 | 2009-05-27T21:32:25Z | [
"python",
"indexing",
"list",
"containers"
] |
Is there a way in Python to index a list of containers (tuples, lists, dictionaries) by an element of a container? | 918,076 | <p>I have been poking around for a recipe / example to index a list of tuples without taking a modification of the decorate, sort, undecorate approach. </p>
<p>For example:</p>
<pre><code>l=[(a,b,c),(x,c,b),(z,c,b),(z,c,d),(a,d,d),(x,d,c) . . .]
</code></pre>
<p>The approach I have been using is to build a dictiona... | 1 | 2009-05-27T21:22:43Z | 918,160 | <p>This will generate the result you want</p>
<pre><code>dict((myTuple[1], index) for index, myTuple in enumerate(l))
>>> l = [(1, 2, 3), (4, 5, 6), (1, 4, 6)]
>>> dict((myTuple[1], index) for index, myTuple in enumerate(l))
{2: 0, 4: 2, 5: 1}
</code></pre>
<p>And if you insist on using a dictionar... | 5 | 2009-05-27T21:44:16Z | [
"python",
"indexing",
"list",
"containers"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 918,174 | <p>See <a href="http://docs.python.org/library/sys.html#sys.path">sys.path</a>
As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. </p>
<p>Use this path as the root folder from which you <a href="http://docs.p... | 6 | 2009-05-27T21:47:16Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 918,178 | <p>In the file that has the script, you want to do something like this:</p>
<pre><code>import os
dir = os.path.dirname(__file__)
filename = os.path.join(dir, '/relative/path/to/file/you/want')
</code></pre>
<p>This will give you the absolute path to the file you're looking for. Note that if you're using setuptools, ... | 94 | 2009-05-27T21:47:50Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 9,768,491 | <p>you need <code>os.path.realpath</code> (sample below adds the parent directory to your path)</p>
<pre><code>import sys,os
sys.path.append(os.path.realpath('..'))
</code></pre>
| 33 | 2012-03-19T10:24:27Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 20,437,590 | <p>Hi first of all you should understand functions <strong>os.path.abspath(path)</strong> and <strong>os.path.relpath(path)</strong> </p>
<p>In short <strong>os.path.abspath(path)</strong> makes a <strong>relative path</strong> to <strong>absolute path</strong>. And if the path provided is itself a absolute path then ... | -1 | 2013-12-07T04:30:00Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 24,040,518 | <p>I'm not sure if this applies to some of the older versions, but I believe Python 3.3 has native relative path support.</p>
<p>For example the following code should create a text file in the same folder as the python script:</p>
<pre><code>open("text_file_name.txt", "w+t")
</code></pre>
<p>(note that there shouldn... | -2 | 2014-06-04T14:41:21Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 26,295,955 | <p>An alternative which works for me: </p>
<pre><code>this_dir = os.path.dirname(__file__)
filename = os.path.realpath("{0}/relative/file.path".format(this_dir))
</code></pre>
| 0 | 2014-10-10T09:19:19Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 32,973,614 | <p>Consider my code:</p>
<pre><code>import os
def readFile(filename):
filehandle = open(filename)
print filehandle.read()
filehandle.close()
fileDir = os.path.dirname(os.path.realpath('__file__'))
print fileDir
#For accessing the file in the same folder
filename = "same.txt"
readFile(filename)
#For ... | 3 | 2015-10-06T15:17:35Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 35,492,963 | <p>Instead of using </p>
<pre><code>import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, '/relative/path/to/file/you/want')
</code></pre>
<p>as in the accepted answer, it would be more robust to use:</p>
<pre><code>import inspect
import os
dirname = os.path.dirname(os.path.abspath(inspect.s... | 2 | 2016-02-18T21:41:34Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 36,209,089 | <p>This code will return the absolute path to the main script.</p>
<pre><code>import os
def whereAmI():
return os.path.dirname(os.path.realpath(__import__("__main__").__file__))
</code></pre>
<p>This will work even in a module.</p>
| 0 | 2016-03-24T20:03:52Z | [
"python",
"relative-path",
"path"
] |
Relative paths in Python | 918,154 | <p>I'm building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don't, however, have the absolute path to the directory where the templates are stored. I do have a relative path from the script but when I call the script it treats that as a path rel... | 71 | 2009-05-27T21:43:21Z | 36,906,785 | <p>As mentioned in the accepted answer <br></p>
<pre><code>import os
dir = os.path.dirname(__file__)
filename = os.path.join(dir, '/relative/path/to/file/you/want')
</code></pre>
<p>I just want to add that</p>
<blockquote>
<p>the latter string can't begin with the backslash , infact no string
should include a ba... | 5 | 2016-04-28T06:22:44Z | [
"python",
"relative-path",
"path"
] |
Swig / Python memory leak detected | 918,180 | <p>I have a very complicated class for which I'm attempting to make Python wrappers in SWIG. When I create an instance of the item in Python, however, I'm unable to initialize certain data members without receiving the message:</p>
<pre><code>>>> myVar = myModule.myDataType()
swig/python detected a memory lea... | 5 | 2009-05-27T21:48:03Z | 918,814 | <p>The error message is pretty clear to me, you need to define a destructor for this type. </p>
| -7 | 2009-05-28T01:37:46Z | [
"python",
"memory-leaks",
"swig"
] |
Swig / Python memory leak detected | 918,180 | <p>I have a very complicated class for which I'm attempting to make Python wrappers in SWIG. When I create an instance of the item in Python, however, I'm unable to initialize certain data members without receiving the message:</p>
<pre><code>>>> myVar = myModule.myDataType()
swig/python detected a memory lea... | 5 | 2009-05-27T21:48:03Z | 920,988 | <p>SWIG always generates destructor wrappers (unless <code>%nodefaultdtor</code> directive is used). However, in case where it doesn't know anything about a type, it will generate an opaque pointer wrapper, which will cause leaks (and the above message).</p>
<p>Please check that <code>myDataType</code> is a type that ... | 5 | 2009-05-28T13:59:41Z | [
"python",
"memory-leaks",
"swig"
] |
Python unicode in Mac os X terminal | 918,294 | <p>Can someone explain to me this odd thing:</p>
<p>When in python shell I type the following Cyrillic string:</p>
<pre><code>>>> print 'абвгд'
абвгд
</code></pre>
<p>but when I type:</p>
<pre><code>>>> print u'абвгд'
Traceback (most recent call last):
File "<stdin>", line 1... | 7 | 2009-05-27T22:17:54Z | 918,312 | <p>A unicode object needs to be encoded before it can be displayed on some consoles. Try</p>
<pre><code>u'абвгд'.encode()
</code></pre>
<p>instead to encode the unicode to a string object (most likely using utf8 as a default encoding, but depends on your python config)</p>
| 0 | 2009-05-27T22:28:32Z | [
"python",
"osx",
"unicode",
"terminal"
] |
Python unicode in Mac os X terminal | 918,294 | <p>Can someone explain to me this odd thing:</p>
<p>When in python shell I type the following Cyrillic string:</p>
<pre><code>>>> print 'абвгд'
абвгд
</code></pre>
<p>but when I type:</p>
<pre><code>>>> print u'абвгд'
Traceback (most recent call last):
File "<stdin>", line 1... | 7 | 2009-05-27T22:17:54Z | 918,321 | <p>Also, make sure the terminal encoding is set to Unicode/UTF-8 (and not ascii, which seems to be your setting):</p>
<p><a href="http://www.rift.dk/news.php?item.7.6" rel="nofollow">http://www.rift.dk/news.php?item.7.6</a></p>
| 2 | 2009-05-27T22:30:55Z | [
"python",
"osx",
"unicode",
"terminal"
] |
Python unicode in Mac os X terminal | 918,294 | <p>Can someone explain to me this odd thing:</p>
<p>When in python shell I type the following Cyrillic string:</p>
<pre><code>>>> print 'абвгд'
абвгд
</code></pre>
<p>but when I type:</p>
<pre><code>>>> print u'абвгд'
Traceback (most recent call last):
File "<stdin>", line 1... | 7 | 2009-05-27T22:17:54Z | 918,364 | <p>In addition to ensuring your OS X terminal is set to UTF-8, you may wish to set your python sys default encoding to UTF-8 or better. Create a file in <code>/Library/Python/2.5/site-packages</code> called <code>sitecustomize.py</code>. In this file put:</p>
<pre><code>import sys
sys.setdefaultencoding('utf-8')
</cod... | 8 | 2009-05-27T22:52:42Z | [
"python",
"osx",
"unicode",
"terminal"
] |
Python unicode in Mac os X terminal | 918,294 | <p>Can someone explain to me this odd thing:</p>
<p>When in python shell I type the following Cyrillic string:</p>
<pre><code>>>> print 'абвгд'
абвгд
</code></pre>
<p>but when I type:</p>
<pre><code>>>> print u'абвгд'
Traceback (most recent call last):
File "<stdin>", line 1... | 7 | 2009-05-27T22:17:54Z | 918,425 | <pre><code>>>> print 'абвгд'
абвгд
</code></pre>
<p>When you type in some characters, your terminal decides how these characters are represented to the application. Your terminal might give the characters to the application encoded as utf-8, ISO-8859-5 or even something that only your terminal under... | 16 | 2009-05-27T23:12:40Z | [
"python",
"osx",
"unicode",
"terminal"
] |
Python unicode in Mac os X terminal | 918,294 | <p>Can someone explain to me this odd thing:</p>
<p>When in python shell I type the following Cyrillic string:</p>
<pre><code>>>> print 'абвгд'
абвгд
</code></pre>
<p>but when I type:</p>
<pre><code>>>> print u'абвгд'
Traceback (most recent call last):
File "<stdin>", line 1... | 7 | 2009-05-27T22:17:54Z | 918,437 | <p>'абвгд' is not a unicode string</p>
<p>u'абвгд' is a unicode string</p>
<p>You cannot print unicode strings without encoding them. When you are dealing with strings in your application you want to make sure that any input is decoded and any output in encoded. This way your application will deal only wi... | 0 | 2009-05-27T23:14:40Z | [
"python",
"osx",
"unicode",
"terminal"
] |
Python unicode in Mac os X terminal | 918,294 | <p>Can someone explain to me this odd thing:</p>
<p>When in python shell I type the following Cyrillic string:</p>
<pre><code>>>> print 'абвгд'
абвгд
</code></pre>
<p>but when I type:</p>
<pre><code>>>> print u'абвгд'
Traceback (most recent call last):
File "<stdin>", line 1... | 7 | 2009-05-27T22:17:54Z | 9,930,894 | <p>As of Python 2.6, you can use the environment variable <code>PYTHONIOENCODING</code> to tell Python that your terminal is UTF-8 capable. The easiest way to make this permanent is by adding the following line to your <code>~/.bash_profile</code>:</p>
<pre><code>export PYTHONIOENCODING=utf-8
</code></pre>
<p><img sr... | 12 | 2012-03-29T18:05:07Z | [
"python",
"osx",
"unicode",
"terminal"
] |
Are asynchronous Django model queries possible? | 918,298 | <p>I'm new to Django, but the application that I have in mind might end up having URLs that look like this:</p>
<pre><code>http://mysite/compare/id_1/id_2
</code></pre>
<p>Where "id_1" and "id_2" are identifiers of two distinct Model objects. In the handler for "compare" I'd like to asynchronously, and in parallel, ... | 18 | 2009-05-27T22:19:00Z | 918,410 | <p>There aren't strictly asynchronous operations as you've described, but I think you can achieve the same effect by using django's <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#in-bulk">in_bulk</a> query operator, which takes a list of ids to query.</p>
<p>Something like this for the <code>urls.... | 11 | 2009-05-27T23:07:42Z | [
"python",
"mysql",
"django",
"multithreading",
"django-models"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,365 | <p>You need to run it multiple times to get a real idea of "how fast" each runs. The JVM startup time [for one] is adding to the single running time of the Java version.</p>
<p>You're also creating a HashSet with a large initial capacity, which means the backing HashMap will be created with that many available slots,... | 2 | 2009-05-27T22:53:09Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,367 | <p>I'm not too familiar with python, but I do know <code>HashSet</code> can't contain primitives, so when you say <code>counts.add(i)</code> the <code>i</code> there is getting autoboxed into a <code>new Integer(i)</code> call. That's probably your problem. </p>
<p>If for some reason you really needed a 'set' of int... | 3 | 2009-05-27T22:54:07Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,370 | <p>Are you using the -server flag with the jvm? You can't test for performance without it. (You also have to warm up the jvm before doing the test.)</p>
<p>Also, you probably want to use <code>TreeSet<Integer></code>. HashSet will be slower in the long run.</p>
<p>And which jvm are you using? The newest I h... | 2 | 2009-05-27T22:56:58Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,385 | <p>I agree with Gandalf about the startup time. Also, you are allocating a huge HashSet which is not at all similar to your python code. I imaging if you put this under a profiler, a good chunk of time would be spent there. Also, inserting new elements is really going to be slow with this size. I would look into Tr... | 0 | 2009-05-27T23:01:27Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,397 | <p>It has generally been my experience that python programs run faster than java programs, despite the fact that java is a bit "lower level" language. Incidently, both languages are compiled into byte code (that's what those .pyc file are -- you can think of them as kind of like .class files). Both languages are by... | 7 | 2009-05-27T23:04:37Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,409 | <p>Another possible explanation is that sets in Python are implemented natively in C code, while HashSet's in Java are implemented in Java itself. So, sets in Python should be inherently much faster.</p>
| 7 | 2009-05-27T23:06:52Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,432 | <p>How much memory did you start the JVM with? It depends? When I run the JVM with your program with 1 Gig of RAM:</p>
<pre><code>$ java -Xmx1024M -Xms1024M -classpath . SpeedTest
TOTAL TIME = 5.682
10000000
$ python speedtest.py
total time = 4.48310899734
10000000
</code></pre>
<p>If I run the JVM with less memo... | 1 | 2009-05-27T23:14:12Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,455 | <p>I find benchmarks like this to be meaningless. I don't solve problems that look like the test case. It's not terribly interesting.</p>
<p>I'd much rather see a solution for a meaningful linear algebra solution using NumPy and JAMA. Maybe I'll try it and report back with results.</p>
| 4 | 2009-05-27T23:20:36Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,456 | <p>There's a number of issues here which I'd like to bring together.</p>
<p>First if it's a program that you are only going to run once, does it matter it takes an extra few seconds?</p>
<p>Secondly, this is just one microbenchmarks. Microbenchmarks are pointless for comparing performance.</p>
<p>Startup has a numbe... | 3 | 2009-05-27T23:21:41Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,467 | <p>The biggest issue is probably that what the given code measures is <a href="http://en.wikipedia.org/wiki/Wall%5Ftime" rel="nofollow">wall time</a> -- what your watch measures -- but what should be measured to compare code runtime is <a href="http://en.wikipedia.org/wiki/Process%5Ftime" rel="nofollow">process time</a... | 0 | 2009-05-27T23:24:43Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,501 | <p><strong>Edit: A TreeSet might be faster for the real use case, depending on allocation patterns. My comments below deals only with this simplified scenario. However, I do not believe that it would make a very significant difference. The real issue lays elsewhere.</strong> </p>
<p>Several people here has recommended... | 5 | 2009-05-27T23:33:12Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,572 | <p>You can make the Java microbenchamrk much faster, by <strong>adding</strong> just a simple little extra.</p>
<pre><code> HashSet counts = new HashSet((2*iterations), 0.75f);
</code></pre>
<p>becomes</p>
<pre><code> HashSet counts = new HashSet((2*iterations), 0.75f) {
@Override public boolean add(Ob... | 0 | 2009-05-27T23:59:54Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,861 | <p>Just a stab in the dark here, but some optimizations that Python is making that Java probably isn't:</p>
<ul>
<li>The range() call in Python is creating all 10000000 integer objects at once, in optimized C code. Java must create an Integer object each iteration, which may be slower. </li>
<li>In Python, ints are i... | 1 | 2009-05-28T01:53:24Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 918,874 | <p>You might want to see if you can "prime" the JIT compiler into compiling the section of code you're interested in, by perhaps running it as a function once beforehand and sleeping briefly afterwords. This might allow the JVM to compile the function down to native code.</p>
| 0 | 2009-05-28T01:59:17Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 919,844 | <p>I suspect that is that Python uses the integer value itself as its hash and the hashtable based implementation of set uses that value directly. From the comments in the <a href="http://svn.python.org/view/python/trunk/Objects/dictobject.c?revision=72958&view=markup" rel="nofollow">source</a>:</p>
<blockquote>
... | 12 | 2009-05-28T08:46:54Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 919,966 | <p>You're not really testing Java vs. Python, you're testing <code>java.util.HashSet</code> using autoboxed Integers vs. Python's native set and integer handling.</p>
<p>Apparently, the Python side in this particular microbenchmark is indeed faster.</p>
<p>I tried replacing HashSet with <code>TIntHashSet</code> from ... | 20 | 2009-05-28T09:26:11Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 919,980 | <p>If you really want to store primitive types in a set, and do heavy work on it, roll your own set in Java. The generic classes are not fast enough for scientific computing.</p>
<p>As Ants Aasma mentions, Python bypasses hashing and uses the integer directly. Java creates an Integer object (<a href="http://java.sun.c... | 2 | 2009-05-28T09:30:18Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 920,067 | <p>I'd like to dispel a couple myths I saw in the answers:</p>
<p>Java is compiled, yes, to bytecode but ultimately to native code in most runtime environments. People who say C is inherently faster aren't telling the entire story, I could make a case that byte compiled languages are inherently faster because the JIT... | 6 | 2009-05-28T09:53:03Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 939,697 | <p>A few changes for faster Python. </p>
<pre><code>#!/usr/bin/python
import time
import sys
import psyco #<<<<
psyco.full()
class Element(object):
__slots__=["num"] #<<<<
def __init__(self, i):
self.num = i
def main(args):
iterations = 100000... | 1 | 2009-06-02T13:53:25Z | [
"java",
"python",
"microbenchmark"
] |
My python program executes faster than my java version of the same program. What gives? | 918,359 | <p><strong>Update: 2009-05-29</strong></p>
<p>Thanks for all the suggestions and advice. <strong>I used your suggestions to make my production code execute 2.5 times faster on average than my best result a couple of days ago.</strong> In the end I was able to make the java code the fastest.</p>
<p>Lessons:</p>
<ul... | 15 | 2009-05-27T22:48:42Z | 1,773,936 | <p>Well, if you're going to tune the Java program, you might as well tune the Python program too.</p>
<pre><code>>>> import timeit
>>> timeit.Timer('x = set()\nfor i in range(10000000):\n x.add(i)').repeat(3, 1)
[2.1174559593200684, 2.0019571781158447, 1.9973630905151367]
>>> timeit.Timer... | 0 | 2009-11-20T23:53:31Z | [
"java",
"python",
"microbenchmark"
] |
Is there a better way than int( byte_buffer.encode('hex'), 16 ) | 918,754 | <p>In Python, I'm constantly using the following sequence to get an integer value from a byte buffer (in python this is a str).</p>
<p>I'm getting the buffer from the struct.unpack() routine. When I unpack a 'char' using</p>
<pre><code>byte_buffer, = struct.unpack('c', raw_buffer)
int_value = int( byte_buffer.encode... | 3 | 2009-05-28T01:16:47Z | 918,774 | <p>The <a href="http://docs.python.org/library/struct" rel="nofollow">struct</a> module is good at unpacking binary data.</p>
<pre><code>int_value = struct.unpack('>I', byte_buffer)[0]
</code></pre>
| 5 | 2009-05-28T01:21:52Z | [
"python",
"integer",
"types",
"byte"
] |
Is there a better way than int( byte_buffer.encode('hex'), 16 ) | 918,754 | <p>In Python, I'm constantly using the following sequence to get an integer value from a byte buffer (in python this is a str).</p>
<p>I'm getting the buffer from the struct.unpack() routine. When I unpack a 'char' using</p>
<pre><code>byte_buffer, = struct.unpack('c', raw_buffer)
int_value = int( byte_buffer.encode... | 3 | 2009-05-28T01:16:47Z | 918,960 | <blockquote>
<p>Bounded to 1 byte â Noah Campbell 18 mins ago</p>
</blockquote>
<p>The best way to do this then is to instantiate a struct unpacker.</p>
<pre><code>from struct import Struct
unpacker = Struct("b")
unpacker.unpack("z")[0]
</code></pre>
<p>Note that you can change "b" to "B" if you want an unsigne... | 2 | 2009-05-28T02:36:26Z | [
"python",
"integer",
"types",
"byte"
] |
Is there a better way than int( byte_buffer.encode('hex'), 16 ) | 918,754 | <p>In Python, I'm constantly using the following sequence to get an integer value from a byte buffer (in python this is a str).</p>
<p>I'm getting the buffer from the struct.unpack() routine. When I unpack a 'char' using</p>
<pre><code>byte_buffer, = struct.unpack('c', raw_buffer)
int_value = int( byte_buffer.encode... | 3 | 2009-05-28T01:16:47Z | 923,041 | <p>If we're talking about getting the integer value of a byte, then you want this:</p>
<pre><code>ord(byte_buffer)
</code></pre>
<p>Can't understand why it isn't already suggested.</p>
| 1 | 2009-05-28T20:40:11Z | [
"python",
"integer",
"types",
"byte"
] |
Case insensitive replace | 919,056 | <p>What's the easiest way to do a case-insensitive string replacement in Python?</p>
| 74 | 2009-05-28T03:35:24Z | 919,067 | <p>The <code>string</code> type doesn't support this. You're probably best off using <a href="http://docs.python.org/library/re.html#re.RegexObject.sub">the regular expression sub method</a> with the <a href="http://docs.python.org/library/re.html#re.IGNORECASE">re.IGNORECASE</a> option.</p>
<pre><code>>>> im... | 104 | 2009-05-28T03:39:13Z | [
"python",
"string",
"case-insensitive"
] |
Case insensitive replace | 919,056 | <p>What's the easiest way to do a case-insensitive string replacement in Python?</p>
| 74 | 2009-05-28T03:35:24Z | 919,074 | <pre><code>import re
pattern = re.compile("hello", re.IGNORECASE)
pattern.sub("bye", "hello HeLLo HELLO")
# 'bye bye bye'
</code></pre>
| 56 | 2009-05-28T03:41:04Z | [
"python",
"string",
"case-insensitive"
] |
Case insensitive replace | 919,056 | <p>What's the easiest way to do a case-insensitive string replacement in Python?</p>
| 74 | 2009-05-28T03:35:24Z | 4,759,732 | <p>This doesn't require RegularExp</p>
<pre><code>def ireplace(old, new, text):
"""
Replace case insensitive
Raises ValueError if string not found
"""
index_l = text.lower().index(old.lower())
return text[:index_l] + new + text[index_l + len(old):]
</code></pre>
| 1 | 2011-01-21T14:09:54Z | [
"python",
"string",
"case-insensitive"
] |
Case insensitive replace | 919,056 | <p>What's the easiest way to do a case-insensitive string replacement in Python?</p>
| 74 | 2009-05-28T03:35:24Z | 4,773,614 | <p>Continuing on bFloch's answer, this function will change not one, but all occurrences of old with new - in a case insensitive fashion. </p>
<pre><code>def ireplace(old, new, text):
idx = 0
while idx < len(text):
index_l = text.lower().find(old.lower(), idx)
if index_l == -1:
r... | 4 | 2011-01-23T11:46:46Z | [
"python",
"string",
"case-insensitive"
] |
Case insensitive replace | 919,056 | <p>What's the easiest way to do a case-insensitive string replacement in Python?</p>
| 74 | 2009-05-28T03:35:24Z | 9,312,577 | <p>never posted an answer before and this thread is really old but i came up with another sollution and figured i could get your respons, Im not seasoned in Python programming so if there are appearant drawbacks to it, please point them out since its good learning :)</p>
<pre><code>i='I want a hIPpo for my birthday'
k... | -2 | 2012-02-16T13:59:28Z | [
"python",
"string",
"case-insensitive"
] |
Case insensitive replace | 919,056 | <p>What's the easiest way to do a case-insensitive string replacement in Python?</p>
| 74 | 2009-05-28T03:35:24Z | 9,709,563 | <p>Very simple, in a single line:</p>
<pre><code>import re
re.sub("(?i)hello","bye", "hello HeLLo HELLO") #'bye bye bye'
re.sub("(?i)he\.llo","bye", "he.llo He.LLo HE.LLO") #'bye bye bye'
</code></pre>
<p>Or, use the optional "flags" argument:</p>
<pre><code>import re
re.sub("hello", "bye", "hello HeLLo HELLO", flag... | 16 | 2012-03-14T20:14:03Z | [
"python",
"string",
"case-insensitive"
] |
Case insensitive replace | 919,056 | <p>What's the easiest way to do a case-insensitive string replacement in Python?</p>
| 74 | 2009-05-28T03:35:24Z | 15,831,118 | <p>Like Blair Conrad says string.replace doesn't support this.</p>
<p>Use the regex <code>re.sub</code>, but remember to escape the replacement string first. Note that there's no flags-option in 2.6 for <code>re.sub</code>, so you'll have to use the embedded modifier <code>'(?i)'</code> (or a RE-object, see Blair Conr... | 1 | 2013-04-05T10:03:57Z | [
"python",
"string",
"case-insensitive"
] |
Case insensitive replace | 919,056 | <p>What's the easiest way to do a case-insensitive string replacement in Python?</p>
| 74 | 2009-05-28T03:35:24Z | 26,600,348 | <p>I was having \t being converted to the <a href="https://docs.python.org/2/reference/lexical_analysis.html#string-literals" rel="nofollow">escape sequences</a> (scroll a bit down), so I noted that <a href="https://docs.python.org/2/library/re.html#re.sub" rel="nofollow">re.sub</a> converts backslashed escaped charact... | 0 | 2014-10-28T03:18:39Z | [
"python",
"string",
"case-insensitive"
] |
Are there memory efficiencies gained when code is wrapped in functions? | 919,103 | <p>I have been working on some code. My usual approach is to first solve all of the pieces of the problem, creating the loops and other pieces of code I need as I work through the problem and then if I expect to reuse the code I go back through it and group the parts of code together that I think should be grouped to... | 3 | 2009-05-28T03:52:02Z | 919,108 | <p>Some extra memory is freed when you return from a function, but that's exactly as much extra memory as was allocated to call the function in the first place. In any case - if you seeing a large amount of difference, that's likely an artifact of the state of the runtime, and is not something you should really be wor... | 0 | 2009-05-28T03:54:34Z | [
"python",
"memory-management",
"function"
] |
Are there memory efficiencies gained when code is wrapped in functions? | 919,103 | <p>I have been working on some code. My usual approach is to first solve all of the pieces of the problem, creating the loops and other pieces of code I need as I work through the problem and then if I expect to reuse the code I go back through it and group the parts of code together that I think should be grouped to... | 3 | 2009-05-28T03:52:02Z | 919,134 | <p>You can use the <a href="http://docs.python.org/library/gc.html" rel="nofollow">Python garbage collector interface</a> provided to more closely examine what (if anything) is being left around in the second case. Specifically, you may want to check out <a href="http://docs.python.org/library/gc.html#gc.get%5Fobjects... | 1 | 2009-05-28T04:08:41Z | [
"python",
"memory-management",
"function"
] |
Are there memory efficiencies gained when code is wrapped in functions? | 919,103 | <p>I have been working on some code. My usual approach is to first solve all of the pieces of the problem, creating the loops and other pieces of code I need as I work through the problem and then if I expect to reuse the code I go back through it and group the parts of code together that I think should be grouped to... | 3 | 2009-05-28T03:52:02Z | 919,172 | <p>Maybe you used some local variables in your function, which are implicitly released by reference counting at the end of the function, while they are not released at the end of your code segment?</p>
| 3 | 2009-05-28T04:22:40Z | [
"python",
"memory-management",
"function"
] |
Are there memory efficiencies gained when code is wrapped in functions? | 919,103 | <p>I have been working on some code. My usual approach is to first solve all of the pieces of the problem, creating the loops and other pieces of code I need as I work through the problem and then if I expect to reuse the code I go back through it and group the parts of code together that I think should be grouped to... | 3 | 2009-05-28T03:52:02Z | 920,679 | <p>Maybe you should re-engineer your code to get rid of unnecessary variables (that may not be freed instantly)... how about the following snippet?</p>
<pre><code>myfile = file(r"c:\fillinglist.txt")
ciklist = sorted(set(x.split("^")[0].strip() for x in myfile if not x.startswith(".")))
</code></pre>
<p>EDIT: I don't... | 0 | 2009-05-28T12:53:26Z | [
"python",
"memory-management",
"function"
] |
Are there memory efficiencies gained when code is wrapped in functions? | 919,103 | <p>I have been working on some code. My usual approach is to first solve all of the pieces of the problem, creating the loops and other pieces of code I need as I work through the problem and then if I expect to reuse the code I go back through it and group the parts of code together that I think should be grouped to... | 3 | 2009-05-28T03:52:02Z | 948,246 | <p>I asked another question about copying lists and the answers, particularly the answer directing me to look at deepcopy caused me to think about some dictionary behavior. The problem I was experiencing had to do with the fact that the original list is never garbage collected because the dictionary maintains referenc... | 0 | 2009-06-04T01:55:24Z | [
"python",
"memory-management",
"function"
] |
Problem in handle PNG by the PIL | 919,104 | <pre><code>from PIL import ImageFile as PILImageFile
p = PILImageFile.Parser()
#Parser the data
for chunk in content.chunks():
p.feed(chunk)
try:
image = p.close()
except IOError:
return None
#Here the model is RGBA
if image.mode != "RGB":
image = image.convert("RGB")
</cod... | 2 | 2009-05-28T03:52:35Z | 18,046,944 | <p>This results from an incorrect coding of close within PIL, its a bug.</p>
<p><strong>Edit the File ( path may be different on your system ):</strong></p>
<p><em>sudo vi /usr/lib64/python2.6/site-packages/PIL/ImageFile.py</em></p>
<p><strong>Online 283 Modify:</strong></p>
<pre><code>def close(self):
self.dat... | 0 | 2013-08-04T19:49:06Z | [
"python",
"png",
"python-imaging-library"
] |
Scalable web application with lot of image servings | 919,248 | <p>I started working on a web application. This application needs lot of image handling. I started off with <a href="http://en.wikipedia.org/wiki/PHP" rel="nofollow">PHP</a> as it was the easiest and cheapest to host. I have used the .NET framework for some of my previous applications and I'm very comfortable with Pyth... | 0 | 2009-05-28T05:02:08Z | 919,339 | <p>If you want performance when serving images, you have to take the FaceBook approach of 'never go to disk unless absolutely necessary' - meaning use as much caching as possible between your image servers and the end user. There are many products that can help you out both commercial and free, including just configur... | 0 | 2009-05-28T05:39:47Z | [
".net",
"python",
"scalability"
] |
Scalable web application with lot of image servings | 919,248 | <p>I started working on a web application. This application needs lot of image handling. I started off with <a href="http://en.wikipedia.org/wiki/PHP" rel="nofollow">PHP</a> as it was the easiest and cheapest to host. I have used the .NET framework for some of my previous applications and I'm very comfortable with Pyth... | 0 | 2009-05-28T05:02:08Z | 919,469 | <p>From what You have written, either .NET or Python would be a good choice for You. Personally, I would go for Python. Why?</p>
<ul>
<li>It is free.</li>
<li>It is scalable.</li>
<li>With Python Imaging Library You can do almost anything with images.</li>
<li>Hey, it is python - less code, same result.</li>
</ul>
<p... | 0 | 2009-05-28T06:29:12Z | [
".net",
"python",
"scalability"
] |
Scalable web application with lot of image servings | 919,248 | <p>I started working on a web application. This application needs lot of image handling. I started off with <a href="http://en.wikipedia.org/wiki/PHP" rel="nofollow">PHP</a> as it was the easiest and cheapest to host. I have used the .NET framework for some of my previous applications and I'm very comfortable with Pyth... | 0 | 2009-05-28T05:02:08Z | 920,157 | <p>Please buy Schlossnagle's book, <a href="http://rads.stackoverflow.com/amzn/click/067232699X" rel="nofollow">Scalable Internet Architectures</a>.</p>
<p>You should not be serving the images from Python (or PHP or .Net) but from Apache and Squid. Same is true for Javascript and CSS files -- they're static media, an... | 1 | 2009-05-28T10:16:54Z | [
".net",
"python",
"scalability"
] |
Scalable web application with lot of image servings | 919,248 | <p>I started working on a web application. This application needs lot of image handling. I started off with <a href="http://en.wikipedia.org/wiki/PHP" rel="nofollow">PHP</a> as it was the easiest and cheapest to host. I have used the .NET framework for some of my previous applications and I'm very comfortable with Pyth... | 0 | 2009-05-28T05:02:08Z | 920,498 | <p>I do not think switching languages will help much with your problem, It's just the architecture you chose initially only works for small amounts of data. I would recommend you to visit <a href="http://highscalability.com" rel="nofollow">http://highscalability.com</a> , It's time you started looking how the big guys... | 0 | 2009-05-28T11:54:58Z | [
".net",
"python",
"scalability"
] |
Scalable web application with lot of image servings | 919,248 | <p>I started working on a web application. This application needs lot of image handling. I started off with <a href="http://en.wikipedia.org/wiki/PHP" rel="nofollow">PHP</a> as it was the easiest and cheapest to host. I have used the .NET framework for some of my previous applications and I'm very comfortable with Pyth... | 0 | 2009-05-28T05:02:08Z | 920,598 | <p>As mentioned previously, any number of development platforms will work, it really depends on your approach to caching the content.</p>
<p>If you are comfortable with Python I would recommend <a href="http://en.wikipedia.org/wiki/Django_%28web_framework%29" rel="nofollow">Django</a>. There is a large development co... | 1 | 2009-05-28T12:31:02Z | [
".net",
"python",
"scalability"
] |
Scalable web application with lot of image servings | 919,248 | <p>I started working on a web application. This application needs lot of image handling. I started off with <a href="http://en.wikipedia.org/wiki/PHP" rel="nofollow">PHP</a> as it was the easiest and cheapest to host. I have used the .NET framework for some of my previous applications and I'm very comfortable with Pyth... | 0 | 2009-05-28T05:02:08Z | 931,199 | <p>I would also recommend looking at <a href="http://www.danga.com/mogilefs/" rel="nofollow">MogileFS</a>. It is a distributed file system which runs on Unix based OS's. I know that digg use it to store their avatar images. </p>
<p>It's from the same guys who created memcached and live journal.</p>
| 0 | 2009-05-31T03:36:19Z | [
".net",
"python",
"scalability"
] |
Resize ctypes array | 919,369 | <p>I'd like to resize a ctypes array. As you can see, ctypes.resize doesn't work like it could. I can write a function to resize an array, but I wanted to know some other solutions to this. Maybe I'm missing some ctypes trick or maybe I simply used resize wrong. The name c_long_Array_0 seems to tell me this may not wor... | 3 | 2009-05-28T05:51:38Z | 919,501 | <pre><code>from ctypes import *
list = (c_int*1)()
def customresize(array, new_size):
resize(array, sizeof(array._type_)*new_size)
return (array._type_*new_size).from_address(addressof(array))
list[0] = 123
list = customresize(list, 5)
>>> list[0]
123
>>> list[4]
0
</code></pre>
| 6 | 2009-05-28T06:47:36Z | [
"python",
"ctypes"
] |
How do we precompile base templates in Cheetah so that #include, #extends and #import works properly in Weby | 919,539 | <p>How do you serve <strong>Cheetah</strong> in <strong>production</strong>?</p>
<p>Guys can you share the setup on how to precompile and serve cheetah in production</p>
<p>Since we dont compile templates in webpy it is getting upstream time out errors. If you could share a good best practise it would help</p>
<p>*<... | 2 | 2009-05-28T07:02:48Z | 933,727 | <p>Maybe compile automagically on as needed basis:</p>
<pre><code>import sys
import os
from os import path
import logging
from Cheetah.Template import Template
from Cheetah.Compiler import Compiler
log = logging.getLogger(__name__)
_import_save = __import__
def cheetah_import(name, *args, **kw):
"""Import function... | 1 | 2009-06-01T06:18:12Z | [
"python",
"inheritance",
"web.py"
] |
How do we precompile base templates in Cheetah so that #include, #extends and #import works properly in Weby | 919,539 | <p>How do you serve <strong>Cheetah</strong> in <strong>production</strong>?</p>
<p>Guys can you share the setup on how to precompile and serve cheetah in production</p>
<p>Since we dont compile templates in webpy it is getting upstream time out errors. If you could share a good best practise it would help</p>
<p>*<... | 2 | 2009-05-28T07:02:48Z | 968,398 | <p><strong>This works</strong></p>
<pre><code>try:web.render('mafbase.tmpl', None, True, 'mafbase')
except:pass
</code></pre>
<p><strong>This is what i did with you code</strong></p>
<pre><code>from cheetahimport import *
sys.path.append('./templates')
cheetah_import('mafbase')
</code></pre>
<h1>includes dont work ... | 0 | 2009-06-09T06:00:17Z | [
"python",
"inheritance",
"web.py"
] |
Can a variable number of arguments be passed to a function? | 919,680 | <p>In a similar way to using varargs in C or C++:</p>
<pre><code>fn(a, b)
fn(a, b, c, d, ...)
</code></pre>
| 180 | 2009-05-28T07:50:10Z | 919,684 | <p>Yes.</p>
<p>This is simple and works if you disregard keyword arguments:</p>
<pre><code>def manyArgs(*arg):
print "I was called with", len(arg), "arguments:", arg
>>> manyArgs(1)
I was called with 1 arguments: (1,)
>>> manyArgs(1, 2,3)
I was called with 3 arguments: (1, 2, 3)
</code></pre>
<p... | 229 | 2009-05-28T07:51:54Z | [
"python"
] |
Can a variable number of arguments be passed to a function? | 919,680 | <p>In a similar way to using varargs in C or C++:</p>
<pre><code>fn(a, b)
fn(a, b, c, d, ...)
</code></pre>
| 180 | 2009-05-28T07:50:10Z | 919,720 | <p>Adding to unwinds post:</p>
<p>You can send multiple key-value args too.</p>
<pre><code>def myfunc(**kwargs):
# kwargs is a dictionary.
for k,v in kwargs.iteritems():
print "%s = %s" % (k, v)
myfunc(abc=123, efh=456)
# abc = 123
# efh = 456
</code></pre>
<p>And you can mix the two:</p>
<pre><co... | 146 | 2009-05-28T07:59:58Z | [
"python"
] |
Can a variable number of arguments be passed to a function? | 919,680 | <p>In a similar way to using varargs in C or C++:</p>
<pre><code>fn(a, b)
fn(a, b, c, d, ...)
</code></pre>
| 180 | 2009-05-28T07:50:10Z | 9,449,581 | <p>Adding to the other excellent posts.</p>
<p>Sometimes you don't want to specify the number of arguments <strong>and</strong> want to use keys for them (the compiler will complain if one argument passed in a dictionary is not used in the method).</p>
<pre><code>def manyArgs1(args):
print args.a, args.b #note args... | 9 | 2012-02-26T00:56:32Z | [
"python"
] |
Can a variable number of arguments be passed to a function? | 919,680 | <p>In a similar way to using varargs in C or C++:</p>
<pre><code>fn(a, b)
fn(a, b, c, d, ...)
</code></pre>
| 180 | 2009-05-28T07:50:10Z | 16,785,702 | <pre><code>def f(dic):
if 'a' in dic:
print dic['a'],
pass
else: print 'None',
if 'b' in dic:
print dic['b'],
pass
else: print 'None',
if 'c' in dic:
print dic['c'],
pass
else: print 'None',
print
pass
f({})
f({'a':20,
'c':30})
f({'a':... | 2 | 2013-05-28T07:00:57Z | [
"python"
] |
Progress bar with long web requests | 919,816 | <p>In a django application I am working on, I have just added the ability to archive a number of files (starting 50mb in total) to a zip file. Currently, i am doing it something like this:</p>
<pre><code>get files to zip
zip all files
send HTML response
</code></pre>
<p>Obviously, this causes a big wait on line two w... | 2 | 2009-05-28T08:38:58Z | 919,919 | <p>Better than a static page, show a Javascript dialog (using Shadowbox, JQuery UI or some custom method) with a throbber ( you can get some at hxxp://www.ajaxload.info/ ). You can also show the throbber in your page, without dialogs. Most users only want to know their action is being handled, and can live without reli... | 0 | 2009-05-28T09:10:37Z | [
"python",
"django"
] |
Progress bar with long web requests | 919,816 | <p>In a django application I am working on, I have just added the ability to archive a number of files (starting 50mb in total) to a zip file. Currently, i am doing it something like this:</p>
<pre><code>get files to zip
zip all files
send HTML response
</code></pre>
<p>Obviously, this causes a big wait on line two w... | 2 | 2009-05-28T08:38:58Z | 919,935 | <p>You should keep in mind showing the progress bar may not be a good idea, since you can get timeouts or get your server suffer from submitting lot of simultaneous requests.</p>
<p>Put the zipping task in the queue and have it callback to notify the user somehow - by e-mail for instance - that the process has finishe... | 3 | 2009-05-28T09:15:37Z | [
"python",
"django"
] |
Progress bar with long web requests | 919,816 | <p>In a django application I am working on, I have just added the ability to archive a number of files (starting 50mb in total) to a zip file. Currently, i am doing it something like this:</p>
<pre><code>get files to zip
zip all files
send HTML response
</code></pre>
<p>Obviously, this causes a big wait on line two w... | 2 | 2009-05-28T08:38:58Z | 919,955 | <p>You could use a 'log-file' to keep track of the zipped files, and of how many files still remain.</p>
<p>The procedural way should be like this:</p>
<ol>
<li>Count the numbers of file, write it in a text file, in a format like totalfiles.filespreocessed</li>
<li>Every file you zip, simply update the file</li>
</ol... | 0 | 2009-05-28T09:21:27Z | [
"python",
"django"
] |
Progress bar with long web requests | 919,816 | <p>In a django application I am working on, I have just added the ability to archive a number of files (starting 50mb in total) to a zip file. Currently, i am doing it something like this:</p>
<pre><code>get files to zip
zip all files
send HTML response
</code></pre>
<p>Obviously, this causes a big wait on line two w... | 2 | 2009-05-28T08:38:58Z | 921,447 | <p>Fun fact: You might be able to use a progress bar to trick users into thinking that things are going faster than they really are.</p>
<p><a href="http://www.chrisharrison.net/projects/progressbars/index.html" rel="nofollow">http://www.chrisharrison.net/projects/progressbars/index.html</a></p>
| 0 | 2009-05-28T15:21:38Z | [
"python",
"django"
] |
How to find a thread id in Python | 919,897 | <p>I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message. </p>
<p>I would like writeLog() to be able to add something to the message t... | 68 | 2009-05-28T09:04:24Z | 919,920 | <p>I saw examples of thread IDs like this:</p>
<pre><code>class myThread(threading.Thread):
def __init__(self, threadID, name, counter):
self.threadID = threadID
...
</code></pre>
<p>The <a href="http://docs.python.org/library/threading.html#threading.Thread.name">threading module docs</a> lists <... | 5 | 2009-05-28T09:11:13Z | [
"python",
"multithreading"
] |
How to find a thread id in Python | 919,897 | <p>I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message. </p>
<p>I would like writeLog() to be able to add something to the message t... | 68 | 2009-05-28T09:04:24Z | 919,928 | <p><a href="http://docs.python.org/library/threading.html#threading.Thread.ident"><code>thread.get_ident()</code></a> works, though <code>thread</code> is deprecated, or <a href="http://docs.python.org/library/threading.html#threading.current_thread"><code>threading.current_thread()</code></a> (or <code>threading.curre... | 85 | 2009-05-28T09:13:40Z | [
"python",
"multithreading"
] |
How to find a thread id in Python | 919,897 | <p>I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message. </p>
<p>I would like writeLog() to be able to add something to the message t... | 68 | 2009-05-28T09:04:24Z | 2,357,652 | <p>Using the <a href="http://docs.python.org/library/logging.html">logging</a> module you can automatically add the current thread identifier in each log entry.
Just use one of these <a href="http://docs.python.org/library/logging.html#logging.LogRecord">LogRecord</a> mapping keys in your logger format string:</p>
<b... | 37 | 2010-03-01T17:19:18Z | [
"python",
"multithreading"
] |
How to find a thread id in Python | 919,897 | <p>I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message. </p>
<p>I would like writeLog() to be able to add something to the message t... | 68 | 2009-05-28T09:04:24Z | 9,410,056 | <p>The <code>thread.get_ident()</code> return long integer on linux, it's not really thread id.
I use <a href="http://blog.devork.be/2010/09/finding-linux-thread-id-from-within.html" rel="nofollow">this method</a> get really thread id on linux.</p>
<pre><code>import ctypes
libc = ctypes.cdll.LoadLibrary('libc.so.6')
... | 4 | 2012-02-23T09:17:44Z | [
"python",
"multithreading"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.