def run(): 'Tests the functionality of the extension.' import spam # spam should have a "system" function...try it out if spam.system('echo test') != 0 or spam.system('this_command_should_cause_an_error') != 1: raise AssertionError('test module did not function correctly') # unload del sys.modules['spam'] print print 'Success!' print
def multiply(a,b): print("Will compute", a, "times", b) c = 0 for i in range(0, a): c = c + b spam.system('dir') unicStr1 = "This is Python world! 한글 시도? 日本語はどうだ?" unicStr2 = "日本語はどうだ?두번째 시도?" print('Python side 1:', unicStr1) print('Python side 2:', unicStr2) return c
def run(): 'Tests the functionality of the extension.' import spam # spam should have a "system" function...try it out if spam.system('echo test') != 0 or spam.system( 'this_command_should_cause_an_error') != 1: raise AssertionError('test module did not function correctly') # unload del sys.modules['spam'] print print 'Success!' print
def test_spam(): import platform print("About to import spam") sys.stdout.flush() import spam if "This is an example spam doc." not in spam.__doc__: raise Exception("spam.__doc__ does not contain the expected text") cmd = { "Windows": "dir", }.get(platform.system(), "ls") print("About to run spam.system(\"{}\")".format(cmd)) sys.stdout.flush() spam.system(cmd)
def test_spam(self): print(dir(sp)) sp.system("ls -l")
def test_command_with_invalid_args(self): status = spam.system("ls --unrecognized-option") self.assertEqual(status, 2)
def test_wrong_arg_type(self): with self.assertRaises(TypeError): spam.system(0)
#usr/bin/python #filenam : test.py import spam spam.system("dir")
def test_command(self): status = spam.system("ls") self.assertEqual(status, 0)
def test_system(self): self.assertEqual(0, spam.system('python -c "exit(0)"')) self.assertNotEqual(0, spam.system('python -c "exit(1)"'))
def test_it_works(self) -> None: p = spam.system('ls -la') assert p == 0 # exit code
import spam assert spam.system('date') == 0 assert spam.system('/bin/false') > 0 try: spam.system('does-not-exist') except spam.error as e: assert str(e) == 'System command failed'
''' :Author: Arthur Goldberg <*****@*****.**> :Date: 2017-04-06 :Copyright: 2017-2018, Karr Lab :License: MIT ''' # example program using example spam module # TODO(Arthur): cover after MVP wc_sim done import spam # pragma: no cover for cmd in ["ls -l", "date", "no_such_command", 7, None, 'kill']: # pragma: no cover try: status = spam.system(cmd) print("'{}' returns: {}".format(cmd, status)) except Exception as e: print("Exception: '{}'".format(str(e))) print(spam.nothing()) # pragma: no cover
import spam spam.system("ls -la")
import spam print(spam.system('ls -l'))
import spam spam.system('dir')
import spam x = spam.system("hello world!") print x
#!/usr/bin/env python3 import spam print() status = spam.system("ls -l | wc") print("status: ", status) print() print('Expect spam.SpamError') try: status = spam.check_system("false") print("status: ", status) except spam.SpamError as ex: print(' ', ex) print(' ignored') print() s = spam.Spam("Real brand of SPAM") s.print() print(s) print() n1 = spam.Noddy1() print(n1) print() n2 = spam.Noddy2(first="Mike", last="Bentley") print(n2) print("Name: ", n2.name()) print()
def testSystem(self): import spam res = spam.system("ls -l") if not res: self.Fail("system returned falsy value: %s" % (res))
# spam_test.py import spam spam.system("sha1sum ../../data/Hello.txt") spam.sha1_file("../../data/Hello.txt")
#!/usr/bin/env python3 # Import the Python documentation Fibonacci module import pydoc_fibonacci as fibonacci # Import the Python documentation Spam module import spam # Test Fibonacci sequence to 100 fibonacci.fib(100) # Test Fibonacci sequence (list return) to 100 print(fibonacci.fib2(100)) # Hello spam module print(spam.system('echo hello spam!')) # Test the Spam module by executing "ls -l" print(spam.system('ls -l'))
import spam spam.system("ls -l") spam.system("ls")
def timeme(method): def wrapper(*args, **kw): startTime = int(round(time.time() * 1000)) result = method(*args, **kw) endTime = int(round(time.time() * 1000)) print(endTime - startTime, 'ms') return result return wrapper spam.greet('cassie') spam.system('ls') print spam.strlen("1234") def python_fb(num): result = [] first = 0 second = 1 for i in xrange(num): if i <= 1: next = i else: next = first + second first = second second = next
def test_true(self): status = spam.system("true") self.assertEqual(status, 0)
import spam spam.system("ls -a")
def __init__(self,im_axis,re_axis,im_data,kernel_mode='',model=None,stdev=None,beta=None,**kwargs): self.kernel_mode = kernel_mode self.im_axis = im_axis self.re_axis = re_axis self.im_data = im_data self.nw = self.re_axis.shape[0] self.wmin = self.re_axis[0] self.wmax = self.re_axis[-1] self.dw = np.diff(np.concatenate(([self.wmin],(self.re_axis[1:]+self.re_axis[:-1])/2.,[self.wmax]))) self.model = model # the model should be normalized by the user himself if (self.kernel_mode == 'freq_bosonic'): self.var = stdev**2 self.E = 1./self.var self.niw = self.im_axis.shape[0] self.kernel = (self.re_axis**2)[None,:] / ((self.re_axis**2)[None,:] + (self.im_axis**2)[:,None]) self.kernel[0,0] = 1. # analytically with de l'Hospital elif (self.kernel_mode == 'time_bosonic'): self.var = stdev**2 self.E = 1. / self.var self.niw = self.im_axis.shape[0] self.kernel = 0.5 * self.re_axis[None,:] * ( np.exp(-self.re_axis[None,:]*self.im_axis[:,None]) + np.exp(-self.re_axis[None,:]*(1. - self.im_axis[:,None]))) / ( 1. - np.exp(-self.re_axis[None,:])) self.kernel[:, 0] = 1. # analytically with de l'Hospital elif (self.kernel_mode == 'freq_fermionic'): self.var = np.concatenate((stdev**2, stdev**2)) self.E = 1. / self.var self.niw = 2 * self.im_axis.shape[0] self.kernel = np.zeros((self.niw, self.nw)) # fermionic Matsubara GF is complex self.kernel[:self.niw/2, :] = -self.re_axis[None,:] / ((self.re_axis**2)[None,:] + (self.im_axis**2)[:,None]) self.kernel[self.niw/2:, :] = -self.im_axis[:,None] / ((self.re_axis**2)[None,:] + (self.im_axis**2)[:,None]) elif (self.kernel_mode == 'time_fermionic'): self.var = stdev**2 self.E = 1. / self.var self.niw = self.im_axis.shape[0] self.kernel = (np.exp(-self.im_axis[:,None] * self.re_axis[None,:]) / (1.+np.exp(-self.re_axis[None,:]))) elif (self.kernel_mode == 'freq_fermionic_phsym'): # in this case, the data must be purely real (the imaginary part!) print ('Warning: phsym kernels do not give good results in this implementation. ') self.var = stdev**2 self.E = 1. / self.var self.niw = self.im_axis.shape[0] self.kernel = -2. * self.im_axis[:,None] / ((self.im_axis**2)[:,None] + (self.re_axis**2)[None,:]) elif (self.kernel_mode == 'time_fermionic_phsym'): print ('Warning: phsym kernels do not give good results in this implementation. ') self.var = stdev**2 self.E = 1. / self.var self.niw = self.im_axis.shape[0] self.kernel = (np.cosh(self.im_axis[:,None] * self.re_axis[None,:]) + np.cosh((1. - self.im_axis[:,None]) * self.re_axis[None,:])) / (1. + np.cosh(self.re_axis[None,:])) else: print ('Unknown kernel') sys.exit() U, S, Vt = np.linalg.svd(self.kernel, full_matrices=False) self.n_sv = np.arange(min(self.nw,self.niw))[S>1e-10][-1] # number of singular values larger than 1e-10 self.U_svd = np.array(U[:, :self.n_sv], dtype=np.float64,order='C') self.V_svd = np.array(Vt[:self.n_sv, :].T, dtype=np.float64,order='C') # numpy.svd returns V.T self.Xi_svd = S[:self.n_sv] print ('spectral points:', self.nw) print ('data points on imaginary axis:', self.niw) print ('significant singular values:', self.n_sv) print ('U', self.U_svd.shape) print ('V', self.V_svd.shape) print ('Xi', self.Xi_svd.shape) #============================================================================================= # First, precompute as much as possible # The precomputation of W2 is done in C, this saves a lot of time! # The other precomputations need less loop, can stay in python for the moment. #============================================================================================= print ('Precomputation of coefficient matrices') # allocate space self.W2 = np.zeros((self.n_sv, self.nw), order='C', dtype=np.float64) self.W3 = np.zeros((self.n_sv, self.n_sv, self.nw)) self.d2chi2 = np.zeros((self.nw, self.nw)) self.Evi = np.zeros((self.n_sv)) # precompute matrices W_ml (W2), W_mil (W3) spam.system(self.W2, self.E, self.U_svd, self.Xi_svd, self.V_svd, self.dw, self.model) #precomp.precompute_W2(self.W2, self.E, self.U_svd, self.Xi_svd, self.V_svd, self.dw, self.model) #self.W3 = self.W2[:, None, :]*(self.V_svd[None, :, :]).transpose((0, 2, 1)) # precompute the evidence vector Evi_m #for m in xrange(self.n_sv): # for k in xrange(self.niw): # self.Evi[m]+=self.Xi_svd[m]*self.U_svd[k, m]*self.E[k]*self.im_data[k] # precompute curvature of likelihood function #precomp.precompute_d2chi2(self.d2chi2, self.kernel, self.dw, self.E) # some arrays that are used later... self.chi2arr = [] self.specarr = [] self.backarr = [] self.entrarr = [] self.alpharr = [] self.uarr = [] self.bayesConv = []
""" Tests for integrating our own modules """ import spam status = spam.system("df")
import spam spam.system('ls -l')
import spam status = spam.system("ls -l")
def test_false(self): status = spam.system("false") self.assertEqual(status, 1)
import spam #expects a C-extension lib named spammodule.so spam.system('ls -l')
def test_command_with_args(self): status = spam.system("ls -l") self.assertEqual(status, 0)
import spam print "Executing spam.system('ls') ... " spam.system("ls")
def test_command_not_found(self): status = spam.system("command_not_found") self.assertEqual(status, 127)
def main(): ret = spam.system("ls -l") print(ret)
import spam spam.system("ls -l")
import spam print spam.system("ls -l")
# import conv_util as cos_module_np import numpy as np # import pylab x = np.arange(0, 2 * np.pi, 0.1) print(x) import spam print(spam.system("ls -l")) import convUtil convUtil.c_ext_forward() import cos_module_np y = cos_module_np.cos_func_(x) print(x,y)
import spam spam.system("ls -l", 4)