title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
python, regex split and special character
647,655
<p>How can I split correctly a string containing a sentence with special chars using whitespaces as separator ? Using regex split method I cannot obtain the desired result.</p> <p>Example code:</p> <pre><code># -*- coding: utf-8 -*- import re s="La felicità è tutto" # "The happiness is everything" in italian l=re...
11
2009-03-15T11:24:44Z
647,674
<p>Try defining an encoding for the regular expression:</p> <pre><code>l=re.compile("\W", re.UNICODE).split(s) </code></pre>
4
2009-03-15T11:36:49Z
[ "python", "regex", "unicode", "split" ]
python, regex split and special character
647,655
<p>How can I split correctly a string containing a sentence with special chars using whitespaces as separator ? Using regex split method I cannot obtain the desired result.</p> <p>Example code:</p> <pre><code># -*- coding: utf-8 -*- import re s="La felicità è tutto" # "The happiness is everything" in italian l=re...
11
2009-03-15T11:24:44Z
647,775
<p>I think it's overkill to use a regexp in this case. If the only thing you want to do is split the string on whitespace characters I recommend using the <code>split</code> method on the string</p> <pre><code>s = 'La felicità è tutto' words = s.split() </code></pre>
3
2009-03-15T12:59:43Z
[ "python", "regex", "unicode", "split" ]
python, regex split and special character
647,655
<p>How can I split correctly a string containing a sentence with special chars using whitespaces as separator ? Using regex split method I cannot obtain the desired result.</p> <p>Example code:</p> <pre><code># -*- coding: utf-8 -*- import re s="La felicità è tutto" # "The happiness is everything" in italian l=re...
11
2009-03-15T11:24:44Z
647,879
<p>Well, after some further tests on Andrew Hare answer I've seen that character as ()[]- and so on are no more considered as separator while I want to split a sentence (maintaining all the separator) in words composed with ensemble of alphanumerical values set eventually expanded with accented chars (that is, everythi...
0
2009-03-15T14:22:00Z
[ "python", "regex", "unicode", "split" ]
python, regex split and special character
647,655
<p>How can I split correctly a string containing a sentence with special chars using whitespaces as separator ? Using regex split method I cannot obtain the desired result.</p> <p>Example code:</p> <pre><code># -*- coding: utf-8 -*- import re s="La felicità è tutto" # "The happiness is everything" in italian l=re...
11
2009-03-15T11:24:44Z
3,234,376
<p>using a unicode regular expression will work, provided you give it a unicode string to start with (which you haven't in the provided example). Try this:</p> <pre><code>s=u"La felicità è tutto" # "The happiness is everything" in italian l=re.compile("(\W)",re.UNICODE).split(s) print " s&gt; "+s print " wordlist&g...
3
2010-07-13T05:17:58Z
[ "python", "regex", "unicode", "split" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
647,774
<p>The reason for why <code>r'\'</code> is syntactical incorrect is that although the string expression is raw the used quotes (single or double) always have to be escape since they would mark the end of the quote otherwise. So if you want to express a single quote inside single quoted string, there is no other way tha...
2
2009-03-15T12:59:20Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
647,787
<p>The reason is explained in the part of that section which I highlighted in bold:</p> <blockquote> <p><strong>String quotes can be escaped with a backslash,</strong> but the backslash remains in the string; for example, <code>r"\""</code> is a valid string literal consisting of two characters: a backslash ...
61
2009-03-15T13:05:31Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
647,797
<p>That's the way it is! I see it as one of those small defects in python!</p> <p>I don't think there's a good reason for it, but it's definitely not parsing; it's really easy to parse raw strings with \ as a last character.</p> <p>The catch is, if you allow \ to be the last character in a raw string then you won't b...
13
2009-03-15T13:17:10Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
647,877
<p>Another user who has since deleted their answer (not sure if they'd like to be credited) suggested that the Python language designers may be able to simplify the parser design by using the same parsing rules and expanding escaped characters to raw form as an afterthought (if the literal was marked as raw).</p> <p>I...
1
2009-03-15T14:20:01Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
648,135
<p>Since \" is allowed inside the raw string. Then it can't be used to identify the end of the string literal. </p> <p>Why not stop parsing the string literal when you encounter the first "?</p> <p>If that was the case, then \" wouldn't be allowed inside the string literal. But it is.</p>
7
2009-03-15T16:59:09Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
648,168
<p>Comming from C it pretty clear to me that a single \ works as escape character allowing you to put special characters such as newlines, tabs and quotes into strings.</p> <p>That does indeed disallow \ as last character since it will escape the " and make the parser choke. But as pointed out earlier \ is legal.</p>
0
2009-03-15T17:14:37Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
648,671
<p>some tips :</p> <p>1) if you need to manipulate backslash for path then standard python module os.path is your friend. for example : </p> <blockquote> <p>os.path.normpath('c:/folder1/')</p> </blockquote> <p>2) if you want to build strings with backslash in it BUT without backslash at the END of your string then...
-1
2009-03-15T22:22:05Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
5,830,053
<p>In order for you to end a raw string with a slash I suggest you can use this trick:</p> <pre><code>&gt;&gt;&gt; print r"c:\test"'\\' test\ </code></pre>
8
2011-04-29T08:57:03Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
7,986,539
<p>Another trick is to use chr(92) as it evaluates to "\". </p> <p>I recently had to clean a string of backslashes and the following did the trick:</p> <pre><code>CleanString = DirtyString.replace(chr(92),'') </code></pre> <p>I realize that this does not take care of the "why" but the thread attracts many people loo...
11
2011-11-02T19:54:40Z
[ "python", "string", "literals" ]
Why can't Python's raw string literals end with a single backslash?
647,769
<p>Technically, any odd number of backslashes, as described in <a href="http://docs.python.org/reference/lexical%5Fanalysis.html#string-literals">the docs</a>.</p> <pre><code>&gt;&gt;&gt; r'\' File "&lt;stdin&gt;", line 1 r'\' ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;...
85
2009-03-15T12:54:53Z
19,654,184
<p>The whole misconception about python's raw strings is that most of people think that backslash (within a raw string) is just a regular character as all others. It is NOT. The key to understand is this python's tutorial sequence:</p> <blockquote> <p>When an '<strong>r</strong>' or '<strong>R</strong>' prefix is pr...
14
2013-10-29T09:24:45Z
[ "python", "string", "literals" ]
Trapping MySQL Warnings In Python
647,805
<p>I would like to catch and log MySQL warnings in Python. For example, MySQL issues a warning to standard error if you submit <code>'DROP DATABASE IF EXISTS database_of_armaments'</code> when no such database exists. I would like to catch this and log it, but even in the try/else syntax the warning message still appe...
13
2009-03-15T13:25:52Z
647,811
<p>Follow these steps.</p> <ol> <li><p>Run it with <code>except Exception, e: print repr(e)</code>.</p></li> <li><p>See what exception you get.</p></li> <li><p>Change the <code>Exception</code> to the exception you actually got.</p></li> </ol> <p>Also, remember that the exception, e, is an object. You can print <cod...
14
2009-03-15T13:32:39Z
[ "python", "mysql" ]
Trapping MySQL Warnings In Python
647,805
<p>I would like to catch and log MySQL warnings in Python. For example, MySQL issues a warning to standard error if you submit <code>'DROP DATABASE IF EXISTS database_of_armaments'</code> when no such database exists. I would like to catch this and log it, but even in the try/else syntax the warning message still appe...
13
2009-03-15T13:25:52Z
647,820
<p>Have you tried something like this?</p> <pre><code>try: cursor.execute(some_statement) except MySQLdb.IntegrityError, e: # handle a specific error condition except MySQLdb.Error, e: # handle a generic error condition except MySQLdb.Warning, e: # handle warnings, if the cursor you're using raises th...
4
2009-03-15T13:39:11Z
[ "python", "mysql" ]
Trapping MySQL Warnings In Python
647,805
<p>I would like to catch and log MySQL warnings in Python. For example, MySQL issues a warning to standard error if you submit <code>'DROP DATABASE IF EXISTS database_of_armaments'</code> when no such database exists. I would like to catch this and log it, but even in the try/else syntax the warning message still appe...
13
2009-03-15T13:25:52Z
647,834
<p>I think the exception you want to catch is a MySQLdb.ProgrammingError, and to get information about it, just add a variable to store the error data (a tuple) in after that i.e:</p> <pre><code>try: cursor.execute('DROP DATABASE IF EXISTS database_of_armaments') except MySQLdb.ProgrammingError, e: print 'Ther...
2
2009-03-15T13:48:53Z
[ "python", "mysql" ]
Trapping MySQL Warnings In Python
647,805
<p>I would like to catch and log MySQL warnings in Python. For example, MySQL issues a warning to standard error if you submit <code>'DROP DATABASE IF EXISTS database_of_armaments'</code> when no such database exists. I would like to catch this and log it, but even in the try/else syntax the warning message still appe...
13
2009-03-15T13:25:52Z
5,681,593
<p>first you should turn on warnings to treat as exceptions, and only then you can catch them. see standard "warnings" module for details.</p>
1
2011-04-15T19:46:05Z
[ "python", "mysql" ]
Trapping MySQL Warnings In Python
647,805
<p>I would like to catch and log MySQL warnings in Python. For example, MySQL issues a warning to standard error if you submit <code>'DROP DATABASE IF EXISTS database_of_armaments'</code> when no such database exists. I would like to catch this and log it, but even in the try/else syntax the warning message still appe...
13
2009-03-15T13:25:52Z
33,024,014
<p>Plain and simple</p> <pre><code>def log_warnings(curs): for msg in curs.messages: if msg[0] == MySQLdb.Warning: logging.warn(msg[1]) cursor.execute('DROP DATABASE IF EXISTS database_of_armaments') log_warnings(cursor) </code></pre> <p>msg[1] example :- <code>(u'Warning', 1366L, u"Incorrect...
0
2015-10-08T19:07:42Z
[ "python", "mysql" ]
Trapping MySQL Warnings In Python
647,805
<p>I would like to catch and log MySQL warnings in Python. For example, MySQL issues a warning to standard error if you submit <code>'DROP DATABASE IF EXISTS database_of_armaments'</code> when no such database exists. I would like to catch this and log it, but even in the try/else syntax the warning message still appe...
13
2009-03-15T13:25:52Z
38,461,265
<p>I managed to trap the mysql warning like this:</p> <pre><code>import _mysql_exceptions try: foo.bar() except _mysql_exceptions.Warning, e: pass </code></pre>
1
2016-07-19T14:23:30Z
[ "python", "mysql" ]
socket trouble in python
647,813
<p>I have a server that's written in C, and I want to write a client in python. The python client will send a string "send some_file" when it wants to send a file, followed by the file's contents, and the string "end some_file". Here is my client code :</p> <pre><code> file = sys.argv[1] host = sys.argv[2] port = int(...
4
2009-03-15T13:34:19Z
647,817
<p>With socket programming, even if you do 2 independent sends, it doesn't mean that the other side will receive them as 2 independent recvs.</p> <p>One simple solution that works for both strings and binary data is to: First send the number of bytes in the message, then send the message.</p> <p>Here is what you shou...
9
2009-03-15T13:36:36Z
[ "python", "sockets" ]
socket trouble in python
647,813
<p>I have a server that's written in C, and I want to write a client in python. The python client will send a string "send some_file" when it wants to send a file, followed by the file's contents, and the string "end some_file". Here is my client code :</p> <pre><code> file = sys.argv[1] host = sys.argv[2] port = int(...
4
2009-03-15T13:34:19Z
647,821
<p>TCP/IP data is buffered, more-or-less randomly.</p> <p>It's just a "stream" of bytes. If you want, you can read it as though it's delimited by '\n' characters. However, it is not broken into meaningful chunks; nor can it be. It must be a continuous stream of bytes.</p> <p>How are you reading it in C? Are you r...
-3
2009-03-15T13:39:25Z
[ "python", "sockets" ]
socket trouble in python
647,813
<p>I have a server that's written in C, and I want to write a client in python. The python client will send a string "send some_file" when it wants to send a file, followed by the file's contents, and the string "end some_file". Here is my client code :</p> <pre><code> file = sys.argv[1] host = sys.argv[2] port = int(...
4
2009-03-15T13:34:19Z
647,835
<p>Possibly using</p> <pre><code>sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) </code></pre> <p>will help send each packet as you want it as this disables <a href="http://en.wikipedia.org/wiki/Nagle%27s%5Falgorithm" rel="nofollow">Nagle's algorithm</a>, as most TCP stacks use this to join several packets...
0
2009-03-15T13:50:01Z
[ "python", "sockets" ]
socket trouble in python
647,813
<p>I have a server that's written in C, and I want to write a client in python. The python client will send a string "send some_file" when it wants to send a file, followed by the file's contents, and the string "end some_file". Here is my client code :</p> <pre><code> file = sys.argv[1] host = sys.argv[2] port = int(...
4
2009-03-15T13:34:19Z
647,865
<p>There are two much simpler ways I can think of in which you can solve this. Both involve some changes in the behaviors of both the client and the server.</p> <p>The first is to use padding. Let's say you're sending a file. What you would do is read the file, encode this into a simpler format like Base64, then send ...
1
2009-03-15T14:12:31Z
[ "python", "sockets" ]
File handling in Django when posting image from service call
647,888
<p>I am using <a href="http://pyamf.org/">PyAMF</a> to transfer a dynamically generated large image from Flex to Django. On the Django side i receive the encodedb64 data as a parameter:</p> <p>My Item model as an imagefield. What i have trouble to do is saving the data as the File Django Field.</p> <pre><code>def sav...
7
2009-03-15T14:23:25Z
648,050
<p>In <code>django.core.files.base</code> you can find the class <code>ContentFile</code>. That class extends the basic Django <code>File</code> class, so you do not need StringIO (which ContentFile though uses internally). The modified save method looks like this:</p> <pre><code>from django.core.files.base import Con...
9
2009-03-15T16:08:11Z
[ "python", "django", "flex", "pyamf" ]
Python - test that succeeds when exception is not raised
647,900
<p>I know about <code>unittest</code> Python module.</p> <p>I know about <code>assertRaises()</code> method of <code>TestCase</code> class.</p> <p>I would like to write a test that succeeds when an exception is <strong>not</strong> raised.</p> <p>Any hints please?</p>
11
2009-03-15T14:33:34Z
647,919
<pre><code>def runTest(self): try: doStuff() except: self.fail("Encountered an unexpected exception.") </code></pre> <p>UPDATE: As liw.fi mentions, the default result is a success, so the example above is something of an antipattern. You should probably only use it if you want to do something s...
18
2009-03-15T14:48:33Z
[ "python", "unit-testing", "exception", "exception-handling" ]
Python - test that succeeds when exception is not raised
647,900
<p>I know about <code>unittest</code> Python module.</p> <p>I know about <code>assertRaises()</code> method of <code>TestCase</code> class.</p> <p>I would like to write a test that succeeds when an exception is <strong>not</strong> raised.</p> <p>Any hints please?</p>
11
2009-03-15T14:33:34Z
647,949
<p>The test runner will catch all exceptions you didn't assert would be raised. Thus:</p> <pre><code>doStuff() self.assert_(True) </code></pre> <p>This should work fine. You can leave out the self.assert_ call, since it doesn't really do anything. I like to put it there to document that I didn't forget an assertion.<...
12
2009-03-15T15:03:50Z
[ "python", "unit-testing", "exception", "exception-handling" ]
Python - test that succeeds when exception is not raised
647,900
<p>I know about <code>unittest</code> Python module.</p> <p>I know about <code>assertRaises()</code> method of <code>TestCase</code> class.</p> <p>I would like to write a test that succeeds when an exception is <strong>not</strong> raised.</p> <p>Any hints please?</p>
11
2009-03-15T14:33:34Z
4,711,722
<p>I use this pattern for the kind of assertion you've asked:</p> <pre><code>with self.assertRaises(Exception): try: doStuff() except: pass else: raise Exception </code></pre> <p>It will fail exactly when exception is raised by doStuff().</p>
4
2011-01-17T09:39:07Z
[ "python", "unit-testing", "exception", "exception-handling" ]
wxPython auinotebook.GetSelection() return index to the first page
647,906
<p>Why do 'GetSelection()' return the index to the first page and not the last created in 'init' and 'new_panel'? It do return correct index in the 'click' method.</p> <p>The output should be 0 0 1 1 2 2, but mine is 0 0 0 0 0 0.</p> <p>Running latest version of python and wxpython in ArchLinux.</p> <p>Ørjan Petter...
1
2009-03-15T14:37:49Z
648,520
<p>I ran your example and got the correct output:</p> <pre><code>0 0 1 1 2 2 </code></pre> <p>I'm using the latest windows release of wxPython</p>
0
2009-03-15T20:50:15Z
[ "python", "wxpython", "wxwidgets" ]
wxPython auinotebook.GetSelection() return index to the first page
647,906
<p>Why do 'GetSelection()' return the index to the first page and not the last created in 'init' and 'new_panel'? It do return correct index in the 'click' method.</p> <p>The output should be 0 0 1 1 2 2, but mine is 0 0 0 0 0 0.</p> <p>Running latest version of python and wxpython in ArchLinux.</p> <p>Ørjan Petter...
1
2009-03-15T14:37:49Z
649,556
<p>The solution was pretty simple. The problem seemed to be that creating the new page didn't generate a page change event. The solution is:</p> <pre><code>self.nb.AddPage(pnl, nm, select=True) </code></pre> <p>Adding 'select=True' will trigger a page change event. So problem solved.</p> <p>Another solution is to ad...
1
2009-03-16T07:27:45Z
[ "python", "wxpython", "wxwidgets" ]
How to make a list comprehension with the group() method in python?
648,276
<p>I'm trying to write a little script to clean my directories. In fact I have:</p> <pre><code>pattern = re.compile(format[i]) ... current_f.append(pattern.search(str(ls))) </code></pre> <p>and I want to use a list comprehension but when I try:</p> <pre><code>In [25]: [i for i in current_f.group(0)] </code></pre> <...
3
2009-03-15T18:21:24Z
648,287
<p>Are you trying to do this?:</p> <pre><code>[f.group(0) for f in current_f] </code></pre>
7
2009-03-15T18:27:53Z
[ "python", "list", "group", "list-comprehension" ]
Clashing guidelines
648,299
<p>While coding in Python it's better to code by following the guidelines of PEP8.</p> <p>And while coding for Symbian it's better to follow its coding standards.</p> <p>But when I code for PyS60 which guidelines should I follow? Till now I have been following PEP8, but <a href="http://wiki.forum.nokia.com/index.php/...
1
2009-03-15T18:35:19Z
648,444
<p>Use the style of the API(s) you're interfacing the most. That's a simple rule that works in most places (where you can see the code, i.e. Java/C# is a bit hard(er).. :)</p>
1
2009-03-15T20:02:29Z
[ "python", "symbian", "coding-style", "pys60", "pep8" ]
Clashing guidelines
648,299
<p>While coding in Python it's better to code by following the guidelines of PEP8.</p> <p>And while coding for Symbian it's better to follow its coding standards.</p> <p>But when I code for PyS60 which guidelines should I follow? Till now I have been following PEP8, but <a href="http://wiki.forum.nokia.com/index.php/...
1
2009-03-15T18:35:19Z
648,598
<p>"Do I need to rework my code?"</p> <p>Does it add value to rework you code?</p> <p>How many folks will help you develop code who </p> <p>A) don't know PEP 8</p> <p>B) only know PyS60 coding standards because that's the only code they've ever seen.</p> <p>and</p> <p>C) cannot be taught anything different than t...
2
2009-03-15T21:43:10Z
[ "python", "symbian", "coding-style", "pys60", "pep8" ]
Clashing guidelines
648,299
<p>While coding in Python it's better to code by following the guidelines of PEP8.</p> <p>And while coding for Symbian it's better to follow its coding standards.</p> <p>But when I code for PyS60 which guidelines should I follow? Till now I have been following PEP8, but <a href="http://wiki.forum.nokia.com/index.php/...
1
2009-03-15T18:35:19Z
648,948
<p>I don't see anything in your code sample that is obviously bogus. It's not the style I'd use, but neither is it hard to read, and it's not so far from PEP8 that I'd call it “the opposite”.</p> <p>PEP8 shouldn't be seen as hard-and-fast law to which all code must conform, character by rigid character. It is a ba...
2
2009-03-16T01:20:42Z
[ "python", "symbian", "coding-style", "pys60", "pep8" ]
Clashing guidelines
648,299
<p>While coding in Python it's better to code by following the guidelines of PEP8.</p> <p>And while coding for Symbian it's better to follow its coding standards.</p> <p>But when I code for PyS60 which guidelines should I follow? Till now I have been following PEP8, but <a href="http://wiki.forum.nokia.com/index.php/...
1
2009-03-15T18:35:19Z
677,935
<p>i'd say use PEP8, but as mentioned above, don't get too hung up on it. when coding IN symbian c++ you should use symbian coding standards, but not necessarily if your program is merely running on the platform. don't get confused between symbian the OS and symbian c++ the (psuedo) language.</p>
0
2009-03-24T15:31:13Z
[ "python", "symbian", "coding-style", "pys60", "pep8" ]
Clashing guidelines
648,299
<p>While coding in Python it's better to code by following the guidelines of PEP8.</p> <p>And while coding for Symbian it's better to follow its coding standards.</p> <p>But when I code for PyS60 which guidelines should I follow? Till now I have been following PEP8, but <a href="http://wiki.forum.nokia.com/index.php/...
1
2009-03-15T18:35:19Z
915,208
<p>Your example code is just that person's personal style. It's NOT following official PyS60 coding convension, there is no such a thing! Write whatever style gives you the best results.</p> <p>Having said that I would recommend using PEP8, but only if you plan to use pylint to give you some additional confidence in y...
0
2009-05-27T11:25:57Z
[ "python", "symbian", "coding-style", "pys60", "pep8" ]
A trivial Python SWIG error question
648,482
<p>I am trying to get Python running with swig to do C/C++. I am running the tutorial <a href="http://www.swig.org/tutorial.html">here</a>, 'building a python module'. When I do the call</p> <pre><code>gcc -c example.c example_wrap.c -I /my_correct_path/python2.5 </code></pre> <p>I get an error:</p> <pre><code>my_...
5
2009-03-15T20:27:49Z
2,909,020
<p>I found this thread looking for an answer for the same "LONGBIT" error while installing python readline for 32bit python on 64bit centos. The link doesn't have the direct answer, so I had to google further for the answer (which might be straight-forward for seasoned linux users/devs). For future reference, the solut...
7
2010-05-25T22:36:08Z
[ "python", "swig" ]
A trivial Python SWIG error question
648,482
<p>I am trying to get Python running with swig to do C/C++. I am running the tutorial <a href="http://www.swig.org/tutorial.html">here</a>, 'building a python module'. When I do the call</p> <pre><code>gcc -c example.c example_wrap.c -I /my_correct_path/python2.5 </code></pre> <p>I get an error:</p> <pre><code>my_...
5
2009-03-15T20:27:49Z
17,958,342
<p>I had the same error when trying to install a Python package, but fixed it.<br> The "LONG_BIT" error was:</p> <pre><code>$ easy_install astropy /my_path/epd/epd-7.3-2-rh5-x86/include/python2.7/pyport.h:849:2: error: #error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)." error: Setup sc...
2
2013-07-30T22:09:36Z
[ "python", "swig" ]
A trivial Python SWIG error question
648,482
<p>I am trying to get Python running with swig to do C/C++. I am running the tutorial <a href="http://www.swig.org/tutorial.html">here</a>, 'building a python module'. When I do the call</p> <pre><code>gcc -c example.c example_wrap.c -I /my_correct_path/python2.5 </code></pre> <p>I get an error:</p> <pre><code>my_...
5
2009-03-15T20:27:49Z
19,668,382
<p>I actually found this thread twice, a couple years apart, both while trying to install <code>libxml2</code> from source. The library's <code>configure</code> script actually has a <code>--without-python</code> option, which I used instead of trying to fix the error.</p>
0
2013-10-29T20:25:24Z
[ "python", "swig" ]
how can i figure if a vim buffer is listed or unlisted from vim's python api?
648,638
<p>for a tool i need to figure all vim buffers that are still listed (there are listed and unlisted buffers)</p> <p>unfortunately <code>vim.buffers</code> contains all buffers and there doesnt seem to be an attribute to figure if a buffer is listed or unlisted</p> <p>the vim command of what i want to do is</p> <pre>...
2
2009-03-15T22:03:57Z
648,645
<p>Here is how you can manage this using just Vim language.</p> <pre><code>function s:buffers_list() let result = [] for buffer_number in range(1, bufnr('$')) if !buflisted(buffer_number) continue endif call add(result, buffer_number) endfor return result endfunct...
5
2009-03-15T22:09:08Z
[ "python", "vim" ]
how can i figure if a vim buffer is listed or unlisted from vim's python api?
648,638
<p>for a tool i need to figure all vim buffers that are still listed (there are listed and unlisted buffers)</p> <p>unfortunately <code>vim.buffers</code> contains all buffers and there doesnt seem to be an attribute to figure if a buffer is listed or unlisted</p> <p>the vim command of what i want to do is</p> <pre>...
2
2009-03-15T22:03:57Z
1,638,543
<p>Using Vim's python api:</p> <pre><code>listedBufs = [] for b in vim.buffers: listed = vim.eval('buflisted(bufnr("%s"))' % b.name) if int(listed) &gt; 0: listedBufs.append(b) </code></pre> <p>or if you don't mind sacrificing some readability:</p> <pre><code>listedBufs = [b for b in vim.buffers ...
3
2009-10-28T16:48:55Z
[ "python", "vim" ]
Use case for nested/multiple list comprehensions or generator expressions. When is it more elegant?
648,675
<p>I see this kind of thing sometimes:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>Now this really bends my brain, and I would rather it wasn't presented in this way.</p> <p>Are there any use-cases, or examples of having used these nested expressions where it was more elegant an...
9
2009-03-15T22:22:52Z
648,721
<p>Since they are generator expressions, you can bind each to it's own name to make it more readable without any change in performance. Changing it to a nested loop would likely be detrimental to performance.</p> <pre><code>irange = (i for i in xrange(10)) jrange = (j for j in irange) krange = (k for k in jrange) </co...
3
2009-03-15T22:40:38Z
[ "python", "list-comprehension", "generator-expression" ]
Use case for nested/multiple list comprehensions or generator expressions. When is it more elegant?
648,675
<p>I see this kind of thing sometimes:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>Now this really bends my brain, and I would rather it wasn't presented in this way.</p> <p>Are there any use-cases, or examples of having used these nested expressions where it was more elegant an...
9
2009-03-15T22:22:52Z
648,723
<p>In the case of your example, I would probably write it as:</p> <pre><code>foos = (i for i in xrange(10)) bars = (j for j in foos) bazs = (k for k in bars) </code></pre> <p>Given more descriptive names, I think this would probably be quite clear, and I can't imagine there being any measurable performance difference...
5
2009-03-15T22:41:06Z
[ "python", "list-comprehension", "generator-expression" ]
Use case for nested/multiple list comprehensions or generator expressions. When is it more elegant?
648,675
<p>I see this kind of thing sometimes:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>Now this really bends my brain, and I would rather it wasn't presented in this way.</p> <p>Are there any use-cases, or examples of having used these nested expressions where it was more elegant an...
9
2009-03-15T22:22:52Z
648,724
<p>If you're worried about too much complexity on one line, you could split it:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>I've always found line continuations to look a little weird in Python, but this does make it easier to see what each one is looping over. Sin...
11
2009-03-15T22:41:50Z
[ "python", "list-comprehension", "generator-expression" ]
Use case for nested/multiple list comprehensions or generator expressions. When is it more elegant?
648,675
<p>I see this kind of thing sometimes:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>Now this really bends my brain, and I would rather it wasn't presented in this way.</p> <p>Are there any use-cases, or examples of having used these nested expressions where it was more elegant an...
9
2009-03-15T22:22:52Z
648,886
<p>Check <a href="http://www.python.org/dev/peps/pep-0202/">PEP 202</a> which was where list comprehensions syntax was introduced to the language.</p> <p>For understanding your example, there is a simple rule from Guido himself:</p> <ul> <li>The form [... for x... for y...] nests, with the last index varying fastes...
16
2009-03-16T00:39:51Z
[ "python", "list-comprehension", "generator-expression" ]
Use case for nested/multiple list comprehensions or generator expressions. When is it more elegant?
648,675
<p>I see this kind of thing sometimes:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>Now this really bends my brain, and I would rather it wasn't presented in this way.</p> <p>Are there any use-cases, or examples of having used these nested expressions where it was more elegant an...
9
2009-03-15T22:22:52Z
650,254
<p>The expression:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>is equivalent to:</p> <pre><code>(i for i in xrange(10)) </code></pre> <p>that is almost the same:</p> <pre><code>xrange(10) </code></pre> <p>The last variant is more elegant than the first one.</p>
0
2009-03-16T12:38:54Z
[ "python", "list-comprehension", "generator-expression" ]
Use case for nested/multiple list comprehensions or generator expressions. When is it more elegant?
648,675
<p>I see this kind of thing sometimes:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>Now this really bends my brain, and I would rather it wasn't presented in this way.</p> <p>Are there any use-cases, or examples of having used these nested expressions where it was more elegant an...
9
2009-03-15T22:22:52Z
22,677,755
<p>I find it can be useful and elegant in situations like these where you have code like this:</p> <pre><code>output = [] for item in list: if item &gt;= 1: new = item += 1 output.append(new) </code></pre> <p>You can make it a one-liner like this:</p> <pre><code>output = [item += 1 for item in li...
0
2014-03-27T03:34:58Z
[ "python", "list-comprehension", "generator-expression" ]
Use case for nested/multiple list comprehensions or generator expressions. When is it more elegant?
648,675
<p>I see this kind of thing sometimes:</p> <pre><code>(k for k in (j for j in (i for i in xrange(10)))) </code></pre> <p>Now this really bends my brain, and I would rather it wasn't presented in this way.</p> <p>Are there any use-cases, or examples of having used these nested expressions where it was more elegant an...
9
2009-03-15T22:22:52Z
34,982,418
<p>Caveat: elegance is partly a matter of taste.</p> <p>List comprehensions are never more <em>clear</em> than the corresponding expanded for loop. For loops are also more <em>powerful</em> than list comprehensions. So why use them at all?</p> <p>What list comprehensions are is <strong>concise</strong> -- they allow ...
1
2016-01-24T22:38:46Z
[ "python", "list-comprehension", "generator-expression" ]
Print out list of function parameters in Python
648,679
<p>Is there a way to print out a function's parameter list? For example:</p> <pre><code>def func(a, b, c): pass print_func_parametes(func) </code></pre> <p>Which will produce something like:</p> <pre><code>["a", "b", "c"] </code></pre>
4
2009-03-15T22:24:37Z
648,689
<p>Use the inspect module.</p> <pre><code>&gt;&gt;&gt; import inspect &gt;&gt;&gt; inspect.getargspec(func) (['a', 'b', 'c'], None, None, None) </code></pre> <p>The first part of returned tuple is what you're looking for.</p>
17
2009-03-15T22:27:58Z
[ "python" ]
Print out list of function parameters in Python
648,679
<p>Is there a way to print out a function's parameter list? For example:</p> <pre><code>def func(a, b, c): pass print_func_parametes(func) </code></pre> <p>Which will produce something like:</p> <pre><code>["a", "b", "c"] </code></pre>
4
2009-03-15T22:24:37Z
648,702
<p>Read the source. Seriously. Python programs and libraries are provided as source. You can read the source.</p>
6
2009-03-15T22:31:48Z
[ "python" ]
Print out list of function parameters in Python
648,679
<p>Is there a way to print out a function's parameter list? For example:</p> <pre><code>def func(a, b, c): pass print_func_parametes(func) </code></pre> <p>Which will produce something like:</p> <pre><code>["a", "b", "c"] </code></pre>
4
2009-03-15T22:24:37Z
648,922
<p>You might also try the built-in <code>help()</code> function, which will provide you not only with a list of the named parameters, but also a description of <code>func()</code> if you provided a docstring:</p> <pre><code>&gt;&gt;&gt; def func(a, b, c): ... """do x to a,b,c and return the result""" ... pass ...
2
2009-03-16T01:03:58Z
[ "python" ]
Why are Python objects of different types ordered by type names?
649,191
<p>From Python docs: <a href="http://docs.python.org/library/stdtypes.html#comparisons" rel="nofollow">http://docs.python.org/library/stdtypes.html#comparisons</a></p> <blockquote> <p>Implementation note: Objects of different types except numbers are ordered by their type names; objects of the same types that don’...
2
2009-03-16T03:46:00Z
649,198
<p>It can be useful for objects of different types to be collected into a single, sorted list, in a definite order. By giving all objects a stable sort order, this behavior is default.</p>
1
2009-03-16T03:49:49Z
[ "python" ]
Why are Python objects of different types ordered by type names?
649,191
<p>From Python docs: <a href="http://docs.python.org/library/stdtypes.html#comparisons" rel="nofollow">http://docs.python.org/library/stdtypes.html#comparisons</a></p> <blockquote> <p>Implementation note: Objects of different types except numbers are ordered by their type names; objects of the same types that don’...
2
2009-03-16T03:46:00Z
649,199
<p>About four lines up from that line you quoted:</p> <blockquote> <p>Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily <strong>(so that sorting a heterogeneous array yields a consistent result...
5
2009-03-16T03:51:00Z
[ "python" ]
In Python, is it possible to access the class which contains a method, given only a method object?
649,329
<p>I'm pretty new to Python, and haven't been able to find an answer to this question from searching online.</p> <p>Here is an example decorator that does nothing (yet)</p> <pre><code>def my_decorator(text): def wrap(f): # grab magic f.parent_class_object.my_var and append text def wrap_f(*args, **kwargs): ...
2
2009-03-16T05:08:54Z
649,358
<p>It is not possible to read it from a function inside a class decorator, since the "methods" are still normal function while the class is being compiled. Also, the class name has not been defined while it is being compiled.</p> <p>What you could do is supply the <code>my_var</code> list to the decorator.</p> <pre><...
3
2009-03-16T05:21:55Z
[ "python", "decorator" ]
Iterability in Python
649,549
<p>I am trying to understand Iterability in Python.</p> <p>As I understand, <code>__iter__()</code> should return an object that has <code>next()</code> method defined which must return a value or raise <code>StopIteration</code> exception. Thus I wrote this class which satisfies both these conditions.</p> <p>But it ...
4
2009-03-16T07:20:51Z
649,555
<p><code>i</code> will never become greater than 5 if you don't increment it in <code>next()</code></p>
4
2009-03-16T07:27:40Z
[ "iterator", "python" ]
Iterability in Python
649,549
<p>I am trying to understand Iterability in Python.</p> <p>As I understand, <code>__iter__()</code> should return an object that has <code>next()</code> method defined which must return a value or raise <code>StopIteration</code> exception. Thus I wrote this class which satisfies both these conditions.</p> <p>But it ...
4
2009-03-16T07:20:51Z
649,566
<p>Your Iterator class is correct. You just have a typo in this statement:</p> <pre><code>if __name__ ==' __main__': </code></pre> <p>There's a leading whitespace in the ' __main__' string. That's why your code is not executed at all.</p>
9
2009-03-16T07:38:39Z
[ "iterator", "python" ]
Iterability in Python
649,549
<p>I am trying to understand Iterability in Python.</p> <p>As I understand, <code>__iter__()</code> should return an object that has <code>next()</code> method defined which must return a value or raise <code>StopIteration</code> exception. Thus I wrote this class which satisfies both these conditions.</p> <p>But it ...
4
2009-03-16T07:20:51Z
649,873
<p>I think in most cases it might be enough to write a <a href="http://docs.python.org/reference/simple%5Fstmts.html#the-yield-statement" rel="nofollow">generator function that uses yield</a> instead of writing a full-fledged iterator. </p>
4
2009-03-16T10:28:03Z
[ "iterator", "python" ]
Iterability in Python
649,549
<p>I am trying to understand Iterability in Python.</p> <p>As I understand, <code>__iter__()</code> should return an object that has <code>next()</code> method defined which must return a value or raise <code>StopIteration</code> exception. Thus I wrote this class which satisfies both these conditions.</p> <p>But it ...
4
2009-03-16T07:20:51Z
650,692
<p>Your current code seems to work. Instead i'll show you some more iterators/generators.</p> <p>the simplest builtin with exactly your behavior.</p> <pre><code>Iterator2 = xrange(2,5) </code></pre> <p>A direct translation of your class to a generator</p> <pre><code>def Iterator3(): i = 1 while i &lt; 5: ...
1
2009-03-16T14:36:31Z
[ "iterator", "python" ]
Iterability in Python
649,549
<p>I am trying to understand Iterability in Python.</p> <p>As I understand, <code>__iter__()</code> should return an object that has <code>next()</code> method defined which must return a value or raise <code>StopIteration</code> exception. Thus I wrote this class which satisfies both these conditions.</p> <p>But it ...
4
2009-03-16T07:20:51Z
36,095,850
<p>Your code has two problems:</p> <ul> <li><code>if name == '__main__'</code>: (missing quotes)</li> <li><code>def next . . .</code>: you don't increment <code>i</code> anywhere</li> </ul>
0
2016-03-18T23:19:08Z
[ "iterator", "python" ]
Directory checksum with python?
649,623
<p>So I'm in the middle of web-based filesystem abstraction layer development. Just like file browser, except it has some extra features like freaky permissions etc. </p> <p>I would like users to be notified somehow about directory changes. So, i.e. when someone uploads a new file via FTP, certain users should get a...
3
2009-03-16T08:22:52Z
649,636
<p>If your server is Linux you can do this with something like <a href="http://pyinotify.sourceforge.net/" rel="nofollow">inotify</a></p> <p>If the only updates are coming from FTP, then another solution I've used in the past is to write an add-on module to <a href="http://www.proftpd.org/features.html" rel="nofollow"...
2
2009-03-16T08:29:11Z
[ "python", "file", "filesystems", "checksum" ]
Directory checksum with python?
649,623
<p>So I'm in the middle of web-based filesystem abstraction layer development. Just like file browser, except it has some extra features like freaky permissions etc. </p> <p>I would like users to be notified somehow about directory changes. So, i.e. when someone uploads a new file via FTP, certain users should get a...
3
2009-03-16T08:22:52Z
649,638
<p>See this question: <a href="http://stackoverflow.com/questions/479762/how-to-quickly-find-added-removed-files">http://stackoverflow.com/questions/479762/how-to-quickly-find-added-removed-files</a></p> <p>But if you can control the upload somehow (i.e. use HTTP POST instead of FTP), you could simply send a notificat...
1
2009-03-16T08:29:47Z
[ "python", "file", "filesystems", "checksum" ]
Directory checksum with python?
649,623
<p>So I'm in the middle of web-based filesystem abstraction layer development. Just like file browser, except it has some extra features like freaky permissions etc. </p> <p>I would like users to be notified somehow about directory changes. So, i.e. when someone uploads a new file via FTP, certain users should get a...
3
2009-03-16T08:22:52Z
649,665
<p>A simple approach would be to monitor/check the last modification date of the working directory (using os.stat() for example). </p> <p>Whenever a file in a directory is modified, the working directory's (the directory the file is in) last modification date changes as well.</p> <p>At least this works on the filesys...
0
2009-03-16T08:47:34Z
[ "python", "file", "filesystems", "checksum" ]
Do Python lists have an equivalent to dict.get?
650,340
<p>I have a list of integers. I want to know whether the number 13 appears in it and, if so, where. Do I have to search the list twice, as in the code below?</p> <pre><code>if 13 in intList: i = intList.index(13) </code></pre> <p>In the case of dictionaries, there's a <code>get</code> function which will ascerta...
7
2009-03-16T13:04:46Z
650,350
<p>You answered it yourself, with the <code>index()</code> method. That will throw an exception if the index is not found, so just catch that:</p> <pre><code>def getIndexOrMinusOne(a, x): try: return a.index(x) except ValueError: return -1 </code></pre>
12
2009-03-16T13:07:46Z
[ "python", "list" ]
Do Python lists have an equivalent to dict.get?
650,340
<p>I have a list of integers. I want to know whether the number 13 appears in it and, if so, where. Do I have to search the list twice, as in the code below?</p> <pre><code>if 13 in intList: i = intList.index(13) </code></pre> <p>In the case of dictionaries, there's a <code>get</code> function which will ascerta...
7
2009-03-16T13:04:46Z
650,359
<p>It looks like you'll just have to catch the exception...</p> <pre><code>try: i = intList.index(13) except ValueError: i = some_default_value </code></pre>
7
2009-03-16T13:10:07Z
[ "python", "list" ]
Do Python lists have an equivalent to dict.get?
650,340
<p>I have a list of integers. I want to know whether the number 13 appears in it and, if so, where. Do I have to search the list twice, as in the code below?</p> <pre><code>if 13 in intList: i = intList.index(13) </code></pre> <p>In the case of dictionaries, there's a <code>get</code> function which will ascerta...
7
2009-03-16T13:04:46Z
650,364
<p>Just put what you got in a function and use it:) </p> <p>You can either use <code>if i in list: return list.index(i)</code> or the <code>try/except</code>, depending on your preferences.</p>
-1
2009-03-16T13:11:07Z
[ "python", "list" ]
Do Python lists have an equivalent to dict.get?
650,340
<p>I have a list of integers. I want to know whether the number 13 appears in it and, if so, where. Do I have to search the list twice, as in the code below?</p> <pre><code>if 13 in intList: i = intList.index(13) </code></pre> <p>In the case of dictionaries, there's a <code>get</code> function which will ascerta...
7
2009-03-16T13:04:46Z
650,368
<p>No, there isn't a direct match for what you asked for. There was <strong><a href="http://mail.python.org/pipermail/python-dev/2006-July/067263.html" rel="nofollow">a discussion a while back</a></strong> on the Python mailing list about this, and people reached the conclusion that it was probably a code smell if you ...
3
2009-03-16T13:12:22Z
[ "python", "list" ]
Do Python lists have an equivalent to dict.get?
650,340
<p>I have a list of integers. I want to know whether the number 13 appears in it and, if so, where. Do I have to search the list twice, as in the code below?</p> <pre><code>if 13 in intList: i = intList.index(13) </code></pre> <p>In the case of dictionaries, there's a <code>get</code> function which will ascerta...
7
2009-03-16T13:04:46Z
650,373
<p>You can catch the ValueError exception, or you can do:</p> <pre><code>i = intList.index(13) if 13 in intList else -1 </code></pre> <p>(Python 2.5+)</p> <p>BTW. if you're going to do a big batch of similar operations, you might consider building inverse dictionary value -> index.</p> <pre><code>intList = [13,1,2,...
2
2009-03-16T13:13:41Z
[ "python", "list" ]
How to make a singleton class with Python Flup fastcgi server?
650,518
<p>I use flup as fastcgi server for Django.</p> <p>Please explain to me how can I use singleton? I'm not sure I understand threading models for Flup well.</p>
1
2009-03-16T13:55:46Z
986,981
<p>If you use a forked server, you will not be able to have a singleton at all (at least no singleton that lifes longer than your actual context).</p> <p>With a threaded server, it should be possibe (but I am not so much in Django and Web servers!).</p> <p>Have you tried such a code (as an additional module):</p> <p...
0
2009-06-12T14:37:52Z
[ "python", "django", "flup" ]
Rotation based on end points
650,646
<p>I'm using pygame to draw a line between two arbitrary points. I also want to append arrows at the end of the lines that face outward in the directions the line is traveling. </p> <p>It's simple enough to stick an arrow image at the end, but I have no clue how the calculate the degrees of rotation to keep the arrows...
2
2009-03-16T14:25:00Z
650,668
<p>I would have to look up the exact functions to use, but how about making a right triangle where the hypotenuse is the line in question and the legs are axis-aligned, and using some basic trigonometry to calculate the angle of the line based on the lengths of the sides of the triangle? Of course, you will have to sp...
1
2009-03-16T14:29:56Z
[ "python", "geometry", "pygame" ]
Rotation based on end points
650,646
<p>I'm using pygame to draw a line between two arbitrary points. I also want to append arrows at the end of the lines that face outward in the directions the line is traveling. </p> <p>It's simple enough to stick an arrow image at the end, but I have no clue how the calculate the degrees of rotation to keep the arrows...
2
2009-03-16T14:25:00Z
650,854
<p>We're assuming that 0 degrees means the arrow is pointing to the right, 90 degrees means pointing straight up and 180 degrees means pointing to the left.</p> <p>There are several ways to do this, the simplest is probably using the atan2 function. if your starting point is (x1,y1) and your end point is (x2,y2) then ...
2
2009-03-16T15:14:25Z
[ "python", "geometry", "pygame" ]
Rotation based on end points
650,646
<p>I'm using pygame to draw a line between two arbitrary points. I also want to append arrows at the end of the lines that face outward in the directions the line is traveling. </p> <p>It's simple enough to stick an arrow image at the end, but I have no clue how the calculate the degrees of rotation to keep the arrows...
2
2009-03-16T14:25:00Z
651,089
<p>Here is the complete code to do it. Note that when using pygame, the y co-ordinate is measured from the top, and so we take the negative when using math functions.</p> <pre><code>import pygame import math import random pygame.init() screen=pygame.display.set_mode((300,300)) screen.fill((255,255,255)) pos1=random....
8
2009-03-16T16:12:37Z
[ "python", "geometry", "pygame" ]
Rotation based on end points
650,646
<p>I'm using pygame to draw a line between two arbitrary points. I also want to append arrows at the end of the lines that face outward in the directions the line is traveling. </p> <p>It's simple enough to stick an arrow image at the end, but I have no clue how the calculate the degrees of rotation to keep the arrows...
2
2009-03-16T14:25:00Z
656,119
<p>just to append to the above code, you'd probably want an event loop so it wouldn't quit right away:</p> <pre><code>... clock = pygame.time.Clock() running = True while (running): clock.tick() </code></pre>
0
2009-03-17T21:34:41Z
[ "python", "geometry", "pygame" ]
Python, redirecting the stream of Popen to a python function
650,877
<p>I'm new to python programming. I have this problem: I have a list of text files (both compressed and not) and I need to : - connect to the server and open them - after the opening of the file, I need to take his content and pass it to another python function that I wrote</p> <pre><code>def readLogs (fileName): f =...
5
2009-03-16T15:19:11Z
650,994
<p>You do not have to split the stream/file into lines yourself. Just iterate:</p> <pre><code>for ln in f: # work on line in ln </code></pre> <p>This should work equally well for files (using open() for file()) and pipes (using Popen). Use the <code>stdout</code> property of the popen object to access the pipe co...
5
2009-03-16T15:48:11Z
[ "python", "stream", "subprocess", "yield", "popen" ]
Python, redirecting the stream of Popen to a python function
650,877
<p>I'm new to python programming. I have this problem: I have a list of text files (both compressed and not) and I need to : - connect to the server and open them - after the opening of the file, I need to take his content and pass it to another python function that I wrote</p> <pre><code>def readLogs (fileName): f =...
5
2009-03-16T15:19:11Z
651,221
<p>Remove InStream and just use the file object.</p> <p>So that your code would read:</p> <pre><code>for nextLine in f.readlines(): . . . </code></pre> <p>Ber has it right. </p> <p>To clarify, the default iteration behavior of a file object is to return the next line. so "<strong>for nextLine in f</str...
0
2009-03-16T16:45:49Z
[ "python", "stream", "subprocess", "yield", "popen" ]
Python, redirecting the stream of Popen to a python function
650,877
<p>I'm new to python programming. I have this problem: I have a list of text files (both compressed and not) and I need to : - connect to the server and open them - after the opening of the file, I need to take his content and pass it to another python function that I wrote</p> <pre><code>def readLogs (fileName): f =...
5
2009-03-16T15:19:11Z
797,183
<p>If you want to do something via ssh, why not use <a href="http://pyssh.sourceforge.net/" rel="nofollow">the Python SSH module</a>?</p>
0
2009-04-28T10:11:44Z
[ "python", "stream", "subprocess", "yield", "popen" ]
Python, redirecting the stream of Popen to a python function
650,877
<p>I'm new to python programming. I have this problem: I have a list of text files (both compressed and not) and I need to : - connect to the server and open them - after the opening of the file, I need to take his content and pass it to another python function that I wrote</p> <pre><code>def readLogs (fileName): f =...
5
2009-03-16T15:19:11Z
7,638,410
<p>Try this page, best info on popen I have found so far....</p> <p><a href="http://jimmyg.org/blog/2009/working-with-python-subprocess.html" rel="nofollow">http://jimmyg.org/blog/2009/working-with-python-subprocess.html</a></p>
0
2011-10-03T17:20:53Z
[ "python", "stream", "subprocess", "yield", "popen" ]
How do you substitue a Python capture followed by a number character?
650,926
<p>When using <a href="http://docs.python.org/library/re.html" rel="nofollow">re.sub</a>, how to you handle a situation where you need a capture followed by a number in the replacement string? For example, you cannot use "\10" for capture 1 followed by a '0' character because it will be interpreted as capture 10.</p>
2
2009-03-16T15:34:43Z
650,956
<pre><code>\g&lt;1&gt;0 </code></pre> <p><a href="http://docs.python.org/library/re.html#re.sub" rel="nofollow">http://docs.python.org/library/re.html#re.sub</a></p> <blockquote> <p>\g&lt;number> uses the corresponding group number; \g&lt;2> is therefore equivalent to \2, but isn’t ambiguous in a replacemen...
5
2009-03-16T15:40:27Z
[ "python", "regex", "string" ]
Is it possible, with the Python Standard Library (say version 2.5) to perform MS-SQL queries which are parameterized?
650,979
<p>While the particular data I'm working with right now will not be user-generated, and will be sanitized within an inch of its life during my usual validation routines, I would like to learn how to do your basic INSERT, SELECT, etc. SQL queries while protecting myself against SQL injection attacks, just for future ref...
2
2009-03-16T15:44:47Z
651,034
<p><a href="http://www.python.org/dev/peps/pep-0249/" rel="nofollow">PEP 249 (DB API 2.0)</a> defines 5 paramstyles, <a href="http://pymssql.sourceforge.net/" rel="nofollow">PyMSSQL</a> uses paramstyle == pyformat. But although it looks like string interpolation, <strong>it is actually binding</strong>.</p> <p>Note di...
5
2009-03-16T15:55:32Z
[ "python", "sql", "binding", "parameters", "sql-injection" ]
Is it possible, with the Python Standard Library (say version 2.5) to perform MS-SQL queries which are parameterized?
650,979
<p>While the particular data I'm working with right now will not be user-generated, and will be sanitized within an inch of its life during my usual validation routines, I would like to learn how to do your basic INSERT, SELECT, etc. SQL queries while protecting myself against SQL injection attacks, just for future ref...
2
2009-03-16T15:44:47Z
651,194
<p>Try <a href="http://code.google.com/p/pyodbc/" rel="nofollow">pyodbc</a></p> <p>But if you want to have things really easy (plus tons of powerful features), take a look at <a href="http://www.sqlalchemy.org/" rel="nofollow">sqlalchemy</a> (which by the way uses pyodbc as the default "driver" for mssql)</p>
2
2009-03-16T16:37:49Z
[ "python", "sql", "binding", "parameters", "sql-injection" ]
How do I check if it's the homepage in a Plone website using ZPT?
651,009
<p>I want to change my website's header only it if's not the homepage. Is there a <em>tal:condition</em> expression for that?</p> <p>I've been reading <a href="http://plone.org/documentation/tutorial/zpt" rel="nofollow">this</a> and can't find what I'm looking for...</p> <p>thanks!</p>
3
2009-03-16T15:50:22Z
651,703
<p>how about something like <code>&lt;tal:condition="python: request.URLPATH0 == '/index_html'</code> ...>`? see <a href="http://docs.zope.org/zope2/zope2book/source/AppendixC.html#built-in-names" rel="nofollow">TALES Built-in Names</a> and the <a href="http://www.zope.org/Documentation/Books/ZopeBook/2%5F6Edition/Appe...
0
2009-03-16T18:56:57Z
[ "python", "plone", "zope", "template-tal", "zpt" ]
How do I check if it's the homepage in a Plone website using ZPT?
651,009
<p>I want to change my website's header only it if's not the homepage. Is there a <em>tal:condition</em> expression for that?</p> <p>I've been reading <a href="http://plone.org/documentation/tutorial/zpt" rel="nofollow">this</a> and can't find what I'm looking for...</p> <p>thanks!</p>
3
2009-03-16T15:50:22Z
1,509,184
<p>I use something similar to ax:</p> <pre><code>&lt;tal:block define="global currentUrl request/getURL" condition="python: u'home' not in str(currentUrl)"&gt; &lt;!-- whatever --&gt; &lt;/tal:block&gt; </code></pre>
1
2009-10-02T12:15:07Z
[ "python", "plone", "zope", "template-tal", "zpt" ]
How do I check if it's the homepage in a Plone website using ZPT?
651,009
<p>I want to change my website's header only it if's not the homepage. Is there a <em>tal:condition</em> expression for that?</p> <p>I've been reading <a href="http://plone.org/documentation/tutorial/zpt" rel="nofollow">this</a> and can't find what I'm looking for...</p> <p>thanks!</p>
3
2009-03-16T15:50:22Z
1,693,676
<p>The best way is to use two really handy plone views that are intended just for this purpose. The interface that defines them is at <a href="https://svn.plone.org/svn/plone/plone.app.layout/trunk/plone/app/layout/globals/interfaces.py">https://svn.plone.org/svn/plone/plone.app.layout/trunk/plone/app/layout/globals/in...
6
2009-11-07T16:54:04Z
[ "python", "plone", "zope", "template-tal", "zpt" ]
Concurrency: Are Python extensions written in C/C++ affected by the Global Interpreter Lock?
651,048
<p>One of Python's strongest points is the ease of writing C and C++ extensions to speed up processor intensive parts of the code. Can these extensions avoid the Global Interpreter Lock or are they also restricted by the GIL? If not, then this "ease of extension" is even more of a killer feature than I previously reali...
16
2009-03-16T16:00:10Z
651,078
<p>C/C++ extensions to Python are not bound by the GIL. However, you really need to know what you're doing. <a href="http://docs.python.org/c-api/init.html">From <a href="http://docs.python.org/c-api/init.html">http://docs.python.org/c-api/init.html</a></a>:</p> <blockquote> <p>The global interpreter lock is used ...
7
2009-03-16T16:08:21Z
[ "c++", "python", "c", "multithreading" ]
Concurrency: Are Python extensions written in C/C++ affected by the Global Interpreter Lock?
651,048
<p>One of Python's strongest points is the ease of writing C and C++ extensions to speed up processor intensive parts of the code. Can these extensions avoid the Global Interpreter Lock or are they also restricted by the GIL? If not, then this "ease of extension" is even more of a killer feature than I previously reali...
16
2009-03-16T16:00:10Z
651,238
<p>Yes, calls to C extensions (C routines called from Python) are still subject to the GIL.</p> <p>However, you can <em>manually</em> release the GIL inside your C extension, so long as you are careful to re-assert it before returning control to the Python VM.</p> <p>For information, take a look at the <code>Py_BEGIN...
15
2009-03-16T16:51:08Z
[ "c++", "python", "c", "multithreading" ]
Concurrency: Are Python extensions written in C/C++ affected by the Global Interpreter Lock?
651,048
<p>One of Python's strongest points is the ease of writing C and C++ extensions to speed up processor intensive parts of the code. Can these extensions avoid the Global Interpreter Lock or are they also restricted by the GIL? If not, then this "ease of extension" is even more of a killer feature than I previously reali...
16
2009-03-16T16:00:10Z
652,452
<p>Here's a long article I wrote for python magazine that touches on the c-extension/GIL/threading thing. It's a bit long at 4000 words, but it should help.</p> <p><a href="http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/">http://jessenoller.com/2009/02/01/python-threads-and-the-global...
7
2009-03-16T22:56:36Z
[ "c++", "python", "c", "multithreading" ]
Concurrency: Are Python extensions written in C/C++ affected by the Global Interpreter Lock?
651,048
<p>One of Python's strongest points is the ease of writing C and C++ extensions to speed up processor intensive parts of the code. Can these extensions avoid the Global Interpreter Lock or are they also restricted by the GIL? If not, then this "ease of extension" is even more of a killer feature than I previously reali...
16
2009-03-16T16:00:10Z
733,143
<p>Check out Cython, it has similar syntax to Python but with a few constructs like "cdef", fast numpy access functions, and a "with nogil" statement (which does what it says).</p>
0
2009-04-09T07:17:20Z
[ "c++", "python", "c", "multithreading" ]
Concurrency: Are Python extensions written in C/C++ affected by the Global Interpreter Lock?
651,048
<p>One of Python's strongest points is the ease of writing C and C++ extensions to speed up processor intensive parts of the code. Can these extensions avoid the Global Interpreter Lock or are they also restricted by the GIL? If not, then this "ease of extension" is even more of a killer feature than I previously reali...
16
2009-03-16T16:00:10Z
36,491,019
<p>If you’re writing your extension in C++, you can use RAII to easily and legibly write code manipulating the GIL. I use this pair of RAII structlets:</p> <pre class="lang-cpp prettyprint-override"><code>namespace py { namespace gil { struct release { PyThreadState* state; bool...
0
2016-04-08T03:29:57Z
[ "c++", "python", "c", "multithreading" ]
A simple Python IRC client library that supports SSL?
651,053
<p>A simple Python IRC client library that supports SSL?</p>
5
2009-03-16T16:01:01Z
651,086
<p><a href="http://twistedmatrix.com/trac/wiki/TwistedWords">Twisted</a> has an IRC client (in twisted.words), and it supports SSL.</p> <p>There is an <a href="http://twistedmatrix.com/projects/words/documentation/examples/ircLogBot.py">example in the documentation</a>, just remember to do <code>reactor.connectSSL</co...
14
2009-03-16T16:12:07Z
[ "python", "irc" ]
A simple Python IRC client library that supports SSL?
651,053
<p>A simple Python IRC client library that supports SSL?</p>
5
2009-03-16T16:01:01Z
651,102
<p>How simple? Chatzilla supports SSL and as any Mozilla Platform applications, allows using Python. https://developer.mozilla.org/En/Python </p> <p>On second thought, that probably is a total overkill. A second on Ali's answer, Twisted will do.</p>
0
2009-03-16T16:15:58Z
[ "python", "irc" ]
A simple Python IRC client library that supports SSL?
651,053
<p>A simple Python IRC client library that supports SSL?</p>
5
2009-03-16T16:01:01Z
23,968,810
<p>irc3 support ssl (via asyncio) <a href="https://irc3.readthedocs.org/" rel="nofollow">https://irc3.readthedocs.org/</a></p>
0
2014-05-31T10:16:41Z
[ "python", "irc" ]
Trappings MySQL Warnings on Calls Wrapped in Classes -- Python
651,358
<p>I can't get Python's try/else blocks to catch MySQL warnings when the execution statements are wrapped in classes.</p> <p>I have a class that has as a MySQL connection object as an attribute, a MySQL cursor object as another, and a method that run queries through that cursor object. The cursor is itself wrapped in...
-1
2009-03-16T17:26:59Z
1,219,572
<p>On first glance at least one problem:</p> <pre><code> if dict_flag: dbMySQL_Cursor = conn.cursor(MySQLdb.cursors.DictCursor) </code></pre> <p>shouldn't that be</p> <pre><code> if dict_flag: self.dbMySQL_Cursor = conn.cursor(MySQLdb.cursors.DictCursor) </code></pre> <p>You're mixin...
0
2009-08-02T19:10:42Z
[ "python", "mysql" ]
Bimodal distribution in C or Python
651,421
<p>What's the easiest way to generate random values according to a bimodal distribution in C or Python?</p> <p>I could implement something like the Ziggurat algorithm or a Box-Muller transform, but if there's a ready-to-use library, or a simpler algorithm I don't know about, that'd be better.</p>
2
2009-03-16T17:42:32Z
651,457
<p>Aren't you just picking values either of two modal distributions?</p> <p><a href="http://docs.python.org/library/random.html#random.triangular" rel="nofollow">http://docs.python.org/library/random.html#random.triangular</a></p> <p>Sounds like you just toggle back and forth between two sets of parameters for your c...
4
2009-03-16T17:49:31Z
[ "python", "c", "random" ]
Bimodal distribution in C or Python
651,421
<p>What's the easiest way to generate random values according to a bimodal distribution in C or Python?</p> <p>I could implement something like the Ziggurat algorithm or a Box-Muller transform, but if there's a ready-to-use library, or a simpler algorithm I don't know about, that'd be better.</p>
2
2009-03-16T17:42:32Z
651,475
<p>There's always the old-fashioned straight-forward <a href="http://en.wikipedia.org/wiki/Rejection%5Fsampling" rel="nofollow">accept-reject algorithm</a>. If it was good enough for Johnny von Neumann it should be good enough for you ;-).</p>
2
2009-03-16T17:53:19Z
[ "python", "c", "random" ]
Which version of python is currently best for os x?
651,717
<p>After going through hell trying to install the latest version of postgresql and psycopg2 today I'm going for a complete reinstall of Leopard.</p> <p>I've been sticking with macpython 2.5 for the past year but now I'm considering macports even 2.6</p> <p>For me it's most important for Twisted, PIL and psycopg2 to b...
2
2009-03-16T19:00:09Z
651,764
<p>I've updated my macbook running leopard to python 2.6 and haven't had any problems with psycopg2. For that matter, I haven't had any compatibility issues anywhere with 2.6, but obviously switching to python3k isn't exactly recommended if you're concerned about backwards compatibility.</p>
1
2009-03-16T19:12:20Z
[ "python", "osx" ]
Which version of python is currently best for os x?
651,717
<p>After going through hell trying to install the latest version of postgresql and psycopg2 today I'm going for a complete reinstall of Leopard.</p> <p>I've been sticking with macpython 2.5 for the past year but now I'm considering macports even 2.6</p> <p>For me it's most important for Twisted, PIL and psycopg2 to b...
2
2009-03-16T19:00:09Z
651,768
<p>You can install them side-by-side. If you've encounter problems just set python 2.5 as the standard python and use e.g. <code>python26</code> for a newer version.</p>
4
2009-03-16T19:14:10Z
[ "python", "osx" ]
Which version of python is currently best for os x?
651,717
<p>After going through hell trying to install the latest version of postgresql and psycopg2 today I'm going for a complete reinstall of Leopard.</p> <p>I've been sticking with macpython 2.5 for the past year but now I'm considering macports even 2.6</p> <p>For me it's most important for Twisted, PIL and psycopg2 to b...
2
2009-03-16T19:00:09Z
651,832
<p>Read this</p> <p><a href="http://farmdev.com/thoughts/66/python-3-0-on-mac-os-x-alongside-2-6-2-5-etc-/" rel="nofollow">http://farmdev.com/thoughts/66/python-3-0-on-mac-os-x-alongside-2-6-2-5-etc-/</a></p>
3
2009-03-16T19:31:46Z
[ "python", "osx" ]
Which version of python is currently best for os x?
651,717
<p>After going through hell trying to install the latest version of postgresql and psycopg2 today I'm going for a complete reinstall of Leopard.</p> <p>I've been sticking with macpython 2.5 for the past year but now I'm considering macports even 2.6</p> <p>For me it's most important for Twisted, PIL and psycopg2 to b...
2
2009-03-16T19:00:09Z
651,835
<p>I would stick with the MacPython version 2.5.x (I believe 2.5.4 currently). Here's my rationale:</p> <ol> <li>Snow Leopard may still be on the 2.5 series, so you might as well be consistent with the future OS (i.e. no point in going too far ahead).</li> <li>For most production apps, nobody is going to want to use ...
1
2009-03-16T19:32:28Z
[ "python", "osx" ]
Which version of python is currently best for os x?
651,717
<p>After going through hell trying to install the latest version of postgresql and psycopg2 today I'm going for a complete reinstall of Leopard.</p> <p>I've been sticking with macpython 2.5 for the past year but now I'm considering macports even 2.6</p> <p>For me it's most important for Twisted, PIL and psycopg2 to b...
2
2009-03-16T19:00:09Z
651,850
<p>I still use macports python25, because so many other packages depend on it, and have not updated to use python26.</p> <pre><code>$ port dependents python25 gnome-doc-utils depends on python25 mod_python25 depends on python25 postgresql83 depends on python25 gtk-doc depends on python25 at-spi depends on python25 gno...
3
2009-03-16T19:36:50Z
[ "python", "osx" ]
Which version of python is currently best for os x?
651,717
<p>After going through hell trying to install the latest version of postgresql and psycopg2 today I'm going for a complete reinstall of Leopard.</p> <p>I've been sticking with macpython 2.5 for the past year but now I'm considering macports even 2.6</p> <p>For me it's most important for Twisted, PIL and psycopg2 to b...
2
2009-03-16T19:00:09Z
651,988
<p>I use both Twisted and Psycopg2 extensively on OSX, and both work fine with Python 2.6. Neither has been ported to Python 3.0, as far as I know.</p> <p>Several of Python 3.0's features have been back-ported to 2.6, so you gain quite a bit by moving from 2.5 to 2.6. But I wouldn't switch to 3.0 until all of your t...
1
2009-03-16T20:14:26Z
[ "python", "osx" ]
Which version of python is currently best for os x?
651,717
<p>After going through hell trying to install the latest version of postgresql and psycopg2 today I'm going for a complete reinstall of Leopard.</p> <p>I've been sticking with macpython 2.5 for the past year but now I'm considering macports even 2.6</p> <p>For me it's most important for Twisted, PIL and psycopg2 to b...
2
2009-03-16T19:00:09Z
652,441
<p>I wrote something today on this very subject, my recommendation? Run multiple version, and slap virtualenv down to compartmentalize things.</p> <p><a href="http://jessenoller.com/2009/03/16/so-you-want-to-use-python-on-the-mac/" rel="nofollow">http://jessenoller.com/2009/03/16/so-you-want-to-use-python-on-the-mac/<...
2
2009-03-16T22:50:30Z
[ "python", "osx" ]