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
Why does this python method gives an error saying global name not defined?
1,286,235
<p>I have a single code file for my Google App Engine project. This simple file has one class, and inside it a few methods. Why does this python method gives an error saying global name not defined? <br>Erro NameError: global name 'gen_groups' is not defined</p> <pre><code>import wsgiref.handlers from google.appengi...
0
2009-08-17T05:07:54Z
1,286,249
<p>It's an instance method, you need to use <code>self.gen_groups(...)</code> and <code>self.gen_albums(...)</code>.</p> <p><strong>Edit:</strong> I'm guessing the <code>TypeError</code> you are getting now is because you removed the 'self' argument from <code>gen_groups()</code>. You'll need to put it back in:</p> <...
4
2009-08-17T05:12:10Z
[ "python", "google-app-engine" ]
Why does this python method gives an error saying global name not defined?
1,286,235
<p>I have a single code file for my Google App Engine project. This simple file has one class, and inside it a few methods. Why does this python method gives an error saying global name not defined? <br>Erro NameError: global name 'gen_groups' is not defined</p> <pre><code>import wsgiref.handlers from google.appengi...
0
2009-08-17T05:07:54Z
1,286,252
<p>You have to use it like this:</p> <pre><code>self.gen_groups(input) </code></pre> <p>There is not implicit "self" in Python.</p>
1
2009-08-17T05:12:39Z
[ "python", "google-app-engine" ]
Using the Python NLTK (2.0b5) on the Google App Engine
1,286,301
<p>I have been trying to make the NLTK (Natural Language Toolkit) work on the Google App Engine. The steps I followed are:</p> <ol> <li>Download the installer and run it (a .dmg file, as I am using a Mac).</li> <li>copy the nltk folder out of the python site-packages directory and place it as a sub-folder in my proje...
17
2009-08-17T05:36:55Z
1,286,326
<p>NLTK, I believe, does try its best to be pure-Python as a fallback (graceful degradation) when it can't have the C-coded accelerator extensions it would like. However one always needs to be moving with utter care to boldly inject such a rich package (recursively zipping up all of the .py files and using zipimport mi...
3
2009-08-17T05:49:33Z
[ "python", "google-app-engine", "nlp", "nltk" ]
Using the Python NLTK (2.0b5) on the Google App Engine
1,286,301
<p>I have been trying to make the NLTK (Natural Language Toolkit) work on the Google App Engine. The steps I followed are:</p> <ol> <li>Download the installer and run it (a .dmg file, as I am using a Mac).</li> <li>copy the nltk folder out of the python site-packages directory and place it as a sub-folder in my proje...
17
2009-08-17T05:36:55Z
1,287,150
<p><a href="http://groups.google.com/group/nltk-users/browse%5Fthread/thread/95db3032ccca7ab8/">oakmad</a> has managed to successfully work through deploying <strong>SEVERAL</strong> NLTK modules to GAE. Hope this helps. But , but be honest, I still don't think it's true even after read the post.</p>
8
2009-08-17T10:08:31Z
[ "python", "google-app-engine", "nlp", "nltk" ]
Using the Python NLTK (2.0b5) on the Google App Engine
1,286,301
<p>I have been trying to make the NLTK (Natural Language Toolkit) work on the Google App Engine. The steps I followed are:</p> <ol> <li>Download the installer and run it (a .dmg file, as I am using a Mac).</li> <li>copy the nltk folder out of the python site-packages directory and place it as a sub-folder in my proje...
17
2009-08-17T05:36:55Z
1,287,621
<p>The problem here is that <code>nltk</code> is attempting to do recursive imports: When <code>nltk/__init__.py</code> is imported, it imports <code>nltk/internals.py</code>, which then attempts to import <code>nltk</code> again. Since <code>nltk</code> is in the middle of being imported itself, it fails with a (rathe...
4
2009-08-17T12:15:05Z
[ "python", "google-app-engine", "nlp", "nltk" ]
Using the Python NLTK (2.0b5) on the Google App Engine
1,286,301
<p>I have been trying to make the NLTK (Natural Language Toolkit) work on the Google App Engine. The steps I followed are:</p> <ol> <li>Download the installer and run it (a .dmg file, as I am using a Mac).</li> <li>copy the nltk folder out of the python site-packages directory and place it as a sub-folder in my proje...
17
2009-08-17T05:36:55Z
4,476,134
<p>I got some basic stuff to work. Code available at <a href="http://code.google.com/p/nltk-gae/" rel="nofollow">http://code.google.com/p/nltk-gae/</a></p>
1
2010-12-18T01:40:06Z
[ "python", "google-app-engine", "nlp", "nltk" ]
Using the Python NLTK (2.0b5) on the Google App Engine
1,286,301
<p>I have been trying to make the NLTK (Natural Language Toolkit) work on the Google App Engine. The steps I followed are:</p> <ol> <li>Download the installer and run it (a .dmg file, as I am using a Mac).</li> <li>copy the nltk folder out of the python site-packages directory and place it as a sub-folder in my proje...
17
2009-08-17T05:36:55Z
13,110,476
<p>I've forked <a href="https://github.com/rutherford/nltk-gae" rel="nofollow">NLTK 2.0.3 on github to run it on app engine</a>; tokenizing and simple POS tagging working with the MaxEnt Treebank tagger.</p>
4
2012-10-28T16:04:08Z
[ "python", "google-app-engine", "nlp", "nltk" ]
Python lazy attributes that don't eval on hasattr()
1,286,548
<p>Is it possible to make a decorator that makes attributes lazy that do not eval when you try to access it with <code>hasattr()</code>? I worked out how to make it lazy, but <code>hasattr()</code> makes it evaluate prematurely. E.g.,</p> <pre><code>class lazyattribute: # Magic. class A: @lazyattribute de...
4
2009-08-17T07:15:58Z
1,286,598
<p>The problem is that <code>hasattr</code> uses <code>getattr</code> so your attribute is always going to be evaluated when you use <code>hasattr</code>. If you post the code for your <code>lazyattribute</code> magic hopefully someone can suggest an alternative way of testing the presence of the attribute which doesn'...
0
2009-08-17T07:37:25Z
[ "python", "decorator", "lazy-evaluation" ]
Python lazy attributes that don't eval on hasattr()
1,286,548
<p>Is it possible to make a decorator that makes attributes lazy that do not eval when you try to access it with <code>hasattr()</code>? I worked out how to make it lazy, but <code>hasattr()</code> makes it evaluate prematurely. E.g.,</p> <pre><code>class lazyattribute: # Magic. class A: @lazyattribute de...
4
2009-08-17T07:15:58Z
1,286,610
<p>It may be difficult to do. From the <a href="http://docs.python.org/library/functions.html#hasattr" rel="nofollow">hasattr documentation</a>:</p> <p></p> <h3>hasattr(object, name)</h3> <p>The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, Fa...
4
2009-08-17T07:40:27Z
[ "python", "decorator", "lazy-evaluation" ]
Python lazy attributes that don't eval on hasattr()
1,286,548
<p>Is it possible to make a decorator that makes attributes lazy that do not eval when you try to access it with <code>hasattr()</code>? I worked out how to make it lazy, but <code>hasattr()</code> makes it evaluate prematurely. E.g.,</p> <pre><code>class lazyattribute: # Magic. class A: @lazyattribute de...
4
2009-08-17T07:15:58Z
1,286,697
<p>well the fix is a little hackish but it consists of the following</p> <ol> <li>rename hasattr (say as _hasattr)</li> <li>rebind hasattr as the following:</li> </ol> <p><pre><code> def hasattr(obj, name): try: return obj._hasattr(name) or _hasattr(obj, name) except: return _hasattr(obj, n...
-2
2009-08-17T08:04:15Z
[ "python", "decorator", "lazy-evaluation" ]
Python lazy attributes that don't eval on hasattr()
1,286,548
<p>Is it possible to make a decorator that makes attributes lazy that do not eval when you try to access it with <code>hasattr()</code>? I worked out how to make it lazy, but <code>hasattr()</code> makes it evaluate prematurely. E.g.,</p> <pre><code>class lazyattribute: # Magic. class A: @lazyattribute de...
4
2009-08-17T07:15:58Z
1,291,621
<p>What nobody seems to have addressed so far is that perhaps the best thing to do is not to use <code>hasattr()</code>. Instead, go for EAFP (Easier to Ask Forgiveness than Permission).</p> <pre><code>try: x = foo.bar except AttributeError: # what went in your else-block ... else: # what went in your ...
2
2009-08-18T03:21:54Z
[ "python", "decorator", "lazy-evaluation" ]
Python lazy attributes that don't eval on hasattr()
1,286,548
<p>Is it possible to make a decorator that makes attributes lazy that do not eval when you try to access it with <code>hasattr()</code>? I worked out how to make it lazy, but <code>hasattr()</code> makes it evaluate prematurely. E.g.,</p> <pre><code>class lazyattribute: # Magic. class A: @lazyattribute de...
4
2009-08-17T07:15:58Z
1,291,773
<p>I'm curious why you need something like this. If <code>hasattr</code> ends up calling your "compute function", then so be it. Just how lazy does your property need to be anyway?</p> <p>Still, here's a rather inelegeant way of doing it by examining the calling function's name. It could probably be coded a little ...
0
2009-08-18T04:16:43Z
[ "python", "decorator", "lazy-evaluation" ]
Django - String to Date - Date to UNIX Timestamp
1,286,619
<p>I need to convert a date from a string (entered into a url) in the form of 12/09/2008-12:40:49. Obviously, I'll need a UNIX Timestamp at the end of it, but before I get that I need the Date object first. </p> <p>How do I do this? I can't find any resources that show the date in that format? Thank you.</p>
4
2009-08-17T07:42:58Z
1,286,807
<p>You can use the <a href="http://docs.python.org/library/time.html#time.strptime" rel="nofollow"><code>time.strptime()</code></a> method to parse a date string. This will return a <code>time_struct</code> that you can pass to <a href="http://docs.python.org/library/time.html#time.mktime" rel="nofollow"><code>time.mkt...
2
2009-08-17T08:38:12Z
[ "python", "django" ]
Django - String to Date - Date to UNIX Timestamp
1,286,619
<p>I need to convert a date from a string (entered into a url) in the form of 12/09/2008-12:40:49. Obviously, I'll need a UNIX Timestamp at the end of it, but before I get that I need the Date object first. </p> <p>How do I do this? I can't find any resources that show the date in that format? Thank you.</p>
4
2009-08-17T07:42:58Z
1,286,817
<p>You need the <code>strptime</code> method. If you're on Python 2.5 or higher, this is a method on <code>datetime</code>, otherwise you have to use a combination of the <code>time</code> and <code>datetime</code> modules to achieve this.</p> <p>Python 2.5 up:</p> <pre><code>from datetime import datetime dt = dateti...
12
2009-08-17T08:40:40Z
[ "python", "django" ]
python scooping and recursion
1,286,626
<p>I am struck in a small recursive code. I have printed output and it prints fine but when I try to put a counter to actually count my answers, it gives me scooping errors.</p> <pre><code>total = 0 def foo(me, t): if t&lt;0: return if t==0: total = total+1 return for i in range(1, me+1): ...
1
2009-08-17T07:44:33Z
1,286,650
<p>You forgot to make sure to set total as a global in your function. You said "I have tried to use global as well but in vain." but when I try it below it doesn't throw any error:</p> <pre><code>total = 0 def foo(me, t): global total if t&lt;0: return if t==0: total = total+1 retur...
1
2009-08-17T07:49:44Z
[ "python", "recursion", "scope" ]
python scooping and recursion
1,286,626
<p>I am struck in a small recursive code. I have printed output and it prints fine but when I try to put a counter to actually count my answers, it gives me scooping errors.</p> <pre><code>total = 0 def foo(me, t): if t&lt;0: return if t==0: total = total+1 return for i in range(1, me+1): ...
1
2009-08-17T07:44:33Z
1,286,814
<p>As mentioned by others, you need the <code>global</code> statement for total. Also, as noted by Svante, the <code>for</code> loop is unnecessary as coded since <code>i</code> is always <code>1</code>. So, with an equivalent version of your code:</p> <pre><code>total = 0 def foo(me, t): global total if t &...
2
2009-08-17T08:39:25Z
[ "python", "recursion", "scope" ]
python scooping and recursion
1,286,626
<p>I am struck in a small recursive code. I have printed output and it prints fine but when I try to put a counter to actually count my answers, it gives me scooping errors.</p> <pre><code>total = 0 def foo(me, t): if t&lt;0: return if t==0: total = total+1 return for i in range(1, me+1): ...
1
2009-08-17T07:44:33Z
1,286,821
<p>I'm not sure you really know what you are trying to do... (at least if you say that adding the global keyword gives incorrect results, but silences the errors)</p> <p>you <strong>do</strong> need the statement "global total" if you are going to to try to reference total (which you are doing)</p> <p>(what are you e...
1
2009-08-17T08:42:00Z
[ "python", "recursion", "scope" ]
python scooping and recursion
1,286,626
<p>I am struck in a small recursive code. I have printed output and it prints fine but when I try to put a counter to actually count my answers, it gives me scooping errors.</p> <pre><code>total = 0 def foo(me, t): if t&lt;0: return if t==0: total = total+1 return for i in range(1, me+1): ...
1
2009-08-17T07:44:33Z
1,286,868
<p>As a generic advice, recursion should <strong>always</strong> use return values, and not global variables. Recursion has already its own load of complexity, increasing it with side-effects and not clear interfaces will make it even worse.</p> <p>You should try something in the lines of this:</p> <pre><code>def foo...
1
2009-08-17T08:54:35Z
[ "python", "recursion", "scope" ]
python scooping and recursion
1,286,626
<p>I am struck in a small recursive code. I have printed output and it prints fine but when I try to put a counter to actually count my answers, it gives me scooping errors.</p> <pre><code>total = 0 def foo(me, t): if t&lt;0: return if t==0: total = total+1 return for i in range(1, me+1): ...
1
2009-08-17T07:44:33Z
1,288,429
<p>Thanks people, thanks youarf, Liffredo. I was wrong, I didnt notice that I was returning before adding-up things, loop was only running once, I have fixed the code. It is like this:</p> <pre><code>def foo(me, t): if t&lt;0: return 0 if t==0: return 1 toreturn = 0 for i in range(1, me+1): ...
0
2009-08-17T14:54:38Z
[ "python", "recursion", "scope" ]
how fast is python's slice
1,286,757
<p>In order to save space and the complexity of having to maintain the consistency of data between different sources, I'm considering storing start/end indices for some substrings instead of storing the substrings themselves. The trick is that if I do so, it's possible I'll be creating slices ALL the time. Is this so...
8
2009-08-17T08:24:06Z
1,286,777
<ol> <li><p>Fast enough as opposed to what? How do you do it right now? What exactly are you storing, what exactly are you retrieving? The answer probably highly depends on this. Which brings us to ...</p></li> <li><p>Measure! Don't discuss and analyze theoretically; try and measure what is the more performant way. The...
7
2009-08-17T08:29:08Z
[ "python", "optimization" ]
how fast is python's slice
1,286,757
<p>In order to save space and the complexity of having to maintain the consistency of data between different sources, I'm considering storing start/end indices for some substrings instead of storing the substrings themselves. The trick is that if I do so, it's possible I'll be creating slices ALL the time. Is this so...
8
2009-08-17T08:24:06Z
1,286,918
<p>premature optimization is the rool of all evil.</p> <p>Prove to yourself that you really have a need to optimize code, then act.</p>
0
2009-08-17T09:08:36Z
[ "python", "optimization" ]
how fast is python's slice
1,286,757
<p>In order to save space and the complexity of having to maintain the consistency of data between different sources, I'm considering storing start/end indices for some substrings instead of storing the substrings themselves. The trick is that if I do so, it's possible I'll be creating slices ALL the time. Is this so...
8
2009-08-17T08:24:06Z
1,287,380
<p>I haven't done any measurements either, but since it sounds like you're already taking a C approach to a problem in Python, you might want to take a look at <a href="http://docs.python.org/library/mmap.html" rel="nofollow">Python's built-in <code>mmap</code> library</a>:</p> <blockquote> <p>Memory-mapped file obj...
1
2009-08-17T11:11:37Z
[ "python", "optimization" ]
how fast is python's slice
1,286,757
<p>In order to save space and the complexity of having to maintain the consistency of data between different sources, I'm considering storing start/end indices for some substrings instead of storing the substrings themselves. The trick is that if I do so, it's possible I'll be creating slices ALL the time. Is this so...
8
2009-08-17T08:24:06Z
1,288,519
<p>In a comment the OP mentions bloat "in the database" -- but no information regarding what database he's talking about; from the scant information in that comment it would seem that Python string slices aren't necessarily what's involved, rather, the "slicing" would be done by the DB engine upon retrieval.</p> <p>If...
3
2009-08-17T15:11:52Z
[ "python", "optimization" ]
how fast is python's slice
1,286,757
<p>In order to save space and the complexity of having to maintain the consistency of data between different sources, I'm considering storing start/end indices for some substrings instead of storing the substrings themselves. The trick is that if I do so, it's possible I'll be creating slices ALL the time. Is this so...
8
2009-08-17T08:24:06Z
1,289,744
<p>Would slices be ineffective because they create copies of the source string? This may or may not be an issue. If it turns out to be an issue, would it not be possible to simply implement a "String view"; an object that has a reference to the source string and has a start and end point.. Upon access/iteration, it jus...
1
2009-08-17T18:53:55Z
[ "python", "optimization" ]
Python SIP library
1,286,875
<p>I need to write python application connect to trixbox that run as SIP server. But I not found any library that implement in python. I found SIP SKD at <a href="http://www.vaxvoip.com/">http://www.vaxvoip.com/</a> but it not support python. Can anyone suggest me an alternative to VaxVoip? Thank you.</p>
19
2009-08-17T08:55:21Z
1,286,973
<p>There are <a href="http://trac.pjsip.org/repos/wiki/Python_SIP_Tutorial">Python bindings</a> for the PJSUA API.</p>
10
2009-08-17T09:22:48Z
[ "python", "voip", "sip", "trixbox" ]
Python SIP library
1,286,875
<p>I need to write python application connect to trixbox that run as SIP server. But I not found any library that implement in python. I found SIP SKD at <a href="http://www.vaxvoip.com/">http://www.vaxvoip.com/</a> but it not support python. Can anyone suggest me an alternative to VaxVoip? Thank you.</p>
19
2009-08-17T08:55:21Z
1,380,887
<p><a href="http://twistedmatrix.com/trac/">Twisted</a> supports SIP. That's really cool</p>
12
2009-09-04T18:41:55Z
[ "python", "voip", "sip", "trixbox" ]
Python SIP library
1,286,875
<p>I need to write python application connect to trixbox that run as SIP server. But I not found any library that implement in python. I found SIP SKD at <a href="http://www.vaxvoip.com/">http://www.vaxvoip.com/</a> but it not support python. Can anyone suggest me an alternative to VaxVoip? Thank you.</p>
19
2009-08-17T08:55:21Z
1,761,507
<p>You might want to have a look at <a href="http://www.b2bua.org/">Sippy</a>. It's a B2BUA with a complete SIP stack implementation underneath (you could use just that). It's written entirely in Python, so it's pretty hackable. Sippy is implemented with Twisted but uses none of its SIP functionality.</p>
8
2009-11-19T07:27:55Z
[ "python", "voip", "sip", "trixbox" ]
'datetime.time' has no 'mktime'
1,287,598
<p>I'm trying to convert a datetime object to a UNIX timestamp (preferably in milliseconds, though I wouldn't mind with and without). </p> <p>Mktime seems to be the method that usually gets it, however I keep getting the error:</p> <p>AttributeError: type object 'datetime.time' has no attribute 'mktime'.</p> <p>Can ...
6
2009-08-17T12:08:48Z
1,287,629
<p>I think you have done</p> <pre><code>from datetime import datetime, time </code></pre> <p>instead of</p> <pre><code>import time from datetime import datetime </code></pre> <p>so that the object called <code>time</code> is actually coming from the datetime module, not the time module.</p>
18
2009-08-17T12:17:10Z
[ "python", "datetime" ]
'datetime.time' has no 'mktime'
1,287,598
<p>I'm trying to convert a datetime object to a UNIX timestamp (preferably in milliseconds, though I wouldn't mind with and without). </p> <p>Mktime seems to be the method that usually gets it, however I keep getting the error:</p> <p>AttributeError: type object 'datetime.time' has no attribute 'mktime'.</p> <p>Can ...
6
2009-08-17T12:08:48Z
9,607,940
<p>Actually, even using the above answer, I still got the same error message.</p> <p>I´ve solved my problem using</p> <pre><code>&gt;&gt;&gt;&gt;from time import mktime as mktime &gt;&gt;&gt;&gt;today = mktime(2012, 12, 21, 0, 0, 0, 0, 0, 0) </code></pre> <p>I don't know the why, but, it only worked using the alias...
0
2012-03-07T19:38:38Z
[ "python", "datetime" ]
Which development environment should I use for developing Google App Engine with Python?
1,287,606
<p>I would like to ask which IDE should I use for developing applications for Google App Engine with Python language?</p> <p>Is <a href="http://code.google.com/intl/it-IT/appengine/articles/eclipse.html" rel="nofollow">Eclipse</a> suitable or is there any other development environment better?</p> <p>Please give me so...
1
2009-08-17T12:11:20Z
1,287,634
<p>I think the answers you are looking for are here </p> <p><a href="http://stackoverflow.com/questions/495579/best-opensource-ide-for-building-applications-on-google-app-engine">http://stackoverflow.com/questions/495579/best-opensource-ide-for-building-applications-on-google-app-engine</a></p>
3
2009-08-17T12:17:35Z
[ "python", "google-app-engine", "ide" ]
Which development environment should I use for developing Google App Engine with Python?
1,287,606
<p>I would like to ask which IDE should I use for developing applications for Google App Engine with Python language?</p> <p>Is <a href="http://code.google.com/intl/it-IT/appengine/articles/eclipse.html" rel="nofollow">Eclipse</a> suitable or is there any other development environment better?</p> <p>Please give me so...
1
2009-08-17T12:11:20Z
1,287,697
<p>Eclipse with the <a href="http://pydev.sourceforge.net/" rel="nofollow">PyDev</a> plugin is very nice. Recent versions even go out of their way to support App Engine, with builtin support for uploading your project, etc without having to use the command line scripts. </p> <p>See the <a href="http://pydev.blogspot.c...
4
2009-08-17T12:31:33Z
[ "python", "google-app-engine", "ide" ]
Which development environment should I use for developing Google App Engine with Python?
1,287,606
<p>I would like to ask which IDE should I use for developing applications for Google App Engine with Python language?</p> <p>Is <a href="http://code.google.com/intl/it-IT/appengine/articles/eclipse.html" rel="nofollow">Eclipse</a> suitable or is there any other development environment better?</p> <p>Please give me so...
1
2009-08-17T12:11:20Z
1,287,752
<p>I've always been using <a href="http://en.wikipedia.org/wiki/Vim_%28text_editor%29" rel="nofollow">Vim</a> (with or without plugins to make it more IDE-like) for Python programming, it's simple but powerful enough.</p>
0
2009-08-17T12:41:30Z
[ "python", "google-app-engine", "ide" ]
Which development environment should I use for developing Google App Engine with Python?
1,287,606
<p>I would like to ask which IDE should I use for developing applications for Google App Engine with Python language?</p> <p>Is <a href="http://code.google.com/intl/it-IT/appengine/articles/eclipse.html" rel="nofollow">Eclipse</a> suitable or is there any other development environment better?</p> <p>Please give me so...
1
2009-08-17T12:11:20Z
1,288,211
<p><a href="http://www.wingware.com/" rel="nofollow">Wing IDE</a> is a pretty solid IDE for python. It's not free though (open source or $$).</p>
0
2009-08-17T14:22:27Z
[ "python", "google-app-engine", "ide" ]
Which development environment should I use for developing Google App Engine with Python?
1,287,606
<p>I would like to ask which IDE should I use for developing applications for Google App Engine with Python language?</p> <p>Is <a href="http://code.google.com/intl/it-IT/appengine/articles/eclipse.html" rel="nofollow">Eclipse</a> suitable or is there any other development environment better?</p> <p>Please give me so...
1
2009-08-17T12:11:20Z
1,293,068
<p>I use <a href="http://www.activestate.com/komodo%5Fedit/" rel="nofollow">Komodo Edit</a>.</p>
2
2009-08-18T10:30:29Z
[ "python", "google-app-engine", "ide" ]
Any efficient way to read datas from large binary file?
1,287,747
<p>I need to handle tens of Gigabytes data in one binary file. Each record in the data file is variable length.</p> <p>So the file is like:</p> <pre><code>&lt;len1&gt;&lt;data1&gt;&lt;len2&gt;&lt;data2&gt;..........&lt;lenN&gt;&lt;dataN&gt; </code></pre> <p>The data contains integer, pointer, double value and so on....
3
2009-08-17T12:40:41Z
1,287,765
<p>have a look at <code>array</code> module, specifically at <a href="http://docs.python.org/library/array.html#array.array.fromfile" rel="nofollow"><code>array.fromfile</code></a> method. This bit:</p> <blockquote> <p>Each record in the data file is variable length.</p> </blockquote> <p>is rather unfortunate. but ...
2
2009-08-17T12:44:40Z
[ "python", "file", "binary" ]
Any efficient way to read datas from large binary file?
1,287,747
<p>I need to handle tens of Gigabytes data in one binary file. Each record in the data file is variable length.</p> <p>So the file is like:</p> <pre><code>&lt;len1&gt;&lt;data1&gt;&lt;len2&gt;&lt;data2&gt;..........&lt;lenN&gt;&lt;dataN&gt; </code></pre> <p>The data contains integer, pointer, double value and so on....
3
2009-08-17T12:40:41Z
1,287,799
<p>For a similar task, I defined a class like this:</p> <pre><code>class foo(Structure): _fields_ = [("myint", c_uint32)] </code></pre> <p>created an instance</p> <pre><code>bar = foo() </code></pre> <p>and did,</p> <pre><code>block = file.read(sizeof(bar)) memmove(addressof(bar), block, sizeof(bar)) </cod...
2
2009-08-17T12:52:27Z
[ "python", "file", "binary" ]
Any efficient way to read datas from large binary file?
1,287,747
<p>I need to handle tens of Gigabytes data in one binary file. Each record in the data file is variable length.</p> <p>So the file is like:</p> <pre><code>&lt;len1&gt;&lt;data1&gt;&lt;len2&gt;&lt;data2&gt;..........&lt;lenN&gt;&lt;dataN&gt; </code></pre> <p>The data contains integer, pointer, double value and so on....
3
2009-08-17T12:40:41Z
1,288,037
<p>What if you use dump the data file into sqlite3 in memory.</p> <pre><code>import sqlite3 sqlite3.Connection(":memory:") </code></pre> <p>You can then use sql to process the data.</p> <p>Besides, you might want to look at <a href="http://docs.python.org/tutorial/classes.html#generators" rel="nofollow">generators</...
1
2009-08-17T13:44:54Z
[ "python", "file", "binary" ]
Any efficient way to read datas from large binary file?
1,287,747
<p>I need to handle tens of Gigabytes data in one binary file. Each record in the data file is variable length.</p> <p>So the file is like:</p> <pre><code>&lt;len1&gt;&lt;data1&gt;&lt;len2&gt;&lt;data2&gt;..........&lt;lenN&gt;&lt;dataN&gt; </code></pre> <p>The data contains integer, pointer, double value and so on....
3
2009-08-17T12:40:41Z
1,288,382
<p><code>struct</code> and <code>array</code>, which other answers recommend, are fine for the details of the implementation, and might be all you need if your needs are always to sequentially read all of the file or a prefix of it. Other options include <a href="http://docs.python.org/library/functions.html?highlight=...
4
2009-08-17T14:48:12Z
[ "python", "file", "binary" ]
Any efficient way to read datas from large binary file?
1,287,747
<p>I need to handle tens of Gigabytes data in one binary file. Each record in the data file is variable length.</p> <p>So the file is like:</p> <pre><code>&lt;len1&gt;&lt;data1&gt;&lt;len2&gt;&lt;data2&gt;..........&lt;lenN&gt;&lt;dataN&gt; </code></pre> <p>The data contains integer, pointer, double value and so on....
3
2009-08-17T12:40:41Z
1,288,491
<p>For help with parsing the file without reading it into memory you can use the <a href="http://python-bitstring.googlecode.com" rel="nofollow">bitstring</a> module.</p> <p>Internally this is using the struct module and a bytearray, but an immutable Bits object can be initialised with a filename so it won't read it a...
2
2009-08-17T15:05:38Z
[ "python", "file", "binary" ]
Any efficient way to read datas from large binary file?
1,287,747
<p>I need to handle tens of Gigabytes data in one binary file. Each record in the data file is variable length.</p> <p>So the file is like:</p> <pre><code>&lt;len1&gt;&lt;data1&gt;&lt;len2&gt;&lt;data2&gt;..........&lt;lenN&gt;&lt;dataN&gt; </code></pre> <p>The data contains integer, pointer, double value and so on....
3
2009-08-17T12:40:41Z
1,289,076
<p>PyTables is a very good library to handle HDF5, a binary format used in astronomy and meteorology to handle very big datasets:</p> <ul> <li><a href="http://www.pytables.org/moin" rel="nofollow">PyTables</a></li> </ul> <p>It works more or less like an hierarchical database, where you can store multiple tables, insi...
1
2009-08-17T16:42:12Z
[ "python", "file", "binary" ]
Can someone suggest a well-designed Python wrapper of a REST API?
1,288,198
<p>I'm writing a new one (for Netflix), and am simply wondering if there are any great reference libraries for me to study. </p> <p>In particular I'm looking for clever ways to express a single REST endpoint in code, which needs a least a URL, method, and params, and in the case of Netflix, information about the auth...
7
2009-08-17T14:19:42Z
1,288,295
<p>Is what you have in mind something like <a href="http://uswaretech.com/blog/2009/06/bing-python-api/" rel="nofollow">this</a>, <a href="http://remixblog.info/2009/02/27/the-premix-python-wrapper-for-the-best-buy-remix-api/" rel="nofollow">this</a> and <a href="http://wiki.github.com/soundcloud/api/python-api-wrapper...
3
2009-08-17T14:35:08Z
[ "python", "api", "rest" ]
Python Strongly type lists
1,288,421
<p>I am using eclips for python and i am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is:When i am accessing any item from the list, the IDE does not know it's type because in python we do not declare the variable with it's type, so t...
2
2009-08-17T14:53:58Z
1,288,452
<p>You shouldn't be writing code to suit your IDE - the IDE is supposed to support you. If you're having trouble with it, then switch. There are plenty of editors that deal with Python properly.</p>
5
2009-08-17T14:59:19Z
[ "python", "eclipse", "list", "strong-typing" ]
Python Strongly type lists
1,288,421
<p>I am using eclips for python and i am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is:When i am accessing any item from the list, the IDE does not know it's type because in python we do not declare the variable with it's type, so t...
2
2009-08-17T14:53:58Z
1,293,342
<p>I think I found a good managable solution. Actually it is trivial but may help (I used it now). When I want to access the list then I assign the object which I want to access to a variable ex: </p> <pre><code>x = AutomataBranch() x = self.cfgAutomata[i] </code></pre> <p>The first line is used only to make the I...
-2
2009-08-18T11:43:32Z
[ "python", "eclipse", "list", "strong-typing" ]
Python Strongly type lists
1,288,421
<p>I am using eclips for python and i am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is:When i am accessing any item from the list, the IDE does not know it's type because in python we do not declare the variable with it's type, so t...
2
2009-08-17T14:53:58Z
1,293,395
<p>Python is strongly typed and Python lists are too. Your problem come from the fact that Python is dynamically typed. Therefor a var can contain any type, and therefor no IDE can guess what is the type of your parameter, nor give you code completion for the methods. </p> <p>This is how it is, there is no clean worka...
4
2009-08-18T11:56:20Z
[ "python", "eclipse", "list", "strong-typing" ]
Python Strongly type lists
1,288,421
<p>I am using eclips for python and i am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is:When i am accessing any item from the list, the IDE does not know it's type because in python we do not declare the variable with it's type, so t...
2
2009-08-17T14:53:58Z
1,293,410
<p>I think you mean to say "statically typed" instead of "strongly typed." Python is strongly typed. You just don't know what that type is at compile time.</p> <p>With that said, you really need to abandon the idea that you're going to find any IDEs that work as well for Python as they do for Java or C#. Python's d...
2
2009-08-18T11:58:57Z
[ "python", "eclipse", "list", "strong-typing" ]
Is there any way to format a complete python buffer in emacs with a key press?
1,288,474
<p>I am looking for any way to have Emacs format a Python buffer by hitting a few keys. By format, I mean:</p> <ol> <li>Replace tabs with 4 spaces</li> <li>Wrap all long lines correctly at 79 chars. This includes wrapping &amp; concatenating long strings, wrapping long comments, wrapping lists, function headers, etc...
2
2009-08-17T15:02:05Z
1,288,546
<p>To change tabs into spaces and fill comments at the same time, you can use this command:</p> <pre><code>(defun my-format-python-text () "untabify and wrap python comments" (interactive) (untabify (point-min) (point-max)) (goto-char (point-min)) (while (re-search-forward comment-start nil t) (call-inte...
4
2009-08-17T15:15:59Z
[ "python", "emacs", "formatter" ]
Is there any way to format a complete python buffer in emacs with a key press?
1,288,474
<p>I am looking for any way to have Emacs format a Python buffer by hitting a few keys. By format, I mean:</p> <ol> <li>Replace tabs with 4 spaces</li> <li>Wrap all long lines correctly at 79 chars. This includes wrapping &amp; concatenating long strings, wrapping long comments, wrapping lists, function headers, etc...
2
2009-08-17T15:02:05Z
1,289,044
<p>About your point 3:</p> <blockquote> <p>Unrelated, but when I hit enter, it'd be nice if the cursor was tabbed in automatically.</p> </blockquote> <p>My emacs Python mode does this by default, apparently. It's simply called <a href="https://launchpad.net/python-mode/" rel="nofollow">python-mode</a>...</p>
1
2009-08-17T16:36:43Z
[ "python", "emacs", "formatter" ]
Is there any way to format a complete python buffer in emacs with a key press?
1,288,474
<p>I am looking for any way to have Emacs format a Python buffer by hitting a few keys. By format, I mean:</p> <ol> <li>Replace tabs with 4 spaces</li> <li>Wrap all long lines correctly at 79 chars. This includes wrapping &amp; concatenating long strings, wrapping long comments, wrapping lists, function headers, etc...
2
2009-08-17T15:02:05Z
1,296,939
<p>The best way to avoid tab characters in your file is to not insert them at all. The following statement will always insert the correct number of spaces, rather than inserting tab characters</p> <pre><code>;; Turn off tab insertion in favor of space insertion (setq-default indent-tabs-mode nil) </code></pre> <p>I ...
0
2009-08-18T22:34:02Z
[ "python", "emacs", "formatter" ]
Using the same decorator (with arguments) with functions and methods
1,288,498
<p>I have been trying to create a decorator that can be used with both functions and methods in python. This on it's own is not that hard, but when creating a decorator that takes arguments, it seems to be.</p> <pre><code>class methods(object): def __init__(self, *_methods): self.methods = _methods d...
11
2009-08-17T15:08:12Z
1,288,547
<p>The decorator is always applied to a function object -- have the decorator <code>print</code> the type of its argument and you'll be able to confirm that; and it should generally return a function object, too (which is already a decorator with the proper <code>__get__</code>!-) although there are exceptions to the l...
4
2009-08-17T15:16:03Z
[ "python", "function", "methods", "arguments", "decorator" ]
Using the same decorator (with arguments) with functions and methods
1,288,498
<p>I have been trying to create a decorator that can be used with both functions and methods in python. This on it's own is not that hard, but when creating a decorator that takes arguments, it seems to be.</p> <pre><code>class methods(object): def __init__(self, *_methods): self.methods = _methods d...
11
2009-08-17T15:08:12Z
1,288,581
<p>Since you're already defining a <code>__get__</code> to use your decorator on the Bound Method, you could pass a flag telling it if it's being used on a method or function.</p> <pre><code>class methods(object): def __init__(self, *_methods, called_on_method=False): self.methods = _methods self.c...
4
2009-08-17T15:24:01Z
[ "python", "function", "methods", "arguments", "decorator" ]
Using the same decorator (with arguments) with functions and methods
1,288,498
<p>I have been trying to create a decorator that can be used with both functions and methods in python. This on it's own is not that hard, but when creating a decorator that takes arguments, it seems to be.</p> <pre><code>class methods(object): def __init__(self, *_methods): self.methods = _methods d...
11
2009-08-17T15:08:12Z
1,288,600
<p>A partial (specific) solution I have come up with relies on exception handling. I am attempting to create a decorator to only allow certain HttpRequest methods, but make it work with both functions that are views, and methods that are views.</p> <p>So, this class will do what I want:</p> <pre><code>class methods(...
1
2009-08-17T15:28:51Z
[ "python", "function", "methods", "arguments", "decorator" ]
Using the same decorator (with arguments) with functions and methods
1,288,498
<p>I have been trying to create a decorator that can be used with both functions and methods in python. This on it's own is not that hard, but when creating a decorator that takes arguments, it seems to be.</p> <pre><code>class methods(object): def __init__(self, *_methods): self.methods = _methods d...
11
2009-08-17T15:08:12Z
1,288,936
<p>To expand on the <code>__get__</code> approach. This can be generalized into a decorator decorator.</p> <pre><code>class _MethodDecoratorAdaptor(object): def __init__(self, decorator, func): self.decorator = decorator self.func = func def __call__(self, *args, **kwargs): return self....
9
2009-08-17T16:15:33Z
[ "python", "function", "methods", "arguments", "decorator" ]
Python Pyme: Simple decryption without user interaction
1,288,959
<p>I am using Pyme to interface with GPGME and have had no problems signing / encrypting. When I try to decrypt, however, it always brings up the prompt for the passphrase despite having set it via a <code>c.set_passphrase_cb</code> callback. Am I doing something wrong?</p>
0
2009-08-17T16:19:45Z
1,836,491
<p>I have a similar problem. My code looks like this:</p> <p>def passphrase_callback(hint='', desc='', prev_bad=''): return 'password'</p> <p>class CryptoEngine: class NoSignKeys(Exception): def <strong>init</strong>(self, str): Exception.<strong>init</strong>(self, str)</p> <pre><code>de...
1
2009-12-02T23:02:27Z
[ "python", "gnupg", "pyme", "gpgme" ]
Python Pyme: Simple decryption without user interaction
1,288,959
<p>I am using Pyme to interface with GPGME and have had no problems signing / encrypting. When I try to decrypt, however, it always brings up the prompt for the passphrase despite having set it via a <code>c.set_passphrase_cb</code> callback. Am I doing something wrong?</p>
0
2009-08-17T16:19:45Z
2,618,041
<p>Add "c.set_armor(1)" before you set the passphrase callback.</p>
0
2010-04-11T17:53:03Z
[ "python", "gnupg", "pyme", "gpgme" ]
Python equivalent of Jstack?
1,289,124
<p>Is there a python equivalent of jstack? I've got a hung process and I really want to see what it's up to because I have yet to reproduce the defect in development.</p>
5
2009-08-17T16:51:32Z
1,289,185
<p><a href="http://wiki.python.org/moin/DebuggingWithGdb" rel="nofollow">Python GDB</a></p>
4
2009-08-17T17:03:03Z
[ "python" ]
Drawing braces with Pyx
1,289,681
<p>How can I draw a “braced” line between two arbitrary points with Pyx?</p> <p>It would look something like this:</p> <p><img src="http://tof.canardpc.com/view/d16770a8-0fc6-4e9d-b43c-a11eaa09304d" alt="Brace example" title="" /></p>
1
2009-08-17T18:42:36Z
1,290,110
<p>You can draw pretty braces using <a href="http://en.wikipedia.org/wiki/Sigmoid%5Ffunction">sigmoidals</a>. I don't have Pyx installed so I'll just plot these using matplotlib (pylab here). Here <code>beta</code> controls the sharpness of the curves in the braces.</p> <pre><code>import numpy as nx import pylab as ...
9
2009-08-17T20:03:46Z
[ "python", "drawing", "pyx" ]
Drawing braces with Pyx
1,289,681
<p>How can I draw a “braced” line between two arbitrary points with Pyx?</p> <p>It would look something like this:</p> <p><img src="http://tof.canardpc.com/view/d16770a8-0fc6-4e9d-b43c-a11eaa09304d" alt="Brace example" title="" /></p>
1
2009-08-17T18:42:36Z
20,308,475
<p>tom10 provides a good solution, but could use some improvement.<br> The key is creates a brace over the range [0,1],[0,1] and then scale it.<br> This version also lets you tweak the shape a bit. For bonus points, it uses the second derivative to figure out how densely to space the points.</p> <p><code>mid</code> se...
1
2013-12-01T03:42:25Z
[ "python", "drawing", "pyx" ]
python multiprocessing vs threading for cpu bound work on windows and linux
1,289,813
<p>So I knocked up some test code to see how the multiprocessing module would scale on cpu bound work compared to threading. On linux I get the performance increase that I'd expect:</p> <pre>linux (dual quad core xeon): serialrun took 1192.319 ms parallelrun took 346.727 ms threadedrun took 2108.172 ms </pre> <p>My d...
25
2009-08-17T19:04:58Z
1,289,843
<p>It's been said that creating processes on Windows is more expensive than on linux. If you search around the site you will find some information. Here's <a href="http://stackoverflow.com/questions/47845/why-is-creating-a-new-process-more-expensive-on-windows-than-linux">one</a> I found easily.</p>
4
2009-08-17T19:12:49Z
[ "python", "multiprocessing" ]
python multiprocessing vs threading for cpu bound work on windows and linux
1,289,813
<p>So I knocked up some test code to see how the multiprocessing module would scale on cpu bound work compared to threading. On linux I get the performance increase that I'd expect:</p> <pre>linux (dual quad core xeon): serialrun took 1192.319 ms parallelrun took 346.727 ms threadedrun took 2108.172 ms </pre> <p>My d...
25
2009-08-17T19:04:58Z
1,289,849
<p>Processes are much more lightweight under UNIX variants. Windows processes are heavy and take much more time to start up. Threads are the recommended way of doing multiprocessing on windows.</p>
12
2009-08-17T19:13:25Z
[ "python", "multiprocessing" ]
python multiprocessing vs threading for cpu bound work on windows and linux
1,289,813
<p>So I knocked up some test code to see how the multiprocessing module would scale on cpu bound work compared to threading. On linux I get the performance increase that I'd expect:</p> <pre>linux (dual quad core xeon): serialrun took 1192.319 ms parallelrun took 346.727 ms threadedrun took 2108.172 ms </pre> <p>My d...
25
2009-08-17T19:04:58Z
1,289,875
<p>The <a href="http://docs.python.org/library/multiprocessing.html">python documentation for multiprocessing</a> blames the lack of os.fork() for the problems in Windows. It may be applicable here.</p> <p>See what happens when you import psyco. First, easy_install it:</p> <pre><code>C:\Users\hughdbrown&gt;\Python26\...
23
2009-08-17T19:20:24Z
[ "python", "multiprocessing" ]
python multiprocessing vs threading for cpu bound work on windows and linux
1,289,813
<p>So I knocked up some test code to see how the multiprocessing module would scale on cpu bound work compared to threading. On linux I get the performance increase that I'd expect:</p> <pre>linux (dual quad core xeon): serialrun took 1192.319 ms parallelrun took 346.727 ms threadedrun took 2108.172 ms </pre> <p>My d...
25
2009-08-17T19:04:58Z
1,289,977
<p>Currently, your counter() function is not modifying much state. Try changing counter() so that it modifies many pages of memory. Then run a cpu bound loop. See if there is still a large disparity between linux and windows.</p> <p>I'm not running python 2.6 right now, so I can't try it myself.</p>
1
2009-08-17T19:40:26Z
[ "python", "multiprocessing" ]
python multiprocessing vs threading for cpu bound work on windows and linux
1,289,813
<p>So I knocked up some test code to see how the multiprocessing module would scale on cpu bound work compared to threading. On linux I get the performance increase that I'd expect:</p> <pre>linux (dual quad core xeon): serialrun took 1192.319 ms parallelrun took 346.727 ms threadedrun took 2108.172 ms </pre> <p>My d...
25
2009-08-17T19:04:58Z
26,808,887
<p>Just starting the pool takes a long time. I have found in 'real world' programs if I can keep a pool open and reuse it for many different processes,passing the reference down through method calls (usually using map.async) then on Linux I can save a few percent but on Windows I can often halve the time taken. Linux i...
1
2014-11-07T19:43:23Z
[ "python", "multiprocessing" ]
How do I mock an open used in a with statement (using the Mock framework in Python)?
1,289,894
<p>How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by <a href="http://www.voidspace.org.uk/python/mock/">Michael Foord's Mock framework</a>):</p> <pre><code>def testme(filepath): with open(filepath, 'r') as f: return f.read() </code></pre>
80
2009-08-17T19:26:50Z
3,268,310
<p>Updated Daryl's answer to fix changes to Mock class.</p> <pre><code>@patch('__builtin__.open') def test_testme(self, open_mock): # # setup # context_manager_mock = Mock() open_mock.return_value = context_manager_mock file_mock = Mock() file_mock.read.return_value = sentinel.file_contents...
27
2010-07-16T19:42:53Z
[ "python", "mocking", "with-statement" ]
How do I mock an open used in a with statement (using the Mock framework in Python)?
1,289,894
<p>How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by <a href="http://www.voidspace.org.uk/python/mock/">Michael Foord's Mock framework</a>):</p> <pre><code>def testme(filepath): with open(filepath, 'r') as f: return f.read() </code></pre>
80
2009-08-17T19:26:50Z
6,112,456
<p>The way to do this has changed in mock 0.7.0 which finally supports mocking the python protocol methods (magic methods), particularly using the MagicMock:</p> <p><a href="http://www.voidspace.org.uk/python/mock/magicmock.html">http://www.voidspace.org.uk/python/mock/magicmock.html</a></p> <p>An example of mocking ...
89
2011-05-24T14:56:44Z
[ "python", "mocking", "with-statement" ]
How do I mock an open used in a with statement (using the Mock framework in Python)?
1,289,894
<p>How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by <a href="http://www.voidspace.org.uk/python/mock/">Michael Foord's Mock framework</a>):</p> <pre><code>def testme(filepath): with open(filepath, 'r') as f: return f.read() </code></pre>
80
2009-08-17T19:26:50Z
19,146,253
<p>With the latest versions of mock, you can use the really useful <a href="http://www.voidspace.org.uk/python/mock/helpers.html#mock-open">mock_open</a> helper:</p> <blockquote> <p><strong>mock_open(mock=None, read_data=None)</strong> </p> <p>A helper function to create a mock to replace the use of open. It ...
45
2013-10-02T20:28:51Z
[ "python", "mocking", "with-statement" ]
How do I mock an open used in a with statement (using the Mock framework in Python)?
1,289,894
<p>How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by <a href="http://www.voidspace.org.uk/python/mock/">Michael Foord's Mock framework</a>):</p> <pre><code>def testme(filepath): with open(filepath, 'r') as f: return f.read() </code></pre>
80
2009-08-17T19:26:50Z
27,429,755
<p>To use <a href="https://docs.python.org/3.3/library/unittest.mock.html#mock-open">mock_open</a> for a simple file <code>read()</code> (the original mock_open snippet <a href="http://stackoverflow.com/a/19146253/552793">already given on this page</a> is geared more for write):</p> <pre><code>my_text = "some text to ...
7
2014-12-11T18:19:22Z
[ "python", "mocking", "with-statement" ]
How do I mock an open used in a with statement (using the Mock framework in Python)?
1,289,894
<p>How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by <a href="http://www.voidspace.org.uk/python/mock/">Michael Foord's Mock framework</a>):</p> <pre><code>def testme(filepath): with open(filepath, 'r') as f: return f.read() </code></pre>
80
2009-08-17T19:26:50Z
32,768,540
<p>I might be a bit late to the game, but this worked for me when calling <code>open</code> in another module without having to create a new file.</p> <p>test.py</p> <pre><code>import unittest from mock import Mock, patch, mock_open from MyObj import MyObj class TestObj(unittest.TestCase): open_ = mock_open() ...
2
2015-09-24T18:35:29Z
[ "python", "mocking", "with-statement" ]
How do I mock an open used in a with statement (using the Mock framework in Python)?
1,289,894
<p>How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by <a href="http://www.voidspace.org.uk/python/mock/">Michael Foord's Mock framework</a>):</p> <pre><code>def testme(filepath): with open(filepath, 'r') as f: return f.read() </code></pre>
80
2009-08-17T19:26:50Z
34,677,735
<p>There is lot of noise in these answers, almost all are correct but outdated and not neat. <a href="https://docs.python.org/3/library/unittest.mock.html#mock-open" rel="nofollow"><code>mock_open</code></a> is part of <a href="https://docs.python.org/3/library/unittest.mock.html#module-unittest.mock" rel="nofollow"><c...
19
2016-01-08T13:00:13Z
[ "python", "mocking", "with-statement" ]
How do I mock an open used in a with statement (using the Mock framework in Python)?
1,289,894
<p>How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by <a href="http://www.voidspace.org.uk/python/mock/">Michael Foord's Mock framework</a>):</p> <pre><code>def testme(filepath): with open(filepath, 'r') as f: return f.read() </code></pre>
80
2009-08-17T19:26:50Z
38,062,933
<p>The top answer is useful but I expanded on it a bit.</p> <p>If you want to set the value of your file object (the <code>f</code> in <code>as f</code>) based on the arguments passed to <code>open()</code> here's one way to do it:</p> <pre><code>def save_arg_return_data(*args, **kwargs): mm = MagicMock(spec=file...
0
2016-06-27T20:45:53Z
[ "python", "mocking", "with-statement" ]
Web Application Frameworks: C++ vs Python
1,289,941
<p>I am familiar with both Python and C++ as a programmer. I was thinking of writing my own simple web application and I wanted to know which language would be more appropriate for server-side web development.</p> <p>Some things I'm looking for:</p> <ul> <li>It has to be intuitive. I recognize that Wt exists and it f...
8
2009-08-17T19:33:40Z
1,289,978
<p>If you'd like to avoid writing HTML, you could try <a href="http://code.google.com/webtoolkit/" rel="nofollow">GWT</a>. However, in my experience, using an intermediate framework to generate HTML and ECMAScript never works anywhere near as well as hand-writing the pages.</p> <p>[edit] nikow mentions in the comments...
7
2009-08-17T19:40:27Z
[ "c++", "python", "wt" ]
Web Application Frameworks: C++ vs Python
1,289,941
<p>I am familiar with both Python and C++ as a programmer. I was thinking of writing my own simple web application and I wanted to know which language would be more appropriate for server-side web development.</p> <p>Some things I'm looking for:</p> <ul> <li>It has to be intuitive. I recognize that Wt exists and it f...
8
2009-08-17T19:33:40Z
1,290,022
<p>The only reason you might want to use C++ over Python is when speed is paramount.</p> <p>If this is going to be your first web-app, you'll probably be ok with just Python, and your development speed will be orders of magnitude better than with CPP.</p> <p>Django's templating language is far from powerless, to me i...
0
2009-08-17T19:46:48Z
[ "c++", "python", "wt" ]
Web Application Frameworks: C++ vs Python
1,289,941
<p>I am familiar with both Python and C++ as a programmer. I was thinking of writing my own simple web application and I wanted to know which language would be more appropriate for server-side web development.</p> <p>Some things I'm looking for:</p> <ul> <li>It has to be intuitive. I recognize that Wt exists and it f...
8
2009-08-17T19:33:40Z
1,290,058
<ul> <li>Django is good point to start web development it is great framework</li> <li>If you look for C++ take a look on <a href="http://cppcms.sourceforge.net/">CppCMS</a>, it is much more close to Django, it is not like Wt that mimics Qt.</li> </ul> <p>In any case, it is really depends on your needs. C++ can be used...
7
2009-08-17T19:54:21Z
[ "c++", "python", "wt" ]
Web Application Frameworks: C++ vs Python
1,289,941
<p>I am familiar with both Python and C++ as a programmer. I was thinking of writing my own simple web application and I wanted to know which language would be more appropriate for server-side web development.</p> <p>Some things I'm looking for:</p> <ul> <li>It has to be intuitive. I recognize that Wt exists and it f...
8
2009-08-17T19:33:40Z
1,290,620
<p>I think you better go firt python in your case, meanwhile you can extend cppCMS functionalities and write your own framework arround it.</p> <p>wt was a good idea design, but somehow not that suitable.</p>
1
2009-08-17T21:40:03Z
[ "c++", "python", "wt" ]
Web Application Frameworks: C++ vs Python
1,289,941
<p>I am familiar with both Python and C++ as a programmer. I was thinking of writing my own simple web application and I wanted to know which language would be more appropriate for server-side web development.</p> <p>Some things I'm looking for:</p> <ul> <li>It has to be intuitive. I recognize that Wt exists and it f...
8
2009-08-17T19:33:40Z
1,290,625
<p>If you are exploring Python frameworks (based on the excepted answer I think you are) I think you really owe it to yourself to check out <a href="http://cherrypy.org/" rel="nofollow">CherryPy</a>. When you write CherryPy apps, you really are just writing Python apps. The framework gets out of your way in a real hurr...
1
2009-08-17T21:40:57Z
[ "c++", "python", "wt" ]
Web Application Frameworks: C++ vs Python
1,289,941
<p>I am familiar with both Python and C++ as a programmer. I was thinking of writing my own simple web application and I wanted to know which language would be more appropriate for server-side web development.</p> <p>Some things I'm looking for:</p> <ul> <li>It has to be intuitive. I recognize that Wt exists and it f...
8
2009-08-17T19:33:40Z
1,475,164
<p>Having looked several ones, like django, pylos, web2py, wt. My recommendation is web2py. It's a python version of "ruby on rails" and easy to learn. </p>
0
2009-09-25T02:39:51Z
[ "c++", "python", "wt" ]
Web Application Frameworks: C++ vs Python
1,289,941
<p>I am familiar with both Python and C++ as a programmer. I was thinking of writing my own simple web application and I wanted to know which language would be more appropriate for server-side web development.</p> <p>Some things I'm looking for:</p> <ul> <li>It has to be intuitive. I recognize that Wt exists and it f...
8
2009-08-17T19:33:40Z
11,300,548
<p>I would go with Wt because:</p> <ul> <li>You already know C++</li> <li>It has a nice <a href="http://www.webtoolkit.eu/widgets/style-and-layout/wboxlayout">layout system</a>, so you don't need to know lots of HTML</li> <li>It is very well written and a pleasure to code in</li> <li>Your deployed apps will handle 50 ...
10
2012-07-02T20:14:47Z
[ "c++", "python", "wt" ]
How do I split different applications across multiple tcp ports on one site?
1,289,953
<p>I have a series of applications which use one model and are all under one site. Essentially a mix of the main website, and public and private api's. Is there a way to make different DJango apps use a different tcp port? I have not been able to find anything in the documentation about it.</p>
0
2009-08-17T19:35:33Z
1,290,017
<p><a href="http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/" rel="nofollow">Django docs</a>. Optionally, use Apache to setup a subdomain for each application so you don't have to remember all the ports.</p>
0
2009-08-17T19:45:49Z
[ "python", "django" ]
How can I capture the error output from the ipython shell?
1,289,971
<p>I'm writing an ipython macro that processes the output of a program. The thing is, the program can sometimes write to <code>stderr</code> , so if I do something like this :</p> <pre><code> out = !my_program </code></pre> <p>the <code>out</code> variable will not contain the output. I think it will contain the exit...
0
2009-08-17T19:38:43Z
1,290,902
<p>foo 2>&amp;1 means redirect all of the output, including handle 2 (that is, STDERR), from the foo command to handle 1 (that is, STDOUT)<br /> so here out = !foo 2>&amp;1 maybe good enough. below is the demo:<br /> egg.py: </p> <pre><code>#!/usr/bin/env python # -*- coding: utf8 -*- def main(): print 'hello' ...
4
2009-08-17T22:43:24Z
[ "python", "ipython" ]
python: Overhead to using classes instead of dictionaries?
1,290,030
<p>First, I'd like to point out that I know OOP concepts and understand the differences between dictionaries and classes. My question is about what makes sense design wise in this case:</p> <p>I am designing a webapp in python and I have to represent something like a book object. Books have chapters and chapters have ...
5
2009-08-17T19:47:44Z
1,290,044
<p>Objects were originally created to bundle data with functionality. If you just want to store data, use dictionaries. If you want to include methods for manipulating the data, use objects.</p>
4
2009-08-17T19:51:00Z
[ "python", "design", "class", "dictionary" ]
python: Overhead to using classes instead of dictionaries?
1,290,030
<p>First, I'd like to point out that I know OOP concepts and understand the differences between dictionaries and classes. My question is about what makes sense design wise in this case:</p> <p>I am designing a webapp in python and I have to represent something like a book object. Books have chapters and chapters have ...
5
2009-08-17T19:47:44Z
1,290,060
<p>A few thoughts:</p> <ol> <li><p>If you start with a dictionary, you can always switch to a custom class later that implements the mapping protocol (or subclasses dict). So it's probably a good starting point.</p></li> <li><p>You can define custom Python objects to use <code>__slots__</code>, which will be faster a...
8
2009-08-17T19:54:33Z
[ "python", "design", "class", "dictionary" ]
python: Overhead to using classes instead of dictionaries?
1,290,030
<p>First, I'd like to point out that I know OOP concepts and understand the differences between dictionaries and classes. My question is about what makes sense design wise in this case:</p> <p>I am designing a webapp in python and I have to represent something like a book object. Books have chapters and chapters have ...
5
2009-08-17T19:47:44Z
1,290,087
<p>Kind of similar to your problem: <a href="http://stackoverflow.com/questions/794132/returning-an-object-vs-returning-a-tuple">http://stackoverflow.com/questions/794132/returning-an-object-vs-returning-a-tuple</a></p> <p>There's no clearcut choice. Dictionaries are fast, nice, and clean. Class instances are, after a...
1
2009-08-17T19:58:51Z
[ "python", "design", "class", "dictionary" ]
python: Overhead to using classes instead of dictionaries?
1,290,030
<p>First, I'd like to point out that I know OOP concepts and understand the differences between dictionaries and classes. My question is about what makes sense design wise in this case:</p> <p>I am designing a webapp in python and I have to represent something like a book object. Books have chapters and chapters have ...
5
2009-08-17T19:47:44Z
1,290,102
<p>If you go with objects, I wouldn't store the data pickled in the database simply for the reasons you gave. It would be considerably worse if you underwent a change of language or similar.</p> <p>FWIW, I would start with a dictionary. If things get complicated or new features are needed, make it an object.</p>
1
2009-08-17T20:01:30Z
[ "python", "design", "class", "dictionary" ]
python: Overhead to using classes instead of dictionaries?
1,290,030
<p>First, I'd like to point out that I know OOP concepts and understand the differences between dictionaries and classes. My question is about what makes sense design wise in this case:</p> <p>I am designing a webapp in python and I have to represent something like a book object. Books have chapters and chapters have ...
5
2009-08-17T19:47:44Z
1,290,132
<p>This is <em>highly</em> unlikely to be the bottleneck in your application, so worrying about the performance is probably a waste of time. However, if this is the bottleneck or you just have some time on your hands, look into using a <a href="http://docs.python.org/dev/library/collections.html#namedtuple-factory-func...
9
2009-08-17T20:07:31Z
[ "python", "design", "class", "dictionary" ]
python: Overhead to using classes instead of dictionaries?
1,290,030
<p>First, I'd like to point out that I know OOP concepts and understand the differences between dictionaries and classes. My question is about what makes sense design wise in this case:</p> <p>I am designing a webapp in python and I have to represent something like a book object. Books have chapters and chapters have ...
5
2009-08-17T19:47:44Z
8,500,864
<p>You actually summarize the trade-offs quite well. It seems lots of people get worried far too early about performance. Rather than repeat the standard advice on the subject I'll suggest you search the web for "Knuth premature optimization". The fact of the matter is that for objects of <em>known structure</em> you w...
4
2011-12-14T07:32:15Z
[ "python", "design", "class", "dictionary" ]
Python urllib2 URLError exception?
1,290,142
<p>I installed Python 2.6.2 earlier on a Windows XP machine and run the following code:</p> <pre><code>import urllib2 import urllib page = urllib2.Request('http://www.python.org/fish.html') urllib2.urlopen( page ) </code></pre> <p>I get the following error.</p> <pre><code>Traceback (most recent call last):&lt;br&gt...
1
2009-08-17T20:10:37Z
1,290,165
<pre><code>import urllib2 response = urllib2.urlopen('http://www.python.org/fish.html') html = response.read() </code></pre> <p>You're doing it wrong.</p>
4
2009-08-17T20:13:51Z
[ "python", "networking", "urllib2" ]
Python urllib2 URLError exception?
1,290,142
<p>I installed Python 2.6.2 earlier on a Windows XP machine and run the following code:</p> <pre><code>import urllib2 import urllib page = urllib2.Request('http://www.python.org/fish.html') urllib2.urlopen( page ) </code></pre> <p>I get the following error.</p> <pre><code>Traceback (most recent call last):&lt;br&gt...
1
2009-08-17T20:10:37Z
1,290,559
<p>Windows Vista, python 2.6.2</p> <p>It's a 404 page, right?</p> <pre><code>&gt;&gt;&gt; import urllib2 &gt;&gt;&gt; import urllib &gt;&gt;&gt; &gt;&gt;&gt; page = urllib2.Request('http://www.python.org/fish.html') &gt;&gt;&gt; urllib2.urlopen( page ) Traceback (most recent call last): File "&lt;stdin&gt;", line 1...
1
2009-08-17T21:28:15Z
[ "python", "networking", "urllib2" ]
Python urllib2 URLError exception?
1,290,142
<p>I installed Python 2.6.2 earlier on a Windows XP machine and run the following code:</p> <pre><code>import urllib2 import urllib page = urllib2.Request('http://www.python.org/fish.html') urllib2.urlopen( page ) </code></pre> <p>I get the following error.</p> <pre><code>Traceback (most recent call last):&lt;br&gt...
1
2009-08-17T20:10:37Z
1,290,632
<p>DJ</p> <p>First, I see no reason to import urllib; I've only ever seen urllib2 used to replace urllib entirely and I know of no functionality that's useful from urllib and yet is missing from urllib2.</p> <p>Next, I notice that <a href="http://www.python.org/fish.html" rel="nofollow">http://www.python.org/fish.htm...
0
2009-08-17T21:42:05Z
[ "python", "networking", "urllib2" ]
Python urllib2 URLError exception?
1,290,142
<p>I installed Python 2.6.2 earlier on a Windows XP machine and run the following code:</p> <pre><code>import urllib2 import urllib page = urllib2.Request('http://www.python.org/fish.html') urllib2.urlopen( page ) </code></pre> <p>I get the following error.</p> <pre><code>Traceback (most recent call last):&lt;br&gt...
1
2009-08-17T20:10:37Z
1,292,261
<p>Have a look in the urllib2 source, at the line specified by the traceback:</p> <pre><code>File "C:\Python26\lib\urllib2.py", line 1105, in do_open raise URLError(err) </code></pre> <p>There you'll see the following fragment:</p> <pre><code> try: h.request(req.get_method(), req.get_selector(), req.data,...
3
2009-08-18T07:09:08Z
[ "python", "networking", "urllib2" ]
Python urllib2 URLError exception?
1,290,142
<p>I installed Python 2.6.2 earlier on a Windows XP machine and run the following code:</p> <pre><code>import urllib2 import urllib page = urllib2.Request('http://www.python.org/fish.html') urllib2.urlopen( page ) </code></pre> <p>I get the following error.</p> <pre><code>Traceback (most recent call last):&lt;br&gt...
1
2009-08-17T20:10:37Z
13,222,052
<p>Name resolution error.</p> <p><code>getaddrinfo</code> is used to resolve the hostname (<code>python.org</code>)in your request. If it fails, it means that the name could not be resolved because:</p> <ol> <li>It does not exist, or the records are outdated (unlikely; python.org is a well-established domain name)</l...
1
2012-11-04T19:30:39Z
[ "python", "networking", "urllib2" ]
converting django ForeignKey to a usable directory name
1,290,202
<p>I'm working on a django app where the user will be able to upload documents of various kinds. The relevant part of my models.py is this:</p> <pre><code>class Materials(models.Model): id = models.AutoField(primary_key=True) id_presentations = models.ForeignKey(Presentations, db_column='id_Presentations', re...
2
2009-08-17T20:22:35Z
1,290,252
<p>Provided that the "name" property is defined on your Presentation model, if you're working with a specific instance of the model then what you want should work. Like this:</p> <pre><code>from models import Materials obj = Materials.objects.get([some criteria here]) name = obj.id_presentation.name </code></pre> <p>...
0
2009-08-17T20:32:00Z
[ "python", "django", "django-models" ]
converting django ForeignKey to a usable directory name
1,290,202
<p>I'm working on a django app where the user will be able to upload documents of various kinds. The relevant part of my models.py is this:</p> <pre><code>class Materials(models.Model): id = models.AutoField(primary_key=True) id_presentations = models.ForeignKey(Presentations, db_column='id_Presentations', re...
2
2009-08-17T20:22:35Z
1,290,536
<p>Note: you can always find out what attributes and methods are available on an object in python by calling dir(object).</p>
0
2009-08-17T21:24:22Z
[ "python", "django", "django-models" ]
converting django ForeignKey to a usable directory name
1,290,202
<p>I'm working on a django app where the user will be able to upload documents of various kinds. The relevant part of my models.py is this:</p> <pre><code>class Materials(models.Model): id = models.AutoField(primary_key=True) id_presentations = models.ForeignKey(Presentations, db_column='id_Presentations', re...
2
2009-08-17T20:22:35Z
1,290,713
<p>As of Django 1.0, the upload_to argument to FileFields can be a callable. If I'm understanding your intentions correctly, something like this should do the trick:</p> <pre><code>def material_path(instance, filename): return 'documents/%d' % instance.id_presentations.id class Materials(models.Model): id_pr...
2
2009-08-17T21:58:51Z
[ "python", "django", "django-models" ]
python regex help: unknown information to skip
1,290,205
<p>I'm having trouble with the needed regular expression... I'm sure I need to probably be using some combination of 'lookaround' or conditional expressions, but I'm at a loss.</p> <p>I have a data string like:</p> <pre><code>pattern1 pattern2 pattern3 unwanted-groups pattern4 random number of tokens pattern5 optiona...
0
2009-08-17T20:23:00Z
1,290,238
<p>You need to use the <code>|</code> operator and findall:</p> <pre><code>&gt;&gt;&gt; re.compile("(regex\d+|optregex\d+)") &gt;&gt;&gt; regex.findall(string) [u'regex1', u'regex2', u'regex3', u'regex4', u'regex5', u'optregex1', u'optregex2', u'optregex3', u'optregex4'] </code></pre> <p>An advice: there are several ...
1
2009-08-17T20:30:25Z
[ "python", "regex" ]
python regex help: unknown information to skip
1,290,205
<p>I'm having trouble with the needed regular expression... I'm sure I need to probably be using some combination of 'lookaround' or conditional expressions, but I'm at a loss.</p> <p>I have a data string like:</p> <pre><code>pattern1 pattern2 pattern3 unwanted-groups pattern4 random number of tokens pattern5 optiona...
0
2009-08-17T20:23:00Z
1,290,262
<p>If all the data is in that format I'd go with <code>split</code> instead. I think it will be faster.</p> <pre><code> str = "regex1 regex2 regex3 unwanted-regex regex4 random number of tokens regex5 optregex1 optregex2 more unknown unwanted junk separated with white spaces optregex3 optregex4 etc" parts = str.split(...
4
2009-08-17T20:34:14Z
[ "python", "regex" ]
python regex help: unknown information to skip
1,290,205
<p>I'm having trouble with the needed regular expression... I'm sure I need to probably be using some combination of 'lookaround' or conditional expressions, but I'm at a loss.</p> <p>I have a data string like:</p> <pre><code>pattern1 pattern2 pattern3 unwanted-groups pattern4 random number of tokens pattern5 optiona...
0
2009-08-17T20:23:00Z
1,291,242
<p>If all of your targets consist of things like "foo1", "bar22" etc (in other words a sequence of letters followed by a sequence of digits) and everything else (sequences of digits, "words" without numeric suffixes, etc) is "junk" then the following seems to be sufficient:</p> <pre><code>re.findall(r'[A-Za-z]+\d+', t...
0
2009-08-18T00:39:12Z
[ "python", "regex" ]
OpenGL Picking with Pyglet
1,290,270
<p>I'm trying to implement picking using Pyglet's OpenGL wrapper, but I'm having trouble converting a <a href="http://www.lighthouse3d.com/opengl/picking/" rel="nofollow">C tutorial</a> to Python. Specifically the part below. </p> <pre>#define BUFSIZE 512 GLuint selectBuf[BUFSIZE] void startPicking(int cursorX, int c...
2
2009-08-17T20:35:32Z
1,290,943
<p>I've had good luck with PyOpenGL. </p> <p><a href="http://pyopengl.sourceforge.net/" rel="nofollow">http://pyopengl.sourceforge.net/</a></p> <p>It's pretty straightforward, and using a C tutorial would be easier with it, I believe. </p>
1
2009-08-17T22:57:32Z
[ "python", "opengl", "pyglet", "picking" ]
OpenGL Picking with Pyglet
1,290,270
<p>I'm trying to implement picking using Pyglet's OpenGL wrapper, but I'm having trouble converting a <a href="http://www.lighthouse3d.com/opengl/picking/" rel="nofollow">C tutorial</a> to Python. Specifically the part below. </p> <pre>#define BUFSIZE 512 GLuint selectBuf[BUFSIZE] void startPicking(int cursorX, int c...
2
2009-08-17T20:35:32Z
1,363,153
<p>Exactly what sort of trouble are you having? Pyglet's OpenGL implementation is a thin wrapper over the DLL and pretty much maps the C calls one-for-one. It's hard to imagine there would be any other library that could be better in terms of following a C tutorial. </p> <p>For example, <a href="http://pyglet.org/doc/...
1
2009-09-01T15:34:28Z
[ "python", "opengl", "pyglet", "picking" ]
OpenGL Picking with Pyglet
1,290,270
<p>I'm trying to implement picking using Pyglet's OpenGL wrapper, but I'm having trouble converting a <a href="http://www.lighthouse3d.com/opengl/picking/" rel="nofollow">C tutorial</a> to Python. Specifically the part below. </p> <pre>#define BUFSIZE 512 GLuint selectBuf[BUFSIZE] void startPicking(int cursorX, int c...
2
2009-08-17T20:35:32Z
1,396,271
<p>I haven't tried your particular example, but the normal way to declare arrays is in the <a href="http://docs.python.org/library/ctypes.html#arrays">ctypes documentation</a>. Essentially you would create an array type like this:</p> <pre><code>FourGLints = GLint * 4 viewport = FourGLints(0, 1, 2, 3) </code></pre>
5
2009-09-08T20:51:33Z
[ "python", "opengl", "pyglet", "picking" ]
Django Admin Inline Change List
1,290,470
<p>I can edit a parent child relationship using the TablularInline and StackedInline classes, however I would prefer to list the child relationships as a change list as there is a lot of information and the forms are too big. Is there an inline change list available in DJango admin or a way or creating one?</p>
4
2009-08-17T21:10:08Z
1,330,208
<p>There's no such functionality built in, but I don't think it would be hard to create your own AdminInline subclass (and an accompanying template for it) that would do this. Just model it off TabularInline, but display fields' data directly instead of rendering form fields.</p>
1
2009-08-25T18:46:08Z
[ "python", "django", "django-admin" ]
Python / ADOX: 'The specified module could not be found.' (win32 extensions)
1,290,472
<p>I'm running pywin32 for python 2.5.</p> <p>I'm following the instructions for python ADO given at <a href="http://www.ecp.cc/pyado.html" rel="nofollow">http://www.ecp.cc/pyado.html</a>.</p> <p>Creating an ADODB.Recordset object works fine. But when I try to create an ADOX.Catalog object I get an error: </p> <pre...
2
2009-08-17T21:10:34Z
1,302,924
<p>Solution: even though ADOX was showing up in the COM browser as an available library, it wasn't "registered" properly. Following the instructions <a href="http://www.accessmonster.com/Uwe/Forum.aspx/access/8356/msadox-dll-registration-problem" rel="nofollow">here</a>, I executed the following at the Start->Run prom...
4
2009-08-19T22:15:34Z
[ "python", "adodb", "pywin32", "adox" ]