Best Python code snippet using avocado_python
parse_gspreadsheet.py
Source:parse_gspreadsheet.py
1# Copyright 2009 Google Inc.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""15parser for feed stored in a google spreadsheet16(note that this is different from other parsers inasmuch as it17expects the caller to pass in the providerID and providerName)18"""19# typical cell20#<entry>21#<id>http://spreadsheets.google.com/feeds/cells/pMY64RHUNSVfKYZKPoVXPBg22#/1/public/basic/R14C13</id>23#<updated>2009-04-28T03:29:56.957Z</updated>24#<category scheme='http://schemas.google.com/spreadsheets/2006' 25#term='http://schemas.google.com/spreadsheets/2006#cell'/>26#<title type='text'>M14</title>27#<content type='text'>ginny@arthur.edu</content>28#<link rel='self' type='application/atom+xml' href='http://spreadsheets.29#google.com/feeds/cells/pMY64RHUNSVfKYZKPoVXPBg/1/public/basic/R14C13'/>30#</entry>31import xml_helpers as xmlh32import re33import urllib34import sys35import time36from datetime import datetime37CURRENT_ROW = None38def parser_error(msg):39 if CURRENT_ROW != None:40 msg = "row "+str(CURRENT_ROW)+": "+msg41 print "parse_gspreadsheet ERROR: "+msg42def raw_recordval(record, key):43 if key in record:44 return str(record[key]).strip()45 return ""46def recordval(record, key):47 return re.sub(r'\s+', ' ', raw_recordval(record, key))48KNOWN_ORGS = {}49def get_dtval(record, field_name):50 val = recordval(record, field_name)51 if val != "" and not re.match(r'\d\d?/\d\d?/\d\d\d\d', val):52 parser_error("bad value in "+field_name+": '"+val+"'-- try MM/DD/YYYY")53 return val54def get_tmval(record, field_name):55 val = recordval(record, field_name)56 if val != "" and not re.match(r'\d?\d:\d\d(:\d\d)?', val):57 parser_error("bad value in "+field_name+": '"+val+"'-- try HH:MM:SS")58 return val59def record_to_fpxml(record):60 fpxml = ""61 fpxml += '<VolunteerOpportunity>'62 fpxml += xmlh.output_val("volunteerOpportunityID", recordval(record, 'oppid'))63 orgname = recordval(record,'SponsoringOrganization')64 if orgname not in KNOWN_ORGS:65 KNOWN_ORGS[orgname] = len(KNOWN_ORGS)66 fpxml += xmlh.output_val("sponsoringOrganizationID", KNOWN_ORGS[orgname])67 title = recordval(record,'OpportunityTitle')68 if title == "":69 parser_error("missing OpportunityTitle-- this field is required.")70 fpxml += xmlh.output_val("title", title)71 fpxml += '<dateTimeDurations>'72 fpxml += '<dateTimeDuration>'73 if ('StartDate' in record and74 recordval(record,'StartDate').find("ongoing") >= 0):75 fpxml += xmlh.output_val('openEnded', 'Yes')76 else:77 fpxml += xmlh.output_val('openEnded', 'No')78 startdtval = get_dtval(record, 'StartDate')79 if startdtval != "":80 fpxml += xmlh.output_val('startDate', startdtval)81 starttmval = get_tmval(record, 'StartTime')82 if starttmval != "":83 fpxml += xmlh.output_val('startTime', starttmval)84 enddtval = get_dtval(record, 'EndDate')85 if enddtval != "":86 fpxml += xmlh.output_val('endDate', enddtval)87 endtmval = get_tmval(record, 'EndTime')88 if endtmval != "":89 fpxml += xmlh.output_val('endTime', endtmval)90 freq = recordval(record,'Frequency').lower()91 if freq == "" or freq.find("once") >= 0:92 fpxml += '<iCalRecurrence/>'93 elif freq.find("daily") >= 0:94 fpxml += '<iCalRecurrence>FREQ=DAILY</iCalRecurrence>'95 elif freq.find("weekly") >= 0:96 fpxml += '<iCalRecurrence>FREQ=WEEKLY</iCalRecurrence>'97 elif freq.find("other") >= 0 and freq.find("week") >= 0:98 fpxml += '<iCalRecurrence>FREQ=WEEKLY;INTERVAL=2</iCalRecurrence>'99 elif freq.find("monthly") >= 0:100 fpxml += '<iCalRecurrence>FREQ=MONTHLY</iCalRecurrence>'101 else:102 parser_error("unsupported frequency: '"+recordval(record,'Frequency')+"'-- skipping")103 fpxml += xmlh.output_val('commitmentHoursPerWeek', recordval(record,'CommitmentHours'))104 fpxml += '</dateTimeDuration>'105 fpxml += '</dateTimeDurations>'106 fpxml += '<locations>'107 fpxml += '<location>'108 if recordval(record,'LocationName').find("virtual") >= 0:109 fpxml += xmlh.output_val('virtual', 'Yes')110 else:111 fpxml += xmlh.output_val('virtual', 'No')112 fpxml += xmlh.output_val('name', recordval(record,'LocationName'))113 fpxml += xmlh.output_val('streetAddress1', recordval(record,'LocationStreet'))114 fpxml += xmlh.output_val('city', recordval(record,'LocationCity'))115 fpxml += xmlh.output_val('region', recordval(record,'LocationProvince'))116 fpxml += xmlh.output_val('postalCode', recordval(record,'LocationPostalCode'))117 fpxml += xmlh.output_val('country', recordval(record,'LocationCountry'))118 fpxml += '</location>'119 fpxml += '</locations>'120 fpxml += xmlh.output_val('paid', recordval(record,'Paid'))121 fpxml += xmlh.output_val('minimumAge', recordval(record,'MinimumAge'))122 # TODO: seniors only, kidfriendly123 fpxml += xmlh.output_val('sexRestrictedTo', recordval(record,'SexRestrictedTo'))124 fpxml += xmlh.output_val('skills', recordval(record,'Skills'))125 fpxml += xmlh.output_val('contactName', recordval(record,'ContactName'))126 fpxml += xmlh.output_val('contactPhone', recordval(record,'ContactPhone'))127 fpxml += xmlh.output_val('contactEmail', recordval(record,'ContactEmail'))128 fpxml += xmlh.output_val('detailURL', recordval(record,'URL'))129 # note: preserve whitespace in description130 fpxml += xmlh.output_val('description', raw_recordval(record,'Description'))131 fpxml += '<lastUpdated olsonTZ="Etc/UTC">'132 fpxml += recordval(record,'LastUpdated') + '</lastUpdated>'133 fpxml += '</VolunteerOpportunity>'134 return fpxml135def cellval(data, row, col):136 key = 'R'+str(row)+'C'+str(col)137 if key not in data:138 return None139 return data[key]140def parse_gspreadsheet(instr, data, updated, progress):141 # look ma, watch me parse XML a zillion times faster!142 #<entry><id>http://spreadsheets.google.com/feeds/cells/pMY64RHUNSVfKYZKPoVXPBg143 #/1/public/basic/R14C15</id><updated>2009-04-28T03:34:21.900Z</updated>144 #<category scheme='http://schemas.google.com/spreadsheets/2006'145 #term='http://schemas.google.com/spreadsheets/2006#cell'/><title type='text'>146 #O14</title><content type='text'>http://www.fake.org/vol.php?id=4</content>147 #<link rel='self' type='application/atom+xml'148 #href='http://spreadsheets.google.com/feeds/cells/pMY64RHUNSVfKYZKPoVXPBg/1/149 #public/basic/R14C15'/></entry>150 regexp = re.compile('<entry>.+?(R(\d+)C(\d+))</id>'+151 '<updated.*?>(.+?)</updated>.*?'+152 '<content.*?>(.+?)</content>.+?</entry>', re.DOTALL)153 maxrow = maxcol = 0154 for i, match in enumerate(re.finditer(regexp, instr)):155 if progress and i > 0 and i % 250 == 0:156 print str(datetime.now())+": ", maxrow, "rows and", i, " cells processed."157 lastupd = re.sub(r'([.][0-9]+)?Z?$', '', match.group(4)).strip()158 #print "lastupd='"+lastupd+"'"159 updated[match.group(1)] = lastupd.strip("\r\n\t ")160 val = match.group(5).strip("\r\n\t ")161 data[match.group(1)] = val162 row = int(match.group(2))163 if row > maxrow:164 maxrow = row165 col = int(match.group(3))166 if col > maxcol:167 maxcol = col168 #print row, col, val169 if progress:170 print str(datetime.now())+": found ", maxrow, "rows and", maxcol, "columns."171 return maxrow, maxcol172def read_gspreadsheet(url, data, updated, progress):173 # read the spreadsheet into a big string174 infh = urllib.urlopen(url)175 instr = infh.read()176 infh.close()177 return parse_gspreadsheet(instr, data, updated, progress)178def find_header_row(data, regexp_str):179 regexp = re.compile(regexp_str, re.IGNORECASE|re.DOTALL)180 header_row = header_startcol = -1181 for row in range(20):182 if header_row != -1:183 break184 for col in range(5):185 val = cellval(data, row, col)186 if (val and re.search(regexp, val)):187 header_row = row188 header_startcol = col189 break190 if header_row == -1:191 parser_error("no header row found: looked for "+regexp_str)192 if header_startcol == -1:193 parser_error("no header start column found")194 return header_row, header_startcol195def parse(instr, maxrecs, progress):196 # TODO: a spreadsheet should really be an object and cellval a method197 data = {}198 updated = {}199 maxrow, maxcol = parse_gspreadsheet(instr, data, updated, progress)200 # find header row: look for "opportunity title" (case insensitive)201 header_row, header_startcol = find_header_row(data, 'opportunity\s*title')202 header_colidx = {}203 header_names = {}204 header_col = header_startcol205 while True:206 header_str = cellval(data, header_row, header_col)207 if not header_str:208 break209 field_name = None210 header_str = header_str.lower()211 if header_str.find("title") >= 0:212 field_name = "OpportunityTitle"213 elif header_str.find("organization") >= 0 and header_str.find("sponsor") >= 0:214 field_name = "SponsoringOrganization"215 elif header_str.find("description") >= 0:216 field_name = "Description"217 elif header_str.find("skills") >= 0:218 field_name = "Skills"219 elif header_str.find("location") >= 0 and header_str.find("name") >= 0:220 field_name = "LocationName"221 elif header_str.find("street") >= 0:222 field_name = "LocationStreet"223 elif header_str.find("city") >= 0:224 field_name = "LocationCity"225 elif header_str.find("state") >= 0 or header_str.find("province") >= 0:226 field_name = "LocationProvince"227 elif header_str.find("zip") >= 0 or header_str.find("postal") >= 0:228 field_name = "LocationPostalCode"229 elif header_str.find("country") >= 0:230 field_name = "LocationCountry"231 elif header_str.find("start") >= 0 and header_str.find("date") >= 0:232 field_name = "StartDate"233 elif header_str.find("start") >= 0 and header_str.find("time") >= 0:234 field_name = "StartTime"235 elif header_str.find("end") >= 0 and header_str.find("date") >= 0:236 field_name = "EndDate"237 elif header_str.find("end") >= 0 and header_str.find("time") >= 0:238 field_name = "EndTime"239 elif header_str.find("contact") >= 0 and header_str.find("name") >= 0:240 field_name = "ContactName"241 elif header_str.find("email") >= 0 or header_str.find("e-mail") >= 0:242 field_name = "ContactEmail"243 elif header_str.find("phone") >= 0:244 field_name = "ContactPhone"245 elif header_str.find("website") >= 0 or header_str.find("url") >= 0:246 field_name = "URL"247 elif header_str.find("often") >= 0:248 field_name = "Frequency"249 elif header_str.find("days") >= 0 and header_str.find("week") >= 0:250 field_name = "DaysOfWeek"251 elif header_str.find("paid") >= 0:252 field_name = "Paid"253 elif header_str.find("commitment") >= 0 or header_str.find("hours") >= 0:254 field_name = "CommitmentHours"255 elif header_str.find("age") >= 0 and header_str.find("min") >= 0:256 field_name = "MinimumAge"257 elif header_str.find("kid") >= 0:258 field_name = "KidFriendly"259 elif header_str.find("senior") >= 0 and header_str.find("only") >= 0:260 field_name = "SeniorsOnly"261 elif header_str.find("sex") >= 0 or header_str.find("gender") >= 0:262 field_name = "SexRestrictedTo"263 elif header_str.find("volunteer appeal") >= 0:264 field_name = None265 else:266 parser_error("couldn't map header '"+header_str+"' to a field name.")267 if field_name != None:268 header_colidx[field_name] = header_col269 header_names[header_col] = field_name270 #print header_str, "=>", field_name271 header_col += 1272 if len(header_names) < 10:273 parser_error("too few fields found: "+str(len(header_names)))274 # check to see if there's a header-description row275 header_desc = cellval(data, header_row+1, header_startcol)276 if not header_desc:277 parser_error("blank row not allowed below header row")278 header_desc = header_desc.lower()279 data_startrow = header_row + 1280 if header_desc.find("up to") >= 0:281 data_startrow += 1282 # find the data283 global CURRENT_ROW284 CURRENT_ROW = row = data_startrow285 blankrows = 0286 MAX_BLANKROWS = 2287 volopps = '<VolunteerOpportunities>'288 numorgs = numopps = 0289 while True:290 blankrow = True291 #rowstr = "row="+str(row)+"\n"292 record = {}293 record['LastUpdated'] = '0000-00-00'294 for field_name in header_colidx:295 col = header_colidx[field_name]296 val = cellval(data, row, col)297 if val:298 blankrow = False299 else:300 val = ""301 #rowstr += " "+field_name+"="+val+"\n"302 record[field_name] = val303 key = 'R'+str(row)+'C'+str(col)304 if (key in updated and305 updated[key] > record['LastUpdated']):306 record['LastUpdated'] = updated[key]307 if blankrow:308 blankrows += 1309 if blankrows > MAX_BLANKROWS:310 break311 else:312 numopps += 1313 blankrows = 0314 record['oppid'] = str(numopps)315 volopps += record_to_fpxml(record)316 row += 1317 CURRENT_ROW = row318 CURRENT_ROW = None319 if progress:320 print str(datetime.now())+": ", numopps, "opportunities found."321 volopps += '</VolunteerOpportunities>'322 outstr = '<?xml version="1.0" ?>'323 outstr += '<FootprintFeed schemaVersion="0.1">'324 outstr += '<FeedInfo>'325 # providerID replaced by caller326 outstr += '<providerID></providerID>'327 # providerName replaced by caller328 outstr += '<providerName></providerName>'329 outstr += '<feedID>1</feedID>'330 outstr += '<createdDateTime>%s</createdDateTime>' % xmlh.current_ts()331 # providerURL replaced by caller332 outstr += '<providerURL></providerURL>'333 outstr += '<description></description>'334 outstr += '</FeedInfo>'335 outstr += "<Organizations>"336 for orgname in KNOWN_ORGS:337 outstr += "<Organization>"338 outstr += xmlh.output_val("organizationID", KNOWN_ORGS[orgname])339 outstr += xmlh.output_val("name", orgname)340 outstr += "</Organization>"341 outstr += "</Organizations>"342 outstr += volopps343 outstr += '</FootprintFeed>'344 #outstr = re.sub(r'><', '>\n<', outstr)345 #print outstr...
gen_test_header.py
Source:gen_test_header.py
1# (c) 2019, 2020 Emir Erbasan(humanova)2# GPL v2 License, see LICENSE for more details34# generates C++ header (src/tests/data.h) for tests5# original test data is located at : misc/test_data/67import time8import numpy as np9import glob1011def convert_to_array(files):12 data = []13 for file in files:14 f = open(file, "rb")15 fn = file[file.rfind('/')+1:file.rfind('.')]16 data.append({"file" : fn, "bytecode" : np.fromfile(f, dtype=np.uint32)})17 return data1819def read_assembly(files):20 data = []21 for file in files:22 f = open(file, "r")23 fn = file[file.rfind('/')+1:file.rfind('.')] + "_asm"24 data.append({"file": fn, "asm" : [("\""+line.replace('\n', '') + "\\n\"") for line in f.readlines()]})25 return data2627def generate_cpp_header(cben_data, basm_data):28 header_str = str()29 header_str += f"/*\n * Generated : {time.strftime('%d-%m-%Y %H:%M:%S.00000', time.gmtime())}\n"30 header_str += " * ----------------------------------------------------------\n"31 header_str += " * This file is generated by gen_test_header.py script. Don't edit directly.\n*/\n\n"3233 header_str += "#include <vector>\n"34 header_str += "#include <string>\n"35 header_str += "#include <cstdint>\n\n"3637 for d in cben_data:38 fn = d['file']39 vector_str = f"std::vector<uint32_t> {fn} = {{{', '.join(hex(inst) for inst in d['bytecode'])}}};"40 header_str += vector_str + "\n"41 header_str += "\n//-----basm strings-----\n\n"4243 for d in basm_data:44 fn = d['file']45 asm = '\n\t'.join(d['asm'])46 asm_str = f"std::string {fn} = \n\t{asm};"47 header_str += asm_str + "\n"48 49 header_str += "\n"5051 for d in cben_data:52 fn = d['file']53 header_str += f"extern std::vector<uint32_t> {fn};\n"54 55 for d in basm_data:56 fn = d['file']57 header_str += f"extern std::string {fn};\n"5859 with open("src/tests/data.h", "w+") as f:60 f.write(header_str)6162if __name__ == "__main__":63 directory = "misc/test_data"64 cben_files = glob.glob(directory + "/bytecode/*.cben")65 basm_files = glob.glob(directory + "/basm/*.basm")66 67 cben_data = convert_to_array(cben_files)68 basm_data = read_assembly(basm_files)
...
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!!