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
Parsing fixed-format data embedded in HTML in python
409,769
<p>I am using google's appengine api</p> <pre><code>from google.appengine.api import urlfetch </code></pre> <p>to fetch a webpage. The result of</p> <pre><code>result = urlfetch.fetch("http://www.example.com/index.html") </code></pre> <p>is a string of the html content (in result.content). The problem is the data...
0
2009-01-03T20:32:20Z
410,210
<p>EDIT: Reading comprehension is a desirable thing. I missed the bit about the lines being run together with no separator between them, which would kinda be the whole point of this, wouldn't it? So, nevermind my answer, it's not actually relevant.</p> <p><hr /></p> <p>If you know that each line is 5 space-separated...
0
2009-01-04T01:08:14Z
[ "python", "html", "google-app-engine", "parsing", "html-content-extraction" ]
Parsing fixed-format data embedded in HTML in python
409,769
<p>I am using google's appengine api</p> <pre><code>from google.appengine.api import urlfetch </code></pre> <p>to fetch a webpage. The result of</p> <pre><code>result = urlfetch.fetch("http://www.example.com/index.html") </code></pre> <p>is a string of the html content (in result.content). The problem is the data...
0
2009-01-03T20:32:20Z
485,465
<p>Further suggestions for splitting the string <code>s</code> into 26-character blocks:</p> <p>As a list:</p> <pre><code>&gt;&gt;&gt; [s[x:x+26] for x in range(0, len(s), 26)] ['AAA 123 888 2008-10-30 ABC', 'BBB 987 2009-01-02 JSE', 'A4A 288 AAA'] </code></pre> <p>As a generator:</p> <pre><cod...
0
2009-01-27T21:52:46Z
[ "python", "html", "google-app-engine", "parsing", "html-content-extraction" ]
socket.shutdown vs socket.close
409,783
<p>I recently saw a bit of code that looked like this (with sock being a socket object of course):</p> <pre><code>sock.shutdown(socket.SHUT_RDWR) sock.close() </code></pre> <p>What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a difference, this socket is being used for no...
87
2009-01-03T20:37:55Z
409,843
<p>Here's one <a href="http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.progcomm/doc/progcomc/skt_shutdn.htm">explanation</a>:</p> <blockquote> <p>Once a socket is no longer required, the calling program can discard the socket by applying a close subroutine to the socket descriptor...
21
2009-01-03T21:10:08Z
[ "python", "sockets", "asynchronous" ]
socket.shutdown vs socket.close
409,783
<p>I recently saw a bit of code that looked like this (with sock being a socket object of course):</p> <pre><code>sock.shutdown(socket.SHUT_RDWR) sock.close() </code></pre> <p>What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a difference, this socket is being used for no...
87
2009-01-03T20:37:55Z
410,231
<p>Explanation of shutdown and close: <a href="http://msdn.microsoft.com/en-us/library/ms738547(VS.85).aspx">Graceful shutdown (msdn)</a></p> <p>Shutdown (in your case) indicates to the other end of the connection there is no further intention to read from or write to the socket. Then close frees up any memory associa...
16
2009-01-04T01:28:29Z
[ "python", "sockets", "asynchronous" ]
socket.shutdown vs socket.close
409,783
<p>I recently saw a bit of code that looked like this (with sock being a socket object of course):</p> <pre><code>sock.shutdown(socket.SHUT_RDWR) sock.close() </code></pre> <p>What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a difference, this socket is being used for no...
87
2009-01-03T20:37:55Z
410,271
<p>there are some flavours of shutdown: <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.shutdown.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.shutdown.aspx</a>. *nix is similar.</p>
3
2009-01-04T01:52:19Z
[ "python", "sockets", "asynchronous" ]
socket.shutdown vs socket.close
409,783
<p>I recently saw a bit of code that looked like this (with sock being a socket object of course):</p> <pre><code>sock.shutdown(socket.SHUT_RDWR) sock.close() </code></pre> <p>What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a difference, this socket is being used for no...
87
2009-01-03T20:37:55Z
598,759
<p>Calling <code>close</code> and <code>shutdown</code> have two different effects on the underlying socket.</p> <p>The first thing to point out is that the socket is a resource in the underlying OS and <strong>multiple processes can have a handle for the same underlying socket.</strong></p> <p>When you call <code>cl...
159
2009-02-28T21:58:05Z
[ "python", "sockets", "asynchronous" ]
socket.shutdown vs socket.close
409,783
<p>I recently saw a bit of code that looked like this (with sock being a socket object of course):</p> <pre><code>sock.shutdown(socket.SHUT_RDWR) sock.close() </code></pre> <p>What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a difference, this socket is being used for no...
87
2009-01-03T20:37:55Z
2,554,482
<p>Shutdown(1) , forces the socket no to send any more data</p> <p>This is usefull in</p> <p>1- Buffer flushing</p> <p>2- Strange error detection</p> <p>3- Safe guarding</p> <p>Let me explain more , when you send a data from A to B , it's not guaranteed to be sent to B , it's only guaranteed to be sent to the A o...
1
2010-03-31T16:03:46Z
[ "python", "sockets", "asynchronous" ]
socket.shutdown vs socket.close
409,783
<p>I recently saw a bit of code that looked like this (with sock being a socket object of course):</p> <pre><code>sock.shutdown(socket.SHUT_RDWR) sock.close() </code></pre> <p>What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a difference, this socket is being used for no...
87
2009-01-03T20:37:55Z
2,605,939
<p>Isn't this code above wrong?</p> <p>The close call directly after the shutdown call might make the kernel discard all outgoing buffers anyway.</p> <p>According to <a href="http://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable" rel="nofollow">http://blog.netherlabs....
2
2010-04-09T08:21:53Z
[ "python", "sockets", "asynchronous" ]
socket.shutdown vs socket.close
409,783
<p>I recently saw a bit of code that looked like this (with sock being a socket object of course):</p> <pre><code>sock.shutdown(socket.SHUT_RDWR) sock.close() </code></pre> <p>What exactly is the purpose of calling shutdown on the socket and then closing it? If it makes a difference, this socket is being used for no...
87
2009-01-03T20:37:55Z
19,009,869
<p>it's mentioned right in the Socket Programming HOWTO (<a href="http://docs.python.org/2/howto/sockets.html#disconnecting" rel="nofollow">py2</a>/<a href="http://docs.python.org/3/howto/sockets.html#disconnecting" rel="nofollow">py3</a>)</p> <blockquote> <p><strong>Disconnecting</strong></p> <p>Strictly speak...
2
2013-09-25T16:05:00Z
[ "python", "sockets", "asynchronous" ]
How do you embed album art into an MP3 using Python?
409,949
<p>I've been using mutagen for reading and writing MP3 tags, but I want to be able to embed album art directly into the file.</p>
21
2009-01-03T21:59:30Z
409,988
<p>Looks like you have to add a special type of frame to the MP3. See the site on <a href="http://www.id3.org/id3v2.3.0#head-70a65d30522ef0d37642224c2a40517ae35b7155" rel="nofollow">ID3 tags</a> </p> <p>Also the tutorial for mutagen implies that you can add ID3 tags in mutagen <a href="http://svn.sacredchao.net/svn/...
3
2009-01-03T22:20:00Z
[ "python", "mp3", "metadata", "id3", "albumart" ]
How do you embed album art into an MP3 using Python?
409,949
<p>I've been using mutagen for reading and writing MP3 tags, but I want to be able to embed album art directly into the file.</p>
21
2009-01-03T21:59:30Z
1,002,814
<p>I've used the <a href="http://eyed3.nicfit.net/">eyeD3</a> module to do this exact thing.</p> <pre><code>def update_id3(mp3_file_name, artwork_file_name, artist, item_title): #edit the ID3 tag to add the title, artist, artwork, date, and genre tag = eyeD3.Tag() tag.link(mp3_file_name) tag.setVer...
10
2009-06-16T17:13:44Z
[ "python", "mp3", "metadata", "id3", "albumart" ]
How do you embed album art into an MP3 using Python?
409,949
<p>I've been using mutagen for reading and writing MP3 tags, but I want to be able to embed album art directly into the file.</p>
21
2009-01-03T21:59:30Z
1,937,425
<p>Here is how to add example.png as album cover into example.mp3 with mutagen:</p> <pre><code>from mutagen.mp3 import MP3 from mutagen.id3 import ID3, APIC, error audio = MP3('example.mp3', ID3=ID3) # add ID3 tag if it doesn't exist try: audio.add_tags() except error: pass audio.tags.add( APIC( ...
28
2009-12-20T23:02:50Z
[ "python", "mp3", "metadata", "id3", "albumart" ]
How do you embed album art into an MP3 using Python?
409,949
<p>I've been using mutagen for reading and writing MP3 tags, but I want to be able to embed album art directly into the file.</p>
21
2009-01-03T21:59:30Z
1,961,919
<p>A nice small CLI tool which helped me a lot with checking what I did while developing id3 stuff is mid3v2 which is the mutagen version of id3v2. It comes bundled with the Python mutagen library. The source of this little tool gave me also lots of answers about how to use mutagen.</p>
0
2009-12-25T20:49:06Z
[ "python", "mp3", "metadata", "id3", "albumart" ]
How do you embed album art into an MP3 using Python?
409,949
<p>I've been using mutagen for reading and writing MP3 tags, but I want to be able to embed album art directly into the file.</p>
21
2009-01-03T21:59:30Z
3,951,901
<p><a href="http://blog.lostpaperclips.com/2010/10/16/embedding-images-in-mp3-files-redux" rel="nofollow">Possible solution</a></p> <p>Are you trying to embed images into a lot of files? If so, I found a script (see the link) that goes through a set of directories, looks for images, and the embeds them into MP3 files....
1
2010-10-17T03:31:38Z
[ "python", "mp3", "metadata", "id3", "albumart" ]
For Python programmers, is there anything equivalent to Perl's CPAN?
410,163
<p>I'm learning Python now because of the Django framework. I have been a Perl programmer for a number of years and I'm so used to Perl's tools. One of the things that I really miss is Perl's CPAN and its tools. Is there anything equivalent in Python? I would like to be able to search, install and maintain Python m...
32
2009-01-04T00:30:37Z
410,170
<p>sammy, have a look at <a href="http://pip.openplans.org/">pip</a>, which will let you do "pip install foo", and will download and install its dependencies (as long as they're on <a href="http://pypi.python.org/">PyPI</a>). There's also <a href="http://peak.telecommunity.com/DevCenter/EasyInstall">EasyInstall</a>, bu...
32
2009-01-04T00:34:17Z
[ "python", "perl" ]
For Python programmers, is there anything equivalent to Perl's CPAN?
410,163
<p>I'm learning Python now because of the Django framework. I have been a Perl programmer for a number of years and I'm so used to Perl's tools. One of the things that I really miss is Perl's CPAN and its tools. Is there anything equivalent in Python? I would like to be able to search, install and maintain Python m...
32
2009-01-04T00:30:37Z
412,211
<p>It might be useful to note that pip and easy_install both use the <a href="http://pypi.python.org/pypi">Python Package Index (PyPI)</a>, sometimes called the "Cheeseshop", to search for packages. Easy_install is currently the most universally supported, as it works with both setuptools and distutils style packaging,...
10
2009-01-05T03:32:27Z
[ "python", "perl" ]
For Python programmers, is there anything equivalent to Perl's CPAN?
410,163
<p>I'm learning Python now because of the Django framework. I have been a Perl programmer for a number of years and I'm so used to Perl's tools. One of the things that I really miss is Perl's CPAN and its tools. Is there anything equivalent in Python? I would like to be able to search, install and maintain Python m...
32
2009-01-04T00:30:37Z
653,629
<p>If you do use <code>easy_install</code>, I'd suggest installing packages by doing..</p> <pre><code>easy_install -v -Z package_name | tee date-package.log </code></pre> <p><code>-Z</code> (short for <code>--always-unzip</code>) unzips the <code>.egg</code> files to directories so you can then..</p> <pre><code>le...
2
2009-03-17T10:09:16Z
[ "python", "perl" ]
Natural/Relative days in Python
410,221
<p>I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc.</p> <p>Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more ...
31
2009-01-04T01:20:55Z
410,328
<p>Are you looking for <a href="http://jehiah.cz/archive/printing-relative-dates-in-python" rel="nofollow">something like this</a> (Printing Relative Dates in Python)?</p>
5
2009-01-04T02:52:41Z
[ "python", "datetime", "human-readable", "datetime-parsing", "humanize" ]
Natural/Relative days in Python
410,221
<p>I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc.</p> <p>Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more ...
31
2009-01-04T01:20:55Z
410,335
<p>Or you could easily adapt <a href="http://code.djangoproject.com/browser/django/trunk/django/utils/timesince.py">timesince.py</a> from Django which only has 2 other dependencies to itself: one for translation (which you might not need) and one for timezones (which can be easily adapted).</p> <p>By the way, <a href=...
7
2009-01-04T03:02:03Z
[ "python", "datetime", "human-readable", "datetime-parsing", "humanize" ]
Natural/Relative days in Python
410,221
<p>I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc.</p> <p>Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more ...
31
2009-01-04T01:20:55Z
410,482
<p>While not useful to you at this very moment, it may be so for future searchers: The babel module, which deals with all sorts of locale stuff, has a function for doing more or less what you want. Currently it's only in their trunk though, not in the latest public release (version 0.9.4). Once the functionality lands ...
16
2009-01-04T04:52:36Z
[ "python", "datetime", "human-readable", "datetime-parsing", "humanize" ]
Natural/Relative days in Python
410,221
<p>I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc.</p> <p>Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more ...
31
2009-01-04T01:20:55Z
5,164,027
<p>Twitter dates in specific are interesting because they are relative only for the first day. After 24 hours they just show the month and day. After a year they start showing the last two digits of the year. Here's a sample function that does something more akin to Twitter relative dates, though it always shows the ye...
29
2011-03-02T06:09:12Z
[ "python", "datetime", "human-readable", "datetime-parsing", "humanize" ]
Natural/Relative days in Python
410,221
<p>I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc.</p> <p>Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more ...
31
2009-01-04T01:20:55Z
30,048,850
<p>There is <a href="https://pypi.python.org/pypi/humanize/">the humanize package</a>:</p> <pre><code>&gt;&gt;&gt; import humanize &gt;&gt;&gt; import datetime &gt;&gt;&gt; humanize.naturalday(datetime.datetime.now()) 'today' &gt;&gt;&gt; humanize.naturalday(datetime.datetime.now() - datetime.timedelta(days=1)) 'yeste...
5
2015-05-05T09:17:37Z
[ "python", "datetime", "human-readable", "datetime-parsing", "humanize" ]
Implementing a buffer-like structure in Python
410,273
<p>I'm trying to write a small wsgi application which will put some objects to an external queue after each request. I want to make this in batch, ie. make the webserver put the object to a buffer-like structure in memory, and another thread and/or process for sending these objects to the queue in batch, when buffer is...
1
2009-01-04T01:53:27Z
410,284
<p>Examine <a href="https://docs.python.org/library/queue.html" rel="nofollow">https://docs.python.org/library/queue.html</a> to see if it meets your needs.</p>
7
2009-01-04T02:03:01Z
[ "python", "multithreading", "data-structures", "message-queue" ]
Implementing a buffer-like structure in Python
410,273
<p>I'm trying to write a small wsgi application which will put some objects to an external queue after each request. I want to make this in batch, ie. make the webserver put the object to a buffer-like structure in memory, and another thread and/or process for sending these objects to the queue in batch, when buffer is...
1
2009-01-04T01:53:27Z
410,321
<p>Since you write "thread and/or <em>process</em>", see also <a href="http://docs.python.org/library/multiprocessing.html#pipes-and-queues" rel="nofollow">multiprocessing.Queue and multiprocessing.JoinableQueue</a> from 2.6. Those are interprocess variants of Queue.</p>
2
2009-01-04T02:48:00Z
[ "python", "multithreading", "data-structures", "message-queue" ]
Implementing a buffer-like structure in Python
410,273
<p>I'm trying to write a small wsgi application which will put some objects to an external queue after each request. I want to make this in batch, ie. make the webserver put the object to a buffer-like structure in memory, and another thread and/or process for sending these objects to the queue in batch, when buffer is...
1
2009-01-04T01:53:27Z
417,538
<p>Use a <a href="http://docs.python.org/3.0/library/io.html#io.BufferedIOBase" rel="nofollow">buffered stream</a> if you are using python 3.0.</p>
1
2009-01-06T18:00:00Z
[ "python", "multithreading", "data-structures", "message-queue" ]
Using AD as authentication for Django
410,392
<p>I'm working on a Django-based application in a corporate environment and would like to use the existing Active Directory system for authentication of users (so they don't get yet another login/password combo). I would also like to continue to use Django's user authorization / permission system to manage user capabi...
15
2009-01-04T03:37:29Z
410,400
<p>How about that? Did you try that one?</p> <p><a href="http://www.djangosnippets.org/snippets/501/" rel="nofollow">http://www.djangosnippets.org/snippets/501/</a></p>
4
2009-01-04T03:44:56Z
[ "python", "django", "active-directory", "ldap" ]
Using AD as authentication for Django
410,392
<p>I'm working on a Django-based application in a corporate environment and would like to use the existing Active Directory system for authentication of users (so they don't get yet another login/password combo). I would also like to continue to use Django's user authorization / permission system to manage user capabi...
15
2009-01-04T03:37:29Z
411,080
<p>Here's another more recent snippet (July 2008):</p> <p><a href="http://djangosnippets.org/snippets/901/" rel="nofollow">Authentication Against Active Directory (LDAP) over SSL</a></p>
6
2009-01-04T14:42:18Z
[ "python", "django", "active-directory", "ldap" ]
Using AD as authentication for Django
410,392
<p>I'm working on a Django-based application in a corporate environment and would like to use the existing Active Directory system for authentication of users (so they don't get yet another login/password combo). I would also like to continue to use Django's user authorization / permission system to manage user capabi...
15
2009-01-04T03:37:29Z
7,914,309
<p>The link provided by Jeff indeed works though it assumes you have a you have a default group where users are added to. I simply replaced:</p> <pre><code>group=Group.objects.get(pk=1) </code></pre> <p>by</p> <pre><code>group,created=Group.objects.get_or_create(name="everyone") </code></pre> <p>If you want tighte...
4
2011-10-27T09:55:43Z
[ "python", "django", "active-directory", "ldap" ]
Using AD as authentication for Django
410,392
<p>I'm working on a Django-based application in a corporate environment and would like to use the existing Active Directory system for authentication of users (so they don't get yet another login/password combo). I would also like to continue to use Django's user authorization / permission system to manage user capabi...
15
2009-01-04T03:37:29Z
25,569,622
<p>I had the same problem, and noticed that django-auth-ldap does not support SASL at all -> plain text passwords over the connection if TSL is not available.</p> <p>Here is what i did for the problem: <a href="https://github.com/susundberg/django-auth-ldap-ad" rel="nofollow">https://github.com/susundberg/django-auth-...
0
2014-08-29T13:44:53Z
[ "python", "django", "active-directory", "ldap" ]
TKinter windows do not appear when using multiprocessing on Linux
410,469
<p>I want to spawn another process to display an error message asynchronously while the rest of the application continues. </p> <p>I'm using the <code>multiprocessing</code> module in Python 2.6 to create the process and I'm trying to display the window with <code>TKinter</code>. </p> <p>This code worked okay on Wind...
4
2009-01-04T04:36:34Z
410,587
<p>Maybe calling the shell command <code>xhost +</code> before calling your program from that same shell will work?</p> <p>I am guessing your problem lies with the X-server. </p>
0
2009-01-04T06:54:20Z
[ "python", "linux", "tkinter", "multiprocessing" ]
TKinter windows do not appear when using multiprocessing on Linux
410,469
<p>I want to spawn another process to display an error message asynchronously while the rest of the application continues. </p> <p>I'm using the <code>multiprocessing</code> module in Python 2.6 to create the process and I'm trying to display the window with <code>TKinter</code>. </p> <p>This code worked okay on Wind...
4
2009-01-04T04:36:34Z
434,207
<p>This <a href="http://pyinsci.blogspot.com/2008/09/python-processing.html" rel="nofollow">discussion</a> could be helpful.</p> <blockquote> <p>Here's some sample problems I found: </p> <ol> <li><p>While the multiprocessing module follows threading closely, it's definitely not an exact match. One example: si...
4
2009-01-12T01:43:08Z
[ "python", "linux", "tkinter", "multiprocessing" ]
what's the 5 character alphanumeric id in reddit URL?
410,485
<p>Whats the <code>7n5lu</code> in the reddit URL <code>http://www.reddit.com/r/reddit.com/comments/7n5lu/man_can_fly_if_you_watch_one_video_in_2</code></p> <p>how is it generated?</p> <p>update: @Gerald, Thanks for the code. I initially thought this is some obfuscation of the id. But, it is just doing the conversi...
12
2009-01-04T05:00:52Z
410,498
<p>That looks like a unique id for the thread. It's most likely used to find the thread in the database.</p>
0
2009-01-04T05:13:45Z
[ "python", "url", "slug", "reddit" ]
what's the 5 character alphanumeric id in reddit URL?
410,485
<p>Whats the <code>7n5lu</code> in the reddit URL <code>http://www.reddit.com/r/reddit.com/comments/7n5lu/man_can_fly_if_you_watch_one_video_in_2</code></p> <p>how is it generated?</p> <p>update: @Gerald, Thanks for the code. I initially thought this is some obfuscation of the id. But, it is just doing the conversi...
12
2009-01-04T05:00:52Z
410,504
<p>The reddit source code <a href="https://github.com/reddit/reddit">is available</a>! Here is what I found for generating that string:</p> <pre><code>def to_base(q, alphabet): if q &lt; 0: raise ValueError, "must supply a positive integer" l = len(alphabet) converted = [] while q != 0: q, r = ...
24
2009-01-04T05:21:43Z
[ "python", "url", "slug", "reddit" ]
what's the 5 character alphanumeric id in reddit URL?
410,485
<p>Whats the <code>7n5lu</code> in the reddit URL <code>http://www.reddit.com/r/reddit.com/comments/7n5lu/man_can_fly_if_you_watch_one_video_in_2</code></p> <p>how is it generated?</p> <p>update: @Gerald, Thanks for the code. I initially thought this is some obfuscation of the id. But, it is just doing the conversi...
12
2009-01-04T05:00:52Z
410,637
<p>Little remark.</p> <p>It is not sufficient for this example but usually appending to lists </p> <pre><code>a = [] for i in range(NNN): a.append(i) a.reverse() </code></pre> <p>really more efficient than inserting at head.</p> <pre><code>a = [] for i in range(NNN): a.insert(0,i) </code></pre> <p>.</p>
-1
2009-01-04T07:56:11Z
[ "python", "url", "slug", "reddit" ]
Best modules to develop a simple windowed 3D modeling application?
410,941
<p>I want to create a very basic 3D modeling tool. The application is supposed to be windowed and will need to respond to mouse click and drag events in the 3D viewport.</p> <p>I've decided on wxPython for the actual window since I'm fairly familiar with it already. However, I need to produce an OpenGL viewport that...
3
2009-01-04T12:52:23Z
411,099
<p>I'm not aware of any boxed up modules which provide that functionality, but you can take some inspiration from <a href="http://www.blender.org/" rel="nofollow">Blender 3D</a>, which has all of the features you described: its a 3D modeling tool, its written in Python, has an OpenGL viewport which responds to mouse ev...
1
2009-01-04T15:00:04Z
[ "python", "opengl", "3d", "wxpython" ]
Best modules to develop a simple windowed 3D modeling application?
410,941
<p>I want to create a very basic 3D modeling tool. The application is supposed to be windowed and will need to respond to mouse click and drag events in the 3D viewport.</p> <p>I've decided on wxPython for the actual window since I'm fairly familiar with it already. However, I need to produce an OpenGL viewport that...
3
2009-01-04T12:52:23Z
411,163
<p>Any reason you wouldn't use wx's <a href="http://wiki.wxpython.org/GLCanvas" rel="nofollow">GLCanvas</a>? <a href="http://code.activestate.com/recipes/325392/" rel="nofollow">Here's</a> an example that draws a sphere.</p>
3
2009-01-04T15:44:54Z
[ "python", "opengl", "3d", "wxpython" ]
Best modules to develop a simple windowed 3D modeling application?
410,941
<p>I want to create a very basic 3D modeling tool. The application is supposed to be windowed and will need to respond to mouse click and drag events in the 3D viewport.</p> <p>I've decided on wxPython for the actual window since I'm fairly familiar with it already. However, I need to produce an OpenGL viewport that...
3
2009-01-04T12:52:23Z
411,174
<p>As a very basic 3D modelling tool I'd recommend <a href="http://stackoverflow.com/questions/3088/best-ways-to-teach-a-beginner-to-program#50351">VPython</a>.</p>
2
2009-01-04T15:54:16Z
[ "python", "opengl", "3d", "wxpython" ]
How do I parse XML from a Google app engine app?
410,954
<p>How do I parse XML from a Google app engine app? Any examples?</p>
14
2009-01-04T13:05:41Z
410,962
<p>AFAIK Google App Engine provides a fairly complete Python environment for you to use. Since Python comes with "batteries included" you may want to evaluate the different APIs which vanilla Python offers you: <a href="http://docs.python.org/library/markup.html" rel="nofollow">http://docs.python.org/library/markup.htm...
4
2009-01-04T13:14:25Z
[ "python", "xml", "google-app-engine", "parsing" ]
How do I parse XML from a Google app engine app?
410,954
<p>How do I parse XML from a Google app engine app? Any examples?</p>
14
2009-01-04T13:05:41Z
410,971
<p>Take a look at <a href="http://stackoverflow.com/search?q=iterparse">existing answers on XML and Python</a>.</p> <p>Something like this could work:</p> <pre><code>from cStringIO import StringIO from xml.etree import cElementTree as etree xml = "&lt;a&gt;aaa&lt;b&gt;bbb&lt;/b&gt;&lt;/a&gt;" for event, elem in...
8
2009-01-04T13:27:43Z
[ "python", "xml", "google-app-engine", "parsing" ]
How do I parse XML from a Google app engine app?
410,954
<p>How do I parse XML from a Google app engine app? Any examples?</p>
14
2009-01-04T13:05:41Z
711,167
<p>Since the question was asked, Google has whitelisted pyexpat, which includes minidom, so you can use the following code without having to upload any libraries:</p> <pre><code>from xml.dom import minidom dom = minidom.parseString('&lt;eg&gt;example text&lt;/eg&gt;') </code></pre> <p>More information: <a href="http...
20
2009-04-02T19:09:49Z
[ "python", "xml", "google-app-engine", "parsing" ]
gtk.Builder, container subclass and binding child widgets
411,708
<p>I'm trying to use custom container widgets in gtk.Builder definition files. As far as instantiating those widgets, it works great:</p> <pre><code>#!/usr/bin/env python import sys import gtk class MyDialog(gtk.Dialog): __gtype_name__ = "MyDialog" if __name__ == "__main__": builder = gtk.Builder() b...
4
2009-01-04T21:30:01Z
412,097
<p>Alright, I guess I answered my own question.</p> <p>One way to do the above is to override gtk.Buildable's parser_finished(), which gives access to the builder object that created the class instance itself. The method is called after entire .xml file has been loaded, so all of the additional widgets we may want to ...
5
2009-01-05T01:56:06Z
[ "python", "gtk", "bind", "subclass", "gtkbuilder" ]
Variable number of inputs with Django forms possible?
411,761
<p>Is it possible to have a variable number of fields using django forms?</p> <p>The specific application is this:</p> <p>A user can upload as many pictures as they want on the image upload form. Once the pictures are uploaded they are taken to a page where they can give the pictures a name and description. The numbe...
17
2009-01-04T22:00:33Z
411,852
<p>If you run</p> <pre><code>python manage.py shell </code></pre> <p>and type:</p> <pre><code>from app.forms import PictureForm p = PictureForm() p.fields type(p.fields) </code></pre> <p>you'll see that p.fields is a SortedDict. you just have to insert a new field. Something like</p> <pre><code>p.fields.insert(len...
7
2009-01-04T22:57:03Z
[ "python", "django", "django-forms" ]
Variable number of inputs with Django forms possible?
411,761
<p>Is it possible to have a variable number of fields using django forms?</p> <p>The specific application is this:</p> <p>A user can upload as many pictures as they want on the image upload form. Once the pictures are uploaded they are taken to a page where they can give the pictures a name and description. The numbe...
17
2009-01-04T22:00:33Z
411,862
<p>Use either multiple forms (django.forms.Form not the tag)</p> <pre><code>class Foo(forms.Form): field = forms.Charfield() forms = [Foo(prefix=i) for i in xrange(x)] </code></pre> <p>or add multiple fields to the form dynamically using self.fields.</p> <pre><code>class Bar(forms.Form): def __init__(self,...
7
2009-01-04T23:00:48Z
[ "python", "django", "django-forms" ]
Variable number of inputs with Django forms possible?
411,761
<p>Is it possible to have a variable number of fields using django forms?</p> <p>The specific application is this:</p> <p>A user can upload as many pictures as they want on the image upload form. Once the pictures are uploaded they are taken to a page where they can give the pictures a name and description. The numbe...
17
2009-01-04T22:00:33Z
412,149
<p>Yes, it's possible to create forms dynamically in Django. You can even mix and match dynamic fields with normal fields.</p> <pre><code>class EligibilityForm(forms.Form): def __init__(self, *args, **kwargs): super(EligibilityForm, self).__init__(*args, **kwargs) # dynamic fields here ... ...
13
2009-01-05T02:35:12Z
[ "python", "django", "django-forms" ]
How do I retrieve a Django model class dynamically?
411,810
<p>Without having the full module path of a Django model, is it possible to do something like:</p> <pre><code>model = 'User' [in Django namespace] model.objects.all() </code></pre> <p>...as opposed to:</p> <pre><code>User.objects.all(). </code></pre> <p>EDIT: I am trying to make this call based on command-line inpu...
15
2009-01-04T22:32:50Z
411,814
<pre><code>from django.authx.models import User model = User model.objects.all() </code></pre>
3
2009-01-04T22:34:45Z
[ "python", "django", "django-models", "django-queryset" ]
How do I retrieve a Django model class dynamically?
411,810
<p>Without having the full module path of a Django model, is it possible to do something like:</p> <pre><code>model = 'User' [in Django namespace] model.objects.all() </code></pre> <p>...as opposed to:</p> <pre><code>User.objects.all(). </code></pre> <p>EDIT: I am trying to make this call based on command-line inpu...
15
2009-01-04T22:32:50Z
411,822
<p>If you have the model name passed as a string I guess one way could be</p> <pre><code>modelname = "User" model = globals()[modelname] </code></pre> <p>But mucking about with globals() might be a bit dangerous in some contexts. So handle with care :)</p>
-2
2009-01-04T22:40:44Z
[ "python", "django", "django-models", "django-queryset" ]
How do I retrieve a Django model class dynamically?
411,810
<p>Without having the full module path of a Django model, is it possible to do something like:</p> <pre><code>model = 'User' [in Django namespace] model.objects.all() </code></pre> <p>...as opposed to:</p> <pre><code>User.objects.all(). </code></pre> <p>EDIT: I am trying to make this call based on command-line inpu...
15
2009-01-04T22:32:50Z
411,880
<p>I think you're looking for this:</p> <pre><code>from django.db.models.loading import get_model model = get_model('app_name', 'model_name') </code></pre> <p>There are other methods, of course, but this is the way I'd handle it if you don't know what models file you need to import into your namespace. (Note there's...
32
2009-01-04T23:11:28Z
[ "python", "django", "django-models", "django-queryset" ]
How do I retrieve a Django model class dynamically?
411,810
<p>Without having the full module path of a Django model, is it possible to do something like:</p> <pre><code>model = 'User' [in Django namespace] model.objects.all() </code></pre> <p>...as opposed to:</p> <pre><code>User.objects.all(). </code></pre> <p>EDIT: I am trying to make this call based on command-line inpu...
15
2009-01-04T22:32:50Z
411,925
<blockquote> <p>model = django.authx.models.User</p> <p>? Django returns an error, "global name django is not defined."</p> </blockquote> <p>Django does not return the error. Python does.</p> <p>First, you MUST import the model. You must import it with</p> <pre><code>from django.authx.models import User <...
1
2009-01-04T23:42:46Z
[ "python", "django", "django-models", "django-queryset" ]
How do I retrieve a Django model class dynamically?
411,810
<p>Without having the full module path of a Django model, is it possible to do something like:</p> <pre><code>model = 'User' [in Django namespace] model.objects.all() </code></pre> <p>...as opposed to:</p> <pre><code>User.objects.all(). </code></pre> <p>EDIT: I am trying to make this call based on command-line inpu...
15
2009-01-04T22:32:50Z
411,927
<p>Classes are "first class" objects in Python, meaning they can be passed around and manipulated just like all other objects.</p> <p>Models are classes -- you can tell from the fact that you create new models using class statements:</p> <pre><code>class Person(models.Model): last_name = models.CharField(max_leng...
0
2009-01-04T23:43:50Z
[ "python", "django", "django-models", "django-queryset" ]
How do I retrieve a Django model class dynamically?
411,810
<p>Without having the full module path of a Django model, is it possible to do something like:</p> <pre><code>model = 'User' [in Django namespace] model.objects.all() </code></pre> <p>...as opposed to:</p> <pre><code>User.objects.all(). </code></pre> <p>EDIT: I am trying to make this call based on command-line inpu...
15
2009-01-04T22:32:50Z
28,380,435
<p>For Django 1.7+, there is an <a href="https://docs.djangoproject.com/en/1.8/ref/applications/#module-django.apps">applications registry</a>. You can use the <a href="https://docs.djangoproject.com/en/1.8/ref/applications/#django.apps.apps.get_model"><code>apps.get_model</code></a> method to dynamically get a model.<...
11
2015-02-07T09:19:49Z
[ "python", "django", "django-models", "django-queryset" ]
Django file upload failing occasionally
411,902
<p>I am trying to port my first Django 1.0.2 application to run on OSX/Leopard with Apache + mod_python 3.3.1 + python 2.6.1 (all running in 64-bit mode) and I am experiencing an occasional error when uploading a file that was not present when testing with the Django development server. </p> <p>The code for the upload...
8
2009-01-04T23:28:35Z
411,971
<p>How large is the file? It may take long enough to upload that the upload script times out, so try increasing the execution time for that script.</p>
0
2009-01-05T00:21:48Z
[ "python", "django", "apache" ]
Django file upload failing occasionally
411,902
<p>I am trying to port my first Django 1.0.2 application to run on OSX/Leopard with Apache + mod_python 3.3.1 + python 2.6.1 (all running in 64-bit mode) and I am experiencing an occasional error when uploading a file that was not present when testing with the Django development server. </p> <p>The code for the upload...
8
2009-01-04T23:28:35Z
413,089
<p>I would chase down the exception value </p> <pre><code>Client read error (Timeout?) </code></pre> <p>this seems odd enough. Try reading <a href="http://groups.google.com/group/django-users/browse_thread/thread/03ff1503377a8bfc" rel="nofollow">this thread</a></p>
1
2009-01-05T13:28:23Z
[ "python", "django", "apache" ]
Django file upload failing occasionally
411,902
<p>I am trying to port my first Django 1.0.2 application to run on OSX/Leopard with Apache + mod_python 3.3.1 + python 2.6.1 (all running in 64-bit mode) and I am experiencing an occasional error when uploading a file that was not present when testing with the Django development server. </p> <p>The code for the upload...
8
2009-01-04T23:28:35Z
416,189
<p>Using mod_wsgi made the problem go away for Firefox. </p> <p>Limiting my research to an interaction problem between Apache and Safari, I stumbled upon this bug report for Apache <a href="https://bugs.webkit.org/show_bug.cgi?id=5760">https://bugs.webkit.org/show_bug.cgi?id=5760</a> that describes something very simi...
8
2009-01-06T11:33:53Z
[ "python", "django", "apache" ]
Django file upload failing occasionally
411,902
<p>I am trying to port my first Django 1.0.2 application to run on OSX/Leopard with Apache + mod_python 3.3.1 + python 2.6.1 (all running in 64-bit mode) and I am experiencing an occasional error when uploading a file that was not present when testing with the Django development server. </p> <p>The code for the upload...
8
2009-01-04T23:28:35Z
2,465,744
<p>Long time unanswered here, but having experienced this myself and tried a few things to resolve it. It seems in my case, it happened if someone canceled a download (or lost connection) during an upload.</p> <p>You can try this yourself to see if this is what is causing the "IOError: request data read error" for you...
0
2010-03-17T20:43:57Z
[ "python", "django", "apache" ]
How to bring program to front using python
412,214
<p>I would like to force my python app to the front if a condition occurs. I'm using Kubuntu &amp; QT3.1</p> <p>I've tried setActiveWindow(), but it only flashes the task bar in KDE.</p> <p>I think Windows has a function bringwindowtofront() for VB.</p> <p>Is there something similar for KDE?</p>
1
2009-01-05T03:36:21Z
412,294
<p>Have you tried using those 3 (in this order) on your window instead of only <code>setActiveWindow</code>?</p> <pre><code>show() raise() # this might be raiseW() in Python setActiveWindow() </code></pre>
1
2009-01-05T04:46:47Z
[ "python", "qt" ]
How to bring program to front using python
412,214
<p>I would like to force my python app to the front if a condition occurs. I'm using Kubuntu &amp; QT3.1</p> <p>I've tried setActiveWindow(), but it only flashes the task bar in KDE.</p> <p>I think Windows has a function bringwindowtofront() for VB.</p> <p>Is there something similar for KDE?</p>
1
2009-01-05T03:36:21Z
413,073
<p>Check if KWin is configured to prevent focus stealing.</p> <p>There might be nothing wrong with your code -- but we linux people don't like applications bugging us when we work, so stealing focus is kinda frowned upon, and difficult under some window managers.</p>
4
2009-01-05T13:23:31Z
[ "python", "qt" ]
How to bring program to front using python
412,214
<p>I would like to force my python app to the front if a condition occurs. I'm using Kubuntu &amp; QT3.1</p> <p>I've tried setActiveWindow(), but it only flashes the task bar in KDE.</p> <p>I think Windows has a function bringwindowtofront() for VB.</p> <p>Is there something similar for KDE?</p>
1
2009-01-05T03:36:21Z
415,183
<p>It works!</p> <pre><code>show() raiseW() setActiveWindow() #in that sequence </code></pre> <p><em>plus</em> KWin config change to force focus steal prevention.</p> <p>Thanks for the help.</p>
1
2009-01-06T02:13:16Z
[ "python", "qt" ]
How to (simply) connect Python to my web site?
412,368
<p>I've been playing with Python for a while and wrote a little program to make a database to keep track of some info (its really basic, and hand written). I want to add the ability to create a website from the data that I will then pass to my special little place on the internet. What should I use to build up the webs...
3
2009-01-05T05:57:25Z
412,371
<p>I would generate a page or two of HTML using a template engine (<a href="http://jinja.pocoo.org/2/" rel="nofollow">Jinja</a> is my personal choice) and just stick them in your <code>public_html</code> directory or wherever the webserver's root is.</p>
11
2009-01-05T06:00:41Z
[ "python", "website" ]
How to (simply) connect Python to my web site?
412,368
<p>I've been playing with Python for a while and wrote a little program to make a database to keep track of some info (its really basic, and hand written). I want to add the ability to create a website from the data that I will then pass to my special little place on the internet. What should I use to build up the webs...
3
2009-01-05T05:57:25Z
415,785
<p>Generating static HTML is great, if that works for you go for it. </p> <p>If you want a dynamic website and the ability to update, <a href="http://webpy.org/" rel="nofollow">web.py</a> might work for you. Simpler than Django and stand-alone (although just about everything I start in web.py eventually gets rewritten...
0
2009-01-06T08:08:27Z
[ "python", "website" ]
Using mocking to test derived classes in Python
412,472
<p>I have code that looks like this:</p> <pre><code>import xmlrpclib class Base(object): def __init__(self, server): self.conn = xmlrpclib.ServerProxy(server) def foo(self): return self.conn.do_something() class Derived(Base): def foo(self): if Base.foo(): return self...
4
2009-01-05T07:16:34Z
412,580
<p>You could create a fake ServerProxy class, and substitute that for testing.</p> <p>Something like this:</p> <pre><code>class FakeServerProxy(object): def __init__(self, server): self.server = server def do_something(self): pass def do_something_else(self): pass def test_derived...
4
2009-01-05T08:33:05Z
[ "python", "mocking" ]
Using mocking to test derived classes in Python
412,472
<p>I have code that looks like this:</p> <pre><code>import xmlrpclib class Base(object): def __init__(self, server): self.conn = xmlrpclib.ServerProxy(server) def foo(self): return self.conn.do_something() class Derived(Base): def foo(self): if Base.foo(): return self...
4
2009-01-05T07:16:34Z
918,439
<p>With some trivial refactoring (call to <code>do_something_else</code> is extracted), you can test <code>Derived.foo</code> logic without needing to "know" about XMLRPC.</p> <pre><code>import xmlrpclib class Base(object): def __init__(self, server): self.conn = xmlrpclib.ServerProxy(server) def foo...
3
2009-05-27T23:14:50Z
[ "python", "mocking" ]
How to construct notes from a given song file?
412,475
<p>Are there any modules in Python that help us to construct or obtain the musical notes and octaves from a given original song?</p> <p>Thanks for the help</p>
2
2009-01-05T07:17:31Z
412,505
<p>This question has far too few details to give any kind of meaningful answers.</p> <p>Questions:</p> <ul> <li>By song file, do you mean like an MP3?</li> <li>Is it a "song" or a "instrumental"? I would gather trying to decipher notes behind a voice would be harder</li> <li>Is it a simple song, like a single voice p...
2
2009-01-05T07:34:15Z
[ "python" ]
How to construct notes from a given song file?
412,475
<p>Are there any modules in Python that help us to construct or obtain the musical notes and octaves from a given original song?</p> <p>Thanks for the help</p>
2
2009-01-05T07:17:31Z
412,532
<p>As lassevk mentioned, this is a complex topic - a bit like reconstructing C code from assembly, in a way. That being said, a nice framework to play with audio stuff is CLAM:</p> <p><a href="http://clam.iua.upf.edu/" rel="nofollow">http://clam.iua.upf.edu/</a></p> <p>It is an open source C++ framework for music/aud...
3
2009-01-05T07:55:53Z
[ "python" ]
How to construct notes from a given song file?
412,475
<p>Are there any modules in Python that help us to construct or obtain the musical notes and octaves from a given original song?</p> <p>Thanks for the help</p>
2
2009-01-05T07:17:31Z
412,555
<p>I think what you are interested in is still topic of research. You won't find any module ready that will do that for you.</p> <p>Besides it is not clear what you mean with "notes and octaves"? What information exaclty would you like to extract?</p>
2
2009-01-05T08:15:43Z
[ "python" ]
How to construct notes from a given song file?
412,475
<p>Are there any modules in Python that help us to construct or obtain the musical notes and octaves from a given original song?</p> <p>Thanks for the help</p>
2
2009-01-05T07:17:31Z
412,803
<p>There are some libraries for Python. Start with this FSF listing for <a href="http://directory.fsf.org/category/audmisc/" rel="nofollow">Audio Misc</a>.</p> <p>There are, however, excellent products: See <a href="http://www.seventhstring.com/xscribe/overview.html" rel="nofollow">Transcribe!</a> and <a href="http:/...
0
2009-01-05T11:01:33Z
[ "python" ]
How to construct notes from a given song file?
412,475
<p>Are there any modules in Python that help us to construct or obtain the musical notes and octaves from a given original song?</p> <p>Thanks for the help</p>
2
2009-01-05T07:17:31Z
12,613,852
<p>Jordi Bartolomé Guillen's "audioSearch.transcriber" module included in <a href="http://web.mit.edu/music21/" rel="nofollow">music21</a> gives pretty accurate transcriptions from monophonic sound files, so if you're working with a solo piece (or one where the melody is much louder than the accompaniment), it's a use...
0
2012-09-27T03:56:52Z
[ "python" ]
How to access to the root path in a mod_python directory?
412,498
<p>In my Apache webserver I put this:</p> <pre><code>&lt;Directory /var/www/MYDOMAIN.com/htdocs&gt; SetHandler mod_python PythonHandler mod_python.publisher PythonDebug On &lt;/Directory&gt; </code></pre> <p>Then I have a handler.py file with an index function.</p> <p>When I go to MYDOMAIN.com/handler.py...
1
2009-01-05T07:30:03Z
412,567
<p>Try creating an empty file called index (or whatever) in the directory and then use</p> <pre><code>DirectoryIndex index </code></pre> <p>Seems like DirectoryIndex checking is done watching the filesystem.</p>
0
2009-01-05T08:24:49Z
[ "python", "mod-python", "publisher" ]
How to access to the root path in a mod_python directory?
412,498
<p>In my Apache webserver I put this:</p> <pre><code>&lt;Directory /var/www/MYDOMAIN.com/htdocs&gt; SetHandler mod_python PythonHandler mod_python.publisher PythonDebug On &lt;/Directory&gt; </code></pre> <p>Then I have a handler.py file with an index function.</p> <p>When I go to MYDOMAIN.com/handler.py...
1
2009-01-05T07:30:03Z
412,780
<p>Yes, but you need to create your own handler. You currently use publisher, it just checks the URI and loads given python module.</p> <p>To create your own handler you need to create a module like this (just a minimalistic example):</p> <pre><code>from mod_python import apache def requesthandler(req): req.cont...
2
2009-01-05T10:51:55Z
[ "python", "mod-python", "publisher" ]
Apps Similar to Nodebox?
412,775
<p>I'm looking for apps/environments similar to <a href="http://nodebox.net/code/index.php/Tutorial" rel="nofollow">Nodebox</a>. Nodebox is so cool and I want to know if there were other similar apps out there. </p> <p>They don't have to be graphics-related; I'm interested in software that uses programming languages i...
3
2009-01-05T10:48:05Z
412,778
<p><a href="http://processing.org/">http://processing.org/</a> is a language for creating graphics and animations similar to Nodebox.</p>
5
2009-01-05T10:51:44Z
[ "python", "nodebox" ]
Apps Similar to Nodebox?
412,775
<p>I'm looking for apps/environments similar to <a href="http://nodebox.net/code/index.php/Tutorial" rel="nofollow">Nodebox</a>. Nodebox is so cool and I want to know if there were other similar apps out there. </p> <p>They don't have to be graphics-related; I'm interested in software that uses programming languages i...
3
2009-01-05T10:48:05Z
413,006
<p>it was referenced on @Jonas processing.org, but <a href="http://www.alice.org/" rel="nofollow">alice.org</a> is interesting.</p>
0
2009-01-05T12:47:00Z
[ "python", "nodebox" ]
Algorithm to keep a list of percentages to add up to 100%
412,943
<p>(code examples are python)<br /> Lets assume we have a list of percentages that add up to 100: </p> <pre><code>mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0] </code></pre> <p>Some values of mylist may be changed, others must stay fixed.<br /> Lets assume the first 3 (2.0, 7.0, 12.0) must stay fixed and the last th...
4
2009-01-05T12:18:09Z
412,966
<p>you're being silly.</p> <p>let's say you want to add 4.0 to the list. You don't need to subtract a certain amount from each one. What you need to do is multiply each item.</p> <p>100 - 4 = 96. therefore, multiply each item by 0.96</p> <p>you want to add 20.0 as an item. so then you multiply each item by 0.8, whic...
3
2009-01-05T12:29:19Z
[ "python", "algorithm" ]
Algorithm to keep a list of percentages to add up to 100%
412,943
<p>(code examples are python)<br /> Lets assume we have a list of percentages that add up to 100: </p> <pre><code>mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0] </code></pre> <p>Some values of mylist may be changed, others must stay fixed.<br /> Lets assume the first 3 (2.0, 7.0, 12.0) must stay fixed and the last th...
4
2009-01-05T12:18:09Z
412,979
<p>How's this?</p> <pre><code>def adjustAppend( v, n ): weight= -n/sum(v) return [ i+i*weight for i in v ] + [n] </code></pre> <p>Given a list of numbers <em>v</em>, append a new number, <em>n</em>. Weight the existing number to keep the sum the same.</p> <pre><code> sum(v) == sum( v + [n] ) </code></pre> <...
9
2009-01-05T12:33:25Z
[ "python", "algorithm" ]
Algorithm to keep a list of percentages to add up to 100%
412,943
<p>(code examples are python)<br /> Lets assume we have a list of percentages that add up to 100: </p> <pre><code>mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0] </code></pre> <p>Some values of mylist may be changed, others must stay fixed.<br /> Lets assume the first 3 (2.0, 7.0, 12.0) must stay fixed and the last th...
4
2009-01-05T12:18:09Z
412,980
<pre><code>NEW_NUMBER = 4.0 mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0] fix = mylist[:3] vari = mylist[3:] weight = (100 - sum(fix) - NEW_NUMBER)/sum(vari) adjusted = [] adjusted.extend( (weight*v for v in vari) ) adjusted.extend(fix) adjusted.append(NEW_NUMBER) print sum(adjusted) # 100.0 </code></p...
1
2009-01-05T12:34:44Z
[ "python", "algorithm" ]
Algorithm to keep a list of percentages to add up to 100%
412,943
<p>(code examples are python)<br /> Lets assume we have a list of percentages that add up to 100: </p> <pre><code>mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0] </code></pre> <p>Some values of mylist may be changed, others must stay fixed.<br /> Lets assume the first 3 (2.0, 7.0, 12.0) must stay fixed and the last th...
4
2009-01-05T12:18:09Z
412,984
<pre><code>new_item = 4.0 CERTAIN_PERCENTAGE = 100 * (float(new_item) / sum(vari)) </code></pre>
2
2009-01-05T12:35:43Z
[ "python", "algorithm" ]
How to implement property() with dynamic name (in python)
412,951
<p>I am programming a simulations for single neurons. Therefore I have to handle a lot of Parameters. Now the Idea is that I have two classes, one for a SingleParameter and a Collection of parameters. I use property() to access the parameter value easy and to make the code more readable. This works perfect for a sinlge...
9
2009-01-05T12:19:45Z
412,997
<p>Look at built-in functions <code>getattr</code> and <code>setattr</code>. You'll probably be a lot happier.</p>
10
2009-01-05T12:42:31Z
[ "python", "oop", "parameters", "properties" ]
How to implement property() with dynamic name (in python)
412,951
<p>I am programming a simulations for single neurons. Therefore I have to handle a lot of Parameters. Now the Idea is that I have two classes, one for a SingleParameter and a Collection of parameters. I use property() to access the parameter value easy and to make the code more readable. This works perfect for a sinlge...
9
2009-01-05T12:19:45Z
413,010
<p>Properties are meant to dynamically evaluate attributes or to make them read-only. What you need is customizing attribute access. <code>__getattr__</code> and <code>__setattr__</code> do that really fine, and there's also <code>__getattribute__</code> if <code>__getattr__</code> is not enough.</p> <p>See <a href="h...
5
2009-01-05T12:48:29Z
[ "python", "oop", "parameters", "properties" ]
How to implement property() with dynamic name (in python)
412,951
<p>I am programming a simulations for single neurons. Therefore I have to handle a lot of Parameters. Now the Idea is that I have two classes, one for a SingleParameter and a Collection of parameters. I use property() to access the parameter value easy and to make the code more readable. This works perfect for a sinlge...
9
2009-01-05T12:19:45Z
413,159
<p>Have you looked at the <a href="http://code.enthought.com/projects/traits/" rel="nofollow">traits package</a>? It seems that you are reinventing the wheel here with your parameter classes. Traits also have additional features that might be useful for your type of application (incidently I know a person that happily ...
3
2009-01-05T13:56:58Z
[ "python", "oop", "parameters", "properties" ]
How to implement property() with dynamic name (in python)
412,951
<p>I am programming a simulations for single neurons. Therefore I have to handle a lot of Parameters. Now the Idea is that I have two classes, one for a SingleParameter and a Collection of parameters. I use property() to access the parameter value easy and to make the code more readable. This works perfect for a sinlge...
9
2009-01-05T12:19:45Z
413,192
<p>Now I implemented a solution with set-/getattr:</p> <pre><code>class Collection(object): ... def __setattr__(self, name, value): if 'dict' in self.__dict__: if name in self.dict: self[name].v = value else: self.__dict__[name] = value def __getattr__(self, name): return self[name]...
2
2009-01-05T14:10:26Z
[ "python", "oop", "parameters", "properties" ]
How to implement property() with dynamic name (in python)
412,951
<p>I am programming a simulations for single neurons. Therefore I have to handle a lot of Parameters. Now the Idea is that I have two classes, one for a SingleParameter and a Collection of parameters. I use property() to access the parameter value easy and to make the code more readable. This works perfect for a sinlge...
9
2009-01-05T12:19:45Z
439,037
<p>Finally I succeded to implement the classes with property(). Thanks a lot for the advice. It took me quite a bit to work it out - but I can promise you that this exercise helps you to understand better pythons OOP.</p> <p>I implemented it also with __getattr__ and __setattr__ but still don't know the advantages and...
0
2009-01-13T14:01:20Z
[ "python", "oop", "parameters", "properties" ]
How to implement property() with dynamic name (in python)
412,951
<p>I am programming a simulations for single neurons. Therefore I have to handle a lot of Parameters. Now the Idea is that I have two classes, one for a SingleParameter and a Collection of parameters. I use property() to access the parameter value easy and to make the code more readable. This works perfect for a sinlge...
9
2009-01-05T12:19:45Z
439,708
<p>Using the same get/set functions for both classes forces you into an ugly hack with the argument list. Very sketchy, this is how I would do it:</p> <p>In class SingleParameter, define get and set as usual:</p> <pre><code>def get(self): return self._s def set(self, value): self._s = value </code></pre> <p>In c...
7
2009-01-13T16:33:49Z
[ "python", "oop", "parameters", "properties" ]
How to implement property() with dynamic name (in python)
412,951
<p>I am programming a simulations for single neurons. Therefore I have to handle a lot of Parameters. Now the Idea is that I have two classes, one for a SingleParameter and a Collection of parameters. I use property() to access the parameter value easy and to make the code more readable. This works perfect for a sinlge...
9
2009-01-05T12:19:45Z
15,555,887
<p>I have a class that does something similar, but I did the following in the collection object:</p> <p>setattr(self, par.name, par.v)</p>
0
2013-03-21T18:57:09Z
[ "python", "oop", "parameters", "properties" ]
Django and units conversion
413,446
<p>I need to store some values in the database, distance, weight etc. In my model, i have field that contains quantity of something and IntegerField whith choices option, that determines what this quantity means(length, time duration etc). Should i create model for units and physical quantity or should i use IntegerFie...
1
2009-01-05T15:27:37Z
413,549
<p>It depends on how you want to use it. Let's say you have length value and two possible units, cm and mm. If you want only to print the value later, you can always print it as <em>value</em>&nbsp;<em>unit</em>.</p> <p>However, if you want to do some calculations with the value, for instance, calculate the area, you ...
1
2009-01-05T15:55:33Z
[ "python", "django" ]
Django and units conversion
413,446
<p>I need to store some values in the database, distance, weight etc. In my model, i have field that contains quantity of something and IntegerField whith choices option, that determines what this quantity means(length, time duration etc). Should i create model for units and physical quantity or should i use IntegerFie...
1
2009-01-05T15:27:37Z
413,600
<p>By "field(enum)" do you mean you are using the <a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#choices" rel="nofollow">choices</a> option on a field? </p> <p>A simple set of choices works out reasonably well for small lists of conversions. It allows you to make simplifying assumptions that helps ...
4
2009-01-05T16:07:26Z
[ "python", "django" ]
Django and units conversion
413,446
<p>I need to store some values in the database, distance, weight etc. In my model, i have field that contains quantity of something and IntegerField whith choices option, that determines what this quantity means(length, time duration etc). Should i create model for units and physical quantity or should i use IntegerFie...
1
2009-01-05T15:27:37Z
413,615
<p>Use a field that indicates the type of measure (weight, length, etc.), and store the value in another field. That should be sufficient. The unit of measure should be implicit. I'm assuming you are using the same unit of measure for each measure type, for example always meters for length.</p> <p>A concrete example: ...
1
2009-01-05T16:10:17Z
[ "python", "django" ]
Is there a Python library than can simulate network traffic from different addresses
414,025
<p>Is there a python library out there than can allow me to send UDP packets to a machine (sending to localhost is ok) from different source addresses and ports? I remember that one existed, but can't find it anymore.</p>
16
2009-01-05T18:20:39Z
414,078
<p>You can spoof an IP address using <a href="http://www.secdev.org/projects/scapy/">Scapy</a> library.</p> <p>Here's an example from <a href="http://web.archive.org/web/20120401161821/http://packetstorm.linuxsecurity.com/papers/general/blackmagic.txt">Packet Wizardry: Ruling the Network with Python</a>:</p> <pre><co...
17
2009-01-05T18:44:27Z
[ "python", "networking" ]
python "'NoneType' object has no attribute 'encode'"
414,230
<p>I wrote this tiny Python snippet that scrapes a feed and prints it out. When I run the code, something in the feed triggers the error message you see here as my question. Here's the complete console output on error:</p> <pre><code>&gt; Traceback (most recent call last): &gt; File "/home/vijay/ffour/ffour5.py", &g...
5
2009-01-05T19:35:09Z
414,239
<blockquote> <pre><code>&gt; sys.stdout.write(entry["title"]).encode('utf-8') </code></pre> </blockquote> <p>This is the culprit. You probably mean:</p> <pre><code>sys.stdout.write(entry["title"].encode('utf-8')) </code></pre> <p>(Notice the position of the last closing bracket.)</p>
12
2009-01-05T19:39:27Z
[ "python", "urlencode" ]
python "'NoneType' object has no attribute 'encode'"
414,230
<p>I wrote this tiny Python snippet that scrapes a feed and prints it out. When I run the code, something in the feed triggers the error message you see here as my question. Here's the complete console output on error:</p> <pre><code>&gt; Traceback (most recent call last): &gt; File "/home/vijay/ffour/ffour5.py", &g...
5
2009-01-05T19:35:09Z
414,270
<p>Lets try to clear up some of the confusion in the exception message.</p> <p>The function call</p> <pre><code>sys.stdout.write(entry["title"]) </code></pre> <p>Returns None. The ".encode('utf-8')" is a call to the encode function on what is returned by the above function.</p> <p>The problem is that None doesn't ...
5
2009-01-05T19:50:07Z
[ "python", "urlencode" ]
Django serialize to JSON
414,543
<p>I have a Django model (schedule) with the class of entity, that is the parent of <code>Activity</code>, that is the parent of <code>Event</code>.</p> <pre><code>class Entity(models.Model): &lt;...&gt; class Activity(models.Model): &lt;...&gt; team_entity = models.ForeignKey(Entity) &lt;...&gt; ...
15
2009-01-05T21:10:09Z
414,573
<p>Have a look at serializing inherited models and objects from the Django documentation available at <a href="http://docs.djangoproject.com/en/dev/topics/serialization/?from=olddocs#inherited-models" rel="nofollow">http://docs.djangoproject.com/en/dev/topics/serialization/?from=olddocs#inherited-models</a></p> <p>Tha...
3
2009-01-05T21:20:21Z
[ "python", "django", "json" ]
Django serialize to JSON
414,543
<p>I have a Django model (schedule) with the class of entity, that is the parent of <code>Activity</code>, that is the parent of <code>Event</code>.</p> <pre><code>class Entity(models.Model): &lt;...&gt; class Activity(models.Model): &lt;...&gt; team_entity = models.ForeignKey(Entity) &lt;...&gt; ...
15
2009-01-05T21:10:09Z
414,696
<p>Before you do serialization, when retrieving your objects, to preserve the relationships use select_related() to get children, grandchildren, etc </p> <p>see <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/">http://docs.djangoproject.com/en/dev/ref/models/querysets/</a></p>
10
2009-01-05T22:11:28Z
[ "python", "django", "json" ]
Django serialize to JSON
414,543
<p>I have a Django model (schedule) with the class of entity, that is the parent of <code>Activity</code>, that is the parent of <code>Event</code>.</p> <pre><code>class Entity(models.Model): &lt;...&gt; class Activity(models.Model): &lt;...&gt; team_entity = models.ForeignKey(Entity) &lt;...&gt; ...
15
2009-01-05T21:10:09Z
415,421
<p>It seems to me that the question the poster was asking was to end up with a result like:</p> <p>For instance, starting with these models:</p> <pre><code>class Entity(models.Model): name = models.CharField(...) class Activity(models.Model): name = models.CharField(...) team_entity = models.ForeignKey(E...
5
2009-01-06T04:07:53Z
[ "python", "django", "json" ]
Django serialize to JSON
414,543
<p>I have a Django model (schedule) with the class of entity, that is the parent of <code>Activity</code>, that is the parent of <code>Event</code>.</p> <pre><code>class Entity(models.Model): &lt;...&gt; class Activity(models.Model): &lt;...&gt; team_entity = models.ForeignKey(Entity) &lt;...&gt; ...
15
2009-01-05T21:10:09Z
518,167
<p>I believe you can find your answer here: <a href="http://code.djangoproject.com/ticket/4656" rel="nofollow">http://code.djangoproject.com/ticket/4656</a></p> <p>This will become part of django serializers at some stage. Right now it should be able to just replace standard django serializers with this and work away....
3
2009-02-05T22:01:47Z
[ "python", "django", "json" ]
Django serialize to JSON
414,543
<p>I have a Django model (schedule) with the class of entity, that is the parent of <code>Activity</code>, that is the parent of <code>Event</code>.</p> <pre><code>class Entity(models.Model): &lt;...&gt; class Activity(models.Model): &lt;...&gt; team_entity = models.ForeignKey(Entity) &lt;...&gt; ...
15
2009-01-05T21:10:09Z
918,010
<p>I now use django-piston. This does the trick.</p>
7
2009-05-27T21:05:55Z
[ "python", "django", "json" ]
Django serialize to JSON
414,543
<p>I have a Django model (schedule) with the class of entity, that is the parent of <code>Activity</code>, that is the parent of <code>Event</code>.</p> <pre><code>class Entity(models.Model): &lt;...&gt; class Activity(models.Model): &lt;...&gt; team_entity = models.ForeignKey(Entity) &lt;...&gt; ...
15
2009-01-05T21:10:09Z
13,697,848
<p>you can do this in simple two lines of code :</p> <pre><code>from django.core import serializers data = serializers.serialize("json", SomeModel.objects.all()) </code></pre>
-1
2012-12-04T07:14:35Z
[ "python", "django", "json" ]
Add class to Django label_tag() output
414,679
<p>I need some way to add a class attribute to the output of the label_tag() method for a forms field. </p> <p>I see that there is the ability to pass in an attrs dictionary and I have tested it in the shell and I can do something like:</p> <pre><code>for field in form: print field.label_tag(attrs{'class':'Foo'})...
16
2009-01-05T22:05:15Z
415,770
<p>A <a href="http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags">custom template tag</a> seems to be the solution. A custom filter would also do, although it can be less elegant. But you would need to fall back to custom form rendering in both cases.</p> <p>If this is a task o...
8
2009-01-06T08:00:00Z
[ "python", "django", "forms", "newforms" ]
Add class to Django label_tag() output
414,679
<p>I need some way to add a class attribute to the output of the label_tag() method for a forms field. </p> <p>I see that there is the ability to pass in an attrs dictionary and I have tested it in the shell and I can do something like:</p> <pre><code>for field in form: print field.label_tag(attrs{'class':'Foo'})...
16
2009-01-05T22:05:15Z
1,653,548
<p>I am agree with answer number one, with css this could be done, but. What is the reson for this to be in django source?</p> <p>In django.forms.forms.py there's this definition that shows there's code to display attrs in labels:</p> <pre><code>class BoundField(StrAndUnicode): # .... def label_tag(self, contents=No...
1
2009-10-31T05:47:09Z
[ "python", "django", "forms", "newforms" ]
Add class to Django label_tag() output
414,679
<p>I need some way to add a class attribute to the output of the label_tag() method for a forms field. </p> <p>I see that there is the ability to pass in an attrs dictionary and I have tested it in the shell and I can do something like:</p> <pre><code>for field in form: print field.label_tag(attrs{'class':'Foo'})...
16
2009-01-05T22:05:15Z
1,933,711
<p>How about adding the CSS class to the form field in the forms.py, like:</p> <pre><code>class MyForm(forms.Form): title = forms.CharField(widget=forms.TextInput(attrs={'class': 'foo'})) </code></pre> <p>then I just do the following in the template:</p> <pre><code>&lt;label for="id_{{form.title.name}}" class="b...
10
2009-12-19T18:16:02Z
[ "python", "django", "forms", "newforms" ]
Add class to Django label_tag() output
414,679
<p>I need some way to add a class attribute to the output of the label_tag() method for a forms field. </p> <p>I see that there is the ability to pass in an attrs dictionary and I have tested it in the shell and I can do something like:</p> <pre><code>for field in form: print field.label_tag(attrs{'class':'Foo'})...
16
2009-01-05T22:05:15Z
6,192,134
<pre><code>@register.simple_tag def advanced_label_tag(field): """ Return form field label html marked to fill by `*` """ classes = [] attrs = {} contents = force_unicode(escape(field.label)) if field.field.required: classes.append(u'required') contents = force_unicode('%s &lt;span&...
0
2011-05-31T18:52:51Z
[ "python", "django", "forms", "newforms" ]
Add class to Django label_tag() output
414,679
<p>I need some way to add a class attribute to the output of the label_tag() method for a forms field. </p> <p>I see that there is the ability to pass in an attrs dictionary and I have tested it in the shell and I can do something like:</p> <pre><code>for field in form: print field.label_tag(attrs{'class':'Foo'})...
16
2009-01-05T22:05:15Z
11,584,458
<p><strong>Technique 1</strong></p> <p>I take issue with another answer's assertion that a filter would be "less elegant." As you can see, it's very elegant indeed.</p> <pre><code>@register.filter(is_safe=True) def label_with_classes(value, arg): return value.label_tag(attrs={'class': arg}) </code></pre> <p>Usi...
7
2012-07-20T17:49:31Z
[ "python", "django", "forms", "newforms" ]
Add class to Django label_tag() output
414,679
<p>I need some way to add a class attribute to the output of the label_tag() method for a forms field. </p> <p>I see that there is the ability to pass in an attrs dictionary and I have tested it in the shell and I can do something like:</p> <pre><code>for field in form: print field.label_tag(attrs{'class':'Foo'})...
16
2009-01-05T22:05:15Z
18,531,433
<pre><code>class CustomBoundField(BoundField): def label_tag(self, contents=None, attrs=None): if self.field.required: attrs = {'class': 'required'} return super(CustomBoundField, self).label_tag(contents, attrs) class ImportViewerForm(forms.Form): url = fields.URLField(widget=forms...
0
2013-08-30T11:08:07Z
[ "python", "django", "forms", "newforms" ]
Add class to Django label_tag() output
414,679
<p>I need some way to add a class attribute to the output of the label_tag() method for a forms field. </p> <p>I see that there is the ability to pass in an attrs dictionary and I have tested it in the shell and I can do something like:</p> <pre><code>for field in form: print field.label_tag(attrs{'class':'Foo'})...
16
2009-01-05T22:05:15Z
28,235,472
<p>A bit too late but came across a similar problem. Hope this helps you. </p> <pre><code>class MyForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['myfield1'].widget.attrs.update( {'class': 'form-control'}) se...
0
2015-01-30T11:59:59Z
[ "python", "django", "forms", "newforms" ]