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
Basic Python loop question - problem reading a list from a text file
1,703,471
<p>I'm trying to read a list of items from a text file and format with square brackets and separators like this: ['item1','item2', .... 'last_item'] but I'm having trouble with the beginning and end item for which I always get: ...,'last_item','], so I do not want the last ,' to be there.</p> <p>In python I've write...
0
2009-11-09T20:16:20Z
1,703,550
<pre><code>def do(filepath): out = [] for line in open(filepath, 'r'): out.append("'" + line.strip() + "'") return out.__str__() </code></pre>
0
2009-11-09T20:27:03Z
[ "python", "loops" ]
Parsing date/time string with timezone abbreviated name in Python?
1,703,546
<p>I'm trying to parse timestamp strings like <code>"Sat, 11/01/09 8:00PM EST"</code> in Python, but I'm having trouble finding a solution that will handle the abbreviated timezone.</p> <p>I'm using <a href="http://labix.org/python-dateutil"><code>dateutil</code></a>'s <code>parse()</code> function, but it doesn't par...
21
2009-11-09T20:26:32Z
1,703,591
<p>That probably won't work because those abbreviations aren't unique. See <a href="http://www.timeanddate.com/library/abbreviations/timezones/">this page</a> for details. You might wind up just having to manually handle it yourself if you're working with a known set of inputs.</p>
10
2009-11-09T20:32:51Z
[ "python", "date", "timezone" ]
Parsing date/time string with timezone abbreviated name in Python?
1,703,546
<p>I'm trying to parse timestamp strings like <code>"Sat, 11/01/09 8:00PM EST"</code> in Python, but I'm having trouble finding a solution that will handle the abbreviated timezone.</p> <p>I'm using <a href="http://labix.org/python-dateutil"><code>dateutil</code></a>'s <code>parse()</code> function, but it doesn't par...
21
2009-11-09T20:26:32Z
1,704,114
<p>The parse() function in dateutil can't handle time zones. The thing I've been using is the %Z formatter and the time.strptime() function. I have no idea how it deals with the ambiguity in time zones, but it seems to tell the difference between CDT and CST, which is all I needed.</p> <p>Background: I store backup ...
5
2009-11-09T21:49:44Z
[ "python", "date", "timezone" ]
Parsing date/time string with timezone abbreviated name in Python?
1,703,546
<p>I'm trying to parse timestamp strings like <code>"Sat, 11/01/09 8:00PM EST"</code> in Python, but I'm having trouble finding a solution that will handle the abbreviated timezone.</p> <p>I'm using <a href="http://labix.org/python-dateutil"><code>dateutil</code></a>'s <code>parse()</code> function, but it doesn't par...
21
2009-11-09T20:26:32Z
4,766,400
<p><code>dateutil</code>'s <code>parser.parse()</code> accepts as keyword argument <code>tzinfos</code> a dictionary of the kind <code>{'EST': -5*3600}</code> (that is, matching the zone name to GMT offset in seconds). So assuming we have that, we can do:</p> <pre><code>&gt;&gt;&gt; import dateutil.parser as dp &gt;&g...
45
2011-01-22T05:46:00Z
[ "python", "date", "timezone" ]
Parsing date/time string with timezone abbreviated name in Python?
1,703,546
<p>I'm trying to parse timestamp strings like <code>"Sat, 11/01/09 8:00PM EST"</code> in Python, but I'm having trouble finding a solution that will handle the abbreviated timezone.</p> <p>I'm using <a href="http://labix.org/python-dateutil"><code>dateutil</code></a>'s <code>parse()</code> function, but it doesn't par...
21
2009-11-09T20:26:32Z
4,773,905
<p>You might try pytz module: <a href="http://pytz.sourceforge.net/">http://pytz.sourceforge.net/</a></p> <blockquote> <p>pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.3 or higher. It also solves the issue of ambiguous ti...
9
2011-01-23T12:55:09Z
[ "python", "date", "timezone" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
1,703,705
<p>Well, the simplest way would be to constantly read from the file, check what's new and test for hits.</p> <pre><code>import time def watch(fn, words): fp = open(fn, 'r') while True: new = fp.readline() # Once all lines are read this just returns '' # until the file changes and a new...
10
2009-11-09T20:49:34Z
[ "python", "file", "tail" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
1,703,716
<p>To my knowledge there's no equivalent to "tail" in the Python function list. Solution would be to use tell() (get file size) and read() to work out the ending lines.</p> <p>This blog post (not by me) has the function written out, looks appropriate to me! <a href="http://www.manugarg.com/2007/04/real-tailing-in-pyth...
0
2009-11-09T20:51:07Z
[ "python", "file", "tail" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
1,703,740
<p>If you can't constraint the problem to work for a line-based read, you need to resort to blocks.</p> <p>This should work:</p> <pre><code>import sys needle = "needle" blocks = [] inf = sys.stdin if len(sys.argv) == 2: inf = open(sys.argv[1]) while True: block = inf.read() blocks.append(block) i...
1
2009-11-09T20:54:08Z
[ "python", "file", "tail" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
1,703,751
<p>EDIT: as the comment below notes, <code>O_NONBLOCK</code> doesn't work for files on disk. This will still help if anyone else comes along looking to tail data coming from a socket or named pipe or another process, but it <strong>doesn't answer the actual question that was asked</strong>. Original answer remains be...
2
2009-11-09T20:55:27Z
[ "python", "file", "tail" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
1,703,878
<p>You can use <code>collections.deque</code> to implement tail.</p> <p>From <a href="http://docs.python.org/library/collections.html#deque-recipes" rel="nofollow">http://docs.python.org/library/collections.html#deque-recipes</a> ...</p> <pre><code>def tail(filename, n=10): 'Return the last n lines of a file' ...
2
2009-11-09T21:17:33Z
[ "python", "file", "tail" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
1,703,997
<pre><code>def tail(f): f.seek(0, 2) while True: line = f.readline() if not line: time.sleep(0.1) continue yield line def process_matches(matchtext): while True: line = (yield) if matchtext in line: do_something_useful() # ema...
3
2009-11-09T21:33:05Z
[ "python", "file", "tail" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
1,704,004
<p>You can use select to poll for new contents in a file.</p> <pre><code>def tail(filename, bufsize = 1024): fds = [ os.open(filename, os.O_RDONLY) ] while True: reads, _, _ = select.select(fds, [], []) if 0 &lt; len(reads): yield os.read(reads[0], bufsize) </code></pre>
2
2009-11-09T21:33:53Z
[ "python", "file", "tail" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
18,747,793
<p>Looks like there's a package for that: <a href="https://github.com/kasun/python-tail" rel="nofollow">https://github.com/kasun/python-tail</a></p>
2
2013-09-11T17:38:24Z
[ "python", "file", "tail" ]
How to implement a pythonic equivalent of tail -F?
1,703,640
<p>What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords?</p> <p>In shell I might say:</p> <pre><code>tail -f "$file" | grep "$string" | while read hit; do #stuff done </code></pre>
14
2009-11-09T20:40:36Z
29,075,223
<p>you can use <a href="https://pypi.python.org/pypi/pytailf/1.0" rel="nofollow">pytailf</a> : Simple python tail -f wrapper</p> <pre><code>from tailf import tailf for line in tailf("myfile.log"): print line </code></pre>
1
2015-03-16T11:02:49Z
[ "python", "file", "tail" ]
A "safe" python HTML text formatting (ala textile)
1,703,678
<p>I've been looking around for an existing python library in the style of textile to format text for users to enter.</p> <p>If it was just me entering it, just textile would have been fine, but since the input is meant for a django app that will take user input and display it, while still maintaining some formatting....
3
2009-11-09T20:45:28Z
1,703,727
<p>Did you try the included <a href="http://docs.djangoproject.com/en/dev/ref/contrib/#markup" rel="nofollow">django.contrib.markup</a> libraries?</p>
1
2009-11-09T20:52:34Z
[ "python", "django", "formatting", "textile" ]
A "safe" python HTML text formatting (ala textile)
1,703,678
<p>I've been looking around for an existing python library in the style of textile to format text for users to enter.</p> <p>If it was just me entering it, just textile would have been fine, but since the input is meant for a django app that will take user input and display it, while still maintaining some formatting....
3
2009-11-09T20:45:28Z
1,703,739
<p>If you're using Django, you could try safe markdown:</p> <pre><code>{% load markup %} {{ foo|markdown:"safe" }} </code></pre> <p>You'll need to have markdown installed, and <code>django.contrib.markup</code> in your <code>settings.py</code> apps.</p> <p>If you want to sanitize HTML on save, I've had good luck us...
6
2009-11-09T20:54:03Z
[ "python", "django", "formatting", "textile" ]
A "safe" python HTML text formatting (ala textile)
1,703,678
<p>I've been looking around for an existing python library in the style of textile to format text for users to enter.</p> <p>If it was just me entering it, just textile would have been fine, but since the input is meant for a django app that will take user input and display it, while still maintaining some formatting....
3
2009-11-09T20:45:28Z
4,820,442
<p>If you are looking for a textile solution: the PyTextile that django markup uses actually has a textile_restricted() function, which for some reason, never made it into django.contrib.markup. You can use this function to provide restricted textile. Either by adding a method to your model that calls textile_restricte...
2
2011-01-27T18:43:57Z
[ "python", "django", "formatting", "textile" ]
python: xml.etree.ElementTree, removing "namespaces"
1,703,882
<p>I like the way ElementTree parses xml, in particular the Xpath feature. I've an output in xml from an application with nested tags.</p> <p>I'd like to access this tags by name without specifying the namespace, is it possible? For example:</p> <pre><code>root.findall("/molpro/job") </code></pre> <p>instead of:</p>...
25
2009-11-09T21:17:47Z
1,704,169
<p>At least with lxml2, it's possible to reduce this overhead somewhat:</p> <pre><code>root.findall("/n:molpro/n:job", namespaces=dict(n="http://www.molpro.net/schema/molpro2006")) </code></pre>
9
2009-11-09T21:58:41Z
[ "python", "xml" ]
python: xml.etree.ElementTree, removing "namespaces"
1,703,882
<p>I like the way ElementTree parses xml, in particular the Xpath feature. I've an output in xml from an application with nested tags.</p> <p>I'd like to access this tags by name without specifying the namespace, is it possible? For example:</p> <pre><code>root.findall("/molpro/job") </code></pre> <p>instead of:</p>...
25
2009-11-09T21:17:47Z
1,704,596
<p>You could write your own function to wrap the nasty looking bits for example:</p> <pre><code>def my_xpath(doc, ns, xp); num = xp.count('/') new_xp = xp.replace('/', '/{%s}') ns_tup = (ns,) * num doc.findall(new_xp % ns_tup) namespace = 'http://www.molpro.net/schema/molpro2006' my_xpath(root, namesp...
5
2009-11-09T23:11:07Z
[ "python", "xml" ]
Commenting JavaScript functions á la Python Docstrings
1,703,888
<p>It is valid JavaScript to write something like this:</p> <pre><code>function example(x) { "Here is a short doc what I do."; // code of the function } </code></pre> <p>The string actually does nothing. Is there any reason, why one shouldn't comment his/her functions in JavaScript in this way?</p> <p>Two po...
14
2009-11-09T21:18:49Z
1,703,923
<p>There's really no point in doing this in Javascript. In Python, the string is made available as the <code>__doc__</code> member of the function, class, or module. So these docstrings are available for introspection, etc.</p> <p>If you create strings like this in Javascript, you get no benefit over using a comment...
12
2009-11-09T21:23:35Z
[ "javascript", "python", "comments", "docstring" ]
Commenting JavaScript functions á la Python Docstrings
1,703,888
<p>It is valid JavaScript to write something like this:</p> <pre><code>function example(x) { "Here is a short doc what I do."; // code of the function } </code></pre> <p>The string actually does nothing. Is there any reason, why one shouldn't comment his/her functions in JavaScript in this way?</p> <p>Two po...
14
2009-11-09T21:18:49Z
24,588,123
<p>I was looking for a way to add multi-line strings to my code without littering it with \n's. It looks like this module fits the bill: <a href="https://github.com/monolithed/__doc__" rel="nofollow">https://github.com/monolithed/<strong>doc</strong></a></p> <p>Unfortunately, the comments won't survive minification, b...
1
2014-07-05T15:53:52Z
[ "javascript", "python", "comments", "docstring" ]
Boost::Python Windows 7 64 Bit
1,704,046
<p>I can't seem to build boost::python correctly on my Windows 7 64Bit machine. It's vanilla 32bit python 2.6.4 in the c:\Python26 directory.</p> <p>Pastebin is here of the debug build output : <a href="http://pastebin.com/m7d70f13e">http://pastebin.com/m7d70f13e</a></p> <p>Cheers,</p> <p>Al</p>
5
2009-11-09T21:40:57Z
1,737,887
<p>On IRC, we have found the following procedure fixes the problem:</p> <ol> <li>Open tools/build/v2/tools/python.jam</li> <li><p>There, locate the following code:</p> <pre><code>if [ version.check-jam-version 3 1 17 ] || ( [ os.name ] != NT ) { # Prior to version 3.1.17 Boost Jam's SHELL command did not support ...
6
2009-11-15T15:47:21Z
[ "python", "boost", "windows-7", "64bit" ]
Python (and Django) best import practices
1,704,058
<p>Out of the various ways to import code, are there some ways that are preferable to use, compared to others? This link <a href="http://effbot.org/zone/import-confusion.htm">http://effbot.org/zone/import-confusion.htm</a> in short states that</p> <pre><code>from foo.bar import MyClass </code></pre> <p>is not the pre...
18
2009-11-09T21:42:35Z
1,704,112
<p>The advantage of the latter is that the origin of MyClass is more explicit. The former puts MyClass in the current namespace so the code can just use MyClass unqualified. So it's less obvious to someone reading the code where MyClass is defined.</p>
2
2009-11-09T21:49:24Z
[ "python", "django", "python-import" ]
Python (and Django) best import practices
1,704,058
<p>Out of the various ways to import code, are there some ways that are preferable to use, compared to others? This link <a href="http://effbot.org/zone/import-confusion.htm">http://effbot.org/zone/import-confusion.htm</a> in short states that</p> <pre><code>from foo.bar import MyClass </code></pre> <p>is not the pre...
18
2009-11-09T21:42:35Z
1,704,156
<p>First, and primary, rule of imports: never ever use <code>from foo import *</code>.</p> <p>The article is discussing the issue of cyclical imports, which still exists today in poorly-structured code. I dislike cyclical imports; their presence is a strong sign that some module is doing too much, and needs to be spli...
12
2009-11-09T21:56:50Z
[ "python", "django", "python-import" ]
Python (and Django) best import practices
1,704,058
<p>Out of the various ways to import code, are there some ways that are preferable to use, compared to others? This link <a href="http://effbot.org/zone/import-confusion.htm">http://effbot.org/zone/import-confusion.htm</a> in short states that</p> <pre><code>from foo.bar import MyClass </code></pre> <p>is not the pre...
18
2009-11-09T21:42:35Z
1,704,262
<p>For me, it's dependent on the situation. If it's a uniquely named method/class (i.e., not <code>process()</code> or something like that), and you're going to use it a <em>lot</em>, then save typing and just do <code>from foo import MyClass</code>. </p> <p>If you're importing multiple things from one module, it's pr...
5
2009-11-09T22:10:43Z
[ "python", "django", "python-import" ]
Python threading test not working
1,704,085
<p><strong>EDIT</strong></p> <p>I solved the issue by forking the process instead of using threads. From the comments and links in the comments, I don't think threading is the right move here.</p> <p>Thanks everyone for your assistance.</p> <p><strong>FINISHED EDIT</strong></p> <p>I haven't done much with threading...
1
2009-11-09T21:46:08Z
1,704,175
<p>The MySQL DB driver isn't thread safe. You're using the same cursor concurrently from all threads.</p> <p>Try creating a new connection in each thread, or create a pool of connections that the threads can use (e.g. keep them in a <code>Queue</code>, each thread <code>get</code>s a connection, and <code>put</code>s ...
8
2009-11-09T21:59:20Z
[ "python", "multithreading" ]
Python threading test not working
1,704,085
<p><strong>EDIT</strong></p> <p>I solved the issue by forking the process instead of using threads. From the comments and links in the comments, I don't think threading is the right move here.</p> <p>Thanks everyone for your assistance.</p> <p><strong>FINISHED EDIT</strong></p> <p>I haven't done much with threading...
1
2009-11-09T21:46:08Z
1,704,181
<p>You should be constructing and populating your queue first. When the entire queue is constructed and populated, then you should construct a number of threads which then, each in a loop, polls the queue and processes an item on the queue.</p>
0
2009-11-09T21:59:59Z
[ "python", "multithreading" ]
Python threading test not working
1,704,085
<p><strong>EDIT</strong></p> <p>I solved the issue by forking the process instead of using threads. From the comments and links in the comments, I don't think threading is the right move here.</p> <p>Thanks everyone for your assistance.</p> <p><strong>FINISHED EDIT</strong></p> <p>I haven't done much with threading...
1
2009-11-09T21:46:08Z
1,704,216
<p>You realize Python doesn't do true multi-threading as you would expect on a multi-core processor:</p> <p><a href="http://en.wikipedia.org/wiki/Global%5FInterpreter%5FLock" rel="nofollow">See Here</a> <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">And Here</a></p> <p>Don...
0
2009-11-09T22:04:56Z
[ "python", "multithreading" ]
Can I use Django 1.1 with django-search-lucene for full-text searching, and if so, what resources/links/docs can I reference to get it up and running?
1,704,278
<p>A little background:</p> <ul> <li>I want to use <a href="http://code.google.com/p/django-search-lucene/" rel="nofollow">Django Search with Lucene</a></li> <li>I have Django 1.1 w/ Python 2.5 installed</li> <li>MySQL 5.1 is being used</li> <li>My local machine is running Windows Vista x64, but we will deploy to <a h...
1
2009-11-09T22:13:26Z
1,705,393
<p>I would recommend <a href="http://lucene.apache.org/solr/" rel="nofollow">Apache SOLR</a>, which is built on top of Lucene. The primary advantage is that it exposes an easy to use API, and can return a native Python object. Here is an example of how to call it from Python:</p> <pre><code>params = urllib.urlencode({...
3
2009-11-10T03:00:10Z
[ "python", "django", "django-search-lucene" ]
Is there a simple process-based parallel map for python?
1,704,401
<p>I'm looking for a simple process-based parallel map for python, that is, a function</p> <pre><code>parmap(function,[data]) </code></pre> <p>that would run function on each element of [data] on a different process (well, on a different core, but AFAIK, the only way to run stuff on different cores in python is to st...
42
2009-11-09T22:33:42Z
1,704,501
<p>I seems like what you need is the <a href="http://docs.python.org/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool.map">map method in multiprocessing.Pool()</a>:</p> <blockquote> <p><strong>map(func, iterable[, chunksize])</strong></p> <pre><code>A parallel equivalent of the map() built-in ...
72
2009-11-09T22:49:55Z
[ "python", "parallel-processing", "smp" ]
Get uncompressed size of a .gz file in python
1,704,458
<p>Using gzip, tell() returns the offset in the uncompressed file.<br> In order to show a progress bar, I want to know the original (uncompressed) size of the file.<br> Is there an easy way to find out? </p>
7
2009-11-09T22:43:45Z
1,704,485
<p>Unix way: use "gunzip -l file.gz" via subprocess.call / os.popen, capture and parse its output.</p>
4
2009-11-09T22:47:22Z
[ "python", "gzip" ]
Get uncompressed size of a .gz file in python
1,704,458
<p>Using gzip, tell() returns the offset in the uncompressed file.<br> In order to show a progress bar, I want to know the original (uncompressed) size of the file.<br> Is there an easy way to find out? </p>
7
2009-11-09T22:43:45Z
1,704,516
<p>Looking at the source for the <code>gzip</code> module, I see that the underlying file object for <code>GzipFile</code> seems to be <code>fileobj</code>. So:</p> <pre><code>mygzipfile = gzip.GzipFile() ... mygzipfile.fileobj.tell() </code></pre> <p>? </p> <p>Maybe it would be good to do some sanity checking bef...
0
2009-11-09T22:52:56Z
[ "python", "gzip" ]
Get uncompressed size of a .gz file in python
1,704,458
<p>Using gzip, tell() returns the offset in the uncompressed file.<br> In order to show a progress bar, I want to know the original (uncompressed) size of the file.<br> Is there an easy way to find out? </p>
7
2009-11-09T22:43:45Z
1,704,537
<p>The last 4 bytes of the .gz hold the original size of the file</p>
4
2009-11-09T22:57:09Z
[ "python", "gzip" ]
Get uncompressed size of a .gz file in python
1,704,458
<p>Using gzip, tell() returns the offset in the uncompressed file.<br> In order to show a progress bar, I want to know the original (uncompressed) size of the file.<br> Is there an easy way to find out? </p>
7
2009-11-09T22:43:45Z
1,704,576
<p>The <a href="http://www.gzip.org/zlib/rfc-gzip.html#header-trailer">gzip format</a> specifies a field called <code>ISIZE</code> that:</p> <blockquote> <p>This contains the size of the original (uncompressed) input data modulo 2^32.</p> </blockquote> <p>In <a href="http://pydoc.org/get.cgi/usr/local/lib/python2.5...
8
2009-11-09T23:07:49Z
[ "python", "gzip" ]
Get uncompressed size of a .gz file in python
1,704,458
<p>Using gzip, tell() returns the offset in the uncompressed file.<br> In order to show a progress bar, I want to know the original (uncompressed) size of the file.<br> Is there an easy way to find out? </p>
7
2009-11-09T22:43:45Z
1,750,299
<p>GzipFile.size stores the uncompressed size, but it's only incremented when you read the file, so you should prefer len(fd.read()) instead of the non-public GzipFile.size.</p>
0
2009-11-17T17:04:45Z
[ "python", "gzip" ]
Get uncompressed size of a .gz file in python
1,704,458
<p>Using gzip, tell() returns the offset in the uncompressed file.<br> In order to show a progress bar, I want to know the original (uncompressed) size of the file.<br> Is there an easy way to find out? </p>
7
2009-11-09T22:43:45Z
5,306,107
<pre><code> f = gzip.open(filename) # kludge - report uncompressed file position so progess bars # don't go to 400% f.tell = f.fileobj.tell </code></pre>
1
2011-03-15T00:09:11Z
[ "python", "gzip" ]
Get uncompressed size of a .gz file in python
1,704,458
<p>Using gzip, tell() returns the offset in the uncompressed file.<br> In order to show a progress bar, I want to know the original (uncompressed) size of the file.<br> Is there an easy way to find out? </p>
7
2009-11-09T22:43:45Z
21,051,107
<pre><code>import gzip File = gzip.open("input.gz", "r") Size = gzip.read32(File) </code></pre>
-1
2014-01-10T17:53:12Z
[ "python", "gzip" ]
Get uncompressed size of a .gz file in python
1,704,458
<p>Using gzip, tell() returns the offset in the uncompressed file.<br> In order to show a progress bar, I want to know the original (uncompressed) size of the file.<br> Is there an easy way to find out? </p>
7
2009-11-09T22:43:45Z
22,348,071
<p>Uncompressed size is stored in the last 4 bytes of the gzip file. We can read the binary data and convert it to an int. (This will only work for files under 4GB)</p> <pre><code>import struct def getuncompressedsize(filename): with open(filename, 'rb') as f: f.seek(-4, 2) return struct.unpack('I...
6
2014-03-12T10:08:41Z
[ "python", "gzip" ]
I want to use ZopeInterfaces, however my project is based on Python 3.x - any suggestions?
1,704,589
<p>Zope Interfaces are a great way to get some Java-style "design by contract" into a python program. It provides some great features such as implement-able interfaces and a really neat pattern for writing adaptors for objects. </p> <p>Unfortunately, since it's part of a very mature platform which runs just fine on Py...
1
2009-11-09T23:09:40Z
1,704,721
<p>There appears to be a Python 3 branch of Zope Interfaces <a href="http://svn.zope.org/zope.interface/branches/regebro-python3/" rel="nofollow">here</a> and announced <a href="http://regebro.wordpress.com/2009/04/07/announcing-zopefixers/" rel="nofollow">here</a>.</p>
2
2009-11-09T23:47:31Z
[ "python", "zope.interface" ]
I want to use ZopeInterfaces, however my project is based on Python 3.x - any suggestions?
1,704,589
<p>Zope Interfaces are a great way to get some Java-style "design by contract" into a python program. It provides some great features such as implement-able interfaces and a really neat pattern for writing adaptors for objects. </p> <p>Unfortunately, since it's part of a very mature platform which runs just fine on Py...
1
2009-11-09T23:09:40Z
1,707,215
<p>Use python 2.x. It is more supported by most libraries. It has many 3.x features plus all 3rd party libraries. Later when dependencies are available you can migrate to py3 using 2to3.</p>
0
2009-11-10T11:13:50Z
[ "python", "zope.interface" ]
Difference between yield in Python and yield in C#
1,704,607
<p>What is the difference between <code>yield</code> keyword in Python and <code>yield</code> keyword in C#?</p>
20
2009-11-09T23:13:41Z
1,704,637
<p>C#'s <code>yield return</code> is equivalent to Python's <code>yield</code> , and <code>yield break</code> is just <code>return</code> in Python.</p> <p>Other than those minor differences, they have basically the same purpose.</p>
24
2009-11-09T23:19:24Z
[ "c#", "python" ]
Difference between yield in Python and yield in C#
1,704,607
<p>What is the difference between <code>yield</code> keyword in Python and <code>yield</code> keyword in C#?</p>
20
2009-11-09T23:13:41Z
1,704,639
<p>This may help you understand it : <a href="http://mg.to/2004/08/03/iterator-cpr" rel="nofollow">Iterators in C#, Python, and Ruby</a></p>
12
2009-11-09T23:19:52Z
[ "c#", "python" ]
Difference between yield in Python and yield in C#
1,704,607
<p>What is the difference between <code>yield</code> keyword in Python and <code>yield</code> keyword in C#?</p>
20
2009-11-09T23:13:41Z
35,074,879
<p>The most important difference is that python yield gives you an iterator, once it is fully iterated that's over.</p> <p>But C# yield return gives you an iterator "<strong>factory"</strong>, which you can pass it around and uses it in multiple places of your code without concerning whether it has been "looped" once ...
4
2016-01-29T00:36:03Z
[ "c#", "python" ]
The easiest DHT to implement
1,704,646
<p>Which Distributed Hash Table (DHT) is easiest to implement in Python? Any good example that is not bloated?</p> <p>I not am looking for a definition of DHT because I am more oriented and focused on design and implementation of such.</p>
14
2009-11-09T23:20:59Z
1,704,695
<p>If you are focused on implementation, rather than looking for an out-of-the-box solution, this article might help a bit: <a href="http://www.linuxjournal.com/article/6797">http://www.linuxjournal.com/article/6797</a> </p>
7
2009-11-09T23:37:05Z
[ "python", "p2p", "dht" ]
The easiest DHT to implement
1,704,646
<p>Which Distributed Hash Table (DHT) is easiest to implement in Python? Any good example that is not bloated?</p> <p>I not am looking for a definition of DHT because I am more oriented and focused on design and implementation of such.</p>
14
2009-11-09T23:20:59Z
1,705,028
<p>In my job I'm working with <a href="http://pypi.python.org/pypi/entangled/0.1">entagled</a>. I can't say it's great code, but it seems to be the only <a href="http://en.wikipedia.org/wiki/Kademlia">Kademlia</a> implementation for Python around.</p> <p>I think Kademlia has become the default DHT for most application...
10
2009-11-10T01:02:29Z
[ "python", "p2p", "dht" ]
The easiest DHT to implement
1,704,646
<p>Which Distributed Hash Table (DHT) is easiest to implement in Python? Any good example that is not bloated?</p> <p>I not am looking for a definition of DHT because I am more oriented and focused on design and implementation of such.</p>
14
2009-11-09T23:20:59Z
11,812,796
<p>You might want to check out <a href="https://github.com/gsko/DHTBot" rel="nofollow">DHTBot</a>. It's a python implementation of the BitTorrent MDHT and is written using twisted, a high-level python networking library.</p> <p>(**Disclaimer: I am the author of DHTBot)</p>
3
2012-08-04T23:32:51Z
[ "python", "p2p", "dht" ]
Abandoned Apache process, how long will it go on?
1,704,710
<p>So lets say there's a server process that takes way too long. The client complains that it "times out."</p> <p>Correct me if I'm wrong, but this particular timeout could have to do with apache's timeout setting, but not necessarily. I believe this to be the case because when testing the page in question we couldn't...
0
2009-11-09T23:44:57Z
1,704,734
<p>Correct. In a C apache module you can add a check like:</p> <pre><code>/* r is the 'request_rec' object from apache */ if (r-&gt;connection-&gt;aborted) { /* stop processing and return */ } </code></pre> <p>to verify that the client is still connected. Probably the python interface has something similar.</p> ...
1
2009-11-09T23:51:35Z
[ "python", "apache", "timeout", "netstat" ]
Convert gzipped data fetched by urllib2 to HTML
1,704,754
<p>I currently use mechanize to read gzipped web page as below:</p> <pre><code>br = mechanize.Browser() br.set_handle_gzip(True) response = br.open(url) data = response.read() </code></pre> <p>I wonder how to decompress gzipped data fetched by urllib2 to HTML text?</p> <pre><code>req = urllib2.Request(url) opener = ...
5
2009-11-09T23:55:18Z
1,704,786
<p>Try this:</p> <pre><code>import StringIO data = StringIO.StringIO(data) import gzip gzipper = gzip.GzipFile(fileobj=data) html = gzipper.read() </code></pre> <p><code>html</code> should now hold the HTML (<em>Print it to see</em>). See <a href="http://docs.activestate.com/activepython/2.6/diveintopython/html/http%...
13
2009-11-10T00:02:27Z
[ "python", "gzip", "urllib2" ]
Convert gzipped data fetched by urllib2 to HTML
1,704,754
<p>I currently use mechanize to read gzipped web page as below:</p> <pre><code>br = mechanize.Browser() br.set_handle_gzip(True) response = br.open(url) data = response.read() </code></pre> <p>I wonder how to decompress gzipped data fetched by urllib2 to HTML text?</p> <pre><code>req = urllib2.Request(url) opener = ...
5
2009-11-09T23:55:18Z
3,849,943
<pre><code>def ungzip(r,b): headers = r.info() if ('Content-Encoding' in headers.keys() and headers['Content-Encoding']=='gzip') or \ ('content-encoding' in headers.keys() and headers['content-encoding']=='gzip'): import gzip gz = gzip.GzipFile(fileobj=r, mode='rb') html = gz.read...
-2
2010-10-03T13:30:57Z
[ "python", "gzip", "urllib2" ]
Find images with similar color palette with Python
1,704,793
<p>Suppose there are 10,000 JPEG, PNG images in a gallery, how to find all images with similar color palettes to a selected image sorted by descending similarity?</p>
5
2009-11-10T00:03:40Z
1,704,811
<p>Build a color histogram for each image. Then when you want to match an image to the collection, simply order the list by how close their histogram is to your selected image's histogram.</p> <p>The number of buckets will depend on how accurate you want to be. The type of data combined to make a bucket will define ho...
10
2009-11-10T00:07:39Z
[ "python", "image", "colors" ]
Find images with similar color palette with Python
1,704,793
<p>Suppose there are 10,000 JPEG, PNG images in a gallery, how to find all images with similar color palettes to a selected image sorted by descending similarity?</p>
5
2009-11-10T00:03:40Z
1,704,861
<p>Your question hgas already been answered. Take a look at these other SO answers:</p> <p><a href="http://stackoverflow.com/questions/75891/algorithm-for-finding-similar-images/83486#83486">Algorithm for finding similar images</a></p> <p><a href="http://stackoverflow.com/questions/189943/how-can-i-quantify-differenc...
0
2009-11-10T00:19:13Z
[ "python", "image", "colors" ]
Initializing numpy matrix to something other than zero or one
1,704,823
<p>I have the following code:</p> <pre><code>r = numpy.zeros(shape = (width, height, 9)) </code></pre> <p>It creates a width x height x 9 matrix filled with zeros. Instead, I'd like to know if there's a function or way to initialize them instead to NaN.</p> <p>Is there any? Without having to resort to manually doing...
61
2009-11-10T00:10:07Z
1,704,848
<p>Are you familiar with <code>numpy.nan</code>?</p> <p>You can create your own method such as:</p> <pre><code>def nans(shape, dtype=float): a = numpy.empty(shape, dtype) a.fill(numpy.nan) return a </code></pre> <p>Then</p> <pre><code>nans([3,4]) </code></pre> <p>would output</p> <pre><code>array([[ N...
20
2009-11-10T00:16:58Z
[ "python", "numpy" ]
Initializing numpy matrix to something other than zero or one
1,704,823
<p>I have the following code:</p> <pre><code>r = numpy.zeros(shape = (width, height, 9)) </code></pre> <p>It creates a width x height x 9 matrix filled with zeros. Instead, I'd like to know if there's a function or way to initialize them instead to NaN.</p> <p>Is there any? Without having to resort to manually doing...
61
2009-11-10T00:10:07Z
1,704,853
<p>You rarely need loops for vector operations in numpy. You can create an uninitialized array and assign to all entries at once:</p> <pre><code>&gt;&gt;&gt; a = numpy.empty((3,3,)) &gt;&gt;&gt; a[:] = numpy.NAN &gt;&gt;&gt; a array([[ NaN, NaN, NaN], [ NaN, NaN, NaN], [ NaN, NaN, NaN]]) </code></p...
87
2009-11-10T00:17:49Z
[ "python", "numpy" ]
Initializing numpy matrix to something other than zero or one
1,704,823
<p>I have the following code:</p> <pre><code>r = numpy.zeros(shape = (width, height, 9)) </code></pre> <p>It creates a width x height x 9 matrix filled with zeros. Instead, I'd like to know if there's a function or way to initialize them instead to NaN.</p> <p>Is there any? Without having to resort to manually doing...
61
2009-11-10T00:10:07Z
26,289,777
<p>Another option is to use <code>np.full</code>, an option available in numpy 1.8+</p> <blockquote> <blockquote> <p>a = np.full([width, height, 9],np.nan)</p> </blockquote> </blockquote> <p>This is pretty flexible and you can fill it with any other number that you want.</p>
28
2014-10-09T23:50:13Z
[ "python", "numpy" ]
Initializing numpy matrix to something other than zero or one
1,704,823
<p>I have the following code:</p> <pre><code>r = numpy.zeros(shape = (width, height, 9)) </code></pre> <p>It creates a width x height x 9 matrix filled with zeros. Instead, I'd like to know if there's a function or way to initialize them instead to NaN.</p> <p>Is there any? Without having to resort to manually doing...
61
2009-11-10T00:10:07Z
27,725,628
<p>You can always use multiplication if you don't immediately recall the <code>.empty</code> or <code>.full</code> methods:</p> <pre><code>&gt;&gt;&gt; np.nan * np.ones(shape=(3,2)) array([[ nan, nan], [ nan, nan], [ nan, nan]]) </code></pre> <p>Of course it works with any other numerical value as we...
3
2014-12-31T19:36:51Z
[ "python", "numpy" ]
Initializing numpy matrix to something other than zero or one
1,704,823
<p>I have the following code:</p> <pre><code>r = numpy.zeros(shape = (width, height, 9)) </code></pre> <p>It creates a width x height x 9 matrix filled with zeros. Instead, I'd like to know if there's a function or way to initialize them instead to NaN.</p> <p>Is there any? Without having to resort to manually doing...
61
2009-11-10T00:10:07Z
30,478,412
<p>As said, numpy.empty() is the way to go. However, for objects, fill() might not do exactly what you think it does:</p> <pre><code>In[36]: a = numpy.empty(5,dtype=object) In[37]: a.fill([]) In[38]: a Out[38]: array([[], [], [], [], []], dtype=object) In[39]: a[0].append(4) In[40]: a Out[40]: array([[4], [4], [4], [4...
2
2015-05-27T09:31:20Z
[ "python", "numpy" ]
Python library for Linux process management
1,705,077
<p>Through my web interface I would like to start/stop certain processes and determine whether a started process is still running. </p> <p>My existing website is Python based and running on a Linux server, so do you know of a suitable library that supports this functionality?</p> <p>Thanks</p>
12
2009-11-10T01:14:23Z
1,705,087
<p>The os module is probably your friend. There's <code>os.kill</code>, for instance to kill a process.</p> <p>In terms of getting a list of processes, you'll probably want to shell out to the <code>ps</code> command. <a href="http://stackoverflow.com/questions/160245">This question</a> has more information on that.</...
1
2009-11-10T01:18:56Z
[ "python", "linux", "process" ]
Python library for Linux process management
1,705,077
<p>Through my web interface I would like to start/stop certain processes and determine whether a started process is still running. </p> <p>My existing website is Python based and running on a Linux server, so do you know of a suitable library that supports this functionality?</p> <p>Thanks</p>
12
2009-11-10T01:14:23Z
1,705,099
<p>Checking the list of running processes is accomplished (even by core utilities like "ps") by looking at the contents of the /proc directory.</p> <p>As such, the library you're interested for querying running processes is the same as used for working with any other files and directories (i.e. <code>sys</code> or <co...
6
2009-11-10T01:24:29Z
[ "python", "linux", "process" ]
Python library for Linux process management
1,705,077
<p>Through my web interface I would like to start/stop certain processes and determine whether a started process is still running. </p> <p>My existing website is Python based and running on a Linux server, so do you know of a suitable library that supports this functionality?</p> <p>Thanks</p>
12
2009-11-10T01:14:23Z
1,705,279
<p>Python subprocess <a href="http://docs.python.org/library/subprocess.html" rel="nofollow">http://docs.python.org/library/subprocess.html</a> might help you. If you create a process with subprocess, you can use Popen.terminate() function to stop it. </p>
0
2009-11-10T02:24:56Z
[ "python", "linux", "process" ]
Python library for Linux process management
1,705,077
<p>Through my web interface I would like to start/stop certain processes and determine whether a started process is still running. </p> <p>My existing website is Python based and running on a Linux server, so do you know of a suitable library that supports this functionality?</p> <p>Thanks</p>
12
2009-11-10T01:14:23Z
1,705,296
<p>Since you said this is a Linux server, calling the external <em>ps</em> binary is usually slower, uses more resources and is more error prone than using the information from <em>/proc</em> directly.</p> <p>Since nobody else mentioned, one simple way is:</p> <pre><code>glob.glob('/proc/[0-9]*/') </code></pre> <p>G...
2
2009-11-10T02:30:56Z
[ "python", "linux", "process" ]
Python library for Linux process management
1,705,077
<p>Through my web interface I would like to start/stop certain processes and determine whether a started process is still running. </p> <p>My existing website is Python based and running on a Linux server, so do you know of a suitable library that supports this functionality?</p> <p>Thanks</p>
12
2009-11-10T01:14:23Z
1,956,845
<p>This is what i use. It uses procfs (so you are limited to Unix like systems, will not work on macs i think) and the previously mentioned glob. It also gets the cmdline, which allows you to identify the process. For killing the process you can use <code>os.kill(signal.SIGTERM, pid)</code>. For using subprocess, pleas...
2
2009-12-24T05:30:35Z
[ "python", "linux", "process" ]
Python library for Linux process management
1,705,077
<p>Through my web interface I would like to start/stop certain processes and determine whether a started process is still running. </p> <p>My existing website is Python based and running on a Linux server, so do you know of a suitable library that supports this functionality?</p> <p>Thanks</p>
12
2009-11-10T01:14:23Z
4,229,517
<p>To start/stop python sub processes you can use the subprocess module. To check whether they are running you might use <a href="http://code.google.com/p/psutil/" rel="nofollow">psutil</a>: </p> <pre><code>&gt;&gt;&gt; import psutil &gt;&gt;&gt; pid = 1034 # some pid &gt;&gt;&gt; psutil.pid_exists(pid) True &gt;&gt;...
12
2010-11-19T21:26:36Z
[ "python", "linux", "process" ]
Python library for Linux process management
1,705,077
<p>Through my web interface I would like to start/stop certain processes and determine whether a started process is still running. </p> <p>My existing website is Python based and running on a Linux server, so do you know of a suitable library that supports this functionality?</p> <p>Thanks</p>
12
2009-11-10T01:14:23Z
5,978,509
<p>I'd use PSutil. To provide a practical example:</p> <pre><code>import psutil for proc in psutil.get_process_list(): if proc.username == 'yourusername': if myappname in proc.cmdline: print 'App is running' </code></pre> <p>Alternatively, Red Hat use and maintain a Python module called <stro...
0
2011-05-12T13:13:30Z
[ "python", "linux", "process" ]
Python Form Processing alternatives
1,705,217
<p><code>django.forms</code> is very nice, and does almost exactly what I want to do on my current project, but unfortunately, Google App Engine makes most of the rest of Django unusable, and so packing it along with the app seems kind of silly. </p> <p>I've also discovered FormAlchemy, which is an SQLAlchemy analog ...
3
2009-11-10T02:02:14Z
1,705,231
<p>Is there a more specific reason you don't want to use django.forms? I've quite successfully used bits and pieces of django all by themselves without trouble in several projects.</p> <p>As an aside, there are several patches that make django sortof work in app-engine, though I assume you've considered and discarded ...
1
2009-11-10T02:07:58Z
[ "python", "google-app-engine", "forms" ]
Python Form Processing alternatives
1,705,217
<p><code>django.forms</code> is very nice, and does almost exactly what I want to do on my current project, but unfortunately, Google App Engine makes most of the rest of Django unusable, and so packing it along with the app seems kind of silly. </p> <p>I've also discovered FormAlchemy, which is an SQLAlchemy analog ...
3
2009-11-10T02:02:14Z
1,705,237
<p>I've grown to love <a href="http://wtforms.simplecodes.com/">WTForms</a>, it's simple, straightforward, and very flexible. It's part of my django-free web stack. </p> <p>It's completely standalone, and carries over the good parts of django's form libraries, while imho having some things much better.</p>
13
2009-11-10T02:09:34Z
[ "python", "google-app-engine", "forms" ]
Python Form Processing alternatives
1,705,217
<p><code>django.forms</code> is very nice, and does almost exactly what I want to do on my current project, but unfortunately, Google App Engine makes most of the rest of Django unusable, and so packing it along with the app seems kind of silly. </p> <p>I've also discovered FormAlchemy, which is an SQLAlchemy analog ...
3
2009-11-10T02:02:14Z
1,705,371
<p>You can also have a look at <a href="http://formencode.org/" rel="nofollow">formencode</a>, it is generic enough so that you may fit it in GAE.</p>
2
2009-11-10T02:53:30Z
[ "python", "google-app-engine", "forms" ]
Python Form Processing alternatives
1,705,217
<p><code>django.forms</code> is very nice, and does almost exactly what I want to do on my current project, but unfortunately, Google App Engine makes most of the rest of Django unusable, and so packing it along with the app seems kind of silly. </p> <p>I've also discovered FormAlchemy, which is an SQLAlchemy analog ...
3
2009-11-10T02:02:14Z
1,706,019
<p>I'm not sure what you mean by "making most of the rest of Django unusable" and especially by "packing it along with the app". Are you familiar with <a href="http://code.google.com/appengine/docs/python/tools/libraries.html#Django" rel="nofollow">the docs</a>? If you just do as they suggest, i.e.</p> <pre><code>im...
3
2009-11-10T06:37:50Z
[ "python", "google-app-engine", "forms" ]
Python Twisted and database connections
1,705,444
<p>Our projects at work include synchronous applications (short lived) and asynchronous Twisted applications (long lived). We're re-factoring our database and are going to build an API module to decouple all of the SQL in that module. I'd like to create that API so both synchronous and asynchronous applications can use...
5
2009-11-10T03:20:59Z
1,705,457
<p>Have you considered borrowing a page from <a href="http://en.wikipedia.org/wiki/Continuation-passing_style" rel="nofollow">continuation-passing style</a>? Stackless Python <a href="http://www.stackless.com/spcpaper.htm" rel="nofollow">supports continuations directly</a>, if you're using it, and the approach appears...
0
2009-11-10T03:26:44Z
[ "python", "mysql", "database", "twisted" ]
Python Twisted and database connections
1,705,444
<p>Our projects at work include synchronous applications (short lived) and asynchronous Twisted applications (long lived). We're re-factoring our database and are going to build an API module to decouple all of the SQL in that module. I'd like to create that API so both synchronous and asynchronous applications can use...
5
2009-11-10T03:20:59Z
1,705,987
<p><a href="http://twistedmatrix.com/projects/core/documentation/howto/rdbms.html" rel="nofollow">twisted.enterprise.adbapi</a> seems the way to go -- do you think it fails to match your requirements, and if so, can you please explain why?</p>
3
2009-11-10T06:26:54Z
[ "python", "mysql", "database", "twisted" ]
Python Twisted and database connections
1,705,444
<p>Our projects at work include synchronous applications (short lived) and asynchronous Twisted applications (long lived). We're re-factoring our database and are going to build an API module to decouple all of the SQL in that module. I'd like to create that API so both synchronous and asynchronous applications can use...
5
2009-11-10T03:20:59Z
1,712,186
<p>Within Twisted, you basically want a wrapper around a function which returns a Deferred (such as the Twisted DB layer), waits for it's results, and returns them. However, you can't busy-wait, since that's using up your reactor cycles, and checking for a task to complete using the Twisted non-blocking wait is probab...
1
2009-11-11T00:17:49Z
[ "python", "mysql", "database", "twisted" ]
Python Twisted and database connections
1,705,444
<p>Our projects at work include synchronous applications (short lived) and asynchronous Twisted applications (long lived). We're re-factoring our database and are going to build an API module to decouple all of the SQL in that module. I'd like to create that API so both synchronous and asynchronous applications can use...
5
2009-11-10T03:20:59Z
1,720,838
<p>All the database libraries I've seen seem to be stubbornly synchronous.</p> <p>It appears that Twisted.enterprise.abapi solves this problem by using a threads to manage a connection pool and wrapping the underlying database libraries. This is obviously not ideal, but I suppose it would work, but I haven't actually ...
0
2009-11-12T08:54:48Z
[ "python", "mysql", "database", "twisted" ]
Organize a python library as plugins
1,705,665
<p>I would like to create a library, say foolib, but to keep different subpackages separated, so to have barmodule, bazmodule, all under the same foolib main package. In other words, I want the client code to be able to do</p> <pre><code>import foolib.barmodule import foolib.bazmodule </code></pre> <p>but to distribu...
0
2009-11-10T04:45:57Z
1,705,704
<p>Yes, simply create a foolib directory, add an <code>__init__.py</code> to it, and make each sub-module a <code>.py</code> file.</p> <pre><code>/foolib barmodule.py bazmodule.py </code></pre> <p>then you can import them like so:</p> <pre><code>from foolib import barmodule barmodule.some_function() </code><...
0
2009-11-10T04:57:52Z
[ "python", "module" ]
Organize a python library as plugins
1,705,665
<p>I would like to create a library, say foolib, but to keep different subpackages separated, so to have barmodule, bazmodule, all under the same foolib main package. In other words, I want the client code to be able to do</p> <pre><code>import foolib.barmodule import foolib.bazmodule </code></pre> <p>but to distribu...
0
2009-11-10T04:45:57Z
1,705,807
<p>You may be looking for <a href="http://stackoverflow.com/questions/1675734/how-do-i-create-a-namespace-package-in-python">namespace packages</a>. See also <a href="http://www.python.org/dev/peps/pep-0382/" rel="nofollow">PEP 382</a>.</p>
3
2009-11-10T05:28:50Z
[ "python", "module" ]
Finding cycle of 3 nodes ( or triangles) in a graph
1,705,824
<p>I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative solution (multiple "for" loop) is not very efficient.</p> <p>I am using python for my programming, if these is some inb...
5
2009-11-10T05:33:17Z
1,705,866
<p>Even though it isn't efficient, you may want to implement a solution, so use the loops. Write a test so you can get an idea as to how long it takes.</p> <p>Then, as you try new approaches you can do two things: 1) Make certain that the answer remains the same. 2) See what the improvement is.</p> <p>Having a faste...
1
2009-11-10T05:45:23Z
[ "python", "graph", "geometry", "cycle" ]
Finding cycle of 3 nodes ( or triangles) in a graph
1,705,824
<p>I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative solution (multiple "for" loop) is not very efficient.</p> <p>I am using python for my programming, if these is some inb...
5
2009-11-10T05:33:17Z
1,705,913
<p>Do you need to find 'all' of the 'triangles', or just 'some'/'any'? Or perhaps you just need to test whether a particular node is part of a triangle?</p> <p>The test is simple - given a node A, are there any two connected nodes B &amp; C that are also directly connected.</p> <p>If you need to find all of the trian...
0
2009-11-10T06:03:52Z
[ "python", "graph", "geometry", "cycle" ]
Finding cycle of 3 nodes ( or triangles) in a graph
1,705,824
<p>I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative solution (multiple "for" loop) is not very efficient.</p> <p>I am using python for my programming, if these is some inb...
5
2009-11-10T05:33:17Z
1,705,927
<p>I don't want to sound harsh, but have you tried to Google it? The first link is a pretty quick algorithm to do that: <a href="http://www.mail-archive.com/algogeeks@googlegroups.com/msg05642.html" rel="nofollow">http://www.mail-archive.com/algogeeks@googlegroups.com/msg05642.html</a></p> <p>And then there is this ar...
1
2009-11-10T06:06:26Z
[ "python", "graph", "geometry", "cycle" ]
Finding cycle of 3 nodes ( or triangles) in a graph
1,705,824
<p>I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative solution (multiple "for" loop) is not very efficient.</p> <p>I am using python for my programming, if these is some inb...
5
2009-11-10T05:33:17Z
1,706,366
<p>A million edges is quite small. Unless you are doing it thousands of times, just use a naive implementation. </p> <p>I'll assume that you have a dictionary of node_ids, which point to a sequence of their neighbors, and that the graph is directed.</p> <p>For example:</p> <pre><code>nodes = {} nodes[0] = 1,2 nodes[...
2
2009-11-10T08:24:53Z
[ "python", "graph", "geometry", "cycle" ]
Finding cycle of 3 nodes ( or triangles) in a graph
1,705,824
<p>I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative solution (multiple "for" loop) is not very efficient.</p> <p>I am using python for my programming, if these is some inb...
5
2009-11-10T05:33:17Z
30,807,849
<p>I am working on the same problem of counting number of triangles on <strong>undirected</strong> graph and wisty's solution works really well in my case. I have modified it a bit so only undirected triangles are counted. </p> <pre><code> #### function for counting undirected cycles def generate_triangles(node...
0
2015-06-12T16:21:27Z
[ "python", "graph", "geometry", "cycle" ]
Finding cycle of 3 nodes ( or triangles) in a graph
1,705,824
<p>I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, using a simple iterative solution (multiple "for" loop) is not very efficient.</p> <p>I am using python for my programming, if these is some inb...
5
2009-11-10T05:33:17Z
32,447,746
<p>Pretty easy and clear way to do is to use Networkx:</p> <p>With Networkx you can get the loops of an undirected graph by <a href="https://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.cycles.cycle_basis.html" rel="nofollow">nx.cycle_basis(G)</a> and then select the ones with 3 node...
1
2015-09-08T00:55:47Z
[ "python", "graph", "geometry", "cycle" ]
ImportError running Google's python appengine on Ubuntu
1,705,831
<p>I'm trying to teach myself python using Google's <a href="http://code.google.com/appengine/downloads.html" rel="nofollow">AppEngine</a>, and I can't get the dev server running. I get this error:</p> <blockquote> <p>Traceback (most recent call last):<br> File "/opt/google_appengine/google_appengine_1.2.7/dev_app...
2
2009-11-10T05:34:48Z
1,705,936
<p>With that error, Python is saying that it can't find or read the name that it's trying to import. Since the import of <code>os_compat</code> is the very first executable line of AppEngine's <code>dev_appserver.py</code>, I suspect that there's a problem with the way that your paths are configured.</p>
0
2009-11-10T06:08:51Z
[ "python", "linux", "google-app-engine" ]
ImportError running Google's python appengine on Ubuntu
1,705,831
<p>I'm trying to teach myself python using Google's <a href="http://code.google.com/appengine/downloads.html" rel="nofollow">AppEngine</a>, and I can't get the dev server running. I get this error:</p> <blockquote> <p>Traceback (most recent call last):<br> File "/opt/google_appengine/google_appengine_1.2.7/dev_app...
2
2009-11-10T05:34:48Z
1,706,803
<p>The problem appears to be the fact that Karmic Koala 9.10 (the latest version of Ubuntu) ships with Ubuntu One, a python app that depends on Google's <a href="http://code.google.com/p/protobuf/" rel="nofollow">protocol buffers</a> library. The python-protobuf package provides the google.protobuf package in /usr/lib/...
3
2009-11-10T09:58:53Z
[ "python", "linux", "google-app-engine" ]
ImportError running Google's python appengine on Ubuntu
1,705,831
<p>I'm trying to teach myself python using Google's <a href="http://code.google.com/appengine/downloads.html" rel="nofollow">AppEngine</a>, and I can't get the dev server running. I get this error:</p> <blockquote> <p>Traceback (most recent call last):<br> File "/opt/google_appengine/google_appengine_1.2.7/dev_app...
2
2009-11-10T05:34:48Z
1,711,607
<p>It was a file permission problem. os_compat.py wasn't readable by user, just by root. I'm not sure if I screwed this up, or if the permissions by default don't have read-all, but that was the fix.</p> <p>I hate to accept my own answer after Kyle gave such a good response, but I don't need the $PYTHONPATH fix to mak...
1
2009-11-10T22:12:29Z
[ "python", "linux", "google-app-engine" ]
ImportError running Google's python appengine on Ubuntu
1,705,831
<p>I'm trying to teach myself python using Google's <a href="http://code.google.com/appengine/downloads.html" rel="nofollow">AppEngine</a>, and I can't get the dev server running. I get this error:</p> <blockquote> <p>Traceback (most recent call last):<br> File "/opt/google_appengine/google_appengine_1.2.7/dev_app...
2
2009-11-10T05:34:48Z
4,019,562
<p>The latest version of Ubuntu (10.10) has also removed Python 2.5 - making it a pain to install the App Engine development environment.</p> <p>I (finally) got my environment working (including using App Engine Helper for unit testing). I built this bash script which might be useful to others. It installs:</p> <ol...
0
2010-10-25T23:03:02Z
[ "python", "linux", "google-app-engine" ]
ImportError running Google's python appengine on Ubuntu
1,705,831
<p>I'm trying to teach myself python using Google's <a href="http://code.google.com/appengine/downloads.html" rel="nofollow">AppEngine</a>, and I can't get the dev server running. I get this error:</p> <blockquote> <p>Traceback (most recent call last):<br> File "/opt/google_appengine/google_appengine_1.2.7/dev_app...
2
2009-11-10T05:34:48Z
7,174,350
<p>Ubuntu 11.04 comes with python 2.6 as the default version. It is suggested to use Google app engine with version 2.5. I am using it though for many years with python 2.6 without any issues. </p> <p>What you need to do in order to execute it smoothly with python 2.6 is to edit <code>google/appengine/tools/dev_appser...
0
2011-08-24T10:59:19Z
[ "python", "linux", "google-app-engine" ]
ImportError running Google's python appengine on Ubuntu
1,705,831
<p>I'm trying to teach myself python using Google's <a href="http://code.google.com/appengine/downloads.html" rel="nofollow">AppEngine</a>, and I can't get the dev server running. I get this error:</p> <blockquote> <p>Traceback (most recent call last):<br> File "/opt/google_appengine/google_appengine_1.2.7/dev_app...
2
2009-11-10T05:34:48Z
31,147,001
<p>If you are now using <a href="https://cloud.google.com/sdk/" rel="nofollow">Google Cloud SDK</a>, put this into <code>~/.profile</code>.</p> <pre><code>export CLOUDSDK_ROOT_DIR="/path/to/google/cloud/sdk/" export APPENGINE_HOME="${CLOUDSDK_ROOT_DIR}/platform/appengine-java-sdk" export GAE_SDK_ROOT="${CLOUDSDK_ROOT_...
0
2015-06-30T19:38:02Z
[ "python", "linux", "google-app-engine" ]
Python Markdown: Markdown Inside HTML Blocks
1,705,895
<p>Is there an extra for Python Markdown that supports Markdown inside HTML block elements, e.g. div, p</p> <p>i.e. is there a way to convert this:</p> <pre><code>&lt;div id="content"&gt; [Google](http://www.google.com) &lt;/div&gt; </code></pre> <p>to</p> <pre><code>&lt;div id="content"&gt; &lt;a href="htt...
5
2009-11-10T05:59:25Z
1,706,086
<p>According to this: <a href="http://daringfireball.net/projects/markdown/syntax#html" rel="nofollow">http://daringfireball.net/projects/markdown/syntax#html</a></p> <p>You would have to use a &lt;span&gt; rather than a &lt;div&gt;. Further explanation is available at the above link.</p>
3
2009-11-10T07:00:25Z
[ "python", "html", "markdown" ]
Python Markdown: Markdown Inside HTML Blocks
1,705,895
<p>Is there an extra for Python Markdown that supports Markdown inside HTML block elements, e.g. div, p</p> <p>i.e. is there a way to convert this:</p> <pre><code>&lt;div id="content"&gt; [Google](http://www.google.com) &lt;/div&gt; </code></pre> <p>to</p> <pre><code>&lt;div id="content"&gt; &lt;a href="htt...
5
2009-11-10T05:59:25Z
5,266,068
<p>With <a href="http://michelf.com/projects/php-markdown/extra/" rel="nofollow">PHP Markdown Extra</a> (not Python, I know, but relevant), you can do this by adding the attribute <code>markdown=1</code> to the block-level element in question.</p> <p>I’m interested in pure-Python libraries that support the “Markdo...
1
2011-03-10T21:21:10Z
[ "python", "html", "markdown" ]
Python Markdown: Markdown Inside HTML Blocks
1,705,895
<p>Is there an extra for Python Markdown that supports Markdown inside HTML block elements, e.g. div, p</p> <p>i.e. is there a way to convert this:</p> <pre><code>&lt;div id="content"&gt; [Google](http://www.google.com) &lt;/div&gt; </code></pre> <p>to</p> <pre><code>&lt;div id="content"&gt; &lt;a href="htt...
5
2009-11-10T05:59:25Z
13,538,239
<p>The relevant bug report states that the extra extension adds that: <a href="https://github.com/waylan/Python-Markdown/issues/80" rel="nofollow">https://github.com/waylan/Python-Markdown/issues/80</a></p> <p>Then you can add markdown="1" to div tags to have markdown parsed inside</p> <pre><code>&lt;div markdown="1"...
2
2012-11-24T04:23:04Z
[ "python", "html", "markdown" ]
Problem with making object callable in python
1,705,928
<p>I wrote code like this</p> <pre><code>&gt;&gt;&gt; class a(object): def __init__(self): self.__call__ = lambda x:x &gt;&gt;&gt; b = a() </code></pre> <p>I expected that object of class a should be callable object but eventually it is not.</p> <pre><code>&gt;&gt;&gt; b() Traceback (most recent call last...
14
2009-11-10T06:06:35Z
1,705,948
<p><code>__call__</code> needs to be defined on the class, not the instance</p> <pre><code>class a(object): def __init__(self): pass __call__ = lambda x:x </code></pre> <p>but most people probably find it more readable to define the method the usual way</p> <pre><code>class a(object): def __init_...
11
2009-11-10T06:12:50Z
[ "python" ]
Problem with making object callable in python
1,705,928
<p>I wrote code like this</p> <pre><code>&gt;&gt;&gt; class a(object): def __init__(self): self.__call__ = lambda x:x &gt;&gt;&gt; b = a() </code></pre> <p>I expected that object of class a should be callable object but eventually it is not.</p> <pre><code>&gt;&gt;&gt; b() Traceback (most recent call last...
14
2009-11-10T06:06:35Z
1,705,954
<p>Special methods are looked up on the <em>type</em> (e.g., class) of the object being operated on, not on the specific instance. Think about it: otherwise, if a class defines <code>__call__</code> for example, when the class is called that <code>__call__</code> should get called... what a disaster! But fortunately t...
15
2009-11-10T06:15:20Z
[ "python" ]
Problem with making object callable in python
1,705,928
<p>I wrote code like this</p> <pre><code>&gt;&gt;&gt; class a(object): def __init__(self): self.__call__ = lambda x:x &gt;&gt;&gt; b = a() </code></pre> <p>I expected that object of class a should be callable object but eventually it is not.</p> <pre><code>&gt;&gt;&gt; b() Traceback (most recent call last...
14
2009-11-10T06:06:35Z
5,373,140
<p>What about this? Define a base class AllowDynamicCall:</p> <pre><code>class AllowDynamicCall(object): def __call__(self, *args, **kwargs): return self._callfunc(self, *args, **kwargs) </code></pre> <p>And then subclass AllowDynamicCall:</p> <pre><code>class Example(AllowDynamicCall): def __init...
1
2011-03-21T02:03:56Z
[ "python" ]
Checking row and column for a word in python
1,705,933
<p>I am trying to create a checking program to see if the word is in a matrix horizontally or vertically. I have the code for checking the row, but would checking the column be similar to the row code?</p> <pre><code>def checkRow(table, r, pos, word): for i in range(0, len(word)): if table[r][pos+i] != wor...
0
2009-11-10T06:07:23Z
1,705,938
<p>Isn't it simple like this:</p> <pre><code>def checkCol(table, r, pos, word): for i in range(0, len(word)): if table[r+i][pos] != word[i]: return False return True </code></pre>
4
2009-11-10T06:09:40Z
[ "python", "list", "matrix" ]
Checking row and column for a word in python
1,705,933
<p>I am trying to create a checking program to see if the word is in a matrix horizontally or vertically. I have the code for checking the row, but would checking the column be similar to the row code?</p> <pre><code>def checkRow(table, r, pos, word): for i in range(0, len(word)): if table[r][pos+i] != wor...
0
2009-11-10T06:07:23Z
1,705,968
<pre><code>import itertools def checkRow(table, r, pos, word): return all(w==x for w, x in itertools.izip(word, table[r][pos:])) def checkCol(table, r, pos, word): return all(w==x for w, x in itertools.izip(word, table[r:][pos])) </code></pre> <p>The OP indicates "they haven't learned about import yet" so th...
4
2009-11-10T06:19:49Z
[ "python", "list", "matrix" ]
Checking row and column for a word in python
1,705,933
<p>I am trying to create a checking program to see if the word is in a matrix horizontally or vertically. I have the code for checking the row, but would checking the column be similar to the row code?</p> <pre><code>def checkRow(table, r, pos, word): for i in range(0, len(word)): if table[r][pos+i] != wor...
0
2009-11-10T06:07:23Z
1,706,011
<pre><code>def intable(table, word): if any(word in ''.join(row) for row in table): # check rows return True return any(word in ''.join(col) for col in zip(*table)) # check columns </code></pre>
1
2009-11-10T06:35:15Z
[ "python", "list", "matrix" ]
Checking row and column for a word in python
1,705,933
<p>I am trying to create a checking program to see if the word is in a matrix horizontally or vertically. I have the code for checking the row, but would checking the column be similar to the row code?</p> <pre><code>def checkRow(table, r, pos, word): for i in range(0, len(word)): if table[r][pos+i] != wor...
0
2009-11-10T06:07:23Z
1,706,020
<pre><code>def checkRow(table, r, pos, word): return word=="".join(table[r][pos:pos+len(word)]) def checkColumn(table, r, pos, word): return word=="".join(row[pos] for row in table[r:r+len(word)]) </code></pre>
2
2009-11-10T06:38:14Z
[ "python", "list", "matrix" ]
How might I handle development versions of Python packages without relying on SCM?
1,705,955
<p>One issue that comes up during Pinax development is dealing with development versions of external apps. I am trying to come up with a solution that doesn't involve bringing in the version control systems. Reason being I'd rather not have to install all the possible version control systems on my system (or force that...
6
2009-11-10T06:16:31Z
1,706,000
<p>Could you handle this using the "==dev" version specifier? If the distribution's page on PyPI includes a link to a .tgz of the current dev version (such as both github and bitbucket provide automatically) and you append "#egg=project_name-dev" to the link, both easy_install and pip will use that .tgz if ==dev is req...
3
2009-11-10T06:29:51Z
[ "python", "django", "packaging", "pinax", "external-dependencies" ]
How might I handle development versions of Python packages without relying on SCM?
1,705,955
<p>One issue that comes up during Pinax development is dealing with development versions of external apps. I am trying to come up with a solution that doesn't involve bringing in the version control systems. Reason being I'd rather not have to install all the possible version control systems on my system (or force that...
6
2009-11-10T06:16:31Z
1,706,001
<p>Most open source distributors (the Debians, Ubuntu's, MacPorts, et al) use some sort of patch management mechanism. So something like: import the base source code for each package as released, as a tar ball, or as a SCM snapshot. Then manage any necessary modifications on top of it using a patch manager, like <a h...
1
2009-11-10T06:30:26Z
[ "python", "django", "packaging", "pinax", "external-dependencies" ]
How might I handle development versions of Python packages without relying on SCM?
1,705,955
<p>One issue that comes up during Pinax development is dealing with development versions of external apps. I am trying to come up with a solution that doesn't involve bringing in the version control systems. Reason being I'd rather not have to install all the possible version control systems on my system (or force that...
6
2009-11-10T06:16:31Z
1,706,060
<p>EDIT: I am not sure I am reading your question correctly so the following may not answer your question directly.</p> <p>Something I've considered, but haven't tested, is using pip's freeze bundle feature. Perhaps using that and distributing the bundle with Pinax would work? My only concern would be how different ...
0
2009-11-10T06:52:01Z
[ "python", "django", "packaging", "pinax", "external-dependencies" ]
How might I handle development versions of Python packages without relying on SCM?
1,705,955
<p>One issue that comes up during Pinax development is dealing with development versions of external apps. I am trying to come up with a solution that doesn't involve bringing in the version control systems. Reason being I'd rather not have to install all the possible version control systems on my system (or force that...
6
2009-11-10T06:16:31Z
1,706,213
<p>I meant to mention that the solution I had considered before asking was to put up a Pinax PyPI and make development releases on it. We could put up an instance of chishop. We are already using pip's --find-links to point at pypi.pinaxproject.com for packages we've had to release ourselves.</p>
3
2009-11-10T07:40:16Z
[ "python", "django", "packaging", "pinax", "external-dependencies" ]
Python if else condition error
1,706,009
<p>This is my code snippet, but it not execute the way that I want.The first if statement executes successfully if the input is a non-negative/character value, but if it is a negative value it ignores the elif statement. What's the issue.I'm using Python 2.6 </p> <pre><code>from math import sqrt import cmath y = raw...
1
2009-11-10T06:34:25Z
1,706,025
<p>your <code>elif</code> never gets evaluated <code>if</code> y is a digit.<br> The program executes the statements within the if scope and then skips to the last line (<code>raw_input ...</code>)</p>
1
2009-11-10T06:40:01Z
[ "python", "syntax" ]