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 |
|---|---|---|---|---|---|---|---|---|---|
How do you return multiple values in Python? | 354,883 | <p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p>
<h3>Option: Using a tuple</h3>
<p>Consider this trivial example:</p>
<pre><code>def f(x):
... | 435 | 2008-12-10T01:55:50Z | 18,218,465 | <p>Python's tuples, dicts, and objects offer the programmer a smooth tradeoff between formality and convenience for small data structures ("things"). For me, the choice of how to represent a thing is dictated mainly by how I'm going to use the structure. In C++, it's a common convention to use <code>struct</code> for d... | 5 | 2013-08-13T20:18:55Z | [
"python",
"coding-style",
"return",
"return-value"
] |
How do you return multiple values in Python? | 354,883 | <p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p>
<h3>Option: Using a tuple</h3>
<p>Consider this trivial example:</p>
<pre><code>def f(x):
... | 435 | 2008-12-10T01:55:50Z | 21,970,184 | <p>Another option would be using generators:</p>
<pre><code>>>> def f(x):
y0 = x + 1
yield y0
yield x * 3
yield y0 ** 4
>>> a, b, c = f(5)
>>> a
6
>>> b
15
>>> c
1296
</code></pre>
<p>Although IMHO tuples are usually best, except in cases ... | 13 | 2014-02-23T15:26:30Z | [
"python",
"coding-style",
"return",
"return-value"
] |
How do you return multiple values in Python? | 354,883 | <p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p>
<h3>Option: Using a tuple</h3>
<p>Consider this trivial example:</p>
<pre><code>def f(x):
... | 435 | 2008-12-10T01:55:50Z | 28,076,666 | <pre><code>>>> def func():
... return [1,2,3]
...
>>> a,b,c = func()
>>> a
1
>>> b
2
>>> c
3
</code></pre>
| 11 | 2015-01-21T20:57:30Z | [
"python",
"coding-style",
"return",
"return-value"
] |
How do you return multiple values in Python? | 354,883 | <p>The canonical way to return multiple values in languages that support it is often <a href="http://stackoverflow.com/questions/38508/whats-the-best-way-to-return-multiple-values-from-a-function-in-python">tupling</a>. </p>
<h3>Option: Using a tuple</h3>
<p>Consider this trivial example:</p>
<pre><code>def f(x):
... | 435 | 2008-12-10T01:55:50Z | 36,632,980 | <p>A lot of the answers seem to suggest you need to return a collection of some sort, like a dictionary or a list. You could also leave off the extra syntax and just write out the return values, comma-separated. Note: this technically returns a tuple.</p>
<pre><code>def f():
return True, False
x, y = f()
print x
p... | 9 | 2016-04-14T20:08:13Z | [
"python",
"coding-style",
"return",
"return-value"
] |
mssql handles line returns rather awkwardly | 355,135 | <p>Here is the problem: </p>
<p>for your reference:</p>
<p><img src="http://i.stack.imgur.com/mmrNH.jpg" alt="http://i.stack.imgur.com/mmrNH.jpg"></p>
<p>database entries 1,2 and 3 are made using jython 2.2.1 using jdbc1.2.
database entry 4 is made using vb the old to be replace program using odbc.</p>
<p>We have f... | 0 | 2008-12-10T05:10:48Z | 355,158 | <p>I suggest to add a debug output to your program, dumping character codes before insertion in DB. There are chances that Jython replace CrLf pair with single character and doesn't restore it when written to DB.</p>
| 1 | 2008-12-10T05:36:27Z | [
"java",
"python",
"sql-server",
"formatting",
"jython"
] |
mssql handles line returns rather awkwardly | 355,135 | <p>Here is the problem: </p>
<p>for your reference:</p>
<p><img src="http://i.stack.imgur.com/mmrNH.jpg" alt="http://i.stack.imgur.com/mmrNH.jpg"></p>
<p>database entries 1,2 and 3 are made using jython 2.2.1 using jdbc1.2.
database entry 4 is made using vb the old to be replace program using odbc.</p>
<p>We have f... | 0 | 2008-12-10T05:10:48Z | 362,847 | <p>You should look at the quopri module (and others regarding email) so you don't have to use dirty tricks as <code>_clean_body</code></p>
| 1 | 2008-12-12T14:06:49Z | [
"java",
"python",
"sql-server",
"formatting",
"jython"
] |
A get() like method for checking for Python attributes | 355,539 | <p>If I had a dictionary <code>dict</code> and I wanted to check for <code>dict['key']</code> I could either do so in a <code>try</code> block (bleh!) or use the <code>get()</code> method, with <code>False</code> as a default value.</p>
<p>I'd like to do the same thing for <code>object.attribute</code>. That is, I alr... | 37 | 2008-12-10T09:49:39Z | 355,562 | <p>Do you mean <code>hasattr()</code> perhaps?</p>
<pre><code>hasattr(object, "attribute name") #Returns True or False
</code></pre>
<p><a href="http://docs.python.org/library/functions.html#hasattr" rel="nofollow">Python.org doc - Built in functions - hasattr()</a></p>
<p>You can also do this, which is a bit more c... | 15 | 2008-12-10T09:59:36Z | [
"python",
"attributes"
] |
A get() like method for checking for Python attributes | 355,539 | <p>If I had a dictionary <code>dict</code> and I wanted to check for <code>dict['key']</code> I could either do so in a <code>try</code> block (bleh!) or use the <code>get()</code> method, with <code>False</code> as a default value.</p>
<p>I'd like to do the same thing for <code>object.attribute</code>. That is, I alr... | 37 | 2008-12-10T09:49:39Z | 355,571 | <p>For checking if a key is in a dictionary you can use <code>in</code>: <code>'key' in dictionary</code>.</p>
<p>For checking for attributes in object use the <code>hasattr()</code> function: <code>hasattr(obj, 'attribute')</code></p>
| 5 | 2008-12-10T10:01:28Z | [
"python",
"attributes"
] |
A get() like method for checking for Python attributes | 355,539 | <p>If I had a dictionary <code>dict</code> and I wanted to check for <code>dict['key']</code> I could either do so in a <code>try</code> block (bleh!) or use the <code>get()</code> method, with <code>False</code> as a default value.</p>
<p>I'd like to do the same thing for <code>object.attribute</code>. That is, I alr... | 37 | 2008-12-10T09:49:39Z | 356,227 | <p>A more direct analogue to <code>dict.get(key, default)</code> than <code>hasattr</code> is <code>getattr</code>.</p>
<pre><code>val = getattr(obj, 'attr_to_check', default_value)
</code></pre>
<p>(Where <code>default_value</code> is optional, raising an exception on no attribute if not found.)</p>
<p>For your exa... | 58 | 2008-12-10T14:27:29Z | [
"python",
"attributes"
] |
Save a deque in a text file | 355,739 | <p>I am writing a crawler in Python, in order to make Ctrl+C not to cause my crawler to start over in next run, I need to save the processing deque in a text file (one item per line) and update it every iteration, the update operation needs to be super fast. In order not to reinvent the wheel, I am asking if there is a... | 1 | 2008-12-10T11:23:19Z | 355,760 | <p>As an alternative, you could set up an exit function, and pickle the deque on exit.</p>
<p><a href="http://docs.python.org/library/sys.html#sys.exitfunc" rel="nofollow">Exit function</a><br>
<a href="http://docs.python.org/library/pickle.html" rel="nofollow">Pickle</a></p>
| 4 | 2008-12-10T11:29:20Z | [
"python",
"web-crawler"
] |
Save a deque in a text file | 355,739 | <p>I am writing a crawler in Python, in order to make Ctrl+C not to cause my crawler to start over in next run, I need to save the processing deque in a text file (one item per line) and update it every iteration, the update operation needs to be super fast. In order not to reinvent the wheel, I am asking if there is a... | 1 | 2008-12-10T11:23:19Z | 355,764 | <p>You should be able to use <a href="http://docs.python.org/library/pickle.html" rel="nofollow">pickle</a> to serialize your lists.</p>
| 1 | 2008-12-10T11:30:32Z | [
"python",
"web-crawler"
] |
Save a deque in a text file | 355,739 | <p>I am writing a crawler in Python, in order to make Ctrl+C not to cause my crawler to start over in next run, I need to save the processing deque in a text file (one item per line) and update it every iteration, the update operation needs to be super fast. In order not to reinvent the wheel, I am asking if there is a... | 1 | 2008-12-10T11:23:19Z | 355,788 | <p>Some things that come to my mind:</p>
<ul>
<li>leave the file handle open (don't close the file everytime you wrote something)</li>
<li>or write the file every n items and catch a close signal to write the current non-written items</li>
</ul>
| 0 | 2008-12-10T11:40:00Z | [
"python",
"web-crawler"
] |
Save a deque in a text file | 355,739 | <p>I am writing a crawler in Python, in order to make Ctrl+C not to cause my crawler to start over in next run, I need to save the processing deque in a text file (one item per line) and update it every iteration, the update operation needs to be super fast. In order not to reinvent the wheel, I am asking if there is a... | 1 | 2008-12-10T11:23:19Z | 355,992 | <p>I am not sure if I understood the question right, I am just curious, so here are few questions and suggestions:</p>
<p>Are you planning to catch the Ctrl+C interrupt and do the deque?
What happens if the crawler crashes for some arbitrary reason like an unhandled exception or crash? You loose the queue status and s... | 1 | 2008-12-10T13:13:45Z | [
"python",
"web-crawler"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 356,102 | <p>Try converting the exponent to a floating number, as the default behaviour of / in Python is integer division</p>
<p>n**(1/float(3))</p>
| -1 | 2008-12-10T13:54:39Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 356,110 | <p>In older versions of Python, <code>1/3</code> is equal to 0. In Python 3.0, <code>1/3</code> is equal to 0.33333333333 (and <code>1//3</code> is equal to 0).</p>
<p>So, either change your code to use <code>1/3.0</code> or switch to Python 3.0 .</p>
| 2 | 2008-12-10T13:57:42Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 356,187 | <p><a href="http://code.google.com/p/gmpy/">Gmpy</a> is a C-coded Python extension module that wraps the GMP library to provide to Python code fast multiprecision arithmetic (integer, rational, and float), random number generation, advanced number-theoretical functions, and more.</p>
<p>Includes a <code>root</code> fu... | 14 | 2008-12-10T14:17:08Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 356,206 | <p>If it's a REALLY big number. You could use a binary search.</p>
<pre><code>def find_invpow(x,n):
"""Finds the integer component of the n'th root of x,
an integer such that y ** n <= x < (y + 1) ** n.
"""
high = 1
while high ** n <= x:
high *= 2
low = high/2
while low <... | 16 | 2008-12-10T14:22:21Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 356,213 | <p>Well, if you're not particularly worried about precision, you could convert it to a sting, chop off some digits, use the exponent function, and then multiply the result by the root of how much you chopped off.</p>
<p>E.g. 32123 is about equal to 32 * 1000, the cubic root is about equak to cubic root of 32 * cubic r... | -2 | 2008-12-10T14:23:20Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 356,235 | <p>Oh, for numbers <em>that</em> big, you would use the decimal module.</p>
<p>ns: your number as a string</p>
<pre><code>ns = "1196800396603096435688561148038340883317234645046733925119609314414104568346308529111567748841162026482694233489799638948504626284726576928088323764946112247973427942441686183439652281915921... | 3 | 2008-12-10T14:29:36Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 358,026 | <p>Possibly for your curiosity:</p>
<p><a href="http://en.wikipedia.org/wiki/Hensel_Lifting" rel="nofollow">http://en.wikipedia.org/wiki/Hensel_Lifting</a></p>
<p>This could be the technique that Maple would use to actually find the nth root of large numbers.</p>
<p>Pose the fact that <code>x^n - 11968003.... = 0 mo... | 2 | 2008-12-10T23:42:49Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 358,134 | <p>You can make it run slightly faster by avoiding the while loops in favor of setting low to 10 ** (len(str(x)) / n) and high to low * 10. Probably better is to replace the len(str(x)) with the bitwise length and using a bit shift. Based on my tests, I estimate a 5% speedup from the first and a 25% speedup from the ... | 4 | 2008-12-11T00:44:34Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 637,321 | <p>If you are looking for something standard, fast to write with high precision. I would use decimal and adjust the precision (getcontext().prec) to at least the length of x.</p>
<h2>Code (Python 3.0)</h2>
<pre><code>from decimal import *
x = '11968003966030964356885611480383408833172346450467339251\
1960931441410... | 6 | 2009-03-12T04:04:59Z | [
"python",
"math",
"nth-root"
] |
How to compute the nth root of a very big integer | 356,090 | <p>I need a way to compute the nth root of a long integer in Python. </p>
<p>I tried <code>pow(m, 1.0/n)</code>, but it doesn't work:</p>
<blockquote>
<p>OverflowError: long int too large to convert to float</p>
</blockquote>
<p>Any ideas?</p>
<p>By long integer I mean REALLY long integers like:</p>
<blockquote>... | 17 | 2008-12-10T13:49:19Z | 37,171,151 | <p>I came up with my own answer, which takes @Mahmoud Kassem's idea, simplifies the code, and makes it more reusable:</p>
<pre><code>def cube_root(x):
return decimal.Decimal(x) ** (decimal.Decimal(1) / decimal.Decimal(3))
</code></pre>
<p>I tested it in Python 3.5.1 and Python 2.7.8, and it seemed to work fine.</... | 0 | 2016-05-11T18:55:54Z | [
"python",
"math",
"nth-root"
] |
Python FastCGI under IIS - stdout writing problems | 356,138 | <p>I'm having a very peculiar problem in my Python FastCGI code - sys.stdout has a file descriptor of '-1', so I can't write to it.
I'm checking this at the first line of my program, so I know it's not any of my code changing it.</p>
<p>I've tried <code>sys.stdout = os.fdopen(1, 'w')</code>, but anything written there... | 2 | 2008-12-10T14:04:51Z | 359,330 | <p>Forgive me if this is a dumb question, but I notice this line in your config file:</p>
<blockquote>
<p>Arguments=-u C:\app\app_wsgi.py</p>
</blockquote>
<p>Are you running a WSGI application or a FastCGI app? There <em>is</em> a difference. In WSGI, writing to stdout isn't a good idea. Your program should hav... | 1 | 2008-12-11T13:24:48Z | [
"python",
"iis",
"stdout",
"fastcgi"
] |
Python FastCGI under IIS - stdout writing problems | 356,138 | <p>I'm having a very peculiar problem in my Python FastCGI code - sys.stdout has a file descriptor of '-1', so I can't write to it.
I'm checking this at the first line of my program, so I know it's not any of my code changing it.</p>
<p>I've tried <code>sys.stdout = os.fdopen(1, 'w')</code>, but anything written there... | 2 | 2008-12-10T14:04:51Z | 359,636 | <p>On windows, it's possible to launch a proces without a valid stdin and stdout. For example, if you execute a python script with pythonw.exe, the stdout is invdalid and if you insist on writing to it, it will block after 140 characters or something.</p>
<p>Writing to another destination than stdout looks like the sa... | 0 | 2008-12-11T15:01:21Z | [
"python",
"iis",
"stdout",
"fastcgi"
] |
Python FastCGI under IIS - stdout writing problems | 356,138 | <p>I'm having a very peculiar problem in my Python FastCGI code - sys.stdout has a file descriptor of '-1', so I can't write to it.
I'm checking this at the first line of my program, so I know it's not any of my code changing it.</p>
<p>I've tried <code>sys.stdout = os.fdopen(1, 'w')</code>, but anything written there... | 2 | 2008-12-10T14:04:51Z | 1,328,558 | <p>Following the PEP 333 you can try to log to environ['wsgi.errors'] which is usualy the logger of the web server itself when you use fastcgi. Of course this is only available when a request is called but not during application startup.</p>
<p>You can get an example in the pylons code: <a href="http://pylonshq.com/do... | 0 | 2009-08-25T14:14:01Z | [
"python",
"iis",
"stdout",
"fastcgi"
] |
Python FastCGI under IIS - stdout writing problems | 356,138 | <p>I'm having a very peculiar problem in my Python FastCGI code - sys.stdout has a file descriptor of '-1', so I can't write to it.
I'm checking this at the first line of my program, so I know it's not any of my code changing it.</p>
<p>I've tried <code>sys.stdout = os.fdopen(1, 'w')</code>, but anything written there... | 2 | 2008-12-10T14:04:51Z | 1,500,628 | <p>Do you have to use FastCGI? If not, you may want to try a ISAPI WSGI method. I have had success using:</p>
<p><a href="http://code.google.com/p/isapi-wsgi/" rel="nofollow">http://code.google.com/p/isapi-wsgi/</a></p>
<p>and have also used PyISAPIe in the past:</p>
<p><a href="http://sourceforge.net/apps/trac/py... | 1 | 2009-09-30T20:58:52Z | [
"python",
"iis",
"stdout",
"fastcgi"
] |
Python FastCGI under IIS - stdout writing problems | 356,138 | <p>I'm having a very peculiar problem in my Python FastCGI code - sys.stdout has a file descriptor of '-1', so I can't write to it.
I'm checking this at the first line of my program, so I know it's not any of my code changing it.</p>
<p>I've tried <code>sys.stdout = os.fdopen(1, 'w')</code>, but anything written there... | 2 | 2008-12-10T14:04:51Z | 5,493,892 | <p>I believe having stdout closed/invalid is in accordance to the <a href="http://www.fastcgi.com/devkit/doc/fcgi-spec.html" rel="nofollow">FastCGI spec</a>:</p>
<blockquote>
<p>The Web server leaves a single file
descriptor, FCGI_LISTENSOCK_FILENO,
open when the application begins
execution. This descriptor r... | 1 | 2011-03-31T00:05:02Z | [
"python",
"iis",
"stdout",
"fastcgi"
] |
Minimal Python build for my application's scripting needs? | 356,452 | <p>what are your advices on building a very minimalistic version of Python(2.x) for my application's scripting needs. </p>
<p>My main motive here is to keep the foot print (both memory and disk wise) as low as possible so that my native application won't suffer from any major performance hit. Even the Python DLL size ... | 2 | 2008-12-10T15:22:41Z | 356,579 | <p>Have you tried <a href="http://www.tinypy.org/" rel="nofollow">Tiny Python</a>?</p>
| 7 | 2008-12-10T16:00:10Z | [
"python",
"scripting",
"python-embedding"
] |
Minimal Python build for my application's scripting needs? | 356,452 | <p>what are your advices on building a very minimalistic version of Python(2.x) for my application's scripting needs. </p>
<p>My main motive here is to keep the foot print (both memory and disk wise) as low as possible so that my native application won't suffer from any major performance hit. Even the Python DLL size ... | 2 | 2008-12-10T15:22:41Z | 2,744,197 | <p>You can use only the dll</p>
| -1 | 2010-04-30T11:39:18Z | [
"python",
"scripting",
"python-embedding"
] |
Python regex findall numbers and dots | 356,483 | <p>I'm using re.findall() to extract some version numbers from an HTML file:</p>
<pre><code>>>> import re
>>> text = "<table><td><a href=\"url\">Test0.2.1.zip</a></td><td>Test0.2.1</td></table> Test0.2.1"
>>> re.findall("Test([\.0-9]*)", text)
... | 6 | 2008-12-10T15:33:09Z | 356,494 | <pre><code>re.findall(r"Test([0-9.]*[0-9]+)", text)
</code></pre>
<p>or, a bit shorter:</p>
<pre><code>re.findall(r"Test([\d.]*\d+)", text)
</code></pre>
<p>By the way - you must not escape the dot in a character class:</p>
<pre><code>[\.0-9] // matches: 0 1 2 3 4 5 6 7 8 9 . \
[.0-9] // matches: 0 1 2 3 4 5 6 7... | 12 | 2008-12-10T15:36:07Z | [
"python",
"regex",
"findall"
] |
How would you parse indentation (python style)? | 356,638 | <p>How would you define your parser and lexer rules to parse a language that uses indentation for defining scope.</p>
<p>I have already googled and found a clever approach for parsing it by generating INDENT and DEDENT tokens in the lexer.</p>
<p>I will go deeper on this problem and post an answer if I come to someth... | 9 | 2008-12-10T16:17:25Z | 356,838 | <p>Also you can track somewhere in lexer how many ident items are preceding first line and pass it to parser. Most interesting part would be trying to pass it to parser correctly :) If your parser uses lookahead (here I mean parser may query for variable number of tokens before it really going to match even one) then t... | 1 | 2008-12-10T17:18:57Z | [
"python",
"parsing",
"indentation",
"lexer"
] |
How would you parse indentation (python style)? | 356,638 | <p>How would you define your parser and lexer rules to parse a language that uses indentation for defining scope.</p>
<p>I have already googled and found a clever approach for parsing it by generating INDENT and DEDENT tokens in the lexer.</p>
<p>I will go deeper on this problem and post an answer if I come to someth... | 9 | 2008-12-10T16:17:25Z | 356,954 | <p>This is kind of hypothetical, as it would depend on what technology you have for your lexer and parser, but the easiest way would seem to be to have BEGINBLOCK and ENDBLOCK tokens analogous to braces in C. Using the <a href="http://en.wikipedia.org/wiki/Off-side_rule">"offsides rule"</a> your lexer needs to keep tr... | 10 | 2008-12-10T17:48:55Z | [
"python",
"parsing",
"indentation",
"lexer"
] |
How to handle constructors or methods with a different set (or type) of arguments in Python? | 356,718 | <p>Is there a way in Python, to have more than one constructor or more than one method with the <em>same name</em>, who differ in the <em>number of arguments</em> they accept or the <em>type(s) of one or more argument(s)</em>?</p>
<p>If not, what would be the best way to handle such situations?</p>
<p>For an example ... | 14 | 2008-12-10T16:42:08Z | 356,782 | <p>You can check the type of the argument passed to your constructor inside:</p>
<pre><code>def __init__(self, r = 0, g = 0, b = 0):
# if r is a list
if (type(r) == type([1,2,3])):
r, g, b = r[0], r[1], r[2]
# if r is a color
if (type(r) == type(self)):
r, g, b = r.r, r.g, r.b
self.... | -1 | 2008-12-10T17:05:22Z | [
"python"
] |
How to handle constructors or methods with a different set (or type) of arguments in Python? | 356,718 | <p>Is there a way in Python, to have more than one constructor or more than one method with the <em>same name</em>, who differ in the <em>number of arguments</em> they accept or the <em>type(s) of one or more argument(s)</em>?</p>
<p>If not, what would be the best way to handle such situations?</p>
<p>For an example ... | 14 | 2008-12-10T16:42:08Z | 356,820 | <p>You can have the factory methods, it is fine. But why not just call it as it is?</p>
<pre><code>Color(r, g, b)
Color(*[r, g, b])
Color(**{'r': r, 'g': g, 'b': b})
</code></pre>
<p>This is the python way. As for the from object constructor, I would prefer something like:</p>
<pre><code>Color(*Color2.as_list())
</c... | 10 | 2008-12-10T17:14:02Z | [
"python"
] |
How to handle constructors or methods with a different set (or type) of arguments in Python? | 356,718 | <p>Is there a way in Python, to have more than one constructor or more than one method with the <em>same name</em>, who differ in the <em>number of arguments</em> they accept or the <em>type(s) of one or more argument(s)</em>?</p>
<p>If not, what would be the best way to handle such situations?</p>
<p>For an example ... | 14 | 2008-12-10T16:42:08Z | 357,004 | <p>Python doesn't accept multiple methods with the same name, period. One method does one thing.</p>
<p>I've seen different approaches recommended on how to handle this ... classmethods (like you outlined above) or factory functions. I like keyword arguments the most.</p>
<pre><code>class Color (object):
def __in... | 7 | 2008-12-10T18:02:06Z | [
"python"
] |
How to handle constructors or methods with a different set (or type) of arguments in Python? | 356,718 | <p>Is there a way in Python, to have more than one constructor or more than one method with the <em>same name</em>, who differ in the <em>number of arguments</em> they accept or the <em>type(s) of one or more argument(s)</em>?</p>
<p>If not, what would be the best way to handle such situations?</p>
<p>For an example ... | 14 | 2008-12-10T16:42:08Z | 357,006 | <p>Python always fully replaces methods with the same name. Unlike C# that, if I remember correctly, will make the methods with the same name options for different argument input.</p>
<p>If there's only a variation of one in the keywords, like either 3 or 4 arguments of the same type, I'd say using a preset of the las... | 0 | 2008-12-10T18:02:28Z | [
"python"
] |
How to handle constructors or methods with a different set (or type) of arguments in Python? | 356,718 | <p>Is there a way in Python, to have more than one constructor or more than one method with the <em>same name</em>, who differ in the <em>number of arguments</em> they accept or the <em>type(s) of one or more argument(s)</em>?</p>
<p>If not, what would be the best way to handle such situations?</p>
<p>For an example ... | 14 | 2008-12-10T16:42:08Z | 357,256 | <p>In general, use factory methods, marked up as <code>@classmethod</code>s. They'll also work correctly on subclasses. From a design perspective, they are more explicit, especially when given a good name. </p>
<p>In this case, mixing everything together is probably more convenient, but it also makes the contract for ... | 9 | 2008-12-10T19:22:09Z | [
"python"
] |
How to handle constructors or methods with a different set (or type) of arguments in Python? | 356,718 | <p>Is there a way in Python, to have more than one constructor or more than one method with the <em>same name</em>, who differ in the <em>number of arguments</em> they accept or the <em>type(s) of one or more argument(s)</em>?</p>
<p>If not, what would be the best way to handle such situations?</p>
<p>For an example ... | 14 | 2008-12-10T16:42:08Z | 357,288 | <p>On the <code>__add__</code> issue:</p>
<p>First, you cannot get "three integers", I assume you mean a 3-tuple of integers?</p>
<p>In that case, you won't get around some <code>isinstance</code> calls:</p>
<pre><code>def __add__(self, other):
if isinstance(other, Color):
...
elif isinstance(other, ... | 2 | 2008-12-10T19:30:23Z | [
"python"
] |
How to handle constructors or methods with a different set (or type) of arguments in Python? | 356,718 | <p>Is there a way in Python, to have more than one constructor or more than one method with the <em>same name</em>, who differ in the <em>number of arguments</em> they accept or the <em>type(s) of one or more argument(s)</em>?</p>
<p>If not, what would be the best way to handle such situations?</p>
<p>For an example ... | 14 | 2008-12-10T16:42:08Z | 7,781,667 | <p>My first advise is to use factory methods.</p>
<p>Although if you really want a single method, give it something to dispatch on for
handling the parameters. </p>
<pre><code>def __init__(self, method, *args, **kw):
getattr(self, '_init_'+method)(*args, **kw)
def _init_coponents(self, r, g, b):
...
def _ini... | 1 | 2011-10-16T00:54:16Z | [
"python"
] |
Pexpect, running ssh-copy-id is hanging when trying to spawn a second process | 356,830 | <p>I'm doing a Python script where I need to spawn several ssh-copy-id processes, and they need for me to type in a password, so i'm using PExpect.</p>
<p>I have basically this:</p>
<pre><code>child = pexpect.spawn('command')
child.expect('password:')
child.sendline('the password')
</code></pre>
<p>and then I want t... | 6 | 2008-12-10T17:16:31Z | 357,021 | <p>Reading <a href="http://pexpect.sourceforge.net/pexpect.html#spawn" rel="nofollow">pexpect documentation for spawn</a>, I think it is waiting for the command to terminate.</p>
<p>I would suggest a couple of different possibilities, depending on your needs:</p>
<p>1) Kill the spawned process. However, this may lead... | 0 | 2008-12-10T18:10:05Z | [
"python",
"process",
"pexpect"
] |
Pexpect, running ssh-copy-id is hanging when trying to spawn a second process | 356,830 | <p>I'm doing a Python script where I need to spawn several ssh-copy-id processes, and they need for me to type in a password, so i'm using PExpect.</p>
<p>I have basically this:</p>
<pre><code>child = pexpect.spawn('command')
child.expect('password:')
child.sendline('the password')
</code></pre>
<p>and then I want t... | 6 | 2008-12-10T17:16:31Z | 357,107 | <p>Actually, I tried many of these alternatives, and neither worked. </p>
<ul>
<li>Calling close() or terminate() hangs (both with True and False as parameters)</li>
<li>Calling wait() or read(-1) or expect(pexpect.EOF) hangs</li>
<li>calling spawn again without caring about the previous spawn command hangs</li>
</ul... | 0 | 2008-12-10T18:43:21Z | [
"python",
"process",
"pexpect"
] |
Pexpect, running ssh-copy-id is hanging when trying to spawn a second process | 356,830 | <p>I'm doing a Python script where I need to spawn several ssh-copy-id processes, and they need for me to type in a password, so i'm using PExpect.</p>
<p>I have basically this:</p>
<pre><code>child = pexpect.spawn('command')
child.expect('password:')
child.sendline('the password')
</code></pre>
<p>and then I want t... | 6 | 2008-12-10T17:16:31Z | 357,749 | <p>I think the problem is, that SSH tries to open PTY and it does not work
on anything else than PTY for security reasons. This won't work well
with pexpect.</p>
<p>I have another ssh client:</p>
<p><a href="http://www.digmia.com/index.php?option=com_content&view=article&id=54:Digmia%20Enterprise%20SSH&It... | 1 | 2008-12-10T21:53:02Z | [
"python",
"process",
"pexpect"
] |
Pexpect, running ssh-copy-id is hanging when trying to spawn a second process | 356,830 | <p>I'm doing a Python script where I need to spawn several ssh-copy-id processes, and they need for me to type in a password, so i'm using PExpect.</p>
<p>I have basically this:</p>
<pre><code>child = pexpect.spawn('command')
child.expect('password:')
child.sendline('the password')
</code></pre>
<p>and then I want t... | 6 | 2008-12-10T17:16:31Z | 1,662,962 | <p>Fortunately or not, but OpenSSH client seems to be very picky about passwords and where they come from.</p>
<p>You may try using <a href="http://www.lag.net/paramiko/" rel="nofollow">Paramiko</a> Python SSH2 library. Here's a simple <a href="http://www.lag.net/pipermail/paramiko/2006-January/000180.html" rel="nofol... | 3 | 2009-11-02T18:49:35Z | [
"python",
"process",
"pexpect"
] |
Proper way of cancelling accept and closing a Python processing/multiprocessing Listener connection | 357,656 | <p>(I'm using the <a href="http://developer.berlios.de/projects/pyprocessing" rel="nofollow">pyprocessing</a> module in this example, but replacing processing with multiprocessing should probably work if you run <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">python 2.6</a> or use the <a hr... | 10 | 2008-12-10T21:25:18Z | 357,793 | <p>Probably not ideal, but you can release the block by sending the socket some data from the signal handler or the thread that is terminating the process.</p>
<p>EDIT: Another way to implement this might be to use the <strike>Connection</strike> <a href="http://docs.python.org/dev/library/multiprocessing.html#pipes-a... | 0 | 2008-12-10T22:14:28Z | [
"python",
"sockets",
"multiprocessing"
] |
Proper way of cancelling accept and closing a Python processing/multiprocessing Listener connection | 357,656 | <p>(I'm using the <a href="http://developer.berlios.de/projects/pyprocessing" rel="nofollow">pyprocessing</a> module in this example, but replacing processing with multiprocessing should probably work if you run <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">python 2.6</a> or use the <a hr... | 10 | 2008-12-10T21:25:18Z | 358,392 | <p>I'm new to the multiprocessing module, but it seems to me that mixing the processing module and the threading module is counter-intuitive, aren't they targetted at solving the same problem?</p>
<p>Anyway, how about wrapping your listen functions into a process itself? I'm not clear how this affects the rest of you... | 1 | 2008-12-11T03:46:03Z | [
"python",
"sockets",
"multiprocessing"
] |
Proper way of cancelling accept and closing a Python processing/multiprocessing Listener connection | 357,656 | <p>(I'm using the <a href="http://developer.berlios.de/projects/pyprocessing" rel="nofollow">pyprocessing</a> module in this example, but replacing processing with multiprocessing should probably work if you run <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">python 2.6</a> or use the <a hr... | 10 | 2008-12-10T21:25:18Z | 431,549 | <p>Isnt that what select is for??</p>
<p>Only call accept on the socket if the select indicates it will not block...</p>
<p>The select has a timeout, so you can break out occasionally occasionally to check
if its time to shut down....</p>
| 3 | 2009-01-10T19:03:29Z | [
"python",
"sockets",
"multiprocessing"
] |
Proper way of cancelling accept and closing a Python processing/multiprocessing Listener connection | 357,656 | <p>(I'm using the <a href="http://developer.berlios.de/projects/pyprocessing" rel="nofollow">pyprocessing</a> module in this example, but replacing processing with multiprocessing should probably work if you run <a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow">python 2.6</a> or use the <a hr... | 10 | 2008-12-10T21:25:18Z | 465,361 | <p>I thought I could avoid it, but it seems I have to do something like this:</p>
<pre><code>from processing import connection
connection.Listener.fileno = lambda self: self._listener._socket.fileno()
import select
l = connection.Listener('/tmp/x', 'AF_UNIX')
r, w, e = select.select((l, ), (), ())
if l in r:
print... | 1 | 2009-01-21T13:46:04Z | [
"python",
"sockets",
"multiprocessing"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 357,826 | <p>Python is well suited for folding on indent, bit for writing my own code I use markers as they can crunch a document down the way you want it and can serve as a kind of a table of contents. I have this in my vimrc to flip between the two when I'm viewing someone elses code. </p>
<pre><code>#Toggle fold methods \fo
... | 5 | 2008-12-10T22:26:19Z | [
"python",
"vim",
"folding"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 357,833 | <p>I really like the <a href="http://www.vim.org/scripts/script.php?script_id=2002" rel="nofollow"><code>python_ifold</code> plugin</a>.</p>
| 5 | 2008-12-10T22:29:04Z | [
"python",
"vim",
"folding"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 358,619 | <p>Try this plugin:</p>
<p><a href="http://vim.sourceforge.net/scripts/script.php?script_id=515" rel="nofollow">http://vim.sourceforge.net/scripts/script.php?script_id=515</a></p>
| 4 | 2008-12-11T07:23:05Z | [
"python",
"vim",
"folding"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 360,634 | <p>Personally I can't convince myself to litter my code with the markers. I've become pretty used to (and efficient) at using indent-folding. Together with my mapping of space bar (see below) to open/close folds and the zR and zM commands, I'm right at home. Perfect for Python!</p>
<blockquote>
<p><code>nnoremap <... | 83 | 2008-12-11T19:47:43Z | [
"python",
"vim",
"folding"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 361,548 | <p>I use <a href="http://www.vim.org/scripts/script.php?script_id=2462" title="this">this</a> syntax file for Python. It sets the folding method to syntax and folds all classes and functions, but nothing else.</p>
| 21 | 2008-12-12T00:32:01Z | [
"python",
"vim",
"folding"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 418,603 | <p>The Python source comes with a vim syntax plugin along with a custom vimrc file. Check the <a href="http://web.archive.org/web/20101128033925/http://python.org/dev/faq/" rel="nofollow">python FAQ on vim</a></p>
| 3 | 2009-01-06T23:24:48Z | [
"python",
"vim",
"folding"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 1,868,951 | <p>I think that indent folding is fine for python. I'm making a multi-branched git repo for vim-config python/django IDE ideas. Fork away!</p>
<p><a href="http://github.com/skyl/vim-config-python-ide">http://github.com/skyl/vim-config-python-ide</a></p>
| 5 | 2009-12-08T18:32:36Z | [
"python",
"vim",
"folding"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 21,112,061 | <p>For me the ideal folding is to fold just the <code>class</code> and <code>def</code> blocks, indent folding is too much for my taste. I think one elegant solution is to use the syntax system like this <a href="http://www.vim.org/scripts/script.php?script_id=2462" rel="nofollow">one</a> mentioned by Tomas. However, t... | 2 | 2014-01-14T11:09:43Z | [
"python",
"vim",
"folding"
] |
What is the recommended way to use Vim folding for Python code | 357,785 | <p>I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.</p>
<p>Does anyone have a preferred way to do Python code folding in Vim? I.e, </p>
<ul>
<li>Do you have a particular Vim plugin that you use and like?</li>
<li>Do you use manual folding or do you place markers ... | 83 | 2008-12-10T22:10:45Z | 33,940,401 | <p>Yet another plugin for folding Python code. Rather simple, handling docstrings, and on the GitHub:</p>
<p><a href="https://github.com/tmhedberg/SimpylFold" rel="nofollow">SimpylFold</a></p>
<p>Enjoy!</p>
| 1 | 2015-11-26T13:51:00Z | [
"python",
"vim",
"folding"
] |
A small question about python's variable scope | 357,810 | <p>I am a beginner of python and have a question, very confusing for me.
If I define a function first but within the function I have to use a variable which is defined in another function below, can I do it like this? Or how can I import the return things of another function into a function?
for example:</p>
<pre><cod... | 1 | 2008-12-10T22:20:07Z | 357,853 | <p>your example program works, because the two instances of 'good' are different variables (you just happen to have both variables with the same name). The following code is exactly the same:</p>
<pre><code>def hello(x,y):
good=hi(iy,ix)
"then do somethings,and use the parameter'good'."
return something
d... | 2 | 2008-12-10T22:35:55Z | [
"python",
"variables",
"scope"
] |
A small question about python's variable scope | 357,810 | <p>I am a beginner of python and have a question, very confusing for me.
If I define a function first but within the function I have to use a variable which is defined in another function below, can I do it like this? Or how can I import the return things of another function into a function?
for example:</p>
<pre><cod... | 1 | 2008-12-10T22:20:07Z | 357,855 | <p>The scope of functions <code>hello</code> and <code>hi</code> are entirely different. They do not have any variables in common.</p>
<p>Note that the result of calling <code>hi(x,y)</code> is some object. You save that object with the name <code>good</code> in the function <code>hello</code>.</p>
<p>The variable ... | 5 | 2008-12-10T22:36:30Z | [
"python",
"variables",
"scope"
] |
A small question about python's variable scope | 357,810 | <p>I am a beginner of python and have a question, very confusing for me.
If I define a function first but within the function I have to use a variable which is defined in another function below, can I do it like this? Or how can I import the return things of another function into a function?
for example:</p>
<pre><cod... | 1 | 2008-12-10T22:20:07Z | 357,858 | <p>The "hello" function doesn't mind you calling the "hi" function which is hasn't been defined yet, provided you don't try to actually use the "hello" function until after the both functions have been defined.</p>
| 1 | 2008-12-10T22:37:05Z | [
"python",
"variables",
"scope"
] |
A small question about python's variable scope | 357,810 | <p>I am a beginner of python and have a question, very confusing for me.
If I define a function first but within the function I have to use a variable which is defined in another function below, can I do it like this? Or how can I import the return things of another function into a function?
for example:</p>
<pre><cod... | 1 | 2008-12-10T22:20:07Z | 358,013 | <p>If you want to define a variable to the global namespace from inside a function, and thereby make it accessible by other functions in this space, you can use the global keyword. Here's some examples</p>
<pre><code>varA = 5 #A normal declaration of an integer in the main "global" namespace
def funcA():
print ... | 3 | 2008-12-10T23:35:45Z | [
"python",
"variables",
"scope"
] |
A small question about python's variable scope | 357,810 | <p>I am a beginner of python and have a question, very confusing for me.
If I define a function first but within the function I have to use a variable which is defined in another function below, can I do it like this? Or how can I import the return things of another function into a function?
for example:</p>
<pre><cod... | 1 | 2008-12-10T22:20:07Z | 358,041 | <p>More details on the python scoping rules are here :</p>
<p><a href="http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules#292502">Short Description of Python Scoping Rules</a></p>
| 2 | 2008-12-10T23:54:34Z | [
"python",
"variables",
"scope"
] |
Problem with Python implementation of Conway's Game of Life | 357,968 | <p>I am working on <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" rel="nofollow">Conway's Game of Life</a> currently and have gotten stuck. My code doesn't work.</p>
<p>When I run my code in GUI, it says:</p>
<pre>
[[0 0 0 0]
[0 1 1 0]
[0 1 0 0]
[0 0 0 0]]
Traceback (most recent call last):
Fil... | 0 | 2008-12-10T23:17:02Z | 357,982 | <p>A cursory glance shows that your <code>number_neighbors</code> indices are off.</p>
<p>Also, you never initialize <code>neighbors</code>.</p>
<p><b>Response to Comment:</b></p>
<pre><code>def number_neighbours(universe_array,iy,ix):
if universe_array[iy,ix-1]==1:
neighbours+=1
if universe_array[iy... | 2 | 2008-12-10T23:20:30Z | [
"python"
] |
Problem with Python implementation of Conway's Game of Life | 357,968 | <p>I am working on <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" rel="nofollow">Conway's Game of Life</a> currently and have gotten stuck. My code doesn't work.</p>
<p>When I run my code in GUI, it says:</p>
<pre>
[[0 0 0 0]
[0 1 1 0]
[0 1 0 0]
[0 0 0 0]]
Traceback (most recent call last):
Fil... | 0 | 2008-12-10T23:17:02Z | 357,994 | <p>This is an extremely low-grade lazy question, but your number_neighbours function is broken, it checks universe_array[iy,ix-1] twice (and hence omits a check it should be doing).</p>
| 0 | 2008-12-10T23:24:28Z | [
"python"
] |
Problem with Python implementation of Conway's Game of Life | 357,968 | <p>I am working on <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" rel="nofollow">Conway's Game of Life</a> currently and have gotten stuck. My code doesn't work.</p>
<p>When I run my code in GUI, it says:</p>
<pre>
[[0 0 0 0]
[0 1 1 0]
[0 1 0 0]
[0 0 0 0]]
Traceback (most recent call last):
Fil... | 0 | 2008-12-10T23:17:02Z | 358,020 | <p>You're trying to increment a variable that doesn't exist yet. Python can't increment something if it doesn't know what is. Try adding the following line at the top of your def number_neighbours function.</p>
<pre><code>neighbours = 0
</code></pre>
| 3 | 2008-12-10T23:39:28Z | [
"python"
] |
Problem with Python implementation of Conway's Game of Life | 357,968 | <p>I am working on <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" rel="nofollow">Conway's Game of Life</a> currently and have gotten stuck. My code doesn't work.</p>
<p>When I run my code in GUI, it says:</p>
<pre>
[[0 0 0 0]
[0 1 1 0]
[0 1 0 0]
[0 0 0 0]]
Traceback (most recent call last):
Fil... | 0 | 2008-12-10T23:17:02Z | 358,029 | <p>Well, I guess that you are also quite new to programming <em>per se</em>, otherwise you should not have any problems in interpreting that simple error message.</p>
<p>I'll help you dissect it:</p>
<ul>
<li>First, all "current" line numbers of your project's files are displayed, in calling order.</li>
<li>Then, it ... | 13 | 2008-12-10T23:44:47Z | [
"python"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 358,012 | <p>Java uses anonymous classes mostly to imitate closures or simply code blocks. Since in Python you can easily pass around methods there's no need for a construct as clunky as anonymous inner classes:</p>
<pre><code>def printStuff():
print "hello"
def doit(what):
what()
doit(printStuff)
</code></pre>
<p>Edit... | 13 | 2008-12-10T23:35:43Z | [
"python",
"class",
"anonymous-class"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 358,035 | <p>In python you have anonymous functions, declared using lambda statement. I do not like them very much - they are not so readable, and have limited functionality.</p>
<p>However, what you are talking about may be implemented in python with a completely different approach:</p>
<pre><code>class a(object):
def meth_... | 2 | 2008-12-10T23:49:53Z | [
"python",
"class",
"anonymous-class"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 358,042 | <p>Python probably has better ways to solve your problem. If you could provide more specific details of what you want to do it would help.</p>
<p>For example, if you need to change the method being called in a specific point in code, you can do this by passing the function as a parameter (functions are first class obj... | 5 | 2008-12-10T23:54:47Z | [
"python",
"class",
"anonymous-class"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 358,055 | <p>You can accomplish this in three ways:</p>
<ol>
<li>Proper subclass (of course)</li>
<li>a custom method that you invoke with the object as an argument</li>
<li>(what you probably want) -- adding a new method to an object (or replacing an existing one).</li>
</ol>
<p>Example of option 3 (edited to remove use of "n... | 11 | 2008-12-11T00:01:54Z | [
"python",
"class",
"anonymous-class"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 358,062 | <p>Well, classes are first class objects, so you can create them in methods if you want. e.g.</p>
<pre><code>from optparse import OptionParser
def make_custom_op(i):
class MyOP(OptionParser):
def exit(self):
print 'custom exit called', i
return MyOP
custom_op_class = make_custom_op(3)
custom_op = custo... | 10 | 2008-12-11T00:05:13Z | [
"python",
"class",
"anonymous-class"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 359,651 | <p>Python doesn't support this directly (anonymous classes) but because of its terse syntax it isn't really necessary:</p>
<pre><code>class MyOptionParser(OptionParser):
def exit(self, status=0, msg=None):
# body of method
p = MyOptionParser()
</code></pre>
<p>The only downside is you add MyOptionParser ... | 6 | 2008-12-11T15:06:37Z | [
"python",
"class",
"anonymous-class"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 3,915,434 | <p>You can use the <a href="http://docs.python.org/library/functions.html#type"><code>type(name, bases, dict)</code></a> builtin function to create classes on the fly. For example:</p>
<pre><code>op = type("MyOptionParser", (OptionParser,object), {"foo": lambda self: "foo" })
op().foo()
</code></pre>
<p>Since Option... | 22 | 2010-10-12T13:53:02Z | [
"python",
"class",
"anonymous-class"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 6,175,128 | <p>You can always hide class by variables:</p>
<pre><code> class var(...):
pass
var = var()
</code></pre>
<p>instead of</p>
<pre><code> var = new ...() {};
</code></pre>
| 0 | 2011-05-30T10:28:57Z | [
"python",
"class",
"anonymous-class"
] |
Does Python have something like anonymous inner classes of Java? | 357,997 | <p>In Java you can define a new class inline using anonymous inner classes. This is useful when you need to rewrite only a single method of the class.</p>
<p>Suppose that you want create a subclass of <code>OptionParser</code> that overrides only a single method (for example <code>exit()</code>). In Java you can write... | 28 | 2008-12-10T23:26:57Z | 12,384,223 | <pre><code>#!/usr/bin/env python
import os;os.system("""ruby << 'GOZER'
anonymous_class = Class.new do
def hi(name)
puts "Hello: #{name}."
end
def too_late(message)
exit 1
end
end
anonymous_ghoul = anonymous_class.new
anonymous_ghoul.hi("Egon Spengler")
anonymous_ghoul.too_late <<-quote
T... | -4 | 2012-09-12T08:38:36Z | [
"python",
"class",
"anonymous-class"
] |
Can I use chart modules with wxpython? | 358,189 | <p>Is it possible to use any chart modules with wxpython? And are there any good ones out there?</p>
<p>I'm thinking of the likes of PyCha (<a href="http://www.lorenzogil.com/projects/pycha/">http://www.lorenzogil.com/projects/pycha/</a>) or any equivalent. Many modules seem to require PyCairo, but I can't figure out ... | 5 | 2008-12-11T01:15:41Z | 358,328 | <p>I recently revisited <a href="http://matplotlib.sourceforge.net/index.html" rel="nofollow">matplotlib</a>, and am pretty happy with the results.
If you're on windows, there are windows installers available to make your installation process a little less painful.</p>
<p>One potential drawback though is that it requi... | 3 | 2008-12-11T02:54:33Z | [
"python",
"wxpython"
] |
Can I use chart modules with wxpython? | 358,189 | <p>Is it possible to use any chart modules with wxpython? And are there any good ones out there?</p>
<p>I'm thinking of the likes of PyCha (<a href="http://www.lorenzogil.com/projects/pycha/">http://www.lorenzogil.com/projects/pycha/</a>) or any equivalent. Many modules seem to require PyCairo, but I can't figure out ... | 5 | 2008-12-11T01:15:41Z | 358,493 | <p>matplotlib does embed quite well in <a href="http://www.scipy.org/Cookbook/Matplotlib/EmbeddingInWx" rel="nofollow">wxpython</a>. I have only used it in Tkinter, which went smoothly for me. I like the optional toolbar that allows direct manipulation of the plot (resizing and panning and such)</p>
| 1 | 2008-12-11T05:11:51Z | [
"python",
"wxpython"
] |
Can I use chart modules with wxpython? | 358,189 | <p>Is it possible to use any chart modules with wxpython? And are there any good ones out there?</p>
<p>I'm thinking of the likes of PyCha (<a href="http://www.lorenzogil.com/projects/pycha/">http://www.lorenzogil.com/projects/pycha/</a>) or any equivalent. Many modules seem to require PyCairo, but I can't figure out ... | 5 | 2008-12-11T01:15:41Z | 365,046 | <p>Use matplotlib. It integrates nicely with wxPython. <a href="http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/" rel="nofollow">Here's a sample</a> of an interactive chart with wxPython and matplotlib. </p>
| 1 | 2008-12-13T09:40:03Z | [
"python",
"wxpython"
] |
log4j with timestamp per log entry | 358,225 | <p>this is my log output</p>
<pre><code>INFO main digestemails - process inbox
INFO main digestemails - checking for emails in c:\development\DCMail\email\KN-Source
INFO main digestemails - digesting [email protected]
INFO main digestemails - extracting attachments
INFO main digestemails - no ... | 41 | 2008-12-11T01:44:39Z | 358,247 | <p>Use %d in your PatternLayout.</p>
<p>Also %d can take a format pattern as in %d{dd MMM yyyy HH:mm:ss,SSS} you can pick and choose the elements that you want. When the format pattern is omitted the date will be in ISO8601 format.</p>
| 77 | 2008-12-11T01:59:03Z | [
"java",
"python",
"log4j",
"jython"
] |
log4j with timestamp per log entry | 358,225 | <p>this is my log output</p>
<pre><code>INFO main digestemails - process inbox
INFO main digestemails - checking for emails in c:\development\DCMail\email\KN-Source
INFO main digestemails - digesting [email protected]
INFO main digestemails - extracting attachments
INFO main digestemails - no ... | 41 | 2008-12-11T01:44:39Z | 358,372 | <p>You can find more conversion characters usage in log4j javadoc.For example, at <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html" rel="nofollow">http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html</a>.</p>
| 5 | 2008-12-11T03:29:46Z | [
"java",
"python",
"log4j",
"jython"
] |
log4j with timestamp per log entry | 358,225 | <p>this is my log output</p>
<pre><code>INFO main digestemails - process inbox
INFO main digestemails - checking for emails in c:\development\DCMail\email\KN-Source
INFO main digestemails - digesting [email protected]
INFO main digestemails - extracting attachments
INFO main digestemails - no ... | 41 | 2008-12-11T01:44:39Z | 358,641 | <p>A extract from my properties file</p>
<pre><code>log4j.rootLogger=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p (%t) [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.R... | 13 | 2008-12-11T07:45:42Z | [
"java",
"python",
"log4j",
"jython"
] |
Keeping a variable around from post to get? | 358,398 | <p>I have a class called myClass which defines post() and get() methods. </p>
<p>From <em>index.html</em>, I have a form with an action that calls myClass.post() which grabs some data from the data base, sets a couple variables and sends the user to <em>new.html</em>. </p>
<p>now, <em>new.html</em> has a form which c... | 0 | 2008-12-11T03:50:02Z | 358,425 | <p>I don't know specifically about the google app engine, but normally, here's what happens:</p>
<p>The server would have some kind of thread pool. Every time an http request is sent to the server, a thread is selected from the pool or created.</p>
<p>In that thread an instance of some kind of controller object will ... | 0 | 2008-12-11T04:09:50Z | [
"python",
"google-app-engine",
"post",
"get"
] |
Keeping a variable around from post to get? | 358,398 | <p>I have a class called myClass which defines post() and get() methods. </p>
<p>From <em>index.html</em>, I have a form with an action that calls myClass.post() which grabs some data from the data base, sets a couple variables and sends the user to <em>new.html</em>. </p>
<p>now, <em>new.html</em> has a form which c... | 0 | 2008-12-11T03:50:02Z | 358,613 | <p>It is not a problem of how the instances are managed. Because HTTP is stateless, your program is, virtually, stateless too. (For long running processes, like GAE, it is possible to make it otherwise, but I am not sure you would need this complexity here)</p>
<p>You haven't supplied any code, but I am assuming you g... | 1 | 2008-12-11T07:12:22Z | [
"python",
"google-app-engine",
"post",
"get"
] |
Keeping a variable around from post to get? | 358,398 | <p>I have a class called myClass which defines post() and get() methods. </p>
<p>From <em>index.html</em>, I have a form with an action that calls myClass.post() which grabs some data from the data base, sets a couple variables and sends the user to <em>new.html</em>. </p>
<p>now, <em>new.html</em> has a form which c... | 0 | 2008-12-11T03:50:02Z | 358,757 | <p>HTTP is stateless, so you have no (built-in) way of knowing if the user that loads one page is the same user that loaded another. Further, even if you do know that, thanks to session cookies, for example, you have no way of telling if the browser window they're loading the subsequent page in is the same one they loa... | 1 | 2008-12-11T09:18:31Z | [
"python",
"google-app-engine",
"post",
"get"
] |
Keeping a variable around from post to get? | 358,398 | <p>I have a class called myClass which defines post() and get() methods. </p>
<p>From <em>index.html</em>, I have a form with an action that calls myClass.post() which grabs some data from the data base, sets a couple variables and sends the user to <em>new.html</em>. </p>
<p>now, <em>new.html</em> has a form which c... | 0 | 2008-12-11T03:50:02Z | 358,805 | <p>Why not just use memcache to temporarily store the variable, and then redirect to the POST URL? That seems like the easiest solution. </p>
| 0 | 2008-12-11T09:41:01Z | [
"python",
"google-app-engine",
"post",
"get"
] |
Keeping a variable around from post to get? | 358,398 | <p>I have a class called myClass which defines post() and get() methods. </p>
<p>From <em>index.html</em>, I have a form with an action that calls myClass.post() which grabs some data from the data base, sets a couple variables and sends the user to <em>new.html</em>. </p>
<p>now, <em>new.html</em> has a form which c... | 0 | 2008-12-11T03:50:02Z | 359,034 | <p>What you're talking about is establishing a "session". That is, a way to remember the user and the state of their transaction.</p>
<p>There are several ways of tackling this, all of which rely on techniques for remembering that you're in a session in the first place.</p>
<p>HTTP provides you no help. You have to... | 3 | 2008-12-11T11:24:44Z | [
"python",
"google-app-engine",
"post",
"get"
] |
Keeping a variable around from post to get? | 358,398 | <p>I have a class called myClass which defines post() and get() methods. </p>
<p>From <em>index.html</em>, I have a form with an action that calls myClass.post() which grabs some data from the data base, sets a couple variables and sends the user to <em>new.html</em>. </p>
<p>now, <em>new.html</em> has a form which c... | 0 | 2008-12-11T03:50:02Z | 360,292 | <p>OK -- Thanks everyone. </p>
<p>I'll try some of these ideas out, soon, and get back to you all. </p>
<p>It seems I can work around some these things by doing a lot of writing and reading from the datastore*, but I thought there might be an easier way of keeping that instance of the class around (I'm trying to use ... | 0 | 2008-12-11T18:02:19Z | [
"python",
"google-app-engine",
"post",
"get"
] |
XMODEM for python | 358,471 | <p>I am writing a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?</p>
| 2 | 2008-12-11T04:49:04Z | 358,480 | <p>I think youâre stuck with rolling your own.</p>
<p>You might be able to use <a href="http://linux.about.com/library/cmd/blcmdl1_sz.htm" rel="nofollow">sz</a>, which implements X/Y/ZMODEM. You could call out to the binary, or port the necessary code to Python.</p>
| 1 | 2008-12-11T04:56:57Z | [
"python",
"serial-port",
"file-transfer",
"xmodem"
] |
XMODEM for python | 358,471 | <p>I am writing a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?</p>
| 2 | 2008-12-11T04:49:04Z | 358,509 | <p>Here is a link to <a href="http://www.programmersheaven.com/download/2167/download.aspx" rel="nofollow">XMODEM</a> documentation that will be useful if you have to write your own. It has detailed description of the original XMODEM, XMODEM-CRC and XMODEM-1K.</p>
<p>You might also find this <a href="http://www.menie.... | 1 | 2008-12-11T05:32:48Z | [
"python",
"serial-port",
"file-transfer",
"xmodem"
] |
XMODEM for python | 358,471 | <p>I am writing a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?</p>
| 2 | 2008-12-11T04:49:04Z | 358,624 | <p>You can try using <a href="http://www.swig.org/" rel="nofollow">SWIG</a> to create Python bindings for the C libraries linked above (or any other C/C++ libraries you find online). That will allow you to use the same C API directly from Python.</p>
<p>The actual implementation will of course still be in C/C++, sinc... | 0 | 2008-12-11T07:26:04Z | [
"python",
"serial-port",
"file-transfer",
"xmodem"
] |
XMODEM for python | 358,471 | <p>I am writing a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?</p>
| 2 | 2008-12-11T04:49:04Z | 705,300 | <pre><code>def xmodem_send(serial, file):
t, anim = 0, '|/-\\'
serial.setTimeout(1)
while 1:
if serial.read(1) != NAK:
t = t + 1
print anim[t%len(anim)],'\r',
if t == 60 : return False
else:
break
p = 1
s = file.read(128)
while s:
s = s + '\xFF'*(128 - len(s))
chk = 0
... | 4 | 2009-04-01T12:40:00Z | [
"python",
"serial-port",
"file-transfer",
"xmodem"
] |
XMODEM for python | 358,471 | <p>I am writing a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?</p>
| 2 | 2008-12-11T04:49:04Z | 5,196,623 | <p>There is XMODEM module on PyPi. It handles both sending and receiving of data with XModem. Below is sample of its usage:</p>
<pre><code>import serial
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
from xmodem import XMODEM, NAK
from time import sleep
def readUntil(char = None):
... | 1 | 2011-03-04T16:37:34Z | [
"python",
"serial-port",
"file-transfer",
"xmodem"
] |
XMODEM for python | 358,471 | <p>I am writing a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?</p>
| 2 | 2008-12-11T04:49:04Z | 26,754,421 | <p>There is a python module that you can use -> <a href="https://pypi.python.org/pypi/xmodem" rel="nofollow">https://pypi.python.org/pypi/xmodem</a></p>
<p>You can see the transfer protocol in <a href="http://pythonhosted.org//xmodem/xmodem.html" rel="nofollow">http://pythonhosted.org//xmodem/xmodem.html</a></p>
| 0 | 2014-11-05T10:09:10Z | [
"python",
"serial-port",
"file-transfer",
"xmodem"
] |
python, set terminal type in pexpect | 358,783 | <p>I have a script which uses pexpect to start a CLI program. It works a bit like a shell where you get a prompt where you can enter some commands.</p>
<p>The problem I have, I think, is that this program uses a coloured prompt.</p>
<p>This is what I do </p>
<pre><code>import pprint
import pexpect
1 a = pexpect.spa... | 4 | 2008-12-11T09:32:43Z | 358,794 | <p>Couldn't find anything in <a href="http://pexpect.sourceforge.net/pexpect.html" rel="nofollow">the pexpect documentation</a> for setting terminals, but you could probably start your program explicitly with a shell, and then set the terminal type there:</p>
<pre><code>shell_cmd = 'ls -l | grep LOG > log_list.txt'... | 2 | 2008-12-11T09:38:59Z | [
"python",
"pexpect"
] |
python, set terminal type in pexpect | 358,783 | <p>I have a script which uses pexpect to start a CLI program. It works a bit like a shell where you get a prompt where you can enter some commands.</p>
<p>The problem I have, I think, is that this program uses a coloured prompt.</p>
<p>This is what I do </p>
<pre><code>import pprint
import pexpect
1 a = pexpect.spa... | 4 | 2008-12-11T09:32:43Z | 359,241 | <p>Ok, I found the answer. csl's answer set me on the right path.</p>
<p>pexpect has a "env" option which I thought I could use. like this:</p>
<pre><code>a = pexpect.spawn('program', env = {"TERM": "dumb"})
</code></pre>
<p>But this spawns a new shell which does not work for me, our development environment
depends... | 7 | 2008-12-11T12:52:34Z | [
"python",
"pexpect"
] |
Execute Commands Sequentially in Python | 359,347 | <p>I would like to execute multiple commands in a row:</p>
<p>ie (just to illustrate my need):</p>
<p><strong>cmd</strong> (the shell)</p>
<p>then</p>
<p><strong>cd dir</strong></p>
<p>and</p>
<p><strong>ls</strong></p>
<p>and read the result of the ls.</p>
<p>Any idea with subprocess module ?</p>
<p>UPDATE:</... | 13 | 2008-12-11T13:30:23Z | 359,355 | <p>Yes, the <code><a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess.Popen</a>()</code> function supports a <code>cwd</code> keyword argument, with which you can set the directory it runs the process in.</p>
<p>I guess the first step, the shell, is not needed, if all you want is to run ... | 2 | 2008-12-11T13:35:39Z | [
"python",
"windows",
"subprocess"
] |
Execute Commands Sequentially in Python | 359,347 | <p>I would like to execute multiple commands in a row:</p>
<p>ie (just to illustrate my need):</p>
<p><strong>cmd</strong> (the shell)</p>
<p>then</p>
<p><strong>cd dir</strong></p>
<p>and</p>
<p><strong>ls</strong></p>
<p>and read the result of the ls.</p>
<p>Any idea with subprocess module ?</p>
<p>UPDATE:</... | 13 | 2008-12-11T13:30:23Z | 359,432 | <p>Finding 'bar' in every file whose name contains 'foo':</p>
<pre><code>from subprocess import Popen, PIPE
find_process = Popen(['find', '-iname', '*foo*'], stdout=PIPE)
grep_process = Popen(['xargs', 'grep', 'bar'], stdin=find_process.stdout, stdout=PIPE)
out, err = grep_process.communicate()
</code></pre>
<p>'out'... | 1 | 2008-12-11T13:59:46Z | [
"python",
"windows",
"subprocess"
] |
Execute Commands Sequentially in Python | 359,347 | <p>I would like to execute multiple commands in a row:</p>
<p>ie (just to illustrate my need):</p>
<p><strong>cmd</strong> (the shell)</p>
<p>then</p>
<p><strong>cd dir</strong></p>
<p>and</p>
<p><strong>ls</strong></p>
<p>and read the result of the ls.</p>
<p>Any idea with subprocess module ?</p>
<p>UPDATE:</... | 13 | 2008-12-11T13:30:23Z | 359,506 | <p>There is an easy way to execute a sequence of commands.</p>
<p>Use the following in <code>subprocess.Popen</code></p>
<pre><code>"command1; command2; command3"
</code></pre>
<p>Or, if you're stuck with windows, you have several choices.</p>
<ul>
<li><p>Create a temporary ".BAT" file, and provide this to <code>su... | 17 | 2008-12-11T14:23:17Z | [
"python",
"windows",
"subprocess"
] |
Execute Commands Sequentially in Python | 359,347 | <p>I would like to execute multiple commands in a row:</p>
<p>ie (just to illustrate my need):</p>
<p><strong>cmd</strong> (the shell)</p>
<p>then</p>
<p><strong>cd dir</strong></p>
<p>and</p>
<p><strong>ls</strong></p>
<p>and read the result of the ls.</p>
<p>Any idea with subprocess module ?</p>
<p>UPDATE:</... | 13 | 2008-12-11T13:30:23Z | 359,737 | <p>To do that, you would have to:</p>
<ul>
<li>supply the <code>shell=True</code> argument in the <code>subprocess.Popen</code> call, and</li>
<li>separate the commands with:
<ul>
<li><code>;</code> if running under a *nix shell (bash, ash, sh, ksh, csh, tcsh, zsh etc)</li>
<li><code>&</code> if running under the ... | 18 | 2008-12-11T15:35:32Z | [
"python",
"windows",
"subprocess"
] |
Execute Commands Sequentially in Python | 359,347 | <p>I would like to execute multiple commands in a row:</p>
<p>ie (just to illustrate my need):</p>
<p><strong>cmd</strong> (the shell)</p>
<p>then</p>
<p><strong>cd dir</strong></p>
<p>and</p>
<p><strong>ls</strong></p>
<p>and read the result of the ls.</p>
<p>Any idea with subprocess module ?</p>
<p>UPDATE:</... | 13 | 2008-12-11T13:30:23Z | 32,923,576 | <p>below python script have 3 function what you went just excute:</p>
<pre><code>import sys
import subprocess
def cd(self,line):
proc1 = subprocess.Popen(['cd'],stdin=subprocess.PIPE)
proc1.communicate()
def ls(self,line):
proc2 = subprocess.Popen(['ls','-l'],stdin=subprocess.PIPE)
proc2.communicate(... | 1 | 2015-10-03T14:18:30Z | [
"python",
"windows",
"subprocess"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.