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 |
|---|---|---|---|---|---|---|---|---|---|
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,068,412 | <p>There's a pretty neat sample from the Python Cookbook <a href="http://oreilly.com/pub/a/python/excerpt/pythonckbk_chap1/index1.html?page=last" rel="nofollow">here</a> -- the fastest version proposed on that URL is:</p>
<pre><code>import itertools
def erat2( ):
D = { }
yield 2
for q in itertools.islice(... | 36 | 2010-01-14T23:52:06Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,068,436 | <p>In general if you need fast number computation python is not the best choice. Today there are a lot of faster (and complex) algorithm. For example on my computer I got 2.2 second for your code, with Mathematica I got 0.088005.</p>
<p>First of all: do you need set?</p>
| 0 | 2010-01-14T23:56:33Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,068,451 | <p>The easiest optimization to implement is that if you want to check whether n is prime, you only have to check to see if n is divisible by a number up to square_root(n).
Is this for Project Euler?</p>
| 0 | 2010-01-15T00:01:05Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,068,522 | <p>The algorithm is fast, but it has a serious flaw:</p>
<pre><code>>>> sorted(get_primes(530))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,
167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 22... | 17 | 2010-01-15T00:14:04Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,068,548 | <p><strong>Warning:</strong> <code>timeit</code> results may vary due to differences in hardware or
version of Python. </p>
<p>Below is a script which compares a number of implementations:
<UL>
<LI>ambi_sieve_plain,
<LI><a href="http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-pyt... | 249 | 2010-01-15T00:19:09Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,070,107 | <p>For the fastest code, the numpy solution is the best. For purely academic reasons, though, I'm posting my pure python version, which is a bit less than 50% faster than the cookbook version posted above. Since I make the entire list in memory, you need enough space to hold everything, but it seems to scale fairly wel... | 3 | 2010-01-15T07:41:25Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,070,175 | <p>For <strong>truly</strong> fastest solution with sufficiently large N would be to download a <a href="http://primes.utm.edu/lists/small/millions/">pre-calculated list of primes</a>, store it as a tuple and do something like:</p>
<pre><code>for pos,i in enumerate(primes):
if i > N:
print primes[:pos]
... | 13 | 2010-01-15T07:58:18Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,073,279 | <p>Using <a href="http://plus.maths.org/issue50/features/havil/index.html">Sundaram's Sieve</a>, I think I broke pure-Python's record:</p>
<pre><code>def sundaram3(max_n):
numbers = range(3, max_n+1, 2)
half = (max_n)//2
initial = 4
for step in xrange(3, max_n+1, 2):
for i in xrange(initial, h... | 25 | 2010-01-15T16:50:02Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,106,069 | <p>A deterministic implementation of Miller-Rabin's Primality test on the assumption that N < 9,080,191</p>
<pre><code>import sys
import random
def miller_rabin_pass(a, n):
d = n - 1
s = 0
while d % 2 == 0:
d >>= 1
s += 1
a_to_power = pow(a, d, n)
if a_to_power == 1:
... | 3 | 2010-01-21T00:17:45Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,106,085 | <p>If you have control over N, the very fastest way to list all primes is to precompute them. Seriously. Precomputing is a way overlooked optimization.</p>
| 4 | 2010-01-21T00:21:40Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,110,724 | <p>Here's the code I normally use to generate primes in Python:</p>
<pre><code>$ python -mtimeit -s'import sieve' 'sieve.sieve(1000000)'
10 loops, best of 3: 445 msec per loop
$ cat sieve.py
from math import sqrt
def sieve(size):
prime=[True]*size
rng=xrange
limit=int(sqrt(size))
for i in rng(3,limit+1,+2):
i... | 3 | 2010-01-21T16:09:36Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 2,129,681 | <p>My guess is that the <em>fastest</em> of all ways is to hard code the primes in your code.</p>
<p>So why not just write a slow script that generates another source file that has all numbers hardwired in it, and then import that source file when you run your actual program.</p>
<p>Of course, this works only if you ... | 0 | 2010-01-25T01:21:22Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 3,035,188 | <p>Related question(dealing with primes generators & including benchmarks):<br>
<a href="http://stackoverflow.com/questions/2897297/speed-up-bitstring-bit-operations-in-python">Speed up bitstring/bit operations in Python?</a> </p>
<p>Faster & more memory-wise pure Python code:</p>
<pre><code>def primes(n):
... | 80 | 2010-06-14T05:49:40Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 3,476,592 | <p>Sorry to bother but erat2() has a serious flaw in the algorithm.</p>
<p>While searching for the next composite, we need to test odd numbers only.
q,p both are odd; then q+p is even and doesn't need to be tested, but q+2*p is always odd. This eliminates the "if even" test in the while loop condition and saves about ... | 0 | 2010-08-13T12:04:55Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 3,638,617 | <p>A slightly different implementation of a half sieve using Numpy:</p>
<p><a href="http://rebrained.com/?p=458" rel="nofollow">http://rebrained.com/?p=458</a></p>
<pre>
import math
import numpy
def prime6(upto):
primes=numpy.arange(3,upto+1,2)
isprime=numpy.ones((upto-1)/2,dtype=bool)
for factor in prime... | 3 | 2010-09-03T18:37:02Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 3,796,455 | <p>The fastest method I've tried so far is based on the <a href="http://oreilly.com/pub/a/python/excerpt/pythonckbk_chap1/index1.html?page=last" rel="nofollow">Python cookbook <code>erat2</code></a> function:</p>
<pre><code>import itertools as it
def erat2a( ):
D = { }
yield 2
for q in it.islice(it.count(... | 0 | 2010-09-26T03:07:35Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 14,972,126 | <p>First time using python, so some of the methods I use in this might seem a bit cumbersome. I just straight converted my c++ code to python and this is what I have (albeit a tad bit slowww in python)</p>
<pre><code>#!/usr/bin/env python
import time
def GetPrimes(n):
Sieve = [1 for x in xrange(n)]
Done = F... | 2 | 2013-02-20T04:06:22Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 16,004,075 | <p>I may be late to the party but will have to add my own code for this. It uses approximately n/2 in space because we don't need to store even numbers and I also make use of the bitarray python module, further draStically cutting down on memory consumption and enabling computing all primes up to 1,000,000,000</p>
<pr... | 0 | 2013-04-14T20:37:12Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 18,944,432 | <p>I know the competition is closed for some years. â¦</p>
<p>Nonetheless this is my suggestion for a pure python prime sieve, based on omitting the multiples of 2, 3 and 5 by using appropriate steps while processing the sieve forward. Nonetheless it is actually slower for N<10^9 than @Robert William Hanks superio... | 2 | 2013-09-22T13:35:58Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 21,505,621 | <p>This is an elegant and simpler solution to find primes using a stored list. Starts with a 4 variables, you only have to test odd primes for divisors, and you only have to test up to a half of what number you are testing as a prime (no point in testing whether 9, 11, 13 divide into 17). It tests previously stored pri... | -2 | 2014-02-02T00:41:24Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 26,704,740 | <p>I collected several prime number sieves over time. The fastest on my computer is this:</p>
<pre><code>from time import time
# 175 ms for all the primes up to the value 10**6
def primes_sieve(limit):
a = [True] * limit
a[0] = a[1] = False
#a[2] = True
for n in xrange(4, limit, 2):
a[n] = Fals... | 0 | 2014-11-02T21:39:27Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 28,504,698 | <p>This is the way you can compare with others.</p>
<pre><code># You have to list primes upto n
nums = xrange(2, n)
for i in range(2, 10):
nums = filter(lambda s: s==i or s%i, nums)
print nums
</code></pre>
<p>So simple...</p>
| -3 | 2015-02-13T16:55:13Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 28,517,084 | <p>I'm slow responding to this question but it seemed like a fun exercise. I'm using numpy which might be cheating and I doubt this method is the fastest but it should be clear. It sieves a Boolean array referring to its indices only and elicits prime numbers from the indices of all True values. No modulo needed.</p>
... | 0 | 2015-02-14T15:25:04Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 31,221,522 | <p>If you don't want to reinvent the wheel, you can install the symbolic maths library <a href="http://www.sympy.org/en/index.html" rel="nofollow">sympy</a> (yes it's Python 3 compatible)</p>
<pre><code>pip install sympy
</code></pre>
<p>And use the <a href="http://docs.sympy.org/dev/modules/ntheory.html#sympy.ntheor... | 3 | 2015-07-04T14:06:49Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 31,322,423 | <p>It's instructive to write your own prime finding code, but it's also useful to have a fast reliable library at hand. I wrote a wrapper around the <a href="http://primesieve.org/" rel="nofollow">C++ library primesieve</a>, named it <a href="https://github.com/hickford/primesieve-python" rel="nofollow">primesieve-pyth... | 3 | 2015-07-09T15:50:07Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 33,356,284 | <p>If you accept itertools but not numpy, here is an adaptation of rwh_primes2 for Python 3 that runs about twice as fast on my machine. The only substantial change is using a bytearray instead of a list for the boolean, and using compress instead of a list comprehension to build the final list. (I'd add this as a co... | 5 | 2015-10-26T21:51:11Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 39,879,618 | <p>It's all written and tested. So there is no need to reinvent the wheel.</p>
<pre><code>python -m timeit -r10 -s"from sympy import sieve" "primes = list(sieve.primerange(1, 10**6))"
</code></pre>
<p>gives us a record breaking <strong>12.2 msec</strong>!</p>
<pre><code>10 loops, best of 10: 12.2 msec per loop
</cod... | 0 | 2016-10-05T16:48:09Z | [
"python",
"math",
"optimization",
"primes"
] |
Fastest way to list all primes below N | 2,068,372 | <p>This is the best algorithm I could come up.</p>
<pre><code>def get_primes(n):
numbers = set(range(n, 1, -1))
primes = []
while numbers:
p = numbers.pop()
primes.append(p)
numbers.difference_update(set(range(p*2, n+1, p)))
return primes
>>> timeit.Timer(stmt='get_pri... | 251 | 2010-01-14T23:40:27Z | 40,101,683 | <p>I tested some <a href="http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n?answertab=active#tab-top">unutbu's functions</a>, i computed it with hungred millions number</p>
<p>The winners are the functions that use numpy library, </p>
<p><strong>Note</strong>: It would also interestin... | 0 | 2016-10-18T07:05:58Z | [
"python",
"math",
"optimization",
"primes"
] |
key/value (general) and tokyo cabinet (python tc-specific) question | 2,068,473 | <p>i have been in the RDBMS world for many years now but wish to explore the whole nosql movement. so here's my first question:</p>
<p>is it bad practice to have the possibility of duplicate keys? for example, an address book keyed off of last name (most probably search item?) could have multiple entities. is it ba... | 1 | 2010-01-15T00:04:59Z | 2,103,908 | <p>In a key-value store you cannot have duplicate keys, there's a single value behind a key. To have duplicate keys you can use the B+ Tree database in Tokyo Cabinet that has values ordered and allows duplicate (you can acces them by going to the first item with that key and iterating).</p>
<p>Not all problems are eas... | 0 | 2010-01-20T18:30:48Z | [
"python",
"tokyo-cabinet"
] |
key/value (general) and tokyo cabinet (python tc-specific) question | 2,068,473 | <p>i have been in the RDBMS world for many years now but wish to explore the whole nosql movement. so here's my first question:</p>
<p>is it bad practice to have the possibility of duplicate keys? for example, an address book keyed off of last name (most probably search item?) could have multiple entities. is it ba... | 1 | 2010-01-15T00:04:59Z | 2,384,015 | <p>This depend on no-sql implementation. Cassandra, for example, allows range queries, so you could model data to do queries on last name, or with full name (starting with last name, then first name).</p>
<p>Beyond this, many simpler key-value stores would indeed require you to store a list structure (or such) for mul... | 1 | 2010-03-05T01:40:36Z | [
"python",
"tokyo-cabinet"
] |
Python Function Reference | 2,068,486 | <p>There're tons of apps/widgets for PHP function reference and even for Ruby but I'm shocked to find there is nothing available for a popular language like Python (besides the official online documentation ofcourse). </p>
<p>Is there really not a single handy reference widget/app available for Python? I have 'Pocket ... | 3 | 2010-01-15T00:06:56Z | 2,068,807 | <p>So it's (often) not very pretty at all, but it's possible that the pydoc command line tool, or pydoc in webserver mode, could help you here. <a href="http://www.oreillynet.com/mac/blog/2005/10/programming_is_just_easier_wit.html" rel="nofollow">Here's an article on pydoc to help you get started</a></p>
| 0 | 2010-01-15T01:16:10Z | [
"python",
"osx",
"documentation",
"reference",
"widget"
] |
Python Function Reference | 2,068,486 | <p>There're tons of apps/widgets for PHP function reference and even for Ruby but I'm shocked to find there is nothing available for a popular language like Python (besides the official online documentation ofcourse). </p>
<p>Is there really not a single handy reference widget/app available for Python? I have 'Pocket ... | 3 | 2010-01-15T00:06:56Z | 2,068,830 | <p>Python libraries have (or should have) built in documentation through docstrings. Also, python code is (mostly) very readable, and reading the source (.py or even .c) is actually the preferred way for many developers to get the information they're looking for, especially since some corner cases may not even be docum... | 1 | 2010-01-15T01:21:08Z | [
"python",
"osx",
"documentation",
"reference",
"widget"
] |
Python Function Reference | 2,068,486 | <p>There're tons of apps/widgets for PHP function reference and even for Ruby but I'm shocked to find there is nothing available for a popular language like Python (besides the official online documentation ofcourse). </p>
<p>Is there really not a single handy reference widget/app available for Python? I have 'Pocket ... | 3 | 2010-01-15T00:06:56Z | 2,069,084 | <ol>
<li><p>The interactive interpreter is a fantastic reference tool. <code>dir(<identifier)</code> lists all the attributes of a module, class, or function <code>help(<identifier>)</code> gives you help about same.</p></li>
<li><p><code>pydoc</code> at the command line is another great tool. It does for Pyth... | 0 | 2010-01-15T02:35:58Z | [
"python",
"osx",
"documentation",
"reference",
"widget"
] |
Python Function Reference | 2,068,486 | <p>There're tons of apps/widgets for PHP function reference and even for Ruby but I'm shocked to find there is nothing available for a popular language like Python (besides the official online documentation ofcourse). </p>
<p>Is there really not a single handy reference widget/app available for Python? I have 'Pocket ... | 3 | 2010-01-15T00:06:56Z | 2,069,310 | <p>I develop on Mac OS.</p>
<p>I have all the Python documentation directly available through a desktop app.</p>
<p>The app is called Safari. I bookmark <a href="http://docs.python.org/index.html" rel="nofollow">http://docs.python.org/index.html</a></p>
<p>It's available as a desktop app.</p>
| -1 | 2010-01-15T03:31:06Z | [
"python",
"osx",
"documentation",
"reference",
"widget"
] |
time complexity of variable loops | 2,068,591 | <p>i want to try to calculate the O(n) of my program (in python). there are two problems: </p>
<p>1: i have a very basic knowledge of O(n) [aka: i know O(n) has to do with time and calculations]</p>
<p>and</p>
<p>2: all of the loops in my program are not set to any particular value. they are based on the input data.... | 1 | 2010-01-15T00:26:16Z | 2,068,621 | <p>The <code>n</code> in <code>O(n)</code> means precisely the input size. So, if I have this code:</p>
<pre><code>def findmax(l):
maybemax = 0
for i in l:
if i > maybemax:
maybemax = i
return maybemax
</code></pre>
<p>Then I'd say that the complexity is <code>O(n)</code> -- how lon... | 4 | 2010-01-15T00:31:53Z | [
"python",
"algorithm",
"big-o"
] |
time complexity of variable loops | 2,068,591 | <p>i want to try to calculate the O(n) of my program (in python). there are two problems: </p>
<p>1: i have a very basic knowledge of O(n) [aka: i know O(n) has to do with time and calculations]</p>
<p>and</p>
<p>2: all of the loops in my program are not set to any particular value. they are based on the input data.... | 1 | 2010-01-15T00:26:16Z | 2,069,027 | <p>Try this out to start, then head to wiki:</p>
<ul>
<li><a href="http://www.cforcoding.com/2009/07/plain-english-explanation-of-big-o.html" rel="nofollow">Plain English Explanation of Big O Notation</a></li>
</ul>
| 1 | 2010-01-15T02:19:06Z | [
"python",
"algorithm",
"big-o"
] |
read multiple files using multiprocessing | 2,068,645 | <p>I need to read some very huge text files (100+ Mb), process every lines with regex and store the data into a structure. My structure inherits from defaultdict, it has a read(self) method that read self.file_name file.</p>
<p>Look at this very simple (but not real) example, I'm not using regex, but I'm splitting lin... | 4 | 2010-01-15T00:38:28Z | 2,069,057 | <p>Multiprocessing is more suited to CPU- or memory-oriented processes since the seek time of rotational drives kills performance when switching between files. Either load your log files into a fast flash drive or some sort of memory disk (physical or virtual), or give up on multiprocessing.</p>
| 0 | 2010-01-15T02:29:59Z | [
"python",
"performance",
"multiprocessing"
] |
read multiple files using multiprocessing | 2,068,645 | <p>I need to read some very huge text files (100+ Mb), process every lines with regex and store the data into a structure. My structure inherits from defaultdict, it has a read(self) method that read self.file_name file.</p>
<p>Look at this very simple (but not real) example, I'm not using regex, but I'm splitting lin... | 4 | 2010-01-15T00:38:28Z | 2,069,108 | <p>You're creating a pool with as many workers as files. That may be too many. Usually, I aim to have the number of workers around the same as the number of cores.</p>
<p>The simple fact is that your final step is going to be a single process merging all the results together. There is no avoiding this, given your p... | 0 | 2010-01-15T02:40:58Z | [
"python",
"performance",
"multiprocessing"
] |
read multiple files using multiprocessing | 2,068,645 | <p>I need to read some very huge text files (100+ Mb), process every lines with regex and store the data into a structure. My structure inherits from defaultdict, it has a read(self) method that read self.file_name file.</p>
<p>Look at this very simple (but not real) example, I'm not using regex, but I'm splitting lin... | 4 | 2010-01-15T00:38:28Z | 2,069,556 | <p>You're probably hitting two problems.</p>
<p>One of them was mentioned: you're reading multiple files at once. Those reads will end up being interleaved, causing disk thrashing. You want to read whole files at once, and then only multithread the computation on the data.</p>
<p>Second, you're hitting the overhead... | 3 | 2010-01-15T04:36:36Z | [
"python",
"performance",
"multiprocessing"
] |
Access models in other project in a Django view cause "table doesn't exist" error | 2,069,254 | <p><strong>Base project structure</strong></p>
<pre><code>baseproject
baseapp
models.py
class BaseModel(models.Model)
...
</code></pre>
<p><strong>Other project structure</strong>:</p>
<pre><code>project
app
views.py
urls.py
</code></pre>
<p><strong>project.ap... | 2 | 2010-01-15T03:19:09Z | 2,072,033 | <p>You are fighting against the framework here, and you'll be better off if you rethink your architecture. Django is built around the assumption that a project = a given set of INSTALLED_APPS, and the project settings name a database to which those apps are synced. It's not clear here what problem you have with just do... | 5 | 2010-01-15T14:07:15Z | [
"python",
"django",
"model"
] |
Example for using Python Twisted with File Descriptors | 2,069,262 | <p>I'm looking to use twisted to control communication across Linux pipes (os.pipe()) and fifos (os.mkfifo()) between a master process and a set of slave processes. While I'm positive tat it's possible to use twisted for these types of file descriptors (after all, twisted is great for tcp sockets which *nix abstracts a... | 3 | 2010-01-15T03:20:02Z | 2,069,292 | <p>It does not have anything built-in for asynchronous I/O. Someone wrote a <a href="http://code.google.com/p/twisted-linux-aio/" rel="nofollow">libaio wrapper</a> for it, but it has not been touched for a long time, and I have no idea if it still works.</p>
<p>In the worst case you could use <code>select</code> to se... | -3 | 2010-01-15T03:27:50Z | [
"python",
"pipe",
"twisted",
"file-descriptor",
"mkfifo"
] |
Example for using Python Twisted with File Descriptors | 2,069,262 | <p>I'm looking to use twisted to control communication across Linux pipes (os.pipe()) and fifos (os.mkfifo()) between a master process and a set of slave processes. While I'm positive tat it's possible to use twisted for these types of file descriptors (after all, twisted is great for tcp sockets which *nix abstracts a... | 3 | 2010-01-15T03:20:02Z | 2,071,881 | <p>You can use <code>reactor.spawnProcess</code> to set up arbitrary file descriptor mappings between a parent process and a child process it spawns. For example, to run a program and give it two extra output descriptors (in addition to stdin, stdout, and stderr) with which it can send bytes back to the parent process... | 12 | 2010-01-15T13:42:33Z | [
"python",
"pipe",
"twisted",
"file-descriptor",
"mkfifo"
] |
What's the correct way to use win32file.ReadFile to get the output from a pipe? | 2,069,344 | <p>I'm using the <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">pywin32</a> extensions to access the win32 API under Python. I'm new at doing Windows programming in Python -- I'm a POSIX guy -- so I may be doing things in a bone-headed manner.</p>
<p>I'm trying to use the <a href="http://docs.active... | 1 | 2010-01-15T03:40:43Z | 2,069,350 | <p>Consider using <code>subprocess</code> to launch the process. It will give you a set of file-like objects that you can use to talk with the other app.</p>
<p>The <code>.terminate()</code> method of the Popen object will allow you to terminate the process if you are running 2.6+.</p>
| -1 | 2010-01-15T03:43:23Z | [
"python",
"windows",
"pywin32",
"readfile"
] |
What's the correct way to use win32file.ReadFile to get the output from a pipe? | 2,069,344 | <p>I'm using the <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">pywin32</a> extensions to access the win32 API under Python. I'm new at doing Windows programming in Python -- I'm a POSIX guy -- so I may be doing things in a bone-headed manner.</p>
<p>I'm trying to use the <a href="http://docs.active... | 1 | 2010-01-15T03:40:43Z | 2,074,751 | <p>For error codes, try winerror.ERROR_MORE_DATA and winerror.ERROR_IO_PENDING</p>
<p>My interpretation of the ActiveState docs is the same as yours. It sounds like the wrapper works slightly differently than the native API. Sorry I haven't actually tried this.</p>
| 2 | 2010-01-15T20:59:24Z | [
"python",
"windows",
"pywin32",
"readfile"
] |
What's the correct way to use win32file.ReadFile to get the output from a pipe? | 2,069,344 | <p>I'm using the <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">pywin32</a> extensions to access the win32 API under Python. I'm new at doing Windows programming in Python -- I'm a POSIX guy -- so I may be doing things in a bone-headed manner.</p>
<p>I'm trying to use the <a href="http://docs.active... | 1 | 2010-01-15T03:40:43Z | 19,068,014 | <p>note that ReadFile is defined as:</p>
<pre><code>(int, string) = ReadFile(hFile, buffer/bufSize , overlapped)
</code></pre>
<p>where...</p>
<pre><code>hFile = PyHANDLE
</code></pre>
<p>which is any windows handle (can be file, process, thread...)</p>
<pre><code>buffer/bufSize = PyOVERLAPPEDReadBuffer
</code></p... | -1 | 2013-09-28T14:30:04Z | [
"python",
"windows",
"pywin32",
"readfile"
] |
Help in Converting Small Python Code to PHP | 2,069,352 | <p>please i need some help in converting a python code to a php syntax
the code is for generating an alphanumeric code using alpha encoding</p>
<p>the code : </p>
<pre><code>def mkcpl(x):
x = ord(x)
set="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for c in set:
d = or... | -5 | 2010-01-15T03:43:45Z | 2,069,453 | <p>i will help you a little. For the rest of it, please read up on the <a href="http://www.php.net/docs.php" rel="nofollow">documentation</a>.</p>
<pre><code>function mkcpl($x){
$x=ord($x);
$set="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$set=str_split($set);
foreach($set as $c){... | 4 | 2010-01-15T04:07:19Z | [
"php",
"python"
] |
Help in Converting Small Python Code to PHP | 2,069,352 | <p>please i need some help in converting a python code to a php syntax
the code is for generating an alphanumeric code using alpha encoding</p>
<p>the code : </p>
<pre><code>def mkcpl(x):
x = ord(x)
set="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for c in set:
d = or... | -5 | 2010-01-15T03:43:45Z | 2,069,596 | <blockquote>
<p>the rest of them, like +=, == etc are
pretty much the same across the 2
languages.</p>
</blockquote>
<p>Careful; in PHP string concatenation is accomplished using <code>.=</code> not <code>+=</code>. If you try to use <code>+=</code> PHP will try to evaluate the expression mathematically (probabl... | 0 | 2010-01-15T04:51:35Z | [
"php",
"python"
] |
'%s' % 'somestring' | 2,069,362 | <p>Here are a couple of examples taken from <a href="http://github.com/nathanborror/django-basic-apps/" rel="nofollow">django-basic-apps</a>:</p>
<pre><code># self.title is a unicode string already
def __unicode__(self):
return u'%s' % self.title
# 'q' is a string
search_term = '%s' % request.GET['q']
</code>... | 5 | 2010-01-15T03:45:29Z | 2,069,398 | <p>At first glance, it doesn't look sensible, but it does have the benefit of forcing the result to be a string (or unicode string), rather than whatever it might have been from before. Another way to do the same thing might be to call <code>str</code> on the format argument (or unicode).</p>
| 1 | 2010-01-15T03:52:19Z | [
"python",
"django"
] |
'%s' % 'somestring' | 2,069,362 | <p>Here are a couple of examples taken from <a href="http://github.com/nathanborror/django-basic-apps/" rel="nofollow">django-basic-apps</a>:</p>
<pre><code># self.title is a unicode string already
def __unicode__(self):
return u'%s' % self.title
# 'q' is a string
search_term = '%s' % request.GET['q']
</code>... | 5 | 2010-01-15T03:45:29Z | 2,069,403 | <p>You're probably better off asking <a href="http://github.com/nathanborror" rel="nofollow">Nathan Borror</a>, the author. It may just be a personal style.</p>
<p>Django does use proxy objects for strings in some cases though, so it may be to force them to "actual" strings. I believe these proxies are for i18n/l10n p... | 1 | 2010-01-15T03:53:54Z | [
"python",
"django"
] |
'%s' % 'somestring' | 2,069,362 | <p>Here are a couple of examples taken from <a href="http://github.com/nathanborror/django-basic-apps/" rel="nofollow">django-basic-apps</a>:</p>
<pre><code># self.title is a unicode string already
def __unicode__(self):
return u'%s' % self.title
# 'q' is a string
search_term = '%s' % request.GET['q']
</code>... | 5 | 2010-01-15T03:45:29Z | 2,069,632 | <p>Maybe the author is used to strictly typed languages and he misses it in python and this is his way to make python more strictly typed than it is. </p>
<p>Here - to make the types of input/output parameters clear only for the <em>reader</em> because provided all is working as expected it is just useless for the <em... | 0 | 2010-01-15T05:05:50Z | [
"python",
"django"
] |
'%s' % 'somestring' | 2,069,362 | <p>Here are a couple of examples taken from <a href="http://github.com/nathanborror/django-basic-apps/" rel="nofollow">django-basic-apps</a>:</p>
<pre><code># self.title is a unicode string already
def __unicode__(self):
return u'%s' % self.title
# 'q' is a string
search_term = '%s' % request.GET['q']
</code>... | 5 | 2010-01-15T03:45:29Z | 2,070,444 | <p>Another idea: Maybe this is done with possible future implementations in mind? self.title and request.GET[â¦] are currently already of the desired type, but implementation details might change in the future, and they might stop being a unicode string or a string.</p>
<p>Now, I would have used str() and unicode(),... | 0 | 2010-01-15T09:07:22Z | [
"python",
"django"
] |
'%s' % 'somestring' | 2,069,362 | <p>Here are a couple of examples taken from <a href="http://github.com/nathanborror/django-basic-apps/" rel="nofollow">django-basic-apps</a>:</p>
<pre><code># self.title is a unicode string already
def __unicode__(self):
return u'%s' % self.title
# 'q' is a string
search_term = '%s' % request.GET['q']
</code>... | 5 | 2010-01-15T03:45:29Z | 2,072,925 | <p>It's just a habit of mine. In these cases it's not necessary.</p>
| 3 | 2010-01-15T16:03:27Z | [
"python",
"django"
] |
Serving up snippets of html and using urlfetch | 2,069,465 | <p>I'm trying to "modularize" a section of an appengine website where a profile is requested as a small hunk of pre-rendered html</p>
<p>Sending a request to /userInfo?id=4992 sends down some html like:</p>
<pre>
<div>
(image of john) John
Information about this user
</div>
</pre>
<p>So, ... | 0 | 2010-01-15T04:11:29Z | 2,069,517 | <p>You're currently serializing urlfetch requests, which ends up summing their wait times and may easily push you beyond your latency deadline. I'm afraid that you'll need to switch to <a href="http://code.google.com/appengine/docs/python/urlfetch/asynchronousrequests.html" rel="nofollow">async urlfetch</a> requests -... | 1 | 2010-01-15T04:26:58Z | [
"python",
"google-app-engine",
"urlfetch"
] |
python listen 2 port same file | 2,069,566 | <p>I would like to listen on 2 different UDP port with the same server.
I use SocketServer lib for my server, and basicly it looks like that;</p>
<p>SocketServer.UDPServer(('', 7878),CLASSNAME)</p>
<p>I would like to listen on 7878 and 7879 with the same server and same file.
Is that possible? If yes how?</p>
| 1 | 2010-01-15T04:40:09Z | 2,069,577 | <p>Nope. Consider using <a href="http://twistedmatrix.com/" rel="nofollow">Twisted</a>.</p>
| 1 | 2010-01-15T04:44:21Z | [
"python",
"udp",
"socketserver"
] |
python listen 2 port same file | 2,069,566 | <p>I would like to listen on 2 different UDP port with the same server.
I use SocketServer lib for my server, and basicly it looks like that;</p>
<p>SocketServer.UDPServer(('', 7878),CLASSNAME)</p>
<p>I would like to listen on 7878 and 7879 with the same server and same file.
Is that possible? If yes how?</p>
| 1 | 2010-01-15T04:40:09Z | 2,069,980 | <p>Sure you can, using threads. Here's a server:</p>
<pre><code>import SocketServer
import threading
class MyUDPHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print "%s wrote:" % self.client_address[0]
print data... | 3 | 2010-01-15T07:01:52Z | [
"python",
"udp",
"socketserver"
] |
python listen 2 port same file | 2,069,566 | <p>I would like to listen on 2 different UDP port with the same server.
I use SocketServer lib for my server, and basicly it looks like that;</p>
<p>SocketServer.UDPServer(('', 7878),CLASSNAME)</p>
<p>I would like to listen on 7878 and 7879 with the same server and same file.
Is that possible? If yes how?</p>
| 1 | 2010-01-15T04:40:09Z | 3,093,178 | <p>No need to use threads for something like this. Consider <a href="http://code.google.com/p/pyev/" rel="nofollow">http://code.google.com/p/pyev/</a></p>
| 0 | 2010-06-22T12:41:53Z | [
"python",
"udp",
"socketserver"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 2,069,670 | <p>This method works for <code>if</code>s, multiple nested loops and other constructs that you can't <code>break</code> from easily.</p>
<p>Wrap the code in its own function. Instead of <code>break</code>, <code>return</code>.</p>
| 38 | 2010-01-15T05:24:07Z | [
"python",
"control-flow"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 2,069,680 | <p>may be this?</p>
<pre><code>if some_condition and condition_a:
# do something
elif some_condition and condition_b:
# do something
# and then exit the outer if block
elif some_condition and not condition_b:
# more code here
else:
#blah
if
</code></pre>
| 2 | 2010-01-15T05:28:19Z | [
"python",
"control-flow"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 2,069,683 | <p>Effectively what you're describing are goto statements, which are generally panned pretty heavily. Your second example is far easier to understand. </p>
<p>However, cleaner still would be:</p>
<pre><code>if some_condition:
...
if condition_a:
your_function1()
else:
your_function2()
...
def... | 1 | 2010-01-15T05:29:21Z | [
"python",
"control-flow"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 2,069,687 | <pre>
from <a href="http://entrian.com/goto/">goto</a> import goto, label
if some_condition:
...
if condition_a:
# do something
# and then exit the outer if block
goto .end
...
if condition_b:
# do something
# and then exit the outer if block
goto .end
# more co... | 24 | 2010-01-15T05:29:47Z | [
"python",
"control-flow"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 2,069,698 | <p>You can emulate goto's functionality with exceptions:</p>
<pre><code>try:
# blah, blah ...
# raise MyFunkyException as soon as you want out
except MyFunkyException:
pass
</code></pre>
<p>Disclaimer: I only mean to bring to your attention the <em>possibility</em> of doing things this way, while in no wa... | 6 | 2010-01-15T05:35:05Z | [
"python",
"control-flow"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 2,069,964 | <p>Generally speaking, don't. If you are nesting "ifs" and breaking from them, you are doing it wrong.</p>
<p>However, if you must:</p>
<pre><code>if condition_a:
def condition_a_fun():
do_stuff()
if we_wanna_escape:
return
condition_a_fun()
if condition_b:
def condition_b_fun():
... | 1 | 2010-01-15T06:55:35Z | [
"python",
"control-flow"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 2,070,212 | <p>I don't like the whole idea of structuring code this way. I fear it leads to the same problems as using <code>goto</code> statements. (luckily, Python has not goto statement).</p>
| -1 | 2010-01-15T08:08:53Z | [
"python",
"control-flow"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 2,090,603 | <pre><code>while some_condition:
...
if condition_a:
# do something
break
...
if condition_b:
# do something
break
# more code here
break
</code></pre>
| 6 | 2010-01-19T02:05:03Z | [
"python",
"control-flow"
] |
How to exit an if clause | 2,069,662 | <p>What sorts of methods exist for prematurely exiting an <code>if</code> clause? </p>
<p>There are times when I'm writing code and want to put a <code>break</code> statement inside of an <code>if</code> clause, only to remember that those can only be used for loops.</p>
<p>Lets take the following code as an example:... | 39 | 2010-01-15T05:20:54Z | 32,452,499 | <p>For what was actually asked, my approach is to put those <code>if</code>s inside a one-looped loop</p>
<pre><code>while (True):
if (some_condition):
...
if (condition_a):
# do something
# and then exit the outer if block
break
...
if (condition... | 1 | 2015-09-08T08:10:49Z | [
"python",
"control-flow"
] |
Doing something *after* handling a request in Google App Engine | 2,069,713 | <p>I would like the GAE to do something else once my app has sent a response.</p>
<p>The handler would look like this:</p>
<pre><code>class FooHandler(webapp.RequestHandler):
def post(self):
self.response.out.write('Bar')
send_response() # this is where I need help!
do_something_else()... | 1 | 2010-01-15T05:38:58Z | 2,069,747 | <p>Presuming you have access to the WSGI layer, you can wrap the WSGI application and provide a callback to be executed once response sent. For how to do this see:</p>
<p><a href="http://code.google.com/p/modwsgi/wiki/RegisteringCleanupCode" rel="nofollow">http://code.google.com/p/modwsgi/wiki/RegisteringCleanupCode</... | 1 | 2010-01-15T05:49:51Z | [
"python",
"google-app-engine",
"wsgi"
] |
Doing something *after* handling a request in Google App Engine | 2,069,713 | <p>I would like the GAE to do something else once my app has sent a response.</p>
<p>The handler would look like this:</p>
<pre><code>class FooHandler(webapp.RequestHandler):
def post(self):
self.response.out.write('Bar')
send_response() # this is where I need help!
do_something_else()... | 1 | 2010-01-15T05:38:58Z | 2,069,762 | <p>You can't GAE sends it's response when the RequestHandler returns if you actually need Threads you will need to write your web application at another hosting company. </p>
| -1 | 2010-01-15T05:57:03Z | [
"python",
"google-app-engine",
"wsgi"
] |
Doing something *after* handling a request in Google App Engine | 2,069,713 | <p>I would like the GAE to do something else once my app has sent a response.</p>
<p>The handler would look like this:</p>
<pre><code>class FooHandler(webapp.RequestHandler):
def post(self):
self.response.out.write('Bar')
send_response() # this is where I need help!
do_something_else()... | 1 | 2010-01-15T05:38:58Z | 2,069,765 | <p>Maybe you can use the <a href="http://code.google.com/appengine/docs/python/taskqueue/" rel="nofollow">Task Queues</a>.</p>
| 9 | 2010-01-15T05:57:19Z | [
"python",
"google-app-engine",
"wsgi"
] |
Doing something *after* handling a request in Google App Engine | 2,069,713 | <p>I would like the GAE to do something else once my app has sent a response.</p>
<p>The handler would look like this:</p>
<pre><code>class FooHandler(webapp.RequestHandler):
def post(self):
self.response.out.write('Bar')
send_response() # this is where I need help!
do_something_else()... | 1 | 2010-01-15T05:38:58Z | 2,093,328 | <p>As already mentioned, you can use <a href="http://code.google.com/appengine/docs/python/taskqueue/" rel="nofollow">task queues</a> or the <a href="http://code.google.com/appengine/articles/deferred.html" rel="nofollow">deferred API</a>. Another option is outlined by Rafe Kaplan towards the end of his section in <a h... | 2 | 2010-01-19T12:15:22Z | [
"python",
"google-app-engine",
"wsgi"
] |
In optparse module - command line option parser, how to confirm if an option was not provided? | 2,069,998 | <p>From Python docs:</p>
<p>"Option.dest : If the optionâs action implies writing or modifying a value somewhere, this tells optparse where to write it: dest names an attribute of the options object that optparse builds as it parses the command line."</p>
<p>Can we put some check on the name of the attribute (dest)... | 0 | 2010-01-15T07:06:30Z | 2,070,184 | <p>You could use a default value of <code>None</code> for such options, which cannot be entered on the command line. Then you can check like</p>
<pre><code>if opts.optional_value is None:
# action for option not given
else:
# use value from command line
</code></pre>
| 1 | 2010-01-15T08:01:02Z | [
"python"
] |
Where can I find source or algorithm of Python's hash() function? | 2,070,276 | <pre><code>>>> hash("\x01")
128000384
>>> hash("\x02")
256000771
>>> hash("\x03")
384001154
>>> hash("\x04")
512001541
</code></pre>
<p>Interesting part is <code>128000384 x 2</code> is not <code>256000771</code>, and also others</p>
<p>I am just wondering how that algorithm works ... | 13 | 2010-01-15T08:26:46Z | 2,070,320 | <p>If you download the source code of Python, you will find it for sure!
But bear in mind the hash function is implemented for each kind of objects differently.</p>
<p>For example, you will find the unicode hash function in <code>Objects/unicodeobject.c</code> in the function <code>unicode_hash</code>. You might have ... | 16 | 2010-01-15T08:37:53Z | [
"python",
"c",
"algorithm",
"hash"
] |
Where can I find source or algorithm of Python's hash() function? | 2,070,276 | <pre><code>>>> hash("\x01")
128000384
>>> hash("\x02")
256000771
>>> hash("\x03")
384001154
>>> hash("\x04")
512001541
</code></pre>
<p>Interesting part is <code>128000384 x 2</code> is not <code>256000771</code>, and also others</p>
<p>I am just wondering how that algorithm works ... | 13 | 2010-01-15T08:26:46Z | 2,070,487 | <p>I recommend you to read the Wikipedia entry for hash functions ( <a href="http://en.wikipedia.org/wiki/Hash_function" rel="nofollow">http://en.wikipedia.org/wiki/Hash_function</a> ) to understand better the hash functions. You'll get a lot of answers for the implementation!</p>
<p>To summarize some key points (not ... | 0 | 2010-01-15T09:17:08Z | [
"python",
"c",
"algorithm",
"hash"
] |
Django ModelForm: adding an alias to a field | 2,070,377 | <p>I have a (GoogleAppEngine) Django ModelForm: </p>
<pre><code>class A(db.Model):
a = db.StringProperty()
class MyAForm(djangoforms.ModelForm):
class Meta:
model = A
</code></pre>
<p>This creates a form which has one string field <code>a</code>. On my form I'd like to call this something else, say <code>b</... | 2 | 2010-01-15T08:50:38Z | 2,070,633 | <pre><code>class A(db.Model):
a=db.StringProperty(verbose_name='b')
</code></pre>
| 0 | 2010-01-15T09:49:35Z | [
"python",
"django",
"google-app-engine",
"django-forms"
] |
Django ModelForm: adding an alias to a field | 2,070,377 | <p>I have a (GoogleAppEngine) Django ModelForm: </p>
<pre><code>class A(db.Model):
a = db.StringProperty()
class MyAForm(djangoforms.ModelForm):
class Meta:
model = A
</code></pre>
<p>This creates a form which has one string field <code>a</code>. On my form I'd like to call this something else, say <code>b</... | 2 | 2010-01-15T08:50:38Z | 2,070,841 | <p>It's not clear what you mean when you say you want to 'call' the field something else on your form. Do you mean the label? That's easy to do :</p>
<pre><code>class MyAForm(djangoforms.ModelForm):
a = forms.CharField(label='b')
class Meta:
model = A
</code></pre>
<p>If you want to change the underlying HTML... | 5 | 2010-01-15T10:33:49Z | [
"python",
"django",
"google-app-engine",
"django-forms"
] |
Django ModelForm: adding an alias to a field | 2,070,377 | <p>I have a (GoogleAppEngine) Django ModelForm: </p>
<pre><code>class A(db.Model):
a = db.StringProperty()
class MyAForm(djangoforms.ModelForm):
class Meta:
model = A
</code></pre>
<p>This creates a form which has one string field <code>a</code>. On my form I'd like to call this something else, say <code>b</... | 2 | 2010-01-15T08:50:38Z | 8,023,994 | <p>the other way should be redefining the label of the field:</p>
<pre><code>class MyAForm(forms.ModelForm):
class Meta:
model = A
def __init__(self, *args, **kwargs):
super(MyAForm, self).__init__(*args, **kwargs)
self.fields['a'].label = 'something'
</code></pre>
| 0 | 2011-11-05T23:09:23Z | [
"python",
"django",
"google-app-engine",
"django-forms"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,070,680 | <p>You can try something like this:</p>
<pre><code>class mylist(list):
def __sub__(self, b):
result = self[:]
b = b[:]
while b:
try:
result.remove(b.pop())
except ValueError:
raise Exception("Not all elements found during subtraction"... | 0 | 2010-01-15T10:03:39Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,070,700 | <p>I'm not sure what the objection to a for loop is: there is no multiset in Python so you can't use a builtin container to help you out.</p>
<p>Seems to me anything on one line (if possible) will probably be helishly complex to understand. Go for readability and KISS. Python is not C :)</p>
| 5 | 2010-01-15T10:06:50Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,070,734 | <p>I know "for" is not what you want, but it's simple and clear:</p>
<pre><code>for x in b:
a.remove(x)
</code></pre>
<p>Or if members of <code>b</code> might not be in <code>a</code> then use:</p>
<pre><code>for x in b:
if x in a:
a.remove(x)
</code></pre>
| 41 | 2010-01-15T10:11:40Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,070,772 | <p>to use list comprehension:</p>
<pre><code>[i for i in a if not i in b or b.remove(i)]
</code></pre>
<p>would do the trick. It would change b in the process though. But I agree with jkp and Dyno Fu that using a for loop would be better.</p>
<p>Perhaps someone can create a better example that uses list comprehensio... | 4 | 2010-01-15T10:19:34Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,070,835 | <p>To prove jkp's point that 'anything on one line will probably be helishly complex to understand', I created a one-liner. Please do not mod me down because I understand this is not a solution that you should actually use. It is just for demonstrational purposes.</p>
<p>The idea is to add the values in a one by one, ... | 2 | 2010-01-15T10:32:24Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,071,172 | <p>Python 2.7 and 3.2 will add the <a href="http://docs.python.org/dev/library/collections.html#collections.Counter">collections.Counter</a> class which is a dictionary that maps elements to the number of occurrences of the element. This can be used as a multiset.</p>
<p>According to the docs you should be able to do... | 24 | 2010-01-15T11:44:40Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,096,851 | <p>I attempted to find a more elegant solution, but the best I could do was basically the same thing that Dyno Fu said:</p>
<pre><code>from copy import copy
def subtract_lists(a, b):
"""
>>> a = [0, 1, 2, 1, 0]
>>> b = [0, 1, 1]
>>> subtract_lists(a, b)
[2, 0]
>&... | 0 | 2010-01-19T20:34:22Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,096,944 | <p>Python 2.7+ and 3.0 have <a href="http://docs.python.org/dev/library/collections.html#collections.Counter">collections.Counter</a> (a.k.a. multiset). The documentation links to <a href="http://code.activestate.com/recipes/576611/">Recipe 576611: Counter class</a> for Python 2.5:</p>
<pre><code>from operator import... | 4 | 2010-01-19T20:49:18Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 2,794,615 | <p><strike>I would do it in an easier way:</p>
<pre><code>a_b = [e for e in a if not e in b ]
</code></pre>
<p></strike></p>
<p>..as wich wrote, this is wrong - it works only if the items are unique in the lists. And if they are, it's better to use</p>
<pre><code>a_b = list(set(a) - set(b))
</code></pre>
| 17 | 2010-05-08T15:26:36Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 11,518,219 | <pre><code>list(set([x for x in a if x not in b]))
</code></pre>
<ul>
<li>Leaves <code>a</code> and <code>b</code> untouched.</li>
<li>Is a unique set of "a - b".</li>
<li>Done. </li>
</ul>
| -3 | 2012-07-17T07:54:33Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 17,289,301 | <pre><code>c = [i for i in b if i not in a]
</code></pre>
| -2 | 2013-06-25T04:42:44Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 27,634,444 | <p>Here's a relatively long but efficient and readable solution. It's O(n).</p>
<pre><code>def list_diff(list1, list2):
counts = {}
for x in list1:
try:
counts[x] += 1
except:
counts[x] = 1
for x in list2:
try:
counts[x] -= 1
if counts... | 0 | 2014-12-24T09:19:37Z | [
"python",
"list",
"collections"
] |
Subtracting two lists in Python | 2,070,643 | <p>In Python, How can one subtract two non-unique, unordered lists? Say we have a = [0,1,2,1,0] and b = [0, 1, 1] I'd like to do something like c = a - b and have c be [2, 0] or [0, 2] order doesn't matter to me. This should throw an exception if a does not contain all elements in b.</p>
<p><strong>Note this is differ... | 25 | 2010-01-15T09:53:17Z | 35,717,697 | <p>You can use the <code>map</code> construct to do this. It looks quite ok, but beware that the <code>map</code> line itself will return a list of <code>None</code>s.</p>
<pre><code>a = [1, 2, 3]
b = [2, 3]
map(lambda x:a.remove(x), b)
a
</code></pre>
| 0 | 2016-03-01T08:12:21Z | [
"python",
"list",
"collections"
] |
user pointer in python | 2,070,768 | <p>*I am trying to display preview from webcam captured using v4l.</p>
<p>Here is an idea of how the code looks like:</p>
<pre><code>from ctypes import *
from v4l2 import *
from Image import fromstring
from Tkinter import Tk, Label
from ImageTk import PhotoImage
from ctypes.util import find_library
libc = CDLL(find_... | 2 | 2010-01-15T10:18:32Z | 2,071,766 | <p>I don't know much about ctypes, but I am doing a similar thing (wrapped c++ webcam capture, displayed with DirectPython).</p>
<p>In my case I just made a buffer in python like this:</p>
<pre><code>bufferSize = imageWidth * imageHeight
buf = "\0" * bufferSize
</code></pre>
<p>Pass buf to your image capture functio... | 0 | 2010-01-15T13:24:45Z | [
"python",
"ctypes",
"v4l2"
] |
user pointer in python | 2,070,768 | <p>*I am trying to display preview from webcam captured using v4l.</p>
<p>Here is an idea of how the code looks like:</p>
<pre><code>from ctypes import *
from v4l2 import *
from Image import fromstring
from Tkinter import Tk, Label
from ImageTk import PhotoImage
from ctypes.util import find_library
libc = CDLL(find_... | 2 | 2010-01-15T10:18:32Z | 2,073,174 | <p>Have you tried passing <code>buffer</code> directly as the first arg? If that doesn't work and you want to make a writeable character buffer with <code>ctypes</code>, <a href="http://docs.python.org/library/ctypes.html?highlight=buffer#ctypes.create_string_buffer" rel="nofollow">create_string_buffer</a> is the only... | 0 | 2010-01-15T16:36:42Z | [
"python",
"ctypes",
"v4l2"
] |
user pointer in python | 2,070,768 | <p>*I am trying to display preview from webcam captured using v4l.</p>
<p>Here is an idea of how the code looks like:</p>
<pre><code>from ctypes import *
from v4l2 import *
from Image import fromstring
from Tkinter import Tk, Label
from ImageTk import PhotoImage
from ctypes.util import find_library
libc = CDLL(find_... | 2 | 2010-01-15T10:18:32Z | 2,085,742 | <p><em>OK, what I did was to leave pygame for Tkinter and PIL</em></p>
<p><em>now after the same allocation I pass</em> <code>buffer.m.userptr</code> *to fromstring method from Image</p>
<p><em>first I have, of course, the following:</em></p>
<pre><code>import Image
import Tkinter
tk = Tkinter.Tk()
preview = ImageT... | 0 | 2010-01-18T11:49:39Z | [
"python",
"ctypes",
"v4l2"
] |
user pointer in python | 2,070,768 | <p>*I am trying to display preview from webcam captured using v4l.</p>
<p>Here is an idea of how the code looks like:</p>
<pre><code>from ctypes import *
from v4l2 import *
from Image import fromstring
from Tkinter import Tk, Label
from ImageTk import PhotoImage
from ctypes.util import find_library
libc = CDLL(find_... | 2 | 2010-01-15T10:18:32Z | 2,089,790 | <p>Looks like <a href="http://docs.python.org/library/ctypes.html#ctypes.string_at" rel="nofollow"><code>ctypes.string_at(address, size)</code></a> is what you want. That'll give you a python string buffer with the contents of the memory at your pointer's address. This should be suitable to pass to <code>Image.fromstri... | 1 | 2010-01-18T22:49:46Z | [
"python",
"ctypes",
"v4l2"
] |
user pointer in python | 2,070,768 | <p>*I am trying to display preview from webcam captured using v4l.</p>
<p>Here is an idea of how the code looks like:</p>
<pre><code>from ctypes import *
from v4l2 import *
from Image import fromstring
from Tkinter import Tk, Label
from ImageTk import PhotoImage
from ctypes.util import find_library
libc = CDLL(find_... | 2 | 2010-01-15T10:18:32Z | 2,139,414 | <p><em>Ok. so for now I fixed the sizeimage problem by setting the sizeimage myself:</em></p>
<p><em>Now frombuffer displays something that is not the frame from the buffer.</em></p>
<pre><code>from ctypes import *
from v4l2 import *
from Image import fromstring
from Tkinter import Tk, Label
from ImageTk import Photo... | 0 | 2010-01-26T12:53:35Z | [
"python",
"ctypes",
"v4l2"
] |
Markdown and Syntax Highlighting in Django with mixed code | 2,070,831 | <p>I have some problems with following string while
trying to syntax highlight them:</p>
<p>Example</p>
<pre><code><code class="php"><? echo "<input type=\"text\">"; ?></code>
</code></pre>
<p>The php part is rendered correctly, but the html part breaks.</p>
<p>I use the Markdown and Syntax... | 2 | 2010-01-15T10:31:25Z | 2,070,860 | <p>Looks like you need to pass your PHP/HTML hybrid code through the <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#escape" rel="nofollow"><code>escape</code></a> filter, to convert instances of <code><</code> to <code>&lt;</code> etc.</p>
<p>Use it like this in a template, assuming you'v... | 1 | 2010-01-15T10:37:28Z | [
"python",
"django",
"syntax-highlighting",
"markdown"
] |
Markdown and Syntax Highlighting in Django with mixed code | 2,070,831 | <p>I have some problems with following string while
trying to syntax highlight them:</p>
<p>Example</p>
<pre><code><code class="php"><? echo "<input type=\"text\">"; ?></code>
</code></pre>
<p>The php part is rendered correctly, but the html part breaks.</p>
<p>I use the Markdown and Syntax... | 2 | 2010-01-15T10:31:25Z | 2,376,996 | <p><strong>Python markdown integrates with Pygments that do the syntax highlighting.</strong></p>
<p>You can go from markdown to html formatted text with source code with highlighted syntax.</p>
<p>The short version is:</p>
<pre><code>import markdown
html = markdown.markdown(text,['codehilite'])
</code></pre>
<p><c... | 1 | 2010-03-04T05:04:40Z | [
"python",
"django",
"syntax-highlighting",
"markdown"
] |
Using classes for Django views, is it Pythonic? | 2,071,258 | <p>I'm currently learning Python and coming from a strong C# background. I keep hearing about doing things in a Pythonic way to take advantage of the dynamic nature of the language and some of it I get and some I don't.</p>
<p>I'm creating a site with Django and my approach to views is to use classes. My current thi... | 3 | 2010-01-15T12:03:00Z | 2,071,322 | <p>Certainly there's nothing wrong with using a class for a view, provided you route the URL to an actual instance of a class and not just a class directly.</p>
| 4 | 2010-01-15T12:13:52Z | [
"python",
"django",
"design"
] |
Using classes for Django views, is it Pythonic? | 2,071,258 | <p>I'm currently learning Python and coming from a strong C# background. I keep hearing about doing things in a Pythonic way to take advantage of the dynamic nature of the language and some of it I get and some I don't.</p>
<p>I'm creating a site with Django and my approach to views is to use classes. My current thi... | 3 | 2010-01-15T12:03:00Z | 2,072,184 | <p>The Django admin does exactly this - look at the source code in django/contrib/admin. </p>
<p>The advantage of classes is that they are much easier to customize, for example you can add hooks for permission checking.</p>
<p>There is a proposal to move all existing generic views over to classes, it was supposed to ... | 2 | 2010-01-15T14:23:56Z | [
"python",
"django",
"design"
] |
Using classes for Django views, is it Pythonic? | 2,071,258 | <p>I'm currently learning Python and coming from a strong C# background. I keep hearing about doing things in a Pythonic way to take advantage of the dynamic nature of the language and some of it I get and some I don't.</p>
<p>I'm creating a site with Django and my approach to views is to use classes. My current thi... | 3 | 2010-01-15T12:03:00Z | 2,075,445 | <p>Setting aside other concerns (such as thread-safety issues), it feels like there's a real possible danger here to cross the bright lines between Model / View / Template. </p>
<p>Or maybe it feels like a replacement for url dispatching (<a href="http://www.pkmeco.com/seinfeld/outing.htm" rel="nofollow">not that ther... | 0 | 2010-01-15T23:32:04Z | [
"python",
"django",
"design"
] |
Using classes for Django views, is it Pythonic? | 2,071,258 | <p>I'm currently learning Python and coming from a strong C# background. I keep hearing about doing things in a Pythonic way to take advantage of the dynamic nature of the language and some of it I get and some I don't.</p>
<p>I'm creating a site with Django and my approach to views is to use classes. My current thi... | 3 | 2010-01-15T12:03:00Z | 2,076,684 | <p>While class-based views are useful, inheritance may not be the right tool for this particular job. Helper functions and decorators are two great ways to factor out common code from your views. They also tend to be be more familiar/natural to other (python) coders who might work on your code.</p>
<p>I'm not sure wha... | 0 | 2010-01-16T08:20:22Z | [
"python",
"django",
"design"
] |
Auto kill fastcgi process when deploying django | 2,071,615 | <p>Everything goes well days ago. But since today, when I run fastcgi, the process will be killed by system automatically. The worst thing is I don't know why and which process kill the fastcgi process.</p>
<p>Let me give some detail.</p>
<p>we use nginx to serve static files for another django app which listen to 8... | 2 | 2010-01-15T13:06:58Z | 2,072,377 | <p>Check your logfiles!</p>
<p>You can make add <em>outlog</em> and <em>errlog</em> options to the <em>runfcgi</em> command to log stderr and stdout to a file. See <code>python manage.py help runfcgi</code>.</p>
| 0 | 2010-01-15T14:52:18Z | [
"python",
"django",
"process",
"fastcgi"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.