Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from yq.operators.base import Operator
from yq.operators.match_error import MatchError
class Comprehension(Operator):
def __init__(self, op):
self.op = op
def _apply_item(self, data):
if not isinstance(data, list):
raise MatchError(self, data, 'tried to apply comprehension %s to non-array' % self)
retval = []
for item in data:
retval.append(self.op._apply_item(item))
return retval
def __repr__(self):
return '[%s]' % self.op
from yq.operators.base import Operator
class Sequence(Operator):
def __init__(self, operators):
self.operators = operators
def apply(self, data):
for operator in self.operators:
data = operator.apply(data)
return data
def __repr__(self):
return ''.join([repr(operator) for operator in self.operators])
from yq.operators.base import Operator
class Subscript(Operator):
def __init__(self, indices):
self.indices = indices
def apply(self, data):
retval = []
for i in self.indices:
try:
retval.append(data[0][i])
except IndexError:
retval.append(None)
return retval
def __repr__(self):
return '[%s]' % ','.join(self.indices)
from yq.operators.base import Operator
from yq.operators.dot import Dot
from yq.operators.match_error import MatchError
class ProjectionItem(object):
def __init__(self, key, op):
self.key = key
self.op = op
class Projection(Operator):
def __init__(self, items):
self.items = items
def _apply_item(self, data):
retval = {}
for item in self.items:
retval[item.key] = item.op._apply_item(data)
return retval
def __repr__(self):
str_items = []
for item in self.items:
if isinstance(item.op, Dot) and item.op.key == item.key:
str_items.append(item.key)
else:
str_items.append('%s: %s' % (item.key, item.op))
from yq.operators.base import Operator
from yq.operators.match_error import MatchError
class Dot(Operator):
def __init__(self, key=''):
self.key = key
def _apply_item(self, data):
if self.key == '':
return data
if not isinstance(data, dict):
raise MatchError(self, data, 'tried to access field %s on a non-object' % self)
try:
return data[self.key]
except KeyError:
return None
def __repr__(self):
return '.%s' % self.key