How-to print a constraint?

33 views
Skip to first unread message

Bernd Becker

unread,
Mar 12, 2014, 8:24:47 AM3/12/14
to scitoo...@googlegroups.com

Hello,

I set the following constraints on latitude and longitude:

IceNS = iris.Constraint(  latitude=lambda rwv: 20. <= rwv <= 40.)
IceEW = iris.Constraint( longitude=lambda rwv: 0. <= rwv <=  30.)


And now I would like to print what is actually set:

print IceNS
Constraint(coord_values={'latitude': <function <lambda> at 0x377f050>})

but I am after the values of lambda

I tried the following to no avail.

 dir ( IceNS)
['_CIM_extract', '__and__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_coord_constraints', '_coord_values', '_coordless_match', '_cube_func', '_name', 'extract']
>>> IceNS._coord_constraints
[_CoordConstraint('latitude', <function <lambda> at 0x377f050>)]
>>>
>>> IceNS.extract                      
<bound method Constraint.extract of Constraint(coord_values={'latitude': <function <lambda> at 0x377f050>})>
>>> print IceNS._coord_constraints
[_CoordConstraint('latitude', <function <lambda> at 0x377f050>)]
>>> print IceNS._coord_values    
{'latitude': <function <lambda> at 0x377f050>}
>>> print IceNS._coord_values.points
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'points'
>>> dir( IceNS._coord_values)     
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
>>> print IceNS._coord_values.viewvalues
<built-in method viewvalues of dict object at 0x3b40d30>
>>> print IceNS._coord_values.viewitems
<built-in method viewitems of dict object at 0x3b40d30>
>>>
>>> print IceNS._coord_values.itervalues
<built-in method itervalues of dict object at 0x3b40d30>
>>> print IceNS._coord_values.items    
<built-in method items of dict object at 0x3b40d30>

How do you print a constraint so that you can check what threshold values are actually doing the constraint?
Thanks!

Andrew Dawson

unread,
Mar 12, 2014, 10:42:15 AM3/12/14
to scitoo...@googlegroups.com
The line you came across is the most useful you are likely to be able to get I think: print IceNS._coord_constraints
 
It tells you which coordinates are to be constrained by functions. We could make it possible for these to appear when you print a Constraint, but to be honest it isn't all that useful.
 
The constraint is defined by an arbitrary function in this case rather than hard coded values. It is not possible in general to work out what the constraint actually does, only to see whether the function returns True or False for any given input. It is in principal possible to print the source code of a lambda function but *only* if it is defined in a script, this doesn't work if it was defined in a python shell. Also consider that the constraining function may not be a lambda function, it can be *any* function, so inspecting the source is a doubly bad idea. Essentially it is not possible to do what you want to do.

Andrew Dawson

unread,
Mar 12, 2014, 10:47:14 AM3/12/14
to scitoo...@googlegroups.com
Although I should add that if your constraint is a value, like Constraint(latitude=33) then I think "print IceNS._coord_constraints" may print something useful, but I don't know for sure and I can't check right now!
Reply all
Reply to author
Forward
0 new messages