title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
wxPython: Good way to overlay a wx.Panel on an existing wx.Panel
1,032,138
<p>I have a wx.Frame, in which there is a main wx.Panel with several widgets inside of it. I want one button in there to cause a "help panel" to come up. This help panel would probably be a wx.Panel, and I want it to overlay the entire main wx.Panel (not including the menu bar of the wx.Frame). There should be some sor...
4
2009-06-23T12:04:54Z
1,032,282
<p>There are several ways</p> <p>a) you can create a custom child panel, and make it same size and position at 0,0 among top of all child widgets. no need of destroying it just Show/Hide it this also resizes with parent frame</p> <p>b) popup a wx.PopupWindow or derived class and place and size it at correct location<...
10
2009-06-23T12:36:25Z
[ "python", "user-interface", "wxpython" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,335
<p>Note that this actually is a bruteforce:</p> <pre><code>l = a.values() b = [x for x in a if l.count(a[x]) == 1] </code></pre>
5
2009-06-23T12:43:40Z
[ "python", "dictionary" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,429
<p>You could do something like this (just count the number of occurrences for each value):</p> <pre><code>def unique(a): from collections import defaultdict count = defaultdict(lambda: 0) for k, v in a.iteritems(): count[v] += 1 for v, c in count.iteritems(): if c &lt;= 1: y...
-1
2009-06-23T12:59:49Z
[ "python", "dictionary" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,444
<p>I think efficient way if dict is too large would be</p> <pre><code>countMap = {} for v in a.itervalues(): countMap[v] = countMap.get(v,0) + 1 uni = [ k for k, v in a.iteritems() if countMap[v] == 1] </code></pre>
12
2009-06-23T13:02:44Z
[ "python", "dictionary" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,491
<pre><code>&gt;&gt;&gt; b = [] &gt;&gt;&gt; import collections &gt;&gt;&gt; bag = collections.defaultdict(lambda: 0) &gt;&gt;&gt; for v in a.itervalues(): ... bag[v] += 1 ... &gt;&gt;&gt; b = [k for (k, v) in a.iteritems() if bag[v] == 1] &gt;&gt;&gt; b.sort() # optional &gt;&gt;&gt; print b ['dog', 'snake'] &gt;&g...
4
2009-06-23T13:10:45Z
[ "python", "dictionary" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,572
<p>Use nested list comprehensions!</p> <pre><code>print [v[0] for v in dict([(v, [k for k in a.keys() if a[k] == v]) for v in set(a.values())]).values() if len(v) == 1] </code></pre>
-2
2009-06-23T13:26:10Z
[ "python", "dictionary" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,575
<p>Here's another variation.</p> <pre><code>&gt;&gt;&gt; import collections &gt;&gt;&gt; inverse= collections.defaultdict(list) &gt;&gt;&gt; for k,v in a.items(): ... inverse[v].append(k) ... &gt;&gt;&gt; [ v[0] for v in inverse.values() if len(v) == 1 ] ['dog', 'snake'] </code></pre> <p>I'm partial to this beca...
0
2009-06-23T13:27:12Z
[ "python", "dictionary" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,583
<p>Here is a solution that only requires traversing the dict once:</p> <pre><code>def unique_values(d): seen = {} # dict (value, key) result = set() # keys with unique values for k,v in d.iteritems(): if v in seen: result.discard(seen[v]) else: seen[v] = k ...
5
2009-06-23T13:29:07Z
[ "python", "dictionary" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,615
<p>A little more verbose, but does need only one pass over a:</p> <pre><code>revDict = {} for k, v in a.iteritems(): if v in revDict: revDict[v] = None else: revDict[v] = k [ x for x in revDict.itervalues() if x != None ] </code></pre> <p>( I hope it works, since I can't test it here )</p>
2
2009-06-23T13:33:54Z
[ "python", "dictionary" ]
Python: finding keys with unique values in a dictionary?
1,032,281
<p>I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.</p> <p>I will clarify with an example. Say my input is dictionary a, constructed as follows:</p> <pre><code>a = dict() a['cat'] = 1 a['fish'] = 1 a['dog'] = 2...
14
2009-06-23T12:35:47Z
1,032,951
<p>What about subclassing? </p> <pre><code>class UniqueValuesDict(dict): def __init__(self, *args): dict.__init__(self, *args) self._inverse = {} def __setitem__(self, key, value): if value in self.values(): if value in self._inverse: del self._inverse[valu...
2
2009-06-23T14:35:02Z
[ "python", "dictionary" ]
Python Formatter Tool
1,032,393
<p>I was wondering if there exists a sort of Python beautifier like the gnu-indent command line tool for C code. Of course indentation is not the point in Python since it is programmer's responsibility but I wish to get my code written in a perfectly homogenous way, taking care particularly of having always identical b...
24
2009-06-23T12:53:52Z
1,032,414
<p><a href="http://freshmeat.net/projects/pylint/" rel="nofollow">PyLint</a> has some formatting checks.</p>
1
2009-06-23T12:57:13Z
[ "python", "formatter" ]
Python Formatter Tool
1,032,393
<p>I was wondering if there exists a sort of Python beautifier like the gnu-indent command line tool for C code. Of course indentation is not the point in Python since it is programmer's responsibility but I wish to get my code written in a perfectly homogenous way, taking care particularly of having always identical b...
24
2009-06-23T12:53:52Z
1,032,415
<p>Have you looked at <a href="http://www.koders.com/python/fid66946EDA1E4704E4DE82C3385E16309B10C0C683.aspx?s=md5" rel="nofollow">pindent</a>?</p>
1
2009-06-23T12:57:14Z
[ "python", "formatter" ]
Python Formatter Tool
1,032,393
<p>I was wondering if there exists a sort of Python beautifier like the gnu-indent command line tool for C code. Of course indentation is not the point in Python since it is programmer's responsibility but I wish to get my code written in a perfectly homogenous way, taking care particularly of having always identical b...
24
2009-06-23T12:53:52Z
1,502,993
<p>I am the one who asks the question. In fact, the tool the closest to my needs seems to be <a href="http://www.lacusveris.com/PythonTidy/">PythonTidy</a> (it's a Python program of course : Python is best served by himself ;) ).</p>
14
2009-10-01T09:55:50Z
[ "python", "formatter" ]
Python Formatter Tool
1,032,393
<p>I was wondering if there exists a sort of Python beautifier like the gnu-indent command line tool for C code. Of course indentation is not the point in Python since it is programmer's responsibility but I wish to get my code written in a perfectly homogenous way, taking care particularly of having always identical b...
24
2009-06-23T12:53:52Z
26,106,417
<p>autopep8 attempts to automate making your code conform to pep8 coding standards</p> <p><a href="https://pypi.python.org/pypi/autopep8" rel="nofollow">https://pypi.python.org/pypi/autopep8</a></p>
2
2014-09-29T18:26:24Z
[ "python", "formatter" ]
Check if only one variable in a list of variables is set
1,032,411
<p>I'm looking for a simple method to check if only one variable in a list of variables has a True value. I've looked at this <a href="http://stackoverflow.com/questions/432842/how-do-you-get-the-logical-xor-of-two-variables-in-python">logical xor post</a> and is trying to find a way to adapt to multiple variables and ...
9
2009-06-23T12:56:22Z
1,032,441
<pre><code>&gt;&gt;&gt; def f(*n): ... n = [bool(i) for i in n] ... return n.count(True) == 1 ... &gt;&gt;&gt; f(0, 0, 0) False &gt;&gt;&gt; f(1, 0, 0) True &gt;&gt;&gt; f(1, 0, 1) False &gt;&gt;&gt; f(1, 1, 1) False &gt;&gt;&gt; f(0, 1, 0) True &gt;&gt;&gt; </code></pre>
3
2009-06-23T13:02:33Z
[ "python", "xor" ]
Check if only one variable in a list of variables is set
1,032,411
<p>I'm looking for a simple method to check if only one variable in a list of variables has a True value. I've looked at this <a href="http://stackoverflow.com/questions/432842/how-do-you-get-the-logical-xor-of-two-variables-in-python">logical xor post</a> and is trying to find a way to adapt to multiple variables and ...
9
2009-06-23T12:56:22Z
1,032,442
<p>There isn't one built in but it's not to hard to roll you own:</p> <pre><code>def TrueXor(*args): return sum(args) == 1 </code></pre> <p>Since "[b]ooleans are a subtype of plain integers" (<a href="http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex">source</a>) you can sum the li...
15
2009-06-23T13:02:34Z
[ "python", "xor" ]
Check if only one variable in a list of variables is set
1,032,411
<p>I'm looking for a simple method to check if only one variable in a list of variables has a True value. I've looked at this <a href="http://stackoverflow.com/questions/432842/how-do-you-get-the-logical-xor-of-two-variables-in-python">logical xor post</a> and is trying to find a way to adapt to multiple variables and ...
9
2009-06-23T12:56:22Z
1,032,477
<p>The question you linked to already provides the solution for two variables. All you have to do is extend it to work on n variables:</p> <pre><code>import operator def only_one_set(*vars): bools = [bool(v) for v in vars] return reduce(operator.xor, bools, False) &gt;&gt;&gt; a, b, c, d, e = False, '', [], ...
1
2009-06-23T13:08:36Z
[ "python", "xor" ]
Check if only one variable in a list of variables is set
1,032,411
<p>I'm looking for a simple method to check if only one variable in a list of variables has a True value. I've looked at this <a href="http://stackoverflow.com/questions/432842/how-do-you-get-the-logical-xor-of-two-variables-in-python">logical xor post</a> and is trying to find a way to adapt to multiple variables and ...
9
2009-06-23T12:56:22Z
1,032,487
<p>Here's my straightforward approach. I've renamed it only_one since xor with more than one input is usually a parity checker, not an "only one" checker.</p> <pre><code>def only_one(*args): result = False for a in args: if a: if result: return False else: ...
1
2009-06-23T13:10:10Z
[ "python", "xor" ]
Check if only one variable in a list of variables is set
1,032,411
<p>I'm looking for a simple method to check if only one variable in a list of variables has a True value. I've looked at this <a href="http://stackoverflow.com/questions/432842/how-do-you-get-the-logical-xor-of-two-variables-in-python">logical xor post</a> and is trying to find a way to adapt to multiple variables and ...
9
2009-06-23T12:56:22Z
1,033,153
<p>I think the sum-based solution is fine for the given example, but keep in mind that boolean predicates in python always short-circuit their evaluation. So you might want to consider something more consistent with <a href="http://docs.python.org/library/functions.html#all">all and any</a>.</p> <pre><code>def any_on...
7
2009-06-23T15:07:06Z
[ "python", "xor" ]
using two for loops in python
1,032,722
<p>I have started to learn python recently and have a question about for loops that I was hoping someone could answer. I want to be able to print all the possible products of two numbers from one to ten. so: 2 by 2, 2 by 3, 2 by 4...2 by 10, 3 by 2, 3 by 3...3 by 10, 4 by 2, 4 by 3 etc...I would have thought the easies...
3
2009-06-23T13:57:41Z
1,032,749
<pre><code>for i in range(1, 11): for j in range(1, 11): print i * j </code></pre>
4
2009-06-23T14:01:16Z
[ "python", "for-loop", "combinations" ]
using two for loops in python
1,032,722
<p>I have started to learn python recently and have a question about for loops that I was hoping someone could answer. I want to be able to print all the possible products of two numbers from one to ten. so: 2 by 2, 2 by 3, 2 by 4...2 by 10, 3 by 2, 3 by 3...3 by 10, 4 by 2, 4 by 3 etc...I would have thought the easies...
3
2009-06-23T13:57:41Z
1,032,782
<p>Just for fun (and the itertools-addicted SO readers) using only one for-loop:</p> <pre><code>from itertools import product for i,j in product(xrange(1,11), xrange(1,11)): print i*j </code></pre> <p>EDIT: using xrange as suggested by Hank Gay</p>
4
2009-06-23T14:07:09Z
[ "python", "for-loop", "combinations" ]
using two for loops in python
1,032,722
<p>I have started to learn python recently and have a question about for loops that I was hoping someone could answer. I want to be able to print all the possible products of two numbers from one to ten. so: 2 by 2, 2 by 3, 2 by 4...2 by 10, 3 by 2, 3 by 3...3 by 10, 4 by 2, 4 by 3 etc...I would have thought the easies...
3
2009-06-23T13:57:41Z
1,032,895
<p>Here is another way</p> <pre><code>a = [i*j for i in xrange(1,11) for j in xrange(i,11)] </code></pre> <p><strong>note</strong> we need to start second iterator from 'i' instead of 1, so this is doubly efficient</p> <p>edit: proof that it is same as simple solution</p> <pre><code>b = [] for i in range(1,11): ...
8
2009-06-23T14:26:16Z
[ "python", "for-loop", "combinations" ]
Dump stacktraces of all active Threads
1,032,813
<p>I'm trying to dump a list of all active threads including the current stack of each. I can get a list of all threads using threading.enumerate(), but i can't figure out a way to get to the stack from there. </p> <p>Background: A Zope/Plone app freaks out from time to time, consuming 100% of cpu and needs to be rest...
19
2009-06-23T14:13:38Z
1,032,896
<p>There is an applicable recipe on <a href="http://code.activestate.com/recipes/496960/" rel="nofollow">ASPN</a>. You can use <code>threading.enumerate()</code> to get all the tids, then just call _async_raise() with some suitable exception to force a stack trace.</p>
0
2009-06-23T14:26:48Z
[ "python", "multithreading", "plone", "zope" ]
Dump stacktraces of all active Threads
1,032,813
<p>I'm trying to dump a list of all active threads including the current stack of each. I can get a list of all threads using threading.enumerate(), but i can't figure out a way to get to the stack from there. </p> <p>Background: A Zope/Plone app freaks out from time to time, consuming 100% of cpu and needs to be rest...
19
2009-06-23T14:13:38Z
1,032,930
<p>2.4. Too bad. From Python 2.5 on there is <code>sys._current_frames()</code>.</p> <p>But you could try <a href="http://www.majid.info/mylos/stories/2004/06/10/threadframe.html" rel="nofollow">threadframe</a>. And if the makefile gives you trouble you could try this <a href="http://www.wsanchez.net/blog/2004/06/stac...
7
2009-06-23T14:31:33Z
[ "python", "multithreading", "plone", "zope" ]
Dump stacktraces of all active Threads
1,032,813
<p>I'm trying to dump a list of all active threads including the current stack of each. I can get a list of all threads using threading.enumerate(), but i can't figure out a way to get to the stack from there. </p> <p>Background: A Zope/Plone app freaks out from time to time, consuming 100% of cpu and needs to be rest...
19
2009-06-23T14:13:38Z
1,038,671
<p>When using Zope, you want to install <a href="http://pypi.python.org/pypi/Products.signalstack" rel="nofollow"><code>Products.signalstack</code></a> or <a href="https://pypi.python.org/pypi/mr.freeze" rel="nofollow">mr.freeze</a>; these were designed for just this purpose!</p> <p>Send a USR1 signal to your Zope ser...
6
2009-06-24T14:18:14Z
[ "python", "multithreading", "plone", "zope" ]
Dump stacktraces of all active Threads
1,032,813
<p>I'm trying to dump a list of all active threads including the current stack of each. I can get a list of all threads using threading.enumerate(), but i can't figure out a way to get to the stack from there. </p> <p>Background: A Zope/Plone app freaks out from time to time, consuming 100% of cpu and needs to be rest...
19
2009-06-23T14:13:38Z
7,317,379
<p>As jitter points out in an earlier answer <code>sys._current_frames()</code> gives you what you need for v2.5+. For the lazy the following code snippet worked for me and may help you:</p> <pre><code>print &gt;&gt; sys.stderr, "\n*** STACKTRACE - START ***\n" code = [] for threadId, stack in sys._current_frames().it...
27
2011-09-06T09:02:15Z
[ "python", "multithreading", "plone", "zope" ]
Dump stacktraces of all active Threads
1,032,813
<p>I'm trying to dump a list of all active threads including the current stack of each. I can get a list of all threads using threading.enumerate(), but i can't figure out a way to get to the stack from there. </p> <p>Background: A Zope/Plone app freaks out from time to time, consuming 100% of cpu and needs to be rest...
19
2009-06-23T14:13:38Z
24,334,576
<p>For Python 3.3 and later, there is <a href="https://docs.python.org/3/library/faulthandler.html#dumping-the-traceback" rel="nofollow"><code>faulthandler.dump_traceback()</code></a>.</p> <p>The code below produces similar output, but includes the thread name and could be enhanced to print more information.</p> <pre...
8
2014-06-20T19:46:18Z
[ "python", "multithreading", "plone", "zope" ]
Dump stacktraces of all active Threads
1,032,813
<p>I'm trying to dump a list of all active threads including the current stack of each. I can get a list of all threads using threading.enumerate(), but i can't figure out a way to get to the stack from there. </p> <p>Background: A Zope/Plone app freaks out from time to time, consuming 100% of cpu and needs to be rest...
19
2009-06-23T14:13:38Z
33,466,873
<p>Just for completeness sake, <a href="https://pypi.python.org/pypi/Products.LongRequestLogger" rel="nofollow">Products.LongRequestLogger</a> is super helpful to identify bottlenecks, and to do so it dumps stacktraces at specific intervals.</p>
1
2015-11-01T20:32:30Z
[ "python", "multithreading", "plone", "zope" ]
Template driven feed parsing
1,032,976
<p>Requirements: I have a python project which parses data feeds from multiple sources in varying formats (Atom, valid XML, invalid XML, csv, almost-garbage, etc...) and inserts the resulting data into a database. The catch is the information required to parse each of the feeds must also be stored in the database.</p> ...
2
2009-06-23T14:39:33Z
1,033,530
<p>Instead of <code>eval</code>ing scripts, maybe you should consider making a package of them? Parsing CSV is one thing — the format is simple and regular, parsing XML requires completely another approach. Considering you don't want to write every single parser from scratch, why not just write a bunch of small modul...
1
2009-06-23T16:01:19Z
[ "python" ]
Decorators that are properties of decorated objects?
1,033,107
<p>I want to create a decorator that allows me to refer back to the decorated object and grab another decorator from it, the same way you can use setter/deleter on properties:</p> <pre><code>@property def x(self): return self._x @x.setter def x(self, y): self._x = y </code></pre> <p>Specifically, I'd like it...
1
2009-06-23T14:59:12Z
1,033,313
<p>I fixed many little details and the following version seems to work as you require:</p> <pre><code>def listprop(indices): def dec(func): class c(object): def __init__(self, l, obj=None): self.l = l self.obj = obj def __get__(self, obj, cls=None): ...
3
2009-06-23T15:29:29Z
[ "python", "decorator" ]
Specifying TkInter Callbacks In Dictionary For Display Launcher Function
1,033,130
<p>I am having trouble building a Python function that launches TkInter objects, with commands bound to menu buttons, using button specifications held in a dictionary.</p> <p>SITUATION</p> <p>I am building a GUI in Python using TkInter. I have written a Display class (based on the GuiMaker class in Lutz, "Programmin...
0
2009-06-23T15:03:04Z
1,033,498
<p>When your <code>lambda</code> executes is when scope applies, but the issue is a bit subtler.</p> <p>In the first case that <code>lambda</code> is a nested function of <code>launchEmployee</code> so the Python compiler (when it compiles the enclosing function) knows to scan its body for references to local variable...
0
2009-06-23T15:56:19Z
[ "python" ]
SQLAlchemy: Object Mappings lost after commit?
1,033,199
<p>I got a simple problem in SQLAlchemy. I have one model in a table, lets call it Model1 here. I want to add a row in this table, and get the autoincremented key, so I can create another model with it, and use this key. This is not a flawed database design (1:1 relation etc). I simply need this key in another table, b...
2
2009-06-23T15:13:00Z
1,034,593
<p>I've only used this with ForeignKeys, so you in the second case rather would do model.AnotherModel(model1=object1), and then it just worked (tm). So I suspect this may be a problem with your models, so maybe you could post them too?</p>
1
2009-06-23T19:20:17Z
[ "python", "database", "sqlalchemy" ]
SQLAlchemy: Object Mappings lost after commit?
1,033,199
<p>I got a simple problem in SQLAlchemy. I have one model in a table, lets call it Model1 here. I want to add a row in this table, and get the autoincremented key, so I can create another model with it, and use this key. This is not a flawed database design (1:1 relation etc). I simply need this key in another table, b...
2
2009-06-23T15:13:00Z
1,035,329
<p>A couple of things:</p> <ul> <li>Could you please explain what the variable <code>transaction</code> is bound to?</li> <li>Exactly what statement raises the UnboundExecutionError?</li> <li>Please provide the full exception message, including stack trace.</li> <li>The 'normal' thing to do in this case, would be to c...
3
2009-06-23T21:24:34Z
[ "python", "database", "sqlalchemy" ]
SQLAlchemy: Object Mappings lost after commit?
1,033,199
<p>I got a simple problem in SQLAlchemy. I have one model in a table, lets call it Model1 here. I want to add a row in this table, and get the autoincremented key, so I can create another model with it, and use this key. This is not a flawed database design (1:1 relation etc). I simply need this key in another table, b...
2
2009-06-23T15:13:00Z
1,041,764
<p>if you want to get new primary key identifiers to generate without anything being committed, just call session.flush(). That will emit everything pending to the database within the current transaction. </p>
1
2009-06-25T01:29:33Z
[ "python", "database", "sqlalchemy" ]
Pylons - use Python 2.5 or 2.6?
1,033,367
<p>Which version of Python is recommended for Pylons, and why?</p>
4
2009-06-23T15:37:44Z
1,033,394
<p>Pylons itself <a href="http://pylonshq.com/docs/en/0.9.7/gettingstarted/" rel="nofollow">says</a> it needs at least 2.3, and recommends 2.4+. Since 2.6 is <a href="http://python.org/download/" rel="nofollow">production ready</a>, I'd use that.</p>
5
2009-06-23T15:41:43Z
[ "python", "pylons" ]
Pylons - use Python 2.5 or 2.6?
1,033,367
<p>Which version of Python is recommended for Pylons, and why?</p>
4
2009-06-23T15:37:44Z
1,033,433
<p><a href="http://pylonshq.com/docs/en/0.9.7/gettingstarted/#requirements" rel="nofollow">You can use Python 2.3 to 2.6</a>, though 2.3 support will be dropped in the next version. <a href="http://wiki.pylonshq.com/display/pylonscommunity/Pylons%2BRoadmap%2Bto%2B1.0" rel="nofollow">You can't use Python 3 yet</a>.</p> ...
2
2009-06-23T15:46:59Z
[ "python", "pylons" ]
Pylons - use Python 2.5 or 2.6?
1,033,367
<p>Which version of Python is recommended for Pylons, and why?</p>
4
2009-06-23T15:37:44Z
1,033,467
<p>I'd say use 2.5 : </p> <p>there is one reason to favor 2.5 over 2.6 : if you need to be compatible with the python given on a linux installation or on Macs (I dont' know what py version mac provide, but you get the idea).</p> <p>Of course, if you need some feature of 2.6, please do it, but if it's not the case why...
1
2009-06-23T15:51:05Z
[ "python", "pylons" ]
How to remove bad path characters in Python?
1,033,424
<p>What is the most cross platform way of removing bad path characters (e.g. "\" or ":" on Windows) in Python?</p> <h3>Solution</h3> <p>Because there seems to be no ideal solution I decided to be relatively restrictive and did use the following code:</p> <pre><code>def remove(value, deletechars): for c in delete...
13
2009-06-23T15:45:39Z
1,033,446
<p>That character is in <code>os.sep</code>, it'll be "\" or ":", depending on which system you're on.</p>
0
2009-06-23T15:48:32Z
[ "python", "path" ]
How to remove bad path characters in Python?
1,033,424
<p>What is the most cross platform way of removing bad path characters (e.g. "\" or ":" on Windows) in Python?</p> <h3>Solution</h3> <p>Because there seems to be no ideal solution I decided to be relatively restrictive and did use the following code:</p> <pre><code>def remove(value, deletechars): for c in delete...
13
2009-06-23T15:45:39Z
1,033,591
<p>If you are using python try <a href="http://docs.python.org/library/os.path.html" rel="nofollow">os.path</a> to avoid cross platform issues with paths.</p>
1
2009-06-23T16:08:22Z
[ "python", "path" ]
How to remove bad path characters in Python?
1,033,424
<p>What is the most cross platform way of removing bad path characters (e.g. "\" or ":" on Windows) in Python?</p> <h3>Solution</h3> <p>Because there seems to be no ideal solution I decided to be relatively restrictive and did use the following code:</p> <pre><code>def remove(value, deletechars): for c in delete...
13
2009-06-23T15:45:39Z
1,033,669
<p>Unfortunately, the set of acceptable characters varies by OS <em>and</em> by filesystem.</p> <ul> <li><p><a href="http://msdn.microsoft.com/en-us/library/aa365247.aspx">Windows</a>:</p> <blockquote> <ul> <li>Use almost any character in the current code page for a name, including Unicode characters and characte...
9
2009-06-23T16:21:28Z
[ "python", "path" ]
How to remove bad path characters in Python?
1,033,424
<p>What is the most cross platform way of removing bad path characters (e.g. "\" or ":" on Windows) in Python?</p> <h3>Solution</h3> <p>Because there seems to be no ideal solution I decided to be relatively restrictive and did use the following code:</p> <pre><code>def remove(value, deletechars): for c in delete...
13
2009-06-23T15:45:39Z
13,593,932
<p>I think the safest approach here is to just replace any suspicious characters. So, I think you can just replace (or get rid of) anything that isn't alphanumeric, -, _, a space, or a period. And here's how you do that:</p> <pre><code>import re re.sub('[^\w\-_\. ]', '_', filename) </code></pre> <p>The above escapes ...
7
2012-11-27T22:01:10Z
[ "python", "path" ]
python class attribute inheritance
1,033,443
<p>I am trying to save myself a bit of typing by writing the following code, but it seems I can't do this:</p> <pre><code>class lgrAdminObject(admin.ModelAdmin): fields = ["title","owner"] list_display = ["title","origin","approved", "sendToFrames"] class Photos(lgrAdminObject): fields.extend(["albums"]) ...
2
2009-06-23T15:47:56Z
1,033,516
<p>Inheritance applies <em>after</em> the class's body executes. In the class body, you can use <code>lgrAdminObject.fields</code> -- you sure you want to alter the superclass's attribute rather than making a copy of it first, though? Seems peculiar... I'd start with a copy:</p> <pre><code>class Photos(lgrAdminObject)...
7
2009-06-23T15:59:32Z
[ "python", "django", "class", "inheritance" ]
python class attribute inheritance
1,033,443
<p>I am trying to save myself a bit of typing by writing the following code, but it seems I can't do this:</p> <pre><code>class lgrAdminObject(admin.ModelAdmin): fields = ["title","owner"] list_display = ["title","origin","approved", "sendToFrames"] class Photos(lgrAdminObject): fields.extend(["albums"]) ...
2
2009-06-23T15:47:56Z
1,033,525
<p>Have you tried this?</p> <pre><code>fields = lgrAdminObject.fields + ["albums"] </code></pre> <p>You need to create a new class attribute, not extend the one from the parent class.</p>
4
2009-06-23T16:00:38Z
[ "python", "django", "class", "inheritance" ]
python class attribute inheritance
1,033,443
<p>I am trying to save myself a bit of typing by writing the following code, but it seems I can't do this:</p> <pre><code>class lgrAdminObject(admin.ModelAdmin): fields = ["title","owner"] list_display = ["title","origin","approved", "sendToFrames"] class Photos(lgrAdminObject): fields.extend(["albums"]) ...
2
2009-06-23T15:47:56Z
1,033,543
<p>If you insist on using class attributes, you can reference the base class directly.</p> <pre><code>class Photos(lgrAdminObject): lgrAdminObject.fields.extend(["albums"]) </code></pre> <p>A trivial check follows:</p> <pre><code>&gt;&gt;&gt; class B0: ... fields = ["title","owner"] ... &gt;&gt;&gt; cla...
2
2009-06-23T16:03:07Z
[ "python", "django", "class", "inheritance" ]
python class attribute inheritance
1,033,443
<p>I am trying to save myself a bit of typing by writing the following code, but it seems I can't do this:</p> <pre><code>class lgrAdminObject(admin.ModelAdmin): fields = ["title","owner"] list_display = ["title","origin","approved", "sendToFrames"] class Photos(lgrAdminObject): fields.extend(["albums"]) ...
2
2009-06-23T15:47:56Z
1,034,359
<p>Also, note that when you have a list as a class attribute, it belongs to the class, not the instances. So if you modify it, it will change for all instances. It's probably better to use a tuple instead, unless you intend to modify it with this effect (and then you should document that clearly, because that is a comm...
1
2009-06-23T18:39:02Z
[ "python", "django", "class", "inheritance" ]
Monkeypatching a method call in Python
1,033,519
<p>How do I put off attribute access in Python?</p> <p>Let's assume we have:</p> <pre><code> def foo(): ... class Bar: ... bar = Bar() </code></pre> <p>Is it possible to implement Bar so that any time <code>bar</code> is accessed, a value returned by the callback <code>foo()</code> would...
-1
2009-06-23T16:00:02Z
1,033,555
<p>I guess you want to link some attribute "data" to foo:</p> <pre><code>class Bar: data = property(lambda self: foo()) bar = Bar() bar.data # calls foo() </code></pre>
6
2009-06-23T16:04:41Z
[ "python", "reference", "properties", "callback" ]
Monkeypatching a method call in Python
1,033,519
<p>How do I put off attribute access in Python?</p> <p>Let's assume we have:</p> <pre><code> def foo(): ... class Bar: ... bar = Bar() </code></pre> <p>Is it possible to implement Bar so that any time <code>bar</code> is accessed, a value returned by the callback <code>foo()</code> would...
-1
2009-06-23T16:00:02Z
1,033,604
<p>You're basically asking for a way to hijack a variable (how would you reassign it?) in the module namespace, which is not possible in Python.</p> <p>You'll have to use attribute accessors of a class if you want the described behavior:</p> <pre><code>class MyClass(object): def __getattr__(self, attr): i...
2
2009-06-23T16:10:53Z
[ "python", "reference", "properties", "callback" ]
Monkeypatching a method call in Python
1,033,519
<p>How do I put off attribute access in Python?</p> <p>Let's assume we have:</p> <pre><code> def foo(): ... class Bar: ... bar = Bar() </code></pre> <p>Is it possible to implement Bar so that any time <code>bar</code> is accessed, a value returned by the callback <code>foo()</code> would...
-1
2009-06-23T16:00:02Z
1,033,624
<p>"any time bar is accessed"... What kinds of accesses are your callers going to be making? (E.g. are they doing "1+bar", are they doing "bar[5:]", are they doing "bar.func()", etc). Will they call the Bar() constructor each time?</p> <p>Right now, your question is so fuzzy and general that I think it's impossible....
0
2009-06-23T16:14:25Z
[ "python", "reference", "properties", "callback" ]
Monkeypatching a method call in Python
1,033,519
<p>How do I put off attribute access in Python?</p> <p>Let's assume we have:</p> <pre><code> def foo(): ... class Bar: ... bar = Bar() </code></pre> <p>Is it possible to implement Bar so that any time <code>bar</code> is accessed, a value returned by the callback <code>foo()</code> would...
-1
2009-06-23T16:00:02Z
1,033,810
<p>Is this what you're looking for?</p> <pre><code>&gt;&gt;&gt; def foo(): print('foo() was called'); ... &gt;&gt;&gt; class Bar: ... pass; ... &gt;&gt;&gt; bar = Bar(); &gt;&gt;&gt; bar.data = foo; &gt;&gt;&gt; bar.data() foo() was called &gt;&gt;&gt; </code></pre>
0
2009-06-23T16:49:06Z
[ "python", "reference", "properties", "callback" ]
Monkeypatching a method call in Python
1,033,519
<p>How do I put off attribute access in Python?</p> <p>Let's assume we have:</p> <pre><code> def foo(): ... class Bar: ... bar = Bar() </code></pre> <p>Is it possible to implement Bar so that any time <code>bar</code> is accessed, a value returned by the callback <code>foo()</code> would...
-1
2009-06-23T16:00:02Z
1,034,142
<p>Thanks for the clarification!</p> <p>This just can't work! A variable is a variable in most languages, not a function-call. You can do much in Python, but you just can't do that.</p> <p>The reason is, that you have always some intrinsic language rules. One rule in Python is, that variables are variables. When you ...
0
2009-06-23T17:58:38Z
[ "python", "reference", "properties", "callback" ]
Monkeypatching a method call in Python
1,033,519
<p>How do I put off attribute access in Python?</p> <p>Let's assume we have:</p> <pre><code> def foo(): ... class Bar: ... bar = Bar() </code></pre> <p>Is it possible to implement Bar so that any time <code>bar</code> is accessed, a value returned by the callback <code>foo()</code> would...
-1
2009-06-23T16:00:02Z
1,034,209
<p>You can override Bar's __new__ method to execute arbitrary code and return a new instance. See: <a href="http://docs.python.org/reference/datamodel.html#object.__new__" rel="nofollow">http://docs.python.org/reference/datamodel.html#object.__new__</a></p> <p>Here's a contrived example:</p> <pre><code>&gt;&gt;&gt; c...
0
2009-06-23T18:12:43Z
[ "python", "reference", "properties", "callback" ]
Monkeypatching a method call in Python
1,033,519
<p>How do I put off attribute access in Python?</p> <p>Let's assume we have:</p> <pre><code> def foo(): ... class Bar: ... bar = Bar() </code></pre> <p>Is it possible to implement Bar so that any time <code>bar</code> is accessed, a value returned by the callback <code>foo()</code> would...
-1
2009-06-23T16:00:02Z
1,034,275
<p>If you want the results of foo(), the easiest way is to do this:</p> <pre><code>foo() </code></pre> <p>Anything else is just unnecessary complication. I suspect that you have oversimplified your example until it doens't make sense.</p> <p>Edit: OK, so you are trying to change somebody elses code. No, it's not pos...
0
2009-06-23T18:22:30Z
[ "python", "reference", "properties", "callback" ]
Methods for modular customization of locale messages?
1,033,580
<p>There many levels for the customization of programs.</p> <p>First of course is making it <strong>speak your language</strong> by creating i18n messages where tools like gettext and xgettext do a great job.</p> <p>Another comes when you need to <strong>modify the meaning</strong> of some messages to suite the purpo...
0
2009-06-23T16:07:24Z
1,034,057
<p>In Java, these localized strings are handled by ResourceBundles. ResourceBundles have a concept of variants. For example, you could have a base English resource, called <code>messages_en.propertie</code>s. Then you could customize for a specific variant of English with <code>message_en_US.properties</code> or <code>...
1
2009-06-23T17:43:51Z
[ "python", "language-agnostic", "localization", "internationalization", "customization" ]
Methods for modular customization of locale messages?
1,033,580
<p>There many levels for the customization of programs.</p> <p>First of course is making it <strong>speak your language</strong> by creating i18n messages where tools like gettext and xgettext do a great job.</p> <p>Another comes when you need to <strong>modify the meaning</strong> of some messages to suite the purpo...
0
2009-06-23T16:07:24Z
1,036,232
<p>I think Qt's powerful i18n facilities (see <a href="http://doc.trolltech.com/4.5/i18n.html" rel="nofollow">here</a>) might meet your needs -- of course, they're available in Python, too, thanks to the usual, blessed <a href="http://www.riverbankcomputing.co.uk/software/pyqt/intro" rel="nofollow">PyQt</a>!-)</p>
1
2009-06-24T03:13:57Z
[ "python", "language-agnostic", "localization", "internationalization", "customization" ]
Python package install using pip or easy_install from repos
1,033,897
<p>The simplest way to deal with python package installations, so far, to me, has been to check out the source from the source control system and then add a symbolic link in the python dist-packages folder.</p> <p>Clearly since source control provides the complete control to downgrade, upgrade to any branch, tag, it w...
14
2009-06-23T17:07:30Z
1,034,006
<p>If you download or check out the source distribution of a package — the one that has its "setup.py" inside of it — then if the package is based on the "setuptools" (which also power easy_install), you can move into that directory and say:</p> <pre><code>$ python setup.py develop </code></pre> <p>and it will cr...
11
2009-06-23T17:32:22Z
[ "python", "svn", "version-control", "easy-install", "pip" ]
Python package install using pip or easy_install from repos
1,033,897
<p>The simplest way to deal with python package installations, so far, to me, has been to check out the source from the source control system and then add a symbolic link in the python dist-packages folder.</p> <p>Clearly since source control provides the complete control to downgrade, upgrade to any branch, tag, it w...
14
2009-06-23T17:07:30Z
1,034,335
<p>easy_install has support for downloading specific versions. For example:</p> <pre><code>easy_install python-dateutil==1.4.0 </code></pre> <p>Will install v1.4, while the latest version 1.4.1 would be picked if no version was specified.</p> <p>There is also support for svn checkouts, but using that doesn't give yo...
4
2009-06-23T18:35:33Z
[ "python", "svn", "version-control", "easy-install", "pip" ]
Python package install using pip or easy_install from repos
1,033,897
<p>The simplest way to deal with python package installations, so far, to me, has been to check out the source from the source control system and then add a symbolic link in the python dist-packages folder.</p> <p>Clearly since source control provides the complete control to downgrade, upgrade to any branch, tag, it w...
14
2009-06-23T17:07:30Z
1,446,729
<p>easy_install accepts a URL for the source tree too. Works at least when the sources are in Subversion.</p>
0
2009-09-18T20:39:07Z
[ "python", "svn", "version-control", "easy-install", "pip" ]
Python package install using pip or easy_install from repos
1,033,897
<p>The simplest way to deal with python package installations, so far, to me, has been to check out the source from the source control system and then add a symbolic link in the python dist-packages folder.</p> <p>Clearly since source control provides the complete control to downgrade, upgrade to any branch, tag, it w...
14
2009-06-23T17:07:30Z
1,625,662
<p>Using <a href="http://pip.openplans.org/">pip</a> this is quite easy. For instance:</p> <pre><code>pip install -e hg+http://bitbucket.org/andrewgodwin/south/#egg=South </code></pre> <p>Pip will automatically clone the source repo and run "setup.py develop" for you to install it into your environment (which hopeful...
26
2009-10-26T16:08:10Z
[ "python", "svn", "version-control", "easy-install", "pip" ]
Per-session transactions in Django
1,033,934
<p>I'm making a Django web-app which allows a user to build up a set of changes over a series of GETs/POSTs before committing them to the database (or reverting) with a final POST. I have to keep the updates isolated from any concurrent database users until they are confirmed (this is a configuration front-end), ruling...
7
2009-06-23T17:16:29Z
1,033,973
<p>Multiple, concurrent, session-scale transactions will generally lead to deadlocks or worse (worse == livelock, long delays while locks are held by another session.)</p> <p>This design is not the best policy, which is why Django discourages it.</p> <p>The better solution is the following.</p> <ol> <li><p>Design a ...
9
2009-06-23T17:24:42Z
[ "python", "django", "transactions" ]
Per-session transactions in Django
1,033,934
<p>I'm making a Django web-app which allows a user to build up a set of changes over a series of GETs/POSTs before committing them to the database (or reverting) with a final POST. I have to keep the updates isolated from any concurrent database users until they are confirmed (this is a configuration front-end), ruling...
7
2009-06-23T17:16:29Z
1,057,379
<p>In case anyone else ever has the exact same problem as me (I hope not), here is my monkeypatch. It's fragile and ugly, and changes private methods, but thankfully it's small. Please don't use it unless you really have to. As mentioned by others, any application using it effectively prevents multiple users doing upda...
1
2009-06-29T09:23:48Z
[ "python", "django", "transactions" ]
Per-session transactions in Django
1,033,934
<p>I'm making a Django web-app which allows a user to build up a set of changes over a series of GETs/POSTs before committing them to the database (or reverting) with a final POST. I have to keep the updates isolated from any concurrent database users until they are confirmed (this is a configuration front-end), ruling...
7
2009-06-23T17:16:29Z
1,121,915
<p>I came up with something similar to the Memento pattern, but different enough that I think it bears posting. When a user starts an editing session, I duplicate the target object to a temporary object in the database. All subsequent editing operations affect the duplicate. Instead of saving the object state in a <...
2
2009-07-13T20:42:10Z
[ "python", "django", "transactions" ]
What is the difference between converting to hex on the client end and using rawtohex?
1,034,068
<p>I have a table that's created like this:</p> <pre><code>CREATE TABLE bin_test (id INTEGER PRIMARY KEY, b BLOB) </code></pre> <p>Using Python and cx_Oracle, if I do this:</p> <pre><code>value = "\xff\x00\xff\x00" #The string represented in hex by ff00ff00 self.connection.execute("INSERT INTO bin_test (b) VALUES (r...
2
2009-06-23T17:46:24Z
1,034,098
<p><code>RAWTOHEX</code> in <code>Oracle</code> is bit order insensitive, while on your machine it's of course sensitive.</p> <p>Also note that an argument to <code>RAWTOHEX()</code> can be implicitly converted to <code>VARCHAR2</code> by your library (i. e. transmitted as <code>SQLT_STR</code>), which makes it also e...
1
2009-06-23T17:51:15Z
[ "python", "oracle", "oracle10g", "blob", "cx-oracle" ]
What is the difference between converting to hex on the client end and using rawtohex?
1,034,068
<p>I have a table that's created like this:</p> <pre><code>CREATE TABLE bin_test (id INTEGER PRIMARY KEY, b BLOB) </code></pre> <p>Using Python and cx_Oracle, if I do this:</p> <pre><code>value = "\xff\x00\xff\x00" #The string represented in hex by ff00ff00 self.connection.execute("INSERT INTO bin_test (b) VALUES (r...
2
2009-06-23T17:46:24Z
1,034,127
<p>rawtohex() is for converting Oracles RAW datatypes to hex strings. It's possible it gets confused by you passing it a string, even if the string contains binary data. In this case, since Oracle expects a string of hex characters, give it a string of hex characters.</p>
1
2009-06-23T17:55:29Z
[ "python", "oracle", "oracle10g", "blob", "cx-oracle" ]
What is the difference between converting to hex on the client end and using rawtohex?
1,034,068
<p>I have a table that's created like this:</p> <pre><code>CREATE TABLE bin_test (id INTEGER PRIMARY KEY, b BLOB) </code></pre> <p>Using Python and cx_Oracle, if I do this:</p> <pre><code>value = "\xff\x00\xff\x00" #The string represented in hex by ff00ff00 self.connection.execute("INSERT INTO bin_test (b) VALUES (r...
2
2009-06-23T17:46:24Z
1,710,558
<p>I usually set the proper type of variable bindings specially when trying to pass an Oracle kinda of RAW data type into a query.</p> <p>for example something like:</p> <pre><code>self.connection.setinputsizes(cx_Oracle.BINARY) self.connection.execute( "INSERT INTO bin_test (b) VALUES (rawtohex(?))", (value,...
0
2009-11-10T19:29:34Z
[ "python", "oracle", "oracle10g", "blob", "cx-oracle" ]
Python List Question
1,034,145
<p>i have an issue i could use some help with, i have python list that looks like this:</p> <pre><code>fail = [ ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\b\\include', 'Test.java'] ['b5cc17d3a35877ca8b76f0b2e07497039c250696', 'ron\\c', 'apa1.txt'] ['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'knark.t...
1
2009-06-23T18:00:27Z
1,034,254
<p>you could simply loop through all the values then use an inner loop to compare directories, then if the directory is the same compare values, then assign lists. this would give you a decent n^2 algorithm to sort it out.</p> <p>maybe like this untested code:</p> <pre><code>&gt;&gt;&gt;for i in range(len(fail)-1): ....
1
2009-06-23T18:19:21Z
[ "python", "list" ]
Python List Question
1,034,145
<p>i have an issue i could use some help with, i have python list that looks like this:</p> <pre><code>fail = [ ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\b\\include', 'Test.java'] ['b5cc17d3a35877ca8b76f0b2e07497039c250696', 'ron\\c', 'apa1.txt'] ['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'knark.t...
1
2009-06-23T18:00:27Z
1,034,453
<pre><code>duplicate = [] # Sort the list so we can compare adjacent values fail.sort() #if you didn't want to modify the list in place you can use: #sortedFail = sorted(fail) # and then use sortedFail in the rest of the code instead of fail for i, x in enumerate(fail): if i+1 == len(fail): #end of the...
3
2009-06-23T18:54:37Z
[ "python", "list" ]
Python List Question
1,034,145
<p>i have an issue i could use some help with, i have python list that looks like this:</p> <pre><code>fail = [ ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\b\\include', 'Test.java'] ['b5cc17d3a35877ca8b76f0b2e07497039c250696', 'ron\\c', 'apa1.txt'] ['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'knark.t...
1
2009-06-23T18:00:27Z
1,034,542
<p>Here's another way to go at it using dictionaries to group by sha and directory. This also gets rid of the random lists in the file names.</p> <pre><code>new_fail = {} # {sha: {dir: [filenames]}} for item in fail: # split data into it's parts sha, directory, filename = item # make sure the correct...
0
2009-06-23T19:11:01Z
[ "python", "list" ]
Python List Question
1,034,145
<p>i have an issue i could use some help with, i have python list that looks like this:</p> <pre><code>fail = [ ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\b\\include', 'Test.java'] ['b5cc17d3a35877ca8b76f0b2e07497039c250696', 'ron\\c', 'apa1.txt'] ['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'knark.t...
1
2009-06-23T18:00:27Z
1,034,556
<p>In the following code sample, I use a key based on the SHA1 and directory name to detect unique and duplicate entries and spare dictionaries for housekeeping. </p> <pre><code># Test dataset fail = [ ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\b\\include', 'Test.java'], ['b5cc17d3a35877ca8b76f0b2e07497039c250...
1
2009-06-23T19:13:15Z
[ "python", "list" ]
Python List Question
1,034,145
<p>i have an issue i could use some help with, i have python list that looks like this:</p> <pre><code>fail = [ ['da39a3ee5e6b4b0d3255bfef95601890afd80709', 'ron\\b\\include', 'Test.java'] ['b5cc17d3a35877ca8b76f0b2e07497039c250696', 'ron\\c', 'apa1.txt'] ['95d1543adea47e88923c3d4ad56e9f65c2b40c76', 'ron\\c', 'knark.t...
1
2009-06-23T18:00:27Z
1,034,700
<p>I believe the accepted answer will be slightly more efficient (Python's internal sort should be faster than my dictionary walk), but since I already came up with this, I may as well post it. :-)</p> <p>This technique uses a multilevel dictionary to avoid both sorting and explicit comparisons.</p> <pre><code>hashe...
0
2009-06-23T19:36:02Z
[ "python", "list" ]
wxPython: Making something expand
1,034,399
<p>How do I make any wxPython widget (like wx.Panel or wx.Button) automatically expand to fill its parent window?</p>
4
2009-06-23T18:46:44Z
1,034,455
<p>The short answer: use a sizer with a proportion of 1 and the wx.Expand tag.</p> <p>So here I am in the <strong>init</strong> of a panel</p> <pre><code>sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.canvas, 1, wx.EXPAND) self.SetSizer(sizer) </code></pre>
8
2009-06-23T18:55:11Z
[ "python", "user-interface", "wxpython" ]
wxPython: Making something expand
1,034,399
<p>How do I make any wxPython widget (like wx.Panel or wx.Button) automatically expand to fill its parent window?</p>
4
2009-06-23T18:46:44Z
1,036,283
<p>this shows how you can expand child panel with frame resize it also show how you can switch two panels, one containing cntrls and one containing help I think this solves all your problems</p> <pre><code>import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None) self.p...
1
2009-06-24T03:40:24Z
[ "python", "user-interface", "wxpython" ]
wxPython: Making something expand
1,034,399
<p>How do I make any wxPython widget (like wx.Panel or wx.Button) automatically expand to fill its parent window?</p>
4
2009-06-23T18:46:44Z
5,628,847
<p>In the case of a Button, to make a button fill its parent window when the parent window (a frame in my case) changes size, put <code>button.SetSize(parent_window.GetSize())</code> in the parent window's OnSize event handling routine.</p>
0
2011-04-11T23:45:36Z
[ "python", "user-interface", "wxpython" ]
How do I use the bash time function from python?
1,034,566
<p>I would like to use python to make system calls to programs and time them. From the Linux command line if you type:</p> <pre><code>$ time prog args </code></pre> <p>You get something along the lines of:</p> <pre><code>real 0m0.110s user 0m0.060s sys 0m0.024s </code></pre> <p>if you do a 'man time', it ...
1
2009-06-23T19:16:47Z
1,034,661
<p>You are correct that bash has it's own version of time.</p> <pre><code>$ type time time is a shell keyword </code></pre> <p>Perhaps you could explicitly invoke bash with the -c option to get it's timing.</p> <p>Depending on which distribution you're using, the default shell may be dash, a simpler shell that doesn...
2
2009-06-23T19:30:59Z
[ "python", "linux", "bash", "unix", "time" ]
How do I use the bash time function from python?
1,034,566
<p>I would like to use python to make system calls to programs and time them. From the Linux command line if you type:</p> <pre><code>$ time prog args </code></pre> <p>You get something along the lines of:</p> <pre><code>real 0m0.110s user 0m0.060s sys 0m0.024s </code></pre> <p>if you do a 'man time', it ...
1
2009-06-23T19:16:47Z
1,034,740
<p>I don't think time will be more accurate than Pythons methods, unless you use os.times() in versions below 2.5.3, and look at the user time, because earlier versions of Python has a big there.</p> <p>And although the time command behaves differently then bash's time builtin, they both return the relevant informatio...
0
2009-06-23T19:42:21Z
[ "python", "linux", "bash", "unix", "time" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,034,598
<pre><code>def xstr(s): return '' if s is None else str(s) </code></pre>
91
2009-06-23T19:21:12Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,034,614
<pre><code>def xstr(s): return s if s else '' s = "%s%s" % (xstr(a), xstr(b)) </code></pre>
0
2009-06-23T19:25:57Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,034,632
<pre><code>def xstr(s): return {None:''}.get(s, s) </code></pre>
4
2009-06-23T19:28:12Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,034,633
<p>If you actually want your function to behave like the <code>str()</code> built-in, but return an empty string when the argument is None, do this:</p> <pre><code>def xstr(s): if s is None: return '' return str(s) </code></pre>
39
2009-06-23T19:28:18Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,034,668
<p>Functional way (one-liner)</p> <pre><code>xstr = lambda s: '' if s is None else s </code></pre>
6
2009-06-23T19:31:58Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,034,716
<p>Use short circuit evaluation:</p> <pre><code>s = a or '' + b or '' </code></pre> <p>Since + is not a very good operation on strings, better use format strings:</p> <pre><code>s = "%s%s" % (a or '', b or '') </code></pre>
-1
2009-06-23T19:37:21Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,035,497
<p>If you know that the value will always either be a string or None:</p> <pre><code>xstr = lambda s: s or "" print xstr("a") + xstr("b") # -&gt; 'ab' print xstr("a") + xstr(None) # -&gt; 'a' print xstr(None) + xstr("b") # -&gt; 'b' print xstr(None) + xstr(None) # -&gt; '' </code></pre>
64
2009-06-23T22:01:12Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,036,222
<p><code>return s or ''</code> will work just fine for your stated problem!</p>
38
2009-06-24T03:08:53Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,036,275
<p>Variation on the above if you need to be compatible with Python 2.4</p> <pre><code>xstr = lambda s: s is not None and s or '' </code></pre>
4
2009-06-24T03:37:06Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
1,036,316
<pre><code>def xstr(s): return s or "" </code></pre>
11
2009-06-24T03:56:07Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
12,215,416
<p>Probably the shortest would be <code>str(s or '')</code></p> <p>Because None is False, and "x or y" returns y if x is false. See <a href="http://docs.python.org/library/stdtypes.html#boolean-operations-and-or-not">Boolean Operators</a> for a detailed explanation. It's short, but not very explicit.</p>
28
2012-08-31T12:28:40Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
23,254,019
<p>I use max function:</p> <pre><code>max(None, '') #Returns blank max("Hello",'') #Returns Hello </code></pre> <p>Works like a charm ;) Just put your string in the first parameter of the function.</p>
1
2014-04-23T19:56:45Z
[ "string", "python", "idioms" ]
Python: most idiomatic way to convert None to empty string?
1,034,573
<p>What is the most idiomatic way to do the following?</p> <pre><code>def xstr(s): if s is None: return '' else: return s s = xstr(a) + xstr(b) </code></pre> <p><strong>update:</strong> I'm incorporating Tryptich's suggestion to use str(s), which makes this routine work for other types beside...
69
2009-06-23T19:17:29Z
30,455,911
<p>We can always avoid type casting in scenarios explained below.</p> <pre><code>customer = "John" name = str(customer) if name is None print "Name is blank" else: print "Customer name : " + name </code></pre> <p>In the example above in case variable customer's value is None the it further gets casting while g...
1
2015-05-26T10:27:53Z
[ "string", "python", "idioms" ]
Obtaining references to function objects on the execution stack from the frame object?
1,034,688
<p>Given the output of <code>inspect.stack()</code>, is it possible to get the function objects from anywhere from the stack frame and call these? If so, how?</p> <p>(I already know how to get the names of the functions.)</p> <p>Here is what I'm getting at: Let's say I'm a function and I'm trying to determine if my c...
1
2009-06-23T19:34:07Z
1,035,610
<p>Here is a code snippet that do it. There is no error checking. The idea is to find in the locals of the grand parent the function object that was called. The function object returned should be the parent. If you want to also search the builtins, then simply look into stacks[2][0].f_builtins. </p> <pre><code>def f()...
2
2009-06-23T22:30:56Z
[ "python", "inspect", "stackframe" ]
Obtaining references to function objects on the execution stack from the frame object?
1,034,688
<p>Given the output of <code>inspect.stack()</code>, is it possible to get the function objects from anywhere from the stack frame and call these? If so, how?</p> <p>(I already know how to get the names of the functions.)</p> <p>Here is what I'm getting at: Let's say I'm a function and I'm trying to determine if my c...
1
2009-06-23T19:34:07Z
1,038,818
<p>I took the following approach, very similar to Eolmar's answer.</p> <pre><code>stack = inspect.stack() parent_locals = stack[1][0].f_locals['self'] parent_func_name = stack[1][3] parent_func_attr = getattr(parent_locals,parent_func_name) parent_func = parent_func_attr.im_func is_parent_gen = inspect.isgeneratorfunc...
0
2009-06-24T14:43:04Z
[ "python", "inspect", "stackframe" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
1,034,863
<p>Use heapsort. It only partially orders the list until you draw the elements out.</p>
1
2009-06-23T20:07:22Z
[ "python", "arrays", "sorting" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
1,034,864
<p>You can iterate the entire sequence maintaining a list of the 5 largest values you find (this will be O(n)). That being said I think it would just be simpler to sort the list.</p>
3
2009-06-23T20:07:52Z
[ "python", "arrays", "sorting" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
1,034,869
<p>You essentially want to produce a "top-N" list and select the one at the end of that list.</p> <p>So you can scan the array once and insert into an empty list when the largeArray item is greater than the last item of your top-N list, then drop the last item.</p> <p>After you finish scanning, pick the last item in ...
1
2009-06-23T20:09:33Z
[ "python", "arrays", "sorting" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
1,034,886
<p>Sorting would require O(nlogn) runtime at minimum - There are very efficient <a href="http://en.wikipedia.org/wiki/Quick%5Fselect">selection algorithms</a> which can solve your problem in linear time.</p> <p><code>Partition-based selection</code> (sometimes <code>Quick select</code>), which is based on the idea of ...
13
2009-06-23T20:12:07Z
[ "python", "arrays", "sorting" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
1,034,923
<p>As people have said, you can walk the list once keeping track of K largest values. If K is large this algorithm will be close to O(n<sup>2</sup>).</p> <p>However, you can store your Kth largest values as a binary tree and the operation becomes O(n log k).</p> <p>According to Wikipedia, this is the best selection a...
1
2009-06-23T20:18:55Z
[ "python", "arrays", "sorting" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
1,034,949
<p>A simple modified quicksort works very well in practice. It has average running time proportional to N (though worst case bad luck running time is O(N^2)).</p> <p>Proceed like a quicksort. Pick a pivot value randomly, then stream through your values and see if they are above or below that pivot value and put them ...
3
2009-06-23T20:23:03Z
[ "python", "arrays", "sorting" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
1,036,240
<p>A heap is the best data structure for this operation and Python has an excellent built-in library to do just this, called heapq.</p> <pre><code>import heapq def nth_largest(n, iter): return heapq.nlargest(n, iter)[-1] </code></pre> <p>Example Usage:</p> <pre><code>&gt;&gt;&gt; import random &gt;&gt;&gt; iter...
18
2009-06-24T03:21:47Z
[ "python", "arrays", "sorting" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
2,129,986
<p>You could try the Median of Medians method - it's speed is O(N).</p>
2
2010-01-25T03:07:17Z
[ "python", "arrays", "sorting" ]
Finding Nth item of unsorted list without sorting the list
1,034,846
<p>Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I'm only interested in one element so there's probably a better way than sorting the entire array...</p>
15
2009-06-23T20:04:41Z
35,329,224
<p>One thing you should do if this is in production code is test with samples of your data. For example, you might consider 1000 or 10000 elements 'large' arrays, and code up a quickselect method from a recipe. </p> <p>The compiled nature of sorted, and its somewhat hidden and constantly evolving optimizations, make ...
0
2016-02-11T01:28:32Z
[ "python", "arrays", "sorting" ]
python multiprocessing manager
1,034,848
<p>My problem is:</p> <p>I have 3 procs that would like to share config loaded from the same class and a couple of queues. I would like to spawn another proc as a multiprocessing.manager to share those informations. </p> <p>How can I do that? Could someone purchase a sample code avoiding use of global vars and making...
3
2009-06-23T20:05:03Z
1,035,629
<p>I found <a href="http://docs.python.org/library/multiprocessing.html#exchanging-objects-between-processes" rel="nofollow">this</a> particular section in the Python multiprocessing docs helpful. The following program:</p> <pre><code>from multiprocessing import Process, Queue, current_process import time def f(q): ...
3
2009-06-23T22:36:17Z
[ "python", "multiprocessing" ]
How can I create a Word document using Python?
1,035,183
<p>I'd like to create a Word document using Python, however, I want to re-use as much of my existing document-creation code as possible. I am currently using an XSLT to generate an HTML file that I <a href="http://www.xhtml2pdf.com/">programatically convert</a> to a PDF file. However, my client is now requesting that...
32
2009-06-23T20:59:08Z
1,035,202
<p>Can you write is as the WordML XML files and zip it up into the .docx format? All your client would need is the Word 2007 filter if they aren't on Office 2007 already.</p> <p>There are many examples out there.</p> <p>You can also load XML directly into Word, starting with 2003, or <a href="http://www.tkachenko.com...
0
2009-06-23T21:02:02Z
[ "python", "xml", "xslt", "ms-word" ]