Ignore:
Timestamp:
Sep 22, 2017 5:14:38 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
f7d720f
Parents:
5a6a84e
Message:

exec magic for dereferencing default units didn't work in python 3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/readers/cansas_reader.py

    r574adc7 re6e89c4  
    630630                else: 
    631631                    save_in = "current_datainfo" 
    632                 exec("default_unit = self.{0}.{1}".format(save_in, unitname)) 
     632                default_unit = getattrchain(self, '.'.join((save_in, unitname))) 
    633633                if (local_unit and default_unit 
    634634                        and local_unit.lower() != default_unit.lower() 
     
    636636                    if HAS_CONVERTER: 
    637637                        # Check local units - bad units raise KeyError 
     638                        #print("loading", tagname, node_value, local_unit, default_unit) 
    638639                        data_conv_q = Converter(local_unit) 
    639640                        value_unit = default_unit 
     
    716717        doc, _ = self._to_xml_doc(datainfo) 
    717718        # Write the file 
    718         file_ref = open(filename, 'w') 
     719        file_ref = open(filename, 'wb') 
    719720        if self.encoding is None: 
    720721            self.encoding = "UTF-8" 
     
    12831284            if units is not None: 
    12841285                toks = variable.split('.') 
    1285                 exec("local_unit = storage.%s_unit" % toks[0]) 
     1286                # TODO: why split() when accessing unit, but not when setting value? 
     1287                local_unit = getattr(storage, toks[0]+"_unit") 
    12861288                if local_unit is not None and units.lower() != local_unit.lower(): 
    12871289                    if HAS_CONVERTER == True: 
    12881290                        try: 
    12891291                            conv = Converter(units) 
    1290                             exec("storage.%s = %g" % 
    1291 -                                (variable, conv(value, units=local_unit))) 
     1292                            setattrchain(storage, variable, conv(value, units=local_unit)) 
    12921293                        except Exception: 
    12931294                            _, exc_value, _ = sys.exc_info() 
     
    13101311                            raise ValueError(err_mess) 
    13111312                else: 
    1312                     exec("storage.%s = value" % variable) 
     1313                    setattrchain(storage, variable, value) 
    13131314            else: 
    1314                 exec("storage.%s = value" % variable) 
     1315                setattrchain(storage, variable, value) 
    13151316 
    13161317    # DO NOT REMOVE - used in saving and loading panel states. 
     
    13751376        return True 
    13761377    return False 
     1378 
     1379def getattrchain(obj, chain, default=None): 
     1380    """Like getattr, but the attr may contain multiple parts separated by '.'""" 
     1381    for part in chain.split('.'): 
     1382        if hasattr(obj, part): 
     1383            obj = getattr(obj, part, None) 
     1384        else: 
     1385            return default 
     1386    return obj 
     1387 
     1388def setattrchain(obj, chain, value): 
     1389    """Like setattr, but the attr may contain multiple parts separated by '.'""" 
     1390    parts = list(chain.split('.')) 
     1391    for part in parts[-1]: 
     1392        obj = getattr(obj, part, None) 
     1393        if obj is None: 
     1394            raise ValueError("missing parent object "+part) 
     1395    setattr(obj, value) 
Note: See TracChangeset for help on using the changeset viewer.