title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
listlengths
1
5
Finding a public facing IP address in Python?
166,545
<p>How can I find the public facing IP for my net work in Python?</p>
14
2008-10-03T12:20:07Z
1,794,595
<p>One way is , you can make a request to the page at</p> <p><a href="http://www.biranchi.com/ip.php" rel="nofollow">http://www.biranchi.com/ip.php</a></p> <p>it returns the IP address of your system</p>
4
2009-11-25T04:19:57Z
[ "python", "ip-address" ]
Finding a public facing IP address in Python?
166,545
<p>How can I find the public facing IP for my net work in Python?</p>
14
2008-10-03T12:20:07Z
10,843,037
<p>All of the answers I see above would report the IP address of any web proxy in use, not necessarily the public facing IP address of your system (anything not being run through a web proxy may have an entirely different IP address).</p>
1
2012-06-01T00:35:58Z
[ "python", "ip-address" ]
Finding a public facing IP address in Python?
166,545
<p>How can I find the public facing IP for my net work in Python?</p>
14
2008-10-03T12:20:07Z
16,304,401
<p>If you don't mind expletives then try:</p> <p><a href="http://wtfismyip.com/json" rel="nofollow">http://wtfismyip.com/json</a></p> <p>Bind it up in the usual urllib stuff as others have shown.</p> <p>There's also:</p> <p><a href="http://www.networksecuritytoolkit.org/nst/tools/ip.php" rel="nofollow">http://www.n...
3
2013-04-30T16:10:35Z
[ "python", "ip-address" ]
Finding a public facing IP address in Python?
166,545
<p>How can I find the public facing IP for my net work in Python?</p>
14
2008-10-03T12:20:07Z
36,406,616
<pre><code>import urllib2 text = urllib2.urlopen('http://www.whatismyip.org').read() urlRE=re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',text) urlRE ['146.148.123.123'] </code></pre> <p>Try putting whatever 'findmyipsite' you can find into a list and iterating through them for comparison. This o...
1
2016-04-04T15:20:27Z
[ "python", "ip-address" ]
Finding a public facing IP address in Python?
166,545
<p>How can I find the public facing IP for my net work in Python?</p>
14
2008-10-03T12:20:07Z
38,436,633
<p><a href="https://api.ipify.org/?format=json" rel="nofollow">https://api.ipify.org/?format=json</a> is pretty straight forward</p> <p>can be parsed by just running <code>requests.get("https://api.ipify.org/?format=json").json()['ip']</code></p>
0
2016-07-18T12:23:19Z
[ "python", "ip-address" ]
Finding a public facing IP address in Python?
166,545
<p>How can I find the public facing IP for my net work in Python?</p>
14
2008-10-03T12:20:07Z
38,445,997
<p>This is simple as</p> <pre><code>&gt;&gt;&gt; import urllib &gt;&gt;&gt; urllib.urlopen('http://icanhazip.com/').read().strip('\n') 'xx.xx.xx.xx' </code></pre>
0
2016-07-18T21:07:25Z
[ "python", "ip-address" ]
Calling Python in PHP
166,944
<p>I have a Python script I recently wrote that I call using the command line with some options. I now want a very thin web interface to call this script locally on my Mac.</p> <p>I don't want to go through the minor trouble of installing mod_python or mod_wsgi on my Mac, so I was just going to do a system() or popen(...
55
2008-10-03T13:44:41Z
167,200
<p>Depending on what you are doing, <a href="http://php.net/manual/en/function.system.php">system()</a> or <a href="http://php.net/manual/en/function.popen.php">popen()</a> may be perfect. Use system() if the Python script has no output, or if you want the Python script's output to go directly to the browser. Use pop...
82
2008-10-03T14:40:12Z
[ "php", "python" ]
Calling Python in PHP
166,944
<p>I have a Python script I recently wrote that I call using the command line with some options. I now want a very thin web interface to call this script locally on my Mac.</p> <p>I don't want to go through the minor trouble of installing mod_python or mod_wsgi on my Mac, so I was just going to do a system() or popen(...
55
2008-10-03T13:44:41Z
167,205
<p>I do this kind of thing all the time for quick-and-dirty scripts. It's quite common to have a CGI or PHP script that just uses system/popen to call some external program.</p> <p>Just be extra careful if your web server is open to the internet at large. Be sure to sanitize your GET/POST input in this case so as to...
7
2008-10-03T14:40:38Z
[ "php", "python" ]
Calling Python in PHP
166,944
<p>I have a Python script I recently wrote that I call using the command line with some options. I now want a very thin web interface to call this script locally on my Mac.</p> <p>I don't want to go through the minor trouble of installing mod_python or mod_wsgi on my Mac, so I was just going to do a system() or popen(...
55
2008-10-03T13:44:41Z
168,678
<p>There's also a PHP extension: <a href="http://www.csh.rit.edu/~jon/projects/pip/">Pip - Python in PHP</a>, which I've never tried but had it bookmarked for just such an occasion</p>
15
2008-10-03T20:13:22Z
[ "php", "python" ]
Calling Python in PHP
166,944
<p>I have a Python script I recently wrote that I call using the command line with some options. I now want a very thin web interface to call this script locally on my Mac.</p> <p>I don't want to go through the minor trouble of installing mod_python or mod_wsgi on my Mac, so I was just going to do a system() or popen(...
55
2008-10-03T13:44:41Z
11,601,572
<p>You can run a python script via php, and outputs on browser.</p> <p>Basically you have to call the python script this way:</p> <pre><code>$command = "python /path/to/python_script.py 2&gt;&amp;1"; $pid = popen( $command,"r"); while( !feof( $pid ) ) { echo fread($pid, 256); flush(); ob_flush(); usleep(100000); ...
8
2012-07-22T15:33:38Z
[ "php", "python" ]
Calling Python in PHP
166,944
<p>I have a Python script I recently wrote that I call using the command line with some options. I now want a very thin web interface to call this script locally on my Mac.</p> <p>I don't want to go through the minor trouble of installing mod_python or mod_wsgi on my Mac, so I was just going to do a system() or popen(...
55
2008-10-03T13:44:41Z
18,921,091
<p>The backquote operator will also allow you to run python scripts using similar syntax to above</p> <p>In a python file called python.py:</p> <pre><code>hello = "hello" world = "world" print hello + " " + world </code></pre> <p>In a php file called python.php:</p> <pre><code>$python = `python python.py`; echo $py...
12
2013-09-20T16:01:27Z
[ "php", "python" ]
Calling Python in PHP
166,944
<p>I have a Python script I recently wrote that I call using the command line with some options. I now want a very thin web interface to call this script locally on my Mac.</p> <p>I don't want to go through the minor trouble of installing mod_python or mod_wsgi on my Mac, so I was just going to do a system() or popen(...
55
2008-10-03T13:44:41Z
37,870,761
<p>If you want to execute your Python script in PHP, it's necessary to do this command in your php script:</p> <pre><code>exec('your script python.py') </code></pre>
-3
2016-06-16T23:03:45Z
[ "php", "python" ]
Removing a subset of a dict from within a list
167,120
<p>This is really only easy to explain with an example, so to remove the intersection of a list from within a dict I usually do something like this:</p> <pre><code>a = {1:'', 2:'', 3:'', 4:''} exclusion = [3, 4, 5] # have to build up a new list or the iteration breaks toRemove = [] for var in a.iterkeys(): if var...
5
2008-10-03T14:24:35Z
167,134
<p>Consider <a href="http://docs.python.org/library/stdtypes.html#dict.pop" rel="nofollow">dict.pop</a>:</p> <pre><code>for key in exclusion: a.pop(key, None) </code></pre> <p>The <code>None</code> keeps pop from raising an exception when <code>key</code> isn't a key.</p>
11
2008-10-03T14:28:29Z
[ "python", "list", "containers" ]
Removing a subset of a dict from within a list
167,120
<p>This is really only easy to explain with an example, so to remove the intersection of a list from within a dict I usually do something like this:</p> <pre><code>a = {1:'', 2:'', 3:'', 4:''} exclusion = [3, 4, 5] # have to build up a new list or the iteration breaks toRemove = [] for var in a.iterkeys(): if var...
5
2008-10-03T14:24:35Z
167,158
<p>Why not just use the <code>keys</code> method, instead of <code>iterkeys</code>? That way you can do it in one loop because it returns a list, not an iterator.</p>
2
2008-10-03T14:32:12Z
[ "python", "list", "containers" ]
Removing a subset of a dict from within a list
167,120
<p>This is really only easy to explain with an example, so to remove the intersection of a list from within a dict I usually do something like this:</p> <pre><code>a = {1:'', 2:'', 3:'', 4:''} exclusion = [3, 4, 5] # have to build up a new list or the iteration breaks toRemove = [] for var in a.iterkeys(): if var...
5
2008-10-03T14:24:35Z
167,167
<pre><code>a = dict((key,value) for (key,value) in a.iteritems() if key not in exclusion) </code></pre>
3
2008-10-03T14:34:20Z
[ "python", "list", "containers" ]
Removing a subset of a dict from within a list
167,120
<p>This is really only easy to explain with an example, so to remove the intersection of a list from within a dict I usually do something like this:</p> <pre><code>a = {1:'', 2:'', 3:'', 4:''} exclusion = [3, 4, 5] # have to build up a new list or the iteration breaks toRemove = [] for var in a.iterkeys(): if var...
5
2008-10-03T14:24:35Z
167,335
<p>You could change your exclusion list to a set, then just use intersection to get the overlap.</p> <pre><code>exclusion = set([3, 4, 5]) for key in exclusion.intersection(a): del a[key] </code></pre>
1
2008-10-03T15:09:12Z
[ "python", "list", "containers" ]
Problem With Python Sockets: How To Get Reliably POSTed data whatever the browser?
167,426
<p>I wrote small Python+Ajax programs (listed at the end) with socket module to study the COMET concept of asynchronous communications.<br/> The idea is to allow browsers to send messages real time each others via my python program.<br/> The trick is to let the "GET messages/..." connexion opened waiting for a message ...
1
2008-10-03T15:28:36Z
167,997
<p>I would recommend using a JS/Ajax library on the client-side just to eliminate the possibility of cross-browser issues with your code. For the same reason I would recommend using a python http server library like <a href="http://docs.python.org/library/simplehttpserver.html" rel="nofollow">SimpleHTTPServer</a> or so...
0
2008-10-03T17:31:22Z
[ "javascript", "python", "sockets", "comet" ]
Problem With Python Sockets: How To Get Reliably POSTed data whatever the browser?
167,426
<p>I wrote small Python+Ajax programs (listed at the end) with socket module to study the COMET concept of asynchronous communications.<br/> The idea is to allow browsers to send messages real time each others via my python program.<br/> The trick is to let the "GET messages/..." connexion opened waiting for a message ...
1
2008-10-03T15:28:36Z
170,005
<p>The problem you have is that</p> <ul> <li>your tcp socket handling isn't reading as much as it should</li> <li>your http handling is not complete</li> </ul> <p>I recommend the following lectures:</p> <ul> <li><a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html" rel="nofollow">rfc2616</a></li> <li><a href="h...
1
2008-10-04T08:47:12Z
[ "javascript", "python", "sockets", "comet" ]
Problem With Python Sockets: How To Get Reliably POSTed data whatever the browser?
167,426
<p>I wrote small Python+Ajax programs (listed at the end) with socket module to study the COMET concept of asynchronous communications.<br/> The idea is to allow browsers to send messages real time each others via my python program.<br/> The trick is to let the "GET messages/..." connexion opened waiting for a message ...
1
2008-10-03T15:28:36Z
170,354
<p><br/> Thank you very much Florian, your code is working!!!!<br/> I reuse the template and complete the <strong>main</strong> with my COMET mecanism and it is working much better<br/> Chrome and Firefox are working perfectly well<br/> IE has still a problem with the "long GET" system<br/> When it received the answer ...
0
2008-10-04T13:46:12Z
[ "javascript", "python", "sockets", "comet" ]
How do I enter a pound sterling character (£) into the Python interactive shell on Mac OS X?
167,439
<p><strong>Update:</strong> Thanks for the suggestions guys. After further research, I’ve reformulated the question here: <a href="http://stackoverflow.com/questions/217020/pythoneditline-on-os-x-163-sign-seems-to-be-bound-to-ed-prev-word">Python/editline on OS X: £ sign seems to be bound to ed-prev-word</a></p> <p...
5
2008-10-03T15:30:59Z
167,465
<p>Must be your setup, I can use the £ (Also european keyboard) under IDLE or the python command line just fine. (python 2.5).</p> <p>edit: I'm using windows, so mayby its a problem with the how python works under the mac OS?</p>
0
2008-10-03T15:36:35Z
[ "python", "bash", "osx", "shell", "terminal" ]
How do I enter a pound sterling character (£) into the Python interactive shell on Mac OS X?
167,439
<p><strong>Update:</strong> Thanks for the suggestions guys. After further research, I’ve reformulated the question here: <a href="http://stackoverflow.com/questions/217020/pythoneditline-on-os-x-163-sign-seems-to-be-bound-to-ed-prev-word">Python/editline on OS X: £ sign seems to be bound to ed-prev-word</a></p> <p...
5
2008-10-03T15:30:59Z
167,466
<p>In unicode it is 00A003. With the Unicode escape it would be u'\u00a003'. </p> <p>Edit: @ Patrick McElhaney said you might need to use 00A3.</p>
2
2008-10-03T15:37:38Z
[ "python", "bash", "osx", "shell", "terminal" ]
How do I enter a pound sterling character (£) into the Python interactive shell on Mac OS X?
167,439
<p><strong>Update:</strong> Thanks for the suggestions guys. After further research, I’ve reformulated the question here: <a href="http://stackoverflow.com/questions/217020/pythoneditline-on-os-x-163-sign-seems-to-be-bound-to-ed-prev-word">Python/editline on OS X: £ sign seems to be bound to ed-prev-word</a></p> <p...
5
2008-10-03T15:30:59Z
167,472
<p>I'd imagine that the terminal emulator is eating the keystroke as a control code. Maybe see if it has a config file you can mess around with?</p>
1
2008-10-03T15:39:05Z
[ "python", "bash", "osx", "shell", "terminal" ]
How do I enter a pound sterling character (£) into the Python interactive shell on Mac OS X?
167,439
<p><strong>Update:</strong> Thanks for the suggestions guys. After further research, I’ve reformulated the question here: <a href="http://stackoverflow.com/questions/217020/pythoneditline-on-os-x-163-sign-seems-to-be-bound-to-ed-prev-word">Python/editline on OS X: £ sign seems to be bound to ed-prev-word</a></p> <p...
5
2008-10-03T15:30:59Z
167,515
<p>Not the best solution, but you could type:</p> <pre><code> pound = u'\u00A3' </code></pre> <p>Then you have it in a variable you can use in the rest of your session.</p>
5
2008-10-03T15:47:29Z
[ "python", "bash", "osx", "shell", "terminal" ]
How do I enter a pound sterling character (£) into the Python interactive shell on Mac OS X?
167,439
<p><strong>Update:</strong> Thanks for the suggestions guys. After further research, I’ve reformulated the question here: <a href="http://stackoverflow.com/questions/217020/pythoneditline-on-os-x-163-sign-seems-to-be-bound-to-ed-prev-word">Python/editline on OS X: £ sign seems to be bound to ed-prev-word</a></p> <p...
5
2008-10-03T15:30:59Z
167,998
<p><code>u'\N{pound sign}'</code></p> <p>If you are using ipython, put</p> <p><code>execute pound = u'\N{pound sign}'</code></p> <p>in your ipythonrc file (in "Section: Python code to execute") this way you will always have "pound" defined as the pound symbol in the interactive shell.</p>
2
2008-10-03T17:31:58Z
[ "python", "bash", "osx", "shell", "terminal" ]
What is best way to remove duplicate lines matching regex from string using Python?
167,923
<p>This is a pretty straight forward attempt. I haven't been using python for too long. Seems to work but I am sure I have much to learn. Someone let me know if I am way off here. Needs to find patterns, write the first line which matches, and then add a summary message for remaining consecutive lines which match patte...
2
2008-10-03T17:13:37Z
168,009
<p>The rematcher function seems to do what you want:</p> <pre><code>def rematcher(re_str, iterable): matcher= re.compile(re_str) in_match= 0 for item in iterable: if matcher.match(item): if in_match == 0: yield item in_match+= 1 else: if ...
1
2008-10-03T17:34:20Z
[ "python", "regex" ]
What is best way to remove duplicate lines matching regex from string using Python?
167,923
<p>This is a pretty straight forward attempt. I haven't been using python for too long. Seems to work but I am sure I have much to learn. Someone let me know if I am way off here. Needs to find patterns, write the first line which matches, and then add a summary message for remaining consecutive lines which match patte...
2
2008-10-03T17:13:37Z
168,052
<p>Updated your code to be a bit more effective</p> <pre><code>#!/usr/bin/env python # import re import types def remove_repeats (l_string, l_regex): """Take a string, remove similar lines and replace with a summary message. l_regex accepts strings/patterns or tuples of strings/patterns. """ # Convert ...
1
2008-10-03T17:44:00Z
[ "python", "regex" ]
What is best way to remove duplicate lines matching regex from string using Python?
167,923
<p>This is a pretty straight forward attempt. I haven't been using python for too long. Seems to work but I am sure I have much to learn. Someone let me know if I am way off here. Needs to find patterns, write the first line which matches, and then add a summary message for remaining consecutive lines which match patte...
2
2008-10-03T17:13:37Z
1,250,162
<p>First, your regular expression will match more slowly than if you had left off the greedy match.</p> <pre><code>.*Dog.* </code></pre> <p>is equivalent to</p> <pre><code>Dog </code></pre> <p>but the latter matches more quickly because no backtracking is involved. The longer the strings, the more likely "Dog" app...
0
2009-08-08T23:45:20Z
[ "python", "regex" ]
Naming conventions in a Python library
168,022
<p>I'm implementing a search algorithm (let's call it MyAlg) in a python package. Since the algorithm is super-duper complicated, the package has to contain an auxiliary class for algorithm options. Currently I'm developing the entire package by myself (and I'm not a programmer), however I expect 1-2 programmers to joi...
1
2008-10-03T17:37:47Z
168,072
<p>I suggest you read <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP8</a> (styleguide for Python code).</p>
6
2008-10-03T17:48:02Z
[ "python", "naming-conventions" ]
Naming conventions in a Python library
168,022
<p>I'm implementing a search algorithm (let's call it MyAlg) in a python package. Since the algorithm is super-duper complicated, the package has to contain an auxiliary class for algorithm options. Currently I'm developing the entire package by myself (and I'm not a programmer), however I expect 1-2 programmers to joi...
1
2008-10-03T17:37:47Z
168,085
<p>Just naming it <code>Options</code> should be fine. The Python standard library generally takes the philosophy that namespaces make it easy and manageable for different packages to have identically named things. For example, <code>open</code> is both a builtin and a function in the <code>os</code> module, several ...
2
2008-10-03T17:50:55Z
[ "python", "naming-conventions" ]
Naming conventions in a Python library
168,022
<p>I'm implementing a search algorithm (let's call it MyAlg) in a python package. Since the algorithm is super-duper complicated, the package has to contain an auxiliary class for algorithm options. Currently I'm developing the entire package by myself (and I'm not a programmer), however I expect 1-2 programmers to joi...
1
2008-10-03T17:37:47Z
168,107
<p>If it all fits in one file, name the class Options. Then your users can write:</p> <pre><code>import myalg searchOpts = myalg.Options() searchOpts.whatever() mySearcher = myalg.SearchAlg(searchOpts) mySearcher.search("where's waldo?") </code></pre> <p>Note the Python Style Guide referenced in another answer sug...
2
2008-10-03T17:56:15Z
[ "python", "naming-conventions" ]
Django: How do I create a generic url routing to views?
168,113
<p>I have a pretty standard django app, and am wondering how to set the url routing so that I don't have to explicitly map each url to a view. </p> <p>For example, let's say that I have the following views: <code>Project, Links, Profile, Contact</code>. I'd rather not have my <code>urlpatterns</code> look like this:</...
6
2008-10-03T17:58:05Z
168,328
<pre><code>mods = ('Project','Links','Profile','Contact') urlpatterns = patterns('', *(('^%s/$'%n, 'mysite.app.views.%s'%n.lower()) for n in mods) ) </code></pre>
5
2008-10-03T18:49:41Z
[ "python", "django", "pylons" ]
Django: How do I create a generic url routing to views?
168,113
<p>I have a pretty standard django app, and am wondering how to set the url routing so that I don't have to explicitly map each url to a view. </p> <p>For example, let's say that I have the following views: <code>Project, Links, Profile, Contact</code>. I'd rather not have my <code>urlpatterns</code> look like this:</...
6
2008-10-03T17:58:05Z
168,601
<p>Unless you have a really <em>huge</em> number of views, writing them down explicitly is not too bad, from a style perspective.</p> <p>You can shorten your example, though, by using the prefix argument of the <code>patterns</code> function:</p> <pre><code>urlpatterns = patterns('mysite.app.views', (r'^Project/$...
5
2008-10-03T19:51:36Z
[ "python", "django", "pylons" ]
Django: How do I create a generic url routing to views?
168,113
<p>I have a pretty standard django app, and am wondering how to set the url routing so that I don't have to explicitly map each url to a view. </p> <p>For example, let's say that I have the following views: <code>Project, Links, Profile, Contact</code>. I'd rather not have my <code>urlpatterns</code> look like this:</...
6
2008-10-03T17:58:05Z
168,656
<p>You might be able to use a special view function along these lines:</p> <pre><code>def router(request, function, module): m =__import__(module, globals(), locals(), [function.lower()]) try: return m.__dict__[function.lower()](request) except KeyError: raise Http404() </code></pre> <p>an...
5
2008-10-03T20:06:35Z
[ "python", "django", "pylons" ]
Initializing cherrypy.session early
168,167
<p>I love CherryPy's API for sessions, except for one detail. Instead of saying <code>cherrypy.session["spam"]</code> I'd like to be able to just say <code>session["spam"]</code>.</p> <p>Unfortunately, I can't simply have a global <code>from cherrypy import session</code> in one of my modules, because the <code>cherr...
2
2008-10-03T18:16:08Z
169,779
<p>For CherryPy 3.1, you would need to find the right subclass of Session, run its 'setup' classmethod, and then set cherrypy.session to a ThreadLocalProxy. That all happens in cherrypy.lib.sessions.init, in the following chunks:</p> <pre><code># Find the storage class and call setup (first time only). storage_class =...
5
2008-10-04T05:12:53Z
[ "python", "cherrypy" ]
How do I build and install P4Python for Mac OS X?
168,273
<p>I've been unable to build <a href="http://www.perforce.com/perforce/loadsupp.html#api" rel="nofollow">P4Python</a> for an Intel Mac OS X 10.5.5.</p> <p>These are my steps:</p> <ol> <li>I downloaded p4python.tgz (from <a href="http://filehost.perforce.com/perforce/r07.3/tools/" rel="nofollow">http://filehost.perfor...
2
2008-10-03T18:38:40Z
170,068
<p>From <a href="http://bugs.mymediasystem.org/?do=details&amp;task_id=676" rel="nofollow">http://bugs.mymediasystem.org/?do=details&amp;task_id=676</a> suggests that Py_ssize_t was added in python 2.5, so it won't work (without some modifications) with python 2.4.</p> <p>Either install/compile your own copy of python...
1
2008-10-04T09:53:03Z
[ "python", "osx", "perforce", "p4python" ]
How do I build and install P4Python for Mac OS X?
168,273
<p>I've been unable to build <a href="http://www.perforce.com/perforce/loadsupp.html#api" rel="nofollow">P4Python</a> for an Intel Mac OS X 10.5.5.</p> <p>These are my steps:</p> <ol> <li>I downloaded p4python.tgz (from <a href="http://filehost.perforce.com/perforce/r07.3/tools/" rel="nofollow">http://filehost.perfor...
2
2008-10-03T18:38:40Z
175,097
<p>Very outdated, but maybe you can use <a href="http://public.perforce.com:8080/@md=d&amp;cd=//guest/miki_tebeka/p4py/&amp;c=5Fm@//guest/miki_tebeka/p4py/main/?ac=83" rel="nofollow">http://public.perforce.com:8080/@md=d&amp;cd=//guest/miki_tebeka/p4py/&amp;c=5Fm@//guest/miki_tebeka/p4py/main/?ac=83</a> for now</p>
0
2008-10-06T16:40:48Z
[ "python", "osx", "perforce", "p4python" ]
How do I build and install P4Python for Mac OS X?
168,273
<p>I've been unable to build <a href="http://www.perforce.com/perforce/loadsupp.html#api" rel="nofollow">P4Python</a> for an Intel Mac OS X 10.5.5.</p> <p>These are my steps:</p> <ol> <li>I downloaded p4python.tgz (from <a href="http://filehost.perforce.com/perforce/r07.3/tools/" rel="nofollow">http://filehost.perfor...
2
2008-10-03T18:38:40Z
478,587
<p>The newer version 2008.1 will build with Python 2.4.</p> <p>I had posted the minor changes required to do that on my P4Python page, but they were rolled in to the official version.</p> <p>Robert</p>
1
2009-01-26T00:18:32Z
[ "python", "osx", "perforce", "p4python" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
168,424
<p>I've done this in the past for a Python script to determine the last updated files in a directory: </p> <pre><code>import glob import os search_dir = "/mydir/" # remove anything from the list that is not a file (directories, symlinks) # thanks to J.F. Sebastion for pointing out that the requirement was a list # o...
65
2008-10-03T19:12:48Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
168,430
<p>Maybe you should use shell commands. In Unix/Linux, find piped with sort will probably be able to do what you want. </p>
-3
2008-10-03T19:14:18Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
168,435
<p>Here's a one-liner:</p> <pre><code>import os import time from pprint import pprint pprint([(x[0], time.ctime(x[1].st_ctime)) for x in sorted([(fn, os.stat(fn)) for fn in os.listdir(".")], key = lambda x: x[1].st_ctime)]) </code></pre> <p>This calls os.listdir() to get a list of the filenames, then calls os.stat()...
15
2008-10-03T19:15:23Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
168,580
<p>Here's my version:</p> <pre><code>def getfiles(dirpath): a = [s for s in os.listdir(dirpath) if os.path.isfile(os.path.join(dirpath, s))] a.sort(key=lambda s: os.path.getmtime(os.path.join(dirpath, s))) return a </code></pre> <p>First, we build a list of the file names. isfile() is used to ski...
12
2008-10-03T19:46:53Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
168,658
<pre><code>sorted(filter(os.path.isfile, os.listdir('.')), key=lambda p: os.stat(p).st_mtime) </code></pre> <p>You could use <code>os.walk('.').next()[-1]</code> instead of filtering with <code>os.path.isfile</code>, but that leaves dead symlinks in the list, and <code>os.stat</code> will fail on them.</p>
4
2008-10-03T20:07:15Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
539,024
<p>Here's a more verbose version of <a href="http://stackoverflow.com/questions/168409/how-do-you-get-a-directory-listing-sorted-by-creation-date-in-python/168435#168435"><code>@Greg Hewgill</code>'s answer</a>. It is the most conforming to the question requirements. It makes a distinction between creation and modifica...
31
2009-02-11T21:58:21Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
4,914,674
<p>There is an <code>os.path.getmtime</code> function that gives the number of seconds since the epoch and should be faster than os.stat.</p> <pre><code>os.chdir(directory) sorted(filter(os.path.isfile, os.listdir('.')), key=os.path.getmtime) </code></pre>
8
2011-02-06T16:47:48Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
7,801,791
<p>this is a basic step for learn:</p> <pre><code>import os, stat, sys import time dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.' listdir = os.listdir(dirpath) for i in listdir: os.chdir(dirpath) data_001 = os.path.realpath(i) listdir_stat1 = os.stat(data_001) listdir_stat2 = ((os.stat(data_0...
0
2011-10-18T02:29:25Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
18,783,474
<p>Here's my answer using glob without filter if you want to read files with a certain extension in date order (Python 3). </p> <pre><code>dataset_path='/mydir/' files = glob.glob(dataset_path+"/morepath/*.extension") files.sort(key=os.path.getmtime) </code></pre>
5
2013-09-13T09:59:29Z
[ "python", "windows", "directory" ]
How do you get a directory listing sorted by creation date in python?
168,409
<p>What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?</p>
52
2008-10-03T19:10:08Z
30,381,619
<p>Without changing directory:</p> <pre><code>import os path = '/path/to/files/' name_list = os.listdir(path) full_list = [os.path.join(path,i) for i in name_list] time_sorted_list = sorted(full_list, key=os.path.getmtime) print time_sorted_list # if you want just the filenames sorted, simply remove the dir fro...
5
2015-05-21T18:28:30Z
[ "python", "windows", "directory" ]
Python - How do I convert "an OS-level handle to an open file" to a file object?
168,559
<p><a href="http://www.python.org/doc/2.5.2/lib/module-tempfile.html">tempfile.mkstemp()</a> returns:</p> <blockquote> <p>a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.</p> </blockquote> <p>How do I convert that OS-lev...
41
2008-10-03T19:41:04Z
168,584
<p>You can use </p> <pre><code>os.write(tup[0], "foo\n") </code></pre> <p>to write to the handle.</p> <p>If you want to open the handle for writing you need to add the <strong>"w"</strong> mode</p> <pre><code>f = os.fdopen(tup[0], "w") f.write("foo") </code></pre>
45
2008-10-03T19:47:17Z
[ "python", "temporary-files", "mkstemp", "fdopen" ]
Python - How do I convert "an OS-level handle to an open file" to a file object?
168,559
<p><a href="http://www.python.org/doc/2.5.2/lib/module-tempfile.html">tempfile.mkstemp()</a> returns:</p> <blockquote> <p>a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.</p> </blockquote> <p>How do I convert that OS-lev...
41
2008-10-03T19:41:04Z
168,640
<p>You forgot to specify the open mode ('w') in fdopen(). The default is 'r', causing the write() call to fail.</p> <p>I think mkstemp() creates the file for reading only. Calling fdopen with 'w' probably reopens it for writing (you <em>can</em> reopen the file created by mkstemp).</p>
6
2008-10-03T20:00:14Z
[ "python", "temporary-files", "mkstemp", "fdopen" ]
Python - How do I convert "an OS-level handle to an open file" to a file object?
168,559
<p><a href="http://www.python.org/doc/2.5.2/lib/module-tempfile.html">tempfile.mkstemp()</a> returns:</p> <blockquote> <p>a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.</p> </blockquote> <p>How do I convert that OS-lev...
41
2008-10-03T19:41:04Z
168,705
<p>What's your goal, here? Is <code>tempfile.TemporaryFile</code> inappropriate for your purposes?</p>
1
2008-10-03T20:19:54Z
[ "python", "temporary-files", "mkstemp", "fdopen" ]
Python - How do I convert "an OS-level handle to an open file" to a file object?
168,559
<p><a href="http://www.python.org/doc/2.5.2/lib/module-tempfile.html">tempfile.mkstemp()</a> returns:</p> <blockquote> <p>a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.</p> </blockquote> <p>How do I convert that OS-lev...
41
2008-10-03T19:41:04Z
1,296,063
<p>Here's how to do it using a with statement:</p> <pre><code>from __future__ import with_statement from contextlib import closing fd, filepath = tempfile.mkstemp() with closing(os.fdopen(fd, 'w')) as tf: tf.write('foo\n') </code></pre>
13
2009-08-18T19:44:31Z
[ "python", "temporary-files", "mkstemp", "fdopen" ]
Python - How do I convert "an OS-level handle to an open file" to a file object?
168,559
<p><a href="http://www.python.org/doc/2.5.2/lib/module-tempfile.html">tempfile.mkstemp()</a> returns:</p> <blockquote> <p>a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.</p> </blockquote> <p>How do I convert that OS-lev...
41
2008-10-03T19:41:04Z
2,414,333
<pre><code>temp = tempfile.NamedTemporaryFile(delete=False) temp.file.write('foo\n') temp.close() </code></pre>
4
2010-03-10T03:33:07Z
[ "python", "temporary-files", "mkstemp", "fdopen" ]
Python - How do I convert "an OS-level handle to an open file" to a file object?
168,559
<p><a href="http://www.python.org/doc/2.5.2/lib/module-tempfile.html">tempfile.mkstemp()</a> returns:</p> <blockquote> <p>a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.</p> </blockquote> <p>How do I convert that OS-lev...
41
2008-10-03T19:41:04Z
16,942,329
<p>I can't comment on the answers, so I will post my comment here:</p> <p>To create a temporary file for write access you can use tempfile.mkstemp and specify "w" as the last parameter, like:</p> <pre><code>f = tempfile.mkstemp("", "", "", "w") # first three params are 'suffix, 'prefix', 'dir'... os.write(f[0], "writ...
0
2013-06-05T14:15:44Z
[ "python", "temporary-files", "mkstemp", "fdopen" ]
Now that Python 2.6 is out, what modules currently in the language should every programmer know about?
168,727
<p>A lot of useful features in Python are somewhat "hidden" inside modules. Named tuples (new in <a href="http://docs.python.org/whatsnew/2.6.html" rel="nofollow">Python 2.6</a>), for instance, are found in the <a href="http://docs.python.org/library/collections.html" rel="nofollow">collections</a> module. </p> <p>The...
9
2008-10-03T20:23:12Z
168,766
<p>May be <a href="http://www.python.org/dev/peps/pep-0361/" rel="nofollow">PEP 0631</a> and <a href="http://docs.python.org/whatsnew/2.6.html" rel="nofollow">What's new in 2.6</a> can provide elements of answer. This last article explains the new features in Python 2.6, released on October 1 2008.</p>
5
2008-10-03T20:30:29Z
[ "python", "module", "language-features" ]
Now that Python 2.6 is out, what modules currently in the language should every programmer know about?
168,727
<p>A lot of useful features in Python are somewhat "hidden" inside modules. Named tuples (new in <a href="http://docs.python.org/whatsnew/2.6.html" rel="nofollow">Python 2.6</a>), for instance, are found in the <a href="http://docs.python.org/library/collections.html" rel="nofollow">collections</a> module. </p> <p>The...
9
2008-10-03T20:23:12Z
168,768
<p>The most impressive new module is probably the <code>multiprocessing</code> module. First because it lets you execute functions in new processes just as easily and with roughly the same API as you would with the <code>threading</code> module. But more importantly because it introduces a lot of great classes for co...
12
2008-10-03T20:31:13Z
[ "python", "module", "language-features" ]
Now that Python 2.6 is out, what modules currently in the language should every programmer know about?
168,727
<p>A lot of useful features in Python are somewhat "hidden" inside modules. Named tuples (new in <a href="http://docs.python.org/whatsnew/2.6.html" rel="nofollow">Python 2.6</a>), for instance, are found in the <a href="http://docs.python.org/library/collections.html" rel="nofollow">collections</a> module. </p> <p>The...
9
2008-10-03T20:23:12Z
168,795
<p>The <a href="http://docs.python.org/library/json.html" rel="nofollow">new <code>json</code> module</a> is a real boon to web programmers!! (It was known as <a href="http://undefined.org/python/#simplejson" rel="nofollow"><code>simplejson</code></a> before being merged into the standard library.)</p> <p>It's ridicu...
6
2008-10-03T20:39:54Z
[ "python", "module", "language-features" ]
Now that Python 2.6 is out, what modules currently in the language should every programmer know about?
168,727
<p>A lot of useful features in Python are somewhat "hidden" inside modules. Named tuples (new in <a href="http://docs.python.org/whatsnew/2.6.html" rel="nofollow">Python 2.6</a>), for instance, are found in the <a href="http://docs.python.org/library/collections.html" rel="nofollow">collections</a> module. </p> <p>The...
9
2008-10-03T20:23:12Z
335,299
<p><strong>Essential Libraries</strong></p> <p>The main challenge for an experienced programmer coming from another language to Python is figuring out how one language maps to another. Here are a few essential libraries and how they relate to Java equivalents.</p> <pre><code>os, os.path </code></pre> <p>Has function...
3
2008-12-02T20:24:27Z
[ "python", "module", "language-features" ]
Python - How do I write a decorator that restores the cwd?
169,070
<p>How do I write a decorator that restores the current working directory to what it was before the decorated function was called? In other words, if I use the decorator on a function that does an os.chdir(), the cwd will not be changed after the function is called.</p>
19
2008-10-03T22:04:01Z
169,079
<pre><code>def preserve_cwd(function): def decorator(*args, **kwargs): cwd = os.getcwd() result = function(*args, **kwargs) os.chdir(cwd) return result return decorator </code></pre> <p>Here's how it's used:</p> <pre><code>@preserve_cwd def test(): print 'was:',os.getcwd() os.chdir('...
3
2008-10-03T22:06:46Z
[ "python", "decorator", "cwd" ]
Python - How do I write a decorator that restores the cwd?
169,070
<p>How do I write a decorator that restores the current working directory to what it was before the decorated function was called? In other words, if I use the decorator on a function that does an os.chdir(), the cwd will not be changed after the function is called.</p>
19
2008-10-03T22:04:01Z
169,112
<p>The answer for a decorator has been given; it works at the function definition stage as requested.</p> <p>With Python 2.5+, you also have an option to do that at the function <em>call</em> stage using a context manager:</p> <pre><code>from __future__ import with_statement # needed for 2.5 ≤ Python &lt; 2.6 impor...
27
2008-10-03T22:19:30Z
[ "python", "decorator", "cwd" ]
Python - How do I write a decorator that restores the cwd?
169,070
<p>How do I write a decorator that restores the current working directory to what it was before the decorated function was called? In other words, if I use the decorator on a function that does an os.chdir(), the cwd will not be changed after the function is called.</p>
19
2008-10-03T22:04:01Z
170,174
<p>The given answers fail to take into account that the wrapped function may raise an exception. In that case, the directory will never be restored. The code below adds exception handling to the previous answers.</p> <p>as a decorator:</p> <pre><code>def preserve_cwd(function): @functools.wraps(function) def ...
16
2008-10-04T11:29:33Z
[ "python", "decorator", "cwd" ]
Python - How do I write a decorator that restores the cwd?
169,070
<p>How do I write a decorator that restores the current working directory to what it was before the decorated function was called? In other words, if I use the decorator on a function that does an os.chdir(), the cwd will not be changed after the function is called.</p>
19
2008-10-03T22:04:01Z
14,019,583
<p>The <a href="https://github.com/jaraco/path.py">path.py</a> module (which you really should use if dealing with paths in python scripts) has a context manager:</p> <pre><code>subdir = d / 'subdir' #subdir is a path object, in the path.py module with subdir: # here current dir is subdir #not anymore </code></pre>...
8
2012-12-24T09:38:59Z
[ "python", "decorator", "cwd" ]
How can I compress a folder and email the compressed file in Python?
169,362
<p>I would like to compress a folder and all its sub-folders/files, and email the zip file as an attachment. What would be the best way to achieve this with Python? </p>
7
2008-10-03T23:53:35Z
169,395
<p>Look at <a href="http://www.python.org/doc/2.5.2/lib/module-zipfile.html" rel="nofollow">zipfile</a> for compressing a folder and it's subfolders.</p> <p>Look at <a href="http://www.python.org/doc/2.5.2/lib/module-smtplib.html" rel="nofollow">smtplib</a> for an email client.</p>
1
2008-10-04T00:10:26Z
[ "python" ]
How can I compress a folder and email the compressed file in Python?
169,362
<p>I would like to compress a folder and all its sub-folders/files, and email the zip file as an attachment. What would be the best way to achieve this with Python? </p>
7
2008-10-03T23:53:35Z
169,403
<p>You can use <a href="http://www.python.org/doc/2.5.2/lib/module-zipfile.html" rel="nofollow">zipfile</a> that ships with python, and <a href="http://snippets.dzone.com/posts/show/2038" rel="nofollow">here</a> you can find an example of sending an email with attachments with the standard smtplib</p>
0
2008-10-04T00:14:54Z
[ "python" ]
How can I compress a folder and email the compressed file in Python?
169,362
<p>I would like to compress a folder and all its sub-folders/files, and email the zip file as an attachment. What would be the best way to achieve this with Python? </p>
7
2008-10-03T23:53:35Z
169,406
<p>You can use the <a href="http://docs.python.org/dev/library/zipfile.html">zipfile</a> module to compress the file using the zip standard, the <a href="http://docs.python.org/dev/library/email.html">email</a> module to create the email with the attachment, and the <a href="http://docs.python.org/dev/library/smtplib.h...
18
2008-10-04T00:17:28Z
[ "python" ]
2D animation in Python
169,810
<p>I'm writing a simulator in Python, and am curious about options and opinions regarding basic 2D animations. By animation, I'm referring to rendering on the fly, not displaying prerendered images.</p> <p>I'm currently using matplotlib (Wxagg backend), and it's possible that I'll be able to continue using it, but I s...
6
2008-10-04T05:36:23Z
169,825
<p>I am a fan of <a href="http://pyglet.org" rel="nofollow">pyglet</a> which is a completely self contained library for doing graphical work under win32, linux, and OS X. </p> <p>It has very low overhead, and you can see this for yourself from the tutorial on the website. It <em>should</em> play well with wxpython, or...
10
2008-10-04T05:50:03Z
[ "python", "animation", "2d" ]
2D animation in Python
169,810
<p>I'm writing a simulator in Python, and am curious about options and opinions regarding basic 2D animations. By animation, I'm referring to rendering on the fly, not displaying prerendered images.</p> <p>I'm currently using matplotlib (Wxagg backend), and it's possible that I'll be able to continue using it, but I s...
6
2008-10-04T05:36:23Z
1,568,711
<p>You can try pygame, its very easy to handle and similar to SDL under c++</p>
3
2009-10-14T20:16:46Z
[ "python", "animation", "2d" ]
How to package Twisted program with py2exe?
169,897
<p>I tried to package a Twisted program with py2exe, but once I run the exe file I built, I got a "No module named resource" error. </p> <p>And I found the py2exe said:</p> <blockquote> <p>The following modules appear to be missing ['FCNTL', 'OpenSSL', 'email.Generator', 'email.Iterators', 'email.Utils', 'pkg_resou...
10
2008-10-04T07:08:05Z
169,913
<p>I've seen this before... py2exe, for some reason, is not detecting that these modules are needed inside the ZIP archive and is leaving them out.</p> <p>You can explicitly specify modules to include on the py2exe command line:</p> <pre><code>python setup.py py2exe -p win32com -i twisted.web.resource </code></pre> ...
10
2008-10-04T07:21:29Z
[ "python", "twisted", "py2exe" ]
How to package Twisted program with py2exe?
169,897
<p>I tried to package a Twisted program with py2exe, but once I run the exe file I built, I got a "No module named resource" error. </p> <p>And I found the py2exe said:</p> <blockquote> <p>The following modules appear to be missing ['FCNTL', 'OpenSSL', 'email.Generator', 'email.Iterators', 'email.Utils', 'pkg_resou...
10
2008-10-04T07:08:05Z
31,598,939
<p>Had same issue with email module. I got it working by explicitly including modules in setup.py:</p> <p>OLD setup.py:</p> <pre><code>setup(console = ['main.py']) </code></pre> <p>New setup.py:</p> <pre><code>setup(console = ['main.py'], options={"py2exe":{"includes":["email.mime.multipart","email.mime.text...
0
2015-07-23T22:10:36Z
[ "python", "twisted", "py2exe" ]
USB Driver Development on a Mac using Python
170,278
<p>I would like to write a driver to talk to my Suunto t3 watch in Python on a Mac. My day job is doing basic web work in C# so my familiarity with Python and developing on a Mac is limited.</p> <p>Can you suggest how one would start doing driver development in general and then more specifically on a Mac. I.e. how to ...
4
2008-10-04T12:45:46Z
170,368
<p>If the watch supports a <a href="http://www.usb.org/developers/devclass_docs#approved" rel="nofollow">standard USB device class specification</a> such as HID or serial communication, there might already be a Macintosh driver for it built into the OS. Otherwise, you're going to have to get information about the vendo...
3
2008-10-04T13:55:06Z
[ "python", "osx", "usb", "drivers" ]
USB Driver Development on a Mac using Python
170,278
<p>I would like to write a driver to talk to my Suunto t3 watch in Python on a Mac. My day job is doing basic web work in C# so my familiarity with Python and developing on a Mac is limited.</p> <p>Can you suggest how one would start doing driver development in general and then more specifically on a Mac. I.e. how to ...
4
2008-10-04T12:45:46Z
170,409
<p>The Mac already has the underlying infrastructure to support USB, so you'll need a Python library that can take advantage of it. For any Python project that needs serial support, whether it's USB, RS-232 or GPIB, I'd recommend the PyVisa library at SourceForge. See <a href="http://pyvisa.sourceforge.net/" rel="nof...
4
2008-10-04T14:20:31Z
[ "python", "osx", "usb", "drivers" ]
Django signals vs. overriding save method
170,337
<p>I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this:</p> <pre><code> def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() ...
49
2008-10-04T13:37:12Z
170,369
<p>If you'll use signals you'd be able to update Review score each time related score model gets saved. But if don't need such functionality i don't see any reason to put this into signal, that's pretty model-related stuff.</p>
2
2008-10-04T13:55:25Z
[ "python", "django", "django-models", "django-signals" ]
Django signals vs. overriding save method
170,337
<p>I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this:</p> <pre><code> def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() ...
49
2008-10-04T13:37:12Z
170,501
<p>It is a kind sort of denormalisation. Look at this <a href="http://groups.google.com/group/django-developers/msg/248e53722acab49e" rel="nofollow">pretty solution</a>. In-place composition field definition.</p>
1
2008-10-04T15:21:21Z
[ "python", "django", "django-models", "django-signals" ]
Django signals vs. overriding save method
170,337
<p>I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this:</p> <pre><code> def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() ...
49
2008-10-04T13:37:12Z
171,703
<p>Save/delete signals are generally favourable in situations where you need to make changes which aren't completely specific to the model in question, or could be applied to models which have something in common, or could be configured for use across models.</p> <p>One common task in overridden <code>save</code> meth...
57
2008-10-05T08:38:39Z
[ "python", "django", "django-models", "django-signals" ]
Django signals vs. overriding save method
170,337
<p>I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this:</p> <pre><code> def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() ...
49
2008-10-04T13:37:12Z
5,435,579
<p>Signals are useful when you have to execute some long term process and don't want to block your user waiting for save to complete.</p>
-14
2011-03-25T16:53:35Z
[ "python", "django", "django-models", "django-signals" ]
Django signals vs. overriding save method
170,337
<p>I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this:</p> <pre><code> def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() ...
49
2008-10-04T13:37:12Z
35,888,559
<p>You asked: </p> <p><em>Would there be any benefits to using Django's signal dispatcher?</em></p> <p>I found this in the django docs:</p> <blockquote> <p>Overridden model methods are not called on bulk operations</p> <p>Note that the delete() method for an object is not necessarily called when deleting ob...
4
2016-03-09T10:10:16Z
[ "python", "django", "django-models", "django-signals" ]
How do I write to a log from mod_python under apache?
170,353
<p>I seem to only be able to write to the Apache error log via stderr. Anyone know of a more structured logging architecture that I could use from my python web project, like commons?</p>
2
2008-10-04T13:46:06Z
170,366
<p>I've used the builtin <a href="http://www.python.org/doc/2.5.2/lib/module-logging.html" rel="nofollow">Python logging module</a> in (non-web) projects in the past, with success - it should work in a web-hosted environment as well.</p>
2
2008-10-04T13:54:49Z
[ "python", "apache", "logging" ]
How do I write to a log from mod_python under apache?
170,353
<p>I seem to only be able to write to the Apache error log via stderr. Anyone know of a more structured logging architecture that I could use from my python web project, like commons?</p>
2
2008-10-04T13:46:06Z
170,792
<p>There isn't any built in support for mod_python logging to Apache currently. If you really want to work within the Apache logs you can check out this thread (make sure you get the second version of the posted code, rather than the first):</p> <ul> <li><a href="http://www.dojoforum.com/node/13239" rel="nofollow">htt...
3
2008-10-04T18:16:55Z
[ "python", "apache", "logging" ]
How do I write to a log from mod_python under apache?
170,353
<p>I seem to only be able to write to the Apache error log via stderr. Anyone know of a more structured logging architecture that I could use from my python web project, like commons?</p>
2
2008-10-04T13:46:06Z
183,615
<p>I concur with Blair Conrad's post about the Python logging module. The standard log handlers sometimes drop messages however. It's worth using the logging module's SocketHandler and building a receiver to listen for messages and write them to file.</p> <p>Here's mine: <a href="http://www.djangosnippets.org/snippets...
0
2008-10-08T16:19:16Z
[ "python", "apache", "logging" ]
How do I write to a log from mod_python under apache?
170,353
<p>I seem to only be able to write to the Apache error log via stderr. Anyone know of a more structured logging architecture that I could use from my python web project, like commons?</p>
2
2008-10-04T13:46:06Z
14,752,499
<p>This must have changed in the past four years. If you come across this question and want to do this then you can do it through the request object, i.e</p> <pre><code>def handler(req) : req.log_error('Hello apache') </code></pre>
2
2013-02-07T13:39:14Z
[ "python", "apache", "logging" ]
UNIX shell written in a reasonable language?
171,267
<p>Has anyone ever heard of a UNIX shell written in a reasonable language, like Python?</p>
10
2008-10-05T00:42:54Z
171,271
<p>Well, there's emacs, which is arguably a shell written in lisp :)</p> <p>Seriously though, are you looking for a reimplementation of an existing shell design in a different language such as Python? Or are you looking for a new implementation of a shell language that looks similar to your language of choice?</p>
5
2008-10-05T00:45:09Z
[ "python", "unix", "shell" ]
UNIX shell written in a reasonable language?
171,267
<p>Has anyone ever heard of a UNIX shell written in a reasonable language, like Python?</p>
10
2008-10-05T00:42:54Z
171,280
<p><a href="http://ipython.scipy.org/moin/" rel="nofollow">iPython</a> (Python) and <a href="http://rush.heroku.com/" rel="nofollow">Rush</a> (Ruby) are shells that are designed for more advanced languages. There's also Hotwire, which is sort of a weird integrated shell/terminal emulator.</p>
10
2008-10-05T00:52:02Z
[ "python", "unix", "shell" ]
UNIX shell written in a reasonable language?
171,267
<p>Has anyone ever heard of a UNIX shell written in a reasonable language, like Python?</p>
10
2008-10-05T00:42:54Z
171,290
<p>Tclsh is pretty nice (assuming you like Tcl, of course).</p>
3
2008-10-05T01:05:42Z
[ "python", "unix", "shell" ]
UNIX shell written in a reasonable language?
171,267
<p>Has anyone ever heard of a UNIX shell written in a reasonable language, like Python?</p>
10
2008-10-05T00:42:54Z
171,294
<ul> <li><a href="http://www.gnu.org/software/emacs/manual/html_node/eshell/index.html" rel="nofollow">Eshell</a> is a Bash-like shell in Emacs Lisp.</li> <li>IPython can be <a href="http://ipython.org/ipython-doc/stable/interactive/shell.html" rel="nofollow">used as a system shell</a>, though the syntax is a bit weird...
22
2008-10-05T01:08:31Z
[ "python", "unix", "shell" ]
UNIX shell written in a reasonable language?
171,267
<p>Has anyone ever heard of a UNIX shell written in a reasonable language, like Python?</p>
10
2008-10-05T00:42:54Z
171,304
<p>From all appearances, Python IS a shell. It runs with <code>#!</code> and it can run interactively. Between the <code>os</code> and <code>shutil</code> packages you have all of the features of standard Unix shells.</p> <p>Since you can do anything in Python with simple, powerful scripts, you don't really need to ...
6
2008-10-05T01:15:26Z
[ "python", "unix", "shell" ]
UNIX shell written in a reasonable language?
171,267
<p>Has anyone ever heard of a UNIX shell written in a reasonable language, like Python?</p>
10
2008-10-05T00:42:54Z
39,234,819
<p>There is xon now:</p> <p><a href="http://xon.sh/" rel="nofollow">http://xon.sh/</a></p> <p><a href="http://xon.sh/tutorial.html#running-commands" rel="nofollow">http://xon.sh/tutorial.html#running-commands</a></p> <p>PyCon video - <a href="https://www.youtube.com/watch?v=uaje5I22kgE" rel="nofollow">https://www.yo...
0
2016-08-30T18:37:50Z
[ "python", "unix", "shell" ]
Sorting music
171,277
<p>So over the years, I've bought music off iTunes, Urge, and Rhapsody and all these files are lying mixed with my non-DRM'd MP3 files that I've ripped off my CDs. Now some of these files have licenses that have expired, and some of them have valid licenses.</p> <p>I want to sort my music by various DRM/license restri...
6
2008-10-05T00:49:51Z
171,284
<p>Do all the files have different extensions? If so this might work (i wrote it all off the top of my head so its not tested):</p> <pre><code>import os music_dir = "/home/johnbloggs/music/" # note the forward slashes and the trailing slash output_dir = "/home/johnbloggs/sorted_music/" for file in os.listdir(music_d...
0
2008-10-05T01:01:00Z
[ "c#", "python", "perl", "mp3", "music" ]
Sorting music
171,277
<p>So over the years, I've bought music off iTunes, Urge, and Rhapsody and all these files are lying mixed with my non-DRM'd MP3 files that I've ripped off my CDs. Now some of these files have licenses that have expired, and some of them have valid licenses.</p> <p>I want to sort my music by various DRM/license restri...
6
2008-10-05T00:49:51Z
171,297
<p>Wouldn't it be great if DRM made sense like other API's? </p> <p>Sadly, you'll have to research each DRM scheme and locate a client API for that DRM scheme.</p> <p>See this <a href="http://www.linuxdevices.com/news/NS2351492178.html" rel="nofollow">article</a> for a proposal to try and cope with the various inane...
4
2008-10-05T01:10:41Z
[ "c#", "python", "perl", "mp3", "music" ]
Sorting music
171,277
<p>So over the years, I've bought music off iTunes, Urge, and Rhapsody and all these files are lying mixed with my non-DRM'd MP3 files that I've ripped off my CDs. Now some of these files have licenses that have expired, and some of them have valid licenses.</p> <p>I want to sort my music by various DRM/license restri...
6
2008-10-05T00:49:51Z
171,780
<p>I have come across this problem too and wrote a python function to fix it; my advice is to cut your losses with the DRM files and just move them out of whatever program you are using for playlists etc. The typical issue is m4p's mixed in with your mp3's and m4a's; whatever your mix this will move all drm'd files in...
1
2008-10-05T10:04:56Z
[ "c#", "python", "perl", "mp3", "music" ]
Sorting music
171,277
<p>So over the years, I've bought music off iTunes, Urge, and Rhapsody and all these files are lying mixed with my non-DRM'd MP3 files that I've ripped off my CDs. Now some of these files have licenses that have expired, and some of them have valid licenses.</p> <p>I want to sort my music by various DRM/license restri...
6
2008-10-05T00:49:51Z
171,933
<p>If you need to look at the ID3 tags in the MP3 files then there are a few modules on CPAN that may help u (for eg... <a href="http://search.cpan.org/dist/MP3-Find/" rel="nofollow">MP3::Find</a> <a href="http://search.cpan.org/dist/MP3-Tag/" rel="nofollow">MP3::Tag</a> )</p> <p>Also these articles may help....<...
5
2008-10-05T12:57:50Z
[ "c#", "python", "perl", "mp3", "music" ]
How would I implement a Python bit map?
171,512
<p>I wish to implement a 2d bit map class in Python. The class would have the following requirements:</p> <ol> <li><p>Allow the creating of arbitrarily sized 2d bitmaps. i.e. to create an 8 x 8 bitmap (8 bytes), something like:</p> <pre><code>bitmap = Bitmap(8,8) </code></pre></li> <li><p>provide an API to access the...
2
2008-10-05T04:51:11Z
171,611
<p>No need to create this yourself.</p> <p>Use the very good <a href="http://www.pythonware.com/products/pil/" rel="nofollow">Python Imaging Library</a> (PIL)</p>
3
2008-10-05T07:11:25Z
[ "python", "class" ]
How would I implement a Python bit map?
171,512
<p>I wish to implement a 2d bit map class in Python. The class would have the following requirements:</p> <ol> <li><p>Allow the creating of arbitrarily sized 2d bitmaps. i.e. to create an 8 x 8 bitmap (8 bytes), something like:</p> <pre><code>bitmap = Bitmap(8,8) </code></pre></li> <li><p>provide an API to access the...
2
2008-10-05T04:51:11Z
171,672
<p><a href="http://mentat.za.net/numpy/refguide/routines.bitwise.xhtml#bit-packing" rel="nofollow">Bit-Packing</a> numpy ( <a href="http://www.scipy.org/" rel="nofollow">SciPY</a> ) arrays does what you are looking for. The example shows 4x3 bit (Boolean) array packed into 4 8-bit bytes. <em>unpackbits</em> unpacks uin...
4
2008-10-05T08:16:06Z
[ "python", "class" ]
Formatting a list of text into columns
171,662
<p>I'm trying to output a list of string values into a 2 column format. The standard way of making a list of strings into "normal text" is by using the <strong>string.join</strong> method. However, it only takes 2 arguments so I can only make a single column using "\n". I thought trying to make a loop that would simply...
11
2008-10-05T08:09:52Z
171,686
<pre><code>data = [ ("1","2"),("3","4") ] print "\n".join(map("\t".join,data)) </code></pre> <p>Not as flexible as the ActiveState solution, but shorter :-)</p>
0
2008-10-05T08:22:22Z
[ "python", "string", "formatting" ]
Formatting a list of text into columns
171,662
<p>I'm trying to output a list of string values into a 2 column format. The standard way of making a list of strings into "normal text" is by using the <strong>string.join</strong> method. However, it only takes 2 arguments so I can only make a single column using "\n". I thought trying to make a loop that would simply...
11
2008-10-05T08:09:52Z
171,707
<p>Two columns, separated by tabs, joined into lines. Look in <em>itertools</em> for iterator equivalents, to achieve a space-efficient solution.</p> <pre><code>import string def fmtpairs(mylist): pairs = zip(mylist[::2],mylist[1::2]) return '\n'.join('\t'.join(i) for i in pairs) print fmtpairs(list(string.as...
10
2008-10-05T08:40:22Z
[ "python", "string", "formatting" ]
Formatting a list of text into columns
171,662
<p>I'm trying to output a list of string values into a 2 column format. The standard way of making a list of strings into "normal text" is by using the <strong>string.join</strong> method. However, it only takes 2 arguments so I can only make a single column using "\n". I thought trying to make a loop that would simply...
11
2008-10-05T08:09:52Z
173,823
<p>It's long-winded, so I'll break it into two parts.</p> <pre><code>def columns( skills_defs, cols=2 ): pairs = [ "\t".join(skills_defs[i:i+cols]) for i in range(0,len(skills_defs),cols) ] return "\n".join( pairs ) </code></pre> <p>It can, obviously, be done as a single loooong statement.</p> <p>This works ...
3
2008-10-06T10:33:04Z
[ "python", "string", "formatting" ]
Formatting a list of text into columns
171,662
<p>I'm trying to output a list of string values into a 2 column format. The standard way of making a list of strings into "normal text" is by using the <strong>string.join</strong> method. However, it only takes 2 arguments so I can only make a single column using "\n". I thought trying to make a loop that would simply...
11
2008-10-05T08:09:52Z
173,933
<p>The <code>format_columns</code> function should do what you want:</p> <pre><code>from __future__ import generators try: import itertools except ImportError: mymap, myzip= map, zip else: mymap, myzip= itertools.imap, itertools.izip def format_columns(string_list, columns, separator=" "): "Produce equal-width co...
0
2008-10-06T11:21:44Z
[ "python", "string", "formatting" ]
Formatting a list of text into columns
171,662
<p>I'm trying to output a list of string values into a 2 column format. The standard way of making a list of strings into "normal text" is by using the <strong>string.join</strong> method. However, it only takes 2 arguments so I can only make a single column using "\n". I thought trying to make a loop that would simply...
11
2008-10-05T08:09:52Z
12,721,559
<p>This may work</p> <pre><code>if len(skills_defs)%2: skills_defs.append("") for i in range(1, len( skills_defs), 2): print('{:&lt;60}{}'.format(skills_defs[i-1], skills_defs[i])) </code></pre> <p>See: <a href="http://docs.python.org/library/string.html#format-examples" rel="nofollow"> String format examples...
2
2012-10-04T06:34:10Z
[ "python", "string", "formatting" ]
Formatting a list of text into columns
171,662
<p>I'm trying to output a list of string values into a 2 column format. The standard way of making a list of strings into "normal text" is by using the <strong>string.join</strong> method. However, it only takes 2 arguments so I can only make a single column using "\n". I thought trying to make a loop that would simply...
11
2008-10-05T08:09:52Z
24,876,899
<p>Here is an extension of the solution provided by gimel, which allows to print equally spaced columns.</p> <pre><code>def fmtcols(mylist, cols): maxwidth = max(map(lambda x: len(x), mylist)) justifyList = map(lambda x: x.ljust(maxwidth), mylist) lines = (' '.join(justifyList[i:i+cols]) for ...
2
2014-07-22T00:12:19Z
[ "python", "string", "formatting" ]
Formatting a list of text into columns
171,662
<p>I'm trying to output a list of string values into a 2 column format. The standard way of making a list of strings into "normal text" is by using the <strong>string.join</strong> method. However, it only takes 2 arguments so I can only make a single column using "\n". I thought trying to make a loop that would simply...
11
2008-10-05T08:09:52Z
27,027,373
<p>I think many of these solutions are conflating two separate things into one.</p> <p>You want to:</p> <ol> <li>be able to force a string to be a certain width</li> <li>print a table</li> </ol> <p>Here's a really simple take on how to do this:</p> <pre><code>import sys skills_defs = ["ACM:Aircraft Mechanic", "BC:...
0
2014-11-19T21:30:54Z
[ "python", "string", "formatting" ]
Applying a common font scheme to multiple objects in wxPython
171,694
<p>Many times I will use the same font scheme for static text in a wxPython application. Currently I am making a <strong>SetFont()</strong> call for each static text object but that seems like a lot of unnecessary work. However, the wxPython demo and <em>wxPython In Action</em> book don't discuss this.</p> <p>Is there...
3
2008-10-05T08:27:57Z
171,702
<p>Maybe try subclassing the text object and in your class <code>__init__</code> method just call SetFont()? </p> <p>Or, do something like:</p> <pre><code>def f(C): x = C() x.SetFont(font) # where font is defined somewhere else return x </code></pre> <p>and then just decorate every text object you create with ...
1
2008-10-05T08:38:36Z
[ "python", "fonts", "wxpython" ]
Applying a common font scheme to multiple objects in wxPython
171,694
<p>Many times I will use the same font scheme for static text in a wxPython application. Currently I am making a <strong>SetFont()</strong> call for each static text object but that seems like a lot of unnecessary work. However, the wxPython demo and <em>wxPython In Action</em> book don't discuss this.</p> <p>Is there...
3
2008-10-05T08:27:57Z
182,923
<p>If all widgets have already been created, you can apply <code>SetFont</code> recursively, for example with the following function:</p> <pre><code>def changeFontInChildren(win, font): ''' Set font in given window and all its descendants. @type win: L{wx.Window} @type font: L{wx.Font} ''' try:...
0
2008-10-08T14:01:16Z
[ "python", "fonts", "wxpython" ]