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
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
4,751,601
<p>Based on Eyecue answer (Jun 10 '10 at 21:28): this class add head() and tail() method to file object.</p> <pre><code>class File(file): def head(self, lines_2find=1): self.seek(0) #Rewind file return [self.next() for x in xrange(lines_2find)] def tail(self, lines_2...
1
2011-01-20T19:29:37Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
5,638,389
<p>Several of these solutions have issues if the file doesn't end in \n or in ensuring the complete first line is read.</p> <pre><code>def tail(file, n=1, bs=1024): f = open(file) f.seek(-1,2) l = 1-f.read(1).count('\n') # If file doesn't end in \n, count it anyway. B = f.tell() while n &gt;= l and...
1
2011-04-12T16:14:42Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
6,813,975
<p>Simple and fast solution with mmap:</p> <pre><code>import mmap import os def tail(filename, n): """Returns last n lines from the filename. No exception handling""" size = os.path.getsize(filename) with open(filename, "rb") as f: # for Windows the mmap parameters are different fm = mmap....
11
2011-07-25T09:18:10Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
7,047,765
<p>S.Lott's answer above almost works for me but ends up giving me partial lines. It turns out that it corrupts data on block boundaries because data holds the read blocks in reversed order. When ''.join(data) is called, the blocks are in the wrong order. This fixes that.</p> <pre><code>def tail(f, window=20): """...
18
2011-08-13T00:43:38Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
10,175,048
<p>I found the Popen above to be the best solution. It's quick and dirty and it works For python 2.6 on Unix machine i used the following</p> <pre><code> def GetLastNLines(self, n, fileName): """ Name: Get LastNLines Description: Gets last n lines using Unix tail Output: ret...
4
2012-04-16T13:26:32Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
12,762,551
<p>There are some existing implementations of tail on pypi which you can install using pip: </p> <ul> <li>mtFileUtil</li> <li>multitail</li> <li>log4tailer</li> <li>...</li> </ul> <p>Depending on your situation, there may be advantages to using one of these existing tools.</p>
1
2012-10-06T18:23:37Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
13,790,289
<p>Here is my answer. Pure python. Using timeit it seems pretty fast. Tailing 100 lines of a log file that has 100,000 lines:</p> <pre><code>&gt;&gt;&gt; timeit.timeit('tail.tail(f, 100, 4098)', 'import tail; f = open("log.txt", "r");', number=10) 0.0014600753784179688 &gt;&gt;&gt; timeit.timeit('tail.tail(f, 100, 409...
10
2012-12-09T18:21:42Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
16,507,215
<p>I had to read a specific value from the last line of a file, and stumbled upon this thread. Rather than reinventing the wheel in Python, I ended up with a tiny shell script, saved as /usr/local/bin/get_last_netp:</p> <pre><code>#! /bin/bash tail -n1 /home/leif/projects/transfer/export.log | awk {'print $14'} </code...
1
2013-05-12T12:11:01Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
16,507,435
<p>Not the first example using a deque, but a simpler one. This one is general: it works on any iterable object, not just a file.</p> <pre><code>#!/usr/bin/env python import sys import collections def tail(iterable, N): deq = collections.deque() for thing in iterable: if len(deq) &gt;= N: ...
0
2013-05-12T12:38:55Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
23,290,416
<pre><code>This is my version of tailf import sys, time, os filename = 'path to file' try: with open(filename) as f: size = os.path.getsize(filename) if size &lt; 1024: s = size else: s = 999 f.seek(-s, 2) l = f.read() print l while ...
0
2014-04-25T10:24:36Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
25,450,971
<pre><code>import time attemps = 600 wait_sec = 5 fname = "YOUR_PATH" with open(fname, "r") as f: where = f.tell() for i in range(attemps): line = f.readline() if not line: time.sleep(wait_sec) f.seek(where) else: print line, # already has newline </...
0
2014-08-22T15:52:53Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
34,029,605
<p>Posting an answer at the behest of commenters on <a href="/a/33811809/364696">my answer to a similar question</a> where the same technique was used to mutate the last line of a file, not just get it.</p> <p>For a file of significant size, <a href="https://docs.python.org/3/library/mmap.html" rel="nofollow"><code>mm...
2
2015-12-01T20:34:50Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
37,176,501
<p>Although this isn't really on the efficient side with big files, this code is pretty straight-forward:<br></p> <ol> <li>It reads the file object, <code>f</code>.</li> <li>It splits the string returned using newlines, <code>\n</code>.</li> <li><p>It gets the array lists last indexes, using the negative sign to stand...
0
2016-05-12T02:57:48Z
[ "python", "file", "file-io", "tail", "logfiles" ]
Get last n lines of a file with Python, similar to tail
136,168
<p>I'm writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest item on the bottom.</p> <p>So I need a <code>tail()</code> method that can read <code>n</code> lines from the bottom and supports an offset. W...
122
2008-09-25T21:11:11Z
37,903,261
<p>Here is a pretty simple implementation:</p> <pre><code>with open('/etc/passwd', 'r') as f: try: f.seek(0,2) s = '' while s.count('\n') &lt; 11: cur = f.tell() f.seek((cur - 10)) s = f.read(10) + s f.seek((cur - 10)) print s except Exception as e: f.readlines() </code>...
0
2016-06-19T01:54:08Z
[ "python", "file", "file-io", "tail", "logfiles" ]
How do you manage your custom modules?
136,207
<p>I write tons of python scripts, and I find myself reusing lots code that I've written for other projects. My solution has been to make sure the code is separated into logical modules/packages (this one's a given). I then make them setuptools-aware and publish them on PyPI. This allows my other scripts to always have...
10
2008-09-25T21:18:09Z
137,052
<p>I have been doing the same thing. Extract common functionality, pretty the code up with extra documentation and unit tests/ doctests, create an easy_install setup.py, and then release on PyPi. Recently, I created a single <a href="http://code.google.com/p/7oars/" rel="nofollow">Google Code site</a> where I manage ...
1
2008-09-26T00:13:02Z
[ "python", "code-reuse" ]
How do you manage your custom modules?
136,207
<p>I write tons of python scripts, and I find myself reusing lots code that I've written for other projects. My solution has been to make sure the code is separated into logical modules/packages (this one's a given). I then make them setuptools-aware and publish them on PyPI. This allows my other scripts to always have...
10
2008-09-25T21:18:09Z
137,291
<p>What kind of modules are we talking about here? If you're planning on distributing your projects to other python developers, setuptools is great. But it's usually not a very good way to distribute apps to end users. Your best bet in the latter case is to tailor your packaging to the platforms you're distributing ...
1
2008-09-26T01:39:58Z
[ "python", "code-reuse" ]
Key Presses in Python
136,734
<p>Is it possible to make it appear to a system that a key was pressed, for example I need to make <kbd>A</kbd> key be pressed thousands of times, and it is much to time consuming to do it manually, I would like to write something to do it for me, and the only thing I know well enough is Python.</p> <p>A better way t...
13
2008-09-25T22:58:01Z
136,741
<p>It's probably <em>possible</em> - but where do you want the key presses to go? To another application? That would probably be a case of understanding your platform's windowing toolkit and sending the right messages to the right window.</p> <p>Clarify your requirements, and I'm sure we can help out.</p>
0
2008-09-25T22:59:55Z
[ "python", "keypress" ]
Key Presses in Python
136,734
<p>Is it possible to make it appear to a system that a key was pressed, for example I need to make <kbd>A</kbd> key be pressed thousands of times, and it is much to time consuming to do it manually, I would like to write something to do it for me, and the only thing I know well enough is Python.</p> <p>A better way t...
13
2008-09-25T22:58:01Z
136,759
<p><a href="http://www.autohotkey.com/" rel="nofollow">AutoHotKey</a> is perfect for this kind of tasks (keyboard automation / remapping)</p> <p>Script to send "A" 100 times:</p> <pre><code>Send {A 100} </code></pre> <p>That's all</p> <p><strong>EDIT</strong>: to send the keys to an specific application:</p> <pre>...
5
2008-09-25T23:03:55Z
[ "python", "keypress" ]
Key Presses in Python
136,734
<p>Is it possible to make it appear to a system that a key was pressed, for example I need to make <kbd>A</kbd> key be pressed thousands of times, and it is much to time consuming to do it manually, I would like to write something to do it for me, and the only thing I know well enough is Python.</p> <p>A better way t...
13
2008-09-25T22:58:01Z
136,780
<p>Install the <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">pywin32</a> extensions. Then you can do the following:</p> <pre><code>import win32com.client as comclt wsh= comclt.Dispatch("WScript.Shell") wsh.AppActivate("Notepad") # select another application wsh.SendKeys("a") # send the keys you wan...
17
2008-09-25T23:09:39Z
[ "python", "keypress" ]
Key Presses in Python
136,734
<p>Is it possible to make it appear to a system that a key was pressed, for example I need to make <kbd>A</kbd> key be pressed thousands of times, and it is much to time consuming to do it manually, I would like to write something to do it for me, and the only thing I know well enough is Python.</p> <p>A better way t...
13
2008-09-25T22:58:01Z
136,786
<p>If you're platform is Windows, I wouldn't actually recommend Python. Instead, look into <a href="http://www.autohotkey.com/" rel="nofollow">Autohotkey</a>. Trust me, I love Python, but in this circumstance a macro program is the ideal tool for the job. Autohotkey's scripting is only decent (in my opinion), but th...
-1
2008-09-25T23:12:13Z
[ "python", "keypress" ]
Key Presses in Python
136,734
<p>Is it possible to make it appear to a system that a key was pressed, for example I need to make <kbd>A</kbd> key be pressed thousands of times, and it is much to time consuming to do it manually, I would like to write something to do it for me, and the only thing I know well enough is Python.</p> <p>A better way t...
13
2008-09-25T22:58:01Z
1,282,600
<p>Alternative way to set prefer window into foreground before send key press event.</p> <pre><code>hwnd = win32gui.FindWindowEx(0,0,0, "App title") win32gui.SetForegroundWindow(hwnd) </code></pre>
0
2009-08-15T19:16:47Z
[ "python", "keypress" ]
Key Presses in Python
136,734
<p>Is it possible to make it appear to a system that a key was pressed, for example I need to make <kbd>A</kbd> key be pressed thousands of times, and it is much to time consuming to do it manually, I would like to write something to do it for me, and the only thing I know well enough is Python.</p> <p>A better way t...
13
2008-09-25T22:58:01Z
33,249,229
<p>You could also use PyAutoGui to send a virtual key presses.</p> <p>Here's the documentation: <a href="https://pyautogui.readthedocs.org/en/latest/" rel="nofollow">https://pyautogui.readthedocs.org/en/latest/</a></p> <pre><code>import pyautogui pyautogui.keypress('Any key combination') </code></pre> <p>You can a...
3
2015-10-21T00:57:43Z
[ "python", "keypress" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
136,749
<p><a href="http://docs.python.org/library/pydoc.html" rel="nofollow">pydoc</a>?</p> <p>I'm not sure if you're looking for something more sophisticated, but it does the trick.</p>
5
2008-09-25T23:00:48Z
[ "python", "reference", "documentation", "python-2.x" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
136,758
<p><a href="https://docs.python.org/py-modindex.html" rel="nofollow">Here</a> is a list of all the modules in Python, not sure if that's what you're really after.</p>
1
2008-09-25T23:03:33Z
[ "python", "reference", "documentation", "python-2.x" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
136,760
<p>If you're working on Windows <a href="http://www.activestate.com/Products/activepython/index.mhtml" rel="nofollow">ActiveState Python</a> comes with the documentation, including the library reference in a searchable help file.</p>
0
2008-09-25T23:04:21Z
[ "python", "reference", "documentation", "python-2.x" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
136,783
<p>The standard python library is fairly well documented. Try jumping into python and importing a module say "os" and running:</p> <pre><code>import os help(os) </code></pre> <p>This reads the doc strings on each of the items in the module and displays it. This is exactly what pydoc will do too.</p> <p>EDIT: <a h...
2
2008-09-25T23:11:13Z
[ "python", "reference", "documentation", "python-2.x" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
136,929
<p>I've downloaded Python 2.5 from Python.org and It does not contains pydoc.</p> <pre><code>Directorio de C:\Python25 9/23/2008 10:45 PM &lt;DIR&gt; . 9/23/2008 10:45 PM &lt;DIR&gt; .. 9/23/2008 10:45 PM &lt;DIR&gt; DLLs 9/23/2008 10:45 PM &lt;DIR&gt; Doc 9/23/2008...
1
2008-09-25T23:42:37Z
[ "python", "reference", "documentation", "python-2.x" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
137,335
<blockquote> <p>BTW I know that I would eventually will read this:</p> <p><a href="http://docs.python.org/lib/lib.html" rel="nofollow">http://docs.python.org/lib/lib.html</a></p> <p>But, well, I think it is not today.</p> </blockquote> <p>I suggest that you're making a mistake. The lib doc has "the clas...
1
2008-09-26T01:56:12Z
[ "python", "reference", "documentation", "python-2.x" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
138,121
<p>It doesn't directly answer your question (so I'll probably be downgraded), but you may be interested in <a href="http://www.jython.org" rel="nofollow">Jython</a>.</p> <blockquote> <p>Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly i...
0
2008-09-26T07:16:09Z
[ "python", "reference", "documentation", "python-2.x" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
138,240
<p>You can set the <em>environment variable</em> <strong>PYTHONDOCS</strong> to point to where the python documentation is installed.</p> <p>On my system, it's in <em>/usr/share/doc/python2.5</em></p> <p>So you can define this variable in your <em>shell profile</em> or somewhere else depending on your system:</p> <b...
1
2008-09-26T08:07:29Z
[ "python", "reference", "documentation", "python-2.x" ]
Python language API
136,739
<p>I'm starting with Python coming from java. </p> <p>I was wondering if there exists something similar to JavaDoc API where I can find the class, its methods and and example of how to use it.</p> <p>I've found very helpul to use <em>help( thing )</em> from the Python ( command line ) </p> <p>I have found this also:...
6
2008-09-25T22:59:10Z
138,317
<p>Also try</p> <pre><code>pydoc -p 11111 </code></pre> <p>Then type in web browser <a href="http://localhost:11111" rel="nofollow">http://localhost:11111</a></p> <p>EDIT: of course you can use any other value for port number instead of 11111</p>
0
2008-09-26T08:34:18Z
[ "python", "reference", "documentation", "python-2.x" ]
How do you make Python / PostgreSQL faster?
136,789
<p>Right now I have a log parser reading through 515mb of plain-text files (a file for each day over the past 4 years). My code currently stands as this: <a href="http://gist.github.com/12978" rel="nofollow">http://gist.github.com/12978</a>. I've used psyco (as seen in the code) and I'm also compiling it and using the ...
4
2008-09-25T23:12:50Z
136,870
<p>Use bind variables instead of literal values in the sql statements and create a cursor for each unique sql statement so that the statement does not need to be reparsed the next time it is used. From the python db api doc:</p> <blockquote> <p>Prepare and execute a database operation (query or command). Para...
3
2008-09-25T23:29:56Z
[ "python", "postgresql" ]
How do you make Python / PostgreSQL faster?
136,789
<p>Right now I have a log parser reading through 515mb of plain-text files (a file for each day over the past 4 years). My code currently stands as this: <a href="http://gist.github.com/12978" rel="nofollow">http://gist.github.com/12978</a>. I've used psyco (as seen in the code) and I'm also compiling it and using the ...
4
2008-09-25T23:12:50Z
137,002
<p>In the for loop, you're inserting into the 'chats' table repeatedly, so you only need a single sql statement with bind variables, to be executed with different values. So you could put this before the for loop:</p> <pre><code>insert_statement=""" INSERT INTO chats(person_id, message_type, created_at, channel) ...
3
2008-09-25T23:59:39Z
[ "python", "postgresql" ]
How do you make Python / PostgreSQL faster?
136,789
<p>Right now I have a log parser reading through 515mb of plain-text files (a file for each day over the past 4 years). My code currently stands as this: <a href="http://gist.github.com/12978" rel="nofollow">http://gist.github.com/12978</a>. I've used psyco (as seen in the code) and I'm also compiling it and using the ...
4
2008-09-25T23:12:50Z
137,076
<p>As Mark suggested, use binding variables. The database only has to prepare each statement once, then "fill in the blanks" for each execution. As a nice side effect, it will automatically take care of string-quoting issues (which your program isn't handling).</p> <p>Turn transactions on (if they aren't already) and ...
2
2008-09-26T00:20:36Z
[ "python", "postgresql" ]
How do you make Python / PostgreSQL faster?
136,789
<p>Right now I have a log parser reading through 515mb of plain-text files (a file for each day over the past 4 years). My code currently stands as this: <a href="http://gist.github.com/12978" rel="nofollow">http://gist.github.com/12978</a>. I've used psyco (as seen in the code) and I'm also compiling it and using the ...
4
2008-09-25T23:12:50Z
137,096
<p>Additionally to the many fine suggestions @Mark Roddy has given, do the following:</p> <ul> <li>don't use <code>readlines</code>, you can iterate over file objects</li> <li>try to use <code>executemany</code> rather than <code>execute</code>: try to do batch inserts rather single inserts, this tends to be faster be...
1
2008-09-26T00:26:21Z
[ "python", "postgresql" ]
How do you make Python / PostgreSQL faster?
136,789
<p>Right now I have a log parser reading through 515mb of plain-text files (a file for each day over the past 4 years). My code currently stands as this: <a href="http://gist.github.com/12978" rel="nofollow">http://gist.github.com/12978</a>. I've used psyco (as seen in the code) and I'm also compiling it and using the ...
4
2008-09-25T23:12:50Z
137,320
<p>Don't waste time profiling. The time is always in the database operations. Do as few as possible. Just the minimum number of inserts.</p> <p>Three Things.</p> <p>One. Don't SELECT over and over again to conform the Date, Hostname and Person dimensions. Fetch all the data ONCE into a Python dictionary and use ...
5
2008-09-26T01:50:23Z
[ "python", "postgresql" ]
How can I perform a HEAD request with the mechanize library?
137,580
<p>I know how to do a HEAD request with httplib, but I have to use mechanize for this site. </p> <p>Essentially, what I need to do is grab a value from the header (filename) without actually downloading the file.</p> <p>Any suggestions how I could accomplish this?</p>
3
2008-09-26T03:20:19Z
137,624
<p>Mechanize itself only sends GETs and POSTs, but you can easily extend the Request class to send HEAD. Example:</p> <pre><code>import mechanize class HeadRequest(mechanize.Request): def get_method(self): return "HEAD" request = HeadRequest("http://www.example.com/") response = mechanize.urlopen(request...
8
2008-09-26T03:37:33Z
[ "python", "http-headers", "mechanize" ]
How can I perform a HEAD request with the mechanize library?
137,580
<p>I know how to do a HEAD request with httplib, but I have to use mechanize for this site. </p> <p>Essentially, what I need to do is grab a value from the header (filename) without actually downloading the file.</p> <p>Any suggestions how I could accomplish this?</p>
3
2008-09-26T03:20:19Z
12,680,334
<p>In mechanize there is no need to do HeadRequest class etc.</p> <p>Simply</p> <pre><code> import mechanize br = mechanize.Browser() r = br.open("http://www.example.com/") print r.info() </code></pre> <p>That's all.</p>
0
2012-10-01T20:11:08Z
[ "python", "http-headers", "mechanize" ]
Get Bound Event Handler in Tkinter
138,029
<p>After a bind a method to an event of a Tkinter element is there a way to get the method back?</p> <pre><code>&gt;&gt;&gt; root = Tkinter.Tk() &gt;&gt;&gt; frame = Tkinter.Frame(root, width=100, height=100) &gt;&gt;&gt; frame.bind('&lt;Button-1&gt;', lambda e: pprint('Click')) # function needed &gt;&gt;&gt; frame.pa...
1
2008-09-26T06:26:17Z
138,039
<p>Doesn't appear to be... why not just save it yourself if you're going to need it, or use a non-anonymous function?</p> <p>Also, your code doesn't work as written: <code>lambda</code> functions can only contain expressions, not statements, so <code>print</code> is a no-go (this will change in Python 3.0 when <code>p...
0
2008-09-26T06:31:03Z
[ "python", "user-interface", "events", "tkinter" ]
Get Bound Event Handler in Tkinter
138,029
<p>After a bind a method to an event of a Tkinter element is there a way to get the method back?</p> <pre><code>&gt;&gt;&gt; root = Tkinter.Tk() &gt;&gt;&gt; frame = Tkinter.Frame(root, width=100, height=100) &gt;&gt;&gt; frame.bind('&lt;Button-1&gt;', lambda e: pprint('Click')) # function needed &gt;&gt;&gt; frame.pa...
1
2008-09-26T06:26:17Z
138,258
<p>The associated call to do that for the tk C API would be <a href="http://linux.about.com/library/cmd/blcmdl3_Tcl_GetCommandInfo.htm" rel="nofollow">Get_GetCommandInfo</a> which</p> <blockquote> <p>places information about the command in the Tcl_CmdInfo structure pointed to by infoPtr</p> </blockquote> <p>How...
2
2008-09-26T08:15:25Z
[ "python", "user-interface", "events", "tkinter" ]
Get Bound Event Handler in Tkinter
138,029
<p>After a bind a method to an event of a Tkinter element is there a way to get the method back?</p> <pre><code>&gt;&gt;&gt; root = Tkinter.Tk() &gt;&gt;&gt; frame = Tkinter.Frame(root, width=100, height=100) &gt;&gt;&gt; frame.bind('&lt;Button-1&gt;', lambda e: pprint('Click')) # function needed &gt;&gt;&gt; frame.pa...
1
2008-09-26T06:26:17Z
226,141
<p>The standard way to do this in Tcl/Tk is trivial: you use the same bind command but without the final argument. </p> <pre><code>bind .b &lt;Button-1&gt; doSomething puts "the function is [bind .b &lt;Button-1&gt;]" =&gt; the function is doSomething </code></pre> <p>You can do something similar with Tkinter but the...
3
2008-10-22T15:01:35Z
[ "python", "user-interface", "events", "tkinter" ]
Is there something like Python's getattr() in C#?
138,045
<p>Is there something like <a href="http://effbot.org/zone/python-getattr.htm">Python's getattr()</a> in C#? I would like to create a window by reading a list which contains the names of controls to put on the window.</p>
12
2008-09-26T06:35:30Z
138,053
<p>Use reflection for this.</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.type.getproperty.aspx" rel="nofollow"><code>Type.GetProperty()</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.type.getproperty.aspx" rel="nofollow"><code>Type.GetProperties()</code></a> each return <a hre...
3
2008-09-26T06:39:43Z
[ "c#", "python", "user-interface" ]
Is there something like Python's getattr() in C#?
138,045
<p>Is there something like <a href="http://effbot.org/zone/python-getattr.htm">Python's getattr()</a> in C#? I would like to create a window by reading a list which contains the names of controls to put on the window.</p>
12
2008-09-26T06:35:30Z
138,054
<p>Yes, you can do this...</p> <pre><code>typeof(YourObjectType).GetProperty("PropertyName").GetValue(instanceObjectToGetPropFrom, null); </code></pre>
1
2008-09-26T06:40:58Z
[ "c#", "python", "user-interface" ]
Is there something like Python's getattr() in C#?
138,045
<p>Is there something like <a href="http://effbot.org/zone/python-getattr.htm">Python's getattr()</a> in C#? I would like to create a window by reading a list which contains the names of controls to put on the window.</p>
12
2008-09-26T06:35:30Z
138,063
<p>There's the System.Reflection.PropertyInfo class that can be created using object.GetType().GetProperties(). That can be used to probe an object's properties using strings. (Similar methods exist for object methods, fields, etc.)</p> <p>I don't think that will help you accomplish your goals though. You should proba...
0
2008-09-26T06:46:51Z
[ "c#", "python", "user-interface" ]
Is there something like Python's getattr() in C#?
138,045
<p>Is there something like <a href="http://effbot.org/zone/python-getattr.htm">Python's getattr()</a> in C#? I would like to create a window by reading a list which contains the names of controls to put on the window.</p>
12
2008-09-26T06:35:30Z
138,079
<p>There is also <a href="http://msdn.microsoft.com/en-us/library/66btctbe.aspx">Type.InvokeMember</a>.</p> <pre><code>public static class ReflectionExt { public static object GetAttr(this object obj, string name) { Type type = obj.GetType(); BindingFlags flags = BindingFlags.Instance | ...
9
2008-09-26T06:57:23Z
[ "c#", "python", "user-interface" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
138,254
<p>Image manipulation is a complex topic, and it's best if you <em>do</em> use a library. I can recommend <a href="http://newcenturycomputers.net/projects/gdmodule.html" rel="nofollow">gdmodule</a> which provides easy access to many different image formats from within Python.</p>
1
2008-09-26T08:14:23Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
138,260
<p>It's probably best to use the <a href="http://www.pythonware.com/products/pil/">Python Image Library</a> to do this which I'm afraid is a separate download.</p> <p>The easiest way to do what you want is via the <a href="http://effbot.org/imagingbook/image.htm">load() method on the Image object</a> which returns a p...
108
2008-09-26T08:15:56Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
138,300
<p>There's a really good article on wiki.wxpython.org entitled <a href="http://wiki.wxpython.org/index.cgi/WorkingWithImages" rel="nofollow">Working With Images</a>. The article mentions the possiblity of using wxWidgets (wxImage), PIL or PythonMagick. Personally, I've used PIL and wxWidgets and both make image manipul...
3
2008-09-26T08:28:57Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
139,070
<p><strong>PyPNG - lightweight PNG decoder/encoder</strong></p> <p>Although the question hints at JPG, I hope my answer will be useful to some people.</p> <p>Here's how to read and write PNG pixels using <a href="https://pypi.python.org/pypi/pypng/0.0.18" rel="nofollow">PyPNG module</a>:</p> <pre><code>import png, a...
20
2008-09-26T12:20:38Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
663,861
<p>You can use <a href="http://www.pygame.org" rel="nofollow">pygame</a>'s surfarray module. This module has a 3d pixel array returning method called pixels3d(surface). I've shown usage below:</p> <pre><code>from pygame import surfarray, image, display import pygame import numpy #important to import pygame.init() ima...
3
2009-03-19T20:14:21Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
5,365,853
<p>As Dave Webb said.</p> <p>Here is my working code snippet printing the pixel colours from an image:</p> <pre><code>import os, sys import Image im = Image.open("image.jpg") x = 3 y = 4 pix = im.load() print pix[x,y] </code></pre>
8
2011-03-20T00:10:43Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
22,877,878
<p>install PIL using the command "sudo apt-get install python-imaging" and run the following program. It will print RGB values of the image. If the image is large redirect the output to a file using '>' later open the file to see RGB values</p> <pre><code>import PIL import Image FILENAME='fn.gif' #image can be in gif ...
2
2014-04-05T07:23:04Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
27,370,477
<p>You could use Tkinter module, which is already installed with Python.(I use Python 2.7 and OS X)</p> <p>Here is the code from <a href="http://tkinter.unpythonic.net/wiki/PhotoImage" rel="nofollow">http://tkinter.unpythonic.net/wiki/PhotoImage</a>.</p> <p>Set RGB values.</p> <pre><code>from Tkinter import * root ...
1
2014-12-09T02:22:25Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
27,960,627
<p>Using <a href="http://python-pillow.github.io/" rel="nofollow">Pillow</a> (which works with Python 3.X as well as Python 2.7+), you can do the following:</p> <pre><code>from PIL import Image im = Image.open('image.jpg', 'r') width, height = im.size pixel_values = list(im.getdata()) </code></pre> <p>Now you have al...
4
2015-01-15T09:46:10Z
[ "python", "graphics", "rgb" ]
How can I read the RGB value of a given pixel in Python?
138,250
<p>If I open an image with <code>open("image.jpg")</code>, how can I get the RGB values of a pixel, if I have the coordinates of the pixel?</p> <p>Then how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?</p> <p>It would be so much better if I didn't have to downl...
63
2008-09-26T08:10:50Z
33,630,650
<pre><code>photo = Image.open('IN.jpg') #your image photo = photo.convert('RGB') width = photo.size[0] #define W and H height = photo.size[1] for y in range(0, height): #each pixel has coordinates row = "" for x in range(0, width): RGB = photo.getpixel((x,y)) R,G,B = RGB #now you can use the...
2
2015-11-10T13:02:32Z
[ "python", "graphics", "rgb" ]
Dynamic radio button creation
138,353
<p>In wxPython, if I create a list of radio buttons and place the list initially, is it possible to change the contents in that list later?</p> <p>For example, I have a panel that uses a boxSizer to place the widgets initially. One of those widgets is a list of radio buttons (I have also tried a normal radiobox). I wo...
1
2008-09-26T08:54:37Z
138,479
<p>Two possible solutions</p> <ol> <li>Rebuild the sizer with the radio widgets each time you have to make a change</li> <li>Hold the radio button widgets in a list, and call SetLabel each time you have to change their labels.</li> </ol>
0
2008-09-26T09:34:03Z
[ "python", "user-interface", "layout", "wxpython" ]
Dynamic radio button creation
138,353
<p>In wxPython, if I create a list of radio buttons and place the list initially, is it possible to change the contents in that list later?</p> <p>For example, I have a panel that uses a boxSizer to place the widgets initially. One of those widgets is a list of radio buttons (I have also tried a normal radiobox). I wo...
1
2008-09-26T08:54:37Z
139,009
<p>To make new list elements appear in correct places, you have to re-layout the grid after adding new elements to it. For example, to add a few new items, you could call:</p> <pre><code>def addNewSkills(self, newSkillList): '''newSkillList is a list of skill names you want to add''' for skillName in newSkillL...
1
2008-09-26T12:06:40Z
[ "python", "user-interface", "layout", "wxpython" ]
Dynamic radio button creation
138,353
<p>In wxPython, if I create a list of radio buttons and place the list initially, is it possible to change the contents in that list later?</p> <p>For example, I have a panel that uses a boxSizer to place the widgets initially. One of those widgets is a list of radio buttons (I have also tried a normal radiobox). I wo...
1
2008-09-26T08:54:37Z
145,580
<p>I was able to fix it by using the info DzinX provided, with some modification.</p> <p>It appears that posting the radio buttons box first "locked in" the box to the sizer. If I tried to add a new box, I would get an error message stating that I was trying to add the widget to the same sizer twice.</p> <p>By simply...
0
2008-09-28T09:47:15Z
[ "python", "user-interface", "layout", "wxpython" ]
Pure Python XSLT library
138,502
<p>Is there an XSLT library that is pure Python?</p> <p>Installing libxml2+libxslt or any similar C libraries is a problem on some of the platforms I need to support.</p> <p>I really only need basic XSLT support, and speed is not a major issue.</p>
19
2008-09-26T09:43:43Z
138,545
<p>Have you looked at <a href="http://4suite.org/index.xhtml" rel="nofollow">4suite</a>?</p>
1
2008-09-26T09:58:28Z
[ "python", "xml", "xslt" ]
Pure Python XSLT library
138,502
<p>Is there an XSLT library that is pure Python?</p> <p>Installing libxml2+libxslt or any similar C libraries is a problem on some of the platforms I need to support.</p> <p>I really only need basic XSLT support, and speed is not a major issue.</p>
19
2008-09-26T09:43:43Z
141,084
<p>If you only need <em>basic</em> support, and your XML isn't too crazy, consider removing the XSLT element from the equation and just using a DOM/SAX parser.</p> <p>Here's some info from the <a href="http://wiki.python.org/" rel="nofollow">PythonInfo Wiki</a>:</p> <blockquote> <p>[DOM] sucks up an entire XML file...
1
2008-09-26T18:18:34Z
[ "python", "xml", "xslt" ]
Pure Python XSLT library
138,502
<p>Is there an XSLT library that is pure Python?</p> <p>Installing libxml2+libxslt or any similar C libraries is a problem on some of the platforms I need to support.</p> <p>I really only need basic XSLT support, and speed is not a major issue.</p>
19
2008-09-26T09:43:43Z
592,466
<p>Unfortunately there are no pure-python XSLT processors at the moment. If you need something that is more platform independent, you may want to use a Java-based XSLT processor like <a href="http://saxon.sourceforge.net/">Saxon</a>. 4Suite is working on a pure-python XPath parser, but it doesn't look like a pure XSLT ...
9
2009-02-26T21:13:59Z
[ "python", "xml", "xslt" ]
Pure Python XSLT library
138,502
<p>Is there an XSLT library that is pure Python?</p> <p>Installing libxml2+libxslt or any similar C libraries is a problem on some of the platforms I need to support.</p> <p>I really only need basic XSLT support, and speed is not a major issue.</p>
19
2008-09-26T09:43:43Z
1,832,692
<p>I don't think you can do it in cpython: there are no pure python XSLT implementations.</p> <p>But you can trivially do it in jython, using the inbuilt XSLT APIs of the JVM. I wrote a blog post for the specific case of doing it on Google AppEngine, but the code given should work under jython in anyn circumstances.</...
6
2009-12-02T12:47:18Z
[ "python", "xml", "xslt" ]
Pure Python XSLT library
138,502
<p>Is there an XSLT library that is pure Python?</p> <p>Installing libxml2+libxslt or any similar C libraries is a problem on some of the platforms I need to support.</p> <p>I really only need basic XSLT support, and speed is not a major issue.</p>
19
2008-09-26T09:43:43Z
14,650,028
<p>There's also <a href="http://lxml.de/" rel="nofollow">http://lxml.de/</a> </p> <blockquote> <p>"lxml is the most feature-rich and easy-to-use library for processing XML and HTML in the Python language."</p> </blockquote>
1
2013-02-01T15:51:58Z
[ "python", "xml", "xslt" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
138,539
<p>This might seem reasonable at first glance, however there are a lot of ordinary things in Python that aren't directly mappable to to a C representation without carrying over a lot of the Python runtime support. For example, duck typing comes to mind. Many functions in Python that read input can take a file <em>or fi...
6
2008-09-26T09:56:08Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
138,548
<p><a href="http://psyco.sourceforge.net/" rel="nofollow">Psyco</a> is a kind of just-in-time (JIT) compiler: dynamic compiler for Python, runs code 2-100 times faster, but it needs much memory.</p> <p>In short: it run your existing Python software much faster, with no change in your source but it doesn't compile to o...
2
2008-09-26T09:59:12Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
138,553
<p>Try <a href="http://shed-skin.blogspot.com/">ShedSkin</a> Python-to-C++ compiler, but it is far from perfect. Also there is Psyco - Python JIT if only speedup is needed. But IMHO this is not worth the effort. For speed-critical parts of code best solution would be to write them as C/C++ extensions. </p>
15
2008-09-26T10:00:15Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
138,554
<p>Jython has a compiler targeting JVM bytecode. The bytecode is fully dynamic, just like the Python language itself! Very cool. (Yes, as Greg Hewgill's answer alludes, the bytecode does use the Jython runtime, and so the Jython jar file must be distributed with your app.)</p>
2
2008-09-26T10:00:16Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
138,582
<p><a href="http://codespeak.net/pypy/dist/pypy/doc/home.html">PyPy</a> is a project to reimplement Python in Python, using compilation to native code as one of the implementation strategies (others being a VM with JIT, using JVM, etc.). Their compiled C versions run slower than CPython on average but much faster for s...
11
2008-09-26T10:06:06Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
138,585
<p>As @Greg Hewgill says it, there are good reasons why this is not always possible. However, certain kinds of code (like very algorithmic code) can be turned into "real" machine code. </p> <p>There are several options:</p> <ul> <li>Use <a href="http://psyco.sourceforge.net/">Psyco</a>, which emits machine code dynam...
41
2008-09-26T10:06:43Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
138,586
<p><a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/" rel="nofollow">Pyrex</a> is a subset of the Python language that compiles to C, done by the guy that first built <a href="http://www.secnetix.de/olli/Python/list_comprehensions.hawk" rel="nofollow">list comprehensions</a> for Python. It was mainly ...
8
2008-09-26T10:06:46Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
138,605
<p>The answer is "Yes, it is possible". You could take Python code and attempt to compile it into the equivalent C code using the CPython API. In fact, there used to be a Python2C project that did just that, but I haven't heard about it in many years (back in the Python 1.5 days is when I last saw it.)</p> <p>You coul...
2
2008-09-26T10:14:09Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
11,415,005
<p>py2c ( <a href="http://code.google.com/p/py2c">http://code.google.com/p/py2c</a>) can convert python code to c/c++ I am the solo developer of py2c.</p>
14
2012-07-10T14:00:44Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
22,952,452
<p><a href="http://www.nuitka.net/">Nuitka</a> is a Python to C++ compiler that links against libpython. It appears to be a relatively new project. The author claims a <a href="http://www.nuitka.net/pages/overview.html">speed improvement</a> over CPython on the pystone benchmark. </p>
6
2014-04-09T03:52:37Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
23,596,973
<p>This doesn't compile Python to machine code. But allows to create a shared library to call Python code.</p> <p>If what you are looking for is an easy way to run Python code from C without relying on execp stuff. You could generate a shared library from python code wrapped with a few calls to <a href="https://docs.p...
2
2014-05-11T19:29:38Z
[ "python", "c", "linker", "compilation" ]
Is it feasible to compile Python to machine code?
138,521
<p>How feasible would it be to compile Python (possibly via an intermediate C representation) into machine code?</p> <p>Presumably it would need to link to a Python runtime library, and any parts of the Python standard library which were Python themselves would need to be compiled (and linked in) too.</p> <p>Also, yo...
93
2008-09-26T09:51:51Z
32,293,870
<p>Some extra references:</p> <ul> <li><p><a href="https://github.com/dropbox/pyston" rel="nofollow">https://github.com/dropbox/pyston</a> is a JIT compiler for Python developped by Dropbox</p></li> <li><p><a href="http://pythonhosted.org/pythran" rel="nofollow">http://pythonhosted.org/pythran</a> is a compile-time py...
3
2015-08-30T06:27:35Z
[ "python", "c", "linker", "compilation" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
138,594
<p>Not with plain regexp. Regular expressions have no "memory" so they cannot distinguish between being "inside" or "outside" quotes. </p> <p>You need something more powerful, for example using <a href="http://gema.sourceforge.net" rel="nofollow">gema</a> it would be straighforward:</p> <pre><code>'&lt;repl&gt;'=$0 r...
1
2008-09-26T10:08:42Z
[ "c#", "python", "regex", "language-agnostic" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
138,598
<p>Sorry to break your hopes, but you need a push-down automata to do that. There is more info here: <a href="http://en.wikipedia.org/wiki/Pushdown_automaton" rel="nofollow">Pushdown Automaton</a></p> <p>In short, Regular expressions, which are finite state machines can only read and has no memory while pushdown autom...
0
2008-09-26T10:10:00Z
[ "c#", "python", "regex", "language-agnostic" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
138,615
<p>I was able to do this with Python:</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.sub(r"x(?=[^']*'([^']|'[^']*')*$)", "P", "axbx'cxdxe'fxgh'ixj'k") "axbx'cPdPe'fxgh'iPj'k" </code></pre> <p>What this does is use the non-capturing match (?=...) to check that the character x is within a quoted string. It looks...
8
2008-09-26T10:18:50Z
[ "c#", "python", "regex", "language-agnostic" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
138,620
<p>Similar discussion about balanced text replaces: <a href="http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns#133771">http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns#133771</a></p> <p>Although you can try this in Vim, ...
1
2008-09-26T10:19:31Z
[ "c#", "python", "regex", "language-agnostic" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
138,755
<p>I converted Greg Hewgill's python code to C# and it worked!</p> <pre><code>[Test] public void ReplaceTextInQuotes() { Assert.AreEqual("axbx'cPdPe'fxgh'iPj'k", Regex.Replace("axbx'cxdxe'fxgh'ixj'k", @"x(?=[^']*'([^']|'[^']*')*$)", "P")); } </code></pre> <p>That test passed.</p>
9
2008-09-26T11:04:02Z
[ "c#", "python", "regex", "language-agnostic" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
139,467
<pre><code>Pattern: (?s)\G((?:^[^']*'|(?&lt;=.))(?:'[^']*'|[^'x]+)*+)x Replacement: \1P </code></pre> <ol> <li><code>\G</code> &mdash; Anchor each match at the end of the previous one, or the start of the string.</li> <li><code>(?:^[^']*'|(?&lt;=.))</code> &mdash; If it is at the beginning of the string, match up ...
1
2008-09-26T13:22:18Z
[ "c#", "python", "regex", "language-agnostic" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
139,802
<p>The trick is to use non-capturing group to match the part of the string <em>following</em> the match (character <strong>x</strong>) we are searching for. Trying to match the string up to <strong>x</strong> will only find either the first or the last occurence, depending whether non-greedy quantifiers are used. Here'...
2
2008-09-26T14:17:33Z
[ "c#", "python", "regex", "language-agnostic" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
140,977
<pre><code>#!/usr/bin/perl -w use strict; # Break up the string. # The spliting uses quotes # as the delimiter. # Put every broken substring # into the @fields array. my @fields; while (&lt;&gt;) { @fields = split /'/, $_; } # For every substring indexed with an odd # number, search for x and replace it # with ...
2
2008-09-26T17:56:13Z
[ "c#", "python", "regex", "language-agnostic" ]
Can Regex be used for this particular string manipulation?
138,552
<p>I need to replace character (say) <strong>x</strong> with character (say) <strong>P</strong> in a string, but only if it is contained in a quoted substring. An example makes it clearer:</p> <pre><code>axbx'cxdxe'fxgh'ixj'k -&gt; axbx'cPdPe'fxgh'iPj'k </code></pre> <p>Let's assume, for the sake of simplicity, that...
7
2008-09-26T10:00:10Z
150,711
<p>A more general (and simpler) solution which allows non-paired quotes.</p> <ol> <li>Find quoted string</li> <li><p>Replace 'x' by 'P' in the string</p> <pre><code>#!/usr/bin/env python import re text = "axbx'cxdxe'fxgh'ixj'k" s = re.sub("'.*?'", lambda m: re.sub("x", "P", m.group(0)), text) print s == "axbx'c...
2
2008-09-29T21:16:12Z
[ "c#", "python", "regex", "language-agnostic" ]
Vim extension (via Python)?
138,680
<p>is it possible to extend vim functionality via custom extension (preferably, written in Python)?</p> <p>What I need ideally is custom command when in command mode. E.g.</p> <p>ESC</p> <p>:do_this</p> <p>:do_that</p>
10
2008-09-26T10:45:57Z
138,709
<p>vim supports scripting in python (and in perl as well, I think).</p> <p>You just have to make sure that the vim distribution you are using has been compiled with python support.</p> <p>If you are using a Linux system, you can download the source and then compile it with</p> <pre><code>./configure --enable-pythoni...
19
2008-09-26T10:55:13Z
[ "python", "vim" ]
Vim extension (via Python)?
138,680
<p>is it possible to extend vim functionality via custom extension (preferably, written in Python)?</p> <p>What I need ideally is custom command when in command mode. E.g.</p> <p>ESC</p> <p>:do_this</p> <p>:do_that</p>
10
2008-09-26T10:45:57Z
138,720
<p>Yes it is. There are several extensions on <a href="http://www.vim.org/scripts/index.php" rel="nofollow">http://www.vim.org/scripts/index.php</a> </p> <p>It can be done with python as well if the support for python is compiled in. </p> <p>Article about it: <a href="http://www.techrepublic.com/article/extending-vim...
5
2008-09-26T10:57:54Z
[ "python", "vim" ]
Vim extension (via Python)?
138,680
<p>is it possible to extend vim functionality via custom extension (preferably, written in Python)?</p> <p>What I need ideally is custom command when in command mode. E.g.</p> <p>ESC</p> <p>:do_this</p> <p>:do_that</p>
10
2008-09-26T10:45:57Z
5,293,536
<p>Had a problems to compile Vim with Python. </p> <p>"...checking if compile and link flags for Python are sane... no: PYTHON DISABLED" in the ./configure output.</p> <p>On Ubuntu 10.04 you have to install '<strong>python2.6-dev</strong>'. The flags for ./configure are:</p> <p>--enable-pythoninterp</p> <p>--with-p...
3
2011-03-14T00:29:53Z
[ "python", "vim" ]
How do I test a django database schema?
138,851
<p>I want to write tests that can show whether or not the database is in sync with my models.py file. Actually I have already written them, only to find out that django creates a new database each time the tests are run based on the models.py file. Is there any way I can make the <strong>models.py test</strong> use th...
6
2008-09-26T11:23:51Z
139,137
<p>What we did was override the default test_runner so that it wouldn't create a new database to test against. This way, it runs the test against whatever our current local database looks like. But be very careful if you use this method because any changes to data you make in your tests will be permanent. I made sur...
8
2008-09-26T12:32:46Z
[ "python", "django", "unit-testing", "model" ]
PyQt - QScrollBar
139,005
<p>Dear Stacktoverflow, can you show me an example of how to use a QScrollBar? Thanks.</p>
0
2008-09-26T12:05:29Z
139,056
<p>It will come down to you using the QScrollArea, it is a widget that implements showing something that is larger than the available space. You will not need to use QScrollBar directly. I don't have a PyQt example but there is a C++ example in the QT distribution it is called the "Image Viewer". The object hierarchy w...
1
2008-09-26T12:18:12Z
[ "python", "pyqt" ]
PyQt - QScrollBar
139,005
<p>Dear Stacktoverflow, can you show me an example of how to use a QScrollBar? Thanks.</p>
0
2008-09-26T12:05:29Z
139,082
<p>In the PyQT source code distribution, look at the file:</p> <blockquote> <p>examples/widgets/sliders.pyw</p> </blockquote> <p>Or there is a minimal example <a href="http://www.zetcode.com/tutorials/pyqt4/widgets/" rel="nofollow">here</a> (I guess I shouldn't copy paste because of potential copyright issues)</p>
0
2008-09-26T12:23:54Z
[ "python", "pyqt" ]
PyQt - QScrollBar
139,005
<p>Dear Stacktoverflow, can you show me an example of how to use a QScrollBar? Thanks.</p>
0
2008-09-26T12:05:29Z
139,107
<pre><code>&gt;&gt;&gt; import sys &gt;&gt;&gt; from PyQt4 import QtCore, QtGui &gt;&gt;&gt; app = QtGui.QApplication(sys.argv) &gt;&gt;&gt; sb = QtGui.QScrollBar() &gt;&gt;&gt; sb.setMinimum(0) &gt;&gt;&gt; sb.setMaximum(100) &gt;&gt;&gt; def on_slider_moved(value): print "new slider position: %i" % (value, ) &gt;&gt;...
2
2008-09-26T12:28:38Z
[ "python", "pyqt" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
139,193
<p>You can use <code>dir(module)</code> to see all available methods/attributes. Also check out PyDocs.</p>
238
2008-09-26T12:40:20Z
[ "python" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
139,198
<p>The inspect module. Also see the <a href="http://docs.python.org/2/library/pydoc.html"><code>pydoc</code></a> module, the <code>help()</code> function in the interactive interpreter and the <code>pydoc</code> command-line tool which generates the documentation you are after. You can just give them the class you wish...
69
2008-09-26T12:41:04Z
[ "python" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
139,258
<pre><code>import types import yourmodule print [yourmodule.__dict__.get(a) for a in dir(yourmodule) if isinstance(yourmodule.__dict__.get(a), types.FunctionType)] </code></pre>
44
2008-09-26T12:50:39Z
[ "python" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
140,106
<p>Once you've <code>import</code>ed the module, you can just do:</p> <pre><code> help(modulename) </code></pre> <p>... To get the docs on all the functions at once, interactively. Or you can use:</p> <pre><code> dir(modulename) </code></pre> <p>... To simply list the names of all the functions and variables defin...
76
2008-09-26T15:08:54Z
[ "python" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
142,501
<p>This will do the trick:</p> <pre><code>dir(module) </code></pre> <p>However, if you find it annoying to read the returned list, just use the following loop to get one name per line.</p> <pre><code>for i in dir(module): print i </code></pre>
20
2008-09-26T23:41:16Z
[ "python" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
9,794,849
<p>An example with inspect:</p> <pre><code>from inspect import getmembers, isfunction from my_project import my_module functions_list = [o for o in getmembers(my_module) if isfunction(o[1])] </code></pre> <p>getmembers returns a list of (object_name, object_type) tuples.</p> <p>You can replace isfunction with any o...
37
2012-03-20T20:59:57Z
[ "python" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
10,079,706
<p><code>dir(module)</code> is the standard way when using a script or the standard interpreter, as mentioned in most answers.</p> <p>However with an interactive python shell like <a href="http://ipython.org">IPython</a> you can use tab-completion to get an overview of all objects defined in the module. This is much ...
13
2012-04-09T20:51:58Z
[ "python" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
11,173,131
<p>It is not (or at least no longer) proper to use dir(module). The code should read like this:</p> <pre><code>dir('module') or dir('modules') </code></pre> <p>Or you specify the module you want like this: <code>dir('sys')</code> to produce results from the module name sys. <code>dir()</code> returns errors while <...
-5
2012-06-23T21:29:31Z
[ "python" ]
listing all functions in a python module
139,180
<p>I have a python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. </p> <p>I want to call the doc function on each one. In ruby I can do something like ClassName.methods to get a list of all the methods available on that class. Is there something simil...
164
2008-09-26T12:38:52Z
30,584,102
<p>None of these answers will work if you are unable to import said Python file without import errors. This was the case for me when I was inspecting a file which comes from a large code base with a lot of dependencies. The following will process the file as text and search for all method names that start with "def" an...
0
2015-06-01T22:05:25Z
[ "python" ]