Skip to content

Commit 8f2a1db

Browse files
Merged in cbillington/labscript/properties (pull request #40)
Changes to set_passed_properties
2 parents 8ea9749 + 0334349 commit 8f2a1db

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
# elif sys.argv[0]:
4040
# labscript_init(sys.argv[0].replace('.py','.h5'), labscript_file=sys.argv[0], new=True, overwrite=overwrite)
4141

42-
__version__ = '2.4.0'
42+
__version__ = '2.5.0'

labscript.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import sys
2424
import subprocess
2525
import keyword
26-
from inspect import getargspec
26+
from inspect import getcallargs
2727
from functools import wraps
2828

2929
# Notes for v3
@@ -141,12 +141,16 @@ def fastflatten(inarray, dtype):
141141

142142
def set_passed_properties(property_names = {}):
143143
"""
144-
This decorator is intended to wrap the __init__ functions and to
145-
write any selected kwargs into the properties.
146-
147-
names is a dictionary {key:val}, where each val
144+
Decorator for device __init__ methods that saves the listed arguments/keyword
145+
arguments as properties. Argument values as passed to __init__ will be saved, with
146+
the exception that if an instance attribute exists after __init__ has run that has
147+
the same name as an argument, the instance attribute will be saved instead of the
148+
argument value. This allows code within __init__ to process default arguments
149+
before they are saved.
150+
151+
property_names is a dictionary {key:val}, where each val
148152
is a list [var1, var2, ...] of variables to be pulled from
149-
properties_dict and added to the property with name key (it's location)
153+
properties_dict and added to the property with name key (its location)
150154
151155
internally they are all accessed by calling self.get_property()
152156
"""
@@ -156,21 +160,24 @@ def new_function(inst, *args, **kwargs):
156160

157161
return_value = func(inst, *args, **kwargs)
158162

159-
# Introspect arguments and named arguments functions. in python 3 this is
160-
# a pair of func.__something__ calls and no import from argspec is needed
161-
a = getargspec(func)
162-
163-
if a.defaults is not None:
164-
args_dict = {key:val for key,val in zip(a.args[-len(a.defaults):],a.defaults)}
165-
else:
166-
args_dict = {}
167-
168-
# Update this list with the values from the passed keywords
169-
args_dict.update(kwargs)
163+
# Get a dict of the call arguments/keyword arguments by name:
164+
call_values = getcallargs(func, inst, *args, **kwargs)
165+
166+
all_property_names = set()
167+
for names in property_names.values():
168+
all_property_names.update(names)
169+
170+
property_values = {}
171+
for name in all_property_names:
172+
# If there is an instance attribute with that name, use that, otherwise
173+
# use the call value:
174+
if hasattr(inst, name):
175+
property_values[name] = getattr(inst, name)
176+
else:
177+
property_values[name] = call_values[name]
170178

171-
# print args_dict
172-
# print property_names
173-
inst.set_properties(args_dict, property_names)
179+
# Save them:
180+
inst.set_properties(property_values, property_names)
174181

175182
return return_value
176183

0 commit comments

Comments
 (0)