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
Sending mail from Python using SMTP
64,505
<p>I'm using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I'm missing ?</p> <pre><code>from smtplib import SMTP import datetime debuglevel = 0 smtp = SMTP() smtp.set_debuglevel(debuglevel) smtp.connect('YOUR.MAIL.SERVER', 26) smtp.login('USERNAME@DOMAIN...
70
2008-09-15T16:36:35Z
64,890
<p>The script I use is quite similar; I post it here as an example of how to use the email.* modules to generate MIME messages; so this script can be easily modified to attach pictures, etc.</p> <p>I rely on my ISP to add the date time header.</p> <p>My ISP requires me to use a secure smtp connection to send mail, I ...
74
2008-09-15T17:24:40Z
[ "python", "smtp" ]
Sending mail from Python using SMTP
64,505
<p>I'm using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I'm missing ?</p> <pre><code>from smtplib import SMTP import datetime debuglevel = 0 smtp = SMTP() smtp.set_debuglevel(debuglevel) smtp.connect('YOUR.MAIL.SERVER', 26) smtp.login('USERNAME@DOMAIN...
70
2008-09-15T16:36:35Z
275,124
<p>Also if you want to do smtp auth with TLS as opposed to SSL then you just have to change the port (use 587) and do smtp.starttls(). This worked for me:</p> <pre><code>... smtp.connect('YOUR.MAIL.SERVER', 587) smtp.ehlo() smtp.starttls() smtp.ehlo() smtp.login('USERNAME@DOMAIN', 'PASSWORD') ... </code></pre>
18
2008-11-08T20:12:26Z
[ "python", "smtp" ]
Sending mail from Python using SMTP
64,505
<p>I'm using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I'm missing ?</p> <pre><code>from smtplib import SMTP import datetime debuglevel = 0 smtp = SMTP() smtp.set_debuglevel(debuglevel) smtp.connect('YOUR.MAIL.SERVER', 26) smtp.login('USERNAME@DOMAIN...
70
2008-09-15T16:36:35Z
11,228,560
<p>What about this? </p> <pre><code>import smtplib SERVER = "localhost" FROM = "[email protected]" TO = ["[email protected]"] # must be a list SUBJECT = "Hello!" TEXT = "This message was sent with Python's smtplib." # Prepare actual message message = """\ From: %s To: %s Subject: %s %s """ % (FROM, ", ".join(TO...
4
2012-06-27T14:31:09Z
[ "python", "smtp" ]
Sending mail from Python using SMTP
64,505
<p>I'm using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I'm missing ?</p> <pre><code>from smtplib import SMTP import datetime debuglevel = 0 smtp = SMTP() smtp.set_debuglevel(debuglevel) smtp.connect('YOUR.MAIL.SERVER', 26) smtp.login('USERNAME@DOMAIN...
70
2008-09-15T16:36:35Z
17,596,848
<p>The method I commonly use...not much different but a little bit</p> <pre><code>import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText msg = MIMEMultipart() msg['From'] = '[email protected]' msg['To'] = '[email protected]' msg['Subject'] = 'simple email in python' message = 'here is...
38
2013-07-11T14:59:54Z
[ "python", "smtp" ]
Sending mail from Python using SMTP
64,505
<p>I'm using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I'm missing ?</p> <pre><code>from smtplib import SMTP import datetime debuglevel = 0 smtp = SMTP() smtp.set_debuglevel(debuglevel) smtp.connect('YOUR.MAIL.SERVER', 26) smtp.login('USERNAME@DOMAIN...
70
2008-09-15T16:36:35Z
26,191,922
<p>following code is working fine for me:</p> <pre><code>import smtplib to = '[email protected]' gmail_user = '[email protected]' gmail_pwd = 'yourpassword' smtpserver = smtplib.SMTP("smtp.gmail.com",587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo() # extra characters to permit edit smtpserver.logi...
2
2014-10-04T10:31:05Z
[ "python", "smtp" ]
Sending mail from Python using SMTP
64,505
<p>I'm using the following method to send mail from Python using SMTP. Is it the right method to use or are there gotchas I'm missing ?</p> <pre><code>from smtplib import SMTP import datetime debuglevel = 0 smtp = SMTP() smtp.set_debuglevel(debuglevel) smtp.connect('YOUR.MAIL.SERVER', 26) smtp.login('USERNAME@DOMAIN...
70
2008-09-15T16:36:35Z
29,720,511
<p>See all those lenghty answers? Please allow me to self promote by doing it all in a couple of lines.</p> <p>Import and Connect:</p> <pre><code>import yagmail yag = yagmail.SMTP('[email protected]', host = 'YOUR.MAIL.SERVER', port = 26) </code></pre> <p>Then it is just a one-liner:</p> <pre><code>yag.send('[email protected]...
1
2015-04-18T17:27:00Z
[ "python", "smtp" ]
How to setup VIM autoindentation properly for editing Python files - *.py
65,076
<p>I've troubles setting VIM (7.1.xxx) for editing python files. Indenting seems to be broken (optimal 4 spaces). I've followed some tutorials I found via Google. Still no effect :/ Please help.</p>
47
2008-09-15T17:48:22Z
65,122
<p>I use this on my macbook:</p> <pre><code>" configure expanding of tabs for various file types au BufRead,BufNewFile *.py set expandtab au BufRead,BufNewFile *.c set noexpandtab au BufRead,BufNewFile *.h set noexpandtab au BufRead,BufNewFile Makefile* set noexpandtab " ----------------------------------------------...
42
2008-09-15T17:53:33Z
[ "python", "vim", "configuration", "spaces" ]
How to setup VIM autoindentation properly for editing Python files - *.py
65,076
<p>I've troubles setting VIM (7.1.xxx) for editing python files. Indenting seems to be broken (optimal 4 spaces). I've followed some tutorials I found via Google. Still no effect :/ Please help.</p>
47
2008-09-15T17:48:22Z
66,818
<p>Ensure you are editing the correct configuration file for VIM. Especially if you are using windows, where the file could be named _vimrc instead of .vimrc as on other platforms.</p> <p>In vim type</p> <p><code>:help vimrc</code></p> <p>and check your path to the _vimrc/.vimrc file with</p> <p><code>:echo $HOME</...
2
2008-09-15T20:50:36Z
[ "python", "vim", "configuration", "spaces" ]
How to setup VIM autoindentation properly for editing Python files - *.py
65,076
<p>I've troubles setting VIM (7.1.xxx) for editing python files. Indenting seems to be broken (optimal 4 spaces). I've followed some tutorials I found via Google. Still no effect :/ Please help.</p>
47
2008-09-15T17:48:22Z
68,002
<p>for more advanced python editing consider installing the <a href="http://eigenclass.org/hiki/simplefold" rel="nofollow">simplefold</a> vim plugin. it allows you do advanced code folding using regular expressions. i use it to fold my class and method definitions for faster editing.</p>
0
2008-09-15T23:42:29Z
[ "python", "vim", "configuration", "spaces" ]
How to setup VIM autoindentation properly for editing Python files - *.py
65,076
<p>I've troubles setting VIM (7.1.xxx) for editing python files. Indenting seems to be broken (optimal 4 spaces). I've followed some tutorials I found via Google. Still no effect :/ Please help.</p>
47
2008-09-15T17:48:22Z
1,868,984
<p>I use the vimrc in the python repo among other things:</p> <p><a href="http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc" rel="nofollow">http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc</a></p> <p>I also add</p> <pre><code>set softtabstop=4 </code></pre> <p><a href="http://github.com/skyl/vim-...
3
2009-12-08T18:37:41Z
[ "python", "vim", "configuration", "spaces" ]
How to setup VIM autoindentation properly for editing Python files - *.py
65,076
<p>I've troubles setting VIM (7.1.xxx) for editing python files. Indenting seems to be broken (optimal 4 spaces). I've followed some tutorials I found via Google. Still no effect :/ Please help.</p>
47
2008-09-15T17:48:22Z
11,830,841
<p>I use:</p> <pre><code>$ cat ~/.vimrc syntax on set showmatch set ts=4 set sts=4 set sw=4 set autoindent set smartindent set smarttab set expandtab set number </code></pre> <p>But but I'm going to try Daren's entries</p>
6
2012-08-06T15:01:33Z
[ "python", "vim", "configuration", "spaces" ]
Caching compiled regex objects in Python?
65,266
<p>Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles are spent compiling the strings into their representative state machines in memory.</p> <pre><code>a = re.compile("a.*b") b = re.compile("c.*d") ... </code></pre> <p>Question: Is it possible to store these ...
17
2008-09-15T18:10:35Z
65,333
<p>Note that each module initializes itself only once during the life of an app, no matter how many times you import it. So if you compile your expressions at the module's global scope (ie. not in a function) you should be fine.</p>
2
2008-09-15T18:16:24Z
[ "python", "regex", "caching" ]
Caching compiled regex objects in Python?
65,266
<p>Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles are spent compiling the strings into their representative state machines in memory.</p> <pre><code>a = re.compile("a.*b") b = re.compile("c.*d") ... </code></pre> <p>Question: Is it possible to store these ...
17
2008-09-15T18:10:35Z
65,440
<blockquote> <p>Is it possible to store these regular expressions in a cache on disk in a pre-compiled manner to avoid having to execute the regex compilations on each import?</p> </blockquote> <p>Not easily. You'd have to write a custom serializer that hooks into the C <code>sre</code> implementation of the Python ...
12
2008-09-15T18:29:51Z
[ "python", "regex", "caching" ]
Caching compiled regex objects in Python?
65,266
<p>Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles are spent compiling the strings into their representative state machines in memory.</p> <pre><code>a = re.compile("a.*b") b = re.compile("c.*d") ... </code></pre> <p>Question: Is it possible to store these ...
17
2008-09-15T18:10:35Z
65,579
<p>It's possible to place each regex (or group of regexs) into a separate file and then dynamically import the file that you need using the imp module. I doubt that it scales very well but it could be what you need.</p>
-1
2008-09-15T18:45:37Z
[ "python", "regex", "caching" ]
Caching compiled regex objects in Python?
65,266
<p>Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles are spent compiling the strings into their representative state machines in memory.</p> <pre><code>a = re.compile("a.*b") b = re.compile("c.*d") ... </code></pre> <p>Question: Is it possible to store these ...
17
2008-09-15T18:10:35Z
65,844
<p>The <a href="http://docs.python.org/lib/module-shelve.html" rel="nofollow">shelve</a> module appears to work just fine:</p> <pre><code> import re import shelve a_pattern = "a.*b" b_pattern = "c.*d" a = re.compile(a_pattern) b = re.compile(b_pattern) x = shelve.open('re_cache') x[a_pattern] = a x[b_pattern] = b x.c...
0
2008-09-15T19:14:24Z
[ "python", "regex", "caching" ]
Caching compiled regex objects in Python?
65,266
<p>Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles are spent compiling the strings into their representative state machines in memory.</p> <pre><code>a = re.compile("a.*b") b = re.compile("c.*d") ... </code></pre> <p>Question: Is it possible to store these ...
17
2008-09-15T18:10:35Z
66,666
<p>Hum,</p> <p>Doesn't shelve use pickle ?</p> <p>Anyway, I agree with the previous anwsers. Since a module is processed only once, I doubt compiling regexps will be your app bottle neck. And Python re module is wicked fast since it's coded in C :-)</p> <p>But the good news is that Python got a nice community, so I ...
-1
2008-09-15T20:34:12Z
[ "python", "regex", "caching" ]
Caching compiled regex objects in Python?
65,266
<p>Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles are spent compiling the strings into their representative state machines in memory.</p> <pre><code>a = re.compile("a.*b") b = re.compile("c.*d") ... </code></pre> <p>Question: Is it possible to store these ...
17
2008-09-15T18:10:35Z
66,846
<p>Open /usr/lib/python2.5/re.py and look for "def _compile". You'll find re.py's internal cache mechanism. </p>
0
2008-09-15T20:53:14Z
[ "python", "regex", "caching" ]
Caching compiled regex objects in Python?
65,266
<p>Each time a python file is imported that contains a large quantity of static regular expressions, cpu cycles are spent compiling the strings into their representative state machines in memory.</p> <pre><code>a = re.compile("a.*b") b = re.compile("c.*d") ... </code></pre> <p>Question: Is it possible to store these ...
17
2008-09-15T18:10:35Z
396,148
<p>First of all, this is a clear limitation in the python re module. It causes a limit how much and how big regular expressions are reasonable. The limit is bigger with long running processes and smaller with short lived processes like command line applications.</p> <p>Some years ago I did look at it and it is possibl...
1
2008-12-28T13:02:56Z
[ "python", "regex", "caching" ]
How to add method using metaclass
65,400
<p>How do I add an instance method to a class using a metaclass (yes I do need to use a metaclass)? The following kind of works, but the func_name will still be "foo":</p> <pre><code>def bar(self): print "bar" class MetaFoo(type): def __new__(cls, name, bases, dict): dict["foobar"] = bar retur...
5
2008-09-15T18:24:14Z
65,682
<p>I think what you want to do is this:</p> <pre><code>&gt;&gt;&gt; class Foo(): ... def __init__(self, x): ... self.x = x ... &gt;&gt;&gt; def bar(self): ... print 'bar:', self.x ... &gt;&gt;&gt; bar.func_name = 'foobar' &gt;&gt;&gt; Foo.foobar = bar &gt;&gt;&gt; f = Foo(12) &gt;&gt;&gt; f.foobar() bar: 12 ...
2
2008-09-15T18:57:29Z
[ "python", "metaclass" ]
How to add method using metaclass
65,400
<p>How do I add an instance method to a class using a metaclass (yes I do need to use a metaclass)? The following kind of works, but the func_name will still be "foo":</p> <pre><code>def bar(self): print "bar" class MetaFoo(type): def __new__(cls, name, bases, dict): dict["foobar"] = bar retur...
5
2008-09-15T18:24:14Z
65,716
<p>Try dynamically extending the bases that way you can take advantage of the mro and the methods are actual methods:</p> <pre><code>class Parent(object): def bar(self): print "bar" class MetaFoo(type): def __new__(cls, name, bases, dict): return type(name, (Parent,) + bases, dict) class Foo(obj...
10
2008-09-15T19:01:27Z
[ "python", "metaclass" ]
Decorating a parent class method
66,636
<p>I would like to make a child class that has a method of the parent class where the method is a 'classmethod' in the child class but <strong>not</strong> in the parent class.</p> <p>Essentially, I am trying to accomplish the following:</p> <pre><code>class foo(Object): def meth1(self, val): self.value =...
1
2008-09-15T20:31:33Z
66,670
<p>What are you trying to accomplish? If I saw such a construct in live Python code, I would consider beating the original programmer.</p>
3
2008-09-15T20:34:53Z
[ "python", "oop", "inheritance" ]
Decorating a parent class method
66,636
<p>I would like to make a child class that has a method of the parent class where the method is a 'classmethod' in the child class but <strong>not</strong> in the parent class.</p> <p>Essentially, I am trying to accomplish the following:</p> <pre><code>class foo(Object): def meth1(self, val): self.value =...
1
2008-09-15T20:31:33Z
66,847
<p>I'm also not entirely sure what the exact behaviour you want is, but assuming its that you want bar.meth1(42) to be equivalent to foo.meth1 being a classmethod of bar (with "self" being the class), then you can acheive this with:</p> <pre><code>def convert_to_classmethod(method): return classmethod(method.im_fu...
3
2008-09-15T20:53:20Z
[ "python", "oop", "inheritance" ]
Decorating a parent class method
66,636
<p>I would like to make a child class that has a method of the parent class where the method is a 'classmethod' in the child class but <strong>not</strong> in the parent class.</p> <p>Essentially, I am trying to accomplish the following:</p> <pre><code>class foo(Object): def meth1(self, val): self.value =...
1
2008-09-15T20:31:33Z
66,936
<p>The question, as posed, seems quite odd to me: I can't see why anyone would want to do that. It is possible that you are misunderstanding just what a "classmethod" is in Python (it's a bit different from, say, a static method in Java).</p> <p>A normal method is more-or-less just a function which takes as its first ...
0
2008-09-15T21:02:08Z
[ "python", "oop", "inheritance" ]
How do I create a new signal in pygtk
66,730
<p>I've created a python object, but I want to send signals on it. I made it inherit from gobject.GObject, but there doesn't seem to be any way to create a new signal on my object.</p>
6
2008-09-15T20:41:58Z
66,883
<p>Here is how:</p> <pre><code>import gobject class MyGObjectClass(gobject.GObject): ... gobject.signal_new("signal-name", MyGObjectClass, gobject.SIGNAL_RUN_FIRST, None, (str, int)) </code></pre> <p>Where the second to last argument is the return type and the last argument is a tuple of argument types.</p>...
4
2008-09-15T20:57:10Z
[ "python", "gtk", "pygtk", "gobject" ]
How do I create a new signal in pygtk
66,730
<p>I've created a python object, but I want to send signals on it. I made it inherit from gobject.GObject, but there doesn't seem to be any way to create a new signal on my object.</p>
6
2008-09-15T20:41:58Z
67,743
<p>You can also define signals inside the class definition:</p> <pre><code>class MyGObjectClass(gobject.GObject): __gsignals__ = { "some-signal": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (object, )), } </code></pre> <p>The contents of the tuple are the the same as the three last arguments to <code>...
10
2008-09-15T22:52:27Z
[ "python", "gtk", "pygtk", "gobject" ]
How do I create a new signal in pygtk
66,730
<p>I've created a python object, but I want to send signals on it. I made it inherit from gobject.GObject, but there doesn't seem to be any way to create a new signal on my object.</p>
6
2008-09-15T20:41:58Z
126,355
<p>If you use kiwi available <a href="http://kiwi.async.com.br/" rel="nofollow">here</a> you can just do:</p> <pre><code>from kiwi.utils import gsignal class MyObject(gobject.GObject): gsignal('signal-name') </code></pre>
2
2008-09-24T10:14:06Z
[ "python", "gtk", "pygtk", "gobject" ]
mod_python/MySQL error on INSERT with a lot of data: "OperationalError: (2006, 'MySQL server has gone away')"
67,180
<p>When doing an INSERT with a lot of data, ie:</p> <pre><code>INSERT INTO table (mediumtext_field) VALUES ('...lots of text here: about 2MB worth...') </code></pre> <p>MySQL returns </p> <blockquote> <p>"OperationalError: (2006, 'MySQL server has gone away')"</p> </blockquote> <p>This is happening within a minut...
0
2008-09-15T21:29:26Z
72,987
<p>check the max_packet setting in your my.cnf file. this determines the largest amount of data you can send to your mysql server in a single statement. exceeding this values results in that error.</p>
1
2008-09-16T14:34:30Z
[ "python", "mysql", "xampp", "mysql-error-2006" ]
why might my pyglet vertex lists and batches be very slow on Windows?
67,223
<p>I'm writing opengl code in python using the library pyglet. When I draw to the screen using pyglet.graphics.vertex_list or pyglet.graphics.batch objects, they are very slow (~0.1 fps) compared to plain old pyglet.graphics.draw() or just glVertex() calls, which are about 40fps for the same geometry. In Linux the vert...
1
2008-09-15T21:33:41Z
67,921
<p>I don't know personally, but I noticed that you haven't posted to <a href="http://groups.google.com/group/pyglet-users" rel="nofollow">the pyglet mailing list</a> about this. More Pyglet users, as well as the primary developer, read that list.</p>
1
2008-09-15T23:24:55Z
[ "python", "opengl", "pyglet" ]
why might my pyglet vertex lists and batches be very slow on Windows?
67,223
<p>I'm writing opengl code in python using the library pyglet. When I draw to the screen using pyglet.graphics.vertex_list or pyglet.graphics.batch objects, they are very slow (~0.1 fps) compared to plain old pyglet.graphics.draw() or just glVertex() calls, which are about 40fps for the same geometry. In Linux the vert...
1
2008-09-15T21:33:41Z
835,601
<p>Don't forget to invoke your pyglet scripts with 'python -O myscript.py', the '-O' flag can make a huge performance difference.</p>
4
2009-05-07T16:09:19Z
[ "python", "opengl", "pyglet" ]
Serving dynamically generated ZIP archives in Django
67,454
<p>How to serve users a dynamically generated ZIP archive in Django?</p> <p>I'm making a site, where users can choose any combination of available books and download them as ZIP archive. I'm worried that generating such archives for each request would slow my server down to a crawl. I have also heard that Django doesn...
41
2008-09-15T22:00:31Z
67,477
<p>Can't you just write a link to a "zip server" or whatnot? Why does the zip archive itself need to be served from Django? A 90's era CGI script to generate a zip and spit it to stdout is really all that's required here, at least as far as I can see.</p>
1
2008-09-15T22:03:03Z
[ "python", "django" ]
Serving dynamically generated ZIP archives in Django
67,454
<p>How to serve users a dynamically generated ZIP archive in Django?</p> <p>I'm making a site, where users can choose any combination of available books and download them as ZIP archive. I'm worried that generating such archives for each request would slow my server down to a crawl. I have also heard that Django doesn...
41
2008-09-15T22:00:31Z
67,527
<p>Django doesn't directly handle the generation of dynamic content (specifically Zip files). That work would be done by Python's standard library. You can take a look at how to dynamically create a Zip file in Python <a href="https://docs.python.org/2/library/zipfile.html#zipfile-objects" rel="nofollow">here</a>.</p> ...
6
2008-09-15T22:09:50Z
[ "python", "django" ]
Serving dynamically generated ZIP archives in Django
67,454
<p>How to serve users a dynamically generated ZIP archive in Django?</p> <p>I'm making a site, where users can choose any combination of available books and download them as ZIP archive. I'm worried that generating such archives for each request would slow my server down to a crawl. I have also heard that Django doesn...
41
2008-09-15T22:00:31Z
72,180
<p>The solution is as follows.</p> <p>Use Python module <a href="http://docs.python.org/lib/module-zipfile.html">zipfile</a> to create zip archive, but as the file specify <a href="http://docs.python.org/lib/module-StringIO.html">StringIO</a> object (ZipFile constructor requires file-like object). Add files you want t...
36
2008-09-16T13:30:56Z
[ "python", "django" ]
Serving dynamically generated ZIP archives in Django
67,454
<p>How to serve users a dynamically generated ZIP archive in Django?</p> <p>I'm making a site, where users can choose any combination of available books and download them as ZIP archive. I'm worried that generating such archives for each request would slow my server down to a crawl. I have also heard that Django doesn...
41
2008-09-15T22:00:31Z
73,617
<p>I suggest to use separate model for storing those temp zip files. You can create zip on-fly, save to model with filefield and finally send url to user.</p> <p>Advantages:</p> <ul> <li>Serving static zip files with django media mechanism (like usual uploads).</li> <li>Ability to cleanup stale zip files by regular c...
1
2008-09-16T15:31:39Z
[ "python", "django" ]
Serving dynamically generated ZIP archives in Django
67,454
<p>How to serve users a dynamically generated ZIP archive in Django?</p> <p>I'm making a site, where users can choose any combination of available books and download them as ZIP archive. I'm worried that generating such archives for each request would slow my server down to a crawl. I have also heard that Django doesn...
41
2008-09-15T22:00:31Z
12,951,461
<p>Here's a Django view to do this:</p> <pre><code>import os import zipfile import StringIO from django.http import HttpResponse def getfiles(request): # Files (local path) to put in the .zip # FIXME: Change this (get paths from DB etc) filenames = ["/tmp/file1.txt", "/tmp/file2.txt"] # Folder name...
29
2012-10-18T09:32:10Z
[ "python", "django" ]
Serving dynamically generated ZIP archives in Django
67,454
<p>How to serve users a dynamically generated ZIP archive in Django?</p> <p>I'm making a site, where users can choose any combination of available books and download them as ZIP archive. I'm worried that generating such archives for each request would slow my server down to a crawl. I have also heard that Django doesn...
41
2008-09-15T22:00:31Z
24,178,943
<p>This module generates and streams an archive: <a href="https://github.com/allanlei/python-zipstream" rel="nofollow">https://github.com/allanlei/python-zipstream</a></p> <p>(I'm not connected to the development. Just thinking about using it.)</p>
0
2014-06-12T07:39:32Z
[ "python", "django" ]
Serving dynamically generated ZIP archives in Django
67,454
<p>How to serve users a dynamically generated ZIP archive in Django?</p> <p>I'm making a site, where users can choose any combination of available books and download them as ZIP archive. I'm worried that generating such archives for each request would slow my server down to a crawl. I have also heard that Django doesn...
41
2008-09-15T22:00:31Z
26,658,291
<p>Shameless plug: you can use <a href="https://github.com/thibault/django-zipview">django-zipview</a> for the same purpose.</p> <p>After a <code>pip install django-zipview</code>:</p> <pre><code>from zipview.views import BaseZipView from reviews import Review class CommentsArchiveView(BaseZipView): """Downloa...
5
2014-10-30T16:24:34Z
[ "python", "django" ]
Serving dynamically generated ZIP archives in Django
67,454
<p>How to serve users a dynamically generated ZIP archive in Django?</p> <p>I'm making a site, where users can choose any combination of available books and download them as ZIP archive. I'm worried that generating such archives for each request would slow my server down to a crawl. I have also heard that Django doesn...
41
2008-09-15T22:00:31Z
40,076,866
<p>For python3 i use the <strong>io.ByteIO</strong> since <strong>StringIO</strong> is deprecated to achieve this. Hope it helps.</p> <pre><code>import io def my_downloadable_zip(request): zip_io = io.BytesIO() with zipfile.ZipFile(zip_io, mode='w', compression=zipfile.ZIP_DEFLATED) as backup_zip: ba...
0
2016-10-17T00:10:09Z
[ "python", "django" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
67,672
<p>I believe you can use <a href="https://docs.python.org/2/library/imp.html#imp.find_module" rel="nofollow"><code>imp.find_module()</code></a> and <a href="https://docs.python.org/2/library/imp.html#imp.load_module" rel="nofollow"><code>imp.load_module()</code></a> to load the specified module. You'll need to split t...
3
2008-09-15T22:37:24Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
67,692
<p>For Python 3.5+ use:</p> <pre><code>import importlib.util spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) foo.MyClass() </code></pre> <p>For Python 3.3 and 3.4 use:</p> <pre><code>from importlib.machinery imp...
575
2008-09-15T22:41:16Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
67,693
<p>You can use the </p> <pre><code>load_source(module_name, path_to_file) </code></pre> <p>method from <a href="https://docs.python.org/library/imp.html">imp module</a>.</p>
12
2008-09-15T22:41:24Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
67,705
<p><strong>Import package modules at runtime (Python recipe)</strong> </p> <p><a href="http://code.activestate.com/recipes/223972/" rel="nofollow">http://code.activestate.com/recipes/223972/</a></p> <pre><code>################### ## # ## classloader.py # ## # ################### import ...
2
2008-09-15T22:43:20Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
67,708
<p>You can also do something like this and add the directory that the configuration file is sitting in to the Python load path, and then just do a normal import, assuming you know the name of the file in advance, in this case "config".</p> <p>Messy, but it works.</p> <pre><code>configfile = '~/config.py' import os i...
13
2008-09-15T22:44:50Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
67,715
<p>Do you mean load or import?</p> <p>You can manipulate the sys.path list specify the path to your module, then import your module. For example, given a module at:</p> <pre><code>/foo/bar.py </code></pre> <p>You could do:</p> <pre><code>import sys sys.path[0:0] = '/foo' # puts the /foo directory at the start of yo...
8
2008-09-15T22:46:35Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
68,628
<pre><code>def import_file(full_path_to_module): try: import os module_dir, module_file = os.path.split(full_path_to_module) module_name, module_ext = os.path.splitext(module_file) save_cwd = os.getcwd() os.chdir(module_dir) module_obj = __import__(module_name) ...
9
2008-09-16T01:43:04Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
129,374
<p>The advantage of adding a path to sys.path (over using imp) is that it simplifies things when importing more than one module from a single package. For example:</p> <pre><code>import sys # the mock-0.3.1 dir contains testcase.py, testutils.py &amp; mock.py sys.path.append('/foo/bar/mock-0.3.1') from testcase impo...
185
2008-09-24T19:36:16Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
6,284,270
<p>I made a package that uses <code>imp</code> for you. I call it <code>import_file</code> and this is how it's used:</p> <pre><code>&gt;&gt;&gt;from import_file import import_file &gt;&gt;&gt;mylib = import_file('c:\\mylib.py') &gt;&gt;&gt;another = import_file('relative_subdir/another.py') </code></pre> <p>You can ...
1
2011-06-08T19:41:29Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
8,721,254
<p>This should work</p> <pre><code>path = os.path.join('./path/to/folder/with/py/files', '*.py') for infile in glob.glob(path): basename = os.path.basename(infile) basename_without_extension = basename[:-3] # http://docs.python.org/library/imp.html?highlight=imp#module-imp imp.load_source(basename_wit...
3
2012-01-04T02:17:21Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
25,827,116
<p>You can use the <code>pkgutil</code> module (specifically the <a href="https://docs.python.org/3/library/pkgutil.html#pkgutil.walk_packages" rel="nofollow"><code>walk_packages</code></a> method) to get a list of the packages in the current directory. From there it's trivial to use the <code>importlib</code> machiner...
2
2014-09-13T19:57:28Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
26,995,106
<p>In Linux, adding a symbolic link in the directory your python script is located works.</p> <p>ie: </p> <p>ln -s /absolute/path/to/module/module.py /absolute/path/to/script/module.py</p> <p>python will create /absolute/path/to/script/module.pyc and will update it if you change the contents of /absolute/path/to/mod...
1
2014-11-18T13:06:35Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
27,127,448
<p>The best way, I think, is from the official documentation (<a href="https://docs.python.org/3.2/library/imp.html#examples" rel="nofollow">29.1. imp — Access the import internals</a>):</p> <pre><code>import imp import sys def __import__(name, globals=None, locals=None, fromlist=None): # Fast path: see if the ...
0
2014-11-25T12:58:47Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
29,589,414
<p>This area of Python 3.4 seems to be extremely tortuous to understand! However with a bit of hacking using the code from Chris Calloway as a start I managed to get something working. Here's the basic function.</p> <pre><code>def import_module_from_file(full_path_to_module): """ Import a module given the full...
1
2015-04-12T12:22:56Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
30,605,451
<p>I'm not saying that it is better, but for the sake of completeness, I wanted to suggest the <a href="https://docs.python.org/3/library/functions.html#exec" rel="nofollow"><code>exec</code></a> function, available in both python 2 and 3. <code>exec</code> allows you to execute arbitrary code in either the global scop...
2
2015-06-02T19:57:46Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
32,905,959
<p>To import a module from a given filename, you can temporarily extend the path, and restore the system path in the finally block <a href="http://effbot.org/zone/import-string.htm" rel="nofollow">reference:</a></p> <pre><code>filename = "directory/module.py" directory, module_name = os.path.split(filename) module_na...
1
2015-10-02T11:14:13Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
34,570,493
<p>It may be obvious but in interactive shell:</p> <pre><code>cd path import module </code></pre>
-1
2016-01-02T20:37:55Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
37,339,817
<p>It sounds like you don't want to specifically import the configuration file (which has a whole lot of side effects and additional complications involved), you just want to run it, and be able to access the resulting namespace. The standard library provides an API specifically for that in the form of <a href="https:/...
1
2016-05-20T06:52:17Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
37,611,448
<p>Here is some code that works in all Python versions, from 2.7-3.5 and probably even others.</p> <pre><code>config_file = "/tmp/config.py" with open(config_file) as f: code = compile(f.read(), config_file, 'exec') exec(code, globals(), locals()) </code></pre> <p>I tested it. It may be ugly but so far is the...
1
2016-06-03T10:04:52Z
[ "python", "configuration", "python-import", "python-module" ]
How to import a module given the full path?
67,631
<p>How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.</p>
498
2008-09-15T22:30:55Z
38,076,473
<pre><code>import sys sys.path.append('/your/modul/path') </code></pre>
1
2016-06-28T12:36:18Z
[ "python", "configuration", "python-import", "python-module" ]
Why do you need explicitly have the "self" argument into a Python method?
68,282
<p>When defining a method on a class in Python, it looks something like this:</p> <pre><code>class MyClass(object): def __init__(self, x, y): self.x = x self.y = y </code></pre> <p>But in some other languages, such as C#, you have a reference to the object that the method is bound to with the "thi...
125
2008-09-16T00:39:55Z
68,320
<p>It's to minimize the difference between methods and functions. It allows you to easily generate methods in metaclasses, or add methods at runtime to pre-existing classes.</p> <p>e.g.</p> <pre><code>&gt;&gt;&gt; class C(object): ... def foo(self): ... print "Hi!" ... &gt;&gt;&gt; &gt;&gt;&gt; def bar(se...
37
2008-09-16T00:47:18Z
[ "python" ]
Why do you need explicitly have the "self" argument into a Python method?
68,282
<p>When defining a method on a class in Python, it looks something like this:</p> <pre><code>class MyClass(object): def __init__(self, x, y): self.x = x self.y = y </code></pre> <p>But in some other languages, such as C#, you have a reference to the object that the method is bound to with the "thi...
125
2008-09-16T00:39:55Z
68,324
<p>I like to quote Peters' Zen of Python. "Explicit is better than implicit."</p> <p>In Java and C++, '<code>this.</code>' can be deduced, except when you have variable names that make it impossible to deduce. So you sometimes need it and sometimes don't.</p> <p>Python elects to make things like this explicit rath...
68
2008-09-16T00:47:55Z
[ "python" ]
Why do you need explicitly have the "self" argument into a Python method?
68,282
<p>When defining a method on a class in Python, it looks something like this:</p> <pre><code>class MyClass(object): def __init__(self, x, y): self.x = x self.y = y </code></pre> <p>But in some other languages, such as C#, you have a reference to the object that the method is bound to with the "thi...
125
2008-09-16T00:39:55Z
68,329
<p>There is also another very simple answer: according to the <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow">zen of python</a>, "explicit is better than implicit".</p>
-1
2008-09-16T00:49:26Z
[ "python" ]
Why do you need explicitly have the "self" argument into a Python method?
68,282
<p>When defining a method on a class in Python, it looks something like this:</p> <pre><code>class MyClass(object): def __init__(self, x, y): self.x = x self.y = y </code></pre> <p>But in some other languages, such as C#, you have a reference to the object that the method is bound to with the "thi...
125
2008-09-16T00:39:55Z
68,472
<p>Python doesn't force you on using "self". You can give it whatever name you want. You just have to remember that the first argument in a method definition header is a reference to the object.</p>
10
2008-09-16T01:15:16Z
[ "python" ]
Why do you need explicitly have the "self" argument into a Python method?
68,282
<p>When defining a method on a class in Python, it looks something like this:</p> <pre><code>class MyClass(object): def __init__(self, x, y): self.x = x self.y = y </code></pre> <p>But in some other languages, such as C#, you have a reference to the object that the method is bound to with the "thi...
125
2008-09-16T00:39:55Z
308,045
<p>I suggest that one should read <a href="http://neopythonic.blogspot.com/" rel="nofollow">Guido van Rossum's blog</a> on this topic - <a href="http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html" rel="nofollow">Why explicit self has to stay</a>.</p> <blockquote> <p>When a method definition i...
38
2008-11-21T06:28:26Z
[ "python" ]
Why do you need explicitly have the "self" argument into a Python method?
68,282
<p>When defining a method on a class in Python, it looks something like this:</p> <pre><code>class MyClass(object): def __init__(self, x, y): self.x = x self.y = y </code></pre> <p>But in some other languages, such as C#, you have a reference to the object that the method is bound to with the "thi...
125
2008-09-16T00:39:55Z
29,369,904
<p>I think it has to do with PEP 227:</p> <blockquote> <p>Names in class scope are not accessible. Names are resolved in the innermost enclosing function scope. If a class definition occurs in a chain of nested scopes, the resolution process skips class definitions. This rule prevents odd interactions betwe...
1
2015-03-31T13:29:09Z
[ "python" ]
Why do you need explicitly have the "self" argument into a Python method?
68,282
<p>When defining a method on a class in Python, it looks something like this:</p> <pre><code>class MyClass(object): def __init__(self, x, y): self.x = x self.y = y </code></pre> <p>But in some other languages, such as C#, you have a reference to the object that the method is bound to with the "thi...
125
2008-09-16T00:39:55Z
31,367,197
<p>Also allows you to do this: (in short, invoking <code>Outer(3).create_inner_class(4)().weird_sum_with_closure_scope(5)</code> will return 12, but will do so in the craziest of ways.</p> <pre><code>class Outer(object): def __init__(self, outer_num): self.outer_num = outer_num def create_inner_class(...
4
2015-07-12T11:18:06Z
[ "python" ]
Why do you need explicitly have the "self" argument into a Python method?
68,282
<p>When defining a method on a class in Python, it looks something like this:</p> <pre><code>class MyClass(object): def __init__(self, x, y): self.x = x self.y = y </code></pre> <p>But in some other languages, such as C#, you have a reference to the object that the method is bound to with the "thi...
125
2008-09-16T00:39:55Z
36,968,741
<p>I think the real reason besides "The Zen of Python" is that Functions are first class citizens in Python. </p> <p>Which essentially makes them an Object. Now The fundamental issue is if your functions are object as well then, in Object oriented paradigm how would you send messages to Objects when the messages thems...
0
2016-05-01T16:01:06Z
[ "python" ]
Change command Method for Tkinter Button in Python
68,327
<p>I create a new Button object but did not specify the <code>command</code> option upon creation. Is there a way in Tkinter to change the command (onclick) function after the object has been created?</p>
3
2008-09-16T00:48:37Z
68,455
<p>Sure; just use the <code>bind</code> method to specify the callback after the button has been created. I've just written and tested the example below. You can find a nice tutorial on doing this at <a href="http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm" rel="nofollow">http://www.pyt...
1
2008-09-16T01:12:22Z
[ "python", "user-interface", "tkinter" ]
Change command Method for Tkinter Button in Python
68,327
<p>I create a new Button object but did not specify the <code>command</code> option upon creation. Is there a way in Tkinter to change the command (onclick) function after the object has been created?</p>
3
2008-09-16T00:48:37Z
68,524
<p>Though <a href="http://stackoverflow.com/questions/68327/change-command-method-for-tkinter-button-in-python#68455">Eli Courtwright's</a> program will work fine¹, what you really seem to want though is just a way to reconfigure after instantiation any attribute which you could have set when you instantiated². How ...
12
2008-09-16T01:24:37Z
[ "python", "user-interface", "tkinter" ]
How to copy a file to a remote server in Python using SCP or SSH?
68,335
<p>I have a text file on my local machine that is generated by a daily Python script run in cron. </p> <p>I would like to add a bit of code to have that file sent securely to my server over SSH.</p>
55
2008-09-16T00:50:10Z
68,365
<p>If you want the simple approach, this should work.</p> <p>You'll want to ".close()" the file first so you know it's flushed to disk from Python.</p> <pre><code>import os os.system("scp FILE USER@SERVER:PATH") #e.g. os.system("scp foo.bar [email protected]:/path/to/foo.bar") </code></pre> <p>You need to generate (on th...
21
2008-09-16T00:55:43Z
[ "python", "ssh", "automation", "scp" ]
How to copy a file to a remote server in Python using SCP or SSH?
68,335
<p>I have a text file on my local machine that is generated by a daily Python script run in cron. </p> <p>I would like to add a bit of code to have that file sent securely to my server over SSH.</p>
55
2008-09-16T00:50:10Z
68,377
<p>Kind of hacky, but the following should work :)</p> <pre><code>import os filePath = "/foo/bar/baz.py" serverPath = "/blah/boo/boom.py" os.system("scp "+filePath+" [email protected]:"+serverPath) </code></pre>
-1
2008-09-16T00:56:31Z
[ "python", "ssh", "automation", "scp" ]
How to copy a file to a remote server in Python using SCP or SSH?
68,335
<p>I have a text file on my local machine that is generated by a daily Python script run in cron. </p> <p>I would like to add a bit of code to have that file sent securely to my server over SSH.</p>
55
2008-09-16T00:50:10Z
68,382
<p>You'd probably use the <a href="http://docs.python.org/lib/module-subprocess.html" rel="nofollow">subprocess module</a>. Something like this:</p> <pre><code>import subprocess p = subprocess.Popen(["scp", myfile, destination]) sts = os.waitpid(p.pid, 0) </code></pre> <p>Where <code>destination</code> is probably of...
23
2008-09-16T00:58:49Z
[ "python", "ssh", "automation", "scp" ]
How to copy a file to a remote server in Python using SCP or SSH?
68,335
<p>I have a text file on my local machine that is generated by a daily Python script run in cron. </p> <p>I would like to add a bit of code to have that file sent securely to my server over SSH.</p>
55
2008-09-16T00:50:10Z
68,566
<p>There are a couple of different ways to approach the problem:</p> <ol> <li>Wrap command-line programs</li> <li>use a Python library that provides SSH capabilities (eg - <a href="http://www.lag.net/paramiko/">Paramiko</a> or <a href="http://twistedmatrix.com/trac/wiki/TwistedConch">Twisted Conch</a>)</li> </ol> <p>...
10
2008-09-16T01:32:08Z
[ "python", "ssh", "automation", "scp" ]
How to copy a file to a remote server in Python using SCP or SSH?
68,335
<p>I have a text file on my local machine that is generated by a daily Python script run in cron. </p> <p>I would like to add a bit of code to have that file sent securely to my server over SSH.</p>
55
2008-09-16T00:50:10Z
69,596
<p>To do this in Python (i.e. not wrapping scp through subprocess.Popen or similar) with the <a href="http://www.lag.net/paramiko/">Paramiko</a> library, you would do something like this:</p> <pre><code>import os import paramiko ssh = paramiko.SSHClient() ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh...
76
2008-09-16T05:27:59Z
[ "python", "ssh", "automation", "scp" ]
How to copy a file to a remote server in Python using SCP or SSH?
68,335
<p>I have a text file on my local machine that is generated by a daily Python script run in cron. </p> <p>I would like to add a bit of code to have that file sent securely to my server over SSH.</p>
55
2008-09-16T00:50:10Z
22,710,513
<p><a href="http://fabfile.org" rel="nofollow"><code>fabric</code></a> could be used to upload files vis ssh:</p> <pre><code>#!/usr/bin/env python from fabric.api import execute, put from fabric.network import disconnect_all if __name__=="__main__": import sys # specify hostname to connect to and the remote/l...
1
2014-03-28T10:41:18Z
[ "python", "ssh", "automation", "scp" ]
How to copy a file to a remote server in Python using SCP or SSH?
68,335
<p>I have a text file on my local machine that is generated by a daily Python script run in cron. </p> <p>I would like to add a bit of code to have that file sent securely to my server over SSH.</p>
55
2008-09-16T00:50:10Z
22,710,752
<p>Calling <code>scp</code> command via subprocess doesn't allow to receive the progress report inside the script. <code>pexpect</code> could be used to extract that info:</p> <pre><code>import pipes import re import pexpect # $ pip install pexpect def progress(locals): # extract percents print(int(re.search(...
0
2014-03-28T10:50:46Z
[ "python", "ssh", "automation", "scp" ]
How to copy a file to a remote server in Python using SCP or SSH?
68,335
<p>I have a text file on my local machine that is generated by a daily Python script run in cron. </p> <p>I would like to add a bit of code to have that file sent securely to my server over SSH.</p>
55
2008-09-16T00:50:10Z
38,556,319
<p>Reached the same problem, but instead of "hacking" or emulating command line:</p> <p>Found this answer <a href="https://pypi.python.org/pypi/scp" rel="nofollow">here</a>.</p> <pre><code>from paramiko import SSHClient from scp import SCPClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('example.com'...
1
2016-07-24T20:12:13Z
[ "python", "ssh", "automation", "scp" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
68,502
<p>Yes. You'd use the <code>urllib2</code> module, and encode using the <code>multipart/form-data</code> content type. Here is some sample code to get you started -- it's a bit more than just file uploading, but you should be able to read through it and see how it works:</p> <pre><code>user_agent = "image uploader" de...
18
2008-09-16T01:21:20Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
68,507
<p>This <a href="http://code.activestate.com/recipes/146306/" rel="nofollow">code</a> might be of use to you</p>
1
2008-09-16T01:22:03Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
75,158
<p>You may also want to have a look at <a href="http://code.google.com/p/httplib2/" rel="nofollow">httplib2</a>, with <a href="http://bitworking.org/projects/httplib2/doc/html/libhttplib2.html#examples" rel="nofollow">examples</a>. I find using httplib2 is more concise than using the built-in HTTP modules.</p>
0
2008-09-16T18:03:09Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
75,186
<p>Blatant self-promotion:</p> <p>check out my <a href="http://atlee.ca/software/poster/">poster</a> module for python. It handles the multipart/form-data encoding, as well as supporting streaming uploads (so you don't have to load the entire file into memory before submitting the HTTP POST request).</p>
59
2008-09-16T18:05:01Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
525,193
<p>Chris Atlee's <a href="http://atlee.ca/software/poster/" rel="nofollow">poster</a> library works really well for this (particularly the convenience function <code>poster.encode.multipart_encode()</code>). As a bonus, it supports streaming of large files without loading an entire file into memory. See also <a href="h...
2
2009-02-08T05:20:36Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
7,969,778
<p>The only thing that stops you from using urlopen directly on a file object is the fact that the builtin file object lacks a <strong>len</strong> definition. A simple way is to create a subclass, which provides urlopen with the correct file. I have also modified the Content-Type header in the file below.</p> <pre><...
4
2011-11-01T16:43:03Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
10,234,640
<p>From <a href="http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file">http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file</a></p> <p>Requests makes it very simple to upload Multipart-encoded files:</p> <pre><code>&gt;&gt;&gt; r = requests.post(...
101
2012-04-19T18:40:02Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
31,305,207
<p>Looks like python requests does not handle extremely large multi-part files.</p> <p>The documentation recommends you look into <code>requests-toolbelt</code>.</p> <p><a href="https://toolbelt.readthedocs.org/en/latest/uploading-data.html" rel="nofollow">Here's the pertinent page</a> from their documentation.</p>
3
2015-07-08T22:51:47Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
36,078,069
<pre><code>def visit_v2(device_code, camera_code): image1 = MultipartParam.from_file("files", "/home/yuzx/1.txt") image2 = MultipartParam.from_file("files", "/home/yuzx/2.txt") datagen, headers = multipart_encode([('device_code', device_code), ('position', 3), ('person_data', person_data), image1, image2]) ...
0
2016-03-18T06:59:16Z
[ "python", "post", "file-upload", "http-post" ]
Send file using POST from a Python script
68,477
<p>Is there a way to send a file using POST from a Python script?</p>
70
2008-09-16T01:16:24Z
37,142,773
<p>I am trying to test django rest api and its working for me:</p> <pre><code>def test_upload_file(self): filename = "/Users/Ranvijay/tests/test_price_matrix.csv" data = {'file': open(filename, 'rb')} client = APIClient() # client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) ...
1
2016-05-10T15:23:21Z
[ "python", "post", "file-upload", "http-post" ]
libxml2-p25 on OS X 10.5 needs sudo?
68,541
<p>When trying to use <code>libxml2</code> as myself I get an error saying the package cannot be found. If I run as as super user I am able to import fine.</p> <p>I have installed <code>python25</code> and all <code>libxml2</code> and <code>libxml2-py25</code> related libraries via fink and own the entire path includi...
0
2008-09-16T01:27:28Z
69,513
<p>Check your path (run 'echo $PATH')</p>
3
2008-09-16T05:01:08Z
[ "python", "osx", "libxml2" ]
libxml2-p25 on OS X 10.5 needs sudo?
68,541
<p>When trying to use <code>libxml2</code> as myself I get an error saying the package cannot be found. If I run as as super user I am able to import fine.</p> <p>I have installed <code>python25</code> and all <code>libxml2</code> and <code>libxml2-py25</code> related libraries via fink and own the entire path includi...
0
2008-09-16T01:27:28Z
70,895
<p>I would suspect the permissions on the library. Can you do a strace or similar to find out the filenames it's looking for, and then check the permissions on them?</p>
0
2008-09-16T10:04:43Z
[ "python", "osx", "libxml2" ]
libxml2-p25 on OS X 10.5 needs sudo?
68,541
<p>When trying to use <code>libxml2</code> as myself I get an error saying the package cannot be found. If I run as as super user I am able to import fine.</p> <p>I have installed <code>python25</code> and all <code>libxml2</code> and <code>libxml2-py25</code> related libraries via fink and own the entire path includi...
0
2008-09-16T01:27:28Z
77,114
<p>The path was the mistake. Thanks. I was looking at everything else to be wrong :-)</p>
0
2008-09-16T21:08:07Z
[ "python", "osx", "libxml2" ]
Are tuples more efficient than lists in Python?
68,630
<p>Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements? </p>
101
2008-09-16T01:43:39Z
68,638
<p>Tuples should be slightly more efficient and because of that, faster, than lists because they are immutable.</p>
1
2008-09-16T01:45:39Z
[ "python", "performance", "python-internals" ]
Are tuples more efficient than lists in Python?
68,630
<p>Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements? </p>
101
2008-09-16T01:43:39Z
68,712
<p>In general, you might expect tuples to be slightly faster. However you should definitely test your specific case (if the difference might impact the performance of your program -- remember "premature optimization is the root of all evil").</p> <p>Python makes this very easy: <a href="https://docs.python.org/2/libr...
131
2008-09-16T01:57:10Z
[ "python", "performance", "python-internals" ]
Are tuples more efficient than lists in Python?
68,630
<p>Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements? </p>
101
2008-09-16T01:43:39Z
68,817
<p>The <a href="https://docs.python.org/3/library/dis.html"><code>dis</code></a> module disassembles the byte code for a function and is useful to see the difference between tuples and lists.</p> <p>In this case, you can see that accessing an element generates identical code, but that assigning a tuple is much faster ...
88
2008-09-16T02:13:29Z
[ "python", "performance", "python-internals" ]
Are tuples more efficient than lists in Python?
68,630
<p>Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements? </p>
101
2008-09-16T01:43:39Z
70,968
<p>Tuples, being immutable, are more memory efficient; lists, for efficiency, overallocate memory in order to allow appends without constant <code>realloc</code>s. So, if you want to iterate through a constant sequence of values in your code (eg <code>for direction in 'up', 'right', 'down', 'left':</code>), tuples are ...
26
2008-09-16T10:16:52Z
[ "python", "performance", "python-internals" ]
Are tuples more efficient than lists in Python?
68,630
<p>Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements? </p>
101
2008-09-16T01:43:39Z
71,295
<p>You should also consider the <code>array</code> module in the standard library if all the items in your list or tuple are of the same type. It can be faster and take less memory.</p>
6
2008-09-16T11:14:08Z
[ "python", "performance", "python-internals" ]
Are tuples more efficient than lists in Python?
68,630
<p>Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements? </p>
101
2008-09-16T01:43:39Z
22,140,115
<p>There are several performance differences between tuples and lists when it comes to instantiation and retrieval of elements:</p> <ol> <li><p>Tuples containing immutable entries can be optimized into constants by Python's peephole optimizer. Lists, on the other hand, get build-up from scratch:</p> <pre><code>&gt;&...
26
2014-03-03T06:30:39Z
[ "python", "performance", "python-internals" ]
Static class variables in Python
68,645
<p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
1,015
2008-09-16T01:46:36Z
68,672
<p>Variables declared inside the class definition, but not inside a method are class or static variables:</p> <pre><code>&gt;&gt;&gt; class MyClass: ... i = 3 ... &gt;&gt;&gt; MyClass.i 3 </code></pre> <p>As @<a href="http://stackoverflow.com/questions/68645/static-class-variables-in-python#answer-69067">millerd...
1,030
2008-09-16T01:51:06Z
[ "python", "class", "methods", "static", "class-variables" ]
Static class variables in Python
68,645
<p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
1,015
2008-09-16T01:46:36Z
68,747
<p>Personally I would use a classmethod whenever I needed a static method. Mainly because I get the class as an argument.</p> <pre><code>class myObj(object): def myMethod(cls) ... myMethod = classmethod(myMethod) </code></pre> <p>or use a decorator</p> <pre><code>class myObj(object): @classmethod de...
9
2008-09-16T02:02:45Z
[ "python", "class", "methods", "static", "class-variables" ]
Static class variables in Python
68,645
<p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
1,015
2008-09-16T01:46:36Z
68,770
<p>Static methods in python are called <a href="http://pyref.infogami.com/classmethod" rel="nofollow">classmethod</a>s. Take a look at the following code</p> <pre><code>class MyClass: def myInstanceMethod(self): print 'output from an instance method' @classmethod def myStaticMethod(cls): pr...
5
2008-09-16T02:05:49Z
[ "python", "class", "methods", "static", "class-variables" ]
Static class variables in Python
68,645
<p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
1,015
2008-09-16T01:46:36Z
69,067
<p>@Blair Conrad said static variables declared inside the class definition, but not inside a method are class or "static" variables:</p> <pre><code>&gt;&gt;&gt; class Test(object): ... i = 3 ... &gt;&gt;&gt; Test.i 3 </code></pre> <p>There are a few gotcha's here. Carrying on from the example above:</p> <pre><c...
400
2008-09-16T03:04:08Z
[ "python", "class", "methods", "static", "class-variables" ]
Static class variables in Python
68,645
<p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
1,015
2008-09-16T01:46:36Z
79,840
<p>To avoid any potential confusion, I would like to contrast static variables and immutable objects.</p> <p>Some primitive object types like integers, floats, strings, and touples are immutable in Python. This means that the object that is referred to by a given name cannot change if it is of one of the aforementione...
2
2008-09-17T04:01:08Z
[ "python", "class", "methods", "static", "class-variables" ]
Static class variables in Python
68,645
<p>Is it possible to have static class variables or methods in python? What syntax is required to do this?</p>
1,015
2008-09-16T01:46:36Z
81,002
<p>You can also add class variables to classes on the fly</p> <pre><code>&gt;&gt;&gt; class X: ... pass ... &gt;&gt;&gt; X.bar = 0 &gt;&gt;&gt; x = X() &gt;&gt;&gt; x.bar 0 &gt;&gt;&gt; x.foo Traceback (most recent call last): File "&lt;interactive input&gt;", line 1, in &lt;module&gt; AttributeError: X instanc...
19
2008-09-17T08:06:22Z
[ "python", "class", "methods", "static", "class-variables" ]