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 pass information using an HTTP redirect (in Django)
599,280
<p>I have a view that accepts a form submission and updates a model.</p> <p>After updating the model, I want to redirect to another page, and I want a message such as "Field X successfully updated" to appear on this page.</p> <p>How can I "pass" this message to the other page? HttpResponseRedirect only accepts a <a h...
19
2009-03-01T05:02:13Z
2,304,004
<p>You could also have the redirect url be the path to an already parameterized view.</p> <p>urls.py:</p> <pre><code>(r'^some/path/(?P&lt;field_name&gt;\w+)/$', direct_to_template, {'template': 'field_updated_message.html', }, 'url-name' ), </code></pre> <p>views.py:</p> <pre><code>HttpResponseRedirect(...
0
2010-02-20T22:30:21Z
[ "python", "django", "http" ]
How to pass information using an HTTP redirect (in Django)
599,280
<p>I have a view that accepts a form submission and updates a model.</p> <p>After updating the model, I want to redirect to another page, and I want a message such as "Field X successfully updated" to appear on this page.</p> <p>How can I "pass" this message to the other page? HttpResponseRedirect only accepts a <a h...
19
2009-03-01T05:02:13Z
4,642,620
<p>I liked the idea of using the message framework, but the example in the django documentation doesn't work for me in the context of the question above. </p> <p>What really annoys me, is the line in the django docs:</p> <p><code>If you're using the context processor, your template should be rendered with a RequestCo...
4
2011-01-09T23:46:57Z
[ "python", "django", "http" ]
How to pass information using an HTTP redirect (in Django)
599,280
<p>I have a view that accepts a form submission and updates a model.</p> <p>After updating the model, I want to redirect to another page, and I want a message such as "Field X successfully updated" to appear on this page.</p> <p>How can I "pass" this message to the other page? HttpResponseRedirect only accepts a <a h...
19
2009-03-01T05:02:13Z
5,131,666
<p>The solution used by Pydev UA is the less intrusive and can be used without modifying almost nothing in your code. When you pass the message, you can update your context in the view that handles the message and in your template you can show it.</p> <p>I used the same approach, but instead passing a simple text, pas...
0
2011-02-27T05:42:47Z
[ "python", "django", "http" ]
How to pass information using an HTTP redirect (in Django)
599,280
<p>I have a view that accepts a form submission and updates a model.</p> <p>After updating the model, I want to redirect to another page, and I want a message such as "Field X successfully updated" to appear on this page.</p> <p>How can I "pass" this message to the other page? HttpResponseRedirect only accepts a <a h...
19
2009-03-01T05:02:13Z
5,402,113
<p>I think this code should work for you</p> <pre><code>request.user.message_set.create(message="This is some message") return http.HttpResponseRedirect('/url') </code></pre>
1
2011-03-23T07:37:42Z
[ "python", "django", "http" ]
How to pass information using an HTTP redirect (in Django)
599,280
<p>I have a view that accepts a form submission and updates a model.</p> <p>After updating the model, I want to redirect to another page, and I want a message such as "Field X successfully updated" to appear on this page.</p> <p>How can I "pass" this message to the other page? HttpResponseRedirect only accepts a <a h...
19
2009-03-01T05:02:13Z
8,704,580
<p>I have read and checked all answers, and it seems to me that the way to go now is using <a href="https://docs.djangoproject.com/en/1.3/ref/contrib/messages/" rel="nofollow">the messaging framework</a>. Some of the replies are fairly old and have probably been the right way at the time of the posting.</p>
4
2012-01-02T19:26:34Z
[ "python", "django", "http" ]
Why doesn't the weakref work on this bound method?
599,430
<p>I have a project where i'm trying to use weakrefs with callbacks, and I don't understand what I'm doing wrong. I have created simplified test that shows the exact behavior i'm confused with. </p> <p>Why is it that in this test test_a works as expected, but the weakref for self.MyCallbackB disappears between the cla...
8
2009-03-01T07:23:20Z
599,486
<p>You want a <a href="http://code.activestate.com/recipes/81253/">WeakMethod</a>. </p> <p>An explanation why your solution doesn't work can be found in the discussion of the recipe:</p> <blockquote> <p>Normal weakref.refs to bound methods don't quite work the way one expects, because bound methods are first-class ...
9
2009-03-01T08:16:06Z
[ "python", "weak-references" ]
Why doesn't the weakref work on this bound method?
599,430
<p>I have a project where i'm trying to use weakrefs with callbacks, and I don't understand what I'm doing wrong. I have created simplified test that shows the exact behavior i'm confused with. </p> <p>Why is it that in this test test_a works as expected, but the weakref for self.MyCallbackB disappears between the cla...
8
2009-03-01T07:23:20Z
599,492
<p>According to the documentation for the Weakref module:</p> <blockquote> <p>In the following, the term referent means the object which is referred to by a weak reference.</p> <p>A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are w...
4
2009-03-01T08:27:33Z
[ "python", "weak-references" ]
Why doesn't the weakref work on this bound method?
599,430
<p>I have a project where i'm trying to use weakrefs with callbacks, and I don't understand what I'm doing wrong. I have created simplified test that shows the exact behavior i'm confused with. </p> <p>Why is it that in this test test_a works as expected, but the weakref for self.MyCallbackB disappears between the cla...
8
2009-03-01T07:23:20Z
15,468,266
<p>The other answers address the <em>why</em> in the original question, but either don't provide a workaround or refer to external sites.</p> <p>After working through several other posts on StackExchange on this topic, many of which are marked as duplicates of this question, I finally came to a succinct workaround. Wh...
0
2013-03-18T01:05:09Z
[ "python", "weak-references" ]
How do I tell a file from directory in Python?
599,474
<p>When using os.listdir method I need to tell which item in the resulting list is a directory or just a file.</p> <p>I've faced a problem when I had to go through all the directories in this list, and then add a file in every single directory.</p> <p>Is there a way to go through this list and remove all files from ...
7
2009-03-01T08:11:13Z
599,481
<p>Use <code>os.path.isdir</code> to filter out the directories. Possibly something like</p> <pre><code>dirs = filter(os.path.isdir, os.listdir('/path')) for dir in dirs: # add your file </code></pre>
14
2009-03-01T08:14:03Z
[ "python", "file", "directory" ]
How do I tell a file from directory in Python?
599,474
<p>When using os.listdir method I need to tell which item in the resulting list is a directory or just a file.</p> <p>I've faced a problem when I had to go through all the directories in this list, and then add a file in every single directory.</p> <p>Is there a way to go through this list and remove all files from ...
7
2009-03-01T08:11:13Z
599,568
<blockquote> <p>dirs = filter(os.path.isdir, os.listdir('/path'))</p> </blockquote> <p>Note this won't work unless '/path' is the current working directory. os.listdir() returns leafnames, so you'll be asking “os.path.isdir('file.txt')”, and if the current directory is elsewhere you'll be looking at the wrong 'f...
2
2009-03-01T09:50:02Z
[ "python", "file", "directory" ]
How do I tell a file from directory in Python?
599,474
<p>When using os.listdir method I need to tell which item in the resulting list is a directory or just a file.</p> <p>I've faced a problem when I had to go through all the directories in this list, and then add a file in every single directory.</p> <p>Is there a way to go through this list and remove all files from ...
7
2009-03-01T08:11:13Z
599,650
<p>This might be faster:</p> <pre><code>current, dirs, files = os.walk('/path').next() </code></pre> <p>The list of directories will be in the <code>dirs</code> variable.</p>
7
2009-03-01T11:18:53Z
[ "python", "file", "directory" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
599,635
<p>Do you really mean <code>u'String'</code>? </p> <p>In any event, can't you just do <code>str(string)</code> to get a string rather than a unicode-string? (This should be different for Python 3, for which all strings are unicode.)</p>
0
2009-03-01T11:01:20Z
[ "python", "unicode", "ascii" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
599,644
<p>Use <code>dir</code> or <code>type</code> on the 'string' to find out what it is. I suspect that it's one of BeautifulSoup's tag objects, that prints like a string, but really isn't one. Otherwise, its inside a list and you need to convert each string separately.</p> <p>In any case, why are you objecting to using U...
0
2009-03-01T11:14:19Z
[ "python", "unicode", "ascii" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
599,653
<p><code>[u'ABC']</code> would be a one-element list of unicode strings. <a href="http://www.crummy.com/software/BeautifulSoup/documentation.html#Beautiful%20Soup%20Gives%20You%20Unicode,%20Dammit">Beautiful Soup always produces Unicode</a>. So you need to convert the list to a single unicode string, and then convert t...
53
2009-03-01T11:22:11Z
[ "python", "unicode", "ascii" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
599,681
<p>You probably have a list containing one unicode string. The <code>repr</code> of this is <code>[u'String']</code>.</p> <p>You can convert this to a list of byte strings using any variation of the following:</p> <pre><code># Functional style. print map(lambda x: x.encode('ascii'), my_list) # List comprehension. pr...
13
2009-03-01T11:40:24Z
[ "python", "unicode", "ascii" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
14,785,555
<p>If accessing/printing single element lists (e.g., sequentially or filtered):</p> <pre><code>my_list = [u'String'] # sample element my_list = [str(my_list[0])] </code></pre>
4
2013-02-09T06:21:39Z
[ "python", "unicode", "ascii" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
16,262,256
<p>pass the output to str() function and it will remove the convert the unicode output. also by printing the output it will remove the u'' tags from it. </p>
1
2013-04-28T11:14:18Z
[ "python", "unicode", "ascii" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
29,584,254
<p><code>encode("latin-1")</code> helped me in my case:</p> <pre><code>facultyname[0].encode("latin-1") </code></pre>
-1
2015-04-11T23:30:43Z
[ "python", "unicode", "ascii" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
36,891,685
<p><code>[u'String']</code> is a text representation of a list that contains a Unicode string on Python 2.</p> <p>If you run <code>print(some_list)</code> then it is equivalent to<br> <code>print'[%s]' % ', '.join(map(repr, some_list))</code> i.e., to create a text representation of a Python object with the type <code...
0
2016-04-27T13:45:09Z
[ "python", "unicode", "ascii" ]
Python string prints as [u'String']
599,625
<p>This will surely be an easy one but it is really bugging me. </p> <p>I have a script that reads in a webpage and uses <a href="https://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a> to parse it. From the <em>soup</em> I extract all the links as my final goal is to print out the link.contents.</p> <p>Al...
59
2009-03-01T10:48:45Z
40,090,338
<pre><code>import json, ast r = {u'name': u'A', u'primary_key': 1} ast.literal_eval(json.dumps(r)) </code></pre> <p>will print </p> <pre><code>{'name': 'A', 'primary_key': 1} </code></pre>
0
2016-10-17T15:30:50Z
[ "python", "unicode", "ascii" ]
PyS60: Bluetooth sockets
599,737
<p>From the website <a href="http://www.mobilepythonbook.org/" rel="nofollow">http://www.mobilepythonbook.org/</a> I found the following example of bluetooth sockets: <a href="http://www.mobilenin.com/mobilepythonbook/examples/057-btchat.html" rel="nofollow">BT chat example</a></p> <p>Here in function chat_server() th...
0
2009-03-01T12:22:41Z
668,345
<p>For IPv4 addresses, two special forms are accepted instead of a host address: the empty string represents INADDR_ANY, and the string '' represents INADDR_BROADCAST -- <a href="http://docs.python.org/library/socket.html" rel="nofollow">http://docs.python.org/library/socket.html</a></p> <p>There you'll find more than...
1
2009-03-20T23:24:33Z
[ "python", "bluetooth", "nokia", "pys60" ]
PyS60: Bluetooth sockets
599,737
<p>From the website <a href="http://www.mobilepythonbook.org/" rel="nofollow">http://www.mobilepythonbook.org/</a> I found the following example of bluetooth sockets: <a href="http://www.mobilenin.com/mobilepythonbook/examples/057-btchat.html" rel="nofollow">BT chat example</a></p> <p>Here in function chat_server() th...
0
2009-03-01T12:22:41Z
669,159
<p>I found the answer the device that starts listening for incoming connections is slave and the one that discovers and requests for connection is the master.</p> <p>Sorry michelpeterson your answer is totally wrong.</p>
0
2009-03-21T11:39:32Z
[ "python", "bluetooth", "nokia", "pys60" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
599,959
<p>The simplest way I can think of is with slicing- </p> <pre><code>def findPath( i_file): lines = open( i_file ).readlines() for line in lines: if line.startswith( "Path=" ) : return line[5:] </code></pre> <p>A quick note on slice notation, it uses two indices instead of the usual on...
3
2009-03-01T15:25:16Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
599,960
<pre><code>line[5:] </code></pre> <p>gives you characters after the first five.</p>
2
2009-03-01T15:25:58Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
599,961
<p><code>line[5:]</code> will give the substring you want. Search the <a href="http://docs.python.org/tutorial/introduction.html" rel="nofollow">introduction</a> and look for 'slice notation'</p>
2
2009-03-01T15:26:31Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
599,962
<p>If the string is fixed you can simply use:</p> <pre><code>if line.startswith("Path="): return line[5:] </code></pre> <p>which gives you everything from position 5 on in the string (a string is also a sequence so these sequence operators work here, too).</p> <p>Or you can split the line at the first <code>=</c...
97
2009-03-01T15:26:56Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
599,979
<p>For slicing (conditional or non-conditional) in general I prefer what a colleague suggested recently; Use replacement with an empty string. Easier to read the code, less code (sometimes) and less risk of specifying the wrong number of characters. Ok; I do not use Python, but in other languages I do prefer this appro...
15
2009-03-01T15:43:50Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
600,002
<pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; p = re.compile(r'path=(.*)', re.IGNORECASE) &gt;&gt;&gt; path = "path=c:\path" &gt;&gt;&gt; re.match(p, path).group(1) 'c:\\path' </code></pre>
3
2009-03-01T15:57:15Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
600,195
<h3>Remove prefix from a string</h3> <pre><code># ... if line.startswith(prefix): return line[len(prefix):] </code></pre> <h3>Split on the first occurrence of the separator via <code>str.partition()</code></h3> <pre><code>def findvar(filename, varname="Path", sep="=") : for line in open(filename): if ...
80
2009-03-01T18:03:24Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
600,644
<p>If you know list comprehensions:</p> <pre><code>lines = [line[5:] for line in file.readlines() if line[:5] == "Path="] </code></pre>
0
2009-03-01T22:11:56Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
5,293,388
<p>I prefer the looks of pop to indexing:</p> <pre><code>value = line.split("Path=").pop() </code></pre> <p>to</p> <pre><code>value = line.split("Path=")[1] param, value = line.split("Path=") </code></pre>
7
2011-03-13T23:58:33Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
7,440,332
<pre><code>def removePrefix(text, prefix): return text[len(prefix):] if text.startswith(prefix) else text </code></pre> <p>Couldn't resist doing this in one line. Requires Python 2.5+.</p>
7
2011-09-16T04:59:30Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
19,083,836
<p>Or why not</p> <pre><code>if line.startswith(prefix): return line.replace(prefix, '', 1) </code></pre>
4
2013-09-29T22:03:51Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
19,929,571
<p>How about..</p> <pre><code>line = 'path=c:\path' head, sep, tail = line.partition('path=') &gt;&gt;&gt; print 'head = ', head head = print 'sep = ',sep sep = path= print 'tail = ',tail tail = c:\path &gt;&gt;&gt; </code></pre>
2
2013-11-12T12:45:27Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
20,348,535
<p>I guess this what you are exactly looking for</p> <pre><code> def findPath(i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ): output_line=line[(line.find("Path=")+len("Path=")):] return output_line </code></p...
0
2013-12-03T10:27:15Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
27,855,275
<p>The pop version isn't quite right. I think you want:</p> <pre><code>&gt;&gt;&gt; print "foofoobar".split("foo",1).pop() foobar </code></pre>
0
2015-01-09T06:50:12Z
[ "python", "string" ]
How to remove the left part of a string?
599,953
<p>I have some simple python code that searches files for a string e.g. <code>path=c:\path</code>, whereby the <code>c:\path</code> may vary. The current code is:</p> <pre><code>def findPath( i_file) : lines = open( i_file ).readlines() for line in lines : if line.startswith( "Path=" ) : return # what to...
63
2009-03-01T15:19:38Z
35,135,905
<p>Another simple one-liner that hasn't been mentioned here:</p> <pre><code>value = line.split("Path=", 1)[-1] </code></pre> <p>This will also work properly for various edge cases:</p> <pre><code>&gt;&gt;&gt; print ("prefixfoobar".split("foo", 1)[-1]) "bar" &gt;&gt;&gt; print ("foofoobar".split("foo", 1)[-1]) "foob...
0
2016-02-01T16:51:40Z
[ "python", "string" ]
Python: Choosing between modules and classes
600,190
<p>In my application I have to maintain some global application state and global application wide methods like currently connected users, total number of answers, create an application config file etc. There are two options:</p> <ol> <li><p>Make a separate appstate.py file with global variables with functions over the...
8
2009-03-01T18:00:39Z
600,199
<p>The second approach seems better. I'd use the first one only for configuration files or something.</p> <p>Anyway, to avoid the problem you could always:</p> <pre><code>from myapp.appstate import AppState </code></pre> <p>That way you don't have to write the long line anymore.</p>
3
2009-03-01T18:05:34Z
[ "python", "design" ]
Python: Choosing between modules and classes
600,190
<p>In my application I have to maintain some global application state and global application wide methods like currently connected users, total number of answers, create an application config file etc. There are two options:</p> <ol> <li><p>Make a separate appstate.py file with global variables with functions over the...
8
2009-03-01T18:00:39Z
600,200
<p>I would go with the classes route as it will better organize your code. Remember that for readability you can do this:</p> <pre><code>from appstate import AppSate </code></pre>
0
2009-03-01T18:05:58Z
[ "python", "design" ]
Python: Choosing between modules and classes
600,190
<p>In my application I have to maintain some global application state and global application wide methods like currently connected users, total number of answers, create an application config file etc. There are two options:</p> <ol> <li><p>Make a separate appstate.py file with global variables with functions over the...
8
2009-03-01T18:00:39Z
600,201
<p>Sounds like the classic conundrum :-) In Python, there's nothing dirty or shameful about choosing to use a module if that's the best approach. After all, modules, functions, and the like are, in fact, first-class citizens in the language, and offer introspection and other properties that many other languages use ob...
13
2009-03-01T18:06:47Z
[ "python", "design" ]
Python: Choosing between modules and classes
600,190
<p>In my application I have to maintain some global application state and global application wide methods like currently connected users, total number of answers, create an application config file etc. There are two options:</p> <ol> <li><p>Make a separate appstate.py file with global variables with functions over the...
8
2009-03-01T18:00:39Z
600,206
<p>I'd definitely go for the second option : having already used the first one, I'm now forced to refactor, as my application evolved and have to support more modular constructs, so I now need to handle multiple simulataneous 'configurations'.</p> <p>The second approach is, IMO, more flexible and future proof. To avoi...
0
2009-03-01T18:08:45Z
[ "python", "design" ]
Python: Choosing between modules and classes
600,190
<p>In my application I have to maintain some global application state and global application wide methods like currently connected users, total number of answers, create an application config file etc. There are two options:</p> <ol> <li><p>Make a separate appstate.py file with global variables with functions over the...
8
2009-03-01T18:00:39Z
600,219
<p>The second approach is only significantly different from the first approach if you have application state stored in an <em>instance</em> of AppState, in which case your complaint doesn't apply. If you're just storing stuff in a class and using static/class methods, your class is no different than a module, and it wo...
4
2009-03-01T18:19:17Z
[ "python", "design" ]
Python: Choosing between modules and classes
600,190
<p>In my application I have to maintain some global application state and global application wide methods like currently connected users, total number of answers, create an application config file etc. There are two options:</p> <ol> <li><p>Make a separate appstate.py file with global variables with functions over the...
8
2009-03-01T18:00:39Z
600,325
<p>Why not go with an instance of that class? That way you might even be able later on to have 2 different "sessions" running, depending on what instance you use. It might make it more flexible. Maybe add some method <code>get_appstate()</code> to the module so it instanciates the class once. Later on if you might want...
0
2009-03-01T19:17:16Z
[ "python", "design" ]
Python: Choosing between modules and classes
600,190
<p>In my application I have to maintain some global application state and global application wide methods like currently connected users, total number of answers, create an application config file etc. There are two options:</p> <ol> <li><p>Make a separate appstate.py file with global variables with functions over the...
8
2009-03-01T18:00:39Z
600,381
<p>Consider this example:</p> <pre><code>configuration | +-&gt; graphics | | | +-&gt; 3D | | | +-&gt; 2D | +-&gt; sound </code></pre> <p>The real question is: What is the difference between classes and modules in this hierarchy, as it could be represented by both means?</p> <p>Classes represent types. If yo...
1
2009-03-01T19:47:52Z
[ "python", "design" ]
How to make Satchmo work in Google App Engine
600,225
<p>I understand that there are big differences in data-store, but surely since django is bundled and it abstracts data-store away from Satchmo, something can be done?</p> <p>Truth is that I am not a Python guy, been mostly Java/PHP thus far, but I am willing to learn.</p> <p>Plus, if this is not possible today, lets ...
3
2009-03-01T18:22:33Z
600,231
<p>You can't. There are alot of dependencies in Satchmo that you aren't allowed to install on AppEngine.</p> <p>See this thread as well: <a href="http://groups.google.com/group/satchmo-users/browse_thread/thread/509265ccd5f5fc1e?pli=1" rel="nofollow">http://groups.google.com/group/satchmo-users/browse_thread/thread/50...
3
2009-03-01T18:25:18Z
[ "python", "google-app-engine", "e-commerce", "satchmo" ]
How to make Satchmo work in Google App Engine
600,225
<p>I understand that there are big differences in data-store, but surely since django is bundled and it abstracts data-store away from Satchmo, something can be done?</p> <p>Truth is that I am not a Python guy, been mostly Java/PHP thus far, but I am willing to learn.</p> <p>Plus, if this is not possible today, lets ...
3
2009-03-01T18:22:33Z
833,823
<p>Nothing is impossible - this will just require lots of effort - if there will be somebody wishing to do so - why not? But it might be easier (cheaper) to get Django friendly hosting instead of spending hours on hacking the code.</p>
2
2009-05-07T10:11:27Z
[ "python", "google-app-engine", "e-commerce", "satchmo" ]
How to make Satchmo work in Google App Engine
600,225
<p>I understand that there are big differences in data-store, but surely since django is bundled and it abstracts data-store away from Satchmo, something can be done?</p> <p>Truth is that I am not a Python guy, been mostly Java/PHP thus far, but I am willing to learn.</p> <p>Plus, if this is not possible today, lets ...
3
2009-03-01T18:22:33Z
835,520
<p>Possible if:</p> <ul> <li><p>Someone writes a generic ORM to Bigtable mapper. Most probably, <a href="http://code.google.com/appengine/articles/app-engine-patch.html" rel="nofollow">Appengine Patch Guys</a></p></li> <li><p>Someone rewrites the views and models of Satchmo to remove existing ORM queries and use the m...
2
2009-05-07T15:57:57Z
[ "python", "google-app-engine", "e-commerce", "satchmo" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
600,606
<p>I think Asa's answer is essentially correct, but you could extend it a little to act more like <code>mkdir -p</code>, either:</p> <pre><code>import os def mkdir_path(path): if not os.access(path, os.F_OK): os.mkdirs(path) </code></pre> <p>or</p> <pre><code>import os import errno def mkdir_path(path)...
5
2009-03-01T21:47:58Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
600,612
<p><code>mkdir -p</code> functionality as follows:</p> <pre><code>import errno import os def mkdir_p(path): try: os.makedirs(path) except OSError as exc: # Python &gt;2.5 if exc.errno == errno.EEXIST and os.path.isdir(path): pass else: raise </code></pre> ...
635
2009-03-01T21:51:46Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
600,879
<p><code>mkdir -p</code> gives you an error if you the file already exists:</p> <pre><code>$ touch /tmp/foo $ mkdir -p /tmp/foo mkdir: cannot create directory `/tmp/foo': File exists </code></pre> <p>So a refinement to the previous suggestions would be to re-<code>raise</code> the exception if <code>os.path.isdir</co...
11
2009-03-02T00:09:10Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
600,940
<p>This is easier than trapping the exception:</p> <pre><code>import os if not os.path.exists(...): os.makedirs(...) </code></pre> <p><strong>Disclaimer</strong> This approach requires two system calls which is more susceptible to race conditions under certain environments/conditions. If you're writing something...
113
2009-03-02T00:48:58Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
11,101,867
<p>In Python >=3.2, that's</p> <pre><code>os.makedirs(path, exist_ok=True) </code></pre> <p>In earlier versions, use <a href="http://stackoverflow.com/a/600612/11343">@tzot's answer</a>.</p>
95
2012-06-19T13:21:48Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
11,825,909
<pre><code>import os import tempfile path = tempfile.mktemp(dir=path) os.makedirs(path) os.rmdir(path) </code></pre>
1
2012-08-06T09:47:43Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
11,860,637
<p>As mentioned in the other solutions, we want to be able to hit the file system once while mimicking the behaviour of <code>mkdir -p</code>. I don't think that this is possible to do, but we should get as close as possible.</p> <p>Code first, explanation later:</p> <pre><code>import os import errno def mkdir_p(pa...
9
2012-08-08T08:31:24Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
14,347,436
<p>Recently, I found this <a href="http://docs.python.org/2/distutils/apiref.html#distutils.dir_util.mkpath">distutils.dir_util.mkpath</a>:</p> <pre><code>In [17]: from distutils.dir_util import mkpath In [18]: mkpath('./foo/bar') Out[18]: ['foo', 'foo/bar'] </code></pre>
30
2013-01-15T21:39:43Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
27,611,157
<p>Function declaration;</p> <pre><code>import os def mkdir_p(filename): try: folder=os.path.dirname(filename) if not os.path.exists(folder): os.makedirs(folder) return True except: return False </code></pre> <p>usage :</p> <pre><code>filename = "./download/80...
1
2014-12-22T22:06:56Z
[ "python", "mkdir" ]
mkdir -p functionality in Python
600,268
<p>Is there a way to get functionality similar to <code>mkdir -p</code> on the shell from within Python. I am looking for a solution other than a system call. I am sure the code is less than 20 lines, and I am wondering if someone has already written it?</p>
441
2009-03-01T18:49:39Z
35,275,153
<p>With <a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir" rel="nofollow">Pathlib</a> from python3 standard library:</p> <pre><code>Path(mypath).mkdir(parents=True, exist_ok=True) </code></pre> <blockquote> <p>If parents is true, any missing parents of this path are created as needed; the...
4
2016-02-08T17:02:36Z
[ "python", "mkdir" ]
Python error when using urllib.open
600,389
<p>When I run this:</p> <pre><code>import urllib feed = urllib.urlopen("http://www.yahoo.com") print feed </code></pre> <p>I get this output in the interactive window (PythonWin):</p> <pre><code>&lt;addinfourl at 48213968 whose fp = &lt;socket._fileobject object at 0x02E14070&gt;&gt; </code></pre> <p>I'm expectin...
21
2009-03-01T19:56:54Z
600,394
<p>Try this:</p> <p><code>print feed.read()</code></p> <p>See Python docs <a href="http://docs.python.org/library/urllib.html">here</a>.</p>
50
2009-03-01T20:00:08Z
[ "python", "urllib" ]
Python error when using urllib.open
600,389
<p>When I run this:</p> <pre><code>import urllib feed = urllib.urlopen("http://www.yahoo.com") print feed </code></pre> <p>I get this output in the interactive window (PythonWin):</p> <pre><code>&lt;addinfourl at 48213968 whose fp = &lt;socket._fileobject object at 0x02E14070&gt;&gt; </code></pre> <p>I'm expectin...
21
2009-03-01T19:56:54Z
600,396
<p>urllib.urlopen actually returns a file-like object so to retrieve the contents you will need to use:</p> <pre><code>import urllib feed = urllib.urlopen("http://www.yahoo.com") print feed.read() </code></pre>
15
2009-03-01T20:00:35Z
[ "python", "urllib" ]
Python error when using urllib.open
600,389
<p>When I run this:</p> <pre><code>import urllib feed = urllib.urlopen("http://www.yahoo.com") print feed </code></pre> <p>I get this output in the interactive window (PythonWin):</p> <pre><code>&lt;addinfourl at 48213968 whose fp = &lt;socket._fileobject object at 0x02E14070&gt;&gt; </code></pre> <p>I'm expectin...
21
2009-03-01T19:56:54Z
600,645
<p>In python 3.0:</p> <pre><code>import urllib import urllib.request fh = urllib.request.urlopen(url) html = fh.read().decode("iso-8859-1") fh.close() print (html) </code></pre>
6
2009-03-01T22:11:58Z
[ "python", "urllib" ]
What features would a *perfect* Python debugger have?
600,401
<p>Please tell me which features you wish your current Python debugger had. I'm creating a new Python IDE/debugger and am looking forward to challenging requests!</p>
3
2009-03-01T20:04:20Z
600,434
<p>The #1 debug feature for me (that my current IDE, Wing, does happen to have) is the ability to drop into a python interpreter and run arbitrary python code when at a breakpoint. Reminds me of using Smalltalk back in the day.</p> <p>Ability to execute code in local scope is incredibly useful, especially in contrast...
2
2009-03-01T20:22:05Z
[ "python", "ide", "debugging" ]
What features would a *perfect* Python debugger have?
600,401
<p>Please tell me which features you wish your current Python debugger had. I'm creating a new Python IDE/debugger and am looking forward to challenging requests!</p>
3
2009-03-01T20:04:20Z
600,436
<p>Forgive me for the shameless functional programming plug, but...</p> <p>The ability to <em>step backwards</em>.</p>
6
2009-03-01T20:23:28Z
[ "python", "ide", "debugging" ]
What features would a *perfect* Python debugger have?
600,401
<p>Please tell me which features you wish your current Python debugger had. I'm creating a new Python IDE/debugger and am looking forward to challenging requests!</p>
3
2009-03-01T20:04:20Z
600,470
<p>I would love a debugger that could somehow split into two when a thread was created, then I could watch both threads with independent call stacks, etc. </p> <p>If it exists, then I just haven't found it yet.</p>
3
2009-03-01T20:42:24Z
[ "python", "ide", "debugging" ]
What features would a *perfect* Python debugger have?
600,401
<p>Please tell me which features you wish your current Python debugger had. I'm creating a new Python IDE/debugger and am looking forward to challenging requests!</p>
3
2009-03-01T20:04:20Z
600,489
<p>I use <a href="http://winpdb.org/" rel="nofollow">winpdb</a> and I like it very much. I think a new debugger would need to have at least its features. It has some GUI nuisiances though, so maybe you fix it or take some ideas from it to write your own.</p> <p><a href="http://winpdb.org/" rel="nofollow">Winpdb</a> is...
7
2009-03-01T20:50:13Z
[ "python", "ide", "debugging" ]
What features would a *perfect* Python debugger have?
600,401
<p>Please tell me which features you wish your current Python debugger had. I'm creating a new Python IDE/debugger and am looking forward to challenging requests!</p>
3
2009-03-01T20:04:20Z
37,810,886
<p>Ability to step through expression evaluation and intuitive visualization of call stack like in Thonny (<a href="http://thonny.cs.ut.ee" rel="nofollow">http://thonny.cs.ut.ee</a>)</p>
0
2016-06-14T11:35:50Z
[ "python", "ide", "debugging" ]
Why does Google Search return HTTP Error 403?
600,536
<p>Consider the following Python code:</p> <pre> 30 url = "http://www.google.com/search?hl=en&safe=off&q=Monkey" 31 url_object = urllib.request.urlopen(url); 32 print(url_object.read()); </pre> <p>When this is run, an Exception is thrown:</p> <pre><code>File "/usr/local/lib/python3.0/urllib/request.py", ...
16
2009-03-01T21:16:25Z
600,547
<p>You're doing it too often. Google has limits in place to prevent getting swamped by search bots. You can also try setting the user-agent to something that more closely resembles a normal browser.</p>
0
2009-03-01T21:20:45Z
[ "python", "google-search" ]
Why does Google Search return HTTP Error 403?
600,536
<p>Consider the following Python code:</p> <pre> 30 url = "http://www.google.com/search?hl=en&safe=off&q=Monkey" 31 url_object = urllib.request.urlopen(url); 32 print(url_object.read()); </pre> <p>When this is run, an Exception is thrown:</p> <pre><code>File "/usr/local/lib/python3.0/urllib/request.py", ...
16
2009-03-01T21:16:25Z
600,551
<p>If you want to do Google searches "properly" through a programming interface, take a look at <a href="http://code.google.com/more/">Google APIs</a>. Not only are these the official way of searching Google, they are also not likely to change if Google changes their result page layout.</p>
23
2009-03-01T21:22:09Z
[ "python", "google-search" ]
Why does Google Search return HTTP Error 403?
600,536
<p>Consider the following Python code:</p> <pre> 30 url = "http://www.google.com/search?hl=en&safe=off&q=Monkey" 31 url_object = urllib.request.urlopen(url); 32 print(url_object.read()); </pre> <p>When this is run, an Exception is thrown:</p> <pre><code>File "/usr/local/lib/python3.0/urllib/request.py", ...
16
2009-03-01T21:16:25Z
854,782
<p>this should do the trick</p> <pre><code>user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7' url = "http://www.google.com/search?hl=en&amp;safe=off&amp;q=Monkey" headers={'User-Agent':user_agent,} request=urllib2.Request(url,None,headers) //The assembled reque...
21
2009-05-12T20:46:05Z
[ "python", "google-search" ]
Why does Google Search return HTTP Error 403?
600,536
<p>Consider the following Python code:</p> <pre> 30 url = "http://www.google.com/search?hl=en&safe=off&q=Monkey" 31 url_object = urllib.request.urlopen(url); 32 print(url_object.read()); </pre> <p>When this is run, an Exception is thrown:</p> <pre><code>File "/usr/local/lib/python3.0/urllib/request.py", ...
16
2009-03-01T21:16:25Z
4,094,342
<p>As <a href="http://stackoverflow.com/questions/600536/why-does-google-search-return-http-error-403/600551#600551">lacqui suggested</a>, the <a href="http://code.google.com/more/" rel="nofollow">Google API's</a> are the way they want you to make requests from code. Unfortunately, I found their documentation was aimed...
1
2010-11-04T06:22:55Z
[ "python", "google-search" ]
BOO Vs IronPython
600,539
<p>What is the difference between <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython">IronPython</a> and <a href="http://boo.codehaus.org/">BOO</a>? Is there a need for 2 Python-like languages?</p>
16
2009-03-01T21:17:30Z
600,552
<p>IronPython is a python implementation wheras Boo is another language with a python-esque syntax. One major difference is that Boo is statically typed by default.</p> <p>I'm sure there are more differences, I've only looked at Boo briefly, but I've been meaning to look at bit deeper (so many languages so little time...
11
2009-03-01T21:22:36Z
[ "python", "clr", "ironpython", "boo" ]
BOO Vs IronPython
600,539
<p>What is the difference between <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython">IronPython</a> and <a href="http://boo.codehaus.org/">BOO</a>? Is there a need for 2 Python-like languages?</p>
16
2009-03-01T21:17:30Z
600,567
<p>IronPython is Python. Boo looks like Python.</p> <p>They have different goals and while IronPython aims to be just like Python, Boo does not. Boo is not worried about compatibility with Python like IronPython is...</p>
3
2009-03-01T21:30:55Z
[ "python", "clr", "ironpython", "boo" ]
BOO Vs IronPython
600,539
<p>What is the difference between <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython">IronPython</a> and <a href="http://boo.codehaus.org/">BOO</a>? Is there a need for 2 Python-like languages?</p>
16
2009-03-01T21:17:30Z
601,106
<p><a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython">IronPython</a> is designed to be a faithful implementation of Python on the .NET platform. Version 1 targets Python 2.4 for compatibility, and version 2 targets version 2.5 (although most of the Python standard library modules implemented in C a...
18
2009-03-02T02:41:34Z
[ "python", "clr", "ironpython", "boo" ]
BOO Vs IronPython
600,539
<p>What is the difference between <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython">IronPython</a> and <a href="http://boo.codehaus.org/">BOO</a>? Is there a need for 2 Python-like languages?</p>
16
2009-03-01T21:17:30Z
2,617,831
<p>In a nutshell, Boo's claim to fame is that it is supposed to give you most of the benefits of Python's elegant, terse syntax and very high-level abstractions, but without sacrificing (most) of the speed advantages of a statically typed language like C#.</p>
1
2010-04-11T16:54:33Z
[ "python", "clr", "ironpython", "boo" ]
Making a 2-player web-based textual game
600,621
<p>I'm making a simple web-based, turn-based game and am trying to determine what modules exist out there to help me on this task. </p> <p>Here's the web app I'm looking to build:</p> <ul> <li>User visits the homepage, clicks on a "play game" link</li> <li>This takes the user to a "game room" where he either joins so...
3
2009-03-01T21:53:51Z
600,641
<p>Try the <a href="http://www.jabber.org" rel="nofollow">Jabber</a> protocol ... It works great for IM, but was designed for use by other types of systems as well and there's already a set of <a href="http://xmpppy.sourceforge.net/" rel="nofollow">bindings for Python</a> since it has become so popular.</p>
1
2009-03-01T22:10:31Z
[ ".net", "javascript", "python", "ajax" ]
Making a 2-player web-based textual game
600,621
<p>I'm making a simple web-based, turn-based game and am trying to determine what modules exist out there to help me on this task. </p> <p>Here's the web app I'm looking to build:</p> <ul> <li>User visits the homepage, clicks on a "play game" link</li> <li>This takes the user to a "game room" where he either joins so...
3
2009-03-01T21:53:51Z
602,291
<p>If you're not going to have huge numbers of concurrent users or want it done quickly I would go for holding game state on the server and polling via Ajax.</p> <p>The js library of your choice will make that polling easier.</p> <p>If you want it to be larger and hairier, you might look at <a href="http://code.stanz...
1
2009-03-02T13:08:22Z
[ ".net", "javascript", "python", "ajax" ]
how to time-out gracefully while downloading with python
600,848
<p>I'm downloading a huge set of files with following code in a loop:</p> <pre><code>try: urllib.urlretrieve(url2download, destination_on_local_filesystem) except KeyboardInterrupt: break except: print "Timed-out or got some other exception: "+url2download </code></pre> <p>If the server times-out on URL u...
8
2009-03-01T23:44:38Z
600,858
<p>There's a discussion of this <a href="http://www.voidspace.org.uk/python/articles/urllib2.shtml#sockets-and-layers" rel="nofollow">here</a>. Caveats (in addition to the ones they mention): I haven't tried it, and they're using <code>urllib2</code>, not <code>urllib</code> (would that be a problem for you?) (Actual...
3
2009-03-01T23:50:43Z
[ "python", "exception-handling", "downloading" ]
how to time-out gracefully while downloading with python
600,848
<p>I'm downloading a huge set of files with following code in a loop:</p> <pre><code>try: urllib.urlretrieve(url2download, destination_on_local_filesystem) except KeyboardInterrupt: break except: print "Timed-out or got some other exception: "+url2download </code></pre> <p>If the server times-out on URL u...
8
2009-03-01T23:44:38Z
600,863
<p>If you're not limited to what's shipped with python out of the box, then the <a href="http://linux.duke.edu/projects/urlgrabber/" rel="nofollow">urlgrabber</a> module might come in handy:</p> <pre><code>import urlgrabber urlgrabber.urlgrab(url2download, destination_on_local_filesystem, timeout=30...
4
2009-03-01T23:55:47Z
[ "python", "exception-handling", "downloading" ]
how to time-out gracefully while downloading with python
600,848
<p>I'm downloading a huge set of files with following code in a loop:</p> <pre><code>try: urllib.urlretrieve(url2download, destination_on_local_filesystem) except KeyboardInterrupt: break except: print "Timed-out or got some other exception: "+url2download </code></pre> <p>If the server times-out on URL u...
8
2009-03-01T23:44:38Z
601,002
<p>This question is more general about timing out a function: <a href="http://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-python">http://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-python</a></p> <p>I've used the method described in my a...
2
2009-03-02T01:33:51Z
[ "python", "exception-handling", "downloading" ]
how to time-out gracefully while downloading with python
600,848
<p>I'm downloading a huge set of files with following code in a loop:</p> <pre><code>try: urllib.urlretrieve(url2download, destination_on_local_filesystem) except KeyboardInterrupt: break except: print "Timed-out or got some other exception: "+url2download </code></pre> <p>If the server times-out on URL u...
8
2009-03-01T23:44:38Z
26,457,801
<p>Try: </p> <p><code>import socket</code></p> <p><code>socket.setdefaulttimeout(30)</code></p>
6
2014-10-20T02:36:10Z
[ "python", "exception-handling", "downloading" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
601,053
<p>Command combination seems to work through <code>|</code> character, so perhaps something like aliasing <code>:w|!your-command-here</code> to a distinct key combination?</p>
3
2009-03-02T02:00:30Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
601,084
<p><strong>Option 1:</strong> </p> <p>Write a function similar to this and place it in your startup settings:</p> <pre><code>function myex() execute ':w' execute ':!!' endfunction </code></pre> <p>You could even map a key combo to it-- look a the docs.</p> <p><hr /></p> <p><strong>Option 2 (better):</strong>...
20
2009-03-02T02:27:33Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
601,107
<p>Here you go:</p> <p><code>:nmap &lt;F1&gt; :w&lt;cr&gt;:!%&lt;cr&gt;</code></p> <p>save &amp; run (you have to be in n mode though - just add esc and a for i mode)</p>
4
2009-03-02T02:41:40Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
601,965
<ol> <li><p>Consider switching to IDLE. F5 does everything.</p></li> <li><p>Consider switching to Komodo. You can define a command so that F5 does everything.</p></li> </ol>
-11
2009-03-02T11:08:13Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
602,056
<p>I got the following from the vim tips wiki:</p> <pre><code>command! -complete=file -nargs=+ shell call s:runshellcommand(&lt;q-args&gt;) function! s:runshellcommand(cmdline) botright vnew setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap call setline(1,a:cmdline) call setline(2,substitute...
0
2009-03-02T11:58:19Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
602,315
<p>Use the <a href="http://vimdoc.sourceforge.net/htmldoc/options.html#%27autowrite%27">autowrite</a> option:</p> <pre><code>:set autowrite </code></pre> <blockquote> <p>Write the contents of the file, if it has been modified, on each :next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, <strong>:!</stro...
9
2009-03-02T13:15:39Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
619,380
<p>Okay, the simplest form of what you're looking for is the pipe command. It allows you to run multiple cmdline commands on the same line. In your case, the two commands are write \<code>w\</code> and execute current file \<code>! %:p\</code>. If you have a specific command you run for you current file, the second ...
34
2009-03-06T16:10:53Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
6,517,768
<p>Another possibility:</p> <pre><code>au BufWriteCmd *.py write | !! </code></pre> <p>Though this will <code>run</code> every time you save, which might not be what you want.</p>
3
2011-06-29T08:33:05Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
25,598,014
<p>Try making it inside the Bash!</p> <p>In case of a C file called t.c, this is very convenient:</p> <p><code>vi t.c &amp;&amp; cc t.c -o t &amp;&amp; ./t</code></p> <p>The and symbols (&amp;&amp;) ensure that one error message breaks the command chain. </p> <p>For Python this might be even easier:</p> <p><code>...
-1
2014-09-01T02:05:15Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
39,241,782
<p>This is what I put in my <code>.vimrc</code> and works like a charm</p> <pre><code>nnoremap &lt;leader&gt;r :w&lt;CR&gt;:!!&lt;CR&gt; </code></pre> <p>Of course you need to run your shell command once before this so it knows what command to execute.</p> <p>Ex:</p> <pre><code>:!node ./test.js </code></pre>
1
2016-08-31T05:58:50Z
[ "python", "vim" ]
VIM: Save and Run at the same time?
601,039
<p>I do a lot of Python quick simulation stuff and I'm constantly saving (:w) and then running (:!!). I'm wondering, is there a way to combine these actions. Maybe a "save and run" command.</p> <p>Thanks for your help.</p>
23
2009-03-02T01:54:29Z
40,002,312
<p>In vim, you could simply redirect any range of your current buffer to an external command (be it bash, python, or you own python script).</p> <pre><code># redirect whole buffer to python :%w !python </code></pre> <p>suppose your current buffer contain two lines as below,</p> <pre><code>import numpy as np print np...
0
2016-10-12T15:18:16Z
[ "python", "vim" ]
Issues with BeautifulSoup parsing
601,166
<p>I am trying to parse an html page with BeautifulSoup, but it appears that BeautifulSoup doesn't like the html or that page at all. When I run the code below, the method prettify() returns me only the script block of the page (see below). Does anybody has an idea why it happens? </p> <pre><code>import urllib2 from B...
4
2009-03-02T03:13:21Z
601,246
<p>BeautifulSoup isn't magic: if the incoming HTML is too horrible then it isn't going to work.</p> <p>In this case, the incoming HTML is exactly that: too broken for BeautifulSoup to figure out what to do. For instance it contains markup like:</p> <p>SCRIPT type=""javascript""</p> <p>(Notice the double quoting.)</p...
2
2009-03-02T04:09:28Z
[ "python", "beautifulsoup" ]
Issues with BeautifulSoup parsing
601,166
<p>I am trying to parse an html page with BeautifulSoup, but it appears that BeautifulSoup doesn't like the html or that page at all. When I run the code below, the method prettify() returns me only the script block of the page (see below). Does anybody has an idea why it happens? </p> <pre><code>import urllib2 from B...
4
2009-03-02T03:13:21Z
601,636
<p>I tested this script on BeautifulSoup version '3.0.7a' and it returns what appears to be correct output. I don't know what changed between '3.0.7a' and '3.1.0.1' but give it a try.</p>
0
2009-03-02T08:31:44Z
[ "python", "beautifulsoup" ]
Issues with BeautifulSoup parsing
601,166
<p>I am trying to parse an html page with BeautifulSoup, but it appears that BeautifulSoup doesn't like the html or that page at all. When I run the code below, the method prettify() returns me only the script block of the page (see below). Does anybody has an idea why it happens? </p> <pre><code>import urllib2 from B...
4
2009-03-02T03:13:21Z
601,725
<p>Try with version 3.0.7a as <a href="http://stackoverflow.com/questions/601166/issues-with-beautifulsoup-parsing/601636#601636">Łukasz</a> suggested. BeautifulSoup 3.1 was designed to be compatible with Python 3.0 so they had to change the parser from SGMLParser to HTMLParser which seems more vulnerable to bad HTML....
6
2009-03-02T09:16:27Z
[ "python", "beautifulsoup" ]
Issues with BeautifulSoup parsing
601,166
<p>I am trying to parse an html page with BeautifulSoup, but it appears that BeautifulSoup doesn't like the html or that page at all. When I run the code below, the method prettify() returns me only the script block of the page (see below). Does anybody has an idea why it happens? </p> <pre><code>import urllib2 from B...
4
2009-03-02T03:13:21Z
617,959
<pre><code>import urllib from BeautifulSoup import BeautifulSoup &gt;&gt;&gt; page = urllib.urlopen('http://www.futureshop.ca/catalog/subclass.asp?catid=10607&amp;mfr=&amp;logon=&amp;langid=FR&amp;sort=0&amp;page=1') &gt;&gt;&gt; soup = BeautifulSoup(page) &gt;&gt;&gt; soup.prettify() </code></pre> <p>In my case by e...
0
2009-03-06T07:31:58Z
[ "python", "beautifulsoup" ]
Issues with BeautifulSoup parsing
601,166
<p>I am trying to parse an html page with BeautifulSoup, but it appears that BeautifulSoup doesn't like the html or that page at all. When I run the code below, the method prettify() returns me only the script block of the page (see below). Does anybody has an idea why it happens? </p> <pre><code>import urllib2 from B...
4
2009-03-02T03:13:21Z
767,905
<p>I had problems parsing the following code too:</p> <pre><code>&lt;script&gt; function show_ads() { document.write("&lt;div&gt;&lt;sc"+"ript type='text/javascript'src='http://pagead2.googlesyndication.com/pagead/show_ads.js'&gt;&lt;/scr"+"ipt&gt;&lt;/div&gt;"); } &lt;/script&gt; </code></pr...
1
2009-04-20T11:39:53Z
[ "python", "beautifulsoup" ]
Issues with BeautifulSoup parsing
601,166
<p>I am trying to parse an html page with BeautifulSoup, but it appears that BeautifulSoup doesn't like the html or that page at all. When I run the code below, the method prettify() returns me only the script block of the page (see below). Does anybody has an idea why it happens? </p> <pre><code>import urllib2 from B...
4
2009-03-02T03:13:21Z
1,223,038
<p>Try <a href="http://codespeak.net/lxml/" rel="nofollow">lxml</a>. Despite its name, it is also for parsing and scraping HTML. It's much, much faster than BeautifulSoup, and it even handles "broken" HTML better than BeautifulSoup, so it might work better for you. It has a compatibility API for BeautifulSoup too if yo...
4
2009-08-03T15:39:32Z
[ "python", "beautifulsoup" ]
Issues with BeautifulSoup parsing
601,166
<p>I am trying to parse an html page with BeautifulSoup, but it appears that BeautifulSoup doesn't like the html or that page at all. When I run the code below, the method prettify() returns me only the script block of the page (see below). Does anybody has an idea why it happens? </p> <pre><code>import urllib2 from B...
4
2009-03-02T03:13:21Z
3,240,985
<p>Samj: If I get things like <code>HTMLParser.HTMLParseError: bad end tag: u"&lt;/scr' + 'ipt&gt;"</code> I just remove the culprit from markup before I serve it to BeautifulSoup and all is dandy:</p> <pre><code>html = urllib2.urlopen(url).read() html = html.replace("&lt;/scr' + 'ipt&gt;","") soup = BeautifulSoup(htm...
1
2010-07-13T20:00:35Z
[ "python", "beautifulsoup" ]
Recommendations for Python development on a Mac?
601,236
<p>I bought a low-end MacBook about a month ago and am finally getting around to configuring it for Python. I've done most of my Python work in Windows up until now, and am finding the choices for OS X a little daunting. It looks like there are at least five options to use for Python development:</p> <ul> <li>"Stock" ...
8
2009-03-02T04:04:06Z
601,243
<p>Here's some helpful info to get you started. <a href="http://www.python.org/download/mac/" rel="nofollow">http://www.python.org/download/mac/</a></p>
3
2009-03-02T04:07:28Z
[ "python", "osx" ]
Recommendations for Python development on a Mac?
601,236
<p>I bought a low-end MacBook about a month ago and am finally getting around to configuring it for Python. I've done most of my Python work in Windows up until now, and am finding the choices for OS X a little daunting. It looks like there are at least five options to use for Python development:</p> <ul> <li>"Stock" ...
8
2009-03-02T04:04:06Z
601,250
<p>I recommend using Python Virtual environments, especially if you use a Timecapsule because Timecapsule will back everything up, except modules you added to Python!</p>
2
2009-03-02T04:14:25Z
[ "python", "osx" ]
Recommendations for Python development on a Mac?
601,236
<p>I bought a low-end MacBook about a month ago and am finally getting around to configuring it for Python. I've done most of my Python work in Windows up until now, and am finding the choices for OS X a little daunting. It looks like there are at least five options to use for Python development:</p> <ul> <li>"Stock" ...
8
2009-03-02T04:04:06Z
601,252
<p>One advantage I see in using the "stock" Python that's included with Mac OS X is that it makes deployment to other Macs a piece of cake. I don't know what your deployment scenario is, but for me this is important. My code has to run on any number of Macs at work, and I try to minimize the amount of work it takes to ...
9
2009-03-02T04:15:18Z
[ "python", "osx" ]