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
Python with Netbeans 6.5
462,068
<p>Can you give me some links or explain how to configure an existing python project onto Netbeans? I'm trying it these days and it continues to crash also code navigation doesn't work well and I've problems with debugging. Surely these problems are related to my low eperience about python and I need support also in t...
1
2009-01-20T16:37:53Z
462,107
<p>Python support is in beta, and as someone who works with NB for a past 2 years, I can say that even a release versions are buggy and sometimes crashes. Early Ruby support was also very shaky.</p>
1
2009-01-20T16:48:32Z
[ "python", "netbeans", "project" ]
Packet sniffing in Python (Windows)
462,439
<p>What is the best way to sniff network packets using Python?</p> <p>I've heard from several places that the best module for this is a module called Scapy, unfortunately, it makes python.exe crash on my system. I would assume that it's just a problem with how I installed it, except that many other people have told me...
5
2009-01-20T18:18:33Z
462,447
<p>Use <a href="http://sourceforge.net/projects/pylibpcap/">python-libpcap</a>.</p> <pre><code>import pcap p = pcap.pcapObject() dev = pcap.lookupdev() p.open_live(dev, 1600, 0, 100) #p.setnonblock(1) try: for pktlen, data, timestamp in p: print "[%s] Got data: %s" % (time.strftime('%H:%M', ...
9
2009-01-20T18:20:59Z
[ "python", "sniffing", "sniffer" ]
Packet sniffing in Python (Windows)
462,439
<p>What is the best way to sniff network packets using Python?</p> <p>I've heard from several places that the best module for this is a module called Scapy, unfortunately, it makes python.exe crash on my system. I would assume that it's just a problem with how I installed it, except that many other people have told me...
5
2009-01-20T18:18:33Z
462,473
<p>Another option is <a href="http://code.google.com/p/pypcap/" rel="nofollow">pypcap</a>.</p> <p>To parse the results, <a href="http://construct.wikispaces.com/" rel="nofollow">Construct</a> is very slick.</p>
1
2009-01-20T18:26:40Z
[ "python", "sniffing", "sniffer" ]
Packet sniffing in Python (Windows)
462,439
<p>What is the best way to sniff network packets using Python?</p> <p>I've heard from several places that the best module for this is a module called Scapy, unfortunately, it makes python.exe crash on my system. I would assume that it's just a problem with how I installed it, except that many other people have told me...
5
2009-01-20T18:18:33Z
462,497
<p>Using <a href="http://code.google.com/p/pypcap/" rel="nofollow">pypcap</a>:</p> <pre><code>import dpkt, pcap pc = pcap.pcap() # construct pcap object pc.setfilter('icmp') # filter out unwanted packets for timestamp, packet in pc: print dpkt.ethernet.Ethernet(packet) </code></pre> <p>output sample:</p> <pr...
7
2009-01-20T18:31:15Z
[ "python", "sniffing", "sniffer" ]
Packet sniffing in Python (Windows)
462,439
<p>What is the best way to sniff network packets using Python?</p> <p>I've heard from several places that the best module for this is a module called Scapy, unfortunately, it makes python.exe crash on my system. I would assume that it's just a problem with how I installed it, except that many other people have told me...
5
2009-01-20T18:18:33Z
32,301,251
<h1>The hard way</h1> <p>You can sniff all of the IP packets using a raw socket.<br> Raw socket is a socket the sends and receives data in binary.<br> Binary in python is represented in a string which looks like this <code>\x00\xff</code>... every <code>\x..</code> is a byte.<br> To read an IP packet you need to analy...
5
2015-08-30T21:19:05Z
[ "python", "sniffing", "sniffer" ]
Packet sniffing in Python (Windows)
462,439
<p>What is the best way to sniff network packets using Python?</p> <p>I've heard from several places that the best module for this is a module called Scapy, unfortunately, it makes python.exe crash on my system. I would assume that it's just a problem with how I installed it, except that many other people have told me...
5
2009-01-20T18:18:33Z
34,158,773
<p>you can use raw sockets, with your interface ip address (in admin mode),</p> <pre><code>import socket s = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_IP) s.bind(("YOUR_INTERFACE_IP",0)) s.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1) s.ioctl(socket.SIO_RCVALL,socket.RCVALL_ON) while True: dat...
2
2015-12-08T14:45:49Z
[ "python", "sniffing", "sniffer" ]
Can I get the matrix determinant using Numpy?
462,500
<p>I read in the manual of Numpy that there is function <code>det(M)</code> that can calculate the determinant. However, I can't find the <code>det()</code> method in Numpy.</p> <p>By the way, I use Python 2.5. There should be no compatibility problems with Numpy.</p>
29
2009-01-20T18:32:16Z
462,514
<p>You can use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.det.html"><code>numpy.linalg.det</code></a> to compute the determinant of an array:</p> <pre><code>In [1]: import numpy In [2]: M = [[1, 2], [3, 4]] In [3]: print numpy.linalg.det(M) Out[3]: -2.0000000000000004 </code></pre>
51
2009-01-20T18:35:26Z
[ "python", "numpy" ]
Can I get the matrix determinant using Numpy?
462,500
<p>I read in the manual of Numpy that there is function <code>det(M)</code> that can calculate the determinant. However, I can't find the <code>det()</code> method in Numpy.</p> <p>By the way, I use Python 2.5. There should be no compatibility problems with Numpy.</p>
29
2009-01-20T18:32:16Z
19,317,237
<p>For <strong>large arrays</strong> underflow/overflow may occur when using <code>numpy.linalg.det</code>, or you may get <code>inf</code> or <code>-inf</code> as an answer.</p> <p>In many of these cases you can use <code>numpy.linalg.slogdet</code> (<a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy....
14
2013-10-11T11:37:57Z
[ "python", "numpy" ]
Is it possible to make text translucent in wxPython?
462,933
<p>I am adding some wx.StaticText objects on top of my main wx.Frame, which already has a background image applied. However, the StaticText always seems to draw with a solid (opaque) background color, hiding the image. I have tried creating a wx.Color object and changing the alpha value there, but that yields no result...
1
2009-01-20T20:20:18Z
464,706
<p>You probably need some graphics rendering widget. As far as I know, in wxPython you can use either built-in wxGraphicsContext or pyCairo directly. Cairo is more powerful. However, I don't know the details.</p>
1
2009-01-21T10:19:09Z
[ "python", "wxpython", "transparency", "opacity" ]
Is it possible to make text translucent in wxPython?
462,933
<p>I am adding some wx.StaticText objects on top of my main wx.Frame, which already has a background image applied. However, the StaticText always seems to draw with a solid (opaque) background color, hiding the image. I have tried creating a wx.Color object and changing the alpha value there, but that yields no result...
1
2009-01-20T20:20:18Z
598,202
<p>I would try aggdraw into a small canvas.</p> <p>Any Static Text uses the platform's native label machinery, so you don't get that sort of control over it.</p>
0
2009-02-28T15:53:54Z
[ "python", "wxpython", "transparency", "opacity" ]
Schwartzian sort example in "Text Processing in Python"
463,032
<p>I was browsing through "Text Processing in Python" and tried its <a href="http://gnosis.cx/TPiP/038.code" rel="nofollow">example</a> about Schwartzian sort. </p> <p>I used following structure for sample data which also contains empty lines. I sorted this data by fifth column:<br /> 383230 -49 -78 1 <strong>100034</...
0
2009-01-20T20:47:26Z
463,081
<p>I don't know what is the question, so I'll try to clarify things in a general way.</p> <p>This algorithm sorts lines by getting the 4th field and placing it in front of the lines. Then built-in <code>sort()</code> will use this field to sort. Later the original line is restored.</p> <p>The lines empty or shorter t...
1
2009-01-20T20:58:59Z
[ "python" ]
Schwartzian sort example in "Text Processing in Python"
463,032
<p>I was browsing through "Text Processing in Python" and tried its <a href="http://gnosis.cx/TPiP/038.code" rel="nofollow">example</a> about Schwartzian sort. </p> <p>I used following structure for sample data which also contains empty lines. I sorted this data by fifth column:<br /> 383230 -49 -78 1 <strong>100034</...
0
2009-01-20T20:47:26Z
463,085
<p>An empty line won't pass the test</p> <pre><code>if len(lst) &gt;= 4: </code></pre> <p>so it will have ['\377'] as its sort key, not the 5th column of your data, which is <code>lst[4]</code> ( <code>lst[0]</code> is the first column).</p>
0
2009-01-20T20:59:29Z
[ "python" ]
Schwartzian sort example in "Text Processing in Python"
463,032
<p>I was browsing through "Text Processing in Python" and tried its <a href="http://gnosis.cx/TPiP/038.code" rel="nofollow">example</a> about Schwartzian sort. </p> <p>I used following structure for sample data which also contains empty lines. I sorted this data by fifth column:<br /> 383230 -49 -78 1 <strong>100034</...
0
2009-01-20T20:47:26Z
463,120
<p>Well, it will sort short lines <em>almost</em> at the end, but not quite always. </p> <p>Actually, both the "naive" and the schwartzian version are flawed (in different ways). Nosklo and wbg already explained the algorithm, and you probably learn more if you try to find the error in the schwartzian version yourself...
0
2009-01-20T21:07:02Z
[ "python" ]
Schwartzian sort example in "Text Processing in Python"
463,032
<p>I was browsing through "Text Processing in Python" and tried its <a href="http://gnosis.cx/TPiP/038.code" rel="nofollow">example</a> about Schwartzian sort. </p> <p>I used following structure for sample data which also contains empty lines. I sorted this data by fifth column:<br /> 383230 -49 -78 1 <strong>100034</...
0
2009-01-20T20:47:26Z
464,815
<p>Not directly related to the question, but note that in recent versions of python (since 2.3 or 2.4 I think), the transform and untransform can be performed automatically using the <code>key</code> argument to <code>sort()</code> or <code>sorted()</code>. eg:</p> <pre><code>def key_func(line): lst = string.spli...
2
2009-01-21T10:53:14Z
[ "python" ]
Schwartzian sort example in "Text Processing in Python"
463,032
<p>I was browsing through "Text Processing in Python" and tried its <a href="http://gnosis.cx/TPiP/038.code" rel="nofollow">example</a> about Schwartzian sort. </p> <p>I used following structure for sample data which also contains empty lines. I sorted this data by fifth column:<br /> 383230 -49 -78 1 <strong>100034</...
0
2009-01-20T20:47:26Z
8,031,908
<p>Although the used of the Schwartzian transform is pretty outdated for Python it is worth mentioning that you could have written the code this way to avoid the possibility of a line with line[4] starting with <code>\377</code> being sorted into the wrong place</p> <pre><code>for n in range(len(lines)): lst = lin...
0
2011-11-07T01:36:14Z
[ "python" ]
UnicodeEncodeError with BeautifulSoup 3.1.0.1 and Python 2.5.2
463,215
<p>With BeautifulSoup 3.1.0.1 and Python 2.5.2, and trying to parse a web page in French. However, as soon as I call findAll, I get the following error:</p> <p><em>UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1146: ordinal not in range(128)</em></p> <p>Below is the code I am currently ...
4
2009-01-20T21:33:25Z
463,382
<p>Here is another idea. Your terminal is not capable of displaying an unicode string from Python. The interpreter tries to convert it to ASCII first. You should encode it explicitly before printing. I don't know the exact semantics of <code>soup.findAll()</code>. But it is probably something like:</p> <pre><code>for ...
11
2009-01-20T22:24:20Z
[ "python", "encoding", "screen-scraping", "beautifulsoup" ]
How can I type-check variables in Python?
463,604
<p>I have a Python function that takes a numeric argument that <strong>must</strong> be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?</p> <p>My first reaction is to do something like this:</p> <pre><code>def isInteger(n): return int(n) == n </code></pre> <p>B...
20
2009-01-20T23:58:45Z
463,630
<pre><code>isinstance(n, int) </code></pre> <p>If you need to know whether it's definitely an actual int and not a subclass of int (generally you shouldn't need to do this):</p> <pre><code>type(n) is int </code></pre> <p>this:</p> <pre><code>return int(n) == n </code></pre> <p>isn't such a good idea, as cross-type...
21
2009-01-21T00:06:11Z
[ "python", "typing", "dynamic-typing" ]
How can I type-check variables in Python?
463,604
<p>I have a Python function that takes a numeric argument that <strong>must</strong> be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?</p> <p>My first reaction is to do something like this:</p> <pre><code>def isInteger(n): return int(n) == n </code></pre> <p>B...
20
2009-01-20T23:58:45Z
463,635
<pre><code>if type(n) is int </code></pre> <p>This checks if <code>n</code> is a Python int, and <em>only</em> an int. It won't accept subclasses of <code>int</code>.</p> <p>Type-checking, however, does not fit the "Python way". You better use <code>n</code> as an int, and if it throws an exception, catch it and act ...
3
2009-01-21T00:08:19Z
[ "python", "typing", "dynamic-typing" ]
How can I type-check variables in Python?
463,604
<p>I have a Python function that takes a numeric argument that <strong>must</strong> be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?</p> <p>My first reaction is to do something like this:</p> <pre><code>def isInteger(n): return int(n) == n </code></pre> <p>B...
20
2009-01-20T23:58:45Z
463,833
<p>Don't type check. The whole point of duck typing is that you shouldn't have to. For instance, what if someone did something like this:</p> <pre><code>class MyInt(int): # ... extra stuff ... </code></pre>
1
2009-01-21T01:32:11Z
[ "python", "typing", "dynamic-typing" ]
How can I type-check variables in Python?
463,604
<p>I have a Python function that takes a numeric argument that <strong>must</strong> be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?</p> <p>My first reaction is to do something like this:</p> <pre><code>def isInteger(n): return int(n) == n </code></pre> <p>B...
20
2009-01-20T23:58:45Z
463,953
<p>Yeah, as Evan said, don't type check. Just try to use the value:</p> <pre><code>def myintfunction(value): """ Please pass an integer """ return 2 + value </code></pre> <p>That doesn't have a typecheck. It is much better! Let's see what happens when I try it:</p> <pre><code>&gt;&gt;&gt; myintfunction(5) 7 </...
17
2009-01-21T02:50:17Z
[ "python", "typing", "dynamic-typing" ]
How can I type-check variables in Python?
463,604
<p>I have a Python function that takes a numeric argument that <strong>must</strong> be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?</p> <p>My first reaction is to do something like this:</p> <pre><code>def isInteger(n): return int(n) == n </code></pre> <p>B...
20
2009-01-20T23:58:45Z
682,400
<p>Programming in Python and performing typechecking as you might in other languages does seem like choosing a screwdriver to bang a nail in with. It is more elegant to use Python's exception handling features.</p> <p>From an interactive command line, you can run a statement like:</p> <pre><code>int('sometext') </cod...
1
2009-03-25T16:34:53Z
[ "python", "typing", "dynamic-typing" ]
How can I type-check variables in Python?
463,604
<p>I have a Python function that takes a numeric argument that <strong>must</strong> be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?</p> <p>My first reaction is to do something like this:</p> <pre><code>def isInteger(n): return int(n) == n </code></pre> <p>B...
20
2009-01-20T23:58:45Z
1,789,397
<p>I would be tempted to to something like:</p> <pre><code>def check_and_convert(x): x = int(x) assert 0 &lt;= x &lt;= 255, "must be between 0 and 255 (inclusive)" return x class IPv4(object): """IPv4 CIDR prefixes is A.B.C.D/E where A-D are integers in the range 0-255, and E is an int ...
0
2009-11-24T11:14:35Z
[ "python", "typing", "dynamic-typing" ]
How can I type-check variables in Python?
463,604
<p>I have a Python function that takes a numeric argument that <strong>must</strong> be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?</p> <p>My first reaction is to do something like this:</p> <pre><code>def isInteger(n): return int(n) == n </code></pre> <p>B...
20
2009-01-20T23:58:45Z
8,256,603
<p>how about:</p> <pre><code>def ip(string): subs = string.split('.') if len(subs) != 4: raise ValueError("incorrect input") out = tuple(int(v) for v in subs if 0 &lt;= int(v) &lt;= 255) if len(out) != 4: raise ValueError("incorrect input") return out </code></pre> <p>ofcourse ther...
0
2011-11-24T11:53:15Z
[ "python", "typing", "dynamic-typing" ]
How can I type-check variables in Python?
463,604
<p>I have a Python function that takes a numeric argument that <strong>must</strong> be an integer in order for it behave correctly. What is the preferred way of verifying this in Python?</p> <p>My first reaction is to do something like this:</p> <pre><code>def isInteger(n): return int(n) == n </code></pre> <p>B...
20
2009-01-20T23:58:45Z
38,092,489
<p>Python now supports <a href="https://en.wikipedia.org/wiki/Gradual_typing" rel="nofollow">gradual typing</a> via the <a href="https://docs.python.org/3/library/typing.html" rel="nofollow">typing module</a> and <a href="http://mypy-lang.org/" rel="nofollow">mypy</a>. The <code>typing</code> module is a part of the st...
0
2016-06-29T07:00:10Z
[ "python", "typing", "dynamic-typing" ]
Python - Doing absolute imports from a subfolder
463,643
<p>Basically I'm asking the same question as this guy: <a href="http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python">http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python</a></p> <p>But no one gave him a correct answer. Given that you are inside a subfolder and you wan...
11
2009-01-21T00:10:47Z
463,691
<p>If I'm <a href="http://www.python.org/dev/peps/pep-0328/" rel="nofollow">reading</a> <a href="http://docs.python.org/whatsnew/2.5.html" rel="nofollow">correctly</a>, in Python 2.5 or higher:</p> <pre><code>from ..Module_B import Module_B </code></pre> <p>I thought I was well-versed in Python but I had no idea that...
2
2009-01-21T00:27:19Z
[ "python", "python-import" ]
Python - Doing absolute imports from a subfolder
463,643
<p>Basically I'm asking the same question as this guy: <a href="http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python">http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python</a></p> <p>But no one gave him a correct answer. Given that you are inside a subfolder and you wan...
11
2009-01-21T00:10:47Z
463,727
<p>If you are then importing Module_B in to App, you would</p> <p>Module_B.py: import ModuleA</p> <p>App.py (which also imports ModuleA which is now by default in your Pythonpath)</p> <pre><code>import Module_B.Module_B </code></pre> <p>Another alternative, is to update __init__.py (the one in Module_A/App fold...
0
2009-01-21T00:42:43Z
[ "python", "python-import" ]
Python - Doing absolute imports from a subfolder
463,643
<p>Basically I'm asking the same question as this guy: <a href="http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python">http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python</a></p> <p>But no one gave him a correct answer. Given that you are inside a subfolder and you wan...
11
2009-01-21T00:10:47Z
463,904
<pre><code>main.py setup.py app/ -&gt; __init__.py package_a/ -&gt; __init__.py module_a.py package_b/ -&gt; __init__.py module_b.py </code></pre> <ol> <li>You run <code>python main.py</code>.</li> <li><code>main.py</code> does: <code>import app.package_a.module_a</code></li> <l...
10
2009-01-21T02:15:30Z
[ "python", "python-import" ]
Django missing translation of some strings. Any idea why?
463,714
<p>I have a medium sized Django project, (running on AppEngine if it makes any difference), and have all the strings living in .po files like they should.</p> <p>I'm seeing strange behavior where certain strings just don't translate. They show up in the .po file when I run make_messages, with the correct file locatio...
7
2009-01-21T00:38:19Z
463,741
<p>Ugh. Django, you're killing me.</p> <p>Here's what was happening:</p> <p><a href="http://blog.e-shell.org/124">http://blog.e-shell.org/124</a></p> <p>For some reason only Django knows, it decided to decorate some of my translations with the comment '# fuzzy'. It seems to have chosen which ones to mark randomly....
8
2009-01-21T00:54:59Z
[ "python", "django", "internationalization", "translation" ]
Django missing translation of some strings. Any idea why?
463,714
<p>I have a medium sized Django project, (running on AppEngine if it makes any difference), and have all the strings living in .po files like they should.</p> <p>I'm seeing strange behavior where certain strings just don't translate. They show up in the .po file when I run make_messages, with the correct file locatio...
7
2009-01-21T00:38:19Z
463,928
<p>The fuzzy marker is added to the .po file by makemessages. When you have a new string (with no translations), it looks for similar strings, and includes them as the translation, with the fuzzy marker. This means, this is a crude match, so don't display it to the user, but it could be a good start for the human tra...
10
2009-01-21T02:31:43Z
[ "python", "django", "internationalization", "translation" ]
Possible to view method/function docs in NetBeans (Python plugin)?
463,806
<p>I just started trying out NetBeans 6.5 with Python support. I like that the IDE provides the docstring documentation for a function/method in a popup when it's auto-completing, but is there any other way to view docstrings, either by mousing over a function/method name, or clicking somewhere? I just can not seem to ...
1
2009-01-21T01:19:13Z
474,589
<p>I received the following answer from Tor Norbye at Sun:</p> <blockquote> <p>Hold the ctrl key (or Cmd on Mac) and then hover.</p> <p>Also, Ctrl-Shift-Space (e.g. code completion + shift) will display -just- the completion doc (which means it doesn't just look at the prefix of the caret, but the whole identif...
2
2009-01-23T21:17:45Z
[ "python", "ide", "netbeans", "documentation" ]
Possible to view method/function docs in NetBeans (Python plugin)?
463,806
<p>I just started trying out NetBeans 6.5 with Python support. I like that the IDE provides the docstring documentation for a function/method in a popup when it's auto-completing, but is there any other way to view docstrings, either by mousing over a function/method name, or clicking somewhere? I just can not seem to ...
1
2009-01-21T01:19:13Z
4,689,073
<p>You can also use this keyboard shortcut on Mac. Shift + Meta + Backslash</p>
1
2011-01-14T08:09:10Z
[ "python", "ide", "netbeans", "documentation" ]
Using DPAPI with Python?
463,832
<p>Is there a way to use the DPAPI (Data Protection Application Programming Interface) on Windows XP with Python?</p> <p>I would prefer to use an existing module if there is one that can do it. Unfortunately I haven't been able to find a way with Google or Stack Overflow.</p> <p><strong>EDIT:</strong> I've taken the...
13
2009-01-21T01:30:50Z
463,840
<p>The easiest way would be to use <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython" rel="nofollow">Iron Python</a>.</p>
2
2009-01-21T01:34:50Z
[ "python", "windows", "security", "encryption", "dpapi" ]
Using DPAPI with Python?
463,832
<p>Is there a way to use the DPAPI (Data Protection Application Programming Interface) on Windows XP with Python?</p> <p>I would prefer to use an existing module if there is one that can do it. Unfortunately I haven't been able to find a way with Google or Stack Overflow.</p> <p><strong>EDIT:</strong> I've taken the...
13
2009-01-21T01:30:50Z
463,852
<p>I have been using <code>CryptProtectData</code> and <code>CryptUnprotectData</code> through ctypes, with the code from</p> <p><a href="http://article.gmane.org/gmane.comp.python.ctypes/420">http://article.gmane.org/gmane.comp.python.ctypes/420</a></p> <p>and it has been working well.</p>
8
2009-01-21T01:37:54Z
[ "python", "windows", "security", "encryption", "dpapi" ]
Using DPAPI with Python?
463,832
<p>Is there a way to use the DPAPI (Data Protection Application Programming Interface) on Windows XP with Python?</p> <p>I would prefer to use an existing module if there is one that can do it. Unfortunately I haven't been able to find a way with Google or Stack Overflow.</p> <p><strong>EDIT:</strong> I've taken the...
13
2009-01-21T01:30:50Z
1,562,366
<p>Also, pywin32 implements CryptProtectData and CryptUnprotectData in the win32crypt module.</p>
4
2009-10-13T19:20:07Z
[ "python", "windows", "security", "encryption", "dpapi" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
463,971
<p>If you are going to use Python, I recommend using <a href="http://twistedmatrix.com/trac/" rel="nofollow">Twisted</a> for this. </p> <p>Specifically <a href="http://twistedmatrix.com/trac/wiki/TwistedRunner" rel="nofollow">Twisted Runner</a>. </p>
2
2009-01-21T02:59:40Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
463,981
<p>Can you elaborate what you mean by <em>in parallel</em>? It sounds like you need to implement some sort of locking in the queue so your entries are not selected twice, etc and the commands run only once.</p> <p>Most queue systems cheat -- they just write a giant to-do list, then select e.g. ten items, work them, an...
-2
2009-01-21T03:08:43Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
464,007
<p>GNU make (and perhaps other implementations as well) has the -j argument, which governs how many jobs it will run at once. When a job completes, make will start another one.</p>
7
2009-01-21T03:31:22Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
464,012
<p>Well, if they are largely independent of each other, I'd think in terms of:</p> <pre><code>Initialize an array of jobs pending (queue, ...) - 200 entries Initialize an array of jobs running - empty while (jobs still pending and queue of jobs running still has space) take a job off the pending queue launch ...
4
2009-01-21T03:34:12Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
464,029
<p>On the shell, <code>xargs</code> can be used to queue parallel command processing. For example, for having always 3 sleeps in parallel, sleeping for 1 second each, and executing 10 sleeps in total do</p> <pre><code>echo {1..10} | xargs -d ' ' -n1 -P3 sh -c 'sleep 1s' _ </code></pre> <p>And it would sleep for 4 sec...
41
2009-01-21T03:53:00Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
464,038
<p>I would imagine you could do this using make and the make -j xx command.</p> <p>Perhaps a makefile like this</p> <pre><code>all : usera userb userc.... usera: imapsync usera userb: imapsync userb .... </code></pre> <p>make -j 10 -f makefile</p>
38
2009-01-21T03:58:50Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
464,039
<p>In python, you could try:</p> <pre><code>import Queue, os, threading # synchronised queue queue = Queue.Queue(0) # 0 means no maximum size # do stuff to initialise queue with strings # representing os commands queue.put('sleep 10') queue.put('echo Sleeping..') # etc # or use python to generate commands, e.g. #...
3
2009-01-21T03:59:01Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
464,059
<p>Python's <a href="http://docs.python.org/dev/library/multiprocessing.html" rel="nofollow">multiprocessing module</a> would seem to fit your issue nicely. It's a high-level package that supports threading by process.</p>
1
2009-01-21T04:10:43Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
628,543
<p>For this kind of job PPSS is written: Parallel processing shell script. Google for this name and you will find it, I won't linkspam.</p>
12
2009-03-10T00:31:35Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
2,148,784
<p><a href="https://savannah.nongnu.org/projects/parallel/">Parallel</a> is made exatcly for this purpose.</p> <pre><code>cat userlist | parallel imapsync </code></pre> <p>One of the beauties of <a href="https://savannah.nongnu.org/projects/parallel/">Parallel</a> compared to other solutions is that it makes sure out...
24
2010-01-27T17:00:56Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
Parallel processing from a command queue on Linux (bash, python, ruby... whatever)
463,963
<p>I have a list/queue of 200 commands that I need to run in a shell on a Linux server. </p> <p>I only want to have a maximum of 10 processes running (from the queue) at once. Some processes will take a few seconds to complete, other processes will take much longer. </p> <p>When a process finishes I want the next com...
42
2009-01-21T02:54:45Z
7,236,743
<p><a href="https://savannah.gnu.org/projects/parallel" rel="nofollow">https://savannah.gnu.org/projects/parallel</a> (gnu parallel) and pssh might help.</p>
2
2011-08-29T22:30:14Z
[ "python", "ruby", "bash", "shell", "parallel-processing" ]
django application configuration
464,010
<p>I'm dying to get started Django but I'm really struggling with the initial setup. I have Python/MySql/Apache2.2/mod_python installed. Now I'm trying to create a proper directory structure and then update Django and Apache settings.py/httpd docs respectively. Specifically the location tag in the latter. Django and Py...
0
2009-01-21T03:32:58Z
464,069
<p>There's a lot to your question, so I'll try to boil it down to this: </p> <ol> <li><p>The tutorial is aimed at getting you to use the framework and to be up and running with as little configuration as possible. No server to configure, etc. If you are trying to load CSS with the dev server, you will need to pull the...
0
2009-01-21T04:15:23Z
[ "python", "django" ]
django application configuration
464,010
<p>I'm dying to get started Django but I'm really struggling with the initial setup. I have Python/MySql/Apache2.2/mod_python installed. Now I'm trying to create a proper directory structure and then update Django and Apache settings.py/httpd docs respectively. Specifically the location tag in the latter. Django and Py...
0
2009-01-21T03:32:58Z
464,453
<p>Don't use Apache for development, that'll make you tear your hair out restarting Apache every fifteen seconds (<strong>EDIT</strong>: or you could just use PythonDebug On).</p> <p><a href="http://docs.djangoproject.com/en/dev/howto/static-files/#limiting-use-to-debug-true" rel="nofollow">This technique</a> is how t...
1
2009-01-21T08:35:45Z
[ "python", "django" ]
django application configuration
464,010
<p>I'm dying to get started Django but I'm really struggling with the initial setup. I have Python/MySql/Apache2.2/mod_python installed. Now I'm trying to create a proper directory structure and then update Django and Apache settings.py/httpd docs respectively. Specifically the location tag in the latter. Django and Py...
0
2009-01-21T03:32:58Z
467,836
<p>Thanks guys. After doing some more searching I found exactly what i was looking for <a href="http://rob.cogit8.org/blog/2008/Jun/20/django-and-relativity/" rel="nofollow">here</a>. It's an example project directory structure and settings.py. If you view the comments there you can see a lot of others were confused a...
1
2009-01-22T02:09:42Z
[ "python", "django" ]
django application configuration
464,010
<p>I'm dying to get started Django but I'm really struggling with the initial setup. I have Python/MySql/Apache2.2/mod_python installed. Now I'm trying to create a proper directory structure and then update Django and Apache settings.py/httpd docs respectively. Specifically the location tag in the latter. Django and Py...
0
2009-01-21T03:32:58Z
3,304,468
<p>We just built and released (under Apache2) Djenesis at OSCON2010:</p> <ul> <li><a href="http://code.google.com/p/djenesis/" rel="nofollow">Djenesis on Google Code</a></li> </ul> <p>The two goals of Djenesis are to</p> <ul> <li>ease bootstrapping new projects based on your</li> <li>provide a default project templa...
1
2010-07-21T22:43:43Z
[ "python", "django" ]
How are POST and GET variables handled in Python?
464,040
<p>In PHP you can just use <code>$_POST</code> for POST and <code>$_GET</code> for GET (Query string) variables. What's the equivalent in Python?</p>
103
2009-01-21T03:59:03Z
464,060
<p>It somewhat depends on what you use as a CGI framework, but they are available in dictionaries accessible to the program. I'd point you to the docs, but I'm not getting through to python.org right now. But <a href="http://mail.python.org/pipermail/python-list/2008-April/485712.html" rel="nofollow">this note on mail...
2
2009-01-21T04:11:28Z
[ "python", "post", "get" ]
How are POST and GET variables handled in Python?
464,040
<p>In PHP you can just use <code>$_POST</code> for POST and <code>$_GET</code> for GET (Query string) variables. What's the equivalent in Python?</p>
103
2009-01-21T03:59:03Z
464,086
<p>Python is only a language, to get GET and POST data, you need a web framework or toolkit written in Python. Django is one, as Charlie points out, the cgi and urllib standard modules are others. Also available are Turbogears, Pylons, CherryPy, web.py, mod_python, fastcgi, etc, etc.</p> <p>In Django, your view func...
-1
2009-01-21T04:30:44Z
[ "python", "post", "get" ]
How are POST and GET variables handled in Python?
464,040
<p>In PHP you can just use <code>$_POST</code> for POST and <code>$_GET</code> for GET (Query string) variables. What's the equivalent in Python?</p>
103
2009-01-21T03:59:03Z
464,087
<p>They are stored in the CGI fieldstorage object.</p> <pre><code>import cgi form = cgi.FieldStorage() print "The user entered %s" % form.getvalue("uservalue") </code></pre>
25
2009-01-21T04:31:44Z
[ "python", "post", "get" ]
How are POST and GET variables handled in Python?
464,040
<p>In PHP you can just use <code>$_POST</code> for POST and <code>$_GET</code> for GET (Query string) variables. What's the equivalent in Python?</p>
103
2009-01-21T03:59:03Z
464,977
<p>suppose you're posting a html form with this:</p> <pre><code>&lt;input type="text" name="username"&gt; </code></pre> <p>If using <a href="http://docs.python.org/library/cgi.html">raw cgi</a>:</p> <pre><code>import cgi form = cgi.FieldStorage() print form["username"] </code></pre> <p>If using <a href="http://docs...
187
2009-01-21T11:53:01Z
[ "python", "post", "get" ]
How are POST and GET variables handled in Python?
464,040
<p>In PHP you can just use <code>$_POST</code> for POST and <code>$_GET</code> for GET (Query string) variables. What's the equivalent in Python?</p>
103
2009-01-21T03:59:03Z
11,353,284
<p>I've found nosklo's answer very extensive and useful! For those, like myself, who might find accessing the raw request data directly also useful, I would like to add the way to do that:</p> <pre><code>import os, sys # the query string, which contains the raw GET data # (For example, for http://example.com/myscript...
21
2012-07-05T22:15:06Z
[ "python", "post", "get" ]
How are POST and GET variables handled in Python?
464,040
<p>In PHP you can just use <code>$_POST</code> for POST and <code>$_GET</code> for GET (Query string) variables. What's the equivalent in Python?</p>
103
2009-01-21T03:59:03Z
27,893,309
<p>I know this is an old question. Yet it's surprising that no good answer was given.</p> <p>First of all the question is completely valid without mentioning the framework. The CONTEXT is PHP language equivalence. Although there are many ways to get the query string parameters in Python, the framework variables are ju...
13
2015-01-11T23:26:51Z
[ "python", "post", "get" ]
Python - Hits per minute implementation?
464,314
<p>This seems like such a trivial problem, but I can't seem to pin how I want to do it. Basically, I want to be able to produce a figure from a socket server that at any time can give the number of packets received in the last minute. How would I do that?</p> <p>I was thinking of maybe summing a dictionary that uses t...
2
2009-01-21T07:14:19Z
464,322
<p>A common pattern for solving this in other languages is to let the thing being measured simply increment an integer. Then you leave it to the listening client to determine intervals and frequencies.</p> <p>So you basically do <em>not</em> let the socket server know about stuff like "minutes", because that's a featu...
3
2009-01-21T07:19:56Z
[ "python" ]
Python - Hits per minute implementation?
464,314
<p>This seems like such a trivial problem, but I can't seem to pin how I want to do it. Basically, I want to be able to produce a figure from a socket server that at any time can give the number of packets received in the last minute. How would I do that?</p> <p>I was thinking of maybe summing a dictionary that uses t...
2
2009-01-21T07:14:19Z
464,329
<p>For what it's worth, your implementation above won't work if you don't receive a packet every second, as the next second entry won't necessarily be reset to 0.</p> <p>Either way, afaik the "correct" way to do this, ala logs analysis, is to keep a limited record of all the queries you receive. So just chuck the quer...
1
2009-01-21T07:24:35Z
[ "python" ]
Python - Hits per minute implementation?
464,314
<p>This seems like such a trivial problem, but I can't seem to pin how I want to do it. Basically, I want to be able to produce a figure from a socket server that at any time can give the number of packets received in the last minute. How would I do that?</p> <p>I was thinking of maybe summing a dictionary that uses t...
2
2009-01-21T07:14:19Z
464,347
<p>When you say the last minute, do you mean the exact last seconds or the last full minute from x:00 to x:59? The latter will be easier to implement and would probably give accurate results. You have one prev variable holding the value of the hits for the previous minute. Then you have a current value that increments ...
1
2009-01-21T07:35:51Z
[ "python" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
464,350
<p>This is simply merging. Treat each list as if it were a stack, and continuously pop the smaller of the two stack heads, adding the item to the result list, until one of the stacks is empty. Then add all remaining items to the resulting list.</p>
24
2009-01-21T07:36:52Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
464,357
<p>Well, the naive approach (combine 2 lists into large one and sort) will be O(N*log(N)) complexity. On the other hand, if you implement the merge manually (i do not know about any ready code in python libs for this, but i'm no expert) the complexity will be O(N), which is clearly faster. The idea is described wery we...
0
2009-01-21T07:39:55Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
464,367
<pre><code>def compareDate(obj1, obj2): if obj1.getDate() &lt; obj2.getDate(): return -1 elif obj1.getDate() &gt; obj2.getDate(): return 1 else: return 0 list = list1 + list2 list.sort(compareDate) </code></pre> <p>Will sort the list in place. Define your own function for compari...
0
2009-01-21T07:44:39Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
464,373
<p>Use the 'merge' step of merge sort, it runs in O(n) time.</p> <p>From <a href="http://en.wikipedia.org/wiki/Merge_sort" rel="nofollow">wikipedia</a> (pseudo-code):</p> <pre><code>function merge(left,right) var list result while length(left) &gt; 0 and length(right) &gt; 0 if first(left) ≤ first(r...
2
2009-01-21T07:49:13Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
464,454
<p>This is simple merging of two sorted lists. Take a look at the sample code below which merges two sorted lists of integers.</p> <pre><code>#!/usr/bin/env python ## merge.py -- Merge two sorted lists -*- Python -*- ## Time-stamp: "2009-01-21 14:02:57 ghoseb" l1 = [1, 3, 4, 7] l2 = [0, 2, 5, 6, 8, 9] def merge_sort...
3
2009-01-21T08:36:40Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
464,538
<p>People seem to be over complicating this.. Just combine the two lists, then sort them:</p> <pre><code>&gt;&gt;&gt; l1 = [1, 3, 4, 7] &gt;&gt;&gt; l2 = [0, 2, 5, 6, 8, 9] &gt;&gt;&gt; l1.extend(l2) &gt;&gt;&gt; sorted(l1) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] </code></pre> <p>..or shorter (and without modifying <code>l1</...
81
2009-01-21T09:14:08Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
464,767
<p>There is a slight flaw in <a href="http://stackoverflow.com/questions/464342/combining-two-sorted-lists-in-python#464454">ghoseb's</a> solution, making it O(n**2), rather than O(n).<br /> The problem is that this is performing:</p> <pre><code>item = l1.pop(0) </code></pre> <p>With linked lists or deques this would...
10
2009-01-21T10:36:56Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
465,043
<blockquote> <p>is there a smarter way to do this in Python</p> </blockquote> <p>This hasn't been mentioned, so I'll go ahead - there is a <a href="http://svn.python.org/view/python/trunk/Lib/heapq.py?view=markup">merge stdlib function</a> in the heapq module of python 2.6+. If all you're looking to do is getting th...
68
2009-01-21T12:16:08Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
465,232
<pre><code>from datetime import datetime from itertools import chain from operator import attrgetter class DT: def __init__(self, dt): self.dt = dt list1 = [DT(datetime(2008, 12, 5, 2)), DT(datetime(2009, 1, 1, 13)), DT(datetime(2009, 1, 3, 5))] list2 = [DT(datetime(2008, 12, 31, 23)), ...
4
2009-01-21T13:10:00Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
482,848
<p>Long story short, unless <code>len(l1 + l2) ~ 1000000</code> use:</p> <pre><code>L = l1 + l2 L.sort() </code></pre> <p><img src="http://i.stack.imgur.com/XsRdj.png" alt="merge vs. sort comparison"></p> <p>Description of the figure and source code can be found <a href="http://stackoverflow.com/questions/464960/cod...
44
2009-01-27T10:09:03Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
10,074,770
<p>Recursive implementation is below. Average performance is O(n).</p> <pre><code>def merge_sorted_lists(A, B, sorted_list = None): if sorted_list == None: sorted_list = [] slice_index = 0 for element in A: if element &lt;= B[0]: sorted_list.append(element) slice_in...
0
2012-04-09T14:32:18Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
10,162,814
<p>Python's sort implementation "timsort" is specifically optimized for lists that contain ordered sections. Plus, it's written in C. </p> <p><a href="http://bugs.python.org/file4451/timsort.txt" rel="nofollow">http://bugs.python.org/file4451/timsort.txt</a><br> <a href="http://en.wikipedia.org/wiki/Timsort" rel="no...
2
2012-04-15T14:20:36Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
18,675,785
<p>If you want to do it in a manner more consistent with learning what goes on in the iteration try this</p> <pre><code>def merge_arrays(a, b): l= [] while len(a) &gt; 0 and len(b)&gt;0: if a[0] &lt; b[0]: l.append(a.pop(0)) else:l.append(b.pop(0)) l.extend(a+b) print( l ) </code>...
1
2013-09-07T17:04:13Z
[ "python", "list", "sorting" ]
Combining two sorted lists in Python
464,342
<p>I have two lists of objects. Each list is already sorted by a property of the object that is of the datetime type. I would like to combine the two lists into one sorted list. Is the best way just to do a sort or is there a smarter way to do this in Python?</p>
46
2009-01-21T07:33:47Z
25,581,987
<pre><code>import random n=int(input("Enter size of table 1")); #size of list 1 m=int(input("Enter size of table 2")); # size of list 2 tb1=[random.randrange(1,101,1) for _ in range(n)] # filling the list with random tb2=[random.randrange(1,101,1) for _ in range(m)] # numbers between 1 and 100 tb1....
1
2014-08-30T11:43:39Z
[ "python", "list", "sorting" ]
unit testing for an application server
464,543
<p>I wrote an application server (using python &amp; twisted) and I want to start writing some tests. But I do not want to use Twisted's Trial due to time constraints and not having time to play with it now. So here is what I have in mind: write a small test client that connects to the app server and makes the necessar...
2
2009-01-21T09:15:42Z
464,596
<p>haven't used twisted before, and the twisted/trial documentation isn't stellar from what I just saw, but it'll likely take you 2-3 days to implement correctly the test system you describe above. Now, like I said I have no idea about Trial, but I GUESS you could probably get it working in 1-2 days, since you already ...
0
2009-01-21T09:35:17Z
[ "python", "unit-testing", "twisted" ]
unit testing for an application server
464,543
<p>I wrote an application server (using python &amp; twisted) and I want to start writing some tests. But I do not want to use Twisted's Trial due to time constraints and not having time to play with it now. So here is what I have in mind: write a small test client that connects to the app server and makes the necessar...
2
2009-01-21T09:15:42Z
464,870
<p><strong>"My question is: Is this a correct approach?"</strong></p> <p>It's what you chose. You made a lot of excuses, so I'm assuming that your pretty well fixed on this course. It's not the best, but you've already listed all your reasons for doing it (and then asked follow-up questions on this specific course o...
1
2009-01-21T11:15:34Z
[ "python", "unit-testing", "twisted" ]
unit testing for an application server
464,543
<p>I wrote an application server (using python &amp; twisted) and I want to start writing some tests. But I do not want to use Twisted's Trial due to time constraints and not having time to play with it now. So here is what I have in mind: write a small test client that connects to the app server and makes the necessar...
2
2009-01-21T09:15:42Z
465,422
<p>I think you chose the wrong direction. It's true that the Trial docs is very light. But Trial is base on unittest and only add some stuff to deal with the reactor loop and the asynchronous calls (it's not easy to write tests that deal with deffers). All your tests that are not including deffer/asynchronous call will...
1
2009-01-21T14:01:38Z
[ "python", "unit-testing", "twisted" ]
unit testing for an application server
464,543
<p>I wrote an application server (using python &amp; twisted) and I want to start writing some tests. But I do not want to use Twisted's Trial due to time constraints and not having time to play with it now. So here is what I have in mind: write a small test client that connects to the app server and makes the necessar...
2
2009-01-21T09:15:42Z
500,629
<p>You should use Trial. It really isn't very hard. Trial's documentation could stand to be improved, but if you know how to use the standard library unit test, the only difference is that instead of writing</p> <pre><code>import unittest </code></pre> <p>you should write</p> <pre><code>from twisted.trial import u...
2
2009-02-01T11:25:33Z
[ "python", "unit-testing", "twisted" ]
Python regular expressions - how to capture multiple groups from a wildcard expression?
464,736
<p>I have a Python regular expression that contains a group which can occur zero or many times - but when I retrieve the list of groups afterwards, only the last one is present. Example:</p> <p><code>re.search("(\w)*", "abcdefg").groups</code>()</p> <p>this returns the list ('g',)</p> <p>I need it to return ('a','b'...
29
2009-01-21T10:29:31Z
464,755
<pre><code>re.findall(r"\w","abcdefg") </code></pre>
34
2009-01-21T10:33:24Z
[ "python", "regex", "lexical-analysis" ]
Python regular expressions - how to capture multiple groups from a wildcard expression?
464,736
<p>I have a Python regular expression that contains a group which can occur zero or many times - but when I retrieve the list of groups afterwards, only the last one is present. Example:</p> <p><code>re.search("(\w)*", "abcdefg").groups</code>()</p> <p>this returns the list ('g',)</p> <p>I need it to return ('a','b'...
29
2009-01-21T10:29:31Z
464,879
<p>In addition to <a href="http://stackoverflow.com/questions/464736/python-regular-expressions-how-to-capture-multiple-groups-from-a-wildcard-expre#464755">Douglas Leeder's solution</a>, here is the explanation:</p> <p>In regular expressions the group count is fixed. Placing a quantifier behind a group does not incre...
23
2009-01-21T11:19:08Z
[ "python", "regex", "lexical-analysis" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
464,882
<p>Have a look at <a href="http://docs.python.org/library/itertools.html#itertools.combinations">itertools.combinations</a>:</p> <blockquote> <pre><code>itertools.combinations(iterable, r) </code></pre> <p>Return r length subsequences of elements from the input iterable.</p> <p>Combinations are emitted in ...
146
2009-01-21T11:20:04Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
5,898,031
<p><a href="http://stackoverflow.com/questions/464864/python-code-to-pick-out-all-possible-combinations-from-a-list/464882#464882">This answer</a> missed one aspect: the OP asked for ALL combinations... not just combinations of length "r".</p> <p>So you'd either have to loop through all lengths "L":</p> <pre><code>im...
327
2011-05-05T12:56:24Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
6,542,458
<p>Here's a lazy one-liner, also using itertools:</p> <pre><code>def combinations(items): return ( set(compress(items,mask)) for mask in product(*[[0,1]]*len(items)) ) # alternative: ...in product([0,1], repeat=len(items)) ) </code></pre> <p>Main idea behind this answer: there are 2^N com...
23
2011-07-01T00:21:10Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
7,140,454
<p>Using list comprehension:</p> <pre><code>def selfCombine( list2Combine, length ): listCombined = str( ['list2Combine[i' + str( i ) + ']' for i in range( length )] ).replace( "'", '' ) \ + 'for i0 in range(len( list2Combine ) )' if length &gt; 1: listCombined += str( [' for i' + ...
1
2011-08-21T19:10:24Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
7,173,919
<p>I agree with Dan H that Ben indeed asked for <strong>all</strong> combinations. <code>itertools.combinations()</code> does not give all combinations.</p> <p>Another issue is, if the input iterable is big, it is perhaps better to return a generator instead of everything in a list:</p> <pre><code>iterable = range(10...
11
2011-08-24T10:24:55Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
23,743,696
<p>Here is one using recursion:</p> <pre><code>&gt;&gt;&gt; import copy &gt;&gt;&gt; def combinations(target,data): ... for i in range(len(data)): ... new_target = copy.copy(target) ... new_data = copy.copy(data) ... new_target.append(data[i]) ... new_data = data[i+1:] ... p...
8
2014-05-19T17:25:51Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
24,402,296
<p>This one-liner gives you all the combinations (between <code>0</code> and <code>n</code> items if the original list/set contains <code>n</code> distinct elements) and uses the native method <a href="https://docs.python.org/2/library/itertools.html#itertools.combinations"><code>itertools.combinations</code></a>:</p> ...
12
2014-06-25T07:08:01Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
29,092,176
<p><strong>You can generating all combinations of a list in python using this simple code</strong></p> <pre><code>import itertools a = [1,2,3,4] for i in xrange(0,len(a)+1): print list(itertools.combinations(a,i)) </code></pre> <p><strong>Result would be :</strong></p> <pre><code>[()] [(1,), (2,), (3,), (4,)] [(...
8
2015-03-17T05:52:28Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
29,981,889
<p>This code employs a simple algorithm with nested lists...</p> <pre><code># FUNCTION getCombos: To generate all combos of an input list, consider the following sets of nested lists... # # [ [ [] ] ] # [ [ [] ], [ [A] ] ] # [ [ [] ], [ [A],[B] ], [ [A,B] ] ] # [ [ [] ],...
-1
2015-05-01T05:07:03Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
32,555,515
<p>Below is a "standard recursive answer", similar to the other similar answer <a href="http://stackoverflow.com/a/23743696/711085">http://stackoverflow.com/a/23743696/711085</a> . (We don't realistically have to worry about running out of stack space since there's no way we could process all N! permutations.)</p> <p>...
0
2015-09-13T23:28:46Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
32,555,776
<p>Here is yet another solution (one-liner), involving using the <code>itertools.combinations</code> function, but here we use a double list comprehension (as opposed to a for loop or sum):</p> <pre><code>def combs(x): return [c for i in range(len(x)+1) for c in combinations(x,i)] </code></pre> <hr> <p>Demo:</p>...
2
2015-09-14T00:13:02Z
[ "python", "combinations" ]
Python code to pick out all possible combinations from a list?
464,864
<p>I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. </p> <p>I've found <a href="http://desk.stinkpot.org:8080/tricks/index.php/2008/04/get-all-possible-combinations-of-a-lists-elements-in-python/">some code</a> (by googling) that apparently does wh...
123
2009-01-21T11:13:40Z
37,092,698
<pre><code>def combinations(iterable, r): # combinations('ABCD', 2) --&gt; AB AC AD BC BD CD # combinations(range(4), 3) --&gt; 012 013 023 123 pool = tuple(iterable) n = len(pool) if r &gt; n: return indices = range(r) yield tuple(pool[i] for i in indices) while True: for i in reversed(range(r)): if in...
0
2016-05-07T19:17:02Z
[ "python", "combinations" ]
Parsing Functions
464,970
<p>I'm making a script parser in python and I'm a little stuck. I am not quite sure how to parse a line for all its functions (or even just one function at a time) and then search for a function with that name, and if it exists, execute that function short of writing a massive list if elif else block....</p> <p><em>ED...
0
2009-01-21T11:49:51Z
464,987
<p>Take a look at <a href="http://www.dabeaz.com/ply/" rel="nofollow">PLY</a>. It should help you keep your parser specification clean.</p>
0
2009-01-21T11:55:53Z
[ "python", "parsing" ]
Parsing Functions
464,970
<p>I'm making a script parser in python and I'm a little stuck. I am not quite sure how to parse a line for all its functions (or even just one function at a time) and then search for a function with that name, and if it exists, execute that function short of writing a massive list if elif else block....</p> <p><em>ED...
0
2009-01-21T11:49:51Z
465,013
<p>Check out <a href="http://pyparsing.wikispaces.com/" rel="nofollow">PyParsing</a>, it allows for definition of the grammar directly in Python code:</p> <p>Assuming a function call is just <code>somename()</code>:</p> <pre><code>&gt;&gt;&gt; from pyparsing import * &gt;&gt;&gt; grammar = Word(alphas + "_", alphanu...
2
2009-01-21T12:06:42Z
[ "python", "parsing" ]
Parsing Functions
464,970
<p>I'm making a script parser in python and I'm a little stuck. I am not quite sure how to parse a line for all its functions (or even just one function at a time) and then search for a function with that name, and if it exists, execute that function short of writing a massive list if elif else block....</p> <p><em>ED...
0
2009-01-21T11:49:51Z
465,022
<p>It all depends on what code you are parsing. </p> <p>If you are parsing Python syntax, use the <code>parser</code> module from Python: <a href="http://docs.python.org/library/parser.html" rel="nofollow">http://docs.python.org/library/parser.html</a></p> <p>A quite complete list of parser libraries available for P...
0
2009-01-21T12:08:41Z
[ "python", "parsing" ]
Parsing Functions
464,970
<p>I'm making a script parser in python and I'm a little stuck. I am not quite sure how to parse a line for all its functions (or even just one function at a time) and then search for a function with that name, and if it exists, execute that function short of writing a massive list if elif else block....</p> <p><em>ED...
0
2009-01-21T11:49:51Z
465,113
<p>Once you get the name of the function, use a dispatch dict to run the function:</p> <pre><code>def mysum(...): ... def myotherstuff(...): ... # create dispatch dict: myfunctions = {'sum': mysum, 'stuff': myotherstuff} # run your parser: function_name, parameters = parse_result(line) # run the function: myfunctio...
3
2009-01-21T12:37:12Z
[ "python", "parsing" ]
Writing unit tests in Django / Python
465,065
<p>I've not used Unit Tests before other than a quick introduction in a Uni course. I'm currently writing an application though and would like to teach myself TDD in the process. The problem is, I've no idea what to test or really how.</p> <p>I'm writing a Django application, and so far have only created the models (a...
28
2009-01-21T12:22:24Z
465,242
<p>I'm not exactly sure of the specifics of what you're trying to test here, I'd need more code snippets for this, but I can give you some general advice.</p> <p>First, read the unit testing chapter of "Dive into Python" (it's free online! <a href="http://diveintopython3.ep.io/unit-testing.html" rel="nofollow">http://...
9
2009-01-21T13:12:53Z
[ "python", "django", "unit-testing" ]
Writing unit tests in Django / Python
465,065
<p>I've not used Unit Tests before other than a quick introduction in a Uni course. I'm currently writing an application though and would like to teach myself TDD in the process. The problem is, I've no idea what to test or really how.</p> <p>I'm writing a Django application, and so far have only created the models (a...
28
2009-01-21T12:22:24Z
465,264
<p>Presumably you've already read Testing <a href="http://docs.djangoproject.com/en/dev/topics/testing/" rel="nofollow">Django Applications</a>.</p> <p>Start testing the normal use cases of your application, creating a new user, adding a blog entry, etc. Just your typical CRUD operations first, then move out to the e...
4
2009-01-21T13:24:11Z
[ "python", "django", "unit-testing" ]
Writing unit tests in Django / Python
465,065
<p>I've not used Unit Tests before other than a quick introduction in a Uni course. I'm currently writing an application though and would like to teach myself TDD in the process. The problem is, I've no idea what to test or really how.</p> <p>I'm writing a Django application, and so far have only created the models (a...
28
2009-01-21T12:22:24Z
465,684
<p><strong>Is a function to test each model within the ModelTests class a good way of writing tests?</strong></p> <p>No.</p> <p><strong>What exactly should I be testing for?</strong></p> <ul> <li><p>That creating a model with all of the fields completed works? </p></li> <li><p>That a half complete model fails? </p><...
36
2009-01-21T15:08:38Z
[ "python", "django", "unit-testing" ]
Tools for creating text as bitmaps (anti-aliased text, custom spacing, transparent background)
465,144
<p>I need to batch create images with text. Requirements:</p> <ol> <li>arbitrary size of bitmap</li> <li>PNG format</li> <li>transparent background</li> <li>black text anti-aliased against transparency</li> <li>adjustable character spacing</li> <li>adjustable text position (x and y coordinates where text begins)</li> ...
3
2009-01-21T12:45:55Z
465,381
<p>(5) indeed looks tricky, short of inserting dummy narrow-spaces into the string (which will break kerning) or using something much higher-level like the SVG or HTML/CSS renderer.</p> <p>However, if you don't mind getting your hands dirty, it looks quite easy to hack PIL's freetype renderer into adding horizontal sp...
3
2009-01-21T13:51:57Z
[ "python", "fonts", "bitmap", "spacing", "unix" ]
Tools for creating text as bitmaps (anti-aliased text, custom spacing, transparent background)
465,144
<p>I need to batch create images with text. Requirements:</p> <ol> <li>arbitrary size of bitmap</li> <li>PNG format</li> <li>transparent background</li> <li>black text anti-aliased against transparency</li> <li>adjustable character spacing</li> <li>adjustable text position (x and y coordinates where text begins)</li> ...
3
2009-01-21T12:45:55Z
465,391
<p>Here's the SVG + ImageMagick solution:</p> <p>Programmatically create SVG documents based on this template, replacing "TEXT HERE" with the desired text content:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-...
2
2009-01-21T13:53:48Z
[ "python", "fonts", "bitmap", "spacing", "unix" ]
Tools for creating text as bitmaps (anti-aliased text, custom spacing, transparent background)
465,144
<p>I need to batch create images with text. Requirements:</p> <ol> <li>arbitrary size of bitmap</li> <li>PNG format</li> <li>transparent background</li> <li>black text anti-aliased against transparency</li> <li>adjustable character spacing</li> <li>adjustable text position (x and y coordinates where text begins)</li> ...
3
2009-01-21T12:45:55Z
465,544
<p>From a quick glance, <a href="http://www.pango.org/" rel="nofollow">Pango</a> has support for <a href="http://library.gnome.org/devel/pango/unstable/pango-Text-Attributes.html#pango-attr-letter-spacing-new" rel="nofollow">letter spacing</a>. Pango has Python bindings and is integrated with Cairo.</p>
2
2009-01-21T14:36:45Z
[ "python", "fonts", "bitmap", "spacing", "unix" ]