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 listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
Searching across multiple tables (best practices) | 1,059,253 | <p>I have property management application consisting of tables:</p>
<pre><code>tenants
landlords
units
properties
vendors-contacts
</code></pre>
<p>Basically I want one search field to search them all rather than having to select which category I am searching. Would this be an acceptable solution (technology wise?)<... | 2 | 2009-06-29T16:36:05Z | 1,059,488 | <p>I would suggest using a specialized full-text indexing tool like Lucene for this. It will probably be easier to get up and running, and the result is faster and more featureful too. Postgres full text indexes will be useful if you also need structured search capability on top of this or transactionality of your sear... | 3 | 2009-06-29T17:31:09Z | [
"python",
"sql",
"mysql",
"postgresql",
"pylons"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 1,059,596 | <p>A case where regular expressions are justified:</p>
<pre><code>import re
DATA = "Hey, you - what are you doing here!?"
print re.findall(r"[\w']+", DATA)
# Prints ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre>
<p>[Edited to include <code>'</code> in the word characters - thanks, Danosaure.]</p>... | 271 | 2009-06-29T17:56:39Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 1,059,600 | <p>You want Python's RegEx module's <code>findall()</code> method:</p>
<p><a href="http://www.regular-expressions.info/python.html" rel="nofollow">http://www.regular-expressions.info/python.html</a></p>
<p><a href="http://stackoverflow.com/questions/234512/splitting-strings-in-python">Example</a></p>
| -2 | 2009-06-29T17:57:25Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 1,059,601 | <p><a href="http://docs.python.org/library/re.html#re.split">re.split()</a></p>
<blockquote>
<p>re.split(pattern, string[, maxsplit=0])</p>
<p>Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the res... | 244 | 2009-06-29T17:57:49Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 1,059,612 | <p>try this:</p>
<pre><code>import re
phrase = "Hey, you - what are you doing here!?"
matches = re.findall('\w+', phrase)
print matches
</code></pre>
<p>this will print <code>['Hey', 'you', 'what', 'are', 'you', 'doing', 'here']</code></p>
| 4 | 2009-06-29T18:01:00Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 1,059,856 | <p>Another way to achieve this is to use the Natural Language Tool Kit (<a href="http://nltk.org/doc/en/ch02.html" rel="nofollow">nltk</a>).</p>
<pre><code>import nltk
data= "Hey, you - what are you doing here!?"
word_tokens = nltk.tokenize.regexp_tokenize(data, r'\w+')
print word_tokens
</code></pre>
<p>This prints:... | 1 | 2009-06-29T18:51:37Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 1,157,473 | <p>Use list comprehensions for this stuff...it seems easier</p>
<pre><code>data= "Hey, you - what are you doing here!?"
tokens = [c for c in data if c not in (',', ' ', '-', '!', '?')]
</code></pre>
<p>I find this easier to comprehend (read..maintain) than using regexp, simply because I am not that good at regexp...w... | -4 | 2009-07-21T05:49:02Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 1,157,498 | <p>Another way, without regex</p>
<pre><code>import string
punc = string.punctuation
thestring = "Hey, you - what are you doing here!?"
s = list(thestring)
''.join([o for o in s if not o in punc]).split()
</code></pre>
| 40 | 2009-07-21T06:02:03Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 2,911,664 | <p>Kinda late answer :), but I had a similar dilemma and didn't want to use 're' module.</p>
<pre><code>def my_split(s, seps):
res = [s]
for sep in seps:
s, res = res, []
for seq in s:
res += seq.split(sep)
return res
print my_split('1111 2222 3333;4444,5555;6666', [' ', ';', ... | 20 | 2010-05-26T09:31:24Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 5,310,226 | <p>got same problem as @ooboo and find this topic
@ghostdog74 inspired me, maybe someone finds my solution usefull</p>
<pre><code>str1='adj:sg:nom:m1.m2.m3:pos'
splitat=':.'
''.join([ s if s not in splitat else ' ' for s in str1]).split()
</code></pre>
<p>input something in space place and split using same character ... | 1 | 2011-03-15T10:12:20Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 5,894,804 | <pre><code>join = lambda x: sum(x,[]) # a.k.a. flatten1([[1],[2,3],[4]]) -> [1,2,3,4]
# ...alternatively...
join = lambda lists: [x for l in lists for x in l]
</code></pre>
<p>Then this becomes a three-liner:</p>
<pre><code>fragments = [text]
for token in tokens:
fragments = join(f.split(token) for f in fragm... | 8 | 2011-05-05T08:35:59Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 6,966,543 | <p>Here is my go at a split with multiple deliminaters:</p>
<pre><code>def msplit( str, delims ):
w = ''
for z in str:
if z not in delims:
w += z
else:
if len(w) > 0 :
yield w
w = ''
if len(w) > 0 :
yield w
</code></pre>
| 1 | 2011-08-06T11:38:15Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 7,215,696 | <p>Another quick way to do this without a regexp is to replace the characters first, as below:</p>
<pre><code>>>> 'a;bcd,ef g'.replace(';',' ').replace(',',' ').split()
['a', 'bcd', 'ef', 'g']
</code></pre>
| 147 | 2011-08-27T16:10:52Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 9,630,983 | <p>I think the following is the best answer to suite your needs :</p>
<p><code>\W+</code> maybe suitable for this case, but may not be suitable for other cases.</p>
<pre><code>filter(None, re.compile('[ |,|\-|!|?]').split( "Hey, you - what are you doing here!?")
</code></pre>
| 0 | 2012-03-09T08:30:11Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 9,943,927 | <p>Use replace two times:</p>
<pre><code>a = '11223FROM33344INTO33222FROM3344'
a.replace('FROM', ',,,').replace('INTO', ',,,').split(',,,')
</code></pre>
<p>results in: </p>
<pre><code>['11223', '33344', '33222', '3344']
</code></pre>
| 4 | 2012-03-30T13:27:30Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 10,250,343 | <p>I'm re-acquainting myself with Python and needed the same thing.
The findall solution may be better, but I came up with this:</p>
<pre><code>tokens = [x.strip() for x in data.split(',')]
</code></pre>
| 2 | 2012-04-20T16:53:46Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 12,189,710 | <p>Pro-Tip: Use <code>string.translate</code> for the fastest string operations Python has.</p>
<p>Some proof...</p>
<p>First, the slow way (sorry pprzemek):</p>
<pre><code>>>> import timeit
>>> S = 'Hey, you - what are you doing here!?'
>>> def my_split(s, seps):
... res = [s]
... ... | 29 | 2012-08-30T04:05:54Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 16,271,773 | <p>Heres my take on it....</p>
<pre><code>def split_string(source,splitlist):
splits = frozenset(splitlist)
l = []
s1 = ""
for c in source:
if c in splits:
if s1:
l.append(s1)
s1 = ""
else:
print s1
s1 = s1 + c
if s... | 0 | 2013-04-29T05:32:04Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 19,211,729 | <p>I like <strong>re</strong>, but here is my solution without it:</p>
<pre><code>from itertools import groupby
sep = ' ,-!?'
s = "Hey, you - what are you doing here!?"
print [''.join(g) for k, g in groupby(s, sep.__contains__) if not k]
</code></pre>
<p><strong>sep.__contains__</strong> is a method used by 'in' oper... | 1 | 2013-10-06T17:30:05Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 20,753,993 | <pre><code>def get_words(s):
l = []
w = ''
for c in s.lower():
if c in '-!?,. ':
if w != '':
l.append(w)
w = ''
else:
w = w + c
if w != '':
l.append(w)
return l
</code></pre>
<p>Here is the usage:</p>
<pre><code>>>... | 1 | 2013-12-24T02:17:13Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 21,639,500 | <p>I like the <code>replace()</code> way the best. The following procedure changes all separators defined in a string <code>splitlist</code> to the first separator in <code>splitlist</code> and then splits the text on that one separator. It also accounts for if <code>splitlist</code> happens to be an empty string. It r... | 1 | 2014-02-07T23:15:39Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 23,720,594 | <p>So many answers, yet I can't find any solution that does efficiently what the <em>title</em> of the questions literally asks for (splitting with multiple separatorsâinstead, many answers remove anything that is not a word). So here is an answer to the question in the title ("string split with multiple separators")... | 96 | 2014-05-18T09:43:54Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 26,743,214 | <p>First of all, I don't think that your intention is to actually use punctuation as delimiters in the split functions. Your description suggests that you simply want to eliminate punctuation from the resultant strings.</p>
<p>I come across this pretty frequently, and my usual solution doesn't require re.</p>
<h2>On... | 0 | 2014-11-04T19:17:37Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 30,589,540 | <p>First of all, always use re.compile() before performing any RegEx operation in a loop because it works faster than normal operation.</p>
<p>so for your problem first compile the pattern and then perform action on it.</p>
<pre><code>import re
DATA = "Hey, you - what are you doing here!?"
reg_tok = re.compile("[\w']... | 1 | 2015-06-02T07:06:45Z | [
"python",
"string",
"split"
] |
Python - Split Strings with Multiple Delimiters | 1,059,559 | <p>I think what I want to do is a fairly common task but I've found no reference on the web. I have text, with punctuation, and I want list of the words. </p>
<pre><code>"Hey, you - what are you doing here!?"
</code></pre>
<p>should be</p>
<pre><code>['hey', 'you', 'what', 'are', 'you', 'doing', 'here']
</code></pre... | 334 | 2009-06-29T17:49:35Z | 37,634,255 | <p>Here is the answer with some explanation.</p>
<pre><code>st = "Hey, you - what are you doing here!?"
# replace all the non alpha-numeric with space and then join.
new_string = ''.join([x.replace(x, ' ') if not x.isalnum() else x for x in st])
# output of new_string
'Hey you what are you doing here '
# str.spli... | 0 | 2016-06-04T19:35:58Z | [
"python",
"string",
"split"
] |
How do I install with distutils to a specific Python installation? | 1,059,594 | <p>I have a Windows machine with Python 2.3, 2.6 and 3.0 installed and 2.5 installed with Cygwin. I've downloaded the pexpect package but when I run "python setup.py install" it installs to the 2.6 installation.</p>
<p>How could I have it install to the Cygwin Python installation, or any other installation?</p>
| 1 | 2009-06-29T17:56:32Z | 1,059,606 | <p>call the specific python version that you want to install for. For example:</p>
<pre><code>$ python2.3 setup.py install
</code></pre>
<p>should install the package for python 2.3</p>
| 5 | 2009-06-29T17:59:00Z | [
"python",
"install",
"distutils"
] |
How do I install with distutils to a specific Python installation? | 1,059,594 | <p>I have a Windows machine with Python 2.3, 2.6 and 3.0 installed and 2.5 installed with Cygwin. I've downloaded the pexpect package but when I run "python setup.py install" it installs to the 2.6 installation.</p>
<p>How could I have it install to the Cygwin Python installation, or any other installation?</p>
| 1 | 2009-06-29T17:56:32Z | 1,085,582 | <p>using "python2.3" can be wrong if another (default) installation patched PATH to itself only.</p>
<p>Task can be solved by:</p>
<ol>
<li>finding full path to desired python interpreter, on ActivePython it is C:\Python26 for default installation of Python 2.6</li>
<li>make a full path to binary (in this case C:\Pyt... | 0 | 2009-07-06T05:46:20Z | [
"python",
"install",
"distutils"
] |
Python Memory Model | 1,059,674 | <p>I have a very large list
Suppose I do that (yeah, I know the code is very unpythonic, but for the example's sake..):</p>
<pre><code>n = (2**32)**2
for i in xrange(10**7)
li[i] = n
</code></pre>
<p>works fine. however:</p>
<pre><code>for i in xrange(10**7)
li[i] = i**2
</code></pre>
<p>consumes a significantl... | 7 | 2009-06-29T18:12:55Z | 1,059,691 | <p>You have only one variable n, but you create many i**2.</p>
<p>What happens is that Python works with references. Each time you do <code>array[i] = n</code> you create a new reference to the value of <code>n</code>. Not to the variable, mind you, to the value. However, in the second case, when you do <code>array[i]... | 3 | 2009-06-29T18:17:37Z | [
"python",
"arrays",
"memory",
"model"
] |
Python Memory Model | 1,059,674 | <p>I have a very large list
Suppose I do that (yeah, I know the code is very unpythonic, but for the example's sake..):</p>
<pre><code>n = (2**32)**2
for i in xrange(10**7)
li[i] = n
</code></pre>
<p>works fine. however:</p>
<pre><code>for i in xrange(10**7)
li[i] = i**2
</code></pre>
<p>consumes a significantl... | 7 | 2009-06-29T18:12:55Z | 1,059,707 | <p>In your first example you are storing the same integer len(arr) times. So python need just store the integer once in memory and refers to it len(arr) times.</p>
<p>In your second example, you are storing len(arr) different integers. Now python must allocate storage for len(arr) integers and refer to to them in each... | 5 | 2009-06-29T18:21:42Z | [
"python",
"arrays",
"memory",
"model"
] |
Python Memory Model | 1,059,674 | <p>I have a very large list
Suppose I do that (yeah, I know the code is very unpythonic, but for the example's sake..):</p>
<pre><code>n = (2**32)**2
for i in xrange(10**7)
li[i] = n
</code></pre>
<p>works fine. however:</p>
<pre><code>for i in xrange(10**7)
li[i] = i**2
</code></pre>
<p>consumes a significantl... | 7 | 2009-06-29T18:12:55Z | 1,059,716 | <p>Java special-cases a few value types (including integers) so that they're stored by value (instead of, by object reference like everything else). Python doesn't special-case such types, so that assigning n to many entries <em>in a list</em> (or other normal Python container) doesn't have to make copies.</p>
<p>Edit... | 14 | 2009-06-29T18:23:07Z | [
"python",
"arrays",
"memory",
"model"
] |
Python Memory Model | 1,059,674 | <p>I have a very large list
Suppose I do that (yeah, I know the code is very unpythonic, but for the example's sake..):</p>
<pre><code>n = (2**32)**2
for i in xrange(10**7)
li[i] = n
</code></pre>
<p>works fine. however:</p>
<pre><code>for i in xrange(10**7)
li[i] = i**2
</code></pre>
<p>consumes a significantl... | 7 | 2009-06-29T18:12:55Z | 1,059,761 | <p>In both examples <code>arr[i]</code> takes reference of object whether it is <code>n</code> or resulting object of <code>i * 2</code>.</p>
<p>In first example, <code>n</code> is already defined so it only takes reference, but in second example, it has to evaluate <code>i * 2</code>, GC has to allocate space if need... | 0 | 2009-06-29T18:33:27Z | [
"python",
"arrays",
"memory",
"model"
] |
Strange behavior with ModelForm and saving | 1,059,831 | <p>This problem is very strange and I'm hoping someone can help me. For the sake of argument, I have a Author model with ForeignKey relationship to the Book model. When I display an author, I would like to have a ChoiceField that ONLY displays the books associated with that author. As such, I override the AuthorForm... | 0 | 2009-06-29T18:45:24Z | 1,059,845 | <p>Not quite sure what you are doing wrong, but it is best to just modify the queryset:</p>
<pre><code>class ClientForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.affiliate = kwargs.pop('affiliate')
super(ClientForm, self).__init__(*args, **kwargs)
self.fields["referral"].... | 2 | 2009-06-29T18:49:58Z | [
"python",
"django",
"modelform"
] |
How to delete all the items of a specific key in a list of dicts? | 1,059,924 | <p>I'm trying to remove some items of a dict based on their key, here is my code:</p>
<pre><code>d1 = {'a': 1, 'b': 2}
d2 = {'a': 1}
l = [d1, d2, d1, d2, d1, d2]
for i in range(len(l)):
if l[i].has_key('b'):
del l[i]['b']
print l
</code></pre>
<p>The output will be:</p>
<pre><code>[{'a': 1}, {'a': 1},... | 0 | 2009-06-29T19:05:35Z | 1,059,981 | <pre><code>d1 = {'a': 1, 'b': 2}
d2 = {'a': 1}
l = [d1, d2, d1, d2, d1, d2]
for d in l:
d.pop('b',None)
print l
</code></pre>
| 16 | 2009-06-29T19:15:45Z | [
"python"
] |
How to delete all the items of a specific key in a list of dicts? | 1,059,924 | <p>I'm trying to remove some items of a dict based on their key, here is my code:</p>
<pre><code>d1 = {'a': 1, 'b': 2}
d2 = {'a': 1}
l = [d1, d2, d1, d2, d1, d2]
for i in range(len(l)):
if l[i].has_key('b'):
del l[i]['b']
print l
</code></pre>
<p>The output will be:</p>
<pre><code>[{'a': 1}, {'a': 1},... | 0 | 2009-06-29T19:05:35Z | 1,060,002 | <p>A slight simplification:</p>
<pre><code> for d in l:
if d.has_key('b'):
del d['b']
</code></pre>
<p>Some people might also do</p>
<pre><code> for d in l:
try:
del d['b']
except KeyError:
pass
</code></pre>
<p>Catching exceptions like this is not considered as expe... | 3 | 2009-06-29T19:18:15Z | [
"python"
] |
How to delete all the items of a specific key in a list of dicts? | 1,059,924 | <p>I'm trying to remove some items of a dict based on their key, here is my code:</p>
<pre><code>d1 = {'a': 1, 'b': 2}
d2 = {'a': 1}
l = [d1, d2, d1, d2, d1, d2]
for i in range(len(l)):
if l[i].has_key('b'):
del l[i]['b']
print l
</code></pre>
<p>The output will be:</p>
<pre><code>[{'a': 1}, {'a': 1},... | 0 | 2009-06-29T19:05:35Z | 1,060,031 | <p>I like your way of doing it (except that you use a loop variable, but others pointed that out already), it's excplicit and easy to understand. If you want something that minimizes typing then this works:</p>
<p>[x.pop('b', None) for x in l]</p>
<p>Note though that only one 'b' will be deleted, because your list l ... | 2 | 2009-06-29T19:25:26Z | [
"python"
] |
How to delete all the items of a specific key in a list of dicts? | 1,059,924 | <p>I'm trying to remove some items of a dict based on their key, here is my code:</p>
<pre><code>d1 = {'a': 1, 'b': 2}
d2 = {'a': 1}
l = [d1, d2, d1, d2, d1, d2]
for i in range(len(l)):
if l[i].has_key('b'):
del l[i]['b']
print l
</code></pre>
<p>The output will be:</p>
<pre><code>[{'a': 1}, {'a': 1},... | 0 | 2009-06-29T19:05:35Z | 1,060,086 | <p>d1 = {'a': 1, 'b': 2}
d2 = {'a': 1}</p>
<p>l = [d1, d2, d1, d2, d1, d2]</p>
<p>for i in range(len(l)):
if l[i].has_key('b'):
del l[i]['b']</p>
<p>print l</p>
<p>Here is little review of your code:</p>
<ol>
<li>iterating on list is not done like in C. If you don't need reference the list index it's b... | 0 | 2009-06-29T19:37:37Z | [
"python"
] |
using pyodbc on ubuntu to insert a image field on SQL Server | 1,060,035 | <p>I am using <strong>Ubuntu 9.04</strong></p>
<p>I have installed the following package versions:</p>
<pre><code>unixodbc and unixodbc-dev: 2.2.11-16build3
tdsodbc: 0.82-4
libsybdb5: 0.82-4
freetds-common and freetds-dev: 0.82-4
python2.6-dev
</code></pre>
<p>I have configured <code>/etc/unixodbc.ini</code> like th... | 7 | 2009-06-29T19:26:37Z | 1,060,182 | <p>I think you should be using a <code>pyodbc.Binary</code> instance to wrap the data:</p>
<pre><code>cur.execute('INSERT INTO testing (myimage) VALUES (?)', (pyodbc.Binary(data),))
</code></pre>
<p>Retrieving should be</p>
<pre><code>cur.execute('SELECT myimage FROM testing')
print "image bytes: %r" % str(cur.fetch... | 3 | 2009-06-29T19:58:17Z | [
"python",
"sql-server",
"image",
"pyodbc",
"freetds"
] |
using pyodbc on ubuntu to insert a image field on SQL Server | 1,060,035 | <p>I am using <strong>Ubuntu 9.04</strong></p>
<p>I have installed the following package versions:</p>
<pre><code>unixodbc and unixodbc-dev: 2.2.11-16build3
tdsodbc: 0.82-4
libsybdb5: 0.82-4
freetds-common and freetds-dev: 0.82-4
python2.6-dev
</code></pre>
<p>I have configured <code>/etc/unixodbc.ini</code> like th... | 7 | 2009-06-29T19:26:37Z | 1,073,801 | <p>Huh, just after offering the bounty, I've found out the solution.</p>
<p>You have to use <code>SET TEXTSIZE 2147483647</code> on the query, in addition of text size configuration option in <code>/etc/freetds/freetds.conf</code>. </p>
<p>I have used</p>
<pre><code>cur.execute('SET TEXTSIZE 2147483647 SELECT myimag... | 5 | 2009-07-02T11:20:33Z | [
"python",
"sql-server",
"image",
"pyodbc",
"freetds"
] |
using pyodbc on ubuntu to insert a image field on SQL Server | 1,060,035 | <p>I am using <strong>Ubuntu 9.04</strong></p>
<p>I have installed the following package versions:</p>
<pre><code>unixodbc and unixodbc-dev: 2.2.11-16build3
tdsodbc: 0.82-4
libsybdb5: 0.82-4
freetds-common and freetds-dev: 0.82-4
python2.6-dev
</code></pre>
<p>I have configured <code>/etc/unixodbc.ini</code> like th... | 7 | 2009-06-29T19:26:37Z | 6,943,683 | <p>I had a similar <code>4096</code> truncation issue on <code>TEXT</code> fields, which <code>SET TEXTSIZE 2147483647</code> fixed for me, but this also fixed it for me:</p>
<pre><code>import os
os.environ['TDSVER'] = '8.0'
</code></pre>
| 1 | 2011-08-04T14:56:53Z | [
"python",
"sql-server",
"image",
"pyodbc",
"freetds"
] |
Python Decorator 3.0 and arguments to the decorator | 1,060,193 | <p>I'm excited to see the latest version of the <code>decorator</code> python module (3.0). It looks a lot cleaner (e.g. the syntax is more sugary than ever) than previous iterations.</p>
<p>However, it seems to have lousy support (e.g. "sour" syntax, to horribly stretch the metaphor) for decorators that take argumen... | 5 | 2009-06-29T20:00:53Z | 1,060,244 | <p>In this case, you need to make your function return the decorator. (Anything can be solved by another level of indirection...)</p>
<pre><code>from decorator import decorator
def substitute_args(arg_sub_dict):
@decorator
def wrapper(fun, arg):
new_arg = arg_sub_dict.get(arg, arg)
return fun(new_arg)
re... | 7 | 2009-06-29T20:11:15Z | [
"python",
"decorator"
] |
Python Decorator 3.0 and arguments to the decorator | 1,060,193 | <p>I'm excited to see the latest version of the <code>decorator</code> python module (3.0). It looks a lot cleaner (e.g. the syntax is more sugary than ever) than previous iterations.</p>
<p>However, it seems to have lousy support (e.g. "sour" syntax, to horribly stretch the metaphor) for decorators that take argumen... | 5 | 2009-06-29T20:00:53Z | 2,187,674 | <p>here is another way i have just discovered: check whether the first (and only) argument to your decorator is callable; if so, you are done and can return your behavior-modifying wrapper method (itself decorated with <code>functools.wraps</code> to preserve name and documentation string). </p>
<p>in the other case, ... | -2 | 2010-02-02T21:01:47Z | [
"python",
"decorator"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 1,060,330 | <p>Why are there two nested iterations? For me it produces the same list of data with only one iteration:</p>
<pre><code>for single_date in (start_date + timedelta(n) for n in range(day_count)):
print ...
</code></pre>
<p>And no list gets stored, only one generator is iterated over. Also the "if" in the generator... | 235 | 2009-06-29T20:27:14Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 1,060,352 | <p>This might be more clear:</p>
<pre><code>d = start_date
delta = datetime.timedelta(days=1)
while d <= end_date:
print d.strftime("%Y-%m-%d")
d += delta
</code></pre>
| 99 | 2009-06-29T20:31:43Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 1,060,376 | <pre><code>import datetime
def daterange(start, stop, step=datetime.timedelta(days=1), inclusive=False):
# inclusive=False to behave like range by default
if step.days > 0:
while start < stop:
yield start
start = start + step
# not +=! don't modify object passed in if it's mutable
... | 12 | 2009-06-29T20:35:13Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 1,061,779 | <p>Use the <a href="http://labix.org/python-dateutil"><code>dateutil</code></a> library:</p>
<pre><code>from datetime import date
from dateutil.rrule import rrule, DAILY
a = date(2009, 5, 30)
b = date(2009, 6, 9)
for dt in rrule(DAILY, dtstart=a, until=b):
print dt.strftime("%Y-%m-%d")
</code></pre>
<p>This pyt... | 91 | 2009-06-30T04:49:37Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 1,063,240 | <pre><code>import datetime
def daterange(start, stop, step_days=1):
current = start
step = datetime.timedelta(step_days)
if step_days > 0:
while current < stop:
yield current
current += step
elif step_days < 0:
while current > stop:
yield ... | 4 | 2009-06-30T11:49:11Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 3,059,362 | <pre class="lang-py prettyprint-override"><code>for i in range(16):
print datetime.date.today() + datetime.timedelta(days=i)
</code></pre>
| 2 | 2010-06-17T06:26:49Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 6,673,470 | <p>What about the following for doing a range incremented by days:</p>
<pre><code>for d in map( lambda x: startDate+datetime.timedelta(days=x), xrange( (stopDate-startDate).days ) ):
# Do stuff here
</code></pre>
<ul>
<li>startDate and stopDate are datetime.date objects</li>
</ul>
<p>For a generic version:</p>
<p... | 0 | 2011-07-13T02:35:14Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 15,969,361 | <p>Why not try:</p>
<pre><code>import datetime as dt
start_date = dt.datetime(2012, 12,1)
end_date = dt.datetime(2012, 12,5)
total_days = (end_date - start_date).days + 1 #inclusive 5 days
for day_number in range(total_days):
current_date = (start_date + dt.timedelta(days = day_number)).date()
print current... | 8 | 2013-04-12T10:47:11Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 23,853,523 | <p>Pandas is great for time series in general, and has direct support for date ranges.</p>
<pre><code>import pandas as pd
daterange = pd.date_range(start_date, end_date)
</code></pre>
<p>You can then loop over the daterange to print the date:</p>
<pre><code>for single_date in daterange:
print (single_date.strfti... | 13 | 2014-05-25T08:44:46Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 24,977,763 | <p>This function has some extra features:</p>
<ul>
<li>can pass a string matching the DATE_FORMAT for start or end and it is converted to a date object</li>
<li>can pass a date object for start or end</li>
<li><p>error checking in case the end is older than the start</p>
<pre><code>import datetime
from datetime impor... | 0 | 2014-07-27T03:55:35Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 36,831,624 | <p><strong>Show the last n days from today:</strong></p>
<pre><code>import datetime
for i in range(0, 100):
print (datetime.date.today() + datetime.timedelta(i)).isoformat()
</code></pre>
<p><strong>Output:</strong> </p>
<pre><code>2016-06-29
2016-06-30
2016-07-01
2016-07-02
2016-07-03
2016-07-04
</code></pre>
| 0 | 2016-04-25T03:34:21Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 37,508,911 | <p>Numpy's <code>arange</code> function can be applied to dates:</p>
<pre><code>import numpy as np
from datetime import datetime, timedelta
d0 = datetime(2009, 1,1)
d1 = datetime(2010, 1,1)
dt = timedelta(days = 1)
dates = np.arange(d0, d1, dt).astype(datetime)
</code></pre>
<p>The use of <code>astype</code> is to co... | 1 | 2016-05-29T10:41:40Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 38,402,483 | <p>Here's code for a general date range function, similar to Ber's answer, but more flexible:</p>
<pre><code>def count_timedelta(delta, step, seconds_in_interval):
"""Helper function for iterate. Finds the number of intervals in the timedelta."""
return int(delta.total_seconds() / (seconds_in_interval * step)... | 0 | 2016-07-15T17:59:27Z | [
"python",
"datetime",
"iteration"
] |
Iterating through a range of dates in Python | 1,060,279 | <p>I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension. </p>
<pre class="lang-py prettyprint-override"><code>day_count = (end_date - start_date).days + 1
for single_da... | 172 | 2009-06-29T20:16:02Z | 40,023,824 | <p>This is the most human-readable solution I can think of.</p>
<pre><code>import datetime
def daterange(start, end, step=datetime.timedelta(1)):
curr = start
while curr < end:
yield curr
curr += step
</code></pre>
| 0 | 2016-10-13T14:28:08Z | [
"python",
"datetime",
"iteration"
] |
How do i output a dynamically generated web page to a .html page instead of .py cgi page? | 1,060,289 | <p>Hi
So ive just started learning python on WAMP, ive got the results of a html form using cgi, and successfully performed a database search with mysqldb. I can return the results to a page that ends with .py by using print statements in the python cgi code, but i want to create a webpage that's .html and have that r... | 1 | 2009-06-29T20:18:18Z | 1,060,342 | <p>First, I'd suggest that you remember that URLs are URLs and that file extensions don't matter, and that you should just leave it.</p>
<p>If that isn't enough, then remember that URLs are URLs and that file extensions don't matter — and configure Apache to use a different rule to determine that is a CGI progra... | 3 | 2009-06-29T20:30:33Z | [
"python",
"html",
"webpage"
] |
USB - sync vs async vs semi-async | 1,060,305 | <p>Updates:</p>
<p>I wrote an asynchronous C version and it works as it should. </p>
<p>Turns out the speed issue was due to Python's GIL. There's a method to fine tune its behavior.
sys.setcheckinterval(interval)</p>
<p>Setting interval to zero (default is 100) fixes the slow speed issue. Now all that's left is to ... | 4 | 2009-06-29T20:22:41Z | 1,082,707 | <p>Ok I don't know if I get you right. You have some device with LCD, you have some firmware on it to handle USB requests. On PC side you are using PyUSB wich wraps libUsb. </p>
<p>Couple of suggestions if you are experiancing speed problems, try to limit data you are transfering. Do not transfer whole raw data, mayby... | 1 | 2009-07-04T18:13:09Z | [
"python",
"usb",
"ctypes",
"libusb"
] |
payment processing - pylons/python | 1,060,334 | <p>I'm building an application that eventually needs to process cc #s. I'd like to handle it completely in my app, and then hand off the information securely to my payment gateway. Ideally the user would have no interaction with the payment gateway directly.</p>
<p>Any thoughts? Is there an easier way?</p>
| 2 | 2009-06-29T20:28:25Z | 1,060,758 | <p>That's something usual to do. Please follow the instructions your payment gateway gives you on how to send info to them, and write the code. If you have some issue, feel free to ask a more specific question.</p>
| 1 | 2009-06-29T21:50:25Z | [
"python",
"pylons",
"payment-gateway",
"payment"
] |
payment processing - pylons/python | 1,060,334 | <p>I'm building an application that eventually needs to process cc #s. I'd like to handle it completely in my app, and then hand off the information securely to my payment gateway. Ideally the user would have no interaction with the payment gateway directly.</p>
<p>Any thoughts? Is there an easier way?</p>
| 2 | 2009-06-29T20:28:25Z | 1,061,221 | <p>Most payment gateways offer a few mechanisms for submitting CC payments:</p>
<p>1) A simple HTTPS POST where your application collects the customer's payment details (card number, expiry date, amount, optional CVV) and then submits this to the gateway. The payment parameters are sent through in the POST variables, ... | 3 | 2009-06-30T00:28:13Z | [
"python",
"pylons",
"payment-gateway",
"payment"
] |
payment processing - pylons/python | 1,060,334 | <p>I'm building an application that eventually needs to process cc #s. I'd like to handle it completely in my app, and then hand off the information securely to my payment gateway. Ideally the user would have no interaction with the payment gateway directly.</p>
<p>Any thoughts? Is there an easier way?</p>
| 2 | 2009-06-29T20:28:25Z | 1,063,700 | <p>You will probably find that it's easier to just let the payment gateway handle it. It's best to leave PCI compliance to the experts.</p>
| 1 | 2009-06-30T13:29:06Z | [
"python",
"pylons",
"payment-gateway",
"payment"
] |
How do I include a PHP script in Python? | 1,060,436 | <p>I have a PHP script (news-generator.php) which, when I include it, grabs a bunch of news items and prints them. Right now, I'm using Python for my website (CGI). When I was using PHP, I used something like this on the "News" page:</p>
<pre><code><?php
print("<h1>News and Updates</h1>");
include("news... | 6 | 2009-06-29T20:44:02Z | 1,060,509 | <p>PHP is a program. You can run any program with <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a>.</p>
<p>The hard part is simulating the whole CGI environment that PHP expects. </p>
| 6 | 2009-06-29T20:57:03Z | [
"php",
"python",
"scripting",
"integration",
"execution"
] |
How do I include a PHP script in Python? | 1,060,436 | <p>I have a PHP script (news-generator.php) which, when I include it, grabs a bunch of news items and prints them. Right now, I'm using Python for my website (CGI). When I was using PHP, I used something like this on the "News" page:</p>
<pre><code><?php
print("<h1>News and Updates</h1>");
include("news... | 6 | 2009-06-29T20:44:02Z | 1,060,515 | <pre><code>import subprocess
def php(script_path):
p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
result = p.communicate()[0]
return result
# YOUR CODE BELOW:
page_html = "<h1>News and Updates</h1>"
news_script_output = php("news-generator.php")
print page_html + news_scri... | 9 | 2009-06-29T20:58:16Z | [
"php",
"python",
"scripting",
"integration",
"execution"
] |
How do I include a PHP script in Python? | 1,060,436 | <p>I have a PHP script (news-generator.php) which, when I include it, grabs a bunch of news items and prints them. Right now, I'm using Python for my website (CGI). When I was using PHP, I used something like this on the "News" page:</p>
<pre><code><?php
print("<h1>News and Updates</h1>");
include("news... | 6 | 2009-06-29T20:44:02Z | 1,060,620 | <p>I think the best answer would be to have apache render both pages separately and then use javascript to load that page into a div. You have the slight slowdown of the ajax load but then you dont have to worry about it. </p>
<p>There is an open-source widget thing that will run multiple languages in 1 page but I can... | 0 | 2009-06-29T21:18:56Z | [
"php",
"python",
"scripting",
"integration",
"execution"
] |
How do I include a PHP script in Python? | 1,060,436 | <p>I have a PHP script (news-generator.php) which, when I include it, grabs a bunch of news items and prints them. Right now, I'm using Python for my website (CGI). When I was using PHP, I used something like this on the "News" page:</p>
<pre><code><?php
print("<h1>News and Updates</h1>");
include("news... | 6 | 2009-06-29T20:44:02Z | 1,061,284 | <p>You could use urllib to get the page from the server (localhost) and execute it in the right environment for php. Not pretty, but it'll work. It may cause performance problems if you do it a lot.</p>
| 0 | 2009-06-30T00:55:49Z | [
"php",
"python",
"scripting",
"integration",
"execution"
] |
How do I include a PHP script in Python? | 1,060,436 | <p>I have a PHP script (news-generator.php) which, when I include it, grabs a bunch of news items and prints them. Right now, I'm using Python for my website (CGI). When I was using PHP, I used something like this on the "News" page:</p>
<pre><code><?php
print("<h1>News and Updates</h1>");
include("news... | 6 | 2009-06-29T20:44:02Z | 1,065,081 | <p>maybe off topic, but if you want to do this in a way where you can access the vars and such created by the php script (eg. array of news items), your best best will be to do the exec of the php script, but return a json encoded array of items from php as a string, then json decode them on the python side, and do you... | 0 | 2009-06-30T18:00:02Z | [
"php",
"python",
"scripting",
"integration",
"execution"
] |
HTML Agility Pack or HTML Screen Scraping libraries for Java, Ruby, Python? | 1,060,484 | <p>I found the <a href="http://www.codeplex.com/htmlagilitypack" rel="nofollow">HTML Agility Pack</a> useful and easy to use for screen scraping web sites. What's the equivalent library for HTML screen scraping in Java, Ruby, Python?</p>
| 2 | 2009-06-29T20:53:56Z | 1,060,527 | <p><a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> is the standard Python screen scraping tool.</p>
<p>Recently, however, I used the (incomplete at the moment) <a href="http://pyquery.org/" rel="nofollow">pyQuery</a>, which is more or less a rewrite of jQuery into python, and f... | 3 | 2009-06-29T20:59:59Z | [
"java",
"python",
"html",
"ruby",
"screen-scraping"
] |
HTML Agility Pack or HTML Screen Scraping libraries for Java, Ruby, Python? | 1,060,484 | <p>I found the <a href="http://www.codeplex.com/htmlagilitypack" rel="nofollow">HTML Agility Pack</a> useful and easy to use for screen scraping web sites. What's the equivalent library for HTML screen scraping in Java, Ruby, Python?</p>
| 2 | 2009-06-29T20:53:56Z | 1,060,595 | <p>Found what I was looking for:
<a href="http://stackoverflow.com/questions/2861/options-for-html-scraping">http://stackoverflow.com/questions/2861/options-for-html-scraping</a></p>
| 5 | 2009-06-29T21:13:47Z | [
"java",
"python",
"html",
"ruby",
"screen-scraping"
] |
Difference between type(obj) and obj.__class__ | 1,060,499 | <p>What is the difference between <code>type(obj)</code> and <code>obj.__class__</code>? Is there ever a possibility of <code>type(obj) is not obj.__class__</code>?</p>
<p>I want to write a function that works generically on the supplied objects, using a default value of 1 in the same type as another parameter. Which... | 33 | 2009-06-29T20:55:52Z | 1,060,537 | <p>Old-style classes are the problem, sigh:</p>
<pre><code>>>> class old: pass
...
>>> x=old()
>>> type(x)
<type 'instance'>
>>> x.__class__
<class __main__.old at 0x6a150>
>>>
</code></pre>
<p>Not a problem in Python 3 since all classes are new-style now;-).<... | 28 | 2009-06-29T21:02:10Z | [
"python",
"new-style-class"
] |
Difference between type(obj) and obj.__class__ | 1,060,499 | <p>What is the difference between <code>type(obj)</code> and <code>obj.__class__</code>? Is there ever a possibility of <code>type(obj) is not obj.__class__</code>?</p>
<p>I want to write a function that works generically on the supplied objects, using a default value of 1 in the same type as another parameter. Which... | 33 | 2009-06-29T20:55:52Z | 1,060,547 | <p><code>type(obj)</code> and <code>type.__class__</code> do not behave the same for old style classes:</p>
<pre><code>>>> class a(object):
... pass
...
>>> class b(a):
... pass
...
>>> class c:
... pass
...
>>> ai=a()
>>> bi=b()
>>> ci=c()
>>>... | 12 | 2009-06-29T21:04:00Z | [
"python",
"new-style-class"
] |
Difference between type(obj) and obj.__class__ | 1,060,499 | <p>What is the difference between <code>type(obj)</code> and <code>obj.__class__</code>? Is there ever a possibility of <code>type(obj) is not obj.__class__</code>?</p>
<p>I want to write a function that works generically on the supplied objects, using a default value of 1 in the same type as another parameter. Which... | 33 | 2009-06-29T20:55:52Z | 10,633,356 | <p>This is an old question, but none of the answers seems to mention that. in the general case, it <strong>IS</strong> possible for a new-style class to have different values for <code>type(instance)</code> and <code>instance.__class__</code>:</p>
<pre><code>class ClassA(object):
def display(self):
print("... | 21 | 2012-05-17T09:50:40Z | [
"python",
"new-style-class"
] |
How to direct tkinter to look elsewhere for Tcl/Tk library (to dodge broken library without reinstalling) | 1,060,745 | <p>I've written a Python script that uses Tkinter. I want to deploy that script on a handful of computers that are on Mac OS 10.4.11. But that build of MAC OS X seems to have a broken TCL/TK install. Even loading the package gives me:</p>
<pre><code>Traceback (most recent call last):
File "<stdin>", line 1, in ?... | 4 | 2009-06-29T21:46:16Z | 1,081,118 | <p>You can change where your system looks for dynamic/shared libraries by altering <code>DYLD_LIBRARY_PATH</code> in your environment before launching Python. You can do this in Terminal like so:</p>
<pre><code>$ DYLD_LIBRARY_PATH=<insert path here>:$DYLD_LIBRARY_PATH python
</code></pre>
<p>... or create a wra... | 1 | 2009-07-03T23:21:24Z | [
"python",
"osx",
"tkinter"
] |
Callable modules | 1,060,796 | <p>Why doesn't Python allow modules to have a <code>__call__</code>? (Beyond the obvious that it wouldn't be easy to import directly.) Specifically, why doesn't using <code>a(b)</code> syntax find the <code>__call__</code> attribute like it does for functions, classes, and objects? (Is lookup just incompatibly differen... | 33 | 2009-06-29T22:01:09Z | 1,060,862 | <p>Special methods are only guaranteed to be called implicitly when they are defined on the type, not on the instance. (<code>__call__</code> is an attribute of the module instance <code>mod_call</code>, not of <code><type 'module'></code>.) You can't add methods to built-in types.</p>
<p><a href="http://docs.... | 24 | 2009-06-29T22:18:56Z | [
"python",
"module"
] |
Callable modules | 1,060,796 | <p>Why doesn't Python allow modules to have a <code>__call__</code>? (Beyond the obvious that it wouldn't be easy to import directly.) Specifically, why doesn't using <code>a(b)</code> syntax find the <code>__call__</code> attribute like it does for functions, classes, and objects? (Is lookup just incompatibly differen... | 33 | 2009-06-29T22:01:09Z | 1,060,872 | <p>Python doesn't allow modules to override or add <em>any</em> magic method, because keeping module objects simple, regular and lightweight is just too advantageous considering how rarely strong use cases appear where you could use magic methods there.</p>
<p>When such use cases <em>do</em> appear, the solution is to... | 62 | 2009-06-29T22:20:06Z | [
"python",
"module"
] |
__lt__ instead of __cmp__ | 1,061,283 | <p>Python 2.x has two ways to overload comparison operators, <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__cmp__"><code>__cmp__</code></a> or the "rich comparison operators" such as <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__lt__"><code>__lt__</code></a>. <strong>The ... | 73 | 2009-06-30T00:55:45Z | 1,061,323 | <p>This is covered by <a href="http://www.python.org/dev/peps/pep-0207/" rel="nofollow">PEP 207 - Rich Comparisons</a></p>
<p>Also, <code>__cmp__</code> goes away in python 3.0. ( Note that it is not present on <a href="http://docs.python.org/3.0/reference/datamodel.html" rel="nofollow">http://docs.python.org/3.0/ref... | 8 | 2009-06-30T01:13:02Z | [
"python",
"operator-overloading"
] |
__lt__ instead of __cmp__ | 1,061,283 | <p>Python 2.x has two ways to overload comparison operators, <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__cmp__"><code>__cmp__</code></a> or the "rich comparison operators" such as <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__lt__"><code>__lt__</code></a>. <strong>The ... | 73 | 2009-06-30T00:55:45Z | 1,061,350 | <p>Yep, it's easy to implement everything in terms of e.g. <code>__lt__</code> with a mixin class (or a metaclass, or a class decorator if your taste runs that way).</p>
<p>For example:</p>
<pre><code>class ComparableMixin:
def __eq__(self, other):
return not self<other and not other<self
def __ne__(sel... | 68 | 2009-06-30T01:28:10Z | [
"python",
"operator-overloading"
] |
__lt__ instead of __cmp__ | 1,061,283 | <p>Python 2.x has two ways to overload comparison operators, <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__cmp__"><code>__cmp__</code></a> or the "rich comparison operators" such as <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__lt__"><code>__lt__</code></a>. <strong>The ... | 73 | 2009-06-30T00:55:45Z | 7,150,594 | <p>I tried out the comparable mixin answer above. I ran into trouble with "None". Here is a modified version that handles equality comparisons with "None". (I saw no reason to bother with inequality comparisons with None as lacking semantics):</p>
<pre><code>
class ComparableMixin(object):
def __eq__(self, oth... | 2 | 2011-08-22T16:35:10Z | [
"python",
"operator-overloading"
] |
__lt__ instead of __cmp__ | 1,061,283 | <p>Python 2.x has two ways to overload comparison operators, <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__cmp__"><code>__cmp__</code></a> or the "rich comparison operators" such as <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__lt__"><code>__lt__</code></a>. <strong>The ... | 73 | 2009-06-30T00:55:45Z | 10,312,447 | <p>To simplify this case there's a class decorator in Python 2.7+/3.2+, <a href="http://docs.python.org/library/functools.html#functools.total_ordering">functools.total_ordering</a>, that can be used to implement what Alex suggests. Example from the docs:</p>
<pre><code>@total_ordering
class Student:
def __eq__(se... | 25 | 2012-04-25T09:01:11Z | [
"python",
"operator-overloading"
] |
__lt__ instead of __cmp__ | 1,061,283 | <p>Python 2.x has two ways to overload comparison operators, <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__cmp__"><code>__cmp__</code></a> or the "rich comparison operators" such as <a href="http://docs.python.org/2.6/reference/datamodel.html#object.__lt__"><code>__lt__</code></a>. <strong>The ... | 73 | 2009-06-30T00:55:45Z | 12,188,881 | <p>Inspired by Alex Martelli's <code>ComparableMixin</code> & <code>KeyedMixin</code> answers, I came up with the following mixin.
It allows you to implement a single <code>_compare_to()</code> method, which uses key-based comparisons
similar to <code>KeyedMixin</code>, but allows your class to pick the most effici... | 0 | 2012-08-30T01:52:08Z | [
"python",
"operator-overloading"
] |
Python-Hotshot error trying to profile a simple program | 1,061,361 | <p>I was trying to learn how to profile a simple python program using hotshot, but am facing a weird error,</p>
<pre><code>import sys
import hotshot
def main(argv):
for i in range(1,1000):
print i
if __name__ == "__main__":
prof = hotshot.Profile("hotshot_edi_stats")
b,c = prof.runcall(main(sys.argv))
pro... | 2 | 2009-06-30T01:40:02Z | 1,061,378 | <p>And I think I've figured out something I missed for over 2 hours.. </p>
<p>Turns out, runcall() should be called as,</p>
<pre><code>runcall(main, self.argv)
</code></pre>
<p>and this makes things work!</p>
| 3 | 2009-06-30T01:46:54Z | [
"python",
"profiler",
"profiling"
] |
Python-Hotshot error trying to profile a simple program | 1,061,361 | <p>I was trying to learn how to profile a simple python program using hotshot, but am facing a weird error,</p>
<pre><code>import sys
import hotshot
def main(argv):
for i in range(1,1000):
print i
if __name__ == "__main__":
prof = hotshot.Profile("hotshot_edi_stats")
b,c = prof.runcall(main(sys.argv))
pro... | 2 | 2009-06-30T01:40:02Z | 1,070,978 | <p>In general, if you have a way to randomly pause or interrupt the program and see the call stack, <a href="http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-code-in-linux/378024#378024">this method always works</a>.</p>
| 1 | 2009-07-01T19:46:59Z | [
"python",
"profiler",
"profiling"
] |
Why isn't this a valid schema for Rx? | 1,061,482 | <p>I'm using YAML as a configuration file format for a Python project.</p>
<p>Recently I found <a href="http://rjbs.manxome.org/rx/" rel="nofollow">Rx</a> to be the only schema validator available for Python and YAML. :-/ <a href="http://www.kuwata-lab.com/kwalify/" rel="nofollow">Kwalify</a> works with YAML, but it's... | 4 | 2009-06-30T02:36:15Z | 1,063,888 | <p>Try this:</p>
<pre><code>type: //map
values:
type: //rec
required:
exec: //str
optional:
aliases:
type: //arr
contents: //str
length: {min: 1, max: 10}
filter:
type: //rec
optional:
sms: //str
email: //str
all: //str
</code></pre>
<p>A map can... | 3 | 2009-06-30T14:06:17Z | [
"python",
"schema",
"yaml"
] |
print statement in for loop only executes once | 1,061,534 | <p>I am teaching myself python. I was thinking of small programs, and came up with an idea to do a keno number generator. For any who don't know, you can pick 4-12 numbers, ranged 1-80, to match. So the first is part asks how many numbers, the second generates them. I came up with</p>
<pre><code>x = raw_input('How man... | 0 | 2009-06-30T03:06:03Z | 1,061,540 | <p>This should do what you want:</p>
<pre><code>x = raw_input('How many numbers do you want to play?')
for i in xrange(int(x)):
print random.randrange(1,81)
</code></pre>
<p>In Python indentation matters. It is the way it knows when you're in a specific block of code. So basically we use the <code>xrange</code> fu... | 5 | 2009-06-30T03:07:55Z | [
"python"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,061,740 | <p>Sorry to say, your code is kind of long and not particularly readable. If you can try to summarize it somehow, maybe someone can help you write it more clearly.</p>
<p>As for the problem itself, my first thought would be to use recursion. (For all I know, you're already doing that. Sorry again for my inability to r... | 1 | 2009-06-30T04:32:51Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,061,744 | <p>Your algorithm seems pretty good at first blush, and I don't think OO or another language would improve the code. I can't say if recursion would have helped but I admire the non-recursive approach. I bet it was harder to get working and it's harder to read but it likely is more efficient and it's definitely quite cl... | 3 | 2009-06-30T04:34:45Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,061,814 | <p>Here's the simplest recursive solution that I can think of to "find all possible combinations of n numbers with values x such that 1 <= x <= max_val and x(1) + ... + x(n) = target". I'm developing it from scratch. Here's a version without any optimization at all, just for simplicity:</p>
<pre><code>def apcnx(... | 2 | 2009-06-30T04:58:43Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,061,899 | <p>First of all, I'd use variable names that mean something, so that the code gets comprehensible. Then, after I understood the problem, it's clearly a recursive problem, as once you have chosen one number, the question of finding the possible values for the rest of the squares are exactly the same problem, but with di... | 2 | 2009-06-30T05:37:53Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,062,037 | <p>First of all, I am learning Python myself so this solution won't be great but this is just an attempt at solving this. I have tried to solve it recursively and I think a recursive solution would be ideal for this kind of problem although THAT recursive solution might not be this one:</p>
<pre><code>def GetFactors(m... | 1 | 2009-06-30T06:31:16Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,062,168 | <p>Here a simple solution in C/C++:</p>
<pre><code>const int max = 6;
int sol[N_CELLS];
void enum_solutions(int target, int n, int min) {
if (target == 0 && n == 0)
report_solution(); /* sol[0]..sol[N_CELLS-1] is a solution */
if (target <= 0 || n == 0) return; /* nothing further to explore */
so... | 1 | 2009-06-30T07:10:49Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,062,810 | <p>Here is a naive, but succinct, solution using generators:</p>
<pre><code>def descending(v):
"""Decide if a square contains values in descending order"""
return list(reversed(v)) == sorted(v)
def latinSquares(max_val, target_sum, n_cells):
"""Return all descending n_cells-dimensional squares,
no cell lar... | 1 | 2009-06-30T09:58:23Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,062,908 | <p>And here is another recursive, generator-based solution, but this time using some simple math to calculate ranges at each step, avoiding needless recursion:</p>
<pre><code>def latinSquares(max_val, target_sum, n_cells):
if n_cells == 1:
assert(max_val >= target_sum >= 1)
return ((target_sum,),)
el... | 1 | 2009-06-30T10:23:57Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm | 1,061,590 | <p>This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to... | 4 | 2009-06-30T03:33:36Z | 1,063,557 | <p>Little bit offtopic, but still might help at programming kenken.</p>
<p>I got good results using DLX algorhitm for solving Killer Sudoku (very simmilar as KenKen it has cages, but only sums). It took less than second for most of problems and it was implemented in MATLAB language. </p>
<p>reference this forum
<a hr... | 1 | 2009-06-30T13:03:02Z | [
"python",
"algorithm",
"statistics",
"puzzle",
"combinations"
] |
Adding Cookie to SOAPpy Request | 1,061,690 | <p>I'm trying to send a SOAP request using SOAPpy as the client. I've found some documentation stating how to add a cookie by extending SOAPpy.HTTPTransport, but I can't seem to get it to work.</p>
<p>I tried to use the example <a href="http://code.activestate.com/recipes/444758/" rel="nofollow">here</a>,
but the serv... | 0 | 2009-06-30T04:13:21Z | 1,061,806 | <p>Error 415 is because of incorrect content-type header.</p>
<p>Install httpfox for firefox or whatever tool (wireshark, Charles or Fiddler) to track what headers are you sending. Try Content-Type: application/xml.</p>
<pre><code>...
t = 'application/xml';
if encoding != None:
t += '; charset="%s"' % encoding
...
... | 0 | 2009-06-30T04:56:46Z | [
"python",
"web-services",
"soap",
"soappy"
] |
Adding Cookie to SOAPpy Request | 1,061,690 | <p>I'm trying to send a SOAP request using SOAPpy as the client. I've found some documentation stating how to add a cookie by extending SOAPpy.HTTPTransport, but I can't seem to get it to work.</p>
<p>I tried to use the example <a href="http://code.activestate.com/recipes/444758/" rel="nofollow">here</a>,
but the serv... | 0 | 2009-06-30T04:13:21Z | 16,461,253 | <p>A nice hack to use cookies with SOAPpy calls
<a href="http://code.activestate.com/recipes/444758-how-to-add-cookiesheaders-to-soappy-calls/" rel="nofollow">Using Cookies with SOAPpy calls</a></p>
| 0 | 2013-05-09T11:58:43Z | [
"python",
"web-services",
"soap",
"soappy"
] |
What's the easiest way to escape HTML in Python? | 1,061,697 | <p>cgi.escape seems like one possible choice. Does it work well? Is there something that is considered better?</p>
| 93 | 2009-06-30T04:15:54Z | 1,061,702 | <p><a href="http://docs.python.org/library/cgi.html#cgi.escape"><code>cgi.escape</code></a> is fine. It escapes:</p>
<ul>
<li><code><</code> to <code>&lt;</code></li>
<li><code>></code> to <code>&gt;</code></li>
<li><code>&</code> to <code>&amp;</code></li>
</ul>
<p>That is enough for all HTML.<... | 133 | 2009-06-30T04:18:11Z | [
"python",
"html"
] |
What's the easiest way to escape HTML in Python? | 1,061,697 | <p>cgi.escape seems like one possible choice. Does it work well? Is there something that is considered better?</p>
| 93 | 2009-06-30T04:15:54Z | 1,061,747 | <p><a href="https://docs.python.org/2/library/cgi.html#cgi.escape" rel="nofollow"><code>cgi.escape</code></a> should be good to escape HTML in the limited sense of escaping the HTML tags and character entities.</p>
<p>But you might have to also consider encoding issues: if the HTML you want to quote has non-ASCII char... | 7 | 2009-06-30T04:35:26Z | [
"python",
"html"
] |
What's the easiest way to escape HTML in Python? | 1,061,697 | <p>cgi.escape seems like one possible choice. Does it work well? Is there something that is considered better?</p>
| 93 | 2009-06-30T04:15:54Z | 5,072,031 | <p>In Python 3.2 a new <code>html</code> module was introduced, which is used for escaping reserved characters from HTML markup.</p>
<p>It has one function <code>escape()</code>:</p>
<pre><code>>>> import html
>>> html.escape('x > 2 && x < 7')
'x &gt; 2 &amp;&amp; x &lt... | 55 | 2011-02-21T22:31:07Z | [
"python",
"html"
] |
What's the easiest way to escape HTML in Python? | 1,061,697 | <p>cgi.escape seems like one possible choice. Does it work well? Is there something that is considered better?</p>
| 93 | 2009-06-30T04:15:54Z | 18,094,938 | <p><strong>If you wish to escape HTML in a URL:</strong></p>
<p>This is probably NOT what the OP wanted (the question doesn't clearly indicate in which context the escaping is meant to be used), but Python's native library <a href="http://docs.python.org/2/library/urllib.html">urllib</a> has a method to escape HTML en... | 4 | 2013-08-07T04:53:27Z | [
"python",
"html"
] |
What's the easiest way to escape HTML in Python? | 1,061,697 | <p>cgi.escape seems like one possible choice. Does it work well? Is there something that is considered better?</p>
| 93 | 2009-06-30T04:15:54Z | 28,511,611 | <h2><code>cgi.escape</code> extended</h2>
<p>This version improves <code>cgi.escape</code>. It also preserves whitespace and newlines. Returns a <code>unicode</code> string.</p>
<pre><code>def escape_html(text):
"""escape strings for display in HTML"""
return cgi.escape(text, quote=True).\
replac... | 1 | 2015-02-14T01:55:32Z | [
"python",
"html"
] |
What's the easiest way to escape HTML in Python? | 1,061,697 | <p>cgi.escape seems like one possible choice. Does it work well? Is there something that is considered better?</p>
| 93 | 2009-06-30T04:15:54Z | 28,827,075 | <p>Via <a href="http://www.crummy.com/software/BeautifulSoup/bs4/doc/" rel="nofollow">BeautifulSoup4</a>:</p>
<pre><code>>>> bs4.dammit import EntitySubstitution
>>> esub = EntitySubstitution()
>>> esub.substitute_html("r&d")
'r&amp;d'
</code></pre>
| 0 | 2015-03-03T08:22:47Z | [
"python",
"html"
] |
What's the easiest way to escape HTML in Python? | 1,061,697 | <p>cgi.escape seems like one possible choice. Does it work well? Is there something that is considered better?</p>
| 93 | 2009-06-30T04:15:54Z | 32,446,224 | <p>Not the easiest way, but still straightforward. The main difference from <strong>cgi.escape</strong> module - it still will work properly if you already have <code>&amp;</code> in your text. As you see from comments to it:</p>
<p><em>cgi.escape version</em></p>
<pre><code>def escape(s, quote=None):
'''Repl... | 0 | 2015-09-07T21:25:18Z | [
"python",
"html"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.