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: list problems
1,908,961
<p>I've read in a text file and converted each line into a list.</p> <p>using this script:</p> <pre><code>l = [s.strip().split() for s in open("cluster2.wcnf").readlines()] </code></pre> <p>How would i go about :</p> <ol> <li>the file it opens is dynamic rather than static? i.e. the user chooses the file to open.</...
0
2009-12-15T17:12:41Z
1,909,024
<p>Things like opening files with names got from the user(i.e. using strings) and selecting/assigning items within a list are really very basic Python concepts. I think the best you can do is to read a good book on Python. I would recommend <a href="http://www.diveintopython.org/toc/index.html" rel="nofollow">Dive Into...
0
2009-12-15T17:26:13Z
[ "python", "list", "select", "line" ]
Python: list problems
1,908,961
<p>I've read in a text file and converted each line into a list.</p> <p>using this script:</p> <pre><code>l = [s.strip().split() for s in open("cluster2.wcnf").readlines()] </code></pre> <p>How would i go about :</p> <ol> <li>the file it opens is dynamic rather than static? i.e. the user chooses the file to open.</...
0
2009-12-15T17:12:41Z
1,909,041
<p>Number 1: When you say you want the file it opens to be dynamic do you mean you want the user to input the file name to be opened? If so you can simply store the file name into a variable. For example:</p> <pre><code>name = "myfile.txt" </code></pre> <p>and replace your "cluster2.wcnf" with name. </p> <p>Numbe...
0
2009-12-15T17:28:16Z
[ "python", "list", "select", "line" ]
Python: list problems
1,908,961
<p>I've read in a text file and converted each line into a list.</p> <p>using this script:</p> <pre><code>l = [s.strip().split() for s in open("cluster2.wcnf").readlines()] </code></pre> <p>How would i go about :</p> <ol> <li>the file it opens is dynamic rather than static? i.e. the user chooses the file to open.</...
0
2009-12-15T17:12:41Z
1,909,185
<p>This is a more optimised way of reading in a whole file where each line is also split into a list</p> <pre><code>import csv, sys # 1 - User specifies file file = input('File: ') f = open(file, 'r') reader = csv.reader(f, delimiter=' ') lines = list(reader) f.close() # 2 - Select specific lines print lines[4] # ...
0
2009-12-15T17:52:29Z
[ "python", "list", "select", "line" ]
Python: list problems
1,908,961
<p>I've read in a text file and converted each line into a list.</p> <p>using this script:</p> <pre><code>l = [s.strip().split() for s in open("cluster2.wcnf").readlines()] </code></pre> <p>How would i go about :</p> <ol> <li>the file it opens is dynamic rather than static? i.e. the user chooses the file to open.</...
0
2009-12-15T17:12:41Z
1,909,198
<p>The way you ask the questions you ask indicates to me that you aren't exactly clear on what you want your program to do eventually.</p> <p>First, that line doesn't just convert the file to a list. It converts it to a list of tuples. Each tuple is a set of space separated values for one line in the file.</p> <p>I...
0
2009-12-15T17:54:16Z
[ "python", "list", "select", "line" ]
Import error with virtualenv
1,909,025
<p>I have a problem with virtualenv. I use it regulary, I use it on my development machine and on several servers. But on this last server I tried to use i got a problem.</p> <p>I created a virtualenv with the --no-site-packages argument, and then I installed some python modules inside the virtualenv. I can confirm th...
5
2009-12-15T17:26:21Z
1,909,095
<p>IIRC, the activate script just puts your virtual env first on your path, so when you type "python" it finds the one in your virtual env first. If the activate script fails, you can always edit your path manually. Also - go here and search for "activate": <a href="http://pylonsbook.com/en/1.1/installing-pylons.html#...
0
2009-12-15T17:36:56Z
[ "python", "virtualenv" ]
Import error with virtualenv
1,909,025
<p>I have a problem with virtualenv. I use it regulary, I use it on my development machine and on several servers. But on this last server I tried to use i got a problem.</p> <p>I created a virtualenv with the --no-site-packages argument, and then I installed some python modules inside the virtualenv. I can confirm th...
5
2009-12-15T17:26:21Z
1,910,294
<p>Is there a bash alias active on this machine for "python", by any chance? That will take priority over the PATH-modifications made by activate, and could cause the wrong python binary to be used.</p> <p>Try running virtualenv/bin/python directly (no need to activate) and see if you can import your module.</p> <p>I...
7
2009-12-15T20:46:57Z
[ "python", "virtualenv" ]
Import error with virtualenv
1,909,025
<p>I have a problem with virtualenv. I use it regulary, I use it on my development machine and on several servers. But on this last server I tried to use i got a problem.</p> <p>I created a virtualenv with the --no-site-packages argument, and then I installed some python modules inside the virtualenv. I can confirm th...
5
2009-12-15T17:26:21Z
1,910,851
<p>After activating the virtual env, try:</p> <pre><code>$ python &gt;&gt;&gt; import sys &gt;&gt;&gt; sys.executable ... </code></pre> <p>... and see if you are running the expected executable.</p> <p>Also check:</p> <pre><code>&gt;&gt;&gt; sys.path [...] </code></pre>
5
2009-12-15T22:21:52Z
[ "python", "virtualenv" ]
Bazaar VCS under IronPython?
1,909,057
<p>Has anyone successfully executed the source control system Bazaar in IronPython?</p>
1
2009-12-15T17:30:53Z
1,911,193
<p>There is several important modules still absent in IronPython which required to run bzr. So, AFAIK today (December 2009) is not possible to run bzr under IronPython.</p> <p>You may ask this question in main bzr mailing list: <a href="http://lists.canonical.com/mailman/listinfo/bazaar" rel="nofollow">http://lists.ca...
1
2009-12-15T23:22:00Z
[ "python", "ironpython", "bazaar", "dynamic-language-runtime" ]
Bazaar VCS under IronPython?
1,909,057
<p>Has anyone successfully executed the source control system Bazaar in IronPython?</p>
1
2009-12-15T17:30:53Z
2,031,255
<p>Yes, Bazaar is pure-python with optional extensions and will run on IronPython. There are a few caveats though. Firstly some changes are needed to Bazaar that haven't yet been merged with the main branch. Secondly, IronPython's slow startup time makes it unsuitable for general bzr usage, and there are are some outst...
2
2010-01-08T22:35:36Z
[ "python", "ironpython", "bazaar", "dynamic-language-runtime" ]
Building app to Classify / Describe Products - Overwhelmed somewhere between planning & execution
1,909,059
<p>Greetings!</p> <p>I recently started working for a company that carries a line of 20,000 Surgical Instruments. Our data on all items is currently spotty and chaotic at best. I intend to fix this.</p> <p>I have been tasked with redesigning the web site. As part of the project, I'm building an app to classify and de...
2
2009-12-15T17:30:55Z
1,909,183
<p>Who will be using the app? How many people at one time? Any particular reasons for choosing CakePHP?</p> <p>From what I've heard, Rails and CakePHP have a lot of 'magic' involved; it becomes a problem when things suddenly don't work right and you have no idea why. If the app is only for people within the company an...
1
2009-12-15T17:51:56Z
[ "python", "mysql", "database-design", "cakephp", "architecture" ]
Building app to Classify / Describe Products - Overwhelmed somewhere between planning & execution
1,909,059
<p>Greetings!</p> <p>I recently started working for a company that carries a line of 20,000 Surgical Instruments. Our data on all items is currently spotty and chaotic at best. I intend to fix this.</p> <p>I have been tasked with redesigning the web site. As part of the project, I'm building an app to classify and de...
2
2009-12-15T17:30:55Z
1,909,352
<p>It sounds like you need a rudimentary inventory system. A way to track the basic attributes of the items you've entered. You will also need some reporting, so the end users can query the system to get an idea of what is in the system.</p> <p>Find a solution that you can reference or review to get idea's from. Your ...
1
2009-12-15T18:17:46Z
[ "python", "mysql", "database-design", "cakephp", "architecture" ]
Building app to Classify / Describe Products - Overwhelmed somewhere between planning & execution
1,909,059
<p>Greetings!</p> <p>I recently started working for a company that carries a line of 20,000 Surgical Instruments. Our data on all items is currently spotty and chaotic at best. I intend to fix this.</p> <p>I have been tasked with redesigning the web site. As part of the project, I'm building an app to classify and de...
2
2009-12-15T17:30:55Z
1,909,409
<p>Worry about your database design first, then what kind of framework you'll use.</p> <p>Regarding EAV, check out <a href="http://decipherinfosys.wordpress.com/2007/01/29/name-value-pair-design/" rel="nofollow">http://decipherinfosys.wordpress.com/2007/01/29/name-value-pair-design/</a> If you still really need to do ...
2
2009-12-15T18:28:05Z
[ "python", "mysql", "database-design", "cakephp", "architecture" ]
Building app to Classify / Describe Products - Overwhelmed somewhere between planning & execution
1,909,059
<p>Greetings!</p> <p>I recently started working for a company that carries a line of 20,000 Surgical Instruments. Our data on all items is currently spotty and chaotic at best. I intend to fix this.</p> <p>I have been tasked with redesigning the web site. As part of the project, I'm building an app to classify and de...
2
2009-12-15T17:30:55Z
1,910,809
<p>I think I like the choice of CakePHP in this instance. Basically you're just providing a web interface to your internal team so they can perform manual data classification, etc. So you just need to get a basic data editing interface up as quickly as possible. Cake's biggest strength, IMO, is its focus on rapid de...
1
2009-12-15T22:17:04Z
[ "python", "mysql", "database-design", "cakephp", "architecture" ]
Building app to Classify / Describe Products - Overwhelmed somewhere between planning & execution
1,909,059
<p>Greetings!</p> <p>I recently started working for a company that carries a line of 20,000 Surgical Instruments. Our data on all items is currently spotty and chaotic at best. I intend to fix this.</p> <p>I have been tasked with redesigning the web site. As part of the project, I'm building an app to classify and de...
2
2009-12-15T17:30:55Z
1,910,993
<p>Nice drawing:)</p> <p>Did you do any experiments with the actual table stucture? It does not look <em>so</em> hard. Here is a go while trying to keep things as simple as possible.</p> <pre><code>==products== id[int] number[varchar]: unique, ie. #50-334 category_id[int]: has-one relation to category.id ==categori...
3
2009-12-15T22:44:59Z
[ "python", "mysql", "database-design", "cakephp", "architecture" ]
Building app to Classify / Describe Products - Overwhelmed somewhere between planning & execution
1,909,059
<p>Greetings!</p> <p>I recently started working for a company that carries a line of 20,000 Surgical Instruments. Our data on all items is currently spotty and chaotic at best. I intend to fix this.</p> <p>I have been tasked with redesigning the web site. As part of the project, I'm building an app to classify and de...
2
2009-12-15T17:30:55Z
1,911,255
<p>Think about your <strong>user</strong> first. Then talk to your users. These sales reps have been working in this field longer than you and they will already have their own mental categorisations of the stock.</p> <p>What kind of interface would they expect to see? The interface you design should reflect the most c...
3
2009-12-15T23:33:57Z
[ "python", "mysql", "database-design", "cakephp", "architecture" ]
Building app to Classify / Describe Products - Overwhelmed somewhere between planning & execution
1,909,059
<p>Greetings!</p> <p>I recently started working for a company that carries a line of 20,000 Surgical Instruments. Our data on all items is currently spotty and chaotic at best. I intend to fix this.</p> <p>I have been tasked with redesigning the web site. As part of the project, I'm building an app to classify and de...
2
2009-12-15T17:30:55Z
1,912,571
<p>This sort of thing is dead easy in Django (assuming I really understand what you're asking). A models.py something like this:</p> <pre><code>from django.db import models class Item(models.Model): price = models.DecimalField(max_digits=9, decimal_places=2) material = models.CharField(max_length=100) class S...
1
2009-12-16T05:47:38Z
[ "python", "mysql", "database-design", "cakephp", "architecture" ]
Building app to Classify / Describe Products - Overwhelmed somewhere between planning & execution
1,909,059
<p>Greetings!</p> <p>I recently started working for a company that carries a line of 20,000 Surgical Instruments. Our data on all items is currently spotty and chaotic at best. I intend to fix this.</p> <p>I have been tasked with redesigning the web site. As part of the project, I'm building an app to classify and de...
2
2009-12-15T17:30:55Z
1,912,944
<p>I agree with drspod - you need to understand what the users want - you may build something of technical genius, but without users it doesn't return the value on your time investment.</p> <p>One you've understood that, I would recommend going down the Python/Django route, or consider Rails as a fast way of getting w...
0
2009-12-16T07:28:25Z
[ "python", "mysql", "database-design", "cakephp", "architecture" ]
How to clean up my Python Installation for a fresh start
1,909,249
<p>I'm developing on Snow Leopard and going through the various "how tos" to get the MySQLdb package installed and working (uphill battle). Things are a mess and I'd like to regain confidence with a fresh, clean, as close to factory install of Python 2.6.</p> <p>What folders should I clean out? </p> <p>What should ...
17
2009-12-15T18:03:17Z
1,909,283
<p>My experience doing development on MacOSX is that the directories for libraries and installation tools are just different enough to cause a lot of problems that you end up having to fix by hand. Eventually, your computer becomes a sketchy wasteland of files and folders duplicated all over the place in an effort to s...
1
2009-12-15T18:09:11Z
[ "python", "osx", "osx-snow-leopard" ]
How to clean up my Python Installation for a fresh start
1,909,249
<p>I'm developing on Snow Leopard and going through the various "how tos" to get the MySQLdb package installed and working (uphill battle). Things are a mess and I'd like to regain confidence with a fresh, clean, as close to factory install of Python 2.6.</p> <p>What folders should I clean out? </p> <p>What should ...
17
2009-12-15T18:03:17Z
1,909,335
<p><a href="http://pypi.python.org/pypi/virtualenv" rel="nofollow">Virtualenv</a> might still work for you. Install it, then create virtual python environments with the <code>--no-site-packages</code> option. This won't clean up your base system, but should allow you to develop in pretty good isolation from the base sy...
5
2009-12-15T18:15:42Z
[ "python", "osx", "osx-snow-leopard" ]
How to clean up my Python Installation for a fresh start
1,909,249
<p>I'm developing on Snow Leopard and going through the various "how tos" to get the MySQLdb package installed and working (uphill battle). Things are a mess and I'd like to regain confidence with a fresh, clean, as close to factory install of Python 2.6.</p> <p>What folders should I clean out? </p> <p>What should ...
17
2009-12-15T18:03:17Z
1,909,611
<p>One thing you should <em>not</em> do is try to remove or change any of the Apple-supplied python files or links: they are in <code>/usr/bin</code> and <code>/System/Library/Frameworks/Python.framework</code>. These are part of OS X and managed by Apple. It is fine to clean up any unnecessary packages you have inst...
11
2009-12-15T19:03:05Z
[ "python", "osx", "osx-snow-leopard" ]
How to clean up my Python Installation for a fresh start
1,909,249
<p>I'm developing on Snow Leopard and going through the various "how tos" to get the MySQLdb package installed and working (uphill battle). Things are a mess and I'd like to regain confidence with a fresh, clean, as close to factory install of Python 2.6.</p> <p>What folders should I clean out? </p> <p>What should ...
17
2009-12-15T18:03:17Z
23,410,716
<p>when doing an "port selfupdate", rsync timesout with rsync.macports.org. There are mirror sites available to use. </p>
0
2014-05-01T15:20:24Z
[ "python", "osx", "osx-snow-leopard" ]
How does one feed a list of files into an app with appscript and Python?
1,909,282
<p>Get your newb-shields up, I'm about to sprinkle you with some.<br> I'm trying to get Photoshop CS4 to open a folderful of JPEG images with AppScript+Python, which could be described like so in BASH: </p> <pre><code>#!/bin/bash for F in `ls ~/Desktop/test`; do open -a "Adobe Photoshop CS4" $F # proceed to m...
1
2009-12-15T18:09:05Z
1,909,827
<p>Photoshop's open command requires an alias object (or list of alias objects).</p> <pre><code>from appscript import * folder = '/path/to/folder' ps = app('Adobe Photoshop CS4') for f in app('Finder').items[mactypes.Alias(folder)].files.get(resulttype=k.alias): ps.open(f) # do stuff here... </code></pre>
3
2009-12-15T19:36:02Z
[ "python", "osx", "applescript", "finder", "appscript" ]
Is it possible to use functions before declaring their body in python?
1,909,325
<p>Is there any way to make possible to use functions in your file before you actually declare their body?</p> <p>The following code doesn't seem to work:</p> <pre><code>abc = myFunction def myFunction(): print "123" </code></pre> <p>Thanks</p>
8
2009-12-15T18:14:27Z
1,909,340
<p>short answer is no.</p> <p>In Python, statements are evaluated along as they are parsed - <code>myFunction</code> wasn't parsed so Python doesn't know about it.</p>
1
2009-12-15T18:16:16Z
[ "python" ]
Is it possible to use functions before declaring their body in python?
1,909,325
<p>Is there any way to make possible to use functions in your file before you actually declare their body?</p> <p>The following code doesn't seem to work:</p> <pre><code>abc = myFunction def myFunction(): print "123" </code></pre> <p>Thanks</p>
8
2009-12-15T18:14:27Z
1,909,379
<p>You can <em>declare</em> functions that use forward declarations, but Python <em>executes</em> the code in your source from top to bottom. So, this would compile and run:</p> <pre><code>def foo(): print "in foo" bar() def bar(): print "in bar" foo() foo() </code></pre> <p>(except it would cause a...
13
2009-12-15T18:21:47Z
[ "python" ]
Is it possible to use functions before declaring their body in python?
1,909,325
<p>Is there any way to make possible to use functions in your file before you actually declare their body?</p> <p>The following code doesn't seem to work:</p> <pre><code>abc = myFunction def myFunction(): print "123" </code></pre> <p>Thanks</p>
8
2009-12-15T18:14:27Z
1,909,389
<p>You can't use the <code>myFunction</code> variable before it's assigned. Your example code is similar to:</p> <pre><code>def myFunction(): print abc abc = 123 </code></pre> <p>To do what you want, either re-arrange the order:</p> <pre><code>def myFunction(): print "123" abc = myFunction </code></pre>...
14
2009-12-15T18:23:27Z
[ "python" ]
Is it possible to use functions before declaring their body in python?
1,909,325
<p>Is there any way to make possible to use functions in your file before you actually declare their body?</p> <p>The following code doesn't seem to work:</p> <pre><code>abc = myFunction def myFunction(): print "123" </code></pre> <p>Thanks</p>
8
2009-12-15T18:14:27Z
1,909,525
<p>You can declare an empty function, use it to set attributes or anything like this, and then modify its code later.</p> <pre><code>def myFunction(): pass myFunction.foo = 'bar' def makeFunction(obj): def myFunction(): print "123" f = myFunction for attr in dir(obj): if attr not in ...
1
2009-12-15T18:50:09Z
[ "python" ]
Is it possible to use functions before declaring their body in python?
1,909,325
<p>Is there any way to make possible to use functions in your file before you actually declare their body?</p> <p>The following code doesn't seem to work:</p> <pre><code>abc = myFunction def myFunction(): print "123" </code></pre> <p>Thanks</p>
8
2009-12-15T18:14:27Z
1,910,269
<p>Python will raise a NameError as it encounters any reference to any name (token that's a valid variable/class/function/object name) for which there was no previous binding.</p> <p>The Python interpreter executes the source code of a file as it reads it. Thus <code>def foo():</code> is actually statement which defi...
1
2009-12-15T20:44:18Z
[ "python" ]
Combine lxml XSLT pretty_print with strip-space
1,909,401
<p>I'm cleaning up some gross XML, and so I've had pretty_print = True set in the call to etree.tostring() on my lxml output of the XSL transform. However, that left me with a few junk whitespace nodes from the original input, so I added</p> <pre><code>&lt;xsl:strip-space elements="*"/&gt; </code></pre> <p>...but tha...
0
2009-12-15T18:25:59Z
1,909,539
<p>Do it in two steps? First strip the spaces, then pretty-print?</p> <p>Just a thought.</p>
1
2009-12-15T18:51:32Z
[ "python", "xml", "xslt", "lxml", "pretty-print" ]
C/Python Socket Performance?
1,909,471
<p>my question simply relates to the difference in performance between a socket in C and in Python. Since my Python build is CPython, I assume it's similar, but I'm curious if someone actually has "real" benchmarks, or at least an opinion that's evidence based. </p> <p>My logics is as such:</p> <ul> <li>C socket much...
10
2009-12-15T18:39:23Z
1,909,504
<p>i would think C would be faster, but python would be a lot easier to manage and use.</p> <p>the difference would be so small, you wouldn't need it unless you were trying to send masses amount of data (something stupid like 1 million gb/second lol)</p> <p>joe</p>
1
2009-12-15T18:45:02Z
[ "python", "c", "sockets", "scapy" ]
C/Python Socket Performance?
1,909,471
<p>my question simply relates to the difference in performance between a socket in C and in Python. Since my Python build is CPython, I assume it's similar, but I'm curious if someone actually has "real" benchmarks, or at least an opinion that's evidence based. </p> <p>My logics is as such:</p> <ul> <li>C socket much...
10
2009-12-15T18:39:23Z
1,909,511
<p>In general, sockets in Python perform just fine. For example, the reference implementation of the BitTorrent tracker server is written in Python.</p> <p>When doing networking operations, the speed of the network is usually the limiting factor. That is, any possible tiny difference in speed between C and Python's so...
11
2009-12-15T18:46:34Z
[ "python", "c", "sockets", "scapy" ]
What is python used for?
1,909,512
<p>Okay, so I am fairly new at programming (knowing only html, CSS, and JavaScript) and I just started diving into python. What I want to know is, what is it used for and what is it designed for?</p>
88
2009-12-15T18:46:46Z
1,909,515
<p>You can check out the <a href="http://en.wikipedia.org/wiki/Python%5F%28programming%5Flanguage%29" rel="nofollow">Wikipedia Entry for Python</a>. One way that you can leverage python is by using it on <a href="http://code.google.com/appengine/" rel="nofollow">Google's AppEngine</a>.</p>
-1
2009-12-15T18:48:04Z
[ "python" ]
What is python used for?
1,909,512
<p>Okay, so I am fairly new at programming (knowing only html, CSS, and JavaScript) and I just started diving into python. What I want to know is, what is it used for and what is it designed for?</p>
88
2009-12-15T18:46:46Z
1,923,081
<p>Python is a dynamic, strongly typed, object oriented, multipurpose programming language, designed to be quick (to learn, to use, and to understand), and to enforce a clean and uniform syntax.</p> <ol> <li>Python is <strong>dynamically</strong> typed: it means that you don't declare a type (e.g. 'integer') for a var...
114
2009-12-17T16:50:33Z
[ "python" ]
What is python used for?
1,909,512
<p>Okay, so I am fairly new at programming (knowing only html, CSS, and JavaScript) and I just started diving into python. What I want to know is, what is it used for and what is it designed for?</p>
88
2009-12-15T18:46:46Z
39,098,766
<h1>Why should you learn Python Programming Language?</h1> <p>Python offers a stepping stone into the world of programming. Even though Python Programming Language has been around for 25 years, it is still rising in popularity. Some of the biggest advantage of Python are it's </p> <ul> <li><strong>Easy to Read &amp; ...
2
2016-08-23T10:32:59Z
[ "python" ]
Python: List to integers
1,909,619
<p>I have read a file in and converted each line into a list. A sample of the list looks like:</p> <p>['15', '2', '0'], ['63', '3', '445', '456' '0'], ['23', '4', '0']</p> <p>i want to retrieve the first number from each list and convert it to and integer so when i carry out the type function i.e.</p> <pre><code>typ...
1
2009-12-15T19:04:42Z
1,909,632
<pre><code># Converts all items in all lists to integers. ls = [map(int, x) for x in the_first_list] </code></pre> <p>Or if you just want the first two items:</p> <pre><code>ls = [map(int, x[:2]) for x in the_first_list] </code></pre> <p>In python 3.x you'd have to also wrap the map in a list constructor, like this<...
1
2009-12-15T19:07:00Z
[ "python", "list", "integer" ]
Python: List to integers
1,909,619
<p>I have read a file in and converted each line into a list. A sample of the list looks like:</p> <p>['15', '2', '0'], ['63', '3', '445', '456' '0'], ['23', '4', '0']</p> <p>i want to retrieve the first number from each list and convert it to and integer so when i carry out the type function i.e.</p> <pre><code>typ...
1
2009-12-15T19:04:42Z
1,909,675
<p>If I understood your question correctly, it is <code>[int x[0] for x in list_of_lists]</code></p>
0
2009-12-15T19:12:55Z
[ "python", "list", "integer" ]
Python: List to integers
1,909,619
<p>I have read a file in and converted each line into a list. A sample of the list looks like:</p> <p>['15', '2', '0'], ['63', '3', '445', '456' '0'], ['23', '4', '0']</p> <p>i want to retrieve the first number from each list and convert it to and integer so when i carry out the type function i.e.</p> <pre><code>typ...
1
2009-12-15T19:04:42Z
1,909,859
<p>If you want a list with the first number of each list, <code>[int(L[0]) for L in lines]</code> (assuming the list of lists is called <code>lines</code>); if you want the first <em>two</em> numbers of each list (it's hard to tell from your question), <code>[int(s) for L in lines for s in L[:2]]</code>; and so forth.<...
2
2009-12-15T19:41:43Z
[ "python", "list", "integer" ]
Python: List to integers
1,909,619
<p>I have read a file in and converted each line into a list. A sample of the list looks like:</p> <p>['15', '2', '0'], ['63', '3', '445', '456' '0'], ['23', '4', '0']</p> <p>i want to retrieve the first number from each list and convert it to and integer so when i carry out the type function i.e.</p> <pre><code>typ...
1
2009-12-15T19:04:42Z
1,910,103
<p>To cast your ints:</p> <pre><code>my_ints = [int(l[0]) for l in your_list] </code></pre> <p>To print them out:</p> <pre><code>print "".join(map(str, my_ints)) </code></pre>
6
2009-12-15T20:18:01Z
[ "python", "list", "integer" ]
Python: List to integers
1,909,619
<p>I have read a file in and converted each line into a list. A sample of the list looks like:</p> <p>['15', '2', '0'], ['63', '3', '445', '456' '0'], ['23', '4', '0']</p> <p>i want to retrieve the first number from each list and convert it to and integer so when i carry out the type function i.e.</p> <pre><code>typ...
1
2009-12-15T19:04:42Z
1,910,162
<pre><code>lines = [['15', '2', '0'], ['63', '3', '445', '456' '0'], ['23', '4', '0']] first_values_as_ints = [int(line[0]) for line in lines] for x in first_values_as_ints: print x, </code></pre>
0
2009-12-15T20:28:00Z
[ "python", "list", "integer" ]
ISO public Django apps developed Agile / BDD style with Doctest, Unitests and Selenium
1,909,674
<p>I've found several blog posts where TDD/BDD is explained, but the examples are usually really basic. Usually they are just for Models. I want to see how people are really using BDD in Real Life.<br> I'd love be pointed towards some Django apps that were built test first style so I can learn from them.</p> <p>I kno...
2
2009-12-15T19:12:55Z
1,910,196
<p>Your likely doing it right already. For the moment, there's an ample amount of blog posts from Django developers where they talk about doing Django development right and how they solved issues with testing some heavy-duty stuff, but not a single resource that covers it all, especially not with employing TDD exclusiv...
3
2009-12-15T20:34:03Z
[ "python", "django", "tdd", "agile", "bdd" ]
How do I add rows and columns to a NUMPY array?
1,909,994
<p>Hello I have a 1000 data series with 1500 points in each. </p> <p>They form a (1000x1500) size Numpy array created using np.zeros((1500, 1000)) and then filled with the data. </p> <p>Now what if I want the array to grow to say 1600 x 1100? Do I have to add arrays using hstack and vstack or is there a better way?</...
6
2009-12-15T20:02:00Z
1,910,150
<p>You should use <a href="http://www.scipy.org/Numpy%5FExample%5FList%5FWith%5FDoc#head-11717acafb821da646a8db6997e59b820ac8761a" rel="nofollow">reshape()</a> and/or <a href="http://www.scipy.org/Numpy%5FExample%5FList%5FWith%5FDoc#head-11717acafb821da646a8db6997e59b820ac8761a" rel="nofollow">resize()</a> depending on...
0
2009-12-15T20:25:50Z
[ "python", "arrays", "numpy", "reshape" ]
How do I add rows and columns to a NUMPY array?
1,909,994
<p>Hello I have a 1000 data series with 1500 points in each. </p> <p>They form a (1000x1500) size Numpy array created using np.zeros((1500, 1000)) and then filled with the data. </p> <p>Now what if I want the array to grow to say 1600 x 1100? Do I have to add arrays using hstack and vstack or is there a better way?</...
6
2009-12-15T20:02:00Z
1,910,401
<p>If you want zeroes in the added elements, <code>my_array.resize((1600, 1000))</code> should work. Note that this differs from <code>numpy.resize(my_array, (1600, 1000))</code>, in which previous lines are duplicated, which is probably not what you want.</p> <p>Otherwise (for instance if you want to avoid initializ...
3
2009-12-15T21:06:06Z
[ "python", "arrays", "numpy", "reshape" ]
How do I add rows and columns to a NUMPY array?
1,909,994
<p>Hello I have a 1000 data series with 1500 points in each. </p> <p>They form a (1000x1500) size Numpy array created using np.zeros((1500, 1000)) and then filled with the data. </p> <p>Now what if I want the array to grow to say 1600 x 1100? Do I have to add arrays using hstack and vstack or is there a better way?</...
6
2009-12-15T20:02:00Z
1,915,966
<p>This should do what you want (<em>ie</em>, using 3x3 array and 4x4 array to represent the two arrays in the OP)</p> <pre><code>&gt;&gt;&gt; import numpy as NP &gt;&gt;&gt; a = NP.random.randint(0, 10, 9).reshape(3, 3) &gt;&gt;&gt; a &gt;&gt;&gt; array([[1, 2, 2], [7, 0, 7], [0, 3, 0]]) &gt;&g...
6
2009-12-16T16:42:54Z
[ "python", "arrays", "numpy", "reshape" ]
How do I add rows and columns to a NUMPY array?
1,909,994
<p>Hello I have a 1000 data series with 1500 points in each. </p> <p>They form a (1000x1500) size Numpy array created using np.zeros((1500, 1000)) and then filled with the data. </p> <p>Now what if I want the array to grow to say 1600 x 1100? Do I have to add arrays using hstack and vstack or is there a better way?</...
6
2009-12-15T20:02:00Z
1,916,520
<p>No matter what, you'll be stuck reallocating a chunk of memory, so it doesn't really matter if you use <code>arr.resize()</code>, <code>np.concatenate</code>, <code>hstack/vstack</code>, etc. Note that if you're accumulating a lot of data sequentially, Python lists are usually more efficient.</p>
2
2009-12-16T18:06:51Z
[ "python", "arrays", "numpy", "reshape" ]
lisp-style style `let` syntax in Python list-comprehensions
1,910,003
<p>Consider the following code:</p> <pre><code>&gt;&gt;&gt; colprint([ (name, versions[name][0].summary or '') for name in sorted(versions.keys()) ]) </code></pre> <p>What this code does is to print the elements of the dictionary <code>versions</code> in ascending order of its <code>keys</code>, b...
6
2009-12-15T20:02:47Z
1,910,045
<p>So you're using "for x in [y]" as a substitute for "let x y".</p> <p>Trying to emulate language's syntax in another language is never a good idea. I think that the original version is much clearer.</p>
4
2009-12-15T20:09:34Z
[ "refactoring", "lisp", "python" ]
lisp-style style `let` syntax in Python list-comprehensions
1,910,003
<p>Consider the following code:</p> <pre><code>&gt;&gt;&gt; colprint([ (name, versions[name][0].summary or '') for name in sorted(versions.keys()) ]) </code></pre> <p>What this code does is to print the elements of the dictionary <code>versions</code> in ascending order of its <code>keys</code>, b...
6
2009-12-15T20:02:47Z
1,910,132
<p>Why not exploit tuples?</p> <pre><code>colprint([(name, version[0].summary or '') for (name, version) in sorted(versions.iteritems())]) </code></pre> <p>or, even</p> <pre><code>colprint(sorted([(name, version[0].summary or '') for (name, version) in versions.iteritems()])) </code></pre> <p>Als...
7
2009-12-15T20:22:15Z
[ "refactoring", "lisp", "python" ]
lisp-style style `let` syntax in Python list-comprehensions
1,910,003
<p>Consider the following code:</p> <pre><code>&gt;&gt;&gt; colprint([ (name, versions[name][0].summary or '') for name in sorted(versions.keys()) ]) </code></pre> <p>What this code does is to print the elements of the dictionary <code>versions</code> in ascending order of its <code>keys</code>, b...
6
2009-12-15T20:02:47Z
1,910,159
<p>You can move the sorting to the end to avoid some intermediate lists.</p> <p>This looks a bit nicer i guess:</p> <pre><code>colprint(sorted( (name, version[0].summary or '') for (name,version) in versions.iteritems()) )) </code></pre> <p>Python3 can do even better:</p> <pre><code>colprint(sor...
0
2009-12-15T20:27:37Z
[ "refactoring", "lisp", "python" ]
lisp-style style `let` syntax in Python list-comprehensions
1,910,003
<p>Consider the following code:</p> <pre><code>&gt;&gt;&gt; colprint([ (name, versions[name][0].summary or '') for name in sorted(versions.keys()) ]) </code></pre> <p>What this code does is to print the elements of the dictionary <code>versions</code> in ascending order of its <code>keys</code>, b...
6
2009-12-15T20:02:47Z
1,911,743
<p>I wouldn't use the "tricky for clause" (or "let-equivalent") in most cases, but I would if it's the natural way to avoid repetition, especially costly repetition. E.g.</p> <pre><code>xs = [(y, y*1.2, y-3.4) for z in zs for y in [somefun(z)] ] </code></pre> <p>looks much better to me than calling <code>somefun</co...
5
2009-12-16T01:41:24Z
[ "refactoring", "lisp", "python" ]
lisp-style style `let` syntax in Python list-comprehensions
1,910,003
<p>Consider the following code:</p> <pre><code>&gt;&gt;&gt; colprint([ (name, versions[name][0].summary or '') for name in sorted(versions.keys()) ]) </code></pre> <p>What this code does is to print the elements of the dictionary <code>versions</code> in ascending order of its <code>keys</code>, b...
6
2009-12-15T20:02:47Z
1,981,059
<p>As Tordek says, you can use <code>items()</code> or <code>iteritems()</code> in this case to avoid the issue:</p> <pre><code>colprint(sorted((name, packages[0].summary or '') for (name, packages) in versions.items())) </code></pre> <p>Moving the sorting outside is a nice touch.</p> <p>[Note that t...
1
2009-12-30T16:29:15Z
[ "refactoring", "lisp", "python" ]
facebook app in python showing file list
1,910,131
<p>i am making a facebook app in python with django. now i have successfully resolved the callback url to my localhost account. but the app is not displaying on facebook.</p> <p>when i navigate to apps.facebook.com/'myappname', it authenticates and then displays the file list on project folder?</p>
0
2009-12-15T20:22:14Z
1,910,419
<p>Based on your comments, I think I see what's going on. 192.168.2.2 is not a valid URL. That is a local network IP, and cannot be accessed from outside your network.</p> <p>You need to set your Canvas Callback URL to the external IP address of your modem.</p>
3
2009-12-15T21:09:25Z
[ "python", "django", "facebook" ]
Need to add httpd support to this wxPython code
1,910,226
<p>I need to add httpd support to this sample wxpython code.</p> <p>It parses the url and display different images.</p> <p>What's the easiest way to do this?</p> <pre><code>import wx a = wx.PySimpleApp() wximg = wx.Image('w.png',wx.BITMAP_TYPE_PNG) wxbmp=wximg.ConvertToBitmap() f = wx.Frame(None, -1, "Show JPEG demo...
0
2009-12-15T20:38:29Z
1,918,115
<p>Problem solved. </p> <p>Need to add </p> <pre><code>from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer .... class w_HttpThread(threading.Thread): def __init__(self, win): ... </code></pre> <p>On particular URL, do wx.PostEvent to the wx windows.</p> <p>wxWindows code will update the wi...
0
2009-12-16T22:03:52Z
[ "python", "wxpython", "apache" ]
Dynamically build and return a Python list in a C Python extension
1,910,258
<p>I'm writing a <a href="http://docs.python.org/extending/extending.html" rel="nofollow">Python extension in C</a>, and I'm trying to figure out how to dynamically build and return a Python list using my extension. I know how to build a list of predetermined size using <a href="http://docs.python.org/c-api/arg.html#P...
3
2009-12-15T20:43:06Z
1,910,394
<p>How about using <code>PyList_Append</code>, just like (modulo reference management and whatnot) you'd use <code>.append</code> in Python?</p> <p>Looking at the <a href="http://docs.python.org/c-api/list.html" rel="nofollow">list API</a> docs may help as well.</p>
4
2009-12-15T21:05:05Z
[ "python", "cpython", "cextension" ]
Unicode filenames on Windows with Python & subprocess.Popen()
1,910,275
<p>Why does the following occur:</p> <pre><code>&gt;&gt;&gt; u'\u0308'.encode('mbcs') #UMLAUT '\xa8' &gt;&gt;&gt; u'\u041A'.encode('mbcs') #CYRILLIC CAPITAL LETTER KA '?' &gt;&gt;&gt; </code></pre> <p>I have a Python application accepting filenames from the operating system. It works for some international users...
11
2009-12-15T20:45:14Z
1,910,462
<p><a href="http://docs.python.org/library/sys.html#sys.getfilesystemencoding" rel="nofollow">Docs for sys.getfilesystemencoding()</a> say that for Windows NT and later, file names are natively Unicode. If you have a valid unicode file name, why would you bother encoding it using mbcs?</p> <p><a href="http://docs.pyth...
3
2009-12-15T21:17:48Z
[ "python", "windows", "unicode" ]
Unicode filenames on Windows with Python & subprocess.Popen()
1,910,275
<p>Why does the following occur:</p> <pre><code>&gt;&gt;&gt; u'\u0308'.encode('mbcs') #UMLAUT '\xa8' &gt;&gt;&gt; u'\u041A'.encode('mbcs') #CYRILLIC CAPITAL LETTER KA '?' &gt;&gt;&gt; </code></pre> <p>I have a Python application accepting filenames from the operating system. It works for some international users...
11
2009-12-15T20:45:14Z
1,972,535
<p>If you need to pass the name of an existing file, then you might have a better chance of success by passing the 8.3 version of the Unicode filename.</p> <p>You need to have the <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">pywin32</a> package installed, then you can do:</p> <pre><code>&gt;&gt;&...
2
2009-12-29T01:54:30Z
[ "python", "windows", "unicode" ]
Unicode filenames on Windows with Python & subprocess.Popen()
1,910,275
<p>Why does the following occur:</p> <pre><code>&gt;&gt;&gt; u'\u0308'.encode('mbcs') #UMLAUT '\xa8' &gt;&gt;&gt; u'\u041A'.encode('mbcs') #CYRILLIC CAPITAL LETTER KA '?' &gt;&gt;&gt; </code></pre> <p>I have a Python application accepting filenames from the operating system. It works for some international users...
11
2009-12-15T20:45:14Z
9,113,914
<p>In Py3K - at least from Python 3.2 - <code>subprocess.Popen</code> and <code>sys.argv</code> work consistently with (default unicode) strings on Windows. <code>CreateProcessW</code> and <code>GetCommandLineW</code> are used obviously. </p> <p>In Python - up to v2.7.2 at least - <code>subprocess.Popen</code> is bugg...
6
2012-02-02T14:22:24Z
[ "python", "windows", "unicode" ]
Django: Problems with serving static files with non ascii names
1,910,427
<p>I've moved my django application from one server to another, and spotted strange bug with media after it:</p> <pre><code>Traceback (most recent call last): File "/usr/lib/python2.5/site- packages/Django-1.1.1-py2.5.egg/django/core/handlers/base.py", line 92, in get_response response = callback(request, *callba...
2
2009-12-15T21:11:29Z
1,910,642
<p>First of all, don't have Django serve your static files. See this: <a href="http://docs.djangoproject.com/en/dev/howto/static-files/" rel="nofollow">http://docs.djangoproject.com/en/dev/howto/static-files/</a></p>
1
2009-12-15T21:48:28Z
[ "python", "django" ]
Django: Problems with serving static files with non ascii names
1,910,427
<p>I've moved my django application from one server to another, and spotted strange bug with media after it:</p> <pre><code>Traceback (most recent call last): File "/usr/lib/python2.5/site- packages/Django-1.1.1-py2.5.egg/django/core/handlers/base.py", line 92, in get_response response = callback(request, *callba...
2
2009-12-15T21:11:29Z
29,412,508
<p>In Django documentation it's written that this may be connected to Apache settings (discovered via <a href="https://groups.google.com/d/msg/django-users/LVzSXVy6TSE/AZEp90hQiRoJ" rel="nofollow">here</a>).</p> <p><a href="https://docs.djangoproject.com/en/1.4/howto/deployment/modpython/#if-you-get-a-unicodeencodeerr...
0
2015-04-02T12:03:45Z
[ "python", "django" ]
csv utf-8 writer - compability with python2.4
1,910,610
<p>At the bottom of this manual <a href="http://docs.python.org/library/csv.html" rel="nofollow">http://docs.python.org/library/csv.html</a> we have example of UnicodeWriter</p> <p>But how can i use this example in python 2.4</p> <p>I got exception about codecs.getincrementalencoder(encoding)(). Property getincremen...
3
2009-12-15T21:41:42Z
1,910,730
<p>Not sure if it will work, but try to use <code>codecs.getencoder</code> instead.</p>
1
2009-12-15T22:04:30Z
[ "python", "utf-8", "csv" ]
More efficient way to count intersections?
1,910,744
<p>I have a list of 300000 lists (fiber tracks), where each track is a list of (x,y,z) tuples/coordinates:</p> <pre><code>tracks= [[(1,2,3),(3,2,4),...] [(4,2,1),(5,7,3),...] ... ] </code></pre> <p>I also have a group of masks, where each mask is defined as a list of (x,y,z) tuples/coordinates: </p> <pre><code>mas...
5
2009-12-15T22:07:01Z
1,910,826
<p>You can probably start by combining the two functions to create both results at once. Also there's no need to make a list of the combinations before looping, as it is already a generator, and that might save you some time.</p> <pre><code>def mask_connectivity_matrix_and_tracks(tracks,masks,masks_coords_list): c...
0
2009-12-15T22:19:13Z
[ "python", "algorithm", "set" ]
More efficient way to count intersections?
1,910,744
<p>I have a list of 300000 lists (fiber tracks), where each track is a list of (x,y,z) tuples/coordinates:</p> <pre><code>tracks= [[(1,2,3),(3,2,4),...] [(4,2,1),(5,7,3),...] ... ] </code></pre> <p>I also have a group of masks, where each mask is defined as a list of (x,y,z) tuples/coordinates: </p> <pre><code>mas...
5
2009-12-15T22:07:01Z
1,910,853
<p>If you stored the each mask set of points: (1,2,3), (1,2,4), (1,3,1) as a dictionary like this: <code>{1: [{2: set([3, 4])}, {3: set([1])}]}</code>, you might end up being able to check for matches faster...but maybe not. </p>
0
2009-12-15T22:22:01Z
[ "python", "algorithm", "set" ]
More efficient way to count intersections?
1,910,744
<p>I have a list of 300000 lists (fiber tracks), where each track is a list of (x,y,z) tuples/coordinates:</p> <pre><code>tracks= [[(1,2,3),(3,2,4),...] [(4,2,1),(5,7,3),...] ... ] </code></pre> <p>I also have a group of masks, where each mask is defined as a list of (x,y,z) tuples/coordinates: </p> <pre><code>mas...
5
2009-12-15T22:07:01Z
1,910,866
<p>A minor optimization (same big-O, sligthly smaller multiplier) can be had by removing redundant operations:</p> <ol> <li>don't call <code>set</code> so many times on each track and mask: call it once per track and once per mask, to set up auxiliary "parallel" lists of sets, then work on those</li> <li><code>if any(...
0
2009-12-15T22:24:29Z
[ "python", "algorithm", "set" ]
More efficient way to count intersections?
1,910,744
<p>I have a list of 300000 lists (fiber tracks), where each track is a list of (x,y,z) tuples/coordinates:</p> <pre><code>tracks= [[(1,2,3),(3,2,4),...] [(4,2,1),(5,7,3),...] ... ] </code></pre> <p>I also have a group of masks, where each mask is defined as a list of (x,y,z) tuples/coordinates: </p> <pre><code>mas...
5
2009-12-15T22:07:01Z
1,910,903
<p>Lame to suggest yet another incremental improvement that might be made, I know, but:</p> <p>Sets of small integers can be modeled as bit-vectors using Python's long ints. Suppose you replace each tuple with a small integer id, then convert each track and each set of mask-coords into a set of those small ids. You co...
0
2009-12-15T22:31:58Z
[ "python", "algorithm", "set" ]
More efficient way to count intersections?
1,910,744
<p>I have a list of 300000 lists (fiber tracks), where each track is a list of (x,y,z) tuples/coordinates:</p> <pre><code>tracks= [[(1,2,3),(3,2,4),...] [(4,2,1),(5,7,3),...] ... ] </code></pre> <p>I also have a group of masks, where each mask is defined as a list of (x,y,z) tuples/coordinates: </p> <pre><code>mas...
5
2009-12-15T22:07:01Z
1,911,105
<p>OK, I think I finally have something that will reduce the complexity. This code should really fly compared to what you've got.</p> <p>It seems like first you need to know which tracks coincide with which masks, the <a href="http://en.wikipedia.org/wiki/Incidence%5Fmatrix" rel="nofollow">incidence matrix</a>.</p> <...
1
2009-12-15T23:06:21Z
[ "python", "algorithm", "set" ]
More efficient way to count intersections?
1,910,744
<p>I have a list of 300000 lists (fiber tracks), where each track is a list of (x,y,z) tuples/coordinates:</p> <pre><code>tracks= [[(1,2,3),(3,2,4),...] [(4,2,1),(5,7,3),...] ... ] </code></pre> <p>I also have a group of masks, where each mask is defined as a list of (x,y,z) tuples/coordinates: </p> <pre><code>mas...
5
2009-12-15T22:07:01Z
1,911,757
<p>Linearize the voxel coordinates, and put them into two scipy.sparse.sparse.csc matrices. </p> <p>Let v be the number of voxels, m the number of masks, and t the number of tracks.<br> Let M be the mask csc matrix, size (m x v), where a 1 at (i,j) means mask i overlaps voxel j.<br> Let T be the track csc matrix, siz...
2
2009-12-16T01:46:14Z
[ "python", "algorithm", "set" ]
Facebook application Django server connection
1,910,846
<p>I am creating a Facebook application in Python and Django. I followed all the instructions mentioned on this page: <a href="http://wiki.developers.facebook.com/index.php/User:PyFacebook_Tutorial" rel="nofollow">http://wiki.developers.facebook.com/index.php/User:PyFacebook_Tutorial</a>. But it ended up giving me this...
1
2009-12-15T22:21:17Z
1,911,344
<p>You need to troubleshoot from somewhere outside your development box and local network. You need to figure out if this is a DNS issue, a port forwarding issue, or perhaps an issue on your dev. box (is a local firewall blocking the requests?).</p> <p>From a network location outside your home network, connect using t...
0
2009-12-15T23:56:41Z
[ "python", "django", "facebook", "networking" ]
How should I deal with a circular import in Google App Engine?
1,910,886
<p>If I have "a.py"</p> <pre><code>from google.appengine.ext import db class A(db.Model): db.ReferenceProperty(b.B) ...other stuff </code></pre> <p>and another file "b.py"</p> <pre><code>from google.appengine.ext import db class B(db.Model): db.ReferenceProperty(a.A) ...other stuff </code></pre> <p>...
9
2009-12-15T22:29:35Z
1,911,251
<p>What happens if you define both models in the same module? e.g. a_b.py</p>
1
2009-12-15T23:33:22Z
[ "python", "google-app-engine" ]
How should I deal with a circular import in Google App Engine?
1,910,886
<p>If I have "a.py"</p> <pre><code>from google.appengine.ext import db class A(db.Model): db.ReferenceProperty(b.B) ...other stuff </code></pre> <p>and another file "b.py"</p> <pre><code>from google.appengine.ext import db class B(db.Model): db.ReferenceProperty(a.A) ...other stuff </code></pre> <p>...
9
2009-12-15T22:29:35Z
1,911,448
<p>According to the <a href="http://code.google.com/appengine/docs/python/datastore/entitiesandmodels.html#References" rel="nofollow">documentation</a>:</p> <blockquote> <p>ReferenceProperty has another handy feature: back-references. When a model has a ReferenceProperty to another model, each referenced entit...
1
2009-12-16T00:19:53Z
[ "python", "google-app-engine" ]
How should I deal with a circular import in Google App Engine?
1,910,886
<p>If I have "a.py"</p> <pre><code>from google.appengine.ext import db class A(db.Model): db.ReferenceProperty(b.B) ...other stuff </code></pre> <p>and another file "b.py"</p> <pre><code>from google.appengine.ext import db class B(db.Model): db.ReferenceProperty(a.A) ...other stuff </code></pre> <p>...
9
2009-12-15T22:29:35Z
1,912,396
<p>The workaround is to have a ReferenceProperty in at least one of the models that doesn't restrict itself to a particular class, and then enforce only referencing that class in your own code.</p> <p>e.g., </p> <pre><code>class A(db.Model): b = db.ReferenceProperty() class B(db.Model): a = db.ReferenceProperty(...
7
2009-12-16T04:56:50Z
[ "python", "google-app-engine" ]
How should I deal with a circular import in Google App Engine?
1,910,886
<p>If I have "a.py"</p> <pre><code>from google.appengine.ext import db class A(db.Model): db.ReferenceProperty(b.B) ...other stuff </code></pre> <p>and another file "b.py"</p> <pre><code>from google.appengine.ext import db class B(db.Model): db.ReferenceProperty(a.A) ...other stuff </code></pre> <p>...
9
2009-12-15T22:29:35Z
15,912,496
<p>Technically speaking, you can replace the circular dependency with an intersection table.</p> <p>In my case, I have denormalized models, <code>Player</code> and <code>Match</code>.</p> <p>The relationship between Matches and Players is many-to-many, (a Player has played one-or-more matches, and a Match references ...
0
2013-04-09T21:03:47Z
[ "python", "google-app-engine" ]
Inlines Python/Django technique for objects
1,910,953
<p>I am reading the source code of the Django application <em>blog</em> at <code>git://github.com/nathanborror/django-basic-apps.git</code>.</p> <p><strong>How do you read the following Django code?</strong></p> <pre><code>{% tags_for_object object as tag_list %} </code></pre> <p><strong>My attempt:</strong> Make th...
0
2009-12-15T22:38:06Z
1,911,001
<p>It looks like <code>tags_for_object</code> is the template tag from the <a href="http://code.google.com/p/django-tagging/" rel="nofollow">django-tagging</a> application.</p> <p>From the django-tagging <a href="http://code.google.com/p/django-tagging/source/browse/trunk/docs/overview.txt" rel="nofollow">documentatio...
4
2009-12-15T22:46:18Z
[ "python", "django" ]
concatenate items in dictionary in python using list comprehension
1,911,014
<p>EDIT: Clarified the question a bit</p> <p>How can I get a string from a dictionary with the format</p> <pre><code>key1 = value1 key2 = value2 </code></pre> <p>in a relatively fast way ? (relative to plain concatenation)</p>
4
2009-12-15T22:49:10Z
1,911,019
<p>Like this:</p> <pre><code>DATA = {'key1': 'value1', 'key2': 'value2'} print "\n".join(sorted(["%s = %s" % (k,v) for (k,v) in DATA.iteritems()])) </code></pre>
2
2009-12-15T22:50:58Z
[ "python", "list-comprehension" ]
concatenate items in dictionary in python using list comprehension
1,911,014
<p>EDIT: Clarified the question a bit</p> <p>How can I get a string from a dictionary with the format</p> <pre><code>key1 = value1 key2 = value2 </code></pre> <p>in a relatively fast way ? (relative to plain concatenation)</p>
4
2009-12-15T22:49:10Z
1,911,025
<pre><code>print '\n'.join('%s = %s' % (key, value) for key, value in d.iteritems()) </code></pre>
7
2009-12-15T22:51:32Z
[ "python", "list-comprehension" ]
concatenate items in dictionary in python using list comprehension
1,911,014
<p>EDIT: Clarified the question a bit</p> <p>How can I get a string from a dictionary with the format</p> <pre><code>key1 = value1 key2 = value2 </code></pre> <p>in a relatively fast way ? (relative to plain concatenation)</p>
4
2009-12-15T22:49:10Z
1,911,027
<p>There's no reason to use list comprehension here.</p> <p>Python 3.x:</p> <pre><code>for k,v in mydict.items(): print(k, '=', v) </code></pre> <p>Python 2.x:</p> <pre><code>for k,v in mydict.iteritems(): print k, '=', v </code></pre> <p><strong>EDIT because of comment by OP in another answer:</strong></p> <...
9
2009-12-15T22:51:40Z
[ "python", "list-comprehension" ]
concatenate items in dictionary in python using list comprehension
1,911,014
<p>EDIT: Clarified the question a bit</p> <p>How can I get a string from a dictionary with the format</p> <pre><code>key1 = value1 key2 = value2 </code></pre> <p>in a relatively fast way ? (relative to plain concatenation)</p>
4
2009-12-15T22:49:10Z
1,911,076
<p><em>Explicit is better than implicit</em>: list comprehension is a <a href="http://www.python.org/dev/peps/pep-0202/" rel="nofollow">way to create list</a>, not to avoid loops.<br> From PEP 202:</p> <blockquote> <p>List comprehensions provide a more concise way to create lists in situations where map() and fi...
3
2009-12-15T23:01:42Z
[ "python", "list-comprehension" ]
concatenate items in dictionary in python using list comprehension
1,911,014
<p>EDIT: Clarified the question a bit</p> <p>How can I get a string from a dictionary with the format</p> <pre><code>key1 = value1 key2 = value2 </code></pre> <p>in a relatively fast way ? (relative to plain concatenation)</p>
4
2009-12-15T22:49:10Z
1,911,186
<p>I prefer the pythonic way:</p> <pre><code>mydict = {'a':1, 'b':2, 'c':3} for (key, value) in mydict.items(): print key, '=', value </code></pre>
0
2009-12-15T23:21:10Z
[ "python", "list-comprehension" ]
Is there a better way to compare dictionary values
1,911,273
<p>I am currently using the following function to compare dictionary values. Is there a faster or better way to do it?</p> <pre><code>match = True for keys in dict1: if dict1[keys] != dict2[keys]: match = False print keys print dict1[keys], print '-&gt;' , print dict2[keys]...
9
2009-12-15T23:39:12Z
1,911,299
<p>If you're just comparing for equality, you can just do this:</p> <pre><code>if not dict1 == dict2: match = False </code></pre> <p>Otherwise, the only major problem I see is that you're going to get a KeyError if there is a key in dict1 that is not in dict2, so you may want to do something like this:</p> <pre>...
1
2009-12-15T23:45:07Z
[ "python", "dictionary" ]
Is there a better way to compare dictionary values
1,911,273
<p>I am currently using the following function to compare dictionary values. Is there a faster or better way to do it?</p> <pre><code>match = True for keys in dict1: if dict1[keys] != dict2[keys]: match = False print keys print dict1[keys], print '-&gt;' , print dict2[keys]...
9
2009-12-15T23:39:12Z
1,911,300
<pre><code>&gt;&gt;&gt; a = {'x': 1, 'y': 2} &gt;&gt;&gt; b = {'y': 2, 'x': 1} &gt;&gt;&gt; print a == b True &gt;&gt;&gt; c = {'z': 1} &gt;&gt;&gt; print a == c False &gt;&gt;&gt; </code></pre>
1
2009-12-15T23:45:23Z
[ "python", "dictionary" ]
Is there a better way to compare dictionary values
1,911,273
<p>I am currently using the following function to compare dictionary values. Is there a faster or better way to do it?</p> <pre><code>match = True for keys in dict1: if dict1[keys] != dict2[keys]: match = False print keys print dict1[keys], print '-&gt;' , print dict2[keys]...
9
2009-12-15T23:39:12Z
1,911,302
<p>Uhm, you are describing <code>dict1 == dict2</code> ( check if boths dicts are equal )</p> <p>But what your code does is <code>all( dict1[k]==dict2[k] for k in dict1 )</code> ( check if all entries in dict1 are equal to those in dict2 )</p>
4
2009-12-15T23:45:35Z
[ "python", "dictionary" ]
Is there a better way to compare dictionary values
1,911,273
<p>I am currently using the following function to compare dictionary values. Is there a faster or better way to do it?</p> <pre><code>match = True for keys in dict1: if dict1[keys] != dict2[keys]: match = False print keys print dict1[keys], print '-&gt;' , print dict2[keys]...
9
2009-12-15T23:39:12Z
1,911,722
<p>If the dicts have identical sets of keys and you need all those prints for any value difference, there isn't much you can do; maybe something like:</p> <pre><code>diffkeys = [k for k in dict1 if dict1[k] != dict2[k]] for k in diffkeys: print k, ':', dict1[k], '-&gt;', dict2[k] </code></pre> <p>pretty much equiva...
15
2009-12-16T01:36:09Z
[ "python", "dictionary" ]
Is there a better way to compare dictionary values
1,911,273
<p>I am currently using the following function to compare dictionary values. Is there a faster or better way to do it?</p> <pre><code>match = True for keys in dict1: if dict1[keys] != dict2[keys]: match = False print keys print dict1[keys], print '-&gt;' , print dict2[keys]...
9
2009-12-15T23:39:12Z
1,911,765
<p>You can use sets for this too</p> <pre><code>&gt;&gt;&gt; a = {'x': 1, 'y': 2} &gt;&gt;&gt; b = {'y': 2, 'x': 1} &gt;&gt;&gt; set(a.iteritems())-set(b.iteritems()) set([]) &gt;&gt;&gt; a['y']=3 &gt;&gt;&gt; set(a.iteritems())-set(b.iteritems()) set([('y', 3)]) &gt;&gt;&gt; set(b.iteritems())-set(a.iteritems()) set(...
6
2009-12-16T01:47:15Z
[ "python", "dictionary" ]
Is there a better way to compare dictionary values
1,911,273
<p>I am currently using the following function to compare dictionary values. Is there a faster or better way to do it?</p> <pre><code>match = True for keys in dict1: if dict1[keys] != dict2[keys]: match = False print keys print dict1[keys], print '-&gt;' , print dict2[keys]...
9
2009-12-15T23:39:12Z
5,635,309
<p>If the true intent of the question is the comparison between dicts (rather than printing differences), the answer is</p> <pre><code>dict1 == dict2 </code></pre> <p>This has been mentioned before, but I felt it was slightly drowning in other bits of information. It might appear superficial, but the value comparison...
36
2011-04-12T12:39:48Z
[ "python", "dictionary" ]
Is there a better way to compare dictionary values
1,911,273
<p>I am currently using the following function to compare dictionary values. Is there a faster or better way to do it?</p> <pre><code>match = True for keys in dict1: if dict1[keys] != dict2[keys]: match = False print keys print dict1[keys], print '-&gt;' , print dict2[keys]...
9
2009-12-15T23:39:12Z
31,978,554
<p>If your dictionaries are deeply nested and if they contain different types of collections, you could convert them to json string and compare.</p> <pre><code>import json match = (json.dumps(dict1) == json.dumps(dict2)) </code></pre> <p>caveat- this solution may not work if your dictionaries have binary strings in t...
0
2015-08-13T02:32:33Z
[ "python", "dictionary" ]
Is there a better way to compare dictionary values
1,911,273
<p>I am currently using the following function to compare dictionary values. Is there a faster or better way to do it?</p> <pre><code>match = True for keys in dict1: if dict1[keys] != dict2[keys]: match = False print keys print dict1[keys], print '-&gt;' , print dict2[keys]...
9
2009-12-15T23:39:12Z
36,909,783
<p>Not sure if this helps but in my app I had to check if a dictionary has changed.</p> <p>Doing this will not work since basically it's still the same object:</p> <pre><code>val={'A':1,'B':2} old_val=val val['A']=10 if old_val != val: print('changed') </code></pre> <p>Using copy/deepcopy works:</p> <pre><code>i...
0
2016-04-28T08:53:37Z
[ "python", "dictionary" ]
How do I get list of methods in a Python class?
1,911,281
<p>I want to iterate through the methods in a class, or handle class or instance objects differently based on the methods present. How do I get a list of class methods?</p> <p>Also see:</p> <ul> <li><a href="http://stackoverflow.com/questions/1280787/how-can-i-list-the-methods-in-a-python-2-5-module">How can I list t...
161
2009-12-15T23:40:41Z
1,911,287
<p>An example (listing the methods of the <code>optparse.OptionParser</code> class):</p> <pre><code>&gt;&gt;&gt; from optparse import OptionParser &gt;&gt;&gt; import inspect &gt;&gt;&gt; inspect.getmembers(OptionParser, predicate=inspect.ismethod) [([('__init__', &lt;unbound method OptionParser.__init__&gt;), ... ('...
165
2009-12-15T23:41:56Z
[ "python" ]
How do I get list of methods in a Python class?
1,911,281
<p>I want to iterate through the methods in a class, or handle class or instance objects differently based on the methods present. How do I get a list of class methods?</p> <p>Also see:</p> <ul> <li><a href="http://stackoverflow.com/questions/1280787/how-can-i-list-the-methods-in-a-python-2-5-module">How can I list t...
161
2009-12-15T23:40:41Z
1,911,291
<p>Try the property <code>__dict__</code>.</p>
19
2009-12-15T23:42:38Z
[ "python" ]
How do I get list of methods in a Python class?
1,911,281
<p>I want to iterate through the methods in a class, or handle class or instance objects differently based on the methods present. How do I get a list of class methods?</p> <p>Also see:</p> <ul> <li><a href="http://stackoverflow.com/questions/1280787/how-can-i-list-the-methods-in-a-python-2-5-module">How can I list t...
161
2009-12-15T23:40:41Z
1,911,313
<p>There is the <code>dir(theobject)</code> method to list all the fields and methods of your object (as a tuple) and the inspect module (as codeape write) to list the fields and methods with their doc (in """).</p> <p>Because everything (even fields) might be called in Python, I'm not sure there is a built-in functio...
80
2009-12-15T23:49:18Z
[ "python" ]
How do I get list of methods in a Python class?
1,911,281
<p>I want to iterate through the methods in a class, or handle class or instance objects differently based on the methods present. How do I get a list of class methods?</p> <p>Also see:</p> <ul> <li><a href="http://stackoverflow.com/questions/1280787/how-can-i-list-the-methods-in-a-python-2-5-module">How can I list t...
161
2009-12-15T23:40:41Z
7,478,804
<p>Note that you need to consider whether you want methods from base classes which are inherited (but not overridden) included in the result. The <code>dir()</code> and <code>inspect.getmembers()</code> operations do include base class methods, but use of the <code>__dict__</code> attribute does not.</p>
6
2011-09-20T00:21:56Z
[ "python" ]
How do I get list of methods in a Python class?
1,911,281
<p>I want to iterate through the methods in a class, or handle class or instance objects differently based on the methods present. How do I get a list of class methods?</p> <p>Also see:</p> <ul> <li><a href="http://stackoverflow.com/questions/1280787/how-can-i-list-the-methods-in-a-python-2-5-module">How can I list t...
161
2009-12-15T23:40:41Z
24,715,094
<pre><code>def find_defining_class(obj, meth_name): for ty in type(obj).mro(): if meth_name in ty.__dict__: return ty </code></pre> <p>So</p> <pre><code>print find_defining_class(car, 'speedometer') </code></pre> <p><em>Think Python page 210</em></p>
-1
2014-07-12T16:41:26Z
[ "python" ]
How do I get list of methods in a Python class?
1,911,281
<p>I want to iterate through the methods in a class, or handle class or instance objects differently based on the methods present. How do I get a list of class methods?</p> <p>Also see:</p> <ul> <li><a href="http://stackoverflow.com/questions/1280787/how-can-i-list-the-methods-in-a-python-2-5-module">How can I list t...
161
2009-12-15T23:40:41Z
24,795,848
<p>I know this is an old post, but just wrote this function and will leave it here is case someone stumbles looking for an answer:</p> <pre><code>def classMethods(the_class,class_only=False,instance_only=False,exclude_internal=True): def acceptMethod(tup): #internal function that analyzes the tuples retur...
0
2014-07-17T05:57:08Z
[ "python" ]
How do I get list of methods in a Python class?
1,911,281
<p>I want to iterate through the methods in a class, or handle class or instance objects differently based on the methods present. How do I get a list of class methods?</p> <p>Also see:</p> <ul> <li><a href="http://stackoverflow.com/questions/1280787/how-can-i-list-the-methods-in-a-python-2-5-module">How can I list t...
161
2009-12-15T23:40:41Z
29,333,454
<p>you can also import the FunctionType from types and test it with the <code>class.__dict__</code>:</p> <pre class="lang-py prettyprint-override"><code>from types import FunctionType class Foo: def bar(self): pass def baz(self): pass def methods(cls): return [x for x, y in cls.__dict__.items() if type(y...
6
2015-03-29T18:58:06Z
[ "python" ]
How do I get list of methods in a Python class?
1,911,281
<p>I want to iterate through the methods in a class, or handle class or instance objects differently based on the methods present. How do I get a list of class methods?</p> <p>Also see:</p> <ul> <li><a href="http://stackoverflow.com/questions/1280787/how-can-i-list-the-methods-in-a-python-2-5-module">How can I list t...
161
2009-12-15T23:40:41Z
39,061,905
<p>Python 3.x answer without external libraries</p> <pre><code>method_list = [func for func in dir(Foo) if callable(getattr(Foo, func))] </code></pre> <p>dunder-excluded result:</p> <pre><code>method_list = [func for func in dir(Foo) if callable(getattr(Foo, func)) and not func.startswith("__")] </code></pre>
1
2016-08-21T07:33:18Z
[ "python" ]
How do add multiple fields to a Form when trying to validate data?
1,911,383
<p>What is the best way to have the IP address be included with the Form is_valid. Let me start with some code examples.</p> <p>urls.py</p> <pre><code>from django.conf.urls.defaults import * from testpost.views import TestPost urlpatterns = patterns('', (r'^djtestforms/', TestPost), ) </code></pre> <p>model.py<...
0
2009-12-16T00:04:26Z
1,911,493
<p>Save with <code>commit=False</code></p> <pre><code>form=TestPostForm(data=request.POST) if form.is_valid(): object=form.save(commit=False) object.ip_address=request.META['REMOTE_ADDR'] object.save() </code></pre> <p>You will probably need to set <code>blank=True</code> in the model or <code>required=False</c...
-1
2009-12-16T00:29:29Z
[ "python", "django" ]
How do add multiple fields to a Form when trying to validate data?
1,911,383
<p>What is the best way to have the IP address be included with the Form is_valid. Let me start with some code examples.</p> <p>urls.py</p> <pre><code>from django.conf.urls.defaults import * from testpost.views import TestPost urlpatterns = patterns('', (r'^djtestforms/', TestPost), ) </code></pre> <p>model.py<...
0
2009-12-16T00:04:26Z
1,911,732
<p>@czarchaic - Your'e answer gave me a good hint on what to do. I changed the model so that blank=True for ip_address, and then did a </p> <pre><code>f = TestPostForm(request.POST) f.data['ip_address']=request.META['REMOTE_ADDR'] </code></pre> <p>After that is_valid worked. Thanks.</p>
2
2009-12-16T01:39:21Z
[ "python", "django" ]
python checking for files
1,911,396
<p>learning python there. I want to write a script to check if my webserver has picture named in the root 123.jpg</p> <p>I have:</p> <pre><code>import urllib2 numeruks=100 adresiuks="http://localhost/" + str(numeruks) +".jpg" try: if numeruks &lt; 150: numeruks = numeruks + 1 urllib2.urlopen(adresiuks).r...
0
2009-12-16T00:08:00Z
1,911,463
<p>You can test for 404 in your attempts to access the URL (and without even having to issue a <code>read()</code>):</p> <pre><code>import urllib2 n = 123 try: url = 'http://localhost/%d.jpg' % n urllib2.urlopen(url) except urllib2.HTTPError, e: if e.code == 404: print '%d.jpg was not found' % n ...
1
2009-12-16T00:22:24Z
[ "python", "urllib2" ]
python checking for files
1,911,396
<p>learning python there. I want to write a script to check if my webserver has picture named in the root 123.jpg</p> <p>I have:</p> <pre><code>import urllib2 numeruks=100 adresiuks="http://localhost/" + str(numeruks) +".jpg" try: if numeruks &lt; 150: numeruks = numeruks + 1 urllib2.urlopen(adresiuks).r...
0
2009-12-16T00:08:00Z
1,911,529
<p>Is this code standing on its own? If so, you're missing a loop. Also, as codeape said, the indentation is wrong and you need an <code>except</code> or a <code>finally</code>.</p> <p>If you want to check all of the numbers between 100 and 150, you'll need to loop over them. Your code as it stands now only updates ...
1
2009-12-16T00:39:41Z
[ "python", "urllib2" ]
python checking for files
1,911,396
<p>learning python there. I want to write a script to check if my webserver has picture named in the root 123.jpg</p> <p>I have:</p> <pre><code>import urllib2 numeruks=100 adresiuks="http://localhost/" + str(numeruks) +".jpg" try: if numeruks &lt; 150: numeruks = numeruks + 1 urllib2.urlopen(adresiuks).r...
0
2009-12-16T00:08:00Z
1,911,546
<p>After you increment numeruks you should reset the adresiuks.</p> <p>I.E.:</p> <pre><code>adresiuks="http://localhost/" + str(numeruks) + ".jpg" try: if numeruks &lt; 150: numeruks = numeruks + 1 adresiuks = "http://localhost/" + str(numeruks) + ".jpg" print adresiuks ...
0
2009-12-16T00:43:21Z
[ "python", "urllib2" ]
Extract data from a website's list, without superfluous tags
1,911,442
<p>Working code: <a href="http://gist.github.com/257389" rel="nofollow">Google dictionary lookup via python and beautiful soup</a> -> simply execute and enter a word.</p> <p>I've quite simply extracted the first definition from a specific list item. However to get plain data, I've had to split my data at the line brea...
0
2009-12-16T00:18:58Z
1,912,355
<p>I think you are looking for findAll(text=True) this will extract the text from the tags </p> <pre><code>definitions = soup('ul')[0].findAll(text=True) </code></pre> <p>Will return a ist of all the text contents broken at the tag boundaries</p>
1
2009-12-16T04:45:04Z
[ "python", "html", "beautifulsoup", "extract" ]
How to completely sanitize a string of illegal characters in python?
1,911,548
<p>I have a feature of my program where the user can upload a csv file, which my program goes through and uses as input. I have one user complaining about a problem where his input is throwing up an error. The error is cause by there being an illegal character that is encoded wrong. The characters is below:</p> <pre><...
3
2009-12-16T00:44:00Z
1,911,575
<p>You can pass, "ignore" to skip invalid characters in .encode/.decode like <code>"ILLEGAL".decode("utf8","ignore")</code></p> <pre><code>&gt;&gt;&gt; "ILLEGA\xa0L".decode("utf8") ... UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 6: unexpected code byte &gt;&gt;&gt; "ILLEGA\xa0L".decode("utf8",...
4
2009-12-16T00:53:03Z
[ "python", "unicode" ]
How to completely sanitize a string of illegal characters in python?
1,911,548
<p>I have a feature of my program where the user can upload a csv file, which my program goes through and uses as input. I have one user complaining about a problem where his input is throwing up an error. The error is cause by there being an illegal character that is encoded wrong. The characters is below:</p> <pre><...
3
2009-12-16T00:44:00Z
17,698,481
<p>Declare the coding on the second line of your script. It really has to be second. Like</p> <pre><code>#!/usr/bin/python # coding=utf-8 </code></pre> <p>This might be enough to solve your problem all by itself. If not, see str.encode('utf-8') and str.decode('utf-8'). </p>
1
2013-07-17T11:22:00Z
[ "python", "unicode" ]
Python: How do I find why IDLE restarts?
1,911,615
<p>I am using python 2.5 on windows. All I am doing is unpickling a large file (18MB - a list of dictionaries) and modifiying some of its values. Now this works fine. But when I add a couple of prints, IDLE restarts. And weirdly enough it seems to be happening where I added the print. I figured this out commenting and ...
0
2009-12-16T01:04:08Z
1,911,639
<p>Enable the debugger and see if it tells you anything.</p>
0
2009-12-16T01:12:12Z
[ "python", "python-idle" ]
Python: How do I find why IDLE restarts?
1,911,615
<p>I am using python 2.5 on windows. All I am doing is unpickling a large file (18MB - a list of dictionaries) and modifiying some of its values. Now this works fine. But when I add a couple of prints, IDLE restarts. And weirdly enough it seems to be happening where I added the print. I figured this out commenting and ...
0
2009-12-16T01:04:08Z
1,911,641
<p>Have you tried running your script from the command line rather than IDLE? Open a command prompt and type <code>python</code> to enter the Python interpreter. See if it crashes there too.</p> <p>Secondly, you should try using the <a href="http://docs.python.org/library/pdb.html" rel="nofollow">pdb</a> module for ...
1
2009-12-16T01:12:33Z
[ "python", "python-idle" ]