Run pyupgrade to clean up Python 2 syntaxes

Update all .py source files by
 $ pyupgrade --py3-only $(git ls-files | grep ".py$")
to modernize the code according to Python 3 syntaxes.

pep8 errors are fixed by
 $ autopep8 --select=E127,E128,E501 --max-line-length 79 -r \
    --in-place yaql

Change-Id: If8fe14ea056d30984669f4cb96098084c4d32e7c
This commit is contained in:
Takashi Kajinami 2024-10-21 22:03:14 +09:00
parent 82a1eb0076
commit c054a36e93
25 changed files with 169 additions and 183 deletions

View File

@ -33,7 +33,7 @@ def _get_modules_names(package):
return sorted(
map(operator.itemgetter(1),
pkgutil.walk_packages(package.__path__,
'{0}.'.format(package.__name__))))
'{}.'.format(package.__name__))))
def _get_functions_names(module):
@ -89,7 +89,7 @@ def write_method_doc(method, output):
else:
call_as = 'function'
call_as_str = ' :callAs: {0}\n'.format(call_as)
call_as_str = ' :callAs: {}\n'.format(call_as)
text = doc[:position] + call_as_str + doc[position:]
except ValueError:
text = doc
@ -168,7 +168,7 @@ def generate_doc(source):
try:
package = importlib.import_module(source)
except ImportError:
return 'Error: No such module {0}'.format(source)
return 'Error: No such module {}'.format(source)
out = io.StringIO()
try:
if hasattr(package, '__path__'):
@ -179,7 +179,7 @@ def generate_doc(source):
return res
except Exception as e:
return '.. code-block:: python\n\n Error: {0}\n {1}\n\n'.format(
return '.. code-block:: python\n\n Error: {}\n {}\n\n'.format(
str(e), '\n '.join([''] + traceback.format_exc().split('\n')))
@ -188,7 +188,7 @@ class YaqlDocNode(nodes.General, nodes.Element):
def __init__(self, source):
self.source = source
super(YaqlDocNode, self).__init__()
super().__init__()
class YaqlDocDirective(rst.Directive):

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@ -34,8 +33,8 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
project = u'yaql'
copyright = u'2013, OpenStack Foundation'
project = 'yaql'
copyright = '2013, OpenStack Foundation'
# If true, '()' will be appended to :func: etc. cross-reference text.
add_function_parentheses = True
@ -69,8 +68,8 @@ htmlhelp_basename = '%sdoc' % project
latex_documents = [
('index',
'%s.tex' % project,
u'%s Documentation' % project,
u'OpenStack Foundation', 'manual'),
'%s Documentation' % project,
'OpenStack Foundation', 'manual'),
]
# Example configuration for intersphinx: refer to the Python standard library.

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
@ -55,8 +54,8 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
project = u'YAQL Release Notes'
copyright = u'2016, YAQL Developers'
project = 'YAQL Release Notes'
copyright = '2016, YAQL Developers'
# Release do not need a version number in the title, they
# cover multiple versions.
@ -212,8 +211,8 @@ latex_elements = {
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
('index', 'YaqlReleaseNotes.tex', u'Yaql Release Notes Documentation',
u'Yaql Developers', 'manual'),
('index', 'YaqlReleaseNotes.tex', 'Yaql Release Notes Documentation',
'Yaql Developers', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
@ -242,8 +241,8 @@ latex_documents = [
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'yaqlreleasenotes', u'Yaql Release Notes Documentation',
[u'Yaql Developers'], 1)
('index', 'yaqlreleasenotes', 'Yaql Release Notes Documentation',
['Yaql Developers'], 1)
]
# If true, show URL addresses after external links.
@ -256,8 +255,8 @@ man_pages = [
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'YaqlReleaseNotes', u'Yaql Release Notes Documentation',
u'Yaql Developers', 'YaqlReleaseNotes',
('index', 'YaqlReleaseNotes', 'Yaql Release Notes Documentation',
'Yaql Developers', 'YaqlReleaseNotes',
'One line description of project.',
'Miscellaneous'),
]

View File

@ -27,7 +27,7 @@ PROMPT = "yaql> "
def main(context, show_tokens, parser):
print("Yet Another Query Language - command-line query tool")
print("Version {0}".format(version))
print("Version {}".format(version))
if context.get_data('legacy', False):
print("Running in a legacy (0.2.x compatible) mode")
print("Copyright (c) 2013-2017 Mirantis, Inc")
@ -78,15 +78,15 @@ def main(context, show_tokens, parser):
res = expr.evaluate(context=context)
print_output(res, context)
except Exception as ex:
print(u'Execution exception: {0}'.format(ex), file=sys.stderr)
print('Execution exception: {}'.format(ex), file=sys.stderr)
def load_data(data_file, context):
try:
json_str = open(os.path.expanduser(data_file)).read()
except IOError as e:
print("Unable to read data file '{0}': {1}".format(data_file,
e.strerror))
except OSError as e:
print("Unable to read data file '{}': {}".format(data_file,
e.strerror))
return
try:
data = json.loads(json_str)
@ -94,7 +94,7 @@ def load_data(data_file, context):
print('Unable to parse data: ' + str(e))
return
context['$'] = utils.convert_input_data(data)
print('Data from file {0} loaded into context'.format(data_file))
print('Data from file {} loaded into context'.format(data_file))
def register_in_context(context, parser):
@ -117,7 +117,7 @@ def evaluate(expr, parser, data, context):
res = parser(expr).evaluate(data, context)
print_output(res, context)
except Exception as ex:
print(u'Execution exception: {0}'.format(ex), file=sys.stderr)
print('Execution exception: {}'.format(ex), file=sys.stderr)
exit(1)

View File

@ -98,7 +98,7 @@ class ContextBase(metaclass=abc.ABCMeta):
class Context(ContextBase):
def __init__(self, parent_context=None, data=utils.NO_VALUE,
convention=None):
super(Context, self).__init__(parent_context, convention)
super().__init__(parent_context, convention)
self._functions = {}
self._data = {}
self._exclusive_funcs = set()
@ -186,12 +186,12 @@ class MultiContext(ContextBase):
filter(lambda t: t, map(lambda t: t.parent, context_list))
)
if not parents:
super(MultiContext, self).__init__(None, convention)
super().__init__(None, convention)
elif len(parents) == 1:
super(MultiContext, self).__init__(parents[0], convention)
super().__init__(parents[0], convention)
else:
super(MultiContext, self).__init__(MultiContext(parents),
convention)
super().__init__(MultiContext(parents),
convention)
def register_function(self, spec, *args, **kwargs):
self._context_list[0].register_function(spec, *args, **kwargs)
@ -256,11 +256,11 @@ class LinkedContext(ContextBase):
def __init__(self, parent_context, linked_context, convention=None):
self.linked_context = linked_context
if linked_context.parent:
super(LinkedContext, self).__init__(
super().__init__(
LinkedContext(parent_context, linked_context.parent,
convention), convention)
else:
super(LinkedContext, self).__init__(parent_context, convention)
super().__init__(parent_context, convention)
def register_function(self, spec, *args, **kwargs):
return self.linked_context.register_function(spec, *args, **kwargs)

View File

@ -31,79 +31,79 @@ class MethodResolutionError(ResolutionError):
class NoFunctionRegisteredException(FunctionResolutionError):
def __init__(self, name):
super(NoFunctionRegisteredException, self).__init__(
u'Unknown function "{0}"'.format(name))
super().__init__(
'Unknown function "{}"'.format(name))
class NoMethodRegisteredException(MethodResolutionError):
def __init__(self, name, receiver):
super(NoMethodRegisteredException, self).__init__(
u'Unknown method "{0}" for receiver {1}'.format(name, receiver))
super().__init__(
'Unknown method "{}" for receiver {}'.format(name, receiver))
class NoMatchingFunctionException(FunctionResolutionError):
def __init__(self, name):
super(NoMatchingFunctionException, self).__init__(
u'No function "{0}" matches supplied arguments'.format(name))
super().__init__(
'No function "{}" matches supplied arguments'.format(name))
class NoMatchingMethodException(MethodResolutionError):
def __init__(self, name, receiver):
super(NoMatchingMethodException, self).__init__(
u'No method "{0}" for receiver {1} matches '
u'supplied arguments'.format(name, receiver))
super().__init__(
'No method "{}" for receiver {} matches '
'supplied arguments'.format(name, receiver))
class AmbiguousFunctionException(FunctionResolutionError):
def __init__(self, name):
super(AmbiguousFunctionException, self).__init__(
u'Ambiguous function "{0}"'.format(name))
super().__init__(
'Ambiguous function "{}"'.format(name))
class AmbiguousMethodException(MethodResolutionError):
def __init__(self, name, receiver):
super(AmbiguousMethodException, self).__init__(
u'Ambiguous method "{0}" for receiver {1}'.format(name, receiver))
super().__init__(
'Ambiguous method "{}" for receiver {}'.format(name, receiver))
class ArgumentException(YaqlException):
def __init__(self, argument_name):
self.parameter_name = argument_name
super(ArgumentException, self).__init__(
u'Invalid argument {0}'.format(argument_name))
super().__init__(
'Invalid argument {}'.format(argument_name))
class MappingTranslationException(YaqlException):
def __init__(self):
super(MappingTranslationException, self).__init__(
u'Cannot convert mapping to keyword argument')
super().__init__(
'Cannot convert mapping to keyword argument')
class ArgumentValueException(YaqlException):
def __init__(self):
super(ArgumentValueException, self).__init__()
super().__init__()
class DuplicateParameterDecoratorException(YaqlException):
def __init__(self, function_name, param_name):
message = u"Function '{0}' has multiple " \
u"decorators for parameter '{1}'". \
message = "Function '{0}' has multiple " \
"decorators for parameter '{1}'". \
format(function_name, param_name)
super(DuplicateParameterDecoratorException, self).__init__(message)
super().__init__(message)
class InvalidMethodException(YaqlException):
def __init__(self, function_name):
message = u"Function '{0}' cannot be called as a method". \
message = "Function '{0}' cannot be called as a method". \
format(function_name)
super(InvalidMethodException, self).__init__(message)
super().__init__(message)
class NoParameterFoundException(YaqlException):
def __init__(self, function_name, param_name):
message = u"Function '{0}' has no parameter called '{1}'". \
message = "Function '{0}' has no parameter called '{1}'". \
format(function_name, param_name)
super(NoParameterFoundException, self).__init__(message)
super().__init__(message)
class YaqlParsingException(YaqlException):
@ -111,46 +111,46 @@ class YaqlParsingException(YaqlException):
self.value = value
self.position = position
self.message = message
super(YaqlParsingException, self).__init__(message)
super().__init__(message)
class YaqlGrammarException(YaqlParsingException):
def __init__(self, expr, value, position):
if position is None:
msg = u'Parse error: unexpected end of statement'
msg = 'Parse error: unexpected end of statement'
else:
msg = u"Parse error: unexpected '{0}' at position {1} of " \
u"expression '{2}'".format(value, position, expr)
super(YaqlGrammarException, self).__init__(value, position, msg)
msg = "Parse error: unexpected '{}' at position {} of " \
"expression '{}'".format(value, position, expr)
super().__init__(value, position, msg)
class YaqlLexicalException(YaqlParsingException):
def __init__(self, value, position):
msg = u"Lexical error: illegal character '{0}' at position {1}" \
msg = "Lexical error: illegal character '{}' at position {}" \
.format(value, position)
super(YaqlLexicalException, self).__init__(value, position, msg)
super().__init__(value, position, msg)
class InvalidOperatorTableException(YaqlException):
def __init__(self, op):
super(InvalidOperatorTableException, self). \
__init__(u"Invalid records in operator table for operator "
u"'{0}".format(op))
super(). \
__init__("Invalid records in operator table for operator "
"'{}".format(op))
class WrappedException(YaqlException):
def __init__(self, exception):
self.wrapped = exception
super(WrappedException, self).__init__(str(exception))
super().__init__(str(exception))
class CollectionTooLargeException(YaqlException):
def __init__(self, count):
super(CollectionTooLargeException, self).__init__(
'Collection length exceeds {0} elements'.format(count))
super().__init__(
'Collection length exceeds {} elements'.format(count))
class MemoryQuotaExceededException(YaqlException):
def __init__(self):
super(MemoryQuotaExceededException, self).__init__(
super().__init__(
'Expression consumed too much memory')

View File

@ -19,7 +19,7 @@ from yaql.language import exceptions
from yaql.language import utils
class Expression(object):
class Expression:
def __call__(self, receiver, context, engine):
pass
@ -34,7 +34,7 @@ class Function(Expression):
return context(self.name, engine, receiver, context)(*self.args)
def __str__(self):
return '{0}({1})'.format(self.name, ', '.join(map(str, self.args)))
return '{}({})'.format(self.name, ', '.join(map(str, self.args)))
class BinaryOperator(Function):
@ -44,7 +44,7 @@ class BinaryOperator(Function):
else:
func_name = '*' + alias
self.operator = op
super(BinaryOperator, self).__init__(func_name, obj1, obj2)
super().__init__(func_name, obj1, obj2)
self.uses_receiver = False
@ -55,31 +55,31 @@ class UnaryOperator(Function):
else:
func_name = '*' + alias
self.operator = op
super(UnaryOperator, self).__init__(func_name, obj)
super().__init__(func_name, obj)
self.uses_receiver = False
class IndexExpression(Function):
def __init__(self, value, *args):
super(IndexExpression, self).__init__('#indexer', value, *args)
super().__init__('#indexer', value, *args)
self.uses_receiver = False
class ListExpression(Function):
def __init__(self, *args):
super(ListExpression, self).__init__('#list', *args)
super().__init__('#list', *args)
self.uses_receiver = False
class MapExpression(Function):
def __init__(self, *args):
super(MapExpression, self).__init__('#map', *args)
super().__init__('#map', *args)
self.uses_receiver = False
class GetContextValue(Function):
def __init__(self, path):
super(GetContextValue, self).__init__('#get_context_data', path)
super().__init__('#get_context_data', path)
self.path = path
self.uses_receiver = False
@ -94,7 +94,7 @@ class Constant(Expression):
def __str__(self):
if isinstance(self.value, str):
return "'{0}'".format(self.value)
return "'{}'".format(self.value)
return str(self.value)
def __call__(self, receiver, context, engine):
@ -124,7 +124,7 @@ class MappingRuleExpression(Expression):
self.uses_receiver = False
def __str__(self):
return u'{0} => {1}'.format(self.source, self.destination)
return '{} => {}'.format(self.source, self.destination)
def __call__(self, receiver, context, engine):
return utils.MappingRule(
@ -137,14 +137,14 @@ class Statement(Function):
self.expression = expression
self.uses_receiver = False
self.engine = engine
super(Statement, self).__init__('#finalize', expression)
super().__init__('#finalize', expression)
def __call__(self, receiver, context, engine):
if not context.collect_functions('#finalize'):
context = context.create_child_context()
context.register_function(lambda x: x, name='#finalize')
try:
return super(Statement, self).__call__(receiver, context, engine)
return super().__call__(receiver, context, engine)
except exceptions.WrappedException as e:
raise e.wrapped.with_traceback(sys.exc_info()[2])

View File

@ -39,13 +39,13 @@ OperatorType = collections.namedtuple('OperatorType', [
)
class YaqlOperators(object):
class YaqlOperators:
def __init__(self, operators, name_value_op=None):
self.operators = operators
self.name_value_op = name_value_op
class YaqlEngine(object):
class YaqlEngine:
def __init__(self, ply_lexer, ply_parser, options, factory):
self._lexer = ply_lexer
self._parser = ply_parser
@ -81,7 +81,7 @@ class YaqlEngine(object):
return YaqlEngine(self._lexer, self._parser, opt, self._factory)
class YaqlFactory(object):
class YaqlFactory:
def __init__(self, keyword_operator='=>', allow_delegates=False):
self._keyword_operator = keyword_operator
self._allow_delegates = allow_delegates
@ -156,7 +156,7 @@ class YaqlFactory(object):
position = i
break
if position < 0:
raise ValueError('Operator {0} is not found'.format(
raise ValueError('Operator {} is not found'.format(
existing_operator))
while position < len(self.operators) and len(
self.operators[position]) > 1:

View File

@ -36,7 +36,7 @@ def decode_escapes(s):
# noinspection PyPep8Naming
class Lexer(object):
class Lexer:
t_ignore = ' \t\r\n'
literals = '()],}'

View File

@ -19,7 +19,7 @@ from yaql.language import expressions
from yaql.language import utils
class Parser(object):
class Parser:
def __init__(self, lexer, yaql_operators, engine):
self.tokens = lexer.tokens
self._aliases = {}
@ -54,7 +54,7 @@ class Parser(object):
la.append(op_name)
binary_doc += ((
'value : ' if not binary_doc else '\n| ') +
'value {0} value'.format(op_name))
'value {} value'.format(op_name))
# noinspection PyProtectedMember
def p_binary(this, p):

View File

@ -21,7 +21,7 @@ from yaql.language import yaqltypes
NO_DEFAULT = utils.create_marker('<NoValue>')
class ParameterDefinition(object):
class ParameterDefinition:
__slots__ = ('value_type', 'name', 'position', 'default', 'alias')
def __init__(self, name, value_type=None, position=None, alias=None,
@ -33,7 +33,7 @@ class ParameterDefinition(object):
self.alias = alias
def __repr__(self):
return '{0} => position={1} value_type={2} default={3}'.format(
return '{} => position={} value_type={} default={}'.format(
self.name, self.position, self.value_type, self.default)
def clone(self):
@ -41,7 +41,7 @@ class ParameterDefinition(object):
self.position, self.alias, self.default)
class FunctionDefinition(object):
class FunctionDefinition:
__slots__ = ('is_method', 'is_function', 'name', 'parameters', 'payload',
'doc', 'no_kwargs', 'meta')
@ -461,7 +461,7 @@ def meta(name, value):
def yaql_property(source_type):
def decorator(func):
@name('#property#{0}'.format(get_function_definition(func).name))
@name('#property#{}'.format(get_function_definition(func).name))
@parameter('obj', source_type)
def wrapper(obj):
return func(obj)

View File

@ -21,7 +21,7 @@ from yaql.language import lexer
def create_marker(msg):
class MarkerClass(object):
class MarkerClass:
def __repr__(self):
return msg
return MarkerClass()
@ -113,7 +113,7 @@ def convert_tuples_to_lists(engine):
return engine.options.get('yaql.convertTuplesToLists', True)
class MappingRule(object):
class MappingRule:
def __init__(self, source, destination):
self.source = source
self.destination = destination

View File

@ -62,7 +62,7 @@ class GenericType(SmartType):
__slots__ = ('checker', 'converter')
def __init__(self, nullable, checker=None, converter=None):
super(GenericType, self).__init__(nullable)
super().__init__(nullable)
self.checker = checker
self.converter = converter
@ -70,7 +70,7 @@ class GenericType(SmartType):
if isinstance(value, expressions.Constant):
value = value.value
if not super(GenericType, self).check(
if not super().check(
value, context, engine, *args, **kwargs):
return False
if value is None or isinstance(value, expressions.Expression):
@ -83,7 +83,7 @@ class GenericType(SmartType):
*args, **kwargs):
if isinstance(value, expressions.Constant):
value = value.value
super(GenericType, self).convert(
super().convert(
value, receiver, context, function_spec, engine, *args, **kwargs)
if value is None or not self.converter:
return value
@ -102,7 +102,7 @@ class PythonType(GenericType):
validators = [validators]
self.validators = validators
super(PythonType, self).__init__(
super().__init__(
nullable,
lambda value, context, *args, **kwargs: isinstance(
value, self.python_type) and all(
@ -127,14 +127,14 @@ class MappingRule(LazyParameterType, SmartType):
__slots__ = tuple()
def __init__(self):
super(MappingRule, self).__init__(False)
super().__init__(False)
def check(self, value, context, *args, **kwargs):
return isinstance(value, expressions.MappingRuleExpression)
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
super(MappingRule, self).convert(
super().convert(
value, receiver, context, function_spec, engine, *args, **kwargs)
wrap = lambda func: lambda: func(receiver, context, engine) # noqa
@ -145,11 +145,11 @@ class String(PythonType):
__slots__ = tuple()
def __init__(self, nullable=False):
super(String, self).__init__(str, nullable=nullable)
super().__init__(str, nullable=nullable)
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
value = super(String, self).convert(
value = super().convert(
value, receiver, context, function_spec, engine, *args, **kwargs)
return None if value is None else str(value)
@ -158,7 +158,7 @@ class Integer(PythonType):
__slots__ = tuple()
def __init__(self, nullable=False):
super(Integer, self).__init__(
super().__init__(
int, nullable=nullable,
validators=[lambda t: not isinstance(t, bool)])
@ -169,7 +169,7 @@ class DateTime(PythonType):
utctz = tz.tzutc()
def __init__(self, nullable=False):
super(DateTime, self).__init__(datetime.datetime, nullable=nullable)
super().__init__(datetime.datetime, nullable=nullable)
def convert(self, value, *args, **kwargs):
if isinstance(value, datetime.datetime):
@ -177,14 +177,14 @@ class DateTime(PythonType):
return value.replace(tzinfo=self.utctz)
else:
return value
return super(DateTime, self).convert(value, *args, **kwargs)
return super().convert(value, *args, **kwargs)
class Iterable(PythonType):
__slots__ = tuple()
def __init__(self, validators=None, nullable=False):
super(Iterable, self).__init__(
super().__init__(
collections.abc.Iterable, nullable,
[lambda t: not isinstance(t, (str, utils.MappingType))] + (
validators or []))
@ -193,12 +193,12 @@ class Iterable(PythonType):
if isinstance(value, utils.MappingType) and engine.options.get(
'yaql.iterableDicts', False):
return True
return super(Iterable, self).check(
return super().check(
value, context, engine, *args, **kwargs)
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
res = super(Iterable, self).convert(
res = super().convert(
value, receiver, context, function_spec, engine, *args, **kwargs)
return None if res is None else utils.limit_iterable(res, engine)
@ -207,7 +207,7 @@ class Iterator(Iterable):
__slots__ = tuple()
def __init__(self, validators=None, nullable=False):
super(Iterator, self).__init__(
super().__init__(
validators=[utils.is_iterator] + (validators or []),
nullable=nullable)
@ -216,7 +216,7 @@ class Sequence(PythonType):
__slots__ = tuple()
def __init__(self, validators=None, nullable=False):
super(Sequence, self).__init__(
super().__init__(
collections.abc.Sequence, nullable, [
lambda t: not isinstance(t, (str, dict))] + (
validators or []))
@ -226,7 +226,7 @@ class Number(PythonType):
__slots__ = tuple()
def __init__(self, nullable=False):
super(Number, self).__init__(
super().__init__(
(int, float), nullable,
validators=[lambda t: not isinstance(t, bool)])
@ -235,7 +235,7 @@ class Lambda(LazyParameterType, SmartType):
__slots__ = ('with_context', 'method')
def __init__(self, with_context=False, method=False):
super(Lambda, self).__init__(True)
super().__init__(True)
self.with_context = with_context
self.method = method
@ -243,7 +243,7 @@ class Lambda(LazyParameterType, SmartType):
if self.method and isinstance(
value, expressions.Expression) and not value.uses_receiver:
return False
return super(Lambda, self).check(value, context, *args, **kwargs)
return super().check(value, context, *args, **kwargs)
@staticmethod
def _publish_params(context, args, kwargs):
@ -264,7 +264,7 @@ class Lambda(LazyParameterType, SmartType):
def convert(self, value, receiver, context, function_spec, engine,
*convert_args, **convert_kwargs):
super(Lambda, self).convert(
super().convert(
value, receiver, context, function_spec, engine,
*convert_args, **convert_kwargs)
if value is None:
@ -301,7 +301,7 @@ class Super(HiddenParameterType, SmartType):
self.with_context = with_context
self.method = method
self.with_name = with_name
super(Super, self).__init__(False)
super().__init__(False)
@staticmethod
def _find_function_context(spec, context):
@ -353,7 +353,7 @@ class Context(HiddenParameterType, SmartType):
__slots__ = tuple()
def __init__(self):
super(Context, self).__init__(False)
super().__init__(False)
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
@ -365,7 +365,7 @@ class Delegate(HiddenParameterType, SmartType):
def __init__(self, name=None, with_context=False, method=False,
use_convention=True):
super(Delegate, self).__init__(False)
super().__init__(False)
self.name = name
self.with_context = with_context
self.method = method
@ -403,7 +403,7 @@ class Receiver(HiddenParameterType, SmartType):
__slots__ = tuple()
def __init__(self):
super(Receiver, self).__init__(False)
super().__init__(False)
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
@ -414,7 +414,7 @@ class Engine(HiddenParameterType, SmartType):
__slots__ = tuple()
def __init__(self):
super(Engine, self).__init__(False)
super().__init__(False)
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
@ -425,7 +425,7 @@ class FunctionDefinition(HiddenParameterType, SmartType):
__slots__ = tuple()
def __init__(self):
super(FunctionDefinition, self).__init__(False)
super().__init__(False)
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
@ -437,16 +437,16 @@ class Constant(SmartType):
def __init__(self, nullable, expand=True):
self.expand = expand
super(Constant, self).__init__(nullable)
super().__init__(nullable)
def check(self, value, context, *args, **kwargs):
return super(Constant, self).check(
return super().check(
value, context, *args, **kwargs) and (
value is None or isinstance(value, expressions.Constant))
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
super(Constant, self).convert(
super().convert(
value, receiver, context, function_spec, engine, *args, **kwargs)
return value.value if self.expand else value
@ -455,7 +455,7 @@ class YaqlExpression(LazyParameterType, SmartType):
__slots__ = ('_expression_types',)
def __init__(self, expression_type=None):
super(YaqlExpression, self).__init__(False)
super().__init__(False)
if expression_type and not utils.is_sequence(expression_type):
expression_type = (expression_type,)
self._expression_types = expression_type
@ -467,7 +467,7 @@ class YaqlExpression(LazyParameterType, SmartType):
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):
super(YaqlExpression, self).convert(
super().convert(
value, receiver, context, function_spec, engine, *args, **kwargs)
return value
@ -476,10 +476,10 @@ class StringConstant(Constant):
__slots__ = tuple()
def __init__(self, nullable=False):
super(StringConstant, self).__init__(nullable)
super().__init__(nullable)
def check(self, value, context, *args, **kwargs):
return super(StringConstant, self).check(
return super().check(
value, context, *args, **kwargs) and (
value is None or isinstance(value.value, str))
@ -488,7 +488,7 @@ class Keyword(Constant):
__slots__ = tuple()
def __init__(self, expand=True):
super(Keyword, self).__init__(False, expand)
super().__init__(False, expand)
def check(self, value, context, *args, **kwargs):
return isinstance(value, expressions.KeywordConstant)
@ -498,10 +498,10 @@ class BooleanConstant(Constant):
__slots__ = tuple()
def __init__(self, nullable=False, expand=True):
super(BooleanConstant, self).__init__(nullable, expand)
super().__init__(nullable, expand)
def check(self, value, context, *args, **kwargs):
return super(BooleanConstant, self).check(
return super().check(
value, context, *args, **kwargs) and (
value is None or type(value.value) is bool)
@ -510,10 +510,10 @@ class NumericConstant(Constant):
__slots__ = tuple()
def __init__(self, nullable=False, expand=True):
super(NumericConstant, self).__init__(nullable, expand)
super().__init__(nullable, expand)
def check(self, value, context, *args, **kwargs):
return super(NumericConstant, self).check(
return super().check(
value, context, *args, **kwargs) and (
value is None or
isinstance(value.value, (int, float)) and
@ -525,7 +525,7 @@ class SmartTypeAggregation(SmartType, metaclass=abc.ABCMeta):
def __init__(self, *args, **kwargs):
self.nullable = kwargs.pop('nullable', False)
super(SmartTypeAggregation, self).__init__(self.nullable)
super().__init__(self.nullable)
self.types = []
for item in args:
@ -625,12 +625,12 @@ class NotOfType(SmartType):
if isinstance(smart_type, (type, tuple)):
smart_type = PythonType(smart_type, nullable=nullable)
self.smart_type = smart_type
super(NotOfType, self).__init__(nullable)
super().__init__(nullable)
def check(self, value, context, engine, *args, **kwargs):
if isinstance(value, expressions.Constant):
value = value.value
if not super(NotOfType, self).check(
if not super().check(
value, context, engine, *args, **kwargs):
return False
if value is None or isinstance(value, expressions.Expression):
@ -643,7 +643,7 @@ class YaqlInterface(HiddenParameterType, SmartType):
__slots__ = tuple()
def __init__(self):
super(YaqlInterface, self).__init__(False)
super().__init__(False)
def convert(self, value, receiver, context, function_spec, engine,
*args, **kwargs):

View File

@ -20,7 +20,7 @@ from yaql.standard_library import legacy as std_legacy
class YaqlFactory(factory.YaqlFactory):
def __init__(self, allow_delegates=False):
# noinspection PyTypeChecker
super(YaqlFactory, self).__init__(
super().__init__(
keyword_operator=None, allow_delegates=allow_delegates)
self.insert_operator(
'or', True, '=>',
@ -30,7 +30,7 @@ class YaqlFactory(factory.YaqlFactory):
options = dict(options or {})
options['yaql.convertTuplesToLists'] = False
options['yaql.iterableDicts'] = True
return super(YaqlFactory, self).create(options)
return super().create(options)
def create_context(*args, **kwargs):

View File

@ -43,8 +43,7 @@ def list_(delegate, *args):
def rec(seq):
for t in seq:
if utils.is_iterator(t):
for t2 in rec(t):
yield t2
yield from rec(t)
else:
yield t
return delegate(rec(args))
@ -69,8 +68,7 @@ def flatten(collection):
"""
for t in collection:
if utils.is_iterable(t):
for t2 in flatten(t):
yield t2
yield from flatten(t)
else:
yield t
@ -868,8 +866,7 @@ def replace_many(collection, position, values, count=1):
if (count >= 0 and position <= i < position + count
or count < 0 and i >= position):
if not yielded:
for v in values:
yield v
yield from values
yielded = True
else:
yield t
@ -1019,17 +1016,14 @@ def insert_many(collection, position, values):
"""
i = -1
if position < 0:
for j in values:
yield j
yield from values
for i, t in enumerate(collection):
if i == position:
for j in values:
yield j
yield from values
yield t
if position > i:
for j in values:
yield j
yield from values
@specs.parameter('s', utils.SetType, alias='set')
@ -1073,8 +1067,7 @@ def set_(delegate, *args):
def rec(seq):
for t in seq:
if utils.is_iterator(t):
for t2 in rec(t):
yield t2
yield from rec(t)
else:
yield t
return delegate(rec(args))

View File

@ -45,7 +45,7 @@ class OrderingIterable(utils.IterableType):
return iter(self.sorted)
def do_sort(outer_self):
class Comparator(object):
class Comparator:
@staticmethod
def compare(left, right):
result = 0
@ -229,10 +229,8 @@ def append(collection, *args):
yaql> [1, 2, 3].append(4, 5)
[1, 2, 3, 4, 5]
"""
for t in collection:
yield t
for t in args:
yield t
yield from collection
yield from args
@specs.parameter('collection', yaqltypes.Iterable())
@ -653,8 +651,7 @@ def select_many(collection, selector):
for item in collection:
inner = selector(item)
if utils.is_iterable(inner):
for t in inner:
yield t
yield from inner
else:
yield inner
@ -847,7 +844,7 @@ def then_by_descending(collection, selector, context):
return collection
class GroupAggregator(object):
class GroupAggregator:
"""A function to aggregate the members of a group found by group_by().
The user-specified function is provided at creation. It is assumed to
@ -1460,7 +1457,7 @@ def _merge_dicts(dict1, dict2, list_merge_func, item_merger, max_levels=0):
if max_levels != 1 and isinstance(value2, utils.MappingType):
if not isinstance(value1, utils.MappingType):
raise TypeError(
'Cannot merge {0} with {1}'.format(
'Cannot merge {} with {}'.format(
type(value1), type(value2)))
result[key] = _merge_dicts(
value1, value2, list_merge_func, item_merger,
@ -1468,7 +1465,7 @@ def _merge_dicts(dict1, dict2, list_merge_func, item_merger, max_levels=0):
elif max_levels != 1 and utils.is_sequence(value2):
if not utils.is_sequence(value1):
raise TypeError(
'Cannot merge {0} with {1}'.format(
'Cannot merge {} with {}'.format(
type(value1), type(value2)))
result[key] = list_merge_func(value1, value2)
else:

View File

@ -584,7 +584,7 @@ def string_by_int(left, right, engine):
yaql> "ab" * 2
"abab"
"""
utils.limit_memory_usage(engine, (-right + 1, u''), (right, left))
utils.limit_memory_usage(engine, (-right + 1, ''), (right, left))
return left * right

View File

@ -120,7 +120,7 @@ def unpack(sequence, context, *args):
"""
lst = tuple(itertools.islice(sequence, len(args) + 1))
if 0 < len(args) != len(lst):
raise ValueError('Cannot unpack {0} elements into {1}'.format(
raise ValueError('Cannot unpack {} elements into {}'.format(
len(lst), len(args)))
if len(args) > 0:
for i in range(len(lst)):
@ -231,7 +231,7 @@ def send_context(left, right):
@specs.method
@specs.parameter('condition', yaqltypes.Lambda())
@specs.parameter('message', yaqltypes.String())
def assert__(engine, obj, condition, message=u'Assertion failed'):
def assert__(engine, obj, condition, message='Assertion failed'):
""":yaql:assert
Evaluates condition against object. If it evaluates to true returns the
@ -329,7 +329,7 @@ def get_property(func, obj, name):
yaql> now().year
2016
"""
func_name = '#property#{0}'.format(name)
func_name = '#property#{}'.format(name)
return func(func_name, obj)

View File

@ -73,7 +73,7 @@ class Yaqlized(yaqltypes.GenericType):
return False
return True
super(Yaqlized, self).__init__(checker=check_value, nullable=False)
super().__init__(checker=check_value, nullable=False)
def _match_name_to_entry(name, entry):

View File

@ -84,7 +84,7 @@ class TestCase(testtools.TestCase):
self._engine = None
self._legacy_context = None
self._legacy_engine = None
super(TestCase, self).setUp()
super().setUp()
def eval(self, expression, data=None, context=None):
expr = self.engine(expression)

View File

@ -18,7 +18,7 @@ import yaql.tests
class TestLegacyNewEngine(yaql.tests.TestCase):
def __init__(self, *args, **kwargs):
super(TestLegacyNewEngine, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.eval = self.legacy_eval_new_engine
def test_dict(self):
@ -123,7 +123,7 @@ class TestLegacyNewEngine(yaql.tests.TestCase):
class TestLegacy(TestLegacyNewEngine):
def __init__(self, *args, **kwargs):
super(TestLegacy, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.eval = self.legacy_eval
def test_tuples_func(self):

View File

@ -368,7 +368,7 @@ class TestQueries(yaql.tests.TestCase):
def test_accumulate(self):
self.assertEqual(
['a', 'aa', u'aab', 'aaba', 'aabaa'],
['a', 'aa', 'aab', 'aaba', 'aabaa'],
self.eval('[a,a,b,a,a].accumulate($1 + $2)'))
self.assertEqual(

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2015 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -35,11 +33,11 @@ class TestStrings(yaql.tests.TestCase):
def test_to_upper(self):
self.assertEqual('QQ', self.eval('qq.toUpper()'))
self.assertEqual(u'ПРИВЕТ', self.eval(u'Привет.toUpper()'))
self.assertEqual('ПРИВЕТ', self.eval('Привет.toUpper()'))
def test_to_lower(self):
self.assertEqual('qq', self.eval('QQ.toLower()'))
self.assertEqual(u'привет', self.eval(u'Привет.toLower()'))
self.assertEqual('привет', self.eval('Привет.toLower()'))
def test_eq(self):
self.assertTrue(self.eval('a = a'))

View File

@ -21,10 +21,10 @@ from yaql import yaqlization
class TestYaqlization(tests.TestCase):
def _get_sample_class(self):
class D(object):
class D:
d_attr = 777
class C(object):
class C:
def __init__(self):
self.attr = 123
@ -176,14 +176,14 @@ class TestYaqlization(tests.TestCase):
def test_yaqlify_decorator(self):
@yaqlization.yaqlize
class C(object):
class C:
attr = 555
self.assertEqual(555, self.eval('$.attr', C()))
def test_yaqlify_decorator_with_parameters(self):
@yaqlization.yaqlize(yaqlize_attributes=True)
class C(object):
class C:
attr = 555
self.assertEqual(555, self.eval('$.attr', C()))

View File

@ -15,7 +15,7 @@
from yaql.language import utils
class YaqlInterface(object):
class YaqlInterface:
def __init__(self, context, engine, receiver=utils.NO_VALUE):
self.__sender = receiver
self.__engine = engine