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 |
|---|---|---|---|---|---|---|---|---|---|
If it is decided that our system needs an overhaul, what is the best way to go about it? | 87,522 | <p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that... | 3 | 2008-09-17T20:48:45Z | 89,434 | <p>Don't throw away your code!</p>
<p>It's the single worst mistake you can make (on a large codebase). See <a href="http://www.joelonsoftware.com/articles/fog0000000069.html" rel="nofollow">Things You Should Never Do, Part 1</a>.</p>
<p>You've invested a lot of effort into that old code and worked out many bugs. T... | 7 | 2008-09-18T02:19:51Z | [
"python",
"asp-classic",
"vbscript"
] |
If it is decided that our system needs an overhaul, what is the best way to go about it? | 87,522 | <p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that... | 3 | 2008-09-17T20:48:45Z | 89,452 | <p>Don't try and go 2.0 ( more features then currently exists or scheduled) instead build your new platform with the intent of resolving the current issues with the code base (maintainability/speed/wtf) and go from there. </p>
| 0 | 2008-09-18T02:23:49Z | [
"python",
"asp-classic",
"vbscript"
] |
If it is decided that our system needs an overhaul, what is the best way to go about it? | 87,522 | <p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that... | 3 | 2008-09-17T20:48:45Z | 101,074 | <p>Half a year ago I took over a large web application (fortunately already in Python) which had some major architectural deficiencies (templates and code mixed, code duplication, you name it...).</p>
<p>My plan is to eventually have the system respond to WSGI, but I am not there yet. I found the best way to do it, is... | 1 | 2008-09-19T10:56:06Z | [
"python",
"asp-classic",
"vbscript"
] |
If it is decided that our system needs an overhaul, what is the best way to go about it? | 87,522 | <p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that... | 3 | 2008-09-17T20:48:45Z | 593,057 | <p>A good place to begin if you're considering the move to Python is to rewrite your administrator interface in Django. This will help you get some of the kinks worked out in terms of getting Python up and running with IIS (or to migrate it to Apache). Speaking of which, I recommend <a href="http://code.google.com/p/... | 0 | 2009-02-27T00:10:31Z | [
"python",
"asp-classic",
"vbscript"
] |
If it is decided that our system needs an overhaul, what is the best way to go about it? | 87,522 | <p>We are mainting a web application that is built on Classic ASP using VBScript as the primary language. We are in agreement that our backend (framework if you will) is out dated and doesn't provide us with the proper tools to move forward in a quick manner. We have pretty much embraced the current webMVC pattern that... | 3 | 2008-09-17T20:48:45Z | 994,326 | <p>I agree with Michael Pryor and Joel that it's almost always a better idea to continue evolving your existing code base rather than re-writing from scratch. There are typically opportunities to just re-write or re-factor certain components for performance or flexibility.</p>
| 0 | 2009-06-15T02:33:35Z | [
"python",
"asp-classic",
"vbscript"
] |
Setting Environment Variables for Mercurial Hook | 88,194 | <p>I am trying to call a shell script that sets a bunch of environment variables on our server from a mercurial hook. The shell script gets called fine when a new changegroup comes in, but the environment variables aren't carrying over past the call to the shell script.</p>
<p>My hgrc file on the respository looks lik... | 1 | 2008-09-17T22:05:56Z | 88,209 | <p>Shell scripts can't modify their enviroment. </p>
<p><a href="http://tldp.org/LDP/abs/html/gotchas.html" rel="nofollow">http://tldp.org/LDP/abs/html/gotchas.html</a></p>
<blockquote>
<p>A script may not export variables back to its parent process, the shell, or to the environment. Just as we learned in biology, ... | 2 | 2008-09-17T22:09:08Z | [
"python",
"shell",
"mercurial",
"mercurial-hook"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 88,331 | <p><strong>Update:</strong> <a href="http://django-configurations.readthedocs.org/en/latest/">django-configurations</a> has been released which is probably a better option for most people than doing it manually.</p>
<p>If you would prefer to do things manually, my earlier answer still applies:</p>
<p>I have multiple ... | 79 | 2008-09-17T22:27:35Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 88,344 | <p>I have two files. <code>settings_base.py</code> which contains common/default settings, and which is checked into source control. Each deployment has a separate <code>settings.py</code>, which executes <code>from settings_base import *</code> at the beginning and then overrides as needed.</p>
| 11 | 2008-09-17T22:29:30Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 89,338 | <p>In addition to the multiple settings files mentioned by Jim, I also tend to place two settings into my settings.py file at the top <code>BASE_DIR</code> and <code>BASE_URL</code> set to the path of the code and the URL to the base of the site, all other settings are modified to append themselves to these.</p>
<p><c... | 1 | 2008-09-18T01:59:37Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 89,823 | <p>Personally, I use a single settings.py for the project, I just have it look up the hostname it's on (my development machines have hostnames that start with "gabriel" so I just have this:</p>
<pre><code>import socket
if socket.gethostname().startswith('gabriel'):
LIVEHOST = False
else:
LIVEHOST = True
</cod... | 25 | 2008-09-18T03:45:38Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 91,608 | <p>At the end of settings.py I have the following:</p>
<pre><code>try:
from settings_local import *
except ImportError:
pass
</code></pre>
<p>This way if I want to override default settings I need to just put settings_local.py right next to settings.py.</p>
| 21 | 2008-09-18T10:57:14Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 108,512 | <p>I think it depends on the size of the site as to whether you need to step up from using SQLite, I've successfully used SQLite on several smaller live sites and it runs great.</p>
| 0 | 2008-09-20T15:37:23Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 1,527,240 | <p>I have my settings.py file in an external directory. That way, it doesn't get checked into source control, or over-written by a deploy. I put this in the settings.py file under my Django project, along with any default settings:</p>
<pre><code>import sys
import os.path
def _load_settings(path):
print "Load... | 1 | 2009-10-06T18:22:51Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 3,201,509 | <p>The most simplistic way I found was:</p>
<p>1) use the default <strong>settings.py</strong> for local development and 2)
create a <strong>production-settings.py</strong> starting with:</p>
<pre><code>import os
from settings import *
</code></pre>
<p>And then just override the settings that differ in production:</... | 6 | 2010-07-08T07:38:28Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 6,846,857 | <p>Somewhat related, for the issue of deploying Django itself with multiple databases, you may want to take a look at <a href="http://bitnami.org/stack/djangostack" rel="nofollow">Djangostack</a>. You can download a completely free installer that allows you to install Apache, Python, Django, etc. As part of the install... | 2 | 2011-07-27T15:20:25Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 7,047,633 | <p>Well i use this configuration</p>
<p>at the end of settings.py</p>
<pre><code>#settings.py
try:
from locale_settings import *
except ImportError:
pass
</code></pre>
<p>and in locale_settings.py</p>
<pre><code>#locale_settings.py
class Settings(object):
def __init__(self):
import settings
... | 0 | 2011-08-13T00:16:52Z | [
"python",
"django"
] |
How do you configure Django for simple development and deployment? | 88,259 | <p>I tend to use <a href="http://en.wikipedia.org/wiki/SQLite">SQLite</a> when doing <a href="http://en.wikipedia.org/wiki/Django%5F%28web%5Fframework%29">Django</a>
development, but on a live server something more robust is
often needed (<a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a>/<a href="http://en.wikiped... | 104 | 2008-09-17T22:16:43Z | 9,694,966 | <p>I use environment </p>
<pre><code>if os.environ.get('WEB_MODE', None) == 'production' :
from settings_production import *
else :
from settings_dev import *
</code></pre>
<p>I believe this is much better approach because eventually you need special settings for your test environment and you can easily add it ... | 0 | 2012-03-14T02:04:30Z | [
"python",
"django"
] |
How do I unit test an __init__() method of a python class with assertRaises()? | 88,325 | <p>I have a class:</p>
<pre><code>class MyClass:
def __init__(self, foo):
if foo != 1:
raise Error("foo is not equal to 1!")
</code></pre>
<p>and a unit test that is supposed to make sure the incorrect arg passed to the constructor properly raises an error:</p>
<pre><code>def testInsufficientArgs(self):
... | 16 | 2008-09-17T22:26:37Z | 88,346 | <p>'Error' in this example could be any exception object. I think perhaps you have read a code example that used it as a metasyntatic placeholder to mean, "The Appropriate Exception Class".</p>
<p>The baseclass of all exceptions is called 'Exception', and most of its subclasses are descriptive names of the type of err... | 18 | 2008-09-17T22:29:33Z | [
"python",
"unit-testing",
"exception"
] |
How do I unit test an __init__() method of a python class with assertRaises()? | 88,325 | <p>I have a class:</p>
<pre><code>class MyClass:
def __init__(self, foo):
if foo != 1:
raise Error("foo is not equal to 1!")
</code></pre>
<p>and a unit test that is supposed to make sure the incorrect arg passed to the constructor properly raises an error:</p>
<pre><code>def testInsufficientArgs(self):
... | 16 | 2008-09-17T22:26:37Z | 88,357 | <p>I think you're thinking of <a href="http://docs.python.org/lib/module-exceptions.html" rel="nofollow">Exception</a>s. Replace the word Error in your description with Exception and you should be good to go :-)</p>
| 0 | 2008-09-17T22:30:21Z | [
"python",
"unit-testing",
"exception"
] |
How do I unit test an __init__() method of a python class with assertRaises()? | 88,325 | <p>I have a class:</p>
<pre><code>class MyClass:
def __init__(self, foo):
if foo != 1:
raise Error("foo is not equal to 1!")
</code></pre>
<p>and a unit test that is supposed to make sure the incorrect arg passed to the constructor properly raises an error:</p>
<pre><code>def testInsufficientArgs(self):
... | 16 | 2008-09-17T22:26:37Z | 88,369 | <p>How about this:</p>
<pre><code>class MyClass:
def __init__(self, foo):
if foo != 1:
raise Exception("foo is not equal to 1!")
import unittest
class Tests(unittest.TestCase):
def testSufficientArgs(self):
foo = 1
MyClass(foo)
def testInsufficientArgs(self):
... | 4 | 2008-09-17T22:32:16Z | [
"python",
"unit-testing",
"exception"
] |
In Python, how do you take tokenized input such as with the C++? | 88,554 | <p>In C++, I can have take input like this: </p>
<p>cin >> a >> b >> c;</p>
<p>And a can be int, b can be float, and c can be whatever... How do I do the same in python? </p>
<p>input() and raw_input(), the way I'm using them, don't seem to be giving me the desired results.</p>
| 2 | 2008-09-17T23:06:21Z | 88,587 | <p>You generally shouldn't use <code>input()</code> in production code. If you want an int and then a float, try this:
<code><pre>
>>> line = raw_input().split()
>>> a = int(line[0])
>>> b = float(line[1])
>>> c = " ".join(line[2:])
</pre></code></p>
<p>It all depends on what exactl... | 4 | 2008-09-17T23:12:21Z | [
"c++",
"python",
"input"
] |
In Python, how do you take tokenized input such as with the C++? | 88,554 | <p>In C++, I can have take input like this: </p>
<p>cin >> a >> b >> c;</p>
<p>And a can be int, b can be float, and c can be whatever... How do I do the same in python? </p>
<p>input() and raw_input(), the way I'm using them, don't seem to be giving me the desired results.</p>
| 2 | 2008-09-17T23:06:21Z | 88,589 | <p>Depending upon what you are doing, something like the <a href="http://docs.python.org/lib/module-getopt.html" rel="nofollow">getopt</a> module could be useful, but only in certain situations and I'm not sure if it would apply in yours.</p>
| 0 | 2008-09-17T23:12:46Z | [
"c++",
"python",
"input"
] |
In Python, how do you take tokenized input such as with the C++? | 88,554 | <p>In C++, I can have take input like this: </p>
<p>cin >> a >> b >> c;</p>
<p>And a can be int, b can be float, and c can be whatever... How do I do the same in python? </p>
<p>input() and raw_input(), the way I'm using them, don't seem to be giving me the desired results.</p>
| 2 | 2008-09-17T23:06:21Z | 88,714 | <p>Since the C++ cin reads from sys.stdin, you'll often do something more like the following.</p>
<pre><code>import sys
tokens= sys.stdin.read().split()
try:
a= int(token[0])
b= float(token[1])
except ValueError, e:
print e # handle the invalid input
</code></pre>
| 3 | 2008-09-17T23:35:49Z | [
"c++",
"python",
"input"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 88,639 | <p>Regular expressions:</p>
<pre><code>>>> import re
>>> splitter = re.compile(r'([+*/])')
>>> splitter.split("2+24*48/32")
</code></pre>
<p>You can expand the regular expression to include any other characters you want to split on.</p>
| 4 | 2008-09-17T23:21:58Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 88,661 | <blockquote>
<blockquote>
<blockquote>
<p>s = "2+24*48/32"</p>
<p>p = re.compile(r'(\W+)')</p>
<p>p.split(s)</p>
</blockquote>
</blockquote>
</blockquote>
| 4 | 2008-09-17T23:25:52Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 88,663 | <p>You can use <code>split</code> from the <code>re</code> module. </p>
<p><a href="http://docs.python.org/library/re.html#re.split" rel="nofollow">re.split(pattern, string, maxsplit=0, flags=0)</a></p>
<blockquote>
<p>Split string by the occurrences of pattern. If capturing parentheses
are used in pattern, then... | 35 | 2008-09-17T23:25:56Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 88,783 | <p>This looks like a parsing problem, and thus I am compelled to present a solution based on parsing techniques.</p>
<p>While it may seem that you want to 'split' this string, I think what you actually want to do is 'tokenize' it. Tokenization or lexxing is the compilation step before parsing. I have amended my origin... | 18 | 2008-09-17T23:54:14Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 89,003 | <p>i'm sure Tim meant </p>
<pre><code>splitter = re.compile(r'([\D])').
</code></pre>
<p>if you copy exactly what he has down you only get the <code>digits</code> not the <code>operators</code>.</p>
| 0 | 2008-09-18T00:45:18Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 89,534 | <pre><code>>>> import re
>>> re.findall(r'\d+|\D+', '2+24*48/32=10')
['2', '+', '24', '*', '48', '/', '32', '=', '10']
</code></pre>
<p>Matches consecutive digits or consecutive non-digits.</p>
<p>Each match is returned as a new element in the list.</p>
<p>Depending on the usage, you may need to a... | 17 | 2008-09-18T02:39:38Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 89,666 | <p>Another solution to this would be to avoid writing a calculator like that altogether. Writing an RPN parser is much simpler, and doesn't have any of the ambiguity inherent in writing math with infix notation.</p>
<pre><code>import operator, math
calc_operands = {
'+': (2, operator.add),
'-': (2, operator.su... | 4 | 2008-09-18T03:07:27Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 99,259 | <p>Why not just use <a href="http://code.google.com/p/sympy/" rel="nofollow">SymPy</a>? It should do what you're trying to achieve.</p>
| 0 | 2008-09-19T03:22:35Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 100,283 | <p>This is a parsing problem, so neither regex not split() are the "good" solution. Use a parser generator instead.</p>
<p>I would look closely at <a href="http://pyparsing.wikispaces.com/">pyparsing</a>. There have also been some decent articles about pyparsing in the <a href="http://pymag.phparch.com/">Python Magazi... | 6 | 2008-09-19T07:37:18Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 111,355 | <p>It just so happens that the tokens you want split are already Python tokens, so you can use the built-in <code>tokenize</code> module. It's almost a one-liner:</p>
<pre><code>from cStringIO import StringIO
from tokenize import generate_tokens
STRING = 1
list(token[STRING] for token
in generate_tokens(StringI... | 49 | 2008-09-21T16:25:42Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 3,517,872 | <p>This doesn't answer the question exactly, but I believe it solves what you're trying to achieve. I would add it as a comment, but I don't have permission to do so yet.</p>
<p>I personally would take advantage of Python's maths functionality directly with exec:</p>
<blockquote>
<p>expression = "2+24*48/32"<br>
... | 0 | 2010-08-19T00:38:43Z | [
"python",
"string",
"list",
"split"
] |
How do I split a string into a list? | 88,613 | <p>If I have this string:</p>
<blockquote>
<p>2+24*48/32</p>
</blockquote>
<p>what is the most efficient approach for creating this list:</p>
<blockquote>
<p>['2', '+', '24', '*', '48', '/', '32']</p>
</blockquote>
| 35 | 2008-09-17T23:17:12Z | 8,863,507 | <pre><code>>>> import re
>>> my_string = "2+24*48/32"
>>> my_list = re.findall(r"-?\d+|\S", my_string)
>>> print my_list
['2', '+', '24', '*', '48', '/', '32']
</code></pre>
<p>This will do the trick. I have encountered this kind of problem before.</p>
| 1 | 2012-01-14T16:21:15Z | [
"python",
"string",
"list",
"split"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 89,237 | <pre><code>import os
os.system("your command")
</code></pre>
<p>Note that this is dangerous, since the command isn't cleaned. I leave it up to you to google for the relevant docs on the 'os' and 'sys' modules. There are a bunch of functions (exec* , spawn*) that will do similar things.</p>
| 27 | 2008-09-18T01:37:24Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 89,238 | <pre><code>import os
cmd = 'ls -al'
os.system(cmd)
</code></pre>
<p>If you want to return the results of the command, you can use <a href="https://docs.python.org/2/library/os.html#os.popen"><code>os.popen</code></a>. However, this is deprecated since version 2.6 in favor of the <a href="https://docs.python.org/2/libr... | 46 | 2008-09-18T01:37:49Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 89,243 | <p>Look at the <a href="https://docs.python.org/2/library/subprocess.html">subprocess module</a> in the stdlib:</p>
<pre><code>from subprocess import call
call(["ls", "-l"])
</code></pre>
<p>The advantage of subprocess vs system is that it is more flexible (you can get the stdout, stderr, the "real" status code, bett... | 2,184 | 2008-09-18T01:39:35Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 89,255 | <p>I'd recommend using the subprocess module instead of os.system because it does shell escaping for you and is therefore much safer: <a href="http://docs.python.org/library/subprocess.html">http://docs.python.org/library/subprocess.html</a></p>
<pre><code>subprocess.call(['ping', 'localhost'])
</code></pre>
| 63 | 2008-09-18T01:42:30Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 89,262 | <p><a href="https://docs.python.org/2/library/subprocess.html">https://docs.python.org/2/library/subprocess.html</a></p>
<p>...or for a very simple command:</p>
<pre><code>import os
os.system('cat testfile')
</code></pre>
| 9 | 2008-09-18T01:43:30Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 89,267 | <p>os.system has been superceeded by the subprocess module. Use subproccess instead.</p>
| 6 | 2008-09-18T01:43:56Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 89,304 | <p>os.system is OK, but kind of dated. It's also not very secure. Instead, try subprocess. subprocess does not call sh directly and is therefore more secure than os.system.</p>
<p>Get more information at <a href="https://docs.python.org/library/subprocess.html" rel="nofollow">https://docs.python.org/library/subproc... | 11 | 2008-09-18T01:53:27Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 92,395 | <p>Here's a summary of the ways to call external programs and the advantages and disadvantages of each:</p>
<ol>
<li><p><code>os.system("some_command with args")</code> passes the command and arguments to your system's shell. This is nice because you can actually run multiple commands at once in this manner and set u... | 1,889 | 2008-09-18T13:11:46Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 95,246 | <p>I typically use:</p>
<pre><code>import subprocess
p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line,
retval = p.wait()
</code></pre>
<p>You are free to do what you want with the stdout data in the pipe. In fact, you can simpl... | 153 | 2008-09-18T18:20:46Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 2,030,768 | <p>There is another difference here which is not mentioned above.</p>
<p>subprocess.Popen executes the as a subprocess. In my case, I need to execute file which needs to communicate with another program . </p>
<p>I tried subprocess, execution was successful. However could not comm w/ .
everything normal when I run... | 7 | 2010-01-08T21:11:30Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 2,251,026 | <p>Some hints on detaching the child process from the calling one (starting the child process in background).</p>
<p>Suppose you want to start a long task from a CGI-script, that is the child process should live longer than the CGI-script execution process.</p>
<p>The classical example from the subprocess module docs... | 95 | 2010-02-12T10:15:34Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 3,879,406 | <p>Check "pexpect" python library, too.
It allows for interactive controlling of external programs/commands, even ssh, ftp, telnet etc. You can just type something like:</p>
<pre><code>child = pexpect.spawn('ftp 192.168.0.24')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
</... | 32 | 2010-10-07T07:09:04Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 4,728,086 | <p><code>subprocess.check_call</code> is convenient if you don't want to test return values. It throws an exception on any error.</p>
| 7 | 2011-01-18T19:21:44Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 5,824,565 | <p>If what you need is the output from the command you are calling you can use subprocess.check_output since Python 2.7</p>
<pre><code>>>> subprocess.check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
</code></pre>
| 23 | 2011-04-28T20:29:29Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 9,676,642 | <p>I always use <code>fabric</code> for this things like:</p>
<pre><code>from fabric.operations import local
result = local('ls', capture=True)
print "Content:/n%s" % (result, )
</code></pre>
<p>But this seem to be a good tool: <a href="https://github.com/amoffat/sh"><code>sh</code> (Python subprocess interface)</a>.... | 31 | 2012-03-13T00:12:54Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 10,988,365 | <p><code>os.system</code> does not allow you to store results, so if you want to store results in some list or something <code>subprocess.call</code> works.</p>
| 7 | 2012-06-11T22:28:35Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 11,507,283 | <p>you can use Popen, then you can check procedure's status</p>
<pre><code>from subprocess import Popen
proc = Popen(['ls', '-l'])
if proc.poll() is None:
proc.kill()
</code></pre>
<p>Check this out <a href="http://docs.python.org/library/subprocess.html#popen-objects">subprocess.Popen</a></p>
| 5 | 2012-07-16T15:16:24Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 11,644,161 | <p>Very simplest way to run any command and get result back:</p>
<pre><code>from commands import getstatusoutput
try:
return getstatusoutput("ls -ltr")
except Exception, e:
return None
</code></pre>
| 4 | 2012-07-25T06:51:50Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 11,940,294 | <p>I quite like <a href="http://shell-command.readthedocs.org/en/latest/index.html" rel="nofollow">shell_command</a> for its simplicity. It's built on top of the subprocess module.</p>
| 3 | 2012-08-13T18:36:32Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 13,106,558 | <p>This is how I run my commands. This code has everything you need pretty much</p>
<pre><code>from subprocess import Popen, PIPE
cmd = "ls -l ~/"
p = Popen(cmd , shell=True, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
print "Return code: ", p.returncode
print out.rstrip(), err.rstrip()
</code></pre>
| 24 | 2012-10-28T05:14:01Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 13,402,722 | <h1>Update:</h1>
<p><code>subprocess.run</code> is the recommended approach <a href="https://docs.python.org/3.6/whatsnew/3.5.html#whatsnew-subprocess">as of Python 3.5</a> if your code does not need to maintain compatibility with earlier Python versions. It's more consistent and offers similar ease-of-use as Envoy. (... | 17 | 2012-11-15T17:13:22Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 15,954,964 | <h1>With Standard Library</h1>
<p>Use <a href="http://docs.python.org/2/library/subprocess.html" rel="nofollow">subprocess module</a>:</p>
<pre><code>from subprocess import call
call(['ls', '-l'])
</code></pre>
<p>It is the recommended standard way. However, more complicated tasks (pipes, output, input, etc.) can be... | 18 | 2013-04-11T17:17:53Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 16,062,355 | <p>There are a lot of different ways to run external commands in python,
and all of them have their own plus sides and drawbacks.</p>
<p>My colleagues and me have been writing python sysadmin tools, so we need to
run a lot of external commands, and sometimes you want them to block or run asynchronously,
time-out, up... | 4 | 2013-04-17T14:10:06Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 16,072,857 | <p>without the output of result</p>
<pre><code>import os
os.system("your command here")
</code></pre>
<p>with output of result</p>
<pre><code>import commands
commands.getoutput("your command here")
or
commands.getstatusoutput("your command here")
</code></pre>
| 18 | 2013-04-18T01:09:33Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 16,089,689 | <p>The <a href="http://docs.python.org/2/library/subprocess.html" rel="nofollow">subprocess module</a> described above by Eli is very powerful, but the syntax to make a bog-standard system call and inspect its output, is unnecessarily prolix.</p>
<p>The easiest way to make a system call is with the <a href="http://doc... | 0 | 2013-04-18T17:39:50Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 17,202,916 | <p>Just to add to the discussion, if you include using a Python console, you can call external commands from ipython. While in the ipython prompt, you can call call shell commands by prefixing '!'. You can also combine python code with shell, and assign the output of shell scripts to python variables. </p>
<p>For inst... | 4 | 2013-06-19T23:18:34Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 22,395,328 | <p>After some research, I have the following code which works very well for me. It basically prints both stdout and stderr in real time. Hope it helps someone else who needs it.</p>
<pre><code>stdout_result = 1
stderr_result = 1
def stdout_thread(pipe):
global stdout_result
while True:
out = pipe.std... | 2 | 2014-03-14T02:59:05Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 23,030,076 | <p>Use <a href="https://docs.python.org/2/library/subprocess.html" rel="nofollow">subprocess.call</a>:</p>
<pre class="lang-python prettyprint-override"><code>from subprocess import call
# using list
call(["echo", "Hello", "world"])
# single string argument varies across platforms so better split it
call("echo Hel... | 1 | 2014-04-12T11:58:23Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 23,391,049 | <p>I tend to use <a href="https://docs.python.org/2/library/subprocess.html">subprocess</a> together with <a href="https://docs.python.org/2/library/shlex.html">shlex</a> (to handle escaping of quoted strings):</p>
<pre><code>>>> import subprocess, shlex
>>> command = 'ls -l "/your/path/with spaces/"... | 5 | 2014-04-30T14:37:04Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 23,416,345 | <p>Shameless plug, I wrote a library for this :P
<a href="https://github.com/houqp/shell.py">https://github.com/houqp/shell.py</a></p>
<p>It's basically a wrapper for popen and shlex for now. It also supports piping commands so you can chain commands easier in Python. So you can do things like:</p>
<pre><code>ex('ech... | 5 | 2014-05-01T20:49:01Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 25,476,624 | <p>A simple way is to use the os module:</p>
<pre><code>import os
os.system('ls')
</code></pre>
<p>Alternatively you can also use the subprocess module</p>
<pre><code>import subprocess
subprocess.check_call('ls')
</code></pre>
<p>If you want the result to be stored in a variable try:</p>
<pre><code>import subproce... | 2 | 2014-08-24T21:46:12Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 26,305,089 | <p>There is also <a href="http://plumbum.readthedocs.org/en/latest/">Plumbum</a></p>
<pre><code>>>> from plumbum import local
>>> ls = local["ls"]
>>> ls
LocalCommand(<LocalPath /bin/ls>)
>>> ls()
u'build.py\ndist\ndocs\nLICENSE\nplumbum\nREADME.rst\nsetup.py\ntests\ntodo.txt\... | 10 | 2014-10-10T17:41:13Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 31,114,625 | <pre><code>import os
cmd = 'ls -al'
os.system(cmd)
</code></pre>
<p><strong>os - This module provides a portable way of using operating system dependent functionality</strong></p>
<p>for the more os functions <a href="https://docs.python.org/2/library/os.html">here</a> is the documentation.</p>
| 6 | 2015-06-29T11:34:22Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 31,618,111 | <p>Using <strong>Popen</strong> function of <strong>subprocess</strong> python module is the simplest way of running linux commands. In that, <strong>Popen.communicate()</strong> function will give your commands output. For example</p>
<pre><code>import subprocess
..
process = subprocess.Popen(..) # pass command an... | 1 | 2015-07-24T19:12:21Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 33,118,899 | <p>Here are my 2 cents: In my view this is best practice when dealing with external commands...</p>
<p>This is return values from execute method...</p>
<pre><code>pass, stdout, stderr = execute(["ls","-la"],"/home/user/desktop")
</code></pre>
<p>This is execute method...</p>
<pre><code>def execute(cmdArray,workingD... | 4 | 2015-10-14T07:12:51Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 36,058,314 | <p>For python 3.5+ it is recommended that you use the <a href="https://docs.python.org/3.5/library/subprocess.html#subprocess.run" rel="nofollow">run function from the subprocess module</a>. This returns a <code>CompletedProcess</code> object, from which you can easily obtain the output as well as return code.</p>
<pr... | 3 | 2016-03-17T10:48:32Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 36,913,076 | <p>I would recommend the following method 'run' and it will help us in getting STDOUT, STDERR and exit status as dictionary; The caller of this can read the dictionary return by 'run' method to know the actual state of process. </p>
<pre><code> def run (cmd):
print "+ DEBUG exec({0})".format(cmd)
p = su... | 1 | 2016-04-28T11:18:22Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 37,877,635 | <p>In windows you can just import subprocess module and run external commands by calling subprocess.Popen(), subprocess.Popen().communicate() and subprocess.Popen().wait() as below:</p>
<pre><code># Python script to run a command line
import subprocess
def execute(cmd):
"""
Purpose : To execute a command... | 0 | 2016-06-17T09:14:24Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 38,012,358 | <p>import subprocess</p>
<pre><code>p = subprocess.Popen("df -h", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
print p.split("\n")
</code></pre>
<p>Gives nice output which is easier to work with</p>
<pre><code>['Filesystem Size Used Avail Use% Mounted on',
'/dev/sda6 32G... | 1 | 2016-06-24T11:29:00Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 38,477,871 | <p>To fetch the network id from the openstack neutron:</p>
<pre><code>#!/usr/bin/python
import os
netid= "nova net-list | awk '/ External / { print $2 }'"
temp=os.popen(netid).read() /* here temp also contains new line (\n) */
networkId=temp.rstrip()
print(networkId)
</code></pre>
<p>Output of <strong>nova net-list<... | 4 | 2016-07-20T09:50:01Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 39,447,501 | <p>Many way to call command.</p>
<ul>
<li>e.g:</li>
</ul>
<p>if <code>and.exe</code> needs two parameters.In cmd we can call sample.exe use this:
<code>and.exe 2 3</code> and it show <code>5</code> on screen.</p>
<p>if we use python script to call <code>and.exe</code>,we should do like..</p>
<ol>
<li><p><code>os... | 0 | 2016-09-12T09:44:09Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
Calling an external command in Python | 89,228 | <p>How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?</p>
| 2,312 | 2008-09-18T01:35:30Z | 39,969,619 | <p>Here is calling a external command and return or print commands output.</p>
<p>Python <a href="https://docs.python.org/2/library/subprocess.html" rel="nofollow">Subprocess</a> check_output is good for </p>
<blockquote>
<p>Run command with arguments and return its output as a byte string.</p>
</blockquote>
<pre>... | 0 | 2016-10-11T02:26:49Z | [
"python",
"shell",
"command",
"subprocess",
"external"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 89,915 | <p>use a regex and see if it matches!</p>
<pre><code>([a-z][A-Z][0-9]\_\-)*
</code></pre>
| -1 | 2008-09-18T04:06:30Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 89,919 | <p>A regular expression will do the trick with very little code:</p>
<pre><code>import re
...
if re.match("^[A-Za-z0-9_-]*$", my_little_string):
# do something here
</code></pre>
| 77 | 2008-09-18T04:08:19Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 89,937 | <pre><code> pat = re.compile ('[^\w-]')
def onlyallowed(s):
return not pat.search (s)
</code></pre>
| 3 | 2008-09-18T04:12:00Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 89,940 | <p>You could always use a list comprehension and check the results with all, it would be a little less resource intensive than using a regex: <code>all([c in string.letters + string.digits + ["_", "-"] for c in mystring])</code></p>
| 0 | 2008-09-18T04:12:12Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 89,971 | <p>There are a variety of ways of achieving this goal, some are clearer than others. For each of my examples, 'True' means that the string passed is valid, 'False' means it contains invalid characters.</p>
<p>First of all, there's the naive approach:</p>
<pre><code>import string
allowed = string.letters + string.digi... | 12 | 2008-09-18T04:18:40Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 91,564 | <p>As an alternative to using regex you could do it in Sets:</p>
<pre><code>from sets import Set
allowed_chars = Set('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-')
if Set(my_little_sting).issubset(allowed_chars):
# your action
print True
</code></pre>
| 4 | 2008-09-18T10:47:49Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 91,572 | <p>If it were not for the dashes and underscores, the easiest solution would be</p>
<pre><code>my_little_string.isalnum()
</code></pre>
<p>(Section <a href="https://docs.python.org/3/library/stdtypes.html#str.isalnum" rel="nofollow">3.6.1</a> of the Python Library Reference)</p>
| 10 | 2008-09-18T10:49:54Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 92,000 | <p>[Edit] There's another solution not mentioned yet, and it seems to outperform the others given so far in most cases.</p>
<p>Use string.translate to replace all valid characters in the string, and see if we have any invalid ones left over. This is pretty fast as it uses the underlying C function to do the work, wi... | 20 | 2008-09-18T12:19:48Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 13,649,498 | <p>Here's something based on Jerub's "naive approach" (naive being his words, not mine!):</p>
<pre><code>import string
ALLOWED = frozenset(string.ascii_letters + string.digits + '_' + '-')
def check(mystring):
return all(c in ALLOWED for c in mystring)
</code></pre>
<p>If <code>ALLOWED</code> was a string then I... | 0 | 2012-11-30T16:50:13Z | [
"python",
"regex",
"string"
] |
How do I verify that a string only contains letters, numbers, underscores and dashes? | 89,909 | <p>I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.</p>
<p>Thanks</p>
| 54 | 2008-09-18T04:04:58Z | 19,970,696 | <p>Well you can ask the help of regex, the great in here :)</p>
<p>code:</p>
<pre><code>import re
string = 'adsfg34wrtwe4r2_()' #your string that needs to be matched.
regex = r'^[\w\d_()]*$' # you can also add a space in regex if u want to allow it in the string
if re.match(regex,string):
print 'yes'
else:
... | 1 | 2013-11-14T06:04:42Z | [
"python",
"regex",
"string"
] |
Issue with Regular expressions in python | 90,052 | <p>Ok, so i'm working on a regular expression to search out all the header information in a site.</p>
<p>I've compiled the regular expression:</p>
<pre><code>regex = re.compile(r'''
<h[0-9]>\s?
(<a[ ]href="[A-Za-z0-9.]*">)?\s?
[A-Za-z0-9.,:'"=/?;\s]*\s?
[A-Za-z0-9.,:'"=/?;\s]?
''', re.X)
... | 2 | 2008-09-18T04:36:00Z | 90,064 | <p>Please, don't manually parse html in python! There are many better options available; I'd recommend the wonderful <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a></p>
| 2 | 2008-09-18T04:38:36Z | [
"python",
"html",
"regex"
] |
Issue with Regular expressions in python | 90,052 | <p>Ok, so i'm working on a regular expression to search out all the header information in a site.</p>
<p>I've compiled the regular expression:</p>
<pre><code>regex = re.compile(r'''
<h[0-9]>\s?
(<a[ ]href="[A-Za-z0-9.]*">)?\s?
[A-Za-z0-9.,:'"=/?;\s]*\s?
[A-Za-z0-9.,:'"=/?;\s]?
''', re.X)
... | 2 | 2008-09-18T04:36:00Z | 90,095 | <p>Parsing things with regular expressions works for regular languages. HTML is not a regular language, and the stuff you find on web pages these days is absolute crap. <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> deals with tag-soup HTML with browser-like heuristics so you g... | 4 | 2008-09-18T04:43:01Z | [
"python",
"html",
"regex"
] |
Issue with Regular expressions in python | 90,052 | <p>Ok, so i'm working on a regular expression to search out all the header information in a site.</p>
<p>I've compiled the regular expression:</p>
<pre><code>regex = re.compile(r'''
<h[0-9]>\s?
(<a[ ]href="[A-Za-z0-9.]*">)?\s?
[A-Za-z0-9.,:'"=/?;\s]*\s?
[A-Za-z0-9.,:'"=/?;\s]?
''', re.X)
... | 2 | 2008-09-18T04:36:00Z | 90,104 | <p>Because of the braces around the anchor tag, that part is interpreted as a capture group. This causes only the capture group to be returned, and not the whole regex match.</p>
<p>Put the entire regex in braces and you'll see the right matches showing up as the first element in the returned tuples.</p>
<p>But indee... | 2 | 2008-09-18T04:44:24Z | [
"python",
"html",
"regex"
] |
Issue with Regular expressions in python | 90,052 | <p>Ok, so i'm working on a regular expression to search out all the header information in a site.</p>
<p>I've compiled the regular expression:</p>
<pre><code>regex = re.compile(r'''
<h[0-9]>\s?
(<a[ ]href="[A-Za-z0-9.]*">)?\s?
[A-Za-z0-9.,:'"=/?;\s]*\s?
[A-Za-z0-9.,:'"=/?;\s]?
''', re.X)
... | 2 | 2008-09-18T04:36:00Z | 90,138 | <p>This question has been asked in several forms over the last few days, so I'm going to say this very clearly.</p>
<h1>Q: How do I parse HTML with Regular Expressions?</h1>
<h1>A: Please Don't.</h1>
<p>Use <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a>, <a href="http://code... | 23 | 2008-09-18T04:52:26Z | [
"python",
"html",
"regex"
] |
Issue with Regular expressions in python | 90,052 | <p>Ok, so i'm working on a regular expression to search out all the header information in a site.</p>
<p>I've compiled the regular expression:</p>
<pre><code>regex = re.compile(r'''
<h[0-9]>\s?
(<a[ ]href="[A-Za-z0-9.]*">)?\s?
[A-Za-z0-9.,:'"=/?;\s]*\s?
[A-Za-z0-9.,:'"=/?;\s]?
''', re.X)
... | 2 | 2008-09-18T04:36:00Z | 90,206 | <p>As has been mentioned, you should use a parser instead of a regex.</p>
<p>This is how you could do it with a regex though:</p>
<pre><code>import re
html = '''
<body>
<h1>Dog </h1>
<h2>Cat </h2>
<h3>Fancy </h3>
<h1>Tall cup of lemons</h1>
<h1><a href=... | 1 | 2008-09-18T05:10:43Z | [
"python",
"html",
"regex"
] |
Issue with Regular expressions in python | 90,052 | <p>Ok, so i'm working on a regular expression to search out all the header information in a site.</p>
<p>I've compiled the regular expression:</p>
<pre><code>regex = re.compile(r'''
<h[0-9]>\s?
(<a[ ]href="[A-Za-z0-9.]*">)?\s?
[A-Za-z0-9.,:'"=/?;\s]*\s?
[A-Za-z0-9.,:'"=/?;\s]?
''', re.X)
... | 2 | 2008-09-18T04:36:00Z | 90,894 | <p>Building on the answers so far:</p>
<p>It's best to use a parsing engine. It can cover a lot of cases and in an elegant way. I've tried <a href="http://www.crummy.com/software/BeautifulSoup/" rel="nofollow">BeautifulSoup</a> and I like it very much. Also easy to use, with a great tutorial.</p>
<p>If sometimes it f... | 2 | 2008-09-18T08:18:38Z | [
"python",
"html",
"regex"
] |
Issue with Regular expressions in python | 90,052 | <p>Ok, so i'm working on a regular expression to search out all the header information in a site.</p>
<p>I've compiled the regular expression:</p>
<pre><code>regex = re.compile(r'''
<h[0-9]>\s?
(<a[ ]href="[A-Za-z0-9.]*">)?\s?
[A-Za-z0-9.,:'"=/?;\s]*\s?
[A-Za-z0-9.,:'"=/?;\s]?
''', re.X)
... | 2 | 2008-09-18T04:36:00Z | 618,080 | <p>I have used beautifulsoup to parse your desired HTML. I have the above HTML code in
a file called foo.html and later read as a file object.</p>
<pre><code>from BeautifulSoup import BeautifulSoup
H_TAGS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
def extract_data():
"""Extract the data from all headers
in a HTML... | 2 | 2009-03-06T08:40:04Z | [
"python",
"html",
"regex"
] |
How do you load an embedded icon from an exe file with PyWin32? | 90,775 | <p>I have an exe file generated with py2exe. In the setup.py I specify an icon to be embedded in the exe:</p>
<pre><code>windows=[{'script': 'my_script.py','icon_resources': [(0, 'my_icon.ico')], ...
</code></pre>
<p>I tried loading the icon using:</p>
<pre><code>hinst = win32api.GetModuleHandle(None)
hicon = win32g... | 7 | 2008-09-18T07:46:43Z | 91,245 | <p>You should set the icon ID to something other than 0:</p>
<pre><code>'icon_resources': [(42, 'my_icon.ico')]
</code></pre>
<p>Windows resource IDs must be between 1 and 32767.</p>
| 0 | 2008-09-18T09:41:01Z | [
"python",
"icons",
"exe",
"pywin32"
] |
How do you load an embedded icon from an exe file with PyWin32? | 90,775 | <p>I have an exe file generated with py2exe. In the setup.py I specify an icon to be embedded in the exe:</p>
<pre><code>windows=[{'script': 'my_script.py','icon_resources': [(0, 'my_icon.ico')], ...
</code></pre>
<p>I tried loading the icon using:</p>
<pre><code>hinst = win32api.GetModuleHandle(None)
hicon = win32g... | 7 | 2008-09-18T07:46:43Z | 91,670 | <p>If you're using wxPython, you can use the following simple code:</p>
<pre><code>wx.Icon(sys.argv[0], wx.BITMAP_TYPE_ICO)
</code></pre>
<p>I usually have code that checks whether it's running from an EXE or not, and acts accordingly:</p>
<pre><code>def get_app_icon():
if hasattr(sys, "frozen") and getattr(sys,... | 1 | 2008-09-18T11:10:35Z | [
"python",
"icons",
"exe",
"pywin32"
] |
How do you load an embedded icon from an exe file with PyWin32? | 90,775 | <p>I have an exe file generated with py2exe. In the setup.py I specify an icon to be embedded in the exe:</p>
<pre><code>windows=[{'script': 'my_script.py','icon_resources': [(0, 'my_icon.ico')], ...
</code></pre>
<p>I tried loading the icon using:</p>
<pre><code>hinst = win32api.GetModuleHandle(None)
hicon = win32g... | 7 | 2008-09-18T07:46:43Z | 92,710 | <p>Well, well... I installed py2exe and I think it's a bug. In py2exe<code>_</code>util.c they should init rt<code>_</code>icon<code>_</code>id to 1 instead of 0. The way it is now, it's impossible to load the first format of the first icon using LoadIcon/LoadImage.</p>
<p>I'll notify the developers about this if it's... | 1 | 2008-09-18T13:52:27Z | [
"python",
"icons",
"exe",
"pywin32"
] |
How do you load an embedded icon from an exe file with PyWin32? | 90,775 | <p>I have an exe file generated with py2exe. In the setup.py I specify an icon to be embedded in the exe:</p>
<pre><code>windows=[{'script': 'my_script.py','icon_resources': [(0, 'my_icon.ico')], ...
</code></pre>
<p>I tried loading the icon using:</p>
<pre><code>hinst = win32api.GetModuleHandle(None)
hicon = win32g... | 7 | 2008-09-18T07:46:43Z | 110,777 | <p>@<a href="#92710">efotinis</a>: You're right. </p>
<p>Here is a workaround until py2exe gets fixed and you don't want to include the same icon twice:</p>
<pre><code>hicon = win32gui.CreateIconFromResource(win32api.LoadResource(None, win32con.RT_ICON, 1), True)
</code></pre>
<p>Be aware that <strong>1</strong> is... | 5 | 2008-09-21T11:07:16Z | [
"python",
"icons",
"exe",
"pywin32"
] |
I need to write code in python for comparing text of two documents using fingerprint techniques | 91,183 | <p>I need to write code in python language for comparing the text of document using fingerprint techniques. I do not know to take fingerprint of a document or to generate fingerprint of a document. I'm asking if anyone knows the method or has source code for generating fingerprints of documents which is stored in bits ... | 3 | 2008-09-18T09:25:30Z | 91,238 | <p>If you want <a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function" rel="nofollow">message digests</a> (cryptographic hashes), use the <a href="http://docs.python.org/lib/module-hashlib.html" rel="nofollow">hashlib</a> library. Here's an example (<a href="http://ipython.scipy.org/" rel="nofollow">IPython<... | 4 | 2008-09-18T09:39:06Z | [
"python",
"diff"
] |
I need to write code in python for comparing text of two documents using fingerprint techniques | 91,183 | <p>I need to write code in python language for comparing the text of document using fingerprint techniques. I do not know to take fingerprint of a document or to generate fingerprint of a document. I'm asking if anyone knows the method or has source code for generating fingerprints of documents which is stored in bits ... | 3 | 2008-09-18T09:25:30Z | 91,249 | <p>You might try the following papers to get started with the concept of fingerprinting:</p>
<ul>
<li><a href="http://theory.stanford.edu/~aiken/publications/papers/sigmod03.pdf" rel="nofollow">Winnowing: Local Algorithms for Document Fingerprinting</a></li>
<li><a href="http://www.cs.cmu.edu/afs/cs/user/nch/www/koala... | 4 | 2008-09-18T09:41:55Z | [
"python",
"diff"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.