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 |
|---|---|---|---|---|---|---|---|---|---|
Getting an input/output error from Python with pySerial | 994,538 | <p>I have a Python script that writes data packets to an Arduino board through <a href="http://pyserial.sourceforge.net/" rel="nofollow">pySerial</a>.
Sometimes while writing the code to the board pySerial raises an input/output error with errno 5.</p>
<p>Some research says that this indicates an error while writing ... | 4 | 2009-06-15T04:55:26Z | 19,815,657 | <p>Let me try to offer a few comments that might be helpful to you and other folks with similar problems. First, try to run your Arduino sketch with the Serial Monitor a few times. You can find the Serial Monitor under Tools in the IDE menu. You can also type Ctrl-Shift-M to invoke the Serial Monitor.</p>
<p>The Seria... | 0 | 2013-11-06T15:10:43Z | [
"python",
"serial-port",
"arduino",
"pyserial"
] |
trouble with pamie | 994,627 | <p>I'm having some strange trouble with pamie: <a href="http://pamie.sourceforge.net/" rel="nofollow">http://pamie.sourceforge.net/</a> .</p>
<p>I have written a script to do some port (25) forwarding based on a recepie that I found on the web, Here is the code that matters:</p>
<pre><code># forwardc2s(source, destin... | 1 | 2009-06-15T05:48:52Z | 994,695 | <p><strong>The PAMIE object does not work within threads!!!</strong></p>
<p>I was originally starting forwardc2s as a thread. When I just call it as a function instead, everything works fine!</p>
<p>Please consider this question resolved... with great thanks to the <a href="http://en.wikipedia.org/wiki/Rubber%5Fduck%... | 1 | 2009-06-15T06:18:26Z | [
"python",
"debugging",
"pamie"
] |
How to strip the 8th bit in a KOI8-R encoded character? | 994,710 | <p>How to strip the 8th bit in a KOI8-R encoded character so as to have translit for a Russian letter? In particular, how to make it in Python?</p>
| 1 | 2009-06-15T06:24:31Z | 994,730 | <p>I'm not exactly sure what you want, but if you want to zero the 8th bit, it can be done like this:</p>
<pre><code>character = character & ~(1 << 7)
</code></pre>
| 1 | 2009-06-15T06:32:38Z | [
"python",
"encoding"
] |
How to strip the 8th bit in a KOI8-R encoded character? | 994,710 | <p>How to strip the 8th bit in a KOI8-R encoded character so as to have translit for a Russian letter? In particular, how to make it in Python?</p>
| 1 | 2009-06-15T06:24:31Z | 994,794 | <p>Here is one way:</p>
<pre><code>import array
mask = ~(1 << 7)
def convert(koistring):
bytes = array.array('B', koistring)
for i in range(len(bytes)):
bytes[i] &= mask
return bytes.tostring()
test = u'Ð ÑÑÑкий ТекÑÑ'.encode('koi8-r')
print convert(test) # rUSSKIJ tEKST
</... | 1 | 2009-06-15T06:59:51Z | [
"python",
"encoding"
] |
How to strip the 8th bit in a KOI8-R encoded character? | 994,710 | <p>How to strip the 8th bit in a KOI8-R encoded character so as to have translit for a Russian letter? In particular, how to make it in Python?</p>
| 1 | 2009-06-15T06:24:31Z | 994,804 | <p>Assuming s is a KOI8-R encoded string you could try this:</p>
<pre><code>>>> s = u'Ðод Ðбмена ÐнÑоÑмаÑией, 8 биÑ'.encode('koi8-r')
>>> s
>>> '\xeb\xcf\xc4 \xef\xc2\xcd\xc5\xce\xc1 \xe9\xce\xc6\xcf\xd2\xcd\xc1\xc3\xc9\xc5\xca, 8 \xc2\xc9\xd4'
>>> print ''.j... | 2 | 2009-06-15T07:05:12Z | [
"python",
"encoding"
] |
why does this code break out of loop? | 994,729 | <pre><code>import math
t=raw_input()
k=[]
a=0
for i in range(0,int(t)):
s=raw_input()
b=1
c=1
a=int(s)
if a==0:
continue
else:
d=math.atan(float(1)/b) + math.atan(float(1)/c)
v=math.atan(float(1)/a)
print v
print d
print float(v)
print floa... | 0 | 2009-06-15T06:32:27Z | 994,755 | <p>Your <code>while</code> loop tests on an empty tuple, which evaluates to <code>False</code>. Thus, the statements within the <code>while</code> loop will never execute:</p>
<p>If you want your <code>while</code> loop to run until it encounters a <code>break</code> statement, do this:</p>
<pre><code>while True:
... | 8 | 2009-06-15T06:41:37Z | [
"python",
"syntax-error"
] |
why does this code break out of loop? | 994,729 | <pre><code>import math
t=raw_input()
k=[]
a=0
for i in range(0,int(t)):
s=raw_input()
b=1
c=1
a=int(s)
if a==0:
continue
else:
d=math.atan(float(1)/b) + math.atan(float(1)/c)
v=math.atan(float(1)/a)
print v
print d
print float(v)
print floa... | 0 | 2009-06-15T06:32:27Z | 994,775 | <p>Well, it didn't reach the break point. The problem is that <code>while()</code> does not loop at all. To do an infinite loop, do <code>while (1):</code> (since the while condition must evaluate to true. Here's a working (cleaned up) sample.</p>
<pre><code>import math
t = raw_input()
k = []
a = 0.0
for i in range(0,... | 0 | 2009-06-15T06:49:54Z | [
"python",
"syntax-error"
] |
why does this code break out of loop? | 994,729 | <pre><code>import math
t=raw_input()
k=[]
a=0
for i in range(0,int(t)):
s=raw_input()
b=1
c=1
a=int(s)
if a==0:
continue
else:
d=math.atan(float(1)/b) + math.atan(float(1)/c)
v=math.atan(float(1)/a)
print v
print d
print float(v)
print floa... | 0 | 2009-06-15T06:32:27Z | 1,449,752 | <p>If is very dangerous to make comparsisons like float(a)==float(b) since float variables have no exact representation. Due to rounding errors you may not have identic values.</p>
<p>Even 2*0.5 may not be equal 1. You may use the following:</p>
<pre><code>if abs(float(a)-float(b)) < verySmallValue:
</code></pre>
| 2 | 2009-09-19T23:04:37Z | [
"python",
"syntax-error"
] |
why does this code break out of loop? | 994,729 | <pre><code>import math
t=raw_input()
k=[]
a=0
for i in range(0,int(t)):
s=raw_input()
b=1
c=1
a=int(s)
if a==0:
continue
else:
d=math.atan(float(1)/b) + math.atan(float(1)/c)
v=math.atan(float(1)/a)
print v
print d
print float(v)
print floa... | 0 | 2009-06-15T06:32:27Z | 1,449,753 | <p><a href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm" rel="nofollow">http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm</a></p>
<blockquote>
<p>Floating point math is not exact.
Simple values like 0.2 cannot be
precisely represented using binary
floating ... | 2 | 2009-09-19T23:04:44Z | [
"python",
"syntax-error"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 994,762 | <p>If all you need to do is to modify structs, you can directly use regex to split and apply changes to each value in the struct, looking for the declaration and the ending }; to know when to stop.</p>
<p>If you really need a more general solution you could use a parser generator, like <a href="http://pyparsing.wikisp... | 3 | 2009-06-15T06:44:49Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 994,769 | <p>You don't really provide much information about how what is to be modified should be determined, but to address your specific example:</p>
<pre><code>$ perl -pi.bak -we'if ( /const BYTE Some_Idx/ .. /;/ ) { s/Some_Idx/Some_Idx_Mod_mul_2/g; s/(\d+)/$1 * 2/ge; }' header.h
</code></pre>
<p>Breaking that down, -p says... | 4 | 2009-06-15T06:47:14Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 994,805 | <p>Keeping your data lying around in a header makes it trickier to get at using other programs like Perl. Another approach you might consider is to keep this data in a database or another file and regenerate your header file as-needed, maybe even as part of your build system. The reason for this is that generating C is... | 9 | 2009-06-15T07:05:21Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 994,912 | <p>There is a Perl module called <a href="http://search.cpan.org/dist/Parse-RecDescent" rel="nofollow">Parse::RecDescent</a> which is a very powerful recursive descent parser generator. It comes with a bunch of examples. One of them is a <a href="http://cpansearch.perl.org/src/DCONWAY/Parse-RecDescent-1.96.0/demo/demo%... | 2 | 2009-06-15T07:47:45Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 994,954 | <p>Python solution (not full, just a hint ;)) Sorry if any mistakes - not tested</p>
<pre><code>import re
text = open('your file.c').read()
patt = r'(?is)(.*?{)(.*?)(}\s*;)'
m = re.search(patt, text)
g1, g2, g3 = m.group(1), m.group(2), m.group(3)
g2 = [int(i) * 2 for i in g2.split(',')
out = open('your file 2.c', 'w'... | 2 | 2009-06-15T07:59:22Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 994,981 | <p>Sorry if this is a stupid question, but why worry about parsing the file at all? Why not write a C program that #includes the header, processes it as required and then spits out the source for the modified header. I'm sure this would be simpler than the Perl/Python solutions, and it would be much more reliable becau... | 6 | 2009-06-15T08:08:37Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 996,289 | <p>There is a really useful Perl module called <a href="http://search.cpan.org/dist/Convert-Binary-C/lib/Convert/Binary/C.pm" rel="nofollow">Convert::Binary::C</a> that parses C header files and converts structs from/to Perl data structures.</p>
| 2 | 2009-06-15T14:02:05Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 997,859 | <p>You could always use <a href="http://perldoc.perl.org/functions/pack.html" rel="nofollow"><code>pack</code></a> / <a href="http://perldoc.perl.org/functions/unpack.html" rel="nofollow"><code>unpack</code></a>, to read, and write the data.</p>
<pre><code>#! /usr/bin/env perl
use strict;
use warnings;
use autodie;
m... | 0 | 2009-06-15T19:15:51Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
How can I parse a C header file with Perl? | 994,732 | <p>I have a header file in which there is a large struct. I need to read this structure using some program and make some operations on each member of the structure and write them back.</p>
<p>For example I have some structure like </p>
<pre><code>const BYTE Some_Idx[] = {
4,7,10,15,17,19,24,29,
31,32,35,45,49,51,52,5... | 6 | 2009-06-15T06:33:14Z | 2,804,421 | <p>For the GCC::TranslationUnit example see hparse.pl from <a href="http://gist.github.com/395160" rel="nofollow">http://gist.github.com/395160</a>
which will make it into C::DynaLib, and the not yet written Ctypes also.
This parses functions for FFI's, and not bare structs contrary to Convert::Binary::C.
hparse will ... | 0 | 2010-05-10T16:29:44Z | [
"python",
"c",
"perl",
"parsing",
"header-files"
] |
customize login in google app engine | 994,965 | <p>I need to add few more options for login and therefor need to customize create_login_url with some html code. Is there a way to add on your code in default login screen of google?
ENvironment - python-google app engine.
I want to continue having the default google ext class Users behavior to conntinue to be in place... | 2 | 2009-06-15T08:03:22Z | 995,085 | <p>You can't customize the login page. Allowing you to do so would introduce the possibility of XSS vulnerabilities, as well as making it harder for users to identify a legitimate login page.</p>
<p>If you want to provide for federated login, you may want to simply redirect users to an interstitial page that allows th... | 2 | 2009-06-15T08:50:51Z | [
"python",
"google-app-engine",
"authentication",
"registration"
] |
customize login in google app engine | 994,965 | <p>I need to add few more options for login and therefor need to customize create_login_url with some html code. Is there a way to add on your code in default login screen of google?
ENvironment - python-google app engine.
I want to continue having the default google ext class Users behavior to conntinue to be in place... | 2 | 2009-06-15T08:03:22Z | 1,010,764 | <p>You might consider OpenID, through any of the various open-source app engine projects for the purpose, such as <a href="http://code.google.com/p/google-app-engine-django-openid/" rel="nofollow">this one</a> for Django.</p>
<p>You can't use the existing Users module with those (save perhaps with some serious hacking... | 1 | 2009-06-18T04:20:43Z | [
"python",
"google-app-engine",
"authentication",
"registration"
] |
customize login in google app engine | 994,965 | <p>I need to add few more options for login and therefor need to customize create_login_url with some html code. Is there a way to add on your code in default login screen of google?
ENvironment - python-google app engine.
I want to continue having the default google ext class Users behavior to conntinue to be in place... | 2 | 2009-06-15T08:03:22Z | 1,952,698 | <p>Nick Johnson recently released an alpha version of a <a href="http://blog.notdot.net/2009/12/OpenID-on-App-Engine-made-easy-with-AEoid" rel="nofollow">WSGI middleware</a> that you could use. The API is very similar to the standard Users API in app engine. It is a way to support auth via OpenID (something Alex Martel... | 2 | 2009-12-23T13:04:31Z | [
"python",
"google-app-engine",
"authentication",
"registration"
] |
Using python regex to extract namespaces from C++ sources | 995,165 | <p>I am trying to extract the namespaces defined in C++ files.<br />
Basically, if my C++ file contains:</p>
<pre>
namespace n1 {
...
namespace n2 { ... } // end namespace n2
...
namespace n3 { ...} //end namespace n3
...
} //end namespace n1
</pre>
<p>I want to be able to retrieve: n1, n1::n2, n1::n3.</p... | 1 | 2009-06-15T09:15:37Z | 995,182 | <p>Searching for the namespace names is pretty easy with a regular expression. However, to determine the nesting level you will have to keep track of the curly bracket nesting level in the source file. This is a parsing problem, one that cannot be solved (sanely) with regular expressions. Also, you may have to deal wit... | 6 | 2009-06-15T09:22:05Z | [
"c++",
"python",
"regex",
"namespaces"
] |
Using python regex to extract namespaces from C++ sources | 995,165 | <p>I am trying to extract the namespaces defined in C++ files.<br />
Basically, if my C++ file contains:</p>
<pre>
namespace n1 {
...
namespace n2 { ... } // end namespace n2
...
namespace n3 { ...} //end namespace n3
...
} //end namespace n1
</pre>
<p>I want to be able to retrieve: n1, n1::n2, n1::n3.</p... | 1 | 2009-06-15T09:15:37Z | 995,277 | <p>You cannot completely ignore preprocessor directives, as they may introduce additional namespaces. I have seen a lot of code like:</p>
<pre><code>#define __NAMESPACE_SYSTEM__ namespace system
__NAMESPACE_SYSTEM__ {
// actual code here...
}
</code></pre>
<p>Yet, I don't see any reason for using such directives,... | 1 | 2009-06-15T09:54:49Z | [
"c++",
"python",
"regex",
"namespaces"
] |
Using python regex to extract namespaces from C++ sources | 995,165 | <p>I am trying to extract the namespaces defined in C++ files.<br />
Basically, if my C++ file contains:</p>
<pre>
namespace n1 {
...
namespace n2 { ... } // end namespace n2
...
namespace n3 { ...} //end namespace n3
...
} //end namespace n1
</pre>
<p>I want to be able to retrieve: n1, n1::n2, n1::n3.</p... | 1 | 2009-06-15T09:15:37Z | 997,065 | <p>Most of the time when someone asks how to do something with regex, they're doing something very wrong. I don't think this case is different.</p>
<p>If you want to parse c++, you need to use a c++ parser. There are many things that can be done that will defeat a regex but still be valid c++.</p>
| 0 | 2009-06-15T16:29:55Z | [
"c++",
"python",
"regex",
"namespaces"
] |
Using python regex to extract namespaces from C++ sources | 995,165 | <p>I am trying to extract the namespaces defined in C++ files.<br />
Basically, if my C++ file contains:</p>
<pre>
namespace n1 {
...
namespace n2 { ... } // end namespace n2
...
namespace n3 { ...} //end namespace n3
...
} //end namespace n1
</pre>
<p>I want to be able to retrieve: n1, n1::n2, n1::n3.</p... | 1 | 2009-06-15T09:15:37Z | 997,076 | <p>You could write a basic lexer for it. It's not that hard.</p>
| 1 | 2009-06-15T16:32:19Z | [
"c++",
"python",
"regex",
"namespaces"
] |
Using python regex to extract namespaces from C++ sources | 995,165 | <p>I am trying to extract the namespaces defined in C++ files.<br />
Basically, if my C++ file contains:</p>
<pre>
namespace n1 {
...
namespace n2 { ... } // end namespace n2
...
namespace n3 { ...} //end namespace n3
...
} //end namespace n1
</pre>
<p>I want to be able to retrieve: n1, n1::n2, n1::n3.</p... | 1 | 2009-06-15T09:15:37Z | 997,110 | <p>The need is simple enough that you may not need a complex parser. You need to:</p>
<ul>
<li>extract the namespace names</li>
<li>count the open/close braces to keep track of where your namespace is defined.</li>
</ul>
<p>This simple approach works if the other conditions are met:</p>
<ul>
<li>you don't get spurio... | 2 | 2009-06-15T16:41:41Z | [
"c++",
"python",
"regex",
"namespaces"
] |
Using python regex to extract namespaces from C++ sources | 995,165 | <p>I am trying to extract the namespaces defined in C++ files.<br />
Basically, if my C++ file contains:</p>
<pre>
namespace n1 {
...
namespace n2 { ... } // end namespace n2
...
namespace n3 { ...} //end namespace n3
...
} //end namespace n1
</pre>
<p>I want to be able to retrieve: n1, n1::n2, n1::n3.</p... | 1 | 2009-06-15T09:15:37Z | 998,230 | <p>That is what I did earlier today: </p>
<ul>
<li>Extract the comment out of the C++ files</li>
<li>Use regex to extract the namespace definition</li>
<li>Use a simple string search to get the open & close braces positions</li>
</ul>
<p>The various sanity check added show that I am successfully processing 99.... | 0 | 2009-06-15T20:34:52Z | [
"c++",
"python",
"regex",
"namespaces"
] |
How can this be written on a single line? | 995,234 | <p>I've seen some Python list comprehensions before, but can this be done in a single line of Python?</p>
<pre><code>errs = {}
for f in form:
if f.errors:
errs[f.auto_id] = f.errors
</code></pre>
| 3 | 2009-06-15T09:38:48Z | 995,238 | <pre><code>errs = dict((f.auto_id, f.errors) for f in form if f.errors)
</code></pre>
| 20 | 2009-06-15T09:40:24Z | [
"python",
"django",
"dictionary",
"list-comprehension"
] |
How can this be written on a single line? | 995,234 | <p>I've seen some Python list comprehensions before, but can this be done in a single line of Python?</p>
<pre><code>errs = {}
for f in form:
if f.errors:
errs[f.auto_id] = f.errors
</code></pre>
| 3 | 2009-06-15T09:38:48Z | 995,239 | <p>It probably could be, but as per the âReadability counts.â rule (<a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow">PEP 20</a>), I'd say it's a bad idea. :)</p>
<p>On the other hand you have âFlat is better than nested.â and âSparse is better than dense.â, so I guess it's a matter of tast... | 4 | 2009-06-15T09:40:44Z | [
"python",
"django",
"dictionary",
"list-comprehension"
] |
How can this be written on a single line? | 995,234 | <p>I've seen some Python list comprehensions before, but can this be done in a single line of Python?</p>
<pre><code>errs = {}
for f in form:
if f.errors:
errs[f.auto_id] = f.errors
</code></pre>
| 3 | 2009-06-15T09:38:48Z | 995,685 | <p>Python 3.0 has dict comprehensions as a shorter/more readable form of the anser provided by Steef:</p>
<pre><code>errs = {f.auto_id: f.errors for f in form if f.errors}
</code></pre>
| 9 | 2009-06-15T11:50:21Z | [
"python",
"django",
"dictionary",
"list-comprehension"
] |
How can this be written on a single line? | 995,234 | <p>I've seen some Python list comprehensions before, but can this be done in a single line of Python?</p>
<pre><code>errs = {}
for f in form:
if f.errors:
errs[f.auto_id] = f.errors
</code></pre>
| 3 | 2009-06-15T09:38:48Z | 996,238 | <p>Both ways are quite readable, however you should think of future maintainers of the code. Sometimes explicit is better. List comprehensions rule though :)</p>
| 0 | 2009-06-15T13:51:05Z | [
"python",
"django",
"dictionary",
"list-comprehension"
] |
ctypes in python, problem calling a function in a DLL | 995,332 | <p>Hey! as you might have noticed I have an annoying issue with ctypes. I'm trying to communicate with an instrument and to do so I have to use ctypes to communicate with the DLL driver.</p>
<p>so far I've managed to export the DLL by doing this:</p>
<pre><code>>>> from ctypes import *
>>>maury = Wi... | 0 | 2009-06-15T10:10:34Z | 995,384 | <p>I figure it's the value you pass at the <code>long max_range[]</code> argument. The function expects a pointer to a <code>long</code> integer there (it asks for an array of <code>long</code> integers), but you're passing a long <em>value</em> of zero (result of the <code>c_long()</code> call), which is implicitly ca... | 3 | 2009-06-15T10:23:17Z | [
"python",
"dll",
"pointers",
"ctypes"
] |
mod_python caching of variables | 995,416 | <p><strong>I'm using mod_python to run Trac in Apache. I'm developing a plugin and am not sure how global variables are stored/cached.</strong></p>
<p>I am new to python and have googled the subject and found that mod_python caches python modules (I think). However, I would expect that cache to be reset when the web s... | 1 | 2009-06-15T10:32:44Z | 995,436 | <p>read the mod-python faq it says</p>
<blockquote>
<p>Global objects live inside mod_python
for the life of the apache process,
which in general is much longer than
the life of a single request. This
means if you expect a global variable
to be initialised every time you will
be surprised....</p>
</block... | 2 | 2009-06-15T10:38:31Z | [
"python",
"caching",
"trac",
"mod-python"
] |
mod_python caching of variables | 995,416 | <p><strong>I'm using mod_python to run Trac in Apache. I'm developing a plugin and am not sure how global variables are stored/cached.</strong></p>
<p>I am new to python and have googled the subject and found that mod_python caches python modules (I think). However, I would expect that cache to be reset when the web s... | 1 | 2009-06-15T10:32:44Z | 995,711 | <p>Obligatory:</p>
<p>Switch to <a href="http://wsgi.org" rel="nofollow">wsgi</a> using <a href="http://code.google.com/p/modwsgi/" rel="nofollow"><code>mod_wsgi</code></a>. </p>
<p><a href="http://wiki.python.org/moin/PoundPythonWeb/mod%5Fpython" rel="nofollow">Don't use <code>mod_python</code></a>.</p>
<p>There is... | 3 | 2009-06-15T11:56:54Z | [
"python",
"caching",
"trac",
"mod-python"
] |
KOI8-R: Having trouble translating a string | 995,531 | <p>This Python script gets translit for Russian letters:</p>
<pre><code>s = u'Ðод Ðбмена ÐнÑоÑмаÑией, 8 биÑ'.encode('koi8-r')
print ''.join([chr(ord(c) & 0x7F) for c in s]) # kOD oBMENA iNFORMACIEJ, 8 BIT
</code></pre>
<p>That works. But I want to modify it so as to get user input. Now I'm ... | 1 | 2009-06-15T11:06:39Z | 995,564 | <p><code>s = unicode(s)</code> expects ascii encoding by default. You need to supply it an encoding your input is in, e.g. <code>s = unicode(s, 'utf-8')</code>.</p>
| 2 | 2009-06-15T11:15:07Z | [
"python",
"encoding"
] |
KOI8-R: Having trouble translating a string | 995,531 | <p>This Python script gets translit for Russian letters:</p>
<pre><code>s = u'Ðод Ðбмена ÐнÑоÑмаÑией, 8 биÑ'.encode('koi8-r')
print ''.join([chr(ord(c) & 0x7F) for c in s]) # kOD oBMENA iNFORMACIEJ, 8 BIT
</code></pre>
<p>That works. But I want to modify it so as to get user input. Now I'm ... | 1 | 2009-06-15T11:06:39Z | 995,566 | <p>try <code>unicode(s, encoding)</code> where encoding is whatever your terminal is in.</p>
| 1 | 2009-06-15T11:15:28Z | [
"python",
"encoding"
] |
KOI8-R: Having trouble translating a string | 995,531 | <p>This Python script gets translit for Russian letters:</p>
<pre><code>s = u'Ðод Ðбмена ÐнÑоÑмаÑией, 8 биÑ'.encode('koi8-r')
print ''.join([chr(ord(c) & 0x7F) for c in s]) # kOD oBMENA iNFORMACIEJ, 8 BIT
</code></pre>
<p>That works. But I want to modify it so as to get user input. Now I'm ... | 1 | 2009-06-15T11:06:39Z | 999,351 | <p>Looking at the error messages that you are seeing, it seems to me that your terminal encoding is probably set to KOI8-R, in which case you don't need to perform any decoding on the input data. If this is the case then all you need is:</p>
<pre><code>>>> s = raw_input("Enter a string you want to translit: "... | 0 | 2009-06-16T02:30:56Z | [
"python",
"encoding"
] |
Using data from django queries in the same view | 995,970 | <p>I might have missed somthing while searching through the documentation - I can't seem to find a way to use data from one query to form another query.</p>
<p>My query is:</p>
<pre><code>sites_list = Site.objects.filter(worker=worker)
</code></pre>
<p>I'm trying to do something like this:</p>
<pre><code>for site i... | 1 | 2009-06-15T12:59:31Z | 995,992 | <p>You could easily do something like this:</p>
<pre><code>sites_list = Site.objects.filter(worker=worker)
for site in sites_list:
new_sites_list = Site.objects.filter(name=site.name).filter(something else)
</code></pre>
| 2 | 2009-06-15T13:03:54Z | [
"python",
"django"
] |
Using data from django queries in the same view | 995,970 | <p>I might have missed somthing while searching through the documentation - I can't seem to find a way to use data from one query to form another query.</p>
<p>My query is:</p>
<pre><code>sites_list = Site.objects.filter(worker=worker)
</code></pre>
<p>I'm trying to do something like this:</p>
<pre><code>for site i... | 1 | 2009-06-15T12:59:31Z | 1,019,848 | <p>You can also use the <code>__in</code> lookup type. For example, if you had an <code>Entry</code> model with a relation to <code>Site</code>, you could write:</p>
<pre><code>Entry.objects.filter(site__in=Site.objects.filter(...some conditions...))
</code></pre>
<p>This will end up doing one query in the DB (the fi... | 0 | 2009-06-19T20:16:01Z | [
"python",
"django"
] |
IOError "no such file or folder" even though files are present | 995,985 | <p>I wrote a script in Python 2.6.2 that scans a directory for SVG's and resizes them if they are too large. I wrote this on my home machine (Vista, Python 2.6.2) and processed a few folders with no problems. Today, I tried this on my work computer (XP SP2, Python 2.6.2) and I get IOErrors for every file, even though... | 0 | 2009-06-15T13:02:36Z | 996,001 | <p>Perhaps it's a security issue? Perhaps you don't have the rights to create files in the folder</p>
| 0 | 2009-06-15T13:05:37Z | [
"python"
] |
IOError "no such file or folder" even though files are present | 995,985 | <p>I wrote a script in Python 2.6.2 that scans a directory for SVG's and resizes them if they are too large. I wrote this on my home machine (Vista, Python 2.6.2) and processed a few folders with no problems. Today, I tried this on my work computer (XP SP2, Python 2.6.2) and I get IOErrors for every file, even though... | 0 | 2009-06-15T13:02:36Z | 996,021 | <p>It looks to me like you are missing a <code>os.path.join(path, tfile)</code> to get the full path to the file you want to open. Currently it should only work for files in the current directory.</p>
| 1 | 2009-06-15T13:11:10Z | [
"python"
] |
Enumeration of combinations of N balls in A boxes? | 996,004 | <p>I want to enumerate all possible combinations of <strong><em>N</em></strong> balls in <strong><em>A</em></strong> boxes.</p>
<p>example:
I have <strong><em>8</em></strong> balls to deal in <strong><em>3</em></strong> boxes :</p>
<pre><code> box_1 box_2 box_3
case-1 8 0 0
case-2 ... | 1 | 2009-06-15T13:06:22Z | 996,030 | <p>Pseudocode:</p>
<pre><code>Enumerate(Balls, Boxes)
if Boxes<=0
Error
elseif Boxes=1
Box[1] = Balls
PrintBoxes
else
forall b in 0..Balls
Box[Boxes] = b
Enumerate(Balls-b, Boxes-1)
endfor
endif
end
</code></pre>
<p>Explanation</p>
<p>Start at the first box, if there are... | 3 | 2009-06-15T13:12:49Z | [
"python",
"enumeration",
"combinations"
] |
Enumeration of combinations of N balls in A boxes? | 996,004 | <p>I want to enumerate all possible combinations of <strong><em>N</em></strong> balls in <strong><em>A</em></strong> boxes.</p>
<p>example:
I have <strong><em>8</em></strong> balls to deal in <strong><em>3</em></strong> boxes :</p>
<pre><code> box_1 box_2 box_3
case-1 8 0 0
case-2 ... | 1 | 2009-06-15T13:06:22Z | 996,070 | <p>if you want to use your own function answer by Gamecat may work
else
either use <a href="http://probstat.sourceforge.net/" rel="nofollow">http://probstat.sourceforge.net/</a> , it is very fast (written in c)</p>
<p>or itertools in python 2.6</p>
| 0 | 2009-06-15T13:20:05Z | [
"python",
"enumeration",
"combinations"
] |
Enumeration of combinations of N balls in A boxes? | 996,004 | <p>I want to enumerate all possible combinations of <strong><em>N</em></strong> balls in <strong><em>A</em></strong> boxes.</p>
<p>example:
I have <strong><em>8</em></strong> balls to deal in <strong><em>3</em></strong> boxes :</p>
<pre><code> box_1 box_2 box_3
case-1 8 0 0
case-2 ... | 1 | 2009-06-15T13:06:22Z | 996,246 | <p>This works just fine starting with python 2.6, (<a href="http://docs.python.org/library/itertools.html#itertools.permutations" rel="nofollow">2.5-friendly implementation of <code>itertools.permutations</code> is available as well</a>):</p>
<pre><code>>>> import itertools
>>> boxes = 3
>>>... | 5 | 2009-06-15T13:52:43Z | [
"python",
"enumeration",
"combinations"
] |
Enumeration of combinations of N balls in A boxes? | 996,004 | <p>I want to enumerate all possible combinations of <strong><em>N</em></strong> balls in <strong><em>A</em></strong> boxes.</p>
<p>example:
I have <strong><em>8</em></strong> balls to deal in <strong><em>3</em></strong> boxes :</p>
<pre><code> box_1 box_2 box_3
case-1 8 0 0
case-2 ... | 1 | 2009-06-15T13:06:22Z | 996,351 | <p>You can define a recursive <a href="http://docs.python.org/tutorial/classes.html#generators" rel="nofollow">generator</a> which creates a sub-generator for each 'for loop' which you wish to nest, like this:</p>
<pre><code>def ballsAndBoxes(balls, boxes, boxIndex=0, sumThusFar=0):
if boxIndex < (boxes - 1):
... | 1 | 2009-06-15T14:15:03Z | [
"python",
"enumeration",
"combinations"
] |
Enumeration of combinations of N balls in A boxes? | 996,004 | <p>I want to enumerate all possible combinations of <strong><em>N</em></strong> balls in <strong><em>A</em></strong> boxes.</p>
<p>example:
I have <strong><em>8</em></strong> balls to deal in <strong><em>3</em></strong> boxes :</p>
<pre><code> box_1 box_2 box_3
case-1 8 0 0
case-2 ... | 1 | 2009-06-15T13:06:22Z | 998,153 | <p>If you simply want to know the number of possibilities, instead of listing them, then the following formula will work:</p>
<blockquote>
<p>Possibilities = (N+A-1) C N = (N+A-1)!/(N!x(A-1)!)</p>
</blockquote>
<p>Where aCb (a choose b) is the number of ways of choosing combinations of size b from a set of size a. ... | 0 | 2009-06-15T20:16:54Z | [
"python",
"enumeration",
"combinations"
] |
Enumeration of combinations of N balls in A boxes? | 996,004 | <p>I want to enumerate all possible combinations of <strong><em>N</em></strong> balls in <strong><em>A</em></strong> boxes.</p>
<p>example:
I have <strong><em>8</em></strong> balls to deal in <strong><em>3</em></strong> boxes :</p>
<pre><code> box_1 box_2 box_3
case-1 8 0 0
case-2 ... | 1 | 2009-06-15T13:06:22Z | 999,234 | <p>See <a href="http://docs.python.org/dev/py3k/library/itertools.html#itertools.combinations%5Fwith%5Freplacement" rel="nofollow">itertools.combinations_with_replacement</a> in 3.1 for an example written in python. Additionally, it's common in combinatorics to transform a combination-with-replacement problem into the... | 1 | 2009-06-16T01:24:45Z | [
"python",
"enumeration",
"combinations"
] |
Enumeration of combinations of N balls in A boxes? | 996,004 | <p>I want to enumerate all possible combinations of <strong><em>N</em></strong> balls in <strong><em>A</em></strong> boxes.</p>
<p>example:
I have <strong><em>8</em></strong> balls to deal in <strong><em>3</em></strong> boxes :</p>
<pre><code> box_1 box_2 box_3
case-1 8 0 0
case-2 ... | 1 | 2009-06-15T13:06:22Z | 34,813,958 | <p>It is good idea to use python generator, as it was done above but here is more straightforward (not sure about efficency) version:</p>
<pre><code>def balls_in_baskets(balls=1, baskets=1):
if baskets == 1:
yield [balls]
elif balls == 0:
yield [0]*baskets
else:
for i in xrange(ball... | 1 | 2016-01-15T15:02:36Z | [
"python",
"enumeration",
"combinations"
] |
py2exe windows service problem | 996,129 | <p>I have successfully converted my python project to a service. When using the usual options of install and start/stop, everything works correctly. However, I wish to compile the project using py2exe, which seems to work correctly until you install the EXE as a service and try and run it.</p>
<p>You get the followi... | 6 | 2009-06-15T13:31:04Z | 996,645 | <p>You will find an example in the py2exe package, look in site-packages\py2exe\samples\advanced.</p>
| 1 | 2009-06-15T15:07:59Z | [
"python",
"windows-services",
"py2exe"
] |
py2exe windows service problem | 996,129 | <p>I have successfully converted my python project to a service. When using the usual options of install and start/stop, everything works correctly. However, I wish to compile the project using py2exe, which seems to work correctly until you install the EXE as a service and try and run it.</p>
<p>You get the followi... | 6 | 2009-06-15T13:31:04Z | 996,667 | <p>You setup.py file should contain</p>
<pre><code>setup(service=["webserver.py"])
</code></pre>
<p>as shown in the <a href="http://www.py2exe.org/old/" rel="nofollow">"old" py2exe docs</a></p>
| 4 | 2009-06-15T15:10:03Z | [
"python",
"windows-services",
"py2exe"
] |
Parsing an existing config file | 996,183 | <p>I have a config file that is in the following form:</p>
<pre><code>protocol sample_thread {
{ AUTOSTART 0 }
{ BITMAP thread.gif }
{ COORDS {0 0} }
{ DATAFORMAT {
{ TYPE hl7 }
{ PREPROCS {
{ ARGS {{}} }
{ PROCS sample_proc }
} }
} }
}
</code></pre>... | 4 | 2009-06-15T13:43:23Z | 996,270 | <p>Maybe you could write a simple script that will convert your config into xml file and then read it just using lxml, Beatuful Soup or anything else? And your converter could use PyParsing or regular expressions for example.</p>
| -1 | 2009-06-15T13:58:00Z | [
"python",
"parsing",
"config"
] |
Parsing an existing config file | 996,183 | <p>I have a config file that is in the following form:</p>
<pre><code>protocol sample_thread {
{ AUTOSTART 0 }
{ BITMAP thread.gif }
{ COORDS {0 0} }
{ DATAFORMAT {
{ TYPE hl7 }
{ PREPROCS {
{ ARGS {{}} }
{ PROCS sample_proc }
} }
} }
}
</code></pre>... | 4 | 2009-06-15T13:43:23Z | 996,275 | <p>I'll try and answer what I think is the missing question(s)...</p>
<p>Configuration files come in many formats. There are well known formats such as *.ini or apache config - these tend to have many parsers available.</p>
<p>Then there are custom formats. That is what yours appears to be (it could be some well-de... | 1 | 2009-06-15T13:58:51Z | [
"python",
"parsing",
"config"
] |
Parsing an existing config file | 996,183 | <p>I have a config file that is in the following form:</p>
<pre><code>protocol sample_thread {
{ AUTOSTART 0 }
{ BITMAP thread.gif }
{ COORDS {0 0} }
{ DATAFORMAT {
{ TYPE hl7 }
{ PREPROCS {
{ ARGS {{}} }
{ PROCS sample_proc }
} }
} }
}
</code></pre>... | 4 | 2009-06-15T13:43:23Z | 996,306 | <p>I searched a little on the <a href="http://pypi.python.org/pypi" rel="nofollow">Cheese Shop</a>, but I didn't find anything helpful for your example. Check the <a href="http://pyparsing.wikispaces.com/Examples" rel="nofollow">Examples</a> page, and <a href="http://pyparsing.wikispaces.com/file/view/dhcpd%5Fleases%5F... | 0 | 2009-06-15T14:04:44Z | [
"python",
"parsing",
"config"
] |
Parsing an existing config file | 996,183 | <p>I have a config file that is in the following form:</p>
<pre><code>protocol sample_thread {
{ AUTOSTART 0 }
{ BITMAP thread.gif }
{ COORDS {0 0} }
{ DATAFORMAT {
{ TYPE hl7 }
{ PREPROCS {
{ ARGS {{}} }
{ PROCS sample_proc }
} }
} }
}
</code></pre>... | 4 | 2009-06-15T13:43:23Z | 996,342 | <p>Look into <a href="http://dinosaur.compilertools.net/" rel="nofollow">LEX and YACC</a>. A bit of a learning curve, but they can generate parsers for any language.</p>
| 0 | 2009-06-15T14:13:24Z | [
"python",
"parsing",
"config"
] |
Parsing an existing config file | 996,183 | <p>I have a config file that is in the following form:</p>
<pre><code>protocol sample_thread {
{ AUTOSTART 0 }
{ BITMAP thread.gif }
{ COORDS {0 0} }
{ DATAFORMAT {
{ TYPE hl7 }
{ PREPROCS {
{ ARGS {{}} }
{ PROCS sample_proc }
} }
} }
}
</code></pre>... | 4 | 2009-06-15T13:43:23Z | 996,366 | <p>You can easily write a script in python which will convert it to python dict, format looks almost like hierarchical name value pairs, only problem seems to be
Coards {0 0}, where {0 0} isn't a name value pair, but a list
so who know what other such cases are in the format
I think your best bet is to have spec for th... | 1 | 2009-06-15T14:18:36Z | [
"python",
"parsing",
"config"
] |
Parsing an existing config file | 996,183 | <p>I have a config file that is in the following form:</p>
<pre><code>protocol sample_thread {
{ AUTOSTART 0 }
{ BITMAP thread.gif }
{ COORDS {0 0} }
{ DATAFORMAT {
{ TYPE hl7 }
{ PREPROCS {
{ ARGS {{}} }
{ PROCS sample_proc }
} }
} }
}
</code></pre>... | 4 | 2009-06-15T13:43:23Z | 996,461 | <p><a href="http://pyparsing.wikispaces.com/Introduction" rel="nofollow">pyparsing</a> is pretty handy for quick and simple parsing like this. A bare minimum would be something like:</p>
<pre><code>import pyparsing
string = pyparsing.CharsNotIn("{} \t\r\n")
group = pyparsing.Forward()
group << pyparsing.Group(p... | 11 | 2009-06-15T14:35:14Z | [
"python",
"parsing",
"config"
] |
Parsing an existing config file | 996,183 | <p>I have a config file that is in the following form:</p>
<pre><code>protocol sample_thread {
{ AUTOSTART 0 }
{ BITMAP thread.gif }
{ COORDS {0 0} }
{ DATAFORMAT {
{ TYPE hl7 }
{ PREPROCS {
{ ARGS {{}} }
{ PROCS sample_proc }
} }
} }
}
</code></pre>... | 4 | 2009-06-15T13:43:23Z | 996,513 | <p>Your config file is very similar to <a href="http://www.json.org/" rel="nofollow">JSON</a> (pretty much, replace all your "{" and "}" with "[" and "]"). Most languages have a built in JSON parser (PHP, Ruby, Python, etc), and if not, there are libraries available to handle it for you.</p>
<p>If you can not change ... | 1 | 2009-06-15T14:44:06Z | [
"python",
"parsing",
"config"
] |
Parsing an existing config file | 996,183 | <p>I have a config file that is in the following form:</p>
<pre><code>protocol sample_thread {
{ AUTOSTART 0 }
{ BITMAP thread.gif }
{ COORDS {0 0} }
{ DATAFORMAT {
{ TYPE hl7 }
{ PREPROCS {
{ ARGS {{}} }
{ PROCS sample_proc }
} }
} }
}
</code></pre>... | 4 | 2009-06-15T13:43:23Z | 1,351,502 | <p>Taking Brian's pyparsing solution another step, you can create a quasi-deserializer for this format by using the Dict class:</p>
<pre><code>import pyparsing
string = pyparsing.CharsNotIn("{} \t\r\n")
# use Word instead of CharsNotIn, to do whitespace skipping
stringchars = pyparsing.printables.replace("{","").repl... | 2 | 2009-08-29T13:57:04Z | [
"python",
"parsing",
"config"
] |
Regex in Python | 996,536 | <p>Goal: Given a number (it may be very long and it is greater than 0), I'd like to get the five least meaningful digits dropping any 0 at the end of that number.</p>
<p>I tried to solve this with regex, Helped by RegexBuddy I came to this one:</p>
<pre><code>[\d]+([\d]{0,4}+[1-9])0*
</code></pre>
<p>But python can'... | 1 | 2009-06-15T14:48:59Z | 996,581 | <p>Small tip. I recommend you test with <a href="http://cthedot.de/retest/" rel="nofollow">reTest</a> instead of RegExBuddy. There are different regular expression engines for different programming languages. ReTest is valuable in that it allows you to quickly test regular expression strings within Python itself. Th... | 2 | 2009-06-15T14:56:55Z | [
"python",
"regex",
"regexbuddy"
] |
Regex in Python | 996,536 | <p>Goal: Given a number (it may be very long and it is greater than 0), I'd like to get the five least meaningful digits dropping any 0 at the end of that number.</p>
<p>I tried to solve this with regex, Helped by RegexBuddy I came to this one:</p>
<pre><code>[\d]+([\d]{0,4}+[1-9])0*
</code></pre>
<p>But python can'... | 1 | 2009-06-15T14:48:59Z | 996,612 | <p>The error seems to be that you have two quantifiers in a row, {0,4} and +. Unless + is meant to be a literal here (which I doubt, since you're talking about numbers), then I don't think you need it at all. Unless it means something different in this situation (possibly the greediness of the {} quantifier)? I woul... | 0 | 2009-06-15T15:02:18Z | [
"python",
"regex",
"regexbuddy"
] |
Regex in Python | 996,536 | <p>Goal: Given a number (it may be very long and it is greater than 0), I'd like to get the five least meaningful digits dropping any 0 at the end of that number.</p>
<p>I tried to solve this with regex, Helped by RegexBuddy I came to this one:</p>
<pre><code>[\d]+([\d]{0,4}+[1-9])0*
</code></pre>
<p>But python can'... | 1 | 2009-06-15T14:48:59Z | 996,613 | <p>That regular expression is very superfluous. Try this:</p>
<pre><code>>>> import re
>>> re.compile(r"(\d{0,4}[1-9])0*$")
</code></pre>
<p>The above regular expression assumes that the number is valid (it will also match "abc<strong>012345</strong>0", for example.) If you really need the validatio... | 10 | 2009-06-15T15:02:28Z | [
"python",
"regex",
"regexbuddy"
] |
Regex in Python | 996,536 | <p>Goal: Given a number (it may be very long and it is greater than 0), I'd like to get the five least meaningful digits dropping any 0 at the end of that number.</p>
<p>I tried to solve this with regex, Helped by RegexBuddy I came to this one:</p>
<pre><code>[\d]+([\d]{0,4}+[1-9])0*
</code></pre>
<p>But python can'... | 1 | 2009-06-15T14:48:59Z | 1,001,910 | <p>\d{0,4}+ is a possessive quantifier supported by certain regular expression flavors such as .NET and Java. Python does not support possessive quantifiers.</p>
<p>In RegexBuddy, select Python in the toolbar at the top, and RegexBuddy will tell you that Python doesn't support possessive quantifiers. The + will be h... | 5 | 2009-06-16T14:38:24Z | [
"python",
"regex",
"regexbuddy"
] |
Regex in Python | 996,536 | <p>Goal: Given a number (it may be very long and it is greater than 0), I'd like to get the five least meaningful digits dropping any 0 at the end of that number.</p>
<p>I tried to solve this with regex, Helped by RegexBuddy I came to this one:</p>
<pre><code>[\d]+([\d]{0,4}+[1-9])0*
</code></pre>
<p>But python can'... | 1 | 2009-06-15T14:48:59Z | 12,403,996 | <p>This is my solution.</p>
<pre><code>re.search(r'[1-9]\d{0,3}[1-9](?=0*(?:\b|\s|[A-Za-z]))', '02324560001230045980a').group(1)
</code></pre>
<p>'4598'</p>
<ul>
<li><code>[1-9]</code> - the number must start with 1 - 9</li>
<li><code>\d{0,3}</code> - 0 or 3 digits</li>
<li><code>[1-9]</code> - the number must finis... | 0 | 2012-09-13T10:13:53Z | [
"python",
"regex",
"regexbuddy"
] |
How can i write my own aggregate functions with sqlalchemy? | 996,922 | <p>How can I write my own aggregate functions with SQLAlchemy? As an easy example I would like to use numpy to calculate the variance. With sqlite it would look like this:</p>
<pre><code>import sqlite3 as sqlite
import numpy as np
class self_written_SQLvar(object):
def __init__(self):
import numpy as np
sel... | 4 | 2009-06-15T15:59:46Z | 997,467 | <p>The creation of new aggregate functions is backend-dependant, and must be done
directly with the API of the underlining connection. SQLAlchemy offers no
facility for creating those.</p>
<p>However after created you can just use them in SQLAlchemy normally.</p>
<p>Example:</p>
<pre><code>import sqlalchemy
from s... | 8 | 2009-06-15T18:01:31Z | [
"python",
"sqlite",
"sqlalchemy",
"aggregate-functions"
] |
How can i write my own aggregate functions with sqlalchemy? | 996,922 | <p>How can I write my own aggregate functions with SQLAlchemy? As an easy example I would like to use numpy to calculate the variance. With sqlite it would look like this:</p>
<pre><code>import sqlite3 as sqlite
import numpy as np
class self_written_SQLvar(object):
def __init__(self):
import numpy as np
sel... | 4 | 2009-06-15T15:59:46Z | 1,052,543 | <p>at first you have to import func from sqlalchemy</p>
<p>you can write </p>
<p>func.avg('fieldname')</p>
<p>or func.avg('fieldname').label('user_deined') </p>
<p>or you can go thru for mre information </p>
<p><a href="http://www.sqlalchemy.org/docs/05/ormtutorial.html#using-subqueries" rel="nofollow">http://www.... | 1 | 2009-06-27T10:07:39Z | [
"python",
"sqlite",
"sqlalchemy",
"aggregate-functions"
] |
Refactor this block cipher keying function | 996,965 | <p>I found a simple pure python blowfish implementation that meets my needs for a particular project.</p>
<p>There's just one part of it that bothers me:</p>
<pre><code>def initialize(key):
"""
Use key to setup subkeys -- requires 521 encryptions
to set p and s boxes. key is a hex number corresponding
... | 1 | 2009-06-15T16:10:33Z | 997,012 | <p>Yes. Use int() with a base of 16.</p>
<pre><code>>>> int('ffffffff',16)
4294967295L
</code></pre>
<p>so:</p>
<pre><code>subkey = int(hexkey[pos:pos+8], 16)
</code></pre>
<p>should do the same thing without needing eval.</p>
<p><strong>[Edit]</strong> In fact, there's generally no reason why you'd need... | 5 | 2009-06-15T16:19:31Z | [
"python",
"encryption"
] |
Refactor this block cipher keying function | 996,965 | <p>I found a simple pure python blowfish implementation that meets my needs for a particular project.</p>
<p>There's just one part of it that bothers me:</p>
<pre><code>def initialize(key):
"""
Use key to setup subkeys -- requires 521 encryptions
to set p and s boxes. key is a hex number corresponding
... | 1 | 2009-06-15T16:10:33Z | 997,030 | <p>An alternative is "int('0x111', 0)". int's second argument is the base. "0" means "use the usual rules: no prefix is decimal, 0 prefix is octal and 0x is hexa -- just like eval".</p>
<p>This is the preferred way to "emulate" the eval operation for intifying strings.</p>
| 1 | 2009-06-15T16:22:27Z | [
"python",
"encryption"
] |
Refactor this block cipher keying function | 996,965 | <p>I found a simple pure python blowfish implementation that meets my needs for a particular project.</p>
<p>There's just one part of it that bothers me:</p>
<pre><code>def initialize(key):
"""
Use key to setup subkeys -- requires 521 encryptions
to set p and s boxes. key is a hex number corresponding
... | 1 | 2009-06-15T16:10:33Z | 997,032 | <p>You can do this with the long function:</p>
<pre><code>subkey = long(hexkey[pos:pos+8], 16)
</code></pre>
<p>From <code>help(long)</code>:</p>
<blockquote>
<p>class long(object)<br />
| long(x[, base]) -> integer<br />
|<br />
| Convert a string or number to a long integer, if possible. A floating<b... | 0 | 2009-06-15T16:22:51Z | [
"python",
"encryption"
] |
Refactor this block cipher keying function | 996,965 | <p>I found a simple pure python blowfish implementation that meets my needs for a particular project.</p>
<p>There's just one part of it that bothers me:</p>
<pre><code>def initialize(key):
"""
Use key to setup subkeys -- requires 521 encryptions
to set p and s boxes. key is a hex number corresponding
... | 1 | 2009-06-15T16:10:33Z | 997,758 | <p><strong>Don't use this code, much less try to improve it.</strong></p>
<p>Using crypto code found on the internet is likely to cause serious security failures in your software. See <a href="http://www.codinghorror.com/blog/archives/001275.html" rel="nofollow">Jeff Atwood's little series</a> on the topic.</p>
<p>It... | 3 | 2009-06-15T19:00:14Z | [
"python",
"encryption"
] |
Unit testing for exceptions in Python constructor | 997,244 | <p>I'm just a beginner in Python and programming in general, and have some questions about the unittest module. </p>
<p>I have a class, and in the <code>__init__</code> method I am doing some assertions to check for bad arguments. I would like to create a unittest which checks for such AssertionError when creating new... | 7 | 2009-06-15T17:10:21Z | 997,281 | <p>Well, as a start, checking for bad arguments is not a good idea in python. Python is dynamically strong typed for a reason.</p>
<p>You should just assume that the arguments are good arguments. You never know your class' users' intent so by checking for good arguments is a way to limit usage of your class in more ge... | 1 | 2009-06-15T17:18:20Z | [
"python",
"unit-testing",
"constructor"
] |
Unit testing for exceptions in Python constructor | 997,244 | <p>I'm just a beginner in Python and programming in general, and have some questions about the unittest module. </p>
<p>I have a class, and in the <code>__init__</code> method I am doing some assertions to check for bad arguments. I would like to create a unittest which checks for such AssertionError when creating new... | 7 | 2009-06-15T17:10:21Z | 997,332 | <p>Don't mess with assertRaises. It's too complicated.</p>
<p>Do this</p>
<pre><code>class Test_Init( unittest.TestCase ):
def test_something( self ):
try:
x= Something( "This Should Fail" )
self.fail( "Didn't raise AssertionError" )
except AssertionError, e:
s... | 5 | 2009-06-15T17:30:42Z | [
"python",
"unit-testing",
"constructor"
] |
Unit testing for exceptions in Python constructor | 997,244 | <p>I'm just a beginner in Python and programming in general, and have some questions about the unittest module. </p>
<p>I have a class, and in the <code>__init__</code> method I am doing some assertions to check for bad arguments. I would like to create a unittest which checks for such AssertionError when creating new... | 7 | 2009-06-15T17:10:21Z | 997,353 | <blockquote>
<p>In the unittest module, one can test (with <code>assertRaises</code>) for specific exception when a callable is called, but obviously that will apply to methods of the class. What is the proper way to run such test for the constructor?</p>
</blockquote>
<p>The constructor is itself a callable:</p>
<... | 13 | 2009-06-15T17:34:49Z | [
"python",
"unit-testing",
"constructor"
] |
Unit testing for exceptions in Python constructor | 997,244 | <p>I'm just a beginner in Python and programming in general, and have some questions about the unittest module. </p>
<p>I have a class, and in the <code>__init__</code> method I am doing some assertions to check for bad arguments. I would like to create a unittest which checks for such AssertionError when creating new... | 7 | 2009-06-15T17:10:21Z | 1,454,676 | <p>S.Lott's answer is incorrect: self.fail() itself raises an exception, which will then be cause by the except in the following line:</p>
<pre><code>class NetworkConfigTest1(unittest.TestCase):
def runTest(self):
try:
NetworkConfig("192.168.256.0/24")
self.fail("Exception expected ... | 0 | 2009-09-21T14:04:04Z | [
"python",
"unit-testing",
"constructor"
] |
Unit testing for exceptions in Python constructor | 997,244 | <p>I'm just a beginner in Python and programming in general, and have some questions about the unittest module. </p>
<p>I have a class, and in the <code>__init__</code> method I am doing some assertions to check for bad arguments. I would like to create a unittest which checks for such AssertionError when creating new... | 7 | 2009-06-15T17:10:21Z | 15,909,885 | <p>Don't know is this helps, but a problem I was having is as follows:</p>
<pre><code>self.assertRaises(Exception, MyFunction())
</code></pre>
<p>Problem is I was not just passing <code>MyFunction</code>, but calling it as well, resulting in a fail and Exception being raised. <code>Myfunction</code> expects an argume... | 0 | 2013-04-09T18:36:50Z | [
"python",
"unit-testing",
"constructor"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,329 | <p>Bubble sort is a horrible algorithm to sort with. That is quite possibly the reason. If speed is necessary, I would try another algorithm like quick sort or merge sort. </p>
| 25 | 2009-06-15T17:30:01Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,334 | <p>If you're interested in making your own sort, you can change a bubble sort to a comb sort with just a couple lines of code. Comb sort is nearly as good as the best sorts. Of course, making your own sort is best left as a learning exercise.</p>
<blockquote>
<p>Comb sort improves on bubble sort, and
rivals in spe... | 1 | 2009-06-15T17:30:51Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,337 | <p>That doesn't look like bubble sort to me, and if it is, it's a very inefficient implementation of it.</p>
| 1 | 2009-06-15T17:32:05Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,361 | <p>Like the other posts say, bubble sort is horrible. It pretty much should be avoided at all costs due to the bad proformance, like you're experiencing.<br />
Luckily for you there are lots of other sorting algorithms, <a href="http://en.wikipedia.org/wiki/Sorting%5Falgorithm" rel="nofollow">http://en.wikipedia.org/w... | 0 | 2009-06-15T17:36:01Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,396 | <p>That's not quite a bubble sort... unless I've made a trivial error, this would be closer to a python bubble sort:</p>
<pre><code>swapped = True
while swapped:
swapped = False
for i in xrange(len(l)-1):
if l[i] > l[i+1]:
l[i],l[i+1] = l[i+1],l[i]
swapped = True
</code></pre>
<p>Note that the ... | 12 | 2009-06-15T17:43:02Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,427 | <p>Bubble sort may be <em>horrible</em> and <em>slow</em> etc, but would you rather have an O(N^2) algorithm over 100 items, or O(1) one that required a dial-up connection?</p>
<p>And a list of 100 items shouldnt take 2 hours. I don't know python, but are you by any chance copying entire lists when you make those assi... | 6 | 2009-06-15T17:50:32Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,463 | <p>What do you mean by numpy solution ? Numpy has some sort facilities, which are instantenous for those reasonably small arrays:</p>
<pre><code>import numpy as np
a = np.random.randn(100000)
# Take a few ms on a decent computer
np.sort(a)
</code></pre>
<p>There are 3 sorts of sort algorithms available, all are Nlog(... | 5 | 2009-06-15T18:01:21Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,578 | <p>If you must code your own, use an insertion sort. Its about the same amount of code, but several times faster.</p>
| 0 | 2009-06-15T18:24:39Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,613 | <p>For one, you're doing too many loops. Your inner loop should proceed from i + 1 to the end of the list, not from 0. Secondly, as noted by others, bubble sort has a O(N^2) complexity so for 100000 elements, you are looping 10,000,000,000 times. This is compounded by the fact that looping is one of the areas where ... | 2 | 2009-06-15T18:33:27Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,707 | <p>I believe you mentioned that you were trying to use that as a benchmark to compare speeds.</p>
<p>I think generally Python is a bit faster than Ruby, but not really near Java/C/C++/C#. Java is within 2x of the C's, but all the interpreted languages were around 100x slower. </p>
<p>You might Google "Programming L... | 4 | 2009-06-15T18:51:44Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,720 | <p>Because it is going execute the comparison and possibly the swap 100,000 x 100,000 times. If the computer is fast enough to execute the innermost statement 1,000,000 times per second, that still is 167 minutes which is slightly short of 3 hours.</p>
<p>On a side note, why are there so many of these inane questions ... | 1 | 2009-06-15T18:53:53Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,917 | <p>Here some code I put together to compare a base bubble sort against a more streamlined version (base vs modified) - the modified is about 2-3 times faster, still a slow sort, but faster</p>
<pre><code>from array import *
from random import *
from time import *
def randarray(typecode, numElements, minValue, maxValu... | 2 | 2009-06-15T19:28:22Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,984 | <p>I think that you are basically wasting your time using bubble on such a large dataset. There are 3 reasons why it is slow:</p>
<p>1) Python is slow
2) Bubble sort is slow
3) The bubble sort listed is coded incorrectly/inefficiently.</p>
<p>Regardless of how it is coded, it will be O(N^2). Why not use a merge/tree ... | 2 | 2009-06-15T19:43:36Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 997,994 | <p>Bubblesort in general does not scale well to most possible inputs as the number of elements in the input grows. (I.e., it's O(N^2).)</p>
<p>As N grows, given a random input array of size N, you are much less likely to get an array that sorts quickly with bubblesort (e.g., almost sorted arrays). You are far more lik... | 2 | 2009-06-15T19:45:08Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 998,093 | <p>Bubble sort makes O(N<sup>2</sup>) compare operations (or iterations). For N = 100,000, that means that there will be 10,000,000,000 iterations. If that takes 2 hours (call it 10,000 seconds), then it means you get 1,000,000 iterations per second - or 1 microsecond per iteration. That's not great speed, but it i... | 2 | 2009-06-15T20:02:54Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 998,116 | <p>I forgot to add, if you have some idea of the size of the dataset and the distribution of keys then you can use a radix sort which would be O(N). To get the idea of radix sort, consider the case where you are sorting say numbers more or less distributed between 0, 100,000. Then you just create something similar to a... | 0 | 2009-06-15T20:06:50Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 998,201 | <p>You can do</p>
<pre><code>l.reverse()
</code></pre>
<p>Script <em>ee.py</em>:</p>
<pre><code>l = []
for i in xrange(100000):
l.append(i)
l.reverse()
</code></pre>
<p>lyrae@localhost:~/Desktop$ time python ee.py</p>
<pre><code>real 0m0.047s
user 0m0.044s
sys 0m0.004s
</code></pre>
| 0 | 2009-06-15T20:25:56Z | [
"python",
"bubble-sort"
] |
Why is my bubble sort in Python so slow? | 997,322 | <p>I have the following code thats use bubble sort to invert a list and has a worst time performance:</p>
<pre><code>for i in xrange(len(l)):
for j in xrange(len(l)):
if l[i]>l[j]:
l[i], l[j] = l[j], l[i]
</code></pre>
<p>In some cases (when <code>len(l) = 100000</code>) the code spend more... | 0 | 2009-06-15T17:28:22Z | 998,370 | <p>First of all, for the purpose of this reply, I'm assuming - since you claim it yourself - that you're only doing this to benchmark different languages. So I won't go into "bubble sort is just slow" territory. The real question is why it's so much slower in Python.</p>
<p>The answer is that Python is inherently much... | 1 | 2009-06-15T21:01:04Z | [
"python",
"bubble-sort"
] |
Any yahoo messenger lib for python? | 997,419 | <p>Is there any lib available to connect to yahoo messenger using either the standard protocol or the http way from python? </p>
| 2 | 2009-06-15T17:48:32Z | 997,444 | <p>Google is your friend.</p>
<p>The <a href="http://pypi.python.org/pypi">Python Package Index</a> has <a href="http://pypi.python.org/pypi?%3Aaction=search&term=yahoo&submit=search">several</a> modules to do with Yahoo, including this <a href="http://pypi.python.org/pypi/pyahoolib/0.2">one</a> which matches ... | 5 | 2009-06-15T17:54:59Z | [
"python",
"yahoo-messenger"
] |
Any yahoo messenger lib for python? | 997,419 | <p>Is there any lib available to connect to yahoo messenger using either the standard protocol or the http way from python? </p>
| 2 | 2009-06-15T17:48:32Z | 3,828,806 | <p>There is also the <a href="http://developer.yahoo.com/messenger/guide/index.html" rel="nofollow">Yahoo IM SDK</a> that might help.</p>
| 1 | 2010-09-30T07:58:28Z | [
"python",
"yahoo-messenger"
] |
What does %s mean in Python? | 997,797 | <p>What does %s mean in Python? And what does the following bit of code do?</p>
<p>For instance... </p>
<pre><code> if len(sys.argv) < 2:
sys.exit('Usage: %s database-name' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
</code></pre>
| 78 | 2009-06-15T19:05:10Z | 997,807 | <p>It is a string formatting syntax (which it borrows from C).</p>
<p>Please see <a href="http://www.diveintopython.net/native_data_types/formatting_strings.html">"Formatting Strings"</a>:</p>
<blockquote>
<p>Python supports formatting values into
strings. Although this can include
very complicated expressions,... | 85 | 2009-06-15T19:06:11Z | [
"python"
] |
What does %s mean in Python? | 997,797 | <p>What does %s mean in Python? And what does the following bit of code do?</p>
<p>For instance... </p>
<pre><code> if len(sys.argv) < 2:
sys.exit('Usage: %s database-name' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
</code></pre>
| 78 | 2009-06-15T19:05:10Z | 997,819 | <p>'%s' indicates a conversion type of 'string' when using python's string formatting capabilities. More specifically, '%s' converts a specified value to a string using the str() function. Compare this with the '%r' conversion type that uses the repr() function for value conversion.</p>
<p>Take a look at the following... | 7 | 2009-06-15T19:09:30Z | [
"python"
] |
What does %s mean in Python? | 997,797 | <p>What does %s mean in Python? And what does the following bit of code do?</p>
<p>For instance... </p>
<pre><code> if len(sys.argv) < 2:
sys.exit('Usage: %s database-name' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
</code></pre>
| 78 | 2009-06-15T19:05:10Z | 997,858 | <p>In answer to your second question: What does this code do?...</p>
<p>This is fairly standard error-checking code for a Python script that accepts command-line arguments.</p>
<p>So the first <code>if</code> statement translates to: if you haven't passed me an argument, I'm going to tell you how you should pass me... | 3 | 2009-06-15T19:15:47Z | [
"python"
] |
What does %s mean in Python? | 997,797 | <p>What does %s mean in Python? And what does the following bit of code do?</p>
<p>For instance... </p>
<pre><code> if len(sys.argv) < 2:
sys.exit('Usage: %s database-name' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
</code></pre>
| 78 | 2009-06-15T19:05:10Z | 998,157 | <p>Andrew's answer is good.</p>
<p>And just to help you out a bit more, here's how you use multiple formatting in one string</p>
<pre><code>"Hello %s, my name is %s" % ('john', 'mike') # Hello john, my name is mike".
</code></pre>
<p>If you are using ints instead of string, use %d instead of %s.</p>
<pre><code>"My ... | 48 | 2009-06-15T20:17:14Z | [
"python"
] |
What does %s mean in Python? | 997,797 | <p>What does %s mean in Python? And what does the following bit of code do?</p>
<p>For instance... </p>
<pre><code> if len(sys.argv) < 2:
sys.exit('Usage: %s database-name' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
</code></pre>
| 78 | 2009-06-15T19:05:10Z | 17,706,778 | <p>The <code>format</code> method was introduced in Python 2.6. It is more capable and not much more difficult to use:</p>
<pre><code>>>> "Hello {}, my name is {}".format('john', 'mike')
'Hello john, my name is mike'.
>>> "{1}, {0}".format('world', 'Hello')
'Hello, world'
>>> "{greeting},... | 5 | 2013-07-17T17:50:53Z | [
"python"
] |
System theme icons and PyQt4 | 997,904 | <p>I'm writing a basic program in python using the PyQt4 module. I'd like to be able to use my system theme's icons for things like the preference dialog's icon, but i have no idea how to do this. So my question is, how do you get the location of an icon, but make sure it changes with the system's icon theme? If it mat... | 7 | 2009-06-15T19:25:28Z | 999,002 | <p>Use the PyKDE4 KIcon class:</p>
<p><a href="http://api.kde.org/pykde-4.2-api/kdeui/KIcon.html" rel="nofollow">http://api.kde.org/pykde-4.2-api/kdeui/KIcon.html</a></p>
| 0 | 2009-06-15T23:44:44Z | [
"python",
"icons",
"pyqt4"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.