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
Prevent python imports compiling
1,331,235
<p>I have have a python file that imports a few frequently changed python files. I have had trouble with the imported files not recompiling when I change them. How do I stop them compiling?</p>
1
2009-08-25T21:52:55Z
1,331,279
<p>There are some modules which might help you:</p> <p>The py_compile module (<a href="http://effbot.org/librarybook/py-compile.htm" rel="nofollow">http://effbot.org/librarybook/py-compile.htm</a>) will allow you to explicitly compile modules (without running them like the 'import' statement does).</p> <pre><code>imp...
1
2009-08-25T22:02:59Z
[ "python", "import", "compilation" ]
Prevent python imports compiling
1,331,235
<p>I have have a python file that imports a few frequently changed python files. I have had trouble with the imported files not recompiling when I change them. How do I stop them compiling?</p>
1
2009-08-25T21:52:55Z
1,331,744
<p>You are looking for <a href="http://docs.python.org/library/compileall.html" rel="nofollow">compileall</a></p> <blockquote> <p>compileall.compile_dir(dir[, maxlevels[, ddir[, force[, rx[, quiet]]]]])</p> <p>Recursively descend the directory tree named by dir, compiling all .py files along the way.</p> ...
0
2009-08-26T00:27:45Z
[ "python", "import", "compilation" ]
Prevent python imports compiling
1,331,235
<p>I have have a python file that imports a few frequently changed python files. I have had trouble with the imported files not recompiling when I change them. How do I stop them compiling?</p>
1
2009-08-25T21:52:55Z
1,331,831
<p>In python 2.6, you should be able to supply the -B option.</p>
1
2009-08-26T00:58:40Z
[ "python", "import", "compilation" ]
iTunes COM interface
1,331,438
<p>I would like to be able to control iTunes with python.</p> <p>I know this is possible and I successfully tested some examples I have seen here at stackoverflow and in other sites, too.</p> <p>However, I would like to read the documentation about this. I have registered in Mac Dev Center, and when try to access the...
0
2009-08-25T22:45:40Z
1,332,030
<p>Apple has moved things around and failed to update their sites appropriately (trying to download the itunes sdk directly does bounce you back as you've observed), but by faithfully following the instructions found <a href="http://www.paraesthesia.com/archive/2009/05/20/itunes-com-for-windows-sdk-now-in-adc.aspx" rel...
3
2009-08-26T02:11:24Z
[ "python", "com", "itunes" ]
iTunes COM interface
1,331,438
<p>I would like to be able to control iTunes with python.</p> <p>I know this is possible and I successfully tested some examples I have seen here at stackoverflow and in other sites, too.</p> <p>However, I would like to read the documentation about this. I have registered in Mac Dev Center, and when try to access the...
0
2009-08-25T22:45:40Z
5,668,626
<p>Pre-Scriptum: I use the COM for Windows, which is usable through Python.</p> <p>I have been able to download (leagaly) both an ActivePython install (<a href="http://www.activestate.com/activepython/downloads" rel="nofollow">here</a>) and the documentation for it (although i can't seem to find the link anymore, I ha...
0
2011-04-14T19:31:18Z
[ "python", "com", "itunes" ]
In-memory size of a Python structure
1,331,471
<p>Is there a reference for the memory size of Python data stucture on 32- and 64-bit platforms?</p> <p>If not, this would be nice to have it on SO. The more exhaustive the better! So how many bytes are used by the following Python structures (depending on the <code>len</code> and the content type when relevant)?</p> ...
57
2009-08-25T22:56:35Z
1,331,541
<p>The recommendation from <a href="http://stackoverflow.com/questions/449560/how-do-i-determine-the-size-of-an-object-in-python">an earlier question</a> on this was to use <a href="http://docs.python.org/library/sys.html#sys.getsizeof" rel="nofollow">sys.getsizeof()</a>, quoting:</p> <pre><code>&gt;&gt;&gt; import sy...
82
2009-08-25T23:13:48Z
[ "python", "memory", "memory-footprint" ]
In-memory size of a Python structure
1,331,471
<p>Is there a reference for the memory size of Python data stucture on 32- and 64-bit platforms?</p> <p>If not, this would be nice to have it on SO. The more exhaustive the better! So how many bytes are used by the following Python structures (depending on the <code>len</code> and the content type when relevant)?</p> ...
57
2009-08-25T22:56:35Z
1,331,952
<p>I've been happily using <a href="http://code.google.com/p/pympler/">pympler</a> for such tasks. It's compatible with many versions of Python -- the <code>asizeof</code> module in particular goes back to 2.2!</p> <p>For example, using hughdbrown's example but with <code>from pympler import asizeof</code> at the star...
29
2009-08-26T01:45:10Z
[ "python", "memory", "memory-footprint" ]
In-memory size of a Python structure
1,331,471
<p>Is there a reference for the memory size of Python data stucture on 32- and 64-bit platforms?</p> <p>If not, this would be nice to have it on SO. The more exhaustive the better! So how many bytes are used by the following Python structures (depending on the <code>len</code> and the content type when relevant)?</p> ...
57
2009-08-25T22:56:35Z
12,667,362
<p>Try memory profiler. <a href="http://pypi.python.org/pypi/memory_profiler">memory profiler</a></p> <pre><code>Line # Mem usage Increment Line Contents ============================================== 3 @profile 4 5.97 MB 0.00 MB def my_func(): 5 13.61 MB ...
5
2012-10-01T03:56:49Z
[ "python", "memory", "memory-footprint" ]
In-memory size of a Python structure
1,331,471
<p>Is there a reference for the memory size of Python data stucture on 32- and 64-bit platforms?</p> <p>If not, this would be nice to have it on SO. The more exhaustive the better! So how many bytes are used by the following Python structures (depending on the <code>len</code> and the content type when relevant)?</p> ...
57
2009-08-25T22:56:35Z
20,783,532
<p>Also you can use <a href="https://pypi.python.org/pypi/guppy/">guppy</a> module.</p> <pre><code>&gt;&gt;&gt; from guppy import hpy; hp=hpy() &gt;&gt;&gt; hp.heap() Partition of a set of 25853 objects. Total size = 3320992 bytes. Index Count % Size % Cumulative % Kind (class / dict of class) 0 11731...
6
2013-12-26T10:49:57Z
[ "python", "memory", "memory-footprint" ]
Using Heapy's Memory Profile Browser with Twisted.web
1,331,561
<p>I am trying to profile twisted python code with <a href="http://stackoverflow.com/questions/110259/python-memory-profiler">Heapy</a>. For example (pseudo code):</p> <pre><code>from twisted.web import resource, server from twisted.internet import reactor from guppy import hpy class RootResource(resource.Resource):...
8
2009-08-25T23:22:03Z
1,472,383
<p>After looking over the guppy website and not finding any information about how to launch the profile browser there, I started looking around the guppy source and eventually found guppy/heapy/Prof.py, at the end of which I saw a docstring containing this line:</p> <pre><code>[0] heapy_Use.html#heapykinds.Use.pb </co...
6
2009-09-24T15:18:55Z
[ "python", "profiling", "heap", "twisted" ]
Extending Jython Syntax
1,331,784
<p>I would like to add syntax to Jython to enable a nicer API for users. For instance, matrix libraries like NumPy would benefit from having both matrix and elementwise operations like Matlab's <code>:*</code> vs. <code>*</code> infix operators.</p> <p>You can create a matrix in Octave using:</p> <pre><code>A = [ 1, ...
0
2009-08-26T00:41:09Z
1,429,218
<p>To the best of my knowledge there is not a macro / syntax expanding facility similar to EasyExtend, although <a href="http://fiber-space.de/wordpress/" rel="nofollow">the developer of EasyExtend</a> has been working on some jython projects recently (including some which are similar to EE). I suppose you could write...
1
2009-09-15T19:36:46Z
[ "python", "jython", "dsl" ]
Why does weakproxy not always preserve equivalence in python?
1,331,800
<p>MySQLDb uses weak proxy to prevent circular dependencies between cursors and connections.</p> <p>But you would expect from the documentation on weakref that you could still tests for equivalence. Yet:</p> <pre><code>In [36]: interactive.cursor.connection.thread_id() Out[36]: 4267758 In [37]: interactive.web_logic...
1
2009-08-26T00:47:17Z
1,331,823
<p>If the object is a standard weakref, you need to call it to get the object itself.</p> <pre><code>import weakref class Test(object): pass a = Test() b = weakref.ref(a) a is b() # True a == b() # True </code></pre> <p>Using weakrefs here seems wrong, though: if I construct a connection, create a cursor from it, and...
0
2009-08-26T00:56:05Z
[ "python", "weak-references" ]
Why does weakproxy not always preserve equivalence in python?
1,331,800
<p>MySQLDb uses weak proxy to prevent circular dependencies between cursors and connections.</p> <p>But you would expect from the documentation on weakref that you could still tests for equivalence. Yet:</p> <pre><code>In [36]: interactive.cursor.connection.thread_id() Out[36]: 4267758 In [37]: interactive.web_logic...
1
2009-08-26T00:47:17Z
1,331,996
<p>I've long found <code>weakref.proxy</code>'s design and implementation to be somewhat shaky. Witness...:</p> <pre><code>&gt;&gt;&gt; import weakref &gt;&gt;&gt; ob=set(range(23)) &gt;&gt;&gt; rob=weakref.proxy(ob) &gt;&gt;&gt; rob==ob False &gt;&gt;&gt; rob.__eq__(ob) True </code></pre> <p>...DEFINITELY peculiar! ...
3
2009-08-26T01:58:41Z
[ "python", "weak-references" ]
Why does weakproxy not always preserve equivalence in python?
1,331,800
<p>MySQLDb uses weak proxy to prevent circular dependencies between cursors and connections.</p> <p>But you would expect from the documentation on weakref that you could still tests for equivalence. Yet:</p> <pre><code>In [36]: interactive.cursor.connection.thread_id() Out[36]: 4267758 In [37]: interactive.web_logic...
1
2009-08-26T00:47:17Z
1,332,246
<p>Wrap the non proxy with weakref.proxy and use the identity operator:</p> <pre><code>&gt;&gt;&gt; interactive.cursor.connection is weakref.proxy(interactive.web_logic.conns.primary) True </code></pre> <p>Calling weakref.proxy() twice will return the same proxy object.</p>
1
2009-08-26T03:54:22Z
[ "python", "weak-references" ]
Regular Expression to match cross platform newline characters
1,331,815
<p>My program can accept data that has newline characters of \n, \r\n or \r (eg Unix, PC or Mac styles)</p> <p>What is the best way to construct a regular expression that will match whatever the encoding is?</p> <p>Alternatively, I could use universal_newline support on input, but now I'm interested to see what the r...
33
2009-08-26T00:54:20Z
1,331,840
<p>The regex I use when I want to be precise is <code>"\r\n?|\n"</code>.</p> <p>When I'm not concerned about consistency or empty lines, I use <code>"[\r\n]+"</code>, I imagine it makes my programs somewhere in the order of 0.2% faster.</p>
64
2009-08-26T01:02:00Z
[ "python", "regex", "cross-platform", "eol" ]
Regular Expression to match cross platform newline characters
1,331,815
<p>My program can accept data that has newline characters of \n, \r\n or \r (eg Unix, PC or Mac styles)</p> <p>What is the best way to construct a regular expression that will match whatever the encoding is?</p> <p>Alternatively, I could use universal_newline support on input, but now I'm interested to see what the r...
33
2009-08-26T00:54:20Z
39,022,365
<p>The pattern can be simplified to <code>\r?\n</code> for a little performance gain, if you don't have to deal with the old Mac style (OS 9 is unsupported since February 2002).</p>
0
2016-08-18T15:40:44Z
[ "python", "regex", "cross-platform", "eol" ]
import csv file into mysql database using django web application
1,332,077
<p>i try to upload a csv file into my web application and store it into mysql database but failed.Please can anyone help me?</p> <p>my user.py script:</p> <pre><code>def import_contact(request): if request.method == 'POST': form = UploadContactForm(request.POST, request.FILES) if form.is_valid(): csvfile = reques...
0
2009-08-26T02:31:38Z
1,332,903
<p>Since you haven't provided the code for the <code>getcsv</code> function, I'll have to use my crystal ball here a bit.</p> <p>One reason why the print in the <code>for row in testReader:</code> loop isn't working is that <code>getcsv</code> may already processes the file. Use <a href="http://docs.python.org/tutoria...
1
2009-08-26T07:20:24Z
[ "python", "django" ]
how to fetch reference property in django template
1,332,290
<p>I am using aep on google app engine</p> <pre><code>class Link(): bag = db.Referencepropery(Bag) #bag have name, id and other property name = db.Stringpropery object_query = Link.all(); p = paginator( object_query) object_list = p.page(1); prefetch_references( object_list.object_list, 'bag') render_to_...
1
2009-08-26T04:19:42Z
1,333,113
<p>There's several things wrong with your code:</p> <ul> <li>Your model classes need to extend db.Model</li> <li>Your properties need to call the constructor, not just reference it</li> <li>A StringProperty isn't a reference, so "prefetch_reference(objectlist, 'name')" doesn't make any sense.</li> </ul> <p>Here's one...
1
2009-08-26T08:14:26Z
[ "python", "google-app-engine" ]
how to fetch reference property in django template
1,332,290
<p>I am using aep on google app engine</p> <pre><code>class Link(): bag = db.Referencepropery(Bag) #bag have name, id and other property name = db.Stringpropery object_query = Link.all(); p = paginator( object_query) object_list = p.page(1); prefetch_references( object_list.object_list, 'bag') render_to_...
1
2009-08-26T04:19:42Z
13,339,316
<p>this problem is addressed in a later GAE django framework NON-REL so going to close this issue.</p>
0
2012-11-12T06:37:47Z
[ "python", "google-app-engine" ]
Using UTM with geodjango
1,332,376
<p>I'm looking into using the <a href="http://en.wikipedia.org/wiki/Universal%5FTransverse%5FMercator%5Fcoordinate%5Fsystem" rel="nofollow">UTM</a> coordinate system with geodjango. And I can't figure out how to get the data in properly.</p> <p>I've been browsing the documentation and it seems that the "<a href="http:...
1
2009-08-26T04:48:07Z
1,435,951
<p>The UTM zone (17N) is already specified by the spatial reference system -- <a href="http://spatialreference.org/ref/epsg/2029/" rel="nofollow">SRID 2029</a>, so you don't need to include it in the WKT you pass to the <code>GEOSGeometry</code> constructor.</p> <pre><code>&gt;&gt;&gt; from django.contrib.gis.geos im...
6
2009-09-16T23:01:01Z
[ "python", "django", "geodjango", "gdal", "geos" ]
Python multiprocessing for bulk file/conversion operation on Windows
1,332,583
<p>I have written a python script which watches a directory for new subdirectories, and then acts on each subdirectory in a loop. We have an external process which creates these subdirectories. Inside each subdirectory is a text file and a number of images. There is one record (line) in the text file for each image....
0
2009-08-26T05:53:19Z
1,332,629
<p>I agree that the design of this sounds like it could benefit from concurrency. Take a look at <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">the multiprocessing module</a>. You may also want to look at <a href="http://docs.python.org/library/threading.html" rel="nofollow">the threadin...
0
2009-08-26T06:09:45Z
[ "python", "windows", "multiprocessing" ]
How to determine whether java is installed on a system through python?
1,332,598
<p>i needed to run a jar file from python code,but before running that file i want to know whether java is installed on the system or not, using the python code itself.Please help thanks </p>
2
2009-08-26T05:59:12Z
1,332,697
<p>You could simply use the system commands.</p> <p>e.g.</p> <pre><code>&gt;&gt;&gt;import os &gt;&gt;&gt;os.system("java -version") java version "1.5.0_19" </code></pre> <p>You will get the output, ofcourse assuming Java is in the classpath. </p>
3
2009-08-26T06:28:36Z
[ "java", "python" ]
How to determine whether java is installed on a system through python?
1,332,598
<p>i needed to run a jar file from python code,but before running that file i want to know whether java is installed on the system or not, using the python code itself.Please help thanks </p>
2
2009-08-26T05:59:12Z
1,332,722
<p>There is no 100% reliable / portable way to do this, but the following procedure should give you some confidence that Java has been installed and configured properly (on a Linux):</p> <ol> <li>Check that the "JAVA_HOME" environment variable has been set and that it points to a directory containing a "bin" directory...
5
2009-08-26T06:35:48Z
[ "java", "python" ]
Overloading failUnlessEqual in unittest.TestCase
1,332,656
<p>I want to overload failUnlessEqual in unittest.TestCase so I created a new TestCase class:</p> <pre><code>import unittest class MyTestCase(unittest.TestCase): def failUnlessEqual(self, first, second, msg=None): if msg: msg += ' Expected: %r - Received %r' % (first, second) unittest....
0
2009-08-26T06:18:33Z
1,332,679
<p>You are calling assertEqual, but define failUnlessEqual. So why would you expect that your method is called - you are calling a different method, after all?</p> <p>Perhaps you have looked at the definition of TestCase, and seen the line</p> <pre><code>assertEqual = assertEquals = failUnlessEqual </code></pre> <p>...
2
2009-08-26T06:23:29Z
[ "python", "unit-testing", "overloading" ]
How can I access the iphone / ipod clipboard using python?
1,332,846
<p>I want to modify a python application written for the ipod/iphone. It should copy a string into the clipboard so that I can use it in another application. Is it possible to access the iphone clipboard using python? Thanks in advance.</p> <p>UPDATE:</p> <p>Thanks for replying.</p> <p>A bit of background: The pytho...
1
2009-08-26T07:08:28Z
1,333,976
<p>Sorry no, I'm assuming since you mention python that this is a web-based application? If so there is no way you can put something into/take something out of the user's clipboard automatically. However if it is webbased the user will be able to select any text/image and copy to paste elsewhere.</p>
0
2009-08-26T11:12:57Z
[ "iphone", "python", "clipboard", "ipod-touch" ]
How to remotely restart a service on a password protected machine using Python?
1,332,853
<p>I decided to tackle Python as a new language to learn. The first thing I want to do is code a script that will allow me to remotely restart services on other machines from my local machine. How would I accomplish this when the remote machine requires a username and password to log on? I don't need a full solution to...
0
2009-08-26T07:10:36Z
1,332,880
<p>People usually recommend <a href="http://www.lag.net/paramiko/" rel="nofollow">paramiko</a> as a library to do ssh (and I'm assuming that you need ssh to get into the remote machine). There is a good <a href="http://www.linuxplanet.com/linuxplanet/tutorials/6618/1/" rel="nofollow">tutorial</a> for it.</p> <p><stron...
3
2009-08-26T07:15:36Z
[ "python", "remote-access" ]
How to remotely restart a service on a password protected machine using Python?
1,332,853
<p>I decided to tackle Python as a new language to learn. The first thing I want to do is code a script that will allow me to remotely restart services on other machines from my local machine. How would I accomplish this when the remote machine requires a username and password to log on? I don't need a full solution to...
0
2009-08-26T07:10:36Z
1,332,891
<p>What kind of OS is your remote machine running? If it's linux, run <code>ssh(1)</code> using the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow"><code>subprocess</code> module</a>.</p> <p>If it's windows, then get the <a href="http://python.net/crew/mhammond/win32/Downloads.html" rel="nofoll...
1
2009-08-26T07:18:02Z
[ "python", "remote-access" ]
How to remotely restart a service on a password protected machine using Python?
1,332,853
<p>I decided to tackle Python as a new language to learn. The first thing I want to do is code a script that will allow me to remotely restart services on other machines from my local machine. How would I accomplish this when the remote machine requires a username and password to log on? I don't need a full solution to...
0
2009-08-26T07:10:36Z
1,332,896
<p>Take a look at <a href="http://www.nongnu.org/fab/" rel="nofollow">Fabric</a> wich is based on <a href="http://www.lag.net/paramiko/" rel="nofollow">paramiko</a>. This is really a good tool to automate remote tasks with python.</p> <p><a href="http://docs.fabfile.org/0.9/" rel="nofollow">Fabric Documentation</a> wi...
3
2009-08-26T07:19:02Z
[ "python", "remote-access" ]
How to remotely restart a service on a password protected machine using Python?
1,332,853
<p>I decided to tackle Python as a new language to learn. The first thing I want to do is code a script that will allow me to remotely restart services on other machines from my local machine. How would I accomplish this when the remote machine requires a username and password to log on? I don't need a full solution to...
0
2009-08-26T07:10:36Z
1,332,904
<p>Which OS for the target machines? If 'service' is 'Windows NT service', and your local machine is also Windows, I'd use IronPython as the Python language implementation and call straight into the WMI facilities in the .net System.Management namespace -- they're meant for remote admin like that.</p>
1
2009-08-26T07:20:26Z
[ "python", "remote-access" ]
How to remotely restart a service on a password protected machine using Python?
1,332,853
<p>I decided to tackle Python as a new language to learn. The first thing I want to do is code a script that will allow me to remotely restart services on other machines from my local machine. How would I accomplish this when the remote machine requires a username and password to log on? I don't need a full solution to...
0
2009-08-26T07:10:36Z
15,391,577
<p>On Windows, the <a href="https://pypi.python.org/pypi/WMI/" rel="nofollow">wmi module</a> is now fantastic for this.</p>
0
2013-03-13T16:53:26Z
[ "python", "remote-access" ]
Renaming a HTML file with Python
1,332,876
<p>A bit of background: When I save a web page from e.g. IE8 as "webpage, complete", the images and such that the page contains are placed in a subfolder with the postfix "_files". This convention allows Windows to synchronize the .htm file and the accompanying folder.</p> <p>Now, in order to keep the synchronization ...
0
2009-08-26T07:15:24Z
1,332,899
<p>If you rename the folder, I'm not sure how you can get around parsing the <code>.htm</code> file and replacing instances of <code>_files</code> with the new suffix. Perhaps you can use a folder alias (shortcut?) but then that's not a very clean solution.</p>
0
2009-08-26T07:19:32Z
[ "python", "html" ]
Renaming a HTML file with Python
1,332,876
<p>A bit of background: When I save a web page from e.g. IE8 as "webpage, complete", the images and such that the page contains are placed in a subfolder with the postfix "_files". This convention allows Windows to synchronize the .htm file and the accompanying folder.</p> <p>Now, in order to keep the synchronization ...
0
2009-08-26T07:15:24Z
1,332,910
<p>There is just one easy way: Have IE save the file again under the new name. But if you want to do it later, you must parse the HTML. In this case, <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> is your friend.</p>
1
2009-08-26T07:21:47Z
[ "python", "html" ]
Renaming a HTML file with Python
1,332,876
<p>A bit of background: When I save a web page from e.g. IE8 as "webpage, complete", the images and such that the page contains are placed in a subfolder with the postfix "_files". This convention allows Windows to synchronize the .htm file and the accompanying folder.</p> <p>Now, in order to keep the synchronization ...
0
2009-08-26T07:15:24Z
1,333,124
<p>you can use simple string replace on your HTML file without parsing it, it can of course be troublesome if the text being replaced is mentioned in the HTML itself.. </p> <pre><code>os.rename("test.html", "test2.html") os.rename("test_files", "test2_files") with open("test2.html", "r") as f: s = f.read().repla...
0
2009-08-26T08:17:18Z
[ "python", "html" ]
Tick Python instances from Python
1,333,016
<p>I am interrested in doing a programming game using python, and I would like to do it in the style of GunTactyx (<a href="http://apocalyx.sourceforge.net/guntactyx/index.php" rel="nofollow">http://apocalyx.sourceforge.net/guntactyx/index.php</a>). Only much simpler, as I am primarily interrested in the parallel execu...
1
2009-08-26T07:49:29Z
1,333,856
<p>Maybe you should look into fork of python: <a href="http://stackless.com" rel="nofollow">stackless</a>, it allows concurrently running thousands of micro-threads without much performance penalty - every "thread" (these aren't real OS threads) could be one Unit.</p> <p>Also it's very easy to implement Actor model wi...
1
2009-08-26T10:53:35Z
[ "python" ]
Problem passing bash output to a python script
1,333,107
<p>I'm fairly new to programming and I searched the internet for a way to pass bash output to a Python script.</p> <p>I came up with this in bash.</p> <pre><code>XAS_SVN=`svn info` ssh hudson@test "python/runtests.py $XAS_SVN" </code></pre> <p>And this in python.</p> <pre><code>import sys print sys.argv[1] </code>...
1
2009-08-26T08:12:17Z
1,333,116
<p>Yes, you need to put in quotes your input to python/runtest.py because otherwise argv[1] gets it only up to the first space. Like this:</p> <pre><code>ssh hudson@test "python/runtest.py \"$XAS_SVN\"" </code></pre>
0
2009-08-26T08:14:54Z
[ "python", "bash" ]
Problem passing bash output to a python script
1,333,107
<p>I'm fairly new to programming and I searched the internet for a way to pass bash output to a Python script.</p> <p>I came up with this in bash.</p> <pre><code>XAS_SVN=`svn info` ssh hudson@test "python/runtests.py $XAS_SVN" </code></pre> <p>And this in python.</p> <pre><code>import sys print sys.argv[1] </code>...
1
2009-08-26T08:12:17Z
1,333,126
<p>Since you have spaces in the variable, you need to escape them or read all the arguments in your script:</p> <pre><code>print ' '.join(sys.argv[1:]) </code></pre> <p>But it might be better to use stdin/stdout to communicate, especially if there can be some characters susceptible to be interpreted by the shell in t...
1
2009-08-26T08:17:57Z
[ "python", "bash" ]
django template system, calling a function inside a model
1,333,189
<p>I want to call a function from my model at a template such as:</p> <pre><code>class ChannelStatus(models.Model): .............................. .............................. def get_related_deltas(self,epk): mystring = "" if not self.get_error_code_delta(epk): return mystring else: ...
46
2009-08-26T08:31:03Z
1,333,277
<p>You can't call a function with parameters from the template. You can only do this in the view. Alternatively you could write a <a href="https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/#writing-custom-template-filters">custom template filter</a>, which might look like this:</p> <pre><code>@register....
64
2009-08-26T08:51:17Z
[ "python", "django", "django-models", "django-templates" ]
django template system, calling a function inside a model
1,333,189
<p>I want to call a function from my model at a template such as:</p> <pre><code>class ChannelStatus(models.Model): .............................. .............................. def get_related_deltas(self,epk): mystring = "" if not self.get_error_code_delta(epk): return mystring else: ...
46
2009-08-26T08:31:03Z
3,598,020
<p>Another option is to define a property. See <a href="http://adam.gomaa.us/blog/2008/aug/11/the-python-property-builtin/" rel="nofollow">http://adam.gomaa.us/blog/2008/aug/11/the-python-property-builtin/</a> .</p> <p>You write your function that can do pretty much anything you want. You make it a read only property....
2
2010-08-30T05:13:40Z
[ "python", "django", "django-models", "django-templates" ]
django template system, calling a function inside a model
1,333,189
<p>I want to call a function from my model at a template such as:</p> <pre><code>class ChannelStatus(models.Model): .............................. .............................. def get_related_deltas(self,epk): mystring = "" if not self.get_error_code_delta(epk): return mystring else: ...
46
2009-08-26T08:31:03Z
4,560,708
<p>If you find that there are too many properties running around everywhere or you have a template filter for every other method that you write, another solution was suggested on IRC thanks @FunkyBob. It's a little well, erm, funky but it is nice in certain cases.</p> <pre><code> class MethodProxy(object): "...
2
2010-12-30T07:05:11Z
[ "python", "django", "django-models", "django-templates" ]
django template system, calling a function inside a model
1,333,189
<p>I want to call a function from my model at a template such as:</p> <pre><code>class ChannelStatus(models.Model): .............................. .............................. def get_related_deltas(self,epk): mystring = "" if not self.get_error_code_delta(epk): return mystring else: ...
46
2009-08-26T08:31:03Z
13,367,016
<p>If the method doesn't require any arguments, you can use the @property decorator and access it normally in the template.</p> <pre><code>class ChannelStatus(models.Model): ... @property def function_you_want_as_property(self): mystring = "" ... </code></pre>
20
2012-11-13T19:03:03Z
[ "python", "django", "django-models", "django-templates" ]
django template system, calling a function inside a model
1,333,189
<p>I want to call a function from my model at a template such as:</p> <pre><code>class ChannelStatus(models.Model): .............................. .............................. def get_related_deltas(self,epk): mystring = "" if not self.get_error_code_delta(epk): return mystring else: ...
46
2009-08-26T08:31:03Z
31,197,650
<p>For > 1 argument, use <a href="https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#simple-tags" rel="nofollow">simple tags</a>:</p> <pre><code>@register.simple_tag def related_deltas(obj, epk, second_arg): return obj.get_related_deltas(epk, second_arg) </code></pre> <p>Template:</p> <pre><code>{...
1
2015-07-03T02:45:00Z
[ "python", "django", "django-models", "django-templates" ]
Python: Set with only existence check?
1,333,381
<p>I have a set of lots of big long strings that I want to do existence lookups for. I don't need the whole string ever to be saved. As far as I can tell, the <code>set()</code> actually stored the string which is eating up a lot of my memory.</p> <p>Does such a data structure exist?</p> <pre><code>done = hash_only_s...
8
2009-08-26T09:13:44Z
1,333,414
<p>It's certainly possible to keep a set of only hashes:</p> <pre><code>done = set() while len(queue) &gt; 0 : item = queue.pop() h = hash(item) if h not in done : process(item) done.add(h) </code></pre> <p>Notice that because of hash collisions, there is a chance that you consider an item done e...
10
2009-08-26T09:18:59Z
[ "python", "data-structures", "hash", "set" ]
Python: Set with only existence check?
1,333,381
<p>I have a set of lots of big long strings that I want to do existence lookups for. I don't need the whole string ever to be saved. As far as I can tell, the <code>set()</code> actually stored the string which is eating up a lot of my memory.</p> <p>Does such a data structure exist?</p> <pre><code>done = hash_only_s...
8
2009-08-26T09:13:44Z
1,333,444
<p>You can use a data structure called <a href="http://en.wikipedia.org/wiki/Bloom%5Ffilter" rel="nofollow"><strong>Bloom Filter</strong></a> specifically for this purpose. A Python implementation can be found <a href="http://github.com/jaybaird/python-bloomfilter/tree/master" rel="nofollow">here</a>.</p> <p><strong>E...
4
2009-08-26T09:27:23Z
[ "python", "data-structures", "hash", "set" ]
Python: Set with only existence check?
1,333,381
<p>I have a set of lots of big long strings that I want to do existence lookups for. I don't need the whole string ever to be saved. As far as I can tell, the <code>set()</code> actually stored the string which is eating up a lot of my memory.</p> <p>Does such a data structure exist?</p> <pre><code>done = hash_only_s...
8
2009-08-26T09:13:44Z
1,333,462
<p>You would have to think about how to do the lookup, since there are two methods that the set needs, <code>__hash__</code> and <code>__eq__</code>.</p> <p>The hash is a "loose part" that you can take away, but the <code>__eq__</code> is not a loose part that you can save; you have to have two strings for the compari...
2
2009-08-26T09:29:19Z
[ "python", "data-structures", "hash", "set" ]
Python: Set with only existence check?
1,333,381
<p>I have a set of lots of big long strings that I want to do existence lookups for. I don't need the whole string ever to be saved. As far as I can tell, the <code>set()</code> actually stored the string which is eating up a lot of my memory.</p> <p>Does such a data structure exist?</p> <pre><code>done = hash_only_s...
8
2009-08-26T09:13:44Z
1,333,634
<p>You need to know the whole string to have 100% certainty. If you have lots of strings with similar prefixes you could save space by using a trie to store the strings. If your strings are long you could also save space by using a large hash function like SHA-1 to make the possibility of hash collisions so remote as t...
3
2009-08-26T10:06:02Z
[ "python", "data-structures", "hash", "set" ]
Python: Set with only existence check?
1,333,381
<p>I have a set of lots of big long strings that I want to do existence lookups for. I don't need the whole string ever to be saved. As far as I can tell, the <code>set()</code> actually stored the string which is eating up a lot of my memory.</p> <p>Does such a data structure exist?</p> <pre><code>done = hash_only_s...
8
2009-08-26T09:13:44Z
1,333,714
<p>If you use a secure (like SHA-256, found in the <code>hashlib</code> module) hash function to hash the strings, it's very unlikely that you would found duplicate (and if you find some you can probably win a prize as with most cryptographic hash functions).</p> <p>The builtin <code>__hash__()</code> method does not ...
4
2009-08-26T10:21:06Z
[ "python", "data-structures", "hash", "set" ]
Python: Set with only existence check?
1,333,381
<p>I have a set of lots of big long strings that I want to do existence lookups for. I don't need the whole string ever to be saved. As far as I can tell, the <code>set()</code> actually stored the string which is eating up a lot of my memory.</p> <p>Does such a data structure exist?</p> <pre><code>done = hash_only_s...
8
2009-08-26T09:13:44Z
1,337,515
<p>As has been hinted already, if the answers offered here (most of which break down in the face of hash collisions) are not acceptable you would need to use a lossless representation of the strings. </p> <p>Python's zlib module provides built-in string compression capabilities and could be used to pre-process the st...
0
2009-08-26T21:10:44Z
[ "python", "data-structures", "hash", "set" ]
Delete None values from Python dict
1,334,020
<p>Newbie to Python, so this may seem silly.</p> <p>I have two dicts:</p> <pre><code>default = {'a': 'alpha', 'b': 'beta', 'g': 'Gamma'} user = {'a': 'NewAlpha', 'b': None} </code></pre> <p>I need to update my defaults with the values that exist in user. But only for those that have a value not equal to None. So I...
8
2009-08-26T11:21:15Z
1,334,038
<pre><code>result = default.copy() result.update((k, v) for k, v in user.iteritems() if v is not None) </code></pre>
18
2009-08-26T11:24:05Z
[ "python" ]
Delete None values from Python dict
1,334,020
<p>Newbie to Python, so this may seem silly.</p> <p>I have two dicts:</p> <pre><code>default = {'a': 'alpha', 'b': 'beta', 'g': 'Gamma'} user = {'a': 'NewAlpha', 'b': None} </code></pre> <p>I need to update my defaults with the values that exist in user. But only for those that have a value not equal to None. So I...
8
2009-08-26T11:21:15Z
1,334,040
<p>With the <code>update()</code> method, and some generator expression:</p> <pre><code>D.update((k, v) for k, v in user.iteritems() if v is not None) </code></pre>
7
2009-08-26T11:24:06Z
[ "python" ]
Python design patterns, cross importing
1,334,134
<p>I am using Python for automating a complex procedure that has few options. I want to have the following structure in python. - One "flow-class" containing the flow - One helper class that contains a lot of "black boxes" (functions that do not often get changed).</p> <p>99% of the time, I modify things in the flow-c...
1
2009-08-26T11:43:00Z
1,334,202
<blockquote> <p>I know that I could obviously solve this by always passing <code>isLinux</code> as a parameter to getUserInput, but this complicates the usage of this method and makes it less intuitive.</p> </blockquote> <p>Actually using global variables complicates the usage of this program waaaay more than a simp...
1
2009-08-26T11:58:52Z
[ "design", "design-patterns", "python-3.x", "python" ]
Python design patterns, cross importing
1,334,134
<p>I am using Python for automating a complex procedure that has few options. I want to have the following structure in python. - One "flow-class" containing the flow - One helper class that contains a lot of "black boxes" (functions that do not often get changed).</p> <p>99% of the time, I modify things in the flow-c...
1
2009-08-26T11:43:00Z
1,334,209
<p>I would like to question you on your last sentence. </p> <p>Usually, as also outlined in <a href="http://cc2e.com/" rel="nofollow">CC2</a>, usage of global variables helps in writing code, but not in reading it.<br /> And code is read many more times than it is written; in your case, I understand you modify the sa...
1
2009-08-26T11:59:27Z
[ "design", "design-patterns", "python-3.x", "python" ]
Python design patterns, cross importing
1,334,134
<p>I am using Python for automating a complex procedure that has few options. I want to have the following structure in python. - One "flow-class" containing the flow - One helper class that contains a lot of "black boxes" (functions that do not often get changed).</p> <p>99% of the time, I modify things in the flow-c...
1
2009-08-26T11:43:00Z
1,334,210
<p>you need to do <code>helper_class.getUserIinput()</code> in your <code>flow_class</code>. It's not about cross-importing. Once it's fixed you'll get <code>AttributeError</code> that is indeed related to cross-importing.</p> <p>At this stage you'll need to implement logic of getting <code>getUserInput</code> defined...
2
2009-08-26T11:59:30Z
[ "design", "design-patterns", "python-3.x", "python" ]
How can I mass-assign SA ORM object attributes?
1,334,171
<p>I have an ORM mapped object, that I want to update. I have all attributes validated and secured in a dictionary (keyword arguments). Now I would like to update all object attributes as in the dictionary.</p> <pre><code>for k,v in kw.items(): setattr(myobject, k, v) </code></pre> <p>doesnt work (AttributeError ...
1
2009-08-26T11:50:32Z
1,334,180
<pre><code>myobject.__dict__.update(**kw) </code></pre>
2
2009-08-26T11:53:08Z
[ "python", "sqlalchemy" ]
How can I mass-assign SA ORM object attributes?
1,334,171
<p>I have an ORM mapped object, that I want to update. I have all attributes validated and secured in a dictionary (keyword arguments). Now I would like to update all object attributes as in the dictionary.</p> <pre><code>for k,v in kw.items(): setattr(myobject, k, v) </code></pre> <p>doesnt work (AttributeError ...
1
2009-08-26T11:50:32Z
1,335,148
<p>You are trying to assign a unicode string to a relation attribute. Say you have:</p> <pre><code> class ClassA(Base): ... b_id = Column(None, ForeignKey('b.id')) b = relation(ClassB) </code></pre> <p>And you are trying to do:</p> <pre><code> my_object = ClassA() my_object.b = "foo" </code></pre> <...
3
2009-08-26T14:34:42Z
[ "python", "sqlalchemy" ]
How to change baseclass
1,334,222
<p>I have a class which is derived from a base class, and have many many lines of code</p> <p>e.g.</p> <pre><code>class AutoComplete(TextCtrl): ..... </code></pre> <p>What I want to do is change the baseclass so that it works like</p> <pre><code>class AutoComplete(PriceCtrl): ..... </code></pre> <p>I have ...
3
2009-08-26T12:02:05Z
1,334,242
<p>You could have a factory for your classes:</p> <pre><code>def completefactory(baseclass): class AutoComplete(baseclass): pass return AutoComplete </code></pre> <p>And then use:</p> <pre><code>TextAutoComplete = completefactory(TextCtrl) PriceAutoComplete = completefactory(PriceCtrl) </code></pre> ...
7
2009-08-26T12:06:52Z
[ "python", "dynamic", "class" ]
How to change baseclass
1,334,222
<p>I have a class which is derived from a base class, and have many many lines of code</p> <p>e.g.</p> <pre><code>class AutoComplete(TextCtrl): ..... </code></pre> <p>What I want to do is change the baseclass so that it works like</p> <pre><code>class AutoComplete(PriceCtrl): ..... </code></pre> <p>I have ...
3
2009-08-26T12:02:05Z
1,334,256
<p>You could modify the <code>__bases__</code> tuple. For example you could add another baseclass:</p> <pre><code>AutoComplete.__bases__ += (PriceCtrl,) </code></pre> <p>But in general I would try to avoid such hacks, it quickly creates a terrible mess.</p>
1
2009-08-26T12:11:51Z
[ "python", "dynamic", "class" ]
How to change baseclass
1,334,222
<p>I have a class which is derived from a base class, and have many many lines of code</p> <p>e.g.</p> <pre><code>class AutoComplete(TextCtrl): ..... </code></pre> <p>What I want to do is change the baseclass so that it works like</p> <pre><code>class AutoComplete(PriceCtrl): ..... </code></pre> <p>I have ...
3
2009-08-26T12:02:05Z
1,335,308
<p>You could use multiple inheritance for this:</p> <pre><code>class AutoCompleteBase(object): # code for your class # remember to call base implementation with super: # super(AutoCompleteBase, self).some_method() class TextAutoComplete(AutoCompleteBase, TextCtrl): pass class PriceAutoComplete(Auto...
2
2009-08-26T14:53:41Z
[ "python", "dynamic", "class" ]
Aggregate photos from various services into one Stream
1,334,477
<p>Helllo All, I'm looking to aggregate photos from various streams into one stream in a similar manner as to friend feed. I'd like to be able to watch flickr and picasa and other sites with RSS feeds of my choosing and then create a timeline of top photos.</p> <p>For example, assume that X's below are photos:</p>...
0
2009-08-26T12:54:28Z
1,334,689
<p>Have you checked out <a href="http://gregarius.net/" rel="nofollow">Gregarius</a>. It's a PHP tool which you install on your own server which allows you to combine/group RSS feeds.</p> <p>A group of RSS feeds has its own RSS feed in gregarius. You don't need to look at the frontend you can just use gregarius as bac...
1
2009-08-26T13:28:09Z
[ "php", "javascript", "python" ]
Aggregate photos from various services into one Stream
1,334,477
<p>Helllo All, I'm looking to aggregate photos from various streams into one stream in a similar manner as to friend feed. I'd like to be able to watch flickr and picasa and other sites with RSS feeds of my choosing and then create a timeline of top photos.</p> <p>For example, assume that X's below are photos:</p>...
0
2009-08-26T12:54:28Z
1,334,874
<p>I suggest using <a href="http://developer.yahoo.com/yql/" rel="nofollow">YQL</a>.</p> <blockquote> <p>The Yahoo! Query Language is an expressive SQL-like language that lets you query, filter, and join data across Web services.</p> </blockquote> <p>With it you can do things like the following:</p> <pre><code>sel...
2
2009-08-26T13:57:08Z
[ "php", "javascript", "python" ]
AttributeError: 'NoneType' object has no attribute 'GetDataStore'
1,334,607
<p>I guys, I developing a utility in python and i have 2 object the main class and an database helper for get sqlserver data.</p> <p>database.py</p> <pre><code>import _mssql class sqlserver(object): global _host, _userid, _pwd, _db def __new__ (self, host, userid, pwd, database): _host = host ...
3
2009-08-26T13:16:01Z
1,334,680
<p>Have a look at what <a href="http://docs.python.org/reference/datamodel.html?highlight=%5F%5Fnew%5F%5F#object.%5F%5Fnew%5F%5F" rel="nofollow"><code>__new__</code></a> is supposed to be doing and what arguments it's supposed to have. I think you wanted to use <code>__init__</code> and not <code>__new__</code>. The re...
1
2009-08-26T13:26:24Z
[ "python" ]
AttributeError: 'NoneType' object has no attribute 'GetDataStore'
1,334,607
<p>I guys, I developing a utility in python and i have 2 object the main class and an database helper for get sqlserver data.</p> <p>database.py</p> <pre><code>import _mssql class sqlserver(object): global _host, _userid, _pwd, _db def __new__ (self, host, userid, pwd, database): _host = host ...
3
2009-08-26T13:16:01Z
1,334,705
<p>First of all:</p> <ul> <li>The <code>__new__</code> method should be named <code>__init__</code>.</li> <li>Remove the global _host etc. line</li> </ul> <p>Then change the <code>__init__</code> method:</p> <pre><code>self.host = host self.userid = userid etc. </code></pre> <p>And change <code>GetDataStore</code>:...
3
2009-08-26T13:30:24Z
[ "python" ]
AttributeError: 'NoneType' object has no attribute 'GetDataStore'
1,334,607
<p>I guys, I developing a utility in python and i have 2 object the main class and an database helper for get sqlserver data.</p> <p>database.py</p> <pre><code>import _mssql class sqlserver(object): global _host, _userid, _pwd, _db def __new__ (self, host, userid, pwd, database): _host = host ...
3
2009-08-26T13:16:01Z
1,334,707
<p>I think you want to change databases.py like this:</p> <pre><code>import _mssql class sqlserver(object): def __init__ (self, host, userid, pwd, database): self.host = host self.userid = userid self.pwd = pwd self.db = database def GetDataStore(self, sql): conn = _m...
0
2009-08-26T13:31:37Z
[ "python" ]
percentage difference between two text files
1,334,725
<p>I know that I can use cmp, diff, etc to compare two files, but what I am looking for is a utility that gives me percentage difference between two files. </p> <p>if there is no such utility, any algorithm would do fine too. I have read about fuzzy programming, but I have not quite understand it.</p>
4
2009-08-26T13:34:15Z
1,334,758
<p>You can use difflib.SequenceMatcher <a href="http://docs.python.org/library/difflib.html#difflib.SequenceMatcher.ratio">ratio</a> method</p> <p>From the documentation:</p> <blockquote> <p>Return a measure of the sequences’ similarity as a float in the range [0, 1].</p> </blockquote> <p>For example:</p> <...
19
2009-08-26T13:39:05Z
[ "python", "linux", "algorithm", "language-agnostic" ]
percentage difference between two text files
1,334,725
<p>I know that I can use cmp, diff, etc to compare two files, but what I am looking for is a utility that gives me percentage difference between two files. </p> <p>if there is no such utility, any algorithm would do fine too. I have read about fuzzy programming, but I have not quite understand it.</p>
4
2009-08-26T13:34:15Z
1,334,820
<p>It looks like Linux has a utility called dwdiff that can give percentage differences by using the "-s" flag</p> <p><a href="http://www.softpanorama.org/Utilities/diff%5Ftools.shtml" rel="nofollow">http://www.softpanorama.org/Utilities/diff_tools.shtml</a> </p>
2
2009-08-26T13:48:42Z
[ "python", "linux", "algorithm", "language-agnostic" ]
percentage difference between two text files
1,334,725
<p>I know that I can use cmp, diff, etc to compare two files, but what I am looking for is a utility that gives me percentage difference between two files. </p> <p>if there is no such utility, any algorithm would do fine too. I have read about fuzzy programming, but I have not quite understand it.</p>
4
2009-08-26T13:34:15Z
33,805,621
<p><a href="http://www.scootersoftware.com/index.php" rel="nofollow">Beyond Compare</a> has very nice file difference statistics export to csv. Differences at line level are reported so it's nice to compare source code files.</p>
0
2015-11-19T13:42:51Z
[ "python", "linux", "algorithm", "language-agnostic" ]
Iteration over list slices
1,335,392
<p>Good day function-wizards,</p> <p>I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.</p> <p>In my mind it is something like:</p> <pre><code>for list_of_x_items in fatherList: foo(list_of_x_items) </code></pre> <p>Is there a way to properly define <code>li...
19
2009-08-26T15:04:04Z
1,335,456
<p>Do you mean something like:</p> <pre><code>def callonslices(size, fatherList, foo): for i in xrange(0, len(fatherList), size): foo(fatherList[i:i+size]) </code></pre> <p>If this is roughly the functionality you want you might, if you desire, dress it up a bit in a generator:</p> <pre><code>def sliceup(size,...
7
2009-08-26T15:12:18Z
[ "python", "loops", "iteration", "slice" ]
Iteration over list slices
1,335,392
<p>Good day function-wizards,</p> <p>I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.</p> <p>In my mind it is something like:</p> <pre><code>for list_of_x_items in fatherList: foo(list_of_x_items) </code></pre> <p>Is there a way to properly define <code>li...
19
2009-08-26T15:04:04Z
1,335,464
<p>Your question could use some more detail, but how about:</p> <pre><code>def iterate_over_slices(the_list, slice_size): for start in range(0, len(the_list)-slice_size): slice = the_list[start:start+slice_size] foo(slice) </code></pre>
2
2009-08-26T15:13:03Z
[ "python", "loops", "iteration", "slice" ]
Iteration over list slices
1,335,392
<p>Good day function-wizards,</p> <p>I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.</p> <p>In my mind it is something like:</p> <pre><code>for list_of_x_items in fatherList: foo(list_of_x_items) </code></pre> <p>Is there a way to properly define <code>li...
19
2009-08-26T15:04:04Z
1,335,466
<p>Use a generator:</p> <pre><code>big_list = [1,2,3,4,5,6,7,8,9] slice_length = 3 def sliceIterator(lst, sliceLen): for i in range(len(lst) - sliceLen + 1): yield lst[i:i + sliceLen] for slice in sliceIterator(big_list, slice_length): foo(slice) </code></pre> <p><code>sliceIterator</code> implements...
5
2009-08-26T15:13:22Z
[ "python", "loops", "iteration", "slice" ]
Iteration over list slices
1,335,392
<p>Good day function-wizards,</p> <p>I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.</p> <p>In my mind it is something like:</p> <pre><code>for list_of_x_items in fatherList: foo(list_of_x_items) </code></pre> <p>Is there a way to properly define <code>li...
19
2009-08-26T15:04:04Z
1,335,572
<p>If you want to be able to consume any iterable you can use these functions:</p> <pre><code>from itertools import chain, islice def ichunked(seq, chunksize): """Yields items from an iterator in iterable chunks.""" it = iter(seq) while True: yield chain([it.next()], islice(it, chunksize-1)) def ...
18
2009-08-26T15:28:54Z
[ "python", "loops", "iteration", "slice" ]
Iteration over list slices
1,335,392
<p>Good day function-wizards,</p> <p>I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.</p> <p>In my mind it is something like:</p> <pre><code>for list_of_x_items in fatherList: foo(list_of_x_items) </code></pre> <p>Is there a way to properly define <code>li...
19
2009-08-26T15:04:04Z
1,335,618
<p>If you want to divide a list into slices you can use this trick:</p> <pre><code>list_of_slices = zip(*(iter(the_list),) * slice_size) </code></pre> <p>For example</p> <pre><code>&gt;&gt;&gt; zip(*(iter(range(10)),) * 3) [(0, 1, 2), (3, 4, 5), (6, 7, 8)] </code></pre> <p>If the number of items is not dividable by...
48
2009-08-26T15:35:05Z
[ "python", "loops", "iteration", "slice" ]
Iteration over list slices
1,335,392
<p>Good day function-wizards,</p> <p>I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.</p> <p>In my mind it is something like:</p> <pre><code>for list_of_x_items in fatherList: foo(list_of_x_items) </code></pre> <p>Is there a way to properly define <code>li...
19
2009-08-26T15:04:04Z
1,336,821
<h3>Answer to the last part of the question:</h3> <blockquote> <p>question update: How to modify the function you have provided to store the extra items and use them when the next fatherList is fed to the function?</p> </blockquote> <p>If you need to store state then you can use an object for that.</p> <pr...
7
2009-08-26T19:02:07Z
[ "python", "loops", "iteration", "slice" ]
Iteration over list slices
1,335,392
<p>Good day function-wizards,</p> <p>I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.</p> <p>In my mind it is something like:</p> <pre><code>for list_of_x_items in fatherList: foo(list_of_x_items) </code></pre> <p>Is there a way to properly define <code>li...
19
2009-08-26T15:04:04Z
1,339,575
<p>I am not sure, but it seems you want to do what is called a moving average. numpy provides facilities for this (the convolve function).</p> <pre> >>> x = numpy.array(range(20)) >>> x array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) >>> n = 2 # moving average win...
5
2009-08-27T08:11:39Z
[ "python", "loops", "iteration", "slice" ]
Iteration over list slices
1,335,392
<p>Good day function-wizards,</p> <p>I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.</p> <p>In my mind it is something like:</p> <pre><code>for list_of_x_items in fatherList: foo(list_of_x_items) </code></pre> <p>Is there a way to properly define <code>li...
19
2009-08-26T15:04:04Z
23,790,716
<p>For a near-one liner (after <code>itertools</code> import) in the vein of Nadia's answer dealing with non-chunk divisible sizes without padding:</p> <pre><code>&gt;&gt;&gt; import itertools as itt &gt;&gt;&gt; chunksize = 5 &gt;&gt;&gt; myseq = range(18) &gt;&gt;&gt; cnt = itt.count() &gt;&gt;&gt; print [ tuple(grp...
1
2014-05-21T18:07:21Z
[ "python", "loops", "iteration", "slice" ]
Python urllib.urlopen() call doesn't work with a URL that a browser accepts
1,335,439
<p>If I point Firefox at <a href="http://bitbucket.org/tortoisehg/stable/wiki/Home/ReleaseNotes" rel="nofollow">http://bitbucket.org/tortoisehg/stable/wiki/Home/ReleaseNotes</a>, I get a page of HTML. But if I try this in Python:</p> <pre> import urllib site = 'http://bitbucket.org/tortoisehg/stable/wiki/Home/Release...
1
2009-08-26T15:09:57Z
1,335,487
<p>I don't think you're doing anything wrong -- it looks like this server was just down? Your script worked fine for me ('text' contained the same data as that displayed in the browser).</p>
-2
2009-08-26T15:16:03Z
[ "python", "urllib", "bitbucket" ]
Python urllib.urlopen() call doesn't work with a URL that a browser accepts
1,335,439
<p>If I point Firefox at <a href="http://bitbucket.org/tortoisehg/stable/wiki/Home/ReleaseNotes" rel="nofollow">http://bitbucket.org/tortoisehg/stable/wiki/Home/ReleaseNotes</a>, I get a page of HTML. But if I try this in Python:</p> <pre> import urllib site = 'http://bitbucket.org/tortoisehg/stable/wiki/Home/Release...
1
2009-08-26T15:09:57Z
1,335,505
<p>You're doing nothing wrong, on the surface, and as the error page says you should contact the site's administrators because they're the ones with the server logs which may explain what's happening. Fortunately, bitbucket's site admins are a friendly bunch!</p> <p>No doubt there is some header or combination of head...
3
2009-08-26T15:18:37Z
[ "python", "urllib", "bitbucket" ]
Python urllib.urlopen() call doesn't work with a URL that a browser accepts
1,335,439
<p>If I point Firefox at <a href="http://bitbucket.org/tortoisehg/stable/wiki/Home/ReleaseNotes" rel="nofollow">http://bitbucket.org/tortoisehg/stable/wiki/Home/ReleaseNotes</a>, I get a page of HTML. But if I try this in Python:</p> <pre> import urllib site = 'http://bitbucket.org/tortoisehg/stable/wiki/Home/Release...
1
2009-08-26T15:09:57Z
1,335,634
<p>You are not doing anything wrong, bitbucket does some user agent detection (to detect mercurial clients for example). Just <a href="http://docs.python.org/library/urllib.html#urllib.%5Furlopener" rel="nofollow">changing the user agent</a> fixes it (if it doesn't have <code>urllib</code> as a substring).</p> <p>You ...
3
2009-08-26T15:36:53Z
[ "python", "urllib", "bitbucket" ]
Keyboard input with timeout in Python
1,335,507
<p>How would you prompt the user for some input but timing out after N seconds?</p> <p>Google is pointing to a mail thread about it at <a href="http://mail.python.org/pipermail/python-list/2006-January/533215.html">http://mail.python.org/pipermail/python-list/2006-January/533215.html</a> but it seems not to work. The ...
16
2009-08-26T15:19:05Z
1,336,751
<p>The example you have linked to is wrong and the exception is actually occuring when calling alarm handler instead of when read blocks. Better try this:</p> <pre><code>import signal TIMEOUT = 5 # number of seconds your want for timeout def interrupted(signum, frame): "called when read times out" print 'inte...
10
2009-08-26T18:50:00Z
[ "python", "timeout", "keyboard-input" ]
Keyboard input with timeout in Python
1,335,507
<p>How would you prompt the user for some input but timing out after N seconds?</p> <p>Google is pointing to a mail thread about it at <a href="http://mail.python.org/pipermail/python-list/2006-January/533215.html">http://mail.python.org/pipermail/python-list/2006-January/533215.html</a> but it seems not to work. The ...
16
2009-08-26T15:19:05Z
2,904,057
<p>Using a select call is shorter, and should be much more portable</p> <pre><code>import sys, select print "You have ten seconds to answer!" i, o, e = select.select( [sys.stdin], [], [], 10 ) if (i): print "You said", sys.stdin.readline().strip() else: print "You said nothing!" </code></pre>
45
2010-05-25T11:18:11Z
[ "python", "timeout", "keyboard-input" ]
Keyboard input with timeout in Python
1,335,507
<p>How would you prompt the user for some input but timing out after N seconds?</p> <p>Google is pointing to a mail thread about it at <a href="http://mail.python.org/pipermail/python-list/2006-January/533215.html">http://mail.python.org/pipermail/python-list/2006-January/533215.html</a> but it seems not to work. The ...
16
2009-08-26T15:19:05Z
3,911,477
<p>And here's one that works on Windows</p> <p>I haven't been able to get any of these examples to work on Windows so I've merged some different StackOverflow answers to get the following:</p> <pre><code> import threading, msvcrt import sys def readInput(caption, default, timeout = 5): class KeyboardThread(threa...
3
2010-10-12T03:25:58Z
[ "python", "timeout", "keyboard-input" ]
Keyboard input with timeout in Python
1,335,507
<p>How would you prompt the user for some input but timing out after N seconds?</p> <p>Google is pointing to a mail thread about it at <a href="http://mail.python.org/pipermail/python-list/2006-January/533215.html">http://mail.python.org/pipermail/python-list/2006-January/533215.html</a> but it seems not to work. The ...
16
2009-08-26T15:19:05Z
9,705,295
<p>A late answer :)</p> <p>I would do something like this:</p> <pre><code>from time import sleep print('Please provide input in 20 seconds! (Hit Ctrl-C to start)') try: for i in range(0,20): sleep(1) # could use a backward counter to be preeety :) print('No input is given.') except KeyboardInterrupt:...
-4
2012-03-14T15:50:21Z
[ "python", "timeout", "keyboard-input" ]
Keyboard input with timeout in Python
1,335,507
<p>How would you prompt the user for some input but timing out after N seconds?</p> <p>Google is pointing to a mail thread about it at <a href="http://mail.python.org/pipermail/python-list/2006-January/533215.html">http://mail.python.org/pipermail/python-list/2006-January/533215.html</a> but it seems not to work. The ...
16
2009-08-26T15:19:05Z
25,860,968
<p>I spent a good twenty minutes or so on this, so I thought it was worth a shot to put this up here. It is directly building off of user137673's answer, though. I found it most useful to do something like this:</p> <pre><code>#! /usr/bin/env python import signal timeout = None def main(): inp = stdinWait("You ...
1
2014-09-16T04:59:11Z
[ "python", "timeout", "keyboard-input" ]
Keyboard input with timeout in Python
1,335,507
<p>How would you prompt the user for some input but timing out after N seconds?</p> <p>Google is pointing to a mail thread about it at <a href="http://mail.python.org/pipermail/python-list/2006-January/533215.html">http://mail.python.org/pipermail/python-list/2006-January/533215.html</a> but it seems not to work. The ...
16
2009-08-26T15:19:05Z
27,222,779
<p>Not a Python solution, but...</p> <p>I ran in to this problem with a script running under CentOS (Linux), and what worked for my situation was just running the Bash "read -t" command in a subprocess. Brutal disgusting hack, I know, but I feel guilty enough about how well it worked that I wanted to share it with ev...
3
2014-12-01T06:23:49Z
[ "python", "timeout", "keyboard-input" ]
Keyboard input with timeout in Python
1,335,507
<p>How would you prompt the user for some input but timing out after N seconds?</p> <p>Google is pointing to a mail thread about it at <a href="http://mail.python.org/pipermail/python-list/2006-January/533215.html">http://mail.python.org/pipermail/python-list/2006-January/533215.html</a> but it seems not to work. The ...
16
2009-08-26T15:19:05Z
39,390,830
<p>Analogous to Locane's for windows:</p> <pre><code>import subprocess subprocess.call('timeout /T 30') </code></pre>
0
2016-09-08T12:23:07Z
[ "python", "timeout", "keyboard-input" ]
Python FTP Most Recent File
1,335,552
<p>How do I determine the most recently modified file from an ftp directory listing? I used the max function on the unix timestamp locally, but the ftp listing is harder to parse. The contents of each line is only separated by a space. </p> <pre><code>from ftplib import FTP ftp = FTP('ftp.cwi.nl') ftp.login() data = [...
3
2009-08-26T15:26:12Z
1,335,757
<p>You can split each line and get the date:</p> <pre><code>date_str = ' '.join(line.split(' ')[5:8]) </code></pre> <p>Then parse the date (check out egenix <a href="http://www.egenix.com/products/python/mxBase/mxDateTime/" rel="nofollow">mxDateTime</a> package, specifically the <code>DateTimeFromString</code> functi...
0
2009-08-26T15:55:23Z
[ "python", "ftp" ]
Python FTP Most Recent File
1,335,552
<p>How do I determine the most recently modified file from an ftp directory listing? I used the max function on the unix timestamp locally, but the ftp listing is harder to parse. The contents of each line is only separated by a space. </p> <pre><code>from ftplib import FTP ftp = FTP('ftp.cwi.nl') ftp.login() data = [...
3
2009-08-26T15:26:12Z
1,335,928
<p>To parse the date, you can use (from version 2.5 onwards):</p> <pre><code>datetime.datetime.strptime('Mar 21 14:32', '%b %d %H:%M') </code></pre>
1
2009-08-26T16:22:06Z
[ "python", "ftp" ]
Python FTP Most Recent File
1,335,552
<p>How do I determine the most recently modified file from an ftp directory listing? I used the max function on the unix timestamp locally, but the ftp listing is harder to parse. The contents of each line is only separated by a space. </p> <pre><code>from ftplib import FTP ftp = FTP('ftp.cwi.nl') ftp.login() data = [...
3
2009-08-26T15:26:12Z
1,336,131
<p>Just to make some corrections:</p> <pre><code>date_str = ' '.join(line.split()[5:8]) time.strptime(date_str, '%b %d %H:%M') # import time </code></pre> <p>And to find the most recent file</p> <pre><code>for line in data: col_list = line.split() date_str = ' '.join(line.split()[5:8]) if datePattern.sea...
3
2009-08-26T16:52:45Z
[ "python", "ftp" ]
Python FTP Most Recent File
1,335,552
<p>How do I determine the most recently modified file from an ftp directory listing? I used the max function on the unix timestamp locally, but the ftp listing is harder to parse. The contents of each line is only separated by a space. </p> <pre><code>from ftplib import FTP ftp = FTP('ftp.cwi.nl') ftp.login() data = [...
3
2009-08-26T15:26:12Z
3,117,005
<p>If the FTP server supports the <code>MLSD</code> command (and quite possibly it does), you can use the <code>FTPDirectory</code> class from <a href="http://stackoverflow.com/questions/2867217/how-to-delete-files-with-a-python-script-from-a-ftp-server-which-are-older-than-7/3114477#3114477">that</a> answer in a relat...
4
2010-06-25T09:42:35Z
[ "python", "ftp" ]
Is it a good idea to hash a Python class?
1,335,556
<p>For example, suppose I do this:</p> <pre><code>&gt;&gt;&gt; class foo(object): ... pass ... &gt;&gt;&gt; class bar(foo): ... pass ... &gt;&gt;&gt; some_dict = { foo : 'foo', ... bar : 'bar'} &gt;&gt;&gt; &gt;&gt;&gt; some_dict[bar] 'bar' &gt;&gt;&gt; some_dict[foo] 'foo' &gt;&gt;&gt; hash(bar) 165007700 ...
4
2009-08-26T15:27:15Z
1,335,582
<p>Yes, any object that doesn't implement a <code>__hash__()</code> function will return its id when hashed. From <a href="http://docs.python.org/reference/datamodel.html#object.%5F%5Fhash%5F%5F">Python Language Reference: Data Model - Basic Customization</a>:</p> <blockquote> <p>User-defined classes have <code>__c...
7
2009-08-26T15:29:36Z
[ "python", "class", "inheritance", "hash", "dictionary" ]
Is it a good idea to hash a Python class?
1,335,556
<p>For example, suppose I do this:</p> <pre><code>&gt;&gt;&gt; class foo(object): ... pass ... &gt;&gt;&gt; class bar(foo): ... pass ... &gt;&gt;&gt; some_dict = { foo : 'foo', ... bar : 'bar'} &gt;&gt;&gt; &gt;&gt;&gt; some_dict[bar] 'bar' &gt;&gt;&gt; some_dict[foo] 'foo' &gt;&gt;&gt; hash(bar) 165007700 ...
4
2009-08-26T15:27:15Z
1,335,991
<p>Classes have default implementations of <code>__eq__</code> and <code>__hash__</code> that use <code>id()</code> to make comparisons and compute hash values, respectively. That is, they compare by identity. The primary rule for implementing <code>__hash__</code> methods is that if two objects compare equal to each o...
5
2009-08-26T16:31:02Z
[ "python", "class", "inheritance", "hash", "dictionary" ]
Python: ZODB file size growing - not updating?
1,335,615
<p>I am using ZODB to store some data that exists in memory for the sake of persistence. If the service with the data in memory every crashes, restarting will load the data from ZODB rather than querying 100s of thousands of rows in a MySQL db. </p> <p>It seems that every time I save, say 500K of data to my database...
1
2009-08-26T15:34:52Z
1,335,705
<p>When the data in ZODB changes, it's appended to the end of the file. Old data is left there. To reduce the filesize, you need to manually "pack" the database.</p> <p>Google came up with <a href="http://mail.zope.org/pipermail/zodb-dev/2004-July/007706.html" rel="nofollow">this mailing list post</a>.</p>
2
2009-08-26T15:48:57Z
[ "python", "zodb" ]
Python: ZODB file size growing - not updating?
1,335,615
<p>I am using ZODB to store some data that exists in memory for the sake of persistence. If the service with the data in memory every crashes, restarting will load the data from ZODB rather than querying 100s of thousands of rows in a MySQL db. </p> <p>It seems that every time I save, say 500K of data to my database...
1
2009-08-26T15:34:52Z
1,336,013
<p>Since you asked about another storage system in a comment, you might want to look into SQLite.</p> <p>Even though SQLite behaves the same in appending to data first, it offers the vacuum command to recover unused storage space. From the <a href="http://docs.python.org/library/sqlite3.html" rel="nofollow">Python AP...
1
2009-08-26T16:32:54Z
[ "python", "zodb" ]
Performance - Python vs. C#/C++/C reading char-by-char
1,336,259
<p>So I have these giant XML files (and by giant, I mean like 1.5GB+) and they don't have CRLFs. I'm trying to run a diff-like program to find the differences between these files.</p> <p>Since I've yet to find a diff program that won't explode due to memory exhaustion, I've decided the best bet was to add CRLFs after ...
0
2009-08-26T17:13:59Z
1,336,278
<p>All of the languages mentioned typically, at some point, revert to the C runtime library for byte by byte file access. Writing this in C will probably be the fastest option.</p> <p>However, I doubt it will provide a huge speed boost. Python is fairly speedy, if you're doing things correctly.</p> <p>The main way ...
0
2009-08-26T17:16:46Z
[ "c#", "python", "performance", "character" ]
Performance - Python vs. C#/C++/C reading char-by-char
1,336,259
<p>So I have these giant XML files (and by giant, I mean like 1.5GB+) and they don't have CRLFs. I'm trying to run a diff-like program to find the differences between these files.</p> <p>Since I've yet to find a diff program that won't explode due to memory exhaustion, I've decided the best bet was to add CRLFs after ...
0
2009-08-26T17:13:59Z
1,336,280
<p>you could try xmldiff - <a href="http://msdn.microsoft.com/en-us/library/aa302294.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa302294.aspx</a> </p> <p>I haven't used it for such huge data but I think it would be reasonably optimized</p>
0
2009-08-26T17:17:30Z
[ "c#", "python", "performance", "character" ]
Performance - Python vs. C#/C++/C reading char-by-char
1,336,259
<p>So I have these giant XML files (and by giant, I mean like 1.5GB+) and they don't have CRLFs. I'm trying to run a diff-like program to find the differences between these files.</p> <p>Since I've yet to find a diff program that won't explode due to memory exhaustion, I've decided the best bet was to add CRLFs after ...
0
2009-08-26T17:13:59Z
1,336,282
<p>Reading and writing a single character at a time is almost always going to be slow, because disks are block-based devices, rather than character-based devices - it will read a lot more than just the one byte you're after, and the surplus parts need to be discarded.</p> <p>Try reading and writing more at a time, say...
10
2009-08-26T17:17:43Z
[ "c#", "python", "performance", "character" ]
Performance - Python vs. C#/C++/C reading char-by-char
1,336,259
<p>So I have these giant XML files (and by giant, I mean like 1.5GB+) and they don't have CRLFs. I'm trying to run a diff-like program to find the differences between these files.</p> <p>Since I've yet to find a diff program that won't explode due to memory exhaustion, I've decided the best bet was to add CRLFs after ...
0
2009-08-26T17:13:59Z
1,336,287
<p>Rather than reading byte by byte, which incurs a disk access for each byte read, try reading ~20 MB at a time and doing your search + replace on that :)</p> <p>You can probably do this in Notepad....</p> <p>Billy3</p>
1
2009-08-26T17:18:53Z
[ "c#", "python", "performance", "character" ]
Performance - Python vs. C#/C++/C reading char-by-char
1,336,259
<p>So I have these giant XML files (and by giant, I mean like 1.5GB+) and they don't have CRLFs. I'm trying to run a diff-like program to find the differences between these files.</p> <p>Since I've yet to find a diff program that won't explode due to memory exhaustion, I've decided the best bet was to add CRLFs after ...
0
2009-08-26T17:13:59Z
1,336,338
<p>For the type of problem you describe, I suspect the algorithm you employ for comparing the data will have a much more significant effect than the I/O model or language. In fact, string allocation and search may be more expensive here than anything else.</p> <p>Some general suggestions before you write this yourself...
1
2009-08-26T17:29:47Z
[ "c#", "python", "performance", "character" ]
Performance - Python vs. C#/C++/C reading char-by-char
1,336,259
<p>So I have these giant XML files (and by giant, I mean like 1.5GB+) and they don't have CRLFs. I'm trying to run a diff-like program to find the differences between these files.</p> <p>Since I've yet to find a diff program that won't explode due to memory exhaustion, I've decided the best bet was to add CRLFs after ...
0
2009-08-26T17:13:59Z
1,336,387
<p>I put this as a comment on another answer, but in case you miss it--you might want to look at <a href="http://shootout.alioth.debian.org/u64q/benchmark.php?test=all&amp;lang=all&amp;lang2=python&amp;box=1" rel="nofollow">The Shootout</a>. It's a highly optimized set of code for various problems in many languages.</...
0
2009-08-26T17:41:37Z
[ "c#", "python", "performance", "character" ]
Performance - Python vs. C#/C++/C reading char-by-char
1,336,259
<p>So I have these giant XML files (and by giant, I mean like 1.5GB+) and they don't have CRLFs. I'm trying to run a diff-like program to find the differences between these files.</p> <p>Since I've yet to find a diff program that won't explode due to memory exhaustion, I've decided the best bet was to add CRLFs after ...
0
2009-08-26T17:13:59Z
1,336,642
<p>Why don't you just use sed? cat giant.xml | sed 's/>/>\x0a\x0d/g' > giant-with-linebreaks.xml</p>
3
2009-08-26T18:29:45Z
[ "c#", "python", "performance", "character" ]
Performance - Python vs. C#/C++/C reading char-by-char
1,336,259
<p>So I have these giant XML files (and by giant, I mean like 1.5GB+) and they don't have CRLFs. I'm trying to run a diff-like program to find the differences between these files.</p> <p>Since I've yet to find a diff program that won't explode due to memory exhaustion, I've decided the best bet was to add CRLFs after ...
0
2009-08-26T17:13:59Z
1,337,252
<p>As others said, if you do it in C it will be pretty much unbeatable, because C buffers I/O, and getc() is inlined (in my memory).</p> <p>Your real performance issue will be in the diff.</p> <p>Maybe there's a pretty good one out there, but for those size files I doubt it. For fun, I'm a do-it-yourselfer. The strat...
0
2009-08-26T20:25:15Z
[ "c#", "python", "performance", "character" ]
job queue implementation for python
1,336,489
<p>Do you know/use any distributed job queue for python? Can you share links or tools</p>
13
2009-08-26T18:01:08Z
1,336,515
<p>You probably want to look at multiprocessing's Queue. Included in Python 2.6, get it on PyPI for earlier versions of Python.</p> <p>Standard library documentation: <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">http://docs.python.org/library/multiprocessing.html</a> On PyPI: <a href="h...
-1
2009-08-26T18:06:14Z
[ "python", "job-queue" ]