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
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
2,186,565
<p><strong>Python 3.5+</strong></p> <p>Starting with Python version 3.5, the <a href="https://docs.python.org/3/library/glob.html"><code>glob</code></a> module supports the <code>"**"</code> directive (which is parsed only if you pass <code>recursive</code> flag):</p> <pre><code>import glob for filename in glob.iglo...
610
2010-02-02T18:26:54Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
2,186,639
<pre><code>import os import fnmatch def recursive_glob(treeroot, pattern): results = [] for base, dirs, files in os.walk(treeroot): goodfiles = fnmatch.filter(files, pattern) results.extend(os.path.join(base, f) for f in goodfiles) return results </code></pre> <p><a href="http://docs.pyth...
29
2010-02-02T18:39:38Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
2,186,673
<p>Similar to other solutions, but using fnmatch.fnmatch instead of glob, since os.walk already listed the filenames:</p> <pre><code>import os, fnmatch def find_files(directory, pattern): for root, dirs, files in os.walk(directory): for basename in files: if fnmatch.fnmatch(basename, pattern)...
80
2010-02-02T18:44:51Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
6,484,486
<p>I've modified the glob module to support ** for recursive globbing, e.g:</p> <pre><code>&gt;&gt;&gt; import glob2 &gt;&gt;&gt; all_header_files = glob2.glob('src/**/*.c') </code></pre> <p><a href="https://github.com/miracle2k/python-glob2/">https://github.com/miracle2k/python-glob2/</a></p> <p>Useful when you wan...
31
2011-06-26T14:14:22Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
7,409,528
<p>Another way to do it using just the glob module. Just seed the rglob method with a starting base directory and a pattern to match and it will return a list of matching file names.</p> <pre><code>import glob import os def _getDirs(base): return [x for x in glob.iglob(os.path.join( base, '*')) if os.path.isdir(x...
3
2011-09-13T22:59:25Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
7,977,340
<p>Here's a solution with nested list comprehensions, <code>os.walk</code> and simple suffix matching instead of <code>glob</code>:</p> <pre><code>import os cfiles = [os.path.join(root, filename) for root, dirnames, filenames in os.walk('src') for filename in filenames if filename.endswith('.c')] <...
10
2011-11-02T08:10:45Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
10,597,254
<p>Johan and Bruno provide excellent solutions on the minimal requirement as stated. I have just released <a href="http://pypi.python.org/pypi/formic">Formic</a> which implements Ant <a href="http://ant.apache.org/manual/dirtasks.html#patterns">FileSet and Globs</a> which can handle this and more complicated scenarios....
5
2012-05-15T08:53:18Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
11,705,046
<p>based on other answers this is my current working implementation, which retrieves nested xml files in a root directory:</p> <pre><code>files = [] for root, dirnames, filenames in os.walk(myDir): files.extend(glob.glob(root + "/*.xml")) </code></pre> <p>I'm really having fun with python :)</p>
4
2012-07-28T22:09:23Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
14,170,943
<p>Recently I had to recover my pictures with the extension .jpg. I ran photorec and recovered 4579 directories 2.2 million files within, having tremendous variety of extensions.With the script below I was able to select 50133 files havin .jpg extension within minutes:</p> <pre><code>#!/usr/binenv python2.7 import gl...
3
2013-01-05T10:36:43Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
16,888,947
<p>Simplified version of Johan Dahlin's answer, without <a href="http://docs.python.org/2/library/fnmatch.html" rel="nofollow">fnmatch</a>.</p> <pre><code>import os matches = [] for root, dirnames, filenames in os.walk('src'): matches += [os.path.join(root, f) for f in filenames if f[-2:] == '.c'] </code></pre>
0
2013-06-03T01:29:07Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
17,273,506
<p>Or with a list comprehension:</p> <pre><code> &gt;&gt;&gt; base = r"c:\User\xtofl" &gt;&gt;&gt; binfiles = [ os.path.join(base,f) for base, _, files in os.walk(root) for f in files if f.endswith(".jpg") ] </code></pre>
1
2013-06-24T10:41:03Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
17,900,945
<p>Just made this.. it will print files and directory in hierarchical way</p> <p>But I didn't used fnmatch or walk</p> <pre><code>#!/usr/bin/python import os,glob,sys def dirlist(path, c = 1): for i in glob.glob(os.path.join(path, "*")): if os.path.isfile(i): filepat...
1
2013-07-27T18:12:29Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
18,021,392
<p>That one uses fnmatch or regular expression:</p> <pre><code>import fnmatch, os def filepaths(directory, pattern): for root, dirs, files in os.walk(directory): for basename in files: try: matched = pattern.match(basename) except AttributeError: mat...
1
2013-08-02T16:01:34Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
21,389,414
<pre><code>import sys, os, glob dir_list = ["c:\\books\\heap"] while len(dir_list) &gt; 0: cur_dir = dir_list[0] del dir_list[0] list_of_files = glob.glob(cur_dir+'\\*') for book in list_of_files: if os.path.isfile(book): print(book) else: dir_list.append(book) ...
0
2014-01-27T19:03:53Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
25,369,194
<p>Here is my solution using list comprehension to search for <strong>multiple</strong> file extensions <strong>recursively</strong> in a directory and all subdirectories:</p> <pre><code>import os, glob def _globrec(path, *exts): """ Glob recursively a directory and all subdirectories for multiple file extensions ...
1
2014-08-18T17:50:33Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
26,869,233
<p>Starting with Python 3.4, one can use the <a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob"><code>glob()</code></a> method of one of the <code>Path</code> classes in the new <a href="https://docs.python.org/3/library/pathlib.html">pathlib</a> module, which supports <code>**</code> wildcards....
24
2014-11-11T16:08:39Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
26,946,439
<p>I modified the top answer in this posting.. and recently created this script which will loop through all files in a given directory (searchdir) and the sub-directories under it... and prints filename, rootdir, modified/creation date, and size.</p> <p>Hope this helps someone... and they can walk the directory and ge...
0
2014-11-15T13:39:49Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
31,142,692
<p>Here is a solution that will match the pattern against the full path and not just the base filename.</p> <p>It uses <a href="https://docs.python.org/2/library/fnmatch.html?highlight=fnmatch.translate#fnmatch.translate" rel="nofollow"><code>fnmatch.translate</code></a> to convert a glob-style pattern into a regular ...
0
2015-06-30T15:39:36Z
[ "python", "path", "glob" ]
Use a Glob() to find files recursively in Python?
2,186,525
<p>This is what I have:</p> <pre><code>glob(os.path.join('src','*.c')) </code></pre> <p>but I want to search the subfolders of src. Something like this would work:</p> <pre><code>glob(os.path.join('src','*.c')) glob(os.path.join('src','*','*.c')) glob(os.path.join('src','*','*','*.c')) glob(os.path.join('src','*','*...
313
2010-02-02T18:19:50Z
34,108,724
<p>In addition to the suggested answers, you can do this with some lazy generation and list comprehension magic:</p> <pre><code>import os, glob, itertools results = itertools.chain.from_iterable(glob.iglob(os.path.join(root,'*.c')) for root, dirs, files in os.walk('src')...
0
2015-12-05T17:42:56Z
[ "python", "path", "glob" ]
Getting correct string length in Python for strings with ANSI color codes
2,186,919
<p>I've got some Python code that will automatically print a set of data in a nice column format, including putting in the appropriate ASCII escape sequences to color various pieces of the data for readability.</p> <p>I eventually end up with each line being represented as a list, with each item being a column that is...
12
2010-02-02T19:18:36Z
2,186,946
<p>The Python code printing out the text should check <code>sys.stdout.isatty()</code> and not use color codes if it is False.</p>
1
2010-02-02T19:21:02Z
[ "python", "escaping", "ansi-colors" ]
Getting correct string length in Python for strings with ANSI color codes
2,186,919
<p>I've got some Python code that will automatically print a set of data in a nice column format, including putting in the appropriate ASCII escape sequences to color various pieces of the data for readability.</p> <p>I eventually end up with each line being represented as a list, with each item being a column that is...
12
2010-02-02T19:18:36Z
2,187,024
<p>The pyparsing wiki includes this <a href="http://pyparsing-public.wikispaces.com/Helpful+Expressions#toc7">helpful expression</a> for matching on ANSI escape sequences:</p> <pre><code>ESC = Literal('\x1b') integer = Word(nums) escapeSeq = Combine(ESC + '[' + Optional(delimitedList(integer,';')) + o...
7
2010-02-02T19:33:30Z
[ "python", "escaping", "ansi-colors" ]
Getting correct string length in Python for strings with ANSI color codes
2,186,919
<p>I've got some Python code that will automatically print a set of data in a nice column format, including putting in the appropriate ASCII escape sequences to color various pieces of the data for readability.</p> <p>I eventually end up with each line being represented as a list, with each item being a column that is...
12
2010-02-02T19:18:36Z
2,187,116
<p>Looking in <a href="http://en.wikipedia.org/wiki/ANSI_escape_code" rel="nofollow">ANSI_escape_code</a>, the sequence in your example is <strong>Select Graphic Rendition</strong> (probably <strong>bold</strong>).</p> <p>Try to control column positioning with the <strong>CUrsor Position</strong> ( <code>CSI n ; m H</...
0
2010-02-02T19:45:53Z
[ "python", "escaping", "ansi-colors" ]
Getting correct string length in Python for strings with ANSI color codes
2,186,919
<p>I've got some Python code that will automatically print a set of data in a nice column format, including putting in the appropriate ASCII escape sequences to color various pieces of the data for readability.</p> <p>I eventually end up with each line being represented as a list, with each item being a column that is...
12
2010-02-02T19:18:36Z
2,188,410
<p>I don't understand TWO things.</p> <p>(1) It is your code, under your control. You want to add escape sequences to your data and then strip them out again so that you can calculate the length of your data?? It seems much simpler to calculate the padding <strong>before</strong> adding the escape sequences. What am I...
3
2010-02-02T22:55:38Z
[ "python", "escaping", "ansi-colors" ]
proxy-like python module
2,186,977
<p>Can a python module hand over other module in case of self being imported? </p>
2
2010-02-02T19:26:50Z
2,187,173
<pre><code>import other_module for name in dir(other_module): globals()[name] = getattr(other_module, name) del other_module </code></pre>
0
2010-02-02T19:53:53Z
[ "python" ]
proxy-like python module
2,186,977
<p>Can a python module hand over other module in case of self being imported? </p>
2
2010-02-02T19:26:50Z
2,187,529
<p>You can sort of do it (in the normal CPython anyway):</p> <pre><code># uglyhack.py import sys import othermodule sys.modules[__name__]= othermodule </code></pre> <p>then:</p> <pre><code>&gt;&gt;&gt; import uglyhack &gt;&gt;&gt; uglyhack &lt;module 'othermodule' from '...'&gt; </code></pre> <p>This relies on th...
2
2010-02-02T20:41:03Z
[ "python" ]
how to check if request is ajax in turbogears
2,187,115
<p>How do I go about checking if a request is an ajax request in a controller method in Turbogears? Further, is it possible to return a 'partial' much like in rails or symfony if the request is an ajax request. I know about the json decorator but I need a way to return a partial of a mako template (because I need to fo...
0
2010-02-02T19:45:46Z
2,187,227
<p>jQuery, YUI, Prototype, Dojo, and MooTools all set the header <code>X-Requested-With: XMLHttpRequest</code>. You should be able to check for that header.</p>
1
2010-02-02T20:02:32Z
[ "python", "ajax", "mako", "turbogears2" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
2,187,161
<p>On 64-bit Python:</p> <pre><code>&gt;&gt;&gt; xrange(9999999999999) xrange(9999999999999) </code></pre> <p>I would not use <code>range()</code> for a 13-digit number. My poor machine would not be able to hold the resultant list.</p>
2
2010-02-02T19:52:07Z
[ "python", "range", "numbers", "xrange" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
2,187,168
<p>I don't think it will work. Functions like <code>len</code> expect the result to fit into a 4 byte integer, due to restrictions in the cPython implementation.</p> <p>In Python 3.0:</p> <pre><code>&gt;&gt;&gt; range(9999999999999) range(0, 9999999999999) </code></pre> <p>It looks like it works, but...</p> <pre><c...
2
2010-02-02T19:52:52Z
[ "python", "range", "numbers", "xrange" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
2,187,176
<p>No problems with creating the range, as long as you don't want 10**13 elements, e.g. </p> <pre><code>range(10**14,10**15,10**14) </code></pre> <p>gives</p> <pre><code>[100000000000000, 200000000000000, 300000000000000, 400000000000000, 500000000000000, 600000000000000, 700000000000000, 800000000000000, 9000000000...
6
2010-02-02T19:53:58Z
[ "python", "range", "numbers", "xrange" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
2,187,235
<p>range(x) returns a list.Python lists cant contain that many elements. You should use xrange() to iterate through those digits if you need to do trillions of cycles.?</p>
1
2010-02-02T20:03:27Z
[ "python", "range", "numbers", "xrange" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
2,187,267
<p>range() and xrange() work in recent enough versions of Python; however, in 2.5 or less you'll need to work around the int to long conversion.</p> <pre><code>def irange(start, stop=None, step=1): if stop is None: stop = long(start) num = 1L else: stop = long(stop) num = long(s...
1
2010-02-02T20:08:08Z
[ "python", "range", "numbers", "xrange" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
2,187,310
<p>if you need enumerating integer try using <a href="http://docs.python.org/library/itertools.html">itertools</a>:</p> <pre><code>itertools.count(1000000000000) </code></pre> <p>it should not allocate memory for a list of <code>1000000000000</code> elements</p>
6
2010-02-02T20:11:45Z
[ "python", "range", "numbers", "xrange" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
2,187,319
<p>You could try this. Same semantics as range:</p> <pre><code>import operator def lrange(num1, num2 = None, step = 1): op = operator.__lt__ if num2 is None: num1, num2 = 0, num1 if num2 &lt; num1: if step &gt; 0: num1 = num2 op = operator.__gt__ elif step &lt; 0: ...
12
2010-02-02T20:12:45Z
[ "python", "range", "numbers", "xrange" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
2,187,340
<p>The difference between range() and xrange() is that the first returns the entire list, while the second returns a generator that generates each number as it is needed. The second one should work for any number, no matter how large.</p> <p>In Python 3.0, xrange() has disappeared and range() behaves like xrange() did...
0
2010-02-02T20:14:53Z
[ "python", "range", "numbers", "xrange" ]
range and xrange for 13-digit numbers in Python?
2,187,135
<p><code>range()</code> and <code>xrange()</code> work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.</p>
12
2010-02-02T19:49:16Z
17,009,029
<p>For sollution of this problem you don't need such long numbers, because you need only prime factors, you can use square root:</p> <pre><code>for i in xrange(2, int((n+1)**0.5)): </code></pre>
0
2013-06-09T11:17:07Z
[ "python", "range", "numbers", "xrange" ]
How can I catch server json response while uploading image using Ajax?
2,187,211
<p>I have a form which is loaded via jQuery from the external template file:</p> <pre><code>$('#imguploadform').html('&amp;nbsp;').load('{% url upload_form %}'); </code></pre> <p>In template it looks like this:</p> <pre><code>&lt;img src="{{ MEDIA_URL }}img/misc/upload.png" alt="Illustration" title="myimage" /&gt; &...
0
2010-02-02T20:00:20Z
2,188,147
<p>Have your success callback take a parameter. That will be the response.</p>
0
2010-02-02T22:17:16Z
[ "python", "django", "image", "upload" ]
How can I catch server json response while uploading image using Ajax?
2,187,211
<p>I have a form which is loaded via jQuery from the external template file:</p> <pre><code>$('#imguploadform').html('&amp;nbsp;').load('{% url upload_form %}'); </code></pre> <p>In template it looks like this:</p> <pre><code>&lt;img src="{{ MEDIA_URL }}img/misc/upload.png" alt="Illustration" title="myimage" /&gt; &...
0
2010-02-02T20:00:20Z
2,190,948
<pre><code>var submit_options = { target: '#picworks', dataType: 'json', success: function(response) { alert('It Works!'); window.location.href = response.path; } }; </code></pre>
0
2010-02-03T09:36:52Z
[ "python", "django", "image", "upload" ]
How can I catch server json response while uploading image using Ajax?
2,187,211
<p>I have a form which is loaded via jQuery from the external template file:</p> <pre><code>$('#imguploadform').html('&amp;nbsp;').load('{% url upload_form %}'); </code></pre> <p>In template it looks like this:</p> <pre><code>&lt;img src="{{ MEDIA_URL }}img/misc/upload.png" alt="Illustration" title="myimage" /&gt; &...
0
2010-02-02T20:00:20Z
2,193,316
<p>I solved the problem! The thing is that function which replace form submitting action with an ajax request is called earlier than the form loads from external file. So it should be like this:</p> <pre><code>$('#imguploadform').html('&amp;nbsp;').load('{% url upload_form %}', function(){ var submit_options...
0
2010-02-03T16:03:32Z
[ "python", "django", "image", "upload" ]
Python: Print only the message on warnings
2,187,269
<p>I'm issuing lots of warnings in a validator, and I'd like to suppress everything in stdout except the message that is supplied to <code>warnings.warn()</code>.</p> <p>I.e., now I see this:</p> <pre><code>./file.py:123: UserWarning: My looong warning message some Python code </code></pre> <p>I'd like to see this:<...
13
2010-02-02T20:08:10Z
2,187,337
<p>Monkeypatch <a href="http://docs.python.org/library/warnings.html#warnings.showwarning"><code>warnings.showwarning()</code></a> with your own custom function.</p>
9
2010-02-02T20:14:34Z
[ "python", "warnings" ]
Python: Print only the message on warnings
2,187,269
<p>I'm issuing lots of warnings in a validator, and I'd like to suppress everything in stdout except the message that is supplied to <code>warnings.warn()</code>.</p> <p>I.e., now I see this:</p> <pre><code>./file.py:123: UserWarning: My looong warning message some Python code </code></pre> <p>I'd like to see this:<...
13
2010-02-02T20:08:10Z
2,187,390
<p>There is always monkeypatching:</p> <pre><code>import warnings def custom_formatwarning(msg, *a): # ignore everything except the message return str(msg) + '\n' warnings.formatwarning = custom_formatwarning warnings.warn("achtung") </code></pre>
7
2010-02-02T20:21:44Z
[ "python", "warnings" ]
Python: Print only the message on warnings
2,187,269
<p>I'm issuing lots of warnings in a validator, and I'd like to suppress everything in stdout except the message that is supplied to <code>warnings.warn()</code>.</p> <p>I.e., now I see this:</p> <pre><code>./file.py:123: UserWarning: My looong warning message some Python code </code></pre> <p>I'd like to see this:<...
13
2010-02-02T20:08:10Z
2,187,450
<p>Use the <a href="http://docs.python.org/library/logging.html" rel="nofollow">logging module</a> instead of <code>warnings</code>.</p>
2
2010-02-02T20:30:16Z
[ "python", "warnings" ]
Python: Print only the message on warnings
2,187,269
<p>I'm issuing lots of warnings in a validator, and I'd like to suppress everything in stdout except the message that is supplied to <code>warnings.warn()</code>.</p> <p>I.e., now I see this:</p> <pre><code>./file.py:123: UserWarning: My looong warning message some Python code </code></pre> <p>I'd like to see this:<...
13
2010-02-02T20:08:10Z
26,256,592
<p>Here's what I'm doing to omit <em>just</em> the source code line. This is by and large as suggested by the documentation, but it was a bit of a struggle to figure out what exactly to change. (In particular, I tried in various ways to keep the source line out of <code>showwarnings</code> but couldn't get it to work ...
2
2014-10-08T12:11:16Z
[ "python", "warnings" ]
Manage Increasingly complex Django URL views efficiently? (urls.py)
2,187,273
<p>So, I have this Django application and I keep adding new features to provide ever more granular views of the data. To give a quick idea of the problem, here's a subset of <code>urls.py</code>:</p> <pre><code># Simple enough . . . (r'^$', 'index'), (r'^date/(?P&lt;year&gt;\d{4})$', 'index'), (r'^date/(?P&lt;year&gt...
1
2010-02-02T20:08:27Z
2,187,478
<p>Why not use GET parameters, or <a href="http://github.com/alex/django-filter" rel="nofollow">django-filter</a>, if all you do is just filter/group the results differently?</p> <p>The reasons I see for using GET are that its easier to implement, and seems a little cleaner: in the URL solution /search/foo/user/bar/ a...
2
2010-02-02T20:33:28Z
[ "python", "django", "django-urls" ]
Manage Increasingly complex Django URL views efficiently? (urls.py)
2,187,273
<p>So, I have this Django application and I keep adding new features to provide ever more granular views of the data. To give a quick idea of the problem, here's a subset of <code>urls.py</code>:</p> <pre><code># Simple enough . . . (r'^$', 'index'), (r'^date/(?P&lt;year&gt;\d{4})$', 'index'), (r'^date/(?P&lt;year&gt...
1
2010-02-02T20:08:27Z
2,187,500
<p>Let's focus on this one:</p> <pre><code> # Simple enough . . . (r'^$', 'index'), (r'^date/(?P&lt;year&gt;\d{4})$', 'index'), (r'^date/(?P&lt;year&gt;\d{4})-(?P&lt;month&gt;\d{2})$', 'index'), (r'^date/(?P&lt;year&gt;\d{4})-(?P&lt;month&gt;\d{2})-(?P&lt;day&gt;\d{2})$', 'index'), (r'^page/(?P&...
0
2010-02-02T20:36:14Z
[ "python", "django", "django-urls" ]
Making a module global?
2,187,381
<p>I would like to know why</p> <pre><code> &gt;&gt;&gt; def func2(): ... global time ... import time ... &gt;&gt;&gt; time Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; NameError: name 'time' is not defined &gt;&gt;&gt; func2() &gt;&gt;&gt; time &lt;module 'time' (built-...
1
2010-02-02T20:20:11Z
2,187,407
<p>Each of your <code>exec()</code> calls happens in a separate namespace. Abandon this path; it will only lead to ruin.</p>
7
2010-02-02T20:24:38Z
[ "python", "module", "global" ]
Making a module global?
2,187,381
<p>I would like to know why</p> <pre><code> &gt;&gt;&gt; def func2(): ... global time ... import time ... &gt;&gt;&gt; time Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; NameError: name 'time' is not defined &gt;&gt;&gt; func2() &gt;&gt;&gt; time &lt;module 'time' (built-...
1
2010-02-02T20:20:11Z
2,187,409
<p>Because <code>exec</code> uses its own scope by default. If you do <code>exec "global {0}; import {0}".format(module) in globals()</code>, then it'll work.</p> <p>You shouldn't be doing that, unless you really need to.</p>
1
2010-02-02T20:24:48Z
[ "python", "module", "global" ]
Making a module global?
2,187,381
<p>I would like to know why</p> <pre><code> &gt;&gt;&gt; def func2(): ... global time ... import time ... &gt;&gt;&gt; time Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; NameError: name 'time' is not defined &gt;&gt;&gt; func2() &gt;&gt;&gt; time &lt;module 'time' (built-...
1
2010-02-02T20:20:11Z
2,187,437
<p>What you are trying to do is either very sophisticated or very odd. This is how it works:</p> <pre><code>exec ("import %s" % module) in globals() </code></pre> <p>Please describe the bigger problem you are trying to solve</p>
0
2010-02-02T20:28:24Z
[ "python", "module", "global" ]
Making a module global?
2,187,381
<p>I would like to know why</p> <pre><code> &gt;&gt;&gt; def func2(): ... global time ... import time ... &gt;&gt;&gt; time Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; NameError: name 'time' is not defined &gt;&gt;&gt; func2() &gt;&gt;&gt; time &lt;module 'time' (built-...
1
2010-02-02T20:20:11Z
2,187,525
<p>To import a module given the name as a string use</p> <pre><code>time=__import__('time') </code></pre> <p>Here's one way you might use it</p> <pre><code>usermodulenames = ["foo","bar","baz"] usermodules = dict((k,__import__(k)) for k in usermodulenames) </code></pre>
1
2010-02-02T20:40:23Z
[ "python", "module", "global" ]
Installing numpy broke NLTK (OS X 10.6.2, Python 2.6)
2,187,510
<p>I had a working installation of NLTK (py26-nltk) on my Mac (OS X 10.6.2). Then I installed numpy. Now when I try to import nltk, I get this:</p> <pre><code>&gt;&gt;&gt; import nltk Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "nltk/__init__.py", line 83, in &lt;module&...
0
2010-02-02T20:37:26Z
2,187,676
<p>It seems to be more of a matter of [version] <strong>incompatibility between SciPy and NumPy</strong> versions than between NLTK and Numpy.<br> While SciPy is not required for NLTK, it is an optional import, and will load if available.</p> <p>A few hypothesis regarding your situation:</p> <p>Hyp #1</p> <ul> <li>y...
3
2010-02-02T21:01:57Z
[ "python", "numpy", "nltk" ]
Installing numpy broke NLTK (OS X 10.6.2, Python 2.6)
2,187,510
<p>I had a working installation of NLTK (py26-nltk) on my Mac (OS X 10.6.2). Then I installed numpy. Now when I try to import nltk, I get this:</p> <pre><code>&gt;&gt;&gt; import nltk Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "nltk/__init__.py", line 83, in &lt;module&...
0
2010-02-02T20:37:26Z
3,444,910
<p>I had the same problem on Python 2.6 on Windows XP and as suggested by mjv, I uninstalled my old SciPy module and installed the latest, at which stage I got the error that the yaml module was missing.</p> <p>After installing the PyYAML module, the "import nltk" statement didn't give any errors.</p> <p>Hope this he...
1
2010-08-09T23:19:50Z
[ "python", "numpy", "nltk" ]
Installing numpy broke NLTK (OS X 10.6.2, Python 2.6)
2,187,510
<p>I had a working installation of NLTK (py26-nltk) on my Mac (OS X 10.6.2). Then I installed numpy. Now when I try to import nltk, I get this:</p> <pre><code>&gt;&gt;&gt; import nltk Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "nltk/__init__.py", line 83, in &lt;module&...
0
2010-02-02T20:37:26Z
8,992,476
<p>Chiming in somewhat late, but I ran into this problem with numpy and fisher, on osx (snow leopard, but I assume this would happen on other x86 machines).</p> <p>I had installed fisher via pypm, and installed numpy from a binary dmg. In a situation like this I assume that somewhere along the way a binary got infect...
1
2012-01-24T19:00:10Z
[ "python", "numpy", "nltk" ]
python 2.6 cPickle.load results in EOFError
2,187,558
<p>I use cPickle to pickle a list of integers, using HIGHEST_PROTOCOL,</p> <pre><code>cPickle.dump(l, f, HIGHEST_PROTOCOL) </code></pre> <p>When I try to unpickle this using the following code, I get an EOFError. I tried 'seeking' to offset 0 before unpickling, but the error persists.</p> <pre><code>l = cPickle.load...
8
2010-02-02T20:46:03Z
2,187,579
<p>If you are on windows, make sure you </p> <pre><code>open(filename, 'wb') # for writing open(filename, 'rb') # for reading </code></pre>
14
2010-02-02T20:49:38Z
[ "python", "pickle", "eoferror" ]
How can I render a Django template that has UTF8 characters in it?
2,187,561
<p>I'm trying to send a django email with UTF-8 characters in the template, specifically:</p> <pre> S'il vous plaît </pre> <p>I get the error:</p> <pre> UnicodeDecodeError: 'utf8' codec can't decode byte 0x94 in position 147: unexpected code byte </pre> <p>When trying to encode the special "î" character (that is ...
2
2010-02-02T20:47:02Z
2,187,606
<p><code>0x94</code> is not part of <code>î</code> in UTF-8. The UTF-8 encoding for î is <code>0xc3 0xae</code>.</p>
1
2010-02-02T20:52:44Z
[ "python", "django", "django-templates" ]
How can I render a Django template that has UTF8 characters in it?
2,187,561
<p>I'm trying to send a django email with UTF-8 characters in the template, specifically:</p> <pre> S'il vous plaît </pre> <p>I get the error:</p> <pre> UnicodeDecodeError: 'utf8' codec can't decode byte 0x94 in position 147: unexpected code byte </pre> <p>When trying to encode the special "î" character (that is ...
2
2010-02-02T20:47:02Z
2,187,613
<p>The editor you're using has saved the file using Mac Roman encoding. Open the template, re-save it as UTF-8, and it should work fine.</p>
6
2010-02-02T20:53:32Z
[ "python", "django", "django-templates" ]
Which is web.py killer app?
2,187,610
<p>A killer app is an app that make a library or framework famous. I think web.py is quite famous, but I don't know any big, widely used app written in web.py.</p> <p>Could you point out any? I've head that the first version of youtube.com was coded using web.py but I'd like you to mention an open source one so I can ...
4
2010-02-02T20:53:10Z
2,187,794
<p>Well, when <a href="http://www.reddit.com/" rel="nofollow">Reddit</a> first moved from Common Lisp to Python they used web.py (<a href="http://www.aaronsw.com/weblog/rewritingreddit" rel="nofollow">src</a>, <a href="http://en.wikipedia.org/wiki/Reddit#Technology" rel="nofollow">src</a>). That made a pretty big spla...
4
2010-02-02T21:19:13Z
[ "python", "web.py" ]
Which is web.py killer app?
2,187,610
<p>A killer app is an app that make a library or framework famous. I think web.py is quite famous, but I don't know any big, widely used app written in web.py.</p> <p>Could you point out any? I've head that the first version of youtube.com was coded using web.py but I'd like you to mention an open source one so I can ...
4
2010-02-02T20:53:10Z
2,187,798
<p>From <a href="http://webpy.org/src" rel="nofollow">web.py</a> website here is a list of "Real Web Apps" written in web.py. None of them has yet become the next twitter.</p> <ul> <li><a href="http://redditriver.com/" rel="nofollow">redditriver.com</a>: a mobile version of reddit.com</li> <li><a href="http://wm.just...
7
2010-02-02T21:19:53Z
[ "python", "web.py" ]
Which is web.py killer app?
2,187,610
<p>A killer app is an app that make a library or framework famous. I think web.py is quite famous, but I don't know any big, widely used app written in web.py.</p> <p>Could you point out any? I've head that the first version of youtube.com was coded using web.py but I'd like you to mention an open source one so I can ...
4
2010-02-02T20:53:10Z
2,205,554
<p>A killer app isn't necessarily defined by whether it makes the framework/language famous. The Killer 'App' is an application that disrupts its industry in such a way that the rules or status quo of that industry is changed forever. E.g. the iPhone. </p> <p>It is 'killer' because it 'kills' off market leaders in its...
0
2010-02-05T07:07:46Z
[ "python", "web.py" ]
What is the equivalent of php's print_r() in python?
2,187,821
<p>Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.</p>
55
2010-02-02T21:24:26Z
2,187,834
<p>Check out <a href="http://docs.python.org/library/pprint.html">pprint module</a>.</p>
18
2010-02-02T21:26:00Z
[ "php", "python" ]
What is the equivalent of php's print_r() in python?
2,187,821
<p>Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.</p>
55
2010-02-02T21:24:26Z
2,187,843
<p>The python <a href="http://docs.python.org/reference/simple_stmts.html#print">print</a> statement does a good job of formatting multidimesion arrays without requiring the <a href="http://php.net/manual/en/function.print-r.php">print_r</a> available in php.</p> <p>As the definition for print states that each object ...
33
2010-02-02T21:27:51Z
[ "php", "python" ]
What is the equivalent of php's print_r() in python?
2,187,821
<p>Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.</p>
55
2010-02-02T21:24:26Z
2,187,885
<p><code>print</code> and <code>pprint</code> are great for built-in data types or classes which define a sane object representation. If you want a full dump of arbitrary objects, you'll have to roll your own. That is not that hard: simply create a recursive function with the base case being any non-container built-in ...
4
2010-02-02T21:35:03Z
[ "php", "python" ]
What is the equivalent of php's print_r() in python?
2,187,821
<p>Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.</p>
55
2010-02-02T21:24:26Z
4,390,580
<p>there is print_r for python <a href="https://github.com/marcbelmont/python-print_r/wiki" rel="nofollow">https://github.com/marcbelmont/python-print_r/wiki</a> but it is better to use standard modules</p>
2
2010-12-08T17:45:55Z
[ "php", "python" ]
What is the equivalent of php's print_r() in python?
2,187,821
<p>Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.</p>
55
2010-02-02T21:24:26Z
16,646,600
<p>You were looking for the <code>repr</code> bult-in function.<br> <a href="http://docs.python.org/2/library/functions.html#func-repr" rel="nofollow">http://docs.python.org/2/library/functions.html#func-repr</a></p> <pre><code>print repr(variable) </code></pre>
1
2013-05-20T09:38:05Z
[ "php", "python" ]
What is the equivalent of php's print_r() in python?
2,187,821
<p>Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.</p>
55
2010-02-02T21:24:26Z
18,012,749
<p>here's one you can try:</p> <p><a href="https://github.com/sha256/python-var-dump" rel="nofollow">https://github.com/sha256/python-var-dump</a></p> <p>you can install it simply using <code>pip</code></p> <pre><code>pip install var_dump </code></pre> <p>disclaimer: I wrote it :)</p>
1
2013-08-02T08:59:01Z
[ "php", "python" ]
What is the equivalent of php's print_r() in python?
2,187,821
<p>Or is there a better way to quickly output the contents of an array (multidimensional or what not). Thanks.</p>
55
2010-02-02T21:24:26Z
27,543,327
<pre><code>from pprint import pprint student = {'Student1': { 'Age':10, 'Roll':1 }, 'Student2': { 'Age':12, 'Roll':2 }, 'Student3': { 'Age':11, 'Roll':3 }, 'Student4': { 'Age':13, 'Roll':4 }, 'Student5': { 'Age':10, 'Roll':5 } } pprint(student) </code></pre>
1
2014-12-18T09:36:31Z
[ "php", "python" ]
Python: how to inherit from two classes?
2,187,899
<p>First of all I'm a python newbie.</p> <p>I'm playing with Django and I'm trying to extend some classes.</p> <p>Now I'm in this situation:</p> <p>I have a new class </p> <pre><code>customBaseModelAdmin(admin.options.BaseModelAdmin): #override a method of BaseModelAdmin </code></pre> <p>and I want to write an...
1
2010-02-02T21:37:19Z
2,187,933
<p>Why not just subclass ModelAdmin for customBaseModelAdmin?</p>
2
2010-02-02T21:43:29Z
[ "python", "django" ]
Python: how to inherit from two classes?
2,187,899
<p>First of all I'm a python newbie.</p> <p>I'm playing with Django and I'm trying to extend some classes.</p> <p>Now I'm in this situation:</p> <p>I have a new class </p> <pre><code>customBaseModelAdmin(admin.options.BaseModelAdmin): #override a method of BaseModelAdmin </code></pre> <p>and I want to write an...
1
2010-02-02T21:37:19Z
2,187,942
<p>Just let <code>customBaseModelAdmin</code> inherit from <code>ModelAdmin</code>. You can still override the method from <code>BaseModelAdmin</code>.</p> <p>But of course it could be that <code>ModelAdmin</code> also overrides this method. I would take a look at the source code of these classes to really know what i...
2
2010-02-02T21:44:49Z
[ "python", "django" ]
Python: how to inherit from two classes?
2,187,899
<p>First of all I'm a python newbie.</p> <p>I'm playing with Django and I'm trying to extend some classes.</p> <p>Now I'm in this situation:</p> <p>I have a new class </p> <pre><code>customBaseModelAdmin(admin.options.BaseModelAdmin): #override a method of BaseModelAdmin </code></pre> <p>and I want to write an...
1
2010-02-02T21:37:19Z
2,187,976
<p>You do it like this:</p> <pre><code>class customModelAdmin(customBaseModelAdmin): etc </code></pre>
0
2010-02-02T21:48:16Z
[ "python", "django" ]
Named arguments: C# vs Python
2,187,921
<p>Both C# and Python allow named arguments, so you can write something like: <code>foo(bar:1)</code>. This is great, especially in combination with optional arguments.</p> <p>My question is: <strong>what are the differences between the C# and Python named arguments, if any?</strong> I'm not interested in which is the...
5
2010-02-02T21:41:17Z
2,187,984
<p>Python lets you "catch" unspecified named arguments into a dict, which is pretty handy</p> <pre><code>&gt;&gt;&gt; def f(**kw): ... print kw ... &gt;&gt;&gt; f(size=3, sides=6, name="hexagon") {'sides': 6, 'name': 'hexagon', 'size': 3} </code></pre>
6
2010-02-02T21:49:37Z
[ "c#", "python", "kwargs" ]
Named arguments: C# vs Python
2,187,921
<p>Both C# and Python allow named arguments, so you can write something like: <code>foo(bar:1)</code>. This is great, especially in combination with optional arguments.</p> <p>My question is: <strong>what are the differences between the C# and Python named arguments, if any?</strong> I'm not interested in which is the...
5
2010-02-02T21:41:17Z
2,188,287
<p>Python not only lets you catch unspecified named arguments into a dict, but also lets you unpack a dict into arguments:</p> <pre><code> &gt;&gt;&gt; def f(alfa, beta, gamma): ... print alfa, beta, gamma ... &gt;&gt;&gt; f(**{'alfa': 1, 'beta': 2, 'gamma': 3}) 1 2 3 </code></pre> <p>and pass ...
5
2010-02-02T22:35:38Z
[ "c#", "python", "kwargs" ]
Possible to retrieve an arbitrary unordered set of named groups in one swoop with Python's re module?
2,187,931
<p>This is super handy for some problems:</p> <pre><code>&gt;&gt;&gt; re.search('(?P&lt;b&gt;.b.).*(?P&lt;i&gt;.i.)', 'abcdefghijk').groupdict() {'i': 'hij', 'b': 'abc'} </code></pre> <p>But what if I don't know what order to expect ahead of time? </p> <p>[update]</p> <p>Eg, say I have an input variable containing...
6
2010-02-02T21:43:08Z
2,188,073
<pre><code>&gt;&gt;&gt; [m.groupdict() for m in re.finditer('(?P&lt;b&gt;.b.)|(?P&lt;i&gt;.i.)', 'abcdefghijk')] [{'i': None, 'b': 'abc'}, {'i': 'hij', 'b': None}] </code></pre> <p>Seems to work fine, though if you have many groups checking which one isn't <code>None</code> might get tedious.</p> <p>This finds all <c...
0
2010-02-02T22:06:58Z
[ "python", "regex", "unordered" ]
Possible to retrieve an arbitrary unordered set of named groups in one swoop with Python's re module?
2,187,931
<p>This is super handy for some problems:</p> <pre><code>&gt;&gt;&gt; re.search('(?P&lt;b&gt;.b.).*(?P&lt;i&gt;.i.)', 'abcdefghijk').groupdict() {'i': 'hij', 'b': 'abc'} </code></pre> <p>But what if I don't know what order to expect ahead of time? </p> <p>[update]</p> <p>Eg, say I have an input variable containing...
6
2010-02-02T21:43:08Z
2,188,076
<p>Use a vertical bar ("or") in the RE pattern, and <code>finditer</code> to get all match objects of interest: each will have a <code>groupdict</code> with <code>None</code> as the value for groups not involved in that match, and you can "merge" the dicts as you prefer.</p> <p>For example:</p> <pre><code>import re ...
1
2010-02-02T22:07:38Z
[ "python", "regex", "unordered" ]
Possible to retrieve an arbitrary unordered set of named groups in one swoop with Python's re module?
2,187,931
<p>This is super handy for some problems:</p> <pre><code>&gt;&gt;&gt; re.search('(?P&lt;b&gt;.b.).*(?P&lt;i&gt;.i.)', 'abcdefghijk').groupdict() {'i': 'hij', 'b': 'abc'} </code></pre> <p>But what if I don't know what order to expect ahead of time? </p> <p>[update]</p> <p>Eg, say I have an input variable containing...
6
2010-02-02T21:43:08Z
2,188,087
<p>The closest I can get is this:</p> <pre><code>&gt;&gt;&gt; [match.groupdict() for match in re.finditer('(?P&lt;b&gt;.b.)|(?P&lt;i&gt;.i.)', 'abcdefghijk')] [{'i': None, 'b': 'abc'}, {'i': 'hij', 'b': None}] </code></pre> <p>How you combine the dictionaries then depends on whether you're expecting more than one mat...
0
2010-02-02T22:08:40Z
[ "python", "regex", "unordered" ]
Possible to retrieve an arbitrary unordered set of named groups in one swoop with Python's re module?
2,187,931
<p>This is super handy for some problems:</p> <pre><code>&gt;&gt;&gt; re.search('(?P&lt;b&gt;.b.).*(?P&lt;i&gt;.i.)', 'abcdefghijk').groupdict() {'i': 'hij', 'b': 'abc'} </code></pre> <p>But what if I don't know what order to expect ahead of time? </p> <p>[update]</p> <p>Eg, say I have an input variable containing...
6
2010-02-02T21:43:08Z
15,112,978
<p>Here is a way which doesn't require <code>finditer</code> nor dictionary merging:</p> <pre><code>&gt;&gt;&gt; pat = re.compile(r'(?:.*?(?:(?P&lt;b&gt;.b.)|(?P&lt;i&gt;.i.))){2}') &gt;&gt;&gt; pat.search('abcdefghijk').groupdict() {'i': 'hij', 'b': 'abc'} &gt;&gt;&gt; pat.search('aicdefghbjk').groupdict() {'i': 'a...
0
2013-02-27T13:11:53Z
[ "python", "regex", "unordered" ]
Integrating Python or Perl with PHP
2,187,998
<p>I'm going to help my friend in a improve of his phpBB board, but I want to make somethings there in Python or Perl. But it's possible to integrate these languages with PHP?</p>
2
2010-02-02T21:53:41Z
2,188,027
<p>You can always call the python or perl interpreter from within PHP! Minimalistic interchange is possible by means of passing command line arguments and capturing stdout (<code>exec</code> or <code>passthru</code> are related php functions).</p> <p>However, I don't think its's a good idea - using two interpreters in...
8
2010-02-02T21:58:10Z
[ "php", "python", "perl", "integration", "phpbb" ]
Integrating Python or Perl with PHP
2,187,998
<p>I'm going to help my friend in a improve of his phpBB board, but I want to make somethings there in Python or Perl. But it's possible to integrate these languages with PHP?</p>
2
2010-02-02T21:53:41Z
2,188,168
<p>I'd say that the only reasonable way of doing that is if you are making a separate service, that you talk to via Ajax or XML or something like that. Everything else is more trouble than it's worth.</p>
8
2010-02-02T22:19:01Z
[ "php", "python", "perl", "integration", "phpbb" ]
Integrating Python or Perl with PHP
2,187,998
<p>I'm going to help my friend in a improve of his phpBB board, but I want to make somethings there in Python or Perl. But it's possible to integrate these languages with PHP?</p>
2
2010-02-02T21:53:41Z
2,189,066
<p>There is <a href="http://pecl.php.net/package/perl" rel="nofollow">Perl PECL package</a> to integrate Perl into PHP.</p>
3
2010-02-03T01:25:57Z
[ "php", "python", "perl", "integration", "phpbb" ]
How to find class of bound method during class construction in Python 3.1?
2,188,409
<p>i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this:</p> <pre><code>def CLASS_WHERE_METHOD_IS_DEFINED( method ): ??? def foobar( method ): print( CLASS_WHERE_MET...
4
2010-02-02T22:55:39Z
2,188,518
<p>When the decorator is called, it's called with a function as its argument, <em>not</em> a method -- therefore it will avail nothing to the decorator to examine and introspect its method as much as it wants to, because it's only a function and carries no information whatsoever about the enclosing class. I hope this ...
6
2010-02-02T23:17:53Z
[ "python", "python-3.x" ]
How to find class of bound method during class construction in Python 3.1?
2,188,409
<p>i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this:</p> <pre><code>def CLASS_WHERE_METHOD_IS_DEFINED( method ): ??? def foobar( method ): print( CLASS_WHERE_MET...
4
2010-02-02T22:55:39Z
37,451,281
<p>This is a very old post, but introspection isn't the way to solve this problem, because it can be more easily solved with a <a href="https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/#controversy" rel="nofollow">metaclass</a> and a bit of clever class construction logic using <a href="https://docs....
0
2016-05-26T03:53:28Z
[ "python", "python-3.x" ]
Where is meta.local_fields set in django.db.models.base.py?
2,188,457
<p>I'm getting the error: Exception Value: (1110, "Column 'about' specified twice")</p> <p>As I was reviewing the Django error page, I noticed that the customizations the model User, seem to be appended to the List twice.</p> <p>This seems to be happening here in django/db/model/base.py in base_save(): </p> <pre...
0
2010-02-02T23:07:38Z
6,524,386
<p>I had the same problem. In my case, the code was executed twice. I had the following situation:</p> <p>In one app:</p> <pre><code>from apps.accounts.models import User </code></pre> <p>And in another app:</p> <pre><code>from accounts.models import User </code></pre> <p>Since the customization code was in the ac...
0
2011-06-29T16:54:45Z
[ "python", "django", "django-models" ]
How can I view the contents of an array within controller directly for debugging?
2,188,496
<p>I'm just getting started with pylons, and am trying to figure out how to view the contents of variables for debugging without rendering the template. </p> <p>For example:</p> <pre><code>class IndexController(BaseController): def index(self): # Return a rendered template #return render('/index.mako') ...
0
2010-02-02T23:14:12Z
2,188,541
<p>You can use <a href="http://docs.python.org/library/cgitb.html" rel="nofollow">cgitb</a> to debug web applications, it can output detailed tracebacks to files, including variables contents. Here is <a href="http://blog.doughellmann.com/2010/01/pymotw-cgitb-detailed-traceback-reports.html" rel="nofollow">an article</...
1
2010-02-02T23:21:45Z
[ "python", "pylons" ]
Writing good tests for Django applications
2,188,551
<p>I've never written any tests in my life, but I'd like to start writing tests for my Django projects. I've read some articles about tests and decided to try to write some tests for an extremely simple Django app or a start.</p> <p>The app has two views (a list view, and a detail view) and a model with four fields:</...
27
2010-02-02T23:23:48Z
2,188,904
<p>I am not perfect in testing but a few thoughts:</p> <blockquote> <p>Basically you should test every function, method, class, whatever, that you have written by yourself.</p> </blockquote> <p>This implies that you don't have to test functions, classes, etc. which the framework provides. </p> <p>That said, a quic...
17
2010-02-03T00:40:15Z
[ "python", "django", "unit-testing", "django-testing" ]
Writing good tests for Django applications
2,188,551
<p>I've never written any tests in my life, but I'd like to start writing tests for my Django projects. I've read some articles about tests and decided to try to write some tests for an extremely simple Django app or a start.</p> <p>The app has two views (a list view, and a detail view) and a model with four fields:</...
27
2010-02-02T23:23:48Z
2,189,476
<p>Break your tests into two completely separate kinds.</p> <ul> <li><p>Model tests. Put these in your <code>models.py</code> file with your model. These tests will exercise the methods in your model classes. You can do simple CRUD (Create, Retrieve, Update, Delete) to simply prove that your model works. Don't tes...
5
2010-02-03T03:25:47Z
[ "python", "django", "unit-testing", "django-testing" ]
How can I detect and track people using OpenCV?
2,188,646
<p>I have a camera that will be stationary, pointed at an indoors area. People will walk past the camera, within about 5 meters of it. Using <strong>OpenCV</strong>, I want to detect individuals walking past - my ideal return is an array of detected individuals, with bounding rectangles.</p> <p>I've looked at several ...
34
2010-02-02T23:50:00Z
2,190,799
<p>This is similar to a project we did as part of a Computer Vision course, and I can tell you right now that it is a hard problem to get right.</p> <p>You could use foreground/background segmentation, find all blobs and then decide that they are a person. The problem is that it will not work very well since people te...
1
2010-02-03T09:13:15Z
[ "python", "opencv", "computer-vision", "motion-detection" ]
How can I detect and track people using OpenCV?
2,188,646
<p>I have a camera that will be stationary, pointed at an indoors area. People will walk past the camera, within about 5 meters of it. Using <strong>OpenCV</strong>, I want to detect individuals walking past - my ideal return is an array of detected individuals, with bounding rectangles.</p> <p>I've looked at several ...
34
2010-02-02T23:50:00Z
2,260,865
<p>This is clearly a non-trivial task. You'll have to look into scientific publications for inspiration (<a href="http://scholar.google.com" rel="nofollow">Google Scholar</a> is your friend here). Here's a paper about human detection and tracking: <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.87.377...
4
2010-02-14T11:19:18Z
[ "python", "opencv", "computer-vision", "motion-detection" ]
How can I detect and track people using OpenCV?
2,188,646
<p>I have a camera that will be stationary, pointed at an indoors area. People will walk past the camera, within about 5 meters of it. Using <strong>OpenCV</strong>, I want to detect individuals walking past - my ideal return is an array of detected individuals, with bounding rectangles.</p> <p>I've looked at several ...
34
2010-02-02T23:50:00Z
2,300,503
<p>The latest SVN version of OpenCV contains an (undocumented) implementation of HOG-based pedestrian detection. It even comes with a pre-trained detector and a python wrapper. The basic usage is as follows:</p> <pre><code>from cv import * storage = CreateMemStorage(0) img = LoadImage(file) # or read from camera ...
26
2010-02-20T00:16:52Z
[ "python", "opencv", "computer-vision", "motion-detection" ]
How can I detect and track people using OpenCV?
2,188,646
<p>I have a camera that will be stationary, pointed at an indoors area. People will walk past the camera, within about 5 meters of it. Using <strong>OpenCV</strong>, I want to detect individuals walking past - my ideal return is an array of detected individuals, with bounding rectangles.</p> <p>I've looked at several ...
34
2010-02-02T23:50:00Z
2,352,019
<p>Nick,</p> <p>What you are looking for is not people detection, but motion detection. If you tell us a lot more about what you are trying to solve/do, we can answer better. Anyway, there are many ways to do motion detection depending on what you are going to do with the results. Simplest one would be differencing f...
5
2010-02-28T18:12:11Z
[ "python", "opencv", "computer-vision", "motion-detection" ]
How can I detect and track people using OpenCV?
2,188,646
<p>I have a camera that will be stationary, pointed at an indoors area. People will walk past the camera, within about 5 meters of it. Using <strong>OpenCV</strong>, I want to detect individuals walking past - my ideal return is an array of detected individuals, with bounding rectangles.</p> <p>I've looked at several ...
34
2010-02-02T23:50:00Z
39,634,243
<p>You should used features like HOOF to train the SVM.</p>
0
2016-09-22T08:43:49Z
[ "python", "opencv", "computer-vision", "motion-detection" ]
Stock Icons not shown on buttons
2,188,659
<pre><code>self.button = gtk.Button(stock=gtk.STOCK_DELETE) </code></pre> <p>Only Shows: Delete</p>
11
2010-02-02T23:51:26Z
2,188,784
<p>This is a recent change in GTK - the developers wanted icons not to appear on buttons. On Linux, this can be changed by editing the gconf key</p> <pre><code>/desktop/gnome/interface/buttons_have_icons </code></pre> <p>On windows, I think (I haven't actually tried this) that you need to set a value in your gtkrc fi...
9
2010-02-03T00:16:46Z
[ "python", "gtk", "pygtk" ]
Stock Icons not shown on buttons
2,188,659
<pre><code>self.button = gtk.Button(stock=gtk.STOCK_DELETE) </code></pre> <p>Only Shows: Delete</p>
11
2010-02-02T23:51:26Z
2,490,071
<p>I had to do this to get it to work from Python without changing my config file. When I called set_image(), no image was being displayed.</p> <pre><code>image = gtk.Image() image.set_from_stock(gtk.STOCK_**, gtk.ICON_SIZE_BUTTON) button = gtk.Button() button.add(image) button.show() </code></pre>
2
2010-03-22T04:51:19Z
[ "python", "gtk", "pygtk" ]
Stock Icons not shown on buttons
2,188,659
<pre><code>self.button = gtk.Button(stock=gtk.STOCK_DELETE) </code></pre> <p>Only Shows: Delete</p>
11
2010-02-02T23:51:26Z
3,130,657
<p>I had the same issue in GTKmm on Windows. The "MS-Windows" theme disables images on stock buttons and the theme has priority over settings in gtkrc (so putting gtk-button-images = true in gtkrc didn't help). What I did is to modify the GTK settings runtime, and the images appeared as expected. :) Here's the code in ...
1
2010-06-28T07:17:02Z
[ "python", "gtk", "pygtk" ]
Stock Icons not shown on buttons
2,188,659
<pre><code>self.button = gtk.Button(stock=gtk.STOCK_DELETE) </code></pre> <p>Only Shows: Delete</p>
11
2010-02-02T23:51:26Z
3,175,809
<p>The Python equivalent for setting the property without having to change any system config files is:</p> <pre><code>settings = gtk.settings_get_default() settings.props.gtk_button_images = True </code></pre> <p>This should follow a call to window.show() and, obviously, precede the gtk.main() loop.</p>
13
2010-07-04T18:50:01Z
[ "python", "gtk", "pygtk" ]
Stock Icons not shown on buttons
2,188,659
<pre><code>self.button = gtk.Button(stock=gtk.STOCK_DELETE) </code></pre> <p>Only Shows: Delete</p>
11
2010-02-02T23:51:26Z
9,119,829
<p>You can show explicitly the button image, justly, Gtk+ developers do not recommend doing this because it's overrides the Gtk+ user configuration.</p> <p>So...</p> <pre><code>button.get_image().show() </code></pre>
0
2012-02-02T20:51:41Z
[ "python", "gtk", "pygtk" ]
Stock Icons not shown on buttons
2,188,659
<pre><code>self.button = gtk.Button(stock=gtk.STOCK_DELETE) </code></pre> <p>Only Shows: Delete</p>
11
2010-02-02T23:51:26Z
11,121,309
<p>If you work with pygobject, the new syntax is:</p> <pre><code>image.set_from_stock(gtk.STOCK_**, Gtk.IconSize.BUTTON) </code></pre>
2
2012-06-20T14:00:27Z
[ "python", "gtk", "pygtk" ]
Stock Icons not shown on buttons
2,188,659
<pre><code>self.button = gtk.Button(stock=gtk.STOCK_DELETE) </code></pre> <p>Only Shows: Delete</p>
11
2010-02-02T23:51:26Z
32,809,264
<p>in Gtk3 <code>gtk.STOCK</code> method has been <a href="http://lazka.github.io/pgi-docs/Gtk-3.0/classes/Button.html#Gtk.Button.new_from_stock" rel="nofollow">deprecated from v3.10</a>.</p> <blockquote> <p>Deprecated since version 3.10: Use Gtk.Button.new_with_label () instead.</p> </blockquote> <p>In the case ...
0
2015-09-27T15:00:11Z
[ "python", "gtk", "pygtk" ]
Multiple Try-Excepts followed by an Else in python
2,188,754
<p>Is there some way to have several consecutive Try-Except clauses that trigger a single Else only if all of them are successful?</p> <p>As an example:</p> <pre><code> try: private.anodization_voltage_meter = Voltmeter(voltage_meter_address.value) #assign voltmeter location except(visa.VisaIOError): #channel tim...
4
2010-02-03T00:10:20Z
2,188,781
<p>You can keep a boolean, initialized at the beginning to: <code>everythingOK=True</code> Then set it to <code>False</code> in all the except blocks and log the final line only if true.</p>
3
2010-02-03T00:16:32Z
[ "python", "exception-handling" ]
Multiple Try-Excepts followed by an Else in python
2,188,754
<p>Is there some way to have several consecutive Try-Except clauses that trigger a single Else only if all of them are successful?</p> <p>As an example:</p> <pre><code> try: private.anodization_voltage_meter = Voltmeter(voltage_meter_address.value) #assign voltmeter location except(visa.VisaIOError): #channel tim...
4
2010-02-03T00:10:20Z
2,188,783
<p>Consider breaking the <code>try/except</code> structure into a function that returns <code>True</code> if the call worked and <code>False</code> if it failed, then use e.g. <code>all()</code> to see that they all succeded:</p> <pre><code>def initfunc(structure, attrname, address, desc): try: var = Voltmeter(a...
6
2010-02-03T00:16:43Z
[ "python", "exception-handling" ]
Multiple Try-Excepts followed by an Else in python
2,188,754
<p>Is there some way to have several consecutive Try-Except clauses that trigger a single Else only if all of them are successful?</p> <p>As an example:</p> <pre><code> try: private.anodization_voltage_meter = Voltmeter(voltage_meter_address.value) #assign voltmeter location except(visa.VisaIOError): #channel tim...
4
2010-02-03T00:10:20Z
2,188,787
<p>Personally I'd just have a trip variable <code>init_ok</code> or somesuch.</p> <p>Set it as True, and have all the except clauses set it False, then test at the end?</p>
6
2010-02-03T00:17:58Z
[ "python", "exception-handling" ]
Multiple Try-Excepts followed by an Else in python
2,188,754
<p>Is there some way to have several consecutive Try-Except clauses that trigger a single Else only if all of them are successful?</p> <p>As an example:</p> <pre><code> try: private.anodization_voltage_meter = Voltmeter(voltage_meter_address.value) #assign voltmeter location except(visa.VisaIOError): #channel tim...
4
2010-02-03T00:10:20Z
2,188,792
<p>Try something like this. keeping the original behaviour of not stopping after the first exception</p> <pre><code>success = True for meter, address, message in ( ('anodization_voltage_meter',voltage_meter_address,'Volt Meter is not on or not on this channel'), ('anodization_current_meter',current_met...
6
2010-02-03T00:19:30Z
[ "python", "exception-handling" ]
Multiple Try-Excepts followed by an Else in python
2,188,754
<p>Is there some way to have several consecutive Try-Except clauses that trigger a single Else only if all of them are successful?</p> <p>As an example:</p> <pre><code> try: private.anodization_voltage_meter = Voltmeter(voltage_meter_address.value) #assign voltmeter location except(visa.VisaIOError): #channel tim...
4
2010-02-03T00:10:20Z
2,190,694
<p>You've got a number of quite similar objects, and in cases like that it's often better to treat them uniformly and use a data-driven approach.</p> <p>So, I'd start with your <code>..._meter_address</code> objects. To me they sound like configuration for the meters, so I'd have a class that looks something like this...
2
2010-02-03T08:51:53Z
[ "python", "exception-handling" ]
getting the id of a created record in SQLAlchemy
2,188,844
<p>How can I get the id of the created record in SQLAlchemy?</p> <p>I'm doing:</p> <pre><code>engine.execute("insert into users values (1,'john')") </code></pre>
4
2010-02-03T00:29:51Z
2,188,862
<p>There's an extra clause you can add: <code>RETURNING</code></p> <p>ie</p> <pre><code>INSERT INTO users (name, address) VALUES ('richo', 'beaconsfield') RETURNING id </code></pre> <p>Then just retrieve a row like your insert was a SELECT statement.</p>
3
2010-02-03T00:32:22Z
[ "python", "insert", "sqlalchemy" ]