def normalize_timedelta(timedelta): """ Given a string like "1w" or "5d", convert it to an integer in seconds. Integers without a suffix are interpreted as seconds. Note: not related to the datetime timedelta class. """ try: return int(timedelta) except ValueError as e: suffix_multipliers = {'s': 1, 'm': 60, 'h': 60*60, 'd': 60*60*24, 'w': 60*60*24*7, 'M': 60*60*24*30, 'y': 60*60*24*365} re_match = re.match('([0-9]+)([\s]*)([a-zA-Z]*)', timedelta) if re_match is None: msg = 'Could not parse time delta {0}'.format(timedelta) raise dxpy.AppInternalError(msg) t, space, suffix = re_match.groups() if suffix in suffix_multipliers: normalized_time = int(t) * suffix_multipliers[suffix] elif suffix not in suffix_multipliers and len(suffix) > 1 and suffix[0] in suffix_multipliers: old_suffix = suffix suffix = suffix[0] print('Not familiar with suffix {0}. Assuming you meant {1}.'.format(old_suffix, suffix), file=sys.stderr) normalized_time = int(t) * suffix_multipliers[suffix] else: msg = 'Valid suffixes for time duration are {0}.'.format(str(suffix_multipliers.keys())) raise dxpy.AppInternalError(msg) return normalized_time
def normalize_timedelta(timedelta): """ Given a string like "1w" or "5d", convert it to an integer in seconds. Integers without a suffix are interpreted as seconds. It is assumed this function is run inside a dnanexus job. Note: not related to the datetime timedelta class. Args: timedelta (int or str): An integer in seconds or a string of the form "1w" or "5d" Returns: normalized_time (int): An integer in seconds Raises: dxpy.AppInternalError if suffixes are not known. Known suffixes are defined in the dictionary suffix_multipliers. Examples: >>> normalize_timedelta(500) 500 >>> normalize_timedelta('5w') 3024000 >>> normalize_timedelta('1d') 86400 >>> normalize_timedelta('1h') 3600 """ try: return int(timedelta) except ValueError as e: suffix_multipliers = {'s': 1, 'm': 60, 'h': 60*60, 'd': 60*60*24, 'w': 60*60*24*7, 'M': 60*60*24*30, 'y': 60*60*24*365} re_match = re.match('([0-9]+)([\s]*)([a-zA-Z]*)', timedelta) if re_match is None: msg = 'Could not parse time delta {0}'.format(timedelta) raise dxpy.AppInternalError(msg) t, _, suffix = re_match.groups() if suffix in suffix_multipliers: normalized_time = int(t) * suffix_multipliers[suffix] elif suffix not in suffix_multipliers and len(suffix) > 1 and suffix[0] in suffix_multipliers: old_suffix = suffix suffix = suffix[0] print('Not familiar with suffix {0}. Assuming you meant {1}.'.format(old_suffix, suffix), file=sys.stderr) normalized_time = int(t) * suffix_multipliers[suffix] else: msg = 'Valid suffixes for time duration are {0}.'.format(str(suffix_multipliers.keys())) raise dxpy.AppInternalError(msg) return normalized_time
def DownloadFilesFromArray(input_ids): print "\nDownloading {n} files".format(n=len(input_ids)) if len(input_ids) < 1: raise dxpy.AppInternalError("No files were given as input") filenames = [] start_time = time.time() for id in input_ids: fn = dxpy.describe(id)["name"] filenames.append(fn) dxpy.download_dxfile(dxid=id, filename=fn) print "Downloaded {files} in {min} minutes".format( files=sorted(filenames), min=float((time.time() - start_time) / 60)) return sorted(filenames)
def normalize_timedelta(timedelta): """ Given a string like "1w" or "5d", convert it to an integer in seconds. Integers without a suffix are interpreted as seconds. Note: not related to the datetime timedelta class. """ try: return int(timedelta) except ValueError as e: t, suffix = timedelta[:-1], timedelta[-1:] suffix_multipliers = {'s': 1, 'm': 60, 'h': 60*60, 'd': 60*60*24, 'w': 60*60*24*7, 'M': 60*60*24*30, 'y': 60*60*24*365} if suffix not in suffix_multipliers: msg = 'Valid suffixes for time duration are shdwMy' raise dxpy.AppInternalError(msg) return int(t) * suffix_multipliers[suffix]
def locate_or_create_dx_drive(): # findDrives is an API method that has not been explicitly added to dxpy yet, so call the API method explicitly. drives = dxpy.DXHTTPRequest('/system/findDrives', {'name': DX_DRIVE_NAME}, always_retry=True)['results'] if len(drives) == 1: return drives[0] elif len(drives) == 0: # if no drive exists, create it access_key, secret_access_key = _get_aws_key() print('Creating drive with name: {0}'.format(DX_DRIVE_NAME)) # create drive using API call new_drive_def = {'name': DX_DRIVE_NAME, 'cloud': 'aws', 'credentials': {'accessKeyId': access_key, 'secretAccessKey': secret_access_key}} drive_id = dxpy.DXHTTPRequest('/drive/new', new_drive_def) print('Created drive with id: {0}'.format(DX_DRIVE_NAME)) return drive_id elif len(drives) > 1: msg = 'More than one drives found with name "{0}"'.format(DX_DRIVE_NAME) raise dxpy.AppInternalError(msg)
def _get_species_name(): project_desc = dxpy.describe(dxpy.PROJECT_CONTEXT_ID, input_params={'properties': True}) if 'species_name' in project_desc['properties']: return project_desc['properties']['species_name'] else: raise dxpy.AppInternalError('No species name was provided as input or as a property of this project.')