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 |
|---|---|---|---|---|---|---|---|---|---|
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 8,759,409 | <p>Using SL4A (which has already been mentioned by itself in other answers) you can <a href="http://groups.google.com/group/web2py/browse_thread/thread/f227e93fe802a902" rel="nofollow">run</a> a full-blown <a href="http://web2py.com/" rel="nofollow">web2py</a> instance (other <a href="http://wiki.python.org/moin/WebFra... | 18 | 2012-01-06T14:34:25Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 8,784,038 | <p>From the <a href="https://github.com/kivy/python-for-android">Python for android</a> site:</p>
<blockquote>
<p>Python for android is a project to create your own Python distribution including the modules you want, and create an apk including python, libs, and your application.</p>
</blockquote>
| 12 | 2012-01-09T04:46:53Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 9,773,282 | <h3>SL4A</h3>
<p><a href="https://github.com/damonkohler/sl4a" rel="nofollow">Scripting Layer for Android</a> does what you want. You can easily install it directly onto your device from their site, and do not need root.</p>
<p>It supports a range of languages; Python is the most mature. By default, it uses Python 2.... | 34 | 2012-03-19T15:45:27Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 10,519,481 | <p>Yet another attempt: <a href="https://code.google.com/p/android-python27/">https://code.google.com/p/android-python27/</a></p>
<p>This one embed directly the Python interpretter in your app apk.</p>
| 12 | 2012-05-09T15:44:49Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 15,335,213 | <p>You can run your Python code using <a href="http://code.google.com/p/android-scripting/">sl4a</a>. sl4a supports Python, <a href="http://en.wikipedia.org/wiki/Perl">Perl</a>, <a href="http://en.wikipedia.org/wiki/JRuby">JRuby</a>, <a href="http://en.wikipedia.org/wiki/Lua_%28programming_language%29">Lua</a>, BeanShe... | 8 | 2013-03-11T09:36:58Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 17,073,989 | <p>I use the QPython application. It has an editor, a console, and you can run your Python programs with it. The application is free, and the link is <a href="http://qpython.com/">http://qpython.com/</a>.</p>
| 14 | 2013-06-12T19:46:35Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 26,219,028 | <p>Didn't see this posted here, but you can do it with Pyside and Qt now that Qt works on Android thanks to Necessitas.</p>
<p>It seems like quite a kludge at the moment but could be a viable route eventually...</p>
<p><a href="http://qt-project.org/wiki/PySide_for_Android_guide" rel="nofollow">http://qt-project.org/... | 4 | 2014-10-06T14:59:14Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 27,271,274 | <p>While there is one book called <strong><em>Developing Android on Android</em></strong>, of which introduce how to develop apps on the Android device by using the Python language, and this book should be a good start for you.</p>
| 0 | 2014-12-03T12:00:59Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 27,913,916 | <h1>Kivy</h1>
<hr>
<p>I want to post this as an extension to what <strong>@JohnMudd</strong> has already answered (<em>but please bare with me as English isn't my first language</em>)</p>
<p>It has been years since then, and <strong>Kivy</strong> has also <em>evoluted</em> to <strong>v1.9-dev</strong>, the biggest s... | 13 | 2015-01-13T02:08:57Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 28,719,773 | <p>Another option if you are looking for 3.4.2 or 3.5.1 is this archive on GitHub. </p>
<p><a href="https://github.com/rave-engine/python3-android" rel="nofollow">Python3-Android 3.4.2</a> or <a href="https://github.com/GRRedWings/python3-android" rel="nofollow">Python3-Android 3.5.1</a></p>
<p>It currently supports... | 4 | 2015-02-25T13:04:47Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 30,294,060 | <p>One more option seems to be <a href="http://pyqt.sourceforge.net/Docs/pyqtdeploy/index.html" rel="nofollow">pyqtdeploy</a> which citing the docs is:</p>
<blockquote>
<p>a tool that, in conjunction with other tools provided with Qt, enables
the deployment of PyQt4 and PyQt5 applications written with Python
v2.... | 1 | 2015-05-18T01:56:41Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Is there a way to run Python on Android? | 101,754 | <p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p>
<p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn... | 1,360 | 2008-09-19T13:21:12Z | 39,523,019 | <p>You can use <strong>QPython</strong>:</p>
<p>It has a Python Console, Editor, as well as Package Management / Installers</p>
<p><a href="http://qpython.com/" rel="nofollow">http://qpython.com/</a></p>
<p>It's an open source project with both Python 2 and Python 3 implementations. You can download the source and t... | 0 | 2016-09-16T02:41:18Z | [
"python",
"android",
"ase",
"android-scripting"
] |
Sorting a dict on __iter__ | 102,394 | <p>I am trying to sort a dict based on its key and return an iterator to the values from within an overridden iter method in a class. Is there a nicer and more efficient way of doing this than creating a new list, inserting into the list as I sort through the keys?</p>
| 4 | 2008-09-19T14:43:24Z | 102,438 | <p>By far the easiest approach, and almost certainly the fastest, is something along the lines of:</p>
<pre><code>def sorted_dict(d):
keys = d.keys()
keys.sort()
for key in keys:
yield d[key]
</code></pre>
<p>You can't sort without fetching all keys. Fetching all keys into a list and then sorting ... | 2 | 2008-09-19T14:48:21Z | [
"python",
"optimization",
"refactoring"
] |
Sorting a dict on __iter__ | 102,394 | <p>I am trying to sort a dict based on its key and return an iterator to the values from within an overridden iter method in a class. Is there a nicer and more efficient way of doing this than creating a new list, inserting into the list as I sort through the keys?</p>
| 4 | 2008-09-19T14:43:24Z | 102,443 | <p>How about something like this:</p>
<pre><code>def itersorted(d):
for key in sorted(d):
yield d[key]
</code></pre>
| 8 | 2008-09-19T14:49:06Z | [
"python",
"optimization",
"refactoring"
] |
Sorting a dict on __iter__ | 102,394 | <p>I am trying to sort a dict based on its key and return an iterator to the values from within an overridden iter method in a class. Is there a nicer and more efficient way of doing this than creating a new list, inserting into the list as I sort through the keys?</p>
| 4 | 2008-09-19T14:43:24Z | 102,480 | <p>Assuming you want a default sort order, you can used sorted(list) or list.sort(). If you want your own sort logic, Python lists support the ability to sort based on a function you pass in. For example, the following would be a way to sort numbers from least to greatest (the default behavior) using a function. </p... | -1 | 2008-09-19T14:53:03Z | [
"python",
"optimization",
"refactoring"
] |
Sorting a dict on __iter__ | 102,394 | <p>I am trying to sort a dict based on its key and return an iterator to the values from within an overridden iter method in a class. Is there a nicer and more efficient way of doing this than creating a new list, inserting into the list as I sort through the keys?</p>
| 4 | 2008-09-19T14:43:24Z | 103,187 | <pre><code>def sortedDict(dictobj):
return (value for key, value in sorted(dictobj.iteritems()))
</code></pre>
<p>This will create a single intermediate list, the 'sorted()' method returns a real list. But at least it's only one.</p>
| 2 | 2008-09-19T15:54:47Z | [
"python",
"optimization",
"refactoring"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,612 | <p>See the "Motivation" section in <a href="http://www.python.org/dev/peps/pep-0255/">PEP 255</a>.</p>
<p>A non-obvious use of generators is creating interruptible functions, which lets you do things like update UI or run several jobs "simultaneously" (interleaved, actually) while not using threads.</p>
| 34 | 2008-09-19T15:07:13Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,632 | <p>Generators give you lazy evaluation. You use them by iterating over them, either explicitly with 'for' or implicitly by passing it to any function or construct that iterates. You can think of generators as returning multiple items, as if they return a list, but instead of returning them all at once they return them ... | 183 | 2008-09-19T15:09:25Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,633 | <p>Basically avoiding call-back functions when iterating over input maintaining state.</p>
<p>See <a href="http://www.python.org/dev/peps/pep-0255/">here</a> and <a href="http://www.dabeaz.com/generators/index.html">here</a> for an overview of what can be done using generators.</p>
| 6 | 2008-09-19T15:09:26Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,634 | <p>One of the reasons to use generator is to make the solution clearer for some kind of solutions.</p>
<p>The other is to treat results one at a time, avoiding building huge lists of results that you would process separated anyway. </p>
<p>If you have a fibonacci-up-to-n function like this:</p>
<pre><code># function... | 71 | 2008-09-19T15:09:28Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,667 | <p>My favorite uses are "filter" and "reduce" operations.</p>
<p>Let's say we're reading a file, and only want the lines which begin with "##".</p>
<pre><code>def filter2sharps( aSequence ):
for l in aSequence:
if l.startswith("##"):
yield l
</code></pre>
<p>We can then use the generator func... | 12 | 2008-09-19T15:13:16Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,674 | <p>Buffering. When it is efficient to fetch data in large chunks, but process it in small chunks, then a generator might help:</p>
<pre><code>def bufferedFetch():
while True:
buffer = getBigChunkOfData()
# insert some code to break on 'end of data'
for i in buffer:
yield i
</code></pre>
... | 26 | 2008-09-19T15:14:10Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,679 | <p>Piles of stuff. Any time you want to generate a sequence of items, but don't want to have to 'materialize' them all into a list at once. For example, you could have a simple generator that returns prime numbers:</p>
<pre><code>def primes():
primes_found = set()
primes_found.add(2)
yield 2
for i in i... | 1 | 2008-09-19T15:14:43Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,682 | <p>The simple explanation:
Consider a <code>for</code> statement</p>
<pre><code>for item in iterable:
do_stuff()
</code></pre>
<p>A lot of the time, all the items in <code>iterable</code> doesn't need to be there from the start, but can be generated on the fly as they're required. This can be a lot more efficient ... | 17 | 2008-09-19T15:15:03Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 102,701 | <p>I use generators when our web server is acting as a proxy:</p>
<ol>
<li>The client requests a proxied url from the server</li>
<li>The server begins to load the target url</li>
<li>The server yields to return the results to the client as soon as it gets them</li>
</ol>
| 2 | 2008-09-19T15:17:51Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 740,763 | <p>I have found that generators are very helpful in cleaning up your code and by giving you a very unique way to encapsulate and modularize code. In a situation where you need something to constantly spit out values based on its own internal processing and when that something needs to be called from anywhere in your c... | 17 | 2009-04-11T20:55:59Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 14,394,854 | <p>I find this explanation which clears my doubt. Because there is a possibility that person who don't know <code>Generators</code> also don't know about <code>yield</code></p>
<p><strong>Return</strong></p>
<p>The return statement is where all the local variables are destroyed and the resulting value is given back (... | 22 | 2013-01-18T08:17:56Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 23,334,878 | <p>Since the send method of a generator has not been mentioned here is an example:</p>
<pre><code>def test():
for i in xrange(5):
val = yield
print(val)
t = test()
# proceed to yield statement
next(t)
# send value to yield
t.send(1)
t.send('2')
t.send([3])
</code></pre>
<p>It shows the possibilit... | 2 | 2014-04-28T07:21:10Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 23,530,101 | <h2>Real World Example</h2>
<p>Lets say you have 100 million domains in your MySQL table and you would like to update alexa rank for each domain.</p>
<p>First thing you need is to select your domain names from the database. </p>
<p>Lets say your database name is <code>domains</code> and table name is <code>domain</c... | 17 | 2014-05-07T23:20:11Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 26,074,771 | <p>A practical example where you could make use of a generator is if you have some kind of shape and you want to iterate over its corners, edges or whatever. For my own project (source code <a href="https://github.com/Pithikos/python-rectangles">here</a>) I had a rectangle:</p>
<pre><code>class Rect():
def __init... | 7 | 2014-09-27T12:40:27Z | [
"python",
"generator"
] |
What can you use Python generator functions for? | 102,535 | <p>I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.</p>
| 157 | 2008-09-19T14:58:49Z | 29,079,078 | <p>Some good answers here, however, I'd also recommend a complete read of the python <a href="https://docs.python.org/dev/howto/functional.html" rel="nofollow">Functional Programming tutorial</a> which helps explain some of the more potent use-cases of generators.</p>
<ul>
<li>Particularly interesting is that it is no... | 4 | 2015-03-16T14:17:35Z | [
"python",
"generator"
] |
How do I merge a 2D array in Python into one string with List Comprehension? | 103,844 | <p>List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.</p>
<p>Say, I have a 2D list:</p>
<pre><code>li = [[0,1,2],[3,4,5],[6,7,8]]
</code></pre>
<p>I would like to merge this either into one long list</p>
<pre><code>li2 = [0,1,2,3,4,5,6,7,8]
<... | 14 | 2008-09-19T17:21:53Z | 103,873 | <pre><code>import itertools
itertools.flatten( li )
</code></pre>
| -1 | 2008-09-19T17:24:56Z | [
"python",
"list-comprehension"
] |
How do I merge a 2D array in Python into one string with List Comprehension? | 103,844 | <p>List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.</p>
<p>Say, I have a 2D list:</p>
<pre><code>li = [[0,1,2],[3,4,5],[6,7,8]]
</code></pre>
<p>I would like to merge this either into one long list</p>
<pre><code>li2 = [0,1,2,3,4,5,6,7,8]
<... | 14 | 2008-09-19T17:21:53Z | 103,883 | <p>To make it a flattened list use either:</p>
<ol>
<li><a href="http://code.activestate.com/recipes/121294/" rel="nofollow">http://code.activestate.com/recipes/121294/</a></li>
<li><a href="http://code.activestate.com/recipes/363051/" rel="nofollow">http://code.activestate.com/recipes/363051/</a></li>
</ol>
<p>Then,... | 0 | 2008-09-19T17:26:44Z | [
"python",
"list-comprehension"
] |
How do I merge a 2D array in Python into one string with List Comprehension? | 103,844 | <p>List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.</p>
<p>Say, I have a 2D list:</p>
<pre><code>li = [[0,1,2],[3,4,5],[6,7,8]]
</code></pre>
<p>I would like to merge this either into one long list</p>
<pre><code>li2 = [0,1,2,3,4,5,6,7,8]
<... | 14 | 2008-09-19T17:21:53Z | 103,886 | <p>There's a couple choices. First, you can just create a new list and add the contents of each list to it:</p>
<pre><code>li2 = []
for sublist in li:
li2.extend(sublist)
</code></pre>
<p>Alternately, you can use the <code>itertools</code> module's <code>chain</code> function, which produces an iterable containin... | 5 | 2008-09-19T17:26:51Z | [
"python",
"list-comprehension"
] |
How do I merge a 2D array in Python into one string with List Comprehension? | 103,844 | <p>List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.</p>
<p>Say, I have a 2D list:</p>
<pre><code>li = [[0,1,2],[3,4,5],[6,7,8]]
</code></pre>
<p>I would like to merge this either into one long list</p>
<pre><code>li2 = [0,1,2,3,4,5,6,7,8]
<... | 14 | 2008-09-19T17:21:53Z | 103,887 | <p>For the second one, there is a built-in string method to do that :</p>
<pre><code>>>> print ','.join(str(x) for x in li2)
"0,1,2,3,4,5,6,7,8"
</code></pre>
<p>For the first one, you can use join within a comprehension list :</p>
<pre><code>>>> print ",".join([",".join(str(x) for x in li])
"0,1,2... | 0 | 2008-09-19T17:26:52Z | [
"python",
"list-comprehension"
] |
How do I merge a 2D array in Python into one string with List Comprehension? | 103,844 | <p>List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.</p>
<p>Say, I have a 2D list:</p>
<pre><code>li = [[0,1,2],[3,4,5],[6,7,8]]
</code></pre>
<p>I would like to merge this either into one long list</p>
<pre><code>li2 = [0,1,2,3,4,5,6,7,8]
<... | 14 | 2008-09-19T17:21:53Z | 103,890 | <p>My favorite, and the shortest one, is this:</p>
<pre><code>li2 = sum(li, [])
</code></pre>
<p>and</p>
<pre><code>s = ','.join(li2)
</code></pre>
<p>EDIT: use <code>sum</code> instead of <code>reduce</code>, (thanks <a href="http://stackoverflow.com/users/17624/thomas-wouters">Thomas Wouters</a>!)</p>
| 5 | 2008-09-19T17:27:43Z | [
"python",
"list-comprehension"
] |
How do I merge a 2D array in Python into one string with List Comprehension? | 103,844 | <p>List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.</p>
<p>Say, I have a 2D list:</p>
<pre><code>li = [[0,1,2],[3,4,5],[6,7,8]]
</code></pre>
<p>I would like to merge this either into one long list</p>
<pre><code>li2 = [0,1,2,3,4,5,6,7,8]
<... | 14 | 2008-09-19T17:21:53Z | 103,895 | <p>Like so:</p>
<pre><code>[ item for innerlist in outerlist for item in innerlist ]
</code></pre>
<p>Turning that directly into a string with separators:</p>
<pre><code>','.join(str(item) for innerlist in outerlist for item in innerlist)
</code></pre>
<p>Yes, the order of 'for innerlist in outerlist' and 'for item... | 22 | 2008-09-19T17:28:14Z | [
"python",
"list-comprehension"
] |
How do I merge a 2D array in Python into one string with List Comprehension? | 103,844 | <p>List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.</p>
<p>Say, I have a 2D list:</p>
<pre><code>li = [[0,1,2],[3,4,5],[6,7,8]]
</code></pre>
<p>I would like to merge this either into one long list</p>
<pre><code>li2 = [0,1,2,3,4,5,6,7,8]
<... | 14 | 2008-09-19T17:21:53Z | 103,908 | <p>Try that:</p>
<pre><code>li=[[0,1,2],[3,4,5],[6,7,8]]
li2 = [ y for x in li for y in x]
</code></pre>
<p>You can read it like this:<br>
Give me the list of every ys.<br>
The ys come from the xs.<br>
The xs come from li.</p>
<p>To map that in a string:</p>
<pre><code>','.join(map(str,li2))
</code></pre>
| 6 | 2008-09-19T17:29:22Z | [
"python",
"list-comprehension"
] |
How do I merge a 2D array in Python into one string with List Comprehension? | 103,844 | <p>List Comprehension for me seems to be like the opaque block of granite that regular expressions are for me. I need pointers.</p>
<p>Say, I have a 2D list:</p>
<pre><code>li = [[0,1,2],[3,4,5],[6,7,8]]
</code></pre>
<p>I would like to merge this either into one long list</p>
<pre><code>li2 = [0,1,2,3,4,5,6,7,8]
<... | 14 | 2008-09-19T17:21:53Z | 244,477 | <p>There are many ways to do this problem. I like <a href="http://numpy.scipy.org/" rel="nofollow">Numpy</a>'s tools because it is normally already imported in everything I do. However, if you aren't using Numpy for anything else this probably isn't a good method.</p>
<pre><code>import numpy
li = [[0,1,2],[3,4,5],[6... | 0 | 2008-10-28T19:17:27Z | [
"python",
"list-comprehension"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 104,426 | <p>This solution implements a generator, to avoid holding all the permutations on memory:</p>
<pre><code>def permutations (orig_list):
if not isinstance(orig_list, list):
orig_list = list(orig_list)
yield orig_list
if len(orig_list) == 1:
return
for n in sorted(orig_list):
ne... | 15 | 2008-09-19T18:41:52Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 104,436 | <p><strong>Starting with Python 2.6</strong> (and if you're on Python 3) you have a <strong>standard-library</strong> tool for this: <a href="https://docs.python.org/2/library/itertools.html#itertools.permutations"><code>itertools.permutations</code></a>.</p>
<hr>
<p>If you're using an <strong>older Python (<2.6)<... | 190 | 2008-09-19T18:43:09Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 104,471 | <p>And in <a href="http://docs.python.org/dev/whatsnew/2.6.html">Python 2.6</a> onwards:</p>
<pre><code>import itertools
itertools.permutations([1,2,3])
</code></pre>
<p>(returned as a generator. Use <code>list(permutations(l))</code> to return as a list.)</p>
| 255 | 2008-09-19T18:48:48Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 105,774 | <p>Forgive my python illiteracy as I won't be offering the solution in python.
As I do not know what method python 2.6 uses to generate the permutations and eliben's one looks like Johnson-Trotter permutation generation, you might look for article
in Wikipedia on <a href="http://en.wikipedia.org/wiki/Permutation#Algori... | 2 | 2008-09-19T21:26:48Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 108,651 | <p>The following code is an in-place permutation of a given list, implemented as a generator. Since it only returns references to the list, the list should not be modified outside the generator.
The solution is non-recursive, so uses low memory. Work well also with multiple copies of elements in the input list.</p>
<p... | 10 | 2008-09-20T16:32:47Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 170,248 | <p><em>The following code with Python 2.6 and above ONLY</em></p>
<p>First, import <code>itertools</code>:</p>
<pre><code>import itertools
</code></pre>
<h3>Permutation (order matters):</h3>
<pre><code>print list(itertools.permutations([1,2,3,4], 2))
[(1, 2), (1, 3), (1, 4),
(2, 1), (2, 3), (2, 4),
(3, 1), (3, 2), ... | 177 | 2008-10-04T12:18:56Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 5,501,066 | <p>A quite obvious way in my opinion might be also:</p>
<pre><code>def permutList(l):
if not l:
return [[]]
res = []
for e in l:
temp = l[:]
temp.remove(e)
res.extend([[e] + r for r in permutList(temp)])
return res
</code></pre>
| 8 | 2011-03-31T13:58:40Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 7,140,205 | <pre><code>list2Perm = [1, 2.0, 'three']
listPerm = [[a, b, c]
for a in list2Perm
for b in list2Perm
for c in list2Perm
if ( a != b and b != c and a != c )
]
print listPerm
</code></pre>
<p>Output:</p>
<pre><code>[
[1, 2.0, 'three'],
[1, 'three', 2.... | 7 | 2011-08-21T18:28:44Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 7,733,966 | <pre><code>def permutations(head, tail=''):
if len(head) == 0: print tail
else:
for i in range(len(head)):
permutations(head[0:i] + head[i+1:], tail+head[i])
</code></pre>
<p>called as:</p>
<pre><code>permutations('abc')
</code></pre>
| 23 | 2011-10-12T00:14:09Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 10,799,849 | <p>One can indeed iterate over the first element of each permutation, as in tzwenn's answer; I prefer to write this solution this way:</p>
<pre><code>def all_perms(elements):
if len(elements) <= 1:
yield elements # Only permutation possible = no permutation
else:
# Iteration over the first ... | 4 | 2012-05-29T13:08:55Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 11,962,517 | <pre><code>#!/usr/bin/env python
def perm(a,k=0):
if(k==len(a)):
print a
else:
for i in xrange(k,len(a)):
a[k],a[i] = a[i],a[k]
perm(a, k+1)
a[k],a[i] = a[i],a[k]
perm([1,2,3])
</code></pre>
<p>Output:</p>
<pre><code>[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, ... | 11 | 2012-08-14T23:58:40Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 14,470,271 | <p>Note that this algorithm has an <code>n factorial</code> time complexity, where <code>n</code> is the length of the input list</p>
<p>Print the results on the run:</p>
<pre><code>global result
result = []
def permutation(li):
if li == [] or li == None:
return
if len(li) == 1:
result.append(li[0])
pr... | 5 | 2013-01-23T00:01:57Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 16,446,022 | <pre><code>from __future__ import print_function
def perm(n):
p = []
for i in range(0,n+1):
p.append(i)
while True:
for i in range(1,n+1):
print(p[i], end=' ')
print("")
i = n - 1
found = 0
while (not found and i>0):
if p[i]<p[i+... | 1 | 2013-05-08T16:48:44Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 17,391,851 | <p>In a functional style</p>
<pre><code>def addperm(x,l):
return [ l[0:i] + [x] + l[i:] for i in range(len(l)+1) ]
def perm(l):
if len(l) == 0:
return [[]]
return [x for y in perm(l[1:]) for x in addperm(l[0],y) ]
print perm([ i for i in range(3)])
</code></pre>
<p>The result:</p>
<pre><code>[... | 7 | 2013-06-30T15:17:16Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 17,504,089 | <p>Here is an algorithm that works on a list without creating new intermediate lists similar to Ber's solution at <a href="http://stackoverflow.com/a/108651/184528">http://stackoverflow.com/a/108651/184528</a>. </p>
<pre><code>def permute(xs, low=0):
if low + 1 >= len(xs):
yield xs
else:
for... | 2 | 2013-07-06T14:56:16Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 18,135,428 | <p>I used an algorithm based on the <a href="http://en.wikipedia.org/wiki/Factorial_number_system" rel="nofollow">factorial number system</a>- For a list of length n, you can assemble each permutation item by item, selecting from the items left at each stage. You have n choices for the first item, n-1 for the second, a... | 4 | 2013-08-08T20:23:50Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 20,014,561 | <p>This is inspired by the Haskell implementation using list comprehension: </p>
<pre><code>def permutation(list):
if len(list) == 0:
return [[]]
else:
return [[x] + ys for x in list for ys in permutation(delete(list, x))]
def delete(list, item):
lc = list[:]
lc.remove(item)
return... | 0 | 2013-11-16T04:10:33Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 23,732,953 | <p>The beauty of recursion:</p>
<pre><code>>>> import copy
>>> def perm(prefix,rest):
... for e in rest:
... new_rest=copy.copy(rest)
... new_prefix=copy.copy(prefix)
... new_prefix.append(e)
... new_rest.remove(e)
... if len(new_r... | 0 | 2014-05-19T08:23:12Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 28,256,360 | <p>This algorithm is the most effective one, it avoids of array passing and manipulation in recursive calls, works in Python 2, 3:</p>
<pre><code>def permute(items):
length = len(items)
def inner(ix=[]):
do_yield = len(ix) == length - 1
for i in range(0, length):
if i in ix: #avoid ... | 1 | 2015-01-31T20:46:34Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 30,112,080 | <pre><code>def pzip(c, seq):
result = []
for item in seq:
for i in range(len(item)+1):
result.append(item[i:]+c+item[:i])
return result
def perm(line):
seq = [c for c in line]
if len(seq) <=1 :
return seq
else:
return pzip(seq[0], perm(seq[1:]))
</code></... | 1 | 2015-05-07T21:34:35Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 30,428,753 | <p>For performance, a numpy solution inspired by <a href="http://www.kcats.org/csci/464/doc/knuth/fascicles/fasc2b.pdf" rel="nofollow">Knuth</a>, (p22) :</p>
<pre><code>from numpy import empty, uint8
from math import factorial
def perms(n):
f = 1
p = empty((2*n-1, factorial(n)), uint8)
for i in range(n):
... | 3 | 2015-05-24T21:56:08Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 32,448,587 | <p>for Python we can use itertools and import both permutations and combinations to solve your problem</p>
<pre><code>from itertools import product, permutations
A = ([1,2,3])
print (list(permutations(sorted(A),2)))
</code></pre>
| -4 | 2015-09-08T02:58:55Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 36,102,351 | <p>Generate all possible permutations</p>
<p>I'm using python3.4:</p>
<pre><code>def calcperm(arr, size):
result = set([()])
for dummy_idx in range(size):
temp = set()
for dummy_lst in result:
for dummy_outcome in arr:
if dummy_outcome not in dummy_lst:
... | 0 | 2016-03-19T13:29:06Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 38,793,421 | <p>I see a <em>lot</em> of iteration going on inside these recursive functions, not exactly <em>pure</em> recursion...</p>
<p>so for those of you who cannot abide by even a single loop, here's a gross, totally unnecessary fully recursive solution</p>
<pre><code>def all_insert(x, e, i=0):
return [x[0:i]+[e]+x[i:]]... | 0 | 2016-08-05T15:59:00Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
How to generate all permutations of a list in Python | 104,420 | <p>How do you generate all the permutations of a list in Python, independently of the type of elements in that list?</p>
<p>For example:</p>
<pre><code>permutations([])
[]
permutations([1])
[1]
permutations([1, 2])
[1, 2]
[2, 1]
permutations([1, 2, 3])
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
</... | 271 | 2008-09-19T18:41:03Z | 39,925,909 | <p>This way is better than the alternatives I'm seeing, check it out.</p>
<pre><code>def permutations(arr):
if not arr:
return
print arr
for idx, val in enumerate(arr):
permutations(arr[:idx]+arr[idx+1:])
</code></pre>
| 0 | 2016-10-07T21:15:05Z | [
"python",
"algorithm",
"permutation",
"combinatorics",
"python-2.5"
] |
What is "thread local storage" in Python, and why do I need it? | 104,983 | <p>In Python specifically, how do variables get shared between threads?</p>
<p>Although I have used <code>threading.Thread</code> before I never really understood or saw examples of how variables got shared. Are they shared between the main thread and the children or only among the children? When would I need to use ... | 62 | 2008-09-19T19:53:19Z | 105,025 | <p>In Python, everything is shared, except for function-local variables (because each function call gets its own set of locals, and threads are always separate function calls.) And even then, only the variables themselves (the names that refer to objects) are local to the function; objects themselves are always global,... | 48 | 2008-09-19T19:59:40Z | [
"python",
"multithreading",
"thread-local"
] |
What is "thread local storage" in Python, and why do I need it? | 104,983 | <p>In Python specifically, how do variables get shared between threads?</p>
<p>Although I have used <code>threading.Thread</code> before I never really understood or saw examples of how variables got shared. Are they shared between the main thread and the children or only among the children? When would I need to use ... | 62 | 2008-09-19T19:53:19Z | 105,058 | <p>Just like in every other language, every thread in Python has access to the same variables. There's no distinction between the 'main thread' and child threads.</p>
<p>One difference with Python is that the Global Interpreter Lock means that only one thread can be running Python code at a time. This isn't much help ... | 0 | 2008-09-19T20:03:30Z | [
"python",
"multithreading",
"thread-local"
] |
What is "thread local storage" in Python, and why do I need it? | 104,983 | <p>In Python specifically, how do variables get shared between threads?</p>
<p>Although I have used <code>threading.Thread</code> before I never really understood or saw examples of how variables got shared. Are they shared between the main thread and the children or only among the children? When would I need to use ... | 62 | 2008-09-19T19:53:19Z | 106,560 | <p>You can create thread local storage using <code>threading.local()</code>.</p>
<pre><code>>>> tls = threading.local()
>>> tls.x = 4
>>> tls.x
4
</code></pre>
<p>Data stored to the tls will be unique to each thread which will help ensure that unintentional sharing does not occur.</p>
| 13 | 2008-09-20T00:31:24Z | [
"python",
"multithreading",
"thread-local"
] |
What is "thread local storage" in Python, and why do I need it? | 104,983 | <p>In Python specifically, how do variables get shared between threads?</p>
<p>Although I have used <code>threading.Thread</code> before I never really understood or saw examples of how variables got shared. Are they shared between the main thread and the children or only among the children? When would I need to use ... | 62 | 2008-09-19T19:53:19Z | 1,894,371 | <p>Consider the following code:</p>
<pre><code>#/usr/bin/env python
from time import sleep
from random import random
from threading import Thread, local
data = local()
def bar():
print "I'm called from", data.v
def foo():
bar()
class T(Thread):
def run(self):
sleep(random())
data.v = s... | 44 | 2009-12-12T18:58:50Z | [
"python",
"multithreading",
"thread-local"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 105,127 | <p>You still need to use locks (your code could be interrupted at any time to execute another thread and this can cause data inconsistencies). The problem with GIL is that it prevents Python code from using more cores at the same time (or multiple processors if they are available).</p>
| 1 | 2008-09-19T20:11:18Z | [
"python",
"multithreading",
"locking"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 105,145 | <p>No - the GIL just protects python internals from multiple threads altering their state. This is a very low-level of locking, sufficient only to keep python's own structures in a consistent state. It doesn't cover the <em>application</em> level locking you'll need to do to cover thread safety in your own code.</p>
... | 20 | 2008-09-19T20:13:54Z | [
"python",
"multithreading",
"locking"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 105,175 | <p>This post describes the GIL at a fairly high-level:</p>
<ul>
<li><a href="https://web.archive.org/web/20080516010343/http://www.pyzine.com/Issue001/Section_Articles/article_ThreadingGlobalInterpreter.html" rel="nofollow">https://web.archive.org/web/20080516010343/http://www.pyzine.com/Issue001/Section_Articles/arti... | 6 | 2008-09-19T20:16:33Z | [
"python",
"multithreading",
"locking"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 105,272 | <p>The Global Interpreter Lock prevents threads from accessing the <em>interpreter</em> simultaneously (thus CPython only ever uses one core). However, as I understand it, the threads are still interrupted and scheduled <em>preemptively</em>, which means you still need locks on shared data structures, lest your threads... | 7 | 2008-09-19T20:24:50Z | [
"python",
"multithreading",
"locking"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 105,369 | <p>You will still need locks if you share state between threads. The GIL only protects the interpreter internally. You can still have inconsistent updates in your own code.</p>
<p>For example:</p>
<pre><code>#!/usr/bin/env python
import threading
shared_balance = 0
class Deposit(threading.Thread):
def run(self)... | 57 | 2008-09-19T20:34:40Z | [
"python",
"multithreading",
"locking"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 241,814 | <p>Adding to the discussion:</p>
<p>Because the GIL exists, some operations are atomic in Python and do not need a lock. </p>
<p><a href="http://www.python.org/doc/faq/library/#what-kinds-of-global-value-mutation-are-thread-safe">http://www.python.org/doc/faq/library/#what-kinds-of-global-value-mutation-are-thread-sa... | 7 | 2008-10-28T00:16:12Z | [
"python",
"multithreading",
"locking"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 385,446 | <p>A little bit of update from Will Harris's example:</p>
<pre><code>class Withdraw(threading.Thread):
def run(self):
for _ in xrange(1000000):
global shared_balance
if shared_balance >= 100:
balance = shared_balance
balance -= 100
shared_balance... | 0 | 2008-12-22T02:56:47Z | [
"python",
"multithreading",
"locking"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 385,887 | <p>Think of it this way:</p>
<p>On a single processor computer, multithreading happens by suspending one thread and starting another fast enough to make it appear to be running at the same time. This is like Python with the GIL: only one thread is ever actually running.</p>
<p>The problem is that the thread can be su... | 3 | 2008-12-22T09:49:35Z | [
"python",
"multithreading",
"locking"
] |
Are locks unnecessary in multi-threaded Python code because of the GIL? | 105,095 | <p>If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?</p>
<p>If the GIL doesn't allow multiple instructions to be executed in parallel, wouldn't shared data be unnecessary to protect?</p>
<p>sorry if this... | 51 | 2008-09-19T20:07:37Z | 20,408,373 | <p>Locks are still needed. I will try explaining why they are needed.</p>
<p>Any operation/instruction is executed in the interpreter. GIL ensures that interpreter is held by a single thread at <strong>a particular instant of time</strong>. And your program with multiple threads works in a single interpreter. At any p... | 1 | 2013-12-05T19:01:46Z | [
"python",
"multithreading",
"locking"
] |
Making a beta code for a public django site | 105,702 | <p>I'm about to put a beta version of the site I'm working on up on the web. It needs to have a beta code to restrict access. The site is written in django.</p>
<p>I don't want to change the fundamental Auth system to accommodate a beta code, and I don't care particularly that the security of the beta code is iron-cla... | 11 | 2008-09-19T21:17:20Z | 105,756 | <p>You should be able to add <code>@login_required</code> decorators across the board and be done with it. Unless you have a boat-load of view functions, it shouldn't be too horrible.</p>
| 0 | 2008-09-19T21:24:56Z | [
"python",
"django",
"authentication",
"django-authentication"
] |
Making a beta code for a public django site | 105,702 | <p>I'm about to put a beta version of the site I'm working on up on the web. It needs to have a beta code to restrict access. The site is written in django.</p>
<p>I don't want to change the fundamental Auth system to accommodate a beta code, and I don't care particularly that the security of the beta code is iron-cla... | 11 | 2008-09-19T21:17:20Z | 105,764 | <p>You can probably restrict access to the entire site via apache with htaccess, taking the problem out of the django's project space entirely.</p>
| 4 | 2008-09-19T21:25:38Z | [
"python",
"django",
"authentication",
"django-authentication"
] |
Making a beta code for a public django site | 105,702 | <p>I'm about to put a beta version of the site I'm working on up on the web. It needs to have a beta code to restrict access. The site is written in django.</p>
<p>I don't want to change the fundamental Auth system to accommodate a beta code, and I don't care particularly that the security of the beta code is iron-cla... | 11 | 2008-09-19T21:17:20Z | 105,945 | <p>Do what StackOverflow did.</p>
<p>They had a simple email/password form. It had a single hard-coded password (falkensmaze). When the user gets the password right set a cookie. eg. auth=1</p>
<p>Don't worry about it being unsecure. Who care's if someone hacks into the beta?</p>
<p>Apache/htaccess is also a nice an... | 2 | 2008-09-19T21:51:26Z | [
"python",
"django",
"authentication",
"django-authentication"
] |
Making a beta code for a public django site | 105,702 | <p>I'm about to put a beta version of the site I'm working on up on the web. It needs to have a beta code to restrict access. The site is written in django.</p>
<p>I don't want to change the fundamental Auth system to accommodate a beta code, and I don't care particularly that the security of the beta code is iron-cla... | 11 | 2008-09-19T21:17:20Z | 106,212 | <p>Start with <a href="http://www.djangosnippets.org/snippets/136/">this Django snippet</a>, but modify it to check <code>request.session['has_beta_access']</code>. If they don't have it, then have it return a redirect to a "enter beta code" page that, when posted to with the right code, sets that session variable to <... | 18 | 2008-09-19T22:43:40Z | [
"python",
"django",
"authentication",
"django-authentication"
] |
Making a beta code for a public django site | 105,702 | <p>I'm about to put a beta version of the site I'm working on up on the web. It needs to have a beta code to restrict access. The site is written in django.</p>
<p>I don't want to change the fundamental Auth system to accommodate a beta code, and I don't care particularly that the security of the beta code is iron-cla... | 11 | 2008-09-19T21:17:20Z | 1,249,496 | <p>I'm not sure what version of the Pinax code you're using, but they've built in the ability to close the site off for a private beta so you don't need to do much work yourself.</p>
<p>The link to the specific project template for a private beta site is here: <a href="http://github.com/pinax/pinax/tree/3ad73d1ba44f37... | 0 | 2009-08-08T17:56:36Z | [
"python",
"django",
"authentication",
"django-authentication"
] |
Making a beta code for a public django site | 105,702 | <p>I'm about to put a beta version of the site I'm working on up on the web. It needs to have a beta code to restrict access. The site is written in django.</p>
<p>I don't want to change the fundamental Auth system to accommodate a beta code, and I don't care particularly that the security of the beta code is iron-cla... | 11 | 2008-09-19T21:17:20Z | 1,770,902 | <p>Great snippet but it resulted lots of problems for me related OpenId sessions. So I end up relying on Cookies instead of the Session:</p>
<pre><code>class BetaMiddleware(object):
"""
Require beta code cookie key in order to view any page.
"""
set_beta = False
def process_request(self, request):
... | 0 | 2009-11-20T14:47:12Z | [
"python",
"django",
"authentication",
"django-authentication"
] |
Why unicode() uses str() on my object only with no encoding given? | 106,630 | <p>I start by creating a string variable with some <strong>non-ascii</strong> <em>utf-8</em> encoded data on it:</p>
<pre><code>>>> text = 'á'
>>> text
'\xc3\xa1'
>>> text.decode('utf-8')
u'\xe1'
</code></pre>
<p>Using <code>unicode()</code> on it raises errors...</p>
<pre><code>>>&... | 5 | 2008-09-20T00:53:24Z | 106,640 | <p><code>unicode</code> does not guess the encoding of your text. If your object can print itself as <code>unicode</code>, define the <code>__unicode__()</code> method that returns a Unicode string.</p>
<p><hr /></p>
<p>The secret is that <code>unicode(r)</code> is not actually calling <code>__str__()</code> itself. ... | 4 | 2008-09-20T00:58:52Z | [
"python",
"unicode",
"encoding"
] |
Why unicode() uses str() on my object only with no encoding given? | 106,630 | <p>I start by creating a string variable with some <strong>non-ascii</strong> <em>utf-8</em> encoded data on it:</p>
<pre><code>>>> text = 'á'
>>> text
'\xc3\xa1'
>>> text.decode('utf-8')
u'\xe1'
</code></pre>
<p>Using <code>unicode()</code> on it raises errors...</p>
<pre><code>>>&... | 5 | 2008-09-20T00:53:24Z | 106,709 | <p>The behaviour does seem confusing, but intensional. I reproduce here the entirety of the unicode documentation from the <a href="http://docs.python.org/lib/built-in-funcs.html" rel="nofollow">Python Built-In Functions documentation</a> (for version 2.5.2, as I write this):</p>
<blockquote>
<p><strong>unicode([obj... | 7 | 2008-09-20T01:32:09Z | [
"python",
"unicode",
"encoding"
] |
How to bundle a Python application including dependencies? | 106,725 | <p>I need to package my python application, its dependencies and python into a single MSI installer. The end result should desirably be:</p>
<ul>
<li>Python is installed in the standard location</li>
<li>the package and its dependencies are installed in a separate directory (possibly site-packages)</li>
<li>the instal... | 43 | 2008-09-20T01:39:48Z | 106,730 | <p>Kind of a dup of this question about <a href="http://stackoverflow.com/questions/2933/an-executable-python-app"> how to make a python into an executable</a>. </p>
<p>It boils down to: </p>
<p><a href="http://www.py2exe.org/">py2exe</a> on windows, <a href="http://wiki.python.org/moin/Freeze">Freeze</a> on Linux, a... | 25 | 2008-09-20T01:41:20Z | [
"python",
"tkinter",
"packaging"
] |
How to bundle a Python application including dependencies? | 106,725 | <p>I need to package my python application, its dependencies and python into a single MSI installer. The end result should desirably be:</p>
<ul>
<li>Python is installed in the standard location</li>
<li>the package and its dependencies are installed in a separate directory (possibly site-packages)</li>
<li>the instal... | 43 | 2008-09-20T01:39:48Z | 106,731 | <p>py2exe is the best way to do this. It's a bit of a PITA to use, but the end result works very well.</p>
| 1 | 2008-09-20T01:41:36Z | [
"python",
"tkinter",
"packaging"
] |
How to bundle a Python application including dependencies? | 106,725 | <p>I need to package my python application, its dependencies and python into a single MSI installer. The end result should desirably be:</p>
<ul>
<li>Python is installed in the standard location</li>
<li>the package and its dependencies are installed in a separate directory (possibly site-packages)</li>
<li>the instal... | 43 | 2008-09-20T01:39:48Z | 106,733 | <p><a href="http://wiki.python.org/moin/Py2Exe" rel="nofollow">py2exe</a> will make windows executables with python bundled in.</p>
| 1 | 2008-09-20T01:41:47Z | [
"python",
"tkinter",
"packaging"
] |
How to bundle a Python application including dependencies? | 106,725 | <p>I need to package my python application, its dependencies and python into a single MSI installer. The end result should desirably be:</p>
<ul>
<li>Python is installed in the standard location</li>
<li>the package and its dependencies are installed in a separate directory (possibly site-packages)</li>
<li>the instal... | 43 | 2008-09-20T01:39:48Z | 106,756 | <p>I use <a href="http://pyinstaller.python-hosting.com/">PyInstaller</a> (the svn version) to create a stand-alone version of my program that includes Python and all the dependencies. It takes a little fiddling to get it to work right and include everything (as does py2exe and other similar programs, see <a href="htt... | 13 | 2008-09-20T01:52:20Z | [
"python",
"tkinter",
"packaging"
] |
How to bundle a Python application including dependencies? | 106,725 | <p>I need to package my python application, its dependencies and python into a single MSI installer. The end result should desirably be:</p>
<ul>
<li>Python is installed in the standard location</li>
<li>the package and its dependencies are installed in a separate directory (possibly site-packages)</li>
<li>the instal... | 43 | 2008-09-20T01:39:48Z | 114,717 | <p>My company uses the free InnoSetup tool. It is a moderately complex program that has tons of flexibility for building installers for windows. I believe that it creates .exe and not .msi files, however. InnoSetup is not python specific but we have created an installer for one of our products that installs python a... | 4 | 2008-09-22T12:58:57Z | [
"python",
"tkinter",
"packaging"
] |
How to bundle a Python application including dependencies? | 106,725 | <p>I need to package my python application, its dependencies and python into a single MSI installer. The end result should desirably be:</p>
<ul>
<li>Python is installed in the standard location</li>
<li>the package and its dependencies are installed in a separate directory (possibly site-packages)</li>
<li>the instal... | 43 | 2008-09-20T01:39:48Z | 18,735,126 | <p>I've had much better results with dependencies and custom folder structures using <a href="http://www.pyinstaller.org/" rel="nofollow">pyinstaller</a>, and it lets you find and specify hidden imports and hooks for larger dependencies like numpy and scipy. Also a PITA, though.</p>
| 3 | 2013-09-11T07:28:07Z | [
"python",
"tkinter",
"packaging"
] |
How to bundle a Python application including dependencies? | 106,725 | <p>I need to package my python application, its dependencies and python into a single MSI installer. The end result should desirably be:</p>
<ul>
<li>Python is installed in the standard location</li>
<li>the package and its dependencies are installed in a separate directory (possibly site-packages)</li>
<li>the instal... | 43 | 2008-09-20T01:39:48Z | 29,111,274 | <p>Ok, I have used py2exe before and it works perfectly except for one thing... It only works on executable windows machines. I then learned about Jython which turn a python script into a .Jar file. Which as you know is executable from any machine that has Java ("To your latest running version") installed. Which is gre... | 0 | 2015-03-17T22:56:41Z | [
"python",
"tkinter",
"packaging"
] |
Unit Testing File Modifications | 106,766 | <p>A common task in programs I've been working on lately is modifying a text file in some way. (Hey, I'm on Linux. Everything's a file. And I do large-scale system admin.)</p>
<p>But the file the code modifies may not exist on my desktop box. And I probably don't want to modify it if it IS on my desktop.</p>
<p>I've ... | 22 | 2008-09-20T01:56:25Z | 106,772 | <p>When I touch files in my code, I tend to prefer to mock the actual reading and writing of the file... so then I can give my classes exact contents I want in the test, and then assert that the test is writing back the contents I expect.</p>
<p>I've done this in Java, and I imagine it is quite simple in Python... but... | 2 | 2008-09-20T02:01:20Z | [
"python",
"linux",
"unit-testing"
] |
Unit Testing File Modifications | 106,766 | <p>A common task in programs I've been working on lately is modifying a text file in some way. (Hey, I'm on Linux. Everything's a file. And I do large-scale system admin.)</p>
<p>But the file the code modifies may not exist on my desktop box. And I probably don't want to modify it if it IS on my desktop.</p>
<p>I've ... | 22 | 2008-09-20T01:56:25Z | 106,779 | <p>I think you are on the right track. Depending on what you need to do <a href="http://en.wikipedia.org/wiki/Chroot" rel="nofollow">chroot</a> may help you set up an environment for your scrpits that 'looks' real, but isn't.</p>
<p>If that doesn't work then you could write your scripts to take a 'root' path as an ar... | 1 | 2008-09-20T02:04:47Z | [
"python",
"linux",
"unit-testing"
] |
Unit Testing File Modifications | 106,766 | <p>A common task in programs I've been working on lately is modifying a text file in some way. (Hey, I'm on Linux. Everything's a file. And I do large-scale system admin.)</p>
<p>But the file the code modifies may not exist on my desktop box. And I probably don't want to modify it if it IS on my desktop.</p>
<p>I've ... | 22 | 2008-09-20T01:56:25Z | 106,780 | <p>You have two levels of testing.</p>
<ol>
<li><p>Filtering and Modifying content. These are "low-level" operations that don't really require physical file I/O. These are the tests, decision-making, alternatives, etc. The "Logic" of the application.</p></li>
<li><p>File system operations. Create, copy, rename, de... | 6 | 2008-09-20T02:05:23Z | [
"python",
"linux",
"unit-testing"
] |
Unit Testing File Modifications | 106,766 | <p>A common task in programs I've been working on lately is modifying a text file in some way. (Hey, I'm on Linux. Everything's a file. And I do large-scale system admin.)</p>
<p>But the file the code modifies may not exist on my desktop box. And I probably don't want to modify it if it IS on my desktop.</p>
<p>I've ... | 22 | 2008-09-20T01:56:25Z | 106,781 | <p>You might want to setup the test so that it runs inside a chroot jail, so you have all the environment the test needs, even if paths and file locations are hardcoded in the code [not really a good practice, but sometimes one gets the file locations from other places...] and then check the results via the exit code.<... | 1 | 2008-09-20T02:06:19Z | [
"python",
"linux",
"unit-testing"
] |
Unit Testing File Modifications | 106,766 | <p>A common task in programs I've been working on lately is modifying a text file in some way. (Hey, I'm on Linux. Everything's a file. And I do large-scale system admin.)</p>
<p>But the file the code modifies may not exist on my desktop box. And I probably don't want to modify it if it IS on my desktop.</p>
<p>I've ... | 22 | 2008-09-20T01:56:25Z | 111,199 | <p>You're talking about testing too much at once. If you start trying to attack a testing problem by saying "Let's verify that it modifies its environment correctly", you're doomed to failure. Environments have dozens, maybe even millions of potential variations.</p>
<p>Instead, look at the pieces ("units") of your ... | 14 | 2008-09-21T15:16:34Z | [
"python",
"linux",
"unit-testing"
] |
Unit Testing File Modifications | 106,766 | <p>A common task in programs I've been working on lately is modifying a text file in some way. (Hey, I'm on Linux. Everything's a file. And I do large-scale system admin.)</p>
<p>But the file the code modifies may not exist on my desktop box. And I probably don't want to modify it if it IS on my desktop.</p>
<p>I've ... | 22 | 2008-09-20T01:56:25Z | 1,866,162 | <p>For later readers who just want a way to test that code writing to files is working correctly, here is a "fake_open" that patches the open builtin of a module to use StringIO. fake_open returns a dict of opened files which can be examined in a unit test or doctest, all without needing a real file-system.</p>
<pre... | 3 | 2009-12-08T10:45:40Z | [
"python",
"linux",
"unit-testing"
] |
How best to draw in the console? | 106,850 | <p>I'm trying to write a console (as in terminal, not gaming console) pong game in python and I'm having trouble figuring how best to (re)draw the game.</p>
<p>I was thinking of having an 2d array as a sort of bitmap, editing the array to reflect the ball/paddles new positions and then casting each row to a string and... | 5 | 2008-09-20T02:45:45Z | 106,851 | <p>It looks like there is a <a href="http://en.wikipedia.org/wiki/Curses_(programming_library)" rel="nofollow">curses</a> port/library for Python:</p>
<p><a href="https://docs.python.org/library/curses.html" rel="nofollow">https://docs.python.org/library/curses.html</a></p>
| 5 | 2008-09-20T02:47:18Z | [
"python",
"console",
"stdout"
] |
How best to draw in the console? | 106,850 | <p>I'm trying to write a console (as in terminal, not gaming console) pong game in python and I'm having trouble figuring how best to (re)draw the game.</p>
<p>I was thinking of having an 2d array as a sort of bitmap, editing the array to reflect the ball/paddles new positions and then casting each row to a string and... | 5 | 2008-09-20T02:45:45Z | 106,853 | <p>I would investigate using the <a href="http://python.org/doc/2.5/lib/module-curses.html" rel="nofollow">curses</a> module. It will take care of a lot of the details and let you focus on the higher level stuff.</p>
| 0 | 2008-09-20T02:48:12Z | [
"python",
"console",
"stdout"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.