The Artima Developer Community
Sponsored Link

Python Buzz Forum
Do What I Mean

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ben Last

Posts: 247
Nickname: benlast
Registered: May, 2004

Ben Last is no longer using Python.
Do What I Mean Posted: Jul 28, 2004 3:12 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ben Last.
Original Post: Do What I Mean
Feed Title: The Law Of Unintended Consequences
Feed URL: http://benlast.livejournal.com/data/rss
Feed Description: The Law Of Unintended Consequences
Latest Python Buzz Posts
Latest Python Buzz Posts by Ben Last
Latest Posts From The Law Of Unintended Consequences

Advertisement
Quasi (0.8) variable substitution has, as the Americans would say, "gotten smarter".  What an odd language they do speak.

Anyway, for a good while, there have been two forms of variable substitution in Quasi.  Given a variable called a, you can write either $a or $(a).  Example:
>>>a = "*.py"
>>>os ls $a
['bag.py', 'bomreader.py', 'fix.py', 'floofinstall.py', 'grabbit.py', 'listproxy.py', 'mutabilitytest.py'...

or
>>>os ls $(a)
['bag.py', 'bomreader.py', 'fix.py', 'floofinstall.py', 'grabbit.py', 'listproxy.py', 'mutabilitytest.py'...


The original idea was that the short form (no parentheses) would be for simple identifiers, while the longer form allowed the parser to recognise Python expressions and the like.  That's still true, so if you wanted to do something clever with a list of filenames, you might write:
os ls $(map(lambda x: x.split('.')[0]+'.pyc', files))

...and that'd all work.  In the context-centric Quasi way of looking at the world, the $() construction escapes out of the os context back into Python.

However, the short form now has added intelligence ("New! Improved! With added context-awareness!").  Since one can always write $(a) instead of $a, there's no real downside to this; it addeth, but it taketh not away.

In general, I'm not a fan of languages that try to do what they think you want (*cough* Perl *cough), as opposed to requiring you to state it explicitly.  Explicit is better than implicit, in fact.  But context-sensitive substitution is plain Interesting, and has a certain "cool!" factor to it, as much as a mundane subject like coding ever can.

Consider this; I have the result of a SQL query in variable res.  In Quasi fashion, it's a self-describing object, an instance of quasi.Bag() which records the column names (as keys), the values (as, er, values) and allows dict or object semantics to access them.  Let's say that it has the effective value:
{ "ColumnA" : 1, "ColumnB" : 2, "ColumnC", "Eric" }

Now, here are some SQL statements with clever variable substitutions and their results.
Ben@KANDINSKY2 ~/python
$ ./quasi.py
Welcome to Quasi, the multiple-context Python Shell.
    (c) 2004 Ben Last, and released under a BSD license.  Enjoy.

Version 0.8

>>>res = quasi.Bag()
>>>res["ColumnA"]=1
>>>res["ColumnB"]=2
>>>res["ColumnC"]="Eric"
>>>
>>>#Check the values
>>>res
(1, 2, 'Eric')
>>>res.keys()
['ColumnA', 'ColumnB', 'ColumnC']
>>>
>>>#use res as a set of column names
>>>explain `sql select $res from MyTable`
"select 'ColumnA','ColumnB','ColumnC' from MyTable"
>>>
>>>#use res as a set of values
>>>explain `sql insert into MyTable set $res`
"insert into MyTable set ColumnA=1,ColumnB=2,ColumnC='Eric'"
>>>
>>>#use res as both
>>>explain `sql update MyTable ($res) values ($res)`
"update MyTable ('ColumnA','ColumnB','ColumnC') values (1,2,'Eric')"
>>>


I think that's rather excellent.  SQL is just regular enough that, by looking backwards along the command from the point where the variable substitution occurs, the parser can figure out how to insert the variable.  It also works (in a different way) for the os and shell contexts - strings get quoted and lists get comma-separated on Windows.  The explain command (as in the examples above) also lets you see what a substitution will do, without having to actually do it.

Read: Do What I Mean

Topic: Wax open issues #1: "property parameters" Previous Topic   Next Topic Topic: Live CD

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use