def analysis(path): global project try: project = load_from_file_path(path) except Exception as msg: print(f'Error: {msg}') global reported reported = 1 # 已分析
def test_api(self): project = load_from_file_path(example_big_scratch_file_path) x = 0 for i in project: print(i) for j in i: x += 1 print(project.stage) print(project.stage.name) x = x + len(project.stage.blocks) print(x) self.assertEqual(x, project.statistic.blocks_count) print(project) print(project.sprites) print(project.filename)
def test_loader(self): f = open(example_path, 'rb') zipfile = ZipFile(example_path) s = zipfile.read('project.json').decode() p1 = load_from_file_path(example_path) p2 = load_from_str(s) p3 = load_from_bytes(f) zipfile.close() f.close() self.assertEqual(p1.statistic.category, p2.statistic.category) self.assertEqual(p3.statistic.category, p2.statistic.category) self.assertEqual(p1.statistic.parts_count, p2.statistic.parts_count) self.assertEqual(p3.statistic.parts_count, p2.statistic.parts_count)
def test_scratch(self): scratch = load_from_file_path(example_path) self.assertIsNot(scratch, None) self.assertEqual(scratch.filename, 'example.sb3') self.assertIn('other', scratch.statistic.category) self.assertIn('事件', scratch.statistic.percent_cn) self.assertEqual(100.0, round(sum(scratch.statistic.percent.values()), 2)) self.assertEqual(len(scratch.statistic.blocks_all), scratch.statistic.blocks_count) print(scratch.statistic.category_cn) print(scratch.statistic.blocks_count) print(scratch.statistic.parts_count) print(scratch.load_time) print(scratch.build_time)
def one(): """\ 分析一个本地scratch3文件 """ project = load_from_file_path(askopenfilename()) print(project.report.txt)
""" 简单入门教程 """ from pyscratch.loader import load_from_file_path # pyscratch.loader提供了许多加载scratch文件的方法,常用的是load_from_file_path # 这个函数会返回一个scratch对象(pyscratch.scratch.Scratch),Scratch对象是这个项目的核心 # 许多scratch有关的数据都存储在这里 project = load_from_file_path('/path/to/example.sb3') # 请根据实际需求替换文件路径 print(dir(project)) # 列举出Scratch提供的属性和方法 # 可以看到,有一个statistic,输出看看有什么信息 print(project.statistic) # 再看一看statistic提供了哪些方法和属性 print(dir(project.statistic))
def test_comparator(self): project = load_from_file_path(example_path) project_copy = load_from_file_path(example_path_copy) x = project.comparator.compare(project_copy) print(x) self.assertTrue(x['code'] > 0.8)