Merge lp:~akkzilla/python-snippets/pysnippets into lp:~jonobacon/python-snippets/trunk

Proposed by Akkana Peck
Status: Merged
Merged at revision: not available
Proposed branch: lp:~akkzilla/python-snippets/pysnippets
Merge into: lp:~jonobacon/python-snippets/trunk
Diff against target: 127 lines (+113/-0)
3 files modified
pythoncore/columnar_display.py (+28/-0)
pythoncore/tee.py (+39/-0)
webkit/webkit_preso.py (+46/-0)
To merge this branch: bzr merge lp:~akkzilla/python-snippets/pysnippets
Reviewer Review Type Date Requested Status
Jono Bacon Pending
Review via email: mp+23739@code.launchpad.net

Description of the change

Three snippets: tee file descriptors, print in columns, and show an HTML fullscreen presentation (webkit-gtk).

To post a comment you must log in.
Revision history for this message
Jono Bacon (jonobacon) wrote :

Thanks Akkana for your snippets, they look great! :-)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'pythoncore/columnar_display.py'
--- pythoncore/columnar_display.py 1970-01-01 00:00:00 +0000
+++ pythoncore/columnar_display.py 2010-04-20 04:15:25 +0000
@@ -0,0 +1,28 @@
1#!/usr/bin/env python
2#
3# [SNIPPET_NAME: Columnar display]
4# [SNIPPET_CATEGORIES: Python Core]
5# [SNIPPET_DESCRIPTION: Print a list of arguments in several columns]
6# [SNIPPET_AUTHOR: Akkana Peck <akkana@shallowsky.com>]
7# [SNIPPET_LICENSE: GPL]
8def columnar_display(list, pagewidth=77) :
9 maxlen = 0
10 for item in list :
11 l = len(str(item))
12 if l > maxlen :
13 maxlen = l
14 maxlen += 2 # space it out a little more
15 numcol = int(pagewidth / maxlen)
16
17 i = 0
18 for item in list :
19 print '{0:{1}}'.format(item, maxlen),
20 i += 1
21 if i % numcol == 0 :
22 print '\n',
23
24list = [ 'Python Core', 'Python VTE', 'Regular Expression', 'socket',
25 'tarfile', 'Testing', 'threading', 'twitter', 'unittest',
26 'Upstart', 'Webkit', 'Zeitgeist' ]
27
28columnar_display(list)
029
=== added file 'pythoncore/tee.py'
--- pythoncore/tee.py 1970-01-01 00:00:00 +0000
+++ pythoncore/tee.py 2010-04-20 04:15:25 +0000
@@ -0,0 +1,39 @@
1#!/usr/bin/env python
2#
3# [SNIPPET_NAME: Tee]
4# [SNIPPET_CATEGORIES: Python Core]
5# [SNIPPET_DESCRIPTION: Duplicate output to two file descriptors, like Unix tee]
6# [SNIPPET_AUTHOR: Akkana Peck <akkana@shallowsky.com>]
7# [SNIPPET_DOCS: http://shallowsky.com/blog/programming/python-tee.html]
8# [SNIPPET_LICENSE: GPL]
9import sys
10
11class tee :
12 def __init__(self, _fd1, _fd2) :
13 self.fd1 = _fd1
14 self.fd2 = _fd2
15
16 def __del__(self) :
17 if self.fd1 != sys.stdout and self.fd1 != sys.stderr :
18 self.fd1.close()
19 if self.fd2 != sys.stdout and self.fd2 != sys.stderr :
20 self.fd2.close()
21
22 def write(self, text) :
23 self.fd1.write(text)
24 self.fd2.write(text)
25
26 def flush(self) :
27 self.fd1.flush()
28 self.fd2.flush()
29
30if len(sys.argv) <= 1 :
31 print "Usage:", sys.argv[0], 'outputfile'
32 sys.exit(1)
33
34outputlog = open(sys.argv[1], "w")
35stderrsav = sys.stderr
36sys.stderr = tee(stderrsav, outputlog)
37
38print >> sys.stderr, "Test 1"
39sys.stderr.write("Test 2\n")
040
=== added file 'webkit/webkit_preso.py'
--- webkit/webkit_preso.py 1970-01-01 00:00:00 +0000
+++ webkit/webkit_preso.py 2010-04-20 04:15:25 +0000
@@ -0,0 +1,46 @@
1#!/usr/bin/env python
2#
3# [SNIPPET_NAME: Presenter]
4# [SNIPPET_CATEGORIES: Webkit]
5# [SNIPPET_DESCRIPTION: Fullscreen presenter program, for HTML presentations]
6# [SNIPPET_AUTHOR: Akkana Peck <akkana@shallowsky.com>]
7# [SNIPPET_DOCS: http://shallowsky.com/blog/programming/webkit-presenter.html]
8# [SNIPPET_LICENSE: GPL]
9
10import sys, os
11import gtk, gobject
12import webkit
13
14class WebBrowser(gtk.Window):
15 def __init__(self, url):
16 gtk.Window.__init__(self)
17
18 # Either run fullscreen, or set an initial window size
19 #self.set_default_size(1024,768)
20 self.fullscreen()
21
22 self._browser= webkit.WebView()
23 self.add(self._browser)
24 self.connect('destroy', gtk.main_quit)
25
26 self._browser.open(url) # throw err if url isn't defined
27 self.show_all()
28
29if __name__ == "__main__":
30 if len(sys.argv) <= 1 :
31 print "Usage:", sys.argv[0], "url"
32 sys.exit(0)
33
34 # Figure out if it's a filename or a url
35 url = sys.argv[1]
36 if url.find(':') < 0 :
37 # If it's a local file, it needs to be converted to an absolute URL
38 if url[0] == '/' :
39 url = 'file://' + url
40 else :
41 url = 'file://' + os.getcwd() + '/' + url
42
43 gobject.threads_init()
44 webbrowser = WebBrowser(url)
45 gtk.main()
46

Subscribers

People subscribed via source and target branches