def run(self): self._app.logger.info('sendmail run') filesize = os.path.getsize(self._filepath) self._app.logger.info('backup size {0}'.format(filesize)) # limit size if 0 < self._app.config.email_limit_size_attach < filesize: raise Exception('InvalidConfigException') # split if 0 < self._app.config.email_chunk_max_size < filesize: # work directory dir_split = os.path.join(os.path.dirname(self._filepath), 'split') if not os.path.exists(dir_split): os.mkdir(dir_split) # 7z a -vSIZE split(self._filepath, os.path.join(dir_split, os.path.basename(self._filepath)), str(self._app.config.email_chunk_max_size) + 'b') for f in os.listdir(dir_split): if os.path.isfile(os.path.join(dir_split, f)): filepath = os.path.join(dir_split, f) getattr(self, 'send_{}'.format( self._app.config.email_transport))(filepath) os.remove(filepath) # clear directory shutil.rmtree(dir_split) else: getattr(self, 'send_{}'.format(self._app.config.email_transport))( self._filepath)
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" if len(typed_words) == 0 or len(reference_words) == 0: return 0.0 i, j = 0, 0 while i < min(len(typed_words), len(reference_words)): if typed_words[i] == reference_words[i]: j += 1 i += 1 return j * 100 / len(typed_words)
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" if len(typed_words) == 0: return 0.0 length = min(len(typed_words), len(reference_words)) valid_num = 0 for i in range(0, length): # typed_words列表和reference_words列表相对应位置进行比较 if typed_words[i] == reference_words[i]: valid_num += 1 percentage = 100 * valid_num / len(typed_words) return percentage
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 num_correct, num_typed = 0, 0 if len(typed_words) == 0 or len(reference_words) == 0: return 0.0 for i in typed_words: if num_typed < len( reference_words) and i == reference_words[num_typed]: num_correct += 1 num_typed += 1 return round(num_correct / len(typed_words), 4) * 100
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" check_list = [ 1 for x in range(len(reference_words)) if typed_words[x] == reference_words[x] ] return (sum(check_list) / len(typed_words)) * 100
def extract_tests(*args): """Extracts all tests and packages them into suites @return: suites, decommented_code """ block_comments, inline_comments, code = Expect.extract_comments(*args) block_tests, inline_tests = Expect.identify_tests(*args) # test suites suites = [] # assemble all block tests for block in block_comments: lines, suite = list(block.split('\n')), [] while lines: line = lines.pop(0).strip() map_break(block_tests, lambda test: line.startswith(test['input_prefix']), lambda test: suite.append( [line, lines.pop(0), test] if not test.get('one-liner', False) else split(line, test['output_prefix']) + [test])) suites.append(suite) # assemble all inline tests for inline in inline_comments: inline = inline.strip() map_break(inline_tests, lambda test: inline.startswith(test['input_prefix']), lambda test: suites.append([split(inline, test['output_prefix']) + [test]])) return suites, code
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 if typed == "": return 0.0 counter = 0 for i in range(min(len(typed_words), len(reference_words))): if typed_words[i] == reference_words[i]: counter += 1 return counter/len(typed_words) * 100
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" if len(typed_words) == 0: return 0.0 count = 0 i = 0 for x in typed_words: if i >= len(reference_words): break if x == reference_words[i]: count += 1 i += 1 return 100 * count / len(typed_words)
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 diff = 0 for i in range(min(len(typed_words), len(reference_words))): if typed_words[i] != reference_words[i]: diff += 1 diff += max(0, len(typed_words) - len(reference_words)) return (len(typed_words) - diff) / len(typed_words) * 100 if\ len(typed_words) != 0 else 0.0
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" length = min(len(reference_words),len(typed_words)) if length == 0: return 0.0 i, accurate =0, 0.0 while i < length: if typed_words[i] == reference_words[i]: accurate += 1 i+=1 if accurate == len(typed_words): return 100.0 return min(100.0, 100.0*accurate/max(len(reference_words), len(typed_words)))
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 if typed == '': return 0.0 typed_words_length = len(typed_words) ref_words_length = len(reference_words) count = 0 for position in range(typed_words_length): if position < ref_words_length and typed_words[position] == reference_words[position]: count += 1 return (count / typed_words_length) * 100
def load_data(self): """Loads the input data. Return: A dictionary containing the data. The entries are: s_coarse - The coarse data of the sample s_fine - The fine data of the sample s_gain - The gain data of the sample r_coarse - The coarse data of the reset r_fine - The fine data of the reset r_gain - The gain data of the reset """ idx = (self._frame, self._row, self._col) data = {} with h5py.File(self._input_fname, "r") as f: # sample path = self._paths["sample"] coarse, fine, gain = utils.split(f[path][idx]) data["s_coarse"] = coarse data["s_fine"] = fine data["s_gain"] = gain # reset path = self._paths["reset"] coarse, fine, gain = utils.split(f[path][idx]) data["r_coarse"] = coarse data["r_fine"] = fine data["r_gain"] = gain return data
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" counter = 0 len_t = len(typed_words) len_r = len(reference_words) if len_t == 0 or len_r == 0: return 0.0 else: for i in range(min(len_t, len_r)): if typed_words[i] == reference_words[i]: counter += 1 return counter / len_t * 100
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" if len(typed_words) == 0: return 0.0 else: counted_words = 0 short_len = min(len(typed_words), len(reference_words)) for i in range(short_len): if typed_words[i] == reference_words[i]: counted_words += 1 return counted_words * 100 / len(typed_words)
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" if typed == "": return 0.0 correct = 0 for i in range(len(reference_words)): if len(typed_words) == i: break if typed_words[i] == reference_words[i]: correct += 1 return (correct / len(typed_words)) * 100.0
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) # BEGIN PROBLEM 3 "*** YOUR CODE HERE ***" score = 0 total = len(typed_words) if total == 0: return 0.0 for i in range(total): if i + 1 > len(reference_words): return score / total * 100 elif typed_words[i] == reference_words[i]: score += 1 return score / total * 100
def getChoiceValue(img): rows = split(img, r= 5) result = [] for i, r in enumerate(rows): rows[i] = crop(r) subrows = split(rows[i], r=5) for sr in subrows: cols = split(sr, c=4) wp = [] for col in cols: wp.append(countWhitePoint(col)) ind = wp.index(max(wp)) res = "A" if ind == 1: res = "B" if ind == 2: res = "C" if ind == 3: res = "D" result.append(res) return result
def accuracy(typed, reference): """Return the accuracy (percentage of words typed correctly) of TYPED when compared to the prefix of REFERENCE that was typed. >>> accuracy('Cute Dog!', 'Cute Dog.') 50.0 >>> accuracy('A Cute Dog!', 'Cute Dog.') 0.0 >>> accuracy('cute Dog.', 'Cute Dog.') 50.0 >>> accuracy('Cute Dog. I say!', 'Cute Dog.') 50.0 >>> accuracy('Cute', 'Cute Dog.') 100.0 >>> accuracy('', 'Cute Dog.') 0.0 """ typed_words = split(typed) reference_words = split(reference) correctness = 0 if len(typed_words) == 0: return 0.0 elif len(typed_words) <= len(reference_words): for i in range(len(typed_words)): if typed_words[i] == reference_words[i]: correctness += 1 else: for i in range(len(reference_words)): if typed_words[i] == reference_words[i]: correctness += 1 return correctness / len(typed_words) * 100
def main_ts(): parser = argparse.ArgumentParser() parser.add_argument("--dir_scratch", type=str, help="temp directory in which to save data") args = parser.parse_args() dir_scratch = args.dir_scratch path_params = dir_scratch + "params.json" dir_data = dir_scratch + "data/" dir_ts = dir_scratch + "ts/" comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() # Use half the cores for merging to keep mem within bounds size_half = int(size / 4) if size_half == 0: size_half = 1 print(rank, "hello") # load jobs from json in the root process if rank == 0: params = load_params(path_params) jobs = params['ts'] jobs_ts = add_paths_to_jobs(jobs, dir_data, dir_ts) make_work_data(jobs_ts, dir_data, dir_ts) jobs_ts = split(jobs_ts, size) else: jobs_ts = None jobs_ts = comm.scatter(jobs_ts, root=0) for j in jobs_ts: worker_ts(j) print(rank, "barrier") comm.Barrier() if rank == 0: jobs_merge = add_paths_to_jobs(jobs, dir_data, dir_ts) jobs_merge = make_jobs_merge(jobs_merge, dir_data, dir_ts) jobs_merge = split(jobs_merge, size_half, size) else: jobs_merge = None jobs_merge = comm.scatter(jobs_merge, root=0) for j in jobs_merge: worker_merge(j) print(rank, "barrier") comm.Barrier()
def second(): good_count = 0 for line in inp: policy, password = utils.split(line, ":") rng, letter = policy.split() min_r, max_r = utils.split(rng, "-", as_num=True) if (password[min_r - 1] == letter) != (password[max_r - 1] == letter): good_count += 1 print(good_count) return
def first(): good_count = 0 for line in inp: policy, password = utils.split(line, ":") rng, letter = policy.split() min_r, max_r = utils.split(rng, "-", as_num=True) cnt = password.count(letter) if (min_r <= cnt <= max_r): good_count += 1 print(good_count) return
def __readData__(self): """main reader function which also populates the class data structures""" with open(self.file, 'r') as f: for line in f: cur_sentence = [] splited_words = split(line, (' ', '\n')) del splited_words[-1] for word_and_tag in splited_words: cur_word, cur_tag = split(word_and_tag, '_') cur_sentence.append((cur_word, cur_tag)) self.sentences.append(cur_sentence)
def __call__(self, sm): sm_r = self.sme.randomize_smiles(sm) # Random transoform if sm_r is None: sm_spaced = split(sm) # Spacing else: sm_spaced = split(sm_r) # Spacing sm_split = sm_spaced.split() if len(sm_split)<=MAX_LEN - 2: return sm_split # List else: return split(sm).split()
def getExamIdValue(img): cols = split(img, c= 3) result = [] for col in cols: rows = split(col, r= 10) wp = [] for row in rows: wp.append(countWhitePoint(row)) result.append(wp.index(max(wp))) return result
def buildTree(data): if len(data) <= 0: return node() currentEnt = entropy(data) bestGain = 0.0 bestCriteria = None bestSets = None dimension = len(data[0]) - 1 for feature in range(dimension): feature_values = {} for item in data: feature_values[data[feature]] = 1 for value in feature_values.keys(): set1, set2 = split(data, feature, value) p = len(set1) / len(set2) infoGain = currentEnt - p * entropy(set1) - (1 - p) * entropy(set2) if infoGain > bestGain and len(set1) > 0 and len(set2) > 0: bestGain = infoGain bestCriteria = (feature, value) bestSets = (set1, set2) if bestGain > 0: leftBranch = buildTree(bestSet[0]) rightBranch = buildTree(bestSet[1]) return node(feature=bestCriteria[0], threshold=bestCriteria[1], left=leftBranch, right=rightBranch) else: return node(results=stats(data))
def bouncy_and_increment(n): """ n {int} number to test for bounciness return a tuple with a boolean for whether it was bouncy and, if it's false, a number for how much to increment the current number by """ digs = split(n) # direction can be -1: decreasing, 0: constant, 1: increasing direction = 0 for i, d in enumerate(digs[1:], 1): if digs[i - 1] < d: # this current digit is greater than last if direction == -1: # it had been decending return True direction = 1 elif digs[i - 1] > d: # this current digit is less than the last if direction == 1: # it had been ascending return True direction = -1 return False
def main(): #Load the data content_data = data_pro.main() #title_feats title_feats = title_transform(content_data) #source_feats source_feats = pd.get_dummies(content_data['source']).values #reporter_feats reporter_feats = pd.get_dummies(content_data['reporter']).values #week_feats week_feats = pd.get_dummies(content_data['week']).values #topology_feat try: content_data['l1_norm'] = pd.read_csv('l1_norm.csv')['l1_norm'] except: print('csv file not found recalculate') content_data['l1_norm'] = content_topology(content_data) train_data,_,_ = utils.split(content_data) sh_ratio_dict,total_count_dict = utils.compute_author(train_data) content_data['sh_ratio_range'] = content_data['author'].apply(lambda x: fill_way(x,sh_ratio_dict)) content_data['total_count_range'] = content_data['author'].apply(lambda x:fill_way(x,total_count_dict)) return title_feats,source_feats,reporter_feats,week_feats,content_data
def test_parse_commas_and_colon(self): val = \ u.split('2GOZ|1|A|A|3:2GOZ|1|A|A|5,2GOZ|1|A|A|12,2GOZ|1|A|A|3:2GOZ|1|A|A|30') ans = [tuple(['2GOZ|1|A|A|3', '2GOZ|1|A|A|5']), tuple(['2GOZ|1|A|A|12', '2GOZ|1|A|A|12']), tuple(['2GOZ|1|A|A|3', '2GOZ|1|A|A|30'])] self.assertEquals(ans, val)
def SquareOfTwoGenerator(limit=1000): """ numerators = 3, 7, 17, 41, 99 denominators = 2, 5, 12, 29, 70 the next numerator is the current numerator + 2 times the last denominator the next denominator is the sum of the current numerator and denominator """ index, num, den = 0, 3, 2 while index < limit: """set the next numerator and denominator return whether the numerator has more digits than the denominator (see above)""" hit = len(utils.split(num)) > len(utils.split(den)) num, den = num + 2 * den, num + den index += 1 yield hit
def main(): # Parse command line args args = parse_args() print("Loading resources...") # Load Errant annotator = errant.load("en") # Open output m2 file out_m2 = open(args.out, "w") print("Processing parallel files...") # Process an arbitrary number of files line by line simultaneously. Python 3.3+ # See https://tinyurl.com/y4cj4gth with ExitStack() as stack: orig_lines = stack.enter_context(open(args.orig, encoding='utf-8')).readlines() cor_lines = stack.enter_context(open(args.cor[0], encoding='utf-8')).readlines() pairs = list(zip(orig_lines, cor_lines)) batch_size = len(orig_lines) // args.n_procs splits = split(pairs, batch_size) partial_func = partial(label_edits, args=args) with Pool(args.n_procs) as pool: results = pool.map(partial_func, splits) labeled = merge(results) for label in tqdm(labeled): out_m2.write(','.join(label) + '\n')
def apply_nodes_to_labels(self): nodes = list(self.data.keys()) result_node = [nodes[-1]] nodes_up, nodes_down = split(nodes[:-1]) self.apply_labels_to_floater(nodes_up, 0.9) self.apply_labels_to_floater(nodes_down, 0.6) self.apply_labels_to_floater(result_node, 0.23, False)
def helper(str): str = split(remove_punctuation(lower(str))) for i in range(0, len(str)): for j in range(0, len(topic)): if (str[i] == topic[j]): return True return False
def select(self, selector=None, as_group=False): if (not selector): selector = lambda action_i: (action_i.person % 2) == 1 train, test = utils.split(self.actions, selector) if (as_group): return ActionGroup(train), ActionGroup(test) return train, test
def cmdstring_to_tuple(cmd): """Convert command string to tuple for run_command() and others :param str cmd: command to be converted :return: command as tuple """ return cmdlist_to_tuple(split(cmd))
def add(self, *components): entities, ports, links = utils.split(components, lambda t: isinstance(t, Entity), lambda t: isinstance(t, Port), lambda t: isinstance(t, Link)) self.entities.extend(entities) self.ports.extend(ports) self.links.extend(links)
def __init__(self, line): if ":" in line: (expr, ranges) = utils.split(line, ":") self.expr = expr.strip() self.ranges = ranges.strip().lstrip("{").rstrip("}") else: self.expr = "nil" self.ranges = "nil"
def _extract_production_rules(self, p_rule): '''Given a p_rule function, extract production rules from it.''' rules = p_rule.__doc__ nonterminals = set() LR = split(rules.replace('\n', ' '), ':') try: head, rhs = LR except ValueError: # 'need more than one value to unpack' (rule derives epsilon) head, rhs = LR[0], EPSILON nonterminals.add(head) alts = map(lambda x: tuple(split(x)), split(rhs, '|')) # alternatives alts = map(lambda alt: alt if alt else (EPSILON,), alts) # check if token_names (terminals) have common names with nonterminals common = list(nonterminals & set(self.tokens)) if common: raise SyntaxError("%r appears in both nonterminals and tokens" % common[0]) return [Production(head=head, body=b, yield_rule=p_rule) for b in alts]
def read_conf_file(self, filename): conf = ConfigParser.ConfigParser() if not conf.read(filename): raise StandardError('invalid configuration file:%s'%filename) self.COOKIE_FILE = conf.get('common', 'reviewboard_cookie_file') DEBUG = conf.getint('common', 'debug') self.REVIEWED_ID_DB_FILE = conf.get('common', 'reviewed_id_db_file') self.RB_SERVER = conf.get('reviewboard', 'url') self.USERNAME = conf.get('reviewboard', 'username') self.PASSWORD = conf.get('reviewboard', 'password') self.MIN_SHIP_IT_COUNT = conf.getint('rule', 'min_ship_it_count') self.MIN_EXPERT_SHIP_IT_COUNT = conf.getint('rule', 'min_expert_ship_it_count') experts = conf.get('rule', 'experts') self.EXPERTS = split(experts) review_path = conf.get('rule', 'review_path') self.REVIEW_PATH = split(review_path) ignore_path = conf.get('rule', 'ignore_path') self.IGNORE_PATH = split(ignore_path)
def Delay(self, delayProf): """ Compute the delayed profile composed of *self* profile and *delayProf*, received by a node for which this *self* profile is the output profile on the sender side. The delay profile describes the delay as a function of time for the link. This function implements the operation: .. math:: o[t + \delta[t]] = l[t] Where * :math:`\delta[t]` is the delay profile * :math:`l[t]` is the profile transmitted into the link (*self*) * :math:`o[t]` is the output profile received at the other end of the link :rtype: :class:`Profile`, :math:`o[t]` :param in delayProf: :class:`Profile` describing the delay """ delays = delayProf.entries['latency'] all0 = True for time, delay in delays: if delay != 0: all0 = False if all0: return copy.deepcopy(self) datas = self.entries['data'] endTime = datas[-1][0] times = [ x[0] for x in delays ] times.extend( [ x[0] for x in datas ] ) times = sorted(list(set(times))) newDatas = [] for t in times: d = utils.get_value_at_time(datas, t) delay = utils.get_value_at_time(delays, t, interpolate = 'latency' in self.interpolated_profiles) newDatas.append([ t + delay, d ]) newDatas = utils.remove_degenerates(newDatas) newDatas, remainder = utils.split(newDatas, endTime) if remainder: t = -remainder[0][0] utils.shift(remainder, t) r_slopes = utils.derive(remainder) d_slopes = utils.derive(newDatas) d_slopes = utils.add_values(d_slopes,r_slopes) newDatas = utils.integrate(d_slopes, endTime) retProf = Profile() retProf.entries['data'] = newDatas retProf.Derive() return retProf
def calc_pmf(h5, gk, grp, prop_obj, prop_dd, A, C): pt_dd = U.get_pt_dd(C, A.property, A.plot_type) if 'bins' not in pt_dd: raise ValueError('bins not found in {0}, but be specified when plotting pmf'.format(A.config)) subgrps = U.split(grp, 10) # 10 is the group_size da = [] for sp in subgrps: bn, psm, pse = calc_distr(h5, '', sp, prop_obj, prop_dd, A, C) pmf, pmf_e = U.prob2pmf(psm, max(psm), pse) for b, i in zip(bn, pmf): print b, i sub_da = np.array([bn, pmf, pmf_e]) da.append(sub_da) return np.array(da)
def __init__(self, line): (decl, loc) = utils.split(line, "@LOCATION:") (func, types, args) = _parse_func(decl) # not parsable (e.g., '0' in ext4) if func is None: func = line args = [] # normal path self.func = func self.args = args self.loc = loc
def _createMenuItem(self, menu, menustyle, label, help, handler, gcmd, keywords, shortcut = '', kind = wx.ITEM_NORMAL): """!Creates menu items There are three menu styles (menu item text styles). 1 -- label only, 2 -- label and cmd name, 3 -- cmd name only """ if not label: menu.AppendSeparator() return if len(gcmd) > 0: helpString = gcmd + ' -- ' + help if menustyle == 1: label += ' [' + gcmd + ']' elif menustyle == 2: label = ' [' + gcmd + ']' else: helpString = help if shortcut: label += '\t' + shortcut menuItem = menu.Append(wx.ID_ANY, label, helpString, kind) self.menucmd[menuItem.GetId()] = gcmd if gcmd: try: cmd = utils.split(str(gcmd)) except UnicodeError: cmd = utils.split(utils.EncodeString((gcmd))) if cmd and cmd[0] not in globalvar.grassCmd['all']: menuItem.Enable(False) rhandler = eval('self.parent.' + handler) self.parent.Bind(wx.EVT_MENU, rhandler, menuItem)
def getDNN(df, random_split=None): df_tr, df_val = split(df, rand_ratio=random_split) X, Y = to_array(df.drop("validation", axis=1)) Xtr, Ytr = to_array(df_tr) Xval, Yval = to_array(df_val) scaler = MinMaxScaler((0, 1)) Xtr = scaler.fit_transform(Xtr) Xval = scaler.transform(Xval) # Start create model print("Create a DNN Classifier") model = Sequential() model.add(Dense(100, input_dim=Xtr.shape[1], activation='tanh')) model.add(PReLU()) model.add(Dropout(0.2)) model.add(Dense(80, activation='linear')) model.add(ELU(alpha=0.3)) model.add(Dropout(0.2)) model.add(Dense(60, activation='tanh')) model.add(PReLU()) model.add(Dropout(0.2)) model.add(Dense(40, activation='linear')) model.add(ELU(alpha=0.1)) model.add(Dropout(0.2)) model.add(Dense(15, activation='linear')) model.add(PReLU()) model.add(Dropout(0.2)) model.add(Dense(1, activation='sigmoid')) # trainer = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) trainer = Adadelta(lr=0.1, tho=0.98, epsilon=1e-7) model.compile(loss='binary_crossentropy', optimizer=trainer) print(Ytr, Yval) model.fit(Xtr, Ytr, nb_epoch=30, batch_size=32, verbose=1, validation_data=(Xval, Yval)) pred_tr = model.predict_proba(Xtr) pred = model.predict_proba(Xval) print("auc on train: {}".format(roc_auc_score(Ytr, pred_tr))) print("auc on validation: {}".format(roc_auc_score(Yval, pred))) X = scaler.fit_transform(X) model.fit(X, Y, nb_epoch=30, batch_size=32) return model, scaler
def OnEnteredText(self, event): """!Text entered""" text = event.GetString() if not text: # control is empty; hide dropdown if shown: if self.dropdown.IsShown(): self._showDropDown(False) event.Skip() return try: cmd = utils.split(str(text)) except ValueError, e: self.statusbar.SetStatusText(str(e)) cmd = text.split(' ')
def _setValueFromSelected(self): """!Sets the wx.TextCtrl value from the selected wx.ListCtrl item. Will do nothing if no item is selected in the wx.ListCtrl. """ sel = self.dropdownlistbox.GetFirstSelected() if sel < 0: return if self._colFetch != -1: col = self._colFetch else: col = self._colSearch itemtext = self.dropdownlistbox.GetItem(sel, col).GetText() cmd = utils.split(str(self.GetValue())) if len(cmd) > 0 and cmd[0] in self._choicesCmd: # -> append text (skip last item) if self._choiceType == 'param': itemtext = itemtext.split(' ')[0] self.SetValue(' '.join(cmd) + ' ' + itemtext + '=') optType = self._module.get_param(itemtext)['prompt'] if optType in ('raster', 'vector'): # -> raster/vector map self.SetChoices(self._choicesMap[optType], optType) elif self._choiceType == 'flag': itemtext = itemtext.split(' ')[0] if len(itemtext) > 1: prefix = '--' else: prefix = '-' self.SetValue(' '.join(cmd[:-1]) + ' ' + prefix + itemtext) elif self._choiceType in ('raster', 'vector'): self.SetValue(' '.join(cmd[:-1]) + ' ' + cmd[-1].split('=', 1)[0] + '=' + itemtext) else: # -> reset text self.SetValue(itemtext + ' ') # define module self._setModule(itemtext) # use parameters as default choices self._choiceType = 'param' self.SetChoices(self._choicesMap['param'], type = 'param') self.SetInsertionPointEnd() self._showDropDown(False)
def OnRunCmd(self, event): """!Run command""" cmdString = event.GetString() if self.standAlone: return if cmdString[:2] == 'd.' and not self.parent.curr_page: self.parent.NewDisplay(show=True) cmd = utils.split(cmdString) if len(cmd) > 1: self.parent.RunCmd(cmd, switchPage = True) else: self.parent.RunCmd(cmd, switchPage = False) self.OnUpdateStatusBar(None)
def print_summary(history, hierarchy): """Split transaction summary per month and category.""" def mkstring(value): return "%.2f (%d)" % (value[0], value[1]) def print_tree(node, indent="\t", level=0): print(mkstring(node[0])) if len(node) > 1: for (label, branch) in sorted(node[1].items()): print(indent * level + str(label), end=": ") print_tree(branch, indent, level + 1) tree = split(history, hierarchy) statistics = lambda ts: [sum(t.amount for t in ts), len(ts)] summary = scan(tree, statistics, lambda xs, ys: [x + y for (x, y) in zip(xs, ys)]) print("total", end=": ") print_tree(summary)
def __init__(self, data, n_valid, corruptor=None, prng=None): """ Parameters ---------- data : numpy array Data matrix array with rows corresponding to data vectors. n_valid : integer Number of data vectors to use as validation set. corruptor : function(Array, RandomState) or None Optional function which applies random 'corruption' / augmentation to data, for example dequantising pixel values, adding noise, applying random affine transformation to image. Applied on initialisation and at end of each training epoch. prng : RandomState or None Seeded pseudo-random number generator - used to shuffle data and for corruptor if specified. """ self.data = data self.n_valid = n_valid self.n_train = data.shape[0] - n_valid self.corruptor = corruptor if prng is None: prng = np.random.RandomState() self.prng = prng shuffled_data, self.perm = utils.shuffle(self.data, self.prng) self.data_valid, self.data_train = utils.split(shuffled_data, n_valid) if corruptor is None: self.x_valid = th.shared( self.data_valid.astype(th.config.floatX), 'x_valid') self.x_train = th.shared( self.data_train.astype(th.config.floatX), 'x_train') else: corrupted_data_valid = self.corruptor(self.data_valid, self.prng) corrupted_data_train = self.corruptor(self.data_train, self.prng) self.x_valid = th.shared( corrupted_data_valid.astype(th.config.floatX), 'x_valid') self.x_train = th.shared( corrupted_data_train.astype(th.config.floatX), 'x_train')
def make(cls, number, ordinal=False): if not isinstance(number, int): raise NotIntegerError() over_5, under_5 = split(number, 5) return unicode(cls.under_5_glyph * under_5 + cls.over_5_glyph * over_5)
def __init__(self, line): (stmt, loc) = utils.split(line, "@LOCATION:") # increment, decrement if "++" in stmt: self.lhs = stmt.rstrip("+") self.rhs = self.lhs + "+ 1" elif "--" in stmt: self.lhs = stmt.rstrip("-") self.rhs = self.lhs + "- 1" # compound assignment elif "+=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "+=") self.rhs = self.rhs + " + " + self.lhs elif "-=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "-=") self.rhs = self.rhs + " - " + self.lhs elif "*=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "*=") self.rhs = self.rhs + " * " + self.lhs elif "/=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "/=") self.rhs = self.rhs + " / " + self.lhs elif "%=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "%=") self.rhs = self.rhs + " % " + self.lhs elif "&=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "&=") self.rhs = self.rhs + " & " + self.lhs elif "|=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "|=") self.rhs = self.rhs + " | " + self.lhs elif "<<=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "<<=") self.rhs = self.rhs + " << " + self.lhs elif ">>=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, ">>=") self.rhs = self.rhs + " >> " + self.lhs # simple assignment elif "=" in stmt: (self.lhs, self.rhs) = utils.split(stmt, "=") else: # unkonw format, bare with real life self.lhs = None self.rhs = line
# Test int_to_base_array() for test in [ ( (255,10), [2,5,5] ), ( (256,10), [2,5,6] ), ( (255,2), [1,1,1,1,1,1,1,1] ), ]: assert int_to_base_array(*test[0]) == test[1] # Test split() for test in [ ( (1,1), (1,0) ), ( (10,1), (10,0) ), ( (0,1), (0,0) ), ( (1.5,0.5), (3,0) ), ( (1.5,1), (1,0.5) ), ( (15,10), (1,5) ), ]: assert split(*test[0]) == test[1] # Test hierarchicize() for test in [ ( (100,[1]), [100] ), ( (100,[1,1]), [100,0] ), ( (100,[100,1]), [1,0] ), ( (1000,[360,12]), [2,23] ), ]: assert hierarchicize(*test[0]) == test[1]
def parse_member_birth(self, member): if 'birth' not in member: return member['birthyear'], member['birthmonth'], member['birthday'] =\ split(member['birth'][0]) del member['birth']
def OnKeyPressed(self, event): """!Key press capture for autocompletion, calltips, and command history @todo event.ControlDown() for manual autocomplete """ # keycodes used: "." = 46, "=" = 61, "-" = 45 pos = self.GetCurrentPos() #complete command after pressing '.' if event.GetKeyCode() == 46 and not event.ShiftDown(): self.autoCompList = list() entry = self.GetTextLeft() self.InsertText(pos, '.') self.CharRight() self.toComplete = self.EntityToComplete() try: if self.toComplete['entity'] == 'command': self.autoCompList = self.moduleList[entry.strip()] except (KeyError, TypeError): return self.ShowList() # complete flags after pressing '-' elif event.GetKeyCode() == 45 and not event.ShiftDown(): self.autoCompList = list() entry = self.GetTextLeft() self.InsertText(pos, '-') self.CharRight() self.toComplete = self.EntityToComplete() if self.toComplete['entity'] == 'flags' and self.cmdDesc: if self.GetTextLeft()[-2:] == ' -': # complete e.g. --quite for flag in self.cmdDesc.get_options()['flags']: if len(flag['name']) == 1: self.autoCompList.append(flag['name']) else: for flag in self.cmdDesc.get_options()['flags']: if len(flag['name']) > 1: self.autoCompList.append(flag['name']) self.ShowList() # complete map or values after parameter elif event.GetKeyCode() == 61 and not event.ShiftDown(): self.autoCompList = list() self.InsertText(pos, '=') self.CharRight() self.toComplete = self.EntityToComplete() if self.toComplete: if self.toComplete['entity'] == 'raster map': self.autoCompList = self.mapList['raster'] elif self.toComplete['entity'] == 'vector map': self.autoCompList = self.mapList['vector'] elif self.toComplete['entity'] == 'param values': param = self.GetWordLeft(withDelimiter = False, ignoredDelimiter='=').strip(' =') self.autoCompList = self.cmdDesc.get_param(param)['values'] self.ShowList() # complete after pressing CTRL + Space elif event.GetKeyCode() == wx.WXK_SPACE and event.ControlDown(): self.autoCompList = list() self.toComplete = self.EntityToComplete() if self.toComplete is None: return #complete command if self.toComplete['entity'] == 'command': for command in globalvar.grassCmd['all']: if command.find(self.toComplete['cmd']) == 0: dotNumber = list(self.toComplete['cmd']).count('.') self.autoCompList.append(command.split('.',dotNumber)[-1]) # complete flags in such situations (| is cursor): # r.colors -| ...w, q, l # r.colors -w| ...w, q, l elif self.toComplete['entity'] == 'flags' and self.cmdDesc: for flag in self.cmdDesc.get_options()['flags']: if len(flag['name']) == 1: self.autoCompList.append(flag['name']) # complete parameters in such situations (| is cursor): # r.colors -w | ...color, map, rast, rules # r.colors col| ...color elif self.toComplete['entity'] == 'params' and self.cmdDesc: for param in self.cmdDesc.get_options()['params']: if param['name'].find(self.GetWordLeft(withDelimiter=False)) == 0: self.autoCompList.append(param['name']) # complete flags or parameters in such situations (| is cursor): # r.colors | ...-w, -q, -l, color, map, rast, rules # r.colors color=grey | ...-w, -q, -l, color, map, rast, rules elif self.toComplete['entity'] == 'params+flags' and self.cmdDesc: self.autoCompList = list() for param in self.cmdDesc.get_options()['params']: self.autoCompList.append(param['name']) for flag in self.cmdDesc.get_options()['flags']: if len(flag['name']) == 1: self.autoCompList.append('-' + flag['name']) else: self.autoCompList.append('--' + flag['name']) self.ShowList() # complete map or values after parameter # r.buffer input=| ...list of raster maps # r.buffer units=| ... feet, kilometers, ... elif self.toComplete['entity'] == 'raster map': self.autoCompList = list() self.autoCompList = self.mapList['raster'] elif self.toComplete['entity'] == 'vector map': self.autoCompList = list() self.autoCompList = self.mapList['vector'] elif self.toComplete['entity'] == 'param values': self.autoCompList = list() param = self.GetWordLeft(withDelimiter = False, ignoredDelimiter='=').strip(' =') self.autoCompList = self.cmdDesc.get_param(param)['values'] self.ShowList() elif event.GetKeyCode() == wx.WXK_TAB: # show GRASS command calltips (to hide press 'ESC') entry = self.GetTextLeft() try: cmd = entry.split()[0].strip() except IndexError: cmd = '' if cmd not in globalvar.grassCmd['all']: return info = gtask.command_info(cmd) self.CallTipSetBackground("#f4f4d1") self.CallTipSetForeground("BLACK") self.CallTipShow(pos, info['usage'] + '\n\n' + info['description']) elif event.GetKeyCode() in [wx.WXK_UP, wx.WXK_DOWN] and \ not self.AutoCompActive(): # Command history using up and down if len(self.cmdbuffer) < 1: return self.DocumentEnd() # move through command history list index values if event.GetKeyCode() == wx.WXK_UP: self.cmdindex = self.cmdindex - 1 if event.GetKeyCode() == wx.WXK_DOWN: self.cmdindex = self.cmdindex + 1 if self.cmdindex < 0: self.cmdindex = 0 if self.cmdindex > len(self.cmdbuffer) - 1: self.cmdindex = len(self.cmdbuffer) - 1 try: txt = self.cmdbuffer[self.cmdindex] except: txt = '' # clear current line and insert command history self.DelLineLeft() self.DelLineRight() pos = self.GetCurrentPos() self.InsertText(pos,txt) self.LineEnd() self.parent.parent.statusbar.SetStatusText('') elif event.GetKeyCode() == wx.WXK_RETURN and \ self.AutoCompActive() == False: # run command on line when <return> is pressed if self.parent.GetName() == "ModelerDialog": self.parent.OnOk(None) return # find the command to run line = self.GetCurLine()[0].strip() if len(line) == 0: return # parse command into list try: cmd = utils.split(str(line)) except UnicodeError: cmd = utils.split(utils.EncodeString((line))) cmd = map(utils.DecodeString, cmd) # send the command list to the processor if cmd[0] in ('r.mapcalc', 'r3.mapcalc') and len(cmd) == 1: self.parent.parent.OnMapCalculator(event = None, cmd = cmd) else: self.parent.RunCmd(cmd) # add command to history & clean prompt self.UpdateCmdHistory(cmd) self.OnCmdErase(None) self.parent.parent.statusbar.SetStatusText('') elif event.GetKeyCode() == wx.WXK_SPACE: items = self.GetTextLeft().split() if len(items) == 1: cmd = items[0].strip() if cmd in globalvar.grassCmd['all'] and \ cmd != 'r.mapcalc' and \ (not self.cmdDesc or cmd != self.cmdDesc.get_name()): try: self.cmdDesc = gtask.parse_interface(cmd) except IOError: self.cmdDesc = None event.Skip() else: event.Skip()
def test_parse_comma_list(self): val = u.split('2GOZ|1|A|A|3,2GOZ|1|A|A|8') ans = [tuple(["2GOZ|1|A|A|3", "2GOZ|1|A|A|3"]), tuple(["2GOZ|1|A|A|8", "2GOZ|1|A|A|8"])] self.assertEquals(ans, val)
def test_parse_range(self): val = u.split('2GOZ|1|A|A|3:2GOZ|1|A|A|5') ans = [tuple(['2GOZ|1|A|A|3', '2GOZ|1|A|A|5'])] self.assertEquals(ans, val)
def Shrink(self, t): """Shrink the profile to be <= *t*.""" for key, values in self.entries.iteritems(): self.entries[key], r = utils.split(values, t) del self.entries['slope'][-1]
def test_parse_single_unit(self): val = u.split('2GOZ|1|A|A|3') ans = [tuple(["2GOZ|1|A|A|3", "2GOZ|1|A|A|3"])] self.assertEquals(ans, val)
def open_file(self): path = askopenfilename() history = main.open_transaction_file(path) self.accounts = split(history, [lambda t: t.dest]) self.fill_account_pane() self.draw_balance_plot()
def __init__(self): super(PathFinder, self).__init__() basedir, filename = utils.split(utils.abspath(__file__)) self.config_name = os.path.join(basedir, "workspace_path_config.json") self.logger = logging.getLogger("main_log.path_finder") self.path_dict = self.read_in_path_config(self.config_name)
# -*- coding: utf-8 -*- """ Created on Thu May 05 16:18:21 2016 @author: Zero """ import pandas as pd import numpy as np import matplotlib.pyplot as plt import utils as u from sklearn import tree from sklearn.externals import joblib import handle if __name__ == "__main__": labels=['churn','appetency','upselling'] label='appetency' df=handle.loadData() df_all=df df_fill=handle.handle_missing(df,label,is_common=True,replace=0) y_=df_fill[label] X_=df_fill.drop(labels,axis='columns') # X=X[u.gbc(X,y,X.columns)] X_train, X_test, y_train, y_test=u.split(X_,y_,test_size=0.1) df_split=handle.split_data(pd.concat([X_train, y_train],axis='columns'),label) # comment start # divide the data into two classes #comment end