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
Outputting data a row at a time from mysql using sqlalchemy
536,051
<p>I want to fetch data from a mysql database using sqlalchemy and use the data in a different class.. Basically I fetch a row at a time, use the data, fetch another row, use the data and so on.. I am running into some problem doing this.. </p> <p>Basically, how do I output data a row at a time from mysql data?.. I ha...
0
2009-02-11T09:19:22Z
536,180
<p>From what I understand, you're interested in something like this:</p> <pre><code># s is object returned by the .select() method rs = s.execute() row = rs.fetchone() # do something with the row row = rs.fetchone() # do something with another row </code></pre> <p>You can find this in a tutorial <a href="http://www.r...
0
2009-02-11T09:59:46Z
[ "python", "mysql", "sqlalchemy" ]
Outputting data a row at a time from mysql using sqlalchemy
536,051
<p>I want to fetch data from a mysql database using sqlalchemy and use the data in a different class.. Basically I fetch a row at a time, use the data, fetch another row, use the data and so on.. I am running into some problem doing this.. </p> <p>Basically, how do I output data a row at a time from mysql data?.. I ha...
0
2009-02-11T09:19:22Z
536,269
<p>Exactly what problems are you running into?</p> <p>You can simply iterate over the ResultProxy object:</p> <pre> for row in conn_or_sess_or_engine.execute(selectable_obj_or_SQLstring): do_something_with(row) </pre>
1
2009-02-11T10:36:33Z
[ "python", "mysql", "sqlalchemy" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
536,164
<p>You could read the file from a std::iostream line by line, put each line into a std::string and then use boost::tokenizer to split it. It won't be quite as elegant/short as the python one but a lot easier than reading things in a character at a time...</p>
3
2009-02-11T09:55:27Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
536,177
<pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; // For replace() using namespace std; struct Point { double a, b, c; }; int main(int argc, char **argv) { vector&lt;Point&gt; points; ifst...
13
2009-02-11T09:59:21Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
536,224
<p>This answer is based on the previous answer by j_random_hacker and makes use of Boost Spirit.</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;boost/spirit.hpp&gt; using namespace std; using namespace boost; using namespace boost::spiri...
7
2009-02-11T10:19:55Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
536,265
<p>I`d do something like this:</p> <pre><code>ifstream f("data.txt"); string str; while (getline(f, str)) { Point p; sscanf(str.c_str(), "%f, %f, %f\n", &amp;p.x, &amp;p.y, &amp;p.z); points.push_back(p); } </code></pre> <p>x,y,z must be floats.</p> <p>And include:</p> <pre><code>#include &lt;iostream&...
24
2009-02-11T10:33:17Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
536,431
<p>All these good examples aside, in C++ you would normally override the <code>operator &gt;&gt;</code> for your point type to achieve something like this:</p> <pre><code>point p; while (file &gt;&gt; p) points.push_back(p); </code></pre> <p>or even:</p> <pre><code>copy( istream_iterator&lt;point&gt;(file), ...
16
2009-02-11T11:45:35Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
537,473
<p>Fun with Boost.Tuples:</p> <pre><code>#include &lt;boost/tuple/tuple_io.hpp&gt; #include &lt;vector&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; int main() { using namespace boost::tuples; typedef boost::tuple&lt;float,float,float&gt; PointT; std::ifstream f("input...
4
2009-02-11T16:01:43Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
538,747
<p>Its nowhere near as terse, and of course I didn't compile this.</p> <pre><code>float atof_s( std::string &amp; s ) { return atoi( s.c_str() ); } { ifstream f("data.txt") string str; vector&lt;vector&lt;float&gt;&gt; data; while( getline( f, str ) ) { vector&lt;float&gt; v; boost::algorithm::split_iterator&lt;s...
0
2009-02-11T20:58:00Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
768,898
<p>The <a href="http://www.partow.net/programming/strtk/index.html">C++ String Toolkit Library (StrTk)</a> has the following solution to your problem:</p> <pre><code>#include &lt;string&gt; #include &lt;deque&gt; #include "strtk.hpp" struct point { double x,y,z; } int main() { std::deque&lt;point&gt; points; p...
13
2009-04-20T15:57:05Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
1,621,028
<p>One of Sony Picture Imagework's open-source projects is <a href="http://code.google.com/p/pystring/" rel="nofollow">Pystring</a>, which should make for a mostly direct translation of the string-splitting parts:</p> <blockquote> <p>Pystring is a collection of C++ functions which match the interface and behavior of...
1
2009-10-25T14:19:26Z
[ "c++", "python", "parsing", "string", "text-files" ]
C++ string parsing (python style)
536,148
<p>I love how in python I can do something like:</p> <pre><code>points = [] for line in open("data.txt"): a,b,c = map(float, line.split(',')) points += [(a,b,c)] </code></pre> <p>Basically it's reading a list of lines where each one represents a point in 3D space, the point is represented as three numbers sep...
15
2009-02-11T09:49:23Z
5,718,665
<p>all these are good examples. yet they dont answer the following:</p> <ol> <li>a CSV file with different column numbers (some rows with more columns than others)</li> <li>or when some of the values have white space (ya yb,x1 x2,,x2,)</li> </ol> <p>so for those who are still looking, this class: <a href="http://www...
0
2011-04-19T15:15:48Z
[ "c++", "python", "parsing", "string", "text-files" ]
Execute arbitrary python code remotely - can it be done?
536,370
<p>I'm working on a grid system which has a number of very powerful computers. These can be used to execute python functions very quickly. My users have a number of python functions which take a long time to calculate on workstations, ideally they would like to be able to call some functions on a remote powerful server...
3
2009-02-11T11:21:41Z
536,408
<p>It sounds like you want to do the following.</p> <ul> <li><p>Define a shared filesystem space.</p></li> <li><p>Put ALL your python source in this shared filesystem space.</p></li> <li><p>Define simple agents or servers that will "execfile" a block of code.</p></li> <li><p>Your client then contacts the agent (REST p...
4
2009-02-11T11:38:18Z
[ "python", "grid" ]
Execute arbitrary python code remotely - can it be done?
536,370
<p>I'm working on a grid system which has a number of very powerful computers. These can be used to execute python functions very quickly. My users have a number of python functions which take a long time to calculate on workstations, ideally they would like to be able to call some functions on a remote powerful server...
3
2009-02-11T11:21:41Z
536,641
<p>You could use a SSH connection to the remote PC and run the commands on the other machine directly. You could even copy the python code to the machine and execute it.</p>
0
2009-02-11T12:55:01Z
[ "python", "grid" ]
Execute arbitrary python code remotely - can it be done?
536,370
<p>I'm working on a grid system which has a number of very powerful computers. These can be used to execute python functions very quickly. My users have a number of python functions which take a long time to calculate on workstations, ideally they would like to be able to call some functions on a remote powerful server...
3
2009-02-11T11:21:41Z
536,649
<p><a href="http://pypy.readthedocs.org/en/latest/stackless.html#unimplemented-features" rel="nofollow">Stackless</a> had ability to pickle and unpickle running code. Unfortunately current implementation doesn't support this feature. </p>
1
2009-02-11T12:57:51Z
[ "python", "grid" ]
Execute arbitrary python code remotely - can it be done?
536,370
<p>I'm working on a grid system which has a number of very powerful computers. These can be used to execute python functions very quickly. My users have a number of python functions which take a long time to calculate on workstations, ideally they would like to be able to call some functions on a remote powerful server...
3
2009-02-11T11:21:41Z
536,988
<p>You could use a ready-made clustering solution like <a href="http://www.parallelpython.com" rel="nofollow">Parallel Python</a>. You can relatively easily set up <a href="http://www.parallelpython.com/content/view/15/30/#QUICKCLUSTERS" rel="nofollow">multiple remote slaves and run arbitrary code on them</a>.</p>
1
2009-02-11T14:21:54Z
[ "python", "grid" ]
Execute arbitrary python code remotely - can it be done?
536,370
<p>I'm working on a grid system which has a number of very powerful computers. These can be used to execute python functions very quickly. My users have a number of python functions which take a long time to calculate on workstations, ideally they would like to be able to call some functions on a remote powerful server...
3
2009-02-11T11:21:41Z
537,168
<p>Take a look at <a href="http://pyro.sourceforge.net/">PyRO</a> (Python Remote objects) It has the ability to set up services on all the computers in your cluster, and invoke them directly, or indirectly through a name server and a publish-subscribe mechanism. </p>
6
2009-02-11T14:56:50Z
[ "python", "grid" ]
Execute arbitrary python code remotely - can it be done?
536,370
<p>I'm working on a grid system which has a number of very powerful computers. These can be used to execute python functions very quickly. My users have a number of python functions which take a long time to calculate on workstations, ideally they would like to be able to call some functions on a remote powerful server...
3
2009-02-11T11:21:41Z
30,859,367
<h1>Syntax:</h1> <p>cat ./test.py | sshpass -p 'password' ssh user@remote-ip "python - script-arguments-if-any for test.py script"</p> <p>1) here "test.py" is the local python script. 2) sshpass used to pass the ssh password to ssh connection</p>
0
2015-06-16T05:20:41Z
[ "python", "grid" ]
Python, Sqlite3 - How to convert a list to a BLOB cell
537,077
<p>What is the most elegant method for dumping a list in python into an sqlite3 DB as binary data (i.e., a BLOB cell)?</p> <pre><code>data = [ 0, 1, 2, 3, 4, 5 ] # now write this to db as binary data # 0000 0000 # 0000 0001 # ... # 0000 0101 </code></pre>
8
2009-02-11T14:38:06Z
537,192
<p>Assuming you want it treated as a sequence of 8-bit unsigned values, use the <code>array</code> module.</p> <pre><code>a = array.array('B', data) &gt;&gt;&gt; a.tostring() '\x00\x01\x02\x03\x04\x05' </code></pre> <p>Use different typecodes than <code>'B'</code> if you want to treat the data as different types. eg...
6
2009-02-11T15:01:45Z
[ "python", "sqlite" ]
Python, Sqlite3 - How to convert a list to a BLOB cell
537,077
<p>What is the most elegant method for dumping a list in python into an sqlite3 DB as binary data (i.e., a BLOB cell)?</p> <pre><code>data = [ 0, 1, 2, 3, 4, 5 ] # now write this to db as binary data # 0000 0000 # 0000 0001 # ... # 0000 0101 </code></pre>
8
2009-02-11T14:38:06Z
539,636
<p>It seems that Brian's solution fits your needs, but keep in mind that with that method your just storing the data as a string.</p> <p>If you want to store the raw binary data into the database (so it doesn't take up as much space), convert your data to a <strong>Binary</strong> sqlite object and then add it to your...
17
2009-02-12T01:27:31Z
[ "python", "sqlite" ]
Python, Sqlite3 - How to convert a list to a BLOB cell
537,077
<p>What is the most elegant method for dumping a list in python into an sqlite3 DB as binary data (i.e., a BLOB cell)?</p> <pre><code>data = [ 0, 1, 2, 3, 4, 5 ] # now write this to db as binary data # 0000 0000 # 0000 0001 # ... # 0000 0101 </code></pre>
8
2009-02-11T14:38:06Z
1,416,888
<p>See this general solution at SourceForge which covers any arbitrary Python object (including list, tuple, dictionary, etc):</p> <p>y_serial.py module :: warehouse Python objects with SQLite</p> <p>"Serialization + persistance :: in a few lines of code, compress and annotate Python objects into SQLite; then later r...
1
2009-09-13T04:57:40Z
[ "python", "sqlite" ]
Python, Sqlite3 - How to convert a list to a BLOB cell
537,077
<p>What is the most elegant method for dumping a list in python into an sqlite3 DB as binary data (i.e., a BLOB cell)?</p> <pre><code>data = [ 0, 1, 2, 3, 4, 5 ] # now write this to db as binary data # 0000 0000 # 0000 0001 # ... # 0000 0101 </code></pre>
8
2009-02-11T14:38:06Z
4,464,661
<p>I have the same problem, and I'm thinking about solving this in another way.</p> <p>I think the <a href="http://docs.python.org/library/pickle.html" rel="nofollow">pickle</a> module is done exactly for something like this (serialization on python objects)</p> <p>Example (this one is for dumping to file... but I th...
3
2010-12-16T19:36:03Z
[ "python", "sqlite" ]
Python, Sqlite3 - How to convert a list to a BLOB cell
537,077
<p>What is the most elegant method for dumping a list in python into an sqlite3 DB as binary data (i.e., a BLOB cell)?</p> <pre><code>data = [ 0, 1, 2, 3, 4, 5 ] # now write this to db as binary data # 0000 0000 # 0000 0001 # ... # 0000 0101 </code></pre>
8
2009-02-11T14:38:06Z
15,392,922
<p>It is possible to store object data as pickle dump, jason etc but it is also possible to index, them, restrict them and run select queries that use those indices. Here is example with tuples, that can be easily applied for any other python class. All that is needed is explained in python sqlite3 documentation (some...
0
2013-03-13T17:57:28Z
[ "python", "sqlite" ]
Reserve memory for list in Python?
537,086
<p>When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? I've looked through the docs for a Python list type, and have not found anything that seems to do this. However, thi...
30
2009-02-11T14:41:14Z
537,111
<p>Just create the list at the beginning like this:</p> <pre><code>a = range(10) </code></pre> <p>Then you can assign values writing</p> <pre><code>a[7] = an object </code></pre>
0
2009-02-11T14:46:02Z
[ "python", "performance", "arrays", "memory-management", "list" ]
Reserve memory for list in Python?
537,086
<p>When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? I've looked through the docs for a Python list type, and have not found anything that seems to do this. However, thi...
30
2009-02-11T14:41:14Z
537,134
<p>you can create list of the known length like this:</p> <pre><code>&gt;&gt;&gt; [None] * known_number </code></pre>
11
2009-02-11T14:49:25Z
[ "python", "performance", "arrays", "memory-management", "list" ]
Reserve memory for list in Python?
537,086
<p>When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? I've looked through the docs for a Python list type, and have not found anything that seems to do this. However, thi...
30
2009-02-11T14:41:14Z
537,183
<p>In most of everyday code you won't need such optimization.</p> <p>However, when list efficiency becomes an issue, the first thing you should do is replace generic list with typed one from <a href="http://docs.python.org/library/array.html"><code>array</code> module</a> which is much more efficient.</p> <p>Here's h...
5
2009-02-11T15:00:53Z
[ "python", "performance", "arrays", "memory-management", "list" ]
Reserve memory for list in Python?
537,086
<p>When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? I've looked through the docs for a Python list type, and have not found anything that seems to do this. However, thi...
30
2009-02-11T14:41:14Z
537,288
<p>In Python, all objects are allocated on the heap.<br> But Python uses a special memory allocator so <code>malloc</code> won't be called every time you need a new object.<br> There are also some optimizations for small integers (and the like) which are cached; however, which types, and how, is implementation dependen...
2
2009-02-11T15:19:39Z
[ "python", "performance", "arrays", "memory-management", "list" ]
Reserve memory for list in Python?
537,086
<p>When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? I've looked through the docs for a Python list type, and have not found anything that seems to do this. However, thi...
30
2009-02-11T14:41:14Z
537,351
<p>If you're wanting to manipulate numbers efficiently in Python then have a look at NumPy ( <a href="http://numpy.scipy.org/" rel="nofollow">http://numpy.scipy.org/</a>). It let's you do things extremely fast while still getting to use Python.</p> <p>To do what your asking in NumPy you'd do something like</p> <pre><...
4
2009-02-11T15:35:34Z
[ "python", "performance", "arrays", "memory-management", "list" ]
Reserve memory for list in Python?
537,086
<p>When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? I've looked through the docs for a Python list type, and have not found anything that seems to do this. However, thi...
30
2009-02-11T14:41:14Z
537,405
<p>Here's four variants:</p> <ul> <li>an incremental list creation</li> <li>"pre-allocated" list</li> <li>array.array()</li> <li>numpy.zeros()</li> </ul> <p>&nbsp;</p> <pre><code>python -mtimeit -s"N=10**6" "a = []; app = a.append;"\ "for i in xrange(N): app(i);" 10 loops, best of 3: 390 msec per loop python -...
26
2009-02-11T15:47:29Z
[ "python", "performance", "arrays", "memory-management", "list" ]
Reserve memory for list in Python?
537,086
<p>When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? I've looked through the docs for a Python list type, and have not found anything that seems to do this. However, thi...
30
2009-02-11T14:41:14Z
13,865,566
<p>Take a look at this:</p> <pre><code>In [7]: %timeit array.array('f', [0.0]*4000*1000) 1 loops, best of 3: 306 ms per loop In [8]: %timeit array.array('f', [0.0])*4000*1000 100 loops, best of 3: 5.96 ms per loop In [11]: %timeit np.zeros(4000*1000, dtype='f') 100 loops, best of 3: 6.04 ms per loop In [9]: %timeit...
7
2012-12-13T17:52:13Z
[ "python", "performance", "arrays", "memory-management", "list" ]
python, funny business with threads and IDEs?
537,196
<p>Maybe i cant do what i want? I want to have 1 thread do w/e it wants and a 2nd thread to recv user input to set the quit flag. using this code i want to enter q anytime to quit or have it timeout after printing hey 6 times</p> <pre><code>import sys import threading import time class MyThread ( threading.Thread ): ...
0
2009-02-11T15:02:29Z
537,480
<p>The problem here is that raw_input waits for an enter to flush the input stream; check out its <a href="http://docs.python.org/library/functions.html#raw_input" rel="nofollow">documentation</a>. PyScripter is probably seeing that the program is waiting for an input and giving you an input box (don't know for sure, n...
2
2009-02-11T16:02:40Z
[ "python", "multithreading", "ide" ]
How would you set up a python web server with multiple vhosts?
537,399
<p>I've been told wsgi is the way to go and not mod_python. But more specifically, how would you set up your multi website server environment? Choice of web server, etc?</p>
1
2009-02-11T15:46:06Z
537,504
<p>I'd recommend Nginx for the web server. Fast and easy to set up. </p> <p>You'd probably want to have one unix user per vhost - so every home directory holds its own application, python environment and server configuration. This allows you to restart a particular app safely, simply by killing worker processes that y...
1
2009-02-11T16:08:12Z
[ "python", "webserver", "environment", "wsgi" ]
How would you set up a python web server with multiple vhosts?
537,399
<p>I've been told wsgi is the way to go and not mod_python. But more specifically, how would you set up your multi website server environment? Choice of web server, etc?</p>
1
2009-02-11T15:46:06Z
537,518
<p>You could use Apache and <a href="http://www.modwsgi.org/" rel="nofollow">mod_wsgi</a>. That way, you can still use Apache's built-in support for vhosts.</p>
0
2009-02-11T16:11:15Z
[ "python", "webserver", "environment", "wsgi" ]
How would you set up a python web server with multiple vhosts?
537,399
<p>I've been told wsgi is the way to go and not mod_python. But more specifically, how would you set up your multi website server environment? Choice of web server, etc?</p>
1
2009-02-11T15:46:06Z
537,521
<p>Apache+mod_wsgi is a common choice.</p> <p>Here's a simple example vhost, setup up to map any requests for /wsgi/something to the application (which can then look at PATH_INFO to choose an action, or however you are doing your dispatching). The root URL '/' is also routed to the WSGI application.</p> <pre><code>Lo...
4
2009-02-11T16:11:33Z
[ "python", "webserver", "environment", "wsgi" ]
Standard python interpreter has a vi command mode?
537,522
<p>this is going to sound pretty ignorant, but:</p> <p>I was working a bit in the python interpreter (python 2.4 on RHEL 5.3), and suddenly found myself in what seems to be a 'vi command mode'. That is, I can edit previous commands with typical vi key bindings, going left with h, deleting with x...</p> <p>I love it -...
20
2009-02-11T16:11:44Z
537,633
<p>This kind of all depends on a few things.</p> <p>First of all, the python shell uses readline, and as such, your <code>~/.inputrc</code> is important here. That's the same with psql the PostgreSQL command-line interpreter and mysql the MySQL shell. All of those can be configured to use vi-style command bindings, wi...
24
2009-02-11T16:32:38Z
[ "python", "vi" ]
Standard python interpreter has a vi command mode?
537,522
<p>this is going to sound pretty ignorant, but:</p> <p>I was working a bit in the python interpreter (python 2.4 on RHEL 5.3), and suddenly found myself in what seems to be a 'vi command mode'. That is, I can edit previous commands with typical vi key bindings, going left with h, deleting with x...</p> <p>I love it -...
20
2009-02-11T16:11:44Z
538,573
<p>Ctrl-Alt-J switches from Emacs mode to Vi mode in <a href="http://tiswww.case.edu/php/chet/readline/rluserman.html#SEC22">readline programs</a>.</p> <p>Alternatively add "set editing-mode vi" to your ~/.inputrc</p>
18
2009-02-11T20:18:07Z
[ "python", "vi" ]
Standard python interpreter has a vi command mode?
537,522
<p>this is going to sound pretty ignorant, but:</p> <p>I was working a bit in the python interpreter (python 2.4 on RHEL 5.3), and suddenly found myself in what seems to be a 'vi command mode'. That is, I can edit previous commands with typical vi key bindings, going left with h, deleting with x...</p> <p>I love it -...
20
2009-02-11T16:11:44Z
29,808,730
<p>For Mac OS X 10.10.3, python2.7, vi mode can be configured by placing "bind -v" in ~/.editrc. The last few paragraphs of the man page hint at this.</p>
2
2015-04-22T21:01:20Z
[ "python", "vi" ]
Standard python interpreter has a vi command mode?
537,522
<p>this is going to sound pretty ignorant, but:</p> <p>I was working a bit in the python interpreter (python 2.4 on RHEL 5.3), and suddenly found myself in what seems to be a 'vi command mode'. That is, I can edit previous commands with typical vi key bindings, going left with h, deleting with x...</p> <p>I love it -...
20
2009-02-11T16:11:44Z
36,364,300
<p>Use <a href="https://docs.python.org/3/library/readline.html#readline.parse_and_bind" rel="nofollow">readline.parse_and_bind</a> method. For example, try on python interactive console:</p> <pre><code>import readline readline.parse_and_bind("set editing-mode vi") </code></pre> <p>It seems any command you can set in...
0
2016-04-01T19:22:22Z
[ "python", "vi" ]
How can I create multiple hashes of a file using only one pass?
537,542
<p>How can I get a MD5, SHA and other hashes from a file but only doing one pass? I have 100mb files, so I'd hate to process those 100MB files multiple times.</p>
6
2009-02-11T16:17:38Z
537,585
<p>Something like this perhaps?</p> <pre><code>&gt;&gt;&gt; import hashlib &gt;&gt;&gt; hashes = (hashlib.md5(), hashlib.sha1()) &gt;&gt;&gt; f = open('some_file', 'r') &gt;&gt;&gt; for line in f: ... for hash in hashes: ... hash.update(line) ... &gt;&gt;&gt; for hash in hashes: ... print hash.name, h...
15
2009-02-11T16:25:24Z
[ "python", "hash" ]
How can I create multiple hashes of a file using only one pass?
537,542
<p>How can I get a MD5, SHA and other hashes from a file but only doing one pass? I have 100mb files, so I'd hate to process those 100MB files multiple times.</p>
6
2009-02-11T16:17:38Z
537,597
<p>I don't know Python but I am familiar w/ hash calculations.</p> <p>If you handle the reading of files manually, just read in one block (of 256 bytes or 4096 bytes or whatever) at a time, and pass each block of data to update the hash of each algorithm. (you'll have to initialize state at the beginning and finalize ...
3
2009-02-11T16:26:27Z
[ "python", "hash" ]
How can I create multiple hashes of a file using only one pass?
537,542
<p>How can I get a MD5, SHA and other hashes from a file but only doing one pass? I have 100mb files, so I'd hate to process those 100MB files multiple times.</p>
6
2009-02-11T16:17:38Z
537,916
<p>Here's a modified <a href="http://stackoverflow.com/questions/537542/how-can-i-create-multiple-hashes-of-a-file-using-only-one-pass/537585#537585"><code>@ʞɔıu</code>'s answer</a> using <a href="http://stackoverflow.com/questions/537542/how-can-i-create-multiple-hashes-of-a-file-using-only-one-pass/537597#537597">...
6
2009-02-11T17:38:04Z
[ "python", "hash" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
537,724
<p>I had a similar dilemma when I first started writing in Python. I couldn't find any plugins for VS so I tried a few alternatives:</p> <ul> <li>IDLE - comes packaged with Python and works but felt very cludgy to me</li> <li><a href="http://www.wingware.com/" rel="nofollow">Wingware Python IDE</a> - not free, but see...
6
2009-02-11T16:48:18Z
[ "python", "visual-studio-2008", "ide" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
537,755
<p>Provided that you have Visual Studio 2008 and the VS 2k8 Shell Integrated Redistributable package, IronPython Studio Integrated will plug into the VS 2k8 IDE.</p> <p><a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=40646580-97FA-4698-B65F-620D4B4B1ED7&amp;displaylang=en" rel="nofollow">Visual Studi...
5
2009-02-11T16:54:35Z
[ "python", "visual-studio-2008", "ide" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
537,761
<p>Have a look at <a href="http://code.google.com/p/pyscripter/">PyScripter</a>, I haven't tried it extensively but heard good things about it.</p> <p>It's not an addon to Visual Studio, it's an independent IDE.</p>
8
2009-02-11T16:55:48Z
[ "python", "visual-studio-2008", "ide" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
537,903
<p>I'll second one of Jon's suggestions: PyDev and Eclipse. This is great if you are already using Eclipse for other development.</p> <p>You can grab it (and links to the Eclipse platform) from here: <a href="http://pydev.sourceforge.net/download.html" rel="nofollow">http://pydev.sourceforge.net/download.html</a></...
2
2009-02-11T17:33:52Z
[ "python", "visual-studio-2008", "ide" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
1,152,075
<p>Java based IDE's are no good they tend to be fat, slow, and unpolished. Eclipse has a poor programming paradigm that is unnecesarrily confusing. The many IDE vendors made a mistake by quitting their native products and basing them instead on Eclipse. They can't differentiate themselves and the experience is awful....
5
2009-07-20T07:13:11Z
[ "python", "visual-studio-2008", "ide" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
3,042,402
<p><a href="http://www.ironpython.net" rel="nofollow">IronPython tools for Visual Studio 2010.</a> is fairly well intergrated. I know you asked for 2008 but now that 2010 is out ;)</p>
4
2010-06-15T03:46:43Z
[ "python", "visual-studio-2008", "ide" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
5,258,719
<p>Python tools for Visual Studio 2010 is out now <a href="http://ironpython.codeplex.com/releases/view/41236" rel="nofollow">http://ironpython.codeplex.com/releases/view/41236</a></p>
2
2011-03-10T11:00:21Z
[ "python", "visual-studio-2008", "ide" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
10,888,693
<p>IronPython and Python Tools for Visual Studio (PTVS) are different things.</p> <p>PTVS <a href="http://pytools.codeplex.com/" rel="nofollow">http://pytools.codeplex.com/</a> is a free &amp; open source plug-in for Visual Studio 2010 from Microsoft's Developer Division. It allows you to code python using Visual Stu...
1
2012-06-04T21:33:23Z
[ "python", "visual-studio-2008", "ide" ]
Python IDE built into Visual Studio 2008?
537,689
<p>Hi I develop in Visual Studio 2008 a lot and would like to find an addin like vsphp which enables intellisense and debugging in Visual Studio. Is IronStudio what I am looking for? As far as I understand IronStudio is a Plugin for .NET.</p> <p>If there is no Plugin for Visual Studio 2008 whats a great IDE for a pyt...
16
2009-02-11T16:42:09Z
16,182,502
<p>Why don't you try the Aptana studio. It is similar to the Eclipse and PyDev. But is an separate IDE based upon python and web development.</p>
0
2013-04-24T02:43:28Z
[ "python", "visual-studio-2008", "ide" ]
Iterating over a string
538,346
<p>In C++, I could do:</p> <pre><code>for (int i = 0; i &lt; str.length(); ++i) std::cout &lt;&lt; str[i] &lt;&lt; std::endl; </code></pre> <p>How do I iterate over a string in Python?</p>
282
2009-02-11T19:22:15Z
538,352
<p>Even easier:</p> <pre><code>for c in "test": print c </code></pre>
65
2009-02-11T19:24:12Z
[ "python", "string", "iteration" ]
Iterating over a string
538,346
<p>In C++, I could do:</p> <pre><code>for (int i = 0; i &lt; str.length(); ++i) std::cout &lt;&lt; str[i] &lt;&lt; std::endl; </code></pre> <p>How do I iterate over a string in Python?</p>
282
2009-02-11T19:22:15Z
538,374
<p>As Johannes pointed out, </p> <pre><code>for c in "string": #do something with c </code></pre> <p>You can iterate pretty much anything in python using the <code>for loop</code> construct, </p> <p>for example, <code>open("file.txt")</code> returns a file object (and opens the file), iterating over it iterates ...
213
2009-02-11T19:30:23Z
[ "python", "string", "iteration" ]
Iterating over a string
538,346
<p>In C++, I could do:</p> <pre><code>for (int i = 0; i &lt; str.length(); ++i) std::cout &lt;&lt; str[i] &lt;&lt; std::endl; </code></pre> <p>How do I iterate over a string in Python?</p>
282
2009-02-11T19:22:15Z
555,631
<p>Just to make a more comprehensive answer, the C way of iterating over a string can apply in Python, if you really wanna force a square peg into a round hole.</p> <pre><code>i = 0 while i &lt; len(str): print str[i] i += 1 </code></pre> <p>But then again, why do that when strings are inherently iterable?</p...
24
2009-02-17T05:36:55Z
[ "python", "string", "iteration" ]
Iterating over a string
538,346
<p>In C++, I could do:</p> <pre><code>for (int i = 0; i &lt; str.length(); ++i) std::cout &lt;&lt; str[i] &lt;&lt; std::endl; </code></pre> <p>How do I iterate over a string in Python?</p>
282
2009-02-11T19:22:15Z
4,547,728
<p>If you need access to the index as you iterate through the string, use <a href="http://docs.python.org/library/functions.html#enumerate"><code>enumerate()</code></a>:</p> <pre><code>&gt;&gt;&gt; for i, c in enumerate('test'): ... print i, c ... 0 t 1 e 2 s 3 t </code></pre>
169
2010-12-28T16:54:32Z
[ "python", "string", "iteration" ]
Iterating over a string
538,346
<p>In C++, I could do:</p> <pre><code>for (int i = 0; i &lt; str.length(); ++i) std::cout &lt;&lt; str[i] &lt;&lt; std::endl; </code></pre> <p>How do I iterate over a string in Python?</p>
282
2009-02-11T19:22:15Z
34,354,312
<p>If you would like to use a more functional approach to iterating over a string (perhaps to transform it somehow), you can split the string into characters, apply a function to each one, then join the resulting list of characters back into a string.</p> <p>A string is inherently a list of characters, hence 'map' wil...
3
2015-12-18T11:11:28Z
[ "python", "string", "iteration" ]
Handling very large numbers in Python
538,551
<p>I've been considering fast poker hand evaluation in Python. It occurred to me that one way to speed the process up would be to represent all the card faces and suits as prime numbers and multiply them together to represent the hands. To whit:</p> <pre><code>class PokerCard: faces = '23456789TJQKA' suits = '...
48
2009-02-11T20:13:45Z
538,571
<p>python supports arbitrarily large integers naturally:</p> <pre><code>In [1]: 59**3*61**4*2*3*5*7*3*5*7 Out[1]: 62702371781194950 In [2]: _ % 61**4 Out[2]: 0 </code></pre>
13
2009-02-11T20:18:04Z
[ "python", "optimization", "largenumber" ]
Handling very large numbers in Python
538,551
<p>I've been considering fast poker hand evaluation in Python. It occurred to me that one way to speed the process up would be to represent all the card faces and suits as prime numbers and multiply them together to represent the hands. To whit:</p> <pre><code>class PokerCard: faces = '23456789TJQKA' suits = '...
48
2009-02-11T20:13:45Z
538,583
<p>Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called <code>long</code> and is separate from the <code>int</code> type, but the interpreter will automatically use whichever is more appropriate. In Python 3.0+, the <code>int</code> type has been dro...
71
2009-02-11T20:19:45Z
[ "python", "optimization", "largenumber" ]
Handling very large numbers in Python
538,551
<p>I've been considering fast poker hand evaluation in Python. It occurred to me that one way to speed the process up would be to represent all the card faces and suits as prime numbers and multiply them together to represent the hands. To whit:</p> <pre><code>class PokerCard: faces = '23456789TJQKA' suits = '...
48
2009-02-11T20:13:45Z
538,797
<p>You could do this for the fun of it, but other than that it's not a good idea. It would not speed up anything I can think of.</p> <ul> <li><p>Getting the cards in a hand will be an integer factoring operation which is much more expensive than just accessing an array.</p></li> <li><p>Adding cards would be multiplic...
19
2009-02-11T21:07:15Z
[ "python", "optimization", "largenumber" ]
Handling very large numbers in Python
538,551
<p>I've been considering fast poker hand evaluation in Python. It occurred to me that one way to speed the process up would be to represent all the card faces and suits as prime numbers and multiply them together to represent the hands. To whit:</p> <pre><code>class PokerCard: faces = '23456789TJQKA' suits = '...
48
2009-02-11T20:13:45Z
20,980,470
<p>python supports <strong><em>arbitrarily</em></strong> large <strong><em>integers</em></strong> naturally:</p> <p>example:</p> <blockquote> <p><strong>>>></strong> 10**1000 100000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000...
21
2014-01-07T19:50:08Z
[ "python", "optimization", "largenumber" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
538,687
<pre><code>&gt;&gt;&gt; str(datetime.timedelta(hours=10.56)) 10:33:36 &gt;&gt;&gt; td = datetime.timedelta(hours=10.505) # any timedelta object &gt;&gt;&gt; ':'.join(str(td).split(':')[:2]) 10:30 </code></pre> <p>Passing the <code>timedelta</code> object to the <code>str()</code> function calls the same formatting co...
29
2009-02-11T20:44:39Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
538,720
<p>Following Joe's example value above, I'd use the modulus arithmetic operator, thusly:</p> <pre><code>td = datetime.timedelta(hours=10.56) td_str = "%d:%d" % (td.seconds/3600, td.seconds%3600/60) </code></pre> <p>Note that integer division in Python rounds down by default; if you want to be more explicit, use math....
6
2009-02-11T20:52:18Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
538,721
<p>You can just convert the timedelta to a string with str(). Here's an example:</p> <pre><code>import datetime start = datetime.datetime(2009,2,10,14,00) end = datetime.datetime(2009,2,10,16,00) delta = end-start print str(delta) # prints 2:00:00 </code></pre>
119
2009-02-11T20:52:24Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
538,818
<p>Thanks everyone for your help. I took many of your ideas and put them together, let me know what you think.</p> <p>I added two methods to the class like this:</p> <pre><code>def hours(self): retval = "" if self.totalTime: hoursfloat = self.totalTime.seconds / 3600 retval = round(hoursfloat...
-7
2009-02-11T21:11:26Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
539,360
<p>As you know, you can get the seconds from a timedelta object by accessing the <code>.seconds</code> attribute.</p> <p>You can convert that to hours and remainder by using a combination of modulo and subtraction:</p> <pre class="lang-py prettyprint-override"><code># arbitrary number of seconds s = 13420 # hours hou...
116
2009-02-11T23:34:33Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
1,644,095
<p>My <code>datetime.timedelta</code> objects went greater than a day. So here is a further problem. All the discussion above assumes less than a day. A <code>timedelta</code> is actually a tuple of days, seconds and microseconds. The above discussion should use <code>td.seconds</code> as joe did, but if you have da...
9
2009-10-29T14:23:22Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
13,756,038
<pre><code>def td_format(td_object): seconds = int(td_object.total_seconds()) periods = [ ('year', 60*60*24*365), ('month', 60*60*24*30), ('day', 60*60*24), ('hour', 60*60), ('minute', 60), ...
20
2012-12-07T02:24:53Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
14,247,500
<p>Questioner wants a nicer format than the typical:</p> <pre><code> &gt;&gt;&gt; import datetime &gt;&gt;&gt; datetime.timedelta(seconds=41000) datetime.timedelta(0, 41000) &gt;&gt;&gt; str(datetime.timedelta(seconds=41000)) '11:23:20' &gt;&gt;&gt; str(datetime.timedelta(seconds=4102.33)) '1:08:22.330000...
9
2013-01-09T22:16:26Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
17,195,550
<p>He already has a timedelta object so why not use its built-in method total_seconds() to convert it to seconds, then use divmod() to get hours and minutes? </p> <pre><code> hours, remainder = divmod(myTimeDelta.total_seconds(), 3600) minutes, seconds = divmod(remainder, 60) # Formatted only for hours an...
9
2013-06-19T15:41:24Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
17,772,597
<pre><code>t1 = datetime.datetime.strptime(StartTime, "%H:%M:%S %d-%m-%y") t2 = datetime.datetime.strptime(EndTime, "%H:%M:%S %d-%m-%y") return str(t2-t1) </code></pre> <p>So for:</p> <pre><code>StartTime = '15:28:53 21-07-13' EndTime = '15:32:40 21-07-13' </code></pre> <p>returns:</p> <pre><code>'0:03:47' </code...
0
2013-07-21T13:01:00Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
19,074,707
<pre><code>def seconds_to_time_left_string(total_seconds): s = int(total_seconds) years = s // 31104000 if years &gt; 1: return '%d years' % years s = s - (years * 31104000) months = s // 2592000 if years == 1: r = 'one year' if months &gt; 0: r += ' and %d mo...
3
2013-09-29T05:21:26Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
23,582,157
<p>I had a similar problem with the output of overtime calculation at work. The value should always show up in HH:MM, even when it is greater than one day and the value can get negative. I combined some of the shown solutions and maybe someone else find this solution useful. I realized that if the timedelta value is ne...
3
2014-05-10T14:21:19Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
28,242,294
<p>I know that this is an old answered question, but I use <code>datetime.utcfromtimestamp()</code> for this. It takes the number of seconds and returns a <code>datetime</code> that can be formatted like any other <code>datetime</code>.</p> <pre><code>duration = datetime.utcfromtimestamp(end - begin) print duration.st...
3
2015-01-30T18:24:06Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
28,731,232
<p>I would seriously consider the Occam's Razor approach here:</p> <pre><code>td = str(timedelta).split('.')[0] </code></pre> <p>This returns a string without the microseconds</p> <p>If you want to regenerate the datetime.timedelta object, just do this:</p> <pre><code>h,m,s = re.split(':', td) new_delta = datetime....
3
2015-02-25T22:53:28Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
35,766,976
<p>Please check this function - it converts timedelta object into string 'HH:MM:SS'</p> <pre><code>def format_timedelta(td): hours, remainder = divmod(td.total_seconds(), 3600) minutes, seconds = divmod(remainder, 60) hours, minutes, seconds = int(hours), int(minutes), int(seconds) if hours &lt; 10: ...
0
2016-03-03T08:34:33Z
[ "python", "string", "datetime", "format", "timedelta" ]
Python format timedelta to string
538,666
<p>I'm a Python newbie (2 weeks) and I'm having trouble formatting a <code>datetime.timedelta</code> object.</p> <p>Here's what I'm trying to do: I have a list of objects and one of the members of the class of the object is a timedelta object that shows the duration of an event. I would like to display that durati...
125
2009-02-11T20:40:43Z
39,704,521
<pre><code>from django.utils.translation import ngettext def localize_timedelta(delta): ret = [] num_years = int(delta.days / 365) if num_years &gt; 0: delta -= timedelta(days=num_years * 365) ret.append(ngettext('%d year', '%d years', num_years) % num_years) if delta.days &gt; 0: ...
0
2016-09-26T13:41:45Z
[ "python", "string", "datetime", "format", "timedelta" ]
Parallel/Async Download of S3 data into EC2 in Python?
538,875
<p>I have large data files stored in S3 that I need to analyze. Each batch consists of ~50 files, each of which can be analyzed independently. </p> <p>I'd like to setup parallel downloads of the S3 data into the EC2 instance, and setup triggers that start the analysis process on each file that downloads.</p> <p>Are t...
1
2009-02-11T21:25:10Z
538,895
<p>It sounds like you're looking for <a href="http://twistedmatrix.com/trac/" rel="nofollow">twisted</a>:</p> <p>"Twisted is an event-driven networking engine written in Python and licensed under the MIT license."</p> <p><a href="http://twistedmatrix.com/trac/" rel="nofollow">http://twistedmatrix.com/trac/</a></p> <...
0
2009-02-11T21:28:41Z
[ "python", "amazon-s3", "amazon-ec2" ]
Parallel/Async Download of S3 data into EC2 in Python?
538,875
<p>I have large data files stored in S3 that I need to analyze. Each batch consists of ~50 files, each of which can be analyzed independently. </p> <p>I'd like to setup parallel downloads of the S3 data into the EC2 instance, and setup triggers that start the analysis process on each file that downloads.</p> <p>Are t...
1
2009-02-11T21:25:10Z
538,902
<p>I don't know of anything that already exists that does exactly what you're looking for, but even if not it should be reasonably easy to put together with Python. For a threaded approach, you might take a look at this <a href="http://code.activestate.com/recipes/284631/" rel="nofollow"><strong>Python recipe</strong><...
0
2009-02-11T21:30:34Z
[ "python", "amazon-s3", "amazon-ec2" ]
Parallel/Async Download of S3 data into EC2 in Python?
538,875
<p>I have large data files stored in S3 that I need to analyze. Each batch consists of ~50 files, each of which can be analyzed independently. </p> <p>I'd like to setup parallel downloads of the S3 data into the EC2 instance, and setup triggers that start the analysis process on each file that downloads.</p> <p>Are t...
1
2009-02-11T21:25:10Z
644,538
<p>Answering my own question, I ended up writing a simple modification to the Amazon S3 python library that lets you download the file in chunks or read it line by line. <a href="http://parand.com/say/index.php/2009/03/13/python-s3-library-for-chunked-streaming-download/" rel="nofollow">Available here</a>.</p>
2
2009-03-13T20:37:58Z
[ "python", "amazon-s3", "amazon-ec2" ]
Return a tuple of arguments to be fed to string.format()
539,066
<p>Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this:</p> <pre><code>class PairEvaluator(HandEvaluator): def returnArbitrary(self): return ('ace', 'king') pe = ...
21
2009-02-11T22:10:10Z
539,102
<p>This attempts to use "cards" as single format input to print, not the contents of cards.</p> <p>Try something like:</p> <pre><code>print('Two pair, %ss and %ss' % cards) </code></pre>
1
2009-02-11T22:20:26Z
[ "python", "string-formatting", "tuples" ]
Return a tuple of arguments to be fed to string.format()
539,066
<p>Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this:</p> <pre><code>class PairEvaluator(HandEvaluator): def returnArbitrary(self): return ('ace', 'king') pe = ...
21
2009-02-11T22:10:10Z
539,106
<pre><code>print('Two pair, {0}s and {1}s'.format(*cards)) </code></pre> <p>You are missing only the star :D</p>
53
2009-02-11T22:20:47Z
[ "python", "string-formatting", "tuples" ]
Return a tuple of arguments to be fed to string.format()
539,066
<p>Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this:</p> <pre><code>class PairEvaluator(HandEvaluator): def returnArbitrary(self): return ('ace', 'king') pe = ...
21
2009-02-11T22:10:10Z
13,582,206
<p>Format is preferred over the % operator, as of its introduction in Python 2.6: <a href="http://docs.python.org/2/library/stdtypes.html#str.format" rel="nofollow">http://docs.python.org/2/library/stdtypes.html#str.format</a></p> <p>It's also a lot simpler just to unpack the tuple with * -- or a dict with ** -- rathe...
2
2012-11-27T10:29:51Z
[ "python", "string-formatting", "tuples" ]
Context processor using Werkzeug and Jinja2
539,116
<p>My application is running on App Engine and is implemented using <a href="http://werkzeug.pocoo.org/" rel="nofollow">Werkzeug</a> and <a href="http://jinja.pocoo.org/2/" rel="nofollow">Jinja2</a>. I'd like to have something functionally equivalent of Django's own context processor: a callable that takes a request an...
4
2009-02-11T22:22:39Z
539,427
<p>One way of achieving this is through late-bound <a href="http://jinja.pocoo.org/2/documentation/api#jinja2.Environment.globals" rel="nofollow">template globals</a> using the <a href="http://werkzeug.pocoo.org/documentation/local" rel="nofollow">thread-local proxy</a> in Werkzeug.</p> <p>A simple example that puts t...
4
2009-02-11T23:55:13Z
[ "python", "django", "google-app-engine", "jinja2", "werkzeug" ]
Context processor using Werkzeug and Jinja2
539,116
<p>My application is running on App Engine and is implemented using <a href="http://werkzeug.pocoo.org/" rel="nofollow">Werkzeug</a> and <a href="http://jinja.pocoo.org/2/" rel="nofollow">Jinja2</a>. I'd like to have something functionally equivalent of Django's own context processor: a callable that takes a request an...
4
2009-02-11T22:22:39Z
562,828
<p>Well, using <a href="http://stackoverflow.com/questions/539116/context-processor-using-werkzeug-and-jinja2/539427#539427">what Ali wrote</a> I came to the solution that is specific to App Engine (because of its import cache). Unfortunately, Ali's code does not work with App Engine, because the code that sets Jinja g...
3
2009-02-18T21:02:52Z
[ "python", "django", "google-app-engine", "jinja2", "werkzeug" ]
Python - Test directory permissions
539,133
<p>In Python on Windows, is there a way to determine if a user has permission to access a directory? I've taken a look at <code>os.access</code> but it gives false results.</p> <pre><code>&gt;&gt;&gt; os.access('C:\haveaccess', os.R_OK) False &gt;&gt;&gt; os.access(r'C:\haveaccess', os.R_OK) True &gt;&gt;&gt; os.acce...
5
2009-02-11T22:26:47Z
539,159
<p>It can be complicated to check for permissions in Windows (beware of issues in Vista with UAC, for example! -- see this <a href="http://stackoverflow.com/questions/450210/how-to-check-if-a-file-can-be-created-inside-given-directory-on-ms-xp-vista">related question</a>).</p> <p>Are you talking about simple read acce...
6
2009-02-11T22:32:04Z
[ "python", "windows", "permissions", "directory" ]
Python - Test directory permissions
539,133
<p>In Python on Windows, is there a way to determine if a user has permission to access a directory? I've taken a look at <code>os.access</code> but it gives false results.</p> <pre><code>&gt;&gt;&gt; os.access('C:\haveaccess', os.R_OK) False &gt;&gt;&gt; os.access(r'C:\haveaccess', os.R_OK) True &gt;&gt;&gt; os.acce...
5
2009-02-11T22:26:47Z
539,269
<p>While os.access tries its best to tell if a path is accessible or not, it doesn't claim to be perfect. From the Python docs:</p> <blockquote> <p>Note: I/O operations may fail even when access() indicates that they would succeed, particularly for operations on network filesystems which may have permissions...
4
2009-02-11T23:04:01Z
[ "python", "windows", "permissions", "directory" ]
Python - Test directory permissions
539,133
<p>In Python on Windows, is there a way to determine if a user has permission to access a directory? I've taken a look at <code>os.access</code> but it gives false results.</p> <pre><code>&gt;&gt;&gt; os.access('C:\haveaccess', os.R_OK) False &gt;&gt;&gt; os.access(r'C:\haveaccess', os.R_OK) True &gt;&gt;&gt; os.acce...
5
2009-02-11T22:26:47Z
539,516
<p>Did you get '\d' characters in the non-raw? Did you mean "c:\haveaccess"</p>
0
2009-02-12T00:30:44Z
[ "python", "windows", "permissions", "directory" ]
Python - Test directory permissions
539,133
<p>In Python on Windows, is there a way to determine if a user has permission to access a directory? I've taken a look at <code>os.access</code> but it gives false results.</p> <pre><code>&gt;&gt;&gt; os.access('C:\haveaccess', os.R_OK) False &gt;&gt;&gt; os.access(r'C:\haveaccess', os.R_OK) True &gt;&gt;&gt; os.acce...
5
2009-02-11T22:26:47Z
2,445,812
<p>Actually 'C:\haveaccess' is different than r'C:\haveaccess'. From Python point of view 'C:\haveaccess' is not a valid path, so use 'C:\\haveaccess' instead. I think os.access works just fine.</p>
1
2010-03-15T08:23:53Z
[ "python", "windows", "permissions", "directory" ]
Background Image on Jython GUI
539,313
<p>I am trying to create a GUI in Jython. I want to import a background image that I can place buttons and textfields on. I've already created the frame with the buttons and labels in their appropriate places, I just need to know how to import a background image. The GUI is implemented in Jython.</p>
0
2009-02-11T23:17:52Z
539,346
<p>Take a look at the Java swing material, essentially you are just using the same api in python syntax. This might help: <a href="http://forums.sun.com/thread.jspa?threadID=599393" rel="nofollow">http://forums.sun.com/thread.jspa?threadID=599393</a></p>
1
2009-02-11T23:29:53Z
[ "python", "jython" ]
Connecting to MS SQL Server using python on linux with 'Windows Credentials'
539,430
<p>Is there any way to connect to an MS SQL Server database with python on linux using Windows Domain Credentials?</p> <p>I can connect perfectly fine from my windows machine using Windows Credentials, but attempting to do the same from a linux python with pyodbs + freetds + unixodbc </p> <pre><code>&gt;&gt;import py...
4
2009-02-11T23:59:39Z
539,550
<p>I don't believe you'll be able to log in to a windows domain account in this way. You need to set up a user in sql directly for this manner of passing credentials.</p>
0
2009-02-12T00:46:42Z
[ "python", "sql-server", "pyodbc", "freetds", "unixodbc" ]
Connecting to MS SQL Server using python on linux with 'Windows Credentials'
539,430
<p>Is there any way to connect to an MS SQL Server database with python on linux using Windows Domain Credentials?</p> <p>I can connect perfectly fine from my windows machine using Windows Credentials, but attempting to do the same from a linux python with pyodbs + freetds + unixodbc </p> <pre><code>&gt;&gt;import py...
4
2009-02-11T23:59:39Z
539,593
<p>I haven't done it in a while, but I remember the whole unixodbc + FreeTDS + pyodbc thing being a little tricky. However, it can be done, and once setup it's not that hard.</p> <p>This website provides very good instructions: <a href="http://web.archive.org/web/20150214152131/http://www.pauldeden.com/2008/12/how-to...
1
2009-02-12T01:05:14Z
[ "python", "sql-server", "pyodbc", "freetds", "unixodbc" ]
Connecting to MS SQL Server using python on linux with 'Windows Credentials'
539,430
<p>Is there any way to connect to an MS SQL Server database with python on linux using Windows Domain Credentials?</p> <p>I can connect perfectly fine from my windows machine using Windows Credentials, but attempting to do the same from a linux python with pyodbs + freetds + unixodbc </p> <pre><code>&gt;&gt;import py...
4
2009-02-11T23:59:39Z
953,930
<p><strong>As pointed out in one of the comments, this answer is quite stale by now. I regularly and routinely use GSSAPI to authenticate from Linux to SQL Server 2008 R2 but mostly with the EasySoft ODBC manager and the (commercial) EasySoft ODBC SQL Server driver.</strong></p> <p>In early 2009, a colleague and I man...
4
2009-06-05T01:47:56Z
[ "python", "sql-server", "pyodbc", "freetds", "unixodbc" ]
Connecting to MS SQL Server using python on linux with 'Windows Credentials'
539,430
<p>Is there any way to connect to an MS SQL Server database with python on linux using Windows Domain Credentials?</p> <p>I can connect perfectly fine from my windows machine using Windows Credentials, but attempting to do the same from a linux python with pyodbs + freetds + unixodbc </p> <pre><code>&gt;&gt;import py...
4
2009-02-11T23:59:39Z
5,832,263
<p>Probably a bit too late to help you out - but I encountered the same issue. At the time of writing, the latest version of pyodbc allows me to login with windows credentials. Just leave the UID field blank in your connection string like so:</p> <pre><code>cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=myserverins...
1
2011-04-29T12:49:35Z
[ "python", "sql-server", "pyodbc", "freetds", "unixodbc" ]
Connecting to MS SQL Server using python on linux with 'Windows Credentials'
539,430
<p>Is there any way to connect to an MS SQL Server database with python on linux using Windows Domain Credentials?</p> <p>I can connect perfectly fine from my windows machine using Windows Credentials, but attempting to do the same from a linux python with pyodbs + freetds + unixodbc </p> <pre><code>&gt;&gt;import py...
4
2009-02-11T23:59:39Z
15,551,792
<p>As of at least March 2013, this seems to work out of the box with FreeTDS. I specified the <a href="http://www.freetds.org/userguide/choosingtdsprotocol.htm" rel="nofollow">TDS protocol version</a> for good measure--not sure if that makes the difference:</p> <pre><code>connStr = "DRIVER={{FreeTDS}};SERVER={0};PORT=...
2
2013-03-21T15:34:02Z
[ "python", "sql-server", "pyodbc", "freetds", "unixodbc" ]
Namespace Specification In Absence of Ambuguity
539,578
<p>Why do some languages, like C++ and Python, require the namespace of an object be specified even when no ambiguity exists? I understand that there are backdoors to this, like <code>using namespace x</code> in C++, or <code>from x import *</code> in Python. However, I can't understand the rationale behind not wanti...
0
2009-02-12T00:56:34Z
539,585
<p>This all depends on your definition of "right thing". Is it the right thing for the compiler to guess your intention if there's only one match?</p> <p>There are arguments for both sides.</p>
0
2009-02-12T01:01:40Z
[ "c++", "python", "namespaces", "language-design" ]
Namespace Specification In Absence of Ambuguity
539,578
<p>Why do some languages, like C++ and Python, require the namespace of an object be specified even when no ambiguity exists? I understand that there are backdoors to this, like <code>using namespace x</code> in C++, or <code>from x import *</code> in Python. However, I can't understand the rationale behind not wanti...
0
2009-02-12T00:56:34Z
539,591
<p>One reason is to protect against accidentally introducing a conflict when you change the code (or for an external module/library, when someone else changes it) later on. For example, in Python you can write</p> <pre><code>from foo import * from bar import * </code></pre> <p>without conflicts if you know that modul...
11
2009-02-12T01:04:47Z
[ "c++", "python", "namespaces", "language-design" ]
Namespace Specification In Absence of Ambuguity
539,578
<p>Why do some languages, like C++ and Python, require the namespace of an object be specified even when no ambiguity exists? I understand that there are backdoors to this, like <code>using namespace x</code> in C++, or <code>from x import *</code> in Python. However, I can't understand the rationale behind not wanti...
0
2009-02-12T00:56:34Z
539,595
<p>There have been languages where the compiler tried to "do the right thing" - Algol and PL/I come to mind. The reason they are not around anymore is that compilers are very bad at doing the right thing, but very good at doing the wrong one, given half a chance!</p>
4
2009-02-12T01:06:29Z
[ "c++", "python", "namespaces", "language-design" ]
Namespace Specification In Absence of Ambuguity
539,578
<p>Why do some languages, like C++ and Python, require the namespace of an object be specified even when no ambiguity exists? I understand that there are backdoors to this, like <code>using namespace x</code> in C++, or <code>from x import *</code> in Python. However, I can't understand the rationale behind not wanti...
0
2009-02-12T00:56:34Z
539,600
<p>The ideal this rule strives for is to make creating reusable components easy - and if you reuse your component, you just don't know which symbols will be defined in other namespaces the client uses. So the rule forces you to make your intention clear with respect to further definitions you don't know about yet.</p> ...
1
2009-02-12T01:10:07Z
[ "c++", "python", "namespaces", "language-design" ]
Namespace Specification In Absence of Ambuguity
539,578
<p>Why do some languages, like C++ and Python, require the namespace of an object be specified even when no ambiguity exists? I understand that there are backdoors to this, like <code>using namespace x</code> in C++, or <code>from x import *</code> in Python. However, I can't understand the rationale behind not wanti...
0
2009-02-12T00:56:34Z
539,604
<p>Python takes the view that 'explicit is better than implicit'. (type <code>import this</code> into a python interpreter)</p> <p>Also, say I'm reading someone's code. Perhaps it's your code; perhaps it's my code from six months ago. I see a reference to <code>bar()</code>. Where did the function come from? I cou...
11
2009-02-12T01:10:48Z
[ "c++", "python", "namespaces", "language-design" ]
Namespace Specification In Absence of Ambuguity
539,578
<p>Why do some languages, like C++ and Python, require the namespace of an object be specified even when no ambiguity exists? I understand that there are backdoors to this, like <code>using namespace x</code> in C++, or <code>from x import *</code> in Python. However, I can't understand the rationale behind not wanti...
0
2009-02-12T00:56:34Z
539,621
<p>The problem is about abstraction and reuse : <strong>you don't really know if there will not be any future ambiguity</strong>. </p> <p>For example, It's very common to setup different libraries in a project just to discover that they all have their own string class implementation, called "string". You compiler will...
4
2009-02-12T01:20:32Z
[ "c++", "python", "namespaces", "language-design" ]
Namespace Specification In Absence of Ambuguity
539,578
<p>Why do some languages, like C++ and Python, require the namespace of an object be specified even when no ambiguity exists? I understand that there are backdoors to this, like <code>using namespace x</code> in C++, or <code>from x import *</code> in Python. However, I can't understand the rationale behind not wanti...
0
2009-02-12T00:56:34Z
539,643
<p>Is it really the right thing?</p> <p>What if I have two types ::bat and ::foo::bar</p> <p>I want to reference the bat type but accidentally hit the r key instead of t (they're right next to each others).</p> <p>Is it "the right thing" for the compiler to then go searching through every namespace to find ::foo::ba...
1
2009-02-12T01:31:41Z
[ "c++", "python", "namespaces", "language-design" ]