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 having trouble running code
2,445,093
<p>For some reason this code will not seem to run in the interpreter. When I hit F5 nothing happens, not even the debugger seems to recognize it. I assume it has something to do with the class, as when removed the interpreter seems to recognize the rest of the code. Please tell me what I am doing wrong. Edit: I have re...
-1
2010-03-15T04:33:24Z
2,445,278
<p>I wonder if you're just having trouble working with IDLE. You mentioned elsewhere that the debugger just says "None". When I select "Debugger" from the IDLE interpreter window Debug menu, it pops up another window with the title "Debug Control", and it says "None" in that window.</p> <p>That window is just a contro...
1
2010-03-15T05:28:13Z
[ "python" ]
Python beginner having trouble running code
2,445,093
<p>For some reason this code will not seem to run in the interpreter. When I hit F5 nothing happens, not even the debugger seems to recognize it. I assume it has something to do with the class, as when removed the interpreter seems to recognize the rest of the code. Please tell me what I am doing wrong. Edit: I have re...
-1
2010-03-15T04:33:24Z
2,445,294
<p>The <code>+=</code> instead on <code>=</code> at line 44 that others have mentioned</p> <p>Then: Inside your class where you have <strong>my</strong>.sort(.....) you should have <strong>self</strong>.sort(.....) -- there are three places to fix.</p> <p>Then it falls over on line 8:</p> <p><code>carry, leave = []<...
0
2010-03-15T05:31:25Z
[ "python" ]
Python beginner having trouble running code
2,445,093
<p>For some reason this code will not seem to run in the interpreter. When I hit F5 nothing happens, not even the debugger seems to recognize it. I assume it has something to do with the class, as when removed the interpreter seems to recognize the rest of the code. Please tell me what I am doing wrong. Edit: I have re...
-1
2010-03-15T04:33:24Z
2,445,311
<p>There are too many errors in your code. Write down in plain English your requirements and start from scratch.</p> <p>What are you trying to do? Is it:</p> <pre><code>x = ['ac', 'bc' ,'ab', 'da'] order = ['d', 'b', 'c', 'a'] assert len(order) == len(x) x = [item for o, item in sorted(zip(order, x))] print x # -&gt;...
0
2010-03-15T05:36:31Z
[ "python" ]
How does Python differentiate between the different data types?
2,445,193
<p>Sorry if this is quite noobish to you, but I'm just starting out to learn Python after learning C++ &amp; Java, and I am wondering how in the world I could just declare variables like <code>id = 0</code> and <code>name = 'John'</code> without any <code>int</code>'s or <code>string</code>'s in front! I figured out th...
5
2010-03-15T05:04:45Z
2,445,203
<p>Python uses the duck-typing method - if it walks, looks and quacks like a duck, then it's a duck. If you pass in a string, and try to do something numerical on it, then it will fail.</p> <p>Have a look at: <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29#Typing" rel="nofollow">http://en.wikip...
6
2010-03-15T05:07:40Z
[ "python" ]
How does Python differentiate between the different data types?
2,445,193
<p>Sorry if this is quite noobish to you, but I'm just starting out to learn Python after learning C++ &amp; Java, and I am wondering how in the world I could just declare variables like <code>id = 0</code> and <code>name = 'John'</code> without any <code>int</code>'s or <code>string</code>'s in front! I figured out th...
5
2010-03-15T05:04:45Z
2,445,220
<p>When it comes to assigning literal values to variables, the type of the literal value can be inferred at the time of lexical analysis. For example, anything matching the regular expression <code>(-)?[1-9][0-9]*</code> can be inferred to be an integer literal. If you want to convert it to a float, there needs to be...
4
2010-03-15T05:12:29Z
[ "python" ]
How does Python differentiate between the different data types?
2,445,193
<p>Sorry if this is quite noobish to you, but I'm just starting out to learn Python after learning C++ &amp; Java, and I am wondering how in the world I could just declare variables like <code>id = 0</code> and <code>name = 'John'</code> without any <code>int</code>'s or <code>string</code>'s in front! I figured out th...
5
2010-03-15T05:04:45Z
2,445,233
<p>The literal objects you mention carry (pointers to;-) their own types with them of course, so when a name's bound to that object the problem of type doesn't arise -- the object always has a type, the name doesn't -- just delegates that to the object it's bound to.</p> <p>There's no "figuring out" in <code>def incre...
13
2010-03-15T05:15:45Z
[ "python" ]
How does Python differentiate between the different data types?
2,445,193
<p>Sorry if this is quite noobish to you, but I'm just starting out to learn Python after learning C++ &amp; Java, and I am wondering how in the world I could just declare variables like <code>id = 0</code> and <code>name = 'John'</code> without any <code>int</code>'s or <code>string</code>'s in front! I figured out th...
5
2010-03-15T05:04:45Z
2,445,238
<p>In Python, Unlike in C++ and Java, numbers and strings are both objects. So this:</p> <pre><code> id = 0 name = 'John' </code></pre> <p>is equivalent to:</p> <pre><code> id = int(0) name = str('John') </code></pre> <p>Since variables <code>id</code> and <code>name</code> are references that may addres...
0
2010-03-15T05:16:32Z
[ "python" ]
How does Python differentiate between the different data types?
2,445,193
<p>Sorry if this is quite noobish to you, but I'm just starting out to learn Python after learning C++ &amp; Java, and I am wondering how in the world I could just declare variables like <code>id = 0</code> and <code>name = 'John'</code> without any <code>int</code>'s or <code>string</code>'s in front! I figured out th...
5
2010-03-15T05:04:45Z
2,445,239
<p>Python is <em>dynamically typed</em>: all variables can refer to an object of any type. <code>id</code> and <code>name</code> can be anything, but the actual objects are of types like <code>int</code> and <code>str</code>. <code>0</code> is a literal that is parsed to make an <code>int</code> object, and <code>'John...
7
2010-03-15T05:16:34Z
[ "python" ]
How to get the list of price offers on an item from Amazon with python-amazon-product-api item_lookup function?
2,445,420
<p>I am trying to write a function to get a list of offers (their prices) for an item based on the ASIN:</p> <pre><code>def price_offers(asin): from amazonproduct import API, ResultPaginator, AWSError from config import AWS_KEY, SECRET_KEY api = API(AWS_KEY, SECRET_KEY, 'de') str_asin = str(asin) n...
3
2010-03-15T06:16:34Z
2,448,539
<p>Seems like there is no Offer element in your response. Try</p> <pre><code>node = api.item_lookup(...) from lxml import etree print etree.tostring(node, pretty_print=True) </code></pre> <p>to see how the returned XML looks like.</p>
6
2010-03-15T16:01:47Z
[ "python", "api", "amazon-web-services", "amazon" ]
How to get the list of price offers on an item from Amazon with python-amazon-product-api item_lookup function?
2,445,420
<p>I am trying to write a function to get a list of offers (their prices) for an item based on the ASIN:</p> <pre><code>def price_offers(asin): from amazonproduct import API, ResultPaginator, AWSError from config import AWS_KEY, SECRET_KEY api = API(AWS_KEY, SECRET_KEY, 'de') str_asin = str(asin) n...
3
2010-03-15T06:16:34Z
2,449,282
<p>OK, thanks. To anwser my own question for others who might have the same problem, the right way to do the above is:</p> <pre><code>def price_offers(asin): from amazonproduct import API, ResultPaginator, AWSError from config import AWS_KEY, SECRET_KEY api = API(AWS_KEY, SECRET_KEY, 'de') str_asin = s...
5
2010-03-15T17:53:55Z
[ "python", "api", "amazon-web-services", "amazon" ]
Django migrations--is it possible to use South in the middle of the project?
2,445,761
<p>I already started a project, and the models are all synced and everything.</p>
5
2010-03-15T08:07:00Z
2,445,802
<p>Yes. I think it is not too late. I've moved to south in a middle of a project and I am happy with that choice. I think it is a big help for deployment.</p> <p>The initialization of the south app can be done at any moment.</p>
4
2010-03-15T08:21:12Z
[ "python", "mysql", "database", "django", "django-south" ]
Django migrations--is it possible to use South in the middle of the project?
2,445,761
<p>I already started a project, and the models are all synced and everything.</p>
5
2010-03-15T08:07:00Z
2,445,833
<p>It's even mentioned in docs:</p> <p><a href="http://south.aeracode.org/wiki/QuickStartGuide#a1.SetupeveryapplicationtobetrackablebySouth" rel="nofollow">http://south.aeracode.org/wiki/QuickStartGuide#a1.SetupeveryapplicationtobetrackablebySouth</a></p>
4
2010-03-15T08:30:52Z
[ "python", "mysql", "database", "django", "django-south" ]
Django migrations--is it possible to use South in the middle of the project?
2,445,761
<p>I already started a project, and the models are all synced and everything.</p>
5
2010-03-15T08:07:00Z
3,287,739
<p>It's quite straight forward to start using South.</p> <p>Just follow the <a href="http://south.aeracode.org/docs/installation.html" rel="nofollow">installation instructions</a> (don't forget to run syncdb at the end).</p> <p>Then you can <a href="http://south.aeracode.org/docs/convertinganapp.html" rel="nofollow">...
2
2010-07-20T07:08:42Z
[ "python", "mysql", "database", "django", "django-south" ]
Distance Between GIS Points
2,445,806
<p>I have a large number of GIS (latitude, longitude) coordinates, and I'd like to get the distance between them. Is there a service that will calculate the shortest path for me? I know about google maps, but I'd like something I can use from Python, and that can handle a large batch of requests at once.</p> <p>I'm lo...
3
2010-03-15T08:21:40Z
2,445,843
<p>If you have interconnections between the points you could use <a href="http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm" rel="nofollow">Dijkstra's algorithm</a> for a 'shortest path from a single point' or <a href="http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm" rel="nofollow">Floyd's algorithm</a>...
0
2010-03-15T08:32:53Z
[ "python", "maps", "gis" ]
Distance Between GIS Points
2,445,806
<p>I have a large number of GIS (latitude, longitude) coordinates, and I'd like to get the distance between them. Is there a service that will calculate the shortest path for me? I know about google maps, but I'd like something I can use from Python, and that can handle a large batch of requests at once.</p> <p>I'm lo...
3
2010-03-15T08:21:40Z
2,449,815
<p>Check out OpenStreetMap. You can download their map data and have it lying around on your local system. <a href="http://wiki.openstreetmap.org/wiki/Routing" rel="nofollow">http://wiki.openstreetmap.org/wiki/Routing</a> discusses the various routing systems for their data.</p> <p>You are aware that the traveling sal...
1
2010-03-15T19:21:52Z
[ "python", "maps", "gis" ]
Distance Between GIS Points
2,445,806
<p>I have a large number of GIS (latitude, longitude) coordinates, and I'd like to get the distance between them. Is there a service that will calculate the shortest path for me? I know about google maps, but I'd like something I can use from Python, and that can handle a large batch of requests at once.</p> <p>I'm lo...
3
2010-03-15T08:21:40Z
2,456,373
<p>using <a href="http://qgis.org" rel="nofollow">Qgis</a>:</p> <ul> <li>Use the delimited text plugin to import the data</li> <li>save the import as a shape file</li> <li>Open the shape file</li> <li>using the ftools plugin, calculate the matrix distance</li> </ul>
1
2010-03-16T16:52:29Z
[ "python", "maps", "gis" ]
Distance Between GIS Points
2,445,806
<p>I have a large number of GIS (latitude, longitude) coordinates, and I'd like to get the distance between them. Is there a service that will calculate the shortest path for me? I know about google maps, but I'd like something I can use from Python, and that can handle a large batch of requests at once.</p> <p>I'm lo...
3
2010-03-15T08:21:40Z
5,701,295
<p>So I take it based on your question and the answers posted that you are asking what program to use? If you can find a way to get a copy for free or cheap (like through work, school, etc) I'd recommend ArcGIS 9.x. It has its quirks, but it's highly supported by the user community and there are a lot of forums and hel...
1
2011-04-18T10:05:26Z
[ "python", "maps", "gis" ]
In Django, what is a one-to-one relationship?
2,445,823
<p>I've always been using ForeignKeys.</p>
1
2010-03-15T08:28:37Z
2,445,847
<p>A one-to-one relationship is a unique relation between two entities in both directions. I.e. for an entity A there exists only one entity B and vice versa.</p> <p>The <a href="http://docs.djangoproject.com/en/1.1/ref/models/fields/#onetoonefield" rel="nofollow">documentation</a> says:</p> <blockquote> <p>Concept...
3
2010-03-15T08:34:00Z
[ "python", "mysql", "database", "django" ]
In Django, why is user.is_authenticated a method and not a member variable like is_staff
2,445,952
<p>I've lost some time with a bug in my app due to user authentication. I think that it's a bit confusing but maybe someone can explain the reason and it will appear to me very logical.</p> <p>The <code>user.is_staff</code> is a member variable while <code>user.is_authenticated</code> is a method. However <code>is_aut...
1
2010-03-15T09:02:05Z
2,445,967
<p>Because <code>is_staff</code> is a database field and <code>is_authenticated</code> is not (since that would be strange). Maybe <code>is_authenticated</code> could be turned into a property, but django devs must have decided, that making it a function would be clearer.</p>
7
2010-03-15T09:04:34Z
[ "python", "django", "django-authentication" ]
In Django, why is user.is_authenticated a method and not a member variable like is_staff
2,445,952
<p>I've lost some time with a bug in my app due to user authentication. I think that it's a bit confusing but maybe someone can explain the reason and it will appear to me very logical.</p> <p>The <code>user.is_staff</code> is a member variable while <code>user.is_authenticated</code> is a method. However <code>is_aut...
1
2010-03-15T09:02:05Z
2,446,560
<p>is_authenticated() just checks that the user has provided a valid username and password. It would not allow to login user as guest user (If i am not wrong).</p>
0
2010-03-15T11:00:01Z
[ "python", "django", "django-authentication" ]
Where can I find PythonMagick documentation?
2,445,985
<p>I cant seem to find the PythonMagick (Bindings for ImageMagick) documentation anywhere. Even the download doesn't have a docs folder. Link please?</p>
5
2010-03-15T09:10:12Z
2,446,164
<p>I don't think it is active anymore. - <strong><a href="http://sxoop.wordpress.com/2006/11/29/whither-pythonmagick/">look here</a></strong></p>
5
2010-03-15T09:47:09Z
[ "python", "imagemagick" ]
Where can I find PythonMagick documentation?
2,445,985
<p>I cant seem to find the PythonMagick (Bindings for ImageMagick) documentation anywhere. Even the download doesn't have a docs folder. Link please?</p>
5
2010-03-15T09:10:12Z
2,446,200
<p>Check out PIL : <a href="http://www.pythonware.com/products/pil/" rel="nofollow">http://www.pythonware.com/products/pil/</a></p> <p>IMHO, does a decent job.</p>
1
2010-03-15T09:52:43Z
[ "python", "imagemagick" ]
Where can I find PythonMagick documentation?
2,445,985
<p>I cant seem to find the PythonMagick (Bindings for ImageMagick) documentation anywhere. Even the download doesn't have a docs folder. Link please?</p>
5
2010-03-15T09:10:12Z
5,188,697
<p>Check out my post here for a workaround.</p> <p><a href="http://stackoverflow.com/questions/1740158/documents-and-examples-of-pythonmagick/5188661#5188661">Documents and examples of PythonMagick</a></p>
1
2011-03-04T01:30:39Z
[ "python", "imagemagick" ]
Where can I find PythonMagick documentation?
2,445,985
<p>I cant seem to find the PythonMagick (Bindings for ImageMagick) documentation anywhere. Even the download doesn't have a docs folder. Link please?</p>
5
2010-03-15T09:10:12Z
17,154,986
<p>I think <a href="http://stackoverflow.com/users/58737/pratik-deoghare">Pratik Deoghare</a> is wrong here, the article by his link was written in 2006, but PythonMagick was active as of September 2012.</p> <p>There are 3 libraries to work with ImageMagick through Python:</p> <ul> <li><a href="http://www.imagemagick...
2
2013-06-17T19:10:26Z
[ "python", "imagemagick" ]
how to add markup to text using JavaScript regex
2,446,056
<p>I need to add markup to some text using JavaScript regular expressions. In Python I could do this with:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.sub('(banana|apple)', r'&lt;b&gt;\1&lt;/b&gt;', 'I have 1 banana and 2 apples!') 'I have 1 &lt;b&gt;banana&lt;/b&gt; and 2 &lt;b&gt;apple&lt;/b&gt;s!' </code>...
2
2010-03-15T09:24:36Z
2,446,071
<p>In the new string, you can reference capture groups via the tokens <code>$1</code>, <code>$2</code>, etc. A lot of the high-level reference sites (like w3schools) fail to document that. It's in <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="nofollow">the spec</a>, of course, or ...
2
2010-03-15T09:27:35Z
[ "javascript", "python", "regex" ]
how to add markup to text using JavaScript regex
2,446,056
<p>I need to add markup to some text using JavaScript regular expressions. In Python I could do this with:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.sub('(banana|apple)', r'&lt;b&gt;\1&lt;/b&gt;', 'I have 1 banana and 2 apples!') 'I have 1 &lt;b&gt;banana&lt;/b&gt; and 2 &lt;b&gt;apple&lt;/b&gt;s!' </code>...
2
2010-03-15T09:24:36Z
2,446,075
<p>You can use <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/Replace" rel="nofollow"><code>String.replace()</code></a> for this and use <code>$n</code> to reference captured groups from the regular expression:</p> <pre><code>var in = 'I have 1 banana and 2 apples!'; var out = i...
2
2010-03-15T09:28:27Z
[ "javascript", "python", "regex" ]
read a text field in Python using regular expressions
2,446,447
<p>I have text file, like</p> <pre><code>FILED AS OF DATE: 20090209 DATE AS OF CHANGE: 20090209 </code></pre> <p>I need to find the position using <code>FILED AS OF DATE:</code> and read the date. I know how to do it using python strings. But using a regular expression seems cooler:)</p> <p>Btw, how to pa...
1
2010-03-15T10:41:21Z
2,446,489
<p>Is this what you need?</p> <pre><code>/FILED.*([0-9]{4})([0-9]{2})([0-9]{2})$/ </code></pre> <p>Search for FILED then anything then parses date divided in 3 groups.</p>
1
2010-03-15T10:48:59Z
[ "python", "regex" ]
read a text field in Python using regular expressions
2,446,447
<p>I have text file, like</p> <pre><code>FILED AS OF DATE: 20090209 DATE AS OF CHANGE: 20090209 </code></pre> <p>I need to find the position using <code>FILED AS OF DATE:</code> and read the date. I know how to do it using python strings. But using a regular expression seems cooler:)</p> <p>Btw, how to pa...
1
2010-03-15T10:41:21Z
2,446,549
<pre><code>#!/usr/bin/env python import datetime, fileinput, re for line in fileinput.input(): if 'FILED AS OF DATE' in line: line = line.rstrip() dt = datetime.datetime.strptime(line, 'FILED AS OF DATE: %Y%m%d') # or with regex date_str, = re.findall(r'\d+', line) dt = da...
3
2010-03-15T10:57:39Z
[ "python", "regex" ]
read a text field in Python using regular expressions
2,446,447
<p>I have text file, like</p> <pre><code>FILED AS OF DATE: 20090209 DATE AS OF CHANGE: 20090209 </code></pre> <p>I need to find the position using <code>FILED AS OF DATE:</code> and read the date. I know how to do it using python strings. But using a regular expression seems cooler:)</p> <p>Btw, how to pa...
1
2010-03-15T10:41:21Z
2,446,565
<p>You really do not need to use <code>RE</code> for this. </p> <p>Regarding parsing date, you can use <a href="http://docs.python.org/library/datetime.html#datetime.datetime.strptime" rel="nofollow">datetime.strptime(date_string, format)</a>. Then you can convert it from <code>datetime.datetime</code> to <code>dateti...
1
2010-03-15T11:00:35Z
[ "python", "regex" ]
How to define initialized C-array in the Pyrex?
2,446,873
<p>I want to define initialized C-array in Pyrex, e.g. equivalent of:</p> <pre><code>unsigned char a[8] = {0,1,2,3,4,5,6,7}; </code></pre> <p>What will be equivalent in Pyrex?</p> <p>Just array is</p> <pre><code>cdef unsigned char a[8] </code></pre> <p>But how can I made it initialized with my values?</p>
3
2010-03-15T12:02:06Z
2,447,961
<p>In <a href="http://www.cython.org/" rel="nofollow">Cython</a>, Pyrex's successor, this feature was added over a year a go to fix this <a href="http://trac.cython.org/cython_trac/ticket/93" rel="nofollow">feature request</a>, so for example the following works in Cython now:</p> <pre><code>cdef double a[] = [0.5, 0....
4
2010-03-15T14:50:23Z
[ "python", "c", "python-c-extension", "pyrex" ]
monkey patching time.time() in python
2,446,987
<p>I've an application where, for testing, I need to replace the time.time() call with a specific timestamp, I've done that in the past using ruby </p> <p>(code available here: <a href="http://github.com/zemariamm/Back-to-Future/blob/master/back_to_future.rb" rel="nofollow">http://github.com/zemariamm/Back-to-Future/b...
4
2010-03-15T12:22:39Z
2,447,007
<p>You can simply set time.time to point to your new time function, like this:</p> <pre><code>import time def my_time(): return 0.0 old_time = time.time time.time = my_time </code></pre>
14
2010-03-15T12:25:56Z
[ "python", "ruby", "datetime", "time", "monkeypatching" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,447,141
<p>Learn a few other languages. It will help you make the difference between algorithms (the structure of processing, unchanged between languages) and the local syntaxic features of the language. Then you can "write Foo in Bar" for any combination of languages "Foo" and "Bar".</p>
0
2010-03-15T12:48:42Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,447,150
<p>This is useful if you want to understand how to code to python in a more pythonic or correct way: <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">http://www.python.org/dev/peps/pep-0008/</a></p>
-2
2010-03-15T12:49:55Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,447,162
<p>You could post your code at <a href="http://refactormycode.com/" rel="nofollow">Refactor my code</a> to see if someone can show you a more Pythonic way to do it.</p>
3
2010-03-15T12:51:25Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,447,198
<p>You might consider immersing yourself in the Python paradigms. The best way is to first know what they are then explore the best practices by reading some literature and reviewing some code samples. I recommend <a href="http://rads.stackoverflow.com/amzn/click/0596158068" rel="nofollow">Learning Python</a> by Mark...
20
2010-03-15T12:56:46Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,447,218
<p>You could start by reading <a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow">The Zen of Python</a>. It'll give you some insight into how Python code is supposed to be written, provided you understand the language enough to understand what it's talking about. :-)</p>
6
2010-03-15T12:59:06Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,447,379
<p>If you want to see some fairly idiomatic Python that does non-trivial stuff, there's <a href="http://diveintopython.net/toc/index.html" rel="nofollow">Dive Into Python</a>, although <a href="http://diveintopython3.net/" rel="nofollow">Dive Into Python 3</a> is newer and might be a better source of style tips. If you...
4
2010-03-15T13:23:44Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,447,483
<p>Some of the major ways in which Python differs from C/Java-like languages are:</p> <ol> <li><p>List comprehensions.</p></li> <li><p>Support for functional programming.</p></li> <li><p>The use of certain <em>Pythonic</em> constructs instead of similar C-like constructs although both seem to work (list comprehensions...
5
2010-03-15T13:39:28Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,448,287
<p>Definitely not a panacea but I think you should try some code golf in Python. Obviously nobody should write "golfed" code IRL, but finding the most terse way to express something really forces you to exploit the built in functionality of the language.</p>
3
2010-03-15T15:31:33Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,448,508
<p>If you are new to Python and coming from Java (or C#, or other similar statically typed OO language), these classic articles from PJ Eby and Ryan Tomayko are necessary reading:</p> <ul> <li><a href="http://dirtsimple.org/2004/12/python-is-not-java.html" rel="nofollow">Python Is Not Java</a> (PJE) </li> <li><a href=...
6
2010-03-15T15:58:02Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,455,985
<p>Someone provided me with this list of how <a href="http://dirtsimple.org/2004/12/python-is-not-java.html" rel="nofollow">"Python is not Java"</a> when I started Python after Java, and it was very helpful.</p> <p>Also, check out <a href="http://stackoverflow.com/questions/2339371/as-a-java-programmer-learning-python...
2
2010-03-16T16:05:17Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,495,445
<p>Eat Python, Sleep Python and Drink Python. That is the only way........</p>
0
2010-03-22T20:23:30Z
[ "java", "python" ]
Programming in Python vs. programming in Java
2,447,118
<p>I've been writing Java for the last couple of years , and now I've started to write in python (in addition). </p> <p>The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java. </p> <p>Any tips ...
28
2010-03-15T12:45:02Z
2,495,504
<p>Try to find algorithms that you understand well and see how they are implemented in python standard libraries.</p> <p>Persist. :)</p>
1
2010-03-22T20:33:04Z
[ "java", "python" ]
does close() imply flush() in Python?
2,447,143
<p>In Python, and in general - does a <code>close()</code> operation on a file object imply a <code>flush()</code> operation?</p>
21
2010-03-15T12:48:51Z
2,447,160
<p>Yes. It uses the underlying <code>close()</code> function which does that for you (<a href="https://hg.python.org/cpython/file/default/Modules/_io/fileio.c">source</a>).</p>
20
2010-03-15T12:50:59Z
[ "python", "operating-system", "flush" ]
does close() imply flush() in Python?
2,447,143
<p>In Python, and in general - does a <code>close()</code> operation on a file object imply a <code>flush()</code> operation?</p>
21
2010-03-15T12:48:51Z
2,447,205
<p>NB: <code>close()</code> and <code>flush()</code> won't ensure that the data is actually secure on the disk. It just ensures that the OS has the data == that it isn't buffered inside the process.</p> <p>You can try sync or fsync to get the data written to the disk.</p>
9
2010-03-15T12:57:18Z
[ "python", "operating-system", "flush" ]
does close() imply flush() in Python?
2,447,143
<p>In Python, and in general - does a <code>close()</code> operation on a file object imply a <code>flush()</code> operation?</p>
21
2010-03-15T12:48:51Z
12,431,141
<p>filehandle.close does not necessarily flush. Surprisingly, filehandle.flush doesn't help either---it still can get stuck in the OS buffers when Python is running. Observe this session where I wrote to a file, closed it and Ctrl-Z to the shell command prompt and examined the file:</p> <pre><code>$ cat xyz ghi $ fg ...
-3
2012-09-14T19:56:53Z
[ "python", "operating-system", "flush" ]
How would I write this query in GeoDjango? (It's a library for geographic calculations in Django)
2,447,257
<p>Right now I'm using raw SQL to <strong><em>find people within 500 meters of the current user</em></strong>.</p> <pre><code>cursor.execute("SELECT user_id FROM myapp_location WHERE\ GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) &lt; %s"\ ,(user_utm_easting...
1
2010-03-15T13:04:19Z
2,447,482
<p>Well assuming you have the appropriate model, </p> <pre><code>from django.contrib.gis.db import models class User(models.Model): location = models.PointField() objects = models.GeoManager() </code></pre> <p>it would look like:</p> <pre><code>User.objects.filter(location__dwithin=(current_user.location, D...
1
2010-03-15T13:39:23Z
[ "python", "mysql", "database", "django", "location" ]
How to get a template tag to auto-check a checkbox in Django
2,447,261
<p>I'm using a ModelForm class to generate a bunch of checkboxes for a ManyToManyField but I've run into one problem: while the default behaviour automatically checks the appropriate boxes (when I'm editing an object), I can't figure out how to get that information in my own custom templatetag.</p> <p>Here's what I've...
1
2010-03-15T13:05:24Z
2,447,990
<p>In your <em>input</em> tag for the checkbox, you can just add the <em>checked</em> attribute based on some condition. Say your box object has property <em>checked</em> which value is either "checked" or empty string ""</p> <pre><code>r += "&lt;label for=\"id_%s_%d\" class=\"%s\"&gt;&lt;input type=\"checkbox\" name=...
3
2010-03-15T14:53:40Z
[ "python", "django", "templatetags" ]
How to get a template tag to auto-check a checkbox in Django
2,447,261
<p>I'm using a ModelForm class to generate a bunch of checkboxes for a ManyToManyField but I've run into one problem: while the default behaviour automatically checks the appropriate boxes (when I'm editing an object), I can't figure out how to get that information in my own custom templatetag.</p> <p>Here's what I've...
1
2010-03-15T13:05:24Z
2,452,082
<p>Turns out the value I was looking for, the elements in the list that were "checked" isn't in the <code>field</code>, but rather part of the <code>form</code> object. I re-worked the template tag to look like this and it does exactly what I need:</p> <pre><code>@register.filter def alignboxes(boxes, cls): r = ...
0
2010-03-16T04:17:19Z
[ "python", "django", "templatetags" ]
__getattr__ on a module
2,447,353
<p>How can implement the equivalent of a <code>__getattr__</code> on a class, on a module?</p> <h2>Example</h2> <p>When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed i...
81
2010-03-15T13:20:08Z
2,447,383
<p>This is a hack, but you can wrap the module with a class:</p> <pre><code>class Wrapper(object): def __init__(self, wrapped): self.wrapped = wrapped def __getattr__(self, name): # Perform custom logic here try: return getattr(self.wrapped, name) except AttributeError: return 'default'...
36
2010-03-15T13:24:22Z
[ "python", "module", "python-3.x", "getattr", "attributeerror" ]
__getattr__ on a module
2,447,353
<p>How can implement the equivalent of a <code>__getattr__</code> on a class, on a module?</p> <h2>Example</h2> <p>When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed i...
81
2010-03-15T13:20:08Z
2,447,391
<p>We don't usually do it that way. </p> <p>What we do is this.</p> <pre><code>class A(object): .... # The implicit global instance a= A() def salutation( *arg, **kw ): a.salutation( *arg, **kw ) </code></pre> <p>Why? So that the implicit global instance is visible.</p> <p>For examples, look at the <code>ra...
17
2010-03-15T13:25:57Z
[ "python", "module", "python-3.x", "getattr", "attributeerror" ]
__getattr__ on a module
2,447,353
<p>How can implement the equivalent of a <code>__getattr__</code> on a class, on a module?</p> <h2>Example</h2> <p>When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed i...
81
2010-03-15T13:20:08Z
2,448,064
<p>Similar to what @HÃ¥vard S proposed, in a case where I needed to implement some magic on a module (like <code>__getattr__</code>), I would define a new class that inherits from <code>types.ModuleType</code> and put that in <code>sys.modules</code> (probably replacing the module where my custom <code>ModuleType</code...
11
2010-03-15T15:02:49Z
[ "python", "module", "python-3.x", "getattr", "attributeerror" ]
__getattr__ on a module
2,447,353
<p>How can implement the equivalent of a <code>__getattr__</code> on a class, on a module?</p> <h2>Example</h2> <p>When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed i...
81
2010-03-15T13:20:08Z
2,448,111
<p>Create your module file that has your classes. Import the module. Run <code>getattr</code> on the module you just imported. You can do a dynamic import using <code>__import__</code> and pull the module from sys.modules.</p> <p>Here's your module <code>some_module.py</code>:</p> <pre><code>class Foo(object): pa...
-1
2010-03-15T15:09:00Z
[ "python", "module", "python-3.x", "getattr", "attributeerror" ]
__getattr__ on a module
2,447,353
<p>How can implement the equivalent of a <code>__getattr__</code> on a class, on a module?</p> <h2>Example</h2> <p>When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed i...
81
2010-03-15T13:20:08Z
2,776,585
<p>This is hackish, but...</p> <pre><code>import types class A(object): def salutation(self, accusative): print "hello", accusative def farewell(self, greeting, accusative): print greeting, accusative def AddGlobalAttribute(classname, methodname): print "Adding " + classname + "." + met...
6
2010-05-05T20:35:45Z
[ "python", "module", "python-3.x", "getattr", "attributeerror" ]
__getattr__ on a module
2,447,353
<p>How can implement the equivalent of a <code>__getattr__</code> on a class, on a module?</p> <h2>Example</h2> <p>When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed i...
81
2010-03-15T13:20:08Z
6,181,791
<p>In some circumstances the <code>globals()</code> dictionary can suffice, for example you can instantiate a class by name from the global scope:</p> <pre><code>from somemodule import * # imports SomeClass someclass_instance = globals()['SomeClass']() </code></pre>
-1
2011-05-31T00:15:03Z
[ "python", "module", "python-3.x", "getattr", "attributeerror" ]
__getattr__ on a module
2,447,353
<p>How can implement the equivalent of a <code>__getattr__</code> on a class, on a module?</p> <h2>Example</h2> <p>When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed i...
81
2010-03-15T13:20:08Z
7,668,273
<p>There are two basic problems you are running into here:</p> <ol> <li><code>__xxx__</code> methods are only looked up on the class</li> <li><code>TypeError: can't set attributes of built-in/extension type 'module'</code></li> </ol> <p>(1) means any solution would have to also keep track of which module was being ex...
80
2011-10-05T21:59:51Z
[ "python", "module", "python-3.x", "getattr", "attributeerror" ]
__getattr__ on a module
2,447,353
<p>How can implement the equivalent of a <code>__getattr__</code> on a class, on a module?</p> <h2>Example</h2> <p>When calling a function that does not exist in a module's statically defined attributes, I wish to create an instance of a class in that module, and invoke the method on it with the same name as failed i...
81
2010-03-15T13:20:08Z
17,625,034
<p>Here's my own humble contribution -- a slight embellishment of @HÃ¥vard S's highly rated answer, but a bit more explicit (so it might be acceptable to @S.Lott, even though probably not good enough for the OP):</p> <pre><code>import sys class A(object): def salutation(self, accusative): print "hello", a...
2
2013-07-12T22:33:56Z
[ "python", "module", "python-3.x", "getattr", "attributeerror" ]
Converting python objects for rpy2
2,447,454
<p>The following code is supposed to create a heatmap in rpy2</p> <pre><code>import numpy as np from rpy2.robjects import r data = np.random.random((10,10)) r.heatmap(data) </code></pre> <p>However, it results in the following error</p> <pre><code>Traceback (most recent call last): File "z.py", line 8, in &lt;...
23
2010-03-15T13:35:44Z
2,447,474
<p>You need to add </p> <pre><code>import rpy2.robjects.numpy2ri rpy2.robjects.numpy2ri.activate() </code></pre> <p>See <a href="http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html">http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html</a>:</p> <blockquote> <p>That import alone is sufficient to switch an a...
31
2010-03-15T13:38:19Z
[ "python", "rpy2" ]
Converting python objects for rpy2
2,447,454
<p>The following code is supposed to create a heatmap in rpy2</p> <pre><code>import numpy as np from rpy2.robjects import r data = np.random.random((10,10)) r.heatmap(data) </code></pre> <p>However, it results in the following error</p> <pre><code>Traceback (most recent call last): File "z.py", line 8, in &lt;...
23
2010-03-15T13:35:44Z
8,261,291
<p>For rpy2 2.2.4 I had to add:</p> <pre><code>import rpy2.robjects.numpy2ri rpy2.robjects.numpy2ri.activate() </code></pre>
10
2011-11-24T18:29:13Z
[ "python", "rpy2" ]
Converting python objects for rpy2
2,447,454
<p>The following code is supposed to create a heatmap in rpy2</p> <pre><code>import numpy as np from rpy2.robjects import r data = np.random.random((10,10)) r.heatmap(data) </code></pre> <p>However, it results in the following error</p> <pre><code>Traceback (most recent call last): File "z.py", line 8, in &lt;...
23
2010-03-15T13:35:44Z
10,884,093
<p>For me (2.2.1) the following also worked (as documented on <a href="http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html" rel="nofollow">http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html</a>):</p> <pre><code>import rpy2.robjects as ro from rpy2.robjects.numpy2ri import numpy2ri ro.conversion.py2ri = numpy2...
1
2012-06-04T15:42:47Z
[ "python", "rpy2" ]
How to model a social news feed on Google App Engine
2,447,488
<p>We want to implement a "News feed" where a user can see messages broadcasted by her friends, sorted with newest message first. But <strong>the feed should reflect changes in her friends list</strong>. (If she adds new friends, messages from those should be included in the feed, and if she removes friends their messa...
4
2010-03-15T13:40:24Z
2,458,330
<p>Pasting the answer I got for this question in the Google Group for Google App Engine <a href="http://groups.google.com/group/google-appengine/browse_thread/thread/09a05c5f41163b4d#" rel="nofollow">http://groups.google.com/group/google-appengine/browse_thread/thread/09a05c5f41163b4d#</a> By Ikai L (Google) </p> ...
2
2010-03-16T21:33:59Z
[ "python", "google-app-engine", "social-networking", "gae-datastore" ]
2d convolution using python and numpy
2,448,015
<p>I am trying to perform a 2d convolution in python using numpy</p> <p>I have a 2d array as follows with kernel H_r for the rows and H_c for the columns</p> <pre><code>data = np.zeros((nr, nc), dtype=np.float32) #fill array with some data here then convolve for r in range(nr): data[r,:] = np.convolve(data[r,:]...
5
2010-03-15T14:56:43Z
2,448,194
<p>Since you already have your kernel separated you should simply use the sepfir2d function from scipy:</p> <pre><code>from scipy.signal import sepfir2d convolved = sepfir2d(data, H_r, H_c) </code></pre> <p>On the other hand, the code you have there looks all right ...</p>
2
2010-03-15T15:18:42Z
[ "python", "numpy", "convolution" ]
2d convolution using python and numpy
2,448,015
<p>I am trying to perform a 2d convolution in python using numpy</p> <p>I have a 2d array as follows with kernel H_r for the rows and H_c for the columns</p> <pre><code>data = np.zeros((nr, nc), dtype=np.float32) #fill array with some data here then convolve for r in range(nr): data[r,:] = np.convolve(data[r,:]...
5
2010-03-15T14:56:43Z
14,606,804
<p>Try to first round and then cast to uint8:</p> <pre><code>data = data.round().astype(np.uint8); </code></pre>
0
2013-01-30T14:51:12Z
[ "python", "numpy", "convolution" ]
2d convolution using python and numpy
2,448,015
<p>I am trying to perform a 2d convolution in python using numpy</p> <p>I have a 2d array as follows with kernel H_r for the rows and H_c for the columns</p> <pre><code>data = np.zeros((nr, nc), dtype=np.float32) #fill array with some data here then convolve for r in range(nr): data[r,:] = np.convolve(data[r,:]...
5
2010-03-15T14:56:43Z
31,201,682
<p>This code incorrect:</p> <pre><code>for r in range(nr): data[r,:] = np.convolve(data[r,:], H_r, 'same') for c in range(nc): data[:,c] = np.convolve(data[:,c], H_c, 'same') </code></pre> <p>See Nussbaumer transformation from multidimentional convolution to one dimentional.</p>
-2
2015-07-03T08:04:43Z
[ "python", "numpy", "convolution" ]
2d convolution using python and numpy
2,448,015
<p>I am trying to perform a 2d convolution in python using numpy</p> <p>I have a 2d array as follows with kernel H_r for the rows and H_c for the columns</p> <pre><code>data = np.zeros((nr, nc), dtype=np.float32) #fill array with some data here then convolve for r in range(nr): data[r,:] = np.convolve(data[r,:]...
5
2010-03-15T14:56:43Z
39,852,783
<h2>Possible Problem</h2> <p>I believe you are doing two 1d convolutions, the first per columns and the second per rows, and replacing the results from the first with the results of the second. </p> <p>Notice that <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html" rel="nofollow"><code>n...
0
2016-10-04T12:48:23Z
[ "python", "numpy", "convolution" ]
How to customize pickle for django model objects
2,448,035
<p>My app uses a "per-user session" to allow multiple sessions from the same user to share state. It operates very similarly to the django session by pickling objects.</p> <p>I need to pickle a complex object that refers to django model objects. The standard pickling process stores a denormalized object in the pickl...
4
2010-03-15T14:58:59Z
2,448,188
<p>You can overload the serialization methods. But it would be simpler to put the id and class in a tuple or dict and pickle that.</p>
0
2010-03-15T15:17:32Z
[ "python", "django", "pickle" ]
How to customize pickle for django model objects
2,448,035
<p>My app uses a "per-user session" to allow multiple sessions from the same user to share state. It operates very similarly to the django session by pickling objects.</p> <p>I need to pickle a complex object that refers to django model objects. The standard pickling process stores a denormalized object in the pickl...
4
2010-03-15T14:58:59Z
2,448,369
<p>It's unclear what your goal is.</p> <p>"But if I just store the id and class in a tuple then I'm necessarily going back to the database every time I use any of the django objects. I'd like to be able to keep the ones I'm using in memory over the course of a page request."</p> <p>This doesn't make sense, since a vi...
1
2010-03-15T15:39:45Z
[ "python", "django", "pickle" ]
How can I use functools.partial on multiple methods on an object, and freeze parameters out of order?
2,448,187
<p>I find functools.partial to be extremely useful, but I would like to be able to freeze arguments out of order (the argument you want to freeze is not always the first one) and I'd like to be able to apply it to several methods on a class at once, to make a proxy object that has the same methods as the underlying obj...
2
2010-03-15T15:17:17Z
2,448,541
<p>You're "binding too deep": change <code>def __getattribute__(self, name):</code> to <code>def __getattr__(self, name):</code> in class <code>PureProxy</code>. <code>__getattribute__</code> intercepts <em>every</em> attribute access and so bypasses everything that you've set with <code>setattr(self, name, ...</code>...
3
2010-03-15T16:01:53Z
[ "functional-programming", "metaprogramming", "python", "standard-library" ]
Determine if current user is in Administrators group (Windows/Python)
2,448,418
<p>I want certain functions in my application to only be accessible if the current user is an administrator.</p> <p>How can I determine if the current user is in the local Administrators group using Python on Windows?</p>
4
2010-03-15T15:46:23Z
2,448,603
<p>You could try <a href="http://www.mail-archive.com/python-win32@python.org/msg02511.html">this</a>:</p> <pre><code>import ctypes print ctypes.windll.shell32.IsUserAnAdmin() </code></pre>
6
2010-03-15T16:10:06Z
[ "python", "windows", "user", "administrator" ]
Determine if current user is in Administrators group (Windows/Python)
2,448,418
<p>I want certain functions in my application to only be accessible if the current user is an administrator.</p> <p>How can I determine if the current user is in the local Administrators group using Python on Windows?</p>
4
2010-03-15T15:46:23Z
36,946,398
<pre><code>import win32net def if_user_in_group(group, member): members = win32net.NetLocalGroupGetMembers(None, group, 1) return member.lower() in list(map(lambda d: d['name'].lower(), members[0])) # Function usage print(if_user_in_group('SOME_GROUP', 'SOME_USER')) </code></pre> <p>Of course in your cas...
0
2016-04-29T19:48:19Z
[ "python", "windows", "user", "administrator" ]
Python - platform-independent 5.1 Sound Library
2,448,652
<p>Is there any dolby/5.1/7.1 audio processing Python library? It would be best if it is platform independent. </p> <p>Would be nice if it looks like:</p> <pre><code>import lib f = lib.open("8channels_audiofile") lib.play(from=f.channel3, to="left rear"); </code></pre>
0
2010-03-15T16:18:37Z
2,448,717
<p><a href="http://pysonic.sourceforge.net/" rel="nofollow">http://pysonic.sourceforge.net/</a> - this depends on FMOD, which is free for non-commercial use, and supported on many platforms.</p> <p>See the FMOD website for details: <a href="http://www.fmod.org/" rel="nofollow">http://www.fmod.org/</a></p>
0
2010-03-15T16:27:22Z
[ "python", "audio", "signal-processing", "multimedia" ]
Check result of AX_PYTHON_MODULE in configure.ac
2,448,756
<p>In using the m4_ax_python_module.m4 macro in configure.ac (AX_PYTHON_MODULE), one can know at configure time if a given module is installed. It takes two arguments, the module name, and second argument which if not empty, will lead to an exit, useful when the module is a must-have. </p> <p>In the case where you don...
1
2010-03-15T16:33:48Z
2,448,969
<p>Ok the best solution I've found so far was:</p> <p>EDIT: using <code>AS_IF</code> instead of just <code>if test</code></p> <pre><code>AS_IF([test "x${HAVE_PYMOD_JSON}" = "xno"], AS_IF([test "x${HAVE_PYMOD_SIMPLEJSON}" = "xno"], [AC_MSG_ERROR([Requires one of json or simplejson])])) </code></pre> <p>W...
1
2010-03-15T17:06:14Z
[ "python", "autotools", "configure" ]
Django: Adding inline formset rows without javascript
2,448,970
<p>This post relates to this: <a href="http://stackoverflow.com/questions/520421/add-row-to-inlines-dynamically-in-django-admin">http://stackoverflow.com/questions/520421/add-row-to-inlines-dynamically-in-django-admin</a></p> <p>Is there a way to achive adding inline formsets WITHOUT using javascript? Obviously, there...
4
2010-03-15T17:06:29Z
2,449,797
<p>Got it.</p> <p>Sometimes it's the simplest solution. Just make a copy of the request.POST data and modify the TOTAL-FORMS.</p> <p>for example..</p> <pre><code>if request.method=='POST': PrimaryFunctionFormSet = inlineformset_factory(Position,Function) if 'add' in request.POST: cp = request.POST.copy() ...
5
2010-03-15T19:19:57Z
[ "python", "django", "django-models", "django-forms", "django-views" ]
What are some good ways to do intermachine locking?
2,448,984
<p>Our server cluster consists of 20 machines, each with 10 pids of 5 threads. We'd like some way to prevent any two threads, in any pid, on any machine, from modifying the same object at the same time.</p> <p>Our code's written in Python and runs on Linux, if that helps narrow things down.</p> <p>Also, it's a pretty...
6
2010-03-15T17:08:54Z
2,449,080
<p>There may be a better way of doing this, but i would use the Lock class from the threading module to access the "protected" objects in a with statement, here would be an example:</p> <pre><code>from __future__ import with_statement from threading import Lock mylock = Lock() with mylock.acquire(): [ 'do thi...
-3
2010-03-15T17:22:05Z
[ "python", "linux", "multithreading", "mutex" ]
What are some good ways to do intermachine locking?
2,448,984
<p>Our server cluster consists of 20 machines, each with 10 pids of 5 threads. We'd like some way to prevent any two threads, in any pid, on any machine, from modifying the same object at the same time.</p> <p>Our code's written in Python and runs on Linux, if that helps narrow things down.</p> <p>Also, it's a pretty...
6
2010-03-15T17:08:54Z
2,449,100
<p>If you want to synchronize across machines you need a <a href="http://en.wikipedia.org/wiki/Distributed_lock_manager" rel="nofollow">Distributed Lock Manager</a>.</p> <p>I did some quick googling and came up with: <a href="http://stackoverflow.com/questions/1059580/distributed-lock-service">Stackoverflow</a>. Unfor...
5
2010-03-15T17:25:54Z
[ "python", "linux", "multithreading", "mutex" ]
What are some good ways to do intermachine locking?
2,448,984
<p>Our server cluster consists of 20 machines, each with 10 pids of 5 threads. We'd like some way to prevent any two threads, in any pid, on any machine, from modifying the same object at the same time.</p> <p>Our code's written in Python and runs on Linux, if that helps narrow things down.</p> <p>Also, it's a pretty...
6
2010-03-15T17:08:54Z
2,449,186
<p>I assume you came across this blog post <a href="http://amix.dk/blog/post/19386" rel="nofollow">http://amix.dk/blog/post/19386</a> during your googling?</p> <p>The author demonstrates a simple interface to memcachedb which it uses as a dummy distributed lock manager. It's a great idea, and memcache is probably one ...
3
2010-03-15T17:39:42Z
[ "python", "linux", "multithreading", "mutex" ]
What are some good ways to do intermachine locking?
2,448,984
<p>Our server cluster consists of 20 machines, each with 10 pids of 5 threads. We'd like some way to prevent any two threads, in any pid, on any machine, from modifying the same object at the same time.</p> <p>Our code's written in Python and runs on Linux, if that helps narrow things down.</p> <p>Also, it's a pretty...
6
2010-03-15T17:08:54Z
2,449,391
<p>Write code using immutable objects. Write objects that implement the Singleton Pattern.</p> <p>Use a stable Distributed messaging technology such as IPC, webservices, or XML-RPC.</p> <p>I would take a look at Twisted. They got plenty of solutions for such task. </p> <p>I wouldn't use threads in Python esp with re...
2
2010-03-15T18:11:19Z
[ "python", "linux", "multithreading", "mutex" ]
What are some good ways to do intermachine locking?
2,448,984
<p>Our server cluster consists of 20 machines, each with 10 pids of 5 threads. We'd like some way to prevent any two threads, in any pid, on any machine, from modifying the same object at the same time.</p> <p>Our code's written in Python and runs on Linux, if that helps narrow things down.</p> <p>Also, it's a pretty...
6
2010-03-15T17:08:54Z
2,466,954
<p>if you can get the complete infrastructure for a distributed lock manager then go ahead and use that. But that infrastructure is not easy to setup! But here is a practical solution:</p> <p>-designate the node with the lowest ip address as the the master node (that means if the node with lowest ip address hangs, a ...
1
2010-03-18T00:52:40Z
[ "python", "linux", "multithreading", "mutex" ]
duplicate each member in a list - python
2,449,077
<p>Hi I want to write a function that revives a list <code>[1,5,3,6,...]</code> and gives <code>[1,1,5,5,3,3,6,6,...]</code> any idea how to do it? thanks</p>
10
2010-03-15T17:21:36Z
2,449,087
<pre><code>&gt;&gt;&gt; a = [1, 2, 3] &gt;&gt;&gt; b = [] &gt;&gt;&gt; for i in a: b.extend([i, i]) &gt;&gt;&gt; b [1, 1, 2, 2, 3, 3] </code></pre> <p>or</p> <pre><code>&gt;&gt;&gt; [a[i//2] for i in range(len(a)*2)] [1, 1, 2, 2, 3, 3] </code></pre>
9
2010-03-15T17:23:23Z
[ "python", "list" ]
duplicate each member in a list - python
2,449,077
<p>Hi I want to write a function that revives a list <code>[1,5,3,6,...]</code> and gives <code>[1,1,5,5,3,3,6,6,...]</code> any idea how to do it? thanks</p>
10
2010-03-15T17:21:36Z
2,449,125
<pre><code>&gt;&gt;&gt; a = range(10) &gt;&gt;&gt; [val for val in a for _ in (0, 1)] [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9] </code></pre> <p>N.B. <code>_</code> is traditionally used as a placeholder variable name where you do not want to do anything with the contents of the variable. In this c...
27
2010-03-15T17:30:05Z
[ "python", "list" ]
duplicate each member in a list - python
2,449,077
<p>Hi I want to write a function that revives a list <code>[1,5,3,6,...]</code> and gives <code>[1,1,5,5,3,3,6,6,...]</code> any idea how to do it? thanks</p>
10
2010-03-15T17:21:36Z
2,449,134
<p>I would use </p> <pre><code>import itertools foo = [1, 5, 3, 6] new = itertools.chain.from_iterable([item, item] for item in foo) </code></pre> <p><code>new</code> will be an iterator that lazily iterates over the duplicated items. If you need the actual list computed, you can do <code>list(new)</code> or use one ...
1
2010-03-15T17:30:53Z
[ "python", "list" ]
duplicate each member in a list - python
2,449,077
<p>Hi I want to write a function that revives a list <code>[1,5,3,6,...]</code> and gives <code>[1,1,5,5,3,3,6,6,...]</code> any idea how to do it? thanks</p>
10
2010-03-15T17:21:36Z
2,449,148
<p>If you already have the <code>roundrobin</code> recipe described in the documentation for <a href="http://docs.python.org/library/itertools.html"><code>itertools</code></a>—and it is quite handy—then you can just use</p> <pre><code>roundrobin(my_list, my_list) </code></pre>
6
2010-03-15T17:33:15Z
[ "python", "list" ]
duplicate each member in a list - python
2,449,077
<p>Hi I want to write a function that revives a list <code>[1,5,3,6,...]</code> and gives <code>[1,1,5,5,3,3,6,6,...]</code> any idea how to do it? thanks</p>
10
2010-03-15T17:21:36Z
2,449,521
<p>For as much as Guido dislikes the functional operators, they can be pretty darned handy:</p> <pre><code>&gt;&gt;&gt; from operator import add &gt;&gt;&gt; a = range(10) &gt;&gt;&gt; b = reduce(add, [(x,x) for x in a]) </code></pre>
0
2010-03-15T18:32:53Z
[ "python", "list" ]
duplicate each member in a list - python
2,449,077
<p>Hi I want to write a function that revives a list <code>[1,5,3,6,...]</code> and gives <code>[1,1,5,5,3,3,6,6,...]</code> any idea how to do it? thanks</p>
10
2010-03-15T17:21:36Z
2,449,640
<p>I would use <code>zip</code> and <code>itertools.chain</code>.</p> <pre><code>&gt;&gt;&gt; import itertools &gt;&gt;&gt; l = [1,5,3,6,16] &gt;&gt;&gt; list(itertools.chain(*zip(l,l))) [1, 1, 5, 5, 3, 3, 6, 6, 16, 16] </code></pre> <p>Note: I only used <code>list</code> to consume the generator to make it fit for p...
5
2010-03-15T18:53:39Z
[ "python", "list" ]
GQL Request BadArgument Error. How to get around with my case?
2,449,090
<p>My query is essentially the following:</p> <pre><code>entries=Entry.all().order("-votes").order("-date").filter("votes &gt;", VOTE_FILTER).fetch(PAGE_SIZE+1, page* PAGE_SIZE) </code></pre> <p>I want to grab N of the latest entries that have a voting score above some benchmark (VOTE_FILTER). Google currently says t...
1
2010-03-15T17:24:02Z
2,449,238
<p>Yep, there are <a href="http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html#Restrictions_on_Queries" rel="nofollow">Restrictions on Queries</a> as this is Gql not Sql. It looks like you'll need to use a <a href="http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html#Q...
0
2010-03-15T17:47:30Z
[ "python", "google-app-engine", "gql" ]
GQL Request BadArgument Error. How to get around with my case?
2,449,090
<p>My query is essentially the following:</p> <pre><code>entries=Entry.all().order("-votes").order("-date").filter("votes &gt;", VOTE_FILTER).fetch(PAGE_SIZE+1, page* PAGE_SIZE) </code></pre> <p>I want to grab N of the latest entries that have a voting score above some benchmark (VOTE_FILTER). Google currently says t...
1
2010-03-15T17:24:02Z
2,449,615
<p>Assuming your 'vote filter' is a fixed threshold, you need to add a property to your model that records if it's above that threshold or not, enabling you to do a simple equality test to determine which records should be included.</p>
4
2010-03-15T18:47:57Z
[ "python", "google-app-engine", "gql" ]
Python Image Library, Close method
2,449,115
<p>I have been using pil for the first time today. And I wanted to resize an image assuming it was larger than 800x600 and also create a thumbnail. I could do either of these tasks separately but not together in one method (I am doing a custom save method in django admin). This returns a "cannot identify image file" er...
0
2010-03-15T17:28:09Z
2,449,251
<p>Ah, if i only open the orginal image once and create the thumbnail after resizing then the problem is solved</p>
0
2010-03-15T17:49:25Z
[ "python", "django", "python-imaging-library" ]
Any way to assign terminal output to variable with python?
2,449,250
<p>I need to grab the duration of a video file via python as part of a larger script. I know I can use ffmpeg to grab the duration, but I need to be able to save that output as a variable back in python. I thought this would work, but it's giving me a value of 0:</p> <pre><code>cmd = 'ffmpeg -i %s 2&gt;&amp;1 | grep "...
7
2010-03-15T17:49:08Z
2,449,301
<p><code>os.system</code> returns a return value indicating the success or failure of the command. It does not return the output from stdout or stderr. To grab the output from stdout (or stderr), use <code>subprocess.Popen</code>.</p> <pre><code>import subprocess proc=subprocess.Popen('echo "to stdout"', shell=True, s...
14
2010-03-15T17:58:28Z
[ "python", "redirect", "terminal", "ffmpeg" ]
Any way to assign terminal output to variable with python?
2,449,250
<p>I need to grab the duration of a video file via python as part of a larger script. I know I can use ffmpeg to grab the duration, but I need to be able to save that output as a variable back in python. I thought this would work, but it's giving me a value of 0:</p> <pre><code>cmd = 'ffmpeg -i %s 2&gt;&amp;1 | grep "...
7
2010-03-15T17:49:08Z
2,449,308
<p>You probably want <a href="http://docs.python.org/library/subprocess.html#module-subprocess" rel="nofollow">subprocess.Popen</a>.</p>
2
2010-03-15T17:59:11Z
[ "python", "redirect", "terminal", "ffmpeg" ]
Any way to assign terminal output to variable with python?
2,449,250
<p>I need to grab the duration of a video file via python as part of a larger script. I know I can use ffmpeg to grab the duration, but I need to be able to save that output as a variable back in python. I thought this would work, but it's giving me a value of 0:</p> <pre><code>cmd = 'ffmpeg -i %s 2&gt;&amp;1 | grep "...
7
2010-03-15T17:49:08Z
2,449,317
<p>os.system returns the exit code of the executed command, not its output. To do this you would need to use either commands.getoutput (deprecated) or subprocess.Popen:</p> <pre><code>from subprocess import Popen, PIPE stdout = Popen('your command here', shell=True, stdout=PIPE).stdout output = stdout.read() </code><...
2
2010-03-15T17:59:59Z
[ "python", "redirect", "terminal", "ffmpeg" ]
Any way to assign terminal output to variable with python?
2,449,250
<p>I need to grab the duration of a video file via python as part of a larger script. I know I can use ffmpeg to grab the duration, but I need to be able to save that output as a variable back in python. I thought this would work, but it's giving me a value of 0:</p> <pre><code>cmd = 'ffmpeg -i %s 2&gt;&amp;1 | grep "...
7
2010-03-15T17:49:08Z
12,319,260
<p>Much simplest way </p> <pre><code>import commands cmd = "ls -l" output = commands.getoutput(cmd) </code></pre>
1
2012-09-07T13:38:25Z
[ "python", "redirect", "terminal", "ffmpeg" ]
Any way to assign terminal output to variable with python?
2,449,250
<p>I need to grab the duration of a video file via python as part of a larger script. I know I can use ffmpeg to grab the duration, but I need to be able to save that output as a variable back in python. I thought this would work, but it's giving me a value of 0:</p> <pre><code>cmd = 'ffmpeg -i %s 2&gt;&amp;1 | grep "...
7
2010-03-15T17:49:08Z
16,331,065
<pre><code>import commands cmd = 'ls' output = commands.getoutput(cmd) print output </code></pre>
0
2013-05-02T05:39:50Z
[ "python", "redirect", "terminal", "ffmpeg" ]
Many to many relation SQLAlchemy (does relation exsist attribute)
2,449,258
<p>I'm re-asking this question but with a different framework this time. I have two Models: User and Book with a M2M-relation. I want Book to have an attribute "read" that is True when the relation exists. Is this possible in SQLAlchemy?</p>
0
2010-03-15T17:50:58Z
2,449,526
<p>Take a look at <a href="http://www.sqlalchemy.org/docs/mappers.html?highlight=column_property#sql-expressions-as-mapped-attributes" rel="nofollow">SQL Expressions as Mapped Attributes</a>. Something like this should do the job for you: </p> <pre><code>Book.read = column_property( select( [func.c...
1
2010-03-15T18:33:24Z
[ "python", "sqlalchemy", "pylons" ]
Python networkx DFS or BFS missing?
2,449,382
<p>I am interested in finding a path (not necessarily shortest) in a short amount of time. Dijsktra and AStar in networkx is taking too long.</p> <p>Why is there no DFS or BFS in networkx? </p> <p>I plan to write my own DFS and BFS search (I am leaning more towards BFS because my graph is pretty deep). Is there anyth...
1
2010-03-15T18:09:48Z
2,449,669
<p>The <a href="http://networkx.lanl.gov/reference/algorithms.traversal.html" rel="nofollow">Traversal</a> module has multiple depth-first-search variations. Breadth-first-search <a href="http://groups.google.com/group/networkx-discuss/browse_thread/thread/93905139065cb578" rel="nofollow">is implemented</a> in the conn...
4
2010-03-15T18:56:49Z
[ "python", "networkx" ]
Python networkx DFS or BFS missing?
2,449,382
<p>I am interested in finding a path (not necessarily shortest) in a short amount of time. Dijsktra and AStar in networkx is taking too long.</p> <p>Why is there no DFS or BFS in networkx? </p> <p>I plan to write my own DFS and BFS search (I am leaning more towards BFS because my graph is pretty deep). Is there anyth...
1
2010-03-15T18:09:48Z
4,097,598
<p>There is now a depth-first search and breadth-first search in <a href="https://networkx.lanl.gov/trac/browser/networkx/networkx/algorithms/traversal" rel="nofollow">https://networkx.lanl.gov/trac/browser/networkx/networkx/algorithms/traversal</a></p> <p>These are modified from Eppstein's code at www.ics.uci.edu/~e...
4
2010-11-04T14:23:59Z
[ "python", "networkx" ]
how to make a variable change from the text "1m" into "1000000" in python
2,449,848
<p>I have variables with values like 1.7m 1.8k and 1.2b how can I convert them to a real number value for example</p> <pre><code>1.7m = 1700000 1.8k = 1800 1.2b = 1200000000 </code></pre>
1
2010-03-15T19:26:19Z
2,449,899
<p>I would define a dictionary:</p> <pre><code>tens = dict(k=10e3, m=10e6, b=10e9) </code></pre> <p>then </p> <pre><code>x='1.7m' factor, exp = x[0:-1], x[-1].lower() ans = int(float(factor) * tens[exp]) </code></pre>
10
2010-03-15T19:33:27Z
[ "python", "numbers" ]
how to make a variable change from the text "1m" into "1000000" in python
2,449,848
<p>I have variables with values like 1.7m 1.8k and 1.2b how can I convert them to a real number value for example</p> <pre><code>1.7m = 1700000 1.8k = 1800 1.2b = 1200000000 </code></pre>
1
2010-03-15T19:26:19Z
2,449,985
<p>Here is an example using <code>re</code>:</p> <pre><code>input = '17k, 14.05m, 1.235b' multipliers = { 'k': 1e3, 'm': 1e6, 'b': 1e9, } pattern = r'([0-9.]+)([bkm])' for number, suffix in re.findall(pattern, input): number = float(number) print number * multip...
1
2010-03-15T19:46:51Z
[ "python", "numbers" ]
how to make a variable change from the text "1m" into "1000000" in python
2,449,848
<p>I have variables with values like 1.7m 1.8k and 1.2b how can I convert them to a real number value for example</p> <pre><code>1.7m = 1700000 1.8k = 1800 1.2b = 1200000000 </code></pre>
1
2010-03-15T19:26:19Z
2,451,056
<p>You might be interested in a units library like <code>quantities</code> or <code>unum</code>.</p>
1
2010-03-15T22:58:34Z
[ "python", "numbers" ]
how to make a variable change from the text "1m" into "1000000" in python
2,449,848
<p>I have variables with values like 1.7m 1.8k and 1.2b how can I convert them to a real number value for example</p> <pre><code>1.7m = 1700000 1.8k = 1800 1.2b = 1200000000 </code></pre>
1
2010-03-15T19:26:19Z
2,455,233
<p>Using lambda:</p> <pre><code>&gt;&gt;&gt; tens = {'k': 10e3, 'm': 10e6, 'b': 10e9} &gt;&gt;&gt; f = lambda x: int(float(x[:-1])*tens[x[-1]]) &gt;&gt;&gt; f('1.7m') 17000000 &gt;&gt;&gt; f('1.8k') 18000 &gt;&gt;&gt; f('1.2b') 12000000000 </code></pre>
0
2010-03-16T14:38:40Z
[ "python", "numbers" ]
Scale 2D coordinates and keep their relative euclidean distances intact?
2,450,035
<p>I have a set of points like: pointA(3302.34,9392.32), pointB(34322.32,11102.03), etc.</p> <p>I need to scale these so each x- and y-coordinate is in the range (0.0 - 1.0). I tried doing this by first finding the largest x value in the data set (maximum_x_value), and the largest y value in the set (minimum_y_value)....
5
2010-03-15T19:53:27Z
2,450,068
<p>If you mean that you are not keeping aspect ratio: just scale to the minimum bounding square instead of minimum bounding rectangle. You should choose the scale factor along both axises to max(dx,dy).</p>
3
2010-03-15T19:58:22Z
[ "python", "math", "coordinates", "scale" ]