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
How do you verify an RSA SHA1 signature in pyOpenSSL
1,471,037
<p>I tried installing M2Crypto and facing problems. I don't want to force my customers to use such libraries which are difficult to install. So, I thought I would give pyOpenSSL a try. I am able to get the public key from pem certificate but am not able to find any way to verify the signature.</p>
1
2009-09-24T11:16:52Z
1,506,756
<p>You simply can't do this with pyOpenSSL. It is a very limited wrapper around openssl library. But I have a guess what's wrong with building M2Crypto on your host. Try running:</p> <pre><code>export SWIG_FEATURES=-I/usr/include/openssl </code></pre> <p>before trying to install M2Crypto.</p>
0
2009-10-01T22:02:07Z
[ "python", "installation", "m2crypto", "pyopenssl" ]
How do you verify an RSA SHA1 signature in pyOpenSSL
1,471,037
<p>I tried installing M2Crypto and facing problems. I don't want to force my customers to use such libraries which are difficult to install. So, I thought I would give pyOpenSSL a try. I am able to get the public key from pem certificate but am not able to find any way to verify the signature.</p>
1
2009-09-24T11:16:52Z
17,179,006
<p>I believe that what you want is the code from this blog entry (it's mine):</p> <p><a href="http://www.v13.gr/blog/?p=303" rel="nofollow">http://www.v13.gr/blog/?p=303</a></p> <p>As you can see you have to use DER to handle the certificate and get the appropriate parts. That took me a considerable amount of effort ...
0
2013-06-18T21:11:10Z
[ "python", "installation", "m2crypto", "pyopenssl" ]
String similarity metrics in Python
1,471,153
<p>I want to find string similarity between two strings. <a href="http://en.wikipedia.org/wiki/Category%3aString_similarity_measures">This</a> page has examples of some of them. Python has an implemnetation of <a href="http://code.google.com/p/pylevenshtein/">Levenshtein algorithm</a>. Is there a better algorithm, (and...
28
2009-09-24T11:43:00Z
1,471,170
<p>Is that what you mean?</p> <pre><code>&gt;&gt;&gt; get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) ['apple', 'ape'] &gt;&gt;&gt; import keyword &gt;&gt;&gt; get_close_matches('wheel', keyword.kwlist) ['while'] &gt;&gt;&gt; get_close_matches('apple', keyword.kwlist) [] &gt;&gt;&gt; get_close_matches('...
3
2009-09-24T11:46:47Z
[ "python", "algorithm", "string", "levenshtein-distance" ]
String similarity metrics in Python
1,471,153
<p>I want to find string similarity between two strings. <a href="http://en.wikipedia.org/wiki/Category%3aString_similarity_measures">This</a> page has examples of some of them. Python has an implemnetation of <a href="http://code.google.com/p/pylevenshtein/">Levenshtein algorithm</a>. Is there a better algorithm, (and...
28
2009-09-24T11:43:00Z
1,471,303
<p>There's a great resource for string similarity metrics at the University of Sheffield. It has a list of various metrics (beyond just Levenshtein) and has open-source implementations of them. Looks like many of them should be easy to adapt into Python.</p> <p><a href="http://web.archive.org/web/20081224234350/http:/...
15
2009-09-24T12:13:28Z
[ "python", "algorithm", "string", "levenshtein-distance" ]
String similarity metrics in Python
1,471,153
<p>I want to find string similarity between two strings. <a href="http://en.wikipedia.org/wiki/Category%3aString_similarity_measures">This</a> page has examples of some of them. Python has an implemnetation of <a href="http://code.google.com/p/pylevenshtein/">Levenshtein algorithm</a>. Is there a better algorithm, (and...
28
2009-09-24T11:43:00Z
1,471,603
<p>I realize it's not the same thing, but this is close enough:</p> <pre><code>&gt;&gt;&gt; import difflib &gt;&gt;&gt; a = 'Hello, All you people' &gt;&gt;&gt; b = 'hello, all You peopl' &gt;&gt;&gt; seq=difflib.SequenceMatcher(a=a.lower(), b=b.lower()) &gt;&gt;&gt; seq.ratio() 0.97560975609756095 </code></pre> <p>Y...
63
2009-09-24T13:10:55Z
[ "python", "algorithm", "string", "levenshtein-distance" ]
String similarity metrics in Python
1,471,153
<p>I want to find string similarity between two strings. <a href="http://en.wikipedia.org/wiki/Category%3aString_similarity_measures">This</a> page has examples of some of them. Python has an implemnetation of <a href="http://code.google.com/p/pylevenshtein/">Levenshtein algorithm</a>. Is there a better algorithm, (and...
28
2009-09-24T11:43:00Z
1,472,165
<p>I would use Levenshtein distance, or the so-called Damerau distance (which takes transpositions into account) rather than the difflib stuff for two reasons (1) "fast enough" (dynamic programming algo) and "whoooosh" (bit-bashing) C code is available and (2) well-understood behaviour e.g. Levenshtein satisfies the tr...
5
2009-09-24T14:41:31Z
[ "python", "algorithm", "string", "levenshtein-distance" ]
String similarity metrics in Python
1,471,153
<p>I want to find string similarity between two strings. <a href="http://en.wikipedia.org/wiki/Category%3aString_similarity_measures">This</a> page has examples of some of them. Python has an implemnetation of <a href="http://code.google.com/p/pylevenshtein/">Levenshtein algorithm</a>. Is there a better algorithm, (and...
28
2009-09-24T11:43:00Z
26,894,959
<p>I know this isn't the same but you can adjust the ratio to filter out strings that are not similar enough and return the closest match to the string you are looking for.</p> <p>Perhaps you would be more interested in semantic similarity metrics.</p> <p><a href="https://www.google.com/search?client=ubuntu&amp;chann...
1
2014-11-12T19:28:21Z
[ "python", "algorithm", "string", "levenshtein-distance" ]
String similarity metrics in Python
1,471,153
<p>I want to find string similarity between two strings. <a href="http://en.wikipedia.org/wiki/Category%3aString_similarity_measures">This</a> page has examples of some of them. Python has an implemnetation of <a href="http://code.google.com/p/pylevenshtein/">Levenshtein algorithm</a>. Is there a better algorithm, (and...
28
2009-09-24T11:43:00Z
31,236,472
<p>This snippet will calculate the difflib, Levenshtein, Sørensen, and Jaccard similarity values for two strings. In the snippet below, I was iterating over a tsv in which the strings of interest occupied columns <code>[3]</code> and <code>[4]</code> of the tsv. (<code>pip install python-Levenshtein</code> and <code>p...
5
2015-07-06T00:51:12Z
[ "python", "algorithm", "string", "levenshtein-distance" ]
How do I determine the proper `paramstyle` when all I have is a `Connection` object?
1,471,304
<p>I have an instance of a <code>Connection</code> (required to DB API 2.0-compliant), but I don't have the module from which it was imported. The problem is that I am trying to use named parameters, but I don't know which <code>paramstyle</code> to use.</p> <p>Since <code>paramstyle</code> is a module-level constant,...
2
2009-09-24T12:14:04Z
1,471,348
<p>Where did you get the instance from? I can't imagine a situation where you won't know the beforehand the source of the connection. If the user of your library is passing you a connection, ask him for the paramstyle as well.</p> <p>Anyway, look at the following console session:</p> <pre><code>&gt;&gt;&gt; import sq...
2
2009-09-24T12:22:11Z
[ "python", "db-api-2" ]
How do I determine the proper `paramstyle` when all I have is a `Connection` object?
1,471,304
<p>I have an instance of a <code>Connection</code> (required to DB API 2.0-compliant), but I don't have the module from which it was imported. The problem is that I am trying to use named parameters, but I don't know which <code>paramstyle</code> to use.</p> <p>Since <code>paramstyle</code> is a module-level constant,...
2
2009-09-24T12:14:04Z
1,471,359
<p>Pass <code>type(connection)</code> (connection class) to <code>inspect.getmodule()</code>, not connection object. The class tracks the module it was defined in so <code>inspect</code> can find it, while object creation is not tracked.</p>
2
2009-09-24T12:24:34Z
[ "python", "db-api-2" ]
How do I determine the proper `paramstyle` when all I have is a `Connection` object?
1,471,304
<p>I have an instance of a <code>Connection</code> (required to DB API 2.0-compliant), but I don't have the module from which it was imported. The problem is that I am trying to use named parameters, but I don't know which <code>paramstyle</code> to use.</p> <p>Since <code>paramstyle</code> is a module-level constant,...
2
2009-09-24T12:14:04Z
1,471,428
<p>You can't.</p> <p>You can try by looking at <code>connection.__class__.__module__</code>, but it's not specified by DB-API that it'll work. In fact for many common cases it won't. (eg. the class is defined in a submodule of the package that acts as the DB-API module object; or the class is a C extension with no <c...
2
2009-09-24T12:37:57Z
[ "python", "db-api-2" ]
How should I handle software packages?
1,471,567
<p>I am trying to install pysqlite and have troubles with that. I found out that the most probable reason of that is missing sqlite headers and I have to install them. My platform: CentOS release 5.3 (Final). I have Python-2.6.2.</p> <p>I also found out that I need .rpm files. As far as I have them I execute:</p> <pr...
1
2009-09-24T13:05:49Z
1,471,595
<p>Python 2.6 (and some earlier) include sqlite <a href="http://file%3A///Users/mark/Public/docs/python-2.6.2-docs-html/library/sqlite3.html" rel="nofollow">Python org library ref</a> so you should not need to do this. Just import it and run</p>
3
2009-09-24T13:09:18Z
[ "python", "sqlite", "rpm", "pysqlite" ]
How should I handle software packages?
1,471,567
<p>I am trying to install pysqlite and have troubles with that. I found out that the most probable reason of that is missing sqlite headers and I have to install them. My platform: CentOS release 5.3 (Final). I have Python-2.6.2.</p> <p>I also found out that I need .rpm files. As far as I have them I execute:</p> <pr...
1
2009-09-24T13:05:49Z
1,472,838
<p>Typically, you should install the python <code>sqlite</code> module through <code>yum</code>, something like:</p> <pre><code>yum install python-sqlite </code></pre> <p>and then edit your code changing <code>sqlite2</code> references to <code>sqlite3</code>.</p> <p>By the way, whenever you read directions to insta...
1
2009-09-24T16:33:45Z
[ "python", "sqlite", "rpm", "pysqlite" ]
How should I handle software packages?
1,471,567
<p>I am trying to install pysqlite and have troubles with that. I found out that the most probable reason of that is missing sqlite headers and I have to install them. My platform: CentOS release 5.3 (Final). I have Python-2.6.2.</p> <p>I also found out that I need .rpm files. As far as I have them I execute:</p> <pr...
1
2009-09-24T13:05:49Z
1,473,910
<p>You can use buildout to create localized version of your project. This will install all necessary packages without having sudo access to the server.</p> <p>To give it try, do the following:</p> <pre><code>mkdir tmp cd tmp wget http://svn.zope.org/*checkout*/zc.buildout/trunk/bootstrap/bootstrap.py python bootstrap...
2
2009-09-24T20:17:23Z
[ "python", "sqlite", "rpm", "pysqlite" ]
making HTTP authentication optional with mod-python
1,471,602
<p>I've a web application that accesses multiple controller classes based on the parameters it is passed. For some of the controllers, I want users to authenticate themselves (by simple HTTP authentication), and for some I want public access.</p> <p>Is there a way to make this happen? In my .htaccess file, I now have<...
1
2009-09-24T13:10:42Z
6,150,509
<p>There are a couple ways to specify authentication scope with Apache, the one most people are used to is gt;Directorylt; based - i.e. anything in or below a directory gets authenticated against htpasswd.</p> <p>There's also gt;Locationlt;, which applies directives to content that live outside the filesystem such as ...
1
2011-05-27T09:46:44Z
[ "python", "apache", "authentication", "mod-python" ]
Copying modules into Django, "No module named [moduleName]"
1,471,707
<p>I run into this problem pretty consistently... keep in mind I am quite new to Django and a total Python amateur.</p> <p>It seems that, for example, whenever I check out my Django project on a new computer after a clean install of Python and Django, it can never find the project/apps I create or copy in.</p> <p>So ...
0
2009-09-24T13:32:10Z
1,471,945
<p>Your third party django module should be searchable by PYTHONPATH, because django module is no other than a python module. Now there are two ways to do this:</p> <ol> <li><p>Create a folder (anywhere you want), put your third party django module under there. Now set that directory to environment variable $PYTHONPAT...
0
2009-09-24T14:10:21Z
[ "python", "django" ]
Copying modules into Django, "No module named [moduleName]"
1,471,707
<p>I run into this problem pretty consistently... keep in mind I am quite new to Django and a total Python amateur.</p> <p>It seems that, for example, whenever I check out my Django project on a new computer after a clean install of Python and Django, it can never find the project/apps I create or copy in.</p> <p>So ...
0
2009-09-24T13:32:10Z
1,472,013
<p>They way Django works is pretty much how Python works. At default the folder you create when you run django-admin.py startproject name is added to your python path. That means that anything you put into there you can get to. But you have to mind that when you write the app into the installed app list. If you have an...
2
2009-09-24T14:19:31Z
[ "python", "django" ]
Small Tables in Python?
1,471,924
<p>Let's say I don't have more than one or two dozen objects with different properties, such as the following:</p> <p>UID, Name, Value, Color, Type, Location</p> <p>I want to be able to call up all objects with Location = "Boston", or Type = "Primary". Classic database query type stuff.</p> <p>Most table solutions ...
12
2009-09-24T14:07:17Z
1,472,025
<p>For small relational problems I love using Python's builtin <a href="http://docs.python.org/library/sets.html">sets</a>.</p> <p>For the example of location = 'Boston' OR type = 'Primary', if you had this data:</p> <pre><code>users = { 1: dict(Name="Mr. Foo", Location="Boston", Type="Secondary"), 2: dict(Name...
13
2009-09-24T14:21:30Z
[ "python", "table", "lookup-tables" ]
Small Tables in Python?
1,471,924
<p>Let's say I don't have more than one or two dozen objects with different properties, such as the following:</p> <p>UID, Name, Value, Color, Type, Location</p> <p>I want to be able to call up all objects with Location = "Boston", or Type = "Primary". Classic database query type stuff.</p> <p>Most table solutions ...
12
2009-09-24T14:07:17Z
1,472,092
<p>If it's really a small amount of data, I'd not bother with an index and probably just write a helper function:</p> <pre><code>users = [ dict(Name="Mr. Foo", Location="Boston", Type="Secondary"), dict(Name="Mr. Bar", Location="New York", Type="Primary"), dict(Name="Mr. Quux", Location="Chicago", Type="Secon...
2
2009-09-24T14:31:39Z
[ "python", "table", "lookup-tables" ]
Small Tables in Python?
1,471,924
<p>Let's say I don't have more than one or two dozen objects with different properties, such as the following:</p> <p>UID, Name, Value, Color, Type, Location</p> <p>I want to be able to call up all objects with Location = "Boston", or Type = "Primary". Classic database query type stuff.</p> <p>Most table solutions ...
12
2009-09-24T14:07:17Z
1,472,343
<p>I do not think sqlite would be "overkill" -- it comes with standard Python since 2.5, so no need to install stuff, and it can make and handle databases in either memory or local disk files. Really, how could it be simpler...? If you want everything in-memory including the initial values, and want to use dicts to exp...
5
2009-09-24T15:11:08Z
[ "python", "table", "lookup-tables" ]
How do I parse an HTTP date-string in Python?
1,471,987
<p>Is there an easy way to parse HTTP date-strings in Python? According to <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3">the standard</a>, there are several ways to format HTTP date strings; the method should be able to handle this.</p> <p>In other words, I want to convert a string like "Wed, ...
27
2009-09-24T14:15:39Z
1,472,008
<pre><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; datetime.datetime.strptime('Wed, 23 Sep 2009 22:15:29 GMT', '%a, %d %b %Y %H:%M:%S GMT') datetime.datetime(2009, 9, 23, 22, 15, 29) </code></pre>
1
2009-09-24T14:18:53Z
[ "python", "http", "datetime", "parsing" ]
How do I parse an HTTP date-string in Python?
1,471,987
<p>Is there an easy way to parse HTTP date-strings in Python? According to <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3">the standard</a>, there are several ways to format HTTP date strings; the method should be able to handle this.</p> <p>In other words, I want to convert a string like "Wed, ...
27
2009-09-24T14:15:39Z
1,472,336
<pre><code>&gt;&gt;&gt; import email.utils as eut &gt;&gt;&gt; eut.parsedate('Wed, 23 Sep 2009 22:15:29 GMT') (2009, 9, 23, 22, 15, 29, 0, 1, -1) </code></pre> <p>If you want a <code>datetime.datetime</code> object, you can do:</p> <pre><code>def my_parsedate(text): return datetime.datetime(*eut.parsedate(text)[:...
35
2009-09-24T15:10:31Z
[ "python", "http", "datetime", "parsing" ]
How do I parse an HTTP date-string in Python?
1,471,987
<p>Is there an easy way to parse HTTP date-strings in Python? According to <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3">the standard</a>, there are several ways to format HTTP date strings; the method should be able to handle this.</p> <p>In other words, I want to convert a string like "Wed, ...
27
2009-09-24T14:15:39Z
21,888,767
<pre><code>httplib.HTTPMessage(filehandle).getdate(headername) httplib.HTTPMessage(filehandle).getdate_tz(headername) mimetools.Message(filehandle).getdate() rfc822.parsedate(datestr) rfc822.parsedate_tz(datestr) </code></pre> <ul> <li>if you have a raw data stream, you can build an HTTPMessage or a mimetools.Message ...
1
2014-02-19T18:07:52Z
[ "python", "http", "datetime", "parsing" ]
What is setup.py?
1,471,994
<p>Can anyone please explain, what is <code>setup.py</code> and how can it be configured or used?</p>
285
2009-09-24T14:16:52Z
1,472,014
<p>setup.py is a python file, which usually tells you that the module/package you are about to install have been packaged and distributed with Distutils, which is the standard for distributing Python Modules.</p> <p>This allows you to easily install Python packages, often it's enough to write:</p> <pre><code>python s...
202
2009-09-24T14:19:35Z
[ "python", "setup.py", "pypi", "python-packaging" ]
What is setup.py?
1,471,994
<p>Can anyone please explain, what is <code>setup.py</code> and how can it be configured or used?</p>
285
2009-09-24T14:16:52Z
1,472,029
<p>If you downloaded package that has "setup.py" in root folder, you can install it by running</p> <pre><code>python setup.py install </code></pre> <p>If you are developing a project and are wondering what this file is useful for, check <a href="http://docs.python.org/distutils/setupscript.html">Python documentation ...
36
2009-09-24T14:21:46Z
[ "python", "setup.py", "pypi", "python-packaging" ]
What is setup.py?
1,471,994
<p>Can anyone please explain, what is <code>setup.py</code> and how can it be configured or used?</p>
285
2009-09-24T14:16:52Z
1,472,031
<p><code>setup.py</code> is a Python script that is usually shipped with libraries or programs, written in that language. It's purpose is the correct installation of the software.</p> <p>Many packages use the <code>distutils</code> framework in conjuction with <code>setup.py</code>.</p> <p><a href="http://docs.python...
14
2009-09-24T14:21:58Z
[ "python", "setup.py", "pypi", "python-packaging" ]
What is setup.py?
1,471,994
<p>Can anyone please explain, what is <code>setup.py</code> and how can it be configured or used?</p>
285
2009-09-24T14:16:52Z
1,472,439
<p><code>setup.py</code> is Python's answer to a multi-platform installer and <code>make</code> file. </p> <p>If you’re familiar with command line installations, then <code>make &amp;&amp; make install</code> translates to <code>python setup.py build &amp;&amp; python setup.py install</code>. </p> <p>Some packages ...
43
2009-09-24T15:29:09Z
[ "python", "setup.py", "pypi", "python-packaging" ]
What is setup.py?
1,471,994
<p>Can anyone please explain, what is <code>setup.py</code> and how can it be configured or used?</p>
285
2009-09-24T14:16:52Z
16,088,409
<p>setup.py can be used in two scenarios , First, you want to install a Python package. Second, you want to create your own Python package. Usually standard Python package has couple of important files like setup.py, setup.cfg and Manifest.in. When you are creating the Python package, these three files will determine t...
9
2013-04-18T16:29:38Z
[ "python", "setup.py", "pypi", "python-packaging" ]
What is setup.py?
1,471,994
<p>Can anyone please explain, what is <code>setup.py</code> and how can it be configured or used?</p>
285
2009-09-24T14:16:52Z
18,870,208
<p>When you download a package with <code>setup.py</code> open your Terminal (Mac,Linux) or Command Prompt (Windows). Using <code>cd</code> and helping you with Tab button set the path right to the folder where you have downloaded the file and where there is <code>setup.py</code> :</p> <pre><code>iMac:~ user $ cd pat...
4
2013-09-18T10:49:14Z
[ "python", "setup.py", "pypi", "python-packaging" ]
What is setup.py?
1,471,994
<p>Can anyone please explain, what is <code>setup.py</code> and how can it be configured or used?</p>
285
2009-09-24T14:16:52Z
23,998,536
<p>To install a Python package you've downloaded, you extract the archive and run the setup.py script inside:</p> <pre><code>python setup.py install </code></pre> <p>To me, this has always felt odd. It would be more natural to point a package manager at the download, as one would do in Ruby and Nodejs, eg. <code>gem ...
5
2014-06-02T15:52:36Z
[ "python", "setup.py", "pypi", "python-packaging" ]
What is setup.py?
1,471,994
<p>Can anyone please explain, what is <code>setup.py</code> and how can it be configured or used?</p>
285
2009-09-24T14:16:52Z
39,811,884
<p>It helps to install a python package <code>foo</code> on your machine (can also be in <code>virtualenv</code>) so that you can import the package <code>foo</code> from other projects and also from Python prompt. </p> <p><strong><em>It does the similar job of <code>pip</code>, <code>easy_install</code> etc.,</em></...
2
2016-10-01T22:04:48Z
[ "python", "setup.py", "pypi", "python-packaging" ]
PyQt4 Highlighting
1,472,044
<p>I'm trying to add some syntax highlighting to a text editor in PyQt4. I've found an example in the documentation which works find when compiled from C++ but when i convert it to Python/PyQt it no longer works.</p> <p>The part of the code that fails (no longer highlights anything) is:</p> <pre><code>def highlightCu...
2
2009-09-24T14:24:48Z
1,472,288
<p>Save <code>lineColor</code> somewhere (like <code>self.lineColor</code>). Otherwise, Python will discard the object when the method returns and the <code>format</code> will use an illegal pointer.</p>
0
2009-09-24T15:02:02Z
[ "python", "qt4", "pyqt4" ]
PyQt4 Highlighting
1,472,044
<p>I'm trying to add some syntax highlighting to a text editor in PyQt4. I've found an example in the documentation which works find when compiled from C++ but when i convert it to Python/PyQt it no longer works.</p> <p>The part of the code that fails (no longer highlights anything) is:</p> <pre><code>def highlightCu...
2
2009-09-24T14:24:48Z
1,505,829
<p>Ok... turns out i wasn't going mad, i was just using an out of date version of PyQt4.</p> <p>For information the version of PyQt4 that ships with Ubuntu 9.04 is 4.4.4 but this functionality seems to require 4.5+.</p> <p>I've upgraded to PyQt4 4.6 and it works fine (plus 4.6 seems to have some nice new functionalit...
1
2009-10-01T19:02:20Z
[ "python", "qt4", "pyqt4" ]
regex for triple quote
1,472,047
<p>What regex will find the triple quote comments (possibly multi-line) in a Python source code?</p>
3
2009-09-24T14:26:01Z
1,472,080
<pre><code>re.findall('(?:\n[\t ]*)\"{3}(.*?)\"{3}', s, re.M | re.S) </code></pre> <p>captures only text within triple quotes that are at the begging of a line and could be preceded by spaces, tabs or nothing, as python docstrings should be.</p>
5
2009-09-24T14:29:57Z
[ "python", "regex" ]
regex for triple quote
1,472,047
<p>What regex will find the triple quote comments (possibly multi-line) in a Python source code?</p>
3
2009-09-24T14:26:01Z
1,472,390
<p>Python is not a regular language and cannot reliably be parsed using regex.</p> <p>If you want a proper Python parser, look at the <a href="http://docs.python.org/library/ast.html" rel="nofollow">ast</a> module. You may be looking for <code>get_docstring</code>.</p>
10
2009-09-24T15:20:41Z
[ "python", "regex" ]
regex for triple quote
1,472,047
<p>What regex will find the triple quote comments (possibly multi-line) in a Python source code?</p>
3
2009-09-24T14:26:01Z
1,491,525
<p>I've found this one from Tim Peters (I think) :</p> <pre><code>pat = """ qqq [^\\q]* ( ( \\\\[\000-\377] | q ( \\\\[\000-\377] | [^\\q] | q ( \\\\[\000-\377] | [^\\q] ) ) ) [^\\q]* )* qqq """ pat = ''...
0
2009-09-29T09:36:13Z
[ "python", "regex" ]
regex for triple quote
1,472,047
<p>What regex will find the triple quote comments (possibly multi-line) in a Python source code?</p>
3
2009-09-24T14:26:01Z
8,798,548
<p>I find this to be working perfectly for me (used it with TextMate):</p> <pre><code>"{3}([\s\S]*?"{3}) </code></pre> <p>I wanted to remove all comments from a library and this took care of the triple-quote comments (single or multi-line, regardless of where they started on the line).</p> <p>For hash comments (much...
2
2012-01-10T04:25:57Z
[ "python", "regex" ]
What is the Python equivalent of application & session scope variables?
1,472,279
<p>Recently started on python, wondered what the equivalent object was for storing session &amp; application scope data?</p> <p>I'm using Google App Engine too so if it has any extra features (can't seem to find any immediate references myself) that would be useful</p>
0
2009-09-24T14:59:59Z
1,472,345
<p>I assume you're talking about a web session, used to retain state between http requests. Python is a general programming language and by itself doesn't contain the concept of a session. </p> <p>However most different python web frameworks have session implementations. Which one are you using? I've linked to the ses...
0
2009-09-24T15:11:32Z
[ "python", "session", "scope", "web-applications" ]
How can I link against libpython.a such the runtime linker can find all the symbols in libpython.a?
1,472,828
<p>In a sequel question to <a href="http://stackoverflow.com/questions/1469370/why-cant-i-import-the-math-library-when-embedding-python-in-c">this question</a>, my corporate environment lacks the <code>libpython2.6.so</code> shared object but has the <code>libpython2.6.a</code> file. Is there a way that I can compile ...
0
2009-09-24T16:32:25Z
1,472,853
<p>You need to pass <code>--export-dynamic</code> to the linker. So from <code>g++</code> it's...</p> <pre><code>g++ -Wl,--export-dynamic ... </code></pre>
2
2009-09-24T16:36:35Z
[ "python", "gcc", "g++" ]
Why is host aborting connection?
1,472,876
<p>I'm teaching myself Python networking, and I recalled that back when I was teaching myself threading, I came across <a href="http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/">this page</a>, so I copied the scripts, updated them for Python 3.1.1 and ran them. They worked perfectly.</p> <p>Then I made ...
7
2009-09-24T16:44:28Z
1,474,829
<p>That's not how you learn python networking programming. Nobody doing anything serious will ever program like that, so you're learning how to NOT do python networking programming.</p> <ol> <li>Sockets are too low-level, and python is a high-level language. Then, to do python network programming, in a pythonic way, y...
-11
2009-09-25T00:22:22Z
[ "python", "networking" ]
Why is host aborting connection?
1,472,876
<p>I'm teaching myself Python networking, and I recalled that back when I was teaching myself threading, I came across <a href="http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/">this page</a>, so I copied the scripts, updated them for Python 3.1.1 and ran them. They worked perfectly.</p> <p>Then I made ...
7
2009-09-24T16:44:28Z
1,475,855
<p>Your client is now correct - you want to open the socket send the data, receive the reply and then close the socket.</p> <p>The error original error was caused by the server closing the socket after it sent the first response which caused the client to receive a connection closed message when it tried to send the s...
3
2009-09-25T07:13:47Z
[ "python", "networking" ]
select_related does not join columns marked with nulll=True
1,472,974
<p>I have a Django model -</p> <pre><code>class NoticedUser(models.Model): user = models.ForeignKey(User, null=False) text = models.CharField(max_length=255, null=True) photo = models.ForeignKey(Photo, null=True, blank=True) article = models.ForeignKey(Article, null=True, blank=True) date = models....
2
2009-09-24T17:02:10Z
1,473,114
<p>It doesn't follow these relations by default when using <code>select_related()</code> with no parameters. You have to explicitly specify the names:</p> <pre><code>NoticedUser.objects.all().select_related('article', 'photo') </code></pre>
4
2009-09-24T17:34:50Z
[ "python", "django" ]
open source data mining/text analysis tools in python
1,473,087
<p>I have a database full of reviews of various products. My task is to perform various calculation and "create" another "database/xml-export" with aggregated data. I am thinking of writing command line programs in python to do that. But I know someone have done this before and I know that there is some open source pyt...
2
2009-09-24T17:29:29Z
1,473,123
<p>What kind of analysis are you trying to do?</p> <p>If you're analyzing text take a look at the <a href="http://www.nltk.org/" rel="nofollow">Natural Language Toolkit</a> (NLTK).</p> <p>If you want to index and search the data, take a look at the <a href="http://whoosh.ca/" rel="nofollow">whoosh</a> search engine.<...
1
2009-09-24T17:36:31Z
[ "python", "database", "data-mining", "analyzer" ]
open source data mining/text analysis tools in python
1,473,087
<p>I have a database full of reviews of various products. My task is to perform various calculation and "create" another "database/xml-export" with aggregated data. I am thinking of writing command line programs in python to do that. But I know someone have done this before and I know that there is some open source pyt...
2
2009-09-24T17:29:29Z
1,473,267
<p>Looks like you are looking for a <strong>Data Integration</strong> solution.<br> One suggestion is the open source <a href="http://kettle.pentaho.org/" rel="nofollow">Kettle project</a> part of the <a href="http://www.pentaho.com/products/data%5Fintegration/" rel="nofollow">Pentaho</a> suite.<br> For python, a quick...
1
2009-09-24T18:06:24Z
[ "python", "database", "data-mining", "analyzer" ]
Django ORM - assigning a raw value to DecimalField
1,473,332
<p>EDIT!!! - The casting of the value to a string seems to work fine when I create a <em>new</em> object, but when I try to edit an existing object, it does not allow it.</p> <p>So I have a decimal field in one of my models of Decimal(3,2)</p> <p>When I query up all these objects and try to set this field:</p> <pre>...
1
2009-09-24T18:20:58Z
1,473,442
<p>The Django <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#decimalfield" rel="nofollow">DecimalField</a> is "...represented in by a python <a href="http://docs.python.org/library/decimal.html" rel="nofollow">Decimal</a> instance." You might try:</p> <pre><code>&gt;&gt;&gt; obj.fieldName = Decimal(...
1
2009-09-24T18:39:29Z
[ "python", "django", "django-models", "casting", "django-orm" ]
Django ORM - assigning a raw value to DecimalField
1,473,332
<p>EDIT!!! - The casting of the value to a string seems to work fine when I create a <em>new</em> object, but when I try to edit an existing object, it does not allow it.</p> <p>So I have a decimal field in one of my models of Decimal(3,2)</p> <p>When I query up all these objects and try to set this field:</p> <pre>...
1
2009-09-24T18:20:58Z
1,474,216
<pre><code>from decimal import Decimal object.fieldName = Decimal("0.85") </code></pre> <p>or</p> <pre><code>f = 0.85 object.fieldName = Decimal(str(f)) </code></pre>
6
2009-09-24T21:20:21Z
[ "python", "django", "django-models", "casting", "django-orm" ]
How to create a hardlink on attached Volumes on Mac?
1,473,368
<p>os.link is not working for the attached Volumes on Mac.</p> <pre><code>~ $ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import os &gt;&gt;&gt; os.link("/Volumes/ARC...
0
2009-09-24T18:28:04Z
1,473,394
<p>What filesystem in on ARCHANA? And are you trying to link to a directory? Not all file systems support hardlinks, and very few support hardlinks to directories.</p> <p>In particular USB mass-storage devices are generally formatted as with FAT filesystems which do not support links.</p>
1
2009-09-24T18:32:15Z
[ "python", "osx" ]
How to create a hardlink on attached Volumes on Mac?
1,473,368
<p>os.link is not working for the attached Volumes on Mac.</p> <pre><code>~ $ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import os &gt;&gt;&gt; os.link("/Volumes/ARC...
0
2009-09-24T18:28:04Z
1,473,431
<p>You're working on a mac, yet the volume ARCHANA might not have a link-able file system. (The uppercase label makes it suspicious.)</p> <p>Also, you are trying to refer a hard link to a directory and "Hard links may not normally refer to directories and may not span file systems." (from the man page.)</p> <p>One l...
2
2009-09-24T18:37:56Z
[ "python", "osx" ]
django view function code runs after return
1,473,543
<p>I have a django view that looks like...</p> <pre> def add_user(request): if User.objects.get(username__exact = request.POST['username']): context = { 'message': "Username already taken"} return render_to_response("mytemplate.html", context, RequestContext(request)) newUs...
0
2009-09-24T18:56:09Z
1,473,571
<p>The return statement will indeed terminate the function. So if you see other code being executed, you either</p> <ul> <li>don't execute the return statement, and thus produce the output somehow differently, or</li> <li>have other code (before the function is called, or in a middleware) that makes the database chang...
1
2009-09-24T19:01:50Z
[ "python", "django", "views" ]
django view function code runs after return
1,473,543
<p>I have a django view that looks like...</p> <pre> def add_user(request): if User.objects.get(username__exact = request.POST['username']): context = { 'message': "Username already taken"} return render_to_response("mytemplate.html", context, RequestContext(request)) newUs...
0
2009-09-24T18:56:09Z
1,473,743
<p>You are correct, assuming your conditions are met, the view will exit on your return statement. The only other thing I can think of that hasn't already been mentioned is indentation -- double-check that you do not have a mix of tabs and spaces. That can sometimes result in the unexpected.</p>
0
2009-09-24T19:35:33Z
[ "python", "django", "views" ]
Writing unicode strings via sys.stdout in Python
1,473,577
<p>Assume for a moment that one cannot use <code>print</code> (and thus enjoy the benefit of automatic encoding detection). So that leaves us with <code>sys.stdout</code>. However, <code>sys.stdout</code> is so dumb as to <a href="http://bugs.python.org/issue4947" rel="nofollow">not do any sensible encoding</a>.</p> <...
13
2009-09-24T19:02:35Z
1,473,764
<p>It's not clear to my why you wouldn't be able to do print; but assuming so, yes, the approach looks right to me.</p>
3
2009-09-24T19:40:07Z
[ "python", "unicode", "osx", "terminal", "stdout" ]
Writing unicode strings via sys.stdout in Python
1,473,577
<p>Assume for a moment that one cannot use <code>print</code> (and thus enjoy the benefit of automatic encoding detection). So that leaves us with <code>sys.stdout</code>. However, <code>sys.stdout</code> is so dumb as to <a href="http://bugs.python.org/issue4947" rel="nofollow">not do any sensible encoding</a>.</p> <...
13
2009-09-24T19:02:35Z
1,475,202
<p>Best idea is to check if you are directly connected to a terminal. If you are, use the terminal's encoding. Otherwise, use system preferred encoding. </p> <pre><code>if sys.stdout.isatty(): default_encoding = sys.stdout.encoding else: default_encoding = locale.getpreferredencoding() </code></pre> <p>It's a...
9
2009-09-25T02:55:36Z
[ "python", "unicode", "osx", "terminal", "stdout" ]
Writing unicode strings via sys.stdout in Python
1,473,577
<p>Assume for a moment that one cannot use <code>print</code> (and thus enjoy the benefit of automatic encoding detection). So that leaves us with <code>sys.stdout</code>. However, <code>sys.stdout</code> is so dumb as to <a href="http://bugs.python.org/issue4947" rel="nofollow">not do any sensible encoding</a>.</p> <...
13
2009-09-24T19:02:35Z
4,027,905
<p>There is an optional environment variable "PYTHONIOENCODING" which may be set to a desired default encoding. It would be one way of grabbing the user-desired encoding in a way consistent with all of Python. It is buried in the Python manual <a href="http://docs.python.org/release/3.1.2/using/cmdline.html?highlight=p...
8
2010-10-26T20:50:59Z
[ "python", "unicode", "osx", "terminal", "stdout" ]
Writing unicode strings via sys.stdout in Python
1,473,577
<p>Assume for a moment that one cannot use <code>print</code> (and thus enjoy the benefit of automatic encoding detection). So that leaves us with <code>sys.stdout</code>. However, <code>sys.stdout</code> is so dumb as to <a href="http://bugs.python.org/issue4947" rel="nofollow">not do any sensible encoding</a>.</p> <...
13
2009-09-24T19:02:35Z
6,361,471
<pre><code>export PYTHONIOENCODING=utf-8 </code></pre> <p>will do the job, but can't set it on python itself ... </p> <p>what we can do is verify if isn't setting and tell the user to set it before call script with :</p> <pre><code>if __name__ == '__main__': if (sys.stdout.encoding is None): print &gt;&g...
23
2011-06-15T17:04:06Z
[ "python", "unicode", "osx", "terminal", "stdout" ]
Writing unicode strings via sys.stdout in Python
1,473,577
<p>Assume for a moment that one cannot use <code>print</code> (and thus enjoy the benefit of automatic encoding detection). So that leaves us with <code>sys.stdout</code>. However, <code>sys.stdout</code> is so dumb as to <a href="http://bugs.python.org/issue4947" rel="nofollow">not do any sensible encoding</a>.</p> <...
13
2009-09-24T19:02:35Z
13,703,054
<p>This is what I am doing in my application:</p> <p><code>sys.stdout.write(s.encode('utf-8'))</code></p> <p>This is the exact opposite fix for reading UTF-8 names from argv:</p> <pre><code>for file in sys.argv[1:]: file = file.decode('utf-8') </code></pre> <p>This is very ugly (IMHO) as it force you to work wi...
5
2012-12-04T12:36:40Z
[ "python", "unicode", "osx", "terminal", "stdout" ]
python multiprocessing manager & composite pattern sharing
1,473,625
<p>I'm trying to share a composite structure through a multiprocessing manager but I felt in trouble with a "<strong>RuntimeError: maximum recursion depth exceeded</strong>" when trying to use just one of the Composite class methods.</p> <p>The class is token from <a href="http://code.activestate.com/recipes/498249/#c...
1
2009-09-24T19:14:06Z
1,473,700
<p>Python has a default maximum recursion depth of 1000 (or 999, I forget...). But you can change the default behavior thusly:</p> <pre><code>import sys sys.setrecursionlimit(n) </code></pre> <p>Where <code>n</code> is the number of recursions you wish to allow. </p> <p><strong>Edit:</strong></p> <p>The above answ...
0
2009-09-24T19:27:23Z
[ "python", "multiprocessing", "manager" ]
python multiprocessing manager & composite pattern sharing
1,473,625
<p>I'm trying to share a composite structure through a multiprocessing manager but I felt in trouble with a "<strong>RuntimeError: maximum recursion depth exceeded</strong>" when trying to use just one of the Composite class methods.</p> <p>The class is token from <a href="http://code.activestate.com/recipes/498249/#c...
1
2009-09-24T19:14:06Z
1,474,232
<p>Is it possible there is a circular reference between the classes? For example, the outer class has a reference to the composite class, and the composite class has a reference back to the outer class.</p> <p>The multiprocessing manager works well, but when you have large, complicated class structures, then you are l...
0
2009-09-24T21:23:40Z
[ "python", "multiprocessing", "manager" ]
python multiprocessing manager & composite pattern sharing
1,473,625
<p>I'm trying to share a composite structure through a multiprocessing manager but I felt in trouble with a "<strong>RuntimeError: maximum recursion depth exceeded</strong>" when trying to use just one of the Composite class methods.</p> <p>The class is token from <a href="http://code.activestate.com/recipes/498249/#c...
1
2009-09-24T19:14:06Z
1,476,681
<p>I think the problem is that you have to instruct the Manager on how to manage you object, which is not a standard python type.</p> <p>In other worlds <strong>you have to create a proxy for you CompositeDict</strong></p> <p>You could look at this doc for an example: <a href="http://ruffus.googlecode.com/svn/trunk/d...
0
2009-09-25T11:05:40Z
[ "python", "multiprocessing", "manager" ]
Raising ValidationError from django model's save method?
1,473,888
<p>I need to raise an exception in a model's save method. I'm hoping that an exception exists that will be caught by any django <code>ModelForm</code> that uses this model including the admin forms. </p> <p>I tried raising <code>django.forms.ValidationError</code>, but this seems to be uncaught by the admin forms. The...
10
2009-09-24T20:13:16Z
1,473,979
<p>There's currently no way of performing validation in model save methods. This is however being developed, as a separate model-validation branch, and should be merged into trunk in the next few months.</p> <p>In the meantime, you need to do the validation at the form level. It's quite simple to create a <code>ModelF...
8
2009-09-24T20:29:42Z
[ "python", "django" ]
Raising ValidationError from django model's save method?
1,473,888
<p>I need to raise an exception in a model's save method. I'm hoping that an exception exists that will be caught by any django <code>ModelForm</code> that uses this model including the admin forms. </p> <p>I tried raising <code>django.forms.ValidationError</code>, but this seems to be uncaught by the admin forms. The...
10
2009-09-24T20:13:16Z
12,356,526
<p>Since Django 1.2, this is what I've been doing:</p> <pre><code>class MyModel(models.Model): &lt;...model fields...&gt; def clean(self, *args, **kwargs): if &lt;some constraint not met&gt;: raise ValidationError('You have not met a constraint!') super(MyModel, self).clean(*args,...
9
2012-09-10T17:19:52Z
[ "python", "django" ]
Ghostscript on Google App Engine?
1,474,000
<p>Is it possible to call ghostscript on an App Engine project? I'd like to be able to perform some document conversion using gs (pdf to tiff in particular), and I am not seeing any specific documentation that either rules out this possibility or states how to pull it off.</p> <p>Thanks!</p> <p>Shaheeb R. </p>
0
2009-09-24T20:34:20Z
1,480,188
<p>From some cursory Googling it looks like ghostscript is written mostly in C, so no, you won't be able to run it on App Engine directly. If you have a server available you can run it there and send in documents for conversion, or you could bring up an EC2 instance remotely when you need to do a conversion.</p>
0
2009-09-26T01:28:41Z
[ "python", "google-app-engine", "ghostscript" ]
Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User
1,474,135
<p>I have an application that makes use of Django's <code>UserProfile</code> to extend the built-in Django <code>User</code> model. Looks a bit like:</p> <pre><code>class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) # Local Stuff image_url_s = models.CharField(max_length=128, bla...
14
2009-09-24T21:01:23Z
1,474,175
<p>One way would be to define a custom form to use for your Team model in the admin, and override the <code>manager</code> field to use a queryset with the correct ordering:</p> <pre><code>from django import forms class TeamForm(forms.ModelForm): manager = forms.ModelChoiceField(queryset=User.objects.order_by('us...
21
2009-09-24T21:10:23Z
[ "python", "django", "sorting", "admin", "user-profile" ]
Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User
1,474,135
<p>I have an application that makes use of Django's <code>UserProfile</code> to extend the built-in Django <code>User</code> model. Looks a bit like:</p> <pre><code>class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) # Local Stuff image_url_s = models.CharField(max_length=128, bla...
14
2009-09-24T21:01:23Z
1,474,195
<p>This</p> <pre><code>class Meta: ordering = ['username'] </code></pre> <p>should be </p> <pre><code> ordering = ['user__username'] </code></pre> <p>if it's in your UserProfile admin class. That'll stop the exception, but I don't think it helps you.</p> <p>Ordering the User model as you describe is quite t...
23
2009-09-24T21:15:58Z
[ "python", "django", "sorting", "admin", "user-profile" ]
Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User
1,474,135
<p>I have an application that makes use of Django's <code>UserProfile</code> to extend the built-in Django <code>User</code> model. Looks a bit like:</p> <pre><code>class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) # Local Stuff image_url_s = models.CharField(max_length=128, bla...
14
2009-09-24T21:01:23Z
8,098,599
<p>This might be dangerous for some reason, but this can be done in one line in your project's <code>models.py</code> file:</p> <pre><code>User._meta.ordering=["username"] </code></pre>
3
2011-11-11T18:54:28Z
[ "python", "django", "sorting", "admin", "user-profile" ]
Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User
1,474,135
<p>I have an application that makes use of Django's <code>UserProfile</code> to extend the built-in Django <code>User</code> model. Looks a bit like:</p> <pre><code>class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) # Local Stuff image_url_s = models.CharField(max_length=128, bla...
14
2009-09-24T21:01:23Z
23,649,887
<p>For me, the only working solution was to use <a href="https://docs.djangoproject.com/en/1.7/topics/db/models/#proxy-models" rel="nofollow">Proxy Model</a>. As stated in the documentation, you can create own proxy models for even built-in models and customize anything like in regular models:</p> <pre><code>class Ord...
0
2014-05-14T08:52:46Z
[ "python", "django", "sorting", "admin", "user-profile" ]
python arbitrarily incrementing an iterator inside a loop
1,474,646
<p>I am probably going about this in the wrong manner, but I was wondering how to handle this in python. </p> <p>First some c code:</p> <pre><code>int i; for(i=0;i&lt;100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } </code></pre> <p>Ok so we never see the 50's... </p> <p>My question is, how can I do ...
32
2009-09-24T23:11:17Z
1,474,675
<p>Not exactly sure I follow your thought process but here is something to feed on..</p> <pre><code>for i in range(len(cdata.split('\n'))): if i in range(50,60): continue line = cdata[i] if exp.match(line): #increment the position of the iterator by 5? pass print line </code></pre> <p>Not sure what yo...
1
2009-09-24T23:22:06Z
[ "python", "iterator" ]
python arbitrarily incrementing an iterator inside a loop
1,474,646
<p>I am probably going about this in the wrong manner, but I was wondering how to handle this in python. </p> <p>First some c code:</p> <pre><code>int i; for(i=0;i&lt;100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } </code></pre> <p>Ok so we never see the 50's... </p> <p>My question is, how can I do ...
32
2009-09-24T23:11:17Z
1,474,683
<p>If you're doing it with numbers a list comprehension can work:</p> <pre><code>for i in [x for x in range(0, 99) if x &lt; 50 and x &gt; 59]: print i </code></pre> <p>Moving an iterator forward is a bit more difficult though. I'd suggest setting your list up beforehand if you don't want to do the counter appro...
2
2009-09-24T23:24:38Z
[ "python", "iterator" ]
python arbitrarily incrementing an iterator inside a loop
1,474,646
<p>I am probably going about this in the wrong manner, but I was wondering how to handle this in python. </p> <p>First some c code:</p> <pre><code>int i; for(i=0;i&lt;100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } </code></pre> <p>Ok so we never see the 50's... </p> <p>My question is, how can I do ...
32
2009-09-24T23:11:17Z
1,474,697
<p>You can drop values from an iterator</p> <pre><code>def dropvalues(iterator, vals): for i in xrange(vals): iterator.next() </code></pre> <p>Now just make sure you have an iterator object to work on with <code>lines = iter(cdata.split('\n'))</code>; and loop over it.</p>
1
2009-09-24T23:31:43Z
[ "python", "iterator" ]
python arbitrarily incrementing an iterator inside a loop
1,474,646
<p>I am probably going about this in the wrong manner, but I was wondering how to handle this in python. </p> <p>First some c code:</p> <pre><code>int i; for(i=0;i&lt;100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } </code></pre> <p>Ok so we never see the 50's... </p> <p>My question is, how can I do ...
32
2009-09-24T23:11:17Z
1,474,751
<p>Maybe with genexps. Not pretty but...</p> <p>Something like that:</p> <pre><code>&gt;&gt;&gt; gx = (line for line in '1 2 x 3 4 5 6 7 x 9 10 11 12 x 1'.split('\n')) &gt;&gt;&gt; for line in gx: ... if line == 'x': ... for i in range(2): ... line = gx.next() ... print line </code></pre> <p>The o...
0
2009-09-24T23:49:43Z
[ "python", "iterator" ]
python arbitrarily incrementing an iterator inside a loop
1,474,646
<p>I am probably going about this in the wrong manner, but I was wondering how to handle this in python. </p> <p>First some c code:</p> <pre><code>int i; for(i=0;i&lt;100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } </code></pre> <p>Ok so we never see the 50's... </p> <p>My question is, how can I do ...
32
2009-09-24T23:11:17Z
1,474,848
<p>There is a fantastic package in Python called <a href="http://docs.python.org/library/itertools.html"><code>itertools</code></a>.</p> <p>But before I get into that, it'd serve well to explain how the iteration protocol is implemented in Python. When you want to provide iteration over your container, you specify the...
35
2009-09-25T00:27:11Z
[ "python", "iterator" ]
python arbitrarily incrementing an iterator inside a loop
1,474,646
<p>I am probably going about this in the wrong manner, but I was wondering how to handle this in python. </p> <p>First some c code:</p> <pre><code>int i; for(i=0;i&lt;100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } </code></pre> <p>Ok so we never see the 50's... </p> <p>My question is, how can I do ...
32
2009-09-24T23:11:17Z
1,474,880
<p>I can't parse the question vary well because there's this block of confusing and irrelevant C code. Please delete it.</p> <p>Focusing on just the Python code and the question about how to skip 5 lines...</p> <pre><code>lineIter= iter( cdata.splitlines() ) for line in lineIter: if exp.match(line): for count ...
-5
2009-09-25T00:41:40Z
[ "python", "iterator" ]
python arbitrarily incrementing an iterator inside a loop
1,474,646
<p>I am probably going about this in the wrong manner, but I was wondering how to handle this in python. </p> <p>First some c code:</p> <pre><code>int i; for(i=0;i&lt;100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } </code></pre> <p>Ok so we never see the 50's... </p> <p>My question is, how can I do ...
32
2009-09-24T23:11:17Z
1,474,881
<h3><a href="http://docs.python.org/library/itertools.html#itertools.islice">itertools.islice</a>:</h3> <pre><code>lines = iter(cdata.splitlines()) for line in lines: if exp.match(line): #increment the position of the iterator by 5 for _ in itertools.islice(lines, 4): pass continue ...
16
2009-09-25T00:42:07Z
[ "python", "iterator" ]
python arbitrarily incrementing an iterator inside a loop
1,474,646
<p>I am probably going about this in the wrong manner, but I was wondering how to handle this in python. </p> <p>First some c code:</p> <pre><code>int i; for(i=0;i&lt;100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } </code></pre> <p>Ok so we never see the 50's... </p> <p>My question is, how can I do ...
32
2009-09-24T23:11:17Z
1,476,998
<p>for your example, as you're working with lists (indexable sequences) and not with iterators, I would recommend the following:</p> <pre><code>lines = cdata.split("\n") for line in lines[:50]+lines[60:]: print line </code></pre> <p>that's not the most efficient since it potentially constructs 3 new lists (but if t...
0
2009-09-25T12:21:18Z
[ "python", "iterator" ]
Python variables
1,474,719
<p>I'm trying to create a basic program that will generate a number based on variables entered by the user. The formula is </p> <pre><code>a = b / (c * d) </code></pre> <p>This is a formula for finding specific heat, whereas <code>b=energy</code>, <code>c=mass</code>, and <code>d= change in temperature</code>.</p> ...
2
2009-09-24T23:39:08Z
1,474,731
<pre><code>b = float(raw_input("Please enter a value: ")) a = b / (c*d) print a </code></pre> <p><code>raw_input()</code> prompts the user for input, assuming you're running this in the console</p> <p><code>float()</code> attempts to convert the parameter (in this case, the user input) to a float. the rest of it shou...
0
2009-09-24T23:43:18Z
[ "python" ]
Python variables
1,474,719
<p>I'm trying to create a basic program that will generate a number based on variables entered by the user. The formula is </p> <pre><code>a = b / (c * d) </code></pre> <p>This is a formula for finding specific heat, whereas <code>b=energy</code>, <code>c=mass</code>, and <code>d= change in temperature</code>.</p> ...
2
2009-09-24T23:39:08Z
1,474,732
<p>The most simple approach is to precede the formula with the fragment</p> <pre><code>b = input("Enter b:") c = input("Enter c:") d = input("Enter d:") </code></pre> <p>A few things to note:</p> <ul> <li>this will require console IO, so you best start the script from a console</li> <li>input() causes the string ent...
4
2009-09-24T23:44:04Z
[ "python" ]
Python variables
1,474,719
<p>I'm trying to create a basic program that will generate a number based on variables entered by the user. The formula is </p> <pre><code>a = b / (c * d) </code></pre> <p>This is a formula for finding specific heat, whereas <code>b=energy</code>, <code>c=mass</code>, and <code>d= change in temperature</code>.</p> ...
2
2009-09-24T23:39:08Z
1,474,738
<p>I think you want something like this:</p> <pre><code>b = float(raw_input("Energy? ")) c = float(raw_input("Mass? ")) d = float(raw_input("Change in temperature? ")) print "Specific heat: %f" % (b / (c * d)) </code></pre> <ul> <li>raw_input() prompts the user and returns the inputted value</li> <li>float() convert...
6
2009-09-24T23:44:59Z
[ "python" ]
Python variables
1,474,719
<p>I'm trying to create a basic program that will generate a number based on variables entered by the user. The formula is </p> <pre><code>a = b / (c * d) </code></pre> <p>This is a formula for finding specific heat, whereas <code>b=energy</code>, <code>c=mass</code>, and <code>d= change in temperature</code>.</p> ...
2
2009-09-24T23:39:08Z
29,723,299
<p>this is a really simple way to do it and easy to understand</p> <pre><code>b= float(raw_input("Enter the energy")) c= float(raw_input("Enter the mass")) d= float(raw_input("Enter change in temperature")) a = b / (c * d) print a </code></pre>
0
2015-04-18T21:53:34Z
[ "python" ]
Google App Engine get PolyModel as child class
1,474,868
<p>When I run Google App Engine likeso:</p> <pre><code> from google.appengine.ext import db from google.appengine.ext.db import polymodel class Father(polymodel.PolyModel): def hello(self): print "Father says hi" class Son(Father): def hello(self): print "Spawn says hi" </code></pr...
0
2009-09-25T00:34:40Z
1,475,187
<p>You did a "Father.get..." so you created an object from the Father class. So why wouldn't it say "Father says hi". </p> <p>If you Father class had lastname and firstname, and your Son class had middle name, you won't get the middle name unless you specifically retrieve the 'Son' record. </p> <p>If you want to do ...
-1
2009-09-25T02:47:39Z
[ "python", "google-app-engine", "polymodel" ]
Google App Engine get PolyModel as child class
1,474,868
<p>When I run Google App Engine likeso:</p> <pre><code> from google.appengine.ext import db from google.appengine.ext.db import polymodel class Father(polymodel.PolyModel): def hello(self): print "Father says hi" class Son(Father): def hello(self): print "Spawn says hi" </code></pr...
0
2009-09-25T00:34:40Z
1,475,548
<p>I can't reproduce your problem -- indeed, your code just dies with an import error (<code>PolyModel</code> is not in module <code>db</code>) on my GAE (version 1.2.5). Once I've fixed things enough to let the code run...:</p> <pre><code>import wsgiref.handlers from google.appengine.ext import webapp from google.ap...
1
2009-09-25T05:17:55Z
[ "python", "google-app-engine", "polymodel" ]
BitString error on Windows XP?
1,475,033
<p>Scott, I'd like to thank you for your BitString program. I am working on interpreting data from a neutron detector, and I've found that this module is just the tool I need. Unfortunately, I have yet to get the module to successfully pass test-bitstring.py. I'm running Windows XP and Python 3.1. I've downloaded y...
0
2009-09-25T01:40:50Z
1,475,275
<p>I just tested that bitstring-0.4.1 's test-bitstring.py works flawlessly on both Python 3.0 and Python 3.1, on a Windows XP host.</p> <p>The 3.1 version, specifically, this is what happens.</p> <pre><code>'3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)]' c:\python31\python test_bitstring.py ...
2
2009-09-25T03:26:14Z
[ "python", "windows-xp", "python-3.x", "bitstring" ]
BitString error on Windows XP?
1,475,033
<p>Scott, I'd like to thank you for your BitString program. I am working on interpreting data from a neutron detector, and I've found that this module is just the tool I need. Unfortunately, I have yet to get the module to successfully pass test-bitstring.py. I'm running Windows XP and Python 3.1. I've downloaded y...
0
2009-09-25T01:40:50Z
1,475,709
<p>feel free to email me queries like this (that's what I meant when I said contact me in the documentation) - I'm somewhat surprised to find a direct question to me on S.O., but I just happened to see it!</p> <p>You should update to the <a href="http://code.google.com/p/python-bitstring/downloads/list" rel="nofollow"...
1
2009-09-25T06:24:25Z
[ "python", "windows-xp", "python-3.x", "bitstring" ]
Easiest way to turn a list into an HTML table in python?
1,475,123
<p>lets say I have a list like so:</p> <pre><code>['one','two','three','four','five','six','seven','eight','nine'] </code></pre> <p>and I want to experiment with turning this data into a HTML table of various dimensions:</p> <pre><code>one two three four five six seven eight nine </code></pre> <p>or<...
6
2009-09-25T02:23:41Z
1,475,148
<p>Well there are several templating libraries around (Genshi is one I like but there are many others).</p> <p>Alternatively you could do something like:</p> <pre><code>def print_table(data, row_length): print '&lt;table&gt;' counter = 0 for element in data: if counter % row_length == 0: ...
2
2009-09-25T02:33:15Z
[ "python", "html-table", "html-generation" ]
Easiest way to turn a list into an HTML table in python?
1,475,123
<p>lets say I have a list like so:</p> <pre><code>['one','two','three','four','five','six','seven','eight','nine'] </code></pre> <p>and I want to experiment with turning this data into a HTML table of various dimensions:</p> <pre><code>one two three four five six seven eight nine </code></pre> <p>or<...
6
2009-09-25T02:23:41Z
1,475,453
<p>I would decompose your problem into two parts:</p> <ul> <li>given a "flat list", produce a list of sublists where the sublists are of a given length and the overall list may be walked into either a "row major" order (your first and third example) or "column major" (your second example);</li> <li>given a list of sub...
19
2009-09-25T04:36:28Z
[ "python", "html-table", "html-generation" ]
Easiest way to turn a list into an HTML table in python?
1,475,123
<p>lets say I have a list like so:</p> <pre><code>['one','two','three','four','five','six','seven','eight','nine'] </code></pre> <p>and I want to experiment with turning this data into a HTML table of various dimensions:</p> <pre><code>one two three four five six seven eight nine </code></pre> <p>or<...
6
2009-09-25T02:23:41Z
1,476,000
<p>Maybe manipulate template is easier for toy codes, =p</p> <pre><code>def get_html_tbl(seq, col_count): if len(seq) % col_count: seq.extend([''] * (col_count - len(seq) % col_count)) tbl_template = '&lt;table&gt;%s&lt;/table&gt;' % ('&lt;tr&gt;%s&lt;/tr&gt;' % ('&lt;td&gt;%s&lt;/td&gt;' * col_count) ...
0
2009-09-25T08:03:23Z
[ "python", "html-table", "html-generation" ]
Easiest way to turn a list into an HTML table in python?
1,475,123
<p>lets say I have a list like so:</p> <pre><code>['one','two','three','four','five','six','seven','eight','nine'] </code></pre> <p>and I want to experiment with turning this data into a HTML table of various dimensions:</p> <pre><code>one two three four five six seven eight nine </code></pre> <p>or<...
6
2009-09-25T02:23:41Z
25,163,013
<p>Just for future reference, I implemented a small Python module called <strong>simpletable</strong> to provide easy HTML table generation. It also that deals with the issue described in this question.</p> <p>The usage is as simple as below:</p> <pre><code>import simpletable test_data = [str(x) for x in range(20)] ...
2
2014-08-06T14:27:49Z
[ "python", "html-table", "html-generation" ]
what happens to a python object when you throw an exception from it
1,475,193
<p>My class contains a socket that connects to a server. Some of the methods of the class can throw an exception. The script I'm running contains an outer loop that catches the exception, logs an error, and creates a new class instance that tries to reconnect to the server.</p> <p>Problem is that the server only handl...
2
2009-09-25T02:49:51Z
1,475,198
<p>This is an artifact of garbage collection. Even though the object is <em>out of scope</em>, it is not necessarily <em>collected</em> and therefore <em>destroyed</em> until a garbage collection run occurs -- this is not like C++ where a destructor is called as soon as an object loses scope.</p> <p>You can probably ...
5
2009-09-25T02:54:02Z
[ "python", "exception", "garbage-collection", "sockets" ]
what happens to a python object when you throw an exception from it
1,475,193
<p>My class contains a socket that connects to a server. Some of the methods of the class can throw an exception. The script I'm running contains an outer loop that catches the exception, logs an error, and creates a new class instance that tries to reconnect to the server.</p> <p>Problem is that the server only handl...
2
2009-09-25T02:49:51Z
1,475,289
<p>Ok, here's the final version. Explicitly close the socket objects when something gets borked.</p> <pre><code>import socket class MyException(Exception): pass class MyClient(object): def __init__(self, port): self.sock = socket.create_connection(('localhost', port)) self.sockfile = self.so...
0
2009-09-25T03:33:52Z
[ "python", "exception", "garbage-collection", "sockets" ]
what happens to a python object when you throw an exception from it
1,475,193
<p>My class contains a socket that connects to a server. Some of the methods of the class can throw an exception. The script I'm running contains an outer loop that catches the exception, logs an error, and creates a new class instance that tries to reconnect to the server.</p> <p>Problem is that the server only handl...
2
2009-09-25T02:49:51Z
1,475,305
<p>The garbage collector can be flagged to clean up by setting the relevant object to None.</p>
0
2009-09-25T03:38:37Z
[ "python", "exception", "garbage-collection", "sockets" ]
what happens to a python object when you throw an exception from it
1,475,193
<p>My class contains a socket that connects to a server. Some of the methods of the class can throw an exception. The script I'm running contains an outer loop that catches the exception, logs an error, and creates a new class instance that tries to reconnect to the server.</p> <p>Problem is that the server only handl...
2
2009-09-25T02:49:51Z
1,476,261
<p>Can you really handle the Exception in connect()?</p> <p>I think you should provide a MyClient.close() method, and write connect() like this:</p> <pre><code>def connect():     try:         c = MyClient(9989)         print c.do_stuff()     finally:         c.close() </code></pre> <p>This is in comp...
1
2009-09-25T09:10:03Z
[ "python", "exception", "garbage-collection", "sockets" ]
global counter in Django Application?
1,475,264
<p>I was wondering if there is "global counter" in Django application, like the way I store "global counter" in Servlet Context scope in Tomcat.</p> <p>something like </p> <p>getServletContext().getAttribute("counter"); counter++;</p>
0
2009-09-25T03:18:24Z
1,475,314
<p>When you write a django application (or any wsgi application, for that matter), you don't know beforehand if your application will end up running standalone on a single server, or multithreaded, or multiprocessed, or even in multiple separate machines as part of a load balancing strategy.</p> <p>If you're going to ...
1
2009-09-25T03:40:05Z
[ "python", "django" ]
How do you bind a language (python, for example) to another (say, C++)?
1,475,637
<p>I'm far from a python expert but I hear this one all the time, about its C/C++ bindings. How does this concept work, and how does Python (and Java) bind to C-based APIs like OpenGL? This stuff has always been a mystery to me.</p>
15
2009-09-25T05:55:40Z
1,475,650
<p>Generally these languages have a way to load extensions written in C. The Java interface is called JNI (Java Native Interface). Python has comprehensive <a href="http://docs.python.org/extending/index.html" rel="nofollow">documentation</a> about its extension interface.</p> <p>Another option for Python is the <a ...
3
2009-09-25T05:59:11Z
[ "java", "python", "binding" ]
How do you bind a language (python, for example) to another (say, C++)?
1,475,637
<p>I'm far from a python expert but I hear this one all the time, about its C/C++ bindings. How does this concept work, and how does Python (and Java) bind to C-based APIs like OpenGL? This stuff has always been a mystery to me.</p>
15
2009-09-25T05:55:40Z
1,475,673
<p>For Perl, there are two ways to call C++ subroutines:</p> <ul> <li><p><a href="http://search.cpan.org/~nwclark/perl-5.8.8/pod/perlxs.pod" rel="nofollow">Perl XS (eXternal Subroutine)</a> (See also <a href="http://en.wikipedia.org/wiki/XS%5F%28Perl%29" rel="nofollow">Wiki</a>) - allows calling subroutines from other...
1
2009-09-25T06:08:17Z
[ "java", "python", "binding" ]
How do you bind a language (python, for example) to another (say, C++)?
1,475,637
<p>I'm far from a python expert but I hear this one all the time, about its C/C++ bindings. How does this concept work, and how does Python (and Java) bind to C-based APIs like OpenGL? This stuff has always been a mystery to me.</p>
15
2009-09-25T05:55:40Z
1,475,687
<p>There are basically two ways of integrating c/c++ with python:</p> <ul> <li>extending: accessing c/c++ from python</li> <li>embedding: accessing the python interpreter from c/c++</li> </ul> <p>What you mention is the first case. Its usually achieved by writing <em>wrapper functions</em> that serves as <em>glue cod...
1
2009-09-25T06:14:26Z
[ "java", "python", "binding" ]
How do you bind a language (python, for example) to another (say, C++)?
1,475,637
<p>I'm far from a python expert but I hear this one all the time, about its C/C++ bindings. How does this concept work, and how does Python (and Java) bind to C-based APIs like OpenGL? This stuff has always been a mystery to me.</p>
15
2009-09-25T05:55:40Z
1,475,717
<h3>Interpreters Written in C89 with Reflection, Who Knew?</h3> <hr> <p>I have a feeling you are looking for an explanation of the mechanism and not a link to the API or instructions on how to code it. So, as I understand it . . .</p> <p>The main interpreter is typically written in C and is dynamically linked. In a ...
11
2009-09-25T06:26:11Z
[ "java", "python", "binding" ]
How do you bind a language (python, for example) to another (say, C++)?
1,475,637
<p>I'm far from a python expert but I hear this one all the time, about its C/C++ bindings. How does this concept work, and how does Python (and Java) bind to C-based APIs like OpenGL? This stuff has always been a mystery to me.</p>
15
2009-09-25T05:55:40Z
1,475,816
<p>The concepts below can be generalized relatively easily, however I'm going to refer specifically to C and Python a lot for clarity.</p> <p><strong>Calling C from Python</strong></p> <p>This can work because most lower level languages/architectures/operating systems have well-defined Application Binary Interfaces w...
2
2009-09-25T06:58:07Z
[ "java", "python", "binding" ]
How do you bind a language (python, for example) to another (say, C++)?
1,475,637
<p>I'm far from a python expert but I hear this one all the time, about its C/C++ bindings. How does this concept work, and how does Python (and Java) bind to C-based APIs like OpenGL? This stuff has always been a mystery to me.</p>
15
2009-09-25T05:55:40Z
1,478,058
<p>The main general concept is known as <a href="http://en.wikipedia.org/wiki/Foreign%5Ffunction%5Finterface" rel="nofollow">FFI</a>, "Foreign Function Interface" -- for Java it's JNI, for Python it's the "Python C API", for Perl it's XS, etc, etc, but I think it's important to give you the general term of art to help ...
6
2009-09-25T15:37:38Z
[ "java", "python", "binding" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
1,475,971
<p>Why don't you just use <code>subprocess.call</code> on <code>tail</code> itself?</p> <pre><code>subproces.call(['tail', '-f', filename]) </code></pre> <p><strong>Edit:</strong> Fixed to eliminate extra shell process.</p> <p><strong>Edit2:</strong> Fixed to eliminate deprecated <code>os.popen</code> and thus the n...
-2
2009-09-25T07:53:20Z
[ "python" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
1,475,976
<p>When reading from a file, your only choice is sleep (<a href="http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c#n877" rel="nofollow">see the source code</a>). If you read from a pipe, you can simply read since the read will block until there is data ready.</p> <p>The reason for this is that the OS does...
10
2009-09-25T07:53:40Z
[ "python" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
1,476,006
<p>(update) Either use FS monitors tools </p> <ul> <li>For <a href="http://pyinotify.sourceforge.net/">linux</a></li> <li>For <a href="http://timgolden.me.uk/python/win32%5Fhow%5Fdo%5Fi/watch%5Fdirectory%5Ffor%5Fchanges.html">Windows</a></li> <li>For <a href="http://pypi.python.org/pypi/pyobjc-framework-FSEvents/2.2b2...
31
2009-09-25T08:05:04Z
[ "python" ]
tail -f in python with no time.sleep
1,475,950
<p>I need to emulate "tail -f" in python, but I don't want to use time.sleep in the reading loop. I want something more elegant like some kind of blocking read, or select.select with timeout, but python 2.6 "select" documentation specifically says: "it cannot be used on regular files to determine whether a file has gro...
24
2009-09-25T07:45:08Z
1,476,039
<p>IMO you should use sleep, it works on all platform and code will be simple</p> <p>Otherwise you can use platform specific APIs which can tell you when file change e.g. on window use <a href="http://msdn.microsoft.com/en-us/library/aa364417%28VS.85%29.aspx" rel="nofollow">FindFirstChangeNotification</a> on folder an...
0
2009-09-25T08:13:03Z
[ "python" ]