I’m on one of my rare, and ordinarily short-lived, programming binges, and happened upon an instance where switch/case
would have come in handy. It can be implemented in PostScript and derivative languages with a dict:
/ImageMatrix <<
(none) [imagewidth 0 0 imageheight 0 0]
(flip) [imagewidth 0 0 imageheight neg 0 imageheight]
(flop) [imagewidth neg 0 0 imageheight imagewidth 0]
(both) [imagewidth neg 0 0 imageheight neg imagewidth imageheight]
>> flip get
The switch is keyed on variable flip
, which can be any one of the strings “flip”, “flop”, “both”, or “none” (PostScript encloses strings in parentheses rather than quotes). Double angle brackets build a dictionary (key-value pairs) simply by listing keys and matching values in sequential order. Here I’m choosing the ImageMatrix based on whether the RGB data is in left-to-right, bottom-to-top order (PostScript default) or is or inverted in one or both ways. The selection is done using key get
, with key of course being flip
.
Suppose you need a default case. The default in this case should arguably be ‘flip’, as left-to-right, top-to-bottom is a more normal format for raw image data. It’s not that much harder, just use stopped
:
{flip get} stopped {(flip) get} if
Which is the equivalent of Python:
flipdict = {'none': [w, 0, 0, h, 0, 0], ...}
try:
ImageMatrix = flipdict.get(flip)
except:
ImageMatrix = flipdict.get('flip')
Python would ordinarily use a targeted except
such as except KeyError
, but the above is the closest to the PostScript code. Python also supports the idiom flipdict.get(flip, flipdict[‘flip’])
, which means if the key in variable flip is not found, use flipdict[‘flip’]
as default.
Anyway, that’s all for now. Got to get back to coding!