def scaled_stack_exists(folder, sf, pixel_size): for im_path in listdir(folder): im_name, im_ext = splitext(im_path) if im_ext not in ['.nrrd', '.tiff', '.tif']: continue split_path = im_name.split('_') sf_index = split_path.index('scaled') + 1 pixel_index = split_path.index('pixel') + 1 im_sf = strconv.convert(split_path[sf_index]) try: float(im_sf) except ValueError: print( "Scaled image has a non-deafult scaling format within its name" ) sys.exit() im_pixel = float(split_path[pixel_index]) if abs(im_sf - sf) < 0.1 and abs(im_pixel - pixel_size) < 0.1: return True return False
def __init__(self, file_path): from strconv import convert from configparser import ConfigParser conf = ConfigParser() conf.read(file_path) for section in conf.sections(): elem = _Section() setattr(AutoConfig, section, elem) for (k, v) in conf.items(section): setattr(elem, k, convert(v))
def test_convert(self): self.assertEqual(strconv.convert('-3'), -3) self.assertEqual(strconv.convert('+0.4'), 0.4) self.assertEqual(strconv.convert('true'), True) self.assertEqual(strconv.convert('3/20/2013'), date(2013, 3, 20)) self.assertEqual(strconv.convert('5:40 PM'), time(17, 40)) self.assertEqual(strconv.convert('March 4, 2013 5:40 PM'), datetime(2013, 3, 4, 17, 40, 0)) self.assertEqual(strconv.convert('March 4, 2013 12:00 AM'), datetime(2013, 3, 4, 0, 0))
def send_multiple_packets(sock, datas, wait_time=config['read_telemetry'].getfloat('cmd_wait_time')): packets = '' for data in datas: data = data.strip().split(' ') cmd = data[0] packet = multiwii.tx_generate(cmd, *[strconv.convert(d) for d in data[1:]]) packets += packet print('|', cmd, packet.encode('hex')) sock.send(packets) print('<', sock.recv(recv_buffer))
def send_multiple_packets( sock, datas, wait_time=config['read_telemetry'].getfloat('cmd_wait_time')): packets = '' for data in datas: data = data.strip().split(' ') cmd = data[0] packet = multiwii.tx_generate(cmd, *[strconv.convert(d) for d in data[1:]]) packets += packet print('|', cmd, packet.encode('hex')) sock.send(packets) print('<', sock.recv(recv_buffer))
def __init__(self, attrs): for key in attrs.keys(): try: attrs[key] = strconv.convert(attrs[key]) except: pass try: if key.endswith('s'): if ',' in attrs[key]: attrs[key] = attrs[key].split(',') except: pass self.__dict__.update(**attrs)
def inferType(key, value): if isinstance(value, list): res = [] for v in value: if type(v) is str: v = str(v.decode('string_escape')) inferred = strconv.infer(v) #print 'List - ', key, ': ', inferred if (inferred is None) or re.match('^date', inferred): res.append(str(v)) else: res.append(str(strconv.convert(v))) #print key, ' result: ', [type(r) for r in res] res = res[0] if len(res) == 1 else ";".join(res) else: if type(value) is str: value = str(value.decode('string_escape')) inferred = strconv.infer(value) #print 'Single - ', key, ': ', inferred if (inferred is None) or re.match('^date', inferred): res = value else: res = strconv.convert(value) #print key, ' result: ', type(res) return res
def make_kwargs_dict(kwargs_list): """Transforms a list of keyword arguments into a keyword arguments dictionary. Splits on the "=" symbol. E.g. ["first=1", "second=2", "third=3"] gets transformed into {"first": 1, "second": 2, "third": 3}. Arguments: kwargs_list {list} -- list of keyword arguments Returns: dict -- dictionary of keyword arguments """ kwargs_dict = { # key -> value converted as the correct type kwarg.split("=")[0]: convert(kwarg.split("=")[1]) for kwarg in kwargs_list } return kwargs_dict
def scaled_stack_exists(folder, sf, pixel_size): for im_path in listdir(folder): im_name, im_ext = splitext(im_path) if im_ext not in ['.nrrd', '.tiff', '.tif']: continue split_path = im_name.split('_') try: sf_index = split_path.index('scaled') + 1 except ValueError: raise ValueError( "'_scaled_ is not in the filename: {}".format(im_name)) try: pixel_index = split_path.index('pixel') + 1 except ValueError: raise ValueError( "'_pixel_' is not in the filename: {}".format(im_name)) im_sf = strconv.convert(split_path[sf_index]) try: float(im_sf) except ValueError: print(( "Scaled image {} has a non-deafult scaling format within its name" .format(im_name))) return False im_pixel = float(split_path[pixel_index]) if abs(im_sf - sf) < 0.1 and abs(im_pixel - pixel_size) < 0.1: return True return False
def test_convert_include_type(self): self.assertEqual(strconv.convert('-3', include_type=True), (-3, 'int'))