title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
How do I find the location of my Python site-packages directory?
122,327
<p>How do I find the location of my site-packages directory?</p>
396
2008-09-23T17:04:43Z
9,155,056
<p>All the answers (or: the same answer repeated over and over) are inadequate. What you want to do is this:</p> <pre><code>from setuptools.command.easy_install import easy_install class easy_install_default(easy_install): """ class easy_install had problems with the fist parameter not being an instance of Dis...
13
2012-02-06T02:40:22Z
[ "python", "installation" ]
How do I find the location of my Python site-packages directory?
122,327
<p>How do I find the location of my site-packages directory?</p>
396
2008-09-23T17:04:43Z
9,946,505
<p>This works for me. It will get you both dist-packages and site-packages folders. If the folder is not on Python's path, it won't be doing you much good anyway.</p> <pre><code>import sys; print [f for f in sys.path if f.endswith('packages')] </code></pre> <p>Output (Ubuntu installation):</p> <pre><code>['/home/us...
5
2012-03-30T16:01:24Z
[ "python", "installation" ]
How do I find the location of my Python site-packages directory?
122,327
<p>How do I find the location of my site-packages directory?</p>
396
2008-09-23T17:04:43Z
10,694,208
<p>This is what worked for me:</p> <pre><code>python -m site --user-site </code></pre>
33
2012-05-22T00:08:55Z
[ "python", "installation" ]
How do I find the location of my Python site-packages directory?
122,327
<p>How do I find the location of my site-packages directory?</p>
396
2008-09-23T17:04:43Z
12,950,101
<pre><code>&gt;&gt;&gt; import site; site.getsitepackages() ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages'] </code></pre> <p>(or just first item with <code>site.getsitepackages()[0]</code>)</p>
404
2012-10-18T08:19:45Z
[ "python", "installation" ]
How do I find the location of my Python site-packages directory?
122,327
<p>How do I find the location of my site-packages directory?</p>
396
2008-09-23T17:04:43Z
18,130,809
<p>The native system packages installed with python installation can be found at :</p> <blockquote> <p>/usr/lib/python2.7/dist-packages/</p> </blockquote> <p>by using this small code :</p> <pre><code>from distutils.sysconfig import get_python_lib print get_python_lib() </code></pre> <p>However, the list of packag...
8
2013-08-08T16:05:17Z
[ "python", "installation" ]
How do I find the location of my Python site-packages directory?
122,327
<p>How do I find the location of my site-packages directory?</p>
396
2008-09-23T17:04:43Z
29,062,571
<p>This should work on all distributions in and out of virtual environment due to it's "low-tech" nature. The os module always resides in the parent directory of 'site-packages'</p> <pre><code>import os; print os.path.dirname(os.__file__) + '/site-packages' </code></pre> <p>To change dir to the site-packages dir I us...
3
2015-03-15T15:43:40Z
[ "python", "installation" ]
How do I find the location of my Python site-packages directory?
122,327
<p>How do I find the location of my site-packages directory?</p>
396
2008-09-23T17:04:43Z
38,432,782
<p>Answer to old question. But use ipython for this. </p> <pre><code>pip install ipython ipython import imaplib imaplib? </code></pre> <p>This will give the following output about imaplib package -</p> <pre><code>Type: module String form: &lt;module 'imaplib' from '/usr/lib/python2.7/imaplib.py'&gt; File: ...
1
2016-07-18T09:12:47Z
[ "python", "installation" ]
Is there an Eclipse add-on to build a python executable for distribution?
122,524
<p>I want to build an executable to distribute to people without python installed on their machines. </p> <p>Is there an add-on to Eclipse that allows this? I couldn't find one. </p> <p>If not, do you have a builder that you recommend that would make it easy to go to my python project directory created in Eclipse, an...
2
2008-09-23T17:39:32Z
122,590
<p>It's not eclipse, but ActiveState's <a href="http://docs.activestate.com/activepython/2.5/faq/windows/index.html#where-is-freeze-for-windows" rel="nofollow">ActivePython FAQ</a> mentions the freeze utility, which sounds like it might be close to what you're asking for.</p>
1
2008-09-23T17:50:47Z
[ "python", "eclipse", "bytecode" ]
Is there an Eclipse add-on to build a python executable for distribution?
122,524
<p>I want to build an executable to distribute to people without python installed on their machines. </p> <p>Is there an add-on to Eclipse that allows this? I couldn't find one. </p> <p>If not, do you have a builder that you recommend that would make it easy to go to my python project directory created in Eclipse, an...
2
2008-09-23T17:39:32Z
123,077
<p>For Windows, there's the <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a> project.</p> <p>There's <a href="http://systemexit.de/bbfreeze/README.html" rel="nofollow">bbfreeze</a>, and <a href="http://pyinstaller.python-hosting.com/" rel="nofollow">PyInstaller</a>, and <a href="http://www.undefined.org/pyth...
1
2008-09-23T19:04:16Z
[ "python", "eclipse", "bytecode" ]
Is there an Eclipse add-on to build a python executable for distribution?
122,524
<p>I want to build an executable to distribute to people without python installed on their machines. </p> <p>Is there an add-on to Eclipse that allows this? I couldn't find one. </p> <p>If not, do you have a builder that you recommend that would make it easy to go to my python project directory created in Eclipse, an...
2
2008-09-23T17:39:32Z
123,990
<p><a href="http://stackoverflow.com/questions/2933/an-executable-python-app">See</a> <a href="http://stackoverflow.com/questions/106725/how-to-bundle-a-python-application-including-dependencies-for-windows">these</a> <a href="http://stackoverflow.com/questions/116657/how-do-you-create-an-osx-applicationdmg-from-a-pyth...
1
2008-09-23T21:23:46Z
[ "python", "eclipse", "bytecode" ]
Is there an Eclipse add-on to build a python executable for distribution?
122,524
<p>I want to build an executable to distribute to people without python installed on their machines. </p> <p>Is there an add-on to Eclipse that allows this? I couldn't find one. </p> <p>If not, do you have a builder that you recommend that would make it easy to go to my python project directory created in Eclipse, an...
2
2008-09-23T17:39:32Z
12,812,574
<p>There is also PyInstaller: <a href="http://www.pyinstaller.org/" rel="nofollow">http://www.pyinstaller.org/</a></p>
0
2012-10-10T05:34:45Z
[ "python", "eclipse", "bytecode" ]
Need to create a layered dict from a flat one
122,763
<p>I have a dict, that looks like this:</p> <pre><code>{ 'foo': { 'opt1': 1, 'opt2': 2, }, 'foo/bar': { 'opt3': 3, 'opt4': 4, }, 'foo/bar/baz': { 'opt5': 5, 'opt6': 6, } } </code></pre> <p>And I need to get it to look like:</p> <...
6
2008-09-23T18:19:25Z
122,785
<p>Like this:</p> <pre><code>def nest(d): rv = {} for key, value in d.iteritems(): node = rv for part in key.split('/'): node = node.setdefault(part, {}) node.update(value) return rv </code></pre>
8
2008-09-23T18:23:31Z
[ "python", "algorithm", "performance" ]
Need to create a layered dict from a flat one
122,763
<p>I have a dict, that looks like this:</p> <pre><code>{ 'foo': { 'opt1': 1, 'opt2': 2, }, 'foo/bar': { 'opt3': 3, 'opt4': 4, }, 'foo/bar/baz': { 'opt5': 5, 'opt6': 6, } } </code></pre> <p>And I need to get it to look like:</p> <...
6
2008-09-23T18:19:25Z
122,812
<pre><code>def layer(dict): for k,v in dict: if '/' in k: del dict[k] subdict = dict.get(k[:k.find('/')],{}) subdict[k[k.find('/')+1:]] = v layer(subdict) </code></pre>
1
2008-09-23T18:25:51Z
[ "python", "algorithm", "performance" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
123,212
<p><a href="http://docs.python.org/2/library/shutil.html"><code>shutil</code></a> has many methods you can use. One of which is:</p> <pre><code>from shutil import copyfile copyfile(src, dst) </code></pre> <p>Copy the contents of the file named <code>src</code> to a file named <code>dst</code>. The destination locati...
964
2008-09-23T19:25:35Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
123,217
<p><a href="https://docs.python.org/library/shutil.html" rel="nofollow">shutil</a> may have what you’re looking for.</p>
6
2008-09-23T19:25:51Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
123,225
<p>Look at module <strong>shutil</strong>. It contains function <strong>copyfile</strong>(<em>src</em>, <em>dst</em>) </p>
9
2008-09-23T19:27:07Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
123,226
<p>Use the <a href="https://docs.python.org/3/library/shutil.html">shutil module</a>.</p> <pre><code>copyfile(src, dst) </code></pre> <p>Copy the contents of the file named src to a file named dst. The destination location must be writable; otherwise, an IOError exception will be raised. If dst already exists, it wil...
25
2008-09-23T19:27:23Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
123,238
<pre><code>import shutil shutil.copy2('/dir/file.ext', '/new/dir/newname.ext') </code></pre> <p>or</p> <pre><code>shutil.copy2('/dir/file.ext', '/new/dir') </code></pre> <p><code>copy2</code> is also often useful, it preserves the original modification and access info (mtime and atime) in the file metadata.</p>
281
2008-09-23T19:29:41Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
125,810
<p>Copying a file is a relatively straightforward operation as shown by the examples below, but you should instead use the <a href="https://docs.python.org/library/shutil.html">shutil stdlib module</a> for that.</p> <pre class="lang-py prettyprint-override"><code>def copyfileobj_example(source, dest, buffer_size=1024*...
54
2008-09-24T07:21:12Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
5,310,215
<p>Directory and File copy example - From Tim Golden's Python Stuff:</p> <p><a href="http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html">http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html</a></p> <pre><code>import os import shutil import tempfile filename1 = tempfile.mktemp (".txt") open (filen...
20
2011-03-15T10:11:21Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
27,575,238
<p>You could use <code>os.system('cp nameoffilegeneratedbyprogram /otherdirectory/')</code></p> <p>or as I did it, </p> <pre><code>os.system('cp '+ rawfile + ' rawdata.dat') </code></pre> <p>where <code>rawfile</code> is the name that I had generated inside the program.</p> <p>This is a Linux only solution </p>
6
2014-12-19T23:18:02Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
30,359,308
<pre><code>------------------------------------------------------------------------- | Function |Copies Metadata|Copies Permissions|Can Specify Buffer| ------------------------------------------------------------------------- | shutil.copy | No | Yes | No | ---------...
126
2015-05-20T20:01:48Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
30,431,587
<p>For large files, what I did was read the file line by line and read each line into an array. Then, once the array reached a certain size, append it to a new file. </p> <pre><code>for line in open("file.txt", "r"): list.append(line) if len(list) == 1000000: output.writelines(list) del list[:...
2
2015-05-25T05:11:47Z
[ "python", "filesystems" ]
How do I copy a file in python?
123,198
<p>How do I copy a file in Python? I couldn't find anything under <a href="https://docs.python.org/2/library/os.html"><code>os</code></a>.</p>
819
2008-09-23T19:23:48Z
36,396,465
<pre class="lang-py prettyprint-override"><code>from subprocess import call call("cp -p &lt;file&gt; &lt;file&gt;", shell=True) </code></pre>
3
2016-04-04T07:08:24Z
[ "python", "filesystems" ]
Passing apache2 digest authentication information to a wsgi script run by mod_wsgi
123,499
<p>I've got the directive</p> <pre><code>&lt;VirtualHost *&gt; &lt;Location /&gt; AuthType Digest AuthName "global" AuthDigestDomain / AuthUserFile /root/apache_users &lt;Limit GET&gt; Require valid-user &lt;/Limit&gt; &lt;/Location&gt; WSGIScript...
6
2008-09-23T20:05:07Z
123,526
<p>add <code>WSGIPassAuthorization On</code>:</p> <pre><code>&lt;VirtualHost *&gt; &lt;Location /&gt; AuthType Digest AuthName "global" AuthDigestDomain / AuthUserFile /root/apache_users &lt;Limit GET&gt; Require valid-user &lt;/Limit&gt; &lt;/Locatio...
13
2008-09-23T20:08:51Z
[ "python", "apache", "authentication", "mod-wsgi", "wsgi" ]
Passing apache2 digest authentication information to a wsgi script run by mod_wsgi
123,499
<p>I've got the directive</p> <pre><code>&lt;VirtualHost *&gt; &lt;Location /&gt; AuthType Digest AuthName "global" AuthDigestDomain / AuthUserFile /root/apache_users &lt;Limit GET&gt; Require valid-user &lt;/Limit&gt; &lt;/Location&gt; WSGIScript...
6
2008-09-23T20:05:07Z
1,038,130
<p>Additional information about Apache/mod_wsgi and access, authentication and authorization mechanisms can be found in:</p> <p><a href="http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms" rel="nofollow">http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms</a></p> <p>The information isn't passed b...
2
2009-06-24T12:43:03Z
[ "python", "apache", "authentication", "mod-wsgi", "wsgi" ]
How to get/set logical directory path in python
123,958
<p>In python is it possible to get or set a logical directory (as opposed to an absolute one).</p> <p>For example if I have:</p> <pre><code>/real/path/to/dir </code></pre> <p>and I have</p> <pre><code>/linked/path/to/dir </code></pre> <p>linked to the same directory.</p> <p>using os.getcwd and os.chdir will alway...
5
2008-09-23T21:19:37Z
123,985
<p>The underlying operational system / shell reports real paths to python. </p> <p>So, there really is no way around it, since <code>os.getcwd()</code> is a wrapped call to C Library <code>getcwd()</code> function.</p> <p>There are some workarounds in the spirit of the one that you already know which is launching <co...
10
2008-09-23T21:22:43Z
[ "python", "path", "symlink" ]
How to get/set logical directory path in python
123,958
<p>In python is it possible to get or set a logical directory (as opposed to an absolute one).</p> <p>For example if I have:</p> <pre><code>/real/path/to/dir </code></pre> <p>and I have</p> <pre><code>/linked/path/to/dir </code></pre> <p>linked to the same directory.</p> <p>using os.getcwd and os.chdir will alway...
5
2008-09-23T21:19:37Z
27,951,538
<p>This also does the trick for me:</p> <pre><code>import os os.popen('pwd').read().strip('\n') </code></pre> <p>Here is a demonstration in python shell:</p> <pre><code>&gt;&gt;&gt; import os &gt;&gt;&gt; os.popen('pwd').read() '/home/projteam/staging/site/proj\n' &gt;&gt;&gt; os.popen('pwd').read().strip('\n') '/ho...
1
2015-01-14T20:09:30Z
[ "python", "path", "symlink" ]
Passing around urls between applications in the same project
124,108
<p>I am trying to mock-up an API and am using separate apps within Django to represent different web services. I would like App A to take in a link that corresponds to App B and parse the <code>json</code> response. </p> <p>Is there a way to dynamically construct the url to App B so that I can test the code in develop...
1
2008-09-23T21:47:35Z
124,137
<p>You could do something like</p> <pre><code>if settings.DEBUG: other = "localhost" else: other = "somehost" </code></pre> <p>and use other to build the external URL. Generally you code in DEBUG mode and deploy in non-DEBUG mode. settings.DEBUG is a 'standard' Django thing.</p>
2
2008-09-23T21:51:15Z
[ "python", "django", "web-services", "development-environment" ]
Passing around urls between applications in the same project
124,108
<p>I am trying to mock-up an API and am using separate apps within Django to represent different web services. I would like App A to take in a link that corresponds to App B and parse the <code>json</code> response. </p> <p>Is there a way to dynamically construct the url to App B so that I can test the code in develop...
1
2008-09-23T21:47:35Z
124,422
<p>By "separate apps within Django" do you mean separate applications with a common settings? That is to say, two applications within the same Django site (or project)?</p> <p>If so, the {% url %} tag will generate a proper absolute URL to any of the apps listed in the settings file.</p> <p>If there are separate Dja...
1
2008-09-23T22:52:54Z
[ "python", "django", "web-services", "development-environment" ]
Comparison of Python and Perl solutions to Wide Finder challenge
124,171
<p>I'd be very grateful if you could compare the winning <a href="http://www.cs.ucsd.edu/~sorourke/wf.pl" rel="nofollow">O’Rourke's Perl solution</a> to <a href="http://effbot.org/zone/wide-finder.htm" rel="nofollow">Lundh's Python solution</a>, as I don't know Perl good enough to understand what's going on there. Mo...
8
2008-09-23T21:57:11Z
124,255
<p>Perl is heavily optimized for text processing. There are so many factors that it's hard to say what's the exact difference. Text is represented completely differently internally (utf-8 versus utf-16/utf-32) and the regular expression engines are completely different too. Python's regular expression engine is a cu...
5
2008-09-23T22:14:18Z
[ "python", "performance", "perl", "analysis" ]
Comparison of Python and Perl solutions to Wide Finder challenge
124,171
<p>I'd be very grateful if you could compare the winning <a href="http://www.cs.ucsd.edu/~sorourke/wf.pl" rel="nofollow">O’Rourke's Perl solution</a> to <a href="http://effbot.org/zone/wide-finder.htm" rel="nofollow">Lundh's Python solution</a>, as I don't know Perl good enough to understand what's going on there. Mo...
8
2008-09-23T21:57:11Z
124,357
<p>The better regex implementation of perl is one part of the story. That can't explain however why the perl implementation scales better. The difference become bigger with more processors. For some reason the python implementation has an issue there.</p>
10
2008-09-23T22:39:07Z
[ "python", "performance", "perl", "analysis" ]
Comparison of Python and Perl solutions to Wide Finder challenge
124,171
<p>I'd be very grateful if you could compare the winning <a href="http://www.cs.ucsd.edu/~sorourke/wf.pl" rel="nofollow">O’Rourke's Perl solution</a> to <a href="http://effbot.org/zone/wide-finder.htm" rel="nofollow">Lundh's Python solution</a>, as I don't know Perl good enough to understand what's going on there. Mo...
8
2008-09-23T21:57:11Z
134,803
<p>The Perl implementation uses the <a href="http://en.wikipedia.org/wiki/Mmap" rel="nofollow">mmap</a> system call. What that call does is establish a pointer which to the process appears to be a normal segment of memory or buffer to the program. It maps the contents of a file to a region of memory. There are perf...
1
2008-09-25T17:47:10Z
[ "python", "performance", "perl", "analysis" ]
Comparison of Python and Perl solutions to Wide Finder challenge
124,171
<p>I'd be very grateful if you could compare the winning <a href="http://www.cs.ucsd.edu/~sorourke/wf.pl" rel="nofollow">O’Rourke's Perl solution</a> to <a href="http://effbot.org/zone/wide-finder.htm" rel="nofollow">Lundh's Python solution</a>, as I don't know Perl good enough to understand what's going on there. Mo...
8
2008-09-23T21:57:11Z
1,831,153
<blockquote> <p>The Perl implementation uses the mmap system call. </p> </blockquote> <p>This. It avoids buffer copying and provides async I/O.</p>
0
2009-12-02T07:12:44Z
[ "python", "performance", "perl", "analysis" ]
Comparison of Python and Perl solutions to Wide Finder challenge
124,171
<p>I'd be very grateful if you could compare the winning <a href="http://www.cs.ucsd.edu/~sorourke/wf.pl" rel="nofollow">O’Rourke's Perl solution</a> to <a href="http://effbot.org/zone/wide-finder.htm" rel="nofollow">Lundh's Python solution</a>, as I don't know Perl good enough to understand what's going on there. Mo...
8
2008-09-23T21:57:11Z
13,300,315
<p>Many-core Engine (MCE) has been released for Perl. MCE does quite well at this, even when reading directly from disk with 8 workers (cold cache). Compare to wf_mmap. MCE follows a bank queuing model when reading input data. Look under the images folder for slides on it.</p> <p>The source code is hosted at <a href="...
3
2012-11-09T00:28:26Z
[ "python", "performance", "perl", "analysis" ]
What is the intended use of the DEFAULT section in config files used by ConfigParser?
124,692
<p>I've used ConfigParser for quite a while for simple configs. One thing that's bugged me for a long time is the DEFAULT section. I'm not really sure what's an appropriate use. I've read the documentation, but I would really like to see some clever examples of its use and how it affects other sections in the file (som...
16
2008-09-24T00:16:34Z
124,785
<p>I found an explanation <a href="http://www.enfoldsystems.com/software/proxy/docs/4.0/configuringmanually.html#the-default-section">here</a> by googling for "windows ini" "default section". Summary: whatever you put in the [DEFAULT] section gets propagated to every other section. Using the example from the linked w...
23
2008-09-24T00:49:13Z
[ "python", "parsing", "configuration-files" ]
What is the easiest, most concise way to make selected attributes in an instance be readonly?
125,034
<p>In Python, I want to make <strong>selected</strong> instance attributes of a class be readonly to code outside of the class. I want there to be no way outside code can alter the attribute, except indirectly by invoking methods on the instance. I want the syntax to be concise. What is the best way? (I give my current...
8
2008-09-24T02:15:21Z
125,053
<p>There is no real way to do this. There are ways to make it more 'difficult', but there's no concept of completely hidden, inaccessible class attributes.</p> <p>If the person using your class can't be trusted to follow the API docs, then that's their own problem. Protecting people from doing stupid stuff just means ...
1
2008-09-24T02:20:16Z
[ "python", "attributes", "readonly" ]
What is the easiest, most concise way to make selected attributes in an instance be readonly?
125,034
<p>In Python, I want to make <strong>selected</strong> instance attributes of a class be readonly to code outside of the class. I want there to be no way outside code can alter the attribute, except indirectly by invoking methods on the instance. I want the syntax to be concise. What is the best way? (I give my current...
8
2008-09-24T02:15:21Z
125,058
<pre><code>class C(object): def __init__(self): self.fullaccess = 0 self.__readonly = 22 # almost invisible to outside code... # define a publicly visible, read-only version of '__readonly': readonly = property(lambda self: self.__readonly) def inc_readonly( self ): self.__re...
2
2008-09-24T02:21:56Z
[ "python", "attributes", "readonly" ]
What is the easiest, most concise way to make selected attributes in an instance be readonly?
125,034
<p>In Python, I want to make <strong>selected</strong> instance attributes of a class be readonly to code outside of the class. I want there to be no way outside code can alter the attribute, except indirectly by invoking methods on the instance. I want the syntax to be concise. What is the best way? (I give my current...
8
2008-09-24T02:15:21Z
125,061
<p>You should use the <code>@property</code> decorator.</p> <pre><code>&gt;&gt;&gt; class a(object): ... def __init__(self, x): ... self.x = x ... @property ... def xval(self): ... return self.x ... &gt;&gt;&gt; b = a(5) &gt;&gt;&gt; b.xval 5 &gt;&gt;&gt; b.xval = 6 Traceback (most...
7
2008-09-24T02:22:16Z
[ "python", "attributes", "readonly" ]
What is the easiest, most concise way to make selected attributes in an instance be readonly?
125,034
<p>In Python, I want to make <strong>selected</strong> instance attributes of a class be readonly to code outside of the class. I want there to be no way outside code can alter the attribute, except indirectly by invoking methods on the instance. I want the syntax to be concise. What is the best way? (I give my current...
8
2008-09-24T02:15:21Z
125,136
<p>You could use a metaclass that auto-wraps methods (or class attributes) that follow a naming convention into properties (shamelessly taken from <a href="http://www.python.org/download/releases/2.2/descrintro/#metaclasses" rel="nofollow">Unifying Types and Classes in Python 2.2</a>:</p> <pre><code>class autoprop(typ...
0
2008-09-24T02:49:37Z
[ "python", "attributes", "readonly" ]
What is the easiest, most concise way to make selected attributes in an instance be readonly?
125,034
<p>In Python, I want to make <strong>selected</strong> instance attributes of a class be readonly to code outside of the class. I want there to be no way outside code can alter the attribute, except indirectly by invoking methods on the instance. I want the syntax to be concise. What is the best way? (I give my current...
8
2008-09-24T02:15:21Z
125,739
<p>Here's how:</p> <pre><code>class whatever(object): def __init__(self, a, b, c, ...): self.__foobar = 1 self.__blahblah = 2 foobar = property(lambda self: self.__foobar) blahblah = property(lambda self: self.__blahblah) </code></pre> <p>(Assuming <code>foobar</code> and <code>blahblah</code> are the ...
2
2008-09-24T06:49:36Z
[ "python", "attributes", "readonly" ]
What is the easiest, most concise way to make selected attributes in an instance be readonly?
125,034
<p>In Python, I want to make <strong>selected</strong> instance attributes of a class be readonly to code outside of the class. I want there to be no way outside code can alter the attribute, except indirectly by invoking methods on the instance. I want the syntax to be concise. What is the best way? (I give my current...
8
2008-09-24T02:15:21Z
2,704,300
<p>I am aware that William Keller is the cleanest solution by far.. but here's something I came up with.. </p> <pre><code>class readonly(object): def __init__(self, attribute_name): self.attribute_name = attribute_name def __get__(self, instance, instance_type): if instance != None: ...
0
2010-04-24T11:37:38Z
[ "python", "attributes", "readonly" ]
Adding New Element to Text Substring
125,102
<p>Say I have the following string:</p> <pre><code>"I am the most foo h4ck3r ever!!" </code></pre> <p>I'm trying to write a makeSpecial(foo) function where the foo substring would be wrapped in a new span element, resulting in:</p> <pre><code>"I am the most &lt;span class="special"&gt;foo&gt;&lt;/span&gt; h4ck3r eve...
1
2008-09-24T02:37:49Z
125,122
<p>How about this:</p> <pre><code>Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; def makeSpecial(mystring, special_substr): ... return mystring.replace(special_substr, '&lt;span class="special...
3
2008-09-24T02:46:54Z
[ "javascript", "jquery", "python", "beautifulsoup" ]
Adding New Element to Text Substring
125,102
<p>Say I have the following string:</p> <pre><code>"I am the most foo h4ck3r ever!!" </code></pre> <p>I'm trying to write a makeSpecial(foo) function where the foo substring would be wrapped in a new span element, resulting in:</p> <pre><code>"I am the most &lt;span class="special"&gt;foo&gt;&lt;/span&gt; h4ck3r eve...
1
2008-09-24T02:37:49Z
125,134
<p>As far as I can tell, you're doing a simple string replace. You're replacing "foo" with "bar foo bar." So from <a href="http://docs.python.org/lib/string-methods.html" rel="nofollow" title="string methods">string</a> you could just use </p> <pre><code>replace(old, new[, count]) </code></pre> <p>Return a copy of th...
1
2008-09-24T02:49:13Z
[ "javascript", "jquery", "python", "beautifulsoup" ]
Adding New Element to Text Substring
125,102
<p>Say I have the following string:</p> <pre><code>"I am the most foo h4ck3r ever!!" </code></pre> <p>I'm trying to write a makeSpecial(foo) function where the foo substring would be wrapped in a new span element, resulting in:</p> <pre><code>"I am the most &lt;span class="special"&gt;foo&gt;&lt;/span&gt; h4ck3r eve...
1
2008-09-24T02:37:49Z
125,142
<p>If you wanted to do it with javascript/jQuery, take a look at this question: <a href="http://stackoverflow.com/questions/119441/highlight-a-word-with-jquery">http://stackoverflow.com/questions/119441/highlight-a-word-with-jquery</a></p>
0
2008-09-24T02:51:15Z
[ "javascript", "jquery", "python", "beautifulsoup" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
125,226
<p>Take a look at <a href="http://stackoverflow.com/questions/124869/how-does-the-doc-format-work" rel="nofollow" title="how the doc format works">how the doc format works</a> and <a href="http://stackoverflow.com/questions/124959/create-word-document-using-php-in-lunux" rel="nofollow" title="create word document using...
4
2008-09-24T03:17:26Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
125,235
<p>I'm not sure if you're going to have much luck without using COM. The .doc format is ridiculously complex, and is often called a "memory dump" of Word at the time of saving!</p> <p>At Swati, that's in HTML, which is fine and dandy, but most word documents aren't so nice!</p>
2
2008-09-24T03:19:53Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
125,248
<p>OpenOffice.org can be scripted with Python: <a href="http://wiki.services.openoffice.org/wiki/Python">see here</a>.</p> <p>Since OOo can load most MS Word files flawlessly, I'd say that's your best bet.</p>
10
2008-09-24T03:23:42Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
125,386
<p>You could make a subprocess call to <a href="http://en.wikipedia.org/wiki/Antiword">antiword</a>. Antiword is a linux commandline utility for dumping text out of a word doc. Works pretty well for simple documents (obviously it loses formatting). It's available through apt, and probably as RPM, or you could compil...
17
2008-09-24T04:13:03Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
404,378
<p>I know this is an old question, but I was recently trying to find a way to extract text from MS word files, and the best solution by far I found was with wvLib:</p> <p><a href="http://wvware.sourceforge.net/">http://wvware.sourceforge.net/</a></p> <p>After installing the library, using it in Python is pretty easy:...
5
2009-01-01T01:14:38Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
1,258,599
<p>(Note: I posted this on <a href="http://stackoverflow.com/questions/1184747/rtf-doc-docx-text-extraction-in-program-written-in-c-qt">this question</a> as well, but it seems relevant here, so please excuse the repost.)</p> <p>Now, this is pretty ugly and pretty hacky, but it seems to work for me for basic text extra...
4
2009-08-11T05:38:51Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
1,723,437
<p>If your intention is to use purely python modules without calling a subprocess, you can use the zipfile python modude.</p> <pre><code>content = "" # Load DocX into zipfile docx = zipfile.ZipFile('/home/whateverdocument.docx') # Unpack zipfile unpacked = docx.infolist() # Find the /word/document.xml file in the pack...
4
2009-11-12T16:18:16Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
1,967,869
<p>benjamin's answer is a pretty good one. I have just consolidated...</p> <pre><code>import zipfile, re docx = zipfile.ZipFile('/path/to/file/mydocument.docx') content = docx.read('word/document.xml') cleaned = re.sub('&lt;(.|\n)*?&gt;','',content) print cleaned </code></pre>
9
2009-12-28T03:39:54Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
1,979,906
<p>Use the <strong>native Python docx module</strong>. Here's how to extract all the text from a doc:</p> <pre><code>document = docx.Document(filename) docText = '\n\n'.join([ paragraph.text.encode('utf-8') for paragraph in document.paragraphs ]) print docText </code></pre> <p>See <a href="https://python-docx.rea...
26
2009-12-30T12:17:09Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
10,617,686
<p>Unoconv might also be a good alternative: <a href="http://linux.die.net/man/1/unoconv" rel="nofollow">http://linux.die.net/man/1/unoconv</a></p>
3
2012-05-16T11:35:57Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
14,829,241
<p>Just an option for reading 'doc' files without using COM: <a href="https://github.com/rembish/Miette" rel="nofollow">miette</a>. Should work on any platform.</p>
1
2013-02-12T09:25:30Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
30,122,779
<p>If you have LibreOffice installed, <a href="http://stackoverflow.com/questions/16516044/is-it-possible-to-read-word-files-doc-docx-in-python/30122239#30122239">you can simply call it from the command line to convert the file to text</a>, then load the text into Python.</p>
1
2015-05-08T11:31:23Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
30,582,548
<p>Is this an old question? I believe that such thing does not exist. There are only answered and unanswered ones. This one is pretty unanswered, or half answered if you wish. Well, methods for reading *.docx (MS Word 2007 and later) documents without using COM interop are all covered. But methods for extracting text f...
1
2015-06-01T20:20:14Z
[ "python", "linux", "ms-word" ]
extracting text from MS word files in python
125,222
<p>for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?</p>
22
2008-09-24T03:15:13Z
38,750,949
<p>To read Word 2007 and later files, including .docx files, you can use the <a href="http://python-docx.readthedocs.io/en/latest/user/documents.html" rel="nofollow">python-docx</a> package:</p> <pre><code>from docx import Document document = Document('existing-document-file.docx') document.save('new-file-name.docx') ...
0
2016-08-03T18:17:37Z
[ "python", "linux", "ms-word" ]
How do I modify a text file in Python?
125,703
<p>I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?</p>
125
2008-09-24T06:30:56Z
125,713
<p>Depends on what you want to do. To append you can open it with "a":</p> <pre><code> with open("foo.txt", "a") as f: f.write("new line\n") </code></pre> <p>If you want to preprend something you have to read from the file first:</p> <pre><code>with open("foo.txt", "r+") as f: old = f.read() # read everyt...
75
2008-09-24T06:35:51Z
[ "python", "file", "text" ]
How do I modify a text file in Python?
125,703
<p>I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?</p>
125
2008-09-24T06:30:56Z
125,759
<p>Unfortunately there is no way to insert into the middle of a file without re-writing it. As previous posters have indicated, you can append to a file or overwrite part of it using seek but if you want to add stuff at the beginning or the middle, you'll have to rewrite it.</p> <p>This is an operating system thing, n...
100
2008-09-24T06:57:21Z
[ "python", "file", "text" ]
How do I modify a text file in Python?
125,703
<p>I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?</p>
125
2008-09-24T06:30:56Z
126,389
<p>Rewriting a file in place is often done by saving the old copy with a modified name. Unix folks add a <code>~</code> to mark the old one. Windows folks do all kinds of things -- add .bak or .old -- or rename the file entirely or put the ~ on the front of the name.</p> <pre><code>import shutil shutil.move( afile, ...
28
2008-09-24T10:27:45Z
[ "python", "file", "text" ]
How do I modify a text file in Python?
125,703
<p>I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?</p>
125
2008-09-24T06:30:56Z
130,844
<p>Python's mmap module will allow you to insert into a file. The following sample shows how it can be done in Unix (Windows mmap may be different). Note that this does not handle all error conditions and you might corrupt or lose the original file. Also, this won't handle unicode strings.</p> <pre><code>import os fro...
13
2008-09-25T00:41:01Z
[ "python", "file", "text" ]
How do I modify a text file in Python?
125,703
<p>I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?</p>
125
2008-09-24T06:30:56Z
1,811,866
<p>The fileinput module of the Python standard library will rewrite a file inplace if you use the inplace=1 parameter:</p> <pre><code>import sys import fileinput # replace all occurrences of 'sit' with 'SIT' and insert a line after the 5th for i, line in enumerate(fileinput.input('lorem_ipsum.txt', inplace=1)): s...
43
2009-11-28T07:25:54Z
[ "python", "file", "text" ]
How do I modify a text file in Python?
125,703
<p>I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?</p>
125
2008-09-24T06:30:56Z
13,464,228
<p>As mentioned by Adam you have to take your system limitations into consideration before you can decide on approach whether you have enough memory to read it all into memory replace parts of it and re-write it. </p> <p>If you're dealing with a small file or have no memory issues this might help:</p> <p><strong>Opti...
7
2012-11-19T23:24:50Z
[ "python", "file", "text" ]
How do I modify a text file in Python?
125,703
<p>I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?</p>
125
2008-09-24T06:30:56Z
35,149,780
<p>Wrote a small class for doing this cleanly.</p> <pre><code>import tempfile class FileModifierError(Exception): pass class FileModifier(object): def __init__(self, fname): self.__write_dict = {} self.__filename = fname self.__tempfile = tempfile.TemporaryFile() with open(fn...
0
2016-02-02T09:42:12Z
[ "python", "file", "text" ]
Python library for rendering HTML and javascript
126,131
<p>Is there any python module for rendering a HTML page with javascript and get back a DOM object?</p> <p>I want to parse a page which generates almost all of its content using javascript. </p>
16
2008-09-24T09:05:57Z
126,216
<p>Only way I know to accomplish this would be to drive real browser, for example using <a href="http://selenium-rc.openqa.org" rel="nofollow">selenium-rc</a>.</p>
1
2008-09-24T09:33:24Z
[ "javascript", "python", "html" ]
Python library for rendering HTML and javascript
126,131
<p>Is there any python module for rendering a HTML page with javascript and get back a DOM object?</p> <p>I want to parse a page which generates almost all of its content using javascript. </p>
16
2008-09-24T09:05:57Z
126,250
<p>The big complication here is emulating the full browser environment outside of a browser. You can use stand alone javascript interpreters like Rhino and SpiderMonkey to run javascript code but they don't provide a complete browser like environment to full render a web page.</p> <p>If I needed to solve a problem lik...
7
2008-09-24T09:42:52Z
[ "javascript", "python", "html" ]
Python library for rendering HTML and javascript
126,131
<p>Is there any python module for rendering a HTML page with javascript and get back a DOM object?</p> <p>I want to parse a page which generates almost all of its content using javascript. </p>
16
2008-09-24T09:05:57Z
126,286
<p><a href="http://doc.trolltech.com/4.4/qtwebkit.html">QtWebKit</a> is contained in PyQt4, but I don't know if you can use it without showing a widget. After a cursory look over the documentation, it seems to me you can only get HTML, not a DOM tree.</p>
5
2008-09-24T09:54:22Z
[ "javascript", "python", "html" ]
Python library for rendering HTML and javascript
126,131
<p>Is there any python module for rendering a HTML page with javascript and get back a DOM object?</p> <p>I want to parse a page which generates almost all of its content using javascript. </p>
16
2008-09-24T09:05:57Z
126,341
<p>You can probably use <a href="http://code.google.com/p/pywebkitgtk/" rel="nofollow">python-webkit</a> for it. Requires a running glib and GTK, but that's probably less problematic than wrapping the parts of webkit without glib.</p> <p>I don't know if it does everything you need, but I guess you should give it a tr...
1
2008-09-24T10:11:15Z
[ "javascript", "python", "html" ]
Example Facebook Application using TurboGears -- pyFacebook
126,356
<p>I have a TurboGears application I'd like to run through Facebook, and am looking for an example TurboGears project using pyFacebook or minifb.py. pyFacebook is Django-centric, and I can probably figure it out, but this is, after all, the lazy web.</p>
2
2008-09-24T10:14:07Z
126,399
<p>Why is pyFacebook django centric? Looks like it works perfectly fine with all kinds of WSGI apps or Python applications in general. No need to use Django.</p>
3
2008-09-24T10:31:00Z
[ "python", "facebook", "turbogears" ]
Example Facebook Application using TurboGears -- pyFacebook
126,356
<p>I have a TurboGears application I'd like to run through Facebook, and am looking for an example TurboGears project using pyFacebook or minifb.py. pyFacebook is Django-centric, and I can probably figure it out, but this is, after all, the lazy web.</p>
2
2008-09-24T10:14:07Z
130,332
<p>pyFacebook is Django-centric because it includes a Django example. I did not intend to irk, but am merely looking for a TurboGears example using pyFacebook.</p>
1
2008-09-24T22:32:28Z
[ "python", "facebook", "turbogears" ]
"cannot find -lpq" when trying to install psycopg2
126,364
<p><strong>Intro</strong>: I'm trying to migrate our Trac SQLite to a PostgreSQL backend, to do that I need psycopg2. After clicking past the embarrassing rant on www.initd.org I downloaded the latest version and tried running <code>setup.py install</code>. This didn't work, telling me I needed mingw. So I downloaded a...
0
2008-09-24T10:16:00Z
126,494
<p>Have you tried the <a href="http://www.stickpeople.com/projects/python/win-psycopg/" rel="nofollow">binary build</a> of psycopg2 for windows? If that works with your python then it mitigates the need to build by hand.</p> <p>I've seen random people ask this question on various lists and it seems one recommendation ...
2
2008-09-24T10:59:27Z
[ "python", "postgresql", "trac" ]
"cannot find -lpq" when trying to install psycopg2
126,364
<p><strong>Intro</strong>: I'm trying to migrate our Trac SQLite to a PostgreSQL backend, to do that I need psycopg2. After clicking past the embarrassing rant on www.initd.org I downloaded the latest version and tried running <code>setup.py install</code>. This didn't work, telling me I needed mingw. So I downloaded a...
0
2008-09-24T10:16:00Z
126,495
<p>Compiling extensions on windows can be tricky. There are precompiled libraries available however: <a href="http://www.stickpeople.com/projects/python/win-psycopg/" rel="nofollow">http://www.stickpeople.com/projects/python/win-psycopg/</a></p>
1
2008-09-24T11:00:28Z
[ "python", "postgresql", "trac" ]
Iterate a list with indexes in Python
126,524
<p>I could swear I've seen the function (or method) that takes a list, like this <code>[3, 7, 19]</code> and makes it into iterable list of tuples, like so: <code>[(0,3), (1,7), (2,19)]</code> to use it instead of:</p> <pre><code>for i in range(len(name_of_list)): name_of_list[i] = something </code></pre> <p>but ...
133
2008-09-24T11:08:52Z
126,533
<pre><code>&gt;&gt;&gt; a = [3,4,5,6] &gt;&gt;&gt; for i, val in enumerate(a): ... print i, val ... 0 3 1 4 2 5 3 6 &gt;&gt;&gt; </code></pre>
227
2008-09-24T11:11:41Z
[ "python", "list" ]
Iterate a list with indexes in Python
126,524
<p>I could swear I've seen the function (or method) that takes a list, like this <code>[3, 7, 19]</code> and makes it into iterable list of tuples, like so: <code>[(0,3), (1,7), (2,19)]</code> to use it instead of:</p> <pre><code>for i in range(len(name_of_list)): name_of_list[i] = something </code></pre> <p>but ...
133
2008-09-24T11:08:52Z
126,535
<p>Yep, that would be the <a href="http://docs.python.org/library/functions.html#enumerate"><code>enumerate</code></a> function! Or more to the point, you need to do:</p> <pre><code>list(enumerate([3,7,19])) [(0, 3), (1, 7), (2, 19)] </code></pre>
78
2008-09-24T11:12:14Z
[ "python", "list" ]
Iterate a list with indexes in Python
126,524
<p>I could swear I've seen the function (or method) that takes a list, like this <code>[3, 7, 19]</code> and makes it into iterable list of tuples, like so: <code>[(0,3), (1,7), (2,19)]</code> to use it instead of:</p> <pre><code>for i in range(len(name_of_list)): name_of_list[i] = something </code></pre> <p>but ...
133
2008-09-24T11:08:52Z
127,375
<p>Here's another using the <code>zip</code> function.</p> <pre><code>&gt;&gt;&gt; a = [3, 7, 19] &gt;&gt;&gt; zip(range(len(a)), a) [(0, 3), (1, 7), (2, 19)] </code></pre>
21
2008-09-24T14:10:58Z
[ "python", "list" ]
Iterate a list with indexes in Python
126,524
<p>I could swear I've seen the function (or method) that takes a list, like this <code>[3, 7, 19]</code> and makes it into iterable list of tuples, like so: <code>[(0,3), (1,7), (2,19)]</code> to use it instead of:</p> <pre><code>for i in range(len(name_of_list)): name_of_list[i] = something </code></pre> <p>but ...
133
2008-09-24T11:08:52Z
18,777,094
<p>Here it is a solution using map function:</p> <pre><code>&gt;&gt;&gt; a = [3, 7, 19] &gt;&gt;&gt; map(lambda x: (x, a[x]), range(len(a))) [(0, 3), (1, 7), (2, 19)] </code></pre> <p>And a solution using list comprehensions:</p> <pre><code>&gt;&gt;&gt; a = [3,7,19] &gt;&gt;&gt; [(x, a[x]) for x in range(len(a))] [(...
6
2013-09-13T02:01:14Z
[ "python", "list" ]
Iterate a list with indexes in Python
126,524
<p>I could swear I've seen the function (or method) that takes a list, like this <code>[3, 7, 19]</code> and makes it into iterable list of tuples, like so: <code>[(0,3), (1,7), (2,19)]</code> to use it instead of:</p> <pre><code>for i in range(len(name_of_list)): name_of_list[i] = something </code></pre> <p>but ...
133
2008-09-24T11:08:52Z
35,429,590
<p>If you have multiple lists, you can do this combining <code>enumerate</code> and <code>zip</code>:</p> <pre><code>list1 = [1, 2, 3, 4, 5] list2 = [10, 20, 30, 40, 50] list3 = [100, 200, 300, 400, 500] for i, (l1, l2, l3) in enumerate(zip(list1, list2, list3)): print(i, l1, l2, l3) </code></pre> Output: <pre><...
0
2016-02-16T10:12:11Z
[ "python", "list" ]
Iterate a list with indexes in Python
126,524
<p>I could swear I've seen the function (or method) that takes a list, like this <code>[3, 7, 19]</code> and makes it into iterable list of tuples, like so: <code>[(0,3), (1,7), (2,19)]</code> to use it instead of:</p> <pre><code>for i in range(len(name_of_list)): name_of_list[i] = something </code></pre> <p>but ...
133
2008-09-24T11:08:52Z
36,571,189
<p>python <code>enumerate</code> function will be satisfied your requirements</p> <pre><code>result = list(enumerate([1,3,7,12])) print result </code></pre> <p><strong>output</strong><br></p> <pre><code>[(0, 1), (1, 3), (2, 7),(3,12)] </code></pre>
1
2016-04-12T10:55:14Z
[ "python", "list" ]
How can i move an object drawn in device context python
126,738
<p>I have drawn an image in the device context using python, and i want to move it somothly/animate either vertically or horizontally? What algorithm should i use? </p> <p>Where can i get info for this kind of tasks in python?</p>
0
2008-09-24T12:12:04Z
126,750
<p>Once an object is drawn in a device context it stays there. If you want to move it you need to redraw it.</p> <p>You can keep a background that's fixed and only redraw the movable elements each time they move. Basically that's how it's done.</p> <p>To move an object smoothly over a line you have to do something li...
1
2008-09-24T12:15:05Z
[ "python", "animation" ]
How can i move an object drawn in device context python
126,738
<p>I have drawn an image in the device context using python, and i want to move it somothly/animate either vertically or horizontally? What algorithm should i use? </p> <p>Where can i get info for this kind of tasks in python?</p>
0
2008-09-24T12:12:04Z
127,216
<p>To smoothly move object between starting coordinate <code>(x1, y1)</code> and destination coordinate <code>(x2,y2)</code>, you need to first ask yourself, how <em>long</em> the object should take to get to its destination. Lets say you want the object to get there in <code>t</code> time units (which maybe seconds, h...
0
2008-09-24T13:45:34Z
[ "python", "animation" ]
Checking for code changes in all imported python modules
126,787
<p>Almost every Python web framework has a simple server that runs a wsgi application and automatically reloads the imported modules every time the source gets changed. I know I can look at the code and see how it's done, but that may take some time and I'm asking just out of curiosity. Does anyone have any idea how th...
1
2008-09-24T12:21:02Z
126,843
<p>As the author of one of the reloader mechanisms (the one in werkzeug) I can tell you that it doesn't work. What all the reloaders do is forking one time and restarting the child process if a monitor thread notices that one module changed on the file system.</p> <p>Inline <code>reload()</code>ing doesn't work becau...
2
2008-09-24T12:28:50Z
[ "python" ]
Checking for code changes in all imported python modules
126,787
<p>Almost every Python web framework has a simple server that runs a wsgi application and automatically reloads the imported modules every time the source gets changed. I know I can look at the code and see how it's done, but that may take some time and I'm asking just out of curiosity. Does anyone have any idea how th...
1
2008-09-24T12:21:02Z
126,866
<p>reload() does not work. "Reloading" is usually implemented by forking.</p> <p>Implementing "real" reload() is extremely difficult and even the most serious attempt, <a href="http://twistedmatrix.com/documents/current/api/twisted.python.rebuild.html" rel="nofollow">twisted.python.rebuild</a> isn't perfect.</p>
1
2008-09-24T12:34:47Z
[ "python" ]
Find out number of capture groups in Python regular expressions
127,055
<p>Is there a way to determine how many capture groups there are in a given regular expression?</p> <p>I would like to be able to do the follwing:</p> <pre><code>def groups(regexp, s): """ Returns the first result of re.findall, or an empty default &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', '123') ('1', '2', '...
16
2008-09-24T13:14:50Z
127,089
<p>First of all if you only need the first result of re.findall it's better to just use re.search that returns a match or None.</p> <p>For the groups number you could count the number of open parenthesis '(' except those that are escaped by '\'. You could use another regex for that:</p> <pre><code>def num_of_groups(r...
1
2008-09-24T13:21:55Z
[ "python", "regex" ]
Find out number of capture groups in Python regular expressions
127,055
<p>Is there a way to determine how many capture groups there are in a given regular expression?</p> <p>I would like to be able to do the follwing:</p> <pre><code>def groups(regexp, s): """ Returns the first result of re.findall, or an empty default &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', '123') ('1', '2', '...
16
2008-09-24T13:14:50Z
127,092
<p>The <code>lastindex</code> property of the match object should be what you are looking for. See the <a href="http://docs.python.org/lib/match-objects.html" rel="nofollow">re module docs</a>.</p>
0
2008-09-24T13:22:27Z
[ "python", "regex" ]
Find out number of capture groups in Python regular expressions
127,055
<p>Is there a way to determine how many capture groups there are in a given regular expression?</p> <p>I would like to be able to do the follwing:</p> <pre><code>def groups(regexp, s): """ Returns the first result of re.findall, or an empty default &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', '123') ('1', '2', '...
16
2008-09-24T13:14:50Z
127,097
<p>Something from inside sre_parse might help.</p> <p>At first glance, maybe something along the lines of:</p> <pre><code>&gt;&gt;&gt; import sre_parse &gt;&gt;&gt; sre_parse.parse('(\d)\d(\d)') [('subpattern', (1, [('in', [('category', 'category_digit')])])), ('in', [('category', 'category_digit')]), ('subpattern'...
2
2008-09-24T13:23:34Z
[ "python", "regex" ]
Find out number of capture groups in Python regular expressions
127,055
<p>Is there a way to determine how many capture groups there are in a given regular expression?</p> <p>I would like to be able to do the follwing:</p> <pre><code>def groups(regexp, s): """ Returns the first result of re.findall, or an empty default &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', '123') ('1', '2', '...
16
2008-09-24T13:14:50Z
127,105
<p>Might be wrong, but I don't think there is a way to find the number of groups that would have been returned had the regex matched. The only way I can think of to make this work the way you want it to is to pass the number of matches your particular regex expects as an argument.</p> <p>To clarify though: When finda...
0
2008-09-24T13:25:54Z
[ "python", "regex" ]
Find out number of capture groups in Python regular expressions
127,055
<p>Is there a way to determine how many capture groups there are in a given regular expression?</p> <p>I would like to be able to do the follwing:</p> <pre><code>def groups(regexp, s): """ Returns the first result of re.findall, or an empty default &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', '123') ('1', '2', '...
16
2008-09-24T13:14:50Z
127,392
<p>Using your code as a basis:</p> <pre><code>def groups(regexp, s): """ Returns the first result of re.findall, or an empty default &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', '123') ('1', '2', '3') &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', 'abc') ('', '', '') """ import re m = re.search(regexp, ...
1
2008-09-24T14:12:57Z
[ "python", "regex" ]
Find out number of capture groups in Python regular expressions
127,055
<p>Is there a way to determine how many capture groups there are in a given regular expression?</p> <p>I would like to be able to do the follwing:</p> <pre><code>def groups(regexp, s): """ Returns the first result of re.findall, or an empty default &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', '123') ('1', '2', '...
16
2008-09-24T13:14:50Z
136,215
<pre><code>def num_groups(regex): return re.compile(regex).groups </code></pre>
25
2008-09-25T21:18:49Z
[ "python", "regex" ]
Find out number of capture groups in Python regular expressions
127,055
<p>Is there a way to determine how many capture groups there are in a given regular expression?</p> <p>I would like to be able to do the follwing:</p> <pre><code>def groups(regexp, s): """ Returns the first result of re.findall, or an empty default &gt;&gt;&gt; groups(r'(\d)(\d)(\d)', '123') ('1', '2', '...
16
2008-09-24T13:14:50Z
28,284,530
<pre><code>f_x = re.search(...) len_groups = len(f_x.groups()) </code></pre>
0
2015-02-02T18:42:26Z
[ "python", "regex" ]
Contributing to Python
127,454
<p>I'm a pretty inexperienced programmer (can make tk apps, text processing, sort of understand oop), but Python is so awesome that I would like to help the community. What's the best way for a beginner to contribute?</p>
10
2008-09-24T14:20:06Z
127,493
<p>I guess one way would be to help with documentation (translation, updating), until you are aware enough about the language. Also following the devs and users mail groups would give you a pretty good idea of what is being done and needs to be done by the community.</p>
3
2008-09-24T14:23:58Z
[ "python" ]
Contributing to Python
127,454
<p>I'm a pretty inexperienced programmer (can make tk apps, text processing, sort of understand oop), but Python is so awesome that I would like to help the community. What's the best way for a beginner to contribute?</p>
10
2008-09-24T14:20:06Z
127,501
<p>If you aren't up to actually working on the Python core, there are still many ways to contribute.. 2 that immediately come to mind is:</p> <p>work on documentation.. it can ALWAYS be improved. Take your favorite modules and check out the documentation and add where you can.</p> <p>Reporting descriptive bugs is v...
1
2008-09-24T14:24:47Z
[ "python" ]
Contributing to Python
127,454
<p>I'm a pretty inexperienced programmer (can make tk apps, text processing, sort of understand oop), but Python is so awesome that I would like to help the community. What's the best way for a beginner to contribute?</p>
10
2008-09-24T14:20:06Z
127,510
<p>Get involved with the community: <a href="http://www.python.org/dev/" rel="nofollow">http://www.python.org/dev/</a></p>
1
2008-09-24T14:25:23Z
[ "python" ]
Contributing to Python
127,454
<p>I'm a pretty inexperienced programmer (can make tk apps, text processing, sort of understand oop), but Python is so awesome that I would like to help the community. What's the best way for a beginner to contribute?</p>
10
2008-09-24T14:20:06Z
127,601
<ol> <li><p>Add to the docs. it is downright crappy</p></li> <li><p>Help out other users on the dev and user mailing lists. </p></li> <li><p>TEST PYTHON. bugs in programming languages are real bad. And I have seen someone discover atleast 1 bug in python</p></li> <li><p>Frequent the #python channel on irc.freenode.net<...
5
2008-09-24T14:43:35Z
[ "python" ]
Contributing to Python
127,454
<p>I'm a pretty inexperienced programmer (can make tk apps, text processing, sort of understand oop), but Python is so awesome that I would like to help the community. What's the best way for a beginner to contribute?</p>
10
2008-09-24T14:20:06Z
127,651
<p>Build something cool in Python and share it with others. Small values of cool are still cool. Not everyone gets to write epic, world-changing software. </p> <p>Every problem solved well using Python is a way of showing how cool Python is.</p>
4
2008-09-24T14:54:21Z
[ "python" ]
Contributing to Python
127,454
<p>I'm a pretty inexperienced programmer (can make tk apps, text processing, sort of understand oop), but Python is so awesome that I would like to help the community. What's the best way for a beginner to contribute?</p>
10
2008-09-24T14:20:06Z
127,787
<p>Start by contributing to a Python project that you use and enjoy. This can be as simple as answering questions on the mailing list or IRC channel, offering to help with documentation and test writing or fixing bugs.</p>
0
2008-09-24T15:15:24Z
[ "python" ]
Contributing to Python
127,454
<p>I'm a pretty inexperienced programmer (can make tk apps, text processing, sort of understand oop), but Python is so awesome that I would like to help the community. What's the best way for a beginner to contribute?</p>
10
2008-09-24T14:20:06Z
132,237
<p>I see two ways of going about it: working on Python directly or working on something that utilizes Python</p> <p>Since you're a beginner, you're probably hesitant to work on the core Python language or feel that you can't contribute in a meaningful way, which is understandable. However, as a beginner, you're in a g...
2
2008-09-25T09:01:24Z
[ "python" ]
Editing XML as a dictionary in python?
127,606
<p>I'm trying to generate customized xml files from a template xml file in python. </p> <p>Conceptually, I want to read in the template xml, remove some elements, change some text attributes, and write the new xml out to a file. I wanted it to work something like this:</p> <pre><code>conf_base = ConvertXmlToDict('co...
8
2008-09-24T14:44:45Z
127,678
<p>I'm not sure if converting the info set to nested dicts first is easier. Using ElementTree, you can do this:</p> <pre><code>import xml.etree.ElementTree as ET doc = ET.parse("template.xml") lvl1 = doc.findall("level1-name")[0] lvl1.remove(lvl1.find("leaf1") lvl1.remove(lvl1.find("leaf2") # or use del lvl1[idx] doc....
11
2008-09-24T14:58:52Z
[ "python", "xml", "dictionary" ]
Editing XML as a dictionary in python?
127,606
<p>I'm trying to generate customized xml files from a template xml file in python. </p> <p>Conceptually, I want to read in the template xml, remove some elements, change some text attributes, and write the new xml out to a file. I wanted it to work something like this:</p> <pre><code>conf_base = ConvertXmlToDict('co...
8
2008-09-24T14:44:45Z
127,720
<p>Have you tried this?</p> <pre><code>print xml.etree.ElementTree.tostring( conf_new ) </code></pre>
0
2008-09-24T15:05:01Z
[ "python", "xml", "dictionary" ]