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
listlengths
1
5
Dynamic loading of python modules
951,124
<p>In python how do you dynamically add modules to a package while your programming is running.</p> <p>I want to be able to add modules to the package directory from an outside process, and be able to use those new modules in my program:</p> <pre><code>import package def doSomething(name): pkg = __import__("pack...
32
2009-06-04T14:59:25Z
951,256
<p>Your code is almost correct.</p> <p>See <a href="http://docs.python.org/library/functions.html#__import__"><code>__import__</code></a> function.</p> <pre><code>def doSomething(name): name = "package." + name mod = __import__(name, fromlist=['']) mod.doSomething() </code></pre>
38
2009-06-04T15:25:02Z
[ "python", "dynamic", "python-import" ]
Dynamic loading of python modules
951,124
<p>In python how do you dynamically add modules to a package while your programming is running.</p> <p>I want to be able to add modules to the package directory from an outside process, and be able to use those new modules in my program:</p> <pre><code>import package def doSomething(name): pkg = __import__("pack...
32
2009-06-04T14:59:25Z
951,268
<p>To detect changes to a directory, on Linux, you can use <a href="http://trac.dbzteam.org/pyinotify" rel="nofollow">pyinotify</a> (<a href="http://david-latham.blogspot.com/2008/06/python-inotify-pyinotify-how-to-watch.html" rel="nofollow">here</a> is a nice working example); on a Mac, <a href="http://www.wilcoxd.com...
3
2009-06-04T15:26:31Z
[ "python", "dynamic", "python-import" ]
Dynamic loading of python modules
951,124
<p>In python how do you dynamically add modules to a package while your programming is running.</p> <p>I want to be able to add modules to the package directory from an outside process, and be able to use those new modules in my program:</p> <pre><code>import package def doSomething(name): pkg = __import__("pack...
32
2009-06-04T14:59:25Z
951,678
<p>Bastien already answered the question, anyway you may find useful this function I use to load all the modules from a subfolder in a dictionary:</p> <pre><code>def loadModules(): res = {} import os # check subfolders lst = os.listdir("services") dir = [] for d in lst: s = os.path.absp...
16
2009-06-04T16:33:38Z
[ "python", "dynamic", "python-import" ]
Dynamic loading of python modules
951,124
<p>In python how do you dynamically add modules to a package while your programming is running.</p> <p>I want to be able to add modules to the package directory from an outside process, and be able to use those new modules in my program:</p> <pre><code>import package def doSomething(name): pkg = __import__("pack...
32
2009-06-04T14:59:25Z
951,846
<p>One trick with Bastien's answer... The <code>__import__()</code> function returns the package object, not the module object. If you use the following function, it will dynamically load the module from the package and return you the module, not the package.</p> <pre><code>def my_import(name): mod = __import__(...
8
2009-06-04T17:02:57Z
[ "python", "dynamic", "python-import" ]
How to build a fully-customizable application (aka database), without lose performance/good-design?
951,387
<p>im in the beginning of the complete restyle of an my web application, and i have some doubt about a good database-design that can be reliable, query-performance, and in the same time fully customizable by the users (users wont customize the database structure, but the funcionality of the application).</p> <p>So, my...
1
2009-06-04T15:45:52Z
951,423
<p>if you do it this way all of your queries will have to join to and use table_refer column, which will kill performance, and make simple queries hard, and hard queries very difficult.</p> <p>if your want multiple e-mails, split the email out to another table so you can have many rows.</p>
0
2009-06-04T15:51:52Z
[ "php", "python", "performance", "database-design", "postgresql" ]
How to build a fully-customizable application (aka database), without lose performance/good-design?
951,387
<p>im in the beginning of the complete restyle of an my web application, and i have some doubt about a good database-design that can be reliable, query-performance, and in the same time fully customizable by the users (users wont customize the database structure, but the funcionality of the application).</p> <p>So, my...
1
2009-06-04T15:45:52Z
951,431
<p>As I said in my <a href="http://stackoverflow.com/questions/885096/how-to-design-db-table-schema-with-ease/885135#885135">Answer</a> to a similar question, "Database Design is Hard." You are going to have to make the decision about which is better for you, normalizing the tables and bringing phone numbers and e-mai...
4
2009-06-04T15:53:15Z
[ "php", "python", "performance", "database-design", "postgresql" ]
How to build a fully-customizable application (aka database), without lose performance/good-design?
951,387
<p>im in the beginning of the complete restyle of an my web application, and i have some doubt about a good database-design that can be reliable, query-performance, and in the same time fully customizable by the users (users wont customize the database structure, but the funcionality of the application).</p> <p>So, my...
1
2009-06-04T15:45:52Z
951,439
<p>You could design your application to request additional data (like emails list for the user) on demand, using AJAX etc. In those highly customizable and rich applications usually you have no need to display all the data - only a single category. </p> <p>To store custom records you can create table <code>field_types...
0
2009-06-04T15:55:15Z
[ "php", "python", "performance", "database-design", "postgresql" ]
How to build a fully-customizable application (aka database), without lose performance/good-design?
951,387
<p>im in the beginning of the complete restyle of an my web application, and i have some doubt about a good database-design that can be reliable, query-performance, and in the same time fully customizable by the users (users wont customize the database structure, but the funcionality of the application).</p> <p>So, my...
1
2009-06-04T15:45:52Z
951,507
<p>I'm not sure about the specifics of postgresql, but if you want highly customisable data structures in a DB that you don't really want to search on, the s<a href="http://martinfowler.com/eaaCatalog/serializedLOB.html" rel="nofollow">erializing the data to a LOB</a> is an option.</p> <p>In fact this is the way ASP.N...
0
2009-06-04T16:08:26Z
[ "php", "python", "performance", "database-design", "postgresql" ]
How to build a fully-customizable application (aka database), without lose performance/good-design?
951,387
<p>im in the beginning of the complete restyle of an my web application, and i have some doubt about a good database-design that can be reliable, query-performance, and in the same time fully customizable by the users (users wont customize the database structure, but the funcionality of the application).</p> <p>So, my...
1
2009-06-04T15:45:52Z
951,546
<p>Your proposed model is composed of two database patterns: an <a href="http://en.wikipedia.org/wiki/Entity-attribute-value%5Fmodel" rel="nofollow">entity-attribute-value table</a> and a <a href="http://en.wikipedia.org/wiki/Polymorphic%5Fassociation" rel="nofollow">polymorphic association</a>.</p> <p>Entity-attribut...
1
2009-06-04T16:13:02Z
[ "php", "python", "performance", "database-design", "postgresql" ]
sudoku obfuscated python -> perl translation
951,666
<p>Anybody care to translate this into obfuscated perl? It's written in Python taken from: <a href="http://stackoverflow.com/questions/201461/shortest-sudoku-solver-in-python-how-does-it-work">here</a></p> <pre><code>def r(a):i=a.find('0');~i or exit(a);[m in[(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)or a[j]for j in ra...
1
2009-06-04T16:30:55Z
951,684
<p>There already are a few Sudoku solvers written in Obfuscated Perl, do you really want another (possibly less efficient) one?</p> <p>If not...</p> <ol> <li>De-obfuscate.</li> <li>Rewrite in Perl.</li> <li>Obfuscate.</li> </ol>
2
2009-06-04T16:36:19Z
[ "python", "perl", "translate" ]
sudoku obfuscated python -> perl translation
951,666
<p>Anybody care to translate this into obfuscated perl? It's written in Python taken from: <a href="http://stackoverflow.com/questions/201461/shortest-sudoku-solver-in-python-how-does-it-work">here</a></p> <pre><code>def r(a):i=a.find('0');~i or exit(a);[m in[(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)or a[j]for j in ra...
1
2009-06-04T16:30:55Z
952,137
<pre><code>sub r{($a=shift)=~/0/g?my$i=pos:die$a;T:for$m(1..9){($i-$_)%9*(int($i/9)^int($_/9))*(int($i/27)^int($_/27)|int($i%9/3)^int($_%9/3))||$a=~/^.{$_}$m/&amp;&amp;next T,for 0..80;substr($a,$i,1)=$m;r($a)}}r@ARGV </code></pre> <p>The braindead translation. Longer, since Python 2's <code>/</code> is integer divis...
3
2009-06-04T17:56:56Z
[ "python", "perl", "translate" ]
caching issues in MySQL response with MySQLdb in Django
952,216
<p>I use MySQL with MySQLdb module in Python, in Django.</p> <p>I'm running in autocommit mode in this case (and Django's transaction.is_managed() actually returns False).</p> <p>I have several processes interacting with the database. </p> <p>One process fetches all Task models with Task.objects.all()</p> <p>Then a...
2
2009-06-04T18:13:14Z
952,424
<p>This certainly seems autocommit/table locking - related.</p> <p>If mysqldb implements the dbapi2 spec it will probably have a connection running as one single continuous transaction. When you say: <code>'running in autocommit mode'</code>: do you mean MySQL itself or the mysqldb module? Or Django?</p> <p><em>Not i...
1
2009-06-04T18:54:12Z
[ "python", "mysql", "django", "connection", "commit" ]
How to sort based on dependencies?
952,302
<p>I have an class that has a list of "dependencies" pointing to other classes of the same base type.</p> <pre><code>class Foo(Base): dependencies = [] class Bar(Base): dependencies = [Foo] class Baz(Base): dependencies = [Bar] </code></pre> <p>I'd like to sort the instances these classes generate based...
7
2009-06-04T18:31:25Z
952,388
<p>It's called a topological sort.</p> <pre><code>def sort_deps(objs): queue = [objs with no dependencies] while queue: obj = queue.pop() yield obj for obj in objs: if dependencies are now satisfied: queue.append(obj) if not all dependencies are satisfied...
15
2009-06-04T18:47:11Z
[ "python", "sorting", "dependencies" ]
How to sort based on dependencies?
952,302
<p>I have an class that has a list of "dependencies" pointing to other classes of the same base type.</p> <pre><code>class Foo(Base): dependencies = [] class Bar(Base): dependencies = [Foo] class Baz(Base): dependencies = [Bar] </code></pre> <p>I'd like to sort the instances these classes generate based...
7
2009-06-04T18:31:25Z
952,856
<p>I had a similar question just last week - wish I'd know about Stack Overflow then! I hunted around a bit until I realized that I had a <em>DAG</em> (Directed <strong>Acyclic</strong> Graph since my dependencies couldn't be recursive or circular). Then I found a few references for algorithms to sort them. I used a...
5
2009-06-04T20:18:09Z
[ "python", "sorting", "dependencies" ]
Inferring appropriate database type declarations from strings in Python
952,541
<p>I am building some Postgres tables from Python dictionaries where the {'key': 'value'} pairs correspond to column 'key' and field 'value'. These are generated from .dbf files -- I now pipe the contents of the .dbf files into a script that returns a list of dicts like:</p> <pre><code>{'Warngentyp': '', 'Lon': '-81.6...
1
2009-06-04T19:14:58Z
952,588
<p>You can determine integers and floats unsafely by <code>type(eval(elem))</code>, where <code>elem</code> is an element of the list. (But then you need to check elem for possible bad code)</p> <p>A safer way could be to do the following</p> <pre><code>a = ['24.2', '.2', '2'] try: if all(elem.isdigit() for elem ...
1
2009-06-04T19:21:43Z
[ "python", "sqlite", "postgresql", "types" ]
Inferring appropriate database type declarations from strings in Python
952,541
<p>I am building some Postgres tables from Python dictionaries where the {'key': 'value'} pairs correspond to column 'key' and field 'value'. These are generated from .dbf files -- I now pipe the contents of the .dbf files into a script that returns a list of dicts like:</p> <pre><code>{'Warngentyp': '', 'Lon': '-81.6...
1
2009-06-04T19:14:58Z
952,618
<p>Don't use eval. If someone inserts bad code, it can hose your database or server.</p> <p>Instead use these</p> <pre><code>def isFloat(s): try: float(s) return True except (ValueError, TypeError), e: return False str.isdigit() </code></pre> <p>And everything else can be a varchar</p>
5
2009-06-04T19:26:18Z
[ "python", "sqlite", "postgresql", "types" ]
Inferring appropriate database type declarations from strings in Python
952,541
<p>I am building some Postgres tables from Python dictionaries where the {'key': 'value'} pairs correspond to column 'key' and field 'value'. These are generated from .dbf files -- I now pipe the contents of the .dbf files into a script that returns a list of dicts like:</p> <pre><code>{'Warngentyp': '', 'Lon': '-81.6...
1
2009-06-04T19:14:58Z
958,621
<p>Thanks for the help, this is a little long for an update, here is how I combined the answers. I am starting with a list of dicts like this, generated from a dbf file:</p> <pre><code>dbf_list = [{'Warngentyp': '', 'Lon': '-81.67170', 'Zwatch_war': '0', 'State':... </code></pre> <p>Then a function that returns 1000...
1
2009-06-06T00:01:24Z
[ "python", "sqlite", "postgresql", "types" ]
Inferring appropriate database type declarations from strings in Python
952,541
<p>I am building some Postgres tables from Python dictionaries where the {'key': 'value'} pairs correspond to column 'key' and field 'value'. These are generated from .dbf files -- I now pipe the contents of the .dbf files into a script that returns a list of dicts like:</p> <pre><code>{'Warngentyp': '', 'Lon': '-81.6...
1
2009-06-04T19:14:58Z
959,882
<p>YOU DON'T NEED TO INFER THE TYPE DECLARATIONS!!!</p> <p>You can derive what you want directly from the .dbf files. Each column has a name, a type code (C=Character, N=Number, D=Date (yyyymmdd), L=Logical (T/F), plus more types if the files are from Foxpro), a length (where relevant), and a number of decimal places ...
2
2009-06-06T15:03:41Z
[ "python", "sqlite", "postgresql", "types" ]
Help me find an appropriate ruby/python parser generator
952,648
<p>The first parser generator I've worked with was Parse::RecDescent, and the guides/tutorials available for it were great, but the most useful feature it has was it's debugging tools, specifically the <b>tracing</b> capabilities ( activated by setting $RD_TRACE to 1 ). I am looking for a parser generator that can help...
5
2009-06-04T19:34:32Z
952,680
<p>Python wiki has a <a href="http://wiki.python.org/moin/LanguageParsing" rel="nofollow">list</a> of Language Parsers written in Python.</p>
0
2009-06-04T19:40:21Z
[ "python", "ruby", "debugging", "parser-generator" ]
Help me find an appropriate ruby/python parser generator
952,648
<p>The first parser generator I've worked with was Parse::RecDescent, and the guides/tutorials available for it were great, but the most useful feature it has was it's debugging tools, specifically the <b>tracing</b> capabilities ( activated by setting $RD_TRACE to 1 ). I am looking for a parser generator that can help...
5
2009-06-04T19:34:32Z
952,785
<p>Python is a pretty easy language to debug. You can just do import pdb pdb.settrace().</p> <p>However, these parser generators supposedly come with good debugging facilities.</p> <p><a href="http://www.antlr.org/">http://www.antlr.org/</a></p> <p><a href="http://www.dabeaz.com/ply/">http://www.dabeaz.com/ply/</a><...
6
2009-06-04T20:02:20Z
[ "python", "ruby", "debugging", "parser-generator" ]
Help me find an appropriate ruby/python parser generator
952,648
<p>The first parser generator I've worked with was Parse::RecDescent, and the guides/tutorials available for it were great, but the most useful feature it has was it's debugging tools, specifically the <b>tracing</b> capabilities ( activated by setting $RD_TRACE to 1 ). I am looking for a parser generator that can help...
5
2009-06-04T19:34:32Z
983,823
<p>I don't know anything about its debugging features, but I've heard good things about PyParsing.</p> <p><a href="http://pyparsing.wikispaces.com/" rel="nofollow">http://pyparsing.wikispaces.com/</a></p>
2
2009-06-11T21:29:55Z
[ "python", "ruby", "debugging", "parser-generator" ]
Help me find an appropriate ruby/python parser generator
952,648
<p>The first parser generator I've worked with was Parse::RecDescent, and the guides/tutorials available for it were great, but the most useful feature it has was it's debugging tools, specifically the <b>tracing</b> capabilities ( activated by setting $RD_TRACE to 1 ). I am looking for a parser generator that can help...
5
2009-06-04T19:34:32Z
1,408,667
<p>I know the bounty has already been claimed, but here is an equivalent parser written in pyparsing (plus support for function calls with zero or more comma-delimted arguments):</p> <pre><code>from pyparsing import * LPAR, RPAR = map(Suppress,"()") EQ = Literal("=") name = Word(alphas, alphanums+"_").setName("name")...
2
2009-09-11T02:08:14Z
[ "python", "ruby", "debugging", "parser-generator" ]
Help me find an appropriate ruby/python parser generator
952,648
<p>The first parser generator I've worked with was Parse::RecDescent, and the guides/tutorials available for it were great, but the most useful feature it has was it's debugging tools, specifically the <b>tracing</b> capabilities ( activated by setting $RD_TRACE to 1 ). I am looking for a parser generator that can help...
5
2009-06-04T19:34:32Z
2,475,183
<p>ANTLR above has the advantage to generate human readable and understandable code, since it is (a very sophisticated and powerful) top-down parser, so you can step through it with a regular debugger and see what it <em>really</em> is doing.</p> <p>That's why it is my parser generator of choice.</p> <p>Bottom up par...
1
2010-03-19T05:37:38Z
[ "python", "ruby", "debugging", "parser-generator" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
952,943
<pre><code>&gt;&gt;&gt; l = [[1,2,3],[4,5,6], [7], [8,9]] &gt;&gt;&gt; reduce(lambda x,y: x+y,l) [1, 2, 3, 4, 5, 6, 7, 8, 9] </code></pre> <p>The <code>extend()</code> method in your example modifies <code>x</code> instead of returning a useful value (which <code>reduce()</code> expects).</p> <p>A faster way to do th...
46
2009-06-04T20:35:30Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
952,946
<pre><code>&gt;&gt;&gt; sum(l, []) [1, 2, 3, 4, 5, 6, 7, 8, 9] </code></pre> <p>Note that only works on lists of lists. For lists of lists of lists, you'll need another solution.</p>
375
2009-06-04T20:35:53Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
952,952
<pre><code>[item for sublist in l for item in sublist] </code></pre> <p>is faster than the shortcuts posted so far. (<code>l</code> is the list to flatten.)</p> <p>For evidence, as always, you can use the <code>timeit</code> module in the standard library:</p> <pre><code>$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7],...
1,587
2009-06-04T20:37:01Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
952,954
<p>Why do you use extend?</p> <pre><code>reduce(lambda x, y: x+y, l) </code></pre> <p>This should work fine.</p>
18
2009-06-04T20:38:08Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
952,993
<p><strong>I take my statement back. sum is not the winner. Although it is faster when the list is small. But the performance degrades significantly with larger lists.</strong> </p> <pre><code>&gt;&gt;&gt; timeit.Timer( '[item for sublist in l for item in sublist]', 'l=[[1, 2, 3], [4, 5, 6, 7, 8], [1, ...
21
2009-06-04T20:46:49Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
952,997
<p>The reason your function didn't work: the extend extends array in-place and doesn't return it. You can still return x from lambda, using some trick:</p> <pre><code>reduce(lambda x,y: x.extend(y) or x, l) </code></pre> <p>Note: extend is more efficient than + on lists.</p>
9
2009-06-04T20:47:13Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
953,050
<p>@Nadia: You have to use much longer lists. Then you see the difference quite strikingly! My results for <code>len(l) = 1600</code></p> <pre><code>A took 14.323 ms B took 13.437 ms C took 1.135 ms </code></pre> <p>where:</p> <pre><code>A = reduce(lambda x,y: x+y,l) B = sum(l, []) C = [item for sublist in l for ite...
57
2009-06-04T20:57:24Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
953,097
<p>You can use <a href="http://docs.python.org/2/library/itertools.html#itertools.chain"><code>itertools.chain()</code></a>:</p> <pre><code>&gt;&gt;&gt; import itertools &gt;&gt;&gt; list2d = [[1,2,3],[4,5,6], [7], [8,9]] &gt;&gt;&gt; merged = list(itertools.chain(*list2d)) </code></pre> <p>or, on Python >=2.6, use <...
687
2009-06-04T21:06:17Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
38,421,470
<p>One can also use NumPy's <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flat.html" rel="nofollow">flat</a>:</p> <pre><code>import numpy as np list(np.array(l).flat) </code></pre>
4
2016-07-17T12:57:48Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
39,493,960
<p>There seems to be a confusion with <code>operator.add</code>! When you add two lists together, the correct term for that is <code>concat</code>, not add. <code>operator.concat</code> is what you need to use.</p> <p>If you're thinking functional, it is as easy as this::</p> <pre><code>&gt;&gt;&gt; list2d = ((1,2,3)...
3
2016-09-14T15:09:16Z
[ "python", "list" ]
Making a flat list out of list of lists in Python
952,914
<p>I wonder whether there is a shortcut to make a simple list out of list of lists in Python.</p> <p>I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with <em>reduce</em>, but I get an error.</p> <p><strong>Code</strong></p> <pre><code>l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(l...
1,039
2009-06-04T20:30:05Z
39,987,478
<p>What about this one-liner:</p> <pre class="lang-python prettyprint-override"><code>[k for k in str(a) if k.isdigit()] </code></pre> <p>It will work no matter how many levels of depth (Although only for one digit numbers)</p>
0
2016-10-11T22:01:23Z
[ "python", "list" ]
Explicit access to Python's built in scope
953,027
<p>How do you explicitly access name in Python's built in scope? </p> <p>One situation where I ran in to this was a in module, say called foo, which happened to have an open function. In another module foo's open function would be accessible as foo.open which works well. In foo itself though, open blocks the built ...
10
2009-06-04T20:54:02Z
953,044
<p>It's something like</p> <pre><code>__builtins__.open() </code></pre>
-2
2009-06-04T20:56:22Z
[ "python" ]
Explicit access to Python's built in scope
953,027
<p>How do you explicitly access name in Python's built in scope? </p> <p>One situation where I ran in to this was a in module, say called foo, which happened to have an open function. In another module foo's open function would be accessible as foo.open which works well. In foo itself though, open blocks the built ...
10
2009-06-04T20:54:02Z
953,063
<p>Use <code>__builtin__</code>.</p> <pre><code>def open(): pass import __builtin__ print open print __builtin__.open </code></pre> <p>... gives you ...</p> <blockquote> <p><code>&lt;function open at 0x011E8670&gt;</code><br /> <code>&lt;built-in function open&gt;</code> </p> </blockquote>
11
2009-06-04T21:00:00Z
[ "python" ]
Why won't python allow me to delete files?
953,040
<p>I've created a python script that gets a list of files from a text file and deletes them if they're empty. It correctly detects empty files but it doesn't want to delete them. It gives me:</p> <pre><code>(32, 'The process cannot access the file because it is being used by another process') </code></pre> <p>I've ...
3
2009-06-04T20:55:38Z
953,052
<p>You need to call <code>.close()</code> on the file object before you try and delete it.</p> <p>Edit: And really you shouldn't be opening the file at all. <code>os.stat()</code> will tell you the size of a file (and 9 other values) without ever opening the file.</p> <p>This (I think) does the same thing but is a li...
14
2009-06-04T20:57:40Z
[ "python", "file-io" ]
Why won't python allow me to delete files?
953,040
<p>I've created a python script that gets a list of files from a text file and deletes them if they're empty. It correctly detects empty files but it doesn't want to delete them. It gives me:</p> <pre><code>(32, 'The process cannot access the file because it is being used by another process') </code></pre> <p>I've ...
3
2009-06-04T20:55:38Z
953,054
<p>Are you opening each file and then trying to delete it? If so, try closing it first.</p>
4
2009-06-04T20:58:09Z
[ "python", "file-io" ]
Why won't python allow me to delete files?
953,040
<p>I've created a python script that gets a list of files from a text file and deletes them if they're empty. It correctly detects empty files but it doesn't want to delete them. It gives me:</p> <pre><code>(32, 'The process cannot access the file because it is being used by another process') </code></pre> <p>I've ...
3
2009-06-04T20:55:38Z
953,055
<p>You probably need to close the filehandle <code>ActiveFile</code> before you try to delete it.</p>
3
2009-06-04T20:58:18Z
[ "python", "file-io" ]
Why won't python allow me to delete files?
953,040
<p>I've created a python script that gets a list of files from a text file and deletes them if they're empty. It correctly detects empty files but it doesn't want to delete them. It gives me:</p> <pre><code>(32, 'The process cannot access the file because it is being used by another process') </code></pre> <p>I've ...
3
2009-06-04T20:55:38Z
953,056
<p>It's <strong>you</strong> that has the file open - you need to close it before trying to delete it:</p> <pre><code>ActiveFile = open(line); Length = len(ActiveFile.read()) ActiveFile.close() # Insert this line! </code></pre> <p>or just get the filesize without opening the file:</p> <pre><code>Length = os.path.g...
6
2009-06-04T20:58:25Z
[ "python", "file-io" ]
Why won't python allow me to delete files?
953,040
<p>I've created a python script that gets a list of files from a text file and deletes them if they're empty. It correctly detects empty files but it doesn't want to delete them. It gives me:</p> <pre><code>(32, 'The process cannot access the file because it is being used by another process') </code></pre> <p>I've ...
3
2009-06-04T20:55:38Z
953,062
<p>Try ActiveFile.close() before doing the unlink.</p> <p>Also, reading the whole file isn't necessary, you can use os.path.getsize(filename) == 0.</p>
9
2009-06-04T20:59:35Z
[ "python", "file-io" ]
SSH Connection with Python 3.0
953,477
<p>How can I make an SSH connection in Python 3.0? I want to save a file on a remote computer where I have password-less SSH set up.</p>
6
2009-06-04T22:39:35Z
953,509
<p>It might take a little work because "<a href="http://twistedmatrix.com/projects/conch/documentation/howto/conch%5Fclient.html" rel="nofollow">twisted:conch</a>" does not appear to have a 3.0 variant.</p>
0
2009-06-04T22:46:57Z
[ "python", "file", "ssh" ]
SSH Connection with Python 3.0
953,477
<p>How can I make an SSH connection in Python 3.0? I want to save a file on a remote computer where I have password-less SSH set up.</p>
6
2009-06-04T22:39:35Z
953,510
<p>I recommend calling ssh as a subprocess. It's reliable and portable.</p> <pre><code>import subprocess proc = subprocess.Popen(['ssh', 'user@host', 'cat &gt; %s' % filename], stdin=subprocess.PIPE) proc.communicate(file_contents) if proc.retcode != 0: ... </code></pre> <p>You'd have to ...
11
2009-06-04T22:47:36Z
[ "python", "file", "ssh" ]
SSH Connection with Python 3.0
953,477
<p>How can I make an SSH connection in Python 3.0? I want to save a file on a remote computer where I have password-less SSH set up.</p>
6
2009-06-04T22:39:35Z
953,533
<p>You want all of the ssh-functionality implemented as a python library? Have a look at paramiko, although I think it's not ported to Python 3.0 (yet?).</p> <p>If you can use an existing ssh installation you can use the <code>subprocess</code> way Dietrich described, or (another way) you could also use <code>pexcept<...
1
2009-06-04T22:55:15Z
[ "python", "file", "ssh" ]
SSH Connection with Python 3.0
953,477
<p>How can I make an SSH connection in Python 3.0? I want to save a file on a remote computer where I have password-less SSH set up.</p>
6
2009-06-04T22:39:35Z
5,141,491
<p>I have written <a href="http://www.no-ack.org/2010/11/python-bindings-for-libssh2.html" rel="nofollow">Python bindings for libssh2</a>, that run on Python 2.4, 2.5, 2.6, 2.7 and <strong>3</strong>.</p>
0
2011-02-28T11:42:11Z
[ "python", "file", "ssh" ]
SSH Connection with Python 3.0
953,477
<p>How can I make an SSH connection in Python 3.0? I want to save a file on a remote computer where I have password-less SSH set up.</p>
6
2009-06-04T22:39:35Z
16,390,822
<p><a href="https://github.com/wallunit/ssh4py" rel="nofollow">libssh2</a> works great for Python 3.x.<br> See this Stack Overflow article <a href="http://stackoverflow.com/questions/14367020/how-to-send-a-file-using-scp-using-python-3-2">How to send a file using scp using python 3.2?</a></p>
0
2013-05-06T01:21:44Z
[ "python", "file", "ssh" ]
SSH Connection with Python 3.0
953,477
<p>How can I make an SSH connection in Python 3.0? I want to save a file on a remote computer where I have password-less SSH set up.</p>
6
2009-06-04T22:39:35Z
18,154,637
<p>First:</p> <p>Two steps to login via ssh without password</p> <p>in your terminal</p> <pre><code>[macm@macm ~]$ ssh-keygen [macm@macm ~]$ ssh-copy-id -i $HOME/.ssh/id_rsa.pub root@192.168.1.XX &lt;== change </code></pre> <p>Now with python</p> <pre><code>from subprocess import PIPE, Popen cmd = 'uname -a' st...
1
2013-08-09T19:55:19Z
[ "python", "file", "ssh" ]
Check unread count of Gmail messages with Python
953,561
<p>How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.</p>
31
2009-06-04T23:05:30Z
953,581
<p>Well it isn't a code snippet but I imagine using <a href="http://docs.python.org/library/imaplib.html">imaplib</a> and the <a href="http://mail.google.com/support/bin/answer.py?hl=en&amp;answer=75725">Gmail IMAP instructions</a> get you most of the way there.</p>
6
2009-06-04T23:12:08Z
[ "python", "email", "gmail", "pop3" ]
Check unread count of Gmail messages with Python
953,561
<p>How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.</p>
31
2009-06-04T23:05:30Z
953,583
<p>Use <a href="http://www.holovaty.com/writing/278/" rel="nofollow">Gmail.py</a></p> <pre><code>file = open("filename","r") usr = file.readline() pwd = file.readline() gmail = GmailClient() gmail.login(usr, pwd) unreadMail = gmail.get_inbox_conversations(is_unread=True) print unreadMail </code></pre> <p>Gets login i...
-1
2009-06-04T23:12:21Z
[ "python", "email", "gmail", "pop3" ]
Check unread count of Gmail messages with Python
953,561
<p>How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.</p>
31
2009-06-04T23:05:30Z
953,607
<p>I advise you to use <a href="https://developers.google.com/gmail/gmail_inbox_feed" rel="nofollow">Gmail atom feed</a></p> <p>It is as simple as this:</p> <pre><code>import urllib url = 'https://mail.google.com/mail/feed/atom/' opener = urllib.FancyURLopener() f = opener.open(url) feed = f.read() </code></pre> <p...
22
2009-06-04T23:22:58Z
[ "python", "email", "gmail", "pop3" ]
Check unread count of Gmail messages with Python
953,561
<p>How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.</p>
31
2009-06-04T23:05:30Z
953,617
<p>Once you are logged in (do this manually or with gmail.py) you should use the feed.</p> <p>It is located here: <a href="http://mail.google.com/mail/feed/atom" rel="nofollow">http://mail.google.com/mail/feed/atom</a></p> <p>It is the way Google does it. Here is a link to their js chrome extension: <a href="http://d...
1
2009-06-04T23:30:34Z
[ "python", "email", "gmail", "pop3" ]
Check unread count of Gmail messages with Python
953,561
<p>How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.</p>
31
2009-06-04T23:05:30Z
954,099
<p>Well, I'm going to go ahead and spell out an imaplib solution as Cletus suggested. I don't see why people feel the need to use gmail.py or Atom for this. This kind of thing is what IMAP was designed for. Gmail.py is particularly egregious as it actually parses Gmail's HTML. That may be necessary for some things,...
22
2009-06-05T03:05:12Z
[ "python", "email", "gmail", "pop3" ]
Check unread count of Gmail messages with Python
953,561
<p>How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.</p>
31
2009-06-04T23:05:30Z
3,984,850
<pre><code>import imaplib obj = imaplib.IMAP4_SSL('imap.gmail.com','993') obj.login('username','password') obj.select() obj.search(None,'UnSeen') </code></pre>
49
2010-10-21T06:30:45Z
[ "python", "email", "gmail", "pop3" ]
Check unread count of Gmail messages with Python
953,561
<p>How can I check the number of unread Gmail message in my inbox with a short Python script? Bonus points for retrieving the password from a file.</p>
31
2009-06-04T23:05:30Z
7,761,930
<p>For a complete implementation of reading the value from the atom feed:</p> <pre><code>import urllib2 import base64 from xml.dom.minidom import parse def gmail_unread_count(user, password): """ Takes a Gmail user name and password and returns the unread messages count as an integer. """ ...
5
2011-10-14T00:28:22Z
[ "python", "email", "gmail", "pop3" ]
Which gps library would you recommend for python?
953,701
<p>I'm looking for a free library for python that can calculate your direction and your speed from GPS coordinates and maybe can calculate if you are in some boundaries or things like this. </p> <p>Are there Libraries that you know and that worked well for you?</p> <p><strong>Edit:</strong> Ok it seems I have to spec...
8
2009-06-05T00:01:57Z
954,101
<p>I'm not sure I understand your exact requirements, but, depending on your device &amp;c, there seem to be many possible candidates, such as:</p> <ul> <li><a href="http://gagravarr.org/code/" rel="nofollow">S60 GPS Info Viewer</a></li> <li><a href="https://github.com/quentinsf/pygarmin" rel="nofollow">pygarmin</a></...
4
2009-06-05T03:06:55Z
[ "python", "gps", "gpsd" ]
Which gps library would you recommend for python?
953,701
<p>I'm looking for a free library for python that can calculate your direction and your speed from GPS coordinates and maybe can calculate if you are in some boundaries or things like this. </p> <p>Are there Libraries that you know and that worked well for you?</p> <p><strong>Edit:</strong> Ok it seems I have to spec...
8
2009-06-05T00:01:57Z
954,729
<p>The book "Beginning Python Visualization" includes just such an example - parsing GPS data and inferring speed and location from it. Its source code is available online at <a href="http://www.apress.com/" rel="nofollow">http://www.apress.com/</a></p>
3
2009-06-05T07:53:42Z
[ "python", "gps", "gpsd" ]
Which gps library would you recommend for python?
953,701
<p>I'm looking for a free library for python that can calculate your direction and your speed from GPS coordinates and maybe can calculate if you are in some boundaries or things like this. </p> <p>Are there Libraries that you know and that worked well for you?</p> <p><strong>Edit:</strong> Ok it seems I have to spec...
8
2009-06-05T00:01:57Z
954,883
<p>The GPS sentences that you receive from a GPS device are pretty easy to decode, and this looks like a fun project. I'm not sure what you get from gspd, but if it's these sentences, I actually had to do something like this for school a few weeks ago (but in LabView):</p> <pre><code>$GPGGA,hhmmss.ss,llll.ll,a,yyyyy.y...
2
2009-06-05T08:48:47Z
[ "python", "gps", "gpsd" ]
Which gps library would you recommend for python?
953,701
<p>I'm looking for a free library for python that can calculate your direction and your speed from GPS coordinates and maybe can calculate if you are in some boundaries or things like this. </p> <p>Are there Libraries that you know and that worked well for you?</p> <p><strong>Edit:</strong> Ok it seems I have to spec...
8
2009-06-05T00:01:57Z
973,022
<p>Apparently the python module that comes with gpsd is the best module to go with for us. For a start look <a href="http://www.perrygeo.net/wordpress/?p=13">here</a></p> <p>The gps module coming with the gpsd has some very useful functions. The first one is getting the data from gpsd and transforming those data in a...
8
2009-06-09T23:31:28Z
[ "python", "gps", "gpsd" ]
Which gps library would you recommend for python?
953,701
<p>I'm looking for a free library for python that can calculate your direction and your speed from GPS coordinates and maybe can calculate if you are in some boundaries or things like this. </p> <p>Are there Libraries that you know and that worked well for you?</p> <p><strong>Edit:</strong> Ok it seems I have to spec...
8
2009-06-05T00:01:57Z
33,309,901
<p>Most good GPS units (we use Oncore M12M) will actually give you, as output, your velocity and heading. I would first check your GPS receiver's documentation to see if this information is already being sent, or if such a message can be enabled. In my experience, this data is usually part of the standard telemetry a r...
1
2015-10-23T19:18:53Z
[ "python", "gps", "gpsd" ]
how to program functions with alternative return value signatures in python? (next() for alternative iterators)
953,914
<p>e.g. so that these would both work - is it possible?</p> <pre><code>(val,VAL2) = func(args) val = func(args) </code></pre> <p>Where <strong>val</strong> is <strong>not a tuple</strong></p> <p>For example I'd like these to work for my custom object <strong>something</strong></p> <pre><code>for item in something:...
0
2009-06-05T01:42:41Z
953,923
<p>Have you tried that? It works.</p> <pre><code>def myfunction(data): datalen = len(data) result1 = data[:datalen/2] result2 = data[datalen/2:] return result1, result2 a, b = myfunction('stuff') print a print b c = myfunction('other stuff') print c </code></pre> <p>In fact there is no such thing a...
4
2009-06-05T01:46:45Z
[ "python" ]
how to program functions with alternative return value signatures in python? (next() for alternative iterators)
953,914
<p>e.g. so that these would both work - is it possible?</p> <pre><code>(val,VAL2) = func(args) val = func(args) </code></pre> <p>Where <strong>val</strong> is <strong>not a tuple</strong></p> <p>For example I'd like these to work for my custom object <strong>something</strong></p> <pre><code>for item in something:...
0
2009-06-05T01:42:41Z
953,926
<p>It's possible only if you're happy for <code>val</code> to be a 2-item tuple (or if <code>args</code> need not be the same in the two cases). The former is what would happen if the function just ended with something like <code>return 23, 45</code>. Here's an example of the latter idea:</p> <pre><code>def weirdfunc(...
1
2009-06-05T01:47:17Z
[ "python" ]
how to program functions with alternative return value signatures in python? (next() for alternative iterators)
953,914
<p>e.g. so that these would both work - is it possible?</p> <pre><code>(val,VAL2) = func(args) val = func(args) </code></pre> <p>Where <strong>val</strong> is <strong>not a tuple</strong></p> <p>For example I'd like these to work for my custom object <strong>something</strong></p> <pre><code>for item in something:...
0
2009-06-05T01:42:41Z
953,933
<p>Update:</p> <p>Given the example use case, I'd write different generators to handle the cases:</p> <pre><code>class Something(object): def __init__(self): self.d = {'a' : 1, 'b' : 2, 'c' : 3} def items(self): for i in self.d.values(): yield i ...
2
2009-06-05T01:48:38Z
[ "python" ]
how to program functions with alternative return value signatures in python? (next() for alternative iterators)
953,914
<p>e.g. so that these would both work - is it possible?</p> <pre><code>(val,VAL2) = func(args) val = func(args) </code></pre> <p>Where <strong>val</strong> is <strong>not a tuple</strong></p> <p>For example I'd like these to work for my custom object <strong>something</strong></p> <pre><code>for item in something:...
0
2009-06-05T01:42:41Z
953,935
<p>Yes, both would work. In the first example, val1 and val2 would have the two values. In the second example, val would have a tuple. You can try this in your python interpreter:</p> <pre><code>&gt;&gt;&gt; def foo(): ... return ( 1, 2 ) ... &gt;&gt;&gt; x = foo() &gt;&gt;&gt; (y,z) = foo() &gt;&gt;&gt; x (1, 2) &g...
1
2009-06-05T01:49:17Z
[ "python" ]
how to program functions with alternative return value signatures in python? (next() for alternative iterators)
953,914
<p>e.g. so that these would both work - is it possible?</p> <pre><code>(val,VAL2) = func(args) val = func(args) </code></pre> <p>Where <strong>val</strong> is <strong>not a tuple</strong></p> <p>For example I'd like these to work for my custom object <strong>something</strong></p> <pre><code>for item in something:...
0
2009-06-05T01:42:41Z
953,942
<p>Yes it's doable:</p> <pre><code>def a(b): if b &lt; 5: return ("o", "k") else: return "ko" </code></pre> <p>and the result:</p> <pre><code>&gt;&gt;&gt; b = a(4) &gt;&gt;&gt; b ('o', 'k') &gt;&gt;&gt; b = a(6) &gt;&gt;&gt; b 'ko' </code></pre> <p>I think the thing after is to be careful when you will use the va...
2
2009-06-05T01:50:38Z
[ "python" ]
how to program functions with alternative return value signatures in python? (next() for alternative iterators)
953,914
<p>e.g. so that these would both work - is it possible?</p> <pre><code>(val,VAL2) = func(args) val = func(args) </code></pre> <p>Where <strong>val</strong> is <strong>not a tuple</strong></p> <p>For example I'd like these to work for my custom object <strong>something</strong></p> <pre><code>for item in something:...
0
2009-06-05T01:42:41Z
953,949
<pre><code>&gt;&gt;&gt; def func(a,b): return (a,b) &gt;&gt;&gt; x = func(1,2) &gt;&gt;&gt; x (1, 2) &gt;&gt;&gt; (y,z) = func(1,2) &gt;&gt;&gt; y 1 &gt;&gt;&gt; z 2 </code></pre> <p>That doesn't really answer your question. The real answer is that the left side of the assignment doesn't affect the returned ty...
2
2009-06-05T01:54:10Z
[ "python" ]
how to program functions with alternative return value signatures in python? (next() for alternative iterators)
953,914
<p>e.g. so that these would both work - is it possible?</p> <pre><code>(val,VAL2) = func(args) val = func(args) </code></pre> <p>Where <strong>val</strong> is <strong>not a tuple</strong></p> <p>For example I'd like these to work for my custom object <strong>something</strong></p> <pre><code>for item in something:...
0
2009-06-05T01:42:41Z
954,202
<p>This is asking for major confusion. Instead you can follow <code>dict</code> with separate keys, values, items, etc. methods, or you can use a convention of naming unused variables with a single underscore. Examples:</p> <pre><code>for k in mydict.keys(): pass for k, v in mydict.items(): pass for a, b in myobj.foo...
0
2009-06-05T03:48:57Z
[ "python" ]
how to program functions with alternative return value signatures in python? (next() for alternative iterators)
953,914
<p>e.g. so that these would both work - is it possible?</p> <pre><code>(val,VAL2) = func(args) val = func(args) </code></pre> <p>Where <strong>val</strong> is <strong>not a tuple</strong></p> <p>For example I'd like these to work for my custom object <strong>something</strong></p> <pre><code>for item in something:...
0
2009-06-05T01:42:41Z
954,859
<p>If you mean, can the function act differently based on the return types the caller is expecting, the answer is no (bar seriously nasty bytecode inspection). In this case, you should provide two different iterators on your object, and write something like:</p> <pre><code>for item in something: # Default iterator: ...
6
2009-06-05T08:40:32Z
[ "python" ]
Error running tutorial that came along wxPython2.8 Docs and Demos
954,132
<p>I tried the following example code from the tutorial that came along "wxPython2.8 Docs and Demos" package.</p> <pre><code>import wx from frame import Frame class App(wx.App): """Application class.""" def OnInit(self): self.frame = Frame() self.frame.Show() self.SetTopWindow(self.f...
1
2009-06-05T03:22:44Z
954,147
<p>I think you should skip the "from frame import Frame" and change:</p> <pre><code>self.frame = Frame() </code></pre> <p>to:</p> <pre><code>self.frame = wx.Frame() </code></pre>
1
2009-06-05T03:28:03Z
[ "python", "windows", "wxpython" ]
Error running tutorial that came along wxPython2.8 Docs and Demos
954,132
<p>I tried the following example code from the tutorial that came along "wxPython2.8 Docs and Demos" package.</p> <pre><code>import wx from frame import Frame class App(wx.App): """Application class.""" def OnInit(self): self.frame = Frame() self.frame.Show() self.SetTopWindow(self.f...
1
2009-06-05T03:22:44Z
954,163
<p>Yeah, it's an ancient doc bug, see for example <a href="http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-02/4023.html" rel="nofollow">this 5-years-old post</a>:-(. Fix:</p> <ul> <li>delete the line that says <code>from frame import Frame</code></li> <li>change the line that says <code>self.frame = F...
0
2009-06-05T03:33:54Z
[ "python", "windows", "wxpython" ]
PyS60 application not going full screen
954,272
<p>I am very new to PyS60. I was testing how to set an application to full screen mode but unfortunately, it doesn't work as expected. I tested the script on Nokia 6120 Classic. Here is what I did:</p> <p><code> appuifw.app.screen = 'full' </code></p> <p>What I get is a half screen of my application with a plain whit...
3
2009-06-05T04:31:11Z
955,340
<p>If you haven't already, I would advise using the latest version of PyS60 from https://garage.maemo.org/frs/?group_id=854 and trying again.</p> <p>Do the other two screen modes work as they are supposed to?</p>
0
2009-06-05T11:05:29Z
[ "python", "symbian", "pys60" ]
PyS60 application not going full screen
954,272
<p>I am very new to PyS60. I was testing how to set an application to full screen mode but unfortunately, it doesn't work as expected. I tested the script on Nokia 6120 Classic. Here is what I did:</p> <p><code> appuifw.app.screen = 'full' </code></p> <p>What I get is a half screen of my application with a plain whit...
3
2009-06-05T04:31:11Z
955,644
<p>Make sure you define own functions for <strong>screen redraw</strong> and <strong>screen rotate</strong> callbacks. When you rotate the device, you have to manually rescale everything to fit the new screen size. Otherwise you might get that "half of screen" effect.</p> <pre><code> canvas = img = None def c...
4
2009-06-05T12:47:22Z
[ "python", "symbian", "pys60" ]
Getting object's parent namespace in python?
954,340
<p>In python it's possible to use '.' in order to access object's dictionary items. For example:</p> <pre><code>class test( object ) : def __init__( self ) : self.b = 1 def foo( self ) : pass obj = test() a = obj.foo </code></pre> <p>From above example, having 'a' object, is it possible to get from it ref...
9
2009-06-05T05:03:01Z
954,347
<h2>Python 2.6+ (including Python 3)</h2> <p>You can use the <a href="https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy" rel="nofollow"><code>__self__</code> property of a bound method</a> to access the instance that the method is bound to.</p> <pre><code>&gt;&gt; a.__self__ &lt;__main__....
14
2009-06-05T05:06:02Z
[ "python", "python-datamodel" ]
Getting object's parent namespace in python?
954,340
<p>In python it's possible to use '.' in order to access object's dictionary items. For example:</p> <pre><code>class test( object ) : def __init__( self ) : self.b = 1 def foo( self ) : pass obj = test() a = obj.foo </code></pre> <p>From above example, having 'a' object, is it possible to get from it ref...
9
2009-06-05T05:03:01Z
954,514
<p>On bound methods, you can use three special read-only parameters:</p> <ul> <li><strong>im_func</strong> which returns the (unbound) function object</li> <li><strong>im_self</strong> which returns the object the function is bound to (class instance)</li> <li><strong>im_class</strong> which returns the class of <em>i...
17
2009-06-05T06:31:40Z
[ "python", "python-datamodel" ]
Getting object's parent namespace in python?
954,340
<p>In python it's possible to use '.' in order to access object's dictionary items. For example:</p> <pre><code>class test( object ) : def __init__( self ) : self.b = 1 def foo( self ) : pass obj = test() a = obj.foo </code></pre> <p>From above example, having 'a' object, is it possible to get from it ref...
9
2009-06-05T05:03:01Z
954,619
<p>since python2.6 synonyms for <code>im_self</code> and <code>im_func</code> are <code>__self__</code> and <code>__func__</code>, respectively. <code>im*</code> attributes are completely gone in py3k. so you would need to change it to:</p> <pre><code>&gt;&gt; a.__self__ &lt;__main__.test object at 0xb7b7d9ac&gt; &gt;...
7
2009-06-05T07:16:59Z
[ "python", "python-datamodel" ]
Help with Django app and payment systems (general queries)
954,478
<p>So I'm working on an app in Django, however this is my first time venturing into advance integration for a webapp with payment systems (I used to work with paypal/2checkout so it was pretty no-skill-required).</p> <p>My partners have chosen PaymentExpress, and there are several sets of API (all of which are pretty ...
1
2009-06-05T06:17:35Z
954,774
<p>PXPost is the most straight-forward solution. You just communicate via HTTP POSTs and XML. You don't need any external dependencies, just <a href="http://docs.python.org/library/urllib2.html" rel="nofollow">urllib2</a> and <a href="http://docs.python.org/library/xml.etree.elementtree.html?highlight=etree#module-xml....
0
2009-06-05T08:07:56Z
[ "python", "django", "payment" ]
How to get files in a directory, including all subdirectories
954,504
<p>I'm trying to get a list of all log files (.log) in directory, including all subdirectories.</p>
15
2009-06-05T06:27:08Z
954,517
<p>Checkout <a href="http://ssscripting.wordpress.com/2009/03/03/python-recursive-directory-walker/">Python Recursive Directory Walker</a>. In short os.listdir() and os.walk() are your friends.</p>
7
2009-06-05T06:33:17Z
[ "python" ]
How to get files in a directory, including all subdirectories
954,504
<p>I'm trying to get a list of all log files (.log) in directory, including all subdirectories.</p>
15
2009-06-05T06:27:08Z
954,519
<p>If You want to list in current directory, You can use something like:</p> <pre><code>import os for e in os.walk(os.getcwd()): print e </code></pre> <p>Just change the</p> <pre><code>os.getcwd() </code></pre> <p>to other path to get results there.</p>
1
2009-06-05T06:34:56Z
[ "python" ]
How to get files in a directory, including all subdirectories
954,504
<p>I'm trying to get a list of all log files (.log) in directory, including all subdirectories.</p>
15
2009-06-05T06:27:08Z
954,522
<pre><code>import os import os.path for dirpath, dirnames, filenames in os.walk("."): for filename in [f for f in filenames if f.endswith(".log")]: print os.path.join(dirpath, filename) </code></pre>
28
2009-06-05T06:35:19Z
[ "python" ]
How to get files in a directory, including all subdirectories
954,504
<p>I'm trying to get a list of all log files (.log) in directory, including all subdirectories.</p>
15
2009-06-05T06:27:08Z
954,948
<p>You can also use the glob module along with os.walk.</p> <pre><code>import os from glob import glob files = [] start_dir = os.getcwd() pattern = "*.log" for dir,_,_ in os.walk(start_dir): files.extend(glob(os.path.join(dir,pattern))) </code></pre>
5
2009-06-05T09:08:38Z
[ "python" ]
By System command
954,823
<p>By using system command i want to open '.py' in the notepad. Ex assume i have "Fact.py" file. Now i want to write a program which will open this file in notepad and we can edit this file.</p>
0
2009-06-05T08:28:09Z
954,828
<pre><code>import os os.system("notepad.exe fact.py") </code></pre> <p>should do it, assuming the Notepad program is in your system's path.</p>
1
2009-06-05T08:30:10Z
[ "python" ]
By System command
954,823
<p>By using system command i want to open '.py' in the notepad. Ex assume i have "Fact.py" file. Now i want to write a program which will open this file in notepad and we can edit this file.</p>
0
2009-06-05T08:28:09Z
954,835
<p>It's best to use subprocess for this, since this will avoid having to deal with quoting files containing spaces etc for the shell.</p> <pre><code>import subprocess subprocess.call(['notepad','Fact.py']) </code></pre>
8
2009-06-05T08:33:14Z
[ "python" ]
How do I use raw_input in Python 3
954,834
<pre><code>import sys print (sys.platform) print (2 ** 100) raw_input( ) </code></pre> <p>I am using Python 3.1 and can't get the raw_input to "freeze" the dos pop-up. The book I'm reading is for 2.5 and I'm using 3.1</p> <p>What should I do to fix this?</p>
220
2009-06-05T08:32:23Z
954,840
<p><code>raw_input()</code> was renamed to <code>input()</code></p> <p>From <a href="http://docs.python.org/dev/py3k/whatsnew/3.0.html">http://docs.python.org/dev/py3k/whatsnew/3.0.html</a></p>
405
2009-06-05T08:35:38Z
[ "python", "python-3.x" ]
How do I use raw_input in Python 3
954,834
<pre><code>import sys print (sys.platform) print (2 ** 100) raw_input( ) </code></pre> <p>I am using Python 3.1 and can't get the raw_input to "freeze" the dos pop-up. The book I'm reading is for 2.5 and I'm using 3.1</p> <p>What should I do to fix this?</p>
220
2009-06-05T08:32:23Z
7,321,970
<p>This works in Python 3.x and 2.x:</p> <pre><code># Fix Python 2.x. try: input = raw_input except NameError: pass print("Hi " + input("Say something: ")) </code></pre>
66
2011-09-06T14:59:18Z
[ "python", "python-3.x" ]
How do I use raw_input in Python 3
954,834
<pre><code>import sys print (sys.platform) print (2 ** 100) raw_input( ) </code></pre> <p>I am using Python 3.1 and can't get the raw_input to "freeze" the dos pop-up. The book I'm reading is for 2.5 and I'm using 3.1</p> <p>What should I do to fix this?</p>
220
2009-06-05T08:32:23Z
9,411,224
<p>In Python 3.xx you just need <code>input()</code> not <code>raw_input()</code></p>
5
2012-02-23T10:31:18Z
[ "python", "python-3.x" ]
How do I use raw_input in Python 3
954,834
<pre><code>import sys print (sys.platform) print (2 ** 100) raw_input( ) </code></pre> <p>I am using Python 3.1 and can't get the raw_input to "freeze" the dos pop-up. The book I'm reading is for 2.5 and I'm using 3.1</p> <p>What should I do to fix this?</p>
220
2009-06-05T08:32:23Z
9,411,780
<p>As others have indicated, the <code>raw_input</code> function has been renamed to <code>input</code> in Python 3.0, and you really would be better served by a more up-to-date book, but I want to point out that there are better ways to see the output of your script.</p> <p>From your description, I think you're using...
11
2012-02-23T11:05:27Z
[ "python", "python-3.x" ]
How do I use raw_input in Python 3
954,834
<pre><code>import sys print (sys.platform) print (2 ** 100) raw_input( ) </code></pre> <p>I am using Python 3.1 and can't get the raw_input to "freeze" the dos pop-up. The book I'm reading is for 2.5 and I'm using 3.1</p> <p>What should I do to fix this?</p>
220
2009-06-05T08:32:23Z
18,332,097
<p>Timmerman's solution works great when running the code, but if you don't want to get <code>Undefined name</code> errors when using pyflakes or a similar linter you could use the following instead:</p> <pre><code>try: import __builtin__ input = getattr(__builtin__, 'raw_input') except (ImportError, Attribute...
4
2013-08-20T09:54:29Z
[ "python", "python-3.x" ]
How do I use raw_input in Python 3
954,834
<pre><code>import sys print (sys.platform) print (2 ** 100) raw_input( ) </code></pre> <p>I am using Python 3.1 and can't get the raw_input to "freeze" the dos pop-up. The book I'm reading is for 2.5 and I'm using 3.1</p> <p>What should I do to fix this?</p>
220
2009-06-05T08:32:23Z
25,972,524
<p>Here's a piece of code I put in my scripts that I wan't to run in py2/3-agnostic environment:</p> <pre><code># Thank you, python2-3 team, for making such a fantastic mess with # input/raw_input :-) real_raw_input = vars(__builtins__).get('raw_input',input) </code></pre> <p>Now you can use real_raw_input. It's quit...
3
2014-09-22T11:02:21Z
[ "python", "python-3.x" ]
How do I use raw_input in Python 3
954,834
<pre><code>import sys print (sys.platform) print (2 ** 100) raw_input( ) </code></pre> <p>I am using Python 3.1 and can't get the raw_input to "freeze" the dos pop-up. The book I'm reading is for 2.5 and I'm using 3.1</p> <p>What should I do to fix this?</p>
220
2009-06-05T08:32:23Z
31,687,067
<p>A reliable way to address this is</p> <pre><code>from six.moves import input </code></pre> <p><a href="http://pythonhosted.org/six/">six</a> is a module which patches over many of the 2/3 common code base pain points.</p>
8
2015-07-28T21:05:42Z
[ "python", "python-3.x" ]
How can I tell if a certain key was pressed in Python?
954,933
<pre><code>import sys print (sys.platform) print (2 ** 100) input('press Enter to exit') </code></pre> <p>Suppose I wanted to use the number 1 as the button that must be pressed to exit. How would I go about doing this?</p>
2
2009-06-05T09:04:14Z
954,951
<p>Something like this will do what you want:</p> <pre><code>while(raw_input('Press "1" to exit.') != '1'): pass </code></pre>
1
2009-06-05T09:10:11Z
[ "python" ]
How can I tell if a certain key was pressed in Python?
954,933
<pre><code>import sys print (sys.platform) print (2 ** 100) input('press Enter to exit') </code></pre> <p>Suppose I wanted to use the number 1 as the button that must be pressed to exit. How would I go about doing this?</p>
2
2009-06-05T09:04:14Z
954,955
<p>Something like this?</p> <p><a href="http://mail.python.org/pipermail/python-list/1999-October/014262.html" rel="nofollow">http://mail.python.org/pipermail/python-list/1999-October/014262.html</a></p> <p>Not so clean, but doable.</p>
2
2009-06-05T09:10:58Z
[ "python" ]
How can I tell if a certain key was pressed in Python?
954,933
<pre><code>import sys print (sys.platform) print (2 ** 100) input('press Enter to exit') </code></pre> <p>Suppose I wanted to use the number 1 as the button that must be pressed to exit. How would I go about doing this?</p>
2
2009-06-05T09:04:14Z
954,996
<p>If you're building a command line app, why not use one of the libraries that help you build one.</p> <p>For example:</p> <ul> <li><a href="http://docs.python.org/library/curses.html" rel="nofollow">curses</a> </li> <li><a href="http://excess.org/urwid/" rel="nofollow">urwid</a>.</li> </ul>
2
2009-06-05T09:23:20Z
[ "python" ]
How do I use a Python library in my Java application?
954,950
<p>What are the basic nuts and bolts of calling (running? interpreting? what can you do?) Python code from a Java program? Are there many ways to do it?</p>
1
2009-06-05T09:10:00Z
954,958
<p>You can <a href="http://www.jython.org/docs/embedding.html" rel="nofollow">embed Jython</a> within your Java application, rather than spawning off a separate process. Provided your library is <a href="http://www.jython.org/docs/differences.html" rel="nofollow">compatible</a> with Jython, that would seem the most log...
4
2009-06-05T09:11:42Z
[ "java", "python", "jython" ]
How do I use a Python library in my Java application?
954,950
<p>What are the basic nuts and bolts of calling (running? interpreting? what can you do?) Python code from a Java program? Are there many ways to do it?</p>
1
2009-06-05T09:10:00Z
954,987
<p>Apart from embedding Jython as mentioned by Brian, you have these options as well.</p> <p>Java 1.6 has inbuilt support for scripting. You can find more info <a href="http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/" rel="nofollow">here</a>.</p> <p>Spring also provides excellent support for s...
2
2009-06-05T09:20:03Z
[ "java", "python", "jython" ]
How do I use a Python library in my Java application?
954,950
<p>What are the basic nuts and bolts of calling (running? interpreting? what can you do?) Python code from a Java program? Are there many ways to do it?</p>
1
2009-06-05T09:10:00Z
955,010
<p>And if none of the other alternatives mentioned (Jython, Spring) work, you can always run an external CPython interpreter and communicate with the JVM through:</p> <ul> <li>CORBA</li> <li>Sockets</li> <li>Pipes</li> <li>Temporary files</li> </ul> <p>Also maybe you would take a look at <a href="http://wiki.services...
0
2009-06-05T09:26:36Z
[ "java", "python", "jython" ]
My regex in python isn't recursing properly
954,989
<p>I'm suppose to capture everything inside a tag and the next lines after it, but it's suppose to stop the next time it meets a bracket. What am i doing wrong?</p> <pre><code>import re #regex regex = re.compile(r""" ^ # Must start in a newline first \[\b(.*)\b\] # Get wha...
2
2009-06-05T09:21:26Z
955,001
<p>Python regex doesn't support recursion afaik.</p> <p>EDIT: but in your case this would work:</p> <pre><code>regex = re.compile(r""" ^ # Must start in a newline first \[(.*?)\] # Get what's enclosed in brackets \n # only capture bracket if a newline is next ...
3
2009-06-05T09:24:39Z
[ "python", "regex", "recursion" ]
My regex in python isn't recursing properly
954,989
<p>I'm suppose to capture everything inside a tag and the next lines after it, but it's suppose to stop the next time it meets a bracket. What am i doing wrong?</p> <pre><code>import re #regex regex = re.compile(r""" ^ # Must start in a newline first \[\b(.*)\b\] # Get wha...
2
2009-06-05T09:21:26Z
955,024
<p>Does this do what you want?</p> <pre><code>regex = re.compile(r""" ^ # Must start in a newline first \[\b(.*)\b\] # Get what's enclosed in brackets \n # only capture bracket if a newline is next ([^[]*) """, re.MULTILINE ...
2
2009-06-05T09:32:38Z
[ "python", "regex", "recursion" ]
My regex in python isn't recursing properly
954,989
<p>I'm suppose to capture everything inside a tag and the next lines after it, but it's suppose to stop the next time it meets a bracket. What am i doing wrong?</p> <pre><code>import re #regex regex = re.compile(r""" ^ # Must start in a newline first \[\b(.*)\b\] # Get wha...
2
2009-06-05T09:21:26Z
959,571
<p>First of all why a regex if you're trying to parse? As you can see you cannot find the source of the problem yourself, because regex gives no feedback. Also you don't have any recursion in that RE.</p> <p>Make your life simple:</p> <pre><code>def ini_parse(src): in_block = None contents = {} for line in s...
3
2009-06-06T12:15:02Z
[ "python", "regex", "recursion" ]
How do I run "dot" as a command from Python?
955,504
<p>I am using Python on Mac OSX Leopard.</p> <p>I am trying to run the program 'dot' (part of Graphviz) from Python:</p> <pre><code># -*- coding: utf-8 -*- import os print os.environ['PATH'] print os.system("ls &gt;&gt; a.txt") print os.system("dot -o9.png -Tpng ./6.dot") </code></pre> <p>The command "ls" is there ...
4
2009-06-05T11:55:09Z
955,523
<p><code>whereis</code> locates man pages, <code>which</code> locates binaries. So try <code>which dot</code>.</p>
10
2009-06-05T12:01:06Z
[ "python", "osx", "path", "graphviz", "dot" ]
How do I run "dot" as a command from Python?
955,504
<p>I am using Python on Mac OSX Leopard.</p> <p>I am trying to run the program 'dot' (part of Graphviz) from Python:</p> <pre><code># -*- coding: utf-8 -*- import os print os.environ['PATH'] print os.system("ls &gt;&gt; a.txt") print os.system("dot -o9.png -Tpng ./6.dot") </code></pre> <p>The command "ls" is there ...
4
2009-06-05T11:55:09Z
955,534
<p>Troubleshooting tips: </p> <p>A. add </p> <p><code>print os.getcwd()</code></p> <p>on the line before os.system("dot etc.</p> <p>Just to make sure that the current directory is the one with the <code>6.dot</code> file.</p> <p>B. Make sure that the <code>dot</code> program is in your path.</p> <p><code>which do...
2
2009-06-05T12:03:29Z
[ "python", "osx", "path", "graphviz", "dot" ]
How do I run "dot" as a command from Python?
955,504
<p>I am using Python on Mac OSX Leopard.</p> <p>I am trying to run the program 'dot' (part of Graphviz) from Python:</p> <pre><code># -*- coding: utf-8 -*- import os print os.environ['PATH'] print os.system("ls &gt;&gt; a.txt") print os.system("dot -o9.png -Tpng ./6.dot") </code></pre> <p>The command "ls" is there ...
4
2009-06-05T11:55:09Z
955,536
<p>You need to add the path to the 'dot' executable in Python's environment. You can do this by modifying the PATH variable in os.environ</p>
3
2009-06-05T12:04:09Z
[ "python", "osx", "path", "graphviz", "dot" ]