Best Python code snippet using nose
fdtablebackup.py
Source:fdtablebackup.py
1'''***********************************************************************************************2Tool Name: fdtablebackup (SourceName=fdtablebackup.py)3Version: ArcGIS 10.34Author: zye 3/28/2017 (Environmental Systems Research Institute Inc.)5ConfigFile: 6Required Arguments:7 0 gdbSource=GDB with 6 tables8 1 gdbTarget=GDB with 6 production tables9 2 gdbBackup=GDB to backup the 6 production tables in the gdbTarget10Description: 11 1. copy 6 tables from gdbTarget->gdbBackup (with datetime stamp of the tbl names)12 2. remove the recs from 6 tables in gdbTarget with where: modelid = 1 or 2.13History: Initial coding - 3/27/201714Usage: fdtablebackup.py gdbSource gdbTarget gdbBackup15# python fdtablebackup.py C:\10DATA\TXDEM\KisterData\TXStats.gdb C:\10DATA\TXDEM\KisterData\TXTarget.gdb C:\10DATA\TXDEM\KisterData\TXBackup.gdb16***********************************************************************************************'''17import sys18import os19import time 20import datetime21import arcpy22import apwrutils23import flooddsconfig24K_Sep = ","25FN_MaxTSTimeDT = "MAX_TSTimeDT"26FN_MaxTSTime = "Max_TSTime"27""" get dNames={key=basename, value=dbname.sde.basename} """28def getSDEBaseNameDict(sdeCon, KeyOnBaseName=True):29 """30 returns a dict = 31 {key=basename:value=dbname.sde.basename} if KeyOnBaseName=True32 {key=dbname.sde.basename, key=basename}, if KeyOnBaseName=False33 """34 sWorkspace = arcpy.env.workspace 35 try:36 arcpy.env.workspace = sdeCon37 lTables = arcpy.ListTables('*') 38 lNames = [x.split('.')[len(x.split('.'))-1] for x in lTables]39 dTables = dict(zip(lNames, lTables)) 40 lFCs = arcpy.ListFeatureClasses('*')41 lNames = [x.split('.')[len(x.split('.'))-1] for x in lFCs]42 dFCs = dict(zip(lNames, lFCs)) 43 dTables.update(dFCs)44 if(KeyOnBaseName==False):45 inv_dTables = {v: k for k, v in dTables.iteritems()}46 dTables = inv_dTables 47 return dTables 48 except:49 pass50 finally:51 arcpy.env.workspace = sWorkspace52 53def trace():54 import traceback, inspect55 tb = sys.exc_info()[2]56 tbinfo = traceback.format_tb(tb)[0]57 # script name + line number58 line = tbinfo.split(", ")[1]59 filename = inspect.getfile(inspect.currentframe())60 # Get Python syntax error61 synerror = traceback.format_exc().splitlines()[-1]62 return line, filename, synerror63class GDBOp:64 GDB = None 65 dSDETableNames = None66 def __init__(self, gdb):67 self.GDB = gdb 68 oDesc = arcpy.Describe(gdb) 69 self.workspaceType = oDesc.workspaceType 70 if(oDesc.workspaceType=='RemoteDatabase'):71 self.isRemote = True72 self.dSDETableNames = getSDEBaseNameDict(gdb) 73 else:74 self.isRemote = False 75 def getSDETableName(self, sName):76 sReturn = sName77 if(self.dSDETableNames):78 try:79 sReturn = self.dSDETableNames[sName]80 except:81 pass82 return sReturn 83"""define the TSValueStatsOp class"""84class ClassOp:85 def __init__(self):86 self.DebugLevel = 087 def __exit__(self, type, value, traceback):88 if((self.DebugLevel & 2) ==2):89 apwrutils.Utils.ShowMsg(self.thisFileName() + " completed at " + time.ctime()) 90 91 def thisFileName(self):92 import inspect93 return inspect.getfile(inspect.currentframe())94 # Load the configuration xml.95 96 def getWorkspace(self, pFL):97 oDesc = arcpy.Describe(pFL)98 ooDesc = arcpy.Describe(oDesc.path)99 if(ooDesc.dataType=='FeatureDataset'):100 sWorkspace = ooDesc.path101 else:102 sWorkspace = oDesc.path103 return sWorkspace104 105 """ execute(self, pParams=(gdbSource, gdbTarget, gdbBackup, lTables) """106 def execute(self, pParams): 107 """ 108 pParams=(gdbSource, gdbTarget, gdbBackup, lTables)109 """110 sMsg = ""111 sOK = apwrutils.C_OK 112 ds = time.clock()113 try:114 (gdbSource, gdbTarget, gdbBackup, lTables) = pParams115 pGDBSource = GDBOp(gdbSource) 116 pGDBBackup = GDBOp(gdbBackup) 117 pGDBTarget = GDBOp(gdbTarget) 118 if((self.DebugLevel & 1)==1):119 sMsg = apwrutils.Utils.getcmdargs(pParams) 120 arcpy.AddMessage(sMsg) 121 #..make sure the target gdb has the tables, if not copy them.122 for i, sTable in enumerate(lTables):123 sTableNameT = pGDBTarget.getSDETableName(sTable) 124 tbTarget = os.path.join(gdbTarget, sTableNameT) 125 if(arcpy.Exists(tbTarget)==False):126 sTableNameS = pGDBSource.getSDETableName(sTable) 127 tbSource = os.path.join(gdbSource, sTableNameS) 128 arcpy.Copy_management(tbSource, os.path.join(gdbTarget, sTable) ) 129 if (self.DebugLevel & 1) == 1: arcpy.AddMessage("{}. Copy {} -> {}".format(i, tbSource, tbTarget))130 #..Copy the tables from target to the backup gdb131 hd = "X_{}".format(apwrutils.Utils.GetDateTimeString())132 for i, sTable in enumerate(lTables):133 tbSource = os.path.join(gdbTarget, pGDBTarget.getSDETableName(sTable)) 134 tbTarget = os.path.join(gdbBackup, "{}_{}".format(hd, sTable))135 arcpy.Copy_management(tbSource, tbTarget) 136 if (self.DebugLevel & 1) == 1: arcpy.AddMessage("{}. Copy {} -> {}".format(i, tbSource, tbTarget))137 for i, sTable in enumerate(lTables):138 sTableS = pGDBSource.getSDETableName(sTable) 139 sTableT = pGDBTarget.getSDETableName(sTable) 140 tbTarget = os.path.join(gdbTarget, sTableT)141 tbSource = os.path.join(gdbSource, sTableS) 142 nCnt = int(arcpy.GetCount_management(tbSource)[0])143 arcpy.DeleteRows_management(tbTarget) 144 arcpy.Append_management(tbSource, tbTarget, "NO_TEST") 145 if(tbTarget.endswith("Max")):146 #..trying to copy the field of Max_TS...147 if(len(arcpy.ListFields(tbTarget,FN_MaxTSTimeDT))>0):148 try:149 arcpy.CalculateField_management(tbTarget,FN_MaxTSTimeDT,"!{}!".format(flooddsconfig.FN_ForecastTime),"PYTHON_9.3")150 except:151 pass152 if(len(arcpy.ListFields(tbTarget,FN_MaxTSTimeDT))>0):153 try:154 arcpy.CalculateField_management(tbTarget,FN_MaxTSTime,"!{}!".format(flooddsconfig.FN_TSTIME),"PYTHON_9.3")155 except:156 pass157 if (self.DebugLevel & 1) == 1: arcpy.AddMessage("{}. Copy {} recs, {} -> {}".format(i, nCnt, tbSource, tbTarget))158 except:159 sMsg = trace()160 arcpy.AddMessage(sMsg)161 sOK = apwrutils.C_NOTOK162 finally:163 pass 164 return (sOK, gdbBackup, sMsg) 165 166 167if __name__ == '__main__':168 #oProcessor = None169 ds = time.clock()170 try:171 debugLevel = 1172 if(len(sys.argv)<2):173 arcpy.AddMessage("Usage: {} {} {}".format(sys.argv[0], "gdbSource gdbTarget gdbBackup"))174 sys.exit() 175 else:176 gdbSource = arcpy.GetParameterAsText(0) # sys.argv[1]177 gdbTarget = arcpy.GetParameterAsText(1) #sys.argv[2]178 gdbBackup = arcpy.GetParameterAsText(2) #sys.argv[3]179 180 lTables = [flooddsconfig.TB_CountyImpact,flooddsconfig.TB_CountyImpactMax, flooddsconfig.TB_DistIDImpact, flooddsconfig.TB_DistIDImpactMax, flooddsconfig.TB_RegIDImpact, flooddsconfig.TB_RegIDImpactMax]181 pProcessor = ClassOp()182 pProcessor.DebugLevel = debugLevel183 pParams=(gdbSource, gdbTarget, gdbBackup, lTables)184 (sOK, gdbBackup, sMsg) = pProcessor.execute(pParams) #pQHFile, pOutFile, iValueIndex, iGroupByIndex, iStats) 185 arcpy.AddMessage("Completed, dt={}.".format(apwrutils.Utils.GetDSMsg(ds)))186 del pProcessor 187 arcpy.SetParameterAsText(3, gdbBackup) 188 except arcpy.ExecuteError:189 arcpy.AddError("{} {}".format(arcpy.GetMessages(2),trace()))190 except:191 arcpy.AddWarning("{} {}".format(arcpy.GetMessages(2),trace()))192 finally:193 dt = datetime.datetime.now()...
fdtableupdate.py
Source:fdtableupdate.py
1'''***********************************************************************************************2Tool Name: fdtablebackup (SourceName=fdtablebackup.py)3Version: ArcGIS 10.34Author: zye 3/28/2017 (Environmental Systems Research Institute Inc.)5ConfigFile: 6Required Arguments:7 0 gdbSource=GDB with 6 tables8 1 gdbTarget=GDB with 6 production tables9 2 gdbBackup=GDB to backup the 6 production tables in the gdbTarget10Description: 11 1. copy 6 tables from gdbTarget->gdbBackup (with datetime stamp of the tbl names)12 2. remove the recs from 6 tables in gdbTarget with where: modelid = 1 or 2.13History: Initial coding - 3/27/201714Usage: fdtablebackup.py gdbSource gdbTarget gdbBackup15# python fdtablebackup.py C:\10DATA\TXDEM\KisterData\TXStats.gdb C:\10DATA\TXDEM\KisterData\TXTarget.gdb C:\10DATA\TXDEM\KisterData\TXBackup.gdb16***********************************************************************************************'''17import sys18import os19import time 20import datetime21import arcpy22import apwrutils23import flooddsconfig24K_Sep = ","25FN_MaxTSTimeDT = "MAX_TSTimeDT"26FN_MaxTSTime = "Max_TSTime"27""" get dNames={key=basename, value=dbname.sde.basename} """28def getSDEBaseNameDict(sdeCon, KeyOnBaseName=True):29 """30 returns a dict = 31 {key=basename:value=dbname.sde.basename} if KeyOnBaseName=True32 {key=dbname.sde.basename, key=basename}, if KeyOnBaseName=False33 """34 sWorkspace = arcpy.env.workspace 35 try:36 arcpy.env.workspace = sdeCon37 lTables = arcpy.ListTables('*') 38 lNames = [x.split('.')[len(x.split('.'))-1] for x in lTables]39 dTables = dict(zip(lNames, lTables)) 40 lFCs = arcpy.ListFeatureClasses('*')41 lNames = [x.split('.')[len(x.split('.'))-1] for x in lFCs]42 dFCs = dict(zip(lNames, lFCs)) 43 dTables.update(dFCs)44 if(KeyOnBaseName==False):45 inv_dTables = {v: k for k, v in dTables.iteritems()}46 dTables = inv_dTables 47 return dTables 48 except:49 pass50 finally:51 arcpy.env.workspace = sWorkspace52 53def trace():54 import traceback, inspect55 tb = sys.exc_info()[2]56 tbinfo = traceback.format_tb(tb)[0]57 # script name + line number58 line = tbinfo.split(", ")[1]59 filename = inspect.getfile(inspect.currentframe())60 # Get Python syntax error61 synerror = traceback.format_exc().splitlines()[-1]62 return line, filename, synerror63class GDBOp:64 GDB = None 65 dSDETableNames = None66 def __init__(self, gdb):67 self.GDB = gdb 68 oDesc = arcpy.Describe(gdb) 69 self.workspaceType = oDesc.workspaceType 70 if(oDesc.workspaceType=='RemoteDatabase'):71 self.isRemote = True72 self.dSDETableNames = getSDEBaseNameDict(gdb) 73 else:74 self.isRemote = False 75 def getSDETableName(self, sName):76 sReturn = sName77 if(self.dSDETableNames):78 try:79 sReturn = self.dSDETableNames[sName]80 except:81 pass82 return sReturn 83"""define the TSValueStatsOp class"""84class ClassOp:85 def __init__(self):86 self.DebugLevel = 087 def __exit__(self, type, value, traceback):88 if((self.DebugLevel & 2) ==2):89 apwrutils.Utils.ShowMsg(self.thisFileName() + " completed at " + time.ctime()) 90 91 def thisFileName(self):92 import inspect93 return inspect.getfile(inspect.currentframe())94 # Load the configuration xml.95 96 def getWorkspace(self, pFL):97 oDesc = arcpy.Describe(pFL)98 ooDesc = arcpy.Describe(oDesc.path)99 if(ooDesc.dataType=='FeatureDataset'):100 sWorkspace = ooDesc.path101 else:102 sWorkspace = oDesc.path103 return sWorkspace104 105 """ execute(self, pParams=(gdbSource, gdbTarget, gdbBackup, lTables) """106 def execute(self, pParams): 107 """ 108 pParams=(gdbSource, gdbTarget, gdbBackup, lTables)109 """110 sMsg = ""111 sOK = apwrutils.C_OK 112 ds = time.clock()113 try:114 (gdbSource, gdbTarget, gdbBackup, lTables) = pParams115 pGDBSource = GDBOp(gdbSource) 116 pGDBBackup = GDBOp(gdbBackup) 117 pGDBTarget = GDBOp(gdbTarget) 118 if((self.DebugLevel & 1)==1):119 sMsg = apwrutils.Utils.getcmdargs(pParams) 120 arcpy.AddMessage(sMsg) 121 #..make sure the target gdb has the tables, if not copy them.122 for i, sTable in enumerate(lTables):123 sTableNameT = pGDBTarget.getSDETableName(sTable) 124 tbTarget = os.path.join(gdbTarget, sTableNameT) 125 if(arcpy.Exists(tbTarget)==False):126 sTableNameS = pGDBSource.getSDETableName(sTable) 127 tbSource = os.path.join(gdbSource, sTableNameS) 128 arcpy.Copy_management(tbSource, os.path.join(gdbTarget, sTable) ) 129 if (self.DebugLevel & 1) == 1: arcpy.AddMessage("{}. Copy {} -> {}".format(i, tbSource, tbTarget))130 #..Copy the tables from target to the backup gdb131 hd = "X_{}".format(apwrutils.Utils.GetDateTimeString())132 for i, sTable in enumerate(lTables):133 tbSource = os.path.join(gdbTarget, pGDBTarget.getSDETableName(sTable)) 134 tbTarget = os.path.join(gdbBackup, "{}_{}".format(hd, sTable))135 arcpy.Copy_management(tbSource, tbTarget) 136 if (self.DebugLevel & 1) == 1: arcpy.AddMessage("{}. Copy {} -> {}".format(i, tbSource, tbTarget))137 for i, sTable in enumerate(lTables):138 sTableS = pGDBSource.getSDETableName(sTable) 139 sTableT = pGDBTarget.getSDETableName(sTable) 140 tbTarget = os.path.join(gdbTarget, sTableT)141 tbSource = os.path.join(gdbSource, sTableS) 142 nCnt = int(arcpy.GetCount_management(tbSource)[0])143 arcpy.DeleteRows_management(tbTarget) 144 arcpy.Append_management(tbSource, tbTarget, "NO_TEST") 145 if(tbTarget.endswith("Max")):146 #..trying to copy the field of Max_TS...147 if(len(arcpy.ListFields(tbTarget,FN_MaxTSTimeDT))>0):148 try:149 arcpy.CalculateField_management(tbTarget,FN_MaxTSTimeDT,"!{}!".format(flooddsconfig.FN_ForecastTime),"PYTHON_9.3")150 except:151 pass152 if(len(arcpy.ListFields(tbTarget,FN_MaxTSTimeDT))>0):153 try:154 arcpy.CalculateField_management(tbTarget,FN_MaxTSTime,"!{}!".format(flooddsconfig.FN_TSTIME),"PYTHON_9.3")155 except:156 pass157 if (self.DebugLevel & 1) == 1: arcpy.AddMessage("{}. Copy {} recs, {} -> {}".format(i, nCnt, tbSource, tbTarget))158 except:159 sMsg = trace()160 arcpy.AddMessage(sMsg)161 sOK = apwrutils.C_NOTOK162 finally:163 pass 164 return (sOK, gdbBackup, sMsg) 165 166 167if __name__ == '__main__':168 #oProcessor = None169 ds = time.clock()170 try:171 debugLevel = 1172 if(len(sys.argv)<2):173 arcpy.AddMessage("Usage: {} {} {}".format(sys.argv[0], "gdbSource gdbTarget gdbBackup"))174 sys.exit() 175 else:176 gdbSource = arcpy.GetParameterAsText(0) # sys.argv[1]177 gdbTarget = arcpy.GetParameterAsText(1) #sys.argv[2]178 gdbBackup = arcpy.GetParameterAsText(2) #sys.argv[3]179 180 lTables = [flooddsconfig.TB_CountyImpact,flooddsconfig.TB_CountyImpactMax, flooddsconfig.TB_DistIDImpact, flooddsconfig.TB_DistIDImpactMax, flooddsconfig.TB_RegIDImpact, flooddsconfig.TB_RegIDImpactMax]181 pProcessor = ClassOp()182 pProcessor.DebugLevel = debugLevel183 pParams=(gdbSource, gdbTarget, gdbBackup, lTables)184 (sOK, gdbBackup, sMsg) = pProcessor.execute(pParams) #pQHFile, pOutFile, iValueIndex, iGroupByIndex, iStats) 185 arcpy.AddMessage("Completed, dt={}.".format(apwrutils.Utils.GetDSMsg(ds)))186 del pProcessor 187 arcpy.SetParameterAsText(3, gdbBackup) 188 except arcpy.ExecuteError:189 arcpy.AddError("{} {}".format(arcpy.GetMessages(2),trace()))190 except:191 arcpy.AddWarning("{} {}".format(arcpy.GetMessages(2),trace()))192 finally:193 dt = datetime.datetime.now()...
13896_inspector.py
Source:13896_inspector.py
...23 while tb.tb_next:24 tb = tb.tb_next25 26 frame = tb.tb_frame27 lines, exc_line = tbsource(tb)28 29 # figure out the set of lines to grab.30 inspect_lines, mark_line = find_inspectable_lines(lines, exc_line)31 src = StringIO(textwrap.dedent(''.join(inspect_lines)))32 exp = Expander(frame.f_locals, frame.f_globals)33 while inspect_lines:34 try:35 for tok in tokenize.generate_tokens(src.readline):36 exp(*tok)37 except tokenize.TokenError as e:38 # this can happen if our inspectable region happens to butt up39 # against the end of a construct like a docstring with the closing40 # """ on separate line41 log.debug("Tokenizer error: %s", e)42 inspect_lines.pop(0)43 mark_line -= 144 src = StringIO(textwrap.dedent(''.join(inspect_lines)))45 exp = Expander(frame.f_locals, frame.f_globals)46 continue47 break48 padded = []49 if exp.expanded_source:50 exp_lines = exp.expanded_source.split('\n')51 ep = 052 for line in exp_lines:53 if ep == mark_line:54 padded.append('>> ' + line)55 else:56 padded.append(' ' + line)57 ep += 158 return '\n'.join(padded)59def tbsource(tb, context=6):60 """Get source from a traceback object.61 A tuple of two things is returned: a list of lines of context from62 the source code, and the index of the current line within that list.63 The optional second argument specifies the number of lines of context64 to return, which are centered around the current line.65 .. Note ::66 This is adapted from inspect.py in the python 2.4 standard library, 67 since a bug in the 2.3 version of inspect prevents it from correctly68 locating source lines in a traceback frame.69 """70 71 lineno = tb.tb_lineno72 frame = tb.tb_frame73 if context > 0:...
inspector.py
Source:inspector.py
...22 while tb.tb_next:23 tb = tb.tb_next24 25 frame = tb.tb_frame26 lines, exc_line = tbsource(tb)27 28 # figure out the set of lines to grab.29 inspect_lines, mark_line = find_inspectable_lines(lines, exc_line)30 src = StringIO(textwrap.dedent(''.join(inspect_lines)))31 exp = Expander(frame.f_locals, frame.f_globals)32 while inspect_lines:33 try:34 tokenize.tokenize(src.readline, exp)35 except tokenize.TokenError, e:36 # this can happen if our inspectable region happens to butt up37 # against the end of a construct like a docstring with the closing38 # """ on separate line39 log.debug("Tokenizer error: %s", e)40 inspect_lines.pop(0)41 mark_line -= 142 src = StringIO(textwrap.dedent(''.join(inspect_lines)))43 exp = Expander(frame.f_locals, frame.f_globals)44 continue45 break46 padded = []47 if exp.expanded_source:48 exp_lines = exp.expanded_source.split('\n')49 ep = 050 for line in exp_lines:51 if ep == mark_line:52 padded.append('>> ' + line)53 else:54 padded.append(' ' + line)55 ep += 156 return '\n'.join(padded)57def tbsource(tb, context=6):58 """Get source from a traceback object.59 A tuple of two things is returned: a list of lines of context from60 the source code, and the index of the current line within that list.61 The optional second argument specifies the number of lines of context62 to return, which are centered around the current line.63 NOTE:64 65 This is adapted from inspect.py in the python 2.4 standard library, since66 a bug in the 2.3 version of inspect prevents it from correctly locating67 source lines in a traceback frame.68 """69 70 lineno = tb.tb_lineno71 frame = tb.tb_frame...
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!!