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
email body from a parsed email object in jython
280,207
<p>I have an object.</p> <pre><code> fp = open(self.currentEmailPath, "rb") p = email.Parser.Parser() self._currentEmailParsedInstance= p.parse(fp) fp.close() </code></pre> <p>self.currentEmailParsedInstance, from this object I want to get the body of an email, text only no HTML....</p> <p>How do I do...
4
2008-11-11T07:07:09Z
280,562
<p>ended up with this</p> <pre><code> parser = email.parser.Parser() self._email = parser.parse(open('/home/vinko/jlm.txt','r')) parts=self._email.get_payload() check=parts[0].get_content_type() if check == "text/plain": return parts[0].get_payload() elif chec...
0
2008-11-11T10:42:45Z
[ "python", "email", "jython", "parsing" ]
Python - sort a list of nested lists
280,222
<p>I have input consisting of a list of nested lists like this:</p> <pre><code>l = [[[[[39]]]], [1, 2, 3], [4, [5, 3], 1], [[[[8, 9], 10], 11], 12]] </code></pre> <p>I want to sort this list based on the sum of all the numbers in the nested lists... so, the values I want to sort by of l would look like this:</p> <pr...
13
2008-11-11T07:15:56Z
280,224
<pre><code>l.sort(key=sum_nested) </code></pre> <p>Where <code>sum_nested()</code> is:</p> <pre><code>def sum_nested(astruct): try: return sum(map(sum_nested, astruct)) except TypeError: return astruct assert sum_nested([[([8, 9], 10), 11], 12]) == 50 </code></pre>
5
2008-11-11T07:18:02Z
[ "python", "list", "sorting", "nested-lists" ]
Python - sort a list of nested lists
280,222
<p>I have input consisting of a list of nested lists like this:</p> <pre><code>l = [[[[[39]]]], [1, 2, 3], [4, [5, 3], 1], [[[[8, 9], 10], 11], 12]] </code></pre> <p>I want to sort this list based on the sum of all the numbers in the nested lists... so, the values I want to sort by of l would look like this:</p> <pr...
13
2008-11-11T07:15:56Z
280,226
<p>A little recursive function would do it:</p> <pre><code>def asum(a): if isinstance(a, list): return sum(asum(x) for x in a) else: return a l = [[[[[39]]]], [1, 2, 3], [4, [5, 3], 1], [[[[8, 9], 10], 11], 12]] l.sort(key=asum) print l </code></pre>
12
2008-11-11T07:19:20Z
[ "python", "list", "sorting", "nested-lists" ]
Python - sort a list of nested lists
280,222
<p>I have input consisting of a list of nested lists like this:</p> <pre><code>l = [[[[[39]]]], [1, 2, 3], [4, [5, 3], 1], [[[[8, 9], 10], 11], 12]] </code></pre> <p>I want to sort this list based on the sum of all the numbers in the nested lists... so, the values I want to sort by of l would look like this:</p> <pr...
13
2008-11-11T07:15:56Z
280,865
<p>A slight simplification and generalization to the answers provided so far, using a recent addition to python's syntax:</p> <pre><code>&gt;&gt;&gt; l = [[[[[39]]]], [1, 2, 3], [4, [5, 3], 1], [[[[8, 9], 10], 11], 12]] &gt;&gt;&gt; def asum(t): return sum(map(asum, t)) if hasattr(t, '__iter__') else t ... &gt;&gt;&gt...
15
2008-11-11T13:26:37Z
[ "python", "list", "sorting", "nested-lists" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
280,248
<p>Here's a rather Scheme way to do it:</p> <pre><code>class cons: def __init__(self, f, r): self.__f = f self.__r = r def __str__(self): return "(%s, %s)" % (str(self.__f), str(self.__r)) __repr__ = __str__ class empty: de...
0
2008-11-11T07:35:22Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
280,284
<p>Immutable lists are best represented through two-tuples, with None representing NIL. To allow simple formulation of such lists, you can use this function:</p> <pre><code>def mklist(*args): result = None for element in reversed(args): result = (element, result) return result </code></pre> <p>To ...
10
2008-11-11T07:54:20Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
280,286
<p>I wrote this up the other day</p> <pre><code>#! /usr/bin/env python class node: def __init__(self): self.data = None # contains the data self.next = None # contains the reference to the next node class linked_list: def __init__(self): self.cur_node = None def add_node(self, d...
35
2008-11-11T07:54:49Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
280,572
<p>When using immutable linked lists, consider using Python's tuple directly. </p> <pre><code>ls = (1, 2, 3, 4, 5) def first(ls): return ls[0] def rest(ls): return ls[1:] </code></pre> <p>Its really that ease, and you get to keep the additional funcitons like len(ls), x in ls, etc.</p>
0
2008-11-11T10:46:43Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
281,294
<p>Here's a slightly more complex version of a linked list class, with a similar interface to python's sequence types (ie. supports indexing, slicing, concatenation with arbitrary sequences etc). It should have O(1) prepend, doesn't copy data unless it needs to and can be used pretty interchangably with tuples.</p> <...
11
2008-11-11T15:59:46Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
282,238
<p>For some needs, a <a href="https://docs.python.org/library/collections.html#collections.deque">deque</a> may also be useful. You can add and remove items on both ends of a deque at O(1) cost.</p> <pre><code>from collections import deque d = deque([1,2,3,4]) print d for x in d: print x print d.pop(), d </code><...
112
2008-11-11T21:45:30Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
283,630
<p>Here is some list functions based on <a href="http://stackoverflow.com/questions/280243/python-linked-list#280284">Martin v. Löwis's representation</a>:</p> <pre><code>cons = lambda el, lst: (el, lst) mklist = lambda *args: reduce(lambda lst, el: cons(el, lst), reversed(args), None) car = lambda lst: lst[0] if l...
51
2008-11-12T11:10:15Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
3,538,133
<p>The accepted answer is rather complicated. Here is a more standard design:</p> <pre><code>L = LinkedList() L.insert(1) L.insert(1) L.insert(2) L.insert(4) print L L.clear() print L </code></pre> <p>It is a simple <code>LinkedList</code> class based on the straightforward C++ design and <a href="http://greenteapres...
13
2010-08-21T16:04:51Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
6,658,636
<p>I based this additional function on <a href="http://stackoverflow.com/users/4960/nick-stinemates">Nick Stinemates</a></p> <pre><code>def add_node_at_end(self, data): new_node = Node() node = self.curr_node while node: if node.next == None: node.next = new_node new_node.ne...
1
2011-07-12T01:37:41Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
8,367,288
<p>I think the implementation below fill the bill quite gracefully.</p> <pre><code>'''singly linked lists, by Yingjie Lan, December 1st, 2011''' class linkst: '''Singly linked list, with pythonic features. The list has pointers to both the first and the last node.''' __slots__ = ['data', 'next'] #memory effic...
0
2011-12-03T11:20:54Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
15,182,629
<p>The following is what I came up with. It's similer to <a href="http://stackoverflow.com/a/280286/176611">Riccardo C.'s</a>, in this thread, except it prints the numbers in order instead of in reverse. I also made the LinkedList object a Python Iterator in order to print the list out like you would a normal Python li...
1
2013-03-03T05:03:45Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
17,850,004
<pre><code>class LinkedList: def __init__(self, value): self.value = value self.next = None def insert(self, node): if not self.next: self.next = node else: self.next.insert(node) def __str__(self): if self.next: return '%s -&gt; ...
0
2013-07-25T05:49:11Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
19,008,675
<p>I just did <a href="https://github.com/tlevine/cons.py/blob/master/cons.py" rel="nofollow">this</a> as a fun toy. It should be immutable as long as you don't touch the underscore-prefixed methods, and it implements a bunch of Python magic like indexing and <code>len</code>.</p>
1
2013-09-25T15:11:25Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
20,658,095
<p>First of all, I assume you want linked lists. In practice, you can use <code>collections.deque</code>, whose current CPython implementation is a doubly linked list of blocks (each block contains an array of 62 cargo objects). It subsumes linked list's functionality. You can also search for a C extension called <code...
0
2013-12-18T12:21:55Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
24,342,598
<pre><code>class LL(object): def __init__(self,val): self.val = val self.next = None def pushNodeEnd(self,top,val): if top is None: top.val=val top.next=None else: tmp=top while (tmp.next != None): tmp=tmp.next ...
0
2014-06-21T14:32:51Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
24,987,486
<p>I've put a Python 2.x and 3.x singly-linked list class at <a href="https://pypi.python.org/pypi/linked_list_mod/" rel="nofollow">https://pypi.python.org/pypi/linked_list_mod/</a></p> <p>It's tested with CPython 2.7, CPython 3.4, Pypy 2.3.1, Pypy3 2.3.1, and Jython 2.7b2, and comes with a nice automated test suite.<...
0
2014-07-28T01:48:23Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
26,088,052
<p>My 2 cents </p> <pre><code>class Node: def __init__(self, value=None, next=None): self.value = value self.next = next def __str__(self): return str(self.value) class LinkedList: def __init__(self): self.first = None self.last = None def add(self, x): ...
0
2014-09-28T18:20:00Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
26,658,719
<pre><code>enter code here enter code here class node: def __init__(self): self.data = None self.next = None class linked_list: def __init__(self): self.cur_node = None self.head = None def add_node(self,data): new_node = node() if self.head == None: ...
0
2014-10-30T16:45:52Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
26,671,637
<p>If you want to just create a simple liked list then refer this code</p> <p>l=[1,[2,[3,[4,[5,[6,[7,[8,[9,[10]]]]]]]]]]</p> <p>for visualize execution for this cod Visit <a href="http://www.pythontutor.com/visualize.html#mode=edit" rel="nofollow">http://www.pythontutor.com/visualize.html#mode=edit</a> </p>
0
2014-10-31T09:47:20Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
29,676,547
<p>The code given by user201788, intended to change Prepend in Nick Stinemates's original code to Append, doesn't work. Because self.cur_node is not updated, it stays as None, so the while loop will never happen. To make it work, we just need to do one more thing, change the initialization of class linked_list.</p> <p...
0
2015-04-16T13:40:36Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
39,681,493
<p>I envision a dictionary as a linked list.</p> <p>Say we have a list of cat -> dog -> rat -> bat:</p> <pre><code>dict1 = {} dict1["root"] = "cat" dict1["cat"] = "dog" dict1["dog"] = "rat" dict1["rat"] = "bat" dict1["bat"] = None </code></pre> <p>Insertion: </p> <pre><code>dict1["peacock"] = dict1["dog"] dict1["do...
0
2016-09-24T22:23:56Z
[ "python", "linked-list" ]
Python Linked List
280,243
<p>What's the easiest way to use a linked list in python? In scheme, a linked list is defined simply by <code>'(1 2 3 4 5)</code>. Python's lists, <code>[1, 2, 3, 4, 5]</code>, and tuples, <code>(1, 2, 3, 4, 5)</code>, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time conc...
110
2008-11-11T07:31:21Z
40,072,598
<pre><code>class LinkedStack: '''LIFO Stack implementation using a singly linked list for storage.''' _ToList = [] #---------- nested _Node class ----------------------------- class _Node: '''Lightweight, nonpublic class for storing a singly linked node.''' __slots__ = '_element', '_next' #streamline memo...
0
2016-10-16T16:30:52Z
[ "python", "linked-list" ]
When is "self" required?
280,324
<p>I have been using classes for only a short while and when I write a method, I make all variables reference self, e.g. self.foo.</p> <p>However, I'm looking through the <em>wxPython in Action</em> book and notice that "self" isn't used all the time. For example:</p> <pre><code> import wx class TextFrame(wx.Frame):...
9
2008-11-11T08:24:00Z
280,332
<p>In those instances, if you do not use <code>self</code> then you will create only a local variable of that name. In the first example, <code>panel</code> is created as a local variable and then referenced later in the function, but it won't be available outside that function. The act of passing <code>self</code> to ...
4
2008-11-11T08:26:41Z
[ "python", "wxpython" ]
When is "self" required?
280,324
<p>I have been using classes for only a short while and when I write a method, I make all variables reference self, e.g. self.foo.</p> <p>However, I'm looking through the <em>wxPython in Action</em> book and notice that "self" isn't used all the time. For example:</p> <pre><code> import wx class TextFrame(wx.Frame):...
9
2008-11-11T08:24:00Z
280,336
<p>self is <strong>always</strong> required when referring to the instance itself, except when calling the base class constructor (wx.Frame.__init__). All the other variables that you see in the examples (panel, basicLabel, basicText, ...) are just local variables - not related to the current object at all. These names...
0
2008-11-11T08:27:44Z
[ "python", "wxpython" ]
When is "self" required?
280,324
<p>I have been using classes for only a short while and when I write a method, I make all variables reference self, e.g. self.foo.</p> <p>However, I'm looking through the <em>wxPython in Action</em> book and notice that "self" isn't used all the time. For example:</p> <pre><code> import wx class TextFrame(wx.Frame):...
9
2008-11-11T08:24:00Z
280,561
<p>You use <code>self.attribute</code> to reference an attribute of your current instance.</p> <p>You use <code>wx.Frame.__init__()</code> to reference a method of the parent class.</p> <p>You don't use <code>self</code> if you only reference a local name (variable) of the method (function) you are in.</p> <p>These ...
7
2008-11-11T10:42:20Z
[ "python", "wxpython" ]
Escaping regex string in Python
280,435
<p>I want to use input from a user as a regex pattern for a search over some text. It works, but how I can handle cases where user puts characters that have meaning in regex? For example, the user wants to search for Word <code>(s)</code>: regex engine will take the <code>(s)</code> as a group. I want it to treat it...
128
2008-11-11T09:33:29Z
280,441
<p>Use the <code>re.escape()</code> function for this:</p> <p><a href="http://docs.python.org/library/re.html#re.escape">4.2.3 <code>re</code> Module Contents</a></p> <blockquote> <p><strong>escape(string)</strong></p> <p>Return string with all non-alphanumerics backslashed; this is useful if you want to match...
174
2008-11-11T09:37:37Z
[ "python", "regex" ]
Escaping regex string in Python
280,435
<p>I want to use input from a user as a regex pattern for a search over some text. It works, but how I can handle cases where user puts characters that have meaning in regex? For example, the user wants to search for Word <code>(s)</code>: regex engine will take the <code>(s)</code> as a group. I want it to treat it...
128
2008-11-11T09:33:29Z
280,463
<p>You can use <a href="http://docs.python.org/library/re.html#re.escape">re.escape()</a>: </p> <blockquote> <p>re.escape(string) Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.</p> </bl...
33
2008-11-11T09:49:25Z
[ "python", "regex" ]
Calculate poisson probability percentage
280,797
<p>When you use the POISSON function in Excel (or in OpenOffice Calc), it takes two arguments:</p> <ul> <li>an integer</li> <li>an 'average' number</li> </ul> <p>and returns a float.</p> <p>In Python (I tried RandomArray and NumPy) it returns an array of random poisson numbers. What I really want is the percentage t...
16
2008-11-11T12:51:43Z
280,831
<p><a href="http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm#pgfId-305426" rel="nofollow">This page</a> explains why you get an array, and the meaning of the numbers in it, at least.</p>
1
2008-11-11T13:12:40Z
[ "python", "statistics", "poisson" ]
Calculate poisson probability percentage
280,797
<p>When you use the POISSON function in Excel (or in OpenOffice Calc), it takes two arguments:</p> <ul> <li>an integer</li> <li>an 'average' number</li> </ul> <p>and returns a float.</p> <p>In Python (I tried RandomArray and NumPy) it returns an array of random poisson numbers. What I really want is the percentage t...
16
2008-11-11T12:51:43Z
280,843
<p><code>scipy</code> has what you want</p> <pre><code>&gt;&gt;&gt; scipy.stats.distributions &lt;module 'scipy.stats.distributions' from '/home/coventry/lib/python2.5/site-packages/scipy/stats/distributions.pyc'&gt; &gt;&gt;&gt; scipy.stats.distributions.poisson.pmf(6, 2.6) array(0.031867055625524499) </code></pre> ...
20
2008-11-11T13:17:55Z
[ "python", "statistics", "poisson" ]
Calculate poisson probability percentage
280,797
<p>When you use the POISSON function in Excel (or in OpenOffice Calc), it takes two arguments:</p> <ul> <li>an integer</li> <li>an 'average' number</li> </ul> <p>and returns a float.</p> <p>In Python (I tried RandomArray and NumPy) it returns an array of random poisson numbers. What I really want is the percentage t...
16
2008-11-11T12:51:43Z
280,862
<p>It is easy to do by hand, but you can overflow doing it that way. You can do the exponent and factorial in a loop to avoid the overflow:</p> <pre><code>def poisson_probability(actual, mean): # naive: math.exp(-mean) * mean**actual / factorial(actual) # iterative, to keep the components from getting too ...
12
2008-11-11T13:24:40Z
[ "python", "statistics", "poisson" ]
Controlling the mouse from Python in OS X
281,133
<p>What would be the easiest way to move the mouse around (and possibly click) using python on OS X? </p> <p>This is just for rapid prototyping, it doesn't have to be elegant.</p> <p>Thanks!</p>
19
2008-11-11T15:04:43Z
281,330
<p>I dug through the source code of Synergy to find the call that generates mouse events:</p> <pre><code>#include &lt;ApplicationServices/ApplicationServices.h&gt; int to(int x, int y) { CGPoint newloc; CGEventRef eventRef; newloc.x = x; newloc.y = y; eventRef = CGEventCreateMouseEvent(NULL, kCGE...
7
2008-11-11T16:10:11Z
[ "python", "osx", "mouse" ]
Controlling the mouse from Python in OS X
281,133
<p>What would be the easiest way to move the mouse around (and possibly click) using python on OS X? </p> <p>This is just for rapid prototyping, it doesn't have to be elegant.</p> <p>Thanks!</p>
19
2008-11-11T15:04:43Z
281,366
<p>When I wanted to do it, I installed <a href="http://www.jython.org/Project/" rel="nofollow">Jython</a> and used the <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Robot.html" rel="nofollow"><code>java.awt.Robot</code></a> class. If you need to make a CPython script this is obviously not suitable, but...
8
2008-11-11T16:23:20Z
[ "python", "osx", "mouse" ]
Controlling the mouse from Python in OS X
281,133
<p>What would be the easiest way to move the mouse around (and possibly click) using python on OS X? </p> <p>This is just for rapid prototyping, it doesn't have to be elegant.</p> <p>Thanks!</p>
19
2008-11-11T15:04:43Z
292,117
<p>The easiest way? Compile <a href="http://www.macosxhints.com/article.php?story=2008051406323031" rel="nofollow">this</a> Cocoa app and pass it your mouse movements.</p> <p>Another way? Import <a href="http://developer.apple.com/cocoa/pyobjc.html" rel="nofollow">pyobjc</a> to access some of the OSX framework and acc...
0
2008-11-15T03:48:07Z
[ "python", "osx", "mouse" ]
Controlling the mouse from Python in OS X
281,133
<p>What would be the easiest way to move the mouse around (and possibly click) using python on OS X? </p> <p>This is just for rapid prototyping, it doesn't have to be elegant.</p> <p>Thanks!</p>
19
2008-11-11T15:04:43Z
664,417
<p>Just try this code:</p> <pre><code>#!/usr/bin/python import objc class ETMouse(): def setMousePosition(self, x, y): bndl = objc.loadBundle('CoreGraphics', globals(), '/System/Library/Frameworks/ApplicationServices.framework') objc.loadBundleFunctions(bndl, globals(), ...
9
2009-03-19T23:15:07Z
[ "python", "osx", "mouse" ]
Controlling the mouse from Python in OS X
281,133
<p>What would be the easiest way to move the mouse around (and possibly click) using python on OS X? </p> <p>This is just for rapid prototyping, it doesn't have to be elegant.</p> <p>Thanks!</p>
19
2008-11-11T15:04:43Z
8,202,674
<p>Try the code at <a href="http://www.geekorgy.com/index.php/2010/06/python-mouse-click-and-move-mouse-in-apple-mac-osx-snow-leopard-10-6-x/">http://www.geekorgy.com/index.php/2010/06/python-mouse-click-and-move-mouse-in-apple-mac-osx-snow-leopard-10-6-x/</a>, pasted below in case the site disappears.</p> <p>It defin...
19
2011-11-20T15:48:30Z
[ "python", "osx", "mouse" ]
Controlling the mouse from Python in OS X
281,133
<p>What would be the easiest way to move the mouse around (and possibly click) using python on OS X? </p> <p>This is just for rapid prototyping, it doesn't have to be elegant.</p> <p>Thanks!</p>
19
2008-11-11T15:04:43Z
10,696,392
<p>The python script from <a href="http://www.geekorgy.com/index.php/2010/06/python-mouse-click-and-move-mouse-in-apple-mac-osx-snow-leopard-10-6-x/" rel="nofollow">geekorgy.com</a> is great except I ran into a few snags since I installed a newer version of python. So here are some tips to others who may be looking fo...
1
2012-05-22T05:51:10Z
[ "python", "osx", "mouse" ]
Controlling the mouse from Python in OS X
281,133
<p>What would be the easiest way to move the mouse around (and possibly click) using python on OS X? </p> <p>This is just for rapid prototyping, it doesn't have to be elegant.</p> <p>Thanks!</p>
19
2008-11-11T15:04:43Z
17,578,617
<p>Your best bet is to use the <a href="https://pypi.python.org/pypi/autopy/0.51" rel="nofollow">AutoPy package</a>. It's extremely simple to use, and cross-platform to boot.</p> <p>To move the cursor to position (200,200):</p> <pre><code>import autopy autopy.mouse.move(200,200) </code></pre>
1
2013-07-10T18:49:19Z
[ "python", "osx", "mouse" ]
Open explorer on a file
281,888
<p>In Python, how do I jump to a file in the Windows Explorer? I found a solution for jumping to folders:</p> <pre><code>import subprocess subprocess.Popen('explorer "C:\path\of\folder"') </code></pre> <p>but I have no solution for files.</p>
30
2008-11-11T19:24:56Z
281,911
<p>From <a href="http://support.microsoft.com/kb/314853">Explorer.exe Command-Line Options for Windows XP</a></p> <pre><code>import subprocess subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"') </code></pre>
33
2008-11-11T19:33:14Z
[ "python", "windows", "explorer" ]
Open explorer on a file
281,888
<p>In Python, how do I jump to a file in the Windows Explorer? I found a solution for jumping to folders:</p> <pre><code>import subprocess subprocess.Popen('explorer "C:\path\of\folder"') </code></pre> <p>but I have no solution for files.</p>
30
2008-11-11T19:24:56Z
27,251,095
<p>For some reason, on windows 7 it always opens the users Path, for me following worked out:</p> <pre><code>import subprocess subprocess.call("explorer C:\\temp\\yourpath", shell=True) </code></pre>
4
2014-12-02T13:51:51Z
[ "python", "windows", "explorer" ]
How can I do synchronous rpc calls
281,922
<p>I'm building a program that has a class used locally, but I want the same class to be used the same way over the network. This means I need to be able to make synchronous calls to any of its public methods. The class reads and writes files, so I think XML-RPC is too much overhead. I created a basic rpc client/server...
4
2008-11-11T19:38:34Z
281,991
<p>For a synchronous client, Twisted probably isn't the right option. Instead, you might want to use the socket module directly.</p> <pre><code>import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.host, self.port)) s.send(output) data = s.recv(size) s.close() </code></pre> <p>The <code...
1
2008-11-11T20:04:35Z
[ "python", "twisted" ]
How can I do synchronous rpc calls
281,922
<p>I'm building a program that has a class used locally, but I want the same class to be used the same way over the network. This means I need to be able to make synchronous calls to any of its public methods. The class reads and writes files, so I think XML-RPC is too much overhead. I created a basic rpc client/server...
4
2008-11-11T19:38:34Z
282,301
<p>If you are using Twisted you should probably know that:</p> <ol> <li>You will not be making synchronous calls to any network service</li> <li>The reactor can only ever be run once, so do not stop it (by calling <code>reactor.stop()</code>) until your application is ready to exit.</li> </ol> <p>I hope this answers ...
1
2008-11-11T22:07:13Z
[ "python", "twisted" ]
How can I do synchronous rpc calls
281,922
<p>I'm building a program that has a class used locally, but I want the same class to be used the same way over the network. This means I need to be able to make synchronous calls to any of its public methods. The class reads and writes files, so I think XML-RPC is too much overhead. I created a basic rpc client/server...
4
2008-11-11T19:38:34Z
288,650
<p>If you're even considering Pyro, check out <a href="http://rpyc.wikidot.com/" rel="nofollow">RPyC</a> first, and re-consider XML-RPC.</p> <p>Regarding Twisted: try leaving the reactor up instead of stopping it, and just <code>ClientCreator(...).connectTCP(...)</code> each time.</p> <p>If you <code>self.transport.l...
2
2008-11-13T22:53:32Z
[ "python", "twisted" ]
How can I do synchronous rpc calls
281,922
<p>I'm building a program that has a class used locally, but I want the same class to be used the same way over the network. This means I need to be able to make synchronous calls to any of its public methods. The class reads and writes files, so I think XML-RPC is too much overhead. I created a basic rpc client/server...
4
2008-11-11T19:38:34Z
315,881
<p>Why do you feel that it needs to be synchronous?</p> <p>If you want to ensure that only one of these is happening at a time, invoke all of the calls through a DeferredSemaphore so you can rate limit the actual invocations (to any arbitrary value).</p> <p>If you want to be able to run multiple streams of these at d...
2
2008-11-24T23:32:46Z
[ "python", "twisted" ]
Python decorating functions before call
282,393
<p>I have a rather complex decorator written by someone else. What I want to do is call a decorated version of the function one time based on a descision or call the original function (not decorated) another time. Is this possible?</p>
6
2008-11-11T22:42:44Z
282,399
<p>With:</p> <pre><code>decorator(original_function)() </code></pre> <p>Without:</p> <pre><code>original_function() </code></pre> <p>A decorator is just a function which takes a function as an argument and returns another one. The @ syntax is totally optional. Perhaps a sift through some <a href="https://wiki.pytho...
21
2008-11-11T22:45:52Z
[ "python", "decorator" ]
Python decorating functions before call
282,393
<p>I have a rather complex decorator written by someone else. What I want to do is call a decorated version of the function one time based on a descision or call the original function (not decorated) another time. Is this possible?</p>
6
2008-11-11T22:42:44Z
282,816
<pre><code>def original_function(): pass decorated_function= decorator(original_function) if use_decorated: decorated_function() else: original_function() </code></pre> <p>Decorate only once, and afterwards choose which version to call.</p>
2
2008-11-12T02:26:08Z
[ "python", "decorator" ]
Python decorating functions before call
282,393
<p>I have a rather complex decorator written by someone else. What I want to do is call a decorated version of the function one time based on a descision or call the original function (not decorated) another time. Is this possible?</p>
6
2008-11-11T22:42:44Z
812,743
<p>Here is the recipe I came up with for the problem. I also needed to keep the signatures the same so I used the decorator module but you could re-jig to avoid that. Basically, the trick was to add an attribute to the function. The 'original' function is unbound so you need to pass in a 'self' as the first paramete...
1
2009-05-01T18:57:04Z
[ "python", "decorator" ]
How can I download python .egg files, when behind a firewall
282,907
<p>I'm going to try out turbogears however I'm on windows vista. however due to firewall proxy problems, it seems i can't download .egg files which is required for setup turbogears to get installed in my windows environment. I do have a bootable, or I can make a bootable Linux USB, I can try cygwin but I am not sure wh...
1
2008-11-12T03:15:07Z
282,939
<p>Add python to the firewall exceptions list. Just make sure you don't run any questionable code made in python, of course.</p>
1
2008-11-12T03:32:16Z
[ "python", "windows", "linux", "cygwin", "turbogears" ]
How can I download python .egg files, when behind a firewall
282,907
<p>I'm going to try out turbogears however I'm on windows vista. however due to firewall proxy problems, it seems i can't download .egg files which is required for setup turbogears to get installed in my windows environment. I do have a bootable, or I can make a bootable Linux USB, I can try cygwin but I am not sure wh...
1
2008-11-12T03:15:07Z
283,174
<p>You can run TG <a href="http://docs.turbogears.org/1.0/InstallWindows" rel="nofollow">locally from windows</a>. The <code>tgsetup.py</code> method of installation uses <a href="http://pypi.python.org/pypi/setuptools/0.6c9" rel="nofollow">setuptools</a> which depends on being able to bring in <code>.egg</code> files ...
3
2008-11-12T06:32:48Z
[ "python", "windows", "linux", "cygwin", "turbogears" ]
How can I download python .egg files, when behind a firewall
282,907
<p>I'm going to try out turbogears however I'm on windows vista. however due to firewall proxy problems, it seems i can't download .egg files which is required for setup turbogears to get installed in my windows environment. I do have a bootable, or I can make a bootable Linux USB, I can try cygwin but I am not sure wh...
1
2008-11-12T03:15:07Z
283,204
<p>Perhaps the problem is not with the firewall per se, but with the fact that you need to use an HTTP proxy. If you do need to use a proxy, try setting the http_proxy environment variable. It might be that your firewall uses NTLM proxy authentication (which Python doesn't support); in this case, try setting up an <a h...
4
2008-11-12T06:56:14Z
[ "python", "windows", "linux", "cygwin", "turbogears" ]
How can I download python .egg files, when behind a firewall
282,907
<p>I'm going to try out turbogears however I'm on windows vista. however due to firewall proxy problems, it seems i can't download .egg files which is required for setup turbogears to get installed in my windows environment. I do have a bootable, or I can make a bootable Linux USB, I can try cygwin but I am not sure wh...
1
2008-11-12T03:15:07Z
283,271
<p>You could use the old firewall hack... try throwing "?file.jpg" or "#file.jpg" on the end (sans quotes). The firewall may see this as you're trying to download an image file which it'll allow, the responding server probably won't care that you've attached a query string, and (I think) python will just see an egg.</p...
2
2008-11-12T07:52:24Z
[ "python", "windows", "linux", "cygwin", "turbogears" ]
How can I download python .egg files, when behind a firewall
282,907
<p>I'm going to try out turbogears however I'm on windows vista. however due to firewall proxy problems, it seems i can't download .egg files which is required for setup turbogears to get installed in my windows environment. I do have a bootable, or I can make a bootable Linux USB, I can try cygwin but I am not sure wh...
1
2008-11-12T03:15:07Z
285,270
<p>This might not be what you are looking for, but you can <a href="http://www.mtu.net/~engstrom/ssh-proxy.php" rel="nofollow">bypass the proxy tunneling SSH</a>. Another possibility is using <a href="http://tor.eff.org" rel="nofollow">Tor</a>.</p>
0
2008-11-12T20:40:16Z
[ "python", "windows", "linux", "cygwin", "turbogears" ]
Django debugging with Emacs
283,294
<p>I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?</p>
20
2008-11-12T08:15:26Z
284,058
<p>I don't really know anything about it, but putting "debugging Python with emacs" into Google gave me this page about <a href="http://twistedmatrix.com/projects/core/documentation/howto/debug-with-emacs.html" rel="nofollow">debugging twisted with emacs</a>, so it seems to be possible.</p>
0
2008-11-12T14:16:45Z
[ "python", "django", "debugging", "emacs" ]
Django debugging with Emacs
283,294
<p>I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?</p>
20
2008-11-12T08:15:26Z
284,607
<p>This isn't emacs specific, but you can use the Python debugger by adding the following to a Django view function:</p> <p><code>import pdb; pdb.set_trace()</code></p> <p>Now when you run the development server and view the page, your browser will appear to hang or load very slowly - switch over to your console, and...
17
2008-11-12T16:57:37Z
[ "python", "django", "debugging", "emacs" ]
Django debugging with Emacs
283,294
<p>I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?</p>
20
2008-11-12T08:15:26Z
286,703
<p>Here's something I found last night that will do exactly what you want when the program crashes:</p> <p><a href="http://code.google.com/p/django-command-extensions/" rel="nofollow">http://code.google.com/p/django-command-extensions/</a></p> <p>Once you install that you can run:</p> <blockquote> <p>python manage...
3
2008-11-13T10:01:41Z
[ "python", "django", "debugging", "emacs" ]
Django debugging with Emacs
283,294
<p>I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?</p>
20
2008-11-12T08:15:26Z
1,665,783
<p>Start pdb like this:</p> <p><kbd>M-x</kbd> <code>pdb</code></p> <p>Then, start the Django development server:</p> <pre><code>python manage.py runserver --noreload </code></pre> <p>Once you have the (Pdb) prompt, you need to do this:</p> <pre><code>import sys sys.path.append('/path/to/directory/containing/views....
13
2009-11-03T07:33:57Z
[ "python", "django", "debugging", "emacs" ]
Django debugging with Emacs
283,294
<p>I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?</p>
20
2008-11-12T08:15:26Z
7,398,616
<p>About the general non-emacs-exclusive way, there is a very nice screencast out there you might be interested in: <a href="http://ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-debugging-series-/" rel="nofollow">http://ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-debugging-se...
0
2011-09-13T08:02:12Z
[ "python", "django", "debugging", "emacs" ]
Django debugging with Emacs
283,294
<p>I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?</p>
20
2008-11-12T08:15:26Z
7,418,949
<p>Because recent versions of Emacs python mode do support 'pdbtrack' functionality by default, all you need is just set up breakpoint in your code this way:</p> <pre><code>import pdb; pdb.set_trace() </code></pre> <p>Also, you have to start your Django application devserver from within Emacs shell: </p> <p><kbd>M-...
1
2011-09-14T15:27:25Z
[ "python", "django", "debugging", "emacs" ]
Why would an "command not recognized" error occur only when a window is populated?
283,431
<p>My record sheet app has a menu option for creating a new, blank record sheet. When I open a sheet window, I can open new windows without a problem, using subprocess.Popen() to do it.</p> <p>However, under Windows (I haven't tested it on other OSes yet), if I open a new window then use the "open file" dialog to popu...
1
2008-11-12T09:38:53Z
283,545
<p>From the error message, it looks like you need to pass the full path of "foo.py" to your Popen call. Normally just having "foo.py" will search in your current working directory, but this can be a bit unpredictable on Windows, I have found. Yours seems to be jumping around with the open file dialog.</p> <p>Secondly,...
4
2008-11-12T10:36:43Z
[ "python", "windows" ]
Why would an "command not recognized" error occur only when a window is populated?
283,431
<p>My record sheet app has a menu option for creating a new, blank record sheet. When I open a sheet window, I can open new windows without a problem, using subprocess.Popen() to do it.</p> <p>However, under Windows (I haven't tested it on other OSes yet), if I open a new window then use the "open file" dialog to popu...
1
2008-11-12T09:38:53Z
286,436
<p>The suggested answer seems to have fixed the problem. I also realized that I needed to use <strong>os.name</strong> to determine which OS is being used, then I can use the correct path format for loading the external Python file.</p>
0
2008-11-13T07:27:55Z
[ "python", "windows" ]
Is it possible to use wxPython inside IronPython?
283,447
<p>When my IronPython program gets to the line </p> <pre><code>import wx </code></pre> <p>I get this message:</p> <pre><code>A first chance exception of type 'IronPython.Runtime.Exceptions.PythonImportErrorException' occurred in IronPython.dll Additional information: No module named _core_ </code></pre> <p>althou...
4
2008-11-12T09:49:43Z
283,520
<p>No, this won't work. Wx bindings (like most other "python bindings") are actually compiled against CPython.</p> <p>In this regards they are not just packages on sys.path to be found, as you have tried. They actually depend on CPython itself. <a href="http://www.python.org/doc/2.5.2/ext/ext.html" rel="nofollow">This...
8
2008-11-12T10:27:31Z
[ "python", "wxpython", "ironpython" ]
Is it possible to use wxPython inside IronPython?
283,447
<p>When my IronPython program gets to the line </p> <pre><code>import wx </code></pre> <p>I get this message:</p> <pre><code>A first chance exception of type 'IronPython.Runtime.Exceptions.PythonImportErrorException' occurred in IronPython.dll Additional information: No module named _core_ </code></pre> <p>althou...
4
2008-11-12T09:49:43Z
284,800
<p>While wxPython is unavailable for the reasons listed by @Ali, you may want to take a look at <a href="http://wxnet.sourceforge.net/">wx.NET</a>. You could use IronPython to call these assemblies instead, and it should be cross-platform (I'm assuming that's what you're after, or you would just use WinForms). If all y...
5
2008-11-12T18:12:13Z
[ "python", "wxpython", "ironpython" ]
python list in sql query as parameter
283,645
<p>I have a python list, say l</p> <pre><code>l = [1,5,8] </code></pre> <p>I want to write a sql query to get the data for all the elements of the list, say</p> <p>"select name from studens where id = |IN THE LIST l|"</p> <p>How do i accomlish this?</p>
45
2008-11-12T11:18:32Z
283,706
<p><a href="http://docs.python.org/library/stdtypes.html#str.join">string.join</a> the list values separated by commas, and use the <a href="http://docs.python.org/library/stdtypes.html#string-formatting">format operator</a> to form a query string.</p> <pre><code>myquery = "select name from studens where id in (%s)" %...
5
2008-11-12T11:49:37Z
[ "python", "sql" ]
python list in sql query as parameter
283,645
<p>I have a python list, say l</p> <pre><code>l = [1,5,8] </code></pre> <p>I want to write a sql query to get the data for all the elements of the list, say</p> <p>"select name from studens where id = |IN THE LIST l|"</p> <p>How do i accomlish this?</p>
45
2008-11-12T11:18:32Z
283,713
<p>The SQL you want is</p> <pre><code>select name from studens where id in (1, 5, 8) </code></pre> <p>If you want to construct this from the python you could use</p> <pre><code>l = [1, 5, 8] sql_query = 'select name from studens where id in (' + ','.join(map(str, l)) + ')' </code></pre> <p>The <a href="http://docs....
12
2008-11-12T11:52:22Z
[ "python", "sql" ]
python list in sql query as parameter
283,645
<p>I have a python list, say l</p> <pre><code>l = [1,5,8] </code></pre> <p>I want to write a sql query to get the data for all the elements of the list, say</p> <p>"select name from studens where id = |IN THE LIST l|"</p> <p>How do i accomlish this?</p>
45
2008-11-12T11:18:32Z
283,801
<p>Answers so far have been templating the values into a plain SQL string. That's absolutely fine for integers, but if we wanted to do it for strings we get the escaping issue.</p> <p>Here's a variant using a parameterised query that would work for both:</p> <pre><code>placeholder= '?' # For SQLite. See DBAPI paramst...
51
2008-11-12T12:30:10Z
[ "python", "sql" ]
python list in sql query as parameter
283,645
<p>I have a python list, say l</p> <pre><code>l = [1,5,8] </code></pre> <p>I want to write a sql query to get the data for all the elements of the list, say</p> <p>"select name from studens where id = |IN THE LIST l|"</p> <p>How do i accomlish this?</p>
45
2008-11-12T11:18:32Z
4,233,213
<p>I like bobince's answer:</p> <pre><code>placeholder= '?' # For SQLite. See DBAPI paramstyle. placeholders= ', '.join(placeholder for unused in l) query= 'SELECT name FROM students WHERE id IN (%s)' % placeholders cursor.execute(query, l) </code></pre> <p>But I noticed this:</p> <pre><code>placeholders= ', '.join(...
2
2010-11-20T14:35:13Z
[ "python", "sql" ]
python list in sql query as parameter
283,645
<p>I have a python list, say l</p> <pre><code>l = [1,5,8] </code></pre> <p>I want to write a sql query to get the data for all the elements of the list, say</p> <p>"select name from studens where id = |IN THE LIST l|"</p> <p>How do i accomlish this?</p>
45
2008-11-12T11:18:32Z
27,145,241
<p>Solution for @umounted answer, because that broke with a one-element tuple, since (1,) is not valid SQL.:</p> <pre><code>&gt;&gt;&gt; random_ids = [1234,123,54,56,57,58,78,91] &gt;&gt;&gt; cursor.execute("create table test (id)") &gt;&gt;&gt; for item in random_ids: cursor.execute("insert into test values (%d)"...
-1
2014-11-26T09:16:45Z
[ "python", "sql" ]
python list in sql query as parameter
283,645
<p>I have a python list, say l</p> <pre><code>l = [1,5,8] </code></pre> <p>I want to write a sql query to get the data for all the elements of the list, say</p> <p>"select name from studens where id = |IN THE LIST l|"</p> <p>How do i accomlish this?</p>
45
2008-11-12T11:18:32Z
39,024,751
<p>For example, if you want the sql query:</p> <pre><code>select name from studens where id in (1, 5, 8) </code></pre> <p>What about:</p> <pre><code>my_list = [1, 5, 8] cur.execute("select name from studens where id in %s" % repr(my_list).replace('[','(').replace(']',')') ) </code></pre>
0
2016-08-18T18:02:23Z
[ "python", "sql" ]
Size of an open file object
283,707
<p>Is there a way to find the size of a file object that is currently open?</p> <p>Specifically, I am working with the tarfile module to create tarfiles, but I don't want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like objects, so I imagine a generic solution would work.</p>
33
2008-11-12T11:49:46Z
283,718
<p>If you have the file descriptor, you can use <code>fstat</code> to find out the size, if any. A more generic solution is to seek to the end of the file, and read its location there.</p>
4
2008-11-12T11:55:27Z
[ "python" ]
Size of an open file object
283,707
<p>Is there a way to find the size of a file object that is currently open?</p> <p>Specifically, I am working with the tarfile module to create tarfiles, but I don't want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like objects, so I imagine a generic solution would work.</p>
33
2008-11-12T11:49:46Z
283,719
<pre><code>$ ls -la chardet-1.0.1.tgz -rwxr-xr-x 1 vinko vinko 179218 2008-10-20 17:49 chardet-1.0.1.tgz $ python Python 2.5.1 (r251:54863, Jul 31 2008, 22:53:39) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; f = open('chardet-1.0.1.tgz...
54
2008-11-12T11:55:30Z
[ "python" ]
Size of an open file object
283,707
<p>Is there a way to find the size of a file object that is currently open?</p> <p>Specifically, I am working with the tarfile module to create tarfiles, but I don't want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like objects, so I imagine a generic solution would work.</p>
33
2008-11-12T11:49:46Z
283,725
<p>Well, if the file object support the tell method, you can do:</p> <pre><code>current_size = f.tell() </code></pre> <p>That will tell you were it is currently writing. If you write in a sequential way this will be the size of the file.</p> <p>Otherwise, you can use the file system capabilities, i.e. <code>os.fstat...
7
2008-11-12T11:59:36Z
[ "python" ]
Size of an open file object
283,707
<p>Is there a way to find the size of a file object that is currently open?</p> <p>Specifically, I am working with the tarfile module to create tarfiles, but I don't want my tarfile to exceed a certain size. As far as I know, tarfile objects are file-like objects, so I imagine a generic solution would work.</p>
33
2008-11-12T11:49:46Z
38,992,375
<p>Another solution is using StringIO "if you are doing in-memory operations".</p> <pre><code>with open(file_path, 'rb') as x: body = StringIO() body.write(x.read()) body.seek(0, 0) </code></pre> <p>Now <code>body</code> behaves like a file object with various attributes like <code>body.read()</code>. </p...
1
2016-08-17T09:07:29Z
[ "python" ]
Pickled file won't load on Mac/Linux
283,766
<p>I have an application that imports data from a pickled file. It works just fine in Windows but Mac and Linux behaviour is odd.</p> <p>In OS X, the pickled file (file extension ".char") is unavailable as a selection unless I set the file type to *.*. Then, if I select a file that has the .char extension, it won't lo...
4
2008-11-12T12:15:52Z
283,802
<p>Probably you didn't open the file in binary mode when writing and/or reading the pickled data. In this case newline format conversion will occur, which can break the binary data.</p> <p>To open a file in binary mode you have to provide "b" as part of the mode string:</p> <pre><code>char_file = open('pickle.char', ...
9
2008-11-12T12:30:35Z
[ "python", "linux", "osx", "cross-platform" ]
Pickled file won't load on Mac/Linux
283,766
<p>I have an application that imports data from a pickled file. It works just fine in Windows but Mac and Linux behaviour is odd.</p> <p>In OS X, the pickled file (file extension ".char") is unavailable as a selection unless I set the file type to *.*. Then, if I select a file that has the .char extension, it won't lo...
4
2008-11-12T12:15:52Z
283,854
<p>As mentioned by <a href="http://stackoverflow.com/questions/283766/pickled-file-wont-load-on-maclinux#283802">Adam</a>, the problem is likely to be the newline format of the pickle file.</p> <p>Unfortunately, the real problem is actually caused on <em>save</em> rather than load. This may be recoverable if you're u...
6
2008-11-12T12:57:32Z
[ "python", "linux", "osx", "cross-platform" ]
Pickled file won't load on Mac/Linux
283,766
<p>I have an application that imports data from a pickled file. It works just fine in Windows but Mac and Linux behaviour is odd.</p> <p>In OS X, the pickled file (file extension ".char") is unavailable as a selection unless I set the file type to *.*. Then, if I select a file that has the .char extension, it won't lo...
4
2008-11-12T12:15:52Z
284,357
<pre><code>self.file = open(self.file_name, 'w')</code></pre> <p>Should be:</p> <pre><code>self.file = open(self.file_name, 'wb')</code></pre> <p>In your <code>createSaveFile</code> function, to save the file in binary mode (rather than text mode). You should also make sure you open the file in binary mode as well ...
2
2008-11-12T15:46:22Z
[ "python", "linux", "osx", "cross-platform" ]
Pickled file won't load on Mac/Linux
283,766
<p>I have an application that imports data from a pickled file. It works just fine in Windows but Mac and Linux behaviour is odd.</p> <p>In OS X, the pickled file (file extension ".char") is unavailable as a selection unless I set the file type to *.*. Then, if I select a file that has the .char extension, it won't lo...
4
2008-11-12T12:15:52Z
9,357,720
<p>Another way to get this error is to forget to close the output file after pickling. This can leave an incomplete file that fails in random ways during subsequent unpickling.</p>
4
2012-02-20T07:45:18Z
[ "python", "linux", "osx", "cross-platform" ]
Pickled file won't load on Mac/Linux
283,766
<p>I have an application that imports data from a pickled file. It works just fine in Windows but Mac and Linux behaviour is odd.</p> <p>In OS X, the pickled file (file extension ".char") is unavailable as a selection unless I set the file type to *.*. Then, if I select a file that has the .char extension, it won't lo...
4
2008-11-12T12:15:52Z
24,923,596
<p>Use dos2unix tool</p> <pre><code>dos2unix pickle.char </code></pre>
0
2014-07-24T01:12:01Z
[ "python", "linux", "osx", "cross-platform" ]
How to use Popen in Windows to invoke an external .py script and wait for its completion
283,894
<p>Have you ever tried this feedback calling an external zip.py script to work? My CGITB does not show any error messages. It simply did not invoke external .py script to work. It simply skipped over to gush. I should be grateful if you can assist me in making this zip.py callable in feedback.py. </p> <p>Regards. Da...
1
2008-11-12T13:13:31Z
284,199
<ul> <li><p><code>zip()</code> is a built-in function in Python. Therefore it is a bad practice to use <code>zip</code> as a variable name. <code>zip_</code> can be used instead of.</p></li> <li><p><code>execfile()</code> function reads and executes a Python script.</p></li> <li><p>It is probably that you actually need...
1
2008-11-12T14:58:20Z
[ "python", "windows" ]
How to use Popen in Windows to invoke an external .py script and wait for its completion
283,894
<p>Have you ever tried this feedback calling an external zip.py script to work? My CGITB does not show any error messages. It simply did not invoke external .py script to work. It simply skipped over to gush. I should be grateful if you can assist me in making this zip.py callable in feedback.py. </p> <p>Regards. Da...
1
2008-11-12T13:13:31Z
284,448
<p>Yay ArcGIS.</p> <p>Just to clarify how are you trying to call this script using popen, can you post some code?</p> <p>If your invoking this script via another script in the ArcGIS environment, then the thing is, when you use Popen the script wont be invoked within the ArcGIS environment, instead it will be invoked...
0
2008-11-12T16:15:35Z
[ "python", "windows" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
284,110
<p>You can use simple print statements, or any other way of writing to stdout. You can also invoke the Python debugger anywhere in your tests.</p> <p>If you use <a href="https://web.archive.org/web/20081120065052/http://www.somethingaboutorange.com/mrl/projects/nose" rel="nofollow">nose</a> to run your tests (which I...
21
2008-11-12T14:33:31Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
284,192
<p>I don't think this is quite what your looking for, there's no way to display variable values that don't fail, but this may help you get closer to outputting the results the way you want.</p> <p>You can use the <strong><a href="http://docs.python.org/library/unittest.html#id3">TestResult object</a></strong> returned...
14
2008-11-12T14:56:25Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
284,283
<p>How about catching the exception that gets generated from the assertion failure? In your catch block you could output the data however you wanted to wherever. Then when you were done you could re-throw the exception. The test runner probably wouldn't know the difference. </p> <p>Disclaimer: I haven't tried this ...
0
2008-11-12T15:25:57Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
284,326
<p>We use the logging module for this.</p> <p>For example:</p> <pre><code>import logging class SomeTest( unittest.TestCase ): def testSomething( self ): log= logging.getLogger( "SomeTest.testSomething" ) log.debug( "this= %r", self.this ) log.debug( "that= %r", self.that ) # etc. ...
51
2008-11-12T15:38:03Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
284,706
<p>I think I might have been overthinking this. One way I've come up with that does the job, is simply to have a global variable, that accumulates the diagnostic data.</p> <p>Somthing like this:</p> <pre><code>log1 = dict() class TestBar(unittest.TestCase): def runTest(self): for t1, t2 in testdata: ...
6
2008-11-12T17:38:52Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
288,568
<p>Another option - start a debugger where the test fails.</p> <p>Try running your tests with Testoob (it will run your unittest suite without changes), and you can use the '--debug' command line switch to open a debugger when a test fails.</p> <p>Here's a terminal session on windows:</p> <pre><code>C:\work&gt; test...
4
2008-11-13T22:28:25Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
13,688,397
<p>Very late answer for someone that, like me, comes here looking for a simple and quick answer.</p> <p>In Python 2.7 you could use an additional parameter <code>msg</code> to add information to the error message like this:</p> <pre><code>self.assertEqual(f.bar(t2), 2, msg='{0}, {1}'.format(t1, t2)) </code></pre> <p...
41
2012-12-03T17:17:16Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
18,791,576
<p>inspect.trace will let you get local variables after an exception has been thrown. You can then wrap the unit tests with a decorator like the following one to save off those local variables for examination during the post mortem.</p> <pre><code>import random import unittest import inspect def store_result(f): ...
1
2013-09-13T17:01:36Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
19,538,362
<p>Expanding @F.C. 's answer, this works quite well for me:</p> <pre><code>class MyTest(unittest.TestCase): def messenger(self, message): try: self.assertEqual(1, 2, msg=message) except AssertionError as e: print "\nMESSENGER OUTPUT: %s" % str(e), </code></pre>
0
2013-10-23T09:50:38Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
22,187,982
<p>The method I use is really simple. I just log it as a warning so it will actually show up. </p> <pre><code>import logging class TestBar(unittest.TestCase): def runTest(self): #this line is important logging.basicConfig() log = logging.getLogger("LOG") for t1, t2 in testdata: ...
4
2014-03-05T03:34:54Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
28,546,167
<p>Use logging:</p> <pre><code>import unittest import logging import inspect import os logging_level = logging.INFO try: log_file = os.environ["LOG_FILE"] except KeyError: log_file = None def logger(stack=None): if not hasattr(logger, "initialized"): logging.basicConfig(filename=log_file, level=...
2
2015-02-16T16:38:21Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
30,038,630
<p>You can use <code>logging</code> module for that.</p> <p>So in the unit test code, use:</p> <pre><code>import logging as log def test_foo(self): log.debug("Some debug message.") log.info("Some info message.") log.warning("Some warning message.") log.error("Some error message.") </code></pre> <p>B...
1
2015-05-04T19:42:27Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
32,245,657
<p>Admitting that I haven't tried it, the <a href="https://pythonhosted.org/testfixtures/logging.html" rel="nofollow">testfixtures' logging feature</a> looks quite useful...</p>
0
2015-08-27T09:32:26Z
[ "python", "unit-testing" ]
Outputting data from unit test in python
284,043
<p>If I'm writing unit tests in python (using the unittest module), is it possible to output data from a failed test, so I can examine it to help deduce what caused the error? I am aware of the ability to create a customized message, which can carry some information, but sometimes you might deal with more complex data,...
76
2008-11-12T14:10:27Z
36,178,137
<p>What I do in these cases is to have a <code>log.debug()</code> with some messages in my application. Since the default logging level is <code>WARNING</code>, such messages don't show in the normal execution.</p> <p>Then, in the unittest I change the logging level to <code>DEBUG</code>, so that such messages are sho...
2
2016-03-23T12:24:52Z
[ "python", "unit-testing" ]
Cross platform hidden file detection
284,115
<p>What is the best way to do cross-platform handling of hidden files? (preferably in Python, but other solutions still appreciated)</p> <p>Simply checking for a leading '.' works for *nix/Mac, and file attributes work on Windows. However, this seems a little simplistic, and also doesn't account for alternative method...
13
2008-11-12T14:35:29Z
284,363
<p>"Is there a standard way to deal with this?" Yes. Use a standard (i.e., POSIX-compliant) OS.</p> <p>Since Windows is non-standard -- well -- there's no applicable standard. Wouldn't it be great if there was? I feel your pain.</p> <p>Anything you try to do that's cross-platform like that will have Win32 odditie...
-3
2008-11-12T15:47:54Z
[ "python", "cross-platform", "filesystems" ]
Cross platform hidden file detection
284,115
<p>What is the best way to do cross-platform handling of hidden files? (preferably in Python, but other solutions still appreciated)</p> <p>Simply checking for a leading '.' works for *nix/Mac, and file attributes work on Windows. However, this seems a little simplistic, and also doesn't account for alternative method...
13
2008-11-12T14:35:29Z
284,439
<p>We actually address this in a project we write. What we do is have a number of different "hidden file checkers" that are registered with a main checker. We pass each file through these to see if it should be hidden or not.</p> <p>These checkers are not only for different OS's etc, but we plug into version control "...
1
2008-11-12T16:11:11Z
[ "python", "cross-platform", "filesystems" ]
Cross platform hidden file detection
284,115
<p>What is the best way to do cross-platform handling of hidden files? (preferably in Python, but other solutions still appreciated)</p> <p>Simply checking for a leading '.' works for *nix/Mac, and file attributes work on Windows. However, this seems a little simplistic, and also doesn't account for alternative method...
13
2008-11-12T14:35:29Z
6,365,265
<p>Here's a script that runs on Python 2.5+ and should do what you're looking for:</p> <pre><code>import ctypes import os def is_hidden(filepath): name = os.path.basename(os.path.abspath(filepath)) return name.startswith('.') or has_hidden_attribute(filepath) def has_hidden_attribute(filepath): try: ...
14
2011-06-15T22:37:28Z
[ "python", "cross-platform", "filesystems" ]
Cross platform hidden file detection
284,115
<p>What is the best way to do cross-platform handling of hidden files? (preferably in Python, but other solutions still appreciated)</p> <p>Simply checking for a leading '.' works for *nix/Mac, and file attributes work on Windows. However, this seems a little simplistic, and also doesn't account for alternative method...
13
2008-11-12T14:35:29Z
15,236,292
<p>Jason R. Coombs's answer is sufficient for Windows. And most POSIX GUI file managers/open dialogs/etc. probably follow the same "dot-prefix-means-hidden" convention as <code>ls</code>. But not Mac OS X.</p> <p>There are at least four ways a file or directory can be hidden in Finder, file open panels, etc.:</p> <ul...
7
2013-03-05T23:42:04Z
[ "python", "cross-platform", "filesystems" ]