Best Python code snippet using green
abaqus2x.py
Source:abaqus2x.py
1'''2Created on Aug 5, 20133@author: Carl Sandstrom4This Python module is used to parse an Abaqus .inp file and store the data in the same way5as the unv2x module. In order to do so, imaginary elements on the faces of the 3D element are 6created from surfaces. The resulting element type depends on the input type and are defined 7in the method setupElement (for example, the c3d10 element are convertyed to type 118 in UNV 8and has surface element of type 42) 9'''10from FEM import *11from oofemctrlreader import *12class ElementProperties:13 def __init__(self, name, unvType=-1, edgeMask=None, faceMask=None, nnodes=-1, surfaceElementType=-1):14 if (edgeMask != None):15 self.name = name.lower() # string - name as Abaqus elements16 self.unvType = unvType17 self.edgeMask = edgeMask # 2D array expressing node masks for OOFEM's edge 1,2,..., original Abaqus node numbering18 self.faceMask = faceMask # 2D array expressing node masks for OOFEM's face 1,2,..., original Abaqus node numbering19 self.nnodes = nnodes20 self.surfaceElementType = surfaceElementType # Element type on surface21 22class AbaqusParser:23 '''24 classdocs25 '''26 27 def __init__(self,filename):28 self.file = None29 self.filename = filename30 self.FEM = FEM()31 self.ElementConfiguration = self.setupElements()32 33 def mapping(self):34 oofem_elemProp = []35 oofem_elemProp.append(oofem_elementProperties("None", [0], [], []))#leave this line [0] as it is36 oofem_elemProp.append(oofem_elementProperties("RepresentsBoundaryLoad", [],[],[]))#special element representing boundary load37 oofem_elemProp.append(oofem_elementProperties("tet21stokes", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [], [[0,1,2,4,5,6],[0,1,3,4,7,8],[1,2,3,5,8,9],[0,2,3,6,7,9]]))38 return oofem_elemProp39 40 def setupElements(self):41 es = [];42 es.append(ElementProperties("c3d10", 118, [], [[0,1,2,4,5,6],[0,1,3,4,7,8],[1,2,3,5,8,9],[0,2,3,6,7,9]], 10, 42))43 # es.append(ElementProperties("c3d20R", 118, [], [[0,1,2,4,5,6],[0,1,3,4,7,8],[1,2,3,5,8,9],[0,2,3,6,7,9]], 20, 42))44 return es45 46 def _read_multiple_lines(self):47 ''' Read a line from input file distributed across several rows. If a row end with a comma, it resumes on the next line '''48 thisLine = ""49 doContinue = True50 while True:51 start_of_line = self.file.tell()52 newLine = self.file.readline().strip()53 if newLine[0]=='*':54 self.file.seek(start_of_line)55 if thisLine=="":56 thisLine = newLine57 break58 thisLine = thisLine + newLine59 if thisLine[-1] != ',':60 thisLine = thisLine + ','61 62 return thisLine63 def _read_nodes(self):64 self.file.readline()65 while True:66 start_of_line = self.file.tell()67 nodeData = self.file.readline().strip().split(",")68 69 if nodeData[0][0] == '*':70 self.file.seek(start_of_line)71 break72 73 id = int(nodeData[0])74 coords = map(float, nodeData[1:4])75 76 self.FEM.nodes.append(Node(id, coords))77 self.FEM.nnodes = self.FEM.nnodes+1 78 def _read_elements(self):79 elementData = self.file.readline().strip().split(",")80 elementType = elementData[1].split("=")[1]81 82 nnodes = -1;83 84 for es in self.ElementConfiguration:85 if elementType.lower() == es.name:86 nnodes = es.nnodes87 elType = es.unvType88 89 if nnodes == -1:90 print "Element not supported"91 exit(0)92 93 while True:94 while True:95 start_of_line = self.file.tell()96 data = self._read_multiple_lines()97 98 if data[0]=='*': # Reached en of element section99 break100 101 elementSection=[]102 elementSection.extend(data.split(","))103 104 while '' in elementSection:105 elementSection.remove('') 106 107 # Add element to FEM object (id,type,material,color,nnodes,cntvt)108 for i in range (0, len(elementSection), nnodes+1):109 thisElement = elementSection[i:(i+nnodes+1)]110 self.FEM.elems.append(Element(int ( thisElement[0] ), elType, 0, 0, nnodes, map(int, thisElement[1:])))111 if data[0]=='*':112 self.file.seek(start_of_line)113 break114 115 def _read_generate(self):116 outList = []117 start_of_line = self.file.tell()118 generateData = self._read_multiple_lines().split(",")119 doContinue = generateData[0][0] != '*'120 121 while doContinue:122 start = int ( generateData[0] )123 end = int ( generateData[1] )124 if len(generateData) > 2:125 step = int ( generateData[2] )126 else:127 step = 1128 outList.extend( range(start, end+1, step) )129 start_of_line = self.file.tell()130 generateData = self._read_multiple_lines().split(",")131 doContinue = generateData[0][0] != '*'132 self.file.seek(start_of_line)133 return outList134 def _read_set(self):135 setData = self.file.readline().strip().split(",")136 data = []137 setName = setData[1].split("=")[1]138 139 doGenerate = False;140 141 for s in setData:142 if s.lower().strip()=='generate':143 doGenerate = True144 break 145 146 if doGenerate:147 gData = self._read_generate()148 data.extend( gData )149 else:150 dataLine = self._read_multiple_lines().split(',')151 while '' in dataLine:152 dataLine.remove('')153 data = map(int, dataLine)154 155 return setName, data156 157 def _read_element_set(self):158 elementSetName, elements = self._read_set()159 160 ID = len(self.FEM.elemsets)+1;161 eset = Group(ID, elementSetName)162 eset.type = 8163 eset.items.extend(elements)164 eset.nitems = len(eset.items)165 self.FEM.elemsets.append(eset)166 self.FEM.nelemsets = len(self.FEM.elemsets)167 pass168 169 def _read_node_set(self):170 nodeSetName, nodes = self._read_set()171 172 ID = len(self.FEM.nodesets)+1173 nset = Group(ID, nodeSetName)174 nset.type = 7175 nset.items.extend ( nodes )176 nset.nitems = len(nset.items)177 self.FEM.nodesets.append(nset)178 self.FEM.nnodesets = len(self.FEM.nodesets)179 pass180 def _read_surface(self):181 surfaceData = self.file.readline().strip().split(",")182 header = []183 elements = []184 185 for s in surfaceData:186 header.append(s.strip().split('='))187 if header[-1][0].lower() == 'name':188 elementSetName = header[-1][1]189 190 while True:191 start_of_line = self.file.tell()192 lineData = self.file.readline().strip().split(',')193 194 if lineData[0][0]=='*':195 self.file.seek(start_of_line)196 break;197 198 face = int( lineData[1].strip()[1] )199 setName = lineData[0].strip()200 201 # Create virtual element from the set 202 for es in self.FEM.elemsets:203 if es.name == setName:204 # es is the element set containing elements having face "face" facing the surface205 for e in es.items:206 # e is the index of the current element207 eNodes = self.FEM.elems[e-1].cntvt208 eProp = []209 for t in self.ElementConfiguration:210 if t.unvType == self.FEM.elems[e-1].type:211 eProp = t212 break;213 214 surfaceNodes = []215 216 for id in eProp.faceMask[face-1]:217 surfaceNodes.append(eNodes[id])218 219 # Create element220 eID = len(self.FEM.elems)+1221 elements.append(eID)222 self.FEM.elems.append(Element(eID, eProp.surfaceElementType, 0, 0, len( surfaceNodes ), surfaceNodes))223 224 #print es 225 break226 227 # Create element set228 ID = len(self.FEM.elemsets)+1;229 eset = Group(ID, elementSetName)230 eset.type = 8231 eset.items.extend(elements)232 eset.nitems = len(eset.items)233 self.FEM.elemsets.append(eset)234 self.FEM.nelemsets = len(self.FEM.elemsets)235 def parse(self):236 """ parse Abaqus file to fill the FEM data structure"""237 self.file=open(self.filename,'r')238 239 while True:240 start_of_line = self.file.tell()241 line = self.file.readline()242 keyword = line.strip().split(",")[0].lower()243 if keyword == "*heading":244 self.heading = self.file.readline()245 elif keyword == "*node":246 self.file.seek(start_of_line)247 self._read_nodes()248 elif keyword == "*element":249 self.file.seek(start_of_line)250 self._read_elements()251 elif keyword == "*elset":252 self.file.seek(start_of_line)253 self._read_element_set()254 elif keyword == "*nset":255 self.file.seek(start_of_line)256 self._read_node_set()257 elif keyword == "*surface":258 self.file.seek(start_of_line)259 self._read_surface() 260 elif not line:261 break262 263 self.file.close()264 265 # Determine which sets that represents surfaces/edges266 return self.FEM267 ...
recipe-579054.py
Source:recipe-579054.py
1import string2def as_rest_table(data, full=False):3 """4 >>> from report_table import as_rest_table5 >>> data = [('what', 'how', 'who'),6 ... ('lorem', 'that is a long value', 3.1415),7 ... ('ipsum', 89798, 0.2)]8 >>> print as_rest_table(data, full=True)9 +-------+----------------------+--------+10 | what | how | who |11 +=======+======================+========+12 | lorem | that is a long value | 3.1415 |13 +-------+----------------------+--------+14 | ipsum | 89798 | 0.2 |15 +-------+----------------------+--------+16 >>> print as_rest_table(data)17 ===== ==================== ======18 what how who 19 ===== ==================== ======20 lorem that is a long value 3.141521 ipsum 89798 0.222 ===== ==================== ======23 """24 data = data if data else [['No Data']]25 table = []26 # max size of each column27 sizes = map(max, zip(*[[len(str(elt)) for elt in member]28 for member in data]))29 num_elts = len(sizes)30 if full:31 start_of_line = '| '32 vertical_separator = ' | '33 end_of_line = ' |'34 line_marker = '-'35 else:36 start_of_line = ''37 vertical_separator = ' '38 end_of_line = ''39 line_marker = '='40 meta_template = vertical_separator.join(['{{{{{0}:{{{0}}}}}}}'.format(i)41 for i in range(num_elts)])42 template = '{0}{1}{2}'.format(start_of_line,43 meta_template.format(*sizes),44 end_of_line)45 # determine top/bottom borders46 if full:47 to_separator = string.maketrans('| ', '+-')48 else:49 to_separator = string.maketrans('|', '+')50 start_of_line = start_of_line.translate(to_separator)51 vertical_separator = vertical_separator.translate(to_separator)52 end_of_line = end_of_line.translate(to_separator)53 separator = '{0}{1}{2}'.format(start_of_line,54 vertical_separator.join([x*line_marker for x in sizes]),55 end_of_line)56 # determine header separator57 th_separator_tr = string.maketrans('-', '=')58 start_of_line = start_of_line.translate(th_separator_tr)59 line_marker = line_marker.translate(th_separator_tr)60 vertical_separator = vertical_separator.translate(th_separator_tr)61 end_of_line = end_of_line.translate(th_separator_tr)62 th_separator = '{0}{1}{2}'.format(start_of_line,63 vertical_separator.join([x*line_marker for x in sizes]),64 end_of_line)65 # prepare result66 table.append(separator)67 # set table header68 titles = data[0]69 table.append(template.format(*titles))70 table.append(th_separator)71 for d in data[1:-1]:72 table.append(template.format(*d))73 if full:74 table.append(separator)75 table.append(template.format(*data[-1]))76 table.append(separator)...
task_01.py
Source:task_01.py
1def is_palindrome(sentence):2 sentence = str(sentence).lower()3 start_of_line, end_of_line = 0, len(sentence) - 14 while start_of_line < end_of_line:5 if not sentence[start_of_line].isalpha():6 start_of_line += 17 elif not sentence[end_of_line].isalpha():8 end_of_line -= 19 elif sentence[start_of_line] != sentence[end_of_line]:10 return False11 else:12 start_of_line += 113 end_of_line -= 1...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!