title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags list |
|---|---|---|---|---|---|---|---|---|---|
How can I pack several decorators into one? | 2,182,858 | <p>I have several decorators on each function, is there a way to pack them in to one instead?</p>
<pre><code>@fun1
@fun2
@fun3
def do_stuf():
pass
</code></pre>
<p>change to: </p>
<pre><code>@all_funs #runs fun1 fun2 and fun3, how should all_funs look like?
def do_stuf():
pass
</code></pre>
| 10 | 2010-02-02T09:04:37Z | 2,182,876 | <p>A decorator is in principle only syntactic sugar for this:</p>
<pre><code>def do_stuf():
pass
do_stuf = fun1(do_stuf)
</code></pre>
<p>So in your all_fun, all you should need to do is to wrap the function in the same kind of chain of decorators:</p>
<pre><code>def all_funs(funky):
return fun1(fun2(fun3(f... | 11 | 2010-02-02T09:08:10Z | [
"python",
"decorator"
] |
How can I pack several decorators into one? | 2,182,858 | <p>I have several decorators on each function, is there a way to pack them in to one instead?</p>
<pre><code>@fun1
@fun2
@fun3
def do_stuf():
pass
</code></pre>
<p>change to: </p>
<pre><code>@all_funs #runs fun1 fun2 and fun3, how should all_funs look like?
def do_stuf():
pass
</code></pre>
| 10 | 2010-02-02T09:04:37Z | 2,183,445 | <p>It's also possible to write a generic decorator that supports decorator chaining:</p>
<pre><code>def fun1(f):
print "fun1"
return f
def fun2(f):
print "fun2"
return f
def fun3(f):
print "fun3"
return f
def chained(*dec_funs):
def _inner_chain(f):
for dec in reversed(dec_funs):... | 7 | 2010-02-02T11:02:09Z | [
"python",
"decorator"
] |
Django template-printing variables | 2,182,977 | <p>All,</p>
<p>In Template condition check,whats wrong with the following code,</p>
<p>selected_id and selected_sub are equal to 5 but still ifequal loop is not working..</p>
<pre><code><tr><td><p>Subjects:</td>
<td>
<select id="subjects" name="subjects" multiple="multiple">
{% fo... | 1 | 2010-02-02T09:30:18Z | 2,183,080 | <p>If, as you say, <code>selected_id</code> and <code>selected_sub</code> are equal to 5 then</p>
<pre><code>for selected_id in selected_sub
</code></pre>
<p>will not work, since 5 is not iterable.</p>
| 2 | 2010-02-02T09:49:19Z | [
"python",
"django"
] |
Django template-printing variables | 2,182,977 | <p>All,</p>
<p>In Template condition check,whats wrong with the following code,</p>
<p>selected_id and selected_sub are equal to 5 but still ifequal loop is not working..</p>
<pre><code><tr><td><p>Subjects:</td>
<td>
<select id="subjects" name="subjects" multiple="multiple">
{% fo... | 1 | 2010-02-02T09:30:18Z | 2,183,097 | <p>Ok, sorry. What is the output you get?</p>
<p><code>selected</code> should have the value <code>'selected="selected"'</code>.</p>
<p>Change to <code>{% with selected as 'selected="selected"' %}</code> and try again.</p>
| 2 | 2010-02-02T09:52:09Z | [
"python",
"django"
] |
Hide filter items that produce zero results in django-filter | 2,183,008 | <p>I have an issue with the django-filter application: how to hide the items that will produce zero results. I think that there is a simple method to do this, but idk how.</p>
<p>I'm using the LinkWidget on a ModelChoiceFilter, like this:</p>
<pre><code>provider = django_filters.ModelChoiceFilter(queryset=Provider.ob... | 4 | 2010-02-02T09:36:46Z | 2,183,535 | <p>Maybe the queryset can be a callable instead of a 'real' queryset object. This way, it can be generated dynamically. At least this works in Django Models for references to other models.</p>
<p>The callable can be a class method in you Model.</p>
| 0 | 2010-02-02T11:17:10Z | [
"python",
"django",
"filter",
"django-filter"
] |
Hide filter items that produce zero results in django-filter | 2,183,008 | <p>I have an issue with the django-filter application: how to hide the items that will produce zero results. I think that there is a simple method to do this, but idk how.</p>
<p>I'm using the LinkWidget on a ModelChoiceFilter, like this:</p>
<pre><code>provider = django_filters.ModelChoiceFilter(queryset=Provider.ob... | 4 | 2010-02-02T09:36:46Z | 2,196,293 | <p>Basically, you need to apply filters, and then apply them again, but on newly-generated queryset. Something like this:</p>
<pre><code>f = SomeFilter(request.GET)
f = SomeFilter(request.GET, queryset=f.qs)
</code></pre>
<p>Now when you have correct queryset, you can override providers dynamically in <strong>init</... | 3 | 2010-02-03T23:23:08Z | [
"python",
"django",
"filter",
"django-filter"
] |
Hide filter items that produce zero results in django-filter | 2,183,008 | <p>I have an issue with the django-filter application: how to hide the items that will produce zero results. I think that there is a simple method to do this, but idk how.</p>
<p>I'm using the LinkWidget on a ModelChoiceFilter, like this:</p>
<pre><code>provider = django_filters.ModelChoiceFilter(queryset=Provider.ob... | 4 | 2010-02-02T09:36:46Z | 2,433,820 | <p>If I understand your question correctly I believe you want to use the AllValuesFilter.</p>
<pre><code>import django_tables
provider = django_filters.AllValuesFilter(
widget=django_filters.widgets.LinkWidget)
</code></pre>
<p>More information is available here: <a href="http://github.com/alex/django-filter/blo... | 0 | 2010-03-12T15:41:35Z | [
"python",
"django",
"filter",
"django-filter"
] |
importing a module in nested packages | 2,183,205 | <p>This is a python newbie question: </p>
<p>I have the following directory structure:</p>
<pre><code>test
-- test_file.py
a
-- b
-- module.py
</code></pre>
<p>where <code>test</code>, <code>a</code> and <code>b</code> are folders. Both <code>test</code> and <code>a</code> are on the same level.</p>
<p>mod... | 12 | 2010-02-02T10:11:31Z | 2,183,321 | <p>The first thing to do would be to quickly browse <a href="http://docs.python.org/tutorial/modules.html">the official docs on this</a>. </p>
<p>To make a directory a package, you'll have to add a <code>__init__.py</code> file. This means that you'll have such a file in the <code>a</code> and <code>b</code> directori... | 6 | 2010-02-02T10:33:41Z | [
"python",
"import",
"package"
] |
importing a module in nested packages | 2,183,205 | <p>This is a python newbie question: </p>
<p>I have the following directory structure:</p>
<pre><code>test
-- test_file.py
a
-- b
-- module.py
</code></pre>
<p>where <code>test</code>, <code>a</code> and <code>b</code> are folders. Both <code>test</code> and <code>a</code> are on the same level.</p>
<p>mod... | 12 | 2010-02-02T10:11:31Z | 2,183,325 | <p>What you want is a relative import like:</p>
<p><code>from ..a.b import module</code></p>
<p>The problem with this is that it doesn't work if you are calling test_file.py as your main module. As stated <a href="http://docs.python.org/tutorial/modules.html">here</a>:</p>
<blockquote>
<p>Note that both explicit a... | 9 | 2010-02-02T10:34:09Z | [
"python",
"import",
"package"
] |
importing a module in nested packages | 2,183,205 | <p>This is a python newbie question: </p>
<p>I have the following directory structure:</p>
<pre><code>test
-- test_file.py
a
-- b
-- module.py
</code></pre>
<p>where <code>test</code>, <code>a</code> and <code>b</code> are folders. Both <code>test</code> and <code>a</code> are on the same level.</p>
<p>mod... | 12 | 2010-02-02T10:11:31Z | 2,183,484 | <ol>
<li><p>The directory <code>a</code> needs to be a package. Add an <code>__init__.py</code> file to make it a package, which is a step up from being a simple directory.</p></li>
<li><p>The directory <code>b</code> also needs to be a subpackage of <code>a</code>. Add an <code>__init__.py</code> file.</p></li>
<li>... | 8 | 2010-02-02T11:10:02Z | [
"python",
"import",
"package"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 2,183,293 | <p>I think you'll have to subclass the <code>Logger</code> class and add a method called <code>trace</code> which basically calls <code>Logger.log</code> with a level lower than <code>DEBUG</code>. I haven't tried this but this is what the <a href="http://docs.python.org/library/logging.html#loggers">docs indicate</a>.... | 7 | 2010-02-02T10:27:50Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 2,819,062 | <p>I find it easier to create a new attribute for the logger object that passes the log() function. I think the logger module provides the addLevelName() and the log() for this very reason. Thus no subclasses or new method needed. </p>
<pre><code>import logging
@property
def log(obj):
logging.addLevelName(5, 'TRA... | 8 | 2010-05-12T13:13:41Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 7,510,137 | <p>In my experience, this is the full solution the the op's problem... to avoid seeing "lambda" as the function in which the message is emitted, go deeper:</p>
<pre><code>MY_LEVEL_NUM = 25
logging.addLevelName(MY_LEVEL_NUM, "MY_LEVEL_NAME")
def log_at_my_log_level(self, message, *args, **kws):
# Yes, logger takes ... | 2 | 2011-09-22T05:55:14Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 11,784,984 | <p>I took the "avoid seeing lambda" answer and had to modify where the log_at_my_log_level was being added. I too saw the problem that Paul did "I don't think this works. Don't you need logger as the first arg in log_at_my_log_level?" This worked for me</p>
<pre><code>import logging
DEBUG_LEVELV_NUM = 9
logging.add... | 40 | 2012-08-02T20:08:51Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 12,049,377 | <p>This worked for me:</p>
<pre><code>import logging
logging.basicConfig(
format=' %(levelname)-8.8s %(funcName)s: %(message)s',
)
logging.NOTE = 32 # positive yet important
logging.addLevelName(logging.NOTE, 'NOTE') # new level
logging.addLevelName(logging.CRITICAL, 'FATAL') # rename existing
log = loggin... | 1 | 2012-08-21T06:24:39Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 13,638,084 | <p>@Eric S.</p>
<p>Eric S.'s answer is excellent, but I learned by experimentation that this will always cause messages logged at the new debug level to be printed -- regardless of what the log level is set to. So if you make a new level number of 9, if you call setLevel(50), the lower level messages will erroneously ... | 76 | 2012-11-30T02:17:06Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 16,955,098 | <p>Who started the bad practice of using internal methods (<code>self._log</code>) and why is each answer based on that?! The pythonic solution would be to use <code>self.log</code> instead so you don't have to mess with any internal stuff:</p>
<pre><code>import logging
SUBDEBUG = 5
logging.addLevelName(SUBDEBUG, 'SU... | 14 | 2013-06-06T06:27:25Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 19,683,896 | <p>As alternative to adding an extra method to the Logger class I would recommend using the <code>Logger.log(level, msg)</code> method.</p>
<pre><code>import logging
TRACE = 5
logging.addLevelName(TRACE, 'TRACE')
FORMAT = '%(levelname)s:%(name)s:%(lineno)d:%(message)s'
logging.basicConfig(format=FORMAT)
l = logging... | 0 | 2013-10-30T13:40:33Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 22,586,200 | <p>This question is rather old, but I just dealt with the same topic and found a way similiar to those already mentioned which appears a little cleaner to me. This was tested on 3.4, so I'm not sure whether the methods used exist in older versions:</p>
<pre><code>from logging import getLoggerClass, addLevelName, setLo... | 13 | 2014-03-23T02:07:19Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 35,804,945 | <p>Combining all of the existing answers with a bunch of usage experience, I think that I have come up with a list of all the things that need to be done to ensure completely seamless usage of the new level. The steps below assume that you are adding a new level <code>TRACE</code> with value <code>logging.DEBUG - 5 == ... | 0 | 2016-03-04T19:54:20Z | [
"python",
"logging"
] |
How to add a custom loglevel to Python's logging facility | 2,183,233 | <p>I'd like to have loglevel TRACE (5) for my application, as I don't think that <code>debug()</code> is sufficient. Additionally <code>log(5, msg)</code> isn't what I want. How can I add a custom loglevel to a Python logger?</p>
<p>I've a <code>mylogger.py</code> with the following content:</p>
<pre><code>import log... | 42 | 2010-02-02T10:16:03Z | 39,007,427 | <p>Tips for creating a custom logger:</p>
<ol>
<li>Do not use <code>_log</code>, use <code>log</code> (you don't have to check <code>isEnabledFor</code>)</li>
<li>the logging module should be the one creating instance of the custom logger since it does some magic in <code>getLogger</code>, so you will need to set the ... | 0 | 2016-08-17T22:51:09Z | [
"python",
"logging"
] |
python regular expression to validate types of strings | 2,183,786 | <p>I want to do the following with python:</p>
<ol>
<li>Validate if a UTF8 string is an integer.</li>
<li>Validate if a UTF8 string is a float.</li>
<li>Validate if a UTF8 string is of length(1-255).</li>
<li>Validate if a UTF8 string is a valid date.</li>
</ol>
<p>I'm totally new to python and I believe this should ... | 1 | 2010-02-02T12:00:51Z | 2,183,799 | <ol>
<li>int() and check for exceptions</li>
<li>float() - but what do you mean float? </li>
<li>int() and then check using if</li>
<li>using datetime formatting </li>
</ol>
| 1 | 2010-02-02T12:02:50Z | [
"python",
"regex",
"validation"
] |
python regular expression to validate types of strings | 2,183,786 | <p>I want to do the following with python:</p>
<ol>
<li>Validate if a UTF8 string is an integer.</li>
<li>Validate if a UTF8 string is a float.</li>
<li>Validate if a UTF8 string is of length(1-255).</li>
<li>Validate if a UTF8 string is a valid date.</li>
</ol>
<p>I'm totally new to python and I believe this should ... | 1 | 2010-02-02T12:00:51Z | 2,183,830 | <p>Why use regex? I'm convinced it would be slower and more cumbersome. </p>
<p>The <code>int()</code> and <code>float()</code> method or better yet the <code>isdigit()</code> method work well here.</p>
<pre><code>a = "03523"
a.isdigit()
>>> True
b = "963spam"
b.isdigit()
>>> False
</code></pre>
<... | 2 | 2010-02-02T12:09:45Z | [
"python",
"regex",
"validation"
] |
python regular expression to validate types of strings | 2,183,786 | <p>I want to do the following with python:</p>
<ol>
<li>Validate if a UTF8 string is an integer.</li>
<li>Validate if a UTF8 string is a float.</li>
<li>Validate if a UTF8 string is of length(1-255).</li>
<li>Validate if a UTF8 string is a valid date.</li>
</ol>
<p>I'm totally new to python and I believe this should ... | 1 | 2010-02-02T12:00:51Z | 2,183,847 | <p>Regex is not a good solution here.</p>
<ol>
<li><p>Validate if a UTF8 string is an integer:</p>
<pre><code>try:
int(val)
is_int = True
except ValueError:
is_int = False
</code></pre></li>
<li><p>Validate if a UTF8 string is a float: same as above, but with <code>float()</code>.</p></li>
<li><p>Validate if a ... | 6 | 2010-02-02T12:12:11Z | [
"python",
"regex",
"validation"
] |
Why am I leaking memory with this python loop? | 2,184,063 | <p>I am writing a custom file system crawler, which gets passed millions of globs to process through sys.stdin. I'm finding that when running the script, its memory usage increases massively over time and the whole thing crawls practically to a halt. I've written a minimal case below which shows the problem. Am I doing... | 11 | 2010-02-02T12:46:57Z | 2,184,120 | <p>I cannot reproduce any actual leak on my system, but I think your "every 100th iteration, 100 objects are freed" is you hitting the cache for compiled regular expressions (via the glob module). If you peek at re.py you'll see <code>_MAXCACHE</code> defaults to 100, and by default the entire cache is blown away once ... | 2 | 2010-02-02T12:55:47Z | [
"python",
"memory",
"memory-leaks",
"glob"
] |
Why am I leaking memory with this python loop? | 2,184,063 | <p>I am writing a custom file system crawler, which gets passed millions of globs to process through sys.stdin. I'm finding that when running the script, its memory usage increases massively over time and the whole thing crawls practically to a halt. I've written a minimal case below which shows the problem. Am I doing... | 11 | 2010-02-02T12:46:57Z | 2,193,286 | <p>I tracked this down to the fnmatch module. glob.glob calls fnmatch to actually perform the globbing, and fnmatch has a cache of regular expressions which is never cleared. So in this usage, the cache was growing continuously and unchecked. I've filed a bug against the fnmatch library [1].</p>
<p>[1]: <a href="http:... | 5 | 2010-02-03T16:01:09Z | [
"python",
"memory",
"memory-leaks",
"glob"
] |
Populate ChoiceField from database | 2,184,108 | <p>I'd like to have several fields in my form being rendered as ChoiceFields which get their content from the database.</p>
<p>I was thinking something like:</p>
<pre><code>class SeriesForm(ModelForm):
series = forms.ChoiceField(choices=Series.objects.all())
class Meta:
model = Series
exclude = ('model',... | 4 | 2010-02-02T12:53:27Z | 2,184,118 | <p>Use a <a href="http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.ModelChoiceField" rel="nofollow"><code>ModelChoiceField</code></a> instead.</p>
| 3 | 2010-02-02T12:55:48Z | [
"python",
"django",
"django-forms"
] |
Python database access using single file/already build file | 2,184,156 | <p>I have to save some data into MySQL from Python.</p>
<p>I have tried <a href="http://mysql-python.sourceforge.net/MySQLdb.html" rel="nofollow">MySQLdb</a>, but it needs to be built. Build fails on my Mac; moreover, I need to have one file to copy to server. I don't have access to install anything.</p>
<p>Can you r... | 2 | 2010-02-02T13:00:50Z | 2,184,206 | <p>I find the easiest way to use C extensions from python on a Mac is to use <a href="http://www.macports.org/" rel="nofollow">Macports</a></p>
<p>Yo will need to install macports and then install the port py26-mysql this will install python 2.6 and MySQL.</p>
<p>However in this case I would have thought MySQLdb sho... | 1 | 2010-02-02T13:08:12Z | [
"python",
"mysql",
"database"
] |
Python database access using single file/already build file | 2,184,156 | <p>I have to save some data into MySQL from Python.</p>
<p>I have tried <a href="http://mysql-python.sourceforge.net/MySQLdb.html" rel="nofollow">MySQLdb</a>, but it needs to be built. Build fails on my Mac; moreover, I need to have one file to copy to server. I don't have access to install anything.</p>
<p>Can you r... | 2 | 2010-02-02T13:00:50Z | 2,184,755 | <p>MySQL has a <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql.html" rel="nofollow">command line interface</a> (similar to <code>psql</code> from Postgres or <code>sqlite3</code> from the database of the same name). I would be surprised if this wasn't installed on the same server already.</p>
<p>You could then ... | 2 | 2010-02-02T14:34:01Z | [
"python",
"mysql",
"database"
] |
Decoding tcp packets using python | 2,184,181 | <p>I am trying to decode data received over a tcp connection. The packets are small, no more than 100 bytes. However when there is a lot of them I receive some of the the packets joined together. Is there a way to prevent this. I am using python</p>
<p>I have tried to separate the packets, my source is below. The pack... | 1 | 2010-02-02T13:04:32Z | 2,184,341 | <p>TCP provides a data stream, not individual packets, at the interface level. If you want discrete packets, you can use UDP (and handle lost or out of order packets on your own), or put some data separator inline. It sounds like you are doing that already, with STX/ETX as your separators. However, as you note, you ... | 4 | 2010-02-02T13:33:40Z | [
"python",
"string",
"tcp",
"decoding",
"packets"
] |
Decoding tcp packets using python | 2,184,181 | <p>I am trying to decode data received over a tcp connection. The packets are small, no more than 100 bytes. However when there is a lot of them I receive some of the the packets joined together. Is there a way to prevent this. I am using python</p>
<p>I have tried to separate the packets, my source is below. The pack... | 1 | 2010-02-02T13:04:32Z | 2,184,466 | <p>Where does the data come from ? Instead of trying to decode it by hand, why not use the excellent Impacket package:</p>
<p><a href="http://oss.coresecurity.com/projects/impacket.html" rel="nofollow">http://oss.coresecurity.com/projects/impacket.html</a></p>
| 0 | 2010-02-02T13:53:34Z | [
"python",
"string",
"tcp",
"decoding",
"packets"
] |
Decoding tcp packets using python | 2,184,181 | <p>I am trying to decode data received over a tcp connection. The packets are small, no more than 100 bytes. However when there is a lot of them I receive some of the the packets joined together. Is there a way to prevent this. I am using python</p>
<p>I have tried to separate the packets, my source is below. The pack... | 1 | 2010-02-02T13:04:32Z | 2,184,519 | <p>Try <a href="http://www.secdev.org/projects/scapy/" rel="nofollow">scapy</a>, a powerful interactive packet manipulation program.</p>
| 3 | 2010-02-02T14:02:00Z | [
"python",
"string",
"tcp",
"decoding",
"packets"
] |
Decoding tcp packets using python | 2,184,181 | <p>I am trying to decode data received over a tcp connection. The packets are small, no more than 100 bytes. However when there is a lot of them I receive some of the the packets joined together. Is there a way to prevent this. I am using python</p>
<p>I have tried to separate the packets, my source is below. The pack... | 1 | 2010-02-02T13:04:32Z | 2,185,373 | <p>I would create a class that is responsible for decoding the packets from a stream, like this:</p>
<pre><code>class PacketDecoder(object):
STX = ...
ETX = ...
def __init__(self):
self._stream = ''
def feed(self, buffer):
self._stream += buffer
def decode(self):
'''
... | 5 | 2010-02-02T15:51:33Z | [
"python",
"string",
"tcp",
"decoding",
"packets"
] |
Decoding tcp packets using python | 2,184,181 | <p>I am trying to decode data received over a tcp connection. The packets are small, no more than 100 bytes. However when there is a lot of them I receive some of the the packets joined together. Is there a way to prevent this. I am using python</p>
<p>I have tried to separate the packets, my source is below. The pack... | 1 | 2010-02-02T13:04:32Z | 2,615,193 | <p>Nice and simple... <code>:)</code>
The trick is in the <a href="http://docs.python.org/library/stdtypes.html#file-objects" rel="nofollow">file</a> object.</p>
<pre><code>f=sock.makefile()
while True:
STX = f.read(1)
pktlen = f.read(1)
wholePacket = STX + pktlen + f.read(ord(pktlen)-2)
doSomethingWithPacket... | 0 | 2010-04-10T22:10:01Z | [
"python",
"string",
"tcp",
"decoding",
"packets"
] |
EventHandler, event, delegate based programming in Python any example would appreciate? | 2,184,263 | <p>Basically I'm a C# developer,
I know the way C# does, EventHandler, delegate, even...</p>
<p>but whats the best way to implement it on Python.</p>
| 10 | 2010-02-02T13:19:07Z | 2,184,282 | <p>I think you should be able to use a function:</p>
<pre><code>def do_work_and_notify(on_done):
// do work
on_done()
def send_email_on_completion():
email_send('joe@example.com', 'you are done')
do_work_and_notify(send_email_on_completion)
</code></pre>
<p>Functions (and even methods) in python are fir... | 15 | 2010-02-02T13:23:25Z | [
"python",
"events",
"delegates",
"function-pointers",
"event-handling"
] |
EventHandler, event, delegate based programming in Python any example would appreciate? | 2,184,263 | <p>Basically I'm a C# developer,
I know the way C# does, EventHandler, delegate, even...</p>
<p>but whats the best way to implement it on Python.</p>
| 10 | 2010-02-02T13:19:07Z | 2,184,468 | <p>This question is a lot like <a href="http://stackoverflow.com/questions/1904351/python-observer-pattern-examples-tips">Python Observer Pattern: Examples, Tips?</a> which has lots of great answers. There's even an implementation of C#-like events in Python.</p>
| 2 | 2010-02-02T13:54:16Z | [
"python",
"events",
"delegates",
"function-pointers",
"event-handling"
] |
How to receive uploaded file from the inlined form in django? | 2,184,284 | <p>I'm trying to create a "cool" image uploading interface in django admin panel. The problem is that I'm using inline interface for uploading image instances, but I'm using jQuery Ajax form to upload images, so there were another form I need to create for uploading. And now django view function can't receive <strong><... | 6 | 2010-02-02T13:23:29Z | 2,260,545 | <p>I tested this code, and files are uploaded just fine. Can you show the jquery that you're using to construct your form with?</p>
<p>Thanks.</p>
| 0 | 2010-02-14T08:43:17Z | [
"python",
"django",
"forms",
"image",
"upload"
] |
How to receive uploaded file from the inlined form in django? | 2,184,284 | <p>I'm trying to create a "cool" image uploading interface in django admin panel. The problem is that I'm using inline interface for uploading image instances, but I'm using jQuery Ajax form to upload images, so there were another form I need to create for uploading. And now django view function can't receive <strong><... | 6 | 2010-02-02T13:23:29Z | 3,456,111 | <p>I suggest using a template fragment which generates the form , but which also can be generated seperately and sent to the jquery. this preservers CSRF tokens and allows tou to parse the file with forms correctly</p>
<p>for instance you would use:</p>
<pre><code>$.ajax({
url: "/uploadPicture?JustForm",
data... | 0 | 2010-08-11T07:13:56Z | [
"python",
"django",
"forms",
"image",
"upload"
] |
How to receive uploaded file from the inlined form in django? | 2,184,284 | <p>I'm trying to create a "cool" image uploading interface in django admin panel. The problem is that I'm using inline interface for uploading image instances, but I'm using jQuery Ajax form to upload images, so there were another form I need to create for uploading. And now django view function can't receive <strong><... | 6 | 2010-02-02T13:23:29Z | 3,925,169 | <p>This tutorial can help.</p>
<p><a href="http://embrangler.com/2010/08/ajax-uploads-images-in-django/" rel="nofollow">http://embrangler.com/2010/08/ajax-uploads-images-in-django/</a></p>
| 0 | 2010-10-13T15:11:58Z | [
"python",
"django",
"forms",
"image",
"upload"
] |
How to receive uploaded file from the inlined form in django? | 2,184,284 | <p>I'm trying to create a "cool" image uploading interface in django admin panel. The problem is that I'm using inline interface for uploading image instances, but I'm using jQuery Ajax form to upload images, so there were another form I need to create for uploading. And now django view function can't receive <strong><... | 6 | 2010-02-02T13:23:29Z | 4,665,739 | <p>request.FILES should work fine with that handmade form. Django doesn't do anything different with the field except adding the id="id_file". I'm fairly certain that your not having an id wouldn't interfere with the transference into request.FILES.</p>
<pre><code>In [5]: from django import forms
In [6]: class Gene... | 2 | 2011-01-12T05:53:48Z | [
"python",
"django",
"forms",
"image",
"upload"
] |
Python Leave Loop Early | 2,184,287 | <p>How do I leave a loop early in python?</p>
<pre><code>for a in b:
if criteria in list1:
print "oh no"
#Force loop i.e. force next iteration without going on
someList.append(a)
</code></pre>
<p>Also, in java you can <code>break</code> out of a loop, is there an equivalent in Python?</p>
| 18 | 2010-02-02T13:23:46Z | 2,184,291 | <p><code>continue</code> and <code>break</code> is what you want. Python works identically to Java/C++ in this regard.</p>
| 30 | 2010-02-02T13:25:04Z | [
"python",
"loops"
] |
Python Leave Loop Early | 2,184,287 | <p>How do I leave a loop early in python?</p>
<pre><code>for a in b:
if criteria in list1:
print "oh no"
#Force loop i.e. force next iteration without going on
someList.append(a)
</code></pre>
<p>Also, in java you can <code>break</code> out of a loop, is there an equivalent in Python?</p>
| 18 | 2010-02-02T13:23:46Z | 2,184,292 | <p>Take a look at <a href="http://docs.python.org/reference/simple_stmts.html#break" rel="nofollow"><code>break</code></a> and <code>continue</code>.</p>
| 2 | 2010-02-02T13:25:06Z | [
"python",
"loops"
] |
Python Leave Loop Early | 2,184,287 | <p>How do I leave a loop early in python?</p>
<pre><code>for a in b:
if criteria in list1:
print "oh no"
#Force loop i.e. force next iteration without going on
someList.append(a)
</code></pre>
<p>Also, in java you can <code>break</code> out of a loop, is there an equivalent in Python?</p>
| 18 | 2010-02-02T13:23:46Z | 2,184,298 | <p>Firstly, bear in mind it might be possible to do what you want with a list comprehension. So you might be able to use something like:</p>
<pre><code>somelist = [a for a in b if not a.criteria in otherlist]
</code></pre>
<p>If you want to leave a loop early in Python you can use <code>break</code>, just like in Ja... | 11 | 2010-02-02T13:25:58Z | [
"python",
"loops"
] |
Python Leave Loop Early | 2,184,287 | <p>How do I leave a loop early in python?</p>
<pre><code>for a in b:
if criteria in list1:
print "oh no"
#Force loop i.e. force next iteration without going on
someList.append(a)
</code></pre>
<p>Also, in java you can <code>break</code> out of a loop, is there an equivalent in Python?</p>
| 18 | 2010-02-02T13:23:46Z | 2,184,307 | <p><code>continue</code> and <code>break</code> work exactly like in other programming languages, except that you cannot <code>break</code> to a label (as you can in Java, for example). That means you can only break <em>one</em> loop at a time.</p>
| 3 | 2010-02-02T13:28:27Z | [
"python",
"loops"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,184,356 | <p>If the objects are hashable then you can use a dict to store the counts:</p>
<pre><code>results = {}
for item in somelist:
if item not in results:
results[item] = 1
else
results[item] += 1
print max(results.iteritems(), key=operator.itemgetter(1))
</code></pre>
| 5 | 2010-02-02T13:36:34Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,184,359 | <p>The best time you can hope to achieve here is O(n) - you'll always need to walk the entire array at least once. The <em>easiest</em> way will certainly be to build a histogram. If your dictionary structure (map of some kind) offers O(1) insert and retrieve then this is as easy as (groovy-ish pseudocode):</p>
<pre... | 1 | 2010-02-02T13:37:14Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,184,369 | <p>Keep a dictionary of how often each object appears.</p>
<p>Walk through the list once, building this table. As you go, keep track of which object has appeared the most often so far.</p>
<p>This code is untested.</p>
<pre><code>from collections import defaultdict
def mode(objects):
h = defaultdict(int)
ma... | 3 | 2010-02-02T13:38:23Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,184,375 | <p>You want an efficient method. Clearly it's possible in O(n) time, so any method that requires sorting the list is out as that would be O(n log(n)). It's not possible to do it faster than O(n) because even if you check the first n/2-1 elements, and they are all "apple", you don't know that the rest of the elements wo... | 2 | 2010-02-02T13:39:40Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,184,417 | <pre><code>def count_reps(item, agg):
k = hash(item)
try:
agg[k] += 1
except KeyError:
agg[k] = 1
return agg
item_dict = reduce(your_array, {})
</code></pre>
<p>item_dict will contain the counts, then you can rate the popularity of each object.</p>
| 0 | 2010-02-02T13:46:02Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,184,652 | <p>Heres a different approach which essentially sorts the list and then processes it in a sorted order.</p>
<pre><code>fruits = ['apple', 'orange', 'apple', 'banana', 'banana', 'orange', 'apple', 'apple']
max_fruit_count = 0
max_fruit = ''
current_fruit_count = 0
current_fruit = ''
for fruit in sorted(fruits) :
i... | 0 | 2010-02-02T14:21:54Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,184,824 | <p>This is not O(n), but O(n^2), so it not may fit your bill as "most efficient way", but it's compact and avoids <code>for</code> loops, which are rather slow in Python. It will be faster than the O(n) option up to 11 unique items.</p>
<pre><code>def most_common(items):
s = set(items)
return max([(items.count... | 0 | 2010-02-02T14:42:36Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,185,297 | <p>Don't reinvent the wheel. In Python 2.7+ you can use <a href="http://docs.python.org/dev/library/collections.html#collections.Counter" rel="nofollow">the Counter class</a>:</p>
<pre><code>import collections
fruit=['apple', 'orange', 'apple', 'banana', 'banana', 'orange', 'apple', 'apple']
c=collections.Counter(frui... | 8 | 2010-02-02T15:41:05Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
How to get the most represented object from an array | 2,184,336 | <p>I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple]</p>
<p>What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate... | 3 | 2010-02-02T13:33:04Z | 2,185,505 | <p>As ~unutbu says: use collections.Counter
Failing that, time your code. Here is my (likely inefficient) approach:</p>
<pre><code>python -m timeit -s "fruit = ['apple']*4 + ['banana'] + ['orange']*2" \
"kL = set(fruit); L = [fruit.count(f) for f in kL]; D = dict(zip(kL,L)); \
sorted(D,key = lambda k: D[k],reverse... | 0 | 2010-02-02T16:05:18Z | [
"python",
"algorithm",
"arrays",
"performance",
"list"
] |
Python thinks a 3000-line text file is one line long? | 2,184,543 | <p>I have a very long text file that I'm trying to process using Python.</p>
<p>However, the following code:</p>
<pre><code>for line in open('textbase.txt', 'r'):
print 'hello world'
</code></pre>
<p>produces only the following output:</p>
<pre><code>hello world
</code></pre>
<p>It's as though Python thinks th... | 11 | 2010-02-02T14:05:34Z | 2,184,566 | <p>You'll probably find it's the "with CR line terminators" that gives the game away. If you're working on a platform that uses newlines as line terminators, it <em>will</em> see your file as one big honkin' line.</p>
<p>Change your input file so that it uses the correct line terminators. Your editor is probably more ... | 6 | 2010-02-02T14:10:00Z | [
"python",
"text",
"character-encoding",
"newline"
] |
Python thinks a 3000-line text file is one line long? | 2,184,543 | <p>I have a very long text file that I'm trying to process using Python.</p>
<p>However, the following code:</p>
<pre><code>for line in open('textbase.txt', 'r'):
print 'hello world'
</code></pre>
<p>produces only the following output:</p>
<pre><code>hello world
</code></pre>
<p>It's as though Python thinks th... | 11 | 2010-02-02T14:05:34Z | 2,184,569 | <p><code>open()</code> returns a file object. You need to use:</p>
<pre><code>for line in open('textbase.txt', 'r').readlines():
print line
</code></pre>
| -1 | 2010-02-02T14:10:17Z | [
"python",
"text",
"character-encoding",
"newline"
] |
Python thinks a 3000-line text file is one line long? | 2,184,543 | <p>I have a very long text file that I'm trying to process using Python.</p>
<p>However, the following code:</p>
<pre><code>for line in open('textbase.txt', 'r'):
print 'hello world'
</code></pre>
<p>produces only the following output:</p>
<pre><code>hello world
</code></pre>
<p>It's as though Python thinks th... | 11 | 2010-02-02T14:05:34Z | 2,184,586 | <p>According to the <a href="http://docs.python.org/library/functions.html#open">documentation for <code>open()</code></a>, you should add a <code>U</code> to the mode:</p>
<pre><code>open('textbase.txt', 'Ur')
</code></pre>
<p>This enables "<a href="http://www.python.org/dev/peps/pep-0278/">universal newlines</a>", ... | 24 | 2010-02-02T14:12:24Z | [
"python",
"text",
"character-encoding",
"newline"
] |
Python thinks a 3000-line text file is one line long? | 2,184,543 | <p>I have a very long text file that I'm trying to process using Python.</p>
<p>However, the following code:</p>
<pre><code>for line in open('textbase.txt', 'r'):
print 'hello world'
</code></pre>
<p>produces only the following output:</p>
<pre><code>hello world
</code></pre>
<p>It's as though Python thinks th... | 11 | 2010-02-02T14:05:34Z | 2,184,597 | <p>it looks like your file has lines terminated only by CR, and Python is probably expecting LF or CRLF. Try using the 'universal newline':</p>
<pre><code>for line in open('textbase.txt', 'rU'):
print 'hello world'
</code></pre>
<p><a href="http://docs.python.org/library/functions.html?highlight=open#open" rel="n... | 1 | 2010-02-02T14:13:52Z | [
"python",
"text",
"character-encoding",
"newline"
] |
How do I detect a lost client connection in a server that inherits from pb.Root? | 2,184,682 | <p>For example, I have a client that connects to the server with the following:</p>
<pre><code>class MyClientFactory(pb.PBClientFactory, ReconnectingClientFactory):
def __init__(self):
pb.PBClientFactory.__init__(self)
self.ipaddress = None
def clientConnectionMade(self, broker):
log.m... | 4 | 2010-02-02T14:25:19Z | 2,185,494 | <p>You can register a callback to be invoked when the connection is lost. There are two APIs for this, one is <code>Broker.notifyOnDisconnect</code>, the other is <code>RemoteReference.notifyOnDisconnect</code>. They do the same thing, but one or the other might be more convenient to access depending on the details o... | 3 | 2010-02-02T16:04:31Z | [
"python",
"twisted"
] |
Even numbers in Python | 2,184,745 | <p>Does anyone know if Python has an in-built function to work to print out even values. Like range() for example.</p>
<p>Thanks</p>
| 9 | 2010-02-02T14:32:30Z | 2,184,764 | <p><a href="http://docs.python.org/library/functions.html#range">Range has three parameters.</a></p>
<p>You can write <code>range(0, 10, 2)</code>.</p>
| 31 | 2010-02-02T14:34:56Z | [
"python",
"range"
] |
Even numbers in Python | 2,184,745 | <p>Does anyone know if Python has an in-built function to work to print out even values. Like range() for example.</p>
<p>Thanks</p>
| 9 | 2010-02-02T14:32:30Z | 2,184,766 | <p>Just use a step of 2:</p>
<pre><code>range(start, end, step)
</code></pre>
| 6 | 2010-02-02T14:35:05Z | [
"python",
"range"
] |
Even numbers in Python | 2,184,745 | <p>Does anyone know if Python has an in-built function to work to print out even values. Like range() for example.</p>
<p>Thanks</p>
| 9 | 2010-02-02T14:32:30Z | 2,184,768 | <p>Try:</p>
<pre><code>range( 0, 10, 2 )
</code></pre>
| 4 | 2010-02-02T14:35:09Z | [
"python",
"range"
] |
Even numbers in Python | 2,184,745 | <p>Does anyone know if Python has an in-built function to work to print out even values. Like range() for example.</p>
<p>Thanks</p>
| 9 | 2010-02-02T14:32:30Z | 2,184,771 | <pre><code>>>> if 100 % 2 == 0 : print "even"
...
even
</code></pre>
| 0 | 2010-02-02T14:35:29Z | [
"python",
"range"
] |
Even numbers in Python | 2,184,745 | <p>Does anyone know if Python has an in-built function to work to print out even values. Like range() for example.</p>
<p>Thanks</p>
| 9 | 2010-02-02T14:32:30Z | 2,184,774 | <p>I don't know if this is what you want to hear, but it's pretty trivial to filter out odd values with list comprehension.</p>
<pre><code>evens = [x for x in range(100) if x%2 == 0]
</code></pre>
<p>or</p>
<pre><code>evens = [x for x in range(100) if x&1 == 0]
</code></pre>
<p>You could also use the optional s... | 4 | 2010-02-02T14:35:35Z | [
"python",
"range"
] |
Even numbers in Python | 2,184,745 | <p>Does anyone know if Python has an in-built function to work to print out even values. Like range() for example.</p>
<p>Thanks</p>
| 9 | 2010-02-02T14:32:30Z | 25,107,504 | <pre><code>#This is not suggestible way to code in Python, but it gives a better understanding
numbers = range(1,10)
even = []
for i in numbers:
if i%2 == 0:
even.append(i)
print (even)
</code></pre>
| 0 | 2014-08-03T17:56:05Z | [
"python",
"range"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 2,184,979 | <p>Document the argument as needing to be a sequence, and use it as a sequence. Don't check the type.</p>
| 15 | 2010-02-02T15:03:24Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 2,185,035 | <p>In principle, I agree with Ignacio, above, but you can also use <em>type</em> to check if something is a tuple or a list.</p>
<pre><code>>>> a = (1,)
>>> type(a)
(type 'tuple')
>>> a = [1]
>>> type(a)
(type 'list')
</code></pre>
| 0 | 2010-02-02T15:10:45Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 2,185,098 | <p>There's nothing wrong with using <code>isinstance</code> as long as it's not redundant. If a variable should only be a list/tuple then document the interface and just use it as such. Otherwise a check is perfectly reasonable:</p>
<pre><code>if isinstance(a, collections.Iterable):
# use as a container
else:
... | 28 | 2010-02-02T15:19:27Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 2,185,159 | <p>Python uses "Duck typing", i.e. if a variable kwaks like a duck, it must be a duck. In your case, you probably want it to be iterable, or you want to access the item at a certain index. You should just do this: i.e. use the object in <code>for var:</code> or <code>var[idx]</code> inside a <code>try</code> block, and... | 6 | 2010-02-02T15:25:00Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 2,185,198 | <p>Go ahead and use <code>isinstance</code> if you need it. It is somewhat evil, as it excludes custom sequences, iterators, and other things that you might actually need. However, sometimes you need to behave differently if someone, for instance, passes a string. My preference there would be to explicitly check for... | 53 | 2010-02-02T15:29:58Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 2,186,104 | <p>If you just need to know if you can use the <code>foo[123]</code> notation with the variable, you can check for the existence of a <code>__getitem__</code> attribute (which is what python calls when you access by index) with <code>hasattr(foo, '__getitem__')</code></p>
| 2 | 2010-02-02T17:15:38Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 4,507,292 | <pre><code>>>> l = []
>>> l.__class__.__name__ in ('list', 'tuple')
True
</code></pre>
| 8 | 2010-12-22T08:11:03Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 8,236,234 | <pre><code>if type(x) is list:
print 'a list'
elif type(x) is tuple:
print 'a tuple'
else:
print 'neither a tuple or a list'
</code></pre>
| 257 | 2011-11-23T01:25:17Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 15,348,388 | <p>How about: <code>hasattr(a, "__iter__")</code> ?</p>
<p>It tells if the object returned can be iterated over as a generator. By default, tuples and lists can, but not the string types.</p>
| 8 | 2013-03-11T20:53:16Z | [
"python",
"types",
"list"
] |
Test if a variable is a list or tuple | 2,184,955 | <p>In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection)</p>
<p>Is isinstance as evil as suggested here? <a href="http://www.canonical.org/~kragen/isinstance/">http://www.canonical.org/~kragen/isinstance/</a></p>
<p>Update : the most common reason I want to distinguish a ... | 115 | 2010-02-02T15:00:23Z | 26,761,237 | <p>On Python 2.8 <code>type(list) is list</code> returns <code>false</code> <br>
I would suggest comparing the type in this horrible way:</p>
<pre><code>if type(a) == type([]) :
print "variable a is a list"
</code></pre>
<p>(well at least on my system, using anaconda on Mac OS X Yosemite)</p>
| 0 | 2014-11-05T15:46:13Z | [
"python",
"types",
"list"
] |
How to pass data to another function from a class (in HTMLParser)? | 2,185,020 | <p>I'm beginning to learn python. My python version is 3.1</p>
<p>I've never learnt OOP before,
so I'm confused by the HTMLParser.</p>
<pre><code>from html.parser import HTMLParser
class parser(HTMLParser):
def handle_data(self, data):
print(data)
p = parser()
page = """<html><h1>title</h1>&l... | 4 | 2010-02-02T15:08:13Z | 2,185,054 | <p>Just an example:</p>
<pre><code>def my_global_fun(data):
print "processing", data
class parser(HTMLParser):
def my_member_fun(self, data):
print "processing", data
def handle_data(self, data):
self.my_member_fun(data)
# or
my_global_fun(data)
</code></pre>
<p>Good luck... | 2 | 2010-02-02T15:13:48Z | [
"python",
"class",
"function"
] |
How to pass data to another function from a class (in HTMLParser)? | 2,185,020 | <p>I'm beginning to learn python. My python version is 3.1</p>
<p>I've never learnt OOP before,
so I'm confused by the HTMLParser.</p>
<pre><code>from html.parser import HTMLParser
class parser(HTMLParser):
def handle_data(self, data):
print(data)
p = parser()
page = """<html><h1>title</h1>&l... | 4 | 2010-02-02T15:08:13Z | 2,185,239 | <p>I did not look into the HTMLParser module itself, but I can see that feed inherently calls handle_data, which in your derived class does a print. @ron's answer suggests passing the data directly to your function, which is totally OK. However, since you are new to OOP, maybe take a look at this code.</p>
<p>This i... | 5 | 2010-02-02T15:35:25Z | [
"python",
"class",
"function"
] |
Suggested GA operators for a TSP problem? | 2,185,177 | <p>I'm building a genetic algorithm to tackle the traveling salesman problem. Unfortunately, I hit peaks that can sustain for over a thousand generations before mutating out of them and getting better results. What crossover and mutation operators generally do well in this case?</p>
| 4 | 2010-02-02T15:27:48Z | 2,185,746 | <p>Could you please clarify </p>
<blockquote>
<p>"Unfortunately, I hit peaks that can
sustain for over a thousand
generations before mutating out of
them and getting better results" ?</p>
</blockquote>
<p>You could check on the crossover operators, which make sure that you have no repeating nodes in the child... | 1 | 2010-02-02T16:33:42Z | [
"python",
"algorithm",
"genetic-algorithm",
"evolutionary-algorithm",
"traveling-salesman"
] |
Suggested GA operators for a TSP problem? | 2,185,177 | <p>I'm building a genetic algorithm to tackle the traveling salesman problem. Unfortunately, I hit peaks that can sustain for over a thousand generations before mutating out of them and getting better results. What crossover and mutation operators generally do well in this case?</p>
| 4 | 2010-02-02T15:27:48Z | 2,185,774 | <p>Ordered mutation and ordered cross-over (see <a href="http://www.permutationcity.co.uk/projects/mutants/tsp.html" rel="nofollow">this article</a>). Standard mutation and cross-over operations will usually result in invalid solutions (i.e. duplicate and/or missing cities in a route).</p>
<p>There was a <a href="htt... | 3 | 2010-02-02T16:37:40Z | [
"python",
"algorithm",
"genetic-algorithm",
"evolutionary-algorithm",
"traveling-salesman"
] |
Suggested GA operators for a TSP problem? | 2,185,177 | <p>I'm building a genetic algorithm to tackle the traveling salesman problem. Unfortunately, I hit peaks that can sustain for over a thousand generations before mutating out of them and getting better results. What crossover and mutation operators generally do well in this case?</p>
| 4 | 2010-02-02T15:27:48Z | 2,185,816 | <p>If your problem is that peaks remain for over one thousand generations, then the problem might not be with the crossover and mutation operators. You might not be introducing or keeping enough variation to your population: I would examine the proportions of crossovers, of mutations, and of survivors from one generat... | 2 | 2010-02-02T16:42:05Z | [
"python",
"algorithm",
"genetic-algorithm",
"evolutionary-algorithm",
"traveling-salesman"
] |
Writing Python packages with multiple C modules | 2,185,197 | <p>I am writing a Python C extension which uses the pygame C API. So far so good. Now I wonder how I organize my source code so that I can have multiple submodules in the package. All the tutorials out there focus on one .c file extensions. I tried looking at some projects setup.py files but they blew my mind with comp... | 9 | 2010-02-02T15:29:57Z | 2,185,588 | <p>I think the easiest way to do this would be to create the package in "pure python"; in other words, create <code>mypackage/</code>, create an empty <code>mypackage/__init__.py</code> , and then put your extension modules at <code>mypackage/module1.so</code>, <code>mypackage/module2.so</code>, and so on.</p>
<p>If y... | 14 | 2010-02-02T16:15:28Z | [
"python",
"module",
"organization"
] |
Writing Python packages with multiple C modules | 2,185,197 | <p>I am writing a Python C extension which uses the pygame C API. So far so good. Now I wonder how I organize my source code so that I can have multiple submodules in the package. All the tutorials out there focus on one .c file extensions. I tried looking at some projects setup.py files but they blew my mind with comp... | 9 | 2010-02-02T15:29:57Z | 5,131,668 | <p>I'm not sure that did I understand what you want. But there is kind of namespace Python package. You can put module in different place, but all of them share same package name. Here you can reference to this question <a href="http://stackoverflow.com/questions/1675734/how-do-i-create-a-namespace-package-in-python"... | 1 | 2011-02-27T05:43:58Z | [
"python",
"module",
"organization"
] |
Python / Regex: exclude everything except one thing | 2,185,229 | <p>Suppose I have these strings:</p>
<pre><code>a = "hello"
b = "-hello"
c = "-"
d = "hell-o"
e = " - "
</code></pre>
<p>How do I match only the <code>-</code>(String <code>C</code>)? I've tried a <code>if "-" in something</code> but obviously that isn't correct. Could someone please advise?</p>
<hr>
<p>Let's sa... | 2 | 2010-02-02T15:34:25Z | 2,185,255 | <p>If you want to match only variable <code>c</code>:</p>
<pre><code>if '-' == something:
print 'hurray!'
</code></pre>
<p>To answer the updates: yes, that would be too messy. You don't need regex there. Simple string methods are faster:</p>
<pre><code>>>> lst =["hello", "-hello", "-", "hell-o"," - "]... | 4 | 2010-02-02T15:36:45Z | [
"python",
"regex"
] |
Python / Regex: exclude everything except one thing | 2,185,229 | <p>Suppose I have these strings:</p>
<pre><code>a = "hello"
b = "-hello"
c = "-"
d = "hell-o"
e = " - "
</code></pre>
<p>How do I match only the <code>-</code>(String <code>C</code>)? I've tried a <code>if "-" in something</code> but obviously that isn't correct. Could someone please advise?</p>
<hr>
<p>Let's sa... | 2 | 2010-02-02T15:34:25Z | 2,185,281 | <p>as a regex its "^-$" </p>
| 1 | 2010-02-02T15:39:56Z | [
"python",
"regex"
] |
Python / Regex: exclude everything except one thing | 2,185,229 | <p>Suppose I have these strings:</p>
<pre><code>a = "hello"
b = "-hello"
c = "-"
d = "hell-o"
e = " - "
</code></pre>
<p>How do I match only the <code>-</code>(String <code>C</code>)? I've tried a <code>if "-" in something</code> but obviously that isn't correct. Could someone please advise?</p>
<hr>
<p>Let's sa... | 2 | 2010-02-02T15:34:25Z | 2,185,295 | <p>If what you're trying to do is strip out the dash (i.e. <code>he-llo</code> gives <code>hello</code>), then this is more of a job for generator expressions.</p>
<pre><code>''.join((char for char in 'he-llo' if char != '-'))
</code></pre>
| 0 | 2010-02-02T15:40:59Z | [
"python",
"regex"
] |
Python / Regex: exclude everything except one thing | 2,185,229 | <p>Suppose I have these strings:</p>
<pre><code>a = "hello"
b = "-hello"
c = "-"
d = "hell-o"
e = " - "
</code></pre>
<p>How do I match only the <code>-</code>(String <code>C</code>)? I've tried a <code>if "-" in something</code> but obviously that isn't correct. Could someone please advise?</p>
<hr>
<p>Let's sa... | 2 | 2010-02-02T15:34:25Z | 2,185,307 | <pre><code>if "-" in c and len(c) ==1 : print "do something"
</code></pre>
<p>OR </p>
<pre><code>if c=="-"
</code></pre>
| 0 | 2010-02-02T15:42:41Z | [
"python",
"regex"
] |
Pylons and Flex 3 | 2,185,329 | <p>Has anyone used Python/Pylons as the server backend for a Flex 3 application? Does anyone have any thoughts on how well this would work? I read Bruce Eckel's article about tying Flex 3 to Twisted, and I've done Twisted programming, but for just a web service I think Pylons is simpler to use.</p>
<p>Thanks in advanc... | 0 | 2010-02-02T15:45:14Z | 2,258,476 | <p>I'm working on webapp which has client-side UI coded in Flex 3 and backend is Pylons app. Our client communicates with backend using HTTP GET and POST requests, POST request bodies and all response bodies carry data in JSON format. Works well, just few gotchas:</p>
<ul>
<li>Flex apps cannot do PUT and DELETE reques... | 0 | 2010-02-13T17:52:40Z | [
"python",
"flex",
"pylons",
"twisted"
] |
Images caching in browser - app-engine-patch aplication | 2,185,449 | <p>I have a little problem with caching the images in the browser for my app-engine aplication
I`m sending last-modified, expires and cache-control headers but image is loaded from the server every time.
Here is the header part of the code:</p>
<p>response['Content-Type'] = 'image/jpg'</p>
<p>response['Last-Modified'... | 4 | 2010-02-02T15:58:54Z | 2,200,653 | <p>Here is an example code for my fix copy in dpaste <a href="http://dpaste.com/hold/154503/" rel="nofollow">here</a></p>
<pre><code>def view_image(request, key):
data = memcache.get(key)
if data is not None:
if(request.META.get('HTTP_IF_MODIFIED_SINCE') >= data['Last-Modified']):
data.status_co... | 7 | 2010-02-04T15:04:57Z | [
"python",
"google-app-engine",
"browser",
"caching",
"app-engine-patch"
] |
Expanding elements in a list | 2,185,822 | <p>I'm looking for a "nice" way to process a list where some elements need to be expanded into more elements (only once, no expansion on the results).</p>
<p>Standard iterative way would be to do:</p>
<pre><code>i=0
while i < len(l):
if needs_expanding(l[i]):
new_is = expand(l[i])
l[i:i] = new_is
... | 3 | 2010-02-02T16:42:25Z | 2,185,875 | <p>The last one is probably your most pythonic, but you could try an implied loop (or in py3, generator) with map:</p>
<pre><code>flatten(map(lambda x: expand(x) if needs_expanding(x) else x, l))
flatten(map(try_expanding, l))
</code></pre>
| 2 | 2010-02-02T16:49:27Z | [
"python",
"list"
] |
Expanding elements in a list | 2,185,822 | <p>I'm looking for a "nice" way to process a list where some elements need to be expanded into more elements (only once, no expansion on the results).</p>
<p>Standard iterative way would be to do:</p>
<pre><code>i=0
while i < len(l):
if needs_expanding(l[i]):
new_is = expand(l[i])
l[i:i] = new_is
... | 3 | 2010-02-02T16:42:25Z | 2,185,900 | <p>Your last two answers are what I would do. I'm not familiar with <code>flatten()</code> though, but if you have such a function then that looks ideal. You can also use the built-in <code>sum()</code>:</p>
<pre><code>sum(expand(x) if needs_expanding(x) else [x] for x in l, [])
sum(needs_expanding(x) and expand(x) or... | 3 | 2010-02-02T16:51:16Z | [
"python",
"list"
] |
Expanding elements in a list | 2,185,822 | <p>I'm looking for a "nice" way to process a list where some elements need to be expanded into more elements (only once, no expansion on the results).</p>
<p>Standard iterative way would be to do:</p>
<pre><code>i=0
while i < len(l):
if needs_expanding(l[i]):
new_is = expand(l[i])
l[i:i] = new_is
... | 3 | 2010-02-02T16:42:25Z | 2,185,971 | <p>If you do not need random access in the list you are generating, you could also use write a generator.</p>
<pre><code>def iter_new_list(old_list):
for x in old_list:
if needs_expanding(x):
for y in expand(x):
yield y
else:
yield x
new_list = list(iter_new_... | 2 | 2010-02-02T16:59:20Z | [
"python",
"list"
] |
How to start a local port on user's computer (Edited question) | 2,185,823 | <p>sorry in the past I have not been able to formulate my question coherently. This will be my last try. =|</p>
<p>Basically, I want to do something like this website is doing: <a href="http://www.ninjavideo.net/video/56388" rel="nofollow">http://www.ninjavideo.net/video/56388</a>. They are rendering an iframe that po... | 0 | 2010-02-02T16:42:30Z | 2,185,867 | <p>You could do this in PHP using a ready-to-run Apache setup (there are some that are ready to run from a USB key, should be possible to make into something that a client can install, but is complicated, see e.g. <a href="http://www.astahost.com/info.php/run-webserver-usb-stick-windows-guide_t14970.html" rel="nofollow... | 0 | 2010-02-02T16:48:43Z | [
"java",
"php",
"python",
"c",
"localhost"
] |
How to start a local port on user's computer (Edited question) | 2,185,823 | <p>sorry in the past I have not been able to formulate my question coherently. This will be my last try. =|</p>
<p>Basically, I want to do something like this website is doing: <a href="http://www.ninjavideo.net/video/56388" rel="nofollow">http://www.ninjavideo.net/video/56388</a>. They are rendering an iframe that po... | 0 | 2010-02-02T16:42:30Z | 2,185,874 | <p>An <a href="http://en.wikipedia.org/wiki/Applet" rel="nofollow">Applet</a> is basically a piece of Java code which is served by a webpage and is supposed to run at the client machine. You can learn more about Applets at <a href="http://java.sun.com/docs/books/tutorial/deployment/applet/" rel="nofollow">Sun's own App... | 2 | 2010-02-02T16:49:28Z | [
"java",
"php",
"python",
"c",
"localhost"
] |
entropy in txt file | 2,185,862 | <p>I have a text file with numbers in it as follows:</p>
<pre><code>1231313123123123
1432423432535345
3532523452345345
1231423432453455
3434535345345345
3452353453253453
</code></pre>
<p>all the lines are the same length, I want to calculate entropy on each line and have output as:</p>
<pre><code>2.64234234
2.654645... | 1 | 2010-02-02T16:47:55Z | 2,185,886 | <pre><code>failas = open('text.txt', 'r')
for row in failas:
print H(row)
</code></pre>
| 10 | 2010-02-02T16:50:19Z | [
"python"
] |
entropy in txt file | 2,185,862 | <p>I have a text file with numbers in it as follows:</p>
<pre><code>1231313123123123
1432423432535345
3532523452345345
1231423432453455
3434535345345345
3452353453253453
</code></pre>
<p>all the lines are the same length, I want to calculate entropy on each line and have output as:</p>
<pre><code>2.64234234
2.654645... | 1 | 2010-02-02T16:47:55Z | 2,185,890 | <p>Perhaps you meant <code>print H(row)</code>.</p>
| 5 | 2010-02-02T16:50:41Z | [
"python"
] |
entropy in txt file | 2,185,862 | <p>I have a text file with numbers in it as follows:</p>
<pre><code>1231313123123123
1432423432535345
3532523452345345
1231423432453455
3434535345345345
3452353453253453
</code></pre>
<p>all the lines are the same length, I want to calculate entropy on each line and have output as:</p>
<pre><code>2.64234234
2.654645... | 1 | 2010-02-02T16:47:55Z | 2,188,484 | <p>All of the above, plus you probably don't want to include the <code>\n</code> at end of each line in the entropy calculation. Use <code>H(row.rstrip('\n'))</code></p>
<p>You can answer a lot of your own questions by examining the data that is being tosssed around by your code. In this case, inserting <code>print re... | 1 | 2010-02-02T23:11:25Z | [
"python"
] |
Multithreading with pyqt - Can't get the separate threads running at the same time? | 2,186,076 | <p>I am trying to get a PyQT GUI running ontop of my python application and I have tried to get it separated into 2 threads so the GUI would be responsive while my main running loop goes, but I have not been able to get it going. Maybe I am misunderstanding it. Here is what I've tried:</p>
<p>My <code>Window</code> a... | 4 | 2010-02-02T17:12:46Z | 2,186,210 | <p>After looking more closely at your code, you have MAIN_WORLD started before QApplication, which isn't what you want.</p>
<p>You want to do something like this:</p>
<pre><code>if __name__ == '__main__':
app = QApplication(sys.argv)
sys.exit(app.exec_())
</code></pre>
<p>And, in your Window class:</p>
<pre... | 1 | 2010-02-02T17:31:15Z | [
"python",
"multithreading",
"pyqt"
] |
Multithreading with pyqt - Can't get the separate threads running at the same time? | 2,186,076 | <p>I am trying to get a PyQT GUI running ontop of my python application and I have tried to get it separated into 2 threads so the GUI would be responsive while my main running loop goes, but I have not been able to get it going. Maybe I am misunderstanding it. Here is what I've tried:</p>
<p>My <code>Window</code> a... | 4 | 2010-02-02T17:12:46Z | 7,607,818 | <p>Granted that is PyQt and not PySide, but:</p>
<ul>
<li>Don't subclass QThread, subclass QObject (see <a href="http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/" rel="nofollow">http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/</a>).</li>
<li>Then the basic workflow is to create a new thread, move you... | 0 | 2011-09-30T08:28:43Z | [
"python",
"multithreading",
"pyqt"
] |
wxPython GUI: migrating gnuplot to matplotlib | 2,186,240 | <p>I currently have a GUI built in wxPython with several sections, one of which displays a .png image of a plot:</p>
<pre><code>self.plot = wx.BitmapButton(self.pane_system, -1, wx.Bitmap("/home/myname/projects/newton/plot/src/graph.png", wx.BITMAP_TYPE_ANY))
</code></pre>
<p>In another part of the GUI, there is a pl... | 0 | 2010-02-02T17:36:16Z | 2,186,994 | <p>Taking your questions in order:</p>
<p>1.) Yes matplotlib is a contained python module. It does have external dependancies but in Windows these dependencies are packaged with the matplotlib install. Do you need to worry about these when you install on other machines? That depends on how you are going to install.... | 2 | 2010-02-02T19:28:18Z | [
"python",
"wxpython",
"matplotlib"
] |
Thread safe locale techniques | 2,186,327 | <p>We're currently writing a web application based on a threaded python web server framework (cherrypy) and would like to simultaneously support users from multiple locales.</p>
<p>The locale module doesn't appear to be thread safe. Are there 3rd party libraries or modules that provide locale parsing and formatting fu... | 2 | 2010-02-02T17:49:15Z | 2,186,449 | <p><a href="http://pyicu.osafoundation.org/" rel="nofollow">pyIcu</a> has some such functionality (and other great support for i18n/l10n tasks), and I've been using it some of the time, but it's far from a smooth port of <code>locale</code>-based code -- you'll basically have to rewrite your code for it. Also, it does... | 0 | 2010-02-02T18:08:38Z | [
"python",
"multithreading",
"locale"
] |
Use a Glob() to find files recursively in Python? | 2,186,525 | <p>This is what I have:</p>
<pre><code>glob(os.path.join('src','*.c'))
</code></pre>
<p>but I want to search the subfolders of src. Something like this would work:</p>
<pre><code>glob(os.path.join('src','*.c'))
glob(os.path.join('src','*','*.c'))
glob(os.path.join('src','*','*','*.c'))
glob(os.path.join('src','*','*... | 313 | 2010-02-02T18:19:50Z | 2,186,555 | <p>You'll want to use <code>os.walk</code> to collect filenames that match your criteria. For example:</p>
<pre><code>import os
cfiles = []
for root, dirs, files in os.walk('src'):
for file in files:
if file.endswith('.c'):
cfiles.append(os.path.join(root, file))
</code></pre>
| 14 | 2010-02-02T18:24:48Z | [
"python",
"path",
"glob"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.