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
Sub-classing float type in Python, fails to catch exception in __init__()
1,936,457
<p>On Python 2.5 I need to use float numbers with a modified <code>__str__()</code> method. Also I need to know when the constructor fails.</p> <p>Why I can't catch exceptions raised from <code>float.__init__()</code>?</p> <p>What is the best way to consult the numeric value of my derived float object? In my code I'm...
9
2009-12-20T17:51:35Z
1,936,463
<p>try <code>__new__</code> instead:</p> <pre><code>class F(float): def __new__(cls, *arg, **kw): try: return float.__new__(cls, *arg, **kw) except ValueError: raise Exception("foo") print F("3.5") print F("asdf") </code></pre> <p>Also "self" is a float already...
7
2009-12-20T17:54:54Z
[ "python", "exception-handling" ]
Sub-classing float type in Python, fails to catch exception in __init__()
1,936,457
<p>On Python 2.5 I need to use float numbers with a modified <code>__str__()</code> method. Also I need to know when the constructor fails.</p> <p>Why I can't catch exceptions raised from <code>float.__init__()</code>?</p> <p>What is the best way to consult the numeric value of my derived float object? In my code I'm...
9
2009-12-20T17:51:35Z
1,936,487
<p><code>float</code> is immutable, therefore its <code>__init__</code>, the <strong>initializer</strong>, is basically a no-op -- nothing substantial can happen there, because the <code>self</code> object cannot be altered (if it's actually an instance of <code>float</code> rather than of a subclass -- but of course <...
17
2009-12-20T18:02:40Z
[ "python", "exception-handling" ]
BeautifulSoup Grab Visible Webpage Text
1,936,466
<p>Basically, I want to use BeautifulSoup to grab strictly the <em>visible text</em> on a webpage. For instance, <a href="http://www.nytimes.com/2009/12/21/us/21storm.html">this webpage</a> is my test case. And I mainly want to just get the body text (article) and maybe even a few tab names here and there. I have tried...
71
2009-12-20T17:55:58Z
1,936,568
<p>The title is inside an <code>&lt;nyt_headline&gt;</code> tag, which is nested inside an <code>&lt;h1&gt;</code> tag and a <code>&lt;div&gt;</code> tag with id "article". </p> <pre><code>soup.findAll('nyt_headline', limit=1) </code></pre> <p>Should work.</p> <p>The article body is inside an <code>&lt;nyt_text&gt;...
0
2009-12-20T18:40:54Z
[ "python", "text", "beautifulsoup", "html-content-extraction" ]
BeautifulSoup Grab Visible Webpage Text
1,936,466
<p>Basically, I want to use BeautifulSoup to grab strictly the <em>visible text</em> on a webpage. For instance, <a href="http://www.nytimes.com/2009/12/21/us/21storm.html">this webpage</a> is my test case. And I mainly want to just get the body text (article) and maybe even a few tab names here and there. I have tried...
71
2009-12-20T17:55:58Z
1,983,219
<p>Try this:</p> <pre><code>html = urllib.urlopen('http://www.nytimes.com/2009/12/21/us/21storm.html').read() soup = BeautifulSoup(html, 'html.parser') texts = soup.findAll(text=True) def visible(element): if element.parent.name in ['style', 'script', '[document]', 'head', 'title']: return False elif ...
106
2009-12-31T00:06:12Z
[ "python", "text", "beautifulsoup", "html-content-extraction" ]
BeautifulSoup Grab Visible Webpage Text
1,936,466
<p>Basically, I want to use BeautifulSoup to grab strictly the <em>visible text</em> on a webpage. For instance, <a href="http://www.nytimes.com/2009/12/21/us/21storm.html">this webpage</a> is my test case. And I mainly want to just get the body text (article) and maybe even a few tab names here and there. I have tried...
71
2009-12-20T17:55:58Z
19,760,007
<p>The approved answer from @jbochi does not work for me. The str() function call raises an exception because it cannot encode the non-ascii characters in the BeautifulSoup element. Here is a more succinct way to filter the example web page to visible text.</p> <pre><code>html = open('21storm.html').read() soup = Be...
17
2013-11-04T00:35:55Z
[ "python", "text", "beautifulsoup", "html-content-extraction" ]
BeautifulSoup Grab Visible Webpage Text
1,936,466
<p>Basically, I want to use BeautifulSoup to grab strictly the <em>visible text</em> on a webpage. For instance, <a href="http://www.nytimes.com/2009/12/21/us/21storm.html">this webpage</a> is my test case. And I mainly want to just get the body text (article) and maybe even a few tab names here and there. I have tried...
71
2009-12-20T17:55:58Z
19,797,373
<p>I completely respect using Beautiful Soup to get rendered content, but it may not be the ideal package for acquiring the rendered content on a page.</p> <p>I had a similar problem to get rendered content, or the visible content in a typical browser. In particular I had many perhaps atypical cases to work with such...
6
2013-11-05T19:37:08Z
[ "python", "text", "beautifulsoup", "html-content-extraction" ]
BeautifulSoup Grab Visible Webpage Text
1,936,466
<p>Basically, I want to use BeautifulSoup to grab strictly the <em>visible text</em> on a webpage. For instance, <a href="http://www.nytimes.com/2009/12/21/us/21storm.html">this webpage</a> is my test case. And I mainly want to just get the body text (article) and maybe even a few tab names here and there. I have tried...
71
2009-12-20T17:55:58Z
24,968,429
<pre><code>import urllib from bs4 import BeautifulSoup url = "https://www.yahoo.com" html = urllib.urlopen(url).read() soup = BeautifulSoup(html) # kill all script and style elements for script in soup(["script", "style"]): script.extract() # rip it out # get text text = soup.get_text() # break into lines an...
10
2014-07-26T06:54:26Z
[ "python", "text", "beautifulsoup", "html-content-extraction" ]
BeautifulSoup Grab Visible Webpage Text
1,936,466
<p>Basically, I want to use BeautifulSoup to grab strictly the <em>visible text</em> on a webpage. For instance, <a href="http://www.nytimes.com/2009/12/21/us/21storm.html">this webpage</a> is my test case. And I mainly want to just get the body text (article) and maybe even a few tab names here and there. I have tried...
71
2009-12-20T17:55:58Z
30,019,131
<p>While, i would completely suggest using beautiful-soup in general, if anyone is looking to display the visible parts of a malformed html (e.g. where you have just a segment or line of a web-page) for whatever-reason, the the following will remove content between <code>&lt;</code> and <code>&gt;</code> tags:</p> <pr...
1
2015-05-03T20:39:31Z
[ "python", "text", "beautifulsoup", "html-content-extraction" ]
Why does this code behave differently in Python3.1 than in Python2.6?
1,936,725
<p>I'm very new to programming so I apologize in advance if my question is too silly. </p> <pre><code>#!/usr/bin/python2.6 import subprocess, time p=subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) for i in 'abcd': p.stdin.write(str.encode(i+'\n')) output=p.stdout.readline() ...
5
2009-12-20T19:34:26Z
1,937,104
<p>Appears to be a difference in buffering. Adding a <code>p.stdin.flush()</code> call solved the problem. (See the comments above).</p> <p>Community wiki as I deserve no credits for this answer, but some answer needs to be marked accepted.</p> <p>[@Geo Pop: Please "accept" this question, as it apparently is correct....
3
2009-12-20T21:24:00Z
[ "python", "subprocess", "stdout", "stdin", "popen" ]
How get sound input from microphone in python, and process it on the fly?
1,936,828
<p>Greetings,</p> <p>I'm trying to write a program in Python which would print a string every time it gets a tap in the microphone. When I say 'tap', I mean a loud sudden noise or something similar.</p> <p>I searched in SO and found this post: <a href="http://stackoverflow.com/questions/1797631/recognising-tone-of-th...
32
2009-12-20T20:01:28Z
1,937,058
<p>If you are using LINUX, you can use <a href="http://larsimmisch.github.io/pyalsaaudio/" rel="nofollow">pyALSAAUDIO</a>. For windows, we have <a href="http://people.csail.mit.edu/hubert/pyaudio/" rel="nofollow">PyAudio</a> and there is also a library called <a href="http://code.google.com/p/pygalaxy/wiki/SoundAnalys...
25
2009-12-20T21:10:42Z
[ "python", "microphone" ]
How get sound input from microphone in python, and process it on the fly?
1,936,828
<p>Greetings,</p> <p>I'm trying to write a program in Python which would print a string every time it gets a tap in the microphone. When I say 'tap', I mean a loud sudden noise or something similar.</p> <p>I searched in SO and found this post: <a href="http://stackoverflow.com/questions/1797631/recognising-tone-of-th...
32
2009-12-20T20:01:28Z
1,937,509
<blockquote> <p>...and when I got one how to process it (do I need to use Fourier Transform like it was instructed in the above post)?</p> </blockquote> <p>If you want a "tap" then I think you are interested in amplitude more than frequency. So Fourier transforms probably aren't useful for your particular goal. You ...
5
2009-12-20T23:42:20Z
[ "python", "microphone" ]
Regular Expression to split on specific character ONLY if that character is not in a pair
1,936,837
<p>After finding the fastest string replace algorithm in <a href="http://stackoverflow.com/questions/1919096/mass-string-replace-in-python">this thread</a>, I've been trying to modify one of them to suit my needs, particularly <a href="http://stackoverflow.com/questions/1919096/mass-string-replace-in-python/1919221#191...
1
2009-12-20T20:05:24Z
1,936,888
<p>You could use a negative lookbehind (assuming the regex engine in question supports it) to only match ampersands that do not follow another ampersand.</p> <pre><code>/(?&lt;!&amp;)&amp;/ </code></pre>
2
2009-12-20T20:20:51Z
[ "python", "regex", "string", "replace" ]
Regular Expression to split on specific character ONLY if that character is not in a pair
1,936,837
<p>After finding the fastest string replace algorithm in <a href="http://stackoverflow.com/questions/1919096/mass-string-replace-in-python">this thread</a>, I've been trying to modify one of them to suit my needs, particularly <a href="http://stackoverflow.com/questions/1919096/mass-string-replace-in-python/1919221#191...
1
2009-12-20T20:05:24Z
1,936,891
<p>Maybe loop while (q = str.find('&amp;', p)) != -1, then append the left side (p + 2 to q - 1) and the replacement value.</p>
0
2009-12-20T20:21:05Z
[ "python", "regex", "string", "replace" ]
Regular Expression to split on specific character ONLY if that character is not in a pair
1,936,837
<p>After finding the fastest string replace algorithm in <a href="http://stackoverflow.com/questions/1919096/mass-string-replace-in-python">this thread</a>, I've been trying to modify one of them to suit my needs, particularly <a href="http://stackoverflow.com/questions/1919096/mass-string-replace-in-python/1919221#191...
1
2009-12-20T20:05:24Z
1,936,939
<p>I think this does the trick:</p> <pre><code>import re def fix(text): dict = {"y":"\033[0;30m", "c":"\033[0;31m", "b":"\033[0;32m", "Y":"\033[0;33m", "u":"\033[0;34m", "&amp;":"&amp;"} myparts = re.split('\&amp;(\&amp;*)', text) myparts[1:]=[d...
0
2009-12-20T20:33:52Z
[ "python", "regex", "string", "replace" ]
Regular Expression to split on specific character ONLY if that character is not in a pair
1,936,837
<p>After finding the fastest string replace algorithm in <a href="http://stackoverflow.com/questions/1919096/mass-string-replace-in-python">this thread</a>, I've been trying to modify one of them to suit my needs, particularly <a href="http://stackoverflow.com/questions/1919096/mass-string-replace-in-python/1919221#191...
1
2009-12-20T20:05:24Z
1,937,602
<p>re.sub will do what you want. It takes a regex pattern and can take a function to process the match and return the replacement. Below if the character following the &amp; is not in the dictionary, no replacement is made. &amp;&amp; is replaced with &amp; to allow escaping an &amp; that is followed by a character ...
0
2009-12-21T00:19:38Z
[ "python", "regex", "string", "replace" ]
PyQt 4 UI freezes
1,936,868
<p>The following programm should just count up and int and displays its value in a label. But after a while the GUI stops working, while the loop continous.</p> <pre><code>from PyQt4 import QtGui,QtCore import sys class main_window(QtGui.QWidget): def __init__(self,parent=None): #Layout QtG...
3
2009-12-20T20:15:40Z
1,936,894
<pre><code>def count(self): self.running=True while self.running: self.cnt+=1 print self.cnt self.lbl.setText(str(self.cnt)) self.repaint() </code></pre> <p>Have you thought about any exit from this endless loop? E.g. <code>self.running=False</code>.<br> GUI may stop working bec...
4
2009-12-20T20:21:20Z
[ "python", "qt", "pyqt", "pyqt4" ]
PyQt 4 UI freezes
1,936,868
<p>The following programm should just count up and int and displays its value in a label. But after a while the GUI stops working, while the loop continous.</p> <pre><code>from PyQt4 import QtGui,QtCore import sys class main_window(QtGui.QWidget): def __init__(self,parent=None): #Layout QtG...
3
2009-12-20T20:15:40Z
1,936,906
<p>You have to let the main event loop run, something you're not doing.</p>
2
2009-12-20T20:25:54Z
[ "python", "qt", "pyqt", "pyqt4" ]
PyQt 4 UI freezes
1,936,868
<p>The following programm should just count up and int and displays its value in a label. But after a while the GUI stops working, while the loop continous.</p> <pre><code>from PyQt4 import QtGui,QtCore import sys class main_window(QtGui.QWidget): def __init__(self,parent=None): #Layout QtG...
3
2009-12-20T20:15:40Z
1,937,665
<p>You're not letting Qt's event loop run, so the GUI is not responding. Also, <code>repaint()</code> is not needed, the <code>QLabel.setText()</code> will repaint the label. All it does is queue up an extra paint event, but this never gets processed. </p> <p>What you need to do is replace <code>self.repaint()</code> ...
5
2009-12-21T00:49:20Z
[ "python", "qt", "pyqt", "pyqt4" ]
Interactive Python script output stored in some file
1,936,996
<p>How do I perform logging of all activities that are done by a Python script and all scripts that are called from it?</p> <p>I had several Bash scripts but now wrote a Python script which call all of these Bash scripts. I would like to have all output produced from these scripts stored in some file.</p> <p>The scri...
1
2009-12-20T20:47:26Z
1,937,002
<p>Here's the Python logging module documentation: <a href="http://docs.python.org/library/logging.html" rel="nofollow">http://docs.python.org/library/logging.html</a>.</p>
0
2009-12-20T20:49:27Z
[ "python", "logging" ]
Interactive Python script output stored in some file
1,936,996
<p>How do I perform logging of all activities that are done by a Python script and all scripts that are called from it?</p> <p>I had several Bash scripts but now wrote a Python script which call all of these Bash scripts. I would like to have all output produced from these scripts stored in some file.</p> <p>The scri...
1
2009-12-20T20:47:26Z
1,937,010
<p>Python has a tracing module: <a href="http://docs.python.org/library/trace.html" rel="nofollow">trace</a>. Usage: <code>python -m trace --trace file.py</code></p>
1
2009-12-20T20:51:53Z
[ "python", "logging" ]
Interactive Python script output stored in some file
1,936,996
<p>How do I perform logging of all activities that are done by a Python script and all scripts that are called from it?</p> <p>I had several Bash scripts but now wrote a Python script which call all of these Bash scripts. I would like to have all output produced from these scripts stored in some file.</p> <p>The scri...
1
2009-12-20T20:47:26Z
1,937,055
<p>If you want to capture the output of any script, then on a *nix-y system you can redirect stdout and stderr to a file:</p> <pre><code>./script.py &gt;&gt; /tmp/outputs.txt 2&gt;&gt; /tmp/outputs.txt </code></pre> <p>If you want <em>everything</em> done by the scripts, not just what they print, then the python trac...
1
2009-12-20T21:10:07Z
[ "python", "logging" ]
Interactive Python script output stored in some file
1,936,996
<p>How do I perform logging of all activities that are done by a Python script and all scripts that are called from it?</p> <p>I had several Bash scripts but now wrote a Python script which call all of these Bash scripts. I would like to have all output produced from these scripts stored in some file.</p> <p>The scri...
1
2009-12-20T20:47:26Z
1,937,063
<p>Making Python's own <code>print</code>s go to both the terminal and a file is not hard:</p> <pre><code>&gt;&gt;&gt; import sys &gt;&gt;&gt; class tee(object): ... def __init__(self, fn='/tmp/foo.txt'): ... self.o = sys.stdout ... self.f = open(fn, 'w') ... def write(self, s): ... self.o.write(s) ......
6
2009-12-20T21:13:47Z
[ "python", "logging" ]
Python Service File Caching Apache Race Condition
1,937,018
<p>I am writing a python service (pyamf) through which a user can access images. All images are stored on a central server. The python services will be running on satellite machines which have network access to server. The service should work as follows:</p> <ol> <li>check locally to see if the file exists, if so, u...
1
2009-12-20T20:53:44Z
1,937,241
<p>I'm guessing its either a threaded or a forked apache, but the effect would be the same since they are accessing a remote resource.</p> <p>This problem is sometimes called the "dog pile" problem and its one of the issues addressed by the Beaker caching library (<a href="http://beaker.groovie.org" rel="nofollow">htt...
1
2009-12-20T22:06:35Z
[ "python", "apache", "service", "race-condition" ]
exposing std::vector<double> with boost.python
1,937,135
<p>I have written some C++ code that generates a std::vector.</p> <p>I also have a python script that manipulates some data that, for now, I am declaring like this (below). </p> <pre><code>import numpy x = numpy.random.randn(1000) y = numpy.random.randn(1000) </code></pre> <p>I can run the script fine. From my C++ ...
4
2009-12-20T21:31:21Z
1,939,016
<p>I'm not sure if I understand correctly. After exporting your class "PyVec" which can hold <code>std::vector&lt;double&gt;</code>, you can export any c++ function taking vector as input or return type. So of course you can populate your vector within c++ and access this data in Python with the interfaced type "PyVec"...
2
2009-12-21T09:08:40Z
[ "c++", "python", "boost-python" ]
exposing std::vector<double> with boost.python
1,937,135
<p>I have written some C++ code that generates a std::vector.</p> <p>I also have a python script that manipulates some data that, for now, I am declaring like this (below). </p> <pre><code>import numpy x = numpy.random.randn(1000) y = numpy.random.randn(1000) </code></pre> <p>I can run the script fine. From my C++ ...
4
2009-12-20T21:31:21Z
2,044,860
<p>The following code works for me (Python 2.6, Boost 1.39). This is almost the same as your code, except without the <code>BOOST_PYTHON_MODULE</code> line itself (but with the <code>class_</code> definition for the vector). <code>BOOST_PYTHON_MODULE</code> only needs to be used when creating extension modules.</p> <p...
11
2010-01-11T20:55:54Z
[ "c++", "python", "boost-python" ]
Word game server in Python, design pros and cons?
1,937,286
<p>I'd like to get busy with a winter programming project and am contemplating writing an online word game (with a server load of up to, say, 500 users simultaneously). I would prefer it to be platform independent. I intend to use Python, which I have some experience with. For user data storage, after previous experien...
2
2009-12-20T22:18:29Z
1,937,304
<p>A project of this kind could be a great way of investigating a new language. I'd say that Erlang is one of the more interesting languages out there, (1) being functional, (2) offering a superb concurrency / (distributed) parallelism paradigm, (2) seeing use in the industry (most notably &amp; traditionally telecommu...
0
2009-12-20T22:25:34Z
[ "python" ]
Word game server in Python, design pros and cons?
1,937,286
<p>I'd like to get busy with a winter programming project and am contemplating writing an online word game (with a server load of up to, say, 500 users simultaneously). I would prefer it to be platform independent. I intend to use Python, which I have some experience with. For user data storage, after previous experien...
2
2009-12-20T22:18:29Z
1,937,324
<p>If you're already fairly familiar with Python, then I'd investigate the <a href="http://twistedmatrix.com" rel="nofollow">Twisted</a> library if I were you. Twisted is an asynchronous comms library that was originally developed to support a large text-based game.</p> <p>The current level of support for Python 3 by ...
0
2009-12-20T22:30:39Z
[ "python" ]
Word game server in Python, design pros and cons?
1,937,286
<p>I'd like to get busy with a winter programming project and am contemplating writing an online word game (with a server load of up to, say, 500 users simultaneously). I would prefer it to be platform independent. I intend to use Python, which I have some experience with. For user data storage, after previous experien...
2
2009-12-20T22:18:29Z
1,937,330
<p>I would go for Python + <a href="http://www.djangoproject.com/" rel="nofollow">Django</a>. It makes web application developments pretty easy.</p>
2
2009-12-20T22:32:29Z
[ "python" ]
Word game server in Python, design pros and cons?
1,937,286
<p>I'd like to get busy with a winter programming project and am contemplating writing an online word game (with a server load of up to, say, 500 users simultaneously). I would prefer it to be platform independent. I intend to use Python, which I have some experience with. For user data storage, after previous experien...
2
2009-12-20T22:18:29Z
1,937,342
<blockquote> <p>Is it worth starting with Python 3, or is it still too poorly supported with ports of modules from previous versions?</p> </blockquote> <p>depends on which modules do you want to use. twisted is a "swiss knife" for the network programming and could be a choice for your project but unfortunately it do...
2
2009-12-20T22:37:10Z
[ "python" ]
Word game server in Python, design pros and cons?
1,937,286
<p>I'd like to get busy with a winter programming project and am contemplating writing an online word game (with a server load of up to, say, 500 users simultaneously). I would prefer it to be platform independent. I intend to use Python, which I have some experience with. For user data storage, after previous experien...
2
2009-12-20T22:18:29Z
1,937,370
<p>Related to your database choice, I'd seriously look at using Postgres instead of MySQL. In my experiance with the two Postgres has shown to be faster on most write operations while MySQL is slightly faster on the reads. </p> <p>However, MySQL also has many issues some of which are:</p> <ul> <li>Live backups are ...
1
2009-12-20T22:46:30Z
[ "python" ]
python libxml2dom xpath question
1,937,477
<p>quick question... i can create/parse a chunk of html using libxml2dom, etc...</p> <p>however, is there a way to somehow display the xpath used to generate/extract the html chunk.. i'm assuming that there's some method/way of doing this that i can't find..</p> <p>ex:</p> <pre><code>import libxml2dom d = libxml2dom...
1
2009-12-20T23:32:14Z
2,161,682
<p>I did this by iterating each node and comparing the textContent with my expected text. For fuzzy comparisons I used the SequenceMatcher class from <a href="http://docs.python.org/library/difflib.html" rel="nofollow">difflib</a>.</p>
0
2010-01-29T11:51:35Z
[ "python", "libxml2" ]
How to iterate over a string using a buffer (python)
1,937,519
<p>I'm trying to find some code that, given a string, will allow me to iterate over each line using the for loop construct, but with the added requirement that separate for loop constructs will not reset the iteration back to the beginning.</p> <p>At the moment I have</p> <pre><code>sList = [line for line in theStrin...
2
2009-12-20T23:46:44Z
1,937,542
<p>Try using a combination of slices and <a href="http://docs.python.org/library/functions.html#enumerate" rel="nofollow"><code>enumerate()</code></a>: </p> <pre><code>sList = theString.split(os.linesep) for i, line in enumerate(sList): if foo: break for j, line in enumerate(sList[i:]): # do more stu...
0
2009-12-20T23:56:06Z
[ "python", "string", "iteration", "buffer" ]
How to iterate over a string using a buffer (python)
1,937,519
<p>I'm trying to find some code that, given a string, will allow me to iterate over each line using the for loop construct, but with the added requirement that separate for loop constructs will not reset the iteration back to the beginning.</p> <p>At the moment I have</p> <pre><code>sList = [line for line in theStrin...
2
2009-12-20T23:46:44Z
1,937,545
<p>Just use a generator expression (genexp) instead of the list comprehension (listcomp) you're now using - i.e.:</p> <pre><code>sList = (line for line in theString.split(os.linesep)) </code></pre> <p>that's all -- if you're otherwise happy with your code (splitting by os.linesep, even though normal text I/O in Pytho...
12
2009-12-20T23:56:52Z
[ "python", "string", "iteration", "buffer" ]
How to iterate over a string using a buffer (python)
1,937,519
<p>I'm trying to find some code that, given a string, will allow me to iterate over each line using the for loop construct, but with the added requirement that separate for loop constructs will not reset the iteration back to the beginning.</p> <p>At the moment I have</p> <pre><code>sList = [line for line in theStrin...
2
2009-12-20T23:46:44Z
1,937,546
<p>Hack at an iterator?</p> <pre><code>def iterOverList(someList): for i in someList: # Do some stuff yield </code></pre> <p>Then just call iterOverList() within a loop a few times, it'll retain state?</p>
0
2009-12-20T23:56:59Z
[ "python", "string", "iteration", "buffer" ]
How to iterate over a string using a buffer (python)
1,937,519
<p>I'm trying to find some code that, given a string, will allow me to iterate over each line using the for loop construct, but with the added requirement that separate for loop constructs will not reset the iteration back to the beginning.</p> <p>At the moment I have</p> <pre><code>sList = [line for line in theStrin...
2
2009-12-20T23:46:44Z
1,937,556
<p>Use another iterator:</p> <pre><code>aList = range(10) anIterator = iter(aList) for item in anIterator: print item if item &gt; 4: break for item in anIterator: print item </code></pre>
2
2009-12-21T00:01:29Z
[ "python", "string", "iteration", "buffer" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
1,937,629
<p>There are several ways, although I do believe the one you mention (and dislike) is the most readable one.</p> <pre><code>&gt;&gt;&gt; t=datetime.date.today() &gt;&gt;&gt; datetime.datetime.fromordinal(t.toordinal()) datetime.datetime(2009, 12, 20, 0, 0) &gt;&gt;&gt; datetime.datetime(t.year, t.month, t.day) datetim...
68
2009-12-21T00:30:24Z
[ "python", "datetime", "date" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
1,937,631
<p>You can use the timetuple() method and varargs.</p> <pre><code>datetime.datetime(*(d.timetuple()[:6])) </code></pre>
30
2009-12-21T00:31:30Z
[ "python", "datetime", "date" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
1,937,636
<p>You can use <a href="https://docs.python.org/2/library/datetime.html#datetime.datetime.combine">datetime.combine</a>(date, time); for the time, you create a <code>datetime.time</code> object initialized to midnight.</p> <pre><code>from datetime import date from datetime import datetime d = date.today() datetime.com...
402
2009-12-21T00:33:40Z
[ "python", "datetime", "date" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
4,472,626
<p>If you need something quick, <code>datetime_object.date()</code> gives you a date of a datetime object.</p>
-4
2010-12-17T16:32:33Z
[ "python", "datetime", "date" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
14,653,296
<p>I am a newbie to Python. But this code worked for me which converts the specified input I provide to datetime. Here's the code. Correct me if I'm wrong.</p> <pre><code>import sys from datetime import datetime from time import mktime, strptime user_date = '02/15/1989' if user_date is not None: user_date = date...
-6
2013-02-01T19:04:58Z
[ "python", "datetime", "date" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
22,514,625
<p>The accepted answer is correct, but I would prefer to avoid using <code>datetime.min.time()</code> because it's not obvious to me exactly what it does. If it's obvious to you, then more power to you. I also feel the same way about the <code>timetuple</code> method and the reliance on the ordering.</p> <p>In my opin...
23
2014-03-19T18:12:22Z
[ "python", "datetime", "date" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
27,760,382
<p>One way to convert from date to datetime that hasn't been mentioned yet:</p> <pre><code>from datetime import date, datetime d = date.today() datetime.strptime(d.strftime('%Y%m%d'), '%Y%m%d') </code></pre>
1
2015-01-03T22:50:17Z
[ "python", "datetime", "date" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
30,143,983
<p>You can use <a href="https://github.com/ralphavalon/easy_date" rel="nofollow">easy_date</a> to make it easy:</p> <pre><code>import date_converter my_datetime = date_converter.date_to_datetime(my_date) </code></pre>
3
2015-05-09T19:11:49Z
[ "python", "datetime", "date" ]
Convert date to datetime in Python
1,937,622
<p>Silly question, but is there a built-in method for converting a <code>date</code> to a <code>datetime</code> in Python, ie. getting the <code>datetime</code> for the midnight of the <code>date</code>? The opposite conversion is easy - <code>datetime</code> has a <code>.date()</code> method. Do I really have to manua...
329
2009-12-21T00:26:01Z
34,576,632
<p>Today being 2016, I think the cleanest solution is provided by pandas Timestamp:</p> <pre><code>from datetime import date import pandas as pd d = date.today() pd.Timestamp(d) </code></pre> <p>Timestamp is the pandas equivalent of datetime and is interchangable with it in most cases. Check:</p> <pre><code>from dat...
3
2016-01-03T12:41:47Z
[ "python", "datetime", "date" ]
python django string rendering issue
1,937,682
<p>I'm trying to render a string into a javascript ( which usually works fine for me ) here's my code</p> <p>HTML:</p> <pre><code>THE USER NAME IS : {{name}} has added app {{has_added_app}} </code></pre> <p>JAVA SCRIPT: </p> <pre><code>&lt;script&gt; &lt;!-- var userName = {{name}} </code></pre> <p>The html...
1
2009-12-21T00:59:10Z
1,937,695
<p>comments for the javascript:</p> <pre><code>var userName = "{{name}}"; </code></pre>
1
2009-12-21T01:04:03Z
[ "python", "django" ]
python django string rendering issue
1,937,682
<p>I'm trying to render a string into a javascript ( which usually works fine for me ) here's my code</p> <p>HTML:</p> <pre><code>THE USER NAME IS : {{name}} has added app {{has_added_app}} </code></pre> <p>JAVA SCRIPT: </p> <pre><code>&lt;script&gt; &lt;!-- var userName = {{name}} </code></pre> <p>The html...
1
2009-12-21T00:59:10Z
1,937,846
<p>Remember that Django templates are purely textual: they don't "know" that you're creating Javascript. You need to include the quotes that Javascript needs around a string literal:</p> <pre><code>var userName = "{{name}}"; </code></pre>
0
2009-12-21T02:16:26Z
[ "python", "django" ]
python django string rendering issue
1,937,682
<p>I'm trying to render a string into a javascript ( which usually works fine for me ) here's my code</p> <p>HTML:</p> <pre><code>THE USER NAME IS : {{name}} has added app {{has_added_app}} </code></pre> <p>JAVA SCRIPT: </p> <pre><code>&lt;script&gt; &lt;!-- var userName = {{name}} </code></pre> <p>The html...
1
2009-12-21T00:59:10Z
1,937,864
<pre><code>var userName = {{name}} </code></pre> <p>Comes out when you view the HTML source as:</p> <pre><code>var userName = Bob </code></pre> <p>Which is an obvious mistake: missing quotes. But, simply putting quotes around it:</p> <pre><code>var userName = '{{name}}'; </code></pre> <p>isn't good enough for the ...
8
2009-12-21T02:27:10Z
[ "python", "django" ]
GeoDjango distance search
1,937,960
<p>I want to use GeoDjango to do basic location searches. Specifically I want to give the search function a ZIP code/city/county and find all the ZIP codes/cities/counties within 5mi, 10mi, 20mi, etc. I found the following paragraph in the documentation:</p> <blockquote> <p>Using a geographic coordinate system may...
3
2009-12-21T03:06:39Z
1,967,312
<p>To get around the limitation in the quote, you can just take the centroid of the zipcode region provided by the user, and then from that point find all zipcode regions that intersect a 5, 10 or whatever mile circle emanating from that point. I'm not sure how that would be achieved in geodjango, but with postgis it's...
3
2009-12-27T23:00:01Z
[ "python", "django", "geodjango" ]
GeoDjango distance search
1,937,960
<p>I want to use GeoDjango to do basic location searches. Specifically I want to give the search function a ZIP code/city/county and find all the ZIP codes/cities/counties within 5mi, 10mi, 20mi, etc. I found the following paragraph in the documentation:</p> <blockquote> <p>Using a geographic coordinate system may...
3
2009-12-21T03:06:39Z
12,540,588
<pre><code>In [1]: o = Place.objects.get(pk=2463583) # Oakland, CA In [2]: sf = Place.objects.get(pk=2487956) # San Francisco, CA In [3]: o.coords.transform(3410) # use the NSIDC EASE-Grid Global projection In [4]: sf.coords.transform(3410) # use the NSIDC EASE-Grid Global projection In [5]: o.coords.distance(s...
0
2012-09-22T04:04:35Z
[ "python", "django", "geodjango" ]
High-precision clock in Python
1,938,048
<p>Is there a way to measure time with high-precision in Python --- more precise than one second? I doubt that there is a cross-platform way of doing that; I'm interesting in high precision time on Unix, particularly Solaris running on a Sun SPARC machine.</p> <p><a href="http://docs.python.org/library/timeit.html">t...
23
2009-12-21T03:41:54Z
1,938,096
<p>You can simply use the standard <code>time</code> module:</p> <pre><code>&gt;&gt;&gt; import time &gt;&gt;&gt; time.time() 1261367718.971009 </code></pre>
22
2009-12-21T03:57:29Z
[ "python", "time" ]
High-precision clock in Python
1,938,048
<p>Is there a way to measure time with high-precision in Python --- more precise than one second? I doubt that there is a cross-platform way of doing that; I'm interesting in high precision time on Unix, particularly Solaris running on a Sun SPARC machine.</p> <p><a href="http://docs.python.org/library/timeit.html">t...
23
2009-12-21T03:41:54Z
1,940,109
<p>You can also use time.clock() It counts the time used by the process on Unix and time since the first call to it on Windows. It's more precise than time.time().</p> <p>It's the usually used function to measure performance.</p> <p>Just call </p> <pre><code>import time t_ = time.clock() #Your code here print 'Time ...
11
2009-12-21T13:30:30Z
[ "python", "time" ]
High-precision clock in Python
1,938,048
<p>Is there a way to measure time with high-precision in Python --- more precise than one second? I doubt that there is a cross-platform way of doing that; I'm interesting in high precision time on Unix, particularly Solaris running on a Sun SPARC machine.</p> <p><a href="http://docs.python.org/library/timeit.html">t...
23
2009-12-21T03:41:54Z
3,444,043
<p><code>time.clock()</code> has 13 decimal points on Windows but only two on Linux. <code>time.time()</code> has 17 decimals on Linux and 16 on Windows but the actual precision is different.</p> <p>I don't agree with the documentation that <a href="https://docs.python.org/library/time.html#time.clock" rel="nofollow">...
3
2010-08-09T20:48:03Z
[ "python", "time" ]
High-precision clock in Python
1,938,048
<p>Is there a way to measure time with high-precision in Python --- more precise than one second? I doubt that there is a cross-platform way of doing that; I'm interesting in high precision time on Unix, particularly Solaris running on a Sun SPARC machine.</p> <p><a href="http://docs.python.org/library/timeit.html">t...
23
2009-12-21T03:41:54Z
3,444,174
<p>Python tries hard to use the most precise time function for your platform to implement <code>time.time()</code>: </p> <pre><code>/* Implement floattime() for various platforms */ static double floattime(void) { /* There are three ways to get the time: (1) gettimeofday() -- resolution in microseconds ...
14
2010-08-09T21:02:51Z
[ "python", "time" ]
High-precision clock in Python
1,938,048
<p>Is there a way to measure time with high-precision in Python --- more precise than one second? I doubt that there is a cross-platform way of doing that; I'm interesting in high precision time on Unix, particularly Solaris running on a Sun SPARC machine.</p> <p><a href="http://docs.python.org/library/timeit.html">t...
23
2009-12-21T03:41:54Z
21,301,124
<p>David's post was attempting to show what the clock resolution is on Windows. I was confused by his output, so I wrote some code that shows that <code>time.time()</code> on my Windows 8 x64 laptop has a resolution of 1 msec:</p> <pre><code># measure the smallest time delta by spinning until the time changes def meas...
11
2014-01-23T06:40:00Z
[ "python", "time" ]
High-precision clock in Python
1,938,048
<p>Is there a way to measure time with high-precision in Python --- more precise than one second? I doubt that there is a cross-platform way of doing that; I'm interesting in high precision time on Unix, particularly Solaris running on a Sun SPARC machine.</p> <p><a href="http://docs.python.org/library/timeit.html">t...
23
2009-12-21T03:41:54Z
38,256,446
<p>If Python 3 is an option, you have two choices:</p> <ul> <li><a href="https://docs.python.org/3.5/library/time.html#time.perf_counter" rel="nofollow"><code>time.perf_counter</code></a> which always use the most accurate clock on your platform. It does include time spent outside of the process.</li> <li><a href="htt...
1
2016-07-07T22:48:48Z
[ "python", "time" ]
Understanding Lambda
1,938,189
<pre><code>X = 5 L = list(map(lambda x: 2**X, range(7))) print (L) </code></pre> <p>... I'm expecting this to return:</p> <pre><code>[1, 2, 4, 8, 16, 32, 64] </code></pre> <p>...instead, it returns:</p> <pre><code>[32, 32, 32, 32, 32, 32, 32] </code></pre> <p>What am I doing wrong?</p>
0
2009-12-21T04:31:30Z
1,938,206
<p>Try <code>L = list(map(lambda x: 2**x, range(7)))</code> once. You were using <code>X</code> instead of <code>x</code>.</p>
2
2009-12-21T04:35:37Z
[ "python", "map", "lambda" ]
Understanding Lambda
1,938,189
<pre><code>X = 5 L = list(map(lambda x: 2**X, range(7))) print (L) </code></pre> <p>... I'm expecting this to return:</p> <pre><code>[1, 2, 4, 8, 16, 32, 64] </code></pre> <p>...instead, it returns:</p> <pre><code>[32, 32, 32, 32, 32, 32, 32] </code></pre> <p>What am I doing wrong?</p>
0
2009-12-21T04:31:30Z
1,938,208
<p>Python is case-sensitive, so <code>lambda x: 2**X</code> means: take an argument, call it (lowercase) <code>x</code>, ignore it completely, and return 2 to the power of global variable (uppercase) <code>X</code>.</p>
10
2009-12-21T04:35:45Z
[ "python", "map", "lambda" ]
Understanding Lambda
1,938,189
<pre><code>X = 5 L = list(map(lambda x: 2**X, range(7))) print (L) </code></pre> <p>... I'm expecting this to return:</p> <pre><code>[1, 2, 4, 8, 16, 32, 64] </code></pre> <p>...instead, it returns:</p> <pre><code>[32, 32, 32, 32, 32, 32, 32] </code></pre> <p>What am I doing wrong?</p>
0
2009-12-21T04:31:30Z
1,938,209
<p>Python is case-sensitive. <code>x</code> and <code>X</code> are different variables.</p> <p>By the way, perhaps an easier way to construct <code>L</code> would be </p> <pre><code>L=[2**x for x in range(7)] </code></pre> <p>Or, if you'd like to use <code>map</code> and <code>lambda</code>, then </p> <p><code>L=ma...
4
2009-12-21T04:35:45Z
[ "python", "map", "lambda" ]
Understanding Lambda
1,938,189
<pre><code>X = 5 L = list(map(lambda x: 2**X, range(7))) print (L) </code></pre> <p>... I'm expecting this to return:</p> <pre><code>[1, 2, 4, 8, 16, 32, 64] </code></pre> <p>...instead, it returns:</p> <pre><code>[32, 32, 32, 32, 32, 32, 32] </code></pre> <p>What am I doing wrong?</p>
0
2009-12-21T04:31:30Z
1,938,228
<p>You have a typo. It should be: </p> <pre><code># Upper case X refers to 5 list(map(lambda x: 2**x, range(7))) </code></pre>
0
2009-12-21T04:42:39Z
[ "python", "map", "lambda" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
1,938,241
<p>try:</p> <pre><code>for i in str(inp): </code></pre> <p>That will iterate over the characters in the string representation. Once you have each character you can use it like a separate number.</p>
5
2009-12-21T04:45:14Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
1,938,245
<p><code>for .. in</code> statements expect you to use a type that has an iterator defined. A simple int type does not have an iterator. </p>
1
2009-12-21T04:45:49Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
1,938,268
<p>Well, you want to process the string representing the number, iterating over the digits, not the number itself (which is an abstract entity that could be written differently, like "CX" in Roman numerals or "0x6e" hexadecimal (both for 110) or whatever).</p> <p>Therefore:</p> <pre><code>inp = input('Enter a number:...
2
2009-12-21T04:52:53Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
1,938,295
<p>First, lose that absurd call to <code>int</code> -- which is taking you <strong>farther</strong> from what you want, so, what ever possessed you to put it in?! Change:</p> <pre><code>inp = int(input("Enter a number:")) </code></pre> <p>to the simpler</p> <pre><code>inp = input("Enter a number:") </code></pre> <...
17
2009-12-21T05:01:38Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
1,939,268
<p>Side note: if you want to get the sum of all digits, you can simply do</p> <pre><code>print sum(int(digit) for digit in raw_input('Enter a number:')) </code></pre>
3
2009-12-21T10:12:19Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
1,942,265
<p>As ghills had already mentioned</p> <pre><code>inp = int(input("Enter a number:")) n = 0 for i in str(inp): n = n + int(i); print n </code></pre> <p>When you are looping through something, keyword is "IN", just always think of it as a list of something. You cannot loop through a plain integer. Therefore, ...
1
2009-12-21T19:59:56Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
6,348,896
<p>Take your input and make sure it's a string so that it's iterable.</p> <p>Then perform a list comprehension and change each value back to a number.</p> <p>Now, you can do the sum of all the numbers if you want:</p> <pre><code>inp = [int(i) for i in str(input("Enter a number:"))] print sum(inp) </code></pre> <p>O...
0
2011-06-14T19:25:03Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
17,435,867
<p>You can try to change <code>for i in inp:</code> into <code>for i in range(1,inp):</code> Iteration doesn't work with a single int. Instead, you need provide a range for it to run.</p>
0
2013-07-02T21:06:22Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
19,754,616
<p>Don't make it a <code>int()</code>, but make it a <code>range()</code> will solve this problem.</p> <pre><code>inp = range(input("Enter a number: ")) </code></pre>
0
2013-11-03T15:28:46Z
[ "python", "int", "loops" ]
int object is not iterable?
1,938,227
<pre><code>inp = int(input("Enter a number:")) for i in inp: n = n + i; print (n) </code></pre> <p>... throws an error: <code>'int' object is not iterable</code></p> <p>I wanted to find out the total by adding each digit, for eg, 110. 1 + 1 + 0 = 2. How do I do that?</p> <p>Thanks</p>
9
2009-12-21T04:42:14Z
31,234,854
<p>maybe you're trying to </p> <pre><code>for i in range(inp) </code></pre> <p>I just had this error because I wasn't using range()</p>
0
2015-07-05T20:47:40Z
[ "python", "int", "loops" ]
Does anyone had success getting Django to send emails when hosting on Dreamhost?
1,938,609
<p>Greetings,</p> <p>Does anyone know what are the required fields to have Django send emails when a "500 Internal Server Error" happend? I am hosting my project on Dreamhost and for the life of me I can't get Django to send emails. What are the required fields when hosting on Dreamhost?</p>
7
2009-12-21T06:57:48Z
1,938,615
<p>Yes, I am, same on dreamhost, but I am using gmail to send email like following sample code</p> <pre><code>import smtplib m = smtplib.SMTP("smtp.gmail.com", 587) m.ehlo() m.starttls() m.ehlo() m.login(USERNAME, PASSWD) m.sendmail(user, to, "From: %s\nTo: %s\n\nHello World!"%(USERNAME,TOADDR)) m.close() </code></pr...
1
2009-12-21T07:03:14Z
[ "python", "django", "dreamhost" ]
Does anyone had success getting Django to send emails when hosting on Dreamhost?
1,938,609
<p>Greetings,</p> <p>Does anyone know what are the required fields to have Django send emails when a "500 Internal Server Error" happend? I am hosting my project on Dreamhost and for the life of me I can't get Django to send emails. What are the required fields when hosting on Dreamhost?</p>
7
2009-12-21T06:57:48Z
1,938,625
<p>As proposed by S.Mark, you can use gmail. Here is what you need in your settings.py</p> <pre><code>ADMINS = ( ('Your Name', 'your_name@email.com'), ) EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_PASSWORD = 'password' EMAIL_HOST_USER = 'gmail_account' EMAIL_SUBJECT_PREFIX = 'something' EMAIL_USE_TL...
17
2009-12-21T07:06:43Z
[ "python", "django", "dreamhost" ]
Does anyone had success getting Django to send emails when hosting on Dreamhost?
1,938,609
<p>Greetings,</p> <p>Does anyone know what are the required fields to have Django send emails when a "500 Internal Server Error" happend? I am hosting my project on Dreamhost and for the life of me I can't get Django to send emails. What are the required fields when hosting on Dreamhost?</p>
7
2009-12-21T06:57:48Z
1,939,248
<p>Do you have an SMTP server set up anywhere? As people have suggested here, you can easily use gmail, but you are by no means limited to using only Gmails SMTP server. You can create your own on your own hardware if you like, or you can use a number of free SMTP servers out there. I'd say the most fun would be to set...
0
2009-12-21T10:07:50Z
[ "python", "django", "dreamhost" ]
Does anyone had success getting Django to send emails when hosting on Dreamhost?
1,938,609
<p>Greetings,</p> <p>Does anyone know what are the required fields to have Django send emails when a "500 Internal Server Error" happend? I am hosting my project on Dreamhost and for the life of me I can't get Django to send emails. What are the required fields when hosting on Dreamhost?</p>
7
2009-12-21T06:57:48Z
3,738,431
<p>One issue we seem to have found with this gmail work around, is that if you try testing by sending from a gmail account to a dreamhost email that forwards back to the same gmail, the message is dropped. This may be some weird security 'feature' that dreamhost has going.</p>
0
2010-09-17T19:38:24Z
[ "python", "django", "dreamhost" ]
Does anyone had success getting Django to send emails when hosting on Dreamhost?
1,938,609
<p>Greetings,</p> <p>Does anyone know what are the required fields to have Django send emails when a "500 Internal Server Error" happend? I am hosting my project on Dreamhost and for the life of me I can't get Django to send emails. What are the required fields when hosting on Dreamhost?</p>
7
2009-12-21T06:57:48Z
11,368,447
<p>Try to use:</p> <pre><code>EMAIL_HOST = "localhost" </code></pre> <p>instead of DNS resolution...</p>
0
2012-07-06T19:21:03Z
[ "python", "django", "dreamhost" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
1,938,621
<pre><code>a[("John", "Doe")] = "123 Fake Street" </code></pre>
0
2009-12-21T07:06:05Z
[ "python", "list", "tuples" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
1,938,623
<pre><code>salaries = {} salaries[('John', 'Smith')] = 10000.0 salaries[('John', 'Parker')] = 99999.0 </code></pre> <p><strong>EDIT 1</strong> Of course you can do <code>salaries['John Smith'] = whatever</code>, but then you'll have to do extra work to separate the key into first and last names. What about <code>point...
15
2009-12-21T07:06:10Z
[ "python", "list", "tuples" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
1,938,638
<p>Classic Example: You want to store point value as tuple of (x, y) </p>
43
2009-12-21T07:14:10Z
[ "python", "list", "tuples" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
1,938,660
<p>I suppose in the case of sorting, there could be merit in using a tuple. For example, suppose the dictionary key represents a sort field (obviously there would be a default sort field to prevent the key from being <code>None</code>). If you needed multiple sort fields, such as the case of sorting by last name, the...
0
2009-12-21T07:22:40Z
[ "python", "list", "tuples" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
1,938,709
<p>I use tuple lots of time as <code>dict</code> key e.g.</p> <ul> <li><p>I do use them when I have to create a unique key from multiple values e.g.</p> <p>based on <code>first_name</code>, <code>last_name</code> key could be key = <code>'%s_%s'%(first_name, last_name)</code> but better way is <code>key = (first_name...
6
2009-12-21T07:36:46Z
[ "python", "list", "tuples" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
1,938,711
<p>I used tuples as dictionary keys in application that compares network devices by geographical location. Since the devices are named similarly for each location, it provides a natural way to know if a device matching that pairing has been seen yet while processing multiples.</p> <p>i.e.</p> <pre><code>seen = {} se...
3
2009-12-21T07:37:37Z
[ "python", "list", "tuples" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
24,663,657
<p>You can use it for funnel analysis if you are building a basic analytics tool.</p> <p><strong>For example</strong>, counting how many people clicked the image3 after hovering on text2.</p> <pre><code> funnels = defaultdict(int) funnels[('hovered_text2', 'clicked_image3')] += 1 </code></pre>
0
2014-07-09T20:56:34Z
[ "python", "list", "tuples" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
37,078,437
<pre><code>def getHash(word): result={} for i in range(len(word)): if word[i] in result: result[word[i]]+=1 else : result[word[i]]=1 return tuple (sorted((result.items()))) def groupAnagrams(words): resultHash={} for i in range(len(words)): s=getHas...
0
2016-05-06T17:42:00Z
[ "python", "list", "tuples" ]
In what case would I use a tuple as a dictionary key?
1,938,614
<p>I was studying the <a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">difference between lists and tuples</a> (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.</p> <p>A sentence in...
30
2009-12-21T07:01:29Z
37,517,478
<p>You use tuples as keys when you want to show multiple elements which form a key together.</p> <p>Eg: <code>{(&lt;x-coordinate&gt;,&lt;y-coordinate&gt;): &lt;indicating letter&gt;}</code></p> <p>Here if we use <code>x-coordinate</code> or <code>y-coordinate</code> separately, we wouldn't be representing that point....
0
2016-05-30T04:09:37Z
[ "python", "list", "tuples" ]
Checking if A is superclass of B in Python
1,938,755
<pre><code>class p1(object): pass class p2(p1): pass </code></pre> <p>So p2 is the subclass of p1. Is there a way to find out programmatically that p1 is [one of] the superclass[es] of p2 ?</p>
20
2009-12-21T07:49:15Z
1,938,766
<p>I think you meant to use "class" instead of "def".. :) Anyway, try <code>p2.__bases__</code></p>
5
2009-12-21T07:54:22Z
[ "python", "reflection", "superclass" ]
Checking if A is superclass of B in Python
1,938,755
<pre><code>class p1(object): pass class p2(p1): pass </code></pre> <p>So p2 is the subclass of p1. Is there a way to find out programmatically that p1 is [one of] the superclass[es] of p2 ?</p>
20
2009-12-21T07:49:15Z
1,938,779
<p>Yes, there is way. You can use a <a href="http://docs.python.org/library/functions.html#issubclass">issubclass</a> function.</p> <p>As follows:</p> <pre><code>class p1(object):pass class p2(p1):pass issubclass(p2, p1) </code></pre>
30
2009-12-21T07:58:39Z
[ "python", "reflection", "superclass" ]
Checking if A is superclass of B in Python
1,938,755
<pre><code>class p1(object): pass class p2(p1): pass </code></pre> <p>So p2 is the subclass of p1. Is there a way to find out programmatically that p1 is [one of] the superclass[es] of p2 ?</p>
20
2009-12-21T07:49:15Z
1,938,795
<p>Depending on what you're trying to do, the "mro" method can also be useful. </p>
6
2009-12-21T08:03:07Z
[ "python", "reflection", "superclass" ]
Checking if A is superclass of B in Python
1,938,755
<pre><code>class p1(object): pass class p2(p1): pass </code></pre> <p>So p2 is the subclass of p1. Is there a way to find out programmatically that p1 is [one of] the superclass[es] of p2 ?</p>
20
2009-12-21T07:49:15Z
1,938,860
<p>using &lt;class&gt;.&#95;&#95;bases&#95;&#95; seems to be what you're looking for...</p> <pre><code>&gt;&gt;&gt; class p1(object): pass &gt;&gt;&gt; class p2(p1): pass &gt;&gt;&gt; p2.__bases__ (&lt;class '__main__.p1'&gt;,) </code></pre>
40
2009-12-21T08:24:07Z
[ "python", "reflection", "superclass" ]
Merging multiple line segments
1,938,831
<p>My program uses PyOpenGL (so it's Python) with psyco.</p> <p>I have around 21,000 line segments which I need to render in each frame of my render (unless the user zooms in, in which case line segments are culled and not sent to the card at all). This is currently taking around 1.5 seconds each frame to complete. Th...
1
2009-12-21T08:16:44Z
1,938,889
<p>20K segments isn't that much. Also, you'll be lucky when you can merge 10-100 lines per frame, so the speedup by this optimization will be neglectable. The rendering process is probably slow because you create the model again and again. Use <code>glNewList()</code> to save all the rendering commands in an GL render ...
0
2009-12-21T08:34:42Z
[ "python", "opengl", "merge", "pyopengl", "line-segment" ]
Merging multiple line segments
1,938,831
<p>My program uses PyOpenGL (so it's Python) with psyco.</p> <p>I have around 21,000 line segments which I need to render in each frame of my render (unless the user zooms in, in which case line segments are culled and not sent to the card at all). This is currently taking around 1.5 seconds each frame to complete. Th...
1
2009-12-21T08:16:44Z
1,938,914
<p>You can define an error metric for merging two line segments into one and then testing all pairs of segments and then merging them if the error is below a certain threshold.</p> <p>One example is this algorithm:</p> <ol> <li>Construct a new line segment X from the two points farthest away from each other in the tw...
0
2009-12-21T08:40:33Z
[ "python", "opengl", "merge", "pyopengl", "line-segment" ]
Merging multiple line segments
1,938,831
<p>My program uses PyOpenGL (so it's Python) with psyco.</p> <p>I have around 21,000 line segments which I need to render in each frame of my render (unless the user zooms in, in which case line segments are culled and not sent to the card at all). This is currently taking around 1.5 seconds each frame to complete. Th...
1
2009-12-21T08:16:44Z
1,938,969
<p>It's almost certainly the overhead of all the immediate mode function calls that's killing your performance. I would do the following.</p> <p>Don't use <code>GL_LINE_STRIPS</code>, use a single list of <code>GL_LINES</code> instead so they can be rendered in one go.</p> <p>Use <code>glDrawArrays</code> instead of ...
4
2009-12-21T08:58:30Z
[ "python", "opengl", "merge", "pyopengl", "line-segment" ]
csv to sparse matrix in python
1,938,894
<p>I have a big csv file which lists connections between nodes in a graph. example:</p> <p>0001,95784<br> 0001,98743<br> 0002,00082<br> 0002,00091 </p> <p>So this means that node id 0001 is connected to node 95784 and 98743 and so on. I need to read this into a sparse matrix in numpy. How can i do this? I am new to ...
6
2009-12-21T08:35:36Z
1,938,998
<p>If you want an adjacency matrix, you can do something like:</p> <pre><code>from scipy.sparse import * from scipy import * from numpy import * import csv S = dok_matrix((10000,10000), dtype=bool) f = open("your_file_name") reader = csv.reader(f) for line in reader: S[int(line[0]),int(line[1])] = True </code></pr...
1
2009-12-21T09:04:45Z
[ "python", "data-structures", "sparse-matrix" ]
csv to sparse matrix in python
1,938,894
<p>I have a big csv file which lists connections between nodes in a graph. example:</p> <p>0001,95784<br> 0001,98743<br> 0002,00082<br> 0002,00091 </p> <p>So this means that node id 0001 is connected to node 95784 and 98743 and so on. I need to read this into a sparse matrix in numpy. How can i do this? I am new to ...
6
2009-12-21T08:35:36Z
1,939,088
<p>You might also be interested in <a href="http://networkx.lanl.gov/" rel="nofollow">Networkx</a>, a pure python network/graphing package.</p> <p>From the website:</p> <blockquote> <p>NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex network...
0
2009-12-21T09:25:29Z
[ "python", "data-structures", "sparse-matrix" ]
csv to sparse matrix in python
1,938,894
<p>I have a big csv file which lists connections between nodes in a graph. example:</p> <p>0001,95784<br> 0001,98743<br> 0002,00082<br> 0002,00091 </p> <p>So this means that node id 0001 is connected to node 95784 and 98743 and so on. I need to read this into a sparse matrix in numpy. How can i do this? I am new to ...
6
2009-12-21T08:35:36Z
1,939,102
<p>Example using <a href="http://www.scipy.org/doc/api%5Fdocs/SciPy.sparse.sparse.lil%5Fmatrix.html">lil_matrix</a> (list of list matrix) of scipy.</p> <blockquote> <p>Row-based linked list matrix.</p> <p>This contains a list (<code>self.rows</code>) of rows, each of which is a sorted list of column indices of ...
10
2009-12-21T09:29:16Z
[ "python", "data-structures", "sparse-matrix" ]
pydev 1.5.3 not working fine with Easy Eclipse 1.3.1
1,938,929
<p>I installed Pydev 1.5.3 (so that I could get the merged version of Pydev Extensions in core PyDev) in an EasyEclipse 1.3.1 installation. After this, <code>Compare with &gt; Base revision</code> etc. comparison operations stopped working. I had to disable the PyDev 1.5.3 and revert back to the pre-installed Pydev 1.3...
1
2009-12-21T08:45:18Z
2,967,350
<p>My pydev broke entirely with 1.5.3. I had to downgrade <code>yum downgrade eclipse-pydev</code> and keep yum from updating it ever since.</p>
0
2010-06-03T15:24:17Z
[ "python", "eclipse", "pydev" ]
pydev 1.5.3 not working fine with Easy Eclipse 1.3.1
1,938,929
<p>I installed Pydev 1.5.3 (so that I could get the merged version of Pydev Extensions in core PyDev) in an EasyEclipse 1.3.1 installation. After this, <code>Compare with &gt; Base revision</code> etc. comparison operations stopped working. I had to disable the PyDev 1.5.3 and revert back to the pre-installed Pydev 1.3...
1
2009-12-21T08:45:18Z
2,973,340
<p>I am now using PyDev 1.5.6 and its working fine with EasyEclipse (along with SubClipse). The issues in comparison seem to have been resolved. In fact, the file diff in 1.5.6 is looking much more beautiful than before. </p>
0
2010-06-04T10:40:55Z
[ "python", "eclipse", "pydev" ]
Is there any libraries could import contacts from hotmail/live/aol account?
1,938,945
<p>I've import contacts from gmail by using gdata api, and is there any apis like that for hotmail/live/Aol ?</p>
1
2009-12-21T08:50:39Z
1,938,981
<p>If you are able to export from them to the standard vCard format, you can read and process vCard data using the <a href="http://pypi.python.org/pypi/vobject" rel="nofollow">vobject</a> package.</p>
1
2009-12-21T09:01:14Z
[ "python", "api" ]
Is there any libraries could import contacts from hotmail/live/aol account?
1,938,945
<p>I've import contacts from gmail by using gdata api, and is there any apis like that for hotmail/live/Aol ?</p>
1
2009-12-21T08:50:39Z
1,939,027
<h2>Hotmail/Live</h2> <p>There is <a href="http://msdn.microsoft.com/en-us/library/bb463989.aspx" rel="nofollow">Windows Live Contact API</a> for Hotmail/Live mail, which is similar to GData API.</p> <h2>AOL</h2> <p>As far as I remember AOL doesn't provide API to access its address book, however they working on it (...
1
2009-12-21T09:11:06Z
[ "python", "api" ]
Is there any libraries could import contacts from hotmail/live/aol account?
1,938,945
<p>I've import contacts from gmail by using gdata api, and is there any apis like that for hotmail/live/Aol ?</p>
1
2009-12-21T08:50:39Z
1,939,043
<p>There is Windows Live Contact API for Hotmail/Live mail. Yahoo Contact API for Yahoo also exists, but to this date, no AOL contact api. </p> <p>I would suggest you try openinviter (openinviter.com) to import contacts. Unfortunately, you will not have OAuth capabilities, but it is the best class out there and works ...
2
2009-12-21T09:14:53Z
[ "python", "api" ]
changing image resolution in Python for a .bmp file
1,938,959
<p>I want to change the resolution of the .bmp image in python. ( i.e. the pixel/inch information) . Using PIL, for jpg image, for instance, the following code works fine</p> <pre><code>import Image im = Image.open("myImg.jpg) im.save("output.jpg", dpi = (75, 75) ) </code></pre> <p>If you view this in some image edi...
1
2009-12-21T08:54:42Z
1,939,087
<p>I think input file can be any valid format and output need to be, jpg, pcx, png, tiff, wmf for dpi parameter I think, may be I am wrong, but when I looked at PIL source files, I don't see one for BMP files.</p>
0
2009-12-21T09:25:24Z
[ "python", "python-imaging-library" ]
changing image resolution in Python for a .bmp file
1,938,959
<p>I want to change the resolution of the .bmp image in python. ( i.e. the pixel/inch information) . Using PIL, for jpg image, for instance, the following code works fine</p> <pre><code>import Image im = Image.open("myImg.jpg) im.save("output.jpg", dpi = (75, 75) ) </code></pre> <p>If you view this in some image edi...
1
2009-12-21T08:54:42Z
1,939,093
<p>I suspect that there aren't many programs out there that respect bmp resolution. Windows bitmaps are all 96 dpi.</p> <p>Also, It looks as though PIL doesn't support a resolution parameter for bitmaps. My <code>PIL/BmpImagePlugin.py</code> just writes in $01000000 for the x &amp; y resolution (i'm not sure what that...
2
2009-12-21T09:26:21Z
[ "python", "python-imaging-library" ]