title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
Splitting up a list into parts of balanced lengths
1,380,162
<p>I need an algorithm which given a list <code>L</code> and a number <code>N</code>, returns a list of <code>N</code> smaller lists where the sublists are "balanced". Examples:</p> <pre><code>algo(range(1, 8), 3) -&gt; [[1,2,3], [4,5], [6,7]] algo(range(1, 6), 4) -&gt; [[1,2], [3], [4], [5]] algo(range(1, 12), 5) ...
6
2009-09-04T16:04:41Z
1,381,193
<p>Here's a tribute to functional lovers:</p> <pre><code>def algo(l, n): if n == 1: return [l] q, r = divmod(len(l),n) if r: q += 1 return [l[:q]] + algo(l[q:], n - 1) </code></pre> <p>This one is a little bit smaller:</p> <pre><code>def algo(l, n): k = l[:] q, r = divmod(len(l),n) return...
0
2009-09-04T19:44:04Z
[ "python", "algorithm" ]
Splitting up a list into parts of balanced lengths
1,380,162
<p>I need an algorithm which given a list <code>L</code> and a number <code>N</code>, returns a list of <code>N</code> smaller lists where the sublists are "balanced". Examples:</p> <pre><code>algo(range(1, 8), 3) -&gt; [[1,2,3], [4,5], [6,7]] algo(range(1, 6), 4) -&gt; [[1,2], [3], [4], [5]] algo(range(1, 12), 5) ...
6
2009-09-04T16:04:41Z
1,674,267
<p>Bit late to the party, but...</p> <pre><code>def algo(l, n): return [l[-(-len(l)*i//n):-(-len(l)*(i+1)//n)] for i in range(n)] </code></pre> <p>Use / instead of // in older versions of Python.</p>
0
2009-11-04T14:51:52Z
[ "python", "algorithm" ]
Set Snow Leopard to use python 2.5 rather than 2.6
1,380,281
<p>I just upgraded to Snow Leopard and I'm trying to get it to use the old python 2.5 install. I had with all my modules in it. Does anyone know how to set the default python install to 2.5?</p>
6
2009-09-04T16:27:01Z
1,380,314
<p>You want <a href="http://python%5Fselect.darwinports.com/" rel="nofollow"><code>python_select</code></a>.</p> <blockquote> <p>Description: Switch the default python interpreter</p> </blockquote>
0
2009-09-04T16:36:01Z
[ "python", "osx", "osx-snow-leopard" ]
Set Snow Leopard to use python 2.5 rather than 2.6
1,380,281
<p>I just upgraded to Snow Leopard and I'm trying to get it to use the old python 2.5 install. I had with all my modules in it. Does anyone know how to set the default python install to 2.5?</p>
6
2009-09-04T16:27:01Z
1,380,328
<p>I worked this out - if you have this problem open a terminal and type:</p> <pre><code>defaults write com.apple.versioner.python Version 2.5 </code></pre>
8
2009-09-04T16:38:04Z
[ "python", "osx", "osx-snow-leopard" ]
Can I add parameters to a python property to reduce code duplication?
1,380,566
<p>I have a the following class:</p> <pre><code> class Vector(object): def __init__(self, x=0, y=0, z=0): self.x = x self.y = y self.z = z def _getx(self): return self._x def _setx(self, value): self._x = float(value) x = property(_getx, _setx) def _gety(se...
4
2009-09-04T17:30:12Z
1,380,594
<p>Not tested, but this should work:</p> <pre><code>def force_float(name): def get(self): return getattr(self, name) def set(self, x): setattr(self, name, float(x)) return property(get, set) class Vector(object): x = force_float("_x") y = force_float("_y") # etc </code></pre>
0
2009-09-04T17:36:23Z
[ "python" ]
Can I add parameters to a python property to reduce code duplication?
1,380,566
<p>I have a the following class:</p> <pre><code> class Vector(object): def __init__(self, x=0, y=0, z=0): self.x = x self.y = y self.z = z def _getx(self): return self._x def _setx(self, value): self._x = float(value) x = property(_getx, _setx) def _gety(se...
4
2009-09-04T17:30:12Z
1,380,598
<p>Sure, make a custom descriptor as per the concepts clearly explained in <a href="http://users.rcn.com/python/download/Descriptor.htm">this doc</a>:</p> <pre><code>class JonProperty(object): def __init__(self, name): self.name = name def __get__(self, obj, objtype): return getattr(obj, self....
17
2009-09-04T17:37:19Z
[ "python" ]
Can I add parameters to a python property to reduce code duplication?
1,380,566
<p>I have a the following class:</p> <pre><code> class Vector(object): def __init__(self, x=0, y=0, z=0): self.x = x self.y = y self.z = z def _getx(self): return self._x def _setx(self, value): self._x = float(value) x = property(_getx, _setx) def _gety(se...
4
2009-09-04T17:30:12Z
1,381,393
<p>Why not just write this?</p> <pre><code>class Vector(object): def __init__(self, x=0, y=0, z=0): self.x = x self.y = y self.z = z </code></pre> <p>If your getters and setters are a carryover in practice from something like Java, then DO NOT WRITE THEM. Just expose the attributes x, y, ...
2
2009-09-04T20:30:11Z
[ "python" ]
Slickest REPL console in any language
1,380,592
<p>Many languages of REPL consoles with additional features like autocomplete and intellisense. For instance, iPython, Mathematica, and PyCrust all make some effort to go beyond a basic read eval loop. REPLs are particularly useful in languages where interactive exploration is very important, such as Matlab or R.</p>...
3
2009-09-04T17:35:46Z
1,380,716
<p>I really like Safari's Web Inspector Javascript console. Specifically:</p> <ul> <li>Collapsible interactive object hierarchies</li> <li><code>sprintf</code>-style logging</li> <li>Pretty-printing of closures, allowing you to peer into the internals of anonymous functions</li> <li>Auto-complete / hinting of object ...
4
2009-09-04T18:03:29Z
[ "python", "matlab", "console" ]
Slickest REPL console in any language
1,380,592
<p>Many languages of REPL consoles with additional features like autocomplete and intellisense. For instance, iPython, Mathematica, and PyCrust all make some effort to go beyond a basic read eval loop. REPLs are particularly useful in languages where interactive exploration is very important, such as Matlab or R.</p>...
3
2009-09-04T17:35:46Z
1,380,780
<p>The command window in MATLAB 7.8.0 (R2009a) has a few nice features:</p> <ul> <li><strong>Tabbed-completion:</strong> You can type part of a variable or function name, then hit tab to get a selectable list of all possible variable or function completions. This also works when typing part of a string, which will bri...
3
2009-09-04T18:17:36Z
[ "python", "matlab", "console" ]
Slickest REPL console in any language
1,380,592
<p>Many languages of REPL consoles with additional features like autocomplete and intellisense. For instance, iPython, Mathematica, and PyCrust all make some effort to go beyond a basic read eval loop. REPLs are particularly useful in languages where interactive exploration is very important, such as Matlab or R.</p>...
3
2009-09-04T17:35:46Z
1,380,870
<p>Common Lisp and emacs with SLIME. All you could really want, think of, dream of, and then some.</p>
5
2009-09-04T18:39:20Z
[ "python", "matlab", "console" ]
Slickest REPL console in any language
1,380,592
<p>Many languages of REPL consoles with additional features like autocomplete and intellisense. For instance, iPython, Mathematica, and PyCrust all make some effort to go beyond a basic read eval loop. REPLs are particularly useful in languages where interactive exploration is very important, such as Matlab or R.</p>...
3
2009-09-04T17:35:46Z
1,380,924
<p>I think <a href="http://www.softpedia.com/screenshots/Windows-PowerShell%5F2.png" rel="nofollow">PowerShell</a> wins without any doubt.</p>
0
2009-09-04T18:48:19Z
[ "python", "matlab", "console" ]
How to get list opened windows in PyGTK or GTK in Ubuntu?
1,380,784
<p>How to get list opened windows in PyGTK or GTK or other programming language? in Ubuntu?</p> <p><strong>edit:</strong></p> <p>i want get list paths opened directories on desktop!</p>
4
2009-09-04T18:18:40Z
1,380,867
<p>From <a href="http://library.gnome.org/devel/pygtk/stable/class-gdkwindow.html#function-gdk--window-get-toplevels" rel="nofollow">the PyGTK reference</a>:</p> <blockquote> <p><code>gtk.gdk.window_get_toplevels()</code></p> <p>The <code>gtk.gdk.window_get_toplevels()</code> function returns a list of all ...
2
2009-09-04T18:37:30Z
[ "python", "ubuntu", "gtk", "pygtk", "window-management" ]
How to get list opened windows in PyGTK or GTK in Ubuntu?
1,380,784
<p>How to get list opened windows in PyGTK or GTK or other programming language? in Ubuntu?</p> <p><strong>edit:</strong></p> <p>i want get list paths opened directories on desktop!</p>
4
2009-09-04T18:18:40Z
1,380,909
<p>I really don't know how to check if a window is a GTK one. But if you want to check how many windows are currently open try "wmctrl -l". Install it first of course.</p>
1
2009-09-04T18:46:50Z
[ "python", "ubuntu", "gtk", "pygtk", "window-management" ]
How to get list opened windows in PyGTK or GTK in Ubuntu?
1,380,784
<p>How to get list opened windows in PyGTK or GTK or other programming language? in Ubuntu?</p> <p><strong>edit:</strong></p> <p>i want get list paths opened directories on desktop!</p>
4
2009-09-04T18:18:40Z
1,380,988
<p>You probably want to use libwnck:</p> <p><a href="http://library.gnome.org/devel/libwnck/stable/">http://library.gnome.org/devel/libwnck/stable/</a></p> <p>I believe there are python bindings in python-gnome or some similar package.</p> <p>Once you have the GTK+ mainloop running, you can do the following:</p> <p...
8
2009-09-04T19:01:40Z
[ "python", "ubuntu", "gtk", "pygtk", "window-management" ]
How to get list opened windows in PyGTK or GTK in Ubuntu?
1,380,784
<p>How to get list opened windows in PyGTK or GTK or other programming language? in Ubuntu?</p> <p><strong>edit:</strong></p> <p>i want get list paths opened directories on desktop!</p>
4
2009-09-04T18:18:40Z
1,381,472
<p>Parsing command line output is usually not the best way, you are dependent on that programs output not changing, which could vary from version or platfrom. Here is how to do it using Xlib:</p> <pre><code>import Xlib.display screen = Xlib.display.Display().screen() root_win = screen.root window_names = [] for wind...
2
2009-09-04T20:47:15Z
[ "python", "ubuntu", "gtk", "pygtk", "window-management" ]
How to get list opened windows in PyGTK or GTK in Ubuntu?
1,380,784
<p>How to get list opened windows in PyGTK or GTK or other programming language? in Ubuntu?</p> <p><strong>edit:</strong></p> <p>i want get list paths opened directories on desktop!</p>
4
2009-09-04T18:18:40Z
4,975,656
<p>For whatever reason, I can't post a comment, but I'd like to add this as an addendum to Sandy's answer. </p> <p>Here's a chunk of code that lists the current windows on the console:</p> <pre><code>import pygtk pygtk.require('2.0') import gtk, wnck if __name__ == "__main__": default = wnck.screen_get_default()...
4
2011-02-12T01:15:48Z
[ "python", "ubuntu", "gtk", "pygtk", "window-management" ]
How to get list opened windows in PyGTK or GTK in Ubuntu?
1,380,784
<p>How to get list opened windows in PyGTK or GTK or other programming language? in Ubuntu?</p> <p><strong>edit:</strong></p> <p>i want get list paths opened directories on desktop!</p>
4
2009-09-04T18:18:40Z
16,703,307
<p>Welcome to 2013! Here's the code using <code>Wnck</code> and its modern GObject Introspection libraries instead of the now deprecated PyGTK method. You may also check <a href="http://stackoverflow.com/a/16703115/624066">my other answer about wnck</a>:</p> <pre><code>from gi.repository import Gtk, Wnck Gtk.init([])...
7
2013-05-22T23:37:06Z
[ "python", "ubuntu", "gtk", "pygtk", "window-management" ]
Is packaging scripts as executables a solution for comercial applications?
1,380,852
<p>What happens when you package a script as an executable? Is this a good way to distribute commercial applications? I remember I read something a long time ago that when you package scripts as executables, at runtime the exe decompresses the scripts to a temporary directory where they get ran.</p> <p>If it's like th...
0
2009-09-04T18:34:54Z
1,380,877
<p>With Python (e.g. <a href="http://www.pyinstaller.org/" rel="nofollow">pyinstaller</a> -- be sure to get the SVN version, the "released" one is WAY out of date -- or <a href="http://www.py2exe.org/" rel="nofollow">py2exe</a>) you can package bytecode. Sure, it can be "reverse compiled", just like Java bytecode or .N...
3
2009-09-04T18:40:14Z
[ "python", "ruby", "executable" ]
Is packaging scripts as executables a solution for comercial applications?
1,380,852
<p>What happens when you package a script as an executable? Is this a good way to distribute commercial applications? I remember I read something a long time ago that when you package scripts as executables, at runtime the exe decompresses the scripts to a temporary directory where they get ran.</p> <p>If it's like th...
0
2009-09-04T18:34:54Z
1,381,231
<p>py2exe is great, I've used it before and it works just fine for encapsulating a program so it can run on other computers, even those lacking python. Like Alex said, it can be disassembled, but then so can C++ binaries. It's just a question of how much work the person is has to put into it. If someone has physical ac...
2
2009-09-04T19:51:39Z
[ "python", "ruby", "executable" ]
Is packaging scripts as executables a solution for comercial applications?
1,380,852
<p>What happens when you package a script as an executable? Is this a good way to distribute commercial applications? I remember I read something a long time ago that when you package scripts as executables, at runtime the exe decompresses the scripts to a temporary directory where they get ran.</p> <p>If it's like th...
0
2009-09-04T18:34:54Z
1,381,289
<p>Creating an application and creating a commercial software business are two distinct things. </p> <p>At my job we have a commercial application developed in Ruby on Rails that is installed at client sites; no obfuscation or encryption applied. </p> <p>There is just so much more going on at a business level: suppor...
3
2009-09-04T20:02:13Z
[ "python", "ruby", "executable" ]
Is packaging scripts as executables a solution for comercial applications?
1,380,852
<p>What happens when you package a script as an executable? Is this a good way to distribute commercial applications? I remember I read something a long time ago that when you package scripts as executables, at runtime the exe decompresses the scripts to a temporary directory where they get ran.</p> <p>If it's like th...
0
2009-09-04T18:34:54Z
1,381,595
<p>This has been discussed before here for the Python case and some answers are valid for Ruby too. I highly recommend you to read a previous question in this site titled: <a href="http://stackoverflow.com/questions/261638/how-do-i-protect-python-code">How do I protect python code?</a>. Pay attention to the first three...
1
2009-09-04T21:21:13Z
[ "python", "ruby", "executable" ]
Add Variables to Tuple
1,380,860
<p>I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. </p> <p><strong>What I am Doing</strong>: I am taking information from the user and store it in variables. Can I add these variables into a tuple?...
128
2009-09-04T18:36:18Z
1,380,872
<p>It's as easy as the following:</p> <pre><code>info_1 = "one piece of info" info_2 = "another piece" vars = (info_1, info_2) # 'vars' is now a tuple with the values ("info_1", "info_2") </code></pre> <p>However, tuples in Python are <em>immutable</em>, so you cannot append variables to a tuple once it is created.</...
5
2009-09-04T18:39:30Z
[ "python", "tuples" ]
Add Variables to Tuple
1,380,860
<p>I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. </p> <p><strong>What I am Doing</strong>: I am taking information from the user and store it in variables. Can I add these variables into a tuple?...
128
2009-09-04T18:36:18Z
1,380,873
<p>I'm pretty sure the syntax for this in python is:</p> <pre><code>user_input1 = raw_input("Enter Name: ") user_input2 = raw_input("Enter Value: ") info = (user_input1, user_input2) </code></pre> <p>once set, tuples cannot be changed.</p>
0
2009-09-04T18:39:31Z
[ "python", "tuples" ]
Add Variables to Tuple
1,380,860
<p>I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. </p> <p><strong>What I am Doing</strong>: I am taking information from the user and store it in variables. Can I add these variables into a tuple?...
128
2009-09-04T18:36:18Z
1,380,875
<p>Tuples are immutable; you can't change which variables they contain after construction. However, you can concatenate or slice them to form new tuples:</p> <pre><code>a = (1, 2, 3) b = a + (4, 5, 6) c = b[1:] </code></pre> <p>And, of course, build them from existing values:</p> <pre><code> name = "Joe" age = ...
160
2009-09-04T18:39:39Z
[ "python", "tuples" ]
Add Variables to Tuple
1,380,860
<p>I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. </p> <p><strong>What I am Doing</strong>: I am taking information from the user and store it in variables. Can I add these variables into a tuple?...
128
2009-09-04T18:36:18Z
1,380,915
<p>As other answers have noted, you cannot change an existing tuple, but you can always create a new tuple (which may take some or all items from existing tuples and/or other sources).</p> <p>For example, if all the items of interest are in scalar variables and you know the names of those variables:</p> <pre><code>de...
1
2009-09-04T18:47:11Z
[ "python", "tuples" ]
Add Variables to Tuple
1,380,860
<p>I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. </p> <p><strong>What I am Doing</strong>: I am taking information from the user and store it in variables. Can I add these variables into a tuple?...
128
2009-09-04T18:36:18Z
1,381,304
<p><strong>" once the info is added to the DB, should I delete the tuple? i mean i dont need the tuple anymore."</strong></p> <p>No.</p> <p>Generally, there's no reason to delete anything. There are some special cases for deleting, but they're very, very rare.</p> <p>Simply define a narrow scope (i.e., a function d...
2
2009-09-04T20:06:31Z
[ "python", "tuples" ]
Add Variables to Tuple
1,380,860
<p>I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. </p> <p><strong>What I am Doing</strong>: I am taking information from the user and store it in variables. Can I add these variables into a tuple?...
128
2009-09-04T18:36:18Z
8,538,676
<p>You can start with a blank tuple with something like t = (). You can add with +, but you have to add another tuple. If you want to add a single element, make it a singleton: t = t + (element,). You can add a tuple of multiple elements with or without that trailing comma.</p> <pre><code>&gt;&gt;&gt; t = () &gt;&gt;&...
111
2011-12-16T18:43:21Z
[ "python", "tuples" ]
Add Variables to Tuple
1,380,860
<p>I am learning Python and creating a database connection. While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB. </p> <p><strong>What I am Doing</strong>: I am taking information from the user and store it in variables. Can I add these variables into a tuple?...
128
2009-09-04T18:36:18Z
23,530,113
<p>Another tactic not yet mentioned is using appending to a list, and then converting the list to a tuple at the end:</p> <pre><code>mylist = [] for x in range(5): mylist.append(x) mytuple = tuple(mylist) print mytuple </code></pre> <p>returns</p> <pre><code>(0, 1, 2, 3, 4) </code></pre> <p>I sometimes use this...
16
2014-05-07T23:20:59Z
[ "python", "tuples" ]
What lightweight python library for simple scientific visualization in 3D
1,380,861
<p>I am writing a program in python to experiment an academic idea. Look at a resultant image the program generates:</p> <p><img src="http://imgur.com/zgdiP.png" alt="leaf" /></p> <p>The thick skeleton lines in the middle of the leaf is what need to be visualized. Every segment of the skeleton lines has a value ass...
4
2009-09-04T18:36:35Z
1,380,886
<p>Have you looked at <a href="http://code.enthought.com/projects/mayavi/" rel="nofollow">mayavi</a>? Don't know if it meets your definition of "lightweight", but it does seem popular and reasonably easy to use for its power.</p>
3
2009-09-04T18:41:53Z
[ "python", "3d", "geometry", "visualization" ]
What lightweight python library for simple scientific visualization in 3D
1,380,861
<p>I am writing a program in python to experiment an academic idea. Look at a resultant image the program generates:</p> <p><img src="http://imgur.com/zgdiP.png" alt="leaf" /></p> <p>The thick skeleton lines in the middle of the leaf is what need to be visualized. Every segment of the skeleton lines has a value ass...
4
2009-09-04T18:36:35Z
1,381,066
<p>If you want lightweight, then you can use <a href="http://pyopengl.sourceforge.net/" rel="nofollow">PyOpenGL</a> to just wrap OpenGL calls in python directly. This is probably the lightest-weight option.</p> <p>If you want lots of features, I'd recommend using <a href="http://vtk.org/" rel="nofollow">VTK</a>. It'...
4
2009-09-04T19:18:31Z
[ "python", "3d", "geometry", "visualization" ]
Python list vs. MySQL Select performance
1,380,917
<p>I have a large list with 15k entries in a MySQL table from which I need to select a few items, many times. For example, I might want all entries with a number field between 1 and 10.</p> <p>In SQL this would be easy:</p> <pre><code>SELECT text FROM table WHERE number&gt;=1 AND number&lt;10; </code></pre> <p>If I ...
0
2009-09-04T18:47:16Z
1,380,942
<p>Read all the data into Python (from the numbers you mention it should handily fit in memory), say into a variable <code>pylist</code> as you say, then prep an auxiliary data structure as follows:</p> <pre><code>import collections d = collections.defaultdict(list) for text, number in pylist: d[number].append(text)...
0
2009-09-04T18:51:39Z
[ "python", "mysql" ]
Python list vs. MySQL Select performance
1,380,917
<p>I have a large list with 15k entries in a MySQL table from which I need to select a few items, many times. For example, I might want all entries with a number field between 1 and 10.</p> <p>In SQL this would be easy:</p> <pre><code>SELECT text FROM table WHERE number&gt;=1 AND number&lt;10; </code></pre> <p>If I ...
0
2009-09-04T18:47:16Z
1,381,149
<p>It is difficult to answer without actual performance, but my gut feeling is that it would be better to go for the SQL with bind variables (I am not MySQL expert, but in this case query syntax <a href="http://www.kitebird.com/articles/pydbapi.html" rel="nofollow">should be</a> something like %varname). </p> <p>The ...
0
2009-09-04T19:34:22Z
[ "python", "mysql" ]
Python list vs. MySQL Select performance
1,380,917
<p>I have a large list with 15k entries in a MySQL table from which I need to select a few items, many times. For example, I might want all entries with a number field between 1 and 10.</p> <p>In SQL this would be easy:</p> <pre><code>SELECT text FROM table WHERE number&gt;=1 AND number&lt;10; </code></pre> <p>If I ...
0
2009-09-04T18:47:16Z
1,381,413
<p>Simply define an index over <code>number</code> in your database, then the database can generate the result sets instantly. Plus it can do some calculations on these sets too, if that is your next step. </p> <p>Databases are actually great at such queries, I'd let it do its job before trying something else.</p>
1
2009-09-04T20:34:17Z
[ "python", "mysql" ]
Python list vs. MySQL Select performance
1,380,917
<p>I have a large list with 15k entries in a MySQL table from which I need to select a few items, many times. For example, I might want all entries with a number field between 1 and 10.</p> <p>In SQL this would be easy:</p> <pre><code>SELECT text FROM table WHERE number&gt;=1 AND number&lt;10; </code></pre> <p>If I ...
0
2009-09-04T18:47:16Z
1,381,435
<p>It's certainly going to be much faster to pull the data into memory than run ~15,000 queries.</p> <p>My advice is to make sure the SQL query sorts the data by <code>number</code>. If the data is sorted, you can use the very fast lookup methods in the <code>bisect</code> standard library module to find indexes.</p>...
1
2009-09-04T20:39:33Z
[ "python", "mysql" ]
Logging multithreaded processes in python
1,380,985
<p>I was thinking of using the logging module to log all events to one file. The number of threads should be constant from start to finish, but if one thread fails, I'd like to just log that and continue on. What's a simple way of accomplishing this? Thanks!</p>
3
2009-09-04T19:01:13Z
1,381,468
<p>Not entirely sure what you mean by "one thread fails", but if by "fail" you mean that an exception propagates all the way up to the top function of the thread, then you can wrap every thread's top function (e.g. in a decorator) to catch any exception, log whatever you wish, and re-raise. The <code>logging</code> mo...
7
2009-09-04T20:46:22Z
[ "python", "multithreading", "logging" ]
How do I wrap this C function, with multiple arguments, with ctypes?
1,381,016
<p>I have the function prototype here:</p> <pre><code>extern "C" void __stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); </code></pre> <p>I need to write some python to access this function that is in a DLL. I have loaded the DLL, but each of the double* is actually pointing to a var...
0
2009-09-04T19:08:26Z
1,381,031
<p>I haven't looked at ctypes too much, but try using a numpy array of the right type. If that doesn't just automatically work, they also have a ctypes attribute that should contain a pointer to the data.</p>
1
2009-09-04T19:12:23Z
[ "python", "arrays", "pointers", "return", "ctypes" ]
How do I wrap this C function, with multiple arguments, with ctypes?
1,381,016
<p>I have the function prototype here:</p> <pre><code>extern "C" void __stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); </code></pre> <p>I need to write some python to access this function that is in a DLL. I have loaded the DLL, but each of the double* is actually pointing to a var...
0
2009-09-04T19:08:26Z
1,381,494
<p>To make an array with, say, <code>n</code> doubles:</p> <pre><code>arr7 = ctypes.c_double * `n` x = arr7() </code></pre> <p>and pass <code>x</code> to your function where it wants a <code>double*</code>. Or if you need to initialize <code>x</code> as you make it:</p> <pre><code>x = arr7(i*0.1 for i in xrange(7)...
1
2009-09-04T20:51:20Z
[ "python", "arrays", "pointers", "return", "ctypes" ]
Wrong Mac OS X framework gets loaded
1,381,177
<p>I've compiled a Python module using my own Qt4 library located in <code>~/opt/qt-4.6.0/</code>, but when I try to import that module, the dynamic libraries that get loaded are from my MacPorts Qt4 installation. </p> <pre><code>$ /opt/local/bin/python2.6 &gt;&gt;&gt; import vtk objc[58041]: Class QMacSoundDelegate i...
2
2009-09-04T19:40:30Z
1,381,353
<p>Try setting the <code>DYLD_LIBRARY_PATH</code> to put your libraries in <code>~/opt/qt/...</code> before the MacPorts' libraries before invoking python (take a look at <code>~/.profile</code> for an example of how to do this if you don't know; MacPorts does the exact same thing to put its libraries on the <code>DYLD...
2
2009-09-04T20:18:04Z
[ "python", "osx", "qt4", "macports", "vtk" ]
Wrong Mac OS X framework gets loaded
1,381,177
<p>I've compiled a Python module using my own Qt4 library located in <code>~/opt/qt-4.6.0/</code>, but when I try to import that module, the dynamic libraries that get loaded are from my MacPorts Qt4 installation. </p> <pre><code>$ /opt/local/bin/python2.6 &gt;&gt;&gt; import vtk objc[58041]: Class QMacSoundDelegate i...
2
2009-09-04T19:40:30Z
1,381,647
<p>Ok, after Barry Wark pointed me to <code>dyld(1)</code>, the man page described a number of variables that I could set.</p> <p>The first hint came from setting the environment variable <code>DYLD_PRINT_LIBRARIES</code>, so I could see what libraries were being loaded.</p> <pre><code>$ DYLD_PRINT_LIBRARIES=1 python...
2
2009-09-04T21:33:33Z
[ "python", "osx", "qt4", "macports", "vtk" ]
Manipulating time in python
1,381,315
<p>In the code shown below, I need to manipulate the time var in python to display a date/time stamp in python to represent that delay.</p> <p>For example, when the user enters the delay time in hours, I need to set the jcarddeliver var to update itself with the value of the current date/time + delay.</p> <p>Also it ...
1
2009-09-04T20:09:42Z
1,381,347
<p>" i need to set the jcarddeliver var to update itself with the value of the current date/time + delay"</p> <p>How about reformulating this to</p> <p><code>jcarddeliver</code> should be the current date-time plus the delay.</p> <p>The "update itself" isn't perfectly sensible.</p> <p>Try the following:</p> <ol> <...
0
2009-09-04T20:16:27Z
[ "python", "datetime" ]
Manipulating time in python
1,381,315
<p>In the code shown below, I need to manipulate the time var in python to display a date/time stamp in python to represent that delay.</p> <p>For example, when the user enters the delay time in hours, I need to set the jcarddeliver var to update itself with the value of the current date/time + delay.</p> <p>Also it ...
1
2009-09-04T20:09:42Z
1,381,372
<p>The result of <code>time.time()</code> is a floating point value of the number of seconds since the Epoch. You can add seconds to this value and use <code>time.localtime()</code>, <code>time.ctime()</code> and other functions to get the result in various forms:</p> <pre><code>&gt;&gt;&gt; now = time.time() &gt;&gt;...
1
2009-09-04T20:23:30Z
[ "python", "datetime" ]
Manipulating time in python
1,381,315
<p>In the code shown below, I need to manipulate the time var in python to display a date/time stamp in python to represent that delay.</p> <p>For example, when the user enters the delay time in hours, I need to set the jcarddeliver var to update itself with the value of the current date/time + delay.</p> <p>Also it ...
1
2009-09-04T20:09:42Z
1,381,769
<p>You could try the datetime module, e.g.</p> <pre><code>import datetime now = datetime.datetime.now() delay = float (raw_input ("enter delay (s): ")) dt = datetime.timedelta (seconds=delay) then = now + dt print now print then </code></pre>
3
2009-09-04T22:17:54Z
[ "python", "datetime" ]
Manipulating time in python
1,381,315
<p>In the code shown below, I need to manipulate the time var in python to display a date/time stamp in python to represent that delay.</p> <p>For example, when the user enters the delay time in hours, I need to set the jcarddeliver var to update itself with the value of the current date/time + delay.</p> <p>Also it ...
1
2009-09-04T20:09:42Z
23,622,496
<p>Try this:</p> <pre><code>now = time.time() then = now + 365*24*3600 print time.strftime('%Y-%m-%d', time.localtime(then)) </code></pre>
0
2014-05-13T03:50:25Z
[ "python", "datetime" ]
How to aggregate all attributes of a hierarchy of classes?
1,381,333
<p>There is a hierchy of classes. Each class <em>may</em> define a class variable (to be specific, it's a dictionary), all of which have the same variable name. I'd like the very root class to be able to somehow access all of these variables (i.e. all the dictionaries joined together), given an instance of a child clas...
0
2009-09-04T20:13:34Z
1,381,443
<p>As long as you're using new-style classes (i.e., <code>object</code> or some other built-in type is the "deepest ancestor"), <code>__mro__</code> is what you're looking for. For example, given:</p> <pre><code>&gt;&gt;&gt; class Root(object): ... d = {'za': 23} ... &gt;&gt;&gt; class Trunk(Root): ... d = {'ki'...
1
2009-09-04T20:41:30Z
[ "python", "class", "hierarchy" ]
Easiest way to write a Python program with access to Django database functionality
1,381,346
<p>I have a website which fetches information from RSS feeds periodically (well, currently manually, and this is my problem). This is currently implemented as a normal Django view, which isn't very nice in my opinion. I'd like to have a Python program which is run using a cronjob instead of manually visiting the corr...
3
2009-09-04T20:16:20Z
1,381,395
<pre><code>from django.core.management import setup_environ from django.core.mail import EmailMultiAlternatives, send_mail from django.contrib.auth.models import User import settings from my.app.models import * setup_environ(settings) </code></pre> <p>This was how I did it for a cron that emailed parties daily updat...
13
2009-09-04T20:30:39Z
[ "python", "django", "django-models", "django-orm" ]
Easiest way to write a Python program with access to Django database functionality
1,381,346
<p>I have a website which fetches information from RSS feeds periodically (well, currently manually, and this is my problem). This is currently implemented as a normal Django view, which isn't very nice in my opinion. I'd like to have a Python program which is run using a cronjob instead of manually visiting the corr...
3
2009-09-04T20:16:20Z
1,381,402
<p>You want something like this in your crontab:</p> <pre><code>PYTHONPATH=/path/to/your/project DJANGO_SETTINGS_MODULE=settings myscript.py </code></pre> <p>And then your python script should start with this:</p> <pre><code>#!/usr/bin/env python from django.conf import settings </code></pre> <p>From there, you sh...
0
2009-09-04T20:31:45Z
[ "python", "django", "django-models", "django-orm" ]
Easiest way to write a Python program with access to Django database functionality
1,381,346
<p>I have a website which fetches information from RSS feeds periodically (well, currently manually, and this is my problem). This is currently implemented as a normal Django view, which isn't very nice in my opinion. I'd like to have a Python program which is run using a cronjob instead of manually visiting the corr...
3
2009-09-04T20:16:20Z
1,381,517
<blockquote> <p>I'd like to have a Python program which is run using a cronjob instead of manually visiting the correct URL to update the information.</p> </blockquote> <p>A simplistic alternative: write a cronjob to <em>automatically</em> "visit the correct URL to update the information" -- could be as simple...
0
2009-09-04T20:58:46Z
[ "python", "django", "django-models", "django-orm" ]
Easiest way to write a Python program with access to Django database functionality
1,381,346
<p>I have a website which fetches information from RSS feeds periodically (well, currently manually, and this is my problem). This is currently implemented as a normal Django view, which isn't very nice in my opinion. I'd like to have a Python program which is run using a cronjob instead of manually visiting the corr...
3
2009-09-04T20:16:20Z
1,381,619
<p>An alternative to all the approaches given here is to write your cron job as a custom <code>./manage.py</code> command. This is very easy to do, and gives you the ability to do <code>./manage.py yourcommand</code> either on the command line or in your crontab.</p> <p><a href="http://docs.djangoproject.com/en/dev/ho...
9
2009-09-04T21:27:12Z
[ "python", "django", "django-models", "django-orm" ]
Easiest way to write a Python program with access to Django database functionality
1,381,346
<p>I have a website which fetches information from RSS feeds periodically (well, currently manually, and this is my problem). This is currently implemented as a normal Django view, which isn't very nice in my opinion. I'd like to have a Python program which is run using a cronjob instead of manually visiting the corr...
3
2009-09-04T20:16:20Z
21,129,096
<p><strong>Edit</strong>: For Django 1.7</p> <pre><code>import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings") django.setup() #have fun </code></pre> <p><a href="https://stackoverflow.com/a/23241093/1552045">Credit</a></p> <hr> <p>Selected answer is <strong>deprecated...
2
2014-01-15T04:24:31Z
[ "python", "django", "django-models", "django-orm" ]
C# way to mimic Python Dictionary Syntax
1,381,359
<p>Is there a good way in C# to mimic the following python syntax:</p> <pre><code>mydict = {} mydict["bc"] = {} mydict["bc"]["de"] = "123"; # &lt;-- This line mydict["te"] = "5"; # &lt;-- While also allowing this line </code></pre> <p>In other words, I'd like something with [] style access that can return e...
11
2009-09-04T20:21:21Z
1,381,380
<p>You can achieve this by having the class, lets call it PythonDictionary, which is returned from <code>mydict["bc"]</code> have the following members.</p> <ul> <li>A indexer property to allow for the ["de"] access</li> <li>A implicit conversion from string to PythonDictionary</li> </ul> <p>That should allow both ca...
11
2009-09-04T20:26:12Z
[ "c#", "python", "syntax", "dictionary", "types" ]
Model inheritance approach with Django's ORM
1,381,423
<p>I want to store events in a web application I am fooling around with and I feel quite unsure about the pros and cons of each respective approach - using inheritance extensively or in a more modest manner.</p> <p>Example:</p> <pre><code>class Event(models.Model): moment = models.DateTimeField() class UserEvent...
3
2009-09-04T20:36:55Z
1,381,528
<p><a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow">Flat is better than nested</a>. I don't see that the "deep inheritance" is really buying you anything in this case: I'd go for the flatter model as a simpler, plainer design, with likely better performance characteristics and ease of access.</p>
5
2009-09-04T21:02:05Z
[ "python", "django", "django-models", "model-inheritance" ]
Model inheritance approach with Django's ORM
1,381,423
<p>I want to store events in a web application I am fooling around with and I feel quite unsure about the pros and cons of each respective approach - using inheritance extensively or in a more modest manner.</p> <p>Example:</p> <pre><code>class Event(models.Model): moment = models.DateTimeField() class UserEvent...
3
2009-09-04T20:36:55Z
1,381,748
<p>You might want to try <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#id6" rel="nofollow">Abstract base models</a>. This implements inheritance without joins, by only creating tables for the derived classes, containing fields from both the parent and the derived model. This would allow you to keep yo...
0
2009-09-04T22:07:25Z
[ "python", "django", "django-models", "model-inheritance" ]
Purely functional data structures with copy-on-write?
1,381,592
<p>I want to have the advantage of functional data structures (multiple versions of data that can share structure) but be able to modify it in an imperative style. </p> <p>What I'm thinking about (and a possible use): a RPG game in which whole game history is stored (for example, to allow for travelling back in time)....
7
2009-09-04T21:19:28Z
1,381,664
<p><a href="http://en.wikipedia.org/wiki/Purely%5Ffunctional#Examples%5Fof%5Fpurely%5Ffunctional%5Fdata%5Fstructures">Functional ("persistent") data structures</a> are typically recursively built up out of immutable nodes (e.g. singly linked list where common suffixes are shared, search tree or heap where only the part...
8
2009-09-04T21:36:12Z
[ "python", "functional-programming", "immutability", "copy-on-write" ]
Purely functional data structures with copy-on-write?
1,381,592
<p>I want to have the advantage of functional data structures (multiple versions of data that can share structure) but be able to modify it in an imperative style. </p> <p>What I'm thinking about (and a possible use): a RPG game in which whole game history is stored (for example, to allow for travelling back in time)....
7
2009-09-04T21:19:28Z
1,382,934
<p>This sounds awfully convoluted and error prone compared to just having a fully immutable data structure and then using a wrapper which holds a reference to it and exposes an imperative interface which works by updating the wrapped version.</p> <p>e.g. in Scala</p> <pre><code>class ImmutableData{ def doStuff(bla...
0
2009-09-05T10:06:15Z
[ "python", "functional-programming", "immutability", "copy-on-write" ]
Purely functional data structures with copy-on-write?
1,381,592
<p>I want to have the advantage of functional data structures (multiple versions of data that can share structure) but be able to modify it in an imperative style. </p> <p>What I'm thinking about (and a possible use): a RPG game in which whole game history is stored (for example, to allow for travelling back in time)....
7
2009-09-04T21:19:28Z
37,143,788
<p><strong>1. Has this been done, and to what extent?</strong></p> <p>Yes, see for example qt5 <a href="http://doc.qt.io/qt-5/implicit-sharing.html" rel="nofollow">implicit sharing</a>.</p> <p><strong>2. Is this a good idea? If not, is there a way to save it?</strong></p> <p>Yes, it is a good idea. One of the propos...
0
2016-05-10T16:08:28Z
[ "python", "functional-programming", "immutability", "copy-on-write" ]
MAC OS X Custom Application Keeping Bouncing in the Dock
1,381,739
<p>First of all, thank you for taking the time to read this. I am new to developing applications for the Mac and I am having some problems. My application works fine, and that is not the focus of my question. Rather, I have a python program which essentially does this:</p> <pre><code>for i in values: os.system(ja...
0
2009-09-04T20:53:48Z
1,381,740
<p>As far as I am aware there is no way to disable the annoying double Java bounce without making your Java application a first class citizen on Mac OS X (much like NetBeans, or Eclipse). As for making certain programs not show in the dock, there are .plist modifications that can be made so that the program does not sh...
0
2009-09-04T21:46:16Z
[ "java", "python", "osx" ]
MAC OS X Custom Application Keeping Bouncing in the Dock
1,381,739
<p>First of all, thank you for taking the time to read this. I am new to developing applications for the Mac and I am having some problems. My application works fine, and that is not the focus of my question. Rather, I have a python program which essentially does this:</p> <pre><code>for i in values: os.system(ja...
0
2009-09-04T20:53:48Z
1,382,272
<p>Does running Java with headless mode = true fix it?</p> <p><a href="http://zzamboni.org/brt/2007/12/07/disable-dock-icon-for-java-programs-in-mac-osx-howto/" rel="nofollow">http://zzamboni.org/brt/2007/12/07/disable-dock-icon-for-java-programs-in-mac-osx-howto/</a></p>
1
2009-09-05T02:20:54Z
[ "java", "python", "osx" ]
MAC OS X Custom Application Keeping Bouncing in the Dock
1,381,739
<p>First of all, thank you for taking the time to read this. I am new to developing applications for the Mac and I am having some problems. My application works fine, and that is not the focus of my question. Rather, I have a python program which essentially does this:</p> <pre><code>for i in values: os.system(ja...
0
2009-09-04T20:53:48Z
1,382,398
<p>It's certainly possible to write a Java application which doesn't display in the Dock... in fact, it's the default. If your application <em>is</em> showing up, it must be doing something which triggers window server access -- your best bet is to try and figure out what that is.</p>
0
2009-09-05T03:46:53Z
[ "java", "python", "osx" ]
Converting latex code to Images (or other displayble format) with Python
1,381,741
<p>I have a function I am consuming that returns a string of latex code. I need to generate an image from this. Most of the methods I have seen for doing so suggest calling an external application via say the <em>subprocess</em> module which will generate the image for me. </p> <p>However, management is not keen on...
10
2009-09-04T22:04:56Z
1,381,791
<p>You're going to need to use LaTeX to process to string. The process of rendering LaTex/TeX is very involved (it generally takes a 100+MB package to do the work), you're just not going to be able toss in a little python module to get the work done.</p>
0
2009-09-04T22:25:54Z
[ "python", "latex" ]
Converting latex code to Images (or other displayble format) with Python
1,381,741
<p>I have a function I am consuming that returns a string of latex code. I need to generate an image from this. Most of the methods I have seen for doing so suggest calling an external application via say the <em>subprocess</em> module which will generate the image for me. </p> <p>However, management is not keen on...
10
2009-09-04T22:04:56Z
1,381,806
<p>Maybe you could use an online service such as this one: <a href="http://www.codecogs.com/components/equationeditor/equationeditor.php" rel="nofollow">http://www.codecogs.com/components/equationeditor/equationeditor.php</a>.</p> <hr> <p>Following Joel A. Christophel's suggestion, here's a working similar website: <...
0
2009-09-04T22:34:23Z
[ "python", "latex" ]
Converting latex code to Images (or other displayble format) with Python
1,381,741
<p>I have a function I am consuming that returns a string of latex code. I need to generate an image from this. Most of the methods I have seen for doing so suggest calling an external application via say the <em>subprocess</em> module which will generate the image for me. </p> <p>However, management is not keen on...
10
2009-09-04T22:04:56Z
1,382,067
<p>If it is just math equations that you need, you will probably have better luck finding a mathml renderer in python. This <a href="http://www.w3.org/Math/Software/mathml%5Fsoftware%5Fcat%5Fconverters.html#Itranslator%5Ffrom%5Ftex%5Flatex%5Fto%5Fmathml" rel="nofollow">page</a> might provide some clues, including some ...
0
2009-09-05T00:31:31Z
[ "python", "latex" ]
Converting latex code to Images (or other displayble format) with Python
1,381,741
<p>I have a function I am consuming that returns a string of latex code. I need to generate an image from this. Most of the methods I have seen for doing so suggest calling an external application via say the <em>subprocess</em> module which will generate the image for me. </p> <p>However, management is not keen on...
10
2009-09-04T22:04:56Z
1,382,986
<p>Take a look at <a href="http://code.google.com/p/mathtex/" rel="nofollow">mathtex</a>.</p>
4
2009-09-05T10:37:59Z
[ "python", "latex" ]
Converting latex code to Images (or other displayble format) with Python
1,381,741
<p>I have a function I am consuming that returns a string of latex code. I need to generate an image from this. Most of the methods I have seen for doing so suggest calling an external application via say the <em>subprocess</em> module which will generate the image for me. </p> <p>However, management is not keen on...
10
2009-09-04T22:04:56Z
29,922,912
<p>this answer might not have been available at the time when the question was asked, but i will add it for those seeking a solution as of 2015.</p> <p>you can use <code>matplotlib.pyplot</code> to render an equation in a graph with axes, and then remove the axes manually. you can also generate the latex with <code>sy...
6
2015-04-28T15:01:54Z
[ "python", "latex" ]
Converting latex code to Images (or other displayble format) with Python
1,381,741
<p>I have a function I am consuming that returns a string of latex code. I need to generate an image from this. Most of the methods I have seen for doing so suggest calling an external application via say the <em>subprocess</em> module which will generate the image for me. </p> <p>However, management is not keen on...
10
2009-09-04T22:04:56Z
36,249,956
<p>SymPy has a builtin preview function that does this.</p> <pre><code>expr = sin(sqrt(x**2 + 20)) + 1 preview(expr, viewer='file', filename='output.png') </code></pre> <p>generates </p> <p><a href="http://i.stack.imgur.com/Js3gz.png" rel="nofollow"><img src="http://i.stack.imgur.com/Js3gz.png" alt="enter image desc...
3
2016-03-27T16:46:30Z
[ "python", "latex" ]
inserting a tuple into a mysql db
1,381,840
<p>can anyone show me the syntax for inserting a python tuple/list into a mysql database? i also need to know if it is possible for user to pass certain rows without inserting anything... for example: a function is returning this tuple:</p> <pre><code>return job(jcardnum, jreg, jcarddate, jcardtime, jcardserve, jdeliv...
0
2009-09-04T22:42:57Z
1,382,194
<p>As a comment says, the <code>job(...)</code> part is a function (or class) call -- whatever is returned from that call also gets returned from this <code>return</code> statement.</p> <p>Let's assume it's a tuple. What if "the user didn't enter anything in <code>jreg</code>" -- well then, depending on a lot of code ...
6
2009-09-05T01:49:55Z
[ "python", "mysql" ]
inserting a tuple into a mysql db
1,381,840
<p>can anyone show me the syntax for inserting a python tuple/list into a mysql database? i also need to know if it is possible for user to pass certain rows without inserting anything... for example: a function is returning this tuple:</p> <pre><code>return job(jcardnum, jreg, jcarddate, jcardtime, jcardserve, jdeliv...
0
2009-09-04T22:42:57Z
1,383,289
<p>Maybe, cursor.executemany is what you need?</p> <blockquote> <p>suppose the user didnt enter anything in jreg would python itself enter null in to the related row in the DB or would i run in to trouble?</p> </blockquote> <p>I think you should just try, you probably see the warning or error if NULL is not a...
0
2009-09-05T13:02:55Z
[ "python", "mysql" ]
Writing Interpreters in Python. Is isinstance considered harmful?
1,381,845
<p>I'm porting over the interpreter for a domain specific language I created from Scala to Python. In the process I tried to find a way that way pythonic to emulate the case class feature of Scala that I used extensively. In the end I resorted to using isinstance, but was left feeling that I was perhaps missing somethi...
4
2009-09-04T22:45:13Z
1,381,883
<p>The article does not attack <code>isinstance</code>. It attacks the idea of making your code test for specific classes.</p> <p>And yes, there is a better way. Or several. You can for example make the handling of a type into a function, and then find the correct function by looking up per type. Like this:</p> <pre>...
1
2009-09-04T23:01:54Z
[ "python", "scala", "language-design", "interpreter" ]
Writing Interpreters in Python. Is isinstance considered harmful?
1,381,845
<p>I'm porting over the interpreter for a domain specific language I created from Scala to Python. In the process I tried to find a way that way pythonic to emulate the case class feature of Scala that I used extensively. In the end I resorted to using isinstance, but was left feeling that I was perhaps missing somethi...
4
2009-09-04T22:45:13Z
1,381,936
<p>Yes.</p> <p>Instead of instance, just use <a href="http://en.wikipedia.org/wiki/Polymorphism%5Fin%5Fobject-oriented%5Fprogramming" rel="nofollow">Polymorphism</a>. It's simpler.</p> <pre><code>class Node( object ): def eval( self, context ): raise NotImplementedError class Add( object ): def eval(...
2
2009-09-04T23:22:44Z
[ "python", "scala", "language-design", "interpreter" ]
Writing Interpreters in Python. Is isinstance considered harmful?
1,381,845
<p>I'm porting over the interpreter for a domain specific language I created from Scala to Python. In the process I tried to find a way that way pythonic to emulate the case class feature of Scala that I used extensively. In the end I resorted to using isinstance, but was left feeling that I was perhaps missing somethi...
4
2009-09-04T22:45:13Z
1,382,013
<p>In a DSL I wrote using Python 3, I used the Composite design pattern so the nodes were all polymorphic in their use, as S. Lott is recommending.</p> <p>But, when I was reading in the input to create those nodes in the first place, I did use isinstance checks a lot (against abstract base classes like collections.Ite...
0
2009-09-05T00:04:16Z
[ "python", "scala", "language-design", "interpreter" ]
Writing Interpreters in Python. Is isinstance considered harmful?
1,381,845
<p>I'm porting over the interpreter for a domain specific language I created from Scala to Python. In the process I tried to find a way that way pythonic to emulate the case class feature of Scala that I used extensively. In the end I resorted to using isinstance, but was left feeling that I was perhaps missing somethi...
4
2009-09-04T22:45:13Z
1,382,058
<p>If you need Polymorphism on arguments (in addition to the receiver), for example to handle type conversions with binary operators as suggested by your example, you can use the following trick:</p> <pre><code>class EValue(object): def __init__(self, v): self.value = v def __str__(self): ret...
-1
2009-09-05T00:27:57Z
[ "python", "scala", "language-design", "interpreter" ]
Writing Interpreters in Python. Is isinstance considered harmful?
1,381,845
<p>I'm porting over the interpreter for a domain specific language I created from Scala to Python. In the process I tried to find a way that way pythonic to emulate the case class feature of Scala that I used extensively. In the end I resorted to using isinstance, but was left feeling that I was perhaps missing somethi...
4
2009-09-04T22:45:13Z
1,383,559
<p>In this particular case, what you seem to be implementing is an operator overloading system that uses the types of the objects as the selection mechanism for the operator you intend to call. Your node types happen to fairly directly correspond to your language's types, but in reality you're writing an interpreter. ...
0
2009-09-05T15:13:07Z
[ "python", "scala", "language-design", "interpreter" ]
Writing Interpreters in Python. Is isinstance considered harmful?
1,381,845
<p>I'm porting over the interpreter for a domain specific language I created from Scala to Python. In the process I tried to find a way that way pythonic to emulate the case class feature of Scala that I used extensively. In the end I resorted to using isinstance, but was left feeling that I was perhaps missing somethi...
4
2009-09-04T22:45:13Z
1,384,643
<p>There's a rule of thumb in python, if you find yourself writing a large block of if/elif statements, with similar conditions (a bunch of isinstance(...) for example) then you're probably solving the problem the wrong way.</p> <p>Better ways involve using classes and polymorphism, visitor pattern, dict lookup, etc. ...
2
2009-09-06T01:15:23Z
[ "python", "scala", "language-design", "interpreter" ]
Writing Interpreters in Python. Is isinstance considered harmful?
1,381,845
<p>I'm porting over the interpreter for a domain specific language I created from Scala to Python. In the process I tried to find a way that way pythonic to emulate the case class feature of Scala that I used extensively. In the end I resorted to using isinstance, but was left feeling that I was perhaps missing somethi...
4
2009-09-04T22:45:13Z
1,390,006
<p><strong>Summary</strong>: This is a common way to write compilers, and its just fine here.</p> <p>A very common way to handle this in other languages is by "pattern matching", which is exactly what you've described. I expect that's the name for that <code>case</code> statement in Scala. Its a very common idiom for ...
2
2009-09-07T16:04:00Z
[ "python", "scala", "language-design", "interpreter" ]
"WindowsError: exception: access violation..." - ctypes question
1,382,076
<p>Howdy all - Here is the prototype for a C function that resides in a DLL:</p> <pre><code>extern "C" void__stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); </code></pre> <p>In another thread, I asked about how to properly create and send the necessary arguments to this function.</...
3
2009-09-05T00:35:26Z
1,403,195
<p>The error you're getting is not related to Administrative rights. The problem is you're using C and inadvertently performing illegal operations (the kind of operations that if went unchecked would probably crash your system).</p> <p>The error you get indicates that your program is trying to write to memory address ...
11
2009-09-10T03:18:18Z
[ "python", "ctypes", "access-violation", "windowserror" ]
"WindowsError: exception: access violation..." - ctypes question
1,382,076
<p>Howdy all - Here is the prototype for a C function that resides in a DLL:</p> <pre><code>extern "C" void__stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); </code></pre> <p>In another thread, I asked about how to properly create and send the necessary arguments to this function.</...
3
2009-09-05T00:35:26Z
2,094,029
<p>ptrpulse and friends are python identifiers that point to various ctypes objects (I guess they are all c_double*2). They either need to be wrapped with a ctypes pointer object, or passed to the C function using ctypes.byref.</p>
0
2010-01-19T14:13:07Z
[ "python", "ctypes", "access-violation", "windowserror" ]
XCode 3.2 Ruby and Python templates
1,382,252
<p>Under xcode 3.2 my ObjectiveC + Python/Ruby projects can still be opened updated and compiled, but you cannot create new projects.</p> <p>Given that all traces of ruby and python are missing from xcode 3.2 (ie create project and add new ruby/python file), is there an easy way to get the templates installed again? <...
6
2009-09-05T02:14:11Z
1,382,454
<p>Beginning with Xcode 3.2, Apple decided to not include project and file templates from 3rd party projects (including PyObjC, RubyCocoa or MacRuby). Since these template files were often updated more frequently than Xcode's release cycle, the templates shipped with Xcode were often out of date. Developers are now enc...
3
2009-09-05T04:23:48Z
[ "python", "ruby", "cocoa", "xcode", "pyobjc" ]
XCode 3.2 Ruby and Python templates
1,382,252
<p>Under xcode 3.2 my ObjectiveC + Python/Ruby projects can still be opened updated and compiled, but you cannot create new projects.</p> <p>Given that all traces of ruby and python are missing from xcode 3.2 (ie create project and add new ruby/python file), is there an easy way to get the templates installed again? <...
6
2009-09-05T02:14:11Z
1,382,461
<p><a href="http://lists.apple.com/archives/Xcode-users/2009/Aug/msg00517.html" rel="nofollow">Here's the word</a> on this from Chris Espinosa on the Xcode-Users mailing list:</p> <blockquote> <p>We are deemphasizing Cocoa-Python and Cocoa-Ruby, though existing project will continue to build in Xcode. You can...
5
2009-09-05T04:27:54Z
[ "python", "ruby", "cocoa", "xcode", "pyobjc" ]
XCode 3.2 Ruby and Python templates
1,382,252
<p>Under xcode 3.2 my ObjectiveC + Python/Ruby projects can still be opened updated and compiled, but you cannot create new projects.</p> <p>Given that all traces of ruby and python are missing from xcode 3.2 (ie create project and add new ruby/python file), is there an easy way to get the templates installed again? <...
6
2009-09-05T02:14:11Z
1,448,482
<p>The above coments were all good and helpful but didn't really help. Easiest thing I could find is to create an empty python/ruby project in xcode 3.1, then make copies of this project folder for every new project you work on.</p> <p>When you open the new/blank project, 3.2 has a new feature that lets you rename the...
0
2009-09-19T12:40:03Z
[ "python", "ruby", "cocoa", "xcode", "pyobjc" ]
XCode 3.2 Ruby and Python templates
1,382,252
<p>Under xcode 3.2 my ObjectiveC + Python/Ruby projects can still be opened updated and compiled, but you cannot create new projects.</p> <p>Given that all traces of ruby and python are missing from xcode 3.2 (ie create project and add new ruby/python file), is there an easy way to get the templates installed again? <...
6
2009-09-05T02:14:11Z
1,454,608
<p>The folder for application templates in 3.2 is: /Developer/Library/Xcode/Project Templates/Application</p> <p>Templates for python are at: <a href="http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/Project%20Templates/" rel="nofollow">http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/Project%20Templat...
6
2009-09-21T13:51:12Z
[ "python", "ruby", "cocoa", "xcode", "pyobjc" ]
XCode 3.2 Ruby and Python templates
1,382,252
<p>Under xcode 3.2 my ObjectiveC + Python/Ruby projects can still be opened updated and compiled, but you cannot create new projects.</p> <p>Given that all traces of ruby and python are missing from xcode 3.2 (ie create project and add new ruby/python file), is there an easy way to get the templates installed again? <...
6
2009-09-05T02:14:11Z
2,633,955
<ul> <li>Download the <a href="http://rubycocoa.svn.sourceforge.net/viewvc/rubycocoa/trunk/src/template/ProjectBuilder/" rel="nofollow" title="Cocoa-Ruby Templates">Cocoa-Ruby Templates</a> to your '/Developer/Library/Xcode/Project Templates/Application/Ruby Application/' and '/Developer/Library/Xcode/File Templates/Ru...
1
2010-04-13T23:44:06Z
[ "python", "ruby", "cocoa", "xcode", "pyobjc" ]
sqlalchemy easy way to insert or update?
1,382,469
<p>I have a sequence of new objects. They all look like similar to this:</p> <p>Foo(pk_col1=x, pk_col2=y, val='bar')</p> <p>Some of those are Foo that exist (i.e. only val differs from the row in the db) and should generate update queries. The others should generate inserts.</p> <p>I can think of a few ways of doing...
22
2009-09-05T04:33:13Z
1,382,493
<pre><code>Session.save_or_update(model) </code></pre>
-5
2009-09-05T04:48:50Z
[ "python", "sqlalchemy" ]
sqlalchemy easy way to insert or update?
1,382,469
<p>I have a sequence of new objects. They all look like similar to this:</p> <p>Foo(pk_col1=x, pk_col2=y, val='bar')</p> <p>Some of those are Foo that exist (i.e. only val differs from the row in the db) and should generate update queries. The others should generate inserts.</p> <p>I can think of a few ways of doing...
22
2009-09-05T04:33:13Z
1,415,306
<p>I think you are after <a href="http://docs.sqlalchemy.org/en/latest/orm/session_state_management.html#merging">new_obj = session.merge(obj)</a>. This will merge an object in a detached state into the session if the primary keys match and will make a new one otherwise. So <code>session.save(new_obj)</code> will work ...
30
2009-09-12T14:55:50Z
[ "python", "sqlalchemy" ]
How do I do Debian packaging of a Python package?
1,382,569
<p>I need to write, or find, a script to create a Debian package, using python-support, from a Python package. The Python package will be pure Python with no C extensions.</p> <p>The Python package for testing purposes will just be a directory with an empty <code>__init__.py</code> file and a single Python module, <co...
39
2009-09-05T05:39:23Z
1,382,580
<p>First off, there are plenty of Python packages already in Debian; you can download the source (including all the packaging) for any of them either using <code>apt-get source</code> or by visiting <a href="http://packages.debian.org">http://packages.debian.org</a>.</p> <p>You may find the following resources of use:...
5
2009-09-05T05:52:10Z
[ "python", "debian" ]
How do I do Debian packaging of a Python package?
1,382,569
<p>I need to write, or find, a script to create a Debian package, using python-support, from a Python package. The Python package will be pure Python with no C extensions.</p> <p>The Python package for testing purposes will just be a directory with an empty <code>__init__.py</code> file and a single Python module, <co...
39
2009-09-05T05:39:23Z
1,382,593
<p>I would take the sources of an existing Debian package, and replace the actual package in it with your package. To find a list of packages that depend on python-support, do</p> <pre><code> apt-cache rdepends python-support </code></pre> <p>Pick a package that is <code>Architecture: all</code>, so that it is a pure...
17
2009-09-05T06:00:43Z
[ "python", "debian" ]
How do I do Debian packaging of a Python package?
1,382,569
<p>I need to write, or find, a script to create a Debian package, using python-support, from a Python package. The Python package will be pure Python with no C extensions.</p> <p>The Python package for testing purposes will just be a directory with an empty <code>__init__.py</code> file and a single Python module, <co...
39
2009-09-05T05:39:23Z
2,796,210
<p>I think what you want is <a href="http://pypi.python.org/pypi/stdeb">http://pypi.python.org/pypi/stdeb</a>:</p> <blockquote> <p>stdeb produces Debian source packages from Python packages via a new distutils command, sdist_dsc. Automatic defaults are provided for the Debian package, but many aspects of ...
19
2010-05-09T00:50:12Z
[ "python", "debian" ]
How do I do Debian packaging of a Python package?
1,382,569
<p>I need to write, or find, a script to create a Debian package, using python-support, from a Python package. The Python package will be pure Python with no C extensions.</p> <p>The Python package for testing purposes will just be a directory with an empty <code>__init__.py</code> file and a single Python module, <co...
39
2009-09-05T05:39:23Z
14,423,375
<p>Most of the answers posted here are outdated, fortunately a great Debian wiki post has been made recently, which explains the current best practices and describes how to build Debian packages for Python modules and applications.</p> <ul> <li><a href="http://wiki.debian.org/Python/Packaging">http://wiki.debian.org/P...
13
2013-01-20T09:55:25Z
[ "python", "debian" ]
How do I do Debian packaging of a Python package?
1,382,569
<p>I need to write, or find, a script to create a Debian package, using python-support, from a Python package. The Python package will be pure Python with no C extensions.</p> <p>The Python package for testing purposes will just be a directory with an empty <code>__init__.py</code> file and a single Python module, <co...
39
2009-09-05T05:39:23Z
25,275,227
<p>The right way of building a deb package is using <code>dpkg-buildpackage</code> but sometimes it is a little bit complicated. Instead you can use <code>dpkg -b &lt;folder&gt;</code> and it will create your Debian package.</p> <p>These are the basics for creating a Debian package with <code>dpkg -b &lt;folder&gt;</c...
9
2014-08-12T22:37:15Z
[ "python", "debian" ]
Which PEP's are must reads?
1,382,648
<p>I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read?</p>
46
2009-09-05T06:43:05Z
1,382,661
<p>Definitely <a href="http://www.python.org/dev/peps/pep-0008/">PEP 8</a>, a Style Guide for Python.</p>
24
2009-09-05T06:51:34Z
[ "python", "pep" ]
Which PEP's are must reads?
1,382,648
<p>I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read?</p>
46
2009-09-05T06:43:05Z
1,382,662
<p>I found that reading the declined ones can give some good insights into what's Pythonic and what isn't. This was a while ago so I don't have any specific examples.</p>
6
2009-09-05T06:53:07Z
[ "python", "pep" ]
Which PEP's are must reads?
1,382,648
<p>I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read?</p>
46
2009-09-05T06:43:05Z
1,382,981
<p>It is now retrospective, but still interesting: I think <a href="http://www.python.org/dev/peps/pep-3099/">Things that will Not Change in Python 3000</a> is a good read, with lots of links to the discussions that preceded the decisions.</p>
8
2009-09-05T10:34:42Z
[ "python", "pep" ]
Which PEP's are must reads?
1,382,648
<p>I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read?</p>
46
2009-09-05T06:43:05Z
1,383,045
<p>Also pep <a href="http://www.python.org/dev/peps/pep-0257/">0257</a> docstring convention</p>
9
2009-09-05T11:00:43Z
[ "python", "pep" ]
Which PEP's are must reads?
1,382,648
<p>I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read?</p>
46
2009-09-05T06:43:05Z
1,383,659
<p>Although Python is incredibly intuitive, a lot of people do not comprehend his philosophy.</p> <blockquote> <p><a href="http://www.python.org/dev/peps/pep-0020/">Pep 20</a>: <strong>The Zen of Python</strong></p> <ul> <li>Beautiful is better than ugly.</li> <li>Explicit is better than implicit.</li> <l...
13
2009-09-05T15:59:54Z
[ "python", "pep" ]
Which PEP's are must reads?
1,382,648
<p>I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read?</p>
46
2009-09-05T06:43:05Z
21,502,630
<p>I'd also recommend PEPs 8 and 257. I know this deviates slightly from the original question, but I'd like to point out that PyCharm (probably the best Python IDE around in my opinion) automatically checks if you're following some of the most important PEP 8 guidelines, just in case anyone's interested...</p>
1
2014-02-01T19:39:02Z
[ "python", "pep" ]
Which PEP's are must reads?
1,382,648
<p>I'm a fairly strong Python coder, but too much of my style is a little haphazard, and I'm sure there are more Pythonic solutions to many problems than the ones I come up with. Which PEPs are essential for any well versed Pythonista to read?</p>
46
2009-09-05T06:43:05Z
21,602,502
<p>Here is a index of PEP - <a href="http://www.python.org/dev/peps/" rel="nofollow">http://www.python.org/dev/peps/</a> </p> <p>when ever one has doubt about a topic, they can search in that</p>
2
2014-02-06T12:04:52Z
[ "python", "pep" ]
String formatting
1,382,663
<p>I have a list filter = ['a', 'b', 'c']. I need to frame the following string out of the list "item -a item -b item -c". Which is the most efficient way to do this? Usually the list filter contains 100 to 200 items and each would be of length 100 - 150. Wouldn't that lead to overflow? And what is the maximum length o...
0
2009-09-05T06:53:57Z
1,382,685
<p>You can use <strong><a href="http://docs.python.org/library/stdtypes.html#str.join" rel="nofollow">join</a></strong> (I believe <code>join</code> is the same in Python 3.0):</p> <pre><code>&gt;&gt;&gt; l = ['a','b','c'] &gt;&gt;&gt; print ' item -'.join([''] + l) &gt;&gt;&gt; ' item -a item -b item -c' &gt;&gt;&gt...
1
2009-09-05T07:11:36Z
[ "python-3.x", "python" ]
String formatting
1,382,663
<p>I have a list filter = ['a', 'b', 'c']. I need to frame the following string out of the list "item -a item -b item -c". Which is the most efficient way to do this? Usually the list filter contains 100 to 200 items and each would be of length 100 - 150. Wouldn't that lead to overflow? And what is the maximum length o...
0
2009-09-05T06:53:57Z
1,382,748
<p>A cleaner way to do this:</p> <pre><code>filter = ['a', 'b', 'c'] " ".join(["item -%s" % val for val in filter]) </code></pre> <p>This works fine with large arrays, eg. <code>filter = ['a'*1000] * 1000</code>.</p>
4
2009-09-05T07:52:45Z
[ "python-3.x", "python" ]
How can I create a numpy array holding values of a multi-variable function?
1,382,846
<p>I want to create an array holding a function <code>f(x,y,z)</code>. If it were a function of one variable I'd do, for instance:</p> <pre><code>sinx = numpy.sin(numpy.linspace(-5,5,100)) </code></pre> <p>to get <code>sin(x)</code> for <code>x</code> in <code>[-5,5]</code></p> <p>How can I do the same to get, for ...
2
2009-09-05T09:11:28Z
1,383,181
<p>I seem to have found a way:</p> <pre><code># define the range of x,y,z x_range = numpy.linspace(x_min,x_max,x_num) y_range = numpy.linspace(y_min,y_max,y_num) z_range = numpy.linspace(z_min,z_max,z_num) # create arrays x,y,z in the correct dimensions # so that they create the grid x,y,z = numpy.ix_(x_range,y_range...
5
2009-09-05T12:00:49Z
[ "python", "function", "multidimensional-array", "numpy" ]
How can I create a numpy array holding values of a multi-variable function?
1,382,846
<p>I want to create an array holding a function <code>f(x,y,z)</code>. If it were a function of one variable I'd do, for instance:</p> <pre><code>sinx = numpy.sin(numpy.linspace(-5,5,100)) </code></pre> <p>to get <code>sin(x)</code> for <code>x</code> in <code>[-5,5]</code></p> <p>How can I do the same to get, for ...
2
2009-09-05T09:11:28Z
2,288,142
<pre><code>xyz = numpy.mgrid[-5:5,-5:5,-5:5] sinxyz = numpy.sin(xyz[0]+xyz[1]+xyz[2]) </code></pre>
4
2010-02-18T11:20:02Z
[ "python", "function", "multidimensional-array", "numpy" ]
How can I create a numpy array holding values of a multi-variable function?
1,382,846
<p>I want to create an array holding a function <code>f(x,y,z)</code>. If it were a function of one variable I'd do, for instance:</p> <pre><code>sinx = numpy.sin(numpy.linspace(-5,5,100)) </code></pre> <p>to get <code>sin(x)</code> for <code>x</code> in <code>[-5,5]</code></p> <p>How can I do the same to get, for ...
2
2009-09-05T09:11:28Z
15,777,259
<p>The numpy.mgrid function would work equally well:</p> <pre><code>x,y,z = numpy.mgrid[x_min:x_max:x_num, y_min:y_max:y_num, z_min:z_max:z_num] sinxyz = numpy.sin(x+y+z) </code></pre> <p>edit: to get it to work <code>x_num</code>, <code>y_num</code> and <code>z_num</code> have to be explicit numbers followed by <c...
-1
2013-04-03T01:16:44Z
[ "python", "function", "multidimensional-array", "numpy" ]
Dynamically attaching a method to an existing Python object generated with swig?
1,382,871
<p>I am working with a Python class, and I don't have write access to its declaration. How can I attach a custom method (such as <strong><code>__str__</code></strong>) to the objects created from that class without modifying the class declaration?</p> <p>EDIT: Thank you for all your answers. I tried them all but they...
7
2009-09-05T09:30:59Z
1,382,883
<pre><code>&gt;&gt;&gt; class C(object): ... pass ... &gt;&gt;&gt; def spam(self): ... return 'spam' ... &gt;&gt;&gt; C.__str__ = spam &gt;&gt;&gt; print C() spam </code></pre> <p>It won't work on classes which use <a href="http://docs.python.org/reference/datamodel.html#slots" rel="nofollow"><code>__slots__...
3
2009-09-05T09:36:29Z
[ "python", "class", "dynamic", "methods" ]