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 |
|---|---|---|---|---|---|---|---|---|---|
Easy Python ASync. Precompiler? | 1,696,152 | <p>imagine you have an io heavy function like this:</p>
<pre><code>def getMd5Sum(path):
with open(path) as f:
return md5(f.read()).hexdigest()
</code></pre>
<p>Do you think Python is flexible enough to allow code like this (notice the $):</p>
<pre><code>def someGuiCallback(filebutton):
...
path =... | 2 | 2009-11-08T11:53:35Z | 1,700,139 | <p>Sure you can access function code (already compiled) from decorator, disassemble and hack it. You can even access the source of module it's defined in and recompile it. But I think this is not necessary. Below is an example using decorated generator, where <code>yield</code> statement serves as a delimiter between s... | 0 | 2009-11-09T10:12:37Z | [
"python",
"compiler-construction",
"syntax",
"asynchronous"
] |
How to read .bin files? | 1,696,165 | <p>I exported a .bin file from RealFlow 4 and now need to be able to read it in Python, to make an importer. How do these files work?</p>
| 1 | 2009-11-08T11:59:34Z | 1,696,534 | <p>You need to know how the data is coded in the file. If you have this information, you can use the <a href="http://docs.python.org/library/struct.html" rel="nofollow">struct package</a> to convert the binary data to something that can be used in python.</p>
<p>I hope it helps</p>
| 1 | 2009-11-08T13:30:40Z | [
"python",
"file",
"binary"
] |
How to read .bin files? | 1,696,165 | <p>I exported a .bin file from RealFlow 4 and now need to be able to read it in Python, to make an importer. How do these files work?</p>
| 1 | 2009-11-08T11:59:34Z | 1,696,809 | <p>Thanks guys, I've found the file. Here I uploaded it, and included a particle file for you.</p>
<p><a href="http://www.mediafire.com/download.php?xujqjghkcim" rel="nofollow">http://www.mediafire.com/download.php?xujqjghkcim</a></p>
| 0 | 2009-11-08T15:06:35Z | [
"python",
"file",
"binary"
] |
How to read .bin files? | 1,696,165 | <p>I exported a .bin file from RealFlow 4 and now need to be able to read it in Python, to make an importer. How do these files work?</p>
| 1 | 2009-11-08T11:59:34Z | 1,697,036 | <p>Here you go:</p>
<pre><code>import struct
class Particle:
"""A single particle. Attributes added in BinFile."""
pass
class BinFile:
"""Parse and store the contents of a RealFlow .bin file."""
def __init__(self, fname):
self.bindata = open(fname, "rb").read()
self.off = 0
... | 4 | 2009-11-08T16:22:48Z | [
"python",
"file",
"binary"
] |
Are all HttpError in python subclasses of IOError | 1,696,195 | <p>In our code we catch IOError and log it before reraising. I am getting a "connection reset by peer", but nothing in the logs. Is "connection reset by peer" a subclass of IOError in python?</p>
<pre><code> .....
File "/usr/lib/python2.5/httplib.py", line 1047, in readline
s = self._read()
File "/usr/lib/... | 1 | 2009-11-08T12:07:51Z | 1,696,240 | <p>The stack trace you pasted looks like some Exception of class <code>error</code> with arguments <code>(104, 'Connection reset by peer</code>).</p>
<p>So it looks like it's not a HTTPError exception at all. It looks to me like it's actually a <a href="http://docs.python.org/library/socket.html#socket.error" rel="nof... | 2 | 2009-11-08T12:25:54Z | [
"python",
"ioerror"
] |
Python: How to replace dashes in filenames? | 1,696,984 | <p>The question is related to the <a href="http://stackoverflow.com/questions/641985/rename-files-python-jython/642295#642295">answer</a> about renaming files recursively.</p>
<p>The code, changed to replace dashes, does not work with cases such as:</p>
<pre><code>./Beginners Tools/Hello's -Trojans-/bif43243
./Linux/... | -2 | 2009-11-08T16:06:44Z | 1,697,016 | <p>The os.path.walk is handy for traversing file system trees, a simple example:</p>
<pre><code>import os, shutil
def rename_file(arg, dirname, filename):
filepath = os.path.join(dirname, filename)
# check if file meets your rename condition here
if os.path.isfile(filepath):
new_name = "something"
... | 2 | 2009-11-08T16:15:04Z | [
"python",
"recursion",
"rename",
"parsing"
] |
Python: How to replace dashes in filenames? | 1,696,984 | <p>The question is related to the <a href="http://stackoverflow.com/questions/641985/rename-files-python-jython/642295#642295">answer</a> about renaming files recursively.</p>
<p>The code, changed to replace dashes, does not work with cases such as:</p>
<pre><code>./Beginners Tools/Hello's -Trojans-/bif43243
./Linux/... | -2 | 2009-11-08T16:06:44Z | 1,697,127 | <p>Most likely your problem is the single quotes, the parenthesis and the dashes. You can either escape them or replace them. </p>
<p>Actually looking at your edit, the original code you linked to is replacing characters in the filename not the whole path. You need to escape the characters in the path:</p>
<pre>
esc_... | 1 | 2009-11-08T16:53:09Z | [
"python",
"recursion",
"rename",
"parsing"
] |
Python: How to replace dashes in filenames? | 1,696,984 | <p>The question is related to the <a href="http://stackoverflow.com/questions/641985/rename-files-python-jython/642295#642295">answer</a> about renaming files recursively.</p>
<p>The code, changed to replace dashes, does not work with cases such as:</p>
<pre><code>./Beginners Tools/Hello's -Trojans-/bif43243
./Linux/... | -2 | 2009-11-08T16:06:44Z | 7,652,835 | <p>this post might help: <a href="http://onesandzeros.posterous.com/python-script-to-recursively-replace-string-i" rel="nofollow">python script to recursively replace string in filename and contents</a></p>
| 0 | 2011-10-04T19:07:09Z | [
"python",
"recursion",
"rename",
"parsing"
] |
What is the return value of subprocess.call()? | 1,696,998 | <p>I am not sure what the return value of <code>subprocess.call()</code> means.</p>
<ul>
<li><p>Can I safely assume a zero value will always mean that the command executed successfully?</p></li>
<li><p>Is the return value equivalent to the exit staus of a shell command?</p></li>
</ul>
<p>For example, will the followi... | 12 | 2009-11-08T16:10:18Z | 1,697,037 | <p>Yes, <code>Subprocess.call</code> returns "actual process return code". </p>
<p>You can check official documentation of <a href="http://docs.python.org/library/subprocess.html#subprocess.call" rel="nofollow">Subprocess.call</a> and <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.returncode"... | 7 | 2009-11-08T16:22:55Z | [
"python",
"linux"
] |
What is the return value of subprocess.call()? | 1,696,998 | <p>I am not sure what the return value of <code>subprocess.call()</code> means.</p>
<ul>
<li><p>Can I safely assume a zero value will always mean that the command executed successfully?</p></li>
<li><p>Is the return value equivalent to the exit staus of a shell command?</p></li>
</ul>
<p>For example, will the followi... | 12 | 2009-11-08T16:10:18Z | 1,697,044 | <p>It is the return code, but keep in mind it's up to the author of the subprocess what the return code means. There is a strong culture of 0 meaning success, but there's nothing enforcing it. </p>
| 6 | 2009-11-08T16:24:27Z | [
"python",
"linux"
] |
What is the return value of subprocess.call()? | 1,696,998 | <p>I am not sure what the return value of <code>subprocess.call()</code> means.</p>
<ul>
<li><p>Can I safely assume a zero value will always mean that the command executed successfully?</p></li>
<li><p>Is the return value equivalent to the exit staus of a shell command?</p></li>
</ul>
<p>For example, will the followi... | 12 | 2009-11-08T16:10:18Z | 1,697,051 | <p>You are at the mercy of the commands that you call.
Consider this:</p>
<p>test.py</p>
<pre><code>#!/usr/bin/env python
success=False
if not success:
exit()
</code></pre>
<p>Then running your code (with cmd='test.py') will result in
SUCCESS!!</p>
<p>merely because test.py does not conform to the convention ... | 3 | 2009-11-08T16:27:23Z | [
"python",
"linux"
] |
How can I reduce memory usage of a Twisted server? | 1,697,009 | <p>I wrote an audio broadcasting server with Python/Twisted. It works fine, but the usage of memory grows too fast! I think that's because some user's network might not be good enough to download the audio in time. </p>
<p>My audio server broadcast audio data to different listener's client, if some of them can't downl... | 2 | 2009-11-08T16:13:08Z | 1,698,828 | <p>Make sure you're using Python's <a href="http://docs.python.org/library/gc.html" rel="nofollow">garbage collector</a> and then go through and delete variables you aren't using.</p>
| -5 | 2009-11-09T02:38:36Z | [
"python",
"memory-management",
"twisted"
] |
How can I reduce memory usage of a Twisted server? | 1,697,009 | <p>I wrote an audio broadcasting server with Python/Twisted. It works fine, but the usage of memory grows too fast! I think that's because some user's network might not be good enough to download the audio in time. </p>
<p>My audio server broadcast audio data to different listener's client, if some of them can't downl... | 2 | 2009-11-08T16:13:08Z | 1,705,364 | <p>You didn't say, but I'm going to assume that you're using TCP. It would be hard to write a UDP-based system which had ever increasing memory because of clients who can't receive data as fast as you're trying to send it.</p>
<p>TCP has built-in flow control capabilities. If a receiver cannot read data as fast as y... | 2 | 2009-11-10T02:50:28Z | [
"python",
"memory-management",
"twisted"
] |
Django/SQL: keeping track of who who read what in a forum | 1,697,045 | <p>I'm working on a not-so-big project in django that will among other things incorporate a forum system.</p>
<p>I have most of the system at a more or less functioning state, but I'm still missing a feature to mark unread threads for the users when there are new posts.</p>
<p>The thing is I can't really think of a w... | 2 | 2009-11-08T16:24:29Z | 1,697,061 | <p>You're much better off storing the "read" bit, not the "unread" bit. And you can store them not as relational data, but in a giant bit-blob. Then you don't have to modify the read data at all when new posts are added, only when a user reads posts.</p>
| 2 | 2009-11-08T16:29:53Z | [
"python",
"sql",
"django",
"database-design",
"database"
] |
Django/SQL: keeping track of who who read what in a forum | 1,697,045 | <p>I'm working on a not-so-big project in django that will among other things incorporate a forum system.</p>
<p>I have most of the system at a more or less functioning state, but I'm still missing a feature to mark unread threads for the users when there are new posts.</p>
<p>The thing is I can't really think of a w... | 2 | 2009-11-08T16:24:29Z | 1,697,224 | <p>You might also simply store the last time a user was reading a particular forum. Any posts that have been updated since that date are new. You'll only be storing one additional piece of information per user as opposed to a piece of information per post per user.</p>
| 1 | 2009-11-08T17:20:39Z | [
"python",
"sql",
"django",
"database-design",
"database"
] |
Which database should I use to store records, and how should I use it? | 1,697,153 | <p>I'm developing an application that will store a sizeable number of records. These records will be something like (URL, date, title, source, {optional data...})</p>
<p>As this is a client-side app, I don't want to use a database server, I just want the info stored into files.</p>
<p>I want the files to be readable ... | 5 | 2009-11-08T17:01:35Z | 1,697,172 | <p>What about <a href="http://www.mongodb.org/pages/viewpage.action?pageId=133415" rel="nofollow">MongoDB</a>? I haven't tried it yet, but it seems interesting.</p>
| 0 | 2009-11-08T17:08:27Z | [
"c++",
"python",
"database",
"persistence"
] |
Which database should I use to store records, and how should I use it? | 1,697,153 | <p>I'm developing an application that will store a sizeable number of records. These records will be something like (URL, date, title, source, {optional data...})</p>
<p>As this is a client-side app, I don't want to use a database server, I just want the info stored into files.</p>
<p>I want the files to be readable ... | 5 | 2009-11-08T17:01:35Z | 1,697,185 | <p>BerkeleyDB is good, also look at the *DBM incarnations (e.g. GDBM). The big question though is: for what do you need to search? Do you need to search by that URL, by a range of URLs or the dates you list? </p>
<p>It is also quite possible to keep groups of records as simple files in the local filesystem, groupe... | 2 | 2009-11-08T17:11:55Z | [
"c++",
"python",
"database",
"persistence"
] |
Which database should I use to store records, and how should I use it? | 1,697,153 | <p>I'm developing an application that will store a sizeable number of records. These records will be something like (URL, date, title, source, {optional data...})</p>
<p>As this is a client-side app, I don't want to use a database server, I just want the info stored into files.</p>
<p>I want the files to be readable ... | 5 | 2009-11-08T17:01:35Z | 1,697,202 | <blockquote>
<p>I am seeing two possibilities: sqlite
and BerkeleyDB. As my use case is
clearly not relational, I am tempted
to go with BerkeleyDB, however I don't
really know how I should use it to
store my records, as it only stores
key/value pairs.</p>
</blockquote>
<p>What you are describing is exact... | 5 | 2009-11-08T17:15:22Z | [
"c++",
"python",
"database",
"persistence"
] |
Which database should I use to store records, and how should I use it? | 1,697,153 | <p>I'm developing an application that will store a sizeable number of records. These records will be something like (URL, date, title, source, {optional data...})</p>
<p>As this is a client-side app, I don't want to use a database server, I just want the info stored into files.</p>
<p>I want the files to be readable ... | 5 | 2009-11-08T17:01:35Z | 1,697,239 | <p>Personally I would use sqlite anyway. It has always just worked for me (and for others I work with). When your app grows and you suddenly do want to do something a little more sophisticated, you won't have to rewrite.</p>
<p>On the other hand, I've seen various comments on the Python dev list about Berkely DB tha... | 2 | 2009-11-08T17:24:55Z | [
"c++",
"python",
"database",
"persistence"
] |
Which database should I use to store records, and how should I use it? | 1,697,153 | <p>I'm developing an application that will store a sizeable number of records. These records will be something like (URL, date, title, source, {optional data...})</p>
<p>As this is a client-side app, I don't want to use a database server, I just want the info stored into files.</p>
<p>I want the files to be readable ... | 5 | 2009-11-08T17:01:35Z | 1,697,498 | <p>If you're only going to use a single field to look up records, a simple key-value store would be a good choice. Store that single field (or any other unique ID) as your key, serialize each record as a string (using JSON or similar), and store that string as the value. Berkeley DB is certainly a reasonable choice f... | 1 | 2009-11-08T18:50:22Z | [
"c++",
"python",
"database",
"persistence"
] |
Which database should I use to store records, and how should I use it? | 1,697,153 | <p>I'm developing an application that will store a sizeable number of records. These records will be something like (URL, date, title, source, {optional data...})</p>
<p>As this is a client-side app, I don't want to use a database server, I just want the info stored into files.</p>
<p>I want the files to be readable ... | 5 | 2009-11-08T17:01:35Z | 1,698,109 | <p>Ok, so you say just storing the data..? You really only need a DB for retrieval, lookup, summarising, etc. So, for storing, just use simple text files and append lines. Compress the data if you need to, use delims between fields - just about any language will be able to read such files. If you do want to retrieve, t... | 0 | 2009-11-08T22:17:56Z | [
"c++",
"python",
"database",
"persistence"
] |
Differences between static and instance variables in python. Do they even exist? | 1,697,273 | <p>A random class definition:</p>
<pre><code>class ABC:
x = 6
</code></pre>
<p>Setting some values, first for the abc instance, later for the static variable:</p>
<pre><code>abc = ABC()
abc.x = 2
ABC.x = 5
</code></pre>
<p>and then print the results:</p>
<pre><code>print abc.x
print ABC.x
</code></pre>
<p>whi... | 8 | 2009-11-08T17:36:11Z | 1,697,283 | <pre><code>class SomeClass:
x=6 # class variable
def __init__(self):
self.y = 666 # instance variable
</code></pre>
<p>There is virtue in declaring a class scoped variable: it serves as default for one. Think of class scoped variable as you would think of "static" variables in some other languages.</p>
| 9 | 2009-11-08T17:40:16Z | [
"python"
] |
Differences between static and instance variables in python. Do they even exist? | 1,697,273 | <p>A random class definition:</p>
<pre><code>class ABC:
x = 6
</code></pre>
<p>Setting some values, first for the abc instance, later for the static variable:</p>
<pre><code>abc = ABC()
abc.x = 2
ABC.x = 5
</code></pre>
<p>and then print the results:</p>
<pre><code>print abc.x
print ABC.x
</code></pre>
<p>whi... | 8 | 2009-11-08T17:36:11Z | 1,697,287 | <p>A class-level variable (called "static" in other languages) is owned by the class, and shared by all instances of the class.</p>
<p>A instance variable is part of by each distinct instance of the class.</p>
<p>However. </p>
<p>You can add a new instance variable any time you want.</p>
<p>So getting <code>abc.x<... | 3 | 2009-11-08T17:42:23Z | [
"python"
] |
Differences between static and instance variables in python. Do they even exist? | 1,697,273 | <p>A random class definition:</p>
<pre><code>class ABC:
x = 6
</code></pre>
<p>Setting some values, first for the abc instance, later for the static variable:</p>
<pre><code>abc = ABC()
abc.x = 2
ABC.x = 5
</code></pre>
<p>and then print the results:</p>
<pre><code>print abc.x
print ABC.x
</code></pre>
<p>whi... | 8 | 2009-11-08T17:36:11Z | 1,697,290 | <p>Python makes a distinction between the two. The purpose could be multiple, but one example is this:</p>
<pre><code>class token(object):
id = 0
def __init__(self, value):
self.value = value
self.id = token.id
token.id += 1
</code></pre>
<p>Here, the class variable <code>token.id</co... | 2 | 2009-11-08T17:42:44Z | [
"python"
] |
Differences between static and instance variables in python. Do they even exist? | 1,697,273 | <p>A random class definition:</p>
<pre><code>class ABC:
x = 6
</code></pre>
<p>Setting some values, first for the abc instance, later for the static variable:</p>
<pre><code>abc = ABC()
abc.x = 2
ABC.x = 5
</code></pre>
<p>and then print the results:</p>
<pre><code>print abc.x
print ABC.x
</code></pre>
<p>whi... | 8 | 2009-11-08T17:36:11Z | 1,697,396 | <p>Every object has a <code>__dict__</code>. The class ABC and its instance, abc, are both objects, and so each has their own separate <code>__dict__</code>:</p>
<pre><code>In [3]: class ABC:
...: x=6
</code></pre>
<p>Notice <code>ABC.__dict__</code> has a 'x' key:</p>
<pre><code>In [4]: ABC.__dict__
Out[4]: ... | 2 | 2009-11-08T18:13:20Z | [
"python"
] |
Differences between static and instance variables in python. Do they even exist? | 1,697,273 | <p>A random class definition:</p>
<pre><code>class ABC:
x = 6
</code></pre>
<p>Setting some values, first for the abc instance, later for the static variable:</p>
<pre><code>abc = ABC()
abc.x = 2
ABC.x = 5
</code></pre>
<p>and then print the results:</p>
<pre><code>print abc.x
print ABC.x
</code></pre>
<p>whi... | 8 | 2009-11-08T17:36:11Z | 1,716,730 | <p>Warning: the following is an oversimplification; I'm ignoring <code>__new__()</code> and a bunch of other special class methods, and handwaving a lot of details. But this explanation will get you pretty far in Python.</p>
<p>When you create an instance of a class in Python, like calling <strong>ABC()</strong> in y... | 14 | 2009-11-11T17:21:43Z | [
"python"
] |
Differences between static and instance variables in python. Do they even exist? | 1,697,273 | <p>A random class definition:</p>
<pre><code>class ABC:
x = 6
</code></pre>
<p>Setting some values, first for the abc instance, later for the static variable:</p>
<pre><code>abc = ABC()
abc.x = 2
ABC.x = 5
</code></pre>
<p>and then print the results:</p>
<pre><code>print abc.x
print ABC.x
</code></pre>
<p>whi... | 8 | 2009-11-08T17:36:11Z | 30,792,503 | <p>The benefit of a "static" or in Python a "class attribute" is that each instance of the class will have access to the same class attribute. This is not true for instance attributes as you may be aware.</p>
<p>Take for example:</p>
<pre><code>class A(object):
b = 1
A.b # => 1
inst = A()
inst2 = A()
i... | 0 | 2015-06-11T22:28:08Z | [
"python"
] |
Algorithm for solving Sudoku | 1,697,334 | <p>I want to write a code in python to solve a sudoku puzzle. Do you guys have any idea about a good algorithm for this purpose. I read somewhere in net about a algorithm which solves it by filling the whole box with all possible numbers, then inserts known values into the corresponding boxes.From the row and coloumn o... | 2 | 2009-11-08T17:54:19Z | 1,697,407 | <p>I wrote a simple program that solved the easy ones. It took its input from a file which was just a matrix with spaces and numbers. The datastructure to solve it was just a 9 by 9 matrix of a bit mask. The bit mask would specify which numbers were still possible on a certain position. Filling in the numbers from the ... | 4 | 2009-11-08T18:16:35Z | [
"python",
"algorithm",
"sudoku"
] |
Algorithm for solving Sudoku | 1,697,334 | <p>I want to write a code in python to solve a sudoku puzzle. Do you guys have any idea about a good algorithm for this purpose. I read somewhere in net about a algorithm which solves it by filling the whole box with all possible numbers, then inserts known values into the corresponding boxes.From the row and coloumn o... | 2 | 2009-11-08T17:54:19Z | 20,279,566 | <p>Here is my sudoku solver in python. It uses simple backtracking algorithm to solve the puzzle.
For simplicity no input validations or fancy output is done. Its the bare minimum code which solves the problem. </p>
<h2>Algorithm</h2>
<ol>
<li>Find all legal values of a given cell</li>
<li>For each legal value, Go re... | 4 | 2013-11-29T06:20:28Z | [
"python",
"algorithm",
"sudoku"
] |
Algorithm for solving Sudoku | 1,697,334 | <p>I want to write a code in python to solve a sudoku puzzle. Do you guys have any idea about a good algorithm for this purpose. I read somewhere in net about a algorithm which solves it by filling the whole box with all possible numbers, then inserts known values into the corresponding boxes.From the row and coloumn o... | 2 | 2009-11-08T17:54:19Z | 35,500,280 | <p>Here is a much faster solution based on hari's answer. The basic difference is that we keep a set of possible values for cells that don't have a value assigned. So when we try a new value, we only try valid values and we also propagate what this choice means for the rest of the sudoku. In the propagation step, we re... | 0 | 2016-02-19T08:04:32Z | [
"python",
"algorithm",
"sudoku"
] |
Algorithm for solving Sudoku | 1,697,334 | <p>I want to write a code in python to solve a sudoku puzzle. Do you guys have any idea about a good algorithm for this purpose. I read somewhere in net about a algorithm which solves it by filling the whole box with all possible numbers, then inserts known values into the corresponding boxes.From the row and coloumn o... | 2 | 2009-11-08T17:54:19Z | 35,500,598 | <p>Not gonna write full code, but I did a sudoku solver a long time ago. I found that it didn't always solve it (the thing people do when they have a newspaper is incomplete!), but now think I know how to do it.</p>
<ul>
<li>Setup: for each square, have a set of flags for each number showing the allowed numbers.</li>
... | 0 | 2016-02-19T08:26:59Z | [
"python",
"algorithm",
"sudoku"
] |
Python: @staticmethod with @property | 1,697,501 | <p>I want</p>
<pre><code>Stats.singleton.twitter_count += 1
</code></pre>
<p>and I thought I could do</p>
<pre><code>class Stats:
singleton_object = None
@property
@staticmethod
def singleton():
if Stats.singleton_object:
return Stats.singleton_object
Stats.singleton_obje... | 22 | 2009-11-08T18:52:31Z | 1,697,546 | <p>Static methods don't make sense in Python. That's because they do nothing that class methods can't, and are class methods much easier to extend in the future (when multiple class methods use each other etc).</p>
<p>What you need is simply a class method property.</p>
<p>I have a class method property from my code ... | 8 | 2009-11-08T19:04:34Z | [
"python",
"singleton",
"properties"
] |
Python: @staticmethod with @property | 1,697,501 | <p>I want</p>
<pre><code>Stats.singleton.twitter_count += 1
</code></pre>
<p>and I thought I could do</p>
<pre><code>class Stats:
singleton_object = None
@property
@staticmethod
def singleton():
if Stats.singleton_object:
return Stats.singleton_object
Stats.singleton_obje... | 22 | 2009-11-08T18:52:31Z | 1,697,656 | <p>Singletons are pointless in python.</p>
<pre><code>class A:
class_var = object()
# two objects
a, b = A(), A()
# same var everywhere
assert a.class_var is b.class_var is A.class_var
</code></pre>
<p>Python's <code>int</code>s are differnt from simple <code>object</code>s, so it's not always that simple . But f... | 13 | 2009-11-08T19:40:34Z | [
"python",
"singleton",
"properties"
] |
Python: @staticmethod with @property | 1,697,501 | <p>I want</p>
<pre><code>Stats.singleton.twitter_count += 1
</code></pre>
<p>and I thought I could do</p>
<pre><code>class Stats:
singleton_object = None
@property
@staticmethod
def singleton():
if Stats.singleton_object:
return Stats.singleton_object
Stats.singleton_obje... | 22 | 2009-11-08T18:52:31Z | 7,864,317 | <p>User kaizer.se was onto something as far as the original question goes. I took it a step further in terms of simplicity, so that it now requires only a single decorator:</p>
<pre><code>class classproperty(property):
def __get__(self, cls, owner):
return classmethod(self.fget).__get__(None, owner)()
</c... | 32 | 2011-10-23T04:20:14Z | [
"python",
"singleton",
"properties"
] |
Python: @staticmethod with @property | 1,697,501 | <p>I want</p>
<pre><code>Stats.singleton.twitter_count += 1
</code></pre>
<p>and I thought I could do</p>
<pre><code>class Stats:
singleton_object = None
@property
@staticmethod
def singleton():
if Stats.singleton_object:
return Stats.singleton_object
Stats.singleton_obje... | 22 | 2009-11-08T18:52:31Z | 23,014,372 | <p>Following up with what KyleAlanHale wrote:</p>
<p>His example works great, until you try and do:</p>
<pre><code>Stats.singleton = 5
</code></pre>
<p>This won't give you an error, it will overwrite the function, so that when you type next</p>
<pre><code>single = Stats.singleton
print single
</code></pre>
<p>You'... | 0 | 2014-04-11T13:43:30Z | [
"python",
"singleton",
"properties"
] |
Clean input strings without using the django Form classes | 1,697,508 | <p>Is there a recommended way of using Django to clean an input string without going through the Django form system? </p>
<p>That is, I'm writing code that delivers form input via AJAX so I'm skipping the whole Form model django offers. But I do want to clean the input prior to submission to the database. </p>
| 10 | 2009-11-08T18:53:11Z | 1,697,569 | <p>Django Form models aren't just about rendering forms, they're more about processing and sanitizing form (GET/POST) input, which <em>is</em> what you want to do. When the POST or GET data from your AJAX request reaches your server it's essentially indistinguishable from form data. I would advocate creating a Form mod... | 15 | 2009-11-08T19:10:52Z | [
"python",
"django"
] |
Numpy, problem with long arrays | 1,697,557 | <p>I have two arrays (a and b) with n integer elements in the range (0,N).</p>
<p><em>typo: arrays with 2^n integers where the largest integer takes the value N = 3^n</em></p>
<p>I want to calculate the sum of every combination of elements in a and b (sum_ij_ = a_i_ + b_j_ for all <em>i,j</em>). Then take modulus N ... | 7 | 2009-11-08T19:07:52Z | 1,697,998 | <p>Edit in response to jonalm's comment:</p>
<blockquote>
<p>jonalm: N~3^n not n~3^N. N is max element in a and n is number of
elements in a.</p>
</blockquote>
<p>n is ~ 2^20. If N is ~ 3^n then N is ~ 3^(2^20) > 10^(500207).
Scientists estimate (<a href="http://www.stormloader.com/ajy/reallife.html" rel="nofollo... | 1 | 2009-11-08T21:38:38Z | [
"python",
"math",
"numpy"
] |
Numpy, problem with long arrays | 1,697,557 | <p>I have two arrays (a and b) with n integer elements in the range (0,N).</p>
<p><em>typo: arrays with 2^n integers where the largest integer takes the value N = 3^n</em></p>
<p>I want to calculate the sum of every combination of elements in a and b (sum_ij_ = a_i_ + b_j_ for all <em>i,j</em>). Then take modulus N ... | 7 | 2009-11-08T19:07:52Z | 1,698,610 | <p>try chunking it. your meshgrid is an NxN matrix, block that up to 10x10 N/10xN/10 and just compute 100 bins, add them up at the end. this only uses ~1% as much memory as doing the whole thing.</p>
| 4 | 2009-11-09T01:07:24Z | [
"python",
"math",
"numpy"
] |
Numpy, problem with long arrays | 1,697,557 | <p>I have two arrays (a and b) with n integer elements in the range (0,N).</p>
<p><em>typo: arrays with 2^n integers where the largest integer takes the value N = 3^n</em></p>
<p>I want to calculate the sum of every combination of elements in a and b (sum_ij_ = a_i_ + b_j_ for all <em>i,j</em>). Then take modulus N ... | 7 | 2009-11-08T19:07:52Z | 1,698,730 | <p>Check your math, that's a <em>lot</em> of space you're asking for:</p>
<p>2^20*2^20 = 2^40 = 1 099 511 627 776</p>
<p>If each of your elements was just one byte, that's already one terabyte of memory.</p>
<p>Add a loop or two. This problem is not suited to maxing out your memory and minimizing your computation.</... | 1 | 2009-11-09T01:51:25Z | [
"python",
"math",
"numpy"
] |
Python threading appears to run threads sequentially | 1,697,571 | <p>I am trying to use threads in a Python project I am working on, but threads don't appear to be behaving as they are supposed to in my code. It seems that all threads run sequentially (i.e. thread2 starts after thread 1 ends, they don't both start at the same time). I wrote a simple script to test this, and that too ... | 9 | 2009-11-08T19:11:07Z | 1,697,586 | <p>In the time it takes the second thread to start the first thread loops and prints already.</p>
<p>Here it looks like this, you can see the 2nd thread starting after the first emitted a few hellos.</p>
<pre><code>Hello
Hello
Hello
Hello
Hello
Helloworld
Helloworld
Helloworld
Helloworld
Helloworld
world
world
w... | 10 | 2009-11-08T19:15:33Z | [
"python",
"multithreading"
] |
Python threading appears to run threads sequentially | 1,697,571 | <p>I am trying to use threads in a Python project I am working on, but threads don't appear to be behaving as they are supposed to in my code. It seems that all threads run sequentially (i.e. thread2 starts after thread 1 ends, they don't both start at the same time). I wrote a simple script to test this, and that too ... | 9 | 2009-11-08T19:11:07Z | 1,697,604 | <p>This really depends on your Operating System's scheduler, your processor.<br>
Other than that, it is known that CPython's threads aren't perfect because of the <a href="http://www.dabeaz.com/python/GIL.pdf" rel="nofollow">GIL</a>(PDF), which, in short, means that a lot of the times threads do run sequentially, or so... | 3 | 2009-11-08T19:21:49Z | [
"python",
"multithreading"
] |
Python threading appears to run threads sequentially | 1,697,571 | <p>I am trying to use threads in a Python project I am working on, but threads don't appear to be behaving as they are supposed to in my code. It seems that all threads run sequentially (i.e. thread2 starts after thread 1 ends, they don't both start at the same time). I wrote a simple script to test this, and that too ... | 9 | 2009-11-08T19:11:07Z | 1,697,670 | <p>Currently in python, threads get changed after executing some specified amount of bytecode instructions. They don't run at the same time. You will only have threads executing in parallel when one of them calls some I/O-intensive or not python-affecting module that can release GIL (global interpreter lock).</p>
<p>I... | 10 | 2009-11-08T19:45:31Z | [
"python",
"multithreading"
] |
Python threading appears to run threads sequentially | 1,697,571 | <p>I am trying to use threads in a Python project I am working on, but threads don't appear to be behaving as they are supposed to in my code. It seems that all threads run sequentially (i.e. thread2 starts after thread 1 ends, they don't both start at the same time). I wrote a simple script to test this, and that too ... | 9 | 2009-11-08T19:11:07Z | 1,698,096 | <p>The behaviour may also change depending on if the system is using has a single processor or multiple processors, as explained by <a href="http://blip.tv/file/2232410" rel="nofollow">this talk</a> by David Beazley.</p>
<p>As viraptor says, the first thread will release the GIL after executing sys.getcheckinterval() ... | 3 | 2009-11-08T22:11:22Z | [
"python",
"multithreading"
] |
Checking if a domain name is registered | 1,697,663 | <p>How would I go about checking if a domain name is registered? I was thinking about checking if it has a corresponding IP but it doesn't seem to work as well as I had hoped. Is there a solution in either PHP or Python that can check?</p>
| 12 | 2009-11-08T19:43:27Z | 1,697,678 | <p>Mike Nott has created a <a href="http://www.nott.org/blog/php-whois-script.html" rel="nofollow">simple PHP class</a> that allows you to query the who.is data for any domain you wish.</p>
<p>Once you call</p>
<pre><code> $whois = getwhois($sld, $tld);
</code></pre>
<p>you then just need to check the contents of... | 2 | 2009-11-08T19:47:48Z | [
"php",
"python",
"dns",
"names"
] |
Checking if a domain name is registered | 1,697,663 | <p>How would I go about checking if a domain name is registered? I was thinking about checking if it has a corresponding IP but it doesn't seem to work as well as I had hoped. Is there a solution in either PHP or Python that can check?</p>
| 12 | 2009-11-08T19:43:27Z | 1,697,681 | <p>"Registered" doesn't mean "assigned an IP address". To know whether a domain name is registered, you'll need to do a whois query.</p>
<p>For Python, there's <a href="http://code.google.com/p/pywhois/">pywhois</a>, but from its web site it seems somewhat immature. Also see <a href="http://stackoverflow.com/questions... | 15 | 2009-11-08T19:48:16Z | [
"php",
"python",
"dns",
"names"
] |
Checking if a domain name is registered | 1,697,663 | <p>How would I go about checking if a domain name is registered? I was thinking about checking if it has a corresponding IP but it doesn't seem to work as well as I had hoped. Is there a solution in either PHP or Python that can check?</p>
| 12 | 2009-11-08T19:43:27Z | 1,698,397 | <p>use <a href="http://pear.php.net/package/Net%5FWhois" rel="nofollow">the net_whois package from pear</a>.
for multiple results, which may occur when server names are also reported along with domain names do something similar to:</p>
<pre><code>require_once ¨Net/Whois.php¨;
$whois = new Net_Whois;
$whois->autho... | 0 | 2009-11-08T23:49:27Z | [
"php",
"python",
"dns",
"names"
] |
Checking if a domain name is registered | 1,697,663 | <p>How would I go about checking if a domain name is registered? I was thinking about checking if it has a corresponding IP but it doesn't seem to work as well as I had hoped. Is there a solution in either PHP or Python that can check?</p>
| 12 | 2009-11-08T19:43:27Z | 35,832,174 | <p>To check if a domain name is registered you need two informations:</p>
<ul>
<li>The whois server for the respective top level domain (or second level domain)</li>
<li>A matching pattern for the response of that whois server</li>
</ul>
<p>Those informations do change frequently. This <a href="https://github.com/who... | 0 | 2016-03-06T20:32:41Z | [
"php",
"python",
"dns",
"names"
] |
How to pass initial parameter to django's ModelForm instance? | 1,697,702 | <p>The particular case I have is like this:</p>
<p>I have a Transaction model, with fields: <code>from</code>, <code>to</code> (both are <code>ForeignKey</code>s to <code>auth.User</code> model) and <code>amount</code>. In my form, I'd like to present the user 2 fields to fill in: <code>amount</code> and <code>from</c... | 16 | 2009-11-08T19:54:40Z | 1,697,715 | <p>In <a href="http://www.djangobook.com/en/2.0/chapter07/" rel="nofollow">http://www.djangobook.com/en/2.0/chapter07/</a> , the section <em>Setting Initial Values</em> describes how to use the <code>initial</code> parameter to the <code>Form</code> constructor. You may also do extra stuff in the <code>__init__</code>... | 2 | 2009-11-08T19:57:36Z | [
"python",
"django",
"django-models",
"django-forms"
] |
How to pass initial parameter to django's ModelForm instance? | 1,697,702 | <p>The particular case I have is like this:</p>
<p>I have a Transaction model, with fields: <code>from</code>, <code>to</code> (both are <code>ForeignKey</code>s to <code>auth.User</code> model) and <code>amount</code>. In my form, I'd like to present the user 2 fields to fill in: <code>amount</code> and <code>from</c... | 16 | 2009-11-08T19:54:40Z | 1,697,770 | <p>Use the following method (hopefully it's clear enough):</p>
<pre><code>class BackupForm(ModelForm):
"""Form for adding and editing backups."""
def __init__(self, *args, **kwargs):
systemid = kwargs.pop('systemid')
super(BackupForm, self).__init__(*args, **kwargs)
self.fields['units'... | 28 | 2009-11-08T20:11:39Z | [
"python",
"django",
"django-models",
"django-forms"
] |
How to pass initial parameter to django's ModelForm instance? | 1,697,702 | <p>The particular case I have is like this:</p>
<p>I have a Transaction model, with fields: <code>from</code>, <code>to</code> (both are <code>ForeignKey</code>s to <code>auth.User</code> model) and <code>amount</code>. In my form, I'd like to present the user 2 fields to fill in: <code>amount</code> and <code>from</c... | 16 | 2009-11-08T19:54:40Z | 10,092,247 | <p>I ran into this problem as well, and this was my solution:</p>
<pre><code>class ChangeEmailForm(forms.ModelForm):
def __init__(self, user, *args, **kwargs):
self.user = user
super(ChangeEmailForm, self).__init__(*args, **kwargs)
self.fields['email'].initial = user.email
class Meta:
... | 6 | 2012-04-10T16:02:28Z | [
"python",
"django",
"django-models",
"django-forms"
] |
How to pass initial parameter to django's ModelForm instance? | 1,697,702 | <p>The particular case I have is like this:</p>
<p>I have a Transaction model, with fields: <code>from</code>, <code>to</code> (both are <code>ForeignKey</code>s to <code>auth.User</code> model) and <code>amount</code>. In my form, I'd like to present the user 2 fields to fill in: <code>amount</code> and <code>from</c... | 16 | 2009-11-08T19:54:40Z | 11,398,832 | <p>Pass the user into the <code>__init__</code> of the form, and then call <code>super(â¦)</code>. Then set <code>self.fields['from'].queryset</code> to <code>user.peers</code></p>
| 0 | 2012-07-09T16:00:48Z | [
"python",
"django",
"django-models",
"django-forms"
] |
How to open a pdb file in Python? | 1,697,739 | <p>I've got an e-book (viewable with isilo) in a pdb file. Is it possible to read it's contents with Python (perl, ruby, php)?</p>
| 2 | 2009-11-08T20:03:56Z | 1,697,779 | <p>PDB was designed for Palm pilots in the very beginning. isilo started kind of as a universal reader on PDAs so they've also implemented the PDB protocol.</p>
<p>For python, you can use the <a href="http://sourceforge.net/projects/pythonpalmdb/" rel="nofollow">Python PalmDB library</a>. </p>
| 4 | 2009-11-08T20:14:03Z | [
"python",
"pdb-palm",
"palmdb"
] |
How to parse a custom string using optparse? | 1,697,761 | <p><strong>How to parse a custom string using optparse, instead of command line argument?</strong></p>
<p>I want to parse a string that I get from using <code>raw_input()</code>.
How can I use optparse for that?</p>
| 1 | 2009-11-08T20:10:13Z | 1,697,776 | <p>Use the <a href="http://docs.python.org/library/shlex.html" rel="nofollow">shlex module</a> to split the input first.</p>
<pre><code>>>> import shlex
>>> shlex.split(raw_input())
this is "a test" of shlex
['this', 'is', 'a test', 'of', 'shlex']
</code></pre>
| 4 | 2009-11-08T20:13:10Z | [
"python",
"optparse"
] |
How to parse a custom string using optparse? | 1,697,761 | <p><strong>How to parse a custom string using optparse, instead of command line argument?</strong></p>
<p>I want to parse a string that I get from using <code>raw_input()</code>.
How can I use optparse for that?</p>
| 1 | 2009-11-08T20:10:13Z | 1,697,781 | <p><code>optparse</code> expects a list of values that have been broken up shell-style (which is what <code>argv[1:]</code> is). To accomplish the same starting with a string, try this:</p>
<pre><code>parser = optparse.OptionParser()
# Set up your OptionParser
inp = raw_input("Enter some crap: ")
try: (options, args... | 8 | 2009-11-08T20:14:32Z | [
"python",
"optparse"
] |
Matching tags in BeautifulSoup | 1,697,774 | <p>I'm trying to count the number of tags in the 'soup' from a beautifulsoup result. I'd like to use a regular expression but am having trouble.
The code Ive tried is as follows:</p>
<pre><code>reg_exp_tag = re.compile("<[^>*>")
tags = re.findall(reg_exp_tag, soup(cast as a string))
</code></pre>
<p>but <cod... | 1 | 2009-11-08T20:12:59Z | 1,697,797 | <p>Shouldn't that be <code>"<[^>]*>"</code> instead of <code>"<[^>*>"</code>?</p>
<p>(the class needs to be closed with a <code>]</code>)</p>
| 1 | 2009-11-08T20:18:59Z | [
"python",
"regex",
"beautifulsoup"
] |
Matching tags in BeautifulSoup | 1,697,774 | <p>I'm trying to count the number of tags in the 'soup' from a beautifulsoup result. I'd like to use a regular expression but am having trouble.
The code Ive tried is as follows:</p>
<pre><code>reg_exp_tag = re.compile("<[^>*>")
tags = re.findall(reg_exp_tag, soup(cast as a string))
</code></pre>
<p>but <cod... | 1 | 2009-11-08T20:12:59Z | 1,697,825 | <p>If you've already parsed the HTML with BeautifulSoup, why parse it again? Try this:</p>
<pre><code>num_tags = len(soup.findAll())
</code></pre>
| 4 | 2009-11-08T20:27:24Z | [
"python",
"regex",
"beautifulsoup"
] |
How do you convert a Python time.struct_time object into a datetime object? | 1,697,815 | <p>How do you convert a Python <code>time.struct_time</code> object into a <code>datetime.datetime</code> object?</p>
<p>I have a library that provides the first one and a second library that wants the second one...</p>
| 220 | 2009-11-08T20:23:23Z | 1,697,838 | <p>Like this:</p>
<pre><code>>>> structTime = time.localtime()
>>> datetime.datetime(*structTime[:6])
datetime.datetime(2009, 11, 8, 20, 32, 35)
</code></pre>
| 99 | 2009-11-08T20:30:37Z | [
"python",
"datetime"
] |
How do you convert a Python time.struct_time object into a datetime object? | 1,697,815 | <p>How do you convert a Python <code>time.struct_time</code> object into a <code>datetime.datetime</code> object?</p>
<p>I have a library that provides the first one and a second library that wants the second one...</p>
| 220 | 2009-11-08T20:23:23Z | 1,697,907 | <p>Use <a href="http://docs.python.org/library/time.html#time.mktime">time.mktime()</a> to convert the time tuple (in localtime) into seconds since the Epoch, then use <a href="http://docs.python.org/library/datetime.html#datetime.datetime.fromtimestamp">datetime.fromtimestamp()</a> to get the datetime object.</p>
<pr... | 299 | 2009-11-08T20:57:16Z | [
"python",
"datetime"
] |
How do you convert a Python time.struct_time object into a datetime object? | 1,697,815 | <p>How do you convert a Python <code>time.struct_time</code> object into a <code>datetime.datetime</code> object?</p>
<p>I have a library that provides the first one and a second library that wants the second one...</p>
| 220 | 2009-11-08T20:23:23Z | 18,726,020 | <p>This is not a direct answer to your question (which was answered pretty well already). However, having had times bite me on the fundament several times, I cannot stress enough that it would behoove you to look closely at what your time.struct_time object is providing, vs. what other time fields may have.</p>
<p>Ass... | 24 | 2013-09-10T18:08:54Z | [
"python",
"datetime"
] |
How to create Django FormWizard for one Model? | 1,697,866 | <p>I have Django Model with many fields which user must fill. If I'll create one ModelForm for this Model it will be big enough for one form. I want to split it using FormWizard. I think it's possible first to create forms dynamically and then create FormWizard using them.</p>
<p>Is this good approach or is there any ... | 2 | 2009-11-08T20:41:18Z | 1,697,884 | <p>To me it seems fine.</p>
<p>The approach for creating partial forms is written in the <a href="http://docs.djangoproject.com/en/dev/topics/forms/modelforms/" rel="nofollow">docs.</a></p>
<p>In short:</p>
<pre><code>class PartialAuthorForm(ModelForm):
class Meta:
model = Author
fields = ('name'... | 2 | 2009-11-08T20:46:48Z | [
"python",
"django",
"django-forms",
"formwizard"
] |
Neural Networks in Python without using any readymade libraries...i.e., from first principles..help! | 1,698,017 | <p>I am trying to learn programming in python and am also working against a deadline for setting up a neural network which looks like it's going to feature multidirectional associative memory and recurrent connections among other things. While the mathematics for all these things can be accessed from various texts and ... | 3 | 2009-11-08T21:44:51Z | 1,698,063 | <p>I don't know anything about neural networks, but <a href="http://pyneurgen.sourceforge.net/recurrent.html" rel="nofollow">PyNeurGen</a> seems like a good resource.</p>
| 1 | 2009-11-08T21:55:47Z | [
"python",
"scipy",
"neural-network"
] |
Neural Networks in Python without using any readymade libraries...i.e., from first principles..help! | 1,698,017 | <p>I am trying to learn programming in python and am also working against a deadline for setting up a neural network which looks like it's going to feature multidirectional associative memory and recurrent connections among other things. While the mathematics for all these things can be accessed from various texts and ... | 3 | 2009-11-08T21:44:51Z | 1,698,110 | <p>If you're familiar with Matlab, check out the excellent Python libraries <strong>numpy</strong>, <strong>scipy</strong>, and <strong>matplotlib</strong>. Together, they provide the most commonly used subset of Matlab functions.</p>
| 4 | 2009-11-08T22:18:46Z | [
"python",
"scipy",
"neural-network"
] |
Neural Networks in Python without using any readymade libraries...i.e., from first principles..help! | 1,698,017 | <p>I am trying to learn programming in python and am also working against a deadline for setting up a neural network which looks like it's going to feature multidirectional associative memory and recurrent connections among other things. While the mathematics for all these things can be accessed from various texts and ... | 3 | 2009-11-08T21:44:51Z | 6,347,056 | <p>You might be interested in <a href="http://neurolab.googlecode.com" rel="nofollow">Neurolab</a> - neural networks in Python with an API like the Neural Networks Toolbox from MATLAB</p>
| 2 | 2011-06-14T16:46:29Z | [
"python",
"scipy",
"neural-network"
] |
Convert little endian string to integer | 1,698,036 | <p>I have read samples out of a wave file using the wave module, but it gives the samples as a string, it's out of wave so it's little endian (for example, <code>\x00</code>).</p>
<p>What is the easiest way to convert this into a python integer, or a numpy.int16 type? (It will eventually become a numpy.int16, so going... | 9 | 2009-11-08T21:49:20Z | 1,698,048 | <p>The <code>struct</code> module converts packed data to Python values, and vice-versa.</p>
<pre><code>>>> import struct
>>> struct.unpack("<h", "\x00\x05")
(1280,)
>>> struct.unpack("<h", "\x00\x06")
(1536,)
>>> struct.unpack("<h", "\x01\x06")
(1537,)
</code></pre>
<p>"h... | 16 | 2009-11-08T21:51:28Z | [
"python",
"numpy",
"wav"
] |
Convert little endian string to integer | 1,698,036 | <p>I have read samples out of a wave file using the wave module, but it gives the samples as a string, it's out of wave so it's little endian (for example, <code>\x00</code>).</p>
<p>What is the easiest way to convert this into a python integer, or a numpy.int16 type? (It will eventually become a numpy.int16, so going... | 9 | 2009-11-08T21:49:20Z | 1,698,160 | <p><a href="http://docs.python.org/library/struct.html"><code>struct</code></a> is fine if you have to convert one or a small number of 2-byte strings to integers, but <a href="http://docs.python.org/library/array.html?highlight=array#module-array"><code>array</code></a> and <code>numpy</code> itself are better options... | 10 | 2009-11-08T22:34:44Z | [
"python",
"numpy",
"wav"
] |
Selective merge of two or more data files | 1,698,188 | <p>I have an executable whose input is contained in an ASCII file with format:</p>
<pre><code>$ GENERAL INPUTS
$ PARAM1 = 123.456
PARAM2=456,789,101112
PARAM3(1)=123,456,789
PARAM4 =
1234,5678,91011E2
PARAM5(1,2)='STRING','STRING2'
$ NEW INSTANCE
NEW(1)=.TRUE.
PAR1=123
[More data here]
$ NEW INSTANCE
NEW(2)=.TRU... | 0 | 2009-11-08T22:42:36Z | 1,698,292 | <p>If you're already able to parse this format (I'd have tried it with pyParsing, but if you already have a working flexx/bison solution, that will be just fine), and the parsed data fit well in memory, then you're basically there. You can represent what you read from each file as a simple object with a dict for "gene... | 1 | 2009-11-08T23:21:08Z | [
"python",
"parsing",
"text-files",
"bison",
"flex-lexer"
] |
Pythonic way to find a regular expression match | 1,698,217 | <p>Is there a more succinct/correct/pythonic way to do the following:</p>
<pre><code>url = "http://0.0.0.0:3000/authenticate/login"
re_token = re.compile("<[^>]*authenticity_token[^>]*value=\"([^\"]*)")
for line in urllib2.urlopen(url):
if re_token.match(line):
token = re_token.findall(line)[0]
... | 2 | 2009-11-08T22:55:43Z | 1,698,247 | <p>Could you use <a href="http://www.crummy.com/software/BeautifulSoup/documentation.html" rel="nofollow">Beautiful Soup</a> for this? The code would essentially look something like so:</p>
<pre><code>from BeautifulSoup import BeautifulSoup
url = "hhttp://0.0.0.0:3000/authenticate/login"
page = urlli2b.urlopen(page)
s... | 6 | 2009-11-08T23:07:42Z | [
"regex",
"python"
] |
Pythonic way to find a regular expression match | 1,698,217 | <p>Is there a more succinct/correct/pythonic way to do the following:</p>
<pre><code>url = "http://0.0.0.0:3000/authenticate/login"
re_token = re.compile("<[^>]*authenticity_token[^>]*value=\"([^\"]*)")
for line in urllib2.urlopen(url):
if re_token.match(line):
token = re_token.findall(line)[0]
... | 2 | 2009-11-08T22:55:43Z | 1,698,249 | <p>You don't need the findall call. Instead use:</p>
<pre><code>m = re_token.match(line)
if m:
token = m.group(1)
....
</code></pre>
<p>I second the recommendation of BeautifulSoup over regular expressions though.</p>
| 1 | 2009-11-08T23:07:50Z | [
"regex",
"python"
] |
Pythonic way to find a regular expression match | 1,698,217 | <p>Is there a more succinct/correct/pythonic way to do the following:</p>
<pre><code>url = "http://0.0.0.0:3000/authenticate/login"
re_token = re.compile("<[^>]*authenticity_token[^>]*value=\"([^\"]*)")
for line in urllib2.urlopen(url):
if re_token.match(line):
token = re_token.findall(line)[0]
... | 2 | 2009-11-08T22:55:43Z | 1,698,374 | <p>there's nothing "pythonic" with using regex. If you don't want to use BeautifulSoup(which you should ideally), just use Python's excellent string manipulation capabilities</p>
<pre><code>for line in open("file"):
line=line.strip()
if "<input name" in line and "value=" in line:
item=line.split()
... | 1 | 2009-11-08T23:44:39Z | [
"regex",
"python"
] |
Pythonic way to find a regular expression match | 1,698,217 | <p>Is there a more succinct/correct/pythonic way to do the following:</p>
<pre><code>url = "http://0.0.0.0:3000/authenticate/login"
re_token = re.compile("<[^>]*authenticity_token[^>]*value=\"([^\"]*)")
for line in urllib2.urlopen(url):
if re_token.match(line):
token = re_token.findall(line)[0]
... | 2 | 2009-11-08T22:55:43Z | 1,698,728 | <p>As to why you shouldn't use regular expressions to search HTML, there are two main reasons.</p>
<p>The first is that HTML is defined recursively, and regular expressions, which compile into stackless state machines, don't do recursion. You can't write a regular expression that can tell, when it encounters an end t... | 0 | 2009-11-09T01:50:43Z | [
"regex",
"python"
] |
Scipy loadmat only load integers? | 1,698,223 | <p>It seems I can only load everything as uint8 type, just with the following two lines,</p>
<p>import scipy.io
X1=scipy.io.loadmat('one.mat')</p>
<p>all double precision numbers get transformed. I believe the creators of scipy
are aware of the fact that floating-point numbers are much more common...</p>
<p>So, what... | 1 | 2009-11-08T22:58:28Z | 1,698,304 | <p>What level matfile are you trying to read? According to <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html" rel="nofollow">the docs</a>,</p>
<blockquote>
<p>v4 (Level 1.0), v6 and v7 to 7.2
matfiles are supported.</p>
<p>You will need an HDF5 python library
to read matlab... | 1 | 2009-11-08T23:25:11Z | [
"python",
"scipy",
"mat-file"
] |
automate navigation of a JavaScript powered website | 1,698,285 | <p>I need to automate navigation around a JavaScript powered website in Python so I can scrape some content. I came across <a href="http://groups.csail.mit.edu/uid/chickenfoot/quickstart.html" rel="nofollow">Chickenfoot</a>, which is a FireFox extension that gives me a programming interface to the browser.</p>
<p>Do y... | 2 | 2009-11-08T23:18:27Z | 1,698,290 | <p>Check out <a href="http://seleniumhq.org/" rel="nofollow">Selenium</a>. </p>
| 2 | 2009-11-08T23:20:26Z | [
"javascript",
"python",
"website"
] |
automate navigation of a JavaScript powered website | 1,698,285 | <p>I need to automate navigation around a JavaScript powered website in Python so I can scrape some content. I came across <a href="http://groups.csail.mit.edu/uid/chickenfoot/quickstart.html" rel="nofollow">Chickenfoot</a>, which is a FireFox extension that gives me a programming interface to the browser.</p>
<p>Do y... | 2 | 2009-11-08T23:18:27Z | 1,698,468 | <p><a href="http://search.cpan.org/dist/WWW-Mechanize/" rel="nofollow">WWW::Mechanize</a> has several extensions/compatible replacements to handle JavaScript: WWW::Mechanize::FireFox, WWW::Mechanize::Plugin::JavaScript/WWW::Scripter::Plugin::JavaScript, Mozilla::Mechanize, Gtk2::WebKit::Mechanize, Win32::IE::Mechanize.... | 3 | 2009-11-09T00:21:13Z | [
"javascript",
"python",
"website"
] |
automate navigation of a JavaScript powered website | 1,698,285 | <p>I need to automate navigation around a JavaScript powered website in Python so I can scrape some content. I came across <a href="http://groups.csail.mit.edu/uid/chickenfoot/quickstart.html" rel="nofollow">Chickenfoot</a>, which is a FireFox extension that gives me a programming interface to the browser.</p>
<p>Do y... | 2 | 2009-11-08T23:18:27Z | 1,699,202 | <p>You can automate Internet Explorer pretty easily from either javascript (.js files) or any other language that can use COM (c#, perl etc)</p>
<p><a href="http://msdn.microsoft.com/en-us/library/aa752084%28VS.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa752084%28VS.85%29.aspx</a></p>
<p>Simp... | 2 | 2009-11-09T05:13:10Z | [
"javascript",
"python",
"website"
] |
automate navigation of a JavaScript powered website | 1,698,285 | <p>I need to automate navigation around a JavaScript powered website in Python so I can scrape some content. I came across <a href="http://groups.csail.mit.edu/uid/chickenfoot/quickstart.html" rel="nofollow">Chickenfoot</a>, which is a FireFox extension that gives me a programming interface to the browser.</p>
<p>Do y... | 2 | 2009-11-08T23:18:27Z | 6,276,258 | <p>check <a href="http://watin.org/" rel="nofollow">http://watin.org/</a>
it's like chickenfoot, but it also support ie. And it's free.</p>
| 0 | 2011-06-08T08:43:21Z | [
"javascript",
"python",
"website"
] |
win32com and PAMIE web page open timeout | 1,698,362 | <p>currently im making some crawler script,<br>one of problem is
sometimes if i open webpage with PAMIE,webpage can't open and hang forever.<br>
are there any method to close PAMIE's IE or win32com's IE ? <br>
such like if webpage didn't response or loading complete less than 10sec or so .<br>
thanks in advance</p>
| 0 | 2009-11-08T23:40:01Z | 1,698,371 | <p>I think what you are looking for is somewhere to set the timeout on your request. I would suggest looking into the documentation on PAMIE.</p>
| 0 | 2009-11-08T23:44:09Z | [
"python",
"time",
"multithreading",
"pamie"
] |
win32com and PAMIE web page open timeout | 1,698,362 | <p>currently im making some crawler script,<br>one of problem is
sometimes if i open webpage with PAMIE,webpage can't open and hang forever.<br>
are there any method to close PAMIE's IE or win32com's IE ? <br>
such like if webpage didn't response or loading complete less than 10sec or so .<br>
thanks in advance</p>
| 0 | 2009-11-08T23:40:01Z | 1,698,422 | <p>Just use, to initialize your PAMIE instance, <code>PAMIE(timeOut=100)</code> or whatever. The units of measure for <code>timeOut</code> are "tenths of a second" (!); the default is 3000 (300 seconds, i.e., 5 minutes); with <code>300</code> as I suggested, you'd time out after 10 seconds as you request.</p>
<p>(You... | 2 | 2009-11-08T23:58:19Z | [
"python",
"time",
"multithreading",
"pamie"
] |
What tools can be used to produce beautiful documentation for a Python project? | 1,698,376 | <p>I'm working on an open-source project called <a href="http://code.google.com/p/python-graph/" rel="nofollow">python-graph</a>. We've come to the point where our APIs are stabilising and we've realised that we need some user docs. We already have automatically generated <a href="http://www.linux.ime.usp.br/~matiello/... | 5 | 2009-11-08T23:45:27Z | 1,698,387 | <p>I have been very impressed by results from <a href="http://sphinx.pocoo.org"><strong>Sphinx</strong></a> which seems to be getting more popular.</p>
<p><em>Edit:</em> To make this more explicit, allow me to quote the key bullet points from the <a href="http://sphinx.pocoo.org"><strong>Sphinx home page</strong></a>:... | 25 | 2009-11-08T23:47:37Z | [
"python",
"documentation",
"latex"
] |
What tools can be used to produce beautiful documentation for a Python project? | 1,698,376 | <p>I'm working on an open-source project called <a href="http://code.google.com/p/python-graph/" rel="nofollow">python-graph</a>. We've come to the point where our APIs are stabilising and we've realised that we need some user docs. We already have automatically generated <a href="http://www.linux.ime.usp.br/~matiello/... | 5 | 2009-11-08T23:45:27Z | 1,698,392 | <p>How about <a href="http://sphinx.pocoo.org/" rel="nofollow">Sphinx</a>, which is used for python.org and many other applications?</p>
| 4 | 2009-11-08T23:48:13Z | [
"python",
"documentation",
"latex"
] |
What tools can be used to produce beautiful documentation for a Python project? | 1,698,376 | <p>I'm working on an open-source project called <a href="http://code.google.com/p/python-graph/" rel="nofollow">python-graph</a>. We've come to the point where our APIs are stabilising and we've realised that we need some user docs. We already have automatically generated <a href="http://www.linux.ime.usp.br/~matiello/... | 5 | 2009-11-08T23:45:27Z | 1,702,183 | <p>Dive into Python is a custom script. You can download the source from here: <a href="http://diveintopython.net/download/diveintopython-common-5.4.zip" rel="nofollow">http://diveintopython.net/download/diveintopython-common-5.4.zip</a></p>
<p>I think that Mark spent a great deal of time on the documentation though -... | 2 | 2009-11-09T16:36:21Z | [
"python",
"documentation",
"latex"
] |
What tools can be used to produce beautiful documentation for a Python project? | 1,698,376 | <p>I'm working on an open-source project called <a href="http://code.google.com/p/python-graph/" rel="nofollow">python-graph</a>. We've come to the point where our APIs are stabilising and we've realised that we need some user docs. We already have automatically generated <a href="http://www.linux.ime.usp.br/~matiello/... | 5 | 2009-11-08T23:45:27Z | 1,703,819 | <p>In regards to the "How did he do that?" question:
If the document is on the web in html, it is probably not LaTeX. LaTeX's end goal is printed documentation (pdf, dvi, or ps, for the most part). LaTeX source can be changed to HTML, but if you desire only HTML output, then LaTeX is certainly NOT the way to go.</p>
<... | 2 | 2009-11-09T21:07:50Z | [
"python",
"documentation",
"latex"
] |
What tools can be used to produce beautiful documentation for a Python project? | 1,698,376 | <p>I'm working on an open-source project called <a href="http://code.google.com/p/python-graph/" rel="nofollow">python-graph</a>. We've come to the point where our APIs are stabilising and we've realised that we need some user docs. We already have automatically generated <a href="http://www.linux.ime.usp.br/~matiello/... | 5 | 2009-11-08T23:45:27Z | 1,703,843 | <p>This other question/answer may also be of interest:</p>
<p><a href="http://stackoverflow.com/questions/58622/can-i-document-python-code-with-doxygen-and-does-it-make-sense" title="Can I document Python Code with Doxygen, and does it make sense?">Can I document Python Code with Doxygen, and does it make sense?</a></... | 2 | 2009-11-09T21:12:46Z | [
"python",
"documentation",
"latex"
] |
Redirect Embedded Python IO to a console created with AllocConsole | 1,698,439 | <p>I am having some trouble getting Python IO redirected to a console that I've allocated for my Win32 app. Is there a Python-specific stream that I need to redirect?</p>
<p>Here's more-or-less what I'm doing now (error checking removed, etc.):</p>
<pre><code>int __stdcall WinMain(/*Usual stuff here*/) {
// Creat... | 9 | 2009-11-09T00:07:44Z | 1,698,521 | <p>Set <code>sys.stdout</code> on the Python side (presumably to an <code>open('CONOUT$', 'wt')</code>) to make Python's <code>print</code> work, and similarly for <code>sys.stderr</code> and <code>sys.stdin</code>. (There are faster ways to make this happen from a C extension, but the simplest way is to just execute ... | 9 | 2009-11-09T00:42:11Z | [
"python",
"console",
"io-redirection"
] |
Redirect Embedded Python IO to a console created with AllocConsole | 1,698,439 | <p>I am having some trouble getting Python IO redirected to a console that I've allocated for my Win32 app. Is there a Python-specific stream that I need to redirect?</p>
<p>Here's more-or-less what I'm doing now (error checking removed, etc.):</p>
<pre><code>int __stdcall WinMain(/*Usual stuff here*/) {
// Creat... | 9 | 2009-11-09T00:07:44Z | 11,144,036 | <p>It is much easier to just tell the embedded python to redirect its output to a file. </p>
<p>Try this code:</p>
<pre><code>PyRun_SimpleString("import sys\n");
PyRun_SimpleString( "sys.stdout = sys.stderr = open(\"C:\\embedded_log_file.txt\", \"w\")\n" );
</code></pre>
| 1 | 2012-06-21T18:00:04Z | [
"python",
"console",
"io-redirection"
] |
Python: appengine urllib2 headers from a 302 | 1,698,487 | <p>A normal <code>urllib2</code> works fine:</p>
<pre><code>>>> import urllib2
>>> r = urllib2.urlopen(u"http://bit.ly/4ovTZw")
>>> r.geturl()
'http://www.writing.com/main/handler/action/show_document/item_id/933413.mp3'
>>> r.headers.get("Content-Type")
'audio/mpeg'
</code></pre>
... | 1 | 2009-11-09T00:32:22Z | 1,698,513 | <p>I know for a fact that AppEngine blacklists some addresses - check your response body for a hint. </p>
<p>It might also be the other way around - some services blacklist AppEngine... I am not sure. I remember in the early days of GAE, accessing Delicious was not possible through AppEngine.</p>
| 0 | 2009-11-09T00:39:33Z | [
"python",
"google-app-engine",
"header",
"urllib2"
] |
Python: appengine urllib2 headers from a 302 | 1,698,487 | <p>A normal <code>urllib2</code> works fine:</p>
<pre><code>>>> import urllib2
>>> r = urllib2.urlopen(u"http://bit.ly/4ovTZw")
>>> r.geturl()
'http://www.writing.com/main/handler/action/show_document/item_id/933413.mp3'
>>> r.headers.get("Content-Type")
'audio/mpeg'
</code></pre>
... | 1 | 2009-11-09T00:32:22Z | 5,285,369 | <p>I have just tried to call that specifi url via <a href="http://shell.appspot.com/" rel="nofollow">Interactive Shell</a>; it works for me using the <a href="http://code.google.com/appengine/docs/python/urlfetch/overview.html" rel="nofollow">urlfetch</a> function.</p>
<pre><code>Google App Engine/1.4.2
Python 2.5.2 (... | 0 | 2011-03-12T20:42:49Z | [
"python",
"google-app-engine",
"header",
"urllib2"
] |
XCode Target Phase Python Script | 1,698,494 | <p>I am trying to add a Python script to into my project to obtain the build and marketing numbers directly from Git.</p>
<p>I have created a new target phase and that runs a script as explained in:<br>
<a href="http://yeahrightkeller.com/2008/10/19/xcode-run-script-build-phase-tip/" rel="nofollow">http://yeahrightkel... | 5 | 2009-11-09T00:34:35Z | 1,698,773 | <p>Apple distributes Python 2.5 and 2.6 with Snow Leopard (10.6) and both are built with a deployment target of 10.6. If you really do need to target your application for 10.5, you can't safely use the Apple-supplied Pythons on 10.6 to deploy on 10.5.</p>
<p>The easiest solution may be to download and install a <a hr... | 1 | 2009-11-09T02:09:42Z | [
"iphone",
"python",
"xcode"
] |
XCode Target Phase Python Script | 1,698,494 | <p>I am trying to add a Python script to into my project to obtain the build and marketing numbers directly from Git.</p>
<p>I have created a new target phase and that runs a script as explained in:<br>
<a href="http://yeahrightkeller.com/2008/10/19/xcode-run-script-build-phase-tip/" rel="nofollow">http://yeahrightkel... | 5 | 2009-11-09T00:34:35Z | 2,152,133 | <p>For the purposes of this script, you can simply temporarily set the value of MACOSX_DEPLOYMENT_TARGET to 10.6. So your command would be:</p>
<pre><code>env MACOSX_DEPLOYMENT_TARGET=10.6 gitversion.py
</code></pre>
| 1 | 2010-01-28T03:37:03Z | [
"iphone",
"python",
"xcode"
] |
XCode Target Phase Python Script | 1,698,494 | <p>I am trying to add a Python script to into my project to obtain the build and marketing numbers directly from Git.</p>
<p>I have created a new target phase and that runs a script as explained in:<br>
<a href="http://yeahrightkeller.com/2008/10/19/xcode-run-script-build-phase-tip/" rel="nofollow">http://yeahrightkel... | 5 | 2009-11-09T00:34:35Z | 2,294,129 | <p>I have the same problem, and I wanted to keep the python version because handling the plist via <code>NSMutableDictionary</code> is much nicer than using regex on <code>Info.plist</code>.</p>
<p>Building on Maxence's answer, the solution was to strip the python code out of the Run Script build phase into a <code>gi... | 2 | 2010-02-19T04:50:54Z | [
"iphone",
"python",
"xcode"
] |
XCode Target Phase Python Script | 1,698,494 | <p>I am trying to add a Python script to into my project to obtain the build and marketing numbers directly from Git.</p>
<p>I have created a new target phase and that runs a script as explained in:<br>
<a href="http://yeahrightkeller.com/2008/10/19/xcode-run-script-build-phase-tip/" rel="nofollow">http://yeahrightkel... | 5 | 2009-11-09T00:34:35Z | 7,660,258 | <p>Here you can find ths SOLUTION:</p>
<p><a href="http://marcocattai.posterous.com/xcode-project-version-number-using-gitsvn" rel="nofollow">http://marcocattai.posterous.com/xcode-project-version-number-using-gitsvn</a></p>
| 0 | 2011-10-05T10:59:22Z | [
"iphone",
"python",
"xcode"
] |
What is meant by 2D array support? | 1,698,553 | <p>I read that Python does not actually support 2D arrays but rather an array of an array. I understand the array of an array thing but what does it mean by supporting 2D arrays?</p>
<p>In C a 2D array is simply converted to a 1D array by doing some fancy math (<a href="http://stackoverflow.com/questions/1242705/perf... | 8 | 2009-11-09T00:49:49Z | 1,698,567 | <p>There are languages that implement 2D (or 3D, etc) arrays. Fortran is one of them. It means you can write an array index expression like <code>array[x,y]</code> and the language will take care of the math to find the correct element.</p>
<p>Also, <a href="http://numpy.scipy.org/" rel="nofollow">Numpy</a> is a num... | 10 | 2009-11-09T00:53:35Z | [
"python",
"c",
"arrays",
"multidimensional-array"
] |
What is meant by 2D array support? | 1,698,553 | <p>I read that Python does not actually support 2D arrays but rather an array of an array. I understand the array of an array thing but what does it mean by supporting 2D arrays?</p>
<p>In C a 2D array is simply converted to a 1D array by doing some fancy math (<a href="http://stackoverflow.com/questions/1242705/perf... | 8 | 2009-11-09T00:49:49Z | 1,698,569 | <p>Fortran has real multi-dimensional arrays. <a href="http://www.obliquity.com/computer/fortran/array.html" rel="nofollow">This link</a> talks about them in the FORTRAN 77 case.</p>
| 1 | 2009-11-09T00:53:50Z | [
"python",
"c",
"arrays",
"multidimensional-array"
] |
What is meant by 2D array support? | 1,698,553 | <p>I read that Python does not actually support 2D arrays but rather an array of an array. I understand the array of an array thing but what does it mean by supporting 2D arrays?</p>
<p>In C a 2D array is simply converted to a 1D array by doing some fancy math (<a href="http://stackoverflow.com/questions/1242705/perf... | 8 | 2009-11-09T00:49:49Z | 1,698,573 | <p>C# has multi-dimensional <a href="http://msdn.microsoft.com/en-us/library/aa288453.aspx" rel="nofollow">arrays</a>, but they are recommended against as jagged arrays (arrays of arrays) are more performant.</p>
| 2 | 2009-11-09T00:54:34Z | [
"python",
"c",
"arrays",
"multidimensional-array"
] |
What is meant by 2D array support? | 1,698,553 | <p>I read that Python does not actually support 2D arrays but rather an array of an array. I understand the array of an array thing but what does it mean by supporting 2D arrays?</p>
<p>In C a 2D array is simply converted to a 1D array by doing some fancy math (<a href="http://stackoverflow.com/questions/1242705/perf... | 8 | 2009-11-09T00:49:49Z | 1,698,589 | <p>I guess 'support of 2D arrays' means how the language allows you to access the data as if it was a 2D array. In c the fancy math is hidden by array constructs:</p>
<pre><code>char arr[5][5];
char c = arr[2][3];
</code></pre>
<p>There is a way to do this by pointer arithmetics as well (which you probably refer as f... | 1 | 2009-11-09T00:57:39Z | [
"python",
"c",
"arrays",
"multidimensional-array"
] |
What is meant by 2D array support? | 1,698,553 | <p>I read that Python does not actually support 2D arrays but rather an array of an array. I understand the array of an array thing but what does it mean by supporting 2D arrays?</p>
<p>In C a 2D array is simply converted to a 1D array by doing some fancy math (<a href="http://stackoverflow.com/questions/1242705/perf... | 8 | 2009-11-09T00:49:49Z | 1,698,650 | <p>Somebody already mentioned <code>numpy</code>, and the OP commented that "it's back to the fancy pointer math with C arrays" -- that's a totally trivial implementation detail! Since the underlying memory in a (normal;-) computer can be seen as an array of bytes (or words), well then <strong>of course</strong> any d... | 6 | 2009-11-09T01:22:52Z | [
"python",
"c",
"arrays",
"multidimensional-array"
] |
What is meant by 2D array support? | 1,698,553 | <p>I read that Python does not actually support 2D arrays but rather an array of an array. I understand the array of an array thing but what does it mean by supporting 2D arrays?</p>
<p>In C a 2D array is simply converted to a 1D array by doing some fancy math (<a href="http://stackoverflow.com/questions/1242705/perf... | 8 | 2009-11-09T00:49:49Z | 1,698,659 | <p>Actual <em>arrays</em> in Python - that is, the <code>array</code> object created by the <code>array</code> module - are strictly one-dimensional. They're also something you probably won't use unless your code has to interoperate with C data structures, you're working with huge quantities of data that must be kept ... | 1 | 2009-11-09T01:26:59Z | [
"python",
"c",
"arrays",
"multidimensional-array"
] |
What is meant by 2D array support? | 1,698,553 | <p>I read that Python does not actually support 2D arrays but rather an array of an array. I understand the array of an array thing but what does it mean by supporting 2D arrays?</p>
<p>In C a 2D array is simply converted to a 1D array by doing some fancy math (<a href="http://stackoverflow.com/questions/1242705/perf... | 8 | 2009-11-09T00:49:49Z | 1,951,095 | <p>Rosetta Code has Python examples for <a href="http://rosettacode.org/wiki/Two-dimensional%5Farray%5F%28runtime%29" rel="nofollow">creating a two dimensional array</a>.</p>
| 1 | 2009-12-23T06:54:24Z | [
"python",
"c",
"arrays",
"multidimensional-array"
] |
How can I traverse a file system with a generator? | 1,698,596 | <p>I'm trying to create a utility class for traversing all the files in a directory, including those within subdirectories and sub-subdirectories. I tried to use a generator because generators are cool; however, I hit a snag.</p>
<pre><code>
def grab_files(directory):
for name in os.listdir(directory):
ful... | 12 | 2009-11-09T01:00:46Z | 1,698,611 | <p>Why reinvent the wheel when you can use <a href="http://docs.python.org/library/os.html#os.walk">os.walk</a></p>
<pre><code>import os
for root, dirs, files in os.walk(path):
for name in files:
print os.path.join(root, name)
</code></pre>
<p>os.walk is a generator that yields the file names in a directo... | 39 | 2009-11-09T01:07:34Z | [
"python",
"recursion",
"iterator",
"generator",
"yield"
] |
How can I traverse a file system with a generator? | 1,698,596 | <p>I'm trying to create a utility class for traversing all the files in a directory, including those within subdirectories and sub-subdirectories. I tried to use a generator because generators are cool; however, I hit a snag.</p>
<pre><code>
def grab_files(directory):
for name in os.listdir(directory):
ful... | 12 | 2009-11-09T01:00:46Z | 1,698,624 | <p>You can use <a href="http://pypi.python.org/pypi/path.py/2.2" rel="nofollow">path.py</a>. Unfortunately the author's website is no longer around, but you can still download the code from PyPI. This library is a wrapper around path functions in the <code>os</code> module.</p>
<p><code>path.py</code> provides a <code... | 0 | 2009-11-09T01:10:51Z | [
"python",
"recursion",
"iterator",
"generator",
"yield"
] |
How can I traverse a file system with a generator? | 1,698,596 | <p>I'm trying to create a utility class for traversing all the files in a directory, including those within subdirectories and sub-subdirectories. I tried to use a generator because generators are cool; however, I hit a snag.</p>
<pre><code>
def grab_files(directory):
for name in os.listdir(directory):
ful... | 12 | 2009-11-09T01:00:46Z | 1,698,711 | <p>I agree with the os.walk solution</p>
<p>For pure pedantic purpose, try iterate over the generator object, instead of returning it directly:</p>
<pre><code>
def grab_files(directory):
for name in os.listdir(directory):
full_path = os.path.join(directory, name)
if os.path.isdir(full_path):
... | 7 | 2009-11-09T01:43:35Z | [
"python",
"recursion",
"iterator",
"generator",
"yield"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.