title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
Python String Cleanup + Manipulation (Accented Characters)
930,303
<p>I have a database full of names like:</p> <pre><code>John Smith Scott J. Holmes Dr. Kaplan Ray's Dog Levi's Adrian O'Brien Perry Sean Smyre Carie Burchfield-Thompson Björn Árnason </code></pre> <p>There are a few foreign names with accents in them that need to be converted to strings with non-acc...
5
2009-05-30T18:36:16Z
931,051
<p>The following function is generic:</p> <pre><code>import unicodedata def not_combining(char): return unicodedata.category(char) != 'Mn' def strip_accents(text, encoding): unicode_text= unicodedata.normalize('NFD', text.decode(encoding)) return filter(not_combining, unicode_text).encode(enc...
3
2009-05-31T01:43:14Z
[ "python", "regex", "unicode", "string" ]
Python String Cleanup + Manipulation (Accented Characters)
930,303
<p>I have a database full of names like:</p> <pre><code>John Smith Scott J. Holmes Dr. Kaplan Ray's Dog Levi's Adrian O'Brien Perry Sean Smyre Carie Burchfield-Thompson Björn Árnason </code></pre> <p>There are a few foreign names with accents in them that need to be converted to strings with non-acc...
5
2009-05-30T18:36:16Z
1,333,215
<p>If you are not afraid to install third-party modules, then have a look at the <a href="http://www.tablix.org/~avian/blog/archives/2009/01/unicode_transliteration_in_python/" rel="nofollow">python port of the Perl module <code>Text::Unidecode</code></a> (it's also <a href="http://pypi.python.org/pypi/Unidecode" rel="...
5
2009-08-26T08:36:53Z
[ "python", "regex", "unicode", "string" ]
Getting the last element of a list in Python
930,397
<p>In Python, how do you get the last element of a list?</p>
826
2009-05-30T19:28:53Z
930,398
<p><code>some_list[-1]</code> is the shortest and most Pythonic.</p> <p>In fact, you can do much more with this syntax. The <code>some_list[-n]</code> syntax gets the nth-to-last element. So <code>some_list[-1]</code> gets the last element, <code>some_list[-2]</code> gets the second to last, etc, all the way down to <...
1,347
2009-05-30T19:29:55Z
[ "python", "list", "indexing" ]
Getting the last element of a list in Python
930,397
<p>In Python, how do you get the last element of a list?</p>
826
2009-05-30T19:28:53Z
930,759
<p>You can also do:</p> <pre><code>alist.pop() </code></pre> <p>It depends on what you want to do with your list because the <code>pop()</code> method will delete the last element.</p>
49
2009-05-30T22:21:08Z
[ "python", "list", "indexing" ]
Getting the last element of a list in Python
930,397
<p>In Python, how do you get the last element of a list?</p>
826
2009-05-30T19:28:53Z
4,139,773
<p>If your str() or list() objects might end up being empty as so: <code>astr = ''</code> or <code>alist = []</code>, then you might want to use <code>alist[-1:]</code> instead of <code>alist[-1]</code> for object "sameness".</p> <p>The significance of this is:</p> <pre><code>alist = [] alist[-1] # will generate an...
116
2010-11-09T23:26:38Z
[ "python", "list", "indexing" ]
Getting the last element of a list in Python
930,397
<p>In Python, how do you get the last element of a list?</p>
826
2009-05-30T19:28:53Z
12,042,371
<p>The simplest way to display last element in python is</p> <pre><code>&gt;&gt;&gt; list[-1:] # returns indexed value [3] &gt;&gt;&gt; list[-1] # returns value 3 </code></pre> <p>there are many other method to achieve such a goal but these are short and sweet to use.</p>
32
2012-08-20T17:41:54Z
[ "python", "list", "indexing" ]
Getting the last element of a list in Python
930,397
<p>In Python, how do you get the last element of a list?</p>
826
2009-05-30T19:28:53Z
31,620,887
<pre><code>popes = ['john paul II', 'benedict XIV', 'francis I'] current_pope = [] for pope in popes: current_pope = pope </code></pre> <p>after the for loop you'll get last item in current_pope variable.</p>
-8
2015-07-24T22:45:23Z
[ "python", "list", "indexing" ]
Getting the last element of a list in Python
930,397
<p>In Python, how do you get the last element of a list?</p>
826
2009-05-30T19:28:53Z
32,998,601
<p>Ok, but what about common in almost every language way <code>items[len(items) - 1]</code>? This is IMO the easiest way to get last element, because it does not require anything <em>pythonic</em> knowledge.</p>
-1
2015-10-07T17:22:04Z
[ "python", "list", "indexing" ]
Getting the last element of a list in Python
930,397
<p>In Python, how do you get the last element of a list?</p>
826
2009-05-30T19:28:53Z
34,705,384
<blockquote> <p>some_list = [1, 2, 3]</p> </blockquote> <p>Method 1:</p> <pre><code>some_list[-1] </code></pre> <p>Method 2:</p> <pre><code>**some_list.reverse()** **some_list[0]** </code></pre> <p>Method 3:</p> <pre><code>some_list.pop() </code></pre>
5
2016-01-10T12:32:06Z
[ "python", "list", "indexing" ]
Getting the last element of a list in Python
930,397
<p>In Python, how do you get the last element of a list?</p>
826
2009-05-30T19:28:53Z
39,908,383
<p><code>list[-1]</code> will retrieve the last element of the list without changing the list. <code>list.pop()</code> will retrieve the last element of the list, but it will mutate/change the original list. Usually, mutating the original list is not recommended.</p> <p>Alternatively, if, for some reason, you're looki...
0
2016-10-07T02:21:23Z
[ "python", "list", "indexing" ]
How to run one last function before getting killed in Python?
930,519
<p>Is there any way to run one last command before a running Python script is stopped by being killed by some other script, keyboard interrupt etc.</p> <p>Thanks for your help!</p>
16
2009-05-30T20:22:53Z
930,521
<pre><code>import signal import sys import time def cleanup(*args): print 'Exiting' sys.exit(0) signal.signal(signal.SIGINT, cleanup) signal.signal(signal.SIGTERM, cleanup) while True: time.sleep(60) # less busy loop </code></pre>
6
2009-05-30T20:24:34Z
[ "python", "function", "interrupt", "quit" ]
How to run one last function before getting killed in Python?
930,519
<p>Is there any way to run one last command before a running Python script is stopped by being killed by some other script, keyboard interrupt etc.</p> <p>Thanks for your help!</p>
16
2009-05-30T20:22:53Z
930,537
<pre><code>import time try: time.sleep(10) finally: print "clean up" clean up Traceback (most recent call last): File "&lt;stdin&gt;", line 2, in &lt;module&gt; KeyboardInterrupt </code></pre> <p>If you need to catch other OS level interrupts, look at the signal module:</p> <p><a href="http://docs.python....
28
2009-05-30T20:29:50Z
[ "python", "function", "interrupt", "quit" ]
How to run one last function before getting killed in Python?
930,519
<p>Is there any way to run one last command before a running Python script is stopped by being killed by some other script, keyboard interrupt etc.</p> <p>Thanks for your help!</p>
16
2009-05-30T20:22:53Z
930,542
<p>I think that you could use the <code>atexit</code> module. With it, you can register a function which will be called at program termination. An example from here: <a href="http://docs.python.org/library/atexit.html">http://docs.python.org/library/atexit.html</a></p> <pre><code>try: _count = int(open("/tmp/count...
8
2009-05-30T20:32:14Z
[ "python", "function", "interrupt", "quit" ]
How to run one last function before getting killed in Python?
930,519
<p>Is there any way to run one last command before a running Python script is stopped by being killed by some other script, keyboard interrupt etc.</p> <p>Thanks for your help!</p>
16
2009-05-30T20:22:53Z
930,545
<p>Use the atexit module to register a function that will be called at the end.</p> <pre><code>import atexit atexit.register(some_function) </code></pre>
3
2009-05-30T20:32:20Z
[ "python", "function", "interrupt", "quit" ]
How to run one last function before getting killed in Python?
930,519
<p>Is there any way to run one last command before a running Python script is stopped by being killed by some other script, keyboard interrupt etc.</p> <p>Thanks for your help!</p>
16
2009-05-30T20:22:53Z
34,575,239
<p>WIth apologies to 'Unknown' for taking their answer and correcting it as though it was my own answer, but my edits were rejected.</p> <p>The approved answer contains an error that will cause a segfault.</p> <p>You cannot use sys.exit() in a signal handler, but you can use os._exit so that it becomes:</p> <pre><co...
3
2016-01-03T09:55:32Z
[ "python", "function", "interrupt", "quit" ]
Python AppEngine; get user info and post parameters?
930,578
<p>Im checking the examples google gives on how to start using python; especifically the code posted here; <a href="http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html" rel="nofollow">http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html</a></p> <p>The thing that i...
2
2009-05-30T20:48:40Z
931,034
<p>The problem is, <code>self.redirect</code> cannot "carry along" the payload of a <code>POST</code> HTTP request, so (from a <code>post</code> method) the redirection to the login-url &amp;c is going to misbehave (in fact I believe the login URL will use <code>get</code> to continue when it's done, and that there's n...
3
2009-05-31T01:33:47Z
[ "python", "google-app-engine" ]
Django Shell shortcut in Windows
930,641
<p>I'm trying to write a bat file so I can quickly launch into the Interactive Shell for one of my Django projects.</p> <p>Basically I need to write a python script that can launch "manage.py shell" and then be able to print from mysite.myapp.models import *</p> <p>The problem is manage.py shell cannot take additiona...
0
2009-05-30T21:12:42Z
930,910
<p>First download django-extensions from google code. search for "django command-extensions"</p> <p>Download and install it by running setup.py install from within the folder (it has a file called "setup.py") You will then be able to run manage.py shell_plus instead of manage.py shell, giving you an enhanced version o...
2
2009-05-31T00:09:29Z
[ "python", "django", "command-line", "windows-xp", "batch-file" ]
Python & parsing IRC messages
930,700
<p>What's the best way to parse messages received from an IRC server with Python according to the RFC? I simply want some kind of list/whatever, for example:</p> <pre><code>:test!~test@test.com PRIVMSG #channel :Hi! </code></pre> <p>becomes this:</p> <pre><code>{ "sender" : "test!~test@test.com", "target" : "#channe...
6
2009-05-30T21:51:58Z
930,706
<p>Look at Twisted's implementation <a href="http://twistedmatrix.com/">http://twistedmatrix.com/</a></p> <p>Unfortunately I'm out of time, maybe someone else can paste it here for you.</p> <h3>Edit</h3> <p>Well I'm back, and strangely no one has pasted it yet so here it is:</p> <p><a href="http://twistedmatrix.com...
16
2009-05-30T21:58:04Z
[ "python", "parsing", "irc" ]
Python & parsing IRC messages
930,700
<p>What's the best way to parse messages received from an IRC server with Python according to the RFC? I simply want some kind of list/whatever, for example:</p> <pre><code>:test!~test@test.com PRIVMSG #channel :Hi! </code></pre> <p>becomes this:</p> <pre><code>{ "sender" : "test!~test@test.com", "target" : "#channe...
6
2009-05-30T21:51:58Z
930,767
<p>You can do it with a simple list comprehension if the format is always like this.</p> <pre><code>keys = ['sender', 'type', 'target', 'message'] s = ":test!~test@test.com PRIVMSG #channel :Hi!" dict((key, value.lstrip(':')) for key, value in zip(keys, s.split())) </code></pre> <p>Result:</p> <pre><code>{'message':...
1
2009-05-30T22:26:11Z
[ "python", "parsing", "irc" ]
Python & parsing IRC messages
930,700
<p>What's the best way to parse messages received from an IRC server with Python according to the RFC? I simply want some kind of list/whatever, for example:</p> <pre><code>:test!~test@test.com PRIVMSG #channel :Hi! </code></pre> <p>becomes this:</p> <pre><code>{ "sender" : "test!~test@test.com", "target" : "#channe...
6
2009-05-30T21:51:58Z
930,806
<p>Do you just want to parse IRC Messages in general or do you want just parse PRIVMSGs? However I have a implementation for that.</p> <pre><code>def parse_message(s): prefix = '' trailing = '' if s.startswith(':'): prefix, s = s[1:].split(' ', 1) if ' :' in s: s, trailing = s.split('...
0
2009-05-30T22:44:01Z
[ "python", "parsing", "irc" ]
Python & parsing IRC messages
930,700
<p>What's the best way to parse messages received from an IRC server with Python according to the RFC? I simply want some kind of list/whatever, for example:</p> <pre><code>:test!~test@test.com PRIVMSG #channel :Hi! </code></pre> <p>becomes this:</p> <pre><code>{ "sender" : "test!~test@test.com", "target" : "#channe...
6
2009-05-30T21:51:58Z
931,274
<p>If you want to keep to a low-level hacking I second the Twisted answer by Unknown, but first I think you should take a look at the very recently announced <strong><a href="http://zork.net/motd/nick/django/introducing-yardbird.html" rel="nofollow">Yardbird</a></strong> which is a nice request parsing layer on top of ...
0
2009-05-31T04:37:44Z
[ "python", "parsing", "irc" ]
Python & parsing IRC messages
930,700
<p>What's the best way to parse messages received from an IRC server with Python according to the RFC? I simply want some kind of list/whatever, for example:</p> <pre><code>:test!~test@test.com PRIVMSG #channel :Hi! </code></pre> <p>becomes this:</p> <pre><code>{ "sender" : "test!~test@test.com", "target" : "#channe...
6
2009-05-30T21:51:58Z
2,288,155
<p>I know it's not Python, but for a regular expression-based approach to this problem, you could take a look at <a href="http://github.com/bingos/poe-filter-ircd/blob/master/lib/POE/Filter/IRCD.pm" rel="nofollow">POE::Filter::IRCD</a>, which handles IRC server protocol (see <a href="http://github.com/bingos/poe-compon...
0
2010-02-18T11:21:55Z
[ "python", "parsing", "irc" ]
Python regex - conditional matching?
930,834
<p>I don't know if that's the right word for it, but I am trying to come up with some regex's that can extract coefficients and exponents from a mathematical expression. The expression will come in the form 'axB+cxD+exF' where the lower case letters are the coefficients and the uppercase letters are the exponents. I ...
0
2009-05-30T23:09:58Z
930,841
<pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; equation = '3x3+6x2+2x1+8x0' &gt;&gt;&gt; re.findall(r'x([0-9]+)', equation) ['3', '2', '1', '0'] &gt;&gt;&gt; re.findall(r'([0-9]+)x', equation) ['3', '6', '2', '8'] </code></pre>
1
2009-05-30T23:14:51Z
[ "python", "regex" ]
Python regex - conditional matching?
930,834
<p>I don't know if that's the right word for it, but I am trying to come up with some regex's that can extract coefficients and exponents from a mathematical expression. The expression will come in the form 'axB+cxD+exF' where the lower case letters are the coefficients and the uppercase letters are the exponents. I ...
0
2009-05-30T23:09:58Z
930,849
<p>You can use <a href="http://www.regular-expressions.info/lookaround.html#lookahead" rel="nofollow">positive look-ahead</a> to match something that is followed by something else. To match the coefficients, you can use:</p> <pre><code>&gt;&gt;&gt; s = '3x3+6x2+2x1+8x0' &gt;&gt;&gt; re.findall(r'\d+(?=x)', s) ['3', '6...
5
2009-05-30T23:22:25Z
[ "python", "regex" ]
Python regex - conditional matching?
930,834
<p>I don't know if that's the right word for it, but I am trying to come up with some regex's that can extract coefficients and exponents from a mathematical expression. The expression will come in the form 'axB+cxD+exF' where the lower case letters are the coefficients and the uppercase letters are the exponents. I ...
0
2009-05-30T23:09:58Z
930,873
<p>Yet another way to do it, without regex: </p> <pre><code>&gt;&gt;&gt; eq = '3x3+6x2+2x1+8x0' &gt;&gt;&gt; op = eq.split('+') ['3x3', '6x2', '2x1', '8x0'] &gt;&gt;&gt; [o.split('x')[0] for o in op] ['3', '6', '2', '8'] &gt;&gt;&gt; [o.split('x')[1] for o in op] ['3', '2', '1', '0'] </code></pre>
1
2009-05-30T23:39:36Z
[ "python", "regex" ]
Referencing another project
930,857
<p>In a simple program I made, I wanted to get a list from another project and access the elements from it. Since I'm new to python, I don't really have any idea what to do. In my project, I checked the box for the project name I wanted to reference and... I don't know what to do. A few google searched did me no good, ...
2
2009-05-30T23:28:44Z
930,879
<p>Use import. Then you can access the "module" (project) and everything in it like an object./</p> <pre><code># a.py some_list = [1,2,3,4] # b.py import a print a.some_list </code></pre> <p>If you run b.py, it will print [1,2,3,4]</p>
2
2009-05-30T23:44:31Z
[ "python" ]
Referencing another project
930,857
<p>In a simple program I made, I wanted to get a list from another project and access the elements from it. Since I'm new to python, I don't really have any idea what to do. In my project, I checked the box for the project name I wanted to reference and... I don't know what to do. A few google searched did me no good, ...
2
2009-05-30T23:28:44Z
930,880
<p>Generally if you have some code in <code>project1.py</code> and then want to use it from a different file in the same directory, you can import <code>project1</code> as a module:</p> <pre><code>import project1 data = project1.my_data_function() </code></pre> <p>This works just the same way as you would import any...
1
2009-05-30T23:44:36Z
[ "python" ]
how to sort by a computed value in django
930,865
<p>Hey I want to sort objects based on a computed value in django... how do I do it?</p> <p>Here is an example User profile model based on stack overflow that explains my predicament:</p> <pre><code>class Profile(models.Model): user = models.ForeignKey(User) def get_reputation(): ... return ...
5
2009-05-30T23:34:20Z
930,894
<p>Since your calculation code exists only within Python, you have to perform the sorting in Python as well:</p> <pre><code>sorted (Profile.objects.all (), key = lambda p: p.reputation) </code></pre>
16
2009-05-30T23:54:29Z
[ "python", "django", "web-applications", "sorting", "django-models" ]
how to sort by a computed value in django
930,865
<p>Hey I want to sort objects based on a computed value in django... how do I do it?</p> <p>Here is an example User profile model based on stack overflow that explains my predicament:</p> <pre><code>class Profile(models.Model): user = models.ForeignKey(User) def get_reputation(): ... return ...
5
2009-05-30T23:34:20Z
931,048
<p>If you need to do the sorting in the database (because you have lots of records, and need to e.g. paginate them), the only real option is to turn reputation into a denormalized field (e.g. updated in an overridden <code>save()</code> method on the model).</p>
3
2009-05-31T01:41:57Z
[ "python", "django", "web-applications", "sorting", "django-models" ]
how to sort by a computed value in django
930,865
<p>Hey I want to sort objects based on a computed value in django... how do I do it?</p> <p>Here is an example User profile model based on stack overflow that explains my predicament:</p> <pre><code>class Profile(models.Model): user = models.ForeignKey(User) def get_reputation(): ... return ...
5
2009-05-30T23:34:20Z
33,516,286
<p>As of Django-1.8, it is possible to sort a QuerySet using <a href="https://docs.djangoproject.com/en/1.8/ref/models/expressions/" rel="nofollow">query expressions</a> as long as the computed value can be rendered in SQL. You can also use <a href="https://docs.djangoproject.com/en/1.8/topics/db/aggregation/#order-by"...
2
2015-11-04T07:44:10Z
[ "python", "django", "web-applications", "sorting", "django-models" ]
assertEquals vs. assertEqual in python
930,995
<p>Is there a difference between <code>assertEquals</code> and <code>assertEqual</code> in the python <code>unittest.TestCase</code>?</p> <p>And if there is not, why are there two functions? Only for convenience? </p>
105
2009-05-31T01:07:03Z
931,005
<p>I don't find any mention of assertEquals in <a href="http://docs.python.org/library/unittest.html" rel="nofollow">http://docs.python.org/library/unittest.html</a>. However, when I import TestCase and then do a "help(TestCase)", it's listed. I think it's just a synonym for convenience.</p>
2
2009-05-31T01:14:37Z
[ "python", "unit-testing" ]
assertEquals vs. assertEqual in python
930,995
<p>Is there a difference between <code>assertEquals</code> and <code>assertEqual</code> in the python <code>unittest.TestCase</code>?</p> <p>And if there is not, why are there two functions? Only for convenience? </p>
105
2009-05-31T01:07:03Z
931,011
<p>Good question!</p> <p>Actually, in Python 2.6, both <code>assertEqual</code> and <code>assertEquals</code> are convenience aliases to <code>failUnlessEqual</code>. The source declares them thus: </p> <pre><code> # Synonyms for assertion methods assertEqual = assertEquals = failUnlessEqual </code></pre> <p>In <a ...
121
2009-05-31T01:17:47Z
[ "python", "unit-testing" ]
assertEquals vs. assertEqual in python
930,995
<p>Is there a difference between <code>assertEquals</code> and <code>assertEqual</code> in the python <code>unittest.TestCase</code>?</p> <p>And if there is not, why are there two functions? Only for convenience? </p>
105
2009-05-31T01:07:03Z
937,144
<p>I think this was tension between the "only one obvious way to do it" vs. "alias to make the overall code flow semantically". Personally I found I like to read</p> <pre><code>failIf(some_condition) </code></pre> <p>over </p> <pre><code>assertFalse(some_condition) </code></pre> <p>but liked</p> <pre><code>assert...
4
2009-06-01T22:35:26Z
[ "python", "unit-testing" ]
assertEquals vs. assertEqual in python
930,995
<p>Is there a difference between <code>assertEquals</code> and <code>assertEqual</code> in the python <code>unittest.TestCase</code>?</p> <p>And if there is not, why are there two functions? Only for convenience? </p>
105
2009-05-31T01:07:03Z
21,993,198
<p>A 3.3 update: From <a href="http://docs.python.org/3.3/library/unittest.html#deprecated-aliases">26.3.7.1.1. Deprecated aliases</a> :</p> <blockquote> <p>For historical reasons, some of the TestCase methods had one or more aliases that are now deprecated. The following table lists the correct names along with the...
21
2014-02-24T16:21:40Z
[ "python", "unit-testing" ]
assertEquals vs. assertEqual in python
930,995
<p>Is there a difference between <code>assertEquals</code> and <code>assertEqual</code> in the python <code>unittest.TestCase</code>?</p> <p>And if there is not, why are there two functions? Only for convenience? </p>
105
2009-05-31T01:07:03Z
27,852,282
<p>Not just for Python 3.x, since Python 2.7 <code>assertEquals</code> has been deprecated as well:</p> <pre><code>Method Name | Deprecated alias(es) _________________________________________________________ assertEqual() | failUnlessEqual, assertEquals </code></pre> <p>From <a href="https://docs....
5
2015-01-09T00:58:59Z
[ "python", "unit-testing" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
931,095
<p>How about:</p> <pre><code>&gt;&gt;&gt; 'hello world'[::-1] 'dlrow olleh' </code></pre> <p>This is <a href="http://docs.python.org/2/whatsnew/2.3.html#extended-slices">extended slice</a> syntax. It works by doing <code>[begin:end:step]</code> - by leaving begin and end off and specifying a step of -1, it reverses a...
1,576
2009-05-31T02:11:46Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
931,099
<p>@Paolo's <code>s[::-1]</code> is fastest; a slower approach (maybe more readable, but that's debatable) is <code>''.join(reversed(s))</code>.</p>
155
2009-05-31T02:13:54Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
26,828,946
<pre><code>def reverse(st): rev = "" for i in range(0 ,len(st)): rev += st[(len(st) -1) - i] return rev </code></pre>
-2
2014-11-09T13:56:00Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
27,401,625
<p>Reverse a string in python without using reversed() or [::-1]</p> <pre><code>def reverse(test): n = len(test) x="" for i in range(n-1,-1,-1): x += test[i] return x </code></pre>
3
2014-12-10T12:57:22Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
27,843,760
<blockquote> <h1>What is the best way of implementing a reverse function for strings?</h1> <p>There is no built-in reverse function in Python's str object. </p> </blockquote> <p>In my long illustrious career of being a programmer, I have never seen a practical need to reverse a string. So congratulations, you m...
51
2015-01-08T15:32:55Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
30,035,731
<p>Here is a no fancy one:</p> <pre><code>def reverse(text): r_text = '' index = len(text) - 1 while index &gt;= 0: r_text += text[index] #string canbe concatenated index -= 1 return r_text print reverse("hello, world!") </code></pre>
3
2015-05-04T17:02:51Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
31,064,900
<pre><code>def reverse(input): return reduce(lambda x,y : y+x, input) </code></pre>
1
2015-06-26T04:25:59Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
33,457,266
<h2>Quick Answer (TL;DR)</h2> <h3>Example</h3> <pre><code>### example01 ------------------- mystring = 'coup_ate_grouping' backwards = mystring[::-1] print backwards ### ... or even ... mystring = 'coup_ate_grouping'[::-1] print mystring ### result01 ------------------- ''' gnipuorg_eta_puoc ''' </code></pr...
12
2015-10-31T22:24:14Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
34,512,034
<p>Here is one without <code>[::-1]</code> or <code>reversed</code> (for learning purposes):</p> <pre><code>def reverse(text): new_string = [] n = len(text) while (n &gt; 0): new_string.append(text[n-1]) n -= 1 return ''.join(new_string) print reverse("abcd") </code></pre> <p>you can u...
1
2015-12-29T13:23:57Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
35,174,761
<p>Sure, in Python you can do very fancy 1-line stuff. :) <br> Here's a simple, all rounder solution that could work in any programming language.</p> <pre><code>def reverse_string(phrase): reversed = "" length = len(phrase) for i in range(length): reversed += phrase[length-1-i] return reversed ...
0
2016-02-03T10:40:43Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
35,247,129
<pre><code>for i in list: rev = (i[::-1]) reverse_list.append(rev) </code></pre>
-3
2016-02-06T22:14:15Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
36,207,493
<pre><code>s = 'hello' ln = len(s) i = 1 while True: rev = s[ln-i] print rev, i = i + 1 if i == ln + 1 : break </code></pre> <p><strong>OUTPUT :</strong> </p> <pre><code>o l l e h </code></pre>
0
2016-03-24T18:28:53Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
36,350,837
<p>A lesser perplexing way to look at it would be:</p> <pre><code>string = 'happy' print(string) </code></pre> <blockquote> <p>'happy'</p> </blockquote> <pre><code>string_reversed = string[-1::-1] print(string_reversed) </code></pre> <blockquote> <p>'yppah'</p> </blockquote> <p>In English [-1::-1] reads as:</p...
1
2016-04-01T07:49:28Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
38,465,000
<p>You can use the reversed function with a list comprehesive. But I don't understand why this method was eliminated in python 3, was unnecessarily. </p> <pre><code>string = [ char for char in reversed(string)] </code></pre>
0
2016-07-19T17:32:45Z
[ "python", "string" ]
Reverse a string in Python
931,092
<p>There is no built in <code>reverse</code> function in Python's <code>str</code> object. What is the best way of implementing this?</p> <p>If supplying a very concise answer, please elaborate on it's efficiency. Is the <code>str</code> converted to a different object, etc. </p>
751
2009-05-31T02:10:10Z
39,564,634
<p>We can also implement a stack approach to this problem by using the built-in lists in python.</p> <pre><code>def revstring(mystr): revstr = '' strList = [] for ch in mystr: strList.append(ch) while not strList == []: revstr = revstr + strList .pop() return revstr print revstring('apple') prin...
0
2016-09-19T02:39:43Z
[ "python", "string" ]
Debug some Python code
931,211
<p>Edit: You can get the full source here: <a href="http://pastebin.com/m26693" rel="nofollow">http://pastebin.com/m26693</a></p> <p>Edit again: I added some highlights to the pastebin page. <a href="http://pastebin.com/m10f8d239" rel="nofollow">http://pastebin.com/m10f8d239</a></p> <p>I'm probably going to regret as...
0
2009-05-31T03:47:57Z
931,229
<p>It appears that your code was truncated, so I can't look through it.</p> <p>Given that you only get the extra argument on methods defined in a class, though, might it be the <code>self</code> variable? Every method on a Python class receives <code>self</code> as the first parameter, and if you don't account for it...
2
2009-05-31T04:03:28Z
[ "python", "debugging" ]
Debug some Python code
931,211
<p>Edit: You can get the full source here: <a href="http://pastebin.com/m26693" rel="nofollow">http://pastebin.com/m26693</a></p> <p>Edit again: I added some highlights to the pastebin page. <a href="http://pastebin.com/m10f8d239" rel="nofollow">http://pastebin.com/m10f8d239</a></p> <p>I'm probably going to regret as...
0
2009-05-31T03:47:57Z
931,388
<p>In class Foo, when you call </p> <pre><code> def __init__(self, visitor): visitor.AddFunction("foo", -1, self.foo) </code></pre> <p>...you are adding what's called a "bound" method argument (that is, <code>self.foo</code>). It is like a function that already has the self argument specified. The reason ...
1
2009-05-31T05:34:33Z
[ "python", "debugging" ]
Coverage not showing executed lines in virtualenv
931,248
<p>I have a project and I am trying to run nosetests with coverage. I am running in a virtualenv. When I run</p> <pre><code>$ python setup.py nosetests </code></pre> <p>The tests run fine but coverage is not showing that any code is executed (coverage is all 0%).</p> <pre> Name Stmts ...
2
2009-05-31T04:20:41Z
932,210
<p>This is going to require some back and forth. How can I see your code?</p> <p>And why did you come to stackoverflow for an answer rather than to the developer (that is, me)? :)</p>
2
2009-05-31T15:19:34Z
[ "python", "osx", "code-coverage", "virtualenv", "nosetests" ]
Coverage not showing executed lines in virtualenv
931,248
<p>I have a project and I am trying to run nosetests with coverage. I am running in a virtualenv. When I run</p> <pre><code>$ python setup.py nosetests </code></pre> <p>The tests run fine but coverage is not showing that any code is executed (coverage is all 0%).</p> <pre> Name Stmts ...
2
2009-05-31T04:20:41Z
947,889
<p>try... </p> <pre><code>easy_install "coverage==2.85" </code></pre> <p>I was having the same issue and this solved my problem and gave me glorious coverage reports as expected. </p>
2
2009-06-03T23:51:31Z
[ "python", "osx", "code-coverage", "virtualenv", "nosetests" ]
Getting friends within a specified degree of separation
931,323
<p>all. I'm a very, very new programmer. My language of choice at the moment is Python, and I feel like I have a decent feel for it. I'm just now starting to learn about recursion. (By the way, if anyone could recommend a good guide on this, please let me know!) Just so you all know, this question is very elementary...
8
2009-05-31T05:00:20Z
931,350
<p>You can move friendList.append(self) to the line before the if - you need it in both cases. You also don't need to assign the result to friendlist - it's a bug.</p> <p>In your algorithm, you will likely to add the same people twice - if A is a friend of B and B is a friend of A. So, you need to keep a set of friend...
1
2009-05-31T05:10:55Z
[ "python", "recursion" ]
Getting friends within a specified degree of separation
931,323
<p>all. I'm a very, very new programmer. My language of choice at the moment is Python, and I feel like I have a decent feel for it. I'm just now starting to learn about recursion. (By the way, if anyone could recommend a good guide on this, please let me know!) Just so you all know, this question is very elementary...
8
2009-05-31T05:00:20Z
931,354
<pre><code>friendList = friendList.append(self) </code></pre> <p>This sets <code>friendList</code> to <code>None</code>, unconditionally, as that's the invariable return value of any list's <code>append</code> method -- so, fix <em>that</em> weirdness first...!-)</p> <p>Once you've fixed that, you still need to fix t...
12
2009-05-31T05:13:07Z
[ "python", "recursion" ]
Getting friends within a specified degree of separation
931,323
<p>all. I'm a very, very new programmer. My language of choice at the moment is Python, and I feel like I have a decent feel for it. I'm just now starting to learn about recursion. (By the way, if anyone could recommend a good guide on this, please let me know!) Just so you all know, this question is very elementary...
8
2009-05-31T05:00:20Z
931,358
<p>Is your identation correct? The body of the method should be indented relative to it's definition</p>
1
2009-05-31T05:13:54Z
[ "python", "recursion" ]
Getting friends within a specified degree of separation
931,323
<p>all. I'm a very, very new programmer. My language of choice at the moment is Python, and I feel like I have a decent feel for it. I'm just now starting to learn about recursion. (By the way, if anyone could recommend a good guide on this, please let me know!) Just so you all know, this question is very elementary...
8
2009-05-31T05:00:20Z
931,392
<p>There's no <code>return</code> statement in the <code>else</code> clause. So if <code>degree != 0</code>, this method will always return <code>None</code>. You want to append the result of each recursive <code>getFriends</code> call to your <code>friendList</code>, and then return <code>friendList</code>.</p> <p>By...
1
2009-05-31T05:38:54Z
[ "python", "recursion" ]
Getting friends within a specified degree of separation
931,323
<p>all. I'm a very, very new programmer. My language of choice at the moment is Python, and I feel like I have a decent feel for it. I'm just now starting to learn about recursion. (By the way, if anyone could recommend a good guide on this, please let me know!) Just so you all know, this question is very elementary...
8
2009-05-31T05:00:20Z
931,560
<p>(Sorry, I can't comment on Alex's answer... <em>yet</em>)</p> <p>I don't really like the idea that getFriends returns a value that is never used. It works, for sure, but it looks a bit intriguing ;) Also, the first call to getFriends would be self.getFriends(degree, []) which is confusing: when getting a list of fr...
1
2009-05-31T07:30:32Z
[ "python", "recursion" ]
Is there a Python equivalent to the PHP function htmlspecialchars()?
931,423
<p>Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs().</p>
13
2009-05-31T05:58:44Z
931,442
<p>If you are using django 1.0 then your template variables will already be encoded and ready for display. You also use the <code>safe</code> operator <code>{{ var|safe }}</code> if you don't want it globally turned on.</p>
0
2009-05-31T06:11:00Z
[ "php", "python", "html-entities", "htmlspecialchars" ]
Is there a Python equivalent to the PHP function htmlspecialchars()?
931,423
<p>Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs().</p>
13
2009-05-31T05:58:44Z
931,445
<p>Closest thing I know about is <a href="http://docs.python.org/library/cgi.html#cgi.escape">cgi.escape</a>.</p>
12
2009-05-31T06:12:45Z
[ "php", "python", "html-entities", "htmlspecialchars" ]
Is there a Python equivalent to the PHP function htmlspecialchars()?
931,423
<p>Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs().</p>
13
2009-05-31T05:58:44Z
931,474
<p>You probably want <a href="http://docs.python.org/library/xml.sax.utils.html#xml.sax.saxutils.escape" rel="nofollow">xml.sax.saxutils.escape</a>:</p> <pre><code>from xml.sax.saxutils import escape escape(unsafe, {'"':'&amp;quot;'}) # ENT_COMPAT escape(unsafe, {'"':'&amp;quot;', '\'':'&amp;#039;'}) # ENT_QUOTES esca...
2
2009-05-31T06:31:53Z
[ "php", "python", "html-entities", "htmlspecialchars" ]
Is there a Python equivalent to the PHP function htmlspecialchars()?
931,423
<p>Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs().</p>
13
2009-05-31T05:58:44Z
931,641
<p>The <code>html.entities</code> module (<code>htmlentitydefs</code> for python 2.x) contains a dictionary <code>codepoint2name</code> which should do what you need.</p> <pre><code>&gt;&gt;&gt; import html.entities &gt;&gt;&gt; html.entities.codepoint2name[ord("&amp;")] 'amp' &gt;&gt;&gt; html.entities.codepoint2name...
1
2009-05-31T08:27:17Z
[ "php", "python", "html-entities", "htmlspecialchars" ]
Is there a Python equivalent to the PHP function htmlspecialchars()?
931,423
<p>Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs().</p>
13
2009-05-31T05:58:44Z
2,513,072
<pre><code>from django.utils.html import escape print escape('&lt;div class="q"&gt;Q &amp; A&lt;/div&gt;') </code></pre>
6
2010-03-25T04:32:22Z
[ "php", "python", "html-entities", "htmlspecialchars" ]
Is there a Python equivalent to the PHP function htmlspecialchars()?
931,423
<p>Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs().</p>
13
2009-05-31T05:58:44Z
5,488,846
<p>I think the simplest way is just to use replace:</p> <pre><code>text.replace("&amp;", "&amp;amp;").replace('"', "&amp;quot;").replace("&lt;", "&amp;lt;").replace("&gt;", "&amp;gt;") </code></pre> <p>PHP only escapes those four entities with htmlspecialchars. Note that if you have ENT_QUOTES set in PHP, you need t...
1
2011-03-30T15:59:40Z
[ "php", "python", "html-entities", "htmlspecialchars" ]
Is there a Python equivalent to the PHP function htmlspecialchars()?
931,423
<p>Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs().</p>
13
2009-05-31T05:58:44Z
35,729,832
<p>Building on @garlon4 answer, you can define your own <code>htmlspecialchars(s)</code>:</p> <pre><code>def htmlspecialchars(text): return ( text.replace("&amp;", "&amp;amp;"). replace('"', "&amp;quot;"). replace("&lt;", "&amp;lt;"). replace("&gt;", "&amp;gt;") ) </code></pre>
0
2016-03-01T17:48:11Z
[ "php", "python", "html-entities", "htmlspecialchars" ]
Replace __str__ method on list object in Python
931,699
<p>This seems like it should be simple:</p> <p>I want a <code>list</code> like any other <code>list</code>, except it has a different <code>.__str__</code> method.</p> <ol> <li>Trying to set <code>object.__str__ = foo</code> results in a read-only error</li> <li>Trying to subclass <code>list</code> means you need som...
8
2009-05-31T09:06:52Z
931,706
<p>You could extend the list class and override it:</p> <pre><code>class myList(list): def __str__(self): # do something return "something" </code></pre> <p>Edit: removed an incorrect part of the answer which suggested dynamically replacing <code>__str__</code> on the list object, which is not allowed in th...
4
2009-05-31T09:11:36Z
[ "python" ]
Replace __str__ method on list object in Python
931,699
<p>This seems like it should be simple:</p> <p>I want a <code>list</code> like any other <code>list</code>, except it has a different <code>.__str__</code> method.</p> <ol> <li>Trying to set <code>object.__str__ = foo</code> results in a read-only error</li> <li>Trying to subclass <code>list</code> means you need som...
8
2009-05-31T09:06:52Z
931,747
<p>I'm a Java programmer but I think that is what you want (tested with python 2.6): </p> <pre><code>&gt;&gt;&gt; class myList(list): ... def __str__(self): ... return "aaa" ... &gt;&gt;&gt; def myListWrapper(list): ... return myList(list) ... &gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; type(a) &lt;type 'list'&gt;...
2
2009-05-31T09:34:16Z
[ "python" ]
Replace __str__ method on list object in Python
931,699
<p>This seems like it should be simple:</p> <p>I want a <code>list</code> like any other <code>list</code>, except it has a different <code>.__str__</code> method.</p> <ol> <li>Trying to set <code>object.__str__ = foo</code> results in a read-only error</li> <li>Trying to subclass <code>list</code> means you need som...
8
2009-05-31T09:06:52Z
931,757
<p>How about wrapping the list?</p> <pre><code>&gt;&gt;&gt; class StrList(object): def __init__(self, data=None): self._data = data or [] def __str__(self): return "StrList!" def __getattr__(self, attr): if attr == "_data": return self.__dict__[attr] return getattr(self._data, att...
0
2009-05-31T09:41:34Z
[ "python" ]
Replace __str__ method on list object in Python
931,699
<p>This seems like it should be simple:</p> <p>I want a <code>list</code> like any other <code>list</code>, except it has a different <code>.__str__</code> method.</p> <ol> <li>Trying to set <code>object.__str__ = foo</code> results in a read-only error</li> <li>Trying to subclass <code>list</code> means you need som...
8
2009-05-31T09:06:52Z
931,765
<p>Which raises the question: why do you want to override the __str__ method?</p> <p>Wouldnt it be better to create a class to encapsulate your object?</p> <pre><code>class MyContainer(object): def __init__(self, list): self.containedList = list def __str__(self): print('All hail Python') </c...
1
2009-05-31T09:50:52Z
[ "python" ]
Replace __str__ method on list object in Python
931,699
<p>This seems like it should be simple:</p> <p>I want a <code>list</code> like any other <code>list</code>, except it has a different <code>.__str__</code> method.</p> <ol> <li>Trying to set <code>object.__str__ = foo</code> results in a read-only error</li> <li>Trying to subclass <code>list</code> means you need som...
8
2009-05-31T09:06:52Z
931,822
<p>This solution works without a wrapper. And works if you join two lists by add. Any operation that modify the list itself will work as expected. Only functions that return a copy of the list like: sorted, reveresed will return the native python list which is fine. sort and reverse on the other hand operate on the lis...
7
2009-05-31T10:37:55Z
[ "python" ]
Replace __str__ method on list object in Python
931,699
<p>This seems like it should be simple:</p> <p>I want a <code>list</code> like any other <code>list</code>, except it has a different <code>.__str__</code> method.</p> <ol> <li>Trying to set <code>object.__str__ = foo</code> results in a read-only error</li> <li>Trying to subclass <code>list</code> means you need som...
8
2009-05-31T09:06:52Z
931,860
<p>If you would like to override <code>__str__</code> for other containers (e.g., <code>tuple</code>), you can take advantage of multiple inheritance:</p> <pre><code>class PrettyStr(object): def __str__(self): ret = '' if isinstance(self, (list, tuple)): ret = ''.join(str(elem) for ele...
4
2009-05-31T11:12:55Z
[ "python" ]
Replace __str__ method on list object in Python
931,699
<p>This seems like it should be simple:</p> <p>I want a <code>list</code> like any other <code>list</code>, except it has a different <code>.__str__</code> method.</p> <ol> <li>Trying to set <code>object.__str__ = foo</code> results in a read-only error</li> <li>Trying to subclass <code>list</code> means you need som...
8
2009-05-31T09:06:52Z
932,063
<pre><code>class MyList(list): def __str__(self): return "foo" str(MyList([1, 2, 3])) </code></pre> <p>'foo'</p> <pre><code>str(MyList(list([1, 2, 3]))) </code></pre> <p>'foo'</p> <p>My earlier comments as an answer. As you can see MyList accepts any sequence in its constructor.</p>
2
2009-05-31T13:38:16Z
[ "python" ]
py2exe - generated executable freezes when connecting to socket
931,851
<p>Pardon my ignorance as I'm still a beginner in coding.</p> <p>I'm trying to convert a python script I wrote to a Windows executable program using py2exe. However, though I am able to successfully convert the script, the executable doesn't seem to be fully functional.</p> <p>After much debugging, I have isolated th...
1
2009-05-31T11:08:16Z
931,929
<p>Are you able to input the IP address? Reading <a href="http://www.mail-archive.com/pygame-users@seul.org/msg04830.html" rel="nofollow">that thread</a> it seems that py2exe requires a special <em>windows</em> argument to launch a console. Otherwise, raw_input tries to read from the standard input, and hangs/crashes b...
1
2009-05-31T12:00:44Z
[ "python", "py2exe" ]
Inaccurate Logarithm in Python
931,995
<p>I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.</p> <p>I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / ...
7
2009-05-31T12:48:26Z
932,003
<p>This is to be expected with computer arithmetic. It is following particular rules, such as <a href="http://en.wikipedia.org/wiki/IEEE%5F754">IEEE 754</a>, that probably don't match the math you learned in school.</p> <p>If this <em>actually</em> matters, use Python's <a href="http://docs.python.org/library/decimal...
43
2009-05-31T12:55:01Z
[ "python", "math", "floating-point" ]
Inaccurate Logarithm in Python
931,995
<p>I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.</p> <p>I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / ...
7
2009-05-31T12:48:26Z
932,007
<p><em>Always</em> assume that floating point operations will have some error in them and check for equality taking that error into account (either a percentage value like 0.00001% or a fixed value like 0.00000000001). This inaccuracy is a given since not all decimal numbers can be represented in binary with a fixed nu...
17
2009-05-31T12:58:09Z
[ "python", "math", "floating-point" ]
Inaccurate Logarithm in Python
931,995
<p>I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.</p> <p>I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / ...
7
2009-05-31T12:48:26Z
932,018
<p>floating-point operations are never exact. They return a result which has an acceptable relative error, for the language/hardware infrastructure.</p> <p>In general, it's quite wrong to assume that floating-point operations are precise, especially with single-precision. <a href="http://en.wikipedia.org/wiki/Floating...
5
2009-05-31T13:01:06Z
[ "python", "math", "floating-point" ]
Inaccurate Logarithm in Python
931,995
<p>I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.</p> <p>I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / ...
7
2009-05-31T12:48:26Z
932,021
<p>This is normal. I would expect log10 to be more accurate then log(x, y), since it knows exactly what the base of the logarithm is, also there may be some hardware support for calculating base-10 logarithms.</p>
2
2009-05-31T13:04:49Z
[ "python", "math", "floating-point" ]
Inaccurate Logarithm in Python
931,995
<p>I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.</p> <p>I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / ...
7
2009-05-31T12:48:26Z
932,095
<p>You should read "What Every Computer Scientist Should Know About Floating-Point Arithmetic". </p> <p><a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" rel="nofollow">http://docs.sun.com/source/806-3568/ncg_goldberg.html</a></p>
19
2009-05-31T14:08:16Z
[ "python", "math", "floating-point" ]
Inaccurate Logarithm in Python
931,995
<p>I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.</p> <p>I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / ...
7
2009-05-31T12:48:26Z
932,641
<p>IEEE double floating point numbers have <a href="http://www.johndcook.com/blog/2009/04/06/anatomy-of-a-floating-point-number/" rel="nofollow">52 bits of precision</a>. Since 10^15 &lt; 2^52 &lt; 10^16, a double has between 15 and 16 significant figures. The result 31.000000000000004 is correct to 16 figures, so it...
3
2009-05-31T19:09:17Z
[ "python", "math", "floating-point" ]
Inaccurate Logarithm in Python
931,995
<p>I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.</p> <p>I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / ...
7
2009-05-31T12:48:26Z
932,726
<p>The <code>repr</code>esentation (<code>float.__repr__</code>) of a number in python tries to return a string of digits as close to the real value as possible when converted back, given that IEEE-754 arithmetic is precise up to a limit. In any case, if you <code>print</code>ed the result, you wouldn't notice:</p> <p...
1
2009-05-31T19:53:56Z
[ "python", "math", "floating-point" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
932,072
<p>Mine is, basically, a directory called "plugins" which the main app can poll and then use <a href="https://docs.python.org/library/imp.html#imp.load_module">imp.load_module</a> to pick up files, look for a well-known entry point possibly with module-level config params, and go from there. I use file-monitoring stuff...
114
2009-05-31T13:51:05Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
932,078
<p>Have a look at <a href="http://wehart.blogspot.com/2009/01/python-plugin-frameworks.html">at this overview over existing plugin frameworks / libraries</a>, it is a good starting point. I quite like <a href="http://yapsy.sourceforge.net/">yapsy</a>, but it depends on your use-case.</p>
29
2009-05-31T13:54:12Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
932,306
<p>While that question is really interesting, I think it's fairly hard to answer, without more details. What sort of application is this? Does it have a GUI? Is it a command-line tool? A set of scripts? A program with an unique entry point, etc...</p> <p>Given the little information I have, I will answer in a very gen...
21
2009-05-31T15:54:28Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
932,564
<p>I am a retired biologist who dealt with digital micrograqphs and found himself having to write an image processing and analysis package (not technically a library) to run on an SGi machine. I wrote the code in C and used Tcl for the scripting language. The GUI, such as it was, was done using Tk. The commands that...
10
2009-05-31T18:05:31Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
932,771
<p>I enjoyed the nice discussion on different plugin architectures given by Dr Andre Roberge at Pycon 2009. He gives a good overview of different ways of implementing plugins, starting from something really simple.</p> <p>Its available as a <a href="http://blip.tv/pycon-us-videos-2009-2010-2011/plugins-and-monkeypatch...
6
2009-05-31T20:16:38Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
933,245
<p><code>module_example.py</code>:</p> <pre><code>def plugin_main(*args, **kwargs): print args, kwargs </code></pre> <p><code>loader.py</code>:</p> <pre><code>def load_plugin(name): mod = __import__("module_%s" % name) return mod def call_plugin(name, *args, **kwargs): plugin = load_plugin(name) ...
38
2009-06-01T01:11:18Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
2,442,096
<p><a href="http://martyalchin.com/2008/jan/10/simple-plugin-framework/">Marty Allchin's simple plugin framework</a> is the base I use for my own needs. I really recommand to take a look at it, I think it is really a good start if you want something simple and easily hackable. You can find it also <a href="http://www.d...
10
2010-03-14T12:14:17Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
14,579,111
<p><a href="http://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points" rel="nofollow">setuptools has an EntryPoint</a>:</p> <blockquote> <p>Entry points are a simple way for distributions to “advertise” Python objects (such as functions or classes) for use by other distributions. Extensible ...
2
2013-01-29T09:03:27Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
19,555,786
<p>I arrived here looking for a minimal plugin architecture, and found a lot of things that all seemed like overkill to me. So, I've implemented <a href="https://github.com/samwyse/sspp" rel="nofollow">Super Simple Python Plugins</a>. To use it, you create one or more directories and drop a special <code>__init__.py<...
2
2013-10-24T02:23:28Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
19,636,157
<p>When i searching for Python Decorators, found a simple but useful code snippet. It may not fit in your needs but very inspiring.</p> <p><a href="http://scipy-lectures.github.io/advanced/advanced_python/#a-plugin-registration-system" rel="nofollow">Scipy Advanced Python#Plugin Registration System</a></p> <pre><code...
4
2013-10-28T13:31:23Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
26,282,173
<p>As one another approach to plugin system, You may check <a href="https://pypi.python.org/pypi/extend_me" rel="nofollow">Extend Me project</a>.</p> <p>For example, let's define simple class and its extension</p> <pre><code># Define base class for extensions (mount point) class MyCoolClass(Extensible): my_attr_1...
2
2014-10-09T15:21:33Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
32,225,807
<p>Actually <em>setuptools</em> works with a "plugins directory", as the following example taken from the project's documentation: <a href="http://peak.telecommunity.com/DevCenter/PkgResources#locating-plugins" rel="nofollow">http://peak.telecommunity.com/DevCenter/PkgResources#locating-plugins</a></p> <p>Example usag...
2
2015-08-26T11:46:54Z
[ "python", "architecture", "plugins" ]
Building a minimal plugin architecture in Python
932,069
<p>I have an application, written in Python, which is used by a fairly technical audience (scientists). </p> <p>I'm looking for a good way to make the application extensible by the users, i.e. a scripting/plugin architecture. </p> <p>I am looking for something <strong>extremely lightweight</strong>. Most scripts, or ...
129
2009-05-31T13:46:41Z
36,554,110
<p>Expanding on the @edomaur's answer may I suggest taking a look at <a href="https://bitbucket.org/petar/simple_plugins" rel="nofollow">simple_plugins</a> (shameless plug), which is a simple plugin framework inspired by the <a href="http://martyalchin.com/2008/jan/10/simple-plugin-framework/" rel="nofollow">work of Ma...
1
2016-04-11T16:19:47Z
[ "python", "architecture", "plugins" ]
Implementing the Koch Curve?
932,222
<p>I was looking at the wikipedia page for the Koch Snowflake (<a href="http://en.wikipedia.org/wiki/Koch%5Fsnowflake" rel="nofollow">here</a>) and was bothered by the all the examples all being in the logo/turtle style. So i set out to make my own that returned a list or coordinates. </p> <p>My implementation is in p...
3
2009-05-31T15:24:10Z
932,498
<p>I don't see it as particularly ugly and I'd only refactor it incrementally, e.g. as a first step (I've removed the <code>try/except</code> because I don't know what you're trying to ward against... if it needs to get back in it should be a bit more explicit, IMHO):</p> <pre><code>import math angles = [math.radians...
5
2009-05-31T17:25:20Z
[ "python", "fractals" ]
Implementing the Koch Curve?
932,222
<p>I was looking at the wikipedia page for the Koch Snowflake (<a href="http://en.wikipedia.org/wiki/Koch%5Fsnowflake" rel="nofollow">here</a>) and was bothered by the all the examples all being in the logo/turtle style. So i set out to make my own that returned a list or coordinates. </p> <p>My implementation is in p...
3
2009-05-31T15:24:10Z
1,542,485
<p>Mathematica is superior when it comes to math stuff:</p> <pre><code>points = {{0.0, 1.0}}; koch[pts_] := Join[ pts/3, (RotationMatrix[60 Degree].#/3 + {1/3, 0}) &amp; /@ pts, (RotationMatrix[-60 Degree].#/3 + {1/2, 1/Sqrt[12]}) &amp; /@ pts, (#/3 + {2/3, 0}) &amp; /@ pts ]; Graphics[Line[Nest[koch, ...
0
2009-10-09T08:07:10Z
[ "python", "fractals" ]
Implementing the Koch Curve?
932,222
<p>I was looking at the wikipedia page for the Koch Snowflake (<a href="http://en.wikipedia.org/wiki/Koch%5Fsnowflake" rel="nofollow">here</a>) and was bothered by the all the examples all being in the logo/turtle style. So i set out to make my own that returned a list or coordinates. </p> <p>My implementation is in p...
3
2009-05-31T15:24:10Z
7,420,270
<p>I liked your question so much that I posted an answer to it as a new question, so that other people could improve it:</p> <p><a href="http://stackoverflow.com/questions/7420248">Koch Curve algorithm in Python without using Turtle/Logo logic - suggestion to improve this code?</a></p> <p>I used no Logo/Turtle stuff,...
1
2011-09-14T17:13:00Z
[ "python", "fractals" ]
Search functionality for Django
932,255
<p>I'm developing a web app using Django, and I'll need to add search functionality soon. Search will be implemented for two models, one being an extension of the auth user class and another one with the fields <code>name</code>, <code>tags</code>, and <code>description</code>. So I guess nothing too scary here in cont...
2
2009-05-31T15:33:44Z
932,360
<p>If that field <code>tags</code> means what I think it means, i.e. you plan to store a string which concatenates multiple tags for an item, then you might need full-text search on it... but it's a bad design; rather, you should have a many-many relationship between items and a tags table (in another table, ItemTag or...
5
2009-05-31T16:15:01Z
[ "python", "database", "django", "search", "full-text-search" ]
Search functionality for Django
932,255
<p>I'm developing a web app using Django, and I'll need to add search functionality soon. Search will be implemented for two models, one being an extension of the auth user class and another one with the fields <code>name</code>, <code>tags</code>, and <code>description</code>. So I guess nothing too scary here in cont...
2
2009-05-31T15:33:44Z
932,402
<p>Whether you need an external library depends on your needs. How much traffic are we talking about? The external libraries are generally better when it comes to performance, but as always there are advantages and disadvantages. I am using Sphinx with django-sphinx plugin, and I would recommend it if you will be do...
0
2009-05-31T16:35:01Z
[ "python", "database", "django", "search", "full-text-search" ]
Search functionality for Django
932,255
<p>I'm developing a web app using Django, and I'll need to add search functionality soon. Search will be implemented for two models, one being an extension of the auth user class and another one with the fields <code>name</code>, <code>tags</code>, and <code>description</code>. So I guess nothing too scary here in cont...
2
2009-05-31T15:33:44Z
932,427
<p><a href="http://haystacksearch.org/" rel="nofollow">Haystack</a> looks promising. And it supports <a href="http://whoosh.ca/" rel="nofollow">Whoosh</a> on the back end.</p>
0
2009-05-31T16:45:25Z
[ "python", "database", "django", "search", "full-text-search" ]