[CentralOH] 2013-03-25 Scribbles
jep200404 at columbus.rr.com
jep200404 at columbus.rr.com
Tue Mar 26 19:50:50 CET 2013
Meetings are likely to be at various places,
so check cohpy.org before each meeting for location.
Start with cohpy.org
redirects to meetup page.
meetup page has link to mailing list
Subscribe or browse archives there.
--------------------------------------------------------------------------------
Most CohPy presentations are about big stuff.
This presentation is a small lightweight one for beginners
about one thing that Python does nicely.
Python is pretty clean.
Python is pretty elegant.
That clean elegance is not just in the basic language itself,
but extends into the libraries and how they work together.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Objects with read or write methods can be used like files
in various places that want a file.
This makes some problems easy to solve.
Such objects are called "file-like objects".
They are surprisingly easy to use.
I will show a real-world example
of how a file-like object was used to simplify code.
The code in this presentation is available at the following URLs.
https://launchpad.net/reduce
http://bazaar.launchpad.net/~brywilharris/reduce/trunk/files
http://bazaar.launchpad.net/~brywilharris/reduce/trunk/view/head:/tee.py
need to add commas, refactor
http://bazaar.launchpad.net/~brywilharris/reduce/trunk/view/head:/rlo.py
s/prcessing/processing/
See "Why You Will Care: print and stdout"
on page 309 of 2011-05-20 printing of 4th edition of Learning Python
by Mark Lutz ISBN 978-0-596-15806-4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
http://mail.python.org/pipermail/centraloh/2011-July/000922.html
Subject: Need Code to Beat On
Date: Tue, 19 Jul 2011 15:31:00 -0400
Please let me know of Python projects that are:
o small
o buggy
I am learning Python. I have been reading much, and following the little
exercises, but need a whole program to beat. I do not have an itch to
scratch right now, except to learn Python.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
http://mail.python.org/pipermail/centraloh/2011-July/000923.html
From: Bryan Harris <brywilharris at gmail.com>
Subject: Re: [CentralOH] Need Code to Beat On
Date: Tue, 19 Jul 2011 17:14:24 -0400
You could look at my little python project:
https://launchpad.net/reduce
...
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
from reduce-2.2.0-jep01/rlo.py:
print time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
print "user:",os.times()[0],"s"
print "system:",os.times()[1],"s"
print >>log_file, time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
print >>log_file, "user:",os.times()[0],"s"
print >>log_file, "system:",os.times()[1],"s"
print "Measurement Files:"
print >>log_file, "Measurement Files:"
for meas_file_name in measurement_file_names :
print meas_file_name
print >>log_file,meas_file_name
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
simplified to:
reduce-2.2.0-jep02/rlo.py
log_file=open(log_file_path, 'w');
t=Tee([sys.stdout, log_file])
...
print >>t, time.strftime('%a, %d %b %Y %H:%M:%S +0000', time.localtime())
print >>t, 'user:', os.times()[0], 's'
print >>t, 'system:', os.times()[1], ' s'
print >>t, 'Measurement Files:'
for meas_file_name in measurement_file_names :
print >>t, meas_file_name
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
just concentrating on three prints
from reduce-2.2.0-jep01/rlo.py:
print time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
print "user:",os.times()[0],"s"
print "system:",os.times()[1],"s"
print >>log_file, time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
print >>log_file, "user:",os.times()[0],"s"
print >>log_file, "system:",os.times()[1],"s"
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
reduce-2.2.0-jep02/rlo.py
log_file=open(log_file_path, 'w');
t=Tee([sys.stdout, log_file])
...
print >>t, time.strftime('%a, %d %b %Y %H:%M:%S +0000', time.localtime())
print >>t, 'user:', os.times()[0], 's'
print >>t, 'system:', os.times()[1], ' s'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
reduce-2.2.0-jep02/tee.py (early version needing commas and PEP 8)
#! /usr/bin/python
# Copyright 2011 James E. Prior
#
# This is the tee module.
#
# Tee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Tee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tee If not, see <http://www.gnu.org/licenses/>.
#
class Tee:
'''
Simplifies writing to multiple files.
Instead of repeating a print for each file one needs to output to,
one uses a single print that redirects to an instance of this class.
For example, instead of doing:
print 'big long ugly messy stuff'
print >>file1 'big long ugly messy stuff'
print >>file2 'big long ugly messy stuff'
!!! need to file bug fix for missing commas above
one does the following:
tee=Tee([sys.stdout, file1, file2])
...
print >>tee 'big long ugly messy stuff'
'''
def __init__(self, list_of_open_files):
'''
Accepts one argument, a list of files that are already open.
'''
self.files=list_of_open_files
def write(self, s):
'''
Accepts one argument, and writes it to each of the files
that were specified when the instance was created.
'''
for f in self.files:
f.write(s)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This final example use of the Tee class
eliminates the need for explicit redirection at each print.
Compare this code with the earliest code.
The output of all prints is sent to both standard output and the log file.
# Redirect writes to stdout to go to both standard output and a log file.
real_stdout = sys.stdout
sys.stdout = tee = Tee([real_stdout, open('script.log', 'w')])
...
print time.strftime('%a, %d %b %Y %H:%M:%S +0000', time.localtime())
print 'user:', os.times()[0], 's'
print 'system:', os.times()[1], 's'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
newest version of Tee class:
http://bazaar.launchpad.net/~brywilharris/reduce/trunk/view/head:/tee.py
need to add commas, refactor
Of course the Tee class was inspired by the Unix tee command.
However, Tee is a bit of a misnomer, because the Tee class does not output
to the arguments _in_addtion_ to standard output like the tee command does.
I.e., The Tee class is like a tee command that always has >/dev/null.
The Unix tee command can accept multiple file arguments to write to.
Shell magic can specify commands as files. For example.
dd if=/dev/sr0 bs=2k | tee >(md5sum) >(sha1sum) >(sha256sum) >/dev/null
More shell fun:
test at cohpy:~$ cat ~/bin/vidf
vim -O "$1" "$2" <(diff "$1" "$2")
test at cohpy:~$
My Tee class is a simple example of how flexible I/O can be using
file-like objects. You will likely find other code that be simplified
by using file-like objects.
--------------------------------------------------------------------------------
dependencies
How does one figure out what the dependencies are?
I try to do everything in a virtualenv,
but sometimes I can not, and have to
install system packages to satisfy dependencies.
--no-site-packages is helpful
https://pypi.python.org/pypi?%3Aaction=search&term=vtk&submit=search
http://mail.python.org/pipermail/centraloh/2013-February/001551.html
--------------------------------------------------------------------------------
XY: Scrapy is awesome!
XY showed how he is using Scrapy for screen scraping.
http://en.wikipedia.org/wiki/Scrapy
Scrapy does not grok Javascript.
--------------------------------------------------------------------------------
icanhazip.com - terse output, great for scripting
wget -O - -q http://icanhazip.com | xxd -g 1 -u
myip=`wget -O - -q http://icanhazip.com`
echo $myip
ifconfig.me
htmlfive.appspot.com/static/whereami.html
All roads lead to Palo Alto
The Django Book web site now has pictures.
http://www.djangobook.com/en/2.0/index.html
Update old mailing list comment.
http://mail.python.org/pipermail/centraloh/2012-April/001251.html
PyOhio
Should be asking for speakers soon.
Want to encourage more first time speakers.
Join the mailing list and say what you want to help with.
http://mail.python.org/mailman/listinfo/pyohio-organizers
Web site will be based on Symposion conference software
instead of rolling our own web site.
http://pyohio.org/
https://github.com/eldarion/pyohio
forecastadvisor.com
why do I get Forbidden (403) CSRF response from Konqueror,
but not Iceweasel? Both had javascript disabled.
Python 3 Wall of Shame / Superpowers
https://python3wos.appspot.com/
Soylent_Green
Blade_Runner
Jibbigo
More information about the CentralOH
mailing list