title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
How to find a thread id in Python | 919,897 | <p>I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message. </p>
<p>I would like writeLog() to be able to add something to the message t... | 68 | 2009-05-28T09:04:24Z | 30,930,569 | <p>I created multiple threads in python, I printed the thread objects, I printed the id using <code>ident</code> variable. I see all the ids are same,</p>
<pre><code><Thread(Thread-1, stopped 140500807628544)>
<Thread(Thread-2, started 140500807628544)>
<Thread(Thread-3, started 140500807628544)>
</c... | 1 | 2015-06-19T05:31:39Z | [
"python",
"multithreading"
] |
In python, how can you unload generated classes | 919,924 | <p>I am working on a library that loads files (hfd5 - pytables) into an object structure. The actual classes being used for the structure is loaded as a string from the hdf5 file, and then
loaded in this fashion:</p>
<pre><code>class NamespaceHolder(dict):
# stmt is the source code holding all the class defs
d... | 1 | 2009-05-28T09:12:55Z | 920,013 | <p>I think the GC can cope with circular references, however you'll need to do is remove the reference from the globals() dict:</p>
<pre><code>try:
del globals()['DummyA']
except KeyError:
pass
</code></pre>
<p>otherwise there will be a non-circular reference to the class object that will stop it being cleane... | 1 | 2009-05-28T09:37:32Z | [
"python",
"dynamic",
"garbage-collection",
"classloader"
] |
In python, how can you unload generated classes | 919,924 | <p>I am working on a library that loads files (hfd5 - pytables) into an object structure. The actual classes being used for the structure is loaded as a string from the hdf5 file, and then
loaded in this fashion:</p>
<pre><code>class NamespaceHolder(dict):
# stmt is the source code holding all the class defs
d... | 1 | 2009-05-28T09:12:55Z | 920,539 | <p>The gc.set_debug(gc.DEBUG_LEAK) causes the leak. Try this:</p>
<pre><code>import gc
def foo():
code = """
class DummyA(object):
pass
"""
context = {}
exec code in context
exec code in context
gc.collect()
print len(gc.garbage), len(gc.get_obje... | 1 | 2009-05-28T12:07:44Z | [
"python",
"dynamic",
"garbage-collection",
"classloader"
] |
Stuck on official Django Tutorial | 919,927 | <p>I am just started out learning Python and also started looking into Django a little bit. So I copied this piece of code from the tutorial:</p>
<pre><code> # Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
... | 3 | 2009-05-28T09:13:27Z | 919,945 | <p>You have three underscores before "unicode__" on the Choice class, it should be only two like in your Poll class, like this:</p>
<pre><code>def __unicode__(self):
return u'%s' % self.choice
</code></pre>
| 8 | 2009-05-28T09:18:32Z | [
"python",
"django"
] |
Stuck on official Django Tutorial | 919,927 | <p>I am just started out learning Python and also started looking into Django a little bit. So I copied this piece of code from the tutorial:</p>
<pre><code> # Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
... | 3 | 2009-05-28T09:13:27Z | 919,949 | <p>Your Unicode method has too many underscores. It should read:</p>
<pre><code>def __unicode__(self):
return u'%s' % self.choice
</code></pre>
| 4 | 2009-05-28T09:19:25Z | [
"python",
"django"
] |
Stuck on official Django Tutorial | 919,927 | <p>I am just started out learning Python and also started looking into Django a little bit. So I copied this piece of code from the tutorial:</p>
<pre><code> # Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
... | 3 | 2009-05-28T09:13:27Z | 919,951 | <p>Change:</p>
<pre><code>class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def ___unicode__(self):
return self.choice #shouldn't this return the choice
</code></pre>
<p>To:</p>
<pre><code>class Choice(models.M... | 3 | 2009-05-28T09:20:03Z | [
"python",
"django"
] |
Stuck on official Django Tutorial | 919,927 | <p>I am just started out learning Python and also started looking into Django a little bit. So I copied this piece of code from the tutorial:</p>
<pre><code> # Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
... | 3 | 2009-05-28T09:13:27Z | 919,953 | <p>The official Django book is a bit outdated. But the comments to the paragraphs are really useful. It should be two underscores:</p>
<pre><code>___unicode__(self):
</code></pre>
<p>should be <code>__unicode__(self):</code></p>
| 2 | 2009-05-28T09:20:44Z | [
"python",
"django"
] |
Different behavior of python logging module when using mod_python | 919,990 | <p>We have a nasty problem where we see that the python logging module is behaving differently when running with mod_python on our servers. When executing the same code in the shell, or in django with the runserver command or with mod_wsgi, the behavior is correct:</p>
<pre><code>import logging
logger = logging.getLog... | 1 | 2009-05-28T09:31:42Z | 920,113 | <p>It may be better if you do not configure logging in <code>settings.py</code>.</p>
<p>We configure your logging in our root <code>urls.py</code>. This seems to work out better. I haven't read enough Django source to know why, precisely, it's better, but it's working out well for us. I would add custom handlers he... | 5 | 2009-05-28T10:04:46Z | [
"python",
"django",
"logging",
"mod-python"
] |
Different behavior of python logging module when using mod_python | 919,990 | <p>We have a nasty problem where we see that the python logging module is behaving differently when running with mod_python on our servers. When executing the same code in the shell, or in django with the runserver command or with mod_wsgi, the behavior is correct:</p>
<pre><code>import logging
logger = logging.getLog... | 1 | 2009-05-28T09:31:42Z | 939,645 | <p>The problem is not solved by using mod_wsgi.</p>
<p>I could solve the problem by placing the complete configuration into one file. Mixing file and code configuration seems to create problems with apache (whether using mod_wsgi or mod_python).</p>
<p>To use a custom logging handler with file configuration, I had to... | 0 | 2009-06-02T13:45:13Z | [
"python",
"django",
"logging",
"mod-python"
] |
Different behavior of python logging module when using mod_python | 919,990 | <p>We have a nasty problem where we see that the python logging module is behaving differently when running with mod_python on our servers. When executing the same code in the shell, or in django with the runserver command or with mod_wsgi, the behavior is correct:</p>
<pre><code>import logging
logger = logging.getLog... | 1 | 2009-05-28T09:31:42Z | 962,224 | <p>You don't appear to have posted all the relevant information - for example, where is your logging configuration file?</p>
<p>You say that:</p>
<blockquote>
<p>When executing the same code in the
shell, or in django with the runserver
command or with mod_wsgi, the behavior
is correct</p>
</blockquote>
<p>Y... | 0 | 2009-06-07T16:26:25Z | [
"python",
"django",
"logging",
"mod-python"
] |
Using cookies with python to store searches | 920,278 | <p>Hey i have a webpage for searching a database. i would like to be able to implement cookies using python to store what a user searches for and provide them with a recently searched field when they return. is there a way to implement this using the python Cookie library??</p>
| 0 | 2009-05-28T10:57:27Z | 920,727 | <p>Usually, we do the following.</p>
<ol>
<li><p>Use a framework.</p></li>
<li><p>Establish a session. Ideally, ask for a username of some kind. If you don't want to ask for names or anything, you can try to the browser's IP address as the key for the session (this can turn into a nightmare, but you can try it.)</p... | 1 | 2009-05-28T13:04:36Z | [
"python",
"cookies"
] |
Using cookies with python to store searches | 920,278 | <p>Hey i have a webpage for searching a database. i would like to be able to implement cookies using python to store what a user searches for and provide them with a recently searched field when they return. is there a way to implement this using the python Cookie library??</p>
| 0 | 2009-05-28T10:57:27Z | 921,746 | <p>To use cookies you can use whichever API for cookies your framework is using.</p>
<p>Here's a <a href="http://cherrypy.org/" rel="nofollow">CherryPy</a> full working example for doing what you want, store searches and provide them later.</p>
<pre><code>import cherrypy
import json
class Root(object):
def index... | 0 | 2009-05-28T16:11:37Z | [
"python",
"cookies"
] |
Python Cookies question | 920,472 | <pre><code>import cgitb
import Cookie, urllib2
from cookielib import FileCookieJar
cgitb.enable()
c = Cookie.SmartCookie()
c['ini'] = 1
savedc = FileCookieJar()
savedc.add_cookie_header(c.output())
savedc.save()
</code></pre>
<p>shoulden't this save the cookie?...</p>
<p>i've been reading over the python documentatio... | 1 | 2009-05-28T11:49:15Z | 920,602 | <p>Make sure you name the file to store cookies in:</p>
<pre><code>savedc = FileCookieJar('cookies.txt')
</code></pre>
<p><code>add_cookie_header</code> takes a Request object; <code>set_cookie</code> takes a Cookie. As it says in the documentation, <code>FileCookieJar.save</code> "raises NotImplementedError. Subcla... | 0 | 2009-05-28T12:32:34Z | [
"python",
"cookies",
"cgi"
] |
Python Cookies question | 920,472 | <pre><code>import cgitb
import Cookie, urllib2
from cookielib import FileCookieJar
cgitb.enable()
c = Cookie.SmartCookie()
c['ini'] = 1
savedc = FileCookieJar()
savedc.add_cookie_header(c.output())
savedc.save()
</code></pre>
<p>shoulden't this save the cookie?...</p>
<p>i've been reading over the python documentatio... | 1 | 2009-05-28T11:49:15Z | 920,697 | <p><strong>Raf</strong>, all I can say is, Egads! The documentation certainly is <em>not</em> clear! I have used Python for years and this simple Stack Overflow question that I thought I'd quickly nab before getting started on real work for the day has taken me more than twenty minutes to answer. :-)</p>
<p>First: i... | 5 | 2009-05-28T12:57:47Z | [
"python",
"cookies",
"cgi"
] |
When to use While or the For in python | 920,645 | <p>I am currently finding problems in when I should use the <code>while loop</code> or the <code>for loop</code> in python. What it looks like is that people prefer using the <code>for loop</code> <em>(less code lines?)</em>. Is there any specific situation which I should use one or the other? Is it a matter of persona... | 15 | 2009-05-28T12:44:27Z | 920,652 | <p>The <code>for</code> is the more pythonic choice for iterating a list since it is simpler and easier to read.</p>
<p>For example this:</p>
<pre><code>for i in range(11):
print i
</code></pre>
<p>is much simpler and easier to read than this:</p>
<pre><code>i = 0
while i <= 10:
print i
i = i + 1
</c... | 8 | 2009-05-28T12:46:14Z | [
"python",
"loops"
] |
When to use While or the For in python | 920,645 | <p>I am currently finding problems in when I should use the <code>while loop</code> or the <code>for loop</code> in python. What it looks like is that people prefer using the <code>for loop</code> <em>(less code lines?)</em>. Is there any specific situation which I should use one or the other? Is it a matter of persona... | 15 | 2009-05-28T12:44:27Z | 920,686 | <p><code>while</code> is useful in scenarios where the break condition doesn't logically depend on any kind of sequence. For example, consider unpredictable interactions:</p>
<pre><code> while user_is_sleeping():
wait()
</code></pre>
<p>Of course, you <em>could</em> write an appropriate iterator to encapsulate t... | 12 | 2009-05-28T12:55:01Z | [
"python",
"loops"
] |
When to use While or the For in python | 920,645 | <p>I am currently finding problems in when I should use the <code>while loop</code> or the <code>for loop</code> in python. What it looks like is that people prefer using the <code>for loop</code> <em>(less code lines?)</em>. Is there any specific situation which I should use one or the other? Is it a matter of persona... | 15 | 2009-05-28T12:44:27Z | 920,692 | <p>Yes, there is a huge difference between while and for.</p>
<p>The <strong>for</strong> statement iterates through a collection or iterable object or generator function.</p>
<p>The <strong>while</strong> statement simply loops until a condition is False.</p>
<p>It isn't preference. It's a question of what your da... | 36 | 2009-05-28T12:55:59Z | [
"python",
"loops"
] |
When to use While or the For in python | 920,645 | <p>I am currently finding problems in when I should use the <code>while loop</code> or the <code>for loop</code> in python. What it looks like is that people prefer using the <code>for loop</code> <em>(less code lines?)</em>. Is there any specific situation which I should use one or the other? Is it a matter of persona... | 15 | 2009-05-28T12:44:27Z | 920,792 | <p>For loops usually make it clearer what the iteration is doing. You can't always use them directly, but most of the times the iteration logic with the while loop can be wrapped inside a generator func. For example:</p>
<pre><code>def path_to_root(node):
while node is not None:
yield node
node = n... | 0 | 2009-05-28T13:17:54Z | [
"python",
"loops"
] |
When to use While or the For in python | 920,645 | <p>I am currently finding problems in when I should use the <code>while loop</code> or the <code>for loop</code> in python. What it looks like is that people prefer using the <code>for loop</code> <em>(less code lines?)</em>. Is there any specific situation which I should use one or the other? Is it a matter of persona... | 15 | 2009-05-28T12:44:27Z | 920,867 | <p>First of all there are differences between the for loop in python and in other languages.
While in python it iterates over a list of values (eg: for value in [4,3,2,7]), in most other languages (C/C++, Java, PHP etc) it acts as a while loop, but easier to read.</p>
<p>For loops are generally used when the number of... | 1 | 2009-05-28T13:32:43Z | [
"python",
"loops"
] |
When to use While or the For in python | 920,645 | <p>I am currently finding problems in when I should use the <code>while loop</code> or the <code>for loop</code> in python. What it looks like is that people prefer using the <code>for loop</code> <em>(less code lines?)</em>. Is there any specific situation which I should use one or the other? Is it a matter of persona... | 15 | 2009-05-28T12:44:27Z | 922,851 | <p>Consider processing iterables. You can do it with a <code>for</code> loop:</p>
<pre><code>for i in mylist:
print i
</code></pre>
<p>Or, you can do it with a <code>while</code> loop:</p>
<pre><code>it = mylist.__iter__()
while True:
try:
print it.next()
except StopIteration:
break
</code></pr... | 2 | 2009-05-28T19:56:04Z | [
"python",
"loops"
] |
The right way to auto filter SQLAlchemy queries? | 920,724 | <p>I've just introspected a pretty nasty schema from a CRM app with sqlalchemy. All of the tables have a deleted column on them and I wanted to auto filter all those entities and relations flagged as deleted. Here's what I came up with:</p>
<p><hr /></p>
<pre><code>class CustomizableQuery(Query):
"""An overridden... | 4 | 2009-05-28T13:03:37Z | 920,752 | <p>You can map to a select. Like this:</p>
<pre><code>mapper(EmailInfo, select([email_join], email_join.c.deleted == False))
</code></pre>
| 6 | 2009-05-28T13:11:18Z | [
"python",
"sqlalchemy",
"sugarcrm"
] |
The right way to auto filter SQLAlchemy queries? | 920,724 | <p>I've just introspected a pretty nasty schema from a CRM app with sqlalchemy. All of the tables have a deleted column on them and I wanted to auto filter all those entities and relations flagged as deleted. Here's what I came up with:</p>
<p><hr /></p>
<pre><code>class CustomizableQuery(Query):
"""An overridden... | 4 | 2009-05-28T13:03:37Z | 925,533 | <p>I'd consider seeing if it was possible to create views for these tables that filter out the deleted elements, and then you might be able to map directly to that view instead of the underlying table, at least for querying operations. However I've never tried this myself!</p>
| 0 | 2009-05-29T11:22:27Z | [
"python",
"sqlalchemy",
"sugarcrm"
] |
Sending Multipart html emails which contain embedded images | 920,910 | <p>I've been playing around with the email module in python but I want to be able to know how to embed images which are included in the html.</p>
<p>So for example if the body is something like</p>
<pre><code><img src="../path/image.png"></img>
</code></pre>
<p>I would like to embed <em>image.png</em> in... | 36 | 2009-05-28T13:45:57Z | 920,928 | <p>Here is an example I found.</p>
<blockquote>
<p><a href="http://code.activestate.com/recipes/473810/"><strong>Recipe 473810: Send an HTML email with embedded image and plain text alternate</strong></a>: </p>
<p>HTML is the method of choice for those
wishing to send emails with rich text,
layout and grap... | 82 | 2009-05-28T13:48:27Z | [
"python",
"email",
"mime",
"attachment",
"multipart"
] |
Formatted output in OpenOffice/Microsoft Word with Python | 920,938 | <p>I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).</p>
<p>Is there a way to generate such a report... | 6 | 2009-05-28T13:49:45Z | 920,980 | <p>I know there is an <a href="http://www.rexx.com/~dkuhlman/odtwriter.html" rel="nofollow">odtwriter for docutils</a>. You could generate your output as <a href="http://docutils.sourceforge.net/rst.html" rel="nofollow">reStructuredText</a> and feed it to odtwriter or look into what odtwriter is using on the backend to... | 1 | 2009-05-28T13:58:03Z | [
"python",
"formatting",
"ms-word",
"ms-office",
"openoffice.org"
] |
Formatted output in OpenOffice/Microsoft Word with Python | 920,938 | <p>I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).</p>
<p>Is there a way to generate such a report... | 6 | 2009-05-28T13:49:45Z | 920,989 | <p>I've used xlwt to create Excel documents using python, but I haven't needed to write word files yet. I've found this package, <a href="http://pypi.python.org/pypi/OOoPy/1.4.4873" rel="nofollow">OOoPy</a>, but I haven't used it.</p>
<p>Also you might want to try outputting html files and having the users open them i... | 1 | 2009-05-28T13:59:56Z | [
"python",
"formatting",
"ms-word",
"ms-office",
"openoffice.org"
] |
Formatted output in OpenOffice/Microsoft Word with Python | 920,938 | <p>I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).</p>
<p>Is there a way to generate such a report... | 6 | 2009-05-28T13:49:45Z | 921,046 | <p>I think OpenOffice has some Python bindings - you should be able to write OO macros in Python.</p>
<p>But I would use HTML instead - Word and OO.org are rather good at editing it and you can write it from Python easily (although Word saves a lot of mess which could complicate parsing it by your Python app).</p>
| 0 | 2009-05-28T14:12:26Z | [
"python",
"formatting",
"ms-word",
"ms-office",
"openoffice.org"
] |
Formatted output in OpenOffice/Microsoft Word with Python | 920,938 | <p>I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).</p>
<p>Is there a way to generate such a report... | 6 | 2009-05-28T13:49:45Z | 921,680 | <p>" The formatting is complex(bullet points, paragraphs, bold face, etc), "</p>
<p>Use <a href="http://docutils.sourceforge.net/docs/user/rst/quickstart.html" rel="nofollow">RST</a>.</p>
<p>It's trivial to produce, since it's plain text.</p>
<p>It's trivial to edit, since it's plain text with a few extra characters... | 2 | 2009-05-28T15:59:54Z | [
"python",
"formatting",
"ms-word",
"ms-office",
"openoffice.org"
] |
Formatted output in OpenOffice/Microsoft Word with Python | 920,938 | <p>I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).</p>
<p>Is there a way to generate such a report... | 6 | 2009-05-28T13:49:45Z | 922,117 | <p>A little known, and slightly evil fact: If you create an HTML file, and stick a .doc extension on it, Word will open it as a Word document, and most users will be none the wiser.</p>
<p>Except maybe a very technical person will say, my this is a small Word file! :)</p>
| 3 | 2009-05-28T17:18:42Z | [
"python",
"formatting",
"ms-word",
"ms-office",
"openoffice.org"
] |
Formatted output in OpenOffice/Microsoft Word with Python | 920,938 | <p>I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).</p>
<p>Is there a way to generate such a report... | 6 | 2009-05-28T13:49:45Z | 4,819,302 | <p>Use the <a href="https://github.com/mikemaccana/python-docx" rel="nofollow">Python Docx</a> module for this - 100% Python, tables, images, document properties, headings, paragraphs, and more.</p>
| 2 | 2011-01-27T16:53:28Z | [
"python",
"formatting",
"ms-word",
"ms-office",
"openoffice.org"
] |
Formatted output in OpenOffice/Microsoft Word with Python | 920,938 | <p>I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).</p>
<p>Is there a way to generate such a report... | 6 | 2009-05-28T13:49:45Z | 12,730,111 | <p>You can use QTextDocument, QTextCursor and QTextDocumentWriter in <a href="http://www.riverbankcomputing.co.uk/software/pyqt/download" rel="nofollow">PyQt4</a>. A simple example to show how to write to an odt file:</p>
<pre><code>>>>from pyqt4 import QtGui
# Create a document object
>>>doc = QtGui... | 1 | 2012-10-04T14:56:49Z | [
"python",
"formatting",
"ms-word",
"ms-office",
"openoffice.org"
] |
How to use classes derived from Python's list class | 921,334 | <p>This is a followup to question 912526 - <a href="http://stackoverflow.com/questions/912526/how-do-i-pass-lots-of-variables-to-and-from-a-function-in-python">http://stackoverflow.com/questions/912526/how-do-i-pass-lots-of-variables-to-and-from-a-function-in-python</a>.</p>
<p>There are lots of variables that need to... | 6 | 2009-05-28T15:05:13Z | 921,510 | <p>There are several issues in your code:</p>
<p>1.If you make any list operation the result will be a native list:</p>
<pre><code>layers1 = Layers()
layers2 = Layers()
layers1 + layers2 -> the result will be a native list
</code></pre>
<p>2.Why define input_string when you can override <a href="http://docs.pytho... | 9 | 2009-05-28T15:31:42Z | [
"python",
"class"
] |
Retrieving all Cookies in Python | 921,532 | <p>How do I read back all of the cookies in Python without knowing their names?</p>
| 14 | 2009-05-28T15:35:09Z | 921,544 | <p>Look at the <code>Cookie:</code> headers in the HTTP response you get, parse their contents with module <code>Cookie</code> in the standard library.</p>
| 3 | 2009-05-28T15:38:04Z | [
"python",
"cookies"
] |
Retrieving all Cookies in Python | 921,532 | <p>How do I read back all of the cookies in Python without knowing their names?</p>
| 14 | 2009-05-28T15:35:09Z | 921,652 | <p>Put <code>os.environ['HTTP_COOKIE']</code> into an array:</p>
<pre><code>#!/usr/bin/env python
import os
if 'HTTP_COOKIE' in os.environ:
cookies = os.environ['HTTP_COOKIE']
cookies = cookies.split('; ')
handler = {}
for cookie in cookies:
cookie = cookie.split('=')
handler[cookie[0]] = cookie[1]
<... | 4 | 2009-05-28T15:55:15Z | [
"python",
"cookies"
] |
Retrieving all Cookies in Python | 921,532 | <p>How do I read back all of the cookies in Python without knowing their names?</p>
| 14 | 2009-05-28T15:35:09Z | 921,744 | <p>Not sure if this is what you are looking for, but here is a simple example where you put cookies in a cookiejar and read them back:</p>
<pre><code>from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib
#Create a CookieJar object to hold the cookies
cj = cookielib.CookieJar()
#... | 21 | 2009-05-28T16:11:27Z | [
"python",
"cookies"
] |
Retrieving all Cookies in Python | 921,532 | <p>How do I read back all of the cookies in Python without knowing their names?</p>
| 14 | 2009-05-28T15:35:09Z | 30,493,213 | <p>This may be exactly what you are looking for.</p>
<p>Python 3.4</p>
<pre><code>import requests
r = requests.get('http://www.about.com/')
c = r.cookies
i = c.items()
for name, value in i:
print(name, value)
</code></pre>
| 1 | 2015-05-27T21:10:44Z | [
"python",
"cookies"
] |
Learning parser in python | 921,792 | <p>I recall I have read about a parser which you just have to feed some sample lines, for it to know how to parse some text.</p>
<p>It just determines the difference between two lines to know what the variable parts are. I thought it was written in python, but i'm not sure. Does anyone know what library that was?</p>
| 12 | 2009-05-28T16:18:45Z | 922,125 | <p>Conceivably you might mean <a href="http://sourceforge.net/project/showfiles.php?group%5Fid=81259" rel="nofollow">Reverend</a>?</p>
| 2 | 2009-05-28T17:20:43Z | [
"python",
"parsing"
] |
Learning parser in python | 921,792 | <p>I recall I have read about a parser which you just have to feed some sample lines, for it to know how to parse some text.</p>
<p>It just determines the difference between two lines to know what the variable parts are. I thought it was written in python, but i'm not sure. Does anyone know what library that was?</p>
| 12 | 2009-05-28T16:18:45Z | 922,930 | <p>Probably you mean <em><a href="http://code.google.com/p/templatemaker/" rel="nofollow">TemplateMaker</a></em>, I haven't tried it yet, but it builds on well-researched <em>longest-common-substring</em> algorithms and thus should work reasonably... If you are interested in different (more complex) approaches, you can... | 10 | 2009-05-28T20:17:54Z | [
"python",
"parsing"
] |
Python and Qt - function reloading | 921,929 | <p>i have an application class inherited from QtGui.QDialog.
I have to reload show-function but save functionality. I take this idea from C#.
There i could do something like this:</p>
<pre><code>static void show()
{
// My code...
base.show();
}
</code></pre>
<p>I want to do something like that but with python and... | 0 | 2009-05-28T16:42:12Z | 922,025 | <p>Checkout the <a href="http://docs.python.org/dev/library/functions.html#super" rel="nofollow">super()</a> function but note some <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=236275" rel="nofollow">pitfalls</a>.</p>
| 1 | 2009-05-28T17:00:09Z | [
"python",
"function",
"reloading"
] |
How to make mechanize not fail with forms on this page? | 922,148 | <pre><code>import mechanize
url = 'http://steamcommunity.com'
br=mechanize.Browser(factory=mechanize.RobustFactory())
br.open(url)
print br.request
print br.form
for each in br.forms():
print each
print
</code></pre>
<p>The above code results in:</p>
<pre><code>Traceback (most recent call last):
File "./... | 4 | 2009-05-28T17:25:21Z | 922,215 | <p>Did you mention that the website is redirecting to an https (ssl) server ?</p>
<p>Well, try to set a new HTTPS handler like this:</p>
<pre><code>mechanize.HTTPSHandler()
</code></pre>
| 2 | 2009-05-28T17:38:11Z | [
"python",
"automation",
"screen-scraping",
"mechanize"
] |
How to make mechanize not fail with forms on this page? | 922,148 | <pre><code>import mechanize
url = 'http://steamcommunity.com'
br=mechanize.Browser(factory=mechanize.RobustFactory())
br.open(url)
print br.request
print br.form
for each in br.forms():
print each
print
</code></pre>
<p>The above code results in:</p>
<pre><code>Traceback (most recent call last):
File "./... | 4 | 2009-05-28T17:25:21Z | 5,920,926 | <p>Use this secret, i'm sure this is work for you ;)</p>
<pre><code>br = mechanize.Browser(factory=mechanize.DefaultFactory(i_want_broken_xhtml_support=True))
</code></pre>
| 2 | 2011-05-07T12:11:30Z | [
"python",
"automation",
"screen-scraping",
"mechanize"
] |
Which process was responsible for an event signalled by inotify? | 922,200 | <p>I am using <code>pyinotify</code> to detect access, changes, etc. on files in a given directory. Is there an easier way to find out which process was responsible for that - without having to <a href="http://lkml.indiana.edu/hypermail/linux/kernel/0603.1/0166.html" rel="nofollow">patch <code>inotify</code></a>?</p>
| 3 | 2009-05-28T17:35:09Z | 1,450,794 | <p>No, you can't, that information isn't in the <code>struct inotify_event</code> sent by the kernel.</p>
<p>Actually there isn't any guarantee that the process responsible is still running when you get the event.</p>
| 1 | 2009-09-20T11:05:19Z | [
"python",
"inotify",
"pyinotify"
] |
Which process was responsible for an event signalled by inotify? | 922,200 | <p>I am using <code>pyinotify</code> to detect access, changes, etc. on files in a given directory. Is there an easier way to find out which process was responsible for that - without having to <a href="http://lkml.indiana.edu/hypermail/linux/kernel/0603.1/0166.html" rel="nofollow">patch <code>inotify</code></a>?</p>
| 3 | 2009-05-28T17:35:09Z | 1,458,541 | <p>Assuming you are on Linux (pyinotify would tend to indicate this) you could use SELinux (running in permissive mode of course) to wrap a process(es) and log all their file access/creation/deletion/etc.</p>
| 1 | 2009-09-22T07:08:21Z | [
"python",
"inotify",
"pyinotify"
] |
Exporting a zope folder with python | 922,319 | <p>We have two zope servers running our company's internal site. One is the live site and one is the dev site. I'm working on writing a python script that moves everything from the dev server to the live server. Right now the process involves a bunch of steps that are done in the zope management interface. I need t... | 5 | 2009-05-28T17:55:41Z | 960,024 | <p>You can try to use the functions <code>manage_exportObject</code> and <code>manage_importObject</code> located in the file <code>$ZOPE_HOME/lib/python/OFS/ObjectManager.py</code></p>
<p>Let say we install two Zope 2.8 instances located at:</p>
<ul>
<li><code>/tmp/instance/dev</code> for the development server (por... | 3 | 2009-06-06T16:03:14Z | [
"python",
"zope"
] |
Exporting a zope folder with python | 922,319 | <p>We have two zope servers running our company's internal site. One is the live site and one is the dev site. I'm working on writing a python script that moves everything from the dev server to the live server. Right now the process involves a bunch of steps that are done in the zope management interface. I need t... | 5 | 2009-05-28T17:55:41Z | 997,279 | <p>To make this more general and allow copying folders not in the root directory I would do something like this:</p>
<pre><code>#!/usr/bin/python
import urllib
import shutil
username_dev = 'admin'
username_prod = 'admin'
password_dev = 'password_dev'
password_prod = 'password_prod'
url_dev = 'localhost:8080'
url_prod... | 2 | 2009-06-15T17:17:57Z | [
"python",
"zope"
] |
Exporting a zope folder with python | 922,319 | <p>We have two zope servers running our company's internal site. One is the live site and one is the dev site. I'm working on writing a python script that moves everything from the dev server to the live server. Right now the process involves a bunch of steps that are done in the zope management interface. I need t... | 5 | 2009-05-28T17:55:41Z | 1,022,056 | <p>If you really move <em>everything</em> you could probably just move the Data.fs instead. But otherwise the import/export above is a good way.</p>
| 0 | 2009-06-20T17:21:05Z | [
"python",
"zope"
] |
How do I deactivate an egg? | 922,323 | <p>I've installed cx_Oracle (repeatedly) and I just can't get it to work on my Intel Mac. How do I deactivate/uninstall it?</p>
| 3 | 2009-05-28T17:55:56Z | 922,381 | <p>You simply delete the <code>.egg</code> file</p>
<p>On OS X they are installed into <code>/Library/Python/2.5/site-packages/</code> - in that folder you should find a file named <code>cx_Oracle.egg</code> or similar. You can simple delete this file and it will be gone.</p>
<p>One way of finding the file is, if you... | 3 | 2009-05-28T18:07:33Z | [
"python",
"uninstall",
"egg"
] |
Is it possible to launch a Paster shell with some modules pre-imported? | 922,351 | <p>Is it possible to run "paster shell blah.ini" (or a variant thereof) and have it automatically load certain libraries?</p>
<p>I hate having to always type "from foo.bar import mystuff" as the first command in every paster shell, and would like the computer to do it for me.</p>
| 5 | 2009-05-28T18:01:54Z | 980,897 | <p>If you set the environment variable PYTHONSTARTUP to the name of a file, it will execute that file on opening the interactive prompt.</p>
<p>I don't know anything about paster shell, but I assume it works similarly.</p>
<p>Alternatively you could look into iPython, which has much more powerful features (particular... | 0 | 2009-06-11T12:53:00Z | [
"python",
"pylons",
"paster"
] |
Is it possible to launch a Paster shell with some modules pre-imported? | 922,351 | <p>Is it possible to run "paster shell blah.ini" (or a variant thereof) and have it automatically load certain libraries?</p>
<p>I hate having to always type "from foo.bar import mystuff" as the first command in every paster shell, and would like the computer to do it for me.</p>
| 5 | 2009-05-28T18:01:54Z | 1,008,481 | <p>An option to try would be to create a sitecustomize.py script. If you have this in the same folder as your paster shell, the python interpreter should load it up on startup. </p>
<p>Let me clarify, sitecustomize.py, if found, is <em>always</em> loaded on startup of the interpreter. So if you put it where it can... | 2 | 2009-06-17T17:26:46Z | [
"python",
"pylons",
"paster"
] |
How to mark a global as deprecated in Python? | 922,550 | <p><a href="http://wiki.python.org/moin/PythonDecoratorLibrary#Smartdeprecationwarnings.28withvalidfilenames.2Clinenumbers.2Cetc..29">I've seen decorators</a> that let you mark a function a deprecated so that a warning is given whenever that function is used. I'd like to do the same thing but for a global variable, but... | 6 | 2009-05-28T18:48:00Z | 922,645 | <p>You could make your module into a class (see e.g <a href="http://stackoverflow.com/questions/880530/can-python-modules-have-properties-the-same-way-that-objects-can">this SO question</a>) and make that deprecated global into a property, so you can execute some of your code when it's accessed and provide the warning ... | 4 | 2009-05-28T19:08:02Z | [
"python",
"hook",
"decorator",
"global",
"deprecated"
] |
How to mark a global as deprecated in Python? | 922,550 | <p><a href="http://wiki.python.org/moin/PythonDecoratorLibrary#Smartdeprecationwarnings.28withvalidfilenames.2Clinenumbers.2Cetc..29">I've seen decorators</a> that let you mark a function a deprecated so that a warning is given whenever that function is used. I'd like to do the same thing but for a global variable, but... | 6 | 2009-05-28T18:48:00Z | 922,693 | <p>You can't do this directly, since theres no way of intercepting the module access. However, you can replace that module with an object of your choosing that acts as a proxy, looking for accesses to certain properties:</p>
<pre><code>import sys, warnings
def WrapMod(mod, deprecated):
"""Return a wrapped object... | 13 | 2009-05-28T19:16:29Z | [
"python",
"hook",
"decorator",
"global",
"deprecated"
] |
How to mark a global as deprecated in Python? | 922,550 | <p><a href="http://wiki.python.org/moin/PythonDecoratorLibrary#Smartdeprecationwarnings.28withvalidfilenames.2Clinenumbers.2Cetc..29">I've seen decorators</a> that let you mark a function a deprecated so that a warning is given whenever that function is used. I'd like to do the same thing but for a global variable, but... | 6 | 2009-05-28T18:48:00Z | 924,306 | <p>Behold:</p>
<h3>Code</h3>
<pre><code>from types import *
def wrapper(f, warning):
def new(*args, **kwargs):
if not args[0].warned:
print "Deprecated Warning: %s" % warning
args[0].warned = True
return f(*args, **kwargs)
return new
class Deprecated(object):
def ... | 4 | 2009-05-29T03:51:55Z | [
"python",
"hook",
"decorator",
"global",
"deprecated"
] |
Unicode problems in PyObjC | 922,562 | <p>I am trying to figure out PyObjC on Mac OS X, and I have written a simple program to print out the names in my Address Book. However, I am having some trouble with the encoding of the output.</p>
<pre><code>#! /usr/bin/env python
# -*- coding: UTF-8 -*-
from AddressBook import *
ab = ABAddressBook.sharedAddressBo... | 3 | 2009-05-28T18:51:32Z | 922,592 | <p>Just writing the variable name sends <code>repr(name)</code> to the standard output and <code>repr()</code> encodes all unicode values.</p>
<p><code>print</code> tries to convert <code>u'Jacob \xc5berg'</code> to ASCII, which doesn't work. Try writing it to a file.</p>
<p>See <a href="http://wiki.python.org/moin/P... | 0 | 2009-05-28T18:56:54Z | [
"python",
"osx",
"unicode",
"pyobjc"
] |
Unicode problems in PyObjC | 922,562 | <p>I am trying to figure out PyObjC on Mac OS X, and I have written a simple program to print out the names in my Address Book. However, I am having some trouble with the encoding of the output.</p>
<pre><code>#! /usr/bin/env python
# -*- coding: UTF-8 -*-
from AddressBook import *
ab = ABAddressBook.sharedAddressBo... | 3 | 2009-05-28T18:51:32Z | 922,622 | <pre><code># -*- coding: UTF-8 -*-
</code></pre>
<p>only affects the way Python decodes comments and string literals in your source, <strong>not</strong> the way standard output is configured, etc, etc. If you set your Mac's Terminal to UTF-8 (Terminal, Preferences, Settings, Advanced, International dropdown) and emi... | 2 | 2009-05-28T19:02:45Z | [
"python",
"osx",
"unicode",
"pyobjc"
] |
Unicode problems in PyObjC | 922,562 | <p>I am trying to figure out PyObjC on Mac OS X, and I have written a simple program to print out the names in my Address Book. However, I am having some trouble with the encoding of the output.</p>
<pre><code>#! /usr/bin/env python
# -*- coding: UTF-8 -*-
from AddressBook import *
ab = ABAddressBook.sharedAddressBo... | 3 | 2009-05-28T18:51:32Z | 922,647 | <p>Convert it to a unicode string through:</p>
<pre><code>print unicode(name)
</code></pre>
| 0 | 2009-05-28T19:08:02Z | [
"python",
"osx",
"unicode",
"pyobjc"
] |
Unicode problems in PyObjC | 922,562 | <p>I am trying to figure out PyObjC on Mac OS X, and I have written a simple program to print out the names in my Address Book. However, I am having some trouble with the encoding of the output.</p>
<pre><code>#! /usr/bin/env python
# -*- coding: UTF-8 -*-
from AddressBook import *
ab = ABAddressBook.sharedAddressBo... | 3 | 2009-05-28T18:51:32Z | 924,626 | <p>If you run the code in your question in the interactive console the interpreter will print the repr of "name" because of the last statement of the loop.</p>
<p>If you change the last line of the loop from just "name" to "print name" the output should be fine. I've tested this with Terminal.app on a 10.5.7 system. <... | 1 | 2009-05-29T06:12:33Z | [
"python",
"osx",
"unicode",
"pyobjc"
] |
Installing/configuring gdbm Python module for cvs2svn? | 922,750 | <p>I am trying to install cvs2svn on a Solaris 10 machine. It has Python 2.4.4 on it. I don't have root access. When I downloaded cvs2svn and tried to run it, it said</p>
<blockquote>
<p>ERROR: cvs2svn uses the anydbm package, which depends on lower level dbm libraries. Your system has dbm, with which cvs2svn is kno... | 2 | 2009-05-28T19:28:09Z | 992,715 | <p>Set the <code>$PYTHONPATH</code> environment variable to point to the location where you installed <code>gdbm</code>. Then when you run <code>cvs2svn</code>, the anybdm module should find <code>gdbm</code> successfully.</p>
| 0 | 2009-06-14T12:10:31Z | [
"python",
"solaris",
"cvs2svn",
"gdbm"
] |
Installing/configuring gdbm Python module for cvs2svn? | 922,750 | <p>I am trying to install cvs2svn on a Solaris 10 machine. It has Python 2.4.4 on it. I don't have root access. When I downloaded cvs2svn and tried to run it, it said</p>
<blockquote>
<p>ERROR: cvs2svn uses the anydbm package, which depends on lower level dbm libraries. Your system has dbm, with which cvs2svn is kno... | 2 | 2009-05-28T19:28:09Z | 1,044,879 | <p>I downloaded Python 2.5.1 and compiled it from the source. I made sure my gdbm libraries were in the appropriate paths and used the altinstall into my home directory. I can now run cvs2svn with my private copy of python.</p>
| 2 | 2009-06-25T16:27:44Z | [
"python",
"solaris",
"cvs2svn",
"gdbm"
] |
Installing/configuring gdbm Python module for cvs2svn? | 922,750 | <p>I am trying to install cvs2svn on a Solaris 10 machine. It has Python 2.4.4 on it. I don't have root access. When I downloaded cvs2svn and tried to run it, it said</p>
<blockquote>
<p>ERROR: cvs2svn uses the anydbm package, which depends on lower level dbm libraries. Your system has dbm, with which cvs2svn is kno... | 2 | 2009-05-28T19:28:09Z | 29,111,972 | <p>To install gdbm for Python, try:</p>
<pre><code>pip install gdbm
</code></pre>
<p>If <code>pip</code> is not present, install it via: <code>easy_install pip</code>.</p>
<p>On OSX, you may try (if <code>brew</code> is installed):</p>
<pre><code>brew install gdbm
</code></pre>
| 0 | 2015-03-18T00:07:46Z | [
"python",
"solaris",
"cvs2svn",
"gdbm"
] |
Syntax Highlight for Mako in Eclipse or TextMate? | 922,771 | <p>Does anyone know of a syntax highlight for Mako templates for Eclipse or for TextMate?</p>
<p>I know that there is a <code>.mako</code> syntax highlighter for the default text editor in Ubuntu.</p>
| 12 | 2009-05-28T19:34:26Z | 922,950 | <p>Claudio,</p>
<p>I don't use mako templates, but a quick google search turned up <a href="http://groups.google.com/group/mako-discuss/browse%5Fthread/thread/796926337af30cf0" rel="nofollow">this article</a> from the mako-discuss google group, which refers to a Colorer library syntax highlighter. This sounds like it... | 2 | 2009-05-28T20:21:41Z | [
"python",
"templates",
"mako"
] |
Syntax Highlight for Mako in Eclipse or TextMate? | 922,771 | <p>Does anyone know of a syntax highlight for Mako templates for Eclipse or for TextMate?</p>
<p>I know that there is a <code>.mako</code> syntax highlighter for the default text editor in Ubuntu.</p>
| 12 | 2009-05-28T19:34:26Z | 923,030 | <p>What I ended up doing was naming my Mako Templates with .html suffix and thus getting the usual HTML syntax highlighting etc. that I am used to. Alternatively I could have associated .mako suffix with the HTML handler. While this does not address Mako specifically, it was enough for me, since I find most of the temp... | 2 | 2009-05-28T20:38:05Z | [
"python",
"templates",
"mako"
] |
Syntax Highlight for Mako in Eclipse or TextMate? | 922,771 | <p>Does anyone know of a syntax highlight for Mako templates for Eclipse or for TextMate?</p>
<p>I know that there is a <code>.mako</code> syntax highlighter for the default text editor in Ubuntu.</p>
| 12 | 2009-05-28T19:34:26Z | 929,130 | <p>I just did some <a href="http://www.google.com/search?q=mako%2Bbundle%2Btextmate" rel="nofollow">googlin'</a>. There is a <a href="http://svn.makotemplates.org/contrib/textmate/Mako.tmbundle/" rel="nofollow">Mako bundle</a>.</p>
<p>I installed it under <code>~/Library/Application Support/TextMate/Bundles/</code> li... | 8 | 2009-05-30T06:03:34Z | [
"python",
"templates",
"mako"
] |
Syntax Highlight for Mako in Eclipse or TextMate? | 922,771 | <p>Does anyone know of a syntax highlight for Mako templates for Eclipse or for TextMate?</p>
<p>I know that there is a <code>.mako</code> syntax highlighter for the default text editor in Ubuntu.</p>
| 12 | 2009-05-28T19:34:26Z | 8,544,623 | <p>You can go to:</p>
<ol>
<li>Preferences->General->Editors->File Associations.</li>
<li>Click to add a new file type and type *.mak and click OK.</li>
<li>In File types click on *.mak and under Associated editors add HTML editor(default), Text Editor, Text Editor(studio) and Web Browser.</li>
</ol>
<p>This colors t... | 1 | 2011-12-17T12:09:00Z | [
"python",
"templates",
"mako"
] |
Syntax Highlight for Mako in Eclipse or TextMate? | 922,771 | <p>Does anyone know of a syntax highlight for Mako templates for Eclipse or for TextMate?</p>
<p>I know that there is a <code>.mako</code> syntax highlighter for the default text editor in Ubuntu.</p>
| 12 | 2009-05-28T19:34:26Z | 28,453,546 | <p>There is a <a href="http://sourceforge.net/projects/makotemplateeditor/%20Mako%20Template%20Editor" rel="nofollow">Mako Template Editor</a> for Eclipse. To install, copy the plugins directory with the jar file into your dropins folder in your Eclipse root folder. That is, a manual installation of a Eclipse plugin in... | 0 | 2015-02-11T11:44:32Z | [
"python",
"templates",
"mako"
] |
Syntax Highlight for Mako in Eclipse or TextMate? | 922,771 | <p>Does anyone know of a syntax highlight for Mako templates for Eclipse or for TextMate?</p>
<p>I know that there is a <code>.mako</code> syntax highlighter for the default text editor in Ubuntu.</p>
| 12 | 2009-05-28T19:34:26Z | 30,838,894 | <ol>
<li><p>Windows (menu) > Preference > General > Editor > File Associations
Add *.mako in File Types (upper box) and add Html editor in Associated editor (lower box)</p></li>
<li><p>Windows (menu) > Preference > General > Editor > Content Types</p></li>
</ol>
<p>Under Text find HTML and add *.mako in File associati... | 0 | 2015-06-15T06:48:55Z | [
"python",
"templates",
"mako"
] |
Syntax Highlight for Mako in Eclipse or TextMate? | 922,771 | <p>Does anyone know of a syntax highlight for Mako templates for Eclipse or for TextMate?</p>
<p>I know that there is a <code>.mako</code> syntax highlighter for the default text editor in Ubuntu.</p>
| 12 | 2009-05-28T19:34:26Z | 31,271,028 | <p>The LiClipse plugin (<a href="http://www.liclipse.com/" rel="nofollow">http://www.liclipse.com/</a>), provides Mako support out of the box for Eclipse (and as a note, if you want support for any other language in Eclipse, LiClipse can load any existing TextMate bundle -- <a href="http://www.liclipse.com/textmate_bun... | 0 | 2015-07-07T14:12:19Z | [
"python",
"templates",
"mako"
] |
Check if input is a list/tuple of strings or a single string | 922,774 | <p>I've a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strings.</p>
<p>Given that strings act as lists of characters, how do I tell which the method has received?</p>
<p>I'd like to be able to accept eit... | 17 | 2009-05-28T19:35:21Z | 922,784 | <p>Have you considered <code>varargs</code> syntax? I'm not really sure if this is what you're asking, but would something like <a href="http://stackoverflow.com/questions/836387/how-can-i-tell-if-a-python-variable-is-a-string-or-a-list/836406#836406">this question</a> be along your lines?</p>
| 0 | 2009-05-28T19:36:46Z | [
"python"
] |
Check if input is a list/tuple of strings or a single string | 922,774 | <p>I've a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strings.</p>
<p>Given that strings act as lists of characters, how do I tell which the method has received?</p>
<p>I'd like to be able to accept eit... | 17 | 2009-05-28T19:35:21Z | 922,796 | <p>You can check if a variable is a string or unicode string with</p>
<pre><code>isinstance(some_object, basestring)
</code></pre>
<p>This will return <code>True</code> for both strings and unicode strings</p>
<p>Edit:</p>
<p>You could do something like this:</p>
<pre><code>if isinstance(some_object, basestring):
... | 27 | 2009-05-28T19:39:05Z | [
"python"
] |
Check if input is a list/tuple of strings or a single string | 922,774 | <p>I've a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strings.</p>
<p>Given that strings act as lists of characters, how do I tell which the method has received?</p>
<p>I'd like to be able to accept eit... | 17 | 2009-05-28T19:35:21Z | 922,799 | <p>Check the type with isinstance(arg, basestring)</p>
| 3 | 2009-05-28T19:39:30Z | [
"python"
] |
Check if input is a list/tuple of strings or a single string | 922,774 | <p>I've a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strings.</p>
<p>Given that strings act as lists of characters, how do I tell which the method has received?</p>
<p>I'd like to be able to accept eit... | 17 | 2009-05-28T19:35:21Z | 922,800 | <p>Type checking:</p>
<pre><code>def func(arg):
if not isinstance(arg, (list, tuple)):
arg = [arg]
# process
func('abc')
func(['abc', '123'])
</code></pre>
<p>Varargs:</p>
<pre><code>def func(*arg):
# process
func('abc')
func('abc', '123')
func(*['abc', '123'])
</code></pre>
| 5 | 2009-05-28T19:39:42Z | [
"python"
] |
Check if input is a list/tuple of strings or a single string | 922,774 | <p>I've a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strings.</p>
<p>Given that strings act as lists of characters, how do I tell which the method has received?</p>
<p>I'd like to be able to accept eit... | 17 | 2009-05-28T19:35:21Z | 922,809 | <p>isinstance is an option:</p>
<pre><code>In [2]: isinstance("a", str)
Out[2]: True
In [3]: isinstance([], str)
Out[3]: False
In [4]: isinstance([], list)
Out[4]: True
In [5]: isinstance("", list)
Out[5]: False
</code></pre>
| 2 | 2009-05-28T19:41:12Z | [
"python"
] |
Check if input is a list/tuple of strings or a single string | 922,774 | <p>I've a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strings.</p>
<p>Given that strings act as lists of characters, how do I tell which the method has received?</p>
<p>I'd like to be able to accept eit... | 17 | 2009-05-28T19:35:21Z | 26,797,718 | <p>As I like to keep things simple, here is the shortest form that is compatible with both 2.x and 3.x:</p>
<pre><code># trick for py2/3 compatibility
if 'basestring' not in globals():
basestring = str
v = "xx"
if isinstance(v, basestring):
print("is string")
</code></pre>
| 4 | 2014-11-07T09:10:54Z | [
"python"
] |
Check if input is a list/tuple of strings or a single string | 922,774 | <p>I've a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strings.</p>
<p>Given that strings act as lists of characters, how do I tell which the method has received?</p>
<p>I'd like to be able to accept eit... | 17 | 2009-05-28T19:35:21Z | 40,032,590 | <p>Can't you do:</p>
<pre><code>(i == list (i) or i == tuple (i))
</code></pre>
<p>It would reply if the input is tuple or list. The only issue is that it doesn't work properly with a tuple holding only one variable.</p>
| 0 | 2016-10-13T23:22:36Z | [
"python"
] |
Writing a kernel mode profiler for processes in python | 922,788 | <p>I would like seek some guidance in writing a "process profiler" which runs in kernel mode. I am asking for a kernel mode profiler is because I run loads of applications and I do not want my profiler to be swapped out.</p>
<p>When I said "process profiler" I mean to something that would monitor resource usage by the... | 2 | 2009-05-28T19:37:54Z | 922,814 | <p>It's going to be very difficult to do the process monitoring part in Python, since the python interpreter doesn't run in the kernel.</p>
<p>I suspect there are two easy approaches to this:</p>
<ol>
<li>use the /proc filesystem if you have one (you don't mention your OS)</li>
<li>Use dtrace if you have dtrace (agai... | 7 | 2009-05-28T19:42:20Z | [
"python",
"kernel"
] |
Writing a kernel mode profiler for processes in python | 922,788 | <p>I would like seek some guidance in writing a "process profiler" which runs in kernel mode. I am asking for a kernel mode profiler is because I run loads of applications and I do not want my profiler to be swapped out.</p>
<p>When I said "process profiler" I mean to something that would monitor resource usage by the... | 2 | 2009-05-28T19:37:54Z | 922,875 | <p><strong>don't</strong> try and get python running in kernel space!</p>
<p>You would be much better using an existing tool and getting it to spit out XML that can be sucked into Python. I wouldn't want to port the Python interpreter to kernel-mode (it sounds grim writing it).</p>
<p>The /proc option does sound good... | 3 | 2009-05-28T20:02:14Z | [
"python",
"kernel"
] |
Writing a kernel mode profiler for processes in python | 922,788 | <p>I would like seek some guidance in writing a "process profiler" which runs in kernel mode. I am asking for a kernel mode profiler is because I run loads of applications and I do not want my profiler to be swapped out.</p>
<p>When I said "process profiler" I mean to something that would monitor resource usage by the... | 2 | 2009-05-28T19:37:54Z | 922,904 | <p>have you looked at <code>PSI</code>? (<a href="http://www.psychofx.com/psi/" rel="nofollow">http://www.psychofx.com/psi/</a>)</p>
<p>"PSI is a Python module providing direct access to real-time system and process information. PSI is a Python C extension, providing the most efficient access to system information dir... | 0 | 2009-05-28T20:10:16Z | [
"python",
"kernel"
] |
Writing a kernel mode profiler for processes in python | 922,788 | <p>I would like seek some guidance in writing a "process profiler" which runs in kernel mode. I am asking for a kernel mode profiler is because I run loads of applications and I do not want my profiler to be swapped out.</p>
<p>When I said "process profiler" I mean to something that would monitor resource usage by the... | 2 | 2009-05-28T19:37:54Z | 923,386 | <p>Some of your comments on other answers suggest that you are a relatively inexperienced programmer. Therefore I would strongly suggest that you stay away from kernel programming, as it is very hard even for experienced programmers.</p>
<p>Why would you want to write something that</p>
<ul>
<li>is a very complex sys... | 0 | 2009-05-28T21:50:29Z | [
"python",
"kernel"
] |
SPNEGO (kerberos token generation/validation) for SSO using Python | 922,805 | <p>I'm attempting to implement a simple Single Sign On scenario where some of the participating servers will be windows (IIS) boxes. It looks like SPNEGO is a reasonable path for this.</p>
<p>Here's the scenario:</p>
<ul>
<li>User logs in to my SSO service using his username and password. I authenticate him using som... | 6 | 2009-05-28T19:40:12Z | 1,001,619 | <p>This is exactly what Apple does with its <a href="http://trac.calendarserver.org/" rel="nofollow">Calendar Server</a>. They have a <a href="http://trac.calendarserver.org/browser/PyKerberos" rel="nofollow">python gssapi</a> library for the kerberos part of the process, in order to implement <a href="http://www.ietf.... | 8 | 2009-06-16T13:49:51Z | [
"python",
"active-directory",
"single-sign-on",
"kerberos",
"spnego"
] |
SPNEGO (kerberos token generation/validation) for SSO using Python | 922,805 | <p>I'm attempting to implement a simple Single Sign On scenario where some of the participating servers will be windows (IIS) boxes. It looks like SPNEGO is a reasonable path for this.</p>
<p>Here's the scenario:</p>
<ul>
<li>User logs in to my SSO service using his username and password. I authenticate him using som... | 6 | 2009-05-28T19:40:12Z | 1,674,918 | <p>Take a look at the <a href="http://spnego.sourceforge.net/credential%5Fdelegation.html" rel="nofollow">http://spnego.sourceforge.net/credential_delegation.html</a> tutorial. It seems to be doing what you are trying to do.</p>
| 1 | 2009-11-04T16:19:56Z | [
"python",
"active-directory",
"single-sign-on",
"kerberos",
"spnego"
] |
SPNEGO (kerberos token generation/validation) for SSO using Python | 922,805 | <p>I'm attempting to implement a simple Single Sign On scenario where some of the participating servers will be windows (IIS) boxes. It looks like SPNEGO is a reasonable path for this.</p>
<p>Here's the scenario:</p>
<ul>
<li>User logs in to my SSO service using his username and password. I authenticate him using som... | 6 | 2009-05-28T19:40:12Z | 34,074,210 | <p>I've been searching quite some time for something similar (on Linux), that has lead me to this page several times, yet giving no answer. So here is my solution, I came up with:</p>
<p>The web-server is a Apache with mod_auth_kerb. It is already running in a Active Directory, single sign-on setup since quite some ti... | 0 | 2015-12-03T19:10:44Z | [
"python",
"active-directory",
"single-sign-on",
"kerberos",
"spnego"
] |
NameError using execfile in python | 922,897 | <p>My application has a button to execute a python script dynamically using <em>execfile</em>. If I define a function inside the script (eg. <em>spam()</em>) and try to use that function inside another function (eg. <em>eggs()</em>), I get this error: </p>
<pre><code>NameError: global name 'spam' is not defined
</code... | 2 | 2009-05-28T20:07:09Z | 922,912 | <p>Are you sure you posted the actual code you are having trouble with? The first script works fine for me.</p>
<p>The second error is to be expected: the name "bleh" is not defined in the outer block, only within the namespace of "mainprogram"</p>
| 1 | 2009-05-28T20:13:07Z | [
"python",
"class",
"scope",
"namespaces"
] |
NameError using execfile in python | 922,897 | <p>My application has a button to execute a python script dynamically using <em>execfile</em>. If I define a function inside the script (eg. <em>spam()</em>) and try to use that function inside another function (eg. <em>eggs()</em>), I get this error: </p>
<pre><code>NameError: global name 'spam' is not defined
</code... | 2 | 2009-05-28T20:07:09Z | 925,155 | <p>In the second case you will need <code>import</code> (not sure whether "mainprogram.py"
is on your <code>$PYTHONPATH</code>)</p>
<pre><code>#mainprogram.py
class mainprogram:
def runme(self):
execfile("my2ndscript.py")
def bleh(self):
print "bleh"
if __name__ == '__main__':
mainprogram()... | 2 | 2009-05-29T09:23:23Z | [
"python",
"class",
"scope",
"namespaces"
] |
NameError using execfile in python | 922,897 | <p>My application has a button to execute a python script dynamically using <em>execfile</em>. If I define a function inside the script (eg. <em>spam()</em>) and try to use that function inside another function (eg. <em>eggs()</em>), I get this error: </p>
<pre><code>NameError: global name 'spam' is not defined
</code... | 2 | 2009-05-28T20:07:09Z | 14,722,331 | <p>You're 3 years 8 months wiser since you posted so I'm assuming you'd have figured the first issue out, but given that a solution has not yet been posted (primarily because no one seemed to have a problem with the first issue), the following is my solution.</p>
<h2>[UPDATED]</h2>
<p>The last solution I provided was... | 8 | 2013-02-06T05:38:23Z | [
"python",
"class",
"scope",
"namespaces"
] |
NameError using execfile in python | 922,897 | <p>My application has a button to execute a python script dynamically using <em>execfile</em>. If I define a function inside the script (eg. <em>spam()</em>) and try to use that function inside another function (eg. <em>eggs()</em>), I get this error: </p>
<pre><code>NameError: global name 'spam' is not defined
</code... | 2 | 2009-05-28T20:07:09Z | 20,475,760 | <p>Addy689 explained the real problem: it is when calling execfile() <strong><em>from a function</em></strong> that it appears. execfile() works well from the global space. That's why answers are often 'for me it works'.</p>
<p>But the solution to modify the called scripts may not be possible.
So I report here the so... | 4 | 2013-12-09T16:38:09Z | [
"python",
"class",
"scope",
"namespaces"
] |
How can I capture the stdout output of a child process? | 923,079 | <p>I'm trying to write a program in Python and I'm told to run an .exe file. When this .exe file is run it spits out a lot of data and I need a certain line printed out to the screen. I'm pretty sure I need to use <code>subprocess.popen</code> or something similar but I'm new to subprocess and have no clue. Anyone have... | 7 | 2009-05-28T20:45:45Z | 923,108 | <p>Something like this:</p>
<pre><code>import subprocess
process = subprocess.Popen(["yourcommand"], stdout=subprocess.PIPE)
result = process.communicate()[0]
</code></pre>
| 14 | 2009-05-28T20:49:38Z | [
"python",
"subprocess",
"stdout"
] |
How can I capture the stdout output of a child process? | 923,079 | <p>I'm trying to write a program in Python and I'm told to run an .exe file. When this .exe file is run it spits out a lot of data and I need a certain line printed out to the screen. I'm pretty sure I need to use <code>subprocess.popen</code> or something similar but I'm new to subprocess and have no clue. Anyone have... | 7 | 2009-05-28T20:45:45Z | 923,127 | <p>@Paolo's solution is perfect if you are interested in printing output after the process has finished executing. In case you want to poll output while the process is running you have to do it this way:</p>
<pre><code>process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
while True:
out = process.stdout.readli... | 20 | 2009-05-28T20:52:54Z | [
"python",
"subprocess",
"stdout"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 923,147 | <p>Try this:</p>
<pre><code>>>> cols = 6
>>> rows = 3
>>> a = [[0]*cols for _ in [0]*rows]
>>> a
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
>>> a[0][3] = 2
>>> a
[[0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
</code></pre>
<p>This i... | 8 | 2009-05-28T20:57:09Z | [
"list",
"matrix",
"python"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 923,164 | <p>This will work</p>
<pre><code>col = 2
row = 3
[[0] * col for row in xrange(row)]
</code></pre>
| 3 | 2009-05-28T20:59:40Z | [
"list",
"matrix",
"python"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 923,170 | <p>What about: </p>
<pre><code>m, n = 2, 3
>>> A = [[0]*m for _ in range(n)]
>>> A
[[0, 0], [0, 0], [0, 0]]
>>> A[0][0] = 1
[[1, 0], [0, 0], [0, 0]]
</code></pre>
<p>Aka List comprehension; from the <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" rel="nofol... | 2 | 2009-05-28T21:00:18Z | [
"list",
"matrix",
"python"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 923,171 | <p>Use a <a href="http://docs.python.org/tutorial/datastructures.html?highlight=list%20comprehension#list-comprehensions">list comprehension</a>:</p>
<pre><code>>>> mat = [[0]*2 for x in xrange(3)]
>>> mat[0][0] = 1
>>> mat
[[1, 0], [0, 0], [0, 0]]
</code></pre>
<p>Or, as a function:</p>
<... | 9 | 2009-05-28T21:00:21Z | [
"list",
"matrix",
"python"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 923,177 | <p>I use</p>
<pre><code>mat = [[0 for col in range(3)] for row in range(2)]
</code></pre>
<p>although depending on what you do with the matrix after you create it, you might take a look at using a NumPy array.</p>
| 4 | 2009-05-28T21:03:09Z | [
"list",
"matrix",
"python"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 2,332,744 | <p>See also <a href="http://stackoverflow.com/questions/2173087/how-to-create-nested-lists-in-python">this question</a> for generalization to an n-levels nested list / n-dimensional matrix.</p>
| 1 | 2010-02-25T08:58:31Z | [
"list",
"matrix",
"python"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 2,332,830 | <p>Is there anything itertools can't do? :)</p>
<pre><code>>>> from itertools import repeat,izip
>>> rows=3
>>> cols=6
>>> A=map(list,izip(*[repeat(0,rows*cols)]*cols))
>>> A
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
>>> A[0][3] = 2
>>> ... | 1 | 2010-02-25T09:15:18Z | [
"list",
"matrix",
"python"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 2,332,919 | <p>This one is faster than the accepted answer!<br>
Using xrange(rows) instead of [0]*rows makes no difference.</p>
<pre><code>>>> from itertools import repeat
>>> rows,cols = 3,6
>>> a=[x[:] for x in repeat([0]*cols,rows)]
</code></pre>
<p>A variation that doesn't use itertools and runs ar... | 6 | 2010-02-25T09:29:17Z | [
"list",
"matrix",
"python"
] |
Creating lists of lists in a pythonic way | 923,111 | <p>I'm using a list of lists to store a matrix in python. I tried to initialise a 2x3 Zero matrix as follows.</p>
<pre><code>mat=[[0]*2]*3
</code></pre>
<p>However, when I change the value of one of the items in the matrix, it changes the value of that entry in <em>every</em> row, since the id of each row in <code>ma... | 9 | 2009-05-28T20:50:30Z | 28,047,760 | <p>If the sizes involved are really only 2 and 3,</p>
<pre><code>mat = [[0, 0], [0, 0], [0, 0]]
</code></pre>
<p>is easily best and hasn't been mentioned yet.</p>
| 1 | 2015-01-20T14:26:58Z | [
"list",
"matrix",
"python"
] |
Keeping a session in python while making HTTP requests | 923,296 | <p>I need to write a python script that makes multiple HTTP requests to the same site. Unless I'm wrong (and I may very well be) urllib reauthenticates for every request. For reasons I won't go into I need to be able to authenticate once and then use that session for the rest of my requests. </p>
<p>I'm using pytho... | 23 | 2009-05-28T21:28:06Z | 923,313 | <p>If you want to keep the authentication you need to reuse the cookie. I'm not sure if urllib2 is available in python 2.3.4 but here is an example on how to do it:</p>
<pre><code>req1 = urllib2.Request(url1)
response = urllib2.urlopen(req1)
cookie = response.headers.get('Set-Cookie')
# Use the cookie is subsequent r... | 20 | 2009-05-28T21:32:41Z | [
"python",
"http",
"authentication"
] |
Keeping a session in python while making HTTP requests | 923,296 | <p>I need to write a python script that makes multiple HTTP requests to the same site. Unless I'm wrong (and I may very well be) urllib reauthenticates for every request. For reasons I won't go into I need to be able to authenticate once and then use that session for the rest of my requests. </p>
<p>I'm using pytho... | 23 | 2009-05-28T21:28:06Z | 925,043 | <p>If this is cookie based authentication use <a href="http://docs.python.org/library/cookielib.html#examples">HTTPCookieProcessor</a>:</p>
<pre><code>import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
r = opener.open("http://example.com/")
</code></pre>... | 14 | 2009-05-29T08:48:54Z | [
"python",
"http",
"authentication"
] |
Keeping a session in python while making HTTP requests | 923,296 | <p>I need to write a python script that makes multiple HTTP requests to the same site. Unless I'm wrong (and I may very well be) urllib reauthenticates for every request. For reasons I won't go into I need to be able to authenticate once and then use that session for the rest of my requests. </p>
<p>I'm using pytho... | 23 | 2009-05-28T21:28:06Z | 10,631,831 | <p>Use <a href="https://github.com/kennethreitz/requests">Requests</a> library. From <a href="http://docs.python-requests.org/en/latest/user/advanced/#session-objects">http://docs.python-requests.org/en/latest/user/advanced/#session-objects</a> :</p>
<blockquote>
<p>The Session object allows you to persist certain p... | 23 | 2012-05-17T07:53:27Z | [
"python",
"http",
"authentication"
] |
Scraping Multiple html files to CSV | 923,318 | <p>I am trying to scrape rows off of over 1200 .htm files that are on my hard drive. On my computer they are here 'file:///home/phi/Data/NHL/pl07-08/PL020001.HTM'. These .htm files are sequential from *20001.htm until *21230.htm. My plan is to eventually toss my data in MySQL or SQLite via a spreadsheet app or just str... | 2 | 2009-05-28T21:34:57Z | 923,373 | <p>You won't need mechanize. Since I do not exactly know the HTML content, I'd try to see what matches, first. Like this: </p>
<pre><code>import glob
from BeautifulSoup import BeautifulSoup
for filename in glob.glob('/home/phi/Data/*.htm'):
soup = BeautifulSoup(open(filename, "r").read()) # assuming some HTML
... | 1 | 2009-05-28T21:45:59Z | [
"python",
"sqlite",
"screen-scraping",
"beautifulsoup",
"mechanize"
] |
Scraping Multiple html files to CSV | 923,318 | <p>I am trying to scrape rows off of over 1200 .htm files that are on my hard drive. On my computer they are here 'file:///home/phi/Data/NHL/pl07-08/PL020001.HTM'. These .htm files are sequential from *20001.htm until *21230.htm. My plan is to eventually toss my data in MySQL or SQLite via a spreadsheet app or just str... | 2 | 2009-05-28T21:34:57Z | 923,460 | <p>MYYN's answer looks like a great start to me. One thing I'd point out that I've had luck with is:</p>
<pre><code>import glob
for file_name in glob.glob('/home/phi/Data/*.htm'):
#read the file and then parse with BeautifulSoup
</code></pre>
<p>I've found both the <code>os</code> and <code>glob</code> i... | 0 | 2009-05-28T22:11:11Z | [
"python",
"sqlite",
"screen-scraping",
"beautifulsoup",
"mechanize"
] |
Matching text within P tags in HTML | 923,472 | <p>I'd like to match the contents within each paragraph in html using a python regular expression. These paragraphs always have BR tags inside them like so:</p>
<pre><code><p class="thisClass">this is nice <br /><br /> isn't it?</p>
</code></pre>
<p>I'm currently using this pattern:</p>
<pre>... | 2 | 2009-05-28T22:16:13Z | 923,494 | <p>I don't think it is failing because of the <code><br/></code> but rather because the paragraph is spread across multiple lines. Use the <a href="http://docs.python.org/library/re.html#re.DOTALL" rel="nofollow">DOTALL</a> mode to fix this:</p>
<pre><code>pattern = re.compile('<p class=\"thisClass\">(.*?)... | 5 | 2009-05-28T22:22:03Z | [
"python",
"html",
"regex"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.