def test_fast_float_with_range_of_exponents_correctly_parses(): for x in range(-300, 300): val = '1.0E{0:d}'.format(x) assert fastnumbers.fast_float(val) == float(val) for x in range(-300, 300): val = '1.0000000000E{0:d}'.format(x) assert fastnumbers.fast_float(val) == float(val)
def calculate(new, old): new = fast_float(new) old = fast_float(old) # Treat anything below 0 upvotes as 0 upvotes if old < 0: old = 0 if new < 0: new = 0 # Compute gain delta = new - old # Treat negative gain as no gain if delta < 0: delta = 0 # Compute the maximum of the sigmoid sig_max = sigmoid_max(old) # Compute the midpoint of the sigmoid sig_mp = sigmoid_midpoint(old) # Compute the steepness of the sigmoid sig_stp = sigmoid_steepness(old) # Calculate return factor = sigmoid(delta, sig_max, sig_mp, sig_stp) return factor
def test_fast_float_given_float_string_returns_float(x): assume(not math.isnan(x)) assume(not x.is_integer()) y = repr(x) assert fastnumbers.fast_float(y) == x assert fastnumbers.fast_float(y, None, True) == x assert fastnumbers.fast_float(y, raise_on_invalid=True) == x assert isinstance(fastnumbers.fast_float(y), float)
def readfile(filedir): with open(filedir, "r") as f: prices = [] hps = [] lines1 = [] lines2 = [] for line in f: line = line.split(',') hp = fn.fast_float(line[21], default=0) price = fn.fast_float(line[25], default=0) if fn.isfloat(hp): if hp > 20 and hp < 300: hps.append(hp) else: lines1.append(line) else: lines1.append(line) if fn.isfloat(price): if price > 1 and price < 60: prices.append(price) else: lines2.append(line) else: lines2.append(line) avghp = sum(hps) / len(hps) avgprice = sum(prices) / len(prices) for line in lines1: hp = avghp price = fn.fast_float(line[21]) hps.append(hp) prices.append(price) for line in lines2: hp = fn.fast_float(line[25]) price = avgprice hps.append(hp) prices.append(price) sort = zip(prices, hps) sort.sort() prices = [x for x, y in sort] hps = [y for x, y in sort] return prices , hps
def calculate(new, old): # Multiplier is detemined by a power function of the relative change in upvotes # since the investment was made. # Functional form: y = x^m ; # y = multiplier, # x = relative growth: (change in upvotes) / (upvotes at time of investment), # m = scale factor: allow curtailing high-growth post returns to make the playing field a bit fairer new = fast_float(new) old = fast_float(old) # Scale factor for multiplier scale_factor = 1 / fast_float(3) # Calculate relative change if old != 0: rel_change = (new - old) / abs(old) # If starting upvotes was zero, avoid dividing by zero else: rel_change = new mult = pow((rel_change + 1), scale_factor) # Investment must grow by more than a threshold amount to win. Decide if # investment was successful and whether you get anything back at all. win_threshold = 1.2 if mult > win_threshold: investment_success = True return_money = True elif mult > 1: investment_success = False return_money = True else: investment_success = False return_money = False # Investor gains money only if investment was successful. If mult # was below win_threshold but above 1 return factor is ratio of # difference between mult and 1 and difference between win_threshold and 1. # Otherwise, if mult was 1 or lower, get back nothing. if investment_success: factor = mult elif return_money: factor = (mult - 1) / (win_threshold - 1) else: factor = 0 return factor
def getcoordlist(file): with open(file, 'r') as coord: coordlist = list(csv.reader(coord, delimiter=',')) # Delete column for sublist in coordlist: del sublist[0] del sublist[3] #convert to float an then numpy array for i in range(len(coordlist)): coordlist[i][0] = fast_float(coordlist[i][0]) coordlist[i][1] = fast_float(coordlist[i][1]) coordlist[i][2] = fast_float(coordlist[i][2]) # print(coordlist[i][0],coordlist[i][1],coordlist[i][2]) # print(type(coordlist[i][0]),type(coordlist[i][1]),type(coordlist[i][2])) coordlist = np.array(coordlist) return coordlist
def invalid(self) -> Union[str, int, float, None]: """Property invalid value""" if self.col_invalid is None: return None if self.data_type in ( DataType.CHAR, DataType.UCHAR, DataType.SHORT, DataType.USHORT, DataType.INT, DataType.UINT, ): try: return fast_int(self.col_invalid, raise_on_invalid=True) except ValueError: return None if self.data_type == DataType.FLOAT: try: return fast_float(self.col_invalid, raise_on_invalid=True) except ValueError: return None return str(self.col_invalid)
def ffloat(string): if string is None: return np.nan if type(string)==float or type(string)==int or type(string)==np.int64 or type(string)==np.float64: return string string = re.sub('[^0-9\.]','',string.split(" ")[0]) return fast_float(string,default=np.nan)
def _sortable_fastnumbers(val): if val is None: return sortable_none return fastnumbers.fast_float(val, default=SortableSTR(val), nan=sortable_nan)
def add_import(update, context): message = update.message.text amount = round(fast_float(message, default=-1), 2) if amount == -1: context.bot.send_message(chat_id=update.effective_chat.id, text="⚠ The amount is not correct! " "Only numbers with 2 decimal are allowed\n\n" f"/{COMMANDS['cancel'].command}") return IMPORT context.bot.sendChatAction(chat_id=update.effective_chat.id, action=telegram.ChatAction.TYPING) logger.info('User Data: %s %s', context.user_data, amount) if context.user_data['element'] == 0: updated = add_new_expense(context.user_data['date'], context.user_data['name'], amount) elif context.user_data['element'] == 1: updated = add_new_earning(context.user_data['date'], context.user_data['name'], amount) else: updated = 0 if updated > 0: context.bot.send_message( chat_id=update.effective_chat.id, text=f"✅ New element added!\n\n" f"📆 Date: {context.user_data['date'].strftime('%d/%m/%Y')}\n" f"✍ Description: {context.user_data['name']}\n" f"💰 Amount: {CURRENCY}{amount}\n") else: context.bot.send_message(chat_id=update.effective_chat.id, text=f"⚠ Element not added, retry!") return ConversationHandler.END
def _submitHandler(self): time = fast_float(self.time.get(), default="invalid") if time == "invalid": return self.result = time self._quit()
def ffloat(string): if string is None: return np.nan if type(string)==float or type(string)==np.float64: return string if type(string)==int or type(string)==np.int64: return string return fast_float(string.split(" ")[0].replace(',','').replace('%',''), default=np.nan)
def round_only(nearest=0): # Make copy of original speed values and round new_speeds = [round(x, nearest) for x in original_speeds] # Save new values to file for j in range(len(original)): cur.execute('UPDATE speeds SET speed = %f WHERE time = %d' % (new_speeds[j], original[j][1])) conn.commit() # Run Elastic Pathing algorithm result = fast_float(os.popen('ruby elastic_pathing.rb ./%s ./map.sq3' % file).read().split(' ')[0], default=-1) restore_file() return (int(10**(-nearest)), threshold(new_speeds, thresh=DEF_THRESH), 100*result/dist)
def calculate(new, old, net_worth=0): new = fast_float(new) old = fast_float(old) net_worth = fast_float(net_worth) # Treat anything below 0 upvotes as 0 upvotes if old < 0: old = 0 if new < 0: new = 0 # Compute gain delta = new - old # Treat negative gain as no gain if delta < 0: delta = 0 # Compute the maximum of the sigmoid sig_max = sigmoid_max(old) # Compute the midpoint of the sigmoid sig_mp = sigmoid_midpoint(old) # Compute the steepness of the sigmoid sig_stp = sigmoid_steepness(old) # Calculate return factor = sigmoid(delta, sig_max, sig_mp, sig_stp) if net_worth: # Normalize between -1 and 1 factor = factor - 1 # Adjust based on net worth factor = factor * net_worth_coefficient(net_worth) # Return investment amount multiplier (change + 1) factor = factor + 1 return factor
def extract_ects(self, sub_title): """ Extract ECTS from subtitle. """ result = -1 ects = re.search(r'(\d+\W\d+)EC', sub_title) if ects != None: if ects.group(1) != None: tmp = fn.fast_float(ects.group(1), default=-1) if tmp > -1: result = tmp return result
def human2bytes(size: Union[int, float, str]) -> float: size = str(size) result = fast_float(size.rstrip("%s%s" % (SIZE_UNITS, SIZE_UNITS.lower()))) if size.lstrip(f"0123456789.").upper() in ("", "B", "O"): return result for unit in SIZE_UNITS.lstrip("B"): result *= 1024 if size.rstrip("iIoObB")[-1].upper() == unit: break return result
def score(self, *args): """ Score setter. It verifies that the score is a valid value (None or float). :param args: List of arguments. Only the first is used. :type args: list | None | float """ score = args[0] if score == ".": score = None elif score is not None: score = fastnumbers.fast_float(args[0]) if score is not None and not isinstance(score, float): raise TypeError(score) self.__score = score
def get_peer_comparison_score(pc): mrk = [] prf = [] mrk_to_prf = [] mrk_val = fast_float(pc[5].replace(',', '')) prf_val = fast_float(pc[7].replace(',', '')) for i in range(8, len(pc), 8): mrk.append(fast_float(pc[i + 5].replace(',', ''))) prf.append(fast_float(pc[i + 7].replace(',', ''))) for i in prf: if (i < 0): return 0.8 for i in mrk: if (i < mrk_val): return 0.8 for i, j in zip(mrk, prf): mrk_to_prf.append((i / mrk_val) / (j / prf_val)) return np.average(mrk_to_prf)
def calculate(new, old, net_worth=0, top_networth=0): new = fast_float(new) old = fast_float(old) net_worth = fast_float(net_worth) # Treat anything below 0 upvotes as 0 upvotes if old < 0: old = 0 if new < 0: new = 0 if net_worth < 0: net_worth = 1 # Compute gain delta = new - old # Treat negative gain as no gain if delta < 0: delta = 0 # Compute the maximum of the sigmoid sig_max = sigmoid_max(old) # Compute the midpoint of the sigmoid sig_mp = sigmoid_midpoint(old) # Compute the steepness of the sigmoid sig_stp = sigmoid_steepness(old) # Calculate return factor = sigmoid(delta, sig_max, sig_mp, sig_stp) factor = adjust(factor, net_worth, top_networth) factor = max(0, factor) return factor
def bytes2human(size: Union[int, float], prefix: str = "", suffix: str = "") -> str: size = fast_float(size) # Prevent proxied size problems with round() unit = "" for unit in SIZE_UNITS: if size < 1024: break size /= 1024 size = fast_int(size) if unit in "BK" else \ round(size, 1) if unit == "M" else \ round(size, 2) return f"{size}{prefix}{unit}{suffix}"
def diff(min=0, max=10, iterations=30): average = 0 error = 0 no_results = 0 for i in range(iterations): new_speeds = [x + random.uniform(min, max) for x in original_speeds] for j in range(len(original)): cur.execute('UPDATE speeds SET speed = %f WHERE time = %d' % (new_speeds[j], original[j][1])) conn.commit() result = fast_float(os.popen('ruby elastic_pathing.rb ./%s ./map.sq3' % file).read().split(' ')[0], default=-1) if result >= 0: average += result error += threshold(new_speeds, thresh=DEF_THRESH) else: no_results += 1 average = 100 - (100*float(average)/(dist*(iterations-no_results))) error /= float(iterations-no_results) restore_file() return (error, average)
def get_score(url): r, _, pl, pc, fi, _ = get_everything(url) p1 = get_point_value(pl[1:], avg=True) p2 = get_point_value(fi[1:], avg=False) p3 = get_point_value(r[1:], avg=True) p4 = get_peer_comparison_score(pc) p5 = fast_float(fi[1].replace(',', '')) / 10 p1 = sigmoid(p1) p2 = sigmoid(p2) p3 = sigmoid(p3) p4 = sigmoid(p4) p5 = sigmoid(p5) score = (p1 + p2 + p3 + p4 + p5) * 20 return int(round(score, 0))
def parse_raw_data( raw_data: List[Tuple[str, str]]) -> List[Tuple[time, measurement]]: """Parses the raw csv data. 'time' is left unchanged. 'measurement' is converted to float, rounded to 4 decimal places. Invadid floats default to 0.0 Parameters: - raw_data: List[Tuple[str,str]] The raw csv data. """ # using fastnumbers.fast_float to parse floats from string # on failure assign 0.0 to_float = lambda x: fast_float(x, default=0.0, nan=0.0, inf=0.0) to_rounded_float = lambda x: round(to_float(x), 4) return [(x[0], to_rounded_float(x[1])) for x in raw_data]
def check_data_type(dict_key, cell_value, data_types_dict): valid_type = type(data_types_dict[dict_key][0]) if pd.isnull(cell_value) and data_types_dict[dict_key][1]: return "Mandatory field left empty." elif pd.isnull(cell_value) and not data_types_dict[dict_key][1]: return "Empty: potential error" elif data_types_dict[dict_key][0] == "datetime": try: parse(cell_value) except ValueError: return "Wrong data type - \"{}\" should be {} NOT {}".format( cell_value, datetime, type(cell_value)) elif valid_type is not type(cell_value) and\ valid_type is not type(fast_float(cell_value)) and\ (valid_type is int) and not (isintlike(cell_value)) and\ (valid_type is int) and not str(cell_value).isdigit(): return "Wrong data type - \"{}\" should be {} NOT {}".format( cell_value, type(data_types_dict[dict_key][0]), type(cell_value)) return "Success"
def age2date(age: str) -> pend.DateTime: try: return pend.parse(age) # If this is already a normal date except pend.parsing.exceptions.ParserError: pass user_unit = age.lstrip("0123456789.") value = abs(fast_float(age.replace(user_unit, ""))) found_unit = None for units, shift_unit in AGE_UNIT_TO_SUBTRACT_ARG.items(): for unit in units: if user_unit in (unit, f"{unit}s"): found_unit = shift_unit if not found_unit: raise ValueError(f"Invalid age unit: {user_unit!r}") return pend.now().subtract(**{found_unit: value})
def shuffle_round_diff(skip_length=40, iterations=10, will_round=False, nearest=0, will_diff=False, diff_min=0, diff_max=10): skips = [1] for i in range(1, len(original)): if i % skip_length == 0: skips.append(i) skips.append(len(original)) errors = [0.0] * len(skips) # Get errors for shuffling with different intervals for k in range(len(skips)): # Repeeat to minimize randomness error = 0 no_results = 0 for i in range(iterations): # Set shuffle offset offset = 0 # Make copy of original speed values # and round/diff if needed if will_round and will_diff: print('Not Implemented') elif will_round: new_speeds = [round(x/nearest)*nearest for x in original_speeds] elif will_diff: new_speeds = [x + random.uniform(diff_min, diff_max) for x in original_speeds] else: new_speeds = original_speeds[:] # Shuffle values and save if skips[k] > 1: while offset < skips[-1]: end = offset + skips[k] if end > skips[-1]: end = skips[-1] shuffle_speeds = new_speeds[offset:end] shuffle(shuffle_speeds) new_speeds[offset:end] = shuffle_speeds offset += skips[k] for j in range(skips[-1]): cur.execute('UPDATE speeds SET speed = %f WHERE time = %d'\ % (new_speeds[j], original[j][1])) conn.commit() # Run Elastic Pathing algorithm result = fast_float(os.popen('ruby elastic_pathing.rb ./%s ./map.sq3' % file).read().split(' ')[0], default=-1) if result >= 0: error += result else: no_results += 1 threshold(new_speeds, thresh=DEF_THRESH) errors[k] = 100*float(error)/float(iterations-no_results)/dist restore_file() return (threshold(new_speeds, thresh=DEF_THRESH), [(skips[i], 100-errors[i]) for i in range(len(errors))])
def new_recurrent_insert(update, context): message = update.message.text amount = round(fast_float(message, default=-1), 2) if amount == -1: context.bot.send_message(chat_id=update.effective_chat.id, text="⚠ The amount is not correct! " "Only numbers with 2 decimal are allowed\n\n" f"/{COMMANDS['cancel'].command}") return RECURRENT_IMPORT context.bot.sendChatAction(chat_id=update.effective_chat.id, action=telegram.ChatAction.TYPING) add_recurrent(context.user_data['type'], context.user_data['day'], context.user_data['name'], amount) context.bot.send_message( chat_id=update.effective_chat.id, text=f"✅ New recurrent element added!\n\n" f"📆 Date: {context.user_data['day']} of every month\n" f"✍ Description: {context.user_data['name']}\n" f"💰 Amount: {CURRENCY}{amount}\n") return ConversationHandler.END
def hist(columns, buckets=10): return self.cols.hist(columns, fast_float(self.cols.min(columns)), fast_float(self.cols.max(columns)), buckets)
def test_fast_float_returns_transformed_input_if_invalid_and_key_is_given(x): assume(not a_number(x)) fastnumbers.fast_float(x, key=len) == len(x)
def test_fast_float_returns_input_as_is_if_valid_and_key_is_given(x): fastnumbers.fast_float(x, key=len) == x fastnumbers.fast_float(str(x), key=len) == x
def test_fast_float(): # 1. float number assert fastnumbers.fast_float(-367.3268) == -367.3268 assert fastnumbers.fast_float(-367.3268, raise_on_invalid=True) == -367.3268 # 2. signed float string assert fastnumbers.fast_float("+367.3268") == +367.3268 assert fastnumbers.fast_float("+367.3268", True) == +367.3268 # 3. float string with exponents assert fastnumbers.fast_float("-367.3268e27") == -367.3268e27 assert fastnumbers.fast_float("-367.3268E27") == -367.3268e27 assert fastnumbers.fast_float("-367.3268e207") == -367.3268e207 assert fastnumbers.fast_float("1.175494351E-3810000000") == 0.0 # 4. float string with padded whitespace assert fastnumbers.fast_float(" -367.04 ") == -367.04 # 5. int number assert fastnumbers.fast_float(499) == 499.0 # 6. signed int string assert fastnumbers.fast_float("-499") == -499.0 # 7. int string with padded whitespace assert fastnumbers.fast_float(" +3001 ") == 3001 # 8. long number assert fastnumbers.fast_float(35892482945872302493) == 35892482945872302493.0 # 9. long string if python_version_tuple()[0] == "2": assert fastnumbers.fast_float("35892482945872302493L") == 35892482945872302493.0 assert fastnumbers.fast_float("35892482945872302493l") == 35892482945872302493.0 assert fastnumbers.fast_float("35892482945872302493") == 35892482945872302493.0 # 10. return type assert isinstance(fastnumbers.fast_float(4029), float) assert isinstance(fastnumbers.fast_float("4029"), float) # 11. TypeError for invalid input with raises(TypeError): fastnumbers.fast_float(["hey"]) # 12. Invalid input string assert fastnumbers.fast_float("not_a_number") == "not_a_number" with raises(ValueError): assert fastnumbers.fast_float("not_a_number", raise_on_invalid=True) # 13. Invalid input string with numbers assert fastnumbers.fast_float("26.8 lb") == "26.8 lb" with raises(ValueError): assert fastnumbers.fast_float("26.8 lb", None, True) # 14. Infinity assert fastnumbers.fast_float("inf") == float("inf") assert fastnumbers.fast_float("-iNFinity") == float("-inf") assert fastnumbers.fast_float("-iNFinity", inf=523) == 523 # 15. NaN assert math.isnan(fastnumbers.fast_float("nAn")) assert math.isnan(fastnumbers.fast_float("-NaN")) assert fastnumbers.fast_float("-NaN", nan=0) == 0 # 16. Sign/'e'/'.' only assert fastnumbers.fast_float("+") == "+" assert fastnumbers.fast_float("-") == "-" assert fastnumbers.fast_float("e") == "e" assert fastnumbers.fast_float(".") == "." # 17. Default on invalid... 'raise_on_invalid' supersedes assert fastnumbers.fast_float("invalid", default=90) == 90 assert fastnumbers.fast_float("invalid", default=None) is None with raises(ValueError): assert fastnumbers.fast_float("invalid", 90, True) # 18. Unicode numbers assert fastnumbers.fast_float(u"⑦") == 7.0 assert fastnumbers.fast_float(u"⁸") == 8.0 assert fastnumbers.fast_float(u"⅔") == 2.0 / 3.0 assert fastnumbers.fast_float(u"Ⅴ") == 5.0 # 19. Key function assert fastnumbers.fast_float(76.8, key=len) == 76.8 assert fastnumbers.fast_float("76.8", key=len) == 76.8 assert fastnumbers.fast_float("invalid", key=len) == 7