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
Object appended to a list instance appears in a different instance of that list
1,860,737
<p>I was writing this little piece of code as an exercise in object-oriented programming.</p> <p>Here I'm trying to define a house as a list of rooms and each room as a list of devices (lamps, for example).</p> <p>First I created all the objects and them appended the two rooms to the house and a different device to e...
1
2009-12-07T15:35:15Z
1,860,763
<p>Default parameter values for functions are evaluated only once. This means that all instances of House will use the same list instance for <code>self.rooms</code> (if rooms parameter wasn't given in construction). In the same manner, all instances of Room will share the same list for <code>self.devs</code>.</p> <p>...
9
2009-12-07T15:39:38Z
[ "python", "default", "mutable", "arguments" ]
Object appended to a list instance appears in a different instance of that list
1,860,737
<p>I was writing this little piece of code as an exercise in object-oriented programming.</p> <p>Here I'm trying to define a house as a list of rooms and each room as a list of devices (lamps, for example).</p> <p>First I created all the objects and them appended the two rooms to the house and a different device to e...
1
2009-12-07T15:35:15Z
1,860,810
<pre><code>def __init__(self, name = 'a room', devs = list()): self.name = name self.devs = devs print('room ' + self.name + ' created') </code></pre> <p>When you do this <code>list()</code> actually is always the same list. You don't get a <em>new</em> empty list each time the constructor is called, you g...
0
2009-12-07T15:46:46Z
[ "python", "default", "mutable", "arguments" ]
Object appended to a list instance appears in a different instance of that list
1,860,737
<p>I was writing this little piece of code as an exercise in object-oriented programming.</p> <p>Here I'm trying to define a house as a list of rooms and each room as a list of devices (lamps, for example).</p> <p>First I created all the objects and them appended the two rooms to the house and a different device to e...
1
2009-12-07T15:35:15Z
1,860,830
<p>The default argument is evaluated once, at the point of declaration of the method. That value is then used in all calls to the method.</p> <p>There are other question on stackoverflow <a href="http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument">exploring the reasons...
3
2009-12-07T15:50:05Z
[ "python", "default", "mutable", "arguments" ]
Writing my own django-cms plugin. Any recommendations?
1,861,267
<p>I don't see any possibility for creating a table in django-cms. I need this functionnality so I am evaluating the possibility to write my own plugin.</p> <p>I am getting started with this product. I've read the documentation carefully and I see more or less how to do that.</p> <p>However, I would be happy to hear ...
1
2009-12-07T16:52:25Z
1,872,391
<p>This all depends on your model. Plugins use standard django admin features.</p> <p>This also depends on the source data for the table. </p> <p>If you have a CSV or Exel sheet as source i only would make a file field and render the file in the render function with some optional caching.</p> <p>If you want to enter...
1
2009-12-09T08:22:30Z
[ "python", "django", "django-cms" ]
How would I call 32bit exes in Windows 64bit with python?
1,861,511
<p>I want to call a exe from python on a 64bit version of vista. I know to use subprocess, but all the 32bit apps are store in C:\Program Files (x86)\, and it doesn't like the spaces I believe. i have tried escape characters, doesn't fire, any ideas? </p>
1
2009-12-07T17:28:16Z
1,861,587
<pre><code>textEditorExecutablePath = 'C:\\Program Files (x86)\\Notepad2\\Notepad2.exe' filepathToOpen = 'C:\\file.txt' subprocess.Popen([textEditorExecutablePath, filepathToOpen]) </code></pre> <p>Works for me. How are you calling Popen?</p>
1
2009-12-07T17:38:05Z
[ "python", "windows", "64bit", "32-bit" ]
Why is this invalid syntax?
1,861,555
<p>Why is this complaining about an invalid syntax?</p> <pre><code>#! /usr/bin/python recipients = [] recipients.append('chris@elserinteractive.com') for recip in recipients: print recip </code></pre> <p>I keep getting:</p> <pre><code>File "send_test_email.py", line 31 print recip ^ SyntaxErr...
2
2009-12-07T17:35:03Z
1,861,576
<p>If you are using Python 3 <code>print</code> is a function. Call it like this: <code>print(recip)</code>.</p>
11
2009-12-07T17:36:57Z
[ "python", "printing", "python-3.x", "syntax-error" ]
Why is this invalid syntax?
1,861,555
<p>Why is this complaining about an invalid syntax?</p> <pre><code>#! /usr/bin/python recipients = [] recipients.append('chris@elserinteractive.com') for recip in recipients: print recip </code></pre> <p>I keep getting:</p> <pre><code>File "send_test_email.py", line 31 print recip ^ SyntaxErr...
2
2009-12-07T17:35:03Z
1,861,578
<p>If it's Python 3, <code>print</code> is now a function. The correct syntax would be</p> <pre><code>print (recip) </code></pre>
3
2009-12-07T17:37:00Z
[ "python", "printing", "python-3.x", "syntax-error" ]
Why is this invalid syntax?
1,861,555
<p>Why is this complaining about an invalid syntax?</p> <pre><code>#! /usr/bin/python recipients = [] recipients.append('chris@elserinteractive.com') for recip in recipients: print recip </code></pre> <p>I keep getting:</p> <pre><code>File "send_test_email.py", line 31 print recip ^ SyntaxErr...
2
2009-12-07T17:35:03Z
1,861,619
<p>In python 3, print is no longer a statement, but a <a href="http://docs.python.org/dev/3.0/whatsnew/3.0.html#print-is-a-function" rel="nofollow">function</a>.</p> <pre><code>Old: print "The answer is", 2*2 New: print("The answer is", 2*2) </code></pre> <p>More python 3 <code>print</code> functionality:</p> <pre><...
4
2009-12-07T17:41:03Z
[ "python", "printing", "python-3.x", "syntax-error" ]
Python IO Gurus: what are the differences between these two methods?
1,861,651
<p>I have two methods for writing binary files: the first works with data received by a server corresponding to a file upload (i.e., handling a form whose enctype="multipart/form-data"), and the second works with file data sent as email attachments (i.e., file data obtained by parsing an email message message body usin...
1
2009-12-07T17:44:53Z
1,861,698
<p>The most obvious difference is the chunked reading of data. You don't specify the error, but I'm guessing that the chunked method fails in the call to <code>read_buffer</code>.</p>
0
2009-12-07T17:51:38Z
[ "python", "file-io" ]
Python IO Gurus: what are the differences between these two methods?
1,861,651
<p>I have two methods for writing binary files: the first works with data received by a server corresponding to a file upload (i.e., handling a form whose enctype="multipart/form-data"), and the second works with file data sent as email attachments (i.e., file data obtained by parsing an email message message body usin...
1
2009-12-07T17:44:53Z
1,861,724
<p>The first one probably expects a file-like object as a parameter, from which it reads the data. The second one expects that parameter to be a string with the actual data to be written.</p> <p>To be sure you have to look at what your <code>read_buffer</code> function does.</p>
1
2009-12-07T17:54:36Z
[ "python", "file-io" ]
Python IO Gurus: what are the differences between these two methods?
1,861,651
<p>I have two methods for writing binary files: the first works with data received by a server corresponding to a file upload (i.e., handling a form whose enctype="multipart/form-data"), and the second works with file data sent as email attachments (i.e., file data obtained by parsing an email message message body usin...
1
2009-12-07T17:44:53Z
1,861,771
<p>The difference is that the HTTP upload method (the first one) - receives as its parameters the file-like object itself (the "f" variable) and creates a CGI module specific "read_buffer" to read data in chunks from that file object to copy them to the actual file. </p> <p>Thsi can make sense in an http upload appli...
2
2009-12-07T18:00:55Z
[ "python", "file-io" ]
Checking File Permissions in Linux with Python
1,861,836
<p>I'm writing a script to check permissions of files in user's directories and if they're not acceptable I'll be warning them, but I want to check permissions of not just the logged in user, but also group and others. How can i do this? It seems to me that .access() in Python can only check the permissions for the use...
30
2009-12-07T18:10:31Z
1,861,849
<p>Use <code>os.access()</code> with flags <code>os.R_OK</code>, <code>os.W_OK</code>, and <code>os.X_OK</code>.</p> <p><strong>Edit</strong>: Check out <a href="http://stackoverflow.com/questions/539133/python-test-directory-permissions">this related question</a> if you are testing directory permissions on Windows.</...
5
2009-12-07T18:13:11Z
[ "python" ]
Checking File Permissions in Linux with Python
1,861,836
<p>I'm writing a script to check permissions of files in user's directories and if they're not acceptable I'll be warning them, but I want to check permissions of not just the logged in user, but also group and others. How can i do this? It seems to me that .access() in Python can only check the permissions for the use...
30
2009-12-07T18:10:31Z
1,861,859
<p>You can check file permissions via <a href="http://docs.python.org/library/os.html#os.stat"><code>os.stat(path)</code></a> in conjunction with the <a href="http://docs.python.org/library/stat.html"><code>stat</code></a> module for interpreting the results.</p>
6
2009-12-07T18:15:00Z
[ "python" ]
Checking File Permissions in Linux with Python
1,861,836
<p>I'm writing a script to check permissions of files in user's directories and if they're not acceptable I'll be warning them, but I want to check permissions of not just the logged in user, but also group and others. How can i do this? It seems to me that .access() in Python can only check the permissions for the use...
30
2009-12-07T18:10:31Z
1,861,894
<p><a href="http://docs.python.org/library/os.html#os.stat" rel="nofollow">os.stat</a> and the associated <a href="http://docs.python.org/library/stat.html#stat.S%5FIRWXU" rel="nofollow">bit masks</a> for the mode.</p>
2
2009-12-07T18:20:22Z
[ "python" ]
Checking File Permissions in Linux with Python
1,861,836
<p>I'm writing a script to check permissions of files in user's directories and if they're not acceptable I'll be warning them, but I want to check permissions of not just the logged in user, but also group and others. How can i do this? It seems to me that .access() in Python can only check the permissions for the use...
30
2009-12-07T18:10:31Z
1,861,970
<p>You're right that <a href="http://docs.python.org/library/os.html?highlight=os.access#os.access">os.access</a>, like the underlying <a href="http://linux.die.net/man/2/access">access</a> syscall, checks for a specific user (real rather than effective IDs, to help out with suid situations).</p> <p><a href="http://do...
63
2009-12-07T18:31:31Z
[ "python" ]
What's a good general way to look SQLAlchemy transactions, complete with authenticated user, etc?
1,862,029
<p>I'm using SQLAlchemy's declarative extension. I'd like all changes to tables logs, including changes in many-to-many relationships (mapping tables). Each table should have a separate "log" table with a similar schema, but additional columns specifying when the change was made, who made the change, etc.</p> <p>My pr...
7
2009-12-07T18:43:29Z
1,865,906
<p>There are too many questions in one, so they that full answers to all them won't fit StackOverflow answer format. I'll try to describe hints in short, so ask separate question for them if it's not enough.</p> <h2>Assigning user and description to transaction</h2> <p>The most popular way to do so is assigning user ...
5
2009-12-08T09:54:21Z
[ "python", "sqlalchemy" ]
What's a good general way to look SQLAlchemy transactions, complete with authenticated user, etc?
1,862,029
<p>I'm using SQLAlchemy's declarative extension. I'd like all changes to tables logs, including changes in many-to-many relationships (mapping tables). Each table should have a separate "log" table with a similar schema, but additional columns specifying when the change was made, who made the change, etc.</p> <p>My pr...
7
2009-12-07T18:43:29Z
1,931,838
<p>We have a pretty comprehensive "versioning" recipe at <a href="http://www.sqlalchemy.org/trac/wiki/UsageRecipes/LogVersions" rel="nofollow">http://www.sqlalchemy.org/trac/wiki/UsageRecipes/LogVersions</a> . It seems some other users have contributed some variants on it. The mechanics of "add a row when something...
1
2009-12-19T03:20:47Z
[ "python", "sqlalchemy" ]
Message instance has no attribute 'read' in Google app engine mail receive
1,862,041
<p>Code in receive handler </p> <pre><code>class LogSenderHandler(InboundMailHandler): def receive(self, mail_message): logging.info("Received a message from: " + mail_message.sender) #logging.info("Received a message from: " + mail_message.attachments) logging.info("Received a message from: " + mail_m...
2
2009-12-07T18:45:25Z
1,868,566
<p>Do you have the latest version of the API? for the incoming mail function need to be the 1.2.6 or greatest.</p> <p>Later i saw in google groups the "solution" I quote Joshua Smith</p> <h2>"I've found that you need to restart the local dev environment before </h2> <h2>doing any inbound mail testing. Otherwise...
0
2009-12-08T17:33:55Z
[ "python", "google-app-engine", "email" ]
Django 1.1.1: How should I store an empty IP address using PostgreSQL?
1,862,123
<p>I am writing a Django application that stores IP addresses with optional routing information. One of the fields for the IP model I have created is <code>nexthop</code> (for next-hop routes), which will usually be empty. Originally we intended to use MySQL, but now project requirements have changed to use PostgreSQ...
3
2009-12-07T18:59:15Z
1,862,225
<p>If you can convince the devs to accept one of the patches, I'd say just run a patched copy of Django until the patched version lands. If not, then it might be less headache to just use a sentinel value, as you suggested, even though it is a hack. You might also just use a regular <code>CharField</code> instead of an...
2
2009-12-07T19:14:57Z
[ "python", "django", "postgresql" ]
Django 1.1.1: How should I store an empty IP address using PostgreSQL?
1,862,123
<p>I am writing a Django application that stores IP addresses with optional routing information. One of the fields for the IP model I have created is <code>nexthop</code> (for next-hop routes), which will usually be empty. Originally we intended to use MySQL, but now project requirements have changed to use PostgreSQ...
3
2009-12-07T18:59:15Z
1,862,264
<p>Have you tried just using <code>blank=True</code> for <code>nexthop</code>, rather than both <code>blank=True</code> and <code>null=True</code>? As noted in the <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#null" rel="nofollow">Django docs</a>:</p> <blockquote> <p>Only use null=True for non-str...
1
2009-12-07T19:19:30Z
[ "python", "django", "postgresql" ]
Django 1.1.1: How should I store an empty IP address using PostgreSQL?
1,862,123
<p>I am writing a Django application that stores IP addresses with optional routing information. One of the fields for the IP model I have created is <code>nexthop</code> (for next-hop routes), which will usually be empty. Originally we intended to use MySQL, but now project requirements have changed to use PostgreSQ...
3
2009-12-07T18:59:15Z
2,023,883
<p>And what about using <strong>empty string</strong> as sentinel, instead of 255.255.255.255 ?</p> <p>As admin back-end stores empty field as empty string (when <code>blank=true</code>), this solution has the advantage to be transparent for the user and doesn't force him to use a faked value...</p>
0
2010-01-07T21:40:38Z
[ "python", "django", "postgresql" ]
Regular Expression search/replace help needed, Python
1,862,782
<p>One rule that I need is that if the last vowel (aeiou) of a string is before a character from the set ('t','k','s','tk'), then a <code>:</code> needs to be added right after the vowel.</p> <p>So, in Python if I have the string <code>"orchestras"</code> I need a rule that will turn it into <code>"orchestra:s"</code>...
1
2009-12-07T20:47:07Z
1,862,794
<pre><code>re.sub(r"([aeiou])(t|k|s|tk)([^aeiou]*)$", r"\1:\2\3", "orchestras") re.sub(r"([aeiou])(t|k|s|tk)$", r"\1:\2", "orchestras") </code></pre> <p>You don't say if there can be other consonants after the t/k/s/tk. The first regex allows for this as long as there aren't any more vowels, so it'll chan...
6
2009-12-07T20:49:31Z
[ "python", "regex" ]
Regular Expression search/replace help needed, Python
1,862,782
<p>One rule that I need is that if the last vowel (aeiou) of a string is before a character from the set ('t','k','s','tk'), then a <code>:</code> needs to be added right after the vowel.</p> <p>So, in Python if I have the string <code>"orchestras"</code> I need a rule that will turn it into <code>"orchestra:s"</code>...
1
2009-12-07T20:47:07Z
1,864,066
<p>If you have not figured it out yet, I recommend trying <strong>[python_root]/tools/scripts/redemo.py</strong> It is a nice testing area.</p>
0
2009-12-08T01:39:40Z
[ "python", "regex" ]
Regular Expression search/replace help needed, Python
1,862,782
<p>One rule that I need is that if the last vowel (aeiou) of a string is before a character from the set ('t','k','s','tk'), then a <code>:</code> needs to be added right after the vowel.</p> <p>So, in Python if I have the string <code>"orchestras"</code> I need a rule that will turn it into <code>"orchestra:s"</code>...
1
2009-12-07T20:47:07Z
1,964,110
<p>Another take on the replacement regex:</p> <pre><code>re.sub("(?&lt;=[aeiou])(?=(?:t|k|s|tk)$)", ":", "orchestras") </code></pre> <p>This one does not need to replace using remembered groups.</p>
0
2009-12-26T19:17:25Z
[ "python", "regex" ]
How to use Emacs with Python
1,862,901
<p>I am new to emacs and I want to use emacs for python development. I am using Ubuntu 9.10. I frustrated to getting emacs work with python. I use GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0).</p> <p>Here what I did. * </p> <p>Emacs come with python mode but it is confusing there are two types of mo...
14
2009-12-07T21:09:35Z
1,862,926
<p>I'm not really sure you had to do anything fancy to get Python development to work. On gNewSense deltah (fork of Ubuntu 8.04) all I did was edit a .py file with the first line being:</p> <p><code>#!/usr/bin/python</code></p> <p>And then Emacs just figures it out and gives you python mode options. I didn't have to ...
0
2009-12-07T21:14:38Z
[ "python", "emacs" ]
How to use Emacs with Python
1,862,901
<p>I am new to emacs and I want to use emacs for python development. I am using Ubuntu 9.10. I frustrated to getting emacs work with python. I use GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0).</p> <p>Here what I did. * </p> <p>Emacs come with python mode but it is confusing there are two types of mo...
14
2009-12-07T21:09:35Z
1,862,990
<p>Emacs worked out of the box for me on Ubuntu 9.10.</p> <p>Did you try <code>C-c TAB</code> (update imports) before trying code completion? I don't think it work unless you do that.</p>
0
2009-12-07T21:24:19Z
[ "python", "emacs" ]
How to use Emacs with Python
1,862,901
<p>I am new to emacs and I want to use emacs for python development. I am using Ubuntu 9.10. I frustrated to getting emacs work with python. I use GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0).</p> <p>Here what I did. * </p> <p>Emacs come with python mode but it is confusing there are two types of mo...
14
2009-12-07T21:09:35Z
1,863,011
<p>I think you <em>do</em> want the package <code>python-mode</code> installed! The ropemacs variants appears to be for refactoring only, and pymacs is allows Python as an Emacs-extension language -- neither of which is what you need for standard support.</p>
2
2009-12-07T21:26:52Z
[ "python", "emacs" ]
How to use Emacs with Python
1,862,901
<p>I am new to emacs and I want to use emacs for python development. I am using Ubuntu 9.10. I frustrated to getting emacs work with python. I use GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0).</p> <p>Here what I did. * </p> <p>Emacs come with python mode but it is confusing there are two types of mo...
14
2009-12-07T21:09:35Z
1,863,018
<p>I haven't tried anything, and I haven't had much luck with rope (giant source tree causes my emacs to hang upon any file save). Instead, I find the default completion works well enough for my purposes.</p> <p>The default completion keybinding is <code>M-/</code>. That runs dabbrev-expand which expands the current...
3
2009-12-07T21:27:55Z
[ "python", "emacs" ]
running a method within another method. python
1,862,945
<p>I am calling a method within another. and the error for this script i am getting is </p> <pre><code>NameError: name 'mnDialog' is not defined </code></pre> <p>Is there a reason for it? I think it has something to do with executing a command which isn't on the global level. (i didn't have the impression that python...
1
2009-12-07T21:17:48Z
1,862,972
<p><code>mnDialog</code> is a local variable in <code>mnProgRun</code>. It is not accessible outside the function scope. If you want it to be, define it at the appropriate scope.</p> <blockquote> <p>(i didn't have the impression that python has a global and local variable declaration.)</p> </blockquote> <p>You ha...
1
2009-12-07T21:21:56Z
[ "python", "methods", "command", "maya" ]
running a method within another method. python
1,862,945
<p>I am calling a method within another. and the error for this script i am getting is </p> <pre><code>NameError: name 'mnDialog' is not defined </code></pre> <p>Is there a reason for it? I think it has something to do with executing a command which isn't on the global level. (i didn't have the impression that python...
1
2009-12-07T21:17:48Z
1,862,979
<p>You should define <code>mnDialog</code> at the top level. It is not in the correct namespace.</p> <p>Also, it's (almost) always unnecessarily complicating to nest functions in Python.</p>
1
2009-12-07T21:22:09Z
[ "python", "methods", "command", "maya" ]
running a method within another method. python
1,862,945
<p>I am calling a method within another. and the error for this script i am getting is </p> <pre><code>NameError: name 'mnDialog' is not defined </code></pre> <p>Is there a reason for it? I think it has something to do with executing a command which isn't on the global level. (i didn't have the impression that python...
1
2009-12-07T21:17:48Z
1,863,005
<p>The problem is that the <code>mnDialog</code> is not being looked up from <code>mnMakeWin</code>, you are passing the name and it gets looked up later when you are not in the correct scope.</p> <p>It may work to pass the function in instead of the name. I don't have maya installed, so I can't try it.</p> <p>Otherw...
2
2009-12-07T21:26:22Z
[ "python", "methods", "command", "maya" ]
running a method within another method. python
1,862,945
<p>I am calling a method within another. and the error for this script i am getting is </p> <pre><code>NameError: name 'mnDialog' is not defined </code></pre> <p>Is there a reason for it? I think it has something to do with executing a command which isn't on the global level. (i didn't have the impression that python...
1
2009-12-07T21:17:48Z
1,865,368
<p>maya always have problems with scoops, you may define mnDialog() and mnMakeWin() outside the function, at the top scoop level, its maya problem not from python, as i faced problem when calling class methods from maya ui command (ex button event).</p> <p>hope that will help you :)</p> <p>##edit</p> <p>import maya....
0
2009-12-08T07:53:47Z
[ "python", "methods", "command", "maya" ]
Python: How do I read and parse a unicode utf-8 text file?
1,862,963
<p>I am exporting UTF-8 text from Excel and I want to read and parse the incoming data using Python. I've read all the online info so I've already tried this, for example:</p> <pre><code> txtFile = codecs.open( 'halout.txt', 'r', 'utf-8' ) for line in txtFile: print repr( line ) </code></pre> <p>The error I am get...
3
2009-12-07T21:20:04Z
1,862,973
<p>That file is not UTF-8; it's <a href="http://unicode.org/faq/utf%5Fbom.html#BOM" rel="nofollow">UTF-16LE with a byte-order marker</a>.</p>
5
2009-12-07T21:21:58Z
[ "python", "xml", "unicode", "parsing", "codec" ]
Python: How do I read and parse a unicode utf-8 text file?
1,862,963
<p>I am exporting UTF-8 text from Excel and I want to read and parse the incoming data using Python. I've read all the online info so I've already tried this, for example:</p> <pre><code> txtFile = codecs.open( 'halout.txt', 'r', 'utf-8' ) for line in txtFile: print repr( line ) </code></pre> <p>The error I am get...
3
2009-12-07T21:20:04Z
1,862,980
<p>That is a <a href="http://en.wikipedia.org/wiki/Byte-order%5Fmark" rel="nofollow">BOM</a></p> <p>EDIT, from the coments, it seems to be a utf-16 bom</p> <pre><code>codecs.open('foo.txt', 'r', 'utf-16') </code></pre> <p>should work.</p>
2
2009-12-07T21:22:11Z
[ "python", "xml", "unicode", "parsing", "codec" ]
Python: How do I read and parse a unicode utf-8 text file?
1,862,963
<p>I am exporting UTF-8 text from Excel and I want to read and parse the incoming data using Python. I've read all the online info so I've already tried this, for example:</p> <pre><code> txtFile = codecs.open( 'halout.txt', 'r', 'utf-8' ) for line in txtFile: print repr( line ) </code></pre> <p>The error I am get...
3
2009-12-07T21:20:04Z
1,863,054
<p>Expanding on Johnathan's comment, this code should read the file correctly:</p> <pre><code>import codecs txtFile = codecs.open( 'halout.txt', 'r', 'utf-16' ) for line in txtFile: print repr( line ) </code></pre>
1
2009-12-07T21:33:30Z
[ "python", "xml", "unicode", "parsing", "codec" ]
Python: How do I read and parse a unicode utf-8 text file?
1,862,963
<p>I am exporting UTF-8 text from Excel and I want to read and parse the incoming data using Python. I've read all the online info so I've already tried this, for example:</p> <pre><code> txtFile = codecs.open( 'halout.txt', 'r', 'utf-8' ) for line in txtFile: print repr( line ) </code></pre> <p>The error I am get...
3
2009-12-07T21:20:04Z
1,863,808
<p>Try to see if the excel file has some blank rows (and then has values again), that might cause the unexpected error. </p>
0
2009-12-08T00:27:50Z
[ "python", "xml", "unicode", "parsing", "codec" ]
grep -r in python
1,863,236
<p>i'd like to implement the unix command 'grep -r' in a python function. i know about commands.getstatusoutput(), but for now i don't want to use that. i came up with this:</p> <pre><code>def grep_r (str, dir): files = [ o[0]+"/"+f for o in os.walk(dir) for f in o[2] if os.path.isfile(o[0]+"/"+f) ] return [...
3
2009-12-07T22:03:01Z
1,863,270
<p>re.match only checks the beginning of the string.</p> <p>Use re.search()</p> <p>From <a href="http://docs.python.org/library/re.html">the docs</a>:</p> <blockquote> <p>Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the strin...
5
2009-12-07T22:08:33Z
[ "python", "grep" ]
grep -r in python
1,863,236
<p>i'd like to implement the unix command 'grep -r' in a python function. i know about commands.getstatusoutput(), but for now i don't want to use that. i came up with this:</p> <pre><code>def grep_r (str, dir): files = [ o[0]+"/"+f for o in os.walk(dir) for f in o[2] if os.path.isfile(o[0]+"/"+f) ] return [...
3
2009-12-07T22:03:01Z
1,863,274
<p>You might want to <code>search()</code> instead of <code>match()</code> to catch matches in the middle of lines, as noted in <a href="http://docs.python.org/library/re.html#matching-vs-searching">http://docs.python.org/library/re.html#matching-vs-searching</a></p> <p>Also, the structure and intent of your code is q...
7
2009-12-07T22:09:51Z
[ "python", "grep" ]
grep -r in python
1,863,236
<p>i'd like to implement the unix command 'grep -r' in a python function. i know about commands.getstatusoutput(), but for now i don't want to use that. i came up with this:</p> <pre><code>def grep_r (str, dir): files = [ o[0]+"/"+f for o in os.walk(dir) for f in o[2] if os.path.isfile(o[0]+"/"+f) ] return [...
3
2009-12-07T22:03:01Z
1,863,286
<pre><code>import os, re def grep_r(regex, dir): for root, dirs, files in os.walk(dir): for f in files: for m in grep(regex, os.path.join(root, f)): yield m def grep(regex, filename): for i, line in enumerate(open(filename)): if re.match(regex, line): # or re.search...
2
2009-12-07T22:12:37Z
[ "python", "grep" ]
grep -r in python
1,863,236
<p>i'd like to implement the unix command 'grep -r' in a python function. i know about commands.getstatusoutput(), but for now i don't want to use that. i came up with this:</p> <pre><code>def grep_r (str, dir): files = [ o[0]+"/"+f for o in os.walk(dir) for f in o[2] if os.path.isfile(o[0]+"/"+f) ] return [...
3
2009-12-07T22:03:01Z
1,863,830
<p>why do you need to use regex?</p> <pre><code>path=os.path.join("/dir1","dir2","dir3") pattern="test" for r,d,f in os.walk(path): for files in f: for n,line in enumerate(open( os.path.join(r,files) ) ): if pattern in line: print "%s found in line: %d of file: %s" %(pattern, n+...
1
2009-12-08T00:37:49Z
[ "python", "grep" ]
grep -r in python
1,863,236
<p>i'd like to implement the unix command 'grep -r' in a python function. i know about commands.getstatusoutput(), but for now i don't want to use that. i came up with this:</p> <pre><code>def grep_r (str, dir): files = [ o[0]+"/"+f for o in os.walk(dir) for f in o[2] if os.path.isfile(o[0]+"/"+f) ] return [...
3
2009-12-07T22:03:01Z
1,864,383
<p>Put all this code into a file called pygrep and chmod +x pygrep:</p> <pre><code>#!/usr/bin/python import os import re import sys def file_match(fname, pat): try: f = open(fname, "rt") except IOError: return for i, line in enumerate(f): if pat.search(line): print "%s...
3
2009-12-08T03:31:06Z
[ "python", "grep" ]
Python elevator simulation problem
1,863,303
<p>I have a homework assignment that's really baking my noodle. It involves an elevator simulation that takes user inputs for the number of floors and the number of people using the elevator. the people's starting floor and destination floors are random numbers within the floors. </p> <p>I realize that my code is ver...
2
2009-12-07T22:16:20Z
1,863,327
<ul> <li>You need to understand the <code>self</code> parameter to all methods.</li> <li>You need to understand <code>__init__</code>, the constructor.</li> <li>You need to understand <code>self.varible</code> for your member variables.</li> <li>You need to understand how to setup a <code>main</code> function.</li> <li...
6
2009-12-07T22:22:10Z
[ "python", "oop" ]
Python elevator simulation problem
1,863,303
<p>I have a homework assignment that's really baking my noodle. It involves an elevator simulation that takes user inputs for the number of floors and the number of people using the elevator. the people's starting floor and destination floors are random numbers within the floors. </p> <p>I realize that my code is ver...
2
2009-12-07T22:16:20Z
1,863,337
<p>Maybe your building class should start like this.</p> <pre><code>class building: def __init__(self, floors, customers): self.num_of_floors = floors self.customer_list = customers self.elevator = elevator() </code></pre>
3
2009-12-07T22:25:25Z
[ "python", "oop" ]
Python elevator simulation problem
1,863,303
<p>I have a homework assignment that's really baking my noodle. It involves an elevator simulation that takes user inputs for the number of floors and the number of people using the elevator. the people's starting floor and destination floors are random numbers within the floors. </p> <p>I realize that my code is ver...
2
2009-12-07T22:16:20Z
1,863,346
<p>You should definately spend some time on <a href="http://docs.python.org/tutorial/" rel="nofollow">Python Tutorial</a> or <a href="http://www.diveintopython.org/" rel="nofollow">Dive into Python</a>.</p>
1
2009-12-07T22:26:12Z
[ "python", "oop" ]
Python elevator simulation problem
1,863,303
<p>I have a homework assignment that's really baking my noodle. It involves an elevator simulation that takes user inputs for the number of floors and the number of people using the elevator. the people's starting floor and destination floors are random numbers within the floors. </p> <p>I realize that my code is ver...
2
2009-12-07T22:16:20Z
1,863,383
<p>The first parameter of every method is a reference to the object and is usually called self. You need it to reference instancemembers of an object.</p> <p>Second, referencing global variables from inside a class is considered a bad idea. You can better pass them to a class via the constructor or parameters.</p>
0
2009-12-07T22:33:47Z
[ "python", "oop" ]
Problem with import curses.ascii
1,863,473
<p>I am trying <code>from curses.ascii import *</code> to django project, but I get: <code>No module named _curses</code>, I am using Python 2.5, any suggestion? Anyway I only need <code>isalpha()</code> function to use....</p>
0
2009-12-07T22:49:13Z
1,863,501
<p>You didn't say which platform you are on, but there is probably a package which will install the curses bindings for you.</p> <p>In debian/ubuntu for example it is part of the default python install</p> <p>If you built the Python yourself, you may be missing the libcurses-dev</p> <p>If you are on windows maybe ch...
3
2009-12-07T22:53:51Z
[ "python", "windows", "django", "curses" ]
Working with bit streams
1,863,571
<p>I have a base64 encoded bit stream, I want to work with. After decoding it with <code>base64.b64decode</code> I get a bytes object (Py3k btw) containing the decoded code. The problem is now, that I would like to work on that bytes object with bit-wise operations, such as shifting, bit wise <code>and</code> etc, but ...
3
2009-12-07T23:13:25Z
1,863,589
<p>try using</p> <pre><code>list(bytestring) </code></pre> <p>Eg.</p> <pre><code>&gt;&gt;&gt; bstring=b"Hello World" &gt;&gt;&gt; list( bstring) [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] &gt;&gt;&gt; </code></pre> <p>If you want one huge bitfield instead of all those octets</p> <pre><code>&gt;&gt;&gt; f...
4
2009-12-07T23:17:34Z
[ "python", "python-3.x" ]
Working with bit streams
1,863,571
<p>I have a base64 encoded bit stream, I want to work with. After decoding it with <code>base64.b64decode</code> I get a bytes object (Py3k btw) containing the decoded code. The problem is now, that I would like to work on that bytes object with bit-wise operations, such as shifting, bit wise <code>and</code> etc, but ...
3
2009-12-07T23:13:25Z
1,863,786
<p>If you're using Python 2.x, you could try using <a href="http://construct.wikispaces.com/" rel="nofollow">Construct</a>. It can do very elegant parsing of data, including <a href="http://construct.wikispaces.com/tut-bitwise" rel="nofollow">bit data</a>.</p> <p>It hasn't had so much active development recently, so I...
0
2009-12-08T00:18:38Z
[ "python", "python-3.x" ]
Working with bit streams
1,863,571
<p>I have a base64 encoded bit stream, I want to work with. After decoding it with <code>base64.b64decode</code> I get a bytes object (Py3k btw) containing the decoded code. The problem is now, that I would like to work on that bytes object with bit-wise operations, such as shifting, bit wise <code>and</code> etc, but ...
3
2009-12-07T23:13:25Z
1,865,854
<p>If you need to make your <code>bytes</code> object mutable then you can construct a <code>bytearray</code> from it:</p> <pre><code>mutable = bytearray(b"immutable") </code></pre> <p>This will let you modify the individual bytes through normal assignments</p> <pre><code>mutable[0] = mutable[1] = 32 </code></pre> ...
2
2009-12-08T09:44:10Z
[ "python", "python-3.x" ]
Reasons for this disparity in execution speed?
1,863,724
<p>I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes.</p> <p>The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes.</p> <p>I...
7
2009-12-07T23:57:48Z
1,863,738
<p>It could be because dicts in Python are much faster than hashes in Ruby</p> <p>I've just run a quick test, building a hash of 12345678 item in Ruby1.8.7 took 3 times as long as Python. Ruby1.9 was about twice as long as Python.</p> <p>Here is how I tested<br> <strong>python</strong> </p> <pre><code>$ time python...
5
2009-12-08T00:02:42Z
[ "python", "ruby" ]
Reasons for this disparity in execution speed?
1,863,724
<p>I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes.</p> <p>The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes.</p> <p>I...
7
2009-12-07T23:57:48Z
1,863,758
<p>On the python side, you could iterate over the dictionary items like this:</p> <pre><code>for key, value in hashes.iteritems(): if value != 0: print "Uneven hash count: %s" % key num_errors += 1 </code></pre> <p>Also:</p> <pre><code>for line in f.readlines(): hashes[line] = hashes.setdefau...
2
2009-12-08T00:10:29Z
[ "python", "ruby" ]
Reasons for this disparity in execution speed?
1,863,724
<p>I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes.</p> <p>The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes.</p> <p>I...
7
2009-12-07T23:57:48Z
1,863,759
<p>I've found Ruby's reference implementation (well, Ruby) to be (unscientifically stated) dog slow.</p> <p>If you have the opportunity, please try running your program under JRuby! Charles Nutter and other Sun folks claim to have sped Ruby up dramatically.</p> <p>I for one would be most interested in your results.</...
5
2009-12-08T00:10:41Z
[ "python", "ruby" ]
Reasons for this disparity in execution speed?
1,863,724
<p>I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes.</p> <p>The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes.</p> <p>I...
7
2009-12-07T23:57:48Z
1,863,847
<p>I'm not a Ruby expert, so someone please correct me if I'm wrong:</p> <p>I see a small optimization potential.</p> <p>If you say</p> <pre><code>hashes = hash.new(0) </code></pre> <p>then a reference to an undefined hash will return 0 and store the key; and you can do</p> <pre><code>hashes[line] += 1 </code></pr...
0
2009-12-08T00:43:20Z
[ "python", "ruby" ]
Reasons for this disparity in execution speed?
1,863,724
<p>I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes.</p> <p>The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes.</p> <p>I...
7
2009-12-07T23:57:48Z
1,864,891
<p>Python's dictionary is brutally fast. See <a href="http://stackoverflow.com/questions/327311/how-are-pythons-built-in-dictionaries-implemented">http://stackoverflow.com/questions/327311/how-are-pythons-built-in-dictionaries-implemented</a> Perhaps Ruby's is not so crash hot.</p> <p>I doubt it's the hash functions. ...
0
2009-12-08T06:00:13Z
[ "python", "ruby" ]
Reasons for this disparity in execution speed?
1,863,724
<p>I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes.</p> <p>The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes.</p> <p>I...
7
2009-12-07T23:57:48Z
1,866,869
<p>I was able to speed up your ruby code a bit as follows:</p> <pre><code>require 'benchmark' Benchmark.bm(10) do |x| x.report("original version") do file = File.open("c:\\file1.txt", "r") hashes = {} file.each_line { |line| if hashes.has_key?(line) hashes[line] += 1 else h...
0
2009-12-08T13:08:55Z
[ "python", "ruby" ]
Reasons for this disparity in execution speed?
1,863,724
<p>I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes.</p> <p>The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes.</p> <p>I...
7
2009-12-07T23:57:48Z
5,194,387
<p>I'd bet results of Ruby 1.9.x, which is faster or on par with Python in most areas, are caused by an additional overhead required by hashes/dictionaries implementation because the are <b>ordered</b> in Ruby contrary to Python.</p>
0
2011-03-04T13:33:22Z
[ "python", "ruby" ]
Reasons for this disparity in execution speed?
1,863,724
<p>I wrote a quick Python script to compare two files, each containing unordered hashes, in order to verify that both files are identical aside from order. Then I rewrote it in Ruby for educational purposes.</p> <p>The Python implementation takes seconds, while the Ruby implementation takes around 4 minutes.</p> <p>I...
7
2009-12-07T23:57:48Z
5,213,807
<p>I'll try to do a benchmark in my copious free time, but try using <code>group_by</code>. It's not only more like functional programming, but I've found it to be a lot faster than the procedural version in MRI.</p> <pre><code>def convert_to_hash(file) values_hash = file.each_line.group_by {|line| line} # Hash.[]...
0
2011-03-06T22:12:21Z
[ "python", "ruby" ]
Using Python Yacc\Lex as a formula parser
1,863,824
<p>At the moment i'm working on using the python implementation of Yacc/Lex to build a formula parser for converting strings of formulae into a set of class defined operands. So far i've been mostly successful but i've come to an empasse in defining the parsing rules due to ambiguity with parentheses and several shift/...
0
2009-12-08T00:33:37Z
1,863,912
<p>START SMALL!!! Regardless of the parser library you end up using, try doing just a simple binary operation like expr &amp; expr, and get that working. Then add support for '|'. Now you have two different operators, and you have enough to represent precedence of operations, and parentheses actually play a part. T...
2
2009-12-08T01:03:21Z
[ "python", "yacc" ]
Using Python Yacc\Lex as a formula parser
1,863,824
<p>At the moment i'm working on using the python implementation of Yacc/Lex to build a formula parser for converting strings of formulae into a set of class defined operands. So far i've been mostly successful but i've come to an empasse in defining the parsing rules due to ambiguity with parentheses and several shift/...
0
2009-12-08T00:33:37Z
1,898,819
<p>Parsers <em>are</em> frustrating, and your notation is a non-trivial one. Creating a parser for infix notation takes a certain mind set. But if this is a core part of your system, you'll have to get it working some time. I'm not sure what was your confusion with lepl, I believe it is fairly similar in concept to py...
2
2009-12-14T03:55:38Z
[ "python", "yacc" ]
WMD Preview Doesn't Match Output
1,864,081
<p>I am using WMD in a google app situation whereby the site administrator can update the pages of the site and the users see the information. </p> <p>The preview function is working fine and I can see the text the way I want it to appear, but when I am in the users section, the markdown is being returned without the ...
0
2009-12-08T01:43:27Z
1,865,706
<p>This doesn't appear to have anything to do with WMD.js, which is an editor and has nothing to do with displaying the content.</p> <p>You don't post your models, but it looks like you are entering content into the "markdown" field, but displaying a different field, "html". I presume you have something in your models...
1
2009-12-08T09:13:11Z
[ "python", "django", "google-app-engine", "markdown", "wmd" ]
WMD Preview Doesn't Match Output
1,864,081
<p>I am using WMD in a google app situation whereby the site administrator can update the pages of the site and the users see the information. </p> <p>The preview function is working fine and I can see the text the way I want it to appear, but when I am in the users section, the markdown is being returned without the ...
0
2009-12-08T01:43:27Z
1,871,093
<p>This is my models.py file </p> <pre><code># models.py from google.appengine.ext import db class GoogleToken(db.Model): session_token = db.StringProperty() scope_url = db.StringProperty() added_on = db.DateTimeProperty(auto_now_add=True) class PageContent(db.Model): html = db.TextProperty() pag...
0
2009-12-09T01:33:59Z
[ "python", "django", "google-app-engine", "markdown", "wmd" ]
WMD Preview Doesn't Match Output
1,864,081
<p>I am using WMD in a google app situation whereby the site administrator can update the pages of the site and the users see the information. </p> <p>The preview function is working fine and I can see the text the way I want it to appear, but when I am in the users section, the markdown is being returned without the ...
0
2009-12-08T01:43:27Z
1,883,132
<p>The reason this is happening is because the Django Form is only seeing the value of the <code>&lt;textarea&gt;</code> tag that represents WMD editor. That value is the actual markdown, not the rendered HTML that you see in the preview.</p> <p>There are several ways to fix this, on either the client or the server.....
2
2009-12-10T18:44:00Z
[ "python", "django", "google-app-engine", "markdown", "wmd" ]
How to deal with deflated response by urllib2?
1,864,191
<p>I currently use following code to decompress gzipped response by urllib2:</p> <pre><code>opener = urllib2.build_opener() response = opener.open(req) data = response.read() if response.headers.get('content-encoding', '') == 'gzip': data = StringIO.StringIO(data) gzipper = gzip.GzipFile(fileobj=data) html...
3
2009-12-08T02:16:48Z
1,864,217
<p>You need a slightly different approach, see <a href="http://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations">this SO thread</a>.</p>
2
2009-12-08T02:26:26Z
[ "python", "gzip", "urllib2", "deflate" ]
How to deal with deflated response by urllib2?
1,864,191
<p>I currently use following code to decompress gzipped response by urllib2:</p> <pre><code>opener = urllib2.build_opener() response = opener.open(req) data = response.read() if response.headers.get('content-encoding', '') == 'gzip': data = StringIO.StringIO(data) gzipper = gzip.GzipFile(fileobj=data) html...
3
2009-12-08T02:16:48Z
1,864,938
<p>To answer from above comment, the HTTP spec (<a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3" rel="nofollow">http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3</a>) says:</p> <blockquote> <p>If no Accept-Encoding field is present in a request, the server MAY assume that the cli...
0
2009-12-08T06:10:58Z
[ "python", "gzip", "urllib2", "deflate" ]
How to deal with deflated response by urllib2?
1,864,191
<p>I currently use following code to decompress gzipped response by urllib2:</p> <pre><code>opener = urllib2.build_opener() response = opener.open(req) data = response.read() if response.headers.get('content-encoding', '') == 'gzip': data = StringIO.StringIO(data) gzipper = gzip.GzipFile(fileobj=data) html...
3
2009-12-08T02:16:48Z
4,038,918
<p>There is a better way outlined at: </p> <ul> <li><a href="http://rationalpie.wordpress.com/2010/06/02/python-streaming-gzip-decompression/" rel="nofollow">http://rationalpie.wordpress.com/2010/06/02/python-streaming-gzip-decompression/</a></li> </ul> <p>The author explains how to decompress chunk by chunk, rather...
4
2010-10-28T01:01:37Z
[ "python", "gzip", "urllib2", "deflate" ]
How to deal with deflated response by urllib2?
1,864,191
<p>I currently use following code to decompress gzipped response by urllib2:</p> <pre><code>opener = urllib2.build_opener() response = opener.open(req) data = response.read() if response.headers.get('content-encoding', '') == 'gzip': data = StringIO.StringIO(data) gzipper = gzip.GzipFile(fileobj=data) html...
3
2009-12-08T02:16:48Z
9,583,564
<p>You can try</p> <pre><code>if response.headers.get('content-encoding', '') == 'deflate': html = zlib.decompress(response.read()) </code></pre> <p>if fail, here is another way, I found it in <a href="https://github.com/kennethreitz/requests/blob/develop/requests/utils.py#L382" rel="nofollow">requests source cod...
4
2012-03-06T12:18:16Z
[ "python", "gzip", "urllib2", "deflate" ]
How to deal with deflated response by urllib2?
1,864,191
<p>I currently use following code to decompress gzipped response by urllib2:</p> <pre><code>opener = urllib2.build_opener() response = opener.open(req) data = response.read() if response.headers.get('content-encoding', '') == 'gzip': data = StringIO.StringIO(data) gzipper = gzip.GzipFile(fileobj=data) html...
3
2009-12-08T02:16:48Z
37,633,573
<p>you can see the code in <a href="https://github.com/shazow/urllib3/blob/master/urllib3/response.py" rel="nofollow">urllib3</a></p> <pre class="lang-python prettyprint-override"><code>class DeflateDecoder(object): def __init__(self): self._first_try = True self._data = binary_type() self...
1
2016-06-04T18:18:19Z
[ "python", "gzip", "urllib2", "deflate" ]
Python Selector (URL routing library), experience/opinions?
1,864,393
<p>Does anyone have opinions about or experience with <a href="https://github.com/lukearno/selector" rel="nofollow">Python Selector</a>? It looks great, but I'm a bit put off by its "Alpha" status on pypi and lack of unit tests.</p> <p>I mostly like that its simple, self contained, and pure WSGI. All other url route...
8
2009-12-08T03:33:34Z
1,864,468
<p>Have you looked at <a href="http://werkzeug.pocoo.org/documentation/dev/routing.html">werkzeug.routing</a>? It's hard to find anything that's simpler, more self-contained, or purer-WSGI than Werkzeug, in general -- I'm quite a fan of it!-)</p>
6
2009-12-08T03:57:30Z
[ "python", "url-routing", "selector", "wsgi" ]
Python Selector (URL routing library), experience/opinions?
1,864,393
<p>Does anyone have opinions about or experience with <a href="https://github.com/lukearno/selector" rel="nofollow">Python Selector</a>? It looks great, but I'm a bit put off by its "Alpha" status on pypi and lack of unit tests.</p> <p>I mostly like that its simple, self contained, and pure WSGI. All other url route...
8
2009-12-08T03:33:34Z
1,867,590
<p>I've used Selector for the last couple years and found it perfectly stable. It's been at 0.8.11 for at least two years now. </p> <p>I would draw two conclusions from that: </p> <ol> <li><p>It could be basically unmaintained. If you find a bug in it or need a new feature, I wouldn't count on being able to get Luke ...
8
2009-12-08T15:09:57Z
[ "python", "url-routing", "selector", "wsgi" ]
Re-format items inside list read from CSV file in Python
1,864,422
<p>I have some lines in a CSV file like this:</p> <pre><code>1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2 </code></pre> <p>If you notice, some numbers are enclosed in " " and has a thousand separator ",". I want to remove the thousand separator and the d...
1
2009-12-08T03:41:00Z
1,864,438
<p>You could simply parse the CSV, make the necessary changes and then write it again.</p> <p>(I haven't tested this code but it should be something like this)</p> <pre><code>import csv reader = csv.reader(open('IN.csv', 'r')) writer = csv.writer(open('OUT.csv', 'w') for row in reader: # do stuff to the row here # ...
2
2009-12-08T03:48:48Z
[ "python", "parsing", "csv", "replace", "delimiter" ]
Re-format items inside list read from CSV file in Python
1,864,422
<p>I have some lines in a CSV file like this:</p> <pre><code>1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2 </code></pre> <p>If you notice, some numbers are enclosed in " " and has a thousand separator ",". I want to remove the thousand separator and the d...
1
2009-12-08T03:41:00Z
1,864,439
<p>If all you want is to remove double quotes and commas from a string, a couple of replaces will do it:</p> <pre><code>s = s.replace('"','').replace(',','') </code></pre> <p>A faster way is to use <code>s.translate</code>, but that requires a minimum of preparation:</p> <pre><code>import string identity = string.ma...
1
2009-12-08T03:49:00Z
[ "python", "parsing", "csv", "replace", "delimiter" ]
Re-format items inside list read from CSV file in Python
1,864,422
<p>I have some lines in a CSV file like this:</p> <pre><code>1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2 </code></pre> <p>If you notice, some numbers are enclosed in " " and has a thousand separator ",". I want to remove the thousand separator and the d...
1
2009-12-08T03:41:00Z
1,864,504
<p>Here is a bit of regular expression fiddling that will do the trick:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; p = re.compile('["]([^"]*)["]') &gt;&gt;&gt; x = """1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2""" &gt;&gt;&gt; p.sub(lambda m: m.g...
2
2009-12-08T04:06:17Z
[ "python", "parsing", "csv", "replace", "delimiter" ]
Re-format items inside list read from CSV file in Python
1,864,422
<p>I have some lines in a CSV file like this:</p> <pre><code>1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2 </code></pre> <p>If you notice, some numbers are enclosed in " " and has a thousand separator ",". I want to remove the thousand separator and the d...
1
2009-12-08T03:41:00Z
1,864,507
<p>Here is something I just tested, you may not need pprint, I just want to use for clear output.</p> <p>test.csv</p> <pre><code>1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2 1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.0...
1
2009-12-08T04:07:37Z
[ "python", "parsing", "csv", "replace", "delimiter" ]
Re-format items inside list read from CSV file in Python
1,864,422
<p>I have some lines in a CSV file like this:</p> <pre><code>1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2 </code></pre> <p>If you notice, some numbers are enclosed in " " and has a thousand separator ",". I want to remove the thousand separator and the d...
1
2009-12-08T03:41:00Z
1,864,800
<p>Use the <a href="http://docs.python.org/library/csv.html" rel="nofollow">csv</a> module. It has all sorts of constants and parameters to help you set the delimiters, quotes, and everything else for the type of file you are working with. It even has a Sniffer that can help you identify the csv format of the file. ...
1
2009-12-08T05:27:54Z
[ "python", "parsing", "csv", "replace", "delimiter" ]
Re-format items inside list read from CSV file in Python
1,864,422
<p>I have some lines in a CSV file like this:</p> <pre><code>1000001234,Account Name,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000.00",Some string,Some string 2 </code></pre> <p>If you notice, some numbers are enclosed in " " and has a thousand separator ",". I want to remove the thousand separator and the d...
1
2009-12-08T03:41:00Z
1,864,926
<p>You should absolutely use the <code>csv</code> module. If you use a <code>csv.reader</code>, you only have one very small problem: testing fields to see if they're numbers, and stripping commas if they are. I've packaged it as a generator:</p> <pre><code>import csv def read_and_fix_numbers(f): """Iterate ove...
1
2009-12-08T06:08:12Z
[ "python", "parsing", "csv", "replace", "delimiter" ]
Strip final 0 off a python string
1,864,613
<pre><code>#!/usr/bin/env python import os, sys, subprocess, time while True: print subprocess.call("xsel", shell=True); time.sleep(1); </code></pre> <p>Takes an entry from the clipboard and prints it, every 1 second.</p> <p>Result:</p> <pre><code>copied0 entry0 from0 clipboard0 </code></pre> <p>I do no...
2
2009-12-08T04:34:32Z
1,864,620
<p><code>"copied0".rstrip("0")</code> should work</p> <p>Actually, you better do like this, It wont show return code to the screen</p> <pre><code>import os, sys, subprocess, time while True: _ = subprocess.call("dir", shell=True); time.sleep(1); </code></pre>
2
2009-12-08T04:36:59Z
[ "python", "string", "integer", "subprocess", "popen" ]
Strip final 0 off a python string
1,864,613
<pre><code>#!/usr/bin/env python import os, sys, subprocess, time while True: print subprocess.call("xsel", shell=True); time.sleep(1); </code></pre> <p>Takes an entry from the clipboard and prints it, every 1 second.</p> <p>Result:</p> <pre><code>copied0 entry0 from0 clipboard0 </code></pre> <p>I do no...
2
2009-12-08T04:34:32Z
1,864,644
<p>It looks to me like it is running "xsel" which is printing its results to stdout, then printing the return code (0) to stdout. You are aren't getting the clip results from python.</p> <p>You probably want subprocess.popen and to capture stdout.</p>
2
2009-12-08T04:42:44Z
[ "python", "string", "integer", "subprocess", "popen" ]
Strip final 0 off a python string
1,864,613
<pre><code>#!/usr/bin/env python import os, sys, subprocess, time while True: print subprocess.call("xsel", shell=True); time.sleep(1); </code></pre> <p>Takes an entry from the clipboard and prints it, every 1 second.</p> <p>Result:</p> <pre><code>copied0 entry0 from0 clipboard0 </code></pre> <p>I do no...
2
2009-12-08T04:34:32Z
1,864,649
<p>If the zero is always at the end of the string, and so you simply always want the last character removed, just do <code>st=st[:-1]</code>.</p> <p>Or, if you are not sure that there will be a zero at the end, you can do <code>if st[-1]==0: st=st[:-1]</code>.</p>
1
2009-12-08T04:43:28Z
[ "python", "string", "integer", "subprocess", "popen" ]
Strip final 0 off a python string
1,864,613
<pre><code>#!/usr/bin/env python import os, sys, subprocess, time while True: print subprocess.call("xsel", shell=True); time.sleep(1); </code></pre> <p>Takes an entry from the clipboard and prints it, every 1 second.</p> <p>Result:</p> <pre><code>copied0 entry0 from0 clipboard0 </code></pre> <p>I do no...
2
2009-12-08T04:34:32Z
1,864,653
<p><strong>Edit</strong>: <code>subprocess.call</code> isn't returning a string, but an int -- that <code>0</code> you're seeing (after xsel's actual output). Use, instead:</p> <pre><code>print subprocess.Popen('xsel', stdout=subprocess.PIPE).communicate()[0] </code></pre>
4
2009-12-08T04:45:02Z
[ "python", "string", "integer", "subprocess", "popen" ]
Strip final 0 off a python string
1,864,613
<pre><code>#!/usr/bin/env python import os, sys, subprocess, time while True: print subprocess.call("xsel", shell=True); time.sleep(1); </code></pre> <p>Takes an entry from the clipboard and prints it, every 1 second.</p> <p>Result:</p> <pre><code>copied0 entry0 from0 clipboard0 </code></pre> <p>I do no...
2
2009-12-08T04:34:32Z
1,864,681
<p>The <code>0</code> and new line feed at each line are the only things printed by the python print command, where zero is the shell return code from <code>subprocess.call</code>. The shell itself first prints it results first to stdout, which is why you see the word.</p> <p>Edit: See the comments in S Mark's post fo...
2
2009-12-08T04:54:09Z
[ "python", "string", "integer", "subprocess", "popen" ]
Strip final 0 off a python string
1,864,613
<pre><code>#!/usr/bin/env python import os, sys, subprocess, time while True: print subprocess.call("xsel", shell=True); time.sleep(1); </code></pre> <p>Takes an entry from the clipboard and prints it, every 1 second.</p> <p>Result:</p> <pre><code>copied0 entry0 from0 clipboard0 </code></pre> <p>I do no...
2
2009-12-08T04:34:32Z
1,864,683
<p>As Mark pointed out, <code>subprocess.call()</code> does not do what you want</p> <p>Something like this should work</p> <pre><code>#!/usr/bin/env python import os, sys, subprocess, time while True: p=subprocess.Popen(["xsel"],stdout=subprocess.PIPE) print p.stdout.read() time.sleep(1) </code></pre>
4
2009-12-08T04:54:24Z
[ "python", "string", "integer", "subprocess", "popen" ]
Convert UTF-8 octets to unicode code points
1,864,701
<p>I have a set of UTF-8 octets and I need to convert them back to unicode code points. How can I do this in python.</p> <p>e.g. UTF-8 octet ['0xc5','0x81'] should be converted to 0x141 codepoint.</p>
6
2009-12-08T04:59:12Z
1,864,715
<p>I'm assuming pre-3.x...</p> <p>Put them in a str, and either call unicode with the string and 'utf-8':</p> <pre><code>&gt;&gt;&gt; unicode('\xc5\x81', 'utf-8') u'\u0141' </code></pre> <p>Or call <code>.decode('utf-8')</code> on the str:</p> <pre><code>&gt;&gt;&gt; '\xc5\x81'.decode('utf-8') u'\u0141' </code></pr...
9
2009-12-08T05:03:15Z
[ "python", "unicode", "utf-8" ]
Convert UTF-8 octets to unicode code points
1,864,701
<p>I have a set of UTF-8 octets and I need to convert them back to unicode code points. How can I do this in python.</p> <p>e.g. UTF-8 octet ['0xc5','0x81'] should be converted to 0x141 codepoint.</p>
6
2009-12-08T04:59:12Z
1,864,719
<pre><code>l = ['0xc5','0x81'] s = ''.join([chr(int(c, 16)) for c in l]).decode('utf8') s &gt;&gt;&gt; u'\u0141' </code></pre>
3
2009-12-08T05:03:42Z
[ "python", "unicode", "utf-8" ]
Convert UTF-8 octets to unicode code points
1,864,701
<p>I have a set of UTF-8 octets and I need to convert them back to unicode code points. How can I do this in python.</p> <p>e.g. UTF-8 octet ['0xc5','0x81'] should be converted to 0x141 codepoint.</p>
6
2009-12-08T04:59:12Z
1,864,722
<pre><code>&gt;&gt;&gt; "".join((chr(int(x,16)) for x in ['0xc5','0x81'])).decode("utf8") u'\u0141' </code></pre>
1
2009-12-08T05:03:54Z
[ "python", "unicode", "utf-8" ]
Convert UTF-8 octets to unicode code points
1,864,701
<p>I have a set of UTF-8 octets and I need to convert them back to unicode code points. How can I do this in python.</p> <p>e.g. UTF-8 octet ['0xc5','0x81'] should be converted to 0x141 codepoint.</p>
6
2009-12-08T04:59:12Z
1,865,044
<p>In lovely 3.x, where all <code>str</code>s are Unicode, and <code>bytes</code> are what <code>str</code>s used to be:</p> <pre><code>&gt;&gt;&gt; s = str(bytes([0xc5, 0x81]), 'utf-8') &gt;&gt;&gt; s 'Ł' &gt;&gt;&gt; ord(s) 321 &gt;&gt;&gt; hex(ord(s)) '0x141' </code></pre> <p>Which is what you asked for.</p>
6
2009-12-08T06:39:17Z
[ "python", "unicode", "utf-8" ]
What does first argument to `type` do?
1,865,250
<p>Some code.</p> <pre><code>In [1]: A = type('B', (), {}) In [2]: a = A() In [3]: b = B() --------------------------------------------------------------------------- NameError Traceback (most recent call last) /home/shabda/&lt;ipython console&gt; in &lt;module&gt;() NameError: name...
1
2009-12-08T07:31:46Z
1,865,296
<p>It's creating a new class with the name <code>B</code>:</p> <pre><code>Python 2.5.4 (r254:67916, Nov 19 2009, 22:14:20) [GCC 4.3.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; type('B', (), {}) &lt;class '__main__.B'&gt; </code></pre> <p>See the second form of <cod...
0
2009-12-08T07:38:58Z
[ "python", "metaprogramming" ]
What does first argument to `type` do?
1,865,250
<p>Some code.</p> <pre><code>In [1]: A = type('B', (), {}) In [2]: a = A() In [3]: b = B() --------------------------------------------------------------------------- NameError Traceback (most recent call last) /home/shabda/&lt;ipython console&gt; in &lt;module&gt;() NameError: name...
1
2009-12-08T07:31:46Z
1,865,350
<p>It's setting the <code>__name__</code> property of the created class.</p> <p>When you say:</p> <pre><code>class B(object): </code></pre> <p>two things happen with that 'B':</p> <ul> <li><p>The name 'B' is assigned the class. This is just like if you'd said "<code>B = ...</code>".</p></li> <li><p>The <code>__name...
2
2009-12-08T07:50:57Z
[ "python", "metaprogramming" ]
What does first argument to `type` do?
1,865,250
<p>Some code.</p> <pre><code>In [1]: A = type('B', (), {}) In [2]: a = A() In [3]: b = B() --------------------------------------------------------------------------- NameError Traceback (most recent call last) /home/shabda/&lt;ipython console&gt; in &lt;module&gt;() NameError: name...
1
2009-12-08T07:31:46Z
1,865,702
<p>'B' is just a string which is the name of A One place it is used is for the default <code>__repr__</code> of classes and their objects</p> <pre><code>&gt;&gt;&gt; A=type('B', (), {}) &gt;&gt;&gt; A &lt;class '__main__.B'&gt; &gt;&gt;&gt; a=A() &gt;&gt;&gt; a &lt;__main__.B object at 0xb7cf88ec&gt; </code></pre> <p...
0
2009-12-08T09:11:10Z
[ "python", "metaprogramming" ]
What languages other than Python have an explicit self?
1,865,262
<p>It seems a bit weird that Python requires you to explicitly pass <code>self</code> as the first argument to all class functions. Are there other languages that require something similar?</p>
3
2009-12-08T07:33:23Z
1,865,329
<p>By explicit, do you mean "explicitly passed as an argument to each class function"? If so, then Python is the only one I know off-hand. </p> <p>Most OO languages support <code>this</code> or <code>self</code> in some form, but most of them let you define class functions without always defining <code>self</code> as ...
5
2009-12-08T07:45:29Z
[ "python", "programming-languages" ]
What languages other than Python have an explicit self?
1,865,262
<p>It seems a bit weird that Python requires you to explicitly pass <code>self</code> as the first argument to all class functions. Are there other languages that require something similar?</p>
3
2009-12-08T07:33:23Z
1,865,369
<p>F# (presumably from its OCAML heritage) requires an explicit name for all self-references; though the name is any arbitrary identifier e.g.</p> <pre><code> override x.BeforeAnalysis() = base.BeforeAnalysis() DoWithLock x.AddReference </code></pre> <p>Here we're defining an overriding member function <code...
3
2009-12-08T07:54:01Z
[ "python", "programming-languages" ]
What languages other than Python have an explicit self?
1,865,262
<p>It seems a bit weird that Python requires you to explicitly pass <code>self</code> as the first argument to all class functions. Are there other languages that require something similar?</p>
3
2009-12-08T07:33:23Z
1,865,386
<p>Depending on your point of view, Lua. To quote the reference: "A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once." You can also define methods using either notation. So you could say that Lua has an optional explicit self.</p>
4
2009-12-08T07:57:31Z
[ "python", "programming-languages" ]
What languages other than Python have an explicit self?
1,865,262
<p>It seems a bit weird that Python requires you to explicitly pass <code>self</code> as the first argument to all class functions. Are there other languages that require something similar?</p>
3
2009-12-08T07:33:23Z
1,865,388
<p>any <a href="http://en.wikipedia.org/wiki/Object-oriented%5Fprogramming" rel="nofollow">Object-Oriented language</a> has a notion of this or self within member functions.</p>
1
2009-12-08T07:57:49Z
[ "python", "programming-languages" ]
What languages other than Python have an explicit self?
1,865,262
<p>It seems a bit weird that Python requires you to explicitly pass <code>self</code> as the first argument to all class functions. Are there other languages that require something similar?</p>
3
2009-12-08T07:33:23Z
1,865,411
<p>many object oriented languages if not all of them</p> <p>for example c++ support "this" instead of "self"</p> <p>but you dont have to pass it, it is passed passively</p> <p>hope that helps ;)</p>
0
2009-12-08T08:04:23Z
[ "python", "programming-languages" ]
What languages other than Python have an explicit self?
1,865,262
<p>It seems a bit weird that Python requires you to explicitly pass <code>self</code> as the first argument to all class functions. Are there other languages that require something similar?</p>
3
2009-12-08T07:33:23Z
1,865,503
<p>The programming language Oberon 2 has an explicitly <em>named</em> but not explicitly passed 'this' or 'self' argument for member functions of classes (known as type bound procedures in Oberon terminology)</p> <p>The following example is an Insert method on a type Text, where the identifier 't' is specified to bind...
3
2009-12-08T08:27:04Z
[ "python", "programming-languages" ]
What languages other than Python have an explicit self?
1,865,262
<p>It seems a bit weird that Python requires you to explicitly pass <code>self</code> as the first argument to all class functions. Are there other languages that require something similar?</p>
3
2009-12-08T07:33:23Z
8,381,989
<p><a href="http://en.wikipedia.org/wiki/Modula-3#Object_Oriented" rel="nofollow">Modula-3</a> does. Which is not too surprising since Python's class mechanism is <a href="http://docs.python.org/tutorial/classes.html#" rel="nofollow">a mixture of the ones found in Modula-3 and C++</a>.</p>
1
2011-12-05T07:04:25Z
[ "python", "programming-languages" ]
What languages other than Python have an explicit self?
1,865,262
<p>It seems a bit weird that Python requires you to explicitly pass <code>self</code> as the first argument to all class functions. Are there other languages that require something similar?</p>
3
2009-12-08T07:33:23Z
8,382,685
<p><a href="http://clojure.org/" rel="nofollow">Clojure</a> isn't an OOP language but does use explicit self parameters in some circumstances: most notably when you implement a protocol, and the "self" argument (you can name it anything you like) is the first parameter to a protocol method. This argument is then used f...
0
2011-12-05T08:33:26Z
[ "python", "programming-languages" ]
Search a file for a string, and execute a function if string not found; in python
1,865,484
<pre><code>def checkCache(cachedText): for line in open("cache"): if cachedText + ":" in line: print line open("cache").close() else: requestDefinition(cachedText) </code></pre> <p>This code searches each line of a file (cache) for a specific string (cachedText +...
0
2009-12-08T08:23:35Z
1,865,513
<p>Something like this:</p> <pre><code>def checkCache(cachedText): for line in open("cache"): if cachedText + ":" in line: print line break else: requestDefinition(cachedText) </code></pre> <p>Notice how the <code>else:</code> is attached to the <code>for</code>, not t...
1
2009-12-08T08:29:01Z
[ "python", "string", "file", "search", "caching" ]
Search a file for a string, and execute a function if string not found; in python
1,865,484
<pre><code>def checkCache(cachedText): for line in open("cache"): if cachedText + ":" in line: print line open("cache").close() else: requestDefinition(cachedText) </code></pre> <p>This code searches each line of a file (cache) for a specific string (cachedText +...
0
2009-12-08T08:23:35Z
1,865,521
<p>My guess is that you want something like this. If the line is found, you should "break". "break" will end the for loop. The else statement attached to the for loop (as opposed to an if statement) will only execute if the for loop iterated through each line without ever hitting the "break" condition. You still want ...
1
2009-12-08T08:31:37Z
[ "python", "string", "file", "search", "caching" ]
Search a file for a string, and execute a function if string not found; in python
1,865,484
<pre><code>def checkCache(cachedText): for line in open("cache"): if cachedText + ":" in line: print line open("cache").close() else: requestDefinition(cachedText) </code></pre> <p>This code searches each line of a file (cache) for a specific string (cachedText +...
0
2009-12-08T08:23:35Z
1,865,524
<p>your for loop is broken. you are actually checking each line of the file and executing the function for each line which does not match.</p> <p>note also that calling <code>open("cache").close()</code> will reopen the cache file and close it immediately, without closing the handle which was open at the beginning of ...
2
2009-12-08T08:32:07Z
[ "python", "string", "file", "search", "caching" ]
How to setting Camera options in Blender
1,865,581
<p>i try to create car's type game, but when i choose camera view , a view is not real every building have miss shape its look like perspective view .So i finding how to config it</p>
0
2009-12-08T08:45:36Z
2,582,123
<p>your grammar is really bad but i think i understand what you are asking. When you use a camera you have to zoom in until the box fills your screen. </p>
1
2010-04-06T01:23:45Z
[ "python", "blender" ]