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
Python beginner confused by a complex line of code
2,581,965
<p>I understand the gist of the code, that it forms permutations; however, I was wondering if someone could explain exactly what is going on in the return statement.</p> <pre><code>def perm(l): sz = len(l) print (l) if sz &lt;= 1: print ('sz &lt;= 1') return [l] return [p[:i]+[l[0]]+p[i...
4
2010-04-06T00:34:51Z
2,581,984
<p>The return statement is using a list comprehension. It's a bit easier to understand if you put it into actual loops:</p> <pre><code>value = [] for i in range(sz): # call this function using all but the first item in l for p in perm(l[1:]): # now place the first item in l between index i-1 and index ...
5
2010-04-06T00:39:28Z
[ "python", "list-comprehension" ]
Python beginner confused by a complex line of code
2,581,965
<p>I understand the gist of the code, that it forms permutations; however, I was wondering if someone could explain exactly what is going on in the return statement.</p> <pre><code>def perm(l): sz = len(l) print (l) if sz &lt;= 1: print ('sz &lt;= 1') return [l] return [p[:i]+[l[0]]+p[i...
4
2010-04-06T00:34:51Z
2,581,995
<p>Look at this:</p> <pre><code>&gt;&gt;&gt; l = [1, 2, 3, 4, 5, 6] &gt;&gt;&gt; p = l[1:] &gt;&gt;&gt; p [2, 3, 4, 5, 6] &gt;&gt;&gt; i = 3 &gt;&gt;&gt; p[:i] [2, 3, 4] &gt;&gt;&gt; p[i:] [5, 6] &gt;&gt;&gt; p[:i]+[l[0]]+p[i:] [2, 3, 4, 1, 5, 6] &gt;&gt;&gt; </code></pre> <p>So, here's the thing, <code>p</code> sta...
2
2010-04-06T00:42:39Z
[ "python", "list-comprehension" ]
Python beginner confused by a complex line of code
2,581,965
<p>I understand the gist of the code, that it forms permutations; however, I was wondering if someone could explain exactly what is going on in the return statement.</p> <pre><code>def perm(l): sz = len(l) print (l) if sz &lt;= 1: print ('sz &lt;= 1') return [l] return [p[:i]+[l[0]]+p[i...
4
2010-04-06T00:34:51Z
2,582,004
<p>This <code>return</code> is returning a list comprehension whose items are made by inserting the first item of <code>l</code> into each position of <code>p</code>, from the first to the last -- <code>p</code> in turn is a list of lists, obtained by a recursive call to <code>perm</code> which excludes the first item ...
11
2010-04-06T00:44:55Z
[ "python", "list-comprehension" ]
Python beginner confused by a complex line of code
2,581,965
<p>I understand the gist of the code, that it forms permutations; however, I was wondering if someone could explain exactly what is going on in the return statement.</p> <pre><code>def perm(l): sz = len(l) print (l) if sz &lt;= 1: print ('sz &lt;= 1') return [l] return [p[:i]+[l[0]]+p[i...
4
2010-04-06T00:34:51Z
2,582,712
<p>There are a couple of examples in the <a href="http://docs.python.org/dev/library/itertools.html?highlight=itertools#itertools.permutations" rel="nofollow">Python documentation for the <code>itertools.permutations()</code> function</a> that are easier to digest. Note that this function is new in Python 2.6, so it wo...
1
2010-04-06T04:59:01Z
[ "python", "list-comprehension" ]
Python beginner confused by a complex line of code
2,581,965
<p>I understand the gist of the code, that it forms permutations; however, I was wondering if someone could explain exactly what is going on in the return statement.</p> <pre><code>def perm(l): sz = len(l) print (l) if sz &lt;= 1: print ('sz &lt;= 1') return [l] return [p[:i]+[l[0]]+p[i...
4
2010-04-06T00:34:51Z
2,582,990
<p>Okay, let's begin.</p> <h2>Starting code</h2> <p>(minus print statements)</p> <pre><code>def perm(l): sz = len(l) if sz &lt;= 1: return [l] return [p[:i]+[l[0]]+p[i:] for i in range(sz) for p in perm(l[1:])] </code></pre> <h2>Revision 1</h2> <pre><code>def perm(s): # Base case: an empty ...
1
2010-04-06T06:13:25Z
[ "python", "list-comprehension" ]
finding and replacing elements in a list (python)
2,582,138
<p>I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this?</p> <p>For example, suppose my list has the following integers</p> <pre><code>&gt;&gt;&gt; a = [1,2,3,4,5,1,2,3,4,5,1] </code></pre> <p>and I...
88
2010-04-06T01:30:18Z
2,582,163
<p>Try using a <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehension</a> and the <a href="http://en.wikipedia.org/wiki/Ternary_operation#Python">ternary operator</a>.</p> <pre><code>&gt;&gt;&gt; a=[1,2,3,1,3,2,1,1] &gt;&gt;&gt; [4 if x==1 else x for x in a] [4, 2, 3, 4, 3...
216
2010-04-06T01:37:11Z
[ "python", "list", "replace" ]
finding and replacing elements in a list (python)
2,582,138
<p>I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this?</p> <p>For example, suppose my list has the following integers</p> <pre><code>&gt;&gt;&gt; a = [1,2,3,4,5,1,2,3,4,5,1] </code></pre> <p>and I...
88
2010-04-06T01:30:18Z
2,582,167
<pre><code>&gt;&gt;&gt; a=[1,2,3,4,5,1,2,3,4,5,1] &gt;&gt;&gt; item_to_replace = 1 &gt;&gt;&gt; replacement_value = 6 &gt;&gt;&gt; indices_to_replace = [i for i,x in enumerate(a) if x==item_to_replace] &gt;&gt;&gt; indices_to_replace [0, 5, 10] &gt;&gt;&gt; for i in indices_to_replace: ... a[i] = replacement_value ...
4
2010-04-06T01:37:52Z
[ "python", "list", "replace" ]
finding and replacing elements in a list (python)
2,582,138
<p>I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this?</p> <p>For example, suppose my list has the following integers</p> <pre><code>&gt;&gt;&gt; a = [1,2,3,4,5,1,2,3,4,5,1] </code></pre> <p>and I...
88
2010-04-06T01:30:18Z
2,582,183
<pre><code>&gt;&gt;&gt; a=[1,2,3,4,5,1,2,3,4,5,1] &gt;&gt;&gt; for n,i in enumerate(a): ... if i==1: ... a[n]=10 ... &gt;&gt;&gt; a [10, 2, 3, 4, 5, 10, 2, 3, 4, 5, 10] </code></pre>
98
2010-04-06T01:41:33Z
[ "python", "list", "replace" ]
finding and replacing elements in a list (python)
2,582,138
<p>I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this?</p> <p>For example, suppose my list has the following integers</p> <pre><code>&gt;&gt;&gt; a = [1,2,3,4,5,1,2,3,4,5,1] </code></pre> <p>and I...
88
2010-04-06T01:30:18Z
2,582,567
<p>List comprehension works well--and looping through with enumerate can save you some memory (b/c the operation's essentially be doing in place).</p> <p>There's also functional programming...see usage of <a href="http://docs.python.org/library/functions.html#map">map</a>:</p> <pre> >>> a = [1,2,3,2,3,4,3,5,6,6,5...
13
2010-04-06T03:57:54Z
[ "python", "list", "replace" ]
finding and replacing elements in a list (python)
2,582,138
<p>I have to search through a list and replace all occurrences of one element with another. So far my attempts in code are getting me nowhere, what is the best way to do this?</p> <p>For example, suppose my list has the following integers</p> <pre><code>&gt;&gt;&gt; a = [1,2,3,4,5,1,2,3,4,5,1] </code></pre> <p>and I...
88
2010-04-06T01:30:18Z
38,311,834
<p>If you have several values to replace, you can also use a dictionary:</p> <pre><code>a = [1, 2, 3, 4, 1, 5, 3, 2, 6, 1, 1] dic = {1:10, 2:20, 3:'foo'} print([dic[n] if n in dic.keys() else n for n in a]) &gt; [10, 20, 'foo', 10, 'foo', 20, 10, 10] </code></pre>
3
2016-07-11T16:28:10Z
[ "python", "list", "replace" ]
Python: combining making two scripts into one
2,582,269
<p>I have two separately made python scripts one that makes a sine wave sound based off time, and another that produces a sine wave graph that is based off the same time factors. I need help combining them into one running file.</p> <p>Here's the first:</p> <pre><code>from struct import pack from math import sin, pi...
0
2010-04-06T02:10:59Z
2,582,451
<pre><code>from Tkinter import * from struct import pack from math import sin, pi import math import time import os def wave(): t = time.strftime("%S", time.localtime()) ti = time.strftime("%M", time.localtime()) tis = float(t) tis = tis / 100 tim = float(ti) tim = tim / 100 root = Tk() ...
1
2010-04-06T03:18:16Z
[ "python", "class", "partial-classes" ]
File io error Python
2,582,550
<p>I have a program that monitors a folder with word documents for any modifications made on the files. The error -Windows Error[2] The system cannot find the file specified- comes when I run the program, open a .doc within the folder make some changes and save it. Any suggestions on how to fix this?</p> <p>Edit1: the...
0
2010-04-06T03:51:19Z
2,582,636
<p>try to use <code>os.path.join()</code> eg</p> <pre><code>root="c:\\" path=os.path.join(root,"Users","keinsfield","Desktop","colegio") .... for rootdir, dirs, files in os.walk(path): .... </code></pre>
0
2010-04-06T04:26:34Z
[ "python", "file-io" ]
File io error Python
2,582,550
<p>I have a program that monitors a folder with word documents for any modifications made on the files. The error -Windows Error[2] The system cannot find the file specified- comes when I run the program, open a .doc within the folder make some changes and save it. Any suggestions on how to fix this?</p> <p>Edit1: the...
0
2010-04-06T03:51:19Z
2,584,230
<p>I think from the traceback it's quite clear that the <em>temporary</em> file was deleted between <code>os.walk</code> and <code>os.stat</code> calls. You don't really need to use the <code>os.walk</code> if you're not recursing into the subdirectories. You could use <a href="http://docs.python.org/library/glob.html"...
0
2010-04-06T10:54:24Z
[ "python", "file-io" ]
Python Mechanize select a form with no name
2,582,580
<p>I am attempting to have mechanize select a form from a page, but the form in question has no "name" attribute in the html. What should I do? when I try to use</p> <pre><code>br.select_form(name = "") </code></pre> <p>I get errors that no form is declared with that name, and the function requires a name input. Ther...
27
2010-04-06T04:01:24Z
2,582,592
<p>Try:</p> <pre><code>br.select_form(nr=0) </code></pre> <p>to select the first form</p> <p>In Mechanize <a href="https://github.com/jjlee/mechanize/blob/b1d786906946f0193051920a7c716b339bd7bf95/mechanize/_mechanize.py#L462">source</a>,</p> <pre><code>def select_form(self, name=None, predicate=None, &lt;b&gt;nr=No...
47
2010-04-06T04:06:03Z
[ "python", "html", "forms", "mechanize" ]
Python Mechanize select a form with no name
2,582,580
<p>I am attempting to have mechanize select a form from a page, but the form in question has no "name" attribute in the html. What should I do? when I try to use</p> <pre><code>br.select_form(name = "") </code></pre> <p>I get errors that no form is declared with that name, and the function requires a name input. Ther...
27
2010-04-06T04:01:24Z
39,817,259
<p>If you want to execute code for multiple forms no matter what their name is, you can loop over every form letting your script knowing which form will work next.</p> <pre><code>currentForm = 0 for form in br.forms(): # Iterate over the forms br.select_form(nr = currentForm) # Select the form ''' ...
0
2016-10-02T12:58:12Z
[ "python", "html", "forms", "mechanize" ]
Requires a valid Date or x-amz-date header?
2,582,600
<p>I'm getting the following error when attempting to upload a file to S3:</p> <pre><code>S3StorageError: &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Error&gt;&lt;Code&gt;AccessDenied&lt;/Code&gt;&lt;Message&gt;AWS authentication requires a valid Date or x-amz-date header&lt;/Message&gt;&lt;RequestId&gt;7910FF83F...
3
2010-04-06T04:08:18Z
2,892,740
<p>I found that timezone difference seemed to be a factor. I'm an expat from Melbourne who's currently in Malaysia. Changing back to my Melbourne timezone and this error went away (was occuring in all major s3 access tools I was using such as s3fox, s3hub etc.). </p>
1
2010-05-23T17:43:48Z
[ "python", "django", "http-headers", "amazon-s3", "amazon-web-services" ]
Requires a valid Date or x-amz-date header?
2,582,600
<p>I'm getting the following error when attempting to upload a file to S3:</p> <pre><code>S3StorageError: &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Error&gt;&lt;Code&gt;AccessDenied&lt;/Code&gt;&lt;Message&gt;AWS authentication requires a valid Date or x-amz-date header&lt;/Message&gt;&lt;RequestId&gt;7910FF83F...
3
2010-04-06T04:08:18Z
3,637,323
<p>I think it's a bad idea having the 'x-amz-date' header in your settings.py as this file only gets run once each time the server is started.</p> <p>I guess you were using the development server, which gets restarted every time you change some .py code.</p> <p>Anyway, django-storages already has code to set the rele...
0
2010-09-03T15:37:26Z
[ "python", "django", "http-headers", "amazon-s3", "amazon-web-services" ]
What's the easiest way to get my facebook status and photos using python?
2,582,627
<p>I just want to import my facebook status and photos to my personal django website but all the examples and documentation i can find are for developing facebook applications.</p> <p>A simple rss feed would be enough but it doesnt seem to exist in facebook. Do i really have to create a full facebook app to do this?</...
0
2010-04-06T04:21:05Z
2,582,677
<p>Probably. Anything that bypassed authentication would be a fairly large privacy issue. </p>
0
2010-04-06T04:42:58Z
[ "python", "django", "facebook", "pyfacebook" ]
What's the easiest way to get my facebook status and photos using python?
2,582,627
<p>I just want to import my facebook status and photos to my personal django website but all the examples and documentation i can find are for developing facebook applications.</p> <p>A simple rss feed would be enough but it doesnt seem to exist in facebook. Do i really have to create a full facebook app to do this?</...
0
2010-04-06T04:21:05Z
2,582,682
<p>A simple facebook application isn't that hard ... excluding trying to decipher the soup on developers.facebook.com.</p> <p>The "problem" is that you need to get an application key, application secret, and sometimes a session key in order to access the web services. Unless someone is sharing a service to do just tha...
2
2010-04-06T04:44:51Z
[ "python", "django", "facebook", "pyfacebook" ]
What's the easiest way to get my facebook status and photos using python?
2,582,627
<p>I just want to import my facebook status and photos to my personal django website but all the examples and documentation i can find are for developing facebook applications.</p> <p>A simple rss feed would be enough but it doesnt seem to exist in facebook. Do i really have to create a full facebook app to do this?</...
0
2010-04-06T04:21:05Z
2,704,299
<p>With the release of the new graph api, this is pretty simple once you get your oauth token. Unfortunately you will need to create an app, but it can be a rather small one to get your oauth token so facebook can authorize your requests. You can use the python sdk here: <a href="http://github.com/facebook/python-sdk...
0
2010-04-24T11:35:56Z
[ "python", "django", "facebook", "pyfacebook" ]
Python beginner, strange output problem
2,582,756
<p>I'm having a weird problem with the following piece of code.</p> <pre><code>from math import sqrt def Permute(array): result1 = [] result2 = [] if len(array) &lt;= 1: return array for subarray in Permute(array[1:]): for i in range(len(array)): temp1 = subarray[:i]+array[...
0
2010-04-06T05:12:35Z
2,582,945
<p>The problem is that you recursively call <code>Permute(array[1:])</code>, then use the recursive result to calculate <code>temp1</code>. Why is this a problem? Your function outputs an array of arrays, where the last subarray is <code>temp2</code>, the distance sum. So, every level of recursion, you will add more...
2
2010-04-06T06:01:24Z
[ "python" ]
Python beginner, strange output problem
2,582,756
<p>I'm having a weird problem with the following piece of code.</p> <pre><code>from math import sqrt def Permute(array): result1 = [] result2 = [] if len(array) &lt;= 1: return array for subarray in Permute(array[1:]): for i in range(len(array)): temp1 = subarray[:i]+array[...
0
2010-04-06T05:12:35Z
2,582,977
<p>I agree with what Justin Ardini said, but I would also suggest that you learn to use a Python debugger like <a href="http://docs.python.org/library/pdb.html" rel="nofollow">pdb</a>. Then you can go through functions like this and figure out what is going on for yourself. You'll learn a lot more that way. </p>
0
2010-04-06T06:09:54Z
[ "python" ]
Python beginner, strange output problem
2,582,756
<p>I'm having a weird problem with the following piece of code.</p> <pre><code>from math import sqrt def Permute(array): result1 = [] result2 = [] if len(array) &lt;= 1: return array for subarray in Permute(array[1:]): for i in range(len(array)): temp1 = subarray[:i]+array[...
0
2010-04-06T05:12:35Z
2,583,451
<p>I think what you're trying to do is:</p> <pre><code>from math import sqrt def Permute(array): result1 = [] result2 = [] if len(array) &lt;= 1: for subarray in array: for i in range(len(array)): temp1 = subarray[:i]+array[0]+subarray[i:] temp2 = [0] ...
0
2010-04-06T08:12:14Z
[ "python" ]
Tuple unpacking: dummy variable vs index
2,582,803
<p>What is the usual/clearest way to write this in Python?</p> <pre><code>value, _ = func_returning_a_tuple() </code></pre> <p>or:</p> <pre><code>value = func_returning_a_tuple()[0] </code></pre>
13
2010-04-06T05:24:56Z
2,582,820
<p>For extracting a single item, indexing is a bit more idiomatic. When you're extracting two or more items, unpacking becomes more idiomatic. That's just empirical observation on my part; I don't know of any style guides recommending or mandating either choice!-)</p>
6
2010-04-06T05:28:48Z
[ "coding-style", "python" ]
Tuple unpacking: dummy variable vs index
2,582,803
<p>What is the usual/clearest way to write this in Python?</p> <pre><code>value, _ = func_returning_a_tuple() </code></pre> <p>or:</p> <pre><code>value = func_returning_a_tuple()[0] </code></pre>
13
2010-04-06T05:24:56Z
2,582,826
<p><code>value = func_returning_a_tuple()[0]</code> seems clearer and also can be generalized.</p> <p>What if the function was returning a tuple with more than 2 values?<br> What if the program logic is interested in the 4th element of an umpteen tuple?<br> What if the size of the returned tuple varies?<br></p> <p>N...
10
2010-04-06T05:29:34Z
[ "coding-style", "python" ]
Tuple unpacking: dummy variable vs index
2,582,803
<p>What is the usual/clearest way to write this in Python?</p> <pre><code>value, _ = func_returning_a_tuple() </code></pre> <p>or:</p> <pre><code>value = func_returning_a_tuple()[0] </code></pre>
13
2010-04-06T05:24:56Z
2,583,088
<p>If you'd appreciate a handy way to do this in python3.x, check out the python enhancement proposal (PEP) 3132 on this <a href="http://docs.python.org/py3k/whatsnew/index.html">page of What's New</a> in Python:</p> <p>Extended Iterable Unpacking. You can now write things like <code>a, b, *rest = some_sequence</code>...
10
2010-04-06T06:38:59Z
[ "coding-style", "python" ]
Tuple unpacking: dummy variable vs index
2,582,803
<p>What is the usual/clearest way to write this in Python?</p> <pre><code>value, _ = func_returning_a_tuple() </code></pre> <p>or:</p> <pre><code>value = func_returning_a_tuple()[0] </code></pre>
13
2010-04-06T05:24:56Z
9,736,856
<p>For list/generator comprehensions with key/value pairs I think the usage of the dummy variable can be quite neat, especially where the unpacked value needs to be used more than once (avoiding repeated indexing), e.g.:</p> <pre><code>l = [('a', 1.54), ('b', 4.34), ('c', 3.22), ('d', 6.43)] s = [x * (1.0 - x) * (2.0 ...
1
2012-03-16T12:02:26Z
[ "coding-style", "python" ]
Simple way to decrease values without making a new attribute?
2,582,863
<p>I'm making a program where you're firing a 'blaster', and I have 5 ammo. I'm blasting an alien who has 5 health. At the end I instantiate the player and make him blast 6 times to check that the program works correctly. But the way I've done it makes it so that the amount won't decrease. Is there an easy fix to this,...
1
2010-04-06T05:37:29Z
2,582,880
<p>Probably you want to do something like this:</p> <pre><code>class Player: def __init__(self, ammo=5): self.ammo=ammo def blast(self, enemy): if self.ammo&gt;0: self.ammo-=1 enemy.die(5) else: print "U are out of ammunition!" </code></pre> <p>You need to use <cod...
0
2010-04-06T05:43:44Z
[ "python", "attributes", "instantiation" ]
Simple way to decrease values without making a new attribute?
2,582,863
<p>I'm making a program where you're firing a 'blaster', and I have 5 ammo. I'm blasting an alien who has 5 health. At the end I instantiate the player and make him blast 6 times to check that the program works correctly. But the way I've done it makes it so that the amount won't decrease. Is there an easy fix to this,...
1
2010-04-06T05:37:29Z
2,582,883
<p>Think about it: the amount of ammunition available is part of a player's state. An object's state is best represented as instance variables of that object. So you should <strong>not</strong> have <code>ammo</code> as an argument to <code>blast</code> -- it should be <code>self.ammo</code> in that method, initializ...
5
2010-04-06T05:44:10Z
[ "python", "attributes", "instantiation" ]
Simple way to decrease values without making a new attribute?
2,582,863
<p>I'm making a program where you're firing a 'blaster', and I have 5 ammo. I'm blasting an alien who has 5 health. At the end I instantiate the player and make him blast 6 times to check that the program works correctly. But the way I've done it makes it so that the amount won't decrease. Is there an easy fix to this,...
1
2010-04-06T05:37:29Z
2,582,884
<p>You need to keep track of the alien's health. All you're doing now is decrementing the "health" local variable in the Alien.die function.</p> <p>Here's a little snippet that should help get you going in the right direction:</p> <pre><code>class Alien(object): def __init__(self): self.health = 5 de...
3
2010-04-06T05:44:19Z
[ "python", "attributes", "instantiation" ]
Simple way to decrease values without making a new attribute?
2,582,863
<p>I'm making a program where you're firing a 'blaster', and I have 5 ammo. I'm blasting an alien who has 5 health. At the end I instantiate the player and make him blast 6 times to check that the program works correctly. But the way I've done it makes it so that the amount won't decrease. Is there an easy fix to this,...
1
2010-04-06T05:37:29Z
2,582,885
<p>In <code>class Player</code> in the method named <code>blast()</code> your references to to <code>ammo</code> are incorrect. You want to refer to <code>self.ammo</code> in all cases within that function/method definition.</p> <p>Furthermore you should probably (almost certainly) define an <code>__init__</code> meth...
0
2010-04-06T05:44:25Z
[ "python", "attributes", "instantiation" ]
Simple way to decrease values without making a new attribute?
2,582,863
<p>I'm making a program where you're firing a 'blaster', and I have 5 ammo. I'm blasting an alien who has 5 health. At the end I instantiate the player and make him blast 6 times to check that the program works correctly. But the way I've done it makes it so that the amount won't decrease. Is there an easy fix to this,...
1
2010-04-06T05:37:29Z
5,780,262
<p>I just modify the above program by making 2 attributes named ammo and health. I think they make program so easy. Try it for various outcomes by changing attributes' initial values.</p> <pre><code>class Player(object): """ A player in a shooter game. """ def __init__(self, ammo): self.ammo = ammo def blast(se...
1
2011-04-25T15:59:45Z
[ "python", "attributes", "instantiation" ]
Implement a server that receives and processes client request(cassandra as backend), Python or C++?
2,582,892
<p>I am planning to build an inverted index searching system with cassandra as its storage backend. But I need some guidances to build a highly efficient searching daemon server. I know a web server written in Python called tornado, my questions are:</p> <ol> <li>Is Python a good choice for developing such kind of app...
1
2010-04-06T05:47:08Z
2,582,910
<p><a href="http://twistedmatrix.com/trac/" rel="nofollow">Twisted framework</a> is a good starting point for develop backend servers. It is fast and scalable. </p>
1
2010-04-06T05:52:04Z
[ "c++", "python", "search", "full-text-search" ]
Implement a server that receives and processes client request(cassandra as backend), Python or C++?
2,582,892
<p>I am planning to build an inverted index searching system with cassandra as its storage backend. But I need some guidances to build a highly efficient searching daemon server. I know a web server written in Python called tornado, my questions are:</p> <ol> <li>Is Python a good choice for developing such kind of app...
1
2010-04-06T05:47:08Z
2,583,480
<p>Python is unlikely to allow you to write the most efficient server possible. However, it may just be that it will be fast enough, because for most applications it <em>is</em>.</p> <p>Therefore, one path you could take is starting with Python. It's a great language for prototyping, much better than C++ for the stage...
0
2010-04-06T08:19:12Z
[ "c++", "python", "search", "full-text-search" ]
Implement a server that receives and processes client request(cassandra as backend), Python or C++?
2,582,892
<p>I am planning to build an inverted index searching system with cassandra as its storage backend. But I need some guidances to build a highly efficient searching daemon server. I know a web server written in Python called tornado, my questions are:</p> <ol> <li>Is Python a good choice for developing such kind of app...
1
2010-04-06T05:47:08Z
2,594,273
<p>For search, <a href="http://github.com/tjake/Lucandra" rel="nofollow">http://github.com/tjake/Lucandra</a> may be interesting prior art.</p>
0
2010-04-07T16:44:09Z
[ "c++", "python", "search", "full-text-search" ]
How to check a list contained by another list without a loop?
2,582,911
<p>As the title mentions,is there any builtins to do this job?I looked for that in <code>dir(list)</code> but got no usable one.Thanks.</p>
6
2010-04-06T05:52:49Z
2,582,917
<p>Depends on what you mean by "contained". Maybe this:</p> <pre><code>if set(a) &lt;= set(b): print "a is in b" </code></pre>
22
2010-04-06T05:54:32Z
[ "python", "list" ]
How to check a list contained by another list without a loop?
2,582,911
<p>As the title mentions,is there any builtins to do this job?I looked for that in <code>dir(list)</code> but got no usable one.Thanks.</p>
6
2010-04-06T05:52:49Z
2,582,919
<p>Assuming that you want to see if all elements of <code>sublist</code> are also elements of <code>superlist</code>:</p> <p><code>all(x in superlist for x in sublist)</code></p>
8
2010-04-06T05:54:47Z
[ "python", "list" ]
How to check a list contained by another list without a loop?
2,582,911
<p>As the title mentions,is there any builtins to do this job?I looked for that in <code>dir(list)</code> but got no usable one.Thanks.</p>
6
2010-04-06T05:52:49Z
2,584,127
<p>the solution depends on what values you expect from your lists. </p> <p>if there is the possiblity of a repetition of a value, and you need to check that there is enough values in the tested container, then here is a time-inefficient solution:</p> <pre><code>def contained(candidate, container): temp = containe...
2
2010-04-06T10:34:00Z
[ "python", "list" ]
running jython bytecode using java
2,583,192
<p>It looks like I'm missing something.</p> <p>When using Jython to run my Python code in Java, Java bytecode files are generated (test.py -> test@py.class).</p> <p>Can I run these classes directly using java?</p> <p>In other words, I want to make this:</p> <pre><code>$ java test@py [additional cp args] </code></pr...
9
2010-04-06T07:06:38Z
2,583,261
<p>See <a href="http://wiki.python.org/jython/JythonFaq/EmbeddingJython" rel="nofollow">FAQ - Embedding Jython</a>.</p> <p>Note that <code>jythonc</code> is <a href="http://wiki.python.org/jython/ReplaceJythonc" rel="nofollow">no longer supported</a>:</p> <blockquote> <p>jythonc doesn't handle generators and is dif...
3
2010-04-06T07:20:50Z
[ "java", "python", "jython", "bytecode" ]
running jython bytecode using java
2,583,192
<p>It looks like I'm missing something.</p> <p>When using Jython to run my Python code in Java, Java bytecode files are generated (test.py -> test@py.class).</p> <p>Can I run these classes directly using java?</p> <p>In other words, I want to make this:</p> <pre><code>$ java test@py [additional cp args] </code></pr...
9
2010-04-06T07:06:38Z
2,584,522
<p>If your only concern is distributing your application without giving away the source, you might want to look at tools like <a href="http://cx-freeze.sourceforge.net/" rel="nofollow">cx_freeze</a> and <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a> on Windows and <a href="http://svn.pythonmac.org/py2app/py...
0
2010-04-06T11:46:40Z
[ "java", "python", "jython", "bytecode" ]
running jython bytecode using java
2,583,192
<p>It looks like I'm missing something.</p> <p>When using Jython to run my Python code in Java, Java bytecode files are generated (test.py -> test@py.class).</p> <p>Can I run these classes directly using java?</p> <p>In other words, I want to make this:</p> <pre><code>$ java test@py [additional cp args] </code></pr...
9
2010-04-06T07:06:38Z
15,939,006
<p>Here's what works for me:</p> <p><strong>test_p.py:</strong></p> <pre class="lang-py prettyprint-override"><code>def foo(): print 'test from Python' </code></pre> <p><strong>TestJ.java:</strong></p> <pre class="lang-java prettyprint-override"><code>import org.python.core.PyFrame; import org.python.core.PyFunct...
3
2013-04-11T00:48:51Z
[ "java", "python", "jython", "bytecode" ]
python unichr problem
2,583,425
<p>I've got some problem with <code>unichr()</code> on my server. Please see below:</p> <p>On my server (Ubuntu 9.04):</p> <pre><code>&gt;&gt;&gt; print unichr(255) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in ...
2
2010-04-06T08:07:31Z
2,583,436
<p>The terminal settings on your server are different, probably set to 7-bit US ASCII.</p>
2
2010-04-06T08:09:54Z
[ "python", "unicode", "ubuntu" ]
python unichr problem
2,583,425
<p>I've got some problem with <code>unichr()</code> on my server. Please see below:</p> <p>On my server (Ubuntu 9.04):</p> <pre><code>&gt;&gt;&gt; print unichr(255) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in ...
2
2010-04-06T08:07:31Z
2,583,445
<p>It's not really <code>unichr()</code> related. Problem is with locale setting in your server environment, as it's probably set to something like <code>en_US</code> and it's not unicode aware.</p>
2
2010-04-06T08:11:34Z
[ "python", "unicode", "ubuntu" ]
python unichr problem
2,583,425
<p>I've got some problem with <code>unichr()</code> on my server. Please see below:</p> <p>On my server (Ubuntu 9.04):</p> <pre><code>&gt;&gt;&gt; print unichr(255) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in ...
2
2010-04-06T08:07:31Z
2,583,558
<p>Consider using an explicit <a href="http://docs.python.org/library/stdtypes.html#str.encode" rel="nofollow">encoding</a> when printing unicode strings where OS settings are not uniform.</p> <blockquote> <p><code>unicode.encode([encoding[, errors]])</code></p> <p>Return an encoded version of the string. Defau...
1
2010-04-06T08:36:40Z
[ "python", "unicode", "ubuntu" ]
python unichr problem
2,583,425
<p>I've got some problem with <code>unichr()</code> on my server. Please see below:</p> <p>On my server (Ubuntu 9.04):</p> <pre><code>&gt;&gt;&gt; print unichr(255) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in ...
2
2010-04-06T08:07:31Z
2,584,296
<p>When using the "<code>print</code>" keyword, you'll be writing to the <code>sys.stdout</code> output stream. <code>sys.stdout</code> can usually only display Unicode strings if the characters can be converted to ascii using <code>str(message)</code>. </p> <p>You'll need to encode to your OS's terminal encoding when...
4
2010-04-06T11:08:27Z
[ "python", "unicode", "ubuntu" ]
How to draw text in a bitmap using wxpython?
2,583,549
<p>I want to draw a number centered inside a wx.EmptyBitmap.</p> <p>How can I do it using wxpython?</p> <p>Thanks in advance :)</p> <pre><code>import wx app = None class Size(wx.Frame): def __init__(self, parent, id, title): frame = wx.Frame.__init__(self, parent, id, title, size=(250, 200)) bm...
3
2010-04-06T08:35:03Z
2,583,619
<p>You use a <a href="http://www.wxpython.org/docs/api/wx.MemoryDC-class.html" rel="nofollow">wx.MemoryDC</a></p>
1
2010-04-06T08:49:39Z
[ "python", "wxpython" ]
How to draw text in a bitmap using wxpython?
2,583,549
<p>I want to draw a number centered inside a wx.EmptyBitmap.</p> <p>How can I do it using wxpython?</p> <p>Thanks in advance :)</p> <pre><code>import wx app = None class Size(wx.Frame): def __init__(self, parent, id, title): frame = wx.Frame.__init__(self, parent, id, title, size=(250, 200)) bm...
3
2010-04-06T08:35:03Z
2,583,774
<p>Select the bmp in a wx.MemoryDC, draw anything on that dc and then select that bitmap out e.g.</p> <pre><code>import wx app = None class Size(wx.Frame): def __init__(self, parent, id, title): frame = wx.Frame.__init__(self, parent, id, title, size=(250, 200)) w, h = 100, 100 bmp = wx.E...
5
2010-04-06T09:26:39Z
[ "python", "wxpython" ]
How to use thread in Django
2,583,567
<p>I want to check users' subscribed dates for certain period. And send mail to users whose subscription is finishing (ex. reminds two days). I think the best way is using thread and timer to check dates. But I have no idea how to call this function. I don't want to make a separate program or shell. I want to combine t...
0
2010-04-06T08:38:43Z
2,583,616
<p>That's case for <code>manage.py</code> command called periodically from cron. <a href="http://docs.djangoproject.com/en/1.1/howto/custom-management-commands/#howto-custom-management-commands" rel="nofollow">Oficial doc</a> about creating those commands. <a href="http://oebfare.com/blog/2008/nov/03/writing-custom-man...
3
2010-04-06T08:49:03Z
[ "python", "django", "multithreading" ]
How to use thread in Django
2,583,567
<p>I want to check users' subscribed dates for certain period. And send mail to users whose subscription is finishing (ex. reminds two days). I think the best way is using thread and timer to check dates. But I have no idea how to call this function. I don't want to make a separate program or shell. I want to combine t...
0
2010-04-06T08:38:43Z
2,583,775
<p>using <a href="http://code.google.com/p/django-cron/" rel="nofollow">Django-cron</a> is much easier and simple</p> <p><strong>EDIT: Added a tip</strong></p> <pre><code>from django_cron import cronScheduler, Job class sendMail(Job): # period run every 300 seconds (5 minutes) run_every = 300 ...
1
2010-04-06T09:27:04Z
[ "python", "django", "multithreading" ]
How to use thread in Django
2,583,567
<p>I want to check users' subscribed dates for certain period. And send mail to users whose subscription is finishing (ex. reminds two days). I think the best way is using thread and timer to check dates. But I have no idea how to call this function. I don't want to make a separate program or shell. I want to combine t...
0
2010-04-06T08:38:43Z
4,132,369
<p>if you need more then only this one asynchronous job have a look at <a href="http://celeryproject.org" rel="nofollow">celery</a>.</p>
2
2010-11-09T09:55:43Z
[ "python", "django", "multithreading" ]
Dynamically create class attributes
2,583,620
<p>I need to dynamically create class attributes from a DEFAULTS dictionary.</p> <pre><code>defaults = { 'default_value1':True, 'default_value2':True, 'default_value3':True, } class Settings(object): default_value1 = some_complex_init_function(defaults[default_value1], ...) default_value2 = some_c...
7
2010-04-06T08:49:58Z
2,583,635
<p>I think that's case for metaclass:</p> <pre><code>class SettingsMeta(type): def __new__(cls, name, bases, dct): for name, value in defaults.items(): dct[name] = some_complex_init_function(value, ...) return type.__new__(cls, name, bases, dct) class Settings(object): __metaclass_...
1
2010-04-06T08:54:47Z
[ "python", "class-attributes" ]
Dynamically create class attributes
2,583,620
<p>I need to dynamically create class attributes from a DEFAULTS dictionary.</p> <pre><code>defaults = { 'default_value1':True, 'default_value2':True, 'default_value3':True, } class Settings(object): default_value1 = some_complex_init_function(defaults[default_value1], ...) default_value2 = some_c...
7
2010-04-06T08:49:58Z
2,584,050
<p>You could do it without metaclasses using decorators. This way is a bit more clear IMO:</p> <pre><code>def apply_defaults(cls): defaults = { 'default_value1':True, 'default_value2':True, 'default_value3':True, } for name, value in defaults.items(): setattr(cls, name, some...
12
2010-04-06T10:16:43Z
[ "python", "class-attributes" ]
Dynamically create class attributes
2,583,620
<p>I need to dynamically create class attributes from a DEFAULTS dictionary.</p> <pre><code>defaults = { 'default_value1':True, 'default_value2':True, 'default_value3':True, } class Settings(object): default_value1 = some_complex_init_function(defaults[default_value1], ...) default_value2 = some_c...
7
2010-04-06T08:49:58Z
2,584,494
<p>When defining a class, the local namespace will be converted into the class namespace at the conclusion of the class body. As such, you can accomplish this with:</p> <pre><code>class Settings(object): for key, val in defaults.iteritems(): locals()[key] = some_complex_init_function(val, ...) </code></pr...
-1
2010-04-06T11:42:12Z
[ "python", "class-attributes" ]
Does wx.DC line has events?
2,583,646
<p>A line created using the DrawLine method of wx.DC has the onClick and onMouseOver events?</p>
0
2010-04-06T08:58:05Z
2,583,745
<p>No wx.DC just draw to a device and those are all plain pixels, wx.DC doesn't track mouse events or any other events.</p> <p>If you want such behavior you will have to track mouse movement on your drawing area and on click check which area it may have clicked e.g. if near by the line show a msg etc.</p>
0
2010-04-06T09:20:07Z
[ "python", "wxpython" ]
Formwizards for editing in Django
2,583,829
<p>I am in the process of making a webapp, and this webapp needs to have a form wizard. The wizard consists of 3 ModelForms, and it works flawlessly. But I need the second form to be a "edit form". That is, i need it to be a form that is passed an instance.</p> <p>How can you do this with a form wizard? How do you pas...
0
2010-04-06T09:37:47Z
2,584,179
<p>you can use formwizard class in a view and pass initial there, something like:</p> <pre><code>def edit(request): initial = {0: {'field1':'value1'}} return FormWizard([form, some_other_form], initial=initial) </code></pre> <p>where initial should be a dict with keys equal to steps and values are dicts of da...
0
2010-04-06T10:45:00Z
[ "python", "django", "django-formwizard" ]
Formwizards for editing in Django
2,583,829
<p>I am in the process of making a webapp, and this webapp needs to have a form wizard. The wizard consists of 3 ModelForms, and it works flawlessly. But I need the second form to be a "edit form". That is, i need it to be a form that is passed an instance.</p> <p>How can you do this with a form wizard? How do you pas...
0
2010-04-06T09:37:47Z
11,507,678
<p>In Django 1.4 you can do something like:</p> <pre><code>def edit(request): initial = { 0: {'subject': 'Hello', 'sender': 'user@example.com'}, 1: {'message': 'Hi there!'} } wiz = FormWizard([form1,form2,form3],initial_dict = initial) return wiz(request) </code></pre>
4
2012-07-16T15:39:56Z
[ "python", "django", "django-formwizard" ]
Formwizards for editing in Django
2,583,829
<p>I am in the process of making a webapp, and this webapp needs to have a form wizard. The wizard consists of 3 ModelForms, and it works flawlessly. But I need the second form to be a "edit form". That is, i need it to be a form that is passed an instance.</p> <p>How can you do this with a form wizard? How do you pas...
0
2010-04-06T09:37:47Z
13,057,749
<p>I think there's a few approaches, depending how complicated the data you have to pass is.</p> <p>You can follow the instructions here: <a href="https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/#providing-initial-data-for-the-forms" rel="nofollow">https://docs.djangoproject.com/en/dev/ref/cont...
0
2012-10-24T21:07:19Z
[ "python", "django", "django-formwizard" ]
Get Django form field from model field
2,584,141
<p>I'd like to create a form that includes fields from two separate models, along with some other regular (non-model) fields. The form will create an instance of each model. I don't <em>think</em> I can use inline formsets for this, since I don't want to include all the fields from both models.</p> <p>I'd like to crea...
11
2010-04-06T10:36:28Z
2,584,972
<p>You also can take a look to django.forms.models.fields_for_model That should give you a dictionary of fields, and then you can add the fields of the form</p>
10
2010-04-06T13:08:24Z
[ "python", "django", "django-models", "django-forms" ]
Get Django form field from model field
2,584,141
<p>I'd like to create a form that includes fields from two separate models, along with some other regular (non-model) fields. The form will create an instance of each model. I don't <em>think</em> I can use inline formsets for this, since I don't want to include all the fields from both models.</p> <p>I'd like to crea...
11
2010-04-06T10:36:28Z
2,587,581
<p>You should never have to build the fields yourself unless you want some special behavior.</p> <p>This should be as simple as using two <code>ModelForm</code>s and an extra <code>Form</code> inside one <code>&lt;form&gt;</code> tag in your template with one submit button.</p> <p>in forms.py:</p> <pre><code>class M...
8
2010-04-06T19:16:07Z
[ "python", "django", "django-models", "django-forms" ]
Get Django form field from model field
2,584,141
<p>I'd like to create a form that includes fields from two separate models, along with some other regular (non-model) fields. The form will create an instance of each model. I don't <em>think</em> I can use inline formsets for this, since I don't want to include all the fields from both models.</p> <p>I'd like to crea...
11
2010-04-06T10:36:28Z
10,148,369
<p>Another solution can be to create one 'uber'-form that aggregates the concrete modelforms. The form supports the methods that a form normally provides and it forward them to all the child forms. Some will be simple, other complicated. The big advantage of that approach is that no code beyond the form is affected (c...
1
2012-04-13T21:11:15Z
[ "python", "django", "django-models", "django-forms" ]
Get Django form field from model field
2,584,141
<p>I'd like to create a form that includes fields from two separate models, along with some other regular (non-model) fields. The form will create an instance of each model. I don't <em>think</em> I can use inline formsets for this, since I don't want to include all the fields from both models.</p> <p>I'd like to crea...
11
2010-04-06T10:36:28Z
35,064,439
<p>There is now a <a href="https://docs.djangoproject.com/en/1.9/ref/models/meta/#django.db.models.options.Options.get_field" rel="nofollow">documented API</a> for getting the model field from a model class:</p> <pre><code>my_model_field = MyModel._meta.get_field('my_model_field_name') </code></pre> <p>Although it's ...
1
2016-01-28T14:41:05Z
[ "python", "django", "django-models", "django-forms" ]
undefined symbol: PyUnicodeUCS2_Decode whilst trying to install psycopg2
2,584,198
<p>I'm getting an error whilst trying to install psycopg2 on ubuntu 9.10 64 bit.</p> <p>The error is:</p> <pre><code>&gt;&gt;&gt; import psycopg2 Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "psycopg2/__init__.py", line 69, in &lt;module&gt; from _psycopg import BINA...
3
2010-04-06T10:47:28Z
2,584,248
<p>Like you say, your Python has been built UCS4. But the psycopg2 you're trying to install has been built against a Python built <em>UCS2</em>. Find one or build one against UCS4.</p>
4
2010-04-06T10:56:28Z
[ "python", "psycopg2" ]
undefined symbol: PyUnicodeUCS2_Decode whilst trying to install psycopg2
2,584,198
<p>I'm getting an error whilst trying to install psycopg2 on ubuntu 9.10 64 bit.</p> <p>The error is:</p> <pre><code>&gt;&gt;&gt; import psycopg2 Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "psycopg2/__init__.py", line 69, in &lt;module&gt; from _psycopg import BINA...
3
2010-04-06T10:47:28Z
2,584,554
<p>fixed it.</p> <p>I had to recompile python with --enable-unicode=ucs4. </p> <p>Still not clear why as the default version on ubuntu uses ucs4.</p>
6
2010-04-06T11:53:26Z
[ "python", "psycopg2" ]
Python Windows File Copy with Wildcard Support
2,584,414
<p>I've been doing this all the time: </p> <pre><code>result = subprocess.call(['copy', '123*.xml', 'out_folder\\.', '/y']) if result == 0: do_something() else: do_something_else() </code></pre> <p>Until today I started to look into pywin32 modules, then I saw functions like win32file.CopyFiles(), but then ...
9
2010-04-06T11:29:34Z
2,584,428
<p>Look at <a href="http://docs.python.org/library/glob.html" rel="nofollow">glob</a> and <a href="http://docs.python.org/library/shutil.html" rel="nofollow">shutil</a> before going any further.</p> <p>Forking a subprocess to copy a file is relatively inefficient. Using <code>shutil</code> will be more efficient.</p...
3
2010-04-06T11:31:39Z
[ "python", "file", "wildcard", "pywin32" ]
Python Windows File Copy with Wildcard Support
2,584,414
<p>I've been doing this all the time: </p> <pre><code>result = subprocess.call(['copy', '123*.xml', 'out_folder\\.', '/y']) if result == 0: do_something() else: do_something_else() </code></pre> <p>Until today I started to look into pywin32 modules, then I saw functions like win32file.CopyFiles(), but then ...
9
2010-04-06T11:29:34Z
2,584,474
<p>The following code provides a portable implementation. </p> <p>Note that I'm using iglob (added in Python 2.5) which creates a generator, so it does not load the entire list of files in memory first (which is what glob does).</p> <pre><code>from glob import iglob from shutil import copy from os.path import join d...
10
2010-04-06T11:39:25Z
[ "python", "file", "wildcard", "pywin32" ]
Python Windows File Copy with Wildcard Support
2,584,414
<p>I've been doing this all the time: </p> <pre><code>result = subprocess.call(['copy', '123*.xml', 'out_folder\\.', '/y']) if result == 0: do_something() else: do_something_else() </code></pre> <p>Until today I started to look into pywin32 modules, then I saw functions like win32file.CopyFiles(), but then ...
9
2010-04-06T11:29:34Z
2,584,528
<p>The below example is fairly naive - doesn't do any checking if something goes wrong, and doesn't create any directories, but might do what you want:</p> <pre><code>import glob import shutil for path in glob.iglob('123*.xml'): shutil.copy(path, 'out_folder/%s' % path) </code></pre> <p>See also: <a href="http:/...
7
2010-04-06T11:47:30Z
[ "python", "file", "wildcard", "pywin32" ]
Python Windows File Copy with Wildcard Support
2,584,414
<p>I've been doing this all the time: </p> <pre><code>result = subprocess.call(['copy', '123*.xml', 'out_folder\\.', '/y']) if result == 0: do_something() else: do_something_else() </code></pre> <p>Until today I started to look into pywin32 modules, then I saw functions like win32file.CopyFiles(), but then ...
9
2010-04-06T11:29:34Z
2,584,576
<blockquote> <p>"glob" is incredibly slow if there are many files.</p> </blockquote> <p>glob is slow is there are a lot of results because it returns one huge list of all the results in a "big bang" approach. If there are a lot of results this will use a lot of memory and take a long time.</p> <p>Use iglob instea...
1
2010-04-06T11:57:28Z
[ "python", "file", "wildcard", "pywin32" ]
Python Windows File Copy with Wildcard Support
2,584,414
<p>I've been doing this all the time: </p> <pre><code>result = subprocess.call(['copy', '123*.xml', 'out_folder\\.', '/y']) if result == 0: do_something() else: do_something_else() </code></pre> <p>Until today I started to look into pywin32 modules, then I saw functions like win32file.CopyFiles(), but then ...
9
2010-04-06T11:29:34Z
2,584,602
<pre><code>import os import shutil path=os.path.join("/home","mypath") destination=os.path.join("/destination","dir") for r,d,f in os.walk(path): for files in f: if files.endswith(".xml"): try: shutil.copy(os.path.join(r,files) , destination) except...
1
2010-04-06T12:02:49Z
[ "python", "file", "wildcard", "pywin32" ]
Search jpeg files using python
2,584,589
<p>My requirement is to search for jpeg images files in a directory using python script and list the file names. Can anyone help me on how to identify jpeg images files.</p> <p>Thanks in advance...</p>
1
2010-04-06T11:59:44Z
2,584,619
<p>Use the <code>magic</code> module to get the MIME type, and look for <code>image/jpeg</code>.</p>
2
2010-04-06T12:05:02Z
[ "python", "search", "file" ]
Search jpeg files using python
2,584,589
<p>My requirement is to search for jpeg images files in a directory using python script and list the file names. Can anyone help me on how to identify jpeg images files.</p> <p>Thanks in advance...</p>
1
2010-04-06T11:59:44Z
2,584,636
<pre><code>import os path=os.path.join("/home","mypath","to_search") for r,d,f in os.walk(path): for files in f: if files[-3:].lower()=='jpg' of files[-4:].lower() =="jpeg": print "found: ",os.path.join(r,files) </code></pre>
1
2010-04-06T12:07:37Z
[ "python", "search", "file" ]
Search jpeg files using python
2,584,589
<p>My requirement is to search for jpeg images files in a directory using python script and list the file names. Can anyone help me on how to identify jpeg images files.</p> <p>Thanks in advance...</p>
1
2010-04-06T11:59:44Z
2,584,674
<p>If you need to search a single folder non-recursively you can simply do</p> <pre><code>&gt;&gt;&gt; import glob &gt;&gt;&gt; glob.glob("D:\\bluetooth\*.jpg") ['D:\\bluetooth\\Image1475.jpg', 'D:\\bluetooth\\Image1514.jpg'] </code></pre> <p>Read more about <a href="http://docs.python.org/library/glob.html" rel="no...
8
2010-04-06T12:14:22Z
[ "python", "search", "file" ]
Search jpeg files using python
2,584,589
<p>My requirement is to search for jpeg images files in a directory using python script and list the file names. Can anyone help me on how to identify jpeg images files.</p> <p>Thanks in advance...</p>
1
2010-04-06T11:59:44Z
2,584,681
<p>If you want to scan subfolders:</p> <pre><code>import os for root, subdirs, files in os.walk(DIRECTORY): for file in files: if os.path.splitext(file)[1].lower() in ('.jpg', '.jpeg'): print os.path.join(root, file) </code></pre> <p>Otherwise, using one of the other glob functions in the ot...
5
2010-04-06T12:15:23Z
[ "python", "search", "file" ]
Search jpeg files using python
2,584,589
<p>My requirement is to search for jpeg images files in a directory using python script and list the file names. Can anyone help me on how to identify jpeg images files.</p> <p>Thanks in advance...</p>
1
2010-04-06T11:59:44Z
2,584,711
<p>If you want to determine the image format by file contents, you can use the <a href="http://www.pythonware.com/products/pil/" rel="nofollow">Python Imaging Library</a>:</p> <pre><code>import Image try: img = Image.open('maybe_jpeg_file') print img.format # Will return 'JPEG' for JPEG files. except IOError: ...
1
2010-04-06T12:19:52Z
[ "python", "search", "file" ]
Building a python module and linking it against a MacOSX framework
2,584,595
<p>I'm trying to build a Python extension on MacOSX 10.6 and to link it against several frameworks (i386 only). I made a setup.py file, using distutils and the Extension object.</p> <p>I order to link against my frameworks, my <strong>LDFLAGS</strong> env var should look like :</p> <pre><code>LDFLAGS = -lc -arch i386...
12
2010-04-06T12:01:41Z
2,584,854
<p>It seems that my framework is compiled for ppc and i386 but not x86_64 :</p> <pre><code>$ file /Library/Frameworks/fwk1.framework/Versions/A/fwk1 /Library/Frameworks/fwk1.framework/Versions/A/fwk1: Mach-O universal binary with 2 architectures /Library/Frameworks/fwk1.framework/Versions/A/fwk1 (for architecture ppc...
1
2010-04-06T12:47:20Z
[ "c++", "python", "osx", "dynamic-linking" ]
Building a python module and linking it against a MacOSX framework
2,584,595
<p>I'm trying to build a Python extension on MacOSX 10.6 and to link it against several frameworks (i386 only). I made a setup.py file, using distutils and the Extension object.</p> <p>I order to link against my frameworks, my <strong>LDFLAGS</strong> env var should look like :</p> <pre><code>LDFLAGS = -lc -arch i386...
12
2010-04-06T12:01:41Z
2,588,890
<p>I am not sure I understand what you are trying to do and your desired outcome but perhaps this will help. Because C extension modules are normally run within the execution context of the Python interpreter, extension modules have to be built to be compatible with the interpreter. On OS X, Python and distutils go t...
3
2010-04-06T22:54:35Z
[ "c++", "python", "osx", "dynamic-linking" ]
Building a python module and linking it against a MacOSX framework
2,584,595
<p>I'm trying to build a Python extension on MacOSX 10.6 and to link it against several frameworks (i386 only). I made a setup.py file, using distutils and the Extension object.</p> <p>I order to link against my frameworks, my <strong>LDFLAGS</strong> env var should look like :</p> <pre><code>LDFLAGS = -lc -arch i386...
12
2010-04-06T12:01:41Z
6,134,675
<p>Although long after the dust has settled, having the same question myself I dug around a little bit and found this:</p> <p>/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/sysconfig.py</p> <pre><code> if 'ARCHFLAGS' in os.environ: archflags = os.environ['ARCHFLAGS'...
3
2011-05-26T06:40:30Z
[ "c++", "python", "osx", "dynamic-linking" ]
Building a python module and linking it against a MacOSX framework
2,584,595
<p>I'm trying to build a Python extension on MacOSX 10.6 and to link it against several frameworks (i386 only). I made a setup.py file, using distutils and the Extension object.</p> <p>I order to link against my frameworks, my <strong>LDFLAGS</strong> env var should look like :</p> <pre><code>LDFLAGS = -lc -arch i386...
12
2010-04-06T12:01:41Z
8,042,155
<p>I just ran into this myself. I had to bypass distutils, because they appear to hard-code the -undefined dynamic_lookup. Here is the Makefile I'm using to emulate distutils:</p> <pre><code>CC = gcc CFLAGS = -pipe -std=c99 -fno-strict-aliasing -fno-common -dynamic -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -D...
0
2011-11-07T20:23:59Z
[ "c++", "python", "osx", "dynamic-linking" ]
Building a python module and linking it against a MacOSX framework
2,584,595
<p>I'm trying to build a Python extension on MacOSX 10.6 and to link it against several frameworks (i386 only). I made a setup.py file, using distutils and the Extension object.</p> <p>I order to link against my frameworks, my <strong>LDFLAGS</strong> env var should look like :</p> <pre><code>LDFLAGS = -lc -arch i386...
12
2010-04-06T12:01:41Z
8,719,066
<p>This has nothing to do with the undefined dynamic_lookup but all with distutils. It appends the extra_link_flags to the link flags it chooses for python building. Instead it should prepend it because the -framework listings must come before the objects that use them on the cmdline (AFAIK this is due how gcc gathers ...
4
2012-01-03T21:46:56Z
[ "c++", "python", "osx", "dynamic-linking" ]
Handling import errors when using doctest
2,584,780
<p>every now and then when I code in Python, I have to do without certain third-party modules. </p> <p>Eg. when I'm writing user authentication, it can be done in several ways and one of them is by using LDAP. However if the user does not want to use LDAP auth., he can choose a different option in a config file and in...
1
2010-04-06T12:33:02Z
2,584,845
<p>The recommended way is</p> <pre><code>try: import foo as auth except ImportError: import bar as auth </code></pre> <p>It avoids race conditions and I don't think it looks bad.</p>
1
2010-04-06T12:45:18Z
[ "python", "doctest" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
2,584,947
<p>With BeautifulSoup:</p> <pre><code>from BeautifulSoup import BeautifulSoup ''.join(BeautifulSoup(page).findAll(text=True)) </code></pre> <p>Found at <a href="http://www.ghastlyfop.com/blog/2008/12/strip-html-tags-from-string-python.html">http://www.ghastlyfop.com/blog/2008/12/strip-html-tags-from-string-python...
5
2010-04-06T13:04:26Z
[ "python" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
2,584,952
<p>Try with:</p> <pre><code>import re input = 'this is test &lt;b&gt; bold text &lt;/b&gt; normal text' output = re.compile(r'&lt;[^&lt;]*?/?&gt;').sub('', input) print output </code></pre>
2
2010-04-06T13:04:58Z
[ "python" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
2,584,959
<p>If you don't mind Python (although regexps are fairly generic), you can take some inspiration from <a href="http://code.djangoproject.com/browser/django/trunk/django/utils/html.py#L57" rel="nofollow">Django's strip_tags filter</a>.</p> <p>Reproduced here for completeness -</p> <pre><code>def strip_tags(value): ...
3
2010-04-06T13:05:55Z
[ "python" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
2,584,980
<p>Looks like you want <code>HTMLParser</code>. (<code>html.parser</code> in Python 3.)</p> <pre><code>from HTMLParser import HTMLParser from sys import stdout class Filter(HTMLParser): def __init__(self, ignored_tags): super(Filter, self).__init__() self.ignorelevel = 0 self. ignored_tags ...
2
2010-04-06T13:09:38Z
[ "python" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
2,585,013
<p>I would use <a href="http://code.google.com/p/html5lib/" rel="nofollow">http://code.google.com/p/html5lib/</a> if you want to include some safe tags.</p> <p>See the "Sanitizing Tokenizer" section at <a href="http://code.google.com/p/html5lib/wiki/UserDocumentation" rel="nofollow">http://code.google.com/p/html5lib/w...
0
2010-04-06T13:14:50Z
[ "python" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
2,585,065
<p>Solution using <code>BeautifulSoup</code>:</p> <pre><code>from BeautifulSoup import BeautifulSoup def removeTag(soup, tagname): for tag in soup.findAll(tagname): contents = tag.contents parent = tag.parent tag.extract() s = BeautifulSoup("abcd &lt;b&gt; btag &lt;/b&gt; hello &lt;d&gt;dt...
9
2010-04-06T13:21:29Z
[ "python" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
2,585,294
<p>Sam's answer should do what's wanted fairly well as far as I can tell, but it may pay to make sure that any left over &lt;&gt; characters are replaced with &amp;lt; and &amp;gt; respectively to prevent misuse/invalid HTML. </p> <p>This approach has the advantage that it can accept incredibly malformed HTML referenc...
0
2010-04-06T13:51:31Z
[ "python" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
2,585,391
<p>This is working code taken from my project <a href="http://sf.net/projects/supybot/" rel="nofollow">Supybot</a>, so it's fairly well tested:</p> <pre>class HtmlToText(sgmllib.SGMLParser): """Taken from some eff-bot code on c.l.p.""" entitydefs = htmlentitydefs.entitydefs.copy() entitydefs['nbsp'] = ' ' ...
0
2010-04-06T14:06:05Z
[ "python" ]
strip tags python
2,584,885
<p>i want the following functionality.</p> <pre><code>input : this is test &lt;b&gt; bold text &lt;/b&gt; normal text expected output: this is test normal text </code></pre> <p>i.e. remove the content of the specified tag</p>
8
2010-04-06T12:52:27Z
27,345,958
<p>Use the webob.exc module:</p> <pre><code>from webob.exc import strip_tags </code></pre> <p>And then use it:</p> <pre><code>print strip_tags('a&lt;br/&gt;b') &gt;&gt; ab </code></pre>
0
2014-12-07T18:07:02Z
[ "python" ]
Emacs bulk indent for Python
2,585,091
<p>Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you indent the whole block at once.</p> <p>I am not an experienced Emacs user, but just find it is the best tool for working through ssh. I am usin...
88
2010-04-06T13:24:47Z
2,585,123
<p><code>indent-region</code> mapped to <code>C-M-\</code> should do the trick.</p>
4
2010-04-06T13:28:20Z
[ "python", "emacs", "ssh" ]
Emacs bulk indent for Python
2,585,091
<p>Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you indent the whole block at once.</p> <p>I am not an experienced Emacs user, but just find it is the best tool for working through ssh. I am usin...
88
2010-04-06T13:24:47Z
2,585,268
<p>In addition to <code>indent-region</code>, which is mapped to <code>C-M-\</code> by default, the rectangle edit commands are very useful for Python. Mark a region as normal, then:</p> <ul> <li><code>C-x r t</code> (<code>string-rectangle</code>): will prompt you for characters you'd like to insert into each line; ...
8
2010-04-06T13:47:58Z
[ "python", "emacs", "ssh" ]
Emacs bulk indent for Python
2,585,091
<p>Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you indent the whole block at once.</p> <p>I am not an experienced Emacs user, but just find it is the best tool for working through ssh. I am usin...
88
2010-04-06T13:24:47Z
2,585,406
<p>If you are programming Python using Emacs, then you should probably be using python-mode. With python-mode, after marking the block of code,</p> <p><code>C-c &gt;</code> or <code>C-c C-l</code> shifts the region 4 spaces to the right</p> <p><code>C-c &lt;</code> or <code>C-c C-r</code> shifts the region 4 spaces...
134
2010-04-06T14:08:32Z
[ "python", "emacs", "ssh" ]
Emacs bulk indent for Python
2,585,091
<p>Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you indent the whole block at once.</p> <p>I am not an experienced Emacs user, but just find it is the best tool for working through ssh. I am usin...
88
2010-04-06T13:24:47Z
2,585,572
<p>I do something like this universally</p> <pre><code>;; intent whole buffer (defun iwb () "indent whole buffer" (interactive) ;;(delete-trailing-whitespace) (indent-region (point-min) (point-max) nil) (untabify (point-min) (point-max))) </code></pre>
-2
2010-04-06T14:28:40Z
[ "python", "emacs", "ssh" ]
Emacs bulk indent for Python
2,585,091
<p>Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you indent the whole block at once.</p> <p>I am not an experienced Emacs user, but just find it is the best tool for working through ssh. I am usin...
88
2010-04-06T13:24:47Z
2,594,446
<p>I've been using this function to handle my indenting and unindenting:</p> <pre><code>(defun unindent-dwim (&amp;optional count-arg) "Keeps relative spacing in the region. Unindents to the next multiple of the current tab-width" (interactive) (let ((deactivate-mark nil) (beg (or (and mark-active (regi...
1
2010-04-07T17:08:30Z
[ "python", "emacs", "ssh" ]
Emacs bulk indent for Python
2,585,091
<p>Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you indent the whole block at once.</p> <p>I am not an experienced Emacs user, but just find it is the best tool for working through ssh. I am usin...
88
2010-04-06T13:24:47Z
31,296,026
<p>I'm an Emacs newb, so this answer it probably bordering on useless.</p> <p>None of the answers mentioned so far cover re-indentation of literals like <code>dict</code> or <code>list</code>. E.g. <code>M-x indent-region</code> or <code>M-x python-indent-shift-right</code> and company aren't going to help if you've c...
0
2015-07-08T14:42:42Z
[ "python", "emacs", "ssh" ]
Sending custom PyQt signals?
2,585,442
<p>I'm practicing PyQt and (Q)threads by making a simple Twitter client. I have two Qthreads.</p> <ol> <li><p>Main/GUI thread.</p></li> <li><p>Twitter fetch thread - fetches data from Twitter every X minutes.</p></li> </ol> <p>So, every X minutes my Twitter thread downloads a new set of status updates (a Python list)...
23
2010-04-06T14:14:20Z
2,585,563
<p>(Py)Qt signals and slots work cross-threads just the same as in a single thread. So there's nothing really special to set up:</p> <p>Define a slot (method) in the main thread, and connect the thread's signal to this slot (the connection shall be also done in the main thread). Then, in the thread, when you want to, ...
3
2010-04-06T14:28:06Z
[ "python", "pyqt", "pyqt4", "qthread" ]
Sending custom PyQt signals?
2,585,442
<p>I'm practicing PyQt and (Q)threads by making a simple Twitter client. I have two Qthreads.</p> <ol> <li><p>Main/GUI thread.</p></li> <li><p>Twitter fetch thread - fetches data from Twitter every X minutes.</p></li> </ol> <p>So, every X minutes my Twitter thread downloads a new set of status updates (a Python list)...
23
2010-04-06T14:14:20Z
2,586,512
<p>Check out this <a href="http://stackoverflow.com/questions/569650/how-to-keep-track-of-thread-progress-in-python-without-freezing-the-pyqt-gui">question I asked</a> a while back. There is a code example that might help you figure out what you need to do.</p> <p>What you said about registering your signal makes me ...
7
2010-04-06T16:27:20Z
[ "python", "pyqt", "pyqt4", "qthread" ]
Sending custom PyQt signals?
2,585,442
<p>I'm practicing PyQt and (Q)threads by making a simple Twitter client. I have two Qthreads.</p> <ol> <li><p>Main/GUI thread.</p></li> <li><p>Twitter fetch thread - fetches data from Twitter every X minutes.</p></li> </ol> <p>So, every X minutes my Twitter thread downloads a new set of status updates (a Python list)...
23
2010-04-06T14:14:20Z
2,587,315
<p>From this example:</p> <p><a href="http://doc.qt.digia.com/4.5/qmetatype.html" rel="nofollow">http://doc.qt.digia.com/4.5/qmetatype.html</a></p> <pre><code> int id = QMetaType.type("MyClass"); </code></pre> <p>You can write down in Python</p> <pre><code>from PyQt4 import QtCore id = QtCore.QMetaType.type('My...
6
2010-04-06T18:30:02Z
[ "python", "pyqt", "pyqt4", "qthread" ]
Sending custom PyQt signals?
2,585,442
<p>I'm practicing PyQt and (Q)threads by making a simple Twitter client. I have two Qthreads.</p> <ol> <li><p>Main/GUI thread.</p></li> <li><p>Twitter fetch thread - fetches data from Twitter every X minutes.</p></li> </ol> <p>So, every X minutes my Twitter thread downloads a new set of status updates (a Python list)...
23
2010-04-06T14:14:20Z
2,595,607
<p>You can also do this, which is much more pythonic (and readable!).</p> <pre><code># create a signal equivalent to "void someSignal(int, QWidget)" someSignal = QtCore.pyqtSignal(int, QtGui.QWidget) # define a slot with the same signature @QtCore.pyqtSlot(int, QtGui.QWidget) def someSlot(status, source): pass #...
23
2010-04-07T20:15:58Z
[ "python", "pyqt", "pyqt4", "qthread" ]
Sending custom PyQt signals?
2,585,442
<p>I'm practicing PyQt and (Q)threads by making a simple Twitter client. I have two Qthreads.</p> <ol> <li><p>Main/GUI thread.</p></li> <li><p>Twitter fetch thread - fetches data from Twitter every X minutes.</p></li> </ol> <p>So, every X minutes my Twitter thread downloads a new set of status updates (a Python list)...
23
2010-04-06T14:14:20Z
5,186,812
<p>When you're using these old-style signals/slots in PyQt, there's actually no need to declare types at all. These should work:</p> <pre><code>QtCore.QObject.connect(self.twit_in, QtCore.SIGNAL("newStatuses"), self.update_tweet_list) ... self.emit(SIGNAL("newStatuses"), statuse...
5
2011-03-03T21:31:49Z
[ "python", "pyqt", "pyqt4", "qthread" ]
How can I monitor mouse events with Python Xlib instead of capture them?
2,585,647
<p>I need to monitor and filter mouse events with Xlib in Python.</p> <p>So far I have found out that this code receives events, but does not pass them on, so I can't actually do anything with the mouse anymore.</p> <pre><code>from Xlib.display import Display from Xlib import X display = Display(':0') root = display...
1
2010-04-06T14:37:19Z
2,622,914
<p>The answer seemed to be to use Xlib with RECORD, the result can be seen here: <a href="http://github.com/pepijndevos/PyMouse/blob/master/unix.py#L38" rel="nofollow">http://github.com/pepijndevos/PyMouse/blob/master/unix.py#L38</a></p>
0
2010-04-12T14:47:53Z
[ "python", "event-handling", "xlib" ]