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
Actionscript flex sockets and telnet
2,215,308
<p>I am trying to make a flex application where it gets data from a telnet connection and I am running into a weird problem.</p> <p>To give a brief introduction, i want to read data from a process that exposes it through a socket. So if in the shell i type <code>telnet localhost 8651</code>i receive the xml and then t...
1
2010-02-07T00:13:09Z
2,270,544
<p>For security reasons, the host you're connecting to must serve Flash socket policy requests on port 943 (or on the same port on which you're attempting your connection). This page shows you how to set this up on the server that you're attempting to connect to:</p> <p><a href="http://www.adobe.com/devnet/flashplayer...
0
2010-02-16T04:01:47Z
[ "python", "flex", "actionscript-3", "sockets", "telnet" ]
Difference Between Modulus Implementation in Python Vs Java
2,215,318
<p>I've noticed differing implementations of the modulus operator in Python and Java.</p> <p>For example, in Python:</p> <pre><code>&gt;&gt;&gt; print -300 % 800 &gt;&gt;&gt; 500 </code></pre> <p>Whereas in Java:</p> <pre><code>System.out.println(-300 % 800); -300 </code></pre> <p>This caught me off guard, since I...
12
2010-02-07T00:16:41Z
2,215,331
<p>I prefer C's interpretation (also used in Python), where <code>%</code> is indeed a modulus operator. Good discussion in the <a href="http://en.wikipedia.org/wiki/Modulo_operation">wikipedia page</a> and the links from it (including one bit about why taking instead the sign of the dividend can lead to one silly bug...
7
2010-02-07T00:20:43Z
[ "java", "python", "modulo" ]
Difference Between Modulus Implementation in Python Vs Java
2,215,318
<p>I've noticed differing implementations of the modulus operator in Python and Java.</p> <p>For example, in Python:</p> <pre><code>&gt;&gt;&gt; print -300 % 800 &gt;&gt;&gt; 500 </code></pre> <p>Whereas in Java:</p> <pre><code>System.out.println(-300 % 800); -300 </code></pre> <p>This caught me off guard, since I...
12
2010-02-07T00:16:41Z
2,215,348
<p>In the C standard, negative modulo is undefined, and many C compilers will give you what Java is giving you. In python though, you will always get a positive number, which is often more useful, because it means you can do modulo addition, and subtraction. If negative modulo were to give you a negative, it would not ...
1
2010-02-07T00:25:50Z
[ "java", "python", "modulo" ]
Difference Between Modulus Implementation in Python Vs Java
2,215,318
<p>I've noticed differing implementations of the modulus operator in Python and Java.</p> <p>For example, in Python:</p> <pre><code>&gt;&gt;&gt; print -300 % 800 &gt;&gt;&gt; 500 </code></pre> <p>Whereas in Java:</p> <pre><code>System.out.println(-300 % 800); -300 </code></pre> <p>This caught me off guard, since I...
12
2010-02-07T00:16:41Z
2,215,631
<p>I prefer <code>x mod n</code> to give numbers between 0 and n-1 or between floor(n/2)-n and floor(n/2); this makes it easiest to do the math in one's head.</p> <p>As long as everything is equivalent, I don't really care when it comes to languages--though one does have to be aware of the convention.</p> <p>Incident...
0
2010-02-07T02:09:59Z
[ "java", "python", "modulo" ]
Difference Between Modulus Implementation in Python Vs Java
2,215,318
<p>I've noticed differing implementations of the modulus operator in Python and Java.</p> <p>For example, in Python:</p> <pre><code>&gt;&gt;&gt; print -300 % 800 &gt;&gt;&gt; 500 </code></pre> <p>Whereas in Java:</p> <pre><code>System.out.println(-300 % 800); -300 </code></pre> <p>This caught me off guard, since I...
12
2010-02-07T00:16:41Z
2,215,695
<p>Now try this:</p> <pre><code>&gt;&gt;&gt; print 300 % -800 -500 </code></pre> <p>The reason for the difference is that language designers can't seem to decide whether the remainder after division should have the sign of the <em>dividend</em> or the <em>divisor</em>. Some languages don't even specify one or the ot...
3
2010-02-07T02:41:22Z
[ "java", "python", "modulo" ]
Django Piston: How can I exclude nested fields from handler results? Is it even possible?
2,215,352
<p>Before the question, the background:</p> <p>I am putting the finishing touches on an API I have written for a Django app utilizing django-piston. The API is able to search by request or IP address which are <code>Request</code> or <code>IPAddress</code> instances respectively. Each request can have 1 or more <code...
5
2010-02-07T00:26:09Z
2,216,589
<p>Can you try using include instead of exclude? E.g.</p> <pre><code>include = (('request', ('inputter', ('username', 'therestofthefields')))) </code></pre> <p>I don't remember if I wrote <code>exclude</code> to be as versatile as <code>include</code>.</p> <p>Also, the django-piston Google group is where we discuss ...
3
2010-02-07T09:47:47Z
[ "python", "django", "django-models", "piston" ]
Django Piston: How can I exclude nested fields from handler results? Is it even possible?
2,215,352
<p>Before the question, the background:</p> <p>I am putting the finishing touches on an API I have written for a Django app utilizing django-piston. The API is able to search by request or IP address which are <code>Request</code> or <code>IPAddress</code> instances respectively. Each request can have 1 or more <code...
5
2010-02-07T00:26:09Z
2,226,040
<p>You can get the desired result by specifying the desired fields via the handler's <code>fields =</code> clause.</p> <p>Model fields coming from foreign keys can be specified like this:</p> <pre><code>('foreign_model_field', ('nested_field1', 'nested_field2')) </code></pre> <p>In your case, the following should wo...
3
2010-02-09T01:03:37Z
[ "python", "django", "django-models", "piston" ]
Fat error bars wanted
2,215,358
<p>Here's a <a href="http://img210.imageshack.us/i/screenshot20100206at703.png/" rel="nofollow">plot</a> I made using <code>matplotlib</code>. It uses the <code>bar</code> and <code>scatter</code> methods from <code>pylab</code>. I have 3 issues:</p> <p>How to make the error bars fatter? No API in <code>bar</code> f...
1
2010-02-07T00:28:53Z
2,215,674
<p>The error bars are drawn with the errorbar method from within the bar method. It accepts an elinewidth argument but it doesn't look like you can pass it through the bar method call. I would just draw them manually.</p> <pre><code>o, = bar(x,m,width=w,color='0.6', yerr=None) # note the comma after the o eBarX = o....
4
2010-02-07T02:28:15Z
[ "python", "matplotlib", "plot" ]
Fat error bars wanted
2,215,358
<p>Here's a <a href="http://img210.imageshack.us/i/screenshot20100206at703.png/" rel="nofollow">plot</a> I made using <code>matplotlib</code>. It uses the <code>bar</code> and <code>scatter</code> methods from <code>pylab</code>. I have 3 issues:</p> <p>How to make the error bars fatter? No API in <code>bar</code> f...
1
2010-02-07T00:28:53Z
29,123,886
<p>Alternatively, to get fat error bars, you can pass "elinewidth" through the "bar"-method as follows:</p> <pre><code>o = bar(x,m,width=w,color='0.6', error_kw={"elinewidth":5}, yerr=e) </code></pre>
0
2015-03-18T13:50:15Z
[ "python", "matplotlib", "plot" ]
Exception when trying to install Django-Treebeard based on instructions
2,215,403
<p>I'm getting a non-descriptive (or at least I don't know how to interpret in this context) error message when sub-lassing from a Django-Treebeard node and am not sure how to debug. I'm using the installation instructions at: <a href="http://code.tabo.pe/django-treebeard/src/tip/tbexample/" rel="nofollow">http://code....
1
2010-02-07T00:45:01Z
2,216,674
<p>You can browse the Django source-code online:</p> <p><a href="https://github.com/django/django/blob/master/django/db/models/base.py#L90" rel="nofollow">https://github.com/django/django/blob/master/django/db/models/base.py#L90</a></p> <p>The relevant code that throws the exception has this comment:</p> <pre><code>...
2
2010-02-07T10:28:01Z
[ "python", "django-treebeard" ]
Exception when trying to install Django-Treebeard based on instructions
2,215,403
<p>I'm getting a non-descriptive (or at least I don't know how to interpret in this context) error message when sub-lassing from a Django-Treebeard node and am not sure how to debug. I'm using the installation instructions at: <a href="http://code.tabo.pe/django-treebeard/src/tip/tbexample/" rel="nofollow">http://code....
1
2010-02-07T00:45:01Z
11,438,472
<p>I had the same error today. To fix you first go to your models.py file and in each of the classes you have to add another class:</p> <pre><code>class Meta: app_label = 'app_name' # medCE.delib in your case </code></pre> <p>I think that you are getting this error because of the period in your app name. If an ap...
3
2012-07-11T17:48:38Z
[ "python", "django-treebeard" ]
Parsing XML with BeautifulSoup and handling missing element
2,215,429
<p>I am using <a href="http://www.crummy.com/software/BeautifulSoup/documentation.html" rel="nofollow">BeautifulSoup</a> to parse XML:</p> <pre><code>xml = """&lt;person&gt; &lt;first_name&gt;Matt&lt;/first_name&gt; &lt;/person&gt;""" soup = BeautifulStoneSoup(xml) first_name = soup.find('first_name').string last_nam...
0
2010-02-07T00:56:11Z
2,215,443
<pre><code>last_name = soup.find('last_name') and soup.find('last_name').string </code></pre> <p>Very silly, but it does meet your equally silly stated restriction (no <code>if</code>). A bit less silly:</p> <pre><code>last_name_node = soup.find('last_name') last_name = last_name_node and last_name_node.string </cod...
4
2010-02-07T01:02:05Z
[ "python", "xml", "exception-handling", "beautifulsoup" ]
Python IRC Client
2,215,448
<pre><code>import socket from time import strftime time = strftime("%H:%M:%S") irc = 'irc.tormented-box.net' port = 6667 channel = '#test' sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sck.connect((irc, port)) print sck.recv(4096) sck.send('NICK supaBOT\r\n') sck.send('USER supaBOT supaBOT supaBOT :supaBOT ...
1
2010-02-07T01:02:51Z
2,215,477
<p>You really need to rethink how you're trying to parse messages; as-is I can say "haha !opwned PRIVMSG" to get ops. (For everyone else: PRIVMSG is part of the IRC protocol, not normally said by users.)</p> <p>However, I don't see the error in your current code, but you've changed what you're really running when you...
3
2010-02-07T01:11:57Z
[ "python" ]
Python IRC Client
2,215,448
<pre><code>import socket from time import strftime time = strftime("%H:%M:%S") irc = 'irc.tormented-box.net' port = 6667 channel = '#test' sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sck.connect((irc, port)) print sck.recv(4096) sck.send('NICK supaBOT\r\n') sck.send('USER supaBOT supaBOT supaBOT :supaBOT ...
1
2010-02-07T01:02:51Z
2,256,715
<p>You may find the Supybot project to be of interest, it is an IRC bot written in Python, and it is very easy to create your own plugins, a lot of this extra work is already done for you, so you can just worry about those features you want. </p> <p>G'Luck!</p> <p><a href="http://sourceforge.net/projects/supybot/revi...
1
2010-02-13T06:31:45Z
[ "python" ]
Placing nodes vertically in Graphviz using pydot
2,215,461
<p>I am using Graphviz in Python via pydot. The diagram I am making has many clusters of directed graphs. pydot is putting them next to each other horizontally resulting in an image that is very wide. How can I tell it to output images of a maximum width so that I can scroll vertically instead?</p>
11
2010-02-07T01:06:03Z
2,215,555
<p>I'm not sure if you're able to do this with your data, but if you change the order that the nodes are inserted into the graph it can really affect the generated graph. If you don't want to supply <em>any</em> ordering information to Graphviz and want Graphviz to attempt solving optimal placement of nodes to minimiz...
5
2010-02-07T01:43:06Z
[ "python", "visualization", "graphviz", "pydot" ]
Placing nodes vertically in Graphviz using pydot
2,215,461
<p>I am using Graphviz in Python via pydot. The diagram I am making has many clusters of directed graphs. pydot is putting them next to each other horizontally resulting in an image that is very wide. How can I tell it to output images of a maximum width so that I can scroll vertically instead?</p>
11
2010-02-07T01:06:03Z
2,215,870
<p>There are several things you can do.</p> <ol> <li><p>You can set the maximum size of your graph, using 'size' (e.g., size = "4, 8" (inches)). This fixes the size of your final layout. Unlike most other node,edge, and graph parameters in the dot language, 'size' has no default. Also, the default orientation is 'port...
10
2010-02-07T03:59:37Z
[ "python", "visualization", "graphviz", "pydot" ]
Placing nodes vertically in Graphviz using pydot
2,215,461
<p>I am using Graphviz in Python via pydot. The diagram I am making has many clusters of directed graphs. pydot is putting them next to each other horizontally resulting in an image that is very wide. How can I tell it to output images of a maximum width so that I can scroll vertically instead?</p>
11
2010-02-07T01:06:03Z
16,312,306
<p>Initialize your graph like this: </p> <p><code>graph = pydot.Dot(graph_type='digraph', rankdir='LR')</code></p> <p>This will set the graph direction from left to right. In general, use the <a href="http://www.graphviz.org/doc/info/attrs.html">graphviz documentation</a> to find the right attribute in order to achie...
7
2013-05-01T03:26:41Z
[ "python", "visualization", "graphviz", "pydot" ]
Django - Determine field type of a variable passed to a template tag
2,215,484
<p>I would like to write a Django template tag to which I can pass a variable.</p> <p>I would like the template tag to behave differently depending on what type of model field the variable was derived from (CharField, BooleanField, IntegerField, etc.) as well as other information used in the field's definition (max_le...
2
2010-02-07T01:13:45Z
2,215,508
<p>You can use python's <code>type</code> function to determine the class type.</p> <pre><code>if type(field) == models.CharField: #CharField specific code elif type(field) == models.IntegerField: #IntegerField specific code </code></pre>
5
2010-02-07T01:25:46Z
[ "python", "django", "variables", "field", "templatetags" ]
Conditional output in Sphinx Documentation
2,215,518
<p>I'm writing some documentation with <a href="http://sphinx.pocoo.org/">Sphinx</a> and I'd like to print out a certain block of text only for HTML documentation, not for LaTeX documentation. Something tells me I should be able to do this with <code>sphinx.ext.ifconfig</code> but I can't figure out how. Does anyone kn...
16
2010-02-07T01:29:35Z
2,215,815
<p>No extension is required. Just use the <a href="http://sphinx.pocoo.org/markup/misc.html?highlight=only#including-content-based-on-tags">only directive</a>. It works like this:</p> <pre> .. only:: latex The stuff in here only appears in the latex output. .. only:: html The stuff in this block only appear...
22
2010-02-07T03:37:11Z
[ "python", "documentation", "python-sphinx" ]
Cgi not executing python
2,215,534
<p>I'm having a problem getting CGI to work for Python.</p> <p>I've added</p> <pre><code>Options ExecCGI AddHandler cgi-script cgi py pl </code></pre> <p>inside /etc/apache2/sites-available/default within <p>and now Perl works, but Python gives out a 500 Internal Server Error..</p> <p>EDIT:</p> <p>This if the cu...
0
2010-02-07T01:35:26Z
2,215,567
<p>Try renaming the .py files to .cgi, it worked for me.</p>
1
2010-02-07T01:48:36Z
[ "python", "apache2", "cgi" ]
Cgi not executing python
2,215,534
<p>I'm having a problem getting CGI to work for Python.</p> <p>I've added</p> <pre><code>Options ExecCGI AddHandler cgi-script cgi py pl </code></pre> <p>inside /etc/apache2/sites-available/default within <p>and now Perl works, but Python gives out a 500 Internal Server Error..</p> <p>EDIT:</p> <p>This if the cu...
0
2010-02-07T01:35:26Z
8,667,161
<p>Try using the python CGI server instead of apache,if that can meet your needs.</p> <pre><code>python -m CGIHTTPServer {port number:default 8000} </code></pre>
0
2011-12-29T11:13:05Z
[ "python", "apache2", "cgi" ]
How do I use gstreamer to make an audio clip from a segment of a longer source?
2,215,683
<p>I would like to use gstreamer to save an arbitrary clip from one audio file to a new file. For example, a segment from 1 minute to 2 minutes in the original. How do I do it?</p>
3
2010-02-07T02:33:01Z
2,224,833
<p>You need <a href="http://wiki.pitivi.org/wiki/GNonLin_documentation" rel="nofollow">gnonlin</a>. See <a href="http://www.jonobacon.org/2006/12/27/using-gnonlin-with-gstreamer-and-python/" rel="nofollow">http://www.jonobacon.org/2006/12/27/using-gnonlin-with-gstreamer-and-python/</a></p> <p>You won't need a <code>gn...
6
2010-02-08T20:58:17Z
[ "python", "audio", "audio-streaming", "gstreamer", "segment" ]
Migrating off AppEngine
2,215,721
<p>I have an application running on AppEngine that uses about 50 CPU hours a day. Most of it is spent waiting for the datastore.</p> <p>I am contemplating moving it off of AppEngine to something like Rackspace Cloud Servers because I think that my application can be more efficient if I can offload some of the work to...
12
2010-02-07T02:59:58Z
2,215,741
<p>If you can redeploy to <a href="http://code.google.com/p/appscale/">appscale</a>, you won't have to rewrite any of your App Engine code.</p>
9
2010-02-07T03:12:01Z
[ "python", "google-app-engine", "web-applications" ]
Migrating off AppEngine
2,215,721
<p>I have an application running on AppEngine that uses about 50 CPU hours a day. Most of it is spent waiting for the datastore.</p> <p>I am contemplating moving it off of AppEngine to something like Rackspace Cloud Servers because I think that my application can be more efficient if I can offload some of the work to...
12
2010-02-07T02:59:58Z
2,220,767
<p>You can use <a href="http://code.google.com/p/typhoonae/" rel="nofollow">TyphoonAE</a>, which is based on the SDK, and designed for small to medium scale deployments - eg, individual servers to small clusters - and should be fairly easy to set up.</p> <p>Sorry to see you go.</p>
1
2010-02-08T10:09:22Z
[ "python", "google-app-engine", "web-applications" ]
Avoid specifying all arguments in a subclass
2,215,923
<p>I have a class:</p> <pre><code>class A(object): def __init__(self,a,b,c,d,e,f,g,...........,x,y,z) #do some init stuff </code></pre> <p>And I have a subclass which needs one extra arg (the last <code>W</code>)</p> <pre><code>class B(A): def __init__(self.a,b,c,d,e,f,g,...........,x,y,z,W) ...
12
2010-02-07T04:24:43Z
2,215,992
<p>Are you wanting something like this?</p> <pre><code>class A(object): def __init__(self, a, b, c, d, e, f, g): # do stuff print a, d, g class B(A): def __init__(self, *args): args = list(args) self.__W = args.pop() A.__init__(self, *args) </code></pre>
0
2010-02-07T04:59:07Z
[ "python", "constructor", "arguments", "subclass" ]
Avoid specifying all arguments in a subclass
2,215,923
<p>I have a class:</p> <pre><code>class A(object): def __init__(self,a,b,c,d,e,f,g,...........,x,y,z) #do some init stuff </code></pre> <p>And I have a subclass which needs one extra arg (the last <code>W</code>)</p> <pre><code>class B(A): def __init__(self.a,b,c,d,e,f,g,...........,x,y,z,W) ...
12
2010-02-07T04:24:43Z
2,216,104
<p>Edit: based on Matt's suggestion, and to address gnibbler's concern re a positional-argument approach; you might check to make sure that the additional subclass-specific argument is being specified—similar to <a href="http://stackoverflow.com/questions/2215923/avoid-specifying-all-arguments-in-a-subclass/2216157#...
5
2010-02-07T05:55:06Z
[ "python", "constructor", "arguments", "subclass" ]
Avoid specifying all arguments in a subclass
2,215,923
<p>I have a class:</p> <pre><code>class A(object): def __init__(self,a,b,c,d,e,f,g,...........,x,y,z) #do some init stuff </code></pre> <p>And I have a subclass which needs one extra arg (the last <code>W</code>)</p> <pre><code>class B(A): def __init__(self.a,b,c,d,e,f,g,...........,x,y,z,W) ...
12
2010-02-07T04:24:43Z
2,216,157
<p>Considering that arguments could be passed either by name or by position, I'd code:</p> <pre><code>class B(A): def __init__(self, *a, **k): if 'W' in k: w = k.pop('W') else: w = a.pop() A.__init__(self, *a, **k) self._W = w </code></pre>
13
2010-02-07T06:20:57Z
[ "python", "constructor", "arguments", "subclass" ]
Avoid specifying all arguments in a subclass
2,215,923
<p>I have a class:</p> <pre><code>class A(object): def __init__(self,a,b,c,d,e,f,g,...........,x,y,z) #do some init stuff </code></pre> <p>And I have a subclass which needs one extra arg (the last <code>W</code>)</p> <pre><code>class B(A): def __init__(self.a,b,c,d,e,f,g,...........,x,y,z,W) ...
12
2010-02-07T04:24:43Z
37,796,160
<p>In situations where some or all of the arguments passed to <code>__init__</code> have default values, it can be useful to avoid repeating the <code>__init__</code> method signature in subclasses.</p> <p>In these cases, <code>__init__</code> can pass any extra arguments to another method, which subclasses can overri...
1
2016-06-13T17:51:17Z
[ "python", "constructor", "arguments", "subclass" ]
gstreamer playbin - setting uri on windows
2,216,064
<p>I am trying to play some audio files with the CLI example on this site:</p> <p><a href="http://pygstdocs.berlios.de/pygst-tutorial/playbin.html" rel="nofollow">http://pygstdocs.berlios.de/pygst-tutorial/playbin.html</a> http://pygstdocs.berlios.de/pygst-tutorial/playbin.html </p> <p>I am on windows and it is givin...
2
2010-02-07T05:33:15Z
2,216,842
<p>As you may have suspected, this code is rather badly written:</p> <pre><code>for filepath in sys.argv[1:]: # ... self.player.set_property("uri", "file://" + filepath) </code></pre> <p>Use something like this:</p> <pre><code>'file:' + urllib.pathname2url(filepath) </code></pre> <p>and (in the command line...
9
2010-02-07T13:12:27Z
[ "python", "gstreamer" ]
gstreamer playbin - setting uri on windows
2,216,064
<p>I am trying to play some audio files with the CLI example on this site:</p> <p><a href="http://pygstdocs.berlios.de/pygst-tutorial/playbin.html" rel="nofollow">http://pygstdocs.berlios.de/pygst-tutorial/playbin.html</a> http://pygstdocs.berlios.de/pygst-tutorial/playbin.html </p> <p>I am on windows and it is givin...
2
2010-02-07T05:33:15Z
2,217,161
<p>Did you notice the actual path you've got is <code>file:///C:/file:/C:/voice.mp3</code>?</p> <p>The correct path should be: <code>file:///C:/path/to/voice.mp3</code>.</p>
4
2010-02-07T15:05:24Z
[ "python", "gstreamer" ]
Google App Engine: How do I save uploaded text file to Blob, then read from it line by line?
2,216,134
<p>I have a huge file (over 16,000 lines) that I want to save in the datastore for parsing later. Each line contains info on an entity. </p> <p>How do I read line by line from the stored Blob? </p> <p>I can't seem to find a good tutorial or documentation on a Blob anywhere. GAE only shows how to deal with images, b...
0
2010-02-07T06:04:22Z
2,216,215
<p>Use the <a href="http://code.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Text.html" rel="nofollow"><code>Text</code></a> <a href="http://code.google.com/appengine/docs/java/datastore/dataclasses.html" rel="nofollow">type</a> to store it instead of a blob. Text does not have any limits o...
1
2010-02-07T06:50:36Z
[ "python", "google-app-engine", "blob" ]
Google App Engine: How do I save uploaded text file to Blob, then read from it line by line?
2,216,134
<p>I have a huge file (over 16,000 lines) that I want to save in the datastore for parsing later. Each line contains info on an entity. </p> <p>How do I read line by line from the stored Blob? </p> <p>I can't seem to find a good tutorial or documentation on a Blob anywhere. GAE only shows how to deal with images, b...
0
2010-02-07T06:04:22Z
2,220,750
<p>If you simply need the lines from the blob, just do:</p> <pre><code>lines = blob.split("\n") </code></pre> <p>If you need to treat the blob like a file, do:</p> <pre><code>fh = StringIO.StringIO(blob) </code></pre>
0
2010-02-08T10:06:55Z
[ "python", "google-app-engine", "blob" ]
How to get path of Start Menu's Programs directory?
2,216,173
<p>...for current user? for all users? </p> <p>I'm working an a small program which needs to create links in the start menu. Currently I'm hardcoding like below, but it only works in english locales, for example it should be "Startmenü" in german. What are cleaner, more portable approaches?</p> <pre><code>OUR_STARTM...
5
2010-02-07T06:26:03Z
2,216,217
<p>I've heard of 2 ways of doing this. First:</p> <pre><code>from win32com.shell import shell shell.SHGetSpecialFolderPath(0,shellcon.CSIDL_COMMON_STARTMENU) </code></pre> <p>Second, using the WScript.Shell object (source : <a href="http://www.mail-archive.com/python-win32@python.org/msg00992.html">http://www.mail-ar...
9
2010-02-07T06:51:11Z
[ "python", "windows" ]
How to get path of Start Menu's Programs directory?
2,216,173
<p>...for current user? for all users? </p> <p>I'm working an a small program which needs to create links in the start menu. Currently I'm hardcoding like below, but it only works in english locales, for example it should be "Startmenü" in german. What are cleaner, more portable approaches?</p> <pre><code>OUR_STARTM...
5
2010-02-07T06:26:03Z
2,216,323
<p>Also, CSIDL_COMMON_STARTMENU is for all user startup and CSIDL_STARTMENU for current user startup.</p>
2
2010-02-07T07:46:57Z
[ "python", "windows" ]
How to get path of Start Menu's Programs directory?
2,216,173
<p>...for current user? for all users? </p> <p>I'm working an a small program which needs to create links in the start menu. Currently I'm hardcoding like below, but it only works in english locales, for example it should be "Startmenü" in german. What are cleaner, more portable approaches?</p> <pre><code>OUR_STARTM...
5
2010-02-07T06:26:03Z
2,234,729
<p>A friend, Luke Pinner of Environment.gov.au, gave a solution by email which uses a core module (python 2.5+). Believed to be multi-lingual as the return from the API call is unicode. Tested on Win7 with Japanese locale, and on another us-english machine by manually changing Start Menu to point to <em>%USERPROFILE%\S...
1
2010-02-10T05:49:59Z
[ "python", "windows" ]
How can I validate a date in Python 3.x?
2,216,250
<p>I would like to have the user input a date, something like:</p> <pre><code>date = input('Date (m/dd/yyyy): ') </code></pre> <p>and then make sure that the input is a valid date. I don't really care that much about the date format.</p> <p>Thanks for any input.</p>
16
2010-02-07T07:13:59Z
2,216,257
<p>You can use the <a href="http://docs.python.org/library/time.html#time.strptime"><code>time</code> module's <code>strptime()</code></a> function:</p> <pre><code>import time date = input('Date (mm/dd/yyyy): ') try: valid_date = time.strptime(date, '%m/%d/%Y') except ValueError: print('Invalid date!') </code></pr...
26
2010-02-07T07:17:23Z
[ "python", "validation", "date", "python-3.x" ]
How can I validate a date in Python 3.x?
2,216,250
<p>I would like to have the user input a date, something like:</p> <pre><code>date = input('Date (m/dd/yyyy): ') </code></pre> <p>and then make sure that the input is a valid date. I don't really care that much about the date format.</p> <p>Thanks for any input.</p>
16
2010-02-07T07:13:59Z
2,226,760
<p>Max S., </p> <p>Thanks for the code. Here is how I implemented it:</p> <pre><code>while True: date = input('Date (m/dd/yyyy): ') try: date = time.strptime(date, '%m/%d/%Y') break except ValueError: print('Invalid date!') continue </code></pre>
1
2010-02-09T04:30:38Z
[ "python", "validation", "date", "python-3.x" ]
How can I validate a date in Python 3.x?
2,216,250
<p>I would like to have the user input a date, something like:</p> <pre><code>date = input('Date (m/dd/yyyy): ') </code></pre> <p>and then make sure that the input is a valid date. I don't really care that much about the date format.</p> <p>Thanks for any input.</p>
16
2010-02-07T07:13:59Z
8,604,597
<pre><code>def validDate(y, m, d): Result = True try: d = datetime.date(int(y), int(m), int(d)) except ValueError, e: Result = False return Result </code></pre> <p>and in the program use the function defined previously:</p> <pre><code> if not validDate(year_file, month_file, day_file): return 0 </c...
1
2011-12-22T13:32:21Z
[ "python", "validation", "date", "python-3.x" ]
What is this traceback error in Python?
2,216,259
<pre><code>Traceback: File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response 92. response = callback(request, *callback_args, **callback_kwargs) File "/home/ea/ea-repos/hell/life/views.py" in linkedin_auth 137. token = oauth_linkedin.get_unauthorised_request_t...
2
2010-02-07T07:18:59Z
2,216,320
<p>Look at this answer to see if it helps you: <a href="http://stackoverflow.com/questions/1925639/httplib-cannotsendrequest-error-in-wsgi">http://stackoverflow.com/questions/1925639/httplib-cannotsendrequest-error-in-wsgi</a></p>
1
2010-02-07T07:45:04Z
[ "python", "django", "url", "oauth" ]
Adding values in a tuple that is in a list in python
2,216,450
<p>I retrieve some data from a database which returns it in a list of tuple values such as this: [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)]</p> <p>Is there a function that can sum up the values in the list of tuples? For example, the above sample should...
3
2010-02-07T08:46:27Z
2,216,456
<pre><code>&gt;&gt;&gt; l = [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)] &gt;&gt;&gt; s = sum(i[0] for i in l) &gt;&gt;&gt; print s 18 </code></pre>
4
2010-02-07T08:48:10Z
[ "python", "list", "tuples" ]
Adding values in a tuple that is in a list in python
2,216,450
<p>I retrieve some data from a database which returns it in a list of tuple values such as this: [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)]</p> <p>Is there a function that can sum up the values in the list of tuples? For example, the above sample should...
3
2010-02-07T08:46:27Z
2,216,486
<pre><code>&gt;&gt;&gt;&gt; l=[(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)] &gt;&gt;&gt; sum(map(sum,l)) 18 &gt;&gt;&gt; l[0]=(1,2,3,) &gt;&gt;&gt; l [(1, 2, 3), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,...
7
2010-02-07T09:04:53Z
[ "python", "list", "tuples" ]
Adding values in a tuple that is in a list in python
2,216,450
<p>I retrieve some data from a database which returns it in a list of tuple values such as this: [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)]</p> <p>Is there a function that can sum up the values in the list of tuples? For example, the above sample should...
3
2010-02-07T08:46:27Z
2,217,008
<p>Just some fun with itertools, not very readable. Works only if you consider 1st element in tuple. </p> <pre><code>&gt;&gt;&gt; import itertools &gt;&gt;&gt; l = [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)] &gt;&gt;&gt; sum(*itertools.izip(*l)) 18 </co...
0
2010-02-07T14:11:26Z
[ "python", "list", "tuples" ]
Python KMeans Orange Framework
2,216,707
<p>I am planning to use <a href="http://orange.biolab.si/" rel="nofollow">orange</a> for kmeans clustering. I have gone through the tutorials, but I still have a couple of questions which I would like to ask:</p> <p>I am dealing with clustering on vectors of high dimension. 1) Is there a cosine distance implemented? 2...
1
2010-02-07T10:44:59Z
2,217,460
<p>Four questions in one question is extremely awkward -- why not make a question one question? It's not as if it would cost you;-). Anyway, wrt "How do I indicate an empty value?", see <a href="http://www.ailab.si/orange/doc/reference/Value.htm" rel="nofollow">the docs</a> regarding attribute <code>value</code> of ins...
2
2010-02-07T17:02:51Z
[ "python", "cluster-analysis", "orange" ]
Python KMeans Orange Framework
2,216,707
<p>I am planning to use <a href="http://orange.biolab.si/" rel="nofollow">orange</a> for kmeans clustering. I have gone through the tutorials, but I still have a couple of questions which I would like to ask:</p> <p>I am dealing with clustering on vectors of high dimension. 1) Is there a cosine distance implemented? 2...
1
2010-02-07T10:44:59Z
28,037,366
<p>I think the original KMeans is not suitable for cosine distance. For its not in Euclidean space, You need to define the centroid of cosine distance, and you couldn't guarantee convergence. But if your feature vectors are all positive, you can try. More information: <a href="https://github.com/scikit-learn/scikit-lea...
0
2015-01-20T03:37:04Z
[ "python", "cluster-analysis", "orange" ]
Disassembling with python - no easy solution?
2,216,816
<p>I'm trying to create a python script that will disassemble a binary (a Windows exe to be precise) and analyze its code. I need the ability to take a certain buffer, and extract some sort of struct containing information about the instructions in it.</p> <p>I've worked with libdisasm in C before, and I found it's in...
7
2010-02-07T13:02:20Z
2,217,220
<p>Well, after much meddling around, I managed to compile SWIGed libdisasm! Unfortunately, it seems to crash python on incorrect (and sometimes correct) usage. How I did it:</p> <ol> <li>I compiled libdisasm.lib using Visual Studio 6, the only thing you need for this is the source code in whichever libdisasm release ...
2
2010-02-07T15:25:52Z
[ "python", "swig", "disassembler" ]
Disassembling with python - no easy solution?
2,216,816
<p>I'm trying to create a python script that will disassemble a binary (a Windows exe to be precise) and analyze its code. I need the ability to take a certain buffer, and extract some sort of struct containing information about the instructions in it.</p> <p>I've worked with libdisasm in C before, and I found it's in...
7
2010-02-07T13:02:20Z
2,217,797
<p>You might try using <a href="http://docs.python.org/library/ctypes.html" rel="nofollow">ctypes</a> to interface directly with libdisasm instead of going through a SWIG layer. It may be take more development time but AFAIK you should be able to access the underlying functionality using ctypes.</p>
2
2010-02-07T18:35:21Z
[ "python", "swig", "disassembler" ]
Disassembling with python - no easy solution?
2,216,816
<p>I'm trying to create a python script that will disassemble a binary (a Windows exe to be precise) and analyze its code. I need the ability to take a certain buffer, and extract some sort of struct containing information about the instructions in it.</p> <p>I've worked with libdisasm in C before, and I found it's in...
7
2010-02-07T13:02:20Z
2,809,749
<p>I recommend you look at <a href="http://code.google.com/p/pymsasid/" rel="nofollow">Pym's disassembly library</a> which is also the backend for <a href="http://pyms86.appspot.com/" rel="nofollow">Pym's online disassembler</a>.</p>
2
2010-05-11T10:12:42Z
[ "python", "swig", "disassembler" ]
Disassembling with python - no easy solution?
2,216,816
<p>I'm trying to create a python script that will disassemble a binary (a Windows exe to be precise) and analyze its code. I need the ability to take a certain buffer, and extract some sort of struct containing information about the instructions in it.</p> <p>I've worked with libdisasm in C before, and I found it's in...
7
2010-02-07T13:02:20Z
10,066,011
<p>You can use the distorm library: <a href="https://code.google.com/p/distorm/" rel="nofollow">https://code.google.com/p/distorm/</a></p> <p>Here's another build: <a href="http://breakingcode.wordpress.com/2009/08/31/using-distorm-with-python-2-6-and-python-3-x-revisited/" rel="nofollow">http://breakingcode.wordpress...
1
2012-04-08T19:58:57Z
[ "python", "swig", "disassembler" ]
Mock Y of (from X import Y) in doctest (python)
2,216,828
<p>I'm trying to create a doctest with mock of function that resides in a separate module and that is imported as bellow</p> <pre><code>from foomodule import foo def bar(): """ &gt;&gt;&gt; from minimock import mock &gt;&gt;&gt; mock('foo', nsdicts=(bar.func_globals,), returns=5) &gt;&gt;&gt; bar() ...
2
2010-02-07T13:06:31Z
2,217,512
<p>You've just met one of the many reasons that make it best to never import object from "within" modules -- only modules themselves (possibly from within packages). We've made this rule part of our style guidelines at Google (published <a href="http://google-styleguide.googlecode.com/svn/trunk/pyguide.html" rel="nofo...
4
2010-02-07T17:16:19Z
[ "python", "testing", "mocking", "doctest" ]
Mock Y of (from X import Y) in doctest (python)
2,216,828
<p>I'm trying to create a doctest with mock of function that resides in a separate module and that is imported as bellow</p> <pre><code>from foomodule import foo def bar(): """ &gt;&gt;&gt; from minimock import mock &gt;&gt;&gt; mock('foo', nsdicts=(bar.func_globals,), returns=5) &gt;&gt;&gt; bar() ...
2
2010-02-07T13:06:31Z
2,217,666
<p>Finally, found out that this was rather an issue of trunk version of MiniMock. Old stable one performs as expected.</p>
1
2010-02-07T18:02:00Z
[ "python", "testing", "mocking", "doctest" ]
How to evaluate a matched number later in a regex? - Lexing FORTRAN 'H' edit descriptor with Ply
2,216,843
<p>I am using <a href="http://www.dabeaz.com/ply/" rel="nofollow">Ply</a> to interpret a FORTRAN format string. I am having trouble writing a regex to match the <a href="http://www.fortran.com/F77_std/rjcnf0001-sh-13.html#sh-13.5.2" rel="nofollow">'H' edit descriptor</a> which is of the form</p> <p><i>x</i>H<i>ccccc ....
1
2010-02-07T13:12:27Z
2,216,855
<p>Regex can't do things like that. You can hack it though:</p> <pre><code>(1[Hh].|2[Hh]..|3[Hh]...|etc...) </code></pre> <p>Ugly!</p>
2
2010-02-07T13:17:28Z
[ "python", "regex", "lexical-analysis", "ply" ]
How to evaluate a matched number later in a regex? - Lexing FORTRAN 'H' edit descriptor with Ply
2,216,843
<p>I am using <a href="http://www.dabeaz.com/ply/" rel="nofollow">Ply</a> to interpret a FORTRAN format string. I am having trouble writing a regex to match the <a href="http://www.fortran.com/F77_std/rjcnf0001-sh-13.html#sh-13.5.2" rel="nofollow">'H' edit descriptor</a> which is of the form</p> <p><i>x</i>H<i>ccccc ....
1
2010-02-07T13:12:27Z
2,217,390
<p>This is what comes of thinking that regexps can replace a lexer.</p> <p>Short version: regular expressions can only deal with that small subset of all possible language termed "regular" (big surprise, I know). But "regular" is not isomorphic to the human understanding of "simple", so even very simple languages can ...
0
2010-02-07T16:36:51Z
[ "python", "regex", "lexical-analysis", "ply" ]
How to evaluate a matched number later in a regex? - Lexing FORTRAN 'H' edit descriptor with Ply
2,216,843
<p>I am using <a href="http://www.dabeaz.com/ply/" rel="nofollow">Ply</a> to interpret a FORTRAN format string. I am having trouble writing a regex to match the <a href="http://www.fortran.com/F77_std/rjcnf0001-sh-13.html#sh-13.5.2" rel="nofollow">'H' edit descriptor</a> which is of the form</p> <p><i>x</i>H<i>ccccc ....
1
2010-02-07T13:12:27Z
2,219,463
<p>Pyparsing includes an adaptive expression that is very similar to this, called countedArray. countedArray(expr) parses a leading integer 'n' and then parses 'n' instances of expr, returning the whole array as a single list. The way this works is that countedArray parses a leading integer expression, followed by an...
0
2010-02-08T03:50:31Z
[ "python", "regex", "lexical-analysis", "ply" ]
How to make a relation between tables using SQLAlchemy?
2,216,887
<p>Using SQLAlchemy, given tables such as these:</p> <pre><code>locations_table = Table('locations', metadata, Column('id', Integer, primary_key=True), Column('name', Text), ) players_table = Table('players', metadata, Column('id', Integer, primary_key=True), Column('email', ...
1
2010-02-07T13:27:31Z
2,216,935
<p>Use sqlalchemy's relation feature:</p> <p><a href="http://www.sqlalchemy.org/docs/ormtutorial.html#building-a-relation" rel="nofollow">http://www.sqlalchemy.org/docs/ormtutorial.html#building-a-relation</a></p>
3
2010-02-07T13:44:13Z
[ "python", "orm", "sqlalchemy", "relation" ]
How to make a relation between tables using SQLAlchemy?
2,216,887
<p>Using SQLAlchemy, given tables such as these:</p> <pre><code>locations_table = Table('locations', metadata, Column('id', Integer, primary_key=True), Column('name', Text), ) players_table = Table('players', metadata, Column('id', Integer, primary_key=True), Column('email', ...
1
2010-02-07T13:27:31Z
2,217,138
<p>This should work for you:</p> <p>mapper(Player, players_table, properties={'location'=relation(Location, uselist=False, backref=backref('players'))})</p> <p>That way you can access the location directly as you won't get a list. Other than that, you can do location.players which will give you an InstrumentedList ba...
1
2010-02-07T14:59:16Z
[ "python", "orm", "sqlalchemy", "relation" ]
Django ModelForm for Many-to-Many fields
2,216,974
<p>Consider the following models and form:</p> <pre><code>class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: mode...
47
2010-02-07T13:59:48Z
2,217,615
<p>I'm not sure if this is what your looking for, but are you aware that Pizza has the <code>topping_set</code> attribute? Using that attribute you could easily add a new topping in your ModelForm.</p> <pre><code>new_pizza.topping_set.add(new_topping) </code></pre>
1
2010-02-07T17:44:13Z
[ "python", "django", "django-forms" ]
Django ModelForm for Many-to-Many fields
2,216,974
<p>Consider the following models and form:</p> <pre><code>class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: mode...
47
2010-02-07T13:59:48Z
2,217,675
<p>To be honest, I would put the many-to-many relation into the <code>Pizza</code> model. I think this closer to reality. Imagine a person that orders several pizzas. He wouldn't say "I would like cheese on pizza one and two and tomatoes on pizza one and three" but probably "One pizza with cheese, one pizza with cheese...
5
2010-02-07T18:05:10Z
[ "python", "django", "django-forms" ]
Django ModelForm for Many-to-Many fields
2,216,974
<p>Consider the following models and form:</p> <pre><code>class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: mode...
47
2010-02-07T13:59:48Z
2,230,481
<p>We had similar problem in our app, which used django admin. There is many to many relation between users and groups and one can't easily add users to a group. I have created a <a href="http://code.djangoproject.com/attachment/ticket/10964/group_admin.patch" rel="nofollow">patch</a> for django, that does this, but th...
2
2010-02-09T16:07:16Z
[ "python", "django", "django-forms" ]
Django ModelForm for Many-to-Many fields
2,216,974
<p>Consider the following models and form:</p> <pre><code>class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: mode...
47
2010-02-07T13:59:48Z
2,232,307
<p>I'm not certain I get the question 100%, so I'm going to run with this assumption: </p> <p>Each <code>Pizza</code> can have many <code>Topping</code>s. Each <code>Topping</code> can have many <code>Pizza</code>s. But if a <code>Topping</code> is added to a <code>Pizza</code>, that <code>Topping</code> then automag...
11
2010-02-09T20:28:48Z
[ "python", "django", "django-forms" ]
Django ModelForm for Many-to-Many fields
2,216,974
<p>Consider the following models and form:</p> <pre><code>class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: mode...
47
2010-02-07T13:59:48Z
2,264,722
<p>I guess you would have here to add a new <code>ModelMultipleChoiceField</code> to your <code>PizzaForm</code>, and manually link that form field with the model field, as Django won't do that automatically for you.</p> <p>The following snippet might be helpful :</p> <pre><code>class PizzaForm(forms.ModelForm): ...
85
2010-02-15T08:31:09Z
[ "python", "django", "django-forms" ]
Django ModelForm for Many-to-Many fields
2,216,974
<p>Consider the following models and form:</p> <pre><code>class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: mode...
47
2010-02-07T13:59:48Z
2,701,440
<p>I did something similar based in code of Clément with a user admin form:</p> <pre><code># models.py class Clinica(models.Model): ... users = models.ManyToManyField(User, null=True, blank=True, related_name='clinicas') # admin.py class CustomUserChangeForm(UserChangeForm): clinicas = forms.ModelMultipleChoic...
0
2010-04-23T19:31:19Z
[ "python", "django", "django-forms" ]
Django ModelForm for Many-to-Many fields
2,216,974
<p>Consider the following models and form:</p> <pre><code>class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name = models.CharField(max_length=50) ison = models.ManyToManyField(Pizza, blank=True) class ToppingForm(forms.ModelForm): class Meta: mode...
47
2010-02-07T13:59:48Z
17,253,659
<p>Another simple way to achieve this is to create an intermediary table and using inline fields to get it done. Please refer to this <a href="https://docs.djangoproject.com/en/1.2/ref/contrib/admin/#working-with-many-to-many-intermediary-models" rel="nofollow">https://docs.djangoproject.com/en/1.2/ref/contrib/admin/#w...
2
2013-06-22T17:52:35Z
[ "python", "django", "django-forms" ]
Override 'in' operator?
2,217,001
<p>If I am creating my own class in Python, what function should I define so as to allow the use of the 'in' operator, e.g.</p> <pre><code>class MyClass(object): ... m = MyClass() if 54 in m: ... </code></pre>
92
2010-02-07T14:08:27Z
2,217,005
<p><a href="http://docs.python.org/reference/datamodel.html#object.__contains__"><code>MyClass.__contains__(self, item)</code></a></p>
122
2010-02-07T14:10:19Z
[ "python", "operators", "operator-overloading" ]
Override 'in' operator?
2,217,001
<p>If I am creating my own class in Python, what function should I define so as to allow the use of the 'in' operator, e.g.</p> <pre><code>class MyClass(object): ... m = MyClass() if 54 in m: ... </code></pre>
92
2010-02-07T14:08:27Z
2,218,413
<p>A more complete answer is:</p> <pre><code>class MyClass(object): def __init__(self): self.numbers = [1,2,3,4,54] def __contains__(self, key): return key in self.numbers </code></pre> <p>Here you would get True when asking if 54 was in m:</p> <pre><code>&gt;&gt;&gt; m = MyClass() &gt;&gt;...
101
2010-02-07T21:26:28Z
[ "python", "operators", "operator-overloading" ]
Override 'in' operator?
2,217,001
<p>If I am creating my own class in Python, what function should I define so as to allow the use of the 'in' operator, e.g.</p> <pre><code>class MyClass(object): ... m = MyClass() if 54 in m: ... </code></pre>
92
2010-02-07T14:08:27Z
2,225,447
<p>You might also want to take a look at an infix operator override framework I was able to use to create a domain-specific language:</p> <p><a href="http://code.activestate.com/recipes/384122/" rel="nofollow">http://code.activestate.com/recipes/384122/</a></p>
4
2010-02-08T22:41:37Z
[ "python", "operators", "operator-overloading" ]
convention to represent the exit status and actual result in XMLRPC
2,217,072
<p>in the C world, a function can return error code to represent the exit status, and use INOUT/OUT parameter to carry the actual fruit of the process. when it comes to xmlrpc, no INOUT/OUT parameter, is there any best practice/conventions to represent the exit status and actual result? </p> <p>the context is i am try...
0
2010-02-07T14:33:31Z
2,217,449
<p>One simple way to implement this in Python is with a tuple. Have your function return a tuple of: (status, result) where the status can be numeric or a string, and the result can be any Python data structure you fancy.</p> <p>Here's an example, adapted from the module documentation. Server code:</p> <pre><code>fro...
1
2010-02-07T17:00:10Z
[ "python", "function", "xml-rpc", "exit-code" ]
convention to represent the exit status and actual result in XMLRPC
2,217,072
<p>in the C world, a function can return error code to represent the exit status, and use INOUT/OUT parameter to carry the actual fruit of the process. when it comes to xmlrpc, no INOUT/OUT parameter, is there any best practice/conventions to represent the exit status and actual result? </p> <p>the context is i am try...
0
2010-02-07T14:33:31Z
2,218,419
<p>"in the C world, a function can return error code to represent the exit status, and use INOUT/OUT parameter to carry the actual fruit of the process"</p> <ol> <li><p>Consider an exit status to be a hack. It's not a C-ism, it's a Linux-ism. C functions return exactly one value. C doesn't have exceptions, so there...
1
2010-02-07T21:28:11Z
[ "python", "function", "xml-rpc", "exit-code" ]
Using a debugger and curses at the same time?
2,217,109
<p>I'm calling <code>python -m pdb myapp.py</code>, when an exception fires, and I'd normally be thrown back to the pdb interpreter to investigate the problem. However this exception is being thrown after I've called through <code>curses.wrapper()</code> and entered curses mode, rendering the pdb interpreter useless. H...
8
2010-02-07T14:51:48Z
2,633,977
<p>Not being familiar with Python, this may not be exactly what you want. But apparently, winpdb can attach to a script - just like gdb can to a running process (IIUC). </p> <p><a href="http://winpdb.org/docs/launch-time/">http://winpdb.org/docs/launch-time/</a></p> <p>Don't be mislead by the name, it is platform ind...
7
2010-04-13T23:48:05Z
[ "python", "exception", "interpreter", "curses", "pdb" ]
Using a debugger and curses at the same time?
2,217,109
<p>I'm calling <code>python -m pdb myapp.py</code>, when an exception fires, and I'd normally be thrown back to the pdb interpreter to investigate the problem. However this exception is being thrown after I've called through <code>curses.wrapper()</code> and entered curses mode, rendering the pdb interpreter useless. H...
8
2010-02-07T14:51:48Z
2,949,419
<p>James` answer is a good and I've upvoted it but I'd also consider trying to split the logic and presentation layers of my program. Keep the curses part a thin layer on top of a library and write a simple driver that invokes the correct routines to recreate the error. Then you can dive in and do what's necessary. </p...
7
2010-06-01T11:44:34Z
[ "python", "exception", "interpreter", "curses", "pdb" ]
Using a debugger and curses at the same time?
2,217,109
<p>I'm calling <code>python -m pdb myapp.py</code>, when an exception fires, and I'd normally be thrown back to the pdb interpreter to investigate the problem. However this exception is being thrown after I've called through <code>curses.wrapper()</code> and entered curses mode, rendering the pdb interpreter useless. H...
8
2010-02-07T14:51:48Z
36,760,821
<p>use pyclewn</p> <p>you can use <a href="http://pyclewn.sourceforge.net" rel="nofollow">pyclewn</a> with vim. or use pdb-clone,the core of pyclewn its good ,its like gdb ,can remote debug</p>
0
2016-04-21T05:57:40Z
[ "python", "exception", "interpreter", "curses", "pdb" ]
Django URL configuration
2,217,149
<p>Assume I have 3 Models: City, Area, Entry.</p> <p>Each city has several Areas and each area can have several entries BUT for "now", there can be will be only one active Entry and it will be shown. So in logic:</p> <p>Note that each city, area, entry will be using slug variable of related model class</p> <p>Format...
0
2010-02-07T15:02:01Z
2,217,270
<p>It should probably look like something like that:</p> <pre><code>urlpatterns = patterns('', (r'^(?P&lt;city&gt;[a-z-]+)/(?P&lt;area&gt;[a-z-]+)/$', 'yourapp.views.areaview'), (r'^(?P&lt;city&gt;[a-z-]+)/(?P&lt;area&gt;[a-z-]+)/(?P&lt;entry&gt;[a-z-]+)/$', 'yourapp.views.entryview'), ) </code></pre>
1
2010-02-07T15:39:42Z
[ "python", "django", "django-urls" ]
Retrieving many-to-many relation properties using SQLAlchemy
2,217,465
<p>I have a many-to-many relationship in which the relation-table contains more columns than only the primary key. As an example, consider a slide show system in which each image could have it's own timeout, and a different timeout depending on the slideshow. A daft example, but it will have to do for the sake of illus...
0
2010-02-07T17:03:14Z
2,221,459
<p>You can define intermediate model based on show_has_image table (use composite primary key) and define relations to it. Then use <a href="http://www.sqlalchemy.org/docs/reference/ext/associationproxy.html#simplifying-association-object-relations" rel="nofollow"><code>association_proxy</code></a> to define <code>Show...
1
2010-02-08T12:18:20Z
[ "python", "sql", "sqlalchemy", "declarative", "turbogears" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
2,217,537
<p>Unfortunately, you cannot just use timedelata as the largest unit it uses is day and leap years will render you calculations invalid. Therefore, let's find number of years then adjust by one if the last year isn't full:</p> <pre><code>from datetime import date birth_date = date(1980, 5, 26) years = date.today().ye...
5
2010-02-07T17:24:15Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
2,218,654
<p>The classic gotcha in this scenario is what to do with people born on the 29th day of February. Example: you need to be aged 18 to vote, drive a car, buy alcohol, etc ... if you are born on 2004-02-29, what is the first day that you are permitted to do such things: 2022-02-28, or 2022-03-01? AFAICT, mostly the first...
3
2010-02-07T22:35:29Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
2,259,711
<pre><code>from datetime import date def calculate_age(born): today = date.today() try: birthday = born.replace(year=today.year) except ValueError: # raised when birth date is February 29 and the current year is not a leap year birthday = born.replace(year=today.year, month=born.month+1, d...
65
2010-02-14T00:39:21Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
3,101,938
<p>As I did not see the correct implementation, I recoded mine this way...</p> <pre><code> def age_in_years(from_date, to_date=datetime.date.today()): if (DEBUG): logger.debug("def age_in_years(from_date='%s', to_date='%s')" % (from_date, to_date)) if (from_date&gt;to_date): # swap when the lower bound is ...
1
2010-06-23T13:18:10Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
4,828,842
<pre><code>from datetime import date days_in_year = 365.2425 age = int((date.today() - birth_date).days / days_in_year) </code></pre> <p>In Python 3, you could perform division on <code>datetime.timedelta</code>:</p> <pre><code>from datetime import date, timedelta age = (date.today() - birth_date) // timedelta(...
12
2011-01-28T13:40:20Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
7,145,154
<p>import datetime</p> <pre><code>def age(date_of_birth): if date_of_birth &gt; datetime.date.today().replace(year = date_of_birth.year): return datetime.date.today().year - date_of_birth.year - 1 else: return datetime.date.today().year - date_of_birth.year </code></pre> <p>In your case:</p> ...
0
2011-08-22T09:00:18Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
9,388,462
<pre><code>from datetime import date def age(birthday): today = date.today() y = today.year - birthday.year if today.month &lt; birthday.month or today.month == birthday.month and today.day &lt; birthday.day: y -= 1 return y </code></pre>
5
2012-02-22T02:52:47Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
9,754,466
<p>That can be done much simpler considering that int(True) is 1 and int(False) is 0:</p> <pre><code>from datetime import date def calculate_age(born): today = date.today() return today.year - born.year - ((today.month, today.day) &lt; (born.month, born.day)) </code></pre>
123
2012-03-17T22:46:32Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
12,467,950
<p>The simplest way is using <code>python-dateutil</code></p> <pre><code>import datetime import dateutil def birthday(date): # Get the current date now = datetime.datetime.utcnow() now = now.date() # Get the difference between the current date and the birthday age = dateutil.relativedelta.relati...
7
2012-09-17T22:28:23Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
17,705,679
<p>Here is a solution to find age of a person as either years or months or days.</p> <p>Lets say a person's date of birth is <strong>2012-01-17T00:00:00</strong> Therefore, his age on <strong>2013-01-16T00:00:00</strong> will be <strong>11 months</strong></p> <p>or if he is born on <strong>2012-12-17T00:00:00</strong...
2
2013-07-17T16:48:28Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
19,461,413
<p>If you're looking to print this in a page using django templates, then the following might be enough:</p> <pre><code>{{ birth_date|timesince }} </code></pre>
1
2013-10-19T01:40:34Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
31,486,620
<p>Expanding on <a href="http://stackoverflow.com/a/9754466/65387">Danny's Solution</a>, but with all sorts of ways to report ages for younger folk (note, today is <code>datetime.date(2015,7,17)</code>):</p> <pre><code>def calculate_age(born): ''' Converts a date of birth (dob) datetime object to years, al...
0
2015-07-18T00:34:18Z
[ "python", "datetime", "date" ]
Age from birthdate in python
2,217,488
<p>How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.</p>
78
2010-02-07T17:10:02Z
37,947,842
<p>As suggested by @[Tomasz Zielinski] and @Williams <a href="http://labix.org/python-dateutil" rel="nofollow">python-dateutil</a> can do it just 5 lines.</p> <pre><code>from dateutil.relativedelta import * from datetime import date today = date.today() dob = date(1982, 7, 5) age = relativedelta(today, dob) &gt;&gt;r...
2
2016-06-21T14:55:05Z
[ "python", "datetime", "date" ]
How to convert ip address to DWORD?
2,217,612
<p>Hey, how can I convert ip address to DWORD using python ?</p> <p>I searched a while but didn't found anything useful.</p> <p>Thanks for the helpers!</p>
1
2010-02-07T17:43:35Z
2,217,631
<p>Assuming you have a correct IPv4, you could do this, for example:</p> <pre><code>import struct ip = "192.168.2.101" components = map(int, ip.split(".")) asLittleEndianDword = struct.pack("&lt;I", (components[0] &lt;&lt; 24) | (components[1] &lt;&lt; 16) | ...
0
2010-02-07T17:48:37Z
[ "python", "ip-address", "dword" ]
How to convert ip address to DWORD?
2,217,612
<p>Hey, how can I convert ip address to DWORD using python ?</p> <p>I searched a while but didn't found anything useful.</p> <p>Thanks for the helpers!</p>
1
2010-02-07T17:43:35Z
2,217,634
<p>Python doesn't have a DWORD type. If you need it as a 4-byte string use:</p> <pre><code>struct.pack('bbbb', *(int(x) for x in '127.0.0.1'.split('.'))) </code></pre>
-1
2010-02-07T17:49:14Z
[ "python", "ip-address", "dword" ]
How to convert ip address to DWORD?
2,217,612
<p>Hey, how can I convert ip address to DWORD using python ?</p> <p>I searched a while but didn't found anything useful.</p> <p>Thanks for the helpers!</p>
1
2010-02-07T17:43:35Z
2,217,743
<p>Don't roll your own solution. Use the <code>socket</code> library.</p> <pre><code>import socket socket.inet_pton(socket.AF_INET, "127.0.0.1") </code></pre> <p>It will throw exceptions when it can't properly parse the address, and writing your own parsers for things is just a recipe for problems down the line.</p>...
4
2010-02-07T18:24:29Z
[ "python", "ip-address", "dword" ]
Deploying Pylons with uWSGI
2,217,679
<p>We're trying to move our intranet to Pylons. My boss is trying to set up Pylons to use uWSGI behind Apache so he can set up multiple, independent applications. However, he's having a difficult time getting it set up, with some apparent code problems in the C source code for uWSGI. Does anyone have any suggestions fo...
5
2010-02-07T18:06:20Z
2,217,715
<p>The Pylons documentation contains <a href="http://pylonsbook.com/en/1.1/deployment.html" rel="nofollow">very detailed instructions about deployment</a>. </p> <p>Is there anything <em>specific</em> that isn't working properly?</p>
0
2010-02-07T18:17:31Z
[ "python", "apache", "deployment", "pylons", "uwsgi" ]
Deploying Pylons with uWSGI
2,217,679
<p>We're trying to move our intranet to Pylons. My boss is trying to set up Pylons to use uWSGI behind Apache so he can set up multiple, independent applications. However, he's having a difficult time getting it set up, with some apparent code problems in the C source code for uWSGI. Does anyone have any suggestions fo...
5
2010-02-07T18:06:20Z
2,218,217
<p>You can directly use paste for deploying pylons on uWSGI: </p> <p><a href="http://projects.unbit.it/uwsgi/wiki/UsePaste" rel="nofollow">http://projects.unbit.it/uwsgi/wiki/UsePaste</a></p>
3
2010-02-07T20:32:50Z
[ "python", "apache", "deployment", "pylons", "uwsgi" ]
Deploying Pylons with uWSGI
2,217,679
<p>We're trying to move our intranet to Pylons. My boss is trying to set up Pylons to use uWSGI behind Apache so he can set up multiple, independent applications. However, he's having a difficult time getting it set up, with some apparent code problems in the C source code for uWSGI. Does anyone have any suggestions fo...
5
2010-02-07T18:06:20Z
2,611,659
<p>Here is how I did it:</p> <p><a href="http://tonylandis.com/python/deployment-howt-pylons-nginx-and-uwsgi/">http://tonylandis.com/python/deployment-howt-pylons-nginx-and-uwsgi/</a></p>
6
2010-04-09T23:51:04Z
[ "python", "apache", "deployment", "pylons", "uwsgi" ]
Can anyone describe how I implement the ckeditor in django.contrib.flatpages?
2,218,064
<p>Can anyone describe how I implement the ckeditor in django.contrib.flatpages?</p>
0
2010-02-07T19:51:18Z
3,073,095
<p>Few steps to get this done. First, make sure ckeditor.js is being served up in some way from django. Info on this can be found at <a href="http://docs.djangoproject.com/en/1.2/howto/static-files/#howto-static-files" rel="nofollow">http://docs.djangoproject.com/en/1.2/howto/static-files/#howto-static-files</a>. Fo...
1
2010-06-18T21:07:24Z
[ "python", "django-admin", "ckeditor", "django-flatpages" ]
Can anyone describe how I implement the ckeditor in django.contrib.flatpages?
2,218,064
<p>Can anyone describe how I implement the ckeditor in django.contrib.flatpages?</p>
0
2010-02-07T19:51:18Z
10,458,497
<p>Found good solution:</p> <blockquote> <p>This is a little tricky. I've got admin.autodiscover() in my urls.py, so it's automatically going to create an admin for flatpages as defined in django.contrib.flatpages. I certainly don't want to go hacking apart something that came with Django, nor do I want to giv...
1
2012-05-05T03:11:10Z
[ "python", "django-admin", "ckeditor", "django-flatpages" ]
Can anyone describe how I implement the ckeditor in django.contrib.flatpages?
2,218,064
<p>Can anyone describe how I implement the ckeditor in django.contrib.flatpages?</p>
0
2010-02-07T19:51:18Z
17,498,600
<p>For a solution without template hacking check this page: <a href="http://johansdevblog.blogspot.it/2009/10/adding-ckeditor-to-django-admin.html" rel="nofollow">http://johansdevblog.blogspot.it/2009/10/adding-ckeditor-to-django-admin.html</a></p> <p>I report here the example if the link goes down. This is a simple m...
0
2013-07-06T01:03:19Z
[ "python", "django-admin", "ckeditor", "django-flatpages" ]
Python: Retrieving only POP3 message text, no headers
2,218,066
<p>I'm trying to make a Python program that retrieves only the body text of an email without passing headers or any other parameters. I'm not sure how to go about this.</p> <p>The goal is to be able to send basic commands to a program via message text.</p> <p>What I have now is this:</p> <pre><code>import poplib ho...
3
2010-02-07T19:51:48Z
2,218,116
<p>You can parse eMails using the <a href="http://docs.python.org/library/email.html" rel="nofollow">email module</a>.</p>
2
2010-02-07T20:03:21Z
[ "python", "email", "text", "pop3", "poplib" ]
Python: Retrieving only POP3 message text, no headers
2,218,066
<p>I'm trying to make a Python program that retrieves only the body text of an email without passing headers or any other parameters. I'm not sure how to go about this.</p> <p>The goal is to be able to send basic commands to a program via message text.</p> <p>What I have now is this:</p> <pre><code>import poplib ho...
3
2010-02-07T19:51:48Z
2,218,448
<p>This is a fragment of code from my own POP3 reader:</p> <pre><code> response, lines, bytes = pop.retr(m) # remove trailing blank lines from message while lines[-1]=="": del lines[-1] try: endOfHeader = lines.index('') header = lines[:endOfHeader]...
2
2010-02-07T21:33:19Z
[ "python", "email", "text", "pop3", "poplib" ]
Python: Retrieving only POP3 message text, no headers
2,218,066
<p>I'm trying to make a Python program that retrieves only the body text of an email without passing headers or any other parameters. I'm not sure how to go about this.</p> <p>The goal is to be able to send basic commands to a program via message text.</p> <p>What I have now is this:</p> <pre><code>import poplib ho...
3
2010-02-07T19:51:48Z
5,394,213
<p>I would use <strong>email</strong> module to get the body of email message with <strong>get_payload()</strong> method which skips header info.</p> <p>I added few lines to your code (they are marked with <code># new statement</code> at the end of line)</p> <pre><code>import poplib import email # new statement host...
3
2011-03-22T16:06:21Z
[ "python", "email", "text", "pop3", "poplib" ]
Sort by field in ForeignKey model
2,218,121
<p>I have created a Django model called Person, which has got a 'user' ForeignKey to django.contrib.auth.models.User</p> <p>How can I set the ordering on class Person to self.user.first_name, self.user.last_name?</p>
1
2010-02-07T20:05:35Z
2,218,265
<pre><code>class Meta: ordering = ['user__first_name', 'user__last_name'] </code></pre> <p>Should do the trick iirc</p>
3
2010-02-07T20:44:44Z
[ "python", "django" ]
Inserting values into specific locations in a list in Python
2,218,238
<p>I am trying to do print all the possible outcomes of a given list and I was wondering how to put a value into various locations in the list. For example, if my list was <code>[A,B]</code>, I want to insert <code>X</code> into all possible index of the list such that it would return this <code>[X,A,B]</code>, <code>[...
18
2010-02-07T20:38:26Z
2,218,249
<p>You could do this with the following list comprehension:</p> <pre><code>[mylist[i:] + [newelement] + mylist[:i] for i in xrange(len(mylist),-1,-1)] </code></pre> <p>With your example:</p> <pre><code>&gt;&gt;&gt; mylist=['A','B'] &gt;&gt;&gt; newelement='X' &gt;&gt;&gt; [mylist[i:] + [newelement] + mylist[:i] for ...
16
2010-02-07T20:41:17Z
[ "python", "list" ]
Inserting values into specific locations in a list in Python
2,218,238
<p>I am trying to do print all the possible outcomes of a given list and I was wondering how to put a value into various locations in the list. For example, if my list was <code>[A,B]</code>, I want to insert <code>X</code> into all possible index of the list such that it would return this <code>[X,A,B]</code>, <code>[...
18
2010-02-07T20:38:26Z
2,218,267
<p>If <code>l</code> is your list and <code>X</code> is your value:</p> <pre><code>for i in range(len(l) + 1): print l[:i] + [X] + l[i:] </code></pre>
0
2010-02-07T20:45:04Z
[ "python", "list" ]