title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
Recurrence sequence in Java / Python / Mathematica | 1,540,519 | <p><strong>How can you write the following statement in the given languages?</strong></p>
<pre><code>a(0) = 1
a_(n+1) = 1 - 1 / ( a_n + 3)
</code></pre>
<p>I need to find the smallest value of <code>n</code> when <code>a_n -> 0.732050...</code>.</p>
<p><strong>My attempt in Mathematica</strong></p>
<pre><code>a[... | 3 | 2009-10-08T21:12:03Z | 1,540,642 | <h3>Mathematica</h3>
<pre><code>a[0] = 1;
a[n_] := a[n] = 1 - 1/(a[n-1] + 3)
</code></pre>
<p>(Note the <a href="http://stackoverflow.com/questions/129877/how-do-i-write-a-generic-memoize-function/173038#173038">memoization trick</a>.)</p>
<p>Also, a[n] converges (very quickly) to sqrt(3)-1:</p>
<pre><code>Solve[x ... | 8 | 2009-10-08T21:36:39Z | [
"java",
"python",
"wolfram-mathematica",
"sequence"
] |
Recurrence sequence in Java / Python / Mathematica | 1,540,519 | <p><strong>How can you write the following statement in the given languages?</strong></p>
<pre><code>a(0) = 1
a_(n+1) = 1 - 1 / ( a_n + 3)
</code></pre>
<p>I need to find the smallest value of <code>n</code> when <code>a_n -> 0.732050...</code>.</p>
<p><strong>My attempt in Mathematica</strong></p>
<pre><code>a[... | 3 | 2009-10-08T21:12:03Z | 1,540,722 | <p><strong>Python</strong></p>
<pre><code>next = lambda x: 1.0 - (1.0 / (float(x) + 3.0))
last, z, count = -1, 0.0, 0
while last != z:
print count, z
last, z, count = z, next(z), count+1
</code></pre>
<p>I try to avoid writing "while True" or such if I can avoid it. Almost certainly no code that I write will loop... | 2 | 2009-10-08T21:55:18Z | [
"java",
"python",
"wolfram-mathematica",
"sequence"
] |
Recurrence sequence in Java / Python / Mathematica | 1,540,519 | <p><strong>How can you write the following statement in the given languages?</strong></p>
<pre><code>a(0) = 1
a_(n+1) = 1 - 1 / ( a_n + 3)
</code></pre>
<p>I need to find the smallest value of <code>n</code> when <code>a_n -> 0.732050...</code>.</p>
<p><strong>My attempt in Mathematica</strong></p>
<pre><code>a[... | 3 | 2009-10-08T21:12:03Z | 1,541,755 | <p>Python, simplest:</p>
<pre><code>def a(n):
if n == 0: return 1
return 1 - 1 / float(a(n-1) + 3)
# limit is sqrt(3) - 1
limit = 3.0 ** 0.5 - 1.0
# get 9 digits' precision
i = 0
while abs(a(i) - limit) > 1.0e-9:
i += 1
print i
</code></pre>
<p>This emits <code>8</code>, suggesting that optimizations such... | 4 | 2009-10-09T04:13:07Z | [
"java",
"python",
"wolfram-mathematica",
"sequence"
] |
Recurrence sequence in Java / Python / Mathematica | 1,540,519 | <p><strong>How can you write the following statement in the given languages?</strong></p>
<pre><code>a(0) = 1
a_(n+1) = 1 - 1 / ( a_n + 3)
</code></pre>
<p>I need to find the smallest value of <code>n</code> when <code>a_n -> 0.732050...</code>.</p>
<p><strong>My attempt in Mathematica</strong></p>
<pre><code>a[... | 3 | 2009-10-08T21:12:03Z | 5,708,211 | <p>A one-liner in Mathematica which gives a list of exact elements of your sequence:</p>
<pre><code>In[66]:= NestWhileList[1 - 1/(#1 + 3) &, 1,
RealExponent[Subtract[##]] > -8 &, 2]
Out[66]= {1, 3/4, 11/15, 41/56, 153/209, 571/780, 2131/2911, \
7953/10864, 29681/40545}
</code></pre>
<p>The difference be... | 1 | 2011-04-18T20:01:44Z | [
"java",
"python",
"wolfram-mathematica",
"sequence"
] |
Java Equivalent to Python Dictionaries | 1,540,673 | <p>I am a long time user of Python and really like the way that the dictionaries are used. They are very intuitive and easy to use. Is there a good Java equivalent to python's dictionaries? I have heard of people using hashmaps and hashtables. Could someone explain the similarities and differences of using hashtables a... | 59 | 2009-10-08T21:44:34Z | 1,540,683 | <p>As far as I'm aware (I don't actually use java) dictionaries are just another name for a hashmap/hashtable.</p>
<p>Grabbing code from <a href="http://www.fluffycat.com/Java/HashMaps/" rel="nofollow">http://www.fluffycat.com/Java/HashMaps/</a> it seems they are used in a very similar manner, with a bit of extra java... | 3 | 2009-10-08T21:46:16Z | [
"java",
"python",
"hash",
"dictionary"
] |
Java Equivalent to Python Dictionaries | 1,540,673 | <p>I am a long time user of Python and really like the way that the dictionaries are used. They are very intuitive and easy to use. Is there a good Java equivalent to python's dictionaries? I have heard of people using hashmaps and hashtables. Could someone explain the similarities and differences of using hashtables a... | 59 | 2009-10-08T21:44:34Z | 1,540,699 | <p>Python's <code>dict</code> class is an implementation of what the Python documentation informally calls "<a href="http://docs.python.org/library/stdtypes.html#mapping-types-dict">mapping types</a>". Internally, <code>dict</code> is implemented using a hashtable.</p>
<p>Java's <a href="http://java.sun.com/javase/6/... | 59 | 2009-10-08T21:49:58Z | [
"java",
"python",
"hash",
"dictionary"
] |
Java Equivalent to Python Dictionaries | 1,540,673 | <p>I am a long time user of Python and really like the way that the dictionaries are used. They are very intuitive and easy to use. Is there a good Java equivalent to python's dictionaries? I have heard of people using hashmaps and hashtables. Could someone explain the similarities and differences of using hashtables a... | 59 | 2009-10-08T21:44:34Z | 1,540,813 | <p>One difference between the two is that <code>dict</code> has stricter requirements as to what data types can act as a key. Java will allow any object to work as a key -- although you should take care to ensure that the object's <code>hashCode()</code> method returns a unique value that reflects its internal state. ... | 3 | 2009-10-08T22:16:35Z | [
"java",
"python",
"hash",
"dictionary"
] |
Java Equivalent to Python Dictionaries | 1,540,673 | <p>I am a long time user of Python and really like the way that the dictionaries are used. They are very intuitive and easy to use. Is there a good Java equivalent to python's dictionaries? I have heard of people using hashmaps and hashtables. Could someone explain the similarities and differences of using hashtables a... | 59 | 2009-10-08T21:44:34Z | 39,432,556 | <p>The idea of dictionary and Map is similar. Both contain elements like</p>
<pre><code>key1:value1, key2:value2 ... and so on
</code></pre>
<p>In Java, <code>Map</code> is implemented different ways like <code>HashMap</code>, or <code>TreeMap</code> etc. <code>put(), get()</code> operations are similar</p>
<pre><co... | 1 | 2016-09-11T03:26:10Z | [
"java",
"python",
"hash",
"dictionary"
] |
how to force python httplib library to use only A requests | 1,540,749 | <p>The problem is that urllib using httplib is querying fro AAAA records what i would like to avoid. Is there a nice way to do that?</p>
<pre><code>>>> import socket
>>> socket.gethostbyname('www.python.org')
'82.94.164.162'
21:52:37.302028 IP 192.168.0.9.44992 > 192.168.0.1.53: 27463+ A? www.py... | 2 | 2009-10-08T22:01:50Z | 1,540,776 | <p>Look here: <a href="http://stackoverflow.com/questions/1189253/how-do-i-resolve-an-srv-record-in-python">how-do-i-resolve-an-srv-record-in-python</a></p>
<p>Once you resolved the correct A ip, use it in your request, instead of the dns.</p>
| 1 | 2009-10-08T22:07:10Z | [
"python",
"dns",
"ipv6",
"ipv4"
] |
how to force python httplib library to use only A requests | 1,540,749 | <p>The problem is that urllib using httplib is querying fro AAAA records what i would like to avoid. Is there a nice way to do that?</p>
<pre><code>>>> import socket
>>> socket.gethostbyname('www.python.org')
'82.94.164.162'
21:52:37.302028 IP 192.168.0.9.44992 > 192.168.0.1.53: 27463+ A? www.py... | 2 | 2009-10-08T22:01:50Z | 1,546,412 | <p>The correct answer is:</p>
<p><a href="http://docs.python.org/library/socket.html">http://docs.python.org/library/socket.html</a></p>
<p>The Python socket library is using the following:</p>
<p>socket.socket([family[, type[, proto]]])
Create a new socket using the given address family, socket type and protocol nu... | 6 | 2009-10-09T22:43:12Z | [
"python",
"dns",
"ipv6",
"ipv4"
] |
Dumping a multiprocessing.Queue into a list | 1,540,822 | <p>I wish to dump a <code>multiprocessing.Queue</code> into a list. For that task I've written the following function:</p>
<pre><code>import Queue
def dump_queue(queue):
"""
Empties all pending items in a queue and returns them in a list.
"""
result = []
# START DEBUG CODE
initial_size = queu... | 14 | 2009-10-08T22:17:37Z | 1,541,117 | <p>Try this:</p>
<pre><code>import Queue
import time
def dump_queue(queue):
"""
Empties all pending items in a queue and returns them in a list.
"""
result = []
for i in iter(queue.get, 'STOP'):
result.append(i)
time.sleep(.1)
return result
import multiprocessing
q = multiprocess... | 18 | 2009-10-08T23:36:18Z | [
"python",
"queue",
"multiprocessing"
] |
Dumping a multiprocessing.Queue into a list | 1,540,822 | <p>I wish to dump a <code>multiprocessing.Queue</code> into a list. For that task I've written the following function:</p>
<pre><code>import Queue
def dump_queue(queue):
"""
Empties all pending items in a queue and returns them in a list.
"""
result = []
# START DEBUG CODE
initial_size = queu... | 14 | 2009-10-08T22:17:37Z | 39,765,300 | <p>In some situations we already computed everything and we want just to convert the Queue.</p>
<pre><code>shared_queue = Queue()
shared_queue_list = []
...
join() #All process are joined
while shared_queue.qsize() != 0:
shared_queue_list.append(shared_queue.get())
</code></pre>
<p>Now shared_queue_list has the r... | 2 | 2016-09-29T08:33:54Z | [
"python",
"queue",
"multiprocessing"
] |
How make a better markdown for developer blog | 1,540,834 | <p>I'm rebuilding my blog at <a href="http://www.elmalabarista.com/blog/" rel="nofollow">http://www.elmalabarista.com/blog/</a>. I have use in my previous version markdown and now I remember why I have almost zero code samples. Doing code samples in markdown is very fragile.</p>
<p>I try to put some python there I can... | 1 | 2009-10-08T22:21:13Z | 1,540,849 | <p>Consider using <a href="http://docutils.sourceforge.net/rst.html" rel="nofollow">reStructuredText</a> -- it's the standard lightweight markup for Python, and is often used for docstrings and embedded documentation. It's quite easy, but also powerful -- if I remember correctly, the core Python libraries and Django bo... | 3 | 2009-10-08T22:26:00Z | [
"python",
"markdown",
"code-formatting"
] |
How make a better markdown for developer blog | 1,540,834 | <p>I'm rebuilding my blog at <a href="http://www.elmalabarista.com/blog/" rel="nofollow">http://www.elmalabarista.com/blog/</a>. I have use in my previous version markdown and now I remember why I have almost zero code samples. Doing code samples in markdown is very fragile.</p>
<p>I try to put some python there I can... | 1 | 2009-10-08T22:21:13Z | 1,540,958 | <p>I've been using <a href="http://code.google.com/p/google-code-prettify/" rel="nofollow">google-code-prettify</a>, which works pretty well.</p>
<p><strong><a href="http://google-code-prettify.googlecode.com/svn/trunk/README.html" rel="nofollow">Usage</a></strong>:</p>
<p>Put code snippets in <pre class="prettypr... | 1 | 2009-10-08T22:52:05Z | [
"python",
"markdown",
"code-formatting"
] |
How make a better markdown for developer blog | 1,540,834 | <p>I'm rebuilding my blog at <a href="http://www.elmalabarista.com/blog/" rel="nofollow">http://www.elmalabarista.com/blog/</a>. I have use in my previous version markdown and now I remember why I have almost zero code samples. Doing code samples in markdown is very fragile.</p>
<p>I try to put some python there I can... | 1 | 2009-10-08T22:21:13Z | 2,082,939 | <p>You need to indent code more than 4 spaces (btw have you noticed on SO if you add 4 spaces it gets recognized as code), this is 4 space indented:</p>
<pre><code>:::python
def hello():
pass
</code></pre>
| 1 | 2010-01-17T22:37:34Z | [
"python",
"markdown",
"code-formatting"
] |
Trace/BPT trap with Python threading module | 1,540,835 | <p>The following code dies with <code>Trace/BPT trap</code>:</p>
<pre><code>from tvdb_api import Tvdb
from threading import Thread
class GrabStuff(Thread):
def run(self):
t = Tvdb()
def main():
threads = [GrabStuff() for x in range(1)]
[x.start() for x in threads]
[x.join() for x in threads]
... | 1 | 2009-10-08T22:21:16Z | 1,547,316 | <p>Bad things can happen when importing modules for the first time in a thread on OS X 10.6. See, for instance, <a href="http://bugs.python.org/issue7085" rel="nofollow">this issue</a>. As a workaround, try looking through Tvdb and add its complete chain of imports to the main module.</p>
| 3 | 2009-10-10T07:41:55Z | [
"python",
"multithreading"
] |
Python 'object' type and inheritance | 1,540,975 | <p>In Python I can define a class 'foo' in the following ways:</p>
<pre><code>class foo:
pass
</code></pre>
<p>or</p>
<pre><code>class foo(object):
pass
</code></pre>
<p>What is the difference? I have tried to use the function issubclass(foo, object) to see if it returns True for both class definitions. I... | 4 | 2009-10-08T22:56:28Z | 1,540,998 | <p>Inheriting from <code>object</code> makes a class a "new-style class". There is a discussion of old-style vs. new-style here: <a href="http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python">Old style and new style classes in Python</a></p>
<p>As @CrazyJugglerDrummer commented below, in... | 9 | 2009-10-08T23:04:13Z | [
"python",
"inheritance",
"object"
] |
Python 'object' type and inheritance | 1,540,975 | <p>In Python I can define a class 'foo' in the following ways:</p>
<pre><code>class foo:
pass
</code></pre>
<p>or</p>
<pre><code>class foo(object):
pass
</code></pre>
<p>What is the difference? I have tried to use the function issubclass(foo, object) to see if it returns True for both class definitions. I... | 4 | 2009-10-08T22:56:28Z | 1,541,003 | <p>The first creates an "old-style" class, which are deprecated and have been removed in Python 3. You should not use it in Python 2.x. See the documentation for the <a href="http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes" rel="nofollow">Python data model</a>.</p>
| 2 | 2009-10-08T23:04:59Z | [
"python",
"inheritance",
"object"
] |
Python 'object' type and inheritance | 1,540,975 | <p>In Python I can define a class 'foo' in the following ways:</p>
<pre><code>class foo:
pass
</code></pre>
<p>or</p>
<pre><code>class foo(object):
pass
</code></pre>
<p>What is the difference? I have tried to use the function issubclass(foo, object) to see if it returns True for both class definitions. I... | 4 | 2009-10-08T22:56:28Z | 1,541,004 | <p><em>Old style</em> and <em>new style</em> objects... they have sightly different behaviours, for example in the constructors, or in the method resolution order in multiple inheritance.</p>
| 2 | 2009-10-08T23:05:12Z | [
"python",
"inheritance",
"object"
] |
Bubble Breaker Game Solver better than greedy? | 1,541,101 | <p>For a mental exercise I decided to try and solve the bubble breaker game found on many cell phones as well as an example here:<a href="http://www.kongregate.com/games/Sevas/bubble-breaker-game" rel="nofollow">Bubble Break Game</a></p>
<ul>
<li>The random (N,M,C) board consists N rows x M columns with C colors</li>
... | 4 | 2009-10-08T23:29:18Z | 1,541,124 | <p>This isn't my area of expertise, but I would like to recommend a book to you. Get a copy of <a href="http://www.algorist.com/" rel="nofollow">The Algorithm Design Manual</a> by Steven Skiena. This has a whole list of different algorithms, and once you read through it you can use it as a reference. If nothing else... | 0 | 2009-10-08T23:37:57Z | [
"python",
"algorithm",
"language-agnostic"
] |
Bubble Breaker Game Solver better than greedy? | 1,541,101 | <p>For a mental exercise I decided to try and solve the bubble breaker game found on many cell phones as well as an example here:<a href="http://www.kongregate.com/games/Sevas/bubble-breaker-game" rel="nofollow">Bubble Break Game</a></p>
<ul>
<li>The random (N,M,C) board consists N rows x M columns with C colors</li>
... | 4 | 2009-10-08T23:29:18Z | 1,541,206 | <p>According to <a href="http://erikdemaine.org/clickomania/">this paper</a>, determining if you can empty the board (which is related to the problem you want to solve) is NP-Complete. That doesn't mean that you won't be able to find a good algorithm, it just means that you likely won't find an efficient one.</p>
| 7 | 2009-10-09T00:01:34Z | [
"python",
"algorithm",
"language-agnostic"
] |
Bubble Breaker Game Solver better than greedy? | 1,541,101 | <p>For a mental exercise I decided to try and solve the bubble breaker game found on many cell phones as well as an example here:<a href="http://www.kongregate.com/games/Sevas/bubble-breaker-game" rel="nofollow">Bubble Break Game</a></p>
<ul>
<li>The random (N,M,C) board consists N rows x M columns with C colors</li>
... | 4 | 2009-10-08T23:29:18Z | 1,541,209 | <p>I'm thinking you could try a branch and bound search with the following idea:</p>
<ol>
<li><p>Given a state of the game S, you branch on S by breaking it up in m sets Si where each Si is the state after taking a legal move of all m legal moves given the state S</p></li>
<li><p>You need two functions U(S) and L(S) t... | 1 | 2009-10-09T00:02:44Z | [
"python",
"algorithm",
"language-agnostic"
] |
Bubble Breaker Game Solver better than greedy? | 1,541,101 | <p>For a mental exercise I decided to try and solve the bubble breaker game found on many cell phones as well as an example here:<a href="http://www.kongregate.com/games/Sevas/bubble-breaker-game" rel="nofollow">Bubble Break Game</a></p>
<ul>
<li>The random (N,M,C) board consists N rows x M columns with C colors</li>
... | 4 | 2009-10-08T23:29:18Z | 1,541,230 | <p>To get a faster solution than exhaustive search, I think what you want is probably dynamic programming. In dynamic programming, you find some sort of "step" that takes you possibly closer to your solution, and keep track of the results of each step in a big matrix. Then, once you have filled in the matrix, you can... | 1 | 2009-10-09T00:10:10Z | [
"python",
"algorithm",
"language-agnostic"
] |
Bubble Breaker Game Solver better than greedy? | 1,541,101 | <p>For a mental exercise I decided to try and solve the bubble breaker game found on many cell phones as well as an example here:<a href="http://www.kongregate.com/games/Sevas/bubble-breaker-game" rel="nofollow">Bubble Break Game</a></p>
<ul>
<li>The random (N,M,C) board consists N rows x M columns with C colors</li>
... | 4 | 2009-10-08T23:29:18Z | 1,542,050 | <p>You can translate this problem into problem of searching shortest path on graph. <a href="http://en.wikipedia.org/wiki/Shortest%5Fpath%5Fproblem" rel="nofollow">http://en.wikipedia.org/wiki/Shortest%5Fpath%5Fproblem</a></p>
<p>I would try whit A* and heuristics would include number of islands.</p>
| 1 | 2009-10-09T06:09:15Z | [
"python",
"algorithm",
"language-agnostic"
] |
Bubble Breaker Game Solver better than greedy? | 1,541,101 | <p>For a mental exercise I decided to try and solve the bubble breaker game found on many cell phones as well as an example here:<a href="http://www.kongregate.com/games/Sevas/bubble-breaker-game" rel="nofollow">Bubble Break Game</a></p>
<ul>
<li>The random (N,M,C) board consists N rows x M columns with C colors</li>
... | 4 | 2009-10-08T23:29:18Z | 1,542,558 | <p>In my chess program I use some ideas which could probably adapted to this problem.</p>
<ul>
<li><p>Move Ordering. First find all
possible moves, store them in a list,
and sort them according to some
heuristic. The "better" ones first,
the "bad" ones last. For example,
this could be a function of the size
of the gro... | 1 | 2009-10-09T08:27:33Z | [
"python",
"algorithm",
"language-agnostic"
] |
Bubble Breaker Game Solver better than greedy? | 1,541,101 | <p>For a mental exercise I decided to try and solve the bubble breaker game found on many cell phones as well as an example here:<a href="http://www.kongregate.com/games/Sevas/bubble-breaker-game" rel="nofollow">Bubble Break Game</a></p>
<ul>
<li>The random (N,M,C) board consists N rows x M columns with C colors</li>
... | 4 | 2009-10-08T23:29:18Z | 3,044,739 | <p>I'm almost finished writing my version of the "solver" in Java. It does both exhaustive search, which takes fricking ages for larger board sizes, and a directed search based on a "pool" of possible paths, which is pruned after every generation, and a fitness function used to prune the pool. I'm just trying to tune... | 1 | 2010-06-15T11:38:38Z | [
"python",
"algorithm",
"language-agnostic"
] |
Difference between GET and FILTER in Django model layer | 1,541,249 | <p>What is the difference, please explain them in laymen's terms with examples. Thanks!</p>
| 18 | 2009-10-09T00:21:12Z | 1,541,322 | <p>I don't know if you really need an example, it's quite easy:</p>
<ul>
<li>if you know it's one object that matches your query, use get. It will fail if it's more than one.</li>
<li>otherwise use filter, which gives you a list of objects.</li>
</ul>
<p>To be more precise:</p>
<ul>
<li><code>MyTable.objects.get(id=... | 32 | 2009-10-09T00:50:56Z | [
"python",
"django"
] |
Why is subprocess.Popen not waiting until the child process terminates? | 1,541,273 | <p>I'm having a problem with Python's subprocess.Popen method. </p>
<p>Here's a test script which demonstrates the problem. It's being run on a Linux box.</p>
<pre><code>#!/usr/bin/env python
import subprocess
import time
def run(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
return p
### S... | 5 | 2009-10-09T00:30:31Z | 1,541,355 | <p><code>subprocess.Popen</code>, when instantiated, runs the program. It does not, however, wait for it -- it fires it off in the background as if you'd typed <code>cmd &</code> in a shell. So, in the code above, you've essentially defined a race condition -- if the inserts can finish in time, it will appear nor... | 19 | 2009-10-09T01:05:31Z | [
"python",
"mysql"
] |
Why is subprocess.Popen not waiting until the child process terminates? | 1,541,273 | <p>I'm having a problem with Python's subprocess.Popen method. </p>
<p>Here's a test script which demonstrates the problem. It's being run on a Linux box.</p>
<pre><code>#!/usr/bin/env python
import subprocess
import time
def run(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
return p
### S... | 5 | 2009-10-09T00:30:31Z | 1,541,701 | <p>Dude, why did you think <code>subprocess.Popen</code> returned an object with a <a href="http://docs.python.org/library/subprocess.html?highlight=subprocess.popen#subprocess.Popen.wait"><code>wait</code></a> method, unless it was because the waiting was <strong>NOT</strong> implicit, intrinsic, immediate, and inevit... | 6 | 2009-10-09T03:51:15Z | [
"python",
"mysql"
] |
Why is subprocess.Popen not waiting until the child process terminates? | 1,541,273 | <p>I'm having a problem with Python's subprocess.Popen method. </p>
<p>Here's a test script which demonstrates the problem. It's being run on a Linux box.</p>
<pre><code>#!/usr/bin/env python
import subprocess
import time
def run(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
return p
### S... | 5 | 2009-10-09T00:30:31Z | 1,541,709 | <p>If you don't absolutely need to use subprocess and popen, it's usually simpler to use <code>os.system</code>. For example, for quick scripts I often do something like this:</p>
<pre><code>import os
run = os.system #convenience alias
result = run('mysql -u ve --execute="select * from wherever" test')
</code></pre>
... | 7 | 2009-10-09T03:54:09Z | [
"python",
"mysql"
] |
python regular expression to parse div tags | 1,541,281 | <p>a question about python regular expression.</p>
<p>I would like to match a div block like </p>
<pre><code><div class="leftTail"><ul class="hotnews">any news stuff</ul></div>
</code></pre>
<p>I was thinking a pattern like</p>
<pre><code>p = re.compile(r'<div\s+class=\"leftTail\">[^(d... | 1 | 2009-10-09T00:32:25Z | 1,541,283 | <p>Don't use regular expressions to parse XML or HTML. You'll never be able to get it to work correctly for nested divs.</p>
| 4 | 2009-10-09T00:35:04Z | [
"python",
"regex"
] |
python regular expression to parse div tags | 1,541,281 | <p>a question about python regular expression.</p>
<p>I would like to match a div block like </p>
<pre><code><div class="leftTail"><ul class="hotnews">any news stuff</ul></div>
</code></pre>
<p>I was thinking a pattern like</p>
<pre><code>p = re.compile(r'<div\s+class=\"leftTail\">[^(d... | 1 | 2009-10-09T00:32:25Z | 1,541,288 | <p>You might want to consider graduating to an actual HTML parser. I suggest you give <a href="http://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> a try. There are many crazy ways for HTML to be formatted, and the regular expressions may not work correctly all the time, even if you write them correctly.... | 12 | 2009-10-09T00:36:04Z | [
"python",
"regex"
] |
python regular expression to parse div tags | 1,541,281 | <p>a question about python regular expression.</p>
<p>I would like to match a div block like </p>
<pre><code><div class="leftTail"><ul class="hotnews">any news stuff</ul></div>
</code></pre>
<p>I was thinking a pattern like</p>
<pre><code>p = re.compile(r'<div\s+class=\"leftTail\">[^(d... | 1 | 2009-10-09T00:32:25Z | 1,541,302 | <p>try this:</p>
<pre><code>p = re.compile(r'<div\s+class=\"leftTail\">.*?</div>')
</code></pre>
| 4 | 2009-10-09T00:41:39Z | [
"python",
"regex"
] |
Django SELECT statement, Order by | 1,541,376 | <p>Suppose I have 2 models. </p>
<p>The 2nd model has a one-to-one relationship with the first model. </p>
<p>I'd like to select information from the first model, but ORDER BY the 2nd model. How can I do that?</p>
<pre><code>class Content(models.Model):
link = models.TextField(blank=True)
title = models.T... | 4 | 2009-10-09T01:15:36Z | 1,541,390 | <p>I think you can do:</p>
<pre><code>Content.objects.filter(...).order_by('score__counter')
</code></pre>
<p>More generally, when models have a relationship, you can select, order, and filter by fields on the "other" model using the <code>relationshipName__fieldName</code> pseudo attribute of the model which you are... | 7 | 2009-10-09T01:23:17Z | [
"python",
"django"
] |
Why Does List Argument in Python Behave Like ByRef? | 1,541,620 | <p>This may be for most languages in general, but I'm not sure. I'm a beginner at Python and have always worked on copies of lists in C# and VB. But in Python whenever I pass a list as an argument and enumerate through using a "for i in range," and then change the value of the list argument, the input values actually... | 1 | 2009-10-09T03:14:43Z | 1,541,625 | <p>Python does pass arguments by value but the value you are receiving is a copy of the reference (incidentally this is the exact same way that C#, VB.NET, and Java behave as well).</p>
<p>This is the important thing to remember:</p>
<blockquote>
<p><em>Objects are not passed by reference - object references are pa... | 8 | 2009-10-09T03:17:09Z | [
"python",
"reference",
"argument-passing",
"object-reference"
] |
Why Does List Argument in Python Behave Like ByRef? | 1,541,620 | <p>This may be for most languages in general, but I'm not sure. I'm a beginner at Python and have always worked on copies of lists in C# and VB. But in Python whenever I pass a list as an argument and enumerate through using a "for i in range," and then change the value of the list argument, the input values actually... | 1 | 2009-10-09T03:14:43Z | 1,541,645 | <p>To add to Andrew's answer, you need to explicitly make a copy of a list if you want to retain the original. You can do this using the <code>copy</code> module, or just do something like </p>
<pre><code>a = [1,2]
b = list(a)
</code></pre>
<p>Since copying objects usually implies a performance hit, I find it helpful... | 0 | 2009-10-09T03:26:44Z | [
"python",
"reference",
"argument-passing",
"object-reference"
] |
Why Does List Argument in Python Behave Like ByRef? | 1,541,620 | <p>This may be for most languages in general, but I'm not sure. I'm a beginner at Python and have always worked on copies of lists in C# and VB. But in Python whenever I pass a list as an argument and enumerate through using a "for i in range," and then change the value of the list argument, the input values actually... | 1 | 2009-10-09T03:14:43Z | 1,541,681 | <p>Python -- just like Java does for anything but primitive scalars, and like C# and VB.NET do for the default kind parameters as opposed to boxed types and out / ref parms -- passes "by object reference" (search for that phrase <a href="http://en.wikipedia.org/wiki/Python%5Fsyntax%5Fand%5Fsemantics" rel="nofollow">her... | 4 | 2009-10-09T03:44:07Z | [
"python",
"reference",
"argument-passing",
"object-reference"
] |
A .net wrapper for Google App Engine? | 1,541,722 | <p>Does anyone know of a .net wrapper around either python or java Google App Engine services?</p>
<p>Any help appreciated // :)</p>
| 3 | 2009-10-09T03:59:11Z | 1,541,765 | <p>I don't believe this is even <em>conceivable</em> (if I understand what you're asking about): App Engine will run code on a Python virtual machine or a Java one, never on a .NET one, so where would this "wrapper" <em>run</em>? If your need is for .NET "in the cloud", consider <a href="http://www.microsoft.com/azure... | 3 | 2009-10-09T04:16:41Z | [
"java",
".net",
"python",
"web-services",
"google-app-engine"
] |
A .net wrapper for Google App Engine? | 1,541,722 | <p>Does anyone know of a .net wrapper around either python or java Google App Engine services?</p>
<p>Any help appreciated // :)</p>
| 3 | 2009-10-09T03:59:11Z | 1,542,102 | <p>I don't think it's impossible to run .net on GAE. At least, it's not impossible to run something that was generated from .net sources or interprets them at runtime. Just think about Ruby or Python, both work on the JVM (using JRuby and Jython). You'll probably have to look for something similar or try to cross-compi... | 2 | 2009-10-09T06:26:20Z | [
"java",
".net",
"python",
"web-services",
"google-app-engine"
] |
A .net wrapper for Google App Engine? | 1,541,722 | <p>Does anyone know of a .net wrapper around either python or java Google App Engine services?</p>
<p>Any help appreciated // :)</p>
| 3 | 2009-10-09T03:59:11Z | 17,356,271 | <p>Python has a .NET alternative called IronPython. But I don't think it will work in GAE as it supports only pure python. I am also facing a similar problem trying to port a source written in C#.net into GAE compatible code. I'm going to work around it by re-inventing the spokes of the wheel (not the wheel itself) by ... | 0 | 2013-06-28T02:24:35Z | [
"java",
".net",
"python",
"web-services",
"google-app-engine"
] |
Upgrade Python to 2.6 on Mac | 1,541,776 | <p>I'd like to upgrade the default python installation (2.5.1) supplied with OS X Leopard to the latest version. Please let me know how I can achieve this.</p>
<p>Thanks</p>
| 14 | 2009-10-09T04:24:03Z | 1,541,785 | <p>May I suggest you leave the "Default" be, and install Python in /usr/local/bin.</p>
<ol>
<li>Download python</li>
<li>Unzip it</li>
<li>./configure</li>
<li>make</li>
<li>sudo make install</li>
</ol>
<p>done.</p>
<p>Since /usr/local/bin comes before /usr/bin in the $PATH, you will invoke 2.6 when you type python,... | 6 | 2009-10-09T04:28:09Z | [
"python",
"osx",
"installation",
"upgrade"
] |
Upgrade Python to 2.6 on Mac | 1,541,776 | <p>I'd like to upgrade the default python installation (2.5.1) supplied with OS X Leopard to the latest version. Please let me know how I can achieve this.</p>
<p>Thanks</p>
| 14 | 2009-10-09T04:24:03Z | 1,541,795 | <p>I believe there are a few options. The one I like is to install alternative UNIX software on my Macs using MacPorts, <a href="http://www.macports.org/" rel="nofollow">http://www.macports.org/</a> -- this way the new software is installed in a different directory and won't mess up anything depending on the apple-inst... | 0 | 2009-10-09T04:30:00Z | [
"python",
"osx",
"installation",
"upgrade"
] |
Upgrade Python to 2.6 on Mac | 1,541,776 | <p>I'd like to upgrade the default python installation (2.5.1) supplied with OS X Leopard to the latest version. Please let me know how I can achieve this.</p>
<p>Thanks</p>
| 14 | 2009-10-09T04:24:03Z | 1,541,806 | <p>You have a number of options</p>
<ul>
<li><p>Install with <a href="http://www.macports.org/">MacPorts</a> or <a href="http://www.finkproject.org/">Fink</a>, e.g.:</p>
<pre><code>sudo port install python2.6
</code></pre></li>
<li>Install from the disc image from <a href="http://python.org/ftp/python/2.6.3/python-2.... | 5 | 2009-10-09T04:32:15Z | [
"python",
"osx",
"installation",
"upgrade"
] |
Upgrade Python to 2.6 on Mac | 1,541,776 | <p>I'd like to upgrade the default python installation (2.5.1) supplied with OS X Leopard to the latest version. Please let me know how I can achieve this.</p>
<p>Thanks</p>
| 14 | 2009-10-09T04:24:03Z | 1,541,850 | <p>When an OS is distributed with some specific Python release and uses it for some OS functionality (as is the case with Mac OS X, as well as many Linux distros &c), you should <strong>not</strong> tamper in any way with the system-supplied Python (as in, "upgrading" it and the like): while Python strives for back... | 20 | 2009-10-09T04:48:25Z | [
"python",
"osx",
"installation",
"upgrade"
] |
Upgrade Python to 2.6 on Mac | 1,541,776 | <p>I'd like to upgrade the default python installation (2.5.1) supplied with OS X Leopard to the latest version. Please let me know how I can achieve this.</p>
<p>Thanks</p>
| 14 | 2009-10-09T04:24:03Z | 1,542,144 | <p>Don't upgrade.</p>
<ol>
<li>Install <a href="http://www.activestate.com/activepython/downloads" rel="nofollow">ActivePython</a> (which <a href="http://docs.activestate.com/activepython/2.6/faq.html#does-activepython-collide-with-other-python-distributions-on-mac-os-x" rel="nofollow">co-exists</a> with others). </li... | 8 | 2009-10-09T06:35:57Z | [
"python",
"osx",
"installation",
"upgrade"
] |
Upgrade Python to 2.6 on Mac | 1,541,776 | <p>I'd like to upgrade the default python installation (2.5.1) supplied with OS X Leopard to the latest version. Please let me know how I can achieve this.</p>
<p>Thanks</p>
| 14 | 2009-10-09T04:24:03Z | 1,701,884 | <p>The best is to use <a href="http://www.macports.org/" rel="nofollow">macports</a> (just like Adam Rosenfield stated on this thread).</p>
<p>You can easily switch between Python versions using macports select mechanism:</p>
<p><code>$ sudo port select --set python pythonXY</code></p>
<p>To view the list of availab... | 4 | 2009-11-09T15:47:00Z | [
"python",
"osx",
"installation",
"upgrade"
] |
Upgrade Python to 2.6 on Mac | 1,541,776 | <p>I'd like to upgrade the default python installation (2.5.1) supplied with OS X Leopard to the latest version. Please let me know how I can achieve this.</p>
<p>Thanks</p>
| 14 | 2009-10-09T04:24:03Z | 1,996,525 | <p>The standard <a href="http://python.org" rel="nofollow">http://python.org</a> install for Mac OSX will happily <em>coexist</em> with the "system python". If you let the installer change your paths, when you run python from a prompt in terminal, it will find the version at </p>
<pre><code> /Library/Frameworks/Python... | 1 | 2010-01-03T22:09:58Z | [
"python",
"osx",
"installation",
"upgrade"
] |
Get window title with python? | 1,541,784 | <p>I'm trying to write a python program that checks every X seconds if the 'window title' for 'last.fm' (<a href="http://www.last.fm/download" rel="nofollow">http://www.last.fm/download</a>) changed, if it did (or it's the first time I run the program) it should use use the string captured from the window title to sear... | 4 | 2009-10-09T04:27:56Z | 1,542,447 | <p>I think by using a automation framework you may be able to achieve this as a subset.
e.g. try dogtail(<a href="https://fedorahosted.org/dogtail/" rel="nofollow">https://fedorahosted.org/dogtail/</a>), it can focus on windows by name, click on buttons by name, so in the src code you may be able to see how to get titl... | 1 | 2009-10-09T07:58:25Z | [
"python",
"kde"
] |
Get window title with python? | 1,541,784 | <p>I'm trying to write a python program that checks every X seconds if the 'window title' for 'last.fm' (<a href="http://www.last.fm/download" rel="nofollow">http://www.last.fm/download</a>) changed, if it did (or it's the first time I run the program) it should use use the string captured from the window title to sear... | 4 | 2009-10-09T04:27:56Z | 1,543,090 | <p>Try using dcop and piloting kwin. You can probably list all the window titles.</p>
<p>See the following for an example on how to use dcop:
<a href="http://docs.kde.org/stable/en/kdegraphics/ksnapshot/dcop.html" rel="nofollow">http://docs.kde.org/stable/en/kdegraphics/ksnapshot/dcop.html</a></p>
| 0 | 2009-10-09T10:48:30Z | [
"python",
"kde"
] |
Get window title with python? | 1,541,784 | <p>I'm trying to write a python program that checks every X seconds if the 'window title' for 'last.fm' (<a href="http://www.last.fm/download" rel="nofollow">http://www.last.fm/download</a>) changed, if it did (or it's the first time I run the program) it should use use the string captured from the window title to sear... | 4 | 2009-10-09T04:27:56Z | 1,543,307 | <p>Have a look at X11 utilities, specifically <code>xlsclients</code> and <code>xprop</code>.</p>
<p>As an example, this is the shell commands I used to get info about my firefox
window:</p>
<pre><code>id_=$(xlsclients -al|grep "Command: firefox-bin" -A1 -B4|head -n1|cut -d ' ' -f 2|tr -d ':')
xprop -id "$_id"
</co... | 0 | 2009-10-09T11:46:30Z | [
"python",
"kde"
] |
Get window title with python? | 1,541,784 | <p>I'm trying to write a python program that checks every X seconds if the 'window title' for 'last.fm' (<a href="http://www.last.fm/download" rel="nofollow">http://www.last.fm/download</a>) changed, if it did (or it's the first time I run the program) it should use use the string captured from the window title to sear... | 4 | 2009-10-09T04:27:56Z | 1,543,979 | <p>You can use the <code>wmctrl</code> utility through the <code>subprocess</code> module. You can type <code>wmctrl -l</code> into a terminal and see the output you can get from it.</p>
| 3 | 2009-10-09T14:03:13Z | [
"python",
"kde"
] |
Get window title with python? | 1,541,784 | <p>I'm trying to write a python program that checks every X seconds if the 'window title' for 'last.fm' (<a href="http://www.last.fm/download" rel="nofollow">http://www.last.fm/download</a>) changed, if it did (or it's the first time I run the program) it should use use the string captured from the window title to sear... | 4 | 2009-10-09T04:27:56Z | 1,893,129 | <p>another answer maybe to check if the application publishes the song change to DBus. if it does then you can just listen for the event and act on that. </p>
| 0 | 2009-12-12T11:31:51Z | [
"python",
"kde"
] |
Check for duplicates in a flat list | 1,541,797 | <p>example 1:</p>
<p><code>['one', 'two', 'one']</code> should return True.</p>
<p>example 2:</p>
<p><code>['one', 'two', 'three']</code> should return False.</p>
| 67 | 2009-10-09T04:30:32Z | 1,541,820 | <p>Recommended for <em>short</em> lists only:</p>
<pre><code>any(thelist.count(x) > 1 for x in thelist)
</code></pre>
<p>Do <strong>not</strong> use on a long list -- it can take time proportional to the <strong>square</strong> of the number of items in the list!</p>
<p>For longer lists with hashable items (strin... | 30 | 2009-10-09T04:36:37Z | [
"python",
"string",
"list",
"duplicates"
] |
Check for duplicates in a flat list | 1,541,797 | <p>example 1:</p>
<p><code>['one', 'two', 'one']</code> should return True.</p>
<p>example 2:</p>
<p><code>['one', 'two', 'three']</code> should return False.</p>
| 67 | 2009-10-09T04:30:32Z | 1,541,827 | <p>Use <code>set()</code> to remove duplicates if all values are <em>hashable</em>:</p>
<pre><code>>>> your_list = ['one', 'two', 'one']
>>> len(your_list) != len(set(your_list))
True
</code></pre>
| 157 | 2009-10-09T04:38:45Z | [
"python",
"string",
"list",
"duplicates"
] |
Check for duplicates in a flat list | 1,541,797 | <p>example 1:</p>
<p><code>['one', 'two', 'one']</code> should return True.</p>
<p>example 2:</p>
<p><code>['one', 'two', 'three']</code> should return False.</p>
| 67 | 2009-10-09T04:30:32Z | 6,250,875 | <p>If you are fond of functional programming style, here is a useful function, self-documented and tested code using <a href="http://docs.python.org/library/doctest.html" rel="nofollow">doctest</a>.</p>
<pre><code>def decompose(a_list):
"""Turns a list into a set of all elements and a set of duplicated elements.
... | 5 | 2011-06-06T10:44:28Z | [
"python",
"string",
"list",
"duplicates"
] |
Check for duplicates in a flat list | 1,541,797 | <p>example 1:</p>
<p><code>['one', 'two', 'one']</code> should return True.</p>
<p>example 2:</p>
<p><code>['one', 'two', 'three']</code> should return False.</p>
| 67 | 2009-10-09T04:30:32Z | 18,515,820 | <p>This is old, but the answers here led me to a slightly different solution. If you are up for abusing comprehensions, you can get short-circuiting this way.</p>
<pre><code>xs = [1, 2, 1]
s = set()
any(x in s or s.add(x) for x in xs)
# You can use a similar approach to actually retrieve the duplicates.
s = set()
dup... | 3 | 2013-08-29T16:03:06Z | [
"python",
"string",
"list",
"duplicates"
] |
Check for duplicates in a flat list | 1,541,797 | <p>example 1:</p>
<p><code>['one', 'two', 'one']</code> should return True.</p>
<p>example 2:</p>
<p><code>['one', 'two', 'three']</code> should return False.</p>
| 67 | 2009-10-09T04:30:32Z | 31,569,431 | <p>I recently answered a related question to <a href="http://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-python-list/31439372#31439372">establish all the duplicates</a> in a list, using a generator. It has the advantage that if used just to establish 'if there is a duplicate' then you just need to g... | 1 | 2015-07-22T16:54:59Z | [
"python",
"string",
"list",
"duplicates"
] |
python/django for loop creating database populated with 0000-9999 | 1,541,891 | <p>Basically, i've created a view to populate my database with Serial models from 0000 to 9999. below is the code i'm using for the view.</p>
<pre><code>def insert_serials(request):
for i in range(0,10000):
serial = Serial(i,False)
serial.save()
else:
print 'The for loop is over'
</code></pre>
<p>... | 0 | 2009-10-09T05:03:47Z | 1,541,911 | <p>Try adding <code>unique=False</code> in the <code>closed</code> field declaration.</p>
<p>Also, you're trying to put integers into a string field. You should do it like <code>Serial('%04d' % i, False)</code> to put values from <code>'0000'</code> to <code>'9999'</code>.</p>
| 0 | 2009-10-09T05:11:49Z | [
"python",
"django"
] |
python/django for loop creating database populated with 0000-9999 | 1,541,891 | <p>Basically, i've created a view to populate my database with Serial models from 0000 to 9999. below is the code i'm using for the view.</p>
<pre><code>def insert_serials(request):
for i in range(0,10000):
serial = Serial(i,False)
serial.save()
else:
print 'The for loop is over'
</code></pre>
<p>... | 0 | 2009-10-09T05:03:47Z | 1,541,936 | <p>There may be positional default arguments, try using keywords:</p>
<pre><code>from django.db import transaction
@transaction.commit_manually
def insert_serials(request):
for i in range(0,10000):
serial = Serial(serial=str(i),closed=False)
serial.save()
transaction.commit()
print 'The fo... | 1 | 2009-10-09T05:19:12Z | [
"python",
"django"
] |
python/django for loop creating database populated with 0000-9999 | 1,541,891 | <p>Basically, i've created a view to populate my database with Serial models from 0000 to 9999. below is the code i'm using for the view.</p>
<pre><code>def insert_serials(request):
for i in range(0,10000):
serial = Serial(i,False)
serial.save()
else:
print 'The for loop is over'
</code></pre>
<p>... | 0 | 2009-10-09T05:03:47Z | 1,542,225 | <p>Your code is working on my site - Mac OS X, Python 2.6.3, django from trunk, sqlite3</p>
<p>I changed your view function code a bit, though - </p>
<pre><code>from django.http import HttpResponse
from models import Serial
def insert_serials(request):
for i in range(0,10000):
serial = Serial(i,False)
... | 1 | 2009-10-09T07:02:46Z | [
"python",
"django"
] |
python/django for loop creating database populated with 0000-9999 | 1,541,891 | <p>Basically, i've created a view to populate my database with Serial models from 0000 to 9999. below is the code i'm using for the view.</p>
<pre><code>def insert_serials(request):
for i in range(0,10000):
serial = Serial(i,False)
serial.save()
else:
print 'The for loop is over'
</code></pre>
<p>... | 0 | 2009-10-09T05:03:47Z | 1,557,851 | <p>Your id field (implied by the absence of a PK definition in your model) is not being autonumbered correctly and therefore every INSERT after the first is failing with a duplicate id value. What's your database? Did you have Django create the table, or did you do it yourself?</p>
| 0 | 2009-10-13T01:43:44Z | [
"python",
"django"
] |
binding local variables in python | 1,542,551 | <p>I wonder if there is a good way to bind local variables in python. Most of my work involves cobbling together short data or text processing scripts with a series of expressions (when python permits), so defining object classes (to use as namespaces) and instantiating them seems a bit much. </p>
<p>So what I had in ... | 4 | 2009-10-09T08:26:23Z | 1,542,570 | <p>Not really knowing Lisp, I can't see what you're trying to do here. But I would say that in general you should not try to write Python as if it were Lisp, or indeed any language as if it were any other language. I've been programming in Python for five years and I've never seen a need to do what you're trying above.... | 1 | 2009-10-09T08:30:48Z | [
"python",
"lisp"
] |
binding local variables in python | 1,542,551 | <p>I wonder if there is a good way to bind local variables in python. Most of my work involves cobbling together short data or text processing scripts with a series of expressions (when python permits), so defining object classes (to use as namespaces) and instantiating them seems a bit much. </p>
<p>So what I had in ... | 4 | 2009-10-09T08:26:23Z | 1,542,574 | <p>It's a bit unclear what you are asking, bit I'll try to answer anyway:</p>
<p>You bind variables to names with = in Python. So your <code>data = [1,2,3]</code> binds the list <code>[1,2,3]</code> to the name <code>data</code>.</p>
<p>You can create local namespaces with classes and functions/methods.</p>
<p>The c... | 2 | 2009-10-09T08:33:08Z | [
"python",
"lisp"
] |
binding local variables in python | 1,542,551 | <p>I wonder if there is a good way to bind local variables in python. Most of my work involves cobbling together short data or text processing scripts with a series of expressions (when python permits), so defining object classes (to use as namespaces) and instantiating them seems a bit much. </p>
<p>So what I had in ... | 4 | 2009-10-09T08:26:23Z | 1,542,769 | <p>I can only second Lennart and Daniel - Python is not Lisp, and trying to write language X into language Y is usually inefficient and frustrating at best.</p>
<p>First point: your example code</p>
<pre><code>data = [1,2,3]
output = ((lambda x: x + x)
(data[2]))
</code></pre>
<p>would be much more readabl... | 3 | 2009-10-09T09:22:50Z | [
"python",
"lisp"
] |
binding local variables in python | 1,542,551 | <p>I wonder if there is a good way to bind local variables in python. Most of my work involves cobbling together short data or text processing scripts with a series of expressions (when python permits), so defining object classes (to use as namespaces) and instantiating them seems a bit much. </p>
<p>So what I had in ... | 4 | 2009-10-09T08:26:23Z | 1,543,348 | <p>You could combine a function decorator and default parameters to get something like let and block scoped variables:</p>
<pre><code>def let(func):
return func()
data = [1,2,3]
@let
def output(x=data[2]):
return x + x
print(output) # 6
# or if a single expression is enough:
output = let(lambda x=data[2]: x... | 2 | 2009-10-09T11:54:30Z | [
"python",
"lisp"
] |
How to write this "model" in Django? | 1,542,659 | <p>I am currently using Django Users model.
Very simple. However, I'd like to add one feature: Adding friends!</p>
<p>I would like to create 2 columns in my table:</p>
<p>UID (the ID of the User)
friend_id (the ID of his friend! ...of course, this ID is also in Django's User model.
The UID-friend_id combination must... | 2 | 2009-10-09T08:54:04Z | 1,542,697 | <p>You should create a model that defines the relationship between two users, and then define two foreign-key fields, each one to a User. You can then add a unique constraint to make sure you don't have duplicates.</p>
<p>There is a article here explaining exactly how to do this: <a href="http://www.packtpub.com/artic... | 11 | 2009-10-09T09:04:01Z | [
"python",
"django"
] |
Accuracy of stat mtime in Windows | 1,542,711 | <p>Have an example piece of (Python) code to check if a directory has changed:</p>
<pre><code>import os
def watch(path, fdict):
"""Checks a directory and children for changes"""
changed = []
for root, dirs, files in os.walk(path):
for f in files:
abspath = os.path.abspath(os.path.join(ro... | 0 | 2009-10-09T09:09:46Z | 1,542,849 | <p>The <em>proper</em> way is to watch a directory instead of polling for changes. </p>
<p>Check out <a href="http://msdn.microsoft.com/en-us/library/aa364417%28VS.85%29.aspx" rel="nofollow">FindFirstChangeNotification Function</a>.<br />
<a href="http://timgolden.me.uk/python/win32%5Fhow%5Fdo%5Fi/watch%5Fdirectory%... | 1 | 2009-10-09T09:42:33Z | [
"python",
"windows",
"filemtime"
] |
Accuracy of stat mtime in Windows | 1,542,711 | <p>Have an example piece of (Python) code to check if a directory has changed:</p>
<pre><code>import os
def watch(path, fdict):
"""Checks a directory and children for changes"""
changed = []
for root, dirs, files in os.walk(path):
for f in files:
abspath = os.path.abspath(os.path.join(ro... | 0 | 2009-10-09T09:09:46Z | 1,542,856 | <p>if this were linux, i'd use inotify. there's apparently a windows inotify equivalent - the java <a href="http://jnotify.sourceforge.net/" rel="nofollow">jnotify library</a> has implemented it - but i don't know if there's a python implementation</p>
| 0 | 2009-10-09T09:44:45Z | [
"python",
"windows",
"filemtime"
] |
Accuracy of stat mtime in Windows | 1,542,711 | <p>Have an example piece of (Python) code to check if a directory has changed:</p>
<pre><code>import os
def watch(path, fdict):
"""Checks a directory and children for changes"""
changed = []
for root, dirs, files in os.walk(path):
for f in files:
abspath = os.path.abspath(os.path.join(ro... | 0 | 2009-10-09T09:09:46Z | 8,386,780 | <p>Watchdog:
<a href="http://packages.python.org/watchdog/quickstart.html" rel="nofollow">http://packages.python.org/watchdog/quickstart.html</a></p>
<p>Is a good project to have some multi-platform changes notification.</p>
| 1 | 2011-12-05T14:27:37Z | [
"python",
"windows",
"filemtime"
] |
Does python's print function handle unicode differently now than when Dive Into Python was written? | 1,542,785 | <p>I'm trying to work my way through some frustrating encoding issues by going back to basics. In Dive Into Python example 9.14 (<a href="http://diveintopython.net/xml_processing/unicode.html" rel="nofollow">here</a>) we have this:</p>
<pre><code>>>> s = u'La Pe\xf1a'
>>> print s
Traceback (innermos... | 4 | 2009-10-09T09:28:47Z | 1,542,823 | <p>The default encoding for <code>print</code> doesn't depend on <code>sys.getdefaultencoding()</code>, but on <code>sys.stdout.encoding</code>. If you launch python with e.g. <code>LANG=C</code> or redirect a python script to a file, the encoding for stdout will be <code>ANSI_X3.4-1968</code>. On the other hand, if <c... | 6 | 2009-10-09T09:36:11Z | [
"python",
"unicode",
"character-encoding"
] |
What to do when Django query returns none? It gives me error | 1,542,878 | <pre><code>to_friend = User.objects.filter(username=friend_q)[0:1]
</code></pre>
<p>If 'friend_q' is NOT inside the User.username...it will give error.
What is the recommended tactic?</p>
<p>Thank you</p>
| 4 | 2009-10-09T09:50:41Z | 1,542,897 | <p>The reason you're getting an error is because you're trying to evaluate the Query object when there's nothing in it. The [0:1] at the end is attempting to take the first item out of an empty list. If you split up the expression a little like below, you can check for an empty list before taking an element out of it.<... | 0 | 2009-10-09T09:55:42Z | [
"python",
"django"
] |
What to do when Django query returns none? It gives me error | 1,542,878 | <pre><code>to_friend = User.objects.filter(username=friend_q)[0:1]
</code></pre>
<p>If 'friend_q' is NOT inside the User.username...it will give error.
What is the recommended tactic?</p>
<p>Thank you</p>
| 4 | 2009-10-09T09:50:41Z | 1,542,919 | <ol>
<li><p>FWIW, Username is unique. So U can use </p>
<pre><code>to_friend = User.objects.get(username=friend_q)
</code></pre></li>
<li><p>If you want to raise a 404 when the user doesn't exist, you may as well,</p>
<pre><code>from django.shortcuts import get_object_or_404
to_friend = get_object_or_404(User,usernam... | 5 | 2009-10-09T10:01:41Z | [
"python",
"django"
] |
What to do when Django query returns none? It gives me error | 1,542,878 | <pre><code>to_friend = User.objects.filter(username=friend_q)[0:1]
</code></pre>
<p>If 'friend_q' is NOT inside the User.username...it will give error.
What is the recommended tactic?</p>
<p>Thank you</p>
| 4 | 2009-10-09T09:50:41Z | 1,542,934 | <p>If friend_q is not a user present in the database, to_friend will be equal to an empty list.</p>
<pre><code>>>> from django.contrib.auth.models import User
>>> User.objects.filter(username='does-not-exist')
[]
</code></pre>
<p>However, it's better to use the get() method to lookup a specific entr... | 8 | 2009-10-09T10:06:39Z | [
"python",
"django"
] |
Help in the following code | 1,542,991 | <pre><code>def startConnection(self):
from ftplib import FTP
self.ftp = FTP(self.loginServer)
print 'Loging In'
print self.ftp.login(self.username, self.password)
data = []
self.ftp.dir(data.append)
for line in data:
try:
self.date_str = ' '.join(line.split()[5:8])
newDate = time.strptime(self.date_str,'... | 0 | 2009-10-09T10:23:52Z | 1,543,006 | <p>Try</p>
<pre><code>self.ftp.dir('-lTr', data.append)
</code></pre>
| 1 | 2009-10-09T10:28:54Z | [
"python",
"ftplib"
] |
Help in the following code | 1,542,991 | <pre><code>def startConnection(self):
from ftplib import FTP
self.ftp = FTP(self.loginServer)
print 'Loging In'
print self.ftp.login(self.username, self.password)
data = []
self.ftp.dir(data.append)
for line in data:
try:
self.date_str = ' '.join(line.split()[5:8])
newDate = time.strptime(self.date_str,'... | 0 | 2009-10-09T10:23:52Z | 1,543,026 | <p>If you are using a command that gives a short-form date, or want to cope with different arguments being handed to <code>ls</code> you'll have to make multiple attempts to parse the date with different format strings, until you don't get a <code>ValueError</code>, post-processing the successful parse according to whi... | 0 | 2009-10-09T10:33:59Z | [
"python",
"ftplib"
] |
Pure Python persistent key and value based container (a hash like interface) with large file system support? | 1,543,087 | <p>I am looking for a (possibly) pure Python library for persistent hash table (btree or b+tree which would provide following features</p>
<ol>
<li>Large file support (possibly in terabytes)</li>
<li>Fast enough and low memory footprint (looking for a descent balance between speed and memory)</li>
<li>Low cost of mana... | 3 | 2009-10-09T10:47:56Z | 1,543,184 | <p>ZODB<br />
<a href="http://pypi.python.org/pypi/ZODB3" rel="nofollow">http://pypi.python.org/pypi/ZODB3</a></p>
<p>Like Lennart says, use the latest version of course</p>
| 2 | 2009-10-09T11:13:37Z | [
"python",
"hash",
"persistence"
] |
Pure Python persistent key and value based container (a hash like interface) with large file system support? | 1,543,087 | <p>I am looking for a (possibly) pure Python library for persistent hash table (btree or b+tree which would provide following features</p>
<ol>
<li>Large file support (possibly in terabytes)</li>
<li>Fast enough and low memory footprint (looking for a descent balance between speed and memory)</li>
<li>Low cost of mana... | 3 | 2009-10-09T10:47:56Z | 1,543,245 | <p>ZODB is indeed a powerful tool, but maybe it's overkill.</p>
<p>You can hack your own solution in few Python lines : simply code a dictionary like object as a data base adapter. Try using <a href="http://sebsauvage.net/python/snyppets/index.html#dbdict" rel="nofollow">this snippets</a>, replacing the SQLite call to... | 1 | 2009-10-09T11:27:21Z | [
"python",
"hash",
"persistence"
] |
Pure Python persistent key and value based container (a hash like interface) with large file system support? | 1,543,087 | <p>I am looking for a (possibly) pure Python library for persistent hash table (btree or b+tree which would provide following features</p>
<ol>
<li>Large file support (possibly in terabytes)</li>
<li>Fast enough and low memory footprint (looking for a descent balance between speed and memory)</li>
<li>Low cost of mana... | 3 | 2009-10-09T10:47:56Z | 1,543,403 | <p>Use a relational database. </p>
<ul>
<li>Really fast when retrieving data based on a key, if you put an index in the key. </li>
<li>Good scaling</li>
<li>Don't get easily corrupted</li>
<li>Tools already available for:
<ul>
<li>Backups</li>
<li>Replication</li>
<li>Clustering</li>
</ul></li>
<li>Cross-platform</li>... | 2 | 2009-10-09T12:09:47Z | [
"python",
"hash",
"persistence"
] |
Google Federated Login (OpenID+Oauth) for Hosted Apps - changing end points? | 1,543,123 | <p>I'm trying to integrate the Google Federated Login with a premier apps account, but I'm having some problems.</p>
<p>When I send the request to: <code>https://www.google.com/accounts/o8/ud</code> with all the parameters (see below), I get back both a <code>request_token</code> and list of attributes asked for by <c... | 8 | 2009-10-09T10:58:14Z | 1,650,977 | <p>For the record, posterity, and anyone else who might come asunder of this, I'll document the (ridiculous) answer.</p>
<p>Ultimately, the problem was calling:</p>
<pre><code>return HttpResponseRedirect(
'https://www.google.com/a/thedomain.com/o8/ud?be=o8'
+ '?'
+ urllib.urlencode(parameters)
)
</code></... | 7 | 2009-10-30T16:44:30Z | [
"python",
"openid",
"oauth",
"hybridauthprovider"
] |
simple dropping elements from the list in python | 1,543,456 | <p>I'd like to achieve following effect</p>
<pre><code>a=[11, -1, -1, -1]
msg=['one','two','tree','four']
msg[where a<0]
['two','tree','four']
</code></pre>
<p>In similar simple fashion (without nasty loops).</p>
<p>PS. For curious people this if statement is working natively in one of functional languages. </p>
... | 1 | 2009-10-09T12:21:07Z | 1,543,463 | <p>You can use <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehensions</a> for this. You need to match the items from the two lists, for which the <a href="http://docs.python.org/3.1/library/functions.html#zip" rel="nofollow"><code>zip</code></a> function is ... | 10 | 2009-10-09T12:23:29Z | [
"python",
"filter"
] |
simple dropping elements from the list in python | 1,543,456 | <p>I'd like to achieve following effect</p>
<pre><code>a=[11, -1, -1, -1]
msg=['one','two','tree','four']
msg[where a<0]
['two','tree','four']
</code></pre>
<p>In similar simple fashion (without nasty loops).</p>
<p>PS. For curious people this if statement is working natively in one of functional languages. </p>
... | 1 | 2009-10-09T12:21:07Z | 1,543,464 | <pre><code>[m for m, i in zip(msg, a) if i < 0]
</code></pre>
| 2 | 2009-10-09T12:23:57Z | [
"python",
"filter"
] |
simple dropping elements from the list in python | 1,543,456 | <p>I'd like to achieve following effect</p>
<pre><code>a=[11, -1, -1, -1]
msg=['one','two','tree','four']
msg[where a<0]
['two','tree','four']
</code></pre>
<p>In similar simple fashion (without nasty loops).</p>
<p>PS. For curious people this if statement is working natively in one of functional languages. </p>
... | 1 | 2009-10-09T12:21:07Z | 1,543,792 | <p>The answers already posted are good, but if you want an alternative you can look at numpy and at its arrays.</p>
<pre><code>>>> import numpy as np
>>> a = np.array([11, -1, -1, -1])
>>> msg = np.array(['one','two','tree','four'])
>>> a < 0
array([False, True, True, True], d... | 1 | 2009-10-09T13:35:58Z | [
"python",
"filter"
] |
simple dropping elements from the list in python | 1,543,456 | <p>I'd like to achieve following effect</p>
<pre><code>a=[11, -1, -1, -1]
msg=['one','two','tree','four']
msg[where a<0]
['two','tree','four']
</code></pre>
<p>In similar simple fashion (without nasty loops).</p>
<p>PS. For curious people this if statement is working natively in one of functional languages. </p>
... | 1 | 2009-10-09T12:21:07Z | 1,543,987 | <p>I think <strong>[msg[i] for i in range(len(a)) if a[i]<0]</strong> will work for you.</p>
| 0 | 2009-10-09T14:05:10Z | [
"python",
"filter"
] |
Can't get django-registration to work (on Windows) | 1,543,601 | <p>Trying to add <a href="http://bitbucket.org/ubernostrum/django-registration/wiki/Home" rel="nofollow">django-registration</a> to my app. I have installed <a href="http://pypi.python.org/pypi/setuptools" rel="nofollow">setup tools</a> to use <a href="http://peak.telecommunity.com/DevCenter/EasyInstall#using-easy-inst... | 0 | 2009-10-09T12:59:30Z | 1,543,639 | <p>Django-registration will be installed on your python path, not in the project itself. You can see if it installed correctly by entering your python prompt and running:</p>
<pre><code>>>>import registration
</code></pre>
<p>If you don't get an error it is working and installed. Just add 'registration' to y... | 4 | 2009-10-09T13:07:46Z | [
"python",
"django",
"registration",
"setuptools",
"easy-install"
] |
Python gzip: is there a way to decompress from a string? | 1,543,652 | <p>I've read this <a href="http://stackoverflow.com/questions/1313845/if-i-have-the-contents-of-a-zipfile-in-a-python-string-can-i-decompress-it-witho">SO post</a> around the problem to no avail.</p>
<p>I am trying to decompress a .gz file coming from an URL.</p>
<pre><code>url_file_handle=StringIO( gz_data )
gzip_fi... | 20 | 2009-10-09T13:09:12Z | 1,543,665 | <p><code>gzip.open</code> is a shorthand for opening a file, what you want is <code>gzip.GzipFile</code> which you can pass a fileobj</p>
<pre><code>open(filename, mode='rb', compresslevel=9)
#Shorthand for GzipFile(filename, mode, compresslevel).
</code></pre>
<p>vs</p>
<pre><code>class GzipFile
__init__(sel... | 27 | 2009-10-09T13:13:46Z | [
"python",
"gzip"
] |
Python gzip: is there a way to decompress from a string? | 1,543,652 | <p>I've read this <a href="http://stackoverflow.com/questions/1313845/if-i-have-the-contents-of-a-zipfile-in-a-python-string-can-i-decompress-it-witho">SO post</a> around the problem to no avail.</p>
<p>I am trying to decompress a .gz file coming from an URL.</p>
<pre><code>url_file_handle=StringIO( gz_data )
gzip_fi... | 20 | 2009-10-09T13:09:12Z | 18,319,515 | <p>If your data is already in a string, try zlib, which claims to be fully gzip compatible:</p>
<pre><code>import zlib
decompressed_data = zlib.decompress(gz_data, 16+zlib.MAX_WBITS)
</code></pre>
<p>Read more: <a href="http://docs.python.org/library/zlib.html">http://docs.python.org/library/zlib.html</a>â</p>
| 17 | 2013-08-19T17:21:45Z | [
"python",
"gzip"
] |
Comprehensions in Python and Javascript are only very basic? | 1,543,820 | <p>Looking at comprehensions in Python and Javascript, so far I can't see some of the main features that I consider most powerful in comprehensions in languages like Haskell. </p>
<p>Do they allow things like multiple generators? Or are they just a basic map-filter form?</p>
<p>If they don't allow multiple generato... | 6 | 2009-10-09T13:39:13Z | 1,543,844 | <p>Yes, you can have multiple iterables in a <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" rel="nofollow">Python list comprehension</a>:</p>
<pre><code>>>> [(x,y) for x in range(2) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
</code></pre>
| 3 | 2009-10-09T13:42:04Z | [
"javascript",
"python",
"haskell",
"list-comprehension"
] |
Comprehensions in Python and Javascript are only very basic? | 1,543,820 | <p>Looking at comprehensions in Python and Javascript, so far I can't see some of the main features that I consider most powerful in comprehensions in languages like Haskell. </p>
<p>Do they allow things like multiple generators? Or are they just a basic map-filter form?</p>
<p>If they don't allow multiple generato... | 6 | 2009-10-09T13:39:13Z | 1,543,870 | <p>Python allows multiple generators:</p>
<pre><code>>>> [(x,y,x*y) for x in range(1,5) for y in range(1,5)]
[(1, 1, 1), (1, 2, 2), (1, 3, 3), (1, 4, 4),
(2, 1, 2), (2, 2, 4), (2, 3, 6), (2, 4, 8),
(3, 1, 3), (3, 2, 6), (3, 3, 9), (3, 4, 12),
(4, 1, 4), (4, 2, 8), (4, 3, 12), (4, 4, 16)]
</code></pre>
<... | 12 | 2009-10-09T13:44:43Z | [
"javascript",
"python",
"haskell",
"list-comprehension"
] |
Comprehensions in Python and Javascript are only very basic? | 1,543,820 | <p>Looking at comprehensions in Python and Javascript, so far I can't see some of the main features that I consider most powerful in comprehensions in languages like Haskell. </p>
<p>Do they allow things like multiple generators? Or are they just a basic map-filter form?</p>
<p>If they don't allow multiple generato... | 6 | 2009-10-09T13:39:13Z | 1,543,956 | <p>Add an if statement as well...</p>
<pre><code>>>> [(x,y) for x in range(5) for y in range(6) if x % 3 == 0 and y % 2 == 0]
[(0, 0), (0, 2), (0, 4), (3, 0), (3, 2), (3, 4)]
</code></pre>
| 1 | 2009-10-09T13:58:33Z | [
"javascript",
"python",
"haskell",
"list-comprehension"
] |
Comprehensions in Python and Javascript are only very basic? | 1,543,820 | <p>Looking at comprehensions in Python and Javascript, so far I can't see some of the main features that I consider most powerful in comprehensions in languages like Haskell. </p>
<p>Do they allow things like multiple generators? Or are they just a basic map-filter form?</p>
<p>If they don't allow multiple generato... | 6 | 2009-10-09T13:39:13Z | 1,543,985 | <p>Comprehensions is very powerful in Haskell to a large extent because Haskell is functional, so it makes extremely much sense for them to be. Python is not functional so it makes less sense.</p>
<p>You can make a lot of complex things with comprehensions in Python but it quickly becomes hard to read, thereby defeati... | 1 | 2009-10-09T14:04:48Z | [
"javascript",
"python",
"haskell",
"list-comprehension"
] |
How to insert repeated message in google protocol buffer _pb2.py file | 1,543,904 | <p>How to insert repeated message in google protocol buffer _pb2.py file</p>
| 0 | 2009-10-09T13:49:15Z | 1,770,798 | <p>make changes to your .proto file. Look at the example : <a href="http://code.google.com/apis/protocolbuffers/docs/pythontutorial.html" rel="nofollow">http://code.google.com/apis/protocolbuffers/docs/pythontutorial.html</a></p>
| 0 | 2009-11-20T14:31:18Z | [
"python",
"buffer",
"protocols"
] |
Force another program's standard output to be unbuffered using Python | 1,544,050 | <p>A python script is controlling an external application on Linux, passing in input via a pipe to the external applications stdin, and reading output via a pipe from the external applications stdout. </p>
<p>The problem is that writes to pipes are buffered by block, and not by line, and therefore delays occur before ... | 11 | 2009-10-09T14:15:36Z | 1,544,155 | <p>Try running the Python interpreter with the -u argument:</p>
<pre><code>python -u myscript.py
</code></pre>
<p>This forces Python to use unbuffered stdin/stdout which may help you.</p>
| -1 | 2009-10-09T14:33:28Z | [
"python",
"linux",
"pipe",
"stdout"
] |
Force another program's standard output to be unbuffered using Python | 1,544,050 | <p>A python script is controlling an external application on Linux, passing in input via a pipe to the external applications stdin, and reading output via a pipe from the external applications stdout. </p>
<p>The problem is that writes to pipes are buffered by block, and not by line, and therefore delays occur before ... | 11 | 2009-10-09T14:15:36Z | 1,544,178 | <p>I don't think it's possible. If the source application doesn't flush its outgoing buffer, the data will not reach outside that process until the buffer overflows and a flush is forced.</p>
<p>Notice how a well-established command such as <a href="http://linux.about.com/library/cmd/blcmdl1%5Ffile.htm" rel="nofollow"... | 3 | 2009-10-09T14:36:23Z | [
"python",
"linux",
"pipe",
"stdout"
] |
Force another program's standard output to be unbuffered using Python | 1,544,050 | <p>A python script is controlling an external application on Linux, passing in input via a pipe to the external applications stdin, and reading output via a pipe from the external applications stdout. </p>
<p>The problem is that writes to pipes are buffered by block, and not by line, and therefore delays occur before ... | 11 | 2009-10-09T14:15:36Z | 1,544,661 | <p>Doing this is possible, but the only solution I can think of is fairly convoluted, non-portable, and probably fraught with problematic details. You can use LD_PRELOAD to cause the external application to load a dynamic library which contains a constructor that invokes setvbuf to unbuffer stdout. You will probably ... | 5 | 2009-10-09T15:58:32Z | [
"python",
"linux",
"pipe",
"stdout"
] |
Force another program's standard output to be unbuffered using Python | 1,544,050 | <p>A python script is controlling an external application on Linux, passing in input via a pipe to the external applications stdin, and reading output via a pipe from the external applications stdout. </p>
<p>The problem is that writes to pipes are buffered by block, and not by line, and therefore delays occur before ... | 11 | 2009-10-09T14:15:36Z | 1,544,690 | <p>Its worth noting that some programs only buffer their output when they think it's not going to a "real user" (ie, a tty). When they detect that their output is being read by another program, they buffer.</p>
<p>The emulation of a tty is one of the things that <a href="http://expect.nist.gov/" rel="nofollow">Expect<... | 2 | 2009-10-09T16:02:16Z | [
"python",
"linux",
"pipe",
"stdout"
] |
Force another program's standard output to be unbuffered using Python | 1,544,050 | <p>A python script is controlling an external application on Linux, passing in input via a pipe to the external applications stdin, and reading output via a pipe from the external applications stdout. </p>
<p>The problem is that writes to pipes are buffered by block, and not by line, and therefore delays occur before ... | 11 | 2009-10-09T14:15:36Z | 1,547,764 | <p>You can use PTYs to solve this by:</p>
<ul>
<li>Creating a pty master/slave pair;</li>
<li>Connecting the child process's stdin, stdout and stderr to the pty slave device;</li>
<li>Reading from and writing to the pty master in the parent.</li>
</ul>
| 5 | 2009-10-10T11:56:37Z | [
"python",
"linux",
"pipe",
"stdout"
] |
Force another program's standard output to be unbuffered using Python | 1,544,050 | <p>A python script is controlling an external application on Linux, passing in input via a pipe to the external applications stdin, and reading output via a pipe from the external applications stdout. </p>
<p>The problem is that writes to pipes are buffered by block, and not by line, and therefore delays occur before ... | 11 | 2009-10-09T14:15:36Z | 6,270,326 | <p>The answer to this question might help:</p>
<p><a href="http://stackoverflow.com/questions/5411780/python-run-a-daemon-sub-process-read-stdout">Python Run a daemon sub-process & read stdout</a></p>
<p>Seems to be addressing the same issue.</p>
| 2 | 2011-06-07T19:07:53Z | [
"python",
"linux",
"pipe",
"stdout"
] |
Force another program's standard output to be unbuffered using Python | 1,544,050 | <p>A python script is controlling an external application on Linux, passing in input via a pipe to the external applications stdin, and reading output via a pipe from the external applications stdout. </p>
<p>The problem is that writes to pipes are buffered by block, and not by line, and therefore delays occur before ... | 11 | 2009-10-09T14:15:36Z | 9,477,623 | <p>This question is a bit old, but I think your problem could now be solved by using subprocess to call <a href="http://www.pixelbeat.org/programming/stdio_buffering/stdbuf-man.html" rel="nofollow">stdbuf</a> with the command you want to exec.</p>
| 1 | 2012-02-28T06:50:45Z | [
"python",
"linux",
"pipe",
"stdout"
] |
Non-intrusively unlock file on Windows | 1,544,275 | <p>Is there a way to unlock a file on Windows with a Python script? The file is exclusively locked by another process. I need a solution without killing or interupting the locking process.</p>
<p>I already had a look at <a href="http://code.activestate.com/recipes/65203/" rel="nofollow">portalocker</a>, a portable loc... | 1 | 2009-10-09T14:48:24Z | 1,544,308 | <p><a href="http://technet.microsoft.com/en-us/magazine/2009.04.windowsconfidential.aspx">Bad idea</a></p>
| 9 | 2009-10-09T14:52:22Z | [
"python",
"windows",
"file",
"winapi",
"locking"
] |
Non-intrusively unlock file on Windows | 1,544,275 | <p>Is there a way to unlock a file on Windows with a Python script? The file is exclusively locked by another process. I need a solution without killing or interupting the locking process.</p>
<p>I already had a look at <a href="http://code.activestate.com/recipes/65203/" rel="nofollow">portalocker</a>, a portable loc... | 1 | 2009-10-09T14:48:24Z | 1,544,319 | <p>Anything you do will affect the other process if that process thinks it has a lock on the file then breaking the lock means that the program has unexpected brhaviour and could brek or corrupt things.</p>
<p>Thus only do this if you know exactly what will happen.</p>
<p>The api used by the other program probably us... | 0 | 2009-10-09T14:55:16Z | [
"python",
"windows",
"file",
"winapi",
"locking"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.