Skip to content
Snippets Groups Projects
Commit f8f37b34 authored by Sebastien Ponce's avatar Sebastien Ponce
Browse files

Merge branch 'jzhuo_AddArrayGrammar' into 'master'

Add array input support for the thor functor grammar

See merge request lhcb/Rec!3283
parents c87b3846 3e848e7a
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
# or submit itself to any jurisdiction. # # or submit itself to any jurisdiction. #
############################################################################### ###############################################################################
from typing import List, Union from typing import List, Union
from array import array
from Functors.grammar import BoundFunctor, Functor, ComposedBoundFunctor from Functors.grammar import BoundFunctor, Functor, ComposedBoundFunctor
from PyConf.dataflow import DataHandle from PyConf.dataflow import DataHandle
import functools import functools
...@@ -37,7 +37,7 @@ VALUE_OR = Functor( ...@@ -37,7 +37,7 @@ VALUE_OR = Functor(
"Functional::ValueOr", "Functional::ValueOr",
'''Return contained value of optional or specified default if optional is empty''', '''Return contained value of optional or specified default if optional is empty''',
Params=[('Value', 'The default value.', (bool, int, float, str, list, Params=[('Value', 'The default value.', (bool, int, float, str, list,
tuple, dict))]) tuple, dict, array))])
HAS_VALUE = Functor('HAS_VALUE', "Functional::HasValue", HAS_VALUE = Functor('HAS_VALUE', "Functional::HasValue",
'''Return true if the input has a valid value.''') '''Return true if the input has a valid value.''')
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
import logging import logging
from inspect import cleandoc from inspect import cleandoc
from textwrap import indent from textwrap import indent
from array import array
from Functors.common import top_level_namespace from Functors.common import top_level_namespace
from GaudiKernel.Configurable import Configurable from GaudiKernel.Configurable import Configurable
...@@ -132,6 +133,26 @@ def python_to_cpp_str(obj, in_container=False, return_type=False): ...@@ -132,6 +133,26 @@ def python_to_cpp_str(obj, in_container=False, return_type=False):
ret_val = cpp_container_type + '{' + ', '.join(items) + '}' ret_val = cpp_container_type + '{' + ', '.join(items) + '}'
if return_type: return (ret_val, cpp_container_type) if return_type: return (ret_val, cpp_container_type)
return ret_val return ret_val
elif isinstance(obj, array):
cpp_types = {
'b': 'signed char',
'B': 'unsigned char',
'u': 'wchart_t',
'h': 'signed short',
'H': 'unsigned short',
'i': 'signed int',
'I': 'unsigned int',
'l': 'signed long',
'L': 'unsigned long',
'q': 'signed long long',
'Q': 'unsigned long long',
'f': 'float',
'd': 'double'
}
if obj.typecode not in cpp_types.keys():
raise TypeError(f'Invalid array typecode: {obj.typecode}')
vector_content = ', '.join([str(value) for value in obj.tolist()])
return f'std::vector<{cpp_types[obj.typecode]}>{{ {vector_content} }}'
elif isinstance(obj, BoundFunctor): elif isinstance(obj, BoundFunctor):
ret_val = obj.code() ret_val = obj.code()
#remove the last curly brace for the type #remove the last curly brace for the type
......
...@@ -13,7 +13,8 @@ from __future__ import print_function ...@@ -13,7 +13,8 @@ from __future__ import print_function
from __future__ import division from __future__ import division
from past.utils import old_div from past.utils import old_div
import pickle import pickle
from Functors import PT, ISMUON, MINIPCUT from array import array
from Functors import PT, ISMUON, MINIPCUT, VALUE_OR
from Functors import math as fmath from Functors import math as fmath
from Functors.tests.categories import dummy_data_pv_container as pvs_dh from Functors.tests.categories import dummy_data_pv_container as pvs_dh
from GaudiKernel.SystemOfUnits import * from GaudiKernel.SystemOfUnits import *
...@@ -47,6 +48,22 @@ def test_basic_grammar(): ...@@ -47,6 +48,22 @@ def test_basic_grammar():
not_empty(f6) not_empty(f6)
def test_array_input():
check(VALUE_OR(array('b', [1, 2, 3]))) # vector< signed char >
check(VALUE_OR(array('B', [1, 2, 3]))) # vector< unsigned char >
check(VALUE_OR(array('u', ['a', 'b']))) # vector< wchart_t >
check(VALUE_OR(array('h', [1, 2, 3]))) # vector< signed short >
check(VALUE_OR(array('H', [1, 2, 3]))) # vector< unsigned short >
check(VALUE_OR(array('i', [1, 2, 3]))) # vector< signed int >
check(VALUE_OR(array('I', [1, 2, 3]))) # vector< unsigned int >
check(VALUE_OR(array('l', [1, 2, 3]))) # vector< signed long >
check(VALUE_OR(array('L', [1, 2, 3]))) # vector< unsigned long >
check(VALUE_OR(array('q', [1, 2, 3]))) # vector< signed long long >
check(VALUE_OR(array('Q', [1, 2, 3]))) # vector< unsigned long long >
check(VALUE_OR(array('f', [1, 2, 3]))) # vector< float >
check(VALUE_OR(array('d', [1, 2, 3]))) # vector< double >
def test_arithmetric(): def test_arithmetric():
check(30 * GeV < PT) check(30 * GeV < PT)
check(PT > 30 * GeV) check(PT > 30 * GeV)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment