Skip to content

Commit

Permalink
fix: action=append for argparse; deduplicate; recursive makeListOfDic…
Browse files Browse the repository at this point in the history
…tFromJSON
  • Loading branch information
wdconinc committed Feb 11, 2024
1 parent 62b7b14 commit c452e5a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
49 changes: 30 additions & 19 deletions DDG4/python/DDSim/Helper/Action.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ def __init__(self):
self._trackerSDTypes = ['tracker']
self._calorimeterSDTypes = ['calorimeter']
self._run = []
self._run_EXTRA = {"action": "append"}
self._event = []
self._event_EXTRA = {"action": "append"}
self._track = []
self._track_EXTRA = {"action": "append"}
self._step = []
self._step_EXTRA = {"action": "append"}
self._stack = []
self._stack_EXTRA = {"action": "append"}
self._closeProperties()

@property
Expand Down Expand Up @@ -148,27 +153,23 @@ def makeListOfDictFromJSON(val):
import json
try:
val = json.loads(val)
# interpret json structure
return Action.makeListOfDictFromJSON(val)
except ValueError:
val = [dict(name=v) for v in val.split(",")]
# returns: [ { "name": "Geant4TestEventAction" } ]
return [dict(name=v) for v in val.split(",")]
if isinstance(val, tuple):
# assumes: ( "Geant4TestEventAction", {"Property_int": 10} )
# creates: { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} }
# returns: [ { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} } ]
# note: not able to be specified as json which only allows a list
val = dict(name=val[0], parameter=val[1])
return [dict(name=val[0], parameter=val[1])]
if isinstance(val, dict):
# assumes: { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} }
# creates: [ { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} } ]
val = [val]
# returns: [ { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} } ]
return [val]
if isinstance(val, list):
if not val:
# empty list
return []
if isinstance(val[0], str):
# assumes: [ "Geant4TestEventAction", "Geant4TestEventAction" ]
return [dict(name=v) for v in val]
if isinstance(val[0], dict):
# assumes: [ { "name": "Geant4TestEventAction", "parameter": {"Property_int": 10} } ]
return val
# interpret each list entry into a list and concatenate
return [i for v in val for i in Action.makeListOfDictFromJSON(v)]
raise RuntimeError("Commandline setting of action is not successful for: %s " % val)

@property
Expand All @@ -178,7 +179,9 @@ def run(self):

@run.setter
def run(self, val):
self._run.extend(Action.makeListOfDictFromJSON(val))
for action in Action.makeListOfDictFromJSON(val):
if action not in self._run:
self._run.append(action)

@property
def event(self):
Expand All @@ -187,7 +190,9 @@ def event(self):

@event.setter
def event(self, val):
self._event.extend(Action.makeListOfDictFromJSON(val))
for action in Action.makeListOfDictFromJSON(val):
if action not in self._event:
self._event.append(action)

@property
def track(self):
Expand All @@ -196,7 +201,9 @@ def track(self):

@track.setter
def track(self, val):
self._track.extend(Action.makeListOfDictFromJSON(val))
for action in Action.makeListOfDictFromJSON(val):
if action not in self._track:
self._track.append(action)

@property
def step(self):
Expand All @@ -205,7 +212,9 @@ def step(self):

@step.setter
def step(self, val):
self._step.extend(Action.makeListOfDictFromJSON(val))
for action in Action.makeListOfDictFromJSON(val):
if action not in self._step:
self._step.append(action)

@property
def stack(self):
Expand All @@ -214,4 +223,6 @@ def stack(self):

@stack.setter
def stack(self, val):
self._stack.extend(Action.makeListOfDictFromJSON(val))
for action in Action.makeListOfDictFromJSON(val):
if action not in self._stack:
self._stack.append(action)
4 changes: 4 additions & 0 deletions DDTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ if (DD4HEP_USE_GEANT4)
--action.step '\[ \"Geant4TestStepAction/StepActionCLI3\" , \"Geant4TestStepAction/StepActionCLI4\" \]'
--action.stack '\{ \"name\" : \"Geant4TestStackAction/StackActionCLI1\" , \"parameter\" : \{ \"Property_int\" : 10 \} \}'
--action.stack '\[ \{ \"name\" : \"Geant4TestStackAction/StackActionCLI2\" , \"parameter\" : { \"Property_int\" : 10 \} \} \]'
--printLevel VERBOSE
)
set_tests_properties( t_ddsimUserActions PROPERTIES
PASS_REGULAR_EXPRESSION "Deleting object StepActionCLI1"
)

endif()
Expand Down

0 comments on commit c452e5a

Please sign in to comment.