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 |
|---|---|---|---|---|---|---|---|---|---|
Python, unit-testing and mocking imports | 178,458 | <p>I am in a project where we are starting refactoring some massive code base. One problem that immediately sprang up is that each file imports a lot of other files. How do I in an elegant way mock this in my unit test without having to alter the actual code so I can start to write unit-tests?</p>
<p>As an example: Th... | 10 | 2008-10-07T13:34:42Z | 178,603 | <p>If you really want to muck around with the python import mechanism, take a look at the <a href="http://pydoc.org/2.4.1/ihooks.html" rel="nofollow"><code>ihooks</code></a> module. It provides tools for changing the behavior of the <code>__import__</code> built-in. But it's not clear from your question why you need ... | 1 | 2008-10-07T14:05:11Z | [
"python",
"unit-testing",
"refactoring",
"python-import"
] |
Python, unit-testing and mocking imports | 178,458 | <p>I am in a project where we are starting refactoring some massive code base. One problem that immediately sprang up is that each file imports a lot of other files. How do I in an elegant way mock this in my unit test without having to alter the actual code so I can start to write unit-tests?</p>
<p>As an example: Th... | 10 | 2008-10-07T13:34:42Z | 178,829 | <p>No difficult manipulation is necessary if you want a quick-and-dirty fix before your unit-tests.</p>
<p>If the unit tests are in the same file as the code you wish to test, simply delete unwanted module from the <code>globals()</code> dictionary.</p>
<p>Here is a rather lengthy example: suppose you have a module <... | 0 | 2008-10-07T14:53:38Z | [
"python",
"unit-testing",
"refactoring",
"python-import"
] |
Python, unit-testing and mocking imports | 178,458 | <p>I am in a project where we are starting refactoring some massive code base. One problem that immediately sprang up is that each file imports a lot of other files. How do I in an elegant way mock this in my unit test without having to alter the actual code so I can start to write unit-tests?</p>
<p>As an example: Th... | 10 | 2008-10-07T13:34:42Z | 179,057 | <p>In your comment <a href="http://stackoverflow.com/questions/178458/python-unit-testing-and-mocking-imports#178829">above</a>, you say you want to convince python that certain modules have already been imported. This still seems like a strange goal, but if that's really what you want to do, in principle you can snea... | 0 | 2008-10-07T15:30:34Z | [
"python",
"unit-testing",
"refactoring",
"python-import"
] |
Python, unit-testing and mocking imports | 178,458 | <p>I am in a project where we are starting refactoring some massive code base. One problem that immediately sprang up is that each file imports a lot of other files. How do I in an elegant way mock this in my unit test without having to alter the actual code so I can start to write unit-tests?</p>
<p>As an example: Th... | 10 | 2008-10-07T13:34:42Z | 179,531 | <p>If you want to import a module while at the same time ensuring that it doesn't import anything, you can replace the <code>__import__</code> builtin function.</p>
<p>For example, use this class:</p>
<pre><code>class ImportWrapper(object):
def __init__(self, real_import):
self.real_import = real_import
... | 7 | 2008-10-07T17:30:27Z | [
"python",
"unit-testing",
"refactoring",
"python-import"
] |
How do I perform an IMAP search in Python (using Gmail and imaplib)? | 179,026 | <p>In Gmail, I have a bunch of labeled messages.</p>
<p>I'd like to use an IMAP client to get those messages, but I'm not sure what the search incantation is.</p>
<pre><code>c = imaplib.IMAP4_SSL('imap.gmail.com')
c.list()
('OK', [..., '(\\HasNoChildren) "/" "GM"', ...])
c.search(???)
</code></pre>
<p>I'm not findin... | 9 | 2008-10-07T15:24:59Z | 179,356 | <p><a href="http://mail.google.com/support/bin/answer.py?ctx=gmail&hl=en&answer=75725">Labels are accessed exactly like IMAP folders</a>, according to Google.</p>
| 5 | 2008-10-07T16:42:21Z | [
"python",
"gmail",
"imap"
] |
How do I perform an IMAP search in Python (using Gmail and imaplib)? | 179,026 | <p>In Gmail, I have a bunch of labeled messages.</p>
<p>I'd like to use an IMAP client to get those messages, but I'm not sure what the search incantation is.</p>
<pre><code>c = imaplib.IMAP4_SSL('imap.gmail.com')
c.list()
('OK', [..., '(\\HasNoChildren) "/" "GM"', ...])
c.search(???)
</code></pre>
<p>I'm not findin... | 9 | 2008-10-07T15:24:59Z | 185,499 | <p>I've been pretty surprised that imaplib doesn't do a lot of the response parsing. And it seems that responses were crafted to be hard to parse.</p>
<p>FWIW, to answer my own question:
c.search(None, 'GM')</p>
<p>(I have no idea what the '(\HasNoChildren) "/"' part is about.)</p>
| 0 | 2008-10-09T00:38:53Z | [
"python",
"gmail",
"imap"
] |
How do I perform an IMAP search in Python (using Gmail and imaplib)? | 179,026 | <p>In Gmail, I have a bunch of labeled messages.</p>
<p>I'd like to use an IMAP client to get those messages, but I'm not sure what the search incantation is.</p>
<pre><code>c = imaplib.IMAP4_SSL('imap.gmail.com')
c.list()
('OK', [..., '(\\HasNoChildren) "/" "GM"', ...])
c.search(???)
</code></pre>
<p>I'm not findin... | 9 | 2008-10-07T15:24:59Z | 421,752 | <p><code>imaplib</code> is intentionally a thin wrapper around the IMAP protocol, I assume to allow for a greater degree of user flexibility and a greater ability to adapt to changes in the IMAP specification. As a result, it doesn't really offer any structure for your search queries and requires you to be familiar wit... | 6 | 2009-01-07T19:37:42Z | [
"python",
"gmail",
"imap"
] |
How do I perform an IMAP search in Python (using Gmail and imaplib)? | 179,026 | <p>In Gmail, I have a bunch of labeled messages.</p>
<p>I'd like to use an IMAP client to get those messages, but I'm not sure what the search incantation is.</p>
<pre><code>c = imaplib.IMAP4_SSL('imap.gmail.com')
c.list()
('OK', [..., '(\\HasNoChildren) "/" "GM"', ...])
c.search(???)
</code></pre>
<p>I'm not findin... | 9 | 2008-10-07T15:24:59Z | 3,101,260 | <pre><code>import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
obj.login('username', 'password')
obj.select('**label name**') # <-- the label in which u want to search message
obj.search(None, 'FROM', '"LDJ"')
</code></pre>
| 6 | 2010-06-23T11:45:49Z | [
"python",
"gmail",
"imap"
] |
How do I perform an IMAP search in Python (using Gmail and imaplib)? | 179,026 | <p>In Gmail, I have a bunch of labeled messages.</p>
<p>I'd like to use an IMAP client to get those messages, but I'm not sure what the search incantation is.</p>
<pre><code>c = imaplib.IMAP4_SSL('imap.gmail.com')
c.list()
('OK', [..., '(\\HasNoChildren) "/" "GM"', ...])
c.search(???)
</code></pre>
<p>I'm not findin... | 9 | 2008-10-07T15:24:59Z | 36,875,058 | <p>The easiest way to use imaplib with Gmail is to use the <code>X-GM-RAW</code> attribute as described in the <a href="https://developers.google.com/gmail/imap_extensions#special-use_extension_of_the_list_command" rel="nofollow">Gmail Imap Extensions page</a>.</p>
<p>The process would be like this:</p>
<p>First conn... | 0 | 2016-04-26T20:25:52Z | [
"python",
"gmail",
"imap"
] |
What is the best way to change text contained in an XML file using Python? | 179,287 | <p>Let's say I have an existing trivial XML file named 'MyData.xml' that contains the following:</p>
<pre><code><?xml version="1.0" encoding="utf-8" ?>
<myElement>foo</myElement>
</code></pre>
<p>I want to change the text value of 'foo' to 'bar' resulting in the following:</p>
<pre><code><?xml v... | 4 | 2008-10-07T16:23:52Z | 179,305 | <p>For quick, non-critical XML manipulations, i really like <a href="http://ponderer.org/p4x" rel="nofollow">P4X</a>. It let's you write like this:</p>
<pre class="lang-python prettyprint-override"><code>import p4x
doc = p4x.P4X (open(file).read)
doc.myElement = 'bar'
</code></pre>
| 3 | 2008-10-07T16:29:04Z | [
"python",
"xml",
"text"
] |
What is the best way to change text contained in an XML file using Python? | 179,287 | <p>Let's say I have an existing trivial XML file named 'MyData.xml' that contains the following:</p>
<pre><code><?xml version="1.0" encoding="utf-8" ?>
<myElement>foo</myElement>
</code></pre>
<p>I want to change the text value of 'foo' to 'bar' resulting in the following:</p>
<pre><code><?xml v... | 4 | 2008-10-07T16:23:52Z | 179,306 | <p>Use Python's <a href="http://docs.python.org/library/xml.dom.minidom.html" rel="nofollow">minidom</a></p>
<p>Basically you will take the following steps:</p>
<ol>
<li>Read XML data into DOM object</li>
<li>Use DOM methods to modify the document</li>
<li>Save new DOM object to new XML document</li>
</ol>
<p>The py... | 4 | 2008-10-07T16:29:06Z | [
"python",
"xml",
"text"
] |
What is the best way to change text contained in an XML file using Python? | 179,287 | <p>Let's say I have an existing trivial XML file named 'MyData.xml' that contains the following:</p>
<pre><code><?xml version="1.0" encoding="utf-8" ?>
<myElement>foo</myElement>
</code></pre>
<p>I want to change the text value of 'foo' to 'bar' resulting in the following:</p>
<pre><code><?xml v... | 4 | 2008-10-07T16:23:52Z | 180,433 | <p>This is what I wrote based on <a href="http://stackoverflow.com/questions/179287/what-is-the-best-way-to-change-text-contained-in-an-xml-file-using-python#179306">@Ryan's answer</a>:</p>
<pre class="lang-python prettyprint-override"><code>from xml.dom.minidom import parse
import os
# create a backup of original fi... | 3 | 2008-10-07T21:07:57Z | [
"python",
"xml",
"text"
] |
What is the best way to change text contained in an XML file using Python? | 179,287 | <p>Let's say I have an existing trivial XML file named 'MyData.xml' that contains the following:</p>
<pre><code><?xml version="1.0" encoding="utf-8" ?>
<myElement>foo</myElement>
</code></pre>
<p>I want to change the text value of 'foo' to 'bar' resulting in the following:</p>
<pre><code><?xml v... | 4 | 2008-10-07T16:23:52Z | 180,564 | <p>You also might want to check out Uche Ogbuji's excellent XML Data Binding Library, Amara:
<a href="http://uche.ogbuji.net/tech/4suite/amara" rel="nofollow">http://uche.ogbuji.net/tech/4suite/amara</a></p>
<p>(Documentation here:
<a href="http://platea.pntic.mec.es/~jmorilla/amara/manual/" rel="nofollow">http://plat... | 1 | 2008-10-07T21:42:17Z | [
"python",
"xml",
"text"
] |
Average difference between dates in Python | 179,716 | <p>I have a series of datetime objects and would like to calculate the average delta between them.</p>
<p>For example, if the input was <code>(2008-10-01 12:15:00, 2008-10-01 12:25:00, 2008-10-01 12:35:00)</code>, then the average delta would be exactly 00:10:00, or 10 minutes.</p>
<p>Any suggestions on how to calcul... | 2 | 2008-10-07T18:18:48Z | 179,738 | <p>As far as algorithms go, that's an easy one. Just find the max and min datetimes, take the difference, and divide by the number of datetimes you looked at.</p>
<p>If you have an array a of datetimes, you can do:</p>
<pre><code>mx = max(a)
mn = min(a)
avg = (mx-mn)/(len(a)-1)
</code></pre>
<p>to get back the aver... | 12 | 2008-10-07T18:26:46Z | [
"python",
"algorithm",
"datetime"
] |
Average difference between dates in Python | 179,716 | <p>I have a series of datetime objects and would like to calculate the average delta between them.</p>
<p>For example, if the input was <code>(2008-10-01 12:15:00, 2008-10-01 12:25:00, 2008-10-01 12:35:00)</code>, then the average delta would be exactly 00:10:00, or 10 minutes.</p>
<p>Any suggestions on how to calcul... | 2 | 2008-10-07T18:18:48Z | 179,760 | <p>Since you seem to be throwing out the 20 minute delta between times 1 and 3 in your example, I'd say you should just sort the list of datetimes, add up the deltas between adjacent times, then divide by n-1.</p>
<p>Do you have any code you can share with us, so we can help you debug it?</p>
| 2 | 2008-10-07T18:32:46Z | [
"python",
"algorithm",
"datetime"
] |
Average difference between dates in Python | 179,716 | <p>I have a series of datetime objects and would like to calculate the average delta between them.</p>
<p>For example, if the input was <code>(2008-10-01 12:15:00, 2008-10-01 12:25:00, 2008-10-01 12:35:00)</code>, then the average delta would be exactly 00:10:00, or 10 minutes.</p>
<p>Any suggestions on how to calcul... | 2 | 2008-10-07T18:18:48Z | 179,761 | <p>You can subtract each successive date from the one prior (resulting in a timedelta object which represents the difference in days, seconds). You can then average the timedelta objects to find your answer.</p>
| 1 | 2008-10-07T18:32:56Z | [
"python",
"algorithm",
"datetime"
] |
Average difference between dates in Python | 179,716 | <p>I have a series of datetime objects and would like to calculate the average delta between them.</p>
<p>For example, if the input was <code>(2008-10-01 12:15:00, 2008-10-01 12:25:00, 2008-10-01 12:35:00)</code>, then the average delta would be exactly 00:10:00, or 10 minutes.</p>
<p>Any suggestions on how to calcul... | 2 | 2008-10-07T18:18:48Z | 181,239 | <p>Say <code>a</code> is your list</p>
<pre><code>sumdeltas = timedelta(seconds=0)
i = 1
while i < len(a):
sumdeltas += a[i-1] - a[i]
i = i + 1
avg_delta = sumdeltas / (len(a) - 1)
</code></pre>
<p>This will indeed average your deltas together.</p>
| 2 | 2008-10-08T03:33:47Z | [
"python",
"algorithm",
"datetime"
] |
Average difference between dates in Python | 179,716 | <p>I have a series of datetime objects and would like to calculate the average delta between them.</p>
<p>For example, if the input was <code>(2008-10-01 12:15:00, 2008-10-01 12:25:00, 2008-10-01 12:35:00)</code>, then the average delta would be exactly 00:10:00, or 10 minutes.</p>
<p>Any suggestions on how to calcul... | 2 | 2008-10-07T18:18:48Z | 1,571,406 | <p>small clarification</p>
<pre><code>from datetime import timedelta
def avg(a):
numdeltas = len(a) - 1
sumdeltas = timedelta(seconds=0)
i = 1
while i < len(a):
delta = abs(a[i] - a[i-1])
try:
sumdeltas += delta
except:
raise
i += 1
avg =... | 0 | 2009-10-15T10:04:17Z | [
"python",
"algorithm",
"datetime"
] |
Python class factory ... or? | 179,985 | <p>We have a database library in C# that we can use like this:</p>
<pre><code>DatabaseConnection conn = DatabaseConnection.FromConnectionString("...");
</code></pre>
<p>This library hides many of the differences between different database engines, like SQL function names, parameter names and specifications, etc.</p>
... | 1 | 2008-10-07T19:34:13Z | 180,009 | <p>This is possible in Python, but is probably not the best way to do it. The class factory pattern is essentially a workaround for languages that don't have first class classes. Since Python does have first class classes, you can store a class in a variable, and use that class directly to create instances. To chang... | 5 | 2008-10-07T19:39:32Z | [
"c#",
"python"
] |
Python class factory ... or? | 179,985 | <p>We have a database library in C# that we can use like this:</p>
<pre><code>DatabaseConnection conn = DatabaseConnection.FromConnectionString("...");
</code></pre>
<p>This library hides many of the differences between different database engines, like SQL function names, parameter names and specifications, etc.</p>
... | 1 | 2008-10-07T19:34:13Z | 180,028 | <p>The first one is absolutely possible, and preferable in my opinion. In python, there's really not a whole lot of magic behind constructors. For all intents and purposes, they're just like any other function. I've used this design pattern a few times to indicate that a class shouldn't be instantiated directly, for... | 1 | 2008-10-07T19:42:54Z | [
"c#",
"python"
] |
Python class factory ... or? | 179,985 | <p>We have a database library in C# that we can use like this:</p>
<pre><code>DatabaseConnection conn = DatabaseConnection.FromConnectionString("...");
</code></pre>
<p>This library hides many of the differences between different database engines, like SQL function names, parameter names and specifications, etc.</p>
... | 1 | 2008-10-07T19:34:13Z | 180,142 | <p>Python doesn't care why type you return.</p>
<pre><code>def DatabaseConnection( str ):
if ( IsOracle( str ) ):
return OracleConnection( str )
else:
return SomeOtherConnection( str )
</code></pre>
| 3 | 2008-10-07T20:04:00Z | [
"c#",
"python"
] |
How to handle a broken pipe (SIGPIPE) in python? | 180,095 | <p>I've written a simple multi-threaded game server in python that creates a new thread for each client connection. I'm finding that every now and then, the server will crash because of a broken-pipe/SIGPIPE error. I'm pretty sure it is happening when the program tries to send a response back to a client that is no l... | 40 | 2008-10-07T19:53:53Z | 180,152 | <p>Read up on the try: statement.</p>
<pre><code>try:
# do something
except socket.error, e:
# A socket error
except IOError, e:
if e.errno == errno.EPIPE:
# EPIPE error
else:
# Other error
</code></pre>
| 30 | 2008-10-07T20:07:27Z | [
"python",
"broken-pipe"
] |
How to handle a broken pipe (SIGPIPE) in python? | 180,095 | <p>I've written a simple multi-threaded game server in python that creates a new thread for each client connection. I'm finding that every now and then, the server will crash because of a broken-pipe/SIGPIPE error. I'm pretty sure it is happening when the program tries to send a response back to a client that is no l... | 40 | 2008-10-07T19:53:53Z | 180,378 | <p><code>SIGPIPE</code> (although I think maybe you mean <code>EPIPE</code>?) occurs on sockets when you shut down a socket and then send data to it. The simple solution is not to shut the socket down before trying to send it data. This can also happen on pipes, but it doesn't sound like that's what you're experienc... | 3 | 2008-10-07T20:55:36Z | [
"python",
"broken-pipe"
] |
How to handle a broken pipe (SIGPIPE) in python? | 180,095 | <p>I've written a simple multi-threaded game server in python that creates a new thread for each client connection. I'm finding that every now and then, the server will crash because of a broken-pipe/SIGPIPE error. I'm pretty sure it is happening when the program tries to send a response back to a client that is no l... | 40 | 2008-10-07T19:53:53Z | 180,421 | <p>My answer is very close to S.Lott's, except I'd be even more particular:</p>
<pre><code>try:
# do something
except IOError, e:
# ooops, check the attributes of e to see precisely what happened.
if e.errno != 23:
# I don't know how to handle this
raise
</code></pre>
<p>where "23" is the ... | -1 | 2008-10-07T21:05:54Z | [
"python",
"broken-pipe"
] |
How to handle a broken pipe (SIGPIPE) in python? | 180,095 | <p>I've written a simple multi-threaded game server in python that creates a new thread for each client connection. I'm finding that every now and then, the server will crash because of a broken-pipe/SIGPIPE error. I'm pretty sure it is happening when the program tries to send a response back to a client that is no l... | 40 | 2008-10-07T19:53:53Z | 180,922 | <p>Assuming that you are using the standard socket module, you should be catching the <code>socket.error: (32, 'Broken pipe')</code> exception (not IOError as others have suggested). This will be raised in the case that you've described, i.e. sending/writing to a socket for which the remote side has disconnected.</p>
... | 44 | 2008-10-08T00:14:18Z | [
"python",
"broken-pipe"
] |
How do I convert a list of ascii values to a string in python? | 180,606 | <p>I've got a list in a Python program that contains a series of numbers, which are themselves ASCII values. How do I convert this into a "regular" string that I can echo to the screen?</p>
| 31 | 2008-10-07T21:51:01Z | 180,615 | <p>You are probably looking for 'chr()':</p>
<pre><code>>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> ''.join(chr(i) for i in L)
'hello, world'
</code></pre>
| 56 | 2008-10-07T21:54:33Z | [
"python",
"string",
"ascii"
] |
How do I convert a list of ascii values to a string in python? | 180,606 | <p>I've got a list in a Python program that contains a series of numbers, which are themselves ASCII values. How do I convert this into a "regular" string that I can echo to the screen?</p>
| 31 | 2008-10-07T21:51:01Z | 180,617 | <pre><code>l = [83, 84, 65, 67, 75]
s = "".join([chr(c) for c in l])
print s
</code></pre>
| 4 | 2008-10-07T21:55:06Z | [
"python",
"string",
"ascii"
] |
How do I convert a list of ascii values to a string in python? | 180,606 | <p>I've got a list in a Python program that contains a series of numbers, which are themselves ASCII values. How do I convert this into a "regular" string that I can echo to the screen?</p>
| 31 | 2008-10-07T21:51:01Z | 181,057 | <p>Same basic solution as others, but I personally prefer to use map instead of the list comprehension:</p>
<pre><code>
>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> ''.join(map(chr,L))
'hello, world'
</code></pre>
| 12 | 2008-10-08T01:22:05Z | [
"python",
"string",
"ascii"
] |
How do I convert a list of ascii values to a string in python? | 180,606 | <p>I've got a list in a Python program that contains a series of numbers, which are themselves ASCII values. How do I convert this into a "regular" string that I can echo to the screen?</p>
| 31 | 2008-10-07T21:51:01Z | 184,708 | <pre><code>import array
def f7(list):
return array.array('B', list).tostring()
</code></pre>
<p>from <a href="http://www.python.org/doc/essays/list2str.html">Python Patterns - An Optimization Anecdote</a></p>
| 8 | 2008-10-08T20:22:31Z | [
"python",
"string",
"ascii"
] |
How do I convert a list of ascii values to a string in python? | 180,606 | <p>I've got a list in a Python program that contains a series of numbers, which are themselves ASCII values. How do I convert this into a "regular" string that I can echo to the screen?</p>
| 31 | 2008-10-07T21:51:01Z | 31,800,289 | <p>def working_ascii():
"""
G r e e t i n g s !
71, 114, 101, 101, 116, 105, 110, 103, 115, 33
"""</p>
<pre><code>hello = [71, 114, 101, 101, 116, 105, 110, 103, 115, 33]
pmsg = ''.join(chr(i) for i in hello)
print(pmsg)
for i in range(33, 256):
print(" ascii: {0}... | 0 | 2015-08-04T03:42:20Z | [
"python",
"string",
"ascii"
] |
How do I convert a list of ascii values to a string in python? | 180,606 | <p>I've got a list in a Python program that contains a series of numbers, which are themselves ASCII values. How do I convert this into a "regular" string that I can echo to the screen?</p>
| 31 | 2008-10-07T21:51:01Z | 34,246,694 | <p>Perhaps not as Pyhtonic a solution, but easier to read for noobs like me:</p>
<pre><code>charlist = [34, 38, 49, 67, 89, 45, 103, 105, 119, 125]
mystring = ""
for char in charlist:
mystring = mystring + chr(char)
print mystring
</code></pre>
| 1 | 2015-12-13T00:35:37Z | [
"python",
"string",
"ascii"
] |
In the Django admin interface, is there a way to duplicate an item? | 180,809 | <p>Just wondering if there is an easy way to add the functionality to duplicate an existing listing in the admin interface?</p>
<p>In data entry we have run into a situation where a lot of items share generic data with another item, and to save time it would be very nice to quickly duplicate an existing listing and on... | 40 | 2008-10-07T23:23:11Z | 180,816 | <p>You can <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_as">save as</a> by just enabling adding this to your ModelAdmin:</p>
<pre><code>save_as = True
</code></pre>
<p>This replaces the "Save and add another" button with a "Save as" button. "Save as" means the... | 71 | 2008-10-07T23:26:15Z | [
"python",
"django",
"django-models",
"django-admin"
] |
What is the difference between Python's re.search and re.match? | 180,986 | <p>What is the difference between the <code>search()</code> and <code>match()</code> functions in the <a href="http://docs.python.org/2/library/re.html">Python <code>re</code> module</a>?</p>
<p>I've read the <a href="http://www.python.org/doc/2.5.2/lib/matching-searching.html">documentation</a> (<a href="http://docs.... | 189 | 2008-10-08T00:51:36Z | 180,993 | <p><code>re.match</code> is anchored at the beginning of the string. That has nothing to do with newlines, so it is not the same as using <code>^</code> in the pattern.</p>
<p>As <a href="http://docs.python.org/2/library/re.html#re.match">re.match documentation</a> says:</p>
<blockquote>
<p>If zero or more characte... | 209 | 2008-10-08T00:53:12Z | [
"python",
"regex",
"search",
"match"
] |
What is the difference between Python's re.search and re.match? | 180,986 | <p>What is the difference between the <code>search()</code> and <code>match()</code> functions in the <a href="http://docs.python.org/2/library/re.html">Python <code>re</code> module</a>?</p>
<p>I've read the <a href="http://www.python.org/doc/2.5.2/lib/matching-searching.html">documentation</a> (<a href="http://docs.... | 189 | 2008-10-08T00:51:36Z | 181,000 | <p>re.match attempts to match a pattern <strong>at the beginning of the string</strong>. re.search attempts to match the pattern <strong>throughout the string</strong> until it finds a match.</p>
| 9 | 2008-10-08T00:54:57Z | [
"python",
"regex",
"search",
"match"
] |
What is the difference between Python's re.search and re.match? | 180,986 | <p>What is the difference between the <code>search()</code> and <code>match()</code> functions in the <a href="http://docs.python.org/2/library/re.html">Python <code>re</code> module</a>?</p>
<p>I've read the <a href="http://www.python.org/doc/2.5.2/lib/matching-searching.html">documentation</a> (<a href="http://docs.... | 189 | 2008-10-08T00:51:36Z | 181,001 | <p>Have you had a look at the <a href="http://docs.python.org/library/re.html#matching-vs-searching" rel="nofollow">documentation</a>?</p>
<blockquote>
<p>Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks f... | 4 | 2008-10-08T00:55:28Z | [
"python",
"regex",
"search",
"match"
] |
What is the difference between Python's re.search and re.match? | 180,986 | <p>What is the difference between the <code>search()</code> and <code>match()</code> functions in the <a href="http://docs.python.org/2/library/re.html">Python <code>re</code> module</a>?</p>
<p>I've read the <a href="http://www.python.org/doc/2.5.2/lib/matching-searching.html">documentation</a> (<a href="http://docs.... | 189 | 2008-10-08T00:51:36Z | 181,028 | <p><code>re.search</code> <strong>search</strong>es for the pattern <strong>throughout the string</strong>, whereas <code>re.match</code> does <em>not search</em> the pattern; if it does not, it has no other choice than to <strong>match</strong> it at start of the string.</p>
| 37 | 2008-10-08T01:07:26Z | [
"python",
"regex",
"search",
"match"
] |
What is the difference between Python's re.search and re.match? | 180,986 | <p>What is the difference between the <code>search()</code> and <code>match()</code> functions in the <a href="http://docs.python.org/2/library/re.html">Python <code>re</code> module</a>?</p>
<p>I've read the <a href="http://www.python.org/doc/2.5.2/lib/matching-searching.html">documentation</a> (<a href="http://docs.... | 189 | 2008-10-08T00:51:36Z | 8,687,988 | <p><code>search</code> ⇒ find something anywhere in the string and return a match object.</p>
<p><code>match</code> ⇒ find something at the <em>beginning</em> of the string and return a match object.</p>
| 32 | 2011-12-31T12:05:43Z | [
"python",
"regex",
"search",
"match"
] |
What is the difference between Python's re.search and re.match? | 180,986 | <p>What is the difference between the <code>search()</code> and <code>match()</code> functions in the <a href="http://docs.python.org/2/library/re.html">Python <code>re</code> module</a>?</p>
<p>I've read the <a href="http://www.python.org/doc/2.5.2/lib/matching-searching.html">documentation</a> (<a href="http://docs.... | 189 | 2008-10-08T00:51:36Z | 31,715,754 | <p>you can refer the below example to understand the working of re.match and re.search</p>
<pre><code>a = "123abc"
t = re.match("[a-z]+",a)
t = re.search("[a-z]+",a)
</code></pre>
<p>re.match will return none, but re.search will return abc.</p>
| 6 | 2015-07-30T05:27:27Z | [
"python",
"regex",
"search",
"match"
] |
What is the difference between Python's re.search and re.match? | 180,986 | <p>What is the difference between the <code>search()</code> and <code>match()</code> functions in the <a href="http://docs.python.org/2/library/re.html">Python <code>re</code> module</a>?</p>
<p>I've read the <a href="http://www.python.org/doc/2.5.2/lib/matching-searching.html">documentation</a> (<a href="http://docs.... | 189 | 2008-10-08T00:51:36Z | 37,363,575 | <p>The difference is, <strong><code>re.match()</code> misleads anyone accustomed to <em>Perl</em>, <em>grep</em>, or <em>sed</em> regular expression matching, and <code>re.search()</code> does not.</strong> :-)</p>
<p>More soberly, <a href="http://www.johndcook.com/blog/python_regex/" rel="nofollow">As John D. Cook re... | 4 | 2016-05-21T13:28:47Z | [
"python",
"regex",
"search",
"match"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 181,553 | <p>This doesn't improve so much but...</p>
<pre><code>allCondsAreOK = (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4')
if allCondsAreOK:
do_something
</code></pre>
| 11 | 2008-10-08T06:31:54Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 181,557 | <p>You don't need to use 4 spaces on your second conditional line. Maybe use:</p>
<pre><code>if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Also, don't forget the whitespace is more flexible than you might think:</p>
<pre><code>if (
... | 296 | 2008-10-08T06:34:25Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 181,641 | <p>I suggest moving the <code>and</code> keyword to the second line and indenting all lines containing conditions with two spaces instead of four:</p>
<pre><code>if (cond1 == 'val1' and cond2 == 'val2'
and cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>This is exactly how I solve this probl... | 13 | 2008-10-08T07:19:09Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 181,848 | <p>I prefer this style when I have a terribly large if-condition:</p>
<pre><code>if (
expr1
and (expr2 or expr3)
and hasattr(thingy1, '__eq__')
or status=="HappyTimes"
):
do_stuff()
else:
do_other_stuff()
</code></pre>
| 11 | 2008-10-08T08:39:58Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 182,050 | <p>I've resorted to the following in the degenerate case where it's simply AND's or OR's.</p>
<pre><code>if all( [cond1 == 'val1', cond2 == 'val2', cond3 == 'val3', cond4 == 'val4'] ):
if any( [cond1 == 'val1', cond2 == 'val2', cond3 == 'val3', cond4 == 'val4'] ):
</code></pre>
<p>It shaves a few characters and make... | 61 | 2008-10-08T10:26:48Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 182,067 | <p>"all" and "any" are nice for the many conditions of same type case. BUT they always evaluates all conditions. As shown in this example:</p>
<pre><code>def c1():
print " Executed c1"
return False
def c2():
print " Executed c2"
return False
print "simple and (aborts early!)"
if c1() and c2():
pa... | 2 | 2008-10-08T10:38:37Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 183,206 | <p><em>Someone</em> has to champion use of vertical whitespace here! :)</p>
<pre><code>if ( cond1 == val1
and cond2 == val2
and cond3 == val3
):
do_stuff()
</code></pre>
<p>This makes each condition clearly visible. It also allows cleaner expression of more complex conditions:</p>
<pre><code>if ... | 27 | 2008-10-08T14:55:02Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 183,889 | <p>Just a few other random ideas for completeness's sake. If they work for you, use them. Otherwise, you're probably better off trying something else.</p>
<p>You could also do this with a dictionary:</p>
<pre><code>>>> x = {'cond1' : 'val1', 'cond2' : 'val2'}
>>> y = {'cond1' : 'val1', 'cond2' : '... | 1 | 2008-10-08T17:26:00Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 185,748 | <p>What if we only insert an additional blank line between the condition and the body and do the rest in the canonical way?</p>
<pre><code>if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>p.s. I always use tabs, not spaces; I cannot fine-tune...<... | 2 | 2008-10-09T02:45:40Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 3,004,525 | <p>Pack your conditions into a list, then do smth. like:</p>
<pre><code>if False not in Conditions:
do_something
</code></pre>
| 0 | 2010-06-09T09:23:43Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 4,689,224 | <p>(I've lightly modified the identifiers as fixed-width names aren't representative of real code â at least not real code that I encounter â and will belie an example's readability.)</p>
<pre><code>if (cond1 == "val1" and cond22 == "val2"
and cond333 == "val3" and cond4444 == "val4"):
do_something
</code></pr... | 1 | 2011-01-14T08:33:47Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 4,690,241 | <p>Here's my very personal take: long conditions are (in my view) a code smell that suggests refactoring into a boolean-returning function/method. For example:</p>
<pre><code>def is_action__required(...):
return (cond1 == 'val1' and cond2 == 'val2'
and cond3 == 'val3' and cond4 == 'val4')
</code></pre>... | 12 | 2011-01-14T10:50:32Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 4,692,294 | <p>I'm surprised not to see my preferred solution,</p>
<pre><code>if (cond1 == 'val1' and cond2 == 'val2'
and cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Since <code>and</code> is a keyword, it gets highlighted by my editor, and looks sufficiently different from the do_something below ... | 4 | 2011-01-14T14:50:50Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 4,740,630 | <p>Adding to what @krawyoti said... Long conditions smell because they are difficult to read and difficult to understand. Using a function or a variable makes the code clearer. In Python, I prefer to use vertical space, enclose parenthesis, and place the logical operators at the beginning of each line so the expression... | 2 | 2011-01-19T20:53:39Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 7,511,872 | <p>I find that when I have long conditions, I often have a short code body. In that case, I just double-indent the body, thus:</p>
<pre><code>if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
| 0 | 2011-09-22T08:31:27Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 9,682,878 | <pre><code> if cond1 == 'val1' and \
cond2 == 'val2' and \
cond3 == 'val3' and \
cond4 == 'val4':
do_something
</code></pre>
<p>or if this is clearer:</p>
<pre><code> if cond1 == 'val1'\
and cond2 == 'val2'\
and cond3 == 'val3'\
and cond4 == 'val4':
do_something
</code></pr... | 0 | 2012-03-13T11:13:19Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 15,264,532 | <p>Personally, I like to add meaning to long if-statements. I would have to search through code to find an appropriate example, but here's the first example that comes to mind: let's say I happen to run into some quirky logic where I want to display a certain page depending on many variables.</p>
<p>English: "If the l... | 2 | 2013-03-07T06:24:12Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 25,085,957 | <p>What I usually do is:</p>
<pre><code>if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'
):
do_something
</code></pre>
<p>this way the closing brace and colon visually mark the end of our condition.</p>
| 1 | 2014-08-01T17:53:44Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 26,414,728 | <p>Here's another approach:</p>
<pre><code>cond_list = ['cond1 == "val1"','cond2=="val2"','cond3=="val3"','cond4=="val4"']
if all([eval(i) for i in cond_list]):
do something
</code></pre>
<p>This also makes it easy to add another condition easily without changing the if statement by simply appending another conditio... | 0 | 2014-10-16T22:08:03Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 27,008,512 | <p>Here's what I do, remember that "all" and "any" accepts an iterable, so I just put a long condition in a list and let "all" do the work.</p>
<pre><code>condition = [cond1 == 'val1', cond2 == 'val2', cond3 == 'val3', cond4 == 'val4']
if all(condition):
do_something
</code></pre>
| 4 | 2014-11-19T03:34:33Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 27,100,017 | <p>Plain and simple, also passes pep8 checks:</p>
<pre><code>if (
cond1 and
cond2
):
print("Hello World!")
</code></pre>
<hr>
<p>In recent times I have been preferring the <code>all</code> and <code>any</code> functions, since I rarely mix And and Or comparisons this works well, and has the additional ad... | 1 | 2014-11-24T07:44:29Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 27,279,883 | <p>I've been struggling to find a decent way to do this as well, so I just came up with an idea (not a silver bullet, since this is mainly a matter of taste).</p>
<pre><code>if bool(condition1 and
condition2 and
...
conditionN):
foo()
bar()
</code></pre>
<p>I find a few merits in this ... | 1 | 2014-12-03T19:19:18Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 28,867,664 | <p>I usually use: </p>
<pre class="lang-py prettyprint-override"><code>if ((cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4')):
do_something()
</code></pre>
| 0 | 2015-03-05T00:12:32Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 31,231,248 | <p>It seems worth quoting <a href="https://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP 0008</a> (Python's official style guide), since it comments upon this issue at modest length:</p>
<blockquote>
<p>When the conditional part of an <code>if</code> -statement is long enough to require that it be written ac... | 3 | 2015-07-05T14:15:27Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 34,117,413 | <p>if our if & an else condition has to execute multiple statement inside of it than we can write like below.
Every when we have if else example with one statement inside of it .</p>
<p>Thanks it work for me.</p>
<pre><code>#!/usr/bin/python
import sys
numberOfArgument =len(sys.argv)
weblogic_username =''
weblogi... | 0 | 2015-12-06T12:27:55Z | [
"python",
"coding-style",
"if-statement"
] |
Python style: multiple-line conditions in IFs | 181,530 | <p>Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is:</p>
<pre><code> if (cond1 == 'val1' and cond2 == 'val2' and
cond3 == 'val3' and cond4 == 'val4'):
do_something
</code></pre>
<p>Isn't very very appealing visually, because the action blends with the conditio... | 270 | 2008-10-08T06:19:07Z | 38,775,892 | <p>You could split it into two lines</p>
<pre><code>total = cond1 == 'val' and cond2 == 'val2' and cond3 == 'val3' and cond4 == val4
if total:
do_something()
</code></pre>
<p>Or even add on one condition at a time. That way, at least it separates the clutter from the <code>if</code>.</p>
| 0 | 2016-08-04T19:30:45Z | [
"python",
"coding-style",
"if-statement"
] |
What is the problem with reduce()? | 181,543 | <p>There seems to be a lot of heated discussion on the net about the changes to the reduce() function in python 3.0 and how it should be removed. I am having a little difficulty understanding why this is the case; I find it quite reasonable to use it in a variety of cases. If the contempt was simply subjective, I canno... | 47 | 2008-10-08T06:27:12Z | 181,593 | <p>People worry it encourages an obfuscated style of programming, doing something that can be achieved with clearer methods.</p>
<p>I'm not against reduce myself, I also find it a useful tool sometimes.</p>
| 7 | 2008-10-08T06:53:54Z | [
"python",
"python-3.x"
] |
What is the problem with reduce()? | 181,543 | <p>There seems to be a lot of heated discussion on the net about the changes to the reduce() function in python 3.0 and how it should be removed. I am having a little difficulty understanding why this is the case; I find it quite reasonable to use it in a variety of cases. If the contempt was simply subjective, I canno... | 47 | 2008-10-08T06:27:12Z | 181,646 | <p><code>reduce()</code> is not being removed -- it's simply being moved into the <code>functools</code> module. Guido's reasoning is that except for trivial cases like summation, code written using <code>reduce()</code> is usually clearer when written as an accumulation loop.</p>
| 24 | 2008-10-08T07:20:43Z | [
"python",
"python-3.x"
] |
What is the problem with reduce()? | 181,543 | <p>There seems to be a lot of heated discussion on the net about the changes to the reduce() function in python 3.0 and how it should be removed. I am having a little difficulty understanding why this is the case; I find it quite reasonable to use it in a variety of cases. If the contempt was simply subjective, I canno... | 47 | 2008-10-08T06:27:12Z | 181,706 | <p>As Guido says in his <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=98196">The fate of reduce() in Python 3000</a> post:</p>
<blockquote>
<p>So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call wi... | 56 | 2008-10-08T07:42:12Z | [
"python",
"python-3.x"
] |
py2exe including MSVC DLLs in the .exe | 181,556 | <p>When using py2exe to distribute Python applications with wxPython, some MSVC DLLs are usually needed to make the .exe work on freshly installed machines. In particular, the two most common DLLs are msvcp71.dll and msvcr71.dll</p>
<p>The former can be included in the .exe using <a href="http://www.py2exe.org/index.c... | 4 | 2008-10-08T06:34:21Z | 181,583 | <p>Wouldn't it fail to launch, then? You want <code>msvcr71.dll</code> in the same directory as the exe, so that the library loader will be able to find and link it into the application's memory map. </p>
<p>It's needed for basic operation, so you can't just let <code>py2exe</code> unpack it with the rest of the DLLs.... | 6 | 2008-10-08T06:46:57Z | [
"python",
"dll",
"py2exe"
] |
py2exe including MSVC DLLs in the .exe | 181,556 | <p>When using py2exe to distribute Python applications with wxPython, some MSVC DLLs are usually needed to make the .exe work on freshly installed machines. In particular, the two most common DLLs are msvcp71.dll and msvcr71.dll</p>
<p>The former can be included in the .exe using <a href="http://www.py2exe.org/index.c... | 4 | 2008-10-08T06:34:21Z | 182,059 | <p>py2exe can't do this. You can wrap py2exe (there is <a href="http://py2exe.org/index.cgi/SingleFileExecutable" rel="nofollow">an example on the wiki</a> showing how to do that with NSIS); you could build your own wrapper if using NSIS or InnoSetup wasn't an option.</p>
<p>Alternatively, if you're positive that you... | 1 | 2008-10-08T10:31:51Z | [
"python",
"dll",
"py2exe"
] |
py2exe including MSVC DLLs in the .exe | 181,556 | <p>When using py2exe to distribute Python applications with wxPython, some MSVC DLLs are usually needed to make the .exe work on freshly installed machines. In particular, the two most common DLLs are msvcp71.dll and msvcr71.dll</p>
<p>The former can be included in the .exe using <a href="http://www.py2exe.org/index.c... | 4 | 2008-10-08T06:34:21Z | 22,450,019 | <p>Yes, py2exe can do this. <a href="http://www.py2exe.org/index.cgi/OverridingCriteraForIncludingDlls" rel="nofollow">View this link.</a>And if you are using python2.7, replace "msvcr71" to "msvcp90".</p>
| 1 | 2014-03-17T08:51:08Z | [
"python",
"dll",
"py2exe"
] |
wxPython: displaying multiple widgets in same frame | 181,573 | <p>I would like to be able to display <code>Notebook</code> and a <code>TxtCtrl</code> wx widgets in a single frame. Below is an example adapted from the wxpython wiki; is it possible to change their layout (maybe with something like <code>wx.SplitterWindow</code>) to display the text box below the <code>Notebook</cod... | 2 | 2008-10-08T06:41:18Z | 181,591 | <p>You can use a splitter, yes.</p>
<p>Also, it makes sense to create a Panel, place your widgets in it (with sizers), and add this panel to the Frame.</p>
| 1 | 2008-10-08T06:52:03Z | [
"python",
"user-interface",
"layout",
"wxpython",
"wxwidgets"
] |
wxPython: displaying multiple widgets in same frame | 181,573 | <p>I would like to be able to display <code>Notebook</code> and a <code>TxtCtrl</code> wx widgets in a single frame. Below is an example adapted from the wxpython wiki; is it possible to change their layout (maybe with something like <code>wx.SplitterWindow</code>) to display the text box below the <code>Notebook</cod... | 2 | 2008-10-08T06:41:18Z | 181,626 | <p>Making two widgets appear on the same frame is easy, actually. You should use sizers to accomplish this.</p>
<p>In your example, you can change your <code>Notebook</code> class implementation to something like this:</p>
<pre><code>class Notebook(wx.Frame):
def __init__(self, parent, id, title):
wx.Fram... | 8 | 2008-10-08T07:11:08Z | [
"python",
"user-interface",
"layout",
"wxpython",
"wxwidgets"
] |
Problem opening berkeley db in python | 181,648 | <p>I have problems opening a berkeley db in python using bdtables. As bdtables is used by the library I am using to access the database, I need it to work.</p>
<p>The problem seems to be that the db environment I am trying to open (I got a copy of the database to open), is version 4.4 while libdb is version 4.6. I get... | 3 | 2008-10-08T07:21:19Z | 185,678 | <p>I think answers should go in the "answer" section rather than as an addendum to the question since that marks the question as having an answer on the various question-list pages. I'll do that for you but, if you also get around to doing it, leave a comment on my answer so I can delete it.</p>
<p>Quoting "answer in... | 3 | 2008-10-09T02:16:06Z | [
"python",
"database",
"berkeley-db"
] |
Problem opening berkeley db in python | 181,648 | <p>I have problems opening a berkeley db in python using bdtables. As bdtables is used by the library I am using to access the database, I need it to work.</p>
<p>The problem seems to be that the db environment I am trying to open (I got a copy of the database to open), is version 4.4 while libdb is version 4.6. I get... | 3 | 2008-10-08T07:21:19Z | 194,022 | <p>Damn, verifying everything in this question I eventually solved the problem. The 'No such file or directory' are caused by some __db.XXX files missing. Using bsddb.dbtables.bsdTableDB([dbname],[folder], create=1) after db4.4_recover, these files got created and everything is now working.</p>
<p>Still, it was a bit ... | 0 | 2008-10-11T12:21:41Z | [
"python",
"database",
"berkeley-db"
] |
When to use the Python debugger | 181,724 | <p>Since Python is a dynamic, interpreted language you don't have to compile your code before running it. Hence, it's very easy to simply write your code, run it, see what problems occur, and fix them. Using hotkeys or macros can make this incredibly quick.</p>
<p>So, because it's so easy to immediately see the output... | 5 | 2008-10-08T07:49:01Z | 181,761 | <p>Any time you want to inspect the contents of variables that may have caused the error. The only way you can do that is to stop execution and take a look at the stack.</p>
<p>pydev in Eclipse is a pretty good IDE if you are looking for one.</p>
| 2 | 2008-10-08T08:02:14Z | [
"python",
"debugging"
] |
When to use the Python debugger | 181,724 | <p>Since Python is a dynamic, interpreted language you don't have to compile your code before running it. Hence, it's very easy to simply write your code, run it, see what problems occur, and fix them. Using hotkeys or macros can make this incredibly quick.</p>
<p>So, because it's so easy to immediately see the output... | 5 | 2008-10-08T07:49:01Z | 181,767 | <p>I use pdb for basic python debugging. Some of the situations I use it are:</p>
<ul>
<li>When you have a loop iterating over 100,000 entries and want to break at a specific point, it becomes really helpful.(conditional breaks)</li>
<li>Trace the control flow of someone else's code.</li>
<li>Its always better to use ... | 7 | 2008-10-08T08:04:08Z | [
"python",
"debugging"
] |
When to use the Python debugger | 181,724 | <p>Since Python is a dynamic, interpreted language you don't have to compile your code before running it. Hence, it's very easy to simply write your code, run it, see what problems occur, and fix them. Using hotkeys or macros can make this incredibly quick.</p>
<p>So, because it's so easy to immediately see the output... | 5 | 2008-10-08T07:49:01Z | 181,768 | <p>Usually when the error is buried in some function, but I don't know exactly what or where. Either I insert dozens of <code>log.debug()</code> calls and then have to take them back out, or just put in:</p>
<pre><code>import pdb
pdb.set_trace ()
</code></pre>
<p>and then run the program. The debugger will launch whe... | 6 | 2008-10-08T08:04:12Z | [
"python",
"debugging"
] |
When to use the Python debugger | 181,724 | <p>Since Python is a dynamic, interpreted language you don't have to compile your code before running it. Hence, it's very easy to simply write your code, run it, see what problems occur, and fix them. Using hotkeys or macros can make this incredibly quick.</p>
<p>So, because it's so easy to immediately see the output... | 5 | 2008-10-08T07:49:01Z | 182,010 | <p>In 30 years of programming I've used a debugger exactly 4 times. All four times were to read the <code>core</code> file produced from a C program crashing to locate the traceback information that's buried in there.</p>
<p>I don't think debuggers help much, even in compiled languages. Many people like debuggers, t... | 8 | 2008-10-08T10:09:56Z | [
"python",
"debugging"
] |
When to use the Python debugger | 181,724 | <p>Since Python is a dynamic, interpreted language you don't have to compile your code before running it. Hence, it's very easy to simply write your code, run it, see what problems occur, and fix them. Using hotkeys or macros can make this incredibly quick.</p>
<p>So, because it's so easy to immediately see the output... | 5 | 2008-10-08T07:49:01Z | 183,563 | <p>I find it very useful to drop into a debugger in a failing test case.</p>
<p>I add <code>import pdb; pdb.set_trace()</code> just before the failure point of the test. The test runs, building up a potentially quite large context (e.g. importing a database fixture or constructing an HTTP request). When the test reach... | 1 | 2008-10-08T16:07:57Z | [
"python",
"debugging"
] |
When to use the Python debugger | 181,724 | <p>Since Python is a dynamic, interpreted language you don't have to compile your code before running it. Hence, it's very easy to simply write your code, run it, see what problems occur, and fix them. Using hotkeys or macros can make this incredibly quick.</p>
<p>So, because it's so easy to immediately see the output... | 5 | 2008-10-08T07:49:01Z | 14,683,302 | <p>You might want to take a look at this other SO post:</p>
<p><a href="http://stackoverflow.com/questions/426569/why-is-debugging-better-in-an-ide">Why is debugging better in an IDE?</a></p>
<p>It's directly relevant to what you're asking about.</p>
| 0 | 2013-02-04T08:57:00Z | [
"python",
"debugging"
] |
How do I turn an RSS feed back into RSS? | 181,818 | <p>According to the <a href="http://feedparser.org/docs/introduction.html" rel="nofollow">feedparser documentation</a>, I can turn an RSS feed into a parsed object like this:</p>
<pre><code>import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
</code></pre>
<p>but I can't find anyth... | 3 | 2008-10-08T08:26:14Z | 181,832 | <pre><code>from xml.dom import minidom
doc= minidom.parse('./your/file.xml')
print doc.toxml()
</code></pre>
<p>The only problem is that it do not download feeds from the internet.</p>
| 0 | 2008-10-08T08:32:52Z | [
"python",
"rss"
] |
How do I turn an RSS feed back into RSS? | 181,818 | <p>According to the <a href="http://feedparser.org/docs/introduction.html" rel="nofollow">feedparser documentation</a>, I can turn an RSS feed into a parsed object like this:</p>
<pre><code>import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
</code></pre>
<p>but I can't find anyth... | 3 | 2008-10-08T08:26:14Z | 181,903 | <p>As a method of making a feed, how about <a href="http://www.dalkescientific.com/Python/PyRSS2Gen.html" rel="nofollow">PyRSS2Gen</a>? :)</p>
<p>I've not played with FeedParser, but have you tried just doing str(yourFeedParserObject)? I've often been suprised by various modules that have <strong>str</strong> methods ... | 0 | 2008-10-08T09:09:29Z | [
"python",
"rss"
] |
How do I turn an RSS feed back into RSS? | 181,818 | <p>According to the <a href="http://feedparser.org/docs/introduction.html" rel="nofollow">feedparser documentation</a>, I can turn an RSS feed into a parsed object like this:</p>
<pre><code>import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
</code></pre>
<p>but I can't find anyth... | 3 | 2008-10-08T08:26:14Z | 181,931 | <p>If you're looking to read in an XML feed, modify it and then output it again, there's <a href="http://wiki.python.org/moin/RssLibraries" rel="nofollow">a page on the main python wiki indicating that the RSS.py library might support what you're after</a> (it reads most RSS and is able to output RSS 1.0). I've not loo... | 1 | 2008-10-08T09:25:41Z | [
"python",
"rss"
] |
How do I turn an RSS feed back into RSS? | 181,818 | <p>According to the <a href="http://feedparser.org/docs/introduction.html" rel="nofollow">feedparser documentation</a>, I can turn an RSS feed into a parsed object like this:</p>
<pre><code>import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
</code></pre>
<p>but I can't find anyth... | 3 | 2008-10-08T08:26:14Z | 191,899 | <p>Appended is a not hugely-elegant, but working solution - it uses feedparser to parse the feed, you can then modify the entries, and it passes the data to PyRSS2Gen. It preserves <em>most</em> of the feed info (the important bits anyway, there are somethings that will need extra conversion, the parsed_feed['feed']['i... | 4 | 2008-10-10T15:33:14Z | [
"python",
"rss"
] |
Code to verify updates from the Google Safe Browsing API | 181,994 | <p>In order to verify the data coming from the <a href="http://code.google.com/apis/safebrowsing/developers_guide.html" rel="nofollow">Google Safe Browsing API</a>, you can calculate a Message Authentication Code (MAC) for each update. The instructions to do this (from Google) are:</p>
<blockquote>
<p>The MAC is co... | 1 | 2008-10-08T09:59:55Z | 182,099 | <pre><code>c="8eirwN1kTwCzgWA2HxTaRQ==".decode('base64')
</code></pre>
| 1 | 2008-10-08T10:47:51Z | [
"python",
"api",
"verification",
"safe-browsing"
] |
Code to verify updates from the Google Safe Browsing API | 181,994 | <p>In order to verify the data coming from the <a href="http://code.google.com/apis/safebrowsing/developers_guide.html" rel="nofollow">Google Safe Browsing API</a>, you can calculate a Message Authentication Code (MAC) for each update. The instructions to do this (from Google) are:</p>
<blockquote>
<p>The MAC is co... | 1 | 2008-10-08T09:59:55Z | 184,617 | <p>Anders' answer gives the necessary information, but isn't that clear: the client key needs to be decoded before it is combined. (The example above is also missing a newline at the end of the final table data).</p>
<p>So the working code is:</p>
<pre><code>>>> s = "+8070465bdf3b9c6ad6a89c32e8162ef1\t\n+86... | 2 | 2008-10-08T20:07:42Z | [
"python",
"api",
"verification",
"safe-browsing"
] |
Setup Python enviroment on windows | 182,053 | <p>How do I setup a Python enviroment on windows computer so I can start writing and running Python scripts, is there an install bundle? Also which database should i use?</p>
<p>Thanks</p>
<p><hr /></p>
<p>Sorry I should of mentioned that I am using this for web based applications. Does it require apache? or does i... | 3 | 2008-10-08T10:29:28Z | 182,057 | <p>Download the Python 2.6 Windows installer from <a href="http://python.org/" rel="nofollow">python.org</a> (<a href="http://python.org/ftp/python/2.6/python-2.6.msi" rel="nofollow">direct link</a>). If you're just learning, use the included SQLite library so you don't have to fiddle with database servers.</p>
<p><hr... | 7 | 2008-10-08T10:31:26Z | [
"python",
"windows",
"database",
"installation",
"development-environment"
] |
Setup Python enviroment on windows | 182,053 | <p>How do I setup a Python enviroment on windows computer so I can start writing and running Python scripts, is there an install bundle? Also which database should i use?</p>
<p>Thanks</p>
<p><hr /></p>
<p>Sorry I should of mentioned that I am using this for web based applications. Does it require apache? or does i... | 3 | 2008-10-08T10:29:28Z | 182,120 | <p>Don't forget to install <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">pywin32</a> after installing the official (command line) installer. This will define additional <em>start menu</em> items and the highly useful <em>PythonWin IDE</em>.</p>
<p>An installer for both is available at <a href="http... | 1 | 2008-10-08T10:54:18Z | [
"python",
"windows",
"database",
"installation",
"development-environment"
] |
Setup Python enviroment on windows | 182,053 | <p>How do I setup a Python enviroment on windows computer so I can start writing and running Python scripts, is there an install bundle? Also which database should i use?</p>
<p>Thanks</p>
<p><hr /></p>
<p>Sorry I should of mentioned that I am using this for web based applications. Does it require apache? or does i... | 3 | 2008-10-08T10:29:28Z | 182,193 | <p>Django tutorial <a href="http://docs.djangoproject.com/en/dev/topics/install/" rel="nofollow">How to install Django</a> provides a good example how a web-development Python environment may look.</p>
| 0 | 2008-10-08T11:12:24Z | [
"python",
"windows",
"database",
"installation",
"development-environment"
] |
Setup Python enviroment on windows | 182,053 | <p>How do I setup a Python enviroment on windows computer so I can start writing and running Python scripts, is there an install bundle? Also which database should i use?</p>
<p>Thanks</p>
<p><hr /></p>
<p>Sorry I should of mentioned that I am using this for web based applications. Does it require apache? or does i... | 3 | 2008-10-08T10:29:28Z | 182,195 | <p><strong>Bundle</strong>: go with Activestate's Python, which bundles many useful win32-related libraries. It has no version 2.6 yet, but most code you'll find online refers to 2.5 and lower anyway.</p>
<p><strong>Database</strong>: any of the popular open-source DBs are simple to configure. But as John already sugg... | 4 | 2008-10-08T11:12:43Z | [
"python",
"windows",
"database",
"installation",
"development-environment"
] |
Setup Python enviroment on windows | 182,053 | <p>How do I setup a Python enviroment on windows computer so I can start writing and running Python scripts, is there an install bundle? Also which database should i use?</p>
<p>Thanks</p>
<p><hr /></p>
<p>Sorry I should of mentioned that I am using this for web based applications. Does it require apache? or does i... | 3 | 2008-10-08T10:29:28Z | 182,199 | <p>I strongly recommend <a href="http://www.activestate.com/Products/activepython/index.mhtml" rel="nofollow">ActiveState Python</a> for python on windows development. It comes with Win32Com and various other goodies, has a mature and clean installer, a chm version of the docs and works really well. I use this all of... | 2 | 2008-10-08T11:13:07Z | [
"python",
"windows",
"database",
"installation",
"development-environment"
] |
Setup Python enviroment on windows | 182,053 | <p>How do I setup a Python enviroment on windows computer so I can start writing and running Python scripts, is there an install bundle? Also which database should i use?</p>
<p>Thanks</p>
<p><hr /></p>
<p>Sorry I should of mentioned that I am using this for web based applications. Does it require apache? or does i... | 3 | 2008-10-08T10:29:28Z | 182,893 | <p>Might I suggest taking a look at <a href="http://karrigell.sourceforge.net/" rel="nofollow">Karrigell</a>? It's really a nice Python web framework if you don't require everything Django and Turbogears offers. It might be easier for you to work with web frameworks until you get comfortable with them.</p>
<p>For deve... | 1 | 2008-10-08T13:56:21Z | [
"python",
"windows",
"database",
"installation",
"development-environment"
] |
How do I watch a file for changes using Python? | 182,197 | <p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p>
<p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin... | 197 | 2008-10-08T11:12:55Z | 182,234 | <p>I don't know any Windows specific function. You could try getting the MD5 hash of the file every second/minute/hour (depends on how fast you need it) and compare it to the last hash. When it differs you know the file has been changed and you read out the newest lines.</p>
| -2 | 2008-10-08T11:25:57Z | [
"python",
"file",
"pywin32",
"watch"
] |
How do I watch a file for changes using Python? | 182,197 | <p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p>
<p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin... | 197 | 2008-10-08T11:12:55Z | 182,235 | <p>I'd try something like this.</p>
<pre><code> try:
f = open(filePath)
except IOError:
print "No such file: %s" % filePath
raw_input("Press Enter to close window")
try:
lines = f.readlines()
while True:
line = f.readline()
... | -6 | 2008-10-08T11:26:44Z | [
"python",
"file",
"pywin32",
"watch"
] |
How do I watch a file for changes using Python? | 182,197 | <p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p>
<p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin... | 197 | 2008-10-08T11:12:55Z | 182,242 | <p>Check <a href="http://stackoverflow.com/questions/62832/reading-data-from-a-log-file-as-a-separate-application-is-writing-to-it#63446">my answer</a> to a <a href="http://stackoverflow.com/questions/62832/">similar question</a>. You could try the same loop in Python. <a href="http://code.activestate.com/recipes/15703... | 6 | 2008-10-08T11:28:12Z | [
"python",
"file",
"pywin32",
"watch"
] |
How do I watch a file for changes using Python? | 182,197 | <p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p>
<p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin... | 197 | 2008-10-08T11:12:55Z | 182,247 | <p>Have you already looked at the documentation available on <a href="http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html">http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html</a>? If you only need it to work under Windows the 2nd example seems to be exactly what you w... | 46 | 2008-10-08T11:29:43Z | [
"python",
"file",
"pywin32",
"watch"
] |
How do I watch a file for changes using Python? | 182,197 | <p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p>
<p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin... | 197 | 2008-10-08T11:12:55Z | 182,259 | <p>If polling is good enough for you, I'd just watch if the "modified time" file stat changes. To read it:</p>
<pre><code>os.stat(filename).st_mtime
</code></pre>
<p>(Also note that the Windows native change event solution does not work in all circumstances, e.g. on network drives.)</p>
<pre><code>import os
class ... | 49 | 2008-10-08T11:34:14Z | [
"python",
"file",
"pywin32",
"watch"
] |
How do I watch a file for changes using Python? | 182,197 | <p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p>
<p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin... | 197 | 2008-10-08T11:12:55Z | 182,297 | <p>As you can see in <a href="http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html" rel="nofollow">Tim Golden's article</a>, pointed by <a href="http://stackoverflow.com/users/22312/horst-gutmann">Horst Gutmann</a>, WIN32 is relatively complex and watches directories, not a single file.</p>
<p... | 2 | 2008-10-08T11:42:12Z | [
"python",
"file",
"pywin32",
"watch"
] |
How do I watch a file for changes using Python? | 182,197 | <p>I have a log file being written by another process which I want to watch for changes. Each time a change occurrs I'd like to read the new data in to do some processing on it.</p>
<p>What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the <code>win32file.Fin... | 197 | 2008-10-08T11:12:55Z | 182,441 | <p>Well, since you are using Python, you can just open a file and keep reading lines from it.</p>
<pre><code>f = open('file.log')
</code></pre>
<p>If the line read is <strong>not empty</strong>, you process it.</p>
<pre><code>line = f.readline()
if line:
// Do what you want with the line
</code></pre>
<p>You ma... | 4 | 2008-10-08T12:18:25Z | [
"python",
"file",
"pywin32",
"watch"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.