def multipleOf(self, arg): # You'd think we could do a mod here, but it has to work for # floats too apparently. So we check if dividing results in a # whole number instead return ( lambda v: r.do(v.div(arg), lambda r: r.floor() == r), 'must be a multiple of %s' % (arg, ), )
def check(v): if isinstance(items, dict): print('going to validate', items) return v.filter(lambda x: ~validate(items)(x)).is_empty() elif isinstance(items, list): return r.and_(*[ r.do(v.nth(i), validator) for i, validator in enumerate(map(validate, items)) ])
def test_do_three(self, conn): base = r.db('generic').table('table') result = r.do( base.get('one'), base.get('two'), base.get('three'), lambda d1, d2, d3: [d1['name'], d2['name'], d3['name']] ).run(conn) assertEqual(['One', 'Two', 'Three'], result)
def prop_check(v): props = [] for prop, prop_schema in arg.items(): sub_path = self.path + '/' + prop props.append( r.branch( v.has_fields(prop), r.do(v[prop], validate(prop_schema, sub_path)), True, )) return r.and_(*props)
def get_top_assets_by_s2vol(self, q, asset_num): return q.group('baseAsset')\ .map((r.row['close'], r.row['quoteAssetVolume'])).map(lambda x: { 'count': 1, 'sum': x[0], 'vol': x[1], 'diff': 0 })\ .reduce(lambda a, b: { 'count': r.add(a['count'], b['count']), 'sum': r.add(a['sum'], b['sum']), 'vol': r.add(a['vol'], b['vol']), 'diff': r.add( a['diff'], b['diff'], r.do( r.sub(a['sum'].div(a['count']), b['sum'].div(b['count'])), r.div(a['count'].mul(b['count']), a['count'].add(b['count'])), lambda avgdelta, weight: r.mul(avgdelta, avgdelta, weight) ) ) })\ .ungroup()\ .map(lambda g: { 'asset': g['group'], 'count': g['reduction']['count'], 'sum': g['reduction']['sum'], 'vol': g['reduction']['vol'], 's2': r.branch(g['reduction']['count'].gt(1), r.div(g['reduction']['diff'], g['reduction']['count'].sub(1)), 0) })\ .merge(lambda d: r.do( r.div(d['sum'], d['count']), r.mul(d['vol'], d['s2']), lambda avg, s2vol: { 'avg': avg, 's2vol': s2vol, }))\ .order_by(r.desc('s2vol'))\ .limit(asset_num)\ .pluck('asset')\ .map(lambda a:a['asset'])
def get_top_assets_by_s2(self, q, asset_num): return q.group('base_asset')\ .map(r.row['close']).map(lambda x: { 'count': 1, 'sum': x, 'min': x, 'max': x, 'diff': 0 # M2,n: sum((val-mean)^2) }).reduce(lambda a, b: { 'count': r.add(a['count'], b['count']), 'sum': r.add(a['sum'], b['sum']), 'min': r.branch(a['min'].lt(b['min']), a['min'], b['min']), 'max': r.branch(a['max'].gt(b['max']), a['max'], b['max']), 'diff': r.add( a['diff'], b['diff'], r.do( r.sub(a['sum'].div(a['count']), b['sum'].div(b['count'])), r.div(a['count'].mul(b['count']), a['count'].add(b['count'])), lambda avgdelta, weight: r.mul(avgdelta, avgdelta, weight) ) ) }).ungroup().map(lambda g: { 'asset': g['group'], 'count': g['reduction']['count'], 'sum': g['reduction']['sum'], 'min': g['reduction']['min'], 'max': g['reduction']['max'], 's2': r.branch(g['reduction']['count'].gt(1), r.div(g['reduction']['diff'], g['reduction']['count'].sub(1)), 0) }).merge(lambda d: r.do( r.div(d['sum'], d['count']), lambda avg: { 'avg': avg, }))\ .order_by(r.desc('s2'))\ .limit(asset_num)\ .pluck('asset')\ .map(lambda a:a['asset'])
def main(match, print_success): conn = r.connect() results = {} for testfilename in glob.glob(TESTFILES + match + '*.json'): name = basename(testfilename).rsplit('.', 1)[0] results[name] = {"passed": 0, "failed": 0} click.secho(name, bold=True) with open(testfilename) as testfile: test_definitions = json.load(testfile) for td in test_definitions: click.secho(' ' + td['description'], bold=True, fg='yellow') try: schema_filter = r.expr(validate(td['schema'])) except NotImplementedError as nie: num_failed = len(td['tests']) click.secho(' schema had unimplemented keyword "%s" ' 'autofailed %s tests' % ( nie.args[0], num_failed), fg='red', bold=True) results[name]['failed'] += num_failed continue for test in td['tests']: passed = False try: result = r.do(test['data'], schema_filter).run(conn) passed = result is test['valid'] except r.ReqlError as re: passed = test['valid'] is False click.echo(' ' + test['description'] + ': ', nl=False) if passed: click.secho('passed', fg='green') results[name]['passed'] += 1 if print_success: click.echo(' Data: ' + json.dumps(test['data'])) click.echo(' Schema: ' + json.dumps(td['schema'])) click.echo(' ReQL: ' + str(r.expr(schema_filter))) else: click.secho('failed', fg='red', bold=True) results[name]['failed'] += 1 click.echo(' Data: ' + json.dumps(test['data'])) click.echo(' Schema: ' + json.dumps(td['schema'])) click.echo(' ReQL: ' + str(r.expr(schema_filter))) summary(results)
def test_do_three(self, conn): base = r.db('generic').table('table') result = r.do( base.get('one'), base.get('two'), base.get('three'), lambda d1, d2, d3: [d1['name'], d2['name'], d3['name']]).run(conn) assertEqual(['One', 'Two', 'Three'], result)
def test_do_simple_2(self, conn): result = r.do( r.db('generic').table('table').get('two'), lambda d: d['name']).run(conn) assertEqual('Two', result)
def test_do_simple_2(self, conn): result = r.do(r.db('generic').table('table').get('two'), lambda d: d['name'] ).run(conn) assertEqual('Two', result)