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 |
|---|---|---|---|---|---|---|---|---|---|
google wave OnBlipSubmitted | 1,584,406 | <p>I'm trying to create a wave robot, and I have the basic stuff working. I'm trying to create a new blip with help text when someone types @help but for some reason it doesnt create it. I'm getting no errors in the log console, and I'm seeing the info log 'in @log'</p>
<pre><code>def OnBlipSubmitted(properties, conte... | 1 | 2009-10-18T08:53:41Z | 1,586,989 | <p>if it just started working, I have two suggestions...</p>
<p>-->Have you been updating the Robot Version in the constructor? You should change the values as you update changes so that the caches can be updated.</p>
<pre><code>if __name__ == '__main__':
myRobot = robot.... | 1 | 2009-10-19T04:42:34Z | [
"python",
"google-app-engine",
"google-wave"
] |
Return value while using cProfile | 1,584,425 | <p>I'm trying to profile an instance method, so I've done something like:</p>
<pre><code>import cProfile
class Test():
def __init__(self):
pass
def method(self):
cProfile.runctx("self.method_actual()", globals(), locals())
def method_actual(self):
print "Run"
if __name__ == "__... | 12 | 2009-10-18T09:04:26Z | 1,584,468 | <p>I discovered that you can do this:</p>
<pre><code>prof = cProfile.Profile()
retval = prof.runcall(self.method_actual, *args, **kwargs)
prof.dump_stats(datafn)
</code></pre>
<p>The downside is that it's undocumented.</p>
| 24 | 2009-10-18T09:39:37Z | [
"python"
] |
Return value while using cProfile | 1,584,425 | <p>I'm trying to profile an instance method, so I've done something like:</p>
<pre><code>import cProfile
class Test():
def __init__(self):
pass
def method(self):
cProfile.runctx("self.method_actual()", globals(), locals())
def method_actual(self):
print "Run"
if __name__ == "__... | 12 | 2009-10-18T09:04:26Z | 3,840,678 | <p>I was struggling with the same problem and used a wrapper function to get over direct return values. Instead of</p>
<pre><code>cP.runctx("a=foo()", globals(), locales())
</code></pre>
<p>I create a wrapper function</p>
<pre><code>def wrapper(b):
b.append(foo())
</code></pre>
<p>and profile the call to the wrap... | 5 | 2010-10-01T15:34:05Z | [
"python"
] |
Return value while using cProfile | 1,584,425 | <p>I'm trying to profile an instance method, so I've done something like:</p>
<pre><code>import cProfile
class Test():
def __init__(self):
pass
def method(self):
cProfile.runctx("self.method_actual()", globals(), locals())
def method_actual(self):
print "Run"
if __name__ == "__... | 12 | 2009-10-18T09:04:26Z | 17,259,420 | <p>An option for any arbitrary code:</p>
<pre><code> import cProfile, pstats, sys
pr = cProfile.Profile()
pr.enable()
...
my_return_val = my_func(my_arg)
...
pr.disable()
ps = pstats.Stats(pr, stream=sys.stdout)
ps.print_stats()
</code></pre>
<p>Taken from <a href="https://docs.python.org/2/library/p... | 10 | 2013-06-23T09:35:45Z | [
"python"
] |
Return value while using cProfile | 1,584,425 | <p>I'm trying to profile an instance method, so I've done something like:</p>
<pre><code>import cProfile
class Test():
def __init__(self):
pass
def method(self):
cProfile.runctx("self.method_actual()", globals(), locals())
def method_actual(self):
print "Run"
if __name__ == "__... | 12 | 2009-10-18T09:04:26Z | 21,734,916 | <p>I think @detly the <code>.runcall()</code> is basically the best answer, but for completeness, I just wanted to take @ThomasH 's answer to be function independent: </p>
<pre><code>def wrapper(b, f, *myargs, **mykwargs):
try:
b.append(f(*myargs, **mykwargs))
except TypeError:
print 'bad args ... | 1 | 2014-02-12T17:16:51Z | [
"python"
] |
Return value while using cProfile | 1,584,425 | <p>I'm trying to profile an instance method, so I've done something like:</p>
<pre><code>import cProfile
class Test():
def __init__(self):
pass
def method(self):
cProfile.runctx("self.method_actual()", globals(), locals())
def method_actual(self):
print "Run"
if __name__ == "__... | 12 | 2009-10-18T09:04:26Z | 32,715,174 | <p>I created a decorator:</p>
<pre><code>import cProfile
import functools
import pstats
def profile(func):
@functools.wraps(func)
def inner(*args, **kwargs):
profiler = cProfile.Profile()
profiler.enable()
try:
retval = func(*args, **kwargs)
finally:
pr... | 0 | 2015-09-22T11:02:38Z | [
"python"
] |
Stochastic calculus library in python | 1,584,584 | <p>I am looking for a python library that would allow me to compute stochastic calculus stuff, like the (conditional) expectation of a random process I would define the diffusion. I had a look a at simpy (simpy.sourceforge.net), but it does not seem to cover my needs.</p>
<p>This is for quick prototyping and experimen... | 15 | 2009-10-18T10:53:07Z | 1,584,618 | <p>I know someone who uses <a href="https://computation.llnl.gov/casc/sundials/download/whatsnew.html" rel="nofollow">Sundials</a> to solve stochastic ODE/PDE problems, though I don't know enough about the library to be sure that it's appropriate in your case. There are python bindings for it <a href="http://pysundials... | 0 | 2009-10-18T11:14:19Z | [
"python",
"random",
"simulation",
"stochastic-process"
] |
Stochastic calculus library in python | 1,584,584 | <p>I am looking for a python library that would allow me to compute stochastic calculus stuff, like the (conditional) expectation of a random process I would define the diffusion. I had a look a at simpy (simpy.sourceforge.net), but it does not seem to cover my needs.</p>
<p>This is for quick prototyping and experimen... | 15 | 2009-10-18T10:53:07Z | 1,584,635 | <p>Have you looked at <a href="http://www.sagemath.org" rel="nofollow">sage</a>?</p>
| 1 | 2009-10-18T11:21:57Z | [
"python",
"random",
"simulation",
"stochastic-process"
] |
Stochastic calculus library in python | 1,584,584 | <p>I am looking for a python library that would allow me to compute stochastic calculus stuff, like the (conditional) expectation of a random process I would define the diffusion. I had a look a at simpy (simpy.sourceforge.net), but it does not seem to cover my needs.</p>
<p>This is for quick prototyping and experimen... | 15 | 2009-10-18T10:53:07Z | 2,548,765 | <p>The closest I've seen to this in Python is <a href="https://github.com/pymc-devs/pymc" rel="nofollow">PyMC</a> - an implementation of various Markov Chain Monte Carlo algorithms.</p>
| 1 | 2010-03-30T21:09:43Z | [
"python",
"random",
"simulation",
"stochastic-process"
] |
Stochastic calculus library in python | 1,584,584 | <p>I am looking for a python library that would allow me to compute stochastic calculus stuff, like the (conditional) expectation of a random process I would define the diffusion. I had a look a at simpy (simpy.sourceforge.net), but it does not seem to cover my needs.</p>
<p>This is for quick prototyping and experimen... | 15 | 2009-10-18T10:53:07Z | 7,592,438 | <p>I'm working on a stochastic processes (including diffusion processes and some conditioning) python library. Check out <a href="http://code.google.com/p/stochastic-processes/" rel="nofollow">this link</a> to the google-project homepage. Cheers!</p>
| 0 | 2011-09-29T04:24:30Z | [
"python",
"random",
"simulation",
"stochastic-process"
] |
Better way to do string filtering/manipulation | 1,584,639 | <pre><code>mystring = '14| "Preprocessor Frame Count Not Incrementing; Card: Motherboard, Port: 2"|minor'
</code></pre>
<p>So I have 3 elements (id, message and level) divided by pipe ("|"). I want to get each element so I have written these little functions:</p>
<pre><code> def get_msg(i):
x = i.split("|"... | 0 | 2009-10-18T11:23:22Z | 1,584,648 | <p>I think the best practice would be to actually have a better formatted string, or not use a string for that. Why is it a string? Where are you parsing this from? A database? Xml? Can the origin be altered? </p>
<pre><code>{ 'id': 14, 'message': 'foo', 'type': 'minor' }
</code></pre>
<p>A datatype like this I think... | 1 | 2009-10-18T11:27:14Z | [
"python",
"string"
] |
Better way to do string filtering/manipulation | 1,584,639 | <pre><code>mystring = '14| "Preprocessor Frame Count Not Incrementing; Card: Motherboard, Port: 2"|minor'
</code></pre>
<p>So I have 3 elements (id, message and level) divided by pipe ("|"). I want to get each element so I have written these little functions:</p>
<pre><code> def get_msg(i):
x = i.split("|"... | 0 | 2009-10-18T11:23:22Z | 1,584,651 | <pre><code>lst = msg.split('|')
level = lst[2].strip()
message = lst[1].strip(' "')
</code></pre>
<p>you're splitting your string twice which is a bit of a waste, other than that modification is minor.</p>
| 2 | 2009-10-18T11:29:18Z | [
"python",
"string"
] |
Better way to do string filtering/manipulation | 1,584,639 | <pre><code>mystring = '14| "Preprocessor Frame Count Not Incrementing; Card: Motherboard, Port: 2"|minor'
</code></pre>
<p>So I have 3 elements (id, message and level) divided by pipe ("|"). I want to get each element so I have written these little functions:</p>
<pre><code> def get_msg(i):
x = i.split("|"... | 0 | 2009-10-18T11:23:22Z | 1,584,695 | <p>I think the most pythonic way is to use the csv module.
From <a href="http://blog.doughellmann.com/2007/08/pymotw-csv.html" rel="nofollow">PyMotW</a> with delimiter option:</p>
<pre><code>import csv
import sys
f = open(sys.argv[1], 'rt')
try:
reader = csv.reader(f, delimiter='|')
for row in reader:
... | 5 | 2009-10-18T11:52:12Z | [
"python",
"string"
] |
Better way to do string filtering/manipulation | 1,584,639 | <pre><code>mystring = '14| "Preprocessor Frame Count Not Incrementing; Card: Motherboard, Port: 2"|minor'
</code></pre>
<p>So I have 3 elements (id, message and level) divided by pipe ("|"). I want to get each element so I have written these little functions:</p>
<pre><code> def get_msg(i):
x = i.split("|"... | 0 | 2009-10-18T11:23:22Z | 1,584,697 | <pre><code>class MyParser(object):
def __init__(self, value):
self.lst = value.split('|')
def id(self):
return self.lst[0]
def level(self):
return self.lst[2].strip()
def message(self):
return self.lst[1].strip(' "')
</code></pre>
| 1 | 2009-10-18T11:53:14Z | [
"python",
"string"
] |
Better way to do string filtering/manipulation | 1,584,639 | <pre><code>mystring = '14| "Preprocessor Frame Count Not Incrementing; Card: Motherboard, Port: 2"|minor'
</code></pre>
<p>So I have 3 elements (id, message and level) divided by pipe ("|"). I want to get each element so I have written these little functions:</p>
<pre><code> def get_msg(i):
x = i.split("|"... | 0 | 2009-10-18T11:23:22Z | 1,585,953 | <p>If you don't need the getter functions, this should work nicely:</p>
<pre><code>>>> m_id,msg,lvl = [s.strip(' "') for s in mystring.split('|')]
>>> m_id,msg,lvl
('14', 'Preprocessor Frame Count Not Incrementing; Card: Motherboard, Port: 2',
'minor')
</code></pre>
<p>Note: avoid shadowing built-in... | 0 | 2009-10-18T20:31:32Z | [
"python",
"string"
] |
Python things which are neither True nor False | 1,584,733 | <p>I just found this :</p>
<pre><code>a = (None,)
print (a is True)
print (a is False)
print (a == True)
print (a == False)
print (a == None)
print (a is None)
if a : print "hello"
if not a : print "goodbye"
</code></pre>
<p>which produces :</p>
<pre><code>False
False
False
False
False
False
hello
</code></pre>
<p>... | 10 | 2009-10-18T12:09:00Z | 1,584,738 | <p>(None,) is a tuple that contains an element, it's not empty and therefore does not evaluate to False in that context.</p>
| 1 | 2009-10-18T12:11:48Z | [
"python",
"boolean"
] |
Python things which are neither True nor False | 1,584,733 | <p>I just found this :</p>
<pre><code>a = (None,)
print (a is True)
print (a is False)
print (a == True)
print (a == False)
print (a == None)
print (a is None)
if a : print "hello"
if not a : print "goodbye"
</code></pre>
<p>which produces :</p>
<pre><code>False
False
False
False
False
False
hello
</code></pre>
<p>... | 10 | 2009-10-18T12:09:00Z | 1,584,742 | <p>Because <code>a=(None,)</code> is a tuple containing a single element <code>None</code></p>
<p>Try again with <code>a=None</code> and you will see there is a different result.</p>
<p>Also try <code>a=()</code> which is the empty tuple. This has a truth value of false </p>
| 1 | 2009-10-18T12:13:14Z | [
"python",
"boolean"
] |
Python things which are neither True nor False | 1,584,733 | <p>I just found this :</p>
<pre><code>a = (None,)
print (a is True)
print (a is False)
print (a == True)
print (a == False)
print (a == None)
print (a is None)
if a : print "hello"
if not a : print "goodbye"
</code></pre>
<p>which produces :</p>
<pre><code>False
False
False
False
False
False
hello
</code></pre>
<p>... | 10 | 2009-10-18T12:09:00Z | 1,584,743 | <p><code>a</code> is a one-member tuple, which evaluates to <code>True</code>. <code>is</code> test identity of the object, therefore, you get <code>False</code> in all those test. <code>==</code> test equality of the objects, therefore, you get <code>False</code> again.</p>
<p>in <code>if</code> statement a <code>__b... | 8 | 2009-10-18T12:13:37Z | [
"python",
"boolean"
] |
Python things which are neither True nor False | 1,584,733 | <p>I just found this :</p>
<pre><code>a = (None,)
print (a is True)
print (a is False)
print (a == True)
print (a == False)
print (a == None)
print (a is None)
if a : print "hello"
if not a : print "goodbye"
</code></pre>
<p>which produces :</p>
<pre><code>False
False
False
False
False
False
hello
</code></pre>
<p>... | 10 | 2009-10-18T12:09:00Z | 1,584,752 | <p>In Python every type can be converted to <code>bool</code> by using the <code>bool()</code> function or the <a href="http://docs.python.org/reference/datamodel.html#object.%5F%5Fnonzero%5F%5F" rel="nofollow"><code>__nonzero__</code> method</a>.</p>
<p>Examples:</p>
<ul>
<li>Sequences (lists, strings, ...) are conv... | 0 | 2009-10-18T12:18:31Z | [
"python",
"boolean"
] |
Python things which are neither True nor False | 1,584,733 | <p>I just found this :</p>
<pre><code>a = (None,)
print (a is True)
print (a is False)
print (a == True)
print (a == False)
print (a == None)
print (a is None)
if a : print "hello"
if not a : print "goodbye"
</code></pre>
<p>which produces :</p>
<pre><code>False
False
False
False
False
False
hello
</code></pre>
<p>... | 10 | 2009-10-18T12:09:00Z | 1,584,865 | <p>I find almost all the explanations here unhelpful, so here is another try:</p>
<p>The confusion here is based on that testing with "is", "==" and "if" are three different things.</p>
<ul>
<li>"is" tests identity, that is, if it's the same object. That is obviously not true in this case.</li>
<li>"==" tests value e... | 12 | 2009-10-18T13:12:54Z | [
"python",
"boolean"
] |
Python things which are neither True nor False | 1,584,733 | <p>I just found this :</p>
<pre><code>a = (None,)
print (a is True)
print (a is False)
print (a == True)
print (a == False)
print (a == None)
print (a is None)
if a : print "hello"
if not a : print "goodbye"
</code></pre>
<p>which produces :</p>
<pre><code>False
False
False
False
False
False
hello
</code></pre>
<p>... | 10 | 2009-10-18T12:09:00Z | 1,586,644 | <p>Things in python don't have to be one of <code>True</code> or <code>False</code>. </p>
<p>When they're used as a text expression for <code>if</code>/<code>while</code> loops, they're converted to booleans. You can't use <code>is</code> or <code>==</code> to test what they evaluate to. You use <code>bool( thing )</c... | 2 | 2009-10-19T01:48:44Z | [
"python",
"boolean"
] |
Installing python on 1and1 shared hosting | 1,584,857 | <p>I'm trying to install python to a 1and1.com shared linux hosting account.</p>
<p>There is a nice guide at this address:
<a href="http://www.jacksinner.com/wordpress/?p=3" rel="nofollow">http://www.jacksinner.com/wordpress/?p=3</a></p>
<p>However I get stuck at step 6 which is: "make install". The error I get is as... | 1 | 2009-10-18T13:09:38Z | 1,584,870 | <p>The short version is, it looks like you've set the prefix to <code>/~/bin/python</code> instead of simply <code>~/bin/python</code>. This is typically done with a <code>--prefix=path</code> argument to <code>configure</code> or some other similar script. Try fixing this and it should then work. I'd suggest actual co... | 4 | 2009-10-18T13:13:44Z | [
"python",
"linux"
] |
Installing python on 1and1 shared hosting | 1,584,857 | <p>I'm trying to install python to a 1and1.com shared linux hosting account.</p>
<p>There is a nice guide at this address:
<a href="http://www.jacksinner.com/wordpress/?p=3" rel="nofollow">http://www.jacksinner.com/wordpress/?p=3</a></p>
<p>However I get stuck at step 6 which is: "make install". The error I get is as... | 1 | 2009-10-18T13:09:38Z | 1,586,134 | <p>You really should consider using the <a href="https://www.activestate.com/activepython/downloads/" rel="nofollow">AS binary package from Activestate</a> for this kind of thing. Download the .tar.gz file, unpack it, change to the python directory and run the install shell script. This installs a completely standalone... | 0 | 2009-10-18T21:54:50Z | [
"python",
"linux"
] |
Grep multi-layered iterable for strings that match (Python) | 1,584,864 | <p>Say that we have a multilayered iterable with some strings at the "final" level, yes strings are iterable, but I think that you get my meaning:</p>
<pre><code>['something',
('Diff',
('diff', 'udiff'),
('*.diff', '*.patch'),
('text/x-diff', 'text/x-patch')),
('Delphi',
('delphi', 'pas', 'pascal', 'objectpascal'),
... | 1 | 2009-10-18T13:12:49Z | 1,584,895 | <p>To get the position use <code>enumerate()</code></p>
<pre><code>>>> data = [('foo', 'bar', 'frrr', 'baz'), ('foo/bar', 'baz/foo')]
>>>
>>> for l1, v1 in enumerate(data):
... for l2, v2 in enumerate(v1):
... if 'f' in v2:
... print l1, l2, v2
...
0 0 f... | 0 | 2009-10-18T13:26:50Z | [
"python",
"regex",
"string",
"search",
"data-structures"
] |
Grep multi-layered iterable for strings that match (Python) | 1,584,864 | <p>Say that we have a multilayered iterable with some strings at the "final" level, yes strings are iterable, but I think that you get my meaning:</p>
<pre><code>['something',
('Diff',
('diff', 'udiff'),
('*.diff', '*.patch'),
('text/x-diff', 'text/x-patch')),
('Delphi',
('delphi', 'pas', 'pascal', 'objectpascal'),
... | 1 | 2009-10-18T13:12:49Z | 1,584,944 | <p>Here is a grep that uses recursion to search the data structure. </p>
<p>Note that good data structures lead the way to elegant solutions.
Bad data structures make you bend over backwards to accomodate.
This feels to me like one of those cases where a bad data structure is obstructing
rather than helping you.</p... | 1 | 2009-10-18T13:53:39Z | [
"python",
"regex",
"string",
"search",
"data-structures"
] |
Grep multi-layered iterable for strings that match (Python) | 1,584,864 | <p>Say that we have a multilayered iterable with some strings at the "final" level, yes strings are iterable, but I think that you get my meaning:</p>
<pre><code>['something',
('Diff',
('diff', 'udiff'),
('*.diff', '*.patch'),
('text/x-diff', 'text/x-patch')),
('Delphi',
('delphi', 'pas', 'pascal', 'objectpascal'),
... | 1 | 2009-10-18T13:12:49Z | 1,584,979 | <p>I'd split recursive enumeration from grepping:</p>
<pre><code>def enumerate_recursive(iter, base=()):
for index, item in enumerate(iter):
if isinstance(item, basestring):
yield (base + (index,)), item
else:
for pair in enumerate_recursive(item, (base + (index,))):
... | 3 | 2009-10-18T14:06:27Z | [
"python",
"regex",
"string",
"search",
"data-structures"
] |
web2py - require selected dropdown values validate from db | 1,584,909 | <p>i have a table member that include <code>SQLField("year", db.All_years)</code></p>
<p>and All_years table as the following:</p>
<pre><code>db.define_table("All_years",
SQLField("fromY","integer"),
SQLField("toY","integer")
)
</code></pre>
<p>and constrains are : </p>
<pre><code>db.member.year.requires = IS... | 2 | 2009-10-18T13:38:17Z | 1,584,943 | <p>I see your project is progressing well!</p>
<p>The validator is <code>IS_IN_DB(dbset, field, label)</code>. So you should try:</p>
<pre><code>db.member.year.requires = IS_IN_DB(db, 'All_years.id', '%(fromY)d')
</code></pre>
<p>to have a correct label in your drop-down list.</p>
<p>Now from your table it looks li... | 2 | 2009-10-18T13:53:35Z | [
"python",
"web2py"
] |
easy_install -f vs easy_install -i | 1,585,077 | <p>This is related to <a href="http://stackoverflow.com/questions/1519589/how-do-you-host-your-own-egg-repository">this</a> question I asked a while back. </p>
<p>The end game is I want to be able to install my package "identity.model" and all dependencies. like so...</p>
<pre><code>$ easy_install -f http://eggs.sadp... | 2 | 2009-10-18T14:54:41Z | 1,585,203 | <p>-f will take the url you give it, and look there for packages, as well as on PyPI. An example of such a page is <a href="http://dist.plone.org/release/3.3.1/" rel="nofollow">http://dist.plone.org/release/3.3.1/</a> As you see, this is a list of distribution files.</p>
<p>With -i you define the main index page. It d... | 3 | 2009-10-18T15:42:23Z | [
"python",
"setuptools"
] |
easy_install -f vs easy_install -i | 1,585,077 | <p>This is related to <a href="http://stackoverflow.com/questions/1519589/how-do-you-host-your-own-egg-repository">this</a> question I asked a while back. </p>
<p>The end game is I want to be able to install my package "identity.model" and all dependencies. like so...</p>
<pre><code>$ easy_install -f http://eggs.sadp... | 2 | 2009-10-18T14:54:41Z | 1,585,416 | <p>Well looks like the trick is in having the rel="download" links on the index.html of the root.</p>
<pre><code><html>
<head>
<title>SadPhaeton Egg Repository</title>
</head>
<body>
<a rel="homepage" href="AlchemyExtra">AlchemyExtra</a> <a rel="download" href... | 0 | 2009-10-18T16:52:34Z | [
"python",
"setuptools"
] |
easy_install -f vs easy_install -i | 1,585,077 | <p>This is related to <a href="http://stackoverflow.com/questions/1519589/how-do-you-host-your-own-egg-repository">this</a> question I asked a while back. </p>
<p>The end game is I want to be able to install my package "identity.model" and all dependencies. like so...</p>
<pre><code>$ easy_install -f http://eggs.sadp... | 2 | 2009-10-18T14:54:41Z | 2,164,230 | <p>The problem is you're trying to mix -i and -f modes of making your page; you need to pick one or the other, as the <code>rel=""</code> stuff <em>only</em> works with -i.</p>
<p>If you want to use -f mode, then you just need a webserver directory with the eggs in it. If you want to use -i, then you must have a subd... | 0 | 2010-01-29T18:34:59Z | [
"python",
"setuptools"
] |
Is the Python GIL really per interpreter? | 1,585,181 | <p>I often see people talking that the GIL is per Python Interpreter (even here on stackoverflow).</p>
<p>But what I see in the source code it seems to be that the GIL is a global variable and therefore there is one GIL for all Interpreters in each python process. I know they did this because there is no interpreter o... | 8 | 2009-10-18T15:35:23Z | 1,585,260 | <p>I believe it is true (at least as of Python 2.6) that each process may have at most one CPython interpreter embedded (other runtimes may have different constraints). I'm not sure if this is an issue with the GIL per se, but it is likely due to global state, or to protect from conflicting global state in third-party ... | 0 | 2009-10-18T16:09:25Z | [
"python",
"multithreading",
"gil"
] |
Is the Python GIL really per interpreter? | 1,585,181 | <p>I often see people talking that the GIL is per Python Interpreter (even here on stackoverflow).</p>
<p>But what I see in the source code it seems to be that the GIL is a global variable and therefore there is one GIL for all Interpreters in each python process. I know they did this because there is no interpreter o... | 8 | 2009-10-18T15:35:23Z | 1,585,641 | <p>Perhaps the confusion comes about because most people assume Python has one interpreter per process. I recall reading that the support for multiple interpreters via the C API was largely untested and hardly ever used. (And when I gave it a go, didn't work properly.)</p>
| 2 | 2009-10-18T18:24:19Z | [
"python",
"multithreading",
"gil"
] |
Is the Python GIL really per interpreter? | 1,585,181 | <p>I often see people talking that the GIL is per Python Interpreter (even here on stackoverflow).</p>
<p>But what I see in the source code it seems to be that the GIL is a global variable and therefore there is one GIL for all Interpreters in each python process. I know they did this because there is no interpreter o... | 8 | 2009-10-18T15:35:23Z | 1,585,939 | <p>The GIL is indeed per-process, not per-interpreter. This is unchanged in 3.x.</p>
| 7 | 2009-10-18T20:25:55Z | [
"python",
"multithreading",
"gil"
] |
Python optional parameters | 1,585,247 | <p>Guys, I just started python recently and get confused with the optional parameters, say I have the program like this:</p>
<pre><code>class B:
pass
class A:
def __init__(self, builds = B()):
self.builds = builds
</code></pre>
<p>If I create A twice</p>
<pre><code>b = A()
c = A()
</code></pre>
<p>a... | 23 | 2009-10-18T16:05:44Z | 1,585,250 | <p>Yes; default parameters are evaluated only at the time when the function is defined.</p>
<p>One possible solution would be to have the parameter be a <strong>class</strong> rather than an instance, a la</p>
<pre><code>def foo(blah, klass = B):
b = klass()
# etc
</code></pre>
| 6 | 2009-10-18T16:07:01Z | [
"python",
"optional-arguments"
] |
Python optional parameters | 1,585,247 | <p>Guys, I just started python recently and get confused with the optional parameters, say I have the program like this:</p>
<pre><code>class B:
pass
class A:
def __init__(self, builds = B()):
self.builds = builds
</code></pre>
<p>If I create A twice</p>
<pre><code>b = A()
c = A()
</code></pre>
<p>a... | 23 | 2009-10-18T16:05:44Z | 1,585,265 | <p>you need to do the following:</p>
<pre><code>class A:
def __init__(self, builds=None):
if builds is None:
builds = B()
self.builds = builds
</code></pre>
<p>it's a very wide-spread error, using mutable parameters as a default arguments. there are plenty of dupes probably on SO.</p>
| 15 | 2009-10-18T16:11:36Z | [
"python",
"optional-arguments"
] |
Python optional parameters | 1,585,247 | <p>Guys, I just started python recently and get confused with the optional parameters, say I have the program like this:</p>
<pre><code>class B:
pass
class A:
def __init__(self, builds = B()):
self.builds = builds
</code></pre>
<p>If I create A twice</p>
<pre><code>b = A()
c = A()
</code></pre>
<p>a... | 23 | 2009-10-18T16:05:44Z | 1,585,451 | <p>You need to understand how default values work in order to use them effectively.</p>
<p>Functions are objects. As such, they have attributes. So, if I create this function:</p>
<pre><code>>>> def f(x, y=[]):
y.append(x)
return y
</code></pre>
<p>I've created an object. Here are its att... | 45 | 2009-10-18T17:10:36Z | [
"python",
"optional-arguments"
] |
union with sort in Google-App-Engine | 1,585,299 | <p>I have a class:</p>
<pre><code>class Transaction(db.Model):
accountDebit = db.ReferenceProperty(reference_class=Account,
collection_name="kontoDuguje")
accountCredit = db.ReferenceProperty(reference_class=Account,
collection_name... | 0 | 2009-10-18T16:21:55Z | 1,586,430 | <p>You can do an OR (Python laboriously synthesizes it for you at application level), which takes care of the "union with sorty". However, if you need to worry about > 1000 transactions, that won't help (nor will offset and limit: the sum of offset + limit is what's limited to 1000!). You'll need to slice by something... | 2 | 2009-10-19T00:20:44Z | [
"python",
"google-app-engine"
] |
Is there a way to perform "if" in python's lambda | 1,585,322 | <p>In <strong>python 2.6</strong>, I want to do:</p>
<pre><code>f = lambda x: if x==2 print x else raise Exception()
f(2) #should print "2"
f(3) #should throw an exception
</code></pre>
<p>This clearly isn't the syntax. Is it possible to perform an <code>if</code> in <code>lambda</code> and if so how to do it?</p>
<... | 119 | 2009-10-18T16:28:03Z | 1,585,334 | <p>Lambdas in Python are fairly restrictive with regard to what you're allowed to use. Specifically, you can't have any keywords (except for operators like <code>and</code>, <code>not</code>, <code>or</code>, etc) in their body.</p>
<p>So, there's no way you could use a lambda for your example (because you can't use <... | 10 | 2009-10-18T16:31:18Z | [
"python",
"lambda"
] |
Is there a way to perform "if" in python's lambda | 1,585,322 | <p>In <strong>python 2.6</strong>, I want to do:</p>
<pre><code>f = lambda x: if x==2 print x else raise Exception()
f(2) #should print "2"
f(3) #should throw an exception
</code></pre>
<p>This clearly isn't the syntax. Is it possible to perform an <code>if</code> in <code>lambda</code> and if so how to do it?</p>
<... | 119 | 2009-10-18T16:28:03Z | 1,585,343 | <p>why don't you just define a function?</p>
<pre><code>def f(x):
if x == 2:
print(x)
else:
raise ValueError
</code></pre>
<p>there really is no justification to use lambda in this case.</p>
| 26 | 2009-10-18T16:33:17Z | [
"python",
"lambda"
] |
Is there a way to perform "if" in python's lambda | 1,585,322 | <p>In <strong>python 2.6</strong>, I want to do:</p>
<pre><code>f = lambda x: if x==2 print x else raise Exception()
f(2) #should print "2"
f(3) #should throw an exception
</code></pre>
<p>This clearly isn't the syntax. Is it possible to perform an <code>if</code> in <code>lambda</code> and if so how to do it?</p>
<... | 119 | 2009-10-18T16:28:03Z | 1,585,396 | <p>The syntax you're looking for:</p>
<pre><code>lambda x: True if x % 2 == 0 else False
</code></pre>
<p>But you can't use <code>print</code> or <code>raise</code> in a lambda.</p>
| 268 | 2009-10-18T16:48:43Z | [
"python",
"lambda"
] |
Is there a way to perform "if" in python's lambda | 1,585,322 | <p>In <strong>python 2.6</strong>, I want to do:</p>
<pre><code>f = lambda x: if x==2 print x else raise Exception()
f(2) #should print "2"
f(3) #should throw an exception
</code></pre>
<p>This clearly isn't the syntax. Is it possible to perform an <code>if</code> in <code>lambda</code> and if so how to do it?</p>
<... | 119 | 2009-10-18T16:28:03Z | 1,586,124 | <p>You can easily raise an exception in a lambda, if that's what you really want to do.</p>
<pre><code>def Raise(exception):
raise exception
x = lambda y: 1 if y < 2 else Raise(ValueError("invalid value"))
</code></pre>
<p>Is this a good idea? My instinct in general is to leave the error reporting out of lamb... | 13 | 2009-10-18T21:48:06Z | [
"python",
"lambda"
] |
Is there a way to perform "if" in python's lambda | 1,585,322 | <p>In <strong>python 2.6</strong>, I want to do:</p>
<pre><code>f = lambda x: if x==2 print x else raise Exception()
f(2) #should print "2"
f(3) #should throw an exception
</code></pre>
<p>This clearly isn't the syntax. Is it possible to perform an <code>if</code> in <code>lambda</code> and if so how to do it?</p>
<... | 119 | 2009-10-18T16:28:03Z | 33,251,758 | <p>Probably the worst python line I've written so far: </p>
<pre><code>f = lambda x: sys.stdout.write(["2\n",][2*(x==2)-2])
</code></pre>
<p>If x == 2 you print, </p>
<p>if x != 2 you raise. </p>
| 9 | 2015-10-21T05:43:22Z | [
"python",
"lambda"
] |
django Unicode GET Parameter Values | 1,585,439 | <p>I'm trying to get a GET parameter value that looks like this:
<a href="http://someurl/handler.json?&q=%E1%F8%E0%F1%F8%E9" rel="nofollow">http://someurl/handler.json?&q=%E1%F8%E0%F1%F8%E9</a></p>
<p>The q parameter in this case is Hebrew.
I'm trying to read the value using the following code:</p>
<pre><code... | 0 | 2009-10-18T17:05:09Z | 1,585,467 | <p>The query string is in ISO-8859-8, but Django's default encoding is UTF-8. You will have to change either <a href="http://docs.djangoproject.com/en/dev/ref/settings/#default-charset" rel="nofollow"><code>DEFAULT_CHARSET</code></a> or <a href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.Htt... | 3 | 2009-10-18T17:16:32Z | [
"python",
"django",
"unicode"
] |
sending email from wave robot | 1,585,487 | <p>Anyone know how to send an email using google wave python api?</p>
<p>Thanks</p>
| 0 | 2009-10-18T17:24:04Z | 1,585,496 | <p>Being a wave robot is nothing special here - you've got to determine at what point you want to send email, but you haven't told us anything about that, so it's hard to advise you.</p>
<p>When you've worked out what you want to send, just follow the normal instructions for <a href="http://code.google.com/appengine/d... | 5 | 2009-10-18T17:27:38Z | [
"python",
"api",
"google-app-engine",
"google-wave"
] |
Python: Do relative imports mean you can't execute a subpackage by itself? | 1,585,756 | <p>I've recently ported my Python project to run on Python 3.1. For that I had to adopt the policy of relative imports within the submodules and subpackages of my project. I've donât that and now the project itself works, but I noticed I can't execute any of the subpackages or submodules in it. If I try, I get "built... | 7 | 2009-10-18T19:12:07Z | 1,585,801 | <p>Yes, it's normal. If you want to execute a module that is also a part of a package (in itself a strange thing to do) you need to have absolute imports. When you execute the module it is not, from the interpreters point of view, a part of a package, but the <code>__main__</code> module. So it wouldn't know where the ... | 4 | 2009-10-18T19:31:10Z | [
"python",
"import",
"python-3.x"
] |
Python: Do relative imports mean you can't execute a subpackage by itself? | 1,585,756 | <p>I've recently ported my Python project to run on Python 3.1. For that I had to adopt the policy of relative imports within the submodules and subpackages of my project. I've donât that and now the project itself works, but I noticed I can't execute any of the subpackages or submodules in it. If I try, I get "built... | 7 | 2009-10-18T19:12:07Z | 1,586,005 | <p>You can use -m flag of the python interpreter to run modules in sub-packages (or even packages in 3.1.).</p>
| 3 | 2009-10-18T20:54:54Z | [
"python",
"import",
"python-3.x"
] |
Python: Do relative imports mean you can't execute a subpackage by itself? | 1,585,756 | <p>I've recently ported my Python project to run on Python 3.1. For that I had to adopt the policy of relative imports within the submodules and subpackages of my project. I've donât that and now the project itself works, but I noticed I can't execute any of the subpackages or submodules in it. If I try, I get "built... | 7 | 2009-10-18T19:12:07Z | 3,652,088 | <p>I had the <a href="http://stackoverflow.com/questions/3616952/how-to-properly-use-relative-or-absolute-imports-in-python-modules">same problem</a> and I considered the <code>-m</code> switch too hard. </p>
<p>Instead I use this:</p>
<pre><code>try:
from . import bar
except ValueError:
import bar
if __name... | -1 | 2010-09-06T14:05:12Z | [
"python",
"import",
"python-3.x"
] |
Human-readable binary data using Python | 1,585,950 | <p>My work requires that I perform a mathematical simulation whose parameters come from a binary file. The simulator can read such binary file without a problem.</p>
<p>However, I need to peek inside the binary file to make sure the parameters are what I need them to be, and I cannot seem to be able to do it.</p>
<p>... | 2 | 2009-10-18T20:30:28Z | 1,585,957 | <p>You can read the file's content into a string in memory:</p>
<pre><code>thedata = open(thefilename, 'rb').read()
</code></pre>
<p>and then locate a string in it:</p>
<pre><code>where = thedata.find('CENTRAL_BODY')
</code></pre>
<p>and finally slice off the part you care about:</p>
<pre><code>thepart = thedata[w... | 4 | 2009-10-18T20:34:38Z | [
"python",
"format",
"ascii",
"binary-data"
] |
Human-readable binary data using Python | 1,585,950 | <p>My work requires that I perform a mathematical simulation whose parameters come from a binary file. The simulator can read such binary file without a problem.</p>
<p>However, I need to peek inside the binary file to make sure the parameters are what I need them to be, and I cannot seem to be able to do it.</p>
<p>... | 2 | 2009-10-18T20:30:28Z | 1,585,996 | <p>If it's a binary file, you will need to use the struct module. You will need to know how the data is formatted in the file. If that is not documented, you will have to reverse engineer it. </p>
<p>Do you have source code of the other dumping program? You may be able to just port that to Python</p>
<p>We can probab... | 0 | 2009-10-18T20:50:07Z | [
"python",
"format",
"ascii",
"binary-data"
] |
Human-readable binary data using Python | 1,585,950 | <p>My work requires that I perform a mathematical simulation whose parameters come from a binary file. The simulator can read such binary file without a problem.</p>
<p>However, I need to peek inside the binary file to make sure the parameters are what I need them to be, and I cannot seem to be able to do it.</p>
<p>... | 2 | 2009-10-18T20:30:28Z | 1,586,242 | <p>It sounds like this 'dump' program already does what you need: interpreting the binary file. I guess my approach would be to write a python program that can take a dump'ed file, extract the parameters you want and display them.</p>
<p>Then parse it with something like this:</p>
<p>myparms.py:</p>
<pre><code>impo... | 1 | 2009-10-18T22:38:06Z | [
"python",
"format",
"ascii",
"binary-data"
] |
Human-readable binary data using Python | 1,585,950 | <p>My work requires that I perform a mathematical simulation whose parameters come from a binary file. The simulator can read such binary file without a problem.</p>
<p>However, I need to peek inside the binary file to make sure the parameters are what I need them to be, and I cannot seem to be able to do it.</p>
<p>... | 2 | 2009-10-18T20:30:28Z | 1,586,575 | <p>You have to know the format the data is stored in; there's simply no way around that.</p>
<p>If there's no written spec on it, try to open it in a hex editor and study the format, using the text-dump as a reference. If you can get the source code for the tool that creates the text-dumps, that would help you alot.</... | 0 | 2009-10-19T01:24:08Z | [
"python",
"format",
"ascii",
"binary-data"
] |
PHP, Python, Ruby application with multiple RDBMS | 1,586,008 | <p>I start feeling old fashioned when I see all these SQL generating database abstraction layers and all those ORMs out there, although I am far from being old. I understand the need for them, but their use spreads to places they normally don't belong to.</p>
<p>I firmly believe that using database abstraction layers ... | 2 | 2009-10-18T20:56:01Z | 1,586,035 | <p>It would be great if code written for one platform would work on every other without any modification whatsoever, but this is usually not the case and probably never will be. What the current frameworks do is about all anyone can. </p>
| 0 | 2009-10-18T21:04:32Z | [
"php",
"python",
"ruby-on-rails",
"database"
] |
PHP, Python, Ruby application with multiple RDBMS | 1,586,008 | <p>I start feeling old fashioned when I see all these SQL generating database abstraction layers and all those ORMs out there, although I am far from being old. I understand the need for them, but their use spreads to places they normally don't belong to.</p>
<p>I firmly believe that using database abstraction layers ... | 2 | 2009-10-18T20:56:01Z | 1,586,105 | <p>If you want to leverage the bells and whistles of various RDBMSes, you can certainly do it. Just apply standard OO Principles. Figure out what kind of API your persistence layer will need to provide. </p>
<p>You'll end up writing a set of isomorphic persistence adapter classes. From the perspective of your mode... | 2 | 2009-10-18T21:39:02Z | [
"php",
"python",
"ruby-on-rails",
"database"
] |
PHP, Python, Ruby application with multiple RDBMS | 1,586,008 | <p>I start feeling old fashioned when I see all these SQL generating database abstraction layers and all those ORMs out there, although I am far from being old. I understand the need for them, but their use spreads to places they normally don't belong to.</p>
<p>I firmly believe that using database abstraction layers ... | 2 | 2009-10-18T20:56:01Z | 1,587,287 | <p>It's even more "old fashioned" than modern ORMs, but doesn't <a href="http://en.wikipedia.org/wiki/Open%5FDatabase%5FConnectivity" rel="nofollow">ODBC</a> address this issue?</p>
| 0 | 2009-10-19T06:50:32Z | [
"php",
"python",
"ruby-on-rails",
"database"
] |
PHP, Python, Ruby application with multiple RDBMS | 1,586,008 | <p>I start feeling old fashioned when I see all these SQL generating database abstraction layers and all those ORMs out there, although I am far from being old. I understand the need for them, but their use spreads to places they normally don't belong to.</p>
<p>I firmly believe that using database abstraction layers ... | 2 | 2009-10-18T20:56:01Z | 1,587,887 | <p>You cannot eat a cake and have it, choose on of the following options.</p>
<ul>
<li>Use your database abstraction layer whenever you can and in the rare cases when you have a need for a hand-made query (eg. performance reasons) stick to the lowest common denominator and don't use stored procedures or any proprietar... | 2 | 2009-10-19T10:09:53Z | [
"php",
"python",
"ruby-on-rails",
"database"
] |
django and executing a separate .py to manipute a database | 1,586,041 | <p>I want to execute a random .py file, say foo.py on the myproject/myapp folder by using crobjob by some periods</p>
<p>I have this basic model in my model.py for the app:</p>
<pre><code>class Mymodel(models.Model):
content = models.TextField()
</code></pre>
<p>Say I have this in my foo.py, I want to check if t... | 1 | 2009-10-18T21:07:17Z | 1,586,115 | <p>You have two separate questions here - it would have been better to split them out.</p>
<p>To run a separate script, you're best off creating a <code>./manage.py</code> command. See <a href="http://docs.djangoproject.com/en/dev/howto/custom-management-commands/" rel="nofollow">the documentation</a> on how to do thi... | 5 | 2009-10-18T21:43:28Z | [
"python",
"django",
"django-models"
] |
django and executing a separate .py to manipute a database | 1,586,041 | <p>I want to execute a random .py file, say foo.py on the myproject/myapp folder by using crobjob by some periods</p>
<p>I have this basic model in my model.py for the app:</p>
<pre><code>class Mymodel(models.Model):
content = models.TextField()
</code></pre>
<p>Say I have this in my foo.py, I want to check if t... | 1 | 2009-10-18T21:07:17Z | 1,586,561 | <p>You might also check out <a href="http://github.com/django-extensions/django-extensions" rel="nofollow">django-extensions</a>, which has a built-in manage.py extension called "runscript" that executes any python script in your django project's context.</p>
| 2 | 2009-10-19T01:19:40Z | [
"python",
"django",
"django-models"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,586,157 | <p>Simple solution: <strong>Use the <a href="http://en.wikipedia.org/wiki/HSL%5Fand%5FHSV">HSL or HSV</a> color space</strong> instead of rgb (convert it to RGB afterwards if you need this). The difference is the meaning of the tuple: Where RGB means values for Red, Green and Blue, in HSL the H is the color (120 degree... | 54 | 2009-10-18T22:04:37Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,586,163 | <p>Check out the <code>colorsys</code> module:</p>
<p><a href="http://docs.python.org/library/colorsys.html" rel="nofollow">http://docs.python.org/library/colorsys.html</a></p>
<p>Use the HSL or HSV color space. Randomize the hue to be close to green, then choose completely random stuff for the saturation and V (brig... | 16 | 2009-10-18T22:09:00Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,586,165 | <p>So in this case you are lucky enough to want variations on a primary color, but for artistic uses like this it is better to specify color wheel coordinates rather than primary color magnitudes.</p>
<p>You probably want something from the <code>colorsys</code> module like:</p>
<pre><code>colorsys.hsv_to_rgb(h, s, v... | 5 | 2009-10-18T22:09:31Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,586,181 | <p>The simplest way to do this is to make sure that the red and blue components are the same, like this: (Forgive my Python)</p>
<pre><code>rb = randint(100, 200)
color = (rb, randint(120, 255), rb)
</code></pre>
| 0 | 2009-10-18T22:19:17Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,586,206 | <p>What you want is to work in terms of <a href="http://en.wikipedia.org/wiki/HSL%5Fand%5FHSV" rel="nofollow">HSL</a> instead of RGB. You could find a range of hue that satisfies "greenish" and pick a random hue from it. You could also pick random saturation and lightness but you'll probably want to keep your saturatio... | 1 | 2009-10-18T22:27:17Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,586,291 | <p>As others have suggested, generating random colours is much easier in the HSV colour space (or HSL, the difference is pretty irrelevant for this)</p>
<p>So, code to generate random "green'ish" colours, and (for demonstration purposes) display them as a series of simple coloured HTML span tags:</p>
<pre><code>#!/us... | 20 | 2009-10-18T23:10:07Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,586,296 | <p>If you stick with RGB, you basically just need to make sure the G value is greater than the R and B, and try to keep the blue and red values similar so that the hue doesn't go too crazy. Extending from Slaks, maybe something like (I know next to nothing about Python):</p>
<pre><code>greenval = randint(100, 255)
red... | 9 | 2009-10-18T23:13:10Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,586,315 | <p>The solution with HSx color space is a very good one. However, if you need something extremely simplistic and have no specific requirements about the distribution of the colors (like uniformity), a simplistic RGB-based solution would be just to make sure that G value is greater than both R and B</p>
<pre><code>rr =... | 3 | 2009-10-18T23:25:04Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
How to generate random 'greenish' colors | 1,586,147 | <p>Anyone have any suggestions on how to make randomized colors that are all greenish? Right now I'm generating the colors by this:</p>
<pre><code>color = (randint(100, 200), randint(120, 255), randint(100, 200))
</code></pre>
<p>That mostly works, but I get brownish colors a lot. </p>
| 32 | 2009-10-18T22:00:37Z | 1,590,881 | <p>I'd go with with the HSV approach everyone else mentioned. Another approach would be to get a nice high resolution photo which some greenery in it, crop out the non-green parts, and pick random pixels from it using <a href="http://www.pythonware.com/products/pil/" rel="nofollow">PIL</a>.</p>
| 0 | 2009-10-19T20:19:33Z | [
"python",
"language-agnostic",
"random",
"colors"
] |
gobject io monitoring + nonblocking reads | 1,586,342 | <p>I've got a problem with using the <code>io_add_watch</code> monitor in python (via gobject). I want to do a nonblocking read of the whole buffer after every notification. Here's the code (shortened a bit):</p>
<pre><code>class SomeApp(object):
def __init__(self):
# some other init that does a lot of stder... | 3 | 2009-10-18T23:38:03Z | 1,588,171 | <p>The <a href="http://www.pygtk.org/pygtk2reference/gobject-functions.html#function-gobject--io-add-watch" rel="nofollow">documentation</a> says you should return <code>TRUE</code> from the callback or it will be removed from the list of event sources.</p>
| 0 | 2009-10-19T11:29:38Z | [
"python",
"input",
"glib",
"gobject"
] |
gobject io monitoring + nonblocking reads | 1,586,342 | <p>I've got a problem with using the <code>io_add_watch</code> monitor in python (via gobject). I want to do a nonblocking read of the whole buffer after every notification. Here's the code (shortened a bit):</p>
<pre><code>class SomeApp(object):
def __init__(self):
# some other init that does a lot of stder... | 3 | 2009-10-18T23:38:03Z | 1,621,815 | <p>This sounds like a race condition in which there is some delay to setting your callback, or else there is a change in the environment which affects whether or not you can set the callback.</p>
<p>I would look carefully at what happens before you call <code>io_add_watch()</code>. For instance the Python fcntl docs s... | 2 | 2009-10-25T19:27:24Z | [
"python",
"input",
"glib",
"gobject"
] |
gobject io monitoring + nonblocking reads | 1,586,342 | <p>I've got a problem with using the <code>io_add_watch</code> monitor in python (via gobject). I want to do a nonblocking read of the whole buffer after every notification. Here's the code (shortened a bit):</p>
<pre><code>class SomeApp(object):
def __init__(self):
# some other init that does a lot of stder... | 3 | 2009-10-18T23:38:03Z | 1,638,863 | <p>What happens if you hook the callback first, prior to any stderr output? Does it still get called when you have debug output enabled?</p>
<p>Also, I suppose you should probably be repeatedly calling <code>os.read()</code> in your handler until it gives no data, in case >1024 bytes become ready between calls.</p>
... | 0 | 2009-10-28T17:39:19Z | [
"python",
"input",
"glib",
"gobject"
] |
Race-condition creating folder in Python | 1,586,648 | <p>I have a urllib2 caching module, which sporadically crashes because of the following code:</p>
<pre><code>if not os.path.exists(self.cache_location):
os.mkdir(self.cache_location)
</code></pre>
<p>The problem is, by the time the second line is being executed, the folder may exist, and will error:</p>
<pre> F... | 13 | 2009-10-19T01:52:13Z | 1,586,663 | <p>Could you catch the exception and then test whether the file exists as a directory or not?</p>
| 2 | 2009-10-19T01:57:54Z | [
"python",
"caching",
"race-condition"
] |
Race-condition creating folder in Python | 1,586,648 | <p>I have a urllib2 caching module, which sporadically crashes because of the following code:</p>
<pre><code>if not os.path.exists(self.cache_location):
os.mkdir(self.cache_location)
</code></pre>
<p>The problem is, by the time the second line is being executed, the folder may exist, and will error:</p>
<pre> F... | 13 | 2009-10-19T01:52:13Z | 1,586,668 | <p>Instead of </p>
<pre><code>if not os.path.exists(self.cache_location):
os.mkdir(self.cache_location)
</code></pre>
<p>you could do</p>
<pre><code>try:
os.makedirs(self.cache_location)
except OSError:
pass
</code></pre>
<p>As you would end up with the same <em>functionality</em>.</p>
<p><sub>DISCLAIM... | 11 | 2009-10-19T02:00:27Z | [
"python",
"caching",
"race-condition"
] |
Race-condition creating folder in Python | 1,586,648 | <p>I have a urllib2 caching module, which sporadically crashes because of the following code:</p>
<pre><code>if not os.path.exists(self.cache_location):
os.mkdir(self.cache_location)
</code></pre>
<p>The problem is, by the time the second line is being executed, the folder may exist, and will error:</p>
<pre> F... | 13 | 2009-10-19T01:52:13Z | 1,586,753 | <p>When you have race conditions often EAFP(easier to ask forgiveness than permission) works better that LBYL(look before you leap)</p>
<p><a href="http://my.safaribooksonline.com/0596001886/pythonian-CHP-6-SECT-6" rel="nofollow">Error checking strategies</a></p>
| 2 | 2009-10-19T02:49:29Z | [
"python",
"caching",
"race-condition"
] |
Race-condition creating folder in Python | 1,586,648 | <p>I have a urllib2 caching module, which sporadically crashes because of the following code:</p>
<pre><code>if not os.path.exists(self.cache_location):
os.mkdir(self.cache_location)
</code></pre>
<p>The problem is, by the time the second line is being executed, the folder may exist, and will error:</p>
<pre> F... | 13 | 2009-10-19T01:52:13Z | 8,192,589 | <p>The code I ended up with was:</p>
<pre><code>import os
import errno
folder_location = "/tmp/example_dir"
try:
os.mkdir(folder_location)
except OSError, e:
if e.errno == errno.EEXIST and os.path.isdir(folder_location):
# File exists, and it's a directory,
# another process beat us to creati... | 4 | 2011-11-19T07:25:30Z | [
"python",
"caching",
"race-condition"
] |
Using multiprocessing pool of workers | 1,586,754 | <p>I have the following code written to make my lazy second CPU core working. What the code does basically is first find the desired "sea" files in the directory hierarchy and later execute set of external scripts to process these binary "sea" files to produce 50 to 100 text and binary files in number. As the title of ... | 10 | 2009-10-19T02:49:44Z | 1,830,785 | <p>There are several things I can think of:</p>
<p>1) Have you printed out the pathfiles? Are you sure that they are all properly generated?</p>
<p>a) I ask as your os.walk is a bit interesting; the dirs.sort() should be ok, but seems quite unncessarily. os.chdir() in general shouldn't be used; the restoration <stro... | 3 | 2009-12-02T05:13:11Z | [
"python",
"multiprocessing"
] |
Using multiprocessing pool of workers | 1,586,754 | <p>I have the following code written to make my lazy second CPU core working. What the code does basically is first find the desired "sea" files in the directory hierarchy and later execute set of external scripts to process these binary "sea" files to produce 50 to 100 text and binary files in number. As the title of ... | 10 | 2009-10-19T02:49:44Z | 1,941,748 | <p>I would start with getting a better feeling for what is going on with the worker process. The multiprocessing module comes with logging for its subprocesses if you need. Since you have simplified the code to narrow down the problem, I would just debug with a few print statements, like so (or you can PrettyPrint th... | 6 | 2009-12-21T18:21:30Z | [
"python",
"multiprocessing"
] |
How to write a program that will automically generate sample exam questions from a file? | 1,587,248 | <p>How can I write a program that will automatically generate a sample examination? </p>
<p>For example, the user will be prompted to supply four categories of questions to be included in the a 6 question exam from the following list: </p>
<ol>
<li>Loops</li>
<li>Functions</li>
<li>Decisions</li>
<li>Data Types</li>
... | -2 | 2009-10-19T06:39:52Z | 1,587,337 | <p>In the absence of additional information required to answer this specific problem, I will outline the general approach I would use to solve this problem. My solution would involve using <a href="http://en.wikipedia.org/wiki/LaTeX" rel="nofollow">LaTeX</a> to typeset the exam and the <a href="http://www.ctan.org/tex... | 3 | 2009-10-19T07:15:13Z | [
"python"
] |
How to write a program that will automically generate sample exam questions from a file? | 1,587,248 | <p>How can I write a program that will automatically generate a sample examination? </p>
<p>For example, the user will be prompted to supply four categories of questions to be included in the a 6 question exam from the following list: </p>
<ol>
<li>Loops</li>
<li>Functions</li>
<li>Decisions</li>
<li>Data Types</li>
... | -2 | 2009-10-19T06:39:52Z | 1,587,341 | <p>las3rjock has a good solution.</p>
<p>You could also move your input file to a SQLite database, using a normalised structure: e.g. Question table, Answer table (with FK to QuestionID), and generate a random answer based on the Question ID. You'll need a third table to keep track of the correct answer per question t... | 2 | 2009-10-19T07:16:38Z | [
"python"
] |
Custom Django-admin command issue | 1,587,282 | <p>trying to understand how custom admin commands work, I have my project named "mailing" and app inside named "msystem", I have written this retrieve.py to the mailing/msystem/management/commands/ folder and I have pasted an empty <strong>init</strong>.py both to the management and cpmmands folders.</p>
<pre><code>fr... | 0 | 2009-10-19T06:49:30Z | 1,587,318 | <p>If you're getting an "indentation error" and everything looks aligned, this usually suggests that you're mixing tabs and spaces.</p>
<p>I suggest ensuring that your module is only using spaces.</p>
| 2 | 2009-10-19T07:06:11Z | [
"python",
"django",
"django-admin"
] |
Custom Django-admin command issue | 1,587,282 | <p>trying to understand how custom admin commands work, I have my project named "mailing" and app inside named "msystem", I have written this retrieve.py to the mailing/msystem/management/commands/ folder and I have pasted an empty <strong>init</strong>.py both to the management and cpmmands folders.</p>
<pre><code>fr... | 0 | 2009-10-19T06:49:30Z | 1,587,323 | <p>Your indentation needs to be consistent through the entire file, which it isn't in the snippet you posted above.</p>
<p>The "help = " line is indented four spaces after "class" but then the "x =" line is indented many more than four.</p>
<p>Maybe you are mixing spaces and tabs and thus have two tabs before "x ="?<... | 4 | 2009-10-19T07:07:37Z | [
"python",
"django",
"django-admin"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 1,587,418 | <p>Just use a loop. I can't immediately think of any way to make the slice operator behave the way you're saying you want it to, except <em>maybe</em> by subclassing numpy's <code>array</code> and overriding the appropriate method with some sort of Python voodoo... but more importantly, the idea that <code>a[1:] = a[0:... | 1 | 2009-10-19T07:49:35Z | [
"python",
"numpy",
"slice"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 1,588,565 | <p>It must have something to do with assigning a slice. Operators, however, as you may already know, do follow your expected behavior:</p>
<pre><code>>>> a = numpy.array([1,0,0,0])
>>> a[1:]+=a[:3]
>>> a
array([1, 1, 1, 1])
</code></pre>
<p>If you already have zeros in your real-world prob... | 1 | 2009-10-19T13:03:14Z | [
"python",
"numpy",
"slice"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 1,588,711 | <p>It is not the correct logic.
I'll try to use letters to explain it.</p>
<p>Image <code>array = abcd</code> with a,b,c,d as elements.<br />
Now, <code>array[1:]</code> means from the element in position <code>1</code> (starting from <code>0</code>) on.<br />
In this case:<code>bcd</code> and <code>array[0:3]</code> ... | 1 | 2009-10-19T13:37:10Z | [
"python",
"numpy",
"slice"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 1,589,600 | <p>Numpy must be checking if the target array is the same as the input array when doing the setkey call. Luckily, there are ways around it. First, I tried using <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.put.html" rel="nofollow"><code>numpy.put</code></a> instead</p>
<pre><code>In [46]: a = n... | 1 | 2009-10-19T16:04:55Z | [
"python",
"numpy",
"slice"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 1,591,185 | <p><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ufunc.accumulate.html" rel="nofollow"><strong>accumulate</strong></a> is designed to do what you seem to want; that is, to proprigate an operation along an array. Here's an example:</p>
<pre><code>from numpy import *
a = array([1,0,0,0])
a[1:] = a... | 2 | 2009-10-19T21:13:02Z | [
"python",
"numpy",
"slice"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 1,603,553 | <p>i would suggest cython instead of looping in c. there <em>might</em> be some fancy numpy way of getting your example to work using a lot of intermediate steps... but since you know how to write it in c already, just write that quick little bit as a cython function and let cython's magic make the rest of the work ea... | 0 | 2009-10-21T20:54:35Z | [
"python",
"numpy",
"slice"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 1,637,711 | <p>You could have a look at np.lib.stride_tricks.</p>
<p>There is some information in these excellent slides:
<a href="http://mentat.za.net/numpy/numpy%5Fadvanced%5Fslides/" rel="nofollow">http://mentat.za.net/numpy/numpy_advanced_slides/</a></p>
<p>with stride_tricks starting at slide 29.</p>
<p>I'm not completely ... | 1 | 2009-10-28T14:41:47Z | [
"python",
"numpy",
"slice"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 2,613,622 | <p>Late answer, but this turned up on Google so I probably point to the doc the OP wanted. Your problem is clear: when using NumPy slices, temporaries are created. Wrap your code in a quick call to weave.blitz to get rid of the temporaries and have the behaviour your want.</p>
<p>Read the weave.blitz section of <a hre... | 4 | 2010-04-10T13:59:41Z | [
"python",
"numpy",
"slice"
] |
Python/numpy tricky slicing problem | 1,587,367 | <p>I have a problem with some numpy stuff. I need a numpy array to behave in an unusual manner by returning a slice as a view of the data I have sliced, not a copy. So heres an example of what I want to do:</p>
<p>Say we have a simple array like this:</p>
<pre><code>a = array([1, 0, 0, 0])
</code></pre>
<p>I would l... | 3 | 2009-10-19T07:28:08Z | 14,805,047 | <p>In the end I came up with the same problem as you. I had to resort to use Jacobi iteration and weaver:</p>
<pre><code> while (iter_n < max_time_steps):
expr = "field[1:-1, 1:-1] = (field[2:, 1:-1] "\
"+ field[:-2, 1:-1]+"\
... | 1 | 2013-02-11T02:21:10Z | [
"python",
"numpy",
"slice"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 1,587,434 | <p>Think about how would you write lazy-cat with recur in clojure. </p>
| -1 | 2009-10-19T07:55:27Z | [
"python",
"clojure"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 1,587,582 | <p>I agree with Pavel, what is intuitive is subjective. Because I'm (slowly) starting to grok Haskell, I can tell what the Clojure code does, even though I've never written a line of Clojure in my life. So I would consider the Clojure line fairly intuitive, because I've seen it before and I'm adapting to a more functio... | 36 | 2009-10-19T08:45:49Z | [
"python",
"clojure"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 1,588,260 | <p>The <a href="http://en.wikibooks.org/wiki/Clojure%5FProgramming/Examples/Lazy%5FFibonacci" rel="nofollow">wiki</a> has an in depth treatment of various Fibonacci implementations in Clojure which may interest you if you haven't seen it already.</p>
| 5 | 2009-10-19T11:53:41Z | [
"python",
"clojure"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 1,588,457 | <p>You should always use a language that fits the problem<sup><code>*</code></sup>. Your Python example is just lower level than the Clojure one -- easier to understand for beginners, but more tedious to write and parse for someone who knows the fitting higher level concepts.</p>
<p><sup><code>*</code></sup> <em>By t... | 2 | 2009-10-19T12:37:36Z | [
"python",
"clojure"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 1,590,285 | <pre><code>(take 5 fibs)
</code></pre>
<p>Seems about as intuitive as it could possibly get. I mean, that is exactly what you're doing. You don't even need to understand anything about the language, or even know what language that is, in order to know what should happen.</p>
| -5 | 2009-10-19T18:23:07Z | [
"python",
"clojure"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 1,590,295 | <p>The Clojure code is intuitive to me (because I know Clojure). If you want something that might look more like something you're familiar with, you can try this, an efficient and overly-verbose recursive version:</p>
<pre><code>(use 'clojure.contrib.def) ; SO's syntax-highlighting still sucks
(defn-memo fib [n]
... | 6 | 2009-10-19T18:26:49Z | [
"python",
"clojure"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 1,590,548 | <p>If you didn't know any imperative languages, would this be intuitive for you?</p>
<pre><code>a = a + 5
</code></pre>
<p>WTF? <code>a</code> <em>clearly</em> isn't the same as <code>a + 5</code>.</p>
<p>if <code>a = a + 5</code>, is <code>a + 5 = a</code>?</p>
<p>Why doesn't this work???</p>
<pre><code>if (a = 5... | 12 | 2009-10-19T19:16:12Z | [
"python",
"clojure"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 1,701,420 | <p>Here is one solution.</p>
<pre><code>(defn fib-seq [a b]
(cons (+ a b) (lazy-seq (fib-seq (+ a b) a))))
(def fibs (concat [1 1] (fib-seq 1 1)))
user=> (take 10 fibs)
(1 1 2 3 5 8 13 21 34 55)
</code></pre>
| 2 | 2009-11-09T14:41:07Z | [
"python",
"clojure"
] |
Lazy infinite sequences in Clojure and Python | 1,587,412 | <p>Here are the best implementations I could find for lazy infinite sequences of Fibonacci numbers in both Clojure and Python:</p>
<p>Clojure:</p>
<pre><code>(def fib-seq (lazy-cat [0 1]
(map + fib-seq (rest fib-seq))))
</code></pre>
<p>sample usage:</p>
<pre><code> (take 5 fib-seq)
</code></pre>
<p>Python:</p>
... | 15 | 2009-10-19T07:47:41Z | 3,547,483 | <p>I like:</p>
<pre><code>(def fibs
(map first
(iterate
(fn [[ a, b ]]
[ b, (+ a b) ])
[0, 1])))
</code></pre>
<p>Which seems to have some similarities to the python/generator version.</p>
| 14 | 2010-08-23T12:25:30Z | [
"python",
"clojure"
] |
what is the correct way to process 4 bits inside an octet in python | 1,587,496 | <p>I'm writing an application to parse certain network packets. A packet field contains the protocol version number in an octet, so that 4 high bits are the 'major' and low 4 are the 'minor' version. Currently I am parsing them as follows, but am wondering if there is a prettier or more 'pythonic' way of doing it:</p>
... | 1 | 2009-10-19T08:18:26Z | 1,587,501 | <p>You can write binary literals like this<code>0b1111000</code></p>
<p>For your example I would proabbly use hex though</p>
<pre><code>v = ord(data[17])
major = (v & 0xF0) >> 4
minor = (v & 0x0F)
</code></pre>
<p>You might also want to use the <a href="http://docs.python.org/library/struct.html" rel="... | 2 | 2009-10-19T08:21:03Z | [
"python",
"bit-manipulation"
] |
what is the correct way to process 4 bits inside an octet in python | 1,587,496 | <p>I'm writing an application to parse certain network packets. A packet field contains the protocol version number in an octet, so that 4 high bits are the 'major' and low 4 are the 'minor' version. Currently I am parsing them as follows, but am wondering if there is a prettier or more 'pythonic' way of doing it:</p>
... | 1 | 2009-10-19T08:18:26Z | 1,587,509 | <p>It would be neater to use literals instead of calling <code>int</code>. You can use binary literals or hex, for example:</p>
<pre><code>major = (v & 0xf0) >> 4
minor = (v & 0x0f)
</code></pre>
<p>Binary literals only work for Python 2.6 or later and are of the form <code>0b11110000</code>. If you are... | 1 | 2009-10-19T08:23:11Z | [
"python",
"bit-manipulation"
] |
what is the correct way to process 4 bits inside an octet in python | 1,587,496 | <p>I'm writing an application to parse certain network packets. A packet field contains the protocol version number in an octet, so that 4 high bits are the 'major' and low 4 are the 'minor' version. Currently I am parsing them as follows, but am wondering if there is a prettier or more 'pythonic' way of doing it:</p>
... | 1 | 2009-10-19T08:18:26Z | 1,587,560 | <p>Just one hint, you'd better apply the mask for the major <em>after</em> shifting, in case it's a negative number and the sign is kept:</p>
<pre><code>major = (v >> 4) & 0x0f
minor = (v & 0x0f)
</code></pre>
| 0 | 2009-10-19T08:38:00Z | [
"python",
"bit-manipulation"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.