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 |
|---|---|---|---|---|---|---|---|---|---|
Reading and running a mathematical expression in Python | 400,050 | <p>Using Python, how would I go about reading in (be from a string, file or url) a mathematical expression (1 + 1 is a good start) and executing it? </p>
<p>Aside from grabbing a string, file or url I have no idea of where to start with this.</p>
| 5 | 2008-12-30T11:22:43Z | 403,252 | <p>Don't writing your own parser unless you want to learn how to write a parser. As already mentioned in the comments by @J.F. Sebastian, I would suggest a full-on <a href="http://en.wikipedia.org/wiki/Computer_algebra_system" rel="nofollow">computer algebra system (CAS)</a> like <a href="http://www.sagemath.org/" rel=... | 2 | 2008-12-31T15:45:28Z | [
"python"
] |
Reading and running a mathematical expression in Python | 400,050 | <p>Using Python, how would I go about reading in (be from a string, file or url) a mathematical expression (1 + 1 is a good start) and executing it? </p>
<p>Aside from grabbing a string, file or url I have no idea of where to start with this.</p>
| 5 | 2008-12-30T11:22:43Z | 5,115,775 | <p>You can take advantage of Python's own evaluation capabilities. However, blind use of eval() is a very dangerous, since somebody can trick your program into:</p>
<pre><code>eval( (__import__("os").system("rm important_file") or 1) + 1)
</code></pre>
<p>The correct way to use eval is with the following receipe, whi... | 0 | 2011-02-25T09:51:54Z | [
"python"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 400,838 | <pre><code>import jpeg
jpeg.setExif(jpeg.getExif('foo.jpg'), 'foo-resized.jpg')
</code></pre>
<p><a href="http://www.emilas.com/jpeg/">http://www.emilas.com/jpeg/</a></p>
| 12 | 2008-12-30T17:00:10Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 400,840 | <p>PIL handles EXIF data, doesn't it? Look in PIL.ExifTags.</p>
| 0 | 2008-12-30T17:01:28Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 401,208 | <p>Why not using <a href="http://www.imagemagick.org/script/index.php" rel="nofollow">ImageMagick</a>?<br>
It is quite a standard tool (for instance, it is the standard tool used by Gallery 2); I have never used it, however it has a <a href="http://www.imagemagick.org/download/python/" rel="nofollow">python interface</... | 4 | 2008-12-30T19:27:17Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 403,293 | <p>You can use pyexiv2 to modify the file after saving it.</p>
| 1 | 2008-12-31T15:59:57Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 4,270,949 | <p>You can use pyexiv2 to copy EXIF data from source image. In the following example image is resized using PIL library, EXIF data copied with pyexiv2 and image size EXIF fields are set with new size.</p>
<pre><code>def resize_image(source_path, dest_path, size):
# resize image
image = Image.open(source_path)
... | 10 | 2010-11-24T19:57:13Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 17,549,368 | <p>For pyexiv2 v0.3.2, the <a href="http://tilloy.net/dev/pyexiv2/api.html#pyexiv2.metadata.ImageMetadata.copy" rel="nofollow">API documentation</a> refers to the copy method to carry over EXIF data from one image to another. In this case it would be the EXIF data of the original image over to the resized image.</p>
<... | 1 | 2013-07-09T13:24:08Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 22,063,878 | <p>There is actually a really simple way of copying EXIF data from a picture to another with only PIL. Though it doesn't permit to modify the exif tags.</p>
<pre class="lang-py prettyprint-override"><code>image = Image.open('test.jpg')
exif = image.info['exif']
# Your picture process here
image = image.rotate(90)
imag... | 7 | 2014-02-27T09:13:16Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 25,659,450 | <pre><code>from PIL import Image
img_path = "/tmp/img.jpg"
img = Image.open(img_path)
exif = img.info['exif']
img.save("output_"+img_path, exif=exif)
</code></pre>
<p>Tested in Pillow 2.5.3</p>
| 0 | 2014-09-04T07:18:15Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Resize image in Python without losing EXIF data | 400,788 | <p>I need to resize jpg images with Python without losing the original image's EXIF data (metadata about date taken, camera model etc.). All google searches about python and images point to the PIL library which I'm currently using, but doesn't seem to be able to retain the metadata. The code I have so far (using PIL) ... | 23 | 2008-12-30T16:42:02Z | 30,236,764 | <p>It seems @Depado's solution does not work for me, in my scenario the image does not even contain an exif segment.</p>
<p>pyexiv2 is hard to install on my Mac, instead I use the module pexif <a href="https://github.com/bennoleslie/pexif/blob/master/pexif.py" rel="nofollow">https://github.com/bennoleslie/pexif/blob/m... | 0 | 2015-05-14T11:55:44Z | [
"python",
"image-processing",
"python-imaging-library",
"exif"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 401,030 | <p>Answered my own question. <em>Sigh</em></p>
<p><a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs">http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs</a></p>
<p>I didn't realize it was passed into the widget constructor.</p>
| 66 | 2008-12-30T18:20:28Z | [
"python",
"django",
"forms",
"newforms"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 401,057 | <p>Here is another solution for adding class definitions to the widgets after declaring the fields in the class.</p>
<pre><code>def __init__(self, *args, **kwargs):
super(SampleClass, self).__init__(*args, **kwargs)
self.fields['name'].widget.attrs['class'] = 'my_class'
</code></pre>
| 59 | 2008-12-30T18:32:11Z | [
"python",
"django",
"forms",
"newforms"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 2,377,554 | <p>Here is a variation on the above which will give all fields the same class (e.g. jquery nice rounded corners).</p>
<pre><code> # Simple way to assign css class to every field
def __init__(self, *args, **kwargs):
super(TranslatedPageForm, self).__init__(*args, **kwargs)
for myField in self.fields:
s... | 3 | 2010-03-04T07:38:15Z | [
"python",
"django",
"forms",
"newforms"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 4,272,671 | <p>Expanding on the method pointed to at docs.djangoproject.com:</p>
<pre><code>class MyForm(forms.Form):
comment = forms.CharField(
widget=forms.TextInput(attrs={'size':'40'}))
</code></pre>
<p>I thought it was troublesome to have to know the native widget type for every field, and thought it funny ... | 23 | 2010-11-25T00:06:15Z | [
"python",
"django",
"forms",
"newforms"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 4,667,831 | <p>Here is Simple way to alter in view. add below in view just before passing it into template.</p>
<pre><code>form = MyForm(instance = instance.obj)
form.fields['email'].widget.attrs = {'class':'here_class_name'}
</code></pre>
| 13 | 2011-01-12T10:42:45Z | [
"python",
"django",
"forms",
"newforms"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 4,676,071 | <p>Yet another solution that doesn't require changes in python code and so is better for designers and one-off presentational changes: <a href="https://github.com/kmike/django-widget-tweaks">django-widget-tweaks</a>. Hope somebody will find it useful.</p>
| 128 | 2011-01-13T01:56:34Z | [
"python",
"django",
"forms",
"newforms"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 18,962,481 | <p>This can be done using a custom template filter.
considering you render your form this way :</p>
<pre><code><form action="/contact/" method="post">
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="id_subject">Email subject:</la... | 26 | 2013-09-23T14:55:01Z | [
"python",
"django",
"forms",
"newforms"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 20,573,612 | <p>If you want all the fields in the form to inherit a certain class, you just define a parent class, that inherits from <code>forms.ModelForm</code>, and then inherit from it</p>
<pre><code>class BaseForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(BaseForm, self).__init__(*args, **kwargs... | 12 | 2013-12-13T18:35:43Z | [
"python",
"django",
"forms",
"newforms"
] |
Define css class in django Forms | 401,025 | <p>Assume I have a form</p>
<pre><code>class SampleClass(forms.Form):
name = forms.CharField(max_length=30)
age = forms.IntegerField()
django_hacker = forms.BooleanField(required=False)
</code></pre>
<p>Is there a way for me to define css classes on each field such that I can then use jQuery based on clas... | 125 | 2008-12-30T18:18:41Z | 28,874,624 | <p>As it turns out you can do this in form constructor (<strong>init</strong> function) or after form class was initiated. This is sometimes required if you are not writing your own form and that form is coming from somewhere else -</p>
<pre><code>def some_view(request):
add_css_to_fields = ['list','of','fields']... | 1 | 2015-03-05T09:44:53Z | [
"python",
"django",
"forms",
"newforms"
] |
ForeignKey form restrictions in Django | 401,118 | <p>I'm using Django to write a blog app, and I'm trying to implement a hierarchical category structure. Each category has a "parent" ForeignKey pointing back to the same Category model. I want to allow admins to add categories, and I want the interface to allow them to select a category's parent category. However, I... | 4 | 2008-12-30T19:04:16Z | 401,186 | <p>"Is there any better way to do this?" Not really. Hierarchies are hard in the relational model. Nothing makes the easier except giving up on SQL entirely.</p>
<p>"write the selection mechanism into my template by looping through pruned_parent_list to get the options" -- probably not optimal. This should happen ... | 0 | 2008-12-30T19:20:22Z | [
"python",
"django",
"django-forms"
] |
ForeignKey form restrictions in Django | 401,118 | <p>I'm using Django to write a blog app, and I'm trying to implement a hierarchical category structure. Each category has a "parent" ForeignKey pointing back to the same Category model. I want to allow admins to add categories, and I want the interface to allow them to select a category's parent category. However, I... | 4 | 2008-12-30T19:04:16Z | 401,256 | <p>If I understand your predicament correctly, the problem itself lies with the way you're dealing with which categories can be parents and which ones can't. One option to avoid these problems is to actually limit the level of categories which can become parents. For example, let's say you have the following categori... | 0 | 2008-12-30T19:42:19Z | [
"python",
"django",
"django-forms"
] |
ForeignKey form restrictions in Django | 401,118 | <p>I'm using Django to write a blog app, and I'm trying to implement a hierarchical category structure. Each category has a "parent" ForeignKey pointing back to the same Category model. I want to allow admins to add categories, and I want the interface to allow them to select a category's parent category. However, I... | 4 | 2008-12-30T19:04:16Z | 401,428 | <p>I am not sure if this is better (interaction-wise or otherwise) but...</p>
<p>You can check hierarchical integrity on save and raise an error if necessary.</p>
<p>Ideally for such a data type I would like to see a tree of instances on the side. Or at least the full ancestory on the object detail view. In both case... | 0 | 2008-12-30T20:36:29Z | [
"python",
"django",
"django-forms"
] |
ForeignKey form restrictions in Django | 401,118 | <p>I'm using Django to write a blog app, and I'm trying to implement a hierarchical category structure. Each category has a "parent" ForeignKey pointing back to the same Category model. I want to allow admins to add categories, and I want the interface to allow them to select a category's parent category. However, I... | 4 | 2008-12-30T19:04:16Z | 402,530 | <p>I have had to deal with arbitrary-depth categories on SQL and it seems not well suited for storing data of this type in a normal form, as nested queries and/or multiple JOINs tend to get ugly <em>extremely</em> quickly.</p>
<p>This is almost the only case where I would go with a sort of improper solution, namely to... | 1 | 2008-12-31T08:20:45Z | [
"python",
"django",
"django-forms"
] |
ForeignKey form restrictions in Django | 401,118 | <p>I'm using Django to write a blog app, and I'm trying to implement a hierarchical category structure. Each category has a "parent" ForeignKey pointing back to the same Category model. I want to allow admins to add categories, and I want the interface to allow them to select a category's parent category. However, I... | 4 | 2008-12-30T19:04:16Z | 406,752 | <p>Take a look at the <a href="http://code.google.com/p/django-treebeard/" rel="nofollow">django-treebeard</a> app.</p>
| 1 | 2009-01-02T13:09:01Z | [
"python",
"django",
"django-forms"
] |
How to limit rate of requests to web services in Python? | 401,215 | <p>I'm working on a Python library that interfaces with a web service API. Like many web services I've encountered, this one requests limiting the rate of requests. I would like to provide an optional parameter, <code>limit</code>, to the class instantiation that, if provided, will hold outgoing requests until the numb... | 9 | 2008-12-30T19:30:11Z | 401,316 | <p>This works out better with a queue and a dispatcher.</p>
<p>You split your processing into two sides: <strong>source</strong> and <strong>dispatch</strong>. These can be separate threads (or separate processes if that's easier).</p>
<p>The <strong>Source</strong> side creates and enqueues requests at whatever rat... | 5 | 2008-12-30T20:00:33Z | [
"python",
"web-services",
"rate-limiting"
] |
How to limit rate of requests to web services in Python? | 401,215 | <p>I'm working on a Python library that interfaces with a web service API. Like many web services I've encountered, this one requests limiting the rate of requests. I would like to provide an optional parameter, <code>limit</code>, to the class instantiation that, if provided, will hold outgoing requests until the numb... | 9 | 2008-12-30T19:30:11Z | 401,332 | <p>Your rate limiting scheme should be heavily influenced by the calling conventions of the underlying code (syncronous or async), as well as what scope (thread, process, machine, cluster?) this rate-limiting will operate at.</p>
<p>I would suggest keeping all the variables within the instance, so you can easily imple... | 1 | 2008-12-30T20:05:47Z | [
"python",
"web-services",
"rate-limiting"
] |
How to limit rate of requests to web services in Python? | 401,215 | <p>I'm working on a Python library that interfaces with a web service API. Like many web services I've encountered, this one requests limiting the rate of requests. I would like to provide an optional parameter, <code>limit</code>, to the class instantiation that, if provided, will hold outgoing requests until the numb... | 9 | 2008-12-30T19:30:11Z | 401,390 | <p>Queuing may be overly complicated. A simpler solution is to give your class a variable for the time the service was last called. Whenever the service is called (!1), set waitTime to <code>delay - Now + lastcalltime</code>. <code>delay</code> should be equal to the minimum allowable time between requests. If this... | 2 | 2008-12-30T20:23:16Z | [
"python",
"web-services",
"rate-limiting"
] |
How to limit rate of requests to web services in Python? | 401,215 | <p>I'm working on a Python library that interfaces with a web service API. Like many web services I've encountered, this one requests limiting the rate of requests. I would like to provide an optional parameter, <code>limit</code>, to the class instantiation that, if provided, will hold outgoing requests until the numb... | 9 | 2008-12-30T19:30:11Z | 401,445 | <p>If your library is designed to be synchronous, then I'd recommend leaving out the limit enforcement (although you could track rates and at least help the caller decide how to honor limits).</p>
<p>I use <a href="http://twistedmatrix.com/" rel="nofollow">twisted</a> to interface with pretty much everything nowadays.... | 1 | 2008-12-30T20:43:37Z | [
"python",
"web-services",
"rate-limiting"
] |
How to limit rate of requests to web services in Python? | 401,215 | <p>I'm working on a Python library that interfaces with a web service API. Like many web services I've encountered, this one requests limiting the rate of requests. I would like to provide an optional parameter, <code>limit</code>, to the class instantiation that, if provided, will hold outgoing requests until the numb... | 9 | 2008-12-30T19:30:11Z | 401,826 | <p>SO I am assuming something simple like
import time
time.sleep(2)
will not work for waiting 2 seconds between requests</p>
| 0 | 2008-12-30T23:25:39Z | [
"python",
"web-services",
"rate-limiting"
] |
Naming Python loggers | 401,277 | <p>In Django, I've got loggers all over the place, currently with hard-coded names.</p>
<p>For module-level logging (i.e., in a module of view functions) I have the urge to do this.</p>
<pre><code>log = logging.getLogger(__name__)
</code></pre>
<p>For class-level logging (i.e., in a class <code>__init__</code> metho... | 44 | 2008-12-30T19:47:53Z | 401,352 | <p>That looks like it will work, except that <code>self</code> won't have a <code>__module__</code> attribute; its class will. The class-level logger call should look like:</p>
<pre><code>self.log = logging.getLogger( "%s.%s" % ( self.__class__.__module__, self.__class__.__name__ ) )
</code></pre>
| 2 | 2008-12-30T20:12:00Z | [
"python",
"django",
"logging"
] |
Naming Python loggers | 401,277 | <p>In Django, I've got loggers all over the place, currently with hard-coded names.</p>
<p>For module-level logging (i.e., in a module of view functions) I have the urge to do this.</p>
<pre><code>log = logging.getLogger(__name__)
</code></pre>
<p>For class-level logging (i.e., in a class <code>__init__</code> metho... | 44 | 2008-12-30T19:47:53Z | 402,471 | <p>I typically don't use or find a need for class-level loggers, but I keep my modules at a few classes at most. A simple:</p>
<pre><code>import logging
LOG = logging.getLogger(__name__)
</code></pre>
<p>At the top of the module and subsequent:</p>
<pre><code>LOG.info('Spam and eggs are tasty!')
</code></pre>
<p>fr... | 63 | 2008-12-31T07:31:40Z | [
"python",
"django",
"logging"
] |
Naming Python loggers | 401,277 | <p>In Django, I've got loggers all over the place, currently with hard-coded names.</p>
<p>For module-level logging (i.e., in a module of view functions) I have the urge to do this.</p>
<pre><code>log = logging.getLogger(__name__)
</code></pre>
<p>For class-level logging (i.e., in a class <code>__init__</code> metho... | 44 | 2008-12-30T19:47:53Z | 1,628,641 | <p>For class level logging, as an alternative to a pseudo-class decorator, you could use a metaclass to make the logger for you at class creation time...</p>
<pre><code>import logging
class Foo(object):
class __metaclass__(type):
def __init__(cls, name, bases, attrs):
type.__init__(name, bases... | 3 | 2009-10-27T03:36:18Z | [
"python",
"django",
"logging"
] |
Regex that only matches text that's not part of HTML markup? (python) | 401,726 | <p>How can I make a pattern match so long as it's not inside of an HTML tag?</p>
<p>Here's my attempt below. Anyone have a better/different approach?</p>
<pre><code>import re
inputstr = 'mary had a <b class="foo"> little loomb</b>'
rx = re.compile('[aob]')
repl = 'x'
outputstr = ''
i = 0
for astr in r... | 1 | 2008-12-30T22:33:22Z | 401,774 | <p>Since you are using Python anyway, if I were you, I would have a look at <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a>, which is a <strong>Python HTML/XML parser</strong>. Really, there are so many special cases and headaches with writing your own parser, it just doesn't w... | 12 | 2008-12-30T22:56:26Z | [
"python",
"regex"
] |
Scaling the y-axis with Matplotlib in Python | 401,787 | <p>How to scale the y-axis with <a href="http://matplotlib.sourceforge.net/" rel="nofollow">Matplotlib</a>? I don't want to change the y-limit, I just want to extend the physical space.</p>
<pre><code>^ ^
| |
| |
+----> |
Before +---->
After
</code></pre>
| 4 | 2008-12-30T23:02:08Z | 401,823 | <p>Just use a larger height value when you instantiate the figure:</p>
<pre><code>from pylab import *
x = linspace(0, 10*pi, 2**10)
y = sin(x)
figure(figsize=(5, 10))
plot(x, y)
show()
</code></pre>
<p>Where <code>figsize=(width, height)</code> and defaults to <code>(8, 6)</code>. Values are in inches (the <code>dpi... | 7 | 2008-12-30T23:21:52Z | [
"python",
"matplotlib"
] |
Scaling the y-axis with Matplotlib in Python | 401,787 | <p>How to scale the y-axis with <a href="http://matplotlib.sourceforge.net/" rel="nofollow">Matplotlib</a>? I don't want to change the y-limit, I just want to extend the physical space.</p>
<pre><code>^ ^
| |
| |
+----> |
Before +---->
After
</code></pre>
| 4 | 2008-12-30T23:02:08Z | 404,971 | <p>Use the subplots_adjust function to control the abount of whitespace:</p>
<p>fig.subplots_adust(bottom=0.05, top=0.95)</p>
<p>There is an icon on the toolbar to do this interactively with a widget </p>
| 2 | 2009-01-01T14:00:22Z | [
"python",
"matplotlib"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 402,248 | <p>If you don't need to query by any of this extra data, then you can store it as a serialized dictionary. Use <code>repr</code> to turn the dictionary into a string, and <code>eval</code> to turn the string back into a dictionary. Take care with eval that there's no user data in the dictionary, or use a safe_eval i... | 13 | 2008-12-31T03:39:18Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 402,387 | <p>Think it over, and find the commonalities of each data set... then define your model. It may require the use of subclasses or not. Foreign keys representing commonalities aren't to be avoided, but encouraged when they make sense.</p>
<p>Stuffing random data into a SQL table is not smart, unless it's truly non-rel... | 2 | 2008-12-31T05:54:12Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 402,452 | <p>I'm not sure exactly sure of the nature of the problem you're trying to solve, but it sounds curiously similar to <a href="http://code.google.com/appengine/docs/datastore/expandoclass.html" rel="nofollow">Google App Engine's BigTable Expando</a>.</p>
<p>Expandos allow you to specify and store additional fields on a... | 3 | 2008-12-31T07:09:29Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 402,487 | <p>As Ned answered, you won't be able to query "some data" if you use the dictionary approach.</p>
<p>If you still need to store dictionaries then the best approach, by far, is the PickleField class documented in Marty Alchin's new book <strong>Pro Django</strong>. This method uses Python class properties to pickle/u... | 6 | 2008-12-31T07:44:46Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 402,526 | <p>If it's really dictionary like arbitrary data you're looking for you can probably use a two-level setup with one model that's a container and another model that's key-value pairs. You'd create an instance of the container, create each of the key-value instances, and associate the set of key-value instances with the ... | 22 | 2008-12-31T08:19:28Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 410,919 | <p>Being "not equal to all instances of the model" sounds to me like a good match for a "Schema-free database". <a href="http://couchdb.apache.org/" rel="nofollow">CouchDB</a> is the poster child for that approach and you might consider that. </p>
<p>In a project I moved several tables which never played very nice wit... | 3 | 2009-01-04T12:28:20Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 859,088 | <p>Django-Geo includes a "DictionaryField" you might find helpful:</p>
<p><a href="http://code.google.com/p/django-geo/source/browse/trunk/fields.py?r=13#49" rel="nofollow">http://code.google.com/p/django-geo/source/browse/trunk/fields.py?r=13#49</a></p>
<p>In general, if you don't need to query across the data use a... | 2 | 2009-05-13T16:50:37Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 859,470 | <p>I agree that you need to refrain stuffing otherwise structured data into a single column. But if you must do that, Django has an <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#xmlfield" rel="nofollow">XMLField</a> build-in.</p>
<p>There's also <a href="http://www.djangosnippets.org/snippets/377/" ... | 2 | 2009-05-13T18:05:44Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 7,469,287 | <p>I came to this post by google's 4rth result to "django store object"</p>
<p>A little bit late, but <a href="http://pypi.python.org/pypi/django-picklefield">django-picklefield</a> looks like good solution to me.</p>
<p>Example from doc:</p>
<p>To use, just define a field in your model:</p>
<pre><code>>>>... | 5 | 2011-09-19T10:10:04Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 17,970,922 | <p>Another clean and fast solution can be found here: <a href="https://github.com/bradjasper/django-jsonfield" rel="nofollow">https://github.com/bradjasper/django-jsonfield</a></p>
<p>For convenience I copied the simple instructions.</p>
<p><strong>Install</strong></p>
<pre><code>pip install jsonfield
</code></pre>
... | 3 | 2013-07-31T12:50:28Z | [
"python",
"django",
"orm",
"persistence"
] |
How to store a dictionary on a Django Model? | 402,217 | <p>I need to store some data in a Django model. These data are not equal to all instances of the model.</p>
<p>At first I thought about subclassing the model, but Iâm trying to keep the application flexible. If I use subclasses, Iâll need to create a whole class each time I need a new kind of object, and thatâs ... | 32 | 2008-12-31T03:18:15Z | 39,235,784 | <p>If you are using Postgres, you can use an hstore field: <a href="https://docs.djangoproject.com/en/1.10/ref/contrib/postgres/fields/#hstorefield" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/contrib/postgres/fields/#hstorefield</a>.</p>
| 0 | 2016-08-30T19:36:59Z | [
"python",
"django",
"orm",
"persistence"
] |
How do you uninstall a python package that was installed using distutils? | 402,359 | <p>Can you simply delete the directory from your python installation, or are there any lingering files that you must delete?</p>
| 72 | 2008-12-31T05:26:14Z | 402,444 | <p>It varies based on the options that you pass to <code>install</code> and the contents of the <a href="http://docs.python.org/install/index.html#inst-config-files">distutils configuration files</a> on the system/in the package. I don't believe that any files are modified outside of directories specified in these ways... | 34 | 2008-12-31T07:00:04Z | [
"python"
] |
How do you uninstall a python package that was installed using distutils? | 402,359 | <p>Can you simply delete the directory from your python installation, or are there any lingering files that you must delete?</p>
| 72 | 2008-12-31T05:26:14Z | 403,540 | <p>If this is for testing and/or development purposes, <a href="http://peak.telecommunity.com/DevCenter/setuptools" rel="nofollow">setuptools</a> has a <a href="http://peak.telecommunity.com/DevCenter/setuptools#develop-deploy-the-project-source-in-development-mode" rel="nofollow">develop</a> command that updates every... | 4 | 2008-12-31T17:24:59Z | [
"python"
] |
How do you uninstall a python package that was installed using distutils? | 402,359 | <p>Can you simply delete the directory from your python installation, or are there any lingering files that you must delete?</p>
| 72 | 2008-12-31T05:26:14Z | 403,563 | <p>The three things that get installed that you will need to delete are:</p>
<ol>
<li>Packages/modules</li>
<li>Scripts</li>
<li>Data files</li>
</ol>
<p>Now on my linux system these live in:</p>
<ol>
<li>/usr/lib/python2.5/site-packages</li>
<li>/usr/bin</li>
<li>/usr/share</li>
</ol>
<p>But on a windows system th... | 12 | 2008-12-31T17:31:17Z | [
"python"
] |
How do you uninstall a python package that was installed using distutils? | 402,359 | <p>Can you simply delete the directory from your python installation, or are there any lingering files that you must delete?</p>
| 72 | 2008-12-31T05:26:14Z | 405,406 | <p>Yes, it is safe to simply delete anything that distutils installed. That goes for installed folders or .egg files. Naturally anything that depends on that code will no longer work. </p>
<p>If you want to make it work again, simply re-install. </p>
<p>By the way, if you are using distutils also consider using the m... | 7 | 2009-01-01T20:06:35Z | [
"python"
] |
How do you uninstall a python package that was installed using distutils? | 402,359 | <p>Can you simply delete the directory from your python installation, or are there any lingering files that you must delete?</p>
| 72 | 2008-12-31T05:26:14Z | 928,621 | <p>I just uninstalled a python package, and even though I'm not <em>certain</em> I did so perfectly, I'm reasonably confident. </p>
<p>I started by getting a list of <em>all</em> python-related files, ordered by date, on the assumption that all of the files in my package will have more or less the same timestamp, and... | 2 | 2009-05-30T00:12:08Z | [
"python"
] |
How do you uninstall a python package that was installed using distutils? | 402,359 | <p>Can you simply delete the directory from your python installation, or are there any lingering files that you must delete?</p>
| 72 | 2008-12-31T05:26:14Z | 4,524,074 | <p>For Windows 7,</p>
<p><strong>Control Panel --> Programs --> Uninstall</strong></p>
<p>, then </p>
<p>choose the python package to remove.</p>
| 2 | 2010-12-24T03:50:18Z | [
"python"
] |
How do you uninstall a python package that was installed using distutils? | 402,359 | <p>Can you simply delete the directory from your python installation, or are there any lingering files that you must delete?</p>
| 72 | 2008-12-31T05:26:14Z | 8,471,143 | <p>Another time stamp based hack:</p>
<ol>
<li>Create an anchor: <code>touch /tmp/ts</code></li>
<li>Reinstall the package to be removed: <code>python setup.py install --prefix=<PREFIX></code></li>
<li>Remove files what are more recent than the anchor file: <code>find <PREFIX> -cnewer /tmp/ts | xargs rm -r... | 11 | 2011-12-12T07:27:00Z | [
"python"
] |
How do you uninstall a python package that was installed using distutils? | 402,359 | <p>Can you simply delete the directory from your python installation, or are there any lingering files that you must delete?</p>
| 72 | 2008-12-31T05:26:14Z | 11,749,229 | <p>In ubuntu 12.04, I have found that the only place you need to look by default is under</p>
<pre><code>/usr/local/lib/python2.7/
</code></pre>
<p>And simply remove the associated folder and file, if there is one!</p>
| 4 | 2012-07-31T21:24:50Z | [
"python"
] |
Help--Function Pointers in Python | 402,364 | <p>My idea of program:</p>
<p>I have a dictionary:</p>
<pre><code>options = { 'string' : select_fun(function pointer),
'float' : select_fun(function pointer),
'double' : select_fun(function pointer)
}
</code></pre>
<p>whatever type comes single function <code>select_fun(function pointer)</code> gets called.
Inside <... | 3 | 2008-12-31T05:34:38Z | 402,369 | <p>Could you be more specific on what you're trying to do? You don't have to do anything special to get function pointers in Python -- you can pass around functions like regular objects:</p>
<pre><code>def plus_1(x):
return x + 1
def minus_1(x):
return x - 1
func_map = {'+' : plus_1, '-' : minus_1}
func_ma... | 20 | 2008-12-31T05:41:07Z | [
"python",
"function",
"pointers"
] |
Help--Function Pointers in Python | 402,364 | <p>My idea of program:</p>
<p>I have a dictionary:</p>
<pre><code>options = { 'string' : select_fun(function pointer),
'float' : select_fun(function pointer),
'double' : select_fun(function pointer)
}
</code></pre>
<p>whatever type comes single function <code>select_fun(function pointer)</code> gets called.
Inside <... | 3 | 2008-12-31T05:34:38Z | 402,382 | <p>You can use the <a href="http://diveintopython.net/power_of_introspection/built_in_functions.html#d0e8510" rel="nofollow"><code>type()</code> built-in function</a> to detect the type of the function.</p>
<p>Say, if you want to check if a certain name hold a string data, you could do this:</p>
<pre><code>if type(th... | 6 | 2008-12-31T05:51:29Z | [
"python",
"function",
"pointers"
] |
Help--Function Pointers in Python | 402,364 | <p>My idea of program:</p>
<p>I have a dictionary:</p>
<pre><code>options = { 'string' : select_fun(function pointer),
'float' : select_fun(function pointer),
'double' : select_fun(function pointer)
}
</code></pre>
<p>whatever type comes single function <code>select_fun(function pointer)</code> gets called.
Inside <... | 3 | 2008-12-31T05:34:38Z | 402,393 | <p>Maybe you want to call the same <code>select_fun()</code> every time, with a different argument. If that is what you mean, you need a different dictionary:</p>
<pre><code>>>> options = {'string' : str, 'float' : float, 'double' : float }
>>> options
{'double': <type 'float'>, 'float': <ty... | 3 | 2008-12-31T06:03:51Z | [
"python",
"function",
"pointers"
] |
Help--Function Pointers in Python | 402,364 | <p>My idea of program:</p>
<p>I have a dictionary:</p>
<pre><code>options = { 'string' : select_fun(function pointer),
'float' : select_fun(function pointer),
'double' : select_fun(function pointer)
}
</code></pre>
<p>whatever type comes single function <code>select_fun(function pointer)</code> gets called.
Inside <... | 3 | 2008-12-31T05:34:38Z | 402,397 | <p>Functions are the first-class objects in Python therefore you can pass them as arguments to other functions as you would with any other object such as string or an integer.</p>
<p>There is no single-precision floating point type in Python. Python's <code>float</code> corresponds to C's <code>double</code>.</p>
<pr... | 4 | 2008-12-31T06:08:04Z | [
"python",
"function",
"pointers"
] |
Help--Function Pointers in Python | 402,364 | <p>My idea of program:</p>
<p>I have a dictionary:</p>
<pre><code>options = { 'string' : select_fun(function pointer),
'float' : select_fun(function pointer),
'double' : select_fun(function pointer)
}
</code></pre>
<p>whatever type comes single function <code>select_fun(function pointer)</code> gets called.
Inside <... | 3 | 2008-12-31T05:34:38Z | 402,489 | <p>Looking at your example, it seems to me some C procedure, directly translated to Python.</p>
<p>For this reason, I think there could be some design issue, because usually, in Python, you do not care about type of an object, but only about the messages you can send to it.</p>
<p>Of course, there are plenty of excep... | 4 | 2008-12-31T07:46:22Z | [
"python",
"function",
"pointers"
] |
Caching result of setUp() using Python unittest | 402,483 | <p>I currently have a unittest.TestCase that looks like..</p>
<pre><code>class test_appletrailer(unittest.TestCase):
def setup(self):
self.all_trailers = Trailers(res = "720", verbose = True)
def test_has_trailers(self):
self.failUnless(len(self.all_trailers) > 1)
# ..more tests..
</co... | 7 | 2008-12-31T07:43:31Z | 402,492 | <p>How about using a class member that only gets initialized once?</p>
<pre><code>class test_appletrailer(unittest.TestCase):
all_trailers = None
def setup(self):
# Only initialize all_trailers once.
if self.all_trailers is None:
self.__class__.all_trailers = Trailers(res = "720",... | 14 | 2008-12-31T07:48:46Z | [
"python",
"unit-testing"
] |
Caching result of setUp() using Python unittest | 402,483 | <p>I currently have a unittest.TestCase that looks like..</p>
<pre><code>class test_appletrailer(unittest.TestCase):
def setup(self):
self.all_trailers = Trailers(res = "720", verbose = True)
def test_has_trailers(self):
self.failUnless(len(self.all_trailers) > 1)
# ..more tests..
</co... | 7 | 2008-12-31T07:43:31Z | 402,495 | <p>What is Trailers class doing?<br>
If it holds some state, then you <strong>have</strong> to reset it each time unit test is being executed.</p>
<p>To solve your problem, I would use a mock object - just emulating the interface of Trailers, providing a faked set of data.</p>
<p><hr /></p>
<p><strong>Update</strong... | 1 | 2008-12-31T07:49:55Z | [
"python",
"unit-testing"
] |
Caching result of setUp() using Python unittest | 402,483 | <p>I currently have a unittest.TestCase that looks like..</p>
<pre><code>class test_appletrailer(unittest.TestCase):
def setup(self):
self.all_trailers = Trailers(res = "720", verbose = True)
def test_has_trailers(self):
self.failUnless(len(self.all_trailers) > 1)
# ..more tests..
</co... | 7 | 2008-12-31T07:43:31Z | 403,541 | <p>An alternative to the proposed solution would be to use a more featured test runner like <a href="http://somethingaboutorange.com/mrl/projects/nose/">Nose</a>. With Nose, you can have module-level setup functions which will be run once for a test module. Since it is entirely compatible with unittest, you wouldn't ha... | 8 | 2008-12-31T17:25:38Z | [
"python",
"unit-testing"
] |
Caching result of setUp() using Python unittest | 402,483 | <p>I currently have a unittest.TestCase that looks like..</p>
<pre><code>class test_appletrailer(unittest.TestCase):
def setup(self):
self.all_trailers = Trailers(res = "720", verbose = True)
def test_has_trailers(self):
self.failUnless(len(self.all_trailers) > 1)
# ..more tests..
</co... | 7 | 2008-12-31T07:43:31Z | 4,578,221 | <p>It's worth mentioning that during 2010 the testrunner used in the Zope/Plone world was released as a separate module, unsurprisingly called <a href="http://pypi.python.org/pypi/zope.testrunner" rel="nofollow">zope.testrunner</a>. It supports caching setUps in the form of "Layers". Check it out.</p>
| 0 | 2011-01-02T11:47:56Z | [
"python",
"unit-testing"
] |
Caching result of setUp() using Python unittest | 402,483 | <p>I currently have a unittest.TestCase that looks like..</p>
<pre><code>class test_appletrailer(unittest.TestCase):
def setup(self):
self.all_trailers = Trailers(res = "720", verbose = True)
def test_has_trailers(self):
self.failUnless(len(self.all_trailers) > 1)
# ..more tests..
</co... | 7 | 2008-12-31T07:43:31Z | 5,539,791 | <p>If you are using Python >= 2.7 you can use the <a href="http://docs.python.org/library/unittest.html#unittest.TestCase.setUpClass" rel="nofollow"><code>setUpClass</code></a> method that is called only once for the whole unit test.</p>
<pre><code>import unittest
from trailers import Trailers
class AppleTrailerTest... | 4 | 2011-04-04T14:16:46Z | [
"python",
"unit-testing"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 402,507 | <p>You may be looking for the <code>type()</code> function.</p>
<p>See the examples below, but there's no "unsigned" type in Python just like Java.</p>
<p>Positive integer:</p>
<pre><code>>>> v = 10
>>> type(v)
<type 'int'>
</code></pre>
<p><em>Large</em> positive integer:</p>
<pre><code>&g... | 172 | 2008-12-31T08:02:04Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 402,508 | <pre><code>print type(variable_name)
</code></pre>
<p>I also highly recommend the <a href="http://ipython.scipy.org/">IPython</a> interactive interpreter when dealing with questions like this. It lets you type <code>variable_name?</code> and will return a whole list of information about the object including the type a... | 9 | 2008-12-31T08:02:11Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 402,511 | <p>The question is somewhat ambiguous -- I'm not sure what you mean by "view". If you are trying to <em>query</em> the type of a native Python object, <a href="http://stackoverflow.com/questions/402504/how-to-determine-the-variable-type-in-python#402516">@atzz</a>'s answer will steer you in the right direction.</p>
<p... | 20 | 2008-12-31T08:08:06Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 402,515 | <p>Do you mean in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> or using <a href="http://www.python.org/doc/2.5/lib/module-ctypes.html">ctypes</a>?</p>
<p>In the first case, you simply cannot - because Python does not have signed/unsigned, 16/32 bit integers.</p>
<p>In the second... | 12 | 2008-12-31T08:10:54Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 402,516 | <p>Python doesn't have such types as you describe. There are two types used to represent integral values: <code>int</code>, which corresponds to platform's int type in C, and <code>long</code>, which is an arbitrary precision integer (i.e. it grows as needed and doesn't have an upper limit). <code>int</code>s are silen... | 10 | 2008-12-31T08:12:34Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 402,704 | <p>Python doesn't have the same types as C/C++, which appears to be your question.</p>
<p>Try this:</p>
<pre><code>>>> i = 123
>>> type(i)
<type 'int'>
>>> type(i) is int
True
>>> i = 123456789L
>>> type(i)
<type 'long'>
>>> type(i) is long
True
>&g... | 582 | 2008-12-31T10:43:07Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 403,093 | <p>It really depends on what level you mean. In Python 2.x, there are two integer types, <code>int</code> (constrained to <code>sys.maxint</code>) and <code>long</code> (unlimited precision), for historical reasons. In Python code, this shouldn't make a bit of difference because the interpreter automatically converts t... | 5 | 2008-12-31T14:36:51Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 18,224,831 | <p>It may be little irrelevant. but you can check types of an object with <code>isinstance(object, type)</code> as mentioned <a href="http://stackoverflow.com/questions/2225038/python-determine-the-type-of-an-object" title="Object type in python">here</a>.</p>
| 9 | 2013-08-14T06:39:32Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 29,113,307 | <p>One more way using <code>__class__</code>:</p>
<pre><code>>>> a = [1, 2, 3, 4]
>>> a.__class__
<type 'list'>
>>> b = {'key1': 'val1'}
>>> b.__class__
<type 'dict'>
>>> c = 12
>>> c.__class__
<type 'int'>
</code></pre>
| 26 | 2015-03-18T02:48:04Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 32,885,953 | <p>It is so simple. You do it like this.</p>
<pre><code>print type(variable_name)
</code></pre>
| 55 | 2015-10-01T11:02:32Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to determine the variable type in Python? | 402,504 | <p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
<p>How do I view it?</p>
| 607 | 2008-12-31T07:58:57Z | 39,921,486 | <blockquote>
<h1>How to determine the variable type in Python?</h1>
<p>How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?</p>
</blockquote>
<p>So if you have a variable, for example:</p>
<pre><code>one = 1
</code></pre>
<p>You want to know its type?</p>
<p>There are right... | -1 | 2016-10-07T16:02:24Z | [
"python",
"types",
"unsigned",
"signed",
"16-bit"
] |
How to sort a list of objects in Python, based on an attribute of the objects? | 403,421 | <p>I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like:</p>
<pre><code>>>> ut
[<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>, <Tag: aes>, <Tag: ajax> ...]
</code></pre>
... | 310 | 2008-12-31T16:41:32Z | 403,426 | <pre><code># To sort the list in place...
ut.sort(key=lambda x: x.count, reverse=True)
# To return a new list, use the sorted() built-in function...
newlist = sorted(ut, key=lambda x: x.count, reverse=True)
</code></pre>
<p>More on <a href="http://wiki.python.org/moin/HowTo/Sorting#Sortingbykeys">sorting by keys &raq... | 531 | 2008-12-31T16:42:59Z | [
"python",
"sorting",
"count"
] |
How to sort a list of objects in Python, based on an attribute of the objects? | 403,421 | <p>I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like:</p>
<pre><code>>>> ut
[<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>, <Tag: aes>, <Tag: ajax> ...]
</code></pre>
... | 310 | 2008-12-31T16:41:32Z | 403,437 | <p>Add rich comparison operators to the object class, then use sort() method of the list.<br>
See <a href="http://docs.python.org/reference/datamodel.html#object.__lt__">rich comparison in python</a>.</p>
<p><hr /></p>
<p><strong>Update</strong>: Although this method would work, I think solution from Triptych is bett... | 5 | 2008-12-31T16:45:15Z | [
"python",
"sorting",
"count"
] |
How to sort a list of objects in Python, based on an attribute of the objects? | 403,421 | <p>I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like:</p>
<pre><code>>>> ut
[<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>, <Tag: aes>, <Tag: ajax> ...]
</code></pre>
... | 310 | 2008-12-31T16:41:32Z | 403,496 | <p>It looks much like a list of Django ORM model instances.</p>
<p>Why not sort them on query like this:</p>
<pre><code>ut = Tag.objects.order_by('-count')
</code></pre>
| 9 | 2008-12-31T17:10:00Z | [
"python",
"sorting",
"count"
] |
How to sort a list of objects in Python, based on an attribute of the objects? | 403,421 | <p>I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like:</p>
<pre><code>>>> ut
[<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>, <Tag: aes>, <Tag: ajax> ...]
</code></pre>
... | 310 | 2008-12-31T16:41:32Z | 403,607 | <p>A way that can be fastest, especially if your list has a lot of records, is to use <code>operator.attrgetter("count")</code>. However, this might run on an pre-operator version of Python, so it would be nice to have a fallback mechanism. You might want to do the following, then:</p>
<pre><code>try: import operator
... | 38 | 2008-12-31T17:48:21Z | [
"python",
"sorting",
"count"
] |
How to sort a list of objects in Python, based on an attribute of the objects? | 403,421 | <p>I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like:</p>
<pre><code>>>> ut
[<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>, <Tag: aes>, <Tag: ajax> ...]
</code></pre>
... | 310 | 2008-12-31T16:41:32Z | 403,795 | <pre><code>from operator import attrgetter
ut.sort(key = attrgetter('count'), reverse = True)
</code></pre>
| 10 | 2008-12-31T19:00:33Z | [
"python",
"sorting",
"count"
] |
How to sort a list of objects in Python, based on an attribute of the objects? | 403,421 | <p>I've got a list of Python objects that I'd like to sort by an attribute of the objects themselves. The list looks like:</p>
<pre><code>>>> ut
[<Tag: 128>, <Tag: 2008>, <Tag: <>, <Tag: actionscript>, <Tag: addresses>, <Tag: aes>, <Tag: ajax> ...]
</code></pre>
... | 310 | 2008-12-31T16:41:32Z | 988,728 | <p>Readers should notice that the key= method:</p>
<pre><code>ut.sort(key=lambda x: x.count, reverse=True)
</code></pre>
<p>is many times faster than adding rich comparison operators to the objects. I was surprised to read this (page 485 of "Python in a Nutshell"). You can confirm this by running tests on this little... | 26 | 2009-06-12T19:54:58Z | [
"python",
"sorting",
"count"
] |
Making the value of a table equal to another value in a different table | 403,433 | <p>I have a small problem with a program that im writing. I have a table - stocks which contains information(products, barcodes etc.) about items stored in a fridge. I then have another table - shop which acts like a shop,containing loads of products and their barcodes.some of the products in the shop table are in the ... | -1 | 2008-12-31T16:44:27Z | 403,491 | <p>Try to make the question shorter. That will generate more responses, I guess.</p>
| 0 | 2008-12-31T17:08:00Z | [
"python",
"mysql"
] |
Making the value of a table equal to another value in a different table | 403,433 | <p>I have a small problem with a program that im writing. I have a table - stocks which contains information(products, barcodes etc.) about items stored in a fridge. I then have another table - shop which acts like a shop,containing loads of products and their barcodes.some of the products in the shop table are in the ... | -1 | 2008-12-31T16:44:27Z | 403,493 | <p>So, if I got that right you have the following tables:</p>
<p><strong>Stock</strong> with at least a <em>barcode</em>, <em>amount</em> and <em>quantity</em> column</p>
<p><strong>Shop</strong> with at least a <em>barcode</em> and a <em>stock</em> column</p>
<p>I don't understand why you need that <em>stock</em> c... | 1 | 2008-12-31T17:08:34Z | [
"python",
"mysql"
] |
Making a SQL Query in two tables | 403,527 | <p>I'm wondering, is it possible to make an sql query that does the same function as
<code>'select products where barcode in table1 = barcode in table2</code>'. I am writing this function in a python program. Once that function is called will the table be joined permanently or just while that function is running?
than... | 0 | 2008-12-31T17:20:11Z | 403,530 | <p>Something like:</p>
<pre><code>SELECT * FROM table1 WHERE barcode IN (SELECT barcode FROM table2)
</code></pre>
<p>Is that what you're looking for?</p>
| 1 | 2008-12-31T17:21:53Z | [
"python",
"sql"
] |
Making a SQL Query in two tables | 403,527 | <p>I'm wondering, is it possible to make an sql query that does the same function as
<code>'select products where barcode in table1 = barcode in table2</code>'. I am writing this function in a python program. Once that function is called will the table be joined permanently or just while that function is running?
than... | 0 | 2008-12-31T17:20:11Z | 403,533 | <p>I think you want to join two tables:</p>
<p><a href="http://www.w3schools.com/Sql/sql_join.asp" rel="nofollow">http://www.w3schools.com/Sql/sql_join.asp</a></p>
| 7 | 2008-12-31T17:22:26Z | [
"python",
"sql"
] |
Making a SQL Query in two tables | 403,527 | <p>I'm wondering, is it possible to make an sql query that does the same function as
<code>'select products where barcode in table1 = barcode in table2</code>'. I am writing this function in a python program. Once that function is called will the table be joined permanently or just while that function is running?
than... | 0 | 2008-12-31T17:20:11Z | 403,535 | <pre><code>SELECT t1.products
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t2.barcode = t1.barcode
</code></pre>
| 10 | 2008-12-31T17:22:52Z | [
"python",
"sql"
] |
Making a SQL Query in two tables | 403,527 | <p>I'm wondering, is it possible to make an sql query that does the same function as
<code>'select products where barcode in table1 = barcode in table2</code>'. I am writing this function in a python program. Once that function is called will the table be joined permanently or just while that function is running?
than... | 0 | 2008-12-31T17:20:11Z | 403,628 | <pre><code>SELECT table1.*, table2.* FROM table1 left join table2 on table1.barcode = table2.barcode
</code></pre>
| 0 | 2008-12-31T17:57:07Z | [
"python",
"sql"
] |
Making a SQL Query in two tables | 403,527 | <p>I'm wondering, is it possible to make an sql query that does the same function as
<code>'select products where barcode in table1 = barcode in table2</code>'. I am writing this function in a python program. Once that function is called will the table be joined permanently or just while that function is running?
than... | 0 | 2008-12-31T17:20:11Z | 403,666 | <p>For those who need to understand joins visually
<a href="http://www.codinghorror.com/blog/archives/000976.html" rel="nofollow">http://www.codinghorror.com/blog/archives/000976.html</a></p>
| 0 | 2008-12-31T18:12:08Z | [
"python",
"sql"
] |
Making a SQL Query in two tables | 403,527 | <p>I'm wondering, is it possible to make an sql query that does the same function as
<code>'select products where barcode in table1 = barcode in table2</code>'. I am writing this function in a python program. Once that function is called will the table be joined permanently or just while that function is running?
than... | 0 | 2008-12-31T17:20:11Z | 403,848 | <p>Here is an example of inner joining two tables based on a common field in both tables.</p>
<p>SELECT table1.Products
FROM table1
INNER JOIN table2 on table1.barcode = table2.barcode
WHERE table1.Products is not null</p>
| 0 | 2008-12-31T19:26:14Z | [
"python",
"sql"
] |
Making a SQL Query in two tables | 403,527 | <p>I'm wondering, is it possible to make an sql query that does the same function as
<code>'select products where barcode in table1 = barcode in table2</code>'. I am writing this function in a python program. Once that function is called will the table be joined permanently or just while that function is running?
than... | 0 | 2008-12-31T17:20:11Z | 403,904 | <p>Here's a way to talk yourself through table design in these cases, based on Object Role Modeling. (Yes, I realize this is only indirectly related to the question.)</p>
<p>You have products and barcodes. Products are uniquely identified by Product Code (e.g. 'A2111'; barcodes are uniquely identified by Value (e.g. 1... | 0 | 2008-12-31T19:48:49Z | [
"python",
"sql"
] |
Optimization in Python - do's, don'ts and rules of thumb | 403,794 | <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/">post</a> and then I came across a code which was:</p>
<pre><code>jokes=range(1000000)
domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
</code></pre>
<p>I thought wouldn't it be b... | 8 | 2008-12-31T18:57:59Z | 403,801 | <p>On a tangentially related note, chaining together generators is much more efficient than chaining together list comprehensions, and often more intuitive.</p>
<p>As for the developer's rules of thumb for optimization in Python, they're the same as they are in all languages.</p>
<ol>
<li>Don't optimize.</li>
<li>(ad... | 1 | 2008-12-31T19:03:40Z | [
"python",
"optimization"
] |
Optimization in Python - do's, don'ts and rules of thumb | 403,794 | <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/">post</a> and then I came across a code which was:</p>
<pre><code>jokes=range(1000000)
domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
</code></pre>
<p>I thought wouldn't it be b... | 8 | 2008-12-31T18:57:59Z | 403,807 | <p>len for lists is O(1). It doesn't need to scan the whole list to find the length, because the size of the list is stored for lookup. But apparently, it is still slightly faster to extract it into a local variable. </p>
<p>To answer your question though, I would never care about performance variations on the orde... | 1 | 2008-12-31T19:05:23Z | [
"python",
"optimization"
] |
Optimization in Python - do's, don'ts and rules of thumb | 403,794 | <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/">post</a> and then I came across a code which was:</p>
<pre><code>jokes=range(1000000)
domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
</code></pre>
<p>I thought wouldn't it be b... | 8 | 2008-12-31T18:57:59Z | 403,808 | <p>Read this: <a href="http://wiki.python.org/moin/PythonSpeed/PerformanceTips" rel="nofollow">Python Speed / Performance Tips</a></p>
<p>Also, in your example, the total time is so short that the margin of error will outweigh any actual difference in speed.</p>
| 4 | 2008-12-31T19:05:40Z | [
"python",
"optimization"
] |
Optimization in Python - do's, don'ts and rules of thumb | 403,794 | <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/">post</a> and then I came across a code which was:</p>
<pre><code>jokes=range(1000000)
domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
</code></pre>
<p>I thought wouldn't it be b... | 8 | 2008-12-31T18:57:59Z | 403,821 | <p>You're not using <a href="http://docs.python.org/library/timeit.html"><code>timeit</code></a> correctly: the argument to <code>-s</code> (setup) is a statement to be executed once initially, so you're really just testing an empty statement. You want to do</p>
<pre><code>$ python -m timeit -s "jokes=range(1000000)" ... | 12 | 2008-12-31T19:13:19Z | [
"python",
"optimization"
] |
Optimization in Python - do's, don'ts and rules of thumb | 403,794 | <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/">post</a> and then I came across a code which was:</p>
<pre><code>jokes=range(1000000)
domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
</code></pre>
<p>I thought wouldn't it be b... | 8 | 2008-12-31T18:57:59Z | 403,828 | <p>This applies to all programming, not just Python:</p>
<ol>
<li>Profile</li>
<li>Identify bottlenecks</li>
<li>Optimize</li>
</ol>
<p>And I would even add not to bother doing any of that unless you have a slowness issue that is causing you pain.</p>
<p>And perhaps most important is that Unit tests will help you du... | 2 | 2008-12-31T19:18:08Z | [
"python",
"optimization"
] |
Optimization in Python - do's, don'ts and rules of thumb | 403,794 | <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/">post</a> and then I came across a code which was:</p>
<pre><code>jokes=range(1000000)
domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
</code></pre>
<p>I thought wouldn't it be b... | 8 | 2008-12-31T18:57:59Z | 403,840 | <p>The most important thing to do is to write idiomatic, clear, and beautiful Python code. Many common tasks are already found in the stdlib, so you don't have to rewrite a slower version. (I'm thinking of string methods and itertools specifically here.) Make liberal use of Python's builtin containers, too. dict for ex... | 1 | 2008-12-31T19:22:57Z | [
"python",
"optimization"
] |
Optimization in Python - do's, don'ts and rules of thumb | 403,794 | <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/">post</a> and then I came across a code which was:</p>
<pre><code>jokes=range(1000000)
domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
</code></pre>
<p>I thought wouldn't it be b... | 8 | 2008-12-31T18:57:59Z | 404,012 | <blockquote>
<p>Can we have some experiences from
people who faced the 'slowness', what
was the problem and how it was
corrected?</p>
</blockquote>
<p>The problem was slow data retrieval in a GUI app. I gained a 50x speedup by adding an index to the table, and was widely hailed as a hero and savior.</p>
| 0 | 2008-12-31T20:52:32Z | [
"python",
"optimization"
] |
Optimization in Python - do's, don'ts and rules of thumb | 403,794 | <p>Well I was reading this <a href="http://handyfloss.wordpress.com/2008/02/17/summary-of-my-python-optimization-adventures/">post</a> and then I came across a code which was:</p>
<pre><code>jokes=range(1000000)
domain=[(0,(len(jokes)*2)-i-1) for i in range(0,len(jokes)*2)]
</code></pre>
<p>I thought wouldn't it be b... | 8 | 2008-12-31T18:57:59Z | 2,792,015 | <p>I have a program that parses log files and generates a data warehouse. A typical run involves around 200M log file lines, and runs for the better part of a day. Well worth optimizing!</p>
<p>Since it's a parser, and parsing some rather variable and idiosyncratic and untrustworthy text at that, there are around 100 ... | 1 | 2010-05-07T22:18:16Z | [
"python",
"optimization"
] |
Calling from a parent file in python | 403,822 | <p>I have a file called main.py and a file called classes.py</p>
<p>main.py contains the application and what's happening while class.py contains some classes.</p>
<p>main.py has the following code</p>
<p><strong>main.py</strong></p>
<pre><code>import classes
def addItem(text):
print text
myClass = classes.Ex... | 3 | 2008-12-31T19:14:59Z | 403,832 | <p>I couldn't answer this any better than <a href="http://mail.python.org/pipermail/python-list/2000-December/059926.html" rel="nofollow">this post by Alex Martelli</a>. Basically any way you try to do this will lead to trouble and you are much better off refactoring the code to avoid mutual dependencies between two mo... | 9 | 2008-12-31T19:20:03Z | [
"python"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.