Changeset 278ddee in sasview for src/sas/sascalc/data_util


Ignore:
Timestamp:
Apr 11, 2017 9:51:05 AM (7 years ago)
Author:
krzywon
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, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
beba407
Parents:
7f75a3f (diff), 97c60f8 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Jeff Krzywon <krzywon@…> (04/11/17 09:51:05)
git-committer:
krzywon <krzywon@…> (04/11/17 09:51:05)
Message:

Merge branch 'master' into ticket-876

# Conflicts:
# src/sas/sascalc/dataloader/readers/ascii_reader.py
# src/sas/sascalc/dataloader/readers/xml_reader.py

Location:
src/sas/sascalc/data_util
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/data_util/calcthread.py

    r934ce649 r7432acb  
    88import traceback 
    99import sys 
     10import logging 
    1011 
    1112if sys.platform.count("darwin") > 0: 
     
    2122    from time import clock 
    2223    from time import sleep 
     24 
     25logger = logging.getLogger(__name__) 
    2326 
    2427 
     
    202205    def update(self, **kwargs): 
    203206        """Update GUI with the lastest results from the current work unit.""" 
    204         if self.updatefn != None and clock() > self._time_for_update: 
     207        if self.updatefn is not None and clock() > self._time_for_update: 
    205208            self._lock.acquire() 
    206209            self._time_for_update = clock() + self._delay 
     
    218221    def complete(self, **kwargs): 
    219222        """Update the GUI with the completed results from a work unit.""" 
    220         if self.completefn != None: 
     223        if self.completefn is not None: 
    221224            self.completefn(**kwargs) 
    222225            sleep(self.yieldtime) 
     
    243246            except Exception: 
    244247                pass 
    245         import logging 
    246         logging.error(traceback.format_exc()) 
     248        logger.error(traceback.format_exc()) 
    247249        #print 'CalcThread exception', 
    248250 
  • src/sas/sascalc/data_util/qsmearing.py

    r9a5097c r235f514  
    4242    # This checks for 2D data (does not throw exception because fail is common) 
    4343    if  data.__class__.__name__ not in ['Data1D', 'Theory1D']: 
    44         if data == None: 
     44        if data is None: 
    4545            return None 
    46         elif data.dqx_data == None or data.dqy_data == None: 
     46        elif data.dqx_data is None or data.dqy_data is None: 
    4747            return None 
    4848        return PySmear2D(data) 
  • src/sas/sascalc/data_util/registry.py

    rb699768 r7f75a3f  
    77""" 
    88 
    9 import os.path 
     9from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException 
     10 
    1011 
    1112class ExtensionRegistry(object): 
     
    6162    def __init__(self, **kw): 
    6263        self.loaders = {} 
     64 
    6365    def __setitem__(self, ext, loader): 
    6466        if ext not in self.loaders: 
    6567            self.loaders[ext] = [] 
    6668        self.loaders[ext].insert(0,loader) 
     69 
    6770    def __getitem__(self, ext): 
    6871        return self.loaders[ext] 
     72 
    6973    def __contains__(self, ext): 
    7074        return ext in self.loaders 
     75 
    7176    def formats(self): 
    7277        """ 
     
    7681        names.sort() 
    7782        return names 
     83 
    7884    def extensions(self): 
    7985        """ 
     
    8389        exts.sort() 
    8490        return exts 
     91 
    8592    def lookup(self, path): 
    8693        """ 
    8794        Return the loader associated with the file type of path. 
    8895         
    89         Raises ValueError if file type is not known. 
     96        :param path: Data file path 
     97        :raises ValueError: When no loaders are found for the file. 
     98        :return: List of available readers for the file extension 
    9099        """         
    91100        # Find matching extensions 
     
    105114        # Raise an error if there are no matching extensions 
    106115        if len(loaders) == 0: 
    107             raise ValueError, "Unknown file type for "+path 
    108         # All done 
     116            raise ValueError("Unknown file type for "+path) 
    109117        return loaders 
     118 
    110119    def load(self, path, format=None): 
    111120        """ 
    112121        Call the loader for the file type of path. 
    113122 
    114         Raises ValueError if no loader is available. 
    115         Raises KeyError if format is not available. 
    116         May raise a loader-defined exception if loader fails.         
     123        :raise ValueError: if no loader is available. 
     124        :raise KeyError: if format is not available. 
     125        May raise a loader-defined exception if loader fails. 
    117126        """ 
     127        loaders = [] 
    118128        if format is None: 
    119             loaders = self.lookup(path) 
     129            try: 
     130                loaders = self.lookup(path) 
     131            except ValueError as e: 
     132                pass 
    120133        else: 
    121             loaders = self.loaders[format] 
     134            try: 
     135                loaders = self.loaders[format] 
     136            except KeyError as e: 
     137                pass 
    122138        for fn in loaders: 
    123139            try: 
    124140                return fn(path) 
    125             except: 
    126                 pass # give other loaders a chance to succeed 
     141            except Exception as e: 
     142                pass  # give other loaders a chance to succeed 
    127143        # If we get here it is because all loaders failed 
    128         raise # reraises last exception 
    129  
    130 def test(): 
    131     reg = ExtensionRegistry() 
    132     class CxError(Exception): pass 
    133     def cx(file): return 'cx' 
    134     def new_cx(file): return 'new_cx' 
    135     def fail_cx(file): raise CxError 
    136     def cat(file): return 'cat' 
    137     def gunzip(file): return 'gunzip' 
    138     reg['.cx'] = cx 
    139     reg['.cx1'] = cx 
    140     reg['.cx'] = new_cx 
    141     reg['.gz'] = gunzip 
    142     reg['.cx.gz'] = new_cx 
    143     reg['.cx1.gz'] = fail_cx 
    144     reg['.cx1'] = fail_cx 
    145     reg['.cx2'] = fail_cx 
    146     reg['new_cx'] = new_cx 
    147  
    148     # Two loaders associated with .cx 
    149     assert reg.lookup('hello.cx') == [new_cx,cx] 
    150     # Make sure the last loader applies first 
    151     assert reg.load('hello.cx') == 'new_cx' 
    152     # Make sure the next loader applies if the first fails 
    153     assert reg.load('hello.cx1') == 'cx' 
    154     # Make sure the format override works 
    155     assert reg.load('hello.cx1',format='.cx.gz') == 'new_cx' 
    156     # Make sure the format override works 
    157     assert reg.load('hello.cx1',format='new_cx') == 'new_cx' 
    158     # Make sure the case of all loaders failing is correct 
    159     try:  reg.load('hello.cx2') 
    160     except CxError: pass # correct failure 
    161     else: raise AssertError,"Incorrect error on load failure" 
    162     # Make sure the case of no loaders fails correctly 
    163     try: reg.load('hello.missing') 
    164     except ValueError,msg: 
    165         assert str(msg)=="Unknown file type for hello.missing",'Message: <%s>'%(msg) 
    166     else: raise AssertError,"No error raised for missing extension" 
    167     assert reg.formats() == ['new_cx'] 
    168     assert reg.extensions() == ['.cx','.cx.gz','.cx1','.cx1.gz','.cx2','.gz'] 
    169     # make sure that it supports multiple '.' in filename 
    170     assert reg.load('hello.extra.cx1') == 'cx' 
    171     assert reg.load('hello.gz') == 'gunzip' 
    172     assert reg.load('hello.cx1.gz') == 'gunzip' # Since .cx1.gz fails 
    173  
    174 if __name__ == "__main__": test() 
     144        raise NoKnownLoaderException(e.message)  # raise generic exception 
Note: See TracChangeset for help on using the changeset viewer.