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
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
550,477
<p>In Python 2.x you can't, because print isn't a function, it's a statement. In Python 3 print is a function, so I suppose it could be overridden (haven't tried it, though).</p>
2
2009-02-15T07:29:39Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
550,483
<p>I don't think you can overload print, but Python has a robust logging package that is highly customizable.</p> <p><a href="http://docs.python.org/library/logging.html" rel="nofollow">http://docs.python.org/library/logging.html</a></p>
3
2009-02-15T07:32:41Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
550,488
<p>Overloading <code>print</code> is a design feature of python 3.0 to address your lack of ability to do so in python 2.x.</p> <p>However, you can override sys.stdout. (<a href="http://code.activestate.com/recipes/119404/">example</a>.) Just assign it to another file-like object that does what you want.</p> <p>Alter...
26
2009-02-15T07:37:01Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
550,546
<p>Though you can't replace the <code>print</code> keyword (in Python 2.x <code>print</code> is a keyword), it's common practice to replace <code>sys.stdout</code> to do something similar to <code>print</code> overriding; for example, with an instance of <code>StringIO.StringIO</code>. This will capture all of the prin...
3
2009-02-15T08:53:28Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
688,816
<p>I came across the same problem.</p> <p>How about this:</p> <pre><code>class writer : def __init__(self, *writers) : self.writers = writers def write(self, text) : for w in self.writers : w.write(text) import sys saved = sys.stdout fout = file('out.log', 'w') sys.stdout = writer(sys.stdou...
9
2009-03-27T07:22:31Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
1,818,580
<p>I answered the same question <a href="http://stackoverflow.com/questions/770657/python-overridding-print/1818572#1818572">on a different SO question</a></p> <p>Essentially, simplest solution is to just redirect the output to stderr as follows, in the wsgi config file.</p> <pre><code>sys.stdout = sys.stderr </code>...
1
2009-11-30T08:51:41Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
10,106,489
<p>For those reviewing the previously dated answers, as of version release "Python 2.6" there is a new answer to the original poster's question.</p> <p>In Python 2.6 and up, you can disable the print statement in favor of the print function, and then override the print function with your own print function:</p> <pre>...
32
2012-04-11T13:06:25Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
15,590,040
<pre><code>class MovieDesc: name = "Name" genders = "Genders" country = "Country" def __str__(self): #Do whatever you want here return "Name: {0}\tGenders: {1} Country: {2} ".format(self.name,self.genders,self.country) ) </code></pre>
4
2013-03-23T17:45:25Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
21,959,061
<p>just thought I'd add my idea... suited my purposes of being able to run sthg in Eclipse and then run from the (Windows) CLI without getting encoding exceptions with each print statement. Whatever you do don't make EncodingStdout a subclass of class file: the line "self.encoding = encoding" would then result in the e...
1
2014-02-22T19:05:39Z
[ "python", "printing", "overloading" ]
overload print python
550,470
<p>Am i able to overload the print function? and call the normal function? What i want to do is after a specific line i want print to call my print which will call the normal print and write a copy to file.</p> <p>Also i dont know how to overload print. I dont know how to do variable length arguments. i'll look it up ...
26
2009-02-15T07:26:05Z
36,407,330
<p>For a very simple example, as of Python3.4 (haven't tested with older versions) this works well for me (placed at top of module):</p> <pre><code>def dprint(string): __builtins__.print("%f -- %s" % (time.time(), string)) print = dprint </code></pre> <p>Note, this only works if the string parameter is a str... Y...
0
2016-04-04T15:53:01Z
[ "python", "printing", "overloading" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
550,644
<p>I'm just going to start with a tip from myself :)</p> <p><strong>Use os.path.dirname() in settings.py to avoid hardcoded dirnames.</strong></p> <p>Don't hardcode path's in your settings.py if you want to run your project in different locations. Use the following code in settings.py if your templates and static fil...
222
2009-02-15T10:15:52Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
551,114
<p>Use <a href="http://bitbucket.org/offline/django-annoying/wiki/Home" rel="nofollow">django-annoying's</a> <code>render_to</code> decorator instead of <code>render_to_response</code>.</p> <pre><code>@render_to('template.html') def foo(request): bars = Bar.objects.all() if request.user.is_authenticated(): ...
119
2009-02-15T16:06:32Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
551,139
<p>I like to use the Python debugger pdb to debug Django projects.</p> <p>This is a helpful link for learning how to use it: <a href="http://www.ferg.org/papers/debugging_in_python.html" rel="nofollow">http://www.ferg.org/papers/debugging_in_python.html</a></p>
37
2009-02-15T16:20:26Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
551,158
<p>Use <a href="http://jinja.pocoo.org/2/" rel="nofollow">Jinja2</a> alongside Django.</p> <p>If you find the Django template language extremely restricting (like me!) then you don't have to be stuck with it. Django is flexible, and the template language is loosely coupled to the rest of the system, so just plug-in an...
35
2009-02-15T16:30:50Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
551,499
<p>Add <code>assert False</code> in your view code to dump debug information.</p>
35
2009-02-15T20:06:58Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
551,615
<p>Install <a href="http://code.google.com/p/django-command-extensions">Django Command Extensions</a> and <a href="http://networkx.lanl.gov/pygraphviz/">pygraphviz</a> and then issue the following command to get a really nice looking Django model visualization:</p> <pre><code>./manage.py graph_models -a -g -o my_proje...
130
2009-02-15T21:18:14Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
558,401
<p>Don't hard-code your URLs! </p> <p>Use <a href="http://docs.djangoproject.com/en/dev/topics/http/urls/#id2" rel="nofollow">url names</a> instead, and the <a href="http://docs.djangoproject.com/en/dev/topics/http/urls/#reverse" rel="nofollow"><code>reverse</code></a> function to get the URL itself.</p> <p>When you ...
88
2009-02-17T19:34:58Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
559,972
<p>Don't write your own login pages. If you're using django.contrib.auth.</p> <p>The real, dirty secret is that if you're also using django.contrib.admin, and django.template.loaders.app_directories.load_template_source is in your template loaders, <strong>you can get your templates free too!</strong></p> <pre><cod...
80
2009-02-18T05:37:30Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
560,064
<p><a href="http://docs.djangoproject.com/en/dev/ref/generic-views/?from=olddocs#django-views-generic-list-detail-object-list" rel="nofollow">django.views.generic.list_detail.object_list</a> -- It provides all the logic &amp; template variables for pagination (one of those I've-written-that-a-thousand-times-now drudger...
16
2009-02-18T06:44:49Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
560,111
<p>This adds to the reply above about <a href="http://stackoverflow.com/questions/550632/favorite-django-tips-features/558401#558401">Django URL names and reverse URL dispatching</a>.</p> <p>The URL names can also be effectively used within templates. For example, for a given URL pattern:</p> <pre><code>url(r'(?P&lt;...
33
2009-02-18T07:07:30Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
562,998
<p>When I was starting out, I didn't know that there was a <a href="http://docs.djangoproject.com/en/dev/topics/pagination/#topics-pagination" rel="nofollow">Paginator</a>, make sure you know of its existence!!</p>
57
2009-02-18T21:54:03Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
709,180
<p>When trying to exchange data between Django and another application, <code>request.raw_post_data</code> is a good friend. Use it to receive and custom-process, say, XML data.</p> <p>Documentation: <a href="http://docs.djangoproject.com/en/dev/ref/request-response/" rel="nofollow">http://docs.djangoproject.com/en/de...
37
2009-04-02T10:30:16Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
864,898
<p><code>django.db.models.get_model</code> does allow you to retrieve a model without importing it.</p> <p>James shows how handy it can be: <a href="http://www.b-list.org/weblog/2006/jun/07/django-tips-write-better-template-tags/" rel="nofollow">"Django tips: Write better template tags — Iteration 4 "</a>.</p>
19
2009-05-14T18:19:48Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
942,214
<p><a href="http://code.djangoproject.com/wiki/TemplatedForm" rel="nofollow">Render form via django template instead of as_(ul|table|p)()</a>.</p> <p>This article shows, how to use template to render CusstomForms instead of <code>as_p()</code>, <code>as_table()</code>...</p> <p>To make it work change </p> <ul> <li><...
5
2009-06-02T22:22:35Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
942,237
<p>Use <a href="http://code.google.com/p/isapi-wsgi/" rel="nofollow">isapi-wsgi</a> and <a href="http://code.google.com/p/django-pyodbc/" rel="nofollow">django-pyodbc</a> to run Django on Windows using IIS and SQL Server!</p>
1
2009-06-02T22:27:43Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
942,426
<p>The <a href="http://docs.djangoproject.com/en/dev/ref/contrib/webdesign/#ref-contrib-webdesign" rel="nofollow">webdesign app</a> is very useful when starting to design your website. Once imported, you can add this to generate sample text:</p> <pre><code>{% load webdesign %} {% lorem 5 p %} </code></pre>
19
2009-06-02T23:32:21Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
946,397
<p>There's a set of custom tags I use all over my site's templates. Looking for a way to autoload it (DRY, remember?), I found the following:</p> <pre><code>from django import template template.add_to_builtins('project.app.templatetags.custom_tag_module') </code></pre> <p>If you put this in a module that's loaded by ...
103
2009-06-03T18:34:02Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
946,443
<p><a href="http://pypi.python.org/pypi/virtualenv#what-it-does" rel="nofollow">Virtualenv</a> + Python = life saver if you are working on multiple Django projects and there is a possibility that they all don't depend on the same version of Django/an application.</p>
97
2009-06-03T18:44:29Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
946,497
<p>Use <a href="http://github.com/robhudson/django-debug-toolbar/">django debug toolbar</a>. For example, it allows to view all SQL queries performed while rendering view and you can also view stacktrace for any of them.</p>
82
2009-06-03T18:53:46Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,018,895
<p>I don't have enough reputation to reply to the comment in question, but it's important to note that if you're going to use <a href="http://en.wikipedia.org/wiki/Jinja%5F%28template%5Fengine%29" rel="nofollow">Jinja</a>, it does NOT support the '-' character in template block names, while Django does. This caused me ...
20
2009-06-19T16:35:18Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,057,512
<p>Just found this link: <a href="http://lincolnloop.com/django-best-practices/#table-of-contents" rel="nofollow">http://lincolnloop.com/django-best-practices/#table-of-contents</a> - "Django Best Practices".</p>
13
2009-06-29T10:01:07Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,067,267
<p>From the <a href="http://docs.djangoproject.com/en/dev/ref/django-admin/" rel="nofollow">django-admin documentation</a>:</p> <p>If you use the Bash shell, consider installing the Django bash completion script, which lives in <code>extras/django_bash_completion</code> in the Django distribution. It enables tab-compl...
41
2009-07-01T04:27:35Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,300,107
<h2>Context processors are awesome.</h2> <p>Say you have a different user model and you want to include that in every response. Instead of doing this:</p> <pre><code>def myview(request, arg, arg2=None, template='my/template.html'): ''' My view... ''' response = dict() myuser = MyUser.objects.get(user=requ...
67
2009-08-19T13:53:50Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,346,574
<p>I learned this one from the documentation for the <a href="http://code.google.com/p/sorl-thumbnail/" rel="nofollow">sorl-thumbnails</a> app. You can use the "as" keyword in template tags to use the results of the call elsewhere in your template.</p> <p>For example:</p> <pre><code>{% url image-processor uid as img_...
18
2009-08-28T11:59:46Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,359,196
<p>Since Django "views" only need to be callables that return an HttpResponse, you can easily create class-based views like those in Ruby on Rails and other frameworks.</p> <p>There are several ways to create class-based views, here's my favorite:</p> <pre><code>from django import http class RestView(object): me...
27
2009-08-31T20:12:58Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,407,331
<p>Use <a href="http://docs.djangoproject.com/en/dev/topics/signals/" rel="nofollow">signals</a> to add accessor-methods on-the-fly.</p> <p>I saw this technique in <a href="http://code.google.com/p/django-photologue" rel="nofollow">django-photologue</a>: For any Size object added, the post_init signal will add the cor...
11
2009-09-10T19:46:16Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,656,759
<p>Use <strong><a href="http://pypi.python.org/pypi/djangorecipe" rel="nofollow">djangorecipe</a></strong> to manage your project</p> <ul> <li>If you're writing a new app, this recipe makes testing it outside of a project really easy</li> <li>It allows you to manage dependencies for a project (e.g. what version of som...
5
2009-11-01T09:47:20Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,735,782
<p>Instead of using <code>render_to_response</code> to bind your context to a template and render it (which is what the Django docs usually show) use the generic view <a href="http://docs.djangoproject.com/en/dev/ref/generic-views/#django-views-generic-simple-direct-to-template" rel="nofollow"><code>direct_to_template<...
22
2009-11-14T22:15:03Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,774,722
<p>Instead of running the Django dev server on localhost, run it on a proper network interface. For example:</p> <pre><code>python manage.py runserver 192.168.1.110:8000 </code></pre> <p>or </p> <pre><code>python manage.py runserver 0.0.0.0:8000 </code></pre> <p>Then you can not only easily use Fiddler (<a href="ht...
9
2009-11-21T06:29:49Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,837,525
<p>Use <a href="http://ipython.scipy.org/moin/" rel="nofollow">IPython</a> to jump into your code at any level and debug using the power of IPython. Once you have installed IPython just put this code in wherever you want to debug: </p> <pre><code>from IPython.Shell import IPShellEmbed; IPShellEmbed()() </code></pre> ...
47
2009-12-03T03:55:57Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
1,837,571
<p>This is a really easy way to never have to import another one of your models again in your python shell.</p> <p>First, install <a href="http://ipython.scipy.org/moin/" rel="nofollow">IPython</a> (If you don't use IPython, what's wrong with you?). Next, create a python script, ipythonrc.py, in your django project d...
4
2009-12-03T04:08:47Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
2,093,549
<p>Everybody knows there is a development server you can run with "manage.py runserver", but did you know that there is a development view for serving static files (CSS / JS / IMG) as well ?</p> <p>Newcomers are always puzzled because Django doesn't come with any way to serve static files. This is because the dev team...
19
2010-01-19T12:57:39Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
2,260,327
<p>Run a development SMTP server that will just output whatever is sent to it (if you don't want to actually install SMTP on your dev server.)</p> <p>command line:</p> <pre><code>python -m smtpd -n -c DebuggingServer localhost:1025 </code></pre>
43
2010-02-14T06:26:28Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
2,740,163
<p>Use <code>wraps</code> decorator in custom views decorators to preserve view's name, module and docstring. E.g.</p> <pre><code>try: from functools import wraps except ImportError: from django.utils.functional import wraps # Python 2.3, 2.4 fallback. def view_decorator(fun): @wraps(fun) def wrapper...
7
2010-04-29T19:22:46Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
2,751,177
<p><strong>Changing Django form field properties on init</strong></p> <p>Sometimes it's useful to pass extra arguments to a Form class.</p> <pre><code>from django import forms from mymodels import Group class MyForm(forms.Form): group=forms.ModelChoiceField(queryset=None) email=forms.EmailField() some_ch...
4
2010-05-01T18:51:42Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
2,830,887
<p><a href="http://www.jetbrains.com/pycharm/" rel="nofollow">PyCharm IDE</a> is a nice environment to code and especially debug, with built-in support for Django.</p>
16
2010-05-13T23:04:24Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
2,832,479
<p>The <a href="http://github.com/robhudson/django-debug-toolbar" rel="nofollow">Django Debug Toolbar</a> is really fantastic. Not really a toolbar, it actually brings up a sidepane that tells you all sorts of information about what brought you the page you're looking at - DB queries, the context variables sent to the ...
7
2010-05-14T07:23:52Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
3,247,524
<p>Use <a href="http://djangorestmodel.sourceforge.net/" rel="nofollow">xml_models</a> to create Django models that use an XML REST API backend (instead of a SQL one). This is very useful especially when modelling third party APIs - you get all the same QuerySet syntax that you're used to. You can install it from PyP...
14
2010-07-14T15:06:49Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
3,262,408
<p>Use reverse in your urlconf.</p> <p>This is one of those tricks where I don't understand why it isn't the default.</p> <p>Here's a link to where I picked it up: <a href="http://andr.in/2009/11/21/calling-reverse-in-django/">http://andr.in/2009/11/21/calling-reverse-in-django/</a></p> <p>Here's the code snippet:</...
5
2010-07-16T06:27:08Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
4,007,017
<p>Create dynamic models for sets of legacy tables with the same structure:</p> <pre><code>class BaseStructure(models.Model): name = models.CharField(max_length=100) address = models.CharField(max_length=100) class Meta: abstract=True class DynamicTable(models.Model): table_name = models.Char...
1
2010-10-24T03:17:55Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
4,007,078
<h1>Remove Database Access Information from settings.py</h1> <p>One thing I've done in my Django site's <code>settings.py</code> is load database access info from a file in <code>/etc</code>. This way the access setup (database host, port, username, password) can be different for each machine, and sensitive info like...
11
2010-10-24T03:41:54Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
4,861,267
<p>The <code>./manage.py runserver_plus</code> facilty which comes with <a href="http://code.google.com/p/django-command-extensions/" rel="nofollow">django_extensions</a> is truly awesome. </p> <p>It creates an enhanced debug page that, amongst other things, uses the Werkzeug debugger to create interactive debugging ...
40
2011-02-01T10:11:58Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
4,888,160
<p>Using an 'apps' folder to organize your applications without editing PYTHONPATH</p> <p>This has come handy when I want to organize my folders like this:</p> <pre><code>apps/ foo/ bar/ site/ settings.py urls.py </code></pre> <p>without overwritting PYTHONPATH or having to add <strong>apps</strong> to every...
6
2011-02-03T15:37:33Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
5,546,778
<p>Instead of evaluating whole queryset to check whether you got back any results, use .exists() in Django 1.2+ and .count() for previous versions.</p> <p>Both exists() and count() clears order by clauses and retrieves a single integer from DB. However exists() will always return 1 where as count may return higher val...
12
2011-04-05T03:12:34Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
5,557,751
<p><code>django_extensions</code> from <a href="https://github.com/django-extensions/django-extensions" rel="nofollow">https://github.com/django-extensions/django-extensions</a> is just great.</p> <p>Few nice <code>./manage.py</code> commands:</p> <ul> <li><code>shell_plus</code> - autoimports models from all INSTALL...
2
2011-04-05T19:59:33Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
5,753,606
<p>PyCharm and Wingware IDE is great tool if you have money to pay for the license. </p> <p>Since I am a poor developer, I use <a href="http://pydev.org/" rel="nofollow">PyDev</a> with <a href="http://www.eclipse.org/" rel="nofollow">Eclipse</a>. </p>
2
2011-04-22T08:22:38Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
5,757,092
<p>When passing variables from a view to a template the response dictionary can become tedious to type out. I find it nice to just pass all the local variables at once using <code>locals()</code> .</p> <pre><code>def show_thing(request, thing_id): thing = Thing.objects.get(pk=thing_id) return render_to_respons...
0
2011-04-22T15:40:32Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
5,809,809
<p>Use database migrations. Use <a href="http://south.aeracode.org" rel="nofollow">South</a>.</p>
14
2011-04-27T19:59:16Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
6,008,305
<p>A bit late to the party. But Django Canvas has recently come out and it deserves a place here.</p> <p>Don't start your project with <code>django-admin.py startproject</code>. Instead you can use something like <a href="http://djangocanvas.com" rel="nofollow">Django Canvas</a> to help piece together a blank project ...
1
2011-05-15T12:13:44Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
6,169,597
<p>If you make changes into model</p> <pre><code>./manage.py dumpdata appname &gt; appname_data.json ./manage.py reset appname django-admin.py loaddata appname_data.json </code></pre>
12
2011-05-29T18:31:22Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
6,326,248
<p><strong>dir() &amp; raise ValueError()</strong></p> <p>For debugging / exploring the state of things during development, I use the following trick:</p> <pre><code>... to_see = dir(inspect_this_thing) to_see2 = inspect_this_thing.some_attribute raise ValueError("Debugging") ... </code></pre> <p>This is espec...
0
2011-06-13T02:54:49Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
7,140,786
<p>Automatically set 'DEBUG' attribute on production environment (settings.py)</p> <pre><code>import socket if socket.gethostname() == 'productionserver.com': DEBUG = False else: DEBUG = True </code></pre> <p>By: <a href="http://nicksergeant.com/2008/automatically-setting-debug-in-your-django-app-based-on-se...
5
2011-08-21T20:05:03Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
8,065,577
<p>Django hasn't got app settings, so i made my own app_settings.py detection. At the bottom of the settings.py i added this code:</p> <pre><code>import sys, os # Append application settings without triggering the __init__. for installed_app in INSTALLED_APPS: # Ignore django applications if not installed_app....
0
2011-11-09T13:17:37Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
8,147,991
<p>Use asynchronous tasks. Use <a href="http://celeryproject.org/" rel="nofollow">Celery</a></p>
1
2011-11-16T07:14:08Z
[ "python", "django", "hidden-features" ]
Favorite Django Tips & Features?
550,632
<p>Inspired by the question series 'Hidden features of ...', I am curious to hear about your favorite Django tips or lesser known but useful features you know of.</p> <ul> <li>Please, include only one tip per answer.</li> <li>Add Django version requirements if there are any.</li> </ul>
309
2009-02-15T10:06:22Z
8,512,853
<p>Read <a href="http://thebuild.com/presentations/unbreaking-django.pdf" rel="nofollow">Unbreaking Django</a> if you haven't already. It contains <em>lots</em> of useful information regarding django pitfalls.</p>
1
2011-12-14T22:48:04Z
[ "python", "django", "hidden-features" ]
Cross-platform way to get PIDs by process name in python
550,653
<p>Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using <strong>python</strong> or <strong>jython</strong>?</p> <ol> <li>I want something like <code>pidof</code> but in python. (I don't have <code>pidof</code> anyway.)</li> <li>I can't pa...
36
2009-02-15T10:23:56Z
550,672
<p>I don't think you will be able to find a purely python-based, portable solution without using /proc or command line utilities, at least not in python itself. Parsing os.system is not ugly - someone has to deal with the multiple platforms, be it you or someone else. Implementing it for the OS you are interested in sh...
3
2009-02-15T10:35:56Z
[ "python", "cross-platform", "jython", "hp-ux" ]
Cross-platform way to get PIDs by process name in python
550,653
<p>Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using <strong>python</strong> or <strong>jython</strong>?</p> <ol> <li>I want something like <code>pidof</code> but in python. (I don't have <code>pidof</code> anyway.)</li> <li>I can't pa...
36
2009-02-15T10:23:56Z
550,819
<p>First, Windows (in all it's incarnations) is a non-standard OS.</p> <p>Linux (and most proprietary unixen) are POSIX-compliant standard operating systems.</p> <p>The C libraries reflect this dichotomy. Python reflects the C libraries.</p> <p>There is no "cross-platform" way to do this. You have to hack up somet...
3
2009-02-15T12:43:05Z
[ "python", "cross-platform", "jython", "hp-ux" ]
Cross-platform way to get PIDs by process name in python
550,653
<p>Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using <strong>python</strong> or <strong>jython</strong>?</p> <ol> <li>I want something like <code>pidof</code> but in python. (I don't have <code>pidof</code> anyway.)</li> <li>I can't pa...
36
2009-02-15T10:23:56Z
557,021
<p>There isn't, I'm afraid. Processes are uniquely identified by pid not by name. If you really must find a pid by name, then you will have use something like you have suggested, but it won't be portable and probably will not work in all cases.</p> <p>If you only have to find the pids for a certain application and you...
1
2009-02-17T14:21:30Z
[ "python", "cross-platform", "jython", "hp-ux" ]
Cross-platform way to get PIDs by process name in python
550,653
<p>Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using <strong>python</strong> or <strong>jython</strong>?</p> <ol> <li>I want something like <code>pidof</code> but in python. (I don't have <code>pidof</code> anyway.)</li> <li>I can't pa...
36
2009-02-15T10:23:56Z
727,024
<p>For jython, if Java 5 is used, then you can get the Java process id as following:</p> <p>from java.lang.management import *<br/> pid = ManagementFactory.getRuntimeMXBean().getName()</p>
1
2009-04-07T18:30:37Z
[ "python", "cross-platform", "jython", "hp-ux" ]
Cross-platform way to get PIDs by process name in python
550,653
<p>Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using <strong>python</strong> or <strong>jython</strong>?</p> <ol> <li>I want something like <code>pidof</code> but in python. (I don't have <code>pidof</code> anyway.)</li> <li>I can't pa...
36
2009-02-15T10:23:56Z
1,226,643
<p>There's no single cross-platform API, you'll have to check for OS. For posix based use /proc. For Windows use following code to get list of all pids with coresponding process names</p> <pre><code>from win32com.client import GetObject WMI = GetObject('winmgmts:') processes = WMI.InstancesOf('Win32_Process') process_...
10
2009-08-04T10:00:18Z
[ "python", "cross-platform", "jython", "hp-ux" ]
Cross-platform way to get PIDs by process name in python
550,653
<p>Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using <strong>python</strong> or <strong>jython</strong>?</p> <ol> <li>I want something like <code>pidof</code> but in python. (I don't have <code>pidof</code> anyway.)</li> <li>I can't pa...
36
2009-02-15T10:23:56Z
2,241,047
<p>You can use psutil ( <a href="https://github.com/giampaolo/psutil" rel="nofollow">https://github.com/giampaolo/psutil</a> ), which works on Windows and UNIX:</p> <pre> import psutil PROCNAME = "python.exe" for proc in psutil.process_iter(): if proc.name() == PROCNAME: print(proc) </pre> <p>The code a...
45
2010-02-10T23:12:17Z
[ "python", "cross-platform", "jython", "hp-ux" ]
Cross-platform way to get PIDs by process name in python
550,653
<p>Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using <strong>python</strong> or <strong>jython</strong>?</p> <ol> <li>I want something like <code>pidof</code> but in python. (I don't have <code>pidof</code> anyway.)</li> <li>I can't pa...
36
2009-02-15T10:23:56Z
14,792,920
<pre><code>import psutil process = filter(lambda p: p.name == "YourProcess.exe", psutil.process_iter()) for i in process: print i.name,i.pid </code></pre> <p>Give all pids of "YourProcess.exe"</p>
4
2013-02-09T23:42:35Z
[ "python", "cross-platform", "jython", "hp-ux" ]
python, dictionary and int error
550,673
<p>I have a very frustrating python problem. In this code</p> <pre><code>fixedKeyStringInAVar = "SomeKey" def myFunc(a, b): global sleepTime global fixedKeyStringInAVar varMe=int("15") sleepTime[fixedKeyStringInAVar] = varMe*60*1000 #more code </code></pre> <p>Now this works. BUT sometimes when I...
0
2009-02-15T10:37:57Z
550,679
<p>Your sleepTime is a global variable. It could be changed to be an int at some point in your program.</p> <p>The item assignment is the "foo[bar] = baz" construction in your function.</p>
1
2009-02-15T10:40:58Z
[ "python", "dictionary", "global" ]
python, dictionary and int error
550,673
<p>I have a very frustrating python problem. In this code</p> <pre><code>fixedKeyStringInAVar = "SomeKey" def myFunc(a, b): global sleepTime global fixedKeyStringInAVar varMe=int("15") sleepTime[fixedKeyStringInAVar] = varMe*60*1000 #more code </code></pre> <p>Now this works. BUT sometimes when I...
0
2009-02-15T10:37:57Z
550,722
<ol> <li><p>Don't use <code>global</code> keyword in a function unless you'd like to change binding of a global name.</p></li> <li><p>Search for '<code>sleepTime =</code>' in your code. You are binding an int object to the <code>sleepTime</code> name at some point in your program.</p></li> </ol>
5
2009-02-15T11:12:08Z
[ "python", "dictionary", "global" ]
python runtime error, can dump a file?
550,804
<p>I am using libcurl to DL a webpage, then i am scanning it for data and doing something with one of the links. However, once in a while the page is different then i except thus i extract bad data and pycurl throws an exception. I tried finding the exception name for pycurl but had no luck.</p> <p>Is there a way i ca...
2
2009-02-15T12:29:46Z
550,815
<p>Can you catch all exceptions somewhere in the main block and use sys.exc_info() for callback information and log that to your file. exc_info() returns not just exception type, but also call traceback so there should information what went wrong.</p>
2
2009-02-15T12:40:18Z
[ "python", "error-handling", "pycurl" ]
python runtime error, can dump a file?
550,804
<p>I am using libcurl to DL a webpage, then i am scanning it for data and doing something with one of the links. However, once in a while the page is different then i except thus i extract bad data and pycurl throws an exception. I tried finding the exception name for pycurl but had no luck.</p> <p>Is there a way i ca...
2
2009-02-15T12:29:46Z
550,822
<p><a href="http://docs.python.org/library/sys.html#sys.excepthook" rel="nofollow">sys.excepthook</a> may help you here, where you can set a global exception handler. I am not sure how pycurl exceptions are handled, it being a binding library, but it will probably work to reassign it to a generic function. Something li...
3
2009-02-15T12:45:56Z
[ "python", "error-handling", "pycurl" ]
python runtime error, can dump a file?
550,804
<p>I am using libcurl to DL a webpage, then i am scanning it for data and doing something with one of the links. However, once in a while the page is different then i except thus i extract bad data and pycurl throws an exception. I tried finding the exception name for pycurl but had no luck.</p> <p>Is there a way i ca...
2
2009-02-15T12:29:46Z
551,449
<p>You can use a generic exception handler.</p> <pre><code>logging.basicConfig( file="someFile.log", level=logging.DEBUG ) logger= logging.getLogger( __name__ ) try: curl = pycurl.Curl() curl.setopt(pycurl.URL, url) # etc. curl.perform() curl.close logger.info( "Read %s", url ) except Exception...
3
2009-02-15T19:37:05Z
[ "python", "error-handling", "pycurl" ]
"Private" (implementation) class in Python
551,038
<p>I am coding a small Python module composed of two parts:</p> <ul> <li>some functions defining a public interface,</li> <li>an implementation class used by the above functions, but which is not meaningful outside the module.</li> </ul> <p>At first, I decided to "hide" this implementation class by defining it inside...
50
2009-02-15T15:29:40Z
551,047
<p>The convention is prepend "_" to internal classes, functions, and variables.</p>
6
2009-02-15T15:33:48Z
[ "python", "design", "access-modifiers" ]
"Private" (implementation) class in Python
551,038
<p>I am coding a small Python module composed of two parts:</p> <ul> <li>some functions defining a public interface,</li> <li>an implementation class used by the above functions, but which is not meaningful outside the module.</li> </ul> <p>At first, I decided to "hide" this implementation class by defining it inside...
50
2009-02-15T15:29:40Z
551,048
<p>Use a single underscore prefix:</p> <pre><code>class _Internal: ... </code></pre> <p>This is the official Python convention for 'internal' symbols; "from module import *" does not import underscore-prefixed objects.</p>
89
2009-02-15T15:34:03Z
[ "python", "design", "access-modifiers" ]
"Private" (implementation) class in Python
551,038
<p>I am coding a small Python module composed of two parts:</p> <ul> <li>some functions defining a public interface,</li> <li>an implementation class used by the above functions, but which is not meaningful outside the module.</li> </ul> <p>At first, I decided to "hide" this implementation class by defining it inside...
50
2009-02-15T15:29:40Z
551,054
<p>Define <code>__all__</code>, a list of names that you want to be exported (<a href="http://docs.python.org/tutorial/modules.html#importing-from-a-package">see documentation</a>).</p> <pre><code>__all__ = ['public_class'] # don't add here the 'implementation_class' </code></pre>
22
2009-02-15T15:36:03Z
[ "python", "design", "access-modifiers" ]
"Private" (implementation) class in Python
551,038
<p>I am coding a small Python module composed of two parts:</p> <ul> <li>some functions defining a public interface,</li> <li>an implementation class used by the above functions, but which is not meaningful outside the module.</li> </ul> <p>At first, I decided to "hide" this implementation class by defining it inside...
50
2009-02-15T15:29:40Z
551,097
<p>A pattern that I sometimes use is this:</p> <p>Define a class:</p> <pre><code>class x(object): def doThis(self): ... def doThat(self): ... </code></pre> <p>Create an instance of the class, overwriting the class name:</p> <pre><code>x = x() </code></pre> <p>Define symbols that expose the ...
8
2009-02-15T15:58:49Z
[ "python", "design", "access-modifiers" ]
"Private" (implementation) class in Python
551,038
<p>I am coding a small Python module composed of two parts:</p> <ul> <li>some functions defining a public interface,</li> <li>an implementation class used by the above functions, but which is not meaningful outside the module.</li> </ul> <p>At first, I decided to "hide" this implementation class by defining it inside...
50
2009-02-15T15:29:40Z
551,169
<p>Use two underscores to prefix names of "private" identifiers. For classes in a module, use a single leading underscore and they will not be imported using "from module import *".</p> <pre><code>class _MyInternalClass: def __my_private_method: pass </code></pre> <p>(There is no such thing as true "priva...
2
2009-02-15T16:37:01Z
[ "python", "design", "access-modifiers" ]
"Private" (implementation) class in Python
551,038
<p>I am coding a small Python module composed of two parts:</p> <ul> <li>some functions defining a public interface,</li> <li>an implementation class used by the above functions, but which is not meaningful outside the module.</li> </ul> <p>At first, I decided to "hide" this implementation class by defining it inside...
50
2009-02-15T15:29:40Z
551,311
<p>To address the issue of design conventions, and as Christopher said, there's really no such thing as "private" in Python. This may sound twisted for someone coming from C/C++ background (like me a while back), but eventually, you'll probably realize following conventions is plenty enough. </p> <p>Seeing something h...
3
2009-02-15T18:09:59Z
[ "python", "design", "access-modifiers" ]
"Private" (implementation) class in Python
551,038
<p>I am coding a small Python module composed of two parts:</p> <ul> <li>some functions defining a public interface,</li> <li>an implementation class used by the above functions, but which is not meaningful outside the module.</li> </ul> <p>At first, I decided to "hide" this implementation class by defining it inside...
50
2009-02-15T15:29:40Z
551,361
<p>In short:</p> <ol> <li><p><strong>You cannot enforce privacy</strong>. There are no private classes/methods/functions in Python. At least, not strict privacy as in other languages, such as Java. </p></li> <li><p><strong>You can only indicate/suggest privacy</strong>. This follows a convention. The python convention...
45
2009-02-15T18:31:49Z
[ "python", "design", "access-modifiers" ]
Deploying application with Python or another embedded scripting language
551,227
<p>I'm thinking about using Python as an <strong>embedded scripting language</strong> in a hobby project written in <strong>C++</strong>. I would not like to depend on separately installed Python distribution. Python documentation seems to be quite clear about general usage, but I couldn't find a clear answer to this.<...
14
2009-02-15T17:12:33Z
551,252
<p>The embedding process is fully documented : <a href="http://docs.python.org/extending/embedding.html" rel="nofollow">Embedding Python in Another Application</a>. The documents suggests a few levels at which embedding is done, choose whatever best fits your requirements. </p> <blockquote> <p>A simple demo of embed...
8
2009-02-15T17:28:19Z
[ "c++", "python", "deployment", "scripting-language", "embedded-language" ]
Deploying application with Python or another embedded scripting language
551,227
<p>I'm thinking about using Python as an <strong>embedded scripting language</strong> in a hobby project written in <strong>C++</strong>. I would not like to depend on separately installed Python distribution. Python documentation seems to be quite clear about general usage, but I couldn't find a clear answer to this.<...
14
2009-02-15T17:12:33Z
551,264
<p>If you're looking for a way to simply deploy a Python app, you could use <a href="http://www.py2exe.org/" rel="nofollow">Py2Exe</a>. That would allow your app all the dependencies it needs without worrying what is or isn't already installed on the users machine.</p> <p>[Edit] If Py2Exe is a no-go for embedded appli...
0
2009-02-15T17:42:00Z
[ "c++", "python", "deployment", "scripting-language", "embedded-language" ]
Deploying application with Python or another embedded scripting language
551,227
<p>I'm thinking about using Python as an <strong>embedded scripting language</strong> in a hobby project written in <strong>C++</strong>. I would not like to depend on separately installed Python distribution. Python documentation seems to be quite clear about general usage, but I couldn't find a clear answer to this.<...
14
2009-02-15T17:12:33Z
551,332
<p>To extend the answer by gimel, there is nothing to stop you from shipping python.dll, using it, and setting a correct PYTHONPATH in order to use your own installation of the python standard library. They are just libraries and files, and your install process can just deal with them as such.</p>
5
2009-02-15T18:16:30Z
[ "c++", "python", "deployment", "scripting-language", "embedded-language" ]
Deploying application with Python or another embedded scripting language
551,227
<p>I'm thinking about using Python as an <strong>embedded scripting language</strong> in a hobby project written in <strong>C++</strong>. I would not like to depend on separately installed Python distribution. Python documentation seems to be quite clear about general usage, but I couldn't find a clear answer to this.<...
14
2009-02-15T17:12:33Z
551,533
<p>Link your application to the python library (pythonXX.lib on Windows) and add the following to your main() function.</p> <pre><code>Py_NoSiteFlag = 1; // Disable importing site.py Py_Initialize(); // Create a python interpreter </code></pre> <p>Put the python standard library bits you need into a zip file (cal...
14
2009-02-15T20:28:31Z
[ "c++", "python", "deployment", "scripting-language", "embedded-language" ]
Getting "global name 'foo' is not defined" with Python's timeit
551,797
<p>I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called <a href="http://docs.python.org/library/timeit.html">timeit</a> that purports to do exactly that:</p> <pre><code>import timeit def foo(): # ... contains c...
58
2009-02-15T23:15:40Z
551,804
<p>have you tried making this line:</p> <pre><code>t = timeit.Timer("foo()") </code></pre> <p>This:</p> <pre><code>t = timeit.Timer("foo()", "from __main__ import foo") </code></pre> <p>Check out the link you provided at the very bottom.</p> <blockquote> <p>To give the timeit module access to functions you defin...
67
2009-02-15T23:18:42Z
[ "python", "scope", "timeit" ]
Getting "global name 'foo' is not defined" with Python's timeit
551,797
<p>I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called <a href="http://docs.python.org/library/timeit.html">timeit</a> that purports to do exactly that:</p> <pre><code>import timeit def foo(): # ... contains c...
58
2009-02-15T23:15:40Z
551,809
<pre><code>t = timeit.Timer("foo()", "from __main__ import foo") </code></pre> <p>Since timeit doesn't have your stuff in scope.</p>
6
2009-02-15T23:22:16Z
[ "python", "scope", "timeit" ]
Getting "global name 'foo' is not defined" with Python's timeit
551,797
<p>I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called <a href="http://docs.python.org/library/timeit.html">timeit</a> that purports to do exactly that:</p> <pre><code>import timeit def foo(): # ... contains c...
58
2009-02-15T23:15:40Z
5,390,326
<p>You can try this hack:</p> <pre><code>import timeit def foo(): print 'bar' def dotime(): t = timeit.Timer("foo()") time = t.timeit(1) print "took %fs\n" % (time,) import __builtin__ __builtin__.__dict__.update(locals()) dotime() </code></pre>
13
2011-03-22T11:16:51Z
[ "python", "scope", "timeit" ]
Help me implement Blackjack in Python (updated)
551,840
<p>I am in the process of writing a blackjack code for python, and i was hoping someone would be able to tell me how to make it:</p> <ol> <li>Recognize what someone has typed i.e. "Hit" or "Stand" and react accordingly.</li> <li>Calculate what the player's score is and whether it is an ace and a jack together, and aut...
3
2009-02-15T23:34:26Z
551,852
<p>This can get you started:</p> <p><a href="http://docs.python.org/library/random.html">http://docs.python.org/library/random.html</a></p> <p><a href="http://docs.python.org/library/strings.html">http://docs.python.org/library/strings.html</a></p> <p><a href="http://docs.python.org/library/stdtypes.html">http://doc...
14
2009-02-15T23:42:10Z
[ "python" ]
Help me implement Blackjack in Python (updated)
551,840
<p>I am in the process of writing a blackjack code for python, and i was hoping someone would be able to tell me how to make it:</p> <ol> <li>Recognize what someone has typed i.e. "Hit" or "Stand" and react accordingly.</li> <li>Calculate what the player's score is and whether it is an ace and a jack together, and aut...
3
2009-02-15T23:34:26Z
551,855
<p>I agreed with SquareCog's comment - some additional information about what you've tried and what's not working and what you're confused about would be helpful.</p> <p>Some info that might be helpful, though:</p> <p>Regarding the generation of numbers between 1-10, ace, king, queen, and jack: it might be helpful to...
4
2009-02-15T23:42:41Z
[ "python" ]
Help me implement Blackjack in Python (updated)
551,840
<p>I am in the process of writing a blackjack code for python, and i was hoping someone would be able to tell me how to make it:</p> <ol> <li>Recognize what someone has typed i.e. "Hit" or "Stand" and react accordingly.</li> <li>Calculate what the player's score is and whether it is an ace and a jack together, and aut...
3
2009-02-15T23:34:26Z
552,111
<p>I've covered the entire thing as a (long) series of exercises.</p> <p><a href="http://homepage.mac.com/s_lott/books/oodesign.html">http://homepage.mac.com/s_lott/books/oodesign.html</a></p> <p>Just read and do the exercises in the book. You'll implement blackjack (and Craps and Roulette, too.)</p>
9
2009-02-16T02:57:38Z
[ "python" ]
Help me implement Blackjack in Python (updated)
551,840
<p>I am in the process of writing a blackjack code for python, and i was hoping someone would be able to tell me how to make it:</p> <ol> <li>Recognize what someone has typed i.e. "Hit" or "Stand" and react accordingly.</li> <li>Calculate what the player's score is and whether it is an ace and a jack together, and aut...
3
2009-02-15T23:34:26Z
552,277
<p>In your code, you wrote:</p> <pre><code>Ace = 1 or 11 </code></pre> <p>I'm afraid this doesn't do what you think. Start the python interpreter, and then type <strong><code>1 or 11</code></strong> into it. Here's what I get:</p> <pre><code>&gt;&gt;&gt; 1 or 11 1 </code></pre> <p>What this means is that when you...
3
2009-02-16T04:36:34Z
[ "python" ]
Help me implement Blackjack in Python (updated)
551,840
<p>I am in the process of writing a blackjack code for python, and i was hoping someone would be able to tell me how to make it:</p> <ol> <li>Recognize what someone has typed i.e. "Hit" or "Stand" and react accordingly.</li> <li>Calculate what the player's score is and whether it is an ace and a jack together, and aut...
3
2009-02-15T23:34:26Z
552,520
<p>Here's a tip to get started: Instead of drawing a number between 1 and 10 and doing something different for face cards, draw a number between 1 and 13, and define a function (or even a class) that interprets these numbers as cards. For example, 1 would map to Ace, 11 to Jack, 2 to a deuce, etc.</p>
0
2009-02-16T07:44:17Z
[ "python" ]
Help me implement Blackjack in Python (updated)
551,840
<p>I am in the process of writing a blackjack code for python, and i was hoping someone would be able to tell me how to make it:</p> <ol> <li>Recognize what someone has typed i.e. "Hit" or "Stand" and react accordingly.</li> <li>Calculate what the player's score is and whether it is an ace and a jack together, and aut...
3
2009-02-15T23:34:26Z
822,440
<p>Check <strong>all</strong> of your inputs. As a young lad, I wrote my blackjack program in Fortran. One of my users pointed out that he could:</p> <ol> <li>Enter a negative number as a bet.</li> <li>Deliberately lose.</li> </ol> <p>The program would then do exactly what it was programmed to do:</p> <ol> <li>Sub...
0
2009-05-04T22:36:59Z
[ "python" ]
"Interfaces" in Python: Yea or Nay?
552,058
<p>So I'm starting a project using Python after spending a significant amount of time in static land. I've seen some projects that make "interfaces" which are really just classes without any implementations. Before, I'd scoff at the idea and ignore that section of those projects. But now, I'm beginning to warm up to...
16
2009-02-16T02:15:17Z
552,066
<p>My personal opinion is positive regarding these <em>interfaces</em> in Python, though I haven't actually use Python interfaces.</p> <p>I have used them quite a bit in PHP with a project because the interfaces really did make that completeness testing you mentioned quite simple.</p>
1
2009-02-16T02:22:55Z
[ "python", "documentation", "interface", "coding-style" ]
"Interfaces" in Python: Yea or Nay?
552,058
<p>So I'm starting a project using Python after spending a significant amount of time in static land. I've seen some projects that make "interfaces" which are really just classes without any implementations. Before, I'd scoff at the idea and ignore that section of those projects. But now, I'm beginning to warm up to...
16
2009-02-16T02:15:17Z
552,068
<p>Seems kind of unnecessary to me - when I'm writing classes like that I usually just make the base class (your <code>ISomething</code>) with no methods, and mention in the actual documentation which methods subclasses are supposed to override.</p>
5
2009-02-16T02:24:33Z
[ "python", "documentation", "interface", "coding-style" ]
"Interfaces" in Python: Yea or Nay?
552,058
<p>So I'm starting a project using Python after spending a significant amount of time in static land. I've seen some projects that make "interfaces" which are really just classes without any implementations. Before, I'd scoff at the idea and ignore that section of those projects. But now, I'm beginning to warm up to...
16
2009-02-16T02:15:17Z
552,076
<p>I'm about to do something similar with my Python project, the only things I would add are:</p> <ul> <li>Extra long, in-depth doc strings for each interface and all the abstract methods.</li> <li>I would add in all the required arguments so there's a definitive list.</li> <li>Raise an exception instead of 'pass'.</l...
3
2009-02-16T02:33:21Z
[ "python", "documentation", "interface", "coding-style" ]