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
Capitalize a string
352,478
<p>Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?</p> <p>For example:</p> <pre><code>asimpletest -&gt; Asimpletest aSimpleTest -&gt; ASimpleTest </code></pre> <p>I would like to be able to do all string lengths as...
25
2008-12-09T11:42:47Z
352,513
<p>@<a href="#352494">saua</a> is right, and </p> <pre><code>str = str[:1].upper() + str[1:] </code></pre> <p>will work for any string</p>
49
2008-12-09T11:52:33Z
[ "python", "string" ]
Capitalize a string
352,478
<p>Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?</p> <p>For example:</p> <pre><code>asimpletest -&gt; Asimpletest aSimpleTest -&gt; ASimpleTest </code></pre> <p>I would like to be able to do all string lengths as...
25
2008-12-09T11:42:47Z
14,162,785
<p>What about <code>your_string.title()</code>?</p> <p>e.g. <code>"banana".title() -&gt; Banana</code></p>
9
2013-01-04T18:30:46Z
[ "python", "string" ]
Capitalize a string
352,478
<p>Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?</p> <p>For example:</p> <pre><code>asimpletest -&gt; Asimpletest aSimpleTest -&gt; ASimpleTest </code></pre> <p>I would like to be able to do all string lengths as...
25
2008-12-09T11:42:47Z
15,279,448
<p>this actually gives you a capitalized word, instead of just capitalizing the first letter</p> <p>cApItAlIzE -> Capitalize</p> <pre><code>def capitalize(str): return str[:1].upper() + str[1:].lower()....... </code></pre>
3
2013-03-07T18:57:11Z
[ "python", "string" ]
Capitalize a string
352,478
<p>Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?</p> <p>For example:</p> <pre><code>asimpletest -&gt; Asimpletest aSimpleTest -&gt; ASimpleTest </code></pre> <p>I would like to be able to do all string lengths as...
25
2008-12-09T11:42:47Z
16,212,385
<pre><code>&gt;&gt;&gt; b = "my name" &gt;&gt;&gt; b.capitalize() 'My name' &gt;&gt;&gt; b.title() 'My Name' </code></pre>
63
2013-04-25T10:29:58Z
[ "python", "string" ]
Capitalize a string
352,478
<p>Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?</p> <p>For example:</p> <pre><code>asimpletest -&gt; Asimpletest aSimpleTest -&gt; ASimpleTest </code></pre> <p>I would like to be able to do all string lengths as...
25
2008-12-09T11:42:47Z
32,772,576
<pre><code>str = str[:].upper() </code></pre> <p>this is the easiest way to do it in my opinion</p>
-2
2015-09-24T23:33:33Z
[ "python", "string" ]
Capitalize a string
352,478
<p>Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?</p> <p>For example:</p> <pre><code>asimpletest -&gt; Asimpletest aSimpleTest -&gt; ASimpleTest </code></pre> <p>I would like to be able to do all string lengths as...
25
2008-12-09T11:42:47Z
38,990,813
<p>for capitalize first word; </p> <pre><code>a="asimpletest" print a.capitalize() </code></pre> <p>for make all the string uppercase use the following tip;</p> <pre><code>print a.upper() </code></pre> <p>this is the easy one i think.</p>
0
2016-08-17T07:42:47Z
[ "python", "string" ]
Extending builtin classes in python
352,537
<p>How can I extend a builtin class in python? I would like to add a method to the str class.<br /> I've done some searching but all I'm finding is older posts, I'm hoping someone knows of something newer.</p>
17
2008-12-09T12:01:30Z
352,546
<p>Just subclass the type</p> <pre><code>&gt;&gt;&gt; class X(str): ... def myMethod( self ): ... return int(self) ... &gt;&gt;&gt; s=X("Hi Mom") &gt;&gt;&gt; s.lower() 'hi mom' &gt;&gt;&gt; s.myMethod() Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "&lt;st...
19
2008-12-09T12:08:35Z
[ "python" ]
Extending builtin classes in python
352,537
<p>How can I extend a builtin class in python? I would like to add a method to the str class.<br /> I've done some searching but all I'm finding is older posts, I'm hoping someone knows of something newer.</p>
17
2008-12-09T12:01:30Z
33,033,631
<p>One way could be to use the "class reopening" concept (natively existing in Ruby) that can be implemented in Python using a class decorator. An exemple is given in this page: <a href="http://www.ianbicking.org/blog/2007/08/opening-python-classes.html">http://www.ianbicking.org/blog/2007/08/opening-python-classes.htm...
5
2015-10-09T08:37:13Z
[ "python" ]
Extending builtin classes in python
352,537
<p>How can I extend a builtin class in python? I would like to add a method to the str class.<br /> I've done some searching but all I'm finding is older posts, I'm hoping someone knows of something newer.</p>
17
2008-12-09T12:01:30Z
39,709,092
<p>Assuming that you can not change builtin classes. To simulate a "class reopening" like Ruby in Python3 where <code>__dict__</code> is an mappingproxy object and not dict object :</p> <pre><code>def extend(class_to_extend): def decorator(extending_class): for k,v in extending_class.__dict__.items(): if k...
0
2016-09-26T17:39:31Z
[ "python" ]
Weighted random selection with and without replacement
352,670
<p>Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn't find any good algori...
43
2008-12-09T13:15:00Z
352,735
<p>I'd recommend you start by looking at section 3.4.2 of Donald Knuth's <a href="http://rads.stackoverflow.com/amzn/click/0201896842" rel="nofollow">Seminumerical Algorithms</a>. </p> <p>If your arrays are large, there are more efficient algorithms in chapter 3 of <a href="http://rads.stackoverflow.com/amzn/click/019...
4
2008-12-09T13:43:46Z
[ "python", "algorithm", "random", "random-sample" ]
Weighted random selection with and without replacement
352,670
<p>Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn't find any good algori...
43
2008-12-09T13:15:00Z
353,510
<p>Here's what I came up with for weighted selection without replacement:</p> <pre><code>def WeightedSelectionWithoutReplacement(l, n): """Selects without replacement n random elements from a list of (weight, item) tuples.""" l = sorted((random.random() * x[0], x[1]) for x in l) return l[-n:] </code></pre> <p>T...
4
2008-12-09T17:06:26Z
[ "python", "algorithm", "random", "random-sample" ]
Weighted random selection with and without replacement
352,670
<p>Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn't find any good algori...
43
2008-12-09T13:15:00Z
353,576
<p>One of the fastest ways to make many with replacement samples from an unchanging list is the alias method. The core intuition is that we can create a set of equal-sized bins for the weighted list that can be indexed very efficiently through bit operations, to avoid a binary search. It will turn out that, done corr...
31
2008-12-09T17:27:01Z
[ "python", "algorithm", "random", "random-sample" ]
Weighted random selection with and without replacement
352,670
<p>Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn't find any good algori...
43
2008-12-09T13:15:00Z
9,827,070
<p>The following is a description of random weighted selection of an element of a set (or multiset, if repeats are allowed), both with and without replacement in O(n) space and O(log n) time.</p> <p>It consists of implementing a binary search tree, sorted by the elements to be selected, where each <em>node</em> of ...
3
2012-03-22T17:07:26Z
[ "python", "algorithm", "random", "random-sample" ]
Weighted random selection with and without replacement
352,670
<p>Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn't find any good algori...
43
2008-12-09T13:15:00Z
20,548,895
<p>A simple approach that hasn't been mentioned here is one proposed in <a href="http://www.sciencedirect.com/science/article/pii/S002001900500298X" rel="nofollow">Efraimidis and Spirakis</a>. In python you could select m items from n >= m weighted items with strictly positive weights stored in weights, returning the s...
4
2013-12-12T16:30:12Z
[ "python", "algorithm", "random", "random-sample" ]
Weighted random selection with and without replacement
352,670
<p>Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn't find any good algori...
43
2008-12-09T13:15:00Z
29,722,230
<p>It is possible to do Weighted Random Selection with replacement in O(1) time, after first creating an additional O(N)-sized data structure in O(N) time. The algorithm is based on the <a href="http://en.wikipedia.org/wiki/Alias_method" rel="nofollow">Alias Method</a> developed by Walker and Vose, which is well desc...
2
2015-04-18T20:07:58Z
[ "python", "algorithm", "random", "random-sample" ]
Weighted random selection with and without replacement
352,670
<p>Recently I needed to do weighted random selection of elements from a list, both with and without replacement. While there are well known and good algorithms for unweighted selection, and some for weighted selection without replacement (such as modifications of the resevoir algorithm), I couldn't find any good algori...
43
2008-12-09T13:15:00Z
40,071,790
<p>Suppose you want to sample 3 elements without replacement from the list ['white','blue','black','yellow','green'] with a prob. distribution [0.1, 0.2, 0.4, 0.1, 0.2]. Using numpy.random module it is as easy as this:</p> <pre><code> import numpy.random as rnd sampling_size = 3 domain = ['white','blue','b...
0
2016-10-16T15:07:49Z
[ "python", "algorithm", "random", "random-sample" ]
How to add file extensions based on file type on Linux/Unix?
352,837
<p>This is a question regarding Unix shell scripting (any shell), but any other "standard" scripting language solution would also be appreciated:</p> <p>I have a directory full of files where the filenames are hash values like this:</p> <pre><code>fd73d0cf8ee68073dce270cf7e770b97 fec8047a9186fdcc98fdbfc0ea6075ee </co...
11
2008-12-09T14:10:18Z
352,846
<p>You can use</p> <pre><code>file -i filename </code></pre> <p>to get a MIME-type. You could potentially lookup the type in a list and then append an extension. You can find a <a href="http://www.iana.org/assignments/media-types/media-types.xhtml" rel="nofollow">list of MIME-types</a> and <a href="http://svn.apach...
12
2008-12-09T14:13:59Z
[ "python", "linux", "bash", "unix", "shell" ]
How to add file extensions based on file type on Linux/Unix?
352,837
<p>This is a question regarding Unix shell scripting (any shell), but any other "standard" scripting language solution would also be appreciated:</p> <p>I have a directory full of files where the filenames are hash values like this:</p> <pre><code>fd73d0cf8ee68073dce270cf7e770b97 fec8047a9186fdcc98fdbfc0ea6075ee </co...
11
2008-12-09T14:10:18Z
352,919
<p>Following csl's response:</p> <blockquote> <p>You can use</p> <pre><code>file -i filename </code></pre> <p>to get a MIME-type. You could potentially lookup the type in a list and then append an extension. You can find list of MIME-types and suggested file extensions on the net.</p> </blockquote> ...
7
2008-12-09T14:33:48Z
[ "python", "linux", "bash", "unix", "shell" ]
How to add file extensions based on file type on Linux/Unix?
352,837
<p>This is a question regarding Unix shell scripting (any shell), but any other "standard" scripting language solution would also be appreciated:</p> <p>I have a directory full of files where the filenames are hash values like this:</p> <pre><code>fd73d0cf8ee68073dce270cf7e770b97 fec8047a9186fdcc98fdbfc0ea6075ee </co...
11
2008-12-09T14:10:18Z
352,973
<p>Here's mimetypes' version:</p> <pre><code>#!/usr/bin/env python """It is a `filename -&gt; filename.ext` filter. `ext` is mime-based. """ import fileinput import mimetypes import os import sys from subprocess import Popen, PIPE if len(sys.argv) &gt; 1 and sys.argv[1] == '--rename': do_rename = True d...
9
2008-12-09T14:47:24Z
[ "python", "linux", "bash", "unix", "shell" ]
How to add file extensions based on file type on Linux/Unix?
352,837
<p>This is a question regarding Unix shell scripting (any shell), but any other "standard" scripting language solution would also be appreciated:</p> <p>I have a directory full of files where the filenames are hash values like this:</p> <pre><code>fd73d0cf8ee68073dce270cf7e770b97 fec8047a9186fdcc98fdbfc0ea6075ee </co...
11
2008-12-09T14:10:18Z
352,985
<p>Of course, it should be added that deciding on a MIME type just based on file(1) output can be very inaccurate/vague (what's "data" ?) or even completely incorrect...</p>
2
2008-12-09T14:51:21Z
[ "python", "linux", "bash", "unix", "shell" ]
How to add file extensions based on file type on Linux/Unix?
352,837
<p>This is a question regarding Unix shell scripting (any shell), but any other "standard" scripting language solution would also be appreciated:</p> <p>I have a directory full of files where the filenames are hash values like this:</p> <pre><code>fd73d0cf8ee68073dce270cf7e770b97 fec8047a9186fdcc98fdbfc0ea6075ee </co...
11
2008-12-09T14:10:18Z
2,352,986
<p>Agreeing with Keltia, and elaborating some on his answer:</p> <p>Take care -- some filetypes may be problematic. <a href="http://www.linuxquestions.org/questions/showthread.php?p=3843206#post3843206" rel="nofollow">JPEG2000</a>, for example.<br> And others might return too much info given the "file" command without...
0
2010-02-28T22:40:36Z
[ "python", "linux", "bash", "unix", "shell" ]
Making Python default to another version installed on a shared host
353,148
<p>I am on a shared host and can not change the symbolic link to Python2.4, it defaults to 2.3. I tried creating a sym link in the director I would be working on to 2.4, but it seems the the 'global' python interpreter under /usr/bin/python take presedence unless I run it as ./python. What alternative ways are there to...
1
2008-12-09T15:35:15Z
353,159
<p>Create a symlink and prepend the path to your PATH variable:</p> <pre><code>ln -s /usr/bin/python2.4 $HOME/bin/python export PATH="$HOME/bin:$PATH" </code></pre>
2
2008-12-09T15:37:31Z
[ "python", "linux" ]
Making Python default to another version installed on a shared host
353,148
<p>I am on a shared host and can not change the symbolic link to Python2.4, it defaults to 2.3. I tried creating a sym link in the director I would be working on to 2.4, but it seems the the 'global' python interpreter under /usr/bin/python take presedence unless I run it as ./python. What alternative ways are there to...
1
2008-12-09T15:35:15Z
353,200
<p>If you're working from the shell, you can create a symbolic link as suggested and update your path in the .profile. This is described in a previous post.</p> <p>In case these are CGI/whatever scripts that you only run on your shared host, you can alter the shebang line at the top of your scripts that tell the syste...
3
2008-12-09T15:47:16Z
[ "python", "linux" ]
How to work with very long strings in Python?
353,152
<p>I'm tackling project euler's <a href="http://projecteuler.net/index.php?section=problems&amp;id=220" rel="nofollow">problem 220</a> (looked easy, in comparison to some of the others - thought I'd try a higher numbered one for a change!)</p> <p>So far I have:</p> <pre><code>D = "Fa" def iterate(D,num): for i ...
5
2008-12-09T15:35:58Z
353,220
<p>If you think about how many "a" and "b" characters there are in D(0), D(1), etc, you'll see that the string gets very long very quickly. Calculate how many characters there are in D(50), and then maybe think again about where you would store that much data. I make it 4.5*10^15 characters, which is 4500 TB at one byt...
2
2008-12-09T15:51:50Z
[ "python" ]
How to work with very long strings in Python?
353,152
<p>I'm tackling project euler's <a href="http://projecteuler.net/index.php?section=problems&amp;id=220" rel="nofollow">problem 220</a> (looked easy, in comparison to some of the others - thought I'd try a higher numbered one for a change!)</p> <p>So far I have:</p> <pre><code>D = "Fa" def iterate(D,num): for i ...
5
2008-12-09T15:35:58Z
353,242
<p>Since you can't materialize the string, you must generate it. If you yield the individual characters instead of returning the whole string, you might get it to work.</p> <pre><code>def repl220( string ): for c in string: if c == 'a': yield "aRbFR" elif c == 'b': yield "LFaLb" else yield...
1
2008-12-09T15:56:53Z
[ "python" ]
How to work with very long strings in Python?
353,152
<p>I'm tackling project euler's <a href="http://projecteuler.net/index.php?section=problems&amp;id=220" rel="nofollow">problem 220</a> (looked easy, in comparison to some of the others - thought I'd try a higher numbered one for a change!)</p> <p>So far I have:</p> <pre><code>D = "Fa" def iterate(D,num): for i ...
5
2008-12-09T15:35:58Z
353,243
<p>The trick is in noticing which patterns emerge as you run the string through each iteration. Try evaluating <code>iterate(D,n)</code> for n between 1 and 10 and see if you can spot them. Also feed the string through a function that calculates the end position and the number of steps, and look for patterns there too....
3
2008-12-09T15:57:07Z
[ "python" ]
How to work with very long strings in Python?
353,152
<p>I'm tackling project euler's <a href="http://projecteuler.net/index.php?section=problems&amp;id=220" rel="nofollow">problem 220</a> (looked easy, in comparison to some of the others - thought I'd try a higher numbered one for a change!)</p> <p>So far I have:</p> <pre><code>D = "Fa" def iterate(D,num): for i ...
5
2008-12-09T15:35:58Z
353,419
<p>You could treat D as a byte stream file.</p> <p>Something like:-</p> seedfile = open('D1.txt', 'w'); seedfile.write("Fa"); seedfile.close(); n = 0 while (n <p>warning totally untested</p>
0
2008-12-09T16:40:18Z
[ "python" ]
How to work with very long strings in Python?
353,152
<p>I'm tackling project euler's <a href="http://projecteuler.net/index.php?section=problems&amp;id=220" rel="nofollow">problem 220</a> (looked easy, in comparison to some of the others - thought I'd try a higher numbered one for a change!)</p> <p>So far I have:</p> <pre><code>D = "Fa" def iterate(D,num): for i ...
5
2008-12-09T15:35:58Z
353,608
<p>Python strings are not going to be the answer to this one. Strings are stored as immutable arrays, so each one of those replacements creates an entirely new string in memory. Not to mention, the set of instructions after 10^12 steps will be at least 1TB in size if you store them as characters (and that's with some m...
2
2008-12-09T17:42:22Z
[ "python" ]
How to work with very long strings in Python?
353,152
<p>I'm tackling project euler's <a href="http://projecteuler.net/index.php?section=problems&amp;id=220" rel="nofollow">problem 220</a> (looked easy, in comparison to some of the others - thought I'd try a higher numbered one for a change!)</p> <p>So far I have:</p> <pre><code>D = "Fa" def iterate(D,num): for i ...
5
2008-12-09T15:35:58Z
8,056,397
<p>Just as a word of warning be careful when using the replace() function. If your strings are very large (in my case ~ 5e6 chars) the replace function would return a subset of the string (around ~ 4e6 chars) without throwing any errors.</p>
1
2011-11-08T20:02:58Z
[ "python" ]
How to blend drawn circles with pygame
353,297
<p>I am trying to create an application like the one here:</p> <p><a href="http://www.eigenfaces.com/" rel="nofollow">http://www.eigenfaces.com/</a></p> <p>Basically lots of overlapping circles drawn with pygame. I cannot figure out how the blend the circles to make them translucent. That is to have overlapping color...
3
2008-12-09T16:09:12Z
353,719
<p>I got it to work by drawing to a surface that is not the display and combining the set colorkey and set alpha functions.</p> <pre><code>import pygame from pygame.locals import * TRANSPARENT = (255,0,255) pygame.init() screen = pygame.display.set_mode((500,500)) surf1 = pygame.Surface((200,200)) surf1.fill(TRANSPA...
6
2008-12-09T18:17:24Z
[ "python", "pygame" ]
How to blend drawn circles with pygame
353,297
<p>I am trying to create an application like the one here:</p> <p><a href="http://www.eigenfaces.com/" rel="nofollow">http://www.eigenfaces.com/</a></p> <p>Basically lots of overlapping circles drawn with pygame. I cannot figure out how the blend the circles to make them translucent. That is to have overlapping color...
3
2008-12-09T16:09:12Z
354,494
<p>I am Dave. Creator of the images at eigenfaces.com. Good luck with your experiments. I posted the code here:</p> <p><a href="http://www.eigenfaces.com/" rel="nofollow">http://www.eigenfaces.com/</a></p> <p>Let me know if it's of use.</p> <p>By the way.. I have also experimented with movies... Here is about 20 ...
3
2008-12-09T22:32:49Z
[ "python", "pygame" ]
Cleaner way to query on a dynamic number of columns in Django?
353,489
<p>In my case, I have a number of column names coming from a form. I want to filter to make sure they're all true. Here's how I currently do it:</p> <pre><code>for op in self.cleaned_data['options']: cars = cars.filter((op, True)) </code></pre> <p>Now it <em>works</em> but there are are a possible ~40 columns to ...
0
2008-12-09T17:00:26Z
353,602
<p>Build the query as a dictionary and use the ** operator to unpack the options as keyword arguments to the filter method.</p> <pre><code>op_kwargs = {} for op in self.cleaned_data['options']: op_kwargs[op] = True cars = CarModel.objects.filter(**op_kwargs) </code></pre> <p>This is covered in the <a href="http:/...
9
2008-12-09T17:37:46Z
[ "python", "django" ]
Cleaner way to query on a dynamic number of columns in Django?
353,489
<p>In my case, I have a number of column names coming from a form. I want to filter to make sure they're all true. Here's how I currently do it:</p> <pre><code>for op in self.cleaned_data['options']: cars = cars.filter((op, True)) </code></pre> <p>Now it <em>works</em> but there are are a possible ~40 columns to ...
0
2008-12-09T17:00:26Z
354,215
<p>Django's query sets are lazy, so what you're currently doing is actually pretty efficient. The database won't be hit until you try to access one of the fields in the QuerySet... assuming, that is, that you didn't edit out some code, and it is effectively like this:</p> <pre><code>cars = CarModel.objects.all() for ...
3
2008-12-09T20:57:51Z
[ "python", "django" ]
Beginner: Trying to understand how apps interact in Django
353,571
<p>I just got done working through the Django tutorials for the second time, and am understanding things much more clearly now. However, I'm still unclear how apps inside a site interact with one another.</p> <p>For example, lets say I'm writing a blog application (a rather popular activity, apparently). Blog posts an...
23
2008-12-09T17:25:23Z
353,615
<p>Your code seems correct. I would keep the post and the comment in a <strong>blog</strong> app though. I am not saying this is <em>the</em> Django way, but those models are close enough to be in the same app.</p> <h3>How To Divide The Project</h3> <p>I would seperate an app if;</p> <ul> <li>I plan to design it res...
2
2008-12-09T17:45:34Z
[ "python", "django", "django-models", "django-apps" ]
Beginner: Trying to understand how apps interact in Django
353,571
<p>I just got done working through the Django tutorials for the second time, and am understanding things much more clearly now. However, I'm still unclear how apps inside a site interact with one another.</p> <p>For example, lets say I'm writing a blog application (a rather popular activity, apparently). Blog posts an...
23
2008-12-09T17:25:23Z
353,667
<p>Take a look at django's built-in <a href="http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#ref-contrib-contenttypes">contenttypes framework</a>:</p> <p><code>django.contrib.contenttypes</code></p> <p>It allows you develop your applications as stand-alone units. This is what the django developers use...
20
2008-12-09T18:01:25Z
[ "python", "django", "django-models", "django-apps" ]
Beginner: Trying to understand how apps interact in Django
353,571
<p>I just got done working through the Django tutorials for the second time, and am understanding things much more clearly now. However, I'm still unclear how apps inside a site interact with one another.</p> <p>For example, lets say I'm writing a blog application (a rather popular activity, apparently). Blog posts an...
23
2008-12-09T17:25:23Z
353,686
<p>"Is what I wrote above, importing a model from another app and setting it as a foreign key, how Django apps interact?"</p> <p>Yep. Works for me.</p> <p>We have about 10 applications that borrow back and forth among themselves.</p> <p>This leads to a kind of dependency in our unit test script.</p> <p>It looks li...
3
2008-12-09T18:06:38Z
[ "python", "django", "django-models", "django-apps" ]
Beginner: Trying to understand how apps interact in Django
353,571
<p>I just got done working through the Django tutorials for the second time, and am understanding things much more clearly now. However, I'm still unclear how apps inside a site interact with one another.</p> <p>For example, lets say I'm writing a blog application (a rather popular activity, apparently). Blog posts an...
23
2008-12-09T17:25:23Z
353,761
<p>There's nothing wrong (imho) with making some app dependent on another. After all, apps are just operations on a set of models. you just have to always be aware of which app depends on which app (I guess you could call that a dependency map).</p> <p>You can achieve loose coupling with the contenttypes framework. It...
3
2008-12-09T18:35:09Z
[ "python", "django", "django-models", "django-apps" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
354,073
<p>In case you are looking for parsing (positive, unsigned) integers instead of floats, you can use the <a href="https://docs.python.org/2/library/stdtypes.html#str.isdigit"><code>isdigit()</code></a> function for string objects.</p> <pre><code>&gt;&gt;&gt; a = "03523" &gt;&gt;&gt; a.isdigit() True &gt;&gt;&gt; b = "9...
1,069
2008-12-09T20:15:18Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
354,130
<blockquote> <p>Which, not only is ugly and slow</p> </blockquote> <p>I'd dispute both.</p> <p>A regex or other string parsing would be uglier and slower. </p> <p>I'm not sure that anything much could be faster than the above. It calls the function and returns. Try/Catch doesn't introduce much overhead because ...
429
2008-12-09T20:30:48Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
354,134
<p>Casting to float and catching ValueError is probably the fastest way, since float() is specifically meant for just that. Anything else that requires string parsing (regex, etc) will likely be slower due to the fact that it's not tuned for this operation. My $0.02.</p>
9
2008-12-09T20:31:49Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
355,489
<p>If you want to know if the <em>entire</em> string can be represented as a number you'll want to use a regexp (or maybe convert the float back to a string and compare it to the source string, but I'm guessing that's not very fast).</p>
1
2008-12-10T09:15:25Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
358,415
<p>Your code looks fine to me.</p> <p>Perhaps you think the code is "clunky" because of using exceptions? Note that Python programmers tend to use exceptions liberally when it improves code readability, thanks to its low performance penalty.</p>
4
2008-12-11T04:03:03Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
358,479
<blockquote> <p>Which, not only is ugly and slow, seems clunky.</p> </blockquote> <p>It may take some getting used to, but this is the pythonic way of doing it. As has been already pointed out, the alternatives are worse. But there is one other advantage of doing things this way: polymorphism.</p> <p>The central...
33
2008-12-11T04:56:26Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
1,139,186
<p>Here's my simple way of doing it. Let's say that I'm looping through some strings and I want to add them to an array if they turn out to be numbers.</p> <pre><code>try: myvar.append( float(string_to_check) ) except: continue </code></pre> <p>Replace the myvar.apppend with whatever operation you want to do ...
1
2009-07-16T17:45:12Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
3,335,060
<p>Updated after Alfe pointed out you don't need to check for float separately as complex handles both:</p> <pre><code>def is_number(s): try: complex(s) # for int, long, float and complex except ValueError: return False return True </code></pre> <hr> <p>Previously said: Is some rare case...
27
2010-07-26T13:10:15Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
3,618,897
<p>There is one exception that you may want to take into account: the string 'NaN'</p> <p>If you want is_number to return FALSE for 'NaN' this code will not work as Python converts it to its representation of a number that is not a number (talk about identity issues):</p> <pre><code>&gt;&gt;&gt; float('NaN') nan </co...
50
2010-09-01T14:06:08Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
3,912,515
<p>I did some speed test. Lets say that if the string is <strong>likely</strong> to be a number the <em>try/except</em> strategy is the fastest possible.If the string is <strong>not likely</strong> to be a number <strong>and</strong> you are interested in <strong>Integer</strong> check, it worths to do some test (isdig...
4
2010-10-12T07:43:19Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
9,337,733
<h2>Just Mimic C#</h2> <p><strong>In C# there are two different functions that handle parsing of scalar values:</strong></p> <ul> <li>Float.Parse()</li> <li>Float.TryParse()</li> </ul> <p><strong>float.parse():</strong></p> <pre><code>def parse(string): try: return float(string) except Exception: ...
12
2012-02-18T01:35:32Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
9,842,626
<p>So to put it all together, checking for Nan, infinity and complex numbers (it would seem they are specified with j, not i, i.e. 1+2j) it results in:</p> <pre><code>def is_number(s): try: n=str(float(s)) if n == "nan" or n=="inf" or n=="-inf" : return False except ValueError: try: ...
6
2012-03-23T16:10:09Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
10,762,002
<p>how about this:</p> <pre><code>'3.14'.replace('.','',1).isdigit() </code></pre> <p>which will return true only if there is one or no '.' in the string of digits.</p> <pre><code>'3.14.5'.replace('.','',1).isdigit() </code></pre> <p>will return false</p> <p>edit: just saw another comment ... adding a <code>.repla...
33
2012-05-25T22:22:32Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
14,352,314
<p>I wanted to see which method is fastest, and turns out catching an exception is the fastest.</p> <pre><code>import time import re check_regexp = re.compile("^\d*\.?\d*$") check_replace = lambda x: x.replace('.','',1).isdigit() numbers = [str(float(x) / 100) for x in xrange(10000000)] def is_number(s): try: ...
5
2013-01-16T06:09:37Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
15,205,926
<p>You can use Unicode strings, they have a method to do just what you want:</p> <pre><code>&gt;&gt;&gt; s = u"345" &gt;&gt;&gt; s.isnumeric() True </code></pre> <p>Or:</p> <pre><code>&gt;&gt;&gt; s = "345" &gt;&gt;&gt; u = unicode(s) &gt;&gt;&gt; u.isnumeric() True </code></pre> <p><a href="http://www.tutorialspoi...
9
2013-03-04T16:12:38Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
16,743,970
<p>You can generalize the exception technique in a useful way by returning more useful values than True and False. For example this function puts quotes round strings but leaves numbers alone. Which is just what I needed for a quick and dirty filter to make some variable definitions for R. </p> <pre><code>import sys...
0
2013-05-24T21:36:36Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
17,926,244
<p>RyanN suggests</p> <blockquote> <p>If you want to return False for a NaN and Inf, change line to x = float(s); return (x == x) and (x - 1 != x). This should return True for all floats except Inf and NaN</p> </blockquote> <p>But this doesn't quite work, because for sufficiently large floats, <code>x-1 == x</code>...
3
2013-07-29T14:08:23Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
23,639,915
<p>I did some <a href="http://nbviewer.ipython.org/github/rasbt/One-Python-benchmark-per-day/blob/master/ipython_nbs/day6_string_is_number.ipynb?create=1" rel="nofollow">benchmarks</a> comparing the different approaches</p> <pre><code>def is_number_tryexcept(s): """ Returns True is string is a number. """ try:...
3
2014-05-13T19:28:03Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
24,559,671
<p>I needed to determine if a string cast into basic types (float,int,str,bool). After not finding anything on the internet I created this:</p> <pre><code>def str_to_type (s): """ Get possible cast type for a string Parameters ---------- s : string Returns ------- float,int,str,bool : typ...
1
2014-07-03T17:12:27Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
25,299,619
<p>For strings of non-numbers, <code>try: except:</code> is actually slower than regular expressions. For strings of valid numbers, regex is slower. So, the appropriate method depends on your input. </p> <p>If you find that you are in a performance bind, you can use a new third-party module called <a href="https://p...
11
2014-08-14T03:34:08Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
26,336,546
<p>Lets say you have digits in string. str = "100949" and you would like to check if it has only numbers</p> <pre><code>if str.isdigit(): returns TRUE or FALSE </code></pre> <p><a href="http://docs.python.org/2/library/stdtypes.html#str.isdigit">isdigit docs</a></p> <p>otherwise your method works great to find the ...
8
2014-10-13T09:17:53Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
26,829,047
<p>I was working on a problem that led me to this thread, namely how to convert a collection of data to strings and numbers in the most intuitive way. I realized after reading the original code that what I needed was different in two ways:</p> <p>1 - I wanted an integer result if the string represented an integer</p>...
0
2014-11-09T14:06:38Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
30,549,042
<p>Try this.</p> <pre><code> def is_number(var): try: if var == int(var): return True except Exception: return False </code></pre>
1
2015-05-30T17:12:56Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
32,167,108
<p>You may use regex.</p> <pre><code>number = raw_input("Enter a number: ") if re.match(r'^\d+$', number): print "It's integer" print int(number) elif re.match(r'^\d+\.\d+$', number): print "It's float" print float(number) else: print("Please enter a number") </code></pre>
0
2015-08-23T13:22:38Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
32,453,110
<p>For <code>int</code> use this:</p> <pre><code>&gt;&gt;&gt; "1221323".isdigit() True </code></pre> <p>But for <code>float</code> we need some tricks ;-). Every float number has one point...</p> <pre><code>&gt;&gt;&gt; "12.34".isdigit() False &gt;&gt;&gt; "12.34".replace('.','',1).isdigit() True &gt;&gt;&gt; "12.3....
10
2015-09-08T08:42:14Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
34,615,173
<p>I know this is particularly old but I would add an answer I believe covers the information missing from the highest voted answer that could be very valuable to any who find this:</p> <p>For each of the following methods connect them with a count if you need any input to be accepted. (Assuming we are using vocal def...
7
2016-01-05T15:21:27Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
39,523,992
<p>To check if the input value is a <code>float</code>, you can compare the type of the input to a <code>float</code></p> <pre><code>def isFloat(s): realFloat = 0.1 if type(s) == type(realFloat): return True else: return False </code></pre> <p><strong>Returns:</strong></p> <pre><code>Fal...
1
2016-09-16T05:00:12Z
[ "python", "casting", "floating-point", "type-conversion" ]
How do I check if a string is a number (float) in Python?
354,038
<p>What is the best possible way to check if a string can be represented as a number in Python? </p> <p>The function I currently have right now is:</p> <pre><code>def is_number(s): try: float(s) return True except ValueError: return False </code></pre> <p>Which, not only is ugly and s...
872
2008-12-09T20:03:42Z
40,064,255
<p>I also used the function you mentioned, but soon I notice that strings as "Nan", "Inf" and it's variation are considered as number. So I propose you improved version of your function, that will return false on those type of input and will not fail "1e3" variants:</p> <pre><code>def is_float(text): try: ...
0
2016-10-15T21:11:09Z
[ "python", "casting", "floating-point", "type-conversion" ]
How to configure vim to not put comments at the beginning of lines while editing python files
354,097
<p>When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it.</p> <p>For example, when writing this in vim</p> <pre><code>for i in range(10): # </code></pre> <p>the # do...
40
2008-12-09T20:21:40Z
354,271
<p>My Vim configuration doesn't do that. You might try the python.vim script available from this link: <a href="http://www.vim.org/scripts/script.php?script_id=790" rel="nofollow">http://www.vim.org/scripts/script.php?script_id=790</a></p>
1
2008-12-09T21:18:38Z
[ "python", "vim" ]
How to configure vim to not put comments at the beginning of lines while editing python files
354,097
<p>When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it.</p> <p>For example, when writing this in vim</p> <pre><code>for i in range(10): # </code></pre> <p>the # do...
40
2008-12-09T20:21:40Z
354,422
<p>I found an answer here <a href="http://vim.wikia.com/wiki/Restoring_indent_after_typing_hash">http://vim.wikia.com/wiki/Restoring_indent_after_typing_hash</a></p> <p>It seems that the vim smartindent option is the cause of the problem. The referenced page above describes work-a-rounds but after reading the help in ...
26
2008-12-09T22:07:18Z
[ "python", "vim" ]
How to configure vim to not put comments at the beginning of lines while editing python files
354,097
<p>When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it.</p> <p>For example, when writing this in vim</p> <pre><code>for i in range(10): # </code></pre> <p>the # do...
40
2008-12-09T20:21:40Z
385,388
<p>It's caused by the 'smartindent' feature. If you have <code>:set smartindent</code> in your .vimrc you need to remove it.</p>
2
2008-12-22T02:13:59Z
[ "python", "vim" ]
How to configure vim to not put comments at the beginning of lines while editing python files
354,097
<p>When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it.</p> <p>For example, when writing this in vim</p> <pre><code>for i in range(10): # </code></pre> <p>the # do...
40
2008-12-09T20:21:40Z
385,724
<p>I have the following lines in my .vimrc, seems to be installed by default with my Ubuntu 8.10</p> <pre><code>set smartindent inoremap # X^H# set autoindent </code></pre> <p>And I don't observe the problem. Maybe you can try this. (Note that ^H should be entered by Ctrl-V Ctrl-H)</p>
8
2008-12-22T07:27:50Z
[ "python", "vim" ]
How to configure vim to not put comments at the beginning of lines while editing python files
354,097
<p>When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it.</p> <p>For example, when writing this in vim</p> <pre><code>for i in range(10): # </code></pre> <p>the # do...
40
2008-12-09T20:21:40Z
777,385
<p>@PolyThinker Though I see that response a lot to this question, in my opinion it's not a good solution. The editor still thinks it should be indented all the way to left - check this by pushing == on a line that starts with a hash, or pushing = while a block of code with comments in it is highlighted to reindent.</p...
15
2009-04-22T14:07:40Z
[ "python", "vim" ]
How to configure vim to not put comments at the beginning of lines while editing python files
354,097
<p>When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it.</p> <p>For example, when writing this in vim</p> <pre><code>for i in range(10): # </code></pre> <p>the # do...
40
2008-12-09T20:21:40Z
23,683,246
<p>My solution to the unindenting of #:</p> <p>If you use cindent, recognize that it is designed for C and C++ coding. Here, a # means you are creating a #DEFINE or #MACRO(), so the behavior is correct. But for other languages where # is a comment, it is irritating.</p> <p>The following worked for me:</p> <pre><co...
2
2014-05-15T15:57:17Z
[ "python", "vim" ]
How to configure vim to not put comments at the beginning of lines while editing python files
354,097
<p>When I add a # in insert mode on an empty line in Vim while editing python files, vim moves the # to the beginning of the line, but I would like the # to be inserted at the tab level where I entered it.</p> <p>For example, when writing this in vim</p> <pre><code>for i in range(10): # </code></pre> <p>the # do...
40
2008-12-09T20:21:40Z
24,045,070
<p>I removed <code>set smartindent</code> from <code>~/.vimrc</code> but it still didn't disable smartindent. When I opened a .py file and ran <code>:set smartindent?</code> it displayed <code>smartindent</code>.</p> <p>Turns out that further down in the <code>~/.vimrc</code> was this line:</p> <pre><code>autocmd Buf...
0
2014-06-04T18:38:23Z
[ "python", "vim" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,149
<p>I think it really is going to depend on the programmer and what you want to do. It's subjective. Someone who is a Java God will outperform someone who is only good at python. Any study that tries to blanketly state that will be far too broad in what it tries to accomplish.</p>
1
2008-12-09T20:37:31Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,158
<p>General claims about productivity do not necessarily make sense. A good software engineer (or project manager) knows what language to pick based on the nature of the project, the requirements of the customer, and the skills of his team.</p> <p>There is generally no "one language to rule them all", though there are ...
3
2008-12-09T20:41:24Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,219
<p>I think the point about IDEs is a particularly strong one. For example, is there a Python IDE on par with Eclipse, offering the same refactoring capabilities? Conversely, does an IDE with strong refactoring capabilities promote less intelligent up-front design?</p> <p>This just highlights the difficulty (or, rath...
0
2008-12-09T20:58:28Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,230
<p>You can't measure it, but if you're experienced with Java, and then you learn python and try to do stuff with it, you will realize it by yourself. </p> <p>It's one of those things that can't be quantified, and frankly even if there was a statistical study, I wouldn't be convinced by it. You can bring a Java guru an...
4
2008-12-09T21:05:00Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,240
<p>Well, the very same google search points also a link to an <a href="http://www.artima.com/intv/speed.html" rel="nofollow">interview to GvR</a>, where he explain the reasons for the productivity gain. He lists, in particular:</p> <ul> <li>terser language, hence less typing</li> <li>built-in data types</li> <li>duck ...
0
2008-12-09T21:06:46Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,249
<p>All evidence is anecdotal.</p> <p>You can't ever find published studies that show the general superiority of one language over another because there are too many confounds:</p> <ul> <li>Individual programmers differ greatly in ability</li> <li>Some tasks are more amenable to a given language/library than others (w...
16
2008-12-09T21:09:45Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,250
<p>Yes, there's an excellent paper by Lutz Prechelt on this subject:</p> <p><a href="http://page.mi.fu-berlin.de/prechelt/Biblio//jccpprt_computer2000.pdf">An Empirical Comparison of Seven Programming Languages</a></p> <p>Of course, this paper doesn’t “prove” the superiority of any particular language. But it p...
19
2008-12-09T21:09:52Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,290
<p>I think there are cases where one or the other may be more productive, but the question I always ask is how would this language perform on a mixed team involving people who were not good at programming and would cause more problems than they solve, people who think they know everything and like to write tricky code ...
-1
2008-12-09T21:25:56Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
354,361
<p>Yes, and there are also statistical studies that prove that dogs are more productive than cats. Both are equally valid. ;-)</p> <p>by popular demand, here are a couple of "studies" - take them with a block of salt!</p> <ol> <li><a href="http://page.mi.fu-berlin.de/prechelt/Biblio/jccpprt_computer2000.pdf" rel="nof...
28
2008-12-09T21:50:45Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
361,425
<p>It's a poorly-conceived question, and as such can't be meaningfully answered. If you came to me with a project proposal and asked me what language would be the most productive, the <em>last thing in the world</em> I would look at was a "scientific" comparison of how random programmers did implementing an algorithm ...
2
2008-12-11T23:29:19Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
24,998,191
<p>Another issue to consider is the size of the codebase:</p> <p><a href="http://dustyvolumes.com/archives/506" rel="nofollow">http://dustyvolumes.com/archives/506</a></p> <blockquote> <p>More interestingly, this seems to have been one of the first papers to note that productivity rates decline as the size of the...
0
2014-07-28T15:03:36Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
29,518,256
<h1>Plat_Forms</h1> <p>You may also want to have a look at <a href="https://www.plat-forms.org" rel="nofollow">Plat_Forms</a>.</p> <p>Plat_Forms was a fairly well-controlled, scientifically evaluated web development contest (teams of 3 worked for 2 days) comparing Java, PHP, Perl, and Ruby. Python is <em>not</em> in...
0
2015-04-08T14:57:11Z
[ "python", "productivity" ]
Are there statistical studies that indicates that Python is "more productive"?
354,124
<p>If I do a google search with the string "python productive" the first results is a page <a href="http://www.ferg.org/projects/python_java_side-by-side.html">http://www.ferg.org/projects/python_java_side-by-side.html</a> claiming that "python is more productive of Java". Many Python programmers that I have talked wi...
44
2008-12-09T20:29:40Z
40,007,599
<p>There's a great new paper on this: <a href="https://arxiv.org/pdf/1409.0252.pdf" rel="nofollow">https://arxiv.org/pdf/1409.0252.pdf</a></p> <p><a href="https://i.stack.imgur.com/AW6Io.png" rel="nofollow"><img src="https://i.stack.imgur.com/AW6Io.png" alt="enter image description here"></a></p> <p>Key finding: </p>...
0
2016-10-12T20:17:08Z
[ "python", "productivity" ]
Adding a SOAP header to a SOAPpy request
354,370
<p>Does anyone know how to do this? I need to add a header of the form:</p> <p> value1 value2 </p>
0
2008-12-09T21:52:26Z
493,527
<p>As the question is phrased, it's hard to guess what the intention (or even the intended semantics) is. For setting headers, try the following:</p> <pre><code>import SOAPpy headers = SOAPpy.Types.headerType() headers.value1 = value2 </code></pre> <p>or</p> <pre><code>[...] headers.foo = value1 headers.bar = value2...
4
2009-01-29T21:29:56Z
[ "python", "soappy" ]
How is ** implemented in Python?
354,421
<p>I'm wondering where I find the source to show how the operator ** is implemented in Python. Can someone point me in the right direction? </p>
9
2008-12-09T22:07:10Z
354,433
<p>It's the power to operator</p> <p><a href="http://www.python.org/doc/2.5.2/ref/power.html" rel="nofollow">python.org doc - Power operator</a></p> <p>Edit: Oh, dang, the code, right. Hope the link still helps. Sloppy read from my part</p>
1
2008-12-09T22:10:08Z
[ "python" ]
How is ** implemented in Python?
354,421
<p>I'm wondering where I find the source to show how the operator ** is implemented in Python. Can someone point me in the right direction? </p>
9
2008-12-09T22:07:10Z
354,622
<p>There are two different implementations one for int (long in 3.0) objects and another one for float objects. </p> <p>The float pow is the float_pow(PyObject *v, PyObject *w, PyObject *z) function defined in Objects/floatobject.c file of the Python source code. This functions calls pow() from C stdlib's math.h</p> ...
1
2008-12-09T23:32:22Z
[ "python" ]
How is ** implemented in Python?
354,421
<p>I'm wondering where I find the source to show how the operator ** is implemented in Python. Can someone point me in the right direction? </p>
9
2008-12-09T22:07:10Z
354,626
<p>The python grammar definition (from which the parser is generated using <a href="http://www.python.org/dev/peps/pep-0269/">pgen</a>), look for 'power': <a href="http://svn.python.org/view/python/trunk/Grammar/Grammar?rev=65872&amp;view=markup">Gramar/Gramar</a></p> <p>The python ast, look for 'ast_for_power': <a hr...
23
2008-12-09T23:33:18Z
[ "python" ]
How is ** implemented in Python?
354,421
<p>I'm wondering where I find the source to show how the operator ** is implemented in Python. Can someone point me in the right direction? </p>
9
2008-12-09T22:07:10Z
354,649
<p>I think caseysrandomthoughts are asking about asterisks at functions definition.</p> <p>You could find answer at this Python doc page: <a href="http://docs.python.org/tutorial/controlflow.html#more-on-defining-functions" rel="nofollow">http://docs.python.org/tutorial/controlflow.html#more-on-defining-functions</a><...
1
2008-12-09T23:47:28Z
[ "python" ]
How can I get the number of records that reference a particular foreign key in Django?
354,755
<p>I'm working on a blog application in Django. Naturally, I have models set up such that there are Posts and Comments, and a particular Post may have many Comments; thus, Post is a ForeignKey in the Comments model.</p> <p>Given a Post object, is there an easy way (ideally, through a method call) to find out how many ...
2
2008-12-10T00:43:29Z
354,765
<pre><code>Comments.objects.filter(post=post).count() </code></pre> <p>or:</p> <pre><code>post.comment_set.count() </code></pre>
6
2008-12-10T00:49:16Z
[ "python", "django", "django-models" ]
How can I get the number of records that reference a particular foreign key in Django?
354,755
<p>I'm working on a blog application in Django. Naturally, I have models set up such that there are Posts and Comments, and a particular Post may have many Comments; thus, Post is a ForeignKey in the Comments model.</p> <p>Given a Post object, is there an easy way (ideally, through a method call) to find out how many ...
2
2008-12-10T00:43:29Z
355,537
<p>You can add field CommentCount to you Post model, and update it in pre_save, pre_delete signals. It's a hard for the db to calculate comments count at every view call and number of queries will be grow.</p>
0
2008-12-10T09:47:32Z
[ "python", "django", "django-models" ]
How do you return multiple values in Python?
354,883
<p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p> <h3>Option: Using a tuple</h3> <p>Consider this trivial example:</p> <pre><code>def f(x): ...
435
2008-12-10T01:55:50Z
354,892
<p>I prefer</p> <pre><code>def g(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** y3 return {'y0':y0, 'y1':y1 ,'y2':y2 } </code></pre> <p>it seems everything else is just extra code to do the same thing.</p>
9
2008-12-10T02:00:49Z
[ "python", "coding-style", "return", "return-value" ]
How do you return multiple values in Python?
354,883
<p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p> <h3>Option: Using a tuple</h3> <p>Consider this trivial example:</p> <pre><code>def f(x): ...
435
2008-12-10T01:55:50Z
354,896
<p>In languages like Python, I would usually use a dictionary as it involves less overhead than creating a new class.</p> <p>However, if I find myself constantly returning the same set of variables, then that probably involves a new class that I'll factor out.</p>
1
2008-12-10T02:02:37Z
[ "python", "coding-style", "return", "return-value" ]
How do you return multiple values in Python?
354,883
<p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p> <h3>Option: Using a tuple</h3> <p>Consider this trivial example:</p> <pre><code>def f(x): ...
435
2008-12-10T01:55:50Z
354,918
<p>Generally, the "specialized structure" actually IS a sensible current state of an object, with its own methods.</p> <pre><code>class Some3SpaceThing(object): def __init__(self,x): self.g(x) def g(self,x): self.y0 = x + 1 self.y1 = x * 3 self.y2 = y0 ** y3 r = Some3SpaceThing( x ) r.y0 r.y1 r.y2...
7
2008-12-10T02:15:34Z
[ "python", "coding-style", "return", "return-value" ]
How do you return multiple values in Python?
354,883
<p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p> <h3>Option: Using a tuple</h3> <p>Consider this trivial example:</p> <pre><code>def f(x): ...
435
2008-12-10T01:55:50Z
354,929
<p>For small projects I find it easiest to work with tuples. When that gets too hard to manage (and not before) I start grouping things into logical structures, however I think your suggested use of dictionaries and ReturnValue objects is wrong (or too simplistic).</p> <p>Returning a dictionary with keys y0, y1, y2 et...
84
2008-12-10T02:22:28Z
[ "python", "coding-style", "return", "return-value" ]
How do you return multiple values in Python?
354,883
<p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p> <h3>Option: Using a tuple</h3> <p>Consider this trivial example:</p> <pre><code>def f(x): ...
435
2008-12-10T01:55:50Z
354,955
<p>I prefer to use tuples whenever a tuple feels "natural"; coordinates are a typical example, where the separate objects can stand on their own, e.g. in one-axis only scaling calculations.</p> <p>I use dictionaries as a return value only when the grouped objects aren't always the same. Think optional email headers.</...
14
2008-12-10T02:40:43Z
[ "python", "coding-style", "return", "return-value" ]
How do you return multiple values in Python?
354,883
<p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p> <h3>Option: Using a tuple</h3> <p>Consider this trivial example:</p> <pre><code>def f(x): ...
435
2008-12-10T01:55:50Z
354,958
<p>I vote for the dictionary.</p> <p>I find that if I make a function that returns anything more than 2-3 variables I'll fold them up in a dictionary. Otherwise I tend to forget the order and content of what I'm returning.</p> <p>Also, introducing a 'special' structure makes your code more difficult to follow. (Some...
33
2008-12-10T02:42:05Z
[ "python", "coding-style", "return", "return-value" ]
How do you return multiple values in Python?
354,883
<p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p> <h3>Option: Using a tuple</h3> <p>Consider this trivial example:</p> <pre><code>def f(x): ...
435
2008-12-10T01:55:50Z
355,036
<p>+1 on S.Lott's suggestion of a named container class.</p> <p>For python 2.6 and up, a <a href="http://docs.python.org/library/collections.html#collections.namedtuple">named tuple</a> provides a useful way of easily creating these container classes, and the results are "lightweight and require no more memory than re...
9
2008-12-10T03:51:47Z
[ "python", "coding-style", "return", "return-value" ]
How do you return multiple values in Python?
354,883
<p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p> <h3>Option: Using a tuple</h3> <p>Consider this trivial example:</p> <pre><code>def f(x): ...
435
2008-12-10T01:55:50Z
356,695
<p><a href="http://docs.python.org/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields">Named tuples</a> were added in 2.6 for this purpose. Also see <a href="http://docs.python.org/library/os.html#os.stat">os.stat</a> for a similar builtin example.</p> <pre><code>&gt;&gt;&gt; import col...
278
2008-12-10T16:36:01Z
[ "python", "coding-style", "return", "return-value" ]