PEP308: Yet another syntax proposal
holger krekel
pyth at devel.trillke.net
Mon Feb 10 19:16:59 EST 2003
David Eppstein wrote:
> On 2/10/03 11:28 PM +0100 holger krekel <pyth at devel.trillke.net> wrote:
> >
> > When i had situations like this I rewrote it to get the special "select
> > all" value not interfere with usual list/tuple-index semantics. E.g.
> > using a dummy-class for "select_all" rather than reusing an otherwise
> > valid key index.
> >
> > Sure. But I'd rather have my co-coders think about easier ways rather
> > having to understand ternary operations often. Had i seen your above
> > code
> >
> > cols[i] if i >= 0 else None
> >
> > just on its own i would be puzzled (which i was). May i ask
> > you to show the complete function where this came from?
>
> Not that I make any claim of having polished this to jewel-like perfection,
> but here it is. It's part of the controller for the Find dialog in a
> table-editing application. The input data structures are:
> self.doc.columns() -- a list of strings used as dict keys
> self.doc.rows() -- a list of dicts keyed by the columns
> contains, beginsWith, endsWith, exactMatch -- localized string compare
> routines
> self.table -- the controller for the table display
> self.column, self.match, self.text -- components of the Find dialog
> Note that self.match and self.columns are both pop-up buttons, but accessed
> differently because one is static and the other has to be updated when the
> self.doc.columns() changes.
>
> def search(self, row, increment):
> def matches(row):
> if col is not None:
> return match(row.get(col, ''), target)
> for c in cols:
> if match(row.get(c, ''), target):
> return 1
> return 0
>
> cols = self.doc.columns()
> col = self.column.indexOfSelectedItem() - 1
> if col < 0:
> col = None
> else:
> col = cols[col]
>
> match = self.match.selectedItem().tag()
> match = (contains, beginsWith, endsWith, exactMatch)[match]
> target = self.text.stringValue()
> rows = self.doc.rows()
> while 1:
> row += increment
> if row < 0 or row >= len(rows):
> return 0
> if matches(rows[row]):
> self.table.setSelectedLine_(row)
> return 1
thanks ...
> Looking at it again, it is possible to simplify this, in a way that gets
> rid of the implicit ternary, allowing the subroutine to be more easily
> folded into the main routine:
interesting ...
> def search(self, row, increment):
> cols = self.doc.columns()
> col = self.column.indexOfSelectedItem() - 1
> if col >= 0:
> cols = [cols[col]]
this was exactly what i hoped for when i asked: avoiding
the special cased index (-1) and constructing the list of
all columns (or just with one item) from the start. Thus
you adapt the Cocoa-semantic right where you invoked it
and doesn't let it interfere with python semantics.
> match = self.match.selectedItem().tag()
> match = (contains, beginsWith, endsWith, exactMatch)[match]
> target = self.text.stringValue()
> rows = self.doc.rows()
> while 1:
> row += increment
> if row < 0 or row >= len(rows):
> return 0
> for c in cols:
> if match(rows[row].get(c, ''), target):
> self.table.setSelectedLine_(row)
> return 1
>
> But, clearly, the existence or nonexistence of a ternary operator had
> nothing to do with my ability to find this simplification -- I already had
> working code for this problem, so had little motivation to work harder on
> simplifying it until you asked me to post it.
sure, but
a) would you have reimplemented it with PEP-308 syntax?
b) it doesn't exactly disprove my point that ternary operators
can often be avoided for great justice :-)
cheers,
holger
More information about the Python-list
mailing list