def levenshtein_distance(lhs, rhs): """ :param lhs: The object to compare :param rhs: The object to compare with :return: An int >= 0 representing the Levenshtein Distance. :raise: ValueError Calculates the Levenshtein distance between two strings as described in more detail `here <https://secure.wikimedia .org/wikipedia/en/wiki/Levenshtein_distance>`__ . """ if not lhs or not rhs: raise ValueError("Input cannot be empty") if type(lhs) != type(rhs): raise ValueError("Input should be of the same type") m = Matrix(len(lhs), len(rhs)) for i in range(len(lhs)): m[i, 0] = i for i in range(len(rhs)): m[0, i] = i for j in range(1, len(rhs)): for i in range(1, len(lhs)): if lhs[i] == rhs[j]: m[i, j] = m[i - 1, j - 1] else: m[i, j] = min(m[i - 1, j] + 1, m[i, j - 1] + 1, m[i - 1, j - 1] + 1) return m[len(lhs) - 1, len(rhs) - 1]
def move(self, stage1, stage2): """ Moves the stages with the specified values. Since GSC-02 is a half-step stepping driver, 1 pulse corresponds to "half-step movement" in the stage catalogues. """ if not (-16777214 <= stage1 <= 16777214): raise ValueError('stage1 must be between -16777214 and 16777214.') if not (-16777214 <= stage2 <= 16777214): raise ValueError('stage2 must be between -16777214 and 16777214.') command = b'M:W' if stage1 >= 0: command += b'+P%d' % stage1 else: command += b'-P%d' % -stage1 if stage2 >= 0: command += b'+P%d' % stage2 else: command += b'-P%d' % -stage2 self.write(command) self.go()
def ip_set_from_address_range(start, end): try: start_ip_address = ip_address_from_address(start) end_ip_address = ip_address_from_address(end) except (NotSupported, ValueError) as e: raise ValueError( ('invalid IP range: %(start)s-%(end)s (%(message)s)') % { 'start': start, 'end': end, 'message': e.message}) except netaddr.AddrFormatError as e: raise ValueError( ("invalid IP range: '%(start)s-%(end)s' (%(message)s)") % { 'start': start, 'end': end, 'message': e.message}) if start_ip_address > end_ip_address: raise ValueError( ('invalid IP range: %(start)s-%(end)s (lower bound IP greater than' ' upper bound)') % { 'start': start, 'end': end}) ip_range = netaddr.IPRange(start_ip_address, end_ip_address) return netaddr.IPSet(ip_range)
def gaussian_with_backward_substitution(A): n = A.shape[0] if n + 1 != A.shape[1]: raise ValueError('Invalid Matrix Size') m = np.matrix([[], []]) m.reshape((n, n)) for i in range(0, n): p = -1 for j in range(i, n): if A[j][i] != 0: p = j break if p == -1: raise ValueError('no unique solution exists') if p != i: A = swapRows(A, i, p) for j in range(i + 1, n + 1): m[j][i] = A[j][i] / A[i][i] A[j] = A[j] - m[j][i] * A[i] if A[0][0] == 0: raise ValueError('no unique solution exists') x = np.array([]) x[n - 1] = A[n - 1][n] / A[n - 1][n - 1] for i in range(n - 2, -1, -1): s = 0 for j in range(i + 1, n - 1): s = s + A[i][j] * x[j] x[i] = (A[i][n + 1] - s) / A[i][i] return x
def __init__(self, *args): self._fields = SortedDict() self._forms = [] self._fields['_forms'] = self._forms self._initial = {} self._fields['_initial'] = self._initial self._form_ids = [] self._fields['_form_ids'] = self._form_ids base_fields = SortedDict() self._fields['base_fields'] = base_fields for form in args: if not isinstance(form, forms.BaseForm): raise ValueError( "All arguments to the ComboForm constructor must be forms." ) elif not hasattr(form, 'save'): raise ValueError( "Only forms that implement a 'save' method can be added " "to the ComboForm.") self._forms.append(form) self._add_fields_for_form(form, base_fields) self._generate_class() self._render(base_fields)
def jaro_winkler(lhs, rhs, prefix_scale=0.1): """ :param lhs: The object to compare :param rhs: The object to compare with :param prefix_scale: The scale factor to use for common prefixes. The value should not be larger than **0.25**, although this is not enforced by the function. :return: A float >= 0.0. For 0.0 <= *prefix_scale* <= 0.25. The return value will be in the range [0.0, 1.0] :raise: ValueError Implements the Jaro Winkler Distance as described `here <https://secure .wikimedia.org/wikipedia/en/wiki/Jaro%E2%80%93Winkler_distance>`__ . The Jaro Winkler favours strings with a common prefix. The weight given to strings with a common prefix is controlled using the *prefix_scale* parameter. The standard value for *prefix_scale* for the Jaro Winkler distance is 0.1 and the value should normally not be greater than 0.5 as this could produce distance values greater than 1.0. For the common prefix, a maximum of 4 characters will be considered. """ if not lhs or not rhs: raise ValueError("Input cannot be empty") if type(lhs) != type(rhs): raise ValueError("Input should be of the same type") dist = jaro_distance(lhs, rhs) prefix = _get_prefix(lhs, rhs) return dist + (prefix * prefix_scale * (1 - dist))
def dice_coefficient(lhs, rhs): """ :param lhs: The object to compare :param rhs: The object to compare with :return: A float in the range [0.0, 1.0] :raise: ValueError Calculates the dice coefficient as described `here <https://secure.wikimedi a.org/wikipedia/en/wiki/Dice%27s_coefficient>`__ using the equation. .. math:: s = \\frac{2 * | lhs \cap rhs |}{| lhs | + | rhs |} When comparing strings, the bigrams are calculated for the both strings and they are then compared, using the above equation. """ if not lhs or not rhs: raise ValueError("Input can not be empty") if type(lhs) != type(rhs): raise ValueError("Input should be of the same type") if isinstance(lhs, (str, unicode)) and isinstance(rhs, (str, unicode)): #Generate the bigrams lhs = [lhs[index:index + 2] for index, _ in enumerate(lhs[0:-1])] rhs = [rhs[index:index + 2] for index, _ in enumerate(rhs[0:-1])] inter = len(set(lhs).intersection(set(rhs))) return (2 * inter) / float(len(lhs) + len(rhs))
def lcs_length(lhs, rhs): """ :param lhs: The object to compare :param rhs: The object to compare with :return: An int >= 0 indicating the Longest Common Subsequence. :raise: ValueError Calculates the longest common subsequence as described in more detail `here <https://secure.wikimedia.org/wikipedia/en/wiki/Long est_common_subsequence_problem>`__. """ if not lhs or not rhs: raise ValueError("Input cannot be empty") if type(lhs) != type(rhs): raise ValueError("Input should be of the same type") m = Matrix(len(lhs) + 1, len(rhs) + 1) for i, char1 in zip(range(1, len(lhs) + 1), lhs): for j, char2 in zip(range(1, len(rhs) + 1), rhs): if char1 == char2: m[i, j] = m[i - 1, j - 1] + 1 else: m[i, j] = max(m[i, j - 1], m[i - 1, j]) return m[len(lhs), len(rhs)]
def jaro_distance(lhs, rhs): """ :param lhs: The object to compare :param rhs: The object to compare with :return: A float in the range [0.0, 1.0]. 1.0 denotes a perfect match. :raise: ValueError Implements the Jaro Distance as described `here <https://secure.wikimedia .org/wikipedia/en/wiki/Jaro%E2%80%93Winkler_distance>`__ . """ if not lhs or not rhs: raise ValueError("Input cannot be empty") if type(lhs) != type(rhs): raise ValueError("Input should be of the same type") max_range = max(floor(float(max(len(lhs), len(rhs))) / float(2.0)) - 1, 0) commons1, _len1 = _get_commons(lhs, rhs, max_range) commons2, _len2 = _get_commons(rhs, lhs, max_range) if _len1 == 0 or _len2 == 0: return 0 num_transpositions = sum(ch1 != ch2 for ch1, ch2 in zip(commons1, commons2)) / 2.0 return (_len1 / float(len(lhs)) + _len2 / float(len(rhs)) + (_len1 - num_transpositions) / float(_len1)) / 3.0
def parse_duration_PYYYY_MM_DDThh_mm_ss(str_iso): '''P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]''' match = RE_DURATION_PYYYY_MM_DDThh_mm_ss.search(str_iso) if not match: return None str_dict = match.groupdict() int_dict = strdict2intdict(str_dict) # # test if int_dict['month'] > 12: raise ValueError('month can not be higher than 12') if int_dict['day'] > 31: raise ValueError('day can not be higher than 31') if int_dict['hour'] > 23: raise ValueError('hour can not be higher than 23') if int_dict['minute'] > 59: raise ValueError('minute can not be higher than 59') if int_dict['second'] > 59: raise ValueError('second can not be higher than 59') # rd = relativedelta(years=int_dict['year'], months=int_dict['month'], days=int_dict['day'], hours=int_dict['hour'], minutes=int_dict['minute'], seconds=int_dict['second']) return rd
def jaccard_distance(lhs, rhs): """ :param lhs: The object to compare :param rhs: The object to compare with :return: A float in the range [0.0, 1.0] :raise: ValueError Calculates the Jaccard Distance for the two objects. The full explanation can be found `here <https://secure.wikimedia .org/wikipedia/en/wiki/Jaccard_index>`__. The equation used is as follows. .. math:: J_{\\delta}(lhs,rhs) = { { |lhs \\cup rhs| - |lhs \cap rhs| } \\over |lhs \\cup rhs| } """ if not lhs or not rhs: raise ValueError("Input cannot be empty") if type(lhs) != type(rhs): raise ValueError("Input should be of the same type") s1 = set(lhs) s2 = set(rhs) try: return 1 - float(len(s1.intersection(s2))) / float(len(s1.union(s2))) except ZeroDivisionError: return 1
def _validates_number(self, key, number): """validates the given number value """ if not isinstance(number, (int, str, unicode)): raise TypeError("Shot.number should be and instance of integer, " "string or unicode") # first convert it to a string number = str(number) # then format it # remove anything which is not a number or letter number = re.sub(r"[^0-9a-zA-Z\-_\ ]+", "", number) number = re.sub(r"[\ ]+", "_", number) #number = number.upper() if number == "": raise ValueError("Shot.number is not in good format, please " "supply something like 1, 2, 3A, 10B") # now check if the number is present for the current Sequence shot_instance = db.session.query(Shot).\ filter(Shot.number==number).\ filter(Shot.sequence_id==self.sequence.id).\ first() if shot_instance is not None: raise ValueError("Shot.number already exists for the given " "sequence please give a unique shot code") return number
def input_parsing(inputs, move_to=None): """ parse input in order to build the list of requested chip directories, and the site and location to move to, as an option. inputs : list of chip numbers, either a number strig (e.g. 0001), or a wildcard string (e.g. 01*) or a file containing chip numbers. assume that user only input ASPIC numbers """ chips = inputs if '*' in chips: chipdirs = glob.glob(os.path.join(data_dir, "CHIP" + chips)) elif os.path.exists(chips): chiplist = np.loadtxt(chips, dtype=str) chipdirs = [os.path.join(data_dir, "CHIP" + chip) for chip in chiplist] else: chipdirs = os.path.join(data_dir, "CHIP" + chips) if not os.path.exists(chipdirs): raise ValueError("%s not valid, %s not found" % (chips, chipdirs)) else: chipdirs = [chipdirs] if move_to is not None: try: site, location = move_to.split('/') except: raise ValueError( 'move_to argument must be in format site/location') return chipdirs, site, location
def active_or_dead(result): if not result.has_key('cmd'): raise KeyError("Not a shell or command action result") if (('systemctl' not in result['cmd']) and ('status' not in result['cmd'])): raise ValueError("Not a systemctl status command") active_lines = [ l for l in result['stdout'].splitlines() if l.strip().startswith('Active: ') ] if len(active_lines) > 1: raise ValueError("Multiple 'Active:' lines detected") elif len(active_lines) < 1: raise ValueError("No 'Active:' lines detected") active_line = active_lines[0] if (((": active (exited)" in active_line) or (": active (running)" in active_line)) and (int(result['rc']) == 0)): return True if (": inactive (dead)" in active_line) and int(result['rc']) != 0: return True return False
def __init__(self, *args, **kwargs): if kwargs.has_key('days'): super(PGDate, self).__init__(**kwargs) elif len(args) == 3: year = int(args[0]) if year < 0: year = year + 1 elif year == 0: raise ValueError( "There is no year zero - http://lmgtfy.com/?q=year+0") month = int(args[1]) if month > 12 or month < 1: raise ValueError( "Month must not be less than 1 or greater than 12") day = int(args[2]) # TO DO - validate input a = (14 - month) / 12 y = year + 4800 - a m = month + 12 * a - 3 super(PGDate, self).__init__(days=(day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32046) + 0.5) else: raise TypeError( "__init__() takes either a kwarg called 'days' or 3 args")
def numToList(string): """Converts a string like '3,5,7-9,14' into a list.""" ret = [] numsplit = string.split(",") # the following code makes nums into a list of all integers for n in numsplit: nr = n.split('-') # handle the case of a single number if len(nr) == 1: try: ret.append(int(n)) except: raise ValueError("number") # handle the case of a range elif len(nr) == 2: try: low = int(nr[0]) nx = nr[1].split("%", 1) if len(nx) == 1: high = int(nr[1]) + 1 step = 1 else: high = int(nx[0]) + 1 step = int(nx[1]) if low > high: raise ValueError("number") ret += range(low, high, step) except ValueError: raise ValueError("number") else: raise ValueError("range") return ret
def next_id(self): now = int(time.time() * 1000) duplicateTime = (now == self.lastPushTime) self.lastPushTime = now timeStampChars = numpy.empty(8, dtype=str) for i in range(7, -1, -1): timeStampChars[i] = self.PUSH_CHARS[now % 64] now = int(now / 64) if (now != 0): raise ValueError('We should have converted the entire timestamp.') uid = ''.join(timeStampChars) if not duplicateTime: for i in range(12): self.lastRandChars[i] = int(random.random() * 64) else: # If the timestamp hasn't changed since last push, use the # same random number, except incremented by 1. for i in range(11, -1, -1): if self.lastRandChars[i] == 63: self.lastRandChars[i] = 0 else: break self.lastRandChars[i] += 1 for i in range(12): uid += self.PUSH_CHARS[self.lastRandChars[i]] if len(uid) != 20: raise ValueError('Length should be 20.') return uid
def mogrify(self, operation, parameters = {}, names = {}): op = operation #resolve names allnames = {} if hasattr(config, "NAMES"): allnames.update(config.NAMES) allnames.update(names) #names parameter has priority over config.NAMES dict for alias, name in allnames.items(): if not re.match("[a-zA-Z0-9_]+", name): raise ValueError(name + " is not a valid name") op = re.sub("%\(" + alias + "\)t", name, op) match = re.search("%\([a-zA-Z0-9_]+\)t", op) if match: raise ValueError(match.group(0) + " is not defined in names dict") #replace %m with metadata op = re.sub("%m", get_metadata(), op) #set schema if self.schema != None: if not re.match("[a-zA-Z0-9_]+", self.schema): raise ValueError(self.schema + " is not a valid schema") op = "SET search_path TO " + self.schema + ", public;" + op return self.cur.mogrify(op, parameters)
def ap_name(self, name): if (len(str(name)) <= 0 or len(str(name)) >= 31): raise ValueError( "Access Point names must be between 1 and 31 characters long.") elif re.findall("\s", name): raise ValueError("Name cannot contain any spaces or tabs.") else: self._ap_name = name
def nbest_align(a1, a2, cost_fn=None, substring_cost=False): """ Find alignment paths through a pair of iterables or lattices, a1 and a2. The arguments a1 and a2 may either be iterables or lattices represented as FrozenGraph objects. Mixing of the two argument types is allowed. The alignment is done using either the values returned by the iterables or the labels on the arcs of the FrozenGraph. FrozenGraph arguments must return True from is_lattice(). An alignment path is a sequence of pairs <t1, t2> where the elements of each pair are either tokens from a1 and a2, respectively, or None. Every pair will have at least one real token; <None,None> pairs will never occur. The paths found by the algorithm have the following properties: 0) Taking only the first or only the second element of each pair, and removing the Nones, each path is a simple path from the start of the corresponding lattice to the end. 1) Considering each pair as an edit, where a (token,token) pair is either an exact match or a substitution, a (token, None) pair is an insertion, and a (None, token) pair is a deletion, and given a cost function on such pairs, the paths are returned in order of cost, with minimal cost paths returned first. The iterator returned by this function returns alignment paths in the form: (total_cost, (edit1, edit2, ... editN)) where each edit is a triple (t1, t2, edit_cost). The paths are returned in non-decreasing order of total cost. cost_fn may be used to provide costs for substitutions, insertions, and deletions, and must take two arguments which are either labels from a1 and a2, respectively, or None, and return a cost C >= 0. If the first argument is None, the cost should be that of an insertion. If the second argument is None, the cost should be that of a deletion. In no case will both arguments be None. If cost_fn is not given, the standard function using 0 for an exact match and 1 for all other costs will be used. If substring_cost is True, align one lattice (a1) within the other (a2). This works like ordinary alignment except that any number of elements from 11 may be freely deleted before the first substitution or insertion and after the last substitution or insertion. Note that insertions from a2 are never free. Please see the help for this module for a collection of examples. """ if not isinstance(a1, FrozenGraph): a1 = _sequence_to_linear_graph(a1) elif not a1.is_lattice(): raise ValueError("First argument is not a lattice") if not isinstance(a2, FrozenGraph): a2 = _sequence_to_linear_graph(a2) elif not a2.is_lattice(): raise ValueError("Second argument is not a lattice") return _lattice_nbest_align(a1, a2, cost_fn, substring_cost)
def set_list_of_values(self, ename, lof): """ Set the list of authorized values for an entry @param ename : name of the entry, must be added by @add_expected_entry@ before @param lof : a list of string items that user can choose. @raise ValueError if lof is not a list of strings """ if ename not in self._expected: raise ValueError("You must add the entry with @add_expected_entry@ before setting a list of values for it") # notice that we allow the list of lists for sensors listing : [label, id] if ( type(lof) != list ) or ( [i for i in lof if type(i) not in [str, list]] != [] ): raise ValueError("`lof` must be a list of strings or an empty list.") self._list_of_values[ename] = lof
def add_filter(self, key, values): """ Add a filter to the restriction list. This filter will help UIs to restrict the choices offered to the user. The method will add the filter entry to the list, or update the existing value if one exists with the same key @param key : a word to describe what should be restricted, ex : housecode, usercode, address, month, etc ... @param values : a list of value to filter to, those values describe what should be *included* @raise ValueError if key is not a one-word string or if values is not a list """ if (key == None) or (type(key) != str) or (len(key.split(' ')) != 1): raise ValueError("`key` must be a one-word string.") if (type(values) != list): raise ValueError("`values` must be a list.") self._filters[key] = values
def set_default_value(self, ename, value): """ Set a default value for en entry This value should be preselected by the form @param ename : name of the entry, must be added by @add_expected_entry@ before @param value : a string to use as default value @raise ValueError if value is not a string """ if ename not in self._expected: raise ValueError("You must add the entry with @add_expected_entry@ before setting a list of values for it") if ( type(value) != str ): raise ValueError("`value` must be a string") self._default[ename] = value
def add_value_to_list(self, ename, value): """ Add a value to the list of authorized values for an entry @param ename : name of the entry, must be added by @add_expected_entry@ before @param value : a string to add @raise ValueError if value is not a string or ename is not defined Does nothing if the value already in list """ if ename not in self._expected: raise ValueError("You must add the entry with @add_expected_entry@ before setting a list of values for it") if ( type(value) != str ): raise ValueError("`value` must be a string") if value in self._list_of_values[ename]: return self._list_of_values[ename].append(value)
def __init__(self, im, vertical, left_edge, right_edge): """horizontal_left and horizontal_right""" if len(left_edge) != len(right_edge) or len(left_edge) != vertical[1] - vertical[0]: raise ValueError("Wrong unequal left_edge, right_edge lengths and vertical range") if min(left_edge) >= max(right_edge): raise ValueError("Minimum of left edge %s >= maximum of right edge %s" % (min(left_edge), max(right_edge))) for ind in range(len(left_edge)): if left_edge[ind] >= right_edge[ind]: raise ValueError("Left edge >= right edge at index %s: %s, %s" % (ind, left_edge[ind], right_edge[ind])) self.im = im self.vertical = vertical self.left_edge = left_edge self.right_edge = right_edge
def set_fixed(self, parname, **kwargs): """ function to fix a parameter to its default value in the LogLikelihood optimisation input : parname (parameter name) """ if parname not in self.sigma.params: raise ValueError('%s not a parameter of logLike function.\n'%parname\ +'\t The parameters are %s'%self.sigma.params.keys()) if parname not in self.free_pars: raise ValueError('%s is already fixed at value %g'%\ (parname, self.sigma.params[parname])) else: del self.free_pars[parname]
def measureAtColor(self, voltages_color, channel, span, stepsize=1): """ measureAtColor returns a list of measured colors wich are half of the range up and down the channel. * voltages_color * span -- number of points * stepsize -- integer """ if channel == "red": index = 0 elif channel == "green": index = 1 elif channel == "blue": index = 2 else: raise ValueError( "Channel must be one of 'red,' 'green,' or 'blue'") voltages = list(voltages_color[0]) measured_series = [] voltages[index] = int(voltages[index] - 0.5 * span * stepsize) for i in range(span + 1): voltages[index] = int(voltages[index] + i * stepsize) print("Between points voltages: " + str(voltages)) measured_series.append(self.measureColor(voltages)) return measured_series
def get_config(self, section=None, option=None): """ Sends the request to get the matching configuration. :param section: the optional name of the section. :param option: the optional option within the section. :return: dictionary containing the configuration. """ if section is None and option is not None: raise ValueError('--section not specified') path = self.CONFIG_BASEURL if section is not None and option is None: path += '/' + section elif section is not None and option is not None: path += '/'.join(['', section, option]) url = build_url(choice(self.list_hosts), path=path) r = self._send_request(url, type_='GET') if r.status_code == codes.ok: return r.json() else: exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content) raise exc_cls(exc_msg)
def process(self, event): if isinstance(event, QueryEvent): self.process_query_event(event) elif isinstance(event, NGramEvent): self.process_ngram_event(event) else: raise ValueError("Unexpected event type passed to NGramIndexProcessor")
def test_multiple_inheritance(self): from exceptions import LookupError, ValueError, Exception, IOError class A(LookupError, ValueError): pass assert issubclass(A, A) assert issubclass(A, Exception) assert issubclass(A, LookupError) assert issubclass(A, ValueError) assert not issubclass(A, KeyError) a = A() assert isinstance(a, A) assert isinstance(a, Exception) assert isinstance(a, LookupError) assert isinstance(a, ValueError) assert not isinstance(a, KeyError) from exceptions import UnicodeDecodeError, UnicodeEncodeError try: class B(UnicodeTranslateError, UnicodeEncodeError): pass except TypeError: pass else: fail("bah") class C(ValueError, IOError): pass c = C() assert isinstance(ValueError(), ValueError) assert isinstance(c, C) assert isinstance(c, Exception) assert isinstance(c, ValueError) assert isinstance(c, IOError) assert isinstance(c, EnvironmentError) assert not isinstance(c, KeyError)