⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sxp.py

📁 xen虚拟机源代码安装包
💻 PY
📖 第 1 页 / 共 2 页
字号:
        out.write(k_list_open)        i = 0        for x in sxpr:            if i: out.write(' ')            show(x, out)            i += 1        out.write(k_list_close)    elif isinstance(sxpr, (types.IntType, types.FloatType)):        out.write(str(sxpr))    elif isinstance(sxpr, types.StringType) and atomp(sxpr):        out.write(sxpr)    else:        out.write(repr(str(sxpr)))def show_xml(sxpr, out=sys.stdout):    """Print an sxpr in XML syntax.    """    if isinstance(sxpr, (types.ListType, types.TupleType)):        element = name(sxpr)        out.write('<%s' % element)        for attr in attributes(sxpr):            out.write(' %s=%s' % (attr[0], attr[1]))        out.write('>')        i = 0        for x in children(sxpr):            if i: out.write(' ')            show_xml(x, out)            i += 1        out.write('</%s>' % element)    elif isinstance(sxpr, types.StringType) and atomp(sxpr):        out.write(sxpr)    else:        out.write(str(sxpr))def elementp(sxpr, elt=None):    """Check if an sxpr is an element of the given type.    sxpr sxpr    elt  element type    """    return (isinstance(sxpr, (types.ListType, types.TupleType))            and len(sxpr)            and (None == elt or sxpr[0] == elt))def name(sxpr):    """Get the element name of an sxpr.    If the sxpr is not an element (i.e. it's an atomic value) its name    is None.    sxpr    returns name (None if not an element).    """    val = None    if isinstance(sxpr, types.StringType):        val = sxpr    elif isinstance(sxpr, (types.ListType, types.TupleType)) and len(sxpr):        val = sxpr[0]    return valdef attributes(sxpr):    """Get the attribute list of an sxpr.    sxpr    returns attribute list    """    val = []    if isinstance(sxpr, (types.ListType, types.TupleType)) and len(sxpr) > 1:        attr = sxpr[1]        if elementp(attr, k_attr_open):            val = attr[1:]    return valdef attribute(sxpr, key, val=None):    """Get an attribute of an sxpr.    sxpr sxpr    key  attribute key    val  default value (default None)    returns attribute value    """    for x in attributes(sxpr):        if x[0] == key:            val = x[1]            break    return valdef children(sxpr, elt=None):    """Get children of an sxpr.    sxpr sxpr    elt  optional element type to filter by    returns children (filtered by elt if specified)    """    val = []    if isinstance(sxpr, (types.ListType, types.TupleType)) and len(sxpr) > 1:        i = 1        x = sxpr[i]        if elementp(x, k_attr_open):            i += 1        val = sxpr[i : ]    if elt:        def iselt(x):            return elementp(x, elt)        val = filter(iselt, val)    return valdef child(sxpr, elt, val=None):    """Get the first child of the given element type.    sxpr sxpr    elt  element type    val  default value    """    for x in children(sxpr):        if elementp(x, elt):            val = x            break    return valdef child_at(sxpr, index, val=None):    """Get the child at the given index (zero-based).    sxpr  sxpr    index index    val   default value    """    kids = children(sxpr)    if len(kids) > index:        val = kids[index]    return valdef child0(sxpr, val=None):    """Get the zeroth child.    """    return child_at(sxpr, 0, val)def child1(sxpr, val=None):    """Get the first child.    """    return child_at(sxpr, 1, val)def child2(sxpr, val=None):    """Get the second child.    """    return child_at(sxpr, 2, val)def child3(sxpr, val=None):    """Get the third child.    """    return child_at(sxpr, 3, val)def child4(sxpr, val=None):    """Get the fourth child.    """    return child_at(sxpr, 4, val)def child_value(sxpr, elt, val=None):    """Get the value of the first child of the given element type.    Assumes the child has an atomic value.    sxpr sxpr    elt  element type    val  default value    """    kid = child(sxpr, elt)    if kid:        val = child_at(kid, 0, val)    return valdef has_id(sxpr, id):    """Test if an s-expression has a given id.    """    return attribute(sxpr, 'id') == iddef with_id(sxpr, id, val=None):    """Find the first s-expression with a given id, at any depth.    sxpr   s-exp or list    id     id    val    value if not found (default None)    return s-exp or val    """    if isinstance(sxpr, (types.ListType, types.TupleType)):        for n in sxpr:            if has_id(n, id):                val = n                break            v = with_id(n, id)            if v is None: continue            val = v            break    return valdef child_with_id(sxpr, id, val=None):    """Find the first child with a given id.    sxpr   s-exp or list    id     id    val    value if not found (default None)    return s-exp or val    """    if isinstance(sxpr, (types.ListType, types.TupleType)):        for n in sxpr:            if has_id(n, id):                val = n                break    return valdef elements(sxpr, ctxt=None):    """Generate elements (at any depth).    Visit elements in pre-order.    Values generated are (node, context)    The context is None if there is no parent, otherwise    (index, parent, context) where index is the node's index w.r.t its parent,    and context is the parent's context.    sxpr   s-exp    returns generator    """    yield (sxpr, ctxt)    i = 0    for n in children(sxpr):        if isinstance(n, (types.ListType, types.TupleType)):            # Calling elements() recursively does not generate recursively,            # it just returns a generator object. So we must iterate over it.            for v in elements(n, (i, sxpr, ctxt)):                yield v        i += 1def merge(s1, s2):    """Merge sxprs s1 and s2.    Returns an sxpr containing all the fields from s1 and s2, with    entries in s1 overriding s2. Recursively merges fields.    @param s1 sxpr    @param s2 sxpr    @return merged sxpr    """    if s1 is None:        val = s2    elif s2 is None:        val = s1    elif elementp(s1):        name1 = name(s1)        (m1, v1) = child_map(s1)        (m2, v2) = child_map(s2)        val = [name1]        for (k1, f1) in m1.items():            merge_list(val, f1, m2.get(k1, []))        for (k2, f2) in m2.items():            if k2 in m1: continue            val.extend(f2)        val.extend(v1)    else:        val = s1    return valdef merge_list(sxpr, l1, l2):    """Merge element lists l1 and l2 into sxpr.    The lists l1 and l2 are all element with the same name.    Values from l1 are merged with values in l2 and stored in sxpr.    If one list is longer than the other the excess values are used    as they are.    @param sxpr to merge into    @param l1 sxpr list    @param l2 sxpr list    @return modified sxpr    """    n1 = len(l1)    n2 = len(l2)    nmin = min(n1, n2)    for i in range(0, nmin):        sxpr.append(merge(l1[i], l2[i]))    for i in range(nmin, n1):        sxpr.append(l1[i])    for i in range(nmin, n2):        sxpr.append(l2[i])    return sxprdef child_map(sxpr):    """Get a dict of the elements in sxpr and a list of its values.    The dict maps element name to the list of elements with that name,    and the list is the non-element children.    @param sxpr    @return (dict, list)    """    m = {}    v = []    for x in children(sxpr):        if elementp(x):            n = name(x)            l = m.get(n, [])            l.append(x)            m[n] = l        else:            v.append(x)    return (m, v)def to_string(sxpr):    """Convert an sxpr to a string.    sxpr sxpr    returns string    """    io = StringIO()    show(sxpr, io)    io.seek(0)    val = io.getvalue()    io.close()    return valdef from_string(s):    """Create an sxpr by parsing a string.    s string    returns sxpr    """    if s == '':        return []    io = StringIO(s)    vals = parse(io)    if vals is []:        return None    else:        return vals[0]    def all_from_string(s):    """Create an sxpr list by parsing a string.    s string    returns sxpr list    """    io = StringIO(s)    vals = parse(io)    return valsdef parse(io):    """Completely parse all input from 'io'.    io	input file object    returns list of values, None if incomplete    raises ParseError on parse error    """    pin = Parser()    while 1:        buf = io.readline()        pin.input(buf)        if len(buf) == 0:            break    if pin.ready():        val = pin.get_all()    else:        val = None    return val   if __name__ == '__main__':    print ">main"    pin = Parser()    while 1:        buf = sys.stdin.read(1024)        #buf = sys.stdin.readline()        pin.input(buf)        while pin.ready():            val = pin.get_val()            print            print '****** val=', val        if len(buf) == 0:            break

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -