def test_multiple_X(): assert (X.y + X.x)(Row(x=2, y=3)) == 5 assert (X['x'] + X['y'])(dict(x=2, y=3)) == 5 assert (X + X)(5) == 10 assert (X - X)(5) == 0 assert (X * X)(5) == 25 assert (X / X)(5) == 1 assert (X.y / X.x)(Row(x=2, y=3)) == 1.5 assert (X.y // X.x)(Row(x=2, y=3)) == 1 assert (X.y % X.x)(Row(x=3, y=5)) == 2 assert (divmod(X.y, X.x))(Row(x=3, y=5)) == (1, 2) assert (X**X)(3) == 27
def test_basic(): assert X.y(Row(x=2, y=3)) == 3 assert X['x'](dict(x=4, y=5)) == 4 assert (X + 3)(5) == 8 assert (X - 2)(6) == 4 assert (X * 3)(4) == 12 assert (X / 2)(9) == 4.5 assert (X // 2)(9) == 4 assert (X % 3)(5) == 2 assert (divmod(X, 3))(5) == (1, 2) assert (X**2)(4) == 16 assert (X == 3)(3) == True assert (3 == X)(4) == False assert (X == 3)(4) == False assert (3 == X)(3) == True assert (X != 3)(4) == True assert (3 != X)(3) == False assert (X > 3)(4) == True assert (X > 3)(2) == False assert (3 > X)(4) == False assert (3 > X)(2) == True assert (X < 3)(4) == False assert (X < 3)(2) == True assert (X >= 3)(3) == True assert (X >= 3)(2) == False assert (X <= 3)(3) == True assert (X <= 3)(2) == True
def chunk_tr(self_): self_ = iter(self_) while True: row = Row.from_values(itt.islice(self_, n)) if len(row) == 0 or strict and len(row) != n: break yield row return self_
def repeat(cls, elems, times=None): '''Create a StreamTable repeating elems >>> StreamTable.repeat(1, 3).show() | repeat | |----------| | 1 | | 1 | | 1 | ''' strm = Stream.repeat(elems, times).map(lambda elem: Row(repeat=elem)) return cls(strm)
def range(cls, start, end=None, step=1): '''Create a StreamTable from range >>> StreamTable.range(1, 10, 3).show() | range | |---------| | 1 | | 4 | | 7 | ''' strm = Stream.range(start, end, step).map(lambda elem: Row(range=elem)) return cls(strm)
def count(cls, start, step=1): '''Create a inifinite consecutive StreamTable >>> StreamTable.count(3, 5).take(3).show() | count | |---------| | 3 | | 8 | | 13 | ''' strm = Stream.count(start, step).map(lambda elem: Row(count=elem)) return cls(strm)
def cycle(cls, iterable): '''Create a StreamTable cycling a iterable >>> StreamTable.cycle([1,2]).take(5).show() | cycle | |---------| | 1 | | 2 | | 1 | | 2 | | 1 | ''' strm = Stream.cycle(iterable).map(lambda elem: Row(cycle=elem)) return cls(strm)
def iterate(cls, func, x): '''Create a StreamTable recursively applying a function to last return value. >>> def multiply2(x): return x * 2 >>> StreamTable.iterate(multiply2, 3).take(4).show() | iterate | |-----------| | 3 | | 6 | | 12 | | 24 | ''' strm = Stream.iterate(func, x).map(lambda elem: Row(iterate=elem)) return cls(strm)
def handle(self): self.email_id_mappings = (Stream( self.argument('email_id_mappings')).map(lambda s: Row.from_values( s.split(':', 1), fields=('email', 'trello_id'))).to_list()) first_updated_min = (Optional.from_value( self.option('first-updated-min')).map(pdl.parse).get_or_none()) self.sync(first_updated_min) now = pdl.now(tz='Asia/Taipei') now = now.set(second=0, microsecond=0) last_minute = now.subtract(minutes=10) while True: last_minute = last_minute.add(minutes=1) self.sync(last_minute) time.sleep(60)
def repeatedly(cls, func, times=None): '''Create a StreamTable repeatedly calling a zero parameter function >>> def counter(): ... counter.num += 1 ... return counter.num >>> counter.num = -1 >>> StreamTable.repeatedly(counter, 5).show() | repeatedly | |--------------| | 0 | | 1 | | 2 | | 3 | | 4 | ''' strm = Stream.repeatedly(func, times).map( lambda elem: Row(repeatedly=elem)) return cls(strm)
def extract_fields_scores(expl): return (Stream( expl['_explanation']['details'][0]['details']).map(lambda d: Row( field=ugly_extract_fieldname(d['details'][0]['description']), score=d['value'])).to_map())
def display_cat_id_map(): cat_id_cat_stbl = StreamTable([ Row(cat_id=cat_id, cat=cat) for cat_id, cat in cat_id_to_cat_map.items() ]) cat_id_cat_stbl.show(len(cat_id_to_cat_map))