def jexcept(*args): """ .. function:: jexcept(jpackA, jpackB) -> jpack Returns the items of jpackA except the items that appear on jpackB. Examples: >>> sql("select jexcept('[1,2,3]', '[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE jexcept('[1,2,3]', '[1,2,3]') ----------------------------- [] >>> sql("select jexcept('[1,2,3]', '[1,3]')") # doctest: +NORMALIZE_WHITESPACE jexcept('[1,2,3]', '[1,3]') --------------------------- 2 """ if len(args) < 2: raise functions.OperatorError("jexcept","operator needs at least two inputs") b = set(jopts.fromj(args[1])) return jopts.toj([x for x in jopts.fromj(args[0]) if x not in b])
def jexcept(*args): """ .. function:: jexcept(jpackA, jpackB) -> jpack Returns the items of jpackA except the items that appear on jpackB. Examples: >>> sql("select jexcept('[1,2,3]', '[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE jexcept('[1,2,3]', '[1,2,3]') ----------------------------- [] >>> sql("select jexcept('[1,2,3]', '[1,3]')") # doctest: +NORMALIZE_WHITESPACE jexcept('[1,2,3]', '[1,3]') --------------------------- 2 """ if len(args) < 2: raise functions.OperatorError("jexcept", "operator needs at least two inputs") b = set(jopts.fromj(args[1])) return jopts.toj([x for x in jopts.fromj(args[0]) if x not in b])
def step(self, *args): if self.outgroup == None: self.outgroup = OrderedDict([(x, None) for x in jopts.fromj(args[0])]) self.outset = set(self.outgroup) for jp in args: for i in self.outset.difference(jopts.fromj(jp)): del (self.outgroup[i]) self.outset = set(self.outgroup)
def j2nl(*args): """ .. function:: j2nl(jpack) -> text Converts multiple input jpacks to a newline separated text. Examples: >>> sql("select j2nl('[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE j2nl('[1,2,3]') --------------- 1 2 3 >>> sql("select j2nl('[1,2,3]','a')") # doctest: +NORMALIZE_WHITESPACE j2nl('[1,2,3]','a') ------------------- 1 2 3 a >>> sql("select j2nl('a', 'b')") # doctest: +NORMALIZE_WHITESPACE j2nl('a', 'b') -------------- a b """ return '\n'.join([unicode(x) for x in jopts.fromj(*args)])
def jfilterempty(*args): """ .. function:: jfilterempty(jpacks.) -> jpack Removes from input jpacks all empty elements. Examples: >>> sql("select jfilterempty('a', '', '[]')") jfilterempty('a', '', '[]') --------------------------- a >>> sql("select jfilterempty('a','[null]',3)") jfilterempty('a','[null]',3) ---------------------------- ["a",3] >>> sql("select jfilterempty('[3]', jpack('b', ''))") jfilterempty('[3]', jpack('b', '')) ----------------------------------- [3,"b"] """ return jopts.toj([x for x in jopts.fromj(*args) if x!='' and x!=[] and x!=None])
def j2t(*args): """ .. function:: j2t(jpack) -> tabpack Converts multiple input jpacks to a tab separated pack (tab separated values). If tab or newline characters are found in the source jpack they are converted to spaces. Examples: >>> sql("select j2t('[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE j2t('[1,2,3]') -------------- 1 2 3 >>> sql("select j2t('[1,2,3]','a')") # doctest: +NORMALIZE_WHITESPACE j2t('[1,2,3]','a') ------------------ 1 2 3 a >>> sql("select j2t('a', 'b')") # doctest: +NORMALIZE_WHITESPACE j2t('a', 'b') ------------- a b """ return '\t'.join([ unicode(x).replace('\t', ' ').replace('\n', ' ') for x in jopts.fromj(*args) ])
def jfilterempty(*args): """ .. function:: jfilterempty(jpacks.) -> jpack Removes from input jpacks all empty elements. Examples: >>> sql("select jfilterempty('a', '', '[]')") jfilterempty('a', '', '[]') --------------------------- a >>> sql("select jfilterempty('a','[null]',3)") jfilterempty('a','[null]',3) ---------------------------- ["a",3] >>> sql("select jfilterempty('[3]', jpack('b', ''))") jfilterempty('[3]', jpack('b', '')) ----------------------------------- [3,"b"] """ return jopts.toj( [x for x in jopts.fromj(*args) if x != '' and x != [] and x != None])
def jintersection(*args): """ .. function:: jintersection(jpackA, jpackB) -> jpack Returns the items of jpackA except the items that appear on jpackB. Examples: >>> sql("select jintersection('[1,2,3]', '[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE jintersection('[1,2,3]', '[1,2,3]') ----------------------------------- [1,2,3] >>> sql("select jintersection('[1,2,3]', '[1,3]', 1)") # doctest: +NORMALIZE_WHITESPACE jintersection('[1,2,3]', '[1,3]', 1) ------------------------------------ 1 """ if len(args) < 2: raise functions.OperatorError("jintersection", "operator needs at least two inputs") return jopts.toj( sorted(set.intersection(*[set(jopts.fromj(x)) for x in args])))
def jsplitv(*args): """ .. function:: jsplitv(jpacks) -> [C1] Splits vertically a jpack. Examples: >>> sql("select jsplitv(jmerge('[1,2,3]', '[1,2,3]', 'b', 'a', 3 ))") # doctest: +NORMALIZE_WHITESPACE C1 -- 1 2 3 1 2 3 b a 3 """ yield ('C1', ) for j1 in jopts.fromj(*args): yield [jopts.toj(j1)]
def jmergeregexpnamed(*args): """ .. function:: jmergeregexpnamed(jpacks) -> jpack Creates a regular expression that matches all of the jpack's contents with named groups. If the number of named groups in a regular expression is greater than 99, then the output will be a jpack of regular expressions. Examples: >>> sql(''' select jmergeregexpnamed('["abc", "def"]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexpnamed('["abc", "def"]') ----------------------------------- (abc)|(def) """ inp = jopts.fromj(*args) inp.sort() out = [] for g in xrange(0, len(inp), 99): out.append('|'.join('('+x+')' for x in inp[g:g+99])) return jopts.toj(out)
def j2s(*args): """ .. function:: j2s(jpack) -> space separated string Converts multiple input jpacks to a space separated string. Newlines are converted to spaces. Examples: >>> sql("select j2s('[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE j2s('[1,2,3]') -------------- 1 2 3 >>> sql("select j2s('[1,2,3]','a')") # doctest: +NORMALIZE_WHITESPACE j2s('[1,2,3]','a') ------------------ 1 2 3 a >>> sql("select j2s('a', 'b')") # doctest: +NORMALIZE_WHITESPACE j2s('a', 'b') ------------- a b """ return ' '.join( [unicode(x).replace('\n', ' ') for x in jopts.fromj(*args)])
def j2t(*args): """ .. function:: j2t(jpack) -> tabpack Converts multiple input jpacks to a tab separated pack (tab separated values). If tab or newline characters are found in the source jpack they are converted to spaces. Examples: >>> sql("select j2t('[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE j2t('[1,2,3]') -------------- 1 2 3 >>> sql("select j2t('[1,2,3]','a')") # doctest: +NORMALIZE_WHITESPACE j2t('[1,2,3]','a') ------------------ 1 2 3 a >>> sql("select j2t('a', 'b')") # doctest: +NORMALIZE_WHITESPACE j2t('a', 'b') ------------- a b """ return '\t'.join([ unicode(x).replace('\t', ' ').replace('\n',' ') for x in jopts.fromj(*args) ])
def j2s(*args): """ .. function:: j2s(jpack) -> space separated string Converts multiple input jpacks to a space separated string. Newlines are converted to spaces. Examples: >>> sql("select j2s('[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE j2s('[1,2,3]') -------------- 1 2 3 >>> sql("select j2s('[1,2,3]','a')") # doctest: +NORMALIZE_WHITESPACE j2s('[1,2,3]','a') ------------------ 1 2 3 a >>> sql("select j2s('a', 'b')") # doctest: +NORMALIZE_WHITESPACE j2s('a', 'b') ------------- a b """ return ' '.join([ unicode(x).replace('\n',' ') for x in jopts.fromj(*args) ])
def jaccard(*args): """ .. function:: jaccard(jpack1,jpack2) Return jaccard similarity value of two jpacks. Example: >>> table1(''' ... user1 movie1 20 ... user1 movie2 30 ... user2 movie1 40 ... user2 movie3 90 ... user2 movie4 90 ... user3 movie1 40 ... user3 movie3 80 ... user4 movie1 70 ... user4 movie2 10 ... ''') NOTE that only column b is jgrouped because *jaccard* operates on packs as sets, not weighted values, So packing also column c would not make any difference. >>> sql(\"""select u1.userid,u2.userid, jaccard(u1.pk, u2.pk) as similarity ... from ... (select a as userid, jgroup(b) as pk from table1 group by a) as u1, ... (select a as userid, jgroup(b) as pk from table1 group by a) as u2 ... where u1.userid<u2.userid\""") userid | userid | similarity -------------------------------- user1 | user2 | 0.25 user1 | user3 | 0.333333333333 user1 | user4 | 1.0 user2 | user3 | 0.666666666667 user2 | user4 | 0.25 user3 | user4 | 0.333333333333 """ if len(args) != 2: raise functions.OperatorError("jaccard", "operator takes exactly two arguments") try: r = jopts.fromj(args[0]) s = jopts.fromj(args[1]) except Exception, e: raise functions.OperatorError("jaccard", " Wrong format arguments: %s" % (e))
def jaccard(*args): """ .. function:: jaccard(jpack1,jpack2) Return jaccard similarity value of two jpacks. Example: >>> table1(''' ... user1 movie1 20 ... user1 movie2 30 ... user2 movie1 40 ... user2 movie3 90 ... user2 movie4 90 ... user3 movie1 40 ... user3 movie3 80 ... user4 movie1 70 ... user4 movie2 10 ... ''') NOTE that only column b is jgrouped because *jaccard* operates on packs as sets, not weighted values, So packing also column c would not make any difference. >>> sql(\"""select u1.userid,u2.userid, jaccard(u1.pk, u2.pk) as similarity ... from ... (select a as userid, jgroup(b) as pk from table1 group by a) as u1, ... (select a as userid, jgroup(b) as pk from table1 group by a) as u2 ... where u1.userid<u2.userid\""") userid | userid | similarity -------------------------------- user1 | user2 | 0.25 user1 | user3 | 0.333333333333 user1 | user4 | 1.0 user2 | user3 | 0.666666666667 user2 | user4 | 0.25 user3 | user4 | 0.333333333333 """ if len(args)!=2: raise functions.OperatorError("jaccard","operator takes exactly two arguments") try: r=jopts.fromj(args[0]) s=jopts.fromj(args[1]) except Exception,e: raise functions.OperatorError("jaccard"," Wrong format arguments: %s" %(e))
def final(self): yield tuple('c'+str(i) for i in xrange(1,self.numofargs)) for groupkey, sumcols in self.groupsdict.iteritems(): cols = list(groupkey) for col in sumcols: try: cols.append(sum(col)) except TypeError: cols.append(jopts.toj(sorted(set( jopts.fromj(*col) )))) yield cols
def final(self): yield tuple('c' + str(i) for i in xrange(1, self.numofargs)) for groupkey, sumcols in self.groupsdict.iteritems(): cols = list(groupkey) for col in sumcols: try: cols.append(sum(col)) except TypeError: cols.append(jopts.toj(sorted(set(jopts.fromj(*col))))) yield cols
def jmergeregexp(*args): """ .. function:: jmergeregexp(jpacks) -> jpack Creates a regular expression that matches all of the jpack's contents. If the input jpack contains keyword pairs, then jmergeregexp returns a regular expression with named groups. Examples: >>> sql(''' select jmergeregexp('["abc", "def"]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexp('["abc", "def"]') ------------------------------ (?:abc)|(?:def) >>> sql(''' select jmergeregexp('[["pos", "p1"], ["neg", "n1"], ["pos", "p2"]]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexp('[["pos", "p1"], ["neg", "n1"], ["pos", "p2"]]') ------------------------------------------------------------- (?P<neg>n1)|(?P<pos>p1|p2) >>> sql(''' select jmergeregexp('[]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexp('[]') ------------------ _^ >>> sql(''' select jmergeregexp('["ab",""]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexp('["ab",""]') ------------------------- (?:ab) """ inp = jopts.fromj(*args) if len(inp) > 0 and type(inp[0]) == list: out = {} for x, y in inp: if x not in out: out[x] = [y] else: out[x].append(y) res = '|'.join('(?P<' + x + '>' + '|'.join(y) + ')' for x, y in out.iteritems() if y != '') if res == '': res = '_^' return res res = '|'.join('(?:' + x + ')' for x in inp if x != '') if res == '': res = '_^' return res
def jmergeregexp(*args): """ .. function:: jmergeregexp(jpacks) -> jpack Creates a regular expression that matches all of the jpack's contents. If the input jpack contains keyword pairs, then jmergeregexp returns a regular expression with named groups. Examples: >>> sql(''' select jmergeregexp('["abc", "def"]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexp('["abc", "def"]') ------------------------------ (?:abc)|(?:def) >>> sql(''' select jmergeregexp('[["pos", "p1"], ["neg", "n1"], ["pos", "p2"]]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexp('[["pos", "p1"], ["neg", "n1"], ["pos", "p2"]]') ------------------------------------------------------------- (?P<neg>n1)|(?P<pos>p1|p2) >>> sql(''' select jmergeregexp('[]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexp('[]') ------------------ _^ >>> sql(''' select jmergeregexp('["ab",""]') ''') # doctest: +NORMALIZE_WHITESPACE jmergeregexp('["ab",""]') ------------------------- (?:ab) """ inp = jopts.fromj(*args) if len(inp)>0 and type(inp[0]) == list: out={} for x,y in inp: if x not in out: out[x] = [y] else: out[x].append(y) res = '|'.join('(?P<'+ x + '>' + '|'.join(y)+')' for x, y in out.iteritems() if y!='') if res == '': res = '_^' return res res = '|'.join('(?:'+x+')' for x in inp if x!='') if res == '': res = '_^' return res
def jpermutations(*args): """ .. function:: jpermutations(jpack, r) -> multiset Returns all length r permutations of jpack. Examples: >>> sql('''select jpermutations('["t1","t2","t3"]',2)''') C1 | C2 ------- t1 | t2 t1 | t3 t2 | t1 t2 | t3 t3 | t1 t3 | t2 >>> sql('''select jpermutations('["t1","t2",["t3","t4"]]',2)''') C1 | C2 ------------------------- t1 | t2 t1 | ["t3","t4"] t2 | t1 t2 | ["t3","t4"] ["t3","t4"] | t1 ["t3","t4"] | t2 >>> sql('''select jpermutations(null,2)''') C1 | C2 ----- >>> sql('''select jpermutations('["t1","t2","t3","t4"]')''') C1 -- t1 t2 t3 t4 """ r=1 if len(args)==2: r=args[1] yield tuple(('C'+str(x) for x in xrange(1,r+1))) for p in itertools.permutations(jopts.fromj(args[0]), r): yield [jopts.toj(x) for x in p]
def jmerge(*args): """ .. function:: jmerge(jpacks) -> jpack Merges multiple jpacks into one jpack. Examples: >>> sql("select jmerge('[1,2,3]', '[1,2,3]', 'a', 3 )") # doctest: +NORMALIZE_WHITESPACE jmerge('[1,2,3]', '[1,2,3]', 'a', 3 ) ------------------------------------- [1,2,3,1,2,3,"a",3] """ return jopts.toj(jopts.fromj(*args))
def jsort(*args): """ .. function:: jsort(jpacks) -> jpack Sorts the input jpacks. Examples: >>> sql("select jsort('[1,2,3]', '[1,2,3]', 'b', 'a', 3 )") # doctest: +NORMALIZE_WHITESPACE jsort('[1,2,3]', '[1,2,3]', 'b', 'a', 3 ) ----------------------------------------- [1,1,2,2,3,3,3,"a","b"] """ return jopts.toj(sorted(jopts.fromj(*args)))
def jset(*args): """ .. function:: jset(jpacks) -> jpack Returns a set representation of a jpack, unifying duplicate items. Examples: >>> sql("select jset('[1,2,3]', '[1,2,3]', 'b', 'a', 3 )") # doctest: +NORMALIZE_WHITESPACE jset('[1,2,3]', '[1,2,3]', 'b', 'a', 3 ) ---------------------------------------- [1,2,3,"a","b"] """ return jopts.toj(sorted(set(jopts.fromj(*args))))
def jpermutations(*args): """ .. function:: jpermutations(jpack, r) -> multiset Returns all length r permutations of jpack. Examples: >>> sql('''select jpermutations('["t1","t2","t3"]',2)''') C1 | C2 ------- t1 | t2 t1 | t3 t2 | t1 t2 | t3 t3 | t1 t3 | t2 >>> sql('''select jpermutations('["t1","t2",["t3","t4"]]',2)''') C1 | C2 ------------------------- t1 | t2 t1 | ["t3","t4"] t2 | t1 t2 | ["t3","t4"] ["t3","t4"] | t1 ["t3","t4"] | t2 >>> sql('''select jpermutations(null,2)''') >>> sql('''select jpermutations('["t1","t2","t3","t4"]')''') C1 -- t1 t2 t3 t4 """ r=1 if len(args)==2: r=args[1] yield tuple(('C'+str(x) for x in xrange(1,r+1))) for p in itertools.permutations(jopts.fromj(args[0]), r): yield [jopts.toj(x) for x in p]
def jsplit(*args): """ .. function:: jsplit(jpacks) -> [C1, C2, ...] Splits horizontally a jpack. Examples: >>> sql("select jsplit('[1,2,3]', '[3,4,5]')") # doctest: +NORMALIZE_WHITESPACE C1 | C2 | C3 | C4 | C5 | C6 --------------------------- 1 | 2 | 3 | 3 | 4 | 5 """ fj = [jopts.toj(x) for x in jopts.fromj(*args)] if fj == []: yield ('C1', ) yield tuple(['C' + str(x) for x in xrange(1, len(fj) + 1)]) yield fj
def jsplit(*args): """ .. function:: jsplit(jpacks) -> [C1, C2, ...] Splits horizontally a jpack. Examples: >>> sql("select jsplit('[1,2,3]', '[3,4,5]')") # doctest: +NORMALIZE_WHITESPACE C1 | C2 | C3 | C4 | C5 | C6 --------------------------- 1 | 2 | 3 | 3 | 4 | 5 """ fj = [jopts.toj(x) for x in jopts.fromj(*args)] if fj == []: yield ('C1',) yield tuple(['C' + str(x) for x in xrange(1, len(fj) + 1)]) yield fj
def jlengthiest(*args): """ .. function:: jlengthiest(jpacks.) -> jpack Returns the string with the greatest length contained in the jpacks. Examples: >>> sql("select jlengthiest('a', '', '[]')") jlengthiest('a', '', '[]') -------------------------- a >>> sql("select jlengthiest('a','longer',3)") jlengthiest('a','longer',3) --------------------------- longer >>> sql("select jlengthiest('[3]', jpack('b', ''))") jlengthiest('[3]', jpack('b', '')) ---------------------------------- 3 """ maxlen=-1 res=None for i in (x for x in jopts.fromj(*args)): if i == None: l=-1 else: l = len(unicode(i)) if l > maxlen: maxlen = l res = i return res
def jlengthiest(*args): """ .. function:: jlengthiest(jpacks.) -> jpack Returns the string with the greatest length contained in the jpacks. Examples: >>> sql("select jlengthiest('a', '', '[]')") jlengthiest('a', '', '[]') -------------------------- a >>> sql("select jlengthiest('a','longer',3)") jlengthiest('a','longer',3) --------------------------- longer >>> sql("select jlengthiest('[3]', jpack('b', ''))") jlengthiest('[3]', jpack('b', '')) ---------------------------------- 3 """ maxlen = -1 res = None for i in (x for x in jopts.fromj(*args)): if i == None: l = -1 else: l = len(unicode(i)) if l > maxlen: maxlen = l res = i return res
def jintersection(*args): """ .. function:: jintersection(jpackA, jpackB) -> jpack Returns the items of jpackA except the items that appear on jpackB. Examples: >>> sql("select jintersection('[1,2,3]', '[1,2,3]')") # doctest: +NORMALIZE_WHITESPACE jintersection('[1,2,3]', '[1,2,3]') ----------------------------------- [1,2,3] >>> sql("select jintersection('[1,2,3]', '[1,3]', 1)") # doctest: +NORMALIZE_WHITESPACE jintersection('[1,2,3]', '[1,3]', 1) ------------------------------------ 1 """ if len(args) < 2: raise functions.OperatorError("jintersection","operator needs at least two inputs") return jopts.toj(sorted(set.intersection(*[set(jopts.fromj(x)) for x in args])))
def step(self, *args): if self.k is None: self.gset.update([(x, None) for x in jopts.fromj(args[0])]) if len(self.gset) >= args[-1]: self.k = args[1]
def step(self, *args): self.outgroupupdate([(x, None) for x in jopts.fromj(*args)])
def jsplice(*args): """ .. function:: jsplice(jpack, range1_start, range1_end, ...) -> jpack Splices input jpack. If only a single range argument is provided, it returns input jpack's element in provided position. If defined position index is positive, then it starts counting from the beginning of input jpack. If defined position is negative it starts counting from the end of input jpack. If more than one range arguments are provided, then the arguments are assumed to be provided in pairs (start, end) that define ranges inside the input jpack that should be joined together in output jpack. Examples: >>> sql(''' select jsplice('[1,2,3,4,5]',0) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]',0) ------------------------ 1 >>> sql(''' select jsplice('[1,2,3,4,5]',-1) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]',-1) ------------------------- 5 >>> sql(''' select jsplice('[1,2,3,4,5]',10) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]',10) ------------------------- None >>> sql(''' select jsplice('[1,2,3,4,5]', 0, 3, 0, 2) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]', 0, 3, 0, 2) ---------------------------------- [1,2,3,1,2] >>> sql(''' select jsplice('[1,2,3,4,5]', 2, -1) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]', 2, -1) ----------------------------- [3,4] """ largs = len(args) if largs == 1: return args[0] fj = jopts.fromj(args[0]) if largs == 2: try: return jopts.toj(fj[args[1]]) except KeyboardInterrupt: raise except: return None outj = [] for i in xrange(1, largs, 2): try: outj += fj[args[i]:args[i + 1]] except KeyboardInterrupt: raise except: pass return jopts.toj(outj)
def jsplice(*args): """ .. function:: jsplice(jpack, range1_start, range1_end, ...) -> jpack Splices input jpack. If only a single range argument is provided, it returns input jpack's element in provided position. If defined position index is positive, then it starts counting from the beginning of input jpack. If defined position is negative it starts counting from the end of input jpack. If more than one range arguments are provided, then the arguments are assumed to be provided in pairs (start, end) that define ranges inside the input jpack that should be joined together in output jpack. Examples: >>> sql(''' select jsplice('[1,2,3,4,5]',0) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]',0) ------------------------ 1 >>> sql(''' select jsplice('[1,2,3,4,5]',-1) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]',-1) ------------------------- 5 >>> sql(''' select jsplice('[1,2,3,4,5]',10) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]',10) ------------------------- None >>> sql(''' select jsplice('[1,2,3,4,5]', 0, 3, 0, 2) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]', 0, 3, 0, 2) ---------------------------------- [1,2,3,1,2] >>> sql(''' select jsplice('[1,2,3,4,5]', 2, -1) ''') # doctest: +NORMALIZE_WHITESPACE jsplice('[1,2,3,4,5]', 2, -1) ----------------------------- [3,4] """ largs=len(args) if largs==1: return args[0] fj=jopts.fromj(args[0]) if largs==2: try: return jopts.toj(fj[args[1]]) except KeyboardInterrupt: raise except: return None outj=[] for i in xrange(1,largs,2): try: outj+=fj[args[i]:args[i+1]] except KeyboardInterrupt: raise except: pass return jopts.toj(outj)