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
list
Inserting values into specific locations in a list in Python
2,218,238
<p>I am trying to do print all the possible outcomes of a given list and I was wondering how to put a value into various locations in the list. For example, if my list was <code>[A,B]</code>, I want to insert <code>X</code> into all possible index of the list such that it would return this <code>[X,A,B]</code>, <code>[...
18
2010-02-07T20:38:26Z
2,218,392
<p>Use insert() to insert an element before a given position.</p> <p>For instance, with</p> <pre><code>arr = ['A','B','C'] arr.insert(0,'D') </code></pre> <p>arr becomes ['D','A','B','C'] because 'D' is inserted before the element at index 0.</p> <p>Now, for</p> <pre><code>arr = ['A','B','C'] arr.insert(4,'D') </c...
36
2010-02-07T21:23:11Z
[ "python", "list" ]
Inserting values into specific locations in a list in Python
2,218,238
<p>I am trying to do print all the possible outcomes of a given list and I was wondering how to put a value into various locations in the list. For example, if my list was <code>[A,B]</code>, I want to insert <code>X</code> into all possible index of the list such that it would return this <code>[X,A,B]</code>, <code>[...
18
2010-02-07T20:38:26Z
12,849,391
<p>If you want to insert a list intro a list, you can do this:</p> <pre><code>&gt;&gt;&gt; a = [1,2,3,4,5] &gt;&gt;&gt; for x in reversed(['a','b','c']): a.insert(2,x) &gt;&gt;&gt; a [1, 2, 'a', 'b', 'c', 3, 4, 5] </code></pre>
2
2012-10-11T22:14:33Z
[ "python", "list" ]
SQLAlchemy INSERT IGNORE
2,218,304
<p>How can I insert multiple data records into table ignoring duplicates. I am using SQLAlchemy.</p> <p>Thank you!</p>
19
2010-02-07T20:54:53Z
2,224,729
<p><code>prefix_with("TEXT")</code> adds arbitrary text between <code>INSERT</code> and the rest of the SQL. <code>execute()</code> accepts a list of dictionaries with the records you would like to insert or a single dictionary if you only want to insert a single record.</p> <p>The SQLite syntax for the behavior you'r...
22
2010-02-08T20:45:04Z
[ "python", "insert", "sqlalchemy", "ignore" ]
Profiling of Python threads
2,218,330
<p>I am trying to figure out how to measure the performance of several python threads in my application. I currently have several tasks that are executing on different threads based on user input and I would like to measure the execution time, maybe even memory consumption of each of the threads. I have tried to use cP...
9
2010-02-07T21:03:04Z
2,218,592
<p>There are a few different ways to tackle this problem. <code>cProfile</code> is great and comes with Python, however many people see multi-threaded profiling as an issue. One way of getting around this is by running separate instances of cProfile for each thread and then combining the results using <code>Stats.add</...
3
2010-02-07T22:13:30Z
[ "python", "multithreading" ]
Deciding on a blog URL scheme with regard to python performance
2,218,363
<p>I'm writing a blog in Python and have come to the point where I have to decide on the URL scheme to use. It's tempting to just list the entries start to end, like:</p> <pre><code>http://myblog.com/1 http://myblog.com/2 ... http://myblog.com/1568 </code></pre> <p>And on the server side I would just have the blog en...
1
2010-02-07T21:14:08Z
2,218,385
<p>What about mapping URLs to blog entries using a dictionary? This is simple, and dictionary has O(1) lookup so it would be fast even if your blog had many entries.</p>
0
2010-02-07T21:22:02Z
[ "python", "url" ]
Deciding on a blog URL scheme with regard to python performance
2,218,363
<p>I'm writing a blog in Python and have come to the point where I have to decide on the URL scheme to use. It's tempting to just list the entries start to end, like:</p> <pre><code>http://myblog.com/1 http://myblog.com/2 ... http://myblog.com/1568 </code></pre> <p>And on the server side I would just have the blog en...
1
2010-02-07T21:14:08Z
2,223,583
<p>The performance difference between the two will be negligible. The overhead of the list or dictionary will be much smaller than the size of the text in the entries.</p> <p>Keeping everything in memory is not a good solution though. You should persist the entries to disk (database, flat file, etc.), then (if necessa...
1
2010-02-08T17:38:54Z
[ "python", "url" ]
What's wrong here? Iterating over a dictionary in Django template
2,218,388
<p>I'm trying to iterate over a dictionary of model values in a Django template - I want to list the verbose_name of each model field alongside its value.</p> <p>Here's what I have in models.py: </p> <pre><code>class Manors(models.Model): structidx = models.IntegerField(primary_key=True, verbose_name="ID") ...
7
2010-02-07T21:22:30Z
2,218,406
<p>You're getting the weird results because I think you're iterating over a string's characters. A for loop in django templates isn't the same as in python. Try using an object and iterating via property accessors <code>for object in my objects</code> and then use <code>object.prop1</code> <code>object.prop2</code> ins...
0
2010-02-07T21:25:10Z
[ "python", "django", "django-templates" ]
What's wrong here? Iterating over a dictionary in Django template
2,218,388
<p>I'm trying to iterate over a dictionary of model values in a Django template - I want to list the verbose_name of each model field alongside its value.</p> <p>Here's what I have in models.py: </p> <pre><code>class Manors(models.Model): structidx = models.IntegerField(primary_key=True, verbose_name="ID") ...
7
2010-02-07T21:22:30Z
2,218,605
<p>To iterate a dictionary wouldn't you need:</p> <pre><code>&lt;h4&gt;Statistics&lt;/h4&gt; &lt;ul&gt; {% for key, value in manor_stats.get_fields.items %} &lt;li&gt; {{ key }}: {{ value }}&lt;/li&gt; {% endfor %} &lt;/ul&gt; </code></pre> <p>But I'd suggest retrieving the dictionary from the function first:</p> ...
27
2010-02-07T22:19:58Z
[ "python", "django", "django-templates" ]
What's wrong here? Iterating over a dictionary in Django template
2,218,388
<p>I'm trying to iterate over a dictionary of model values in a Django template - I want to list the verbose_name of each model field alongside its value.</p> <p>Here's what I have in models.py: </p> <pre><code>class Manors(models.Model): structidx = models.IntegerField(primary_key=True, verbose_name="ID") ...
7
2010-02-07T21:22:30Z
2,219,447
<p>Iterating over a dict yields its keys. I don't know why Django thinks you want to do an incomplete sequence expansion on the key name instead of throwing an exception, but I'll chalk it up to <em>ANOTHER</em> one of Django's template engine's quirks.</p> <p>Anyways, yes, get <code>key</code> from the dict in your f...
1
2010-02-08T03:45:07Z
[ "python", "django", "django-templates" ]
Many2ManyField is not saving via Modelforms
2,218,424
<p>I have a Modelform:</p> <pre><code>class POwner4NewModel(ModelForm): class Meta: model = ProductOwner exclude = ("o_owner","o_owner_desc","o_product_model","o_main_image","o_thumbnail","o_gallery_images","o_timestamp","o_status") </code></pre> <p>This is the model's schema:</p> <pre><code>clas...
0
2010-02-07T21:28:36Z
2,219,460
<p>form doesn't have <code>save_m2m()</code> because you overwrote <code>form</code> with a model instance when you did <code>form = form.save(commit=False)</code></p> <p>try using something else like <code>instance = form.save(commit=False)</code> etc. then you should be able to use <code>form.save_m2m()</code> (of c...
4
2010-02-08T03:50:08Z
[ "python", "django" ]
Python + Komodo Edit
2,218,637
<p>I am trying to get scapy to auto complete in komodo edit with no success, has anyone successfully done this?</p> <p>Thanks, Python New Comer </p>
1
2010-02-07T22:30:52Z
2,219,126
<p>Autocomplete in Python is a hit or miss proposition. It varies widely -- some things can be analyzed by Komodo and some can't. </p> <p>If it won't autocomplete, it's probably because it relies on too many metaclass techniques that seem to baffle Komodo.</p>
1
2010-02-08T01:21:15Z
[ "python", "scapy", "komodoedit" ]
Python + Komodo Edit
2,218,637
<p>I am trying to get scapy to auto complete in komodo edit with no success, has anyone successfully done this?</p> <p>Thanks, Python New Comer </p>
1
2010-02-07T22:30:52Z
2,220,978
<p>If you're in virtualenv or have some tricky python paths you have to add additional import directories in:</p> <blockquote> <p>Preferences -> Languages -> Python -> Additional Python Import Directories</p> </blockquote> <p>Komodo can't understand these things out of the box.</p> <p>Also make sure you have fol...
1
2010-02-08T10:45:18Z
[ "python", "scapy", "komodoedit" ]
Unable to access ID property from a datastore entity
2,218,693
<p>Using Google App Engine SDK and Python, I'm facing an issue : I'm unable to access the ID property of a given entity properties. The only properties I can access are those defined in my class Model, plus the key property (see answer below) :</p> <pre><code>class Question(db.Model): text = db.StringProperty() ...
6
2010-02-07T22:45:26Z
2,218,736
<p>According to the <strong><a href="http://code.google.com/appengine/docs/python/datastore/modelclass.html">documentation</a></strong>, there is no <code>id()</code> instance method defined for Model subclasses.</p> <p>Try <code>{{ question.key }}</code> instead.</p> <p>Also note that the key is not created until th...
8
2010-02-07T22:59:20Z
[ "python", "google-app-engine", "entity", "gae-datastore" ]
Unable to access ID property from a datastore entity
2,218,693
<p>Using Google App Engine SDK and Python, I'm facing an issue : I'm unable to access the ID property of a given entity properties. The only properties I can access are those defined in my class Model, plus the key property (see answer below) :</p> <pre><code>class Question(db.Model): text = db.StringProperty() ...
6
2010-02-07T22:45:26Z
2,218,833
<p>I just found a possible (inelegant, IMO) solution. After querying and fetching entities, loop through all of them and manually add the id parameter :</p> <pre><code>query = Question.all() questions = query.fetch(10) # Add ID property : for question in questions: question.id = str(question.key().id()) </code></...
4
2010-02-07T23:35:42Z
[ "python", "google-app-engine", "entity", "gae-datastore" ]
time.localtime() - how does it work? Brief questions on how to use it too - super easy stuff!
2,218,737
<p>How does time.localtime() work exactly? I can call up the "array" (tupple, I think it is called - because it is immutable?) and reference/index components of it. For example:</p> <pre><code>&gt;&gt;&gt; time.localtime()[0] 2010 </code></pre> <p>But if I do:</p> <pre><code>print time.localtime() time.struct_time...
1
2010-02-07T23:00:18Z
2,218,772
<p><code>time.localtime()[0]</code> calls <code>__gettiem__()</code> on the <code>time.struct_time</code> instance. This is how it gets this list/tuple like behaviour from. Here is a really simple example</p> <pre><code>&gt;&gt;&gt; class MyTime(object): ... def __init__(self, year, month, mday, hour, minute): ......
3
2010-02-07T23:13:47Z
[ "python", "arrays", "time" ]
time.localtime() - how does it work? Brief questions on how to use it too - super easy stuff!
2,218,737
<p>How does time.localtime() work exactly? I can call up the "array" (tupple, I think it is called - because it is immutable?) and reference/index components of it. For example:</p> <pre><code>&gt;&gt;&gt; time.localtime()[0] 2010 </code></pre> <p>But if I do:</p> <pre><code>print time.localtime() time.struct_time...
1
2010-02-07T23:00:18Z
2,218,784
<ol> <li><p>The <code>print</code> statement tries to convert all its arguments to a string representation. And the <code>__repr__</code> or <code>__str__</code> method of a struct_time object yields the result</p> <p>time.struct_time(tm_year=2010, tm_mon=2, tm_mday=7, tm_hour=14, tm_min=46, tm_sec=58, tm_wday=6, tm_...
0
2010-02-07T23:18:00Z
[ "python", "arrays", "time" ]
time.localtime() - how does it work? Brief questions on how to use it too - super easy stuff!
2,218,737
<p>How does time.localtime() work exactly? I can call up the "array" (tupple, I think it is called - because it is immutable?) and reference/index components of it. For example:</p> <pre><code>&gt;&gt;&gt; time.localtime()[0] 2010 </code></pre> <p>But if I do:</p> <pre><code>print time.localtime() time.struct_time...
1
2010-02-07T23:00:18Z
2,218,845
<p>You can make your own "named tuples", i.e., subclasses of <code>tuple</code> with items accessible by either indexing or as attributes, with <a href="http://docs.python.org/library/collections.html?highlight=namedtuple#collections.namedtuple" rel="nofollow">collections.namedtuple</a> (in Python 2.6 or better). There...
1
2010-02-07T23:41:39Z
[ "python", "arrays", "time" ]
time.localtime() - how does it work? Brief questions on how to use it too - super easy stuff!
2,218,737
<p>How does time.localtime() work exactly? I can call up the "array" (tupple, I think it is called - because it is immutable?) and reference/index components of it. For example:</p> <pre><code>&gt;&gt;&gt; time.localtime()[0] 2010 </code></pre> <p>But if I do:</p> <pre><code>print time.localtime() time.struct_time...
1
2010-02-07T23:00:18Z
2,218,847
<p>Nascent Notes,</p> <p>The function <code>time.localtime()</code> accepts an optional argument of <a href="http://en.wikipedia.org/wiki/Unix_epoch" rel="nofollow">Epoch seconds</a>, without the argument it takes the current time (probably from <code>time.time()</code>). The function then calculates what time this co...
0
2010-02-07T23:42:34Z
[ "python", "arrays", "time" ]
Right way to return proxy model instance from a base model instance in Django?
2,218,867
<p>Say I have models:</p> <pre><code>class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print "Woof!" class Meta: proxy = True class Cat(Animal): def make_sound(self): print "Meow!" class Meta: proxy = True ...
20
2010-02-07T23:51:55Z
2,218,881
<p>You can perhaps make Django models polymorphic using the approach described <a href="http://groups.google.com/group/django-developers/msg/b567b75fe61d5044" rel="nofollow">here</a>. That code is in early stages of development, I believe, but worth investigating.</p>
1
2010-02-07T23:56:29Z
[ "python", "django", "django-models", "proxy-classes" ]
Right way to return proxy model instance from a base model instance in Django?
2,218,867
<p>Say I have models:</p> <pre><code>class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print "Woof!" class Meta: proxy = True class Cat(Animal): def make_sound(self): print "Meow!" class Meta: proxy = True ...
20
2010-02-07T23:51:55Z
5,293,266
<p>the only way known to the human kind is to use Metaclass programming.</p> <p>Here is short answer:</p> <pre><code>from django.db.models.base import ModelBase class InheritanceMetaclass(ModelBase): def __call__(cls, *args, **kwargs): obj = super(InheritanceMetaclass, cls).__call__(*args, **kwargs) ...
4
2011-03-13T23:30:53Z
[ "python", "django", "django-models", "proxy-classes" ]
Right way to return proxy model instance from a base model instance in Django?
2,218,867
<p>Say I have models:</p> <pre><code>class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print "Woof!" class Meta: proxy = True class Cat(Animal): def make_sound(self): print "Meow!" class Meta: proxy = True ...
20
2010-02-07T23:51:55Z
21,958,395
<p>This answer may be side-stepping the question somewhat because it doesn't use proxy models. However, as the question asks, it does let one write the following (and without having to update the <code>Animal</code> class if new types are added)--</p> <pre><code>animals = Animal.objects.all() for animal in animals: ...
0
2014-02-22T18:16:29Z
[ "python", "django", "django-models", "proxy-classes" ]
Right way to return proxy model instance from a base model instance in Django?
2,218,867
<p>Say I have models:</p> <pre><code>class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print "Woof!" class Meta: proxy = True class Cat(Animal): def make_sound(self): print "Meow!" class Meta: proxy = True ...
20
2010-02-07T23:51:55Z
23,663,043
<p>The Metaclass approach proposed by thedk is indeed a very powerful way to go, however, I had to combine it with an answer to the question <a href="http://stackoverflow.com/questions/3006753/how-to-convert-request-user-into-a-proxy-auth-user-class">here</a> to have the query return a <em>proxy</em> model instance. Th...
5
2014-05-14T19:05:08Z
[ "python", "django", "django-models", "proxy-classes" ]
Python properties: Two instances of variable?
2,218,989
<p>Really confused about what's going on here. I have a class defined as follows:</p> <pre><code>class Profile(models.Model): user = models.OneToOneField(User) primary_phone = models.CharField(max_length=20) address = models.ForeignKey(Address) @property def primary_email(self): return self.user.e...
1
2010-02-08T00:33:55Z
2,219,000
<p>I'm not a Django user, but I'd guess it's because you didn't update the model after changing <code>u.email</code>. Try calling <code>u.save()</code> (or whatever the method happens to be called) before accessing the user's email through the profile.</p> <p>You can use Django's <a href="http://docs.djangoproject.com...
2
2010-02-08T00:41:10Z
[ "python", "django" ]
Python properties: Two instances of variable?
2,218,989
<p>Really confused about what's going on here. I have a class defined as follows:</p> <pre><code>class Profile(models.Model): user = models.OneToOneField(User) primary_phone = models.CharField(max_length=20) address = models.ForeignKey(Address) @property def primary_email(self): return self.user.e...
1
2010-02-08T00:33:55Z
2,220,569
<p>Python properties per se <a href="http://code.djangoproject.com/ticket/3148" rel="nofollow">do not work</a> in django models because django's models do some magic to set instance attributes. Maybe this is having an effect.</p>
2
2010-02-08T09:29:56Z
[ "python", "django" ]
Pylons: Webhelpers: missing secure_form module
2,219,316
<p>I installed Pylons 0.9.7 using the go-pylons.py script.<br> I have a line of python: </p> <pre><code>from webhelpers.html.secure_form import secure_form </code></pre> <p>When I try to serve my application I get the error: no module secure_form.</p> <p>I've tried writing import webhelpers.html.tags and other modu...
2
2010-02-08T02:58:22Z
2,219,475
<p>ugh, so for some reason I have 1.0b4 of webhelpers installed and the path to secure_form changed... (http://groups.google.com/group/pylons-discuss/msg/695d73b831a4aee3) I guess my question now becomes: how do I install a previous version of webhelpers? I have easy_install</p>
1
2010-02-08T03:53:32Z
[ "python", "module", "pylons" ]
Pylons: Webhelpers: missing secure_form module
2,219,316
<p>I installed Pylons 0.9.7 using the go-pylons.py script.<br> I have a line of python: </p> <pre><code>from webhelpers.html.secure_form import secure_form </code></pre> <p>When I try to serve my application I get the error: no module secure_form.</p> <p>I've tried writing import webhelpers.html.tags and other modu...
2
2010-02-08T02:58:22Z
2,219,493
<p>if your webhelpers version is 1.0b4 or above, secure_form is under webhelpers.pylonslib, ie.</p> <pre><code>from webhelpers.pylonslib import secure_form </code></pre>
7
2010-02-08T03:58:40Z
[ "python", "module", "pylons" ]
Pylons: Webhelpers: missing secure_form module
2,219,316
<p>I installed Pylons 0.9.7 using the go-pylons.py script.<br> I have a line of python: </p> <pre><code>from webhelpers.html.secure_form import secure_form </code></pre> <p>When I try to serve my application I get the error: no module secure_form.</p> <p>I've tried writing import webhelpers.html.tags and other modu...
2
2010-02-08T02:58:22Z
2,335,731
<p>I just ran into this as well.</p> <p>In case the other posts aren't obvious to get the old version of webhelpers you can run:</p> <pre><code>easy_install webhelpers==0.6.4 </code></pre>
2
2010-02-25T16:39:28Z
[ "python", "module", "pylons" ]
Pylons: Webhelpers: missing secure_form module
2,219,316
<p>I installed Pylons 0.9.7 using the go-pylons.py script.<br> I have a line of python: </p> <pre><code>from webhelpers.html.secure_form import secure_form </code></pre> <p>When I try to serve my application I get the error: no module secure_form.</p> <p>I've tried writing import webhelpers.html.tags and other modu...
2
2010-02-08T02:58:22Z
3,251,972
<p>Actually looking at this, I got something similar and the proper tested line of import is:</p> <p>from webhelpers.pylonslib.secure_form import secure_form</p>
1
2010-07-15T01:58:51Z
[ "python", "module", "pylons" ]
Independent instances of 'random'
2,219,436
<p>The below code attempts to illustrate what I want. I basically want two instances of "random" that operate independently of each other. I want to seed "random" within one class without affecting "random" in another class. How can I do that?</p> <pre><code>class RandomSeeded: def __init__(self, seed): im...
6
2010-02-08T03:42:33Z
2,219,470
<p>Class <code>random.Random</code> exists specifically to allow the behavior you want -- modules are intrinsically singletons, but classes are meant to be multiply instantiated, so both kinds of needs are covered.</p> <p>Should you ever need an independent copy of a module (which you definitely don't in the case of <...
12
2010-02-08T03:51:36Z
[ "python", "class", "random", "module", "seed" ]
Independent instances of 'random'
2,219,436
<p>The below code attempts to illustrate what I want. I basically want two instances of "random" that operate independently of each other. I want to seed "random" within one class without affecting "random" in another class. How can I do that?</p> <pre><code>class RandomSeeded: def __init__(self, seed): im...
6
2010-02-08T03:42:33Z
2,219,525
<p>For the seeded random numbers, make your own instance of <code>random.Random</code>. The <a href="http://docs.python.org/library/random.html"><code>random</code> documentation</a> explains this class, which the module depends on a single instance of when you use the functions directly within it.</p>
5
2010-02-08T04:08:43Z
[ "python", "class", "random", "module", "seed" ]
Independent instances of 'random'
2,219,436
<p>The below code attempts to illustrate what I want. I basically want two instances of "random" that operate independently of each other. I want to seed "random" within one class without affecting "random" in another class. How can I do that?</p> <pre><code>class RandomSeeded: def __init__(self, seed): im...
6
2010-02-08T03:42:33Z
2,221,769
<p>Sadly, having two independent RNG's is can be less random than having a single RNG using an "offset" into the generated sequence. </p> <p>Using an "offset" means you have to generate both complete sequences of samples, and then use them for your simulation. Something like this.</p> <pre><code>def makeSequences( ...
2
2010-02-08T13:14:06Z
[ "python", "class", "random", "module", "seed" ]
Matplotlib turn off antialias for text in plot?
2,219,503
<p>Is there any way to turn off antialias for all text in a plot, especially the ticklabels?</p>
2
2010-02-08T04:01:57Z
2,220,130
<p>Did you try playing with the font/text properties of matplotlibrc ? That would be the first thing to try, I think (see here for examples: <a href="http://matplotlib.sourceforge.net/users/customizing.html" rel="nofollow">http://matplotlib.sourceforge.net/users/customizing.html</a>)</p>
0
2010-02-08T07:32:36Z
[ "python", "matplotlib", "antialiasing" ]
Matplotlib turn off antialias for text in plot?
2,219,503
<p>Is there any way to turn off antialias for all text in a plot, especially the ticklabels?</p>
2
2010-02-08T04:01:57Z
2,223,165
<p>It seems this is not possible. Some classes such as Line2D have a "set_antialiased" method, but Text lacks this. I suggest you file a feature request on the Sourceforge tracker, and send an email to the matplotlib mailing list mentioning the request.</p>
0
2010-02-08T16:41:10Z
[ "python", "matplotlib", "antialiasing" ]
Matplotlib turn off antialias for text in plot?
2,219,503
<p>Is there any way to turn off antialias for all text in a plot, especially the ticklabels?</p>
2
2010-02-08T04:01:57Z
2,223,527
<p>I believe the anti-aliasing of Text objects is up to the font engine being used. <a href="http://matplotlib.sourceforge.net/users/text_intro.html" rel="nofollow">It seems the freetype2 fonts support this.</a> </p>
0
2010-02-08T17:31:51Z
[ "python", "matplotlib", "antialiasing" ]
Sizing controls for items in wx.lib.CustomTreeCtrl
2,219,514
<p>Is there a trick to sizing controls for wx.lib.CUstomTreeCtrl? I've been trying to create my own custom controls (just panels with sub-controls in them) and add them as items in my CustomTreeCtrl, but when the tree renders, it's as if the panels aren't expanded to the appropriate size. I can set the panel size man...
0
2010-02-08T04:05:24Z
2,309,469
<p>Can you put a self contained sample code and we can try to fix that.</p> <p>Have you tried adding custom items which return correct height for your panel?</p>
0
2010-02-22T07:40:39Z
[ "python", "wxpython" ]
System V shared memory in Python?
2,219,604
<p>How can I make use of the <code>shmat()</code>, <code>shmdt()</code>, <code>shmctl()</code>, <code>shmget()</code> calls from Python? Are they hidden somewhere in the standard library?</p> <h2>Update0</h2> <p>I'm after System V bindings that can be found in the Ubuntu repositories, or Python standard libraries (no...
8
2010-02-08T04:37:51Z
2,219,614
<p>Google finds <a href="http://semanchuk.com/philip/sysv_ipc/" rel="nofollow"><code>sysv_ipc</code></a>.</p>
2
2010-02-08T04:41:38Z
[ "python", "ipc", "sysv", "shared-memory" ]
System V shared memory in Python?
2,219,604
<p>How can I make use of the <code>shmat()</code>, <code>shmdt()</code>, <code>shmctl()</code>, <code>shmget()</code> calls from Python? Are they hidden somewhere in the standard library?</p> <h2>Update0</h2> <p>I'm after System V bindings that can be found in the Ubuntu repositories, or Python standard libraries (no...
8
2010-02-08T04:37:51Z
2,219,623
<p><a href="http://semanchuk.com/philip/PythonIpc/" rel="nofollow">This page</a> offers a feature matrix to help you choose between the <a href="http://semanchuk.com/philip/posix_ipc/" rel="nofollow">posix_ipc</a>, <a href="http://semanchuk.com/philip/sysv_ipc/" rel="nofollow">sysv_ipc</a>, and <a href="http://nikitath...
3
2010-02-08T04:43:56Z
[ "python", "ipc", "sysv", "shared-memory" ]
System V shared memory in Python?
2,219,604
<p>How can I make use of the <code>shmat()</code>, <code>shmdt()</code>, <code>shmctl()</code>, <code>shmget()</code> calls from Python? Are they hidden somewhere in the standard library?</p> <h2>Update0</h2> <p>I'm after System V bindings that can be found in the Ubuntu repositories, or Python standard libraries (no...
8
2010-02-08T04:37:51Z
3,809,996
<p>If you don't want to use any non-standard Python libraries, perhaps you could wrap the functions you need yourself using <a href="http://docs.python.org/library/ctypes.html" rel="nofollow">ctypes</a>?</p>
3
2010-09-28T05:43:17Z
[ "python", "ipc", "sysv", "shared-memory" ]
System V shared memory in Python?
2,219,604
<p>How can I make use of the <code>shmat()</code>, <code>shmdt()</code>, <code>shmctl()</code>, <code>shmget()</code> calls from Python? Are they hidden somewhere in the standard library?</p> <h2>Update0</h2> <p>I'm after System V bindings that can be found in the Ubuntu repositories, or Python standard libraries (no...
8
2010-02-08T04:37:51Z
4,488,283
<p>The <a href="http://pypi.python.org/pypi/processing" rel="nofollow">processing</a> package also supports shared memory objects, and works on unix/mac/windows.</p>
1
2010-12-20T09:20:22Z
[ "python", "ipc", "sysv", "shared-memory" ]
Does the Python standard library contain a module for manipulating URIs?
2,219,827
<p>I'd like to pass a URI to a constructor and get back an object on which I can call obj.type, obj.host, obj.port, etc. The "Request" object of the urllib2 module is close to what I need, but not quite it.</p>
1
2010-02-08T05:49:47Z
2,219,853
<p>Maybe something like the <a href="http://docs.python.org/library/urlparse.html#module-urlparse" rel="nofollow"><code>urlparse</code> module</a>? </p> <blockquote> <p>The <code>urlparse</code> module is renamed to <code>urllib.parse</code> in Python 3.0.</p> </blockquote> <p>From the doc:</p> <pre><code>&gt;&gt...
6
2010-02-08T05:56:56Z
[ "python", "url", "string" ]
Getting PHP to run a Python script
2,219,982
<p>I am trying to run a Python program using PHP. Here's the code</p> <pre><code>$command = '/usr/local/bin/python script.py file'; $temp = exec($command, $output); </code></pre> <p>This works through the command line but not while running it through the browser. I am using Apache so probably it needs the right privi...
12
2010-02-08T06:41:41Z
2,219,992
<p>Few checkpoints</p> <ul> <li>script.py should be pass full path, eg, <code>/home/abhinav/script.py</code></li> <li>script.py should be executable, <code>chmod +x script.py</code></li> </ul>
2
2010-02-08T06:44:49Z
[ "php", "python", "exec" ]
Getting PHP to run a Python script
2,219,982
<p>I am trying to run a Python program using PHP. Here's the code</p> <pre><code>$command = '/usr/local/bin/python script.py file'; $temp = exec($command, $output); </code></pre> <p>This works through the command line but not while running it through the browser. I am using Apache so probably it needs the right privi...
12
2010-02-08T06:41:41Z
2,220,014
<p>You need to pass the full path to the script and you also need to make sure that the script is readable by the user running the web server (which means every directory in the path must be +x to the web user).</p>
6
2010-02-08T06:49:46Z
[ "php", "python", "exec" ]
Getting PHP to run a Python script
2,219,982
<p>I am trying to run a Python program using PHP. Here's the code</p> <pre><code>$command = '/usr/local/bin/python script.py file'; $temp = exec($command, $output); </code></pre> <p>This works through the command line but not while running it through the browser. I am using Apache so probably it needs the right privi...
12
2010-02-08T06:41:41Z
2,251,957
<p>Realized what was wrong:</p> <ol> <li><p>The domain was set up as a virtual host and PHP's safe_mode was enabled. proc_open, exec, system, passthru etc. do not work under safe_mode I guess.</p></li> <li><p>Put the script in the directory accessible by the vhost. Apache wasn't able to access the directories outside ...
6
2010-02-12T13:13:18Z
[ "php", "python", "exec" ]
Getting PHP to run a Python script
2,219,982
<p>I am trying to run a Python program using PHP. Here's the code</p> <pre><code>$command = '/usr/local/bin/python script.py file'; $temp = exec($command, $output); </code></pre> <p>This works through the command line but not while running it through the browser. I am using Apache so probably it needs the right privi...
12
2010-02-08T06:41:41Z
14,463,905
<p>To get my CodeIgniter helper to run a python script, I had to put #!/usr/bin/python on the first line of my python script, and NOT call python from the exec.</p> <p>+1 for also doing chmod +x</p> <p>My helper looks like this:</p> <pre><code>&lt;?php function scrape($site, $key, $user_id) { $cmd = ...
0
2013-01-22T16:57:17Z
[ "php", "python", "exec" ]
How to know all the derived classes of a parent?
2,219,998
<p>Suppose you have a base class A, and this class is reimplemented by B and C. Suppose also there's a class method <code>A.derived()</code> that tells you which classes are reimplementing A, hence returns [B, C], and if you later on have <code>class D(A): pass</code> or <code>class D(B): pass</code>, now <code>A.deriv...
14
2010-02-08T06:46:38Z
2,220,121
<p>If you define your classes as a new-style class (subclass of <code>object</code>) then this is possible since the subclasses are saved in <code>__subclasses__</code>.</p> <pre><code>class A(object): def hello(self): print "Hello A" class B(A): def hello(self): print "Hello B" &gt;&gt;&gt; for cls in A.__su...
13
2010-02-08T07:28:40Z
[ "python" ]
How to know all the derived classes of a parent?
2,219,998
<p>Suppose you have a base class A, and this class is reimplemented by B and C. Suppose also there's a class method <code>A.derived()</code> that tells you which classes are reimplementing A, hence returns [B, C], and if you later on have <code>class D(A): pass</code> or <code>class D(B): pass</code>, now <code>A.deriv...
14
2010-02-08T06:46:38Z
2,220,737
<p>Given the discussion on <strong>subclasses</strong> then an implementation might look like this:</p> <pre><code>class A(object): @classmethod def derived(cls): return [c.__name__ for c in cls.__subclasses__()] </code></pre> <p><strong>edit</strong>: you might also want to look at this <a href="http...
3
2010-02-08T10:04:17Z
[ "python" ]
How to know all the derived classes of a parent?
2,219,998
<p>Suppose you have a base class A, and this class is reimplemented by B and C. Suppose also there's a class method <code>A.derived()</code> that tells you which classes are reimplementing A, hence returns [B, C], and if you later on have <code>class D(A): pass</code> or <code>class D(B): pass</code>, now <code>A.deriv...
14
2010-02-08T06:46:38Z
2,220,969
<p>Here is another implementation that will recursively print out all subclasses, with indentation.</p> <pre> def findsubclass(baseclass, indent=0): if indent == 0: print "Subclasses of %s are:" % baseclass.__name__ indent = indent + 1 for c in baseclass.__subclasses__(): print "-"*indent*4...
2
2010-02-08T10:43:27Z
[ "python" ]
perror equivalent function in python
2,220,038
<p>I'm using try except block in python, while the try block fails , how to print meaningful error message. I'm looking for something like perror() in C </p>
3
2010-02-08T06:57:28Z
2,220,059
<pre><code>&gt;&gt;&gt; try: ... 0/0 ... except Exception,e: ... print e.message ... integer division or modulo by zero </code></pre> <p>or in Python 2.6 and above, <code>e.args</code>, because of <code>BaseException.message has been deprecated</code></p> <pre><code>&gt;&gt;&gt; try: ... 0/0 ... except Ex...
4
2010-02-08T07:02:09Z
[ "python", "try-catch" ]
perror equivalent function in python
2,220,038
<p>I'm using try except block in python, while the try block fails , how to print meaningful error message. I'm looking for something like perror() in C </p>
3
2010-02-08T06:57:28Z
2,220,668
<pre><code>try: pass except Exception, err: print err </code></pre>
-2
2010-02-08T09:51:36Z
[ "python", "try-catch" ]
Simple example of gang scheduling of processes in Python?
2,220,083
<p>I have a pool of processes that need to be executed. I would like to fully utilize the machine, so that all CPUs are executing processes. I do not want to over-subscribe the system, so what i really want is #executing_processes=#cpus at any given moment. I also need to store the stdout,stderr and return code of eac...
3
2010-02-08T07:11:06Z
2,220,103
<p>If you are talking about <em>your own, Python-implemented</em> processes:</p> <p>The <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow"><strong><code>multiprocessing</code></strong></a> module gives you the ability to spawn multiple processes. In particular, it sounds like you would want ...
2
2010-02-08T07:18:57Z
[ "python", "multiprocessing" ]
python tuple division
2,220,099
<p>TypeError: unsupported operand type(s) for /: 'tuple' and 'tuple'</p> <p>I'm getting above error , while I fetched a record using query "select max(rowid) from table" and assigned it to variable and while performing / operation is throws above message. How to resolve this.</p>
1
2010-02-08T07:18:15Z
2,220,107
<p>Sql query <code>select max(rowid)</code> would return Tuple data like <code>records=(1000,)</code></p> <p>You may need to do like <code>numerator / records[0]</code></p>
4
2010-02-08T07:22:20Z
[ "python", "tuples" ]
Catching errors when logging with SocketHandler in Python
2,220,159
<p>My web application runs on multpile apache instances and I am having multiprocess logging issues because of this. I am currently using a SocketHandler for logging to a daemon using SocketServer that then writes logs to a single log file (<a href="http://docs.python.org/library/logging.html#logging-to-a-single-file-...
2
2010-02-08T07:40:01Z
2,220,262
<p>There is no way to do that with the current resilient implementation. "Resilient" means in this case that the <code>SocketHandler</code> will handle problems with the socket already by trying to reopen it as soon as there is a problem.</p> <p>Just restart the server and the handlers will reconnect eventually.</p>
1
2010-02-08T08:15:28Z
[ "python", "sockets", "exception-handling", "mod-python" ]
Catching errors when logging with SocketHandler in Python
2,220,159
<p>My web application runs on multpile apache instances and I am having multiprocess logging issues because of this. I am currently using a SocketHandler for logging to a daemon using SocketServer that then writes logs to a single log file (<a href="http://docs.python.org/library/logging.html#logging-to-a-single-file-...
2
2010-02-08T07:40:01Z
2,220,356
<p>When a socket creation operation fails (e.g. because there is no server listening), the default behaviour is to retry the next time an event is logged, with an exponential back-off algorithm. Here are some approaches you could try:</p> <ol> <li>Subclass <code>SocketHandler</code>, override the <code>createSocket</c...
3
2010-02-08T08:40:17Z
[ "python", "sockets", "exception-handling", "mod-python" ]
delete xml node using lxml
2,220,185
<pre><code>&lt;login&gt; &lt;user&gt; &lt;userid&gt;admin&lt;/userid&gt; &lt;/user&gt; . . . . &lt;user&gt; &lt;userid&gt;admin&lt;/userid&gt; &lt;/user&gt; &lt;/login&gt; </code></pre> <p>this my xml file. when i user clear()or del method it will clear all the child and a blank node is creati...
4
2010-02-08T07:52:40Z
2,220,208
<p><a href="http://codespeak.net/lxml/api/lxml.etree._Element-class.html#remove" rel="nofollow">Removing</a> match user node from parent Node would be suitable for this case.</p>
4
2010-02-08T08:01:07Z
[ "python", "xml", "lxml" ]
writing to data to excel
2,220,351
<p>I have data that I need to export to excel, I just don't know how to go about it, here's the view I'm using, I've commented out my attempts.A push to the right direction will be greatly appreciated.</p> <pre><code>def month_end(request): """ A simple view that will generate a month end report as a PDF respo...
1
2010-02-08T08:39:11Z
2,220,402
<p>Have a look to xlwt <a href="http://pypi.python.org/pypi/xlwt" rel="nofollow">http://pypi.python.org/pypi/xlwt</a></p>
4
2010-02-08T08:51:40Z
[ "python", "django", "csv" ]
writing to data to excel
2,220,351
<p>I have data that I need to export to excel, I just don't know how to go about it, here's the view I'm using, I've commented out my attempts.A push to the right direction will be greatly appreciated.</p> <pre><code>def month_end(request): """ A simple view that will generate a month end report as a PDF respo...
1
2010-02-08T08:39:11Z
2,220,430
<p>You could directly write CSV from MySQL records,</p> <pre><code>import csv csv_writer = csv.writer(open(FILENAME,'w'), delimiter=',',quotechar="'") data = cursor.fetchall() for row in data: csv_writer.writerow(row) </code></pre> <p>Full example at</p> <p><a href="http://snipplr.com/view/11970/simple-csv-dump...
4
2010-02-08T08:57:07Z
[ "python", "django", "csv" ]
What would I use Stackless Python for?
2,220,645
<p>There are many questions related to Stackless Python. But none answering this my question, I think (correct me if wrong - please!). There's some buzz about it all the time so I curious to know. What would I use Stackless for? How is it better than CPython?</p> <p>Yes it has green threads (stackless) that allow quic...
38
2010-02-08T09:48:12Z
2,220,658
<p>EVEOnline is largely programmed in Stackless Python. They have several dev blogs on the use of it. It seems it is very useful for high performance computing.</p>
6
2010-02-08T09:51:05Z
[ "python", "python-stackless" ]
What would I use Stackless Python for?
2,220,645
<p>There are many questions related to Stackless Python. But none answering this my question, I think (correct me if wrong - please!). There's some buzz about it all the time so I curious to know. What would I use Stackless for? How is it better than CPython?</p> <p>Yes it has green threads (stackless) that allow quic...
38
2010-02-08T09:48:12Z
2,221,057
<p>Stackless Python's main benefit is the support for very lightweight coroutines. CPython doesn't support coroutines natively (although I expect someone to post a generator-based hack in the comments) so Stackless is a clear improvement on CPython when you have a problem that benefits from coroutines. </p> <p>I think...
12
2010-02-08T10:58:20Z
[ "python", "python-stackless" ]
What would I use Stackless Python for?
2,220,645
<p>There are many questions related to Stackless Python. But none answering this my question, I think (correct me if wrong - please!). There's some buzz about it all the time so I curious to know. What would I use Stackless for? How is it better than CPython?</p> <p>Yes it has green threads (stackless) that allow quic...
38
2010-02-08T09:48:12Z
2,249,019
<p>It allows you to work with massive amounts of concurrency. Nobody sane would create one hundred thousand system threads, but you can do this using stackless.</p> <p>This article tests doing just that, creating one hundred thousand tasklets in both Python and Google Go (a new programming language): <a href="http://d...
27
2010-02-12T01:15:33Z
[ "python", "python-stackless" ]
What would I use Stackless Python for?
2,220,645
<p>There are many questions related to Stackless Python. But none answering this my question, I think (correct me if wrong - please!). There's some buzz about it all the time so I curious to know. What would I use Stackless for? How is it better than CPython?</p> <p>Yes it has green threads (stackless) that allow quic...
38
2010-02-08T09:48:12Z
2,251,860
<p>Thirler already mentioned that stackless was used in Eve Online. Keep in mind, that:</p> <blockquote> <p>(..) stackless adds a further twist to this by allowing tasks to be separated into smaller tasks, Tasklets, which can then be split off the main program to execute on their own. This can be used for fire-and-...
8
2010-02-12T12:50:27Z
[ "python", "python-stackless" ]
What would I use Stackless Python for?
2,220,645
<p>There are many questions related to Stackless Python. But none answering this my question, I think (correct me if wrong - please!). There's some buzz about it all the time so I curious to know. What would I use Stackless for? How is it better than CPython?</p> <p>Yes it has green threads (stackless) that allow quic...
38
2010-02-08T09:48:12Z
2,276,236
<p>The basic usefulness for green threads, the way I see it, is to implement a system in which you have a large amount of objects that do high latency operations. A concrete example would be communicating with other machines:</p> <pre><code>def Run(): # Do stuff request_information() # This call might block ...
5
2010-02-16T20:50:31Z
[ "python", "python-stackless" ]
What would I use Stackless Python for?
2,220,645
<p>There are many questions related to Stackless Python. But none answering this my question, I think (correct me if wrong - please!). There's some buzz about it all the time so I curious to know. What would I use Stackless for? How is it better than CPython?</p> <p>Yes it has green threads (stackless) that allow quic...
38
2010-02-08T09:48:12Z
2,279,081
<p>While I've not used Stackless itself, I have used Greenlet for implementing highly-concurrent network applications. Some of the use cases Linden Lab has put it towards are: high-performance smart proxies, a fast system for distributing commands over huge numbers of machines, and an application that does a ton of da...
6
2010-02-17T07:50:37Z
[ "python", "python-stackless" ]
Does Django have `__not_equal`?
2,220,682
<p>Does Django have a field lookup like <code>__not_equal</code>? (Field lookups are <code>__exact</code>, <code>__contains</code>, etc.)</p>
1
2010-02-08T09:53:47Z
2,220,700
<p>Use one of <code>__exact</code>, <code>__contains</code> etc. with <a href="http://docs.djangoproject.com/en/1.1/ref/models/querysets/#exclude-kwargs" rel="nofollow"><code>exclude()</code></a> instead of <code>filter()</code>.</p>
5
2010-02-08T09:56:57Z
[ "python", "django", "orm" ]
Does Django have `__not_equal`?
2,220,682
<p>Does Django have a field lookup like <code>__not_equal</code>? (Field lookups are <code>__exact</code>, <code>__contains</code>, etc.)</p>
1
2010-02-08T09:53:47Z
2,220,743
<p>You can also use the <a href="http://docs.djangoproject.com/en/1.1/topics/db/queries/#complex-lookups-with-q-objects" rel="nofollow"><code>Q</code> object</a> and negate it.</p> <p>E.g.</p> <pre><code>Poll.objects.filter(~Q(question='Who')) </code></pre>
3
2010-02-08T10:05:58Z
[ "python", "django", "orm" ]
What's the difference between eval, exec, and compile in Python?
2,220,699
<p>I've been looking at dynamic evaluation of Python code, and come across the <code>eval()</code> and <code>compile()</code> functions, and the <code>exec</code> statement.</p> <p>Can someone please explain the difference between <code>eval</code> and <code>exec</code>, and how the different modes of <code>compile()<...
231
2010-02-08T09:56:43Z
2,220,790
<ol> <li><p><code>exec</code> is not an expression: a statement in Python 2.x, and a function in Python 3.x. It compiles and immediately evaluates a statement or set of statement contained in a string. Example:</p> <pre><code>exec('print(5)') # prints 5. # exec 'print 5' if you use Python 2.x, nor the ex...
175
2010-02-08T10:13:31Z
[ "python", "dynamic", "eval", "exec" ]
What's the difference between eval, exec, and compile in Python?
2,220,699
<p>I've been looking at dynamic evaluation of Python code, and come across the <code>eval()</code> and <code>compile()</code> functions, and the <code>exec</code> statement.</p> <p>Can someone please explain the difference between <code>eval</code> and <code>exec</code>, and how the different modes of <code>compile()<...
231
2010-02-08T09:56:43Z
14,097,282
<p>exec is for statement and does not return anything. eval is for expression and returns value of expression.</p> <p>expression means "something" while statement means "do something".</p>
42
2012-12-31T04:02:10Z
[ "python", "dynamic", "eval", "exec" ]
What's the difference between eval, exec, and compile in Python?
2,220,699
<p>I've been looking at dynamic evaluation of Python code, and come across the <code>eval()</code> and <code>compile()</code> functions, and the <code>exec</code> statement.</p> <p>Can someone please explain the difference between <code>eval</code> and <code>exec</code>, and how the different modes of <code>compile()<...
231
2010-02-08T09:56:43Z
29,456,463
<h1>The short answer, or TL;DR</h1> <p>Basically, <a href="https://docs.python.org/3/library/functions.html#eval"><code>eval</code></a> is used to <strong>eval</strong>uate a single dynamically generated Python expression, and <a href="https://docs.python.org/3/library/functions.html#exec"><code>exec</code></a> is use...
158
2015-04-05T10:44:44Z
[ "python", "dynamic", "eval", "exec" ]
Rasterizing a GDAL layer
2,220,749
<p><strong>Edit</strong></p> <p>Here is the proper way to do it, and the <a href="http://gdal.org/gdal__alg_8h.html#dfe5e5d287d6c184aab03acbfa567cb1">documentation</a>:</p> <pre><code>import random from osgeo import gdal, ogr RASTERIZE_COLOR_FIELD = "__color__" def rasterize(pixel_size=25) # Open the data s...
22
2010-02-08T10:06:43Z
2,220,980
<p>EDIT: I guess I'd use qGIS python bindings: <a href="http://www.qgis.org/wiki/Python_Bindings">http://www.qgis.org/wiki/Python_Bindings</a></p> <p>That's the easiest way I can think of. I remember hand rolling something before, but it's ugly. qGIS would be easier, even if you had to make a separate Windows installa...
8
2010-02-08T10:45:45Z
[ "python", "gis", "gdal", "rasterizing" ]
Django: Can't set ForeignKey value to None from admin
2,220,838
<p>I have a model Category which has a ForeignKey to a SimplePage model. <code>null</code> and <code>blank</code> are set to True. The problem is, when I edit a Category from the admin interface, I can't change the ForeignKey to <code>---------</code> (Which looks like the admin's way of saying None.) The value can be ...
0
2010-02-08T10:20:26Z
2,235,688
<p>Maybe that's because you have OneToOne relationship defined also in category and you can't therefore break this relationship. Try to remove it and see, if you can set category in SimplePage to None.</p>
2
2010-02-10T09:31:36Z
[ "python", "django", "orm" ]
Django: Can't set ForeignKey value to None from admin
2,220,838
<p>I have a model Category which has a ForeignKey to a SimplePage model. <code>null</code> and <code>blank</code> are set to True. The problem is, when I edit a Category from the admin interface, I can't change the ForeignKey to <code>---------</code> (Which looks like the admin's way of saying None.) The value can be ...
0
2010-02-08T10:20:26Z
2,235,848
<p>gruszczy above is right, each side of o2o relation cannot be null after it was assigned, because django won't let you have orphan object in DB.</p>
0
2010-02-10T09:57:12Z
[ "python", "django", "orm" ]
Equivalent functionality to Google Charts API from python ? (Venn Diagrams also needed!)
2,220,929
<p>is there any library (for C or python) that I can use to get roughly the same functionality as I can get from Google Charts ?</p> <p>I specifically need the pie diagrams (standard), multi-dataset-pie-diagrams (not-so-standard), and venn diagrams (rare)...</p>
0
2010-02-08T10:37:24Z
2,220,972
<p><a href="http://pygooglechart.slowchop.com/" rel="nofollow">http://pygooglechart.slowchop.com/</a> is a Python wrapper for the Google Charts API. Also see <a href="http://stackoverflow.com/questions/52652/pretty-graphs-and-charts-in-python">http://stackoverflow.com/questions/52652/pretty-graphs-and-charts-in-python<...
1
2010-02-08T10:43:51Z
[ "python", "charts" ]
Python: Setting an element of a Numpy matrix
2,220,968
<p>I am a pretty new to python. I have created an empty matrix</p> <pre><code>a = numpy.zeros(shape=(n,n)) </code></pre> <p>Now I can access each element using </p> <pre><code>a.item(i,j) </code></pre> <p>How do I set an index (i,j)?</p>
12
2010-02-08T10:43:24Z
2,220,983
<p>Here's how:</p> <pre><code>a[i,j] = x </code></pre>
28
2010-02-08T10:46:52Z
[ "python" ]
Python: Setting an element of a Numpy matrix
2,220,968
<p>I am a pretty new to python. I have created an empty matrix</p> <pre><code>a = numpy.zeros(shape=(n,n)) </code></pre> <p>Now I can access each element using </p> <pre><code>a.item(i,j) </code></pre> <p>How do I set an index (i,j)?</p>
12
2010-02-08T10:43:24Z
2,220,992
<p>Try this</p> <pre><code>a[i,j]=5 </code></pre>
1
2010-02-08T10:48:22Z
[ "python" ]
Python: Setting an element of a Numpy matrix
2,220,968
<p>I am a pretty new to python. I have created an empty matrix</p> <pre><code>a = numpy.zeros(shape=(n,n)) </code></pre> <p>Now I can access each element using </p> <pre><code>a.item(i,j) </code></pre> <p>How do I set an index (i,j)?</p>
12
2010-02-08T10:43:24Z
2,221,017
<p>Or</p> <pre><code>a.itemset((i,j),x) </code></pre>
6
2010-02-08T10:52:25Z
[ "python" ]
Logging in and using cookies in pycurl
2,221,191
<p>I need to download a file that is on a password protected page. To get to the page manually I first have to authenticate via an ordinary login page. I want to use curl to fetch this page in script.<br> My script first logins. It appears to succeed--it returns a 200 from a PUT to /login. However, the fetch of the ...
6
2010-02-08T11:24:04Z
2,335,107
<p>I believe Curl will store the cookies but you need to use them explicitly. I've only ever used the command line interface for this though. Scanning the documentation I think you might want to try:</p> <pre><code>C.setopt(pycurl.COOKIEFILE, 'cookie.txt') </code></pre> <p>(before the second request)</p>
10
2010-02-25T15:21:43Z
[ "python", "curl", "pycurl" ]
Logging in and using cookies in pycurl
2,221,191
<p>I need to download a file that is on a password protected page. To get to the page manually I first have to authenticate via an ordinary login page. I want to use curl to fetch this page in script.<br> My script first logins. It appears to succeed--it returns a 200 from a PUT to /login. However, the fetch of the ...
6
2010-02-08T11:24:04Z
4,671,702
<p>wds is right on. </p> <p>for your further edification, the available options are based on those at <a href="http://curl.haxx.se/libcurl/c/curl_easy_setopt.html" rel="nofollow">http://curl.haxx.se/libcurl/c/curl_easy_setopt.html</a> (see the section on cookie shortcuts).</p> <p>a 500 is an internal server error.....
2
2011-01-12T17:11:18Z
[ "python", "curl", "pycurl" ]
Logging in and using cookies in pycurl
2,221,191
<p>I need to download a file that is on a password protected page. To get to the page manually I first have to authenticate via an ordinary login page. I want to use curl to fetch this page in script.<br> My script first logins. It appears to succeed--it returns a 200 from a PUT to /login. However, the fetch of the ...
6
2010-02-08T11:24:04Z
19,699,568
<p>You should store cookie first and then read from it:</p> <pre><code>C.setopt(pycurl.COOKIEJAR, 'cookie.txt') C.setopt(pycurl.COOKIEFILE, 'cookie.txt') </code></pre> <p>Here what curl --help returned:</p> <pre><code>-b, --cookie STRING/FILE String or file to read cookies from (H) -c, --cookie-jar FILE Write cook...
4
2013-10-31T06:12:39Z
[ "python", "curl", "pycurl" ]
Why doesn't this loop display an updated object count every five seconds?
2,221,247
<p>I use this python code to output the number of Things every 5 seconds:</p> <pre><code>def my_count(): while True: print "Number of Things: %d" % Thing.objects.count() time.sleep(5) my_count() </code></pre> <p>If another process generates a new Thing while my_count() is running, my_count(...
7
2010-02-08T11:34:29Z
2,221,400
<p>Because Python DB API is by default in AUTOCOMMIT=OFF mode, and (at least for MySQLdb) on REPEATABLE READ isolation level. This means that behind the scenes you have an ongoing database transaction (InnoDB is transactional engine) in which the first access to given row (or maybe even table, I'm not sure) fixes "view...
16
2010-02-08T12:05:49Z
[ "python", "mysql", "django" ]
Custom instance unpickling in Python: should the object dictionary be updated, or is replacing it OK?
2,221,279
<p>When defining how objects of a certain class should be unpickled, via <a href="http://docs.python.org/library/pickle.html#pickling-and-unpickling-normal-class-instances" rel="nofollow">__setstate__</a>, I gather that it is safe to do</p> <pre><code>def __setstate__(self, dict_returned_by_pickle): self.__dict__....
1
2010-02-08T11:42:33Z
2,221,338
<p>Replacing it is fine. That is what the <a href="http://code.activestate.com/recipes/66531/" rel="nofollow">borg pattern</a> does.</p> <p>The original dict will be garbage collected if there are no other references to it.</p>
0
2010-02-08T11:55:12Z
[ "python", "dictionary", "pickle" ]
Django admin inline form error
2,221,901
<p>I have an inline formset in my admin site. I also have save_as = True in admin.py. My models are, for example:</p> <pre><code>class Poll(models.Model): question = models.CharField(max_length=200, unique = True) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models....
1
2010-02-08T13:39:21Z
2,263,095
<p>I've never had much luck with Save as and relationships. Although, I think I was trying to do complicated many to many stuff.</p> <p>What is the url of the page that is giving that error... From the errors it looks like it would be something like.... <code>/admin/myapp/poll//</code> whereas it should be something ...
1
2010-02-14T22:34:16Z
[ "python", "django", "django-models" ]
Django admin inline form error
2,221,901
<p>I have an inline formset in my admin site. I also have save_as = True in admin.py. My models are, for example:</p> <pre><code>class Poll(models.Model): question = models.CharField(max_length=200, unique = True) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models....
1
2010-02-08T13:39:21Z
3,935,107
<p>I just had this error with a different part of the tutorial. </p> <p>Have you checked your templates for typos? I had a template that was supposed to pass <code>choice_id</code> and instead I was passing <code>choice_if</code> which was nonsense.</p>
0
2010-10-14T15:55:44Z
[ "python", "django", "django-models" ]
The ${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} syntax doesn't work in a Mako template
2,221,910
<p>In a <a href="http://en.wikipedia.org/wiki/Mako_%28template_engine%29" rel="nofollow">Mako</a> template, I need to do something like:</p> <pre><code>${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} </code></pre> <p>When A do that, I get this error:</p> <blockquote> <p>SyntaxException: (SyntaxError) unexpected EO...
0
2010-02-08T13:42:47Z
2,222,172
<p>A work-around is to pass the dict object in a different way. For example:</p> <pre><code>from mako.template import Template print Template("${'foo %(a)s bar %(b)s' % data}").render(data=dict(a='Alpha',b='Beta')) </code></pre>
0
2010-02-08T14:25:16Z
[ "python", "templates", "mako" ]
The ${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} syntax doesn't work in a Mako template
2,221,910
<p>In a <a href="http://en.wikipedia.org/wiki/Mako_%28template_engine%29" rel="nofollow">Mako</a> template, I need to do something like:</p> <pre><code>${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} </code></pre> <p>When A do that, I get this error:</p> <blockquote> <p>SyntaxException: (SyntaxError) unexpected EO...
0
2010-02-08T13:42:47Z
2,222,328
<p>Solution:</p> <pre><code>${'foo %(a)s bar %(b)s' % dict((('a', '1'), ('b', '2'),))} </code></pre>
-1
2010-02-08T14:47:03Z
[ "python", "templates", "mako" ]
How to refer to a method name from with a method in Python?
2,222,044
<p>Say I have the following class defined with the method <code>foo</code>:</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % __name__ </code></pre> <p>Now when I call <code>foo()</code> I expect/want to see this printed out </p> <pre><code>My name is foo </code></pre> <p>However I...
8
2010-02-08T14:05:28Z
2,222,092
<p>Names always refer to local variables or (if one doesn't exist) then global variables. There is a a global __name__ that has the module's name.</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % MyClass.foo.__name__ </code></pre> <p>Of course, that's redundant and almost entirely pointless...
4
2010-02-08T14:14:30Z
[ "python", "methods", "introspection" ]
How to refer to a method name from with a method in Python?
2,222,044
<p>Say I have the following class defined with the method <code>foo</code>:</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % __name__ </code></pre> <p>Now when I call <code>foo()</code> I expect/want to see this printed out </p> <pre><code>My name is foo </code></pre> <p>However I...
8
2010-02-08T14:05:28Z
2,222,101
<p><code>__name__</code> refers to the module because <a href="http://docs.python.org/tutorial/modules.html" rel="nofollow">that's what it's supposed to do</a>. The only way to get at the currently running function would be to introspect the stack.</p>
3
2010-02-08T14:15:45Z
[ "python", "methods", "introspection" ]
How to refer to a method name from with a method in Python?
2,222,044
<p>Say I have the following class defined with the method <code>foo</code>:</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % __name__ </code></pre> <p>Now when I call <code>foo()</code> I expect/want to see this printed out </p> <pre><code>My name is foo </code></pre> <p>However I...
8
2010-02-08T14:05:28Z
2,222,107
<p>This will do it:</p> <p>(You need to refer to <code>self.__class__._name__</code>.)</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % self.__class__.__name__ </code></pre>
-1
2010-02-08T14:16:38Z
[ "python", "methods", "introspection" ]
How to refer to a method name from with a method in Python?
2,222,044
<p>Say I have the following class defined with the method <code>foo</code>:</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % __name__ </code></pre> <p>Now when I call <code>foo()</code> I expect/want to see this printed out </p> <pre><code>My name is foo </code></pre> <p>However I...
8
2010-02-08T14:05:28Z
2,222,132
<p>This does what you're after:</p> <pre><code> from inspect import currentframe, getframeinfo class MyClass: def foo(self): print "My name is %s" % getframeinfo(currentframe())[2] </code></pre>
11
2010-02-08T14:20:04Z
[ "python", "methods", "introspection" ]
How to refer to a method name from with a method in Python?
2,222,044
<p>Say I have the following class defined with the method <code>foo</code>:</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % __name__ </code></pre> <p>Now when I call <code>foo()</code> I expect/want to see this printed out </p> <pre><code>My name is foo </code></pre> <p>However I...
8
2010-02-08T14:05:28Z
2,222,159
<p>Use introspection with the <a href="http://docs.python.org/library/inspect.html" rel="nofollow">inspect</a> module.</p> <pre><code>import inspect class MyClass: def foo(self): print "My name is %s" % inspect.stack()[0][3] </code></pre>
1
2010-02-08T14:23:24Z
[ "python", "methods", "introspection" ]
How to refer to a method name from with a method in Python?
2,222,044
<p>Say I have the following class defined with the method <code>foo</code>:</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % __name__ </code></pre> <p>Now when I call <code>foo()</code> I expect/want to see this printed out </p> <pre><code>My name is foo </code></pre> <p>However I...
8
2010-02-08T14:05:28Z
2,222,161
<p>The other answers explain it quite well so I contribute with a more concrete example.</p> <p><strong>name.py</strong></p> <pre><code>def foo(): print "name in foo",__name__ foo() print "foo's name",foo.__name__ print "name at top",__name__ </code></pre> <p><strong>Output</strong></p> <pre><code>name in foo ...
2
2010-02-08T14:23:40Z
[ "python", "methods", "introspection" ]
How to refer to a method name from with a method in Python?
2,222,044
<p>Say I have the following class defined with the method <code>foo</code>:</p> <pre><code>class MyClass: def foo(self): print "My name is %s" % __name__ </code></pre> <p>Now when I call <code>foo()</code> I expect/want to see this printed out </p> <pre><code>My name is foo </code></pre> <p>However I...
8
2010-02-08T14:05:28Z
2,222,184
<p>Have a look at the <a href="http://docs.python.org/library/inspect.html" rel="nofollow">the <code>inspect</code> module</a>.</p> <p>Try:</p> <pre><code>&gt;&gt;&gt; import inspect &gt;&gt;&gt; def foo(): ... print inspect.getframeinfo(inspect.currentframe())[2] ... &gt;&gt;&gt; foo() foo </code></pre> <p>or:<...
1
2010-02-08T14:26:19Z
[ "python", "methods", "introspection" ]
Printing to a file from a list of lists in Python
2,222,189
<p>I am trying to print to a file that will look like:</p> <pre><code>'A' '1' 'B' '2' 'C' '3' </code></pre> <p>Given the code below, however, the result is :</p> <pre><code>['A'] ['B'] ['C'] </code></pre> <p>This is probably a 'softball' question, but what am I doing wrong here?</p> <pre><code>l1 = ['1'] l2 = ['A...
1
2010-02-08T14:26:42Z
2,222,226
<p>Your code could be a lot simpler:</p> <pre><code>for list in listoflists: f.write(str(list)) f.write('\n') </code></pre> <p>But, this is going to print something like <code>['1']</code>. It seems like you want something more like:</p> <pre><code>for list in listoflists: f.write(str(list[0])) f.wr...
1
2010-02-08T14:31:52Z
[ "python", "file", "list", "iteration" ]
Printing to a file from a list of lists in Python
2,222,189
<p>I am trying to print to a file that will look like:</p> <pre><code>'A' '1' 'B' '2' 'C' '3' </code></pre> <p>Given the code below, however, the result is :</p> <pre><code>['A'] ['B'] ['C'] </code></pre> <p>This is probably a 'softball' question, but what am I doing wrong here?</p> <pre><code>l1 = ['1'] l2 = ['A...
1
2010-02-08T14:26:42Z
2,222,228
<p>First of all, don't use <code>iter</code> and <code>next()</code>, that's what <code>for</code> is for. Secondly, you are actually writing a list to the file, not its contents. So you could either print the first element of the list (i.e. <code>l1[0]</code>) or iterate through all the inner lists elements.</p> <p>Y...
7
2010-02-08T14:31:56Z
[ "python", "file", "list", "iteration" ]
Printing to a file from a list of lists in Python
2,222,189
<p>I am trying to print to a file that will look like:</p> <pre><code>'A' '1' 'B' '2' 'C' '3' </code></pre> <p>Given the code below, however, the result is :</p> <pre><code>['A'] ['B'] ['C'] </code></pre> <p>This is probably a 'softball' question, but what am I doing wrong here?</p> <pre><code>l1 = ['1'] l2 = ['A...
1
2010-02-08T14:26:42Z
2,222,272
<p>I think the best way to solve this is just with a basic nested loop. Try this:</p> <pre><code>l1 = ['1'] l2 = ['A'] l3 = ['2'] l4 = ['B'] l5 = ['3'] l6 = ['C'] listoflists = [l1,l2,l3,l4,l5,l6] f = open("out.txt","w") # for each list and # for each item in the list; # write the item to the file, separated by a co...
2
2010-02-08T14:39:04Z
[ "python", "file", "list", "iteration" ]
Printing to a file from a list of lists in Python
2,222,189
<p>I am trying to print to a file that will look like:</p> <pre><code>'A' '1' 'B' '2' 'C' '3' </code></pre> <p>Given the code below, however, the result is :</p> <pre><code>['A'] ['B'] ['C'] </code></pre> <p>This is probably a 'softball' question, but what am I doing wrong here?</p> <pre><code>l1 = ['1'] l2 = ['A...
1
2010-02-08T14:26:42Z
2,222,339
<p>Including the quotes in the output is a bit odd, but if you insist:</p> <pre><code>for entry in listoflists: print &gt;&gt;f, repr(entry[0]) </code></pre> <p>You don't specify what will happen if the inner list does not have just one element, so any other possibility is ignored here.</p>
0
2010-02-08T14:48:40Z
[ "python", "file", "list", "iteration" ]
Printing to a file from a list of lists in Python
2,222,189
<p>I am trying to print to a file that will look like:</p> <pre><code>'A' '1' 'B' '2' 'C' '3' </code></pre> <p>Given the code below, however, the result is :</p> <pre><code>['A'] ['B'] ['C'] </code></pre> <p>This is probably a 'softball' question, but what am I doing wrong here?</p> <pre><code>l1 = ['1'] l2 = ['A...
1
2010-02-08T14:26:42Z
2,222,409
<p>You can simply iterate through all list elements with <code>itertools.chain</code> (documented <a href="http://docs.python.org/library/itertools.html#itertools.chain" rel="nofollow">here</a>):</p> <pre><code>import itertools l1 = ['1'] l2 = ['A'] l3 = ['2'] l4 = ['B'] l5 = ['3'] l6 = ['C'] chainedlists = itertool...
0
2010-02-08T14:59:07Z
[ "python", "file", "list", "iteration" ]
Printing to a file from a list of lists in Python
2,222,189
<p>I am trying to print to a file that will look like:</p> <pre><code>'A' '1' 'B' '2' 'C' '3' </code></pre> <p>Given the code below, however, the result is :</p> <pre><code>['A'] ['B'] ['C'] </code></pre> <p>This is probably a 'softball' question, but what am I doing wrong here?</p> <pre><code>l1 = ['1'] l2 = ['A...
1
2010-02-08T14:26:42Z
2,222,554
<p>The simple reason why you are getting the wrong file contents is because you are calling <code>iter</code> twice. Lines 15-16 are:</p> <pre><code>itr.next() s = str(itr.next()) </code></pre> <p>For more Pythonic printing semantics, see the other answers</p>
1
2010-02-08T15:21:49Z
[ "python", "file", "list", "iteration" ]
Python: defining functions on the fly
2,222,466
<p>I have the following code:</p> <pre><code> funcs = [] for i in range(10): def func(): print i funcs.append(func) for f in funcs: f() </code></pre> <p>The problem is that <code>func</code> is being overriden. Ie the output of the code is:</p> <pre><code>9 9 9 ... </code></pre> <p>How would you s...
4
2010-02-08T15:06:26Z
2,222,486
<p>Returning <code>func</code> from another function <a href="http://code.activestate.com/recipes/502271/" rel="nofollow">is safest</a>.</p>
0
2010-02-08T15:10:54Z
[ "python", "lambda" ]
Python: defining functions on the fly
2,222,466
<p>I have the following code:</p> <pre><code> funcs = [] for i in range(10): def func(): print i funcs.append(func) for f in funcs: f() </code></pre> <p>The problem is that <code>func</code> is being overriden. Ie the output of the code is:</p> <pre><code>9 9 9 ... </code></pre> <p>How would you s...
4
2010-02-08T15:06:26Z
2,222,530
<p>The problem is not that func is being overwritten, it's that the value of <code>i</code> is being evaluated when the function is called, not when it is defined. If you want to evaluate <code>i</code> at definition time, put it in the function declaration, as a default argument to <code>func</code>. </p> <pre><cod...
13
2010-02-08T15:17:28Z
[ "python", "lambda" ]
Python: defining functions on the fly
2,222,466
<p>I have the following code:</p> <pre><code> funcs = [] for i in range(10): def func(): print i funcs.append(func) for f in funcs: f() </code></pre> <p>The problem is that <code>func</code> is being overriden. Ie the output of the code is:</p> <pre><code>9 9 9 ... </code></pre> <p>How would you s...
4
2010-02-08T15:06:26Z
2,226,361
<p>You could try </p> <pre><code>for i in range(10): def func(j=i): print j funcs.append(func) for f in funcs: f() </code></pre>
0
2010-02-09T02:30:20Z
[ "python", "lambda" ]
Python sort parallel arrays in place?
2,222,769
<p>Is there an easy (meaning without rolling one's own sorting function) way to sort parallel lists <strong>without unnecessary copying</strong> in Python? For example:</p> <pre><code>foo = range(5) bar = range(5, 0, -1) parallelSort(bar, foo) print foo # [4,3,2,1,0] print bar # [1,2,3,4,5] </code></pre> <p>I've see...
6
2010-02-08T15:49:29Z
2,222,965
<p>To achieve this, you would have to implement your own sort. </p> <p>However: Does the unnecessary copying really hurt your application? Often parts of Python strike me as inefficient, too, but they are efficient enough for what I need.</p>
0
2010-02-08T16:12:57Z
[ "python", "algorithm", "sorting" ]