ZPatterns: SAVING doesn't tolerate missing attributes
In SkinScript such as WHEN eventspec CALL expression SAVING mementolist if any of the mementos in mementolist are not found, the script raises a KeyError. I'd like a way of saving a memento if it exists, or NOT_FOUND otherwise. For example, an Executive might have a CompanyCar. WITH CompanyCars.getCar(self.car_id) or NOT_FOUND COMPUTE car=RESULT, car_registration=registration OTHERWISE LET car=NOT_FOUND, car_registration="no car" When an Executive changes her car for a different one, I want to perform certain actions on the old car, and certain on the new car: WHEN OBJECT CHANGED CALL HAS_CHANGED('car_id') and (self.car_id and CompanyCars.getItem(self.car_id).decommission(), ORIGINAL['car_id'] and OLD['car'].commission()) SAVING car However, I can't do this if the executive is being assigned a car from a state of having no car. It fails with a KeyError because the ZPatterns machinery can't save "car". -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
Hi Steve, Could you let some other value represent no car? (e.g., _.None?). untested... WITH CompanyCars.getCar(self.car_id) or NOT_FOUND COMPUTE car=RESULT, car_registration=registration OTHERWISE LET car=_.None car_registration="no car" WHEN OBJECT CHANGED CALL HAS_CHANGED('car_id') and (self.car_id and CompanyCars.getItem(self.car_id).decommission(), ORIGINAL['car_id'] and (OLD['car'] and OLD['car'].commission())) SAVING car Would that work? -steve
"SA" == Steve Alexander <steve@cat-box.net> writes:
SA> In SkinScript such as SA> WHEN eventspec CALL expression SAVING mementolist SA> if any of the mementos in mementolist are not found, the SA> script raises a KeyError.
Steve Spicklemire wrote:
Hi Steve,
Could you let some other value represent no car? (e.g., _.None?).
untested...
WITH CompanyCars.getCar(self.car_id) or NOT_FOUND COMPUTE car=RESULT, car_registration=registration OTHERWISE LET car=_.None car_registration="no car"
WHEN OBJECT CHANGED CALL HAS_CHANGED('car_id') and (self.car_id and CompanyCars.getItem(self.car_id).decommission(), ORIGINAL['car_id'] and (OLD['car'] and OLD['car'].commission())) SAVING car
Would that work?
Sure, that would work. The workaround I've chosen is to use CompanyCars.getItem(ORIGINAL['car_id']).commission() in place of OLD['car'].commission() Oh, and I jsut noticed, I had commission and decommission the wrong way around in the example :-) I'd still prefer SAVING to tolerate not-found attributes, though. -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
participants (2)
-
Steve Alexander -
Steve Spicklemire