def main(): """Parse command-line arguments and print flattened list""" parser = argparse.ArgumentParser() parser.add_argument("-l", help="List to be flattened") args = parser.parse_args() l = ast.literal_eval(args.l) print flatten(l)
def test_happy_path(self): self.assertEqual( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], flatten([[1, 2, 3],[[4, [[5]]], [6, 7]], 8, [[[9], [10]]]])) self.assertEqual( [1, -2, 3, -4, 5, -6, 7, -8, 9, -10], flatten([[1, -2, 3],[[-4, [[5]]], [-6, 7]], -8, [[[9], [-10]]]]))
def test_happy_path(self): self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], flatten([[1, 2, 3], [[4, [[5]]], [6, 7]], 8, [[[9], [10]]]])) self.assertEqual([1, -2, 3, -4, 5, -6, 7, -8, 9, -10], flatten([[1, -2, 3], [[-4, [[5]]], [-6, 7]], -8, [[[9], [-10]]]]))
def test_flatten(self): TestCase = namedtuple('TestCase', 'input expected_output') test_cases = [ TestCase( input=[1, 2, 3], expected_output=[1, 2, 3], ), TestCase( input=[[[[[1]]]], 2], expected_output=[1, 2], ), TestCase( input=[[1], [2], [3]], expected_output=[1, 2, 3], ), TestCase( input=[[1, 2, 3], [4], [], [5, [6, [7, 8]]]], expected_output=[1, 2, 3, 4, 5, 6, 7, 8], ), ] for test_case in test_cases: is_correct = False try: flat = flatten(np.array(test_case.input).shape) result = flat.flatten(np.array(test_case.input)) is_correct = np.array_equal(result, np.array(test_case.expected_output)) finally: if not is_correct: flatten(np.array(test_case.input).shape).flatten(np.array(test_case.input), debug=True) self.assertTrue(is_correct)
def dummyDir(path, currDir): hash = random.getrandbits(16) workDir = currDir + "\\" + str(hash) #print workDir shutil.copytree(path, workDir) os.chdir(workDir) #move to working dir #print os.getcwd() flatten.flatten() return os.getcwd()
def test__wrong_input_type(self): sample = 0 expected_result = None assert flatten(sample) == expected_result sample = 'test' expected_result = None assert flatten(sample) == expected_result
def test_zonebounds(): """py.test for zonebounds""" data = ZoneBounds_Data dct, bounds = data.dct1, data.bounds1 result = eplusgeom.zonebounds(dct, "first") fresult, fbounds = flatten(result), flatten(bounds) for v1, v2 in zip(fresult, fbounds): assert almostequals(v1, v2, err=5) dct, bounds = data.dct2, data.bounds2 result = eplusgeom.zonebounds(dct, "first") fresult, fbounds = flatten(result), flatten(bounds) for v1, v2 in zip(fresult, fbounds): assert almostequals(v1, v2, err=5)
def won(self): from flatten import flatten mines = [tile for tile in flatten(self.minefield) if tile.mine] if mines == [x for x in mines if x.flag]: mines_flagged = True else: mines_flagged = False non_mines = [x for x in flatten(self.minefield) if not x.mine] if non_mines == [x for x in non_mines if x.explored]: non_mines_explored = True else: non_mines_explored = False return mines_flagged and non_mines_explored
def test_no_non_lists(self): """ Tests that non-lists cannot be passed as initial parameter. """ with self.assertRaises(TypeError): flatten(1) flatten("hi") flatten({"1": 2}) flatten((1, 2))
def init(): pev = [element for element in flatten(plot_elements.values())] if len(pev) == 1: pev = (pev[0], ) else: pev = tuple(pev) return pev
def test_chat_match_alternative(self): search_str = 'Zebra' res = { 'count': 1, 'similarity': 1, 'value': { 'subject': ('eels', 'Zebra Moray'), 'keywords': (('eel', 'snake-like'), ('zebra', 'strip', 'moray')), 'phrase': ('Zebra Moray', ), 'info': ({ 'name': 'Diet', 'value': 'Sea urchins, mollusks, and crustaceans.' }, ), 'isLeaf': True }, 'lastCount': 1, 'lastSimilarity': 1, 'matching': ('zebra', ), 'certainty': 2 } thisChat = Chat(flatten(resources), ['fishes', 'nothingthere']) self.assertEqual(thisChat.match(search_str), res)
def unpack_exemptions_line(jsonline): # we are aiming to build the following table. # etag | company_number | exemption_type | from | to | kind | links_self # --------------------------------------------------------------------------------------- # 1we4 | OC123456 | type_1 | 03.2009 | 06.2010 | exemptions | resource_link # 1we4 | OC123456 | type_2 | 01.2010 | 05.2012 | exemptions | resource_link # 1we4 | OC123456 | type_2 | 04.2013 | 02.2015 | exemptions | resource_link # 34rf | OC456789 | type_3 | 07.2008 | 05.2009 | exemptions | resource_link root = jsonline.pop("company_number") data = jsonline.pop("data") exemptions = data.pop("exemptions") for k, v in exemptions.items(): type_ = v["exemption_type"] for date_dic in v["items"]: # the generator will contain dictionaries which will map to # a pg line like above. yield { "company_number": root, **flatten(data), "exemption_type": type_, **date_dic }
def detect_pods(self): pods = {} test = {} # Process project.yml file and create required Pods with open('project.yml', "r") as file: project = yaml.full_load(file) for name, info in project['targets'].items(): # We don't want podspecs for Test targets if 'Test' in name: continue category = info['templateAttributes']['layer'] if 'dependencies' in info: test[name] = [dependencies['target'] for dependencies in info['dependencies']] deps = [InternalDependency(dependencies['target']) for dependencies in info['dependencies']] else: deps = [] target = Target(name, self.version, category, deps) # Manually adding 3rd party libraries # - Think of a better way to handle this (JP) pods[target.name] = target self.pods = pods self.sorted_dependency_pods = flatten(test)
def test_arbitrary(): """ :return: """ assert flatten.flatten([[2, 3], [[3, 4], 5], [[[5]]], 7]) == [2, 3, 3, 4, 5, 5, 7]
def boom(self): from flatten import flatten mines = [tile for tile in flatten(self.minefield) if tile.mine] for x in mines: if x.explored: return True return False
def expand_generators(s): from flatten import flatten from numpy import nan # for eval t = eval("[" + s + "]") t = flatten(t) s = repr(t).strip("[]") return s
def getDataset(self): df = pd.read_csv(self.carrerasPath) habilidades = flatten(df['habilidades'].to_numpy()) del df['habilidades'] df = pd.concat([df, habilidades], axis=1, join='inner') #df.to_html("out.html") return df
def test_flatten_ints_and_chars(): inp = [ 'a', 'b', [1, 2, 3], ['c', 'd', ['e', 'f', ['g', 'h']]], [4, [5, 6, [7, [8]]]] ] expected = ['a', 'b', 1, 2, 3, 'c', 'd', 'e', 'f', 'g', 'h', 4, 5, 6, 7, 8] assert list(flatten(inp)) == expected
def _process_resource(obj): """ Flatten and process FHIR resources of the indicated types. """ obj_type = type(obj) assert dict == obj_type # flatten the JSON, convert time strings to datetimes flattened_obj = flatten(obj) flattened_obj = _convert_datetimes(flattened_obj) # read the resource type and process accordingly result = None if _STR_RESOURCE_TYPE in flattened_obj: rt = obj[_STR_RESOURCE_TYPE] if 'Patient' == rt: result = _process_patient(flattened_obj) elif 'Observation' == rt: result = _process_observation(flattened_obj) elif 'Procedure' == rt: result = _process_procedure(flattened_obj) elif 'Condition' == rt: result = _process_condition(flattened_obj) elif 'MedicationStatement' == rt: result = _process_medication_statement(flattened_obj) elif 'MedicationOrder' == rt: result = _process_medication_order(flattened_obj) elif 'MedicationRequest' == rt: result = _process_medication_request(flattened_obj) elif 'MedicationAdministration' == rt: result = _process_medication_administration(flattened_obj) return result
def _process_datetime(obj): """ Process a FHIR datetime resource. """ _KEY_RESULT = 'result' assert dict == type(obj) # flatten the JSON and convert the timestring flattened_dt = flatten(obj) flattened_dt = _convert_datetimes(flattened_dt) # add 'date_time' field for time sorting if _KEY_RESULT in obj: dt = obj[_KEY_RESULT] flattened_dt[KEY_DATE_TIME] = dt if KEY_NAME in flattened_dt: flattened_dt[KEY_CQL_FEATURE] = flattened_dt[KEY_NAME] if _TRACE: _dump_dict(flattened_dt, 'Flattened dateTime resource: ') return flattened_dt
def feature_extractor(image_path, options=None): """Extracts a set of features (described in config file) from an image. Args: image_path: the path to the image options: the configuration file settings. In this case the settings should contain relevant image processing feature options. Return: an array of features which depending on the config options """ features = [] image = imread(image_path) if 'grey_required' in options: grey_image = rgb2grey(image) # GLCM features if 'glcm' in options: glcm_config = options['glcm'] glcm_features = glcm.glcm_features(grey_image, glcm_config['modes']) features.append(glcm_features) # ORB features if 'orb' in options: orb_config = options['orb'] orb_extractor = ORB(downscale=orb_config['downscale'], n_scales=orb_config['n_scales'], n_keypoints=orb_config['n_keypoints'], fast_n=orb_config['fast_n'], fast_threshold=orb_config['fast_threshold'], harris_k=orb_config['harris_k']) orb_extractor.detect_and_extract(grey_image) # TODO add these to config system features.append(orb_extractor.keypoints.tolist()) # features.append(orb_extractor.scales.tolist()) # features.append(orb_extractor.orientations.tolist()) # features.append(orb_extractor.responses.tolist()) # features.append(orb_extractor.descriptors.tolist()) if 'kmeans' in options: k_image = np.array(image, dtype=np.float64) / 255 w, h, d = original_shape = tuple(k_image.shape) assert d == 3 image_array = np.reshape(k_image, (w * h, d)) kmeans = KMeans( n_clusters=options['kmeans']['clusters']).fit(image_array) features.append(kmeans.cluster_centers_.tolist()) return list(flatten.flatten(features))
def merge_json_files(path): data = [] for file in os.listdir(path): with open(path + '/' + file, encoding='utf-8') as f: json_object = json.load(f) for row in json_object: data.append(flatten(row)) return data
def test_flatten(self): lists = [list_generator() for i in range(5)] ans = _("The flatten version of {} is {} and you returned {}.") for i in range(len(lists)): stu_ans = flatten.flatten(lists[i]) corr_ans = corr.flatten(lists[i]) self.assertEqual(corr_ans, stu_ans, ans.format(lists[i], corr_ans, stu_ans))
def getGradient(self,cost,x): # based on the current x, update the cost sess=self.sess self.assign_x(x) var_grad = tf.gradients(cost,self.var) vg=sess.run(var_grad, feed_dict=self.feed) c=sess.run(cost, feed_dict=self.feed) g,shape=flatten.flatten(vg) return c,np.array(g)
def test_flatten_various_levels_different_contant(): inp = [ 1, 2, [3, 4], [5, [6, 7]], [8, [9, [10]]], [11, [12, 13], [14, [15, 16, [17, 18, [19, 20]]]]] ] expected = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ] assert list(flatten(inp)) == expected
def permutations(s, depth): def p(s, depth, prefix): if not s or not depth: return [prefix] else: return [p(s[:i] + s[i+1:], depth - 1, prefix + c) for i, c in enumerate(s)] return flatten(p(s, depth, ""))
def finalize(): # Replace $(foo) for options by config.variableSyntax format: for v in mk.__vars_opt: mk.__vars_opt[v] = config.variableSyntax % v # evaluate variables: finalEvaluation(outputVarsOnly=0) # eliminate references: utils.__refEval = 1 finalEvaluation() # evaluate options default values: finalizeOptions() # delete pseudo targets now: pseudos = [ t for t in mk.targets if mk.targets[t].pseudo ] for t in pseudos: del mk.targets[t] # remove unused conditions: purgeUnusedConditions() # purge conditional variables that have same value for all conditions # and make variables that are empty: reeval = purgeConstantCondVars() if purgeEmptyMakeVars(): reeval = 1 if reeval: finalEvaluation() # purge unused options, cond vars and make vars: while purgeUnusedOptsVars(): finalEvaluation() # eliminate duplicates in cond vars: if eliminateDuplicateCondVars(): finalEvaluation() if mk.vars['FORMAT_SUPPORTS_CONDITIONS'] != '1' and \ mk.vars['FORMAT_SUPPORTS_CONFIGURATIONS'] == '1': import flatten flatten.flatten() # replace \$ with $: replaceEscapeSequences()
def init(): pev = [element for element in flatten(plot_elements.values())] #returns one dimensional list containing the elements the user wants for the plot #ensure the return line returns an iterable of artists if len(pev) == 1: pev = (pev[0],) else: pev = tuple(pev) return pev
def test_nested_empty_dict(self): d = { "a": 1, "b": {}, "date of": { "d": {}, "birth": 7 } } self.assertEqual(flatten(d), {'a': 1, 'b': None, 'date of.d': None, "date of.birth": 7})
def test_empty(self): a = [] ans = _( "The flatten version of {} is {} and you returned {}. \n You should watch you behaviour in case of empty lists." ) for i in range(len(a)): stu_ans = flatten.flatten(a) corr_ans = corr.flatten(a) self.assertEqual(corr_ans, stu_ans, ans.format(a, corr_ans, stu_ans))
def test_flatten_mixed_with_empty(self): expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] make_flat = [ 1, [ 2, [], [], [[[[]]]], [3, [4, [5, [6], 7], 8, [], [9], 10]], 11, [12], [], [[[[[[[[13]]]]]]]] ] ] self.assertEquals(expected, flatten(make_flat))
def test_flatten_shot(self): dictionary = { "a": 5, "b": 6, "c": { "f": 9, } } dictionary_flat = {"a": 5, "b": 6, "c.f": 9} self.assertEqual(flatten.flatten(dictionary), dictionary_flat)
def AMScore(self): allAns = [] for trial in range(self.nTrial): allAns.append(self.outputlist[trial]['Answers']) anslist = flatten(allAns) ok = anslist.count('recogOKposOK') ws = anslist.count('recogOKposWrong') fa = anslist.count('falseAlarm') amscore = ok /(ws + fa) return amscore
def test_flatten_strip_duplicate_in_tree(self): resources2 = [ { "name": "fishes", "keywords": ("fish seafood animal", ), "phrase": ("It looks like you're looking for fishes.", ), "options": ("What kind of fishes are you looking for?", ), "children": ( { "name": "deep sea fishes", "keywords": ("deep bottom-dwellers", ), "phrase": ("We have several fishes that swim deep.", ), "children": [{ "name": "Sword Fish", # strip out this duplicate "keywords": ("Sword Fish", ), "info": ({ "name": "Unusual characteristics", "value": "sword" }, ), "isLeaf": True, "phrase": ("Sword Fish", ), }] }, ) }, ] res = [{ 'subject': ('fishes', ), 'keywords': (('fish', 'seafood', 'animal'), ), 'phrase': ("It looks like you're looking for fishes.", ), 'options': ('What kind of fishes are you looking for?', ), 'children': ('We have several fishes that swim deep.', ) }, { 'subject': ('fishes', 'deep sea fishes'), 'keywords': (('fish', 'seafood', 'animal'), ('deep', 'bottom-dwellers')), 'phrase': ('We have several fishes that swim deep.', ), 'children': ('Sword Fish', ) }, { 'subject': ('fishes', 'deep sea fishes', 'Sword Fish'), 'keywords': (('fish', 'seafood', 'animal'), ('deep', 'bottom-dwellers'), ('sword', )), 'phrase': ('Sword Fish', ), 'info': ({ 'name': 'Unusual characteristics', 'value': 'sword' }, ), 'isLeaf': True }] self.assertEqual(flatten(resources2, 0), res)
def test_flatten(): assert flatten.flatten(1) == [1] assert flatten.flatten((1, )) == [1] assert flatten.flatten([1]) == [1] assert flatten.flatten({1}) == [1] assert flatten.flatten(None) == [None] assert flatten.flatten([1, [2, (3, )], [[[[[[4, 5]]]]], 6]]) == [1, 2, 3, 4, 5, 6] assert flatten.flatten({"dict": 1}) == [{"dict": 1}]
def __init__(self,cost,feed,var_t,sess): self.cost=cost # cost function to be minimized self.feed=feed # feed variables self.var=[] # list of dictionary containing the trainable variables w=[] # this part will convert the variables(tensors) into a list of real numbers(x) self.sess=sess for tl in var_t: for t in tl: self.var.append(tl[t]) w.append(tl[t].eval(session=sess)) self.x,self.shape=np.array(flatten.flatten(w)) # shape of the variables used to update the values of the
def flatten(list_of_lists): # new = [] # for i in list_of_lists: # if isinstance(i, (list, tuple)): # new.extend(flatten(i)) # else: # new.append(i) # return new return sum(([i] if not isinstance(i, (list, tuple)) else flatten(i) for i in list_of_lists), [])
def make_pancakes(input_dir, output_dir): templates = TemplateDirectory(input_dir) for template_name in template_names(input_dir): print "Writing %s" % template_name pancake = flatten(template_name, templates) outfile = os.path.join(output_dir, template_name) try: os.makedirs(os.path.dirname(outfile)) except OSError: # Already exists. pass with open(outfile, 'w') as fp: fp.write(pancake)
def make_pancakes(template_dirs, output_dir): templates = TemplateDirectories(template_dirs) for template_name in set(templates.list()): outfile = os.path.join(output_dir, template_name) print "'%s' -> '%s'" % (template_name, outfile) pancake = flatten(template_name, templates) try: os.makedirs(os.path.dirname(outfile)) except OSError: # Already exists. pass with codecs.open(outfile, 'w', 'utf-8') as fp: fp.write(pancake)
def extract_locations(x): locations = flatten( [ subject_l(x), branch_l(x), gen_l(x, "fil_inst", "FIL_INST", "city", "state", "country_code"), gen_l(x, "fin_inst", "FIN_INST", "city", "state", "country"), ] ) for l in locations: l.update({"locus": make_locus(l, ["city", "state", "country"])}) return filter(lambda x: x["locus"] != "", locations)
def gen_l(x, name, field, city, state, country): if field in x: return map( lambda sub: { "locus_actor": name, "resolution": "city", "city": s_lower(sub[city]), "state": s_lower(sub[state]), "country": s_lower(sub[country]), }, flatten([x[field]]), ) else: return []
def printboard(board,selected=[],dests=[]): from string import maketrans import re import math from flatten import flatten # if selected != '': # print bin(selected)[2:].zfill(32) #add player piece locations together to make string (0 no pieces, 1 player 1's piece, 2 player 2's piece) board_string = str(int(bin(board[0])[2:].zfill(32)) + 2 * int(bin(board[1])[2:].zfill(32)) + 4 * int(bin(board[2])[2:].zfill(32))).zfill(32) selected = flatten([selected]) if selected != []: board_list = list(board_string) for i in range(0,len(selected)): board_list[len(board_list)-int(math.log(selected[i],2))-1] = '3' board_string = "".join(board_list) if dests != []: board_list = list(board_string) for i in range(0,len(dests)): board_list[len(board_list)-int(math.log(dests[i],2))-1] = '4' board_string = "".join(board_list) #add underscores to represent unplayable squares board_string = re.sub('(.{0})', '\\1|', board_string, 4, re.DOTALL) board_string = board_string[:8] + re.sub('(.{1})', '\\1|', board_string[8:], 4, re.DOTALL) board_string = board_string[:16] + re.sub('(.{0})', '\\1|', board_string[16:], 4, re.DOTALL) board_string = board_string[:24] + re.sub('(.{1})', '\\1|', board_string[24:], 4, re.DOTALL) board_string = board_string[:32] + re.sub('(.{0})', '\\1|', board_string[32:], 4, re.DOTALL) board_string = board_string[:40] + re.sub('(.{1})', '\\1|', board_string[40:], 4, re.DOTALL) board_string = board_string[:48] + re.sub('(.{0})', '\\1|', board_string[48:], 4, re.DOTALL) board_string = board_string[:56] + re.sub('(.{1})', '\\1|', board_string[56:], 4, re.DOTALL) #add a space between every adjacent square in each row board_string = re.sub('(.{1})', '\\1 ', board_string, 0, re.DOTALL) #add newlines every 8 squares to create the 2D representation board_string = re.sub('(.{16})', '\\1\n', board_string, 0, re.DOTALL) #change representation of player pieces and empty squares board_string = board_string.translate(maketrans('0123456','_HOPDKK'),"'[]") print board_string
def branch_l(x): if "FIN_INST" in x: if isinstance(x["FIN_INST"], dict): x["FIN_INST"] = [x["FIN_INST"]] adds = [a["branch"] for a in x["FIN_INST"]] adds = filter(lambda x: x != (), adds) adds = flatten(adds) return map( lambda sub: { "locus_actor": "fin_inst_branch", "resolution": "city", "city": s_lower(sub["city"]), "state": s_lower(sub["state"]), "country": s_lower(sub["country_code"]), }, adds, ) else: return []
def subject_l(x): if "SUBJECTS" in x: if isinstance(x["SUBJECTS"], dict): x["SUBJECTS"] = [x["SUBJECTS"]] adds = [a["subject_address"] for a in x["SUBJECTS"]] adds = filter(lambda x: x != (), adds) adds = flatten(adds) return map( lambda sub: { "locus_actor": "subject", "resolution": "city", "city": s_lower(sub["city"]), "state": s_lower(sub["state"]), "country": s_lower(sub["country_code"]), }, adds, ) else: return []
def replace_type(items, spec, loader, found): """ Go through and replace types in the 'spec' mapping""" items = copy.deepcopy(items) if isinstance(items, dict): # recursively check these fields for types to replace if "type" in items and items["type"] in ("record", "enum"): if items.get("name"): if items["name"] in found: return items["name"] else: found.add(items["name"]) for n in ("type", "items", "fields"): if n in items: items[n] = replace_type(items[n], spec, loader, found) if isinstance(items[n], list): items[n] = flatten(items[n]) return items elif isinstance(items, list): # recursively transform list return [replace_type(i, spec, loader, found) for i in items] elif isinstance(items, basestring): # found a string which is a symbol corresponding to a type. replace_with = None if items in loader.vocab: # If it's a vocabulary term, first expand it to its fully qualified URI items = loader.vocab[items] if items in spec: # Look up in specialization map replace_with = spec[items] if replace_with: return replace_type(replace_with, spec, loader, found) return items
if position > i: continue else: print("Impossible de retourner en arrière !") except ValueError as e: print("Nombre invalide :",go) elif goon == "y": try: z=parse(phrase,verbose=True) arbre=treemaker(z,phrase) except KeyboardInterrupt as e: print("Parsing interrompu...\n") continue if arbre[0]: print(evaluation.writetree(flatten(arbre))) else: print("Échec du parsing") del z print() else: if goon: print("Commande inconnue :",goon) continue else: logging.info("phrase numéro "+str(i)+" en cours de traitement. Longueur : "+str(len(phrase))) z=parse(phrase) arbre=treemaker(z,phrase) if arbre[0]:
import upfront from flatten import flatten txt = open('a.txt', 'r').read() lst = txt.split('--------------------') lst.pop(0) lst1 = [block.splitlines()[1:] for block in lst] from flatten import flatten lst2 = [x.split(',') for x in flatten(lst1)] pts = [[float(p) for p in pt] for pt in lst2] nn = 0 outer = [] for i, item in enumerate(lst1): inner = [] for j in range(len(item)): inner.append(nn) nn += 1 outer.append(inner) upfront.saveupfver1((pts, outer), outfile='b.up1')
def markov_transition(self, v=None, stochastic=False): """ Creates the transition matrix for workforce sizes and states of demand. """ if v == None: v = np.asarray([np.zeros(self.grid.size)]*self.states.size) v = compute_fixed_point(self.bellman_operator, v, verbose=0, max_iter=30) new_wf = self.next_workforce(v, stochastic=stochastic) pll, plh, phl, phh = self.transition.flatten() if stochastic: bottom = 0 top = 2 incoming = np.asarray([[(new_wf[state-1][y] - y)*(new_wf[state-1][y] > y) for y in self.grid] for state in self.states], dtype=int) inc_index_0 = [[i for y_1 in xrange(int(bottom*y), int(top*y + 1))] for i, y in enumerate(incoming[0]) if abs(y) >.1] inc_col_0 = [[i + y_1 for y_1 in xrange(int(bottom*y), int(top*y + 1))] for i, y in enumerate(incoming[0]) if abs(y) >.1] inc_val_0 = [[1./(int(top*y+1)-int(bottom*y)) if y_1!=y else 1./(int(top*y+1)-int(bottom*y)) - 1 for y_1 in xrange(int(bottom*y), int(top*y + 1))] for i, y in enumerate(incoming[0]) if abs(y) >.1] inc_index_1 = [[i for y_1 in xrange(int(bottom*y), int(top*y + 1))] for i, y in enumerate(incoming[1]) if abs(y) >.1] inc_col_1 = [[i + y_1 for y_1 in xrange(int(bottom*y), int(top*y + 1))] for i, y in enumerate(incoming[1]) if abs(y) >.1] inc_val_1 = [[1./(int(top*y+1)-int(bottom*y)) if y_1!=y else 1./(int(top*y+1)-int(bottom*y)) - 1 for y_1 in xrange(int(bottom*y), int(top*y + 1))] for i, y in enumerate(incoming[1]) if abs(y) >.1] inc_index_0 = np.asarray(flatten(inc_index_0)) inc_index_1 = np.asarray(flatten(inc_index_1)) inc_val_0 = np.asarray(flatten(inc_val_0)) inc_val_1 = np.asarray(flatten(inc_val_1)) inc_col_0 = np.asarray(flatten(inc_col_0)) inc_col_1 = np.asarray(flatten(inc_col_1)) incoming_ind = np.hstack((inc_index_0, inc_index_0, self.grid.size + inc_index_1, self.grid.size + inc_index_1)) incoming_col = np.hstack((inc_col_0, self.grid.size + inc_col_0, inc_col_1, self.grid.size + inc_col_1)) incoming_val = np.hstack((pll*inc_val_0, plh*inc_val_0, phl*inc_val_1, phh*inc_val_1)) I = np.hstack((self.grid, self.grid, self.grid.size + self.grid, self.grid.size + self.grid, incoming_ind)) J = np.hstack((new_wf[0], self.grid.size + new_wf[0], new_wf[1], self.grid.size + new_wf[1], incoming_col)) V = np.hstack((pll*np.ones(self.grid.size), plh*np.ones(self.grid.size), phl*np.ones(self.grid.size), phh*np.ones(self.grid.size), incoming_val)) else: I = np.hstack((self.grid, self.grid, self.grid.size + self.grid, self.grid.size + self.grid)) J = np.hstack((new_wf[0], self.grid.size + new_wf[0], new_wf[1], self.grid.size + new_wf[1])) V = np.hstack((pll*np.ones(self.grid.size), plh*np.ones(self.grid.size), phl*np.ones(self.grid.size), phh*np.ones(self.grid.size))) A = coo_matrix((V,(I,J)),shape=(self.states.size*self.grid.size, self.states.size*self.grid.size)) M = A.tocsr().transpose() return M
def test_array_with_wrong_values(self): self.assertRaises(ValueError, lambda: flatten([1, "2"])) self.assertRaises(ValueError, lambda: flatten(["2"])) self.assertRaises(ValueError, lambda: flatten([long(2)])) self.assertRaises(ValueError, lambda: flatten([1, "2", []]))
"""n musical Hacker Schoolers are playing one of 7 notes, each on a different instrument. What are all the possible tones? (example: ["A", "A", "B"] would be one if there n were 3)""" from flatten import flatten from test import test NOTES = ["A", "B", "C"] def tones(player_count, tone=[]): if not player_count: return " ".join(tone) else: ts = [] for n in NOTES: t = tone[:] t.append(n) ts.append(tones(player_count - 1, t)) return ts test(flatten(tones(0)), []) test(flatten(tones(2)), ['A A', 'A B', 'A C', 'B A', 'B B', 'B C', 'C A', 'C B', 'C C'])
if len(l1) != len(l2): return False else: for l1_item in l1: if not l1_item in l2: return False return True def str_chars(cs): return ["".join(p) for p in list(cs)] ## tests s = "abcd" test(sorted(flatten(permutations(s, 2))), sorted(str_chars(itertools.permutations(s, 2)))) s = "abcd" test(sorted(flatten(permutations(s, 4))), sorted(str_chars(itertools.permutations(s, 4)))) s = "abcd" test(sorted(flatten(permutations(s, 1))), sorted(str_chars(itertools.permutations(s, 1)))) s = "abcd" test(sorted(flatten(permutations(s, 0))), sorted(str_chars(itertools.permutations(s, 0)))) s = ""
def test_array_with_nested_empty_arrays(self): self.assertEqual([], flatten([[[[[[]]]]],[[], []]]))
def test_array_with_nested_empty_arrays_and_correct_values(self): self.assertEqual([1, 2, 3, 4, 5, 6], flatten([[[[[[1]]]]],[2, [], [3], [4, 5, 6]]])) self.assertEqual([1, 4, 3, 2, 5, 6], flatten([[[[[[1]]]]],[4, [], [3], [2, 5, 6]]]))
def test_flatten(self): import flatten for test_list, elements in TestFlatten.test_cases: with self.subTest(i=test_list): self.assertEqual(set(flatten.flatten(test_list)), elements)
def job(self, joborder, input_basedir, output_callback, **kwargs): builder = self._init_job(joborder, input_basedir, **kwargs) if self.tool["baseCommand"]: for n, b in enumerate(aslist(self.tool["baseCommand"])): builder.bindings.append({"position": [-1000000, n], "valueFrom": b}) if self.tool.get("arguments"): for i, a in enumerate(self.tool["arguments"]): if isinstance(a, dict): a = copy.copy(a) if a.get("position"): a["position"] = [a["position"], i] else: a["position"] = [0, i] a["do_eval"] = a["valueFrom"] a["valueFrom"] = None builder.bindings.append(a) else: builder.bindings.append({"position": [0, i], "valueFrom": a}) builder.bindings.sort(key=lambda a: a["position"]) reffiles = set((f["path"] for f in builder.files)) j = self.makeJobRunner() j.joborder = builder.job j.stdin = None j.stdout = None j.successCodes = self.tool.get("successCodes") j.temporaryFailCodes = self.tool.get("temporaryFailCodes") j.permanentFailCodes = self.tool.get("permanentFailCodes") j.requirements = self.requirements j.hints = self.hints _logger.debug( "[job %s] initializing from %s%s", id(j), self.tool.get("id", ""), " as part of %s" % kwargs["part_of"] if "part_of" in kwargs else "", ) _logger.debug("[job %s] %s", id(j), json.dumps(joborder, indent=4)) builder.pathmapper = None if self.tool.get("stdin"): j.stdin = builder.do_eval(self.tool["stdin"]) if isinstance(j.stdin, dict) and "ref" in j.stdin: j.stdin = builder.job[j.stdin["ref"][1:]]["path"] reffiles.add(j.stdin) if self.tool.get("stdout"): j.stdout = builder.do_eval(self.tool["stdout"]) if os.path.isabs(j.stdout) or ".." in j.stdout: raise validate.ValidationException("stdout must be a relative path") builder.pathmapper = self.makePathMapper(reffiles, input_basedir, **kwargs) builder.requirements = j.requirements for f in builder.files: f["path"] = builder.pathmapper.mapper(f["path"])[1] _logger.debug("[job %s] command line bindings is %s", id(j), json.dumps(builder.bindings, indent=4)) _logger.debug( "[job %s] path mappings is %s", id(j), json.dumps({p: builder.pathmapper.mapper(p) for p in builder.pathmapper.files()}, indent=4), ) dockerReq, _ = self.get_requirement("DockerRequirement") if dockerReq and kwargs.get("use_container"): out_prefix = kwargs.get("tmp_outdir_prefix") j.outdir = kwargs.get("outdir") or tempfile.mkdtemp(prefix=out_prefix) tmpdir_prefix = kwargs.get("tmpdir_prefix") j.tmpdir = kwargs.get("tmpdir") or tempfile.mkdtemp(prefix=tmpdir_prefix) else: j.outdir = builder.outdir j.tmpdir = builder.tmpdir createFiles, _ = self.get_requirement("CreateFileRequirement") j.generatefiles = {} if createFiles: for t in createFiles["fileDef"]: j.generatefiles[builder.do_eval(t["filename"])] = copy.deepcopy(builder.do_eval(t["fileContent"])) j.environment = {} evr, _ = self.get_requirement("EnvVarRequirement") if evr: for t in evr["envDef"]: j.environment[t["envName"]] = builder.do_eval(t["envValue"]) j.command_line = flatten(map(builder.generate_arg, builder.bindings)) j.pathmapper = builder.pathmapper j.collect_outputs = functools.partial(self.collect_output_ports, self.tool["outputs"], builder) j.output_callback = output_callback yield j
random = raw_input('Should computer play randomly? (y/n)') == 'y' self = raw_input('Should computer play itself? (y/n)') == 'y' while bin(board[0]).count('1') != 0 and bin(board[1]).count('1') != 0: print "Turn: " + str(turn) if self: #make computer play itself if random: sleep(time) print "Computer " + str(player+1) + " is looking at its options." all_pieces = show_all_moves(board,player) sleep(time) print "It's picked a piece..." [piece,dests,jumped] = show_moves(choice(all_pieces),board,player) for i in range(0,len(jumped)): jumped[i] = flatten(jumped[i]) [dest,jumped] = check_dest(choice(dests),dests,jumped,piece,board,1) sleep(time) print "And now it's moved:" else: print "Do AI, dumby" elif player == 0: #player's turn print "Player 1's turn." show = raw_input('Show all possible moves? (y/n)') == 'y' if show: show_all_moves(board,player) [piece,dests,jumped] = show_moves(check_owner(map_input(check_input(input('Choose piece to move: '))),board,player),board,player); for i in range(0,len(jumped)): jumped[i] = flatten(jumped[i]) [dest,jumped] = check_dest(check_free(map_input(check_input(input('Choose piece destination: '))),board),dests,jumped,piece,board); print "Player 1 moved:"
def job(self, joborder, input_basedir, output_callback, **kwargs): builder = self._init_job(joborder, input_basedir, **kwargs) if self.tool["baseCommand"]: for n, b in enumerate(aslist(self.tool["baseCommand"])): builder.bindings.append({ "position": [-1000000, n], "valueFrom": b }) if self.tool.get("arguments"): for i, a in enumerate(self.tool["arguments"]): if isinstance(a, dict): a = copy.copy(a) if a.get("position"): a["position"] = [a["position"], i] else: a["position"] = [0, i] a["do_eval"] = a["valueFrom"] a["valueFrom"] = None builder.bindings.append(a) else: builder.bindings.append({ "position": [0, i], "valueFrom": a }) builder.bindings.sort(key=lambda a: a["position"]) reffiles = set((f["path"] for f in builder.files)) j = self.makeJobRunner() j.builder = builder j.joborder = builder.job j.stdin = None j.stdout = None j.successCodes = self.tool.get("successCodes") j.temporaryFailCodes = self.tool.get("temporaryFailCodes") j.permanentFailCodes = self.tool.get("permanentFailCodes") j.requirements = self.requirements j.hints = self.hints j.name = uniquename(kwargs.get("name", str(id(j)))) _logger.debug("[job %s] initializing from %s%s", j.name, self.tool.get("id", ""), " as part of %s" % kwargs["part_of"] if "part_of" in kwargs else "") _logger.debug("[job %s] %s", j.name, json.dumps(joborder, indent=4)) builder.pathmapper = None if self.tool.get("stdin"): j.stdin = builder.do_eval(self.tool["stdin"]) if isinstance(j.stdin, dict) and "ref" in j.stdin: j.stdin = builder.job[j.stdin["ref"][1:]]["path"] reffiles.add(j.stdin) if self.tool.get("stdout"): j.stdout = builder.do_eval(self.tool["stdout"]) if os.path.isabs(j.stdout) or ".." in j.stdout: raise validate.ValidationException("stdout must be a relative path") builder.pathmapper = self.makePathMapper(reffiles, input_basedir, **kwargs) builder.requirements = j.requirements # map files to assigned path inside a container. We need to also explicitly # walk over input as implicit reassignment doesn't reach everything in builder.bindings def _check_adjust(f): if not f.get("containerfs"): f["path"] = builder.pathmapper.mapper(f["path"])[1] f["containerfs"] = True return f _logger.debug("[job %s] path mappings is %s", j.name, json.dumps({p: builder.pathmapper.mapper(p) for p in builder.pathmapper.files()}, indent=4)) adjustFileObjs(builder.files, _check_adjust) adjustFileObjs(builder.bindings, _check_adjust) _logger.debug("[job %s] command line bindings is %s", j.name, json.dumps(builder.bindings, indent=4)) dockerReq, _ = self.get_requirement("DockerRequirement") if dockerReq and kwargs.get("use_container"): out_prefix = kwargs.get("tmp_outdir_prefix") j.outdir = kwargs.get("outdir") or tempfile.mkdtemp(prefix=out_prefix) tmpdir_prefix = kwargs.get('tmpdir_prefix') j.tmpdir = kwargs.get("tmpdir") or tempfile.mkdtemp(prefix=tmpdir_prefix) else: j.outdir = builder.outdir j.tmpdir = builder.tmpdir createFiles, _ = self.get_requirement("CreateFileRequirement") j.generatefiles = {} if createFiles: for t in createFiles["fileDef"]: j.generatefiles[builder.do_eval(t["filename"])] = copy.deepcopy(builder.do_eval(t["fileContent"])) j.environment = {} evr, _ = self.get_requirement("EnvVarRequirement") if evr: for t in evr["envDef"]: j.environment[t["envName"]] = builder.do_eval(t["envValue"]) shellcmd, _ = self.get_requirement("ShellCommandRequirement") if shellcmd: cmd = [] for b in builder.bindings: arg = builder.generate_arg(b) if b.get("shellQuote", True): arg = [shellescape.quote(a) for a in aslist(arg)] cmd.extend(aslist(arg)) j.command_line = ["/bin/sh", "-c", " ".join(cmd)] else: j.command_line = flatten(map(builder.generate_arg, builder.bindings)) j.pathmapper = builder.pathmapper j.collect_outputs = functools.partial(self.collect_output_ports, self.tool["outputs"], builder) j.output_callback = output_callback yield j