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
Fastest way to zero out low values in array?
1,623,849
<p>So, lets say I have 100,000 float arrays with 100 elements each. I need the highest X number of values, BUT only if they are greater than Y. Any element not matching this should be set to 0. What would be the fastest way to do this in Python? Order must be maintained. Most of the elements are already set to 0....
22
2009-10-26T09:24:02Z
1,624,096
<p>Use a <a href="http://www.python.org/doc/2.5.2/lib/module-heapq.html" rel="nofollow">heap</a>.</p> <p>This works in time <code>O(n*lg(HighCountX))</code>.</p> <pre><code>import heapq heap = [] array = [.06, .25, 0, .15, .5, 0, 0, 0.04, 0, 0] highCountX = 3 lowValY = .1 for i in range(1,highCountX): heappush...
0
2009-10-26T10:33:18Z
[ "python", "arrays", "floating-point", "max", "min" ]
Fastest way to zero out low values in array?
1,623,849
<p>So, lets say I have 100,000 float arrays with 100 elements each. I need the highest X number of values, BUT only if they are greater than Y. Any element not matching this should be set to 0. What would be the fastest way to do this in Python? Order must be maintained. Most of the elements are already set to 0....
22
2009-10-26T09:24:02Z
1,624,215
<p>There's a special MaskedArray class in NumPy that does exactly that. You can "mask" elements based on any precondition. This better represent your need than assigning zeroes: numpy operations will ignore masked values when appropriate (for example, finding mean value).</p> <pre><code>&gt;&gt;&gt; from numpy impor...
6
2009-10-26T11:05:35Z
[ "python", "arrays", "floating-point", "max", "min" ]
Fastest way to zero out low values in array?
1,623,849
<p>So, lets say I have 100,000 float arrays with 100 elements each. I need the highest X number of values, BUT only if they are greater than Y. Any element not matching this should be set to 0. What would be the fastest way to do this in Python? Order must be maintained. Most of the elements are already set to 0....
22
2009-10-26T09:24:02Z
1,628,792
<p>Using a heap is a good idea, as egon says. But you can use the <code>heapq.nlargest</code> function to cut down on some effort:</p> <pre><code>import heapq array = [.06, .25, 0, .15, .5, 0, 0, 0.04, 0, 0] highCountX = 3 lowValY = .1 threshold = max(heapq.nlargest(highCountX, array)[-1], lowValY) array = [x if ...
0
2009-10-27T04:32:19Z
[ "python", "arrays", "floating-point", "max", "min" ]
Fastest way to zero out low values in array?
1,623,849
<p>So, lets say I have 100,000 float arrays with 100 elements each. I need the highest X number of values, BUT only if they are greater than Y. Any element not matching this should be set to 0. What would be the fastest way to do this in Python? Order must be maintained. Most of the elements are already set to 0....
22
2009-10-26T09:24:02Z
22,291,243
<pre><code>from scipy.stats import threshold thresholded = threshold(array, 0.5) </code></pre> <p>:)</p>
11
2014-03-10T02:42:55Z
[ "python", "arrays", "floating-point", "max", "min" ]
Problem in importing classes from models file in GAE
1,623,941
<p>I am very new to python and after a brief intro on to python + Google app engine, I've started to work on a pilot project. I have bulkloaded 2 entities UserDetails and PhoneBook with data onto the app engine. Now in my UI I try to first take in the user name then query it to get the user id from UserDetails, then us...
0
2009-10-26T09:49:28Z
1,624,799
<p>The first error you're getting occurs when it can import the module ('models') fine, but can't find the member ('UserDetails') in it. This would generally be the case if you'd mistyped the name of the class, or hadn't defined it in that module.</p> <p>The second error is expected, because you're importing the 'mode...
1
2009-10-26T13:30:13Z
[ "python", "google-app-engine", "gae-datastore" ]
Problem in importing classes from models file in GAE
1,623,941
<p>I am very new to python and after a brief intro on to python + Google app engine, I've started to work on a pilot project. I have bulkloaded 2 entities UserDetails and PhoneBook with data onto the app engine. Now in my UI I try to first take in the user name then query it to get the user id from UserDetails, then us...
0
2009-10-26T09:49:28Z
1,627,860
<p>If models.py is in a subdirectory you might need to add it to the system path, such as:</p> <pre><code>sys.path.append(os.path.join(os.path.dirname(__file__), "lib")) </code></pre> <p>If models is the directory and the classes are in UserDetails.py etc. you may need to:</p> <pre><code>sys.path.append(os.path.join...
0
2009-10-26T23:11:21Z
[ "python", "google-app-engine", "gae-datastore" ]
Python - RegExp - Modify text files
1,624,050
<p>Newbie to Python.... help requested with the following task :-)</p> <p>I have tree of various files, some of them are C source code. I would like to modify these C files with python script.</p> <p>The C code has 4 defines -</p> <pre><code>#define ZR_LOG0(Id, Class, Seveity, Format) #define ZR_LOG1(Id, Class, Seve...
0
2009-10-26T10:20:32Z
1,624,146
<p>A few points:</p> <ul> <li>You can match whitespace with '\s'.</li> <li>The regexp 'capturing groups' are useful here.</li> </ul> <p>So, I would do something like this:</p> <pre><code>output = '' counter = 1 for line in lines: # Match only ZR_LOG lines and capture everything surrounding "Id" match = re.ma...
1
2009-10-26T10:45:50Z
[ "python", "regex" ]
virtualenv confusion
1,624,245
<p>So I open a terminal, cd to my desktop, and run:</p> <p><code>virtualenv test_env</code></p> <p>I then create the following file in my normal environment:</p> <p><code>/home/jesse/.local/lib/python2.6/site-packages/foo_package/__init__.py</code></p> <p>This file contains one line:</p> <p><code>print "importing ...
4
2009-10-26T11:15:57Z
1,624,305
<p>Edit: Post above me is correct, you forgot to activate. Using virtualenvwrapper I've never really done that step so my bad :)</p> <p>Looking at that, it looks like you're doing everything right but I would like to make a suggestion incase you've never heard of it: <a href="http://www.doughellmann.com/projects/virtu...
0
2009-10-26T11:30:56Z
[ "python", "virtualenv" ]
virtualenv confusion
1,624,245
<p>So I open a terminal, cd to my desktop, and run:</p> <p><code>virtualenv test_env</code></p> <p>I then create the following file in my normal environment:</p> <p><code>/home/jesse/.local/lib/python2.6/site-packages/foo_package/__init__.py</code></p> <p>This file contains one line:</p> <p><code>print "importing ...
4
2009-10-26T11:15:57Z
1,624,314
<p>From the steps you mentioned, it seems you haven't activated the virtual env. Do:</p> <pre><code>source bin/activate </code></pre> <p>.. within the virtualenv you created. You should see something like:</p> <pre><code>(test_env)computername:foldername$ </code></pre> <p>Running python from here should get your vi...
2
2009-10-26T11:32:41Z
[ "python", "virtualenv" ]
virtualenv confusion
1,624,245
<p>So I open a terminal, cd to my desktop, and run:</p> <p><code>virtualenv test_env</code></p> <p>I then create the following file in my normal environment:</p> <p><code>/home/jesse/.local/lib/python2.6/site-packages/foo_package/__init__.py</code></p> <p>This file contains one line:</p> <p><code>print "importing ...
4
2009-10-26T11:15:57Z
1,627,737
<p>And you need to create the virtual environment with the option --no-site-packages</p>
0
2009-10-26T22:37:29Z
[ "python", "virtualenv" ]
virtualenv confusion
1,624,245
<p>So I open a terminal, cd to my desktop, and run:</p> <p><code>virtualenv test_env</code></p> <p>I then create the following file in my normal environment:</p> <p><code>/home/jesse/.local/lib/python2.6/site-packages/foo_package/__init__.py</code></p> <p>This file contains one line:</p> <p><code>print "importing ...
4
2009-10-26T11:15:57Z
1,630,805
<p>You're running into a bug in virtualenv. It has not yet been updated to handle .local directories properly. I've filed <a href="http://bitbucket.org/ianb/virtualenv/issue/8/user-local-directory-not-isolated-from" rel="nofollow">an issue for this at the bug tracker</a>.</p> <p><strong>UPDATE</strong>: this bug <a hr...
6
2009-10-27T13:29:09Z
[ "python", "virtualenv" ]
How do I close the stdout-pipe when killing a process started with python subprocess Popen?
1,624,254
<p>I wonder if it is possible to shut down the communication pipe when killing a subprocess started in a different thread. If I do not call communicate() then kill() will work as expected, terminating the process after one second instead of five.</p> <p>I found a discussion of a similar problem <a href="http://bugs.py...
9
2009-10-26T11:18:43Z
1,625,491
<p>It looks like you may be a victim of Python's super coarse grained concurrency. Change your script to this:</p> <pre><code>#!/bin/bash echo "sleeping five" sleep 5 echo "sleeping five again" sleep 5 echo "slept five" </code></pre> <p>And then the output becomes:</p> <pre><code>process: sleeping five real 0m5....
-1
2009-10-26T15:30:54Z
[ "python", "pipe", "subprocess", "stdout", "kill" ]
How do I close the stdout-pipe when killing a process started with python subprocess Popen?
1,624,254
<p>I wonder if it is possible to shut down the communication pipe when killing a subprocess started in a different thread. If I do not call communicate() then kill() will work as expected, terminating the process after one second instead of five.</p> <p>I found a discussion of a similar problem <a href="http://bugs.py...
9
2009-10-26T11:18:43Z
1,625,904
<p>It seems to me that the easiest way to do this and sidestep the multithreading problems would be to set a kill flag from the main thread, and check for it in the script-running thread just before the communication, killing the script when the flag is <code>True</code>.</p>
0
2009-10-26T16:51:38Z
[ "python", "pipe", "subprocess", "stdout", "kill" ]
How do I close the stdout-pipe when killing a process started with python subprocess Popen?
1,624,254
<p>I wonder if it is possible to shut down the communication pipe when killing a subprocess started in a different thread. If I do not call communicate() then kill() will work as expected, terminating the process after one second instead of five.</p> <p>I found a discussion of a similar problem <a href="http://bugs.py...
9
2009-10-26T11:18:43Z
7,396,875
<p>I think the problem is that process.kill() only kills the immediate child process (bash), not the sub-processes of the bash script.</p> <p>The problem and solution are described here:</p> <ul> <li><a href="http://www.doughellmann.com/PyMOTW/subprocess/#process-groups-sessions">http://www.doughellmann.com/PyMOTW/su...
5
2011-09-13T04:38:41Z
[ "python", "pipe", "subprocess", "stdout", "kill" ]
Django or similar for composite primary keys
1,624,257
<p>I am writing a web application for my engineering company (warning: I am a programmer only by hobby) and was planning on using Django until I hit this snag. The models I want to use naturally have multi-column primary keys. Per <a href="http://code.djangoproject.com/ticket/373">http://code.djangoproject.com/ticket/3...
14
2009-10-26T11:19:28Z
1,624,291
<p>A work around is to create a surrogate key (an auto increment column) as the primary key column and place a unique index on your domain composite key.</p> <p>Foreign keys will then refer to the surrogate primary key column.</p>
18
2009-10-26T11:27:24Z
[ "python", "django", "web-applications", "django-models" ]
Django or similar for composite primary keys
1,624,257
<p>I am writing a web application for my engineering company (warning: I am a programmer only by hobby) and was planning on using Django until I hit this snag. The models I want to use naturally have multi-column primary keys. Per <a href="http://code.djangoproject.com/ticket/373">http://code.djangoproject.com/ticket/3...
14
2009-10-26T11:19:28Z
1,624,298
<p>Why not add a normal primary key, and then specify that <code>part_number</code> and <code>part_revision</code> as <a href="http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together"><code>unique_together</code></a>?</p> <p>This essentially is the Djangoish (Djangonic?) way of doing what Mitch Wheat ...
23
2009-10-26T11:29:19Z
[ "python", "django", "web-applications", "django-models" ]
Django or similar for composite primary keys
1,624,257
<p>I am writing a web application for my engineering company (warning: I am a programmer only by hobby) and was planning on using Django until I hit this snag. The models I want to use naturally have multi-column primary keys. Per <a href="http://code.djangoproject.com/ticket/373">http://code.djangoproject.com/ticket/3...
14
2009-10-26T11:19:28Z
1,624,560
<p>I strongly suggest using a surrogate key. Not because it is "Djangoesque". Suppose that you use a composite key that includes part_number. What if some time later your company decides to change the format (and therefore values) of that field? Or in general terms, any field? You would not want to deal with changing p...
13
2009-10-26T12:37:23Z
[ "python", "django", "web-applications", "django-models" ]
Django or similar for composite primary keys
1,624,257
<p>I am writing a web application for my engineering company (warning: I am a programmer only by hobby) and was planning on using Django until I hit this snag. The models I want to use naturally have multi-column primary keys. Per <a href="http://code.djangoproject.com/ticket/373">http://code.djangoproject.com/ticket/3...
14
2009-10-26T11:19:28Z
1,624,703
<p>SQLAlchemy has support for composite primary and foreign keys, so any SQLAlchemy based framework (Pylons and Werkzeug comes to mind) should suite your needs. But surrogate primary key is easier to use and better supported anyway.</p>
1
2009-10-26T13:13:31Z
[ "python", "django", "web-applications", "django-models" ]
Removing Array Elements in Python while keeping track of their position
1,624,395
<p>I'v got two numpy arrays. The first array contains some zeros (which are distributed randomly over the length of the array), which I would like to remove. </p> <p>My issue is that I would also like to remove the entries of the second array at the index positions where the first array elements are zero. </p> <p>I o...
1
2009-10-26T11:59:38Z
1,624,578
<p>Is it what you want? I am a NumPy newbie. </p> <pre><code>In [1]: import numpy as np In [2]: a = np.array([1,2,0,3,0,4]) In [3]: b = np.array([1,2,3,4,5,6]) In [4]: b[np.where(a)] Out[4]: array([1, 2, 4, 6]) In [5]: np.where(a) Out[5]: (array([0, 1, 3, 5]),) In [6]: a[np.where(a)] Out[6]: array([1, 2, 3...
4
2009-10-26T12:41:04Z
[ "python", "arrays", "numpy" ]
Removing Array Elements in Python while keeping track of their position
1,624,395
<p>I'v got two numpy arrays. The first array contains some zeros (which are distributed randomly over the length of the array), which I would like to remove. </p> <p>My issue is that I would also like to remove the entries of the second array at the index positions where the first array elements are zero. </p> <p>I o...
1
2009-10-26T11:59:38Z
1,637,050
<p>You can use boolean indexing. x!=0 gives you a boolean array with True where x!=0 false where x==0. If you index either x or y with this array (ie x_nozeros=x[x!=0]) then you will get only the elements where x!=0. eg:</p> <pre><code>In [1]: import numpy as np In [2]: x = np.array([1,2,0,3,0,4]) In [3]: y = np.arang...
0
2009-10-28T12:46:05Z
[ "python", "arrays", "numpy" ]
How to avoid repetition of exception handling?
1,624,518
<p>I've moved IOError handling to a separate function to avoid boilerplate when opening files for reading.</p> <p>But what if IOError fires when the file is being read? If sshfs disconnects, or file is deleted by root, etc.?</p> <pre><code>def safe_open(*args): try: return open(*args) except IOError: ...
1
2009-10-26T12:27:26Z
1,624,589
<p>I would probably try move the file operations into separate functions and wrap those in a try... except</p> <p>actually I just got an even better idea... put the error handling into a decorator and apply the decorator to each of the functions that does file operations</p> <pre><code>def catch_io_errors(fn): de...
7
2009-10-26T12:43:35Z
[ "python", "exception-handling" ]
GUI + multithreading support + regex support. Which language? JAVA / Python / Ruby?
1,624,570
<p>I'm interested in learning a programming language with support for GUI, multithreading and easy test manipulation (support for regex).</p> <p>Mainly on Windows but preferably cross-platform. What does the Stack Overflow community suggest?</p>
1
2009-10-26T12:38:54Z
1,624,585
<p>I am a fan of Erlang: </p> <ul> <li><p>Wx GUI tool</p></li> <li><p>Regex (module regexp)</p></li> <li><p>Cross-platform</p></li> <li><p>Multi-threading (of course !)</p></li> <li><p>EUnit testing</p></li> </ul> <p>Of course Python is really appropriate too!</p>
1
2009-10-26T12:42:36Z
[ "java", "python", "user-interface", "multithreading" ]
GUI + multithreading support + regex support. Which language? JAVA / Python / Ruby?
1,624,570
<p>I'm interested in learning a programming language with support for GUI, multithreading and easy test manipulation (support for regex).</p> <p>Mainly on Windows but preferably cross-platform. What does the Stack Overflow community suggest?</p>
1
2009-10-26T12:38:54Z
1,624,603
<p>My suggestion would be Java. You can do all of that and much more.</p>
5
2009-10-26T12:50:09Z
[ "java", "python", "user-interface", "multithreading" ]
GUI + multithreading support + regex support. Which language? JAVA / Python / Ruby?
1,624,570
<p>I'm interested in learning a programming language with support for GUI, multithreading and easy test manipulation (support for regex).</p> <p>Mainly on Windows but preferably cross-platform. What does the Stack Overflow community suggest?</p>
1
2009-10-26T12:38:54Z
1,624,677
<ul> <li><p>Python is nice but has <a href="http://en.wikipedia.org/wiki/Global%5FInterpreter%5FLock" rel="nofollow">major problems</a> with multithreading (unless you are using <a href="http://en.wikipedia.org/wiki/Stackless%5FPython" rel="nofollow">Stackless</a>). it has nice support of multiprocessing, though. There...
0
2009-10-26T13:06:49Z
[ "java", "python", "user-interface", "multithreading" ]
GUI + multithreading support + regex support. Which language? JAVA / Python / Ruby?
1,624,570
<p>I'm interested in learning a programming language with support for GUI, multithreading and easy test manipulation (support for regex).</p> <p>Mainly on Windows but preferably cross-platform. What does the Stack Overflow community suggest?</p>
1
2009-10-26T12:38:54Z
1,624,683
<p>If you really like typing go for Java, if you really like whitespace go for python, if you like programming more than you like high performance go for Ruby.</p> <p>Seriously, Java is very complete and very cross-platform. I don't know how Python adds up for GUI stuff but when I was looking at Ruby in detail a coupl...
1
2009-10-26T13:09:04Z
[ "java", "python", "user-interface", "multithreading" ]
GUI + multithreading support + regex support. Which language? JAVA / Python / Ruby?
1,624,570
<p>I'm interested in learning a programming language with support for GUI, multithreading and easy test manipulation (support for regex).</p> <p>Mainly on Windows but preferably cross-platform. What does the Stack Overflow community suggest?</p>
1
2009-10-26T12:38:54Z
1,633,915
<p>I would call Tcl/Tk a natural choice for the features you listed.</p> <p>But might be called old fashioned.</p> <ul> <li>GUI - Tk, well integrated, looks okay if you know what your doing, you can also use Qt or gtk but thats a bit less common.</li> <li>Multithreading - Tcl has both CoRoutines for cooperative multi...
0
2009-10-27T22:00:12Z
[ "java", "python", "user-interface", "multithreading" ]
Django model fields validation
1,624,782
<p>Where should the validation of <em>model fields</em> go in django?</p> <p>I could name at least two possible choices: in the overloaded .save() method of the model or in the .to_python() method of the models.Field subclass (obviously for that to work you must write custom fields).</p> <p>Possible use cases:</p> <...
49
2009-10-26T13:27:15Z
1,625,061
<p>If I understand you "clearly" - you must override function get_db_prep_save instead of to_python</p>
1
2009-10-26T14:15:38Z
[ "python", "django", "django-models" ]
Django model fields validation
1,624,782
<p>Where should the validation of <em>model fields</em> go in django?</p> <p>I could name at least two possible choices: in the overloaded .save() method of the model or in the .to_python() method of the models.Field subclass (obviously for that to work you must write custom fields).</p> <p>Possible use cases:</p> <...
49
2009-10-26T13:27:15Z
1,625,211
<p>The root issue for this, is that the validation should happen on models. This has been discussed for quite some time in django (search form model aware validation on the dev mailing list). It leads to either duplication or things escaping validation before hitting the db.</p> <p>While that doesn't hit trunk, Malcol...
2
2009-10-26T14:40:00Z
[ "python", "django", "django-models" ]
Django model fields validation
1,624,782
<p>Where should the validation of <em>model fields</em> go in django?</p> <p>I could name at least two possible choices: in the overloaded .save() method of the model or in the .to_python() method of the models.Field subclass (obviously for that to work you must write custom fields).</p> <p>Possible use cases:</p> <...
49
2009-10-26T13:27:15Z
1,625,760
<p>Django has a <a href="https://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects">model validation</a> system in place since version 1.2.</p> <p>In comments sebpiq says "Ok, now there is a place to put model validation ... except that it is run only when using a ModelForm! So the question remain...
49
2009-10-26T16:27:12Z
[ "python", "django", "django-models" ]
Django model fields validation
1,624,782
<p>Where should the validation of <em>model fields</em> go in django?</p> <p>I could name at least two possible choices: in the overloaded .save() method of the model or in the .to_python() method of the models.Field subclass (obviously for that to work you must write custom fields).</p> <p>Possible use cases:</p> <...
49
2009-10-26T13:27:15Z
5,177,410
<p>I think you want this -></p> <pre><code>from django.db.models.signals import pre_save def validate_model(sender, **kwargs): if 'raw' in kwargs and not kwargs['raw']: kwargs['instance'].full_clean() pre_save.connect(validate_model, dispatch_uid='validate_models') </code></pre> <p>(Copied from <a href=...
7
2011-03-03T06:24:40Z
[ "python", "django", "django-models" ]
How to determine default Accept-Language header based on IP (country code)?
1,624,816
<p>I want like to enhance spam protection on my site. I've found out that after being banned by ip bots don't change the Accept-Language and Accept-Charset http headers (so most of spam comes with windows-1251 accept-charset). I understand that there may be normal users with unordinary preferences, but anyways, how can...
0
2009-10-26T13:33:32Z
1,625,676
<p>This answer has two parts: determining where your user comes from, and what language they speak. To determine where they come from, you can use a service such as <a href="http://www.hostip.info/use.html" rel="nofollow">hostip.info</a>, which has an API which takes an IP address and returns a country code. Second, yo...
1
2009-10-26T16:11:35Z
[ "python", "spam", "geoip" ]
Using Boost Python with Weak Ptrs?
1,624,905
<p>Trying to set up a dependency in C++ with a parent-child relationship. The parent contains the child and the child has a weak pointer to the parent.</p> <p>I would also like to be able to derive from the parent in Python. However, when I do this, I get a weak pointer error connecting up this parent-child relationsh...
1
2009-10-26T13:49:27Z
1,656,972
<p>I played with the code without the python stuff.</p> <p>This reproduced the problem:</p> <pre><code>Parent* p(new Parent); p-&gt;initialize(); </code></pre> <p>The problem is nothing is holding on to the shared_ptr object. This fixes it:</p> <pre><code>boost::shared_ptr&lt;Parent&gt; p(new Parent); p-&gt;initial...
2
2009-11-01T11:51:02Z
[ "c++", "python", "boost", "weak-references", "boost-python" ]
Using Boost Python with Weak Ptrs?
1,624,905
<p>Trying to set up a dependency in C++ with a parent-child relationship. The parent contains the child and the child has a weak pointer to the parent.</p> <p>I would also like to be able to derive from the parent in Python. However, when I do this, I get a weak pointer error connecting up this parent-child relationsh...
1
2009-10-26T13:49:27Z
6,990,631
<p>The interface of class_ allows you to control how the object is held. It is a template parameter called HeldType. There is more information in the Boost.Python documentation on class_, but your Python binding might look more like this:</p> <pre><code>class_&lt;Parent, boost::shared_ptr&lt;Parent&gt; &gt;("Parent"...
1
2011-08-09T01:57:15Z
[ "c++", "python", "boost", "weak-references", "boost-python" ]
Is there a way to debug a subprocess using pydev?
1,624,932
<p>I'm using Eclipse / PyDev trying to find a way to debug code that uses <code>subprocess.Popen</code> to create a child process: I want to be able to debug the child process that is created. The problem is that I cannot find a way to debug accross process boundaries, and I'm guessing that it is actually not possible...
10
2009-10-26T13:53:10Z
1,631,296
<p>I does not seem PyDev can do it (neither can PyDbg and WinDbg), but it looks like gdb can: <a href="http://wiki.python.org/moin/DebuggingWithGdb" rel="nofollow">http://wiki.python.org/moin/DebuggingWithGdb</a>.</p>
4
2009-10-27T14:43:46Z
[ "python", "eclipse", "debugging", "pydev", "waf" ]
Is there a way to debug a subprocess using pydev?
1,624,932
<p>I'm using Eclipse / PyDev trying to find a way to debug code that uses <code>subprocess.Popen</code> to create a child process: I want to be able to debug the child process that is created. The problem is that I cannot find a way to debug accross process boundaries, and I'm guessing that it is actually not possible...
10
2009-10-26T13:53:10Z
3,892,155
<p>I've found something of a workaround that might work for you.</p> <p>Like you, I first found the remote debugging option of manually inserting calls to pydevd.settrace() at desired breakpoints. But I also noticed that subsequent PyDev breakpoints (i.e. those created by clicking in the left margin) were obeyed. So...
3
2010-10-08T15:44:25Z
[ "python", "eclipse", "debugging", "pydev", "waf" ]
Problem using break command in Python tutorial
1,625,174
<p>I'm following the python tutorial at their site and I'm currently at the break continue section. I just tried this sample code.</p> <pre><code>&gt;&gt;&gt; for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... els...
2
2009-10-26T14:33:33Z
1,625,231
<p>You may need to update your Python interpreter. </p> <p>This code runs correctly for me (notice Python version number):</p> <pre><code>Python 2.6.1 (r261:67515, Dec 6 2008, 16:42:21) [GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt...
0
2009-10-26T14:43:23Z
[ "python", "for-loop", "break" ]
Problem using break command in Python tutorial
1,625,174
<p>I'm following the python tutorial at their site and I'm currently at the break continue section. I just tried this sample code.</p> <pre><code>&gt;&gt;&gt; for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... els...
2
2009-10-26T14:33:33Z
1,625,254
<p>The output which you show contains <strong>ten</strong> times the string "<em>x</em> is a prime number". However, this string is printed in the <code>else</code> clause of the inner loop, and is as such executed <em>at most</em> once for each execution of the inner loop.</p> <p>Since the outer loop performs eight i...
4
2009-10-26T14:46:06Z
[ "python", "for-loop", "break" ]
Problem using break command in Python tutorial
1,625,174
<p>I'm following the python tutorial at their site and I'm currently at the break continue section. I just tried this sample code.</p> <pre><code>&gt;&gt;&gt; for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... els...
2
2009-10-26T14:33:33Z
1,625,282
<p>My best guess is that your 'else:' statement isn't properly indented, and then your result is logical, check that you else's indentation is on level with 'for x'.</p> <p>I.e. you use:</p> <pre><code>for n in range(2,10): for x in range(2,n): if n%x == 0: print(n, '=', x, '*', n/x) break ...
1
2009-10-26T14:50:28Z
[ "python", "for-loop", "break" ]
Problem using break command in Python tutorial
1,625,174
<p>I'm following the python tutorial at their site and I'm currently at the break continue section. I just tried this sample code.</p> <pre><code>&gt;&gt;&gt; for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... els...
2
2009-10-26T14:33:33Z
1,625,320
<p>I think you have got indents wrong. If I take your code, and indent the else so that it is under the if statement, I get exactly the output that you are getting.</p> <p>The code below reproduces your output,</p> <pre><code>for n in range(2, 10): for x in range(2, n): if n % x == 0: print n,...
0
2009-10-26T14:56:28Z
[ "python", "for-loop", "break" ]
Problem using break command in Python tutorial
1,625,174
<p>I'm following the python tutorial at their site and I'm currently at the break continue section. I just tried this sample code.</p> <pre><code>&gt;&gt;&gt; for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print n, 'equals', x, '*', n/x ... break ... els...
2
2009-10-26T14:33:33Z
5,328,979
<p>I thought that <code>else</code> should always be aligned with <code>if</code>. That is what I have read. But in this prime number generator code, the only way to get the primes written once, is aligning <code>else</code> with <code>for x</code>. So I have no explanation for such identation. Although I am just start...
0
2011-03-16T17:09:29Z
[ "python", "for-loop", "break" ]
Facebook API registerUsers - Error 100: Invalid email hash
1,625,742
<p>Using PyFacebook I am trying to register a test user of my site with my facebook application. I can connect to the API fine and return a list of friends etc. However when trying to register an address using:</p> <pre><code>hashed_emails = facebook.hash_email('foo@bar.com') accounts = [hashed_emails] facebook.connec...
0
2009-10-26T16:23:48Z
1,635,490
<p>Is the hash being stored as the right type?</p> <p>Also, it may be good to store the hash as a separate variable in case there's some strange race condition popping up..</p>
0
2009-10-28T06:28:13Z
[ "python", "facebook" ]
Facebook API registerUsers - Error 100: Invalid email hash
1,625,742
<p>Using PyFacebook I am trying to register a test user of my site with my facebook application. I can connect to the API fine and return a list of friends etc. However when trying to register an address using:</p> <pre><code>hashed_emails = facebook.hash_email('foo@bar.com') accounts = [hashed_emails] facebook.connec...
0
2009-10-26T16:23:48Z
1,639,713
<p>My request array to the API was incorrectly formatted. It should have been:</p> <pre><code>hashed_emails = facebook.hash_email('foo@bar.com') # Wrong: accounts = [hashed_emails] accounts = [{"email_hash": hashed_emails}] facebook.connect.registerUsers(accounts) </code></pre> <p>Which returned the expected respo...
0
2009-10-28T19:58:02Z
[ "python", "facebook" ]
How to convert dumbo sequence file input to tab separated text
1,625,757
<p>I have in input, which could be a single primitive or a list or tuple of primitives.</p> <p>I'd like to flatten it to just a list, like so:</p> <pre><code>def flatten(values): return list(values) </code></pre> <p>The normal case would be flatten(someiterablethatisn'tastring)</p> <p>But if values = '1234', I'...
0
2009-10-26T16:26:45Z
1,625,785
<p>Sounds like you want <a href="http://docs.python.org/library/itertools.html#itertools.chain" rel="nofollow"><code>itertools.chain()</code></a>. You will need to special-case strings, though, since they are really just iterables of characters.</p> <p><strong>Update</strong>:</p> <p>This is a much simpler problem i...
3
2009-10-26T16:32:09Z
[ "python", "text", "hadoop" ]
How to convert dumbo sequence file input to tab separated text
1,625,757
<p>I have in input, which could be a single primitive or a list or tuple of primitives.</p> <p>I'd like to flatten it to just a list, like so:</p> <pre><code>def flatten(values): return list(values) </code></pre> <p>The normal case would be flatten(someiterablethatisn'tastring)</p> <p>But if values = '1234', I'...
0
2009-10-26T16:26:45Z
1,625,862
<p>I must say that the stated requirements are odd, and I don't think <em>flatten</em> is the right name for this kind of operation. But if you're <strong>really</strong> sure that this is what you want, then this is what I can distil from your question:</p> <pre><code>&gt;&gt;&gt; import itertools &gt;&gt;&gt; def t...
0
2009-10-26T16:45:36Z
[ "python", "text", "hadoop" ]
How to convert dumbo sequence file input to tab separated text
1,625,757
<p>I have in input, which could be a single primitive or a list or tuple of primitives.</p> <p>I'd like to flatten it to just a list, like so:</p> <pre><code>def flatten(values): return list(values) </code></pre> <p>The normal case would be flatten(someiterablethatisn'tastring)</p> <p>But if values = '1234', I'...
0
2009-10-26T16:26:45Z
1,626,963
<p>Based on your restated question, this <code>mapper</code> function might do what you want:</p> <pre><code>def mapper(key, values): r"""Specification: do some stuff yield k, v where k is a string from the first part in the key, and value is a tab separated string containing the rest of the key and the va...
1
2009-10-26T19:52:46Z
[ "python", "text", "hadoop" ]
Is there an easy way to create derived attributes in Django Model/Python classes?
1,626,155
<p>Every Django model has a default primary-key <code>id</code> created automatically. I want the model objects to have another attribute <code>big_id</code> which is calculated as:<br /> <code>big_id = id * SOME_CONSTANT</code></p> <p>I want to access <code>big_id</code> as <code>model_obj.big_id</code> without the c...
4
2009-10-26T17:30:44Z
1,626,205
<pre><code>class Person(models.Model): x = 5 name = .. email = .. def _get_y(self): if self.id: return x * self.id return None y = property(_get_y) </code></pre>
1
2009-10-26T17:38:17Z
[ "python", "django", "django-models", "oop" ]
Is there an easy way to create derived attributes in Django Model/Python classes?
1,626,155
<p>Every Django model has a default primary-key <code>id</code> created automatically. I want the model objects to have another attribute <code>big_id</code> which is calculated as:<br /> <code>big_id = id * SOME_CONSTANT</code></p> <p>I want to access <code>big_id</code> as <code>model_obj.big_id</code> without the c...
4
2009-10-26T17:30:44Z
1,626,216
<p>Well, django model instances are just python objects, or so I've been told anyway :P</p> <p>That is how I would do it:</p> <pre><code>class MyModel(models.Model): CONSTANT = 1234 id = models.AutoField(primary_key=True) # not really needed, but hey @property def big_id(self): return self.pk...
8
2009-10-26T17:40:48Z
[ "python", "django", "django-models", "oop" ]
Is there an easy way to create derived attributes in Django Model/Python classes?
1,626,155
<p>Every Django model has a default primary-key <code>id</code> created automatically. I want the model objects to have another attribute <code>big_id</code> which is calculated as:<br /> <code>big_id = id * SOME_CONSTANT</code></p> <p>I want to access <code>big_id</code> as <code>model_obj.big_id</code> without the c...
4
2009-10-26T17:30:44Z
1,626,233
<p>You've got two options I can think of right now:</p> <ol> <li>Since you don't want the field in the database your best bet is to define a method on the model that returns self.id * SOME_CONSTANT, say you call it big_id(). You can access this method anytime as yourObj.big_id(), and it will be available in templates ...
0
2009-10-26T17:45:11Z
[ "python", "django", "django-models", "oop" ]
Soundex algorithm in Python (homework help request)
1,626,217
<p>The US census bureau uses a special encoding called “soundex” to locate information about a person. The soundex is an encoding of surnames (last names) based on the way a surname sounds rather than the way it is spelled. Surnames that sound the same, but are spelled differently, like SMITH and SMYTH, have the sa...
0
2009-10-26T17:41:05Z
1,626,309
<p>I would suggest you try the following.</p> <ul> <li>Store a CurrentCoded and LastCoded variable to work with before appended to your output</li> <li>Break down the system into useful functions, such as <ol> <li>Boolean IsVowel(Char)</li> <li>Int Coded(Char)</li> <li>Boolean IsRule1(Char, Char)</li> </ol></li> </ul>...
1
2009-10-26T17:58:15Z
[ "python", "soundex" ]
Soundex algorithm in Python (homework help request)
1,626,217
<p>The US census bureau uses a special encoding called “soundex” to locate information about a person. The soundex is an encoding of surnames (last names) based on the way a surname sounds rather than the way it is spelled. Surnames that sound the same, but are spelled differently, like SMITH and SMYTH, have the sa...
0
2009-10-26T17:41:05Z
1,627,095
<p>This is hardly perfect (for instance, it produces the wrong result if the input doesn't start with a letter), and it doesn't implement the rules as independently-testable functions, so it's not really going to serve as an answer to the homework question. But this is how I'd implement it:</p> <pre><code>&gt;&gt;&gt...
0
2009-10-26T20:16:47Z
[ "python", "soundex" ]
How can this behavior be acomplished? Python "short circuting" test
1,626,267
<p>I have the following code:</p> <pre><code>def testGeodatabase(self): geodatabaseList = self.gp.ListWorkspaces("*","ALL") for x in geodatabaseList: if x == self.outputGeodatabase: return True else: pass return False </code></pre> <p>What i need to know the followi...
0
2009-10-26T17:51:42Z
1,626,287
<p>The <code>return</code> statement will indeed cause the function to be exited at that point. No further code is executed in the function.</p> <p>Here is a simple test which you could run to prove the point:</p> <pre><code>def someFunction(nums): for i in nums: if i == 1: return "Found 1!" ...
1
2009-10-26T17:54:29Z
[ "python", "short-circuiting" ]
How can this behavior be acomplished? Python "short circuting" test
1,626,267
<p>I have the following code:</p> <pre><code>def testGeodatabase(self): geodatabaseList = self.gp.ListWorkspaces("*","ALL") for x in geodatabaseList: if x == self.outputGeodatabase: return True else: pass return False </code></pre> <p>What i need to know the followi...
0
2009-10-26T17:51:42Z
1,626,289
<p><code>return</code> is the end of the line, and nothing else will happen in that function afterwards. On the other hand, you could rewrite your function as</p> <pre><code>def testGeodatabase(self): return self.outputGeodatabase in self.gp.ListWorkspaces("*","ALL") </code></pre>
8
2009-10-26T17:54:48Z
[ "python", "short-circuiting" ]
How can this behavior be acomplished? Python "short circuting" test
1,626,267
<p>I have the following code:</p> <pre><code>def testGeodatabase(self): geodatabaseList = self.gp.ListWorkspaces("*","ALL") for x in geodatabaseList: if x == self.outputGeodatabase: return True else: pass return False </code></pre> <p>What i need to know the followi...
0
2009-10-26T17:51:42Z
1,626,298
<p>You don't need the break keyword in the code above. Actually, you don't need the</p> <pre><code>else: pass </code></pre> <p>either. The</p> <pre><code>return True </code></pre> <p>will exit the function.</p>
2
2009-10-26T17:56:06Z
[ "python", "short-circuiting" ]
How can this behavior be acomplished? Python "short circuting" test
1,626,267
<p>I have the following code:</p> <pre><code>def testGeodatabase(self): geodatabaseList = self.gp.ListWorkspaces("*","ALL") for x in geodatabaseList: if x == self.outputGeodatabase: return True else: pass return False </code></pre> <p>What i need to know the followi...
0
2009-10-26T17:51:42Z
1,626,482
<p>I think that using <code>any()</code> is the best choice:</p> <pre><code>def testGeodatabase(self): geodatabaseList = self.gp.ListWorkspaces("*","ALL") return any(x == self.outputGeodatabase for x in geodatabaseList) </code></pre>
0
2009-10-26T18:26:54Z
[ "python", "short-circuiting" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
1,626,358
<p>Remember that settings.py is a live code file. Assuming that you don't have DEBUG set on production (which is a best practice), you can do something like:</p> <pre><code>if DEBUG: STATIC_PATH = /path/to/dev/files else: STATIC_PATH = /path/to/production/files </code></pre> <p>Pretty basic, but you could, in...
2
2009-10-26T18:05:19Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
1,626,371
<p>I use a settings_local.py and a settings_production.py. After trying several options I've found that it's easy to waste time with complex solutions when simply having two settings files feels easy and fast.</p> <p>When you use mod_python/mod_wsgi for your Django project you need to point it to your settings file. I...
12
2009-10-26T18:07:20Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
1,626,529
<p>I use a slightly modified version of the "if DEBUG" style of settings that Harper Shelby posted. Obviously depending on the environment (win/linux/etc.) the code might need to be tweaked a bit.</p> <p>I was in the past using the "if DEBUG" but I found that occasionally I needed to do testing with DEUBG set to Fals...
12
2009-10-26T18:34:04Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
1,627,065
<p>My solution to that problem is also somewhat of a mix of some solutions already stated here:</p> <ul> <li>I keep a file called <code>local_settings.py</code> that has the content <code>USING_LOCAL = True</code> in dev and <code>USING_LOCAL = False</code> in prod</li> <li>In <code>settings.py</code> I do an import o...
1
2009-10-26T20:10:27Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
1,627,123
<p>You might want to look at this question: <a href="http://stackoverflow.com/questions/1406892/elegantly-handle-site-specific-settings-configuration-in-svn-hg-git-etc">http://stackoverflow.com/questions/1406892/elegantly-handle-site-specific-settings-configuration-in-svn-hg-git-etc</a></p>
4
2009-10-26T20:21:56Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
1,629,770
<p>In <code>settings.py</code>:</p> <pre><code>try: from local_settings import * except ImportError as e: pass </code></pre> <p>You can override what needed in <code>local_settings.py</code>; it should stay out of your version control then. But since you mention copying I'm guessing you use none ;)</p>
90
2009-10-27T10:01:08Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
1,630,310
<p>I differentiate it in manage.py and created two separate settings file: local_settings.py and prod_settings.py. </p> <p>In manage.py I check whether the server is local server or production server. If it is a local server it would load up local_settings.py and it is a production server it would load up prod_setting...
0
2009-10-27T11:52:22Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
5,528,824
<p>For most of my projects I use following pattern:</p> <ol> <li>Create settings_base.py where I store settings that are common for all environments </li> <li>Whenever I need to use new environment with specific requirements I create new settings file (eg. settings_local.py) which inherits contents of settings_base.py...
1
2011-04-03T09:53:07Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
5,572,141
<p>I use a variation of what jpartogi mentioned above, that I find a little shorter:</p> <pre><code>import platform from django.core.management import execute_manager computername = platform.node() try: settings = __import__(computername + '_settings') except ImportError: import sys sys.stderr.write("Error: ...
1
2011-04-06T19:48:52Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
9,517,763
<p>The problem with most of these solutions is that you either have your local settings applied <em>before</em> the common ones, or <em>after</em> them.</p> <p>So it's impossible to override things like</p> <ul> <li>the env-specific settings define the addresses for the memcached pool, and in the main settings file t...
4
2012-03-01T14:18:02Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
15,315,143
<p>Instead of <code>settings.py</code>, use this layout:</p> <pre><code>. └── settings/    ├── __init__.py &lt;= not versioned    ├── common.py    ├── dev.py    └── prod.py </code></pre> <p><code>common.py</code> is where most of your configuration lives.</p> <p><code>prod....
49
2013-03-09T19:48:26Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
15,325,966
<p><a href="http://twoscoopspress.org/products/two-scoops-of-django-1-5">Two Scoops of Django: Best Practices for Django 1.5</a> suggests using version control for your settings files and storing the files in a separate directory:</p> <pre><code>project/ app1/ app2/ project/ __init__.py set...
184
2013-03-10T18:34:25Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
21,822,440
<p>I found the responses here very helpful. (Has this been more definitively solved? The last response was a year ago.) After considering all the approaches listed, I came up with a solution that I didn't see listed here. </p> <p>My criteria were:</p> <ul> <li><em>Everything</em> should be in source control. I don't ...
-2
2014-02-17T06:43:48Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
25,114,673
<p>I am also working with Laravel and I like the implementation there. I tried to mimic it and combining it with the solution proposed by T. Stone (look above):</p> <pre><code>PRODUCTION_SERVERS = ['*.webfaction.com','*.whatever.com',] def check_env(): for item in PRODUCTION_SERVERS: match = re.match(r"(^...
2
2014-08-04T08:38:05Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
33,485,243
<p>I manage my configurations with the help of <a href="https://github.com/sobolevn/django-split-settings" rel="nofollow">django-split-settings</a>. </p> <p>It is a drop-in replacement for the default settings. It is simple, yet configurable. And refactoring of your exisitng settings is not required.</p> <p>Here's a ...
2
2015-11-02T19:21:55Z
[ "python", "django", "deployment" ]
How to manage local vs production settings in Django?
1,626,326
<p>What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but some of them (like paths to static files) need to remain different, and hence should not be overwritten every time the new code is deployed.</p> ...
178
2009-10-26T18:00:26Z
36,996,922
<p>As an alternative to maintain different file if you wiil: If you are using git or any other VCS to push codes from local to server, what you can do is add the settings file to .gitignore.</p> <p>This will allow you to have different content in both places without any problem. SO on server you can configure an indep...
0
2016-05-03T06:15:30Z
[ "python", "django", "deployment" ]
Python email lib - How to remove attachment from existing message?
1,626,403
<p>I have an email that I'm reading with the Python email lib that I need to modify the attachments of. The email Message class has the "attach" method, but does not have anything like "detach". How can I remove an attachment from a multipart message? If possible, I want to do this without recreating the message from s...
1
2009-10-26T18:14:27Z
1,626,527
<p>Well, from my experience, in the context you are working, everything is a <code>Message</code> object. The message, its parts, attachments, everything. So, to accomplish what you want to do, you need to</p> <ol> <li>parse the message using the <a href="http://docs.python.org/library/email.parser.html" rel="nofollow...
2
2009-10-26T18:33:27Z
[ "python", "email", "mime" ]
Python email lib - How to remove attachment from existing message?
1,626,403
<p>I have an email that I'm reading with the Python email lib that I need to modify the attachments of. The email Message class has the "attach" method, but does not have anything like "detach". How can I remove an attachment from a multipart message? If possible, I want to do this without recreating the message from s...
1
2009-10-26T18:14:27Z
1,626,650
<p>The way I've figured out to do it is:</p> <ol> <li>Set the payload to an empty list with set_payload</li> <li>Create the payload, and attach to the message.</li> </ol>
1
2009-10-26T18:50:11Z
[ "python", "email", "mime" ]
Python email lib - How to remove attachment from existing message?
1,626,403
<p>I have an email that I'm reading with the Python email lib that I need to modify the attachments of. The email Message class has the "attach" method, but does not have anything like "detach". How can I remove an attachment from a multipart message? If possible, I want to do this without recreating the message from s...
1
2009-10-26T18:14:27Z
1,626,677
<p><a href="http://docs.python.org/library/email.message.html#email.message.Message.set%5Fpayload" rel="nofollow"><code>set_payload()</code></a> may help.</p> <blockquote> <p><code>set_payload(payload[, charset])</code></p> <p>Set the entire message object’s payload to payload. It is the client’s responsibi...
2
2009-10-26T18:56:37Z
[ "python", "email", "mime" ]
Python __getattribute__ (or __getattr__) to emulate php __call
1,626,478
<p>I would like to create a class that effectively does this (mixing a little PHP with Python)</p> <pre> class Middle(object) : # self.apply is a function that applies a function to a list # e.g self.apply = [] ... self.apply.append(foobar) def __call(self, name, *args) : self.apply(na...
2
2009-10-26T18:26:40Z
1,626,545
<p>Consider passing arguments to your methods as arguments, not encoded into the method name which will then be magically used as an argument.</p> <p>Where are you writing code that doesn't know what methods it will be calling?</p> <p>Why call <code>c.do_Something(x)</code> and then unpack the method name instead of ...
2
2009-10-26T18:37:03Z
[ "python", "class" ]
Python __getattribute__ (or __getattr__) to emulate php __call
1,626,478
<p>I would like to create a class that effectively does this (mixing a little PHP with Python)</p> <pre> class Middle(object) : # self.apply is a function that applies a function to a list # e.g self.apply = [] ... self.apply.append(foobar) def __call(self, name, *args) : self.apply(na...
2
2009-10-26T18:26:40Z
1,626,554
<p>You need to define <code>__getattr__</code>, it <a href="http://docs.python.org/reference/datamodel.html#object.%5F%5Fgetattr%5F%5F" rel="nofollow">is called if an attribute is not otherwise found</a> on your object.</p> <p>Notice that getattr is called for any failed lookup, and that you don't get it like a functi...
3
2009-10-26T18:38:13Z
[ "python", "class" ]
Python __getattribute__ (or __getattr__) to emulate php __call
1,626,478
<p>I would like to create a class that effectively does this (mixing a little PHP with Python)</p> <pre> class Middle(object) : # self.apply is a function that applies a function to a list # e.g self.apply = [] ... self.apply.append(foobar) def __call(self, name, *args) : self.apply(na...
2
2009-10-26T18:26:40Z
1,626,760
<p>I actually did this recently. Here's an example of how I solved it:</p> <pre><code>class Example: def FUNC_1(self, arg): return arg - 1 def FUNC_2(self, arg): return arg - 2 def decode(self, func, arg): try: exec( "result = self.FUNC_%s(arg)" % (func) ) exce...
-1
2009-10-26T19:11:25Z
[ "python", "class" ]
Installing TurboGears on windows 7
1,626,619
<p>I tried installing TurboGears 1.0 on Windows 7 using tgsetup.py. and got following error</p> <pre><code>error: Couldn't find a setup script in c:\users\sandre~1\appdata\local\temp\ easy_install-jimbkt\Cheetah-2.4.0.linux-i686.tar.gz </code></pre> <p>When looking into this folder I see easy_install-jimbkt folder ap...
0
2009-10-26T18:46:03Z
1,626,956
<p>Looks like it's trying to install the Linux binaries of Cheetah, which definitely won't work on Windows. To get that piece of the puzzle to run in Windows, you can download and install the source version with the following command (I'll assume you already have Python and setuptools installed and working): </p> <pre...
0
2009-10-26T19:51:21Z
[ "python", "easy-install", "turbogears" ]
Would it be a good idea or bad idea to connect a VB.NET frontend with a Python backend using sockets?
1,626,631
<p>I have some really nice Python code to do what I need to do. I don't particularly like any of the Python GUI choices though. wxPython is nice, but for what I need, the speed on resizing, refreshing and dynamically adding controls just isn't there. I would like to create the GUI in VB.NET. I imagine I could use IronP...
1
2009-10-26T18:47:40Z
1,626,911
<p>It's not a terrible idea. In fact, if you write the Python code to have a RESTful interface, and then access that from VB.NET, it is a downright good idea. Later on you could reuse that Python server from any other application written in Python or VB.NET or something else. Because REST is standard and easy to test, ...
1
2009-10-26T19:39:47Z
[ "python", "vb.net", "user-interface", "sockets" ]
Would it be a good idea or bad idea to connect a VB.NET frontend with a Python backend using sockets?
1,626,631
<p>I have some really nice Python code to do what I need to do. I don't particularly like any of the Python GUI choices though. wxPython is nice, but for what I need, the speed on resizing, refreshing and dynamically adding controls just isn't there. I would like to create the GUI in VB.NET. I imagine I could use IronP...
1
2009-10-26T18:47:40Z
1,627,082
<p>I think this is an excellent idea. I'll second Michael Dillon's recommendation for a REST API, and I'll further recommend that you use <a href="http://www.djangoproject.com/" rel="nofollow">Django</a> to implement your REST server.</p> <p>I wrote a REST web service using Django, and Django made it really easy and ...
0
2009-10-26T20:14:30Z
[ "python", "vb.net", "user-interface", "sockets" ]
Generating Separate Output files in Hadoop Streaming
1,626,786
<p>Using only a mapper (a Python script) and no reducer, how can I output a separate file with the key as the filename, for each line of output, rather than having long files of output?</p>
7
2009-10-26T19:17:29Z
1,664,639
<p>Is it possible to replace the outputFormatClass, when using streaming? In a native Java implementation you would extend the MultipleTextOutputFormat class and modify the method that names the output file. Then define your implementation as new outputformat with JobConf's setOutputFormat method</p> <p>you should ver...
1
2009-11-03T00:59:12Z
[ "python", "streaming", "hadoop", "mapreduce" ]
Generating Separate Output files in Hadoop Streaming
1,626,786
<p>Using only a mapper (a Python script) and no reducer, how can I output a separate file with the key as the filename, for each line of output, rather than having long files of output?</p>
7
2009-10-26T19:17:29Z
1,669,224
<p>You can either write to a text file on the local filesystem using python file functions or if you want to use HDFS use the <a href="http://wiki.apache.org/hadoop/HDFS-APIs" rel="nofollow">Thrift API</a>.</p>
1
2009-11-03T18:23:15Z
[ "python", "streaming", "hadoop", "mapreduce" ]
Generating Separate Output files in Hadoop Streaming
1,626,786
<p>Using only a mapper (a Python script) and no reducer, how can I output a separate file with the key as the filename, for each line of output, rather than having long files of output?</p>
7
2009-10-26T19:17:29Z
1,690,092
<p>The input and outputformat classes can be replaced by use of the -inputformat and -outputformat commandline parameters.</p> <p>One example of how to do this can be found in the <a href="http://dumbotics.com" title="Dumbo">dumbo project</a>, which is a python framework for writing streaming jobs. It has a feature fo...
7
2009-11-06T20:14:35Z
[ "python", "streaming", "hadoop", "mapreduce" ]
pythonic approach to dynamic class variable names (ala PHP's $$var)
1,626,842
<p>The following code:</p> <pre><code>class Log: BAT_STATS = ['AB', 'R', 'H', 'HR'] def __init__(self, type): for cat in Log.BAT_STATS: self.cat = 0 </code></pre> <p>I want the loop there to create a class property of each key in BAT_STATS, so I can go:</p> <pre><code>log = Log() print log.HR; <...
1
2009-10-26T19:28:45Z
1,626,887
<p>Maybe this?</p> <pre><code>class Log: BAT_STATS = ['AB', 'R', 'H', 'HR'] def __init__(self, type): for cat in Log.BAT_STATS: setattr(self, cat, 0) </code></pre> <p>EDIT - Oops, indentation was a bit messed up.</p> <p>@EOL: Are you suggesting putting it straight into the class definiti...
8
2009-10-26T19:36:12Z
[ "python" ]
pythonic approach to dynamic class variable names (ala PHP's $$var)
1,626,842
<p>The following code:</p> <pre><code>class Log: BAT_STATS = ['AB', 'R', 'H', 'HR'] def __init__(self, type): for cat in Log.BAT_STATS: self.cat = 0 </code></pre> <p>I want the loop there to create a class property of each key in BAT_STATS, so I can go:</p> <pre><code>log = Log() print log.HR; <...
1
2009-10-26T19:28:45Z
1,626,912
<p><strong>Explicit recommendation</strong>: DO NOT USE the following.<br> and please ease-up on the downvotes...</p> <p>Edit: I even found a quote from Alex Martelli expressing how bad an idea my snippet is.<br> (Short excerpt from <em>Python in a Nutshell</em>, 2nd ed. O'Reilly 2006)<br> <em>Use exec only when it is...
0
2009-10-26T19:39:54Z
[ "python" ]
template fragment caching doesn't seem to work for some custom template tags
1,627,131
<p>I've been implementing caching in my django application, and used per view caching via the cache API and template fragment caching. On some of my pages I use a custom django template tag, this tag is provided via a third party developer, it takes some arguments in its template tags, and then make a request to a remo...
3
2009-10-26T20:22:52Z
1,653,254
<p>Have you tried to use a different name for the cache fragment? There could be a problem with using request.user.username for a couple of reasons:</p> <ul> <li><p>If a user is not signed in, request.user.username could be empty, resulting in a non-named cache fragment</p></li> <li><p>If a user is signed in, this wil...
0
2009-10-31T02:58:47Z
[ "python", "django", "memcached", "fragment-caching" ]
template fragment caching doesn't seem to work for some custom template tags
1,627,131
<p>I've been implementing caching in my django application, and used per view caching via the cache API and template fragment caching. On some of my pages I use a custom django template tag, this tag is provided via a third party developer, it takes some arguments in its template tags, and then make a request to a remo...
3
2009-10-26T20:22:52Z
1,653,404
<p>I think the problem is the custom tag, as you suggested.</p> <p>I disagree that the request.user.username is a problem, as the documentation for the subject actually gives that as an example, and I have used it with internal caching (number of posts, for instance), in testing, and it worked fine.</p> <p>The low le...
0
2009-10-31T04:25:43Z
[ "python", "django", "memcached", "fragment-caching" ]
template fragment caching doesn't seem to work for some custom template tags
1,627,131
<p>I've been implementing caching in my django application, and used per view caching via the cache API and template fragment caching. On some of my pages I use a custom django template tag, this tag is provided via a third party developer, it takes some arguments in its template tags, and then make a request to a remo...
3
2009-10-26T20:22:52Z
1,670,873
<p>I don't think this has anything to do with the custom tag.</p> <p>We endded up rewriting the Django caching tag because we needed more control than was possible with the one that was supplied. You might make a copy of it yourself and stick some debugging print statements into it. In particular, check the filename (...
0
2009-11-03T23:40:42Z
[ "python", "django", "memcached", "fragment-caching" ]
template fragment caching doesn't seem to work for some custom template tags
1,627,131
<p>I've been implementing caching in my django application, and used per view caching via the cache API and template fragment caching. On some of my pages I use a custom django template tag, this tag is provided via a third party developer, it takes some arguments in its template tags, and then make a request to a remo...
3
2009-10-26T20:22:52Z
1,679,549
<p>If the template fragment you're trying to cache can't be pickled, memcached won't be able to store it and will raise an exception. From what I can gather, exceptions generated when rendering Django templates are suppressed. Since your custom tag is doing HTTP requests, maybe socket objects (which can't be pickled) a...
3
2009-11-05T10:05:25Z
[ "python", "django", "memcached", "fragment-caching" ]
django flatpage redirects
1,627,146
<p>I want to make sure all of my flatpages have the "www" subdomain and redirect to it if they don't. I've looked at some middlewares that redirect to www, but 1. they usually redirect all urls to www and 2. the ones I've found don't work with flatpages. </p> <p>I don't want all of my site urls to redirect to includ...
1
2009-10-26T20:26:15Z
1,627,683
<p>One option is to modify a middleware, so that it only redirects if <code>response.status_code == 404</code>. Put the middleware just before the flatpage middleware in settings.py. This would redirect</p> <pre><code>http://example.com/flatpage/ -&gt; http://www.example.com/flatpage/ </code></pre> <p>but also</p> <...
0
2009-10-26T22:25:34Z
[ "python", "django", "django-flatpages" ]
django flatpage redirects
1,627,146
<p>I want to make sure all of my flatpages have the "www" subdomain and redirect to it if they don't. I've looked at some middlewares that redirect to www, but 1. they usually redirect all urls to www and 2. the ones I've found don't work with flatpages. </p> <p>I don't want all of my site urls to redirect to includ...
1
2009-10-26T20:26:15Z
1,630,416
<p>In your urls.py file do something like this:</p> <pre><code>urlpatterns = patterns('', (r'^flat/(?P&lt;static&gt;.*)$', 'django.views.generic.simple.redirect_to', {'url': 'http://www.mysite.com/flat/%(static)s'}), # other stuff ) </code></pre>
0
2009-10-27T12:16:18Z
[ "python", "django", "django-flatpages" ]
PyQT events between multiple objects
1,627,148
<p>I am creating a GUI program in Python/PyQT and would like to know how I can connect an event which happens in a child object to the parent?</p> <p>For example, if someone clicks a 'Submit' button, how would i trigger something to happen in the parent object (lets say update a QLabel on the parent)</p> <p>Any help ...
0
2009-10-26T20:26:43Z
1,627,180
<p>It is done like in C++ Qt by connecting signals to slots, you will find all <a href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#new-style-signal-and-slot-support" rel="nofollow">the information on this page</a> (and <a href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html...
4
2009-10-26T20:34:26Z
[ "python", "events", "pyqt" ]
PyQT events between multiple objects
1,627,148
<p>I am creating a GUI program in Python/PyQT and would like to know how I can connect an event which happens in a child object to the parent?</p> <p>For example, if someone clicks a 'Submit' button, how would i trigger something to happen in the parent object (lets say update a QLabel on the parent)</p> <p>Any help ...
0
2009-10-26T20:26:43Z
1,813,391
<p>You must connect these methods every time you set new parent (and remove old connections!!) <a href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qobject.html#connect" rel="nofollow">http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qobject.html#connect</a> (If widget parent change and You sti...
0
2009-11-28T18:54:04Z
[ "python", "events", "pyqt" ]
Python mkdir giving me wrong permissions
1,627,198
<p>I'm trying to create a folder and create a file within it.</p> <p>Whenever i create that folder (via Python), it creates a folder that gives me no permissions at all and read-only mode.</p> <p>When i try to create the file i get an IOError.</p> <pre><code>Error: &lt;type 'exceptions.IOError'&gt; </code></pre> <...
6
2009-10-26T20:38:34Z
1,627,222
<p>After you create the folder you can set the permissions with <code>os.chmod</code></p> <p>The mod is written in base 8, if you convert it to binary it would be</p> <pre><code>000 111 111 000 rwx rwx rwx </code></pre> <p>The first <code>rwx</code> is for owner, the second is for the group and the third is for ...
17
2009-10-26T20:42:58Z
[ "python", "windows", "filesystems", "mkdir" ]
Python mkdir giving me wrong permissions
1,627,198
<p>I'm trying to create a folder and create a file within it.</p> <p>Whenever i create that folder (via Python), it creates a folder that gives me no permissions at all and read-only mode.</p> <p>When i try to create the file i get an IOError.</p> <pre><code>Error: &lt;type 'exceptions.IOError'&gt; </code></pre> <...
6
2009-10-26T20:38:34Z
1,627,246
<p>You've probably got a funky umask. Try <code>os.umask(0002)</code> before making your directory.</p>
4
2009-10-26T20:47:50Z
[ "python", "windows", "filesystems", "mkdir" ]
Python mkdir giving me wrong permissions
1,627,198
<p>I'm trying to create a folder and create a file within it.</p> <p>Whenever i create that folder (via Python), it creates a folder that gives me no permissions at all and read-only mode.</p> <p>When i try to create the file i get an IOError.</p> <pre><code>Error: &lt;type 'exceptions.IOError'&gt; </code></pre> <...
6
2009-10-26T20:38:34Z
1,627,610
<p>The Python manual says:</p> <blockquote> <pre><code>os.mkdir(path[, mode]) </code></pre> <p>Create a directory named path with numeric mode mode. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. Availability: Unix, Windows.</p> <...
3
2009-10-26T22:06:37Z
[ "python", "windows", "filesystems", "mkdir" ]
Python mkdir giving me wrong permissions
1,627,198
<p>I'm trying to create a folder and create a file within it.</p> <p>Whenever i create that folder (via Python), it creates a folder that gives me no permissions at all and read-only mode.</p> <p>When i try to create the file i get an IOError.</p> <pre><code>Error: &lt;type 'exceptions.IOError'&gt; </code></pre> <...
6
2009-10-26T20:38:34Z
1,628,362
<p>Since your on Windows, this might be a crapshoot. Make sure there aren't any wacky special permissions on the parent directory or with the policy settings that defines the permissions any directories created by your account get. I doubt this is a python problem as I haven't been able to recreate the problem on Win...
1
2009-10-27T01:52:16Z
[ "python", "windows", "filesystems", "mkdir" ]
How do I extract a ieee-be binary file embedded in a zipfile?
1,627,376
<p>I have a set of zip files which contains several ieee-be encoded binary and text files. I have used Pythons ZipFile module and can extract the contents of the text file</p> <pre><code>def readPropFile(myZipFile): zf = zipfile.ZipFile(myZipFile,'r') # Open zip file for reading zFileList=zf.namelist() # extra...
0
2009-10-26T21:13:17Z
1,627,569
<p>Most of what you need is in this answer <a href="http://stackoverflow.com/questions/1053121/how-do-i-convert-a-python-float-to-a-hexadecimal-string-in-python-2-5-nonworking">http://stackoverflow.com/questions/1053121/how-do-i-convert-a-python-float-to-a-hexadecimal-string-in-python-2-5-nonworking</a></p> <p>See the...
2
2009-10-26T21:58:07Z
[ "python" ]
How do I extract a ieee-be binary file embedded in a zipfile?
1,627,376
<p>I have a set of zip files which contains several ieee-be encoded binary and text files. I have used Pythons ZipFile module and can extract the contents of the text file</p> <pre><code>def readPropFile(myZipFile): zf = zipfile.ZipFile(myZipFile,'r') # Open zip file for reading zFileList=zf.namelist() # extra...
0
2009-10-26T21:13:17Z
1,627,576
<p>In the snippet from the question, the "properties files" (whatever that is) are detected, in a rather loose fashion, by the presence of the string 'properties' in their contents, when read as text. I don't know what the equivalent of this would be for binary IEEE-le files.</p> <p>However, with Python, a easy way t...
1
2009-10-26T21:59:37Z
[ "python" ]
How do I extract a ieee-be binary file embedded in a zipfile?
1,627,376
<p>I have a set of zip files which contains several ieee-be encoded binary and text files. I have used Pythons ZipFile module and can extract the contents of the text file</p> <pre><code>def readPropFile(myZipFile): zf = zipfile.ZipFile(myZipFile,'r') # Open zip file for reading zFileList=zf.namelist() # extra...
0
2009-10-26T21:13:17Z
1,627,805
<p>You could also try the Numpy extension (<a href="http://numpy.scipy.org" rel="nofollow">here</a>), which is a bit lighter than SciPy. Numpy has lots of I/O routines. For example,</p> <pre><code>import numpy f = file ('example.dat') data_type = numpy.dtype ('float32').newbyteorder ('&gt;') x = numpy.fromfile (f, d...
1
2009-10-26T22:53:07Z
[ "python" ]
Is there a convenient way to alias only conflicting columns when joining tables in SQLAlchemy?
1,627,429
<p>Sometimes it is useful to map a class against a <code>join</code> instead of a single table when using <a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a>'s declarative extension. When column names collide, usually in a one-to-many because all primary keys are named <code>id</code> by default, you can...
3
2009-10-26T21:27:57Z
1,636,615
<p>You can create alias for each column separately with its <code>label()</code> method. So it's possible something similar to the following (not tested):</p> <pre><code>from sqlalchemy import select def alias_dups(join): dups = set(col.key for col in join.left.columns) &amp; \ set(col.key for col...
4
2009-10-28T11:13:45Z
[ "python", "join", "sqlalchemy", "alias" ]