Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 22 additions & 66 deletions picard/script/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,35 +680,42 @@ def func_ne(parser, x, y):
return ""


@script_function(documentation=N_(
"""`$lt(x,y[,type])`

Returns true if `x` is less than `y` using the comparison specified in `type`.
Possible values of `type` are "int" (integer), "float" (floating point), "text"
(case-sensitive text) and "nocase" (case-insensitive text), with "int" used as
the default comparison method if `type` is not specified."""
))
def func_lt(parser, x, y, _type=None):
def _cmp(op, x, y, _type):
"""Compare x vs y using op method and specified _type
op is expected to be a method from operator module
"""
if not _type:
_type = 'int'
_typer = None
if _type == 'text':
return "1" if x < y else ""
return "1" if op(x, y) else ""
elif _type == 'nocase':
return "1" if x.lower() < y.lower() else ""
return "1" if op(x.lower(), y.lower()) else ""
elif _type == 'float':
_typer = float
elif _type == 'int':
_typer = int
if _typer is not None:
try:
if _typer(x) < _typer(y):
if op(_typer(x), _typer(y)):
return "1"
except ValueError:
pass
return ""


@script_function(documentation=N_(
"""`$lt(x,y[,type])`

Returns true if `x` is less than `y` using the comparison specified in `type`.
Possible values of `type` are "int" (integer), "float" (floating point), "text"
(case-sensitive text) and "nocase" (case-insensitive text), with "int" used as
the default comparison method if `type` is not specified."""
))
def func_lt(parser, x, y, _type=None):
return _cmp(operator.lt, x, y, _type)


@script_function(documentation=N_(
"""`$lte(x,y[,type])`

Expand All @@ -718,24 +725,7 @@ def func_lt(parser, x, y, _type=None):
the default comparison method if `type` is not specified."""
))
def func_lte(parser, x, y, _type=None):
if not _type:
_type = 'int'
_typer = None
if _type == 'text':
return "" if x > y else "1"
elif _type == 'nocase':
return "" if x.lower() > y.lower() else "1"
elif _type == 'float':
_typer = float
elif _type == 'int':
_typer = int
if _typer is not None:
try:
if _typer(x) <= _typer(y):
return "1"
except ValueError:
pass
return ""
return _cmp(operator.le, x, y, _type)


@script_function(documentation=N_(
Expand All @@ -747,24 +737,7 @@ def func_lte(parser, x, y, _type=None):
the default comparison method if `type` is not specified."""
))
def func_gt(parser, x, y, _type=None):
if not _type:
_type = 'int'
_typer = None
if _type == 'text':
return "1" if x > y else ""
elif _type == 'nocase':
return "1" if x.lower() > y.lower() else ""
elif _type == 'float':
_typer = float
elif _type == 'int':
_typer = int
if _typer is not None:
try:
if _typer(x) > _typer(y):
return "1"
except ValueError:
pass
return ""
return _cmp(operator.gt, x, y, _type)


@script_function(documentation=N_(
Expand All @@ -776,24 +749,7 @@ def func_gt(parser, x, y, _type=None):
the default comparison method if `type` is not specified."""
))
def func_gte(parser, x, y, _type=None):
if not _type:
_type = 'int'
_typer = None
if _type == 'text':
return "" if x < y else "1"
elif _type == 'nocase':
return "" if x.lower() < y.lower() else "1"
elif _type == 'float':
_typer = float
elif _type == 'int':
_typer = int
if _typer is not None:
try:
if _typer(x) >= _typer(y):
return "1"
except ValueError:
pass
return ""
return _cmp(operator.ge, x, y, _type)


@script_function(documentation=N_(
Expand Down