[Baypiggies] April snippets meeting - tdc, undent, let/IN, property2
Doug Landauer
zia at cruzio.com
Fri Apr 13 10:43:47 CEST 2007
The snippets I talked about at the 12 April meeting (plus a couple
more) are in my weblog entry here:
http://radio.weblogs.com/0100945/2007/04/12.html
A few things I forgot to mention about my three-character date stamp:
* I call the script "tdc" but I've forgotten what the "C" stood for.
Maybe "Converter".
* It's actually a six-character date-and-time stamp, with a
one-second precision and a 26- or 52-year range (26 only if
you're using it for filenames *and* you aren't using a case-
sensitive filesystem).
* I've been using it since 2001.
* In its most frequently-used form, it's a (zsh) shell function, and
I arranged to have my zsh call it before every prompt, and insert
its output into every prompt. Available on request.
* When I use it manually, it's usually just the 3-character date
part, as a filename prefix.
* A string sort on these is also a chronological sort.
* Google for "gigo beH" to see my 2002/05/13 entry about it
(it's the second result, today).
About undent:
* Google for "gigo undent" to see my February entry about that.
I prefer the look of having a separator character, over the
barer-looking form that is used with the library function that
Drew mentioned (textwrap.dedent) and with the ASPN cookbook entry.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Chris Clark already sent out a message about the unicode/normalization
snippet that he presented.
Let me (DAL) just add this: http://www.unicode.org/reports/tr15/
makes it pretty clear that "NFKD" is *not* an acronym; the
NF stands for "Normalization Form", but the "KD" stands
somehow for "Compatibility Decomposition".
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#
# Zach Collins posted a nice small snippet that allows you to
# use a "let" syntax similar to what scheme or OCaml have:
# "let <var1> = <expr1>,
# <var2> = <expr2>
# IN <exprN>"
#
# The code snippet itself is tiny:
class let:
def __init__(self, **kwds):
self.kwds = kwds
def IN(self, func):
return func(**self.kwds)
#
# It looks trivial, but it lets you name and initialize
# temporary variables, then use them in a longer expression,
# and then disappear (i.e., without polluting the containing
# scope).
#
#
# Here's the sample Zach typed in, mostly from memory; a fragment
# from a "Longest Common Substring" calculation. I think I've
# removed all of the removable backslashes:
left = \
let( rightToLeftLcsTable =
let( leftHalf = str2[:right + 1],
firstRow = [0] * len(str2) ).
IN(lambda leftHalf, firstRow:
lcsTable(stringReverse(str1), stringReverse(leftHalf),
firstRow, eq)
+ [firstRow])). \
IN(lambda rightToLeftLcsTable:
match(lcsFarBound(rightToLeftLcsTable[0]),
({"const": -1}, -1),
(__, lambda x: x - 1)))
#
# Here's a bit of a "how it works" note that I didn't notice during
# the meeting: Notice the dot at the end of each of the lines
# that precedes an "IN(...)" line. The "IN" is a member function
# called on the "let" object. So the value of the expression as
# a whole is as if you had typed:
#
# let_obj = let( ... ) # set those variables
# left = let_obj.IN( <lambda function that uses those variables>
)
#
# Cool, though if I were doing it, I think I'd be using one- or
two-letter
# variable names instead of 8 to 19-letter names. I wonder if there's
a
# way to do this that lowers the repetition factor (i.e., doesn't
require
# you to type each variable name at least twice)..
#
# Note that the cool-looking "match" function is left as an exercise
# for the reader.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Responding to Marilyn Davis' snippet about the "property" decorator
function, Drew showed an implementation of "property2" which
can be used with the "@" attribute syntax, unlike the Python
library "property" decorator. Also left as an exercise for
the reader, or for Drew to post here in due time.
-- Doug Landauer
More information about the Baypiggies
mailing list