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
1=== added file 'pythoncore/columnar_display.py'
2--- pythoncore/columnar_display.py 1970-01-01 00:00:00 +0000
3+++ pythoncore/columnar_display.py 2010-04-20 04:15:25 +0000
4@@ -0,0 +1,28 @@
5+#!/usr/bin/env python
6+#
7+# [SNIPPET_NAME: Columnar display]
8+# [SNIPPET_CATEGORIES: Python Core]
9+# [SNIPPET_DESCRIPTION: Print a list of arguments in several columns]
10+# [SNIPPET_AUTHOR: Akkana Peck <akkana@shallowsky.com>]
11+# [SNIPPET_LICENSE: GPL]
12+def columnar_display(list, pagewidth=77) :
13+ maxlen = 0
14+ for item in list :
15+ l = len(str(item))
16+ if l > maxlen :
17+ maxlen = l
18+ maxlen += 2 # space it out a little more
19+ numcol = int(pagewidth / maxlen)
20+
21+ i = 0
22+ for item in list :
23+ print '{0:{1}}'.format(item, maxlen),
24+ i += 1
25+ if i % numcol == 0 :
26+ print '\n',
27+
28+list = [ 'Python Core', 'Python VTE', 'Regular Expression', 'socket',
29+ 'tarfile', 'Testing', 'threading', 'twitter', 'unittest',
30+ 'Upstart', 'Webkit', 'Zeitgeist' ]
31+
32+columnar_display(list)
33
34=== added file 'pythoncore/tee.py'
35--- pythoncore/tee.py 1970-01-01 00:00:00 +0000
36+++ pythoncore/tee.py 2010-04-20 04:15:25 +0000
37@@ -0,0 +1,39 @@
38+#!/usr/bin/env python
39+#
40+# [SNIPPET_NAME: Tee]
41+# [SNIPPET_CATEGORIES: Python Core]
42+# [SNIPPET_DESCRIPTION: Duplicate output to two file descriptors, like Unix tee]
43+# [SNIPPET_AUTHOR: Akkana Peck <akkana@shallowsky.com>]
44+# [SNIPPET_DOCS: http://shallowsky.com/blog/programming/python-tee.html]
45+# [SNIPPET_LICENSE: GPL]
46+import sys
47+
48+class tee :
49+ def __init__(self, _fd1, _fd2) :
50+ self.fd1 = _fd1
51+ self.fd2 = _fd2
52+
53+ def __del__(self) :
54+ if self.fd1 != sys.stdout and self.fd1 != sys.stderr :
55+ self.fd1.close()
56+ if self.fd2 != sys.stdout and self.fd2 != sys.stderr :
57+ self.fd2.close()
58+
59+ def write(self, text) :
60+ self.fd1.write(text)
61+ self.fd2.write(text)
62+
63+ def flush(self) :
64+ self.fd1.flush()
65+ self.fd2.flush()
66+
67+if len(sys.argv) <= 1 :
68+ print "Usage:", sys.argv[0], 'outputfile'
69+ sys.exit(1)
70+
71+outputlog = open(sys.argv[1], "w")
72+stderrsav = sys.stderr
73+sys.stderr = tee(stderrsav, outputlog)
74+
75+print >> sys.stderr, "Test 1"
76+sys.stderr.write("Test 2\n")
77
78=== added file 'webkit/webkit_preso.py'
79--- webkit/webkit_preso.py 1970-01-01 00:00:00 +0000
80+++ webkit/webkit_preso.py 2010-04-20 04:15:25 +0000
81@@ -0,0 +1,46 @@
82+#!/usr/bin/env python
83+#
84+# [SNIPPET_NAME: Presenter]
85+# [SNIPPET_CATEGORIES: Webkit]
86+# [SNIPPET_DESCRIPTION: Fullscreen presenter program, for HTML presentations]
87+# [SNIPPET_AUTHOR: Akkana Peck <akkana@shallowsky.com>]
88+# [SNIPPET_DOCS: http://shallowsky.com/blog/programming/webkit-presenter.html]
89+# [SNIPPET_LICENSE: GPL]
90+
91+import sys, os
92+import gtk, gobject
93+import webkit
94+
95+class WebBrowser(gtk.Window):
96+ def __init__(self, url):
97+ gtk.Window.__init__(self)
98+
99+ # Either run fullscreen, or set an initial window size
100+ #self.set_default_size(1024,768)
101+ self.fullscreen()
102+
103+ self._browser= webkit.WebView()
104+ self.add(self._browser)
105+ self.connect('destroy', gtk.main_quit)
106+
107+ self._browser.open(url) # throw err if url isn't defined
108+ self.show_all()
109+
110+if __name__ == "__main__":
111+ if len(sys.argv) <= 1 :
112+ print "Usage:", sys.argv[0], "url"
113+ sys.exit(0)
114+
115+ # Figure out if it's a filename or a url
116+ url = sys.argv[1]
117+ if url.find(':') < 0 :
118+ # If it's a local file, it needs to be converted to an absolute URL
119+ if url[0] == '/' :
120+ url = 'file://' + url
121+ else :
122+ url = 'file://' + os.getcwd() + '/' + url
123+
124+ gobject.threads_init()
125+ webbrowser = WebBrowser(url)
126+ gtk.main()
127+

Subscribers

People subscribed via source and target branches