title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags list |
|---|---|---|---|---|---|---|---|---|---|
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 16,028,076 | <p>The <a href="http://en.wikipedia.org/wiki/YAML" rel="nofollow">YAML</a> parser can help you figure out what datatype your string is. Use <code>yaml.load()</code>, and then you can use <code>type(result)</code> to test for type:</p>
<pre><code>>>> import yaml
>>> a = "545.2222"
>>> result... | 7 | 2013-04-16T03:03:11Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 17,815,252 | <h2>Localization and commas</h2>
<p>You should consider the possibility of commas in the string representation of a number, for cases like <code>float("545,545.2222")</code> which throws an exception. Instead, use methods in <code>locale</code> to convert the strings to numbers and interpret commas correctly. The <co... | 26 | 2013-07-23T16:00:21Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 20,929,983 | <h2>Python method to check if a string is a float:</h2>
<pre><code>def isfloat(value):
try:
float(value)
return True
except:
return False
</code></pre>
<h2>What is, and is not a float in <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29">Python</a> may surprise you:</h2>
<pre><c... | 246 | 2014-01-05T04:15:39Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 25,299,501 | <p>If you aren't averse to third-party modules, you could check out the <a href="https://pypi.python.org/pypi/fastnumbers">fastnumbers</a> module. It provides a function called <a href="http://pythonhosted.org//fastnumbers/fast.html#fast-real">fast_real</a> that does exactly what this question is asking for and does it... | 8 | 2014-08-14T03:21:37Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 31,588,754 | <blockquote>
<p><strong>In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222? Or parse the string "31" to an integer, 31?</strong>
I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
</blockquote>
<p>It's good t... | 6 | 2015-07-23T13:26:05Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 33,017,514 | <pre><code>def get_int_or_float(v):
try:
number_float = float(v)
number_int = int(v)
return number_int if number_float == number_int else number_float
except ValueError:
raise
</code></pre>
| 3 | 2015-10-08T13:39:08Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 33,044,577 | <pre><code>def num(s):
"""num(s)
num(3),num(3.7)-->3
num('3')-->3, num('3.7')-->3.7
num('3,700')-->ValueError
num('3a'),num('a3'),-->ValueError
num('3e4') --> 30000.0
"""
try:
return int(s)
except ValueError:
try:
return float(s)
except ValueError:
raise ValueError('argumen... | 5 | 2015-10-09T18:04:52Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 39,437,128 | <pre><code>>>> str_float = "545.2222"
>>> float(str_float)
545.2222
>>> type(_) #check its type
<type 'float'>
>>> str_int = "31"
>>> int(str_int)
31
>>> type(_) #check its type
<type 'int'>
</code></pre>
| 0 | 2016-09-11T14:32:41Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
Parse String to Float or Int | 379,906 | <p>In Python, how can I parse a numeric string like <code>"545.2222"</code> to its corresponding float value, <code>542.2222</code>? Or parse the string <code>"31"</code> to an integer, <code>31</code>?</p>
<p>I just want to know how to parse a float string to a float, and (separately) an int string to an int.</p>
| 1,130 | 2008-12-19T01:52:26Z | 39,940,402 | <pre><code>def num(s):
try:
for each in s:
yield int(each)
except ValueError:
yield float(each)
a = num(["123.55","345","44"])
print a.next()
print a.next()
</code></pre>
<p>This is the most Pythonic way i could come up with. </p>
| 0 | 2016-10-09T05:40:12Z | [
"python",
"string",
"parsing",
"floating-point",
"integer"
] |
How do you make a class attribute that isn't a standard data type? | 379,995 | <p>I have one class that needs to grab an attribute that is set in another. It's not a standard data type though. Here's the code;</p>
<pre><code>class graphics:
def __init__(self, Fullscreen = False, Width = 640, Height = 480):
print "Graphics Init"
SCREEN_SIZE = (Width, Height)
pygame.init()
if Fulls... | 0 | 2008-12-19T03:03:34Z | 380,001 | <p>It's likely an <code>object</code> type so</p>
<pre><code>self.screen = object()
</code></pre>
<p>might work (If I understood your question correctly).</p>
| 0 | 2008-12-19T03:09:01Z | [
"python",
"pygame"
] |
How do you make a class attribute that isn't a standard data type? | 379,995 | <p>I have one class that needs to grab an attribute that is set in another. It's not a standard data type though. Here's the code;</p>
<pre><code>class graphics:
def __init__(self, Fullscreen = False, Width = 640, Height = 480):
print "Graphics Init"
SCREEN_SIZE = (Width, Height)
pygame.init()
if Fulls... | 0 | 2008-12-19T03:03:34Z | 380,018 | <p>ugh...PEBKAC.</p>
<p>I'm so used to C that I keep forgetting you can do more than just prototype outside of defs.</p>
<p>Rewrote as this and it worked:</p>
<pre><code>class graphics:
SCREEN_SIZE = (640, 480)
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
def __init__(self, Fullscreen = False, Width = 640, H... | 0 | 2008-12-19T03:22:25Z | [
"python",
"pygame"
] |
How do you make a class attribute that isn't a standard data type? | 379,995 | <p>I have one class that needs to grab an attribute that is set in another. It's not a standard data type though. Here's the code;</p>
<pre><code>class graphics:
def __init__(self, Fullscreen = False, Width = 640, Height = 480):
print "Graphics Init"
SCREEN_SIZE = (Width, Height)
pygame.init()
if Fulls... | 0 | 2008-12-19T03:03:34Z | 380,026 | <p>You rarely, if ever, reference attributes of a class. You reference attributes of an object.</p>
<p>(Also, class names should be uppercase: <code>Graphics</code>).</p>
<pre><code>class Graphics:
SCREEN_SIZE = (640, 480)
def __init__(self, Fullscreen = False, Width = 640, Height = 480):
print "Graphics Init"
... | 0 | 2008-12-19T03:28:48Z | [
"python",
"pygame"
] |
How do you make a class attribute that isn't a standard data type? | 379,995 | <p>I have one class that needs to grab an attribute that is set in another. It's not a standard data type though. Here's the code;</p>
<pre><code>class graphics:
def __init__(self, Fullscreen = False, Width = 640, Height = 480):
print "Graphics Init"
SCREEN_SIZE = (Width, Height)
pygame.init()
if Fulls... | 0 | 2008-12-19T03:03:34Z | 380,666 | <p>I'm thinking that a short explanation of the difference between class and instance attributes in Python might be helpful to you.</p>
<p>When you write code like so:</p>
<pre><code>class Graphics:
screen_size = (1024, 768)
</code></pre>
<p>The class <code>Graphics</code> is actually an object itself -- a class... | 1 | 2008-12-19T11:01:06Z | [
"python",
"pygame"
] |
How do I respond to mouse clicks on sprites in PyGame? | 380,420 | <p>What is the canonical way of making your sprites respond to mouse clicks in PyGame ? </p>
<p>Here's something simple, in my event loop:</p>
<pre><code>for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game()
[...]
elif ( event.type == pygame.MOUSEBUTTONDOWN and
py... | 5 | 2008-12-19T08:54:57Z | 393,256 | <p>I usually give my clickable objects a click function, like in your example. I put all of those objects in a list, for easy iteration when the click functions are to be called.</p>
<p>when checking for which mousebutton you press, use the button property of the event.</p>
<pre><code>import pygame
from pygame.locals... | 10 | 2008-12-25T22:46:33Z | [
"python",
"pygame"
] |
How do I get the full XML or HTML content of an element using ElementTree? | 380,603 | <p>That is, all text and subtags, without the tag of an element itself?</p>
<p>Having</p>
<pre><code><p>blah <b>bleh</b> blih</p>
</code></pre>
<p>I want </p>
<pre><code>blah <b>bleh</b> blih
</code></pre>
<p>element.text returns "blah " and etree.tostring(element) returns:</p>
... | 5 | 2008-12-19T10:33:52Z | 380,717 | <p>ElementTree works perfectly, you have to assemble the answer yourself. Something like this...</p>
<pre><code>"".join( [ "" if t.text is None else t.text ] + [ xml.tostring(e) for e in t.getchildren() ] )
</code></pre>
<p>Thanks to JV amd PEZ for pointing out the errors.</p>
<p><hr /></p>
<p>Edit.</p>
<pre><cod... | 11 | 2008-12-19T11:21:52Z | [
"python",
"xml",
"api",
"elementtree"
] |
How do I get the full XML or HTML content of an element using ElementTree? | 380,603 | <p>That is, all text and subtags, without the tag of an element itself?</p>
<p>Having</p>
<pre><code><p>blah <b>bleh</b> blih</p>
</code></pre>
<p>I want </p>
<pre><code>blah <b>bleh</b> blih
</code></pre>
<p>element.text returns "blah " and etree.tostring(element) returns:</p>
... | 5 | 2008-12-19T10:33:52Z | 380,719 | <p>No idea if an external library might be an option, but anyway -- assuming there is one <code><p></code> with this text on the page, a jQuery-solution would be:</p>
<pre><code>alert($('p').html()); // returns blah <b>bleh</b> blih
</code></pre>
| -3 | 2008-12-19T11:23:59Z | [
"python",
"xml",
"api",
"elementtree"
] |
How do I get the full XML or HTML content of an element using ElementTree? | 380,603 | <p>That is, all text and subtags, without the tag of an element itself?</p>
<p>Having</p>
<pre><code><p>blah <b>bleh</b> blih</p>
</code></pre>
<p>I want </p>
<pre><code>blah <b>bleh</b> blih
</code></pre>
<p>element.text returns "blah " and etree.tostring(element) returns:</p>
... | 5 | 2008-12-19T10:33:52Z | 380,783 | <p>I doubt ElementTree is the thing to use for this. But assuming you have strong reasons for using it maybe you could try stripping the root tag from the fragment:</p>
<pre><code> re.sub(r'(^<%s\b.*?>|</%s\b.*?>$)' % (element.tag, element.tag), '', ElementTree.tostring(element))
</code></pre>
| 1 | 2008-12-19T11:56:30Z | [
"python",
"xml",
"api",
"elementtree"
] |
How do I get the full XML or HTML content of an element using ElementTree? | 380,603 | <p>That is, all text and subtags, without the tag of an element itself?</p>
<p>Having</p>
<pre><code><p>blah <b>bleh</b> blih</p>
</code></pre>
<p>I want </p>
<pre><code>blah <b>bleh</b> blih
</code></pre>
<p>element.text returns "blah " and etree.tostring(element) returns:</p>
... | 5 | 2008-12-19T10:33:52Z | 381,614 | <p>This is the solution I ended up using:</p>
<pre><code>def element_to_string(element):
s = element.text or ""
for sub_element in element:
s += etree.tostring(sub_element)
s += element.tail
return s
</code></pre>
| 5 | 2008-12-19T17:27:09Z | [
"python",
"xml",
"api",
"elementtree"
] |
How do I get the full XML or HTML content of an element using ElementTree? | 380,603 | <p>That is, all text and subtags, without the tag of an element itself?</p>
<p>Having</p>
<pre><code><p>blah <b>bleh</b> blih</p>
</code></pre>
<p>I want </p>
<pre><code>blah <b>bleh</b> blih
</code></pre>
<p>element.text returns "blah " and etree.tostring(element) returns:</p>
... | 5 | 2008-12-19T10:33:52Z | 34,084,933 | <p>These are good answers, which answer the OP's question, particularly if the question is confined to HTML. But documents are inherently messy, and the depth of element nesting is usually impossible to predict.</p>
<p>To simulate DOM's getTextContent() you would have to use a (very) simple recursive mechanism. </p>... | 3 | 2015-12-04T09:29:26Z | [
"python",
"xml",
"api",
"elementtree"
] |
How do I get PIL to work when built on mingw/cygwin? | 380,731 | <p>I'm trying to build PIL 1.1.6 against cygwin or mingw whilst running against a windows install of python. When I do either the build works but I get the following failure when trying to save files.</p>
<pre>
$ python25
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
win32
Type "help"... | 3 | 2008-12-19T11:31:12Z | 471,677 | <p>As far as I can tell from some cursory Google searching, you need to rebase the DLLs after building PIL in order for it to work properly on Cygwin.</p>
<p>References: </p>
<ul>
<li><p><strong><a href="http://jetfar.com/cygwin-install-python-imaging-library/" rel="nofollow">http://jetfar.com/cygwin-install-python-i... | 1 | 2009-01-23T02:29:23Z | [
"python",
"windows",
"cygwin",
"python-imaging-library"
] |
How to do this - python dictionary traverse and search | 380,734 | <p>I have nested dictionaries:</p>
<pre><code>{'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
u'key1': {'attrs': {'entity': 'r',
'hash': '34njasd3h43b4n3',
'id': '4130-1'},
u'key2': {'attrs': {'entity': ... | 7 | 2008-12-19T11:33:06Z | 380,759 | <p>Well, if you have to do it only a few times, you can just use nested dict.iteritems() to find what you are looking for.</p>
<p>If you plan to do it several times, performances will quickly becomes an issue. In that case you could :</p>
<ul>
<li><p>change the way you data is returned to you to something more suitab... | 0 | 2008-12-19T11:43:22Z | [
"python",
"dictionary",
"parsing"
] |
How to do this - python dictionary traverse and search | 380,734 | <p>I have nested dictionaries:</p>
<pre><code>{'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
u'key1': {'attrs': {'entity': 'r',
'hash': '34njasd3h43b4n3',
'id': '4130-1'},
u'key2': {'attrs': {'entity': ... | 7 | 2008-12-19T11:33:06Z | 380,769 | <p>If you want to solve the problem in a general way, no matter how many level of nesting you have in your dict, then create a recursive function which will traverse the tree:</p>
<pre><code>def traverse_tree(dictionary, id=None):
for key, value in dictionary.items():
if key == 'id':
if value =... | 12 | 2008-12-19T11:44:56Z | [
"python",
"dictionary",
"parsing"
] |
How to do this - python dictionary traverse and search | 380,734 | <p>I have nested dictionaries:</p>
<pre><code>{'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
u'key1': {'attrs': {'entity': 'r',
'hash': '34njasd3h43b4n3',
'id': '4130-1'},
u'key2': {'attrs': {'entity': ... | 7 | 2008-12-19T11:33:06Z | 380,874 | <p>Your structure is unpleasantly irregular. Here's a version with a <strong>Visitor</strong> function that traverses the <code>attrs</code> sub-dictionaries.</p>
<pre><code>def walkDict( aDict, visitor, path=() ):
for k in aDict:
if k == 'attrs':
visitor( path, aDict[k] )
elif type(a... | 12 | 2008-12-19T12:45:28Z | [
"python",
"dictionary",
"parsing"
] |
How to do this - python dictionary traverse and search | 380,734 | <p>I have nested dictionaries:</p>
<pre><code>{'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
u'key1': {'attrs': {'entity': 'r',
'hash': '34njasd3h43b4n3',
'id': '4130-1'},
u'key2': {'attrs': {'entity': ... | 7 | 2008-12-19T11:33:06Z | 380,987 | <p>This kind of problem is often better solved with proper class definitions, not generic dictionaries.</p>
<pre><code>class ProperObject( object ):
"""A proper class definition for each "attr" dictionary."""
def __init__( self, path, attrDict ):
self.path= path
self.__dict__.update( attrDict )... | 9 | 2008-12-19T13:37:03Z | [
"python",
"dictionary",
"parsing"
] |
How to do this - python dictionary traverse and search | 380,734 | <p>I have nested dictionaries:</p>
<pre><code>{'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
u'key1': {'attrs': {'entity': 'r',
'hash': '34njasd3h43b4n3',
'id': '4130-1'},
u'key2': {'attrs': {'entity': ... | 7 | 2008-12-19T11:33:06Z | 16,508,033 | <p>This is an old question but still a top google result, so I'll update:</p>
<p>A friend and myself published a library to solve (very nearly) this exact problem. dpath-python (no relation to the perl dpath module which does similar things).</p>
<p><a href="http://github.com/akesterson/dpath-python" rel="nofollow">h... | 1 | 2013-05-12T13:46:28Z | [
"python",
"dictionary",
"parsing"
] |
How to do this - python dictionary traverse and search | 380,734 | <p>I have nested dictionaries:</p>
<pre><code>{'key0': {'attrs': {'entity': 'p', 'hash': '34nj3h43b4n3', 'id': '4130'},
u'key1': {'attrs': {'entity': 'r',
'hash': '34njasd3h43b4n3',
'id': '4130-1'},
u'key2': {'attrs': {'entity': ... | 7 | 2008-12-19T11:33:06Z | 23,568,748 | <p>Since recursion is known to be limited in python (see
<a href="http://stackoverflow.com/questions/3323001/maximum-recursion-depth">Maximum recursion depth?</a>)
I would rather have a loop based answer to this question, so the answer can be adapted to any level of depth in the dictionary. For that, the function </p>... | 0 | 2014-05-09T15:41:51Z | [
"python",
"dictionary",
"parsing"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 380,876 | <p>I don't know if it's pythonic enough, but in the Java world listening on a defined port is a pretty widely used solution, as it works on all major platforms and doesn't have any problems with crashing programs.</p>
<p>Another advantage of listening to a port is that you could send a command to the running instance.... | 16 | 2008-12-19T12:46:38Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 380,894 | <p>This may work.</p>
<ol>
<li><p>Attempt create a PID file to a known location. If you fail, someone has the file locked, you're done.</p></li>
<li><p>When you finish normally, close and remove the PID file, so someone else can overwrite it.</p></li>
</ol>
<p>You can wrap your program in a shell script that removes... | 4 | 2008-12-19T12:56:55Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 380,904 | <p>Using a lock-file is a quite common approach on unix. If it crashes, you have to clean up manually. You could stor the PID in the file, and on startup check if there is a process with this PID, overriding the lock-file if not. (However, you also need a lock around the read-file-check-pid-rewrite-file). You will ... | 3 | 2008-12-19T13:01:54Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 380,943 | <p>Use a pid file. You have some known location, "/path/to/pidfile" and at startup you do something like this (partially pseudocode because I'm pre-coffee and don't want to work all that hard):</p>
<pre><code>import os, os.path
pidfilePath = """/path/to/pidfile"""
if os.path.exists(pidfilePath):
pidfile = open(pid... | 5 | 2008-12-19T13:16:39Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 384,493 | <p>Simple, <s>cross-platform</s> solution, found in <strong><a href="http://stackoverflow.com/questions/220525/ensuring-a-single-instance-of-an-application-in-linux#221159">another question</a></strong> by <a href="http://stackoverflow.com/users/12138/zgoda">zgoda</a>:</p>
<pre><code>import fcntl, sys
pid_file = 'prog... | 25 | 2008-12-21T14:02:47Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 385,862 | <p>You already found reply to similar question in another thread, so for completeness sake see how to achieve the same on Windows uning named mutex.</p>
<p><a href="http://code.activestate.com/recipes/474070/" rel="nofollow">http://code.activestate.com/recipes/474070/</a></p>
| 4 | 2008-12-22T09:35:09Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 1,265,445 | <p>The following code should do the job, it is cross-platform and runs on Python 2.4-3.2. I tested it on Windows, OS X and Linux.</p>
<pre><code>from tendo import singleton
me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running
</code></pre>
<p>The latest code version is available <a href="h... | 56 | 2009-08-12T10:45:24Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 1,662,504 | <p>This code is Linux specific ( it uses 'abstract' UNIX domain sockets ) but it is simple and won't leave stale lock files around. I prefer it to the solution above because it doesn't require a specially reserved TCP port. </p>
<pre><code>try:
import socket
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM... | 19 | 2009-11-02T17:10:48Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 4,337,301 | <p>I'm posting this as an answer because I'm a new user and Stack Overflow won't let me vote yet.</p>
<p>Sorin Sbarnea's solution works for me under OS X, Linux and Windows, and I am grateful for it.</p>
<p>However, tempfile.gettempdir() behaves one way under OS X and Windows and another under other some/many/all(?) ... | 2 | 2010-12-02T16:29:22Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 7,258,093 | <p>Never written python before, but this is what I've just implemented in mycheckpoint, to prevent it being started twice or more by crond:</p>
<pre><code>import os
import sys
import fcntl
fh=0
def run_once():
global fh
fh=open(os.path.realpath(__file__),'r')
try:
fcntl.flock(fh,fcntl.LOCK_EX|fcntl... | 5 | 2011-08-31T14:02:54Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 15,135,898 | <p>I would just do something like: </p>
<pre><code>if commands.getstatusoutput("mkdir /tmp/test")[0]:
print "Exiting"
sys.exit(1)
</code></pre>
<p>And somewhere at the end of my code, I'll remove the directory. <code>mkdir</code> is a atomic and works pretty well here. </p>
| -1 | 2013-02-28T12:45:15Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 17,838,832 | <p>I keep suspecting there ought to be a good POSIXy solution using process groups, without having to hit the file system, but I can't quite nail it down. Something like:</p>
<p>On startup, your process sends a 'kill -0' to all processes in a particular group. If any such processes exist, it exits. Then it joins the g... | 0 | 2013-07-24T15:44:41Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 23,262,587 | <p>I use <code>single_process</code> on my gentoo;</p>
<pre><code>pip install single_process
</code></pre>
<p><strong>example</strong>: </p>
<pre><code>from single_process import single_process
@single_process
def main():
print 1
if __name__ == "__main__":
main()
</code></pre>
<p>refer: <a href="https:... | 2 | 2014-04-24T07:31:24Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 28,186,309 | <p>I ran into this exact problem last week, and although I did find some good solutions, I decided to make a very simple and clean python package and uploaded it to PyPI. It differs from tendo in that it can lock any string resource name. Although you could certainly lock <code>__file__</code> to achieve the same effec... | 0 | 2015-01-28T06:51:51Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 33,127,087 | <p>linux example</p>
<p>This method is based on the creation of a temporary file automatically deleted after you close the application.
the program launch we verify the existence of the file;
if the file exists ( there is a pending execution) , the program is closed ; otherwise it creates the file and continues the ex... | 0 | 2015-10-14T13:44:47Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 36,489,564 | <p>For anybody using <strong>wxPython</strong> for their application, you can use the function <code>wx.SingleInstanceChecker</code> <a href="https://wxpython.org/Phoenix/docs/html/wx.SingleInstanceChecker.html" rel="nofollow">documented here</a>.</p>
<p>I personally use a subclass of <code>wx.App</code> which makes ... | 2 | 2016-04-08T00:34:12Z | [
"python",
"locking"
] |
Python: single instance of program | 380,870 | <p>Is there a Pythonic way to have only one instance of a program running? </p>
<p>The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? ... | 66 | 2008-12-19T12:42:52Z | 39,665,123 | <p>Here is my eventual Windows-only solution. Put the following into a module, perhaps called 'onlyone.py', or whatever. Include that module directly into your __ main __ python script file.</p>
<pre><code>import win32event, win32api, winerror, time, sys, os
main_path = os.path.abspath(sys.modules['__main__'].__file... | 0 | 2016-09-23T16:04:05Z | [
"python",
"locking"
] |
Psyco x64? | 381,479 | <p>Is there a way to get the same sort of speedup on x64 architecture as you can get from psyco on 32 bit processors?</p>
| 3 | 2008-12-19T16:40:38Z | 381,618 | <p>No, unfortunately, Psyco <a href="http://psyco.sourceforge.net/introduction.html" rel="nofollow">only runs on 32-bit x86 right now</a>.</p>
| 5 | 2008-12-19T17:28:33Z | [
"python",
"psyco"
] |
Comparing massive lists of dictionaries in python | 382,466 | <p>I never actually thought I'd run into speed-issues with python, but I have. I'm trying to compare really big lists of dictionaries to each other based on the dictionary values. I compare two lists, with the first like so</p>
<pre><code>biglist1=[{'transaction':'somevalue', 'id':'somevalue', 'date':'somevalue' ...},... | 11 | 2008-12-19T22:49:15Z | 382,483 | <p>What you want to do is to use correct data structures: </p>
<ol>
<li><p>Create a dictionary of mappings of tuples of other values in the first dictionary to their id.</p></li>
<li><p>Create two sets of tuples of values in both dictionaries. Then use set operations to get the tuple set you want.</p></li>
<li><p>Use ... | 4 | 2008-12-19T22:58:09Z | [
"python"
] |
Comparing massive lists of dictionaries in python | 382,466 | <p>I never actually thought I'd run into speed-issues with python, but I have. I'm trying to compare really big lists of dictionaries to each other based on the dictionary values. I compare two lists, with the first like so</p>
<pre><code>biglist1=[{'transaction':'somevalue', 'id':'somevalue', 'date':'somevalue' ...},... | 11 | 2008-12-19T22:49:15Z | 382,495 | <p>In O(m*n)...</p>
<pre><code>for item in biglist2:
for transaction in biglist1:
if (item['transaction'] == transaction['transaction'] &&
item['date'] == transaction['date'] &&
item['foo'] == transaction['foo'] ) :
list_transactionnamematches.append(transact... | 0 | 2008-12-19T23:02:50Z | [
"python"
] |
Comparing massive lists of dictionaries in python | 382,466 | <p>I never actually thought I'd run into speed-issues with python, but I have. I'm trying to compare really big lists of dictionaries to each other based on the dictionary values. I compare two lists, with the first like so</p>
<pre><code>biglist1=[{'transaction':'somevalue', 'id':'somevalue', 'date':'somevalue' ...},... | 11 | 2008-12-19T22:49:15Z | 382,507 | <p>Forgive my rusty python syntax, it's been a while, so consider this partially pseudocode</p>
<pre><code>import operator
biglist1.sort(key=(operator.itemgetter(2),operator.itemgetter(0)))
biglist2.sort(key=(operator.itemgetter(2),operator.itemgetter(0)))
i1=0;
i2=0;
while i1 < len(biglist1) and i2 < len(biglis... | 1 | 2008-12-19T23:07:13Z | [
"python"
] |
Comparing massive lists of dictionaries in python | 382,466 | <p>I never actually thought I'd run into speed-issues with python, but I have. I'm trying to compare really big lists of dictionaries to each other based on the dictionary values. I compare two lists, with the first like so</p>
<pre><code>biglist1=[{'transaction':'somevalue', 'id':'somevalue', 'date':'somevalue' ...},... | 11 | 2008-12-19T22:49:15Z | 382,707 | <p>Index on the fields you want to use for lookup. O(n+m)</p>
<pre><code>matches = []
biglist1_indexed = {}
for item in biglist1:
biglist1_indexed[(item["transaction"], item["date"])] = item
for item in biglist2:
if (item["transaction"], item["date"]) in biglist1_indexed:
matches.append(item)
</code></pre>
<p>... | 18 | 2008-12-20T01:01:00Z | [
"python"
] |
Comparing massive lists of dictionaries in python | 382,466 | <p>I never actually thought I'd run into speed-issues with python, but I have. I'm trying to compare really big lists of dictionaries to each other based on the dictionary values. I compare two lists, with the first like so</p>
<pre><code>biglist1=[{'transaction':'somevalue', 'id':'somevalue', 'date':'somevalue' ...},... | 11 | 2008-12-19T22:49:15Z | 382,739 | <p>The approach I would probably take to this is to make a very, very lightweight class with one instance variable and one method. The instance variable is a pointer to a dictionary; the method overrides the built-in special method <code>__hash__(self)</code>, returning a value calculated from all the values in the di... | 0 | 2008-12-20T01:31:19Z | [
"python"
] |
Comparing massive lists of dictionaries in python | 382,466 | <p>I never actually thought I'd run into speed-issues with python, but I have. I'm trying to compare really big lists of dictionaries to each other based on the dictionary values. I compare two lists, with the first like so</p>
<pre><code>biglist1=[{'transaction':'somevalue', 'id':'somevalue', 'date':'somevalue' ...},... | 11 | 2008-12-19T22:49:15Z | 383,097 | <p>Have a look at Psyco. Its a Python compiler that can create very fast, optimized machine code from your source.</p>
<p><a href="http://sourceforge.net/projects/psyco/" rel="nofollow">http://sourceforge.net/projects/psyco/</a></p>
<p>While this isn't a direct solution to your code's efficiency issues, it could sti... | 0 | 2008-12-20T08:32:02Z | [
"python"
] |
Comparing massive lists of dictionaries in python | 382,466 | <p>I never actually thought I'd run into speed-issues with python, but I have. I'm trying to compare really big lists of dictionaries to each other based on the dictionary values. I compare two lists, with the first like so</p>
<pre><code>biglist1=[{'transaction':'somevalue', 'id':'somevalue', 'date':'somevalue' ...},... | 11 | 2008-12-19T22:49:15Z | 8,356,511 | <p>I'm also a newbie. My code is structured in much the same way as his. </p>
<pre><code>for A in biglist:
for B in biglist:
if ( A.get('somekey') <> B.get('somekey') and #don't match to itself
len( set(A.get('list')) - set(B.get('list')) ) > 10:
[do stuff...]
</code></pre... | 0 | 2011-12-02T12:44:44Z | [
"python"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,605 | <p>Off the top of the head, it can be useful as an initial value when searching for a minimum value.</p>
<p>For example:</p>
<pre><code>min = float('inf')
for x in somelist:
if x<min:
min=x
</code></pre>
<p>Which I prefer to setting <code>min</code> initially to the first value of <code>somelist</code></p... | 34 | 2008-12-19T23:55:12Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,618 | <p>When working in a problem domain where trig is used (especially tangent) infinity is an answer that can come up. Trig ends up being used heavily in graphics applications, games, and geospatial applications, plus the obvious math applications. </p>
| 1 | 2008-12-20T00:01:33Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,628 | <p>In some physics calculations you can normalize irregularities (ie, infinite numbers) of the same order with each other, canceling them both and allowing a approximate result to come through.</p>
<p>When you deal with limits, calculations like (infinity / infinity) -> approaching a finite a number could be achieved.... | 11 | 2008-12-20T00:07:03Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,642 | <p>I'm sure there are other ways to do this, but you could use Infinity to check for reasonable inputs in a String-to-Float conversion. In Java, at least, the Float.isNaN() static method will return false for numbers with infinite magnitude, indicating they are valid numbers, even though your program might want to clas... | 1 | 2008-12-20T00:19:37Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,674 | <p>Dijkstra's Algorithm typically assigns infinity as the initial edge weights in a graph. This doesn't <em>have</em> to be "infinity", just some arbitrarily constant but in java I typically use Double.Infinity. I assume ruby could be used similarly.</p>
| 34 | 2008-12-20T00:40:24Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,686 | <p><a href="http://en.wikipedia.org/wiki/Alpha-beta_pruning">Alpha-beta pruning</a></p>
| 8 | 2008-12-20T00:46:51Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,690 | <p>Some programmers use Infinity or <code>NaN</code>s to show a variable has never been initialized or assigned in the program.</p>
| 0 | 2008-12-20T00:51:04Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,735 | <p>I use it when I have a Range object where one or both ends need to be open</p>
| 2 | 2008-12-20T01:28:57Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 382,736 | <p>Use <code>Infinity</code> and <code>-Infinity</code> when implementing a mathematical algorithm calls for it.</p>
<p>In Ruby, <code>Infinity</code> and <code>-Infinity</code> have nice comparative properties so that <code>-Infinity</code> < <code>x</code> < <code>Infinity</code> for any real number <code>x</c... | 10 | 2008-12-20T01:29:23Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 383,348 | <p>There seems to be an implied "Why does this functionality even exist?" in your question. And the reason is that Ruby and Python are just giving access to the full range of values that one can specify in floating point form as specified by IEEE. </p>
<p>This page seems to describe it well:
<a href="http://steve.h... | 17 | 2008-12-20T14:03:13Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 386,291 | <p>I've used it in the <a href="http://en.wikipedia.org/wiki/Minimax" rel="nofollow">minimax algorithm</a>. When I'm generating new moves, if the min player wins on that node then the value of the node is -â. Conversely, if the max player wins then the value of that node is +â.</p>
<p>Also, if you're generating no... | 3 | 2008-12-22T13:56:57Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 398,869 | <p>I've used it in a DSL similar to Rails' <code>has_one</code> and <code>has_many</code>:</p>
<pre><code>has 0..1 :author
has 0..INFINITY :tags
</code></pre>
<p>This makes it easy to express concepts like Kleene star and plus in your DSL.</p>
| 3 | 2008-12-29T22:14:52Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 399,013 | <p>I use it to specify the mass and inertia of a static object in physics simulations. Static objects are essentially unaffected by gravity and other simulation forces.</p>
| 8 | 2008-12-29T23:05:55Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 419,337 | <p>I've used it for cases where you want to define ranges of preferences / allowed.</p>
<p>For example in 37signals apps you have like a limit to project number</p>
<pre><code>Infinity = 1 / 0.0
FREE = 0..1
BASIC = 0..5
PREMIUM = 0..Infinity
</code></pre>
<p>then you can do checks like </p>
<pre><code>if PREMIUM.in... | 5 | 2009-01-07T05:57:16Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 422,343 | <p>I've used symbolic values for positive and negative infinity in dealing with range comparisons to eliminate corner cases that would otherwise require special handling:</p>
<p>Given two ranges A=[a,b) and C=[c,d) do they intersect, is one greater than the other, or does one contain the other?</p>
<pre><code>A > ... | 2 | 2009-01-07T22:04:54Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 439,197 | <p>It is used quite extensively in graphics. For example, any pixel in a 3D image that is not part of an actual object is marked as infinitely far away. So that it can later be replaced with a background image.</p>
| 1 | 2009-01-13T14:49:21Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 1,213,937 | <p>I used it for representing camera focus distance and to my surprise in Python:</p>
<pre><code>>>> float("inf") is float("inf")
False
>>> float("inf") == float("inf")
True
</code></pre>
<p>I wonder why is that.</p>
| 4 | 2009-07-31T17:49:14Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 1,885,570 | <p>In Ruby infinity can be used to implement lazy lists. Say i want N numbers starting at 200 which get successively larger by 100 units each time:</p>
<pre><code>Inf = 1.0 / 0.0
(200..Inf).step(100).take(N)
</code></pre>
<p>More info here: <a href="http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in... | 6 | 2009-12-11T03:08:47Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 2,632,913 | <p>I ran across this because I'm looking for an "infinite" value to set for a maximum, if a given value doesn't exist, in an attempt to create a binary tree. (Because I'm selecting based on a range of values, and not just a single value, I quickly realized that even a hash won't work in my situation.)</p>
<p>Since I ... | 2 | 2010-04-13T20:18:39Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 5,601,710 | <p>I'm using a network library where you can specify the maximum number of reconnection attempts. Since I want mine to reconnect forever:</p>
<p><code>my_connection = ConnectionLibrary(max_connection_attempts = float('inf'))</code></p>
<p>In my opinion, it's more clear than the typical "set to -1 to retry forever" st... | 1 | 2011-04-08T23:18:20Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 8,032,341 | <p>You can to use:</p>
<pre><code>import decimal
decimal.Decimal("Infinity")
</code></pre>
<p>or:</p>
<pre><code>from decimal import *
Decimal("Infinity")
</code></pre>
| -2 | 2011-11-07T03:05:08Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 25,448,893 | <h1>For sorting</h1>
<p>I've seen it used as a sort value, to say "always sort these items to the bottom".</p>
| -1 | 2014-08-22T14:04:23Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 29,044,406 | <p>If you want the largest number from an input but they might use very large negatives. If I enter -13543124321.431 it still works out as the largest number since it's bigger than -inf.</p>
<pre><code>enter code here
initial_value = float('-inf')
while True:
try:
x = input('gimmee a number or type the wor... | 0 | 2015-03-14T01:20:30Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
In what contexts do programming languages make real use of an Infinity value? | 382,603 | <p>So in Ruby there is a trick to specify infinity:</p>
<pre><code>1.0/0
=> Infinity
</code></pre>
<p>I believe in Python you can do something like this</p>
<pre><code>float('inf')
</code></pre>
<p>These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use... | 25 | 2008-12-19T23:54:04Z | 29,147,218 | <h2>To specify a non-existent maximum</h2>
<p>If you're dealing with numbers, <code>nil</code> represents an unknown quantity, and should be preferred to <code>0</code> for that case. Similarly, <code>Infinity</code> represents an unbounded quantity, and should be preferred to <code>(arbitrarily_large_number)</code> i... | -1 | 2015-03-19T14:28:04Z | [
"python",
"ruby",
"language-agnostic",
"idioms",
"infinity"
] |
Is @measured a standard decorator? What library is it in? | 382,624 | <p>In <a href="http://abstracthack.wordpress.com/2007/09/05/multi-threaded-map-for-python/" rel="nofollow">this blog article</a> they use the construct:</p>
<pre><code> @measured
def some_func():
#...
# Presumably outputs something like "some_func() is finished in 121.333 s" somewhere
</code></pre>
<p>This <... | 2 | 2008-12-20T00:04:41Z | 382,627 | <p>Yes it's real. It's a function decorator. </p>
<p>Function decorators in Python are functions that take a function as it's single argument, and return a new function in it's place. </p>
<p><code>@classmethod</code> and <code>@staticmethod</code> are two built in function decorators.</p>
<p><a href="http://www.p... | 3 | 2008-12-20T00:05:52Z | [
"python",
"decorator"
] |
Is @measured a standard decorator? What library is it in? | 382,624 | <p>In <a href="http://abstracthack.wordpress.com/2007/09/05/multi-threaded-map-for-python/" rel="nofollow">this blog article</a> they use the construct:</p>
<pre><code> @measured
def some_func():
#...
# Presumably outputs something like "some_func() is finished in 121.333 s" somewhere
</code></pre>
<p>This <... | 2 | 2008-12-20T00:04:41Z | 382,655 | <p>measured is the name of a function that must be defined before that code will work.</p>
<p>In general any function used as a decorator must accept a function and return a function. The function will be replaced with the result of passing it to the decorator - measured() in this case.</p>
| 0 | 2008-12-20T00:29:56Z | [
"python",
"decorator"
] |
Is @measured a standard decorator? What library is it in? | 382,624 | <p>In <a href="http://abstracthack.wordpress.com/2007/09/05/multi-threaded-map-for-python/" rel="nofollow">this blog article</a> they use the construct:</p>
<pre><code> @measured
def some_func():
#...
# Presumably outputs something like "some_func() is finished in 121.333 s" somewhere
</code></pre>
<p>This <... | 2 | 2008-12-20T00:04:41Z | 382,666 | <p><code>@measured</code> decorates the some_func() function, using a function or class named <code>measured</code>. The <code>@</code> is the decorator syntax, <code>measured</code> is the decorator function name.</p>
<p>Decorators can be a bit hard to understand, but they are basically used to either wrap code aroun... | 13 | 2008-12-20T00:37:25Z | [
"python",
"decorator"
] |
Using Python's ctypes to pass/read a parameter declared as "struct_name *** param_name"? | 383,010 | <p>I am trying to use Python's ctypes library to access some methods in the scanning library <a href="http://www.sane-project.org/" rel="nofollow">SANE</a>. This is my first experience with ctypes and the first time I have had to deal with C datatypes in over a year so there is a fair learning curve here, but I think ... | 1 | 2008-12-20T06:26:39Z | 383,074 | <p>A <code>const SANE_Device ***</code> is a three-level pointer: it's a pointer to a pointer to a pointer to a constant SANE_Device. You can use the program <a href="http://gd.tuwien.ac.at/linuxcommand.org/man_pages/cdecl1.html">cdecl</a> to decipher complicated C/C++ type definitions.</p>
<p>According to the <a hre... | 7 | 2008-12-20T07:49:30Z | [
"python",
"pointers",
"ctypes",
"structure",
"sane"
] |
Django: How can I use my model classes to interact with my database from outside Django? | 383,073 | <p>I'd like to write a script that interacts with my DB using a Django app's model. However, I would like to be able to run this script from the command line or via cron. What all do I need to import to allow this?</p>
| 13 | 2008-12-20T07:48:15Z | 383,089 | <p>You need to set up the Django environment variables. These tell Python where your project is, and what the name of the settings module is (the project name in the settings module is optional):</p>
<pre><code>import os
os.environ['PYTHONPATH'] = '/path/to/myproject'
os.environ['DJANGO_SETTINGS_MODULE'] = 'myprojec... | 15 | 2008-12-20T08:18:44Z | [
"python",
"django",
"django-models"
] |
Django: How can I use my model classes to interact with my database from outside Django? | 383,073 | <p>I'd like to write a script that interacts with my DB using a Django app's model. However, I would like to be able to run this script from the command line or via cron. What all do I need to import to allow this?</p>
| 13 | 2008-12-20T07:48:15Z | 383,102 | <p>Depending on your specific needs, <a href="http://code.google.com/p/django-command-extensions/wiki/CommandExtensions">django-command-extensions</a> might save you a bit of time. To run any script as-is without messing around with environment variables just type:</p>
<pre><code>./manage.py runscript path/to/my/scri... | 5 | 2008-12-20T08:39:53Z | [
"python",
"django",
"django-models"
] |
Django: How can I use my model classes to interact with my database from outside Django? | 383,073 | <p>I'd like to write a script that interacts with my DB using a Django app's model. However, I would like to be able to run this script from the command line or via cron. What all do I need to import to allow this?</p>
| 13 | 2008-12-20T07:48:15Z | 383,246 | <p>The preferred way should be to add a <a href="http://docs.djangoproject.com/en/dev/howto/custom-management-commands/">custom command</a> and then run it as any other <code>django-admin</code> (not to be confused with <code>django.contrib.admin</code>) command:</p>
<pre><code>./manage.py mycustomcommand --customarg
... | 13 | 2008-12-20T12:06:39Z | [
"python",
"django",
"django-models"
] |
JavaScript implementation that allows access to [[Call]] | 383,189 | <p>The ECMA standard defines a hidden, internal property <code>[[Call]]</code>, which, if implemented, mean the object is callable / is a function.</p>
<p>In Python, something similar takes place, except that you can override it yourself to create your own callable objects:</p>
<pre><code>>>> class B:
... ... | 2 | 2008-12-20T10:47:09Z | 383,199 | <p>As far as I know it's not possible. It is supposed to be an internal property of an object and not exposed to the script itself. The only way I know is to define a function.</p>
<p>However, since functions is first class citizens you can add properties to them:</p>
<pre><code>function myfunc(){
var myself = argu... | 2 | 2008-12-20T11:04:11Z | [
"javascript",
"python",
"function"
] |
JavaScript implementation that allows access to [[Call]] | 383,189 | <p>The ECMA standard defines a hidden, internal property <code>[[Call]]</code>, which, if implemented, mean the object is callable / is a function.</p>
<p>In Python, something similar takes place, except that you can override it yourself to create your own callable objects:</p>
<pre><code>>>> class B:
... ... | 2 | 2008-12-20T10:47:09Z | 383,465 | <p>It is not possible yet: [[Call]] is hidden and not directly available in all conformat implementations. It may change in the <a href="http://wiki.ecmascript.org/doku.php?id=es3.1:es3.1_proposal_working_draft" rel="nofollow">new standard of ECMAScript</a>.</p>
| 0 | 2008-12-20T16:41:53Z | [
"javascript",
"python",
"function"
] |
JavaScript implementation that allows access to [[Call]] | 383,189 | <p>The ECMA standard defines a hidden, internal property <code>[[Call]]</code>, which, if implemented, mean the object is callable / is a function.</p>
<p>In Python, something similar takes place, except that you can override it yourself to create your own callable objects:</p>
<pre><code>>>> class B:
... ... | 2 | 2008-12-20T10:47:09Z | 384,249 | <p>[[Call]] is an internal property used to describe a particular piece of functionality in the language specification. There's no guarantee that such a property is even available in an interpreter. There are many other properties and objects that are referenced in the spec, such as the Completion object, which is on... | 1 | 2008-12-21T08:20:23Z | [
"javascript",
"python",
"function"
] |
JavaScript implementation that allows access to [[Call]] | 383,189 | <p>The ECMA standard defines a hidden, internal property <code>[[Call]]</code>, which, if implemented, mean the object is callable / is a function.</p>
<p>In Python, something similar takes place, except that you can override it yourself to create your own callable objects:</p>
<pre><code>>>> class B:
... ... | 2 | 2008-12-20T10:47:09Z | 996,597 | <p>Applications that use the Mozilla Spidermonkey implementation of Javascript 1.5 or greater (e.g. Firefox) have access to the <a href="https://developer.mozilla.org/en/Core%5FJavaScript%5F1.5%5FReference/Global%5FObjects/Object/noSuchMethod" rel="nofollow"><code>__noSuchMethod__</code></a> mechanism:</p>
<pre><code>... | 1 | 2009-06-15T15:00:06Z | [
"javascript",
"python",
"function"
] |
How to iterate over a list repeating each element in Python | 383,565 | <p>I'm using Python to <strong>infinitely</strong> iterate over a list, repeating each element in the list a number of times. For example given the list:</p>
<pre><code>l = [1, 2, 3, 4]
</code></pre>
<p>I would like to output each element two times and then repeat the cycle:</p>
<pre><code>1, 1, 2, 2, 3, 3, 4, 4, 1,... | 6 | 2008-12-20T18:23:52Z | 383,573 | <p>Solution should be something like</p>
<pre><code>iterable = [1, 2, 3, 4]
n = 2
while (True):
for elem in iterable:
for dummy in range(n):
print elem # or call function, or whatever
</code></pre>
<p>Edit: added 'While (True)' to iterate indefinitely.</p>
| 1 | 2008-12-20T18:30:05Z | [
"python",
"iterator"
] |
How to iterate over a list repeating each element in Python | 383,565 | <p>I'm using Python to <strong>infinitely</strong> iterate over a list, repeating each element in the list a number of times. For example given the list:</p>
<pre><code>l = [1, 2, 3, 4]
</code></pre>
<p>I would like to output each element two times and then repeat the cycle:</p>
<pre><code>1, 1, 2, 2, 3, 3, 4, 4, 1,... | 6 | 2008-12-20T18:23:52Z | 383,574 | <p>You could do it with a generator pretty easily:</p>
<pre><code>def cycle(iterable):
while True:
for item in iterable:
yield item
yield item
x=[1,2,3]
c=cycle(x)
print [c.next() for i in range(10)] // prints out [1,1,2,2,3,3,1,1,2,2]
</code></pre>
| 6 | 2008-12-20T18:31:49Z | [
"python",
"iterator"
] |
How to iterate over a list repeating each element in Python | 383,565 | <p>I'm using Python to <strong>infinitely</strong> iterate over a list, repeating each element in the list a number of times. For example given the list:</p>
<pre><code>l = [1, 2, 3, 4]
</code></pre>
<p>I would like to output each element two times and then repeat the cycle:</p>
<pre><code>1, 1, 2, 2, 3, 3, 4, 4, 1,... | 6 | 2008-12-20T18:23:52Z | 383,578 | <p>How about this:</p>
<pre><code>import itertools
def bicycle(iterable, repeat=1):
for item in itertools.cycle(iterable):
for _ in xrange(repeat):
yield item
c = bicycle([1,2,3,4], 2)
print [c.next() for _ in xrange(10)]
</code></pre>
<p>EDIT: incorporated <a href="http://stackoverflow.com/... | 12 | 2008-12-20T18:36:27Z | [
"python",
"iterator"
] |
How to iterate over a list repeating each element in Python | 383,565 | <p>I'm using Python to <strong>infinitely</strong> iterate over a list, repeating each element in the list a number of times. For example given the list:</p>
<pre><code>l = [1, 2, 3, 4]
</code></pre>
<p>I would like to output each element two times and then repeat the cycle:</p>
<pre><code>1, 1, 2, 2, 3, 3, 4, 4, 1,... | 6 | 2008-12-20T18:23:52Z | 383,585 | <pre><code>[ "%d, %d" % (i, i) for i in [1, 2, 3, 4] * 4]
</code></pre>
<p>The last 4 there is the number of cycles.</p>
| 0 | 2008-12-20T18:40:13Z | [
"python",
"iterator"
] |
How to iterate over a list repeating each element in Python | 383,565 | <p>I'm using Python to <strong>infinitely</strong> iterate over a list, repeating each element in the list a number of times. For example given the list:</p>
<pre><code>l = [1, 2, 3, 4]
</code></pre>
<p>I would like to output each element two times and then repeat the cycle:</p>
<pre><code>1, 1, 2, 2, 3, 3, 4, 4, 1,... | 6 | 2008-12-20T18:23:52Z | 386,685 | <pre><code>import itertools as it
def ncycle(iterable, n=1):
if n == 1:
return it.cycle(iterable)
return it.cycle(it.chain(*it.izip(*([iterable]*n))))
</code></pre>
| 1 | 2008-12-22T16:32:41Z | [
"python",
"iterator"
] |
How to iterate over a list repeating each element in Python | 383,565 | <p>I'm using Python to <strong>infinitely</strong> iterate over a list, repeating each element in the list a number of times. For example given the list:</p>
<pre><code>l = [1, 2, 3, 4]
</code></pre>
<p>I would like to output each element two times and then repeat the cycle:</p>
<pre><code>1, 1, 2, 2, 3, 3, 4, 4, 1,... | 6 | 2008-12-20T18:23:52Z | 36,515,591 | <pre><code>itertools.chain.from_iterable(itertools.repeat(item, repeat) for item in itertools.cycle(l))
</code></pre>
| 0 | 2016-04-09T10:30:56Z | [
"python",
"iterator"
] |
How do you apply 'or' to all values of a list in Python? | 383,623 | <p>How do you apply 'or' to all values of a list in Python? I'm thinking something like:</p>
<pre><code>or([True, True, False])
</code></pre>
<p>or if it was possible:</p>
<pre><code>reduce(or, [True, True, False])
</code></pre>
| 14 | 2008-12-20T19:05:12Z | 383,637 | <p>You can do this:</p>
<pre><code>reduce(lambda a,b: a or b, [True, True, False])
</code></pre>
| 2 | 2008-12-20T19:11:38Z | [
"python",
"list",
"reduce"
] |
How do you apply 'or' to all values of a list in Python? | 383,623 | <p>How do you apply 'or' to all values of a list in Python? I'm thinking something like:</p>
<pre><code>or([True, True, False])
</code></pre>
<p>or if it was possible:</p>
<pre><code>reduce(or, [True, True, False])
</code></pre>
| 14 | 2008-12-20T19:05:12Z | 383,638 | <p>reduce should do it for you, shouldn't it?</p>
<pre><code>>>> def _or(x, y):
... return x or y
...
>>> reduce(_or, [True, True, False])
True
</code></pre>
| 1 | 2008-12-20T19:12:26Z | [
"python",
"list",
"reduce"
] |
How do you apply 'or' to all values of a list in Python? | 383,623 | <p>How do you apply 'or' to all values of a list in Python? I'm thinking something like:</p>
<pre><code>or([True, True, False])
</code></pre>
<p>or if it was possible:</p>
<pre><code>reduce(or, [True, True, False])
</code></pre>
| 14 | 2008-12-20T19:05:12Z | 383,642 | <p>The built-in function <code>any</code> does what you want:</p>
<pre><code>>>> any([True, True, False])
True
>>> any([False, False, False])
False
>>> any([False, False, True])
True
</code></pre>
<p><code>any</code> has the advantage over <code>reduce</code> of shortcutting the test for la... | 31 | 2008-12-20T19:16:37Z | [
"python",
"list",
"reduce"
] |
How do you apply 'or' to all values of a list in Python? | 383,623 | <p>How do you apply 'or' to all values of a list in Python? I'm thinking something like:</p>
<pre><code>or([True, True, False])
</code></pre>
<p>or if it was possible:</p>
<pre><code>reduce(or, [True, True, False])
</code></pre>
| 14 | 2008-12-20T19:05:12Z | 383,643 | <pre><code>>>> all([True,False,True])
False
>>> any([True,False,True])
True
</code></pre>
<p>Python 2.5 and up (<a href="http://docs.python.org/library/functions.html" rel="nofollow">documentation</a>)</p>
| 3 | 2008-12-20T19:17:12Z | [
"python",
"list",
"reduce"
] |
How do you apply 'or' to all values of a list in Python? | 383,623 | <p>How do you apply 'or' to all values of a list in Python? I'm thinking something like:</p>
<pre><code>or([True, True, False])
</code></pre>
<p>or if it was possible:</p>
<pre><code>reduce(or, [True, True, False])
</code></pre>
| 14 | 2008-12-20T19:05:12Z | 383,668 | <p>No one has mentioned it, but "<code>or</code>" is available as a function in the operator module:</p>
<pre><code>from operator import or_
</code></pre>
<p>Then you can use <code>reduce</code> as above.</p>
<p>Would always advise "<code>any</code>" though in more recent Pythons.</p>
| 7 | 2008-12-20T19:49:58Z | [
"python",
"list",
"reduce"
] |
104, 'Connection reset by peer' socket error, or When does closing a socket result in a RST rather than FIN? | 383,738 | <p>We're developing a Python web service and a client web site in parallel. When we make an HTTP request from the client to the service, one call consistently raises a socket.error in socket.py, in read:</p>
<pre>(104, 'Connection reset by peer')</pre>
<p>When I listen in with wireshark, the "good" and "bad" respons... | 20 | 2008-12-20T21:04:42Z | 383,816 | <p>I've had this problem. See <a href="http://www.itmaybeahack.com/homepage/iblog/architecture/C551260341/E20081031204203/index.html">The Python "Connection Reset By Peer" Problem</a>.</p>
<p>You have (most likely) run afoul of small timing issues based on the Python Global Interpreter Lock.</p>
<p>You can (sometime... | 10 | 2008-12-20T22:18:15Z | [
"python",
"sockets",
"wsgi",
"httplib2",
"werkzeug"
] |
104, 'Connection reset by peer' socket error, or When does closing a socket result in a RST rather than FIN? | 383,738 | <p>We're developing a Python web service and a client web site in parallel. When we make an HTTP request from the client to the service, one call consistently raises a socket.error in socket.py, in read:</p>
<pre>(104, 'Connection reset by peer')</pre>
<p>When I listen in with wireshark, the "good" and "bad" respons... | 20 | 2008-12-20T21:04:42Z | 384,415 | <p>Normally, you'd get an RST if you do a close which doesn't linger (i.e. in which data can be discarded by the stack if it hasn't been sent and ACK'd) and a normal FIN if you allow the close to linger (i.e. the close waits for the data in transit to be ACK'd).</p>
<p>Perhaps all you need to do is set your socket to ... | 1 | 2008-12-21T12:44:18Z | [
"python",
"sockets",
"wsgi",
"httplib2",
"werkzeug"
] |
104, 'Connection reset by peer' socket error, or When does closing a socket result in a RST rather than FIN? | 383,738 | <p>We're developing a Python web service and a client web site in parallel. When we make an HTTP request from the client to the service, one call consistently raises a socket.error in socket.py, in read:</p>
<pre>(104, 'Connection reset by peer')</pre>
<p>When I listen in with wireshark, the "good" and "bad" respons... | 20 | 2008-12-20T21:04:42Z | 481,952 | <p>Don't use wsgiref for production. Use Apache and mod_wsgi, or something else.</p>
<p>We continue to see these connection resets, sometimes frequently, with wsgiref (the backend used by the werkzeug test server, and possibly others like the Django test server). Our solution was to log the error, retry the call in ... | 7 | 2009-01-27T00:37:29Z | [
"python",
"sockets",
"wsgi",
"httplib2",
"werkzeug"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.