def insert_new_moving_piece(self, template): if template == DUCK_INDICES: for obs in self.observers: obs.notify(QuackEvent()) part = Part(template, self.column_nr) part.position_index = self.column_nr / 2 - 1 part.rotate(random.randint(0, 3), clockwise=True) self.moving_piece = part
def __init__(self, partsDescriptionDir="PartDescriptionFiles"): self._parts = {} """Parts lilsted by name.""" for f in os.listdir(partsDescriptionDir): f = os.path.join(partsDescriptionDir, f) if not os.path.isfile(f): continue if not f.lower().endswith(".xml"): continue part = Part(f) name = part.getName() if name in self._parts: raise PgmError("Duplicate part names %s" % name) self._parts[part.getName()] = part
def select_parts_by_value(self, val, connection): ''' Return the Part(s) of given value in a list. ''' parts = [] try: cur = connection.cursor() sql = 'SELECT * FROM parts WHERE value=? INTERSECT SELECT * FROM parts WHERE project=?' params = (val, self.name) for row in cur.execute(sql, params): parts.append(Part.new_from_row(row, connection, self)) finally: cur.close() return parts
def select_parts_by_name(self, name, connection): ''' Return the Part(s) of given name. ''' parts = [] try: cur = connection.cursor() #sql = 'SELECT * FROM parts WHERE name=? INTERSECT SELECT * FROM parts WHERE project=?' sql = 'SELECT * FROM parts WHERE name=? AND project=?' params = (name, self.name) for row in cur.execute(sql, params): parts.append(Part.new_from_row(row, connection, self)) finally: cur.close() return parts
def select_parts_by_product(self, prod, connection): ''' Return the Part(s) of given product in a list. ''' parts = [] try: cur = connection.cursor() if prod is None or prod == '' or len(prod) == 0 or prod == 'NULL': sql = 'SELECT * FROM parts WHERE project=? AND product IS NULL' params = (self.name,) else: sql = 'SELECT * FROM parts WHERE product=? AND project=?' params = (prod, self.name,) for row in cur.execute(sql, params): parts.append(Part.new_from_row(row, connection, self)) finally: cur.close() return parts
def set_filename(self): filenames_str = filedialog.askopenfilenames(filetypes=[("*.STL", "*.stl")]) print("filenames_str {0}".format(filenames_str)) print("Adding STL files:") for stl_file in list(filenames_str): print("- {0} ".format(stl_file)) part_name = stl_file.split("/")[-1].replace(".STL", "") if (part_name in self.order.parts.keys()): print("!!!! Warning part already in order !!!!") part = Part(part_name) part.read_mesh(stl_file) part.calc_props() self.order.parts[part_name] = part #print ("filenames {0}".format(filenames_str.splitlist())) self.write_stls()
def neighbours(self, point): neighbours = { (-1, 0): Part(is_empty=True), (-1, -1): Part(is_empty=True), (-1, 1): Part(is_empty=True), (0, -1): Part(is_empty=True), (0, 1): Part(is_empty=True), (1, -1): Part(is_empty=True), (1, 1): Part(is_empty=True), (1, 0): Part(is_empty=True) } row = point[0] col = point[1] if row < 7: if not self.board[row + 1][col].is_empty: neighbours[(1, 0)] = Part(self.board[row + 1][col].is_empty, self.board[row + 1][col].is_white) if col < 7: if not self.board[row + 1][col + 1].is_empty: neighbours[(1, 1)] = Part( self.board[row + 1][col + 1].is_empty, self.board[row + 1][col + 1].is_white) if not self.board[row][col + 1].is_empty: neighbours[(0, 1)] = Part(self.board[row][col + 1].is_empty, self.board[row][col + 1].is_white) if col > 0: if not self.board[row + 1][col - 1].is_empty: neighbours[(1, -1)] = Part( self.board[row + 1][col - 1].is_empty, self.board[row + 1][col - 1].is_white) if not self.board[row][col - 1].is_empty: neighbours[(0, -1)] = Part(self.board[row][col - 1].is_empty, self.board[row][col - 1].is_white) if row > 0: if not self.board[row - 1][col].is_empty: neighbours[(-1, 0)] = Part(self.board[row - 1][col].is_empty, self.board[row - 1][col].is_white) if col < 7: if not self.board[row - 1][col + 1].is_empty: neighbours[(-1, 1)] = Part( self.board[row - 1][col + 1].is_empty, self.board[row - 1][col + 1].is_white) if not self.board[row][col + 1].is_empty: neighbours[(0, 1)] = Part(self.board[row][col + 1].is_empty, self.board[row][col + 1].is_white) if col > 0: if not self.board[row - 1][col - 1].is_empty: neighbours[(-1, -1)] = Part( self.board[row - 1][col - 1].is_empty, self.board[row - 1][col - 1].is_white) if not self.board[row][col - 1].is_empty: neighbours[(0, -1)] = Part(self.board[row][col - 1].is_empty, self.board[row][col - 1].is_white) return neighbours
def __init__(self): self.board = [[Part(is_empty=True) for _ in range(8)] for _ in range(8)] self.white_team_name = '' self.black_team_name = ''
def _initParts(self): gm = ['Alignment._initParts()'] if len(self.parts): for p in self.parts: del(p) self.parts = [] if self.equates: eqSymb = self.equates.keys() eqSymb.sort() eqSymb = string.join(eqSymb, '') else: eqSymb = '' if len(self.sequences) and self.length and self.symbols and self.dim: pass else: gm.append("Can't allocate part.") if not len(self.sequences): gm.append("-no sequences.") elif not self.length: gm.append("-the sequences have no length") elif not self.symbols: gm.append("-no symbols") elif not self.dim: gm.append("-dim not set") raise P4Error(gm) # its all one part if not self.nexusSets or not self.nexusSets.charPartition: aPart = Part() aPart.alignment = self aPart.name = 'all' aPart.lowName = 'all' aPart.dataType = self.dataType aPart.dim = self.dim aPart.symbols = self.symbols aPart.equates = self.equates aPart.nTax = len(self.sequences) aPart.nChar = self.length assert aPart.nChar if 0: print gm[0] print " symbols=%s" % self.symbols aPart.cPart = pf.newPart(len(self.sequences), self.length, eqSymb, self.symbols) if not aPart or not aPart.cPart: gm.append("Failed to get memory for part.") raise P4Error(gm) # Make the equates table verbose = 0 equatesTable = [] if verbose: print "equates is %s" % self.equates print "eqSymb is %s" % eqSymb # the keys print "symbols is %s" % self.symbols for i in range(len(eqSymb)): if verbose: print "%3s: " % eqSymb[i], e = self.equates[eqSymb[i]] if verbose: print "%8s : " % e, for s in self.symbols: if s in e: if verbose: print "%1i" % 1, equatesTable.append('1') else: if verbose: print "%1i" % 0, equatesTable.append('0') if verbose: print '' equatesTable = string.join(equatesTable, '') if verbose: print "\n\nequatesTable:" print equatesTable pf.pokeEquatesTable(aPart.cPart, equatesTable) sList = [] for s in self.sequences: sList.append(s.sequence) if 0: print gm[0] print "sList = %s" % sList print "joined = %s" % string.join(sList, '') pf.pokeSequences(aPart.cPart, string.join(sList, '')) # print "about to makePatterns ..." pf.makePatterns(aPart.cPart) # print "about to setInvar" pf.setGlobalInvarSitesVec(aPart.cPart) # pf.dumpPart(aPart.cPart) self.parts.append(aPart) elif self.nexusSets.charPartition: for cpp in self.nexusSets.charPartition.subsets: # print "Doing subset '%s', mask: %s" % (cpp.name, cpp.mask) # print "About to subsetUsingMask (self length is %i)" % # self.length b = self.subsetUsingMask(cpp.mask) # This very method, but now there are no charPartitions in b. b._initParts() b.parts[0].name = cpp.name b.parts[0].lowName = string.lower(cpp.name) self.parts.append(b.parts[0]) b.parts = [] # so we don't try free-ing it twice
def parse_legacy(obj,mymodel): """ take a loaded legacy dictionary, decides between current filetype and legacy caDNAno 1.0 filetype Parameters ---------- obj: dictionary object generated from a JSON file See Also -------- Examples -------- """ from assembly import Assembly from part import Part my_assembly = Assembly() my_part = Part(my_assembly.createPartID()) vhelixlist = obj["vstrands"] # should rename to name = obj["name"] # placeholder, not really used # create dictionaries (keyed by vstrand #) of # row/col, scaf array, stap array vhToRowCol = {} vhToScaf = {} vhToStap = {} vhNums = [] for helix in vhelixlist: # strand should be helix num = helix["num"] # helix number vhNums.append(num) # keep track of a list of helix numbers row = helix["row"] # slice row for this helix col = helix["col"] # slice column scaf = helix["scaf"] # array of scaffold points stap = helix["stap"] # array of staple pointers vhToRowCol[num] = [row,col] vhToScaf[num] = scaf vhToStap[num] = stap # extract scaffold 5' breakpoints scafBreaks = [] for vh in vhNums: scaf = vhToScaf[vh] for i in range(len(scaf)): base = scaf[i] if (base[1] == -1) & (base[3] != -1): scafBreaks.append([vh, i]) # extract staple 5' breakpoints stapBreaks = [] for vh in vhNums: stap = vhToStap[vh] for i in range(len(stap)): base = stap[i] if (base[1] == -1) & (base[3] != -1): stapBreaks.append([vh, i]) # extract scaffold paths, starting at 5' breakpoints scafPaths = [] for scafBreak in scafBreaks: path = [] [curr_vh, curr_base] = scafBreak [next_vh, next_base] = vhToScaf[curr_vh][curr_base][2:4] while next_base != -1: [row, col] = vsToRowCol[curr_vs] [x, y, z] = getScafCoord(row,col,curr_base) path.append([curr_vs,curr_base,[x, y, z]]) # append midpoint for crossover if (curr_vs != next_vs) & (curr_base == next_base): (x1,y1,z1) = getScafCoord(row,col,curr_base) [nextrow, nextcol] = vsToRowCol[next_vs] (x2,y2,z2) = getScafCoord(nextrow,nextcol,next_base) midxyz = [(x1+x2)/2,(y1+y2)/2,(z1+z2)/2] path.append([curr_vs,curr_base,midxyz]) [curr_vs, curr_base] = [next_vs, next_base] [next_vs, next_base] = vsToScaf[curr_vs][curr_base][2:4] [row, col] = vsToRowCol[curr_vs] [x, y, z] = getScafCoord(row,col,curr_base) path.append([curr_vs,curr_base,[x, y, z]]) scafPaths.append(path) # extract staple paths, starting at 5' breakpoints stapPaths = [] for stapBreak in stapBreaks: path = [] [curr_vs, curr_base] = stapBreak [next_vs, next_base] = vsToStap[curr_vs][curr_base][2:4] while next_base != -1: [row, col] = vsToRowCol[curr_vs] [x, y, z] = getStapCoord(row,col,curr_base) path.append([curr_vs,curr_base, [x, y, z]]) # append midpoint for crossover if (curr_vs != next_vs) & (curr_base == next_base): (x1,y1,z1) = getStapCoord(row,col,curr_base) [nextrow, nextcol] = vsToRowCol[next_vs] (x2,y2,z2) = getStapCoord(nextrow,nextcol,next_base) midxyz = [(x1+x2)/2,(y1+y2)/2,(z1+z2)/2] path.append([curr_vs,curr_base,midxyz]) [curr_vs, curr_base] = [next_vs, next_base] [next_vs, next_base] = vsToStap[curr_vs][curr_base][2:4] [row, col] = vsToRowCol[curr_vs] [x, y, z] = getStapCoord(row,col,curr_base) path.append([curr_vs,curr_base, [x, y, z]]) stapPaths.append(path) my_part.VHelix = vhelixlist my_assembly.addPart(my_part) return my_parts, my_assembly
def createPart(self, loc): return Part(self.surface, loc, self.color, self.size)
def __init__(self, parameters): super().__init__(parameters) self.SUPPORTED_TYPES = {'man', 'woman'} self._set_default_type_if_needed('human') chosen_type = self._get_chosen_type() if chosen_type is None: print('{} hair type not implemented!'.format( self.parameters[EP.TYPE])) elif chosen_type == 'man': self.parts.append( Part(entity=Oval({ EP.LINE_THICKNESS: -1, EP.COLOR: self.parameters[EP.COLOR] }), relative_position=RelativePosition(left=0.0, right=1.0, top=0.0, bottom=0.8))) self.parts.append( Part(entity=Oval({ EP.LINE_THICKNESS: -1, EP.COLOR: self.parameters[EP.COLOR] }), relative_position=RelativePosition(left=0.0, right=0.1, top=0.0, bottom=1.0))) self.parts.append( Part(entity=Oval({ EP.LINE_THICKNESS: -1, EP.COLOR: self.parameters[EP.COLOR] }), relative_position=RelativePosition(left=0.9, right=1.0, top=0.0, bottom=1.0))) elif chosen_type == 'woman': self.parts.append( Part(entity=Oval({ EP.LINE_THICKNESS: -1, EP.COLOR: self.parameters[EP.COLOR] }), relative_position=RelativePosition(left=0.0, right=1.0, top=0.0, bottom=0.2))) self.parts.append( Part(entity=Oval({ EP.LINE_THICKNESS: -1, EP.COLOR: self.parameters[EP.COLOR] }), relative_position=RelativePosition(left=0.0, right=0.1, top=0.0, bottom=1.0))) self.parts.append( Part(entity=Oval({ EP.LINE_THICKNESS: -1, EP.COLOR: self.parameters[EP.COLOR] }), relative_position=RelativePosition(left=0.9, right=1.0, top=0.0, bottom=1.0)))
def test_csv(self): try: self.wspace.create_tables() other_wspace = Workspace('DB Tests 2', os.path.join(os.getcwd(), 'testfixtures', 'dbtests2.sqlite')) other_wspace.create_tables() from product import Product, Listing from part import Part from bom import BOM other_proj = BOM.new_project('other_proj', 'other', '', other_wspace.memory) test_c5_prod = Product('TDK Corporation', 'C1005X5R1V104K', 'general_B11.pdf', 'CAP CER 0.1UF 35V 10% X5R 0402', '0402 (1005 Metric)') test_c5_prod.scrape(other_wspace.memory) # Don't want to add this to the main test DB test_c5 = Part('C5', other_proj, '0.1uF', 'C-USC0402', 'C0402', 'CAPACITOR, American symbol', test_c5_prod) test1_csv = os.path.join(os.getcwd(), 'testfixtures', "test1.csv") test2_csv = os.path.join(os.getcwd(), 'testfixtures', "test2.csv") test3_csv = os.path.join(os.getcwd(), 'testfixtures', "test3.csv") self.wspace.projects = self.wspace.list_projects() assert len(self.wspace.projects) == 0 test1_bom = BOM.new_project('test1', 'Product column, no header', test1_csv, self.wspace.memory) test2_bom = BOM.new_project('test2', 'No product column, no header', test2_csv, self.wspace.memory) test3_bom = BOM.new_project('test3', 'Header row, no PN attribute', test3_csv, self.wspace.memory) self.wspace.projects = self.wspace.list_projects() assert len(self.wspace.projects) == 3 test1_bom.read_from_file(self.wspace.memory) assert len(test1_bom.parts) == 6 test1_c5_query = test1_bom.select_parts_by_name('C5', self.wspace.memory) assert len(test1_c5_query) == 1 test1_c5 = test1_c5_query[0] assert test1_c5.equals(test_c5, True, True, False, True) == True assert test1_c5.equals(test_c5, True, True, True, True) == False assert test1_c5.product.equals(test_c5_prod) test2_bom.read_from_file(self.wspace.memory) assert len(test2_bom.parts) == 6 test2_c5_query = test2_bom.select_parts_by_name('C5', self.wspace.memory) assert len(test2_c5_query) == 1 test2_c5 = test2_c5_query[0] assert test2_c5.project is test2_bom # Check: Attribs, Name, Proj, Prod try: assert test2_c5.equals(test_c5, False, True, False, True) == True except AssertionError: print 'Assertion failed: assert test2_c5.equals(test_c5, False, True, False, True) == True' print 'Reference C5: ', test_c5 print 'Test 1 C5: ', test1_c5 print 'Test 2 C5: ', test2_c5 raise AssertionError assert test2_c5.equals(test_c5, True, True, False, True) == True assert test2_c5.equals(test_c5, True, True, True, True) == False assert test2_c5.product.equals(test_c5_prod) assert test1_bom.parts == test2_bom.parts test3_bom.read_from_file(self.wspace.memory) assert len(test3_bom.parts) == 382 test3_c5_query = test3_bom.select_parts_by_name('C5', self.wspace.memory) assert len(test3_c5_query) == 1 test3_c5 = test3_c5_query[0] #print 'test3_c5 1st check: \n', test3_c5.show() assert test3_c5.equals(test_c5, True, True, False, True) == True assert test3_c5.equals(test_c5, True, True, True, True) == False assert test3_c5.product.equals(test_c5_prod) test3_c11_query = test3_bom.select_parts_by_name('C11', self.wspace.memory) assert len(test3_c11_query) == 1 test3_c11 = test3_c11_query[0] assert test3_c5.project is test3_bom assert test3_c11.product.equals(test_c5_prod) c5_prod_query = Product.select_by_pn('C1005X5R1V104K', self.wspace.memory) assert len(c5_prod_query) == 1 c5_prod = c5_prod_query[0] assert c5_prod.equals(test_c5_prod) test3_c63_query = test3_bom.select_parts_by_name('C63', self.wspace.memory) # Mystery 'None' printout here... huh? try: #print 'Trying assertion: assert len(test3_c63_query) == 1' assert len(test3_c63_query) == 1 except AssertionError: print 'Assertion failed: assert len(test3_c63_query) == 1' print 'len(test3_c63_query): ', len(test3_c63_query) for p in test3_c63_query: print 'test3_c63_query contains this part: ', p raise AssertionError test3_c63 = test3_c63_query[0] #print '\ntest3_c63: \n', test3_c63.show() VOLT attribute is being preserved OK # C63 has a VOLT = 25V attribute, which C5 does not. # Therefore, C63's product should remain unset. try: #print 'Trying assertion: assert test3_c63.product is None' assert test3_c63.product is None except AssertionError: print 'Assertion failed: assert test3_c63.product is None' attribs = [] print test3_c63 print test3_c5 cur = self.wspace.memory.cursor() cur.execute('SELECT * FROM part_attributes') #params = ('C63', 'test3',) #cur.execute('SELECT * FROM part_attributes WHERE part=? AND project=? ORDER BY id', params) for row in cur.fetchall(): attribs.append((row[0], row[1], row[2], row[3], row[4])) print 'ATTRIBUTES TABLE: \n', attribs cur.close() raise AssertionError # Check: Attribs, Name, Proj, Prod assert test3_c5.equals(test3_c63, True, False, True, False) == False assert test3_c5.equals(test3_c63, False, False, False, False) == True assert test3_c5.equals(test3_c63, False, False, False, True) == False assert test3_c5.equals(test3_c63, False, False, True, False) == True try: assert test3_c63.equals(test3_c5, True, False, True, False) == False except AssertionError: print 'Assertion failed: assert test3_c63.equals(test3_c5, True, False, True, False) == False' print test3_c63 print test3_c5 raise AssertionError assert test3_c63.equals(test3_c5, False, False, True, False) == False assert test3_c63.equals(test3_c5, True, False, False, False) == False assert test3_c63.equals(test3_c5, False, False, False, False) == False other_wspace.memory.close() del other_wspace finally: self.wspace.memory.close()
def read_from_file(self, connection): ''' Parses a BOM spreadsheet in CSV format and writes it to the DB. Passing an open connection to this method is HIGHLY recommended. ''' # TODO: product_updater calls are hardcoded to always check wspace print "BOM.read_from_file" # Clear self.parts del self.parts[:] with open(self.input, 'rb') as f: sniffer = csv.Sniffer() sniffed_dialect = sniffer.sniff(f.read(1024)) f.seek(0) has_header = sniffer.has_header(f.read(2048)) f.seek(0) reader = csv.reader(f, dialect=sniffed_dialect) if has_header is True: rownum = 0 #print 'Header found in CSV' for row in reader: if rownum == 0: header = row # Process column names from header index = 0 name_col = -1 val_col = -1 dev_col = -1 pkg_col = -1 desc_col = -1 prod_col = -1 bom_attribs = {} # Key = column index, value = name of attribute for column in header: col = column.lower() if 'part' in col or 'name' in col: name_col = index elif 'value' in col: val_col = index elif 'device' in col: dev_col = index elif 'package' in col: pkg_col = index elif 'description' in col: desc_col = index elif 'partno' in col or 'partnum' in col or 'part number' in col or 'part#' in col or ('pn' in col and 'vendor' not in col): prod_col = index else: bom_attribs[index] = column index += 1 if name_col == -1: raise KeyError("Component name column not found in header!") else: print 'Row: ', row #row_attribs = {} row_attribs = dict({}) row_attribs.clear() for attrib in bom_attribs.items(): if len(row[attrib[0]]) > 0: row_attribs[attrib[1]] = row[attrib[0]] #print 'Row attribs: ', row_attribs # No need for a "name column found" check here, # as a KeyError would already have been raised above new_name = row[name_col] if val_col != -1: new_val = row[val_col] else: new_val = "" if dev_col != -1: new_dev = row[dev_col] else: new_dev = "" if pkg_col != -1: new_pkg = row[pkg_col] else: new_pkg = "" if desc_col != -1: new_desc = row[desc_col] else: new_desc = "" if prod_col != -1: prod = Product.select_by_pn(row[prod_col], connection) if prod is not None and len(prod) > 0: new_prod = prod[0] else: new_prod = None else: new_prod = None part = Part(new_name, self, new_val, new_dev, new_pkg, new_desc, new_prod, row_attribs) part.product_updater(connection) if part.product is None: self.parts.append([part.name, part.value, '']) else: self.parts.append([part.name, part.value, part.product.manufacturer_pn]) rownum += 1 else: # No column headers for row in reader: print row # Check for optional product column if len(row) == 6: if len(row[5]) > 0: prod = Product.select_by_pn(row[5], connection) if prod is not None and len(prod) > 0: part = Part(row[0], self, row[1], row[2], row[3], row[4], prod[0]) else: new_prod = Product('NULL', row[5]) new_prod.insert(connection) new_prod.scrape(connection) part = Part(row[0], self, row[1], row[2], row[3], row[4], new_prod) else: part = Part(row[0], self, row[1], row[2], row[3], row[4], None) else: part = Part(row[0], self, row[1], row[2], row[3], row[4], None) #print 'Got part from CSV: ' #part.show() part.product_updater(connection) if part.product is None: self.parts.append([part.name, part.value, '']) else: self.parts.append([part.name, part.value, part.product.manufacturer_pn])
def __init__(self, parameters): super().__init__(parameters) self.SUPPORTED_TYPES = {'human', 'cat'} self._set_default_type_if_needed('human') chosen_type = self._get_chosen_type() self._change_default_aspect_ratio_to_value( Face.ASPECT_RATIO[chosen_type]) if chosen_type is None: print('{} face not implemented'.format(self.parameters[EP.TYPE])) elif chosen_type in ['human', 'cat']: # TODO move 'cat' into separate branch self.parameters[EP.TYPE] = 'human' brow_line = (math.sqrt(2)) / (2 + math.sqrt(2)) nose_line = (1 + math.sqrt(2)) / (2 + math.sqrt(2)) eyes_bottom_line = brow_line + 0.2 / Eye.ASPECT_RATIO[ self.parameters[EP.TYPE]] mouth_height = 0.12 mouth_center_height = (1.5 + math.sqrt(2)) / (2 + math.sqrt(2)) self.parts.append( Part(entity=Oval({EP.ASPECT_RATIO: 'fit'}), relative_position=RelativePosition(left=0.1, top=0, right=0.9, bottom=1))) self.parts.append( Part(entity=Eye({ EP.SHAPE: Eye.STANDARD_SHAPE, EP.TYPE: self.parameters[EP.TYPE] }), relative_position=RelativePosition( left=0.2, top=brow_line, right=0.4, bottom=eyes_bottom_line))) self.parts.append( Part(entity=Eye({ EP.SHAPE: Eye.STANDARD_SHAPE, EP.TYPE: self.parameters[EP.TYPE] }), relative_position=RelativePosition( left=0.6, top=brow_line, right=0.8, bottom=eyes_bottom_line))) self.parts.append( Part(entity=Nose({EP.TYPE: self.parameters[EP.TYPE]}), relative_position=RelativePosition(left=0.4, right=0.6, top=eyes_bottom_line, bottom=nose_line))) self.parts.append( Part(entity=Mouth({EP.TYPE: self.parameters[EP.TYPE]}), relative_position=RelativePosition( left=0.32, right=0.68, top=mouth_center_height - mouth_height / 2, bottom=mouth_center_height + mouth_height / 2))) self.parts.append( Part(entity=Ear({EP.TYPE: self.parameters[EP.TYPE]}), relative_position=RelativePosition(left=0, right=0.1, top=brow_line, bottom=nose_line))) self.parts.append( Part(entity=Ear({EP.TYPE: self.parameters[EP.TYPE]}), relative_position=RelativePosition(left=0.9, right=1.0, top=brow_line, bottom=nose_line)))
def read_from_file(self, connection): ''' Parses a BOM spreadsheet in CSV format and writes it to the DB. Passing an open connection to this method is HIGHLY recommended. ''' # TODO: product_updater calls are hardcoded to always check wspace print "BOM.read_from_file" # Clear self.parts del self.parts[:] with open(self.input, 'rb') as f: sniffer = csv.Sniffer() sniffed_dialect = sniffer.sniff(f.read(1024)) f.seek(0) has_header = sniffer.has_header(f.read(2048)) f.seek(0) reader = csv.reader(f, dialect=sniffed_dialect) if has_header is True: rownum = 0 #print 'Header found in CSV' for row in reader: if rownum == 0: header = row # Process column names from header index = 0 name_col = -1 val_col = -1 dev_col = -1 pkg_col = -1 desc_col = -1 prod_col = -1 bom_attribs = { } # Key = column index, value = name of attribute for column in header: col = column.lower() if 'part' in col or 'name' in col: name_col = index elif 'value' in col: val_col = index elif 'device' in col: dev_col = index elif 'package' in col: pkg_col = index elif 'description' in col: desc_col = index elif 'partno' in col or 'partnum' in col or 'part number' in col or 'part#' in col or ( 'pn' in col and 'vendor' not in col): prod_col = index else: bom_attribs[index] = column index += 1 if name_col == -1: raise KeyError( "Component name column not found in header!") else: print 'Row: ', row #row_attribs = {} row_attribs = dict({}) row_attribs.clear() for attrib in bom_attribs.items(): if len(row[attrib[0]]) > 0: row_attribs[attrib[1]] = row[attrib[0]] #print 'Row attribs: ', row_attribs # No need for a "name column found" check here, # as a KeyError would already have been raised above new_name = row[name_col] if val_col != -1: new_val = row[val_col] else: new_val = "" if dev_col != -1: new_dev = row[dev_col] else: new_dev = "" if pkg_col != -1: new_pkg = row[pkg_col] else: new_pkg = "" if desc_col != -1: new_desc = row[desc_col] else: new_desc = "" if prod_col != -1: prod = Product.select_by_pn( row[prod_col], connection) if prod is not None and len(prod) > 0: new_prod = prod[0] else: new_prod = None else: new_prod = None part = Part(new_name, self, new_val, new_dev, new_pkg, new_desc, new_prod, row_attribs) part.product_updater(connection) if part.product is None: self.parts.append([part.name, part.value, '']) else: self.parts.append([ part.name, part.value, part.product.manufacturer_pn ]) rownum += 1 else: # No column headers for row in reader: print row # Check for optional product column if len(row) == 6: if len(row[5]) > 0: prod = Product.select_by_pn(row[5], connection) if prod is not None and len(prod) > 0: part = Part(row[0], self, row[1], row[2], row[3], row[4], prod[0]) else: new_prod = Product('NULL', row[5]) new_prod.insert(connection) new_prod.scrape(connection) part = Part(row[0], self, row[1], row[2], row[3], row[4], new_prod) else: part = Part(row[0], self, row[1], row[2], row[3], row[4], None) else: part = Part(row[0], self, row[1], row[2], row[3], row[4], None) #print 'Got part from CSV: ' #part.show() part.product_updater(connection) if part.product is None: self.parts.append([part.name, part.value, '']) else: self.parts.append([ part.name, part.value, part.product.manufacturer_pn ])
def load_item(json_string, object_dec, json_filename): s = json_string.read() jdict = json.loads(s, object_hook=object_dec.decode_component) jdict.__dict__['json_id'] = json_filename item = Part(**(jdict.to_dict())) return item
def test_db(self): #try: self.wspace.create_tables() from product import Product, Listing from part import Part from bom import BOM tables = [] cur = self.wspace.memory.cursor() for row in cur.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"): tables.append(row[0]) cur.close() assert 'products' in tables assert 'parts' in tables assert 'part_attributes' in tables assert 'listings' in tables assert 'projects' in tables assert 'pricebreaks' in tables assert 'preferred_listings' in tables self.test_BOM = BOM.new_project('dbtests', 'Databse Unit tests', '', self.wspace.memory) self.wspace.projects = self.wspace.list_projects() assert len(self.wspace.projects) == 1 assert 'dbtests' in self.wspace.projects self.test_part = Part('C1', self.test_BOM, '1uF', 'C-USC0603', 'C0603', 'CAPACITOR, American symbol', self.test_product, self.part_attribs) self.test_product.insert(self.wspace.memory) self.test_listing_ct.insert(self.wspace.memory) self.test_listing_tr.insert(self.wspace.memory) self.test_listing_dr.insert(self.wspace.memory) preferred_listing = self.test_product.get_preferred_listing(self.test_BOM, self.wspace.memory) assert preferred_listing is None self.test_product.set_preferred_listing(self.test_BOM, self.test_listing_ct, self.wspace.memory) preferred_listing = self.test_product.get_preferred_listing(self.test_BOM, self.wspace.memory) assert preferred_listing is not None assert preferred_listing.key() == self.test_listing_ct.key() self.test_product.set_preferred_listing(self.test_BOM, self.test_listing_dr, self.wspace.memory) preferred_listing = self.test_product.get_preferred_listing(self.test_BOM, self.wspace.memory) assert preferred_listing is not None assert preferred_listing.key() == self.test_listing_dr.key() self.test_part.insert(self.wspace.memory) # Product.select_by_pn fetches listings for the product, and fetch_listings fetches the price dicts ret_products = Product.select_by_pn(self.test_product.manufacturer_pn, self.wspace.memory) assert self.test_product.is_in_db(self.wspace.memory) # Should only return one result: assert len(ret_products) == 1 # Product.equals() calls Listing.equals() as part of the check assert len (Listing.select_by_vendor_pn(self.test_listing_ct.vendor_pn, self.wspace.memory)) == 1 assert len (Listing.select_by_vendor_pn(self.test_listing_tr.vendor_pn, self.wspace.memory)) == 1 assert len (Listing.select_by_vendor_pn(self.test_listing_dr.vendor_pn, self.wspace.memory)) == 1 assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 3 assert self.test_product.equals(ret_products[0]) assert self.test_part.has_attribute('TOL', self.wspace.memory) assert self.test_part.has_attribute('VOLT', self.wspace.memory) assert self.test_part.has_attribute('TC', self.wspace.memory) ret_parts = Part.select_by_name(self.test_part.name, self.wspace.memory, self.test_BOM) try: assert len(ret_parts) == 1 except AssertionError: print 'Assertion failed: assert len(ret_parts) == 1' print 'len(ret_parts): ', len(ret_parts) raise AssertionError assert self.test_part.equals(ret_parts[0]) assert self.test_part.is_in_db(self.wspace.memory) ret_parts = self.test_BOM.select_parts_by_name(self.test_part.name, self.wspace.memory) assert len(ret_parts) == 1 assert self.test_part.equals(ret_parts[0]) self.test_part.delete(self.wspace.memory) assert len(self.test_BOM.select_parts_by_name(self.test_part.name, self.wspace.memory)) == 0 assert self.test_part.is_in_db(self.wspace.memory) == False assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 3 self.test_listing_ct.delete(self.wspace.memory) assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 2 assert len (Listing.select_by_vendor_pn(self.test_listing_ct.vendor_pn, self.wspace.memory)) == 0 self.test_listing_tr.delete(self.wspace.memory) assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 1 assert len (Listing.select_by_vendor_pn(self.test_listing_tr.vendor_pn, self.wspace.memory)) == 0 self.test_listing_dr.delete(self.wspace.memory) assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 0 assert len (Listing.select_by_vendor_pn(self.test_listing_dr.vendor_pn, self.wspace.memory)) == 0 assert len(Product.select_by_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 1 self.test_product.delete(self.wspace.memory) assert len(Product.select_by_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 0 assert self.test_product.is_in_db(self.wspace.memory) == False self.test_BOM.delete(self.wspace.memory) self.wspace.projects = self.wspace.list_projects() assert len(self.wspace.projects) == 0 assert 'dbtests' not in self.wspace.projects tables = [] cur = self.wspace.memory.cursor() cur.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name") for row in cur.fetchall(): tables.append(row[0]) cur.close() assert 'products' in tables assert 'parts' in tables assert 'part_attributes' in tables assert 'listings' in tables assert 'projects' in tables assert 'pricebreaks' in tables assert 'preferred_listings' in tables assert 'dbtests' not in self.wspace.projects
) ap.add_argument( '-c', '--cid-mode', action='store_true', help= 'Specify videos by cid rather than avid. Argument "avid" will be regarded as cid. Use space to seperate multiple cids.' ) args = ap.parse_args() if not args.cid_mode: for avid in args.avid: if avid.startswith('av'): avid = avid[2:] logging.info('Downloading post av%s' % avid) post = Post(avid) post.showInfo() if args.download: if args.part == -1: logging.info('Downloading post: %s (%d parts in total)' % (post.title, len(post.parts))) for p in post.parts: p.download() logging.info('Download finished!') else: post.parts[args.part - 1].download() else: if args.download: for cid in args.avid: p = Part(cid, str(cid), str(cid), None) p.download()
class EagleManagerTestCase(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) from product import * from part import Part from bom import BOM self.wspace = Workspace('DB Tests', os.path.join(os.getcwd(), 'testfixtures', 'dbtests.sqlite')) self.test_product = Product('TDK Corporation', 'C1608X5R1E105K', 'general_B11.pdf', 'CAP CER 1UF 25V 10% X5R 0603', '0603 (1608 Metric)') self.prices_ct = dict({10 : 0.09000, 100 : 0.04280, 250 : 0.03600, 500 : 0.03016, 1000 : 0.02475}) self.prices_tr = dict({4000 : 0.01935, 8000 : 0.01800, 12000 : 0.01710, 280000 : 0.01620, 100000 : 0.01227}) self.prices_dr = dict({10 : 0.09000, 100 : 0.04280, 250 : 0.03600, 500 : 0.03016, 1000 : 0.02475}) self.test_listing_ct = Listing(VENDOR_DK, '445-5146-1-ND', 'C1608X5R1E105K', self.prices_ct, 566342, 'Cut Tape (CT)', 0, 'Capacitors', 'Ceramic', 'C') self.test_listing_tr = Listing(VENDOR_DK, '445-5146-2-ND', 'C1608X5R1E105K', self.prices_tr, 552000, 'Tape & Reel (TR)', 0, 'Capacitors', 'Ceramic', 'C') self.test_listing_dr = Listing(VENDOR_DK, '445-5146-6-ND', 'C1608X5R1E105K', self.prices_dr, 566342, 'Digi-Reel', 7, 'Capacitors', 'Ceramic', 'C') self.test_product.listings[self.test_listing_ct.key()] = self.test_listing_ct self.test_product.listings[self.test_listing_tr.key()] = self.test_listing_tr self.test_product.listings[self.test_listing_dr.key()] = self.test_listing_dr self.part_attribs = dict({'TOL' : '10%', 'VOLT' : '25V', 'TC' : 'X5R'}) def test_db(self): #try: self.wspace.create_tables() from product import Product, Listing from part import Part from bom import BOM tables = [] cur = self.wspace.memory.cursor() for row in cur.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"): tables.append(row[0]) cur.close() assert 'products' in tables assert 'parts' in tables assert 'part_attributes' in tables assert 'listings' in tables assert 'projects' in tables assert 'pricebreaks' in tables assert 'preferred_listings' in tables self.test_BOM = BOM.new_project('dbtests', 'Databse Unit tests', '', self.wspace.memory) self.wspace.projects = self.wspace.list_projects() assert len(self.wspace.projects) == 1 assert 'dbtests' in self.wspace.projects self.test_part = Part('C1', self.test_BOM, '1uF', 'C-USC0603', 'C0603', 'CAPACITOR, American symbol', self.test_product, self.part_attribs) self.test_product.insert(self.wspace.memory) self.test_listing_ct.insert(self.wspace.memory) self.test_listing_tr.insert(self.wspace.memory) self.test_listing_dr.insert(self.wspace.memory) preferred_listing = self.test_product.get_preferred_listing(self.test_BOM, self.wspace.memory) assert preferred_listing is None self.test_product.set_preferred_listing(self.test_BOM, self.test_listing_ct, self.wspace.memory) preferred_listing = self.test_product.get_preferred_listing(self.test_BOM, self.wspace.memory) assert preferred_listing is not None assert preferred_listing.key() == self.test_listing_ct.key() self.test_product.set_preferred_listing(self.test_BOM, self.test_listing_dr, self.wspace.memory) preferred_listing = self.test_product.get_preferred_listing(self.test_BOM, self.wspace.memory) assert preferred_listing is not None assert preferred_listing.key() == self.test_listing_dr.key() self.test_part.insert(self.wspace.memory) # Product.select_by_pn fetches listings for the product, and fetch_listings fetches the price dicts ret_products = Product.select_by_pn(self.test_product.manufacturer_pn, self.wspace.memory) assert self.test_product.is_in_db(self.wspace.memory) # Should only return one result: assert len(ret_products) == 1 # Product.equals() calls Listing.equals() as part of the check assert len (Listing.select_by_vendor_pn(self.test_listing_ct.vendor_pn, self.wspace.memory)) == 1 assert len (Listing.select_by_vendor_pn(self.test_listing_tr.vendor_pn, self.wspace.memory)) == 1 assert len (Listing.select_by_vendor_pn(self.test_listing_dr.vendor_pn, self.wspace.memory)) == 1 assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 3 assert self.test_product.equals(ret_products[0]) assert self.test_part.has_attribute('TOL', self.wspace.memory) assert self.test_part.has_attribute('VOLT', self.wspace.memory) assert self.test_part.has_attribute('TC', self.wspace.memory) ret_parts = Part.select_by_name(self.test_part.name, self.wspace.memory, self.test_BOM) try: assert len(ret_parts) == 1 except AssertionError: print 'Assertion failed: assert len(ret_parts) == 1' print 'len(ret_parts): ', len(ret_parts) raise AssertionError assert self.test_part.equals(ret_parts[0]) assert self.test_part.is_in_db(self.wspace.memory) ret_parts = self.test_BOM.select_parts_by_name(self.test_part.name, self.wspace.memory) assert len(ret_parts) == 1 assert self.test_part.equals(ret_parts[0]) self.test_part.delete(self.wspace.memory) assert len(self.test_BOM.select_parts_by_name(self.test_part.name, self.wspace.memory)) == 0 assert self.test_part.is_in_db(self.wspace.memory) == False assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 3 self.test_listing_ct.delete(self.wspace.memory) assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 2 assert len (Listing.select_by_vendor_pn(self.test_listing_ct.vendor_pn, self.wspace.memory)) == 0 self.test_listing_tr.delete(self.wspace.memory) assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 1 assert len (Listing.select_by_vendor_pn(self.test_listing_tr.vendor_pn, self.wspace.memory)) == 0 self.test_listing_dr.delete(self.wspace.memory) assert len (Listing.select_by_manufacturer_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 0 assert len (Listing.select_by_vendor_pn(self.test_listing_dr.vendor_pn, self.wspace.memory)) == 0 assert len(Product.select_by_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 1 self.test_product.delete(self.wspace.memory) assert len(Product.select_by_pn(self.test_product.manufacturer_pn, self.wspace.memory)) == 0 assert self.test_product.is_in_db(self.wspace.memory) == False self.test_BOM.delete(self.wspace.memory) self.wspace.projects = self.wspace.list_projects() assert len(self.wspace.projects) == 0 assert 'dbtests' not in self.wspace.projects tables = [] cur = self.wspace.memory.cursor() cur.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name") for row in cur.fetchall(): tables.append(row[0]) cur.close() assert 'products' in tables assert 'parts' in tables assert 'part_attributes' in tables assert 'listings' in tables assert 'projects' in tables assert 'pricebreaks' in tables assert 'preferred_listings' in tables assert 'dbtests' not in self.wspace.projects def test_csv(self): try: self.wspace.create_tables() other_wspace = Workspace('DB Tests 2', os.path.join(os.getcwd(), 'testfixtures', 'dbtests2.sqlite')) other_wspace.create_tables() from product import Product, Listing from part import Part from bom import BOM other_proj = BOM.new_project('other_proj', 'other', '', other_wspace.memory) test_c5_prod = Product('TDK Corporation', 'C1005X5R1V104K', 'general_B11.pdf', 'CAP CER 0.1UF 35V 10% X5R 0402', '0402 (1005 Metric)') test_c5_prod.scrape(other_wspace.memory) # Don't want to add this to the main test DB test_c5 = Part('C5', other_proj, '0.1uF', 'C-USC0402', 'C0402', 'CAPACITOR, American symbol', test_c5_prod) test1_csv = os.path.join(os.getcwd(), 'testfixtures', "test1.csv") test2_csv = os.path.join(os.getcwd(), 'testfixtures', "test2.csv") test3_csv = os.path.join(os.getcwd(), 'testfixtures', "test3.csv") self.wspace.projects = self.wspace.list_projects() assert len(self.wspace.projects) == 0 test1_bom = BOM.new_project('test1', 'Product column, no header', test1_csv, self.wspace.memory) test2_bom = BOM.new_project('test2', 'No product column, no header', test2_csv, self.wspace.memory) test3_bom = BOM.new_project('test3', 'Header row, no PN attribute', test3_csv, self.wspace.memory) self.wspace.projects = self.wspace.list_projects() assert len(self.wspace.projects) == 3 test1_bom.read_from_file(self.wspace.memory) assert len(test1_bom.parts) == 6 test1_c5_query = test1_bom.select_parts_by_name('C5', self.wspace.memory) assert len(test1_c5_query) == 1 test1_c5 = test1_c5_query[0] assert test1_c5.equals(test_c5, True, True, False, True) == True assert test1_c5.equals(test_c5, True, True, True, True) == False assert test1_c5.product.equals(test_c5_prod) test2_bom.read_from_file(self.wspace.memory) assert len(test2_bom.parts) == 6 test2_c5_query = test2_bom.select_parts_by_name('C5', self.wspace.memory) assert len(test2_c5_query) == 1 test2_c5 = test2_c5_query[0] assert test2_c5.project is test2_bom # Check: Attribs, Name, Proj, Prod try: assert test2_c5.equals(test_c5, False, True, False, True) == True except AssertionError: print 'Assertion failed: assert test2_c5.equals(test_c5, False, True, False, True) == True' print 'Reference C5: ', test_c5 print 'Test 1 C5: ', test1_c5 print 'Test 2 C5: ', test2_c5 raise AssertionError assert test2_c5.equals(test_c5, True, True, False, True) == True assert test2_c5.equals(test_c5, True, True, True, True) == False assert test2_c5.product.equals(test_c5_prod) assert test1_bom.parts == test2_bom.parts test3_bom.read_from_file(self.wspace.memory) assert len(test3_bom.parts) == 382 test3_c5_query = test3_bom.select_parts_by_name('C5', self.wspace.memory) assert len(test3_c5_query) == 1 test3_c5 = test3_c5_query[0] #print 'test3_c5 1st check: \n', test3_c5.show() assert test3_c5.equals(test_c5, True, True, False, True) == True assert test3_c5.equals(test_c5, True, True, True, True) == False assert test3_c5.product.equals(test_c5_prod) test3_c11_query = test3_bom.select_parts_by_name('C11', self.wspace.memory) assert len(test3_c11_query) == 1 test3_c11 = test3_c11_query[0] assert test3_c5.project is test3_bom assert test3_c11.product.equals(test_c5_prod) c5_prod_query = Product.select_by_pn('C1005X5R1V104K', self.wspace.memory) assert len(c5_prod_query) == 1 c5_prod = c5_prod_query[0] assert c5_prod.equals(test_c5_prod) test3_c63_query = test3_bom.select_parts_by_name('C63', self.wspace.memory) # Mystery 'None' printout here... huh? try: #print 'Trying assertion: assert len(test3_c63_query) == 1' assert len(test3_c63_query) == 1 except AssertionError: print 'Assertion failed: assert len(test3_c63_query) == 1' print 'len(test3_c63_query): ', len(test3_c63_query) for p in test3_c63_query: print 'test3_c63_query contains this part: ', p raise AssertionError test3_c63 = test3_c63_query[0] #print '\ntest3_c63: \n', test3_c63.show() VOLT attribute is being preserved OK # C63 has a VOLT = 25V attribute, which C5 does not. # Therefore, C63's product should remain unset. try: #print 'Trying assertion: assert test3_c63.product is None' assert test3_c63.product is None except AssertionError: print 'Assertion failed: assert test3_c63.product is None' attribs = [] print test3_c63 print test3_c5 cur = self.wspace.memory.cursor() cur.execute('SELECT * FROM part_attributes') #params = ('C63', 'test3',) #cur.execute('SELECT * FROM part_attributes WHERE part=? AND project=? ORDER BY id', params) for row in cur.fetchall(): attribs.append((row[0], row[1], row[2], row[3], row[4])) print 'ATTRIBUTES TABLE: \n', attribs cur.close() raise AssertionError # Check: Attribs, Name, Proj, Prod assert test3_c5.equals(test3_c63, True, False, True, False) == False assert test3_c5.equals(test3_c63, False, False, False, False) == True assert test3_c5.equals(test3_c63, False, False, False, True) == False assert test3_c5.equals(test3_c63, False, False, True, False) == True try: assert test3_c63.equals(test3_c5, True, False, True, False) == False except AssertionError: print 'Assertion failed: assert test3_c63.equals(test3_c5, True, False, True, False) == False' print test3_c63 print test3_c5 raise AssertionError assert test3_c63.equals(test3_c5, False, False, True, False) == False assert test3_c63.equals(test3_c5, True, False, False, False) == False assert test3_c63.equals(test3_c5, False, False, False, False) == False other_wspace.memory.close() del other_wspace finally: self.wspace.memory.close() #os.remove(os.path.join(os.getcwd(), 'testfixtures', 'dbtests2.sqlite')) def tearDown(self): unittest.TestCase.tearDown(self) del self.wspace
def addToHead(self, location): self.body.addBegin((Part(self.surface, location, self.color, self.size)))
def addPartToRegistry(self, partName, partType, partVolume): part = Part(partName, partType, partVolume) self.registry[partName] = part
def __init__(self, backup, position, step): self.array = [] self.training_step = step + 1 self.init = False self.given = False # If this is an agents backup store default values for all parts if backup: for k in range(0, 15): self.array.append(Part(0, 0)) # Otherwise spawn agent in a random position else: # Initialise the body and head of the agent random = randint(-30, 30) # The back of the agent is the heaviest so that it does ot topple forwards if self.training_step == 1: self.array.append( Part(random + randint(-89, 89), 50, 23.44, position)) self.array.append(Part(random, 50, 11.72)) self.array.append(Part(random + randint(-89, 89), 50, 8.24)) else: self.array.append(Part(0, 50, 23.44, position)) self.array.append(Part(0, 50, 11.72)) self.array.append(Part(0, 50, 8.24)) # Load the head and bodies image files self.array[0].load_image("image_resources/body.png") self.array[1].load_image("image_resources/body.png") self.array[2].load_image("image_resources/head.png") # Initialise the legs of the agent for i in range(0, 2): random_values = [randint(0, 360), randint(-90, 90)] # The legs on each side of the agent should have matching rotations # The weight of each leg is 0.66 * 2 (Since each leg is made of 2 parts) self.array.append(Part(random_values[0], 12, 0.66)) self.array.append(Part(random_values[0], 12, 0.66)) self.array.append(Part(random_values[0], 12, 0.66)) self.array.append( Part(random_values[0] + random_values[1], 12, 0.66)) self.array.append( Part(random_values[0] + random_values[1], 12, 0.66)) self.array.append( Part(random_values[0] + random_values[1], 12, 0.66)) # Load the image files for the legs for j in range(3, 15): self.array[j].load_image("image_resources/leg.png") # Set each parts position and constraints self.set_positions(position) self.set_constraints()
def __get_part(self, component): max_variation = len(self.assets[component]) variation = random.randint(0, max_variation - 1) selection = self.assets[component][variation] part = Part(selection) return part