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
Traversing FTP listing
1,854,572
<p>I am trying to to get all directories' name from an FTP server and store them in hierarchical order in a multidimensional list or dict</p> <p>So for example, a server that contains the following structure:</p> <pre><code>/www/ mysite.com images png jpg </code></pre> <p>at the e...
3
2009-12-06T06:42:50Z
1,854,932
<p>Here is a naive and slow implementation. It is slow because it tries to CWD to each directory entry to determine if it is a directory or a file, but this works. One could optimize it by parsing LIST command output, but this is strongly server-implementation dependent.</p> <pre><code>import ftplib def traverse(ftp,...
6
2009-12-06T09:59:56Z
[ "python", "ftp", "traversal" ]
Traversing FTP listing
1,854,572
<p>I am trying to to get all directories' name from an FTP server and store them in hierarchical order in a multidimensional list or dict</p> <p>So for example, a server that contains the following structure:</p> <pre><code>/www/ mysite.com images png jpg </code></pre> <p>at the e...
3
2009-12-06T06:42:50Z
1,856,763
<p>You're not going to like this, but "it depends on the server" or, more accurately, "it depends on the output format of the server".</p> <p>Different servers can be set to display different output, so your initial proposal is bound to failure in the general case.</p> <p>The "naive and slow implementation" above wil...
2
2009-12-06T22:01:36Z
[ "python", "ftp", "traversal" ]
Traversing FTP listing
1,854,572
<p>I am trying to to get all directories' name from an FTP server and store them in hierarchical order in a multidimensional list or dict</p> <p>So for example, a server that contains the following structure:</p> <pre><code>/www/ mysite.com images png jpg </code></pre> <p>at the e...
3
2009-12-06T06:42:50Z
1,856,802
<p>If we are using Python look at:</p> <p><a href="http://docs.python.org/library/os.path.html" rel="nofollow">http://docs.python.org/library/os.path.html</a> (os.path.walk)</p> <p>If there already is a good module for this, don't reinvent the wheel. Can't believe the post two spots above got two ups, anyway, enjoy.<...
-3
2009-12-06T22:14:13Z
[ "python", "ftp", "traversal" ]
Traversing FTP listing
1,854,572
<p>I am trying to to get all directories' name from an FTP server and store them in hierarchical order in a multidimensional list or dict</p> <p>So for example, a server that contains the following structure:</p> <pre><code>/www/ mysite.com images png jpg </code></pre> <p>at the e...
3
2009-12-06T06:42:50Z
3,114,571
<p>If the server supports the <code>MLSD</code> command, then use the “a directory and its descendants” code from <a href="http://stackoverflow.com/questions/2867217/how-to-delete-files-with-a-python-script-from-a-ftp-server-which-are-older-than-7/3114477#3114477">that</a> answer.</p>
2
2010-06-24T23:12:06Z
[ "python", "ftp", "traversal" ]
Traversing FTP listing
1,854,572
<p>I am trying to to get all directories' name from an FTP server and store them in hierarchical order in a multidimensional list or dict</p> <p>So for example, a server that contains the following structure:</p> <pre><code>/www/ mysite.com images png jpg </code></pre> <p>at the e...
3
2009-12-06T06:42:50Z
35,470,574
<p>Here's a first draft of a Python 3 script that worked for me. It's much faster than calling <code>cwd()</code>. Pass in server, port, directory, username, and password as arguments. I left output as a list as an exercise for the reader.</p> <pre><code>import ftplib import sys def ftp_walk(ftp, dir): dirs = [] ...
2
2016-02-18T00:34:17Z
[ "python", "ftp", "traversal" ]
Fast conversion of numeric data into fixed width format file in Python
1,854,713
<p>What is the fastest way of converting records holding only numeric data into fixed with format strings and writing them to a file in Python? For example, suppose <code>record</code> is a huge list consisting of objects with attributes <code>id</code>, <code>x</code>, <code>y</code>, and <code>wt</code> and we frequ...
3
2009-12-06T08:08:50Z
1,854,765
<p>you can try to build all the output strings in the memory, e.g. use a long string. and then write this long string in the file.</p> <p>more faster: you may want to use binary files rather text files for logging information. But then you need to write another tool to view the binary files. </p>
0
2009-12-06T08:29:03Z
[ "python", "performance", "large-data-volumes" ]
Fast conversion of numeric data into fixed width format file in Python
1,854,713
<p>What is the fastest way of converting records holding only numeric data into fixed with format strings and writing them to a file in Python? For example, suppose <code>record</code> is a huge list consisting of objects with attributes <code>id</code>, <code>x</code>, <code>y</code>, and <code>wt</code> and we frequ...
3
2009-12-06T08:08:50Z
1,854,868
<p>I don't see anything about your snippet of code that I could really optimize. So, I think we need to do something completely different to solve your problem.</p> <p>Your problem seems to be that you are chewing large amounts of data, and it's slow to format the data into strings and write the strings to a file. Y...
2
2009-12-06T09:26:50Z
[ "python", "performance", "large-data-volumes" ]
Fast conversion of numeric data into fixed width format file in Python
1,854,713
<p>What is the fastest way of converting records holding only numeric data into fixed with format strings and writing them to a file in Python? For example, suppose <code>record</code> is a huge list consisting of objects with attributes <code>id</code>, <code>x</code>, <code>y</code>, and <code>wt</code> and we frequ...
3
2009-12-06T08:08:50Z
1,856,300
<p>Now that you updated your question, I have a slightly better idea of what you are facing.</p> <p>I don't know what the "current database solution that is deemed both too slow and cumbersome" is, but I still think a database would help if used correctly.</p> <p>Run the Python code to collect data, and use an ORM m...
0
2009-12-06T19:21:42Z
[ "python", "performance", "large-data-volumes" ]
Fast conversion of numeric data into fixed width format file in Python
1,854,713
<p>What is the fastest way of converting records holding only numeric data into fixed with format strings and writing them to a file in Python? For example, suppose <code>record</code> is a huge list consisting of objects with attributes <code>id</code>, <code>x</code>, <code>y</code>, and <code>wt</code> and we frequ...
3
2009-12-06T08:08:50Z
1,856,322
<p>I was trying to check if <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html" rel="nofollow">numpy.savetxt</a> could speed things up a bit so I wrote the following simulation:</p> <pre><code>import sys import numpy as np fmt = '%7.0f %11.5e %11.5e %7.5f' records = 10000 np.random.seed(...
3
2009-12-06T19:26:55Z
[ "python", "performance", "large-data-volumes" ]
Fast conversion of numeric data into fixed width format file in Python
1,854,713
<p>What is the fastest way of converting records holding only numeric data into fixed with format strings and writing them to a file in Python? For example, suppose <code>record</code> is a huge list consisting of objects with attributes <code>id</code>, <code>x</code>, <code>y</code>, and <code>wt</code> and we frequ...
3
2009-12-06T08:08:50Z
1,856,353
<p>You could use try to push your loop to C using ctypes.</p>
0
2009-12-06T19:36:29Z
[ "python", "performance", "large-data-volumes" ]
How to auto-run a script
1,854,718
<p>I created a script that will tell me what to wear in the morning based on the weather (ie rain slicker if it will rain, heavy jacket if it will be cold, etc). I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click fr...
3
2009-12-06T08:10:24Z
1,854,734
<p>What you want to do is create an executable file.</p> <p>I've never used a Mac or Python, but look at this question and the first answer:</p> <p><a href="http://stackoverflow.com/questions/2933/an-executable-python-app">http://stackoverflow.com/questions/2933/an-executable-python-app</a></p> <p>Seems <a href="htt...
0
2009-12-06T08:16:58Z
[ "python", "autorun" ]
How to auto-run a script
1,854,718
<p>I created a script that will tell me what to wear in the morning based on the weather (ie rain slicker if it will rain, heavy jacket if it will be cold, etc). I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click fr...
3
2009-12-06T08:10:24Z
1,854,777
<p>You want the script to download the weather information online and output the clothes based on your predefined rules?</p> <p>If this is the case, use urllib to download the page and do some ad hoc parsing over the downloaded html page to get the whether information. And write your logic using nested IF THEN ELSE bl...
-3
2009-12-06T08:33:19Z
[ "python", "autorun" ]
How to auto-run a script
1,854,718
<p>I created a script that will tell me what to wear in the morning based on the weather (ie rain slicker if it will rain, heavy jacket if it will be cold, etc). I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click fr...
3
2009-12-06T08:10:24Z
1,854,901
<p>This worked for me on Snow Leopard:</p> <p>-Put the python script on the desktop.</p> <p>-Right click on the script file, and choose "Get info"</p> <p>-Find "Open With", and choose "Python Launcher" from the dropdown box</p> <p>Now double-clicking the script file will run the script in a new terminal window. </p...
4
2009-12-06T09:42:37Z
[ "python", "autorun" ]
How to auto-run a script
1,854,718
<p>I created a script that will tell me what to wear in the morning based on the weather (ie rain slicker if it will rain, heavy jacket if it will be cold, etc). I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click fr...
3
2009-12-06T08:10:24Z
34,125,858
<p>Use a batch file to make it automatic </p> <p>Example : 1. Open Notepad -> type the following. This one's for Windows..It might give you a hint :start C:\Python34\python.exe(your python file location)Your *.py file location.</p> <p>:end Save this with a *.bat extension That's it ..you can configure more on t...
0
2015-12-07T03:15:45Z
[ "python", "autorun" ]
Is there an Open Source Python library for sanitizing HTML and removing all Javascript?
1,854,806
<p>I want to write a web application that allows users to enter any HTML that can occur inside a <code>&lt;div&gt;</code> element. This HTML will then end up being displayed to other users, so I want to make sure that the site doesn't open people up to XSS attacks.</p> <p>Is there a nice library in Python that will c...
4
2009-12-06T08:51:41Z
1,854,817
<p>You could use <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a>. It allows you to traverse the markup structure fairly easily, even if it's not well-formed. I don't know that there's something made to order that works only on script tags.</p>
0
2009-12-06T08:57:53Z
[ "javascript", "python", "parsing", "xss" ]
Is there an Open Source Python library for sanitizing HTML and removing all Javascript?
1,854,806
<p>I want to write a web application that allows users to enter any HTML that can occur inside a <code>&lt;div&gt;</code> element. This HTML will then end up being displayed to other users, so I want to make sure that the site doesn't open people up to XSS attacks.</p> <p>Is there a nice library in Python that will c...
4
2009-12-06T08:51:41Z
1,854,818
<p>I would honestly look at using something like bbcode or some other alternative markup with it.</p>
0
2009-12-06T08:58:08Z
[ "javascript", "python", "parsing", "xss" ]
Is there an Open Source Python library for sanitizing HTML and removing all Javascript?
1,854,806
<p>I want to write a web application that allows users to enter any HTML that can occur inside a <code>&lt;div&gt;</code> element. This HTML will then end up being displayed to other users, so I want to make sure that the site doesn't open people up to XSS attacks.</p> <p>Is there a nice library in Python that will c...
4
2009-12-06T08:51:41Z
1,855,343
<p>As Klaus mentions, the clear consensus in the community is to use BeautifulSoup for these tasks:</p> <pre><code>soup = BeautifulSoup.BeautifulSoup(html) for script_elt in soup.findAll('script'): script_elt.extract() html = str(soup) </code></pre>
5
2009-12-06T13:03:37Z
[ "javascript", "python", "parsing", "xss" ]
Is there an Open Source Python library for sanitizing HTML and removing all Javascript?
1,854,806
<p>I want to write a web application that allows users to enter any HTML that can occur inside a <code>&lt;div&gt;</code> element. This HTML will then end up being displayed to other users, so I want to make sure that the site doesn't open people up to XSS attacks.</p> <p>Is there a nice library in Python that will c...
4
2009-12-06T08:51:41Z
1,855,818
<p>Whitelist approach to allowed tags, attributes and their values is the only reliable way. Take a look at <a href="http://code.activestate.com/recipes/496942/" rel="nofollow">Recipe 496942: Cross-site scripting (XSS) defense</a></p> <p>What is wrong with existing markup languages such as used on this very site?</p>
4
2009-12-06T16:33:27Z
[ "javascript", "python", "parsing", "xss" ]
Is there an Open Source Python library for sanitizing HTML and removing all Javascript?
1,854,806
<p>I want to write a web application that allows users to enter any HTML that can occur inside a <code>&lt;div&gt;</code> element. This HTML will then end up being displayed to other users, so I want to make sure that the site doesn't open people up to XSS attacks.</p> <p>Is there a nice library in Python that will c...
4
2009-12-06T08:51:41Z
1,857,575
<p>Eric,</p> <p>Have you thought about using a 'SAX' type parser for the HTML? I'm really not sure though that it would ignore the events properly though. It would also be a bit harder to construct than using something like Beautiful Soup. Handling syntax errors may be a problem with SAX as well. </p> <p>What I l...
0
2009-12-07T03:03:28Z
[ "javascript", "python", "parsing", "xss" ]
Google App Engine Application Extremely slow
1,854,821
<p>I created a Hello World website in Google App Engine. It is using Django 1.1 without any patch.</p> <p>Even though it is just a very simple web page, it takes long time and often it times out.</p> <p>Any suggestions to solve this?</p> <p>Note: It is responding fast after the first call.</p>
15
2009-12-06T08:58:43Z
1,854,829
<p>If it's responding quickly after the first request, it's probably just a case of getting the relevant process up and running. Admittedly it's slightly surprising that it takes so long that it times out. Is this after you've updated the application and verified that the AppEngine dashboard shows it as being ready?</p...
4
2009-12-06T09:05:12Z
[ "python", "django", "google-app-engine" ]
Google App Engine Application Extremely slow
1,854,821
<p>I created a Hello World website in Google App Engine. It is using Django 1.1 without any patch.</p> <p>Even though it is just a very simple web page, it takes long time and often it times out.</p> <p>Any suggestions to solve this?</p> <p>Note: It is responding fast after the first call.</p>
15
2009-12-06T08:58:43Z
1,854,875
<p>This is a horrible suggestion but I'll make it anyway:</p> <p>Build a little client application or just use <code>wget</code> with <code>cron</code> to periodically access your app, maybe once every 5 minutes or so. That should keep Google from putting it into a dormant state.</p> <p>I say this is a horrible sugge...
14
2009-12-06T09:29:52Z
[ "python", "django", "google-app-engine" ]
Google App Engine Application Extremely slow
1,854,821
<p>I created a Hello World website in Google App Engine. It is using Django 1.1 without any patch.</p> <p>Even though it is just a very simple web page, it takes long time and often it times out.</p> <p>Any suggestions to solve this?</p> <p>Note: It is responding fast after the first call.</p>
15
2009-12-06T08:58:43Z
1,856,432
<p>I encounteres the same with pylons based app. I have the initial page server as static, and have a dummy ajax call in it to bring the app up, before the user types in credentials. It is usually enough to avoid a lengthy response... Just an idea that you might use before you actually have a million users ;).</p>
3
2009-12-06T20:02:18Z
[ "python", "django", "google-app-engine" ]
Google App Engine Application Extremely slow
1,854,821
<p>I created a Hello World website in Google App Engine. It is using Django 1.1 without any patch.</p> <p>Even though it is just a very simple web page, it takes long time and often it times out.</p> <p>Any suggestions to solve this?</p> <p>Note: It is responding fast after the first call.</p>
15
2009-12-06T08:58:43Z
1,856,888
<p>One more tip which might increase the response time.</p> <p>Enabling billing does increase the quotas, and, to my personal experience, increase the overall response of an application as well. Probably because of the higher priority for billing-enabled applications google has. For instance, an app with billing disab...
4
2009-12-06T22:39:40Z
[ "python", "django", "google-app-engine" ]
Google App Engine Application Extremely slow
1,854,821
<p>I created a Hello World website in Google App Engine. It is using Django 1.1 without any patch.</p> <p>Even though it is just a very simple web page, it takes long time and often it times out.</p> <p>Any suggestions to solve this?</p> <p>Note: It is responding fast after the first call.</p>
15
2009-12-06T08:58:43Z
1,864,345
<p>To summarize <a href="http://groups.google.com/group/google-appengine/browse%5Fthread/thread/22692895421825cb">this thread</a> so far:</p> <ul> <li>Cold starts take a long time</li> <li>Google discourages pinging apps to keep them warm, but people do not know the alternative</li> <li>There is <a href="http://code.g...
7
2009-12-08T03:17:55Z
[ "python", "django", "google-app-engine" ]
Google App Engine Application Extremely slow
1,854,821
<p>I created a Hello World website in Google App Engine. It is using Django 1.1 without any patch.</p> <p>Even though it is just a very simple web page, it takes long time and often it times out.</p> <p>Any suggestions to solve this?</p> <p>Note: It is responding fast after the first call.</p>
15
2009-12-06T08:58:43Z
2,908,765
<p>I used pingdom for obvious reasons - no cold starts is a bonus. Of course the customers will soon come flocking and it will be a non-issue</p>
3
2010-05-25T21:45:42Z
[ "python", "django", "google-app-engine" ]
Google App Engine Application Extremely slow
1,854,821
<p>I created a Hello World website in Google App Engine. It is using Django 1.1 without any patch.</p> <p>Even though it is just a very simple web page, it takes long time and often it times out.</p> <p>Any suggestions to solve this?</p> <p>Note: It is responding fast after the first call.</p>
15
2009-12-06T08:58:43Z
5,017,635
<p>Now Google has added a payment option "Always On" which is 0.30$ a day.</p> <p>Using this feature, your application will not have to cold start any more.</p> <blockquote> <p>Always On</p> <p>While warmup requests help your application scale smoothly, they do not help if your application has very low a...
19
2011-02-16T14:29:37Z
[ "python", "django", "google-app-engine" ]
Google App Engine Application Extremely slow
1,854,821
<p>I created a Hello World website in Google App Engine. It is using Django 1.1 without any patch.</p> <p>Even though it is just a very simple web page, it takes long time and often it times out.</p> <p>Any suggestions to solve this?</p> <p>Note: It is responding fast after the first call.</p>
15
2009-12-06T08:58:43Z
23,840,818
<p>You may want to try <a href="http://www.thecloudup.com" rel="nofollow">CloudUp</a>. It pings your google apps periodically to keep them active. It's free and you can add as many apps as you want. It also supports azure and heroku.</p>
1
2014-05-24T02:43:51Z
[ "python", "django", "google-app-engine" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
1,855,107
<p>Using the base Python <a href="http://docs.python.org/library/zipfile.html" rel="nofollow">zipfile module</a>.</p>
0
2009-12-06T11:17:40Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
1,855,108
<p>You probably want to look at the <code>zipfile</code> module; there's documentation at <a href="http://docs.python.org/library/zipfile.html" rel="nofollow">http://docs.python.org/library/zipfile.html</a>.</p> <p>You may also want <code>os.walk()</code> to index the directory structure.</p>
1
2009-12-06T11:17:50Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
1,855,118
<p>As others have pointed out, you should use <a href="http://docs.python.org/library/zipfile.html">zipfile</a>. The documentation tells you what functions are available, but doesn't really explain how you can use them to zip an entire directory. I think it's easiest to explain with some example code:</p> <pre><code>#...
206
2009-12-06T11:23:55Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
1,855,122
<p>To add the contents of <code>mydirectory</code> to a new zip file, including all files and subdirectories:</p> <pre><code>import os import zipfile zf = zipfile.ZipFile("myzipfile.zip", "w") for dirname, subdirs, files in os.walk("mydirectory"): zf.write(dirname) for filename in files: zf.write(os.p...
45
2009-12-06T11:28:24Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
16,254,846
<p>For adding compression to the resulting zip file, check out <a href="http://neopatel.blogspot.ca/2010/07/creating-zip-file-in-python-and.html" rel="nofollow">this link</a>.</p> <p>You need to change:</p> <pre><code>zip = zipfile.ZipFile('Python.zip', 'w') </code></pre> <p>to </p> <pre><code>zip = zipfile.ZipFile...
8
2013-04-27T17:14:02Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
17,020,687
<p>I've made some changes to <a href="http://stackoverflow.com/a/1855118">code given by Mark Byers</a>. Below function will also adds empty directories if you have them. Examples should make it more clear what is the path added to the zip.</p> <pre><code>#!/usr/bin/env python import os import zipfile def addDirToZip(...
3
2013-06-10T09:28:26Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
17,080,988
<p>This function will recursively zip up a directory tree, <em>compressing</em> the files, and recording the correct relative filenames in the archive. The archive entries are the same as those generated by <code>zip -r output.zip source_dir</code>.</p> <pre><code>def make_zipfile(output_filename, source_dir): rel...
21
2013-06-13T06:57:03Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
25,048,491
<p>Here is a variation on the answer given by Nux that works for me:</p> <pre><code>def WriteDirectoryToZipFile( zipHandle, srcPath, zipLocalPath = "", zipOperation = zipfile.ZIP_DEFLATED ): basePath = os.path.split( srcPath )[ 0 ] for root, dirs, files in os.walk( srcPath ): p = os.path.join( zipLocal...
1
2014-07-30T23:13:20Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
25,650,295
<p>The easiest way is to use <a href="https://docs.python.org/2/library/shutil.html#shutil.make_archive">shutil.make_archive</a>. It supports both zip and tar formats.</p> <pre><code>import shutil shutil.make_archive(output_filename, 'zip', dir_name) </code></pre> <p>If you need to do something more complicated than ...
138
2014-09-03T17:26:41Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
29,821,859
<p><strong>Try the below one .it worked for me</strong>. </p> <pre><code>import zipfile, os zipf = "compress.zip" def main(): directory = r"Filepath" toZip(directory) def toZip(directory): zippedHelp = zipfile.ZipFile(zipf, "w", compression=zipfile.ZIP_DEFLATED ) list = os.listdir(directory) fo...
1
2015-04-23T11:18:16Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
32,904,890
<p>I have another code example that may help, using python3, pathlib and zipfile. It should work in any OS.</p> <pre><code>from pathlib import Path import zipfile from datetime import datetime DATE_FORMAT = '%y%m%d' def date_str(): """returns the today string year, month, day""" return '{}'.format(datetime....
1
2015-10-02T10:03:14Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
36,132,604
<p>If you want a functionality like the compress folder of any common graphical file manager you can use the following code, it uses the <a href="https://docs.python.org/2/library/zipfile.html" rel="nofollow">zipfile</a> module. Using this code you will have the zip file with the path as its root folder. </p> <pre><co...
0
2016-03-21T13:40:10Z
[ "python", "zipfile" ]
How to create a zip archive of a directory
1,855,095
<p>How can I create a zip archive of a directory structure in Python? </p>
137
2009-12-06T11:12:53Z
36,341,469
<blockquote> <h1>How can I create a zip archive of a directory structure in Python?</h1> </blockquote> <h2>In a Python script</h2> <p>In Python 2.7+, <code>shutil</code> has a <code>make_archive</code> function.</p> <pre><code>from shutil import make_archive make_archive( 'zipfile_name', 'zip', # th...
7
2016-03-31T18:57:20Z
[ "python", "zipfile" ]
Dealing with URLs in Django
1,855,184
<p>So, basically what I'm trying to do is a hockey pool application, and there are a ton of ways I should be able to filter to view the data. For example, filter by free agent, goals, assists, position, etc. </p> <p>I'm planning on doing this with a bunch of query strings, but I'm not sure what the best approach would...
0
2009-12-06T11:54:54Z
1,855,204
<p>Check out filter mechanism in the admin application, it includes dealing with dynamically constructed URLs with filter information supplied in the query string.</p> <p>In addition - consider saving actual state information in cookies/sessions.</p>
0
2009-12-06T12:04:12Z
[ "python", "django", "http", "request" ]
Dealing with URLs in Django
1,855,184
<p>So, basically what I'm trying to do is a hockey pool application, and there are a ton of ways I should be able to filter to view the data. For example, filter by free agent, goals, assists, position, etc. </p> <p>I'm planning on doing this with a bunch of query strings, but I'm not sure what the best approach would...
0
2009-12-06T11:54:54Z
1,855,215
<p>Have you looked at <a href="http://github.com/alex/django-filter" rel="nofollow">django-filter</a>? It's really awesome.</p>
1
2009-12-06T12:09:00Z
[ "python", "django", "http", "request" ]
Dealing with URLs in Django
1,855,184
<p>So, basically what I'm trying to do is a hockey pool application, and there are a ton of ways I should be able to filter to view the data. For example, filter by free agent, goals, assists, position, etc. </p> <p>I'm planning on doing this with a bunch of query strings, but I'm not sure what the best approach would...
0
2009-12-06T11:54:54Z
1,855,230
<p>Firstly, think about whether you really want to save <em>all</em> the parameters each time. In the example you give, you change the sort order but preserve the page number. Does this really make sense, considering you will now have different elements on that page. Even more, if you change the filters, the currently ...
2
2009-12-06T12:13:17Z
[ "python", "django", "http", "request" ]
Dealing with URLs in Django
1,855,184
<p>So, basically what I'm trying to do is a hockey pool application, and there are a ton of ways I should be able to filter to view the data. For example, filter by free agent, goals, assists, position, etc. </p> <p>I'm planning on doing this with a bunch of query strings, but I'm not sure what the best approach would...
0
2009-12-06T11:54:54Z
1,855,279
<p>If You want to save all the "parameters", I'd say they are resource identifiers and should normally be the part of URI.</p>
0
2009-12-06T12:32:00Z
[ "python", "django", "http", "request" ]
Django: PYTHON_EGG_CACHE, access denied error
1,855,219
<p>I am deploying my django application on a server, and on last stages I am getting this error:</p> <pre><code>ExtractionError at /admin/ Can't extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: '/.python-eggs' The Pytho...
2
2009-12-06T12:09:47Z
1,855,276
<p>Well, you are using some strange setuptools-enabled library.</p> <p>But anyway, is there a problem for you to setup PYTHON_EGG_CACHE environment variable to any directory writable for application user?</p>
6
2009-12-06T12:30:06Z
[ "python", "django", "python-egg-cache" ]
What is the best way to call Java code from Python?
1,855,320
<p>I have a Java class library (3rd party, proprietary) and I want my python script to call its functions. I already have java code that uses this library. What is the best way to achieve this?</p>
11
2009-12-06T12:54:15Z
1,855,323
<p>Can you run your current Python scripts under <a href="http://www.jython.org">Jython</a> ? If so, that's probably the best way, since the Java library can be exposed directly into Jython as scriptable objects.</p> <p>Failing that, there are a number of solutions listed <a href="http://wiki.cacr.caltech.edu/danse/in...
14
2009-12-06T12:55:44Z
[ "java", "python" ]
What is the best way to call Java code from Python?
1,855,320
<p>I have a Java class library (3rd party, proprietary) and I want my python script to call its functions. I already have java code that uses this library. What is the best way to achieve this?</p>
11
2009-12-06T12:54:15Z
1,855,374
<p>The other answer is <a href="http://jpype.sourceforge.net/" rel="nofollow">JPype</a>, which allows CPython to talk to Java. It's useful if you can't switch to Jython.</p>
8
2009-12-06T13:19:04Z
[ "java", "python" ]
What is the best way to call Java code from Python?
1,855,320
<p>I have a Java class library (3rd party, proprietary) and I want my python script to call its functions. I already have java code that uses this library. What is the best way to achieve this?</p>
11
2009-12-06T12:54:15Z
1,856,651
<p>There's also <a href="http://lucene.apache.org/pylucene/jcc/index.html" rel="nofollow">JCC</a> which is not mentioned on the Caltech page.</p> <p>Quote:</p> <blockquote> <p>JCC is a C++ code generator that produces a C++ object interface wrapping a Java library via Java's Native Interface (JNI). JCC also ...
3
2009-12-06T21:22:26Z
[ "java", "python" ]
Converting a Tuples List into a nested List using Python
1,855,471
<p>I want to convert a tuples list into a nested list using Python. How do I do that?</p> <p>I have a sorted list of tuples (sorted by the second value):</p> <pre><code>[(1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (2, 1)] </code></pre> <p>Now I want it to h...
3
2009-12-06T14:04:32Z
1,855,498
<p>Don't know how fast this will be for bigger sets, but you could do something like that:</p> <pre><code>input = [ (1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (2, 1) ] output = [[] for _ in xrange(input[0][1])] for value, key in input: outp...
0
2009-12-06T14:13:44Z
[ "python", "list", "nested", "tuples" ]
Converting a Tuples List into a nested List using Python
1,855,471
<p>I want to convert a tuples list into a nested list using Python. How do I do that?</p> <p>I have a sorted list of tuples (sorted by the second value):</p> <pre><code>[(1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (2, 1)] </code></pre> <p>Now I want it to h...
3
2009-12-06T14:04:32Z
1,855,502
<pre><code>from operator import itemgetter from itertools import groupby lst = [(1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (2, 1)] result = [[x for x, y in group] for key, group in groupby(lst, key=itemgetter(1))] </code></pre> <p><code>grou...
11
2009-12-06T14:16:01Z
[ "python", "list", "nested", "tuples" ]
Converting a Tuples List into a nested List using Python
1,855,471
<p>I want to convert a tuples list into a nested list using Python. How do I do that?</p> <p>I have a sorted list of tuples (sorted by the second value):</p> <pre><code>[(1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (2, 1)] </code></pre> <p>Now I want it to h...
3
2009-12-06T14:04:32Z
1,855,504
<p>It is a bit convoluted, but you can do it with the itertools.groupby function:</p> <pre><code>&gt;&gt;&gt; lst = [(1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (2, 1)] &gt;&gt;&gt; from operator import itemgetter &gt;&gt;&gt; import itertools &gt;&gt;&gt; [...
2
2009-12-06T14:16:16Z
[ "python", "list", "nested", "tuples" ]
Converting a Tuples List into a nested List using Python
1,855,471
<p>I want to convert a tuples list into a nested list using Python. How do I do that?</p> <p>I have a sorted list of tuples (sorted by the second value):</p> <pre><code>[(1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (2, 1)] </code></pre> <p>Now I want it to h...
3
2009-12-06T14:04:32Z
1,855,508
<p>Maybe not the most pythonesque answer, but this works:</p> <pre><code>d = {} a = [(1,5), (5,4), (13,3), (4,3), (3,2), (14,1), (12,1)] for value in a: if value[0] not in d: d[ value[0] ] = [] d[ value[0] ].append( a[1] ) print d.values() </code></pre>
1
2009-12-06T14:17:18Z
[ "python", "list", "nested", "tuples" ]
Converting a Tuples List into a nested List using Python
1,855,471
<p>I want to convert a tuples list into a nested list using Python. How do I do that?</p> <p>I have a sorted list of tuples (sorted by the second value):</p> <pre><code>[(1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (12, 1), (10, 1), (9, 1), (8, 1), (7, 1), (6, 1), (2, 1)] </code></pre> <p>Now I want it to h...
3
2009-12-06T14:04:32Z
1,855,516
<p>The simple solution:</p> <pre><code>n_list = [] c_snd = None for (fst, snd) in o_list: if snd == c_snd: n_list[-1].append(fst) else: c_snd = snd n_list.append([fst]) </code></pre> <p>Explanation: use <code>c_snd</code> to store the current second part of the tuple. If that changes, start a new list in ...
1
2009-12-06T14:22:00Z
[ "python", "list", "nested", "tuples" ]
how to access sys.argv (or any string variable) in raw mode?
1,855,477
<p>I'm having difficulties parsing filepaths sent as arguments:</p> <p>If I type:</p> <pre><code>os.path.normpath('D:\Data2\090925') </code></pre> <p>I get</p> <pre><code>'D:\\Data2\x0090925' </code></pre> <p>Obviously the \0 in the folder name is upsetting the formatting. I can correct it with the following:</p> ...
1
2009-12-06T14:05:46Z
1,855,539
<p>"Losing" the last part of your path is nothing to do with escaping (or lack of it) in <code>sys.argv</code>.</p> <p>It is the expected behaviour if you use <code>os.path.normpath()</code> and then <code>os.path.dirname()</code>.</p> <pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; os.path.normpath("c:/foo/bar/") 'c:...
5
2009-12-06T14:32:51Z
[ "python", "string-literals" ]
how to access sys.argv (or any string variable) in raw mode?
1,855,477
<p>I'm having difficulties parsing filepaths sent as arguments:</p> <p>If I type:</p> <pre><code>os.path.normpath('D:\Data2\090925') </code></pre> <p>I get</p> <pre><code>'D:\\Data2\x0090925' </code></pre> <p>Obviously the \0 in the folder name is upsetting the formatting. I can correct it with the following:</p> ...
1
2009-12-06T14:05:46Z
1,856,376
<p>Here is a snippet of Python code to add a backslash to the end of the directory path:</p> <pre><code>def add_path_slash(s_path): if not s_path: return s_path if 2 == len(s_path) and ':' == s_path[1]: return s_path # it is just a drive letter if s_path[-1] in ('/', '\\'): return ...
0
2009-12-06T19:42:57Z
[ "python", "string-literals" ]
Call method from string
1,855,558
<p>If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it:</p> <pre><code>class CallMe: # Class def App(): # Method one ... def Foo(): # Method two ... variable = "App" # Method to call CallMe.variable() #...
17
2009-12-06T14:41:13Z
1,855,575
<p>You can do this:</p> <pre><code>getattr(CallMe, variable)() </code></pre> <p><a href="http://docs.python.org/library/functions.html#getattr">getattr</a> is a builtin method, it returns the value of the named attributed of object. The value in this case is a method object that you can call with ()</p>
50
2009-12-06T14:46:35Z
[ "python", "oop" ]
Call method from string
1,855,558
<p>If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it:</p> <pre><code>class CallMe: # Class def App(): # Method one ... def Foo(): # Method two ... variable = "App" # Method to call CallMe.variable() #...
17
2009-12-06T14:41:13Z
1,855,592
<p>Your code does not look like python, may be you want to do like this?</p> <pre><code>class CallMe: def App(self): #// Method one print "hello" def Foo(self): #// Method two return None variable = App #// Method to call CallMe().variable() #// Calling App() </code></pre>
3
2009-12-06T14:54:49Z
[ "python", "oop" ]
Call method from string
1,855,558
<p>If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it:</p> <pre><code>class CallMe: # Class def App(): # Method one ... def Foo(): # Method two ... variable = "App" # Method to call CallMe.variable() #...
17
2009-12-06T14:41:13Z
1,855,772
<p>You can use getattr, or you can assign bound or unbound methods to the variable. Bound methods are tied to a particular instance of the class, and unbound methods are tied to the class, so you have to pass an instance in as the first parameter.</p> <p>e.g.</p> <pre><code>class CallMe: def App(self): p...
1
2009-12-06T16:08:08Z
[ "python", "oop" ]
Call method from string
1,855,558
<p>If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it:</p> <pre><code>class CallMe: # Class def App(): # Method one ... def Foo(): # Method two ... variable = "App" # Method to call CallMe.variable() #...
17
2009-12-06T14:41:13Z
1,856,344
<p>Your class has been declared as an "old-style class". I recommend you make all your classes be "new-style classes".</p> <p>The difference between the old and the new is that new-style classes can use inheritance, which you might not need right away. But it's a good habit to get into.</p> <p>Here is all you have ...
1
2009-12-06T19:33:49Z
[ "python", "oop" ]
How do I get the remote user agent inside a Genshi template when using Trac, and WSGI?
1,855,576
<p>I'm trying to do some customization of a Trac project management website and have run into an interesting problem. The project has a set of images that are both SVG and PNG. The SVG images have numerous advantages including multiple hyperlinks and a smaller transmitted size against PNG which is bigger and can only...
1
2009-12-06T14:46:37Z
1,855,643
<pre><code>user_agent = environ.get('HTTP_USER_AGENT', None) </code></pre> <p>Or if <code>environ</code> is wrapped in some sort of <code>Request</code> object:</p> <pre><code>user_agent = request.user_agent </code></pre> <p>btw, You should probably look at <code>HTTP_ACCEPT</code> header instead of <code>HTTP_USER_...
0
2009-12-06T15:09:49Z
[ "python", "trac", "wsgi", "genshi" ]
How do I get the remote user agent inside a Genshi template when using Trac, and WSGI?
1,855,576
<p>I'm trying to do some customization of a Trac project management website and have run into an interesting problem. The project has a set of images that are both SVG and PNG. The SVG images have numerous advantages including multiple hyperlinks and a smaller transmitted size against PNG which is bigger and can only...
1
2009-12-06T14:46:37Z
1,860,228
<p>Okay, so I did some digging on the issue, not by grepping through the source code, but by writing a custom Genshi handler that spit out the recursive <code>repr()</code> of every element in locals (with help provided by a <a href="http://stackoverflow.com/questions/1050452/">previous question</a> that addressed how ...
0
2009-12-07T14:16:28Z
[ "python", "trac", "wsgi", "genshi" ]
Dynamically Refreshed Pages produced by Python
1,855,748
<p>I've been researching this on and off for a number of months now, but I am incapable of finding clear direction. </p> <p>My goal is to have a page which has a form on it and a graph on it. The form can be filled out and then sent to the CGI Python script (yeah, I'll move to WSGI or fast_cgi later, I'm starting simp...
2
2009-12-06T15:59:42Z
1,855,787
<p>Update <code>img.src</code> attribute in <code>onsubmit()</code> handler.</p> <ul> <li><code>img.src</code> url points to your Python script that should generate an image in response.</li> <li><code>onsubmit()</code> for your form could be registered and written using JQuery. </li> </ul>
0
2009-12-06T16:17:59Z
[ "javascript", "python", "ajax" ]
Dynamically Refreshed Pages produced by Python
1,855,748
<p>I've been researching this on and off for a number of months now, but I am incapable of finding clear direction. </p> <p>My goal is to have a page which has a form on it and a graph on it. The form can be filled out and then sent to the CGI Python script (yeah, I'll move to WSGI or fast_cgi later, I'm starting simp...
2
2009-12-06T15:59:42Z
1,855,823
<p>If you'll be loading HTML and Javascript that needs to be executed, and your only reason for not wanting to load a new page is to preserve the surrounding elements, you could probably just stick the form in an IFRAME. When the form is POSTed, only the contents of the IFRAME are replaced with the new contents. No A...
2
2009-12-06T16:35:20Z
[ "javascript", "python", "ajax" ]
Dynamically Refreshed Pages produced by Python
1,855,748
<p>I've been researching this on and off for a number of months now, but I am incapable of finding clear direction. </p> <p>My goal is to have a page which has a form on it and a graph on it. The form can be filled out and then sent to the CGI Python script (yeah, I'll move to WSGI or fast_cgi later, I'm starting simp...
2
2009-12-06T15:59:42Z
1,855,836
<p>I'm assuming that you have two pages at the moment - a page which shows the form, and a page which receives the POST request and displays the graph.</p> <p>Will a little jQuery you can do exactly what you want.</p> <p>First add to your form page an empty div with <code>id="results"</code>. Next in your graph plot...
4
2009-12-06T16:40:48Z
[ "javascript", "python", "ajax" ]
Dynamically Refreshed Pages produced by Python
1,855,748
<p>I've been researching this on and off for a number of months now, but I am incapable of finding clear direction. </p> <p>My goal is to have a page which has a form on it and a graph on it. The form can be filled out and then sent to the CGI Python script (yeah, I'll move to WSGI or fast_cgi later, I'm starting simp...
2
2009-12-06T15:59:42Z
1,855,854
<blockquote> <p>I'd like the form to be able to send multiple times, so the user can update the graph, but I don't want the page to reload every time it doe that.</p> </blockquote> <p>The general pattern goes like that:</p> <ol> <li>Generate an XMLHttpRequest (in form's onsubmit or it's 'submit' button onclick hand...
1
2009-12-06T16:52:06Z
[ "javascript", "python", "ajax" ]
How to find/replace text in html while preserving html tags/structure
1,856,014
<p>I use regexps to transform text as I want, but I want to preserve the HTML tags. e.g. if I want to replace "stack overflow" with "stack underflow", this should work as expected: if the input is <code>stack &lt;sometag&gt;overflow&lt;/sometag&gt;</code>, I must obtain <code>stack &lt;sometag&gt;underflow&lt;/sometag...
6
2009-12-06T17:44:58Z
1,856,019
<p>Use a DOM library, not regular expressions, when dealing with manipulating HTML:</p> <ul> <li>lxml: a parser, document, and HTML serializer. Also can use BeautifulSoup and html5lib for parsing.</li> <li>BeautifulSoup: a parser, document, and HTML serializer.</li> <li>html5lib: a parser. It has a serializer.</li> <l...
9
2009-12-06T17:46:11Z
[ "python", "html", "html-parsing" ]
How to find/replace text in html while preserving html tags/structure
1,856,014
<p>I use regexps to transform text as I want, but I want to preserve the HTML tags. e.g. if I want to replace "stack overflow" with "stack underflow", this should work as expected: if the input is <code>stack &lt;sometag&gt;overflow&lt;/sometag&gt;</code>, I must obtain <code>stack &lt;sometag&gt;underflow&lt;/sometag...
6
2009-12-06T17:44:58Z
1,856,020
<p><a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a> or <a href="http://docs.python.org/library/htmllib.html" rel="nofollow">HTMLParser</a> is your answer.</p>
3
2009-12-06T17:46:16Z
[ "python", "html", "html-parsing" ]
How to find/replace text in html while preserving html tags/structure
1,856,014
<p>I use regexps to transform text as I want, but I want to preserve the HTML tags. e.g. if I want to replace "stack overflow" with "stack underflow", this should work as expected: if the input is <code>stack &lt;sometag&gt;overflow&lt;/sometag&gt;</code>, I must obtain <code>stack &lt;sometag&gt;underflow&lt;/sometag...
6
2009-12-06T17:44:58Z
1,856,050
<p>Use html parser such as provided by <a href="http://codespeak.net/lxml/" rel="nofollow"><code>lxml</code></a> or <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow"><code>BeautifulSoup</code></a>. Another option is to use XSLT transformations (<a href="http://jython.xhaus.com/transforming-with-xsl...
1
2009-12-06T17:56:00Z
[ "python", "html", "html-parsing" ]
How to find/replace text in html while preserving html tags/structure
1,856,014
<p>I use regexps to transform text as I want, but I want to preserve the HTML tags. e.g. if I want to replace "stack overflow" with "stack underflow", this should work as expected: if the input is <code>stack &lt;sometag&gt;overflow&lt;/sometag&gt;</code>, I must obtain <code>stack &lt;sometag&gt;underflow&lt;/sometag...
6
2009-12-06T17:44:58Z
1,856,571
<p>I don't think that the DOM / HTML parser library recommendations posted so far address the specific problem in the given example: <code>overflow</code> should replaced with <code>underflow</code> only when preceded by <code>stack</code> in the rendered document, whether or not there are tags between them. Such a lib...
0
2009-12-06T20:52:46Z
[ "python", "html", "html-parsing" ]
How to find/replace text in html while preserving html tags/structure
1,856,014
<p>I use regexps to transform text as I want, but I want to preserve the HTML tags. e.g. if I want to replace "stack overflow" with "stack underflow", this should work as expected: if the input is <code>stack &lt;sometag&gt;overflow&lt;/sometag&gt;</code>, I must obtain <code>stack &lt;sometag&gt;underflow&lt;/sometag...
6
2009-12-06T17:44:58Z
1,856,634
<p>Note that arbitrary replacements can't be done unambiguously. Consider the following examples:</p> <h3>1)</h3> <p>HTML:</p> <pre><code>A&lt;tag&gt;B&lt;/tag&gt; </code></pre> <p>Pattern -> replacement:</p> <pre><code>AB -&gt; AXB </code></pre> <p>Possible results:</p> <pre><code>AX&lt;tag&gt;B&lt;/tag&gt; A&l...
2
2009-12-06T21:14:12Z
[ "python", "html", "html-parsing" ]
How to find/replace text in html while preserving html tags/structure
1,856,014
<p>I use regexps to transform text as I want, but I want to preserve the HTML tags. e.g. if I want to replace "stack overflow" with "stack underflow", this should work as expected: if the input is <code>stack &lt;sometag&gt;overflow&lt;/sometag&gt;</code>, I must obtain <code>stack &lt;sometag&gt;underflow&lt;/sometag...
6
2009-12-06T17:44:58Z
2,839,532
<p>Fun stuff to try. It sorta works. My friends like it when I attach this script to a textarea and let them "translate" things. I guess you could use it for anything really. Meh. Check the code over a few times if you're going to use it, it works but I'm new to all this. I think it's been 2 or three weeks since I star...
-1
2010-05-15T08:55:58Z
[ "python", "html", "html-parsing" ]
Python module database configuration?
1,856,356
<p>I have a python module which contains a few objects, one of which uses a MySQL connection to persist some data. What's the best way to allow for easy configuration of the MySQL connection information without making the user go into the installed module location and edit files?</p>
2
2009-12-06T19:37:06Z
1,856,378
<p>You could pull the configuration details for MySQL from an <code>.ini</code> file using <a href="http://docs.python.org/library/configparser.html" rel="nofollow">ConfigParser</a>.</p>
2
2009-12-06T19:43:28Z
[ "python" ]
Python module database configuration?
1,856,356
<p>I have a python module which contains a few objects, one of which uses a MySQL connection to persist some data. What's the best way to allow for easy configuration of the MySQL connection information without making the user go into the installed module location and edit files?</p>
2
2009-12-06T19:37:06Z
1,856,381
<p>Allow the user to write configuration information for the program in a file of the format that <a href="http://docs.python.org/library/configparser.html?highlight=config#ConfigParser.ConfigParser" rel="nofollow">ConfigParser</a> knows how to parse -- this way, the user doesn't have to "go into the installed module l...
4
2009-12-06T19:45:15Z
[ "python" ]
Python popen wont work with block devices
1,856,372
<p>I am wring a small forensic app which looks like this so far</p> <pre><code>import time, os,sys def getter(): filename = sys.argv[1] print "File Entered: " + filename os.system('file ' + filename) print "\n" pipe = os.popen("xxd " + filename, "r") print pipe.read() </code></pre> <p>I inpu...
1
2009-12-06T19:42:21Z
1,856,382
<p>The problem probably lies with how you call <code>xxd</code>, given that your application doesn't do anything at all with the file.</p>
1
2009-12-06T19:45:20Z
[ "python", "pipe", "subprocess" ]
Python popen wont work with block devices
1,856,372
<p>I am wring a small forensic app which looks like this so far</p> <pre><code>import time, os,sys def getter(): filename = sys.argv[1] print "File Entered: " + filename os.system('file ' + filename) print "\n" pipe = os.popen("xxd " + filename, "r") print pipe.read() </code></pre> <p>I inpu...
1
2009-12-06T19:42:21Z
1,856,393
<p>What happens if you just run <code>xxd /dev/diskwhatever</code> from the shell prompt? Does it work, and how much information does it spew out? Assuming that, as superuser, you do have read permission, the attempt to read everything at one gulp in your code's last line would be the point where failure could be exp...
3
2009-12-06T19:49:50Z
[ "python", "pipe", "subprocess" ]
Python popen wont work with block devices
1,856,372
<p>I am wring a small forensic app which looks like this so far</p> <pre><code>import time, os,sys def getter(): filename = sys.argv[1] print "File Entered: " + filename os.system('file ' + filename) print "\n" pipe = os.popen("xxd " + filename, "r") print pipe.read() </code></pre> <p>I inpu...
1
2009-12-06T19:42:21Z
1,856,410
<p>I assume you've tried the same command from the shell prompt and it worked well, even when piped to <code>less</code> or something of the sort.</p> <p>I have a strong feeling that using <a href="http://docs.python.org/library/subprocess.html" rel="nofollow"><code>subprocess</code></a> will fix this behavior; This m...
4
2009-12-06T19:55:09Z
[ "python", "pipe", "subprocess" ]
SyntaxError inconsistency in Python?
1,856,408
<p>Consider these two snippets:</p> <pre><code>try: a+a=a except SyntaxError: print "first exception caught" </code></pre> <p>.</p> <pre><code>try: eval("a+a=a") except SyntaxError: print "second exception caught" </code></pre> <p>In the second case the "second exception .." statement is printed (ex...
8
2009-12-06T19:54:43Z
1,856,431
<p>In the first case, the exception is raised by the compiler, which is running <em>before</em> the <code>try/except</code> structure even <em>exists</em> (since it's the compiler itself that will set it up right after parsing). In the second case, the compiler is running twice -- and the exception is getting raised w...
14
2009-12-06T20:02:04Z
[ "python", "exception" ]
SyntaxError inconsistency in Python?
1,856,408
<p>Consider these two snippets:</p> <pre><code>try: a+a=a except SyntaxError: print "first exception caught" </code></pre> <p>.</p> <pre><code>try: eval("a+a=a") except SyntaxError: print "second exception caught" </code></pre> <p>In the second case the "second exception .." statement is printed (ex...
8
2009-12-06T19:54:43Z
1,856,434
<p>Short answer: No.</p> <p>Syntax errors happen when the code is parsed, which for normal Python code is before the code is executed - the code is not executing inside the try/except block since the code is not executing, period.</p> <p>However when you eval or exec some code, then you are parsing it at runtime, so ...
2
2009-12-06T20:03:01Z
[ "python", "exception" ]
Defining PYTHONPATH for http requests on a shared server
1,856,439
<p>I'm installing Django on Bluehost and one of the steps to install it was to install flup on their server. I did so and everything works great when I'm logged in via the SSH. However when I actually hit the page in my browser it can't find flup. I get this error in the server log:</p> <blockquote> <p>ERROR: No mod...
1
2009-12-06T20:03:52Z
1,856,448
<p>If you can identify what module exactly is trying to <code>import flup</code>, you can prepend that import with a <code>sys.path.append</code> of the path to which you have installed <code>flup</code> -- as long as the <code>sys.path.append</code> happens <em>before</em> the <code>import flup</code>, you're in clove...
2
2009-12-06T20:08:14Z
[ "python", "django", "pythonpath", "bluehost", "flup" ]
python can't remove a file after closing it, "being used by another process"
1,856,718
<p>I am trying to remove a file after reading from it, but getting "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"</p> <pre><code>file = open(self.filePath) for line in file: #do things file.close() os.remove(self.filePath) #throws error os.rename(sel...
13
2009-12-06T21:42:43Z
1,856,746
<p>There are lots of possible reasons for this. However, the most likely one is that you have a directory window open that contains the file, or that such a window was closed recently and Windows has yet to flush the directory from its cache. Try closing any directory window that you were looking in and try again.</p>
0
2009-12-06T21:54:57Z
[ "python" ]
python can't remove a file after closing it, "being used by another process"
1,856,718
<p>I am trying to remove a file after reading from it, but getting "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"</p> <pre><code>file = open(self.filePath) for line in file: #do things file.close() os.remove(self.filePath) #throws error os.rename(sel...
13
2009-12-06T21:42:43Z
1,856,787
<p>Another <em>possibility</em> is that a virus checker still has the file open at the time you try to delete or rename it. This doesn't happen often but when it does, it's really annoying to track down.</p>
8
2009-12-06T22:10:36Z
[ "python" ]
python can't remove a file after closing it, "being used by another process"
1,856,718
<p>I am trying to remove a file after reading from it, but getting "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"</p> <pre><code>file = open(self.filePath) for line in file: #do things file.close() os.remove(self.filePath) #throws error os.rename(sel...
13
2009-12-06T21:42:43Z
1,856,857
<p>Use <a href="http://www.emptyloop.com/unlocker/">Unlocker</a>(easier) or <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">Process Explorer</a> to see what program has the file open.</p> <p><em>(as suggested by <a href="http://stackoverflow.com/users/224346/anon">Anon</a>)</em></p>
6
2009-12-06T22:29:44Z
[ "python" ]
python can't remove a file after closing it, "being used by another process"
1,856,718
<p>I am trying to remove a file after reading from it, but getting "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"</p> <pre><code>file = open(self.filePath) for line in file: #do things file.close() os.remove(self.filePath) #throws error os.rename(sel...
13
2009-12-06T21:42:43Z
17,325,764
<p>I have the same problem right now with an access-AB. Process Explorer shows that .. 'python.exe' is using the file that I which to delete. I can not kill Python. It is an annoying solution.</p>
1
2013-06-26T16:37:34Z
[ "python" ]
Proper way to implement a Direct Connect client in Twisted?
1,856,786
<p>I'm working on writing a Python client for Direct Connect P2P networks. Essentially, it works by connecting to a central server, and responding to other users who are searching for files.</p> <p>Occasionally, another client will ask us to connect to them, and they might begin downloading a file from us. This is a d...
3
2009-12-06T22:10:29Z
1,857,145
<p>Without knowing all the details of the protocol, I would still recommend using a single reactor -- a reactor scales <em>quite</em> well (especially advanced ones such as <code>PollReactor</code>) and this way you will avoid the overhead connected with threads (that's how Twisted and other async systems get their fun...
3
2009-12-07T00:31:37Z
[ "python", "twisted", "p2p" ]
Intelligently launching the default editor from inside a Python CLI program?
1,856,792
<p>The answers in <a href="http://stackoverflow.com/questions/1442841">this question</a> didn't get to the heart of the problem. In a CLI-based Python program, I want the user to be able to edit a file and then return to the program. Before returning, I want them to be able to cancel their edits. This should feel like ...
5
2009-12-06T22:11:37Z
1,856,908
<p>Subversion, et al use the $EDITOR environment variable to determine which program to use to edit text files. Of course, $EDITOR will only work if you're on a unixy platform in a shell. You'll have to do something different for Windows (<code>cmd /c start tempfile.txt</code>) or Mac OS X (<code>open tempfile.txt</cod...
2
2009-12-06T22:45:17Z
[ "python", "editor" ]
Intelligently launching the default editor from inside a Python CLI program?
1,856,792
<p>The answers in <a href="http://stackoverflow.com/questions/1442841">this question</a> didn't get to the heart of the problem. In a CLI-based Python program, I want the user to be able to edit a file and then return to the program. Before returning, I want them to be able to cancel their edits. This should feel like ...
5
2009-12-06T22:11:37Z
1,856,911
<p>You could try looking through the sources to Mercurial, which is written in Python.</p> <p>They use <code>os.environ</code> to read the value of environment variables <code>HGEDITOR</code>, <code>VISUAL</code>, and <code>EDITOR</code>, defaulting to vi. Then they use <code>os.system</code> to launch the editor on ...
10
2009-12-06T22:46:19Z
[ "python", "editor" ]
Urllib2 Send Post data through proxy
1,856,814
<p>I have configured a proxy using proxyhandler and sent a request with some POST data:</p> <pre><code>cookiejar = cookielib.CookieJar() proxies = {'http':'http://some-proxy:port/'} opener = urllib2.build_opener(urllib2.ProxyHandler(proxies),urllib2.HTTPCookieProcessor(cookiejar) ) opener.addheaders = [('User-agent',...
2
2009-12-06T22:18:46Z
1,856,914
<p>Auto-respond </p> <p>The problem was user-agent header...</p> <p>Sorry for the stupid question...</p>
1
2009-12-06T22:47:56Z
[ "python", "proxy", "urllib2" ]
Mac Based Python GUI Libraries
1,856,924
<p>I am currently building a GUI based Python application on my mac and was wondering could anyone suggest a good GUI library to use? </p> <p>I was looking at <a href="http://wiki.python.org/moin/GuiProgramming" rel="nofollow">python's gui programming faq</a> and there was a lot of options making it hard to choose.</p...
3
2009-12-06T22:52:03Z
1,856,930
<p>If you're not concerned about cross-platform compatibility, then <a href="http://pyobjc.sourceforge.net/" rel="nofollow">PyObjC</a> (also see <a href="http://developer.apple.com/cocoa/pyobjc.html" rel="nofollow">Apple's info about PyObjC</a>) provides a direct bridge to the native OS X Cocoa interfaces.</p> <blockq...
5
2009-12-06T22:54:17Z
[ "python", "osx", "user-interface" ]
Mac Based Python GUI Libraries
1,856,924
<p>I am currently building a GUI based Python application on my mac and was wondering could anyone suggest a good GUI library to use? </p> <p>I was looking at <a href="http://wiki.python.org/moin/GuiProgramming" rel="nofollow">python's gui programming faq</a> and there was a lot of options making it hard to choose.</p...
3
2009-12-06T22:52:03Z
1,856,949
<p><a href="http://www.wxpython.org/" rel="nofollow">wxPython</a> and Qt (via <a href="http://www.riverbankcomputing.co.uk/" rel="nofollow">PyQT</a> or <a href="http://www.pyside.org/" rel="nofollow">PySide</a>) provide native OS X widgets and work across all major platforms.</p>
5
2009-12-06T23:06:04Z
[ "python", "osx", "user-interface" ]
Mac Based Python GUI Libraries
1,856,924
<p>I am currently building a GUI based Python application on my mac and was wondering could anyone suggest a good GUI library to use? </p> <p>I was looking at <a href="http://wiki.python.org/moin/GuiProgramming" rel="nofollow">python's gui programming faq</a> and there was a lot of options making it hard to choose.</p...
3
2009-12-06T22:52:03Z
1,857,010
<p>There's a relatively new project active now called <a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python%5Fgui/" rel="nofollow">PyGUI</a> which aims to provide a more modern cross-platform GUI for Python apps. On OS X, it uses <code>PyObjC</code> to provide native GUI elements. It might be easier to get star...
3
2009-12-06T23:29:58Z
[ "python", "osx", "user-interface" ]
python soappy add header
1,856,963
<p>I have the following PHP example code:</p> <pre><code>$client = new SoapClient("http://example.com/example.wsdl"); $h = Array(); array_push($h, new SoapHeader("http://example2.com/example2/", "h", "v")); $client-&gt;__setSoapHeaders($h); $s = $client-&gt;__soapCall('Op', $data); </code></pre> <p>My question: wha...
1
2009-12-06T23:13:08Z
1,857,200
<p>Here's an example using <a href="https://fedorahosted.org/suds/wiki/Documentation#SOAPHEADERS" rel="nofollow">suds</a> library (an alternative to SOAPpy). It assumes that the custom header is not defined in the wsdl.</p> <pre><code>from suds.client import Client from suds.sax.element import Element client = ...
1
2009-12-07T00:52:40Z
[ "python", "soap", "header", "soappy" ]
Python attribute error: type object '_socketobject' has no attribute 'gethostbyname'
1,857,146
<p>I am trying to do this in my program:</p> <pre><code>dest = socket.gethostbyname(host) </code></pre> <p>I have included the line:</p> <pre><code>from socket import * </code></pre> <p>in the beginning of the file.</p> <p>I am getting this error:</p> <blockquote> <p>AttributeError: type object '_socketobjec...
7
2009-12-07T00:31:55Z
1,857,155
<p>After <code>from socket import *</code>, you'd need to call just the barename <code>gethostbyname</code> -- the barename <code>socket</code> now refers to a type, not to the module. That <code>import *</code> is horrible practice, by the way: do, instead, <code>import socket</code>, and <strong>then</strong> <code>...
2
2009-12-07T00:35:29Z
[ "python", "attributeerror", "gethostbyname" ]
Python attribute error: type object '_socketobject' has no attribute 'gethostbyname'
1,857,146
<p>I am trying to do this in my program:</p> <pre><code>dest = socket.gethostbyname(host) </code></pre> <p>I have included the line:</p> <pre><code>from socket import * </code></pre> <p>in the beginning of the file.</p> <p>I am getting this error:</p> <blockquote> <p>AttributeError: type object '_socketobjec...
7
2009-12-07T00:31:55Z
1,857,159
<p>You shoulod either use</p> <pre><code>import socket dest = socket.gethostbyname(host) </code></pre> <p>or use</p> <pre><code>from socket import * dest = gethostbyname(host) </code></pre> <p>Note: the first option is by far the recommended one.</p>
12
2009-12-07T00:36:44Z
[ "python", "attributeerror", "gethostbyname" ]
Send selected text to a command line argument
1,857,287
<p>I found this utility, <a href="http://py-translate.sourceforge.net" rel="nofollow" title="pytranslate">pytranslate</a>, which translates a variety of languages into each other using Google's translation API. It works exactly as described. </p> <p>However I've gotten sick of selecting a word I do not understand, the...
2
2009-12-07T01:25:15Z
1,857,315
<p><strong>Note</strong>: this answer was useless to the questioner, who wasn't using Windows. Given that the title doesn't specify the OS, I'll leave it around for Windows users who may come this way.</p> <p><hr></p> <p>You can easily whip one up yourself using the pywin32 package and the win32clipboard module. Se...
0
2009-12-07T01:36:02Z
[ "python", "terminal", "clipboard", "ubuntu-9.04", "google-translate" ]
Send selected text to a command line argument
1,857,287
<p>I found this utility, <a href="http://py-translate.sourceforge.net" rel="nofollow" title="pytranslate">pytranslate</a>, which translates a variety of languages into each other using Google's translation API. It works exactly as described. </p> <p>However I've gotten sick of selecting a word I do not understand, the...
2
2009-12-07T01:25:15Z
1,857,328
<p>Would you be able to use clipboard support in the <a href="http://library.gnome.org/devel/pygtk/stable/class-gtkclipboard.html" rel="nofollow">PyGTK</a> package to do the job? It claims to have access to the "primary" X clipboard which it says is where you'd normally find the selected text.</p>
1
2009-12-07T01:42:44Z
[ "python", "terminal", "clipboard", "ubuntu-9.04", "google-translate" ]
Send selected text to a command line argument
1,857,287
<p>I found this utility, <a href="http://py-translate.sourceforge.net" rel="nofollow" title="pytranslate">pytranslate</a>, which translates a variety of languages into each other using Google's translation API. It works exactly as described. </p> <p>However I've gotten sick of selecting a word I do not understand, the...
2
2009-12-07T01:25:15Z
1,857,330
<pre><code>#!/bin/bash pytranslate "$(xsel -p)" </code></pre> <p>Now just put this in <code>~/bin</code> (make sure that's included in your PATH), and run it. (You may need to install the xsel package.) It will take the current contents of the primary selection buffer and pass it to pytranslate.</p> <p>If you want ...
4
2009-12-07T01:43:12Z
[ "python", "terminal", "clipboard", "ubuntu-9.04", "google-translate" ]
Send selected text to a command line argument
1,857,287
<p>I found this utility, <a href="http://py-translate.sourceforge.net" rel="nofollow" title="pytranslate">pytranslate</a>, which translates a variety of languages into each other using Google's translation API. It works exactly as described. </p> <p>However I've gotten sick of selecting a word I do not understand, the...
2
2009-12-07T01:25:15Z
1,857,506
<p>Taking inspiration from Roger Pate's brilliant one liner I've created a simple looping script for pytranslate. This is currently provisional - as I haven't implemented error catching yet - wait for new edits.</p> <pre><code>#!/bin/bash # Primary Clipboard poller using xsel (middle click) and pytranslate # Checks f...
2
2009-12-07T02:42:31Z
[ "python", "terminal", "clipboard", "ubuntu-9.04", "google-translate" ]
python optparse, how to include additional info in usage output?
1,857,346
<p>Using python's optparse module I would like to add extra example lines below the regular usage output. My current help_print() output looks like this:</p> <pre><code>usage: check_dell.py [options] options: -h, --help show this help message and exit -s, --storage checks virtual and physical disks -c, --chassis...
23
2009-12-07T01:48:08Z
1,857,357
<p>Use the <code>usage</code> parameter:</p> <pre><code>usage = "usage: %prog [options] arg1 arg2" parser = OptionParser(usage=usage) </code></pre> <p>You can add more through (just an example):</p> <pre><code>group = OptionGroup(parser, "Dangerous Options", "Caution: use these options at your ow...
5
2009-12-07T01:51:22Z
[ "python", "optparse" ]
python optparse, how to include additional info in usage output?
1,857,346
<p>Using python's optparse module I would like to add extra example lines below the regular usage output. My current help_print() output looks like this:</p> <pre><code>usage: check_dell.py [options] options: -h, --help show this help message and exit -s, --storage checks virtual and physical disks -c, --chassis...
23
2009-12-07T01:48:08Z
1,857,366
<p>There is a <code>description</code> parameter you can pass to the <code>OptionParser</code> constructor. This allows you to include arbitrary text that appears after <code>usage</code>, but before the list of options.</p> <p>See <a href="http://docs.python.org/library/optparse#creating-the-parser" rel="nofollow">1...
2
2009-12-07T01:54:49Z
[ "python", "optparse" ]
python optparse, how to include additional info in usage output?
1,857,346
<p>Using python's optparse module I would like to add extra example lines below the regular usage output. My current help_print() output looks like this:</p> <pre><code>usage: check_dell.py [options] options: -h, --help show this help message and exit -s, --storage checks virtual and physical disks -c, --chassis...
23
2009-12-07T01:48:08Z
1,857,374
<pre><code>parser = optparse.OptionParser(epilog="otherstuff") </code></pre> <p>The default <code>format_epilog</code> strips the newlines (uses textwrap), so you would need to override <code>format_epilog</code> in your parser like this.</p> <pre><code>def main(): class MyParser(optparse.OptionParser): ...
38
2009-12-07T01:57:59Z
[ "python", "optparse" ]