title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
How to unescape apostrophes and such in Python? | 816,272 | <p>I have a string with symbols like this:</p>
<pre><code>&#39;
</code></pre>
<p>That's an apostrophe apparently.</p>
<p>I tried saxutils.unescape() without any luck and tried urllib.unquote()</p>
<p>How can I decode this? Thanks!</p>
| 7 | 2009-05-03T03:37:49Z | 816,612 | <p>The most robust solution seems to be <a href="http://effbot.org/zone/re-sub.htm#unescape-html" rel="nofollow">this function</a> by Python luminary Fredrik Lundh. It is not the shortest solution, but it handles named entities as well as hex and decimal codes.</p>
| 1 | 2009-05-03T08:53:22Z | [
"python",
"html",
"django",
"html-entities"
] |
How to unescape apostrophes and such in Python? | 816,272 | <p>I have a string with symbols like this:</p>
<pre><code>&#39;
</code></pre>
<p>That's an apostrophe apparently.</p>
<p>I tried saxutils.unescape() without any luck and tried urllib.unquote()</p>
<p>How can I decode this? Thanks!</p>
| 7 | 2009-05-03T03:37:49Z | 816,805 | <p>Try this: (found it <a href="http://github.com/sku/python-twitter-ircbot/blob/321d94e0e40d0acc92f5bf57d126b57369da70de/html%5Fdecode.py" rel="nofollow">here</a>)</p>
<pre><code>from htmlentitydefs import name2codepoint as n2cp
import re
def decode_htmlentities(string):
"""
Decode HTML entitiesâhex, decim... | 2 | 2009-05-03T11:12:42Z | [
"python",
"html",
"django",
"html-entities"
] |
Where is Python's "best ASCII for this Unicode" database? | 816,285 | <p>I have some text that uses Unicode punctuation, like left double quote, right single quote for apostrophe, and so on, and I need it in ASCII. Does Python have a database of these characters with obvious ASCII substitutes so I can do better than turning them all into "?" ?</p>
| 66 | 2009-05-03T03:46:17Z | 816,318 | <p>Interesting question. </p>
<p>Google helped me find <a href="http://www.peterbe.com/plog/unicode-to-ascii">this page</a> which descibes using the <a href="http://effbot.org/librarybook/unicodedata.htm">unicodedata module</a> as the following:</p>
<pre><code>import unicodedata
unicodedata.normalize('NFKD', title).e... | 16 | 2009-05-03T04:15:43Z | [
"python",
"unicode",
"ascii"
] |
Where is Python's "best ASCII for this Unicode" database? | 816,285 | <p>I have some text that uses Unicode punctuation, like left double quote, right single quote for apostrophe, and so on, and I need it in ASCII. Does Python have a database of these characters with obvious ASCII substitutes so I can do better than turning them all into "?" ?</p>
| 66 | 2009-05-03T03:46:17Z | 816,319 | <p>In my original answer, I also suggested <code>unicodedata.normalize</code>. However, I decided to test it out and it turns out it doesn't work with Unicode quotation marks. It does a good job translating accented Unicode characters, so I'm guessing <code>unicodedata.normalize</code> is implemented using the <code>un... | 24 | 2009-05-03T04:16:58Z | [
"python",
"unicode",
"ascii"
] |
Where is Python's "best ASCII for this Unicode" database? | 816,285 | <p>I have some text that uses Unicode punctuation, like left double quote, right single quote for apostrophe, and so on, and I need it in ASCII. Does Python have a database of these characters with obvious ASCII substitutes so I can do better than turning them all into "?" ?</p>
| 66 | 2009-05-03T03:46:17Z | 1,360,215 | <p>There's additional discussion about this at <a href="http://code.activestate.com/recipes/251871/" rel="nofollow">http://code.activestate.com/recipes/251871/</a> which has the NFKD solution and some ways of doing a conversion table, for things like ± => +/- and other non-letter characters.</p>
| 3 | 2009-09-01T02:03:07Z | [
"python",
"unicode",
"ascii"
] |
Where is Python's "best ASCII for this Unicode" database? | 816,285 | <p>I have some text that uses Unicode punctuation, like left double quote, right single quote for apostrophe, and so on, and I need it in ASCII. Does Python have a database of these characters with obvious ASCII substitutes so I can do better than turning them all into "?" ?</p>
| 66 | 2009-05-03T03:46:17Z | 1,701,378 | <p><a href="http://pypi.python.org/pypi/Unidecode">Unidecode</a> looks like a complete solution. It converts fancy quotes to ascii quotes, accented latin characters to unaccented and even attempts transliteration to deal with characters that don't have ASCII equivalents. That way your users don't have to see a bunch of... | 73 | 2009-11-09T14:37:23Z | [
"python",
"unicode",
"ascii"
] |
Python: avoiding pylint warnings about too many arguments | 816,391 | <p>I want to refactor a big Python function into smaller ones. For example, consider this following code snippet:</p>
<pre><code>x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
</code></pre>
<p>Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope va... | 16 | 2009-05-03T05:13:02Z | 816,394 | <p>Simplify or break apart the function so that it doesn't require nine arguments (or ignore pylint, but dodges like the ones you're proposing defeat the purpose of a lint tool).</p>
<p>EDIT: if it's a temporary measure, disable the warning for the particular function in question using a comment as described here: <a ... | 4 | 2009-05-03T05:16:17Z | [
"python",
"refactoring",
"pylint"
] |
Python: avoiding pylint warnings about too many arguments | 816,391 | <p>I want to refactor a big Python function into smaller ones. For example, consider this following code snippet:</p>
<pre><code>x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
</code></pre>
<p>Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope va... | 16 | 2009-05-03T05:13:02Z | 816,396 | <p>You could try using <a href="http://docs.python.org/tutorial/controlflow.html#arbitrary-argument-lists">Python's variable arguments</a> feature:</p>
<pre><code>def myfunction(*args):
for x in args:
# Do stuff with specific argument here
</code></pre>
| 9 | 2009-05-03T05:17:42Z | [
"python",
"refactoring",
"pylint"
] |
Python: avoiding pylint warnings about too many arguments | 816,391 | <p>I want to refactor a big Python function into smaller ones. For example, consider this following code snippet:</p>
<pre><code>x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
</code></pre>
<p>Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope va... | 16 | 2009-05-03T05:13:02Z | 816,399 | <p>Perhaps you could turn some of the arguments into member variables. If you need that much state a class sounds like a good idea to me. </p>
| 5 | 2009-05-03T05:18:05Z | [
"python",
"refactoring",
"pylint"
] |
Python: avoiding pylint warnings about too many arguments | 816,391 | <p>I want to refactor a big Python function into smaller ones. For example, consider this following code snippet:</p>
<pre><code>x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
</code></pre>
<p>Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope va... | 16 | 2009-05-03T05:13:02Z | 816,402 | <p>Python has some nice functional programming tools that are likely to fit your needs well. Check out <a href="http://www.secnetix.de/olli/Python/lambda%5Ffunctions.hawk" rel="nofollow">lambda functions</a> and <a href="http://docs.python.org/library/functions.html" rel="nofollow">map</a>. Also, you're using dicts whe... | 0 | 2009-05-03T05:19:36Z | [
"python",
"refactoring",
"pylint"
] |
Python: avoiding pylint warnings about too many arguments | 816,391 | <p>I want to refactor a big Python function into smaller ones. For example, consider this following code snippet:</p>
<pre><code>x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
</code></pre>
<p>Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope va... | 16 | 2009-05-03T05:13:02Z | 816,517 | <p>First, one of <a href="http://www.cs.yale.edu/quotes.html">Perlis's epigrams</a>:</p>
<blockquote>
<p>"If you have a procedure with 10
parameters, you probably missed some."</p>
</blockquote>
<p>Some of the 10 arguments are presumably related. Group them into an object, and pass that instead.</p>
<p>Making an... | 35 | 2009-05-03T07:22:58Z | [
"python",
"refactoring",
"pylint"
] |
Python: avoiding pylint warnings about too many arguments | 816,391 | <p>I want to refactor a big Python function into smaller ones. For example, consider this following code snippet:</p>
<pre><code>x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
</code></pre>
<p>Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope va... | 16 | 2009-05-03T05:13:02Z | 816,549 | <p>Do you want a better way to pass the arguments or just a way to stop <code>pylint</code> from giving you a hard time? If the latter, I seem to recall that you could stop the nagging by putting <code>pylint</code>-controlling comments in your code along the lines of:</p>
<pre><code>#pylint: disable-msg=R0913
</code>... | 18 | 2009-05-03T07:59:40Z | [
"python",
"refactoring",
"pylint"
] |
Python: avoiding pylint warnings about too many arguments | 816,391 | <p>I want to refactor a big Python function into smaller ones. For example, consider this following code snippet:</p>
<pre><code>x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
</code></pre>
<p>Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope va... | 16 | 2009-05-03T05:13:02Z | 816,789 | <p>You can easily change the maximum allowed number of arguments in pylint. Just open your pylintrc file (generate it if you don't already have one) and change:</p>
<p>max-args=5</p>
<p>to:</p>
<p>max-args = 6 # or any value that suits you </p>
<p>From pylint's <a href="http://www.logilab.org/card/pylint%5Fmanual">... | 11 | 2009-05-03T10:58:03Z | [
"python",
"refactoring",
"pylint"
] |
Python: avoiding pylint warnings about too many arguments | 816,391 | <p>I want to refactor a big Python function into smaller ones. For example, consider this following code snippet:</p>
<pre><code>x = x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9
</code></pre>
<p>Of course, this is a trivial example. In practice, the code is more complex. My point is that it contains many local-scope va... | 16 | 2009-05-03T05:13:02Z | 26,778,409 | <p>Comment about paxdiablo's reply - as I do not have enough reputation to comment there directly :-/</p>
<p>I do not like referring to the number, the sybolic name is much more expressive and avoid having to add a comment that could become obsolete over time.</p>
<p>So I'd rather do:</p>
<pre><code>#pylint: disable... | 3 | 2014-11-06T11:31:02Z | [
"python",
"refactoring",
"pylint"
] |
how to get the url of the current page in a GAE template | 816,683 | <p>In Google App Engine, is there a tag or other mechanism to get the URL of the current page in a template or is it necessary to pass the url as a variable to the template from the python code?</p>
| 2 | 2009-05-03T09:36:30Z | 816,728 | <p>It depends how you are populating the templates. If you are using them outside of Django, then you have to populate them with the URL yourself. If you are using them in Django with the default configuration, you would have to populate them with the URL yourself. An alternative that would avoid you having to popul... | 3 | 2009-05-03T10:13:19Z | [
"python",
"google-app-engine",
"templates",
"django-templates",
"web-applications"
] |
save an image with selenium & firefox | 816,704 | <p>i'm trying to save an image from a website using selenium server & python client.
i know the image's URL, but i can't find the code to save it, either when it's the the document itself, or when it's embedded in the current browser session.</p>
<p>the workaround i found so far is to save the page's screenshot (... | 8 | 2009-05-03T09:51:07Z | 816,777 | <p>How about going to the image URL and then taking a screenshot of the page? Firefox displays the image in full screen. Hope this helps..</p>
| -1 | 2009-05-03T10:48:43Z | [
"python",
"selenium"
] |
save an image with selenium & firefox | 816,704 | <p>i'm trying to save an image from a website using selenium server & python client.
i know the image's URL, but i can't find the code to save it, either when it's the the document itself, or when it's embedded in the current browser session.</p>
<p>the workaround i found so far is to save the page's screenshot (... | 8 | 2009-05-03T09:51:07Z | 816,814 | <p>I haven't used selenium, but if you know the image's URL, why not just do:</p>
<pre><code>from urllib import urlretrieve
urlretrieve(url, filename)
</code></pre>
<p>which will save the url to the filename.
more info <a href="http://docs.python.org/library/urllib.html#urllib.urlretrieve" rel="nofollow">here</a></p... | -1 | 2009-05-03T11:21:02Z | [
"python",
"selenium"
] |
save an image with selenium & firefox | 816,704 | <p>i'm trying to save an image from a website using selenium server & python client.
i know the image's URL, but i can't find the code to save it, either when it's the the document itself, or when it's embedded in the current browser session.</p>
<p>the workaround i found so far is to save the page's screenshot (... | 8 | 2009-05-03T09:51:07Z | 827,891 | <p>To do this the way you want (to actually capture the content sent down to the browser) you'd need to modify Selenium RC's proxy code (see ProxyHandler.java) and store the files locally on the disk in parallel to sending the response back to the browser.</p>
| 2 | 2009-05-06T03:18:48Z | [
"python",
"selenium"
] |
save an image with selenium & firefox | 816,704 | <p>i'm trying to save an image from a website using selenium server & python client.
i know the image's URL, but i can't find the code to save it, either when it's the the document itself, or when it's embedded in the current browser session.</p>
<p>the workaround i found so far is to save the page's screenshot (... | 8 | 2009-05-03T09:51:07Z | 2,699,314 | <p>I was trying to accomplish the same task, but the images I wanted to grab were the size of my monitor (wallpaper) -- so the capture screenshot workaround didn't work for me. I figured out a way to do it...</p>
<p>I've got selenium set up to go to the page I want (which induces all the session goodies)
Then I used ... | 2 | 2010-04-23T14:25:26Z | [
"python",
"selenium"
] |
save an image with selenium & firefox | 816,704 | <p>i'm trying to save an image from a website using selenium server & python client.
i know the image's URL, but i can't find the code to save it, either when it's the the document itself, or when it's embedded in the current browser session.</p>
<p>the workaround i found so far is to save the page's screenshot (... | 8 | 2009-05-03T09:51:07Z | 4,686,774 | <p>I found code that puts an image in to a canvas, then converts it to data - which could then be base64 encoded for example. My thought was to call this using the eval command in selenium, however in my testing the toDataURL is throwing a security error 1000. Seems like it is so close to a solution if not for that ... | 4 | 2011-01-14T00:10:28Z | [
"python",
"selenium"
] |
Dynamically change range in Python? | 816,712 | <p>So say I'm using BeautifulSoup to parse pages and my code figures out that there are at least 7 pages to a query.</p>
<p>The pagination looks like</p>
<pre><code> 1 2 3 4 5 6 7 Next
</code></pre>
<p>If I paginate all the way to 7, sometimes there are more than 7 pages, so that if I am on page 7, the pagination lo... | 1 | 2009-05-03T09:59:57Z | 816,721 | <p>You could probably çreate a generator that has mutable state that determines when it terminates... but what about something simple like this?</p>
<pre><code>page = 1
while page < num_pages + 1:
# do stuff that possibly updates num_pages here
page += 1
</code></pre>
| 6 | 2009-05-03T10:07:23Z | [
"python",
"beautifulsoup"
] |
Dynamically change range in Python? | 816,712 | <p>So say I'm using BeautifulSoup to parse pages and my code figures out that there are at least 7 pages to a query.</p>
<p>The pagination looks like</p>
<pre><code> 1 2 3 4 5 6 7 Next
</code></pre>
<p>If I paginate all the way to 7, sometimes there are more than 7 pages, so that if I am on page 7, the pagination lo... | 1 | 2009-05-03T09:59:57Z | 816,800 | <p>Here's a code free answer, but I think it's simple if you take advantage of what beautiful soup lets you do:</p>
<p>To start with, on the first page you have somewhere the page numbers & links; from your question they look like this:</p>
<pre><code>1 2 3 4 5 6 7 [next]
</code></pre>
<p>Different sites handle ... | 2 | 2009-05-03T11:05:47Z | [
"python",
"beautifulsoup"
] |
Dynamically change range in Python? | 816,712 | <p>So say I'm using BeautifulSoup to parse pages and my code figures out that there are at least 7 pages to a query.</p>
<p>The pagination looks like</p>
<pre><code> 1 2 3 4 5 6 7 Next
</code></pre>
<p>If I paginate all the way to 7, sometimes there are more than 7 pages, so that if I am on page 7, the pagination lo... | 1 | 2009-05-03T09:59:57Z | 817,795 | <p>I like John's <code>while</code>-based solution, but to use a <code>for</code> you could do something like:</p>
<pre><code>pages = range(1, num_pages+1)
for p in pages:
...possibly pages.extend(range(something, something)) here...
</code></pre>
<p>that is, you have to give a name to the range you're looping on,... | 1 | 2009-05-03T19:27:35Z | [
"python",
"beautifulsoup"
] |
wxPython: A foldable panel widget | 816,887 | <p>I have my main program window, and I would like to make a foldable panel. What I mean is, a panel which is aligned to one of the sides of the window, with a fold/unfold button. It's important that when the panel gets folded/unfolded, the other widgets change their size accordingly to take advantage of the space they... | 2 | 2009-05-03T12:04:04Z | 817,004 | <p>The layout managers for wxPython (and Swing and others) should be able to do this for you if you create the hierarchy properly. Let's assume it's bound to the right hand side, thus:</p>
<pre><code>+-----------------------------+
|+----------------+ +--------+|
|| | | This is||
|| | | y... | 1 | 2009-05-03T12:57:31Z | [
"python",
"user-interface",
"wxpython",
"widget"
] |
wxPython: A foldable panel widget | 816,887 | <p>I have my main program window, and I would like to make a foldable panel. What I mean is, a panel which is aligned to one of the sides of the window, with a fold/unfold button. It's important that when the panel gets folded/unfolded, the other widgets change their size accordingly to take advantage of the space they... | 2 | 2009-05-03T12:04:04Z | 817,095 | <p>Here is one way using wx.SplitterWindow</p>
<pre><code>import wx, wx.calendar
class FoldableWindowContainer(wx.Panel):
def __init__(self, parent, left, right):
wx.Panel.__init__(self, parent)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(sizer)
self.splitter = wx.SplitterWi... | 3 | 2009-05-03T13:47:19Z | [
"python",
"user-interface",
"wxpython",
"widget"
] |
Call a function with argument list in python | 817,087 | <p>I'm trying to call a function inside another function in python, but can't find the right syntax. What I want to do is something like this:</p>
<pre><code>def wrapper(func, args):
func(args)
def func1(x):
print(x)
def func2(x, y, z):
return x+y+z
wrapper(func1, [x])
wrapper(func2, [x, y, z])
</code><... | 104 | 2009-05-03T13:43:33Z | 817,093 | <p>You can use *args and **kwargs syntax for variable length arguments.</p>
<p><a href="http://stackoverflow.com/questions/287085/what-does-args-and-kwargs-mean">http://stackoverflow.com/questions/287085/what-does-args-and-kwargs-mean</a></p>
<p>And from the official python tutorial </p>
<p><a href="http://docs.pyth... | 10 | 2009-05-03T13:47:05Z | [
"python",
"function"
] |
Call a function with argument list in python | 817,087 | <p>I'm trying to call a function inside another function in python, but can't find the right syntax. What I want to do is something like this:</p>
<pre><code>def wrapper(func, args):
func(args)
def func1(x):
print(x)
def func2(x, y, z):
return x+y+z
wrapper(func1, [x])
wrapper(func2, [x, y, z])
</code><... | 104 | 2009-05-03T13:43:33Z | 817,111 | <p>You need to use arguments unpacking..</p>
<pre><code>def wrapper(func, *args):
func(*args)
def func1(x):
print(x)
def func2(x, y, z):
print x+y+z
wrapper(func1, 1)
wrapper(func2, 1, 2, 3)
</code></pre>
| 8 | 2009-05-03T13:53:57Z | [
"python",
"function"
] |
Call a function with argument list in python | 817,087 | <p>I'm trying to call a function inside another function in python, but can't find the right syntax. What I want to do is something like this:</p>
<pre><code>def wrapper(func, args):
func(args)
def func1(x):
print(x)
def func2(x, y, z):
return x+y+z
wrapper(func1, [x])
wrapper(func2, [x, y, z])
</code><... | 104 | 2009-05-03T13:43:33Z | 817,296 | <p>To expand a little on the other answers:</p>
<p>In the line:</p>
<pre><code>def wrapper(func, *args):
</code></pre>
<p>The * next to <code>args</code> means "take the rest of the parameters given and put them in a list called <code>args</code>". </p>
<p>In the line:</p>
<pre><code> func(*args)
</code></pre>
... | 187 | 2009-05-03T15:17:44Z | [
"python",
"function"
] |
Call a function with argument list in python | 817,087 | <p>I'm trying to call a function inside another function in python, but can't find the right syntax. What I want to do is something like this:</p>
<pre><code>def wrapper(func, args):
func(args)
def func1(x):
print(x)
def func2(x, y, z):
return x+y+z
wrapper(func1, [x])
wrapper(func2, [x, y, z])
</code><... | 104 | 2009-05-03T13:43:33Z | 817,834 | <p>The literal answer to your question (to do exactly what you asked, changing only the wrapper, not the functions or the function calls) is simply to alter the line</p>
<pre><code>func(args)
</code></pre>
<p>to read </p>
<pre><code>func(*args)
</code></pre>
<p>This tells Python to take the list given (in this case... | 7 | 2009-05-03T19:48:20Z | [
"python",
"function"
] |
Call a function with argument list in python | 817,087 | <p>I'm trying to call a function inside another function in python, but can't find the right syntax. What I want to do is something like this:</p>
<pre><code>def wrapper(func, args):
func(args)
def func1(x):
print(x)
def func2(x, y, z):
return x+y+z
wrapper(func1, [x])
wrapper(func2, [x, y, z])
</code><... | 104 | 2009-05-03T13:43:33Z | 817,968 | <p>The simpliest way to wrap a function </p>
<pre><code> func(*args, **kwargs)
</code></pre>
<p>... is to manually write a wrapper that would call <em>func()</em> inside itself:</p>
<pre><code> def wrapper(*args, **kwargs):
# do something before
try:
return func(*a, **kwargs)
... | 13 | 2009-05-03T20:50:24Z | [
"python",
"function"
] |
Delete digits in Python (Regex) | 817,122 | <p>I'm trying to delete all digits from a string.
However the next code deletes as well digits contained in any word, and obviously I don't want that.
I've been trying many regular expressions with no success.</p>
<p>Thanks!</p>
<p><hr /></p>
<pre><code>s = "This must not b3 delet3d, but the number at the end yes 13... | 6 | 2009-05-03T13:59:17Z | 817,140 | <p>Add a space before the \d+.</p>
<pre><code>>>> s = "This must not b3 delet3d, but the number at the end yes 134411"
>>> s = re.sub(" \d+", " ", s)
>>> s
'This must not b3 delet3d, but the number at the end yes '
</code></pre>
<p>Edit: After looking at the comments, I decided to form a m... | 7 | 2009-05-03T14:04:05Z | [
"python",
"regex",
"digits"
] |
Delete digits in Python (Regex) | 817,122 | <p>I'm trying to delete all digits from a string.
However the next code deletes as well digits contained in any word, and obviously I don't want that.
I've been trying many regular expressions with no success.</p>
<p>Thanks!</p>
<p><hr /></p>
<pre><code>s = "This must not b3 delet3d, but the number at the end yes 13... | 6 | 2009-05-03T13:59:17Z | 817,148 | <p>If your number is allways at the end of your strings try :
re.sub("\d+$", "", s)</p>
<p>otherwise, you may try
re.sub("(\s)\d+(\s)", "\1\2", s)</p>
<p>You can adjust the back-references to keep only one or two of the spaces (\s match any white separator)</p>
| 1 | 2009-05-03T14:06:05Z | [
"python",
"regex",
"digits"
] |
Delete digits in Python (Regex) | 817,122 | <p>I'm trying to delete all digits from a string.
However the next code deletes as well digits contained in any word, and obviously I don't want that.
I've been trying many regular expressions with no success.</p>
<p>Thanks!</p>
<p><hr /></p>
<pre><code>s = "This must not b3 delet3d, but the number at the end yes 13... | 6 | 2009-05-03T13:59:17Z | 817,164 | <p>Try this:</p>
<pre><code>"\b\d+\b"
</code></pre>
<p>That'll match only those digits that are not part of another word.</p>
| 11 | 2009-05-03T14:12:44Z | [
"python",
"regex",
"digits"
] |
Delete digits in Python (Regex) | 817,122 | <p>I'm trying to delete all digits from a string.
However the next code deletes as well digits contained in any word, and obviously I don't want that.
I've been trying many regular expressions with no success.</p>
<p>Thanks!</p>
<p><hr /></p>
<pre><code>s = "This must not b3 delet3d, but the number at the end yes 13... | 6 | 2009-05-03T13:59:17Z | 817,189 | <p>To handle digit strings at the beginning of a line as well:</p>
<pre><code>s = re.sub(r"(^|\W)\d+", "", s)
</code></pre>
| 2 | 2009-05-03T14:23:58Z | [
"python",
"regex",
"digits"
] |
Delete digits in Python (Regex) | 817,122 | <p>I'm trying to delete all digits from a string.
However the next code deletes as well digits contained in any word, and obviously I don't want that.
I've been trying many regular expressions with no success.</p>
<p>Thanks!</p>
<p><hr /></p>
<pre><code>s = "This must not b3 delet3d, but the number at the end yes 13... | 6 | 2009-05-03T13:59:17Z | 817,271 | <p>Using <code>\s</code> isn't very good, since it doesn't handle tabs, et al. A first cut at a better solution is:</p>
<pre><code>re.sub(r"\b\d+\b", "", s)
</code></pre>
<p>Note that the pattern is a raw string because <code>\b</code> is normally the backspace escape for strings, and we want the special word boundar... | 4 | 2009-05-03T15:05:28Z | [
"python",
"regex",
"digits"
] |
Delete digits in Python (Regex) | 817,122 | <p>I'm trying to delete all digits from a string.
However the next code deletes as well digits contained in any word, and obviously I don't want that.
I've been trying many regular expressions with no success.</p>
<p>Thanks!</p>
<p><hr /></p>
<pre><code>s = "This must not b3 delet3d, but the number at the end yes 13... | 6 | 2009-05-03T13:59:17Z | 817,304 | <p>Non-regex solution:</p>
<pre><code>>>> s = "This must not b3 delet3d, but the number at the end yes 134411"
>>> " ".join([x for x in s.split(" ") if not x.isdigit()])
'This must not b3 delet3d, but the number at the end yes'
</code></pre>
<p>Splits by <code>" "</code>, and checks if the chunk is ... | 0 | 2009-05-03T15:21:27Z | [
"python",
"regex",
"digits"
] |
Delete digits in Python (Regex) | 817,122 | <p>I'm trying to delete all digits from a string.
However the next code deletes as well digits contained in any word, and obviously I don't want that.
I've been trying many regular expressions with no success.</p>
<p>Thanks!</p>
<p><hr /></p>
<pre><code>s = "This must not b3 delet3d, but the number at the end yes 13... | 6 | 2009-05-03T13:59:17Z | 817,328 | <p>I don't know what your real situation looks like, but most of the answers look like they won't handle negative numbers or decimals,</p>
<p><code>re.sub(r"(\b|\s+\-?|^\-?)(\d+|\d*\.\d+)\b","")</code></p>
<p>The above should also handle things like,</p>
<p>"This must not b3 delet3d, but the number at the end yes -1... | 1 | 2009-05-03T15:37:32Z | [
"python",
"regex",
"digits"
] |
Overriding the save method in Django ModelForm | 817,284 | <p>I'm having trouble overriding a <code>ModelForm</code> save method. This is the error I'm receiving:</p>
<pre><code>Exception Type: TypeError
Exception Value: save() got an unexpected keyword argument 'commit'
</code></pre>
<p>My intentions are to have a form submit many values for 3 fields, to then creat... | 48 | 2009-05-03T15:12:00Z | 817,364 | <p>In your <code>save</code> you have to have the argument <code>commit</code>. If anything overrides your form, or wants to modify what it's saving, it will do <code>save(commit=False)</code>, modify the output, and then save it itself.</p>
<p>Also, your ModelForm should return the model it's saving. Usually a ModelF... | 115 | 2009-05-03T15:54:10Z | [
"python",
"django",
"django-forms",
"django-admin"
] |
NetBeans debugging of Python (GAE) | 817,407 | <p>dev_appserver works normal when run it. But if i try o debug, i found an error caused by</p>
<pre><code> __file__
</code></pre>
<p>that is changed on jpydaemon.py. Has anyone successfully debugged apps on NetBeans?</p>
| 2 | 2009-05-03T16:13:11Z | 1,202,861 | <p>Nope but I'm interested in setting this up. You have to attach the netbeans debugger to the port somehow. This may help you: here's an example I was reading about for java: <a href="http://blogs.oracle.com/leonfan/entry/netbeans_development_series_for_google" rel="nofollow">http://blogs.oracle.com/leonfan/entry/netb... | 1 | 2009-07-29T20:33:21Z | [
"python",
"debugging",
"google-app-engine",
"netbeans"
] |
NetBeans debugging of Python (GAE) | 817,407 | <p>dev_appserver works normal when run it. But if i try o debug, i found an error caused by</p>
<pre><code> __file__
</code></pre>
<p>that is changed on jpydaemon.py. Has anyone successfully debugged apps on NetBeans?</p>
| 2 | 2009-05-03T16:13:11Z | 35,345,329 | <p>You need to make sure the port in your debug settings is not in use.</p>
<p>I am assuming you have the python plugin in stalled for Netbeans.</p>
<p>Go to your settings and select python and then the Debugger tab.
The debugger port is set there.</p>
<p>Check your in use ports (In Windows open a command prompt and... | 1 | 2016-02-11T16:52:31Z | [
"python",
"debugging",
"google-app-engine",
"netbeans"
] |
Update Facebooks Status using Python | 817,431 | <p>Is there an easy way to update my Facebook status ("What's on your mind?" box) using Python code ?</p>
| 3 | 2009-05-03T16:25:43Z | 817,437 | <p>The Facebook Developers site for Python is a great place to start. You should be able to accomplish this with a REST call.</p>
<p><a href="http://wiki.developers.facebook.com/index.php/Python" rel="nofollow">http://wiki.developers.facebook.com/index.php/Python</a></p>
| 3 | 2009-05-03T16:29:45Z | [
"python",
"facebook"
] |
Update Facebooks Status using Python | 817,431 | <p>Is there an easy way to update my Facebook status ("What's on your mind?" box) using Python code ?</p>
| 3 | 2009-05-03T16:25:43Z | 817,443 | <p>Check out <a href="http://github.com/sciyoshi/pyfacebook/tree/master" rel="nofollow">PyFacebook</a> which has a <a href="http://wiki.developers.facebook.com/index.php/PyFacebook%5FTutorial" rel="nofollow">tutorial</a>, from... Facebook!</p>
<p>Blatantly ripped from the documentation on that page and untested, you'd... | 12 | 2009-05-03T16:31:21Z | [
"python",
"facebook"
] |
Python's random: What happens if I don't use seed(someValue)? | 817,705 | <p>a)In this case does the random number generator uses the system's clock (making the seed change) on each run? </p>
<p>b)Is the seed used to generate the pseudo-random values of expovariate(lambda)? </p>
| 17 | 2009-05-03T18:44:19Z | 817,716 | <p>a) It typically uses the system clock, the clock on some systems may only have ms precision and so seed twice very quickly may result in the same value.</p>
<blockquote>
<p>seed(self, a=None)
Initialize internal state from hashable object.</p>
<pre><code>None or no argument seeds from current time or from ... | 6 | 2009-05-03T18:51:43Z | [
"python",
"random",
"seed"
] |
Python's random: What happens if I don't use seed(someValue)? | 817,705 | <p>a)In this case does the random number generator uses the system's clock (making the seed change) on each run? </p>
<p>b)Is the seed used to generate the pseudo-random values of expovariate(lambda)? </p>
| 17 | 2009-05-03T18:44:19Z | 817,717 | <blockquote>
<p>current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).</p>
</blockquot... | 2 | 2009-05-03T18:52:06Z | [
"python",
"random",
"seed"
] |
Python's random: What happens if I don't use seed(someValue)? | 817,705 | <p>a)In this case does the random number generator uses the system's clock (making the seed change) on each run? </p>
<p>b)Is the seed used to generate the pseudo-random values of expovariate(lambda)? </p>
| 17 | 2009-05-03T18:44:19Z | 817,742 | <p>"Use the Source, Luke!"...;-). Studying <a href="http://svn.python.org/view/python/trunk/Lib/random.py?revision=68378&view=markup">http://svn.python.org/view/python/trunk/Lib/random.py?revision=68378&view=markup</a> will rapidly reassure you;-).</p>
<p>What happens when seed isn't set (that's the "i is Non... | 17 | 2009-05-03T19:04:15Z | [
"python",
"random",
"seed"
] |
Unique session id in python | 817,882 | <p>How do I generate a unique session id in Python?</p>
| 33 | 2009-05-03T20:11:02Z | 817,892 | <p>You can use the <a href="http://docs.python.org/library/uuid.html">uuid library</a> like so:</p>
<pre>
import uuid
my_id = uuid.uuid1() # or uuid.uuid4()
</pre>
| 21 | 2009-05-03T20:14:37Z | [
"python",
"session"
] |
Unique session id in python | 817,882 | <p>How do I generate a unique session id in Python?</p>
| 33 | 2009-05-03T20:11:02Z | 817,940 | <p>It can be as simple as creating a random number. Of course, you'd have to store your session IDs in a database or something and check each one you generate to make sure it's not a duplicate, but odds are it never will be if the numbers are large enough.</p>
| 1 | 2009-05-03T20:32:03Z | [
"python",
"session"
] |
Unique session id in python | 817,882 | <p>How do I generate a unique session id in Python?</p>
| 33 | 2009-05-03T20:11:02Z | 818,040 | <pre><code>import os, base64
def generate_session():
return base64.b64encode(os.urandom(16))
</code></pre>
| 18 | 2009-05-03T21:19:46Z | [
"python",
"session"
] |
Unique session id in python | 817,882 | <p>How do I generate a unique session id in Python?</p>
| 33 | 2009-05-03T20:11:02Z | 820,198 | <p>What's the session for? A web app? You might wanna look at <a href="http://beaker.groovie.org/" rel="nofollow">the beaker module</a>. It is the default module for handling sessions in Pylons.</p>
| 1 | 2009-05-04T13:53:34Z | [
"python",
"session"
] |
Unique session id in python | 817,882 | <p>How do I generate a unique session id in Python?</p>
| 33 | 2009-05-03T20:11:02Z | 6,092,448 | <p>I hate to say this, but none of the other solutions posted here are correct with regards to being a "secure session ID." </p>
<pre><code># pip install M2Crypto
import base64, M2Crypto
def generate_session_id(num_bytes = 16):
return base64.b64encode(M2Crypto.m2.rand_bytes(num_bytes))
</code></pre>
<p>Neither <c... | 70 | 2011-05-23T03:07:22Z | [
"python",
"session"
] |
Creating dictionaries with pre-defined keys | 817,884 | <p>In python, is there a way to create a class that is treated like a dictionary but have the keys pre-defined when a new instance is created?</p>
| 3 | 2009-05-03T20:12:18Z | 817,893 | <p>You can easily extend any built in type. This is how you'd do it with a dict:</p>
<pre><code>>>> class MyClass(dict):
... def __init__(self, *args, **kwargs):
... self['mykey'] = 'myvalue'
... self['mykey2'] = 'myvalue2'
...
>>> x = MyClass()
>>> x['mykey']
'my... | 7 | 2009-05-03T20:14:56Z | [
"python",
"dictionary"
] |
Creating dictionaries with pre-defined keys | 817,884 | <p>In python, is there a way to create a class that is treated like a dictionary but have the keys pre-defined when a new instance is created?</p>
| 3 | 2009-05-03T20:12:18Z | 817,902 | <p>Just create a subclass of dict and add the keys in the <strong>init</strong> method.</p>
<pre>
class MyClass(dict)
def __init__(self):
"""Creates a new dict with default values""""
self['key1'] = 'value1'
</pre>
<p>Remember though, that in python any class that 'acts like a dict' is usually treated like... | 0 | 2009-05-03T20:17:28Z | [
"python",
"dictionary"
] |
Creating dictionaries with pre-defined keys | 817,884 | <p>In python, is there a way to create a class that is treated like a dictionary but have the keys pre-defined when a new instance is created?</p>
| 3 | 2009-05-03T20:12:18Z | 818,019 | <p>Yes, in Python <code>dict</code> is a class , so you can subclass it:</p>
<pre><code> class SubDict(dict):
def __init__(self):
dict.__init__(self)
self.update({
'foo': 'bar',
'baz': 'spam',})
</code></pre>
<p>Here you override dict's <code>__init__... | 3 | 2009-05-03T21:13:03Z | [
"python",
"dictionary"
] |
Creating dictionaries with pre-defined keys | 817,884 | <p>In python, is there a way to create a class that is treated like a dictionary but have the keys pre-defined when a new instance is created?</p>
| 3 | 2009-05-03T20:12:18Z | 818,116 | <p>You can also have the dict subclass restrict the keys to a predefined list, by overriding <code>__setitem__()</code></p>
<pre><code>>>> class LimitedDict(dict):
_keys = "a b c".split()
def __init__(self, valtype=int):
for key in LimitedDict._keys:
self[key] = valtype()
def __setitem_... | 5 | 2009-05-03T22:00:11Z | [
"python",
"dictionary"
] |
Creating dictionaries with pre-defined keys | 817,884 | <p>In python, is there a way to create a class that is treated like a dictionary but have the keys pre-defined when a new instance is created?</p>
| 3 | 2009-05-03T20:12:18Z | 819,017 | <p>I'm not sure this is what you're looking for, but when I read your post I immediately thought you were looking to dynamically generate keys for counting exercises.</p>
<p>Unlike perl, which will do this for you by default,</p>
<pre><code>
grep{$_{$_}++} qw/ a a b c c c /;
print map{$_."\t".$_{$_}."\n"} sort {$_{$b... | 0 | 2009-05-04T06:44:26Z | [
"python",
"dictionary"
] |
C55: More Info? | 818,016 | <p>I saw a PyCon09 keynote presentation (slides: <a href="http://www.slideshare.net/kn0thing/ride-the-snake-reddit-keynote-pycon-09?c55" rel="nofollow">http://www.slideshare.net/kn0thing/ride-the-snake-reddit-keynote-pycon-09?c55</a>) given by the reddit guys, and in it they mention a CSS compiler called C55. They said... | 3 | 2009-05-03T21:11:43Z | 818,146 | <p>Just from the talk, the main advantage over simply generating CSS from templates is that it allows nesting, which is conceptually a lot nicer to work with.</p>
<p>So you could do something like this in C55 (obviously I'm kind of making up the syntax):</p>
<pre><code>div.content
{
color: $content_color ;
.left... | 3 | 2009-05-03T22:14:50Z | [
"python",
"css",
"reddit"
] |
MPI4Py Scatter sendbuf Argument Type? | 818,362 | <p>I'm having trouble with the Scatter function in the MPI4Py Python module.
My assumption is that I should be able to pass it a single list for the sendbuffer. However, I'm getting a consistent error message when I do that, or indeed add the other two arguments, recvbuf and root:</p>
<pre><code> File "code/step3.py... | 2 | 2009-05-03T23:47:51Z | 3,712,648 | <p>If you want to move raw buffers (as with <code>Gather</code>), you provide a triplet <code>[buffer, size, type]</code>. Look at the demos for examples of this. If you want to send Python objects, you should use the higher level interface and call <code>gather</code> (note the lowercase) which uses <code>pickle</co... | 6 | 2010-09-14T20:29:17Z | [
"python",
"parallel-processing",
"mpi"
] |
Letting users upload Python scripts for execution | 818,402 | <p>I understand that letting any anonymous user upload any sort of file in general can be dangerous, especially if it's code. However, I have an idea to let users upload custom AI scripts to my website. I would provide the template so that the user could compete with other AI's in an online web game I wrote in Python. ... | 12 | 2009-05-04T00:20:54Z | 818,405 | <p>Yes.</p>
<p>Allow them to script their client, not your server.</p>
| 3 | 2009-05-04T00:25:46Z | [
"python",
"cgi"
] |
Letting users upload Python scripts for execution | 818,402 | <p>I understand that letting any anonymous user upload any sort of file in general can be dangerous, especially if it's code. However, I have an idea to let users upload custom AI scripts to my website. I would provide the template so that the user could compete with other AI's in an online web game I wrote in Python. ... | 12 | 2009-05-04T00:20:54Z | 818,417 | <p>I am in no way associated with this site and I'm only linking it because it tries to achieve what you are getting after: jailing of python. The site is <a href="http://codepad.org/about">code pad</a>.</p>
<p>According to the about page it is ran under <a href="http://www.xs4all.nl/~weegen/eelis/geordi/">geordi</a>... | 8 | 2009-05-04T00:34:51Z | [
"python",
"cgi"
] |
Letting users upload Python scripts for execution | 818,402 | <p>I understand that letting any anonymous user upload any sort of file in general can be dangerous, especially if it's code. However, I have an idea to let users upload custom AI scripts to my website. I would provide the template so that the user could compete with other AI's in an online web game I wrote in Python. ... | 12 | 2009-05-04T00:20:54Z | 818,430 | <p>Using PyPy you can create a python sandbox. The sandbox is a separate and supposedly secure python environment where you can execute their scripts. More info here</p>
<p><a href="http://codespeak.net/pypy/dist/pypy/doc/sandbox.html" rel="nofollow">http://codespeak.net/pypy/dist/pypy/doc/sandbox.html</a></p>
<p>"... | 8 | 2009-05-04T00:44:33Z | [
"python",
"cgi"
] |
Letting users upload Python scripts for execution | 818,402 | <p>I understand that letting any anonymous user upload any sort of file in general can be dangerous, especially if it's code. However, I have an idea to let users upload custom AI scripts to my website. I would provide the template so that the user could compete with other AI's in an online web game I wrote in Python. ... | 12 | 2009-05-04T00:20:54Z | 818,558 | <p>Along with other safeguards, you can also incorporate human review of the code. Assuming part of the experience is reviewing other members' solutions, and everyone is a python developer, don't allow new code to be activated until a certain number of members vote for it. Your users aren't going to approve malicious... | 4 | 2009-05-04T01:58:12Z | [
"python",
"cgi"
] |
Letting users upload Python scripts for execution | 818,402 | <p>I understand that letting any anonymous user upload any sort of file in general can be dangerous, especially if it's code. However, I have an idea to let users upload custom AI scripts to my website. I would provide the template so that the user could compete with other AI's in an online web game I wrote in Python. ... | 12 | 2009-05-04T00:20:54Z | 819,227 | <p>PyPy is probably a decent bet on the server side as suggested, but I'd look into having your python backend provide well defined APIs and data formats and have the users implement the AI and logic in Javascript so it can run in their browser. So the interaction would look like: For each match/turn/etc, pass data to ... | 1 | 2009-05-04T08:11:25Z | [
"python",
"cgi"
] |
Letting users upload Python scripts for execution | 818,402 | <p>I understand that letting any anonymous user upload any sort of file in general can be dangerous, especially if it's code. However, I have an idea to let users upload custom AI scripts to my website. I would provide the template so that the user could compete with other AI's in an online web game I wrote in Python. ... | 12 | 2009-05-04T00:20:54Z | 878,455 | <p>Have an extensive API for the users and strip all other calls upon upload (such as import statements). Also, strip everything that has anything to do with file i/o.</p>
<p>(You might want to do multiple passes to ensure that you didn't miss anything.)</p>
| 0 | 2009-05-18T15:59:40Z | [
"python",
"cgi"
] |
Shouldn't __metaclass__ force the use of a metaclass in Python? | 818,483 | <p>I've been trying to learn about metaclasses in Python. I get the main idea, but I can't seem to activate the mechanism. As I understand it, you can specify M to be as the metaclass when constructing a class K by setting <code>__metaclass__</code> to M at the global or class level. To test this out, I wrote the follo... | 6 | 2009-05-04T01:18:10Z | 818,502 | <p>In Python 3 (which you are using) metaclasses are specified by a keyword parameter in the class definition:</p>
<pre><code>class ClassMeta(metaclass=M):
pass
</code></pre>
<p>Specifying a <code>__metaclass__</code> class property or global variable is old syntax from Python 2.x and not longer supported. See also... | 13 | 2009-05-04T01:29:25Z | [
"python",
"oop",
"python-3.x",
"metaclass"
] |
Shouldn't __metaclass__ force the use of a metaclass in Python? | 818,483 | <p>I've been trying to learn about metaclasses in Python. I get the main idea, but I can't seem to activate the mechanism. As I understand it, you can specify M to be as the metaclass when constructing a class K by setting <code>__metaclass__</code> to M at the global or class level. To test this out, I wrote the follo... | 6 | 2009-05-04T01:18:10Z | 818,508 | <p>This works as you expect in Python 2.6 (and earlier), but in 3.0 metaclasses are specified differently:</p>
<pre><code>class ArgMeta(metaclass=M): ...
</code></pre>
| 2 | 2009-05-04T01:33:34Z | [
"python",
"oop",
"python-3.x",
"metaclass"
] |
Shouldn't __metaclass__ force the use of a metaclass in Python? | 818,483 | <p>I've been trying to learn about metaclasses in Python. I get the main idea, but I can't seem to activate the mechanism. As I understand it, you can specify M to be as the metaclass when constructing a class K by setting <code>__metaclass__</code> to M at the global or class level. To test this out, I wrote the follo... | 6 | 2009-05-04T01:18:10Z | 818,514 | <p>The syntax of metaclasses has <a href="http://docs.python.org/3.0/whatsnew/3.0.html" rel="nofollow">changed</a> in Python 3.0. The <code>__metaclass__</code> attribute is no longer special at either the class nor the module level. To do what you're trying to do, you need to specify <code>metaclass</code> as a keyw... | 2 | 2009-05-04T01:37:32Z | [
"python",
"oop",
"python-3.x",
"metaclass"
] |
Python: Replace string with prefixStringSuffix keeping original case, but ignoring case when searching for match | 818,691 | <p>So what I'm trying to do is replace a string "keyword" with
<code>"<b>keyword</b>"</code>
in a larger string.</p>
<p>Example:</p>
<p>myString = "HI there. You should higher that person for the job. Hi hi."</p>
<p>keyword = "hi"</p>
<p>result I would want would be:</p>
<p><code>result = "<b&... | 2 | 2009-05-04T03:26:39Z | 818,737 | <p>This ok?</p>
<pre><code>>>> import re
>>> myString = "HI there. You should higher that person for the job. Hi hi."
>>> keyword = "hi"
>>> search = re.compile(r'\b(%s)\b' % keyword, re.I)
>>> search.sub('<b>\\1</b>', myString)
'<b>HI</b> there. You... | 3 | 2009-05-04T04:01:01Z | [
"python",
"regex",
"search",
"replace",
"nltk"
] |
Python: Replace string with prefixStringSuffix keeping original case, but ignoring case when searching for match | 818,691 | <p>So what I'm trying to do is replace a string "keyword" with
<code>"<b>keyword</b>"</code>
in a larger string.</p>
<p>Example:</p>
<p>myString = "HI there. You should higher that person for the job. Hi hi."</p>
<p>keyword = "hi"</p>
<p>result I would want would be:</p>
<p><code>result = "<b&... | 2 | 2009-05-04T03:26:39Z | 818,740 | <p>You should be able to do this very easily with <code>re.sub</code> using the word boundary assertion <code>\b</code>, which only matches at a word boundary:</p>
<pre><code>import re
def SurroundWith(text, keyword, before, after):
regex = re.compile(r'\b%s\b' % keyword, re.IGNORECASE)
return regex.sub(r'%s\0%s'... | 0 | 2009-05-04T04:01:59Z | [
"python",
"regex",
"search",
"replace",
"nltk"
] |
Python: Replace string with prefixStringSuffix keeping original case, but ignoring case when searching for match | 818,691 | <p>So what I'm trying to do is replace a string "keyword" with
<code>"<b>keyword</b>"</code>
in a larger string.</p>
<p>Example:</p>
<p>myString = "HI there. You should higher that person for the job. Hi hi."</p>
<p>keyword = "hi"</p>
<p>result I would want would be:</p>
<p><code>result = "<b&... | 2 | 2009-05-04T03:26:39Z | 818,748 | <p>I think the best solution would be regular expression...</p>
<pre><code>import re
def reg(keyword, myString) :
regx = re.compile(r'\b(' + keyword + r')\b', re.IGNORECASE)
return regx.sub(r'<b>\1</b>', myString)
</code></pre>
<p>of course, you must first make your keyword "regular expression safe"... | 0 | 2009-05-04T04:06:50Z | [
"python",
"regex",
"search",
"replace",
"nltk"
] |
Python: Replace string with prefixStringSuffix keeping original case, but ignoring case when searching for match | 818,691 | <p>So what I'm trying to do is replace a string "keyword" with
<code>"<b>keyword</b>"</code>
in a larger string.</p>
<p>Example:</p>
<p>myString = "HI there. You should higher that person for the job. Hi hi."</p>
<p>keyword = "hi"</p>
<p>result I would want would be:</p>
<p><code>result = "<b&... | 2 | 2009-05-04T03:26:39Z | 1,155,326 | <p>Here's one suggestion, from the nitpicking committee. :-)</p>
<pre><code>myString = "HI there. You should higher that person for the job. Hi hi."
myString.replace('higher','hire')
</code></pre>
| 0 | 2009-07-20T19:08:31Z | [
"python",
"regex",
"search",
"replace",
"nltk"
] |
Spliting a file into lines in Python using re.split | 818,705 | <p>I'm trying to split a file with a list comprehension using code similar to:</p>
<pre><code>lines = [x for x in re.split(r"\n+", file.read()) if not re.match(r"com", x)]
</code></pre>
<p>However, the lines list always has an empty string as the last element. Does anyone know a way to avoid this (excluding the cludg... | 1 | 2009-05-04T03:36:47Z | 818,712 | <p>lines = file.readlines()</p>
<p><strong>edit:</strong>
or if you didnt want blank lines in there, you can do</p>
<p>lines = filter(lambda a:(a!='\n'), file.readlines()) </p>
<p><strong>edit^2:</strong>
to remove trailing newines, you can do </p>
<p>lines = [re.sub('\n','',line) for line in filter(lambda a:(a!='\... | 3 | 2009-05-04T03:40:00Z | [
"python",
"regex",
"list-comprehension"
] |
Spliting a file into lines in Python using re.split | 818,705 | <p>I'm trying to split a file with a list comprehension using code similar to:</p>
<pre><code>lines = [x for x in re.split(r"\n+", file.read()) if not re.match(r"com", x)]
</code></pre>
<p>However, the lines list always has an empty string as the last element. Does anyone know a way to avoid this (excluding the cludg... | 1 | 2009-05-04T03:36:47Z | 818,757 | <p>Put the regular expression hammer away :-)</p>
<ol>
<li>You can iterate over a file directly; <code>readlines()</code> is almost obsolete these days.</li>
<li>Read about <a href="http://docs.python.org/library/stdtypes.html#str.strip"><code>str.strip()</code></a> (and its friends, <code>lstrip()</code> and <code>rs... | 8 | 2009-05-04T04:14:20Z | [
"python",
"regex",
"list-comprehension"
] |
Spliting a file into lines in Python using re.split | 818,705 | <p>I'm trying to split a file with a list comprehension using code similar to:</p>
<pre><code>lines = [x for x in re.split(r"\n+", file.read()) if not re.match(r"com", x)]
</code></pre>
<p>However, the lines list always has an empty string as the last element. Does anyone know a way to avoid this (excluding the cludg... | 1 | 2009-05-04T03:36:47Z | 818,851 | <p>This should work, and eliminate the regular expressions as well:</p>
<pre><code>all_lines = (line.rstrip()
for line in open(filename)
if "com" not in line)
# filter out the empty lines
lines = filter(lambda x : x, all_lines)
</code></pre>
<p>Since you're using a list comprehension and not... | 0 | 2009-05-04T05:21:06Z | [
"python",
"regex",
"list-comprehension"
] |
Spliting a file into lines in Python using re.split | 818,705 | <p>I'm trying to split a file with a list comprehension using code similar to:</p>
<pre><code>lines = [x for x in re.split(r"\n+", file.read()) if not re.match(r"com", x)]
</code></pre>
<p>However, the lines list always has an empty string as the last element. Does anyone know a way to avoid this (excluding the cludg... | 1 | 2009-05-04T03:36:47Z | 818,985 | <p>another handy trick, especially when you need the line number, is to use enumerate:</p>
<p><pre><code>
fp = open("myfile.txt", "r")
for n, line in enumerate(fp.readlines()):
dosomethingwith(n, line)
</pre></code></p>
<p>i only found out about enumerate quite recently but it has come in handy quite a few times ... | 1 | 2009-05-04T06:26:10Z | [
"python",
"regex",
"list-comprehension"
] |
Web-Based Music Library (programming concept) | 818,752 | <p>So, I've been tossing this idea around in my head for a while now. At its core, it's mostly a project for me to learn programming. The idea is that, I have a large set of data, my music collection. There are quite a few datasets that my music has. Format, artist, title, album, genre, length, year of release, fil... | 2 | 2009-05-04T04:10:38Z | 818,758 | <p>I think this is a fine project to learn programming with. By using your own "product" you can really get after things that are missing and are much more motivated to learn and better your program - this is known as <a href="http://www.codinghorror.com/blog/archives/000287.html" rel="nofollow">dogfooding</a>. Curious... | 3 | 2009-05-04T04:14:24Z | [
"php",
"python",
"mysql"
] |
Web-Based Music Library (programming concept) | 818,752 | <p>So, I've been tossing this idea around in my head for a while now. At its core, it's mostly a project for me to learn programming. The idea is that, I have a large set of data, my music collection. There are quite a few datasets that my music has. Format, artist, title, album, genre, length, year of release, fil... | 2 | 2009-05-04T04:10:38Z | 818,763 | <p>Working on something you care about is the best way to learn programming, so I think this is a great idea.</p>
<p>I also recommend Python as a place to start. Have fun!</p>
| 1 | 2009-05-04T04:18:10Z | [
"php",
"python",
"mysql"
] |
Web-Based Music Library (programming concept) | 818,752 | <p>So, I've been tossing this idea around in my head for a while now. At its core, it's mostly a project for me to learn programming. The idea is that, I have a large set of data, my music collection. There are quite a few datasets that my music has. Format, artist, title, album, genre, length, year of release, fil... | 2 | 2009-05-04T04:10:38Z | 818,779 | <p>If you use Python, you could build it with the <a href="http://code.google.com/appengine/" rel="nofollow">Google App Engine</a>. It gives you a very nice database interface, and the tutorial will take you from 'Hello world!' to a functioning web app.</p>
<p>You don't even need to upload the result to Google; you c... | 1 | 2009-05-04T04:29:32Z | [
"php",
"python",
"mysql"
] |
Web-Based Music Library (programming concept) | 818,752 | <p>So, I've been tossing this idea around in my head for a while now. At its core, it's mostly a project for me to learn programming. The idea is that, I have a large set of data, my music collection. There are quite a few datasets that my music has. Format, artist, title, album, genre, length, year of release, fil... | 2 | 2009-05-04T04:10:38Z | 818,866 | <p>I think python would be excellent choice as </p>
<ol>
<li>easy to learn but have advanced features too</li>
<li>good web frameworks available e.g. django which you can run on your machine</li>
<li>Word class Free python hosting available i.e. google app engine</li>
<li>Libraries available for almost anything imagin... | 1 | 2009-05-04T05:28:08Z | [
"php",
"python",
"mysql"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 818,836 | <p>Off the top of my head, no.</p>
<p>I think the best you could do is something like this:</p>
<pre><code>def loop(f,n):
for i in xrange(n): f()
loop(lambda: <insert expression here>, 5)
</code></pre>
<p>But I think you can just live with the extra <code>i</code> variable.</p>
<p>Here is the option to u... | 58 | 2009-05-04T05:14:35Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 818,849 | <p>The general idiom for assigning to a value that isn't used is to name it <code>_</code>.</p>
<pre><code>for _ in range(times):
do_stuff()
</code></pre>
| 36 | 2009-05-04T05:20:11Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 818,852 | <p>May be answer would depend on what problem you have with using iterator?
may be use</p>
<pre><code>i = 100
while i:
print i
i-=1
</code></pre>
<p>or </p>
<pre><code>def loop(N, doSomething):
if not N:
return
print doSomething(N)
loop(N-1, doSomething)
loop(100, lambda a:a)
</code></pr... | 2 | 2009-05-04T05:21:23Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 818,888 | <p>You may be looking for</p>
<pre><code>for _ in itertools.repeat(None, times): ...
</code></pre>
<p>this is THE fastest way to iterate <code>times</code> times in Python.</p>
| 46 | 2009-05-04T05:39:39Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 818,903 | <p>What everyone suggesting you to use _ isn't saying is that _ is frequently used as a shortcut to one of the <a href="http://docs.python.org/library/gettext.html">gettext</a> functions, so if you want your software to be available in more than one language then you're best off avoiding using it for other purposes.</p... | 17 | 2009-05-04T05:44:00Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 829,729 | <p>Here's a random idea that utilizes (abuses?) the <a href="http://docs.python.org/reference/datamodel.html#object.%5F%5Fnonzero%5F%5F">data model</a>.</p>
<pre><code>class Counter(object):
def __init__(self, val):
self.val = val
def __nonzero__(self):
self.val -= 1
return self.val >= 0
x = Count... | 9 | 2009-05-06T14:00:39Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 1,356,365 | <pre><code>t=0
for _ in range (0, 10):
print t
t = t+1
</code></pre>
<p>OUTPUT:<br />
0
1
2
3
4
5
6
7
9</p>
| 1 | 2009-08-31T08:10:29Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 8,754,535 | <p>I generally agree with solutions given above. Namely with: </p>
<ol>
<li>Using underscore in <code>for</code>-loop (2 and more lines)</li>
<li>Defining a normal <code>while</code> counter (3 and more lines)</li>
<li>Declaring a custom class with <code>__nonzero__</code> implementation (many more lines) </li>
</ol>
... | 0 | 2012-01-06T07:07:47Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 9,920,109 | <p>You can use _11 (or any number or another invalid identifier) to prevent name-colision with gettext. Any time you use underscore + invalid identifier you get a dummy name that can be used in for loop.</p>
| 5 | 2012-03-29T06:24:20Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 12,837,610 | <p>What about:</p>
<pre><code>while range(some_number):
#do something
</code></pre>
| -7 | 2012-10-11T10:34:01Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 15,392,368 | <p>Instead of an unneeded counter, now you have an unneeded list.
Best solution is to use a variable that starts with "_", that tells syntax checkers that you are aware you are not using the variable.</p>
<pre><code>x = range(5)
while len(x) > 0:
x.pop()
print "Work!"
</code></pre>
| 0 | 2013-03-13T17:30:37Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 15,482,560 | <pre><code>#Return first n items of the iterable as a list
list(itertools.islice(iterable, n))
</code></pre>
<p>Taken from <a href="http://docs.python.org/2/library/itertools.html" rel="nofollow">http://docs.python.org/2/library/itertools.html</a></p>
| -1 | 2013-03-18T17:01:12Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 20,464,059 | <p>We have had some fun with the following, interesting to share so: </p>
<pre><code>class RepeatFunction:
def __init__(self,n=1): self.n = n
def __call__(self,Func):
for i in xrange(self.n):
Func()
return Func
#----usage
k = 0
@RepeatFunction(7) #decorator ... | 0 | 2013-12-09T05:55:27Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 29,060,307 | <p>If you <strong>really</strong> want to avoid putting something with a name (either an iteration variable as in the OP, or unwanted list or unwanted generator returning true the wanted amount of time) you could do it if you really wanted:</p>
<pre><code>for type('', (), {}).x in range(somenumber):
dosomething()
... | 0 | 2015-03-15T11:55:38Z | [
"python",
"loops",
"for-loop",
"range"
] |
Is it possible to implement a Python for range loop without an iterator variable? | 818,828 | <p>Is is possible to do this;</p>
<pre><code>for i in range(some_number):
#do something
</code></pre>
<p>without the i? If you just want to do something x amount of times and don't need the iterator.</p>
| 115 | 2009-05-04T05:06:46Z | 32,925,357 | <p>According to what Alex Martelli said. It is not true that <code>itertools.repeat()</code> is faster than <code>range</code>. </p>
<p>I've run several times a generation of random numbers in for loop using both methods of iteration. Basically in repeats up to 100 000 times <code>range</code> is faster than <code>ite... | -1 | 2015-10-03T17:21:28Z | [
"python",
"loops",
"for-loop",
"range"
] |
Parse a .txt file | 818,936 | <p>I have a .txt file like:</p>
<pre><code>Symbols from __ctype_tab.o:
Name Value Class Type Size Line Section
__ctype |00000000| D | OBJECT |00000004| |.data
__ctype_tab |00000000| r | OBJECT |00000101| |.rodata
Symbols from ... | 1 | 2009-05-04T06:01:35Z | 818,944 | <p>Here is a basic approach. What do you think?</p>
<pre><code># Suppose you have filename "thefile.txt"
import re
obj = ''
for line in file('thefile.txt'):
# Checking for the .o file
match = re.search('Symbols from (.*):', line)
if match:
obj = match.groups()[0]
# Checking for the symbols.
... | 2 | 2009-05-04T06:06:42Z | [
"python",
"parsing",
"text-files"
] |
Parse a .txt file | 818,936 | <p>I have a .txt file like:</p>
<pre><code>Symbols from __ctype_tab.o:
Name Value Class Type Size Line Section
__ctype |00000000| D | OBJECT |00000004| |.data
__ctype_tab |00000000| r | OBJECT |00000101| |.rodata
Symbols from ... | 1 | 2009-05-04T06:01:35Z | 818,946 | <pre><code>for line in open('thefile.txt'):
fields = line.split('|')
if len(fields) < 4: continue
if fields[3].trim() != 'FUNC': continue
dowhateveryouwishwith(line, fields)
</code></pre>
| 8 | 2009-05-04T06:07:10Z | [
"python",
"parsing",
"text-files"
] |
Parse a .txt file | 818,936 | <p>I have a .txt file like:</p>
<pre><code>Symbols from __ctype_tab.o:
Name Value Class Type Size Line Section
__ctype |00000000| D | OBJECT |00000004| |.data
__ctype_tab |00000000| r | OBJECT |00000101| |.rodata
Symbols from ... | 1 | 2009-05-04T06:01:35Z | 822,462 | <p>I think this might cost less than the use of regexes though i am not totally clear on what you are trying to accomplish</p>
<pre><code>symbolList=[]
for line in open('datafile.txt','r'):
if '.o' in line:
tempname=line.split()[-1][0:-2]
pass
if 'FUNC' not in line:
pass
else:
symbolList.append((tempn... | 4 | 2009-05-04T22:42:35Z | [
"python",
"parsing",
"text-files"
] |
WxPython: Cross-Platform Way to Conform Ok/Cancel Button Order | 818,942 | <p>I'm learning wxPython so most of the libraries and classes are new to me.</p>
<p>I'm creating a Preferences dialog class but don't know the best way to make sure the OK/Cancel (or Save/Close) buttons are in the correct order for the platform. This program is intended to run both on GNOME and Windows, so I want to m... | 2 | 2009-05-04T06:04:52Z | 819,026 | <p>The appearance of a dialog can change only if you use stock dialogs (like wx.FileDialog), if you make your own the layout will stay the same on every platform.</p>
<p>wx.Dialog has a CreateStdDialogButtonSizer method that creates a wx.StdDialogButtonSizer with standard buttons where you might see differences in lay... | 4 | 2009-05-04T06:48:22Z | [
"python",
"user-interface",
"cross-platform",
"wxpython"
] |
WxPython: Cross-Platform Way to Conform Ok/Cancel Button Order | 818,942 | <p>I'm learning wxPython so most of the libraries and classes are new to me.</p>
<p>I'm creating a Preferences dialog class but don't know the best way to make sure the OK/Cancel (or Save/Close) buttons are in the correct order for the platform. This program is intended to run both on GNOME and Windows, so I want to m... | 2 | 2009-05-04T06:04:52Z | 819,110 | <p>If you're going to use wx (or any other x-platform toolkit) you'd better trust that it does the right thing, mon!-)</p>
| 0 | 2009-05-04T07:20:45Z | [
"python",
"user-interface",
"cross-platform",
"wxpython"
] |
WxPython: Cross-Platform Way to Conform Ok/Cancel Button Order | 818,942 | <p>I'm learning wxPython so most of the libraries and classes are new to me.</p>
<p>I'm creating a Preferences dialog class but don't know the best way to make sure the OK/Cancel (or Save/Close) buttons are in the correct order for the platform. This program is intended to run both on GNOME and Windows, so I want to m... | 2 | 2009-05-04T06:04:52Z | 819,330 | <p>There's the GenericMessageDialog widget that should do the right thing depending on the platform (but I've never used it so I'm not sure it does). See the wxPython demo. </p>
<p>You can also use the SizedControls addon library (it's part of wxPython). The SizedDialog class helps to create dialogs that conform to th... | 0 | 2009-05-04T08:52:47Z | [
"python",
"user-interface",
"cross-platform",
"wxpython"
] |
WxPython: Cross-Platform Way to Conform Ok/Cancel Button Order | 818,942 | <p>I'm learning wxPython so most of the libraries and classes are new to me.</p>
<p>I'm creating a Preferences dialog class but don't know the best way to make sure the OK/Cancel (or Save/Close) buttons are in the correct order for the platform. This program is intended to run both on GNOME and Windows, so I want to m... | 2 | 2009-05-04T06:04:52Z | 822,790 | <p>You can use a StdDialogButtonSizer</p>
<p><a href="http://www.wxpython.org/docs/api/wx.StdDialogButtonSizer-class.html" rel="nofollow">http://www.wxpython.org/docs/api/wx.StdDialogButtonSizer-class.html</a></p>
<p>So long as your buttons have the standard IDs they will be put in the correct order.</p>
<p>Just to ... | 2 | 2009-05-05T00:38:45Z | [
"python",
"user-interface",
"cross-platform",
"wxpython"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.