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 |
|---|---|---|---|---|---|---|---|---|---|
Why saving of MSWord document can silently fail? | 1,340,950 | <p>I need to change some custom properties values in many files. Here is an example of code - how I do it for a single file:</p>
<pre><code>import win32com.client
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = False
doc = MSWord.Documents.Open(file)
doc.CustomDocumentProperties('Some Property... | 3 | 2009-08-27T13:02:27Z | 1,341,028 | <p>It fails silently since you ignore errors (<code>except: pass</code>).</p>
<p>The most common reason why saving a Word file usually fails is that it's open in Word.</p>
| 0 | 2009-08-27T13:14:58Z | [
"python",
"automation",
"ms-word",
"ole"
] |
Why saving of MSWord document can silently fail? | 1,340,950 | <p>I need to change some custom properties values in many files. Here is an example of code - how I do it for a single file:</p>
<pre><code>import win32com.client
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = False
doc = MSWord.Documents.Open(file)
doc.CustomDocumentProperties('Some Property... | 3 | 2009-08-27T13:02:27Z | 1,341,031 | <p>you're saving file only if <code>Value</code> was successfully changed. May be you could try to remove <code>try</code>-<code>except</code> clause and see what is actually happening when you're file is not saved. And, btw, using bare <code>except</code> is not a good practice.</p>
| 1 | 2009-08-27T13:15:17Z | [
"python",
"automation",
"ms-word",
"ole"
] |
Why saving of MSWord document can silently fail? | 1,340,950 | <p>I need to change some custom properties values in many files. Here is an example of code - how I do it for a single file:</p>
<pre><code>import win32com.client
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = False
doc = MSWord.Documents.Open(file)
doc.CustomDocumentProperties('Some Property... | 3 | 2009-08-27T13:02:27Z | 1,361,808 | <p>You were using the <code>CustomDocumentProperties</code> in the wrong way, and as other people pointed out, you could not see it, because you were swallowing the exception.</p>
<p>Moreover - and here I could not find anything in the documentation - the <code>Saved</code> property was not reset while changing proper... | 3 | 2009-09-01T11:00:32Z | [
"python",
"automation",
"ms-word",
"ole"
] |
How do I get PyParsing set up on the Google App Engine? | 1,341,137 | <p>I saw on the Google App Engine documentation that <a href="http://www.antlr.org/" rel="nofollow">http://www.antlr.org/</a> Antlr3 is used as the parsing third party library.</p>
<p>But from what I know Pyparsing seems to be the easier to use and I am only aiming to parse some simple syntax. </p>
<p>Is there an alt... | 1 | 2009-08-27T13:35:21Z | 1,341,822 | <p>"Just do it"!-) Get pyparsing.py, e.g. from <a href="http://pyparsing.svn.sourceforge.net/viewvc/pyparsing/src/pyparsing.py?revision=181" rel="nofollow">here</a>, and put it in your app engine app's directory; now you can just <code>import pyparsing</code> in your app code and use it.</p>
<p>For example, tweak the... | 1 | 2009-08-27T15:27:32Z | [
"python",
"google-app-engine",
"pyparsing"
] |
How do I get PyParsing set up on the Google App Engine? | 1,341,137 | <p>I saw on the Google App Engine documentation that <a href="http://www.antlr.org/" rel="nofollow">http://www.antlr.org/</a> Antlr3 is used as the parsing third party library.</p>
<p>But from what I know Pyparsing seems to be the easier to use and I am only aiming to parse some simple syntax. </p>
<p>Is there an alt... | 1 | 2009-08-27T13:35:21Z | 1,351,445 | <p>Pyparsing's runtime footprint is intentionally small for just this purpose. It is a single source file, pyparsing.py, so just drop it in amongst your own source files and parse away!</p>
<p>-- Paul</p>
| 4 | 2009-08-29T13:29:24Z | [
"python",
"google-app-engine",
"pyparsing"
] |
python: dictionaries of lists are somehow coupled | 1,341,208 | <p>I wrote a small python program to iterate over data file (*input_file*) and perform calculations. If calculation result reaches certain states (<em>stateA</em> or <em>stateB</em>), information (<em>hits</em>) are extracted from the results. The hits to extract depend on parameters from three parameter sets.<br />
I ... | 1 | 2009-08-27T13:45:37Z | 1,341,240 | <p>Dictionaries and lists are passed around by reference by default. For a dictionary, instead of:</p>
<pre><code>hits_old = hits # just for testing purpose
</code></pre>
<p>it would be:</p>
<pre><code>hits_old = hits.copy() # just for testing purpose
</code></pre>
<p>This will copy the dictionary's key/... | 4 | 2009-08-27T13:50:08Z | [
"python",
"variables",
"dictionary"
] |
python: dictionaries of lists are somehow coupled | 1,341,208 | <p>I wrote a small python program to iterate over data file (*input_file*) and perform calculations. If calculation result reaches certain states (<em>stateA</em> or <em>stateB</em>), information (<em>hits</em>) are extracted from the results. The hits to extract depend on parameters from three parameter sets.<br />
I ... | 1 | 2009-08-27T13:45:37Z | 1,341,718 | <p>Your line:</p>
<pre><code>hits = dict.fromkeys(param_sets, [])
</code></pre>
<p>is equivalent to:</p>
<pre><code>hits = dict()
onelist = []
for k in param_sets:
hits[k] = onelist
</code></pre>
<p>That is, every entry in <code>hits</code> has as its value the SAME list object, initially empty, no matter what ... | 8 | 2009-08-27T15:10:05Z | [
"python",
"variables",
"dictionary"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 1,342,036 | <pre><code>s.replace(u'Ã ', '') # u before string is important
</code></pre>
<p>and make your <code>.py</code> file unicode.</p>
| 1 | 2009-08-27T15:58:37Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 1,342,042 | <pre><code>>>> unicode_string = u"hello aåbäcö"
>>> unicode_string.encode("ascii", "ignore")
'hello abc'
</code></pre>
| 24 | 2009-08-27T15:59:53Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 1,342,068 | <pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-
s = u"6Ã 918Ã 417Ã 712"
s = s.replace(u"Ã", "")
print s
</code></pre>
<p>This will print out <code>6 918 417 712</code></p>
| 3 | 2009-08-27T16:03:47Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 1,342,079 | <p>Python 2 uses <code>ascii</code> as the default encoding for source files, which means you must specify another encoding at the top of the file to use non-ascii unicode characters in literals. Python 3 uses <code>utf-8</code> as the default encoding for source files, so this is less of an issue.</p>
<p>See:
<a href... | 46 | 2009-08-27T16:04:51Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 1,342,373 | <pre><code>def removeNonAscii(s): return "".join(filter(lambda x: ord(x)<128, s))
</code></pre>
<p>edit: my first impulse is always to use a filter, but the generator expression is more memory efficient (and shorter)...</p>
<pre><code>def removeNonAscii(s): return "".join(i for i in s if ord(i)<128)
</code></pr... | 148 | 2009-08-27T16:57:23Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 1,342,407 | <p>This is a dirty hack, but may work.</p>
<pre><code>s2 = ""
for i in s:
if ord(i) < 128:
s2 += i
</code></pre>
| 1 | 2009-08-27T17:02:11Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 1,343,125 | <p>Using Regex:</p>
<pre><code>import re
strip_unicode = re.compile("([^-_a-zA-Z0-9!@#%&=,/'\";:~`\$\^\*\(\)\+\[\]\.\{\}\|\?\<\>\\]+|[^\s]+)")
print strip_unicode.sub('', u'6Ã 918Ã 417Ã 712')
</code></pre>
| 7 | 2009-08-27T18:54:33Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 6,113,618 | <p>I know it's an old thread, but I felt compelled to mention the translate method, which is always a good way to replace all character codes above 128 (or other if necessary).</p>
<p><em>Usage</em> : str.<strong>translate</strong>(<em>table[, deletechars]</em>)</p>
<pre><code>>>> trans_table = ''.join( [chr... | 2 | 2011-05-24T16:24:34Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 8,045,344 | <p>For what it was worth, my character set was <code>utf-8</code> and I had included the classic "<code># -*- coding: utf-8 -*-</code>" line.</p>
<p>However, I discovered that I didn't have Universal Newlines when reading this data from a webpage. </p>
<p>My text had two words, separated by "<code>\r\n</code>". I was... | 0 | 2011-11-08T02:41:10Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 8,045,715 | <p>Way too late for an answer, but the original string was in UTF-8 and '\xc2\xa0' is UTF-8 for NO-BREAK SPACE. Simply decode the original string as <code>s.decode('utf-8')</code> (\xa0 displays as a space when decoded incorrectly as Windows-1252 or latin-1:</p>
<h3>Example (Python 3)</h3>
<pre><code>s = b'6\xc2\xa0... | 3 | 2011-11-08T03:48:31Z | [
"python",
"unicode"
] |
How to make the python interpreter correctly handle non-ASCII characters in string operations? | 1,342,000 | <p>I have a string that looks like so:</p>
<pre><code>6ÃÂ 918ÃÂ 417ÃÂ 712
</code></pre>
<p>The clear cut way to trim this string (as I understand Python) is simply to say the string is in a variable called <code>s</code>, we get:</p>
<pre><code>s.replace('Ã ', '')
</code></pre>
<p>That should do the trick. But ... | 74 | 2009-08-27T15:53:31Z | 10,268,212 | <p>The following code will replace all non ASCII characters with question marks.</p>
<pre><code>"".join([x if ord(x) < 128 else '?' for x in s])
</code></pre>
| 14 | 2012-04-22T13:12:10Z | [
"python",
"unicode"
] |
Problem with python and __import__ | 1,342,128 | <p>Sorry for the generic title, will change it once I understand the source of my problem
I have the following structure:</p>
<pre><code>foo/
foo/__init__.py
foo/bar/
foo/bar/__init__.py
foo/bar/some_module.py
</code></pre>
<p>When I try to import some_module by doing so:</p>
<pre><code>from foo.bar import some_modu... | 1 | 2009-08-27T16:12:05Z | 1,342,164 | <p>I believe the proper way to do this is:</p>
<pre><code>mod = __import__('foo.bar', fromlist = ['some_module'])
</code></pre>
<p>This way even the 'foo.bar' part can be changed at runtime.
As a result <code>some_module</code>will be available as <code>mod.some_module</code>; use getattr if you want it in a separat... | 7 | 2009-08-27T16:18:23Z | [
"python",
"import"
] |
Problem with python and __import__ | 1,342,128 | <p>Sorry for the generic title, will change it once I understand the source of my problem
I have the following structure:</p>
<pre><code>foo/
foo/__init__.py
foo/bar/
foo/bar/__init__.py
foo/bar/some_module.py
</code></pre>
<p>When I try to import some_module by doing so:</p>
<pre><code>from foo.bar import some_modu... | 1 | 2009-08-27T16:12:05Z | 1,342,177 | <p>From the <a href="http://docs.python.org/library/functions.html#%5F%5Fimport%5F%5F" rel="nofollow">docs</a>:</p>
<blockquote>
<p>Direct use of <code>__import__()</code> is rare, except in cases where you want to import a module whose name is only known at runtime.</p>
</blockquote>
<p>However, the dotted notatio... | 0 | 2009-08-27T16:20:09Z | [
"python",
"import"
] |
Problem with python and __import__ | 1,342,128 | <p>Sorry for the generic title, will change it once I understand the source of my problem
I have the following structure:</p>
<pre><code>foo/
foo/__init__.py
foo/bar/
foo/bar/__init__.py
foo/bar/some_module.py
</code></pre>
<p>When I try to import some_module by doing so:</p>
<pre><code>from foo.bar import some_modu... | 1 | 2009-08-27T16:12:05Z | 1,342,243 | <pre><code>from foo.bar import *
</code></pre>
<p>is a bad practice since it imports <code>some_module</code> into the global scope. </p>
<p>You should be able to access your module through:</p>
<pre><code>import foo.bar
mod = getattr(foo.bar, 'some_module')
</code></pre>
<p>It can be easily demonstrated that this ... | 1 | 2009-08-27T16:32:20Z | [
"python",
"import"
] |
nose tests of Pylons app with models in init_model? | 1,342,232 | <p>I have a stock Pylons app created using <code>paster create -t pylons</code> with one controller and matched functional test, added using <code>paster controller</code>, and a SQLAlchemy table and mapped ORM class. The SQLAlchemy stuff is defined in the <code>init_model()</code> function rather than in module scope ... | 1 | 2009-08-27T16:29:18Z | 1,344,479 | <p>I would try debugging your nosetest run. Why not put:</p>
<pre><code>import pdb;pdb.set_trace()
</code></pre>
<p>in the <code>init_model()</code> function and see how it is getting invoked more than once.</p>
<p>With PDB running you can see the stack trace using the <code>where</code> command:</p>
<pre><code>w(h... | 3 | 2009-08-28T00:43:46Z | [
"python",
"sqlalchemy",
"pylons",
"nose",
"nosetests"
] |
Can you really use the Visual Studio 2008 IDE to code in Python? | 1,342,377 | <p>I have a friend who I am trying to teach how to program. He comes from a very basic PHP background, and for some reason is ANTI C#, I guess because some of his PHP circles condemn anything that comes from Microsoft.</p>
<p>Anyways - I've told him its possible to use either Ruby or Python with the VS2008 IDE, becaus... | 2 | 2009-08-27T16:57:39Z | 1,342,391 | <p>I don't know why you would want to - perhaps something like <a href="http://www.codeplex.com/IronPythonStudio" rel="nofollow">IronPython Studio</a> would be a happy medium. But as I said I don't know why you would want to use Visual Studio for Python development when there are <a href="http://wiki.python.org/moin/I... | 1 | 2009-08-27T16:59:49Z | [
"python",
"visual-studio",
"ironpython"
] |
Can you really use the Visual Studio 2008 IDE to code in Python? | 1,342,377 | <p>I have a friend who I am trying to teach how to program. He comes from a very basic PHP background, and for some reason is ANTI C#, I guess because some of his PHP circles condemn anything that comes from Microsoft.</p>
<p>Anyways - I've told him its possible to use either Ruby or Python with the VS2008 IDE, becaus... | 2 | 2009-08-27T16:57:39Z | 1,342,405 | <p>This has been discussed before <a href="http://stackoverflow.com/questions/537689/python-ide-built-into-visual-studio-2008">in this thread.</a> I personally prefer eclipse and pyDev.</p>
| 1 | 2009-08-27T17:02:05Z | [
"python",
"visual-studio",
"ironpython"
] |
Can you really use the Visual Studio 2008 IDE to code in Python? | 1,342,377 | <p>I have a friend who I am trying to teach how to program. He comes from a very basic PHP background, and for some reason is ANTI C#, I guess because some of his PHP circles condemn anything that comes from Microsoft.</p>
<p>Anyways - I've told him its possible to use either Ruby or Python with the VS2008 IDE, becaus... | 2 | 2009-08-27T16:57:39Z | 1,342,421 | <p>If you want to use Python together with the .NET Common Language Runtime, then you want one of:</p>
<ul>
<li><a href="http://pythonnet.sourceforge.net/" rel="nofollow">Python.NET</a> (extension to vanilla Python that adds .NET support)</li>
<li><a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython"... | 8 | 2009-08-27T17:03:58Z | [
"python",
"visual-studio",
"ironpython"
] |
Can you really use the Visual Studio 2008 IDE to code in Python? | 1,342,377 | <p>I have a friend who I am trying to teach how to program. He comes from a very basic PHP background, and for some reason is ANTI C#, I guess because some of his PHP circles condemn anything that comes from Microsoft.</p>
<p>Anyways - I've told him its possible to use either Ruby or Python with the VS2008 IDE, becaus... | 2 | 2009-08-27T16:57:39Z | 1,342,463 | <p>I find it odd that your friend is against C# but is ok with Visual Studio. There is, after all, an open source development environment for .NET called SharpDevelop. The C# language is a standard. .NET is free (as in beer) and there is an open source implementation of that platform called Mono. The only "un-free"... | 2 | 2009-08-27T17:11:27Z | [
"python",
"visual-studio",
"ironpython"
] |
Can you really use the Visual Studio 2008 IDE to code in Python? | 1,342,377 | <p>I have a friend who I am trying to teach how to program. He comes from a very basic PHP background, and for some reason is ANTI C#, I guess because some of his PHP circles condemn anything that comes from Microsoft.</p>
<p>Anyways - I've told him its possible to use either Ruby or Python with the VS2008 IDE, becaus... | 2 | 2009-08-27T16:57:39Z | 1,343,191 | <p>Firstly, there seems to be a question as to whether python (or various implementations) are as 'powerful' as C#. I'm not quite sure what to take powerful to mean, but in my experience of both languages it will be somewhat easier and faster to write a given piece of code in python than in C#. C# is faster than cpytho... | 1 | 2009-08-27T19:07:51Z | [
"python",
"visual-studio",
"ironpython"
] |
Can you really use the Visual Studio 2008 IDE to code in Python? | 1,342,377 | <p>I have a friend who I am trying to teach how to program. He comes from a very basic PHP background, and for some reason is ANTI C#, I guess because some of his PHP circles condemn anything that comes from Microsoft.</p>
<p>Anyways - I've told him its possible to use either Ruby or Python with the VS2008 IDE, becaus... | 2 | 2009-08-27T16:57:39Z | 1,343,342 | <p>Go <a href="http://knowbody.livejournal.com/15675.html" rel="nofollow">here</a> for a discussion on the Visual Studio IronPython IDEs.</p>
| 0 | 2009-08-27T19:34:19Z | [
"python",
"visual-studio",
"ironpython"
] |
write special characters into excel table by python package pyExcelerator/xlwt | 1,342,402 | <p>Task:<br />
I generate formated excel tables from csv-files by using the python package pyExcelerator (comparable with xlwt). I need to be able to write less-than-or-equal-to (â¤) and greater-than-or-equal-to (â¥) signs.</p>
<p>So far:<br />
I can save my table as csv-files with UTF-8 encoding, so that I can view... | 4 | 2009-08-27T17:01:41Z | 1,349,666 | <p>You may be overthinking the problem. The font shouldn't play into the matter, although character encoding might.</p>
<p>In any case, I was able to use xlwt to create an excel spreadsheet with less-than-equal and greater-than-equal signs with the following script:</p>
<pre><code>import xlwt
wb = xlwt.Workbook()
ws... | 4 | 2009-08-28T22:19:38Z | [
"python",
"excel",
"character-encoding",
"special-characters",
"xlwt"
] |
write special characters into excel table by python package pyExcelerator/xlwt | 1,342,402 | <p>Task:<br />
I generate formated excel tables from csv-files by using the python package pyExcelerator (comparable with xlwt). I need to be able to write less-than-or-equal-to (â¤) and greater-than-or-equal-to (â¥) signs.</p>
<p>So far:<br />
I can save my table as csv-files with UTF-8 encoding, so that I can view... | 4 | 2009-08-27T17:01:41Z | 1,349,905 | <p>(1) Re: """
I can save my table as csv-files with UTF-8 encoding, so that I can view the special characters in my text editor, by adding the following line to my python source code:</p>
<p><code>#! /usr/bin/env python</code><br />
<code># -*- coding: UTF-8 -*-</code><br />
"""</p>
<p>Being able to write a file wit... | 1 | 2009-08-28T23:39:36Z | [
"python",
"excel",
"character-encoding",
"special-characters",
"xlwt"
] |
write special characters into excel table by python package pyExcelerator/xlwt | 1,342,402 | <p>Task:<br />
I generate formated excel tables from csv-files by using the python package pyExcelerator (comparable with xlwt). I need to be able to write less-than-or-equal-to (â¤) and greater-than-or-equal-to (â¥) signs.</p>
<p>So far:<br />
I can save my table as csv-files with UTF-8 encoding, so that I can view... | 4 | 2009-08-27T17:01:41Z | 2,477,641 | <p>This should help with writing <code>UTF-8</code> chars using <strike>pyexcelerator or</strike> xlwt:</p>
<pre><code>wb = xlwt.Workbook(**encoding='utf-8'**)
</code></pre>
<p>edit:</p>
<p>Seems it's not working for pyexcelerator, but I havent confirmed it.</p>
| 4 | 2010-03-19T13:31:24Z | [
"python",
"excel",
"character-encoding",
"special-characters",
"xlwt"
] |
Slow regex in Python? | 1,342,589 | <p>I'm trying to match these kinds of strings</p>
<pre><code>{@csm.foo.bar}
</code></pre>
<p>without matching any of these</p>
<pre><code>{@csm.foo.bar-@csm.ooga.booga}
{@csm.foo.bar-42}
</code></pre>
<p>The regex I use is</p>
<pre><code>r"\{@csm.((?:[a-zA-Z0-9_]+\.?)+)\}"
</code></pre>
<p>It gets dog slow if the... | 0 | 2009-08-27T17:34:37Z | 1,342,646 | <p>I'm not exactly a regex expert, but it might be due to the brace at the end of the match. You might try to match <code>r"\{@csm.((?:[a-zA-Z0-9_]+\.?)+)"</code> and just check manually whether a closing brace occurs at the end or not.</p>
| 0 | 2009-08-27T17:43:15Z | [
"python",
"regex"
] |
Slow regex in Python? | 1,342,589 | <p>I'm trying to match these kinds of strings</p>
<pre><code>{@csm.foo.bar}
</code></pre>
<p>without matching any of these</p>
<pre><code>{@csm.foo.bar-@csm.ooga.booga}
{@csm.foo.bar-42}
</code></pre>
<p>The regex I use is</p>
<pre><code>r"\{@csm.((?:[a-zA-Z0-9_]+\.?)+)\}"
</code></pre>
<p>It gets dog slow if the... | 0 | 2009-08-27T17:34:37Z | 1,342,688 | <p>Can you supply a test case of a string for which the first match is "dog slow"? BTW, though I don't know if that matters to performance, there's an imprecision in the RE -- it matches any single character after the <code>{@csm</code> start, not just a dot; maybe a better expression (possibly faster as it doesn't mak... | 4 | 2009-08-27T17:49:08Z | [
"python",
"regex"
] |
Slow regex in Python? | 1,342,589 | <p>I'm trying to match these kinds of strings</p>
<pre><code>{@csm.foo.bar}
</code></pre>
<p>without matching any of these</p>
<pre><code>{@csm.foo.bar-@csm.ooga.booga}
{@csm.foo.bar-42}
</code></pre>
<p>The regex I use is</p>
<pre><code>r"\{@csm.((?:[a-zA-Z0-9_]+\.?)+)\}"
</code></pre>
<p>It gets dog slow if the... | 0 | 2009-08-27T17:34:37Z | 1,342,809 | <p>You probably need to give a better example of exactly what's slow. For a reasonably long string containing stuff that does and doesn't match:</p>
<pre><code>x="".join(['{@csm.foo.bar-%d}\n{@csm.foo.%dx.baz}\n' % (a,a)
for a in xrange(10000)])
mymatch=r"\{@csm.((?:[a-zA-Z0-9_]+\.?)+)\}"
for y in re.find... | 0 | 2009-08-27T18:07:26Z | [
"python",
"regex"
] |
In Django, how to control which DB connection and cursor a queryset will use | 1,342,594 | <p>I'm trying to get a queryset to issue its query over a different DB connection, using a different cursor class. Does anyone know if that's possible and if so how it might be done? In psuedo-code:</p>
<pre><code> # setup a new db connection:
db = db_connect(cursorclass=AlternateCursor)
# setup a generic quer... | 3 | 2009-08-27T17:35:36Z | 1,343,429 | <p>This is possible from Django 1.0 on - the trick is to use a custom manager for your model and replace the manager's connection object. See the code at Eric Florenzano's post at <a href="http://www.eflorenzano.com/blog/post/easy-multi-database-support-django/" rel="nofollow">http://www.eflorenzano.com/blog/post/easy-... | 2 | 2009-08-27T19:52:37Z | [
"python",
"django",
"database"
] |
Pythonic way of checking if a condition holds for any element of a list | 1,342,601 | <p>I have a list in Python, and I want to check if any elements are negative. Specman has the <code>has()</code> method for lists which does:</p>
<pre><code>x: list of uint;
if (x.has(it < 0)) {
// do something
};
</code></pre>
<p>Where <code>it</code> is a Specman keyword mapped to each element of the list i... | 29 | 2009-08-27T17:36:55Z | 1,342,617 | <p><a href="http://docs.python.org/library/functions.html#any">any()</a>:</p>
<pre><code>if any(t < 0 for t in x):
# do something
</code></pre>
<p>Also, if you're going to use "True in ...", make it a generator expression so it doesn't take O(n) memory:</p>
<pre><code>if True in (t < 0 for t in x):
</code>... | 65 | 2009-08-27T17:38:53Z | [
"python",
"list"
] |
Pythonic way of checking if a condition holds for any element of a list | 1,342,601 | <p>I have a list in Python, and I want to check if any elements are negative. Specman has the <code>has()</code> method for lists which does:</p>
<pre><code>x: list of uint;
if (x.has(it < 0)) {
// do something
};
</code></pre>
<p>Where <code>it</code> is a Specman keyword mapped to each element of the list i... | 29 | 2009-08-27T17:36:55Z | 1,342,622 | <p>Use <code>any()</code>.</p>
<pre><code>if any(t < 0 for t in x):
# do something
</code></pre>
| 18 | 2009-08-27T17:39:10Z | [
"python",
"list"
] |
Pythonic way of checking if a condition holds for any element of a list | 1,342,601 | <p>I have a list in Python, and I want to check if any elements are negative. Specman has the <code>has()</code> method for lists which does:</p>
<pre><code>x: list of uint;
if (x.has(it < 0)) {
// do something
};
</code></pre>
<p>Where <code>it</code> is a Specman keyword mapped to each element of the list i... | 29 | 2009-08-27T17:36:55Z | 1,342,629 | <p>Python has a built in <a href="http://docs.python.org/library/functions.html#any">any()</a> function for exactly this purpose.</p>
| 8 | 2009-08-27T17:40:26Z | [
"python",
"list"
] |
Is it possible to reference the output of an IronPython project from within a c# project? | 1,342,645 | <p>I would like to build a code library in IronPython and have another C# project reference it. Can I do this? How?</p>
<p>Is this just as simple as building the project and referencing the dll? Is there any conflict with the dynamic aspect of it?</p>
| 0 | 2009-08-27T17:43:09Z | 1,380,583 | <p>There is currently no way to build <a href="http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx" rel="nofollow">CLS</a>-compliant assemblies from IronPython. The <code>pyc</code> tool will generate a DLL from Python code, but it's really only useful from IronPython.</p>
<p>If you want to use IronPython from a C# ... | 1 | 2009-09-04T17:34:00Z | [
"c#",
"python",
"assemblies",
"ironpython"
] |
Django empty field fallback | 1,342,679 | <p>I have a model that holds user address. This model has to have first_name and last_name fields since one would like to set address to a recipient (like his company etc.). What I'm trying to achieve is:</p>
<ul>
<li>if the fist_name/last_name field in the address is filled - return simply that field</li>
<li>if the ... | 3 | 2009-08-27T17:47:49Z | 1,342,817 | <p>There are two options here. The first is to create a method to look it up dynamically, but use the <code>property</code> decorator so that other code can still use straight attribute access.</p>
<pre><code>class MyModel(models.Model):
_first_name = models.CharField(max_length=100, db_column='first_name')
@... | 5 | 2009-08-27T18:09:17Z | [
"python",
"django",
"django-models",
"models",
"field"
] |
How can I force subtraction to be signed in Python? | 1,342,782 | <p>You can skip to the bottom line if you don't care about the background:</p>
<p>I have the following code in Python:</p>
<pre><code>ratio = (point.threshold - self.points[0].value) / (self.points[1].value - self.points[0].value)
</code></pre>
<p>Which is giving me wrong values. For instance, for:</p>
<pre><code>... | 0 | 2009-08-27T18:03:38Z | 1,342,833 | <p>Well, the obvious solution would probably be to cast to floats:</p>
<pre><code>ratio = (float(point.threshold) - float(self.points[0].value)) / (float(self.points[1].value) - float(self.points[0].value))
</code></pre>
<p>Or I suppose you could cast to one of the numpy signed types.</p>
| 2 | 2009-08-27T18:11:59Z | [
"python",
"sign",
"subtraction",
"uint"
] |
How can I force subtraction to be signed in Python? | 1,342,782 | <p>You can skip to the bottom line if you don't care about the background:</p>
<p>I have the following code in Python:</p>
<pre><code>ratio = (point.threshold - self.points[0].value) / (self.points[1].value - self.points[0].value)
</code></pre>
<p>Which is giving me wrong values. For instance, for:</p>
<pre><code>... | 0 | 2009-08-27T18:03:38Z | 1,342,899 | <p>Almost anything <strong>but</strong> uints will work here, so just cast these to something else before you do the subtraction. </p>
<p>Since threshold = 25.0 (note the decimal point), it's a float, so the subtraction and division will all work as long as you're not using uints.</p>
| 2 | 2009-08-27T18:24:35Z | [
"python",
"sign",
"subtraction",
"uint"
] |
How can I force subtraction to be signed in Python? | 1,342,782 | <p>You can skip to the bottom line if you don't care about the background:</p>
<p>I have the following code in Python:</p>
<pre><code>ratio = (point.threshold - self.points[0].value) / (self.points[1].value - self.points[0].value)
</code></pre>
<p>Which is giving me wrong values. For instance, for:</p>
<pre><code>... | 0 | 2009-08-27T18:03:38Z | 1,342,902 | <p>Do those values actually NEED to be uint16 instead of int16? Unless they have to be able to take values of <code>2**15</code> or above (but still below <code>2**16</code>) you could simply keep them as int16 and be done with it -- unsigned ints, as you discovered, can be tricky (and not just in numpy;-). If you DO ... | 0 | 2009-08-27T18:24:44Z | [
"python",
"sign",
"subtraction",
"uint"
] |
How does python decide whether a parameter is a reference or a value? | 1,342,953 | <p>In C++, <code>void somefunction(int)</code> passes a value, while <code>void somefunction(int&)</code> passes a reference. In Java, primitives are passed by value, while objects are passed by reference. How does python make this decision?</p>
<p>Edit: Since everything is passed by reference, why does this:</p>
... | 4 | 2009-08-27T18:31:35Z | 1,342,974 | <p>Everything is passed by reference. Everything is an object, too.</p>
| 1 | 2009-08-27T18:33:27Z | [
"python",
"pointers",
"reference"
] |
How does python decide whether a parameter is a reference or a value? | 1,342,953 | <p>In C++, <code>void somefunction(int)</code> passes a value, while <code>void somefunction(int&)</code> passes a reference. In Java, primitives are passed by value, while objects are passed by reference. How does python make this decision?</p>
<p>Edit: Since everything is passed by reference, why does this:</p>
... | 4 | 2009-08-27T18:31:35Z | 1,342,976 | <p>It passes everything by reference. Even when you specify a numeric value, it is a reference against a table containing that value. This is the difference between static and dynamic languages. The type stays with the value, not with the container, and variables are just references towards a "value space" where all va... | 11 | 2009-08-27T18:33:36Z | [
"python",
"pointers",
"reference"
] |
How does python decide whether a parameter is a reference or a value? | 1,342,953 | <p>In C++, <code>void somefunction(int)</code> passes a value, while <code>void somefunction(int&)</code> passes a reference. In Java, primitives are passed by value, while objects are passed by reference. How does python make this decision?</p>
<p>Edit: Since everything is passed by reference, why does this:</p>
... | 4 | 2009-08-27T18:31:35Z | 1,343,052 | <p>In response to your edit, it is because integers are immutable in Python. So <code>a</code> is not changed for the same reason it is not changed when running this code:</p>
<pre><code>a = 4
num = a
num *= 2
print(a)
</code></pre>
<p>You aren't changing <code>num</code> (and therefore <code>a</code>) in place, you... | 3 | 2009-08-27T18:43:10Z | [
"python",
"pointers",
"reference"
] |
How does python decide whether a parameter is a reference or a value? | 1,342,953 | <p>In C++, <code>void somefunction(int)</code> passes a value, while <code>void somefunction(int&)</code> passes a reference. In Java, primitives are passed by value, while objects are passed by reference. How does python make this decision?</p>
<p>Edit: Since everything is passed by reference, why does this:</p>
... | 4 | 2009-08-27T18:31:35Z | 1,343,071 | <p>Arguments are actually passed by value. The function is passed the object the variable refers to, not the variable itself. A function cannot rebind a caller's variables. A function cannot change an immutable object, but can change (request changes to) a mutable one.</p>
| 3 | 2009-08-27T18:45:55Z | [
"python",
"pointers",
"reference"
] |
How does python decide whether a parameter is a reference or a value? | 1,342,953 | <p>In C++, <code>void somefunction(int)</code> passes a value, while <code>void somefunction(int&)</code> passes a reference. In Java, primitives are passed by value, while objects are passed by reference. How does python make this decision?</p>
<p>Edit: Since everything is passed by reference, why does this:</p>
... | 4 | 2009-08-27T18:31:35Z | 1,343,188 | <p>There is disagreement on terminology here. In the Java community, they say that everything is passed by value: primitives are passed by value; references are passed by value. (Just search this site for Java and pass by reference if you don't believe this.) Note that "objects" are not values in the language; only ref... | 9 | 2009-08-27T19:07:23Z | [
"python",
"pointers",
"reference"
] |
How does python decide whether a parameter is a reference or a value? | 1,342,953 | <p>In C++, <code>void somefunction(int)</code> passes a value, while <code>void somefunction(int&)</code> passes a reference. In Java, primitives are passed by value, while objects are passed by reference. How does python make this decision?</p>
<p>Edit: Since everything is passed by reference, why does this:</p>
... | 4 | 2009-08-27T18:31:35Z | 1,346,078 | <p>This is not really about the function call semantics but the assignment semantics. In Python assignment is done by rebinding the reference, not by overwriting the original object. This is why the example code prints 4 instead of 8 - it has nothing to do with mutability of objects as such, more that the *= operator i... | 1 | 2009-08-28T10:06:41Z | [
"python",
"pointers",
"reference"
] |
Python Packages? | 1,342,975 | <p>Ok, I think whatever I'm doing wrong, it's probably blindingly obvious, but I can't figure it out. I've read and re-read the tutorial section on packages and the only thing I can figure is that this won't work because I'm executing it directly. Here's the directory setup:</p>
<pre><code>eulerproject/
__init__.py
... | 9 | 2009-08-27T18:33:28Z | 1,343,149 | <p>I had the same problem. I now use <a href="http://nose.readthedocs.org/en/latest/" rel="nofollow">nose</a> to run my tests, and relative imports are correctly handled.</p>
<p>Yeah, this whole relative import thing is confusing.</p>
| 8 | 2009-08-27T18:58:48Z | [
"python",
"unit-testing",
"packages"
] |
Python Packages? | 1,342,975 | <p>Ok, I think whatever I'm doing wrong, it's probably blindingly obvious, but I can't figure it out. I've read and re-read the tutorial section on packages and the only thing I can figure is that this won't work because I'm executing it directly. Here's the directory setup:</p>
<pre><code>eulerproject/
__init__.py
... | 9 | 2009-08-27T18:33:28Z | 1,343,173 | <p>Generally you would have a directory, the name of which is your package name, somewhere on your PYTHONPATH. For example:</p>
<pre><code>eulerproject/
euler/
__init__.py
euler1.py
...
tests/
...
setup.py
</code></pre>
<p>Then, you can either install this systemwid... | 8 | 2009-08-27T19:03:55Z | [
"python",
"unit-testing",
"packages"
] |
Can Python's logging format be modified depending on the message log level? | 1,343,227 | <p>I'm using Python's <code>logging</code> mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "in... | 38 | 2009-08-27T19:13:52Z | 1,343,273 | <p>Yes, you can do this by having a custom <code>Formatter</code> class:</p>
<pre><code>class MyFormatter(logging.Formatter):
def format(self, record):
#compute s according to record.levelno
#for example, by setting self._fmt
#according to the levelno, then calling
#the superclass t... | 25 | 2009-08-27T19:20:42Z | [
"python",
"logging"
] |
Can Python's logging format be modified depending on the message log level? | 1,343,227 | <p>I'm using Python's <code>logging</code> mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "in... | 38 | 2009-08-27T19:13:52Z | 8,349,076 | <p>I just ran into this issue and had trouble filling in the "holes" left in the above example. Here's a more complete, working version that I used. Hopefully this helps someone:</p>
<pre><code># Custom formatter
class MyFormatter(logging.Formatter):
err_fmt = "ERROR: %(msg)s"
dbg_fmt = "DBG: %(module)s: ... | 30 | 2011-12-01T22:15:30Z | [
"python",
"logging"
] |
Can Python's logging format be modified depending on the message log level? | 1,343,227 | <p>I'm using Python's <code>logging</code> mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "in... | 38 | 2009-08-27T19:13:52Z | 14,520,171 | <p>And again like JS answer but more compact.</p>
<pre><code>class SpecialFormatter(logging.Formatter):
FORMATS = {logging.DEBUG :"DBG: %(module)s: %(lineno)d: %(message)s",
logging.ERROR : "ERROR: %(message)s",
logging.INFO : "%(message)s",
'DEFAULT' : "%(levelname)s: ... | 9 | 2013-01-25T10:44:12Z | [
"python",
"logging"
] |
Can Python's logging format be modified depending on the message log level? | 1,343,227 | <p>I'm using Python's <code>logging</code> mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "in... | 38 | 2009-08-27T19:13:52Z | 16,660,369 | <p>This is an adaptation of <a href="http://stackoverflow.com/a/14520171/760767">estani's answer</a> to the new implementation of <code>logging.Formatter</code> which now relies on formatting styles. Mine relies on <code>'{'</code> style format, but it can be adapted. Could be refined to be more general and allow selec... | 6 | 2013-05-21T00:37:39Z | [
"python",
"logging"
] |
Can Python's logging format be modified depending on the message log level? | 1,343,227 | <p>I'm using Python's <code>logging</code> mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "in... | 38 | 2009-08-27T19:13:52Z | 21,943,339 | <p>The above solution works with 3.3.3 release.
However with 3.3.4 you get the following error.</p>
<pre><code>FORMATS = { logging.DEBUG : logging._STYLES['{']("{module} DEBUG: {lineno}: {message}"),
</code></pre>
<p>TypeError: 'tuple' object is not callable</p>
<p>After some searching around in the logging class
L... | 2 | 2014-02-21T19:19:27Z | [
"python",
"logging"
] |
Can Python's logging format be modified depending on the message log level? | 1,343,227 | <p>I'm using Python's <code>logging</code> mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "in... | 38 | 2009-08-27T19:13:52Z | 25,101,727 | <p>If you are just looking to skip formatting certain levels, you can do something simpler than the other answers like the following:</p>
<pre><code>class FormatterNotFormattingInfo(logging.Formatter):
def __init__(self, fmt = '%(levelname)s:%(message)s'):
logging.Formatter.__init__(self, fmt)
def for... | 2 | 2014-08-03T04:46:26Z | [
"python",
"logging"
] |
Can Python's logging format be modified depending on the message log level? | 1,343,227 | <p>I'm using Python's <code>logging</code> mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "in... | 38 | 2009-08-27T19:13:52Z | 27,835,318 | <p>Instead of relying on styles or internal fields, you could also create a Formatter that delegates to other formatters depending on record.levelno (or other criteria). This is a slightly cleaner solution in my humble opinion. Code below should work for any python version >= 2.7: </p>
<p>The simple way would look som... | 3 | 2015-01-08T07:51:54Z | [
"python",
"logging"
] |
How to find Title case phrases from a passage or bunch of paragraphs | 1,343,479 | <p>How do I parse sentence case phrases from a passage.</p>
<p>For example from this passage</p>
<p>Conan Doyle said that the character of Holmes was inspired by Dr. Joseph Bell, for whom Doyle had worked as a clerk at the Edinburgh Royal Infirmary. Like Holmes, Bell was noted for drawing large conclusions from the s... | 1 | 2009-08-27T20:04:07Z | 1,343,568 | <p>This kind of processing can be very tricky. This simple code does almost the right thing:</p>
<pre><code>for s in re.finditer(r"([A-Z][a-z]+[. ]+)+([A-Z][a-z]+)?", text):
print s.group(0)
</code></pre>
<p>produces:</p>
<pre><code>Conan Doyle
Holmes
Dr. Joseph Bell
Doyle
Edinburgh Royal Infirmary. Like Holmes... | 5 | 2009-08-27T20:25:25Z | [
"python",
"parsing",
"nlp",
"text-parsing"
] |
How to find Title case phrases from a passage or bunch of paragraphs | 1,343,479 | <p>How do I parse sentence case phrases from a passage.</p>
<p>For example from this passage</p>
<p>Conan Doyle said that the character of Holmes was inspired by Dr. Joseph Bell, for whom Doyle had worked as a clerk at the Edinburgh Royal Infirmary. Like Holmes, Bell was noted for drawing large conclusions from the s... | 1 | 2009-08-27T20:04:07Z | 1,344,498 | <p>The "re" approach runs out of steam very quickly. Named entity recognition is a very complicated topic, way beyond the scope of an SO answer. If you think you have a good approach to this problem, please point it at Flann O'Brien a.k.a. Myles na cGopaleen, Sukarno, Harry S. Truman, J. Edgar Hoover, J. K. Rowling, th... | 2 | 2009-08-28T00:53:20Z | [
"python",
"parsing",
"nlp",
"text-parsing"
] |
Nice exception handling when re-trying code | 1,343,541 | <p>I have some test cases. The test cases rely on data which takes time to compute. To speed up testing, I've cached the data so that it doesn't have to be recomputed.</p>
<p>I now have <code>foo()</code>, which looks at the cached data. I can't tell ahead of time what it will look at, as that depends a lot on the tes... | 3 | 2009-08-27T20:19:49Z | 1,343,567 | <p>Is there a way to tell if you want to do foobetter() before making the call? If you get an exception it should be because something unexpected (exceptional!) happened. Don't use exceptions for flow control.</p>
| 0 | 2009-08-27T20:25:24Z | [
"python",
"exception",
"exception-handling",
"code-formatting"
] |
Nice exception handling when re-trying code | 1,343,541 | <p>I have some test cases. The test cases rely on data which takes time to compute. To speed up testing, I've cached the data so that it doesn't have to be recomputed.</p>
<p>I now have <code>foo()</code>, which looks at the cached data. I can't tell ahead of time what it will look at, as that depends a lot on the tes... | 3 | 2009-08-27T20:19:49Z | 1,343,588 | <p>Using blanket exceptions isn't usually a great idea. What kind of Exception are you expecting there? Is it a KeyError, AttributeError, TypeError...</p>
<p>Once you've identified what type of error you're looking for you can use something like <code>hasattr()</code> or the <code>in</code> operator or many other thin... | 1 | 2009-08-27T20:29:44Z | [
"python",
"exception",
"exception-handling",
"code-formatting"
] |
Nice exception handling when re-trying code | 1,343,541 | <p>I have some test cases. The test cases rely on data which takes time to compute. To speed up testing, I've cached the data so that it doesn't have to be recomputed.</p>
<p>I now have <code>foo()</code>, which looks at the cached data. I can't tell ahead of time what it will look at, as that depends a lot on the tes... | 3 | 2009-08-27T20:19:49Z | 1,343,823 | <p>I disagree with the key suggestion in the existing answers, which basically boils down to treating exceptions in Python as you would in, say, C++ or Java -- that's NOT the preferred style in Python, where often the good old idea that "it's better to ask forgiveness than permission" (attempt an operation and deal wit... | 4 | 2009-08-27T21:26:26Z | [
"python",
"exception",
"exception-handling",
"code-formatting"
] |
Nice exception handling when re-trying code | 1,343,541 | <p>I have some test cases. The test cases rely on data which takes time to compute. To speed up testing, I've cached the data so that it doesn't have to be recomputed.</p>
<p>I now have <code>foo()</code>, which looks at the cached data. I can't tell ahead of time what it will look at, as that depends a lot on the tes... | 3 | 2009-08-27T20:19:49Z | 1,344,513 | <p>Sometimes there's no nice way to express a flow, it's just complicated. But here's a way to call foo() in only one place, and have the error handling in only one place:</p>
<pre><code>if cacheExists:
loadCache()
dataComputed = False
else:
calculateData()
dataComputed = True
while True:
try:
... | 1 | 2009-08-28T01:00:47Z | [
"python",
"exception",
"exception-handling",
"code-formatting"
] |
Nice exception handling when re-trying code | 1,343,541 | <p>I have some test cases. The test cases rely on data which takes time to compute. To speed up testing, I've cached the data so that it doesn't have to be recomputed.</p>
<p>I now have <code>foo()</code>, which looks at the cached data. I can't tell ahead of time what it will look at, as that depends a lot on the tes... | 3 | 2009-08-27T20:19:49Z | 1,346,770 | <p>I like the alternative approach proposed by Alex Martelli.</p>
<p>What do you think about using a list of functions as argument of the may_raise. The functions would be executed until one succeed!</p>
<p>Here is the code</p>
<pre>
def foo(x):
raise Exception("Arrrgh!")
return 0
def foobetter(x):
prin... | 1 | 2009-08-28T12:40:01Z | [
"python",
"exception",
"exception-handling",
"code-formatting"
] |
Is there a FileIO in Python? | 1,343,666 | <p>I know there is a StringIO stream in Python, but is there such a thing as a file stream in Python? Also is there a better way for me to look up these things? Documentation, etc...</p>
<p>I am trying to pass a "stream" to a "writer" object I made. I was hoping that I could pass a file handle/stream to this writer ob... | 1 | 2009-08-27T20:48:47Z | 1,343,676 | <p>There is a builtin file() which works much the same way. Here are the docs: <a href="http://docs.python.org/library/functions.html#file" rel="nofollow">http://docs.python.org/library/functions.html#file</a> and <a href="http://python.org/doc/2.5.2/lib/bltin-file-objects.html" rel="nofollow">http://python.org/doc/2.5... | 5 | 2009-08-27T20:50:36Z | [
"python",
"file-io",
"stream"
] |
Is there a FileIO in Python? | 1,343,666 | <p>I know there is a StringIO stream in Python, but is there such a thing as a file stream in Python? Also is there a better way for me to look up these things? Documentation, etc...</p>
<p>I am trying to pass a "stream" to a "writer" object I made. I was hoping that I could pass a file handle/stream to this writer ob... | 1 | 2009-08-27T20:48:47Z | 1,343,682 | <p>I am guessing you are looking for open(). <a href="http://docs.python.org/library/functions.html#open">http://docs.python.org/library/functions.html#open</a></p>
<pre><code>outfile = open("/path/to/file", "w")
[...]
outfile.write([...])
</code></pre>
<p>Documentation on all the things you can do with streams (thes... | 8 | 2009-08-27T20:52:36Z | [
"python",
"file-io",
"stream"
] |
Is there a FileIO in Python? | 1,343,666 | <p>I know there is a StringIO stream in Python, but is there such a thing as a file stream in Python? Also is there a better way for me to look up these things? Documentation, etc...</p>
<p>I am trying to pass a "stream" to a "writer" object I made. I was hoping that I could pass a file handle/stream to this writer ob... | 1 | 2009-08-27T20:48:47Z | 1,346,880 | <p>In Python, all the I/O operations are wrapped in a hight level API : the file likes objects.</p>
<p>It means that any file likes object will behave the same, and can be used in a function expecting them. This is called duck typing, and for file like objects you can expect the following behavior :</p>
<ul>
<li>open... | 1 | 2009-08-28T13:08:09Z | [
"python",
"file-io",
"stream"
] |
Basic tree in Python with a Django QuerySet | 1,343,845 | <p>Here's where I'm exposed as the fraud of a programmer I am. </p>
<p>I've never created a data tree.</p>
<p>Basically, I have a table with four fields: A, B, C, and D. I need to create a tree of unordered lists based on these fields. Ultimately, it would look something like this:</p>
<ul>
<li>A1
<ul>
<li>B1
<ul>
<... | 0 | 2009-08-27T21:30:27Z | 1,344,181 | <p>I am not sure of your question, is there something specific you are asking?</p>
<p>Here are a few reusable applications for storing hierarchical data:</p>
<ul>
<li><a href="http://code.google.com/p/django-mptt/" rel="nofollow">django-mptt</a></li>
<li><a href="http://code.google.com/p/django-treebeard/" rel="nofol... | 4 | 2009-08-27T22:52:16Z | [
"python",
"django",
"tree",
"iteration",
"django-queryset"
] |
Django hitting MySQL even after select_related()? | 1,344,016 | <p>I'm trying to optimize the database calls coming from a fairly small Django app. At current I have a couple of models, <code>Inquiry</code> and <code>InquiryStatus</code>. When selecting all of the records from MySQL, I get a nice <code>JOIN</code> statement on the two tables, followed by many requests to the Inqu... | 1 | 2009-08-27T22:10:02Z | 1,344,421 | <p>I believe this has to do with lazy evaluation. Django only hits the DB if and when necessary, not when you invoke models.Inquiry.objects.select_related('status').all()</p>
<p><a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#id3" rel="nofollow">http://docs.djangoproject.com/en/dev/topics/db/queries/#... | 0 | 2009-08-28T00:20:01Z | [
"python",
"django",
"django-models",
"django-select-related"
] |
Django hitting MySQL even after select_related()? | 1,344,016 | <p>I'm trying to optimize the database calls coming from a fairly small Django app. At current I have a couple of models, <code>Inquiry</code> and <code>InquiryStatus</code>. When selecting all of the records from MySQL, I get a nice <code>JOIN</code> statement on the two tables, followed by many requests to the Inqu... | 1 | 2009-08-27T22:10:02Z | 1,345,360 | <p>The code you've shown shouldn't actually generate any queries at all - QuerySets are only evaluated when necessary, not when they're defined, and you don't use the value anywhere so the execution won't be done.</p>
<p>Please show us a template or some other code that actually evaluates the qs - slices it, iterates,... | 0 | 2009-08-28T06:36:26Z | [
"python",
"django",
"django-models",
"django-select-related"
] |
Django hitting MySQL even after select_related()? | 1,344,016 | <p>I'm trying to optimize the database calls coming from a fairly small Django app. At current I have a couple of models, <code>Inquiry</code> and <code>InquiryStatus</code>. When selecting all of the records from MySQL, I get a nice <code>JOIN</code> statement on the two tables, followed by many requests to the Inqu... | 1 | 2009-08-27T22:10:02Z | 1,347,456 | <p>It seems that <a href="http://code.google.com/p/fullhistory/" rel="nofollow">fullhistory</a> ends up serializing the object, which evaluates each field in the instance to give it a base to compare to.</p>
<p>Take a look at the <code>get_all_data</code> function:</p>
<p><a href="http://code.google.com/p/fullhistory... | 0 | 2009-08-28T14:43:58Z | [
"python",
"django",
"django-models",
"django-select-related"
] |
How to explicitly specify a path to Firefox for Selenium? | 1,344,026 | <p>I got Selenium IDE, followed <a href="http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html">this post</a>, got to</p>
<pre><code>python test_default_server.py
</code></pre>
<p>and it complains Firefox is not in my path:</p>
<pre><code>Please add the directory containing ''firefox.exe'' to you... | 8 | 2009-08-27T22:12:11Z | 1,345,097 | <p>You have to use the following string when you instantiate selenium instead of just "*firefox":</p>
<pre><code>"*firefox C:\\Program Files\\Mozilla Firefox\\firefox.exe"
</code></pre>
<p>Notice: I'm not sure that path is correct, but it should be a similar one.</p>
<p>Update: Where do you instantiate your browser?... | 14 | 2009-08-28T05:00:00Z | [
"python",
"windows",
"firefox",
"testing",
"selenium"
] |
How to explicitly specify a path to Firefox for Selenium? | 1,344,026 | <p>I got Selenium IDE, followed <a href="http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html">this post</a>, got to</p>
<pre><code>python test_default_server.py
</code></pre>
<p>and it complains Firefox is not in my path:</p>
<pre><code>Please add the directory containing ''firefox.exe'' to you... | 8 | 2009-08-27T22:12:11Z | 1,346,616 | <p>The *firefox etc are the keys for which browser to use to run the tests. </p>
<p>There is a long list of them at <a href="http://stackoverflow.com/questions/1317055/how-to-run-google-chrome-with-selenium-rc/1317131#1317131">http://stackoverflow.com/questions/1317055/how-to-run-google-chrome-with-selenium-rc/131713... | 0 | 2009-08-28T12:10:25Z | [
"python",
"windows",
"firefox",
"testing",
"selenium"
] |
How to explicitly specify a path to Firefox for Selenium? | 1,344,026 | <p>I got Selenium IDE, followed <a href="http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html">this post</a>, got to</p>
<pre><code>python test_default_server.py
</code></pre>
<p>and it complains Firefox is not in my path:</p>
<pre><code>Please add the directory containing ''firefox.exe'' to you... | 8 | 2009-08-27T22:12:11Z | 4,083,009 | <p>If on C# editor, use the following string:</p>
<pre><code>selenium = new DefaultSelenium("localhost", 4444, "*firefox C:\\Program Files\\firefox.exe", "http://www.google.com/");
</code></pre>
<p>Note: use an extra back slash before Program Files and firefox.exe, since a single backslash becomes an unrecognized esc... | 2 | 2010-11-02T23:28:51Z | [
"python",
"windows",
"firefox",
"testing",
"selenium"
] |
How to explicitly specify a path to Firefox for Selenium? | 1,344,026 | <p>I got Selenium IDE, followed <a href="http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html">this post</a>, got to</p>
<pre><code>python test_default_server.py
</code></pre>
<p>and it complains Firefox is not in my path:</p>
<pre><code>Please add the directory containing ''firefox.exe'' to you... | 8 | 2009-08-27T22:12:11Z | 4,806,380 | <pre><code>selenium("localhost", 4444, "*firefox C:\Program Files\Mozilla Firefox\firefox.exe", "http://change-this-to-the-site-you-are-testing/")
</code></pre>
<p>Worked in Java.</p>
| 1 | 2011-01-26T15:32:24Z | [
"python",
"windows",
"firefox",
"testing",
"selenium"
] |
How to explicitly specify a path to Firefox for Selenium? | 1,344,026 | <p>I got Selenium IDE, followed <a href="http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html">this post</a>, got to</p>
<pre><code>python test_default_server.py
</code></pre>
<p>and it complains Firefox is not in my path:</p>
<pre><code>Please add the directory containing ''firefox.exe'' to you... | 8 | 2009-08-27T22:12:11Z | 5,539,835 | <p>This helps very much. </p>
<pre><code>setUp("http://localhost:8080/BingDemo/BingDriver.html", "*firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
</code></pre>
| 0 | 2011-04-04T14:21:11Z | [
"python",
"windows",
"firefox",
"testing",
"selenium"
] |
How to explicitly specify a path to Firefox for Selenium? | 1,344,026 | <p>I got Selenium IDE, followed <a href="http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html">this post</a>, got to</p>
<pre><code>python test_default_server.py
</code></pre>
<p>and it complains Firefox is not in my path:</p>
<pre><code>Please add the directory containing ''firefox.exe'' to you... | 8 | 2009-08-27T22:12:11Z | 5,539,946 | <p>This helps very much. <code>setUp("http://localhost:8080/BingDemo/BingDriver.html", "*firefox C:\Program Files (x86)\Mozilla Firefox\firefox.exe");</code></p>
<p>However, replace all occurrences of <code>\</code> with <code>\\</code> in *firefox <code>C:\Program Files (x86)\Mozilla Firefox\firefox.exe</code></p>
<... | 1 | 2011-04-04T14:27:02Z | [
"python",
"windows",
"firefox",
"testing",
"selenium"
] |
How to explicitly specify a path to Firefox for Selenium? | 1,344,026 | <p>I got Selenium IDE, followed <a href="http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html">this post</a>, got to</p>
<pre><code>python test_default_server.py
</code></pre>
<p>and it complains Firefox is not in my path:</p>
<pre><code>Please add the directory containing ''firefox.exe'' to you... | 8 | 2009-08-27T22:12:11Z | 28,178,020 | <p>I found it worth useful...</p>
<pre><code>Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "http://gmail.com");
</code></pre>
| 0 | 2015-01-27T19:03:03Z | [
"python",
"windows",
"firefox",
"testing",
"selenium"
] |
How to explicitly specify a path to Firefox for Selenium? | 1,344,026 | <p>I got Selenium IDE, followed <a href="http://jimmyg.org/blog/2009/getting-started-with-selenium-and-python.html">this post</a>, got to</p>
<pre><code>python test_default_server.py
</code></pre>
<p>and it complains Firefox is not in my path:</p>
<pre><code>Please add the directory containing ''firefox.exe'' to you... | 8 | 2009-08-27T22:12:11Z | 28,340,046 | <p>For Java Solution using Selenium Webdriver, you can import the below class:</p>
<pre><code>import org.openqa.selenium.firefox.FirefoxBinary;
</code></pre>
<p>and use the code snippet below to instantiate a new driver by explicitly specifying the path to the firefox.exe in your local system.</p>
<pre><code>Desire... | 0 | 2015-02-05T09:12:12Z | [
"python",
"windows",
"firefox",
"testing",
"selenium"
] |
Condensing code in Python with Mappings | 1,344,208 | <p>I seem to be using this block of code alot in Python.</p>
<pre><code>if Y is not None:
obj[X][0]=Y
</code></pre>
<p>How do I establish a mapping from X=>Y and then iterate through this entire mapping while calling that block of code on X and Y</p>
| 1 | 2009-08-27T23:02:44Z | 1,344,228 | <p>If Y is None, you can do something like:</p>
<pre><code>default_value = 0
obj[X][0] = Y if not None else default_value
</code></pre>
| 0 | 2009-08-27T23:09:31Z | [
"python"
] |
Condensing code in Python with Mappings | 1,344,208 | <p>I seem to be using this block of code alot in Python.</p>
<pre><code>if Y is not None:
obj[X][0]=Y
</code></pre>
<p>How do I establish a mapping from X=>Y and then iterate through this entire mapping while calling that block of code on X and Y</p>
| 1 | 2009-08-27T23:02:44Z | 1,344,229 | <pre><code>mapping = {X1: Y1, X2: Y2, X3: Y3}
mapping[X4] = Y4
mapping[X5] = Y5
for X,Y in mapping.items():
if Y is not None:
obj[X][0] = Y
</code></pre>
| 5 | 2009-08-27T23:10:00Z | [
"python"
] |
In Python, find item in list of dicts, using bisect | 1,344,308 | <p>I have a list of dicts, something like this:</p>
<pre><code>test_data = [
{ 'offset':0, 'data':1500 },
{ 'offset':1270, 'data':120 },
{ 'offset':2117, 'data':30 },
{ 'offset':4055, 'data':30000 },
]
</code></pre>
<p>The dict items are sorted in the list according to the <code>'offset'</code> data. ... | 7 | 2009-08-27T23:43:19Z | 1,344,347 | <p>The usual pattern here is similar to sorting by an attribute, decorate, operate, and undecorate. So in this case you'd just need to decorate and then call. However you'd want to avoid doing this since decorate would be O(n) whereas you want this to be O(logn). Therefore I'd consider your method best.</p>
| 3 | 2009-08-27T23:56:26Z | [
"python",
"dictionary",
"binary-search"
] |
In Python, find item in list of dicts, using bisect | 1,344,308 | <p>I have a list of dicts, something like this:</p>
<pre><code>test_data = [
{ 'offset':0, 'data':1500 },
{ 'offset':1270, 'data':120 },
{ 'offset':2117, 'data':30 },
{ 'offset':4055, 'data':30000 },
]
</code></pre>
<p>The dict items are sorted in the list according to the <code>'offset'</code> data. ... | 7 | 2009-08-27T23:43:19Z | 1,344,355 | <p>When you say the real data could be much longer, does that prevent you from keeping a list of offset values on hand?</p>
<pre><code>offset_values = [i['offset'] for i in test_data]
bisect.bisect(offset_values, 1900)
</code></pre>
<p>Your method seems fine to me though.</p>
| 4 | 2009-08-27T23:58:14Z | [
"python",
"dictionary",
"binary-search"
] |
In Python, find item in list of dicts, using bisect | 1,344,308 | <p>I have a list of dicts, something like this:</p>
<pre><code>test_data = [
{ 'offset':0, 'data':1500 },
{ 'offset':1270, 'data':120 },
{ 'offset':2117, 'data':30 },
{ 'offset':4055, 'data':30000 },
]
</code></pre>
<p>The dict items are sorted in the list according to the <code>'offset'</code> data. ... | 7 | 2009-08-27T23:43:19Z | 1,344,501 | <p>What you can do is this</p>
<pre><code>class OffsetWithAttributes( object ):
def __init__( self, offset, **kw ):
self.offset= offset
self.attributes= kw
def __eq__( self, other ):
return self.offset == other.offset
def __lt__( self, other ):
return self.offset < other.... | 3 | 2009-08-28T00:55:35Z | [
"python",
"dictionary",
"binary-search"
] |
In Python, find item in list of dicts, using bisect | 1,344,308 | <p>I have a list of dicts, something like this:</p>
<pre><code>test_data = [
{ 'offset':0, 'data':1500 },
{ 'offset':1270, 'data':120 },
{ 'offset':2117, 'data':30 },
{ 'offset':4055, 'data':30000 },
]
</code></pre>
<p>The dict items are sorted in the list according to the <code>'offset'</code> data. ... | 7 | 2009-08-27T23:43:19Z | 22,946,210 | <p>You could also use one of Python's many SortedDict implementations to manage your test_data. A sorted dict sorts the elements by key and maintains a mapping to a value. Some implementations also support a bisect operation on the keys. For example, the <a href="http://www.grantjenks.com/docs/sortedcontainers/">Python... | 5 | 2014-04-08T19:14:32Z | [
"python",
"dictionary",
"binary-search"
] |
Django vs. Pylons | 1,344,824 | <p>I've recently become a little frustrated with Django as a whole. It seems like I can't get full control over anything. I love Python to death, but I want to be able (and free) to do something as simple as adding a css class to an auto-generated form. </p>
<p>One MVC framework that I have really been enjoying workin... | 16 | 2009-08-28T03:03:37Z | 1,344,842 | <p>I'm using Pylons right now. The flexibility is great. It's all about best-of-breed rather than The Django Way. It's more oriented toward custom application development, as opposed to content-based web sites. You can certainly do content sites in it; it's just not specifically designed for them.</p>
<p>On the other ... | 19 | 2009-08-28T03:11:29Z | [
"python",
"django",
"grails",
"pylons"
] |
Django vs. Pylons | 1,344,824 | <p>I've recently become a little frustrated with Django as a whole. It seems like I can't get full control over anything. I love Python to death, but I want to be able (and free) to do something as simple as adding a css class to an auto-generated form. </p>
<p>One MVC framework that I have really been enjoying workin... | 16 | 2009-08-28T03:03:37Z | 1,344,869 | <p>Pylons is not that much simpler than Django and it doesn't seem to have the same community. For lightweight apps I would recommend <a href="http://webpy.org/">web.py</a>. Even though there is a little magic, it doesn't feel like it. You see everything you do. For lots of other ideas see this very current list of ... | 5 | 2009-08-28T03:21:56Z | [
"python",
"django",
"grails",
"pylons"
] |
Django vs. Pylons | 1,344,824 | <p>I've recently become a little frustrated with Django as a whole. It seems like I can't get full control over anything. I love Python to death, but I want to be able (and free) to do something as simple as adding a css class to an auto-generated form. </p>
<p>One MVC framework that I have really been enjoying workin... | 16 | 2009-08-28T03:03:37Z | 1,344,897 | <p>Something as simple as adding CSS classes to Django form fields <b>IS</b> <a href="http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.Widget.attrs" rel="nofollow">possible</a>.</p>
| 1 | 2009-08-28T03:33:16Z | [
"python",
"django",
"grails",
"pylons"
] |
Django vs. Pylons | 1,344,824 | <p>I've recently become a little frustrated with Django as a whole. It seems like I can't get full control over anything. I love Python to death, but I want to be able (and free) to do something as simple as adding a css class to an auto-generated form. </p>
<p>One MVC framework that I have really been enjoying workin... | 16 | 2009-08-28T03:03:37Z | 2,607,291 | <p>With the risk of going a bit off-topic here, "I want to be able (and free) to do something as simple as adding a css class to an auto-generated form" might not be the best indicator of the power (or lack of power) of a framework. Form generation is notoriously hard to do in a flexible way (cf. <a href="http://blog.i... | 1 | 2010-04-09T12:20:35Z | [
"python",
"django",
"grails",
"pylons"
] |
Programming a Self Learning Music Maker | 1,344,884 | <p>I want to learn how to program a music application that will analyze songs.</p>
<p>How would I get started in this and is there a library for analyzing soundwaves?</p>
<p>I know C, C++, Java, Python, some assembly, and some Perl.</p>
<p><strong>Related question:</strong> <a href="http://stackoverflow.com/question... | 6 | 2009-08-28T03:28:03Z | 1,344,890 | <p>You may like to start by looking at the MIDI format, it's reasonable simple compared to the compressed formats, and you can generate some nice things in it.</p>
<p>Depends what you want to do really.</p>
| 0 | 2009-08-28T03:30:04Z | [
"python",
"perl",
"music",
"waveform"
] |
Programming a Self Learning Music Maker | 1,344,884 | <p>I want to learn how to program a music application that will analyze songs.</p>
<p>How would I get started in this and is there a library for analyzing soundwaves?</p>
<p>I know C, C++, Java, Python, some assembly, and some Perl.</p>
<p><strong>Related question:</strong> <a href="http://stackoverflow.com/question... | 6 | 2009-08-28T03:28:03Z | 1,345,133 | <p>To analyze soundwaves you need some sort of fourier transformation (fft), so you can split the song up into it's frequencies and how they change over time. There exists fft support in numpy, I haven't used it, so I don't know if it's any good. But it would be a great place to start.</p>
<p>After that you then need ... | 3 | 2009-08-28T05:12:37Z | [
"python",
"perl",
"music",
"waveform"
] |
Programming a Self Learning Music Maker | 1,344,884 | <p>I want to learn how to program a music application that will analyze songs.</p>
<p>How would I get started in this and is there a library for analyzing soundwaves?</p>
<p>I know C, C++, Java, Python, some assembly, and some Perl.</p>
<p><strong>Related question:</strong> <a href="http://stackoverflow.com/question... | 6 | 2009-08-28T03:28:03Z | 1,345,313 | <p>Once you get past the FFT stuff that Lennart mentioned, you might want to have a look at Markov chains for analyzing intervals between notes, and aggregated patterns. </p>
<p>This is kind of treaded ground, but Markov chains have been used in the past to build a kind of statistical model of melodies from various s... | 6 | 2009-08-28T06:22:35Z | [
"python",
"perl",
"music",
"waveform"
] |
Programming a Self Learning Music Maker | 1,344,884 | <p>I want to learn how to program a music application that will analyze songs.</p>
<p>How would I get started in this and is there a library for analyzing soundwaves?</p>
<p>I know C, C++, Java, Python, some assembly, and some Perl.</p>
<p><strong>Related question:</strong> <a href="http://stackoverflow.com/question... | 6 | 2009-08-28T03:28:03Z | 1,346,272 | <p>Composition and analysis of music by computer is a huge field. There are two basic areas in this type of work, which overlap somewhat. </p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Algorithmic%5Fcomposition"><strong>Algorithmic composition</strong></a> is concerned with the <em>generation</em> of music. This ... | 9 | 2009-08-28T10:50:37Z | [
"python",
"perl",
"music",
"waveform"
] |
Programming a Self Learning Music Maker | 1,344,884 | <p>I want to learn how to program a music application that will analyze songs.</p>
<p>How would I get started in this and is there a library for analyzing soundwaves?</p>
<p>I know C, C++, Java, Python, some assembly, and some Perl.</p>
<p><strong>Related question:</strong> <a href="http://stackoverflow.com/question... | 6 | 2009-08-28T03:28:03Z | 1,349,807 | <p>There's the <a href="http://code.google.com/p/echo-nest-remix/" rel="nofollow">Echo Nest remix API</a> that lets you analyze and manipulate music in Python. Some examples here: <a href="http://musicmachinery.com/2009/06/21/wheres-the-pow/" rel="nofollow">Where's the pow</a> and here: <a href="http://musicmachinery.... | 0 | 2009-08-28T23:03:28Z | [
"python",
"perl",
"music",
"waveform"
] |
Replacing leading and trailing hyphens with spaces? | 1,345,025 | <p>What is the best way to replace each occurrence of a leading or trailing hyphen with a space?</p>
<p>For example, I want</p>
<p>---ab---c-def--</p>
<p>to become</p>
<p>000ab---c-def00
(where the zeros are spaces)</p>
<p>I'm trying to do this in Python, but I can't seem to come up with a regex that will do the s... | 2 | 2009-08-28T04:31:41Z | 1,345,050 | <pre><code>re.sub(r'^-+|-+$', lambda m: ' '*len(m.group()), '---ab---c-def--')
</code></pre>
<p>Explanation: the pattern matches 1 or more leading or trailing dashes; the substitution is best performed by a callable, which receives each match object -- so m.group() is the matched substring -- and returns the string th... | 5 | 2009-08-28T04:40:08Z | [
"python",
"regex"
] |
Replacing leading and trailing hyphens with spaces? | 1,345,025 | <p>What is the best way to replace each occurrence of a leading or trailing hyphen with a space?</p>
<p>For example, I want</p>
<p>---ab---c-def--</p>
<p>to become</p>
<p>000ab---c-def00
(where the zeros are spaces)</p>
<p>I'm trying to do this in Python, but I can't seem to come up with a regex that will do the s... | 2 | 2009-08-28T04:31:41Z | 1,345,051 | <p>Use a callable as the substitution target:</p>
<pre><code>s = re.sub("^(-+)", lambda m: " " * (m.end() - m.start()), s)
s = re.sub("(-+)$", lambda m: " " * (m.end() - m.start()), s)
</code></pre>
| 3 | 2009-08-28T04:40:39Z | [
"python",
"regex"
] |
Replacing leading and trailing hyphens with spaces? | 1,345,025 | <p>What is the best way to replace each occurrence of a leading or trailing hyphen with a space?</p>
<p>For example, I want</p>
<p>---ab---c-def--</p>
<p>to become</p>
<p>000ab---c-def00
(where the zeros are spaces)</p>
<p>I'm trying to do this in Python, but I can't seem to come up with a regex that will do the s... | 2 | 2009-08-28T04:31:41Z | 1,345,083 | <p>Whenever you want to match at the end of a string, always consider carefully whether you need <code>$</code> or <code>\Z</code>. Examples, using '0' instead of ' ' for clarity:</p>
<pre><code>>>> re.sub(r"^-+|-+\Z", lambda m: '0'*len(m.group()), "--ab--c-def--")
'00ab--c-def00'
>>> re.sub(r"^-+|-+... | 0 | 2009-08-28T04:54:44Z | [
"python",
"regex"
] |
Simple recursive descent in PyParsing | 1,345,039 | <p>I've tried taking <a href="http://stackoverflow.com/questions/634432/need-help-on-making-the-recursive-parser-using-pyparsing/634725#634725">this code</a> and converting it to something for a project I'm working on for programming language processing, but I'm running into an issue with a simplified version:</p>
<pr... | 17 | 2009-08-28T04:38:13Z | 1,345,151 | <p>A grammar like:</p>
<pre><code>expr :: expr op expr
</code></pre>
<p>is hard to work with because the recursion just keeps diving into the left.</p>
<p>A normal arithmetic grammar would look something like:</p>
<pre><code>expr :: mulxp | mulxp '+' expr
mulxp :: atom | atom '*' expr
atom :: Word(nums) | '(' + exp... | 4 | 2009-08-28T05:19:10Z | [
"python",
"pyparsing",
"recursive-descent"
] |
Simple recursive descent in PyParsing | 1,345,039 | <p>I've tried taking <a href="http://stackoverflow.com/questions/634432/need-help-on-making-the-recursive-parser-using-pyparsing/634725#634725">this code</a> and converting it to something for a project I'm working on for programming language processing, but I'm running into an issue with a simplified version:</p>
<pr... | 17 | 2009-08-28T04:38:13Z | 1,345,155 | <p>Is this more or less what you want...?</p>
<pre><code>from pyparsing import Literal,Word,ZeroOrMore,Forward,nums,oneOf
def Syntax():
op = oneOf( '+ - / *')
lpar = Literal( '(' )
rpar = Literal( ')' )
num = Word(nums)
expr = Forward()
atom = num | ( lpar + expr + rpar )
expr << ... | 9 | 2009-08-28T05:19:59Z | [
"python",
"pyparsing",
"recursive-descent"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.