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
How to setup and run Python on Wampserver?
2,137,394
<p>Can anyone help me to set up Python to run on Wampserver. From what I've read so far you would need to use a combination of Wampser, Python, mod_pyhton, and adjustment to the Apache http.conf file. I've tried it but i belive i am having conflict when it comes to versions. Does anyone know of a cobination of versions...
10
2010-01-26T03:40:21Z
8,041,600
<p>Here are some instructions here: <a href="http://www.imladris.com/Scripts/PythonForWindows.html" rel="nofollow">http://www.imladris.com/Scripts/PythonForWindows.html</a></p>
2
2011-11-07T19:39:57Z
[ "python", "apache", "installation", "mod-python", "wampserver" ]
How to setup and run Python on Wampserver?
2,137,394
<p>Can anyone help me to set up Python to run on Wampserver. From what I've read so far you would need to use a combination of Wampser, Python, mod_pyhton, and adjustment to the Apache http.conf file. I've tried it but i belive i am having conflict when it comes to versions. Does anyone know of a cobination of versions...
10
2010-01-26T03:40:21Z
29,623,977
<p>My WSGI setup made on <a href="http://www.wampserver.com/en/#wampserver-32-bits-php-5-5" rel="nofollow">WAMP server 2.5</a>, 32bits (Apache 2.4.9 32bits) with PythonWin 2.7.8 (default, Jul 2 2014, 19:50:44) [MSC v.1500 32 bit (Intel)] on win32 went on the next way.</p> <pre><code>WAMP route = C:/wamp/ </code></p...
1
2015-04-14T09:38:22Z
[ "python", "apache", "installation", "mod-python", "wampserver" ]
Convert Line Number Into its corresponding co-ordinates
2,137,517
<p>I have a application written in PyGtk. I need to convert a particular line number into its corresponding window co-ordinates in a GtkTextView. How can this be done?</p>
0
2010-01-26T04:26:14Z
2,137,653
<p>It can't be done, in general. If you're talking about a line of your pygtk program, then you would need an API for your IDE (development environment) to find out where a particular source code line is currently displayed on the screen, if anywhere. If by "line number" you mean something else, then as Ignacio says,...
0
2010-01-26T05:25:33Z
[ "python", "pygtk", "gtk" ]
Convert Line Number Into its corresponding co-ordinates
2,137,517
<p>I have a application written in PyGtk. I need to convert a particular line number into its corresponding window co-ordinates in a GtkTextView. How can this be done?</p>
0
2010-01-26T04:26:14Z
2,138,262
<p>Begin by using <code><a href="http://library.gnome.org/devel/gtk/stable/GtkTextBuffer.html#gtk-text-buffer-get-iter-at-line" rel="nofollow">gtk_text_buffer_get_iter_at_line()</a></code> to get a handle on the line. Then use <code><a href="http://library.gnome.org/devel/gtk/stable/GtkTextView.html#gtk-text-view-get-l...
2
2010-01-26T08:44:13Z
[ "python", "pygtk", "gtk" ]
How to invoke a method that has been discovered through inspect.getmembers?
2,137,539
<pre><code>import re import sys import inspect import testcases testClass = re.compile(r'.*Case$') testMethod = re.compile(r'.*Test$') for class_name, class_obj in inspect.getmembers(testcases, inspect.isclass): if testClass.match(class_name): for method_name, method_obj in inspect.getmembers(class_obj, i...
1
2010-01-26T04:33:07Z
2,137,589
<p>Right after your code:</p> <pre><code>for class_name, class_obj in inspect.getmembers(testcases, inspect.isclass): if testClass.match(class_name): for method_name, method_obj in inspect.getmembers(class_obj, inspect.ismethod): if testMethod.match(method_name): # RIGHT HERE I ...
6
2010-01-26T04:55:22Z
[ "python", "inspect" ]
How to invoke a method that has been discovered through inspect.getmembers?
2,137,539
<pre><code>import re import sys import inspect import testcases testClass = re.compile(r'.*Case$') testMethod = re.compile(r'.*Test$') for class_name, class_obj in inspect.getmembers(testcases, inspect.isclass): if testClass.match(class_name): for method_name, method_obj in inspect.getmembers(class_obj, i...
1
2010-01-26T04:33:07Z
2,140,567
<p>Thank you very much alex for the quick response.</p> <blockquote> <p>The first argument is presumably going to be an instance of the class -- which instance? How do you retrieve or create it?</p> </blockquote> <p>Okay, so if i understood you correctly, the first arugment of the method call methodobj should be an...
0
2010-01-26T16:01:10Z
[ "python", "inspect" ]
Scala equivalent to Python generators?
2,137,619
<p>Is it possible to implement in Scala something equivalent to the Python <code>yield</code> statement where it remembers the local state of the function where it is used and "yields" the next value each time it is called?</p> <p>I wanted to have something like this to convert a recursive function into an iterator. S...
39
2010-01-26T05:10:02Z
2,137,785
<p>To do this in a general way, I think you need the <a href="http://blog.richdougherty.com/search/label/continuations" rel="nofollow">continuations plugin</a>.</p> <p>A naive implementation (freehand, not compiled/checked):</p> <pre><code>def iterator = new { private[this] var done = false // Define your yieldi...
2
2010-01-26T06:16:30Z
[ "python", "scala" ]
Scala equivalent to Python generators?
2,137,619
<p>Is it possible to implement in Scala something equivalent to the Python <code>yield</code> statement where it remembers the local state of the function where it is used and "yields" the next value each time it is called?</p> <p>I wanted to have something like this to convert a recursive function into an iterator. S...
39
2010-01-26T05:10:02Z
2,139,849
<p>While Python generators are cool, trying to duplicate them really isn't the best way to go about in Scala. For instance, the following code does the equivalent job to what you want:</p> <pre class="lang-scala prettyprint-override"><code>def classStream(clazz: Class[_]): Stream[Class[_]] = clazz match { case null ...
25
2010-01-26T14:04:57Z
[ "python", "scala" ]
Scala equivalent to Python generators?
2,137,619
<p>Is it possible to implement in Scala something equivalent to the Python <code>yield</code> statement where it remembers the local state of the function where it is used and "yields" the next value each time it is called?</p> <p>I wanted to have something like this to convert a recursive function into an iterator. S...
39
2010-01-26T05:10:02Z
2,146,456
<p>Another continuations plugin based solution, this time with a more or less encapsulated Generator type,</p> <pre><code>import scala.continuations._ import scala.continuations.ControlContext._ object Test { def loopWhile(cond: =&gt;Boolean)(body: =&gt;(Unit @suspendable)): Unit @suspendable = { if (cond) { ...
10
2010-01-27T11:32:45Z
[ "python", "scala" ]
Scala equivalent to Python generators?
2,137,619
<p>Is it possible to implement in Scala something equivalent to the Python <code>yield</code> statement where it remembers the local state of the function where it is used and "yields" the next value each time it is called?</p> <p>I wanted to have something like this to convert a recursive function into an iterator. S...
39
2010-01-26T05:10:02Z
4,880,342
<p>Scala's for-loop of the form <code>for (e &lt;- Producer) f(e)</code> translates into a <code>foreach</code> call, and not directly into <code>iterator</code> / <code>next</code>.</p> <p>In the <code>foreach</code> we don't need to linearize objects' creations and have them in one place, as it would be needed for i...
2
2011-02-02T21:56:35Z
[ "python", "scala" ]
Depth-First search in Python
2,137,731
<p>I'm trying to do a Depth-First search in Python but it's not working.</p> <p>Basically we have a peg-solitaire board:</p> <pre><code>[1,1,1,1,1,0,1,1,1,1] </code></pre> <p>1's represent a peg, and 0 is an open spot. You must move a peg one at a time TWO SLOTS backwards or forward ONLY to an empty spot. If you ju...
3
2010-01-26T06:00:34Z
2,137,747
<p>It doesn't appear that you're creating new nodes, just re-using existing ones. DFS requires some kind of stack (either the call stack, or your own stack). Where is that?</p>
1
2010-01-26T06:05:33Z
[ "python", "depth-first-search" ]
Depth-First search in Python
2,137,731
<p>I'm trying to do a Depth-First search in Python but it's not working.</p> <p>Basically we have a peg-solitaire board:</p> <pre><code>[1,1,1,1,1,0,1,1,1,1] </code></pre> <p>1's represent a peg, and 0 is an open spot. You must move a peg one at a time TWO SLOTS backwards or forward ONLY to an empty spot. If you ju...
3
2010-01-26T06:00:34Z
2,137,767
<p>Well, first of all a depth-first search assumes a tree. Now, that makes sense here as you have several possible moves in most cases. A depth first-search would simply try the first possible move, and then the first possible move in the new situation, and the first possible move in that new situation, until success o...
0
2010-01-26T06:11:42Z
[ "python", "depth-first-search" ]
Depth-First search in Python
2,137,731
<p>I'm trying to do a Depth-First search in Python but it's not working.</p> <p>Basically we have a peg-solitaire board:</p> <pre><code>[1,1,1,1,1,0,1,1,1,1] </code></pre> <p>1's represent a peg, and 0 is an open spot. You must move a peg one at a time TWO SLOTS backwards or forward ONLY to an empty spot. If you ju...
3
2010-01-26T06:00:34Z
2,137,789
<p>The normal way to implement DFS in a situation where each step is a "move" from a "board position" to some possible next one, until a goal is reached, is as follows (pseudocode)</p> <pre><code>seenpositions = set() currentpositions = set([startingposition]) while currentpositions: nextpositions = set() for p in...
8
2010-01-26T06:17:07Z
[ "python", "depth-first-search" ]
Depth-First search in Python
2,137,731
<p>I'm trying to do a Depth-First search in Python but it's not working.</p> <p>Basically we have a peg-solitaire board:</p> <pre><code>[1,1,1,1,1,0,1,1,1,1] </code></pre> <p>1's represent a peg, and 0 is an open spot. You must move a peg one at a time TWO SLOTS backwards or forward ONLY to an empty spot. If you ju...
3
2010-01-26T06:00:34Z
2,137,798
<p>The basic algorithmic problem is that the <code>succ</code> function always only produces just one possible move for a given board state. Even if there would be more than one possible moves, the <code>succ</code> function just returns the first one it can find. A depth first search needs to process all possible move...
0
2010-01-26T06:19:56Z
[ "python", "depth-first-search" ]
Django - Store unescaped html in model
2,137,736
<p>I am trying to store raw, unescaped HTML inside one of my Django models for display on my home page. However, when I store it in a TextField it gets escaped, and ends up just being displayed as raw text. How can I store raw HTML in a Django model?</p> <p>** EDIT **</p> <p>It seems as if its not getting escaped in ...
10
2010-01-26T06:01:57Z
2,137,790
<p>You can use the <code>safe</code> filter to present unescaped text, or <code>escape</code> filter to present escaped text. You can also use <code>autoescape</code> tag to set a block. (<code>{% autoescape on %}</code> or <code>{% autoescape off %}</code>)</p>
15
2010-01-26T06:17:25Z
[ "python", "django", "django-models" ]
Change class instance inside an instance method
2,137,772
<p>Any idea if there is a way to make the following code to work</p> <pre><code>class Test(object): def __init__(self, var): self.var = var def changeme(self): self = Test(3) t = Test(1) assert t.var == 1 t.changeme() assert t.var == 3 </code></pre> <p>is something like the following safe t...
6
2010-01-26T06:13:28Z
2,137,786
<p>No, and no.</p> <p>Having said that, you <em>can</em> change the <em>class</em>, but don't do that either.</p>
3
2010-01-26T06:16:32Z
[ "python", "class", "instance", "hotswap" ]
Change class instance inside an instance method
2,137,772
<p>Any idea if there is a way to make the following code to work</p> <pre><code>class Test(object): def __init__(self, var): self.var = var def changeme(self): self = Test(3) t = Test(1) assert t.var == 1 t.changeme() assert t.var == 3 </code></pre> <p>is something like the following safe t...
6
2010-01-26T06:13:28Z
2,137,800
<p>The former code works, except it won't do much, seeing as it just replaces the object named 'self' within the scope of changeme(). Python names aren't locked to values, they are always relative to their scope or namespace.</p> <p>To do what you want you'd need to have access to a name outside the class, which you c...
2
2010-01-26T06:20:08Z
[ "python", "class", "instance", "hotswap" ]
Change class instance inside an instance method
2,137,772
<p>Any idea if there is a way to make the following code to work</p> <pre><code>class Test(object): def __init__(self, var): self.var = var def changeme(self): self = Test(3) t = Test(1) assert t.var == 1 t.changeme() assert t.var == 3 </code></pre> <p>is something like the following safe t...
6
2010-01-26T06:13:28Z
2,137,801
<p><code>self = Test(3)</code> is re-binding the local name <code>self</code>, with no externally observable effects.</p> <p>Assigning <code>self.__dict__</code> (unless you're talking about instances with <code>__slots__</code> or from classes with non-trivial metaclasses) is usually OK, and so is <code>self.__init__...
7
2010-01-26T06:20:31Z
[ "python", "class", "instance", "hotswap" ]
Problems using PIL 1.1.7
2,137,953
<p>The trivial </p> <pre><code>import Image im = Image.OPEN('C:\abc.bmp') </code></pre> <p>results in the following exception</p> <pre><code>Traceback (most recent call last): File "&lt;pyshell#1&gt;", line 1, in &lt;module&gt; im = Image.OPEN('C:\Documents and Settings\umair.ahmed\My Documents\My Pictures\ava...
3
2010-01-26T07:12:21Z
2,137,965
<p>Use:</p> <pre><code>Image.open() </code></pre> <p>It's case-sensitive.</p>
4
2010-01-26T07:14:43Z
[ "python", "python-imaging-library" ]
Problems using PIL 1.1.7
2,137,953
<p>The trivial </p> <pre><code>import Image im = Image.OPEN('C:\abc.bmp') </code></pre> <p>results in the following exception</p> <pre><code>Traceback (most recent call last): File "&lt;pyshell#1&gt;", line 1, in &lt;module&gt; im = Image.OPEN('C:\Documents and Settings\umair.ahmed\My Documents\My Pictures\ava...
3
2010-01-26T07:12:21Z
2,137,971
<p>I don't think the error message came from your input, because the file names are different, but you should not use <code>'C:\abc.bmp'</code>, in your <code>open()</code> call, but use either <code>C:/abc.bmp</code>, or <code>r'C:\abc.bmp'</code>. Backslash is an escape character in Python.</p>
1
2010-01-26T07:15:28Z
[ "python", "python-imaging-library" ]
Creating an Instance of a Class with a variable in Python
2,138,027
<p>I had <a href="http://stackoverflow.com/questions/2136760/creating-an-instance-of-a-class-with-a-variable-in-python">this question</a> posted earlier, but it wasn't very clear, and I had trouble with the answers. Since I edited it to make MUCH more sense it seems that people haven't been looking at it, perhaps becau...
1
2010-01-26T07:33:31Z
2,138,048
<p>There is no point in making a special getNewToy function. Just create the class:</p> <pre><code> newtoy = Toy(name, data1, data2, data3, data4, data5) </code></pre> <p>That's all you need to do.</p> <p>"method I want the instance to reflect the name of the Toy each time one is called."</p> <pre><code>class Toy: ...
2
2010-01-26T07:39:41Z
[ "python", "class" ]
Creating an Instance of a Class with a variable in Python
2,138,027
<p>I had <a href="http://stackoverflow.com/questions/2136760/creating-an-instance-of-a-class-with-a-variable-in-python">this question</a> posted earlier, but it wasn't very clear, and I had trouble with the answers. Since I edited it to make MUCH more sense it seems that people haven't been looking at it, perhaps becau...
1
2010-01-26T07:33:31Z
2,138,052
<p>I don't see why you need a <code>getNewToy</code> method. Every time you call <code>Toy()</code> you will get a new instance of your class. You probably want code like this:</p> <pre><code>class Toy: def __init__(self, name, data1, data2, data3, data4, data5): self.name = name self.data1 = dat...
1
2010-01-26T07:40:44Z
[ "python", "class" ]
Creating an Instance of a Class with a variable in Python
2,138,027
<p>I had <a href="http://stackoverflow.com/questions/2136760/creating-an-instance-of-a-class-with-a-variable-in-python">this question</a> posted earlier, but it wasn't very clear, and I had trouble with the answers. Since I edited it to make MUCH more sense it seems that people haven't been looking at it, perhaps becau...
1
2010-01-26T07:33:31Z
2,138,201
<p>Heres how I would do what you explained:</p> <pre><code># The following two classes are toys, both have a playWith # as you wanted, each playWith do different things class Ball: def __init__(self): self.name = "ball" def playWith(self): print "the ball bounces" class Car: def __init__...
2
2010-01-26T08:28:08Z
[ "python", "class" ]
Creating an Instance of a Class with a variable in Python
2,138,027
<p>I had <a href="http://stackoverflow.com/questions/2136760/creating-an-instance-of-a-class-with-a-variable-in-python">this question</a> posted earlier, but it wasn't very clear, and I had trouble with the answers. Since I edited it to make MUCH more sense it seems that people haven't been looking at it, perhaps becau...
1
2010-01-26T07:33:31Z
2,139,120
<p>Perhaps you will always have 5 data items, but even if so, consider using *args:</p> <pre><code>class Toy: def __init__(self, name, *args): self.name = name self.data = args </code></pre>
0
2010-01-26T11:48:18Z
[ "python", "class" ]
RegFree DLL with CPython using win32com module
2,138,415
<p>I am currently using the win32com module of CPython to use a DLL. I know some people using IronPython to automagically get the list of functions provided by this DLL. They don't need to register the DLL.</p> <p>I'd like to do the same as them but with CPython.</p> <p>1) Is it possible to use CPython and win32com t...
3
2010-01-26T09:18:16Z
13,718,199
<p>This kind of information is <em>hard</em>, very very hard to find information about. I am attempting to find the same but for Ruby, however I expect that the same issue will occur. Ultimately, however, a Manifest file is required, and at least during development, it may help to register the .dll anyway, and then w...
0
2012-12-05T07:20:53Z
[ "python", "dll", "regfreecom", "win32com" ]
Python bind - allow multiple keys to be pressed simultaniously
2,138,518
<p>I have a problem in Python.</p> <p>I'm using Tkinter and have four bind events, that listen to key presses on my form. My problem is, that these don't run asynchronously. So, for example I can press one button, and the events are recognized. But when I press and hold two keys at the same time, just one event gets f...
2
2010-01-26T09:38:11Z
2,140,341
<p>You could bind to "&lt;Key&gt;" and then check event.char and perform the action you want based on the value of that? Granted, I have no idea if this works when multiple keys are pressed at the same time, it may still run in to the exact same problem. I haven't used Tk in ages.</p> <p>"&lt;Key&gt; The user press...
0
2010-01-26T15:23:19Z
[ "python", "bind", "tkinter", "keyboard-events" ]
Python bind - allow multiple keys to be pressed simultaniously
2,138,518
<p>I have a problem in Python.</p> <p>I'm using Tkinter and have four bind events, that listen to key presses on my form. My problem is, that these don't run asynchronously. So, for example I can press one button, and the events are recognized. But when I press and hold two keys at the same time, just one event gets f...
2
2010-01-26T09:38:11Z
2,141,044
<p>Unfortunately, you are somewhat at the mercy of the underlying auto-repeat mechanism of your system. For example, on the mac I'm using at the moment if I press and hold "w" I'll get a stream of press and release events. While pressed, if I press "o" I get a stream of presses and releases for "o" but no more events f...
5
2010-01-26T17:03:27Z
[ "python", "bind", "tkinter", "keyboard-events" ]
How can I link (C++) with renamed Python .lib and .dll?
2,138,625
<p>When I include "Python.h" from Python 2.5 in a C++ project, it knows through some magical process that it has to link with "python25.lib" and load "python25.dll" at runtime, though I didn't specified anything neither in "Linker -> Additional Dependencies" nor in "Linker -> Additional Library Directories".</p> <p>No...
4
2010-01-26T09:57:20Z
2,138,641
<p>MSVC supports this feature through pragmas:</p> <pre><code>#pragma comment(lib, "python25.lib"); </code></pre> <p>More info in <a href="http://msdn.microsoft.com/en-us/library/7f0aews7%28VS.80%29.aspx" rel="nofollow">MSDN</a>.</p> <p>Look into Python.h file and modify the name of the linkage, if that what you wan...
1
2010-01-26T09:59:41Z
[ "c++", "python", "dll", "linker" ]
Signing requests in Python for OAuth
2,138,656
<p>currently I'm interfacing the Twitter API using the OAuth protocol and writing the code in Python. As most of the users out there, I think the toughest part of the specs is dealing with signatures.</p> <p>After wandering around the web in search for a solution, I decided to go for my custom code, so as to have a be...
4
2010-01-26T10:02:53Z
2,159,965
<p>My knee-jerk reaction to this is <a href="http://chargen.matasano.com/chargen/2009/7/22/if-youre-typing-the-letters-a-e-s-into-your-code-youre-doing.html" rel="nofollow">If You're Typing The Letters A-E-S Into Your Code, You're Doing It Wrong</a>. Or, as redditor <a href="http://www.reddit.com/r/netsec/comments/att...
3
2010-01-29T04:59:08Z
[ "python", "oauth", "signature" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
2,138,894
<pre><code>item = defaultvalue for item in my_iter: pass </code></pre>
41
2010-01-26T10:56:42Z
[ "python", "iterator" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
2,138,903
<p>I would use <code>reversed</code>, except that it only takes sequences instead of iterators, which seems rather arbitrary. </p> <p>Any way you do it, you'll have to run through the entire iterator. At maximum efficiency, if you don't need the iterator ever again, you could just trash all the values:</p> <pre><co...
1
2010-01-26T10:57:54Z
[ "python", "iterator" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
2,139,104
<p>There's this</p> <pre><code>list( the_iter )[-1] </code></pre> <p>If the length of the iteration is truly epic -- so long that materializing the list will exhaust memory -- then you really need to rethink the design.</p>
0
2010-01-26T11:42:54Z
[ "python", "iterator" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
2,139,171
<p>This is unlikely to be faster than the empty for loop due to the lambda, but maybe it will give someone else an idea</p> <pre><code>reduce(lambda x,y:y,my_iter) </code></pre> <p>If the iter is empty, a TypeError is raised</p>
13
2010-01-26T11:59:14Z
[ "python", "iterator" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
2,211,077
<p>Probably worth using <code>__reversed__</code> if it is available</p> <pre><code>if hasattr(my_iter,'__reversed__'): last = next(reversed(my_iter)) else: for last in my_iter: pass </code></pre>
15
2010-02-05T23:01:46Z
[ "python", "iterator" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
3,137,506
<p>The question is wrong and can only lead to an answer that is complicated and inefficient. To get an iterator, you of course start out from something that is iterable, which will in most cases offer a more direct way of accessing the last element. </p> <p>Once you create an iterator from an iterable you are stuck i...
-5
2010-06-29T02:37:01Z
[ "python", "iterator" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
3,169,701
<p>Use a <a href="https://docs.python.org/library/collections.html#collections.deque"><code>deque</code></a> of size 1.</p> <pre><code>from collections import deque #aa is an interator aa = iter('apple') dd = deque(aa, maxlen=1) last_element = dd.pop() </code></pre>
26
2010-07-02T23:39:12Z
[ "python", "iterator" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
4,672,292
<p>See this code for something similar:</p> <p><a href="http://excamera.com/sphinx/article-islast.html" rel="nofollow">http://excamera.com/sphinx/article-islast.html</a></p> <p>you might use it to pick up the last item with:</p> <pre><code>[(last, e) for (last, e) in islast(the_iter) if last] </code></pre>
1
2011-01-12T18:08:48Z
[ "python", "iterator" ]
Cleanest way to get last item from Python iterator
2,138,873
<p>What's the best way of getting the last item from an iterator in Python 2.6? For example, say</p> <pre><code>my_iter = iter(range(5)) </code></pre> <p>What is the shortest-code / cleanest way of getting <code>4</code> from <code>my_iter</code>?</p> <p>I could do this, but it doesn't seem very efficient:</p> <pre...
47
2010-01-26T10:53:07Z
6,251,297
<p>As simple as:</p> <pre class="lang-py prettyprint-override"><code>max(enumerate(the_iter))[1] </code></pre>
4
2011-06-06T11:24:17Z
[ "python", "iterator" ]
Import by file/module name in GAE
2,139,110
<p>I am trying to make a simple localization module that takes a key name and returns the localized string based on the language given. The language is one of the constants and maps to a python file that contains the string table. I want to do this dynamically at runtime. Below is my approach, but GAE does not support ...
2
2010-01-26T11:45:04Z
2,139,168
<p>The alternative to the imp module that (I believe) should be available in GAE is <code>__import__()</code>. It's in fact what the 'import' statement calls to do the actual importing.</p> <pre><code>user_language = getattr(__import__('localizable.%s' % language), language) </code></pre> <p>or</p> <pre><code>user_l...
2
2010-01-26T11:58:48Z
[ "python", "google-app-engine" ]
Build Python scripts and call methods from C#
2,139,202
<p>Is there any way to make this scenario work?</p> <p>There is a Python script. It is built into a DLL by running this script with IronPython:</p> <pre><code>import clr clr.CompileModules("CompiledScript.dll", "script.py") </code></pre> <p>The goal is to call this DLL's methods from C# code. <a href="http://en.wiki...
10
2010-01-26T12:03:25Z
2,141,958
<p>The <code>clr.CompileModules</code> is purely a load-time optimization - it doesn't make the scripts directly available to a static languge like C#. You'll need to host the IronPython runtime, and then you can load the DLL into the runtime and use IronPython's hosting interfaces to access it.</p>
6
2010-01-26T19:21:16Z
[ "c#", "python", "ironpython" ]
Build Python scripts and call methods from C#
2,139,202
<p>Is there any way to make this scenario work?</p> <p>There is a Python script. It is built into a DLL by running this script with IronPython:</p> <pre><code>import clr clr.CompileModules("CompiledScript.dll", "script.py") </code></pre> <p>The goal is to call this DLL's methods from C# code. <a href="http://en.wiki...
10
2010-01-26T12:03:25Z
2,142,224
<p>Sort of. You cannot access the Python methods directly from C# code. Unless you are playing with C# 4.0 and the dynamic keyword or you are very, very special ;). However, you can compile an IronPython class to a DLL and then use IronPython hosting in C# to access the methods (this is for IronPython 2.6 and .NET 2.0...
7
2010-01-26T20:02:38Z
[ "c#", "python", "ironpython" ]
how to install open-library on ubuntu?
2,139,262
<p>i want to install openlibrary in my ubuntu system and i followed the instruction from the below link <a href="http://openlibrary.org/dev/docs/setup" rel="nofollow">http://openlibrary.org/dev/docs/setup</a> </p> <pre><code>$ ./scripts/openlibrary-server openlibrary.yml </code></pre> <p>while i am using the abov...
0
2010-01-26T12:16:26Z
2,139,272
<p>You will need to install 'infogami'. See <a href="http://infogami.org/InstallGuide?v=12" rel="nofollow">here</a></p>
2
2010-01-26T12:18:50Z
[ "python", "ubuntu", "installation" ]
breaking a 32-bit number into individual fields
2,139,377
<p>Is there a quick way in python to split a 32-bit variable, say, <code>a1a2a3a4</code> into <code>a1</code>, <code>a2</code>, <code>a3</code>, <code>a4</code> quickly? I've done it by changing the value into hex and then splitting it, but it seems like a waste of time doing <code>int</code>-><code>string</code>-><cod...
2
2010-01-26T12:43:40Z
2,139,394
<p>There isn't really such thing as a 32-bit variable in Python. Is this what you mean?</p> <pre><code>x = 0xa1a2a3a4 a4 = int(x &amp; 0xff) a3 = int((x &gt;&gt; 8) &amp; 0xff) a2 = int((x &gt;&gt; 16) &amp; 0xff) a1 = int(x &gt;&gt; 24) </code></pre> <p>As SilentGhost points out, you don't need the int conversion, s...
5
2010-01-26T12:48:12Z
[ "python" ]
breaking a 32-bit number into individual fields
2,139,377
<p>Is there a quick way in python to split a 32-bit variable, say, <code>a1a2a3a4</code> into <code>a1</code>, <code>a2</code>, <code>a3</code>, <code>a4</code> quickly? I've done it by changing the value into hex and then splitting it, but it seems like a waste of time doing <code>int</code>-><code>string</code>-><cod...
2
2010-01-26T12:43:40Z
2,139,402
<p>Use bitwise operations.</p> <p>For example:</p> <pre><code>myvar = 0xcdf100cd a0 = myvar&amp;0xff a1 = (myvar&gt;&gt;8)&amp;0xff a2 = (myvar&gt;&gt;16)&amp;0xff a3 = (myvar&gt;&gt;24)&amp;0xff </code></pre> <p>the <code>&amp;0xff</code> keeps the lower 8 bits of the value and zeros the rest, while the bitshift op...
2
2010-01-26T12:50:54Z
[ "python" ]
breaking a 32-bit number into individual fields
2,139,377
<p>Is there a quick way in python to split a 32-bit variable, say, <code>a1a2a3a4</code> into <code>a1</code>, <code>a2</code>, <code>a3</code>, <code>a4</code> quickly? I've done it by changing the value into hex and then splitting it, but it seems like a waste of time doing <code>int</code>-><code>string</code>-><cod...
2
2010-01-26T12:43:40Z
2,141,018
<p>Standard library module <a href="http://docs.python.org/library/struct.html?highlight=struct#module-struct">struct</a> does short work of it:</p> <pre><code>&gt;&gt;&gt; import struct &gt;&gt;&gt; x = 0xa1a2a3a4 &gt;&gt;&gt; struct.unpack('4B', struct.pack('&gt;I', x)) (161, 162, 163, 164) </code></pre> <p>"packin...
10
2010-01-26T16:59:03Z
[ "python" ]
Can I use WSGI with a URI that has spaces?
2,139,613
<p>I wrote a small WSGI App:</p> <pre><code>def foo(environ, start_response): bar = 'Your request is %s' % environ['PATH_INFO'] status = '200 OK' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(bar)))] start_response(status, res...
1
2010-01-26T13:28:07Z
2,139,682
<p>You should use "%20" in URL's to encode spaces into then -- but don't do that manually: use urllib.quote function, like:</p> <blockquote> <blockquote> <blockquote> <p>import urllib base = "http://localhost:8000/" path = urllib.quote("foo bar") checkURL = base + path</p> </blockquot...
-1
2010-01-26T13:41:24Z
[ "python", "wsgi", "spaces" ]
Can I use WSGI with a URI that has spaces?
2,139,613
<p>I wrote a small WSGI App:</p> <pre><code>def foo(environ, start_response): bar = 'Your request is %s' % environ['PATH_INFO'] status = '200 OK' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(bar)))] start_response(status, res...
1
2010-01-26T13:28:07Z
2,139,690
<p><strong>Update</strong></p> <p>Typically a WSGI URI will look something like <code>localhost:8000/foo/bar/baz</code> or <code>localhost:8000/?foo=bar</code> and not use spaces, so I suspect that the server is timing out because it doesn't have built-in handling for spaces. </p> <p>Maybe your question is really <st...
0
2010-01-26T13:42:06Z
[ "python", "wsgi", "spaces" ]
Can I use WSGI with a URI that has spaces?
2,139,613
<p>I wrote a small WSGI App:</p> <pre><code>def foo(environ, start_response): bar = 'Your request is %s' % environ['PATH_INFO'] status = '200 OK' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(bar)))] start_response(status, res...
1
2010-01-26T13:28:07Z
2,139,976
<p>From <a href="http://www.ietf.org/rfc/rfc2396.txt" rel="nofollow">http://www.ietf.org/rfc/rfc2396.txt</a></p> <p>The space character is excluded because significant spaces may disappear and insignificant spaces may be introduced when URI are transcribed or typeset or subjected to the treatment of word- pro...
1
2010-01-26T14:27:11Z
[ "python", "wsgi", "spaces" ]
Can I use WSGI with a URI that has spaces?
2,139,613
<p>I wrote a small WSGI App:</p> <pre><code>def foo(environ, start_response): bar = 'Your request is %s' % environ['PATH_INFO'] status = '200 OK' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(bar)))] start_response(status, res...
1
2010-01-26T13:28:07Z
2,146,438
<p>I moved from wsgiref.simple_server to cherrypy and it work well. Client request will time out after about 1s. Thanks jcoon and S.Lott very much!</p>
0
2010-01-27T11:30:59Z
[ "python", "wsgi", "spaces" ]
How to read a CSV line with "?
2,139,750
<p>A trivial CSV line could be spitted using string split function. But some lines could have <code>"</code>, e.g.</p> <pre><code>"good,morning", 100, 300, "1998,5,3" </code></pre> <p>thus directly using string split would not solve the problem. </p> <p>My solution is to first split out the line using <code>,</code>...
4
2010-01-26T13:52:03Z
2,139,763
<p>There's a <a href="http://docs.python.org/library/csv.html" rel="nofollow">csv</a> module in Python, which handles this.</p> <p><strong>Edit</strong>: This task falls into "build a lexer" category. The standard way to do such tasks is to build a state machine (or use a lexer library/framework that will do it for yo...
9
2010-01-26T13:53:48Z
[ "python", "csv" ]
How to read a CSV line with "?
2,139,750
<p>A trivial CSV line could be spitted using string split function. But some lines could have <code>"</code>, e.g.</p> <pre><code>"good,morning", 100, 300, "1998,5,3" </code></pre> <p>thus directly using string split would not solve the problem. </p> <p>My solution is to first split out the line using <code>,</code>...
4
2010-01-26T13:52:03Z
2,139,776
<p>From <a href="http://docs.python.org/library/csv.html#csv-examples" rel="nofollow">python's CSV module</a>:</p> <p>reading a normal CSV file:</p> <pre><code>import csv reader = csv.reader(open("some.csv", "rb")) for row in reader: print row </code></pre> <p>Reading a file with an alternate format:</p> <pre><...
3
2010-01-26T13:56:49Z
[ "python", "csv" ]
How to read a CSV line with "?
2,139,750
<p>A trivial CSV line could be spitted using string split function. But some lines could have <code>"</code>, e.g.</p> <pre><code>"good,morning", 100, 300, "1998,5,3" </code></pre> <p>thus directly using string split would not solve the problem. </p> <p>My solution is to first split out the line using <code>,</code>...
4
2010-01-26T13:52:03Z
2,139,863
<p><em>Chapter 4 of The Practice of Programming</em> gave both C and C++ implementations of the CSV parser.</p>
1
2010-01-26T14:06:33Z
[ "python", "csv" ]
How to read a CSV line with "?
2,139,750
<p>A trivial CSV line could be spitted using string split function. But some lines could have <code>"</code>, e.g.</p> <pre><code>"good,morning", 100, 300, "1998,5,3" </code></pre> <p>thus directly using string split would not solve the problem. </p> <p>My solution is to first split out the line using <code>,</code>...
4
2010-01-26T13:52:03Z
2,139,887
<p>The generic implementation detail would be something like this (untested)</p> <pre><code>def csvline2fields(line): fields = [] quote = None while line.strip(): line = line.strip() if line[0] in ("'", '"'): # Find the next quote: end = line.find(line[0]) ...
1
2010-01-26T14:09:55Z
[ "python", "csv" ]
Bash or Python or Awk to match and modify files
2,139,823
<p>I have a set of 10000 files c1.dat ... c10000.dat. Each of these files contains a line which starts with @ and contains a string with spaces specific for this file, lije c37 7.379 6.23.</p> <p>I have another set of 10000 files kind of determined_cXXX_send.dat (where XXX goes from 1 to 10000). Each of these files h...
1
2010-01-26T14:02:16Z
2,139,845
<p>A language basically made for processing text: <a href="http://www.perl.org/" rel="nofollow">Perl!</a></p>
1
2010-01-26T14:04:31Z
[ "python", "bash", "awk" ]
Bash or Python or Awk to match and modify files
2,139,823
<p>I have a set of 10000 files c1.dat ... c10000.dat. Each of these files contains a line which starts with @ and contains a string with spaces specific for this file, lije c37 7.379 6.23.</p> <p>I have another set of 10000 files kind of determined_cXXX_send.dat (where XXX goes from 1 to 10000). Each of these files h...
1
2010-01-26T14:02:16Z
2,139,893
<p>If each of the two types of files only has one line:</p> <pre><code>for i in {1..10000} do paste "c${i}.dat" "determined_c${i}_send.dat" &gt; c${i}.out &amp;&amp; mv "c{$i}.out" "determined_c${i}_send.dat" done </code></pre> <p><strong>Edit:</strong></p> <pre><code>for i in {1..10000} do line=$(grep ...
1
2010-01-26T14:10:37Z
[ "python", "bash", "awk" ]
Bash or Python or Awk to match and modify files
2,139,823
<p>I have a set of 10000 files c1.dat ... c10000.dat. Each of these files contains a line which starts with @ and contains a string with spaces specific for this file, lije c37 7.379 6.23.</p> <p>I have another set of 10000 files kind of determined_cXXX_send.dat (where XXX goes from 1 to 10000). Each of these files h...
1
2010-01-26T14:02:16Z
2,139,942
<p>In Python, you could do something like that</p> <pre><code># loop on all the files for num in range(1,1000): cfile = open ( 'c%u.dat'%num, mode='r') # find the specific line for line in cfile: if line[0]=='@': # open the determined file and add the line dfile = open( '...
1
2010-01-26T14:20:34Z
[ "python", "bash", "awk" ]
Bash or Python or Awk to match and modify files
2,139,823
<p>I have a set of 10000 files c1.dat ... c10000.dat. Each of these files contains a line which starts with @ and contains a string with spaces specific for this file, lije c37 7.379 6.23.</p> <p>I have another set of 10000 files kind of determined_cXXX_send.dat (where XXX goes from 1 to 10000). Each of these files h...
1
2010-01-26T14:02:16Z
2,139,973
<p>Doing this in Python should be pretty trivial. It's probably possible in awk, but sounds a bit too complicated to be fun. It's surely is possible in bash, but programming in bash is for masochists.</p> <p>I'd go with Python, of the given options, although Perl and Ruby are good options too if you know them.</p>
0
2010-01-26T14:26:29Z
[ "python", "bash", "awk" ]
Bash or Python or Awk to match and modify files
2,139,823
<p>I have a set of 10000 files c1.dat ... c10000.dat. Each of these files contains a line which starts with @ and contains a string with spaces specific for this file, lije c37 7.379 6.23.</p> <p>I have another set of 10000 files kind of determined_cXXX_send.dat (where XXX goes from 1 to 10000). Each of these files h...
1
2010-01-26T14:02:16Z
2,140,017
<p>if "c37 7.379 6.23" is constant, then there's no need to grab this string from cXXX.dat files. But i am guessing this string is dynamic ,and it comes after @., so you can try this</p> <pre><code>#!/bin/bash shopt -s nullglob for file in c{1..1000}.dat do if [ -e "$file" ];then tag=${file%.dat} w...
0
2010-01-26T14:34:35Z
[ "python", "bash", "awk" ]
Ping a network using threads and testing it
2,139,947
<p>I am trying to ping two different networks with thread. I am able to get the response I want but I want to convert it into a test. I have the code that I have tried below but the test runner says that no tests were run. The code is below:</p> <pre><code>#!/home/workspace/downloads/Python-2.6.4/python from threading...
0
2010-01-26T14:21:01Z
2,140,044
<p>When you subclass <code>unittest.TestCase</code>, all methods whose name begins with <code>test</code>, get run automatically. Otherwise, the code is not run as a test. (So <code>RunTest</code> is not getting run).</p> <p>So if you change <code>RunTest</code> to (the not so mellifluous) <code>test_RunTest</code>:</...
3
2010-01-26T14:38:27Z
[ "python", "testing", "multithreading", "ping" ]
PyFacebook Infinite Session
2,140,142
<p>I am trying to set a cron task to read updates for a Facebook application. I have prompted the user to grant Offline Access permissions and i have store the session_key in the db.</p> <p>I am crearing a new Facebook object and besides api and secret key I also use the session_key (previously stored in db) and the f...
2
2010-01-26T14:53:29Z
2,140,233
<p>I've never used PyFacebook. Or tried to resume sessions in this manner. But I'd imagine just storing <code>session_key</code> and <code>uid</code> is not enough. There are other params too, and a signature param that's calculated based on all the <code>fb_*</code> params. So you might need to store all of them.</p> ...
0
2010-01-26T15:08:02Z
[ "python", "facebook", "pyfacebook" ]
PyFacebook Infinite Session
2,140,142
<p>I am trying to set a cron task to read updates for a Facebook application. I have prompted the user to grant Offline Access permissions and i have store the session_key in the db.</p> <p>I am crearing a new Facebook object and besides api and secret key I also use the session_key (previously stored in db) and the f...
2
2010-01-26T14:53:29Z
2,188,152
<p>I just wrote a <a href="http://blog.crater.net/2010/02/writing-basic-pyfacebook-desktop-app.html" rel="nofollow">blog entry</a> about my search for something similar - need to write a Python cron script. Here's what I came up with:</p> <pre><code>#!/usr/bin/python import os, sys, optparse, time, json import facebo...
2
2010-02-02T22:18:11Z
[ "python", "facebook", "pyfacebook" ]
PyFacebook Infinite Session
2,140,142
<p>I am trying to set a cron task to read updates for a Facebook application. I have prompted the user to grant Offline Access permissions and i have store the session_key in the db.</p> <p>I am crearing a new Facebook object and besides api and secret key I also use the session_key (previously stored in db) and the f...
2
2010-01-26T14:53:29Z
11,015,676
<p>I faced the same problem where the error displayed was:</p> <blockquote> <p>"facebook.FacebookError: Error 104: Incorrect signature"</p> </blockquote> <p>Just reset your APP Secret_key and make corresponding change in the code and that sgould fix the problem.</p> <p>Cheers!</p>
1
2012-06-13T13:16:47Z
[ "python", "facebook", "pyfacebook" ]
Ignoring modules other than own in testoob coverage reporting
2,140,219
<p>I use testoob in the following way:</p> <pre><code>def suite(): import unittest return unittest.TestLoader().loadTestsFromNames([ 'my_module.my_unittest_class', 'my_module.my_other_unittest_class', ]) if __name__ == '__main__': import testoob testoob.main(defaultTest="suite") ...
1
2010-01-26T15:04:28Z
12,791,902
<p>I ended up overriding the private <code>_should_cover_frame</code> function in the <code>testoob</code> <code>Coverage</code> class and comparing the frame's file path to my module. Not the nicest solution, but at least it works.</p> <pre><code>from testoob.coverage import Coverage orig_should_cover = Coverage._sho...
0
2012-10-09T02:01:52Z
[ "python", "code-coverage" ]
Reformatting code with Regular Expressions
2,140,268
<p>We have an <code>ArrayList</code> of items in several classes which are giving me trouble every time I'd like to insert a new item into the list. It was a mistake on my part to have designed the classes in the way I did but changing the design now would be more headache than it's worth (bureaucratic waterfall model...
6
2010-01-26T15:13:56Z
2,140,295
<p>Don't do this with regexp. Create symbolic constants (using for example an enum) that map the names to numbers.</p>
4
2010-01-26T15:17:38Z
[ "java", "python", "regex", "perl" ]
Reformatting code with Regular Expressions
2,140,268
<p>We have an <code>ArrayList</code> of items in several classes which are giving me trouble every time I'd like to insert a new item into the list. It was a mistake on my part to have designed the classes in the way I did but changing the design now would be more headache than it's worth (bureaucratic waterfall model...
6
2010-01-26T15:13:56Z
2,140,302
<p>I'm doing this (well, something very similar) right now, but using Excel and VBA macros. All of the Business Values are organized and ordered in a spreadsheet. I just have to click a button to generate the appropriate code for the selected cells, then copy-paste to the IDE. Better yet, I have several "code columns" ...
0
2010-01-26T15:18:16Z
[ "java", "python", "regex", "perl" ]
Reformatting code with Regular Expressions
2,140,268
<p>We have an <code>ArrayList</code> of items in several classes which are giving me trouble every time I'd like to insert a new item into the list. It was a mistake on my part to have designed the classes in the way I did but changing the design now would be more headache than it's worth (bureaucratic waterfall model...
6
2010-01-26T15:13:56Z
2,140,681
<p>You want your regular expression to be as flexible as the compiler will be with respect to whitespace between tokens. Doing so and mimicking whitespace usage makes the pattern pretty messy. The code below (sorry: Perl, not Python) edits your source files in-place.</p> <pre><code>#! /usr/bin/perl -i.bak use warn...
4
2010-01-26T16:13:19Z
[ "java", "python", "regex", "perl" ]
Reformatting code with Regular Expressions
2,140,268
<p>We have an <code>ArrayList</code> of items in several classes which are giving me trouble every time I'd like to insert a new item into the list. It was a mistake on my part to have designed the classes in the way I did but changing the design now would be more headache than it's worth (bureaucratic waterfall model...
6
2010-01-26T15:13:56Z
2,140,981
<p>Comments about bad-practices apart - here is the code you asked in the language you asked for. The best thing if you are keeping the system this way, probably would be to make these java files be automatically generated in the build process itself -- you 'd just keep a names list in a .txt file in the directory. Thi...
4
2010-01-26T16:52:37Z
[ "java", "python", "regex", "perl" ]
How to require login for django generic views?
2,140,550
<p>I want to restrict access to urls serves by django generic views. For my views i know that login_required decorator does the job. Also Create/update/delete generic views takes login_requied argument but I couldn't find a way to do this for other generic views. </p>
52
2010-01-26T15:59:27Z
2,140,596
<p>Use the following:</p> <pre><code>from django.contrib.auth.decorators import login_required @login_required def your_view(): # your code here </code></pre>
0
2010-01-26T16:04:21Z
[ "python", "django" ]
How to require login for django generic views?
2,140,550
<p>I want to restrict access to urls serves by django generic views. For my views i know that login_required decorator does the job. Also Create/update/delete generic views takes login_requied argument but I couldn't find a way to do this for other generic views. </p>
52
2010-01-26T15:59:27Z
2,140,678
<p>For Django &lt; 1.5, you can add a decorator by wrapping the function in your urls, which allows you to wrap the generic views:</p> <pre><code>from django.contrib.auth.decorators import login_required from django.views.generic.simple import direct_to_template urlpatterns = patterns('', (r'^foo/$', login_require...
73
2010-01-26T16:12:51Z
[ "python", "django" ]
How to require login for django generic views?
2,140,550
<p>I want to restrict access to urls serves by django generic views. For my views i know that login_required decorator does the job. Also Create/update/delete generic views takes login_requied argument but I couldn't find a way to do this for other generic views. </p>
52
2010-01-26T15:59:27Z
2,140,680
<p>If you don't want to write your own thin wrapper around the generic views in question (as Aamir suggested), you can also do something like this in your <code>urls.py</code> file:</p> <pre><code>from django.conf.urls.defaults import * # Directly import whatever generic views you're using and the login_required # de...
9
2010-01-26T16:13:05Z
[ "python", "django" ]
How to require login for django generic views?
2,140,550
<p>I want to restrict access to urls serves by django generic views. For my views i know that login_required decorator does the job. Also Create/update/delete generic views takes login_requied argument but I couldn't find a way to do this for other generic views. </p>
52
2010-01-26T15:59:27Z
5,891,307
<p>The generic views have changed from functions to objects with version 1.3 of Django. As such, there is a slight change needed for Will McCutchen and Will Hardy answers to work with version 1.3:</p> <pre><code>from django.contrib.auth.decorators import login_required from django.views.generic import TemplateView u...
31
2011-05-05T00:17:20Z
[ "python", "django" ]
How to require login for django generic views?
2,140,550
<p>I want to restrict access to urls serves by django generic views. For my views i know that login_required decorator does the job. Also Create/update/delete generic views takes login_requied argument but I couldn't find a way to do this for other generic views. </p>
52
2010-01-26T15:59:27Z
10,729,513
<p>I wanted a re-usable way to require auth on many views derived from generic views. I created a replacement dispatch function which I can add to my view class in the same way as it's other declarations.</p> <pre><code>class Index(generic.ListView): model = models.HomePage dispatch = auth.dispatch </code></p...
1
2012-05-24T00:01:00Z
[ "python", "django" ]
How to require login for django generic views?
2,140,550
<p>I want to restrict access to urls serves by django generic views. For my views i know that login_required decorator does the job. Also Create/update/delete generic views takes login_requied argument but I couldn't find a way to do this for other generic views. </p>
52
2010-01-26T15:59:27Z
14,709,812
<h2>Django 1.9 or using django-braces</h2> <p>Django 1.9 has introduced a <a href="https://docs.djangoproject.com/en/1.9/topics/auth/default/#django.contrib.auth.mixins.LoginRequiredMixin">LoginRequiredMixin</a> that is used thus:</p> <pre><code>from django.contrib.auth.mixins import LoginRequiredMixin class MyView(...
35
2013-02-05T14:27:03Z
[ "python", "django" ]
Python eval error suppression
2,140,614
<p>First off, I'm aware of eval's disadvantages and it will be used in an experiment I want to make only.</p> <p>I'm creating a script that works just like a Brute-Force algorithm but it won't break passwords but find the solution to a special form of an equation (more details are unnecessary).</p> <p>There will be l...
1
2010-01-26T16:06:12Z
2,140,635
<p>To suppress <a href="http://docs.python.org/library/exceptions.html#exceptions.SyntaxError" rel="nofollow"><code>SyntaxError</code></a>:</p> <pre><code>try: eval("1 + 2) + 3") except SyntaxError: pass </code></pre>
4
2010-01-26T16:08:15Z
[ "python", "eval" ]
Python eval error suppression
2,140,614
<p>First off, I'm aware of eval's disadvantages and it will be used in an experiment I want to make only.</p> <p>I'm creating a script that works just like a Brute-Force algorithm but it won't break passwords but find the solution to a special form of an equation (more details are unnecessary).</p> <p>There will be l...
1
2010-01-26T16:06:12Z
2,140,662
<p>Eval usually raises SyntaxError, you can cover your code with</p> <pre><code>try: a = eval('1+2)+3') except SyntaxError: pass </code></pre> <p>Remember, you can isolate <code>eval</code> from accessing any functions passing <code>{ '__builtin__': None }</code> as second parameter.</p>
1
2010-01-26T16:11:12Z
[ "python", "eval" ]
Python eval error suppression
2,140,614
<p>First off, I'm aware of eval's disadvantages and it will be used in an experiment I want to make only.</p> <p>I'm creating a script that works just like a Brute-Force algorithm but it won't break passwords but find the solution to a special form of an equation (more details are unnecessary).</p> <p>There will be l...
1
2010-01-26T16:06:12Z
6,812,514
<p>use literal_eval from ast instead and catch ValueError and SyntaxError</p> <pre><code>from ast import literal_eval try: a = literal_eval('1+2)+3') except (ValueError, SyntaxError): pass </code></pre>
1
2011-07-25T06:36:39Z
[ "python", "eval" ]
How can you search Github project Network for unmerged commits to a particular file?
2,140,756
<p>I'm working on a project that is hosted @ Github.com It seems that forum/models.py has some errors in it that are preventing me from syncdb.</p> <p>I was curious if there was a way to search through the network to find all the changes that had been made in the entire Branch Network to forum/models.py to see if some...
2
2010-01-26T16:23:31Z
2,140,788
<p>Take a look at these: <a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#exploring-git-history" rel="nofollow">Git history commands</a></p> <p>HIH</p> <p>...richie</p>
0
2010-01-26T16:27:26Z
[ "python", "git", "github" ]
How can you search Github project Network for unmerged commits to a particular file?
2,140,756
<p>I'm working on a project that is hosted @ Github.com It seems that forum/models.py has some errors in it that are preventing me from syncdb.</p> <p>I was curious if there was a way to search through the network to find all the changes that had been made in the entire Branch Network to forum/models.py to see if some...
2
2010-01-26T16:23:31Z
2,140,912
<p>May be 'git blame' can help you, if you know a line where error is located.</p> <p><a href="http://www.kernel.org/pub/software/scm/git-core/docs/git-blame.html" rel="nofollow">git blame manual</a></p>
0
2010-01-26T16:42:35Z
[ "python", "git", "github" ]
How can you search Github project Network for unmerged commits to a particular file?
2,140,756
<p>I'm working on a project that is hosted @ Github.com It seems that forum/models.py has some errors in it that are preventing me from syncdb.</p> <p>I was curious if there was a way to search through the network to find all the changes that had been made in the entire Branch Network to forum/models.py to see if some...
2
2010-01-26T16:23:31Z
2,141,221
<p>The closest thing I can think of is to do something like the following:</p> <pre><code>git log branch1 branch2 branch3 -- forum/models.py </code></pre> <p>where <code>branch1</code> etc. are the various branches, which you would need local copies of.</p>
1
2010-01-26T17:25:10Z
[ "python", "git", "github" ]
How can you search Github project Network for unmerged commits to a particular file?
2,140,756
<p>I'm working on a project that is hosted @ Github.com It seems that forum/models.py has some errors in it that are preventing me from syncdb.</p> <p>I was curious if there was a way to search through the network to find all the changes that had been made in the entire Branch Network to forum/models.py to see if some...
2
2010-01-26T16:23:31Z
2,142,616
<p>I contacted Github:<br> There isn't a search for unmerged commits, but if you hit the <a href="http://github.com/cnprog/CNPROG/forkqueue" rel="nofollow">forkqueue</a> you'll get a nice list of unmerged commits.<br> Assuming that people wrote good commit messages, you might be able to find the fix there if it exists....
1
2010-01-26T21:03:14Z
[ "python", "git", "github" ]
PyQt 4.7 - ImportError after installing on Windows
2,140,836
<p>I've been trying to install <strong>PyQt 4.7</strong> on Vista, but I am getting an ImportError when I try to do: <code>from PyQt4 import QtCore, QtGui</code>. </p> <blockquote> <p>ImportError: DLL load failed: The specified module could not be found.</p> </blockquote> <p>I've checked my System Path, and <strong...
12
2010-01-26T16:33:36Z
2,141,242
<p>Are you sure there is a <code>\bin</code> directory for your version of PyQt? I am running PyQt 4.4, and all my binaries are right in:</p> <p><strong>C:\Python31\Lib\site-packages\PyQt4</strong></p> <p>Which is what I have my path pointing too. It sounds like PyQt4 was installed improperly... did you run this?</p>...
5
2010-01-26T17:27:33Z
[ "python", "windows", "pyqt", "importerror" ]
PyQt 4.7 - ImportError after installing on Windows
2,140,836
<p>I've been trying to install <strong>PyQt 4.7</strong> on Vista, but I am getting an ImportError when I try to do: <code>from PyQt4 import QtCore, QtGui</code>. </p> <blockquote> <p>ImportError: DLL load failed: The specified module could not be found.</p> </blockquote> <p>I've checked my System Path, and <strong...
12
2010-01-26T16:33:36Z
4,452,810
<p>Just wanted to chime in that I had the same problem on a WinXP install of:</p> <ul> <li>python 2.7 </li> <li>Qt 4.7.1 (10.05)</li> <li>PyQt 4.8.1</li> </ul> <p>I used the windows installer version of all 3 of those items.</p> <p>Copying the contents of the <strong>C:\Python27\Lib\site-packages\PyQt4\bin</strong> ...
1
2010-12-15T17:14:39Z
[ "python", "windows", "pyqt", "importerror" ]
PyQt 4.7 - ImportError after installing on Windows
2,140,836
<p>I've been trying to install <strong>PyQt 4.7</strong> on Vista, but I am getting an ImportError when I try to do: <code>from PyQt4 import QtCore, QtGui</code>. </p> <blockquote> <p>ImportError: DLL load failed: The specified module could not be found.</p> </blockquote> <p>I've checked my System Path, and <strong...
12
2010-01-26T16:33:36Z
5,795,870
<p>Also chiming in. I installed both python 2.6 and PyQt 4.8.3 on a Windows 7 machine using the windows installers (I did NOT run 'python setup.py install').</p> <p>I tried to run spyder (which requires PyQt 4.4 or greater) and failed because it couldn't find the PyQt .dlls. I copied all the .dlls from the \Lib\site-p...
1
2011-04-26T20:11:24Z
[ "python", "windows", "pyqt", "importerror" ]
PyQt 4.7 - ImportError after installing on Windows
2,140,836
<p>I've been trying to install <strong>PyQt 4.7</strong> on Vista, but I am getting an ImportError when I try to do: <code>from PyQt4 import QtCore, QtGui</code>. </p> <blockquote> <p>ImportError: DLL load failed: The specified module could not be found.</p> </blockquote> <p>I've checked my System Path, and <strong...
12
2010-01-26T16:33:36Z
10,279,385
<p>PyQt installation also depends on the version of python installed on your platform.Python3.+ is incompatible with Python version &lt; 3.x.</p> <p>I was facing the same problem as I have Python 2.7 installed on my machine but I downloaded the latest binary which was PyQt-Py3.2-x86-gpl-4.9.exe. If you see here the bi...
1
2012-04-23T11:09:05Z
[ "python", "windows", "pyqt", "importerror" ]
PyQt 4.7 - ImportError after installing on Windows
2,140,836
<p>I've been trying to install <strong>PyQt 4.7</strong> on Vista, but I am getting an ImportError when I try to do: <code>from PyQt4 import QtCore, QtGui</code>. </p> <blockquote> <p>ImportError: DLL load failed: The specified module could not be found.</p> </blockquote> <p>I've checked my System Path, and <strong...
12
2010-01-26T16:33:36Z
11,774,820
<p>I had the same problem. I got my program running from within Eclipse but when I tried running it directly from the command line I still got the same error.</p> <p>I solved it by renaming the <strong>C:\Users\Me\AppData\Roaming\Python\Python27\site-packages\PyQt4</strong> directory. (I'm guessing leftovers from a pr...
0
2012-08-02T09:39:14Z
[ "python", "windows", "pyqt", "importerror" ]
PyQt 4.7 - ImportError after installing on Windows
2,140,836
<p>I've been trying to install <strong>PyQt 4.7</strong> on Vista, but I am getting an ImportError when I try to do: <code>from PyQt4 import QtCore, QtGui</code>. </p> <blockquote> <p>ImportError: DLL load failed: The specified module could not be found.</p> </blockquote> <p>I've checked my System Path, and <strong...
12
2010-01-26T16:33:36Z
27,086,381
<p>I think there are at least two possible error conditions</p> <p><strong>1. ImportError: DLL load failed: The specified module could not be found.</strong></p> <p>then you have to check your PyQT version is comptible with your python. In other words, if you use python 3.3, then you can only use PyQT for python3.3 a...
0
2014-11-23T06:53:16Z
[ "python", "windows", "pyqt", "importerror" ]
PyQt 4.7 - ImportError after installing on Windows
2,140,836
<p>I've been trying to install <strong>PyQt 4.7</strong> on Vista, but I am getting an ImportError when I try to do: <code>from PyQt4 import QtCore, QtGui</code>. </p> <blockquote> <p>ImportError: DLL load failed: The specified module could not be found.</p> </blockquote> <p>I've checked my System Path, and <strong...
12
2010-01-26T16:33:36Z
39,492,751
<p>Here's how I could fix those errors on Windows 7:</p> <ol> <li>Get the PyQt4 wheel from <a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4" rel="nofollow">http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4</a>. Make sure that the version is exactly the same as your Python version (i.e. Python 3.3 doesn't work ...
0
2016-09-14T14:16:46Z
[ "python", "windows", "pyqt", "importerror" ]
using the symbol font for Greek symbols in TeX via matplotlib
2,140,865
<p>To annotate my figures with Greek letters in the matplotlib package of Python, I use the following:</p> <pre><code>import matplotlib matplotlib.use('PDF') import matplotlib.pyplot as plt from matplotlib import rc rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) plt.rcParams['ps.useafm'] = True rc('fo...
2
2010-01-26T16:37:42Z
2,140,991
<p>Funny you should ask this; I've been struggling with similar problems not too long ago. Considering how complicated font handling is in TeX, I'd sidestep TeX altogether.</p> <p>However, any decent Helvetica has the Greek letters built-in, so you don't need to use the Symbol font. Just put some Unicode code points i...
5
2010-01-26T16:54:33Z
[ "python", "latex", "matplotlib" ]
RabbitMQ / Celery with Django hangs on delay/ready/etc - No useful log info
2,141,083
<p>So I just setup celery and rabbitmq, created my user, setup the vhost, mapped the user to the vhost, and ran the celery daemon succesfully (or so I assume)</p> <pre><code>(queuetest)corky@corky-server:~/projects/queuetest$ ./manage.py celeryd celery@corky-server v0.9.5 is starting. Configuration -&gt; . broker -&...
5
2010-01-26T17:08:36Z
2,146,131
<p>I just fixed a really ugly bug that would only show up for new users that would have caused this. (http://github.com/ask/celery/commit/a9c1316b15055b67ee3c38d294461fa82ed6d2b5)</p> <p>Please pull from the master branch at github. If it still doesn't work you probably have to stop rabbitmq, remove the rabbitmq data...
4
2010-01-27T10:41:32Z
[ "python", "django", "rabbitmq", "celery" ]
RabbitMQ / Celery with Django hangs on delay/ready/etc - No useful log info
2,141,083
<p>So I just setup celery and rabbitmq, created my user, setup the vhost, mapped the user to the vhost, and ran the celery daemon succesfully (or so I assume)</p> <pre><code>(queuetest)corky@corky-server:~/projects/queuetest$ ./manage.py celeryd celery@corky-server v0.9.5 is starting. Configuration -&gt; . broker -&...
5
2010-01-26T17:08:36Z
4,268,685
<p>For anyone stumbling upon this: it really does seem to help to remove your /var/lib/rabbitmq, even if the problem seems to go away with updating celery. I was seeing lots of unreliability and unpredictability until I did so. </p>
2
2010-11-24T15:50:51Z
[ "python", "django", "rabbitmq", "celery" ]
List Comprehension: why is this a syntax error?
2,141,098
<p>Why is <code>print(x)</code> here not valid (<code>SyntaxError</code>) in the following list-comprehension?</p> <pre><code>my_list=[1,2,3] [print(my_item) for my_item in my_list] </code></pre> <p>To contrast - the following doesn't give a syntax error:</p> <pre><code>def my_func(x): print(x) [my_func(my_item)...
31
2010-01-26T17:10:11Z
2,141,116
<p>Because print is not a function, it's a statement, and you can't have them in expressions. This gets more obvious if you use normal Python 2 syntax:</p> <pre><code>my_list=[1,2,3] [print my_item for my_item in my_list] </code></pre> <p>That doesn't look quite right. :) The parenthesizes around my_item tricks you. ...
41
2010-01-26T17:12:17Z
[ "python", "list-comprehension", "python-2.6" ]
List Comprehension: why is this a syntax error?
2,141,098
<p>Why is <code>print(x)</code> here not valid (<code>SyntaxError</code>) in the following list-comprehension?</p> <pre><code>my_list=[1,2,3] [print(my_item) for my_item in my_list] </code></pre> <p>To contrast - the following doesn't give a syntax error:</p> <pre><code>def my_func(x): print(x) [my_func(my_item)...
31
2010-01-26T17:10:11Z
2,141,120
<p>It's a syntax error because <code>print</code> is not a function. It's a statement. Since you obviously don't care about the return value from <code>print</code> (since it has none), just write the normal loop:</p> <pre><code>for my_item in my_list: print my_item </code></pre>
4
2010-01-26T17:12:28Z
[ "python", "list-comprehension", "python-2.6" ]
List Comprehension: why is this a syntax error?
2,141,098
<p>Why is <code>print(x)</code> here not valid (<code>SyntaxError</code>) in the following list-comprehension?</p> <pre><code>my_list=[1,2,3] [print(my_item) for my_item in my_list] </code></pre> <p>To contrast - the following doesn't give a syntax error:</p> <pre><code>def my_func(x): print(x) [my_func(my_item)...
31
2010-01-26T17:10:11Z
26,571,146
<p>list comprehension are designed to create a list. So using print inside it will give an error no-matter we use print() or print in 2.7 or 3.x. The code</p> <pre><code>[my_item for my_item in my_list] </code></pre> <p>makes a new object of type list.</p> <pre><code>print [my_item for my_item in my_list] </code></...
2
2014-10-26T08:23:29Z
[ "python", "list-comprehension", "python-2.6" ]
pygame saving audio files
2,141,315
<p>How can I convert a .wav file to some other format such as .mp3 in pygame? </p> <p><strong>Update:</strong> Why not Gstreamer or Pygame: I want to use native Windows environment to install a package that can do this (i.e. don't want to install cygwin). I am searching for a package which has a binary installer avai...
3
2010-01-26T17:39:00Z
2,141,369
<p>Pygame is an SDL wrapper. Not a multimedia framework. Why do you want to do audio format conversions in Pygame? Can't you use something else like maybe the gstreamer bindings for Python?</p>
-2
2010-01-26T17:44:10Z
[ "python", "pygame" ]
pygame saving audio files
2,141,315
<p>How can I convert a .wav file to some other format such as .mp3 in pygame? </p> <p><strong>Update:</strong> Why not Gstreamer or Pygame: I want to use native Windows environment to install a package that can do this (i.e. don't want to install cygwin). I am searching for a package which has a binary installer avai...
3
2010-01-26T17:39:00Z
2,166,895
<p>The answer is you can not do this using PyGame. </p> <p>I found out GStreamer installer for windows from GStreamer-Winbuild project: <a href="http://www.gstreamer-winbuild.ylatuya.es/doku.php" rel="nofollow">http://www.gstreamer-winbuild.ylatuya.es/doku.php</a> So, I will be using Gstreamer framework for audio hand...
1
2010-01-30T04:46:41Z
[ "python", "pygame" ]
Addressing part of a string via the array syntax in Python
2,141,334
<p>Is it, in Python, possible to address a specific character in a string by the standard array syntax?</p> <p>Example, PHP:</p> <pre><code>$foo = 'bar'; echo $foo[1]; // Output: a </code></pre> <p>It didn't work like in PHP so I wanted to know if it is possible using some other way?</p>
3
2010-01-26T17:40:39Z
2,141,392
<p>As Adam pointed out, reading from a string array is possible in Python using the indexing syntax. What isn't possible though is writing to a string using this syntax:</p> <pre><code>&gt;&gt;&gt; s = 'bar' &gt;&gt;&gt; s[2] = 'z' Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; T...
5
2010-01-26T17:46:31Z
[ "python", "string" ]
Addressing part of a string via the array syntax in Python
2,141,334
<p>Is it, in Python, possible to address a specific character in a string by the standard array syntax?</p> <p>Example, PHP:</p> <pre><code>$foo = 'bar'; echo $foo[1]; // Output: a </code></pre> <p>It didn't work like in PHP so I wanted to know if it is possible using some other way?</p>
3
2010-01-26T17:40:39Z
2,141,394
<p>Yes, this works. In fact, it works exactly like it does in PHP. The only difference is that variable names in Python are not preceeded by the <code>$</code> character.</p> <pre><code>foo = 'bar' print foo[1] # Output: a </code></pre> <p>Is Python, strings are one of a number of sequence types. You can find all of ...
1
2010-01-26T17:46:39Z
[ "python", "string" ]
Convert datetime fields in Chrome history file (sqlite) to readable format
2,141,537
<p>Working on a script to collect users browser history with time stamps ( educational setting). Firefox 3 history is kept in a sqlite file, and stamps are in UNIX epoch time... getting them and converting to readable format via a SQL command in python is pretty straightforward:</p> <pre><code>sql_select = """ SELECT ...
9
2010-01-26T18:08:46Z
2,141,621
<p>The <code>sqlite</code> module returns <code>datetime</code> objects for datetime fields, which have a format method for printing readable strings called <code>strftime</code>. </p> <p>You can do something like this once you have the recordset:</p> <pre><code>for record in get_hist: date_string = record[0].strf...
1
2010-01-26T18:25:12Z
[ "python", "datetime", "sqlite3" ]
Convert datetime fields in Chrome history file (sqlite) to readable format
2,141,537
<p>Working on a script to collect users browser history with time stamps ( educational setting). Firefox 3 history is kept in a sqlite file, and stamps are in UNIX epoch time... getting them and converting to readable format via a SQL command in python is pretty straightforward:</p> <pre><code>sql_select = """ SELECT ...
9
2010-01-26T18:08:46Z
2,379,412
<p>This may not be the most Pythonic code in the world, but here's a solution: Cheated by adjusting for time zone (EST here) by doing this: </p> <pre><code>utctime = datetime.datetime(1601,1,1) + datetime.timedelta(microseconds = ms, hours =-5) </code></pre> <p>Here's the function : It assumes that the Chrome history...
1
2010-03-04T13:04:25Z
[ "python", "datetime", "sqlite3" ]
Convert datetime fields in Chrome history file (sqlite) to readable format
2,141,537
<p>Working on a script to collect users browser history with time stamps ( educational setting). Firefox 3 history is kept in a sqlite file, and stamps are in UNIX epoch time... getting them and converting to readable format via a SQL command in python is pretty straightforward:</p> <pre><code>sql_select = """ SELECT ...
9
2010-01-26T18:08:46Z
3,174,614
<p>This is a more pythonic and memory-friendly way to do what you described (by the way, thanks for the initial code!):</p> <pre><code>#!/usr/bin/env python import os import datetime import sqlite3 import opster from itertools import izip SQL_TIME = 'SELECT time FROM info' SQL_URL = 'SELECT c0url FROM pages_content...
4
2010-07-04T12:18:33Z
[ "python", "datetime", "sqlite3" ]