def test_wrapping_value4(self): f = { returns_10(), returns_10(), returns_60() } g = Do.wrap(f) self.assertEqual( Do.run(g) , {10,60} )
def test_wrapping_value3(self): f = { returns_10(): True, returns_10(): False, returns_60(): None } g = Do.wrap(f) with self.assertRaises(Do.DuplicateKeyError): Do.run(g)
def test_boolean_operations(self): # and # or # xor values = [(True,returns_True),(False,returns_False)] for a in values: for b in values: self.assertEqual( Do.run( a[1]() & b[1]() ) , a[0] & b[0] ) self.assertEqual( Do.run( a[1]() | b[1]() ) , a[0] | b[0] ) self.assertEqual( Do.run( a[1]() ^ b[1]() ) , a[0] ^ b[0] )
def test_wrapping_type(self): self.assertTrue(isinstance(Do.wrap( () ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( (1,2,3) ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( [] ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( [1,2,3] ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( set() ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( {1,2,3} ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( dict() ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( {"happy":True,"value":5} ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( 7 ),Do.Struct)) self.assertTrue(isinstance(Do.wrap( None ),Do.Struct))
def compare_test(): output = [] output.append( returns_10() == returns_60() ) # False output.append( returns_60() == returns_10() ) # False output.append( returns_60() == 60 ) # True output.append( returns_10() != returns_60() ) # True output.append( returns_60() != returns_10() ) # True output.append( returns_60() != 60 ) # False output.append( returns_10() < returns_60() ) # True output.append( returns_60() < returns_10() ) # False output.append( returns_60() < returns_60() ) # False output.append( returns_10() > returns_60() ) # False output.append( returns_60() > returns_10() ) # True output.append( returns_60() > returns_60() ) # False output.append( returns_10() <= returns_60() ) # True output.append( returns_60() <= returns_10() ) # False output.append( returns_60() <= returns_60() ) # True output.append( returns_10() >= returns_60() ) # False output.append( returns_60() >= returns_10() ) # True output.append( returns_60() >= returns_60() ) # True return Do.wrap(output)
def Expand(pArgs, pFilePath, pSearchPath, pSection, pSep='!!', pList=False, pVerbose=False): ''' Extract command from file and replace all macros pArgs -- Args passed to program except file path and section name pFilePath -- File to use in macro expansion pSearchPath -- File to scan for command ' pCount -- Number of lines at the start of the file to scan pSep -- String used to identify end of command pHelp -- True to display available commands ''' # ---- Find command lCommand = FindCommand(pArgs[0], pFilePath, pSearchPath, pSection, pList=pList, pVerbose=pVerbose) # ---- Expand and insert/append any passed arguments # Arguments on original pb.py command line will replace {} from left to right # otherwise they will be appended to the end of the command lStart = 1 if len(lCommand) > 0: if len(pArgs) > lStart: for lArg in pArgs[lStart:]: if lArg.find('{') >= 0: lArg = Do2.ExpandArg(lArg, pFilePath, '') if len(lArg) > 0: try: lTest = os.path.abspath(lArg) if os.path.exists(lTest): if lTest.find(" ") > 0: lTest = '"' + lTest + '"' lArg = lTest except: pass lPos = lCommand.find('{}') if lPos >= 0: lCommand = lCommand[0:lPos] + lArg + lCommand[lPos + 2:] else: lCommand += ' ' + lArg # ---- Prevent unwanted arguments appended to command lPos = lCommand.rfind(pSep) if lPos > 0: lCommand = lCommand[0:lPos] # ---- Expand all remaining macros if lCommand.find('{') >= 0: lCommand = Do.Expand(lCommand, pFilePath) return lCommand
def test_compare(self): y = [ False, False, True, True, True, False, True, False, False, False, True, False, True, False, True, False, True, True ] self.assertEqual( Do.run(compare_test()) , y )
def main(): do_list = [] py_list = [] a = time.time() b = time.time() for i in range(100): a = time.time() Do.run(doack(3,3)) b = time.time() do_list.append(b-a) a = time.time() pyack(3,3) b = time.time() py_list.append(b-a) Do_mean = np.mean(do_list) Py_mean = np.mean(py_list) print("Do",Do_mean) print("Py",Py_mean) print("Ratio",Do_mean/Py_mean)
def Expand(pArgs, pFilePath, pSearchPath, pCount, pSep='!!', pDefault='#!', pList=False, pWidth=15, pVerbose=False): ''' Extract command from file and replace all macros pArgs -- Args passed to program except file path #!key and any arguments pFilePath -- File to use in macro expansion pSearchPath -- File to scan for command ' pCount -- Number of lines at the start of the file to scan pSep -- String used to identify end of command, extra arguments will not be appended pList -- True to display available commands pWidth -- Number of columns to scan for #! ''' # ---- Find command if len(pArgs[1]) == 0: lKey = pDefault lStart = 1 elif pArgs[1].startswith(pDefault): lKey = pArgs[1] lStart = 2 else: lKey = pDefault lStart = 1 lCommand = FindCommand(lKey, pFilePath, pSearchPath, pCount, pDefault=pDefault, pList=pList, pWidth=pWidth, pVerbose=pVerbose) if lCommand == '': print 'pb.py Command', lKey, 'not found' # ---- Expand and insert/append any passed arguments # Arguments on original pb.py command line will replace {} from left to right # otherwise they will be appended to the end of the command if len(lCommand) > 0: if len(pArgs) > lStart: for lArg in pArgs[lStart:]: if lArg.find('{') >= 0: lArg = Do2.ExpandArg(lArg, pFilePath, '') lPos = lCommand.find('{}') if lPos >= 0: lCommand = lCommand[0:lPos] + lArg + lCommand[lPos+2:] else: lCommand += ' ' + lArg # ---- Prevent unwanted arguments appended to command lPos = lCommand.rfind(pSep) if lPos > 0: lCommand = lCommand[0:lPos] # ---- Expand all remaining macros if lCommand.find('{') >= 0: lCommand = Do.Expand(lCommand, pFilePath) return lCommand
def ExpandArg(pArg, pFilePath, pDefaultPath, pLeft='{', pSep=';', pQuote='"', pPref='{<-}', pJoin='\\'): ''' Expand single command line argument ''' # Expand argument if pArg.find(pLeft) >= 0: lArg = Do.Expand(pArg, pFilePath) else: lArg = pArg # If starts with {<-} and single path scan parent folders for # first matching file. Each folder above specified path will # be checked until file is found. if lArg.startswith(pPref) and lArg.find(pSep) < 0: lArg = os.path.abspath(lArg[2:]) print 'Parent Search 1' lArg = ParentSearch(lArg, pDefaultPath, pJoin=pJoin) # If multiple paths separated by pSep(';') select first valid path lFound = '' if lArg.find(pSep) >= 0: lPaths = lArg.split(pSep) for lPath in lPaths: if lPath.startswith(pPref): lPath = os.path.abspath(lPath[4:]) lPath = ParentSearch(lPath, pDefaultPath, pJoin=pJoin) # 01/19/11 if lPath == '': # continue # lPath = os.path.abspath(lPath) if os.path.exists(lPath): lFound = lPath break else: lFound = os.path.abspath(pDefaultPath) else: lFound = lArg # If argument contains space enclose it within quotes lFound = lFound.strip() if lFound.find(' ') > 0 and lFound[0] != pQuote: lFound = pQuote + lFound + pQuote return lFound
os.environ[pValue] = '' if __name__ == '__main__': (mOptions, mArgs) = getopt.getopt(sys.argv[1:], 'd:e:hlm:v') mVerbose = False mHelp = False mList = False mSearchPath = '{o}\menus.ini;{i}\menus.ini' for (mKey, mValue) in mOptions: if mKey == '-d': # Set current directory if mValue.find('{') >= 0: if len(mArgs) > 2: mFilePath = os.path.abspath(mArgs[0]) mValue = Do.ExpandArg(mValue, mFilePath) else: print('DoM.py No primary file, could not set directory') break else: os.chdir(mValue) elif mKey == '-e': # Set environment variable setenviron(mValue, mFilePath) elif mKey == '-h': print(mHelpText) mHelp = True elif mKey == '-l': mList = True
def test_update(self): a = StateTester() b = Do.update(a,"increment_by",8) c = Do.update(b,"increment_by",-4) d = Do.update(c,len,c) self.assertEqual( Do.run(d).value , 10 )
def test_sqrt(self): a = Do.run(sqrt_test(2)) self.assertTrue( abs(a-4) < 10e-6 )
def test_get_set(self): # test dot operations # test non-tree graph self.assertEqual( Do.run(outer_example()) , "hellohellohello" )
def test_truediv(self): # true division self.assertEqual( Do.run(truediv_test()) , 6.0 )
mSection = '' elif mFields[-2] != '': mSection = mFields[-2] else: mPos = mFilePath.rfind('.') if mPos >= 0: mSection = mFilePath[lPos+1:] else: mSection = 'txt' mKey = mFields[-1] mArgs[0] = mKey if mSection.find('{'): mSection = Do.Expand(mSection, mFilePath) # ---- Extract command from registry # Remove this and import if this is going to far # Can not list commands from registry if mKey[0] == '{': if mExtension == mDefaultExtension: # No extension specified lPos = mFilePath.rfind('.') if lPos > 0: mExtension = mFilePath[lPos:] # Contains extension with '.' else: mExtension = mDefaultExtension if len(mArgs) > 2: mArgs = mArgs[2:] else: mArgs = []
mArgs = mArgs[1:] else: mCommand = "Open" lPos = mExpandFile.rfind('.') if lPos >= 0: mExtension = mExpandFile[lPos:] else: mExtension = '' mVerbose = False for (mKey, mValue) in mOptions: if mKey == '-d': # Set current directory if mValue.find('}'): mValue = Do.Expand(mValue, mFileName) os.chdir(mValue) elif mKey == '-e': # Set environment variable setenviron(mValue, mFileName) elif mKey == '-v': # Run in verbose mode mVervose = True elif mKey == '-x': # override extension where command mExtension = mValue # is extracted if mExtension == '': mExtension = 'txt' DoCommand(mExpandFile, mCommand, mExtension, mArgs, pVerbose=mVerbose)
def test_wrapping_value1(self): a = ack(2,2) b = [None,{"hello":a},-99] c = Do.wrap(b) self.assertEqual( Do.run(c) , [None,{"hello":7},-99] )
def test_invert(self): # invert self.assertEqual( Do.run(invert_test()) , -61 )
def test_abs(self): # abs # neg self.assertEqual( Do.run(abs(neg_test())) , 60 )
def test_shifts(self): # lshift # rshift self.assertEqual( Do.run( returns_5() << returns_1() ) , 5 << 1 ) self.assertEqual( Do.run( returns_5() >> returns_1() ) , 5 >> 1 )
def test_pow(self): # pow # floor division # addition self.assertEqual( Do.run(pow_test1()) , 1000000 ) self.assertEqual( Do.run(pow_test2()) , pow(17,10,60) )
def test_mod(self): # mod self.assertEqual( Do.run(mod_test()) , 3 )
def test_floordiv(self): # floor division self.assertEqual( Do.run(floordiv_test()) , 7 )
def test_fact(self): # multiplication self.assertEqual( Do.run(fact(10)) , 3628800 )
def test_wrapping_value2(self): a1 = fib(6) a2 = fact(10) b = [a2,{6,False,(a1,a2)},a1] c = Do.wrap(b) self.assertEqual( Do.run(c) , [3628800,{6,False,(8,3628800)},8] )
def test_neg(self): # neg self.assertEqual( Do.run(neg_test()) , -60 )
def BuildCommand(pExpandFile, pKey, pExtension, pArgs, pVerbose=False): ''' The extension from pExpandFile is used to select the command associated with the command named in pKey for the file type associated with the given extension. pExpandFile - File to be processed pKey - Command name pExtension - Extension used to select command text pArgs - Passed arguments Note: c:\messagebox.exe displays its command line in a popup window. ''' if pVerbose: # not currently used for anything lValues = GetExtensionInfo(pExtension) else: lValues = {} if len(pArgs) > 1: pArgs = pArgs[1:] else: pArgs = [] lExpandFile = pExpandFile lKey = pKey lAbort = False lText = '' if len(lKey) > 0 and len(lExpandFile) > 0: lPos = lExpandFile.rfind('.') if lPos > 0: if pExtension == '': lExtension = lExpandFile[lPos:] else: lExtension = pExtension lFileType = QueryValue(HKEY_CLASSES_ROOT, lExtension) if len(lFileType) > 0: (lAbort, lText) = GetCommand(lFileType, lKey) if lAbort: (lAbort, lText) = GetCommand('*', lKey) else: lAbort = True pass else: lAbort = True pass else: lAbort = True pass lArgs = '' if len(pArgs) > 0: lArgs = ' '.join(pArgs) if not lAbort: if lText.find("%") >= 0: lText = lText.replace("%1", str(lExpandFile)) for lCount in range(0, len(pArgs)): lString = '%' + str(lCount + 2) lText = lText.replace(lString, pArgs[lCount]) lText = lText.replace("%*", lArgs) else: lText += " " + lExpandFile + " " + lArgs if lText.find('{') >= 0: lText = Do.Expand(lText, lExpandFile) else: lText = '' return lText
def test_pos(self): # pos self.assertEqual( Do.run(pos_test()) , 60 )
# ---- Set run options mDefaultLabel = 'light green' mDefaultButton = 'light blue' mOrient = VERTICAL for (mKey, mValue) in mOptions: if mKey == '-b': mDefaultButton = mValue elif mKey == '-d': # Set current directory if mValue.find('}') >= 0: mValue = Expand(mValue, mFileName) os.chdir(mValue) elif mKey == '-e': # Set environment variable Do.setenviron(mValue, mFileName) elif mKey == '-h': print mHelpText elif mKey == '-l': mDefaultLabel = mValue elif mKey == '-o': mOrient = HORIZONTAL tk = Tkinter.Tk() if len(mArgs) > 0: try: mDefaultMenu = os.environ['MENUPATH']
def test_sub(self): # subtraction self.assertEqual( Do.run(sub_test()) , 50 )