title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
Showing an image from console in Python
1,413,540
<p>What is the easiest way to show a <code>.jpg</code> or <code>.gif</code> image from Python console?</p> <p>I've got a Python console program that is checking a data set which contains links to images stored locally. How should I write the script so that it would display images pop-up graphical windows?</p>
13
2009-09-11T22:22:50Z
1,413,575
<p>Why not just display it in the user's web browser?</p>
7
2009-09-11T22:36:26Z
[ "python", "image" ]
Showing an image from console in Python
1,413,540
<p>What is the easiest way to show a <code>.jpg</code> or <code>.gif</code> image from Python console?</p> <p>I've got a Python console program that is checking a data set which contains links to images stored locally. How should I write the script so that it would display images pop-up graphical windows?</p>
13
2009-09-11T22:22:50Z
1,413,639
<p>Or simply execute the image through the shell, as in </p> <pre><code>import subprocess subprocess.call([ fname ], shell=True) </code></pre> <p>and whatever program is installed to handle images will be launched.</p>
4
2009-09-11T22:53:41Z
[ "python", "image" ]
Showing an image from console in Python
1,413,540
<p>What is the easiest way to show a <code>.jpg</code> or <code>.gif</code> image from Python console?</p> <p>I've got a Python console program that is checking a data set which contains links to images stored locally. How should I write the script so that it would display images pop-up graphical windows?</p>
13
2009-09-11T22:22:50Z
1,413,693
<p>Since you are probably running Windows (from looking at your tags), this would be the easiest way to open and show an image file from the console without installing extra stuff like PIL.</p> <pre><code>import os os.system('start pic.png') </code></pre>
4
2009-09-11T23:21:46Z
[ "python", "image" ]
Showing an image from console in Python
1,413,540
<p>What is the easiest way to show a <code>.jpg</code> or <code>.gif</code> image from Python console?</p> <p>I've got a Python console program that is checking a data set which contains links to images stored locally. How should I write the script so that it would display images pop-up graphical windows?</p>
13
2009-09-11T22:22:50Z
13,843,577
<p>In Xterm-compatible terminals, you can show the image directly in the terminal. See <a href="http://stackoverflow.com/a/13843534/309483">my answer to "PPM image to ASCII art in Python"</a></p> <p><img src="http://i.stack.imgur.com/3mwxc.png" alt="ImageMagick's &quot;logo:&quot; image in Xterm (show picture in new t...
4
2012-12-12T15:54:58Z
[ "python", "image" ]
Showing an image from console in Python
1,413,540
<p>What is the easiest way to show a <code>.jpg</code> or <code>.gif</code> image from Python console?</p> <p>I've got a Python console program that is checking a data set which contains links to images stored locally. How should I write the script so that it would display images pop-up graphical windows?</p>
13
2009-09-11T22:22:50Z
37,520,984
<h2>In a new window using Pillow/PIL</h2> <p>Install <a href="https://pypi.python.org/pypi/Pillow/2.7.0" rel="nofollow">Pillow</a> (or PIL), e.g.:</p> <pre><code>$ pip install pillow </code></pre> <p>Now you can</p> <pre><code>from PIL import Image with Image.open('path/to/file.jpg') as img: img.show() </code><...
1
2016-05-30T08:27:31Z
[ "python", "image" ]
Python with matplotlib - reusing drawing functions
1,413,681
<p>I have a follow up question to this <a href="http://stackoverflow.com/questions/1401102/python-with-matplotlib-drawing-multiple-figures-in-parallel/1401686#1401686">question</a>.</p> <p>Is it possible to streamline the figure generation by having multiple python scripts that work on different parts of the figure?</...
11
2009-09-11T23:15:09Z
1,413,865
<p>Okey, I've figured out how to do this. It was a lot simpler than what I had imagined. It just required a bit of reading <a href="http://matplotlib.sourceforge.net/users/artists.html" rel="nofollow">here</a> with the <a href="http://matplotlib.sourceforge.net/api/figure%5Fapi.html#matplotlib.figure.Figure" rel="nofol...
0
2009-09-12T00:34:45Z
[ "python", "matplotlib" ]
Python with matplotlib - reusing drawing functions
1,413,681
<p>I have a follow up question to this <a href="http://stackoverflow.com/questions/1401102/python-with-matplotlib-drawing-multiple-figures-in-parallel/1401686#1401686">question</a>.</p> <p>Is it possible to streamline the figure generation by having multiple python scripts that work on different parts of the figure?</...
11
2009-09-11T23:15:09Z
1,413,894
<p>Here you want to use the <a href="http://matplotlib.sourceforge.net/users/artists.html">Artist objects</a>, and pass them as needed to the functions:</p> <pre><code>import numpy as np import matplotlib.pyplot as plt def myhist(ax, color): ax.hist(np.log(np.arange(1, 10, .1)), facecolor=color) def say_somethin...
8
2009-09-12T00:54:29Z
[ "python", "matplotlib" ]
Python Unicode and MIMEE
1,413,735
<p>Can someone who is way smarter than I tell me what I'm doing wrong.. Shouldn't this simply process...</p> <pre><code># encoding: utf-8 from email.MIMEText import MIMEText msg = MIMEText("hi") msg.set_charset('utf-8') print msg.as_string() a = 'Ho\xcc\x82tel Ste\xcc\x81phane ' b = unicode(a, "utf-8") print b ms...
0
2009-09-11T23:36:14Z
1,413,879
<p>Assuming Python 2.* (alas, you don't tell us whether you're on Python 3, but as you're using <code>print</code> as a statement it looks like you aren't): <code>MIMEText" takes a string -- a plain string, NOT a Unicode object. So, use </code>b.encode('utf-8')<code> as the argument if what you start with is a Unicode...
2
2009-09-12T00:44:29Z
[ "python", "email", "unicode" ]
python serialize string
1,413,763
<p>I am needing to unserialize a string into an array in python just like php and then serialize it back.</p>
0
2009-09-11T23:49:42Z
1,413,783
<p>Take a look at the <a href="http://docs.python.org/library/pickle.html" rel="nofollow">pickle module</a>. It is probably what you're looking for. </p> <pre><code>import pickle # Unserialize the string to an array my_array = pickle.loads(serialized_data) # Serialized back serialized_data = pickle.dumps(my_array) <...
0
2009-09-11T23:57:40Z
[ "python", "serialization" ]
python serialize string
1,413,763
<p>I am needing to unserialize a string into an array in python just like php and then serialize it back.</p>
0
2009-09-11T23:49:42Z
1,413,872
<p>What "serialized" the data in question into the string in the first place? Do you really mean an <a href="http://docs.python.org/library/array.html" rel="nofollow">array</a> (and if so, of what simple type?), or do you actually mean a <a href="http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-...
0
2009-09-12T00:38:51Z
[ "python", "serialization" ]
python serialize string
1,413,763
<p>I am needing to unserialize a string into an array in python just like php and then serialize it back.</p>
0
2009-09-11T23:49:42Z
1,413,949
<p>A string is already a sequence type in Python. You can iterate over the string one character at a time like this:</p> <pre><code>for char in somestring: do_something(char) </code></pre> <p>The question is... what did you want to do with it? Maybe we can give more details with a more detailed question.</p>
0
2009-09-12T01:17:29Z
[ "python", "serialization" ]
python serialize string
1,413,763
<p>I am needing to unserialize a string into an array in python just like php and then serialize it back.</p>
0
2009-09-11T23:49:42Z
1,413,993
<p>If you mean PHP's explode, try this</p> <pre><code>&gt;&gt;&gt; list("foobar") ['f', 'o', 'o', 'b', 'a', 'r'] &gt;&gt;&gt; ''.join(['f', 'o', 'o', 'b', 'a', 'r']) 'foobar' </code></pre>
1
2009-09-12T01:43:16Z
[ "python", "serialization" ]
Expected LP_c_double instance instead of c_double_Array - python ctypes error
1,413,851
<p>I have a function in a DLL that I have to wrap with python code. The function is expecting a pointer to an array of doubles. This is the error I'm getting:</p> <pre><code>Traceback (most recent call last): File "C:\....\.FROGmoduleTEST.py", line 243, in &lt;module&gt; FROGPCGPMonitorDLL.ReturnPulse(ptrpulse, ...
1
2009-09-12T00:28:09Z
1,413,936
<p>Are you writing the wrapper in Python yourself? The error saying "expected LP_c_double instance" means it's expecting a pointer to a single double, not an array as you've suggested. </p> <pre><code>&gt;&gt;&gt; ctypes.POINTER(ctypes.c_double * 10)() &lt;__main__.LP_c_double_Array_10 object at 0xb7eb24f4&gt; &gt;&...
0
2009-09-12T01:11:29Z
[ "python", "types", "ctypes" ]
Expected LP_c_double instance instead of c_double_Array - python ctypes error
1,413,851
<p>I have a function in a DLL that I have to wrap with python code. The function is expecting a pointer to an array of doubles. This is the error I'm getting:</p> <pre><code>Traceback (most recent call last): File "C:\....\.FROGmoduleTEST.py", line 243, in &lt;module&gt; FROGPCGPMonitorDLL.ReturnPulse(ptrpulse, ...
1
2009-09-12T00:28:09Z
1,414,227
<p>LP_c_double is created dynamically by ctypes when you create a pointer to a double. i.e.</p> <pre><code>LP_c_double = POINTER(c_double) </code></pre> <p>At this point, you've created a C <em>type</em>. You can now create instances of these pointers.</p> <pre><code>my_pointer_one = LP_c_double() </code></pre> <p...
2
2009-09-12T03:52:04Z
[ "python", "types", "ctypes" ]
help with python forking child server for doing ajax push, long polling
1,414,098
<p>Alright, I only know some basic python but if I can get help with this then I am considering making it open source.</p> <p>What I am trying to do: - (Done) Ajax send for init content - Python server recv command "init" to send most recent content - (Done) Ajax recv content and then immediately calls back to python ...
0
2009-09-12T02:41:33Z
1,414,153
<p>Look at <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">subprocess</a>.</p> <p>Read all of these related questions on StackOverflow: <a href="http://stackoverflow.com/search?q=%5Bpython%5D+web+subprocess">http://stackoverflow.com/search?q=%5Bpython%5D+web+subprocess</a></p>
1
2009-09-12T03:12:16Z
[ "python", "sockets", "long-polling", "fork" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
1,414,147
<p>Make sure your EMAIL_HOST and EMAIL_PORT are set up right in settings.py (these refer to your SMTP server). It might be assuming that you have an SMTP server running on localhost.</p> <p>To test this locally, run Python's built-in test SMTP server:</p> <pre><code>python -m smtpd -n -c DebuggingServer localhost:102...
12
2009-09-12T03:06:46Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
1,414,348
<p>Try this</p> <pre><code># ./manage shell &gt;&gt;&gt; from django.core.mail import send_mail &gt;&gt;&gt; send_mail('Subject here', 'Here is the message.', 'from@example.com',['to@example.com'], fail_silently=False) </code></pre> <p>With a to@example.com that you actually get email at.</p>
1
2009-09-12T05:09:25Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
1,414,408
<p>My web hosting provider - Webfaction - only allows emails to be sent From an email that has been explicitly created in the administrator panel. Creating one fixed the problem.</p>
6
2009-09-12T05:52:17Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
1,414,460
<p>While likely not ideal, I have found using Gmail as the SMTP host works just fine. There is a useful guide at <a href="http://nathanostgard.com/archives/2007/7/2/gmail%5Fand%5Fdjango/" rel="nofollow">nathanostgard.com</a>. </p> <p>Feel free to post your relevant settings.py sections (including EMAIL_*, SERVER_EMAI...
0
2009-09-12T06:25:34Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
1,794,213
<p>Make sure you have DEBUG = False</p>
2
2009-11-25T02:04:24Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
6,551,508
<p>In my case the cause was missing <a href="https://docs.djangoproject.com/en/dev/ref/settings/#std%3asetting-SERVER_EMAIL">SERVER_EMAIL</a> setting.</p> <p>The default for <a href="https://docs.djangoproject.com/en/dev/ref/settings/#server-email"><code>SERVER_EMAIL</code></a> is <code>root@localhost</code>. But many...
69
2011-07-01T17:54:58Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
8,446,164
<p>Although it's been a while, here's my response, so that other people can benefit in the future.</p> <p>In my case, what was preventing emails to be sent to the ADMINS list, when an error occured, was an application specific setting. I was using django-piston, which provides the setting attributes PISTON_EMAIL_ERROR...
1
2011-12-09T13:29:09Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
9,541,647
<p>I had the same situation. I created a new project and app and it worked, so I knew it was my code. I tracked it down to the LOGGING dictionary in settings.py. I had made some changes a few weeks back for logging with Sentry, but for some reason the error just started today. I changed back to the original and got it ...
21
2012-03-02T23:08:40Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
16,166,097
<p>If, for some reason, you set DEBUG_PROPAGATE_EXCEPTIONS to True (it's False by default), email to admin will not work.</p>
1
2013-04-23T09:45:04Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
18,136,491
<p>Another possibility for error is trouble with your ADMINS setting. The following setting will cause the sending of mail to admins to fail quietly:</p> <pre><code>ADMINS = ( ('your name', 'me@mydomain.com') ) </code></pre> <p>What's wrong with that? Well ADMINS needs to be a tuple of tuples, so the above needs to...
31
2013-08-08T21:30:20Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
20,324,455
<p>Sorry if it is too naive, but in my case the emails were sent but were going directly to the SPAM folder. Before trying more complicated things check your SPAM folder first.</p>
2
2013-12-02T09:08:02Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
25,344,287
<p>For what it's worth I had this issue and none of these suggestions worked for me. It turns out that my problem was that <code>SERVER_EMAIL</code> was set to an address that the server (Webfaction) didn't recognise. If this site were hosted <em>on</em> Webfaction (as my other sites are), this wouldn't be a problem,...
0
2014-08-16T21:44:03Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
28,100,754
<p>Another thing worth noting here is that settings <code>handler500</code> might bypass the mechanism that sends errors on a 500 if the response from the view doesn't have a status code of 500. If you have a <code>handler500</code> set, then in that view respond with something like this.</p> <pre><code>t = loader.get...
2
2015-01-22T23:48:59Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
29,504,810
<p>In my case, it's the <code>include_html</code> in <code>mail_admins</code>.</p> <p>When I set <code>include_html</code> to <code>True</code>,the email server reject to send my email because it think that my emails are spam.</p> <p>Everything works just fine when I set <code>include_html</code> to <code>False</code...
0
2015-04-08T02:22:56Z
[ "python", "django" ]
Django not sending emails to admins
1,414,130
<p>According to the <a href="http://docs.djangoproject.com/en/dev/howto/error-reporting/">documentation</a>, if <code>DEBUG</code> is set to <code>False</code> and something is provided under the <code>ADMINS</code> setting, Django will send an email whenever the code raises a 500 status code. I have the email settings...
47
2009-09-12T02:57:59Z
29,805,903
<p>... and then there's the facepalm error, when you've used this in development to prevent emails from going out, and then accidentally copy the setting to production:</p> <pre><code># Print emails to console EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' </code></pre> <p>(of course you don't see ...
0
2015-04-22T18:23:19Z
[ "python", "django" ]
Should I use pgreloaded? Or subversion of pygame?
1,414,258
<p>I'm using pygame with python 2.6 right now, But I want to use python 3.1.1 instead. The normal pygame only works with 2.x, but the subversion ones work with 3.x I think. But what about pgreloaded? Is that useable yet? The normal pygame actually works with 3.1 too, but not for os x (at least there isn't a download). ...
3
2009-09-12T04:16:37Z
1,416,509
<p>Just stick with Python 2.6.</p>
1
2009-09-13T00:06:58Z
[ "python", "python-3.x", "pygame" ]
Should I use pgreloaded? Or subversion of pygame?
1,414,258
<p>I'm using pygame with python 2.6 right now, But I want to use python 3.1.1 instead. The normal pygame only works with 2.x, but the subversion ones work with 3.x I think. But what about pgreloaded? Is that useable yet? The normal pygame actually works with 3.1 too, but not for os x (at least there isn't a download). ...
3
2009-09-12T04:16:37Z
1,656,917
<p>pgreloaded has documentation - there is even an own package at <a href="http://code.google.com/p/pygame/downloads/list" rel="nofollow">http://code.google.com/p/pygame/downloads/list</a>.</p>
1
2009-11-01T11:20:29Z
[ "python", "python-3.x", "pygame" ]
Should I use pgreloaded? Or subversion of pygame?
1,414,258
<p>I'm using pygame with python 2.6 right now, But I want to use python 3.1.1 instead. The normal pygame only works with 2.x, but the subversion ones work with 3.x I think. But what about pgreloaded? Is that useable yet? The normal pygame actually works with 3.1 too, but not for os x (at least there isn't a download). ...
3
2009-09-12T04:16:37Z
1,913,924
<p>Or consider using Pyglet (<a href="http://pyglet.org/" rel="nofollow">http://pyglet.org/</a>), which is a thin wrapper around openGL, esp. made for games. It works pretty well, documentation is reasonable, but it of course lacks the massive userbase that Pygame has. It is more mature than PGreloaded imho. </p>
0
2009-12-16T11:01:56Z
[ "python", "python-3.x", "pygame" ]
Should I use pgreloaded? Or subversion of pygame?
1,414,258
<p>I'm using pygame with python 2.6 right now, But I want to use python 3.1.1 instead. The normal pygame only works with 2.x, but the subversion ones work with 3.x I think. But what about pgreloaded? Is that useable yet? The normal pygame actually works with 3.1 too, but not for os x (at least there isn't a download). ...
3
2009-09-12T04:16:37Z
5,506,950
<p>I think JD's link to pgreloaded doc is out of date, try this one: <a href="http://wiki.pgreloaded.googlecode.com/hg/documentation/index.html" rel="nofollow">http://wiki.pgreloaded.googlecode.com/hg/documentation/index.html</a></p>
1
2011-03-31T22:10:43Z
[ "python", "python-3.x", "pygame" ]
Local functions in Python
1,414,304
<p>In the following Python code, I get an <code>UnboundLocalError</code>. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I recognise that <code>a</code> is an immutable value in this context, but that should not be a problem.</p> <pr...
26
2009-09-12T04:45:08Z
1,414,320
<p>I believe you're correct in seeing this as a "mutability" problem. While the code you posted does throw an "UnboundLocalError", the following code does not:</p> <pre><code>def outer(): a = 0 def inner(): print a inner() outer() </code></pre> <p>Python doesn't allow you to reassign the value of...
26
2009-09-12T04:54:30Z
[ "python", "function", "binding", "model", "local" ]
Local functions in Python
1,414,304
<p>In the following Python code, I get an <code>UnboundLocalError</code>. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I recognise that <code>a</code> is an immutable value in this context, but that should not be a problem.</p> <pr...
26
2009-09-12T04:45:08Z
1,414,323
<p>Try binding the variable as an argument. </p> <pre><code>def outer(): a = 0 def inner(a=a): a += 1 inner() outer() </code></pre> <p>I'll try and dig up the appropriate documents.</p> <p><strong>edit</strong></p> <p>Since you want the inner function to have a side effect on the outer scope, ...
8
2009-09-12T04:55:11Z
[ "python", "function", "binding", "model", "local" ]
Local functions in Python
1,414,304
<p>In the following Python code, I get an <code>UnboundLocalError</code>. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I recognise that <code>a</code> is an immutable value in this context, but that should not be a problem.</p> <pr...
26
2009-09-12T04:45:08Z
15,334,246
<p>You should specify your variable as nonlocal to preserve it's state in closure, so definition should be like this</p> <pre><code>def outer(): a = 0 def inner(): nonlocal a a += 1 inner() </code></pre>
5
2013-03-11T08:41:38Z
[ "python", "function", "binding", "model", "local" ]
Python recursive program to prime factorize a number
1,414,581
<p>I wrote the following program to prime factorize a number:</p> <pre><code>import math def prime_factorize(x,li=[]): until = int(math.sqrt(x))+1 for i in xrange(2,until): if not x%i: li.append(i) break else: #This else belongs to for li.append(x) prin...
2
2009-09-12T07:50:42Z
1,414,600
<p>Your prime_factorize function doesn't have a return statement in the recursive case -- you want to invoke "return prime_factorize(x/i,li)" on its last line. Try it with a prime number (so the recursive call isn't needed) to see that it works in that case.</p> <p>Also you probably want to make the signature somethin...
9
2009-09-12T08:05:27Z
[ "python", "algorithm", "recursion", "primes", "prime-factoring" ]
Python recursive program to prime factorize a number
1,414,581
<p>I wrote the following program to prime factorize a number:</p> <pre><code>import math def prime_factorize(x,li=[]): until = int(math.sqrt(x))+1 for i in xrange(2,until): if not x%i: li.append(i) break else: #This else belongs to for li.append(x) prin...
2
2009-09-12T07:50:42Z
1,415,182
<p>A more functional-style version.</p> <pre><code>def prime_factorize( number ): def recurse( factors, x, n ): if x&lt;2: return factors # 0,1 dont have prime factors if n &gt; 1+x**0.5: # reached the upper limit factors.append( x ) # the only prime left is x itself return ...
2
2009-09-12T13:59:57Z
[ "python", "algorithm", "recursion", "primes", "prime-factoring" ]
Python recursive program to prime factorize a number
1,414,581
<p>I wrote the following program to prime factorize a number:</p> <pre><code>import math def prime_factorize(x,li=[]): until = int(math.sqrt(x))+1 for i in xrange(2,until): if not x%i: li.append(i) break else: #This else belongs to for li.append(x) prin...
2
2009-09-12T07:50:42Z
1,415,324
<p>@Anthony's correctly answered your original question about <code>print</code>. However, in the spirit of the several tips that were also offered, here's a simple refactorization using tail recursion removal:</p> <pre><code>def prime_factorize(x): li = [] while x &gt;= 2: until = int(math.sqrt(x))+1 for ...
3
2009-09-12T15:04:33Z
[ "python", "algorithm", "recursion", "primes", "prime-factoring" ]
Python recursive program to prime factorize a number
1,414,581
<p>I wrote the following program to prime factorize a number:</p> <pre><code>import math def prime_factorize(x,li=[]): until = int(math.sqrt(x))+1 for i in xrange(2,until): if not x%i: li.append(i) break else: #This else belongs to for li.append(x) prin...
2
2009-09-12T07:50:42Z
6,992,179
<pre><code>def primeFactorization(n): """ Return the prime factors of the given number. """ factors = [] lastresult = n while 1: if lastresult == 1: break c = 2 while 1: if lastresult % c == 0: break c += 1 factors.a...
0
2011-08-09T06:24:34Z
[ "python", "algorithm", "recursion", "primes", "prime-factoring" ]
Python recursive program to prime factorize a number
1,414,581
<p>I wrote the following program to prime factorize a number:</p> <pre><code>import math def prime_factorize(x,li=[]): until = int(math.sqrt(x))+1 for i in xrange(2,until): if not x%i: li.append(i) break else: #This else belongs to for li.append(x) prin...
2
2009-09-12T07:50:42Z
13,167,918
<p>If you want to do it completely recursive, I'd recommend this code, it does return the correct answer and the way it works is pretty clear. If you want to make the program as efficient as possible I'd recommend you to stick to one of your previous methods.</p> <pre><code>def primeFact (i, f): if i &lt; f: ...
5
2012-10-31T21:46:45Z
[ "python", "algorithm", "recursion", "primes", "prime-factoring" ]
PyObjC development with Xcode 3.2
1,414,727
<p>Xcode 3.2 has removed the default templates for the scripting languages (Ruby, Python etc). How do I find these templates to use in Xcode 3.2? Would I need to add anything else to Xcode to support working with and 'building' PyObjC programs?</p> <p>Additionally, is there any documentation and/or resources that woul...
16
2009-09-12T09:27:39Z
1,414,954
<p>Apple now encourages people to get the templates directly from the PyObjC project. There's a nice thread of explanation archived on <a href="http://www.cocoabuilder.com/archive/message/xcode/2009/8/31/30205">Cocoabuilder</a>, with the following advice from bbum:</p> <blockquote> <p>You'll need to download and ins...
29
2009-09-12T12:01:45Z
[ "python", "xcode", "pyobjc" ]
PyObjC development with Xcode 3.2
1,414,727
<p>Xcode 3.2 has removed the default templates for the scripting languages (Ruby, Python etc). How do I find these templates to use in Xcode 3.2? Would I need to add anything else to Xcode to support working with and 'building' PyObjC programs?</p> <p>Additionally, is there any documentation and/or resources that woul...
16
2009-09-12T09:27:39Z
1,632,544
<p>Check out this blog posting... fixed it perfectly for me: <a href="http://ioanna.me/2009/09/installing-pyobjc-xcode-templates-in-snow-leopard/" rel="nofollow">http://ioanna.me/2009/09/installing-pyobjc-xcode-templates-in-snow-leopard/</a></p>
4
2009-10-27T17:49:52Z
[ "python", "xcode", "pyobjc" ]
PyObjC development with Xcode 3.2
1,414,727
<p>Xcode 3.2 has removed the default templates for the scripting languages (Ruby, Python etc). How do I find these templates to use in Xcode 3.2? Would I need to add anything else to Xcode to support working with and 'building' PyObjC programs?</p> <p>Additionally, is there any documentation and/or resources that woul...
16
2009-09-12T09:27:39Z
17,125,659
<p>Xcode has been changed, this is the way you do it:</p> <ol> <li><p>Download the templates from <a href="https://github.com/gregneagle/Xcode4CocoaPythonTemplates" rel="nofollow">https://github.com/gregneagle/Xcode4CocoaPythonTemplates</a></p></li> <li><p>Copy the two folders, File Templates and Project Templates to ...
1
2013-06-15T16:27:24Z
[ "python", "xcode", "pyobjc" ]
Prompt on exit in PyQt application
1,414,781
<p>Is there any way to promt user to exit the gui-program written in Python?</p> <p>Something like "Are you sure you want to exit the program?"</p> <p>I'm using PyQt.</p>
13
2009-09-12T10:06:04Z
1,414,906
<p>Yes. You need to override the default close behaviour of the QWidget representing your application so that it doesn't immediately accept the event. The basic structure you want is something like this:</p> <pre><code>def closeEvent(self, event): quit_msg = "Are you sure you want to exit the program?" reply ...
31
2009-09-12T11:25:00Z
[ "python", "pyqt", "exit" ]
Profiling a python multiprocessing pool
1,414,841
<p>I'm trying to run cProfile.runctx() on each process in a multiprocessing pool, to get an idea of what the multiprocessing bottlenecks are in my source. Here is a simplified example of what I'm trying to do:</p> <pre><code>from multiprocessing import Pool import cProfile def square(i): return i*i def square_wr...
5
2009-09-12T10:41:21Z
1,414,859
<p>Try this:</p> <pre><code>def square_wrapper(i): result = [None] cProfile.runctx("result[0] = square(i)", globals(), locals(), "file_%d" % i) return result[0] </code></pre>
5
2009-09-12T10:54:16Z
[ "python", "profiling", "multiprocessing", "pool" ]
Using safe filter in Django for rich text fields
1,414,986
<p>I am using <a href="http://en.wikipedia.org/wiki/TinyMCE">TinyMCE</a> editor for textarea fileds in <a href="http://en.wikipedia.org/wiki/Django_%28web_framework%29">Django</a> forms.</p> <p>Now, in order to display the rich text back to the user, I am forced to use the "safe" filter in Django templates so that HTM...
7
2009-09-12T12:20:59Z
1,414,993
<p>You are right to be concerned about raw HTML, but not just for Javascript-disabled browsers. When considering the security of your server, you have to ignore any work done in the browser, and look solely at what the server accepts and what happens to it. Your server accepts HTML and displays it on the page. This ...
10
2009-09-12T12:26:04Z
[ "python", "django", "django-templates", "filter" ]
Using safe filter in Django for rich text fields
1,414,986
<p>I am using <a href="http://en.wikipedia.org/wiki/TinyMCE">TinyMCE</a> editor for textarea fileds in <a href="http://en.wikipedia.org/wiki/Django_%28web_framework%29">Django</a> forms.</p> <p>Now, in order to display the rich text back to the user, I am forced to use the "safe" filter in Django templates so that HTM...
7
2009-09-12T12:20:59Z
1,414,996
<p>There isn't a good answer to this one. TinyMCE generates HTML, and django's auto-escape specifically removes HTML. </p> <p>The traditional solution to this problem has been to either use some non-html markup language in the user input side (bbcode, markdown, etc.) or to whitelist a limited number of HTML tags. Tiny...
3
2009-09-12T12:27:11Z
[ "python", "django", "django-templates", "filter" ]
Using safe filter in Django for rich text fields
1,414,986
<p>I am using <a href="http://en.wikipedia.org/wiki/TinyMCE">TinyMCE</a> editor for textarea fileds in <a href="http://en.wikipedia.org/wiki/Django_%28web_framework%29">Django</a> forms.</p> <p>Now, in order to display the rich text back to the user, I am forced to use the "safe" filter in Django templates so that HTM...
7
2009-09-12T12:20:59Z
3,376,419
<p>You can use the template filter "<a href="http://docs.djangoproject.com/en/1.1/ref/templates/builtins/#removetags">removetags</a>" and just remove 'script'.</p>
8
2010-07-31T01:36:21Z
[ "python", "django", "django-templates", "filter" ]
Using safe filter in Django for rich text fields
1,414,986
<p>I am using <a href="http://en.wikipedia.org/wiki/TinyMCE">TinyMCE</a> editor for textarea fileds in <a href="http://en.wikipedia.org/wiki/Django_%28web_framework%29">Django</a> forms.</p> <p>Now, in order to display the rich text back to the user, I am forced to use the "safe" filter in Django templates so that HTM...
7
2009-09-12T12:20:59Z
27,523,737
<p>Use <a href="https://django-bleach.readthedocs.org/en/latest/" rel="nofollow">django-bleach</a>. This provides you with a <code>bleach</code> template filter that allows you to filter out just the tags you want:</p> <pre><code>{% load bleach_tags %} {{ mymodel.my_html_field|bleach }} </code></pre> <p>The trick is...
2
2014-12-17T10:45:04Z
[ "python", "django", "django-templates", "filter" ]
How do i set the PDU mode of a modem using python sms 0.3 module?
1,415,162
<p>Am using python <a href="http://pypi.python.org/pypi/sms/0.3" rel="nofollow">sms 0.3</a> module to access my modem on com port. Am trying to send an sms but am getting the following error</p> <p>sms.ModemError: ['\r\n', '+CMS ERROR: 304\r\n']</p> <p>When i read the Modem error codes, Error code 304 is for PDU mode...
0
2009-09-12T13:48:15Z
2,203,986
<p>Not sure if you can do it via the SMS library, but you can do it directly by sending via the serial port to the modem.: "AT+CMGF=0" sets PDU mode for SMS messages "AT+CMGF=1" sets text mode for SMS messages "AT+CMGF?" should give the current setting</p>
0
2010-02-04T23:23:08Z
[ "python", "sms", "modem" ]
Resources for developing Python and Google App Engine
1,415,208
<p>I would like to ask about some sources for developing applications with Python and Google App Engine.</p> <p>For example, some controls to generate automatically pages with the insert/update/delete of a database table, or any other useful resources are welcome.</p> <p>Thank you!</p>
2
2009-09-12T14:07:55Z
1,415,236
<p>The google app engine "Getting Started" tutorial is very good. The django documentation is also really detailed. Take a look at GoogleIO on youtube and watch some of the tutorials.</p>
3
2009-09-12T14:16:50Z
[ "python", "google-app-engine", "user-controls", "controls" ]
Resources for developing Python and Google App Engine
1,415,208
<p>I would like to ask about some sources for developing applications with Python and Google App Engine.</p> <p>For example, some controls to generate automatically pages with the insert/update/delete of a database table, or any other useful resources are welcome.</p> <p>Thank you!</p>
2
2009-09-12T14:07:55Z
1,419,518
<p>The Python community tends to look askance at code generation; so, @Hoang, if you think code generation is THE way to go, I suggest you try just about any other language BUT Python.</p> <p>@Dominic has already suggested some excellent resources, I could point you to more (App Engine Fan, App Engine Utilities, etc, ...
7
2009-09-14T03:08:32Z
[ "python", "google-app-engine", "user-controls", "controls" ]
Resources for developing Python and Google App Engine
1,415,208
<p>I would like to ask about some sources for developing applications with Python and Google App Engine.</p> <p>For example, some controls to generate automatically pages with the insert/update/delete of a database table, or any other useful resources are welcome.</p> <p>Thank you!</p>
2
2009-09-12T14:07:55Z
1,424,765
<p>App Engine Documentation <a href="http://code.google.com/appengine/docs/" rel="nofollow">http://code.google.com/appengine/docs/</a></p> <p>App Engine Google Group <a href="http://code.google.com/appengine/docs/" rel="nofollow">http://groups.google.com/group/google-appengine</a></p> <p>Google I/O conference videos ...
2
2009-09-15T02:01:27Z
[ "python", "google-app-engine", "user-controls", "controls" ]
How to improve speed of this readline loop in python?
1,415,369
<p>i'm importing several parts of a Databasedump in text Format into MySQL, the problem is that before the interesting Data there is very much non-interesting stuff infront. I wrote this loop to get to the needed data:</p> <pre><code>def readloop(DBFILE): txtdb=open(DBFILE, 'r') sline = "" # loop till 1st "custo...
2
2009-09-12T15:26:19Z
1,415,399
<p>This might help: <a href="http://braydon.com/blog/2009/2/11/python-performance-part-2%3A-parsing-large-strings-for-a-href-hypertext" rel="nofollow">Python Performance Part 2: Parsing Large Strings for 'A Href' Hypertext</a></p>
0
2009-09-12T15:40:33Z
[ "python", "loops", "readline" ]
How to improve speed of this readline loop in python?
1,415,369
<p>i'm importing several parts of a Databasedump in text Format into MySQL, the problem is that before the interesting Data there is very much non-interesting stuff infront. I wrote this loop to get to the needed data:</p> <pre><code>def readloop(DBFILE): txtdb=open(DBFILE, 'r') sline = "" # loop till 1st "custo...
2
2009-09-12T15:26:19Z
1,415,402
<p>I guess you are writing this import script and it gets boring to wait during testing it, so the data stays the same all the time.</p> <p>You can run the script once to detect the actual positions in the file you want to jump to, with <code>print txtdb.tell()</code>. Write those down and replace the searching code w...
1
2009-09-12T15:40:52Z
[ "python", "loops", "readline" ]
How to improve speed of this readline loop in python?
1,415,369
<p>i'm importing several parts of a Databasedump in text Format into MySQL, the problem is that before the interesting Data there is very much non-interesting stuff infront. I wrote this loop to get to the needed data:</p> <pre><code>def readloop(DBFILE): txtdb=open(DBFILE, 'r') sline = "" # loop till 1st "custo...
2
2009-09-12T15:26:19Z
1,415,407
<p>Tell us more about the file.</p> <p>Can you use file.seek to do a binary search? Seek to the halfway mark, read a few lines, determine if you are before or after the part you need, recurse. That will turn your O(n) search into O(logn).</p>
0
2009-09-12T15:42:35Z
[ "python", "loops", "readline" ]
How to improve speed of this readline loop in python?
1,415,369
<p>i'm importing several parts of a Databasedump in text Format into MySQL, the problem is that before the interesting Data there is very much non-interesting stuff infront. I wrote this loop to get to the needed data:</p> <pre><code>def readloop(DBFILE): txtdb=open(DBFILE, 'r') sline = "" # loop till 1st "custo...
2
2009-09-12T15:26:19Z
1,415,451
<p>The general idea for optimization is to proceed "by big blocks" (mostly-ignoring line structure) to locate the first line of interest, then move on to by-line processing for the rest). It's somewhat finicky and error-prone (off-by-one and the like) so it really needs testing, but the general idea is as follows...:</...
1
2009-09-12T16:10:31Z
[ "python", "loops", "readline" ]
How to improve speed of this readline loop in python?
1,415,369
<p>i'm importing several parts of a Databasedump in text Format into MySQL, the problem is that before the interesting Data there is very much non-interesting stuff infront. I wrote this loop to get to the needed data:</p> <pre><code>def readloop(DBFILE): txtdb=open(DBFILE, 'r') sline = "" # loop till 1st "custo...
2
2009-09-12T15:26:19Z
1,416,116
<p>Please do not write this code:</p> <pre><code>while condition is False: </code></pre> <p>Boolean conditions are <em>boolean</em> for cryin' out loud, so they can be tested (or negated and tested) directly:</p> <pre><code>while not condition: </code></pre> <p>Your second while loop isn't written as "while conditi...
5
2009-09-12T20:54:14Z
[ "python", "loops", "readline" ]
Does anybody use DjVu files in their production tools?
1,415,400
<p>When it's about archiving and doc portability, it's all about PDF. I heard about <a href="http://djvu.org/" rel="nofollow">DjVu</a> somes years ago, and it seems to be now mature enough for serious usages. The benefits seems to be a small size format and a fast open / read experience.</p> <p>But I have absolutely n...
5
2009-09-12T15:40:34Z
1,415,721
<p>I've found DjVu to be ideal for image-intensive documents. I used to sell books of highly details maps, and those were always in DjVu. PDF however works really well; it's a standard, and -everybody- will be able to open it without installing additional software.</p> <p>There's more info at: <a href="http://print-dr...
3
2009-09-12T17:50:40Z
[ "python", "pdf", "djvu" ]
How to get instance type of a win32com object?
1,415,405
<p>First of all, please excuse me for any incoherence in the tile of this question. It probably has some, but really don't know better.</p> <p>This question was raised in the context of controlling iTunes via COM from python.</p> <pre><code>&gt;&gt;&gt; itunes = win32com.client.Dispatch("iTunes.Application") &gt;&gt;...
1
2009-09-12T15:41:33Z
1,415,479
<p>Unfortunately I have no Windows machine at hand to try, but I think <code>itunes.__class__</code> is the (old_style) class of the <code>itunes</code> object in question, and <code>lib.__class__</code> that of <code>lib</code>. So looking at the <code>__name__</code> attribute of the classes should give you what you ...
1
2009-09-12T16:17:16Z
[ "python", "com", "itunes", "instance" ]
GQL get ID field
1,415,553
<p>On google appengine, you can retrieve the hash key value by doing a query like</p> <pre> select __key__ from furniture </pre> <p>But appengine datastore maintains a nicely autoincremented field ID for each kind.</p> <p>How can I access it from a query? </p> <pre> select ID from furniture </pre> <p>Doesn't work<...
3
2009-09-12T16:45:08Z
1,415,669
<p>I believe that a Gcl query cannot incorporate calls to accessor methods or attribute extraction (much in the same vein as the fact it can only do <code>"SELECT * FROM"</code> to fetch whole entities or <code>"SELECT __key__ FROM"</code> to fetch keys only -- it cannot pick and choose fields as in [hypothetical!-)] <...
4
2009-09-12T17:31:26Z
[ "python", "google-app-engine" ]
GQL get ID field
1,415,553
<p>On google appengine, you can retrieve the hash key value by doing a query like</p> <pre> select __key__ from furniture </pre> <p>But appengine datastore maintains a nicely autoincremented field ID for each kind.</p> <p>How can I access it from a query? </p> <pre> select ID from furniture </pre> <p>Doesn't work<...
3
2009-09-12T16:45:08Z
9,053,686
<p>you could also try </p> <pre><code>entity.key().id() </code></pre>
4
2012-01-29T13:53:15Z
[ "python", "google-app-engine" ]
GQL get ID field
1,415,553
<p>On google appengine, you can retrieve the hash key value by doing a query like</p> <pre> select __key__ from furniture </pre> <p>But appengine datastore maintains a nicely autoincremented field ID for each kind.</p> <p>How can I access it from a query? </p> <pre> select ID from furniture </pre> <p>Doesn't work<...
3
2009-09-12T16:45:08Z
17,932,630
<p>I am not sure if it was there back then when you have asked this question, but now it is pretty easy to retrieve entity by id. </p> <pre><code>YourModel.get_by_id (int(ids)) </code></pre> <p>You can read more about it here in the documentation: <a href="https://developers.google.com/appengine/docs/python/datastore...
0
2013-07-29T19:32:17Z
[ "python", "google-app-engine" ]
How to create hover effect on StaticBitmap in wxpython?
1,415,727
<p>I want to create hover effect on StaticBitmap - If the cursor of mouse is over the the bitmap, shows one image, if not, shows second image. It's trivial program (works perfectly with a button). However, StaticBitmap doesn't emit EVT_WINDOW_ENTER, EVT_WINDOW_LEAVE events.</p> <p>I can work with EVT_MOTION. If images...
4
2009-09-12T17:52:55Z
1,415,815
<p>It looks like this is a wxGTK bug, ENTER and LEAVE events work fine on windows. You should direct the attention of the core developers to the problem, a good place to do this is their <a href="http://trac.wxwidgets.org/" rel="nofollow">bug tracker</a>. This is an issue you should not have to work around IMHO.</p> <...
1
2009-09-12T18:44:06Z
[ "python", "image", "wxpython", "wxwidgets", "hover" ]
How to create hover effect on StaticBitmap in wxpython?
1,415,727
<p>I want to create hover effect on StaticBitmap - If the cursor of mouse is over the the bitmap, shows one image, if not, shows second image. It's trivial program (works perfectly with a button). However, StaticBitmap doesn't emit EVT_WINDOW_ENTER, EVT_WINDOW_LEAVE events.</p> <p>I can work with EVT_MOTION. If images...
4
2009-09-12T17:52:55Z
1,416,831
<p>There may be bug in wxStaticBitmap implementation, but if wxBitmapButton works you can use it for same effect, with less code</p> <pre><code>#!/usr/bin/python # -*- coding: utf-8 -*- import wx app = wx.App() frame = wx.Frame(None, wx.ID_ANY, title="Hover effect") w = wx.Window(frame) c = wx.BitmapButton(w, -1, w...
0
2009-09-13T04:13:45Z
[ "python", "image", "wxpython", "wxwidgets", "hover" ]
Why use **kwargs in python? What are some real world advantages over using named arguments?
1,415,812
<p>I come from a background in static languages. Can someone explain (ideally through example) the real world <strong>advantages of using **kwargs over named arguments</strong>?</p> <p>To me it only seems to make the function call more ambiguous. Thanks.</p>
42
2009-09-12T18:38:01Z
1,415,817
<p><code>**kwargs</code> are good if you don't know in advance the name of the parameters. For example the <code>dict</code> constructor uses them to initialize the keys of the new dictionary. </p> <blockquote> <pre><code>dict(**kwargs) -&gt; new dictionary initialized with the name=value pairs in the keyword argu...
3
2009-09-12T18:45:42Z
[ "python", "kwargs" ]
Why use **kwargs in python? What are some real world advantages over using named arguments?
1,415,812
<p>I come from a background in static languages. Can someone explain (ideally through example) the real world <strong>advantages of using **kwargs over named arguments</strong>?</p> <p>To me it only seems to make the function call more ambiguous. Thanks.</p>
42
2009-09-12T18:38:01Z
1,415,826
<p>Real-world examples:</p> <p>Decorators - they're usually generic, so you can't specify the arguments upfront:</p> <pre><code>def decorator(old): def new(*args, **kwargs): # ... return old(*args, **kwargs) return new </code></pre> <p>Places where you want to do magic with an unknown number ...
29
2009-09-12T18:50:27Z
[ "python", "kwargs" ]
Why use **kwargs in python? What are some real world advantages over using named arguments?
1,415,812
<p>I come from a background in static languages. Can someone explain (ideally through example) the real world <strong>advantages of using **kwargs over named arguments</strong>?</p> <p>To me it only seems to make the function call more ambiguous. Thanks.</p>
42
2009-09-12T18:38:01Z
1,415,827
<p>One example is implementing <a href="http://stackoverflow.com/questions/277922/python-argument-binders">python-argument-binders</a>, used like this:</p> <blockquote> <pre><code>&gt;&gt;&gt; from functools import partial &gt;&gt;&gt; def f(a, b): ... return a+b &gt;&gt;&gt; p = partial(f, 1, 2) &gt;&gt;&gt; p() ...
0
2009-09-12T18:50:59Z
[ "python", "kwargs" ]
Why use **kwargs in python? What are some real world advantages over using named arguments?
1,415,812
<p>I come from a background in static languages. Can someone explain (ideally through example) the real world <strong>advantages of using **kwargs over named arguments</strong>?</p> <p>To me it only seems to make the function call more ambiguous. Thanks.</p>
42
2009-09-12T18:38:01Z
1,415,830
<p>You may want to accept nearly-arbitrary named arguments for a series of reasons -- and that's what the <code>**kw</code> form lets you do.</p> <p>The most common reason is to pass the arguments right on to some other function you're wrapping (decorators are one case of this, but FAR from the only one!) -- in this c...
47
2009-09-12T18:52:09Z
[ "python", "kwargs" ]
Why use **kwargs in python? What are some real world advantages over using named arguments?
1,415,812
<p>I come from a background in static languages. Can someone explain (ideally through example) the real world <strong>advantages of using **kwargs over named arguments</strong>?</p> <p>To me it only seems to make the function call more ambiguous. Thanks.</p>
42
2009-09-12T18:38:01Z
1,415,840
<p>There are two common cases:</p> <p>First: You are wrapping another function which takes a number of keyword argument, but you are just going to pass them along:</p> <pre><code>def my_wrapper(a, b, **kwargs): do_something_first(a, b) the_real_function(**kwargs) </code></pre> <p>Second: You are willing to a...
9
2009-09-12T18:56:22Z
[ "python", "kwargs" ]
Why use **kwargs in python? What are some real world advantages over using named arguments?
1,415,812
<p>I come from a background in static languages. Can someone explain (ideally through example) the real world <strong>advantages of using **kwargs over named arguments</strong>?</p> <p>To me it only seems to make the function call more ambiguous. Thanks.</p>
42
2009-09-12T18:38:01Z
1,415,882
<p>Another reason you might want to use <code>**kwargs</code> (and <code>*args</code>) is if you're extending an existing method in a subclass. You want to pass all the existing arguments onto the superclass's method, but want to ensure that your class keeps working even if the signature changes in a future version:</p...
29
2009-09-12T19:15:33Z
[ "python", "kwargs" ]
Why use **kwargs in python? What are some real world advantages over using named arguments?
1,415,812
<p>I come from a background in static languages. Can someone explain (ideally through example) the real world <strong>advantages of using **kwargs over named arguments</strong>?</p> <p>To me it only seems to make the function call more ambiguous. Thanks.</p>
42
2009-09-12T18:38:01Z
1,415,956
<p>Here's an example, I used in CGI Python. I created a class that took <code>**kwargs</code> to the <code>__init__</code> function. That allowed me to emulate the DOM on the server-side with classes:</p> <pre><code>document = Document() document.add_stylesheet('style.css') document.append(Div(H1('Imagist\'s Page Ti...
2
2009-09-12T19:49:47Z
[ "python", "kwargs" ]
Why use **kwargs in python? What are some real world advantages over using named arguments?
1,415,812
<p>I come from a background in static languages. Can someone explain (ideally through example) the real world <strong>advantages of using **kwargs over named arguments</strong>?</p> <p>To me it only seems to make the function call more ambiguous. Thanks.</p>
42
2009-09-12T18:38:01Z
1,416,928
<p>And here's another typical example:</p> <pre><code>MESSAGE = "Lo and behold! A message {message!r} came from {object_} with data {data!r}." def proclaim(object_, message, data): print(MESSAGE.format(**locals())) </code></pre>
1
2009-09-13T05:30:16Z
[ "python", "kwargs" ]
how can i obtain pattern string from compiled regexp pattern in python
1,415,924
<p>I have some code like this one:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; p = re.compile('my pattern') &gt;&gt;&gt; print p _sre.SRE_Pattern object at 0x02274380 </code></pre> <p>Is it possible to get string <code>"my pattern"</code> from <code>p</code> variable?</p>
49
2009-09-12T19:34:41Z
1,415,932
<pre><code>p.pattern </code></pre> <p>Read more about re module here: <a href="http://docs.python.org/library/re.html">http://docs.python.org/library/re.html</a></p>
52
2009-09-12T19:38:30Z
[ "python", "regex" ]
how can i obtain pattern string from compiled regexp pattern in python
1,415,924
<p>I have some code like this one:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; p = re.compile('my pattern') &gt;&gt;&gt; print p _sre.SRE_Pattern object at 0x02274380 </code></pre> <p>Is it possible to get string <code>"my pattern"</code> from <code>p</code> variable?</p>
49
2009-09-12T19:34:41Z
1,415,934
<p>Yes:</p> <pre><code>print p.pattern </code></pre> <p>hint, use the <code>dir</code> function in python to obtain a list of members:</p> <pre><code>dir(p) </code></pre> <p>this lists:</p> <pre><code>['__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattrib...
4
2009-09-12T19:39:16Z
[ "python", "regex" ]
how can i obtain pattern string from compiled regexp pattern in python
1,415,924
<p>I have some code like this one:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; p = re.compile('my pattern') &gt;&gt;&gt; print p _sre.SRE_Pattern object at 0x02274380 </code></pre> <p>Is it possible to get string <code>"my pattern"</code> from <code>p</code> variable?</p>
49
2009-09-12T19:34:41Z
1,415,943
<p>Thanks. p.pattern really works. But my dir don't display this attribute</p> <pre> <code> >>> sys.version '2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]' >>> dir(p) ['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner', 'search', 'split', 'sub', 'subn'] >>> p.pattern 'my patt...
0
2009-09-12T19:44:50Z
[ "python", "regex" ]
how can i obtain pattern string from compiled regexp pattern in python
1,415,924
<p>I have some code like this one:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; p = re.compile('my pattern') &gt;&gt;&gt; print p _sre.SRE_Pattern object at 0x02274380 </code></pre> <p>Is it possible to get string <code>"my pattern"</code> from <code>p</code> variable?</p>
49
2009-09-12T19:34:41Z
1,415,977
<p>From the <a href="http://docs.python.org/library/re.html#regular-expression-objects">"Regular Expression Objects"</a> section of the <code>re</code> module documentation:</p> <blockquote> <p><code>RegexObject.pattern</code></p> <p>The pattern string from which the RE object was compiled.</p> </blockquote> <...
12
2009-09-12T19:59:20Z
[ "python", "regex" ]
Python - Virtualenv , python 3?
1,416,005
<p>Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to h...
8
2009-09-12T20:10:43Z
1,416,051
<p>Not sure if I understood you correctly, but here goes :) </p> <p>I don't know about OS X, but in Linux you can install both 2.6 and 3. Then you can either specify to use python25 or python3, or change the /usr/bin/python symlink to the version you want to use by default.</p>
0
2009-09-12T20:25:12Z
[ "python", "osx", "python-3.x", "pygame", "virtualenv" ]
Python - Virtualenv , python 3?
1,416,005
<p>Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to h...
8
2009-09-12T20:10:43Z
1,416,152
<p><code>virtualenv</code> is designed to create isolated environments of a Python environment. The trick to using it with multiple Python instances is to either install <code>virtualenv</code> into each of the Python versions you want to use it with, for example:</p> <pre><code>/usr/bin/easy_install-2.6 virtualenv /...
1
2009-09-12T21:12:19Z
[ "python", "osx", "python-3.x", "pygame", "virtualenv" ]
Python - Virtualenv , python 3?
1,416,005
<p>Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to h...
8
2009-09-12T20:10:43Z
1,416,162
<p>Your use case doesn't actually need virtualenv. You just need to install several different Python versions.</p>
3
2009-09-12T21:18:14Z
[ "python", "osx", "python-3.x", "pygame", "virtualenv" ]
Python - Virtualenv , python 3?
1,416,005
<p>Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to h...
8
2009-09-12T20:10:43Z
3,211,439
<p>For me <a href="http://bitbucket.org/brandon/virtualenv3" rel="nofollow">virtualenv3</a> works very well. I also have pypi.python.org/pypi/distribute installed. That also works with the mentioned www.doughellmann.com/docs/virtualenvwrapper/ . I've only tested that on Linux though.</p>
1
2010-07-09T09:39:29Z
[ "python", "osx", "python-3.x", "pygame", "virtualenv" ]
Python - Virtualenv , python 3?
1,416,005
<p>Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to h...
8
2009-09-12T20:10:43Z
28,298,765
<p>It's an old question by now, but I found it myself on top of google search for the answer, and I don't think the answers provided are what people are looking for.</p> <p>As I understand it you want to create different virtual environments with different Python versions?</p> <p>This is very easy, and you only need ...
11
2015-02-03T12:19:05Z
[ "python", "osx", "python-3.x", "pygame", "virtualenv" ]
To Ruby or not to Ruby
1,416,570
<p>I know that this is a difficult question to answer, but I thought I would try anwyays....</p> <p>I am just starting at a new company where they have a minimal existing code base. It probably has a month of man effort invested at this point. It is currently written in Ruby. </p> <p>It is also currently using Ruby...
1
2009-09-13T00:59:06Z
1,416,574
<p>Rails apps can potentially make a great REST backend for a php based system.</p> <p>The REST stuff is made for it. You want xml? ok. You want json? ok. </p> <p>If you and the staff aren't comfy with it, use something you are more comfy in to write a REST interface. :)</p>
1
2009-09-13T01:01:23Z
[ "python", "ruby" ]
To Ruby or not to Ruby
1,416,570
<p>I know that this is a difficult question to answer, but I thought I would try anwyays....</p> <p>I am just starting at a new company where they have a minimal existing code base. It probably has a month of man effort invested at this point. It is currently written in Ruby. </p> <p>It is also currently using Ruby...
1
2009-09-13T00:59:06Z
1,416,583
<p>Why wouldn't you just keep it consistent and use PHP?</p>
0
2009-09-13T01:07:16Z
[ "python", "ruby" ]
To Ruby or not to Ruby
1,416,570
<p>I know that this is a difficult question to answer, but I thought I would try anwyays....</p> <p>I am just starting at a new company where they have a minimal existing code base. It probably has a month of man effort invested at this point. It is currently written in Ruby. </p> <p>It is also currently using Ruby...
1
2009-09-13T00:59:06Z
1,416,625
<p>IMO, if you're comfortable with Python then you shouldn't have too much trouble picking up Ruby. This doesn't mean Ruby is the best choice - you should still evaluate the options.</p>
0
2009-09-13T01:40:25Z
[ "python", "ruby" ]
To Ruby or not to Ruby
1,416,570
<p>I know that this is a difficult question to answer, but I thought I would try anwyays....</p> <p>I am just starting at a new company where they have a minimal existing code base. It probably has a month of man effort invested at this point. It is currently written in Ruby. </p> <p>It is also currently using Ruby...
1
2009-09-13T00:59:06Z
1,416,827
<h3>Ruby Madness. Sure, it seems cool, but it might lead to the hard stuff: Lisp</h3> <hr> <p>My predictions:</p> <ol> <li><p>It's pretty clear that you are OK with either <strong>Ruby</strong> or <strong>Python</strong>, and obviously <strong>php</strong> can be made to work. </p></li> <li><p>You will really like <...
4
2009-09-13T04:11:43Z
[ "python", "ruby" ]
To Ruby or not to Ruby
1,416,570
<p>I know that this is a difficult question to answer, but I thought I would try anwyays....</p> <p>I am just starting at a new company where they have a minimal existing code base. It probably has a month of man effort invested at this point. It is currently written in Ruby. </p> <p>It is also currently using Ruby...
1
2009-09-13T00:59:06Z
1,417,451
<p>My advice would depend on your own goals, which might look like this... you might want to ask yourself (or score each of these from 1-10) if you prefer to:</p> <ol> <li>learn a new language you might use in future? = Ruby</li> <li>deepen your Python skills by using it for everything (say <a href="http://www.djangop...
7
2009-09-13T10:53:08Z
[ "python", "ruby" ]
To Ruby or not to Ruby
1,416,570
<p>I know that this is a difficult question to answer, but I thought I would try anwyays....</p> <p>I am just starting at a new company where they have a minimal existing code base. It probably has a month of man effort invested at this point. It is currently written in Ruby. </p> <p>It is also currently using Ruby...
1
2009-09-13T00:59:06Z
25,123,266
<p>Using Ruby für two years now , i find it powerful, easy to learn, having good libraries, good community.</p> <p>But how should we know if it fits your needs? Try it an you will find out.</p>
0
2014-08-04T16:25:39Z
[ "python", "ruby" ]
pythonic way to convert variable to list
1,416,646
<p>I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner. </p> <p>Currently I have this:</p> <pre><code>def my_func(input): if not isinstance(input, list): input = [...
7
2009-09-13T02:01:29Z
1,416,659
<p>This seems like a reasonable way to do it. You're wanting to test if the element is a list, and this accomplishes that directly. It gets more complicated if you want to support other 'list-like' data types, too, for example:</p> <pre><code>isinstance(input, (list, tuple)) </code></pre> <p>or more generally, abstra...
-1
2009-09-13T02:14:30Z
[ "python", "list", "arguments" ]
pythonic way to convert variable to list
1,416,646
<p>I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner. </p> <p>Currently I have this:</p> <pre><code>def my_func(input): if not isinstance(input, list): input = [...
7
2009-09-13T02:01:29Z
1,416,668
<p>You can do direct type comparisons using <code>type()</code>.</p> <pre><code>def my_func(input): if not type(input) is list: input = [input] for e in input: # do something </code></pre> <p>However, the way you have it will allow <em>any</em> type derived from the <code>list</code> type to b...
0
2009-09-13T02:17:16Z
[ "python", "list", "arguments" ]
pythonic way to convert variable to list
1,416,646
<p>I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner. </p> <p>Currently I have this:</p> <pre><code>def my_func(input): if not isinstance(input, list): input = [...
7
2009-09-13T02:01:29Z
1,416,672
<p>Your aproach seems right to me.</p> <p>It's similar to how you use <code>atom?</code> in Lisp when you iterate over lists and check the current item to see if it is a list or not, because if it is a list you want to process its items, too.</p> <p>So, yeah, don't see anything wrong with that.</p>
0
2009-09-13T02:17:43Z
[ "python", "list", "arguments" ]
pythonic way to convert variable to list
1,416,646
<p>I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner. </p> <p>Currently I have this:</p> <pre><code>def my_func(input): if not isinstance(input, list): input = [...
7
2009-09-13T02:01:29Z
1,416,674
<p>You can put * before your argument, this way you'll always get a tuple:</p> <pre><code>def a(*p): print type(p) print p a(4) &gt;&gt;&gt; &lt;type 'tuple'&gt; &gt;&gt;&gt; (4,) a(4, 5) &gt;&gt;&gt; &lt;type 'tuple'&gt; &gt;&gt;&gt; (4,5,) </code></pre> <p>But that will force you to call your function with va...
2
2009-09-13T02:19:59Z
[ "python", "list", "arguments" ]
pythonic way to convert variable to list
1,416,646
<p>I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner. </p> <p>Currently I have this:</p> <pre><code>def my_func(input): if not isinstance(input, list): input = [...
7
2009-09-13T02:01:29Z
1,416,677
<p>Typically, strings (plain and unicode) are the only iterables that you want to nevertheless consider as "single elements" -- the <code>basestring</code> builtin exists SPECIFICALLY to let you test for either kind of strings with <code>isinstance</code>, so it's very UN-grotty for that special case;-).</p> <p>So my ...
9
2009-09-13T02:24:29Z
[ "python", "list", "arguments" ]
pythonic way to convert variable to list
1,416,646
<p>I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner. </p> <p>Currently I have this:</p> <pre><code>def my_func(input): if not isinstance(input, list): input = [...
7
2009-09-13T02:01:29Z
1,416,678
<p>That is an ok way to do it (don't forget to include tuples).</p> <p>However, you may also want to consider if the argument has a __iter__ <a href="http://docs.python.org/reference/datamodel.html#object.%5F%5Fiter%5F%5F" rel="nofollow">method</a> or __getitem__ <a href="http://docs.python.org/reference/datamodel.htm...
0
2009-09-13T02:24:46Z
[ "python", "list", "arguments" ]
pythonic way to convert variable to list
1,416,646
<p>I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner. </p> <p>Currently I have this:</p> <pre><code>def my_func(input): if not isinstance(input, list): input = [...
7
2009-09-13T02:01:29Z
1,416,684
<p>I like Andrei Vajna's suggestion of <code>hasattr(var,'__iter__')</code>. Note these results from some typical Python types:</p> <pre><code>&gt;&gt;&gt; hasattr("abc","__iter__") False &gt;&gt;&gt; hasattr((0,),"__iter__") True &gt;&gt;&gt; hasattr({},"__iter__") True &gt;&gt;&gt; hasattr(set(),"__iter__") True </...
6
2009-09-13T02:29:35Z
[ "python", "list", "arguments" ]
pythonic way to convert variable to list
1,416,646
<p>I have a function whose input argument can either be an element or a list of elements. If this argument is a single element then I put it in a list so I can iterate over the input in a consistent manner. </p> <p>Currently I have this:</p> <pre><code>def my_func(input): if not isinstance(input, list): input = [...
7
2009-09-13T02:01:29Z
1,416,924
<p>First, there is no general method that could tell a "single element" from "list of elements" since by definition list can be an element of another list.</p> <p>I would say you need to define what kinds of data you might have, so that you might have:</p> <ul> <li>any descendant of <code>list</code> against anything...
1
2009-09-13T05:25:58Z
[ "python", "list", "arguments" ]
Emacs defadvice on python-mode function
1,416,882
<p>In python-mode, there is a function called py-execute-region which sends a highlighted region of code to the Python buffer for evaluation. After evaluation, the cursor is in the Python buffer, but I would prefer that it remain in the script buffer so I can continue producing more code. I wrote a simple advising func...
5
2009-09-13T04:52:45Z
1,417,682
<p>What you have there works fine for me. And it should auto-activate, so a separate activation should be unnecessary. However, you do need to de-active and re-activate advice for changes to take effect:</p> <p>1) define and activate advice</p> <p>2) it doesn't do what you want, so change the advice</p> <p>3) deac...
1
2009-09-13T12:51:22Z
[ "python", "emacs", "elisp", "advising-functions", "defadvice" ]
Emacs defadvice on python-mode function
1,416,882
<p>In python-mode, there is a function called py-execute-region which sends a highlighted region of code to the Python buffer for evaluation. After evaluation, the cursor is in the Python buffer, but I would prefer that it remain in the script buffer so I can continue producing more code. I wrote a simple advising func...
5
2009-09-13T04:52:45Z
1,418,640
<p>Use around-advice to wrap the function in a call to <code>save-window-excursion</code>, which will restore the previous window configuration after the command completes.</p> <pre><code>(defadvice py-execute-region (around preserve-window-configuration activate) "After execution, return cursor to script buffer...
2
2009-09-13T19:30:32Z
[ "python", "emacs", "elisp", "advising-functions", "defadvice" ]