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
listlengths
1
5
If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?
717,506
<p>In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary concerns. </p> <p>However, I rarely hear the same arguments made in the context of Python although it is also permitted in the Python langua...
12
2009-04-04T17:54:50Z
717,563
<p>It's a technique less practised in Python, in part because "core" classes in Python (those implemented in C) are not really modifiable. In Ruby, on the other hand, because of the way it's implemented internally (not better, just different) just about anything can be modified dynamically.</p> <p>Philosophically, it'...
20
2009-04-04T18:22:54Z
[ "python", "ruby", "language-features", "monkeypatching" ]
If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?
717,506
<p>In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary concerns. </p> <p>However, I rarely hear the same arguments made in the context of Python although it is also permitted in the Python langua...
12
2009-04-04T17:54:50Z
717,947
<p>Actually in Python it's a bit harder to modify basic types. </p> <p>For example imagine, that you redefine integer.</p> <p>Ruby:</p> <pre><code>class Fixnum def *(n) 5 end end </code></pre> <p>Now 2*2 yields 5.</p> <p>Python:</p> <pre><code>&gt;&gt;&gt; class int(int): def __mul__(self, x): ...
2
2009-04-04T22:27:42Z
[ "python", "ruby", "language-features", "monkeypatching" ]
If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?
717,506
<p>In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary concerns. </p> <p>However, I rarely hear the same arguments made in the context of Python although it is also permitted in the Python langua...
12
2009-04-04T17:54:50Z
718,062
<p>As a Python programmer who has had a taste of Ruby (and likes it), I think there is somewhat of an ironic parallel to when Python was beginning to become popular.</p> <p>C and Java programmers would ‘bash’ Python, stating that it wasn't a real language, and that the dynamic nature of its types would be dangerou...
16
2009-04-04T23:48:08Z
[ "python", "ruby", "language-features", "monkeypatching" ]
If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?
717,506
<p>In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary concerns. </p> <p>However, I rarely hear the same arguments made in the context of Python although it is also permitted in the Python langua...
12
2009-04-04T17:54:50Z
718,259
<p>In Python, any literal (<code>""</code>, <code>{}</code>, <code>1.0</code>, etc) creates an instance of the standard class, even if you tried to monkeypatch it and redefined the corresponding class in your namespace.</p> <p>It just won't work how you intended:</p> <pre class="lang-python prettyprint-override"><cod...
3
2009-04-05T02:57:28Z
[ "python", "ruby", "language-features", "monkeypatching" ]
If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?
717,506
<p>In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary concerns. </p> <p>However, I rarely hear the same arguments made in the context of Python although it is also permitted in the Python langua...
12
2009-04-04T17:54:50Z
721,025
<p>I think that monkey patching should only be used as the last solution.</p> <p>Normally Python programmers know how a class or a method behave. They know that class xxx is doing things in a certain way.</p> <p>When you monkey patch a class or a method, you are changing it's behavior. Other Python programmers using ...
2
2009-04-06T11:18:34Z
[ "python", "ruby", "language-features", "monkeypatching" ]
If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?
717,506
<p>In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary concerns. </p> <p>However, I rarely hear the same arguments made in the context of Python although it is also permitted in the Python langua...
12
2009-04-04T17:54:50Z
726,497
<p>If you want to do some monkey patching in Python, it is relatively easy, as long as you are not modifying a built-in type (int, float, str).</p> <pre><code>class SomeClass: def foo(self): print "foo" def tempfunc(self): print "bar" SomeClass.bar = tempfunc del tempfunc </code></pre> <p>This will a...
1
2009-04-07T16:08:35Z
[ "python", "ruby", "language-features", "monkeypatching" ]
Python and sockets +upnp
717,687
<p>I have a question about python and sockets. As I understand, if you have router you must open a port before you can use it in your program. But if user can't do that... I heard something about UPnP. I don't know will it help with my problem, so I've asked you. Best regards.</p>
0
2009-04-04T19:46:03Z
718,777
<p>For UPnP support you can use <a href="http://miniupnp.free.fr" rel="nofollow">MiniUPnP</a> library, it has python support.</p>
2
2009-04-05T11:18:12Z
[ "python" ]
Python and sockets +upnp
717,687
<p>I have a question about python and sockets. As I understand, if you have router you must open a port before you can use it in your program. But if user can't do that... I heard something about UPnP. I don't know will it help with my problem, so I've asked you. Best regards.</p>
0
2009-04-04T19:46:03Z
27,227,364
<p>In usual setups, if you have a home router your machine doesn't have a public IP address, only the router has and does NAT for other machines to access the Internet.</p> <p>In order to open a "listening" socket so internet machines can reach your private machine, you have to redirect a public port to your local mac...
0
2014-12-01T11:24:28Z
[ "python" ]
Does the Python library httplib2 cache URIs with GET strings?
717,700
<p>In the following example what is cached correctly? Is there a Vary-Header I have to set server-side for the GET string?</p> <pre><code>import httplib2 h = httplib2.Http(".cache") resp, content = h.request("http://test.com/list/") resp, content = h.request("http://test.com/list?limit=10") resp, content = h.request("...
1
2009-04-04T19:50:11Z
717,829
<p>httplib2 uses the full URI for the cache key, so in this case each of the URLs you have in your example will be cached separately by the client.</p> <p>For the chapter and verse from the <code>__init__.py</code> file for httplib2, if you would like proof, have a look at call to the cache on around line 1000:</p> <...
4
2009-04-04T21:14:14Z
[ "python", "caching", "httplib2" ]
can pylons + authkit ignore particular responses with 401 status?
717,776
<p>i am writing a pylons app, and I am using authkit for authentication/authorization. if a user is not logged in and hits a page that requires authorization, authkit swallows the 401 (not authenticated) response and redirects to a login page. this is great for the web interface, but not great for our web services. whe...
2
2009-04-04T20:38:46Z
719,344
<p>It looks like the <a href="http://wiki.pylonshq.com/display/authkitcookbook/Configuration" rel="nofollow">authkit.setup.intercept</a> option is designed to do precisely this.</p>
1
2009-04-05T17:34:31Z
[ "python", "http", "pylons", "authkit" ]
Self-repairing Python threads
717,831
<p>I've created a web spider that accesses both a US and EU server. The US and EU servers are the same data structure, but have different data inside them, and I want to collate it all. In order to be nice to the server, there's a wait time between each request. As the program is exactly the same, in order to speed up ...
4
2009-04-04T21:14:32Z
717,845
<p>Can you have e.g. the main thread function as a monitoring thread? E.g. require that the worker thread regularly update some thread-specific timestamp value, and if a thread hasn't updated it's timestamp within a suitable time, have the monitoring thread kill it and restart?</p> <p>Or, see <a href="http://stackover...
3
2009-04-04T21:25:17Z
[ "python", "multithreading" ]
Self-repairing Python threads
717,831
<p>I've created a web spider that accesses both a US and EU server. The US and EU servers are the same data structure, but have different data inside them, and I want to collate it all. In order to be nice to the server, there's a wait time between each request. As the program is exactly the same, in order to speed up ...
4
2009-04-04T21:14:32Z
717,847
<p>Just use a <code>try: ... except: ...</code> block in the <code>run</code> method. If something weird happens that causes the thread to fail, it's highly likely that an error will be thrown somewhere in your code (as opposed to in the threading subsystem itself); this way you can catch it, log it, and restart the th...
8
2009-04-04T21:25:52Z
[ "python", "multithreading" ]
Paypal NVP API with Django
717,911
<p>I am looking into using the paypal NVP API to allow users to pay on my website for a recurring subscription. </p> <p>I have a few questions about the requirements. Will my site have to meet the "PCI Compliance" stuff. I guess I will have to get an SSL certificate and is there anything else that is required or tha...
3
2009-04-04T22:06:58Z
717,974
<p>There is nothing forcing you to meet PCI Compliance and use SSL, but you should anyway to limit your liability and inspire a little customer trust. </p> <p>I thought I read something on the <a href="http://satchmoproject.com" rel="nofollow">Satchmo</a> Developer's Google group about a person implementing PayPal NVP...
0
2009-04-04T22:44:59Z
[ "python", "django", "paypal" ]
Paypal NVP API with Django
717,911
<p>I am looking into using the paypal NVP API to allow users to pay on my website for a recurring subscription. </p> <p>I have a few questions about the requirements. Will my site have to meet the "PCI Compliance" stuff. I guess I will have to get an SSL certificate and is there anything else that is required or tha...
3
2009-04-04T22:06:58Z
1,290,156
<p>I know this question is a bit out of date, but I wanted to add a note that I've recently released an <a href="http://www.chickenwingsw.com/scratches/python/paypal-on-python" rel="nofollow">open source Python API to the PayPal NVP interface</a>.</p>
0
2009-08-17T20:12:32Z
[ "python", "django", "paypal" ]
How to install SimpleJson Package for Python
718,040
<p><a href="http://pypi.python.org/pypi/simplejson">http://pypi.python.org/pypi/simplejson</a></p> <p>I am just diving into the Python world and want to make a simple twitter application which requires the installation of simplejson but not sure how I can set it up and get it working..</p> <p>I am on a Windows System...
32
2009-04-04T23:31:10Z
718,073
<p>I would recommend <a href="http://pypi.python.org/pypi/setuptools#windows">EasyInstall</a>, a package management application for Python.</p> <p>Once you've installed EasyInstall, you should be able to go to a command window and type:</p> <pre><code>easy_install simplejson </code></pre> <p>This may require putting...
43
2009-04-04T23:54:42Z
[ "python", "simplejson" ]
How to install SimpleJson Package for Python
718,040
<p><a href="http://pypi.python.org/pypi/simplejson">http://pypi.python.org/pypi/simplejson</a></p> <p>I am just diving into the Python world and want to make a simple twitter application which requires the installation of simplejson but not sure how I can set it up and get it working..</p> <p>I am on a Windows System...
32
2009-04-04T23:31:10Z
718,097
<p>If you have Python 2.6 installed then you already have simplejson - just import <code>json</code>; it's the same thing.</p>
15
2009-04-05T00:12:08Z
[ "python", "simplejson" ]
How to install SimpleJson Package for Python
718,040
<p><a href="http://pypi.python.org/pypi/simplejson">http://pypi.python.org/pypi/simplejson</a></p> <p>I am just diving into the Python world and want to make a simple twitter application which requires the installation of simplejson but not sure how I can set it up and get it working..</p> <p>I am on a Windows System...
32
2009-04-04T23:31:10Z
3,674,861
<p>Download the source code, unzip it to and directory, and execute python setup.py install. </p>
3
2010-09-09T08:16:35Z
[ "python", "simplejson" ]
How to install SimpleJson Package for Python
718,040
<p><a href="http://pypi.python.org/pypi/simplejson">http://pypi.python.org/pypi/simplejson</a></p> <p>I am just diving into the Python world and want to make a simple twitter application which requires the installation of simplejson but not sure how I can set it up and get it working..</p> <p>I am on a Windows System...
32
2009-04-04T23:31:10Z
12,865,599
<p>You can import json as simplejson like this:</p> <pre><code>import json as simplejson </code></pre> <p>and keep backward compatibility.</p>
4
2012-10-12T19:12:25Z
[ "python", "simplejson" ]
How to install SimpleJson Package for Python
718,040
<p><a href="http://pypi.python.org/pypi/simplejson">http://pypi.python.org/pypi/simplejson</a></p> <p>I am just diving into the Python world and want to make a simple twitter application which requires the installation of simplejson but not sure how I can set it up and get it working..</p> <p>I am on a Windows System...
32
2009-04-04T23:31:10Z
16,538,587
<p>Really simple way is:</p> <pre><code>pip install simplejson </code></pre>
17
2013-05-14T08:39:32Z
[ "python", "simplejson" ]
Unable to put Python code to Joomla
718,498
<p>I have a Python code from Google app engine. I need to implement it to Joomla.</p> <p><strong>How can you implement Python code to Joomla?</strong></p> <p><strong>[edit after the 1st answer]</strong></p> <p>It is enough for me that I can put the code to a module position.</p>
0
2009-04-05T07:08:27Z
718,511
<p>Joomla is PHP based whereas Google App Engine is Python based (and tends to use Django). Your best bet is to either find an alternative to the python code, find someone to translate it, or learn python and manually translate it. </p> <p>There's no straight python to php conversion though.</p> <p>EDIT: but if you...
2
2009-04-05T07:17:23Z
[ "python", "joomla" ]
Changing the title of a Tab in wx.Notebook
718,546
<p>I'm experimenting with wxPython,</p> <p>I have a tabbed interface (notebook) and each tab is basically a file list view (yes, I'm trying to make a file manager)</p> <p>The file list inherits from wx.ListCtrl, and the tabbed interface inherits from wx.Notebook</p> <p>I'm just starting .. and I had it so double cli...
3
2009-04-05T07:49:10Z
718,566
<p>As .GetPage returns a wx.Window, I think <code>tab.Label = title</code> should work.</p>
-1
2009-04-05T08:08:05Z
[ "python", "wxpython", "wxwidgets", "tabbed-interface" ]
Changing the title of a Tab in wx.Notebook
718,546
<p>I'm experimenting with wxPython,</p> <p>I have a tabbed interface (notebook) and each tab is basically a file list view (yes, I'm trying to make a file manager)</p> <p>The file list inherits from wx.ListCtrl, and the tabbed interface inherits from wx.Notebook</p> <p>I'm just starting .. and I had it so double cli...
3
2009-04-05T07:49:10Z
718,597
<p>I think doing something like this helps :</p> <pre><code> notebook.get_tab_label(notebook.get_nth_page(your_page_number)).set_text("Your text") </code></pre> <p>If you want to have a reference to the current tab always, you must connect the "switch-page" signal, and save the page in a variable.</p>
0
2009-04-05T08:48:31Z
[ "python", "wxpython", "wxwidgets", "tabbed-interface" ]
Changing the title of a Tab in wx.Notebook
718,546
<p>I'm experimenting with wxPython,</p> <p>I have a tabbed interface (notebook) and each tab is basically a file list view (yes, I'm trying to make a file manager)</p> <p>The file list inherits from wx.ListCtrl, and the tabbed interface inherits from wx.Notebook</p> <p>I'm just starting .. and I had it so double cli...
3
2009-04-05T07:49:10Z
718,621
<p>I don't know wxPython, but I assume it wraps all the methods of the C++ classes.</p> <p>There is <em>wxNotebook::GetSelection()</em> which returns <em>wxNOT&#95;FOUND</em> or the index of the selected page, which can then be used to call <em>wxNotebook::SetPageText()</em>.</p> <p>Or use <em>wxNotebook::GetPage()</...
2
2009-04-05T09:12:20Z
[ "python", "wxpython", "wxwidgets", "tabbed-interface" ]
App Engine - problem trying to set a Model property value
718,553
<p>I'm pretty new to app engine, and I'm trying to set a bit of text into the app engine database for the first time.</p> <p>Here's my code:</p> <pre><code>def setVenueIntroText(text): venue_obj = db.GqlQuery("SELECT * FROM Venue").get() venue_obj.intro_text = text # Works if I comment out db.put(venue_obj)...
1
2009-04-05T07:59:07Z
718,584
<p>I think this should work:</p> <pre><code>def setVenueIntroText(text): query = db.GqlQuery("SELECT * FROM Venue") for result in query: result.intro_text = text db.put(result) </code></pre>
1
2009-04-05T08:30:22Z
[ "python", "google-app-engine", "bigtable" ]
App Engine - problem trying to set a Model property value
718,553
<p>I'm pretty new to app engine, and I'm trying to set a bit of text into the app engine database for the first time.</p> <p>Here's my code:</p> <pre><code>def setVenueIntroText(text): venue_obj = db.GqlQuery("SELECT * FROM Venue").get() venue_obj.intro_text = text # Works if I comment out db.put(venue_obj)...
1
2009-04-05T07:59:07Z
719,551
<p>I think the main problem was that I couldn't see the error messages - really stupid of me, I forgot to put DEBUG = True in my settings.py</p> <p>It turns out I needed a multiline=True in my StringProperty</p> <p>Django is catching my exceptions for me.</p>
1
2009-04-05T19:42:56Z
[ "python", "google-app-engine", "bigtable" ]
Reversible version of compile() in Python
718,769
<p>I'm trying to make a function in Python that does the equivalent of compile(), but also lets me get the original string back. Let's call those two functions comp() and decomp(), for disambiguation purposes. That is,</p> <pre><code>a = comp("2 * (3 + x)", "", "eval") eval(a, dict(x=3)) # =&gt; 12 decomp(a) # =&gt; "...
3
2009-04-05T11:12:02Z
718,878
<p>My approach would be to wrap the code object in another object. Something like this:</p> <pre><code>class CodeObjectEnhanced(object): def __init__(self, *args): self.compiled = compile(*args) self.original = args[0] def comp(*args): return CodeObjectEnhanced(*args) </code></pre> <p>Then whe...
4
2009-04-05T12:39:41Z
[ "python", "metaprogramming" ]
Reversible version of compile() in Python
718,769
<p>I'm trying to make a function in Python that does the equivalent of compile(), but also lets me get the original string back. Let's call those two functions comp() and decomp(), for disambiguation purposes. That is,</p> <pre><code>a = comp("2 * (3 + x)", "", "eval") eval(a, dict(x=3)) # =&gt; 12 decomp(a) # =&gt; "...
3
2009-04-05T11:12:02Z
719,902
<p>This is kind of a weird problem, and my initial reaction is that you might be better off doing something else entirely to accomplish whatever it is you're trying to do. But it's still an interesting question, so here's my crack at it: I make the original code source an unused constant of the code object.</p> <pre>...
6
2009-04-05T23:51:08Z
[ "python", "metaprogramming" ]
Django Template - New Variable Declaration
719,127
<p>Let me preface by I am just starting Python so if this is really a simple question ^_^</p> <p>I have a html file with the following content:</p> <pre><code> {%for d in results%} &lt;div class="twt"&gt; &lt;img src="{{ d.profile_image_url }}" width="48px" height="48px" /&gt; &lt;span&gt; {{ d.text }} &lt;/...
13
2009-04-05T15:37:15Z
719,132
<p>Check out the <a href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#for">documentation</a> on the <code>for</code> loop.</p> <p>It automatically creates a variable called <code>forloop.counter</code> that holds the current iteration index.</p> <p>As far as the greater question on how to declare var...
20
2009-04-05T15:38:44Z
[ "python", "django", "django-templates" ]
Django Template - New Variable Declaration
719,127
<p>Let me preface by I am just starting Python so if this is really a simple question ^_^</p> <p>I have a html file with the following content:</p> <pre><code> {%for d in results%} &lt;div class="twt"&gt; &lt;img src="{{ d.profile_image_url }}" width="48px" height="48px" /&gt; &lt;span&gt; {{ d.text }} &lt;/...
13
2009-04-05T15:37:15Z
5,040,262
<p>If you want to set any variable inside a Django template, you can use <a href="http://www.soyoucode.com/2011/set-variable-django-template" rel="nofollow">this small template tag I've written</a>.</p>
7
2011-02-18T10:38:13Z
[ "python", "django", "django-templates" ]
Django Template - New Variable Declaration
719,127
<p>Let me preface by I am just starting Python so if this is really a simple question ^_^</p> <p>I have a html file with the following content:</p> <pre><code> {%for d in results%} &lt;div class="twt"&gt; &lt;img src="{{ d.profile_image_url }}" width="48px" height="48px" /&gt; &lt;span&gt; {{ d.text }} &lt;/...
13
2009-04-05T15:37:15Z
8,127,934
<p>In template:</p> <pre><code>{% for outer_obj in outer_list %} {% for inner_obj in inner_list %} {% increment_counter forloop.counter0 forloop.parentloop.counter0 outer_list.count %} {% endfor %} {% endfor %} </code></pre> <p>Templatetag:</p> <pre><code>@register.simple_tag def increment_counter(...
2
2011-11-14T20:51:48Z
[ "python", "django", "django-templates" ]
Django Template - New Variable Declaration
719,127
<p>Let me preface by I am just starting Python so if this is really a simple question ^_^</p> <p>I have a html file with the following content:</p> <pre><code> {%for d in results%} &lt;div class="twt"&gt; &lt;img src="{{ d.profile_image_url }}" width="48px" height="48px" /&gt; &lt;span&gt; {{ d.text }} &lt;/...
13
2009-04-05T15:37:15Z
19,348,859
<p>this is how to do it:</p> <pre><code>{% with name="World" greeting="Hello" %} &lt;html&gt; &lt;div&gt;{{ greeting }} {{name}}!&lt;/div&gt; &lt;/html&gt; {% endwith %} </code></pre> <p>see also: <a href="https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#with" rel="nofollow">with tag</a></p> <p>by ...
3
2013-10-13T18:43:18Z
[ "python", "django", "django-templates" ]
How can you make a vote-up-down button like in Stackoverflow?
719,194
<p><strong>Problems</strong></p> <ol> <li>how to make an Ajax buttons (upward and downward arrows) such that the number can increase or decrease</li> <li>how to save the action af an user to an variable NumberOfVotesOfQuestionID</li> </ol> <p>I am not sure whether I should use database or not for the variable. Howeve...
30
2009-04-05T16:07:30Z
719,249
<p>I thnk the answers for these questions are to long for stackoverflow.</p> <p>I'd recommend storing the votes in a Database.</p> <p>You don't mention a server-side programming language.</p> <p>please give us some more information</p> <p><a href="http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=109" rel...
0
2009-04-05T16:28:28Z
[ "javascript", "python", "html", "ajax" ]
How can you make a vote-up-down button like in Stackoverflow?
719,194
<p><strong>Problems</strong></p> <ol> <li>how to make an Ajax buttons (upward and downward arrows) such that the number can increase or decrease</li> <li>how to save the action af an user to an variable NumberOfVotesOfQuestionID</li> </ol> <p>I am not sure whether I should use database or not for the variable. Howeve...
30
2009-04-05T16:07:30Z
719,293
<p>You create the buttons, which can be links or images or whatever. Now hook a JavaScript function up to each button's click event. On clicking, the function fires and</p> <ul> <li>Sends a request to the server code that says, more or less, +1 or -1.</li> <li>Server code takes over. This will vary wildly depending on...
3
2009-04-05T17:02:04Z
[ "javascript", "python", "html", "ajax" ]
How can you make a vote-up-down button like in Stackoverflow?
719,194
<p><strong>Problems</strong></p> <ol> <li>how to make an Ajax buttons (upward and downward arrows) such that the number can increase or decrease</li> <li>how to save the action af an user to an variable NumberOfVotesOfQuestionID</li> </ol> <p>I am not sure whether I should use database or not for the variable. Howeve...
30
2009-04-05T16:07:30Z
719,475
<p>This is a dirty/untested theoretical implementation using jQuery/Django.</p> <p>We're going to assume the voting up and down is for questions/answers like on this site, but that can obviously be adjusted to your real life use case.</p> <h3>The template</h3> <pre><code>&lt;div id="answer_595" class="answer"&gt; ...
57
2009-04-05T18:55:08Z
[ "javascript", "python", "html", "ajax" ]
How can you make a vote-up-down button like in Stackoverflow?
719,194
<p><strong>Problems</strong></p> <ol> <li>how to make an Ajax buttons (upward and downward arrows) such that the number can increase or decrease</li> <li>how to save the action af an user to an variable NumberOfVotesOfQuestionID</li> </ol> <p>I am not sure whether I should use database or not for the variable. Howeve...
30
2009-04-05T16:07:30Z
719,606
<p>A couple of points no one has mentioned:</p> <ul> <li>You don't want to use GET when changing the state of your database. Otherwise I could put an image on my site with <code>src="http://stackoverflow.com/question_555/vote/up/answer_3/"</code>.</li> <li>You also need <a href="http://docs.djangoproject.com/en/dev/re...
7
2009-04-05T20:16:48Z
[ "javascript", "python", "html", "ajax" ]
Python 3: formatting zip module arguments correctly (newb)
719,503
<p>Please tell me why this code fails. I am new and I don't understand why my formatting of my zip arguments is incorrect. Since I am unsure how to communicate best so I will show the code, the error message, and what I believe is happening.</p> <pre><code>#!c:\python30 # Filename: backup_ver5.py import os import t...
1
2009-04-05T19:16:12Z
719,516
<p>Looks like it's because you have an extra pair of double quotes around your pathname. Remove the double quotes, and see if it works.</p>
3
2009-04-05T19:21:44Z
[ "python", "zip" ]
Python 3: formatting zip module arguments correctly (newb)
719,503
<p>Please tell me why this code fails. I am new and I don't understand why my formatting of my zip arguments is incorrect. Since I am unsure how to communicate best so I will show the code, the error message, and what I believe is happening.</p> <pre><code>#!c:\python30 # Filename: backup_ver5.py import os import t...
1
2009-04-05T19:16:12Z
719,521
<p>To answer your other question: the double backslashes are there because they are <a href="http://www.codecodex.com/wiki/index.php?title=Escape%5Fsequences#Python" rel="nofollow">escaped</a>.</p>
1
2009-04-05T19:24:31Z
[ "python", "zip" ]
What is the purpose of python's inner classes?
719,705
<p>Python's inner/nested classes confuse me. Is there something that can't be accomplished without them? If so, what is that thing?</p>
58
2009-04-05T21:13:28Z
719,718
<p>Quoted from <a href="http://www.geekinterview.com/question_details/64739">http://www.geekinterview.com/question_details/64739</a>:</p> <blockquote> <h3>Advantages of inner class:</h3> <ul> <li><strong>Logical grouping of classes</strong>: If a class is useful to only one other class then it is logical to e...
56
2009-04-05T21:24:34Z
[ "python", "class", "oop", "language-features" ]
What is the purpose of python's inner classes?
719,705
<p>Python's inner/nested classes confuse me. Is there something that can't be accomplished without them? If so, what is that thing?</p>
58
2009-04-05T21:13:28Z
722,036
<p>Nesting classes within classes:</p> <ul> <li><p>Nested classes bloat the class definition making it harder to see whats going on.</p></li> <li><p>Nested classes can create coupling that would make testing more difficult.</p></li> <li><p>In Python you can put more than one class in a file/module, unlike Java, so the...
9
2009-04-06T15:43:54Z
[ "python", "class", "oop", "language-features" ]
What is the purpose of python's inner classes?
719,705
<p>Python's inner/nested classes confuse me. Is there something that can't be accomplished without them? If so, what is that thing?</p>
58
2009-04-05T21:13:28Z
722,175
<blockquote> <p>Is there something that can't be accomplished without them?</p> </blockquote> <p>No. They are absolutely equivalent to defining the class normally at top level, and then copying a reference to it into the outer class.</p> <p>I don't think there's any special reason nested classes are ‘allowed’, ...
40
2009-04-06T16:23:21Z
[ "python", "class", "oop", "language-features" ]
What is the purpose of python's inner classes?
719,705
<p>Python's inner/nested classes confuse me. Is there something that can't be accomplished without them? If so, what is that thing?</p>
58
2009-04-05T21:13:28Z
722,237
<p>There's something you need to wrap your head around to be able to understand this. In most languages, class definitions are directives to the compiler. That is, the class is created before the program is ever run. In python, all statements are executable. That means that this statement:</p> <pre><code>class foo...
18
2009-04-06T16:38:01Z
[ "python", "class", "oop", "language-features" ]
What is the purpose of python's inner classes?
719,705
<p>Python's inner/nested classes confuse me. Is there something that can't be accomplished without them? If so, what is that thing?</p>
58
2009-04-05T21:13:28Z
10,376,716
<p>I understand the arguments against nested classes, but there is a case for using them in some occasions. Imagine I'm creating a doubly-linked list class, and I need to create a node class for maintaing the nodes. I have two choices, create Node class inside the DoublyLinkedList class, or create the Node class outsid...
4
2012-04-29T22:52:04Z
[ "python", "class", "oop", "language-features" ]
What is the purpose of python's inner classes?
719,705
<p>Python's inner/nested classes confuse me. Is there something that can't be accomplished without them? If so, what is that thing?</p>
58
2009-04-05T21:13:28Z
28,581,492
<p>I have used Python's inner classes to create deliberately buggy subclasses within unittest functions (i.e. inside <code>def test_something():</code>) in order to get closer to 100% test coverage (e.g. testing very rarely triggered logging statements by overriding some methods).</p> <p>In retrospect it's similar to ...
0
2015-02-18T10:37:54Z
[ "python", "class", "oop", "language-features" ]
How do I set Session name with Cherrypy?
719,710
<p>In PHP I would do it like this:</p> <pre><code>session_name("special_session_name"); </code></pre> <p>So how do I do it with Cherrypy? Just need to find exact equivalent for it. PHP manual page: <a href="http://fi2.php.net/session_name" rel="nofollow">http://fi2.php.net/session_name</a></p>
0
2009-04-05T21:15:20Z
719,780
<p>Reading the docs and the source most probably you have to set "tools.sessions.name" in your config file:</p> <pre><code>cherrypy.config.update({'tools.sessions.name': "special_session_name"}) </code></pre>
3
2009-04-05T22:00:49Z
[ "php", "python", "session", "cherrypy" ]
What is the best ordered dict implementation in python?
719,744
<p>I've seen (and written) a number of implementations of this. Is there one that is considered the best or is emerging as a standard?</p> <p>What I mean by ordered dict is that the object has some concept of the order of the keys in it, similar to an array in PHP.</p> <p>odict from <a href="http://www.python.org/de...
6
2009-04-05T21:40:44Z
719,772
<p>I haven't seen a standard; everyone seems to roll their own (see answers to <a href="http://stackoverflow.com/questions/60848/how-do-you-retrieve-items-from-a-dictionary-in-the-order-that-theyre-inserted">this question</a>). If you can use the <code>OrderedDict</code> <a href="http://bugs.python.org/issue5397">patc...
8
2009-04-05T21:56:17Z
[ "python", "dictionary", "ordereddictionary" ]
What is the best ordered dict implementation in python?
719,744
<p>I've seen (and written) a number of implementations of this. Is there one that is considered the best or is emerging as a standard?</p> <p>What I mean by ordered dict is that the object has some concept of the order of the keys in it, similar to an array in PHP.</p> <p>odict from <a href="http://www.python.org/de...
6
2009-04-05T21:40:44Z
2,030,935
<p>This one by Raymond Hettinger is a drop-in substitute for the collections.OrderedDict that will appear in Python 2.7: <a href="http://pypi.python.org/pypi/ordereddict">http://pypi.python.org/pypi/ordereddict</a></p> <p>The dev version of the collections docs say it's equivalent to what will be in Python 2.7, so it'...
12
2010-01-08T21:37:05Z
[ "python", "dictionary", "ordereddictionary" ]
What is the best ordered dict implementation in python?
719,744
<p>I've seen (and written) a number of implementations of this. Is there one that is considered the best or is emerging as a standard?</p> <p>What I mean by ordered dict is that the object has some concept of the order of the keys in it, similar to an array in PHP.</p> <p>odict from <a href="http://www.python.org/de...
6
2009-04-05T21:40:44Z
15,743,916
<p>Python 2.7 and later have OrderedDict in the <code>collections</code> module, so you should consider that as 'standard'. If its functionality is enough you should probably be using that.</p> <p>However its implementation approach is minimalistic and if that is not enough you should look at <a href="http://www.voids...
1
2013-04-01T12:56:31Z
[ "python", "dictionary", "ordereddictionary" ]
What is the best ordered dict implementation in python?
719,744
<p>I've seen (and written) a number of implementations of this. Is there one that is considered the best or is emerging as a standard?</p> <p>What I mean by ordered dict is that the object has some concept of the order of the keys in it, similar to an array in PHP.</p> <p>odict from <a href="http://www.python.org/de...
6
2009-04-05T21:40:44Z
27,007,361
<p><code>collections.OrderedDict</code> should now be widely available, but if performance is concern, you might consider using my package <a href="https://github.com/shoyer/cyordereddict" rel="nofollow">cyordereddict</a> as an alternative. It's a direct port of standard library's OrderedDict to Cython that is 2-6x fas...
2
2014-11-19T01:26:23Z
[ "python", "dictionary", "ordereddictionary" ]
How can domain aliases be set up using Django?
719,771
<p>I am working on creating a website in Django which consists of two parts: the website itself, and the forum. They will both be on separate domains, i.e. example.com and exampleforum.com. How can this be done in Django, when the forum and main site are part of the same instance?</p>
0
2009-04-05T21:55:34Z
719,821
<p>This is done at the web server level. Django doesn't care about the domain on the incoming request.</p> <p>If you are using Apache just put multiple ServerAlias directives inside your virtual host like this:</p> <pre><code>&lt;VirtualHost *:80&gt; ServerName www.mydomain.com ServerAlias mydomain.com S...
4
2009-04-05T22:35:38Z
[ "python", "django", "dns", "cross-domain" ]
Help Me Figure Out A Random Scheduling Algorithm using Python and PostgreSQL
719,886
<p>I am trying to do the schedule for the upcoming season for my simulation baseball team. I have an existing Postgresql database that contains the old schedule.</p> <p>There are 648 rows in the database: 27 weeks of series for 24 teams. The problem is that the schedule has gotten predictable and allows teams to kno...
0
2009-04-05T23:34:32Z
719,909
<p>I'm not sure I fully understand the problem, but here is how I would do it: 1. create a complete list of matches that need to happen 2. iterate over the weeks, selecting which match needs to happen in this week.</p> <p>You can use Python lists to represent the matches that still need to happen, and, for each week, ...
1
2009-04-05T23:57:31Z
[ "python", "postgresql" ]
Help Me Figure Out A Random Scheduling Algorithm using Python and PostgreSQL
719,886
<p>I am trying to do the schedule for the upcoming season for my simulation baseball team. I have an existing Postgresql database that contains the old schedule.</p> <p>There are 648 rows in the database: 27 weeks of series for 24 teams. The problem is that the schedule has gotten predictable and allows teams to kno...
0
2009-04-05T23:34:32Z
719,913
<p>Have you considered keeping your same "schedule", and just shuffling the teams? Generating a schedule where everyone plays each other the proper number of times is possible, but if you already have such a schedule then it's much easier to just shuffle the teams.</p> <p>You could keep your current table, but replac...
2
2009-04-05T23:59:14Z
[ "python", "postgresql" ]
Help Me Figure Out A Random Scheduling Algorithm using Python and PostgreSQL
719,886
<p>I am trying to do the schedule for the upcoming season for my simulation baseball team. I have an existing Postgresql database that contains the old schedule.</p> <p>There are 648 rows in the database: 27 weeks of series for 24 teams. The problem is that the schedule has gotten predictable and allows teams to kno...
0
2009-04-05T23:34:32Z
719,966
<p>I think I've understood your question correctly, but anyhow, you can make use of Python's set datatype and generator functionality:</p> <pre><code>import random def scheduler(teams): """ schedule generator: only accepts an even number of teams! """ if 0 != len(teams) % 2: return while teams: ...
1
2009-04-06T00:45:28Z
[ "python", "postgresql" ]
How do I apply Django model Meta options to models that I did not write?
720,083
<p>I want to apply the <a href="http://docs.djangoproject.com/en/dev/ref/models/options/#ordering">"ordering"</a> Meta option to the Django model User from django.contrib.auth.models. Normally I would just put the Meta class in the model's definition, but in this case I did not define the model. So where do I put the...
5
2009-04-06T02:21:26Z
720,090
<p><a href="http://docs.djangoproject.com/en/dev/topics/db/models/#specifying-the-parent-link-field" rel="nofollow">This is how the Django manual recommends you do it</a>:</p> <blockquote> <p>You could also use a proxy model to define a different default ordering on a model. The standard User model has no ordering d...
9
2009-04-06T02:26:08Z
[ "python", "django" ]
How do I apply Django model Meta options to models that I did not write?
720,083
<p>I want to apply the <a href="http://docs.djangoproject.com/en/dev/ref/models/options/#ordering">"ordering"</a> Meta option to the Django model User from django.contrib.auth.models. Normally I would just put the Meta class in the model's definition, but in this case I did not define the model. So where do I put the...
5
2009-04-06T02:21:26Z
720,093
<p>You can either subclass User:</p> <pre><code>class OrderedUser(User): class Meta: ordering = ['-id', 'username'] </code></pre> <p>Or you could use the ordering in ModelAdmin:</p> <pre><code>class UserAdmin(admin.ModelAdmin): ordering = ['-id', 'username'] # unregister user since its already been ...
3
2009-04-06T02:27:28Z
[ "python", "django" ]
How do I apply Django model Meta options to models that I did not write?
720,083
<p>I want to apply the <a href="http://docs.djangoproject.com/en/dev/ref/models/options/#ordering">"ordering"</a> Meta option to the Django model User from django.contrib.auth.models. Normally I would just put the Meta class in the model's definition, but in this case I did not define the model. So where do I put the...
5
2009-04-06T02:21:26Z
720,114
<p>Contact the author and ask them to make a change.</p>
0
2009-04-06T02:37:57Z
[ "python", "django" ]
How do I apply Django model Meta options to models that I did not write?
720,083
<p>I want to apply the <a href="http://docs.djangoproject.com/en/dev/ref/models/options/#ordering">"ordering"</a> Meta option to the Django model User from django.contrib.auth.models. Normally I would just put the Meta class in the model's definition, but in this case I did not define the model. So where do I put the...
5
2009-04-06T02:21:26Z
720,187
<p>Paolo's answer is great; I wasn't previously aware of the new proxy support. The only issue with it is that you need to target your code to the OrderedUser model - which is in a sense similar to simply doing a <code>User.objects.filter(....).order_by('username')</code>. In other words, it's less verbose but you ne...
6
2009-04-06T03:35:39Z
[ "python", "django" ]
Find Hyperlinks in Text using Python (twitter related)
720,113
<p>How can I parse text and find all instances of hyperlinks with a string? The hyperlink will not be in the html format of <code>&lt;a href="http://test.com"&gt;test&lt;/a&gt;</code> but just <code>http://test.com</code></p> <p>Secondly, I would like to then convert the original string and replace all instances of hy...
12
2009-04-06T02:37:48Z
720,137
<p>Here's a Python port of <a href="http://stackoverflow.com/questions/32637/easiest-way-to-convert-a-url-to-a-hyperlink-in-a-c-string/32648#32648">http://stackoverflow.com/questions/32637/easiest-way-to-convert-a-url-to-a-hyperlink-in-a-c-string/32648#32648</a>:</p> <pre><code>import re myString = "This is my tweet ...
20
2009-04-06T02:53:17Z
[ "python", "regex" ]
Find Hyperlinks in Text using Python (twitter related)
720,113
<p>How can I parse text and find all instances of hyperlinks with a string? The hyperlink will not be in the html format of <code>&lt;a href="http://test.com"&gt;test&lt;/a&gt;</code> but just <code>http://test.com</code></p> <p>Secondly, I would like to then convert the original string and replace all instances of hy...
12
2009-04-06T02:37:48Z
2,102,648
<p><a href="http://mail.python.org/pipermail/tutor/2002-September/017228.html">Here</a> is a much more sophisticated regexp from 2002.</p>
8
2010-01-20T15:45:58Z
[ "python", "regex" ]
Find Hyperlinks in Text using Python (twitter related)
720,113
<p>How can I parse text and find all instances of hyperlinks with a string? The hyperlink will not be in the html format of <code>&lt;a href="http://test.com"&gt;test&lt;/a&gt;</code> but just <code>http://test.com</code></p> <p>Secondly, I would like to then convert the original string and replace all instances of hy...
12
2009-04-06T02:37:48Z
8,982,755
<p>Django also has a solution that doesn't just use regex. It is <a href="https://github.com/django/django/blob/master/django/utils/html.py#L236" rel="nofollow">django.utils.html.urlize()</a>. I found this to be very helpful, especially if you happen to be using django.</p> <p>You can also extract the <a href="https:/...
5
2012-01-24T06:16:53Z
[ "python", "regex" ]
Find Hyperlinks in Text using Python (twitter related)
720,113
<p>How can I parse text and find all instances of hyperlinks with a string? The hyperlink will not be in the html format of <code>&lt;a href="http://test.com"&gt;test&lt;/a&gt;</code> but just <code>http://test.com</code></p> <p>Secondly, I would like to then convert the original string and replace all instances of hy...
12
2009-04-06T02:37:48Z
13,078,582
<p>Jinja2 (Flask uses this) has a filter <code>urlize</code> which does the same.</p> <p><a href="http://jinja.pocoo.org/docs/templates/#urlize" rel="nofollow">Docs</a></p>
1
2012-10-25T22:57:03Z
[ "python", "regex" ]
How do i use Django session to read/set cookies?
720,329
<p>I am trying to use the Django sessions to read and set my cookies, but when i do the following the program just does not respond!</p> <p><strong>sessionID = request.session["userid"]</strong></p> <p>The program does not pass this point!</p> <p>Any ideas?</p>
1
2009-04-06T05:16:05Z
720,394
<p>First, Django already creates a user object for you so you don't need to store it in the session. Just access it as:</p> <pre><code>request.user </code></pre> <p>For example, to get the username you would use:</p> <pre><code>request.user.username </code></pre> <p>Next, if you want to store information in the se...
4
2009-04-06T06:43:15Z
[ "python", "django" ]
Function definition in Python
720,621
<p>I am new to Python. I was trying to define and run a simple function in a class.</p> <p>Can anybody please tell me what's wrong in my code:</p> <pre><code>class A : def m1(name,age,address) : print('Name --&gt;',name) print('Age --&gt;',age) print('Address --&gt;',address) &gt;&gt;&gt; a = A()...
2
2009-04-06T08:36:23Z
720,631
<p>By convention, methods in a class instance receive an object reference as the 1st argument, named <code>self</code>.</p> <pre><code>&gt;&gt;&gt; class A: ... def m1(self,name,age,address): ... print('Name --&gt;',name) ... print('Age --&gt;',age) ... print('Address --&gt;',address) ... &gt;...
4
2009-04-06T08:40:32Z
[ "python" ]
Function definition in Python
720,621
<p>I am new to Python. I was trying to define and run a simple function in a class.</p> <p>Can anybody please tell me what's wrong in my code:</p> <pre><code>class A : def m1(name,age,address) : print('Name --&gt;',name) print('Age --&gt;',age) print('Address --&gt;',address) &gt;&gt;&gt; a = A()...
2
2009-04-06T08:36:23Z
720,632
<p>Instance methods take instance as first argument:</p> <pre><code>class A : def m1(self, name,age,address) : print('Name --&gt;',name) print('Age --&gt;',age) print('Address --&gt;',address) </code></pre> <p>You can also use <a href="http://docs.python.org/library/functions.html#staticme...
19
2009-04-06T08:40:47Z
[ "python" ]
Function definition in Python
720,621
<p>I am new to Python. I was trying to define and run a simple function in a class.</p> <p>Can anybody please tell me what's wrong in my code:</p> <pre><code>class A : def m1(name,age,address) : print('Name --&gt;',name) print('Age --&gt;',age) print('Address --&gt;',address) &gt;&gt;&gt; a = A()...
2
2009-04-06T08:36:23Z
720,633
<p>The first parameter is always the object itself.</p> <pre><code>class A : def m1(self, name,age,address) : print('Name --&gt;',name) print('Age --&gt;',age) print('Address --&gt;',address) </code></pre>
4
2009-04-06T08:41:13Z
[ "python" ]
Removing specific items from Django's cache?
720,800
<p>I'm using site wide caching with <a href="http://en.wikipedia.org/wiki/Memcached">memcached</a> as the backend. I would like to invalidate pages in the cache when the underlying database object changes. </p> <p>If the page name changes then I would invalidate the whole cache (as it affects navigation on every page....
8
2009-04-06T09:36:35Z
721,749
<p>I haven't done a lot of caching with Django, but I think what you want here are <a href="http://docs.djangoproject.com/en/dev/topics/signals/" rel="nofollow"><code>signals</code></a>.</p> <p>You can set up a <code>post_save</code> signal on the underlying object, and have the callback function invalidate that page ...
4
2009-04-06T14:36:44Z
[ "python", "django", "caching", "memcached" ]
Removing specific items from Django's cache?
720,800
<p>I'm using site wide caching with <a href="http://en.wikipedia.org/wiki/Memcached">memcached</a> as the backend. I would like to invalidate pages in the cache when the underlying database object changes. </p> <p>If the page name changes then I would invalidate the whole cache (as it affects navigation on every page....
8
2009-04-06T09:36:35Z
1,995,155
<p>tghw's solution does not actually work, because the cache key is NOT the absolute path. The key is calculated from the absolute path and the HTTP headers. See <a href="http://stackoverflow.com/questions/1995126/invalidating-a-path-from-the-django-cache-recursively">this question</a> for an example.</p>
4
2010-01-03T14:39:44Z
[ "python", "django", "caching", "memcached" ]
HTTP Authentication in Python
720,867
<p>Whats is the python urllib equivallent of </p> <pre><code>curl -u username:password status="abcd" http://example.com/update.json </code></pre> <p>I did this:</p> <pre><code>handle = urllib2.Request(url) authheader = "Basic %s" % base64.encodestring('%s:%s' % (username, password)) handle.add_header("Authorization...
15
2009-04-06T10:03:12Z
720,880
<p>The trick is to create a password manager, and then tell urllib about it. Usually, you won't care about the realm of the authentication, just the host/url part. For example, the following:</p> <pre><code>password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() top_level_url = "http://example.com/" password_mgr.ad...
20
2009-04-06T10:09:33Z
[ "authentication", "curl", "http-headers", "python" ]
HTTP Authentication in Python
720,867
<p>Whats is the python urllib equivallent of </p> <pre><code>curl -u username:password status="abcd" http://example.com/update.json </code></pre> <p>I did this:</p> <pre><code>handle = urllib2.Request(url) authheader = "Basic %s" % base64.encodestring('%s:%s' % (username, password)) handle.add_header("Authorization...
15
2009-04-06T10:03:12Z
720,881
<p>Yes, have a look at the <a href="http://docs.python.org/library/urllib2.html#httpbasicauthhandler-objects">urllib2.HTTP*AuthHandlers</a>.</p> <p>Example from the documentation:</p> <pre><code>import urllib2 # Create an OpenerDirector with support for Basic HTTP Authentication... auth_handler = urllib2.HTTPBasicAut...
6
2009-04-06T10:11:21Z
[ "authentication", "curl", "http-headers", "python" ]
String inside a string
720,927
<pre><code>BASE_URL = 'http://foobar.com?foo=%s' variable = 'bar' final_url = BASE_URL % (variable) </code></pre> <p>I get this <code>'<a href="http://foobar.com?foo=bar" rel="nofollow">http://foobar.com?foo=bar</a>'</code> # It ignores the inside string.</p> <p>But i wanted something like this <code>'<a href="http...
1
2009-04-06T10:43:45Z
720,929
<p>Change your <code>BASE_URL</code> to either</p> <pre><code>BASE_URL = "http://foobar.com?foo='%s'" </code></pre> <p>or</p> <pre><code>BASE_URL = 'http://foobar.com?foo=\'%s\'' </code></pre>
7
2009-04-06T10:45:41Z
[ "python", "string" ]
String inside a string
720,927
<pre><code>BASE_URL = 'http://foobar.com?foo=%s' variable = 'bar' final_url = BASE_URL % (variable) </code></pre> <p>I get this <code>'<a href="http://foobar.com?foo=bar" rel="nofollow">http://foobar.com?foo=bar</a>'</code> # It ignores the inside string.</p> <p>But i wanted something like this <code>'<a href="http...
1
2009-04-06T10:43:45Z
720,942
<p>If you're working with URL parameters, it's probably safer to use urllib.urlencode:</p> <pre><code>import urllib BASE_URL = 'http://foobar.com/?%s' print BASE_URL % urllib.urlencode({ 'foo': 'bar', }) </code></pre> <p>Regarding the quotes: Why do you explicitly want them? Normally your HTTP-wrapper would ha...
6
2009-04-06T10:50:45Z
[ "python", "string" ]
String inside a string
720,927
<pre><code>BASE_URL = 'http://foobar.com?foo=%s' variable = 'bar' final_url = BASE_URL % (variable) </code></pre> <p>I get this <code>'<a href="http://foobar.com?foo=bar" rel="nofollow">http://foobar.com?foo=bar</a>'</code> # It ignores the inside string.</p> <p>But i wanted something like this <code>'<a href="http...
1
2009-04-06T10:43:45Z
721,054
<p>It seems, you are a bit confused about how <em>string literals</em> work.</p> <p>When you say <code>s = 'this is a string'</code>, you are assigning a string to a variable. What string? Well, a string literal that you hardcoded in your program.</p> <p>Python uses the apostrophes to indicate start and end of a stri...
3
2009-04-06T11:27:47Z
[ "python", "string" ]
String inside a string
720,927
<pre><code>BASE_URL = 'http://foobar.com?foo=%s' variable = 'bar' final_url = BASE_URL % (variable) </code></pre> <p>I get this <code>'<a href="http://foobar.com?foo=bar" rel="nofollow">http://foobar.com?foo=bar</a>'</code> # It ignores the inside string.</p> <p>But i wanted something like this <code>'<a href="http...
1
2009-04-06T10:43:45Z
724,698
<p>If you want single quotes to appear in URL you can use</p> <pre><code>BASE_URL = 'http://foobar.com?foo=%s' variable = "'bar'" final_url = BASE_URL % (variable) </code></pre> <p>But this variant is quite insecure, if variable is coming from somewhere (like user input).</p>
1
2009-04-07T08:52:28Z
[ "python", "string" ]
Django templates stripping spaces?
721,035
<p>I'm having trouble with Django templates and CharField models.</p> <p>So I have a model with a CharField that creates a slug that replaces spaces with underscores. If I create an object, Somename Somesurname, this creates slug <em>Somename_Somesurname</em> and gets displayed as expected on the template. </p> <p>Ho...
13
2009-04-06T11:22:15Z
721,082
<p>Django sees the object internally as having two spaces (judging by the two underscores and two spaces in the <code>repr</code> output). The fact that it only shows up with one space in the template is just how HTML works. Notice how, in the question you just asked, most of the places where you entered two spaces, ...
10
2009-04-06T11:45:05Z
[ "python", "django", "django-templates" ]
Django templates stripping spaces?
721,035
<p>I'm having trouble with Django templates and CharField models.</p> <p>So I have a model with a CharField that creates a slug that replaces spaces with underscores. If I create an object, Somename Somesurname, this creates slug <em>Somename_Somesurname</em> and gets displayed as expected on the template. </p> <p>Ho...
13
2009-04-06T11:22:15Z
721,249
<p><a href="http://docs.djangobrasil.org/ref/templates/builtins.html#slugify" rel="nofollow">Slugify</a> removes all leading spaces, you'll need to rewrite this as a custom template tag to get the behaviour you're after. The original filter code looks like this</p> <pre><code>def slugify(value): """ Normalizes s...
0
2009-04-06T12:41:11Z
[ "python", "django", "django-templates" ]
Django templates stripping spaces?
721,035
<p>I'm having trouble with Django templates and CharField models.</p> <p>So I have a model with a CharField that creates a slug that replaces spaces with underscores. If I create an object, Somename Somesurname, this creates slug <em>Somename_Somesurname</em> and gets displayed as expected on the template. </p> <p>Ho...
13
2009-04-06T11:22:15Z
722,266
<p>Let me preface this by saying <a href="#721082">@DNS's answer</a> is correct as to why the spaces are not showing.</p> <p>With that in mind, this template filter will replace any spaces in the string with <code>&amp;nbsp;</code></p> <p>Usage:</p> <pre><code>{{ "hey there world"|spacify }} </code></pre> <p>Outpu...
29
2009-04-06T16:46:38Z
[ "python", "django", "django-templates" ]
Django templates stripping spaces?
721,035
<p>I'm having trouble with Django templates and CharField models.</p> <p>So I have a model with a CharField that creates a slug that replaces spaces with underscores. If I create an object, Somename Somesurname, this creates slug <em>Somename_Somesurname</em> and gets displayed as expected on the template. </p> <p>Ho...
13
2009-04-06T11:22:15Z
10,258,813
<p>There is a built-in template tag <strong>spaceless</strong></p> <pre><code>{% spaceless %} &lt;p&gt; &lt;a href="foo/"&gt;Foo&lt;/a&gt; &lt;/p&gt; {% endspaceless %} </code></pre> <p>Which results:</p> <pre><code>&lt;p&gt;&lt;a href="foo/"&gt;Foo&lt;/a&gt;&lt;/p&gt; </code></pre> <p>Read more in ...
5
2012-04-21T12:16:28Z
[ "python", "django", "django-templates" ]
Django templates stripping spaces?
721,035
<p>I'm having trouble with Django templates and CharField models.</p> <p>So I have a model with a CharField that creates a slug that replaces spaces with underscores. If I create an object, Somename Somesurname, this creates slug <em>Somename_Somesurname</em> and gets displayed as expected on the template. </p> <p>Ho...
13
2009-04-06T11:22:15Z
13,642,196
<p>This tag keeps spaces and newlines. I copied Django's own tag linebreaksbr and then added a line to replace the spaces with nbsp. It does not replace single spaces so text source is still readable. I wrote this because I couldn't get the spacify tag (other answer) to work with linebreaksbr. </p> <pre><code>from dja...
0
2012-11-30T09:21:57Z
[ "python", "django", "django-templates" ]
Django templates stripping spaces?
721,035
<p>I'm having trouble with Django templates and CharField models.</p> <p>So I have a model with a CharField that creates a slug that replaces spaces with underscores. If I create an object, Somename Somesurname, this creates slug <em>Somename_Somesurname</em> and gets displayed as expected on the template. </p> <p>Ho...
13
2009-04-06T11:22:15Z
18,688,638
<p><code>'\s'</code> might be ok in the use case described above, but be careful, this replaces other whitespaces like <code>'\t'</code> or <code>'\n'</code> as well! If this is not what you want, just use <code>" "</code> instead.</p>
0
2013-09-08T21:04:24Z
[ "python", "django", "django-templates" ]
What's a good two-way encryption library implemented in Python?
721,436
<p>The authentication system for an application we're using right now uses a two-way hash that's basically little more than a glorified caesar cypher. Without going into too much detail about what's going on with it, I'd like to replace it with a more secure encryption algorithm (and it needs to be done server-side). ...
4
2009-04-06T13:27:18Z
721,444
<p>If it's two-way, it's not really a "hash". It's encryption (and from the sounds of things this is really more of a 'salt' or 'cypher', <em>not</em> real encryption.) A hash is one-way <em>by definition</em>. So rather than something like MD5 or SHA1 you need to look for something more like PGP.</p> <p>Secondly, ...
7
2009-04-06T13:30:05Z
[ "python", "encryption" ]
What's a good two-way encryption library implemented in Python?
721,436
<p>The authentication system for an application we're using right now uses a two-way hash that's basically little more than a glorified caesar cypher. Without going into too much detail about what's going on with it, I'd like to replace it with a more secure encryption algorithm (and it needs to be done server-side). ...
4
2009-04-06T13:27:18Z
721,457
<p>I assume you want an encryption algorithm, not a hash. The <a href="https://pypi.python.org/pypi/pycrypto" rel="nofollow">PyCrypto</a> library offers a pretty wide range of options. It's in the middle of moving over to a <a href="http://www.dlitz.net/software/pycrypto/" rel="nofollow">new maintainer</a>, so the do...
19
2009-04-06T13:32:06Z
[ "python", "encryption" ]
What's a good two-way encryption library implemented in Python?
721,436
<p>The authentication system for an application we're using right now uses a two-way hash that's basically little more than a glorified caesar cypher. Without going into too much detail about what's going on with it, I'd like to replace it with a more secure encryption algorithm (and it needs to be done server-side). ...
4
2009-04-06T13:27:18Z
721,458
<p><a href="http://www.dlitz.net/software/pycrypto/" rel="nofollow">PyCrypto</a> supports AES, DES, IDEA, RSA, ElGamal, etc.</p> <p>I've found the documentation <a href="http://www.dlitz.net/software/pycrypto/doc/" rel="nofollow">here</a>.</p>
4
2009-04-06T13:32:16Z
[ "python", "encryption" ]
Monitoring internet activity
722,046
<p>I'm looking into writing a small app (in Python) that monitors internet activity. The same idea as <a href="http://www.metal-machine.de/readerror/index.php?page=10" rel="nofollow">NetMeter</a> except with a little more customisation (I need to be able to set off-peak time ranges).</p> <p>Anyway, I've been having a ...
2
2009-04-06T15:45:44Z
722,072
<p>The <a href="http://sourceforge.net/projects/pylibpcap/" rel="nofollow">pylibpcap</a> project may actually give you what you want out of the box, or at least a leg up on implementing one yourself. It's a set of python bindings, as the name suggests, to the libpcap packet capture library.</p>
4
2009-04-06T15:52:28Z
[ "python", "monitoring", "bandwidth" ]
How can I get the width of a wx.ListCtrl and its column name?
722,298
<p>I'm working in wx.Python and I want to get the columns of my wx.ListCtrl to auto-resize i.e. to be at minimum the width of the column name and otherwise as wide as the widest element or its column name. At first I thought the ListCtrlAutoWidthMixin might do this but it doesn't so it looks like I might have to do it...
1
2009-04-06T16:54:59Z
722,878
<p>Yes, you would have to make this yourself for wx.ListCtrl and I'm not sure it would be easy (or elegant) to do right.</p> <p>Consider using a wx.Grid, here is a small example to get you going:</p> <pre><code>import wx, wx.grid class GridData(wx.grid.PyGridTableBase): _cols = "This is a long column name,b,c".s...
1
2009-04-06T19:43:54Z
[ "python", "wxpython", "wxwidgets", "width", "listctrl" ]
How can I get the width of a wx.ListCtrl and its column name?
722,298
<p>I'm working in wx.Python and I want to get the columns of my wx.ListCtrl to auto-resize i.e. to be at minimum the width of the column name and otherwise as wide as the widest element or its column name. At first I thought the ListCtrlAutoWidthMixin might do this but it doesn't so it looks like I might have to do it...
1
2009-04-06T16:54:59Z
723,585
<p>If you'd like to save yourself a lot of headache related to wx.ListCtrl you should switch over to using <a href="http://objectlistview.sourceforge.net/python/" rel="nofollow">ObjectListView</a> (has a nice cookbook and forum for code examples). It's very nice and I tend to use it for anything more than a very basic...
3
2009-04-06T23:03:42Z
[ "python", "wxpython", "wxwidgets", "width", "listctrl" ]
How can I get the width of a wx.ListCtrl and its column name?
722,298
<p>I'm working in wx.Python and I want to get the columns of my wx.ListCtrl to auto-resize i.e. to be at minimum the width of the column name and otherwise as wide as the widest element or its column name. At first I thought the ListCtrlAutoWidthMixin might do this but it doesn't so it looks like I might have to do it...
1
2009-04-06T16:54:59Z
4,856,472
<p>This works for me</p> <pre><code>import wx class Frame(wx.Frame): def __init__(self, *args, **kw): wx.Frame.__init__(self, *args, **kw) self.list = wx.ListCtrl(self, style=wx.LC_REPORT) items = ['A', 'b', 'something really REALLY long'] self.list.InsertColumn(0, "AA") f...
1
2011-01-31T21:43:01Z
[ "python", "wxpython", "wxwidgets", "width", "listctrl" ]
How can I get the width of a wx.ListCtrl and its column name?
722,298
<p>I'm working in wx.Python and I want to get the columns of my wx.ListCtrl to auto-resize i.e. to be at minimum the width of the column name and otherwise as wide as the widest element or its column name. At first I thought the ListCtrlAutoWidthMixin might do this but it doesn't so it looks like I might have to do it...
1
2009-04-06T16:54:59Z
5,705,303
<p>In Addition to jakepars answer: this should check, whether the header is bigger, or the item which takes the most space in the column. Not to elegant but working...</p> <pre><code>import wx class Frame(wx.Frame): def __init__(self, *args, **kw): wx.Frame.__init__(self, *args, **kw) self.list =...
4
2011-04-18T15:34:10Z
[ "python", "wxpython", "wxwidgets", "width", "listctrl" ]
Django GROUP BY strftime date format
722,325
<p>I would like to do a SUM on rows in a database and group by date.</p> <p>I am trying to run this SQL query using Django aggregates and annotations:</p> <pre><code>select strftime('%m/%d/%Y', time_stamp) as the_date, sum(numbers_data) from my_model group by the_date; </code></pre> <p>I tried the following:...
14
2009-04-06T17:04:07Z
722,352
<p>Any reason not to just do this in the database, by running the following query against the database:</p> <pre><code>select date, sum(numbers_data) from my_model group by date; </code></pre> <p>If your answer is, the date is a datetime with non-zero hours, minutes, seconds, or milliseconds, my answer is to use a ...
1
2009-04-06T17:12:10Z
[ "python", "django", "orm", "django-models", "django-aggregation" ]
Django GROUP BY strftime date format
722,325
<p>I would like to do a SUM on rows in a database and group by date.</p> <p>I am trying to run this SQL query using Django aggregates and annotations:</p> <pre><code>select strftime('%m/%d/%Y', time_stamp) as the_date, sum(numbers_data) from my_model group by the_date; </code></pre> <p>I tried the following:...
14
2009-04-06T17:04:07Z
722,880
<p>This works for me:</p> <pre><code>select_data = {"d": """strftime('%%m/%%d/%%Y', time_stamp)"""} data = My_Model.objects.extra(select=select_data).values('d').annotate(Sum("numbers_data")).order_by() </code></pre> <p>Took a bit to figure out I had to escape the % signs.</p>
14
2009-04-06T19:45:11Z
[ "python", "django", "orm", "django-models", "django-aggregation" ]
Django GROUP BY strftime date format
722,325
<p>I would like to do a SUM on rows in a database and group by date.</p> <p>I am trying to run this SQL query using Django aggregates and annotations:</p> <pre><code>select strftime('%m/%d/%Y', time_stamp) as the_date, sum(numbers_data) from my_model group by the_date; </code></pre> <p>I tried the following:...
14
2009-04-06T17:04:07Z
36,063,115
<p>I'm not sure about strftime, my solution below is using sql postgres trunc...</p> <pre><code>select_data = {"date": "date_trunc('day', creationtime)"} ttl = ReportWebclick.objects.using('cms')\ .extra(select=select_data)\ .filter(**filters)\ .values...
1
2016-03-17T14:13:30Z
[ "python", "django", "orm", "django-models", "django-aggregation" ]
Django GROUP BY strftime date format
722,325
<p>I would like to do a SUM on rows in a database and group by date.</p> <p>I am trying to run this SQL query using Django aggregates and annotations:</p> <pre><code>select strftime('%m/%d/%Y', time_stamp) as the_date, sum(numbers_data) from my_model group by the_date; </code></pre> <p>I tried the following:...
14
2009-04-06T17:04:07Z
38,285,830
<p>As of v1.8, you can use <a href="https://docs.djangoproject.com/en/1.8/ref/models/expressions/#func-expressions" rel="nofollow">Func() expressions</a>.</p> <p>For example, if you happen to be targeting AWS Redshift's date and time functions:</p> <pre><code>from django.db.models import F, Func, Value def TimezoneC...
1
2016-07-09T19:57:13Z
[ "python", "django", "orm", "django-models", "django-aggregation" ]
Why doesn't the handle_read method get called with asyncore?
722,605
<p>I am trying to proto-type send/recv via a packet socket using the asyncore dispatcher (code below). Although my handle_write method gets called promptly, the handle_read method doesn't seem to get invoked. The loop() does call the readable method every so often, but I am not able to receive anything. I know there ar...
1
2009-04-06T18:25:01Z
723,053
<p>I finally got this to work with some help from a co-worker. This has to do with passing the protocol argument to the <code>create_socket()</code> method. Unfortunately <code>create_socket()</code> of the dispatcher doesn't take a third argument - so I had to modify my <code>packet_socket()</code> constructor to tak...
1
2009-04-06T20:22:06Z
[ "python", "sockets", "packet" ]
Python class inclusion wrong behaviour
722,640
<p>I have into my main.py</p> <pre><code>from modules import controller ctrl = controller help(ctrl) print(ctrl.div(5,2)) </code></pre> <p>and the controllor.py is:</p> <pre><code>class controller: def div(self, x, y): return x // y </code></pre> <p>when I run my main I got the error:</p> <pre><code>T...
0
2009-04-06T18:37:35Z
722,650
<p>You should create an instance of controller, like this:</p> <pre><code>ctrl = controller() </code></pre> <p>Note the brackets after controller.</p>
2
2009-04-06T18:40:47Z
[ "python" ]
Python class inclusion wrong behaviour
722,640
<p>I have into my main.py</p> <pre><code>from modules import controller ctrl = controller help(ctrl) print(ctrl.div(5,2)) </code></pre> <p>and the controllor.py is:</p> <pre><code>class controller: def div(self, x, y): return x // y </code></pre> <p>when I run my main I got the error:</p> <pre><code>T...
0
2009-04-06T18:37:35Z
722,653
<blockquote> <p>ctrl = controller</p> </blockquote> <p>‘controller’ is a module, representing your whole ‘controller.py’ file. In Python, unlike in Java, there can be any number of symbols defined inside a module, so there isn't a 1:1 relationship between the imported module and the class defined in it.</p> ...
3
2009-04-06T18:41:45Z
[ "python" ]
Python class inclusion wrong behaviour
722,640
<p>I have into my main.py</p> <pre><code>from modules import controller ctrl = controller help(ctrl) print(ctrl.div(5,2)) </code></pre> <p>and the controllor.py is:</p> <pre><code>class controller: def div(self, x, y): return x // y </code></pre> <p>when I run my main I got the error:</p> <pre><code>T...
0
2009-04-06T18:37:35Z
722,696
<p>This is very confusing as shown.</p> <p>When you say</p> <pre><code>from modules import controller </code></pre> <p>You're making the claim that you have a module with a filename of <code>modules.py</code>.</p> <p>OR</p> <p>You're making the claim that you have a package named <code>modules</code>. This direct...
4
2009-04-06T18:55:02Z
[ "python" ]
Python class inclusion wrong behaviour
722,640
<p>I have into my main.py</p> <pre><code>from modules import controller ctrl = controller help(ctrl) print(ctrl.div(5,2)) </code></pre> <p>and the controllor.py is:</p> <pre><code>class controller: def div(self, x, y): return x // y </code></pre> <p>when I run my main I got the error:</p> <pre><code>T...
0
2009-04-06T18:37:35Z
724,665
<p>When you execute following code</p> <pre><code>from modules import controller ctrl = controller </code></pre> <p><strong>ctrl</strong> variable becomes a pointer to <strong>controller</strong> class. To create an instance of <strong>controller</strong> class you need to add parenthesis:</p> <pre><code>from module...
1
2009-04-07T08:36:36Z
[ "python" ]
Best way to turn word list into frequency dict
722,697
<p>What's the best way to convert a list/tuple into a dict where the keys are the distinct values of the list and the values are the the frequencies of those distinct values?</p> <p>In other words:</p> <pre><code>['a', 'b', 'b', 'a', 'b', 'c'] --&gt; {'a': 2, 'b': 3, 'c': 1} </code></pre> <p>(I've had to do somethi...
4
2009-04-06T18:55:11Z
722,702
<p>Kind of</p> <pre><code>from collections import defaultdict fq= defaultdict( int ) for w in words: fq[w] += 1 </code></pre> <p>That usually works nicely.</p>
11
2009-04-06T18:56:16Z
[ "python" ]
Best way to turn word list into frequency dict
722,697
<p>What's the best way to convert a list/tuple into a dict where the keys are the distinct values of the list and the values are the the frequencies of those distinct values?</p> <p>In other words:</p> <pre><code>['a', 'b', 'b', 'a', 'b', 'c'] --&gt; {'a': 2, 'b': 3, 'c': 1} </code></pre> <p>(I've had to do somethi...
4
2009-04-06T18:55:11Z
722,755
<p>I have to share an interesting but kind of ridiculous way of doing it that I just came up with:</p> <pre><code>&gt;&gt;&gt; class myfreq(dict): ... def __init__(self, arr): ... for k in arr: ... self[k] = 1 ... def __setitem__(self, k, v): ... dict.__setitem__(self, k, self.get(k...
0
2009-04-06T19:11:35Z
[ "python" ]
Best way to turn word list into frequency dict
722,697
<p>What's the best way to convert a list/tuple into a dict where the keys are the distinct values of the list and the values are the the frequencies of those distinct values?</p> <p>In other words:</p> <pre><code>['a', 'b', 'b', 'a', 'b', 'c'] --&gt; {'a': 2, 'b': 3, 'c': 1} </code></pre> <p>(I've had to do somethi...
4
2009-04-06T18:55:11Z
722,767
<p>This is an abomination, but:</p> <pre><code>from itertools import groupby dict((k, len(list(xs))) for k, xs in groupby(sorted(items))) </code></pre> <p>I can't think of a reason one would choose this method over S.Lott's, but if someone's going to point it out, it might as well be me. :)</p>
2
2009-04-06T19:15:59Z
[ "python" ]
Best way to turn word list into frequency dict
722,697
<p>What's the best way to convert a list/tuple into a dict where the keys are the distinct values of the list and the values are the the frequencies of those distinct values?</p> <p>In other words:</p> <pre><code>['a', 'b', 'b', 'a', 'b', 'c'] --&gt; {'a': 2, 'b': 3, 'c': 1} </code></pre> <p>(I've had to do somethi...
4
2009-04-06T18:55:11Z
722,806
<p>I find that the easiest to understand (while might not be the most efficient) way is to do:</p> <pre><code>{i:words.count(i) for i in set(words)} </code></pre>
18
2009-04-06T19:28:51Z
[ "python" ]
Best way to turn word list into frequency dict
722,697
<p>What's the best way to convert a list/tuple into a dict where the keys are the distinct values of the list and the values are the the frequencies of those distinct values?</p> <p>In other words:</p> <pre><code>['a', 'b', 'b', 'a', 'b', 'c'] --&gt; {'a': 2, 'b': 3, 'c': 1} </code></pre> <p>(I've had to do somethi...
4
2009-04-06T18:55:11Z
727,509
<p>Just a note that, starting with Python 2.7/3.1, this functionality will be built in to the <code>collections</code> module, see <a href="http://bugs.python.org/issue1696199">this bug</a> for more information. Here's the example from the <a href="http://docs.python.org/dev/whatsnew/2.7.html">release notes</a>:</p> ...
6
2009-04-07T20:42:20Z
[ "python" ]