def __init__(self, patent_id, company, inventor_id, lat, lng, city, state, country): self.patent_id = patent_id self.inventor_id = inventor_id self.lat = fast_real(lat) self.lng = fast_real(lng) self.company = company self.called_api = False self.failed_api = False if self.lat == '' or self.lng == '': try: print(unidecode(city)) print(unidecode(state)) response = requests.get("http://dev.virtualearth.net/REST/v1/Locations/" + city + ' ' + state + ' ' + country, params={"include":"queryParse", "key":formulas.get_api_key()}) data = response.json() self.called_api = True self.lat = data['resourceSets'][0]['resources'][0]['point']['coordinates'][0] self.lng = data['resourceSets'][0]['resources'][0]['point']['coordinates'][1] except: print('error when finding lat and lng') self.failed_api = True self.lat = 404 self.lng = 404 self.city = city self.state = state self.country = country
def test_fast_real_with_coerce_given_dumb_class_responds_to_internal_ValueError( ): x = DumbFloatClass() assert fastnumbers.fast_real(x, coerce=True) is x with raises(ValueError): fastnumbers.fast_real(x, coerce=True, raise_on_invalid=True) assert fastnumbers.fast_real(x, coerce=True, default=5) == 5
def test_fast_real_given_float_string_returns_float(x): assume(not math.isnan(x)) y = repr(x) assert fastnumbers.fast_real(y, coerce=False) == x assert fastnumbers.fast_real(y, raise_on_invalid=True, coerce=False) == x assert fastnumbers.fast_real(y, None, True, coerce=False) == x assert isinstance(fastnumbers.fast_real(y, coerce=False), float)
def fixTimeAndAlt(data): '''Statyczna funkcja majaca na celu zamiane dlugosci wyrazonej w stopach na metry oraz ustawienie, aby czas liczony byl od 0s @param data - lista danych ''' start_time = fast_real(data[0][0]) for i in data: i[0] = fast_real(i[0]) - start_time i[0] = str(i[0]) i[3] = fast_real(i[3])*0.3048 i[3] = str(i[3])
def fixTimeAndAlt(data): '''Statyczna funkcja majaca na celu zamiane dlugosci wyrazonej w stopach na metry oraz ustawienie, aby czas liczony byl od 0s @param data - lista danych ''' start_time = fast_real(data[0][0]) for i in data: i[0] = fast_real(i[0]) - start_time i[0] = str(i[0]) i[3] = fast_real(i[3]) * 0.3048 i[3] = str(i[3])
def fixDistance(data): '''Funkcja statyczna obliczajaca przebyty dystans na podstawie szerokosci i wysokosci geografizcnej @param data - lista danych @return dist - lista zawierajaca przebyty dystans ''' start_Lo = fast_real(data[0][1]) start_La = fast_real(data[0][2]) dist = [] for i in data: dist.append(math.sqrt(math.pow(math.cos((math.pi*start_La)/180.0)*(fast_real(i[1])-start_Lo),2)+math.pow((fast_real(i[2])-start_La),2))*math.pi*(12756.274/360)*1000) return dist
def prepareSpeed(data): '''Statyczna funkcja obliczajaca predkosc samolotu w danym czasie @param data - lista danch @result speed - lista zawierajaca predkosci ''' dst = Fixer.fixDistance(data) speed = [] s = fast_real(dst[0]) for i in dst: dist = fast_real(i)-s speed.append(dist*3.6) s = i return speed
def prepareSpeed(data): '''Statyczna funkcja obliczajaca predkosc samolotu w danym czasie @param data - lista danch @result speed - lista zawierajaca predkosci ''' dst = Fixer.fixDistance(data) speed = [] s = fast_real(dst[0]) for i in dst: dist = fast_real(i) - s speed.append(dist * 3.6) s = i return speed
def get_min(data_list): new = [] for i in data_list: if i != -99: new.append(fast_real(i)) min = sorted(new) return round(min[0], 4) # calculates min of a list of values
def get_max(data_list): new = [] for i in data_list: if i != -99: new.append(fast_real(i)) max = sorted(new) return round(max[len(new) - 1], 4) # calculates max of a list of values
def create(self, token, sheet=None): if token.type == Token.OPERAND: #First lets check if token is a range and have has no sheet, in which case add sheet name to token value if token.subtype == Token.RANGE and '!' not in token.value and ':' in token.value: token.value = '{}!{}'.format(sheet, token.value) return RangeNode(token) elif token.subtype == Token.RANGE and '!' not in token.value and ':' not in token.value: token.value = '{}!{}'.format(sheet, token.value) return CellNode(token) # Then lets check if token is a number, in which case update assign float or int to token value elif token.subtype == Token.NUMBER: token.value = fast_real(token.value) return OperandNode(token) elif token.subtype == Token.LOGICAL: token.value = token.value == 'TRUE' return OperandNode(token) # Token must be subtype Text, Logical or Error - in which case do nothing else: return OperandNode(token) elif token.is_funcopen: return FunctionNode(token) elif token.is_operator: return OperatorNode(token)
def createHardcode(self,value): if isinstance(fast_real(value), str): tok = Token(value, Token.OPERAND, "TEXT") else: tok = Token(value, Token.OPERAND, "NUMBER") self.rpn.append(OperandNode(tok, self.model)) self.needs_calc = False
def convert_to_bytes(self, value): value = fast_real(value) if not (isinstance(value, int) or isinstance(value, float)): raise NoValidMessageException( str.format("Message is no int/float: {}", value)) value = int(value * 10**self._decimal) return struct.pack('!h', value)
def transform(self, value): if isinstance(value, string_types): value = re.sub(',', '.', value) try: return fastnumbers.fast_real(value, np.nan, raise_on_invalid=False) except TypeError: return np.nan
def received_info(self, edge, prob): global G global Links_weight_Dictionary print '-----------------------------------' print 'The previous link weight list is' print '-----------------------------------' print Links_weight_Dictionary print '-----------------------------------' print "The failed link is = {}".format(edge) print "The failed link probability is = {}".format(prob) Link = ast.literal_eval( edge) # To convert the link from Unicode into Tuple object F_Probability = fast_real( prob) # To convert the link failure probability into float Links_weight_Dictionary[ Link] = F_Probability # To update the link failure probability print '-----------------------------------' print 'The updated link weight list is' print '-----------------------------------' print Links_weight_Dictionary print '-----------------------------------'
def convert_to_bytes(self, value): value = fast_real(value) if not isinstance(value, int) or value < 0: raise NoValidMessageException( str.format("Message is no int or is smaller than 0: {}", value)) return struct.pack('!B', value)
def get_max(data_list): new = [] for i in data_list: if i != -99: new.append(fast_real(i)) max = sorted(new) return round(max[len(new)-1], 4) # calculates max of a list of values
def formula(self, excel_formula): ''' If excel formula is set, this TRIGGERS creation of rpn formula and tree @param excel_formula: excel formula as a string @return: rpn formula ''' self._formula = excel_formula logging.debug("Processing RPN for formula {} at cell {}".format( excel_formula, self)) #First check if formula starts with correct operator if str(excel_formula).startswith(('=', '+')): self.rpn = self.make_rpn(excel_formula) # creates list of precedents (who do I depend on) self.createPrec() # This means formula must be a hardcode else: logging.debug( "Formula does not start with = or +. Creating a hardcode cell") if isinstance(fast_real(self.address), str): tok = Token(self.address, Token.OPERAND, "TEXT") self.rpn.append(OperandNode(tok)) self.needs_calc = False else: tok = Token(self.address, Token.OPERAND, "NUMBER") self.rpn.append(OperandNode(tok)) logging.info("RPN is: {}".format(self.rpn))
def load_old_rects(floor_id, img_name): building, num = parse_name(img_name) prev_file = "rects/{}/{}_rects_{}.txt".format(floor_id, building, num) prev_rects = [] if isfile(prev_file): with open(prev_file, "r") as f: data = [l.strip() for l in f.read().split("\n") if len(l.strip()) > 0] for l in data: rect = l.split(" ") x, y, width, height = rect[0], rect[1], rect[2], rect[3] prev_rects.append({ "x": fast_real(x), "y": fast_real(y), "width": fast_real(width), "height": fast_real(height) }) return prev_rects
def get_Resume_info(self): resume_price = self.app.driver.find_element_by_css_selector( '.resume-item.resume-checkout .price-format span.value').text resume_name = self.app.driver.find_element_by_css_selector('h1').text resume_phone = { 'name': resume_name, 'cost': fastnumbers.fast_real(resume_price) }
def fixDistance(data): '''Funkcja statyczna obliczajaca przebyty dystans na podstawie szerokosci i wysokosci geografizcnej @param data - lista danych @return dist - lista zawierajaca przebyty dystans ''' start_Lo = fast_real(data[0][1]) start_La = fast_real(data[0][2]) dist = [] for i in data: dist.append( math.sqrt( math.pow( math.cos((math.pi * start_La) / 180.0) * (fast_real(i[1]) - start_Lo), 2) + math.pow((fast_real(i[2]) - start_La), 2)) * math.pi * (12756.274 / 360) * 1000) return dist
def get_mean(data_list): sum = 0.0 count = 0.0 # get mean for i in data_list: if i != -99: sum += fast_real(i) count += 1 mean = round(sum/count, 4) return mean # calculates mean of a list of values
def get_mean(data_list): sum = 0.0 count = 0.0 # get mean for i in data_list: if i != -99: sum += fast_real(i) count += 1 mean = round(sum / count, 4) return mean # calculates mean of a list of values
def product_info(self, device): model = device.find_element_by_css_selector(".item-info p a") price = device.find_element_by_css_selector( '.item-price.stick-bottom span.value').text converted = fastnumbers.fast_real(price) phone = ({ "name": model.text, "costa": converted, "link": model.get_attribute('href') }) return phone
def count_numbers(string): ss = string.split(' ') ii = [] for i in ss: if type(fast_real(i)) == float or i.isdigit(): i = float(i) ii.append(i) print(ii) print(len(ii)) print(sum(ii)) print(max(ii))
def get_numpy_val(input_name, input_val): """Convert input values to NumPy-compatible numerical values""" if input_val == "inf" or input_val == "+inf": return np.inf elif input_val == "-inf": return -np.inf else: num = fast_real(input_val) if isinstance(num, (int, long, float)): return num else: raise BadRequest( "Invalid value for \"{0}\" ({1}). Expected: -inf, inf, or a numerical value.".format(input_name, input_val))
def number_guess(numbers): """ number_guess tries to parse each potential value into a number. It returns the percentage (using 0.0 to 1.0) that were parsable. """ try: num_good_matches = 0.0 for probable_number in numbers: result = fast_real(probable_number) if (type(result) is int) or (type(result) is float): num_good_matches = num_good_matches + 1 return num_good_matches/len(numbers) except: return 0.0
def get_std_lat_lon(data_list): sum = 0.0 count = 0.0 new = [] # get mean for i in data_list: if i != -99: sum += fast_real(i) count += 1 new.append(fast_real(i)) mean = sum/count a = [] b = [] c = 0.0 for i in new: a.append(i - mean) for i in a: b.append(i*i) for i in b: c+=i std = math.sqrt(c/count) return round(std, 4) # calculates stdev of a list of values
def get_std_lat_lon(data_list): sum = 0.0 count = 0.0 new = [] # get mean for i in data_list: if i != -99: sum += fast_real(i) count += 1 new.append(fast_real(i)) mean = sum / count a = [] b = [] c = 0.0 for i in new: a.append(i - mean) for i in a: b.append(i * i) for i in b: c += i std = math.sqrt(c / count) return round(std, 4) # calculates stdev of a list of values
def extractValue(value): if isinstance(value, str): if value.startswith("#"): # Convert constant convertedValue = fast_real(value[1:]) if convertedValue == "false": return False elif convertedValue == "true": return True return convertedValue elif value.startswith("&"): return value else: newValue = getValueFromMemory(value) return newValue
def split(self, line): if fastnumbers is not None: return [fastnumbers.fast_real(s, nan=u'nan', inf=u'inf') \ for s in line.split()] l = [] for s in line.split(): try: l.append(int(s)) except: try: # Make sure we don't convert 'inf' and 'nan' strings to # float assert(not math.isinf(float(s))) assert(not math.isnan(float(s))) l.append(float(s)) except: l.append(s) return l
def create(self): #Assigning to variable token for readability token = self.token if token.type == Token.OPERAND: #First lets check if token is a RangeNode if token.subtype == Token.RANGE and ':' in token.value: return RangeNode(token, self.model) # Then check if this is a CellNode elif token.subtype == Token.RANGE and ':' not in token.value: return CellNode(token, self.model) # Then lets check if token is a number, in which case update assign float or int to token value elif token.subtype == Token.NUMBER: token.value = fast_real(token.value) return OperandNode(token, self.model) elif token.subtype == Token.LOGICAL: token.value = token.value == 'TRUE' return OperandNode(token, self.model) elif token.subtype == Token.TEXT: token.value = token.value.replace('"', '') return OperandNode(token, self.model) # Token must be Error - in which case do nothing else: logger.error( "Formula has error {}. Please fix error before uploading again" .format(token.value)) return OperandNode(token, self.model) elif token.is_funcopen: return FunctionNode(token, self.model) elif token.is_operator: return OperatorNode(token, self.model)
def convert_number_or_into_string(str_input, col): """Optimizes the Data Types. If VARCHAR type is defined in SQL for any value, it saves the similar value by adding quotation marks. However, if not, then by default it's a Number. In the later case, if the value is NULL, convert it to 0 else save the number.""" if (col == "CRD-revision-side ID") or (col == "CRD") or \ (col == "CRD revision") or (col == "Folder name") or (col == "Symmetric") or (col == "Side") or ( col == "User") or (col == "Origin") or (col == "Tire size") or \ (col == "CTC revision") or (col == "ERD-ARD") or (col == "Design manual") or (col == "Design code") or \ (col == "Rim flange protector") or (col == "Apex 3 layup") or (col == "Ply layu") or ( col == "Flipper layup") or \ (col == "Stiffener inside layup") or (col == "First chipper layup") or (col == "Toeguard layup") or ( col == "Sidewall layup") or \ (col == "Overlay layup") or (col == "Tread layup") or (col == "Bead configuration") or (col == "Type") or ( col == "Description") or \ (col == "Construction") or (col == "Material model") or (col == "DEW version") or ( col == "Rolling surface") or (col == "Cooldown") or \ (col == "Unit system") or (col == "Rim contour") or (col == "Test-component ID") or ( col == "Component") or (col == "Compound") or ( col == "Sample ID") or \ (col == "Cord code") or (col == "Cord serial") or ( col == "Treatment code") or \ (col == "Test-load-pressure ID") or (col == "TD") or ( col == "FP") or (col == "SR") or (col == "RR") or \ (col == "FM") or (col == "COSTGV") or (col == "COSBO") or (col == "Groove contact") or (col == "Sipe contact") \ or (col == "Test-load-pressure-component I") or (col == "Test-load-pressure-component I") or (col == "Test ID")\ or (col == "Test path"): return "'" + str_input + "'" if str_input == 'null' or str_input == 'None': return 0 elif str_input.isdigit() or (str_input.replace('.', '', 1).isdigit() and str_input.count('.') < 2) or ( str_input.replace('-', '', 1).replace( '.', '', 1).isdigit()): return fast_real(str_input) else: return "'" + str_input + "'"
def test_fast_real_returns_input_as_is_if_valid_and_key_is_given(x): fastnumbers.fast_real(x, key=len) == x fastnumbers.fast_real(str(x), key=len) == x
def test_fast_real_returns_raises_ValueError_if_raise_on_invalid_is_True_and_default_is_given(x): assume(not a_number(x)) with raises(ValueError): fastnumbers.fast_real(x, default=90, raise_on_invalid=True) fastnumbers.fast_real(x, 90, True)
def test_fast_real_returns_transformed_input_if_invalid_and_key_is_given(x): assume(not a_number(x)) fastnumbers.fast_real(x, key=len) == len(x)
def test_fast_real(): # 1. float number assert fastnumbers.fast_real(-367.3268) == -367.3268 assert fastnumbers.fast_real(-367.3268, raise_on_invalid=True) == -367.3268 # 2. signed float string assert fastnumbers.fast_real("+367.3268") == +367.3268 assert fastnumbers.fast_real("+367.3268", True) == +367.3268 # 3. float string with exponents assert fastnumbers.fast_real("-367.3268e207") == -367.3268e207 assert fastnumbers.fast_real("1.175494351e-3810000000") == 0.0 # 4. float string with padded whitespace assert fastnumbers.fast_real(" -367.04 ") == -367.04 # 5. int number assert fastnumbers.fast_real(499) == 499 # 6. signed int string assert fastnumbers.fast_real("-499") == -499 # 7. int string with padded whitespace assert fastnumbers.fast_real(" +3001 ") == 3001 # 8. long number assert fastnumbers.fast_real(35892482945872302493) == 35892482945872302493 # 9. long string if python_version_tuple()[0] == "2": assert fastnumbers.fast_real("35892482945872302493L") == 35892482945872302493 assert fastnumbers.fast_real("35892482945872302493l") == 35892482945872302493 assert fastnumbers.fast_real("35892482945872302493") == 35892482945872302493 # 10. return type assert isinstance(fastnumbers.fast_real(4029), int) assert isinstance(fastnumbers.fast_real(4029.0, coerce=False), float) assert isinstance(fastnumbers.fast_real(4029), int) assert isinstance(fastnumbers.fast_real(4029.0), int) assert isinstance(fastnumbers.fast_real(4029.5), float) assert isinstance(fastnumbers.fast_real("4029"), int) assert isinstance(fastnumbers.fast_real("4029.0"), int) assert isinstance(fastnumbers.fast_real("4029.0", coerce=False), float) # 11. TypeError for invalid input with raises(TypeError): fastnumbers.fast_real(["hey"]) # 12. Invalid input string assert fastnumbers.fast_real("not_a_number") == "not_a_number" with raises(ValueError): assert fastnumbers.fast_real("not_a_number", raise_on_invalid=True) # 13. Invalid input string with numbers assert fastnumbers.fast_real("26.8 lb") == "26.8 lb" with raises(ValueError): assert fastnumbers.fast_real("26.8 lb", None, True) # 14. Infinity assert fastnumbers.fast_real("inf") == float("inf") assert fastnumbers.fast_real("-iNFinity") == float("-inf") assert fastnumbers.fast_real("-iNFinity", inf=7608) == 7608 # 15. NaN assert math.isnan(fastnumbers.fast_real("nan")) assert math.isnan(fastnumbers.fast_real("-NaN")) assert fastnumbers.fast_real("-NaN", nan=0) == 0 # 16. Sign/'e'/'.' only assert fastnumbers.fast_real("+") == "+" assert fastnumbers.fast_real("-") == "-" assert fastnumbers.fast_real("e") == "e" assert fastnumbers.fast_real(".") == "." # 17. Default on invalid... 'raise_on_invalid' supersedes assert fastnumbers.fast_real("invalid", default=90) == 90 assert fastnumbers.fast_real("invalid", default=None) is None with raises(ValueError): assert fastnumbers.fast_real("invalid", 90, True) # 18. Unicode numbers assert fastnumbers.fast_real(u"⑦") == 7 assert fastnumbers.fast_real(u"⁸") == 8 assert fastnumbers.fast_real(u"⅔") == 2.0 / 3.0 assert fastnumbers.fast_real(u"Ⅴ") == 5 # 19. Key function assert fastnumbers.fast_real(76.8, key=len) == 76.8 assert fastnumbers.fast_real("76.8", key=len) == 76.8 assert fastnumbers.fast_real("invalid", key=len) == 7
import config import os.path from fastnumbers import fast_real import csv import numpy as np # Return parameter types and names. objectFeatures = {} firstFilePath = config.inputPath + "HT01/HT01A004_ftrs.tab" with open(firstFilePath, 'rb') as csvfile: reader = csv.reader(csvfile, delimiter='\t') header = reader.next() firstValues = [fast_real(v) for v in reader.next()] types = [np.int32 if isinstance(v, int) else np.float32 if isinstance(v, float) else np.object for v in firstValues] for i, name in enumerate(header): objectFeatures[name] = types[i] objectFeatures['plate'] = np.int32 objectFeatures['column'] = np.int32 objectFeatures['row'] = np.int32 del objectFeatures['class'] del objectFeatures['spot'] features = objectFeatures.keys() def openTextFile(feature, mode): return open(config.outputPath + feature + ".txt", mode)
def __call__(self, form, field): """Convert input values to NumPy-compatible numerical values""" if not field.data == "inf" and not field.data == "+inf" and not field.data == "-inf": num = fast_real(field.data) if not isinstance(num, (int, long, float)): raise ValidationError("Invalid value \"{0}\". Expected: -inf, inf, or a numerical value.".format(field.data))
sheet_GPGGA = workbook1.sheet_by_index(0) # extracting info. from sheet GPGGA GPGGA_lines = sheet_GPGGA.nrows -1 n_s_indicator = sheet_GPGGA.cell_value(1, 4) if n_s_indicator == "N": n_s_indicator = "(North)" elif n_s_indicator == "S": n_s_indicator = "(South)" e_w_indicator = sheet_GPGGA.cell_value(1, 6) if e_w_indicator == "E": e_w_indicator = "(East)" elif e_w_indicator == "W": e_w_indicator = "(West)" for i in range(1, sheet_GPGGA.nrows): fix_indicator.append(fast_real(sheet_GPGGA.cell_value(i, 7))) local = sheet_GPGGA.cell_value(i, 0).split("_")[1] local = fast_real(local.split("-")[0]+local.split("-")[1]+local.split("-")[2]) local_time.append(local) #UTC+8 hours # latitude if sheet_GPGGA.cell_value(i, 3) == "": latitude.append(-99) else: latitude.append(fast_real((sheet_GPGGA.cell_value(i, 3)))) # longitude if sheet_GPGGA.cell_value(i, 5) == "": longitude.append(-99) else: longitude.append(str(fast_real(sheet_GPGGA.cell_value(i, 5)))) # no_of_satelites_used if sheet_GPGGA.cell_value(i, 8) == "":