Example #1
0
def is_number(s):
    if s is True or s is False or is_null(s):
        return False

    try:
        s = float(s)
        return not isnan(s)
    except Exception:
        return False
Example #2
0
def scrub(result):
    if result == None:
        return None
    elif isinstance(result, text):
        if result == "null":
            return None
        return result
    elif isinstance(result, binary_type):
        return result.decode("utf8")
    elif isinstance(result, number_types):
        return result
    elif isinstance(result, dict) and not result:
        return result
    elif isinstance(result, list):
        output = [rr for r in result for rr in [scrub(r)]]

        if not output:
            return None
        elif len(output) == 1:
            return output[0]
        else:
            return scrub_literal(output)
    else:
        # ATTEMPT A DICT INTERPRETATION
        kv_pairs = list(result.items())
        output = {k: vv for k, v in kv_pairs for vv in [scrub(v)] if not is_null(vv)}
        if isinstance(result, dict):
            return output
        elif output:
            if debug.DEBUGGING:
                # CHECK THAT NO ITEMS WERE MISSED
                def look(r):
                    for token in r.tokens:
                        if isinstance(token, ParseResults):
                            if token.name:
                                continue
                            elif token.length() == 0:
                                continue
                            elif isinstance(token.type, Group):
                                Log.error(
                                    "This token is lost during scrub: {{token}}",
                                    token=token,
                                )
                            else:
                                look(token)
                        else:
                            Log.error(
                                "This token is lost during scrub: {{token}}",
                                token=token,
                            )

                look(result)

            return output
        temp = list(result)
        return scrub(temp)
Example #3
0
 def parseImpl(self, string, loc, doActions=True):
     try:
         result = self.expr._parse(string, loc, doActions)
         return ParseResults(self, result.start, result.end, [result])
     except Exception as cause:
         if is_null(self.expr):
             Log.warning(
                 "Ensure you have assigned a ParserElement (<<) to this Forward",
                 cause=cause,
             )
         raise cause
Example #4
0
def quote(value):
    """
    return JSON-quoted value
    :param value:
    :return:
    """
    if is_null(value):
        output = ""
    else:
        output = json.dumps(value)
    return output
Example #5
0
def listwrap(value):
    """
    PERFORMS THE FOLLOWING TRANSLATION
    None -> []
    value -> [value]
    [...] -> [...]  (unchanged list)
    """
    if is_null(value):
        return []
    elif isinstance(value, many_types):
        return value
    else:
        return [value]
Example #6
0
 def __eq__(self, other):
     if is_null(other):
         return not self.__bool__()
     elif is_text(other):
         try:
             return "".join(self) == other
         except Exception as e:
             return False
     elif is_many(other):
         return all(s == o for s, o in zip_longest(self, other))
     elif self.length() == 1:
         return self[0] == other
     elif not self:
         return False
     else:
         Log.error("do not know how to handle")