def inflect(lemma, inflection): """\ Return the inflected form given lemma and inflection rules. Supports front, back and mid changes. """ form = lemma # replace irregular if inflection.startswith('*'): form = inflection[1:] # if there are changes, perform them elif inflection != '': # find out the front, mid, back changes diffs = inflection.split(",") front = first(lambda x: x.startswith('<'), diffs) back = first(lambda x: x.startswith('>'), diffs) mid = first(lambda x: '-' in x, diffs) # perform the changes add_back = '' # chop off the things from the back if back is not None: chop, add_back = re.match(r'^>([0-9]+)(.*)$', back).groups() chop = int(chop) if chop != 0: form = form[0:-chop] # changes in the middle if mid is not None: mids = mid.split(' ') search_part = form[0:-1].lower() for change in mids: orig, changed = change.split('-') orig_len = len(orig) # numbered change format -- position and length if re.match(r'^[0-9]', orig): neg_pos, orig_len = re.match(r'^([0-9]+):([0-9]+)$', orig).groups() orig_len = int(orig_len) pos = len(lemma) - int(neg_pos) # letter change format: find by adjacent letters elif len(search_part) > 0: pos = search_part.rfind(orig, 0) else: pos = len(form) - 1 if pos >= -1: # TODO what if we prohibit -1 ? form = form[0:pos] + changed + form[pos + orig_len:] search_part = search_part[0:pos] # add things to beginning and end if front is not None: form = front[1:] + form form = form + add_back # return the resulting word form return form
def __get_job_state(self): """\ Parse the qstat command and try to retrieve the current job state and the machine it is running on. """ # get state of job assuming it is in the queue output = self.__try_command('qstat') # get the relevant line of the qstat output output = first(lambda line: re.search(self.jobid, line), output.split("\n")) # job does not exist anymore if output is None: return self.FINISH, None # parse the correct line: fields = re.split(r'\s+', output) state, host = fields[4], fields[7] host = re.sub(r'.*@([^.]+)\..*', r'\1', host) return state, host
def remove_dependency(self, dependency): """\ Removes the given Job(s) from the dependencies list. """ # single element removed if isinstance(dependency, (Job, basestring, int)): if isinstance(dependency, int): jobid = str(dependency) else: jobid = dependency rem = first(lambda d: d == jobid, self.__dependencies) if rem is not None: self.__dependencies.remove(rem) else: raise ValueError('Cannot find dependency!') elif isinstance(dependency, collections.Iterable): for dep_elem in dependency: self.remove_dependency(dep_elem) else: raise ValueError('Unknown dependency type!')