Search using the CAS number, barcode or chemical name

def search(self, query, locations: list=None):
        '''Search using the CAS number, barcode or chemical name
        '''
        cas_number = re.search(r"\b[1-9]{1}[0-9]{1,5}-\d{2}-\d\b", str(query))
        if cas_number:
            query = cas_number[0]
            search_type = 'cas'   
        else:
            try:
                query = int(query)
                search_type = 'barcode'
            except ValueError:
                query = f"%{query}%"
                search_type = 'name'
        if not locations:
            locations = self.get_locations(filter_to_my_group=True)
            locations = [loc.inventory_id for loc in locations]
        data = {
            'groupid': self.groupid,
            'searchtype': search_type,
            'searchterm':  query,
            'limitlocations': locations.append(1)
        }
        r = self._post('search-search', referer_path='search', data=data)
        
        #return a list of container objects
        if r['searchresults']['containers']:
            containers = []
            for container in r['searchresults']['containers']:
                loc = Location(name=container.get('location'))
                ct = Container(
                    inventory_id = container.get('id'), 
                    compound_id = container.get('sid'),
                    name=container.get('containername'),
                    location=loc,
                    size=container.get('size'),
                    smiles=container.get('smiles'),
                    cas=container.get('cas'),
                    comments=container.get('comments'),
                    barcode=container.get('barcode'),
                    supplier=container.get('supplier'),
                    date_acquired=container.get('dateacquired'),
                    owner=container.get('owner'))
                containers.append(ct)
            return containers
        else:
            return []