title stringlengths 10 172 | question_id int64 469 40.1M | question_body stringlengths 22 48.2k | question_score int64 -44 5.52k | question_date stringlengths 20 20 | answer_id int64 497 40.1M | answer_body stringlengths 18 33.9k | answer_score int64 -38 8.38k | answer_date stringlengths 20 20 | tags listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
python, "a in b" keyword, how about multiple a's? | 763,944 | <p>My adventures in Python continue and my favorite books are silent again. Python offers a built-in way to test if a variable is inside an iterable object, using the 'in' keyword:</p>
<pre><code>if "a" in "abrakadabra" :
print "it is definitely here"
</code></pre>
<p>But is it possible to test if more than one ite... | 15 | 2009-04-18T18:53:11Z | 763,963 | <p>If you want <em>any</em> check then you would use this:</p>
<pre><code>inthere = False
checks = ('a', 'b')
for check in checks:
if check in 'abrakadabra':
inthere = True
break
</code></pre>
<p>If you want <em>all</em> check out you could use this:</p>
<pre><code>inthere = True
checks = ('a', ... | 1 | 2009-04-18T19:01:07Z | [
"python"
] |
python, "a in b" keyword, how about multiple a's? | 763,944 | <p>My adventures in Python continue and my favorite books are silent again. Python offers a built-in way to test if a variable is inside an iterable object, using the 'in' keyword:</p>
<pre><code>if "a" in "abrakadabra" :
print "it is definitely here"
</code></pre>
<p>But is it possible to test if more than one ite... | 15 | 2009-04-18T18:53:11Z | 763,979 | <p>You could also use <a href="http://docs.python.org/3.0/library/stdtypes.html#set-types-set-frozenset" rel="nofollow">set methods and operators</a>:</p>
<pre><code>not alternatives.isdisjoint(sTxt) # for "any"
(alternatives & sTxt) != set() # Again, the intersection is nonempty
alternatives <= sTxt # for "... | 1 | 2009-04-18T19:12:29Z | [
"python"
] |
python, "a in b" keyword, how about multiple a's? | 763,944 | <p>My adventures in Python continue and my favorite books are silent again. Python offers a built-in way to test if a variable is inside an iterable object, using the 'in' keyword:</p>
<pre><code>if "a" in "abrakadabra" :
print "it is definitely here"
</code></pre>
<p>But is it possible to test if more than one ite... | 15 | 2009-04-18T18:53:11Z | 764,117 | <p>If you're testing lots of lines for the same words, it may be faster to compile them as a regular expression. eg:</p>
<pre><code>import re
words = ["// @in ", "// @out ", "// @ret "] + ["// @test%s " % i for i in range(10)]
my_regex = re.compile("|".join(map(re.escape, words)))
for line in lines_to_search:
... | 6 | 2009-04-18T20:20:41Z | [
"python"
] |
Determine if a function is available in a Python module | 763,971 | <p>I am working on some Python socket code that's using the <a href="http://docs.python.org/library/socket.html#socket.fromfd"><code>socket.fromfd()</code></a> function.</p>
<p>However, this method is not available on all platforms, so I am writing some fallback code in the case that the method is not defined.</p>
<p... | 14 | 2009-04-18T19:06:00Z | 763,975 | <p><a href="http://docs.python.org/library/functions.html#hasattr"><code>hasattr()</code></a> is the best choice. Go with that. :)</p>
<pre><code>if hasattr(socket, 'fromfd'):
pass
else:
pass
</code></pre>
<p><strong>EDIT</strong>: Actually, according to the docs all hasattr is doing is calling getattr and c... | 17 | 2009-04-18T19:09:31Z | [
"python"
] |
Determine if a function is available in a Python module | 763,971 | <p>I am working on some Python socket code that's using the <a href="http://docs.python.org/library/socket.html#socket.fromfd"><code>socket.fromfd()</code></a> function.</p>
<p>However, this method is not available on all platforms, so I am writing some fallback code in the case that the method is not defined.</p>
<p... | 14 | 2009-04-18T19:06:00Z | 763,977 | <p>Or simply use a try..except block:</p>
<pre><code>try:
sock = socket.fromfd(...)
except AttributeError:
sock = socket.socket(...)
</code></pre>
| 17 | 2009-04-18T19:11:40Z | [
"python"
] |
Determine if a function is available in a Python module | 763,971 | <p>I am working on some Python socket code that's using the <a href="http://docs.python.org/library/socket.html#socket.fromfd"><code>socket.fromfd()</code></a> function.</p>
<p>However, this method is not available on all platforms, so I am writing some fallback code in the case that the method is not defined.</p>
<p... | 14 | 2009-04-18T19:06:00Z | 763,992 | <p>hasattr(obj, 'attributename') is probably a better one. hasattr will try to access the attribute, and if it's not there, it'll return false. </p>
<p>It's possible to have dynamic methods in python, i.e. methods that are created when you try to access them. They would not be in dir(...). However hasattr would check ... | 2 | 2009-04-18T19:20:28Z | [
"python"
] |
mod_wsgi/python sys.path.exend problems | 764,081 | <p>I'm working on a mod_wsgi script.. at the beginning is:</p>
<pre><code>sys.path.extend(map(os.path.abspath, ['/media/server/www/webroot/']))
</code></pre>
<p>But I've noticed, that every time I update the script the sys.path var keeps growing with duplicates of this extension:</p>
<pre><code>['/usr/lib64/python25... | 1 | 2009-04-18T20:02:05Z | 764,148 | <p>One fairly simple way to do this is to check to see if the path has already been extended before extending it::</p>
<pre><code>path_extension = map(os.path.abspath,['/media/server/www/webroot/'])
if path_extension[0] not in sys.path:
sys.path.extend(path_extension)
</code></pre>
<p>This has the disadvantage, ... | 3 | 2009-04-18T20:33:16Z | [
"python",
"apache",
"mod-wsgi"
] |
mod_wsgi/python sys.path.exend problems | 764,081 | <p>I'm working on a mod_wsgi script.. at the beginning is:</p>
<pre><code>sys.path.extend(map(os.path.abspath, ['/media/server/www/webroot/']))
</code></pre>
<p>But I've noticed, that every time I update the script the sys.path var keeps growing with duplicates of this extension:</p>
<pre><code>['/usr/lib64/python25... | 1 | 2009-04-18T20:02:05Z | 764,683 | <p>No need to worry about checking or using abspath yourself. Use the âsiteâ module's built-in <a href="http://docs.python.org/library/site.html#site.addsitedir">addsitedir</a> function. It will take care of these issues and others (eg. pth files) automatically:</p>
<pre><code>import site
site.addsitedir('/media/s... | 7 | 2009-04-19T02:15:56Z | [
"python",
"apache",
"mod-wsgi"
] |
mod_wsgi/python sys.path.exend problems | 764,081 | <p>I'm working on a mod_wsgi script.. at the beginning is:</p>
<pre><code>sys.path.extend(map(os.path.abspath, ['/media/server/www/webroot/']))
</code></pre>
<p>But I've noticed, that every time I update the script the sys.path var keeps growing with duplicates of this extension:</p>
<pre><code>['/usr/lib64/python25... | 1 | 2009-04-18T20:02:05Z | 766,705 | <p>The mod_wsgi <a href="http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode" rel="nofollow">documentation on code reloading</a> covers this.</p>
| 2 | 2009-04-20T02:51:33Z | [
"python",
"apache",
"mod-wsgi"
] |
Trying to get enum style choices= working for django but the whole tuplets are appearing in the drop down | 764,177 | <p>Im using appengine and the appenginepatch (so my issue could be related to that)</p>
<p>I have set up a model with a property that has several choices but when trying to display on a form or via admin interface I am getting an error:</p>
<blockquote>
<p>Property mode is 'o'; must be one of (('s', 'Single'), ('m'... | 1 | 2009-04-18T20:50:54Z | 764,332 | <p>It looks like this is an issue in Django/appengine support. It's documented <a href="http://code.google.com/p/google-app-engine-django/issues/detail?id=72" rel="nofollow">here</a> on the google-app-engine-django bug tracker, but it's closed as "wontfix" there. It is also documented <a href="http://code.google.com/... | 2 | 2009-04-18T22:11:45Z | [
"python",
"django",
"google-app-engine"
] |
Python: How do I get time from a datetime.timedelta object? | 764,184 | <p>A mysql database table has a column whose datatype is time ( <a href="http://dev.mysql.com/doc/refman/5.0/en/time.html">http://dev.mysql.com/doc/refman/5.0/en/time.html</a> ). When the table data is accessed, Python returns the value of this column as a datetime.timedelta object. How do I extract the time out of thi... | 16 | 2009-04-18T20:55:48Z | 764,198 | <p>It's strange that Python returns the value as a <code>datetime.timedelta</code>. It probably should return a <code>datetime.time</code>. Anyway, it looks like it's returning the elapsed time since midnight (assuming the column in the table is 6:00 PM). In order to convert to a <code>datetime.time</code>, you can ... | 22 | 2009-04-18T21:04:40Z | [
"python",
"mysql",
"timedelta"
] |
Python: How do I get time from a datetime.timedelta object? | 764,184 | <p>A mysql database table has a column whose datatype is time ( <a href="http://dev.mysql.com/doc/refman/5.0/en/time.html">http://dev.mysql.com/doc/refman/5.0/en/time.html</a> ). When the table data is accessed, Python returns the value of this column as a datetime.timedelta object. How do I extract the time out of thi... | 16 | 2009-04-18T20:55:48Z | 26,671,843 | <p>It seems to me that the TIME type in MySQL is intended to represent time intervals as datetime.timedelta does in Python. From the docs you referenced:</p>
<blockquote>
<p>TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type can be used not only to represent ... | 1 | 2014-10-31T09:57:54Z | [
"python",
"mysql",
"timedelta"
] |
Dictionary to lowercase in Python | 764,235 | <p>I wish to do this but for a dictionary:</p>
<pre><code>"My string".lower()
</code></pre>
<p>Is there a built in function or should I use a loop?</p>
| 27 | 2009-04-18T21:23:14Z | 764,244 | <p>You will need to use either a loop or a list/generator comprehension. If you want to lowercase all the keys and values, you can do this::</p>
<pre><code>dict((k.lower(), v.lower()) for k,v in {'My Key':'My Value'}.iteritems())
</code></pre>
<p>If you want to lowercase just the keys, you can do this::</p>
<pre><c... | 53 | 2009-04-18T21:31:02Z | [
"python"
] |
Dictionary to lowercase in Python | 764,235 | <p>I wish to do this but for a dictionary:</p>
<pre><code>"My string".lower()
</code></pre>
<p>Is there a built in function or should I use a loop?</p>
| 27 | 2009-04-18T21:23:14Z | 765,409 | <p>The following is identical to Rick Copeland's answer, just written without a using generator expression:</p>
<pre><code>outdict = {}
for k, v in {'My Key': 'My Value'}.iteritems():
outdict[k.lower()] = v.lower()
</code></pre>
<p>Generator-expressions, list comprehension's and (in Python 2.7 and higher) dict co... | 13 | 2009-04-19T13:25:52Z | [
"python"
] |
Dictionary to lowercase in Python | 764,235 | <p>I wish to do this but for a dictionary:</p>
<pre><code>"My string".lower()
</code></pre>
<p>Is there a built in function or should I use a loop?</p>
| 27 | 2009-04-18T21:23:14Z | 34,940,226 | <p>If the dictionary supplied have multiple type of key/values(numeric, string etc.); then use the following solution.</p>
<p>For example; if you have a dictionary named mydict as shown below</p>
<pre><code>mydict = {"FName":"John","LName":"Doe",007:true}
</code></pre>
<p><strong>In Python 2.x</strong></p>
<pre><co... | 0 | 2016-01-22T06:18:25Z | [
"python"
] |
Dictionary to lowercase in Python | 764,235 | <p>I wish to do this but for a dictionary:</p>
<pre><code>"My string".lower()
</code></pre>
<p>Is there a built in function or should I use a loop?</p>
| 27 | 2009-04-18T21:23:14Z | 37,166,894 | <pre><code>def convert_to_lower_case(data):
if type(data) is dict:
for k, v in data.iteritems():
if type(v) is str:
data[k] = v.lower()
elif type(v) is list:
data[k] = [x.lower() for x in v]
elif type(v) is dict:
data[k] = c... | -1 | 2016-05-11T15:18:00Z | [
"python"
] |
Dictionary to lowercase in Python | 764,235 | <p>I wish to do this but for a dictionary:</p>
<pre><code>"My string".lower()
</code></pre>
<p>Is there a built in function or should I use a loop?</p>
| 27 | 2009-04-18T21:23:14Z | 38,572,808 | <p><strong>Shorter way in python 3:</strong> <code>{k.lower(): v for k, v in my_dict.items()}</code></p>
| 1 | 2016-07-25T16:17:22Z | [
"python"
] |
How do I modify sys.path from .htaccess to allow mod_python to see Django? | 764,312 | <p>The host I'm considering for hosting a Django site has mod_python installed, but does not have Django. Django's INSTALL file indicates that I can simply copy the django directory to Python's site-packages directory to install Django, so I suspect that it might be possible to configure Python / mod_python to look for... | 4 | 2009-04-18T22:02:45Z | 764,341 | <p>Is the <a href="http://www.modpython.org/live/current/doc-html/dir-other-pp.html" rel="nofollow">PythonPath</a> setting what you are looking for? I haven't tried it with Django, but I would assume that it should do the job for you.</p>
| 1 | 2009-04-18T22:20:16Z | [
"python",
"django",
"apache",
".htaccess",
"mod-python"
] |
How do I modify sys.path from .htaccess to allow mod_python to see Django? | 764,312 | <p>The host I'm considering for hosting a Django site has mod_python installed, but does not have Django. Django's INSTALL file indicates that I can simply copy the django directory to Python's site-packages directory to install Django, so I suspect that it might be possible to configure Python / mod_python to look for... | 4 | 2009-04-18T22:02:45Z | 764,349 | <p>According to <a href="http://code.djangoproject.com/ticket/2255" rel="nofollow">ticket #2255</a> for Django, you need admin access to httpd.conf in order to use Django with mod_python, and this is not going to change, so you may be dead in the water. To answer the basic question of how to modify <code>sys.path</cod... | 3 | 2009-04-18T22:22:26Z | [
"python",
"django",
"apache",
".htaccess",
"mod-python"
] |
How do I modify sys.path from .htaccess to allow mod_python to see Django? | 764,312 | <p>The host I'm considering for hosting a Django site has mod_python installed, but does not have Django. Django's INSTALL file indicates that I can simply copy the django directory to Python's site-packages directory to install Django, so I suspect that it might be possible to configure Python / mod_python to look for... | 4 | 2009-04-18T22:02:45Z | 764,769 | <p>You're using mod_python wrong. It was never intended to serve python web applications. You should be using WSGI for this... or at least FastCGI.</p>
| 1 | 2009-04-19T03:15:58Z | [
"python",
"django",
"apache",
".htaccess",
"mod-python"
] |
A list of string replacements in Python | 764,360 | <p>Is there a far shorter way to write the following code?</p>
<pre><code>my_string = my_string.replace('A', '1')
my_string = my_string.replace('B', '2')
my_string = my_string.replace('C', '3')
my_string = my_string.replace('D', '4')
my_string = my_string.replace('E', '5')
</code></pre>
<p>Note that I don't need thos... | 17 | 2009-04-18T22:28:06Z | 764,368 | <pre>replaceDict = {'A':'1','B':'2','C':'3','D':'4','E':'5'}
for key, replacement in replaceDict.items():
my_string = my_string.replace( key, replacement )</pre>
| 6 | 2009-04-18T22:32:28Z | [
"python",
"string",
"replace"
] |
A list of string replacements in Python | 764,360 | <p>Is there a far shorter way to write the following code?</p>
<pre><code>my_string = my_string.replace('A', '1')
my_string = my_string.replace('B', '2')
my_string = my_string.replace('C', '3')
my_string = my_string.replace('D', '4')
my_string = my_string.replace('E', '5')
</code></pre>
<p>Note that I don't need thos... | 17 | 2009-04-18T22:28:06Z | 764,374 | <p>Looks like a good opportunity to use a loop:</p>
<pre><code>mapping = { 'A':'1', 'B':'2', 'C':'3', 'D':'4', 'E':'5'}
for k, v in mapping.iteritems():
my_string = my_string.replace(k, v)
</code></pre>
<p>A faster approach if you don't mind the parentheses would be:</p>
<pre><code>mapping = [ ('A', '1'), ('B', ... | 33 | 2009-04-18T22:36:06Z | [
"python",
"string",
"replace"
] |
A list of string replacements in Python | 764,360 | <p>Is there a far shorter way to write the following code?</p>
<pre><code>my_string = my_string.replace('A', '1')
my_string = my_string.replace('B', '2')
my_string = my_string.replace('C', '3')
my_string = my_string.replace('D', '4')
my_string = my_string.replace('E', '5')
</code></pre>
<p>Note that I don't need thos... | 17 | 2009-04-18T22:28:06Z | 764,385 | <p>Also look into <a href="http://docs.python.org/library/stdtypes.html#str.translate" rel="nofollow"><code>str.translate()</code></a>. It replaces characters according to a mapping you provide for Unicode strings, or otherwise must be told what to replace each character from chr(0) to chr(255) with.</p>
| 15 | 2009-04-18T22:43:25Z | [
"python",
"string",
"replace"
] |
A list of string replacements in Python | 764,360 | <p>Is there a far shorter way to write the following code?</p>
<pre><code>my_string = my_string.replace('A', '1')
my_string = my_string.replace('B', '2')
my_string = my_string.replace('C', '3')
my_string = my_string.replace('D', '4')
my_string = my_string.replace('E', '5')
</code></pre>
<p>Note that I don't need thos... | 17 | 2009-04-18T22:28:06Z | 764,591 | <p>You can easily use string.maketrans() to create the mapping string to pass to str.translate():</p>
<pre><code>import string
trans = string.maketrans("ABCDE","12345")
my_string = my_string.translate(trans)
</code></pre>
| 29 | 2009-04-19T00:59:10Z | [
"python",
"string",
"replace"
] |
A list of string replacements in Python | 764,360 | <p>Is there a far shorter way to write the following code?</p>
<pre><code>my_string = my_string.replace('A', '1')
my_string = my_string.replace('B', '2')
my_string = my_string.replace('C', '3')
my_string = my_string.replace('D', '4')
my_string = my_string.replace('E', '5')
</code></pre>
<p>Note that I don't need thos... | 17 | 2009-04-18T22:28:06Z | 765,835 | <p>If you want to get the wrong answer, slowly, then use string.replace in a loop. (Though it does work in this case of no overlap among the patterns and replacements.)</p>
<p>For the general case with possible overlaps or a long subject string, use re.sub:</p>
<pre><code>import re
def multisub(subs, subject):
"... | 9 | 2009-04-19T18:20:59Z | [
"python",
"string",
"replace"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 764,418 | <p>Some uses cases I have seen before include making <a href="http://code.google.com/p/metapython/wiki/Tutorial" rel="nofollow">class factories</a> or <a href="http://blog.pythonisito.com/2009/03/announcing-metapython-macros-for-python.html" rel="nofollow">stripping logging</a> statements out of production code.</p>
| 3 | 2009-04-18T23:00:39Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 764,419 | <p>There's a <a href="http://lists.warhead.org.uk/pipermail/iwe/2005-July/000130.html" rel="nofollow">mailing list posting</a> (<a href="http://web.archive.org/web/20080117093622/http%3A//lists.warhead.org.uk/pipermail/iwe/2005-July/000130.html" rel="nofollow">archive.org mirror</a>) which explains this rather well. Th... | 4 | 2009-04-18T23:01:42Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 764,425 | <p>I believe that macros run counter to Python's culture. Macros in Lisp allow the <a href="http://en.wikipedia.org/wiki/Big%5Fball%5Fof%5Fmud">big ball of mud</a> approach; you get to redefine the language to become more suited to your problem domain. Conversely Pythonic code uses the most natural built in feature of ... | 14 | 2009-04-18T23:05:46Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 764,459 | <p>In lisp, macros are just another way to abstract ideas.</p>
<p>This is an example from an incomplete ray-tracer written in clojure:</p>
<pre><code>(defmacro per-pixel
"Macro.
Excecutes body for every pixel. Binds i and j to the current pixel coord."
[i j & body]
`(dotimes [~i @width]
(dotimes [~j @h... | 4 | 2009-04-18T23:22:39Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 764,466 | <p>See also this question: <a href="http://stackoverflow.com/questions/454648/pythonic-macro-syntax">Pythonic macro syntax</a></p>
| 2 | 2009-04-18T23:24:12Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 764,906 | <p>Some examples of lisp macros:</p>
<ul>
<li><a href="http://common-lisp.net/project/iterate">ITERATE</a> which is a funny and extensible loop facility</li>
<li><a href="http://www.pps.jussieu.fr/~jch/software/cl-yacc/">CL-YACC</a>/<a href="http://common-lisp.net/project/fucc">FUCC</a> that are parser generators that... | 12 | 2009-04-19T05:45:58Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 764,990 | <p>I don't think Python needs macros, because they are useful for 2 things:</p>
<ol>
<li><p>Creating a DSL or more eloquent syntax for something (Lisp LOOP macro is a nice example). In this case, Python philosophy decided against it deliberately. If there is some explicit notation you're missing, you can always ask fo... | 2 | 2009-04-19T07:05:08Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 983,180 | <p>Read "The Lambda Papers" so you might find out generally why one would take advtage of macros at all.</p>
<p>You should start with âAIM-353 Lambda:The Ultimate Imperativeâ and follow it with âAIM-443 Lambda: The Ultimate GOTOâ. Both may be found here:</p>
<p><a href="http://library.readscheme.org/page1.htm... | 1 | 2009-06-11T19:37:34Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 2,444,061 | <p>Here's one real-world example I came across that would be trivial with macros or real metaprogramming support, but has to be done with CPython bytecode manipulation due to absence of both in Python:</p>
<p><a href="http://www.aminus.net/dejavu/chrome/common/doc/2.0a/html/intro.html#cpython">http://www.aminus.net/de... | 6 | 2010-03-14T22:28:58Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 6,200,044 | <p>Hy,
For my own use, I created a Python module (Espy) that allows macro definitions with arguments, loop and conditional code generation:
You create a source.espy file, then launch the appropriate function, then source.py is generated.</p>
<p>It allows syntaxes as following:</p>
<pre><code>macro repeat(arg1):
f... | 1 | 2011-06-01T10:57:08Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 8,869,336 | <p>Possibly if you want the source code at runtime such as for debugging (say printf debugging an expression's value with the name of it so you don't have to write it twice).</p>
<p><a href="http://stackoverflow.com/questions/8573023/python-is-there-no-better-way-to-get-the-expression-in-a-debug-function">The only way... | 0 | 2012-01-15T11:37:54Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 10,083,737 | <p>Well, I'd like instead of</p>
<pre><code>print >> sys.stderr, "abc"
</code></pre>
<p>to write</p>
<pre><code>err "abc"
</code></pre>
<p>in some scripts which have many debug printout statements.</p>
<p>I can do</p>
<pre><code>import sys
err = sys.stderr
</code></pre>
<p>and then </p>
<pre><code>print &... | 0 | 2012-04-10T05:51:02Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 14,507,882 | <p>I want to use macro to enable sql statement in python code. -
select * from table1</p>
| 0 | 2013-01-24T18:08:35Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 15,610,890 | <p>I'd use it to wrap <code>yield</code> to enable me to build more powerful generator pipelines.</p>
| 0 | 2013-03-25T08:55:04Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 16,174,524 | <p>Currently, the only way features can be added to Python the language is through a PEP (Python Enhancement Proposal). This can be slow, and doesn't help you in the cases when you want to add a feature to the language that is only useful for your use case.</p>
<p>For example, <a href="http://www.python.org/dev/peps/p... | 0 | 2013-04-23T16:27:33Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 16,493,714 | <p>This is a somewhat late answer, but <a href="https://github.com/lihaoyi/macropy">MacroPy</a> is a new project of mine to bring macros to Python. We have a pretty substantial list of demos, all of which are use cases which require macros to implement, for example providing an extremely concise way of declaring classe... | 13 | 2013-05-11T04:19:22Z | [
"python",
"macros",
"lisp",
"scheme"
] |
Python Macros: Use Cases? | 764,412 | <p>If Python had a macro facility similar to Lisp/Scheme (something like <a href="https://code.google.com/p/metapython/" rel="nofollow">MetaPython</a>), how would you use it? </p>
<p>If you are a Lisp/Scheme programmer, what sorts of things do you use macros for (other than things that have a clear syntactic parallel... | 20 | 2009-04-18T22:56:16Z | 22,334,057 | <p>Coming from a C-world, I'd really appreciate being able to do efficient logging of rich messages. Instead of writing</p>
<pre><code>if logger.level > logger.DEBUG:
logger.log('%s' % (an_expensive_call_that_gives_useful_info(),))
</code></pre>
<p>with macros, one could instead do something like</p>
<pre><c... | 0 | 2014-03-11T18:53:32Z | [
"python",
"macros",
"lisp",
"scheme"
] |
How to hide console window in python? | 764,631 | <p>I am writing an IRC bot in Python. </p>
<p>I wish to make stand-alone binaries for Linux and Windows of it. And mainly I wish that when the bot initiates, the console window should hide and the user should not be able to see the window.</p>
<p>What can I do for that?</p>
| 33 | 2009-04-19T01:35:30Z | 764,642 | <p>In linux, just run it, no problem. In Windows, you want to use the pythonw executable.</p>
<h3>Update</h3>
<p>Okay, if I understand the question in the comments, you're asking how to make the command window in which you've started the bot from the command line go away afterwards?</p>
<ul>
<li>UNIX (Linux)</li>
<... | 13 | 2009-04-19T01:44:08Z | [
"python",
"console",
"hide"
] |
How to hide console window in python? | 764,631 | <p>I am writing an IRC bot in Python. </p>
<p>I wish to make stand-alone binaries for Linux and Windows of it. And mainly I wish that when the bot initiates, the console window should hide and the user should not be able to see the window.</p>
<p>What can I do for that?</p>
| 33 | 2009-04-19T01:35:30Z | 764,654 | <p>Simply save it with a <code>.pyw</code> extension. This will prevent the console window from opening.</p>
<blockquote>
<p>On Windows systems, there is no notion of an âexecutable modeâ. The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it ... | 54 | 2009-04-19T01:51:12Z | [
"python",
"console",
"hide"
] |
How to hide console window in python? | 764,631 | <p>I am writing an IRC bot in Python. </p>
<p>I wish to make stand-alone binaries for Linux and Windows of it. And mainly I wish that when the bot initiates, the console window should hide and the user should not be able to see the window.</p>
<p>What can I do for that?</p>
| 33 | 2009-04-19T01:35:30Z | 15,148,338 | <p>Save it with a <code>.pyw</code> extension and will open with <code>pythonw.exe</code>. Double click it, and Python will start without a window. </p>
<p>For example, if you have the following script name:</p>
<pre><code>foo.py
</code></pre>
<p>Rename it to:</p>
<pre><code>foo.pyw
</code></pre>
| 7 | 2013-03-01T00:30:37Z | [
"python",
"console",
"hide"
] |
SQLite Performance Benchmark -- why is :memory: so slow...only 1.5X as fast as disk? | 764,710 | <h2>Why is :memory: in sqlite so slow?</h2>
<p>I've been trying to see if there are any performance improvements gained by using in-memory sqlite vs. disk based sqlite. Basically I'd like to trade startup time and memory to get extremely rapid queries which do <em>not</em> hit disk during the course of the application... | 37 | 2009-04-19T02:33:43Z | 764,718 | <p>Could it be that sqlite3 isnt actually writing your data to disk from cache? which might explain why the numbers are similar.</p>
<p>It could also be possible that your OS is paging due to low memory avaliable? </p>
| 1 | 2009-04-19T02:40:29Z | [
"python",
"database",
"sqlite",
"memory",
"benchmarking"
] |
SQLite Performance Benchmark -- why is :memory: so slow...only 1.5X as fast as disk? | 764,710 | <h2>Why is :memory: in sqlite so slow?</h2>
<p>I've been trying to see if there are any performance improvements gained by using in-memory sqlite vs. disk based sqlite. Basically I'd like to trade startup time and memory to get extremely rapid queries which do <em>not</em> hit disk during the course of the application... | 37 | 2009-04-19T02:33:43Z | 764,719 | <p>I note you are focussing on queries that involve relatively large data sets to return. I wonder what effect you would see with smaller sets of data? To return a single row many times might require the disk to seek a lot - the random access time for memory might be much faster.</p>
| 1 | 2009-04-19T02:40:59Z | [
"python",
"database",
"sqlite",
"memory",
"benchmarking"
] |
SQLite Performance Benchmark -- why is :memory: so slow...only 1.5X as fast as disk? | 764,710 | <h2>Why is :memory: in sqlite so slow?</h2>
<p>I've been trying to see if there are any performance improvements gained by using in-memory sqlite vs. disk based sqlite. Basically I'd like to trade startup time and memory to get extremely rapid queries which do <em>not</em> hit disk during the course of the application... | 37 | 2009-04-19T02:33:43Z | 764,743 | <p>It has to do with the fact that SQLite has a page cache. According to the <a href="http://www.sqlite.org/compile.html">Documentation</a>, the default page cache is 2000 1K pages or about 2Mb. Since this is about 75% to 90% of your data, it isn't surprising that the two number are very similar. My guess is that in ad... | 32 | 2009-04-19T02:57:03Z | [
"python",
"database",
"sqlite",
"memory",
"benchmarking"
] |
SQLite Performance Benchmark -- why is :memory: so slow...only 1.5X as fast as disk? | 764,710 | <h2>Why is :memory: in sqlite so slow?</h2>
<p>I've been trying to see if there are any performance improvements gained by using in-memory sqlite vs. disk based sqlite. Basically I'd like to trade startup time and memory to get extremely rapid queries which do <em>not</em> hit disk during the course of the application... | 37 | 2009-04-19T02:33:43Z | 765,136 | <p>You're doing SELECTs, you're using memory cache. Try to interleave SELECTs with UPDATEs.</p>
| 7 | 2009-04-19T09:38:08Z | [
"python",
"database",
"sqlite",
"memory",
"benchmarking"
] |
SQLite Performance Benchmark -- why is :memory: so slow...only 1.5X as fast as disk? | 764,710 | <h2>Why is :memory: in sqlite so slow?</h2>
<p>I've been trying to see if there are any performance improvements gained by using in-memory sqlite vs. disk based sqlite. Basically I'd like to trade startup time and memory to get extremely rapid queries which do <em>not</em> hit disk during the course of the application... | 37 | 2009-04-19T02:33:43Z | 790,522 | <p>Memory database in SQLite is actually page cache that never touches the disk.
So you should forget using memory db in SQLite for performance tweaks</p>
<p>It's possible to turn off journal, turn off sync mode, set large page cache and you will have almost the same performance on most of operations, but durability w... | 6 | 2009-04-26T09:07:26Z | [
"python",
"database",
"sqlite",
"memory",
"benchmarking"
] |
SQLite Performance Benchmark -- why is :memory: so slow...only 1.5X as fast as disk? | 764,710 | <h2>Why is :memory: in sqlite so slow?</h2>
<p>I've been trying to see if there are any performance improvements gained by using in-memory sqlite vs. disk based sqlite. Basically I'd like to trade startup time and memory to get extremely rapid queries which do <em>not</em> hit disk during the course of the application... | 37 | 2009-04-19T02:33:43Z | 1,056,148 | <p>My question to you is, What are you trying to benchmark?</p>
<p>As already mentioned, SQLite's :memory: DB is just the same as the disk-based one, i.e. paged, and the only difference is that the pages are never written to disk. So the only difference between the two are the disk writes :memory: doesn't need to do (... | 17 | 2009-06-29T00:45:03Z | [
"python",
"database",
"sqlite",
"memory",
"benchmarking"
] |
SQLite Performance Benchmark -- why is :memory: so slow...only 1.5X as fast as disk? | 764,710 | <h2>Why is :memory: in sqlite so slow?</h2>
<p>I've been trying to see if there are any performance improvements gained by using in-memory sqlite vs. disk based sqlite. Basically I'd like to trade startup time and memory to get extremely rapid queries which do <em>not</em> hit disk during the course of the application... | 37 | 2009-04-19T02:33:43Z | 7,851,376 | <p>numpy arrays are slower than dict and tuple and other object sequences until you are dealing with 5 million or more objects in a sequence. You can significantly improve the speed of processing massive amounts of data by iterating over it and using generators to avoid creating and recreating temporary large objects.<... | 0 | 2011-10-21T15:04:28Z | [
"python",
"database",
"sqlite",
"memory",
"benchmarking"
] |
SQLite Performance Benchmark -- why is :memory: so slow...only 1.5X as fast as disk? | 764,710 | <h2>Why is :memory: in sqlite so slow?</h2>
<p>I've been trying to see if there are any performance improvements gained by using in-memory sqlite vs. disk based sqlite. Basically I'd like to trade startup time and memory to get extremely rapid queries which do <em>not</em> hit disk during the course of the application... | 37 | 2009-04-19T02:33:43Z | 10,404,068 | <p>Thank you for the code. I have tested in on 2 x XEON 2690 with 192GB RAM with 4 SCSI 15k hard drives in RAID 5 and the results are:</p>
<pre><code> disk | memory | qsize
-----------------------
6.3590 | 2.3280 | 15713
6.6250 | 2.3690 | 8914
6.0040 | 2.3260 | 225168
6.0210 | 2.4080 | 132388
6.1400 | 2.4050 | 264038... | 3 | 2012-05-01T20:43:36Z | [
"python",
"database",
"sqlite",
"memory",
"benchmarking"
] |
how to send mail in python ssmtp vs smtplib | 764,778 | <p>I need to send email in delbian linux. How to send? I run my server on 256 MB linux box and I heard postfix and sendmail is overkill.</p>
<p>Recently I came across the ssmtp, that seems to be an executable, needs to be executed as a process and called through python using os modules.</p>
<p>alternatively, python a... | 3 | 2009-04-19T03:24:33Z | 765,044 | <p>In a Python program, there is no advantage.</p>
<p>The only purpose of ssmtp is to wrap the SMTP protocol in the sendmail API. That is, it provides a program <code>/usr/sbin/sendmail</code> that accepts the same options, arguments, and inputs as the full-blown sendmail (though most of the options do nothing); but b... | 5 | 2009-04-19T08:03:55Z | [
"python",
"smtplib",
"ssmtp"
] |
how to send mail in python ssmtp vs smtplib | 764,778 | <p>I need to send email in delbian linux. How to send? I run my server on 256 MB linux box and I heard postfix and sendmail is overkill.</p>
<p>Recently I came across the ssmtp, that seems to be an executable, needs to be executed as a process and called through python using os modules.</p>
<p>alternatively, python a... | 3 | 2009-04-19T03:24:33Z | 766,413 | <p>Additionally, <em>postfix</em> is very easy to install in "satellite" mode, where all it does is queue and deliver email for you. Way easier than implementing your own email queue. Most decent package management systems will let you configure it this way.</p>
| 2 | 2009-04-19T23:42:01Z | [
"python",
"smtplib",
"ssmtp"
] |
how to send mail in python ssmtp vs smtplib | 764,778 | <p>I need to send email in delbian linux. How to send? I run my server on 256 MB linux box and I heard postfix and sendmail is overkill.</p>
<p>Recently I came across the ssmtp, that seems to be an executable, needs to be executed as a process and called through python using os modules.</p>
<p>alternatively, python a... | 3 | 2009-04-19T03:24:33Z | 768,031 | <p>There are other lightweight SMTP senders, such as <a href="http://msmtp.sourceforge.net/" rel="nofollow">msmtp</a>, the one I prefer.</p>
<p>But Postfix is fine for a 256 Mb machine. The good thing about a full MTA like Postfix is that it keeps the message and retries if the destination server is down. With smtplib... | 1 | 2009-04-20T12:22:44Z | [
"python",
"smtplib",
"ssmtp"
] |
Drawing a Dragons curve in Python | 765,048 | <p>I am trying to work out how to draw the dragons curve, with pythons turtle using the An L-System or Lindenmayer system. I no the code is something like </p>
<p>the Dragon curve; initial state = âFâ, replacement rule â replace âFâ with âF+F-Fâ, number of replacements = 8, length = 5, angle = 60</p>
<... | -1 | 2009-04-19T08:06:50Z | 765,072 | <p>First hit on Google for "dragons curve python": </p>
<p><a href="http://www.pynokio.org/dragon.py.htm" rel="nofollow">http://www.pynokio.org/dragon.py.htm</a></p>
<p>You can probably modify that to work with your plotting program of choice. I'd try matplotlib. </p>
| 3 | 2009-04-19T08:30:21Z | [
"python",
"fractals"
] |
Drawing a Dragons curve in Python | 765,048 | <p>I am trying to work out how to draw the dragons curve, with pythons turtle using the An L-System or Lindenmayer system. I no the code is something like </p>
<p>the Dragon curve; initial state = âFâ, replacement rule â replace âFâ with âF+F-Fâ, number of replacements = 8, length = 5, angle = 60</p>
<... | -1 | 2009-04-19T08:06:50Z | 766,310 | <p>Well, presumably, you could start by defining:</p>
<pre><code>def replace(s):
return s.replace('F', 'F+F-F')
</code></pre>
<p>Then you can generate your sequence as:</p>
<pre><code>code = 'F'
for i in range(8):
code = replace(code)
</code></pre>
<p>I'm not familiar with <code>turtle</code> so I can't hel... | 0 | 2009-04-19T22:56:20Z | [
"python",
"fractals"
] |
Drawing a Dragons curve in Python | 765,048 | <p>I am trying to work out how to draw the dragons curve, with pythons turtle using the An L-System or Lindenmayer system. I no the code is something like </p>
<p>the Dragon curve; initial state = âFâ, replacement rule â replace âFâ with âF+F-Fâ, number of replacements = 8, length = 5, angle = 60</p>
<... | -1 | 2009-04-19T08:06:50Z | 1,059,019 | <p>Draw the dragon curve using <code>turtle</code> module (suggested by <a href="http://stackoverflow.com/questions/765048/drawing-a-dragons-curve-in-python/766310#766310">@John Fouhy</a>):</p>
<pre><code>#!/usr/bin/env python
import turtle
from functools import partial
nreplacements = 8
angle = 60
step = 5
# genera... | 3 | 2009-06-29T15:46:15Z | [
"python",
"fractals"
] |
Standard Django way for letting users edit rich content | 765,066 | <p>I have a Django website in which I want site administrators to be able to edit rich content.
Suppose we're talking about an organizational info page, which might include some pictures, and some links, where the page is not as structured as a news page (which updates with news pieces every few days), but still needs ... | 2 | 2009-04-19T08:24:27Z | 765,070 | <p><strong>Use one of the existing rich-text editors</strong></p>
<p>The lightest weight would be to use something at the js level like DojoEditor: </p>
<p><a href="http://code.djangoproject.com/wiki/AddDojoEditor" rel="nofollow">http://code.djangoproject.com/wiki/AddDojoEditor</a></p>
<p>See also this thread: </p>
... | 4 | 2009-04-19T08:28:12Z | [
"python",
"django",
"django-models",
"django-admin"
] |
Standard Django way for letting users edit rich content | 765,066 | <p>I have a Django website in which I want site administrators to be able to edit rich content.
Suppose we're talking about an organizational info page, which might include some pictures, and some links, where the page is not as structured as a news page (which updates with news pieces every few days), but still needs ... | 2 | 2009-04-19T08:24:27Z | 766,119 | <p>For what you're describing I'd use <a href="http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/" rel="nofollow">flatpages</a>, which is a django app that lets users create and edit pages in the admin panel.</p>
<p>As for formatting, I'd use <a href="http://tinymce.moxiecode.com/" rel="nofollow">TinyMCE</a>.... | 1 | 2009-04-19T21:22:23Z | [
"python",
"django",
"django-models",
"django-admin"
] |
How's Python Multiprocessing Implemented on Windows? | 765,129 | <p>Given the absence of a Windows fork() call, how's the multiprocessing package in Python 2.6 implemented under Windows? On top of Win32 threads or some sort of fake fork or just compatibility on top of the existing multithreading?</p>
| 12 | 2009-04-19T09:32:46Z | 765,207 | <p>It's done using a subprocess call to sys.executable (i.e. start a new Python process) followed by serializing all of the globals, and sending those over the pipe. A poor man's cloning of the current process. This is the cause of the <a href="http://docs.python.org/library/multiprocessing.html#windows">extra restrict... | 29 | 2009-04-19T10:41:44Z | [
"python",
"multithreading",
"fork"
] |
Proxy Check in python | 765,305 | <p>I have written a script in python that uses cookies and POST/GET. I also included proxy support in my script. However, when one enters a dead proxy proxy, the script crashes. Is there any way to check if a proxy is dead/alive before running the rest of my script?</p>
<p>Furthermore, I noticed that some proxies don'... | 8 | 2009-04-19T12:01:51Z | 765,436 | <p>The simplest was is to simply catch the IOError exception from urllib:</p>
<pre><code>try:
urllib.urlopen(
"http://example.com",
proxies={'http':'http://example.com:8080'}
)
except IOError:
print "Connection error! (Check proxy)"
else:
print "All was fine"
</code></pre>
<p>Also, fro... | 11 | 2009-04-19T13:41:40Z | [
"python",
"http",
"proxy"
] |
Proxy Check in python | 765,305 | <p>I have written a script in python that uses cookies and POST/GET. I also included proxy support in my script. However, when one enters a dead proxy proxy, the script crashes. Is there any way to check if a proxy is dead/alive before running the rest of my script?</p>
<p>Furthermore, I noticed that some proxies don'... | 8 | 2009-04-19T12:01:51Z | 7,135,302 | <p>I think that the better approach is like dbr said, handling the exception.</p>
<p>Another solution that could be better in some cases, is to use an external <strong><a href="http://proxyipchecker.com/" rel="nofollow">online proxy checker</a></strong> tool to check if a proxy server is alive and then continue using ... | 1 | 2011-08-20T23:10:29Z | [
"python",
"http",
"proxy"
] |
Proxy Check in python | 765,305 | <p>I have written a script in python that uses cookies and POST/GET. I also included proxy support in my script. However, when one enters a dead proxy proxy, the script crashes. Is there any way to check if a proxy is dead/alive before running the rest of my script?</p>
<p>Furthermore, I noticed that some proxies don'... | 8 | 2009-04-19T12:01:51Z | 16,950,568 | <p>There is one nice package <a href="http://docs.grablib.org/" rel="nofollow">Grab</a>
So, if it ok for you, you can write something like this(simple valid proxy checker-generator):</p>
<pre><code>from grab import Grab, GrabError
def get_valid_proxy(proxy_list): #format of items e.g. '128.2.198.188:3124'
g = Gra... | 0 | 2013-06-05T21:55:17Z | [
"python",
"http",
"proxy"
] |
Using PIL to make all white pixels transparent? | 765,736 | <p>I'm trying to make all white pixels transparent using the Python Image Library. (I'm a C hacker trying to learn python so be gentle)
I've got the conversion working (at least the pixel values look correct) but I can't figure out how to convert the list into a buffer to re-create the image. Here's the code</p>
<pr... | 28 | 2009-04-19T17:13:27Z | 765,774 | <p>You need to make the following changes:</p>
<ul>
<li>append a tuple <code>(255, 255, 255, 0)</code> and not a list <code>[255, 255, 255, 0]</code></li>
<li>use <code>img.putdata(newData)</code></li>
</ul>
<p>This is the working code:</p>
<pre><code>from PIL import Image
img = Image.open('img.png')
img = img.conv... | 34 | 2009-04-19T17:38:26Z | [
"python",
"image",
"python-imaging-library"
] |
Using PIL to make all white pixels transparent? | 765,736 | <p>I'm trying to make all white pixels transparent using the Python Image Library. (I'm a C hacker trying to learn python so be gentle)
I've got the conversion working (at least the pixel values look correct) but I can't figure out how to convert the list into a buffer to re-create the image. Here's the code</p>
<pr... | 28 | 2009-04-19T17:13:27Z | 765,829 | <p>You can also use pixel access mode to modify the image in-place:</p>
<pre><code>from PIL import Image
img = Image.open('img.png')
img = img.convert("RGBA")
pixdata = img.load()
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y] == (255, 255, 255, 255):
pixdata[x,... | 24 | 2009-04-19T18:17:14Z | [
"python",
"image",
"python-imaging-library"
] |
Using PIL to make all white pixels transparent? | 765,736 | <p>I'm trying to make all white pixels transparent using the Python Image Library. (I'm a C hacker trying to learn python so be gentle)
I've got the conversion working (at least the pixel values look correct) but I can't figure out how to convert the list into a buffer to re-create the image. Here's the code</p>
<pr... | 28 | 2009-04-19T17:13:27Z | 4,531,395 | <pre><code>import Image
import ImageMath
def distance2(a, b):
return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + (a[2] - b[2]) * (a[2] - b[2])
def makeColorTransparent(image, color, thresh2=0):
image = image.convert("RGBA")
red, green, blue, alpha = image.split()
image.putalpha(Ima... | 3 | 2010-12-25T19:34:34Z | [
"python",
"image",
"python-imaging-library"
] |
Testing time sensitive applications in Python | 765,773 | <p>I've written an auction system in Django. I want to write unit tests but the application is time sensitive (e.g. the amount advertisers are charged is a function of how long their ad has been active on a website). What's a good approach for testing this type of application?</p>
<p>Here's one possible solution: a ... | 3 | 2009-04-19T17:36:55Z | 766,112 | <p>In the link you provided, the author somewhat rejects the idea of adding additional parameters to your methods for the sake of unit testing, but in some cases I think you can justify this as just an extension of your business logic. In my opinion, it's a form of inversion of control that can make your model more fl... | 3 | 2009-04-19T21:19:09Z | [
"python",
"django",
"unit-testing",
"datetime"
] |
Testing time sensitive applications in Python | 765,773 | <p>I've written an auction system in Django. I want to write unit tests but the application is time sensitive (e.g. the amount advertisers are charged is a function of how long their ad has been active on a website). What's a good approach for testing this type of application?</p>
<p>Here's one possible solution: a ... | 3 | 2009-04-19T17:36:55Z | 942,062 | <p>In general I try to make the production code take date objects as input (where the semantics allows). In many testing situations a DateFactory as you describe is what people do.</p>
<p>In Python you can also get away with changing the static module methods Datetime.now or Time.now directly. You need to be careful... | 1 | 2009-06-02T21:46:51Z | [
"python",
"django",
"unit-testing",
"datetime"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 765,804 | <p>How exact do you need it to be? <code>td.days / 365.25</code> will get you pretty close, if you're worried about leap years.</p>
| 0 | 2009-04-19T18:04:03Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 765,812 | <p>First off, at the most detailed level, the problem can't be solved exactly. Years vary in length, and there isn't a clear "right choice" for year length. </p>
<p>That said, get the difference in whatever units are "natural" (probably seconds) and divide by the ratio between that and years. E.g.</p>
<pre><code>d... | 7 | 2009-04-19T18:08:08Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 765,862 | <p>If you're trying to check if someone is 18 years of age, using <code>timedelta</code> will not work correctly on some edge cases because of leap years. For example, someone born on January 1, 2000, will turn 18 exactly 6575 days later on January 1, 2018 (5 leap years included), but someone born on January 1, 2001, ... | 39 | 2009-04-19T18:36:03Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 765,990 | <p>You need more than a <code>timedelta</code> to tell how many years have passed; you also need to know the beginning (or ending) date. (It's a leap year thing.) </p>
<p>Your best bet is to use the <code>dateutil.relativedelta</code> <a href="http://labix.org/python-dateutil">object</a>, but that's a 3rd party modu... | 85 | 2009-04-19T20:05:15Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 1,078,826 | <p>Even though this thread is already dead, might i suggest a working solution for this very same problem i was facing. Here it is (date is a string in the format dd-mm-yyyy):</p>
<pre><code>def validatedate(date):
parts = date.strip().split('-')
if len(parts) == 3 and False not in [x.isdigit() for x in parts... | 0 | 2009-07-03T10:51:42Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 1,361,482 | <p>this function returns the difference in years between two dates (taken as strings in ISO format, but it can easily modified to take in any format)</p>
<pre><code>import time
def years(earlydateiso, laterdateiso):
"""difference in years between two dates in ISO format"""
ed = time.strptime(earlydateiso, "... | 0 | 2009-09-01T09:29:40Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 1,588,693 | <p>Well, question seems rather easy.
You need to check the number of 'full' years, and only if it's equal to 18 you need to bother with months and days. The edge case is: <code>endDate.year - startDate.year == 18</code> and it splits to two cases: <code>startDate.month != endDate.month</code> and <code>startDate.month... | -2 | 2009-10-19T13:33:56Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 1,817,519 | <pre><code>def age(dob):
import datetime
today = datetime.date.today()
if today.month < dob.month or \
(today.month == dob.month and today.day < dob.day):
return today.year - dob.year - 1
else:
return today.year - dob.year
>>> import datetime
>>> datetime.... | 1 | 2009-11-30T01:51:16Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 1,824,065 | <p>I'll suggest <a href="http://www.ferg.org/pyfdate/index.html" rel="nofollow">Pyfdate</a> </p>
<blockquote>
<p><strong>What is pyfdate?</strong></p>
<p>Given Python's goal to be a powerful and easy-to-use scripting
language, its features for working
with dates and times are not as
user-friendly as they ... | 0 | 2009-12-01T04:48:56Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 1,959,733 | <p>Yet another 3rd party lib not mentioned here is mxDateTime (predecessor of both python <code>datetime</code> and 3rd party <code>timeutil</code>) could be used for this task.</p>
<p>The aforementioned <code>yearsago</code> would be:</p>
<pre><code>from mx.DateTime import now, RelativeDateTime
def years_ago(years,... | 1 | 2009-12-24T20:52:12Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 1,959,782 | <p>Get the number of days, then divide by 365.2425 (the mean Gregorian year) for years. Divide by 30.436875 (the mean Gregorian month) for months.</p>
| 1 | 2009-12-24T21:13:42Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 3,260,826 | <p>In the end what you have is a maths issue. If every 4 years we have an extra day lets then dived the timedelta in days, not by 365 but 365*4 + 1, that would give you the amount of 4 years. Then divide it again by 4.
timedelta / ((365*4) +1) / 4 = timedelta * 4 / (365*4 +1)</p>
| 1 | 2010-07-15T23:20:49Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 5,261,066 | <p>This is the solution I worked out, I hope can help ;-)</p>
<pre><code>def menor_edad_legal(birthday):
""" returns true if aged<18 in days """
try:
today = time.localtime()
fa_divuit_anys=date(year=today.tm_year-18, month=today.tm_mon, day=today.tm_mday)
... | 1 | 2011-03-10T14:29:20Z | [
"python",
"datetime",
"timedelta"
] |
Python timedelta in years | 765,797 | <p>I need to check if some number of years have been since some date. Currently I've got <code>timedelta</code> from <code>datetime</code> module and I don't know how to convert it to years.</p>
| 79 | 2009-04-19T18:00:40Z | 11,900,199 | <p>Here's a updated DOB function, which calculates birthdays the same way humans do:</p>
<pre><code>import datetime
import locale
# Source: https://en.wikipedia.org/wiki/February_29
PRE = [
'US',
'TW',
]
POST = [
'GB',
'HK',
]
def get_country():
code, _ = locale.getlocale()
try:
ret... | 4 | 2012-08-10T10:53:50Z | [
"python",
"datetime",
"timedelta"
] |
Amazon S3 permissions | 765,964 | <p>Trying to understand S3...How do you limit access to a file you upload to S3? For example, from a web application, each user has files they can upload, but how do you limit access so only that user has access to that file? It seems like the query string authentication requires an expiration date and that won't wor... | 10 | 2009-04-19T19:51:07Z | 766,004 | <p>You might find some help in this <a href="http://stackoverflow.com/questions/629102/updating-permissions-on-amazon-s3-files-that-were-uploaded-via-jungledisk">SO question's</a> responses.</p>
| 1 | 2009-04-19T20:12:28Z | [
"python",
"django",
"amazon-web-services",
"amazon-s3"
] |
Amazon S3 permissions | 765,964 | <p>Trying to understand S3...How do you limit access to a file you upload to S3? For example, from a web application, each user has files they can upload, but how do you limit access so only that user has access to that file? It seems like the query string authentication requires an expiration date and that won't wor... | 10 | 2009-04-19T19:51:07Z | 766,030 | <p>You will have to build the whole access logic to S3 in your applications</p>
| 1 | 2009-04-19T20:24:55Z | [
"python",
"django",
"amazon-web-services",
"amazon-s3"
] |
Amazon S3 permissions | 765,964 | <p>Trying to understand S3...How do you limit access to a file you upload to S3? For example, from a web application, each user has files they can upload, but how do you limit access so only that user has access to that file? It seems like the query string authentication requires an expiration date and that won't wor... | 10 | 2009-04-19T19:51:07Z | 768,050 | <p>There are various ways to control access to the S3 objects:</p>
<ol>
<li><p>Use the query string auth - but as you noted this does require an expiration date. You could make it far in the future, which has been good enough for most things I have done.</p></li>
<li><p>Use the S3 ACLS - but this requires the user to... | 14 | 2009-04-20T12:28:03Z | [
"python",
"django",
"amazon-web-services",
"amazon-s3"
] |
Amazon S3 permissions | 765,964 | <p>Trying to understand S3...How do you limit access to a file you upload to S3? For example, from a web application, each user has files they can upload, but how do you limit access so only that user has access to that file? It seems like the query string authentication requires an expiration date and that won't wor... | 10 | 2009-04-19T19:51:07Z | 768,090 | <ol>
<li>Have the user hit your server</li>
<li>Have the server set up a query-string authentication with a short expiration (minutes, hours?)</li>
<li>Have your server redirect to #2</li>
</ol>
| 8 | 2009-04-20T12:43:17Z | [
"python",
"django",
"amazon-web-services",
"amazon-s3"
] |
Amazon S3 permissions | 765,964 | <p>Trying to understand S3...How do you limit access to a file you upload to S3? For example, from a web application, each user has files they can upload, but how do you limit access so only that user has access to that file? It seems like the query string authentication requires an expiration date and that won't wor... | 10 | 2009-04-19T19:51:07Z | 814,738 | <p>I've been dealing with this, too. Don, who wrote the <a href="http://undesigned.org.za/2007/10/22/amazon-s3-php-class" rel="nofollow">S3 PHP</a> class I'm using, pointed out you can use dirs inside buckets. So you can put your file in a dir with a random string and then redirect to that. mybucket.amazon.net/wef49kfe... | 0 | 2009-05-02T12:00:20Z | [
"python",
"django",
"amazon-web-services",
"amazon-s3"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 766,159 | <p>It's called Slice Notation in Python and you can read a bit more of how it works here:</p>
<p><a href="http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation">http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation</a></p>
| 2 | 2009-04-19T21:42:52Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 766,160 | <p>It's the <a href="https://docs.python.org/whatsnew/2.3.html#extended-slices" rel="nofollow">extended slice notation</a>:</p>
<pre><code>sequence[start:end:step]
</code></pre>
<p>In this case, step -1 means backwards, and omitting the start and end means you want the whole string.</p>
| 2 | 2009-04-19T21:43:04Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 766,161 | <p>The "-1" part represents the "step" part of the slicingâin this case, it goes through the string 1 character at a time, but backwards (a negative step means start from the end of the string). If you specify the step to be 2, for instance, you would get every other character of the string, starting with the first o... | 6 | 2009-04-19T21:43:30Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 766,162 | <p>It's using extended slicing - a string is a sequence in Python, and shares some methods with other sequences (namely lists and tuples). There are three parts to slicing - start, stop and step. All of them have default values - start defaults to 0, stop defaults to len(sequence), and step defaults to 1. By specifying... | 2 | 2009-04-19T21:43:36Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 766,163 | <p>Sure, the <code>[::]</code> is the <a href="https://docs.python.org/whatsnew/2.3.html#extended-slices">extended slice</a> operator. It allows you to take substrings. Basically, it works by specifying which elements you want as [begin:end:step], and it works for all sequences. Two neat things about it:</p>
<ul>
<... | 59 | 2009-04-19T21:43:46Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 766,164 | <p>[::-1] gives a slice of the string a. the full syntax is <code>a[begin:end:step]</code>
which gives a[begin], a[begin+step], ... a[end-1]. WHen step is negative, you start at end and move to begin.</p>
<p>Finally, begin defaults to the beginning of the sequence, end to the end, and step to -1.</p>
| 1 | 2009-04-19T21:44:01Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 766,165 | <p>a string is essentially a sequence of characters and so the slicing operation works on it. What you are doing is in fact:</p>
<p>-> get an slice of 'a' from start to end in steps of 1 backward.</p>
| 1 | 2009-04-19T21:44:29Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 766,291 | <p>I think the following makes a bit more sense for print strings in reverse, but maybe that's just me:</p>
<pre><code>for char in reversed( myString ):
print( char, end = "" )
</code></pre>
| 5 | 2009-04-19T22:48:09Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 6,238,928 | <p>I would do it like this:</p>
<pre><code>variable = "string"
message = ""
for b in variable:
message = b+message
print (message)
</code></pre>
<p>and it prints: gnirts</p>
| 0 | 2011-06-04T19:33:28Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 9,827,706 | <p>Consider the list below</p>
<pre><code>l=[12,23,345,456,67,7,945,467]
</code></pre>
<p>Another trick for reversing a list may be :</p>
<pre><code>l[len(l):-len(l)-1:-1] [467, 945, 7, 67, 456, 345, 23, 12]
l[:-len(l)-1:-1] [467, 945, 7, 67, 456, 345, 23, 12]
l[len(l)::-1] [467, 945, 7, 67, 456, 345, 23, 12]
</co... | 0 | 2012-03-22T17:46:18Z | [
"python",
"string",
"slice"
] |
reverse a string in Python | 766,141 | <p>I was looking for a way to print a string backwards, and after a quick search on google, I found this method:</p>
<p>Suppose 'a' is a string variable. This will return the 'a' string backwards:</p>
<pre><code>a[::-1]
</code></pre>
<p>Can anyone explain how that works?</p>
| 25 | 2009-04-19T21:35:54Z | 25,497,656 | <p>we can use append and pop to do it</p>
<pre><code>def rev(s):
i = list(s)
o = list()
while len(i) > 0:
o.append(t.pop())
return ''.join(o)
</code></pre>
| 0 | 2014-08-26T03:39:09Z | [
"python",
"string",
"slice"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.