Page 1 of 1

generate dictionary of hoc (h.) things

Posted: Fri Nov 21, 2014 5:00 pm
by wwlytton
I wanted a dictionary of hoc stuff for reasons that in retrospect was unnecessary, but having done this I thought I would share it

hdict = dict([(name,h.__getattribute__(name)) for name in dir(h) if hasattr(h,name)])

the hasattr() turned out to be the trick since for reasons obscure some things turn up in the dir() that are not accessible -- thanks to robert mcdougal for showing me that

note that this is not a dynamic dict so has to be regenerated after loading something

hdict['tstop'] # not found
h.load_file("stdrun.hoc")
hdict = dict([(name,h.__getattribute__(name)) for name in dir(h) if hasattr(h,name)])
hdict['tstop'] # 5.0

Re: generate dictionary of hoc (h.) things

Posted: Fri Nov 21, 2014 5:09 pm
by wwlytton
in python 2.7+ can also do dict comprehensions so

hdict = {name:h.__getattribute__(name) for name in dir(h) if hasattr(h,name)}