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
In django, how to write a query that selects all possible combinations of four integers?
1,631,333
<p>I'm writing a Game Website, where the draw is a series of four digits. e.g 1234</p> <p>I"m trying to write a query in django that will select all winners based on the four digits entered. winners are any combination of the same numbers or the same combination, 1 2 3 4, 2 3 1 4, 4 1 3 2 are all winners.</p> <p>how...
1
2009-10-27T14:48:32Z
1,631,885
<p>Is the order of the numbers important? </p> <p>If not, you could sort the digits for tickets and draws in ascending order, then use your code</p> <pre><code>winners = Ticket.objects.filter(draw=self.id,digit1=self.digit1,digit2=self.digit2,digit3=self.digit3,digit4=self.digit4) </code></pre> <p>As an aside, Your ...
0
2009-10-27T16:06:46Z
[ "python", "django", "django-models" ]
How to allow scaling with uniform aspect ratio in (Py)Qt?
1,631,574
<p>If you have a QImage wrapped inside a QLabel, is it possible to scale it up or down when you resize the window and maintain the aspect ratio (so the image doesn't become distorted)? I figured out that it can scale using setScaledContents(), and you can set a minimum and maximum size, but the image still loses its as...
1
2009-10-27T15:23:50Z
1,631,653
<p>Convert the image to a <code>QPixmap</code> (use <code>QPixmap.fromImage(img)</code>) and then, you can use <a href="http://doc.trolltech.com/4.5/qpixmap.html#scaledToHeight" rel="nofollow"><code>scaledToHeight()</code></a>.</p>
0
2009-10-27T15:33:07Z
[ "python", "user-interface", "qt", "pyqt" ]
How to allow scaling with uniform aspect ratio in (Py)Qt?
1,631,574
<p>If you have a QImage wrapped inside a QLabel, is it possible to scale it up or down when you resize the window and maintain the aspect ratio (so the image doesn't become distorted)? I figured out that it can scale using setScaledContents(), and you can set a minimum and maximum size, but the image still loses its as...
1
2009-10-27T15:23:50Z
1,632,726
<p>I'm showing this as C++, which is what the documentation I'm looking at is in. It shouldn't be too difficult to convert to python.</p> <p>You need to create a custom derivative of <code>QLayoutItem</code>, which overrides <code>bool hasHeightForWidth()</code> and <code>int heightForWidth( int width)</code> to pres...
2
2009-10-27T18:19:42Z
[ "python", "user-interface", "qt", "pyqt" ]
"Featuring" content in a Django website
1,631,592
<p>I'm working on a new Django project, and the client wants to "feature" content on the homepage and a few other sections of the website. Content in this case could be a blog post, an event, a news story, etc. Each item would have a "start featuring" datetime and an "stop featuring" datetime. </p> <p>I've done this a...
1
2009-10-27T15:26:07Z
1,631,786
<p>Have you looked at the <a href="http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#module-django.contrib.contenttypes" rel="nofollow">contenttypes framework</a>? You could set up a <code>FeaturedItem</code> model, with start and end datetimes, and a generic foreign key. This allows the relationship to be...
7
2009-10-27T15:52:14Z
[ "python", "django", "content-management-system" ]
"Featuring" content in a Django website
1,631,592
<p>I'm working on a new Django project, and the client wants to "feature" content on the homepage and a few other sections of the website. Content in this case could be a blog post, an event, a news story, etc. Each item would have a "start featuring" datetime and an "stop featuring" datetime. </p> <p>I've done this a...
1
2009-10-27T15:26:07Z
2,115,548
<p>I'm trying something similar, using the contenttypes framework but the problem comes when displaying the relationship in the admin area. I've been trying to hack this together to simply show a checkbox with "Featured?" next to it.</p> <pre><code>class FeaturedContent(models.Model): content_type = models.Foreign...
0
2010-01-22T06:46:13Z
[ "python", "django", "content-management-system" ]
Python LDAP old password still working
1,631,739
<p>I have the LDAP python module installed to authorise logins via active directory, but if I change the password, the new and the old one work together. Does anyone know how to resolve this issue?</p>
1
2009-10-27T15:46:43Z
1,632,895
<p>I strongly suspect that it is active directory who is caching the credentials -at least for some time after the change- ; have a look at <a href="http://support.microsoft.com/?kbid=906305" rel="nofollow">this link</a>.</p>
2
2009-10-27T18:50:22Z
[ "python", "login", "ldap" ]
Access violation on run function from dll
1,631,755
<p>I have DLL, interface on C++ for work with he. In bcb, msvc it works fine. I want to use Python-scripts to access function in this library. Generate python-package using Swig.</p> <p>File <em>setup.py</em></p> <pre><code> import distutils from distutils.core import setup, Extension setup(name = "DCM", vers...
1
2009-10-27T15:48:44Z
1,706,634
<p>I find bug. If you using DLL, you must write calling conventions in an explicit form like this:</p> <pre><code>class IRegistrationMessage { public: ... virtual int _cdecl GetLength() const = 0; virtual void _cdecl SetLength(int value) = 0; ... }; </code></pre> <p>I append calling conventions and now all wo...
0
2009-11-10T09:27:36Z
[ "c++", "python", "swig" ]
Python: Public methods calling their 'brother' private methods
1,631,855
<p>I have been writing Python code for only a couple of weeks, so I'm still figuring out the lay of the land. But let's say I have a method that MAY be called on by a 'user' on occasion as well as used HEAVILY internally (ie, the arguments have already been checked before the call). Here is what I am currently doing:</...
2
2009-10-27T16:01:39Z
1,631,894
<p>There is no "true" support for private members in python, but the pythonic way to indicate a member as private is to use two leading underscores. In your case, <code>__do_something</code>.</p> <p>For further details, see <a href="http://docs.python.org/tutorial/classes.html" rel="nofollow">python.org - classes</a><...
0
2009-10-27T16:07:47Z
[ "python", "methods", "public-method" ]
Python: Public methods calling their 'brother' private methods
1,631,855
<p>I have been writing Python code for only a couple of weeks, so I'm still figuring out the lay of the land. But let's say I have a method that MAY be called on by a 'user' on occasion as well as used HEAVILY internally (ie, the arguments have already been checked before the call). Here is what I am currently doing:</...
2
2009-10-27T16:01:39Z
1,631,908
<p>I'm just learning python myself (and enjoying it) but I think that's the way to do it. However, the private method should have two underscores and called like 'self.__do_something()'.</p>
-2
2009-10-27T16:09:11Z
[ "python", "methods", "public-method" ]
Python: Public methods calling their 'brother' private methods
1,631,855
<p>I have been writing Python code for only a couple of weeks, so I'm still figuring out the lay of the land. But let's say I have a method that MAY be called on by a 'user' on occasion as well as used HEAVILY internally (ie, the arguments have already been checked before the call). Here is what I am currently doing:</...
2
2009-10-27T16:01:39Z
1,631,913
<p>That is fine. The call to the "brother" method is wrong in your code, though. You should do it like this:</p> <pre><code># Now call the 'brother' method that does the real work. return self._do_something(arg1, arg2, arg3, arg3) </code></pre> <p>That is, you should call it "through" the self reference, since it's a...
2
2009-10-27T16:09:52Z
[ "python", "methods", "public-method" ]
Python: Public methods calling their 'brother' private methods
1,631,855
<p>I have been writing Python code for only a couple of weeks, so I'm still figuring out the lay of the land. But let's say I have a method that MAY be called on by a 'user' on occasion as well as used HEAVILY internally (ie, the arguments have already been checked before the call). Here is what I am currently doing:</...
2
2009-10-27T16:01:39Z
1,631,915
<p>well, unless the error checking code is very expensive, I would have only one method, which always does the error checking. It may repeat some checks, but it does offer you more security, and it may come in handy if someone inherits from your class.</p> <p>If later on you need performance you can always cache resul...
0
2009-10-27T16:09:59Z
[ "python", "methods", "public-method" ]
Python File Slurp
1,631,897
<p>Is there a one-liner to read all the lines of a file in Python, rather than the standard:</p> <pre><code>f = open('x.txt') cts = f.read() f.close() </code></pre> <p>Seems like this is done so often that there's got to be a one-liner. Any ideas?</p>
33
2009-10-27T16:08:07Z
1,631,916
<pre><code>f = open('x.txt').read() </code></pre> <p>if you want a single string, or</p> <pre><code>f = open('x.txt').readlines() </code></pre> <p>if you want a list of lines. Both don't guarantee the file is immediately closed (in practice it will be immediately closed in current CPython, but closed "only when the...
80
2009-10-27T16:10:00Z
[ "python", "file-io" ]
Python File Slurp
1,631,897
<p>Is there a one-liner to read all the lines of a file in Python, rather than the standard:</p> <pre><code>f = open('x.txt') cts = f.read() f.close() </code></pre> <p>Seems like this is done so often that there's got to be a one-liner. Any ideas?</p>
33
2009-10-27T16:08:07Z
18,977,657
<p>If you are on Python3, make sure you properly respect your file's input encoding, e.g.:</p> <pre><code>import codecs with codecs.open(filename, 'r', encoding="utf8") as file: cts = file.read() </code></pre> <p>Find the list of codec names in the <a href="http://docs.python.org/3/library/codecs.html#standard-en...
-3
2013-09-24T09:25:29Z
[ "python", "file-io" ]
How to access a forms instance in a modelformset django
1,632,043
<p>In my view I create a formset of photos belonging to a specific article, this works brilliantly, and I am able to render and process the forms. However for the image field I would like to display the already uploaded image. Normally I would access the path through the instance <code>form.instance.image.get_thumbnail...
2
2009-10-27T16:29:18Z
1,632,934
<p>Not sure, but it may be that you don't need <code>instance</code>:</p> <pre><code>{% for form in formset.forms %} {{ form.image.get_thumbnail_url }} {% endfor %} </code></pre>
2
2009-10-27T18:55:41Z
[ "python", "django", "django-forms" ]
Using properties defined at a per-instance level not per-class
1,632,170
<p>What I am trying to achieve is something like this:</p> <pre><code>class object: def __init__(self): WidthVariable(self) print self.width #Imagine I did this 60frames/1second later print self.width #output: &gt;&gt;0 &gt;&gt;25 </code></pre> <p>What I want happening (as above)...
1
2009-10-27T16:47:04Z
1,632,350
<p>I don't understand the way you've constructed your example, nor do I understand what you mean about "normal properties" only working "on classes". Here's how you create a read-only property:</p> <pre><code>class Foo(object): # create read-only property "rop" rop = property(lambda self: self._x) def __i...
0
2009-10-27T17:19:15Z
[ "python", "properties", "python-2.6" ]
Using properties defined at a per-instance level not per-class
1,632,170
<p>What I am trying to achieve is something like this:</p> <pre><code>class object: def __init__(self): WidthVariable(self) print self.width #Imagine I did this 60frames/1second later print self.width #output: &gt;&gt;0 &gt;&gt;25 </code></pre> <p>What I want happening (as above)...
1
2009-10-27T16:47:04Z
1,632,376
<p>Not very clear what you want? but I assume you want obj.width to be customized for each instance Here is a easy way by using plain properties, width property returns value returned by a per instance callback</p> <pre><code>class MyClass(object): def __init__(self, callback): self.callback = callback ...
0
2009-10-27T17:24:15Z
[ "python", "properties", "python-2.6" ]
Using properties defined at a per-instance level not per-class
1,632,170
<p>What I am trying to achieve is something like this:</p> <pre><code>class object: def __init__(self): WidthVariable(self) print self.width #Imagine I did this 60frames/1second later print self.width #output: &gt;&gt;0 &gt;&gt;25 </code></pre> <p>What I want happening (as above)...
1
2009-10-27T16:47:04Z
1,632,870
<p>I believe I now understand your question, and I also believe you're <a href="http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes" rel="nofollow">out of luck</a>.</p> <blockquote> <p>For new-style classes, implicit invocations of special methods are only guaranteed to wo...
0
2009-10-27T18:47:27Z
[ "python", "properties", "python-2.6" ]
Using properties defined at a per-instance level not per-class
1,632,170
<p>What I am trying to achieve is something like this:</p> <pre><code>class object: def __init__(self): WidthVariable(self) print self.width #Imagine I did this 60frames/1second later print self.width #output: &gt;&gt;0 &gt;&gt;25 </code></pre> <p>What I want happening (as above)...
1
2009-10-27T16:47:04Z
1,633,363
<p>Since, as @Jonathan says, descriptors (including properties) are per-class, not per-instance, the only way to get different per-instance descriptors is to have each instance individualize its own class. That's pretty shallow and easy as far as metaprogramming goes;-)...:</p> <pre><code>class Individualistic(object_...
4
2009-10-27T20:09:38Z
[ "python", "properties", "python-2.6" ]
List running processes on 64-bit Windows
1,632,234
<p>I amm writing a little python script that will grab information from VMs of Windows that I am running.</p> <p>At the moment I can list the processes on a 32bit XP machine with the following method:</p> <p><a href="http://code.activestate.com/recipes/305279/" rel="nofollow">http://code.activestate.com/recipes/30527...
4
2009-10-27T16:55:38Z
1,632,265
<p>You should be able to do this by exposing <a href="http://msdn.microsoft.com/en-us/library/aa394582%28VS.85%29.aspx" rel="nofollow">Windows Management Instrumentation</a> within each VM. This tool gives you access to a bunch of system data, including processes, see <a href="http://technet.microsoft.com/en-us/library...
0
2009-10-27T17:03:19Z
[ "python", "windows", "process" ]
List running processes on 64-bit Windows
1,632,234
<p>I amm writing a little python script that will grab information from VMs of Windows that I am running.</p> <p>At the moment I can list the processes on a 32bit XP machine with the following method:</p> <p><a href="http://code.activestate.com/recipes/305279/" rel="nofollow">http://code.activestate.com/recipes/30527...
4
2009-10-27T16:55:38Z
1,632,274
<p>There is another recipe on activestate that does a similar thing, but uses the Performance Data Helper library (PDH) instead.</p> <p>I have tested this on my Windows 7 64bit machine and it works there - so presumably the same function will work on both 32bit and 64 bit windows.</p> <p>You can find the recipe here:...
12
2009-10-27T17:06:08Z
[ "python", "windows", "process" ]
List running processes on 64-bit Windows
1,632,234
<p>I amm writing a little python script that will grab information from VMs of Windows that I am running.</p> <p>At the moment I can list the processes on a 32bit XP machine with the following method:</p> <p><a href="http://code.activestate.com/recipes/305279/" rel="nofollow">http://code.activestate.com/recipes/30527...
4
2009-10-27T16:55:38Z
8,111,814
<p>If you don't want to rely on any extra installed modules then you can parse the output of <a href="http://technet.microsoft.com/en-us/library/bb742610.aspx">wmic</a>, e.g.:</p> <pre><code>c:\&gt; wmic process get description,executablepath ... explorer.exe C:\Windows\explorer.exe cmd.exe ...
15
2011-11-13T13:22:10Z
[ "python", "windows", "process" ]
List running processes on 64-bit Windows
1,632,234
<p>I amm writing a little python script that will grab information from VMs of Windows that I am running.</p> <p>At the moment I can list the processes on a 32bit XP machine with the following method:</p> <p><a href="http://code.activestate.com/recipes/305279/" rel="nofollow">http://code.activestate.com/recipes/30527...
4
2009-10-27T16:55:38Z
16,519,604
<p>For similar purposes I have used <a href="https://github.com/giampaolo/psutil" rel="nofollow">psutil</a> library. Some hints:</p> <ul> <li>list processes with <code>psutil.pids()</code> (<a href="http://pythonhosted.org/psutil/#psutil.pids" rel="nofollow">reference</a>)</li> <li>inspect process information with <co...
3
2013-05-13T10:14:59Z
[ "python", "windows", "process" ]
Best way to construct a "complex" data structure in Python
1,632,304
<p>I need to construct a tool that will be used to create field mappings (between tables) in the most automated manner possible.</p> <p>Here is the deal: imagine a table being appended to other. (lets ignore field type, just for a second...)</p> <pre><code>CREATE OR REPLACE TABLE fooA( id, name, type, foo) CREATE OR...
2
2009-10-27T17:11:08Z
1,632,327
<p>I'd honestly just take hints from (or use) <a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a> or <a href="http://docs.djangoproject.com/en/dev/topics/db/models/" rel="nofollow">Django Models</a>. These are tried and true data representation methods.</p>
5
2009-10-27T17:15:28Z
[ "python", "list", "data-structures" ]
Best way to construct a "complex" data structure in Python
1,632,304
<p>I need to construct a tool that will be used to create field mappings (between tables) in the most automated manner possible.</p> <p>Here is the deal: imagine a table being appended to other. (lets ignore field type, just for a second...)</p> <pre><code>CREATE OR REPLACE TABLE fooA( id, name, type, foo) CREATE OR...
2
2009-10-27T17:11:08Z
1,632,397
<p>Think about this</p> <pre><code>class Column( object ): def __init__( self, name, type_information=None ): self.name = name self.type_information = type_information self.pk = None self.fk_ref = None def fk( self, column ): self.fk_ref = column class Table( object ):...
1
2009-10-27T17:28:42Z
[ "python", "list", "data-structures" ]
Best way to construct a "complex" data structure in Python
1,632,304
<p>I need to construct a tool that will be used to create field mappings (between tables) in the most automated manner possible.</p> <p>Here is the deal: imagine a table being appended to other. (lets ignore field type, just for a second...)</p> <pre><code>CREATE OR REPLACE TABLE fooA( id, name, type, foo) CREATE OR...
2
2009-10-27T17:11:08Z
1,633,708
<p>Try to avoid accessing your data through fixed numerical indexes as in <code>fieldMap['tabelax'][0][1]</code>. After a year of not looking at your code, it may take you (or others) a while to figure out what it all means in human terms (e.g. "the value of idFoo in table tabelax"). Also, if you ever need to change yo...
2
2009-10-27T21:11:27Z
[ "python", "list", "data-structures" ]
Best way to construct a "complex" data structure in Python
1,632,304
<p>I need to construct a tool that will be used to create field mappings (between tables) in the most automated manner possible.</p> <p>Here is the deal: imagine a table being appended to other. (lets ignore field type, just for a second...)</p> <pre><code>CREATE OR REPLACE TABLE fooA( id, name, type, foo) CREATE OR...
2
2009-10-27T17:11:08Z
1,634,679
<p>Here is a little wrapper class for FooB's to mimic FooA's, but still retain their FooB-ishness.</p> <pre><code>from collections import namedtuple # use namedtuple to define some simple classes (requires Py2.6 or later) FooA = namedtuple('FooA', 'id name type foo') FooB = namedtuple('FooB', 'idfoo namefoo spam') #...
3
2009-10-28T01:22:24Z
[ "python", "list", "data-structures" ]
Python File Slurp w/ endian conversion
1,632,673
<p>It was recently asked <a href="http://stackoverflow.com/questions/1631897/python-file-slurp">how to do a file slurp in python</a>, and the accepted answer suggested something like:</p> <pre><code>with open('x.txt') as x: f = x.read() </code></pre> <p>How would I go about doing this to read the file in and convert ...
5
2009-10-27T18:11:43Z
1,632,783
<p>You could coble together an <a href="http://utilitybase.com/forum/index.php?action=vthread&amp;forum=16&amp;topic=170" rel="nofollow">ASM based solution</a> using <a href="http://www.corepy.org/" rel="nofollow">CorePy</a>. I wonder though, if you might be able to gain enough performance from the some other part of ...
0
2009-10-27T18:30:37Z
[ "python", "struct", "numpy", "endianness", "mmap" ]
Python File Slurp w/ endian conversion
1,632,673
<p>It was recently asked <a href="http://stackoverflow.com/questions/1631897/python-file-slurp">how to do a file slurp in python</a>, and the accepted answer suggested something like:</p> <pre><code>with open('x.txt') as x: f = x.read() </code></pre> <p>How would I go about doing this to read the file in and convert ...
5
2009-10-27T18:11:43Z
1,632,901
<p>I'd expect something like this to be faster</p> <pre><code>arrayName[0] = unpack('&gt;'+'f'*line_count*sample_count, map.read(arrayName.itemsize*line_count*sample_count)) </code></pre> <p>Please don't use <code>map</code> as a variable name</p>
0
2009-10-27T18:51:14Z
[ "python", "struct", "numpy", "endianness", "mmap" ]
Python File Slurp w/ endian conversion
1,632,673
<p>It was recently asked <a href="http://stackoverflow.com/questions/1631897/python-file-slurp">how to do a file slurp in python</a>, and the accepted answer suggested something like:</p> <pre><code>with open('x.txt') as x: f = x.read() </code></pre> <p>How would I go about doing this to read the file in and convert ...
5
2009-10-27T18:11:43Z
1,633,412
<pre><code>with open(fileName, "rb") as f: arrayName = numpy.fromfile(f, numpy.float32) arrayName.byteswap(True) </code></pre> <p>Pretty hard to beat for speed AND conciseness;-). For byteswap see <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.byteswap.html?highlight=byteswap#numpy.ndarr...
6
2009-10-27T20:19:49Z
[ "python", "struct", "numpy", "endianness", "mmap" ]
Python File Slurp w/ endian conversion
1,632,673
<p>It was recently asked <a href="http://stackoverflow.com/questions/1631897/python-file-slurp">how to do a file slurp in python</a>, and the accepted answer suggested something like:</p> <pre><code>with open('x.txt') as x: f = x.read() </code></pre> <p>How would I go about doing this to read the file in and convert ...
5
2009-10-27T18:11:43Z
1,633,525
<p>Slightly modified <a href="http://stackoverflow.com/questions/1632673/python-file-slurp-w-endian-conversion/1633412#1633412">@Alex Martelli's answer</a>:</p> <pre><code>arr = numpy.fromfile(filename, numpy.dtype('&gt;f4')) # no byteswap is needed regardless of endianess of the machine </code></pre>
7
2009-10-27T20:42:17Z
[ "python", "struct", "numpy", "endianness", "mmap" ]
Is a Python closure a good replacement for `__all__`?
1,632,739
<p>Is it a good idea to use a closure instead of <code>__all__</code> to limit the names exposed by a Python module? This would prevent programmers from accidentally using the wrong name for a module (<code>import urllib; urllib.os.getlogin()</code>) as well as avoiding "<code>from x import *</code>" namespace pollutio...
3
2009-10-27T18:21:19Z
1,632,926
<p>The problem with <code>from x import *</code> is that it can hide <code>NameErrors</code> which makes trivial bugs hard to track down. "namespace pollution" means adding stuff to the namespace that you have no idea where it came from.</p> <p>Which is kind of what your closure does too. Plus it might confuse IDEs, o...
4
2009-10-27T18:55:01Z
[ "python", "closures" ]
Is a Python closure a good replacement for `__all__`?
1,632,739
<p>Is it a good idea to use a closure instead of <code>__all__</code> to limit the names exposed by a Python module? This would prevent programmers from accidentally using the wrong name for a module (<code>import urllib; urllib.os.getlogin()</code>) as well as avoiding "<code>from x import *</code>" namespace pollutio...
3
2009-10-27T18:21:19Z
1,633,108
<p>I am a fan of writing code that is absolutely as brain-dead simple as it can be.</p> <p><code>__all__</code> is a feature of Python, added explicitly to solve the problem of limiting what names are made visible by a module. When you use it, people immediately understand what you are doing with it.</p> <p>Your clo...
7
2009-10-27T19:25:52Z
[ "python", "closures" ]
Is a Python closure a good replacement for `__all__`?
1,632,739
<p>Is it a good idea to use a closure instead of <code>__all__</code> to limit the names exposed by a Python module? This would prevent programmers from accidentally using the wrong name for a module (<code>import urllib; urllib.os.getlogin()</code>) as well as avoiding "<code>from x import *</code>" namespace pollutio...
3
2009-10-27T18:21:19Z
1,633,556
<p>Okay, I'm beginning to understand this issue a bit more. The closure really does allow for hiding private stuff. Here's a simple example.</p> <p>Without the closure:</p> <pre><code># module named "foo.py" def _bar(): return 5 def foo(): return _bar() - 2 </code></pre> <p>With the closure:</p> <pre><co...
1
2009-10-27T20:47:43Z
[ "python", "closures" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,632,988
<p>Your list comprehension and lambda are doing different things, the list comprehension matching the lambda would be <code>[val for val in T if val in S]</code>.</p> <p>Efficiency isn't the reason why list comprehension are preferred (while they actually are slightly faster in almost all cases). The reason why they a...
1
2009-10-27T19:04:52Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,632,994
<p>Your tests are doing very different things. With S being 1M elements and T being 300:</p> <pre><code>[x for x in S for y in T if x==y]= 54.875 </code></pre> <p>This option does 300M equality comparisons.</p> <h3>&nbsp;</h3> <pre><code>filter(lambda x:x in S,T)= 0.391000032425 </code></pre> <p>This option does 3...
23
2009-10-27T19:05:46Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,633,010
<p>Sets are the correct solution for this. However try swapping S and T and see how long it takes!</p> <pre><code>filter(lambda x:x in T,S) $ python -m timeit -s'S=[x for x in range(1000000)];T=[y**2 for y in range(300)]' 'filter(lambda x:x in S,T)' 10 loops, best of 3: 485 msec per loop $ python -m timeit -r1 -n1 -s...
2
2009-10-27T19:08:31Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,633,015
<p>First of all, test like this:</p> <pre><code>import timeit S=[x for x in range(10000)] T=[y**2 for y in range(30)] print "v1", timeit.Timer('[x for x in S for y in T if x==y]', 'from __main__ import S,T').timeit(100) print "v2", timeit.Timer('filter(lambda x:x in S,T)', 'from __main__ import S,T').t...
4
2009-10-27T19:08:58Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,633,036
<p>Your profiling is done wrong. Take a look the <a href="http://docs.python.org/library/timeit.html#module-timeit" rel="nofollow">timeit module</a> and try again.</p> <p><code>lambda</code> defines anonymous functions. Their main problem is that many people don't know the whole python library and use them to re-imple...
1
2009-10-27T19:12:38Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,633,053
<p>This is pretty fast:</p> <pre><code>def binary_search(a, x, lo=0, hi=None): if hi is None: hi = len(a) while lo &lt; hi: mid = (lo+hi)//2 midval = a[mid] if midval &lt; x: lo = mid+1 elif midval &gt; x: hi = mid else: retur...
0
2009-10-27T19:15:10Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,633,079
<p>When I fix your code so that the list comprehension and the call to <code>filter</code> are actually doing the same work things change a whole lot:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time() N=[x for x in T if x in S] time2 = time.time() print 'tim...
18
2009-10-27T19:19:22Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,633,284
<p>List comprehensions can make a bigger difference if you have to process your filtered results. In your case you just build a list, but if you had to do something like this:</p> <pre><code>n = [f(i) for i in S if some_condition(i)] </code></pre> <p>you would gain from LC optimization over this:</p> <pre><code>n = ...
0
2009-10-27T19:54:35Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,633,482
<p>Many people have already pointed out that you're comparing apples with oranges, etc, etc. But I think nobody's shown how to a really simple comparison -- list comprehension vs map plus lambda with little else to get in the way -- and that might be:</p> <pre><code>$ python -mtimeit -s'L=range(1000)' 'map(lambda x: ...
3
2009-10-27T20:32:44Z
[ "python", "lambda", "set", "list-comprehension" ]
lambda versus list comprehension performance
1,632,902
<p>I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test:</p> <pre><code>import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time...
11
2009-10-27T18:51:22Z
1,633,738
<p>Q: Why is lambda etc being pushed aside?</p> <p>A: List comprehensions and generator expressions are generally considered to be a nice mix of power and readability. The pure functional-programming style where you use <code>map()</code>, <code>reduce()</code>, and <code>filter()</code> with functions (often <code>l...
12
2009-10-27T21:15:51Z
[ "python", "lambda", "set", "list-comprehension" ]
Difference in python between basestring and types.StringType?
1,633,082
<p>What's the difference between:</p> <pre><code>isinstance(foo, types.StringType) </code></pre> <p>and</p> <pre><code>isinstance(foo, basestring) </code></pre> <p>?</p> <p>Thanks, /YGA</p> <p>PS I know I shouldn't use <code>isinstance</code> at all, blah blah blah.</p>
9
2009-10-27T19:19:36Z
1,633,094
<p><code>basestring</code> is the base class for both <code>str</code> and <code>unicode</code>, while <code>types.StringType</code> <em>is</em> <code>str</code>. If you want to check if something is a string, use <code>basestring</code>. If you want to check if something is a bytestring, use <code>str</code> and forge...
12
2009-10-27T19:22:47Z
[ "python" ]
Difference in python between basestring and types.StringType?
1,633,082
<p>What's the difference between:</p> <pre><code>isinstance(foo, types.StringType) </code></pre> <p>and</p> <pre><code>isinstance(foo, basestring) </code></pre> <p>?</p> <p>Thanks, /YGA</p> <p>PS I know I shouldn't use <code>isinstance</code> at all, blah blah blah.</p>
9
2009-10-27T19:19:36Z
1,633,178
<p>This stuff is completely different in Python3</p> <p><code>types</code> not longer has <code>StringType</code><br /> <code>str</code> is always unicode<br /> <code>basestring</code> no longer exists</p> <p>So try not to sprinkle that stuff through your code too much if you might ever need to port it</p>
8
2009-10-27T19:38:28Z
[ "python" ]
Difference in python between basestring and types.StringType?
1,633,082
<p>What's the difference between:</p> <pre><code>isinstance(foo, types.StringType) </code></pre> <p>and</p> <pre><code>isinstance(foo, basestring) </code></pre> <p>?</p> <p>Thanks, /YGA</p> <p>PS I know I shouldn't use <code>isinstance</code> at all, blah blah blah.</p>
9
2009-10-27T19:19:36Z
1,633,491
<pre><code>&gt;&gt;&gt; import types &gt;&gt;&gt; isinstance(u'ciao', types.StringType) False &gt;&gt;&gt; isinstance(u'ciao', basestring) True &gt;&gt;&gt; </code></pre> <p>Pretty important difference, it seems to me;-).</p>
1
2009-10-27T20:34:38Z
[ "python" ]
Difference in python between basestring and types.StringType?
1,633,082
<p>What's the difference between:</p> <pre><code>isinstance(foo, types.StringType) </code></pre> <p>and</p> <pre><code>isinstance(foo, basestring) </code></pre> <p>?</p> <p>Thanks, /YGA</p> <p>PS I know I shouldn't use <code>isinstance</code> at all, blah blah blah.</p>
9
2009-10-27T19:19:36Z
9,018,359
<p>For Python 2.x:</p> <pre><code>try: basestring # added in Python 2.3 except NameError: basestring = (str, unicode) ... if isinstance(foo, basestring): ... </code></pre> <p>Of course this might not work for Python 3, but I'm quite sure the 2to3 converter will take care of the topic.</p>
0
2012-01-26T12:53:09Z
[ "python" ]
creating a MIME email template with images to send with python / django
1,633,109
<p>In my web application I send emails occasionally using a reusable mailer application like this:</p> <pre><code>user - self.user subject = ("My subject") from = "me@mydomain.com" message = render_to_string("welcomeEmail/welcome.eml", { "user" : user, }) send_mail(subject, message, fr...
10
2009-10-27T19:26:10Z
1,633,493
<p><strong>Update</strong></p> <p>Many thanks to <a href="http://stackoverflow.com/users/1742777/saqib-ali">Saqib Ali</a> for resurrecting this old question nearly 5 years after my reply. </p> <p>The instructions I gave at the time no longer work. I suspect there have been some improvements to Django in the interveni...
21
2009-10-27T20:34:55Z
[ "python", "django", "email", "mime" ]
Getting a Python library listed in easy_setup and pip?
1,633,180
<p>Every Python developer is familiar with easy_install and setup tools. </p> <p>If I want to install a library that's well known, all I have to do is this:</p> <p>sudo easy_setup install django</p> <p>Now I have a library that I've written and would love to see widespread. How do you get added to this library list?...
9
2009-10-27T19:38:59Z
1,633,188
<p>Upload it to <a href="http://pypi.python.org/pypi">PyPI</a>. See the <a href="http://wiki.python.org/moin/CheeseShopTutorial">tutorial</a>.</p>
8
2009-10-27T19:40:49Z
[ "python" ]
python re: no such group
1,633,181
<p>I'm newbie in Python. I can't understand why this code does not work:</p> <pre><code>reOptions = re.search( "[\s+@twitter\s+(?P&lt;login&gt;\w+):(?P&lt;password&gt;.*?)\s+]", document_text) if reOptions: login = reOptions.group('login') password = reOptions.group('password') </code></pre> <p>I'm ha...
1
2009-10-27T19:39:28Z
1,633,193
<p>You need to escape the brackets [ and ] as <code>\[</code> and <code>\]</code>.</p> <pre><code>\[\s+@twitter\s+(?P&lt;login&gt;\w+):(?P&lt;password&gt;.*?)\s+\] </code></pre>
4
2009-10-27T19:41:59Z
[ "python", "regex" ]
python re: no such group
1,633,181
<p>I'm newbie in Python. I can't understand why this code does not work:</p> <pre><code>reOptions = re.search( "[\s+@twitter\s+(?P&lt;login&gt;\w+):(?P&lt;password&gt;.*?)\s+]", document_text) if reOptions: login = reOptions.group('login') password = reOptions.group('password') </code></pre> <p>I'm ha...
1
2009-10-27T19:39:28Z
1,633,194
<p>The <code>[</code> and <code>]</code> are special regular expression characters. Escape them to match literal <code>[</code> and <code>]</code>. See <a href="http://docs.python.org/library/re.html#regular-expression-syntax" rel="nofollow">Regular Expression Syntax</a>.</p>
3
2009-10-27T19:42:00Z
[ "python", "regex" ]
How to put parameterized sql query into variable and then execute in Python?
1,633,332
<p>I understand that the correct way to format a sql query in Python is like this:</p> <pre><code>cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", var1, var2, var3) </code></pre> <p>so that it prevents sql injection. My question is if there is a way to put the query in a variable and then execute it? I have tr...
4
2009-10-27T20:02:48Z
1,633,340
<p>You might try this:</p> <pre><code>sql="INSERT INTO table VALUES (%s, %s, %s)" cursor.execute(sql, var1, var2, var3) </code></pre> <p>But I'm not sure if that's what you're looking for. It depends on whether you want the values as part of the variable or not. If you do, Ants Aasma's answer is probably correct.</...
0
2009-10-27T20:05:23Z
[ "python", "sql" ]
How to put parameterized sql query into variable and then execute in Python?
1,633,332
<p>I understand that the correct way to format a sql query in Python is like this:</p> <pre><code>cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", var1, var2, var3) </code></pre> <p>so that it prevents sql injection. My question is if there is a way to put the query in a variable and then execute it? I have tr...
4
2009-10-27T20:02:48Z
1,633,344
<p>You're pretty close.</p> <pre><code>sql_and_params = "INSERT INTO table VALUES (%s, %s, %s)", var1, var2, var3 cursor.execute(*sql_and_params) </code></pre> <p>The asterisk means that the variable isn't to be considered as one parameter but instead unpacked into many parameters.</p>
4
2009-10-27T20:05:58Z
[ "python", "sql" ]
How to put parameterized sql query into variable and then execute in Python?
1,633,332
<p>I understand that the correct way to format a sql query in Python is like this:</p> <pre><code>cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", var1, var2, var3) </code></pre> <p>so that it prevents sql injection. My question is if there is a way to put the query in a variable and then execute it? I have tr...
4
2009-10-27T20:02:48Z
1,633,589
<p>Here is the call signature for cursor.execute:</p> <pre><code>Definition: cursor.execute(self, query, args=None) query -- string, query to execute on server args -- optional sequence or mapping, parameters to use with query. </code></pre> <p>So execute expects at most 3 arguments (args is optional). If ar...
10
2009-10-27T20:51:47Z
[ "python", "sql" ]
Advice on set-up/management of the WSGI stack?
1,633,342
<p>After looking through the many useful and shiny Python frameworks, I find none of them get close to what I need or provide <em>way</em> more than my needs. I'm looking to put something together myself; could define it as a framework, but not full-stack. However, I can't find online what the Python community sees as ...
2
2009-10-27T20:05:52Z
1,633,377
<p>I'd have to say that Apache/mod_wsgi is probably the most "manageable" of the setups I've used.</p> <p>nginx/fcgi is the fastest, but its a bit of a headache.</p>
1
2009-10-27T20:12:19Z
[ "python", "wsgi" ]
Advice on set-up/management of the WSGI stack?
1,633,342
<p>After looking through the many useful and shiny Python frameworks, I find none of them get close to what I need or provide <em>way</em> more than my needs. I'm looking to put something together myself; could define it as a framework, but not full-stack. However, I can't find online what the Python community sees as ...
2
2009-10-27T20:05:52Z
1,633,438
<p>What middleware do you think you need? You may very well not need to include any WSGI ‘middleware’-like components at all. You can perfectly well put together a loose ‘pseudo-framework’ of standalone libraries without needing to ‘wrap’ the application in middleware at all.</p> <p>(Personally I use a sep...
1
2009-10-27T20:25:25Z
[ "python", "wsgi" ]
Advice on set-up/management of the WSGI stack?
1,633,342
<p>After looking through the many useful and shiny Python frameworks, I find none of them get close to what I need or provide <em>way</em> more than my needs. I'm looking to put something together myself; could define it as a framework, but not full-stack. However, I can't find online what the Python community sees as ...
2
2009-10-27T20:05:52Z
1,633,580
<p>That is what a framework does. Some frameworks like Django are fairly rigid and others like Pylons make it easier to mix and match. </p> <p>Since you will likely be using some of the WSGI components from the Paste project sooner or later, you might as well read this article from the Paste folks about a <a href="htt...
4
2009-10-27T20:50:39Z
[ "python", "wsgi" ]
Advice on set-up/management of the WSGI stack?
1,633,342
<p>After looking through the many useful and shiny Python frameworks, I find none of them get close to what I need or provide <em>way</em> more than my needs. I'm looking to put something together myself; could define it as a framework, but not full-stack. However, I can't find online what the Python community sees as ...
2
2009-10-27T20:05:52Z
1,633,635
<p>My advice is to read the <a href="http://www.python.org/dev/peps/pep-0333/" rel="nofollow">PEP</a> on WSGI, specifically the part on <a href="http://www.python.org/dev/peps/pep-0333/#middleware-components-that-play-both-sides" rel="nofollow">middleware</a>. If you have a question about anything with the words "stand...
0
2009-10-27T20:58:35Z
[ "python", "wsgi" ]
Advice on set-up/management of the WSGI stack?
1,633,342
<p>After looking through the many useful and shiny Python frameworks, I find none of them get close to what I need or provide <em>way</em> more than my needs. I'm looking to put something together myself; could define it as a framework, but not full-stack. However, I can't find online what the Python community sees as ...
2
2009-10-27T20:05:52Z
1,634,032
<p>If you liked the Do-It-Yourself-Framework tutorial mentioned before, but you want to manage these things in a config file, <a href="http://pythonpaste.org/deploy/" rel="nofollow">Paste Deploy</a> would be the obvious answer. (It is mentioned in the tutorial, but only very briefly in the very last paragraph).</p> <p...
0
2009-10-27T22:30:20Z
[ "python", "wsgi" ]
Advice on set-up/management of the WSGI stack?
1,633,342
<p>After looking through the many useful and shiny Python frameworks, I find none of them get close to what I need or provide <em>way</em> more than my needs. I'm looking to put something together myself; could define it as a framework, but not full-stack. However, I can't find online what the Python community sees as ...
2
2009-10-27T20:05:52Z
1,634,724
<p>"it seems too simple when working with a number of middleware packages."</p> <p>How big a number?</p> <p>You won't be working with hundreds or thousands. </p> <p>It will be (a) a small number (under a dozen) and (b) the "right" order isn't magical. </p> <p>Each piece of middleware will have a very, very specifi...
0
2009-10-28T01:39:18Z
[ "python", "wsgi" ]
Amazon Web Service ItemSearch DetailPageURL's with Associate IDs?
1,633,357
<p>DetailPageURL's returned by ItemSearch seem to include an incorrect ID/tag rather than the associate ID I requested the search with.</p> <p>I'm getting:</p> <blockquote> <p><a href="http://rads.stackoverflow.com/amzn/click/1590595009" rel="nofollow">http://www.amazon.co.uk/gp/product/1590595009?SubscriptionId=XX...
1
2009-10-27T20:09:02Z
1,633,593
<p>Simple error in the end..... I was including the tag in the initial search:</p> <blockquote> <p>for searchResult in ecs.ItemSearch(item, SearchIndex=index, AssociateTag='wwwmydomain-12')</p> </blockquote> <p>But not in the secondary loop that steps through each result getting more details:</p> <blockquote...
2
2009-10-27T20:52:33Z
[ "python", "amazon-web-services", "amazon" ]
SQLAlchemy declarative concrete autoloaded table inheritance
1,633,447
<p>I've an already existing database and want to access it using SQLAlchemy. Because, the database structure's managed by another piece of code (Django ORM, actually) and I don't want to repeat myself, describing every table structure, I'm using <code>autoload</code> introspection. I'm stuck with a simple concrete tabl...
3
2009-10-27T20:26:40Z
1,636,168
<p>Your table structures are similar to what is used in joint table inheritance, but they certainly don't correspond to concrete table inheritance where all fields of parent class are duplicated in the table of subclass. Right now you have a subclass with less fields than parent and a reference to instance of parent cl...
4
2009-10-28T09:47:37Z
[ "python", "inheritance", "sqlalchemy", "autoload", "concrete" ]
Unix paths: officially work in Python for any platform?
1,633,643
<p>Can all paths in a Python program use ".." (for the parent directory) and / (for separating path components), and still work <em>whatever the platform</em>?</p> <p>On one hand, I have never seen such a claim in the documentation (I may have missed it), and the os and os.path modules do provide facilities for handli...
8
2009-10-27T21:01:05Z
1,633,714
<p>OS/X and Linux are both Unix compatible, so by definition they use the format you gave at the beginning of the question. Windows allows "/" in addition to "\" so that programs could be interchangeable with Xenix, a Unix variant that Microsoft was trying out a long time ago, and that compatibility has been carried f...
1
2009-10-27T21:12:14Z
[ "python", "path", "portability", "relative-path" ]
Unix paths: officially work in Python for any platform?
1,633,643
<p>Can all paths in a Python program use ".." (for the parent directory) and / (for separating path components), and still work <em>whatever the platform</em>?</p> <p>On one hand, I have never seen such a claim in the documentation (I may have missed it), and the os and os.path modules do provide facilities for handli...
8
2009-10-27T21:01:05Z
1,633,719
<p>Within python, using <code>/</code> will always work. You will need to be aware of the OS convention if you want to execute a command in a subshell</p> <pre><code>myprog = "/path/to/my/program" os.system([myprog, "-n"]) # 1 os.system([myprog, "C:/input/file/to/myprog"]) # 2 </code></p...
3
2009-10-27T21:12:40Z
[ "python", "path", "portability", "relative-path" ]
Unix paths: officially work in Python for any platform?
1,633,643
<p>Can all paths in a Python program use ".." (for the parent directory) and / (for separating path components), and still work <em>whatever the platform</em>?</p> <p>On one hand, I have never seen such a claim in the documentation (I may have missed it), and the os and os.path modules do provide facilities for handli...
8
2009-10-27T21:01:05Z
1,633,721
<p>Windows supports <code>/</code> as a path separator. The only incompatibilities between Unix filenames and Windows filenames are:</p> <ul> <li>the allowed characters in filenames</li> <li>the special names and</li> <li>case sensitivity</li> </ul> <p><a href="http://msdn.microsoft.com/en-us/library/aa365247.aspx" r...
3
2009-10-27T21:12:50Z
[ "python", "path", "portability", "relative-path" ]
Unix paths: officially work in Python for any platform?
1,633,643
<p>Can all paths in a Python program use ".." (for the parent directory) and / (for separating path components), and still work <em>whatever the platform</em>?</p> <p>On one hand, I have never seen such a claim in the documentation (I may have missed it), and the os and os.path modules do provide facilities for handli...
8
2009-10-27T21:01:05Z
1,633,725
<p>I've never had any problems with using <code>..</code>, although it might be a good idea to convert it to an absolute path using <a href="http://docs.python.org/library/os.path.html#os.path.abspath">os.path.abspath</a>. Secondly, I would recommend always using os.path.join whereever possible. There are a lot of co...
11
2009-10-27T21:13:19Z
[ "python", "path", "portability", "relative-path" ]
Unix paths: officially work in Python for any platform?
1,633,643
<p>Can all paths in a Python program use ".." (for the parent directory) and / (for separating path components), and still work <em>whatever the platform</em>?</p> <p>On one hand, I have never seen such a claim in the documentation (I may have missed it), and the os and os.path modules do provide facilities for handli...
8
2009-10-27T21:01:05Z
1,633,742
<p>It works on Windows, so if you define "whatever the platform" to be Unix and Windows, you're fine.</p> <p>On the other hand, Python also runs on VMS, RISC OS, and other odd platforms that use completely different filename conventions. However, it's probable that trying to get your application to run on VMS, blind, ...
3
2009-10-27T21:16:30Z
[ "python", "path", "portability", "relative-path" ]
Unix paths: officially work in Python for any platform?
1,633,643
<p>Can all paths in a Python program use ".." (for the parent directory) and / (for separating path components), and still work <em>whatever the platform</em>?</p> <p>On one hand, I have never seen such a claim in the documentation (I may have missed it), and the os and os.path modules do provide facilities for handli...
8
2009-10-27T21:01:05Z
1,633,753
<p>As others have said, a forward slash will work in all cases, but you're better off creating a list of path segments and os.path.join()-ing them.</p>
0
2009-10-27T21:19:00Z
[ "python", "path", "portability", "relative-path" ]
Unix paths: officially work in Python for any platform?
1,633,643
<p>Can all paths in a Python program use ".." (for the parent directory) and / (for separating path components), and still work <em>whatever the platform</em>?</p> <p>On one hand, I have never seen such a claim in the documentation (I may have missed it), and the os and os.path modules do provide facilities for handli...
8
2009-10-27T21:01:05Z
1,633,870
<p>"Almost always safe" is right. All of the platforms you care about probably work ok today and I don't think they will be changing their conventions any time soon.</p> <p>However Python is very portable and runs on a lot more than the usual platforms. The reason for the <code>os</code> module is to help smooth thing...
6
2009-10-27T21:48:56Z
[ "python", "path", "portability", "relative-path" ]
How to Disable Django / mod_WSGI Page Caching
1,633,684
<p>I have Django running in Apache via mod_wsgi. I believe Django is caching my pages server-side, which is causing some of the functionality to not work correctly.</p> <p>I have a countdown timer that works by getting the current server time, determining the remaining countdown time, and outputting that number to the...
7
2009-10-27T21:08:10Z
1,633,771
<p>Did you specifically setup Django caching? From the docs it seems you would clearly know if Django was caching as it requires work beforehand to get it working. Specifically, you need to define where the cached files are saved.</p> <p><a href="http://docs.djangoproject.com/en/dev/topics/cache/" rel="nofollow">http...
1
2009-10-27T21:25:13Z
[ "python", "django", "apache", "caching", "mod-wsgi" ]
How to Disable Django / mod_WSGI Page Caching
1,633,684
<p>I have Django running in Apache via mod_wsgi. I believe Django is caching my pages server-side, which is causing some of the functionality to not work correctly.</p> <p>I have a countdown timer that works by getting the current server time, determining the remaining countdown time, and outputting that number to the...
7
2009-10-27T21:08:10Z
1,634,871
<p>From my experience with mod_wsgi in Apache, it is highly unlikely that they are causing caching. A couple of things to try:</p> <ol> <li>It is possible that you have some <a href="http://www.mnot.net/cache%5Fdocs/">proxy server</a> between your computer and the web server that is appropriately or inappropriately c...
5
2009-10-28T02:25:50Z
[ "python", "django", "apache", "caching", "mod-wsgi" ]
How to Disable Django / mod_WSGI Page Caching
1,633,684
<p>I have Django running in Apache via mod_wsgi. I believe Django is caching my pages server-side, which is causing some of the functionality to not work correctly.</p> <p>I have a countdown timer that works by getting the current server time, determining the remaining countdown time, and outputting that number to the...
7
2009-10-27T21:08:10Z
1,636,161
<p>Are you using a multiprocess configuration for Apache/mod_wsgi? If you are, that will account for why different responses can have a different value for the timer as likely that when timer is initialised will be different for each process handling requests. Thus why it can jump around.</p> <p>Have a read of:</p> <...
1
2009-10-28T09:45:08Z
[ "python", "django", "apache", "caching", "mod-wsgi" ]
How to Disable Django / mod_WSGI Page Caching
1,633,684
<p>I have Django running in Apache via mod_wsgi. I believe Django is caching my pages server-side, which is causing some of the functionality to not work correctly.</p> <p>I have a countdown timer that works by getting the current server time, determining the remaining countdown time, and outputting that number to the...
7
2009-10-27T21:08:10Z
16,753,509
<p>I just came across this:</p> <blockquote> <p>Support for Automatic Reloading To help deployment tools you can activate support for automatic reloading. Whenever something changes the .wsgi file, mod_wsgi will reload all the daemon processes for us.</p> <p>For that, just add the following directive to you...
2
2013-05-25T20:16:30Z
[ "python", "django", "apache", "caching", "mod-wsgi" ]
Oriented forest TAoCP - Algorithm in python
1,633,833
<p>I try to implement <a href="http://books.google.com.au/books?id=56LNfE2QGtYC&amp;lpg=PA23&amp;ots=vMcdKi%5FeYa&amp;dq=taocp%20oriented%20forests&amp;pg=PA24#v=onepage&amp;q=&amp;f=false" rel="nofollow">Algorithm O (Oriented forests)</a> from Donald E. Knuth: 'The Art of Computer Programming - Volume 4, Fascile 4, Ge...
2
2009-10-27T21:40:34Z
1,634,240
<p>I refactored your code a little, mostly to eliminate the duplication in step 5.<br /> However the output is still the same as you are getting</p> <pre><code>def generate_oriented_forest(n): """Algorithm O from Knuth TAoCP, Fascicle 4, p. 25. """ p = range(-1,n) while True: yield p[1:] if...
0
2009-10-27T23:18:42Z
[ "python", "algorithm", "taocp" ]
Oriented forest TAoCP - Algorithm in python
1,633,833
<p>I try to implement <a href="http://books.google.com.au/books?id=56LNfE2QGtYC&amp;lpg=PA23&amp;ots=vMcdKi%5FeYa&amp;dq=taocp%20oriented%20forests&amp;pg=PA24#v=onepage&amp;q=&amp;f=false" rel="nofollow">Algorithm O (Oriented forests)</a> from Donald E. Knuth: 'The Art of Computer Programming - Volume 4, Fascile 4, Ge...
2
2009-10-27T21:40:34Z
1,634,938
<p>I do not understand the algorithm and have no idea if my suggested change to the algorithm (below) is correct or not. All I know is that it produces the expected output that you quote for n=4:</p> <pre><code>def generate_oriented_forest(n): """Algorithm O from Knuth TAoCP, Fascicle 4, p. 25. """ p = range(-...
0
2009-10-28T02:41:25Z
[ "python", "algorithm", "taocp" ]
Oriented forest TAoCP - Algorithm in python
1,633,833
<p>I try to implement <a href="http://books.google.com.au/books?id=56LNfE2QGtYC&amp;lpg=PA23&amp;ots=vMcdKi%5FeYa&amp;dq=taocp%20oriented%20forests&amp;pg=PA24#v=onepage&amp;q=&amp;f=false" rel="nofollow">Algorithm O (Oriented forests)</a> from Donald E. Knuth: 'The Art of Computer Programming - Volume 4, Fascile 4, Ge...
2
2009-10-27T21:40:34Z
1,646,677
<p>I digged up the original articel: SIAM Journal of Computing, T. Beyer and S.M. Hedetniemi, 'Constant Time generation of rooted trees". The paper explains the algorithm really well. Here's my implementation:</p> <pre><code>def generate_oriented_forest_2(n): """SIAM J. COMPUT. Vol. 9, No. 4, November 1980 T....
0
2009-10-29T21:29:47Z
[ "python", "algorithm", "taocp" ]
Oriented forest TAoCP - Algorithm in python
1,633,833
<p>I try to implement <a href="http://books.google.com.au/books?id=56LNfE2QGtYC&amp;lpg=PA23&amp;ots=vMcdKi%5FeYa&amp;dq=taocp%20oriented%20forests&amp;pg=PA24#v=onepage&amp;q=&amp;f=false" rel="nofollow">Algorithm O (Oriented forests)</a> from Donald E. Knuth: 'The Art of Computer Programming - Volume 4, Fascile 4, Ge...
2
2009-10-27T21:40:34Z
1,714,382
<p><strong>Congratulations!</strong></p> <p>It sure looks like you just found an error in TAOCP. As you no doubt know, there's a reward of one hexadecimal dollar (drawn on the Bank of San Seriffe) for the first finder of such errors. I've got one, and I can tell you, it's a hell of thing to put on your wall. </p> <...
2
2009-11-11T10:40:40Z
[ "python", "algorithm", "taocp" ]
Oriented forest TAoCP - Algorithm in python
1,633,833
<p>I try to implement <a href="http://books.google.com.au/books?id=56LNfE2QGtYC&amp;lpg=PA23&amp;ots=vMcdKi%5FeYa&amp;dq=taocp%20oriented%20forests&amp;pg=PA24#v=onepage&amp;q=&amp;f=false" rel="nofollow">Algorithm O (Oriented forests)</a> from Donald E. Knuth: 'The Art of Computer Programming - Volume 4, Fascile 4, Ge...
2
2009-10-27T21:40:34Z
5,344,978
<p>The answer is: everyone is right!</p> <p>Knuth's algorithm calculates the parent codes, not the level codes. It seems "THE Donald" was still thinking in terms of parent codes, even while remarking that the B &amp; H algorithm uses level codes instead.</p> <p>However, if you look at the text of Algorithm O, you sho...
0
2011-03-17T20:38:58Z
[ "python", "algorithm", "taocp" ]
Google Analytics and Python
1,633,899
<p>I'm brand new at Python and I'm trying to write an extension to an app that imports GA information and parses it into MySQL. There is a shamfully sparse amount of infomation on the topic. The Google Docs only seem to have examples in JS and Java... </p> <p>...I have gotten to the point where my user can authenticat...
5
2009-10-27T21:57:06Z
1,634,790
<p>Maybe <a href="http://www.bubblefoundry.com/blog/tag/gdata-python-client/" rel="nofollow">this post</a> can help out. Seems like there are not Analytics specific bindings yet, so you are working with the generic gdata.</p>
3
2009-10-28T02:05:56Z
[ "python", "google-analytics", "analytics", "google-analytics-api" ]
Google Analytics and Python
1,633,899
<p>I'm brand new at Python and I'm trying to write an extension to an app that imports GA information and parses it into MySQL. There is a shamfully sparse amount of infomation on the topic. The Google Docs only seem to have examples in JS and Java... </p> <p>...I have gotten to the point where my user can authenticat...
5
2009-10-27T21:57:06Z
1,897,866
<p>I've been using GA for a little over a year now and since about April 2009, i have used python bindings supplied in a package called python-googleanalytics by Clint Ecker et al. So far, it works quite well.</p> <p>Here's where to get it: <a href="http://github.com/clintecker/python-googleanalytics" rel="nofollow">h...
2
2009-12-13T21:37:02Z
[ "python", "google-analytics", "analytics", "google-analytics-api" ]
Google Analytics and Python
1,633,899
<p>I'm brand new at Python and I'm trying to write an extension to an app that imports GA information and parses it into MySQL. There is a shamfully sparse amount of infomation on the topic. The Google Docs only seem to have examples in JS and Java... </p> <p>...I have gotten to the point where my user can authenticat...
5
2009-10-27T21:57:06Z
23,874,282
<p>You need 3 files within the app. client_secrets.json, analytics.dat and google_auth.py.</p> <p><strong>Create a module Query.py within the app:</strong></p> <pre><code>class Query(object): def __init__(self, startdate, enddate, filter, metrics): self.startdate = startdate.strftime('%Y-%m-%d') s...
0
2014-05-26T16:24:56Z
[ "python", "google-analytics", "analytics", "google-analytics-api" ]
Slice a string after a certain phrase?
1,633,932
<p>I've got a batch of strings that I need to cut down. They're basically a descriptor followed by codes. I only want to keep the descriptor.</p> <pre><code>'a descriptor dps 23 fd' 'another 23 fd' 'and another fd' 'and one without a code' </code></pre> <p>The codes above are <code>dps</code>, <code>23</code> and <co...
5
2009-10-27T22:03:44Z
1,634,000
<p>The short answer, as @THC4K points out in a comment:</p> <pre><code>string.split(pattern, 1)[0] </code></pre> <p>where <code>string</code> is your original string, <code>pattern</code> is your "break" pattern, <code>1</code> indicates to split no more than 1 time, and <code>[0]</code> means take the first element ...
15
2009-10-27T22:21:03Z
[ "python" ]
Slice a string after a certain phrase?
1,633,932
<p>I've got a batch of strings that I need to cut down. They're basically a descriptor followed by codes. I only want to keep the descriptor.</p> <pre><code>'a descriptor dps 23 fd' 'another 23 fd' 'and another fd' 'and one without a code' </code></pre> <p>The codes above are <code>dps</code>, <code>23</code> and <co...
5
2009-10-27T22:03:44Z
1,634,033
<p>You seem to be describing something like this:</p> <pre><code>def get_descriptor(text): codes = ('12', 'dps', '23') for c in codes: try: return text[:text.index(c)].rstrip() except ValueError: continue raise ValueError("No descriptor found in `%s'" % (text)) </co...
2
2009-10-27T22:30:20Z
[ "python" ]
Slice a string after a certain phrase?
1,633,932
<p>I've got a batch of strings that I need to cut down. They're basically a descriptor followed by codes. I only want to keep the descriptor.</p> <pre><code>'a descriptor dps 23 fd' 'another 23 fd' 'and another fd' 'and one without a code' </code></pre> <p>The codes above are <code>dps</code>, <code>23</code> and <co...
5
2009-10-27T22:03:44Z
1,634,313
<pre><code>codes = ('12', 'dps', '23') def get_descriptor(text): words = text.split() for c in codes: if c in words: i = words.index(c) return " ".join(words[:i]) raise ValueError("No code found in `%s'" % (text)) </code></pre>
1
2009-10-27T23:31:30Z
[ "python" ]
Slice a string after a certain phrase?
1,633,932
<p>I've got a batch of strings that I need to cut down. They're basically a descriptor followed by codes. I only want to keep the descriptor.</p> <pre><code>'a descriptor dps 23 fd' 'another 23 fd' 'and another fd' 'and one without a code' </code></pre> <p>The codes above are <code>dps</code>, <code>23</code> and <co...
5
2009-10-27T22:03:44Z
1,634,503
<p>I'd probably use a regular expression to do this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; descriptors = ('foo x', 'foo y', 'bar $', 'baz', 'bat') &gt;&gt;&gt; data = ['foo x 123', 'foo y 123', 'bar $123', 'baz 123', 'bat 123', 'nothing'] &gt;&gt;&gt; p = re.compile("(" + "|".join(map(re.escape, descripto...
1
2009-10-28T00:25:59Z
[ "python" ]
Slice a string after a certain phrase?
1,633,932
<p>I've got a batch of strings that I need to cut down. They're basically a descriptor followed by codes. I only want to keep the descriptor.</p> <pre><code>'a descriptor dps 23 fd' 'another 23 fd' 'and another fd' 'and one without a code' </code></pre> <p>The codes above are <code>dps</code>, <code>23</code> and <co...
5
2009-10-27T22:03:44Z
1,637,434
<p>Here's an answer that works for all codes rather than forcing you to call the function for each code, and is a bit simpler than some of the answers above. It also works for all of your examples.</p> <pre><code>strings = ('a descriptor dps 23 fd', 'another 23 fd', 'and another fd', 'and one without...
0
2009-10-28T13:59:18Z
[ "python" ]
Designing a simple network packet
1,633,934
<p>I'm learning socket programming (in python) and I was wondering what the best/typical way of encapsulating data is? My packets will be used to issue run, stop, configure, etc. commands on the receiving side. Is it helpful to use JSON or just straight text?</p>
3
2009-10-27T22:03:58Z
1,634,093
<p>I suggest you use a fixed, or mostly fixed format, as this make things easier.<br> By then using features such as the standard library's <strong>struct.Struct</strong>, with its pack() and umpack() methods, or possibly a slightly more featured pacakges such as <a href="http://construct.wikispaces.com/" rel="nofollow...
1
2009-10-27T22:44:20Z
[ "python", "network-programming" ]
Designing a simple network packet
1,633,934
<p>I'm learning socket programming (in python) and I was wondering what the best/typical way of encapsulating data is? My packets will be used to issue run, stop, configure, etc. commands on the receiving side. Is it helpful to use JSON or just straight text?</p>
3
2009-10-27T22:03:58Z
1,634,178
<p>If you're developing something as a learning exercise you might find it best to go with a structured text (ie. human readable and human writable) format.</p> <p>An example would be to use a fixed number of fields per command, fixed width text fields and/or easily parsable field delimiters.</p> <p>Generally text is...
0
2009-10-27T23:03:42Z
[ "python", "network-programming" ]
Designing a simple network packet
1,633,934
<p>I'm learning socket programming (in python) and I was wondering what the best/typical way of encapsulating data is? My packets will be used to issue run, stop, configure, etc. commands on the receiving side. Is it helpful to use JSON or just straight text?</p>
3
2009-10-27T22:03:58Z
1,634,957
<p>Take a look at how <a href="http://www.secdev.org/projects/scapy/doc/" rel="nofollow">scapy</a> (an awesome Python packet manipulation library) implements it. Looks like that have a <a href="http://www.secdev.org/projects/scapy/doc/build%5Fdissect.html#fields" rel="nofollow">handful of fields</a>.</p>
0
2009-10-28T02:50:10Z
[ "python", "network-programming" ]
Designing a simple network packet
1,633,934
<p>I'm learning socket programming (in python) and I was wondering what the best/typical way of encapsulating data is? My packets will be used to issue run, stop, configure, etc. commands on the receiving side. Is it helpful to use JSON or just straight text?</p>
3
2009-10-27T22:03:58Z
1,635,005
<p>I suggest plain text to begin with - it is easier to debug. The format that your text takes depends on what you're doing, how many commands, arguments, etc. Have you fleshed out how your commands will look? Once you figure out what that looks like it'll likely suggest a format all on its own.</p> <p>Are you using T...
1
2009-10-28T03:09:51Z
[ "python", "network-programming" ]
Extracting ALL matches of a nested regular expression in python
1,633,984
<p>I am trying to parse a list of items which satisfies the python regex</p> <pre><code>r'\A(("[\w\s]+"|\w+)\s+)*\Z' </code></pre> <p>that is, it's a space separated list except that spaces are allowed inside quoted strings. I would like to get a list of items in the list (that is of items matched by the</p> <pre><c...
1
2009-10-27T22:16:53Z
1,634,124
<p>I don't think that regex is the right tool here. Try <a href="http://docs.python.org/library/csv.html" rel="nofollow"><code>csv</code></a> module:</p> <pre><code>&gt;&gt;&gt; s = 'foo "bar baz" "bob" ' &gt;&gt;&gt; for i in csv.reader([s], delimiter=' '): print(i) ['foo', 'bar baz', 'bob', ''] </code></pre>
2
2009-10-27T22:50:49Z
[ "python", "regex", "parsing" ]
Extracting ALL matches of a nested regular expression in python
1,633,984
<p>I am trying to parse a list of items which satisfies the python regex</p> <pre><code>r'\A(("[\w\s]+"|\w+)\s+)*\Z' </code></pre> <p>that is, it's a space separated list except that spaces are allowed inside quoted strings. I would like to get a list of items in the list (that is of items matched by the</p> <pre><c...
1
2009-10-27T22:16:53Z
1,634,442
<p>Here's a solution that splits on any whitespace that isn't inside a pair of quotation marks:</p> <pre><code>re.split('\s+(?=[^"]*(?:"[^"]*"[^"]*)*$)', target) </code></pre> <p>The lookahead succeeds only if there's an even number of quotation marks ahead of the just-matched whitespace. If quoted sections in your ...
1
2009-10-28T00:10:03Z
[ "python", "regex", "parsing" ]
Extracting ALL matches of a nested regular expression in python
1,633,984
<p>I am trying to parse a list of items which satisfies the python regex</p> <pre><code>r'\A(("[\w\s]+"|\w+)\s+)*\Z' </code></pre> <p>that is, it's a space separated list except that spaces are allowed inside quoted strings. I would like to get a list of items in the list (that is of items matched by the</p> <pre><c...
1
2009-10-27T22:16:53Z
1,652,105
<p>Alright, I ended up deciding to do this in two steps.</p> <p>First I check that the expression is syntactically valid and second I break it into individual pieces:</p> <pre><code>def parse(expr): if re.match(r'\A(("[\w\s]+"|\w+)\s+)*\Z', expr): return re.findall(r'("[\w\s]+"|\w+)', expr) </code></pre> ...
1
2009-10-30T20:26:46Z
[ "python", "regex", "parsing" ]
Python, lambda, find minimum
1,634,054
<p>I have foreach function which calls specified function on every element which it contains. I want to get minimum from thise elements but I have no idea how to write lambda or function or even a class that would manage that. Thanks for every help. <hr> I use my foreach function like this:</p> <pre><code>o.foreach( l...
5
2009-10-27T22:37:03Z
1,634,143
<p>Python has built-in <a href="http://docs.python.org/library/functions.html#min" rel="nofollow">support for finding minimums</a>:</p> <pre><code>&gt;&gt;&gt; min([1, 2, 3]) 1 </code></pre> <p>If you need to process the list with a function first, you can do that with <a href="http://docs.python.org/library/function...
11
2009-10-27T22:54:27Z
[ "python", "lambda" ]
Python, lambda, find minimum
1,634,054
<p>I have foreach function which calls specified function on every element which it contains. I want to get minimum from thise elements but I have no idea how to write lambda or function or even a class that would manage that. Thanks for every help. <hr> I use my foreach function like this:</p> <pre><code>o.foreach( l...
5
2009-10-27T22:37:03Z
1,634,164
<p>Okay, one thing you need to understand: <code>lambda</code> creates a function object for you. But so does plain, ordinary <code>def</code>. Look at this example:</p> <pre><code>lst = range(10) print filter(lambda x: x % 2 == 0, lst) def is_even(x): return x % 2 == 0 print filter(is_even, lst) </code></pre...
1
2009-10-27T23:00:28Z
[ "python", "lambda" ]
Python, lambda, find minimum
1,634,054
<p>I have foreach function which calls specified function on every element which it contains. I want to get minimum from thise elements but I have no idea how to write lambda or function or even a class that would manage that. Thanks for every help. <hr> I use my foreach function like this:</p> <pre><code>o.foreach( l...
5
2009-10-27T22:37:03Z
1,634,319
<p>Suppose you have</p> <pre><code>&gt;&gt;&gt; seq = range(-4,4) &gt;&gt;&gt; def f(x): ... return x*x-2 </code></pre> <p>for the minimum value of f</p> <pre><code>&gt;&gt;&gt; min(f(x) for x in seq) -2 </code></pre> <p>for the value of x at the minimum</p> <pre><code>&gt;&gt;&gt; min(seq, key=f) 0 </code></pre>...
0
2009-10-27T23:32:35Z
[ "python", "lambda" ]
Python, lambda, find minimum
1,634,054
<p>I have foreach function which calls specified function on every element which it contains. I want to get minimum from thise elements but I have no idea how to write lambda or function or even a class that would manage that. Thanks for every help. <hr> I use my foreach function like this:</p> <pre><code>o.foreach( l...
5
2009-10-27T22:37:03Z
1,634,395
<blockquote> <p>I have foreach function which calls specified function on every element which it contains</p> </blockquote> <p>It sounds, from the comment you subsequently posted, that you have re-invented the built-in <code>map</code> function.</p> <p>It sounds like you're looking for something like this:</p> <pr...
1
2009-10-27T23:53:03Z
[ "python", "lambda" ]
Python, lambda, find minimum
1,634,054
<p>I have foreach function which calls specified function on every element which it contains. I want to get minimum from thise elements but I have no idea how to write lambda or function or even a class that would manage that. Thanks for every help. <hr> I use my foreach function like this:</p> <pre><code>o.foreach( l...
5
2009-10-27T22:37:03Z
1,634,418
<p>You can't do this with <code>foreach</code> and a lambda. If you want to do this in a functional style without actually using <code>min</code>, you'll find <code>reduce</code> is pretty close to the function you were trying to define.</p> <pre><code>l = [5,2,6,7,9,8] reduce(lambda a,b: a if a &lt; b else b, l[1:], ...
4
2009-10-28T00:00:01Z
[ "python", "lambda" ]
Python, lambda, find minimum
1,634,054
<p>I have foreach function which calls specified function on every element which it contains. I want to get minimum from thise elements but I have no idea how to write lambda or function or even a class that would manage that. Thanks for every help. <hr> I use my foreach function like this:</p> <pre><code>o.foreach( l...
5
2009-10-27T22:37:03Z
1,635,302
<p>Writing <code>foreach</code> method is not very pythonic. You should better make it an iterator so that it works with standard python functions like <code>min</code>.</p> <p>Instead of writing something like this:</p> <pre><code>def foreach(self, f): for d in self._data: f(d) </code></pre> <p>write th...
5
2009-10-28T05:09:46Z
[ "python", "lambda" ]
python line.split semantic error fromtext file
1,634,117
<p>this section of code is supposed to make a list containing the values of the second column in the text file, but it takes the second letter. anyone know what my problem is? </p> <pre><code>TEXTFILE opi 60 kid 60 pou 60 ret 60 kai 60 bob 100 for line in lst: line.split(' ...
0
2009-10-27T22:49:27Z
1,634,136
<pre><code>line.split(' ') </code></pre> <p>Your mistake is there, the <code>split</code> doesn't split <code>line</code> but returns a line split. So instead what you need to do is</p> <pre><code>columns = line.split(' ') </code></pre> <p>and use columns instead</p>
0
2009-10-27T22:53:28Z
[ "python", "string" ]