From: Clark C . E. <cc...@cl...> - 2001-11-17 22:18:15
|
Ok. There isn't a nice way to do this; I withdraw the suggestion. Anchors work "dumb" mode. Clark On Sat, Nov 17, 2001 at 05:17:17PM -0500, Clark C . Evans wrote: | | > And anchors may shadow previous | | > anchors for aliases at the same level of indentation or higher. This | | > shadowing policy allows for simple, cut, indent, and paste. | | | | I doubt it is that simple: | | | | a0: | | b0: &001 | | c0: &002 ... | | a1: *001 | | a2: *002 | | a3: | | b1: &002 shadows it... | | c1: &001 shadows it... | | | | You can't get away with "level" tricks here. | | It's ugly... but here is a first pass (a sparse | list would be nice)... written but not tested. | The goal is to have each anchor be a mapping | to a list of values that are relevant depending | on your level. Efficient it isn't. But I'm | sure with some mental work this could be a | fairly quick operation. In the "best" case | when anchors are never reused, it is only a | tiny bit slower (a tuple is used). However, | on the shadow cases, an entire list of values | indexed by level is required. | | ... | | import types | anchors = {} | | def set_list(lst,lvl,val): | # poor man's sparse list | # make sure list is big enough, | # lvl is 1,2,3,4 | diff = lvl - len(lst) | if diff > 0: lst.extend((None,)*diff) | lst[lvl-1] = val | return lst[:lvl] | | | def get_list(lst,lvl): | # return most specific value | # for the given level | ret = None | idx = 0 | while idx < len(lst): | if idx > (lvl-1): | if ret: | return ret | ret = lst[idx] | return ret | | def set(lvl,key,val): | cur = anchors.get(key,None) | if cur: | if type(cur) is types.ListType: | # if it is a list already | anchors.set(key,set_list(cur,lvl,val)) | else: | # if it is a scalar | (old_lvl,old_val) = cur | if old_val >= lvl: | # update the scalar | anchors.set(key,(lvl,val)) | else | # we need to make a list | anchors.set(key, | set_list( | set_list([],lvl,val), | old_lvl, old_val)) | else: | # brand new value | anchors.set(key,(lvl,val)) | | def get(lvl,key): | ret = None | cur = anchors.get(key,None) | if cur: | if type(cur) is types.ListType: | ret = get_list(cur,lvl) | else: | ret = cur[1] | return ret | | | | _______________________________________________ | Yaml-core mailing list | Yam...@li... | https://lists.sourceforge.net/lists/listinfo/yaml-core -- Clark C. Evans Axista, Inc. http:\\axista.com 800.926.5525 Collaborative Software for Project Management Patriotisim means protecting core values during difficult times, not pasting a flag on your SUV and repealing the Bill of Rights. |