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
Adding a generic image field onto a ModelForm in django
467,985
<p>I have two models, <code>Room</code> and <code>Image</code>. <code>Image</code> is a generic model that can tack onto any other model. I want to give users a form to upload an image when they post information about a room. I've written code that works, but I'm afraid I've done it the hard way, and specifically in...
2
2009-01-22T03:45:49Z
550,174
<p>Ok I figured it out with some more reading... I feel like you want to do exactly what I have done so here it is.</p> <p>I'll be using GenericForeignKeys for this.</p> <p>First the imports for <code>models.py</code></p> <pre><code>from django.contrib.contenttypes.models import ContentType from django.contrib.conte...
0
2009-02-15T02:10:21Z
[ "python", "django", "django-forms" ]
Adding a generic image field onto a ModelForm in django
467,985
<p>I have two models, <code>Room</code> and <code>Image</code>. <code>Image</code> is a generic model that can tack onto any other model. I want to give users a form to upload an image when they post information about a room. I've written code that works, but I'm afraid I've done it the hard way, and specifically in...
2
2009-01-22T03:45:49Z
2,401,025
<p>Use two forms, one for the room and one for the image:</p> <p>class Image(models.Model)</p> <pre><code>content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') image = models.ImageField(upload_to='') </code></pre...
0
2010-03-08T11:49:47Z
[ "python", "django", "django-forms" ]
Tracking file load progress in Python
468,238
<p>A lot of modules I use import entire files into memory or trickle a file's contents in while they process it. I'm wondering if there's any way to track this sort of loading progress? Possibly a wrapper class that takes a callback?</p>
3
2009-01-22T06:44:52Z
468,349
<p>I would go by this by determining the size of the file, and then simply dividing the total by the number of bytes read. Like this:</p> <pre><code>import os def show_progress(file_name, chunk_size=1024): fh = open(file_name, "r") total_size = os.path.getsize(file_name) total_read = 0 while True: ...
7
2009-01-22T07:46:14Z
[ "python", "file", "load", "progress" ]
Tracking file load progress in Python
468,238
<p>A lot of modules I use import entire files into memory or trickle a file's contents in while they process it. I'm wondering if there's any way to track this sort of loading progress? Possibly a wrapper class that takes a callback?</p>
3
2009-01-22T06:44:52Z
468,796
<p>If you actually mean "import" (not "read") then you can override the import module definitions. You can add timing capabilities.</p> <p>See the <a href="http://www.python.org/doc/2.5.2/lib/module-imp.html" rel="nofollow">imp</a> module.</p> <p>If you mean "read", then you can trivially wrap Python files with your...
2
2009-01-22T11:41:32Z
[ "python", "file", "load", "progress" ]
Python: convert alphabetically spelled out numbers to numerics?
468,241
<p>I'm looking for a library, service, or code suggestions to turn spelled out numbers and amounts (eg. "thirty five dollars and fifteen cents", "one point five") into numerics ($35.15, 1.5) . Suggestions?</p>
0
2009-01-22T06:47:33Z
468,249
<p>I wrote some code to do this for integers a while ago: <a href="http://github.com/ghewgill/text2num">http://github.com/ghewgill/text2num</a></p> <p>Feel free to fork and hack.</p>
6
2009-01-22T06:52:37Z
[ "python", "parsing" ]
Python code for sorting files into folders
468,383
<p>Python 2.5.1</p> <p><a href="http://www.cgsecurity.org/wiki/After_Using_PhotoRec" rel="nofollow">http://www.cgsecurity.org/wiki/After_Using_PhotoRec</a></p> <p>I've just run PhotoRec and the code given as a way to sort file types into their own folder is coming back with this error. Any suggestions on how to alter...
1
2009-01-22T08:07:43Z
468,395
<p>It simply means that the program is expecting two command line arguments: source and destination. If you wish to use the same code in another function, replace sys.argv[1] and [2] with your own variables.</p>
2
2009-01-22T08:14:34Z
[ "python", "scripting", "recovery" ]
Python code for sorting files into folders
468,383
<p>Python 2.5.1</p> <p><a href="http://www.cgsecurity.org/wiki/After_Using_PhotoRec" rel="nofollow">http://www.cgsecurity.org/wiki/After_Using_PhotoRec</a></p> <p>I've just run PhotoRec and the code given as a way to sort file types into their own folder is coming back with this error. Any suggestions on how to alter...
1
2009-01-22T08:07:43Z
468,463
<p>Since the script is going to ask for paths if they don't exist, you could make the program arguments optional.</p> <p>Change</p> <pre><code>source = sys.argv[1] destination = sys.argv[2] </code></pre> <p>to</p> <pre><code>source = sys.argv[1] if len(sys.argv &gt; 1) else "" destination = sys.argv[2] if len(sys.a...
0
2009-01-22T08:55:29Z
[ "python", "scripting", "recovery" ]
Python code for sorting files into folders
468,383
<p>Python 2.5.1</p> <p><a href="http://www.cgsecurity.org/wiki/After_Using_PhotoRec" rel="nofollow">http://www.cgsecurity.org/wiki/After_Using_PhotoRec</a></p> <p>I've just run PhotoRec and the code given as a way to sort file types into their own folder is coming back with this error. Any suggestions on how to alter...
1
2009-01-22T08:07:43Z
468,472
<p>Or you can modify the original script and add</p> <pre><code>if len(sys.argv) != 3: print "Require 2 arguments: %s &lt;source&gt; &lt;destination&gt;" %(sys.argv[0]) sys.exit(1) </code></pre> <p>after the import statements for proper error handling.</p>
2
2009-01-22T08:57:37Z
[ "python", "scripting", "recovery" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
468,433
<p>As far as I know not exactly but you get somewhere with</p> <pre><code>object.methods.sort </code></pre>
44
2009-01-22T08:39:41Z
[ "python", "ruby", "inspection" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
468,442
<p>You can take a module, such as <code>Enumerable</code>, and send the <code>methods</code> method which lists all the methods the module defines. Classes that include this module will respond to these methods.</p> <pre><code>&gt;&gt; Enumerable.methods =&gt; ["inspect", "private_class_method", "const_missing", "clon...
4
2009-01-22T08:41:01Z
[ "python", "ruby", "inspection" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
468,635
<p>I'd go for something like this:</p> <pre><code>y String.methods.sort </code></pre> <p>Which will give you a yaml representation of the sorted array of methods. Note that this can be used to list the methods of both classes and objects.</p>
1
2009-01-22T10:25:56Z
[ "python", "ruby", "inspection" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
468,771
<p>Tip for "searching" for a method in irb: </p> <pre><code>"something".methods.select {|item| item =~ /query/ } </code></pre> <p>Tip for trying out methods on a value for comparison:</p> <pre><code>value = "something" [:upcase, :downcase, :capitalize].collect {|method| [method, value.send(method)] } </code></pre> ...
4
2009-01-22T11:33:44Z
[ "python", "ruby", "inspection" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
469,011
<p>Not really. Like the others said, you can get part of what you want by listing class instance methods (e.g. <code>String.instance_methods</code>) but that doesn't help you if a file you open reopens a class (unless you check before and after).</p> <p>If you don't need programmatic access to the list of methods, co...
0
2009-01-22T12:57:58Z
[ "python", "ruby", "inspection" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
469,926
<p>I would have made this a comment to jonelf's answer, but apparently I don't have enough rep.</p> <p>some_object.methods.sort - Object.new.methods</p> <p>This isn't exactly what you were asking as others have said, but it gives you the info you are after.</p>
0
2009-01-22T16:53:00Z
[ "python", "ruby", "inspection" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
470,967
<p>If I stricly read your question, I must answer it that way: a file as specified by <code>require</code> in Ruby is just a container and does not have necessarely have any relation with a class. The content can be:</p> <ul> <li>a class</li> <li>a module</li> <li>plain code</li> </ul> <p>or any combination of the a...
0
2009-01-22T21:50:35Z
[ "python", "ruby", "inspection" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
704,557
<p>I like to have this in my .irbrc:</p> <pre><code>class Object def local_methods (methods - Object.instance_methods).sort end end </code></pre> <p>So when I'm in irb:</p> <pre><code>&gt;&gt; Time.now.local_methods =&gt; ["+", "-", "&lt;", "&lt;=", "&lt;=&gt;", "&gt;", "&gt;=", "_dump", "asctime", "between...
15
2009-04-01T08:20:38Z
[ "python", "ruby", "inspection" ]
Ruby equivalent of Python's "dir"?
468,421
<p>In Python we can "dir" a module, like this:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; dir(re) </code></pre> <p>And it lists all functions in the module. Is there a similar way to do this in Ruby?</p>
39
2009-01-22T08:32:05Z
24,001,868
<p>Maybe not answering the original question (depends on the use case), but for those who are looking for this to be used in the <code>irb</code> only, you can use "double-TAB" for autocompletion. Which, effectively, can also list (almost all) the methods available for a given object.</p> <p>Put the following line int...
0
2014-06-02T19:32:19Z
[ "python", "ruby", "inspection" ]
Is there a standalone Python type conversion library?
468,639
<p>Are there any standalone type conversion libraries?</p> <p>I have a data storage system that only understands bytes/strings, but I can tag metadata such as the type to be converted to.</p> <p>I could hack up some naive system of type converters, as every other application has done before me, or I could hopefully u...
0
2009-01-22T10:29:33Z
468,683
<p>You've got two options, either use the <a href="http://docs.python.org/library/struct.html#module-struct" rel="nofollow">struct</a> or <a href="http://docs.python.org/library/pickle.html#module-pickle" rel="nofollow">pickle</a> modules.</p> <p>With struct you specify a format and it compacts your data to byte array...
3
2009-01-22T10:59:44Z
[ "python", "type-conversion" ]
Is there a standalone Python type conversion library?
468,639
<p>Are there any standalone type conversion libraries?</p> <p>I have a data storage system that only understands bytes/strings, but I can tag metadata such as the type to be converted to.</p> <p>I could hack up some naive system of type converters, as every other application has done before me, or I could hopefully u...
0
2009-01-22T10:29:33Z
468,866
<p>Consider this.</p> <pre><code>import datetime def toDate( someString ): return datetime.datetime.strptime( someString, "%x" ).date() typeConversionMapping = { 'integer': int, 'string': str, 'float': float, 'date': toDate } def typeConversionFunction( typeConversionTuple ): theStringRepresentation, theType...
3
2009-01-22T12:02:48Z
[ "python", "type-conversion" ]
Is there a standalone Python type conversion library?
468,639
<p>Are there any standalone type conversion libraries?</p> <p>I have a data storage system that only understands bytes/strings, but I can tag metadata such as the type to be converted to.</p> <p>I could hack up some naive system of type converters, as every other application has done before me, or I could hopefully u...
0
2009-01-22T10:29:33Z
1,584,810
<p>Flatland does this well. <a href="http://discorporate.us/projects/flatland/" rel="nofollow">http://discorporate.us/projects/flatland/</a></p>
1
2009-10-18T12:51:46Z
[ "python", "type-conversion" ]
Templates within templates. How to avoid rendering twice?
468,736
<p>I've got a CMS that takes some dynamic content and renders it using a standard template. However I am now using template tags in the dynamic content itself so I have to do a render_to_string and then pass the results of that as a context variable to render_to_response. This seems wasteful.</p> <p>What's a better wa...
1
2009-01-22T11:22:00Z
468,751
<p>"This seems wasteful" Why does it seem that way?</p> <p>Every template is a mix of tags and text. In your case some block of text has already been visited by a template engine. So what? Once it's been transformed it's just text and passes through the next template engine very, very quickly.</p> <p>Do you have ...
2
2009-01-22T11:28:28Z
[ "python", "django", "django-templates" ]
Templates within templates. How to avoid rendering twice?
468,736
<p>I've got a CMS that takes some dynamic content and renders it using a standard template. However I am now using template tags in the dynamic content itself so I have to do a render_to_string and then pass the results of that as a context variable to render_to_response. This seems wasteful.</p> <p>What's a better wa...
1
2009-01-22T11:22:00Z
468,953
<p>What you're doing sounds fine, but the question could be asked: Why not put the templatetag references directly in your template instead of manually rendering them?</p> <pre><code>&lt;div&gt; {% if object matches some criteria %} {% render_type1_object object %} {% else %} {% render_type2_o...
0
2009-01-22T12:39:47Z
[ "python", "django", "django-templates" ]
In Django how do i return the total number of items that are related to a model?
469,110
<p>In Django how can i return the total number of items (count) that are related to another model, e.g the way stackoverflow does a list of questions then on the side it shows the count on the answers related to that question.</p> <p>This is easy if i get the questionid, i can return all answers related to that questi...
1
2009-01-22T13:26:22Z
469,423
<p><a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#count">QuerySet.count()</a></p> <p>See also an <a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward">example</a> how to build QuerySets of related models.</p>
5
2009-01-22T14:56:38Z
[ "python", "django" ]
In Django how do i return the total number of items that are related to a model?
469,110
<p>In Django how can i return the total number of items (count) that are related to another model, e.g the way stackoverflow does a list of questions then on the side it shows the count on the answers related to that question.</p> <p>This is easy if i get the questionid, i can return all answers related to that questi...
1
2009-01-22T13:26:22Z
470,293
<p>If you're willing to use trunk, you can take advantage of the brand new annotate() QuerySet method added just a week or so ago, which solves this exact problem:</p> <p><a href="http://docs.djangoproject.com/en/dev/topics/db/aggregation/" rel="nofollow">http://docs.djangoproject.com/en/dev/topics/db/aggregation/</a>...
1
2009-01-22T18:24:37Z
[ "python", "django" ]
Any Python OLAP/MDX ORM engines?
469,200
<p>I'm new to the MDX/OLAP and I'm wondering if there is any ORM similar like Django ORM for Python that would support OLAP.</p> <p>I'm a Python/Django developer and if there would be something that would have some level of integration with Django I would be much interested in learning more about it.</p>
7
2009-01-22T13:56:23Z
471,669
<p>Django has some OLAP features that are nearing release.</p> <p>Read <a href="http://www.eflorenzano.com/blog/post/secrets-django-orm/" rel="nofollow">http://www.eflorenzano.com/blog/post/secrets-django-orm/</a></p> <p><a href="http://doughellmann.com/2007/12/30/using-raw-sql-in-django.html" rel="nofollow">http://d...
6
2009-01-23T02:24:53Z
[ "python", "django", "orm", "olap", "mdx" ]
Any Python OLAP/MDX ORM engines?
469,200
<p>I'm new to the MDX/OLAP and I'm wondering if there is any ORM similar like Django ORM for Python that would support OLAP.</p> <p>I'm a Python/Django developer and if there would be something that would have some level of integration with Django I would be much interested in learning more about it.</p>
7
2009-01-22T13:56:23Z
2,683,321
<p>I had a similar need - not for a full blown ORM but for a simple OLAP-like data store in Python. After coming up dry searching for existing tools I wrote this little hack:</p> <p><a href="https://github.com/kpwebb/python-cube/blob/master/src/cube.py" rel="nofollow">https://github.com/kpwebb/python-cube/blob/master/...
0
2010-04-21T13:33:32Z
[ "python", "django", "orm", "olap", "mdx" ]
Any Python OLAP/MDX ORM engines?
469,200
<p>I'm new to the MDX/OLAP and I'm wondering if there is any ORM similar like Django ORM for Python that would support OLAP.</p> <p>I'm a Python/Django developer and if there would be something that would have some level of integration with Django I would be much interested in learning more about it.</p>
7
2009-01-22T13:56:23Z
2,989,623
<p>Same thing as <em>kpw</em>, I write my own stuff, except that it is exclusively for Django :</p> <p><a href="https://code.google.com/p/django-cube/" rel="nofollow">https://code.google.com/p/django-cube/</a></p>
1
2010-06-07T12:53:01Z
[ "python", "django", "orm", "olap", "mdx" ]
Any Python OLAP/MDX ORM engines?
469,200
<p>I'm new to the MDX/OLAP and I'm wondering if there is any ORM similar like Django ORM for Python that would support OLAP.</p> <p>I'm a Python/Django developer and if there would be something that would have some level of integration with Django I would be much interested in learning more about it.</p>
7
2009-01-22T13:56:23Z
19,683,765
<p>There is also <a href="http://cubes.databrewery.org/" rel="nofollow">http://cubes.databrewery.org/</a> . Lightweight OLAP engine in python.</p>
1
2013-10-30T13:35:23Z
[ "python", "django", "orm", "olap", "mdx" ]
adding scrollbars to pythoncard application
469,219
<p>scrollingwindow as main frame for the application is not supported yet for pythoncard. how can i add scrollbars to main frame(background)?</p>
2
2009-01-22T14:02:18Z
540,375
<p>Ive never used pythoncard but in pure wxpython you can just put a ScrolledWindow inside the frame, then use a sizer to controll the scrollbars (asumming the contents of the sizer dont fit in the window). Eg this short code snipit will give you a window with a vertical scrollbar.</p> <pre><code>class Scrolled(wx.Scr...
2
2009-02-12T07:43:20Z
[ "python", "wxpython", "scroll", "pythoncard" ]
How can I listen for 'usb device inserted' events in Linux, in Python?
469,243
<p>I'd like to write a Python script for Amarok in Linux to automatically copy the stackoverflow podcast to my player. When I plug in the player, it would mount the drive, copy any pending podcasts, and eject the player. How can I listen for the "plugged in" event? I have looked through hald but couldn't find a good ex...
28
2009-01-22T14:09:59Z
469,433
<p>I haven't tried writing such a program myself, however I've just looked at the following two links (thanks Google!), which I think will be of help:</p> <ul> <li><a href="http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html" rel="nofollow">dbus-python tutorial</a> (which talks about how to use Python to acc...
7
2009-01-22T14:59:36Z
[ "python", "linux", "usb" ]
How can I listen for 'usb device inserted' events in Linux, in Python?
469,243
<p>I'd like to write a Python script for Amarok in Linux to automatically copy the stackoverflow podcast to my player. When I plug in the player, it would mount the drive, copy any pending podcasts, and eject the player. How can I listen for the "plugged in" event? I have looked through hald but couldn't find a good ex...
28
2009-01-22T14:09:59Z
469,522
<p>I think D-Bus would work as Chris mentioned, but if you're using KDE4, you might use the Solid framework in a manner similar to the KDE4 "New Device Notifier" applet.</p> <p>The C++ source for that applet is <a href="http://kollide.net:8060/browse/Plasma/applets/devicenotifier/devicenotifier.cpp?r=21574" rel="nofol...
4
2009-01-22T15:15:46Z
[ "python", "linux", "usb" ]
How can I listen for 'usb device inserted' events in Linux, in Python?
469,243
<p>I'd like to write a Python script for Amarok in Linux to automatically copy the stackoverflow podcast to my player. When I plug in the player, it would mount the drive, copy any pending podcasts, and eject the player. How can I listen for the "plugged in" event? I have looked through hald but couldn't find a good ex...
28
2009-01-22T14:09:59Z
471,099
<p><strong>Update</strong>: As said in comments, Hal is not supported in recent distributions, the standard now is udev, Here is a small example that makes use of glib loop and <strong>udev</strong>, I keep the Hal version for historical reasons.</p> <p>This is basically the <a href="http://pyudev.readthedocs.org/en/l...
45
2009-01-22T22:27:58Z
[ "python", "linux", "usb" ]
How can I listen for 'usb device inserted' events in Linux, in Python?
469,243
<p>I'd like to write a Python script for Amarok in Linux to automatically copy the stackoverflow podcast to my player. When I plug in the player, it would mount the drive, copy any pending podcasts, and eject the player. How can I listen for the "plugged in" event? I have looked through hald but couldn't find a good ex...
28
2009-01-22T14:09:59Z
39,288,660
<p>Here is a solution in 5 lines.</p> <pre><code>import pyudev context = pyudev.Context() monitor = pyudev.Monitor.from_netlink(context) monitor.filter_by(subsystem='usb') for device in iter(monitor.poll, None): if device.action == 'add': print('{} connected'.format(device)) # do something very i...
0
2016-09-02T09:13:16Z
[ "python", "linux", "usb" ]
deleting rows of a numpy array based on uniqueness of a value
469,931
<p>let's say I have a bi-dimensional array like that</p> <pre><code>numpy.array( [[0,1,1.2,3], [1,5,3.2,4], [3,4,2.8,4], [2,6,2.3,5]]) </code></pre> <p>I want to have an array formed eliminating whole rows based on uniqueness of values of last column, selecting the row to keep based on value of third...
4
2009-01-22T16:54:05Z
470,521
<p>My numpy is way out of practice, but this should work:</p> <pre><code>#keepers is a dictionary of type int: (int, int) #the key is the row's final value, and the tuple is (row index, row[2]) keepers = {} deletions = [] for i, row in enumerate(n): key = row[3] if key not in keepers: keepers[key] = (i...
1
2009-01-22T19:41:26Z
[ "python", "arrays", "numpy", "unique" ]
deleting rows of a numpy array based on uniqueness of a value
469,931
<p>let's say I have a bi-dimensional array like that</p> <pre><code>numpy.array( [[0,1,1.2,3], [1,5,3.2,4], [3,4,2.8,4], [2,6,2.3,5]]) </code></pre> <p>I want to have an array formed eliminating whole rows based on uniqueness of values of last column, selecting the row to keep based on value of third...
4
2009-01-22T16:54:05Z
27,403,267
<p>This can be achieved efficiently in Numpy by combining <code>lexsort</code> and <code>unique</code> as follows</p> <pre><code>import numpy as np a = np.array([[0, 1, 1.2, 3], [1, 5, 3.2, 4], [3, 4, 2.8, 4], [2, 6, 2.3, 5]]) # Sort by last column and 3rd column when valu...
1
2014-12-10T14:16:41Z
[ "python", "arrays", "numpy", "unique" ]
problem using an instance in a with_statement
469,950
<p>I've recently started to learn python , and I reached the <strong>with</strong> statement . I've tried to use it with a class instance , but I think I'm doing something wrong . Here is the code :</p> <pre><code> from __future__ import with_statement import pdb class Geo: def __init__(self,text): self.text =...
4
2009-01-22T16:58:42Z
469,992
<p>Your <code>__enter__</code> method needs to return the object that should be used for the "<code>as g</code>" part of the with statement. See the <a href="http://docs.python.org/reference/compound_stmts.html#with">documentation</a>, where it states:</p> <ul> <li>If a target was included in the with statement, the ...
12
2009-01-22T17:08:38Z
[ "python", "with-statement" ]
AppEngine and Django: including a template file
470,008
<p>As the title suggests, I'm using Google App Engine and Django.</p> <p>I have quite a bit of identical code across my templates and would like to reduce this by including template files. So, in my main application directory I have the python handler file, the main template, and the template I want to include in my m...
4
2009-01-22T17:12:54Z
470,233
<p>First, you should consider using <a href="http://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance" rel="nofollow">template inheritance</a> rather than the <code>include</code> tag, which is often appropriate but sometimes far inferior to template inheritance.</p> <p>Unfortunately, I have no expe...
3
2009-01-22T18:07:57Z
[ "python", "django", "google-app-engine", "templates", "include" ]
AppEngine and Django: including a template file
470,008
<p>As the title suggests, I'm using Google App Engine and Django.</p> <p>I have quite a bit of identical code across my templates and would like to reduce this by including template files. So, in my main application directory I have the python handler file, the main template, and the template I want to include in my m...
4
2009-01-22T17:12:54Z
504,733
<p>I found that it works "out of the box" if I don't load Templates first and render them with a Context object. Instead, I use the standard method shown in the <a href="http://code.google.com/appengine/docs/python/gettingstarted/templates.html" rel="nofollow">AppEngine tutorial</a>.</p>
1
2009-02-02T20:25:05Z
[ "python", "django", "google-app-engine", "templates", "include" ]
AppEngine and Django: including a template file
470,008
<p>As the title suggests, I'm using Google App Engine and Django.</p> <p>I have quite a bit of identical code across my templates and would like to reduce this by including template files. So, in my main application directory I have the python handler file, the main template, and the template I want to include in my m...
4
2009-01-22T17:12:54Z
2,906,723
<p>I've done the following to get around using includes:</p> <pre><code>def render(file, map={}): return template.render( os.path.join(os.path.dirname(__file__), '../templates', file), map) table = render("table.html", {"headers": headers, "rows": rows}) final = render("final.html", {"table": table}) self...
0
2010-05-25T16:58:12Z
[ "python", "django", "google-app-engine", "templates", "include" ]
AppEngine and Django: including a template file
470,008
<p>As the title suggests, I'm using Google App Engine and Django.</p> <p>I have quite a bit of identical code across my templates and would like to reduce this by including template files. So, in my main application directory I have the python handler file, the main template, and the template I want to include in my m...
4
2009-01-22T17:12:54Z
5,761,711
<p>I am having the same problem and tracked it down into the ext.webapp package. In template.py, you'll find this comment on line 33:</p> <p>Django uses a global setting for the directory in which it looks for templates. This is not natural in the context of the webapp module, so our load method takes in a complete t...
1
2011-04-23T02:22:30Z
[ "python", "django", "google-app-engine", "templates", "include" ]
Why does 1+++2 = 3 in python?
470,139
<p>I am from C background and I just started learning python... </p> <p>while trying some programs, I got this doubt...</p> <p>how python evaluates the expression 1+++2?</p> <p>No matter how many number of '+' I put in between, it is printing 3 as the answer. Please can anyone explain this behavior</p> <p>and for 1...
16
2009-01-22T17:41:44Z
470,156
<p>Your expression is the same as:</p> <pre><code>1+(+(+2)) </code></pre> <p>Any numeric expression can be preceded by <code>-</code> to make it negative, or <code>+</code> to do nothing (the option is present for symmetry). With negative signs:</p> <pre><code>1-(-(2)) = 1-(-2) = 1+2 = 3 </code></p...
47
2009-01-22T17:46:08Z
[ "python", "evaluation" ]
Why does 1+++2 = 3 in python?
470,139
<p>I am from C background and I just started learning python... </p> <p>while trying some programs, I got this doubt...</p> <p>how python evaluates the expression 1+++2?</p> <p>No matter how many number of '+' I put in between, it is printing 3 as the answer. Please can anyone explain this behavior</p> <p>and for 1...
16
2009-01-22T17:41:44Z
470,160
<p>1+(+(+2)) = 3</p> <p>1 - (-2) = 3</p> <p>1 - (-(-2)) = -1</p>
3
2009-01-22T17:46:22Z
[ "python", "evaluation" ]
Why does 1+++2 = 3 in python?
470,139
<p>I am from C background and I just started learning python... </p> <p>while trying some programs, I got this doubt...</p> <p>how python evaluates the expression 1+++2?</p> <p>No matter how many number of '+' I put in between, it is printing 3 as the answer. Please can anyone explain this behavior</p> <p>and for 1...
16
2009-01-22T17:41:44Z
470,162
<p>The extra +'s are not incrementors (like ++a or a++ in c++). They are just showing that the number is positive.</p> <p>There is no such ++ operator. There is a unary + operator and a unary - operator though. The unary + operator has no effect on its argument. The unary - operator negates its operator or mulitpli...
11
2009-01-22T17:47:04Z
[ "python", "evaluation" ]
Why does 1+++2 = 3 in python?
470,139
<p>I am from C background and I just started learning python... </p> <p>while trying some programs, I got this doubt...</p> <p>how python evaluates the expression 1+++2?</p> <p>No matter how many number of '+' I put in between, it is printing 3 as the answer. Please can anyone explain this behavior</p> <p>and for 1...
16
2009-01-22T17:41:44Z
470,163
<p>Think it as 1 + (+1*(+1*2))). The first + is operator and following plus signs are sign of second operand (= 2).</p> <p>Just like 1---2 is same as 1 - -(-(2)) or 1- (-1*(-1*(2))</p>
0
2009-01-22T17:47:08Z
[ "python", "evaluation" ]
Why does 1+++2 = 3 in python?
470,139
<p>I am from C background and I just started learning python... </p> <p>while trying some programs, I got this doubt...</p> <p>how python evaluates the expression 1+++2?</p> <p>No matter how many number of '+' I put in between, it is printing 3 as the answer. Please can anyone explain this behavior</p> <p>and for 1...
16
2009-01-22T17:41:44Z
470,165
<p>I believe it's being parsed as, the first + as a binary operation (add), and the rest as unary operations (make positive). </p> <pre><code> 1 + (+(+2)) </code></pre>
1
2009-01-22T17:47:36Z
[ "python", "evaluation" ]
Why does 1+++2 = 3 in python?
470,139
<p>I am from C background and I just started learning python... </p> <p>while trying some programs, I got this doubt...</p> <p>how python evaluates the expression 1+++2?</p> <p>No matter how many number of '+' I put in between, it is printing 3 as the answer. Please can anyone explain this behavior</p> <p>and for 1...
16
2009-01-22T17:41:44Z
470,168
<p>Trying <a href="http://docs.python.org/reference/expressions.html#unary-arithmetic-operations" rel="nofollow">Unary Plus and Unary minus</a>:</p> <blockquote> <p>The unary - (minus) operator yields the negation of its numeric argument.</p> <p>The unary + (plus) operator yields its numeric argument unchanged....
2
2009-01-22T17:47:47Z
[ "python", "evaluation" ]
Why does 1+++2 = 3 in python?
470,139
<p>I am from C background and I just started learning python... </p> <p>while trying some programs, I got this doubt...</p> <p>how python evaluates the expression 1+++2?</p> <p>No matter how many number of '+' I put in between, it is printing 3 as the answer. Please can anyone explain this behavior</p> <p>and for 1...
16
2009-01-22T17:41:44Z
470,178
<p>It's simple. There are no post-incrementation or post-decrementation operators in Python.</p> <p>See here: <a href="http://mail.python.org/pipermail/python-list/2006-January/361771.html" rel="nofollow">http://mail.python.org/pipermail/python-list/2006-January/361771.html</a></p>
0
2009-01-22T17:51:11Z
[ "python", "evaluation" ]
ISO encoded attachment names and python
470,567
<p>First of all i don't have the code example on this computer, but i have an example that is quite similar.</p> <p>(<a href="http://docs.python.org/library/email-examples.html" rel="nofollow">http://docs.python.org/library/email-examples.html</a>)</p> <p>The 4th one.</p> <p>My issue lies within this bit of code</p>...
1
2009-01-22T19:54:50Z
479,238
<p>I found the issue, it was with </p> <pre><code>mimetypes.guess_extension(part.get_content_type()) </code></pre> <p>And images with "image/pjpeg" as the content type</p> <p>@S.Lott i have changed the code to resemble the above example, but i added this to fix the pjpeg issue.</p> <pre><code>if not filename: ext ...
0
2009-01-26T09:38:26Z
[ "python", "email", "attachment" ]
Jython and python modules
471,000
<p>I've just started using the <code>PythonInterpreter</code> from within my Java classes, and it works great! However, if I try to include python modules (<code>re</code>, <code>HTMLParser</code>, etc.), I'm receiving the following exception (for <code>re</code>):</p> <pre> Exception in thread "main" Traceback (inner...
18
2009-01-22T21:57:46Z
471,143
<p>According to the <a href="http://www.jython.org/Project/userfaq.html#jython-modules" rel="nofollow">FAQ</a>:</p> <blockquote> <h2>4.1 What parts of the Python library are supported?</h2> <p>The good news is that Jython now supports a large majority of the standard Python library. The bad news is that this ...
7
2009-01-22T22:39:59Z
[ "java", "python", "interop", "jython" ]
Jython and python modules
471,000
<p>I've just started using the <code>PythonInterpreter</code> from within my Java classes, and it works great! However, if I try to include python modules (<code>re</code>, <code>HTMLParser</code>, etc.), I'm receiving the following exception (for <code>re</code>):</p> <pre> Exception in thread "main" Traceback (inner...
18
2009-01-22T21:57:46Z
471,153
<p>Check your jython sys.path . Make sure that the library you want to load are in this path. Look at <a href="http://www.jython.org/cgi-bin/faqw.py?req=show&amp;file=faq02.003.htp" rel="nofollow">jython faq</a> for more details.</p>
1
2009-01-22T22:44:13Z
[ "java", "python", "interop", "jython" ]
Jython and python modules
471,000
<p>I've just started using the <code>PythonInterpreter</code> from within my Java classes, and it works great! However, if I try to include python modules (<code>re</code>, <code>HTMLParser</code>, etc.), I'm receiving the following exception (for <code>re</code>):</p> <pre> Exception in thread "main" Traceback (inner...
18
2009-01-22T21:57:46Z
483,165
<p>You embed jython and you will use some Python-Modules somewere:</p> <p>if you want to set the path (sys.path) in your Java-Code :</p> <pre><code>public void init() { interp = new PythonInterpreter(null, new PySystemState()); PySystemState sys = Py.getSystemState(); sys.path.append(new PySt...
14
2009-01-27T12:09:48Z
[ "java", "python", "interop", "jython" ]
Jython and python modules
471,000
<p>I've just started using the <code>PythonInterpreter</code> from within my Java classes, and it works great! However, if I try to include python modules (<code>re</code>, <code>HTMLParser</code>, etc.), I'm receiving the following exception (for <code>re</code>):</p> <pre> Exception in thread "main" Traceback (inner...
18
2009-01-22T21:57:46Z
33,936,241
<p>You can refer here for the solution <a href="http://stackoverflow.com/questions/3256135/importing-python-modules-in-jython/33936158#33936158">Importing python modules in jython</a></p> <p>Download <code>ez_setup.py</code> from here <a href="http://peak.telecommunity.com/dist/ez_setup.py" rel="nofollow">http://peak....
0
2015-11-26T10:30:07Z
[ "java", "python", "interop", "jython" ]
How do I remove VSS hooks from a VS Web Site?
471,190
<p>I have a Visual Studio 2008 solution with 7 various projects included with it. 3 of these 'projects' are Web Sites (the kind of project without a project file).</p> <p>I have stripped all the various Visual Sourcesafe files from all the directories, removed the Scc references in the SLN file and all the project fi...
0
2009-01-22T22:56:34Z
471,391
<p>It probably is only trying to add it on your instance of VS. You have to remove the cache so VS thinks its no longer under SS</p> <ol> <li>under file -> SourceControl -> Workspaces</li> <li>Select the SS location</li> <li>Edit</li> <li>Choose the working folder</li> <li>Remove!</li> </ol>
1
2009-01-23T00:11:25Z
[ "python", "visual-studio", "visual-sourcesafe" ]
How do I remove VSS hooks from a VS Web Site?
471,190
<p>I have a Visual Studio 2008 solution with 7 various projects included with it. 3 of these 'projects' are Web Sites (the kind of project without a project file).</p> <p>I have stripped all the various Visual Sourcesafe files from all the directories, removed the Scc references in the SLN file and all the project fi...
0
2009-01-22T22:56:34Z
471,655
<p>Those things are pernicious! Visual Studio sticks links to SourceSafe in everywhere, including into the XML that makes up your sln file.</p> <p>I wrote an <a href="http://billmill.org/ss2svn.html" rel="nofollow">article</a> about my experiences converting sourcesafe to subversion, and included with it the python sc...
1
2009-01-23T02:19:56Z
[ "python", "visual-studio", "visual-sourcesafe" ]
How do I remove VSS hooks from a VS Web Site?
471,190
<p>I have a Visual Studio 2008 solution with 7 various projects included with it. 3 of these 'projects' are Web Sites (the kind of project without a project file).</p> <p>I have stripped all the various Visual Sourcesafe files from all the directories, removed the Scc references in the SLN file and all the project fi...
0
2009-01-22T22:56:34Z
520,070
<p>In your %APPDATA% directory Visual Studio saves a list of websites used in Visual Studio, with some settings of that site:</p> <p>On my Vista Machine the exact location of the file is</p> <pre><code>C:\Users\{name}\AppData\Local\Microsoft\WebsiteCache\Websites.xml </code></pre> <p>This file contains entries like<...
0
2009-02-06T12:11:59Z
[ "python", "visual-studio", "visual-sourcesafe" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
471,217
<p>There's certainly a performance difference when running a compiled script. If you run normal <code>.py</code> scripts, the machine compiles it every time it is run and this takes time. On modern machines this is hardly noticeable but as the script grows it may become more of an issue.</p>
7
2009-01-22T23:02:39Z
[ "python", "compilation" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
471,220
<p>There is a performance increase in running compiled python. However when you run a .py file as an imported module, python will compile and store it, and as long as the .py file does not change it will always use the compiled version.</p> <p>With any interpeted language when the file is used the process looks someth...
8
2009-01-22T23:03:26Z
[ "python", "compilation" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
471,222
<p>Yep, performance is the main reason and, as far as I know, the only reason.</p> <p>If some of your files aren't getting compiled, maybe Python isn't able to write to the .pyc file, perhaps because of the directory permissions or something. Or perhaps the uncompiled files just aren't ever getting loaded... (scripts...
2
2009-01-22T23:03:58Z
[ "python", "compilation" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
471,227
<p>It's compiled to bytecode which can be used much, much, much faster.</p> <p>The reason some files aren't compiled is that the main script, which you invoke with <code>python main.py</code> is recompiled every time you run the script. All imported scripts will be compiled and stored on the disk.</p> <p><em>Importan...
175
2009-01-22T23:06:13Z
[ "python", "compilation" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
471,242
<p>As already mentioned, you can get a performance increase from having your python code compiled into bytecode. This is usually handled by python itself, for imported scripts only.</p> <p>Another reason you might want to compile your python code, could be to protect your intellectual property from being copied and/or...
7
2009-01-22T23:09:53Z
[ "python", "compilation" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
471,252
<p>The .pyc file is Python that has already been compiled to byte-code. Python automatically runs a .pyc file if it finds one with the same name as a .py file you invoke.</p> <p>"An Introduction to Python" <a href="http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html">says</a> this about compiled Pytho...
57
2009-01-22T23:14:33Z
[ "python", "compilation" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
20,998,077
<p>Beginners assume Python is compiled because of .pyc files. The .pyc file is the compiled bytecode, which is then interpreted. So if you've run your Python code before and have the .pyc file handy, it will run faster the second time, as it doesn't have to re-compile the bytecode</p> <p><strong>compiler:</strong> ...
3
2014-01-08T14:13:34Z
[ "python", "compilation" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
23,256,357
<p><strong>Pluses:</strong></p> <p>First: mild, defeatable obfuscation.</p> <p>Second: if compilation results in a significantly smaller file, you will get faster load times. Nice for the web.</p> <p>Third: Python can skip the compilation step. Faster at intial load. Nice for the CPU and the web.</p> <p>Fourth: the...
20
2014-04-23T22:26:10Z
[ "python", "compilation" ]
Why compile Python code?
471,191
<p>Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? </p> <p>I also notice that some files in my application get compiled into .pyc while others do not, why is this?</p>
160
2009-01-22T22:57:34Z
30,850,028
<p>We use compiled code to distribute to users who do not have access to the source code. Basically to stop inexperienced programers accidentally changing something or fixing bugs without telling us.</p>
2
2015-06-15T16:14:32Z
[ "python", "compilation" ]
What is the best way to extract load average float values from a string in Python?
471,211
<p>If I have a string such as</p> <p>"17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15, 0.10"</p> <p>what is the best way to extract just the x3 load average values at the end? I have written a regexp that does this but is this the most efficient / fastest method?</p> <pre><code>&gt;&gt;&gt; s = "17:3...
1
2009-01-22T23:01:19Z
471,235
<p>This should work:</p> <pre><code># s is the string to parse loadavg = [float(x) for x in s.rsplit('load average: ', 1)[1].split(', ')] </code></pre>
5
2009-01-22T23:08:04Z
[ "python", "string", "floating-point" ]
What is the best way to extract load average float values from a string in Python?
471,211
<p>If I have a string such as</p> <p>"17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15, 0.10"</p> <p>what is the best way to extract just the x3 load average values at the end? I have written a regexp that does this but is this the most efficient / fastest method?</p> <pre><code>&gt;&gt;&gt; s = "17:3...
1
2009-01-22T23:01:19Z
471,237
<p>You have the same information in <code>/proc/loadavg</code> special file, so you can do:</p> <pre><code>&gt;&gt;&gt; open("/proc/loadavg").readline().split(" ")[:3] </code></pre>
5
2009-01-22T23:08:32Z
[ "python", "string", "floating-point" ]
What is the best way to extract load average float values from a string in Python?
471,211
<p>If I have a string such as</p> <p>"17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15, 0.10"</p> <p>what is the best way to extract just the x3 load average values at the end? I have written a regexp that does this but is this the most efficient / fastest method?</p> <pre><code>&gt;&gt;&gt; s = "17:3...
1
2009-01-22T23:01:19Z
471,239
<p>Your way seems fine. If you want to avoid regexps you could do something like</p> <pre><code>&gt;&gt;&gt; print s.split(': ')[1].split(', ') ['0.22', '0.15', '0.10'] </code></pre>
0
2009-01-22T23:08:51Z
[ "python", "string", "floating-point" ]
What is the best way to extract load average float values from a string in Python?
471,211
<p>If I have a string such as</p> <p>"17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15, 0.10"</p> <p>what is the best way to extract just the x3 load average values at the end? I have written a regexp that does this but is this the most efficient / fastest method?</p> <pre><code>&gt;&gt;&gt; s = "17:3...
1
2009-01-22T23:01:19Z
471,240
<p>I'd use a regex, definitely. You could perhaps increase efficiency a bit by first calling <code>s.find('load average')</code> and starting the regexp match from that position instead of at the beginning of the string (which is the default).</p>
0
2009-01-22T23:09:07Z
[ "python", "string", "floating-point" ]
What is the best way to extract load average float values from a string in Python?
471,211
<p>If I have a string such as</p> <p>"17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15, 0.10"</p> <p>what is the best way to extract just the x3 load average values at the end? I have written a regexp that does this but is this the most efficient / fastest method?</p> <pre><code>&gt;&gt;&gt; s = "17:3...
1
2009-01-22T23:01:19Z
471,245
<p>A regular expression is the way. But maybe more robustly:</p> <pre><code>re.search(r"load average: (\d+.\d\d), (\d+.\d\d), (\d+.\d\d)$", s).groups() </code></pre> <p>Unless you're doing this really often in a tight loop or some such you needn't worry about performance. Clarity is what's most important. And there I...
0
2009-01-22T23:11:09Z
[ "python", "string", "floating-point" ]
What is the best way to extract load average float values from a string in Python?
471,211
<p>If I have a string such as</p> <p>"17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15, 0.10"</p> <p>what is the best way to extract just the x3 load average values at the end? I have written a regexp that does this but is this the most efficient / fastest method?</p> <pre><code>&gt;&gt;&gt; s = "17:3...
1
2009-01-22T23:01:19Z
12,470,834
<p>Or if you are actually looking for the load averages then in Python 2.3+ you have:</p> <pre><code>import os os.getloadavg() </code></pre>
3
2012-09-18T05:22:11Z
[ "python", "string", "floating-point" ]
Organising a GUI application
471,279
<p>This is going to be a generic question.</p> <p>I am struggling in designing a GUI application, esp. with dealing with interactions between different parts.</p> <p>I don't know how I should deal with shared state. On one hand, shared state is bad, and things should be as explicit as possible. On the other hand, not...
9
2009-01-22T23:25:40Z
471,297
<p>If you've looked at MVC you're probably moving in the right direction. MVC, MVP, Passive View, Supervising Controller. Those are all different ways, each with their own pros and cons, of accomplishing what you're after. I find that Passive View is the "ideal", but it causes you to introduce far too many widgets i...
2
2009-01-22T23:36:00Z
[ "python", "model-view-controller", "user-interface", "architecture", "wxpython" ]
Organising a GUI application
471,279
<p>This is going to be a generic question.</p> <p>I am struggling in designing a GUI application, esp. with dealing with interactions between different parts.</p> <p>I don't know how I should deal with shared state. On one hand, shared state is bad, and things should be as explicit as possible. On the other hand, not...
9
2009-01-22T23:25:40Z
471,307
<p>In MVC, the Model stuff <em>is</em> the shared state of the information.</p> <p>The Control stuff is the shared state of the GUI control settings and responses to mouse-clicks and what-not.</p> <p>Your scripting angle can </p> <p>1) Update the Model objects. This is good. The Control can be "Observers" of the m...
1
2009-01-22T23:40:19Z
[ "python", "model-view-controller", "user-interface", "architecture", "wxpython" ]
Organising a GUI application
471,279
<p>This is going to be a generic question.</p> <p>I am struggling in designing a GUI application, esp. with dealing with interactions between different parts.</p> <p>I don't know how I should deal with shared state. On one hand, shared state is bad, and things should be as explicit as possible. On the other hand, not...
9
2009-01-22T23:25:40Z
505,858
<p>Sorry to jump on this question so late, but nothing, I mean <em>nothing</em> can beat looking at the source of an application that does something similar. (I might recommend something like <a href="http://pida.co.uk" rel="nofollow">http://pida.co.uk</a>, but there are plenty of extensible wx+Python IDEs out there as...
3
2009-02-03T02:39:33Z
[ "python", "model-view-controller", "user-interface", "architecture", "wxpython" ]
Is there a Term::ANSIScreen equivalent for Python?
471,463
<p>Perl has the excellent module <code>Term::ANSIScreen</code> for doing all sorts of fancy cursor movement and terminal color control. I'd like to reimplement a program that's currently in Perl in Python instead, but the terminal ANSI colors are key to its function. Is anyone aware of an equivalent?</p>
3
2009-01-23T00:43:24Z
471,467
<p>While I haven't used it myself, I believe the curses library is commonly used for this:</p> <p><a href="http://docs.python.org/library/curses.html" rel="nofollow">http://docs.python.org/library/curses.html</a></p> <p>And the How-to:</p> <p><a href="http://docs.python.org/howto/curses.html#curses-howto" rel="nofol...
2
2009-01-23T00:48:12Z
[ "python", "perl", "ansi" ]
Is there a Term::ANSIScreen equivalent for Python?
471,463
<p>Perl has the excellent module <code>Term::ANSIScreen</code> for doing all sorts of fancy cursor movement and terminal color control. I'd like to reimplement a program that's currently in Perl in Python instead, but the terminal ANSI colors are key to its function. Is anyone aware of an equivalent?</p>
3
2009-01-23T00:43:24Z
471,512
<p>Here's a <a href="http://code.activestate.com/recipes/574451/" rel="nofollow">cookbook recipe</a> on ActiveState to get you started. It covers colors and positioning.</p> <p><em>[Edit: The pygments code submitted above by Jorge Vargas is a better approach. ]</em></p>
3
2009-01-23T01:19:26Z
[ "python", "perl", "ansi" ]
Is there a Term::ANSIScreen equivalent for Python?
471,463
<p>Perl has the excellent module <code>Term::ANSIScreen</code> for doing all sorts of fancy cursor movement and terminal color control. I'd like to reimplement a program that's currently in Perl in Python instead, but the terminal ANSI colors are key to its function. Is anyone aware of an equivalent?</p>
3
2009-01-23T00:43:24Z
1,977,010
<p>If you only need colors You may want to borrow the implementation from pygments. IMO it's much cleaner than the one from ActiveState</p> <p><a href="http://dev.pocoo.org/hg/pygments-main/file/b2deea5b5030/pygments/console.py">http://dev.pocoo.org/hg/pygments-main/file/b2deea5b5030/pygments/console.py</a></p>
8
2009-12-29T21:18:25Z
[ "python", "perl", "ansi" ]
Is there a Term::ANSIScreen equivalent for Python?
471,463
<p>Perl has the excellent module <code>Term::ANSIScreen</code> for doing all sorts of fancy cursor movement and terminal color control. I'd like to reimplement a program that's currently in Perl in Python instead, but the terminal ANSI colors are key to its function. Is anyone aware of an equivalent?</p>
3
2009-01-23T00:43:24Z
2,257,128
<p>There is also the <a href="http://pypi.python.org/pypi/termcolor" rel="nofollow">termcolor</a> and the <a href="https://github.com/gfxmonk/termstyle" rel="nofollow">termstyle</a> packages. The latter is capable of disabling colour output if stdout is not a terminal.</p> <p>See also <a href="http://stackoverflow.com...
3
2010-02-13T09:56:43Z
[ "python", "perl", "ansi" ]
Any way to override the and operator in Python?
471,546
<p>I tried overriding <code>__and__</code>, but that is for the &amp; operator, not <em>and</em> - the one that I want. Can I override <em>and</em>?</p>
18
2009-01-23T01:36:42Z
471,559
<p>Not really. There's no special method name for the short-circuit logic operators.</p>
1
2009-01-23T01:41:49Z
[ "python" ]
Any way to override the and operator in Python?
471,546
<p>I tried overriding <code>__and__</code>, but that is for the &amp; operator, not <em>and</em> - the one that I want. Can I override <em>and</em>?</p>
18
2009-01-23T01:36:42Z
471,561
<p>You cannot override the <code>and</code>, <code>or</code>, and <code>not</code> boolean operators.</p>
14
2009-01-23T01:42:34Z
[ "python" ]
Any way to override the and operator in Python?
471,546
<p>I tried overriding <code>__and__</code>, but that is for the &amp; operator, not <em>and</em> - the one that I want. Can I override <em>and</em>?</p>
18
2009-01-23T01:36:42Z
471,567
<p>No you can't override <code>and</code> and <code>or</code>. With the behavior that these have in Python (i.e. short-circuiting) they are more like control flow tools than operators and overriding them would be more like overriding <code>if</code> than + or -.</p> <p>You <em>can</em> influence the truth value of you...
29
2009-01-23T01:44:25Z
[ "python" ]
Customizing an Admin form in Django while also using autodiscover
471,550
<p>I want to modify a few tiny details of Django's built-in <code>django.contrib.auth</code> module. Specifically, I want a different form that makes username an email field (and email an alternate email address. (I'd rather not modify <code>auth</code> any more than necessary -- a simple form change <em>seems</em> ...
22
2009-01-23T01:38:17Z
471,661
<p>None of the above. Just use admin.site.unregister(). Here's how I recently added filtering Users on is_active in the admin (<strong>n.b.</strong> is_active filtering is now on the User model by default in Django core; still works here as an example), all DRY as can be:</p> <pre><code>from django.contrib import ad...
43
2009-01-23T02:22:18Z
[ "python", "django", "forms", "django-admin", "customization" ]
Customizing an Admin form in Django while also using autodiscover
471,550
<p>I want to modify a few tiny details of Django's built-in <code>django.contrib.auth</code> module. Specifically, I want a different form that makes username an email field (and email an alternate email address. (I'd rather not modify <code>auth</code> any more than necessary -- a simple form change <em>seems</em> ...
22
2009-01-23T01:38:17Z
472,602
<p>I think it might be easier to do this with a custom auth backend and thus remove the need for a customized ModelAdmin.</p> <p>I did something similar with this snippet: <a href="http://www.djangosnippets.org/snippets/74/" rel="nofollow">http://www.djangosnippets.org/snippets/74/</a></p>
2
2009-01-23T11:30:25Z
[ "python", "django", "forms", "django-admin", "customization" ]
Python/Twisted multiuser server - what is more efficient?
471,660
<p>In Python, if I want my server to scale well CPU-wise, I obviously need to spawn multiple processes. I was wondering which is better (using Twisted):</p> <p>A) The manager process (the one who holds the actual socket connections) puts received packets into a shared queue (the one from the multiprocessing module), a...
2
2009-01-23T02:21:45Z
474,353
<p>I think that B is problematic. The thread would only run on one CPU, and even if it runs a process, the thread is still running. A may be better.</p> <p>It is best to try and measure both in terms of time and see which one is faster and which one scales well. However, I'll reiterate that I highly doubt that B will ...
2
2009-01-23T20:17:42Z
[ "python", "twisted", "multi-user" ]
Python/Twisted multiuser server - what is more efficient?
471,660
<p>In Python, if I want my server to scale well CPU-wise, I obviously need to spawn multiple processes. I was wondering which is better (using Twisted):</p> <p>A) The manager process (the one who holds the actual socket connections) puts received packets into a shared queue (the one from the multiprocessing module), a...
2
2009-01-23T02:21:45Z
500,624
<p>I think that "A" is the answer you want, but you don't have to do it yourself.</p> <p>Have you considered <a href="https://launchpad.net/ampoule" rel="nofollow">ampoule</a>?</p>
1
2009-02-01T11:20:45Z
[ "python", "twisted", "multi-user" ]
Pros and cons of IronPython and IronPython Studio
471,712
<p>We are ready in our company to move everything to Python instead of C#, we are a consulting company and we usually write small projects in C# we don't do huge projects and our work is more based on complex mathematical models not complex software structures. So we believe IronPython is a good platform for us because...
16
2009-01-23T02:49:32Z
471,725
<p>The way you describe things, it sounds like you're company is switching to Python simple for the sake of Python. Is there some specific reason you want to use Python? Is a more dynamic language necessary? Is the functional programming going to help you at all? If you've got a perfectly good working set of tools ...
5
2009-01-23T02:59:23Z
[ "python", "ironpython", "ironpython-studio" ]
Pros and cons of IronPython and IronPython Studio
471,712
<p>We are ready in our company to move everything to Python instead of C#, we are a consulting company and we usually write small projects in C# we don't do huge projects and our work is more based on complex mathematical models not complex software structures. So we believe IronPython is a good platform for us because...
16
2009-01-23T02:49:32Z
472,312
<p>There are a lot of reasons why you want to switch from C# to python, i did this myself recently. After a lot of investigating, here are the reasons why i stick to CPython:</p> <ul> <li>Performance: There are some articles out there stating that there are always cases where ironpython is slower, so if performance is...
8
2009-01-23T09:06:35Z
[ "python", "ironpython", "ironpython-studio" ]
Pros and cons of IronPython and IronPython Studio
471,712
<p>We are ready in our company to move everything to Python instead of C#, we are a consulting company and we usually write small projects in C# we don't do huge projects and our work is more based on complex mathematical models not complex software structures. So we believe IronPython is a good platform for us because...
16
2009-01-23T02:49:32Z
472,355
<p>My company, Resolver Systems, develops what is probably the biggest application written in IronPython yet. (It's called Resolver One, and it's a Pythonic spreadsheet). We are also hosting the Ironclad project (to run CPython extensions under IronPython) and that is going well (we plan to release a beta of Resolver O...
17
2009-01-23T09:29:31Z
[ "python", "ironpython", "ironpython-studio" ]
Way to have compiled python files in a separate folder?
471,928
<p>Is it possible to have Python save the <code>.pyc</code> files to a separate folder location that is in <code>sys.path</code>?</p> <pre><code>/code foo.py foo.pyc bar.py bar.pyc </code></pre> <p>To:</p> <pre><code>/code foo.py bar.py /code_compiled foo.pyc bar.pyc </code></pre> <p>I w...
25
2009-01-23T05:02:12Z
471,985
<p>There is <a href="http://www.python.org/dev/peps/pep-0304/">PEP 304: Controlling Generation of Bytecode Files</a>. Its status is <code>Withdrawn</code> and corresponding <a href="http://bugs.python.org/issue677103">patch</a> rejected. Therefore there might be no direct way to do it. </p> <p>If you don't need source...
16
2009-01-23T05:26:17Z
[ "python", "file", "compiled" ]
Way to have compiled python files in a separate folder?
471,928
<p>Is it possible to have Python save the <code>.pyc</code> files to a separate folder location that is in <code>sys.path</code>?</p> <pre><code>/code foo.py foo.pyc bar.py bar.pyc </code></pre> <p>To:</p> <pre><code>/code foo.py bar.py /code_compiled foo.pyc bar.pyc </code></pre> <p>I w...
25
2009-01-23T05:02:12Z
472,583
<p>"I feel it'd be more organized" Why? How? What are you trying to accomplish?</p> <p>The point of saving the compiler output is to save a tiny bit of load time when the module gets imported. Why make this more complex? If you don't like the .pyc's, then run a "delete all the .pyc's" script periodically.</p> <p...
0
2009-01-23T11:20:21Z
[ "python", "file", "compiled" ]
Way to have compiled python files in a separate folder?
471,928
<p>Is it possible to have Python save the <code>.pyc</code> files to a separate folder location that is in <code>sys.path</code>?</p> <pre><code>/code foo.py foo.pyc bar.py bar.pyc </code></pre> <p>To:</p> <pre><code>/code foo.py bar.py /code_compiled foo.pyc bar.pyc </code></pre> <p>I w...
25
2009-01-23T05:02:12Z
472,862
<p>I agree, distributing your code as an egg is a great way to keep it organized. What could be more organized than a single-file containing all of the code and meta-data you would ever need. Changing the way the bytecode compiler works is only going to cause confusion.</p> <p>If you really do not like the location of...
3
2009-01-23T13:23:25Z
[ "python", "file", "compiled" ]
Way to have compiled python files in a separate folder?
471,928
<p>Is it possible to have Python save the <code>.pyc</code> files to a separate folder location that is in <code>sys.path</code>?</p> <pre><code>/code foo.py foo.pyc bar.py bar.pyc </code></pre> <p>To:</p> <pre><code>/code foo.py bar.py /code_compiled foo.pyc bar.pyc </code></pre> <p>I w...
25
2009-01-23T05:02:12Z
2,036,126
<p>I disagree. The reasons are wrong or at least not well formulated; but the direction is valid. There are good reasons for being able to segregate source code from compiled objects. Here are a few of them (all of them I have run into at one point or another):</p> <ul> <li>embedded device reading off a ROM, but abl...
2
2010-01-10T04:49:45Z
[ "python", "file", "compiled" ]
Way to have compiled python files in a separate folder?
471,928
<p>Is it possible to have Python save the <code>.pyc</code> files to a separate folder location that is in <code>sys.path</code>?</p> <pre><code>/code foo.py foo.pyc bar.py bar.pyc </code></pre> <p>To:</p> <pre><code>/code foo.py bar.py /code_compiled foo.pyc bar.pyc </code></pre> <p>I w...
25
2009-01-23T05:02:12Z
2,985,939
<p>If you're willing to sacrifice bytecode generation altogether for it, there's a command line flag:</p> <pre><code>python -B file_that_imports_others.py </code></pre> <p>Can be put into IDE's build/run preferences</p>
2
2010-06-06T21:19:39Z
[ "python", "file", "compiled" ]
Way to have compiled python files in a separate folder?
471,928
<p>Is it possible to have Python save the <code>.pyc</code> files to a separate folder location that is in <code>sys.path</code>?</p> <pre><code>/code foo.py foo.pyc bar.py bar.pyc </code></pre> <p>To:</p> <pre><code>/code foo.py bar.py /code_compiled foo.pyc bar.pyc </code></pre> <p>I w...
25
2009-01-23T05:02:12Z
11,849,405
<p>There is ongoing pep that will <a href="http://www.python.org/dev/peps/pep-3147/" rel="nofollow">enable building bytecode to magic directory</a>. </p> <p>Basically all python files will be compiled to directory <code>__pythoncache__</code>.</p>
1
2012-08-07T15:40:23Z
[ "python", "file", "compiled" ]
Way to have compiled python files in a separate folder?
471,928
<p>Is it possible to have Python save the <code>.pyc</code> files to a separate folder location that is in <code>sys.path</code>?</p> <pre><code>/code foo.py foo.pyc bar.py bar.pyc </code></pre> <p>To:</p> <pre><code>/code foo.py bar.py /code_compiled foo.pyc bar.pyc </code></pre> <p>I w...
25
2009-01-23T05:02:12Z
15,776,555
<p>Since Python 3.2 has been implemented <a href="http://www.python.org/dev/peps/pep-3147/" rel="nofollow">PEP 3147</a>: this means that all .pyc files are generated inside a <strong>__<em>pycache</em>__</strong> directory (there will be a <strong>__<em>pycache</em>__</strong> directory for each directory where you hav...
1
2013-04-02T23:54:50Z
[ "python", "file", "compiled" ]
Way to have compiled python files in a separate folder?
471,928
<p>Is it possible to have Python save the <code>.pyc</code> files to a separate folder location that is in <code>sys.path</code>?</p> <pre><code>/code foo.py foo.pyc bar.py bar.pyc </code></pre> <p>To:</p> <pre><code>/code foo.py bar.py /code_compiled foo.pyc bar.pyc </code></pre> <p>I w...
25
2009-01-23T05:02:12Z
16,476,434
<p><em>In the dark and ancient days of 2003, PEP 304 came forth to challenge this problem. Its patch was found wanting. Environment variable platform dependencies and version skews ripped it to shreds and left its bits scattered across the wastelands.</em></p> <p><em>After years of suffering, a new challenger rose i...
7
2013-05-10T06:30:04Z
[ "python", "file", "compiled" ]
Usage of __slots__?
472,000
<p>What is the purpose of <a href="https://docs.python.org/3/reference/datamodel.html#slots"><code>__slots__</code></a> in Python — especially with respect to when would I want to use it and when not?</p>
306
2009-01-23T05:37:23Z
472,017
<p>You would want to use <code>__slots__</code> if you are going to instantiate a lot (hundreds, thousands) of objects of the same class. <code>__slots__</code> only exists as a memory optimization tool.</p> <p>It's highly discouraged to use <code>__slots__</code> for constraining attribute creation, and in general yo...
105
2009-01-23T05:50:21Z
[ "python" ]
Usage of __slots__?
472,000
<p>What is the purpose of <a href="https://docs.python.org/3/reference/datamodel.html#slots"><code>__slots__</code></a> in Python — especially with respect to when would I want to use it and when not?</p>
306
2009-01-23T05:37:23Z
472,024
<p>Quoting <a href="http://code.activestate.com/lists/python-list/531365/">Jacob Hallen</a>:</p> <blockquote> <p>The proper use of <code>__slots__</code> is to save space in objects. Instead of having a dynamic dict that allows adding attributes to objects at anytime, there is a static structure which does not a...
243
2009-01-23T05:54:46Z
[ "python" ]
Usage of __slots__?
472,000
<p>What is the purpose of <a href="https://docs.python.org/3/reference/datamodel.html#slots"><code>__slots__</code></a> in Python — especially with respect to when would I want to use it and when not?</p>
306
2009-01-23T05:37:23Z
472,570
<p>You have — essentially — no use for <code>__slots__</code>. </p> <p>For the time when you think you might need <code>__slots__</code>, you actually want to use <strong>Lightweight</strong> or <strong>Flyweight</strong> design patterns. These are cases when you no longer want to use purely Python objects. Inste...
2
2009-01-23T11:15:19Z
[ "python" ]
Usage of __slots__?
472,000
<p>What is the purpose of <a href="https://docs.python.org/3/reference/datamodel.html#slots"><code>__slots__</code></a> in Python — especially with respect to when would I want to use it and when not?</p>
306
2009-01-23T05:37:23Z
472,899
<p>Each python object has a <code>__dict__</code> atttribute which is a dictionary containing all other attributes. e.g. when you type <code>self.attr</code> python is actually doing <code>self.__dict__['attr']</code>. As you can imagine using a dictionary to store attribute takes some extra space &amp; time for access...
48
2009-01-23T13:38:21Z
[ "python" ]
Usage of __slots__?
472,000
<p>What is the purpose of <a href="https://docs.python.org/3/reference/datamodel.html#slots"><code>__slots__</code></a> in Python — especially with respect to when would I want to use it and when not?</p>
306
2009-01-23T05:37:23Z
13,547,906
<p>Slots are very useful for library calls to eliminate the "named method dispatch" when making function calls. This is mentioned in the SWIG <a href="http://www.swig.org/Doc2.0/Python.html#Python_builtin_types">documentation</a>. For high performance libraries that want to reduce function overhead for commonly calle...
10
2012-11-25T03:06:11Z
[ "python" ]