Пример #1
0
def jpack(*args):
    """
    .. function:: jpack(args...) -> jpack

    Converts multiple input arguments into a single string. Jpacks preserve the types
    of their inputs and are based on JSON encoding. Single values are represented as
    themselves where possible.

    Examples:

    >>> sql("select jpack('a')")
    jpack('a')
    ----------
    a

    >>> sql("select jpack('a','b',3)")
    jpack('a','b',3)
    ----------------
    ["a","b",3]

    >>> sql("select jpack('a', jpack('b',3))")
    jpack('a', jpack('b',3))
    ------------------------
    ["a",["b",3]]

    """

    return jopts.toj(jopts.elemfromj(*args))
Пример #2
0
def jsonstrict(*args):
    """
    .. function:: jsonstrict(args...) -> json string

    Sometimes we wish to process json lists from another application. Jsonstrict function
    tries to always create json compatible lists. So it always returns json lists.

    Examples:

    >>> sql("select jsonstrict('a')")
    jsonstrict('a')
    ---------------
    ["a"]

    >>> sql("select jsonstrict('a','b',3)")
    jsonstrict('a','b',3)
    ---------------------
    ["a","b",3]

    >>> sql("select jsonstrict('a', jpack('b',3))")
    jsonstrict('a', jpack('b',3))
    -----------------------------
    ["a",["b",3]]

    """

    return json.dumps(jopts.elemfromj(*args),
                      separators=(',', ':'),
                      ensure_ascii=False)
Пример #3
0
def jlen(*args):
    """
    .. function:: jlen(args...) -> int

    Returns the total length in elements of the input jpacks.

    Examples:

    >>> sql("select jlen('abc')")
    jlen('abc')
    -----------
    1

    >>> sql("select jlen('a','b',3)")
    jlen('a','b',3)
    ---------------
    3

    >>> sql("select jlen('a', jpack('b',3))")
    jlen('a', jpack('b',3))
    -----------------------
    3

    >>> sql("select jlen('[1,2,3]')")
    jlen('[1,2,3]')
    ---------------
    3

    """
    return sum([
        len(x) if type(x) in (dict, list) else 1
        for x in (jopts.elemfromj(*args))
    ])
Пример #4
0
def jlen(*args):

    """
    .. function:: jlen(args...) -> int

    Returns the total length in elements of the input jpacks.

    Examples:

    >>> sql("select jlen('abc')")
    jlen('abc')
    -----------
    1

    >>> sql("select jlen('a','b',3)")
    jlen('a','b',3)
    ---------------
    3

    >>> sql("select jlen('a', jpack('b',3))")
    jlen('a', jpack('b',3))
    -----------------------
    3

    >>> sql("select jlen('[1,2,3]')")
    jlen('[1,2,3]')
    ---------------
    3

    """
    return sum([len(x) if type(x) in (dict,list) else 1 for x in (jopts.elemfromj(*args))])
Пример #5
0
def jsonstrict(*args):

    """
    .. function:: jsonstrict(args...) -> json string

    Sometimes we wish to process json lists from another application. Jsonstrict function
    tries to always create json compatible lists. So it always returns json lists.

    Examples:

    >>> sql("select jsonstrict('a')")
    jsonstrict('a')
    ---------------
    ["a"]

    >>> sql("select jsonstrict('a','b',3)")
    jsonstrict('a','b',3)
    ---------------------
    ["a","b",3]

    >>> sql("select jsonstrict('a', jpack('b',3))")
    jsonstrict('a', jpack('b',3))
    -----------------------------
    ["a",["b",3]]

    """

    return json.dumps(jopts.elemfromj(*args), separators=(',',':'), ensure_ascii=False)
Пример #6
0
def jpack(*args):
    """
    .. function:: jpack(args...) -> jpack

    Converts multiple input arguments into a single string. Jpacks preserve the types
    of their inputs and are based on JSON encoding. Single values are represented as
    themselves where possible.

    Examples:

    >>> sql("select jpack('a')")
    jpack('a')
    ----------
    a

    >>> sql("select jpack('a','b',3)")
    jpack('a','b',3)
    ----------------
    ["a","b",3]

    >>> sql("select jpack('a', jpack('b',3))")
    jpack('a', jpack('b',3))
    ------------------------
    ["a",["b",3]]

    """

    return jopts.toj(jopts.elemfromj(*args))
Пример #7
0
def jzip(*args):
    """
    .. function:: jzip(args...) -> json string

    It combines the corresponding elements of input jpacks.

    Examples:

    >>> sql('''select jzip('["a", "b"]', '[1,2]','[4,5]')''')
    jzip('["a", "b"]', '[1,2]','[4,5]')
    -----------------------------------
    [["a",1,4],["b",2,5]]

    """
    return json.dumps([list(x) for x in zip(*jopts.elemfromj(*args))], separators=(',', ':'), ensure_ascii=False)
Пример #8
0
def jzipdict(*args):

    """
    .. function:: jzipdict(args...) -> json string

    It combines the correspinding elements of input jpacks into a jdict.

    Examples:

    >>> sql('''select jzipdict('["a", "b"]', '[1,2]','[4,5]')''')
    jzipdict('["a", "b"]', '[1,2]','[4,5]')
    ---------------------------------------
    {"a":[1,4],"b":[2,5]}

    """
    return json.dumps(dict(tuple([x[0], x[1:]]) for x in zip(*jopts.elemfromj(*args))), separators=(',',':'), ensure_ascii=False)
Пример #9
0
def jzip(*args):

    """
    .. function:: jzip(args...) -> json string

    It combines the corresponding elements of input jpacks.

    Examples:

    >>> sql('''select jzip('["a", "b"]', '[1,2]','[4,5]')''')
    jzip('["a", "b"]', '[1,2]','[4,5]')
    -----------------------------------
    [["a",1,4],["b",2,5]]

    """
    return json.dumps([list(x) for x in zip(*jopts.elemfromj(*args))], separators=(',',':'), ensure_ascii=False)
Пример #10
0
def jflatten(*args):
    """
    .. function:: jflattten(jpacks) -> jpack

    Flattens all nested sub-jpacks.

    Examples:

    >>> sql(''' select jflatten('1', '[2]') ''') # doctest: +NORMALIZE_WHITESPACE
    jflatten('1', '[2]')
    --------------------
    ["1",2]

    >>> sql(''' select jflatten('[["word1", 1], ["word2", 1], [["word3", 2], ["word4", 2]], 3]') ''') # doctest: +NORMALIZE_WHITESPACE
    jflatten('[["word1", 1], ["word2", 1], [["word3", 2], ["word4", 2]], 3]')
    -------------------------------------------------------------------------
    ["word1",1,"word2",1,"word3",2,"word4",2,3]

    """

    return jopts.toj(jopts.flatten(jopts.elemfromj(*args)))
Пример #11
0
def jflatten(*args):
    """
    .. function:: jflattten(jpacks) -> jpack

    Flattens all nested sub-jpacks.

    Examples:

    >>> sql(''' select jflatten('1', '[2]') ''') # doctest: +NORMALIZE_WHITESPACE
    jflatten('1', '[2]')
    --------------------
    ["1",2]

    >>> sql(''' select jflatten('[["word1", 1], ["word2", 1], [["word3", 2], ["word4", 2]], 3]') ''') # doctest: +NORMALIZE_WHITESPACE
    jflatten('[["word1", 1], ["word2", 1], [["word3", 2], ["word4", 2]], 3]')
    -------------------------------------------------------------------------
    ["word1",1,"word2",1,"word3",2,"word4",2,3]

    """

    return jopts.toj(jopts.flatten(jopts.elemfromj(*args)))
Пример #12
0
 def step(self, *args):
     if len(args) == 1:
         self.outgroup += (jopts.elemfromj(args[0]))
     else:
         self.outgroup.append(jopts.elemfromj(*args))
Пример #13
0
 def step(self, *args):
     if len(args) == 1:
         self.outgroup += (jopts.elemfromj(args[0]))
     else:
         self.outgroup.append(jopts.elemfromj(*args))