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 |
|---|---|---|---|---|---|---|---|---|---|
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 386,770 | <p>While at uni we were writing a computer vision system for analysing human behaviour based on video clips. We used python because of the excellent PIL, to speed up development and let us get easy access to the image frames we'd extracted from the video for converting to arrays etc.</p>
<p>For 90% of what we wanted i... | 7 | 2008-12-22T17:12:50Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 386,909 | <p>Whenever I find a Python bottleneck, I rewrite that code in C as a Python module.</p>
<p>For example, I have some hardware that sends image pixels as 4-byte 0RGB. Converting 8M from 0RGB to RGB in Python takes too long so I rewrote it as a Python module.</p>
<p>Writing Python (or other higher level languages) is m... | 5 | 2008-12-22T18:03:54Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 386,999 | <p>Not so far. I work for a company that has a molecular simulation engine and a bunch of programs written in python for processing the large multi-gigabyte datasets. All of our analysis software is now being written in Python because of the huge advantages in development flexibility and time. </p>
<p>If something is ... | 7 | 2008-12-22T18:41:19Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 387,988 | <p>No, I've never had to rewrite. In fact, I started using Python in Maya 8.5. Before Maya 8, the only scripting language available was the built in MEL (Maya Expression Language). Python is actually faster than the built in language that it wraps.</p>
<p>Python's ability to work with complex data types also made i... | 1 | 2008-12-23T02:14:06Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 390,700 | <p>This kind of question is likely to start a religious war among language people so let me answer it a little bit differently.</p>
<p>For most cases in today's computing environments your choice of programming language should be based on what you can program efficiently, program well and what makes you happy not the ... | 4 | 2008-12-24T03:55:24Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 393,185 | <p>A month ago i had this little program written in Python (for work) that analyzes logs.
When then number of log files grew, the program begun to be very slow and i thought i could rewrite it in Java.</p>
<p>I was very interesting. It took a whole day to migrate the same algorithm from Python to Java. At the end of t... | 1 | 2008-12-25T20:57:40Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 393,200 | <p>I once had to write a pseudo-random number generator for a simulator. I wrote it in Python first, but Python proved to be way too slow; I ended up rewriting it in C, and even that was slow, but not nearly as slow as Python.</p>
<p>Luckily, it's fairly easy to bridge Python and C, so I was able to write the PRNG as ... | 1 | 2008-12-25T21:29:04Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 397,300 | <p>The following link provides an on going comparison between a number of computer languages. It should give you an idea of some of Python's strengths and weaknesses across different problem domains.</p>
<p><a href="http://shootout.alioth.debian.org/gp4/" rel="nofollow">Computer Language Benchmarks Game </a></p>
| 1 | 2008-12-29T07:56:05Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 397,313 | <p>I'm in the process of rewriting the Perl program <a href="http://www.openkore.com" rel="nofollow">OpenKore</a> in Python under the name <a href="http://erok.sf.net" rel="nofollow">Erok</a> (reverse of the original <a href="http://kore.sf.net" rel="nofollow">Kore</a>). So far, Python is proving to be an overall bette... | 1 | 2008-12-29T08:07:11Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 478,872 | <p>Adding my $0.02 for the record.</p>
<p>My work involves developing numeric models that run over 100's of gigabytes of data. The hard problems are in coming up with a revenue-generating solution quickly (i.e. time-to-market). To be commercially successful the solution also has to <em>execute</em> quickly (compute th... | 14 | 2009-01-26T04:54:13Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 501,942 | <p>I am developing in python for several years now. Recently i had to list all files in a directory and build a struct with filename, size, attributes and modification date. I did this with <code>os.listdir</code> and <code>os.stat</code>. The code was quite fast, but the more entries in the directories, the slower my ... | 2 | 2009-02-02T00:56:30Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 673,767 | <p>While implementing a specialized memcache server for a certain datatype, storage backend would be more memory efficient and lookup time could be decreased by bit wise lookup operations (<em>i.e: O(1) lookups</em>).</p>
<p>I wrote all the protocol implementation and event driven daemon part with Python within 2 days... | 4 | 2009-03-23T15:12:34Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 1,900,043 | <p>I generally don't rewrite to C before I :</p>
<ul>
<li>profile</li>
<li>rewrite with bette algorithms (generally this is enough)</li>
<li>rewrite python code with low level performance in mind (but never to the point of having non pythonic / non readable code)</li>
<li>spend some time rechecking a library cannot d... | 0 | 2009-12-14T10:09:46Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Python Performance - have you ever had to rewrite in something else? | 386,655 | <p>Has anyone ever had code in Python, that turned out not to perform fast enough?</p>
<p>I mean, you were forced to <em>choose another language</em> because of it?</p>
<p>We are investigating using Python for a couple of larger projects, and my feeling is that in most cases, Python is plenty fast enough for most sce... | 42 | 2008-12-22T16:23:13Z | 3,122,149 | <p>Yes, twice:</p>
<ul>
<li><p>An audio DSP application I wound up completely rewriting in C++ because I couldn't get appropriate performance in Python; I don't consider the Python implementation wasted because it let me prototype the concept very easily, and the C++ port went smoothly because I had a working referenc... | 2 | 2010-06-25T23:55:49Z | [
"python",
"performance",
"optimization",
"rewrite"
] |
Tricky Python string literals in passing parameter to timeit.Timer() function | 386,664 | <p>I'm having a hard time with the setup statement in Python's timeit.Timer(stmt, setup_stmt). I appreciate any help to get me out of this tricky problem:</p>
<p>So my sniplet looks like this:</p>
<pre><code>def compare(string1, string2):
# compare 2 strings
if __name__ = '__main__':
str1 = "This string has ... | 4 | 2008-12-22T16:25:42Z | 386,718 | <p>Consider This as an alternative.</p>
<pre><code>t = timeit.Timer('compare(p1, p2)', "from __main__ import compare; p1=%r; p2=%r" % (str1,str2))
</code></pre>
<p>The <code>%r</code> uses the repr for the string, which Python always quotes and escapes correctly.</p>
<p>EDIT: Fixed code by changing a comma to a semi... | 5 | 2008-12-22T16:47:19Z | [
"python",
"string-literals",
"timeit"
] |
Tricky Python string literals in passing parameter to timeit.Timer() function | 386,664 | <p>I'm having a hard time with the setup statement in Python's timeit.Timer(stmt, setup_stmt). I appreciate any help to get me out of this tricky problem:</p>
<p>So my sniplet looks like this:</p>
<pre><code>def compare(string1, string2):
# compare 2 strings
if __name__ = '__main__':
str1 = "This string has ... | 4 | 2008-12-22T16:25:42Z | 387,911 | <p>Why bother quoting the strings at all? Just use them directly.
ie. change your last line to:</p>
<pre><code>t = timeit.Timer('compare(str1, str2)', "from __main__ import compare, str1, str2")
</code></pre>
| 2 | 2008-12-23T01:14:42Z | [
"python",
"string-literals",
"timeit"
] |
How do I convert part of a python tuple (byte array) into an integer | 386,753 | <p>I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:</p>
<p>response = (0, 0, 117, 143, 6)</p>
<p>The first 4 values are a 32-bit int telling me how many bytes have been used and the last valu... | 4 | 2008-12-22T17:06:00Z | 386,763 | <p>See <a href="http://stackoverflow.com/questions/5415/">Convert Bytes to Floating Point Numbers in Python</a> </p>
<p>You probably want to use the struct module, e.g.</p>
<pre><code>import struct
response = (0, 0, 117, 143, 6)
struct.unpack(">I", ''.join([chr(x) for x in response[:-1]]))
</code></pre>
<p>Assum... | 9 | 2008-12-22T17:10:02Z | [
"python",
"tuples"
] |
How do I convert part of a python tuple (byte array) into an integer | 386,753 | <p>I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:</p>
<p>response = (0, 0, 117, 143, 6)</p>
<p>The first 4 values are a 32-bit int telling me how many bytes have been used and the last valu... | 4 | 2008-12-22T17:06:00Z | 386,830 | <p>Would,</p>
<blockquote>
<p>num = (response[0] << 24) +
(response[1] << 16) + (response[2] <<
8) + response[3]</p>
</blockquote>
<p>meet your needs?</p>
<p>aid</p>
| 13 | 2008-12-22T17:36:02Z | [
"python",
"tuples"
] |
How do I convert part of a python tuple (byte array) into an integer | 386,753 | <p>I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:</p>
<p>response = (0, 0, 117, 143, 6)</p>
<p>The first 4 values are a 32-bit int telling me how many bytes have been used and the last valu... | 4 | 2008-12-22T17:06:00Z | 386,841 | <p>OK, You don't specify the endinanness or whether the integer is signed or and it (perhaps) is faster to with the struct module but:</p>
<pre><code>b = (8, 1, 0, 0)
sum(b[i] << (i * 8) for i in range(4))
</code></pre>
| 4 | 2008-12-22T17:39:55Z | [
"python",
"tuples"
] |
How do I convert part of a python tuple (byte array) into an integer | 386,753 | <p>I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:</p>
<p>response = (0, 0, 117, 143, 6)</p>
<p>The first 4 values are a 32-bit int telling me how many bytes have been used and the last valu... | 4 | 2008-12-22T17:06:00Z | 386,998 | <p>You could also make use of the array module</p>
<pre><code>import struct
from array import array
response = (0, 0, 117, 143, 6)
a = array('B', response[:4])
struct.unpack('>I', a)
(30095L,)
</code></pre>
| 4 | 2008-12-22T18:41:19Z | [
"python",
"tuples"
] |
How do I convert part of a python tuple (byte array) into an integer | 386,753 | <p>I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:</p>
<p>response = (0, 0, 117, 143, 6)</p>
<p>The first 4 values are a 32-bit int telling me how many bytes have been used and the last valu... | 4 | 2008-12-22T17:06:00Z | 903,377 | <p>How about using the map function:</p>
<pre><code>a = (0, 0, 117, 143, 6)
b = []
map(b.append, a)
</code></pre>
<p>Also, I don't know if this is you are looking for:</p>
<pre><code>response = (0, 0, 117, 143, 6)
response[0:4]
</code></pre>
| 0 | 2009-05-24T08:56:54Z | [
"python",
"tuples"
] |
How do I convert part of a python tuple (byte array) into an integer | 386,753 | <p>I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:</p>
<p>response = (0, 0, 117, 143, 6)</p>
<p>The first 4 values are a 32-bit int telling me how many bytes have been used and the last valu... | 4 | 2008-12-22T17:06:00Z | 5,737,783 | <p>This looks like a job for reduce!</p>
<p>What you basically need is to, bit-shift a byte at a time, and then add (addition) the next byte in the sequence. </p>
<pre><code>a = (0, 0, 117, 143, 6)
reduce(lambda x, y: (x<<8) + y, a)
7704326
</code></pre>
| 3 | 2011-04-20T23:26:12Z | [
"python",
"tuples"
] |
Evaluate environment variables into a string | 386,934 | <p>I have a string representing a path. Because this application is used on Windows, OSX and Linux, we've defined environment variables to properly map volumes from the different file systems. The result is:</p>
<pre><code>"$C/test/testing"
</code></pre>
<p>What I want to do is evaluate the environment variables in... | 24 | 2008-12-22T18:18:05Z | 386,978 | <p>Use <a href="http://docs.python.org/library/os.path.html#os.path.expandvars">os.path.expandvars</a> to expand the environment variables in the string, for example:</p>
<pre><code>>>> os.path.expandvars('$C/test/testing')
'/stackoverflow/test/testing'
</code></pre>
| 41 | 2008-12-22T18:35:16Z | [
"python",
"filesystems",
"environment-variables"
] |
Using user input to find information in a Mysql database | 387,606 | <p>I need to design a program using python that will ask the user for a barcode. Then, using this barcode, it will search a mysql to find its corresponding product. </p>
<p>I am a bit stuck on how to get started. Does anyone have any tips for me?</p>
| 0 | 2008-12-22T22:37:08Z | 387,622 | <p>A barcode is simply a graphical representation of a series of characters (alphanumeric)</p>
<p>So if you have a method for users to enter this code (a barcode scanner), then its just an issue of querying the mysql database for the character string.</p>
| 1 | 2008-12-22T22:40:49Z | [
"python",
"sql",
"user-input"
] |
Using user input to find information in a Mysql database | 387,606 | <p>I need to design a program using python that will ask the user for a barcode. Then, using this barcode, it will search a mysql to find its corresponding product. </p>
<p>I am a bit stuck on how to get started. Does anyone have any tips for me?</p>
| 0 | 2008-12-22T22:37:08Z | 387,694 | <p>That is a very ambiguous question. What you want can be done in many ways depending on what you actually want to do.</p>
<p>How are your users going to enter the bar code? Are they going to use a bar code scanner? Are they entering the bar code numbers manually? </p>
<p>Is this going to run on a desktop/laptop com... | 0 | 2008-12-22T23:01:49Z | [
"python",
"sql",
"user-input"
] |
Using user input to find information in a Mysql database | 387,606 | <p>I need to design a program using python that will ask the user for a barcode. Then, using this barcode, it will search a mysql to find its corresponding product. </p>
<p>I am a bit stuck on how to get started. Does anyone have any tips for me?</p>
| 0 | 2008-12-22T22:37:08Z | 387,800 | <p>To start with, treat the barcode input as plain old text. </p>
<p>It has been quite a while since I worked with barcode scanners, but I doubt they have changed that much, the older ones used to just piggyback on the keyboard input, so from a programming perspective, the net result was a stream of characters in th... | 0 | 2008-12-22T23:47:58Z | [
"python",
"sql",
"user-input"
] |
Using user input to find information in a Mysql database | 387,606 | <p>I need to design a program using python that will ask the user for a barcode. Then, using this barcode, it will search a mysql to find its corresponding product. </p>
<p>I am a bit stuck on how to get started. Does anyone have any tips for me?</p>
| 0 | 2008-12-22T22:37:08Z | 393,356 | <p>Use <a href="http://mysql-python.sourceforge.net/MySQLdb.html#using-and-extending" rel="nofollow">python-mysql</a>. It is a <a href="http://www.python.org/dev/peps/pep-0249/" rel="nofollow">dbapi-compatible</a> module that lets you talk to the database.</p>
<pre><code>import MySQLdb
user_input = raw_input("Please ... | 3 | 2008-12-26T01:07:53Z | [
"python",
"sql",
"user-input"
] |
Mysql Connection, one or many? | 387,619 | <p>I'm writing a script in python which basically queries WMI and updates the information in a mysql database. One of those "write something you need" to learn to program exercises.</p>
<p>In case something breaks in the middle of the script, for example, the remote computer turns off, it's separated out into functio... | 2 | 2008-12-22T22:40:09Z | 387,735 | <p>I don't think that there is "better" solution. Its too early to think about resources. And since wmi is quite slow ( in comparison to sql connection ) the db is not an issue.</p>
<p>Just make it work. And then make it better.</p>
<p>The good thing about working with open connection here, is that the "natural" solu... | 2 | 2008-12-22T23:16:46Z | [
"python",
"mysql"
] |
Mysql Connection, one or many? | 387,619 | <p>I'm writing a script in python which basically queries WMI and updates the information in a mysql database. One of those "write something you need" to learn to program exercises.</p>
<p>In case something breaks in the middle of the script, for example, the remote computer turns off, it's separated out into functio... | 2 | 2008-12-22T22:40:09Z | 387,759 | <p>Think for a moment about the following scenario:</p>
<pre><code>for dataItem in dataSet:
update(dataItem)
</code></pre>
<p>If you open and close your connection inside of the <em>update</em> function and your <em>dataSet</em> contains a thousand items then you will destroy the performance of your application a... | 1 | 2008-12-22T23:26:00Z | [
"python",
"mysql"
] |
Mysql Connection, one or many? | 387,619 | <p>I'm writing a script in python which basically queries WMI and updates the information in a mysql database. One of those "write something you need" to learn to program exercises.</p>
<p>In case something breaks in the middle of the script, for example, the remote computer turns off, it's separated out into functio... | 2 | 2008-12-22T22:40:09Z | 387,932 | <p>"However, opening and closing the connection with each update seems more 'neat'. " </p>
<p>It's also a huge amount of overhead -- and there's no actual benefit.</p>
<p>Creating and disposing of connections is relatively expensive. More importantly, what's the actual reason? How does it improve, simplify, clarify... | 6 | 2008-12-23T01:36:24Z | [
"python",
"mysql"
] |
Mysql Connection, one or many? | 387,619 | <p>I'm writing a script in python which basically queries WMI and updates the information in a mysql database. One of those "write something you need" to learn to program exercises.</p>
<p>In case something breaks in the middle of the script, for example, the remote computer turns off, it's separated out into functio... | 2 | 2008-12-22T22:40:09Z | 389,364 | <p>Useful clues in S.Lott's and Igal Serban's answers. I think you should first find out your actual requirements and code accordingly.</p>
<p>Just to mention a different strategy; some applications keep a pool of database (or whatever) connections and in case of a transaction just pull one from that pool. It seems ra... | 0 | 2008-12-23T16:36:23Z | [
"python",
"mysql"
] |
What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django? | 387,686 | <ul>
<li>I have a <em>Client</em> and <em>Groupe</em> Model.</li>
<li>A <em>Client</em> can be part of multiple <em>groups</em>.</li>
<li><em>Clients</em> that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (<em>ClientGroupe</em>) comes in with th... | 30 | 2008-12-22T22:58:24Z | 478,384 | <p>When you save your form, you save Client object. Now if you want to assign client to the group you should do this:</p>
<pre><code>clientgroupe = ClientGroupe.objects.create(client=client_instance, groupe=groupe_instance, dt=datetime.datetime.now())
</code></pre>
<p>where client_instance and groupe_instance your cl... | 0 | 2009-01-25T21:56:02Z | [
"python",
"django",
"django-models",
"django-templates",
"django-forms"
] |
What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django? | 387,686 | <ul>
<li>I have a <em>Client</em> and <em>Groupe</em> Model.</li>
<li>A <em>Client</em> can be part of multiple <em>groups</em>.</li>
<li><em>Clients</em> that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (<em>ClientGroupe</em>) comes in with th... | 30 | 2008-12-22T22:58:24Z | 559,703 | <p>You probably need to remove the ManyToMany field from your Client model, or else carefully exclude it from your form. Unfortunately, the default widget for the ManyToMany field cannot populate the ClientGroupe Model properly (even if the missing field, dt, had been set to autonow=True). This is something you'll ei... | 4 | 2009-02-18T03:15:42Z | [
"python",
"django",
"django-models",
"django-templates",
"django-forms"
] |
What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django? | 387,686 | <ul>
<li>I have a <em>Client</em> and <em>Groupe</em> Model.</li>
<li>A <em>Client</em> can be part of multiple <em>groups</em>.</li>
<li><em>Clients</em> that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (<em>ClientGroupe</em>) comes in with th... | 30 | 2008-12-22T22:58:24Z | 932,234 | <p>If you use the save method right now, Django will try to save using the manager (which Django doesn't allow). Unfortunately, the behavior you want is a little bit trickier than what <code>ModelForm</code> does by default. What you need to do is create a <em>formset</em>.</p>
<p>First of all, you will need to chan... | 18 | 2009-05-31T15:27:30Z | [
"python",
"django",
"django-models",
"django-templates",
"django-forms"
] |
What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django? | 387,686 | <ul>
<li>I have a <em>Client</em> and <em>Groupe</em> Model.</li>
<li>A <em>Client</em> can be part of multiple <em>groups</em>.</li>
<li><em>Clients</em> that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (<em>ClientGroupe</em>) comes in with th... | 30 | 2008-12-22T22:58:24Z | 3,882,808 | <pre><code>â¦
if form.is_valid():
client_mod = form.save(commit=False)
client_mod.save()
for groupe in form.cleaned_data.get('groupes'):
clientgroupe = ClientGroupe(client=client_mod, groupe=groupe)
clientgroupe.save()
â¦
</code></pre>
| 10 | 2010-10-07T14:41:20Z | [
"python",
"django",
"django-models",
"django-templates",
"django-forms"
] |
What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django? | 387,686 | <ul>
<li>I have a <em>Client</em> and <em>Groupe</em> Model.</li>
<li>A <em>Client</em> can be part of multiple <em>groups</em>.</li>
<li><em>Clients</em> that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (<em>ClientGroupe</em>) comes in with th... | 30 | 2008-12-22T22:58:24Z | 21,145,127 | <p>I'm providing an alternative solution due to issues I encountered with forms_valid not being called:</p>
<pre><code>class SplingCreate(forms.ModelForm):
class Meta:
model = SplingModel
fields = ('Link', 'Genres', 'Image', 'ImageURL',)
def save(self, commit=True):
from django.forms.models import save_in... | 0 | 2014-01-15T18:11:46Z | [
"python",
"django",
"django-models",
"django-templates",
"django-forms"
] |
printing a list of persons with more than one home, each home with more than one phone number | 387,991 | <p>I have a class Person which can have several Homes, each one with one or many Phone numbers.</p>
<p>I have defined the classes, but now i am trying to create a view wich list every person, with all its homes and all the phone numbers for each home address... something like:</p>
<pre><code>john smith
123 fake str
... | 1 | 2008-12-23T02:15:08Z | 388,009 | <p>You have what appears to be three nested collections: Person, Home, Phone Number.</p>
<p>Step 1 - How would you write this in a view function?</p>
<pre><code>for p in Person.objects.all():
print "person", p
for h in p.home_address_set.all():
print " home", h
for ph in h.phone_set.all():
... | 6 | 2008-12-23T02:25:53Z | [
"python",
"django",
"django-templates"
] |
How to gracefully deal with failed future feature (__future__) imports due to old interpreter version? | 388,069 | <p>How do you gracefully handle failed future feature imports? If a user is running using Python 2.5 and the first statement in my module is:</p>
<pre><code>from __future__ import print_function
</code></pre>
<p>Compiling this module for Python 2.5 will fail with a:</p>
<pre><code> File "__init__.py", line 1
fr... | 68 | 2008-12-23T03:10:51Z | 388,083 | <p>"I'd like to inform the user that they need to rerun the program with Python >= 2.6 and maybe provide some instructions on how to do so."</p>
<p>Isn't that what a README file is for?</p>
<p>Here's your alternative. A "wrapper": a little blob of Python that checks the environment before running your target aop.</p... | 57 | 2008-12-23T03:20:03Z | [
"python",
"python-import"
] |
How to gracefully deal with failed future feature (__future__) imports due to old interpreter version? | 388,069 | <p>How do you gracefully handle failed future feature imports? If a user is running using Python 2.5 and the first statement in my module is:</p>
<pre><code>from __future__ import print_function
</code></pre>
<p>Compiling this module for Python 2.5 will fail with a:</p>
<pre><code> File "__init__.py", line 1
fr... | 68 | 2008-12-23T03:10:51Z | 3,037,220 | <p>A rather hacky but simple method I've used before is to exploit the fact that byte literals were introduced in Python 2.6 and use something like this near the start of the file:</p>
<pre><code>b'This module needs Python 2.6 or later. Please do xxx.'
</code></pre>
<p>This is harmless in Python 2.6 or later, but a <... | 46 | 2010-06-14T12:36:33Z | [
"python",
"python-import"
] |
How to gracefully deal with failed future feature (__future__) imports due to old interpreter version? | 388,069 | <p>How do you gracefully handle failed future feature imports? If a user is running using Python 2.5 and the first statement in my module is:</p>
<pre><code>from __future__ import print_function
</code></pre>
<p>Compiling this module for Python 2.5 will fail with a:</p>
<pre><code> File "__init__.py", line 1
fr... | 68 | 2008-12-23T03:10:51Z | 7,252,783 | <p>Just put a comment on the same line with the <code>"from __future__ import ..."</code>, like this:</p>
<pre><code>from __future__ import print_function, division # We require Python 2.6 or later
</code></pre>
<p>Since Python displays the line containing the error, if you try to run the module with Python 2.5 you'... | 38 | 2011-08-31T05:10:18Z | [
"python",
"python-import"
] |
Python: flush a buffer before program termination via a finalizer | 388,154 | <p>I keep a cache of transactions to flush (to persistent storage) on the event of a watermark or object finalization. Since <code>__del__</code> is <a href="http://docs.python.org/reference/datamodel.html?highlight=__del__#object.__del__" rel="nofollow">no longer guaranteed to be called</a> on every object, is the app... | 3 | 2008-12-23T04:15:05Z | 388,224 | <p>If you have to handle ressources the prefered way is to have an explicit call to a <code>close()</code> or <code>finalize()</code> method. Have a look at the <code>with</code> statement to abstract that. In your case the <code>weakref</code> module might be an option. The cached object can be garbage collected by t... | 4 | 2008-12-23T05:10:35Z | [
"python",
"buffer",
"destructor",
"finalizer"
] |
Python: flush a buffer before program termination via a finalizer | 388,154 | <p>I keep a cache of transactions to flush (to persistent storage) on the event of a watermark or object finalization. Since <code>__del__</code> is <a href="http://docs.python.org/reference/datamodel.html?highlight=__del__#object.__del__" rel="nofollow">no longer guaranteed to be called</a> on every object, is the app... | 3 | 2008-12-23T04:15:05Z | 388,323 | <p>I think <code>atexit</code> is the way to go here.</p>
| 0 | 2008-12-23T06:51:34Z | [
"python",
"buffer",
"destructor",
"finalizer"
] |
Python: flush a buffer before program termination via a finalizer | 388,154 | <p>I keep a cache of transactions to flush (to persistent storage) on the event of a watermark or object finalization. Since <code>__del__</code> is <a href="http://docs.python.org/reference/datamodel.html?highlight=__del__#object.__del__" rel="nofollow">no longer guaranteed to be called</a> on every object, is the app... | 3 | 2008-12-23T04:15:05Z | 388,609 | <p>Put the following in a file called <code>destructor.py</code></p>
<pre><code>import atexit
objects = []
def _destructor():
global objects
for obj in objects:
obj.destroy()
del objects
atexit.register(_destructor)
</code></pre>
<p>now use it this way:</p>
<pre><code>import destructor
class ... | 2 | 2008-12-23T10:46:14Z | [
"python",
"buffer",
"destructor",
"finalizer"
] |
Python: flush a buffer before program termination via a finalizer | 388,154 | <p>I keep a cache of transactions to flush (to persistent storage) on the event of a watermark or object finalization. Since <code>__del__</code> is <a href="http://docs.python.org/reference/datamodel.html?highlight=__del__#object.__del__" rel="nofollow">no longer guaranteed to be called</a> on every object, is the app... | 3 | 2008-12-23T04:15:05Z | 388,959 | <p>I would say <code>atexit</code> or try and see if you can refactor the code into being able to be expressed using a <code>with_statement</code> which is in the <code>__future__</code> in 2.5 and in 2.6 by default. 2.5 includes a module contextlib to simplify things a bit. I've done something like this when using C... | 3 | 2008-12-23T14:21:43Z | [
"python",
"buffer",
"destructor",
"finalizer"
] |
Python: flush a buffer before program termination via a finalizer | 388,154 | <p>I keep a cache of transactions to flush (to persistent storage) on the event of a watermark or object finalization. Since <code>__del__</code> is <a href="http://docs.python.org/reference/datamodel.html?highlight=__del__#object.__del__" rel="nofollow">no longer guaranteed to be called</a> on every object, is the app... | 3 | 2008-12-23T04:15:05Z | 390,177 | <p>If you don't need your object to be alive at the time you perform the flush, you could use <a href="http://docs.python.org/library/weakref.html" rel="nofollow">weak references</a></p>
<p>This is similar to your proposed solution, but rather than using a real reference, store a list of weak references, with a callba... | 2 | 2008-12-23T21:52:16Z | [
"python",
"buffer",
"destructor",
"finalizer"
] |
Tidier way of trying to import a module from multiple locations? | 388,173 | <p>Is there a way to tidy-up the following code, rather than a series of nested try/except statements?</p>
<pre><code>try:
import simplejson as json
except ImportError:
try:
import json
except ImportError:
try:
from django.utils import simplejson as json
except:
... | 8 | 2008-12-23T04:25:09Z | 388,360 | <p>I found the following function at <a href="http://mail.python.org/pipermail/python-list/2007-May/441896.html" rel="nofollow">http://mail.python.org/pipermail/python-list/2007-May/441896.html</a>. It seems to work quite well, and I'm pretty sure the way its importing won't stomp on any existing imports you might alr... | 6 | 2008-12-23T07:28:56Z | [
"python",
"refactoring",
"python-import"
] |
Tidier way of trying to import a module from multiple locations? | 388,173 | <p>Is there a way to tidy-up the following code, rather than a series of nested try/except statements?</p>
<pre><code>try:
import simplejson as json
except ImportError:
try:
import json
except ImportError:
try:
from django.utils import simplejson as json
except:
... | 8 | 2008-12-23T04:25:09Z | 388,511 | <pre><code>def import_any(*mod_list):
res = None
for mod in mod_list:
try:
res = __import__(mod)
return res
except ImportError:
pass
raise ImportError("Requires one of " + ', '.join(mod_list))
json = import_any('simplejson', 'json', 'django.utils.simplejs... | 3 | 2008-12-23T09:45:03Z | [
"python",
"refactoring",
"python-import"
] |
Tidier way of trying to import a module from multiple locations? | 388,173 | <p>Is there a way to tidy-up the following code, rather than a series of nested try/except statements?</p>
<pre><code>try:
import simplejson as json
except ImportError:
try:
import json
except ImportError:
try:
from django.utils import simplejson as json
except:
... | 8 | 2008-12-23T04:25:09Z | 388,606 | <p>I appreciate the pretty functions for doing this, but the pattern you illustrate in the original question is the most commonly used pattern for this requirement. You can see it used in many open source projects.</p>
<p>I suggest you stick with it. Remember "ugly" is not always "bad".</p>
| 1 | 2008-12-23T10:45:05Z | [
"python",
"refactoring",
"python-import"
] |
Tidier way of trying to import a module from multiple locations? | 388,173 | <p>Is there a way to tidy-up the following code, rather than a series of nested try/except statements?</p>
<pre><code>try:
import simplejson as json
except ImportError:
try:
import json
except ImportError:
try:
from django.utils import simplejson as json
except:
... | 8 | 2008-12-23T04:25:09Z | 31,116,274 | <p>I've come up with a simple alternative that doesn't rely on defining functions:</p>
<pre><code># Create a dummy enclosing
while True:
try:
import simplejson as json
break
except:
pass
try:
import json
break
except:
pass
try:
from django.u... | 0 | 2015-06-29T12:53:54Z | [
"python",
"refactoring",
"python-import"
] |
How can i create a lookup in Django? | 388,233 | <p>I have a Question model & Form, one of the fields in this model is <strong>userid=ForeignKey(User)</strong>, this Works perfectly well on the Question Model, am able to pick the user from a drop down. </p>
<p>But kind a tricky when i want to list the question from the model, which is the best way to <strong>loo... | 0 | 2008-12-23T05:18:07Z | 388,296 | <p>I find your question vague. If you want to fetch all <code>Question</code> instances that are related to a particular <code>User</code> instance given a <code>user_name</code>, you can do thus:</p>
<pre><code>questions = Question.objects.filter( userid__username='user_name' )
</code></pre>
<p>If you already have a... | 0 | 2008-12-23T06:20:27Z | [
"python",
"django",
"lookup"
] |
How can i create a lookup in Django? | 388,233 | <p>I have a Question model & Form, one of the fields in this model is <strong>userid=ForeignKey(User)</strong>, this Works perfectly well on the Question Model, am able to pick the user from a drop down. </p>
<p>But kind a tricky when i want to list the question from the model, which is the best way to <strong>loo... | 0 | 2008-12-23T05:18:07Z | 389,701 | <p>The name of your field (<code>userid</code> instead of <code>user</code>) makes me think that you may be confused about the behavior of Django's <code>ForeignKey</code>.</p>
<p>If you define a model like this:</p>
<pre><code>from django.contrib.auth.models import User
from django.db import models
class Question(m... | 5 | 2008-12-23T18:35:56Z | [
"python",
"django",
"lookup"
] |
How do I use the built in password reset/change views with my own templates | 388,800 | <p>For example I can point the <code>url '^/accounts/password/reset/$'</code> to <code>django.contrib.auth.views.password_reset</code> with my template filename in the context but I think need to send more context details.</p>
<p>I need to know exactly what context to add for each of the password reset and change vie... | 67 | 2008-12-23T12:47:32Z | 388,811 | <p>The <a href="http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.django.contrib.auth.views.password_change" rel="nofollow">documentation</a> says that there only one context variable, <code>form</code>.</p>
<p>If you're having trouble with login (which is common), the <a href="http://docs.djangopr... | 1 | 2008-12-23T12:54:28Z | [
"python",
"django",
"passwords"
] |
How do I use the built in password reset/change views with my own templates | 388,800 | <p>For example I can point the <code>url '^/accounts/password/reset/$'</code> to <code>django.contrib.auth.views.password_reset</code> with my template filename in the context but I think need to send more context details.</p>
<p>I need to know exactly what context to add for each of the password reset and change vie... | 67 | 2008-12-23T12:47:32Z | 388,858 | <p>If you take a look at the sources for <a href="http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/views.py">django.contrib.auth.views.password_reset</a> you'll see that it uses <a href="http://code.djangoproject.com/browser/django/trunk/django/template/__init__.py"><code>RequestContext</code></a>... | 74 | 2008-12-23T13:21:11Z | [
"python",
"django",
"passwords"
] |
How do I use the built in password reset/change views with my own templates | 388,800 | <p>For example I can point the <code>url '^/accounts/password/reset/$'</code> to <code>django.contrib.auth.views.password_reset</code> with my template filename in the context but I think need to send more context details.</p>
<p>I need to know exactly what context to add for each of the password reset and change vie... | 67 | 2008-12-23T12:47:32Z | 389,679 | <p>You just need to wrap the existing functions and pass in the template you want. For example:</p>
<pre><code>from django.contrib.auth.views import password_reset
def my_password_reset(request, template_name='path/to/my/template'):
return password_reset(request, template_name)
</code></pre>
<p>To see this just ... | 9 | 2008-12-23T18:27:06Z | [
"python",
"django",
"passwords"
] |
How do I use the built in password reset/change views with my own templates | 388,800 | <p>For example I can point the <code>url '^/accounts/password/reset/$'</code> to <code>django.contrib.auth.views.password_reset</code> with my template filename in the context but I think need to send more context details.</p>
<p>I need to know exactly what context to add for each of the password reset and change vie... | 67 | 2008-12-23T12:47:32Z | 14,868,595 | <p>Strongly recommend this article.</p>
<p>I just plugged it in and it worked</p>
<p><a href="http://garmoncheg.blogspot.com.au/2012/07/django-resetting-passwords-with.html">http://garmoncheg.blogspot.com.au/2012/07/django-resetting-passwords-with.html</a></p>
| 22 | 2013-02-14T05:52:07Z | [
"python",
"django",
"passwords"
] |
How do I use the built in password reset/change views with my own templates | 388,800 | <p>For example I can point the <code>url '^/accounts/password/reset/$'</code> to <code>django.contrib.auth.views.password_reset</code> with my template filename in the context but I think need to send more context details.</p>
<p>I need to know exactly what context to add for each of the password reset and change vie... | 67 | 2008-12-23T12:47:32Z | 15,203,487 | <p>I was using this two lines in the url and the template from the admin what i was changing to my need</p>
<pre><code>url(r'^change-password/$', 'django.contrib.auth.views.password_change', {
'template_name': 'password_change_form.html'}, name="password-change"),
url(r'^change-password-done/$', 'django.contrib.au... | 1 | 2013-03-04T14:11:37Z | [
"python",
"django",
"passwords"
] |
How do I use the built in password reset/change views with my own templates | 388,800 | <p>For example I can point the <code>url '^/accounts/password/reset/$'</code> to <code>django.contrib.auth.views.password_reset</code> with my template filename in the context but I think need to send more context details.</p>
<p>I need to know exactly what context to add for each of the password reset and change vie... | 67 | 2008-12-23T12:47:32Z | 16,193,133 | <p>You can do the following:</p>
<ol>
<li>add to your urlpatterns (r'^/accounts/password/reset/$', password_reset)</li>
<li>put your template in '/templates/registration/password_reset_form.html'</li>
<li>make your app come before 'django.contrib.auth' in INSTALLED_APPS</li>
</ol>
<p>Explanation:</p>
<p>When the tem... | 5 | 2013-04-24T13:20:44Z | [
"python",
"django",
"passwords"
] |
Trouble with encoding in emails | 389,398 | <p>I have a little python script that pulls emails from a POP mail address and dumps them into a file (one file one email)</p>
<p>Then a PHP script runs through the files and displays them.</p>
<p>I am having an issue with ISO-8859-1 (Latin-1) encoded email</p>
<p>Here's an example of the text i get: =?iso-8859-1?Q?... | 1 | 2008-12-23T16:46:47Z | 389,408 | <p>That's MIME content, and that's how the email actually looks like, not a bug somewhere. You have to use a MIME decoding library (or decode it yourself manually) on the PHP side of things (which, if I understood correctly, is the one acting as email renderer).</p>
<p>In Python you'd use <a href="http://docs.python.o... | 0 | 2008-12-23T16:50:58Z | [
"python",
"email",
"encoding"
] |
Trouble with encoding in emails | 389,398 | <p>I have a little python script that pulls emails from a POP mail address and dumps them into a file (one file one email)</p>
<p>Then a PHP script runs through the files and displays them.</p>
<p>I am having an issue with ISO-8859-1 (Latin-1) encoded email</p>
<p>Here's an example of the text i get: =?iso-8859-1?Q?... | 1 | 2008-12-23T16:46:47Z | 389,413 | <p>Until very recently, plain Latin-N or utf-N were no allowed in headers which means that they would get to be encoded by a method described at first in <a href="http://www.faqs.org/rfcs/rfc1522.html" rel="nofollow">RFC-1522</a> but it has been superseded later. Accents are encoded either in quoted-printable or in Ba... | 1 | 2008-12-23T16:53:22Z | [
"python",
"email",
"encoding"
] |
Trouble with encoding in emails | 389,398 | <p>I have a little python script that pulls emails from a POP mail address and dumps them into a file (one file one email)</p>
<p>Then a PHP script runs through the files and displays them.</p>
<p>I am having an issue with ISO-8859-1 (Latin-1) encoded email</p>
<p>Here's an example of the text i get: =?iso-8859-1?Q?... | 1 | 2008-12-23T16:46:47Z | 390,688 | <p>You can use the python email library (python 2.5+) to avoid these problems:</p>
<pre><code>import email
import poplib
import random
from cStringIO import StringIO
from email.generator import Generator
pop = poplib.POP3(server)
mail_count = len(pop.list()[1])
for message_num in xrange(mail_count):
message = "... | 3 | 2008-12-24T03:39:06Z | [
"python",
"email",
"encoding"
] |
Trouble with encoding in emails | 389,398 | <p>I have a little python script that pulls emails from a POP mail address and dumps them into a file (one file one email)</p>
<p>Then a PHP script runs through the files and displays them.</p>
<p>I am having an issue with ISO-8859-1 (Latin-1) encoded email</p>
<p>Here's an example of the text i get: =?iso-8859-1?Q?... | 1 | 2008-12-23T16:46:47Z | 391,031 | <p>That's the MIME encoding of headers, <a href="http://www.ietf.org/rfc/rfc2047.txt" rel="nofollow">RFC 2047</a>. Here is how to decode it in Python:</p>
<pre><code>import email.Header
import sys
header_and_encoding = email.Header.decode_header(sys.stdin.readline())
for part in header_and_encoding:
if part[1] is... | 2 | 2008-12-24T08:44:28Z | [
"python",
"email",
"encoding"
] |
Trouble with encoding in emails | 389,398 | <p>I have a little python script that pulls emails from a POP mail address and dumps them into a file (one file one email)</p>
<p>Then a PHP script runs through the files and displays them.</p>
<p>I am having an issue with ISO-8859-1 (Latin-1) encoded email</p>
<p>Here's an example of the text i get: =?iso-8859-1?Q?... | 1 | 2008-12-23T16:46:47Z | 397,621 | <p>There is a better way to do this, but this is what i ended up with. Thanks for your help guys.</p>
<pre><code>import poplib, quopri
import random, md5
import sys, rfc822, StringIO
import email
from email.Generator import Generator
user = "email@example.com"
password = "password"
server = "mail.example.com"
# conn... | 2 | 2008-12-29T12:18:41Z | [
"python",
"email",
"encoding"
] |
Producing documentation for Python classes | 389,688 | <p>I'm about to start a project where I will be the only one doing actual code and two less experienced programmers (scary to think of myself as experienced!) will be watching and making suggestions on the program in general.</p>
<p>Is there a good (free) system that I can use to provide documentation for classes and ... | 11 | 2008-12-23T18:30:53Z | 389,704 | <p>I have used <a href="http://epydoc.sourceforge.net/">epydoc</a> to generate documentation for Python modules from embedded docstrings. It's pretty easy to use and generates nice looking output in multiple formats.</p>
| 12 | 2008-12-23T18:37:06Z | [
"python",
"data-structures",
"documentation"
] |
Producing documentation for Python classes | 389,688 | <p>I'm about to start a project where I will be the only one doing actual code and two less experienced programmers (scary to think of myself as experienced!) will be watching and making suggestions on the program in general.</p>
<p>Is there a good (free) system that I can use to provide documentation for classes and ... | 11 | 2008-12-23T18:30:53Z | 389,706 | <p>python.org is now using <a href="http://sphinx.pocoo.org/" rel="nofollow">sphinx</a> for it's documentation.</p>
<p>I personally like the output of sphinx over epydoc. I also feel the restructured text is easier to read in the docstrings than the epydoc markup.</p>
| 11 | 2008-12-23T18:37:36Z | [
"python",
"data-structures",
"documentation"
] |
Producing documentation for Python classes | 389,688 | <p>I'm about to start a project where I will be the only one doing actual code and two less experienced programmers (scary to think of myself as experienced!) will be watching and making suggestions on the program in general.</p>
<p>Is there a good (free) system that I can use to provide documentation for classes and ... | 11 | 2008-12-23T18:30:53Z | 389,711 | <p>See answers to <a href="http://stackoverflow.com/questions/58622/can-i-document-python-code-with-doxygen-and-does-it-make-sense">can-i-document-python-code-with-doxygen-and-does-it-make-sense</a>, especially those mentioning <a href="http://epydoc.sourceforge.net/" rel="nofollow">Epydoc</a> and <a href="http://codes... | 2 | 2008-12-23T18:38:30Z | [
"python",
"data-structures",
"documentation"
] |
Producing documentation for Python classes | 389,688 | <p>I'm about to start a project where I will be the only one doing actual code and two less experienced programmers (scary to think of myself as experienced!) will be watching and making suggestions on the program in general.</p>
<p>Is there a good (free) system that I can use to provide documentation for classes and ... | 11 | 2008-12-23T18:30:53Z | 390,678 | <p><a href="http://sphinx.pocoo.org/" rel="nofollow">Sphinx</a> can be useful for generating very verbose and informative docs that go above and beyond what simple API docs give you. However in many cases you'll be better served to use a wiki for these kind of documents. Also consider writing functional tests which dem... | 4 | 2008-12-24T03:23:08Z | [
"python",
"data-structures",
"documentation"
] |
Producing documentation for Python classes | 389,688 | <p>I'm about to start a project where I will be the only one doing actual code and two less experienced programmers (scary to think of myself as experienced!) will be watching and making suggestions on the program in general.</p>
<p>Is there a good (free) system that I can use to provide documentation for classes and ... | 11 | 2008-12-23T18:30:53Z | 900,314 | <p>I use Sphinx for my project, not only because it looks good, but also because Sphinx encourages writing documentation for <em>humans</em> to read, not just <em>computers</em>.</p>
<p>I find the JavaDoc-style documentation produced by tools like epydoc quite sad to read. It happens all too often that the programmer ... | 3 | 2009-05-22T22:43:16Z | [
"python",
"data-structures",
"documentation"
] |
How can I read Perl data structures from Python? | 389,945 | <p>I've often seen people use Perl data structures in lieu of configuration files; i.e. a lone file containing only:</p>
<pre><code>%config = (
'color' => 'red',
'numbers' => [5, 8],
qr/^spam/ => 'eggs'
);
</code></pre>
<p>What's the best way to convert the contents of these files into Python-equ... | 9 | 2008-12-23T20:11:08Z | 389,970 | <p>Is using pure Python a requirement? If not, you can load it in Perl and convert it to YAML or JSON. Then use PyYAML or something similar to load them in Python.</p>
| 17 | 2008-12-23T20:19:38Z | [
"python",
"perl",
"configuration",
"data-structures"
] |
How can I read Perl data structures from Python? | 389,945 | <p>I've often seen people use Perl data structures in lieu of configuration files; i.e. a lone file containing only:</p>
<pre><code>%config = (
'color' => 'red',
'numbers' => [5, 8],
qr/^spam/ => 'eggs'
);
</code></pre>
<p>What's the best way to convert the contents of these files into Python-equ... | 9 | 2008-12-23T20:11:08Z | 390,062 | <p>Not sure what the use case is. Here's my assumption: you're going to do a one-time conversion from Perl to Python.</p>
<p>Perl has this</p>
<pre><code>%config = (
'color' => 'red',
'numbers' => [5, 8],
qr/^spam/ => 'eggs'
);
</code></pre>
<p>In Python, it would be</p>
<pre><code>config = {
... | 8 | 2008-12-23T20:56:23Z | [
"python",
"perl",
"configuration",
"data-structures"
] |
How can I read Perl data structures from Python? | 389,945 | <p>I've often seen people use Perl data structures in lieu of configuration files; i.e. a lone file containing only:</p>
<pre><code>%config = (
'color' => 'red',
'numbers' => [5, 8],
qr/^spam/ => 'eggs'
);
</code></pre>
<p>What's the best way to convert the contents of these files into Python-equ... | 9 | 2008-12-23T20:11:08Z | 390,441 | <p>I've also found <a href="http://search.cpan.org/dist/pyperl/" rel="nofollow">PyPerl</a>, but it doesn't seem to be maintained. I guess something like this is what I was looking for -- a module that did some basic interpretation of Perl and passed the result as a Python object. A Perl interpreter that died on anythin... | 0 | 2008-12-23T23:57:12Z | [
"python",
"perl",
"configuration",
"data-structures"
] |
How can I read Perl data structures from Python? | 389,945 | <p>I've often seen people use Perl data structures in lieu of configuration files; i.e. a lone file containing only:</p>
<pre><code>%config = (
'color' => 'red',
'numbers' => [5, 8],
qr/^spam/ => 'eggs'
);
</code></pre>
<p>What's the best way to convert the contents of these files into Python-equ... | 9 | 2008-12-23T20:11:08Z | 390,589 | <p>I'd just turn the Perl data structure into something else. Not seeing the actual file, there might be some extra work that my solution doesn't do. </p>
<p>If the only thing that's in the file is the one variable declaration (so, no <code>1;</code> at the end, and so on), it can be really simple to turn your <code>%... | 11 | 2008-12-24T01:41:06Z | [
"python",
"perl",
"configuration",
"data-structures"
] |
Elegant ways to support equivalence ("equality") in Python classes | 390,250 | <p>When writing custom classes it is often important to allow equivalence by means of the <code>==</code> and <code>!=</code> operators. In Python, this is made possible by implementing the <code>__eq__</code> and <code>__ne__</code> special methods, respectively. The easiest way I've found to do this is the following ... | 222 | 2008-12-23T22:30:52Z | 390,280 | <p>You don't have to override both <code>__eq__</code> and <code>__ne__</code> you can override only <code>__cmp__</code> but this will make an implication on the result of ==, !==, < , > and so on.</p>
<p><code>is</code> tests for object identity. This means a <code>is</code> b will be <code>True</code> in the cas... | 8 | 2008-12-23T22:44:26Z | [
"python",
"equality",
"equivalence"
] |
Elegant ways to support equivalence ("equality") in Python classes | 390,250 | <p>When writing custom classes it is often important to allow equivalence by means of the <code>==</code> and <code>!=</code> operators. In Python, this is made possible by implementing the <code>__eq__</code> and <code>__ne__</code> special methods, respectively. The easiest way I've found to do this is the following ... | 222 | 2008-12-23T22:30:52Z | 390,335 | <p>I think that the two terms you're looking for are <strong>equality</strong> (==) and <strong>identity</strong> (is). For example:</p>
<pre><code>>>> a = [1,2,3]
>>> b = [1,2,3]
>>> a == b
True <-- a and b have values which are equal
>>> a is b
False <-- a and b a... | 2 | 2008-12-23T23:12:07Z | [
"python",
"equality",
"equivalence"
] |
Elegant ways to support equivalence ("equality") in Python classes | 390,250 | <p>When writing custom classes it is often important to allow equivalence by means of the <code>==</code> and <code>!=</code> operators. In Python, this is made possible by implementing the <code>__eq__</code> and <code>__ne__</code> special methods, respectively. The easiest way I've found to do this is the following ... | 222 | 2008-12-23T22:30:52Z | 390,511 | <p>The way you describe is the way I've always done it. Since it's totally generic, you can always break that functionality out into a mixin class and inherit it in classes where you want that functionality.</p>
<pre><code>class CommonEqualityMixin(object):
def __eq__(self, other):
return (isinstance(othe... | 143 | 2008-12-24T00:44:13Z | [
"python",
"equality",
"equivalence"
] |
Elegant ways to support equivalence ("equality") in Python classes | 390,250 | <p>When writing custom classes it is often important to allow equivalence by means of the <code>==</code> and <code>!=</code> operators. In Python, this is made possible by implementing the <code>__eq__</code> and <code>__ne__</code> special methods, respectively. The easiest way I've found to do this is the following ... | 222 | 2008-12-23T22:30:52Z | 390,640 | <p>You need to be careful with inheritance:</p>
<pre><code>>>> class Foo:
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
else:
return False
>>> class Bar(Foo):pass
>>> b = Bar()
>>> ... | 166 | 2008-12-24T02:30:13Z | [
"python",
"equality",
"equivalence"
] |
Elegant ways to support equivalence ("equality") in Python classes | 390,250 | <p>When writing custom classes it is often important to allow equivalence by means of the <code>==</code> and <code>!=</code> operators. In Python, this is made possible by implementing the <code>__eq__</code> and <code>__ne__</code> special methods, respectively. The easiest way I've found to do this is the following ... | 222 | 2008-12-23T22:30:52Z | 390,662 | <p>The 'is' test will test for identity using the builtin 'id()' function which essentially returns the memory address of the object and therefore isn't overloadable.</p>
<p>However in the case of testing the equality of a class you probably want to be a little bit more strict about your tests and only compare the dat... | 1 | 2008-12-24T02:55:28Z | [
"python",
"equality",
"equivalence"
] |
Elegant ways to support equivalence ("equality") in Python classes | 390,250 | <p>When writing custom classes it is often important to allow equivalence by means of the <code>==</code> and <code>!=</code> operators. In Python, this is made possible by implementing the <code>__eq__</code> and <code>__ne__</code> special methods, respectively. The easiest way I've found to do this is the following ... | 222 | 2008-12-23T22:30:52Z | 12,494,556 | <p>Not a direct answer but seemed relevant enough to be tacked on as it saves a bit of verbose tedium on occasion. Cut straight from the docs...</p>
<hr>
<p><a href="http://docs.python.org/library/functools.html#functools.total_ordering" rel="nofollow">functools.total_ordering(cls)</a></p>
<p><strong>Given a class d... | 11 | 2012-09-19T12:19:09Z | [
"python",
"equality",
"equivalence"
] |
Elegant ways to support equivalence ("equality") in Python classes | 390,250 | <p>When writing custom classes it is often important to allow equivalence by means of the <code>==</code> and <code>!=</code> operators. In Python, this is made possible by implementing the <code>__eq__</code> and <code>__ne__</code> special methods, respectively. The easiest way I've found to do this is the following ... | 222 | 2008-12-23T22:30:52Z | 25,176,504 | <p>Sorry, but non of the answers fully "work".
Consider the simple problem:</p>
<pre><code>class Number:
"""Very basic"""
def __init__(self, some_number):
self.some_number = some_number
n1 = Number(1)
n2 = Number(1)
print n1 == n2 # False -- oops
</code></pre>
<p>So, Python by default uses the id of... | 75 | 2014-08-07T07:24:42Z | [
"python",
"equality",
"equivalence"
] |
Elegant ways to support equivalence ("equality") in Python classes | 390,250 | <p>When writing custom classes it is often important to allow equivalence by means of the <code>==</code> and <code>!=</code> operators. In Python, this is made possible by implementing the <code>__eq__</code> and <code>__ne__</code> special methods, respectively. The easiest way I've found to do this is the following ... | 222 | 2008-12-23T22:30:52Z | 30,880,440 | <p>From this answer: <a href="http://stackoverflow.com/a/30676267/541136">http://stackoverflow.com/a/30676267/541136</a> I have demonstrated that, while it's correct to define <code>__ne__</code> in terms <code>__eq__</code> - instead of </p>
<pre><code>def __ne__(self, other):
return not self.__eq__(other)
</code... | 2 | 2015-06-17T00:30:12Z | [
"python",
"equality",
"equivalence"
] |
Interpreting Excel Currency Values | 390,263 | <p>I am using python to read a currency value from excel. The returned from the range.Value method is a tuple that I don't know how to parse.</p>
<p>For example, the cell appears as $548,982, but in python the value is returned as (1, 1194857614).</p>
<p>How can I get the numerical amount from excel or how can I conv... | 1 | 2008-12-23T22:37:51Z | 390,304 | <p>I tried this with Excel 2007 and VBA. It is giving correct value.</p>
<p>1) Try pasting this value in a new excel workbook<br>
2) Press Alt + F11. Gets you to VBA Editor.<br>
3) Press Ctrl + G. Gets you to immediate window.<br>
4) In the immediate window, type ?cells("a1").Value <br>
here "a1" is the cell where yo... | 0 | 2008-12-23T22:54:07Z | [
"python",
"excel",
"pywin32"
] |
Interpreting Excel Currency Values | 390,263 | <p>I am using python to read a currency value from excel. The returned from the range.Value method is a tuple that I don't know how to parse.</p>
<p>For example, the cell appears as $548,982, but in python the value is returned as (1, 1194857614).</p>
<p>How can I get the numerical amount from excel or how can I conv... | 1 | 2008-12-23T22:37:51Z | 391,076 | <p>Try this:</p>
<pre><code>import struct
try: import decimal
except ImportError:
divisor= 10000.0
else:
divisor= decimal.Decimal(10000)
def xl_money(i1, i2):
byte8= struct.unpack(">q", struct.pack(">ii", i1, i2))[0]
return byte8 / divisor
>>> xl_money(1, 1194857614)
Decimal("548982.49... | 3 | 2008-12-24T09:19:13Z | [
"python",
"excel",
"pywin32"
] |
How do you debug Mako templates? | 390,409 | <p>So far I've found it impossible to produce usable tracebacks when Mako templates aren't coded correctly.</p>
<p>Is there any way to debug templates besides iterating for every line of code?</p>
| 31 | 2008-12-23T23:43:30Z | 390,603 | <p>I break them down into pieces, and then reassemble the pieces when I've found the problem.</p>
<p>Not good, but it's really hard to tell what went wrong in a big, complex template.</p>
| 1 | 2008-12-24T01:49:17Z | [
"python",
"debugging",
"templates",
"jinja2",
"mako"
] |
How do you debug Mako templates? | 390,409 | <p>So far I've found it impossible to produce usable tracebacks when Mako templates aren't coded correctly.</p>
<p>Is there any way to debug templates besides iterating for every line of code?</p>
| 31 | 2008-12-23T23:43:30Z | 536,087 | <p>Mako actually provides a <a href="http://docs.makotemplates.org/en/latest/usage.html#handling-exceptions">VERY nice way to track down errors in a template</a>:</p>
<pre><code>from mako import exceptions
try:
template = lookup.get_template(uri)
print template.render()
except:
print exceptions.html_error... | 38 | 2009-02-11T09:31:33Z | [
"python",
"debugging",
"templates",
"jinja2",
"mako"
] |
How do you debug Mako templates? | 390,409 | <p>So far I've found it impossible to produce usable tracebacks when Mako templates aren't coded correctly.</p>
<p>Is there any way to debug templates besides iterating for every line of code?</p>
| 31 | 2008-12-23T23:43:30Z | 13,316,747 | <p>Using flask_mako, I find it's easier to skip over the TemplateError generation and just pass up the exception. I.e. in flask_mako.py, comment out the part that makes the TemplateError and just do a raise:</p>
<pre><code>def _render(template, context, app):
"""Renders the template and fires the signal"""
app.update... | 0 | 2012-11-09T22:07:43Z | [
"python",
"debugging",
"templates",
"jinja2",
"mako"
] |
How do you debug Mako templates? | 390,409 | <p>So far I've found it impossible to produce usable tracebacks when Mako templates aren't coded correctly.</p>
<p>Is there any way to debug templates besides iterating for every line of code?</p>
| 31 | 2008-12-23T23:43:30Z | 21,950,573 | <p>Looking at the <a href="http://pythonhosted.org/Flask-Mako/" rel="nofollow">Flask-Mako</a> source, I found an undocumented configuration parameter called <code>MAKO_TRANSLATE_EXCEPTIONS</code>. </p>
<p>Set this to <code>False</code> in your Flask app config and you'll get nice exceptions bubbling up from the templa... | 3 | 2014-02-22T06:18:39Z | [
"python",
"debugging",
"templates",
"jinja2",
"mako"
] |
Is there any built-in way to get the length of an iterable in python? | 390,852 | <p>For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. </p>
<p>One quick way is to do this:</p>
<pre><code>lines = len(list(open(fname)))
</code></pre>
<p>However, this loads the whole file into memory (at once). This rather defeats the purpose ... | 23 | 2008-12-24T05:55:37Z | 390,861 | <p>If you need a count of lines you can do this, I don't know of any better way to do it:</p>
<pre><code>line_count = sum(1 for line in open("yourfile.txt"))
</code></pre>
| 15 | 2008-12-24T06:03:06Z | [
"python",
"iterator"
] |
Is there any built-in way to get the length of an iterable in python? | 390,852 | <p>For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. </p>
<p>One quick way is to do this:</p>
<pre><code>lines = len(list(open(fname)))
</code></pre>
<p>However, this loads the whole file into memory (at once). This rather defeats the purpose ... | 23 | 2008-12-24T05:55:37Z | 390,885 | <p>Short of iterating through the iterable and counting the number of iterations, no. That's what makes it an iterable and not a list. This isn't really even a python-specific problem. Look at the classic linked-list data structure. Finding the length is an O(n) operation that involves iterating the whole list to find ... | 43 | 2008-12-24T06:23:35Z | [
"python",
"iterator"
] |
Is there any built-in way to get the length of an iterable in python? | 390,852 | <p>For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. </p>
<p>One quick way is to do this:</p>
<pre><code>lines = len(list(open(fname)))
</code></pre>
<p>However, this loads the whole file into memory (at once). This rather defeats the purpose ... | 23 | 2008-12-24T05:55:37Z | 390,911 | <p>We'll, if you think about it, how do you propose you find the number of lines in a file without reading the whole file for newlines? Sure, you can find the size of the file, and if you can gurantee that the length of a line is x, you can get the number of lines in a file. But unless you have some kind of constrain... | 0 | 2008-12-24T06:39:10Z | [
"python",
"iterator"
] |
Is there any built-in way to get the length of an iterable in python? | 390,852 | <p>For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. </p>
<p>One quick way is to do this:</p>
<pre><code>lines = len(list(open(fname)))
</code></pre>
<p>However, this loads the whole file into memory (at once). This rather defeats the purpose ... | 23 | 2008-12-24T05:55:37Z | 390,928 | <p>Absolutely not, for the simple reason that iterables are not guaranteed to be finite.</p>
<p>Consider this perfectly legal generator function:</p>
<pre><code>def forever():
while True:
yield "I will run forever"
</code></pre>
<p>Attempting to calculate the length of this function with <code>len([x for... | 8 | 2008-12-24T06:54:08Z | [
"python",
"iterator"
] |
Is there any built-in way to get the length of an iterable in python? | 390,852 | <p>For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. </p>
<p>One quick way is to do this:</p>
<pre><code>lines = len(list(open(fname)))
</code></pre>
<p>However, this loads the whole file into memory (at once). This rather defeats the purpose ... | 23 | 2008-12-24T05:55:37Z | 390,989 | <p>I've used this redefinition for some time now:</p>
<pre><code>def len(thingy):
try:
return thingy.__len__()
except AttributeError:
return sum(1 for item in iter(thingy))
</code></pre>
| 7 | 2008-12-24T07:49:02Z | [
"python",
"iterator"
] |
Is there any built-in way to get the length of an iterable in python? | 390,852 | <p>For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines. </p>
<p>One quick way is to do this:</p>
<pre><code>lines = len(list(open(fname)))
</code></pre>
<p>However, this loads the whole file into memory (at once). This rather defeats the purpose ... | 23 | 2008-12-24T05:55:37Z | 28,385,557 | <p>The <code>cardinality</code> package provides an efficient <code>count()</code> function and some related functions to count and check the size of any iterable: <a href="http://cardinality.readthedocs.org/" rel="nofollow">http://cardinality.readthedocs.org/</a></p>
<p>Internally it uses <code>enumerate()</code> and... | 2 | 2015-02-07T18:04:18Z | [
"python",
"iterator"
] |
Calling Application Methods from a wx Frame Class | 390,867 | <p>I'm starting out with wxPython and have been working my way through every tutorial and example I can get my hands on. I've run into a slight problem, however, and it has to do with the wx.App versus the wx.Frame and which should contain specific methods. Just about every example I've seen don't go much beyond layo... | 1 | 2008-12-24T06:07:30Z | 390,887 | <p>In a proper OOP design, this would be independent or part of a filesystem class - it wouldn't be part of the app or the frame.</p>
| 0 | 2008-12-24T06:24:15Z | [
"python",
"wxpython",
"wxwidgets",
"model-view-controller",
"project-organization"
] |
Calling Application Methods from a wx Frame Class | 390,867 | <p>I'm starting out with wxPython and have been working my way through every tutorial and example I can get my hands on. I've run into a slight problem, however, and it has to do with the wx.App versus the wx.Frame and which should contain specific methods. Just about every example I've seen don't go much beyond layo... | 1 | 2008-12-24T06:07:30Z | 390,912 | <p>As Mark stated you should make a new class that handles things like this. </p>
<p>The ideal layout of code when using something like wxWidgets is the model view controller where the wxFrame class only has the code needed to display items and all the logic and business rules are handled by other class that interact ... | 2 | 2008-12-24T06:39:56Z | [
"python",
"wxpython",
"wxwidgets",
"model-view-controller",
"project-organization"
] |
Calling Application Methods from a wx Frame Class | 390,867 | <p>I'm starting out with wxPython and have been working my way through every tutorial and example I can get my hands on. I've run into a slight problem, however, and it has to do with the wx.App versus the wx.Frame and which should contain specific methods. Just about every example I've seen don't go much beyond layo... | 1 | 2008-12-24T06:07:30Z | 394,333 | <p>Your classes that inherit from wxWidgets/wxPython data types should not implement any business logic. wxWidgets is a GUI library, so any subclasses of wxApp or wxFrame should remain focused on GUI, that is on displaying the interface and being responsive to user actions. </p>
<p>The code that does something useful ... | 2 | 2008-12-26T22:01:47Z | [
"python",
"wxpython",
"wxwidgets",
"model-view-controller",
"project-organization"
] |
Calling Application Methods from a wx Frame Class | 390,867 | <p>I'm starting out with wxPython and have been working my way through every tutorial and example I can get my hands on. I've run into a slight problem, however, and it has to do with the wx.App versus the wx.Frame and which should contain specific methods. Just about every example I've seen don't go much beyond layo... | 1 | 2008-12-24T06:07:30Z | 395,992 | <p>I probably should have been a lot clearer from the start, but I found what I was looking for:</p>
<p><a href="http://wiki.wxpython.org/ModelViewController/" rel="nofollow">http://wiki.wxpython.org/ModelViewController/</a></p>
<p>Burried within the wxpython wiki, I found several simple, concrete examples of MVC pro... | 2 | 2008-12-28T08:55:34Z | [
"python",
"wxpython",
"wxwidgets",
"model-view-controller",
"project-organization"
] |
pyGTK Radio Button Help | 391,237 | <p>Alright, I'll preface this with the fact that I'm a GTK <em>and</em> Python newb, but I haven't been able to dig up the information I needed. Basically what I have is a list of Radio Buttons, and based on which one is checked, I need to connect a button to a different function. I tried creating all my radio buttons,... | 1 | 2008-12-24T10:54:03Z | 391,288 | <p>First, I presume that's a typo and you're actually calling get_active() in your code and not set_active()? Other than that, without seeing the code, I can point you to a pygtk tutorial about <a href="http://www.pygtk.org/pygtk2tutorial/sec-RadioButtons.html" rel="nofollow">radio buttons</a></p>
| 0 | 2008-12-24T11:31:58Z | [
"python",
"gtk",
"pygtk"
] |
pyGTK Radio Button Help | 391,237 | <p>Alright, I'll preface this with the fact that I'm a GTK <em>and</em> Python newb, but I haven't been able to dig up the information I needed. Basically what I have is a list of Radio Buttons, and based on which one is checked, I need to connect a button to a different function. I tried creating all my radio buttons,... | 1 | 2008-12-24T10:54:03Z | 391,289 | <p>Edit: (since you posted some code), just use:</p>
<pre><code>active = [r for r in self.updatePageRadio.get_group() if r.get_active()][0]
</code></pre>
<p>and use that to look up in a dict of functions and call it:</p>
<pre><code>my_actions[active]()
</code></pre>
<p><hr /></p>
<p>Edit: I totally forgot to menti... | 7 | 2008-12-24T11:32:24Z | [
"python",
"gtk",
"pygtk"
] |
Organising my Python project | 391,879 | <p>I'm starting a Python project and expect to have 20 or more classes in it. As is good practice I want to put them in a separate file each. However, the project directory quickly becomes swamped with files (or will when I do this).</p>
<p>If I put a file to import in a folder I can no longer import it. How do I impo... | 45 | 2008-12-24T17:23:14Z | 391,899 | <p>Create an <code>__init__.py</code> file in your projects folder, and it will be treated like a module by Python.</p>
<p>Classes in your package directory can then be imported using syntax like:</p>
<pre><code>from package import class
import package.class
</code></pre>
<p>Within <code>__init__.py</code>, you may ... | 30 | 2008-12-24T17:31:40Z | [
"python",
"project-organization"
] |
Organising my Python project | 391,879 | <p>I'm starting a Python project and expect to have 20 or more classes in it. As is good practice I want to put them in a separate file each. However, the project directory quickly becomes swamped with files (or will when I do this).</p>
<p>If I put a file to import in a folder I can no longer import it. How do I impo... | 45 | 2008-12-24T17:23:14Z | 391,902 | <p>Python doesn't force you into Java's nasty one-class-per-file style. In fact, it's not even considered good style to put each class in a separate file unless they are huge. (If they are huge, you probably have to do refactoring anyway.) Instead, you should group similar classes and functions in modules. For example,... | 13 | 2008-12-24T17:33:00Z | [
"python",
"project-organization"
] |
Organising my Python project | 391,879 | <p>I'm starting a Python project and expect to have 20 or more classes in it. As is good practice I want to put them in a separate file each. However, the project directory quickly becomes swamped with files (or will when I do this).</p>
<p>If I put a file to import in a folder I can no longer import it. How do I impo... | 45 | 2008-12-24T17:23:14Z | 391,904 | <p>simple answer is to create an empty file called <code>__init__.py</code> in the new folder you made. Then in your top level .py file include with something like:</p>
<pre><code>import mynewsubfolder.mynewclass
</code></pre>
| 7 | 2008-12-24T17:33:09Z | [
"python",
"project-organization"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.