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
Does an application-wide exception handler make sense?
95,642
<p>Long story short, I have a substantial Python application that, among other things, does outcalls to "losetup", "mount", etc. on Linux. Essentially consuming system resources that must be released when complete.</p> <p>If my application crashes, I want to ensure these system resources are properly released.</p> <...
10
2008-09-18T18:52:30Z
95,676
<p>I like top-level exception handlers in general (regardless of language). They're a great place to cleanup resources that may not be immediately related to resources consumed inside the method that throws the exception.</p> <p>It's also a fantastic place to <strong>log</strong> those exceptions if you have such a fr...
11
2008-09-18T18:55:38Z
[ "python", "exception-handling" ]
Does an application-wide exception handler make sense?
95,642
<p>Long story short, I have a substantial Python application that, among other things, does outcalls to "losetup", "mount", etc. on Linux. Essentially consuming system resources that must be released when complete.</p> <p>If my application crashes, I want to ensure these system resources are properly released.</p> <...
10
2008-09-18T18:52:30Z
95,682
<p>A destructor (as in a __del__ method) is a bad idea, as these are not guaranteed to be called. The atexit module is a safer approach, although these will still not fire if the Python interpreter crashes (rather than the Python application), or if os._exit() is used, or the process is killed aggressively, or the mach...
7
2008-09-18T18:56:07Z
[ "python", "exception-handling" ]
Does an application-wide exception handler make sense?
95,642
<p>Long story short, I have a substantial Python application that, among other things, does outcalls to "losetup", "mount", etc. on Linux. Essentially consuming system resources that must be released when complete.</p> <p>If my application crashes, I want to ensure these system resources are properly released.</p> <...
10
2008-09-18T18:52:30Z
95,692
<p>That seems like a reasonable approach, and more straightforward and reliable than a destructor on a singleton class. You might also look at the "<a href="http://docs.python.org/lib/module-atexit.html" rel="nofollow">atexit</a>" module. (Pronounced "at exit", not "a tex it" or something like that. I confused that ...
1
2008-09-18T18:56:44Z
[ "python", "exception-handling" ]
Does an application-wide exception handler make sense?
95,642
<p>Long story short, I have a substantial Python application that, among other things, does outcalls to "losetup", "mount", etc. on Linux. Essentially consuming system resources that must be released when complete.</p> <p>If my application crashes, I want to ensure these system resources are properly released.</p> <...
10
2008-09-18T18:52:30Z
98,085
<p>if you use classes, you should free the resources they allocate in their destructors instead, of course. Use the try: on entire application just if you want to free resources that aren't already liberated by your classes' destructors.</p> <p>And instead of using a catch-all except:, you should use the following blo...
2
2008-09-18T23:45:15Z
[ "python", "exception-handling" ]
Does an application-wide exception handler make sense?
95,642
<p>Long story short, I have a substantial Python application that, among other things, does outcalls to "losetup", "mount", etc. on Linux. Essentially consuming system resources that must be released when complete.</p> <p>If my application crashes, I want to ensure these system resources are properly released.</p> <...
10
2008-09-18T18:52:30Z
120,224
<p>Consider writing a context manager and using the with statement.</p>
1
2008-09-23T10:26:25Z
[ "python", "exception-handling" ]
Python reading Oracle path
95,950
<p>On my desktop I have written a small Pylons app that connects to Oracle. I'm now trying to deploy it to my server which is running Win2k3 x64. (My desktop is 32-bit XP) The Oracle installation on the server is also 64-bit.</p> <p>I was getting errors about loading the OCI dll, so I installed the 32 bit client int...
0
2008-09-18T19:19:43Z
96,016
<p>sys.path is python's internal representation of the PYTHONPATH, it sounds to me like you want to modify the PATH.</p> <p>I'm not sure that this will work, but you can try:</p> <pre><code>import os os.environ['PATH'] += os.pathsep + "C:\\oracle32\\bin" </code></pre>
2
2008-09-18T19:26:04Z
[ "python", "oracle", "pylons", "cx-oracle" ]
Python reading Oracle path
95,950
<p>On my desktop I have written a small Pylons app that connects to Oracle. I'm now trying to deploy it to my server which is running Win2k3 x64. (My desktop is 32-bit XP) The Oracle installation on the server is also 64-bit.</p> <p>I was getting errors about loading the OCI dll, so I installed the 32 bit client int...
0
2008-09-18T19:19:43Z
125,163
<p>You need to append the c:\Oracle32\bin directory to the PATH variable of your environment before you execute python.exe.<br> In Linux, I need to set up the LD_LIBRARY_PATH variable for similar reasons, to locate the Oracle libraries, before calling python. I use wrapper shell scripts that set the variable and then c...
0
2008-09-24T02:58:07Z
[ "python", "oracle", "pylons", "cx-oracle" ]
Python reading Oracle path
95,950
<p>On my desktop I have written a small Pylons app that connects to Oracle. I'm now trying to deploy it to my server which is running Win2k3 x64. (My desktop is 32-bit XP) The Oracle installation on the server is also 64-bit.</p> <p>I was getting errors about loading the OCI dll, so I installed the 32 bit client int...
0
2008-09-18T19:19:43Z
142,775
<p>If your Python application runs in the 64-bit space, you will need to access a 64-bit installation of Oracle's oci.dll, rather than the 32-bit version. Normally you would update the system path to include the appropriate Oracle Home bin directory, prior to running the script. The solution may also vary depending on ...
0
2008-09-27T02:21:39Z
[ "python", "oracle", "pylons", "cx-oracle" ]
How to associated the cn in an ssl cert of pyOpenSSL verify_cb to a generated socket
96,508
<p>I am a little new to pyOpenSSL. I am trying to figure out how to associate the generated socket to an ssl cert. verify_cb gets called which give me access to the cert and a conn but how do I associate those things when this happens:</p> <p>cli,addr = self.server.accept()</p>
3
2008-09-18T20:24:43Z
96,797
<p>After the handshake is complete, you can get the client certificate. While the client certificate is also available in the verify callback (verify_cb), there's not really any reason to try to do anything aside from verify the certificate in that callback. Setting up an application-specific mapping is better done a...
4
2008-09-18T20:49:43Z
[ "python", "pyopenssl" ]
How to load a python module into a fresh interactive shell in Komodo?
97,513
<p>When using PyWin I can easily load a python file into a fresh interactive shell and I find this quite handy for prototyping and other exploratory tasks.</p> <p>I would like to use Komodo as my python editor, but I haven't found a replacement for PyWin's ability to restart the shell and reload the current module. ...
2
2008-09-18T22:09:21Z
97,635
<p>I use Komodo Edit, which might be a little less sophisticated than full Komodo.</p> <p>I create a "New Command" with <code>%(python) -i %f</code> as the text of the command. I have this run in a "New Console". I usually have the starting directory as %p, the top of the project directory.</p> <p>The -i option run...
5
2008-09-18T22:25:29Z
[ "python", "shell", "interpreter", "komodo" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
98,146
<p>Any particular reason you want to use Django's templates? Both <a href="http://jinja.pocoo.org/">Jinja</a> and <a href="http://genshi.edgewall.org/">Genshi</a> are, in my opinion, superior.</p> <p><hr /></p> <p>If you really want to, then see the <a href="http://docs.djangoproject.com/en/dev/topics/settings/#topic...
8
2008-09-18T23:56:36Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
98,150
<p>Google <code>AppEngine</code> uses the Django templating engine, have you taken a look at how they do it? You could possibly just use that.</p>
0
2008-09-18T23:57:12Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
98,154
<p>Found this:</p> <p><a href="http://snippets.dzone.com/posts/show/3339" rel="nofollow">http://snippets.dzone.com/posts/show/3339</a></p>
1
2008-09-18T23:58:12Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
98,178
<p>The solution is simple. It's actually <a href="http://docs.djangoproject.com/en/dev/ref/templates/api/#configuring-the-template-system-in-standalone-mode">well documented</a>, but not too easy to find. (I had to dig around -- it didn't come up when I tried a few different Google searches.)</p> <p>The following code...
117
2008-09-19T00:01:39Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
98,214
<p><a href="http://jinja.pocoo.org/2/">Jinja2</a> <a href="http://jinja.pocoo.org/2/documentation/templates">syntax</a> is pretty much the same as Django's with very few differences, and you get a much more powerfull template engine, which also compiles your template to bytecode (FAST!).</p> <p>I use it for templating...
38
2008-09-19T00:08:41Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
98,276
<p>I echo the above statements. Jinja 2 is a pretty good superset of Django templates for general use. I think they're working on making the Django templates a little less coupled to the settings.py, but Jinja should do well for you.</p>
0
2008-09-19T00:18:31Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
109,380
<p>I would also recommend jinja2. There is a <a href="https://web.archive.org/web/20090421084229/http://lucumr.pocoo.org/2008/9/16/why-jinja-is-not-django-and-why-django-should-have-a-look-at-it" rel="nofollow">nice article</a> on <code>django</code> vs. <code>jinja2</code> that gives some in-detail information on why ...
7
2008-09-20T21:02:58Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
304,195
<p>Don't. Use <a href="http://www.stringtemplate.org/" rel="nofollow">StringTemplate</a> instead--there is no reason to consider any other template engine once you know about it.</p>
1
2008-11-20T02:43:43Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
345,360
<p>I would say <a href="http://jinja.pocoo.org/" rel="nofollow">Jinja</a> as well. It is definitely <strong>more powerful</strong> than Django Templating Engine and it is <strong>stand alone</strong>.</p> <p>If this was an external plug to an existing Django application, you could create <a href="http://docs.djangopro...
2
2008-12-05T22:15:35Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
11,519,049
<p>While running the <code>manage.py</code> shell:</p> <pre><code>&gt;&gt;&gt; from django import template &gt;&gt;&gt; t = template.Template('My name is {{ me }}.') &gt;&gt;&gt; c = template.Context({'me': 'ShuJi'}) &gt;&gt;&gt; t.render(c) </code></pre>
0
2012-07-17T08:50:43Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
20,480,267
<p>Thanks for the help folks. Here is one more addition. The case where you need to use custom template tags.</p> <p>Let's say you have this important template tag in the module read.py</p> <pre><code>from django import template register = template.Library() @register.filter(name='bracewrap') def bracewrap(value): ...
2
2013-12-09T20:37:34Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
How do I use Django templates without the rest of Django?
98,135
<p>I want to use the Django template engine in my (Python) code, but I'm not building a Django-based web site. How do I use it without having a settings.py file (and others) and having to set the DJANGO_SETTINGS_MODULE environment variable?</p> <p>If I run the following code:</p> <pre><code>&gt;&gt;&gt; import djang...
85
2008-09-18T23:55:21Z
34,494,931
<p>According to the Jinja documentation, <a href="http://jinja.pocoo.org/docs/dev/intro/#experimental-python-3-support" rel="nofollow">Python 3 support is still experimental</a>. So if you are on Python 3 and performance is not an issue, you can use django's built in template engine. </p> <p>Django 1.8 introduced supp...
0
2015-12-28T14:00:18Z
[ "python", "django", "templates", "django-templates", "template-engine" ]
What is the best solution for database connection pooling in python?
98,687
<p>I have developed some custom DAO-like classes to meet some very specialized requirements for my project that is a server-side process that does not run inside any kind of framework. </p> <p>The solution works great except that every time a new request is made, I open a new connection via MySQLdb.connect. </p> <p...
24
2008-09-19T01:36:03Z
98,703
<p>Wrap your connection class.</p> <p>Set a limit on how many connections you make. Return an unused connection. Intercept close to free the connection.</p> <p>Update: I put something like this in dbpool.py:</p> <pre><code>import sqlalchemy.pool as pool import MySQLdb as mysql mysql = pool.manage(mysql) </code></pre...
11
2008-09-19T01:38:19Z
[ "python", "mysql", "connection-pooling" ]
What is the best solution for database connection pooling in python?
98,687
<p>I have developed some custom DAO-like classes to meet some very specialized requirements for my project that is a server-side process that does not run inside any kind of framework. </p> <p>The solution works great except that every time a new request is made, I open a new connection via MySQLdb.connect. </p> <p...
24
2008-09-19T01:36:03Z
98,906
<p>IMO, the "more obvious/more idiomatic/better solution" is to use an existing ORM rather than invent DAO-like classes.</p> <p>It appears to me that ORM's are more popular than "raw" SQL connections. Why? Because Python <em>is</em> OO, and the mapping from SQL row to to object <em>is</em> absolutely essential. Ther...
12
2008-09-19T02:13:07Z
[ "python", "mysql", "connection-pooling" ]
What is the best solution for database connection pooling in python?
98,687
<p>I have developed some custom DAO-like classes to meet some very specialized requirements for my project that is a server-side process that does not run inside any kind of framework. </p> <p>The solution works great except that every time a new request is made, I open a new connection via MySQLdb.connect. </p> <p...
24
2008-09-19T01:36:03Z
99,565
<p>In MySQL?</p> <p>I'd say don't bother with the connection pooling. They're often a source of trouble and with MySQL they're not going to bring you the performance advantage you're hoping for. This road may be a lot of effort to follow--politically--because there's so much best practices hand waving and textbook v...
21
2008-09-19T04:11:38Z
[ "python", "mysql", "connection-pooling" ]
What is the best solution for database connection pooling in python?
98,687
<p>I have developed some custom DAO-like classes to meet some very specialized requirements for my project that is a server-side process that does not run inside any kind of framework. </p> <p>The solution works great except that every time a new request is made, I open a new connection via MySQLdb.connect. </p> <p...
24
2008-09-19T01:36:03Z
864,728
<p>I've just been looking for the same sort of thing.</p> <p>I've found <a href="http://code.google.com/p/pysqlpool/" rel="nofollow">pysqlpool</a> and the <a href="http://www.sqlalchemy.org/docs/04/pooling.html" rel="nofollow">sqlalchemy pool module</a></p>
1
2009-05-14T17:41:51Z
[ "python", "mysql", "connection-pooling" ]
What is the best solution for database connection pooling in python?
98,687
<p>I have developed some custom DAO-like classes to meet some very specialized requirements for my project that is a server-side process that does not run inside any kind of framework. </p> <p>The solution works great except that every time a new request is made, I open a new connection via MySQLdb.connect. </p> <p...
24
2008-09-19T01:36:03Z
7,154,158
<p>Making your own connection pool is a BAD idea if your app ever decides to start using multi-threading. Making a connection pool for a multi-threaded application is much more complicated than one for a single-threaded application. You can use something like PySQLPool in that case.</p> <p>It's also a BAD idea to use ...
2
2011-08-22T22:05:55Z
[ "python", "mysql", "connection-pooling" ]
What is the best solution for database connection pooling in python?
98,687
<p>I have developed some custom DAO-like classes to meet some very specialized requirements for my project that is a server-side process that does not run inside any kind of framework. </p> <p>The solution works great except that every time a new request is made, I open a new connection via MySQLdb.connect. </p> <p...
24
2008-09-19T01:36:03Z
24,741,694
<p>Old thread, but for general-purpose pooling (connections or any expensive object), I use something like:</p> <pre><code>def pool(ctor, limit=None): local_pool = multiprocessing.Queue() n = multiprocesing.Value('i', 0) @contextlib.contextmanager def pooled(ctor=ctor, lpool=local_pool, n=n): #...
2
2014-07-14T16:53:14Z
[ "python", "mysql", "connection-pooling" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
100,037
<p>Metaclasses are the secret sauce that make 'class' work. The default metaclass for a new style object is called 'type'.</p> <pre class="lang-none prettyprint-override"><code>class type(object) | type(object) -&gt; the object's type | type(name, bases, dict) -&gt; a new type </code></pre> <p>Metaclasses take ...
217
2008-09-19T06:26:10Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
100,059
<p>I think the ONLamp introduction to metaclass programming is well written and gives a really good introduction to the topic despite being several years old already.</p> <p><a href="http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html">http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html</a></p> ...
56
2008-09-19T06:32:58Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
100,091
<p>One use for metaclasses is adding new properties and methods to an instance automatically.</p> <p>For example, if you look at <a href="http://docs.djangoproject.com/en/dev/topics/db/models/">Django models</a>, their definition looks a bit confusing. It looks as if you are only defining class properties:</p> <pre><...
82
2008-09-19T06:45:40Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
100,146
<p>A metaclass is the class of a class. Like a class defines how an instance of the class behaves, a metaclass defines how a class behaves. A class is an instance of a metaclass.</p> <p><a href="http://i.stack.imgur.com/QQ0OK.png"><img src="http://i.stack.imgur.com/QQ0OK.png" alt="metaclass diagram"></a></p> <p>While...
1,179
2008-09-19T07:01:58Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
6,428,779
<p>Others have explained how metaclasses work and how they fit into the Python type system. Here's an example of what they can be used for. In a testing framework I wrote, I wanted to keep track of the order in which classes were defined, so that I could later instantiate them in this order. I found it easiest to do th...
70
2011-06-21T16:30:26Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
6,581,949
<h1>Classes as objects</h1> <p>Before understanding metaclasses, you need to master classes in Python. And Python has a very peculiar idea of what classes are, borrowed from the Smalltalk language.</p> <p>In most languages, classes are just pieces of code that describe how to produce an object. That's kinda true in P...
4,510
2011-07-05T11:29:50Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
21,999,253
<p>A metaclass is a class that tells how (some) other class should be created.</p> <p>This is a case where I saw metaclass as a solution to my problem: I had a really complicated problem, that probably could have been solved differently, but I chose to solve it using a metaclass. Because of the complexity, it is one ...
20
2014-02-24T21:20:49Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
31,930,795
<blockquote> <h1>What are metaclasses? What do you use them for?</h1> </blockquote> <p>A class is to an instance as a metaclass is to a class. </p> <p>Put another way, a class is an instance of a metaclass.</p> <p>Put a third way, a metaclass is a class's class.</p> <p>Still hopelessly confused? So was I, until I...
38
2015-08-10T23:28:09Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
35,732,111
<p><strong>Python 3 update</strong></p> <p>There are (at this point) two key methods in a metaclass:</p> <ul> <li><code>__prepare__</code>, and</li> <li><code>__new__</code></li> </ul> <p><code>__prepare__</code> lets you supply a custom mapping (such as an <code>OrderedDict</code>) to be used as the namespace while...
23
2016-03-01T19:48:34Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
38,858,285
<p><code>type</code> is actually a <code>metaclass</code> -- a class that creates another classes. Most <code>metaclass</code> are the subclasses of <code>type</code>. The <code>metaclass</code> receives the <code>new</code> class as its first argument and provide access to class object with details as mentioned below:...
4
2016-08-09T18:49:44Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is a metaclass in Python?
100,003
<p>What are metaclasses? What do you use them for?</p>
3,219
2008-09-19T06:10:46Z
40,017,019
<h1>Role of a metaclass's <code>__call__()</code> method when creating a class instance</h1> <p>If you've done Python programming for more than a few months you'll eventually stumble upon code that looks like this:</p> <pre><code># define a class class SomeClass(object): # ... # some definition here ... #...
0
2016-10-13T09:21:26Z
[ "python", "oop", "metaclass", "python-datamodel" ]
What is the standard way to add N seconds to datetime.time in Python?
100,210
<p>Given a <code>datetime.time</code> value in Python, is there a standard way to add an integer number of seconds to it, so that <code>11:34:59</code> + 3 = <code>11:35:02</code>, for example?</p> <p>These obvious ideas don't work:</p> <pre><code>&gt;&gt;&gt; datetime.time(11, 34, 59) + 3 TypeError: unsupported oper...
188
2008-09-19T07:19:36Z
100,250
<p>Try adding a datetime.datetime to a datetime.timedelta. If you only want the time portion, you can call the time() method on the resultant datetime.datetime object to get it.</p>
1
2008-09-19T07:29:25Z
[ "python", "datetime", "time", "math" ]
What is the standard way to add N seconds to datetime.time in Python?
100,210
<p>Given a <code>datetime.time</code> value in Python, is there a standard way to add an integer number of seconds to it, so that <code>11:34:59</code> + 3 = <code>11:35:02</code>, for example?</p> <p>These obvious ideas don't work:</p> <pre><code>&gt;&gt;&gt; datetime.time(11, 34, 59) + 3 TypeError: unsupported oper...
188
2008-09-19T07:19:36Z
100,345
<p>You can use full <code>datetime</code> variables with <code>timedelta</code>, and by providing a dummy date then using <code>time</code> to just get the time value.</p> <p>For example:</p> <pre><code>import datetime a = datetime.datetime(100,1,1,11,34,59) b = a + datetime.timedelta(0,3) # days, seconds, then other...
281
2008-09-19T07:54:39Z
[ "python", "datetime", "time", "math" ]
What is the standard way to add N seconds to datetime.time in Python?
100,210
<p>Given a <code>datetime.time</code> value in Python, is there a standard way to add an integer number of seconds to it, so that <code>11:34:59</code> + 3 = <code>11:35:02</code>, for example?</p> <p>These obvious ideas don't work:</p> <pre><code>&gt;&gt;&gt; datetime.time(11, 34, 59) + 3 TypeError: unsupported oper...
188
2008-09-19T07:19:36Z
100,404
<p>One little thing, might add clarity to override the default value for seconds</p> <pre><code>&gt;&gt;&gt; b = a + datetime.timedelta(seconds=3000) &gt;&gt;&gt; b datetime.datetime(1, 1, 1, 12, 24, 59) </code></pre>
9
2008-09-19T08:08:10Z
[ "python", "datetime", "time", "math" ]
What is the standard way to add N seconds to datetime.time in Python?
100,210
<p>Given a <code>datetime.time</code> value in Python, is there a standard way to add an integer number of seconds to it, so that <code>11:34:59</code> + 3 = <code>11:35:02</code>, for example?</p> <p>These obvious ideas don't work:</p> <pre><code>&gt;&gt;&gt; datetime.time(11, 34, 59) + 3 TypeError: unsupported oper...
188
2008-09-19T07:19:36Z
100,776
<p>Thanks to @<a href="#100345">Pax Diablo</a>, @bvmou and @Arachnid for the suggestion of using full datetimes throughout. If I have to accept datetime.time objects from an external source, then this seems to be an alternative <code>add_secs_to_time()</code> function:</p> <pre><code>def add_secs_to_time(timeval, sec...
7
2008-09-19T09:40:06Z
[ "python", "datetime", "time", "math" ]
What is the standard way to add N seconds to datetime.time in Python?
100,210
<p>Given a <code>datetime.time</code> value in Python, is there a standard way to add an integer number of seconds to it, so that <code>11:34:59</code> + 3 = <code>11:35:02</code>, for example?</p> <p>These obvious ideas don't work:</p> <pre><code>&gt;&gt;&gt; datetime.time(11, 34, 59) + 3 TypeError: unsupported oper...
188
2008-09-19T07:19:36Z
101,947
<p>As others here have stated, you can just use full datetime objects throughout:</p> <pre><code>sometime = get_some_time() # the time to which you want to add 3 seconds later = (datetime.combine(date.today(), sometime) + timedelta(seconds=3)).time() </code></pre> <p>However, I think it's worth explaining why full da...
34
2008-09-19T13:47:29Z
[ "python", "datetime", "time", "math" ]
What is the standard way to add N seconds to datetime.time in Python?
100,210
<p>Given a <code>datetime.time</code> value in Python, is there a standard way to add an integer number of seconds to it, so that <code>11:34:59</code> + 3 = <code>11:35:02</code>, for example?</p> <p>These obvious ideas don't work:</p> <pre><code>&gt;&gt;&gt; datetime.time(11, 34, 59) + 3 TypeError: unsupported oper...
188
2008-09-19T07:19:36Z
6,839,259
<p>If it's worth adding another file / dependency to your project, I've just written a tiny little class that extends <code>datetime.time</code> with the ability to do arithmetic. When you go past midnight, it wraps around zero. Now, "What time will it be, 24 hours from now" has a lot of corner cases, including dayli...
4
2011-07-27T03:49:05Z
[ "python", "datetime", "time", "math" ]
How can I analyze Python code to identify problematic areas?
100,298
<p>I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.</p> <p>Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some ...
91
2008-09-19T07:40:22Z
100,394
<p>For static analysis there is <a href="http://www.logilab.org/857">pylint</a> and <a href="http://pychecker.sourceforge.net/">pychecker</a>. Personally I use pylint as it seems to be more comprehensive than pychecker. </p> <p>For cyclomatic complexity you can try <a href="http://www.journyx.com/curt/complexity.html"...
17
2008-09-19T08:05:48Z
[ "python", "static-analysis", "cyclomatic-complexity" ]
How can I analyze Python code to identify problematic areas?
100,298
<p>I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.</p> <p>Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some ...
91
2008-09-19T07:40:22Z
100,687
<p>Thanks to <a href="http://pydev.org/" rel="nofollow">Pydev</a>, you can <a href="http://pydev.org/manual_adv_pylint.html" rel="nofollow">integrate pylint</a> in the <a href="http://www.eclipse.org/" rel="nofollow">Eclipse IDE</a> really easily and get a code report each time you save a modified file.</p>
6
2008-09-19T09:19:57Z
[ "python", "static-analysis", "cyclomatic-complexity" ]
How can I analyze Python code to identify problematic areas?
100,298
<p>I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.</p> <p>Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some ...
91
2008-09-19T07:40:22Z
105,473
<p>For measuring cyclomatic complexity, there's a nice tool available at <a href="http://www.traceback.org/2008/03/31/measuring-cyclomatic-complexity-of-python-code/">traceback.org</a>. The page also gives a good overview of how to interpret the results.</p> <p>+1 for <a href="http://www.logilab.org/project/pylint">p...
29
2008-09-19T20:44:22Z
[ "python", "static-analysis", "cyclomatic-complexity" ]
How can I analyze Python code to identify problematic areas?
100,298
<p>I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.</p> <p>Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some ...
91
2008-09-19T07:40:22Z
574,673
<p>There is a tool called <a href="http://clonedigger.sourceforge.net/" rel="nofollow">CloneDigger</a> that helps you find similar code snippets.</p>
4
2009-02-22T09:57:22Z
[ "python", "static-analysis", "cyclomatic-complexity" ]
How can I analyze Python code to identify problematic areas?
100,298
<p>I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.</p> <p>Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some ...
91
2008-09-19T07:40:22Z
2,799,127
<p>Pycana works like charm when you need to understand a new project!</p> <blockquote> <p><a href="http://sourceforge.net/projects/pycana/">PyCAna</a> (Python Code Analyzer) is a fancy name for a simple code analyzer for python that creates a class diagram after executing your code.</p> </blockquote> <p>See...
11
2010-05-09T20:50:34Z
[ "python", "static-analysis", "cyclomatic-complexity" ]
How can I analyze Python code to identify problematic areas?
100,298
<p>I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.</p> <p>Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some ...
91
2008-09-19T07:40:22Z
14,793,812
<p>For cyclomatic complexity you can use <code>radon</code>: <a href="https://github.com/rubik/radon">https://github.com/rubik/radon</a></p> <p>(Use <code>pip</code> to install it: <code>pip install radon</code>)</p> <p>Additionally it also has these features:</p> <ul> <li>raw metrics (these include SLOC, comment li...
24
2013-02-10T01:47:31Z
[ "python", "static-analysis", "cyclomatic-complexity" ]
How can I analyze Python code to identify problematic areas?
100,298
<p>I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.</p> <p>Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some ...
91
2008-09-19T07:40:22Z
22,649,562
<p>Use <a href="https://pypi.python.org/pypi/flake8" rel="nofollow">flake8</a>, which provides pep8, pyflakes, and cyclomatic complexity analysis in one tool</p>
4
2014-03-26T01:08:02Z
[ "python", "static-analysis", "cyclomatic-complexity" ]
How can I analyze Python code to identify problematic areas?
100,298
<p>I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.</p> <p>Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some ...
91
2008-09-19T07:40:22Z
39,937,878
<p>For checking cyclomatic complexity, there is of course the <a href="https://pypi.python.org/pypi/mccabe/" rel="nofollow"><strong><code>mccabe</code></strong></a> package.</p> <p>Installation:</p> <pre><code>$ sudo pip install --upgrade mccabe </code></pre> <p>Usage:</p> <pre><code>$ python -m mccabe --min=6 /pat...
0
2016-10-08T22:11:25Z
[ "python", "static-analysis", "cyclomatic-complexity" ]
Python on Windows - how to wait for multiple child processes?
100,624
<p>How to wait for multiple child processes in Python on Windows, without active wait (polling)? Something like this <em>almost</em> works for me:</p> <pre><code>proc1 = subprocess.Popen(['python','mytest.py']) proc2 = subprocess.Popen(['python','mytest.py']) proc1.wait() print "1 finished" proc2.wait() print "2 f...
20
2008-09-19T09:02:43Z
100,886
<p>It might seem overkill, but, here it goes:</p> <pre><code>import Queue, thread, subprocess results= Queue.Queue() def process_waiter(popen, description, que): try: popen.wait() finally: que.put( (description, popen.returncode) ) process_count= 0 proc1= subprocess.Popen( ['python', 'mytest.py'] ) thread.st...
12
2008-09-19T10:09:47Z
[ "python", "windows", "asynchronous" ]
Python on Windows - how to wait for multiple child processes?
100,624
<p>How to wait for multiple child processes in Python on Windows, without active wait (polling)? Something like this <em>almost</em> works for me:</p> <pre><code>proc1 = subprocess.Popen(['python','mytest.py']) proc2 = subprocess.Popen(['python','mytest.py']) proc1.wait() print "1 finished" proc2.wait() print "2 f...
20
2008-09-19T09:02:43Z
111,225
<p>Twisted has an <a href="http://twistedmatrix.com/documents/8.1.0/api/twisted.internet.interfaces.IReactorProcess.html">asynchronous process-spawning API</a> which works on Windows. There are actually several different implementations, many of which are not so great, but you can switch between them without changing ...
5
2008-09-21T15:25:33Z
[ "python", "windows", "asynchronous" ]
Python on Windows - how to wait for multiple child processes?
100,624
<p>How to wait for multiple child processes in Python on Windows, without active wait (polling)? Something like this <em>almost</em> works for me:</p> <pre><code>proc1 = subprocess.Popen(['python','mytest.py']) proc2 = subprocess.Popen(['python','mytest.py']) proc1.wait() print "1 finished" proc2.wait() print "2 f...
20
2008-09-19T09:02:43Z
149,327
<p>Twisted on Windows will perform an active wait under the covers. If you don't want to use threads, you will have to use the win32 API to avoid polling. Something like this:</p> <pre><code>import win32process import win32event # Note: CreateProcess() args are somewhat cryptic, look them up on MSDN proc1, thread1, p...
4
2008-09-29T15:52:35Z
[ "python", "windows", "asynchronous" ]
Python on Windows - how to wait for multiple child processes?
100,624
<p>How to wait for multiple child processes in Python on Windows, without active wait (polling)? Something like this <em>almost</em> works for me:</p> <pre><code>proc1 = subprocess.Popen(['python','mytest.py']) proc2 = subprocess.Popen(['python','mytest.py']) proc1.wait() print "1 finished" proc2.wait() print "2 f...
20
2008-09-19T09:02:43Z
573,196
<p>Building on zseil's answer, you can do this with a mix of subprocess and win32 API calls. I used straight ctypes, because my Python doesn't happen to have win32api installed. I'm just spawning sleep.exe from MSYS here as an example, but clearly you could spawn any process you like. I use OpenProcess() to get a HANDL...
5
2009-02-21T15:12:19Z
[ "python", "windows", "asynchronous" ]
Python on Windows - how to wait for multiple child processes?
100,624
<p>How to wait for multiple child processes in Python on Windows, without active wait (polling)? Something like this <em>almost</em> works for me:</p> <pre><code>proc1 = subprocess.Popen(['python','mytest.py']) proc2 = subprocess.Popen(['python','mytest.py']) proc1.wait() print "1 finished" proc2.wait() print "2 f...
20
2008-09-19T09:02:43Z
20,292,161
<p>You can use <a href="https://code.google.com/p/psutil/" rel="nofollow">psutil</a>:</p> <pre><code>&gt;&gt;&gt; import subprocess &gt;&gt;&gt; import psutil &gt;&gt;&gt; &gt;&gt;&gt; proc1 = subprocess.Popen(['python','mytest.py']) &gt;&gt;&gt; proc2 = subprocess.Popen(['python','mytest.py']) &gt;&gt;&gt; ls = ...
1
2013-11-29T19:17:31Z
[ "python", "windows", "asynchronous" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,762
<p>In the first test, Python try to convert the object to a <code>bool</code> value if it is not already one. Roughly, <strong>we are asking the object : are you meaningful or not ?</strong> This is done using the following algorithm :</p> <ol> <li><p>If the object has a <code>__nonzero__</code> special method (as do ...
139
2008-09-19T09:38:10Z
[ "python" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,764
<p>Because <code>None</code> is not the only thing that is considered false.</p> <pre><code>if not False: print "False is false." if not 0: print "0 is false." if not []: print "An empty list is false." if not (): print "An empty tuple is false." if not {}: print "An empty dict is false." if not ""...
30
2008-09-19T09:38:26Z
[ "python" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,766
<p>These two comparisons serve different purposes. The former checks for boolean value of something, the second checks for identity with None value.</p>
2
2008-09-19T09:38:37Z
[ "python" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,768
<p>For one the first example is shorter and looks nicer. As per the other posts what you choose also depends on what you really want to do with the comparison.</p>
0
2008-09-19T09:39:01Z
[ "python" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,771
<p><a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">Style guide</a> recommends to use is or is not if you are testing for None-ness</p> <blockquote> <pre><code>- Comparisons to singletons like None should always be done with 'is' or 'is not', never the equality operators. </code></pre> </blockquote>...
1
2008-09-19T09:39:16Z
[ "python" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,775
<p>The answer is "it depends".</p> <p>I use the first example if I consider 0, "", [] and False (list not exhaustive) to be equivalent to None in this context.</p>
0
2008-09-19T09:39:41Z
[ "python" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,828
<p>Personally, I chose a consistent approach across languages: I do <code>if (var)</code> (or equivalent) only if var is declared as boolean (or defined as such, in C we don't have a specific type). I even prefix these variables with a <code>b</code> (so it would be <code>bVar</code> actually) to be sure I won't accide...
0
2008-09-19T09:53:04Z
[ "python" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,903
<p>These are actually both poor practices. Once upon a time, it was considered OK to casually treat None and False as similar. However, since Python 2.2 this is not the best policy.</p> <p>First, when you do an <code>if x</code> or <code>if not x</code> kind of test, Python has to implicitly convert <code>x</code> ...
39
2008-09-19T10:12:34Z
[ "python" ]
Why is "if not someobj:" better than "if someobj == None:" in Python?
100,732
<p>I've seen several examples of code like this:</p> <pre><code>if not someobj: #do something </code></pre> <p>But I'm wondering why not doing:</p> <pre><code>if someobj == None: #do something </code></pre> <p>Is there any difference? Does one have an advantage over the other?</p>
82
2008-09-19T09:29:19Z
100,974
<p>If you ask</p> <pre><code>if not spam: print "Sorry. No SPAM." </code></pre> <p>the <em>__nonzero__</em> method of <em>spam</em> gets called. From the Python manual:</p> <blockquote> <p><strong>__nonzero__</strong>(<em>self</em>) Called to implement truth value testing, and the built-in operation bool()...
3
2008-09-19T10:27:52Z
[ "python" ]
Building Python C extension modules for Windows
101,061
<p>I have a C extension module and it would be nice to distribute built binaries. Setuptools makes it easy to build extensions modules on OS X and GNU/Linux, since those OSs come with GCC, but I don't know how to do it in Windows.</p> <p>Would I need to buy a copy of Visual Studio, or does Visual Studio Express work? ...
10
2008-09-19T10:53:12Z
101,087
<p>You can use both MinGW and VC++ Express (free, no need to buy it).</p> <p>See:</p> <ol> <li><a href="http://eli.thegreenplace.net/2008/06/28/compiling-python-extensions-with-distutils-and-mingw/">http://eli.thegreenplace.net/2008/06/28/compiling-python-extensions-with-distutils-and-mingw/</a></li> <li><a href="htt...
13
2008-09-19T10:57:36Z
[ "python", "windows" ]
Building Python C extension modules for Windows
101,061
<p>I have a C extension module and it would be nice to distribute built binaries. Setuptools makes it easy to build extensions modules on OS X and GNU/Linux, since those OSs come with GCC, but I don't know how to do it in Windows.</p> <p>Would I need to buy a copy of Visual Studio, or does Visual Studio Express work? ...
10
2008-09-19T10:53:12Z
101,090
<p>Setuptools and distutils don't come with gcc, but they use the same compiler Python was built with. The difference is mostly that on the typical UNIX system that compiler is 'gcc' and you have it installed.</p> <p>In order to compile extension modules on Windows, you need a compiler for Windows. MSVS will do, even ...
1
2008-09-19T10:57:53Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
101,143
<p>Try win32clipboard from the win32all package (that's probably installed if you're on ActiveState Python).</p> <p>See sample here: <a href="http://code.activestate.com/recipes/474121/" rel="nofollow">http://code.activestate.com/recipes/474121/</a></p>
3
2008-09-19T11:15:07Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
101,167
<p>You can use the module called <a href="http://docs.activestate.com/activepython/2.5/pywin32/win32clipboard.html">win32clipboard</a>, which is part of <a href="http://sourceforge.net/projects/pywin32/">pywin32</a>.</p> <p>Here is an example that first sets the clipboard data then gets it:</p> <pre><code>import win3...
47
2008-09-19T11:20:29Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
8,039,424
<p>I've seen many suggestions to use the win32 module, but Tkinter provides the shortest and easiest method I've seen, as in this post: <a href="http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python/4203897#4203897">How do I copy a string to the clipboard on Windows u...
18
2011-11-07T16:27:31Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
11,096,779
<p>The most upvoted answer above is weird in a way that it simply clears the Clipboard and then gets the content (which is then empty). One could clear the clipboard to be sure that some clipboard content type like "formated text" does not "cover" your plain text content you want to save in the clipboard.</p> <p>The f...
10
2012-06-19T08:00:02Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
23,285,159
<p>If you don't want to install extra packages, <code>ctypes</code> can get the job done as well.</p> <pre><code>import ctypes CF_TEXT = 1 kernel32 = ctypes.windll.kernel32 user32 = ctypes.windll.user32 user32.OpenClipboard(0) if user32.IsClipboardFormatAvailable(CF_TEXT): data = user32.GetClipboardData(CF_TEXT...
4
2014-04-25T05:54:12Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
23,844,754
<p>you can easily get this done through the built-in module <a href="https://docs.python.org/2/library/tkinter.html">Tkinter</a> which is basically a GUI library. This code creates a blank widget to get the clipboard content from OS.</p> <pre><code>#from tkinter import Tk # Python 3 from Tkinter import Tk Tk().clipbo...
10
2014-05-24T11:58:36Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
27,995,097
<p>For my <strong>console program</strong> the answers with tkinter above did not quite work for me because the .destroy() always gave an error,:</p> <blockquote> <p>can't invoke "event" command: application has been destroyed while executing...</p> </blockquote> <p>or when using .withdraw() the console window did ...
1
2015-01-17T01:08:26Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
36,886,989
<p>Use Pythons library <a href="https://pypi.python.org/pypi/clipboard/0.0.4" rel="nofollow">Clipboard </a> </p> <p>Its simply used like this:</p> <pre><code>import clipboard clipboard.copy("this text is now in the clipboard") print clipboard.paste() </code></pre>
1
2016-04-27T10:19:44Z
[ "python", "windows" ]
How do I read text from the (windows) clipboard from python?
101,128
<p>How do I read text from the (windows) clipboard from python?</p>
44
2008-09-19T11:09:27Z
38,171,680
<p>I found out this was the easiest way to get access to the clipboard from python:</p> <p>1) Install pyperclip: <code>pip install pyperclip</code></p> <p>2) Usage:</p> <pre><code>import pyperclip s = pyperclip.paste() pyperclip.copy(s) # the type of s is string </code></pre> <p>Tested on Win10 64-bit, Python 3.5...
0
2016-07-03T15:54:43Z
[ "python", "windows" ]
Is there a zip-like method in .Net?
101,174
<p>In Python there is a really neat function called <code>zip</code> which can be used to iterate through two lists at the same time:</p> <pre><code>list1 = [1, 2, 3] list2 = ["a", "b", "c"] for v1, v2 in zip(list1, list2): print v1 + " " + v2 </code></pre> <p>The above code shoul produce the following:</p> <pre...
12
2008-09-19T11:21:57Z
101,207
<p>Nope, there is no such function in .NET. You have roll out your own. Note that C# doesn't support tuples, so python-like syntax sugar is missing too.</p> <p>You can use something like this:</p> <pre><code>class Pair&lt;T1, T2&gt; { public T1 First { get; set;} public T2 Second { get; set;} } static IEnume...
5
2008-09-19T11:31:17Z
[ ".net", "python", "list", "iteration" ]
Is there a zip-like method in .Net?
101,174
<p>In Python there is a really neat function called <code>zip</code> which can be used to iterate through two lists at the same time:</p> <pre><code>list1 = [1, 2, 3] list2 = ["a", "b", "c"] for v1, v2 in zip(list1, list2): print v1 + " " + v2 </code></pre> <p>The above code shoul produce the following:</p> <pre...
12
2008-09-19T11:21:57Z
101,222
<p>Update: It is built-in in C# 4 as <a href="https://msdn.microsoft.com/en-us/library/vstudio/dd267698(v=vs.110).aspx" rel="nofollow">System.Linq.Enumerable.Zip Method</a></p> <p>Here is a C# 3 version:</p> <pre><code>IEnumerable&lt;TResult&gt; Zip&lt;TResult,T1,T2&gt; (IEnumerable&lt;T1&gt; a, IEnumerable&...
25
2008-09-19T11:34:43Z
[ ".net", "python", "list", "iteration" ]
Is there a zip-like method in .Net?
101,174
<p>In Python there is a really neat function called <code>zip</code> which can be used to iterate through two lists at the same time:</p> <pre><code>list1 = [1, 2, 3] list2 = ["a", "b", "c"] for v1, v2 in zip(list1, list2): print v1 + " " + v2 </code></pre> <p>The above code shoul produce the following:</p> <pre...
12
2008-09-19T11:21:57Z
101,227
<p>As far as I know there is not. I wrote one for myself (as well as a few other useful extensions and put them in a project called <a href="http://www.codeplex.com/nextension" rel="nofollow">NExtension</a> on Codeplex.</p> <p>Apparently the Parallel extensions for .NET have a Zip function.</p> <p>Here's a simplified...
8
2008-09-19T11:35:28Z
[ ".net", "python", "list", "iteration" ]
Is there a zip-like method in .Net?
101,174
<p>In Python there is a really neat function called <code>zip</code> which can be used to iterate through two lists at the same time:</p> <pre><code>list1 = [1, 2, 3] list2 = ["a", "b", "c"] for v1, v2 in zip(list1, list2): print v1 + " " + v2 </code></pre> <p>The above code shoul produce the following:</p> <pre...
12
2008-09-19T11:21:57Z
101,284
<p>There's also one in F#:</p> <p>let zipped = Seq.zip firstEnumeration secondEnumation</p>
2
2008-09-19T11:55:09Z
[ ".net", "python", "list", "iteration" ]
IronClad equivalent for Jython
101,301
<p>For IronPython there is a project - <a href="http://www.resolversystems.com/documentation/index.php/Ironclad" rel="nofollow">IronClad</a>, that aims to transparently run C extensions in it. Is there a similiar project for Jython?</p>
3
2008-09-19T11:58:00Z
101,539
<p>You can probably use Java's loadLibrary to do that (provided it works in your platform's java). It is in the java library: <a href="http://java.sun.com/javase/6/docs/api/java/lang/System.html#loadLibrary(java.lang.String)" rel="nofollow" title="sun.com loadLibrary documentation">java.System.loadLibrary()</a>.</p> <...
1
2008-09-19T12:44:51Z
[ "python", "c", "ironpython", "jython", "ironclad" ]
IronClad equivalent for Jython
101,301
<p>For IronPython there is a project - <a href="http://www.resolversystems.com/documentation/index.php/Ironclad" rel="nofollow">IronClad</a>, that aims to transparently run C extensions in it. Is there a similiar project for Jython?</p>
3
2008-09-19T11:58:00Z
30,607,649
<p>Keep an eye on JyNI (<a href="http://www.jyni.org" rel="nofollow">http://www.jyni.org</a>), which is to Jython exactly what is Ironclad to IronPython. As of this writing JyNI is still alpha state though.</p> <p>If you just want to use some C-library from Jython, simply use JNA from Jython like you would do from Jav...
1
2015-06-02T22:23:15Z
[ "python", "c", "ironpython", "jython", "ironclad" ]
How do you access an authenticated Google App Engine service from a (non-web) python client?
101,742
<p>I have a Google App Engine app - <a href="http://mylovelyapp.appspot.com/">http://mylovelyapp.appspot.com/</a> It has a page - mylovelypage</p> <p>For the moment, the page just does <code>self.response.out.write('OK')</code></p> <p>If I run the following Python at my computer:</p> <pre><code>import urllib2 f = ur...
49
2008-09-19T13:19:09Z
102,158
<p>I'm not a python expert or a app engine expert. But did you try following the sample appl at <a href="http://code.google.com/appengine/docs/gettingstarted/usingusers.html" rel="nofollow">http://code.google.com/appengine/docs/gettingstarted/usingusers.html</a>. I created one at <a href="http://quizengine.appspot.com"...
-1
2008-09-19T14:16:12Z
[ "python", "web-services", "google-app-engine", "authentication" ]
How do you access an authenticated Google App Engine service from a (non-web) python client?
101,742
<p>I have a Google App Engine app - <a href="http://mylovelyapp.appspot.com/">http://mylovelyapp.appspot.com/</a> It has a page - mylovelypage</p> <p>For the moment, the page just does <code>self.response.out.write('OK')</code></p> <p>If I run the following Python at my computer:</p> <pre><code>import urllib2 f = ur...
49
2008-09-19T13:19:09Z
102,162
<p>Im not too familiar with AppEngine, or Googles web apis, but for a brute force approach you could write a script with something like mechanize (<a href="http://wwwsearch.sourceforge.net/mechanize/" rel="nofollow">http://wwwsearch.sourceforge.net/mechanize/</a>) to simply walk through the login process before you beg...
0
2008-09-19T14:16:24Z
[ "python", "web-services", "google-app-engine", "authentication" ]
How do you access an authenticated Google App Engine service from a (non-web) python client?
101,742
<p>I have a Google App Engine app - <a href="http://mylovelyapp.appspot.com/">http://mylovelyapp.appspot.com/</a> It has a page - mylovelypage</p> <p>For the moment, the page just does <code>self.response.out.write('OK')</code></p> <p>If I run the following Python at my computer:</p> <pre><code>import urllib2 f = ur...
49
2008-09-19T13:19:09Z
102,509
<p>appcfg.py, the tool that uploads data to App Engine has to do exactly this to authenticate itself with the App Engine server. The relevant functionality is abstracted into appengine_rpc.py. In a nutshell, the solution is:</p> <ol> <li>Use the <a href="http://code.google.com/apis/accounts/docs/AuthForInstalledApps.h...
38
2008-09-19T14:55:24Z
[ "python", "web-services", "google-app-engine", "authentication" ]
How do you access an authenticated Google App Engine service from a (non-web) python client?
101,742
<p>I have a Google App Engine app - <a href="http://mylovelyapp.appspot.com/">http://mylovelyapp.appspot.com/</a> It has a page - mylovelypage</p> <p>For the moment, the page just does <code>self.response.out.write('OK')</code></p> <p>If I run the following Python at my computer:</p> <pre><code>import urllib2 f = ur...
49
2008-09-19T13:19:09Z
103,410
<p>thanks to Arachnid for the answer - it worked as suggested</p> <p>here is a simplified copy of the code, in case it is helpful to the next person to try! </p> <pre><code>import os import urllib import urllib2 import cookielib users_email_address = "[email protected]" users_password = "billybobspassword" t...
34
2008-09-19T16:22:06Z
[ "python", "web-services", "google-app-engine", "authentication" ]
How do you access an authenticated Google App Engine service from a (non-web) python client?
101,742
<p>I have a Google App Engine app - <a href="http://mylovelyapp.appspot.com/">http://mylovelyapp.appspot.com/</a> It has a page - mylovelypage</p> <p>For the moment, the page just does <code>self.response.out.write('OK')</code></p> <p>If I run the following Python at my computer:</p> <pre><code>import urllib2 f = ur...
49
2008-09-19T13:19:09Z
4,813,590
<p>for those who can't get ClientLogin to work, try app engine's <a href="http://code.google.com/appengine/docs/python/oauth/overview.html" rel="nofollow">OAuth support</a>.</p>
1
2011-01-27T06:59:18Z
[ "python", "web-services", "google-app-engine", "authentication" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
101,787
<p>Not at the moment and you would be lucky to get Jython to work soon. If you're planning to start your development now you would be better off with just sticking to Java for now on.</p>
20
2008-09-19T13:26:53Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
256,069
<p>As a <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> lover and Android programmer, I am sad to say this is not really a good way to go. There are two problems.</p> <p>One problem is that there is a lot more than just a programming language to the Android development tools. A lot ...
40
2008-11-01T20:29:44Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
383,473
<p>I just posted some <a href="http://www.damonkohler.com/2008/12/python-on-android.html">directions for cross compiling Python 2.4.5 for Android</a>. It takes some patching, and not all modules are supported, but the basics are there.</p>
37
2008-12-20T16:56:57Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
973,765
<p><a href="http://google-opensource.blogspot.com/2009/06/introducing-android-scripting.html">YES!</a></p> <p>An example <a href="http://www.mattcutts.com/blog/android-barcode-scanner/">via Matt Cutts</a> -- "here’s a barcode scanner written in six lines of Python code:</p> <pre><code>import android droid = android...
143
2009-06-10T05:13:13Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
973,786
<p>There is also the new <a href="http://www.talkandroid.com/1225-android-scripting-environment/">Android Scripting Environment</a> (ASE) project. It looks awesome, and it has some integration with native Android components. </p>
248
2009-06-10T05:24:29Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
4,381,935
<p>Check out the blog post <a href="http://www.saffirecorp.com/?p=113">http://www.saffirecorp.com/?p=113</a> that explains how to install and run <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> and a simple webserver written in Python on Android.</p>
16
2010-12-07T21:46:18Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
4,828,127
<p><em>"The <a href="http://www.renpy.org/pygame/">Pygame Subset for Android</a> is a port of a subset of Pygame functionality to the Android platform. The goal of the project is to allow the creation of Android-specific games, and to ease the porting of games from PC-like platforms to Android."</em></p> <p>The exampl...
54
2011-01-28T12:18:47Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
5,475,949
<p>There's also python-on-a-chip possibly running mosync: <a href="http://groups.google.com/group/python-on-a-chip/browse_thread/thread/df1c837bae2200f2/02992219b9c0003e?lnk=gst&amp;q=mosync#02992219b9c0003e" rel="nofollow">google group</a></p>
7
2011-03-29T16:42:06Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
6,136,305
<p>There's also <a href="https://github.com/damonkohler/sl4a" rel="nofollow">SL4A</a> written by a Google employee.</p>
54
2011-05-26T09:21:31Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
7,741,114
<p>I've posted instructions and a patch for cross compiling Python 2.7.2 for Android, you can get it at my blog here: <a href="http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/">http://mdqinc.com/blog/2011/09/cross-compiling-python-for-android/</a></p> <p>EDIT: I've open sourced <a href="http://ignifu...
50
2011-10-12T13:49:09Z
[ "python", "android", "ase", "android-scripting" ]
Is there a way to run Python on Android?
101,754
<p>We are working on an <a href="http://en.wikipedia.org/wiki/S60_%28software_platform%29">S60</a> version and this platform has a nice Python API. </p> <p>However, there is nothing official about Python on Android, but since <a href="http://en.wikipedia.org/wiki/Jython">Jython</a> exists, is there a way to let the sn...
1,360
2008-09-19T13:21:12Z
8,189,603
<p>One way is to use <a href="http://kivy.org/">Kivy</a>:</p> <blockquote> <p>Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.</p> </blockquote> <p></p> <blockquote> <p>Kivy runs on Linux, Windows, OS X, Android and iOS. You...
539
2011-11-18T21:49:45Z
[ "python", "android", "ase", "android-scripting" ]