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
How should I build this Django model to do what I want
1,923,551
<p>This is what I had before (but realized that you can't obviously do it in this order:</p> <pre><code>class MasterAdmin(models.Model): """ A permanent admin (one per Account) that shouldn't be deleted. """ admin = models.OneToOneField(AccountAdmin) class Account(models.Model): """ A top-leve...
0
2009-12-17T18:05:29Z
1,923,598
<p>Why not just make is_master a property of AccountAdmin and then override the delete() method to ensure is_master is not true?</p>
3
2009-12-17T18:16:11Z
[ "python", "django", "django-models", "foreign-keys", "one-to-one" ]
How should I build this Django model to do what I want
1,923,551
<p>This is what I had before (but realized that you can't obviously do it in this order:</p> <pre><code>class MasterAdmin(models.Model): """ A permanent admin (one per Account) that shouldn't be deleted. """ admin = models.OneToOneField(AccountAdmin) class Account(models.Model): """ A top-leve...
0
2009-12-17T18:05:29Z
1,923,651
<p>When you have forward references, use the quotes.</p> <pre><code>admin = models.OneToOneField('AccountAdmin') </code></pre> <p><a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey" rel="nofollow">See the docs</a>.</p> <blockquote> <p>If you need to create a relationship on a model that ha...
2
2009-12-17T18:26:46Z
[ "python", "django", "django-models", "foreign-keys", "one-to-one" ]
How do I customize formish error messages?
1,923,634
<p>I'm using <a href="http://ish.io/embedded/formish/" rel="nofollow">formish</a> to handle web forms but I don't like the automatically generated error messages when validation fails. Where do I customize those error messages?</p>
0
2009-12-17T18:23:13Z
2,077,084
<p>The best place for feedback on formish is in the google groups which is linked to from the <a href="http://form.ish.io" rel="nofollow">http://form.ish.io</a> page.. </p> <p>As for customising the error messages, the best way would be to create your own validator (which is pretty simple, have a look in the validatis...
0
2010-01-16T11:02:34Z
[ "python", "webforms", "formish" ]
How do I customize formish error messages?
1,923,634
<p>I'm using <a href="http://ish.io/embedded/formish/" rel="nofollow">formish</a> to handle web forms but I don't like the automatically generated error messages when validation fails. Where do I customize those error messages?</p>
0
2009-12-17T18:23:13Z
2,086,015
<p>I've added custom messages to the validatish module for you .. If you get it from github, you'll see how it works (you pass a dictionary of messages to the validate function or object - e.g. <code>Required(messages={'required': 'dont forget it!'}) )</code></p> <p>If you need a release for it quickly, let me know..<...
0
2010-01-18T12:36:40Z
[ "python", "webforms", "formish" ]
Multiprocessing launching too many instances of Python VM
1,923,706
<p>I am writing some multiprocessing code (Python 2.6.4, WinXP) that spawns processes to run background tasks. In playing around with some trivial examples, I am running into an issue where my code just continuously spawns new processes, even though I only tell it to spawn a fixed number.</p> <p>The program itself ru...
12
2009-12-17T18:37:24Z
1,923,736
<p>When I run this in Linux with python2.6, I see a maximum of 4 python2.6 processes and I can't guarantee that they're all from this process. They're definitely not filling up the machine.</p> <p>Need new python version? Linux/Windows difference?</p>
0
2009-12-17T18:43:10Z
[ "python", "windows", "multiprocessing" ]
Multiprocessing launching too many instances of Python VM
1,923,706
<p>I am writing some multiprocessing code (Python 2.6.4, WinXP) that spawns processes to run background tasks. In playing around with some trivial examples, I am running into an issue where my code just continuously spawns new processes, even though I only tell it to spawn a fixed number.</p> <p>The program itself ru...
12
2009-12-17T18:37:24Z
1,923,788
<p>I don't see anything wrong with that. Works fine on Ubuntu 9.10 (Python 2.6.4).</p> <p>Are you sure you don't have cron or something starting multiple copies of your script? Or that the spawned script is not calling anything that would start a new instance, for example as a side effect of import if your code runs d...
0
2009-12-17T18:50:24Z
[ "python", "windows", "multiprocessing" ]
Multiprocessing launching too many instances of Python VM
1,923,706
<p>I am writing some multiprocessing code (Python 2.6.4, WinXP) that spawns processes to run background tasks. In playing around with some trivial examples, I am running into an issue where my code just continuously spawns new processes, even though I only tell it to spawn a fixed number.</p> <p>The program itself ru...
12
2009-12-17T18:37:24Z
1,924,559
<p>It looks like you didn't carefully follow the guidelines in the documentation, specifically <a href="http://docs.python.org/library/multiprocessing.html#windows">this section</a> where it talks about "Safe importing of main module".</p> <p>You need to protect your launch code with an <code>if __name__ == '__main__'...
19
2009-12-17T21:00:01Z
[ "python", "windows", "multiprocessing" ]
How to make paramiko wait for transfer to finish?
1,923,794
<p>I've tried various ways to upload a file locally to a FTP, ncftpput was really slow compared to lftp so I switched.</p> <p>but what I've noticed is my python script waits for ncftpput to finish but when using lftp, it just uploads the file to the FTP and it continues on with the script..</p> <p>I am using paramiko...
0
2009-12-17T18:51:12Z
1,923,825
<p>See the <a href="http://lftp.yar.ru/lftp-man.html" rel="nofollow">lftp docs</a> for proper usage of the ftp:sync-mode setting. (You want <code>true</code>.)</p>
-1
2009-12-17T18:56:03Z
[ "python" ]
Django: How do I validate unique_together from within the model
1,923,826
<p>I have the following:</p> <pre><code>class AccountAdmin(models.Model): account = models.ForeignKey(Account) is_master = models.BooleanField() name = models.CharField(max_length=255) email = models.EmailField() class Meta: unique_together = (('Account', 'is_master'), ('Account', 'userna...
1
2009-12-17T18:56:18Z
1,924,035
<p>There are two options:</p> <p>a) Have a try block where you save your model and capture the IntegrityError and deal with it. Something like:</p> <pre><code>try: new_accountadmin_form.save() except IntegrityError: new_accountadmin_form._errors["account"] = ["some message"] new_accountadmin_form._errors[...
5
2009-12-17T19:32:32Z
[ "python", "django", "django-models", "django-views", "unique-constraint" ]
Django: How do I validate unique_together from within the model
1,923,826
<p>I have the following:</p> <pre><code>class AccountAdmin(models.Model): account = models.ForeignKey(Account) is_master = models.BooleanField() name = models.CharField(max_length=255) email = models.EmailField() class Meta: unique_together = (('Account', 'is_master'), ('Account', 'userna...
1
2009-12-17T18:56:18Z
1,924,041
<p>Model.Meta.unique_together creates a constraint limited to the database, while ModelForm.is_valid() is primarily based on correct types. Event if it did check constraints you would have a race condition that could still cause an IntegrityError in the save() call.</p> <p>You probably want to be catching IntegrityErr...
0
2009-12-17T19:33:48Z
[ "python", "django", "django-models", "django-views", "unique-constraint" ]
Django: How do I validate unique_together from within the model
1,923,826
<p>I have the following:</p> <pre><code>class AccountAdmin(models.Model): account = models.ForeignKey(Account) is_master = models.BooleanField() name = models.CharField(max_length=255) email = models.EmailField() class Meta: unique_together = (('Account', 'is_master'), ('Account', 'userna...
1
2009-12-17T18:56:18Z
14,155,196
<p>And for a completely generic way. In the model have the following two helper fns:</p> <pre><code>def getField(self,fieldName): # return the actual field (not the db representation of the field) try: return self._meta.get_field_by_name(fieldName)[0] except models.fields.FieldDoesNotExist: return None ...
2
2013-01-04T10:20:03Z
[ "python", "django", "django-models", "django-views", "unique-constraint" ]
suggestion required related to rewriting and string manipulation
1,924,043
<p>I had to read from a file and for each data between delimiter i need to remove the white space and i have written the following program in jython </p> <p>When i am trying to rewrite ,its rewriting at the end of source file. </p> <pre><code>filesrc = open('c:/FILE/split_doc.txt','r+') for list in filesrc.readlines(...
0
2009-12-17T19:34:12Z
1,924,188
<p>You are overwriting as you go, but your final results are shorter than the original, so you are getting the last X characters of the original bleeding through, where X is the difference in size from the original to the new version. The extra .seek() and truncate() calls in this version will seek to the end of your n...
0
2009-12-17T19:59:06Z
[ "python", "file", "rewrite", "jython" ]
suggestion required related to rewriting and string manipulation
1,924,043
<p>I had to read from a file and for each data between delimiter i need to remove the white space and i have written the following program in jython </p> <p>When i am trying to rewrite ,its rewriting at the end of source file. </p> <pre><code>filesrc = open('c:/FILE/split_doc.txt','r+') for list in filesrc.readlines(...
0
2009-12-17T19:34:12Z
1,924,215
<p>You don't want to write to the same file while you're reading it. It's technically possible, but that path is fraught with trouble and misery.</p> <p>Here's the plain and simple process you should follow:</p> <ul> <li>read the whole file into a string then close the file</li> <li>split the string on newlines into ...
1
2009-12-17T20:02:50Z
[ "python", "file", "rewrite", "jython" ]
suggestion required related to rewriting and string manipulation
1,924,043
<p>I had to read from a file and for each data between delimiter i need to remove the white space and i have written the following program in jython </p> <p>When i am trying to rewrite ,its rewriting at the end of source file. </p> <pre><code>filesrc = open('c:/FILE/split_doc.txt','r+') for list in filesrc.readlines(...
0
2009-12-17T19:34:12Z
1,924,329
<p>This does not answer your question, but have you considered not doing this with jython?</p> <p>Tried with <a href="http://en.wikipedia.org/wiki/Sed" rel="nofollow">Sed</a>?</p>
0
2009-12-17T20:25:39Z
[ "python", "file", "rewrite", "jython" ]
Python lambdas and scoping
1,924,214
<p>Given this snippet of code:</p> <pre><code>funcs = [] for x in range(3): funcs.append(lambda: x) print [f() for f in funcs] </code></pre> <p>I would expect it to print <code>[0, 1, 2]</code>, but instead it prints <code>[2, 2, 2]</code>. Is there something fundamental I'm missing about how lambdas work with sc...
6
2009-12-17T20:02:49Z
1,924,252
<p>x is bound to the module-level x (which is left over from the for loop).</p> <p>A little clearer:</p> <pre><code>funcs = [] for x in range(10): funcs.append(lambda: x) x = 'Foo' print [f() for f in funcs] # Prints ['Foo', 'Foo', 'Foo', 'Foo', 'Foo', 'Foo', 'Foo', 'Foo', 'Foo', 'Foo'] </code></pre>
5
2009-12-17T20:09:43Z
[ "python", "lambda" ]
Python lambdas and scoping
1,924,214
<p>Given this snippet of code:</p> <pre><code>funcs = [] for x in range(3): funcs.append(lambda: x) print [f() for f in funcs] </code></pre> <p>I would expect it to print <code>[0, 1, 2]</code>, but instead it prints <code>[2, 2, 2]</code>. Is there something fundamental I'm missing about how lambdas work with sc...
6
2009-12-17T20:02:49Z
1,924,275
<p>This is a frequent question in Python. Basically the scoping is such that when <code>f()</code> is called, it will use the current value of <code>x</code>, not the value of <code>x</code> at the time the lambda is formed. There is a standard workaround:</p> <pre><code>funcs = [] for x in range(10): funcs.append(la...
7
2009-12-17T20:12:57Z
[ "python", "lambda" ]
Python lambdas and scoping
1,924,214
<p>Given this snippet of code:</p> <pre><code>funcs = [] for x in range(3): funcs.append(lambda: x) print [f() for f in funcs] </code></pre> <p>I would expect it to print <code>[0, 1, 2]</code>, but instead it prints <code>[2, 2, 2]</code>. Is there something fundamental I'm missing about how lambdas work with sc...
6
2009-12-17T20:02:49Z
1,924,292
<p>You know the answer: yes. ;) Take comfort, however, as this is a very common discovery for budding pythonistas. When you define a function or lambda that references variables not "created" inside that function, it creates a closure over the variables. The effect is that you get the value of the variable when cal...
3
2009-12-17T20:16:43Z
[ "python", "lambda" ]
Couple of matplotlib newbie doubts
1,924,323
<p>I am just starting to use 'matplotlib' and I have hit upon 2 major roadblocks, which I can't seem to work around from the docs/examples,etc: Here is Python source:</p> <pre><code>#!/usr/bin/python import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt for i in range(0,301): print "Plotting",i...
1
2009-12-17T20:23:58Z
1,924,655
<p><UL> <LI>As Tim Pietzcker points out, you can shorten if filename code at the end by using string number formatting.</p> <pre><code>filename='plot%03d.png'%i </code></pre> <p>replaces <code>%03d</code> with the integer <code>i</code> padded with up to 3 zero's. In Python2.6+, one can do the same thing with the les...
2
2009-12-17T21:16:11Z
[ "python", "matplotlib", "scientific-computing" ]
Inheritable custom exceptions in python
1,924,380
<p>I want to create some custom exceptions for my class. I am trying to figure out the best way to make these exception classes inheritable in derived classes. The tutorial shows how to create the Exception classes. So I did that like this:</p> <p>I created a baseclass.py:</p> <pre><code>class Error(Exception): ...
2
2009-12-17T20:33:29Z
1,924,448
<p>The custom exceptions has to be imported in all modules its used in.</p> <p>Also, there is an error in derivedclass.py</p> <pre><code>Wrong (because of the way its imported) raise baseclass.OtherError, "Error doing other" Fixed raise OtherError, "Error doing other" </code></pre>
0
2009-12-17T20:43:39Z
[ "python" ]
Inheritable custom exceptions in python
1,924,380
<p>I want to create some custom exceptions for my class. I am trying to figure out the best way to make these exception classes inheritable in derived classes. The tutorial shows how to create the Exception classes. So I did that like this:</p> <p>I created a baseclass.py:</p> <pre><code>class Error(Exception): ...
2
2009-12-17T20:33:29Z
1,924,668
<blockquote> <p>So as you can see, I imported the exception classes from the base class into the derived class, then again from the derived class into the program.</p> </blockquote> <p>You did not, and cannot, import exceptions (or anything) from classes and into classes. You import things from modules and (...
0
2009-12-17T21:18:49Z
[ "python" ]
Inheritable custom exceptions in python
1,924,380
<p>I want to create some custom exceptions for my class. I am trying to figure out the best way to make these exception classes inheritable in derived classes. The tutorial shows how to create the Exception classes. So I did that like this:</p> <p>I created a baseclass.py:</p> <pre><code>class Error(Exception): ...
2
2009-12-17T20:33:29Z
1,924,679
<p>If the user imports your error classes by name, they'll notice the problem as soon as the import statement tries to execute:</p> <pre><code>ImportError: cannot import name FrotzError File "enduser.py", line 7, in &lt;module&gt; from ptcmark.derivedclass import FrotzError </code></pre> <p>And of course you'll doc...
0
2009-12-17T21:21:13Z
[ "python" ]
Define a list with type
1,924,469
<p>I come from Java, and I want to do some <a href="https://en.wikipedia.org/wiki/Data_transfer_object" rel="nofollow">data transfer objects</a> (DTOs) like this:</p> <pre><code>class ErrorDefinition(): code = '' message = '' exception = '' class ResponseDTO(): sucess = True errors = list() # How ...
0
2009-12-17T20:46:00Z
1,924,507
<p>I'm pretty sure you can't define a type for a list. You're going to need to import ErrorDefinition every time (which looks like the already existing <code>Exception</code> class).</p>
0
2009-12-17T20:50:45Z
[ "python" ]
Define a list with type
1,924,469
<p>I come from Java, and I want to do some <a href="https://en.wikipedia.org/wiki/Data_transfer_object" rel="nofollow">data transfer objects</a> (DTOs) like this:</p> <pre><code>class ErrorDefinition(): code = '' message = '' exception = '' class ResponseDTO(): sucess = True errors = list() # How ...
0
2009-12-17T20:46:00Z
1,924,536
<p>Python is dynamically typed, and you just don't declare types for variables like you do in Java. <a href="http://docs.python.org/" rel="nofollow">The official tutorial</a> is highly suggested reading at this stage.</p>
5
2009-12-17T20:56:10Z
[ "python" ]
Define a list with type
1,924,469
<p>I come from Java, and I want to do some <a href="https://en.wikipedia.org/wiki/Data_transfer_object" rel="nofollow">data transfer objects</a> (DTOs) like this:</p> <pre><code>class ErrorDefinition(): code = '' message = '' exception = '' class ResponseDTO(): sucess = True errors = list() # How ...
0
2009-12-17T20:46:00Z
1,924,564
<p>DTO is a design pattern for Java. Trying to use Java semantics in Python is not going to work. You need to step out another level and ask. This is the problem I am trying to solve ... , in Java I would use DTO - how would you approach it using Python?</p>
0
2009-12-17T21:01:00Z
[ "python" ]
Define a list with type
1,924,469
<p>I come from Java, and I want to do some <a href="https://en.wikipedia.org/wiki/Data_transfer_object" rel="nofollow">data transfer objects</a> (DTOs) like this:</p> <pre><code>class ErrorDefinition(): code = '' message = '' exception = '' class ResponseDTO(): sucess = True errors = list() # How ...
0
2009-12-17T20:46:00Z
1,924,597
<blockquote> <p>errors = list() # How do I say it that it is directly of the ErrorDefinition() type, to not import it every time that I'm going to append an error definition?</p> </blockquote> <p>I am not sure what you are trying to say in this comment, but if I understand right, the best way to get something close ...
1
2009-12-17T21:06:05Z
[ "python" ]
Define a list with type
1,924,469
<p>I come from Java, and I want to do some <a href="https://en.wikipedia.org/wiki/Data_transfer_object" rel="nofollow">data transfer objects</a> (DTOs) like this:</p> <pre><code>class ErrorDefinition(): code = '' message = '' exception = '' class ResponseDTO(): sucess = True errors = list() # How ...
0
2009-12-17T20:46:00Z
1,924,740
<p>Please explain what you mean by "import it every time".</p> <p>You need to reconsider using class-level attributes before you have explored exactly what they do, especially when you use mutable types like lists. Consider this:</p> <pre><code>&gt;&gt;&gt; class Borg(object): ... alist = list() ... &gt;&gt;&gt; ...
0
2009-12-17T21:33:46Z
[ "python" ]
Linux USB Mapping Question
1,924,646
<p>I'm working on a utility that will auto mount an inserted USB stick on linux. I have tied into D-Bus to receive notification of when a device is inserted, and that works great. However, I need to determine which device in /dev is mapped to the inserted USB stick. I am getting the D-Bus notification and then scanning...
2
2009-12-17T21:14:56Z
1,924,687
<p>Why not use <strong>"os"</strong> module to mount the file system:</p> <pre><code>os.system ("mount ... ") </code></pre> <p>Or if you want to examine output use "popen":</p> <pre><code>l = op.popen ("mount ....").readlines() </code></pre>
0
2009-12-17T21:23:08Z
[ "python", "linux", "usb", "dbus" ]
Linux USB Mapping Question
1,924,646
<p>I'm working on a utility that will auto mount an inserted USB stick on linux. I have tied into D-Bus to receive notification of when a device is inserted, and that works great. However, I need to determine which device in /dev is mapped to the inserted USB stick. I am getting the D-Bus notification and then scanning...
2
2009-12-17T21:14:56Z
1,924,715
<p>what about using <code>dmesg</code> output to find out the device name (sdc1 etc...)</p> <p>use it right after dbus tells you something is was inserted in USB. you could do <code>tail dmesg</code> for example</p>
0
2009-12-17T21:27:19Z
[ "python", "linux", "usb", "dbus" ]
Linux USB Mapping Question
1,924,646
<p>I'm working on a utility that will auto mount an inserted USB stick on linux. I have tied into D-Bus to receive notification of when a device is inserted, and that works great. However, I need to determine which device in /dev is mapped to the inserted USB stick. I am getting the D-Bus notification and then scanning...
2
2009-12-17T21:14:56Z
1,924,984
<p>You should probably ask HAL about that. You say you already get notifications from HAL by D-Bus... It maintains list of USB devices, together with their IDs and device names (<code>block.device</code> property).</p> <p>Here's a nice example of how to get device file name together with the notification of new USB de...
2
2009-12-17T22:16:14Z
[ "python", "linux", "usb", "dbus" ]
Use <optgroup> with form.fields.queryset?
1,924,704
<p>Is it possible to set a form's ForeignKey field's queryset so that it will take separate queryset's and output them in <code>&lt;optgroup&gt;</code>'s?</p> <p>Here is what I have:</p> <p>views.py</p> <pre><code>form = TemplateFormBasic(initial={'template': digest.template.id}) form.fields['template'].queryset = T...
5
2009-12-17T21:25:27Z
1,924,792
<p>I've done in the past by not using a foreign key on the form, but rather a <a href="http://www.djangoproject.com/documentation/models/choices/" rel="nofollow">charfield with choices</a>.</p> <p>A CharField with choices support optgroups. You need to have the choices in this format:</p> <blockquote> <p>('Group 1'...
4
2009-12-17T21:43:08Z
[ "python", "django", "django-forms", "django-queryset" ]
Use <optgroup> with form.fields.queryset?
1,924,704
<p>Is it possible to set a form's ForeignKey field's queryset so that it will take separate queryset's and output them in <code>&lt;optgroup&gt;</code>'s?</p> <p>Here is what I have:</p> <p>views.py</p> <pre><code>form = TemplateFormBasic(initial={'template': digest.template.id}) form.fields['template'].queryset = T...
5
2009-12-17T21:25:27Z
1,925,072
<p>I was able to figure it out using the example given on <a href="http://dealingit.wordpress.com/2009/10/26/django-tip-showing-optgroup-in-a-modelform/">this blog</a></p> <p>views.py</p> <pre><code>form.fields['template'].choices = templates_as_choices(request) def templates_as_choices(request): templates = [] ...
10
2009-12-17T22:33:59Z
[ "python", "django", "django-forms", "django-queryset" ]
get datatype length in python by parsing a string
1,924,828
<p>I have strings like</p> <pre><code>uint8_t char[5] int[3] </code></pre> <p>How can I write a short function to get the type and length separately in an elegant way</p> <p>for eg</p> <pre><code>uint8_t // return 'uint8_t', '1' char[5] // return 'char', '5' ... </code></pre>
0
2009-12-17T21:49:06Z
1,924,888
<pre><code>In [1]: import re In [2]: r = re.compile('([\w_]+)(?:\[(\d+)\])?') In [3]: m = r.match('char[5]') In [4]: m.group(1), m.group(2) or 1 Out[4]: ('char', '5') I...
0
2009-12-17T21:57:17Z
[ "python" ]
get datatype length in python by parsing a string
1,924,828
<p>I have strings like</p> <pre><code>uint8_t char[5] int[3] </code></pre> <p>How can I write a short function to get the type and length separately in an elegant way</p> <p>for eg</p> <pre><code>uint8_t // return 'uint8_t', '1' char[5] // return 'char', '5' ... </code></pre>
0
2009-12-17T21:49:06Z
1,924,911
<pre><code>import re def parse_type(text): match = re.match(r'(.+)\[(\d+)\]', text) if match: return match.groups() return text, 1 print parse_type('uint8_t') print parse_type('char[5]') print parse_type('int[3]') </code></pre>
0
2009-12-17T22:04:07Z
[ "python" ]
get datatype length in python by parsing a string
1,924,828
<p>I have strings like</p> <pre><code>uint8_t char[5] int[3] </code></pre> <p>How can I write a short function to get the type and length separately in an elegant way</p> <p>for eg</p> <pre><code>uint8_t // return 'uint8_t', '1' char[5] // return 'char', '5' ... </code></pre>
0
2009-12-17T21:49:06Z
1,924,912
<p>given these tests:</p> <pre><code>&gt;&gt;&gt; s = "char[5]" &gt;&gt;&gt; p = s.split("[") &gt;&gt;&gt; p ['char', '5]'] &gt;&gt;&gt; p[1].strip("]") '5' &gt;&gt;&gt; s = "uint8_t" &gt;&gt;&gt; p = s.split("[") &gt;&gt;&gt; p ['uint8_t'] &gt;&gt;&gt; </code></pre> <p>here's a little function that gives you what yo...
0
2009-12-17T22:04:22Z
[ "python" ]
get datatype length in python by parsing a string
1,924,828
<p>I have strings like</p> <pre><code>uint8_t char[5] int[3] </code></pre> <p>How can I write a short function to get the type and length separately in an elegant way</p> <p>for eg</p> <pre><code>uint8_t // return 'uint8_t', '1' char[5] // return 'char', '5' ... </code></pre>
0
2009-12-17T21:49:06Z
1,924,916
<p>Let's make it a one-liner:</p> <pre><code>import re def type_and_size(s): return re.split('[][]', s+'[1]', 2)[:2] type_and_size('char') ['char', '1'] type_and_size('char[5]') ['char', '5'] </code></pre> <p>Obviously you can do:</p> <pre><code>type, size = type_and_size('char[5]') </code></pre>
3
2009-12-17T22:04:44Z
[ "python" ]
python import a method that depends on a imported class
1,924,906
<p>So if I have 2 files that look like this:</p> <p>File 1</p> <pre><code>import class1 import method1 def method2(something): result = method1(classname=class1) </code></pre> <p>File 2</p> <pre><code>def method1(classname): some_result = classname.resultfinder return some_result </code></pre> <p>Will...
1
2009-12-17T22:02:33Z
1,924,918
<p>I think that should be fine - imagine having to import every possible type that could be passed to a function at runtime. I don't think a "dynamic" language like that would last very long.</p>
1
2009-12-17T22:04:47Z
[ "python", "import" ]
python import a method that depends on a imported class
1,924,906
<p>So if I have 2 files that look like this:</p> <p>File 1</p> <pre><code>import class1 import method1 def method2(something): result = method1(classname=class1) </code></pre> <p>File 2</p> <pre><code>def method1(classname): some_result = classname.resultfinder return some_result </code></pre> <p>Will...
1
2009-12-17T22:02:33Z
1,924,925
<p>What happened when you tried it?</p> <p>Note that your import of <code>method1</code> is wrong. Apart from that --- yes, you do not need to import everything. Do you think the standard library imports your stuff whenever you use it? ;-)</p>
2
2009-12-17T22:05:56Z
[ "python", "import" ]
python import a method that depends on a imported class
1,924,906
<p>So if I have 2 files that look like this:</p> <p>File 1</p> <pre><code>import class1 import method1 def method2(something): result = method1(classname=class1) </code></pre> <p>File 2</p> <pre><code>def method1(classname): some_result = classname.resultfinder return some_result </code></pre> <p>Will...
1
2009-12-17T22:02:33Z
1,926,439
<p>It doesn't quite look right--you don't exactly "import class1", you more import a module (in a file) that <em>contains</em> class1, or a module that <em>contains</em> method1. So I'd expect to see more like</p> <pre><code>from file2 import method1 from file3 import class1 </code></pre> <p>Also, <code>method1</code...
0
2009-12-18T05:20:48Z
[ "python", "import" ]
XML/SWF charts example not working with cherryPy
1,924,973
<p>I am trying to use use the XML/SWF Charts library with cherrypy. I want to generate html reports with nice looking charts. </p> <p>I am trying to expose one of the default examples of XML/SWF charts with cherryPy, but for some reason the javascript is not working properly with cherryPy.</p> <p>I created the follow...
0
2009-12-17T22:14:28Z
1,925,027
<pre><code>conf = {'/js/AC_RunActiveContent.js': {'tools.staticfile.on': True, 'tools.staticfile.filename': os.path.join(current_dir, 'data', 'scripts', 'AC_RunActiveContent.js')}} </code></pre> <p>And later</p> <pre><code>&lt;script src="AC_RunActiveContent.js" language="javascript"&gt;&lt;/script&...
1
2009-12-17T22:24:14Z
[ "javascript", "python", "cherrypy", "xml-swf-charts" ]
PyQt: Always on top
1,925,015
<p>This is on PyQt4, Linux and Python 2.5</p> <p>Can I make PyQt set my window "always on top" over other applications?</p> <p>For example, in GTK i use the property: Modal. </p> <p>Now, in PyQt I am using a QWidget, but, I can't find a way to do that.</p> <p>Any ideas??</p>
15
2009-12-17T22:22:15Z
1,925,061
<p>Pass the QMainWindow the <code>WindowStaysOnTopHint</code> <a href="http://doc.qt.io/qt-5/qt.html#WindowType-enum">window flag</a> (or use setWindowFlags). </p> <p>As in the name, this is a hint to the windowing manager (not a hard guarantee).</p> <p>Simplest possible example:</p> <pre><code>import sys from PyQt4...
22
2009-12-17T22:32:19Z
[ "python", "pyqt" ]
PyQt: Always on top
1,925,015
<p>This is on PyQt4, Linux and Python 2.5</p> <p>Can I make PyQt set my window "always on top" over other applications?</p> <p>For example, in GTK i use the property: Modal. </p> <p>Now, in PyQt I am using a QWidget, but, I can't find a way to do that.</p> <p>Any ideas??</p>
15
2009-12-17T22:22:15Z
16,459,043
<pre><code>setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) </code></pre> <p><code>setwindowaFlags</code> is a method that can call it from <code>form</code> object and just take one parameter is a constant <code>QtCore.Qt.WindowStaysOnTopHint</code> that refer to make your form Stays On Top</p>
6
2013-05-09T09:55:59Z
[ "python", "pyqt" ]
Specifying chars in python
1,925,043
<p>I need a functions that iterates over all the lines in the file.<br> Here's what I have so far: </p> <pre><code>def LineFeed(file): ret = "" for byte in file: ret = ret + str(byte) if str(byte) == '\r': yield ret ret = "" </code></pre> <p>All the lines in the file en...
0
2009-12-17T22:27:18Z
1,925,056
<p>I think that you are confused with what "for x in file" does. Assuming you got your handle like "file = open(file_name)", byte in this case will be an entire line, not a single character. So you are only calling yield when the entire line consists of a single carriage return. Try changing "byte" to "line" and ite...
2
2009-12-17T22:31:32Z
[ "python", "binaryfiles" ]
Specifying chars in python
1,925,043
<p>I need a functions that iterates over all the lines in the file.<br> Here's what I have so far: </p> <pre><code>def LineFeed(file): ret = "" for byte in file: ret = ret + str(byte) if str(byte) == '\r': yield ret ret = "" </code></pre> <p>All the lines in the file en...
0
2009-12-17T22:27:18Z
1,925,077
<p><strong>Edit:</strong> </p> <ul> <li><p><code>string1 += string2</code> string concatenation is slow. Try joining a list of strings.</p></li> <li><p>ddaa is right--You shouldn't need the <code>struct</code> package if the binary file only contains ASCII. Also, my generator returns the string after the final '\r'...
0
2009-12-17T22:35:35Z
[ "python", "binaryfiles" ]
Specifying chars in python
1,925,043
<p>I need a functions that iterates over all the lines in the file.<br> Here's what I have so far: </p> <pre><code>def LineFeed(file): ret = "" for byte in file: ret = ret + str(byte) if str(byte) == '\r': yield ret ret = "" </code></pre> <p>All the lines in the file en...
0
2009-12-17T22:27:18Z
1,925,223
<p>So, your problem is iterating over the lines of a file open in binary mode that use <code>'\r'</code> as a line separator. Since the file is in binary mode, you cannot use the universal newline feature, and it turns out that <code>'\r'</code> is not interpreted as a line separator in binary mode.</p> <p>Reading a f...
0
2009-12-17T23:06:37Z
[ "python", "binaryfiles" ]
Specifying chars in python
1,925,043
<p>I need a functions that iterates over all the lines in the file.<br> Here's what I have so far: </p> <pre><code>def LineFeed(file): ret = "" for byte in file: ret = ret + str(byte) if str(byte) == '\r': yield ret ret = "" </code></pre> <p>All the lines in the file en...
0
2009-12-17T22:27:18Z
1,925,260
<p>Perhaps if you were to explain what this file represents, why it has lots of '\x00', why you think you need to read it in binary mode, we could help you with your underlying problem.</p> <p>Otherwise, try the following code; it avoids any dependence on (or interference from) your operating system's line-ending conv...
2
2009-12-17T23:14:46Z
[ "python", "binaryfiles" ]
Specifying chars in python
1,925,043
<p>I need a functions that iterates over all the lines in the file.<br> Here's what I have so far: </p> <pre><code>def LineFeed(file): ret = "" for byte in file: ret = ret + str(byte) if str(byte) == '\r': yield ret ret = "" </code></pre> <p>All the lines in the file en...
0
2009-12-17T22:27:18Z
1,925,926
<p>If you're in control of how you open the file, I'd recommend opening it with universal newlines, since \r isn't recognized as a linefeed character if you just use 'rb' mode, but it is if you use 'Urb'.</p> <p>This will only work if you aren't including \n as well as \r in your binary file somewhere, since the disti...
1
2009-12-18T02:36:13Z
[ "python", "binaryfiles" ]
declaring empty class member in python
1,925,246
<p>i am trying to read a tree structure file in python. I created a class to hold the tree objects. One of the members should hold the parent object. Since the parentObject member is of the same type as the class itself, I need to declare this as an empty variable of type "self". </p> <p>How do I do that in python?<...
3
2009-12-17T23:09:31Z
1,925,252
<p>In Python, you don't declare variables as having any type. You just assign to them. A single variable can be assigned objects of different types in succession, if you wanted to do so. "self" is not the type but a naming convention used to refer to the current object, like "this" in Java / C++ (except in Python you...
6
2009-12-17T23:11:35Z
[ "python", "variables" ]
declaring empty class member in python
1,925,246
<p>i am trying to read a tree structure file in python. I created a class to hold the tree objects. One of the members should hold the parent object. Since the parentObject member is of the same type as the class itself, I need to declare this as an empty variable of type "self". </p> <p>How do I do that in python?<...
3
2009-12-17T23:09:31Z
1,925,257
<p>I assume that for the root node, you want such an object. It's much more Pythonic to say:</p> <pre><code>self.parent = None </code></pre> <p>in your root object, than to create an empty object of type <code>self.__class__</code>. For every other node object in the tree, <code>self.parent</code> should refer to t...
4
2009-12-17T23:14:25Z
[ "python", "variables" ]
declaring empty class member in python
1,925,246
<p>i am trying to read a tree structure file in python. I created a class to hold the tree objects. One of the members should hold the parent object. Since the parentObject member is of the same type as the class itself, I need to declare this as an empty variable of type "self". </p> <p>How do I do that in python?<...
3
2009-12-17T23:09:31Z
1,925,285
<blockquote> <p>Since the parentObject member is of the same type as the class itself, I need to declare this as an empty variable of type "self".</p> </blockquote> <p>No, you do not need to declare anything in Python. You just define things.</p> <p>And self is not a type, but the conventional name for the fi...
3
2009-12-17T23:20:24Z
[ "python", "variables" ]
Searching a file in 3 different ways
1,925,284
<p>I have been writing a program that searches a file in 3 different ways. But firstly, to choose which search program to use is differentiated in the command line.</p> <p>For example in the command line I type:</p> <blockquote> <p>Program 1 search: python file.py 'search_term' 'file-to-be-searched'</p> <p>p...
0
2009-12-17T23:20:19Z
1,925,307
<p>Maybe it's a little to heavy for a short script, but in pythons standard library, you'll encounter the <a href="http://docs.python.org/library/getopt.html" rel="nofollow">getopt</a> and the more convenient <a href="http://docs.python.org/library/optparse.html#module-optparse" rel="nofollow">optparse</a> module.</p> ...
1
2009-12-17T23:25:40Z
[ "python", "file", "search" ]
Searching a file in 3 different ways
1,925,284
<p>I have been writing a program that searches a file in 3 different ways. But firstly, to choose which search program to use is differentiated in the command line.</p> <p>For example in the command line I type:</p> <blockquote> <p>Program 1 search: python file.py 'search_term' 'file-to-be-searched'</p> <p>p...
0
2009-12-17T23:20:19Z
1,925,310
<p>First of all you should look at two very useful Python modules:</p> <ul> <li><a href="http://docs.python.org/library/fileinput.html" rel="nofollow">fileinput</a>: Iterate over lines from multiple input streams</li> <li><a href="http://docs.python.org/library/optparse.html" rel="nofollow">optparse</a>: A powerful co...
3
2009-12-17T23:26:37Z
[ "python", "file", "search" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
1,925,372
<p><a href="http://github.com/mattup/ruleby" rel="nofollow">ruleby</a> is a rule engine in written in ruby. It was subject of a presentation at rubyhoedown 2008: <a href="http://rubyhoedown2008.confreaks.com/10-joe-kutner-ruleby-the-rule-engine-for-ruby.html" rel="nofollow">ruleby-the-rule-engine-for-ruby</a></p>
1
2009-12-17T23:42:07Z
[ "java", "python", "c", "rule-engine" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
1,925,382
<p>Pychinko has been around for a while. I've never used it in production, but investigated it for possible production application a while back. It looks like it has pretty good features and a decent community of users.</p> <p><a href="http://www.mindswap.org/~katz/pychinko/" rel="nofollow">http://www.mindswap.org/~ka...
1
2009-12-17T23:44:20Z
[ "java", "python", "c", "rule-engine" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
1,925,588
<p>In effect, Python <em>is</em> a rules engine.</p> <p>"The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc."</p> <p>You need sensors and controllers. You write your "rules" as ordinary Python objects.</p> <p>Your main "program" collects events from your s...
0
2009-12-18T00:44:44Z
[ "java", "python", "c", "rule-engine" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
1,927,747
<p>You could look at <a href="http://clipsrules.sourceforge.net/" rel="nofollow">CLIPS</a> as already suggested or, if you want to pay money or need it <a href="http://www.pst.com/rete2.htm" rel="nofollow">Rete2</a>. I've used CLIPS in the past on Unix and sucessfully embedded it into other applications. </p> <p>Hop...
2
2009-12-18T11:44:50Z
[ "java", "python", "c", "rule-engine" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
1,929,520
<p>In your search for RETE based rules engine in Python either <a href="http://pyke.sourceforge.net/" rel="nofollow">Pyke</a> or <a href="http://pyclips.sourceforge.net/web/" rel="nofollow">PyCLIPS</a> could be the one you would want to use. </p> <p>PS: I had left a comment to S.Lott's answer about Pyke. I have posted...
5
2009-12-18T17:03:16Z
[ "java", "python", "c", "rule-engine" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
1,936,813
<p>Rulecore is indeed written partly in Python. But it does not really matter. You as an user would not see any of these implementation details anyway.</p> <p>The rules are purely declarative and defined using XML. The XML is sent into ruleCore CEP Server as events using a web services or JMS or other protocols.</p>
0
2009-12-20T19:58:01Z
[ "java", "python", "c", "rule-engine" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
1,936,839
<p>Here is a list of 13 open source rules engines in java, Drools is possibly the best of these. <a href="http://java-sources.org/open-source/rule-engines" rel="nofollow">http://java-sources.org/open-source/rule-engines</a></p>
0
2009-12-20T20:05:45Z
[ "java", "python", "c", "rule-engine" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
2,138,573
<p>I know ruleCore has some parts written in Python. But the API uses XML and ActiveMQ or WebServices so it is on a higher abstraction level.</p>
0
2010-01-26T09:47:46Z
[ "java", "python", "c", "rule-engine" ]
Rules Engine in C or Python
1,925,360
<p>I am looking for a rules engine in C or Python, but if you know a rules engine that is implemented in another language I would be glad to know about it.</p> <p>The engine will be used as way to automate a house, like turning the light off when somebody leaves a room etc. So no "office" rules there (aka do you rules...
7
2009-12-17T23:39:14Z
25,924,225
<p><a href="https://nebrios.com" rel="nofollow">Nebri</a> is the easiest way to write rules for home automation AND other software/machines. Here's an example to accomplish the light shutoff:</p> <pre><code>class high_temp_shutdown(NebriOS): listens_to == ['shared.pebble_location'] def check(self): ...
0
2014-09-18T23:49:16Z
[ "java", "python", "c", "rule-engine" ]
apache + mod_wsgi + aspell-python on OS X 10.5.8
1,925,425
<p>I have a website that processes user submitted documents in a variety of ways, one of which is to do a spell check on a part of each document. When I set this website up on a Mac Mini (yes, I realize that's a pretty weak piece of equipment for a website, but it's internal and no one outside the office sees it), I r...
0
2009-12-17T23:56:48Z
1,925,507
<p>If your upgrade was only from one patch revision of 10.5 to later patch revision of 10.5, then you shouldn't have seen any change in behaviour in respect of requirement for 32 bit vs 64 bit. If it broke now, it should have broke before as it has always behave same for 10.5 and didn't change in a patch revision.</p> ...
1
2009-12-18T00:17:36Z
[ "python", "django", "apache", "mod-wsgi", "aspell" ]
XML-RPC method parameter data typing in Python
1,925,487
<p>I have built an XML-RPC interface in Python and I need to enforce some stricter typing. For example, passing string '10' instead of int 10. I can clean this up with some type casting and a little exception handling, but I am wondering if there is any other way of forcing type integrity such as something XML-RPC sp...
2
2009-12-18T00:12:06Z
1,925,617
<p>It's always going to be converted to a string anyway, so why do you care what's being passed in? If you use <code>"%s" % number</code> or even just <code>str(number)</code>, then it doesn't matter whether <code>number</code> is a string or an int.</p>
1
2009-12-18T00:51:10Z
[ "python", "django" ]
XML-RPC method parameter data typing in Python
1,925,487
<p>I have built an XML-RPC interface in Python and I need to enforce some stricter typing. For example, passing string '10' instead of int 10. I can clean this up with some type casting and a little exception handling, but I am wondering if there is any other way of forcing type integrity such as something XML-RPC sp...
2
2009-12-18T00:12:06Z
1,927,472
<p>XML-RPC methods (at least in <code>xmlrpclib</code>) are dispatched to Python functions or method, so you have to enforce type checking in them. There is a lot of recipes on doing this task with decorators: <a href="http://wiki.python.org/moin/PythonDecoratorLibrary#TypeEnforcement.28accepts.2BAC8-returns.29" rel="n...
0
2009-12-18T10:42:53Z
[ "python", "django" ]
XML-RPC method parameter data typing in Python
1,925,487
<p>I have built an XML-RPC interface in Python and I need to enforce some stricter typing. For example, passing string '10' instead of int 10. I can clean this up with some type casting and a little exception handling, but I am wondering if there is any other way of forcing type integrity such as something XML-RPC sp...
2
2009-12-18T00:12:06Z
1,927,481
<p>I always just use the re.escape function eg.:</p> <pre><code>number = re.escape(number) </code></pre> <p>This way things always gets converted to strings</p>
0
2009-12-18T10:46:46Z
[ "python", "django" ]
Get Cygwin installation path in a Python script
1,925,552
<p>I'm writing a cross-platform python script that needs to know if and where Cygwin is installed if the platform is NT. Right now I'm just using a naive check for the existence of the default install path 'C:\Cygwin'. I would like to be able to determine the installation path programmatically.</p> <p>The Windows regi...
2
2009-12-18T00:29:47Z
1,925,606
<p>That's what I'd do. There are registry entries for the cygwin drive mount points:</p> <p><a href="http://www.cygwin.com/ml/cygwin/2004-12/msg00200.html" rel="nofollow">http://www.cygwin.com/ml/cygwin/2004-12/msg00200.html</a></p> <p>You can use the _winreg (or winreg in python 3.0) module to look the values up:</p...
1
2009-12-18T00:48:32Z
[ "python", "windows", "registry", "cygwin" ]
Get Cygwin installation path in a Python script
1,925,552
<p>I'm writing a cross-platform python script that needs to know if and where Cygwin is installed if the platform is NT. Right now I'm just using a naive check for the existence of the default install path 'C:\Cygwin'. I would like to be able to determine the installation path programmatically.</p> <p>The Windows regi...
2
2009-12-18T00:29:47Z
2,568,441
<p>You can use the <code>HKEY_LOCAL_MACHINE\SOFTWARE\Cygwin\setup\rootdir</code> value for <strong>Cygwin 1.7</strong></p>
0
2010-04-02T18:36:04Z
[ "python", "windows", "registry", "cygwin" ]
Get Cygwin installation path in a Python script
1,925,552
<p>I'm writing a cross-platform python script that needs to know if and where Cygwin is installed if the platform is NT. Right now I'm just using a naive check for the existence of the default install path 'C:\Cygwin'. I would like to be able to determine the installation path programmatically.</p> <p>The Windows regi...
2
2009-12-18T00:29:47Z
2,627,203
<p>Valid for Cygwin 1.7 only:</p> <p>You need to check both HKEY_CURRENT_USER and HKEY_LOCAL_MAHINE for the Cygwin registry key. Depending on how Cygwin was installed it could be under either key.</p> <p>The following is an example of how to query the value using the current user.</p> <pre><code>CYGWIN_KEY = "SOFTWA...
2
2010-04-13T05:37:16Z
[ "python", "windows", "registry", "cygwin" ]
Handling an exception in another thread
1,925,635
<p>What is the "correct" way of detecting and handling an exception in another thread in Python, when the code in that other thread is not under your control?</p> <p>For instance, say you set a function that requires 2 parameters as the target of the <code>threading.Thread</code> object, but at runtime attempt to pass...
2
2009-12-18T00:57:37Z
1,925,664
<p>I think you can only decorate your target function or subclass threading.Thread to take care of exceptions. </p> <pre><code>def safer( func ): def safer(*args,**kwargs): try: return func(*args,**kwargs) except Exception,e: print "Couldn't call", func # do_stuf...
3
2009-12-18T01:05:45Z
[ "python", "multithreading", "exception" ]
httplib CannotSendRequest error in WSGI
1,925,639
<p>I've used two different python oauth libraries with Django to authenticate with twitter. The setup is on apache with WSGI. When I restart the server everything works great for about 10 minutes and then the httplib seems to lock up (see the following error).</p> <p>I'm running only 1 process and 1 thread of WSGI b...
13
2009-12-18T00:58:42Z
1,927,317
<p>This exception is raised when you reuse <code>httplib.HTTP</code> object for new request while you havn't called its <code>getresponse()</code> method for previous request. Probably there was some other error before this one that left connection in broken state. The simplest reliable way to fix the problem is creati...
22
2009-12-18T10:00:26Z
[ "python", "twitter", "oauth", "wsgi", "httplib" ]
httplib CannotSendRequest error in WSGI
1,925,639
<p>I've used two different python oauth libraries with Django to authenticate with twitter. The setup is on apache with WSGI. When I restart the server everything works great for about 10 minutes and then the httplib seems to lock up (see the following error).</p> <p>I'm running only 1 process and 1 thread of WSGI b...
13
2009-12-18T00:58:42Z
30,005,978
<p>Also check your Python version. I had a similar situation after updating to Py-2.7 from Py-2.6. In Py-2.6 everything worked without any problems. Py-2.7 httplib uses HTTP/1.1 by default which made the server did not send back the <em>Connection: close</em> option in the respond, therefore the connection handling was...
0
2015-05-02T18:31:29Z
[ "python", "twitter", "oauth", "wsgi", "httplib" ]
Read a formatted date in Python 2.3?
1,925,678
<p>I had never worked with the datetime module in Python 2.3, and I have a very silly problem. I need to read a date in the format </p> <pre><code>'10-JUL-2010' </code></pre> <p>then subtract a day (I would use <code>timedelta</code>), and return the string </p> <pre><code>'09-JUL-2010 00:00:00 ET' </code></pre> <p...
1
2009-12-18T01:08:09Z
1,925,706
<p>You're looking for <code>datetime.datetime.strptime()</code>, but the documentation is awful for that function, it's effectively the reverse operation of <code>datetime.datetime.strftime()</code>.</p> <p>The format string you're looking for is: <code>'%d-%b-%Y'</code></p> <p>See: <a href="http://www.python.org/doc...
2
2009-12-18T01:15:19Z
[ "python", "datetime" ]
Read a formatted date in Python 2.3?
1,925,678
<p>I had never worked with the datetime module in Python 2.3, and I have a very silly problem. I need to read a date in the format </p> <pre><code>'10-JUL-2010' </code></pre> <p>then subtract a day (I would use <code>timedelta</code>), and return the string </p> <pre><code>'09-JUL-2010 00:00:00 ET' </code></pre> <p...
1
2009-12-18T01:08:09Z
1,925,711
<p>Well, there is no builtin for that in 2.3, <a href="http://docs.python.org/library/datetime.html#datetime.datetime.strptime" rel="nofollow">only from 2.5 on</a>. But for that one format you can parse it by hand ...</p> <pre><code>months = { 'JAN' : 1, 'FEB' : 2, ... } # write that yourself :p day,mon,year = thedate...
2
2009-12-18T01:16:43Z
[ "python", "datetime" ]
Read a formatted date in Python 2.3?
1,925,678
<p>I had never worked with the datetime module in Python 2.3, and I have a very silly problem. I need to read a date in the format </p> <pre><code>'10-JUL-2010' </code></pre> <p>then subtract a day (I would use <code>timedelta</code>), and return the string </p> <pre><code>'09-JUL-2010 00:00:00 ET' </code></pre> <p...
1
2009-12-18T01:08:09Z
1,925,839
<p>If strptime() is not working out for you there is also the option of brute forcing it with a regex:</p> <pre><code>import re import date timestamp_regex = re.compile(r"(\d\d)-(\w\w\w)-(\d\d\d\d)") # month_mapping: a mapping for 3 letter months to integers d1 = datetime.date(int(match.group(3)), #year ...
0
2009-12-18T02:00:17Z
[ "python", "datetime" ]
What problems will one see in using Python multiprocessing naively?
1,925,718
<p>We're considering re-factoring a large application with a complex GUI which is isolated in a decoupled fashion from the back-end, to use the new (Python 2.6) multiprocessing module. The GUI/backend interface uses Queues with Message objects exchanged in both directions.</p> <p>One thing I've just concluded (tentat...
7
2009-12-18T01:19:13Z
1,926,926
<p>I have not used multiprocessing itself, but the problems presented are similar to experience I've had in two other domains: distributed systems, and object databases. Python object identity can be a blessing and a curse!</p> <p>As for general gotchas, it helps if the application you are refactoring can acknowledge...
2
2009-12-18T07:55:39Z
[ "python", "multiprocessing" ]
What problems will one see in using Python multiprocessing naively?
1,925,718
<p>We're considering re-factoring a large application with a complex GUI which is isolated in a decoupled fashion from the back-end, to use the new (Python 2.6) multiprocessing module. The GUI/backend interface uses Queues with Message objects exchanged in both directions.</p> <p>One thing I've just concluded (tentat...
7
2009-12-18T01:19:13Z
1,927,429
<p>Well, of course testing for identity on non-singleton object (es. "a is None" or "a is False") isn't usually a good practice - it might be quick, but a really-quick workaround would be to exchange the "is" for the "==" test and use an incremental counter to define identity:</p> <pre><code># this is not threadsafe. ...
1
2009-12-18T10:28:44Z
[ "python", "multiprocessing" ]
What problems will one see in using Python multiprocessing naively?
1,925,718
<p>We're considering re-factoring a large application with a complex GUI which is isolated in a decoupled fashion from the back-end, to use the new (Python 2.6) multiprocessing module. The GUI/backend interface uses Queues with Message objects exchanged in both directions.</p> <p>One thing I've just concluded (tentat...
7
2009-12-18T01:19:13Z
1,964,208
<p>You can try the <code>persistent</code> package from my project GarlicSim. It's LGPL'ed.</p> <p><a href="http://github.com/cool-RR/GarlicSim/tree/development/garlicsim/garlicsim/misc/persistent/" rel="nofollow">http://github.com/cool-RR/GarlicSim/tree/development/garlicsim/garlicsim/misc/persistent/</a></p> <p>(Th...
1
2009-12-26T19:54:47Z
[ "python", "multiprocessing" ]
Python programming on Eclipse with Pydev
1,925,750
<p>I need major help getting started! I managed to create a new project, and add python.exe as the interpreter. But when the project is created it's blank. How do I start programming? Ugh.</p>
0
2009-12-18T01:30:49Z
1,925,758
<p>Open a new text file and start writing code?</p>
4
2009-12-18T01:32:55Z
[ "python", "eclipse", "ide", "pydev" ]
Python programming on Eclipse with Pydev
1,925,750
<p>I need major help getting started! I managed to create a new project, and add python.exe as the interpreter. But when the project is created it's blank. How do I start programming? Ugh.</p>
0
2009-12-18T01:30:49Z
1,925,760
<ol> <li>Create PyDev project</li> <li>Add "Source Folder" under the project</li> <li>Add "Modules" to the "Source Folder"</li> <li>Get coding :-)</li> </ol>
4
2009-12-18T01:33:31Z
[ "python", "eclipse", "ide", "pydev" ]
Python programming on Eclipse with Pydev
1,925,750
<p>I need major help getting started! I managed to create a new project, and add python.exe as the interpreter. But when the project is created it's blank. How do I start programming? Ugh.</p>
0
2009-12-18T01:30:49Z
1,925,767
<p>You need to make sure you create a Pydev project. If you don't already have a Python interpreter installed, you'll need to get one (this is mainly a problem on Windows, ActiveState is a great commercial grade distribution, python.org also has some great free builds of Python.) Once this is complete, you'll need to g...
1
2009-12-18T01:34:51Z
[ "python", "eclipse", "ide", "pydev" ]
python import fails when called from PHP
1,925,764
<p>I'm having a puzzling problem when trying to import a module in python only when the script is called from php via system or exec.</p> <p>From the python shell:</p> <pre><code>import igraph #This works. </code></pre> <p>if the previous line was in a file, say, test_module.py, then:<br> python test_module.py in th...
0
2009-12-18T01:34:39Z
1,925,782
<p>Is the igraph module in Python's standard module path, or is it in the same directory as your individual script? If so, it's quite possible that PHP is calling the python file with a different working directory, and it's trying to import things relative to that path instead of the path of the script.</p>
0
2009-12-18T01:40:37Z
[ "php", "python", "import", "exec" ]
python import fails when called from PHP
1,925,764
<p>I'm having a puzzling problem when trying to import a module in python only when the script is called from php via system or exec.</p> <p>From the python shell:</p> <pre><code>import igraph #This works. </code></pre> <p>if the previous line was in a file, say, test_module.py, then:<br> python test_module.py in th...
0
2009-12-18T01:34:39Z
1,925,785
<p>one thing to check is <code>sys.path</code></p> <p>see what the difference is when called each way</p>
1
2009-12-18T01:41:42Z
[ "php", "python", "import", "exec" ]
python import fails when called from PHP
1,925,764
<p>I'm having a puzzling problem when trying to import a module in python only when the script is called from php via system or exec.</p> <p>From the python shell:</p> <pre><code>import igraph #This works. </code></pre> <p>if the previous line was in a file, say, test_module.py, then:<br> python test_module.py in th...
0
2009-12-18T01:34:39Z
34,461,628
<p>This is happening because you have installed those packages under a different user, maybe root, or something else. </p> <p>How i debugged this, is i checked the output of sys.path for both cases (shell, and php's exec, which has the user www-data by default), and than i compared both. </p> <p>I noticed the <code...
0
2015-12-25T09:23:28Z
[ "php", "python", "import", "exec" ]
Eclipse (with Pydev) keeps throwing SyntaxError
1,925,852
<p>My code:</p> <p><code>print "Hello World!"</code></p> <p>I even tried adding a semicolon behind, but everytime I save and run (as Python run) it says:</p> <blockquote> <p>File "E:\Software\Eclipse\Workspace\Python1\src\main.py", line 1 print "Hello World!";</p> <p>SyntaxError: invalid syntax</p> </blockq...
13
2009-12-18T02:07:04Z
1,925,861
<p>In Python, indentation is really important... Have you check your indentation? Also, lose the <code>;</code> (don't need it).</p> <p>correct:</p> <p><code>print("hello")</code> or <code>print "hello"</code> (for &lt; 3.0)</p> <p>not correct:</p> <p><code>...print("hello")</code> or <code>print "hello"</code> (f...
1
2009-12-18T02:10:27Z
[ "python", "eclipse", "syntax", "pydev" ]
Eclipse (with Pydev) keeps throwing SyntaxError
1,925,852
<p>My code:</p> <p><code>print "Hello World!"</code></p> <p>I even tried adding a semicolon behind, but everytime I save and run (as Python run) it says:</p> <blockquote> <p>File "E:\Software\Eclipse\Workspace\Python1\src\main.py", line 1 print "Hello World!";</p> <p>SyntaxError: invalid syntax</p> </blockq...
13
2009-12-18T02:07:04Z
1,925,865
<p>This is kind of a longshot but - if you're running python 3.0 that is invalid syntax. Try </p> <pre><code>print("Hello World!") </code></pre> <p>to see if this is the case.</p>
4
2009-12-18T02:12:22Z
[ "python", "eclipse", "syntax", "pydev" ]
Eclipse (with Pydev) keeps throwing SyntaxError
1,925,852
<p>My code:</p> <p><code>print "Hello World!"</code></p> <p>I even tried adding a semicolon behind, but everytime I save and run (as Python run) it says:</p> <blockquote> <p>File "E:\Software\Eclipse\Workspace\Python1\src\main.py", line 1 print "Hello World!";</p> <p>SyntaxError: invalid syntax</p> </blockq...
13
2009-12-18T02:07:04Z
1,925,868
<p>What version of Python are you using? Python 2.X has <code>print</code> as a keyword, but Python 3.X only has <code>print()</code> as a function - you'd need to use <code>print("Hello, World!")</code> instead.</p>
34
2009-12-18T02:12:43Z
[ "python", "eclipse", "syntax", "pydev" ]
Django Unhandled Exception
1,925,898
<p>It is running under DEBUG = True mode. Sometimes it can throw out an error message with traceback information when encounter an error but sometimes it just display the following lines:</p> <pre><code>Unhandled Exception An unhandled exception was thrown by the application. </code></pre> <p>I have to switch to dev...
9
2009-12-18T02:24:23Z
1,925,931
<p>That's what DEBUG=True is for: to show the full traceback. The idea is that regular users do not want (nor do you want them to) see anything other than a simple error message.</p>
1
2009-12-18T02:36:48Z
[ "python", "django" ]
Django Unhandled Exception
1,925,898
<p>It is running under DEBUG = True mode. Sometimes it can throw out an error message with traceback information when encounter an error but sometimes it just display the following lines:</p> <pre><code>Unhandled Exception An unhandled exception was thrown by the application. </code></pre> <p>I have to switch to dev...
9
2009-12-18T02:24:23Z
1,925,939
<p>Are you using Apache?<br/> Just out of interest is this your Production or Dev environment where you want to see the traceback?</p> <p>From the <a href="http://www.djangobook.com/en/beta/chapter20/" rel="nofollow">DJango Book on security - Exposed error messages</a><br/></p> <p><i>Users deploying under Apache and ...
3
2009-12-18T02:38:54Z
[ "python", "django" ]
Django Unhandled Exception
1,925,898
<p>It is running under DEBUG = True mode. Sometimes it can throw out an error message with traceback information when encounter an error but sometimes it just display the following lines:</p> <pre><code>Unhandled Exception An unhandled exception was thrown by the application. </code></pre> <p>I have to switch to dev...
9
2009-12-18T02:24:23Z
1,926,186
<p>Maybe you can use this snippet, this will log exceptions in apache's log:</p> <p><code>utils.py</code>:</p> <pre><code>def log_traceback(exception, args): import sys, traceback, logging exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() logging.debug(exception) logging.debug(args) ...
6
2009-12-18T03:56:56Z
[ "python", "django" ]
Django Unhandled Exception
1,925,898
<p>It is running under DEBUG = True mode. Sometimes it can throw out an error message with traceback information when encounter an error but sometimes it just display the following lines:</p> <pre><code>Unhandled Exception An unhandled exception was thrown by the application. </code></pre> <p>I have to switch to dev...
9
2009-12-18T02:24:23Z
4,585,251
<p>Just connect to the got_request_exception signal and log the exception:</p> <pre><code>from django.core.signals import got_request_exception import logging def log(*args, **kwargs): logging.exception('error') got_request_exception.connect(log) </code></pre> <p>This will log the whole trace. In the dev se...
9
2011-01-03T14:27:28Z
[ "python", "django" ]
How to get the LAN IP that a socket is sending (linux)
1,925,974
<p>I need some code to get the address of the socket i just created (to filter out packets originating from localhost on a multicast network)</p> <p>this:</p> <p>socket.gethostbyname(socket.gethostname())</p> <p>works on mac but it returns only the localhost IP in linux... is there anyway to get the LAN address</p> ...
0
2009-12-18T02:49:46Z
1,926,048
<p>quick answer - <code>socket.getpeername()</code> (provided that socket is a socket object, not a module)</p> <p>(playing around in python/ipython/idle/... interactive shell is very helpful)</p> <p>.. or if I read you question carefully, maybe <code>socket.getsockname()</code> :)</p>
0
2009-12-18T03:12:01Z
[ "python", "sockets", "ip-address" ]
How to get the LAN IP that a socket is sending (linux)
1,925,974
<p>I need some code to get the address of the socket i just created (to filter out packets originating from localhost on a multicast network)</p> <p>this:</p> <p>socket.gethostbyname(socket.gethostname())</p> <p>works on mac but it returns only the localhost IP in linux... is there anyway to get the LAN address</p> ...
0
2009-12-18T02:49:46Z
1,926,054
<p>Looks like you're looking for <a href="http://docs.python.org/library/socket.html?highlight=socket#socket.socket.getsockname" rel="nofollow">the <code>getsockname</code> method of socket objects</a>.</p>
1
2009-12-18T03:13:27Z
[ "python", "sockets", "ip-address" ]
this python script can be shortened/optimized, how?
1,925,999
<p>I'm not used to doing things the python way yet, but I'm almost certain the following script can be condensed. I'm not looking for speed optimization here, I'm looking for more readable code. Make it slower for all I care, but what are some ways to make this look more Python-esque.</p> <p>I'm simply reading in a cs...
1
2009-12-18T02:55:56Z
1,926,022
<p>Please do not build SQL text like that. Please do not. Please.</p> <p>First. The variable <code>ziplist</code> is not used. Delete it.</p> <p>Second. Use real SQL binding.</p> <pre><code>c.execute( "INSERT...", row ) </code></pre> <p>This is documented in the MySQLdb interface. <a href="http://mysql-python...
3
2009-12-18T03:02:34Z
[ "python" ]
this python script can be shortened/optimized, how?
1,925,999
<p>I'm not used to doing things the python way yet, but I'm almost certain the following script can be condensed. I'm not looking for speed optimization here, I'm looking for more readable code. Make it slower for all I care, but what are some ways to make this look more Python-esque.</p> <p>I'm simply reading in a cs...
1
2009-12-18T02:55:56Z
1,926,029
<p>This part:</p> <pre><code>multis = '%s, '*23 multis = multis[:-2] </code></pre> <p>should be</p> <pre><code>multis = ', '.join(['%s'] * 23) </code></pre> <p><code>ziplist</code> is not used, so you can just remove the line that sets it.</p> <pre><code> (row[0], row[1], row[2], row[3], row[4], row[5],...
7
2009-12-18T03:04:05Z
[ "python" ]
Python programming general questions
1,926,019
<p>I heard that Python is easy and powerful, but I don't know if I'm on the right track to learn it. I learn from online tutorials, I know basic maths calculation and printing strings, but how long will it take to develop something useful? I don't really know the exact uses of Python, though.</p>
0
2009-12-18T03:02:11Z
1,926,032
<p>I'm not exactly sure what you're looking for, but I think one or more of the following may be the next step you're looking for. </p> <p>Perhaps you would like to use a variety of different protocols for a <strong>networking</strong> program, you could check out <a href="http://twistedmatrix.com/trac/wiki/TwistedPro...
8
2009-12-18T03:05:28Z
[ "python" ]
Python programming general questions
1,926,019
<p>I heard that Python is easy and powerful, but I don't know if I'm on the right track to learn it. I learn from online tutorials, I know basic maths calculation and printing strings, but how long will it take to develop something useful? I don't really know the exact uses of Python, though.</p>
0
2009-12-18T03:02:11Z
1,926,040
<p>What other programming background do you have? What programming <em>interests</em> do you have -- web apps, numerical / scientific computations, games, ...? Python is good at many different things, for both beginners and experienced programmers, but the most fruitful approach(es) to it do depend on what you alread...
1
2009-12-18T03:08:12Z
[ "python" ]
Python programming general questions
1,926,019
<p>I heard that Python is easy and powerful, but I don't know if I'm on the right track to learn it. I learn from online tutorials, I know basic maths calculation and printing strings, but how long will it take to develop something useful? I don't really know the exact uses of Python, though.</p>
0
2009-12-18T03:02:11Z
1,926,046
<p>Python is a general purpose language. You can use it to make a lot of different things, but it's best suited at stuff that doesn't require a lot of speed, since the high level features have a performance cost.</p> <p>It's hard to tell how long it will take you to develop something useful. The other day I made a scr...
4
2009-12-18T03:11:04Z
[ "python" ]
Python programming general questions
1,926,019
<p>I heard that Python is easy and powerful, but I don't know if I'm on the right track to learn it. I learn from online tutorials, I know basic maths calculation and printing strings, but how long will it take to develop something useful? I don't really know the exact uses of Python, though.</p>
0
2009-12-18T03:02:11Z
1,926,050
<p>The best way to learn how to do something useful is come up with something useful you want to do. Make sure it's not way out of your league, then do research to accomplish it. That's how a lot of programmers learn languages.</p>
2
2009-12-18T03:12:22Z
[ "python" ]
Python programming general questions
1,926,019
<p>I heard that Python is easy and powerful, but I don't know if I'm on the right track to learn it. I learn from online tutorials, I know basic maths calculation and printing strings, but how long will it take to develop something useful? I don't really know the exact uses of Python, though.</p>
0
2009-12-18T03:02:11Z
1,926,053
<p>Have a look at Mark Pilgrim's freely available book called <a href="http://www.diveintopython.org/" rel="nofollow">Dive Into Python</a>. I think it's a better choice to start with than online tutorials. The best way to learn a language is to start to work on a project. As I read this book, I started to implement a s...
3
2009-12-18T03:13:10Z
[ "python" ]
Python programming general questions
1,926,019
<p>I heard that Python is easy and powerful, but I don't know if I'm on the right track to learn it. I learn from online tutorials, I know basic maths calculation and printing strings, but how long will it take to develop something useful? I don't really know the exact uses of Python, though.</p>
0
2009-12-18T03:02:11Z
1,926,100
<p>Like every other programming language In order to learn Python you need to write a program with it.<br> Find a pet project and use python to code it. I also recommend Dive into python" (like anyone else that answered your question).</p> <p>A few months ago I've decided to learn IronPython (.NET implementation of py...
3
2009-12-18T03:31:15Z
[ "python" ]
Python programming general questions
1,926,019
<p>I heard that Python is easy and powerful, but I don't know if I'm on the right track to learn it. I learn from online tutorials, I know basic maths calculation and printing strings, but how long will it take to develop something useful? I don't really know the exact uses of Python, though.</p>
0
2009-12-18T03:02:11Z
1,926,110
<p>Bram who invented bittorrent with python says python is good for writing protocols in addition to aforementioned webapps, games and general purpose. Compared to Java python solves same problem in less code and less ways in longer development and VM time where Java has more ways solve same problem in more code and fa...
0
2009-12-18T03:32:32Z
[ "python" ]