def validate_eans(eans): if isinstance(eans, str): eansSplit = [x.strip() for x in eans.split(',')] else: eansSplit = [str(eans)] valid_eans = [] for ean in eansSplit: if len(ean) in (8, 13): try: ean_digits = [int(x) for x in ean] except ValueError: logging.warning("'{}' has non-numeric characters".format(ean)) continue for i in range(len(ean_digits[:-1])): if len(ean) == 8: if i % 2 == 0: ean_digits[i] = ean_digits[i] * 3 else: if i % 2 != 0: ean_digits[i] = ean_digits[i] * 3 round_nearest_ten = int( roundup(float(sum(ean_digits[:-1])) / 10) * 10) if round_nearest_ten - sum(ean_digits[:-1]) == ean_digits[-1]: valid_eans.append('<EAN>' + ean + '</EAN>') else: logging.warning('EAN: Invalid Checkdigit - {}'.format(ean)) elif len(ean) in range(9, 13): zeroes = '' add = 13 - len(ean) for i in range(add): zeroes += '0' ean = zeroes + ean try: ean_digits = [int(x) for x in ean] for i in range(len(ean_digits[:-1])): if i % 2 != 0: ean_digits[i] = ean_digits[i] * 3 round_nearest_ten = int( roundup(float(sum(ean_digits[:-1])) / 10) * 10) if round_nearest_ten - sum(ean_digits[:-1]) == ean_digits[-1]: valid_eans.append('<EAN>' + ean + '</EAN>') else: logging.warning('EAN: Invalid Length - {}'.format(ean)) except ValueError: ean = ean[len(zeroes):] logging.warning( ean, 'EAN: Invalid Length - Too Many Zeroes? - {}'.format(ean)) elif ean != '': logging.warning('EAN: Invalid Length') if len(valid_eans) > 0: all_eans = "".join(set(valid_eans)) return all_eans else: return ''
def validate_upcs(product_id, upcs, broken_gtins, broken_gtin_count): valid_upcs = [] for upc in upcs: if len(upc) in (6, 12): upc_digits = [int(x) for x in upc] for i in range(len(upc_digits[:-1])): if i % 2 == 0: upc_digits[i] = upc_digits[i] * 3 round_nearest_ten = int( roundup(float(sum(upc_digits[:-1])) / 10) * 10) if round_nearest_ten - sum(upc_digits[:-1]) == upc_digits[-1]: valid_upcs.append(upc) else: broken_gtins.writerow( [product_id, upc, 'UPC: Invalid Checkdigit']) broken_gtin_count += 1 elif len(upc) in range(7, 12): zeroes = '' add = 12 - len(upc) for i in range(add): zeroes += '0' upc = zeroes + upc try: upc_digits = [int(x) for x in upc] for i in range(len(upc_digits[:-1])): if i % 2 == 0: upc_digits[i] = upc_digits[i] * 3 round_nearest_ten = int( roundup(float(sum(upc_digits[:-1])) / 10) * 10) if round_nearest_ten - sum(upc_digits[:-1]) == upc_digits[-1]: valid_upcs.append(upc) else: upc = upc[len(zeroes):] broken_gtins.writerow( [product_id, upc, 'UPC: Invalid Length']) broken_gtin_count += 1 except ValueError: upc = upc[len(zeroes):] broken_gtins.writerow([product_id, upc, 'UPC: Invalid Length']) broken_gtin_count += 1 else: broken_gtins.writerow([product_id, upc, 'UPC: Invalid Length']) broken_gtin_count += 1 if len(valid_upcs) >= 1: x_upcs = objectify.SubElement(x_Product, "UPCs") # product_string_array.append(' ' + '<UPCs>' + '\n') for upc in valid_upcs: objectify.SubElement(x_upcs, "UPC") x_upcs.UPC = upc # product_string_array.append(' ' + '<UPC>' + upc + '</UPC>' + '\n') # product_string_array.append(' ' + '</UPCs>' + '\n') print(broken_gtin_count) return broken_gtin_count
def validate_eans(product_id, eans, broken_gtins): valid_eans = [] for ean in eans: if len(ean) in (8, 13): # if ean length is 8 or 13, validate ean_digits = [int(x) for x in ean] for i in range(len(ean_digits[:-1])): if len(ean) == 8: # xxx - what is going on here? if i % 2 == 0: ean_digits[i] = ean_digits[i] * 3 else: if i % 2 != 0: ean_digits[i] = ean_digits[i] * 3 round_nearest_ten = int( roundup(float(sum(ean_digits[:-1])) / 10) * 10) if round_nearest_ten - sum(ean_digits[:-1]) == ean_digits[-1]: valid_eans.append(ean) else: broken_gtins.writerow( [product_id, ean, 'EAN: Invalid Checkdigit']) # if ean length between 9 - 13, add leading zeros elif len(ean) in range(9, 13): zeroes = '' add = 13 - len(ean) for i in range(add): zeroes += '0' ean = zeroes + ean try: ean_digits = [int(x) for x in ean] for i in range(len(ean_digits[:-1])): if i % 2 != 0: ean_digits[i] = ean_digits[i] * 3 round_nearest_ten = int( roundup(float(sum(ean_digits[:-1])) / 10) * 10) if round_nearest_ten - sum(ean_digits[:-1]) == ean_digits[-1]: valid_eans.append(ean) else: ean = ean[len(zeroes):] broken_gtins.writerow( [product_id, ean, 'EAN: Invalid Length']) except ValueError: print(ean, 'too many zeroes?') ean = ean[len(zeroes):] broken_gtins.writerow([product_id, ean, 'EAN: Invalid Length']) else: broken_gtins.writerow([product_id, ean, 'EAN: Invalid Length']) if len(valid_eans) >= 1: x_eans = objectify.SubElement(x_Product, 'EANs') # product_string_array.append(' ' + '<EANs>' + '\n') for index, ean in enumerate(valid_eans): x_ean = objectify.SubElement(x_eans, 'EAN') x_ean[index] = ean
def validate_upcs(upcs): if isinstance(upcs, int): upcsSplit = [str(upcs)] else: upcsSplit = [x.strip() for x in upcs.split(',')] valid_upcs = [] for upc in upcsSplit: if len(upc) in (6, 12): try: upc_digits = [int(x) for x in upc] except ValueError: logging.warning("'{}' has non-numeric characters".format(upc)) continue for i in range(len(upc_digits[:-1])): if i % 2 == 0: upc_digits[i] = upc_digits[i] * 3 round_nearest_ten = int( roundup(float(sum(upc_digits[:-1])) / 10) * 10) if round_nearest_ten - sum(upc_digits[:-1]) == upc_digits[-1]: valid_upcs.append('<UPC>' + upc + '</UPC>') else: logging.warning('UPC: Invalid Checkdigit - {}'.format(upc)) elif len(upc) in range(7, 12): zeroes = '' add = 12 - len(upc) for i in range(add): zeroes += '0' upc = zeroes + upc try: upc_digits = [int(x) for x in upc] for i in range(len(upc_digits[:-1])): if i % 2 == 0: upc_digits[i] = upc_digits[i] * 3 round_nearest_ten = int( roundup(float(sum(upc_digits[:-1])) / 10) * 10) if round_nearest_ten - sum(upc_digits[:-1]) == upc_digits[-1]: valid_upcs.append('<UPC>' + upc + '</UPC>') else: upc = upc[len(zeroes):] logging.warning('UPC: Invalid Length - {}'.format(upc)) except ValueError: upc = upc[len(zeroes):] logging.warning('UPC: Invalid Length - {}'.format(upc)) if len(valid_upcs) > 0: all_upcs = "".join(set(valid_upcs)) return all_upcs else: return ''
def train_ann(training_set): '''Train the anni ''' #Constants total_datapoints = len(training_set) num_inputs = len(training_set[0][0]) num_outputs = len(training_set[0][1]) learning_rate = 0.01 momentum = 0.95 desired_error = 0.0001 iterations_between_reports = 0 maximum_iterations = 10000 num_hiddens1 = roundup(num_inputs * 0.95) num_hiddens2 = roundup(num_inputs * 0.85) #Gotta make a training data file data_file = "../data/validation/training.data" f = open(data_file, 'w') f.write("%s %s %s\n" % (total_datapoints, num_inputs, num_outputs)) for datapoint in training_set: inp = ' '.join(str(x) for x in datapoint[0]) targ = ' '.join(str(x) for x in datapoint[1]) f.write("%s\n" % inp) f.write("%s\n" % targ) f.close() mse = 1 while mse > desired_error: ann = lf.neural_net() ann.create_standard_array( [num_inputs, num_hiddens1, num_hiddens2, num_outputs]) ann.set_learning_rate(learning_rate) ann.set_learning_momentum(momentum) ann.set_activation_function_hidden( lf.SIGMOID_SYMMETRIC ) #This is to ensure negative and positive influences can be detected ann.set_activation_function_output( lf.SIGMOID) #Ensure output isnt negative ann.set_bit_fail_limit(0.01) #ann.set_train_error_function(lf.ERRORFUNC_TANH) # Train ann.train_on_file(data_file, maximum_iterations, iterations_between_reports, desired_error) mse = ann.get_MSE() return ann
def struct(self, address, fmt): size = calcsize(fmt) result = [] for _ in range(roundup(size/PyMemory.BUFFER_SIZE)): self.buffer_load(address, size) result += unpack(fmt, self.buffer[:size]) return result
def post(self): """Mark a scooter as not reserved and return success or fail status boolean. Also charge customer and update scooter location.""" dataResponse = defaultResponse q = """ UPDATE scooters SET is_reserved = false WHERE id={id.int} AND is_reserved = true RETURNING true; """ payload, newQuery = Validator.validatePayload(q, request) if payload: # End reservation success code dataResponse = getQueryResponse(payload, newQuery, queryType='update') if dataResponse[0]: q = """SELECT ST_Distance(location, ST_MakePoint({endlng.float}, {endlat.float})) FROM scooters WHERE id = {id.int}""" payload, newQuery = Validator.validatePayload(q, request) if payload: # Charge customer and update scooter location. distanceTraveled = getQueryResponse(payload, newQuery, queryType='query')[0] distanceTraveled = distanceTraveled[0] while type(distanceTraveled) is tuple: distanceTraveled = distanceTraveled[0] distanceTraveled = roundup( distanceTraveled ) if distanceTraveled > 0 else 1 # Min distance traveled is always 1. pricePerMeter = 1.0 # Ideally, this value is should not be hard coded fareCost = pricePerMeter * distanceTraveled q = """UPDATE users SET (last_fare, fares, scooter_ids, distances_traveled) = ({fareCost}::real, array_append(fares, {fareCost}::real), array_append(scooter_ids, {id.int}::bigint), array_append(distances_traveled, {distanceTraveled}::bigint)) WHERE id={userid.int}; UPDATE scooters SET (lon, lat, distances_traveled, rider_ids, location) = ({endlng.float}, {endlat.float}, array_append(distances_traveled, {distanceTraveled}::bigint), array_append(rider_ids, {userid.int}::bigint), ST_POINT({endlng.float}, {endlat.float})) WHERE id = {id.int}; """ q = q.replace('{fareCost}', str(fareCost)).replace( '{distanceTraveled}', str(distanceTraveled)) # Partial format subtitution payload, newQuery = Validator.validatePayload(q, request) if payload: _ = getQueryResponse(payload, newQuery, queryType='update') return dataResponse
def Listify(crypt,key): keyl=list(key) length=len(key) #gets numbers of each letter keyln=[] for index in range(len(keyl)): keyln.append(ord(keyl[index])) m = [[] for x in range(length)] #puts crypt into 2D array i = 0 for row in range(int(roundup(len(unshuffledString)/length))+1): for col in range(length): try: m[col].append(crypt[i]) except: continue i+=1 return m
def Listify(crypt, key): keyl = list(key) length = len(key) #gets numbers of each letter keyln = [] for index in range(len(keyl)): keyln.append(ord(keyl[index])) m = [[] for x in range(length)] #puts crypt into 2D array i = 0 for row in range(int(roundup(len(unshuffledString) / length)) + 1): for col in range(length): try: m[col].append(crypt[i]) except: continue i += 1 return m
saved_net_file = "../data/networks/order_all_averaged_fixed.net" ## Grab the number of inputs, outputs (first line of .data in format #datapoints, inputs, outputs) f = open(data_file, 'r') info = (f.readline()).split(' ') num_inputs = int(info[1]) num_outputs = int(info[2]) f.close() ## Settings learning_rate = 0.1 momentum = 0.95 desired_error = 0.00001 iterations_between_reports = 100 maximum_iterations = 10000 ## Determine number of hidden nodes num_hiddens1 = roundup(num_inputs * 0.95) num_hiddens2 = roundup(num_inputs * 0.85) # Create network ann = lf.neural_net() ann.create_standard_array( [num_inputs, num_hiddens1, num_hiddens2, num_outputs]) ann.set_learning_rate(learning_rate) ann.set_learning_momentum(momentum) ann.set_activation_function_hidden( lf.SIGMOID_SYMMETRIC ) #This is to ensure negative and positive influences can be detected ann.set_activation_function_output(lf.SIGMOID) #Ensure output isnt negative ann.set_bit_fail_limit(0.01) # Train
# print(sin(6.28)) # print(sin(pi*2)) # four = sqrt(4) # print(four) #imported all math functions from Python module #in particular, we noted we imported sin. No need for "math.sin."" Just ".sin" #however, we must note "math." for the rest of the math functions #we can rename Python function; e.g. give "ceil" a new name "roundup" import math from math import sin, pi, ceil as roundup num = 5.6 print(math.ceil(num)) print(roundup(num)) #renamed math.ceil as roundup or math.ceil=roundup print(math.floor(num)) print(math.factorial(4)) print(math.pow(2, 4)) print(2**4) print(math.exp(2)) print(math.log(16, 2)) print(sin(6.28)) #no need for "math." for sin because we noted from import print(sin(pi * 2)) #no need for "math." for pi because we noted from import four = math.sqrt(4) print(four) print(math.degrees(pi * 2)) #a circle 360.0 degrees