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
How to serialize db.Model objects to json?
2,114,659
<p>When using</p> <pre><code>from django.utils import simplejson </code></pre> <p>on objects of types that derive from <code>db.Model</code> it throws exceptions. How to circumvent this?</p>
16
2010-01-22T02:53:41Z
2,124,395
<p>Since I could not find an appropriate solution I wrote my own, which is not exactly a JSON serializer, but a Javascript serializer</p> <pre><code>from google.appengine.ext import db from google.appengine.api.datastore_types import * def dumpStr(obj): return "'" + obj + "'" def dumps(obj): if isinstance(ob...
0
2010-01-23T19:26:18Z
[ "python", "google-app-engine", "simplejson" ]
How to serialize db.Model objects to json?
2,114,659
<p>When using</p> <pre><code>from django.utils import simplejson </code></pre> <p>on objects of types that derive from <code>db.Model</code> it throws exceptions. How to circumvent this?</p>
16
2010-01-22T02:53:41Z
2,124,952
<p>From what i can understand - and i am new to python - with google app engine the work around is to serialize the model object to a dictioanry python object and then use simple json to dump it as a json string - this makes no sense to me - maybe someone has the know to serialise to a ditionary (pickel?) Any help on t...
-1
2010-01-23T22:11:24Z
[ "python", "google-app-engine", "simplejson" ]
How to serialize db.Model objects to json?
2,114,659
<p>When using</p> <pre><code>from django.utils import simplejson </code></pre> <p>on objects of types that derive from <code>db.Model</code> it throws exceptions. How to circumvent this?</p>
16
2010-01-22T02:53:41Z
2,125,046
<p>Ok - my python not great so any help would be appreciated - You dont need to write a parser - this is the solution:</p> <p>add this utlity class <a href="http://code.google.com/p/google-app-engine-samples/source/browse/trunk/geochat/json.py?r=55">http://code.google.com/p/google-app-engine-samples/source/browse/trun...
14
2010-01-23T22:51:12Z
[ "python", "google-app-engine", "simplejson" ]
How to serialize db.Model objects to json?
2,114,659
<p>When using</p> <pre><code>from django.utils import simplejson </code></pre> <p>on objects of types that derive from <code>db.Model</code> it throws exceptions. How to circumvent this?</p>
16
2010-01-22T02:53:41Z
2,333,152
<p>The example provided by Jader Dias works fine for my concern after some twaeking. Remove the encode method as it contains a circular reference. The adjusted class should look like:</p> <pre><code>import datetime import time from google.appengine.api import users from google.appengine.ext import db from django...
10
2010-02-25T10:07:40Z
[ "python", "google-app-engine", "simplejson" ]
Python approach to Web Services and/or handeling GET and POST
2,114,847
<p>I have been working with python for a while now. Recently I got into Sockets with Twisted which was good for learning Telnet, SSH, and Message Passing. I wanted to take an idea and implement it in a web fashion. A week of searching and all I can really do is create a resource that handles GET and POST all to itself....
2
2010-01-22T04:01:45Z
2,114,986
<p>I'd recommend against building your own web server and handling raw socket calls to build web applications; it makes much more sense to just write your web services as wsgi applications and use an existing web server, whether it's something like tornado or apache with mod_wsgi.</p>
1
2010-01-22T04:36:22Z
[ "python", "webserver", "twisted", "tornado" ]
Python approach to Web Services and/or handeling GET and POST
2,114,847
<p>I have been working with python for a while now. Recently I got into Sockets with Twisted which was good for learning Telnet, SSH, and Message Passing. I wanted to take an idea and implement it in a web fashion. A week of searching and all I can really do is create a resource that handles GET and POST all to itself....
2
2010-01-22T04:01:45Z
2,114,993
<p>If what you're doing is more of a web site than an API, look into using a normal web framework like <a href="http://www.djangoproject.com/" rel="nofollow">Django</a>.</p>
1
2010-01-22T04:38:03Z
[ "python", "webserver", "twisted", "tornado" ]
Python approach to Web Services and/or handeling GET and POST
2,114,847
<p>I have been working with python for a while now. Recently I got into Sockets with Twisted which was good for learning Telnet, SSH, and Message Passing. I wanted to take an idea and implement it in a web fashion. A week of searching and all I can really do is create a resource that handles GET and POST all to itself....
2
2010-01-22T04:01:45Z
9,015,002
<p>I'll try to answer your various points individually.</p> <blockquote> <p>Are other options like Tornado and Standard Python Sockets a better (or more popular) approach?</p> </blockquote> <p>WSGI frameworks are by far the most popular options these days. They can give you access to <code>GET</code> and <code>P...
0
2012-01-26T07:25:38Z
[ "python", "webserver", "twisted", "tornado" ]
explorer right click context menu with python?
2,114,853
<p>I'm wondering how to go about adding a menu item to explorers right click context menu. For instance, when I right click on a file I get things like winrars "Add to archive" I want something like that and I'm wondering how to do it with python.</p>
4
2010-01-22T04:03:41Z
2,114,886
<p><a href="http://msdn.microsoft.com/en-us/library/aa753589%28VS.85%29.aspx" rel="nofollow">Adding Entries to the Standard Context Menu</a></p> <p><a href="http://docs.python.org/library/_winreg.html" rel="nofollow">_winreg - Windows registry access</a></p>
0
2010-01-22T04:11:15Z
[ "python" ]
explorer right click context menu with python?
2,114,853
<p>I'm wondering how to go about adding a menu item to explorers right click context menu. For instance, when I right click on a file I get things like winrars "Add to archive" I want something like that and I'm wondering how to do it with python.</p>
4
2010-01-22T04:03:41Z
2,114,894
<p>To Add more items to explorer right click menu, you just need to add some registry keys.</p> <p>For example, take a look at this site, there is some examples and some tools.</p> <p><a href="http://windowsxp.mvps.org/context_folders.htm">http://windowsxp.mvps.org/context_folders.htm</a></p>
5
2010-01-22T04:12:58Z
[ "python" ]
Simpler framework than Django?
2,115,098
<p>Django installation itself was quite a task (for a beginner like me) and reading a book to learn it would not be possible right now. I'm just looking to create simple test applications only to learn Python better, so I'm not ready for Django yet.</p> <p>Is there any simple web framework that can be used for test pr...
4
2010-01-22T04:58:41Z
2,115,159
<p>Try <a href="http://webpy.org/" rel="nofollow">web.py</a> or <a href="http://docs.python.org/library/cgi.html" rel="nofollow">Python's cgi</a>.</p>
2
2010-01-22T05:10:36Z
[ "python", "html", "django", "frameworks" ]
Simpler framework than Django?
2,115,098
<p>Django installation itself was quite a task (for a beginner like me) and reading a book to learn it would not be possible right now. I'm just looking to create simple test applications only to learn Python better, so I'm not ready for Django yet.</p> <p>Is there any simple web framework that can be used for test pr...
4
2010-01-22T04:58:41Z
2,115,166
<p>I've used Django. Project setup and configuration shouldn't take that long compared to writing the project code. I think it would probably be fastest to use Django. The next-best alternative would be to use SimpleHTTPServer, which is in the Python library. You have to do a lot of work yourself, such as generatin...
2
2010-01-22T05:11:26Z
[ "python", "html", "django", "frameworks" ]
Simpler framework than Django?
2,115,098
<p>Django installation itself was quite a task (for a beginner like me) and reading a book to learn it would not be possible right now. I'm just looking to create simple test applications only to learn Python better, so I'm not ready for Django yet.</p> <p>Is there any simple web framework that can be used for test pr...
4
2010-01-22T04:58:41Z
2,115,320
<p><a href="http://pylonshq.com/" rel="nofollow">Pylons</a> is pretty easy to set up - there's a command that generates the skeleton code for your application and you can write your code with no configuration other than the port number you want it to listen on.</p>
3
2010-01-22T05:47:40Z
[ "python", "html", "django", "frameworks" ]
Simpler framework than Django?
2,115,098
<p>Django installation itself was quite a task (for a beginner like me) and reading a book to learn it would not be possible right now. I'm just looking to create simple test applications only to learn Python better, so I'm not ready for Django yet.</p> <p>Is there any simple web framework that can be used for test pr...
4
2010-01-22T04:58:41Z
2,115,374
<p><a href="http://www.cherrypy.org/" rel="nofollow">cherrypy</a> is another more lightweight solution. It's just the web server part of the equation, and lets you handle templating/database (if any) as you prefer.</p> <p>Here's the minimal application, from the <a href="http://www.cherrypy.org/wiki/CherryPyTutorial" ...
4
2010-01-22T06:03:42Z
[ "python", "html", "django", "frameworks" ]
Simpler framework than Django?
2,115,098
<p>Django installation itself was quite a task (for a beginner like me) and reading a book to learn it would not be possible right now. I'm just looking to create simple test applications only to learn Python better, so I'm not ready for Django yet.</p> <p>Is there any simple web framework that can be used for test pr...
4
2010-01-22T04:58:41Z
2,115,782
<p>I started with <a href="http://www.web2py.com" rel="nofollow">web2py</a> and found it both easy and powerful.</p>
4
2010-01-22T07:43:23Z
[ "python", "html", "django", "frameworks" ]
Simpler framework than Django?
2,115,098
<p>Django installation itself was quite a task (for a beginner like me) and reading a book to learn it would not be possible right now. I'm just looking to create simple test applications only to learn Python better, so I'm not ready for Django yet.</p> <p>Is there any simple web framework that can be used for test pr...
4
2010-01-22T04:58:41Z
2,115,801
<p>I would definitely recommend <a href="http://dev.pocoo.org/projects/werkzeug/" rel="nofollow">werkzeug</a>. It is really dead simple and will expose you to WSGI which is also used by several framework like django and <a href="http://code.google.com/appengine/" rel="nofollow">Google App Engine</a>.</p>
2
2010-01-22T07:51:01Z
[ "python", "html", "django", "frameworks" ]
Simpler framework than Django?
2,115,098
<p>Django installation itself was quite a task (for a beginner like me) and reading a book to learn it would not be possible right now. I'm just looking to create simple test applications only to learn Python better, so I'm not ready for Django yet.</p> <p>Is there any simple web framework that can be used for test pr...
4
2010-01-22T04:58:41Z
2,116,471
<p>you could also try <a href="http://code.google.com/appengine/docs/python/tools/webapp/" rel="nofollow">google app engines webapp framework</a> if you want something to create toy webapps that deploy really easily (and cheaply !)</p>
2
2010-01-22T10:10:53Z
[ "python", "html", "django", "frameworks" ]
Simpler framework than Django?
2,115,098
<p>Django installation itself was quite a task (for a beginner like me) and reading a book to learn it would not be possible right now. I'm just looking to create simple test applications only to learn Python better, so I'm not ready for Django yet.</p> <p>Is there any simple web framework that can be used for test pr...
4
2010-01-22T04:58:41Z
2,116,772
<p>I think you don't need to read a complete book on django. It's also very common to start with the excellent <a href="http://docs.djangoproject.com/en/dev/intro/tutorial01/" rel="nofollow">django tutorial</a>. After that you can play a bit with your code and still have one of the greatest documentions that is availab...
2
2010-01-22T11:08:07Z
[ "python", "html", "django", "frameworks" ]
Using Android's `Timer.schedule` in Python
2,115,227
<p>Could someone explain how to use the <a href="http://developer.android.com/reference/java/util/Timer.html#schedule%28java.util.TimerTask,%20java.util.Date%29" rel="nofollow">Timer.schedule</a> - that is used for scheduling tasks - in <a href="http://code.google.com/p/android-scripting/wiki/PythonAndroidAPI" rel="nof...
2
2010-01-22T05:24:55Z
2,742,310
<p>time.sleep does work on the Android Scripting Environment (at least for the last 5 releases or so). I've just tested it on the interpreter console.</p> <p>On the other hand, some dialogs (like makeToast, for instance) won't hold the script execution while they're being displayed. Perhaps that's why you may be feeli...
0
2010-04-30T04:33:02Z
[ "python", "android", "timer", "android-scripting" ]
Index xml files from a outside website
2,115,290
<p>Using python django I would like to access this site <a href="http://www.reta-vortaro.de/revo/" rel="nofollow">http://www.reta-vortaro.de/revo/</a> It is a dictionary site for a language called esperanto, I need to be able to search for a word, and get back its definition, it looks like Each Esperanto root word ...
1
2010-01-22T05:38:26Z
2,115,336
<p>Most programming languages have access to both some sort of XML parser as well as some persistent embedded key-value store. Once you've decided on a programming language, just find one of each that you can feel comfortable with.</p>
2
2010-01-22T05:51:27Z
[ "python", "xml", "django" ]
Index xml files from a outside website
2,115,290
<p>Using python django I would like to access this site <a href="http://www.reta-vortaro.de/revo/" rel="nofollow">http://www.reta-vortaro.de/revo/</a> It is a dictionary site for a language called esperanto, I need to be able to search for a word, and get back its definition, it looks like Each Esperanto root word ...
1
2010-01-22T05:38:26Z
2,117,902
<p>Wonder, if you have access to WSDL. You might be able to access the data that way. What exactly is the problem you are encountering?</p>
2
2010-01-22T14:34:40Z
[ "python", "xml", "django" ]
Index xml files from a outside website
2,115,290
<p>Using python django I would like to access this site <a href="http://www.reta-vortaro.de/revo/" rel="nofollow">http://www.reta-vortaro.de/revo/</a> It is a dictionary site for a language called esperanto, I need to be able to search for a word, and get back its definition, it looks like Each Esperanto root word ...
1
2010-01-22T05:38:26Z
2,169,816
<p>As soon as you need indexing and fast search, it might worth looking for <a href="http://en.wikipedia.org/wiki/XML_database" rel="nofollow">XML database</a> for storing your dictionary (especially for complex queries and big dictionaries). You can easily access most XML databases from PHP.</p>
1
2010-01-30T22:59:04Z
[ "python", "xml", "django" ]
Index xml files from a outside website
2,115,290
<p>Using python django I would like to access this site <a href="http://www.reta-vortaro.de/revo/" rel="nofollow">http://www.reta-vortaro.de/revo/</a> It is a dictionary site for a language called esperanto, I need to be able to search for a word, and get back its definition, it looks like Each Esperanto root word ...
1
2010-01-22T05:38:26Z
2,169,835
<p>Have you tried asking the site's administrator for the data? Or perhaps he could set up a web service for you?</p>
0
2010-01-30T23:02:47Z
[ "python", "xml", "django" ]
Index xml files from a outside website
2,115,290
<p>Using python django I would like to access this site <a href="http://www.reta-vortaro.de/revo/" rel="nofollow">http://www.reta-vortaro.de/revo/</a> It is a dictionary site for a language called esperanto, I need to be able to search for a word, and get back its definition, it looks like Each Esperanto root word ...
1
2010-01-22T05:38:26Z
2,204,319
<p>Well, you can fetch each XML file with <code>file_get_contents()</code>, curl, wget or the tool that pleases you the most.</p> <p>Then, You can save the XML files on the file system, or even better use Oracle's Berkeley <a href="http://www.oracle.com/database/berkeley-db/xml/index.html" rel="nofollow">DBXML</a>, wi...
0
2010-02-05T00:40:58Z
[ "python", "xml", "django" ]
Index xml files from a outside website
2,115,290
<p>Using python django I would like to access this site <a href="http://www.reta-vortaro.de/revo/" rel="nofollow">http://www.reta-vortaro.de/revo/</a> It is a dictionary site for a language called esperanto, I need to be able to search for a word, and get back its definition, it looks like Each Esperanto root word ...
1
2010-01-22T05:38:26Z
2,209,115
<p>I would consider such workflow for you:</p> <ol> <li>Download all the files</li> <li>Load their contents and filenames into database (any database will fit) </li> <li>Setup sphinx search tool ( <a href="http://sphinx.pocoo.org/" rel="nofollow">http://sphinx.pocoo.org/</a> )</li> <li>Run sphinx to build an index for...
1
2010-02-05T17:38:01Z
[ "python", "xml", "django" ]
What is the deal with the pony in Python community?
2,115,360
<p>Pony references are in several places:</p> <ul> <li><a href="http://www.mail-archive.com/python-dev@python.org/msg44751.html">http://www.mail-archive.com/python-dev@python.org/msg44751.html</a></li> <li><a href="http://pypi.python.org/pypi/django-pony/">http://pypi.python.org/pypi/django-pony/</a></li> <li><a href=...
31
2010-01-22T06:00:26Z
2,115,368
<p>It's the Django mascot.</p>
4
2010-01-22T06:02:40Z
[ "python", "django", "pony" ]
What is the deal with the pony in Python community?
2,115,360
<p>Pony references are in several places:</p> <ul> <li><a href="http://www.mail-archive.com/python-dev@python.org/msg44751.html">http://www.mail-archive.com/python-dev@python.org/msg44751.html</a></li> <li><a href="http://pypi.python.org/pypi/django-pony/">http://pypi.python.org/pypi/django-pony/</a></li> <li><a href=...
31
2010-01-22T06:00:26Z
2,115,369
<p>Its a Django unofficial mascot. See <a href="http://twothirty.am/blog/2009/10/07/blessing-mythical-django-pony/">this blog post for an explanation</a>.</p> <p>Python generally uses Monty Python references (and sometimes snake references, for the misguided). </p>
15
2010-01-22T06:02:49Z
[ "python", "django", "pony" ]
What is the deal with the pony in Python community?
2,115,360
<p>Pony references are in several places:</p> <ul> <li><a href="http://www.mail-archive.com/python-dev@python.org/msg44751.html">http://www.mail-archive.com/python-dev@python.org/msg44751.html</a></li> <li><a href="http://pypi.python.org/pypi/django-pony/">http://pypi.python.org/pypi/django-pony/</a></li> <li><a href=...
31
2010-01-22T06:00:26Z
2,115,375
<p>When you start listing what you want, "I want a fast HTTP parser", "I want ORM that just works", "I want higher order functions", the idea is that while you're wishing for things, you might as well wish for a pony too. This is probably a reference to a Calvin and Hobbes strip from "Someone under the bed is drooling...
57
2010-01-22T06:03:37Z
[ "python", "django", "pony" ]
What is the deal with the pony in Python community?
2,115,360
<p>Pony references are in several places:</p> <ul> <li><a href="http://www.mail-archive.com/python-dev@python.org/msg44751.html">http://www.mail-archive.com/python-dev@python.org/msg44751.html</a></li> <li><a href="http://pypi.python.org/pypi/django-pony/">http://pypi.python.org/pypi/django-pony/</a></li> <li><a href=...
31
2010-01-22T06:00:26Z
2,115,634
<p>See also <a href="http://avalonstar.com/legacy/blog/2008/sep/9/the-web-framework-for-ponies/">Bryan Veloso's post about the pony mascot image</a>.</p>
19
2010-01-22T07:09:03Z
[ "python", "django", "pony" ]
What is the deal with the pony in Python community?
2,115,360
<p>Pony references are in several places:</p> <ul> <li><a href="http://www.mail-archive.com/python-dev@python.org/msg44751.html">http://www.mail-archive.com/python-dev@python.org/msg44751.html</a></li> <li><a href="http://pypi.python.org/pypi/django-pony/">http://pypi.python.org/pypi/django-pony/</a></li> <li><a href=...
31
2010-01-22T06:00:26Z
2,115,901
<p>Look at what the pony does! &nbsp;</p> <ul> <li><a href="http://code.djangoproject.com/ticket/10668" rel="nofollow">http://code.djangoproject.com/ticket/10668</a></li> <li><a href="http://www.flickr.com/photos/bastispicks/3400633546/" rel="nofollow">http://www.flickr.com/photos/bastispicks/3400633546/</a></li> </u...
8
2010-01-22T08:18:55Z
[ "python", "django", "pony" ]
Does Python have a module for parsing HTTP requests and responses?
2,115,410
<p>httplib (now http.client) and friends all have conn.getresponse() and an HTTPResponse class, but the server-side operations of conn.getrequest() and an HTTPRequest class seem to be lacking.</p> <p>I understand that BaseHTTPServer and BaseHTTPRequestHandler can perform this functionality, but they don't expose these...
20
2010-01-22T06:11:32Z
2,117,073
<p>For server-side processing you want to look at something like <a href="http://docs.python.org/library/wsgiref.html" rel="nofollow">wsgiref</a>.</p> <p>The WSGI standard parses the request into a simple dictionary with all of the relevant headers and elements.</p>
2
2010-01-22T12:12:18Z
[ "python", "http", "parsing" ]
Does Python have a module for parsing HTTP requests and responses?
2,115,410
<p>httplib (now http.client) and friends all have conn.getresponse() and an HTTPResponse class, but the server-side operations of conn.getrequest() and an HTTPRequest class seem to be lacking.</p> <p>I understand that BaseHTTPServer and BaseHTTPRequestHandler can perform this functionality, but they don't expose these...
20
2010-01-22T06:11:32Z
2,117,382
<p>You would probably find <a href="http://pythonpaste.org/webob/" rel="nofollow">WebOb</a> useful. Frameworks like Pylons, Turbogears, and Bfg use it as part of their api. It does operate under the assumption that you are working under WSGI though. </p>
2
2010-01-22T13:15:22Z
[ "python", "http", "parsing" ]
Does Python have a module for parsing HTTP requests and responses?
2,115,410
<p>httplib (now http.client) and friends all have conn.getresponse() and an HTTPResponse class, but the server-side operations of conn.getrequest() and an HTTPRequest class seem to be lacking.</p> <p>I understand that BaseHTTPServer and BaseHTTPRequestHandler can perform this functionality, but they don't expose these...
20
2010-01-22T06:11:32Z
5,964,334
<p>Jeff, to enable parsing I create a small nine-line subclass of the base HTTP request handler:</p> <pre><code>from BaseHTTPServer import BaseHTTPRequestHandler from StringIO import StringIO class HTTPRequest(BaseHTTPRequestHandler): def __init__(self, request_text): self.rfile = StringIO(request_text) ...
37
2011-05-11T12:37:03Z
[ "python", "http", "parsing" ]
what does this code snippet do?
2,115,578
<p>can somebody give me an explanation about the following code?</p> <pre><code>from twisted.internet import protocol, reactor from twisted.protocols import basic class FingerProtocol(basic.LineReceiver): def lineReceived(self, user): self.transport.write(self.factory.getUser(user)+"\r\n") self.tr...
0
2010-01-22T06:52:24Z
2,115,582
<p>It's <a href="http://docs.python.org/tutorial/controlflow.html#keyword-arguments" rel="nofollow">keyword argument notation</a>.</p>
2
2010-01-22T06:53:34Z
[ "python" ]
what does this code snippet do?
2,115,578
<p>can somebody give me an explanation about the following code?</p> <pre><code>from twisted.internet import protocol, reactor from twisted.protocols import basic class FingerProtocol(basic.LineReceiver): def lineReceived(self, user): self.transport.write(self.factory.getUser(user)+"\r\n") self.tr...
0
2010-01-22T06:52:24Z
2,115,628
<p>The call to FingerFactory (a strange name, BTW) is instantiating a FingerFactory object. The parameters to that call are passed to the <code>__init__</code> function of the class, where they are accepted by the <code>**kwargs</code> parameter as a dictionary: </p> <pre><code>{'moshez': 'Happy and well'} </code></pr...
0
2010-01-22T07:08:00Z
[ "python" ]
what does this code snippet do?
2,115,578
<p>can somebody give me an explanation about the following code?</p> <pre><code>from twisted.internet import protocol, reactor from twisted.protocols import basic class FingerProtocol(basic.LineReceiver): def lineReceived(self, user): self.transport.write(self.factory.getUser(user)+"\r\n") self.tr...
0
2010-01-22T06:52:24Z
2,115,630
<p>And the other question, regarding <code>FingerFactory</code> call.</p> <p>That's how you do instantiation in Python. You don't use a <code>new</code> keyword. You just call the class as though it were a function. The constructor of the class is <code>__init__</code></p>
0
2010-01-22T07:08:17Z
[ "python" ]
assigning value to shell variable using a function return value from Python
2,115,615
<p>I have a Python function, fooPy() that returns some value. ( int / double or string) </p> <p>I want to use this value and assign it in a shell script. For example following is the python function:</p> <pre><code>def fooPy(): return "some string" #return 10 .. alternatively, it can be an int fooPy() </co...
10
2010-01-22T07:04:53Z
2,115,632
<p>You can print your value in Python, like this:</p> <pre><code>print fooPy() </code></pre> <p>and in your shell script:</p> <pre><code>fooShell=$(python fooPy.py) </code></pre> <p>Be sure not to leave spaces around the <code>=</code> in the shell script.</p>
27
2010-01-22T07:08:56Z
[ "python", "shell" ]
assigning value to shell variable using a function return value from Python
2,115,615
<p>I have a Python function, fooPy() that returns some value. ( int / double or string) </p> <p>I want to use this value and assign it in a shell script. For example following is the python function:</p> <pre><code>def fooPy(): return "some string" #return 10 .. alternatively, it can be an int fooPy() </co...
10
2010-01-22T07:04:53Z
2,115,635
<p>In your Python code, you need to print the result.</p> <pre><code>import sys def fooPy(): return 10 # or whatever if __name__ == '__main__': sys.stdout.write("%s\n", fooPy()) </code></pre> <p>Then in the shell, you can do:</p> <pre><code>fooShell=$(python fooPy.py) # note no space around the '=' </code><...
6
2010-01-22T07:09:21Z
[ "python", "shell" ]
assigning value to shell variable using a function return value from Python
2,115,615
<p>I have a Python function, fooPy() that returns some value. ( int / double or string) </p> <p>I want to use this value and assign it in a shell script. For example following is the python function:</p> <pre><code>def fooPy(): return "some string" #return 10 .. alternatively, it can be an int fooPy() </co...
10
2010-01-22T07:04:53Z
2,115,637
<p>You should <code>print</code> the value returned by <code>fooPy</code>. The shell substitution reads from stdout. Replace the last line of your program with <code>print fooPy()</code> and then use the second shell pipeline you've mentioned. It should work.</p>
2
2010-01-22T07:09:18Z
[ "python", "shell" ]
assigning value to shell variable using a function return value from Python
2,115,615
<p>I have a Python function, fooPy() that returns some value. ( int / double or string) </p> <p>I want to use this value and assign it in a shell script. For example following is the python function:</p> <pre><code>def fooPy(): return "some string" #return 10 .. alternatively, it can be an int fooPy() </co...
10
2010-01-22T07:04:53Z
2,115,772
<p>If you want the value from the Python <code>sys.exit</code> statement, it will be in the shell special variable <code>$?</code>.</p> <pre><code>$ var=$(foo.py) $ returnval=$? $ echo $var Some string $ echo returnval 10 </code></pre>
4
2010-01-22T07:40:44Z
[ "python", "shell" ]
Calling Python function in Django template
2,115,869
<p>Inside a django template I'm trying to call the split function on one of the template variables and then get the last element, so I did something like this:</p> <pre><code>{{ newsletter.NewsletterPath.split('/').-1 }} </code></pre> <p>Unfortunately, it doesn't like the split. Some might suggest that I do the split...
7
2010-01-22T08:09:20Z
2,115,936
<p>From the <a href="http://www.djangobook.com/en/2.0/chapter04/" rel="nofollow">django book</a>:</p> <blockquote> <p>Note that you do not include parentheses in the method calls. Also, it’s not possible to pass arguments to the methods; you can only call methods that have no required arguments.</p> </blockq...
6
2010-01-22T08:27:54Z
[ "python", "django", "django-templates" ]
Calling Python function in Django template
2,115,869
<p>Inside a django template I'm trying to call the split function on one of the template variables and then get the last element, so I did something like this:</p> <pre><code>{{ newsletter.NewsletterPath.split('/').-1 }} </code></pre> <p>Unfortunately, it doesn't like the split. Some might suggest that I do the split...
7
2010-01-22T08:09:20Z
2,115,941
<p>Templates are deliberately not able to do such stuff. The purpose is to prevent you from putting your business logic in templates, which are meant to deal only with the layout.</p> <p>So a possible way to do this is to define a NewsletterPathLastElement(self) function in your newsletter Model, and call that from th...
4
2010-01-22T08:28:25Z
[ "python", "django", "django-templates" ]
Calling Python function in Django template
2,115,869
<p>Inside a django template I'm trying to call the split function on one of the template variables and then get the last element, so I did something like this:</p> <pre><code>{{ newsletter.NewsletterPath.split('/').-1 }} </code></pre> <p>Unfortunately, it doesn't like the split. Some might suggest that I do the split...
7
2010-01-22T08:09:20Z
2,115,954
<p>What do you mean by "it doesn't like the split"? How does it manifest its dislike? </p> <p>If I remember correctly, you can not pass any arbitrary arguments to methods, that are called from the django template and the identifiers, that can be used in the templates can only consist of a-z, A-Z, 0-9, underscores and...
6
2010-01-22T08:31:02Z
[ "python", "django", "django-templates" ]
Calling Python function in Django template
2,115,869
<p>Inside a django template I'm trying to call the split function on one of the template variables and then get the last element, so I did something like this:</p> <pre><code>{{ newsletter.NewsletterPath.split('/').-1 }} </code></pre> <p>Unfortunately, it doesn't like the split. Some might suggest that I do the split...
7
2010-01-22T08:09:20Z
2,116,042
<p>Yes, as others have said, you shouldn't really be doing it in the templates.</p> <p>But if you want to, then you need to define a filter and load it in the template and use it.</p>
0
2010-01-22T08:52:28Z
[ "python", "django", "django-templates" ]
Python Class attributes - resetting
2,116,090
<p>Whats the best way to reset class attributes in Python.</p> <p>I have a class whichs has about 20 class attributes, in my init I have </p> <pre><code>class MyClass: def __init__(self) self.time=0 self.pos=0 self.vel=0 self.acc=0 self.rot=0 self.dyn=0 </code></pre> <p>...
3
2010-01-22T09:01:45Z
2,116,144
<p>I'm not sure if this any more neat, but:</p> <pre><code>class MyClass: def __init__(self): for v in ('time', 'pos', 'vel', 'acc', 'rot', 'dyn'): exec("self.%s = 0" % v) </code></pre> <p>As SilentGhost suggested, you should probably put them in a sane data structure, such as tuple e.g.</p> ...
0
2010-01-22T09:12:00Z
[ "python", "class", "attributes" ]
Python Class attributes - resetting
2,116,090
<p>Whats the best way to reset class attributes in Python.</p> <p>I have a class whichs has about 20 class attributes, in my init I have </p> <pre><code>class MyClass: def __init__(self) self.time=0 self.pos=0 self.vel=0 self.acc=0 self.rot=0 self.dyn=0 </code></pre> <p>...
3
2010-01-22T09:01:45Z
2,116,153
<p>I'd rather not reset them in the <code>init</code> method but define a <code>reset</code> method for this, and call <code>reset</code> from <code>init</code> and before every subsequent iteration.</p>
4
2010-01-22T09:13:26Z
[ "python", "class", "attributes" ]
Python Class attributes - resetting
2,116,090
<p>Whats the best way to reset class attributes in Python.</p> <p>I have a class whichs has about 20 class attributes, in my init I have </p> <pre><code>class MyClass: def __init__(self) self.time=0 self.pos=0 self.vel=0 self.acc=0 self.rot=0 self.dyn=0 </code></pre> <p>...
3
2010-01-22T09:01:45Z
2,116,160
<p>you can use vars() to return a dictionary of values that you can edit.</p> <p>something like that</p> <pre><code>class A: def __init__(self): self.a=1 self.b=1 def reset(self): dic = vars(self) for i in dic.keys(): dic[i] = 0 inst = A() print inst.a , inst.b #...
4
2010-01-22T09:14:24Z
[ "python", "class", "attributes" ]
Python Class attributes - resetting
2,116,090
<p>Whats the best way to reset class attributes in Python.</p> <p>I have a class whichs has about 20 class attributes, in my init I have </p> <pre><code>class MyClass: def __init__(self) self.time=0 self.pos=0 self.vel=0 self.acc=0 self.rot=0 self.dyn=0 </code></pre> <p>...
3
2010-01-22T09:01:45Z
2,116,207
<pre><code>class MyClass(object): def __init__(self): attribs = 'time', 'pos', 'vel', 'acc', 'rot', 'dyn' vars(self).update((x, 0) for x in attribs) </code></pre>
2
2010-01-22T09:23:32Z
[ "python", "class", "attributes" ]
XML to store system paths in Python with lxml
2,116,121
<p>I'm using an xml file to store configurations for a software.</p> <p>One of theese configurations would be a system path like </p> <pre><code>&gt; set_value = "c:\\test\\3 tests\\test" </code></pre> <p>i can store it by using:</p> <pre><code>&gt; setting = etree.SubElement(settings, &gt; "setting", name=tmp_set_...
1
2010-01-22T09:07:35Z
2,116,410
<blockquote> <p>Now I read it again with the etree.parse method</p> <p>I obtain an etree child object with a string value, but the string contains the</p> <pre><code>\3 </code></pre> <p>character and if i try to use it to write again to xml it will be interpreted !!!!!</p> </blockquote> <p>I jus...
1
2010-01-22T10:00:12Z
[ "python", "xml", "lxml" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,317
<p>flatten, sort, 1 for loop comparing numbers before and after</p>
-1
2010-01-22T09:42:35Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,318
<p>The same way as you'd do it by hand:</p> <pre><code>seen = set() repeated = set() for l in list_of_lists: for i in set(l): if i in seen: repeated.add(i) else: seen.add(i) </code></pre> <p>By the way, here's the one liner (without counting the import) that some people were seeking (should be l...
13
2010-01-22T09:42:51Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,322
<p>You can use a set see <a href="http://docs.python.org/library/stdtypes.html#set" rel="nofollow">http://docs.python.org/library/stdtypes.html#set</a></p>
0
2010-01-22T09:43:23Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,329
<p>You can use a dictionary to get the count of each</p> <pre><code>from collections import defaultdict init_list = [[1,2,3], [3,4,5], [5,6,7], [7,8,9]] #defaultdict, every new key will have a int(0) as default value d = defaultdict(int) for values in init_list: #Transform each list in a set to avoid false positive...
1
2010-01-22T09:44:13Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,345
<pre><code>l=[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] d={} for x in l: for y in x: if not d.has_key(y): d[y]=0 d[y]+=1 [x for x,y in d.iteritems() if y&gt;1] </code></pre>
0
2010-01-22T09:47:15Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,346
<p>reference: <a href="http://docs.python.org/library/stdtypes.html#set" rel="nofollow">http://docs.python.org/library/stdtypes.html#set</a></p> <pre><code>#!/usr/bin/python ll = [[1,2,3], [3,4,5], [5,6,7], [7,8,9]] ls = [set(l) for l in ll] su = ls[0] #union ssd = ls[0] #symmetric_difference for s in ls[1:]: su ...
2
2010-01-22T09:47:35Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,355
<p>Try this:</p> <pre><code>data = [[1,2,3], [3,4,5], [5,6,7], [7,8,9], [1,2,3]] res = set() for i in data: for j in data: if i is not j: res |= set(i) &amp; set(j) print res </code></pre>
0
2010-01-22T09:48:58Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,405
<p>Cleanest way would probably be to use reduce: </p> <pre><code>def findCommon(L): def R(a, b, seen=set()): a.update(b &amp; seen) seen.update(b) return a return reduce(R, map(set, L), set()) result = findCommon([[1,2,3], [3,4,5], [5,6,7], [7,8,9]]) </code></pre> <p>Result is a se...
4
2010-01-22T09:59:24Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,449
<pre><code>&gt;&gt;&gt; sets = [[1,2,3], [3,4,5], [5,6,7], [7,8,9]] &gt;&gt;&gt; seen = set() &gt;&gt;&gt; duplicates = set() &gt;&gt;&gt; &gt;&gt;&gt; for subset in map(set, sets) : ... duplicates |= (subset &amp; seen) ... seen |= subset ... &gt;&gt;&gt; print(duplicates) set([3, 5, 7]) &gt;&gt;&gt; </code...
1
2010-01-22T10:06:29Z
[ "python", "list" ]
Python: Find identical items in multiple lists
2,116,286
<p>I have a list of an arbitrary number of lists, for instance:</p> <pre><code>[[1,2,3], [3,4,5], [5,6,7], [7,8,9]] </code></pre> <p>Now I would like a list containing all elements that are present in more than one list:</p> <pre><code>[3,5,7] </code></pre> <p>How would I do that?</p> <p>Thanks!</p>
4
2010-01-22T09:36:47Z
2,116,565
<p>Here is my go:</p> <pre><code>seen = set() result = set() for s in map(set, [[1,2,3], [3,4,5], [5,6,7], [7,8,9]]): result.update(s &amp; seen) seen.update(s) print result </code></pre> <p>This prints:</p> <pre><code>set([3, 5, 7]) </code></pre>
0
2010-01-22T10:27:22Z
[ "python", "list" ]
Python and Boost - Trying to run vPython and getting __doc__ is read only error
2,116,323
<p>This is super urgent! Need to get vPython working, can anyone give me help on any way to do this?</p> <p>I'm running on Mac OS X with Python 2.6 , and recently downloaded vPython. Upon installation it also installs the Boost libraries for Python. Now, when I try to import the vPython libraries ('visual'), the follo...
0
2010-01-22T09:43:56Z
6,221,962
<p>Have you tried using:</p> <pre><code>from visual import * </code></pre> <p>If thats not working than possibly reinstall Python on your machine as well as vPython.</p>
0
2011-06-03T00:56:18Z
[ "python", "osx", "boost", "vpython" ]
Python# screenshot failure
2,116,368
<p>I am using PIL(Python Imaging Library) for grabbing the image. But <code>grabber()</code> throws the following error message if I minimized the window</p> <pre><code>img=ImageGrab.grab() File "C:\Python26\lib\site-packages\PIL\ImageGrab.py", line 47, in grab size, data = grabber() IOError: screen grab failed </...
2
2010-01-22T09:50:47Z
2,251,884
<p>There is a very similar question located <a href="http://stackoverflow.com/questions/69645/take-a-screenshot-via-a-python-script-linux">here</a> - it even has some alternatives and solutions, and (as Adam Bernier has written), imagegrab works only on Windows. </p>
0
2010-02-12T12:57:00Z
[ "python", "python-imaging-library" ]
Create string(s) from list(s) items
2,116,597
<p>I have a list, <code>list = ['foo','bar']</code> and now i want to create a string from each item.</p> <p>Each string is named as the item and has the value of the item</p> <p>foo = 'foo' bar = 'bar'</p> <p>Thanks to all, i will use a dict instead</p>
0
2010-01-22T10:30:54Z
2,116,610
<p>Don't do that. Use a dict instead.</p> <pre><code>strings = dict((x, x) for x in L) </code></pre>
9
2010-01-22T10:33:03Z
[ "python" ]
Create string(s) from list(s) items
2,116,597
<p>I have a list, <code>list = ['foo','bar']</code> and now i want to create a string from each item.</p> <p>Each string is named as the item and has the value of the item</p> <p>foo = 'foo' bar = 'bar'</p> <p>Thanks to all, i will use a dict instead</p>
0
2010-01-22T10:30:54Z
2,116,620
<p>In spite of their names, variables in Python should in fact not themselves be variable. If you have data you want to associate with a 'name', you should be using a dict instead:</p> <pre><code>data = {} for s in ['foo', 'bar']: data[s] = s </code></pre> <p>It is sometimes(!) possible to modify locals() to intr...
0
2010-01-22T10:34:58Z
[ "python" ]
Create string(s) from list(s) items
2,116,597
<p>I have a list, <code>list = ['foo','bar']</code> and now i want to create a string from each item.</p> <p>Each string is named as the item and has the value of the item</p> <p>foo = 'foo' bar = 'bar'</p> <p>Thanks to all, i will use a dict instead</p>
0
2010-01-22T10:30:54Z
2,116,621
<p>Try:</p> <pre><code>&gt;&gt;&gt; l = ["foo", "bar"] &gt;&gt;&gt; for item in l: exec("%s = '%s'" % (item, item)) </code></pre> <p><strong>Note:</strong> Why do you need this? Are you sure this is the best way to do what you want to do? I ask because this is usually not such a great idea.</p> <p><strong>Securi...
1
2010-01-22T10:35:08Z
[ "python" ]
Create string(s) from list(s) items
2,116,597
<p>I have a list, <code>list = ['foo','bar']</code> and now i want to create a string from each item.</p> <p>Each string is named as the item and has the value of the item</p> <p>foo = 'foo' bar = 'bar'</p> <p>Thanks to all, i will use a dict instead</p>
0
2010-01-22T10:30:54Z
2,116,637
<p><a href="http://stackoverflow.com/users/20862/ignacio-vazquez-abrams">Ignacio Vazquez-Abrams</a> is right, using a dict is better. But if you insist on having them available as variables, you can always do this:</p> <pre><code>strings = dict((x, x) for x in L) locals().update(strings) </code></pre> <p>PS: <a href=...
3
2010-01-22T10:39:03Z
[ "python" ]
Create string(s) from list(s) items
2,116,597
<p>I have a list, <code>list = ['foo','bar']</code> and now i want to create a string from each item.</p> <p>Each string is named as the item and has the value of the item</p> <p>foo = 'foo' bar = 'bar'</p> <p>Thanks to all, i will use a dict instead</p>
0
2010-01-22T10:30:54Z
2,116,650
<p>Take a look at <a href="http://docs.python.org/library/functions.html#setattr" rel="nofollow">http://docs.python.org/library/functions.html#setattr</a></p> <p>You should be able to do something along the lines of</p> <pre><code>for x in list: setattr(object, x, x) </code></pre> <p>Assuming object is whatever ...
0
2010-01-22T10:42:05Z
[ "python" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
2,116,755
<p>Start with the recursive algorithm (pseudocode) : </p> <pre><code>traverse(node): if node != None do: traverse(node.left) print node.value traverse(node.right) endif </code></pre> <p>This is a clear case of tail recursion, so you can easily turn it into a while-loop. </p> <pre><code>traverse(node)...
71
2010-01-22T11:05:51Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
2,116,757
<pre><code>def traverseInorder(node): lifo = Lifo() while node is not None: if node.left is not None: lifo.push(node) node = node.left continue print node.value if node.right is not None: node = node.right continue node = lifo.Pop() if node is not None : p...
1
2010-01-22T11:06:20Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
2,116,763
<p>I think part of the problem is the use of the "prev" variable. You shouldn't have to store the previous node you should be able to maintain the state on the stack (Lifo) itself.</p> <p>From <a href="http://en.wikipedia.org/wiki/Tree_traversal" rel="nofollow">Wikipedia</a>, the algorithm you are aiming for is:</p> ...
-1
2010-01-22T11:07:05Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
2,353,766
<p>State can be remembered implicitly,</p> <pre><code>traverse(node) { if(!node) return; push(stack, node); while (!empty(stack)) { /*Remember the left nodes in stack*/ while (node-&gt;left) { push(stack, node-&gt;left); node = node-&gt;left; } /*Process the node*/ ...
0
2010-03-01T03:40:21Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
4,915,707
<p>Here is a simple in-order non-recursive c++ code ..</p> <pre><code>void inorder (node *n) { stack s; while(n){ s.push(n); n=n-&gt;left; } while(!s.empty()){ node *t=s.pop(); cout&lt;&lt;t-&gt;data; t=t-&gt;right; while(t){ s.push(t); ...
13
2011-02-06T19:55:31Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
6,887,163
<p>@Victor, I have some suggestion on your implementation trying to push the state into the stack. I don't see it is necessary. Because every element you take from the stack is already left traversed. so instead of store the information into the stack, all we need is a flag to indicate if the next node to be processed ...
0
2011-07-31T02:25:41Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
8,704,234
<pre> def print_tree_in(root): stack = [] current = root while True: while current is not None: stack.append(current) current = current.getLeft(); if not stack: return current = stack.pop() print current.getValue() while current.get...
2
2012-01-02T18:44:54Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
24,719,707
<p>Here is a sample of in order traversal using stack in c# (.net):</p> <p>(for post order iterative you may refer to: <a href="http://stackoverflow.com/questions/1294701/post-order-traversal-of-binary-tree-without-recursion/24719772#24719772">Post order traversal of binary tree without recursion</a>)</p> <pre><code>...
1
2014-07-13T05:02:54Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
32,515,044
<p>Little Optimization of answer by @Emadpres</p> <pre><code>def in_order_search(node): stack = Stack() current = node while True: while current is not None: stack.push(current) current = current.l_child if stack.size() == 0: break current = st...
0
2015-09-11T03:37:17Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
33,882,575
<p>This may be helpful (Java implementation)</p> <pre><code>public void inorderDisplay(Node root) { Node current = root; LinkedList&lt;Node&gt; stack = new LinkedList&lt;&gt;(); while (true) { if (current != null) { stack.push(current); current = current.left; } else...
0
2015-11-23T23:14:53Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
39,933,353
<p>Simple iterative inorder traversal without recursion</p> <pre><code>'''iterative inorder traversal, O(n) time &amp; O(n) space ''' class Node: def __init__(self, value, left = None, right = None): self.value = value self.left = left self.right = right def inorder_iter(root): stack...
0
2016-10-08T14:06:34Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
Help me understand Inorder Traversal without using recursion
2,116,662
<p>I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion.</p> <p>This is what I've tried so far:</p> <pre><code>def traverseInorder(node): lifo = Lifo(...
28
2010-01-22T10:44:42Z
40,075,565
<pre><code>class Tree: def __init__(self, value): self.left = None self.right = None self.value = value def insert(self,root,node): if root is None: root = node else: if root.value &lt; node.value: if root.right is None: ...
0
2016-10-16T21:15:30Z
[ "python", "algorithm", "tree", "tree-traversal", "non-recursive" ]
What are the cons of returning an Exception instance instead of raising it in Python?
2,116,972
<p>I have been doing some work with python-couchdb and desktopcouch. In one of the patches I submitted I wrapped the <code>db.update</code> function from couchdb. For anyone that is not familiar with python-couchdb the function is the following:</p> <pre><code>def update(self, documents, **options): """Perform a b...
3
2010-01-22T11:51:17Z
2,117,013
<p>exceptions intended to be raised. It helps with debugging, handling causes of the errors and it's clear and well-established practise of other developers.</p> <p>I think looking at the interface of the programme, it's not clear what am I supposed to do with returned exception. raise it? from outside of the chain th...
1
2010-01-22T11:59:47Z
[ "exception", "python" ]
What are the cons of returning an Exception instance instead of raising it in Python?
2,116,972
<p>I have been doing some work with python-couchdb and desktopcouch. In one of the patches I submitted I wrapped the <code>db.update</code> function from couchdb. For anyone that is not familiar with python-couchdb the function is the following:</p> <pre><code>def update(self, documents, **options): """Perform a b...
3
2010-01-22T11:51:17Z
2,117,169
<p>I think it is ok to return the exceptions in this case, because some parts of the update function may succeed and some may fail. When you raise the exception, the API user has no control over what succeeded already.</p>
4
2010-01-22T12:31:17Z
[ "exception", "python" ]
What are the cons of returning an Exception instance instead of raising it in Python?
2,116,972
<p>I have been doing some work with python-couchdb and desktopcouch. In one of the patches I submitted I wrapped the <code>db.update</code> function from couchdb. For anyone that is not familiar with python-couchdb the function is the following:</p> <pre><code>def update(self, documents, **options): """Perform a b...
3
2010-01-22T11:51:17Z
2,117,332
<p>Exceptions cause the normal program flow to break; then exceptions go up the call stack until they're intercept, or they may reach the top if they aren't. Hence they're employed to mark a really <strong>special condition</strong> that should be handled by the caller. Raising an exception is useful since the program ...
1
2010-01-22T13:06:05Z
[ "exception", "python" ]
What are the cons of returning an Exception instance instead of raising it in Python?
2,116,972
<p>I have been doing some work with python-couchdb and desktopcouch. In one of the patches I submitted I wrapped the <code>db.update</code> function from couchdb. For anyone that is not familiar with python-couchdb the function is the following:</p> <pre><code>def update(self, documents, **options): """Perform a b...
3
2010-01-22T11:51:17Z
2,119,108
<p>Raising an Exception is a notification that something that was expected to work did not work. It breaks the program flow, and should only be done if whatever is going on now is flawed in a way that the program doesn't know how to handle.</p> <p>But sometimes you want to raise a little error flag without breaking pr...
2
2010-01-22T17:23:36Z
[ "exception", "python" ]
How can I query objects with a date field, using a specific month and year?
2,117,056
<p>I need to make a gql query against a set of some objects which have a date field. I am very new to python and also the GAE so I am a bit igorant to this. I am looking in the documentation but cannot find quite what I am looking for. Basically I have made the following class method</p> <pre><code>Event.getEventsF...
1
2010-01-22T12:07:48Z
2,117,673
<p>First, store your dates as DateProperty or DateTimeProperty instances in the datastore.</p> <p>Then, you can do your query something like this:</p> <pre><code>def getEventsForMonth(self, month, year): start_date = datetime.datetime(year, month, 1) if month == 12: end_date = datetime.datetime(year + 1, 1, 1...
3
2010-01-22T14:02:10Z
[ "python", "django", "google-app-engine" ]
Python deep getsizeof list with contents?
2,117,255
<p>I was surprised that <code>sys.getsizeof( 10000*[x] )</code> is 40036 regardless of x: 0, "a", 1000*"a", {}.<br> Is there a <code>deep_getsizeof</code> which properly considers elements that share memory ?<br> (The question came from looking at in-memory database tables like range(1000000) -> province names: list o...
16
2010-01-22T12:49:08Z
2,117,311
<p>10000 * [x] will produce a list of 10000 times <em>the same object</em>, so the sizeof is actually closer to correct than you think. However, a deep sizeof is very problematic because it's impossible to tell Python when you want to stop the measurement. Every object references a typeobject. Should the typeobject be ...
12
2010-01-22T13:00:52Z
[ "python", "memory" ]
Python deep getsizeof list with contents?
2,117,255
<p>I was surprised that <code>sys.getsizeof( 10000*[x] )</code> is 40036 regardless of x: 0, "a", 1000*"a", {}.<br> Is there a <code>deep_getsizeof</code> which properly considers elements that share memory ?<br> (The question came from looking at in-memory database tables like range(1000000) -> province names: list o...
16
2010-01-22T12:49:08Z
2,117,379
<p>If you list is only holding objects with the same length you could get a more accurate estimate number by doing this</p> <pre><code>def getSize(array): return sys.getsizeof(array) + len(array) * sys.getsizeof(array[0]) </code></pre> <p>Obviously it's not going to work as good for strings with variable length.<...
5
2010-01-22T13:14:27Z
[ "python", "memory" ]
Python deep getsizeof list with contents?
2,117,255
<p>I was surprised that <code>sys.getsizeof( 10000*[x] )</code> is 40036 regardless of x: 0, "a", 1000*"a", {}.<br> Is there a <code>deep_getsizeof</code> which properly considers elements that share memory ?<br> (The question came from looking at in-memory database tables like range(1000000) -> province names: list o...
16
2010-01-22T12:49:08Z
2,117,412
<p><code>mylist = 10000 * [x]</code> means <em>create a list of size 10000 with 10000 references to object x</em>.</p> <p>Object <code>x</code> is <strong>not copied</strong> - only a single one exists in memory!!!</p> <p>So to use getsizeof, it would be: <code>sys.getsizeof(mylist) + sys.getsizeof(x)</code></p>
0
2010-01-22T13:21:38Z
[ "python", "memory" ]
Python deep getsizeof list with contents?
2,117,255
<p>I was surprised that <code>sys.getsizeof( 10000*[x] )</code> is 40036 regardless of x: 0, "a", 1000*"a", {}.<br> Is there a <code>deep_getsizeof</code> which properly considers elements that share memory ?<br> (The question came from looking at in-memory database tables like range(1000000) -> province names: list o...
16
2010-01-22T12:49:08Z
2,117,568
<p>Have a look at <a href="http://guppy-pe.sourceforge.net/">guppy/heapy</a>; I haven't played around with it too much myself, but a few of my co-workers have used it for memory profiling with good results.</p> <p>The documentation could be better, but <a href="http://www.pkgcore.org/trac/pkgcore/doc/dev-notes/heapy.r...
5
2010-01-22T13:47:01Z
[ "python", "memory" ]
Python deep getsizeof list with contents?
2,117,255
<p>I was surprised that <code>sys.getsizeof( 10000*[x] )</code> is 40036 regardless of x: 0, "a", 1000*"a", {}.<br> Is there a <code>deep_getsizeof</code> which properly considers elements that share memory ?<br> (The question came from looking at in-memory database tables like range(1000000) -> province names: list o...
16
2010-01-22T12:49:08Z
10,858,766
<p>There's a more recent effort <a href="http://packages.python.org/Pympler/index.html" rel="nofollow" title="in the pympler package">in the Pympler package</a> that works for Python 2.7+ as well. </p>
0
2012-06-02T00:06:29Z
[ "python", "memory" ]
Python :: How to open a page in the Non Default browser
2,117,545
<p>I was trying to create a simple script to open a locally hosted web site for testing the css in 2 or more browsers. The default browser is IE7 and it opens the page fine but when I try to open a non default browser such as Firefox or Arora it just fails.</p> <p>I am using the webbrowser module and have tried this ...
4
2010-01-22T13:44:24Z
2,117,600
<p>This basically boils down to:</p> <pre><code>- run 'firefox "url"' - run 'iexplore "url"' - run 'other_browser "url"' </code></pre> <p>I don't know enough python to know how the system() call is implemented there but it should be quite simple.</p>
0
2010-01-22T13:50:36Z
[ "python", "browser" ]
Python :: How to open a page in the Non Default browser
2,117,545
<p>I was trying to create a simple script to open a locally hosted web site for testing the css in 2 or more browsers. The default browser is IE7 and it opens the page fine but when I try to open a non default browser such as Firefox or Arora it just fails.</p> <p>I am using the webbrowser module and have tried this ...
4
2010-01-22T13:44:24Z
2,117,730
<p>The <a href="http://www.python.org/doc/2.4/lib/module-subprocess.html" rel="nofollow">subprocess module</a> should provide what you want if you feed subprocess the path to the browser. Note that you need Python 2.4 or later to use subprocess, but that's common nowadays.</p> <p>Update - code for a method to call Chr...
1
2010-01-22T14:09:45Z
[ "python", "browser" ]
Python :: How to open a page in the Non Default browser
2,117,545
<p>I was trying to create a simple script to open a locally hosted web site for testing the css in 2 or more browsers. The default browser is IE7 and it opens the page fine but when I try to open a non default browser such as Firefox or Arora it just fails.</p> <p>I am using the webbrowser module and have tried this ...
4
2010-01-22T13:44:24Z
2,118,786
<p>Matt's right and it's a pretty useful module to know...</p> <p><a href="http://docs.python.org/library/subprocess.html#module-subprocess" rel="nofollow">18.1. subprocess</a></p> <pre><code>IDLE 2.6.2 &gt;&gt;&gt; import subprocess &gt;&gt;&gt; chrome = 'C:\Users\Ted\AppData\Local\Google\Chrome\Application\ch...
3
2010-01-22T16:38:39Z
[ "python", "browser" ]
How can I check compilation errors in python?
2,117,586
<pre><code>#!/usr/bin/python str = "this" if(1): print "Hi" else: print str.any_random_function() </code></pre> <p>This doesn't fail when I run the program. I tried py_compile but that didn't indicate the error in the 'else' loop either. Now how can I compile the program and detect errors reliably in p...
9
2010-01-22T13:48:44Z
2,117,602
<p>I think your best bet would be <a href="http://pypi.python.org/pypi/pylint">pylint</a>.</p>
4
2010-01-22T13:50:53Z
[ "python", "compilation" ]
How can I check compilation errors in python?
2,117,586
<pre><code>#!/usr/bin/python str = "this" if(1): print "Hi" else: print str.any_random_function() </code></pre> <p>This doesn't fail when I run the program. I tried py_compile but that didn't indicate the error in the 'else' loop either. Now how can I compile the program and detect errors reliably in p...
9
2010-01-22T13:48:44Z
2,117,652
<p>Python is a dynamic language, so you can't simply check for compiling errors like in static languages (C/C++/Java). If you assign <code>str.any_random_function</code>, the above code would be correct (okay that's a bad example...).</p> <p>I'd suggest you to use <a href="http://pydev.org/" rel="nofollow">PyDev for E...
2
2010-01-22T13:57:52Z
[ "python", "compilation" ]
Is there any metaprogramming patterns catalog for Python?
2,117,927
<p>I have just read Python Cookbook. The book is amazing. </p> <p>I think the best use of this book is that it provides lots of examples that show python in real problem applications. Many of the idioms include metaprogramming techniques.</p> <p>I wonder if there is any catalog that summarizes metaprogramming idioms ...
8
2010-01-22T14:38:25Z
2,117,967
<ul> <li><a href="http://www.oreillynet.com/pub/a/python/2003/04/17/metaclasses.html" rel="nofollow">A Primer on Python Metaclass Programming</a>.</li> <li><a href="http://www.ibm.com/developerworks/linux/library/l-pymeta.html" rel="nofollow">http://www.ibm.com/developerworks/linux/library/l-pymeta.html</a></li> </ul>
4
2010-01-22T14:44:02Z
[ "python", "design-patterns", "metaprogramming", "idioms" ]
Parse boolean arithmetic including parentheses with regex?
2,118,261
<p>Is there a single regular expression that can parse a string (in Python and/or Javascript, does not need to be the same expression) that represents simple boolean arithmetic? For example I want to parse this string:</p> <pre><code>a and (b and c) and d or e and (f or g) </code></pre> <p>Assuming that:<br> * parent...
3
2010-01-22T15:25:50Z
2,118,309
<p>Normally you would use for example a <a href="http://en.wikipedia.org/wiki/Recursive_descent_parser" rel="nofollow">recursive descent parser</a> for this task, but you can grab all the parts (tokens) with a regex:</p> <pre><code>x = 'a and (b and c) and d or e and (f or g)' import re matches = re.findall(r'\(.*?\)...
2
2010-01-22T15:33:22Z
[ "javascript", "python", "regex" ]
Parse boolean arithmetic including parentheses with regex?
2,118,261
<p>Is there a single regular expression that can parse a string (in Python and/or Javascript, does not need to be the same expression) that represents simple boolean arithmetic? For example I want to parse this string:</p> <pre><code>a and (b and c) and d or e and (f or g) </code></pre> <p>Assuming that:<br> * parent...
3
2010-01-22T15:25:50Z
2,118,589
<p>Assuming no nesting simplifies it to a level where regex can be used. A regex to match that would be (assuming and/or only, can be easily extended):</p> <pre><code>&gt;&gt;&gt; expr = 'a and (b and c) and d or e and (f or g)' &gt;&gt;&gt; regex = re.compile('\((\w+)\s+(and|or)\s+(\w)\)|(\w+)') &gt;&gt;&gt; results ...
1
2010-01-22T16:09:59Z
[ "javascript", "python", "regex" ]
Parse boolean arithmetic including parentheses with regex?
2,118,261
<p>Is there a single regular expression that can parse a string (in Python and/or Javascript, does not need to be the same expression) that represents simple boolean arithmetic? For example I want to parse this string:</p> <pre><code>a and (b and c) and d or e and (f or g) </code></pre> <p>Assuming that:<br> * parent...
3
2010-01-22T15:25:50Z
2,121,454
<p>The <a href="http://pyparsing.wikispaces.com/Examples" rel="nofollow">Examples page</a> on the pyparsing wiki includes a sample SimpleBool.py that will parse and evaluate expressions such as:</p> <pre><code>test = ["p and not q", "not not p", "not(p and q)", "q or not p and r", "q or...
1
2010-01-23T00:20:57Z
[ "javascript", "python", "regex" ]
Python List Exclusions
2,118,503
<p>I have a dictionary of lists with info such as <code>var1=vara</code>, <code>var1=varb</code>, <code>var2=vara</code> etc. This can have lots of entries, and I print it out ok like this </p> <pre><code>for y in myDict: print(y+"\t"+myDict[y]) </code></pre> <p>I have another list which has exclusions in like...
0
2010-01-22T15:59:15Z
2,118,542
<p>Do you mean</p> <pre><code>for key in myDict: if key not in myList: print(key+"\t"+myDict[key]) </code></pre> <p>Or one of many alternatives:</p> <pre><code>for key in (set(myDict)-set(myList)): print(key+"\t"+myDict[key]) </code></pre>
5
2010-01-22T16:04:20Z
[ "python" ]
Python List Exclusions
2,118,503
<p>I have a dictionary of lists with info such as <code>var1=vara</code>, <code>var1=varb</code>, <code>var2=vara</code> etc. This can have lots of entries, and I print it out ok like this </p> <pre><code>for y in myDict: print(y+"\t"+myDict[y]) </code></pre> <p>I have another list which has exclusions in like...
0
2010-01-22T15:59:15Z
2,118,543
<pre><code>mySet = set(myList) myNewDict = dict(((k, v) for k, v in myDict if k not in mySet)) </code></pre> <p>Note that using <code>mySet</code> instead of <code>myList</code> isn't a concern unless myList has a large number of entries.</p>
1
2010-01-22T16:04:40Z
[ "python" ]
GAE email me errors
2,118,524
<p>Can GAE be configured to bust me an email when there's an error?</p>
3
2010-01-22T16:01:48Z
2,119,644
<p>I think the best you can do is to have in your <code>main</code> function some code like...:</p> <pre><code>try: ...normal body of your main goes here... except: from google.appengine.api import mail import sys mail.send_mail(sender="Your GAE App &lt;yourappname@example.com&gt;", to="You &lt;...
4
2010-01-22T18:50:44Z
[ "python", "google-app-engine" ]
GAE email me errors
2,118,524
<p>Can GAE be configured to bust me an email when there's an error?</p>
3
2010-01-22T16:01:48Z
4,263,164
<p>Here's an example of using GAE to send an email. You could build on this example to catch exceptions and send an email to yourself....</p> <p><a href="http://www.fishbonecloud.com/2010/11/automated-email-using-google-app-engine.html" rel="nofollow">http://www.fishbonecloud.com/2010/11/automated-email-using-google-...
0
2010-11-24T03:15:53Z
[ "python", "google-app-engine" ]