title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
Get a list/tuple/dict of the arguments passed to a function?
2,521,901
<p>Given the following function:</p> <pre><code>def foo(a, b, c): pass </code></pre> <p>How would one obtain a list/tuple/dict/etc of the arguments passed in, <strong>without having to build the structure myself</strong>?</p> <p>Specifically, I'm looking for Python's version of JavaScript's <code>arguments</code...
30
2010-03-26T08:30:54Z
2,521,948
<p>One solution, using decorators, is <a href="http://stackoverflow.com/questions/1408818/getting-the-the-keyword-arguments-actually-passed-to-a-python-method">here</a>.</p>
1
2010-03-26T08:39:56Z
[ "python", "function", "arguments" ]
Get a list/tuple/dict of the arguments passed to a function?
2,521,901
<p>Given the following function:</p> <pre><code>def foo(a, b, c): pass </code></pre> <p>How would one obtain a list/tuple/dict/etc of the arguments passed in, <strong>without having to build the structure myself</strong>?</p> <p>Specifically, I'm looking for Python's version of JavaScript's <code>arguments</code...
30
2010-03-26T08:30:54Z
2,521,971
<p>I would use <code>*args</code> or <code>**kwargs</code> and throw an exception if the arguments are not as expected</p> <p>If you want to have the same errors than the ones checked by python you can do something like</p> <pre><code>def check_arguments(function_name,args,arg_names): missing_count = len(arg_name...
2
2010-03-26T08:43:24Z
[ "python", "function", "arguments" ]
Get a list/tuple/dict of the arguments passed to a function?
2,521,901
<p>Given the following function:</p> <pre><code>def foo(a, b, c): pass </code></pre> <p>How would one obtain a list/tuple/dict/etc of the arguments passed in, <strong>without having to build the structure myself</strong>?</p> <p>Specifically, I'm looking for Python's version of JavaScript's <code>arguments</code...
30
2010-03-26T08:30:54Z
2,527,340
<p>You can use the inspect module:</p> <pre><code>def foo(x): return x inspect.getargspec(foo) Out[23]: ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None) </code></pre> <p>This is a duplicate of <a href="http://stackoverflow.com/questions/218616/getting-method-parameter-names-in-python">this</a> and...
5
2010-03-26T23:04:03Z
[ "python", "function", "arguments" ]
Get a list/tuple/dict of the arguments passed to a function?
2,521,901
<p>Given the following function:</p> <pre><code>def foo(a, b, c): pass </code></pre> <p>How would one obtain a list/tuple/dict/etc of the arguments passed in, <strong>without having to build the structure myself</strong>?</p> <p>Specifically, I'm looking for Python's version of JavaScript's <code>arguments</code...
30
2010-03-26T08:30:54Z
29,526,383
<p>From the accepted answer from a duplicate (older??) question <a href="http://stackoverflow.com/a/582206/1136458">http://stackoverflow.com/a/582206/1136458</a> :</p> <pre><code> frame = inspect.currentframe() args, _, _, values = inspect.getargvalues(frame) </code></pre>
0
2015-04-08T22:21:25Z
[ "python", "function", "arguments" ]
Create a Python User() class that both creates new users and modifies existing users
2,521,907
<p>I'm trying to figure out the best way to create a class that can modify and create new users all in one. This is what I'm thinking:</p> <pre><code>class User(object): def __init__(self,user_id): if user_id == -1 self.new_user = True else: self.new_user = False #fetch...
1
2010-03-26T08:32:50Z
2,521,983
<p>Small change to your initializer:</p> <pre><code>def __init__(self, user_id=None): if user_id is None: </code></pre>
2
2010-03-26T08:46:49Z
[ "python", "oop" ]
Create a Python User() class that both creates new users and modifies existing users
2,521,907
<p>I'm trying to figure out the best way to create a class that can modify and create new users all in one. This is what I'm thinking:</p> <pre><code>class User(object): def __init__(self,user_id): if user_id == -1 self.new_user = True else: self.new_user = False #fetch...
1
2010-03-26T08:32:50Z
2,521,987
<p>Off the top of my head, I would suggest the following:</p> <p>1: Use a default argument <code>None</code> instead of -1 for <code>user_id</code> in the constructor:</p> <pre><code>def __init__(self, user_id=None): if user_id is None: ... </code></pre> <p>2: Skip the <code>getFullName</code> method - ...
3
2010-03-26T08:47:53Z
[ "python", "oop" ]
Create a Python User() class that both creates new users and modifies existing users
2,521,907
<p>I'm trying to figure out the best way to create a class that can modify and create new users all in one. This is what I'm thinking:</p> <pre><code>class User(object): def __init__(self,user_id): if user_id == -1 self.new_user = True else: self.new_user = False #fetch...
1
2010-03-26T08:32:50Z
2,522,016
<p>What you are trying to achieve is called <a href="http://en.wikipedia.org/wiki/Active_record_pattern" rel="nofollow">Active Record pattern</a>. I suggest learning existing systems providing this sort of things such as <a href="http://elixir.ematia.de/trac/wiki" rel="nofollow">Elixir</a>.</p>
3
2010-03-26T08:55:24Z
[ "python", "oop" ]
Create a Python User() class that both creates new users and modifies existing users
2,521,907
<p>I'm trying to figure out the best way to create a class that can modify and create new users all in one. This is what I'm thinking:</p> <pre><code>class User(object): def __init__(self,user_id): if user_id == -1 self.new_user = True else: self.new_user = False #fetch...
1
2010-03-26T08:32:50Z
2,849,532
<p>You can do this with metaclasses. Consider this : </p> <pre><code>class MetaCity: def __call__(cls,name): “”“ If it’s in the database, retrieve it and return it If it’s not there, create it and return it ““” theCity = database.get(name) # your ...
2
2010-05-17T13:46:29Z
[ "python", "oop" ]
Cost of exception handlers in Python
2,522,005
<p>In <a href="http://stackoverflow.com/questions/2521558/avoid-if-statement-after-condition-has-been-met">another question</a>, the accepted answer suggested replacing a (very cheap) if statement in Python code with a try/except block to improve performance.</p> <p>Coding style issues aside, and assuming that the exc...
33
2010-03-26T08:52:20Z
2,522,013
<p>Why don't you measure it using the <a href="http://docs.python.org/library/timeit.html"><code>timeit</code> module</a>? That way you can see whether it's relevant to your application.</p> <p>OK, so I've just tried the following:</p> <pre><code>import timeit statements=["""\ try: b = 10/a except ZeroDivisionEr...
46
2010-03-26T08:54:50Z
[ "python", "performance", "exception", "micro-optimization" ]
Cost of exception handlers in Python
2,522,005
<p>In <a href="http://stackoverflow.com/questions/2521558/avoid-if-statement-after-condition-has-been-met">another question</a>, the accepted answer suggested replacing a (very cheap) if statement in Python code with a try/except block to improve performance.</p> <p>Coding style issues aside, and assuming that the exc...
33
2010-03-26T08:52:20Z
2,529,713
<p>This question is actually answered in the <a href="http://docs.python.org/2/faq/design.html#how-fast-are-exceptions"> Design and History FAQ</a>:</p> <blockquote> <p>A try/except block is extremely efficient if no exceptions are raised Actually catching an exception is expensive.</p> </blockquote>
18
2010-03-27T14:56:19Z
[ "python", "performance", "exception", "micro-optimization" ]
Cost of exception handlers in Python
2,522,005
<p>In <a href="http://stackoverflow.com/questions/2521558/avoid-if-statement-after-condition-has-been-met">another question</a>, the accepted answer suggested replacing a (very cheap) if statement in Python code with a try/except block to improve performance.</p> <p>Coding style issues aside, and assuming that the exc...
33
2010-03-26T08:52:20Z
3,743,528
<p>This question is misleading. If you assume the exception is <em>never</em> triggered, neither one is optimal code. </p> <p>If you assume the exception is triggered as part of an error condition, you are already outside the realm of wanting optimal code (and you probably aren't handling it at a fine-grained level li...
7
2010-09-18T21:12:39Z
[ "python", "performance", "exception", "micro-optimization" ]
Problem simulating a key press on Python for Symbian platform on a Nokia 5th ed phone
2,522,033
<p>I am developing an app for Nokia 5800 Music Express (S60 5th edition) using PyS60 (Python for S60) ,I want to simulate a KeyPress say if a message comes,I detect a message and Press a Key. </p> <p>There does exist a Keypress module for PyS60 for 2nd edition phones which allows this. However I have not been able to ...
0
2010-03-26T09:01:13Z
2,522,339
<p>There are compatibility breaks between 2nd and 3rd editions so binaries developed on one won't work on the other.</p> <p>There's also a <a href="http://wiki.forum.nokia.com/index.php/How_to_simulate_a_keypress_in_PyS60" rel="nofollow">keypress module for 3rd edition</a>. It should also work on 5th edition but I hav...
0
2010-03-26T10:03:38Z
[ "python", "symbian", "nokia", "pys60" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,522,161
<p>No it's not the fastest, because you know that the characters have a limited range and you could use a list and direct indexing, using the numeric representation of the character, to store the frequencies.</p>
6
2010-03-26T09:33:01Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,522,169
<p>I'm not familiar with python, but for finding frequencies, unless you know the range of frequencies (in which case you can use an array), dictionary is the way to go.<br> If you know your characters in a unicode, ASCII, etc. range, you can define an array with the correct number of values.<br> However, this will cha...
4
2010-03-26T09:33:54Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,522,170
<p>It is very, very hard to beat <code>dict</code>. It is very highly tuned since almost everything in Python is dict-based.</p>
5
2010-03-26T09:34:09Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,522,226
<p>How about avoiding float operations inside the loop and do it after everything is done?</p> <p>By that way, you could just do +1 everytime, and its should be faster.</p> <p>And better use collections.defaultdict as S.Lott advised.</p> <pre><code>freqs=collections.defaultdict(int) for char in text: freqs[ch...
16
2010-03-26T09:42:41Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,522,227
<p>To avoid the try except overhead you can use a defaultdict.</p>
1
2010-03-26T09:42:42Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,522,232
<p>Small speed up will be usage of <a href="http://docs.python.org/library/stdtypes.html#dict.setdefault" rel="nofollow"><code>dict.setdefault</code></a> method, that way you will not pay rather big price for every new encountered character:</p> <pre><code>for char in text: freq[char] = freq.setdefault(char, 0.0) ...
1
2010-03-26T09:43:54Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,522,330
<p>If you are really concerned about speed, you might consider first counting characters with <em>integers</em> and then obtaining frequencies through (float) division.</p> <p>Here are the numbers:</p> <pre><code>python -mtimeit -s'x=0' 'x+=1' 10000000 loops, best of 3: 0.0661 usec per loop python -mtimeit -s'...
2
2010-03-26T10:02:34Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,522,683
<p>well, you can do it in the old fashioned style... as we know that there are not too many different characters and they are contiguous, we can use a plain array (or list here) and use the characters ordinal numbering for indexing:</p> <pre><code>s = 1.0*len(text) counts = [0]*256 # change this if working with unicod...
2
2010-03-26T11:01:38Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,523,336
<p>Using this code on Alice in Wonderland (163793 chars) and "The Bible, Douay-Rheims Version" (5649295 chars) from Project Gutenberg:</p> <pre><code>from collections import defaultdict import timeit def countchars(): f = open('8300-8.txt', 'rb') #f = open('11.txt') s = f.read() f.close() charDict...
2
2010-03-26T12:56:51Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,524,913
<p>If the data is in a single byte encoding you can use numpy to accelerate the count process:</p> <pre><code>import numpy as np def char_freq(data): counts = np.bincount(np.frombuffer(data, dtype=np.byte)) freqs = counts.astype(np.double) / len(data) return dict((chr(idx), freq) for idx, freq in enumerat...
2
2010-03-26T16:30:18Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,525,617
<h2>Performance comparison</h2> <p>Note: time in the table doesn't include the time it takes to load files.</p> <pre><code>| approach | american-english, | big.txt, | time w.r.t. defaultdict | | | time, seconds | time, seconds | | |----------------+---------------...
44
2010-03-26T18:04:25Z
[ "python", "algorithm", "probability", "frequency" ]
Python - Is a dictionary slow to find frequency of each character?
2,522,152
<p>I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:</p> <pre><code>s = len(text) P = 1.0/s freqs = {} for char in text: try: freqs[char]+=P except: freqs[char]=P </code></pre> <p>but I doubt that this diction...
23
2010-03-26T09:31:23Z
2,532,564
<p>I've written Char Counter C Extension to Python, looks like <strong>300x</strong> faster than <code>collections.Counter</code> and <strong>150x</strong> faster than <code>collections.default(int)</code></p> <pre><code>C Char Counter : 0.0469999313354 s 93 chars {u' ': 1036511, u'$': 110, u'(': 1748, u',': 77675, u'...
10
2010-03-28T10:35:48Z
[ "python", "algorithm", "probability", "frequency" ]
How to reduce latency of data sent through a REST api
2,522,204
<p>I have an application which obtains data in JSON format from one of our other servers. The problem I am facing is, there is is significant delay when when requesting for this information. Since a lot of data is passed (approx 1000 records per request where each record is pretty huge) is there a way that compression ...
3
2010-03-26T09:39:19Z
2,522,254
<p><a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/" rel="nofollow">gzip</a> might significantly reduce the size of text data and optimize load speeds. It's also recommended by <a href="http://developer.yahoo.net/blog/archives/2007/07/high_performanc_3.html" rel="nofollow">Y...
2
2010-03-26T09:47:44Z
[ "python", "ruby-on-rails", "networking", "compression", "latency" ]
How to reduce latency of data sent through a REST api
2,522,204
<p>I have an application which obtains data in JSON format from one of our other servers. The problem I am facing is, there is is significant delay when when requesting for this information. Since a lot of data is passed (approx 1000 records per request where each record is pretty huge) is there a way that compression ...
3
2010-03-26T09:39:19Z
2,522,464
<p>I would first look at how much of this 8s delay is related to:</p> <ol> <li><p>Server side processing (how much took for the data to be generated) There are a lot of techniques to improve this time, including:</p> <ul> <li><p>DB indexes</p></li> <li><p>caching</p></li> <li><p>a faster to_json library</p></li> </ul...
2
2010-03-26T10:25:20Z
[ "python", "ruby-on-rails", "networking", "compression", "latency" ]
Installing Sphinx on App Engine - possible?
2,522,255
<p>Following up on my <a href="http://stackoverflow.com/questions/1775503/minimalistic-tools-for-developer-documentation">last year's question</a> on documentation, I now want to get started and try out Python-based <a href="http://sphinx.pocoo.org/" rel="nofollow">Sphinx</a> for putting together the developer document...
3
2010-03-26T09:48:11Z
2,522,295
<p>The difference between deployment locally via <code>dev_appserver</code> and remotely via <code>appcfg</code> has been - in my experience - limited to which of the two commands I execute. That said, I've no experience with Sphinx.</p>
1
2010-03-26T09:56:28Z
[ "python", "google-app-engine", "python-sphinx" ]
Installing Sphinx on App Engine - possible?
2,522,255
<p>Following up on my <a href="http://stackoverflow.com/questions/1775503/minimalistic-tools-for-developer-documentation">last year's question</a> on documentation, I now want to get started and try out Python-based <a href="http://sphinx.pocoo.org/" rel="nofollow">Sphinx</a> for putting together the developer document...
3
2010-03-26T09:48:11Z
2,522,318
<p>You do not need to install Sphinx on GAE at all.</p> <p>You use Sphinx to create a directory of static HTML, CSS and JS. When this step is finished, you simply upload the output from Sphinx -- in it's entirety.</p> <p>The output from Sphinx (HTML, CSS and JS) is simply served <strong>from one place</strong>. You...
6
2010-03-26T10:00:19Z
[ "python", "google-app-engine", "python-sphinx" ]
Installing Sphinx on App Engine - possible?
2,522,255
<p>Following up on my <a href="http://stackoverflow.com/questions/1775503/minimalistic-tools-for-developer-documentation">last year's question</a> on documentation, I now want to get started and try out Python-based <a href="http://sphinx.pocoo.org/" rel="nofollow">Sphinx</a> for putting together the developer document...
3
2010-03-26T09:48:11Z
2,522,567
<p>Interesting project! The main issue you're going to run into is that of filesystem access: The filesystem on App Engine is read-only, and based on looking at the source, Sphinx is fairly hard-coded to use the filesystem for output. It also expects to read the configuration file and input files from the local filesys...
3
2010-03-26T10:42:44Z
[ "python", "google-app-engine", "python-sphinx" ]
Installing Sphinx on App Engine - possible?
2,522,255
<p>Following up on my <a href="http://stackoverflow.com/questions/1775503/minimalistic-tools-for-developer-documentation">last year's question</a> on documentation, I now want to get started and try out Python-based <a href="http://sphinx.pocoo.org/" rel="nofollow">Sphinx</a> for putting together the developer document...
3
2010-03-26T09:48:11Z
2,530,598
<p>Sphinx compiles documentation, running it on a webserver makes as much sense as running gcc on a webserver.</p>
1
2010-03-27T19:19:30Z
[ "python", "google-app-engine", "python-sphinx" ]
Multi choice form field in Django
2,522,332
<p>I'am developing application on app-engine-path. I would like to make form with multichoice (acceptably languages for user). Code look like this:</p> <p>Language settings:</p> <pre><code>settings.LANGUAGES = ((u"cs", u"Čeština"), (u"en", u"English")) </code></pre> <p>Form model: </p> <pre><code>class UserFor...
0
2010-03-26T10:02:39Z
3,403,022
<p>I solved it this way:</p> <pre><code>some_view(request): ... form = UserForm(instance=user, initial={"languages":user.languages}) ... </code></pre>
0
2010-08-04T06:09:39Z
[ "python", "django-forms", "app-engine-patch" ]
Japanese in python function
2,522,408
<p>I wrote a function in Python which is used to tell me whether the two words are similar or not.</p> <p>Now I want to pass Japanese text in my same function. It is giving error "not a ascii character." I tried using utf-8 encoding, but then it giving the same error</p> <pre><code>Non-ASCII character '\xe3' in file ...
0
2010-03-26T10:15:40Z
2,522,417
<p>Don't use UTF-8. Use <a href="http://farmdev.com/talks/unicode/" rel="nofollow"><code>unicode</code>s</a>.</p>
0
2010-03-26T10:17:32Z
[ "python", "internationalization" ]
Japanese in python function
2,522,408
<p>I wrote a function in Python which is used to tell me whether the two words are similar or not.</p> <p>Now I want to pass Japanese text in my same function. It is giving error "not a ascii character." I tried using utf-8 encoding, but then it giving the same error</p> <pre><code>Non-ASCII character '\xe3' in file ...
0
2010-03-26T10:15:40Z
2,522,448
<p>Put:</p> <pre><code># -*- coding: utf-8 -*- </code></pre> <p>In one of the first two lines of your script. This way the interpreter will know what encoding is used for the code and strings in it.</p> <p>And use Unicode strings wherever possible. If you have luck the function may work well with the Unicode (e.g. <...
0
2010-03-26T10:23:19Z
[ "python", "internationalization" ]
Japanese in python function
2,522,408
<p>I wrote a function in Python which is used to tell me whether the two words are similar or not.</p> <p>Now I want to pass Japanese text in my same function. It is giving error "not a ascii character." I tried using utf-8 encoding, but then it giving the same error</p> <pre><code>Non-ASCII character '\xe3' in file ...
0
2010-03-26T10:15:40Z
2,522,501
<p>Please do not do this:</p> <pre><code>adtext = str.lower(adText) keyword = str.lower(keyword) </code></pre> <p>Please do this:</p> <pre><code>adtext= adText.lower() keyword = keyword.lower() </code></pre> <p>Please do not do this:</p> <pre><code>cmp(str.lower(adText), str.lower(keyword)) </code></pre> <p>Pleas...
1
2010-03-26T10:30:34Z
[ "python", "internationalization" ]
Japanese in python function
2,522,408
<p>I wrote a function in Python which is used to tell me whether the two words are similar or not.</p> <p>Now I want to pass Japanese text in my same function. It is giving error "not a ascii character." I tried using utf-8 encoding, but then it giving the same error</p> <pre><code>Non-ASCII character '\xe3' in file ...
0
2010-03-26T10:15:40Z
2,522,547
<p>This worked for me:</p> <pre><code># -*- coding: utf-8 -*- def filterKeyword(keyword, adText, filterType): # same as yours filterKeyword(u'ポケモン', u'黄色のポケモン', 'contains') </code></pre>
3
2010-03-26T10:38:38Z
[ "python", "internationalization" ]
Japanese in python function
2,522,408
<p>I wrote a function in Python which is used to tell me whether the two words are similar or not.</p> <p>Now I want to pass Japanese text in my same function. It is giving error "not a ascii character." I tried using utf-8 encoding, but then it giving the same error</p> <pre><code>Non-ASCII character '\xe3' in file ...
0
2010-03-26T10:15:40Z
2,524,733
<p>I would just like to note well:</p> <p><code>unicode('ポケモン')</code> (a non-unicode string constant passed to the unicode() constructor)</p> <p>IS NOT THE SAME AS</p> <p><code>u'ポケモン'</code> (a unicode string constant)</p>
0
2010-03-26T16:07:02Z
[ "python", "internationalization" ]
does cProfile profile calls inside threads?
2,522,452
<p>I ran cprofile on a bit of code, which among other things spawns several threads that do most of the work. When I looked at the output of the profiling, I see no logging of all the functions that were called inside the threads. I am sure they were called, as they do stuff that is easy to see such as writing to a DB ...
4
2010-03-26T10:23:33Z
2,522,527
<p>Found the answer here: <a href="http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script/1922945#1922945">link</a></p> <blockquote> <p>It's worth pointing out that using the profiler only works (by default) on the main thread, and you won't get any information from other threads if you use th...
5
2010-03-26T10:35:02Z
[ "python", "multithreading", "cprofile" ]
Advanced Python list comprehension
2,522,503
<p>Given two lists:</p> <pre><code>chars = ['ab', 'bc', 'ca'] words = ['abc', 'bca', 'dac', 'dbc', 'cba'] </code></pre> <p>how can you use list comprehensions to generate a filtered list of <code>words</code> by the following condition: given that each word is of length <code>n</code> and <code>chars</code> is of len...
9
2010-03-26T10:30:45Z
2,522,531
<pre><code>[w for w in words if all([w[i] in chars[i] for i in range(len(w))])] </code></pre>
12
2010-03-26T10:35:43Z
[ "python", "list-comprehension" ]
Advanced Python list comprehension
2,522,503
<p>Given two lists:</p> <pre><code>chars = ['ab', 'bc', 'ca'] words = ['abc', 'bca', 'dac', 'dbc', 'cba'] </code></pre> <p>how can you use list comprehensions to generate a filtered list of <code>words</code> by the following condition: given that each word is of length <code>n</code> and <code>chars</code> is of len...
9
2010-03-26T10:30:45Z
2,522,553
<pre><code>&gt;&gt;&gt; [word for word in words if all(l in chars[i] for i, l in enumerate(word))] ['abc', 'bca'] </code></pre>
19
2010-03-26T10:40:26Z
[ "python", "list-comprehension" ]
Advanced Python list comprehension
2,522,503
<p>Given two lists:</p> <pre><code>chars = ['ab', 'bc', 'ca'] words = ['abc', 'bca', 'dac', 'dbc', 'cba'] </code></pre> <p>how can you use list comprehensions to generate a filtered list of <code>words</code> by the following condition: given that each word is of length <code>n</code> and <code>chars</code> is of len...
9
2010-03-26T10:30:45Z
2,522,577
<p>Using zip:</p> <pre><code>[w for w in words if all([a in c for a, c in zip(w, chars)])] </code></pre> <p>or using enumerate:</p> <pre><code>[w for w in words if not [w for i, c in enumerate(chars) if w[i] not in c]] </code></pre>
3
2010-03-26T10:44:09Z
[ "python", "list-comprehension" ]
Advanced Python list comprehension
2,522,503
<p>Given two lists:</p> <pre><code>chars = ['ab', 'bc', 'ca'] words = ['abc', 'bca', 'dac', 'dbc', 'cba'] </code></pre> <p>how can you use list comprehensions to generate a filtered list of <code>words</code> by the following condition: given that each word is of length <code>n</code> and <code>chars</code> is of len...
9
2010-03-26T10:30:45Z
6,188,016
<p>Why so complex? This works as well:</p> <pre><code>[words[x] for x in range(len(chars)) if chars[x] in words[x]] </code></pre>
-1
2011-05-31T13:05:22Z
[ "python", "list-comprehension" ]
Advanced Python list comprehension
2,522,503
<p>Given two lists:</p> <pre><code>chars = ['ab', 'bc', 'ca'] words = ['abc', 'bca', 'dac', 'dbc', 'cba'] </code></pre> <p>how can you use list comprehensions to generate a filtered list of <code>words</code> by the following condition: given that each word is of length <code>n</code> and <code>chars</code> is of len...
9
2010-03-26T10:30:45Z
7,678,198
<p>This works, using <code>index</code>:</p> <pre><code>[words[chars.index(char)] for char in chars if char in words[chars.index(char)]] </code></pre> <p>Am I missing something?</p>
0
2011-10-06T17:30:18Z
[ "python", "list-comprehension" ]
Advanced Python list comprehension
2,522,503
<p>Given two lists:</p> <pre><code>chars = ['ab', 'bc', 'ca'] words = ['abc', 'bca', 'dac', 'dbc', 'cba'] </code></pre> <p>how can you use list comprehensions to generate a filtered list of <code>words</code> by the following condition: given that each word is of length <code>n</code> and <code>chars</code> is of len...
9
2010-03-26T10:30:45Z
10,273,019
<p>A more simple approach:</p> <pre><code>yourlist = [ w for w in words for ch in chars if w.startswith(ch) ] </code></pre>
0
2012-04-22T23:17:04Z
[ "python", "list-comprehension" ]
Suppose I have a python dictionary , many nests
2,522,651
<p>If a lot of nested dictionarys, I am trying to find a certain key.</p> <p>this key is called "fruit". But it's nested inside somewhere. How do I find the value of this key?</p>
2
2010-03-26T10:57:21Z
2,522,690
<p>(Making some wild guesses about your data structure...)</p> <p>Do it recursively:</p> <pre><code>def findkey(d, key): if key in d: return d[key] for k,subdict in d.iteritems(): val = findkey(subdict, key) if val: return val </code></pre>
2
2010-03-26T11:02:37Z
[ "python", "dictionary" ]
Suppose I have a python dictionary , many nests
2,522,651
<p>If a lot of nested dictionarys, I am trying to find a certain key.</p> <p>this key is called "fruit". But it's nested inside somewhere. How do I find the value of this key?</p>
2
2010-03-26T10:57:21Z
2,522,706
<p>Just traverse the dictionary and check for the keys (note the comment in the bottom about the "not found" value).</p> <pre><code>def find_key_recursive(d, key): if key in d: return d[key] for k, v in d.iteritems(): if type(v) is dict: # Only recurse if we hit a dict value value = find_key_recursiv...
0
2010-03-26T11:04:58Z
[ "python", "dictionary" ]
Suppose I have a python dictionary , many nests
2,522,651
<p>If a lot of nested dictionarys, I am trying to find a certain key.</p> <p>this key is called "fruit". But it's nested inside somewhere. How do I find the value of this key?</p>
2
2010-03-26T10:57:21Z
2,524,202
<p>@HÃ¥vard's recursive solution is probably going to be OK... unless the level of nesting is too high, and then you get a <code>RuntimeError: maximum recursion depth exceeded</code>. To remedy that, you can use the usual technique for recursion removal: keep your own stack of items to examine (as a list that's under ...
5
2010-03-26T15:02:38Z
[ "python", "dictionary" ]
How can I add a code that is always executed by Waf before exit?
2,522,773
<p>I want to make <a href="http://code.google.com/p/waf/" rel="nofollow">Waf</a> generate a beep when it finishes the execution of any command that took more than 10 seconds. </p> <p>I don't know how do add this and assure that the code executes when Waf exits.</p> <p>This should run for any Waf command not only buil...
2
2010-03-26T11:16:18Z
2,524,272
<p>In your <code>wscript</code> module, you can use the Python standard library's <a href="http://docs.python.org/library/atexit.html?highlight=atexit#module-atexit" rel="nofollow">atexit</a> to register callables that you want to be called when the process exit. For example:</p> <pre><code>import atexit import time ...
3
2010-03-26T15:10:39Z
[ "python", "waf" ]
Is it possible to detect the browser version from Django server side?
2,522,774
<p>in server side, not browser. </p>
1
2010-03-26T11:16:22Z
2,522,807
<p><code>request.env['HTTP_USER_AGENT']</code> will give the user agent string the browser sent.</p>
2
2010-03-26T11:20:41Z
[ "python", "django" ]
Is it possible to detect the browser version from Django server side?
2,522,774
<p>in server side, not browser. </p>
1
2010-03-26T11:16:22Z
2,522,809
<p>You can use the HTTP_USER_AGENT in <a href="http://docs.djangoproject.com/en/dev/ref/request-response/" rel="nofollow">HttpRequest</a></p>
4
2010-03-26T11:21:25Z
[ "python", "django" ]
Is it possible to detect the browser version from Django server side?
2,522,774
<p>in server side, not browser. </p>
1
2010-03-26T11:16:22Z
2,588,289
<p>Unfortunately <code>request.env</code> won't work. </p> <p>However, you can get it through <code>request.META.get("HTTP_USER_AGENT")</code></p>
1
2010-04-06T21:00:20Z
[ "python", "django" ]
Is it possible to autoload a file based on the namespace in PHP?
2,522,801
<p>Would what mentioned in the title be possible? Python module style that is. See this example for what I exactly mean.</p> <h3>index.php</h3> <pre><code>&lt;?php use Hello\World; World::greet(); </code></pre> <h3>Hello/World.php</h3> <pre><code>&lt;?php namespace Hello\World; function greet() { echo 'Hello, World...
6
2010-03-26T11:20:02Z
2,522,878
<p>Yes, have a look at the example of <a href="http://de3.php.net/manual/en/function.spl-autoload-register.php">spl_autoload_register</a></p> <pre><code>namespace Foobar; class Foo { static public function test($name) { print '[['. $name .']]'; } } spl_autoload_register(__NAMESPACE__ .'\Foo::test'); ...
5
2010-03-26T11:32:55Z
[ "php", "python", "module", "namespaces", "autoload" ]
Assignment to None
2,522,854
<p>I have a function which returns 3 numbers, e.g.:</p> <pre><code>def numbers(): return 1,2,3 </code></pre> <p>usually I call this function to receive all three returned numbers e.g.:</p> <pre><code>a, b, c = numbers() </code></pre> <p>However, I have one case in which I only need the first returned number. I t...
15
2010-03-26T11:29:31Z
2,522,856
<pre><code>a, _, _ = numbers() </code></pre> <p>is a pythonic way to do this. In Python 3, you could also use: </p> <pre><code>a, *_ = numbers() </code></pre> <p>To clarify <code>_</code> is a normal variable name in Python, except it is conventionally used to refer to non-important variables.</p>
21
2010-03-26T11:30:07Z
[ "python" ]
Assignment to None
2,522,854
<p>I have a function which returns 3 numbers, e.g.:</p> <pre><code>def numbers(): return 1,2,3 </code></pre> <p>usually I call this function to receive all three returned numbers e.g.:</p> <pre><code>a, b, c = numbers() </code></pre> <p>However, I have one case in which I only need the first returned number. I t...
15
2010-03-26T11:29:31Z
2,522,898
<p>Another way is of course <code>a=numbers()[0]</code>, if you do not want to declare another variable. Having said this though, I generally use _ myself.</p>
9
2010-03-26T11:35:47Z
[ "python" ]
Assignment to None
2,522,854
<p>I have a function which returns 3 numbers, e.g.:</p> <pre><code>def numbers(): return 1,2,3 </code></pre> <p>usually I call this function to receive all three returned numbers e.g.:</p> <pre><code>a, b, c = numbers() </code></pre> <p>However, I have one case in which I only need the first returned number. I t...
15
2010-03-26T11:29:31Z
25,972,470
<p>The way I do it:</p> <pre><code>a = numbers()[0] </code></pre> <p>Seeing as the type returned is a tuple, you can access it via index.</p> <p>This solution keeps you from having to declare unused/meaningless variables such as "_". Most people don't mind unused variables, however my pylint is setup to warn me of s...
0
2014-09-22T10:58:50Z
[ "python" ]
Iterating over consecutive sublists in Python
2,523,236
<p>Does Python offer a way to iterate over all "consecutive sublists" of a given list L - i.e. sublists of L where any two consecutive elements are also consecutive in L - or should I write my own?</p> <p>(Example: if L = [1, 2, 3], then the set over which I want to iterate is {[1], [2], [3], [1, 2], [2,3], [1, 2, 3]}...
0
2010-03-26T12:39:07Z
2,523,255
<p>I don't think there's a built-in for exactly that; but it probably wouldn't be too difficult to code up by hand - you're basically just looping through all of the possible lengths from 1 to L.length, and then taking all substrings of each length.</p> <p>You could probably use <a href="http://docs.python.org/library...
2
2010-03-26T12:43:27Z
[ "python", "functionality" ]
Iterating over consecutive sublists in Python
2,523,236
<p>Does Python offer a way to iterate over all "consecutive sublists" of a given list L - i.e. sublists of L where any two consecutive elements are also consecutive in L - or should I write my own?</p> <p>(Example: if L = [1, 2, 3], then the set over which I want to iterate is {[1], [2], [3], [1, 2], [2,3], [1, 2, 3]}...
0
2010-03-26T12:39:07Z
2,523,338
<p>This should work:</p> <pre><code>def sublists(lst): for sublen in xrange(1,len(lst)+1): for idx in xrange(0,len(lst)-sublen+1): yield lst[idx:idx+sublen] </code></pre>
1
2010-03-26T12:57:02Z
[ "python", "functionality" ]
Iterating over consecutive sublists in Python
2,523,236
<p>Does Python offer a way to iterate over all "consecutive sublists" of a given list L - i.e. sublists of L where any two consecutive elements are also consecutive in L - or should I write my own?</p> <p>(Example: if L = [1, 2, 3], then the set over which I want to iterate is {[1], [2], [3], [1, 2], [2,3], [1, 2, 3]}...
0
2010-03-26T12:39:07Z
2,523,345
<p>Try something like this:</p> <pre><code>def iter_sublists(l): n = len(l)+1 for i in xrange(n): for j in xrange(i+1, n): yield l[i:j] &gt;&gt;&gt; print list(iter_sublists([1,2,3])) [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]] </code></pre>
1
2010-03-26T12:58:23Z
[ "python", "functionality" ]
compare two files in python
2,523,329
<p>in a.txt i have the text(line one after the other)</p> <pre><code>login;user;name login;user;name1 login;user </code></pre> <p>in b.txt i have the text</p> <pre><code>login;user login;user login;user;name2 </code></pre> <p>after comparing it should display in a text file as </p> <pre><code>login;user;name login...
0
2010-03-26T12:55:56Z
2,523,366
<p>Perhaps the standard-lib <code>difflib</code> module can be of help - check out its documentation. Your question is not clear enough for a more complete answer.</p>
1
2010-03-26T13:02:30Z
[ "python", "string" ]
compare two files in python
2,523,329
<p>in a.txt i have the text(line one after the other)</p> <pre><code>login;user;name login;user;name1 login;user </code></pre> <p>in b.txt i have the text</p> <pre><code>login;user login;user login;user;name2 </code></pre> <p>after comparing it should display in a text file as </p> <pre><code>login;user;name login...
0
2010-03-26T12:55:56Z
2,523,380
<pre><code>for a, b in zip(open('a'), open('b')): print(a if len(a.split(';')) == 3 else b) </code></pre>
4
2010-03-26T13:05:27Z
[ "python", "string" ]
compare two files in python
2,523,329
<p>in a.txt i have the text(line one after the other)</p> <pre><code>login;user;name login;user;name1 login;user </code></pre> <p>in b.txt i have the text</p> <pre><code>login;user login;user login;user;name2 </code></pre> <p>after comparing it should display in a text file as </p> <pre><code>login;user;name login...
0
2010-03-26T12:55:56Z
2,527,527
<p>Based on the vague information given, I would try something like the following:</p> <pre><code>import itertools def merger(fni1, fni2): "merge two files ignoring 'login;user\n' lines" fp1= open(fni1, "r") fp2= open(fni2, "r") try: for line in itertools.chain(fp1, fp2): if line !...
0
2010-03-26T23:50:01Z
[ "python", "string" ]
Easiest ways to generate graphs from Python?
2,523,689
<p>I'm using Python to process CSV files filled with data that I want to run calculations on, and then graph. I'm looking for a library to use that I can send processed CSV information to, or a dict of some sort, and then choose different graphing styles with. </p> <p>Does anyone have any recommendations?</p>
6
2010-03-26T13:49:14Z
2,523,705
<p>I'm personally using <a href="http://matplotlib.sourceforge.net/" rel="nofollow">matplotlib</a> and am very happy with it.</p>
7
2010-03-26T13:51:12Z
[ "python", "graph", "visualization", "google-visualization" ]
Easiest ways to generate graphs from Python?
2,523,689
<p>I'm using Python to process CSV files filled with data that I want to run calculations on, and then graph. I'm looking for a library to use that I can send processed CSV information to, or a dict of some sort, and then choose different graphing styles with. </p> <p>Does anyone have any recommendations?</p>
6
2010-03-26T13:49:14Z
2,523,722
<p><a href="http://matplotlib.sourceforge.net/" rel="nofollow">Matplotlib</a> and <a href="http://gnuplot-py.sourceforge.net/" rel="nofollow">Gnuplot.py</a> are popular choices. I've used both.</p>
6
2010-03-26T13:52:43Z
[ "python", "graph", "visualization", "google-visualization" ]
Easiest ways to generate graphs from Python?
2,523,689
<p>I'm using Python to process CSV files filled with data that I want to run calculations on, and then graph. I'm looking for a library to use that I can send processed CSV information to, or a dict of some sort, and then choose different graphing styles with. </p> <p>Does anyone have any recommendations?</p>
6
2010-03-26T13:49:14Z
2,523,738
<p>I've been using <a href="http://matplotlib.sourceforge.net/" rel="nofollow" title="matplotlib">matplotlib</a> for about 3 years now to plot experimental data. Before I was using Excel and that was just a pain. I've been happy with matplotlib ever since. It's great and very powerful.</p>
3
2010-03-26T13:55:06Z
[ "python", "graph", "visualization", "google-visualization" ]
Easiest ways to generate graphs from Python?
2,523,689
<p>I'm using Python to process CSV files filled with data that I want to run calculations on, and then graph. I'm looking for a library to use that I can send processed CSV information to, or a dict of some sort, and then choose different graphing styles with. </p> <p>Does anyone have any recommendations?</p>
6
2010-03-26T13:49:14Z
2,523,777
<p>For client-side charts <a href="http://teethgrinder.co.uk/open-flash-chart-2/" rel="nofollow">Open Flash Chart</a> or <a href="http://code.google.com/apis/charttools/" rel="nofollow">Google Charts Tools</a>.</p>
2
2010-03-26T14:01:09Z
[ "python", "graph", "visualization", "google-visualization" ]
Easiest ways to generate graphs from Python?
2,523,689
<p>I'm using Python to process CSV files filled with data that I want to run calculations on, and then graph. I'm looking for a library to use that I can send processed CSV information to, or a dict of some sort, and then choose different graphing styles with. </p> <p>Does anyone have any recommendations?</p>
6
2010-03-26T13:49:14Z
2,523,794
<p>There is <a href="http://home.gna.org/pychart/" rel="nofollow">pychart</a> or <a href="http://bitbucket.org/lgs/pycha/wiki/Home" rel="nofollow">PyCha</a>, both of which I have not used but have been considering myself. </p>
1
2010-03-26T14:04:04Z
[ "python", "graph", "visualization", "google-visualization" ]
Easiest ways to generate graphs from Python?
2,523,689
<p>I'm using Python to process CSV files filled with data that I want to run calculations on, and then graph. I'm looking for a library to use that I can send processed CSV information to, or a dict of some sort, and then choose different graphing styles with. </p> <p>Does anyone have any recommendations?</p>
6
2010-03-26T13:49:14Z
2,523,803
<p>It depends on the kind of graph you want, most of the time, i'm using matplotlib but sometimes<a href="http://dkbza.org/pydot/pydot.html" rel="nofollow">pydot</a> is good. The various layout algorithms are good for me when dealing with huge graphs</p>
0
2010-03-26T14:05:07Z
[ "python", "graph", "visualization", "google-visualization" ]
LXML E builder for java?
2,523,715
<p>There is one thing I really love about LXML, and that the E builder. I love that I can throw XML together like this:</p> <pre><code>message = E.Person( E.Name( E.First("jack") E.Last("Ripper") ) E.PhoneNumber("555-555-5555") ) </code></pre> <p>To make:</p> <pre><code>&lt;Person&gt; &lt;Name&gt; ...
1
2010-03-26T13:52:04Z
2,523,884
<p>will be hard with pure Java, but if you can use Groovy in your projects then you could use the <a href="http://groovy.codehaus.org/Creating+XML+using+Groovy%27s+MarkupBuilder" rel="nofollow">MarkupBuilder</a> which comes very close to what you're asking for</p> <pre><code>def xml = new MarkupBuilder(writer) xml.rec...
2
2010-03-26T14:16:42Z
[ "java", "python", "lxml" ]
Access is denied error with pregenerated .pyc or .pyo files
2,523,961
<p>I am getting an <em>Access is denied error</em> while I am trying to run the .pyo file by double click or from the command prompt.</p> <p>Lets say I have abc.py (keeping main method entry point) which imports files xyz.py and imports wx etc.</p> <p>I generate the .pyo file. But once I try to run abc.pyo I get the ...
1
2010-03-26T14:27:45Z
2,525,133
<p>You don't "run" a .pyo file, as it's not an executable. You can give it to the python interpreter in lieu of the .py file, but in general, you should use a .py file as your entry point, so that the .pyc or .pyo file can be recreated when necessary.</p> <pre><code>$ python imported.pyo Success! $ ./imported.pyo bash...
1
2010-03-26T16:59:46Z
[ "python", "wxpython" ]
Access is denied error with pregenerated .pyc or .pyo files
2,523,961
<p>I am getting an <em>Access is denied error</em> while I am trying to run the .pyo file by double click or from the command prompt.</p> <p>Lets say I have abc.py (keeping main method entry point) which imports files xyz.py and imports wx etc.</p> <p>I generate the .pyo file. But once I try to run abc.pyo I get the ...
1
2010-03-26T14:27:45Z
2,525,684
<p>You can tell the system that your <code>hw.pyo</code> file is "executable", for example (in Linux, MacOSX, or any other Unix-y system) by executing the command <code>chmod +w hw.pyo</code> at the terminal shell prompt. Consider, for example, the following short and simple shell session:</p> <pre><code>$ cat &gt;hw...
2
2010-03-26T18:15:02Z
[ "python", "wxpython" ]
Backup Google Calendar programmatically: https://www.google.com/calendar/exporticalzip
2,524,011
<p>I'm struggling with writing a python script that automatically grabs the zip fail containing all my google calendars and stores it (as a backup) on my harddisk.</p> <p>I'm using ClientLogin to get an authentication token (and successfully can obtain the token).</p> <p>Unfortunately, i'm unable to retrieve the file...
4
2010-03-26T14:36:30Z
2,524,036
<p>Calendars have an URL which allows you to download it without authentication (see Google Calendar's UI, Calendar settings, under Private URL). You should be able to download the iCal or XML file using <code>urllib</code> without problems.</p>
0
2010-03-26T14:39:20Z
[ "python", "authentication", "calendar" ]
Backup Google Calendar programmatically: https://www.google.com/calendar/exporticalzip
2,524,011
<p>I'm struggling with writing a python script that automatically grabs the zip fail containing all my google calendars and stores it (as a backup) on my harddisk.</p> <p>I'm using ClientLogin to get an authentication token (and successfully can obtain the token).</p> <p>Unfortunately, i'm unable to retrieve the file...
4
2010-03-26T14:36:30Z
2,524,878
<p>You could write a script with <a href="http://wwwsearch.sourceforge.net/mechanize/">mechanize</a> to walk through google login process before downloading Calendar from your preferred url.</p> <p>So try with:</p> <pre><code>import mechanize br=mechanize.Browser() br.open('https://www.google.com/calendar/exporticalz...
5
2010-03-26T16:25:28Z
[ "python", "authentication", "calendar" ]
Backup Google Calendar programmatically: https://www.google.com/calendar/exporticalzip
2,524,011
<p>I'm struggling with writing a python script that automatically grabs the zip fail containing all my google calendars and stores it (as a backup) on my harddisk.</p> <p>I'm using ClientLogin to get an authentication token (and successfully can obtain the token).</p> <p>Unfortunately, i'm unable to retrieve the file...
4
2010-03-26T14:36:30Z
3,394,487
<p>Great solution systempuntoout! For those of you using Google Apps, just needs a tweak to the URLs and the Email.</p> <pre><code>import mechanize br=mechanize.Browser() br.open('https://www.google.com/calendar/hosted/yourdomain.com/exporticalzip') br.select_form(nr=0) br['Email']='Username' br['Passwd']='Password' b...
0
2010-08-03T07:41:32Z
[ "python", "authentication", "calendar" ]
How to connect a variable to Entry widget?
2,524,031
<p>I'm trying to associate a variable with a Tkinter entry widget, in a way that:</p> <ol> <li><p>Whenever I change the value (the "content") of the entry, mainly by typing something into it, the variable automatically gets assigned the value of what I've typed. Without me having to push a button "Update value " or so...
8
2010-03-26T14:38:26Z
2,524,242
<p>I think you want something like this. In the example below, I created a variable <code>myvar</code> and assigned it to be <code>textvariable</code> of both a <code>Label</code> and <code>Entry</code> widgets. This way both are coupled and changes in the Entry widget will reflect automatically in Label.</p> <p>You c...
12
2010-03-26T15:07:24Z
[ "python", "variables", "tkinter", "validation", "entry" ]
WebService client libraries for Python and Perl
2,524,195
<p>I want to access web service in Python or/and Perl scripts. What are the most popular and reliable libraries today?</p> <p>I read <a href="http://stackoverflow.com/questions/159802/python-web-services">this</a> question, and I know about SOAPpy and ZSI. Can anybody say something about this libraries? Are they relia...
2
2010-03-26T15:01:44Z
2,524,549
<p>If you're talking about SOAP then for Python I would definitely recommend <code>suds</code> : <a href="https://fedorahosted.org/suds/" rel="nofollow">https://fedorahosted.org/suds/</a></p>
2
2010-03-26T15:46:01Z
[ "python", "perl", "web-services" ]
WebService client libraries for Python and Perl
2,524,195
<p>I want to access web service in Python or/and Perl scripts. What are the most popular and reliable libraries today?</p> <p>I read <a href="http://stackoverflow.com/questions/159802/python-web-services">this</a> question, and I know about SOAPpy and ZSI. Can anybody say something about this libraries? Are they relia...
2
2010-03-26T15:01:44Z
2,525,107
<p>Checking couple of libraries for SOAP (including suds) only ZSI worked reliably for me. It is not complicated to use and it just works(tm). I recommend that.</p>
0
2010-03-26T16:55:29Z
[ "python", "perl", "web-services" ]
WebService client libraries for Python and Perl
2,524,195
<p>I want to access web service in Python or/and Perl scripts. What are the most popular and reliable libraries today?</p> <p>I read <a href="http://stackoverflow.com/questions/159802/python-web-services">this</a> question, and I know about SOAPpy and ZSI. Can anybody say something about this libraries? Are they relia...
2
2010-03-26T15:01:44Z
2,525,951
<p>Perl has fantastic CPAN libraries available for doing SOAP. Probably the easiest would be <a href="http://search.cpan.org/perldoc?SOAP%3a%3aLite" rel="nofollow">SOAP::Lite</a>:</p> <blockquote> <p>SOAP::Lite is a collection of Perl modules which provides a simple and lightweight interface to the Simple Object Ac...
0
2010-03-26T19:00:08Z
[ "python", "perl", "web-services" ]
Entity references and lxml
2,524,299
<p>Here's the code I have:</p> <pre><code>from cStringIO import StringIO from lxml import etree xml = StringIO('''&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE root [ &lt;!ENTITY test "This is a test"&gt; ]&gt; &lt;root&gt; &lt;sub&gt;&amp;test;&lt;/sub&gt; &lt;/root&gt;''') d1 = etree.parse(xml) print...
8
2010-03-26T15:14:23Z
2,524,627
<p>The "unresolved" Entity is left as child node of the element node <code>sub</code></p> <pre><code>&gt;&gt;&gt; print d2.find('/sub')[0] &amp;test; &gt;&gt;&gt; d2.find('/sub').getchildren() [&amp;test;] </code></pre>
19
2010-03-26T15:54:11Z
[ "python", "xml", "lxml" ]
How do I join three tables with SQLalchemy and keeping all of the columns in one of the tables?
2,524,600
<p>So, I have three tables:</p> <p>The class defenitions:</p> <pre><code>engine = create_engine('sqlite://test.db', echo=False) SQLSession = sessionmaker(bind=engine) Base = declarative_base() class Channel(Base): __tablename__ = 'channel' id = Column(Integer, primary_key = True) title = Column(String) ...
7
2010-03-26T15:52:05Z
2,525,164
<p>To make this a little easyer I've added relationships to your model, that way you can just do user.subscriptions to get all the subscriptions.</p> <pre><code>engine = create_engine('sqlite://test.db', echo=False) SQLSession = sessionmaker(bind=engine) Base = declarative_base() class Channel(Base): __tablename_...
1
2010-03-26T17:06:20Z
[ "python", "sql", "sqlite", "sqlalchemy" ]
How do I join three tables with SQLalchemy and keeping all of the columns in one of the tables?
2,524,600
<p>So, I have three tables:</p> <p>The class defenitions:</p> <pre><code>engine = create_engine('sqlite://test.db', echo=False) SQLSession = sessionmaker(bind=engine) Base = declarative_base() class Channel(Base): __tablename__ = 'channel' id = Column(Integer, primary_key = True) title = Column(String) ...
7
2010-03-26T15:52:05Z
2,525,585
<p>Don't query from the user. Query from the Channel.</p> <pre><code>user = query(User).filter_by(id=1).one() for channel in query(Channel).all(): print channel.id, channel.title, user in channel.subscriptions.user </code></pre> <p>That way you get all the channels, not just the ones that are associated with the...
0
2010-03-26T17:58:28Z
[ "python", "sql", "sqlite", "sqlalchemy" ]
How do I join three tables with SQLalchemy and keeping all of the columns in one of the tables?
2,524,600
<p>So, I have three tables:</p> <p>The class defenitions:</p> <pre><code>engine = create_engine('sqlite://test.db', echo=False) SQLSession = sessionmaker(bind=engine) Base = declarative_base() class Channel(Base): __tablename__ = 'channel' id = Column(Integer, primary_key = True) title = Column(String) ...
7
2010-03-26T15:52:05Z
2,527,255
<p><strong>Option-1:</strong></p> <p><code>Subscription</code> is just a many-to-many relation object, and I would suggest that you model it as such rather then as a separate class. See <a href="http://www.sqlalchemy.org/docs/reference/ext/declarative.html#configuring-many-to-many-relationships">Configuring Many-to-Ma...
10
2010-03-26T22:44:11Z
[ "python", "sql", "sqlite", "sqlalchemy" ]
Machine Learning Algorithm for Predicting Order of Events?
2,524,608
<p>Simple machine learning question. Probably numerous ways to solve this:</p> <p>There is an <strong>infinite</strong> stream of 4 possible events:</p> <p><code>'event_1', 'event_2', 'event_4', 'event_4'</code></p> <p>The events do not come in in completely random order. We will assume that there are some complex ...
31
2010-03-26T15:52:17Z
2,524,759
<blockquote> <p>The question arises of how long of a history that the predictor should maintain</p> </blockquote> <p>The only answer is "it depends".</p> <p>It depends on how accurate this needs to be. I don't believe this strategy could ever be 100% accurate even with an infinite history. Try out a history of 10 a...
0
2010-03-26T16:10:02Z
[ "python", "compression", "machine-learning", "neural-network", "evolutionary-algorithm" ]
Machine Learning Algorithm for Predicting Order of Events?
2,524,608
<p>Simple machine learning question. Probably numerous ways to solve this:</p> <p>There is an <strong>infinite</strong> stream of 4 possible events:</p> <p><code>'event_1', 'event_2', 'event_4', 'event_4'</code></p> <p>The events do not come in in completely random order. We will assume that there are some complex ...
31
2010-03-26T15:52:17Z
2,524,780
<p>We just studied about <a href="http://en.wikipedia.org/wiki/Branch_predictor" rel="nofollow">branch-predictors</a> in computer architecture (Because the processor would take too long to actually evaluate a condition if(EXPRESSION), it tries to 'guess' and save some time that way). I am sure more research has been do...
0
2010-03-26T16:11:42Z
[ "python", "compression", "machine-learning", "neural-network", "evolutionary-algorithm" ]
Machine Learning Algorithm for Predicting Order of Events?
2,524,608
<p>Simple machine learning question. Probably numerous ways to solve this:</p> <p>There is an <strong>infinite</strong> stream of 4 possible events:</p> <p><code>'event_1', 'event_2', 'event_4', 'event_4'</code></p> <p>The events do not come in in completely random order. We will assume that there are some complex ...
31
2010-03-26T15:52:17Z
2,525,149
<p>This is essentially a sequence prediction problem, so you want Recurrent neural networks or hidden Markov models.</p> <p>If you only have a fixed time to look back, time window approaches might suffice. You take the sequence data and split it into overlapping windows of length n. (eg. you split a sequence ABCDEFG i...
20
2010-03-26T17:04:07Z
[ "python", "compression", "machine-learning", "neural-network", "evolutionary-algorithm" ]
Machine Learning Algorithm for Predicting Order of Events?
2,524,608
<p>Simple machine learning question. Probably numerous ways to solve this:</p> <p>There is an <strong>infinite</strong> stream of 4 possible events:</p> <p><code>'event_1', 'event_2', 'event_4', 'event_4'</code></p> <p>The events do not come in in completely random order. We will assume that there are some complex ...
31
2010-03-26T15:52:17Z
2,525,170
<p>Rather than keeping a full history, one can keep <strong><em>aggregated information</em></strong> about the past (along with a relatively short sliding history, to be used as input to the Predictor logic).</p> <p>A tentative implementation could go like this:<br> In a nutshell: <strong>Managing a set of Markov chai...
10
2010-03-26T17:06:43Z
[ "python", "compression", "machine-learning", "neural-network", "evolutionary-algorithm" ]
Machine Learning Algorithm for Predicting Order of Events?
2,524,608
<p>Simple machine learning question. Probably numerous ways to solve this:</p> <p>There is an <strong>infinite</strong> stream of 4 possible events:</p> <p><code>'event_1', 'event_2', 'event_4', 'event_4'</code></p> <p>The events do not come in in completely random order. We will assume that there are some complex ...
31
2010-03-26T15:52:17Z
3,144,105
<p>Processors use a few really lightweight tricks to predict whether a branch statement will branch or not. This helps them with efficient pipe-lining. They may not be as general as Markov models for instance, but they are interesting because of their simplicity. <a href="http://en.wikipedia.org/wiki/Branch_predictor" ...
0
2010-06-29T19:39:26Z
[ "python", "compression", "machine-learning", "neural-network", "evolutionary-algorithm" ]
command CHOICE in DOS batch replacement/reproduction in python
2,524,693
<p>I want to reproduce the behavior of the command CHOICE in DOS batch but with python.</p> <p>raw_input requires the user to type whatever then press the ENTER/RETURN key. What I really want is for the user to press a single key and the script to continue from there. </p>
2
2010-03-26T16:01:38Z
2,524,716
<p>For Unix, it uses sys, tty, termios modules.</p> <pre><code>import sys, tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) </code></pre> <p>For Windows, it uses msvcrt module.</p> <pre><code>import msvcrt ch = msvcrt.getch() </code></pre...
3
2010-03-26T16:04:29Z
[ "python", "batch-file" ]
command CHOICE in DOS batch replacement/reproduction in python
2,524,693
<p>I want to reproduce the behavior of the command CHOICE in DOS batch but with python.</p> <p>raw_input requires the user to type whatever then press the ENTER/RETURN key. What I really want is for the user to press a single key and the script to continue from there. </p>
2
2010-03-26T16:01:38Z
2,524,763
<p>A small utility class to read single characters from standard input : <a href="http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/" rel="nofollow">http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/</a></p>
2
2010-03-26T16:10:27Z
[ "python", "batch-file" ]
Python: try statement in a single line
2,524,853
<p>Is there a way in python to turn a try/except into a single line?</p> <p>something like...</p> <pre><code>b = 'some variable' a = c | b #try statement goes here </code></pre> <p>Where <code>b</code> is a declared variable and <code>c</code> is not... so <code>c</code> would throw an error and <code>a</code> would...
31
2010-03-26T16:21:19Z
2,524,880
<p>There is no way to compress a <code>try</code>/<code>except</code> block onto a single line in Python.</p> <p>Also, it is a bad thing not to know whether a variable exists in Python, like you would in some other dynamic languages. The safer way (and the prevailing style) is to set all variables to something. If the...
26
2010-03-26T16:26:17Z
[ "python", "exception-handling" ]
Python: try statement in a single line
2,524,853
<p>Is there a way in python to turn a try/except into a single line?</p> <p>something like...</p> <pre><code>b = 'some variable' a = c | b #try statement goes here </code></pre> <p>Where <code>b</code> is a declared variable and <code>c</code> is not... so <code>c</code> would throw an error and <code>a</code> would...
31
2010-03-26T16:21:19Z
2,525,031
<p>You mentioned that you're using django. If it makes sense for what you're doing you might want to use:</p> <pre><code>my_instance, created = MyModel.objects.get_or_create() </code></pre> <p><code>created</code> will be True or False. Maybe this will help you.</p>
2
2010-03-26T16:44:45Z
[ "python", "exception-handling" ]
Python: try statement in a single line
2,524,853
<p>Is there a way in python to turn a try/except into a single line?</p> <p>something like...</p> <pre><code>b = 'some variable' a = c | b #try statement goes here </code></pre> <p>Where <code>b</code> is a declared variable and <code>c</code> is not... so <code>c</code> would throw an error and <code>a</code> would...
31
2010-03-26T16:21:19Z
2,525,605
<p>You can do it by accessing the namespace dict using <code>vars()</code>, <code>locals()</code>, or <code>globals()</code>, whichever is most appropriate for your situation.</p> <pre><code>&gt;&gt;&gt; b = 'some variable' &gt;&gt;&gt; a = vars().get('c', b) </code></pre>
3
2010-03-26T18:02:07Z
[ "python", "exception-handling" ]
Python: try statement in a single line
2,524,853
<p>Is there a way in python to turn a try/except into a single line?</p> <p>something like...</p> <pre><code>b = 'some variable' a = c | b #try statement goes here </code></pre> <p>Where <code>b</code> is a declared variable and <code>c</code> is not... so <code>c</code> would throw an error and <code>a</code> would...
31
2010-03-26T16:21:19Z
8,061,176
<p>This is terribly hackish, but I've used it at the prompt when I wanted to write up a sequence of actions for debugging:</p> <pre><code>exec "try: some_problematic_thing()\nexcept: problem=sys.exc_info()" print "The problem is %s" % problem[1] </code></pre> <p>For the most part, I'm not at all bothered by the no-si...
34
2011-11-09T06:26:30Z
[ "python", "exception-handling" ]
Which Python module to use for Memcached?
2,524,881
<p>I am implementing caching for my Python application and I want to use memcached. Which module do you suggest me to use? There are so many that I don't know which one to choose.</p> <p>Thanks, Boda Cydo.</p>
3
2010-03-26T16:26:19Z
2,524,956
<p>I use <a href="http://www.tummy.com/Community/software/python-memcached/" rel="nofollow">python-memcached</a> and there's some good advice on usage in the source code header, referenced in <a href="http://stackoverflow.com/questions/868690/good-examples-of-python-memcache-memcached-being-used-in-python/868731#868731...
3
2010-03-26T16:35:02Z
[ "python", "caching", "memcached" ]
Which Python module to use for Memcached?
2,524,881
<p>I am implementing caching for my Python application and I want to use memcached. Which module do you suggest me to use? There are so many that I don't know which one to choose.</p> <p>Thanks, Boda Cydo.</p>
3
2010-03-26T16:26:19Z
2,525,034
<p>I use cmemcache, which is more performant (but no more mantained). As its developer suggest, you can switch to <a href="http://code.google.com/p/python-libmemcached" rel="nofollow">http://code.google.com/p/python-libmemcached</a>.</p>
2
2010-03-26T16:45:48Z
[ "python", "caching", "memcached" ]
Which Python module to use for Memcached?
2,524,881
<p>I am implementing caching for my Python application and I want to use memcached. Which module do you suggest me to use? There are so many that I don't know which one to choose.</p> <p>Thanks, Boda Cydo.</p>
3
2010-03-26T16:26:19Z
16,429,824
<p>I use <a href="https://pypi.python.org/pypi/python-memcached/" rel="nofollow">python-memcache</a> because:</p> <ol> <li>You can run in locally</li> <li>It is embedded in <a href="https://www.djangoproject.com/" rel="nofollow">Django framework</a></li> <li>Simple to use</li> </ol> <p>from memcached.py header:</p> ...
0
2013-05-07T22:46:03Z
[ "python", "caching", "memcached" ]
Python float copy question
2,524,998
<p>I'm puzzled by some behaviour I'm seeing when copying a float array member into another variable - please help!</p> <p>For example </p> <pre><code>data_entry[1] = 9.6850069951 new_value = data_entry[1] &lt;comment&gt; #print both 9.6850069951 9.6850663300 </code></pre> <p>I'm aware of the problem of binary...
0
2010-03-26T16:40:02Z
2,525,041
<p>Didn't work for me with Python 2.6.2 on Linux:</p> <pre><code>&gt;&gt;&gt; data_entry = [1, 2] &gt;&gt;&gt; data_entry[1] = 9.6850069951 &gt;&gt;&gt; new_value = data_entry[1] &gt;&gt;&gt; print data_entry[1] --&gt; print(data_entry[1]) 9.6850069951 &gt;&gt;&gt; print new_value --&gt; print(new_value) 9.6850069951 ...
0
2010-03-26T16:46:41Z
[ "python", "floating-point", "variable-assignment" ]
Python float copy question
2,524,998
<p>I'm puzzled by some behaviour I'm seeing when copying a float array member into another variable - please help!</p> <p>For example </p> <pre><code>data_entry[1] = 9.6850069951 new_value = data_entry[1] &lt;comment&gt; #print both 9.6850069951 9.6850663300 </code></pre> <p>I'm aware of the problem of binary...
0
2010-03-26T16:40:02Z
2,525,101
<p>You've left some code out.</p> <pre><code>&gt;&gt;&gt; data_entry=[0,0] &gt;&gt;&gt; data_entry[1] = 9.6850069951 &gt;&gt;&gt; &gt;&gt;&gt; new_value = data_entry[1] &gt;&gt;&gt; print data_entry [0, 9.6850069951000002] &gt;&gt;&gt; print new_value 9.6850069951 &gt;&gt;&gt; print data_entry[1] 9.6850069951 </code>...
0
2010-03-26T16:54:28Z
[ "python", "floating-point", "variable-assignment" ]
Python float copy question
2,524,998
<p>I'm puzzled by some behaviour I'm seeing when copying a float array member into another variable - please help!</p> <p>For example </p> <pre><code>data_entry[1] = 9.6850069951 new_value = data_entry[1] &lt;comment&gt; #print both 9.6850069951 9.6850663300 </code></pre> <p>I'm aware of the problem of binary...
0
2010-03-26T16:40:02Z
2,525,225
<p>After an assignment the variable new_value is not a copy of the float, it's just another reference to the exact same object. Therefore it cannot possibly have a different printed representation. So there's definitely some detail omitted in the original question.</p> <p>Stuart - can you please try the following and ...
4
2010-03-26T17:15:08Z
[ "python", "floating-point", "variable-assignment" ]
Python float copy question
2,524,998
<p>I'm puzzled by some behaviour I'm seeing when copying a float array member into another variable - please help!</p> <p>For example </p> <pre><code>data_entry[1] = 9.6850069951 new_value = data_entry[1] &lt;comment&gt; #print both 9.6850069951 9.6850663300 </code></pre> <p>I'm aware of the problem of binary...
0
2010-03-26T16:40:02Z
2,525,734
<p>If you're really using the <a href="http://docs.python.org/library/array.html?highlight=array#module-array" rel="nofollow">array</a> module (or <code>numpy</code>'s arrays) the precision loss is easy to explain, e.g.:</p> <pre><code>&gt;&gt;&gt; dataentry = array.array('f', [9.6850069951]) &gt;&gt;&gt; dataentry[0]...
3
2010-03-26T18:25:44Z
[ "python", "floating-point", "variable-assignment" ]
Python float copy question
2,524,998
<p>I'm puzzled by some behaviour I'm seeing when copying a float array member into another variable - please help!</p> <p>For example </p> <pre><code>data_entry[1] = 9.6850069951 new_value = data_entry[1] &lt;comment&gt; #print both 9.6850069951 9.6850663300 </code></pre> <p>I'm aware of the problem of binary...
0
2010-03-26T16:40:02Z
2,598,914
<p>Here's some edited code formatted:</p> <pre><code>old code: data = [] for data_entry in data: if (data_entry[1] != 0): value = data_entry[1] modlog(logging.INFO,'raw value = %.12f',data_entry[1]) modlog(logging.INFO,'value_in = %.12f', value) output: :INFO:raw value = 2.334650748292 :INFO:value_in ...
0
2010-04-08T09:55:40Z
[ "python", "floating-point", "variable-assignment" ]
How can I catch non valid ASCII characters in Panic Coda?
2,525,002
<p>Sometimes I get errors while coding, because of typing some combination of keys (eg. ALT + SHIFT + SQUARE BRACKET) in a wrong way. So I get Syntax Error in Python, but I can't see where the illegal character is, 'cause Coda do not show it to me. Any solution?</p>
0
2010-03-26T16:40:41Z
2,525,363
<p>Python uses the 7-bit ASCII character set for program text. So all you need to do is convert your program to ASCII by going to text > convert to ASCII. Any bad characters will automatically be removed. If you want to specifically know which characters were the bad ones, you can use diff to compare changed and unchan...
0
2010-03-26T17:31:24Z
[ "python", "ascii", "coda" ]