def avaliation(num_testes): for i in range(num_testes): convergencias = 0 itr_converg = [] qtd_convergidos = [] fitness_med = [] ofile = open('teste' + str(i) + '.csv', "w", newline='') writer = csv.writer(ofile) #writer.writerow(['config','fitness']) print("Teste " + str(i + 1)) (it_converg, total_converg, fit_medio) = main(False, writer) convergencias += 1 if total_converg > 0 else 0 itr_converg.append(it_converg) qtd_convergidos.append(total_converg) fitness_med.append(fit_medio) ofile.close() ofile = open('info' + str(i) + '.csv', "w", newline='') writer = csv.writer(ofile) #writer.writerow(('dado','valor')) writer.writerow( (TAM_POPULACAO, total_converg, it_converg, "%.3f" % fit_medio))
def integration(num_testes): dir = "cutcross" list_modules = os.listdir(dir) for module in list_modules: if module.split('.')[-1] != 'py': list_modules.remove(module) #endif #endfor oinfo = open('info_cutcross.csv', "w", newline='') oteste = open('cutcross.csv', "w", newline='') writeri = csv.writer(oinfo) writert = csv.writer(oteste) writeri.writerow(('populacao', 'total_cenverg', 'it_converg', 'fitmedio')) writert.writerow(('config', 'fitness')) for module in list_modules: print(module) imp.load_source('module', dir + os.sep + module) from module import main for i in range(num_testes): convergencias = 0 itr_converg = [] qtd_convergidos = [] fitness_med = [] #writer.writerow(['config','fitness']) print("Teste " + str(i + 1)) (it_converg, total_converg, fit_medio) = main(False, writert) convergencias += 1 if total_converg > 0 else 0 itr_converg.append(it_converg) qtd_convergidos.append(total_converg) fitness_med.append(fit_medio) #writer.writerow(('dado','valor')) writeri.writerow( (TAM_POPULACAO, total_converg, it_converg, "%.3f" % fit_medio)) #writer.writerow(('total_converg', total_converg)) #writer.writerow(('it_converg', it_converg)) #writer.writerow(('mdconvergidos', statistics.mean(qtd_convergidos))) #writer.writerow(('fitmedio', ("%.3f" % fit_medio))) # endFor #endFor oinfo.close() oteste.close()
''' 导入module.py并执行module后的main方法后,方法执行结果不是main We are in module ''' from module import main main()
def test_main_returns_None(self): self.assertEqual(None, main())
#!/usr/bin/env python import logging import json from settings import settings import module class JsonCrawl(module.Crawler): def __init__(self, path): module.Crawler.__init__(self) self.path = path def crawl(self): for l in open(self.path): yield json.loads(l) if __name__ == '__main__': module.main(JsonCrawl)
def test_main_returns_1(self): old_module_var = module.var module.var = 1 self.assertEqual(module.var, main()) self.assertEqual(2, module.var) module.var = old_module_var
#!/usr/bin/env python import logging import json from settings import settings import module class Tee(module.UserFilter): def __init__(self, path): module.UserFilter.__init__(self) self.path = path def ufilter(self, users): f = open(self.path,'w+') for user in users: user['tee'] = 1+user.get('tee',0) print>>f, json.dumps(user) yield user f.close() if __name__ == '__main__': module.main(Tee)
from module import main main()
import module module.main() # 输出We are in module
''' Documentation, License etc. @package module ''' import module module.proceedMessage() if module.updateScript(): import module reload(module) module.main()
def imp(): x = m.main() x.run_configurator()
from module import main main('aa')
def imp(): m.main()
#!/usr/bin/env python import logging import json from settings import settings import module class Tee(module.UserFilter): def __init__(self, path): module.UserFilter.__init__(self) self.path = path def ufilter(self, users): f = open(self.path, 'w+') for user in users: user['tee'] = 1 + user.get('tee', 0) print >> f, json.dumps(user) yield user f.close() if __name__ == '__main__': module.main(Tee)