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
Calling from a parent file in python
403,822
<p>I have a file called main.py and a file called classes.py</p> <p>main.py contains the application and what's happening while class.py contains some classes.</p> <p>main.py has the following code</p> <p><strong>main.py</strong></p> <pre><code>import classes def addItem(text): print text myClass = classes.Ex...
3
2008-12-31T19:14:59Z
403,839
<p>I would suggest putting common functions either in classes.py, or probably even better in a third module, perhaps utils.py.</p>
1
2008-12-31T19:22:40Z
[ "python" ]
Calling from a parent file in python
403,822
<p>I have a file called main.py and a file called classes.py</p> <p>main.py contains the application and what's happening while class.py contains some classes.</p> <p>main.py has the following code</p> <p><strong>main.py</strong></p> <pre><code>import classes def addItem(text): print text myClass = classes.Ex...
3
2008-12-31T19:14:59Z
403,850
<p>All your executable code should be inside a <code>if __name__ == "__main__"</code> . This will prevent it from being execucted when imported as a module. In main.py</p> <pre><code>if __name__=="__main__": myClass = classes.ExampleClass() </code></pre> <p>However, as dF states, it is probably better to refactor...
1
2008-12-31T19:26:31Z
[ "python" ]
Calling from a parent file in python
403,822
<p>I have a file called main.py and a file called classes.py</p> <p>main.py contains the application and what's happening while class.py contains some classes.</p> <p>main.py has the following code</p> <p><strong>main.py</strong></p> <pre><code>import classes def addItem(text): print text myClass = classes.Ex...
3
2008-12-31T19:14:59Z
403,875
<p>The suggestions to refactor are good ones. If you have to leave the files as they are, then you can edit main.py to make sure that nothing is executed simply by importing the file, then import main in the function that needs it:</p> <pre><code>class ExampleClass (object): def __init__(self): import mai...
3
2008-12-31T19:38:23Z
[ "python" ]
using results from a sql query in a python program in another sql query
403,897
<p>sorry for my previous question which was very ambiguous, but i think if i get the answer to this question I can work it out. In the program below i have selected the barcodes of products where the amount is less than the quantity. I want to say, that if the barcodes(in the fridge table) match barcodes in another tab...
0
2008-12-31T19:44:58Z
403,941
<pre><code>UPDATE products SET stock = 0 WHERE barcode IN ( SELECT fridge.barcode FROM fridge WHERE fridge.amount &lt; fridge.quantity ); </code></pre> <p>I know this doesn't answer the question exactly but two SQL statements are not required.</p> <p>To do it in python:</p> <pre><code>import MySQLdb def order(...
5
2008-12-31T20:03:09Z
[ "python", "sql" ]
using results from a sql query in a python program in another sql query
403,897
<p>sorry for my previous question which was very ambiguous, but i think if i get the answer to this question I can work it out. In the program below i have selected the barcodes of products where the amount is less than the quantity. I want to say, that if the barcodes(in the fridge table) match barcodes in another tab...
0
2008-12-31T19:44:58Z
403,947
<p>This is more of SQL query than Python, but still I will try to answer that: (I haven't worked with MySQL but PostgreSQL, so there might slight variation in interpretation of things here).</p> <p>when you did</p> <pre><code>cursor.execute('select barcode from fridge where amount &lt; quantity') db.commit() row = cu...
4
2008-12-31T20:09:21Z
[ "python", "sql" ]
All code in one file
403,934
<p>After asking <a href="http://stackoverflow.com/questions/391879/organising-my-python-project">organising my Python project</a> and then <a href="http://stackoverflow.com/questions/403822/calling-from-a-parent-file-in-python">calling from a parent file in Python</a> it's occurring to me that it'll be so much easier t...
5
2008-12-31T19:59:29Z
403,944
<p>If you are planning to use any kind of SCM then you are going to be screwed. Having one file is a guaranteed way to have lots of collisions and merges that will be painstaking to deal with over time.</p> <p>Stick to conventions and break apart your files. If nothing more than to save the guy who will one day have t...
12
2008-12-31T20:04:33Z
[ "python", "version-control", "project-management" ]
All code in one file
403,934
<p>After asking <a href="http://stackoverflow.com/questions/391879/organising-my-python-project">organising my Python project</a> and then <a href="http://stackoverflow.com/questions/403822/calling-from-a-parent-file-in-python">calling from a parent file in Python</a> it's occurring to me that it'll be so much easier t...
5
2008-12-31T19:59:29Z
403,949
<p>If your code is going to work together all the time anyway, and isn't useful separately, there's nothing wrong with keeping everything in one file. I can think of at least popular package (BeautifulSoup) that does this. Sure makes installation easier.</p> <p>Of course, if it seems, down the road, that you could u...
4
2008-12-31T20:11:12Z
[ "python", "version-control", "project-management" ]
All code in one file
403,934
<p>After asking <a href="http://stackoverflow.com/questions/391879/organising-my-python-project">organising my Python project</a> and then <a href="http://stackoverflow.com/questions/403822/calling-from-a-parent-file-in-python">calling from a parent file in Python</a> it's occurring to me that it'll be so much easier t...
5
2008-12-31T19:59:29Z
403,952
<p>It's always a now verses then argument. If you're under the gun to get it done, do it. Source control will be a problem later, as with many things there's no black and white answer. You need to be responsible to both your deadline and the long term maintenance of the code.</p>
2
2008-12-31T20:14:52Z
[ "python", "version-control", "project-management" ]
All code in one file
403,934
<p>After asking <a href="http://stackoverflow.com/questions/391879/organising-my-python-project">organising my Python project</a> and then <a href="http://stackoverflow.com/questions/403822/calling-from-a-parent-file-in-python">calling from a parent file in Python</a> it's occurring to me that it'll be so much easier t...
5
2008-12-31T19:59:29Z
404,003
<p>If that's the best way to organise it, you're probably doing something wrong.</p> <p>If it's more than just a toy program or a simple script, then you should break it up into separate files, etc. It's the only sane way of doing it. When your project gets big enough that you need someone else helping on it, then it ...
2
2008-12-31T20:40:50Z
[ "python", "version-control", "project-management" ]
All code in one file
403,934
<p>After asking <a href="http://stackoverflow.com/questions/391879/organising-my-python-project">organising my Python project</a> and then <a href="http://stackoverflow.com/questions/403822/calling-from-a-parent-file-in-python">calling from a parent file in Python</a> it's occurring to me that it'll be so much easier t...
5
2008-12-31T19:59:29Z
404,095
<p>Since <a href="http://stackoverflow.com/questions/403822/calling-from-a-parent-file-in-python">Calling from a parent file in Python</a> indicates serious design problems, I'd say that you have two choices.</p> <ol> <li><p>Don't have a library module try to call back to main. You'll have to rewrite things to fix th...
2
2008-12-31T21:42:25Z
[ "python", "version-control", "project-management" ]
All code in one file
403,934
<p>After asking <a href="http://stackoverflow.com/questions/391879/organising-my-python-project">organising my Python project</a> and then <a href="http://stackoverflow.com/questions/403822/calling-from-a-parent-file-in-python">calling from a parent file in Python</a> it's occurring to me that it'll be so much easier t...
5
2008-12-31T19:59:29Z
404,228
<p>Looking at your earlier questions I would say <strong>all code in one file would be a good intermediate state</strong> on the way to a <strong>complete refactoring of your project</strong>. To do this you'll need a <strong>regression test suite</strong> to make sure you don't break the project while refactoring it....
2
2008-12-31T23:02:44Z
[ "python", "version-control", "project-management" ]
Python program to calculate harmonic series
404,346
<p>Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...</p>
4
2009-01-01T00:55:16Z
404,354
<p>This ought to do the trick.</p> <pre><code>def calc_harmonic(n): return sum(1.0/d for d in range(2,n+1)) </code></pre>
3
2009-01-01T00:59:15Z
[ "python", "math" ]
Python program to calculate harmonic series
404,346
<p>Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...</p>
4
2009-01-01T00:55:16Z
404,361
<p>How about this:</p> <pre><code>partialsum = 0 for i in xrange(1,1000000): partialsum += 1.0 / i print partialsum </code></pre> <p>where 1000000 is the upper bound.</p>
1
2009-01-01T01:00:40Z
[ "python", "math" ]
Python program to calculate harmonic series
404,346
<p>Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...</p>
4
2009-01-01T00:55:16Z
404,364
<p>The harmonic series diverges, i.e. its sum is infinity..</p> <p>edit: Unless you want partial sums, but you weren't really clear about that.</p>
4
2009-01-01T01:02:21Z
[ "python", "math" ]
Python program to calculate harmonic series
404,346
<p>Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...</p>
4
2009-01-01T00:55:16Z
404,371
<p>Homework? </p> <p>It's a divergent series, so it's impossible to sum it for all terms.</p> <p>I don't know Python, but I know how to write it in Java.</p> <pre><code>public class Harmonic { private static final int DEFAULT_NUM_TERMS = 10; public static void main(String[] args) { int numTerms...
0
2009-01-01T01:06:44Z
[ "python", "math" ]
Python program to calculate harmonic series
404,346
<p>Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...</p>
4
2009-01-01T00:55:16Z
404,425
<p><a href="http://stackoverflow.com/questions/404346/python-program-to-calculate-harmonic-series#404354">@recursive's solution</a> is correct for a floating point approximation. If you prefer, you can get the exact answer in Python 3.0 using the fractions module:</p> <pre><code>&gt;&gt;&gt; from fractions import Frac...
12
2009-01-01T02:31:41Z
[ "python", "math" ]
Python program to calculate harmonic series
404,346
<p>Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...</p>
4
2009-01-01T00:55:16Z
404,587
<p>Just a footnote on the other answers that used floating point; starting with the largest divisor and iterating <strong>downward</strong> (toward the reciprocals with largest value) will put off accumulated round-off error as much as possible.</p>
4
2009-01-01T05:35:22Z
[ "python", "math" ]
Python program to calculate harmonic series
404,346
<p>Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...</p>
4
2009-01-01T00:55:16Z
404,843
<p><a href="http://stackoverflow.com/questions/404346/python-program-to-calculate-harmonic-series#404425">@Kiv's answer</a> is correct but it is slow for large n if you don't need an infinite precision. It is better to use an <a href="http://en.wikipedia.org/wiki/Harmonic_number">asymptotic formula</a> in this case:</p...
17
2009-01-01T10:47:42Z
[ "python", "math" ]
Python program to calculate harmonic series
404,346
<p>Does anyone know how to write a program in Python that will calculate the addition of the harmonic series. i.e. 1 + 1/2 +1/3 +1/4...</p>
4
2009-01-01T00:55:16Z
27,683,292
<p>A fast, accurate, smooth, complex-valued version of the H function can be calculated using the digamma function as explained <a href="https://en.wikipedia.org/wiki/Harmonic_number#Generalization_to_the_complex_plane" rel="nofollow" title="here">here</a>. The Euler-Mascheroni (gamma) constant and the digamma functio...
3
2014-12-29T04:12:19Z
[ "python", "math" ]
Python globals, locals, and UnboundLocalError
404,534
<p>I ran across this case of <code>UnboundLocalError</code> recently, which seems strange:</p> <pre><code>import pprint def main(): if 'pprint' in globals(): print 'pprint is in globals()' pprint.pprint('Spam') from pprint import pprint pprint('Eggs') if __name__ == '__main__': main() </code></pre> ...
8
2009-01-01T04:48:54Z
404,610
<p>Looks like Python sees the <code>from pprint import pprint</code> line and marks <code>pprint</code> as a name local to <code>main()</code> <em>before</em> executing any code. Since Python thinks pprint ought to be a local variable, referencing it with <code>pprint.pprint()</code> before "assigning" it with the <co...
4
2009-01-01T06:02:48Z
[ "python", "binding", "scope", "identifier" ]
Python globals, locals, and UnboundLocalError
404,534
<p>I ran across this case of <code>UnboundLocalError</code> recently, which seems strange:</p> <pre><code>import pprint def main(): if 'pprint' in globals(): print 'pprint is in globals()' pprint.pprint('Spam') from pprint import pprint pprint('Eggs') if __name__ == '__main__': main() </code></pre> ...
8
2009-01-01T04:48:54Z
404,709
<p>Well, that was interesting enough for me to experiment a bit and I read through <a href="http://docs.python.org/reference/executionmodel.html" rel="nofollow">http://docs.python.org/reference/executionmodel.html</a></p> <p>Then did some tinkering with your code here and there, this is what i could find:</p> <p>code...
5
2009-01-01T08:21:10Z
[ "python", "binding", "scope", "identifier" ]
Python globals, locals, and UnboundLocalError
404,534
<p>I ran across this case of <code>UnboundLocalError</code> recently, which seems strange:</p> <pre><code>import pprint def main(): if 'pprint' in globals(): print 'pprint is in globals()' pprint.pprint('Spam') from pprint import pprint pprint('Eggs') if __name__ == '__main__': main() </code></pre> ...
8
2009-01-01T04:48:54Z
404,789
<p>Where's the surprise? <em>Any</em> variable global to a scope that you reassign within that scope is marked local to that scope by the compiler. </p> <p>If imports would be handled differently, <em>that</em> would be surprising imho.</p> <p>It may make a case for not naming modules after symbols used therein, or v...
6
2009-01-01T09:42:23Z
[ "python", "binding", "scope", "identifier" ]
Python globals, locals, and UnboundLocalError
404,534
<p>I ran across this case of <code>UnboundLocalError</code> recently, which seems strange:</p> <pre><code>import pprint def main(): if 'pprint' in globals(): print 'pprint is in globals()' pprint.pprint('Spam') from pprint import pprint pprint('Eggs') if __name__ == '__main__': main() </code></pre> ...
8
2009-01-01T04:48:54Z
2,146,410
<p>This question got answered several weeks ago, but I think I can clarify the answers a little. First some facts.</p> <p>1: In Python,</p> <pre><code>import foo </code></pre> <p>is almost exactly the same as</p> <pre><code>foo = __import__("foo", globals(), locals(), [], -1) </code></pre> <p>2: When executing co...
4
2010-01-27T11:25:45Z
[ "python", "binding", "scope", "identifier" ]
Determining application path in a Python EXE generated by pyInstaller
404,744
<p>I have an application that resides in a single .py file. I've been able to get pyInstaller to bundle it successfully into an EXE for Windows. The problem is, the application requires a .cfg file that always sits directly beside the application in the same directory.</p> <p>Normally, I build the path using the fol...
30
2009-01-01T08:50:22Z
404,750
<p>I found a solution. You need to check if the application is running as a script or as a frozen exe:</p> <pre><code>import os import sys config_name = 'myapp.cfg' # determine if application is a script file or frozen exe if getattr(sys, 'frozen', False): application_path = os.path.dirname(sys.executable) elif...
62
2009-01-01T08:53:20Z
[ "python", "executable", "relative-path", "pyinstaller" ]
Determining application path in a Python EXE generated by pyInstaller
404,744
<p>I have an application that resides in a single .py file. I've been able to get pyInstaller to bundle it successfully into an EXE for Windows. The problem is, the application requires a .cfg file that always sits directly beside the application in the same directory.</p> <p>Normally, I build the path using the fol...
30
2009-01-01T08:50:22Z
14,372,748
<pre><code>os.path.dirname(sys.argv[0]) </code></pre> <p>That works for me.</p>
3
2013-01-17T05:31:57Z
[ "python", "executable", "relative-path", "pyinstaller" ]
LBYL vs EAFP in Java?
404,795
<p>I was recently teaching myself Python and discovered the LBYL/EAFP idioms with regards to error checking before code execution. In Python, it seems the accepted style is EAFP, and it seems to work well with the language.</p> <p>LBYL (<strong><em>L</strong>ook <strong>B</strong>efore <strong>Y</strong>ou <strong>L</...
39
2009-01-01T09:50:56Z
404,802
<p>Personally, and I think this is backed up by convention, EAFP is never a good way to go. You can look at it as an equivalent to the following:</p> <pre><code>if (o != null) o.doSomething(); else // handle </code></pre> <p>as opposed to:</p> <pre><code>try { o.doSomething() } catch (NullPointerExceptio...
5
2009-01-01T09:57:00Z
[ "java", "python", "error-handling", "idioms" ]
LBYL vs EAFP in Java?
404,795
<p>I was recently teaching myself Python and discovered the LBYL/EAFP idioms with regards to error checking before code execution. In Python, it seems the accepted style is EAFP, and it seems to work well with the language.</p> <p>LBYL (<strong><em>L</strong>ook <strong>B</strong>efore <strong>Y</strong>ou <strong>L</...
39
2009-01-01T09:50:56Z
404,999
<p>Exceptions are handled more efficiently in Python than in Java, which is at least <em>partly</em> why you see that construct in Python. In Java, it's more inefficient (in terms of performance) to use exceptions in that way.</p>
10
2009-01-01T14:30:19Z
[ "java", "python", "error-handling", "idioms" ]
LBYL vs EAFP in Java?
404,795
<p>I was recently teaching myself Python and discovered the LBYL/EAFP idioms with regards to error checking before code execution. In Python, it seems the accepted style is EAFP, and it seems to work well with the language.</p> <p>LBYL (<strong><em>L</strong>ook <strong>B</strong>efore <strong>Y</strong>ou <strong>L</...
39
2009-01-01T09:50:56Z
405,220
<p>If you are accessing files, EAFP is more reliable than LBYL, because the operations involved in LBYL are not atomic, and the file system might change between the time you look and the time you leap. Actually, the standard name is TOCTOU - Time of Check, Time of Use; bugs caused by inaccurate checking are TOCTOU bug...
89
2009-01-01T17:52:24Z
[ "java", "python", "error-handling", "idioms" ]
LBYL vs EAFP in Java?
404,795
<p>I was recently teaching myself Python and discovered the LBYL/EAFP idioms with regards to error checking before code execution. In Python, it seems the accepted style is EAFP, and it seems to work well with the language.</p> <p>LBYL (<strong><em>L</strong>ook <strong>B</strong>efore <strong>Y</strong>ou <strong>L</...
39
2009-01-01T09:50:56Z
408,305
<p>In addition to the relative cost of exceptions in Python and Java, keep in mind that there's a difference in philosophy / attitude between them. Java tries to be very strict about types (and everything else), requiring explicit, detailed declarations of class/method signatures. It assumes that you should know, at a...
37
2009-01-02T23:31:52Z
[ "java", "python", "error-handling", "idioms" ]
I need help--lists and Python
404,825
<p>How to return a list in Python???</p> <p>When I tried returning a list,I got an empty list.What's the reason???</p>
-3
2009-01-01T10:28:31Z
404,837
<p>to wit:</p> <pre><code>In [1]: def pants(): ...: return [1, 2, 'steve'] ...: In [2]: pants() Out[2]: [1, 2, 'steve'] </code></pre>
0
2009-01-01T10:42:02Z
[ "python", "list", "return" ]
I need help--lists and Python
404,825
<p>How to return a list in Python???</p> <p>When I tried returning a list,I got an empty list.What's the reason???</p>
-3
2009-01-01T10:28:31Z
404,874
<p>As Andrew commented, you will receive better answers if you show us the code you are currently using. Also if you could state what version of Python you are using that would be great.</p> <p>There are a few ways you can return a list. Say for example we have a function called retlist.</p> <pre><code>def retlist():...
2
2009-01-01T11:26:36Z
[ "python", "list", "return" ]
Line reading chokes on 0x1A
405,058
<p>I have the following file:</p> <pre><code>abcde kwakwa &lt;0x1A&gt; line3 linllll </code></pre> <p>Where <code>&lt;0x1A&gt;</code> represents a byte with the hex value of 0x1A. When attempting to read this file in Python as:</p> <pre><code>for line in open('t.txt'): print line, </code></pre> <p>It only reads...
12
2009-01-01T15:29:35Z
405,061
<p>0x1A is Ctrl-Z, and DOS historically used that as an end-of-file marker. For example, try using a command prompt, and "type"ing your file. It will only display the content up the Ctrl-Z. </p> <p>Python uses the Windows CRT function _wfopen, which implements the "Ctrl-Z is EOF" semantics.</p>
26
2009-01-01T15:31:38Z
[ "python", "windows", "binary-data" ]
Line reading chokes on 0x1A
405,058
<p>I have the following file:</p> <pre><code>abcde kwakwa &lt;0x1A&gt; line3 linllll </code></pre> <p>Where <code>&lt;0x1A&gt;</code> represents a byte with the hex value of 0x1A. When attempting to read this file in Python as:</p> <pre><code>for line in open('t.txt'): print line, </code></pre> <p>It only reads...
12
2009-01-01T15:29:35Z
405,169
<p><a href="http://stackoverflow.com/questions/405058/line-reading-chokes-on-0x1a#405061">Ned</a> is of course correct.</p> <p>If your curiosity runs a little deeper, the root cause is backwards compatibility taken to an extreme. Windows is compatible with DOS, which used Ctrl-Z as an optional end of file marker for t...
8
2009-01-01T17:13:22Z
[ "python", "windows", "binary-data" ]
I can't but help get the idea I'm doing it all wrong (Python, again)
405,106
<p>All of the questions that I've asked recently about Python have been for this project. I have realised that the reason I'm asking so many questions may not be because I'm so new to Python (but I know a good bit of PHP) and is probably not because Python has some inherent flaw.</p> <p>Thus I will now say what the pr...
2
2009-01-01T16:27:44Z
405,129
<p>I have narrowed your issue down to:</p> <blockquote> <p>With Python I have to remake that array each time I import the relevant data file</p> </blockquote> <p>Well you have two choices really, the first and easiest is to keep the structure in memory. That way (just like PHP) you can in theory access it from ...
3
2009-01-01T16:43:47Z
[ "php", "python", "project", "project-planning" ]
I can't but help get the idea I'm doing it all wrong (Python, again)
405,106
<p>All of the questions that I've asked recently about Python have been for this project. I have realised that the reason I'm asking so many questions may not be because I'm so new to Python (but I know a good bit of PHP) and is probably not because Python has some inherent flaw.</p> <p>Thus I will now say what the pr...
2
2009-01-01T16:27:44Z
405,158
<blockquote> <p>With Python I have to remake that array each time I import the relevant data file</p> </blockquote> <p>You're missing a subtle point of Python semantics here. When you import a module for a second time, you aren't re-executing the code in that module. The name is found in a list of all modules ...
5
2009-01-01T17:03:41Z
[ "php", "python", "project", "project-planning" ]
I can't but help get the idea I'm doing it all wrong (Python, again)
405,106
<p>All of the questions that I've asked recently about Python have been for this project. I have realised that the reason I'm asking so many questions may not be because I'm so new to Python (but I know a good bit of PHP) and is probably not because Python has some inherent flaw.</p> <p>Thus I will now say what the pr...
2
2009-01-01T16:27:44Z
405,328
<p>Please don't reinvent the wheel. Your <code>formationsHash</code> as a list of key values isn't helpful and it duplicates the features of a dictionary.</p> <pre><code>def createFormations(logger): """This creates all the formations that will be used""" formations = {} formations['Tight']= Formation(log...
3
2009-01-01T19:12:45Z
[ "php", "python", "project", "project-planning" ]
I can't but help get the idea I'm doing it all wrong (Python, again)
405,106
<p>All of the questions that I've asked recently about Python have been for this project. I have realised that the reason I'm asking so many questions may not be because I'm so new to Python (but I know a good bit of PHP) and is probably not because Python has some inherent flaw.</p> <p>Thus I will now say what the pr...
2
2009-01-01T16:27:44Z
405,356
<p>"data.py creates an array (well, list), to use this list from another file I need to import data.py and remake said list."</p> <p>I can't figure out what you're talking about. Seriously.</p> <p>Here's a main program, which imports the data, and another module.</p> <p>SomeMainProgram.py</p> <pre><code>import dat...
1
2009-01-01T19:35:40Z
[ "php", "python", "project", "project-planning" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,188
<p>I'd go with Ruby. It's got all kinds of metaprogramming and duck punching hacks that make it really easy to extend. Features like blocks may not seem like much at first, but they make for some really clean syntax if you use them right. Open classes can be debugging hell if you screw them up, but if you're a responsi...
24
2009-01-01T17:24:30Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,206
<p><a href="http://norvig.com">Peter Norvig</a>, <a href="http://norvig.com/paip.html">a famous and great lisper</a>, converted to Python. He wrote the article <a href="http://norvig.com/python-lisp.html">Python for Lisp Programmers</a>, which you might find interesting with its detailed comparison of features.</p> <...
31
2009-01-01T17:40:13Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,211
<p>I also recommend the article by <a href="http://norvig.com/python-lisp.html" rel="nofollow">Peter Norvig</a> that namin posted. If you want to look at functional programming in Python check out the <a href="http://docs.python.org/dev/3.0/library/functools.html" rel="nofollow">functools</a> module in the standard lib...
3
2009-01-01T17:42:31Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,228
<p>Speaking as a "Rubyist", I'd agree with Kiv. The two languages both grant a nice amount of leeway when it comes to programming paradigms, but are also have benefits/shortcomings. I think that the compromises you make either way are a lot about your own programming style and taste.</p> <p>Personally, I think Ruby ca...
12
2009-01-01T18:00:04Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,310
<p>Both Ruby and Python are fairly distant from the Lisp traditions of immutable data, programs as data, and macros. But Ruby is very nearly a clone of Smalltalk (and I hope will grow more like Smalltalk as the Perlish cruft is deprecated), and Smalltalk, like Lisp, is a language that takes one idea to extremes. Base...
8
2009-01-01T19:01:50Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,312
<p>Alex Martelli gives a <a href="http://groups.google.com/group/comp.lang.python/msg/28422d707512283" rel="nofollow">good analysis of the subject</a>. It's a bit dated now, but I agree with the basic gist of it: <strong>Python and Ruby are two different ways of implementing the same thing</strong>. Sure there are s...
5
2009-01-01T19:03:23Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,317
<p>If you need Unicode support, remember to check how well supported it is. AFAIK, Python's support for Unicode is better than Ruby's, especially since Python 3.0. On the other hand, Python 3 is still missing some popular packages and 3rd party libraries, so that might play against.</p>
1
2009-01-01T19:06:39Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,342
<p><strong>Devils Advocate: Who Cares?</strong></p> <p>They are both good systems and have an ecosystem of good web frameworks and active developer communities. I'm guessing that you're framing your decision based on the wrong criteria. The question sounds like you're fretting about whether you will hit implementati...
17
2009-01-01T19:23:00Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,382
<p>I am a Pythonista; however, based on your requirements, especially the "cool hacks on the language level", I would suggest you work on Ruby. Ruby is more flexible in the Perl way and you can do a lot of hacks; Python is targeted towards <em>readability</em>, which is a very good thing, and generally language hacks a...
3
2009-01-01T19:56:37Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,474
<p>I'm a Rubyist who chose the language based on very similar criteria. Python is a good language and I enjoy working with it too, but I think Ruby is somewhat more Lispy in the degree of freedom it gives to the programmer. Python seems to impose its opinions a little bit more (which can be a good thing, but isn't acco...
0
2009-01-01T20:58:37Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
405,577
<p>pick the most popular one for your domain so your work gets the most visibility. some might say ruby/rails for web, python for everything else. picking a language just because its like lisp is really not appropriate for a professional.</p>
0
2009-01-01T22:27:38Z
[ "python", "ruby", "lisp" ]
Please advise on Ruby vs Python, for someone who likes LISP a lot
405,165
<p>I am a C++ developer, slowly getting into web development. I like LISP a lot but don't like AllegroCL and web-frameworks available for LISP. I am looking for more freedom and ability to do cool hacks on language level. I don't consider tabs as a crime against nature.</p> <p>Which one is closer to LISP: Python or Ru...
20
2009-01-01T17:12:12Z
6,325,009
<p>If you like lisp, I think you'll be better like ruby but c++ reminds me more of python. I've posted a small post about this subject : <a href="http://hartator.wordpress.com/2011/06/12/ruby-vs-python-2011/" rel="nofollow">http://hartator.wordpress.com/2011/06/12/ruby-vs-python-2011/</a></p>
0
2011-06-12T22:06:54Z
[ "python", "ruby", "lisp" ]
Many instances of a class
405,282
<p>I am trying to write a life simulation in python with a variety of animals. It is impossible to name each instance of the classes I am going to use because I have no way of knowing how many there will be.</p> <p>So, my question:</p> <p>How can I automatically give a name to an object?</p> <p>I was thinking of cre...
3
2009-01-01T18:47:32Z
405,292
<p>Hm, well you normally just stuff all those instances in a list and then iterate over that list if you want to do something with them. If you want to automatically keep track of each instance created you can also make the adding to the list implicit in the class' constructor or create a factory method that keeps trac...
7
2009-01-01T18:52:03Z
[ "python", "class", "object", "multiple-instances" ]
Many instances of a class
405,282
<p>I am trying to write a life simulation in python with a variety of animals. It is impossible to name each instance of the classes I am going to use because I have no way of knowing how many there will be.</p> <p>So, my question:</p> <p>How can I automatically give a name to an object?</p> <p>I was thinking of cre...
3
2009-01-01T18:47:32Z
405,297
<p>you could make an 'animal' class with a name attribute.</p> <p>Or</p> <p>you could programmically define the class like so:</p> <pre><code> from new import classobj my_class=classobj('Foo',(object,),{}) </code></pre> <p>Found this: <a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=445037" rel=...
2
2009-01-01T18:53:15Z
[ "python", "class", "object", "multiple-instances" ]
Many instances of a class
405,282
<p>I am trying to write a life simulation in python with a variety of animals. It is impossible to name each instance of the classes I am going to use because I have no way of knowing how many there will be.</p> <p>So, my question:</p> <p>How can I automatically give a name to an object?</p> <p>I was thinking of cre...
3
2009-01-01T18:47:32Z
405,331
<p>Like this?</p> <pre><code>class Animal( object ): pass # lots of details omitted herd= [ Animal() for i in range(10000) ] </code></pre> <p>At this point, herd will have 10,000 distinct instances of the <code>Animal</code> class.</p>
3
2009-01-01T19:17:31Z
[ "python", "class", "object", "multiple-instances" ]
Many instances of a class
405,282
<p>I am trying to write a life simulation in python with a variety of animals. It is impossible to name each instance of the classes I am going to use because I have no way of knowing how many there will be.</p> <p>So, my question:</p> <p>How can I automatically give a name to an object?</p> <p>I was thinking of cre...
3
2009-01-01T18:47:32Z
405,575
<p>If you need a way to refer to them individually, it's relatively common to have the class give each instance a unique identifier on initialization:</p> <pre><code>&gt;&gt;&gt; import itertools &gt;&gt;&gt; class Animal(object): ... id_iter = itertools.count(1) ... def __init__(self): ... self.id...
3
2009-01-01T22:26:20Z
[ "python", "class", "object", "multiple-instances" ]
Many instances of a class
405,282
<p>I am trying to write a life simulation in python with a variety of animals. It is impossible to name each instance of the classes I am going to use because I have no way of knowing how many there will be.</p> <p>So, my question:</p> <p>How can I automatically give a name to an object?</p> <p>I was thinking of cre...
3
2009-01-01T18:47:32Z
405,759
<p>Any instance could have a name attribute. So it sounds like you may be asking how to dynamically name a <em>class</em>, not an <em>instance</em>. If that's the case, you can explicitly set the __name__ attribute of a class, or better yet just create the class with the builtin <a href="http://docs.python.org/librar...
1
2009-01-02T00:44:58Z
[ "python", "class", "object", "multiple-instances" ]
Mysql connection pooling question: is it worth it?
405,352
<p>I recall hearing that the connection process in mysql was designed to be very fast compared to other RDBMSes, and that therefore using <a href="http://www.sqlalchemy.org/">a library that provides connection pooling</a> (SQLAlchemy) won't actually help you that much if you enable the connection pool.</p> <p>Does any...
15
2009-01-01T19:32:29Z
405,357
<p>Even if the connection part of MySQL itself is pretty slick, presumably there's still a network connection involved (whether that's loopback or physical). If you're making a <em>lot</em> of requests, that could get significantly expensive. It will depend (as is so often the case) on exactly what your application doe...
6
2009-01-01T19:37:35Z
[ "python", "mysql", "sqlalchemy", "connection-pooling" ]
Mysql connection pooling question: is it worth it?
405,352
<p>I recall hearing that the connection process in mysql was designed to be very fast compared to other RDBMSes, and that therefore using <a href="http://www.sqlalchemy.org/">a library that provides connection pooling</a> (SQLAlchemy) won't actually help you that much if you enable the connection pool.</p> <p>Does any...
15
2009-01-01T19:32:29Z
405,398
<p>The connection pool speeds things up in that fact that you do not have create a java.sql.Connection object every time you do a database query. I use the Tomcat connection pool to a mysql database for web applications that do a lot of queries, during high user load there is noticeable speed improvement. </p>
0
2009-01-01T20:02:42Z
[ "python", "mysql", "sqlalchemy", "connection-pooling" ]
Mysql connection pooling question: is it worth it?
405,352
<p>I recall hearing that the connection process in mysql was designed to be very fast compared to other RDBMSes, and that therefore using <a href="http://www.sqlalchemy.org/">a library that provides connection pooling</a> (SQLAlchemy) won't actually help you that much if you enable the connection pool.</p> <p>Does any...
15
2009-01-01T19:32:29Z
405,914
<p>There's no need to worry about residual state on a connection when using SQLA's connection pool, unless your application is changing connectionwide options like transaction isolation levels (which generally is not the case). SQLA's connection pool issues a connection.rollback() on the connection when its checked ba...
10
2009-01-02T02:41:50Z
[ "python", "mysql", "sqlalchemy", "connection-pooling" ]
Mysql connection pooling question: is it worth it?
405,352
<p>I recall hearing that the connection process in mysql was designed to be very fast compared to other RDBMSes, and that therefore using <a href="http://www.sqlalchemy.org/">a library that provides connection pooling</a> (SQLAlchemy) won't actually help you that much if you enable the connection pool.</p> <p>Does any...
15
2009-01-01T19:32:29Z
415,006
<p>Short answer: you need to benchmark it.</p> <p>Long answer: it depends. MySQL is fast for connection setup, so avoiding that cost is not a good reason to go for connection pooling. Where you win there is if the queries run are few and fast because then you will see a win with pooling.</p> <p>The other worry is how...
2
2009-01-06T00:10:55Z
[ "python", "mysql", "sqlalchemy", "connection-pooling" ]
Mysql connection pooling question: is it worth it?
405,352
<p>I recall hearing that the connection process in mysql was designed to be very fast compared to other RDBMSes, and that therefore using <a href="http://www.sqlalchemy.org/">a library that provides connection pooling</a> (SQLAlchemy) won't actually help you that much if you enable the connection pool.</p> <p>Does any...
15
2009-01-01T19:32:29Z
7,840,841
<p>I made a simple RESTful service with Django and tested it with and without connection pooling. In my case, the difference was quite noticeable.</p> <p>In a LAN, without it, response time was between 1 and 5 seconds. With it, less than 20 ms. Results may vary, but the configuration I'm using for the MySQL &amp; Apac...
0
2011-10-20T19:00:38Z
[ "python", "mysql", "sqlalchemy", "connection-pooling" ]
Python update object from dictionary
405,489
<p>Is there a built-in function/operator I could use to unpack values from a dictionary and assign it into instance variables?</p> <p>This is what I intend to do:</p> <pre><code>c = MyClass() c.foo = 123 c.bar = 123 # c.foo == 123 and c.bar == 123 d = {'bar': 456} c.update(d) # c.foo == 123 and c.bar == 456 </cod...
11
2009-01-01T21:12:51Z
405,492
<p>Have you tried</p> <pre><code>f.__dict__.update( b ) </code></pre> <p>?</p>
21
2009-01-01T21:14:32Z
[ "python", "iterable-unpacking" ]
Python update object from dictionary
405,489
<p>Is there a built-in function/operator I could use to unpack values from a dictionary and assign it into instance variables?</p> <p>This is what I intend to do:</p> <pre><code>c = MyClass() c.foo = 123 c.bar = 123 # c.foo == 123 and c.bar == 123 d = {'bar': 456} c.update(d) # c.foo == 123 and c.bar == 456 </cod...
11
2009-01-01T21:12:51Z
405,498
<p>Also, maybe it would be good style to have a wrapper around the dict's update method:</p> <pre><code>def update(self, b): self.__dict__.update(b) </code></pre> <p>PS: Sorry for not commenting at @<a href="http://stackoverflow.com/users/10661/s-lott">S.Lott</a> 's post but I don't have the rep yet.</p>
4
2009-01-01T21:21:15Z
[ "python", "iterable-unpacking" ]
Python update object from dictionary
405,489
<p>Is there a built-in function/operator I could use to unpack values from a dictionary and assign it into instance variables?</p> <p>This is what I intend to do:</p> <pre><code>c = MyClass() c.foo = 123 c.bar = 123 # c.foo == 123 and c.bar == 123 d = {'bar': 456} c.update(d) # c.foo == 123 and c.bar == 456 </cod...
11
2009-01-01T21:12:51Z
408,016
<p>there is also another way of doing it by looping through the items in d. this doesn't have the same assuption that they will get stored in <code>c.__dict__</code> which isn't always true. </p> <pre><code>d = {'bar': 456} for key,value in d.items(): setattr(c,key,value) </code></pre> <p>or you could write a <co...
24
2009-01-02T21:02:16Z
[ "python", "iterable-unpacking" ]
Hiding implementation details on an email templating system written in Python
405,509
<p>I am writing an application where one of the features is to allow the user to write an email template using Markdown syntax.</p> <p>Besides formatting, the user must be able to use placeholders for a couple of variables that would get replaced at runtime. </p> <p>The way this is currently working is very simple: t...
0
2009-01-01T21:34:26Z
405,513
<p>Use a real template tool: <a href="http://www.makotemplates.org/" rel="nofollow">mako</a> or <a href="http://jinja.pocoo.org/" rel="nofollow">jinja</a>. Don't roll your own. Not worth it.</p>
6
2009-01-01T21:36:53Z
[ "python", "email", "formatting", "templates" ]
Hiding implementation details on an email templating system written in Python
405,509
<p>I am writing an application where one of the features is to allow the user to write an email template using Markdown syntax.</p> <p>Besides formatting, the user must be able to use placeholders for a couple of variables that would get replaced at runtime. </p> <p>The way this is currently working is very simple: t...
0
2009-01-01T21:34:26Z
405,515
<p>Have a light templating system ... I am not sure if you can use some of the ones TurboGears provides (Kid or Genshi)</p>
2
2009-01-01T21:39:26Z
[ "python", "email", "formatting", "templates" ]
Hiding implementation details on an email templating system written in Python
405,509
<p>I am writing an application where one of the features is to allow the user to write an email template using Markdown syntax.</p> <p>Besides formatting, the user must be able to use placeholders for a couple of variables that would get replaced at runtime. </p> <p>The way this is currently working is very simple: t...
0
2009-01-01T21:34:26Z
406,723
<p>I would recommend <a href="http://jinja.pocoo.org/2/" rel="nofollow">jinja2</a>.</p> <p>It shouldn't create runtime performance issue since it compiles templates to python. It would offer much greater flexibility. As for <em>maintainability</em>; it depends much on the coder, but theoretically (and admittedly super...
1
2009-01-02T12:56:21Z
[ "python", "email", "formatting", "templates" ]
Hiding implementation details on an email templating system written in Python
405,509
<p>I am writing an application where one of the features is to allow the user to write an email template using Markdown syntax.</p> <p>Besides formatting, the user must be able to use placeholders for a couple of variables that would get replaced at runtime. </p> <p>The way this is currently working is very simple: t...
0
2009-01-01T21:34:26Z
406,786
<p>You could try Python 2.6/3.0's new str.format() method: <a href="http://docs.python.org/library/string.html#formatstrings" rel="nofollow">http://docs.python.org/library/string.html#formatstrings</a></p> <p>This looks a bit different to %-formatting and might not be as instantly recognisable as Python:</p> <pre><co...
0
2009-01-02T13:27:39Z
[ "python", "email", "formatting", "templates" ]
if all in list == something
405,516
<p>Using python 2.6 is there a way to check if all the items of a sequence equals a given value, in one statement?</p> <pre><code>[pseudocode] my_sequence = (2,5,7,82,35) if all the values in (type(i) for i in my_sequence) == int: do() </code></pre> <p>instead of, say:</p> <pre><code>my_sequence = (2,5,7,82,35...
13
2009-01-01T21:41:22Z
405,519
<p>Do you mean</p> <pre><code>all( type(i) is int for i in my_list ) </code></pre> <p>?</p> <p>Edit: Changed to <code>is</code>. Slightly faster.</p>
13
2009-01-01T21:43:29Z
[ "python", "python-2.6" ]
if all in list == something
405,516
<p>Using python 2.6 is there a way to check if all the items of a sequence equals a given value, in one statement?</p> <pre><code>[pseudocode] my_sequence = (2,5,7,82,35) if all the values in (type(i) for i in my_sequence) == int: do() </code></pre> <p>instead of, say:</p> <pre><code>my_sequence = (2,5,7,82,35...
13
2009-01-01T21:41:22Z
405,520
<p>Use: </p> <pre><code>all( type(i) is int for i in lst ) </code></pre> <p>Example:</p> <pre><code>In [1]: lst = range(10) In [2]: all( type(i) is int for i in lst ) Out[2]: True In [3]: lst.append('steve') In [4]: all( type(i) is int for i in lst ) Out[4]: False </code></pre> <p>[Edit]. Made cleaner as per commen...
34
2009-01-01T21:45:31Z
[ "python", "python-2.6" ]
if all in list == something
405,516
<p>Using python 2.6 is there a way to check if all the items of a sequence equals a given value, in one statement?</p> <pre><code>[pseudocode] my_sequence = (2,5,7,82,35) if all the values in (type(i) for i in my_sequence) == int: do() </code></pre> <p>instead of, say:</p> <pre><code>my_sequence = (2,5,7,82,35...
13
2009-01-01T21:41:22Z
405,649
<p>I would suggest:</p> <pre><code>if all(isinstance(i, int) for i in my_list): </code></pre> <p><a href="http://www.python.org/doc/current/library/functions.html#all"><code>all</code></a> and <a href="http://www.python.org/doc/current/library/functions.html#any"><code>any</code></a> first appeared in Python 2.5. If ...
5
2009-01-01T23:06:42Z
[ "python", "python-2.6" ]
if all in list == something
405,516
<p>Using python 2.6 is there a way to check if all the items of a sequence equals a given value, in one statement?</p> <pre><code>[pseudocode] my_sequence = (2,5,7,82,35) if all the values in (type(i) for i in my_sequence) == int: do() </code></pre> <p>instead of, say:</p> <pre><code>my_sequence = (2,5,7,82,35...
13
2009-01-01T21:41:22Z
18,727,943
<p>For the sake of completeness I thought I would add the fact that numpy's 'all' is different from the built-in 'all'. If for example running Python through Python(x,y), numpy is loaded automatically (and cannot be unloaded afaik), so when trying to run the above code it produces rather unexpected results:</p> <pre><...
0
2013-09-10T20:13:05Z
[ "python", "python-2.6" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
405,559
<p>Erm, I am not sure as I a haven't used them at all in real life, but it could be used to simulate a nested data structure? See <a href="http://www.cs.nott.ac.uk/~nhn/TFP2006/Papers/27-GhaniHamanaUustaluVene-CyclicStructuresAsNestedDatatypes.pdf" rel="nofollow">this</a> link</p>
1
2009-01-01T22:15:06Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
405,562
<p>One example would be a linked list where the last item points the first. This would allow you to create a fixed number of items but always be able to get a next item.</p>
0
2009-01-01T22:17:08Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
405,564
<p>Lots of things. Circular buffer, for example: you have some collection of data with a front and a back, but an arbitrary number of nodes, and the "next" item from the last should take you back to the first.</p> <p>Graph structures are often cyclic; acyclicity is a special case. Consider, for example, a graph con...
18
2009-01-01T22:17:30Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
405,565
<p>when doing lattice simulations cyclic/toroidal boundary conditions are often used. usually a simple <code>lattice[i%L]</code> would suffice, but i suppose one could create the lattice to be cyclic.</p>
0
2009-01-01T22:17:42Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
405,566
<p>A nested structure could be used in a test case for a garbage collector.</p>
1
2009-01-01T22:18:04Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
405,571
<p>I recently created a cyclic data structure to represent the eight cardinal and ordinal directions. Its useful for each direction to know its neighbors. For instance, Direction.North knows that Direction.NorthEast and Direction.NorthWest are its neighbors. </p> <p>This is cyclic because each neighor knows its neighb...
6
2009-01-01T22:21:42Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
405,592
<p>Suppose you have limited storage, and data constantly accumulates. In many real life cases, you don't mind getting rid of old data, but you don't want to move data. You can use a cyclic vector; implemented using a vector v of size N with two special indices: begin and end, which initiate on 0.</p> <p>Insertion of "...
0
2009-01-01T22:37:49Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
918,091
<p>Cyclic data structures are usually used to represent circular relationships. That sounds obvious, but it happens more than you think. I can't think of any times I've used terribly complicated cyclical data structures, but bidirectional relationships are fairly common. For example, suppose I wanted to make an IM c...
0
2009-05-27T21:27:11Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
918,114
<p>Any kind of object hierarchy where parents know about their children and children know about their parents. I'm always having to deal with this in ORMs because I want databases to know their tables and tables to know what database they're a part of, and so on.</p>
0
2009-05-27T21:32:23Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
918,223
<p>It is a bit confusing since it is a list that contains itself, but the way I made sense of it is to not think of L as a list, but a node, and instead of things in a list, you think of it as other nodes reachable by this node.</p> <p>To put a more real world example, think of them as flight paths from a city.</p> <...
1
2009-05-27T21:58:11Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
918,340
<p><code>L</code> just contains a reference to itself as one of it's elements. Nothing really special about this. </p> <p>There are some obvious uses of cyclical structures where the last element knows about the first element. But this functionality is already covered by regular python lists.</p> <p>You can get the l...
1
2009-05-27T22:38:53Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
918,508
<p>The data structures iterated by <a href="http://en.wikipedia.org/wiki/Deterministic%5FFinite%5FAutomaton" rel="nofollow">deterministic finite automata</a> are often cyclical.</p>
1
2009-05-27T23:35:20Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
What is a cyclic data structure good for?
405,540
<p>I was just reading through <a href="http://books.google.com/books?id=1HxWGezDZcgC&amp;lpg=PP1&amp;dq=learning%20python&amp;pg=PA254#v=onepage&amp;q=cyclic&amp;f=false" rel="nofollow">"Learning Python" by Mark Lutz and came across this code sample</a>:</p> <pre><code> >>> L = ['grail'] >>> L.append(L) >>> L ['grail'...
13
2009-01-01T22:06:09Z
919,535
<p>Let's look at a single practical example.</p> <p>Let us say we're programming a menu navigation for a game. We want to store for each menu-item</p> <ol> <li>The entry's name</li> <li>The other menu we'll reach after pressing it.</li> <li>The action that would be performed when pressing the menu.</li> </ol> <p>Whe...
0
2009-05-28T07:00:04Z
[ "python", "data-structures", "recursion", "cyclic-reference" ]
Function and class documentation best practices for Python
405,582
<p>I am looking for best practices for function/class/module documentation, i.e. comments in the code itself. Ideally I would like a comment template which is both human readable and consumable by Python documentation utilities.</p> <p>I have read the <a href="http://docs.python.org/tutorial/controlflow.html">Python d...
68
2009-01-01T22:30:39Z
405,624
<p>I think the best resource will be <a href="http://docs.python.org/documenting/index.html">Documenting Python</a></p> <p>Quote:</p> <p><code>This document describes the style guide for our documentation, the custom reStructuredText markup introduced to support Python documentation and how it should be used, as well...
5
2009-01-01T22:56:09Z
[ "python", "documentation", "coding-style" ]
Function and class documentation best practices for Python
405,582
<p>I am looking for best practices for function/class/module documentation, i.e. comments in the code itself. Ideally I would like a comment template which is both human readable and consumable by Python documentation utilities.</p> <p>I have read the <a href="http://docs.python.org/tutorial/controlflow.html">Python d...
68
2009-01-01T22:30:39Z
405,663
<p>The best way to learn documentation practices is probably to look at the source code of a well known project. Like the <a href="http://www.djangoproject.com/" rel="nofollow">Djangoproject</a>.</p>
2
2009-01-01T23:16:43Z
[ "python", "documentation", "coding-style" ]
Function and class documentation best practices for Python
405,582
<p>I am looking for best practices for function/class/module documentation, i.e. comments in the code itself. Ideally I would like a comment template which is both human readable and consumable by Python documentation utilities.</p> <p>I have read the <a href="http://docs.python.org/tutorial/controlflow.html">Python d...
68
2009-01-01T22:30:39Z
405,710
<p>You should <a href="http://www.python.org/dev/peps/pep-0287/">use reStructuredText</a> and check out the <a href="http://sphinx.pocoo.org/markup/index.html">Sphinx markup constructs</a>. All the cool kids are doing it.</p> <p>You should <a href="http://www.python.org/dev/peps/pep-0257/">follow docstring conventions...
45
2009-01-01T23:54:52Z
[ "python", "documentation", "coding-style" ]
Function and class documentation best practices for Python
405,582
<p>I am looking for best practices for function/class/module documentation, i.e. comments in the code itself. Ideally I would like a comment template which is both human readable and consumable by Python documentation utilities.</p> <p>I have read the <a href="http://docs.python.org/tutorial/controlflow.html">Python d...
68
2009-01-01T22:30:39Z
406,346
<p>As Emji said, Django is a good project to follow for clear, consistent style guides.</p> <p>For example, their <a href="http://docs.djangoproject.com/en/dev/internals/contributing/?from=olddocs#coding-style">Contribute to Django style guide</a> even goes as far as describing how they'd like to see documentation. S...
8
2009-01-02T09:05:02Z
[ "python", "documentation", "coding-style" ]
Accepting File Argument in Python (from Send To context menu)
405,612
<p>I'm going to start of by noting that I have next to no python experience.</p> <p><img src="http://www.aquate.us/u/9986423875612301299.jpg" alt="alt text" /></p> <p>As you may know, by simply dropping a shortcut in the Send To folder on your Windows PC, you can allow a program to take a file as an argument.</p> <p...
3
2009-01-01T22:50:42Z
405,625
<pre><code>import sys for arg in sys.argv: print arg </code></pre>
1
2009-01-01T22:56:55Z
[ "python", "urllib2", "contextmenu", "sendto" ]
Accepting File Argument in Python (from Send To context menu)
405,612
<p>I'm going to start of by noting that I have next to no python experience.</p> <p><img src="http://www.aquate.us/u/9986423875612301299.jpg" alt="alt text" /></p> <p>As you may know, by simply dropping a shortcut in the Send To folder on your Windows PC, you can allow a program to take a file as an argument.</p> <p...
3
2009-01-01T22:50:42Z
405,629
<ol> <li>Find out what the dragged file was: <a href="http://docs.python.org/library/sys.html#sys.argv">http://docs.python.org/library/sys.html#sys.argv</a></li> <li>Open it: <a href="http://docs.python.org/library/functions.html#open">http://docs.python.org/library/functions.html#open</a></li> <li>Read it in: <a href=...
7
2009-01-01T22:57:47Z
[ "python", "urllib2", "contextmenu", "sendto" ]
insert two values from an mysql table into another table using a python program
405,617
<p>I'm having a small problem with a Python program (below) that I'm writing. </p> <p>I want to insert two values from a MySQL table into another table from a Python program.</p> <p>The two fields are priority and product and I have selected them from the shop table and I want to insert them into the products table. ...
-2
2009-01-01T22:54:43Z
405,668
<p>Well, the same thing again:</p> <pre><code>import MySQLdb def checkOut(): db = MySQLdb.connect(host='localhost', user = 'root', passwd = '$$', db = 'fillmyfridge') cursor = db.cursor(MySQLdb.cursors.DictCursor) user_input = raw_input('please enter the product barcode that you are taking out of the frid...
1
2009-01-01T23:21:40Z
[ "python", "mysql", "database" ]
insert two values from an mysql table into another table using a python program
405,617
<p>I'm having a small problem with a Python program (below) that I'm writing. </p> <p>I want to insert two values from a MySQL table into another table from a Python program.</p> <p>The two fields are priority and product and I have selected them from the shop table and I want to insert them into the products table. ...
-2
2009-01-01T22:54:43Z
405,672
<p>You don't mention what the problem is, but in the code you show this:</p> <pre><code>cursor.execute('insert into products(product, barcode, priority) values (%s, %s)', (rows["product"], user_input, rows["priority"])) </code></pre> <p>where your values clause only has two %s's in it, where it should have three:</p>...
1
2009-01-01T23:22:56Z
[ "python", "mysql", "database" ]
Passing arguments with wildcards to a Python script
405,652
<p>I want to do something like this:</p> <pre><code>c:\data\&gt; python myscript.py *.csv </code></pre> <p>and pass all of the .csv files in the directory to my python script (such that <code>sys.argv</code> contains <code>["file1.csv", "file2.csv"]</code>, etc.) </p> <p>But <code>sys.argv</code> just receives <code...
12
2009-01-01T23:08:01Z
405,662
<p>You can use the glob module, that way you won't depend on the behavior of a particular shell (well, you still depend on the shell not expanding the arguments, but at least you can get this to happen in Unix by escaping the wildcards :-) ).</p> <pre><code>from glob import glob filelist = glob('*.csv') #You can pass ...
20
2009-01-01T23:15:46Z
[ "python", "windows", "command-line", "arguments" ]
Passing arguments with wildcards to a Python script
405,652
<p>I want to do something like this:</p> <pre><code>c:\data\&gt; python myscript.py *.csv </code></pre> <p>and pass all of the .csv files in the directory to my python script (such that <code>sys.argv</code> contains <code>["file1.csv", "file2.csv"]</code>, etc.) </p> <p>But <code>sys.argv</code> just receives <code...
12
2009-01-01T23:08:01Z
405,667
<p>In Unix, the shell expands wildcards, so programs get the expanded list of filenames. Windows doesn't do this: the shell passes the wildcards directly to the program, which has to expand them itself.</p> <p>Vinko is right: the glob module does the job:</p> <pre><code>import glob, sys for arg in glob.glob(sys.arg...
11
2009-01-01T23:19:50Z
[ "python", "windows", "command-line", "arguments" ]
Modifying Microsoft Outlook contacts from Python
405,724
<p>I have written a few Python tools in the past to extract data from my Outlook contacts. Now, I am trying to <em>modify</em> my Outlook Contacts. I am finding that my changes are being noted by Outlook, but they aren't sticking. I seem to be updating some cache, but not the real record.</p> <p>The code is straightfo...
9
2009-01-02T00:11:57Z
405,758
<p>I believe there is a .Save() method on the contact, so you need to add:</p> <p>contact.Save()</p>
6
2009-01-02T00:44:33Z
[ "python", "winapi", "outlook", "mapi" ]
Floating Point Limitations
406,361
<p>My code:</p> <pre><code>a = '2.3' </code></pre> <p>I wanted to display <code>a</code> as a floating point value.</p> <p>Since <code>a</code> is a string, I tried:</p> <pre><code>float(a) </code></pre> <p>The result I got was :</p> <pre><code>2.2999999999999998 </code></pre> <p>I want a solution for this probl...
4
2009-01-02T09:17:26Z
406,367
<p>I think it reflects more on your understanding of floating point types than on Python. See <a href="http://csharpindepth.com/Articles/General/FloatingPoint.aspx" rel="nofollow">my article about floating point numbers</a> (.NET-based, but still relevant) for the reasons behind this "inaccuracy". If you need to keep t...
18
2009-01-02T09:22:15Z
[ "python", "floating-point", "precision", "floating-accuracy" ]
Floating Point Limitations
406,361
<p>My code:</p> <pre><code>a = '2.3' </code></pre> <p>I wanted to display <code>a</code> as a floating point value.</p> <p>Since <code>a</code> is a string, I tried:</p> <pre><code>float(a) </code></pre> <p>The result I got was :</p> <pre><code>2.2999999999999998 </code></pre> <p>I want a solution for this probl...
4
2009-01-02T09:17:26Z
406,373
<p>This is not a drawback of python, rather, it is a drawback of the way floating point numbers are stored on a computer. Regardless of implementation language, you will find similar problems.</p> <p>You say that you want to 'display' A as a floating point, why not just display the string? Visually it will be identica...
6
2009-01-02T09:27:15Z
[ "python", "floating-point", "precision", "floating-accuracy" ]
Floating Point Limitations
406,361
<p>My code:</p> <pre><code>a = '2.3' </code></pre> <p>I wanted to display <code>a</code> as a floating point value.</p> <p>Since <code>a</code> is a string, I tried:</p> <pre><code>float(a) </code></pre> <p>The result I got was :</p> <pre><code>2.2999999999999998 </code></pre> <p>I want a solution for this probl...
4
2009-01-02T09:17:26Z
407,366
<p>Excellent answers explaining reasons. I just wish to add a possible practical solution from the standard library:</p> <pre><code>&gt;&gt;&gt; from decimal import Decimal &gt;&gt;&gt; a = Decimal('2.3') &gt;&gt;&gt; print a 2.3 </code></pre> <p>This is actually a (very) F.A.Q. for Python and you can <a href="http:/...
4
2009-01-02T16:43:44Z
[ "python", "floating-point", "precision", "floating-accuracy" ]
Why does Ruby have Rails while Python has no central framework?
406,907
<p>This is a(n) historical question, not a comparison-between-languages question:</p> <p><a href="http://tomayko.com/writings/no-rails-for-python">This article from 2005</a> talks about the lack of a single, central framework for Python. For Ruby, this framework is clearly Rails. <strong>Why, historically speaking, di...
8
2009-01-02T14:26:57Z
406,925
<p>I don't think it's right to characterise Rails as 'the' 'single' 'central' Ruby framework.</p> <p>Other frameworks for Ruby include Merb, Camping and Ramaze.</p> <p>... which sort of invalidates the question.</p>
7
2009-01-02T14:33:24Z
[ "python", "ruby-on-rails", "ruby", "frameworks", "history" ]
Why does Ruby have Rails while Python has no central framework?
406,907
<p>This is a(n) historical question, not a comparison-between-languages question:</p> <p><a href="http://tomayko.com/writings/no-rails-for-python">This article from 2005</a> talks about the lack of a single, central framework for Python. For Ruby, this framework is clearly Rails. <strong>Why, historically speaking, di...
8
2009-01-02T14:26:57Z
406,940
<p>Rails was somewhat revolutionary in its extreme "convention over configuration" approach which set it apart from pretty much anything else and made it the "killer app" of Ruby, causing a lot of people to notice Ruby in the first place. </p> <p>So the question is really "Why did David Hansson decide to write Rails i...
5
2009-01-02T14:39:57Z
[ "python", "ruby-on-rails", "ruby", "frameworks", "history" ]
Why does Ruby have Rails while Python has no central framework?
406,907
<p>This is a(n) historical question, not a comparison-between-languages question:</p> <p><a href="http://tomayko.com/writings/no-rails-for-python">This article from 2005</a> talks about the lack of a single, central framework for Python. For Ruby, this framework is clearly Rails. <strong>Why, historically speaking, di...
8
2009-01-02T14:26:57Z
406,949
<p>Remember that Ruby had existed for a long time before Rails was created. According to Wikipedia, Ruby was created in the mid-90's; Rails didn't come around until 2004. Ruby is simply the language that David Hansson chose to use for Rails.</p> <p>And yes, I would say <b>Ruby is to Rails as Python is to Django</b>....
5
2009-01-02T14:43:19Z
[ "python", "ruby-on-rails", "ruby", "frameworks", "history" ]