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
How do nested functions work in Python?
2,005,956
<pre><code>def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 </code></pre> <p>Why does the nested function remember the first value <code>2</code> even though <code>maker()</code> has return...
34
2010-01-05T12:29:20Z
2,005,994
<p>You are defining TWO functions. When you call </p> <pre><code>f = maker(2) </code></pre> <p>you're defining a function that returns twice the number, so</p> <pre><code>f(2) --&gt; 4 f(3) --&gt; 6 </code></pre> <p>Then, you define ANOTHER DIFFERENT FUNCTION</p> <pre><code>g = maker(3) </code></pre> <p>that retu...
13
2010-01-05T12:37:54Z
[ "python", "function", "nested", "closures", "nested-function" ]
How do nested functions work in Python?
2,005,956
<pre><code>def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 </code></pre> <p>Why does the nested function remember the first value <code>2</code> even though <code>maker()</code> has return...
34
2010-01-05T12:29:20Z
2,006,009
<p>That is what's called "<a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">closure</a>". Simply put, for most if not all programming languages that treat functions as <a href="http://en.wikipedia.org/wiki/First_class_object">first-class object</a>, any variables that are used within a function obje...
9
2010-01-05T12:39:41Z
[ "python", "function", "nested", "closures", "nested-function" ]
How do nested functions work in Python?
2,005,956
<pre><code>def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 </code></pre> <p>Why does the nested function remember the first value <code>2</code> even though <code>maker()</code> has return...
34
2010-01-05T12:29:20Z
2,006,017
<p>You can see it as all the variables originating in the parent function being replaced by their actual value inside the child function. This way, there is no need to keep track of the scope of the parent function to make the child function run correctly.</p> <p>See it as "dynamically creating a function".</p> <pre>...
19
2010-01-05T12:41:12Z
[ "python", "function", "nested", "closures", "nested-function" ]
How do nested functions work in Python?
2,005,956
<pre><code>def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 </code></pre> <p>Why does the nested function remember the first value <code>2</code> even though <code>maker()</code> has return...
34
2010-01-05T12:29:20Z
2,006,023
<p>Because at the time when you create the function, <code>n</code> was <code>2</code>, so your function is:</p> <pre><code>def action(x): return x ** 2 </code></pre> <p>When you call f(3), <code>x</code> is set to <code>3</code>, so your function will return <code>3 ** 2</code></p>
2
2010-01-05T12:42:20Z
[ "python", "function", "nested", "closures", "nested-function" ]
How do nested functions work in Python?
2,005,956
<pre><code>def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 </code></pre> <p>Why does the nested function remember the first value <code>2</code> even though <code>maker()</code> has return...
34
2010-01-05T12:29:20Z
2,006,193
<p>People answered correctly about the closure, that is: the valid value for "n" inside action is the last value it had whenever "maker" was called.</p> <p>One ease way to overcome this is to make your freevar (n) a variable inside the "action" function, which receives a copy of "n" in the moment it is run:</p> <p>Th...
1
2010-01-05T13:13:08Z
[ "python", "function", "nested", "closures", "nested-function" ]
How do nested functions work in Python?
2,005,956
<pre><code>def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 </code></pre> <p>Why does the nested function remember the first value <code>2</code> even though <code>maker()</code> has return...
34
2010-01-05T12:29:20Z
2,007,255
<p>When you create a function with the def keyword, you are doing exactly that: you are creating a new function object and assigning it to a variable. In the code you gave you are assigning that new function object to a local variable called action.</p> <p>WHen you call it a second time you are creating a second funct...
0
2010-01-05T16:04:01Z
[ "python", "function", "nested", "closures", "nested-function" ]
How do nested functions work in Python?
2,005,956
<pre><code>def maker(n): def action(x): return x ** n return action f = maker(2) print(f) print(f(3)) print(f(4)) g = maker(3) print(g(3)) print(f(3)) # still remembers 2 </code></pre> <p>Why does the nested function remember the first value <code>2</code> even though <code>maker()</code> has return...
34
2010-01-05T12:29:20Z
32,437,268
<p>Let’s look at three common reasons for writing inner functions.</p> <h2>1. Closures and Factory Functions</h2> <p>The value in the enclosing scope is remembered even when the variable goes out of scope or the function itself is removed from the current namespace.</p> <pre><code>def print_msg(msg): """This i...
2
2015-09-07T11:11:50Z
[ "python", "function", "nested", "closures", "nested-function" ]
Python Encoding Issue
2,006,115
<p>I am really lost in all the encoding/decoding issues with Python. Having read quite few docs about how to handle incoming perfectly, i still have issues with few languages, like Korean. Anyhow, here is the what i am doing.</p> <pre><code>korean_text = korean_text.encode('utf-8', 'ignore') korean_text = unicode(kore...
1
2010-01-05T12:57:16Z
2,006,345
<p>Even having read some docs, you seem to be confused on how unicode works.</p> <ul> <li>Unicode is not an encoding. Unicode is the absence of encodings.</li> <li><code>utf-8</code> is not unicode. <code>utf-8</code> is an encoding. </li> <li>You <strong>decode</strong> utf-8 bytestrings to get unicode. You <strong>e...
8
2010-01-05T13:45:18Z
[ "python", "encoding", "utf-8" ]
Python Encoding Issue
2,006,115
<p>I am really lost in all the encoding/decoding issues with Python. Having read quite few docs about how to handle incoming perfectly, i still have issues with few languages, like Korean. Anyhow, here is the what i am doing.</p> <pre><code>korean_text = korean_text.encode('utf-8', 'ignore') korean_text = unicode(kore...
1
2010-01-05T12:57:16Z
2,006,354
<p>Read through <a href="http://www.stereoplex.com/two-voices/python-unicode-and-unicodedecodeerror" rel="nofollow">this post</a> about handling Unicode in Python.</p> <p>You basically want to be doing these things:</p> <pre><code>.encode() text to a particular encoding (such as utf-8) before sending it to the databa...
0
2010-01-05T13:46:20Z
[ "python", "encoding", "utf-8" ]
Python Encoding Issue
2,006,115
<p>I am really lost in all the encoding/decoding issues with Python. Having read quite few docs about how to handle incoming perfectly, i still have issues with few languages, like Korean. Anyhow, here is the what i am doing.</p> <pre><code>korean_text = korean_text.encode('utf-8', 'ignore') korean_text = unicode(kore...
1
2010-01-05T12:57:16Z
2,007,028
<p>The problem is most certainly (especially if other non-ASCII characters appear to work fine) that your browser or OS doesn't have the right fonts to display Korean text, or that the default font used by your browser doesn't support Korean. Try to choose another font until it works.</p>
0
2010-01-05T15:33:26Z
[ "python", "encoding", "utf-8" ]
Multithreading python application hangs while running its threads
2,006,132
<p>I am trying to create a MainObject which is availible as a DBus service. This MainObject should always stay responsive to other objects/processes and for this non-blocking even while processing its items. for that reason items are processed in a seperate thread one after another (queue-style). You can add items to t...
3
2010-01-05T12:59:57Z
2,006,451
<p>Threads in python can be a trap -- it is an open problem, actually. The main problem is the GIL - Python's Global Interpreter Lock.</p> <p>One of the ways they invented to overcome this is the "multiprocessing" module (new in python 2.6) - it preserves the "threading" interface, but actually runs the code in a sep...
0
2010-01-05T14:02:57Z
[ "python", "multithreading", "loops", "sleep", "blocking" ]
Multithreading python application hangs while running its threads
2,006,132
<p>I am trying to create a MainObject which is availible as a DBus service. This MainObject should always stay responsive to other objects/processes and for this non-blocking even while processing its items. for that reason items are processed in a seperate thread one after another (queue-style). You can add items to t...
3
2010-01-05T12:59:57Z
2,007,009
<p>The gobject bindings by default aren't multithread-aware. Please try to do the following just after you have imported <code>gobject</code>:</p> <pre><code>gobject.threads_init() </code></pre>
2
2010-01-05T15:30:52Z
[ "python", "multithreading", "loops", "sleep", "blocking" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,006,200
<p>Use a module scoped variable?</p> <p><code>from config_module import debug_flag</code></p> <p>and use this "variable" to gate access to the logging function(s). You would build yourself a <code>logging</code> module that uses the <code>debug_flag</code> to gate the logging functionality.</p>
0
2010-01-05T13:14:12Z
[ "python", "comments", "preprocessor", "commenting" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,006,521
<p>I think that completely aboiding the calling on a function is not posible, as Python works in a different way that C. The #define takes place in the pre-compiler, before the code is compiled. In Python, there's no such thing.</p> <p>If you want to completely remove the calling to debug in a work environment, I thin...
0
2010-01-05T14:16:22Z
[ "python", "comments", "preprocessor", "commenting" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,006,640
<p>Python does not have a preprocessor, although you could run your python source through an external preprocessor to get the same effect - e.g. <code>sed "/logging.debug/d"</code> will strip out all the debug logging commands. This is not very elegant though - you will end up needing some sort of build system to run ...
15
2010-01-05T14:33:39Z
[ "python", "comments", "preprocessor", "commenting" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,006,760
<p>You can't <em>skip</em> function calls. You could redefine these as empty though, e.g. by creating another logging object that provides the same interface, but with empty functions.</p> <p>But by far the cleanest approach is to ignore the low priority log messages (as you suggested):</p> <pre><code>logging.root.se...
-1
2010-01-05T14:53:59Z
[ "python", "comments", "preprocessor", "commenting" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,006,779
<p>Before you do this, have you profiled to verify that the logging is actually taking a substantial amount of time? You may find that you spend more time trying to remove the calls than you save.</p> <p>Next, have you tried something like <a href="http://psyco.sourceforge.net/" rel="nofollow">Psyco</a>? If you've g...
0
2010-01-05T14:57:28Z
[ "python", "comments", "preprocessor", "commenting" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,006,874
<p>Well, you can always implement your own simple preprocessor that does the trick. Or, even better, you can use an already existing one. Say <a href="http://code.google.com/p/preprocess/" rel="nofollow">http://code.google.com/p/preprocess/</a></p>
1
2010-01-05T15:13:02Z
[ "python", "comments", "preprocessor", "commenting" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,007,129
<p>Although I think the question is perfectly clear and valid (notwithstanding the many responses that suggest otherwise), the short answer is "there's no support in Python for this".</p> <p>The only potential solution other than the <a href="http://stackoverflow.com/questions/2006190/python-equivalent-of-define-func-...
1
2010-01-05T15:46:50Z
[ "python", "comments", "preprocessor", "commenting" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,009,984
<p>define a function that does nothing, ie</p> <pre><code>def nuzzing(*args, **kwargs): pass </code></pre> <p>Then just overload all the functions you want to get rid of with your function, ala</p> <pre><code>logging.debug = nuzzing </code></pre>
0
2010-01-05T23:38:09Z
[ "python", "comments", "preprocessor", "commenting" ]
python equivalent of '#define func() ' or how to comment out a function call in python
2,006,190
<p>my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.) for example:</p> <pre><code>import logging logging.root.setLevel(logging.DEBUG) logging.debug('hello') j = 0 for i in range(10): j += i logging.debug('i %d j %d' % (i,j)) print(j) logging.debug('bye') </cod...
8
2010-01-05T13:12:20Z
2,218,439
<p>I like the 'if _<em>debug</em>' solution except that putting it in front of every call is a bit distracting and ugly. I had this same problem and overcame it by writing a script which automatically parses your source files and replaces logging statements with pass statements (and commented out copies of the logging...
0
2010-02-07T21:32:12Z
[ "python", "comments", "preprocessor", "commenting" ]
web2py Exception in sql rows
2,006,259
<p>when i do the following code:</p> <pre><code>family_members =db(db.member.id == membership_id).select (db.member.name,db.member.id) family_members.colnames = ('Name','Membership ID') </code></pre> <p>It cause the following error...</p> <pre><code>Traceback (most recent call last): File "/home/abeer/Desktop/web2...
0
2010-01-05T13:24:49Z
2,006,750
<p>Do not use <code>colnames</code>. That attribute is internal to web2py. Use <code>db.table.field.label='..'</code> or <code>SQLTABLE(rows, headers={...})</code> depending on what you need.</p>
0
2010-01-05T14:52:01Z
[ "python", "web2py" ]
Click overlay for html page with click stats
2,006,320
<p>How would you setup click overlay for html page that has click stats with number of clicks stored. Is there a jquery example for something like this. The ideal click overlay will have have a little box on top on each link with how many times it was clicked.</p>
2
2010-01-05T13:39:23Z
2,006,643
<p>I think this tutorial is perfect for you: <a href="http://celeryproject.org/tutorials/clickcounter.html" rel="nofollow">http://celeryproject.org/tutorials/clickcounter.html</a></p> <p>You can make a json service for get the click stats.</p>
0
2010-01-05T14:33:52Z
[ "jquery", "python", "django" ]
Click overlay for html page with click stats
2,006,320
<p>How would you setup click overlay for html page that has click stats with number of clicks stored. Is there a jquery example for something like this. The ideal click overlay will have have a little box on top on each link with how many times it was clicked.</p>
2
2010-01-05T13:39:23Z
2,006,836
<p>Not sure if you want to use a 3rd party service, but Crazy Egg does this exact thing: <a href="http://crazyegg.com/" rel="nofollow">http://crazyegg.com/</a></p> <p>Or a tutorial here: <a href="http://css-tricks.com/tracking-clicks-building-a-clickmap-with-php-and-jquery/" rel="nofollow">http://css-tricks.com/tracki...
1
2010-01-05T15:07:23Z
[ "jquery", "python", "django" ]
making undo in python
2,006,404
<p>first of all .. sorry if my english was bad. its my 3rd language <br> im working on a paint software that draw over images and save them again ( for commenting propose ) <br> i use pile and wxpython. but im still having problems with some features .. <br> what is the ideal way to make the undo option ? <br> another...
3
2010-01-05T13:54:40Z
2,006,426
<p>The canonical strategy is to use the <a href="http://en.wikipedia.org/wiki/Command_pattern"><strong>Command pattern</strong></a>. You'll represent the things you can do as Command objects, and each object is placed on a stack. The state of the application is then defined by an initial state plus everything that the ...
12
2010-01-05T13:58:23Z
[ "python", "image", "wxpython", "python-imaging-library", "undo" ]
making undo in python
2,006,404
<p>first of all .. sorry if my english was bad. its my 3rd language <br> im working on a paint software that draw over images and save them again ( for commenting propose ) <br> i use pile and wxpython. but im still having problems with some features .. <br> what is the ideal way to make the undo option ? <br> another...
3
2010-01-05T13:54:40Z
2,007,604
<p>Also, keep in mind that Python's functions are first-class objects, which can make implementing the Command pattern very smooth:</p> <pre><code>actions = [] def do_action1(arg1, arg2): # .. do the action .. # remember we did the action: actions.append(do_action1, (arg1, arg2)) def do_action2(arg1, ar...
7
2010-01-05T16:53:06Z
[ "python", "image", "wxpython", "python-imaging-library", "undo" ]
Executing python script in background in init.d
2,006,483
<p>to interact with my iPhone, i have created a python script that sends and recives data through a socket, the script must be started after emule in order to work, i have thought of something like this:</p> <pre><code>PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/bin/amuled WEB=/...
3
2010-01-05T14:08:15Z
2,006,539
<p>try launching with <code>nohup process.py &amp;</code></p>
2
2010-01-05T14:20:02Z
[ "python", "bash", "init.d", "sysv" ]
Executing python script in background in init.d
2,006,483
<p>to interact with my iPhone, i have created a python script that sends and recives data through a socket, the script must be started after emule in order to work, i have thought of something like this:</p> <pre><code>PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/local/bin/amuled WEB=/...
3
2010-01-05T14:08:15Z
2,006,735
<p>Put the <code>su</code> process in the background, not its child. For example:</p> <pre><code>su $USER -c "$WEB --quiet" &amp; </code></pre> <p>Notice that the ampersand is outside the quotes.</p>
3
2010-01-05T14:50:02Z
[ "python", "bash", "init.d", "sysv" ]
How to share data between python processes without writing to disk
2,006,624
<p>Helllo, I would like to share small amounts of data (&lt; 1K) between python and processes. The data is physical pc/104 IO data which changes rapidly and often (24x7x365). There will be a single "server" writing the data and multiple clients reading portions of it. The system this will run on uses flash memory (CF c...
13
2010-01-05T14:31:04Z
2,006,668
<p>Create a ramfs partition and write to that. (You could use tmpfs, but unlike tmpfs, ramfs is not swapped to disk). However, as ramfs doesn't have a size limit, you must take care that you don't run out of memory; since you're only writing a tiny bit of data there, it shouldn't be a problem.</p> <p>This way, your da...
4
2010-01-05T14:38:26Z
[ "python", "ipc", "mmap", "json-rpc", "pc104" ]
How to share data between python processes without writing to disk
2,006,624
<p>Helllo, I would like to share small amounts of data (&lt; 1K) between python and processes. The data is physical pc/104 IO data which changes rapidly and often (24x7x365). There will be a single "server" writing the data and multiple clients reading portions of it. The system this will run on uses flash memory (CF c...
13
2010-01-05T14:31:04Z
2,006,721
<p>An alternative to writing the data to file in the server process might be to directly write to the client processes:</p> <p>Use UNIX domain sockets (or TCP/IP sockets if the clients run on different machines) to connect each client to the server, and have the server write into those sockets. Depending on your parti...
8
2010-01-05T14:48:02Z
[ "python", "ipc", "mmap", "json-rpc", "pc104" ]
How to share data between python processes without writing to disk
2,006,624
<p>Helllo, I would like to share small amounts of data (&lt; 1K) between python and processes. The data is physical pc/104 IO data which changes rapidly and often (24x7x365). There will be a single "server" writing the data and multiple clients reading portions of it. The system this will run on uses flash memory (CF c...
13
2010-01-05T14:31:04Z
2,007,678
<p>According to the <a href="http://en.wikipedia.org/wiki/Mmap" rel="nofollow">Wikipedia article about the mmap system call</a>, memory mapped files contents <strong>are written back to disk</strong> when updated.</p> <p>Have you looked at the multiprocessing module (in standard library) - especially the part Sharing ...
2
2010-01-05T17:03:56Z
[ "python", "ipc", "mmap", "json-rpc", "pc104" ]
How to share data between python processes without writing to disk
2,006,624
<p>Helllo, I would like to share small amounts of data (&lt; 1K) between python and processes. The data is physical pc/104 IO data which changes rapidly and often (24x7x365). There will be a single "server" writing the data and multiple clients reading portions of it. The system this will run on uses flash memory (CF c...
13
2010-01-05T14:31:04Z
2,008,152
<p>When running on flash systems, make sure your filesystem is designed properly to maximize the life of the flash memory (wear levelling). <a href="http://en.wikipedia.org/wiki/JFFS" rel="nofollow">JFFS</a> and, I believe, others are now capable of doing this effectively. If you use such a system, you shouldn't be o...
0
2010-01-05T18:22:37Z
[ "python", "ipc", "mmap", "json-rpc", "pc104" ]
Email multiple contacts in Python
2,006,648
<p>I am trying to send an email to multiple addresses. The code below shows what I'm attempting to achieve. When I add two addresses the email does not send to the second address. The code is:</p> <pre><code> me = 'a@a.com' you = 'a@a.com, a@a.com' msg['Subject'] = "Some Subject" msg['From'] = me msg['To...
10
2010-01-05T14:34:45Z
2,006,665
<p>You want this:</p> <pre><code>from email.utils import COMMASPACE ... you = ["foo@example.com", "bar@example.com"] ... msg['To'] = COMMASPACE.join(you) ... s.sendmail(me, you, msg.as_string()) </code></pre>
7
2010-01-05T14:38:12Z
[ "python" ]
Email multiple contacts in Python
2,006,648
<p>I am trying to send an email to multiple addresses. The code below shows what I'm attempting to achieve. When I add two addresses the email does not send to the second address. The code is:</p> <pre><code> me = 'a@a.com' you = 'a@a.com, a@a.com' msg['Subject'] = "Some Subject" msg['From'] = me msg['To...
10
2010-01-05T14:34:45Z
2,006,667
<p>Try</p> <pre><code>s.sendmail(me, you.split(","), msg.as_string()) </code></pre> <p>If you do <code>you = ['a@a.com', 'a@a.com']</code> </p> <p>Try</p> <pre><code>msg['To'] = ",".join(you) ... s.sendmail(me, you, msg.as_string()) </code></pre>
11
2010-01-05T14:38:25Z
[ "python" ]
Email multiple contacts in Python
2,006,648
<p>I am trying to send an email to multiple addresses. The code below shows what I'm attempting to achieve. When I add two addresses the email does not send to the second address. The code is:</p> <pre><code> me = 'a@a.com' you = 'a@a.com, a@a.com' msg['Subject'] = "Some Subject" msg['From'] = me msg['To...
10
2010-01-05T14:34:45Z
2,006,710
<pre><code>you = ('one@address', 'another@address') s.sendmail(me, you, msg.as_string()) </code></pre>
2
2010-01-05T14:45:40Z
[ "python" ]
How can I disable clear of clipboard on exit of PyQt4 application?
2,007,103
<p>I have a simple PyQt4 application (see the code below) that reveals next problem: if I select the text from QLineEdit and copy it to clipboard then I can paste it to another application only while my application is running. It seems that on exit PyQt application clears the clipboard so I can't paste the text after t...
4
2010-01-05T15:43:39Z
2,011,376
<p>OK, there is not exactly clear of clipboard occurs. Just QT store some sort of pointer of text in the clipboard instead of just text. Gordon Tyler has pointed me to this discussion: <a href="http://old.nabble.com/Re:-Searching-for-a-very-small-scprit-using-CLIPBOARD-p23246491.html" rel="nofollow">http://old.nabble.c...
4
2010-01-06T06:52:49Z
[ "python", "qt4", "pyqt", "clipboard", "pyqt4" ]
How can I disable clear of clipboard on exit of PyQt4 application?
2,007,103
<p>I have a simple PyQt4 application (see the code below) that reveals next problem: if I select the text from QLineEdit and copy it to clipboard then I can paste it to another application only while my application is running. It seems that on exit PyQt application clears the clipboard so I can't paste the text after t...
4
2010-01-05T15:43:39Z
30,426,291
<p>I stumbled upon this question when I ran into similar issue under GNU/Linux and found the answer on the site referenced by bialix (whose address has changed, but can still be reached via web search). To cite the most relevant part:</p> <blockquote> <p>Remember that in Linux, unless you have something like klipper...
0
2015-05-24T17:28:12Z
[ "python", "qt4", "pyqt", "clipboard", "pyqt4" ]
Piston customize response representation
2,007,195
<p>I am using piston and I would like to spit out a custom format for my response.</p> <p>My model is something like this:</p> <pre><code>class Car(db.Model): name = models.CharField(max_length=256) color = models.CharField(max_length=256) </code></pre> <p>Now when I issue a GET request to something like /api/...
6
2010-01-05T15:57:14Z
2,007,251
<p>Django comes with a serialization library. You'll also need a json library to get it into the format you want</p> <p><a href="http://docs.djangoproject.com/en/dev/topics/serialization/" rel="nofollow">http://docs.djangoproject.com/en/dev/topics/serialization/</a></p> <pre><code>from django.core import serializers ...
-2
2010-01-05T16:03:47Z
[ "python", "django", "rest", "piston" ]
Piston customize response representation
2,007,195
<p>I am using piston and I would like to spit out a custom format for my response.</p> <p>My model is something like this:</p> <pre><code>class Car(db.Model): name = models.CharField(max_length=256) color = models.CharField(max_length=256) </code></pre> <p>Now when I issue a GET request to something like /api/...
6
2010-01-05T15:57:14Z
2,007,515
<p>You can return custom format by returning a Python dictionary. Here is an example on one of my app. I hope it helps.</p> <pre><code>from models import * from piston.handler import BaseHandler from django.http import Http404 class ZipCodeHandler(BaseHandler): methods_allowed = ('GET',) def read(self, reque...
6
2010-01-05T16:40:40Z
[ "python", "django", "rest", "piston" ]
Piston customize response representation
2,007,195
<p>I am using piston and I would like to spit out a custom format for my response.</p> <p>My model is something like this:</p> <pre><code>class Car(db.Model): name = models.CharField(max_length=256) color = models.CharField(max_length=256) </code></pre> <p>Now when I issue a GET request to something like /api/...
6
2010-01-05T15:57:14Z
9,404,645
<p>Its been two years since this question was asked, so its obviously late for OP. But for others who, like me, had a similar dilemma there exists a <strong>Pistonic</strong> way to get this done.</p> <p>Using the Django example of polls and choices -</p> <p>You will notice that for the ChoiceHandler the JSON respons...
1
2012-02-22T23:05:17Z
[ "python", "django", "rest", "piston" ]
pygtk: cannot set parent on toplevel widget
2,007,299
<p>I'm working on a project that has a glade GUI.</p> <p>I need the main window to have 2 section, divided by a gtk.Hpaned widget (horizontal panes).</p> <p>The left pane would have a tool-bar like layout of buttons, maybe 3 or more.</p> <p>What I need is a way to create different windows and display them on the rig...
2
2010-01-05T16:09:38Z
2,008,053
<p>Do you try this?</p> <pre><code>gtk.Widget.reparent(new_parent) </code></pre> <p>The reparent() method moves a widget from one gtk.Container to another.</p>
2
2010-01-05T18:01:41Z
[ "python", "pygtk" ]
pygtk: cannot set parent on toplevel widget
2,007,299
<p>I'm working on a project that has a glade GUI.</p> <p>I need the main window to have 2 section, divided by a gtk.Hpaned widget (horizontal panes).</p> <p>The left pane would have a tool-bar like layout of buttons, maybe 3 or more.</p> <p>What I need is a way to create different windows and display them on the rig...
2
2010-01-05T16:09:38Z
2,016,770
<p>Instead of creating windows you could put a notebook in the right pane. Then create all the previous windows as pages. Click on the button can then show the appropriate page in the notebook. </p>
0
2010-01-06T22:25:23Z
[ "python", "pygtk" ]
How to get the difference between two list based on substrings withing each string in the seperate lists
2,007,755
<p>I have two long list, one from a log file that contains lines formatted like </p> <p><code>201001050843 blah blah blah &lt;email@site.com&gt; blah blah</code></p> <p>and a second file in csv format. I need to generate a list of all the entries in file2 that do not contain a email address in the log file, while mai...
1
2010-01-05T17:13:58Z
2,007,812
<p><code>line.split()</code> splits at whitespace. Use <code>line.split(',')</code> instead.</p> <p>Also: Does the order of the lines matter? If not, then you should really use a <code>set()</code> instead of a list. That will make the code much faster.</p>
1
2010-01-05T17:23:04Z
[ "python", "list", "string", "list-manipulation" ]
How to get the difference between two list based on substrings withing each string in the seperate lists
2,007,755
<p>I have two long list, one from a log file that contains lines formatted like </p> <p><code>201001050843 blah blah blah &lt;email@site.com&gt; blah blah</code></p> <p>and a second file in csv format. I need to generate a list of all the entries in file2 that do not contain a email address in the log file, while mai...
1
2010-01-05T17:13:58Z
2,007,865
<p>You could create the set of emails as you do and then:</p> <pre><code># emails is a set of emails for line in fileinput.input("csvfile.csv",inplace =1): parts = line.split(',') if parts[3] not in emails: print line </code></pre> <p>This only works, if the email in the CSV file is always at position...
1
2010-01-05T17:31:37Z
[ "python", "list", "string", "list-manipulation" ]
How to get the difference between two list based on substrings withing each string in the seperate lists
2,007,755
<p>I have two long list, one from a log file that contains lines formatted like </p> <p><code>201001050843 blah blah blah &lt;email@site.com&gt; blah blah</code></p> <p>and a second file in csv format. I need to generate a list of all the entries in file2 that do not contain a email address in the log file, while mai...
1
2010-01-05T17:13:58Z
2,010,228
<p>here's another way, with minimalistic check on email addr's position.</p> <pre><code>import fileinput emails=[] for line in open("file1"): start=line.find("&lt;") end=line.find("&gt;") if start != -1 and end !=-1: emails.append(line[start+1:end]) for line in fileinput.FileInput("file2",inplace=...
0
2010-01-06T00:39:23Z
[ "python", "list", "string", "list-manipulation" ]
Python decorator with instantiation-time variable?
2,007,786
<p>I want to make a decorator that creates a new function/method that makes use of an object <code>obj</code>. If the decorated object is a function, <code>obj</code> must be instantiated when the function is created. If the decorated object is a method, a new <code>obj</code> must be instantiated and bound to each ins...
0
2010-01-05T17:19:05Z
2,007,926
<p>Since a decorator is just syntactic sugar for saying</p> <pre><code>def func(): ... func = decorator(func) </code></pre> <p>Why not do that in the object constructor?</p> <pre><code>class A(object): def __init__(self): # apply decorator at instance creation self.f = dec(self.f) def f(s...
1
2010-01-05T17:40:12Z
[ "python", "decorator" ]
Python decorator with instantiation-time variable?
2,007,786
<p>I want to make a decorator that creates a new function/method that makes use of an object <code>obj</code>. If the decorated object is a function, <code>obj</code> must be instantiated when the function is created. If the decorated object is a method, a new <code>obj</code> must be instantiated and bound to each ins...
0
2010-01-05T17:19:05Z
2,007,941
<p>Your writing style is really hard to read. Normal sentences are half as long as yours :P</p> <p>Do you want this or what?</p> <pre><code>__all__ = ['dec', 'A'] from time import time, sleep import inspect def dec(f): def new(self, *args, **kwargs): print self.initiated # print the time the objecte was...
0
2010-01-05T17:43:02Z
[ "python", "decorator" ]
Python decorator with instantiation-time variable?
2,007,786
<p>I want to make a decorator that creates a new function/method that makes use of an object <code>obj</code>. If the decorated object is a function, <code>obj</code> must be instantiated when the function is created. If the decorated object is a method, a new <code>obj</code> must be instantiated and bound to each ins...
0
2010-01-05T17:19:05Z
2,008,827
<p>It's a little difficult to figure out what you're after exactly. The list and __call__ stuff is confusing me so I'm mainly sticking with your first paragraph:</p> <pre><code>__all__ = ['dec', 'A'] from types import InstanceType from functools import wraps import inspect def dec(func): #get the sig of the fun...
1
2010-01-05T20:24:42Z
[ "python", "decorator" ]
Python decorator with instantiation-time variable?
2,007,786
<p>I want to make a decorator that creates a new function/method that makes use of an object <code>obj</code>. If the decorated object is a function, <code>obj</code> must be instantiated when the function is created. If the decorated object is a method, a new <code>obj</code> must be instantiated and bound to each ins...
0
2010-01-05T17:19:05Z
2,010,159
<p>Okay based on manifest's code, I've got a solution.</p> <p>As you can see, pydoc will still have decent documentation about the decorated thing:</p> <blockquote> <pre><code>class A(__builtin__.object) | Methods defined here: | | f(*args, **kwargs) | f(self, x) | something </code></pre> </blockqu...
0
2010-01-06T00:23:01Z
[ "python", "decorator" ]
How to use C# client to consume Django/Python web service (all methods are returning null)?
2,007,908
<p>I have a C# command-line client that I'm testing the consumption of SOAP/WSDL via Django/Python/soaplib created WSDL. I've managed to successfully connect to the web service by adding a service reference. I then call one of service's methods, and the service processes the data I send, but it returns null instead of ...
3
2010-01-05T17:36:48Z
2,008,000
<p>One thing you can do is start by building a manual proxy using WebClient, or WebRequest/WebResponse. Construct your manual proxy to send the desired data to the WS for testing.</p> <p>Couple of things to check on the WSDL implementation:</p> <ul> <li>The WSDL definition needs to match <strong>exactly</strong>, inc...
0
2010-01-05T17:52:30Z
[ "c#", "python", "django", "soap", "wsdl" ]
How to use C# client to consume Django/Python web service (all methods are returning null)?
2,007,908
<p>I have a C# command-line client that I'm testing the consumption of SOAP/WSDL via Django/Python/soaplib created WSDL. I've managed to successfully connect to the web service by adding a service reference. I then call one of service's methods, and the service processes the data I send, but it returns null instead of ...
3
2010-01-05T17:36:48Z
2,008,062
<p>We faced the similar problem while consuming a web service it was the type of data returned we were getting data in UTF-16 format. </p> <p>Please check if you have proper data type in use.</p>
0
2010-01-05T18:04:28Z
[ "c#", "python", "django", "soap", "wsdl" ]
Can you recommend an Amazon AMI for Python?
2,008,055
<p>I want to remove as much complexity as I can from administering Python in on Amazon EC2 following some truly awful experiences with hosting providers who claim support for Python. I am looking for some guidance on which AMI to choose so that I have a stable and easily managed environment which already included Pyth...
3
2010-01-05T18:01:58Z
2,008,170
<p>If you can get by with using the Amazon provided ones, I'd recommend it. I tend to use ami-84db39ed.</p> <p>Honestly though, if you plan on leaving this running all the time, you would probably save a bit of money by just going with a VPS. Amazon tends to be cheaper if you are turning the service on and off over ti...
2
2010-01-05T18:25:46Z
[ "python", "amazon-ec2" ]
Can you recommend an Amazon AMI for Python?
2,008,055
<p>I want to remove as much complexity as I can from administering Python in on Amazon EC2 following some truly awful experiences with hosting providers who claim support for Python. I am looking for some guidance on which AMI to choose so that I have a stable and easily managed environment which already included Pyth...
3
2010-01-05T18:01:58Z
2,008,634
<p>Try the <a href="http://uec-images.ubuntu.com/releases/11.04/release/" rel="nofollow">Ubuntu EC2 images</a>. Python 2.7 is installed by default. The rest you just <code>apt-get install</code> and optionally create an image when the baseline is the way you want it (or just maintain a script that installs all the piec...
4
2010-01-05T19:49:55Z
[ "python", "amazon-ec2" ]
How to debug dynamically defined functions in Python?
2,008,496
<p>Is there a way to debug a function that is defined dynamically in run time? </p> <p>Or at least is there an easy way to find out where this function is produced?</p> <p><em>Update to give more detail:</em></p> <p>I used <code>inspect</code> module:</p> <pre><code>ipdb&gt; inspect.getmodule(im.get_thumbnail_url) ...
4
2010-01-05T19:23:05Z
2,008,612
<p>Not sure, but you can get a function to print out the source file and line number it was defined in using the inspect module:</p> <pre><code>import inspect f = lambda: inspect.currentframe().f_code.co_filename + ':' + \ str(inspect.currentframe().f_lineno); print f() </code></pre> <p>It prints:</p> <pre><code>s...
1
2010-01-05T19:44:32Z
[ "python", "debugging", "dynamic" ]
How to debug dynamically defined functions in Python?
2,008,496
<p>Is there a way to debug a function that is defined dynamically in run time? </p> <p>Or at least is there an easy way to find out where this function is produced?</p> <p><em>Update to give more detail:</em></p> <p>I used <code>inspect</code> module:</p> <pre><code>ipdb&gt; inspect.getmodule(im.get_thumbnail_url) ...
4
2010-01-05T19:23:05Z
2,009,758
<p>Given a function <code>f</code>, in CPython you can print <code>f.func_code.co_filename</code> and <code>f.func_code.co_firstlineno</code> to get the file and first line number. This will not help if the function was created using <code>eval</code> or <code>exec</code>.</p> <p>Tracebacks contain file and line infor...
1
2010-01-05T22:52:40Z
[ "python", "debugging", "dynamic" ]
Display objects within a function, how to NOT terminate the program after close the display?
2,008,641
<pre><code>from visual import * def hello(): newyear=2010 sphere() return newyear </code></pre> <p>my problem here is when I call function hello(), a sphere display window shows up, and also prints 2010, however if I close the display window, the program terminates. That is not what I want, how can I avoid...
0
2010-01-05T19:51:36Z
2,009,863
<p>You need to fork the process before hand.</p> <p>(Totally untested)</p> <pre><code>import os if os.fork() == 0: exit() </code></pre> <p>Placing that stanza at the start of your program should cause execution to continue in a forked process, detached from your tty. Someone can probably correct me though.</p>
0
2010-01-05T23:12:43Z
[ "python", "python-visual" ]
Python + Facebook, getting info about a group easily
2,008,816
<p>I have a need to display some basic info about a facebook group on a website i am building. All i am really looking to show is the total number of members, and maybe a list of the few most recent people who joined. </p> <p>I would like to not have to login to FB to accomplish this, is there an API for groups that a...
1
2010-01-05T20:23:12Z
2,012,448
<p>Use the Python Facebook module on Google Code.</p>
1
2010-01-06T11:11:06Z
[ "python", "django", "facebook" ]
python struct pack double
2,008,871
<p>I want to convert -123.456 into a C double for network transmission in python. So I tried this:</p> <pre><code>struct.pack('d', -123.456) </code></pre> <p>I get this as a result:</p> <pre><code>'w\xbe\x9f\x1a/\xdd^\xc0' </code></pre> <p>Obviously there is some hex in there, but what is with the w, /, and ^ sprin...
1
2010-01-05T20:32:20Z
2,008,889
<p>They are, respectively, a "w", "/", and "^". Some byte sequences do correspond to ASCII characters.</p>
1
2010-01-05T20:34:27Z
[ "python", "networking", "struct.pack" ]
Any ideas on optimizing this script? (Python)
2,008,891
<p>I'm working on a little script to help me learn the Japanese Kana (Hiragana/Katakana). In total, there are probably 100+ (+||-). </p> <p>Basically, all it would do is take in the english version and convert it to the character. </p> <p>ie. a = 'あ' which is 12354 in decimal</p> <p>What I have so far is this:</p>...
2
2010-01-05T20:34:35Z
2,008,919
<p>Put an encoding header as per <a href="http://www.python.org/dev/peps/pep-0263/" rel="nofollow">PEP 263</a>, then use the characters directly:</p> <pre><code># -*- coding: utf-8 -*- hiraDict = {'a': u'あ', 'i': u'い', ...} </code></pre>
3
2010-01-05T20:37:27Z
[ "python", "unicode" ]
Any ideas on optimizing this script? (Python)
2,008,891
<p>I'm working on a little script to help me learn the Japanese Kana (Hiragana/Katakana). In total, there are probably 100+ (+||-). </p> <p>Basically, all it would do is take in the english version and convert it to the character. </p> <p>ie. a = 'あ' which is 12354 in decimal</p> <p>What I have so far is this:</p>...
2
2010-01-05T20:34:35Z
2,008,920
<p>From the docs:</p> <blockquote> <p>has_key(key)</p> <p>Test for the presence of key in the dictionary. has_key() is deprecated in favor of key in d.</p> </blockquote> <p>So rewrite to</p> <p><code>if sys.argv[1] in hiraDict:</code></p>
1
2010-01-05T20:37:41Z
[ "python", "unicode" ]
Any ideas on optimizing this script? (Python)
2,008,891
<p>I'm working on a little script to help me learn the Japanese Kana (Hiragana/Katakana). In total, there are probably 100+ (+||-). </p> <p>Basically, all it would do is take in the english version and convert it to the character. </p> <p>ie. a = 'あ' which is 12354 in decimal</p> <p>What I have so far is this:</p>...
2
2010-01-05T20:34:35Z
2,008,996
<p>No need to check for presence, just get it and provide a default argument if you don't want an exception:</p> <pre><code># -*- coding: utf-8 -*- import sys hiraDict = {'a': 'あ', 'i': 'い', } print(hiraDict.get(sys.argv[1], None)) </code></pre> <p>... and for python 2.x:</p> <pre><code># -*- coding: utf-8 -*- i...
5
2010-01-05T20:48:20Z
[ "python", "unicode" ]
Any ideas on optimizing this script? (Python)
2,008,891
<p>I'm working on a little script to help me learn the Japanese Kana (Hiragana/Katakana). In total, there are probably 100+ (+||-). </p> <p>Basically, all it would do is take in the english version and convert it to the character. </p> <p>ie. a = 'あ' which is 12354 in decimal</p> <p>What I have so far is this:</p>...
2
2010-01-05T20:34:35Z
2,009,286
<p>Not only 100, this would work fine with 100k characters too.</p> <p>So no need to worry about optimizing performance really, unless you will be using this routine thousands times per second, which you probably would not.</p>
1
2010-01-05T21:39:01Z
[ "python", "unicode" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,206
<p>In reality, you should do both; if it's what you want. For me though, out of the two, I'd only look at Python. I have no real interest in languages that are so new.</p>
9
2010-01-05T21:26:55Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,214
<p>How long have you been working with Python?</p> <p>If it were me, I'd do my best to maybe get a working knowledge of Go (basic syntax, some familiarity with unique language features), and continue with Python as I normally would.</p> <p>Eventually you might come up with a small project that Go seems suited for (or...
3
2010-01-05T21:28:51Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,216
<p>These two languages accomplish different goals. Go is only 20% slower than C. If you want crazy speed and easy parallelism, then learn Go. However, readability was not a design goal. Also, it has no exceptions. It can be used for real-time products though ...</p>
1
2010-01-05T21:29:15Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,218
<p>IMO you should improve your Python knowledge. Python is a widely adopted language, whereas Go is still at a very, very early stage and there's no reason to believe that it will become successful.</p> <p>From a purely utilitarian perspective, you will get a lot more value from learning Python.</p>
1
2010-01-05T21:29:22Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,219
<p>It's not easy to answer this without knowing how good your Python is, or what you do, or where you'd use Go. If you're looking for employment, I suspect Python is the way to go. I'd be surprised to find anyone is using Go for major projects at the moment (outside Google).</p> <p>Note also that Go is by no means fin...
1
2010-01-05T21:29:28Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,251
<p>It's up to you. You should probably do both if you can, because that way you will have more tools on your metaphorical programmer's belt.</p> <p>There are a number of things that I think are worth considering whenever I'm in a similar dilemma.</p> <p>Is a new language (not just unfamiliar, but actually new, like G...
2
2010-01-05T21:33:20Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,289
<p>Python is a available for most operating systems, it's generally accepted as a scriptin language, and it has matured to production quality.</p> <p>Go is a research language that's only available in beta quality on Linux and OS X. Nowhere else. It's interesting from an intellectual point of view in that you can lear...
1
2010-01-05T21:39:52Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,485
<p>If you're just looking to have fun and expand your horizons, then I'd learn Go, since you already know some Python.</p> <p>If you're looking to improve as a developer, I'd personally recommend working on an actual project (using Python, as it's the language you have the most experience with):</p> <ul> <li>This wil...
17
2010-01-05T22:10:55Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,489
<p>It depends on what your goals and your needs are.</p> <p>If you're looking to develop your skills for a job, then go with Python or Ruby. You're unlikely to see Go show up in the workplace for quite some time (if ever) unless you're working at Google. (Even then it's questionable.)</p> <p>If you want to have fun, ...
8
2010-01-05T22:11:41Z
[ "python", "ruby", "go" ]
Learn Go Or Improve My Python/Ruby Knowledge
2,009,194
<p>I was reading about Go, and I can see that it's very good and can be a language used by many developers in some months, but I want to decide a simple thing: <strong>Learn Go or improve my Python or Ruby knowledge?</strong></p> <p>Years developing with Python: 1<br> Years developing with Ruby: 0.3</p>
5
2010-01-05T21:24:56Z
2,009,509
<p>First of all, it's a very very personal question, and my first recommendation will be , if you think so, try Go for one month or so, learning the basics, and then deciding... Each one has the mind fitted more to some particular languages than another...</p> <p>I also would recommend keep using both Python and Ruby,...
2
2010-01-05T22:16:07Z
[ "python", "ruby", "go" ]
How to Speed Up Python's urllib2 when doing multiple requests
2,009,243
<p>I am making several http requests to a particular host using python's urllib2 library. Each time a request is made a new tcp and http connection is created which takes a noticeable amount of time. Is there any way to keep the tcp/http connection alive using urllib2?</p>
18
2010-01-05T21:32:14Z
6,272,067
<p>If you switch to <a href="https://docs.python.org/2/library/httplib.html" rel="nofollow">httplib</a>, you will have finer control over the underlying connection.</p> <p>For example:</p> <pre><code>import httplib conn = httplib.HTTPConnection(url) conn.request('GET', '/foo') r1 = conn.getresponse() r1.read() con...
24
2011-06-07T21:55:40Z
[ "python", "http", "urllib2" ]
How to Speed Up Python's urllib2 when doing multiple requests
2,009,243
<p>I am making several http requests to a particular host using python's urllib2 library. Each time a request is made a new tcp and http connection is created which takes a noticeable amount of time. Is there any way to keep the tcp/http connection alive using urllib2?</p>
18
2010-01-05T21:32:14Z
6,272,120
<p>I've used the third-party <a href="https://pypi.python.org/pypi/urllib3" rel="nofollow"><code>urllib3</code></a> library to good effect in the past. It's designed to complement <code>urllib2</code> by pooling connections for reuse.</p> <p>Modified example from <a href="http://code.google.com/p/urllib3/wiki/Examples...
2
2011-06-07T22:01:14Z
[ "python", "http", "urllib2" ]
How to Speed Up Python's urllib2 when doing multiple requests
2,009,243
<p>I am making several http requests to a particular host using python's urllib2 library. Each time a request is made a new tcp and http connection is created which takes a noticeable amount of time. Is there any way to keep the tcp/http connection alive using urllib2?</p>
18
2010-01-05T21:32:14Z
27,090,284
<p>If you need something more automatic than plain httplib, this might help, though it's not threadsafe.</p> <pre><code>try: from http.client import HTTPConnection, HTTPSConnection except ImportError: from httplib import HTTPConnection, HTTPSConnection import select connections = {} def request(method, url, ...
0
2014-11-23T14:59:14Z
[ "python", "http", "urllib2" ]
Python multiprocessing: Permission denied
2,009,278
<p>I'm getting an error when trying to execute python program that uses multiprocessing package:</p> <pre><code> File "/usr/local/lib/python2.6/multiprocessing/__init__.py", line 178, in RLock return RLock() File "/usr/local/lib/python2.6/multiprocessing/synchronize.py", line 142, in __init__ SemLock.__init...
41
2010-01-05T21:37:06Z
2,009,505
<p>For POSIX semaphores to work, the users need r/w access to shared memory (<code>/dev/shm</code>).</p> <p>Check the permissions to <code>/dev/shm</code>. On my laptop (Ubuntu) it looks like this:</p> <pre><code>$ ls -ld /dev/shm drwxrwxrwt 2 root root 40 2010-01-05 20:34 shm </code></pre> <p>To permanentl...
56
2010-01-05T22:15:45Z
[ "python", "linux" ]
Python multiprocessing: Permission denied
2,009,278
<p>I'm getting an error when trying to execute python program that uses multiprocessing package:</p> <pre><code> File "/usr/local/lib/python2.6/multiprocessing/__init__.py", line 178, in RLock return RLock() File "/usr/local/lib/python2.6/multiprocessing/synchronize.py", line 142, in __init__ SemLock.__init...
41
2010-01-05T21:37:06Z
26,776,810
<p>In my OVH VPS Classic, this error was caused by a loop in /dev/shm and /run/shm. Both were symlinks linking to each other. So as root here is what I did:</p> <pre><code># rm /dev/shm # mkdir /dev/shm # chmod 777 /dev/shm # nano /etc/fstab </code></pre> <p>Then I modified the shm line from:</p> <pre><code>none /de...
2
2014-11-06T10:14:58Z
[ "python", "linux" ]
Interpolate Question
2,009,379
<pre><code>import re from decimal import * import numpy from scipy.signal import cspline1d, cspline1d_eval import scipy.interpolate import scipy import math import numpy from scipy import interpolate Y1 =[0.48960000000000004, 0.52736099999999997, 0.56413900000000006, 0.60200199999999993...
5
2010-01-05T21:54:11Z
2,009,538
<p>The X value 0.029999999999999999 occurs twice, with two different Y coordinates. It wouldn't surprise me if that caused a problem trying to fit a polynomial spline segment....</p>
1
2010-01-05T22:18:52Z
[ "python", "scipy" ]
Interpolate Question
2,009,379
<pre><code>import re from decimal import * import numpy from scipy.signal import cspline1d, cspline1d_eval import scipy.interpolate import scipy import math import numpy from scipy import interpolate Y1 =[0.48960000000000004, 0.52736099999999997, 0.56413900000000006, 0.60200199999999993...
5
2010-01-05T21:54:11Z
2,085,669
<p>I believe it's due to the X1 values not being ordered from smallest to largest plus also you have one duplicate x point, i.e, you need to sort the values for X1 and Y1 before you can use the splrep and remove duplicates.</p> <p>splrep from the docs seem to be low level access to FITPACK libraries which expects a so...
7
2010-01-18T11:36:14Z
[ "python", "scipy" ]
Read/Write Python Closures
2,009,402
<p>Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another lexical scope cannot ...
24
2010-01-05T21:57:06Z
2,009,424
<p><code>nonlocal</code> in 3.x should remedy this.</p>
15
2010-01-05T22:00:59Z
[ "python", "language-design", "closures", "readonly" ]
Read/Write Python Closures
2,009,402
<p>Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another lexical scope cannot ...
24
2010-01-05T21:57:06Z
2,009,474
<p>You could do this and it would work more or less the same way:</p> <pre><code>class counter(object): def __init__(self, count=0): self.count = count def __call__(self): self.count += 1 return self.count </code></pre> <p>Or, a bit of a hack:</p> <pre><code>def counter(): cou...
19
2010-01-05T22:09:13Z
[ "python", "language-design", "closures", "readonly" ]
Read/Write Python Closures
2,009,402
<p>Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another lexical scope cannot ...
24
2010-01-05T21:57:06Z
2,009,545
<p>I would use a generator:</p> <pre><code>&gt;&gt;&gt; def counter(): count = 0 while True: count += 1 yield(count) &gt;&gt;&gt; c = counter() &gt;&gt;&gt; c.next() 1 &gt;&gt;&gt; c.next() 2 &gt;&gt;&gt; c.next() 3 </code></pre> <p><strong>EDIT</strong>: I believe the ultimate answer to your...
13
2010-01-05T22:19:49Z
[ "python", "language-design", "closures", "readonly" ]
Read/Write Python Closures
2,009,402
<p>Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another lexical scope cannot ...
24
2010-01-05T21:57:06Z
2,009,575
<p>Functions can also have attributes, so this would work, too:</p> <pre><code>def counter(): def c(): while True: yield c.count c.count += 1 c.count = 0 return c </code></pre> <p>However, in this specific example, I'd use a generator as suggested by jbochi.</p> <p>As for ...
6
2010-01-05T22:24:39Z
[ "python", "language-design", "closures", "readonly" ]
Read/Write Python Closures
2,009,402
<p>Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another lexical scope cannot ...
24
2010-01-05T21:57:06Z
2,009,645
<p>To expand on Ignacio's answer:</p> <pre><code>def counter(): count = 0 def c(): nonlocal count count += 1 return count return c x = counter() print([x(),x(),x()]) </code></pre> <p>gives [1,2,3] in Python 3; invocations of <code>counter()</code> give independent counters. Other ...
22
2010-01-05T22:34:44Z
[ "python", "language-design", "closures", "readonly" ]
Read/Write Python Closures
2,009,402
<p>Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another lexical scope cannot ...
24
2010-01-05T21:57:06Z
2,009,899
<p>This behavior is quite thoroughly explained the official <a href="http://docs.python.org/tutorial/classes.html#python-scopes-and-namespaces">Python tutorial</a> as well as in the <a href="http://docs.python.org/reference/executionmodel.html">Python execution model</a>. In particular, from the tutorial:</p> <blockqu...
5
2010-01-05T23:17:44Z
[ "python", "language-design", "closures", "readonly" ]
Read/Write Python Closures
2,009,402
<p>Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another lexical scope cannot ...
24
2010-01-05T21:57:06Z
2,011,492
<p>It is not that they are read-only, as much as the scope is more strict that you realize. If you can't <code>nonlocal</code> in Python 3+, then you can at least use explicit scoping. Python 2.6.1, with explicit scoping at the module level:</p> <pre><code>&gt;&gt;&gt; def counter(): ... sys.modules[__name__].cou...
0
2010-01-06T07:28:47Z
[ "python", "language-design", "closures", "readonly" ]
Delayed execution in python for big data
2,009,708
<p>I'm trying to think about how a Python API might look for large datastores like Cassandra. R, Matlab, and NumPy tend to use the "everything is a matrix" formulation and execute each operation separately. This model has proven itself effective for data that can fit in memory. However, one of the benefits of SAS fo...
1
2010-01-05T22:43:51Z
2,009,997
<p>I don't know anything about Cassandra/NumPy, but if you adapt your second approach (using NumPy) to process data in chunks of a reasonable size, you might benefit from the CPU and/or filesystem cache and therefore prevent any slowdown caused by looping over the data twice, without giving up the benefit of using opti...
2
2010-01-05T23:42:47Z
[ "python", "cassandra" ]
Delayed execution in python for big data
2,009,708
<p>I'm trying to think about how a Python API might look for large datastores like Cassandra. R, Matlab, and NumPy tend to use the "everything is a matrix" formulation and execute each operation separately. This model has proven itself effective for data that can fit in memory. However, one of the benefits of SAS fo...
1
2010-01-05T22:43:51Z
2,024,107
<p>I don't have a perfect answer, just a rough idea, but maybe it is worthwhile. It centers around Python generators, in sort of a producer-consumer style combination.</p> <p>For one, as you don't want to loop twice, I think there is no way around an explicit loop for the rows, like this:</p> <pre><code>for row in ro...
1
2010-01-07T22:19:15Z
[ "python", "cassandra" ]
Ruby equivalent to Python's help()?
2,009,730
<p>When working in interactive Python, I tend to rely on the built-in <code>help()</code> function to tell me what something expects and/or returns, and print out any documentation that might help me. Is there a Ruby equivalent to this function?</p> <p>I'm looking for something I could use in irb. For example, in inte...
16
2010-01-05T22:47:30Z
2,009,742
<p><a href="http://rubylearning.com/satishtalim/ruby_ri_tool.html">Try using <strong>ri</strong> from the command line.</a></p> <p>It takes a class name, method, or module as an argument, and gives you appropriate documentation. Many popular gems come with this form of documentation, as well, so it should typically wo...
5
2010-01-05T22:48:56Z
[ "python", "ruby" ]
Ruby equivalent to Python's help()?
2,009,730
<p>When working in interactive Python, I tend to rely on the built-in <code>help()</code> function to tell me what something expects and/or returns, and print out any documentation that might help me. Is there a Ruby equivalent to this function?</p> <p>I'm looking for something I could use in irb. For example, in inte...
16
2010-01-05T22:47:30Z
2,009,844
<p>It's definitely a poor cousin to iPython's help, and one of the main features I miss after moving to Ruby, but you can also use <code>ri</code> from within <code>irb</code>. I'd recommend the <a href="http://pablotron.org/software/wirble/" rel="nofollow">wirble gem</a> as an easy way to set this up.</p>
4
2010-01-05T23:07:54Z
[ "python", "ruby" ]
Ruby equivalent to Python's help()?
2,009,730
<p>When working in interactive Python, I tend to rely on the built-in <code>help()</code> function to tell me what something expects and/or returns, and print out any documentation that might help me. Is there a Ruby equivalent to this function?</p> <p>I'm looking for something I could use in irb. For example, in inte...
16
2010-01-05T22:47:30Z
2,009,893
<p>There's supposed to be <a href="http://tagaholic.me/2009/05/11/demystifying-irb-commands.html#miscellaneous_commands" rel="nofollow">irb_help</a>. But like mentioned in that post, it's broken in my ruby install as well.</p>
0
2010-01-05T23:17:16Z
[ "python", "ruby" ]
Ruby equivalent to Python's help()?
2,009,730
<p>When working in interactive Python, I tend to rely on the built-in <code>help()</code> function to tell me what something expects and/or returns, and print out any documentation that might help me. Is there a Ruby equivalent to this function?</p> <p>I'm looking for something I could use in irb. For example, in inte...
16
2010-01-05T22:47:30Z
25,671,404
<p>It's now late 2014 and here's the two ways to get the Python <code>help()</code> *similarity, as long as you have the <a href="http://stackoverflow.com/a/13886612/2062384">Ruby Docs</a> installed:</p> <ol> <li><p>From inside <code>irb</code>, You can call the <code>help</code> method with a string describing what y...
3
2014-09-04T17:16:05Z
[ "python", "ruby" ]
Ruby equivalent to Python's help()?
2,009,730
<p>When working in interactive Python, I tend to rely on the built-in <code>help()</code> function to tell me what something expects and/or returns, and print out any documentation that might help me. Is there a Ruby equivalent to this function?</p> <p>I'm looking for something I could use in irb. For example, in inte...
16
2010-01-05T22:47:30Z
33,597,990
<p>For quick shell access to ruby documentation, just type ri followed by the method you're wanting to learn more about (from your shell). </p> <p>For example: </p> <p><code>ri puts</code></p> <p>This must be fired up in your shell, not your irb (interactive ruby environment)</p> <p>If you're in your irb environme...
0
2015-11-08T19:21:14Z
[ "python", "ruby" ]
Accessing py2exe program over network in Windows 98 throws ImportErrors
2,009,873
<p>I'm running a py2exe-compiled python program from one server machine on a number of client machines (mapped to a network drive on every machine, say W:). </p> <p>For Windows XP and later machines, have so far had zero problems with Python picking up W:\python23.dll (yes, I'm using Python 2.3.5 for W98 compatibility...
2
2010-01-05T23:13:57Z
2,009,998
<p>This isn't a direct answer, but possibly some help. Are you familiar with the <code>-v</code> option in Python. Type <code>python -h</code> to learn more. Note the equivalent to the <code>PYTHONVERBOSE</code> environment variable for py2exe'd scripts is <code>PY2EXE_VERBOSE</code>, as described almost nowhere exc...
2
2010-01-05T23:42:50Z
[ "python", "py2exe", "importerror", "python-module", "windows-98" ]
Accessing py2exe program over network in Windows 98 throws ImportErrors
2,009,873
<p>I'm running a py2exe-compiled python program from one server machine on a number of client machines (mapped to a network drive on every machine, say W:). </p> <p>For Windows XP and later machines, have so far had zero problems with Python picking up W:\python23.dll (yes, I'm using Python 2.3.5 for W98 compatibility...
2
2010-01-05T23:13:57Z
7,440,912
<p>The problem is no longer a problem for me, I found an alternative solution (which may or may not have involved quitting my job there). Accepting this answer until someone else can provide a more satisfactory solution.</p>
-1
2011-09-16T06:23:53Z
[ "python", "py2exe", "importerror", "python-module", "windows-98" ]
Please help me find the official name of this programming approach
2,010,132
<p>I [surely re] invented this [wheel] when I wanted to compute the union and the intersection and diff of two sets (stored as lists) at the same time. Initial code (not the tightest):</p> <pre><code>dct = {} for a in lst1: dct[a] = 1 for b in lst2: if b in dct: dct[b] -= 1 else: dct[b] = -1 union = [k ...
7
2010-01-06T00:16:18Z
2,010,175
<p>Is the idea of a <a href="http://en.wikipedia.org/wiki/Bit_field" rel="nofollow">bit field</a> what you're looking for? Every bit of your ... field (for lack of a better word) represents a flag. In this case, your flag is membership in set N.</p> <p>Edit - I think I misunderstood which idea you were referring to. D...
1
2010-01-06T00:25:42Z
[ "python", "algorithm", "language-agnostic", "set", "bit-fields" ]
Please help me find the official name of this programming approach
2,010,132
<p>I [surely re] invented this [wheel] when I wanted to compute the union and the intersection and diff of two sets (stored as lists) at the same time. Initial code (not the tightest):</p> <pre><code>dct = {} for a in lst1: dct[a] = 1 for b in lst2: if b in dct: dct[b] -= 1 else: dct[b] = -1 union = [k ...
7
2010-01-06T00:16:18Z
2,010,181
<p>It's very common to use an integer to represent a set of small integers; it's often called a <em>bitset</em> or <em>bitvector</em>. Here you're using an integer to represent "the set of input sequences that contain this value".</p> <p>The operation you're doing reminds me of <strong>reversing a multimap</strong>.</...
2
2010-01-06T00:27:50Z
[ "python", "algorithm", "language-agnostic", "set", "bit-fields" ]
Please help me find the official name of this programming approach
2,010,132
<p>I [surely re] invented this [wheel] when I wanted to compute the union and the intersection and diff of two sets (stored as lists) at the same time. Initial code (not the tightest):</p> <pre><code>dct = {} for a in lst1: dct[a] = 1 for b in lst2: if b in dct: dct[b] -= 1 else: dct[b] = -1 union = [k ...
7
2010-01-06T00:16:18Z
2,010,507
<p>Using an N bit integer to represent N booleans is a special case of the data structure known as a <strong>perfect hash table</strong>. Notice you're explicitly using dicts (which are general hash tables) in the idea that prompted you to think about bitsets. It's a hash table because you use hashes to find a value,...
7
2010-01-06T01:55:01Z
[ "python", "algorithm", "language-agnostic", "set", "bit-fields" ]
Please help me find the official name of this programming approach
2,010,132
<p>I [surely re] invented this [wheel] when I wanted to compute the union and the intersection and diff of two sets (stored as lists) at the same time. Initial code (not the tightest):</p> <pre><code>dct = {} for a in lst1: dct[a] = 1 for b in lst2: if b in dct: dct[b] -= 1 else: dct[b] = -1 union = [k ...
7
2010-01-06T00:16:18Z
2,010,536
<p>Yes, it is sometimes used in databases, for example PostgreSQL. As mentions Wikipedia:</p> <blockquote> <p>Some database systems that do not offer persistent bitmap indexes use bitmaps internally to speed up query processing. For example, PostgreSQL versions 8.1 and later implement a "bitmap index scan"...
4
2010-01-06T02:03:27Z
[ "python", "algorithm", "language-agnostic", "set", "bit-fields" ]
Speeding up the python "import" loader
2,010,255
<p>I'm getting seriously frustrated at how slow python startup is. Just importing more or less basic modules takes a second, since python runs down the sys.path looking for matching files (and generating 4 <code>stat()</code> calls - ["foo", "foo.py", "foo.pyc", "foo.so"] - for each check). For a complicated project ...
18
2010-01-06T00:47:29Z
2,010,309
<p>The first things that come to mind are: </p> <ul> <li>Try a smaller path</li> <li>Make sure your modules are pyc's so they'll load faster</li> <li>Make sure you don't double import, or import too much</li> </ul> <p>Other than that, are you sure that the disk operations are what's bogging you down? Is your disk/ope...
4
2010-01-06T01:01:21Z
[ "python" ]