title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
python conditional lock
625,491
<p>How can I implement conditional lock in threaded application, for instance I haw 30 threads that are calling function and for most off the time all threads can access is simultaneous, but depending on function input there can be condition when only one thread can do that one thing. (If value for input is repeated a...
0
2009-03-09T09:03:54Z
625,499
<p>Try this: have a lock in the module where your function is, and if the input to the function is such that locking is required, acquire the lock inside the function. Otherwise don't.</p> <pre><code>l = threading.RLock() def fn(arg): if arg == arg_that_needs_lock: l.acquire() try: # d...
5
2009-03-09T09:07:58Z
[ "python", "multithreading" ]
python conditional lock
625,491
<p>How can I implement conditional lock in threaded application, for instance I haw 30 threads that are calling function and for most off the time all threads can access is simultaneous, but depending on function input there can be condition when only one thread can do that one thing. (If value for input is repeated a...
0
2009-03-09T09:03:54Z
625,554
<p>It sounds like you want something similar to a <a href="http://en.wikipedia.org/wiki/Readers-writer%5Flock" rel="nofollow">Readers-Writer lock</a>.</p> <p>This is probably not what you want, but might be a clue:</p> <pre><code>from __future__ import with_statement import threading def RWLock(readers = 1, writers ...
1
2009-03-09T09:31:39Z
[ "python", "multithreading" ]
wxpython auinotebook close tab event
625,714
<p>What event is used when I close a tab in an auinotebook? I tested with EVT_AUINOTEBOOK_PAGE_CLOSE(D). It didn't work.</p> <p>I would also like to fire a right click on the tab itself event.</p> <p>Where can I find all the events that can be used with the aui manager/notebook? Might just be my poor searching skills...
7
2009-03-09T10:26:40Z
625,851
<p>This is the bind command you want:</p> <pre><code>self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSED, self.close, self.nb) </code></pre> <p>To detect a right click on the tab (e.g. to show a custom context menu):</p> <pre><code>self.Bind(wx.aui.EVT_AUINOTEBOOK_TAB_RIGHT_DOWN, self.right, self.nb) </code></pre> <p>Here...
8
2009-03-09T11:17:31Z
[ "python", "wxpython", "wxwidgets" ]
How can I pass all the parameters to a decorator?
625,786
<p>I tried to trace the execution of some methods using a decorator. Here is the decorator code:</p> <pre class="lang-py prettyprint-override"><code>def trace(func): def ofunc(*args): func_name = func.__name__ xargs = args print "entering %s with args %s" % (func_name,xargs) ...
2
2009-03-09T10:51:18Z
625,806
<p>This line is incorrect:</p> <pre><code>ret_val = func(args) </code></pre> <p>You're forgetting to expand the argument list when you're passing it on. It should be:</p> <pre><code>ret_val = func(*args) </code></pre> <p>Sample output with this modification in place:</p> <pre><code>&gt;&gt;&gt; class Test2: ... ...
6
2009-03-09T10:58:51Z
[ "python", "decorator" ]
Store Django form.cleaned_data in null model field?
625,977
<p>I have a django model, which has a int field (with null=True, blank=True). Now when I get a form submit from the user, I assign it like so:</p> <pre><code>my_model.width= form.cleaned_data['width'] </code></pre> <p>However sometimes I get an error:</p> <pre><code>ValueError: invalid literal for int() with base 10...
2
2009-03-09T12:08:52Z
626,130
<p>No, it doesn't. If you want to assign NULL, use Python's <code>None</code>. Otherwise Django will try to parse a number from the string and that fails for the empty string. </p> <p>You can use the <code>or</code> construct to achieve this:</p> <pre><code>my_model.width = form.cleaned_data['width'] or None </code><...
5
2009-03-09T13:03:09Z
[ "python", "django" ]
Loading bundled python framework dependencies using only python
626,157
<p>I've come across <a href="http://stackoverflow.com/questions/331377/">this question</a> but I don't like the solution that is presented. Shell scripting is operating system dependent. </p> <p>Is there a python solution to this problem?</p> <p>I'm not looking for python to machine code compilers, just a way to modi...
1
2009-03-09T13:09:37Z
626,180
<p>Generally speaking, python follows the paths in sys.path when trying to resolve library dependencies. <code>sys.path</code> is a list, and it is searched in order. If your application modified <code>sys.path</code> on load to put its own library paths at the front, this should do the trick.</p> <p>This section from...
3
2009-03-09T13:17:18Z
[ "python" ]
Loading bundled python framework dependencies using only python
626,157
<p>I've come across <a href="http://stackoverflow.com/questions/331377/">this question</a> but I don't like the solution that is presented. Shell scripting is operating system dependent. </p> <p>Is there a python solution to this problem?</p> <p>I'm not looking for python to machine code compilers, just a way to modi...
1
2009-03-09T13:09:37Z
626,204
<p>Well, as far as I know there are many different pieces of advice on how to do it. </p> <p>Here are 2 approaches that may help you distribute your app</p> <p>I/</p> <p>. include all dependencies in my project directory structure</p> <p>. manipulate the sys.path to make sure that the app is always looking in my di...
2
2009-03-09T13:22:42Z
[ "python" ]
File editing in python
626,617
<p>I wrote a little python program as a personal utility to help me with some refactoring. It's similar to unix replace, except it supports regular expressions and operates on all files in a directory and (optionally) all subdirectories.</p> <p>The problem is I'm not replacing in place. I'm opening files, passing th...
3
2009-03-09T15:12:30Z
626,638
<p>Freaks out how? What you're describing, if it's working, <em>is</em> editing the file "in place", at least as much as <em>vi(1)</em> does.</p>
0
2009-03-09T15:16:23Z
[ "python", "svn" ]
File editing in python
626,617
<p>I wrote a little python program as a personal utility to help me with some refactoring. It's similar to unix replace, except it supports regular expressions and operates on all files in a directory and (optionally) all subdirectories.</p> <p>The problem is I'm not replacing in place. I'm opening files, passing th...
3
2009-03-09T15:12:30Z
626,649
<p>Try 'file = open('path/to/file', 'w+')'. This means you are updating an existing file, not writing a new one.</p>
0
2009-03-09T15:18:45Z
[ "python", "svn" ]
File editing in python
626,617
<p>I wrote a little python program as a personal utility to help me with some refactoring. It's similar to unix replace, except it supports regular expressions and operates on all files in a directory and (optionally) all subdirectories.</p> <p>The problem is I'm not replacing in place. I'm opening files, passing th...
3
2009-03-09T15:12:30Z
626,666
<p>I suspect the problem is that you are in fact editing wrong files. Subversion should never raise any errors about check sums when you are just modifying your tracked files -- independently of <em>how</em> you are modifying them.</p> <p>Maybe you are accidentally editing files in the <code>.svn</code> directory? In ...
8
2009-03-09T15:21:22Z
[ "python", "svn" ]
File editing in python
626,617
<p>I wrote a little python program as a personal utility to help me with some refactoring. It's similar to unix replace, except it supports regular expressions and operates on all files in a directory and (optionally) all subdirectories.</p> <p>The problem is I'm not replacing in place. I'm opening files, passing th...
3
2009-03-09T15:12:30Z
626,667
<p>What do you mean by "SVN freaks out"?</p> <p>Anyway, the way vi/emacs/etc works is as follows:</p> <pre><code>f = open("/path/to/.file.tmp", "w") f.write(out_string) f.close() os.rename("/path/to/.file.tmp", "/path/to/file") </code></pre> <p>(ok, there's actually an "fsync" in there... But I don't know off hand h...
2
2009-03-09T15:22:11Z
[ "python", "svn" ]
File editing in python
626,617
<p>I wrote a little python program as a personal utility to help me with some refactoring. It's similar to unix replace, except it supports regular expressions and operates on all files in a directory and (optionally) all subdirectories.</p> <p>The problem is I'm not replacing in place. I'm opening files, passing th...
3
2009-03-09T15:12:30Z
627,369
<p>Perhaps the <code>fileinput</code> module can make your code simpler/shorter:</p> <p>Here's an example:</p> <pre><code>import fileinput for line in fileinput.input("test.txt", inplace=1): print "%d: %s" % (fileinput.filelineno(), line), </code></pre>
1
2009-03-09T18:03:39Z
[ "python", "svn" ]
File editing in python
626,617
<p>I wrote a little python program as a personal utility to help me with some refactoring. It's similar to unix replace, except it supports regular expressions and operates on all files in a directory and (optionally) all subdirectories.</p> <p>The problem is I'm not replacing in place. I'm opening files, passing th...
3
2009-03-09T15:12:30Z
627,374
<p>I suspect <a href="http://stackoverflow.com/questions/626617/file-editing-in-python/626666#626666">Ferdinand's answer</a>, that you are recursing into the .svn dir, explains why you are messing up SVN, but note that there is another flaw in the way you are processing files.</p> <p>If your program is killed, or your...
0
2009-03-09T18:05:26Z
[ "python", "svn" ]
Validating a Unicode Name
626,697
<p>In ASCII, validating a name isn't too difficult: just make sure all the characters are alphabetical.</p> <p>But what about in Unicode (utf-8) ? How can I make sure there are no commas or underscores (outside of ASCII scope) in a given string?</p> <p>(ideally in Python)</p>
3
2009-03-09T15:30:47Z
626,721
<p>Depending on how you define "name", you could go with checking it against this regex:</p> <pre><code>^\w+$ </code></pre> <p>However, this will allow numbers and underscores. To rule them out, you can do a second test against:</p> <pre><code>[\d_] </code></pre> <p>and make your check fail on match. These two coul...
1
2009-03-09T15:35:53Z
[ "python", "unicode", "validation", "character-properties" ]
Validating a Unicode Name
626,697
<p>In ASCII, validating a name isn't too difficult: just make sure all the characters are alphabetical.</p> <p>But what about in Unicode (utf-8) ? How can I make sure there are no commas or underscores (outside of ASCII scope) in a given string?</p> <p>(ideally in Python)</p>
3
2009-03-09T15:30:47Z
626,722
<p>The <code>letters</code> property of the <code>string</code> module should give you what you want. This property is locale-specific, so as long as you know the language of the text being passed to you, you can use <code>setlocale()</code> and validate against those characters.</p> <p><a href="http://docs.python.org...
0
2009-03-09T15:35:58Z
[ "python", "unicode", "validation", "character-properties" ]
Validating a Unicode Name
626,697
<p>In ASCII, validating a name isn't too difficult: just make sure all the characters are alphabetical.</p> <p>But what about in Unicode (utf-8) ? How can I make sure there are no commas or underscores (outside of ASCII scope) in a given string?</p> <p>(ideally in Python)</p>
3
2009-03-09T15:30:47Z
626,745
<p>Maybe the <a href="http://docs.python.org/library/unicodedata.html" rel="nofollow">unicodedata module</a> is useful for this task. Especially the <code>category()</code> function. For existing unicode categories look at <a href="http://unicode.org/Public/UNIDATA/UCD.html#General%5FCategory%5FValues" rel="nofollow">u...
5
2009-03-09T15:39:52Z
[ "python", "unicode", "validation", "character-properties" ]
Validating a Unicode Name
626,697
<p>In ASCII, validating a name isn't too difficult: just make sure all the characters are alphabetical.</p> <p>But what about in Unicode (utf-8) ? How can I make sure there are no commas or underscores (outside of ASCII scope) in a given string?</p> <p>(ideally in Python)</p>
3
2009-03-09T15:30:47Z
626,790
<p>Just convert bytestring (your utf-8) to unicode objects and check if all characters are alphabetic:</p> <pre><code>s.isalpha() </code></pre> <p>This method is locale-dependent for bytestrings.</p>
5
2009-03-09T15:46:43Z
[ "python", "unicode", "validation", "character-properties" ]
Validating a Unicode Name
626,697
<p>In ASCII, validating a name isn't too difficult: just make sure all the characters are alphabetical.</p> <p>But what about in Unicode (utf-8) ? How can I make sure there are no commas or underscores (outside of ASCII scope) in a given string?</p> <p>(ideally in Python)</p>
3
2009-03-09T15:30:47Z
627,864
<p>This might be a step towards a solution:</p> <pre><code>import unicodedata EXCEPTIONS= frozenset(u"'.") CATEGORIES= frozenset( ('Lu', 'Ll', 'Lt', 'Pd', 'Zs') ) # O'Rourke, Franklin D. Roosevelt def test_unicode_name(unicode_name): return all( uchar in EXCEPTIONS or unicodedata.category(uchar) in ...
1
2009-03-09T20:21:06Z
[ "python", "unicode", "validation", "character-properties" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
626,764
<p>Lists are mutable; tuples are not.</p> <p>From <a href="http://docs.python.org/2/tutorial/datastructures.html">docs.python.org/2/tutorial/datastructures.html</a> </p> <blockquote> <p>Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (see later in th...
24
2009-03-09T15:41:59Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
626,768
<p>The key difference is that tuples are immutable. This means that you cannot change the values in a tuple once you have created it.</p> <p>So if you're going to need to change the values use a List.</p> <p>Benefits to tuples:</p> <ol> <li>Slight performance improvement.</li> <li>As a tuple is immutable it can be ...
54
2009-03-09T15:42:38Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
626,871
<p>Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. <strong>Tuples have structure, lists have order.</strong> </p> <p>Using this distinctio...
636
2009-03-09T16:02:51Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
627,165
<p>If you went for a walk, you could note your coordinates at any instant in an (x,y) tuple.</p> <p>If you wanted to record your journey, you could append your location every few seconds to a list.</p> <p>But you couldn't do it the other way around.</p>
136
2009-03-09T17:14:47Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
627,901
<p>Lists are intended to be homogeneous sequences, while tuples are heterogeneous data structures. </p>
8
2009-03-09T20:29:55Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
16,097,958
<p>Lists are for looping, tuples are for structures i.e. <code>"%s %s" %tuple</code>.</p> <p>Lists are usually homogeneous, tuples are usually heterogeneous. </p> <p>Lists are for variable length, tuples are for fixed length.</p>
11
2013-04-19T05:43:14Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
16,960,687
<p>The values of <strong>list</strong> can be changed any time but the values of <strong>tuples</strong> can't be change.</p> <p>The <strong>advantages and disadvantages</strong> depends upon the use. If you have such a data which you never want to change then you should have to use tuple, otherwise list is the best o...
4
2013-06-06T11:14:54Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
18,181,253
<p>It's <a href="http://stackoverflow.com/a/626871/915501">been mentioned</a> that the difference is largely semantic: people expect a tuple and list to represent different information. But this goes further than a guideline, some libraries actually behave differently based on what they are passed. Take numpy for examp...
10
2013-08-12T07:06:24Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
18,892,969
<p>Difference between list and tuple</p> <ol> <li><p><strong>Literal</strong></p> <pre><code>someTuple = (1,2) someList = [1,2] </code></pre></li> <li><p><strong>Size</strong></p> <pre><code>a = tuple(range(1000)) b = list(range(1000)) a.__sizeof__() # 8024 b.__sizeof__() # 9088 </code></pre> <p>Due to the small...
157
2013-09-19T11:07:28Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
26,128,173
<p>List is mutable and tuples is immutable. The main difference between mutable and immutable is memory usage when you are trying to append an item. </p> <p>When you create a variable, some fixed memory is assigned to the variable. If it is a list, more memory is assigned than actually used. E.g. if current memory ass...
-1
2014-09-30T19:01:37Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
36,156,178
<p>This is an example of Python lists:</p> <pre><code>my_list = [0,1,2,3,4] top_rock_list = ["Bohemian Rhapsody","Kashmir","Sweet Emotion", "Fortunate Son"] </code></pre> <p>This is an example of Python tuple:</p> <pre><code>my_tuple = (a,b,c,d,e) celebrity_tuple = ("John", "Wayne", 90210, "Actor", "Male", "Dead") <...
6
2016-03-22T13:48:58Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
36,495,737
<p>First of all, they both are the Non-scalar object (also known as a compound object) in Python.</p> <ul> <li>Tuples, ordered sequence of elements (which can contain any object with no aliasing issue) <ul> <li>Immutable (tuple, int, float, str)</li> <li>Concatenation using <code>+</code> (brand new tuple will be cre...
0
2016-04-08T09:10:36Z
[ "python", "list", "tuples" ]
What's the difference between list and tuples?
626,759
<p>What's the difference?</p> <p>What are the advantages / disadvantages of tuples / lists?</p>
555
2009-03-09T15:41:25Z
39,644,028
<p>The <a href="https://www.python.org/dev/peps/pep-0484/#the-typing-module" rel="nofollow">PEP 484 -- Type Hints</a> says that the types of elements of a <code>tuple</code> can be individually typed; so that you can say <code>Tuple[str, int, float]</code>; but a <code>list</code>, with <code>List</code> typing class c...
0
2016-09-22T16:13:04Z
[ "python", "list", "tuples" ]
How do I find the Windows common application data folder using Python?
626,796
<p>I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?</p>
28
2009-03-09T15:48:55Z
626,841
<p>Take a look at <a href="http://ginstrom.com/code/winpaths.html">http://ginstrom.com/code/winpaths.html</a>. This is a simple module that will retrieve Windows folder information. The module implements <code>get_common_appdata</code> to get the App Data folder for all users.</p>
10
2009-03-09T15:58:23Z
[ "python", "windows", "application-data", "common-files" ]
How do I find the Windows common application data folder using Python?
626,796
<p>I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?</p>
28
2009-03-09T15:48:55Z
626,848
<p><em>Previous answer removed due to incompatibility with non-US versions of Windows, and Vista.</em></p> <p><strong>EDIT:</strong> To expand on Out Into Space's answer, you would use the <code>winpaths.get_common_appdata</code> function. You can get winpaths using <code>easy_install winpaths</code> or by going to...
3
2009-03-09T15:59:26Z
[ "python", "windows", "application-data", "common-files" ]
How do I find the Windows common application data folder using Python?
626,796
<p>I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?</p>
28
2009-03-09T15:48:55Z
626,927
<p>If you don't want to add a dependency for a third-party module like winpaths, I would recommend using the environment variables already available in Windows: </p> <ul> <li><a href="http://windowsitpro.com/article/articleid/23873/what-environment-variables-are-available-in-windows.html"><strong>What environment vari...
37
2009-03-09T16:12:41Z
[ "python", "windows", "application-data", "common-files" ]
How do I find the Windows common application data folder using Python?
626,796
<p>I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?</p>
28
2009-03-09T15:48:55Z
627,071
<p>You can access all of your OS environment variables using the <code>os.environ</code> dictionary in the <code>os</code> module. Choosing which key to use from that dictionary could be tricky, though. In particular, you should remain aware of internationalized (i.e., non-English) versions of Windows when using thes...
3
2009-03-09T16:51:46Z
[ "python", "windows", "application-data", "common-files" ]
How do I find the Windows common application data folder using Python?
626,796
<p>I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?</p>
28
2009-03-09T15:48:55Z
1,448,255
<p>From <a href="http://snipplr.com/view.php?codeview&amp;id=7354" rel="nofollow">http://snipplr.com/view.php?codeview&amp;id=7354</a></p> <pre><code>homedir = os.path.expanduser('~') # ...works on at least windows and linux. # In windows it points to the user's folder # (the one directly under Documents and Setti...
10
2009-09-19T09:57:14Z
[ "python", "windows", "application-data", "common-files" ]
How do I find the Windows common application data folder using Python?
626,796
<p>I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?</p>
28
2009-03-09T15:48:55Z
20,079,912
<p>Since <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181.aspx" rel="nofollow">SHGetFolderPath</a> is deprecated, you can also use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb762188.aspx" rel="nofollow">SHGetKnownFolderPath</a> in Vista and newer. This also lets you look ...
0
2013-11-19T19:05:26Z
[ "python", "windows", "application-data", "common-files" ]
Is it possible to write to a python frame object as returned by sys._getframe() from python code running within the interpreter?
626,835
<p>Apropos of <a href="http://stackoverflow.com/questions/541329/is-it-possible-to-programmatically-construct-a-python-stack-frame-and-start-execu">This question</a>, there is a bit of scaffolding within the interpreter to inspect frame objects, which can be retrieved by <code>sys._getframe()</code>. The frame objects...
1
2009-03-09T15:57:39Z
626,939
<p>From CPython source, <code>Objects/frameobject.c</code>:</p> <pre class="lang-c prettyprint-override"><code>static PyMemberDef frame_memberlist[] = { {"f_back", T_OBJECT, OFF(f_back), RO}, {"f_code", T_OBJECT, OFF(f_code), RO}, {"f_builtins", T_OBJECT, OFF(f_builtins),...
7
2009-03-09T16:15:24Z
[ "python", "introspection", "frame", "sys" ]
Is it possible to write to a python frame object as returned by sys._getframe() from python code running within the interpreter?
626,835
<p>Apropos of <a href="http://stackoverflow.com/questions/541329/is-it-possible-to-programmatically-construct-a-python-stack-frame-and-start-execu">This question</a>, there is a bit of scaffolding within the interpreter to inspect frame objects, which can be retrieved by <code>sys._getframe()</code>. The frame objects...
1
2009-03-09T15:57:39Z
627,145
<p>The f_locals['foo'] example by NXC works because the code is in module scope. In that case, f_locals is f_globals, and f_globals is both modifiable and modifications are reflected in the module.</p> <p>Inside of function scope, locals() and f_locals are writable, but "[changes may not affect the values of local var...
1
2009-03-09T17:08:37Z
[ "python", "introspection", "frame", "sys" ]
How to remove an element from a list by index in Python?
627,435
<p>How to remove an element from a list by index in Python?</p> <p>I found the <code>list.remove</code> method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.</p>
623
2009-03-09T18:16:11Z
627,441
<p>You probably want <code>pop</code>:</p> <pre><code>a = ['a', 'b', 'c', 'd'] a.pop(1) # now a is ['a', 'c', 'd'] </code></pre> <p>By default, <code>pop</code> without any arguments removes the last item:</p> <pre><code>a = ['a', 'b', 'c', 'd'] a.pop() # now a is ['a', 'b', 'c'] </code></pre>
300
2009-03-09T18:17:48Z
[ "python", "list" ]
How to remove an element from a list by index in Python?
627,435
<p>How to remove an element from a list by index in Python?</p> <p>I found the <code>list.remove</code> method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.</p>
623
2009-03-09T18:16:11Z
627,453
<p>Use <code>del</code> and specify the element you want to delete with the index:</p> <pre><code>In [9]: a = range(10) In [10]: a Out[10]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [11]: del a[-1] In [12]: a Out[12]: [0, 1, 2, 3, 4, 5, 6, 7, 8] </code></pre> <p><a href="http://docs.python.org/tutorial/datastructures.html#th...
730
2009-03-09T18:21:24Z
[ "python", "list" ]
How to remove an element from a list by index in Python?
627,435
<p>How to remove an element from a list by index in Python?</p> <p>I found the <code>list.remove</code> method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.</p>
623
2009-03-09T18:16:11Z
19,466,364
<p><a href="https://docs.python.org/library/stdtypes.html#mutable-sequence-types"><code>pop</code></a> is also useful to remove and keep an item from a list. Where <code>del</code> actually trashes the item.</p> <pre><code>&gt;&gt;&gt; x = [1, 2, 3, 4] &gt;&gt;&gt; p = x.pop(1) &gt;&gt;&gt; p 2 </code></pre>
31
2013-10-19T13:10:02Z
[ "python", "list" ]
How to remove an element from a list by index in Python?
627,435
<p>How to remove an element from a list by index in Python?</p> <p>I found the <code>list.remove</code> method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.</p>
623
2009-03-09T18:16:11Z
24,352,671
<p>Like others mentioned pop and del are <em>the</em> efficient ways to remove an item of given index. Yet just for the sake of completion ( since the same thing can be done via many ways in python ):</p> <p><strong>Using slices ( This does not do inplace removal of item from original list ) :</strong></p> <p>( Also ...
41
2014-06-22T15:21:00Z
[ "python", "list" ]
How to remove an element from a list by index in Python?
627,435
<p>How to remove an element from a list by index in Python?</p> <p>I found the <code>list.remove</code> method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.</p>
623
2009-03-09T18:16:11Z
32,948,546
<p>Generally, I am using the following method:</p> <pre><code>&gt;&gt;&gt; myList = [10,20,30,40,50] &gt;&gt;&gt; rmovIndxNo = 3 &gt;&gt;&gt; del myList[rmovIndxNo] &gt;&gt;&gt; myList [10, 20, 30, 50] </code></pre>
3
2015-10-05T12:23:32Z
[ "python", "list" ]
How to remove an element from a list by index in Python?
627,435
<p>How to remove an element from a list by index in Python?</p> <p>I found the <code>list.remove</code> method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.</p>
623
2009-03-09T18:16:11Z
35,122,409
<p>One can either use del or pop, but I prefer del, since you can specify index and slices, giving the user more control over the data.</p>
0
2016-02-01T03:00:01Z
[ "python", "list" ]
How to remove an element from a list by index in Python?
627,435
<p>How to remove an element from a list by index in Python?</p> <p>I found the <code>list.remove</code> method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.</p>
623
2009-03-09T18:16:11Z
38,066,478
<p>As previously mentioned, best practice is del(); or pop() if you need to know the value.</p> <p>An alternate solution is to re-stack only those elements you want:</p> <pre><code> a = ['a', 'b', 'c', 'd'] def remove_element(list_,index_): clipboard = [] for i in range(len(list_)): ...
0
2016-06-28T03:15:54Z
[ "python", "list" ]
How can I use named arguments in a decorator?
627,501
<p>If I have the following function:</p> <pre><code> def intercept(func): # do something here @intercept(arg1=20) def whatever(arg1,arg2): # do something here </code></pre> <p>I would like for intercept to fire up only when <strong>arg1</strong> is 20. I would like to be able to pass named parameters to the func...
4
2009-03-09T18:37:20Z
627,519
<pre><code>from functools import wraps def intercept(target,**trigger): def decorator(func): names = getattr(func,'_names',None) if names is None: code = func.func_code names = code.co_varnames[:code.co_argcount] @wraps(func) def decorated(*args,**kwargs): ...
7
2009-03-09T18:43:30Z
[ "python", "language-features", "decorator" ]
How can I use named arguments in a decorator?
627,501
<p>If I have the following function:</p> <pre><code> def intercept(func): # do something here @intercept(arg1=20) def whatever(arg1,arg2): # do something here </code></pre> <p>I would like for intercept to fire up only when <strong>arg1</strong> is 20. I would like to be able to pass named parameters to the func...
4
2009-03-09T18:37:20Z
627,527
<p>You can do this by using *args and **kwargs in the decorator:</p> <pre><code>def intercept(func, *dargs, **dkwargs): def intercepting_func(*args, **kwargs): if (&lt;some condition on dargs, dkwargs, args and kwargs&gt;): print 'I intercepted you.' return func(*args, **kwargs) ret...
3
2009-03-09T18:45:26Z
[ "python", "language-features", "decorator" ]
How can I use named arguments in a decorator?
627,501
<p>If I have the following function:</p> <pre><code> def intercept(func): # do something here @intercept(arg1=20) def whatever(arg1,arg2): # do something here </code></pre> <p>I would like for intercept to fire up only when <strong>arg1</strong> is 20. I would like to be able to pass named parameters to the func...
4
2009-03-09T18:37:20Z
628,309
<p>Remember that</p> <pre><code>@foo def bar(): pass </code></pre> <p>is equivalent to:</p> <pre><code>def bar(): pass bar = foo(bar) </code></pre> <p>so if you do:</p> <pre><code>@foo(x=3) def bar(): pass </code></pre> <p>that's equivalent to:</p> <pre><code>def bar(): pass bar = foo(x=3)(bar) <...
10
2009-03-09T22:38:50Z
[ "python", "language-features", "decorator" ]
Form Field API?
627,583
<p>I am iterating through list of form fields. How do I identify the type of each field? For checkbox I can call field.is_checkbox...are there similar methods for lists, multiplechoicefields etc. ?</p> <p>Thanks </p>
2
2009-03-09T19:01:00Z
627,592
<p>Presuming you're using HTML here... Because it isn't very clear.</p> <p>How about giving it an extra class.</p> <p>And if you didn't know allready, the class attribute will recognise this:</p> <pre><code>class="hello there you" </code></pre> <p>as having 3 classes. The class 'hello', the class 'there', and the c...
-1
2009-03-09T19:03:52Z
[ "python", "django", "django-forms" ]
Form Field API?
627,583
<p>I am iterating through list of form fields. How do I identify the type of each field? For checkbox I can call field.is_checkbox...are there similar methods for lists, multiplechoicefields etc. ?</p> <p>Thanks </p>
2
2009-03-09T19:01:00Z
627,622
<p>Have a look at the class for each field on your form:</p> <pre><code>for f_name, f_type in my_form_instance.fields.items(): print "I am a ",type(f_type) # or f_type.__class__ </code></pre> <p>This will produce output similar to <code>&lt;class 'django.forms.fields.BooleanField'&gt;</code>.</p> <p>You can ...
3
2009-03-09T19:13:14Z
[ "python", "django", "django-forms" ]
Form Field API?
627,583
<p>I am iterating through list of form fields. How do I identify the type of each field? For checkbox I can call field.is_checkbox...are there similar methods for lists, multiplechoicefields etc. ?</p> <p>Thanks </p>
2
2009-03-09T19:01:00Z
627,642
<p>I am not sure if this is what you want, but if you want to know what kind of field it is going to end up being in the HTML, you can check it with this:</p> <pre><code>{% for field in form %} {{ field.field.widget.input_type }} {% endfor %} </code></pre> <p>widget.input_type will hold text, password, select, et...
1
2009-03-09T19:22:29Z
[ "python", "django", "django-forms" ]
Using Python to get Windows system internals info
627,596
<p>I'd like to write some quick Python code to assess the CPU, memory, disk, and networking usage of my Windows XP system. </p> <p>Are there existing Python libraries that would allow me to access that information? Or, are there DLL's that I can call from Python? (If so, a code sample would be appreciated)</p>
1
2009-03-09T19:06:09Z
627,619
<p>I think WMI is the resource to use. Especially, look at the <a href="http://msdn.microsoft.com/en-us/library/aa394084%28VS.85%29.aspx" rel="nofollow"><code>Win32_PerfFormattedData*</code> classes in the MSDN</a>.</p> <p>A quick search turned this up (among others):</p> <p><a href="http://timgolden.me.uk/python/wmi...
4
2009-03-09T19:12:58Z
[ "python", "windows-xp" ]
Using Python to get Windows system internals info
627,596
<p>I'd like to write some quick Python code to assess the CPU, memory, disk, and networking usage of my Windows XP system. </p> <p>Are there existing Python libraries that would allow me to access that information? Or, are there DLL's that I can call from Python? (If so, a code sample would be appreciated)</p>
1
2009-03-09T19:06:09Z
628,334
<p>The MS <a href="http://www.microsoft.com/downloads/details.aspx?DisplayLang=en&amp;FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178" rel="nofollow">Scriptomatic</a> tool can generate WMI scripts in Python as well as VBScript, JScript and Perl.</p>
0
2009-03-09T22:47:46Z
[ "python", "windows-xp" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,731
<p>I'd go with Perl. Not everyone will agree with me here, but it's a great language well suited to system administration work, and it'll expose you to some more functional programming constructs. It's a great language for learning how to use the smallest amount of code for a given task, as well.</p> <p>For the usage ...
10
2009-03-09T19:48:40Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,764
<p>Honestly, the "majority" of my programming has been in Perl and PHP and I recently decided to do my latest project in Python, and I must admit it is very nice to program with. I was hesitant of the whole no curly braces thing as that's what I've always done, but it is really very clean. At the end of the day, though...
11
2009-03-09T19:56:31Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,788
<p>I have no experience with Python. I vouch strongly to learn Perl, not out of attrition, but because there is a TON to learn in the platform. The key concepts of Perl are: Do What I Mean (<a href="http://en.wikipedia.org/wiki/DWIM" rel="nofollow">DWIM</a>) and There's More Than One Way To Do It (<a href="http://en....
7
2009-03-09T20:01:48Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,790
<p>As a Perl programmer, I would normally say Perl. But coming from PHP, I think Perl is too similar and you won't actually get that much out of it. (Not because there isn't a lot to learn, but you are likely to program in Perl using the same style as you program in PHP.)</p> <p>I'd suggest something completely diff...
3
2009-03-09T20:02:01Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,794
<p>I haven't worked with Python much, but I can tell why I <em>didn't</em> like about Perl when I used it.</p> <ol> <li>OO support feels tacked on. OO in perl is very different from OO support in the other languages I've used (which include things like PHP, Java, and C#)</li> <li>TMTOWTDI (There's More Than One Way T...
7
2009-03-09T20:02:32Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,862
<p>Perl is a very nice language and CPAN has a ton of mature modules that will save you a lot of time. Furthermore, Perl is really moving forwards nowadays with a lot of interesting projects (unlike what uninformed fanboys like to spread around). Even a <a href="http://rakudo.org/">Perl 6 implementation</a> is by now r...
24
2009-03-09T20:20:42Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,879
<p>Why isn't there Ruby on your list? Maybe you should give it a try.</p>
2
2009-03-09T20:23:34Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,893
<p>If those 2 are your only choices, I would choose Python.</p> <p>Otherwise you should learn javascript. </p> <p>No I mean <em>really</em> learn it...</p>
1
2009-03-09T20:27:15Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
627,972
<p>"I'd like to follow OOP structure..." advocates for Python or, even more so if you're open, Ruby. On the other hand, in terms of existing libraries, the order is probably Perl > Python >> Ruby. In terms of your career, Perl on your resume is unlikely to make you stand out, while Python and Ruby may catch the eye of ...
2
2009-03-09T20:46:48Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
628,181
<p>If you won't be doing web development with this language, either of them would do. If you are, you may find that doing web development in perl is a bit more complicated, since all of the frameworks require more knowledge of the language. You can do nice things in both, but my opinion is that perl allows more rapid d...
1
2009-03-09T21:55:09Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
628,947
<p>I suggest going through a beginner tutorial of each and decide for yourself which fits you better. You'll find you can do what you need to do in either:</p> <p><strong><a href="http://docs.python.org/tutorial/index.html" rel="nofollow">Python Tutorial</a></strong> (<a href="http://docs.python.org/tutorial/classes....
3
2009-03-10T04:33:54Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
629,604
<p>I recently made the step from Perl over to Python, after a couple of Perl-only years. Soon thereafter I discovered I had started to read through all kinds of Python-code just as it were any other easy to read text — something I've never done with Perl. Having to delve into third-party Perl code has always been kin...
7
2009-03-10T10:32:52Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
632,119
<p>Python is clean and elegant, and the fact that LOTS of C APIs have been wrapped, gives you powerful hooks to much. I also like the "<a href="http://www.python.org/dev/peps/pep-0020/">Zen of Python</a>".</p> <ul> <li>Beautiful is better than ugly.</li> <li>Explicit is better than implicit.</li> <li>Simple is better ...
5
2009-03-10T20:24:03Z
[ "python", "perl" ]
Next step after PHP: Perl or Python?
627,721
<p>It might seem it has been asked numerous times, but in fact it hasn't. I did my research, and now I'm eager to hear <em>others' opinions</em>.</p> <p>I have experience with <strong>PHP 5</strong>, both with functional and object oriented programming methods. I created a few feature-minimalistic websites.</p> <p>Pr...
20
2009-03-09T19:45:26Z
18,619,414
<p>Every dynamic language is from same family. It does not matter Which is the tool you work with it matter how you do..</p> <p><a href="http://blog.oykko.com/2013/09/python-or-php.html" rel="nofollow">PHP VS PYTHON OT PERL OR RUBY? Stop it</a></p> <p>As many comments mentioned python is cleaner well sometime whose c...
0
2013-09-04T16:40:52Z
[ "python", "perl" ]
Django forms
627,772
<p>I had asked a question pertaining to this. But I think it would be better to ask my question directly. I have a "User" table with manytomany relationship with two other tables "Domain" and "Groups". So in the admin interface I see the Groups and Domains as 2 ModelMultipleChoiceFields. But I want to present them on t...
2
2009-03-09T19:58:35Z
627,936
<p>I think the built-in widget <code>CheckboxSelectMultiple</code> does what you want. If it doesn't, you're going to have to create your own widget. The <a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#ref-forms-widgets" rel="nofollow">documentation for widgets</a> is a good place to start. The easiest...
2
2009-03-09T20:39:20Z
[ "python", "django", "django-admin" ]
Django forms
627,772
<p>I had asked a question pertaining to this. But I think it would be better to ask my question directly. I have a "User" table with manytomany relationship with two other tables "Domain" and "Groups". So in the admin interface I see the Groups and Domains as 2 ModelMultipleChoiceFields. But I want to present them on t...
2
2009-03-09T19:58:35Z
627,997
<p>I'm not completely sure I understand what you are attempting to do, but perhaps something like <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#filter-horizontal" rel="nofollow">filter_horizontal</a> would do what you want.</p>
0
2009-03-09T20:52:04Z
[ "python", "django", "django-admin" ]
Django forms
627,772
<p>I had asked a question pertaining to this. But I think it would be better to ask my question directly. I have a "User" table with manytomany relationship with two other tables "Domain" and "Groups". So in the admin interface I see the Groups and Domains as 2 ModelMultipleChoiceFields. But I want to present them on t...
2
2009-03-09T19:58:35Z
629,235
<p>It actually uses model form by default in the admin. So, you need to overwrite it.</p> <pre><code>from django import forms from django.forms import widgets class DomainForm(forms.ModelForm): field2 = YourField(widget=widgets.CheckboxSelectMultiple) class Meta: model = Domain() fields = ('...
1
2009-03-10T07:36:02Z
[ "python", "django", "django-admin" ]
Django forms
627,772
<p>I had asked a question pertaining to this. But I think it would be better to ask my question directly. I have a "User" table with manytomany relationship with two other tables "Domain" and "Groups". So in the admin interface I see the Groups and Domains as 2 ModelMultipleChoiceFields. But I want to present them on t...
2
2009-03-09T19:58:35Z
630,463
<p>Well to be precise its the widget that Django Admin choses to show in case of ManyToManyField. Here in this case its SelectMultiple widget which you feel, is less user friendly.</p> <p>Well easy part is, you can always chose the widget while using your own ModelForm. But in case you want that in Django Admin you ne...
2
2009-03-10T14:27:24Z
[ "python", "django", "django-admin" ]
Django forms
627,772
<p>I had asked a question pertaining to this. But I think it would be better to ask my question directly. I have a "User" table with manytomany relationship with two other tables "Domain" and "Groups". So in the admin interface I see the Groups and Domains as 2 ModelMultipleChoiceFields. But I want to present them on t...
2
2009-03-09T19:58:35Z
4,285,813
<p>You can change django admin interface field widget</p> <pre><code>from django.forms import widgets class UserAdmin(admin.ModelAdmin): model = User def formfield_for_manytomany(self, db_field, request=None, **kwargs): if db_field.name == 'domain' or db_field.name == 'groups': kwargs['widget...
0
2010-11-26T13:47:34Z
[ "python", "django", "django-admin" ]
Python dlopen/dlfunc/dlsym wrappers
627,786
<p>Anybody knows if actually exists a wrapper or ported library to access to Unix dynamic linker on Python?</p>
3
2009-03-09T20:01:36Z
627,820
<p>Would <a href="http://docs.python.org/library/ctypes.html">ctypes</a> do what you want?</p>
7
2009-03-09T20:09:46Z
[ "python", "linker", "dlopen" ]
Python dlopen/dlfunc/dlsym wrappers
627,786
<p>Anybody knows if actually exists a wrapper or ported library to access to Unix dynamic linker on Python?</p>
3
2009-03-09T20:01:36Z
627,829
<p>The module is called <a href="http://docs.python.org/library/dl.html" rel="nofollow">dl</a>:</p> <pre><code>&gt;&gt;&gt; import dl &gt;&gt;&gt; dl.open("libfoo.so") &lt;dl.dl object at 0xb7f580c0&gt; &gt;&gt;&gt; dl.open("libfoo.so").sym('bar') 1400432 </code></pre> <p>... though it's nasty and you might want to c...
1
2009-03-09T20:12:31Z
[ "python", "linker", "dlopen" ]
Django Form Preview - How to work with 'cleaned_data'
628,132
<p>Thanks to Insin for answering a previous <a href="http://stackoverflow.com/questions/625800/django-form-preview-adding-the-user-to-the-form-before-save">question</a> related to this one.</p> <p>His answer worked and works well, however, I'm perplexed at the provision of 'cleaned_data', or more precisely, how to use...
3
2009-03-09T21:39:21Z
628,470
<p>I've never tried what you're doing here with a ModelForm before, but you might be able to use the ** operator to expand your cleaned_data dictionary into the keyword arguments expected for your Registration constructor:</p> <pre><code> registration = Registration (**cleaned_data) </code></pre> <p>The constructor...
9
2009-03-09T23:46:11Z
[ "python", "django", "django-models", "django-forms" ]
How to run an operation on a collection in Python and collect the results?
628,150
<p>How to run an operation on a collection in Python and collect the results?</p> <p>So if I have a list of 100 numbers, and I want to run a function like this for each of them:</p> <pre><code>Operation ( originalElement, anotherVar ) # returns new number. </code></pre> <p>and collect the result like so:</p> <p>res...
4
2009-03-09T21:45:56Z
628,159
<p><a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" rel="nofollow">List comprehensions.</a> In Python they look something like:</p> <pre><code>a = [f(x) for x in bar] </code></pre> <p>Where f(x) is some function and bar is a sequence.</p> <p>You can define f(x) as a partially applie...
10
2009-03-09T21:48:40Z
[ "python", "list", "lambda" ]
How to run an operation on a collection in Python and collect the results?
628,150
<p>How to run an operation on a collection in Python and collect the results?</p> <p>So if I have a list of 100 numbers, and I want to run a function like this for each of them:</p> <pre><code>Operation ( originalElement, anotherVar ) # returns new number. </code></pre> <p>and collect the result like so:</p> <p>res...
4
2009-03-09T21:45:56Z
628,178
<p><a href="http://docs.python.org/tutorial/datastructures.html" rel="nofollow">List comprehensions</a>, <a href="http://www.python.org/dev/peps/pep-0289/" rel="nofollow">generator expressions</a>, <a href="http://docs.python.org/library/functions.html#reduce" rel="nofollow">reduce function</a>.</p>
0
2009-03-09T21:54:18Z
[ "python", "list", "lambda" ]
How to run an operation on a collection in Python and collect the results?
628,150
<p>How to run an operation on a collection in Python and collect the results?</p> <p>So if I have a list of 100 numbers, and I want to run a function like this for each of them:</p> <pre><code>Operation ( originalElement, anotherVar ) # returns new number. </code></pre> <p>and collect the result like so:</p> <p>res...
4
2009-03-09T21:45:56Z
629,270
<p>Another (somewhat depreciated) method of doing this is:</p> <p>def kevin(v): return v*v</p> <p>vals = range(0,100)</p> <p>map(kevin,vals)</p>
1
2009-03-10T07:53:30Z
[ "python", "list", "lambda" ]
Single implementation to cover both single and multiple values in Python?
628,162
<p>Say you have a value like this:</p> <p>n = 5</p> <p>and a method that returns the factorial of it, like so:</p> <p>Factorial ( 5 )</p> <p>How do you handle multiple values:</p> <p>nums = [1,2,3,4,5]</p> <p>Factorial ( nums )</p> <p>so it returns the factorials of all these values as a list.</p> <p>What's the...
4
2009-03-09T21:49:37Z
628,173
<p><a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" rel="nofollow">List comprehension</a>:</p> <pre><code>[fac(n) for n in nums] </code></pre> <p><strong>EDIT:</strong></p> <p>Sorry, I misunderstood, you want a method that handles both sequences and single values? I can't imagine wh...
9
2009-03-09T21:53:08Z
[ "python", "list" ]
Single implementation to cover both single and multiple values in Python?
628,162
<p>Say you have a value like this:</p> <p>n = 5</p> <p>and a method that returns the factorial of it, like so:</p> <p>Factorial ( 5 )</p> <p>How do you handle multiple values:</p> <p>nums = [1,2,3,4,5]</p> <p>Factorial ( nums )</p> <p>so it returns the factorials of all these values as a list.</p> <p>What's the...
4
2009-03-09T21:49:37Z
628,177
<p>If you're asking if Python can do method overloading: no. Hence, doing multi-methods like that is a rather un-Pythonic way of defining a method. Also, naming convention usually upper-cases class names, and lower-cases functions/methods.</p> <p>If you want to go ahead anyway, simplest way would be to just make a bra...
6
2009-03-09T21:54:11Z
[ "python", "list" ]
Single implementation to cover both single and multiple values in Python?
628,162
<p>Say you have a value like this:</p> <p>n = 5</p> <p>and a method that returns the factorial of it, like so:</p> <p>Factorial ( 5 )</p> <p>How do you handle multiple values:</p> <p>nums = [1,2,3,4,5]</p> <p>Factorial ( nums )</p> <p>so it returns the factorials of all these values as a list.</p> <p>What's the...
4
2009-03-09T21:49:37Z
628,186
<pre><code>def Factorial(arg): try: it = iter(arg) except TypeError: pass else: return [Factorial(x) for x in it] return math.factorial(arg) </code></pre> <p>If it's iterable, apply recursivly. Otherwise, proceed normally.</p> <p>Alternatively, you could move the last <code>ret...
13
2009-03-09T21:56:19Z
[ "python", "list" ]
Single implementation to cover both single and multiple values in Python?
628,162
<p>Say you have a value like this:</p> <p>n = 5</p> <p>and a method that returns the factorial of it, like so:</p> <p>Factorial ( 5 )</p> <p>How do you handle multiple values:</p> <p>nums = [1,2,3,4,5]</p> <p>Factorial ( nums )</p> <p>so it returns the factorials of all these values as a list.</p> <p>What's the...
4
2009-03-09T21:49:37Z
628,257
<p>This is done sometimes.</p> <pre><code>def factorial( *args ): def fact( n ): if n == 0: return 1 return n*fact(n-1) return [ fact(a) for a in args ] </code></pre> <p>It gives an almost magical function that works with simple values as well as sequences.</p> <pre><code>&gt;&gt;&gt; factori...
7
2009-03-09T22:20:37Z
[ "python", "list" ]
Single implementation to cover both single and multiple values in Python?
628,162
<p>Say you have a value like this:</p> <p>n = 5</p> <p>and a method that returns the factorial of it, like so:</p> <p>Factorial ( 5 )</p> <p>How do you handle multiple values:</p> <p>nums = [1,2,3,4,5]</p> <p>Factorial ( nums )</p> <p>so it returns the factorials of all these values as a list.</p> <p>What's the...
4
2009-03-09T21:49:37Z
628,265
<p>Or if you don't like the list comprehension syntax, and wish to skip having a new method:</p> <pre><code>def factorial(num): if num == 0: return 1 elif num &gt; 0: return num * factorial(num - 1) else: raise Exception("Negative num has no factorial.") nums = [1, 2, 3, 4, 5] # [1...
3
2009-03-09T22:22:41Z
[ "python", "list" ]
Single implementation to cover both single and multiple values in Python?
628,162
<p>Say you have a value like this:</p> <p>n = 5</p> <p>and a method that returns the factorial of it, like so:</p> <p>Factorial ( 5 )</p> <p>How do you handle multiple values:</p> <p>nums = [1,2,3,4,5]</p> <p>Factorial ( nums )</p> <p>so it returns the factorials of all these values as a list.</p> <p>What's the...
4
2009-03-09T21:49:37Z
628,313
<p>You might want to take a look at NumPy/SciPy's <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html" rel="nofollow">vectorize</a>.</p> <p>In the numpy world, given your single-int-arg Factorial function, you'd do things like</p> <pre><code> vFactorial=np.vectorize(Factorial) vFactor...
3
2009-03-09T22:39:54Z
[ "python", "list" ]
Python equivalent to java.util.SortedSet?
628,192
<p>Does anybody know if Python has an equivalent to Java's SortedSet interface?</p> <p>Heres what I'm looking for: lets say I have an object of type <code>foo</code>, and I know how to compare two objects of type <code>foo</code> to see whether <code>foo1</code> is "greater than" or "less than" <code>foo2</code>. I wa...
19
2009-03-09T21:58:14Z
628,246
<p>If you only need the keys, and no associated value, Python offers sets:</p> <pre><code>s = set(a_list) for k in sorted(s): print k </code></pre> <p>However, you'll be sorting the set each time you do this. If that is too much overhead you may want to look at <a href="http://docs.python.org/library/heapq.html"...
3
2009-03-09T22:16:35Z
[ "python", "data-structures" ]
Python equivalent to java.util.SortedSet?
628,192
<p>Does anybody know if Python has an equivalent to Java's SortedSet interface?</p> <p>Heres what I'm looking for: lets say I have an object of type <code>foo</code>, and I know how to compare two objects of type <code>foo</code> to see whether <code>foo1</code> is "greater than" or "less than" <code>foo2</code>. I wa...
19
2009-03-09T21:58:14Z
628,264
<p>You can use <code>insort</code> from the <a href="http://docs.python.org/library/bisect.html#module-bisect"><code>bisect</code></a> module to insert new elements efficiently in an already sorted list:</p> <pre><code>from bisect import insort items = [1,5,7,9] insort(items, 3) insort(items, 10) print items # -&gt;...
12
2009-03-09T22:22:21Z
[ "python", "data-structures" ]
Python equivalent to java.util.SortedSet?
628,192
<p>Does anybody know if Python has an equivalent to Java's SortedSet interface?</p> <p>Heres what I'm looking for: lets say I have an object of type <code>foo</code>, and I know how to compare two objects of type <code>foo</code> to see whether <code>foo1</code> is "greater than" or "less than" <code>foo2</code>. I wa...
19
2009-03-09T21:58:14Z
628,319
<p>If you're looking for an implementation of an efficient container type for Python implemented using something like a balanced search tree (A Red-Black tree for example) then it's not part of the standard library.</p> <p>I was able to find this, though:</p> <p><a href="http://www.brpreiss.com/books/opus7/" rel="nof...
4
2009-03-09T22:41:52Z
[ "python", "data-structures" ]
Python equivalent to java.util.SortedSet?
628,192
<p>Does anybody know if Python has an equivalent to Java's SortedSet interface?</p> <p>Heres what I'm looking for: lets say I have an object of type <code>foo</code>, and I know how to compare two objects of type <code>foo</code> to see whether <code>foo1</code> is "greater than" or "less than" <code>foo2</code>. I wa...
19
2009-03-09T21:58:14Z
628,432
<p>Take a look at <a href="http://pypi.python.org/pypi?%3Aaction=search&amp;term=btree">BTrees</a>. It look like you need one of them. As far as I understood you need structure that will support relatively cheap insertion of element into storage structure and cheap sorting operation (or even lack of it). BTrees offers ...
11
2009-03-09T23:33:08Z
[ "python", "data-structures" ]
Python equivalent to java.util.SortedSet?
628,192
<p>Does anybody know if Python has an equivalent to Java's SortedSet interface?</p> <p>Heres what I'm looking for: lets say I have an object of type <code>foo</code>, and I know how to compare two objects of type <code>foo</code> to see whether <code>foo1</code> is "greater than" or "less than" <code>foo2</code>. I wa...
19
2009-03-09T21:58:14Z
12,309,435
<p>Use <code>blist.sortedlist</code> from the <a href="http://pypi.python.org/pypi/blist/" rel="nofollow">blist package</a>.</p> <pre><code>from blist import sortedlist z = sortedlist([2, 3, 5, 7, 11]) z.add(6) z.add(3) z.add(10) print z </code></pre> <p>This will output:</p> <pre><code>sortedlist([2, 3, 3, 5, 6, ...
2
2012-09-06T22:36:48Z
[ "python", "data-structures" ]
Python equivalent to java.util.SortedSet?
628,192
<p>Does anybody know if Python has an equivalent to Java's SortedSet interface?</p> <p>Heres what I'm looking for: lets say I have an object of type <code>foo</code>, and I know how to compare two objects of type <code>foo</code> to see whether <code>foo1</code> is "greater than" or "less than" <code>foo2</code>. I wa...
19
2009-03-09T21:58:14Z
20,666,684
<p>Do you have the possibility of using Jython? I just mention it because using TreeMap, TreeSet, etc. is trivial. Also if you're coming from a Java background and you want to head in a Pythonic direction Jython is wonderful for making the transition easier. Though I recognise that use of TreeSet in this case would n...
0
2013-12-18T19:24:11Z
[ "python", "data-structures" ]
Python equivalent to java.util.SortedSet?
628,192
<p>Does anybody know if Python has an equivalent to Java's SortedSet interface?</p> <p>Heres what I'm looking for: lets say I have an object of type <code>foo</code>, and I know how to compare two objects of type <code>foo</code> to see whether <code>foo1</code> is "greater than" or "less than" <code>foo2</code>. I wa...
19
2009-03-09T21:58:14Z
25,988,158
<p>Similar to blist.sortedlist, the <a href="http://www.grantjenks.com/docs/sortedcontainers/" rel="nofollow">sortedcontainers</a> module provides a sorted list, sorted set, and sorted dict data type. It uses a modified B-tree in the underlying implementation and is faster than blist in most cases.</p> <p>The sortedco...
2
2014-09-23T06:19:16Z
[ "python", "data-structures" ]
Decoding HTML Entities With Python
628,332
<p>The following Python code uses BeautifulStoneSoup to fetch the LibraryThing API information for Tolkien's "The Children of Húrin".</p> <pre><code>import urllib2 from BeautifulSoup import BeautifulStoneSoup URL = ("http://www.librarything.com/services/rest/1.0/" "?method=librarything.ck.getwork&amp;id...
0
2009-03-09T22:47:01Z
628,347
<p>The web page may be lying about its encoding. The output looks like UTF-8. If you got a str at the end then you'll need to decode it as UTF-8. If you have a unicode instead then you'll need to encode as Latin-1 first.</p>
1
2009-03-09T22:53:49Z
[ "python", "unicode", "encoding", "utf-8", "beautifulsoup" ]
Decoding HTML Entities With Python
628,332
<p>The following Python code uses BeautifulStoneSoup to fetch the LibraryThing API information for Tolkien's "The Children of Húrin".</p> <pre><code>import urllib2 from BeautifulSoup import BeautifulStoneSoup URL = ("http://www.librarything.com/services/rest/1.0/" "?method=librarything.ck.getwork&amp;id...
0
2009-03-09T22:47:01Z
628,382
<p>In the source of the web page it looks like this: <code>The Children of H&amp;Atilde;&amp;ordm;rin</code>. So the encoding is already broken somewhere on their side before it even gets converted to XML...</p> <p>If it's a general issue with all the books and you need to work around it, this seems to work:</p> <pre...
4
2009-03-09T23:05:28Z
[ "python", "unicode", "encoding", "utf-8", "beautifulsoup" ]
Google Data Source JSON not valid?
628,505
<p>I am implementing a Google Data Source using their <a href="http://code.google.com/apis/visualization/documentation/dev/gviz_api_lib.html#tojsonexample" rel="nofollow">Python Library</a>. I would like the response from the library to be able to be imported in another Python script using the <a href="http://simplejso...
5
2009-03-10T00:07:31Z
628,634
<p>It is considered to be invalid JSON without the string keys.</p> <pre><code>{id:'name',label:'Name',type:'string'} </code></pre> <p>must be:</p> <pre><code>{'id':'name','label':'Name','type':'string'} </code></pre> <p>According to the <a href="http://code.google.com/apis/visualization/documentation/dev/implement...
8
2009-03-10T01:35:25Z
[ "python", "simplejson" ]
How to divide a set of overlapping ranges into non-overlapping ranges?
628,837
<p>Let's say you have a set of ranges:</p> <ul> <li>0 - 100: 'a'</li> <li>0 - 75: 'b'</li> <li>95 - 150: 'c'</li> <li>120 - 130: 'd'</li> </ul> <p>Obviously, these ranges overlap at certain points. How would you dissect these ranges to produce a list of non-overlapping ranges, while retaining information associated w...
8
2009-03-10T03:21:07Z
628,849
<p>I'd say create a list of the endpoints and sort it, also index the list of ranges by starting and ending points. Then iterate through the list of sorted endpoints, and for each one, check the ranges to see which ones are starting/stopping at that point.</p> <p>This is probably better represented in code... if your ...
1
2009-03-10T03:25:59Z
[ "python", "algorithm", "math", "range", "rectangles" ]
How to divide a set of overlapping ranges into non-overlapping ranges?
628,837
<p>Let's say you have a set of ranges:</p> <ul> <li>0 - 100: 'a'</li> <li>0 - 75: 'b'</li> <li>95 - 150: 'c'</li> <li>120 - 130: 'd'</li> </ul> <p>Obviously, these ranges overlap at certain points. How would you dissect these ranges to produce a list of non-overlapping ranges, while retaining information associated w...
8
2009-03-10T03:21:07Z
628,859
<p>I had the same question when writing a program to mix (partly overlapping) audio samples.</p> <p>What I did was add an "start event" and "stop event" (for each item) to a list, sort the list by time point, and then process it in order. You could do the same, except using an integer point instead of a time, and ins...
11
2009-03-10T03:32:48Z
[ "python", "algorithm", "math", "range", "rectangles" ]
How to divide a set of overlapping ranges into non-overlapping ranges?
628,837
<p>Let's say you have a set of ranges:</p> <ul> <li>0 - 100: 'a'</li> <li>0 - 75: 'b'</li> <li>95 - 150: 'c'</li> <li>120 - 130: 'd'</li> </ul> <p>Obviously, these ranges overlap at certain points. How would you dissect these ranges to produce a list of non-overlapping ranges, while retaining information associated w...
8
2009-03-10T03:21:07Z
628,899
<p>Pseudocode:</p> <pre><code>unusedRanges = [ (each of your ranges) ] rangesInUse = [] usedRanges = [] beginningBoundary = nil boundaries = [ list of all your ranges' start and end values, sorted ] resultRanges = [] for (boundary in boundaries) { rangesStarting = [] rangesEnding = [] // determine which...
0
2009-03-10T04:01:29Z
[ "python", "algorithm", "math", "range", "rectangles" ]
How to divide a set of overlapping ranges into non-overlapping ranges?
628,837
<p>Let's say you have a set of ranges:</p> <ul> <li>0 - 100: 'a'</li> <li>0 - 75: 'b'</li> <li>95 - 150: 'c'</li> <li>120 - 130: 'd'</li> </ul> <p>Obviously, these ranges overlap at certain points. How would you dissect these ranges to produce a list of non-overlapping ranges, while retaining information associated w...
8
2009-03-10T03:21:07Z
628,906
<p>What you describe is an example of set theory. For a general algorithm for computing unions, intersections, and differences of sets see:</p> <p><a href="http://www.gvu.gatech.edu/~jarek/graphics/papers/04PolygonBooleansMargalit.pdf" rel="nofollow">www.gvu.gatech.edu/~jarek/graphics/papers/04PolygonBooleansMargalit....
1
2009-03-10T04:07:40Z
[ "python", "algorithm", "math", "range", "rectangles" ]
Performance Advantages to Iterators?
628,903
<p>What (if any) performance advantages are offered by using iterators. It seems like the 'Right Way' to solve many problems, but does it create faster/more memory-conscious code? I'm thinking specifically in Python, but don't restrict answers to just that. </p>
14
2009-03-10T04:05:59Z
628,957
<p>Iterators are just classes that implement <a href="http://docs.python.org/library/stdtypes.html#iterator-types" rel="nofollow">a particular interface</a>, specifically an interface for <em>going to the next one</em>. In Python, lists, tuples, dicts, strings, and files all implement this interface. If they are impl...
2
2009-03-10T04:42:36Z
[ "python", "performance", "iterator" ]