本文整理汇总了Python中yaql.create_context函数的典型用法代码示例。如果您正苦于以下问题:Python create_context函数的具体用法?Python create_context怎么用?Python create_context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了create_context函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_int_bool_resolving
def test_int_bool_resolving(self):
@parameter('param', arg_type=types.IntType)
def int_func(param):
return "int: " + str(param)
@parameter('param', arg_type=types.BooleanType)
def bool_func(param):
return "bool: " + str(param)
context1 = yaql.create_context(False)
context2 = yaql.create_context(False)
context3 = yaql.create_context(False)
context4 = yaql.create_context(False)
context1.register_function(int_func, 'foo')
context2.register_function(bool_func, 'foo')
context3.register_function(int_func, 'foo')
context3.register_function(bool_func, 'foo')
context4.register_function(bool_func, 'foo')
context4.register_function(int_func, 'foo')
self.assertEquals("int: 1", self.eval('foo(1)', context=context1))
self.assertEquals("int: 0", self.eval('foo(0)', context=context1))
self.assertRaises(Exception, self.eval, "foo('1')", context=context1)
self.assertRaises(Exception, self.eval, 'foo(1)', context=context2)
self.assertEquals("bool: True",
self.eval('foo(true)', context=context2))
self.assertEquals("bool: False",
self.eval('foo(false)', context=context2))
self.assertRaises(Exception, self.eval, "foo(1)", context=context2)
self.assertRaises(Exception, self.eval, 'foo(0)', context=context2)
self.assertRaises(Exception, self.eval, 'foo(True)', context=context2)
self.assertRaises(Exception, self.eval, "foo('true')",
context=context2)
self.assertEquals("int: 1", self.eval('foo(1)', context=context3))
self.assertEquals("int: 0", self.eval('foo(0)', context=context3))
self.assertEquals("bool: True",
self.eval('foo(true)', context=context3))
self.assertEquals("bool: False",
self.eval('foo(false)', context=context3))
self.assertEquals("int: 1", self.eval('foo(1)', context=context4))
self.assertEquals("int: 0", self.eval('foo(0)', context=context4))
self.assertEquals("bool: True",
self.eval('foo(true)', context=context4))
self.assertEquals("bool: False",
self.eval('foo(false)', context=context4))
示例2: create_context
def create_context(add_serializers=False, add_datadiff=False, **kwargs):
context = yaql.create_context(**kwargs)
if add_serializers:
serializers.register(context)
if add_datadiff:
datadiff.register(context)
return context
示例3: main
def main():
p = optparse.OptionParser()
p.add_option("--data", "-d")
p.add_option("-t", action="store_true", dest="tokens")
p.add_option("--legacy", action="store_true", dest="legacy")
options, arguments = p.parse_args()
if options.data:
try:
with open(options.data) as f:
data = json.load(f)
except Exception:
print("Unable to load data from " + options.data)
return
else:
data = None
engine_options = {"yaql.limitIterators": 100, "yaql.treatSetsAsLists": True, "yaql.memoryQuota": 10000}
if options.legacy:
factory = yaql.legacy.YaqlFactory()
context = yaql.legacy.create_context()
context["legacy"] = True
else:
factory = yaql.YaqlFactory()
context = yaql.create_context()
parser = factory.create(options=engine_options)
cli_functions.register_in_context(context, parser)
if options.tokens:
parser("__main(true)").evaluate(data, context)
else:
parser("__main(false)").evaluate(data, context)
示例4: __init__
def __init__(self, cleaned_data, forms=None, templates=None,
application=None, **kwargs):
self.cleaned_data = cleaned_data
self.templates = templates or {}
if application is None:
raise ValueError('Application section is required')
else:
self.application = application
self.context = yaql.create_context()
yaql_functions.register(self.context)
self.forms = []
for key, value in kwargs.iteritems():
setattr(self, key, value)
if forms:
for data in forms:
(name, field_specs, validators,
verbose_name) = self.extract_form_data(data)
self._add_form(name, field_specs, validators, verbose_name)
# Add ManageWorkflowForm
workflow_form = catalog_forms.WorkflowManagementForm
self._add_form(workflow_form.name, workflow_form.field_specs,
workflow_form.validators, _("What's next?"))
示例5: create_context
def create_context(*args, **kwargs):
tuples = kwargs.pop('tuples', True)
context = yaql.create_context(*args, **kwargs)
context = context.create_child_context()
std_legacy.register(context, tuples)
return context
示例6: __init__
def __init__(self, object_store):
self._object_store = object_store
self._root_context = yaql.create_context(True)
@ContextAware()
def resolve(context, name, obj):
return self._resolve(context, name, obj)
self._root_context.register_function(resolve, '#resolve')
示例7: evaluate
def evaluate(self, data=utils.NO_VALUE, context=None):
if context is None or context is utils.NO_VALUE:
context = yaql.create_context()
if data is not utils.NO_VALUE:
if self.engine.options.get('yaql.convertInputData', True):
context['$'] = utils.convert_input_data(data)
else:
context['$'] = data
return self(utils.NO_VALUE, context, self.engine)
示例8: evaluate
def evaluate(self, data=None, context=None):
if not context:
context = Context(yaql.create_context())
if data:
context.set_data(data)
f = self.create_callable(context)
# noinspection PyCallingNonCallable
return f()
示例9: __init__
def __init__(self, *args, **kwargs):
LOG.info("Creating form {0}".format(self.__class__.__name__))
super(ServiceConfigurationForm, self).__init__(*args, **kwargs)
self.auto_id = '{0}_%s'.format(self.initial.get('app_id'))
self.context = yaql.create_context()
yaql_functions.register(self.context)
self.finalize_fields()
self.update_fields()
示例10: main
def main():
p = optparse.OptionParser()
p.add_option('--data', '-d', help="input file")
p.add_option('--string', '-s', action='store_true',
help="input is a string")
p.add_option('--native', '-n', action='store_true',
help="output data in Python native format")
p.add_option('--array', '-a', action='store_true',
help="read input line by line")
p.add_option('--tokens', '-t', action='store_true', dest='tokens',
help="print lexical tokens info")
p.add_option('--legacy', action='store_true', dest='legacy',
help="enable legacy v0.2 compatibility mode")
options, arguments = p.parse_args()
if options.data:
try:
if options.data == '-':
data = read_data(sys.stdin, options)
else:
with open(options.data) as f:
data = read_data(f, options)
except Exception:
print('Unable to load data from ' + options.data, file=sys.stderr)
exit(1)
else:
data = None
engine_options = {
'yaql.limitIterators': 1000,
'yaql.convertSetsToLists': True,
'yaql.memoryQuota': 100000
}
if options.legacy:
factory = yaql.legacy.YaqlFactory()
context = yaql.legacy.create_context()
context['legacy'] = True
else:
factory = yaql.YaqlFactory()
context = yaql.create_context()
if options.native:
context['#nativeOutput'] = True
parser = factory.create(options=engine_options)
cli_functions.register_in_context(context, parser)
if len(arguments) > 0:
for arg in arguments:
cli_functions.evaluate(arg, parser, data, context)
elif options.tokens:
parser('__main(true)').evaluate(data, context)
else:
parser('__main(false)').evaluate(data, context)
示例11: _evaluate
def _evaluate(yaql_expression, yaml_data, legacy=False):
if legacy:
factory = yaql.legacy.YaqlFactory()
context = yaql.legacy.create_context()
context['legacy'] = True
else:
factory = yaql.YaqlFactory()
context = yaql.create_context()
parser = factory.create()
return parser(yaql_expression).evaluate(yaml_data, context)
示例12: test_evaluate
def test_evaluate(self):
yaql_value = mock.Mock(yaql_expression.YaqlExpression,
return_value='atom')
complex_value = {yaql_value: ['some', (1, yaql_value), 'hi!'],
'sample': [yaql_value, six.moves.range(5)]}
complex_literal = utils.FrozenDict({
'atom': ('some', (1, 'atom'), 'hi!'),
'sample': ('atom', (0, 1, 2, 3, 4))
})
context = yaql.create_context()
evaluated_value = helpers.evaluate(yaql_value, context)
evaluated_complex_value = helpers.evaluate(complex_value, context)
self.assertEqual('atom', evaluated_value)
self.assertEqual(complex_literal, evaluated_complex_value)
示例13: get_yaql_context
def get_yaql_context(data_context):
global ROOT_CONTEXT
if not ROOT_CONTEXT:
ROOT_CONTEXT = yaql.create_context()
_register_functions(ROOT_CONTEXT)
new_ctx = ROOT_CONTEXT.create_child_context()
new_ctx['$'] = data_context
if isinstance(data_context, dict):
new_ctx['__env'] = data_context.get('__env')
new_ctx['__execution'] = data_context.get('__execution')
return new_ctx
示例14: _evaluate
def _evaluate(yaql_expression, yaml_data, legacy=False):
engine_options = {
'yaql.limitIterators': 100,
'yaql.convertSetsToLists': True,
'yaql.memoryQuota': 10000
}
if legacy:
factory = yaql.legacy.YaqlFactory()
context = yaql.legacy.create_context()
context['legacy'] = True
else:
factory = yaql.YaqlFactory()
context = yaql.create_context()
parser = factory.create(options=engine_options)
return parser(yaql_expression).evaluate(yaml_data, context)
示例15: main
def main():
p = optparse.OptionParser()
p.add_option('--data', '-d')
options, arguments = p.parse_args()
if options.data:
try:
json_str = open(options.data).read()
decoder = JSONDecoder()
data = decoder.decode(json_str)
except:
print "Unable to load data from "+options.data
return
else:
data = None
context = yaql.create_context()
extensions.register_in_context(context)
yaql.parse('__main()').evaluate(data, context)