def main_func(): '''function that creates a file called results.log with a level info (i.e. to confirm that function works properly or not) and then print started and some lines and then calling addition function from test.py ''' logging.basicConfig(filename='results.log', level=logging.INFO) logging.info('Started') logging.info('-' * 10) test.add(2, 5) logging.info('-' * 10) logging.info('Finished')
import sys sys.path.append('..') import augmentedgreedy as ag import test test.add(ag.all_pair_subsets, ({1, 2, 3}, ), {frozenset([1, 2]), frozenset([1, 3]), frozenset([2, 3])}) test.add(ag.alpha, ({1, 3}, ag.all_pair_subsets({1, 2, 3})), {frozenset([1, 2]), frozenset([2, 3])}) test.run()
import test print("test.add(3, 4) = ", test.add(3, 4)) print("test.add(8, 9) = ", test.add(8, 9)) help(test)
import test print test.add(10, 20) print test.power(10, 2) print test.mul(10, 5) from test import add print add(10, 20) from test import add, mul print mul(10, 2) from test import * print power(10, 2)
import test print dir(test) print test.add(2, 3)
from test import add add(5) add(6)
模块的定义: 模块就是程序,模块的名称就是不含.py后缀的文件名 分类: python标准模块(python内置模块、python标准库) 第三方模块/库(pypi.org) 自定义模块 好处: 可维护性更强; 方便代码的重用; 模块导入: import module_name 定位: 当前包-->内置函数-->sys.path(环境变量) 模块的属性: dir --列出对象的所有属性及方法 ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__','__package__', '__spec__', 'test.txt'] help --查看类,方法的帮助信息 __name__ --模块的名称 __file__ --文件全路径 import sys print(sys.path) #查看当前环境变量 ''' import test print(dir(test)) print(test.__name__) #模块的名称 print(test.__doc__) #模块的注释 print(test.__file__) #模块文件的路径 test.add(2, 5) print(help(test))
import sys print type(sys.path) for i in sys.path: print i import test print test.add test.add()
def siteprocess(request): test.add() return HttpResponse("Yaay",status='200')
def test_add(self): result = test.add(3, 6) act = 9 self.assertEqual(result, act, f'ไม่ผ่านค่าควรได้คือ {act} แต่ได้ {result}')
def test_add(self): self.assertTrue(add(-1, 5))
from test import add a = [1, 'python'] a = 'a string' c = 1 d = 2 c, d = d, c def func(): a = 1 b = 257 print(a + b) print(a) function() add(1, 2)
import test result = test.add(11, 22) print(result)
import test result = test.add(1,3) print(result)
def AddMoey(): global Money Money = Money + 1 print Money AddMoey() print Money from test import * print add(1, 2, 5, 8) # print sub(100,34,2,5) import test print test.add(1, 2, 3, 4, 5) print test.sub(100, 2, 34, 5) reload(test) str1 = 8 def foo(): print 'calling foo()..' aStr = 'bar' anInt = 23 print "foo()'s globals:", globals() print '1-' * 40 print "foo()'s local:", locals().keys()
import test # 这么导入就是把test文件先全部执行一遍了 result = test.add(11, 12) print(result)
def testAdd1(): assert 10 == t.add(5,6),"test failed" assert 11 = t.add(6,6),"wtf"
import sys sys.path.append('..') import transformedlazygreedy as tlg import test test.add(tlg.only_one, ({1, 2}, {1, 2, 3}), False) test.add(tlg.only_one, ({1, 2}, {1, 3}), True) test.add(tlg.eval_utility, ({1, 2}, {1, 2, 3, 4}, {4}), 2) cs = ({1, 2, 3}, {4, 5}, {1, 2, 3, 4}) events = {1, 2, 3, 4, 5} test.add(tlg.lazy_greedy_msc, (cs, events), [({1, 2, 3, 4}, 2), ({4, 5}, 1)]) cs = ({1, 2}, {4, 5}, {5}) events = {1, 2, 3, 4, 5} test.add(tlg.lazy_greedy_msc, (cs, events), [({1, 2}, 0), ({4, 5}, 1)]) cs = ({1, 2}, {4, 5}) events = {1, 2, 3, 4, 5} test.add(tlg.lazy_greedy_msc, (cs, events), [({1, 2}, 0), ({4, 5}, 1)]) cs = ( {0, 1}, {2}, ) events = {0, 1, 2} res = ([{1, 2}, {1, 2}], {0, 1, 2}) test.add(tlg.mtc_to_msc, (cs, events), res) test.run()
import test2 import test test.add([1, 2, 3]) d = test.get_value() #必须要通过get_value才能把值传出来。 test.add([2, 3, 4]) # d=test.get_value() print(d) #python函数return只返回值,无论是数还是数组都不返回地址,如上所示,如果python返回值是地址, #则d获取的就是数组a的地址,在运行test.add([2,3,4])后d的内容应该随a而改变,结果并没有。
import test a = test.add()
def test_add2(self): test_param = 'asdasd' result = test.add(test_param) self.assertEqual(result, 'number')
# 拆出的 .py 文件就是一个模块 # 拆除的目录可能就是一个包 # 引入同级目录下的test模块 import test print(test.add(10, 20)) #sys Python标准库自带的模块。一些和Python解释器相关的 import sys for line in sys.path: print(line)
def test_add3(self): test_param = 'qwesdfgasd' result = test.add(test_param) self.assertEqual(result, 'number')
import test add = test.add(1, 2) print(add)
def test_add4(self): test_param = None result = test.add(test_param) self.assertEqual(result, 'number')
def fun1(): add() print("from func1")
def test_add(self): '''코멘트 달 수 있음''' test_param = 10 result = test.add(test_param) self.assertEqual(result, 15)
def index(): x = test.add() return x
import test print dir(test) print test.add(2,3)
client.send(query.to_wire()) answer, _ = recvfrom_message(client) if answer is None: raise Exception("no answer received") if not query.is_response(answer): raise Exception("not a mirror response") finally: server.stop() client.close() if __name__ == "__main__": # Self-test code test = test.Test() test.add("testserver/sendrecv", test_sendrecv) if test.run() != 0: sys.exit(1) # Mirror server server = TestServer(None) server.start() server.get_server_socket(None, socket.AF_INET) print ("[==========] Mirror server running at", server.address()) try: while True: time.sleep(0.5) except KeyboardInterrupt: print ("[==========] Shutdown.") pass server.stop()
def test_add(self): self.assertTrue(add(-1,5))
#coding=utf-8 import test result = test.add(3, 2) print(result)
# 三中导入模块的方式 # import test ,在使用模块中的东西的时候,前面必须要带上模块的名称 # from test import add,number ,从模块中导入一部分,使用的时候,可以不带模块名 # from test import * ,将模块中的所有东西都导入到文件中,在使用模块中的东西的时候可以不加模块名 # 但是可以通过__all__来限制*所获得的的内容 import test result = test.add(1, 2, 3, 4, 5, 6, 7, 8) print(result) print(test.number) from test import number print(number) from test import * print(add(1, 2, 3, 4, 5, 6, 7, 8)) # print(minue(1,2,3,4)) 无法直接调用因为在test模块中定义了,__all__=['add','number'] # __name__和__main__ # 用法:在编写模块的时候,经常需要测试模块的性能, # 所以就会在模块中直接调用已经写好的函数, # 但是如果在模块中有函数调用的话,就会感觉很突兀, # 因为模块只是提供函数的,如果突然之间出现一个莫名其妙的函数调用,这很不合理 # 一种方法就是直接删除这段代码,但是如果这段代码,以后还是会用到话就不能删除 # 说这么多,其实__name__ == __main__就是和c++中的# ifndef差不多,预编译的 # 在有__name__的文件中执行程序,那么__name__就是__main__ # 如果在另一个文件中执行程序但是在导入的模块中还有__name__,那么此时__name__就是模块的名字
import test test.add(10, 20)
#!/usr/bin/env python # coding=utf-8 import test if __name__ == "__main__": print test.add(100,100)