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
Creating a list of objects in Python
348,196
<p>I'm trying to create a Python script that opens several databases and compares their contents. In the process of creating that script, I've run into a problem in creating a list whose contents are objects that I've created.</p> <p>I've simplified the program to its bare bones for this posting. First I create a new...
42
2008-12-07T22:15:46Z
352,505
<p>To fill a list with seperate instances of a class, you can use a for loop in the declaration of the list. The * multiply will link each copy to the same instance.</p> <pre><code>instancelist = [ MyClass() for i in range(29)] </code></pre> <p>and then access the instances through the index of the list.</p> <pre><c...
31
2008-12-09T11:50:39Z
[ "python", "list", "object", "loops" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
348,396
<p>Yes, you can do pretty much everything with the built-in libraries. Do a search here looking for the tags <code>[python]</code> and <code>[email]</code> and you'll see how it's done.</p>
2
2008-12-08T00:15:54Z
[ "python", "email" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
348,403
<p>poplib and smtplib will be your friends when developing your app.</p>
4
2008-12-08T00:21:53Z
[ "python", "email" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
348,423
<p>Python has an SMTPD module that will be helpful to you for writing a server. You'll probably also want the SMTP module to do the re-send. Both modules are in the standard library at least since version 2.3.</p>
6
2008-12-08T00:39:49Z
[ "python", "email" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
348,551
<p>Here is a very simple example:</p> <pre><code>import smtplib server = 'mail.server.com' user = '' password = '' recipients = ['[email protected]', '[email protected]'] sender = '[email protected]' message = 'Hello World' session = smtplib.SMTP(server) # if your SMTP server doesn't need authentications, # you don't need the ...
21
2008-12-08T02:36:49Z
[ "python", "email" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
348,579
<p>Depending on the amount of mail you are sending you might want to look into using a real mail server like postifx or sendmail (*nix systems) Both of those programs have the ability to send a received mail to a program based on the email address. </p>
2
2008-12-08T03:06:33Z
[ "python", "email" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
349,001
<p>The sending part has been covered, for the receiving you can use <a href="http://docs.python.org/library/poplib.html" rel="nofollow">pop</a> or <a href="http://docs.python.org/library/imaplib.html" rel="nofollow">imap</a></p>
3
2008-12-08T09:37:39Z
[ "python", "email" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
349,352
<p>I do not think it would be a good idea to write a real mail server in Python. This is certainly possible (see mcrute's and Manuel Ceron's posts to have details) but it is a lot of work when you think of everything that a real mail server must handle (queuing, retransmission, dealing with spam, etc).</p> <p>You shou...
10
2008-12-08T12:23:21Z
[ "python", "email" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
26,783,166
<p>Found a helpful example for reading emails by connecting using IMAP: </p> <p><a href="http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/">Python — imaplib IMAP example with Gmail</a></p> <pre><code>import imaplib mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('[email protected]...
6
2014-11-06T15:28:55Z
[ "python", "email" ]
Receive and send emails in python
348,392
<p>How can i receive and send email in python? A 'mail server' of sorts.</p> <p>I am looking into making an app that listens to see if it recieves an email addressed to [email protected], and sends an email to the sender.</p> <p>Now, am i able to do this all in python, would it be best to use 3rd party libraries? </...
29
2008-12-08T00:12:25Z
30,649,434
<p>The best way to do this would be to create a windows service in python that receives the emails using imaplib2</p> <p>Below is a sample python script to do the same.You can install this script to run as a windows service by running the following command on the command line "python THENAMEOFYOURSCRIPTFILE.py instal...
0
2015-06-04T16:35:48Z
[ "python", "email" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
348,649
<p>I'm not an expert on Perl, but what I do know is that GMail supports IMAP and POP3, 2 protocols that are completely standard and allow you to do just that. </p> <p>Maybe that helps you to get started. </p>
10
2008-12-08T04:17:37Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
348,696
<p>Within gmail, you can filter on "has:attachment", use it to identify the messages you should be getting when testing. Note this appears to give both messages with attached files (paperclip icon shown), as well as inline attached images (no paperclip shown).</p> <p>There is no Gmail API, so IMAP or POP are your only...
4
2008-12-08T05:10:38Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
348,715
<p>Since Gmail supports the standard protocols POP and IMAP, any platform, tool, application, component, or API that provides the client side of either protocol should work.</p> <p>I suggest doing a Google search for your favorite language/platform (e.g., "python"), plus "pop", plus "imap", plus perhaps "open source",...
1
2008-12-08T05:23:54Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
413,041
<p>You should be aware of the fact that you need SSL to connect to GMail (both for POP3 and IMAP - this is of course true also for their SMTP-servers apart from port 25 but that's another story).</p>
1
2009-01-05T13:07:02Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
641,409
<p>Have you taken a look at the <a href="http://en.wikipedia.org/wiki/Gmail#Gmail%5F3rd%5Fparty%5FAdd-Ins" rel="nofollow">GMail 3rd party add-ons</a> at wikipedia?</p> <p>In particular, <a href="http://en.wikipedia.org/wiki/PhpGmailDrive" rel="nofollow">PhpGmailDrive</a> is an open source add-on that you may be able t...
0
2009-03-13T03:52:27Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
641,843
<pre><code>#!/usr/bin/env python """Save all attachments for given gmail account.""" import os, sys from libgmail import GmailAccount ga = GmailAccount("[email protected]", "pA$$w0Rd_") ga.login() # folders: inbox, starred, all, drafts, sent, spam for thread in ga.getMessagesByFolder('all', allPages=True): f...
7
2009-03-13T08:40:25Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
642,335
<p>For Java, you will find <a href="http://g4j.sourceforge.net/" rel="nofollow">G4J</a> of use. It's a set of APIs to communicate with Google Mail via Java (the screenshot on the homepage is a demonstration email client built around this)</p>
0
2009-03-13T11:56:17Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
642,988
<p>Hard one :-)</p> <pre><code>import email, getpass, imaplib, os detach_dir = '.' # directory where to save attachments (default: current) user = raw_input("Enter your GMail username:") pwd = getpass.getpass("Enter your password: ") # connecting to the gmail imap server m = imaplib.IMAP4_SSL("imap.gmail.com") m.log...
141
2009-03-13T14:34:44Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
643,366
<p>Take a look at <a href="http://search.cpan.org/~mincus/Mail-Webmail-Gmail-1.09/lib/Mail/Webmail/Gmail.pm#GETTING%5FATTACHMENTS">Mail::Webmail::Gmail</a>:</p> <p><strong>GETTING ATTACHMENTS</strong></p> <p>There are two ways to get an attachment:</p> <p>1 -> By sending a reference to a specific attachment returned...
7
2009-03-13T15:52:00Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
4,172,062
<p>Here's something I wrote to download my bank statements in <a href="http://groovy.codehaus.org/" rel="nofollow">Groovy</a> (dynamic language for the Java Platform).</p> <pre><code>import javax.mail.* import java.util.Properties String gmailServer int gmailPort def user, password, LIMIT def inboxFolder, root, Star...
1
2010-11-13T10:32:33Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
18,259,764
<p>If any of you have updated to python 3.3 I took the 2.7 script from <a href="https://gist.github.com/baali/2633554" rel="nofollow">HERE</a> and updated it to 3.3. Also fixed some issues with the way gmail was returning the information.</p> <pre><code># Something in lines of http://stackoverflow.com/questions/34863...
2
2013-08-15T19:02:05Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
26,009,465
<pre><code>/*based on http://www.codejava.net/java-ee/javamail/using-javamail-for-searching-e-mail-messages*/ package getMailsWithAtt; import java.io.File; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import javax.m...
1
2014-09-24T05:48:52Z
[ "java", "python", "perl", "gmail" ]
How can I download all emails with attachments from Gmail?
348,630
<p>How do I connect to Gmail and determine which messages have attachments? I then want to download each attachment, printing out the Subject: and From: for each message as I process it.</p>
80
2008-12-08T03:57:49Z
37,088,611
<p>The question is quite old and at that time Gmail API was not available. But now Google provides Gmail API to access IMAP. Please see Google's Gmail API <a href="https://developers.google.com/gmail/api/quickstart/python" rel="nofollow">here</a>.</p>
1
2016-05-07T12:35:30Z
[ "java", "python", "perl", "gmail" ]
Adding a mimetype in python
348,999
<p>On my Centos server Python's mimetypes.guess_type("mobile.3gp") returns (None, None), instead of ('video/3gpp', None).</p> <p>Where does Python get the list of mimetypes from, and is it possible to add a missing type to the list?</p>
6
2008-12-08T09:33:04Z
349,020
<p>On my system (Debian lenny) its in /usr/lib/python2.5/mimetypes.py in the list <code>knownfiles</code> you can supply your own files for the <code>init()</code> function.</p>
5
2008-12-08T09:45:31Z
[ "python", "mime-types" ]
Adding a mimetype in python
348,999
<p>On my Centos server Python's mimetypes.guess_type("mobile.3gp") returns (None, None), instead of ('video/3gpp', None).</p> <p>Where does Python get the list of mimetypes from, and is it possible to add a missing type to the list?</p>
6
2008-12-08T09:33:04Z
349,024
<p>The mimetypes module uses mime.types files as they are common on Linux/Unix systems. If you look in mimetypes.knownfiles you will find a list of files that Python tries to access to load the data. You can also specify your own file to add new types by adding it to that list.</p>
2
2008-12-08T09:46:40Z
[ "python", "mime-types" ]
Formatting a data structure into a comma-separated list of arguments
349,175
<p>I need to convert a list (or a dict) into a comma-separated list for passing to another language.</p> <p>Is there a nicer way of doing this than:</p> <pre><code> result = '' args = ['a', 'b', 'c', 'd'] i = 0 for arg in args: if i != 0: result += arg else: result += arg + ', ' i += 1 ...
6
2008-12-08T11:00:39Z
349,182
<p><code>', '.join(args)</code> will do the trick.</p>
12
2008-12-08T11:03:54Z
[ "python", "refactoring", "list" ]
Formatting a data structure into a comma-separated list of arguments
349,175
<p>I need to convert a list (or a dict) into a comma-separated list for passing to another language.</p> <p>Is there a nicer way of doing this than:</p> <pre><code> result = '' args = ['a', 'b', 'c', 'd'] i = 0 for arg in args: if i != 0: result += arg else: result += arg + ', ' i += 1 ...
6
2008-12-08T11:00:39Z
349,197
<pre><code>'function(%s)' % ', '.join(args) </code></pre> <p>produces</p> <pre><code>'function(a, b, c, d)' </code></pre>
11
2008-12-08T11:09:37Z
[ "python", "refactoring", "list" ]
Formatting a data structure into a comma-separated list of arguments
349,175
<p>I need to convert a list (or a dict) into a comma-separated list for passing to another language.</p> <p>Is there a nicer way of doing this than:</p> <pre><code> result = '' args = ['a', 'b', 'c', 'd'] i = 0 for arg in args: if i != 0: result += arg else: result += arg + ', ' i += 1 ...
6
2008-12-08T11:00:39Z
349,229
<p>Why not use a standard that both languages can parse, like JSON, XML, or YAML? <a href="http://pypi.python.org/pypi/simplejson" rel="nofollow">simplejson</a> is handy, and included as json in python 2.6.</p>
0
2008-12-08T11:21:52Z
[ "python", "refactoring", "list" ]
Formatting a data structure into a comma-separated list of arguments
349,175
<p>I need to convert a list (or a dict) into a comma-separated list for passing to another language.</p> <p>Is there a nicer way of doing this than:</p> <pre><code> result = '' args = ['a', 'b', 'c', 'd'] i = 0 for arg in args: if i != 0: result += arg else: result += arg + ', ' i += 1 ...
6
2008-12-08T11:00:39Z
351,629
<pre><code>result = 'function (%s)' % ', '.join(map(str,args)) </code></pre> <p>I recommend the map(str, args) instead of just args because some of your arguments could potentially not be strings and would cause a TypeError, for example, with an int argument in your list:</p> <pre><code>Traceback (most recent call la...
1
2008-12-09T02:39:34Z
[ "python", "refactoring", "list" ]
How do I find the "concrete class" of a django model baseclass
349,206
<p>I'm trying to find the actual class of a django-model object, when using model-inheritance.</p> <p>Some code to describe the problem:</p> <pre><code>class Base(models.model): def basemethod(self): ... class Child_1(Base): pass class Child_2(Base): pass </code></pre> <p>If I create various ob...
10
2008-12-08T11:11:45Z
349,235
<p>It feels brittle because it is. (This is a reprint of an answer in a different context. <a href="http://stackoverflow.com/questions/243082/c-casting-programmatically-can-it-be-done">See C++ casting programmatically : can it be done ?</a>)</p> <p>Read up on polymorphism. Almost every "dynamic cast" situation is an...
-2
2008-12-08T11:23:59Z
[ "python", "django", "inheritance", "django-models" ]
How do I find the "concrete class" of a django model baseclass
349,206
<p>I'm trying to find the actual class of a django-model object, when using model-inheritance.</p> <p>Some code to describe the problem:</p> <pre><code>class Base(models.model): def basemethod(self): ... class Child_1(Base): pass class Child_2(Base): pass </code></pre> <p>If I create various ob...
10
2008-12-08T11:11:45Z
349,494
<p>Django implements model inheritance with a OneToOneField between the parent model's table and the child model's table. When you do <code>Base.object.all()</code>, Django is querying just the Base table, and so has no way of knowing what the child table is. Therefore, unfortunately, it's not possible to go directly...
12
2008-12-08T13:05:59Z
[ "python", "django", "inheritance", "django-models" ]
How do I find the "concrete class" of a django model baseclass
349,206
<p>I'm trying to find the actual class of a django-model object, when using model-inheritance.</p> <p>Some code to describe the problem:</p> <pre><code>class Base(models.model): def basemethod(self): ... class Child_1(Base): pass class Child_2(Base): pass </code></pre> <p>If I create various ob...
10
2008-12-08T11:11:45Z
2,936,296
<p>Well... My problem was. In a view, I had this principal model, lets say "Big_Model" and there were some "Small_Model" related to "Big_Model". So when I wanted to retrieve all "Small_Model" related to a certain instance of "Big_Model" I did that **_set.all() stuff. But the point is that Small_Model has Child Classes ...
0
2010-05-29T19:19:48Z
[ "python", "django", "inheritance", "django-models" ]
How do I find the "concrete class" of a django model baseclass
349,206
<p>I'm trying to find the actual class of a django-model object, when using model-inheritance.</p> <p>Some code to describe the problem:</p> <pre><code>class Base(models.model): def basemethod(self): ... class Child_1(Base): pass class Child_2(Base): pass </code></pre> <p>If I create various ob...
10
2008-12-08T11:11:45Z
8,478,666
<p>Have a look at InheritanceManager in <a href="https://github.com/carljm/django-model-utils/">django-model-utils</a> – attaching it to a model gives you the concrete child classes (at least at the first level):</p> <pre><code>from model_utils.managers import InheritanceManager class Base(models.Model): object...
5
2011-12-12T17:59:21Z
[ "python", "django", "inheritance", "django-models" ]
How do I find the "concrete class" of a django model baseclass
349,206
<p>I'm trying to find the actual class of a django-model object, when using model-inheritance.</p> <p>Some code to describe the problem:</p> <pre><code>class Base(models.model): def basemethod(self): ... class Child_1(Base): pass class Child_2(Base): pass </code></pre> <p>If I create various ob...
10
2008-12-08T11:11:45Z
17,443,716
<p>Slightly modified version of <a href="http://stackoverflow.com/a/349494/1042635">what Daniel Naab proposed</a>:</p> <pre><code>from django.contrib.contenttypes.models import ContentType from django.db import models def ParentClass(models.Model): superclass = models.CharField(max_length = 255, blank = True) ...
0
2013-07-03T08:56:44Z
[ "python", "django", "inheritance", "django-models" ]
How do I pass a python list in the post query?
349,369
<p>I want to send some strings in a list in a POST call. eg:</p> <pre><code> www.example.com/?post_data = A list of strings </code></pre> <p>The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?</p>
7
2008-12-08T12:28:28Z
349,384
<p>There's no such thing as a "list of strings" in a URL (or in practically anything in HTTP - if you specify multiple values for the same header, they come out as a single delimited value in most web app frameworks IME). It's just a single string. I suggest you delimit the strings in some way (e.g. comma-separated) an...
8
2008-12-08T12:35:54Z
[ "python", "web-services" ]
How do I pass a python list in the post query?
349,369
<p>I want to send some strings in a list in a POST call. eg:</p> <pre><code> www.example.com/?post_data = A list of strings </code></pre> <p>The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?</p>
7
2008-12-08T12:28:28Z
349,386
<p>If the big string you're receiving is merely delimited then you could try splitting it. See <a href="http://www.java2s.com/Code/Python/String/Splittingstrings.htm" rel="nofollow">Splitting strings</a>.</p> <p>To clarify, you get the delimited list of the strings, split that list into a python list, and voila!, you ...
2
2008-12-08T12:36:52Z
[ "python", "web-services" ]
How do I pass a python list in the post query?
349,369
<p>I want to send some strings in a list in a POST call. eg:</p> <pre><code> www.example.com/?post_data = A list of strings </code></pre> <p>The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?</p>
7
2008-12-08T12:28:28Z
349,389
<p>Are you talking about this?</p> <pre><code>post_data= ",".join( list_of_strings ) </code></pre>
1
2008-12-08T12:38:23Z
[ "python", "web-services" ]
How do I pass a python list in the post query?
349,369
<p>I want to send some strings in a list in a POST call. eg:</p> <pre><code> www.example.com/?post_data = A list of strings </code></pre> <p>The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?</p>
7
2008-12-08T12:28:28Z
349,515
<p>TRY JSON(JavaScript Object Notation) it's available in the python package. Find out here: <a href="http://docs.python.org/library/json.html" rel="nofollow">http://docs.python.org/library/json.html</a></p> <p>You can Encode your list to an array represented in JSON and append to the post argument. Later decode it ba...
4
2008-12-08T13:13:35Z
[ "python", "web-services" ]
How do I pass a python list in the post query?
349,369
<p>I want to send some strings in a list in a POST call. eg:</p> <pre><code> www.example.com/?post_data = A list of strings </code></pre> <p>The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?</p>
7
2008-12-08T12:28:28Z
349,522
<p>It depends on your server to format the incoming arguments. for example, when zope gets a request like this: <a href="http://www.zope.org?ids:list=1&amp;ids:list=2" rel="nofollow">http://www.zope.org?ids:list=1&amp;ids:list=2</a></p> <p>you can get the the ids as a list. But this feature depends on the server. If y...
2
2008-12-08T13:15:32Z
[ "python", "web-services" ]
How do I pass a python list in the post query?
349,369
<p>I want to send some strings in a list in a POST call. eg:</p> <pre><code> www.example.com/?post_data = A list of strings </code></pre> <p>The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?</p>
7
2008-12-08T12:28:28Z
349,776
<p>Data passed to a POST statement is (as far as I understood) encoded as key-value pairs, using the application/x-www-form-urlencoded encoding.</p> <p>So, I'll assume that you represent your list of string as the following dictionnary :</p> <pre><code>&gt;&gt;&gt; my_string_list= { 's1': 'I', ...
1
2008-12-08T14:55:56Z
[ "python", "web-services" ]
How do I pass a python list in the post query?
349,369
<p>I want to send some strings in a list in a POST call. eg:</p> <pre><code> www.example.com/?post_data = A list of strings </code></pre> <p>The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?</p>
7
2008-12-08T12:28:28Z
351,047
<p>A data structure like <a href="http://code.djangoproject.com/browser/django/trunk/django/utils/datastructures.py" rel="nofollow"><code>django.utils.datastructures.MultiValueDict</code></a> is a clean way to represent such data. AFAIK it preserves order.</p> <pre><code>&gt;&gt;&gt; d = MultiValueDict({'name': ['Adri...
0
2008-12-08T21:47:47Z
[ "python", "web-services" ]
How do I pass a python list in the post query?
349,369
<p>I want to send some strings in a list in a POST call. eg:</p> <pre><code> www.example.com/?post_data = A list of strings </code></pre> <p>The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?</p>
7
2008-12-08T12:28:28Z
2,071,413
<p>If you can't or don't want to simply separate them with a comma and you want to send them in a more list-ish way. I have a list of numbers that I want to pass and I use a PHP webservice on the other end, I don't want to rebuild my webservice since I'v used a common multiselect element that Zend Framework provided.</...
2
2010-01-15T12:33:24Z
[ "python", "web-services" ]
Run a shortcut under windows
349,653
<p>The following doesn't work, because it doesn't wait until the process is finished:</p> <pre><code>import subprocess p = subprocess.Popen('start /WAIT /B MOZILL~1.LNK', shell=True) p.wait() </code></pre> <p>Any idea how to run a shortcut and wait that the subprocess returns ?</p> <p><strong>Edit:</strong> original...
1
2008-12-08T14:09:25Z
349,697
<p>You will need to invoke a shell to get the subprocess option to work:</p> <pre><code>p = subprocess.Popen('start /B MOZILL~1.LNK', shell=True) p.wait() </code></pre> <p>This however will still exit immediately (see @R. Bemrose).</p> <p>If <code>p.pid</code> contains the correct pid (I'm not sure on windows), then...
4
2008-12-08T14:21:50Z
[ "python", "windows" ]
Run a shortcut under windows
349,653
<p>The following doesn't work, because it doesn't wait until the process is finished:</p> <pre><code>import subprocess p = subprocess.Popen('start /WAIT /B MOZILL~1.LNK', shell=True) p.wait() </code></pre> <p>Any idea how to run a shortcut and wait that the subprocess returns ?</p> <p><strong>Edit:</strong> original...
1
2008-12-08T14:09:25Z
349,793
<p>Note: I am simply adding on Jim's reply, with a small trick. What about using 'WAIT' option for start?</p> <pre><code>p = subprocess.Popen('start /B MOZILL~1.LNK /WAIT', shell=True) p.wait() </code></pre> <p>This should work.</p>
0
2008-12-08T15:01:43Z
[ "python", "windows" ]
Run a shortcut under windows
349,653
<p>The following doesn't work, because it doesn't wait until the process is finished:</p> <pre><code>import subprocess p = subprocess.Popen('start /WAIT /B MOZILL~1.LNK', shell=True) p.wait() </code></pre> <p>Any idea how to run a shortcut and wait that the subprocess returns ?</p> <p><strong>Edit:</strong> original...
1
2008-12-08T14:09:25Z
349,851
<p>cmd.exe is terminating as soon as start launches the program. This behavior is documented (in start /? ):</p> <blockquote> <p>If Command Extensions are enabled, external command invocation through the command line or the START command changes as follows:</p> </blockquote> <p>...</p> <blockquote> <p>Whe...
3
2008-12-08T15:24:43Z
[ "python", "windows" ]
Getting the lesser n elements of a list in Python
350,519
<p>I need to get the lesser n numbers of a list in Python. I need this to be really fast because it's in a critical part for performance and it needs to be repeated a lot of times.</p> <p>n is usually no greater than 10 and the list usually has around 20000 elements. The list is always different each time I call the f...
8
2008-12-08T19:10:10Z
350,568
<p>You actually want a sorted sequence of mins.</p> <pre><code>mins = items[:n] mins.sort() for i in items[n:]: if i &lt; mins[-1]: mins.append(i) mins.sort() mins= mins[:n] </code></pre> <p>This runs <em>much</em> faster because you aren't even looking at mins unless it's provably got a ...
13
2008-12-08T19:27:39Z
[ "python", "algorithm", "sorting" ]
Getting the lesser n elements of a list in Python
350,519
<p>I need to get the lesser n numbers of a list in Python. I need this to be really fast because it's in a critical part for performance and it needs to be repeated a lot of times.</p> <p>n is usually no greater than 10 and the list usually has around 20000 elements. The list is always different each time I call the f...
8
2008-12-08T19:10:10Z
350,578
<p>A possibility is to use the <a href="http://docs.python.org/library/bisect.html" rel="nofollow">bisect</a> module:</p> <pre><code>import bisect def mins(items, n): mins = [float('inf')]*n for item in items: bisect.insort(mins, item) mins.pop() return mins </code></pre> <p>However, it's...
2
2008-12-08T19:32:09Z
[ "python", "algorithm", "sorting" ]
Getting the lesser n elements of a list in Python
350,519
<p>I need to get the lesser n numbers of a list in Python. I need this to be really fast because it's in a critical part for performance and it needs to be repeated a lot of times.</p> <p>n is usually no greater than 10 and the list usually has around 20000 elements. The list is always different each time I call the f...
8
2008-12-08T19:10:10Z
350,597
<p>If speed is of utmost concern, the fastest method is going to be with c. Psyco has an upfront cost, but may prove to be pretty fast. I would recommend Cython for python -> c compilation (a more up to date for pf Pyrex).</p> <p>Hand coding it in c would be the best, and allow you to use data structures specific to y...
2
2008-12-08T19:37:25Z
[ "python", "algorithm", "sorting" ]
Getting the lesser n elements of a list in Python
350,519
<p>I need to get the lesser n numbers of a list in Python. I need this to be really fast because it's in a critical part for performance and it needs to be repeated a lot of times.</p> <p>n is usually no greater than 10 and the list usually has around 20000 elements. The list is always different each time I call the f...
8
2008-12-08T19:10:10Z
350,683
<p>I like erickson's heap idea. I don't know Python either, but there appears to be a canned solution here: <a href="http://docs.python.org/library/heapq.html" rel="nofollow">heapq — Heap queue algorithm</a></p>
3
2008-12-08T20:00:39Z
[ "python", "algorithm", "sorting" ]
Getting the lesser n elements of a list in Python
350,519
<p>I need to get the lesser n numbers of a list in Python. I need this to be really fast because it's in a critical part for performance and it needs to be repeated a lot of times.</p> <p>n is usually no greater than 10 and the list usually has around 20000 elements. The list is always different each time I call the f...
8
2008-12-08T19:10:10Z
350,685
<pre><code>import heapq nlesser_items = heapq.nsmallest(n, items) </code></pre> <p>Here's a correct version of <a href="http://stackoverflow.com/questions/350519/getting-the-lesser-n-elements-of-a-list-in-python#350568">S.Lott's algorithm</a>:</p> <pre><code>from bisect import insort from itertools import islice ...
10
2008-12-08T20:01:12Z
[ "python", "algorithm", "sorting" ]
Getting the lesser n elements of a list in Python
350,519
<p>I need to get the lesser n numbers of a list in Python. I need this to be really fast because it's in a critical part for performance and it needs to be repeated a lot of times.</p> <p>n is usually no greater than 10 and the list usually has around 20000 elements. The list is always different each time I call the f...
8
2008-12-08T19:10:10Z
22,357,180
<p>why not just call the select_n_th element in O(N) time and then divide the array into two parts by the n_th element, this should be the fastest one.</p> <p>ps: This O(N) algorithm works if you don't specify the order of the n-smallest elements The link below seems to do the selection algorithm. <a href="http://code...
0
2014-03-12T16:02:43Z
[ "python", "algorithm", "sorting" ]
Why is my Python C Extension leaking memory?
350,647
<p>The function below takes a python file handle, reads in packed binary data from the file, creates a Python dictionary and returns it. If I loop it endlessly, it'll continually consume RAM. What's wrong with my RefCounting?</p> <pre><code>static PyObject* __binParse_getDBHeader(PyObject *self, PyObject *args){ Py...
5
2008-12-08T19:51:04Z
350,695
<p><code>PyDict_New()</code> returns a new reference, check the <a href="http://docs.python.org/c-api/dict.html">docs</a> for <code>PyDict</code>. So if you increase the refcount immediately after creating it, you have two references to it. One is transferred to the caller when you return it as a result value, but the ...
16
2008-12-08T20:04:32Z
[ "python", "c", "refcounting" ]
Why is my Python C Extension leaking memory?
350,647
<p>The function below takes a python file handle, reads in packed binary data from the file, creates a Python dictionary and returns it. If I loop it endlessly, it'll continually consume RAM. What's wrong with my RefCounting?</p> <pre><code>static PyObject* __binParse_getDBHeader(PyObject *self, PyObject *args){ Py...
5
2008-12-08T19:51:04Z
350,719
<p>OT: Using successive calls to <code>PyList_Append</code> is a performance issue. Since you know how many results you'll get in advance, you can use:</p> <pre><code>PyObject *pyTimeList = PyList_New(NUM_DRAWERS); int i; for (i=0; i&lt;NUM_DRAWERS; i++){ o = PyInt_FromLong(pdbHeader-&gt;last_good_test[i]); Py...
5
2008-12-08T20:12:44Z
[ "python", "c", "refcounting" ]
Why is my Python C Extension leaking memory?
350,647
<p>The function below takes a python file handle, reads in packed binary data from the file, creates a Python dictionary and returns it. If I loop it endlessly, it'll continually consume RAM. What's wrong with my RefCounting?</p> <pre><code>static PyObject* __binParse_getDBHeader(PyObject *self, PyObject *args){ Py...
5
2008-12-08T19:51:04Z
350,743
<p>I don't know about Python-C. However, My experience with COM reference counting says that a newly created reference-counted object has a reference count of <strong>1</strong>. So your Py_INCREF(pyDB) after PyArg_ParseTuple(args, "O", &amp;pyDB) and PyObject *pyDBHeader = PyDict_New(); are the culprit. Their referenc...
2
2008-12-08T20:18:09Z
[ "python", "c", "refcounting" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
350,817
<p>It has to do with the meta class that is used in defining the form class. I think it keeps an internal list of the fields and if you insert into the middle of the list it might work. It has been a while since I looked at that code.</p>
0
2008-12-08T20:41:55Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
350,851
<p>Form fields have an attribute for creation order, called <code>creation_counter</code>. <code>.fields</code> attribute is a dictionary, so simple adding to dictionary and changing <code>creation_counter</code> attributes in all fields to reflect new ordering should suffice (never tried this, though).</p>
5
2008-12-08T20:52:25Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
350,913
<p>I went ahead and answered my own question. Here's the answer for future reference:</p> <p>In Django <code>form.py</code> does some dark magic using the <code>__new__</code> method to load your class variables ultimately into <code>self.fields</code> in the order defined in the class. <code>self.fields</code> is a...
38
2008-12-08T21:12:56Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
871,048
<p>Use a counter in the Field class. Sort by that counter:</p> <pre><code>import operator import itertools class Field(object): _counter = itertools.count() def __init__(self): self.count = Field._counter.next() self.name = '' def __repr__(self): return "Field(%r)" % self.name cla...
4
2009-05-15T22:03:32Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
1,191,310
<p><strong>[NOTE: this answer is now somewhat outdated - please see the discussion below it].</strong></p> <p>If f is a form, its fields are f.fields, which is a <code>django.utils.datastructures.SortedDict</code> (it presents the items in the order they are added). After form construction f.fields has a keyOrder att...
86
2009-07-28T00:12:20Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
2,551,889
<p>For future reference: things have changed a bit since newforms. This is one way of reordering fields from base formclasses you have no control over:</p> <pre><code>def move_field_before(form, field, before_field): content = form.base_fields[field] del(form.base_fields[field]) insert_at = list(form.base_...
3
2010-03-31T09:53:37Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
5,747,259
<p>Fields are listed in the order they are defined in ModelClass._meta.fields. But if you want to change order in Form, you can do by using keyOrder function. For example : </p> <pre><code>class ContestForm(ModelForm): class Meta: model = Contest exclude=('create_date', 'company') def __init__(self, *args...
11
2011-04-21T16:53:43Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
25,449,079
<p>Using <code>fields</code> in inner <code>Meta</code> class is what worked for me on <code>Django==1.6.5</code>:</p> <pre><code>#!/usr/bin/env python # -*- coding: utf-8 -*- """ Example form declaration with custom field order. """ from django import forms from app.models import AppModel class ExampleModelForm(...
0
2014-08-22T14:14:27Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
27,315,534
<p>I've used this to move fields about:</p> <pre><code>def move_field_before(frm, field_name, before_name): fld = frm.fields.pop(field_name) pos = frm.fields.keys().index(before_name) frm.fields.insert(pos, field_name, fld) </code></pre> <p>This works in 1.5 and I'm reasonably sure it still works in more ...
0
2014-12-05T12:05:19Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
27,322,256
<p>If either <code>fields = '__all__'</code>:</p> <pre><code>class AuthorForm(ModelForm): class Meta: model = Author fields = '__all__' </code></pre> <p>or <code>exclude</code> are used:</p> <pre><code>class PartialAuthorForm(ModelForm): class Meta: model = Author exclude = ['...
2
2014-12-05T18:19:05Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
27,595,191
<p>With Django >= 1.7 your must modify <code>ContactForm.base_fields</code> as below:</p> <pre><code>from collections import OrderedDict ... class ContactForm(forms.Form): ... ContactForm.base_fields = OrderedDict( (k, ContactForm.base_fields[k]) for k in ['your', 'field', 'in', 'order'] ) </code></pre...
4
2014-12-22T00:13:52Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
28,843,066
<p>As of Django 1.7 forms use OrderedDict which does not support the append operator. So you have to rebuild the dictionary from scratch...</p> <pre><code>class ChecklistForm(forms.ModelForm): class Meta: model = Checklist fields = ['name', 'email', 'website'] def __init__(self, guide, *args, **kwargs):...
4
2015-03-03T22:18:34Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
34,502,078
<p>New to Django 1.9 is <strong><a href="https://docs.djangoproject.com/en/1.9/ref/forms/api/#django.forms.Form.field_order">Form.field_order</a></strong> and <strong><a href="https://docs.djangoproject.com/en/1.9/ref/forms/api/#django.forms.Form.order_fields">Form.order_fields()</a></strong>.</p>
7
2015-12-28T23:05:23Z
[ "python", "django", "class", "django-forms", "contacts" ]
How does Django Know the Order to Render Form Fields?
350,799
<p>If I have a Django form such as:</p> <pre><code>class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() </code></pre> <p>And I call the as_table() method of an instance of this form, Django will render the fields as the same order...
70
2008-12-08T20:37:44Z
39,412,472
<p>The easiest way to order fields in django 1.9 forms is to use <code>field_order</code> in your form <a href="https://docs.djangoproject.com/en/1.9/ref/forms/api/#django.forms.Form.field_order" rel="nofollow">Form.field_order</a></p> <p>Here is a small example</p> <pre><code>class ContactForm(forms.Form): subj...
0
2016-09-09T13:17:11Z
[ "python", "django", "class", "django-forms", "contacts" ]
What does the function set use to check if two objects are different?
351,271
<p>Simple code:</p> <pre><code>&gt;&gt;&gt; set([2,2,1,2,2,2,3,3,5,1]) set([1, 2, 3, 5]) </code></pre> <p>Ok, in the resulting sets there are no duplicates. What if the object in the list are not int but are some defined by me? What method does it check to understand if they are different? I implemented __eq__ and __...
6
2008-12-08T23:02:23Z
351,287
<p>According to the <a href="http://docs.python.org/library/stdtypes.html#set-types-set-frozenset" rel="nofollow">set documentation</a>, the elements must be <a href="http://docs.python.org/glossary.html#term-hashable" rel="nofollow">hashable</a>. </p> <p>An object is hashable if it has a hash value which never chang...
13
2008-12-08T23:08:24Z
[ "python", "methods", "set" ]
Determine record in multi record html form
351,440
<p>In a html form, I'm displaying multiple records from a table, ready for update.</p> <p>Right now I use: <code>name=&lt;column-name&gt;_&lt;pk-id&gt; value=&lt;value&gt;</code> for the fields. Then in my python-script I go for:</p> <pre><code>for key in form.keys(): if key.startswith('&lt;name-A&gt;_'): ...
1
2008-12-09T00:33:01Z
351,457
<p>In java apps, it's common to JSONify the name.</p> <pre><code>&lt;input name="records[pk].fieldName"/&gt; </code></pre> <p><code>pk</code> being the primary key of the row and <code>fieldName</code> the field. Of course most frameworks handle this transparently. Each record ends up as a instance of a class with a ...
1
2008-12-09T00:48:36Z
[ "python", "html", "cgi" ]
How do I get data from stdin using os.system()
351,456
<p>The only reliable method that I a have found for using a script to download text from wikipedia is with cURL. So far the only way I have for doing that is to call <code>os.system()</code>. Even though the output appears properly in the python shell I can't seem to the function it to return anything other than the ex...
0
2008-12-09T00:48:28Z
351,469
<p>Answering the question, Python has a subprocess module which allows you to interact with spawned processes.<a href="http://docs.python.org/library/subprocess.html#subprocess.Popen" rel="nofollow">http://docs.python.org/library/subprocess.html#subprocess.Popen</a></p> <p>It allows you to read the stdout for the invo...
2
2008-12-09T00:55:36Z
[ "python", "shell", "curl", "urllib", "os.system" ]
How do I get data from stdin using os.system()
351,456
<p>The only reliable method that I a have found for using a script to download text from wikipedia is with cURL. So far the only way I have for doing that is to call <code>os.system()</code>. Even though the output appears properly in the python shell I can't seem to the function it to return anything other than the ex...
0
2008-12-09T00:48:28Z
351,472
<p>As an alternetive to urllib, you could use the libCurl <a href="http://curl.haxx.se/libcurl/python/" rel="nofollow">Python bindings</a>.</p>
0
2008-12-09T01:00:21Z
[ "python", "shell", "curl", "urllib", "os.system" ]
How do I get data from stdin using os.system()
351,456
<p>The only reliable method that I a have found for using a script to download text from wikipedia is with cURL. So far the only way I have for doing that is to call <code>os.system()</code>. Even though the output appears properly in the python shell I can't seem to the function it to return anything other than the ex...
0
2008-12-09T00:48:28Z
351,475
<p>From <a href="http://diveintopython.net/html_processing/extracting_data.html" rel="nofollow">Dive into Python:</a> </p> <pre><code>import urllib sock = urllib.urlopen("http://en.wikipedia.org/wiki/Python_(programming_language)") htmlsource = sock.read() sock.close() print htmlsource </code></pre> <p>That will pri...
7
2008-12-09T01:01:28Z
[ "python", "shell", "curl", "urllib", "os.system" ]
How do I mock an IMAP server in Python, despite extreme laziness?
351,656
<p>I'm curious to know if there is an easy way to mock an IMAP server (a la the <code>imaplib</code> module) in Python, <em>without</em> doing a lot of work.</p> <p>Is there a pre-existing solution? Ideally I could connect to the existing IMAP server, do a dump, and have the mock server run off the real mailbox/email ...
9
2008-12-09T02:57:49Z
351,675
<p>I found it quite easy to write an IMAP server in twisted last time I tried. It comes with support for writing IMAP servers and you have a huge amount of flexibility.</p>
8
2008-12-09T03:14:39Z
[ "python", "testing", "imap", "mocking" ]
How do I mock an IMAP server in Python, despite extreme laziness?
351,656
<p>I'm curious to know if there is an easy way to mock an IMAP server (a la the <code>imaplib</code> module) in Python, <em>without</em> doing a lot of work.</p> <p>Is there a pre-existing solution? Ideally I could connect to the existing IMAP server, do a dump, and have the mock server run off the real mailbox/email ...
9
2008-12-09T02:57:49Z
352,194
<p>I never tried but, if I had to, I would start with the existing SMTP server.</p>
1
2008-12-09T09:12:17Z
[ "python", "testing", "imap", "mocking" ]
How do I mock an IMAP server in Python, despite extreme laziness?
351,656
<p>I'm curious to know if there is an easy way to mock an IMAP server (a la the <code>imaplib</code> module) in Python, <em>without</em> doing a lot of work.</p> <p>Is there a pre-existing solution? Ideally I could connect to the existing IMAP server, do a dump, and have the mock server run off the real mailbox/email ...
9
2008-12-09T02:57:49Z
353,175
<p>How much of it do you really need for any one test? If you start to build something on the order of complexity of a real server so that you can use it on all your tests, you've already gone wrong. Just mock the bits any one tests needs. </p> <p>Don't bother trying so hard to share a mock implementation. They're ...
6
2008-12-09T15:41:57Z
[ "python", "testing", "imap", "mocking" ]
How do I mock an IMAP server in Python, despite extreme laziness?
351,656
<p>I'm curious to know if there is an easy way to mock an IMAP server (a la the <code>imaplib</code> module) in Python, <em>without</em> doing a lot of work.</p> <p>Is there a pre-existing solution? Ideally I could connect to the existing IMAP server, do a dump, and have the mock server run off the real mailbox/email ...
9
2008-12-09T02:57:49Z
35,488,764
<p>As I didn't find something convenient in python 3 for my needs (mail part of twisted is not running in python 3), I did a small mock with asyncio that you can improve if you'd like :</p> <p>I defined an ImapProtocol which extends asyncio.Protocol. Then launch a server like this :</p> <pre><code>factory = loop.crea...
0
2016-02-18T17:48:13Z
[ "python", "testing", "imap", "mocking" ]
Python method arguments with spaces
351,760
<p>I would like to create a simple file format/DSL which would allow my users to input data. My system is in python and using python's parser is appealing. Syntax like this for defining a data element seems quite convenient.</p> <pre><code>Allocation(Param1 = Val1, Param2 = Val2 ) </code></pre> <p>However, it does ...
1
2008-12-09T04:14:45Z
351,776
<p>You can do this:</p> <pre><code>def Allocation(**kwargs): print kwargs myargs = {"Param 1":Val1, "Param 2":Val1} Allocation(**myargs) </code></pre> <p><strong>Edit:</strong> Your edit now includes my answer so no, there is no easier way to have spaces in keyword arguments.</p>
0
2008-12-09T04:22:03Z
[ "python", "dsl" ]
Python method arguments with spaces
351,760
<p>I would like to create a simple file format/DSL which would allow my users to input data. My system is in python and using python's parser is appealing. Syntax like this for defining a data element seems quite convenient.</p> <pre><code>Allocation(Param1 = Val1, Param2 = Val2 ) </code></pre> <p>However, it does ...
1
2008-12-09T04:14:45Z
351,795
<p>Unless I am mistaking your basic premise here, there's nothing to stop you from writing a class that parses your own custom syntax, and then using that custom syntax as a single-argument string:</p> <pre><code>Allocation("Param 1=Check Up; Param 2=Mean Value Theorem;") </code></pre> <p>In this example, semicolons ...
1
2008-12-09T04:40:19Z
[ "python", "dsl" ]
Python method arguments with spaces
351,760
<p>I would like to create a simple file format/DSL which would allow my users to input data. My system is in python and using python's parser is appealing. Syntax like this for defining a data element seems quite convenient.</p> <pre><code>Allocation(Param1 = Val1, Param2 = Val2 ) </code></pre> <p>However, it does ...
1
2008-12-09T04:14:45Z
351,802
<p>I'd imagine that there would be some way to do it. But I feel compelled to ask, is there really a big enough difference in readability from this</p> <pre><code>Allocation(Param1 = Val1, Param2 = Val2 ) </code></pre> <p>To this:</p> <pre><code>Allocation(Param 1 = Val1, Param 2 = Val2 ) </code></pre> <p>to make ...
3
2008-12-09T04:42:47Z
[ "python", "dsl" ]
Python method arguments with spaces
351,760
<p>I would like to create a simple file format/DSL which would allow my users to input data. My system is in python and using python's parser is appealing. Syntax like this for defining a data element seems quite convenient.</p> <pre><code>Allocation(Param1 = Val1, Param2 = Val2 ) </code></pre> <p>However, it does ...
1
2008-12-09T04:14:45Z
353,389
<p>Here's my preference.</p> <pre><code>AllocationSet( Alloc( name="some name", value=1.23 ), Alloc( name="another name", value=2.34 ), Alloc( name="yet another name", value=4.56 ), ) </code></pre> <p>These are relatively easy class declarations to create. The resulting structure is pleasant to process, ...
1
2008-12-09T16:36:00Z
[ "python", "dsl" ]
destroying a Toplevel tk window in python
351,821
<p>I was trying to write code that would auto-close a Toplevel Tk window in Python.</p> <p>I ended up getting it to work, but ran into a little problem along the way that I wasn't able to figure out.</p> <p>The second two buttons work, but the first one doesn't and I don't understand why...</p> <p>Any ideas?</p> <p...
1
2008-12-09T04:53:58Z
351,832
<p>Because it returns a function and not its result.</p> <p>You should put:</p> <pre><code>command=TL.destroy </code></pre> <p>or if you used lambda:</p> <pre><code>command=lambda: TL.destroy() </code></pre>
8
2008-12-09T04:59:40Z
[ "python", "tkinter" ]
Python imaplib Gmail authenticate failure
351,927
<p>I just ran into an issue with Python's imaplib and Gmail's authentication mechanism:</p> <pre><code>&gt;&gt;&gt; import imaplib &gt;&gt;&gt; imap = imaplib.IMAP4_SSL('imap.gmail.com', 993) &gt;&gt;&gt; imap.authenticate('[email protected]', 'Bob Dole likes your style!') Traceback (most recent call last): ... imap...
7
2008-12-09T06:07:50Z
351,930
<p>I found the solution on <a href="http://codeclimber.blogspot.com/2008/06/using-ruby-for-imap-with-gmail.html" rel="nofollow">this helpful blog post</a>. Although Gmail doesn't support AUTHENTICATE, it does support the LOGIN capability, like so:</p> <pre><code>&gt;&gt;&gt; imap.login('[email protected]', 'Bob Dole l...
0
2008-12-09T06:10:14Z
[ "python", "gmail", "imap" ]
Python imaplib Gmail authenticate failure
351,927
<p>I just ran into an issue with Python's imaplib and Gmail's authentication mechanism:</p> <pre><code>&gt;&gt;&gt; import imaplib &gt;&gt;&gt; imap = imaplib.IMAP4_SSL('imap.gmail.com', 993) &gt;&gt;&gt; imap.authenticate('[email protected]', 'Bob Dole likes your style!') Traceback (most recent call last): ... imap...
7
2008-12-09T06:07:50Z
351,933
<p>The following works for me:</p> <pre><code>srv = imaplib.IMAP4_SSL("imap.gmail.com") srv.login(account, password) </code></pre> <p>I think using <code>login()</code> is required.</p>
2
2008-12-09T06:10:34Z
[ "python", "gmail", "imap" ]
Python imaplib Gmail authenticate failure
351,927
<p>I just ran into an issue with Python's imaplib and Gmail's authentication mechanism:</p> <pre><code>&gt;&gt;&gt; import imaplib &gt;&gt;&gt; imap = imaplib.IMAP4_SSL('imap.gmail.com', 993) &gt;&gt;&gt; imap.authenticate('[email protected]', 'Bob Dole likes your style!') Traceback (most recent call last): ... imap...
7
2008-12-09T06:07:50Z
351,934
<p>Instead of</p> <pre><code>&gt;&gt;&gt; imap.authenticate('[email protected]', 'Bob Dole likes your style!') </code></pre> <p>use</p> <pre><code>&gt;&gt;&gt; imap.login('[email protected]', 'Bob Dole likes your style!') </code></pre>
8
2008-12-09T06:10:52Z
[ "python", "gmail", "imap" ]
Most pythonic form for mapping a series of statements?
352,076
<p>This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here):</p> <pre><code>result = [ f(x) for x in list ...
0
2008-12-09T08:06:41Z
352,105
<p>There are cases where it's best to go back to the for-loop, yes, but more often I prefer one of these approaches:</p> <p>Use appropriate line breaks and indentation to keep it readable:</p> <pre><code>result = [blah(blah(blah(x))) for x in list] </code></pre> <p>Or extract (enough of) the logic into ano...
0
2008-12-09T08:26:04Z
[ "python", "coding-style", "idioms" ]
Most pythonic form for mapping a series of statements?
352,076
<p>This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here):</p> <pre><code>result = [ f(x) for x in list ...
0
2008-12-09T08:06:41Z
352,121
<p>If your only concerned with the last result, your last answer is the best. It's clear for anyone looking at it what your doing. </p> <p>I often take any code that starts to get complex and move it to a function. This basically serves as a comment for that block of code. (any complex code probably needs a re-writ...
1
2008-12-09T08:34:29Z
[ "python", "coding-style", "idioms" ]
Most pythonic form for mapping a series of statements?
352,076
<p>This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here):</p> <pre><code>result = [ f(x) for x in list ...
0
2008-12-09T08:06:41Z
352,219
<p>Follow the style that most matches your tastes.<br> I would not worry about performance; only in case you really see some issue you can try to move to a different style.</p> <p>Here some other possible suggestions, in addition to your proposals:</p> <pre><code>result = [f( g( h(x) ...
3
2008-12-09T09:25:04Z
[ "python", "coding-style", "idioms" ]
Most pythonic form for mapping a series of statements?
352,076
<p>This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here):</p> <pre><code>result = [ f(x) for x in list ...
0
2008-12-09T08:06:41Z
353,060
<p>You can easily do function composition in Python. </p> <p>Here's a demonstrates of a way to create a new function which is a composition of existing functions.</p> <pre><code>&gt;&gt;&gt; def comp( a, b ): def compose( args ): return a( b( args ) ) return compose &gt;&gt;&gt; def times2(x): return x...
5
2008-12-09T15:11:15Z
[ "python", "coding-style", "idioms" ]
Most pythonic form for mapping a series of statements?
352,076
<p>This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here):</p> <pre><code>result = [ f(x) for x in list ...
0
2008-12-09T08:06:41Z
353,147
<p>If this is something you're doing often and with several different statements you could write something like</p> <pre><code>def seriesoffncs(fncs,x): for f in fncs[::-1]: x=f(x) return x </code></pre> <p>where fncs is a list of functions. so seriesoffncs((f,g,h),x) would return f(g(h(x))). This w...
2
2008-12-09T15:35:13Z
[ "python", "coding-style", "idioms" ]
Most pythonic form for mapping a series of statements?
352,076
<p>This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here):</p> <pre><code>result = [ f(x) for x in list ...
0
2008-12-09T08:06:41Z
353,729
<p>A variation of <a href="http://stackoverflow.com/questions/352076/most-pythonic-form-for-mapping-a-series-of-statements#353147">dagw.myopenid.com</a>'s function:</p> <pre><code>def chained_apply(*args): val = args[-1] for f in fncs[:-1:-1]: val=f(val) return val </code></pre> <p>Instead of seri...
1
2008-12-09T18:20:06Z
[ "python", "coding-style", "idioms" ]
Most pythonic form for mapping a series of statements?
352,076
<p>This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here):</p> <pre><code>result = [ f(x) for x in list ...
0
2008-12-09T08:06:41Z
406,944
<p>As far as I know there's no built-in/native syntax for composition in Python, but you can write your own function to compose stuff without too much trouble.</p> <pre><code>def compose(*f): return f[0] if len(f) == 1 else lambda *args: f[0](compose(*f[1:])(*args)) def f(x): return 'o ' + str(x) def g(x): ...
1
2009-01-02T14:41:29Z
[ "python", "coding-style", "idioms" ]
How to adding middleware to Appengine's webapp framework?
352,079
<p>Im using the appengine webapp framework (<a href="http://code.google.com/appengine/docs/webapp/" rel="nofollow">link</a>). Is it possible to add Django middleware? I cant find any examples. Im currently trying to get the firepython middleware to work (<a href="http://github.com/woid/firepython/tree/master" rel="no...
3
2008-12-09T08:09:07Z
436,831
<p>Could you provide any details on your failure?</p> <p>AFAIK Google App Engine lets you use django middleware as long as it does not use django models/ORM.</p> <p>BTW. Thanks for pointing out Firepython, looks sweet :)</p>
1
2009-01-12T20:12:04Z
[ "python", "django", "google-app-engine", "middleware", "django-middleware" ]
How to adding middleware to Appengine's webapp framework?
352,079
<p>Im using the appengine webapp framework (<a href="http://code.google.com/appengine/docs/webapp/" rel="nofollow">link</a>). Is it possible to add Django middleware? I cant find any examples. Im currently trying to get the firepython middleware to work (<a href="http://github.com/woid/firepython/tree/master" rel="no...
3
2008-12-09T08:09:07Z
442,831
<p>It's easy: You create the WSGI application as per normal, then wrap that application in your WSGI middleware before executing it.</p> <p>See <a href="http://github.com/Arachnid/bloog/blob/bb777426376d298765d5dee5b88f53964cc6b5f3/main.py#L71">this code</a> from Bloog to see how firepython is added as middleware.</p>...
5
2009-01-14T12:49:37Z
[ "python", "django", "google-app-engine", "middleware", "django-middleware" ]
How to adding middleware to Appengine's webapp framework?
352,079
<p>Im using the appengine webapp framework (<a href="http://code.google.com/appengine/docs/webapp/" rel="nofollow">link</a>). Is it possible to add Django middleware? I cant find any examples. Im currently trying to get the firepython middleware to work (<a href="http://github.com/woid/firepython/tree/master" rel="no...
3
2008-12-09T08:09:07Z
455,888
<p>The GAE webapp framework does not map one to one to the Django framework. It would be hard to do what you want without implementing some kind of adapter yourself, I do not know of any third party handler adapters to do this.</p> <p>That said, I generally use the app-engine-patch so I can use the latest 1.0.2 Djan...
0
2009-01-18T21:04:44Z
[ "python", "django", "google-app-engine", "middleware", "django-middleware" ]
How to adding middleware to Appengine's webapp framework?
352,079
<p>Im using the appengine webapp framework (<a href="http://code.google.com/appengine/docs/webapp/" rel="nofollow">link</a>). Is it possible to add Django middleware? I cant find any examples. Im currently trying to get the firepython middleware to work (<a href="http://github.com/woid/firepython/tree/master" rel="no...
3
2008-12-09T08:09:07Z
548,865
<p>"Middleware" as understood by Django is a kind of request/response processor, quite different from what WSGI calls "middleware". Think: django-like middleware will add <code>session</code> attribute to request object basing on what Beaker (WSGI middleware) has put in <code>environ['beaker.session']</code>. While add...
0
2009-02-14T10:04:13Z
[ "python", "django", "google-app-engine", "middleware", "django-middleware" ]
Return file from python module
352,340
<p>Edit: How to return/serve a file from a python controller (back end) over a web server, with the file_name? as suggested by @JV</p>
1
2008-12-09T10:34:14Z
352,385
<p>You can either pass back a reference to the file itself i.e. the full path to the file. Then you can open the file or otherwise manipulate it.</p> <p>Or, the more normal case is to pass back the file handle, and, use the standard read/write operations on the file handle.</p> <p>It is not recommended to pass the ac...
2
2008-12-09T10:56:37Z
[ "python", "file", "mime-types", "download" ]
Return file from python module
352,340
<p>Edit: How to return/serve a file from a python controller (back end) over a web server, with the file_name? as suggested by @JV</p>
1
2008-12-09T10:34:14Z
352,399
<p>For information on MIME types (which are how downloads happen), start here: <a href="https://developer.mozilla.org/en/Properly_Configuring_Server_MIME_Types" rel="nofollow">Properly Configure Server MIME Types</a>.</p> <p>For information on CherryPy, look at the attributes of a <a href="http://www.cherrypy.org/wiki...
0
2008-12-09T11:06:04Z
[ "python", "file", "mime-types", "download" ]
Return file from python module
352,340
<p>Edit: How to return/serve a file from a python controller (back end) over a web server, with the file_name? as suggested by @JV</p>
1
2008-12-09T10:34:14Z
352,497
<p>Fully supported in CherryPy using</p> <pre><code>from cherrypy.lib.static import serve_file </code></pre> <p>As documented in the <a href="http://www.cherrypy.org/wiki/FileDownload" rel="nofollow">CherryPy docs - FileDownload</a>:</p> <pre><code>import glob import os.path import cherrypy from cherrypy.lib.static...
1
2008-12-09T11:48:15Z
[ "python", "file", "mime-types", "download" ]
Capitalize a string
352,478
<p>Does anyone know of a really simple way of capitalizing just the first letter of a string, regardless of the capitalization of the rest of the string?</p> <p>For example:</p> <pre><code>asimpletest -&gt; Asimpletest aSimpleTest -&gt; ASimpleTest </code></pre> <p>I would like to be able to do all string lengths as...
25
2008-12-09T11:42:47Z
352,494
<pre><code>str = str[0].upper() + str[1:] </code></pre> <p>This should work with every string, except for the empty string (<code>""</code>).</p>
7
2008-12-09T11:47:59Z
[ "python", "string" ]