title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
Python Printing StdOut As It Received | 1,152,160 | <p>I'm trying to run wrap a simple (windows) command line tool up in a PyQt GUI app that I am writing. The problem I have is that the command line tool throws it's progress out to stdout (it's a server reset command so you get "Attempting to stop" and "Restarting" type output.</p>
<p>What I am trying to do is capture... | 2 | 2009-07-20T07:41:19Z | 1,152,178 | <p>Interactive communication through stdin/stdout is a common problem.</p>
<p>You're in luck though, with PyQt you can use QProcess, as described here:
<a href="http://diotavelli.net/PyQtWiki/Capturing%5FOutput%5Ffrom%5Fa%5FProcess" rel="nofollow">http://diotavelli.net/PyQtWiki/Capturing_Output_from_a_Process</a></p>
| 1 | 2009-07-20T07:47:39Z | [
"python",
"stdout"
] |
Python Printing StdOut As It Received | 1,152,160 | <p>I'm trying to run wrap a simple (windows) command line tool up in a PyQt GUI app that I am writing. The problem I have is that the command line tool throws it's progress out to stdout (it's a server reset command so you get "Attempting to stop" and "Restarting" type output.</p>
<p>What I am trying to do is capture... | 2 | 2009-07-20T07:41:19Z | 1,198,143 | <p>Do I understand the question?
I believe you're running something like "echo first; sleep 60; echo second" and you want see the "first" well-ahead of the "second", but they're both spitting out at the same time.</p>
<p>The reason you're having issues is that the operating system stores the output of processes in its... | 0 | 2009-07-29T05:07:16Z | [
"python",
"stdout"
] |
Python iterators â how to dynamically assign self.next within a new style class? | 1,152,238 | <p>As part of some WSGI middleware I want to write a python class that wraps an iterator to implement a close method on the iterator.</p>
<p>This works fine when I try it with an old-style class, but throws a TypeError when I try it with a new-style class. What do I need to do to get this working with a new-style cla... | 11 | 2009-07-20T08:07:03Z | 1,152,294 | <p>Just return the iterator. That's what <code>__iter__</code> is for. It makes no sense to try to monkey-patch the object into being in iterator and return it when you already have an iterator.</p>
<p>EDIT: Now with two methods. Once, monkey patching the wrapped iterator, second, kitty-wrapping the iterator.</p>
<pr... | 2 | 2009-07-20T08:23:43Z | [
"python",
"iterator"
] |
Python iterators â how to dynamically assign self.next within a new style class? | 1,152,238 | <p>As part of some WSGI middleware I want to write a python class that wraps an iterator to implement a close method on the iterator.</p>
<p>This works fine when I try it with an old-style class, but throws a TypeError when I try it with a new-style class. What do I need to do to get this working with a new-style cla... | 11 | 2009-07-20T08:07:03Z | 1,152,328 | <p>Looks like built-in <code>iter</code> doesn't check for <code>next</code> callable in an instance but in a class and <code>IteratorWrapper2</code> doesn't have any <code>next</code>. Below is simpler version of your problem</p>
<pre><code>class IteratorWrapper2(object):
def __init__(self, otheriter):
s... | 3 | 2009-07-20T08:31:11Z | [
"python",
"iterator"
] |
Python iterators â how to dynamically assign self.next within a new style class? | 1,152,238 | <p>As part of some WSGI middleware I want to write a python class that wraps an iterator to implement a close method on the iterator.</p>
<p>This works fine when I try it with an old-style class, but throws a TypeError when I try it with a new-style class. What do I need to do to get this working with a new-style cla... | 11 | 2009-07-20T08:07:03Z | 1,152,353 | <p>What you're trying to do makes sense, but there's something evil going on inside Python here.</p>
<pre><code>class foo(object):
c = 0
def __init__(self):
self.next = self.next2
def __iter__(self):
return self
def next(self):
if self.c == 5: raise StopIteration
self.... | 8 | 2009-07-20T08:33:45Z | [
"python",
"iterator"
] |
Python iterators â how to dynamically assign self.next within a new style class? | 1,152,238 | <p>As part of some WSGI middleware I want to write a python class that wraps an iterator to implement a close method on the iterator.</p>
<p>This works fine when I try it with an old-style class, but throws a TypeError when I try it with a new-style class. What do I need to do to get this working with a new-style cla... | 11 | 2009-07-20T08:07:03Z | 12,657,458 | <p>There are a bunch of places where CPython take surprising shortcuts based on <em>class</em> properties instead of <em>instance</em> properties. This is one of those places.</p>
<p>Here is a simple example that demonstrates the issue:</p>
<pre><code>def DynamicNext(object):
def __init__(self):
self.next... | 5 | 2012-09-29T23:40:18Z | [
"python",
"iterator"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 1,152,484 | <p>I don't think using <code>with</code> is the solution in this case. You'd have to raise an exception in the <code>BLOCK</code> part (which is specified by the user) and have the <code>__exit__</code> method return <code>True</code> to "swallow" the exception. So it would never look good.</p>
<p>I'd suggest going fo... | 0 | 2009-07-20T09:05:03Z | [
"python",
"code-golf",
"idioms"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 1,152,490 | <p>I don't think it's trivial. I don't want to have to sprinkle a redundant conditional around my code if I'm writing code like that often.</p>
<p>This is slightly odd, but you can do this with an iterator:</p>
<pre><code>import re
def rematch(pattern, inp):
matcher = re.compile(pattern)
matches = matcher.m... | 11 | 2009-07-20T09:06:08Z | [
"python",
"code-golf",
"idioms"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 1,152,578 | <p>If you're doing a lot of these in one place, here's an alternative answer:</p>
<pre><code>import re
class Matcher(object):
def __init__(self):
self.matches = None
def set(self, matches):
self.matches = matches
def __getattr__(self, name):
return getattr(self.matches, name)
class... | 0 | 2009-07-20T09:25:15Z | [
"python",
"code-golf",
"idioms"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 7,669,147 | <p>This is not really pretty-looking, but you can profit from the <code>getattr(object, name[, default])</code> built-in function using it like this:</p>
<pre><code>>>> getattr(re.match("(\d+)g", "123g"), 'group', lambda n:'')(1)
'123'
>>> getattr(re.match("(\d+)g", "X23g"), 'group', lambda n:'')(1)
... | 0 | 2011-10-06T00:08:25Z | [
"python",
"code-golf",
"idioms"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 8,512,642 | <p>Not the perfect solution, but does allow you to chain several match options for the same str:</p>
<pre><code>class MatchWrapper(object):
def __init__(self):
self._matcher = None
def wrap(self, matcher):
self._matcher = matcher
def __getattr__(self, attr):
return getattr(self._matcher, attr)
def... | 0 | 2011-12-14T22:30:17Z | [
"python",
"code-golf",
"idioms"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 13,722,876 | <p>Another nice syntax would be something like this:</p>
<pre><code>header = re.compile('(.*?) = (.*?)$')
footer = re.compile('(.*?): (.*?)$')
if header.match(line) as m:
key, value = m.group(1,2)
elif footer.match(line) as m
key, value = m.group(1,2)
else:
key, value = None, None
</code></pre>
| 4 | 2012-12-05T12:05:15Z | [
"python",
"code-golf",
"idioms"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 19,917,971 | <p>I have another way of doing this, based on Glen Maynard's solution:</p>
<pre><code>for match in [m for m in [re.match(pattern,key)] if m]:
print "It matched: %s" % match
</code></pre>
<p>Similar to Glen's solution, this itterates either 0 (if no match) or 1 (if a match) times.</p>
<p>No sub needed, but less t... | 1 | 2013-11-11T23:22:42Z | [
"python",
"code-golf",
"idioms"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 23,440,622 | <p>Here's my solution:</p>
<pre><code>import re
s = 'hello world'
match = []
if match.append(re.match('w\w+', s)) or any(match):
print('W:', match.pop().group(0))
elif match.append(re.match('h\w+', s)) or any(match):
print('H:', match.pop().group(0))
else:
print('No match found')
</code></pre>
<p>You ca... | 0 | 2014-05-03T05:24:06Z | [
"python",
"code-golf",
"idioms"
] |
Alternative to the `match = re.match(); if match: ...` idiom? | 1,152,385 | <p>If you want to check if something matches a regex, if so, print the first group, you do..</p>
<pre><code>import re
match = re.match("(\d+)g", "123g")
if match is not None:
print match.group(1)
</code></pre>
<p>This is completely pedantic, but the intermediate <code>match</code> variable is a bit annoying..</p>... | 23 | 2009-07-20T08:40:43Z | 26,278,129 | <p>This is what I do:</p>
<pre><code>def re_match_cond (match_ref, regex, text):
match = regex.match (text)
del match_ref[:]
match_ref.append (match)
return match
if __name__ == '__main__':
match_ref = []
if re_match_cond (match_ref, regex_1, text):
match = match_ref[0]
### ...... | 0 | 2014-10-09T12:11:58Z | [
"python",
"code-golf",
"idioms"
] |
django documentation locally setting up | 1,152,479 | <p>I was trying to setup django . I do have Django-1.1-alpha-1. I was trying to make the documentation which is located at Django-1.1-alpha-1/doc using make utility.</p>
<p>But I am getting some error saying </p>
<pre>
> C:\django\Django-1.1-alpha-1\docs>C:\cygwin\bin\make.exe html
<br>mkdir -p _build/html _build/doc... | 6 | 2009-07-20T09:03:42Z | 1,152,499 | <p>Install <a href="http://sphinx.pocoo.org/">sphinx</a>.</p>
<pre><code>$ easy_install -U Sphinx
</code></pre>
| 8 | 2009-07-20T09:08:29Z | [
"python",
"django",
"python-sphinx"
] |
Is it better to use an exception or a return code in Python? | 1,152,541 | <p>You may know this recommendation from Microsoft about the use of exceptions in .NET:</p>
<blockquote>
<p>Performance Considerations</p>
<p>... </p>
<p>Throw exceptions only for
extraordinary conditions, ...</p>
<p>In addition, do not throw an exception
when a return code is sufficient...</p>
</... | 34 | 2009-07-20T09:18:45Z | 1,152,556 | <p>The pythonic thing to do is to raise and handle exceptions. The excellent book "Python in a nutshell" discusses this in 'Error-Checking Strategies' in Chapter 6.</p>
<p>The book discusses EAFP ("it's easier to ask forgiveness than permission") vs. LBYL ("look before you leap").</p>
<p>So to answer your question:</... | 31 | 2009-07-20T09:21:54Z | [
"python",
"performance",
"exception"
] |
Is it better to use an exception or a return code in Python? | 1,152,541 | <p>You may know this recommendation from Microsoft about the use of exceptions in .NET:</p>
<blockquote>
<p>Performance Considerations</p>
<p>... </p>
<p>Throw exceptions only for
extraordinary conditions, ...</p>
<p>In addition, do not throw an exception
when a return code is sufficient...</p>
</... | 34 | 2009-07-20T09:18:45Z | 1,152,562 | <p>In Python exceptions are not very expensive like they are in some other languages, so I wouldn't recommend trying to avoid exceptions. But if you do throw an exception you would usually want catch it somewhere in your code, the exception being if a fatal error occurs.</p>
| 7 | 2009-07-20T09:23:05Z | [
"python",
"performance",
"exception"
] |
Is it better to use an exception or a return code in Python? | 1,152,541 | <p>You may know this recommendation from Microsoft about the use of exceptions in .NET:</p>
<blockquote>
<p>Performance Considerations</p>
<p>... </p>
<p>Throw exceptions only for
extraordinary conditions, ...</p>
<p>In addition, do not throw an exception
when a return code is sufficient...</p>
</... | 34 | 2009-07-20T09:18:45Z | 1,153,109 | <p>Usually, Python is geared towards expressiveness.<br>
I would apply the same principle here: usually, you expect a function to return a <strong>result</strong> (in line with its name!) and not an error code.<br>
For this reason, it is usually better raising an exception than returning an error code.</p>
<p>However... | 7 | 2009-07-20T11:49:51Z | [
"python",
"performance",
"exception"
] |
Is it better to use an exception or a return code in Python? | 1,152,541 | <p>You may know this recommendation from Microsoft about the use of exceptions in .NET:</p>
<blockquote>
<p>Performance Considerations</p>
<p>... </p>
<p>Throw exceptions only for
extraordinary conditions, ...</p>
<p>In addition, do not throw an exception
when a return code is sufficient...</p>
</... | 34 | 2009-07-20T09:18:45Z | 1,153,149 | <p>The best way to understand exceptions is "<a href="http://www.hanselman.com/blog/IfYourMethodCantDoWhatItsNamePromisesItCanThrow.aspx">if your method can't do what its name says it does, throw</a>." My personal opinion is that this advice should be applied equally to both .NET and Python.</p>
<p>The key difference ... | 8 | 2009-07-20T12:02:16Z | [
"python",
"performance",
"exception"
] |
Is it better to use an exception or a return code in Python? | 1,152,541 | <p>You may know this recommendation from Microsoft about the use of exceptions in .NET:</p>
<blockquote>
<p>Performance Considerations</p>
<p>... </p>
<p>Throw exceptions only for
extraordinary conditions, ...</p>
<p>In addition, do not throw an exception
when a return code is sufficient...</p>
</... | 34 | 2009-07-20T09:18:45Z | 1,269,369 | <p>I think whether to return an error code or throw an exception is something very valid to think about, and a cross-linguistic comparison may be helpful and informative. I guess the very generalized answer to this concern is simply the consideration: that <strong>the set of legal return values for any function should ... | 3 | 2009-08-13T00:03:53Z | [
"python",
"performance",
"exception"
] |
Is it better to use an exception or a return code in Python? | 1,152,541 | <p>You may know this recommendation from Microsoft about the use of exceptions in .NET:</p>
<blockquote>
<p>Performance Considerations</p>
<p>... </p>
<p>Throw exceptions only for
extraordinary conditions, ...</p>
<p>In addition, do not throw an exception
when a return code is sufficient...</p>
</... | 34 | 2009-07-20T09:18:45Z | 17,359,901 | <p>I did a simple experiment to compare the performance of raising exceptions with the following code:</p>
<pre><code>from functools import wraps
from time import time
import logging
def timed(foo):
@wraps(foo)
def bar(*a, **kw):
s = time()
foo(*a, **kw)
e = time()
print '%f se... | 5 | 2013-06-28T07:55:11Z | [
"python",
"performance",
"exception"
] |
memcache entities without ReferenceProperty | 1,152,690 | <p>I have a list of entities which I want to store in the memcache. The
problem is that I have large Models referenced by their
ReferenceProperty which are automatically also stored in the memcache.
As a result I'm exceeding the size limit for objects stored in
memcache. </p>
<p>Is there any possibility to prevent... | 2 | 2009-07-20T09:57:29Z | 1,152,934 | <pre><code>odict = self.copy()
del odict.model
</code></pre>
<p>would probably be better than using <strong>dict</strong> (unless getstate needs to return dict - i'm not familiar with it). Not sure if this solves Your problem, though... You could implement <strong>del</strong> in Model to test if it's freed. For me it... | 0 | 2009-07-20T11:08:06Z | [
"python",
"google-app-engine",
"memcached"
] |
memcache entities without ReferenceProperty | 1,152,690 | <p>I have a list of entities which I want to store in the memcache. The
problem is that I have large Models referenced by their
ReferenceProperty which are automatically also stored in the memcache.
As a result I'm exceeding the size limit for objects stored in
memcache. </p>
<p>Is there any possibility to prevent... | 2 | 2009-07-20T09:57:29Z | 1,185,319 | <p>For large entities, you might want to manually handle the loading of the related entities by storing the keys of the large entities as something other than a ReferenceProperty. That way you can choose when to load the large entity and when not to. Just use a long property store ids or a string property to store key... | 1 | 2009-07-26T19:16:15Z | [
"python",
"google-app-engine",
"memcached"
] |
Is it possible to fetch a https page via an authenticating proxy with urllib2 in Python 2.5? | 1,152,980 | <p>I'm trying to add authenticating proxy support to an existing script, as it is the script connects to a https url (with urllib2.Request and urllib2.urlopen), scrapes the page and performs some actions based on what it has found. Initially I had hoped this would be as easy as simply adding a urllib2.ProxyHandler({"ht... | 3 | 2009-07-20T11:20:31Z | 1,153,048 | <p>You may want to look into <a href="http://code.google.com/p/httplib2/" rel="nofollow">httplib2</a>. One of the <a href="http://code.google.com/p/httplib2/wiki/Examples" rel="nofollow">examples</a> claims support for SOCKS proxies if the <a href="http://socksipy.sourceforge.net/" rel="nofollow">socks</a> module is in... | 1 | 2009-07-20T11:36:22Z | [
"python",
"proxy",
"https",
"urllib2"
] |
Simple python / Beautiful Soup type question | 1,153,167 | <p>I'm trying to do some simple string manipulation with the href attribute of a hyperlink extracted using <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a>:</p>
<pre><code>from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<a href="http://www.some-site.com/">So... | 3 | 2009-07-20T12:05:38Z | 1,153,182 | <p>Python strings do not have an <code>indexOf</code> method.</p>
<p>Use <code>href.index('/')</code></p>
<p><code>href.find('/')</code> is similar. But <code>find</code> returns <code>-1</code> if the string is not found, while <code>index</code> raises a <code>ValueError</code>.</p>
<p>So the correct thing is to u... | 7 | 2009-07-20T12:08:10Z | [
"python",
"string",
"beautifulsoup"
] |
Simple python / Beautiful Soup type question | 1,153,167 | <p>I'm trying to do some simple string manipulation with the href attribute of a hyperlink extracted using <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a>:</p>
<pre><code>from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<a href="http://www.some-site.com/">So... | 3 | 2009-07-20T12:05:38Z | 1,153,200 | <p>href is a unicode string. If you need the regular string, then use </p>
<pre><code>regular_string = str(href)
</code></pre>
| 0 | 2009-07-20T12:11:12Z | [
"python",
"string",
"beautifulsoup"
] |
Simple python / Beautiful Soup type question | 1,153,167 | <p>I'm trying to do some simple string manipulation with the href attribute of a hyperlink extracted using <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">Beautiful Soup</a>:</p>
<pre><code>from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<a href="http://www.some-site.com/">So... | 3 | 2009-07-20T12:05:38Z | 1,153,687 | <p>You mean find(), not indexOf().</p>
<p><a href="http://docs.python.org/library/stdtypes.html" rel="nofollow">Python docs on strings</a>.</p>
| 0 | 2009-07-20T13:47:23Z | [
"python",
"string",
"beautifulsoup"
] |
Python: question about parsing human-readable text | 1,153,183 | <p>I'm parsing human-readable scientific text that is mostly in the field of chemistry. What I'm interested in is breaking the text into a list of words, scientific terms (more on that below), and punctuation marks.</p>
<p>So for example, I expect the text "hello, world." to break into 4 tokens: 1) "hello"; 2) comma; ... | 2 | 2009-07-20T12:08:21Z | 1,153,254 | <p>There might be a regex parsing what you want, but I don't think it will be very readable/maintainable. My advice would be to use a parser generator like ANTLR. I think you'll have to throw the notion overboard that you can make the chemical descriptions a single token, much too complex. ANTLR even has a debugger so ... | 0 | 2009-07-20T12:19:11Z | [
"python",
"parsing"
] |
Python: question about parsing human-readable text | 1,153,183 | <p>I'm parsing human-readable scientific text that is mostly in the field of chemistry. What I'm interested in is breaking the text into a list of words, scientific terms (more on that below), and punctuation marks.</p>
<p>So for example, I expect the text "hello, world." to break into 4 tokens: 1) "hello"; 2) comma; ... | 2 | 2009-07-20T12:08:21Z | 1,153,461 | <p>This will solve your current example. It can be tweaked for a larger data set.</p>
<pre><code>import re
splitterForIndexing = re.compile(r"(?:[a-zA-Z0-9\-,]+[a-zA-Z0-9\-])|(?:[,.])")
source = "Hello. 1-methyl-4-phenylpyridinium is ultra-bad. However, 1-methyl-4-phenyl-1,2,3,6-tetrahydropyridine is worse."
print "\n... | 2 | 2009-07-20T13:06:02Z | [
"python",
"parsing"
] |
Python: question about parsing human-readable text | 1,153,183 | <p>I'm parsing human-readable scientific text that is mostly in the field of chemistry. What I'm interested in is breaking the text into a list of words, scientific terms (more on that below), and punctuation marks.</p>
<p>So for example, I expect the text "hello, world." to break into 4 tokens: 1) "hello"; 2) comma; ... | 2 | 2009-07-20T12:08:21Z | 1,157,062 | <p>I agree with Sebastiaan Megens that a regex solution may be possible, but probably not very readable or maintainable, especially if you are not already good with regular expressions. I would recommend the <a href="http://pyparsing.wikispaces.com/" rel="nofollow">pyparsing module</a>, if you're sticking with Python ... | 0 | 2009-07-21T02:51:46Z | [
"python",
"parsing"
] |
Stopping a Long-Running Subprocess | 1,153,407 | <p>I create a subprocess using subprocess.Popen() that runs for a long time. It is called from its own thread, and the thread is blocked until the subprocess completes/returns.</p>
<p>I want to be able to interrupt the subprocess so the process terminates when I want.</p>
<p>Any ideas?</p>
| 1 | 2009-07-20T12:55:40Z | 1,153,442 | <p>I think you're looking for <a href="http://docs.python.org/library/subprocess.html#subprocess.Popen.terminate" rel="nofollow"><code>Popen.terminate</code></a> or <code>.kill</code> function. They were added in python 2.6.</p>
| 4 | 2009-07-20T13:02:16Z | [
"python",
"subprocess"
] |
Mercurial scripting with python | 1,153,469 | <p>I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.</p>
<p>The reason is that I want to add it to the css/js files on our website like so:</p>
<pre><code><link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
</code></pre>
<... | 28 | 2009-07-20T13:08:01Z | 1,153,499 | <p>give a try to <a href="http://mercurial.selenic.com/wiki/KeywordExtension" rel="nofollow">the keyword extension</a></p>
| 3 | 2009-07-20T13:14:48Z | [
"python",
"documentation",
"mercurial",
"revision"
] |
Mercurial scripting with python | 1,153,469 | <p>I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.</p>
<p>The reason is that I want to add it to the css/js files on our website like so:</p>
<pre><code><link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
</code></pre>
<... | 28 | 2009-07-20T13:08:01Z | 1,153,698 | <p>Do you mean <a href="http://mercurial.selenic.com/wiki/MercurialApi">this documentation</a>?<br />
Note that, as stated in that page, there is no <em>official</em> API, because they still reserve the right to change it at any time. But you can see the list of changes in the last few versions, it is not very extensiv... | 8 | 2009-07-20T13:49:47Z | [
"python",
"documentation",
"mercurial",
"revision"
] |
Mercurial scripting with python | 1,153,469 | <p>I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.</p>
<p>The reason is that I want to add it to the css/js files on our website like so:</p>
<pre><code><link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
</code></pre>
<... | 28 | 2009-07-20T13:08:01Z | 1,154,440 | <p>It's true there's no official API, but you can get an idea about best practices by reading other extensions, particularly those bundled with hg. For this particular problem, I would do something like this:</p>
<pre><code>from mercurial import ui, hg
from mercurial.node import hex
repo = hg.repository('/path/to/rep... | 14 | 2009-07-20T16:06:03Z | [
"python",
"documentation",
"mercurial",
"revision"
] |
Mercurial scripting with python | 1,153,469 | <p>I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.</p>
<p>The reason is that I want to add it to the css/js files on our website like so:</p>
<pre><code><link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
</code></pre>
<... | 28 | 2009-07-20T13:08:01Z | 13,568,616 | <p>I wanted to do the same thing the OP wanted to do, get <code>hg id -i</code> from a script (get tip revision of the whole REPOSITORY, not of a single FILE in that repo) but I did not want to use popen, and the code from <code>brendan</code> got me started, but wasn't what I wanted. </p>
<p>So I wrote this... Commen... | 0 | 2012-11-26T16:08:18Z | [
"python",
"documentation",
"mercurial",
"revision"
] |
Mercurial scripting with python | 1,153,469 | <p>I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.</p>
<p>The reason is that I want to add it to the css/js files on our website like so:</p>
<pre><code><link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
</code></pre>
<... | 28 | 2009-07-20T13:08:01Z | 13,568,980 | <p>FWIW to avoid fetching that value on every page/view render, I just have my deploy put it into the <code>settings.py</code> file. Then I can reference <code>settings.REVISION</code> without all the overhead of accessing mercurial and/or another process. Do you ever have this value change w/o reloading your server?... | 1 | 2012-11-26T16:28:54Z | [
"python",
"documentation",
"mercurial",
"revision"
] |
Mercurial scripting with python | 1,153,469 | <p>I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.</p>
<p>The reason is that I want to add it to the css/js files on our website like so:</p>
<pre><code><link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
</code></pre>
<... | 28 | 2009-07-20T13:08:01Z | 22,000,832 | <p>An updated, cleaner subprocess version (uses <code>.check_output()</code>, added in Python 2.7/3.1) that I use in my Django settings file for a crude end-to-end deployment check (I dump it into an HTML comment):</p>
<pre><code>import subprocess
HG_REV = subprocess.check_output(['hg', 'id', '--id']).strip()
</code>... | 3 | 2014-02-24T22:50:00Z | [
"python",
"documentation",
"mercurial",
"revision"
] |
Mercurial scripting with python | 1,153,469 | <p>I am trying to get the mercurial revision number/id (it's a hash not a number) programmatically in python.</p>
<p>The reason is that I want to add it to the css/js files on our website like so:</p>
<pre><code><link rel="stylesheet" href="example.css?{% mercurial_revision "example.css" %}" />
</code></pre>
<... | 28 | 2009-07-20T13:08:01Z | 28,800,428 | <p>
<strong>If you are using Python 2, you want to use <a href="http://mercurial.selenic.com/wiki/pythonhglib" rel="nofollow"><code>hglib</code></a>.</strong> </p>
<p>I don't know what to use if you're using Python 3, sorry. Probably <a href="https://pypi.python.org/pypi/hgapi/1.7.2" rel="nofollow"><code>hgapi</code><... | 3 | 2015-03-01T23:12:55Z | [
"python",
"documentation",
"mercurial",
"revision"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 1,153,591 | <p>You can write <a href="http://www.python.org/doc/2.5.2/ext/intro.html" rel="nofollow">python extensions</a> in C++. Basically python itself is written in C and you can use that to call into your C code. You have full access to your python objects. Also check out <a href="http://www.boost.org/doc/libs/1%5F39%5F0/libs... | 1 | 2009-07-20T13:30:06Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 1,153,604 | <p>Yes, it is possible, encouraged and <a href="http://docs.python.org/extending/extending.html" rel="nofollow">documented</a>. I have done it myself and found it to be very easy.</p>
| 7 | 2009-07-20T13:31:37Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 1,153,605 | <p><a href="http://docs.python.org/c-api/index.html" rel="nofollow">Python/C API Reference Manual</a> - the API used by C and C++ programmers who want to write extension modules or embed Python.</p>
<p><a href="http://docs.python.org/extending/index.html#extending-index" rel="nofollow">Extending and Embedding the Pyth... | 3 | 2009-07-20T13:31:48Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 1,153,606 | <p>Try <a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/" rel="nofollow">Pyrex</a>. Makes writing C++ extensions for Python easier.</p>
| 3 | 2009-07-20T13:31:53Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 1,153,623 | <p>See this:</p>
<p>Extending Python with C or C++ </p>
<p>"It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can't be done directly in Python: they can implement new built-in object types, and they can call C library functions and s... | 1 | 2009-07-20T13:33:47Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 1,153,635 | <p>Interfacing Python with C/C++ is not an easy task.</p>
<p>Here I copy/paste a <a href="http://stackoverflow.com/questions/456884/extending-python-to-swig-or-not-to-swig/456949#456949">previous answer</a> on a previous question for the different methods to write a python extension. Featuring Boost.Python, SWIG, Pybi... | 53 | 2009-07-20T13:35:54Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 1,153,884 | <p>I've used PyCxx <a href="http://cxx.sourceforge.net/" rel="nofollow">http://cxx.sourceforge.net/</a> in the past and i found that it was very good. </p>
<p>It wraps the python c API in a very elegant manner and makes it very simple to use.
It is very easy to write python extension in c++. It is provided with clear ... | 2 | 2009-07-20T14:19:57Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 1,154,056 | <p>We use <a href="http://www.swig.org/" rel="nofollow">swig</a> very successfully in our product.</p>
<p>Basically swig takes your C++ code and generates a python wrapper around it.</p>
| 3 | 2009-07-20T14:52:10Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 9,513,677 | <p>It depends on your portability requirements. I've been struggling with this for a while, and I ended up wrapping my C++ using the <a href="http://docs.python.org/extending/index.html" rel="nofollow">python API</a> directly because I need something that works on systems where the admin has only hacked together a most... | 2 | 2012-03-01T09:31:20Z | [
"c++",
"python",
"integration"
] |
Integrate Python And C++ | 1,153,577 | <p>I'm learning C++ because it's a very flexible language. But for internet things like Twitter, Facebook, Delicious and others, Python seems a much better solution.</p>
<p>Is it possible to integrate C++ and Python in the same project?</p>
| 29 | 2009-07-20T13:28:04Z | 18,262,313 | <p>Another interesting way to do is python code generation by running python itself to parse c++ header files. <a href="http://answers.opencv.org/question/6618/how-python-api-is-generated/" rel="nofollow">OpenCV team successfully took this approach</a> and now they have done exact same thing to make java wrapper for Op... | 1 | 2013-08-15T21:37:47Z | [
"c++",
"python",
"integration"
] |
How do I debug a py2exe 'application failed to initialize properly' error? | 1,153,643 | <p>I'm very new to Python in general, but I made an app in Python 2.6 / wxPython 2.8 that works perfectly when I run it through Python. But I wanted to go a step further and be able to deploy it as a Windows executable, so I've been trying out py2exe. But I haven't been able to get it to work. It would always compile a... | 7 | 2009-07-20T13:37:08Z | 1,153,721 | <p>I've used py2exe before and I've never run across a situation like this....</p>
<p>However, it sounds like missing dependencies are your problem.....</p>
<p>Does the packaged program work on your machine but not on others?</p>
<p>If so, run the packaged application within DEPENDS (dependency walker) on both machi... | 0 | 2009-07-20T13:54:20Z | [
"python",
"wxpython",
"py2exe"
] |
How do I debug a py2exe 'application failed to initialize properly' error? | 1,153,643 | <p>I'm very new to Python in general, but I made an app in Python 2.6 / wxPython 2.8 that works perfectly when I run it through Python. But I wanted to go a step further and be able to deploy it as a Windows executable, so I've been trying out py2exe. But I haven't been able to get it to work. It would always compile a... | 7 | 2009-07-20T13:37:08Z | 1,153,867 | <p>You are not supposed to copy ALL of the .dlls it complains about! Some of them are Windows system files, and they are present in the correct places on the system. If you copy them into the dist folder, things won't work correctly.</p>
<p>In general, you only want to copy .dlls which are specific to your applicatio... | 0 | 2009-07-20T14:17:17Z | [
"python",
"wxpython",
"py2exe"
] |
How do I debug a py2exe 'application failed to initialize properly' error? | 1,153,643 | <p>I'm very new to Python in general, but I made an app in Python 2.6 / wxPython 2.8 that works perfectly when I run it through Python. But I wanted to go a step further and be able to deploy it as a Windows executable, so I've been trying out py2exe. But I haven't been able to get it to work. It would always compile a... | 7 | 2009-07-20T13:37:08Z | 1,154,115 | <p>Are you sure that you give the same dlls that the one used by wxPython.</p>
<p>The vc++ dlls used by wxpython can be downloaded from the wxpython download page. Did you try these one?</p>
| 0 | 2009-07-20T15:04:14Z | [
"python",
"wxpython",
"py2exe"
] |
How do I debug a py2exe 'application failed to initialize properly' error? | 1,153,643 | <p>I'm very new to Python in general, but I made an app in Python 2.6 / wxPython 2.8 that works perfectly when I run it through Python. But I wanted to go a step further and be able to deploy it as a Windows executable, so I've been trying out py2exe. But I haven't been able to get it to work. It would always compile a... | 7 | 2009-07-20T13:37:08Z | 1,154,736 | <p>Note that there is a later version of the Visual C++ 2008 Redistributable package: <a href="http://www.microsoft.com/downloads/info.aspx?na=47&p=2&SrcDisplayLang=en&SrcCategoryId=&SrcFamilyId=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&u=details.aspx%3ffamilyid%3dA5C84275-3B97-4AB7-A40D-3802B2AF5FC2%26d... | 9 | 2009-07-20T17:11:13Z | [
"python",
"wxpython",
"py2exe"
] |
How do I debug a py2exe 'application failed to initialize properly' error? | 1,153,643 | <p>I'm very new to Python in general, but I made an app in Python 2.6 / wxPython 2.8 that works perfectly when I run it through Python. But I wanted to go a step further and be able to deploy it as a Windows executable, so I've been trying out py2exe. But I haven't been able to get it to work. It would always compile a... | 7 | 2009-07-20T13:37:08Z | 1,573,757 | <p>Your question was: How do I debug py2exe</p>
<p>Here is a tip:</p>
<ul>
<li>Use <a href="http://www.dependencywalker.com" rel="nofollow">dependency walker</a></li>
</ul>
<p>As for your specific problem, I expect that you will find a solution on this more complete thread dealing with the same problem:</p>
<p><a h... | 1 | 2009-10-15T17:07:57Z | [
"python",
"wxpython",
"py2exe"
] |
Listing builtin functions and methods (Python) | 1,153,690 | <p>I have came up with this:</p>
<pre><code>[a for a in dir(__builtins__) if str(type(getattr(__builtins__,a))) == "<type 'builtin_function_or_method'>"]
</code></pre>
<p>I know its ugly. Can you show me a better/more pythonic way of doing this?</p>
| 3 | 2009-07-20T13:47:57Z | 1,153,722 | <p>There is the <a href="http://docs.python.org/library/inspect.html" rel="nofollow"><code>inspect</code> module</a>:</p>
<pre><code>import inspect
filter(inspect.isbuiltin, (member for name, member in inspect.getmembers(__builtins__)))
</code></pre>
<p>Edit: reading the documentation a little more closely, I came u... | 6 | 2009-07-20T13:54:31Z | [
"python",
"module",
"introspection",
"inspect"
] |
Listing builtin functions and methods (Python) | 1,153,690 | <p>I have came up with this:</p>
<pre><code>[a for a in dir(__builtins__) if str(type(getattr(__builtins__,a))) == "<type 'builtin_function_or_method'>"]
</code></pre>
<p>I know its ugly. Can you show me a better/more pythonic way of doing this?</p>
| 3 | 2009-07-20T13:47:57Z | 1,153,803 | <p>Here's a variation without getattr:</p>
<pre><code>import inspect
[n.__name__ for n in __builtins__.__dict__.values() if inspect.isbuiltin(n)]
</code></pre>
<p>And if you want the actual function pointers:</p>
<pre><code>import inspect
[n for n in __builtins__.__dict__.values() if inspect.isbuiltin(n)]
</code></p... | 2 | 2009-07-20T14:08:05Z | [
"python",
"module",
"introspection",
"inspect"
] |
PyQt: how to handle auto-resize of widgets when their content changes | 1,153,714 | <p>I am having some issues with the size of qt4 widgets when their content changes.</p>
<p>I will illustrate my problems with two simple scenarios:</p>
<p>Scenario 1:</p>
<p>I have a QLineEdit widget. Sometimes, when I'm changing its content using QLineEdit.setText(), the one-line string doesn't fit into the widget ... | 9 | 2009-07-20T13:53:55Z | 1,166,388 | <p>I'm answering in C++ here, since that's what I'm most familiar with, and your problem isn't specific to PyQt.</p>
<p>Normally, you just need to call <code>QWidget::updateGeometry()</code> when the <code>sizeHint()</code> may have changed, just like you need to call <code>QWidget::update()</code> when the contents m... | 7 | 2009-07-22T16:08:31Z | [
"c++",
"python",
"qt",
"qt4",
"pyqt4"
] |
PyQt: how to handle auto-resize of widgets when their content changes | 1,153,714 | <p>I am having some issues with the size of qt4 widgets when their content changes.</p>
<p>I will illustrate my problems with two simple scenarios:</p>
<p>Scenario 1:</p>
<p>I have a QLineEdit widget. Sometimes, when I'm changing its content using QLineEdit.setText(), the one-line string doesn't fit into the widget ... | 9 | 2009-07-20T13:53:55Z | 1,188,257 | <p>For the QTextBrowser case you should be able to get the size of the document using</p>
<pre><code>QTextBrowser::document()->size();
</code></pre>
<p>after setting the html, and then resizing it the QTextBrowser afterwards. </p>
| 2 | 2009-07-27T13:49:41Z | [
"c++",
"python",
"qt",
"qt4",
"pyqt4"
] |
PyQt: how to handle auto-resize of widgets when their content changes | 1,153,714 | <p>I am having some issues with the size of qt4 widgets when their content changes.</p>
<p>I will illustrate my problems with two simple scenarios:</p>
<p>Scenario 1:</p>
<p>I have a QLineEdit widget. Sometimes, when I'm changing its content using QLineEdit.setText(), the one-line string doesn't fit into the widget ... | 9 | 2009-07-20T13:53:55Z | 1,813,475 | <p>Ok implement <code>sizeHint()</code> method. And every time your content change size call <code>updateGeometry()</code>
When content change without changing size use <code>update()</code>. (<code>updateGeometry()</code> automatically call <code>update()</code>).</p>
| 0 | 2009-11-28T19:18:00Z | [
"c++",
"python",
"qt",
"qt4",
"pyqt4"
] |
PyQt: how to handle auto-resize of widgets when their content changes | 1,153,714 | <p>I am having some issues with the size of qt4 widgets when their content changes.</p>
<p>I will illustrate my problems with two simple scenarios:</p>
<p>Scenario 1:</p>
<p>I have a QLineEdit widget. Sometimes, when I'm changing its content using QLineEdit.setText(), the one-line string doesn't fit into the widget ... | 9 | 2009-07-20T13:53:55Z | 1,817,169 | <p>i achieve a similar effect by using the following C++ class:</p>
<h2>textedit.h</h2>
<pre><code>#ifndef TEXTEDIT_H
#define TEXTEDIT_H
#include <QTextEdit>
class TextEdit : public QTextEdit
{
Q_DISABLE_COPY( TextEdit )
public:
TextEdit( QWidget* parent = NULL );
TextEdit( const QString& text, QWi... | 1 | 2009-11-29T23:39:50Z | [
"c++",
"python",
"qt",
"qt4",
"pyqt4"
] |
PyQt: how to handle auto-resize of widgets when their content changes | 1,153,714 | <p>I am having some issues with the size of qt4 widgets when their content changes.</p>
<p>I will illustrate my problems with two simple scenarios:</p>
<p>Scenario 1:</p>
<p>I have a QLineEdit widget. Sometimes, when I'm changing its content using QLineEdit.setText(), the one-line string doesn't fit into the widget ... | 9 | 2009-07-20T13:53:55Z | 4,453,797 | <p>Maybe take a look at <a href="https://sourceforge.net/project/stats/?group_id=384208&ugn=pyqtresize" rel="nofollow">Python QT Automatic Widget Resizer</a>. It's written in python though but it may give you some ideas on how to go about doing what you need.</p>
| 2 | 2010-12-15T19:05:33Z | [
"c++",
"python",
"qt",
"qt4",
"pyqt4"
] |
SQLAlchemy and django, is it production ready? | 1,154,331 | <p>Has anyone used <code>SQLAlchemy</code> in addition to <code>Django</code>'s ORM?</p>
<p>I'd like to use Django's ORM for object manipulation and SQLalchemy for complex queries (like those that require left outer joins). </p>
<p>Is it possible?</p>
<p>Note: I'm aware about <code>django-sqlalchemy</code> but the p... | 21 | 2009-07-20T15:44:44Z | 1,154,949 | <p>I don't think it's good practice to use both. You should either:</p>
<ol>
<li>Use Django's ORM and use custom SQL where Django's built-in SQL generation doesn't meet your needs, or</li>
<li>Use SQLAlchemy (which gives you finer control at the price of added complexity) and, if needed, use a declarative layer such a... | 6 | 2009-07-20T17:54:00Z | [
"python",
"database",
"django",
"sqlalchemy"
] |
SQLAlchemy and django, is it production ready? | 1,154,331 | <p>Has anyone used <code>SQLAlchemy</code> in addition to <code>Django</code>'s ORM?</p>
<p>I'd like to use Django's ORM for object manipulation and SQLalchemy for complex queries (like those that require left outer joins). </p>
<p>Is it possible?</p>
<p>Note: I'm aware about <code>django-sqlalchemy</code> but the p... | 21 | 2009-07-20T15:44:44Z | 1,155,407 | <p>What I would do,</p>
<ol>
<li><p>Define the schema in Django orm, let it write the db via syncdb. You get the admin interface.</p></li>
<li><p>In view1 you need a complex join</p></li>
</ol>
<pre>
<code>
def view1(request):
import sqlalchemy
data = sqlalchemy.complex_join_magic(...)
...
... | 17 | 2009-07-20T19:21:06Z | [
"python",
"database",
"django",
"sqlalchemy"
] |
SQLAlchemy and django, is it production ready? | 1,154,331 | <p>Has anyone used <code>SQLAlchemy</code> in addition to <code>Django</code>'s ORM?</p>
<p>I'd like to use Django's ORM for object manipulation and SQLalchemy for complex queries (like those that require left outer joins). </p>
<p>Is it possible?</p>
<p>Note: I'm aware about <code>django-sqlalchemy</code> but the p... | 21 | 2009-07-20T15:44:44Z | 1,308,718 | <p>Jacob Kaplan-Moss admitted to typing "import sqlalchemy" from time to time. I may write a queryset adapter for sqlalchemy results in the not too distant future.</p>
| 4 | 2009-08-20T20:47:20Z | [
"python",
"database",
"django",
"sqlalchemy"
] |
SQLAlchemy and django, is it production ready? | 1,154,331 | <p>Has anyone used <code>SQLAlchemy</code> in addition to <code>Django</code>'s ORM?</p>
<p>I'd like to use Django's ORM for object manipulation and SQLalchemy for complex queries (like those that require left outer joins). </p>
<p>Is it possible?</p>
<p>Note: I'm aware about <code>django-sqlalchemy</code> but the p... | 21 | 2009-07-20T15:44:44Z | 3,555,602 | <p>I've done it before and it's fine. Use the SQLAlchemy feature where it can read in the schema so you don't need to declare your fields twice.</p>
<p>You can grab the connection settings from the settings, the only problem is stuff like the different flavours of postgres driver (e.g. with psyco and without).</p>
<... | 6 | 2010-08-24T10:45:36Z | [
"python",
"database",
"django",
"sqlalchemy"
] |
SQLAlchemy and django, is it production ready? | 1,154,331 | <p>Has anyone used <code>SQLAlchemy</code> in addition to <code>Django</code>'s ORM?</p>
<p>I'd like to use Django's ORM for object manipulation and SQLalchemy for complex queries (like those that require left outer joins). </p>
<p>Is it possible?</p>
<p>Note: I'm aware about <code>django-sqlalchemy</code> but the p... | 21 | 2009-07-20T15:44:44Z | 27,579,527 | <p>Nowadays you can use <a href="https://pypi.python.org/pypi/aldjemy/0.3.10" rel="nofollow">Aldjemy</a>. Consider using this <a href="http://rodic.fr/blog/sqlalchemy-django/" rel="nofollow">tutorial</a>.</p>
| 2 | 2014-12-20T10:59:47Z | [
"python",
"database",
"django",
"sqlalchemy"
] |
Python write to line flow | 1,154,373 | <p>Part of my script is taking values and putting them to a text file delimited by tabs. So I have this:</p>
<pre><code>for linesplit in fileList:
for i in range (0, len(linesplit)):
t.write (linesplit[i]+'\t')
</code></pre>
<p>I get as an output in the file what I expect in the first line but in the foll... | 1 | 2009-07-20T15:54:50Z | 1,154,387 | <p>I'm sorry... After I hit submit it dawned on me. My last value already has a \n in it which causes the newline.</p>
| 1 | 2009-07-20T15:57:40Z | [
"python",
"file-io"
] |
Python write to line flow | 1,154,373 | <p>Part of my script is taking values and putting them to a text file delimited by tabs. So I have this:</p>
<pre><code>for linesplit in fileList:
for i in range (0, len(linesplit)):
t.write (linesplit[i]+'\t')
</code></pre>
<p>I get as an output in the file what I expect in the first line but in the foll... | 1 | 2009-07-20T15:54:50Z | 1,154,391 | <p>it doesn't produce what you want because original lines (<code>linesplit</code>) contain end of line character (<code>\n</code>) that you're not stripping. insert the following before your second for loop:</p>
<pre><code>linesplit = linesplit.strip('\n')
</code></pre>
<p>That should do the job.</p>
| 6 | 2009-07-20T15:58:04Z | [
"python",
"file-io"
] |
Python write to line flow | 1,154,373 | <p>Part of my script is taking values and putting them to a text file delimited by tabs. So I have this:</p>
<pre><code>for linesplit in fileList:
for i in range (0, len(linesplit)):
t.write (linesplit[i]+'\t')
</code></pre>
<p>I get as an output in the file what I expect in the first line but in the foll... | 1 | 2009-07-20T15:54:50Z | 1,154,396 | <p>Your last value must have a "\n" which causes both problems: The first problem because it outputs '\t' after the newline, the second problem should be obvious - the mystery newlines are coming from your last value.</p>
| 1 | 2009-07-20T15:59:23Z | [
"python",
"file-io"
] |
Python write to line flow | 1,154,373 | <p>Part of my script is taking values and putting them to a text file delimited by tabs. So I have this:</p>
<pre><code>for linesplit in fileList:
for i in range (0, len(linesplit)):
t.write (linesplit[i]+'\t')
</code></pre>
<p>I get as an output in the file what I expect in the first line but in the foll... | 1 | 2009-07-20T15:54:50Z | 1,154,398 | <p>you linesplit variable could have newlines. just use strip() to remove it. </p>
| 1 | 2009-07-20T15:59:34Z | [
"python",
"file-io"
] |
Python write to line flow | 1,154,373 | <p>Part of my script is taking values and putting them to a text file delimited by tabs. So I have this:</p>
<pre><code>for linesplit in fileList:
for i in range (0, len(linesplit)):
t.write (linesplit[i]+'\t')
</code></pre>
<p>I get as an output in the file what I expect in the first line but in the foll... | 1 | 2009-07-20T15:54:50Z | 1,154,399 | <p>Your <code>"value3"</code> strings are really <code>"value3\n"</code>, which explains the magical newlines and the extraneous tabs on all but the first line.</p>
| 1 | 2009-07-20T16:00:00Z | [
"python",
"file-io"
] |
Python write to line flow | 1,154,373 | <p>Part of my script is taking values and putting them to a text file delimited by tabs. So I have this:</p>
<pre><code>for linesplit in fileList:
for i in range (0, len(linesplit)):
t.write (linesplit[i]+'\t')
</code></pre>
<p>I get as an output in the file what I expect in the first line but in the foll... | 1 | 2009-07-20T15:54:50Z | 1,154,401 | <p>The inner loop isn't required. Perhaps you meant to use:</p>
<pre><code>t.write('\t'.join(linesplit))
</code></pre>
| 0 | 2009-07-20T16:00:24Z | [
"python",
"file-io"
] |
How to run statistics Cumulative Distribution Function and Probablity Density Function using SciPy? | 1,154,378 | <p>I am new to Python and new to SciPy libraries. I wanted to take some ques from the experts here on the list before dive into SciPy world.</p>
<p>I was wondering if some one could provide a rough guide about how to run two stats functions: Cumulative Distribution Function (CDF) and Probability Distribution Function ... | 4 | 2009-07-20T15:55:52Z | 1,154,859 | <p>See this article: <a href="http://www.johndcook.com/blog/2009/07/20/probability-distributions-scipy/">Probability distributions in SciPy</a>.</p>
| 8 | 2009-07-20T17:35:06Z | [
"python",
"statistics",
"scipy",
"probability"
] |
Python: List initialization differences | 1,154,494 | <p>I want a list full of the same thing, where the thing will either be a string or a number. Is there a difference in the way these two list are created? Is there anything hidden that I should probably know about?</p>
<pre><code>list_1 = [0] * 10
list_2 = [0 for i in range(10)]
</code></pre>
<p>Are there any bett... | 3 | 2009-07-20T16:17:33Z | 1,154,512 | <p>I personally would advice to use the first method, since it is most likely the best performing one, since the system knows in advance the size of the list and the contents.</p>
<p>In the second form, it must first evaluate the generator and collect all the values. Most likely by building up the list incrementally -... | 3 | 2009-07-20T16:22:50Z | [
"python",
"list"
] |
Python: List initialization differences | 1,154,494 | <p>I want a list full of the same thing, where the thing will either be a string or a number. Is there a difference in the way these two list are created? Is there anything hidden that I should probably know about?</p>
<pre><code>list_1 = [0] * 10
list_2 = [0 for i in range(10)]
</code></pre>
<p>Are there any bett... | 3 | 2009-07-20T16:17:33Z | 1,154,521 | <p>It depends on whether your list elements are mutable, if they are, there'll be a difference:</p>
<pre><code>>>> l = [[]] * 10
>>> l
[[], [], [], [], [], [], [], [], [], []]
>>> l[0].append(1)
>>> l
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]
>>> l = [[] for i in r... | 15 | 2009-07-20T16:24:26Z | [
"python",
"list"
] |
Python: List initialization differences | 1,154,494 | <p>I want a list full of the same thing, where the thing will either be a string or a number. Is there a difference in the way these two list are created? Is there anything hidden that I should probably know about?</p>
<pre><code>list_1 = [0] * 10
list_2 = [0 for i in range(10)]
</code></pre>
<p>Are there any bett... | 3 | 2009-07-20T16:17:33Z | 1,155,237 | <p>The first one is not only faster, but is also more readable: just by a quick look, you immediately understand what's into that list, while in the second case you have to stop and see the iteration. </p>
<p>Since source code is written once and read many times, for immutable elements I definitely vote for the first... | 1 | 2009-07-20T18:49:24Z | [
"python",
"list"
] |
python distutils / setuptools: how to exclude a module, or honor svn:ignore flag | 1,154,586 | <p>I have a python project, 'myproject', that contains several packages. one of those packages, 'myproject.settings', contains a module 'myproject.settings.local' that is excluded from version control via 'svn:ignore' property.</p>
<p>I would like setuptools to ignore this file when making a bdist or bdist_egg target.... | 4 | 2009-07-20T16:36:17Z | 1,155,321 | <p>I don't know if there is a regular way to do that but you you try a workaround like proposed in the <a href="http://stackoverflow.com/questions/1129180/how-can-i-make-setuptools-ignore-subversion-inventory">http://stackoverflow.com/questions/1129180/how-can-i-make-setuptools-ignore-subversion-inventory</a></p>
<p>s... | 2 | 2009-07-20T19:07:25Z | [
"python",
"setuptools",
"distutils"
] |
Using Cheetah Templating system with windows and python 2.6.1 (namemapper problem) | 1,155,065 | <p>So I am trying to use the Cheetah templating engine in conjunction with the Django web framework, and that is actually working fine. I did some simple tests with that and I was able to render pages and whatnot.</p>
<p>However, problems arise whenever doing anything other than using very simple variable/attribute/m... | 5 | 2009-07-20T18:17:16Z | 1,248,643 | <p>I have compiled the PYD file for Python 2.6 as well as Windows installers that have it bundled in, so that users don't have to figure out where to drop the PYD on Windows.</p>
<p>Installers: <a href="http://feisley.com/python/cheetah/" rel="nofollow">http://feisley.com/python/cheetah/</a> (pyd files are in the /pyd... | 6 | 2009-08-08T11:41:28Z | [
"python",
"windows",
"django",
"template-engine",
"cheetah"
] |
Are there any libraries for generating Python source? | 1,155,186 | <p>I'd like to write a code generation tool that will allow me to create sourcefiles for dynamically generated classes. I can create the class and use it in code, but it would be nice to have a sourcefile both for documentation and to allow something to import.</p>
<p>Does such a thing exist? I've seen <a href="http... | 0 | 2009-07-20T18:41:42Z | 1,155,229 | <p>I'm not aware of any off-the-shelf library, but have a look at the Python templating engines <a href="http://www.makotemplates.org/" rel="nofollow">Mako</a> and <a href="http://jinja.pocoo.org/2/" rel="nofollow">Jinja2</a>. They can both generate Python source behind the scenes (they convert text templates to Python... | 2 | 2009-07-20T18:48:21Z | [
"python",
"code-generation"
] |
Prevent opening a second instance | 1,155,315 | <p>What's the easiest way to check if my program is already running with WxPython under Windows? Ideally, if the user tries to launch the program a second time, the focus should return to the first instance (even if the window is minimized).</p>
<p>This <a href="http://stackoverflow.com/questions/94274/return-to-an-al... | 2 | 2009-07-20T19:05:52Z | 1,155,368 | <p>You should use <a href="http://www.wxpython.org/docs/api/wx.SingleInstanceChecker-class.html" rel="nofollow">wx.SingleInstanceChecker</a>. See <a href="http://wiki.wxpython.org/OneInstanceRunning" rel="nofollow">here</a> for more information on how to use it, and <a href="http://aspn.activestate.com/ASPN/Mail/Messag... | 3 | 2009-07-20T19:14:50Z | [
"python",
"windows",
"user-interface",
"wxpython"
] |
How do I access an inherited class's inner class and modify it? | 1,155,351 | <p>So I have a class, specifically, this:</p>
<pre><code>class ProductVariantForm_PRE(ModelForm):
class Meta:
model = ProductVariant
exclude = ("productowner","status")
def clean_meta(self):
if len(self.cleaned_data['meta']) == 0:
raise forms.ValidationError(_(u'You have to... | 1 | 2009-07-20T19:12:33Z | 1,155,448 | <p>Take a look at the Django documentation for model inheritance <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#meta-inheritance" rel="nofollow">here</a>. From that page:</p>
<blockquote>
<p>When an abstract base class is
created, Django makes any Meta inner
class you declared in the base class... | 4 | 2009-07-20T19:26:07Z | [
"python",
"django"
] |
Python: re..find longest sequence | 1,155,376 | <p>I have a string that is randomly generated:</p>
<pre><code>polymer_str = "diol diNCO diamine diNCO diamine diNCO diamine diNCO diol diNCO diamine"
</code></pre>
<p>I'd like to find the longest sequence of "diNCO diol" and the longest of "diNCO diamine". So in the case above the longest "diNCO diol" sequence is 1 ... | 0 | 2009-07-20T19:15:36Z | 1,155,442 | <p>One was is to use <code>findall</code>:</p>
<pre><code>polymer_str = "diol diNCO diamine diNCO diamine diNCO diamine diNCO diol diNCO diamine"
len(re.findall("diNCO diamine", polymer_str)) # returns 4.
</code></pre>
| 0 | 2009-07-20T19:25:40Z | [
"python",
"regex"
] |
Python: re..find longest sequence | 1,155,376 | <p>I have a string that is randomly generated:</p>
<pre><code>polymer_str = "diol diNCO diamine diNCO diamine diNCO diamine diNCO diol diNCO diamine"
</code></pre>
<p>I'd like to find the longest sequence of "diNCO diol" and the longest of "diNCO diamine". So in the case above the longest "diNCO diol" sequence is 1 ... | 0 | 2009-07-20T19:15:36Z | 1,155,466 | <p>Using re:</p>
<pre><code> m = re.search(r"(\bdiNCO diamine\b\s?)+", polymer_str)
len(m.group(0)) / len("bdiNCO diamine")
</code></pre>
| 0 | 2009-07-20T19:29:42Z | [
"python",
"regex"
] |
Python: re..find longest sequence | 1,155,376 | <p>I have a string that is randomly generated:</p>
<pre><code>polymer_str = "diol diNCO diamine diNCO diamine diNCO diamine diNCO diol diNCO diamine"
</code></pre>
<p>I'd like to find the longest sequence of "diNCO diol" and the longest of "diNCO diamine". So in the case above the longest "diNCO diol" sequence is 1 ... | 0 | 2009-07-20T19:15:36Z | 1,155,508 | <p>I think the op wants the longest contiguous sequence. You can get all contiguous sequences like:
seqs = re.findall("(?:diNCO diamine)+", polymer_str) </p>
<p>and then find the longest.</p>
| 3 | 2009-07-20T19:37:33Z | [
"python",
"regex"
] |
Python: re..find longest sequence | 1,155,376 | <p>I have a string that is randomly generated:</p>
<pre><code>polymer_str = "diol diNCO diamine diNCO diamine diNCO diamine diNCO diol diNCO diamine"
</code></pre>
<p>I'd like to find the longest sequence of "diNCO diol" and the longest of "diNCO diamine". So in the case above the longest "diNCO diol" sequence is 1 ... | 0 | 2009-07-20T19:15:36Z | 1,155,805 | <p>Expanding on <a href="http://stackoverflow.com/users/90354/ealdwulf">Ealdwulf</a>'s <a href="http://stackoverflow.com/questions/1155376/python-re-find-longest-sequence/1155508#1155508">answer</a>:</p>
<p>Documentation on <code>re.findall</code> can be found <a href="http://docs.python.org/library/re.html#re.findall... | 5 | 2009-07-20T20:31:51Z | [
"python",
"regex"
] |
Python: re..find longest sequence | 1,155,376 | <p>I have a string that is randomly generated:</p>
<pre><code>polymer_str = "diol diNCO diamine diNCO diamine diNCO diamine diNCO diol diNCO diamine"
</code></pre>
<p>I'd like to find the longest sequence of "diNCO diol" and the longest of "diNCO diamine". So in the case above the longest "diNCO diol" sequence is 1 ... | 0 | 2009-07-20T19:15:36Z | 1,156,704 | <pre><code>import re
pat = re.compile("[^|]+")
p = "diol diNCO diamine diNCO diamine diNCO diamine diNCO diol diNCO diamine".replace("diNCO diamine","|").replace(" ","")
print max(map(len,pat.split(p)))
</code></pre>
| 2 | 2009-07-21T00:25:54Z | [
"python",
"regex"
] |
Python accessing multiple webpages at once | 1,155,404 | <p>I have a tkinter GUI that downloads data from multiple websites at once. I run a seperate thread for each download (about 28). Is that too much threads for one GUI process? because it's really slow, each individual page should take about 1 to 2 seconds but when all are run at once it takes over 40 seconds. Is there ... | 0 | 2009-07-20T19:20:13Z | 1,155,473 | <p>It's probably the GIL (global interpreter lock) that gets in your way. Python has some performance problems with many threads.</p>
<p>You could try twisted.web.getPage (see <a href="http://twistedmatrix.com/projects/core/documentation/howto/async.html" rel="nofollow">http://twistedmatrix.com/projects/core/documenta... | 2 | 2009-07-20T19:31:22Z | [
"python",
"user-interface",
"multithreading",
"download",
"tkinter"
] |
Python accessing multiple webpages at once | 1,155,404 | <p>I have a tkinter GUI that downloads data from multiple websites at once. I run a seperate thread for each download (about 28). Is that too much threads for one GUI process? because it's really slow, each individual page should take about 1 to 2 seconds but when all are run at once it takes over 40 seconds. Is there ... | 0 | 2009-07-20T19:20:13Z | 1,155,479 | <p>You can try using processes instead of threads. Python has GIL which might cause some delays in your situation.</p>
| 0 | 2009-07-20T19:31:59Z | [
"python",
"user-interface",
"multithreading",
"download",
"tkinter"
] |
Python accessing multiple webpages at once | 1,155,404 | <p>I have a tkinter GUI that downloads data from multiple websites at once. I run a seperate thread for each download (about 28). Is that too much threads for one GUI process? because it's really slow, each individual page should take about 1 to 2 seconds but when all are run at once it takes over 40 seconds. Is there ... | 0 | 2009-07-20T19:20:13Z | 1,155,498 | <p>A process can have hundreds of threads on any modern OS without any problem.</p>
<p>If you're bandwidth-limited, 1 to 2 seconds times 28 means 40 seconds is about right. If you're latency limited, it should be faster, but with no information, all I can suggest is:</p>
<ul>
<li>add logging to your code to make sur... | 1 | 2009-07-20T19:35:07Z | [
"python",
"user-interface",
"multithreading",
"download",
"tkinter"
] |
Django, how to make a view atomic? | 1,155,513 | <p>I have a simple django app to simulate a stock market, users come in and buy/sell. When they choose to trade, </p>
<ol>
<li>the market price is read, and</li>
<li>based on the buy/sell order the market price is increased/decreased.</li>
</ol>
<p>I'm not sure how this works in django, but is there a way to make the... | 1 | 2009-07-20T19:38:26Z | 1,155,531 | <p>Wrap the DB queries that read and the ones that update in a transaction. The syntax depends on what ORM you are using.</p>
| -1 | 2009-07-20T19:43:43Z | [
"python",
"database",
"django",
"locking",
"atomic"
] |
Django, how to make a view atomic? | 1,155,513 | <p>I have a simple django app to simulate a stock market, users come in and buy/sell. When they choose to trade, </p>
<ol>
<li>the market price is read, and</li>
<li>based on the buy/sell order the market price is increased/decreased.</li>
</ol>
<p>I'm not sure how this works in django, but is there a way to make the... | 1 | 2009-07-20T19:38:26Z | 1,155,591 | <p>This is database transactions, with some notes. All notes for Postgresql; all databases have locking mechanisms but the details are different.</p>
<p>Many databases don't do this level of locking by default, even if you're in a transaction. You need to get an explicit lock on the data.</p>
<p>In Postgresql, you ... | 1 | 2009-07-20T19:57:18Z | [
"python",
"database",
"django",
"locking",
"atomic"
] |
Django, how to make a view atomic? | 1,155,513 | <p>I have a simple django app to simulate a stock market, users come in and buy/sell. When they choose to trade, </p>
<ol>
<li>the market price is read, and</li>
<li>based on the buy/sell order the market price is increased/decreased.</li>
</ol>
<p>I'm not sure how this works in django, but is there a way to make the... | 1 | 2009-07-20T19:38:26Z | 24,523,421 | <p>Pretty old question, but since <a href="https://docs.djangoproject.com/en/dev/topics/db/transactions/" rel="nofollow">1.6</a> you can use <code>transaction.atomic()</code> as decorator.</p>
<p><strong>views.py</strong></p>
<pre><code>@transaction.atomic()
def stock_action(request):
#trade here
</code></pre>
| 0 | 2014-07-02T05:23:12Z | [
"python",
"database",
"django",
"locking",
"atomic"
] |
Getting rows from XML using XPath and Python | 1,155,566 | <p>I'd like to get some rows (<strong>z:row</strong> rows) from XML using:</p>
<pre><code><rs:data>
<z:row Attribute1="1" Attribute2="1" />
<z:row Attribute1="2" Attribute2="2" />
<z:row Attribute1="3" Attribute2="3" />
<z:row Attribute1="4" Attribute2="4" />
<z:row... | 0 | 2009-07-20T19:50:51Z | 1,155,581 | <p>If you don't want to figure out setting up namespaces properly, you can ignore them like this:</p>
<pre><code>XPathGet("//*[local-name() = 'row']")
</code></pre>
<p>Which selects every node whose name (without namespace) is <code>row</code>.</p>
| 1 | 2009-07-20T19:54:53Z | [
"python",
"xml",
"xpath"
] |
Getting rows from XML using XPath and Python | 1,155,566 | <p>I'd like to get some rows (<strong>z:row</strong> rows) from XML using:</p>
<pre><code><rs:data>
<z:row Attribute1="1" Attribute2="1" />
<z:row Attribute1="2" Attribute2="2" />
<z:row Attribute1="3" Attribute2="3" />
<z:row Attribute1="4" Attribute2="4" />
<z:row... | 0 | 2009-07-20T19:50:51Z | 1,155,584 | <p>The "z:" prefixes represent an XML namespace. you'll need to find out what that namespace is, and do the following:</p>
<pre><code>XmlDocument doc = new XmlDocument();
doc.Load(@"File.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("z", @"http://thenamespace.com");
XmlNodeLis... | 1 | 2009-07-20T19:55:31Z | [
"python",
"xml",
"xpath"
] |
Getting rows from XML using XPath and Python | 1,155,566 | <p>I'd like to get some rows (<strong>z:row</strong> rows) from XML using:</p>
<pre><code><rs:data>
<z:row Attribute1="1" Attribute2="1" />
<z:row Attribute1="2" Attribute2="2" />
<z:row Attribute1="3" Attribute2="3" />
<z:row Attribute1="4" Attribute2="4" />
<z:row... | 0 | 2009-07-20T19:50:51Z | 1,155,892 | <p>If I define the namespaces like this: </p>
<pre><code><?xml version="1.0"?>
<rs:data xmlns="http://example.com" xmlns:rs="http://example.com/rs" xmlns:z="http://example.com/z">
<z:row Attribute1="1" Attribute2="1" />
<z:row Attribute1="2" Attribute2="2" />
<z:row Attribute1="3" A... | 1 | 2009-07-20T20:49:24Z | [
"python",
"xml",
"xpath"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 1,155,636 | <p>Regular expressions maybe?</p>
<pre><code>import re
my_string = "Mary had a little lamb"
len(re.findall("a", my_string))
</code></pre>
| 31 | 2009-07-20T20:03:19Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 1,155,647 | <blockquote>
<p><a href="https://docs.python.org/2/library/stdtypes.html#str.count" rel="nofollow">str.count(sub[, start[, end]])</a></p>
<p>Return the number of non-overlapping occurrences of substring <code>sub</code> in the range <code>[start, end]</code>. Optional arguments <code>start</code> and <code>end</... | 617 | 2009-07-20T20:04:26Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 1,155,648 | <p>You can use <a href="https://docs.python.org/2/library/string.html#string.count" rel="nofollow">count()</a> :</p>
<pre><code>>>> 'Mary had a little lamb'.count('a')
4
</code></pre>
| 76 | 2009-07-20T20:04:45Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 1,155,649 | <pre><code>myString.count('a');
</code></pre>
<p>more info <a href="http://docs.python.org/library/stdtypes.html">here</a></p>
| 20 | 2009-07-20T20:04:47Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 1,155,652 | <pre><code>"aabc".count("a")
</code></pre>
| 13 | 2009-07-20T20:05:03Z | [
"python",
"string",
"count"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.