{"dataset": "bigcode_python_code", "docid": "", "text": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n# @Date : 2015-04-20 20:58:52\n# @Author : Your Name ([email protected])\n# @Link : http://example.org\n# @Version : $Id$\n\nimport os\nimport json\n\nbooks = [\n {\n 'id': 1,\n 'dir': 'book1',\n 'title': '有些失去,不曾离开',\n },\n {\n 'id': 2,\n 'dir': 'book2',\n 'title': '架构师-2014年3月刊',\n },\n]\n\nfor b in books:\n os.chdir(b['dir'])\n book = {\n 'id': b['id'],\n 'title': b['title'],\n 'parts': list()\n }\n part_no = 0\n for fn in os.listdir(\".\"):\n if fn.startswith(\"content\") and fn.endswith(\".json\"):\n with open(fn) as f:\n pt = json.load(f)\n part = {\n 'id': part_no,\n 'title': pt['title'],\n 'contents': []\n }\n\n for cnt in pt['contents']:\n ncnt = {\n 'id': cnt['id']\n }\n if cnt['type'] in ['paragraph', 'headline', 'code']:\n ncnt['type'] = 'text'\n ncnt['text'] = cnt['data']['text']\n ncnt['format'] = {\n 'size': cnt['data']['format']['p_quote'] and 0.8 or 1,\n 'align': cnt['data']['format']['p_align'],\n 'italic': cnt['type'] == 'code' or cnt['data']['format']['p_quote'],\n 'bold': cnt['type'] == 'headline' or cnt['data']['format']['p_bold'],\n # 'indent': 0,\n }\n elif cnt['type'] == 'illus' and cnt['data']['seq']:\n ncnt['type'] = 'image'\n ncnt['src'] = 'data/book%s/image/%s.bmp' % (b['id'], cnt['data']['seq'])\n ncnt['single'] = True\n ncnt['format'] = {\n 'width': cnt['data']['width'],\n 'height': cnt['data']['height'],\n\n 'align': 'center',\n }\n else:\n print(cnt)\n part['contents'].append(ncnt)\n\n with open('part%d.json' % part_no, 'w+') as nf:\n json.dump(part, nf)\n\n book['parts'].append({'id': part_no, 'title': pt['title'], 'level': 0})\n\n part_no += 1\n\n with open('book.json', 'w+') as f:\n json.dump(book, f)\n\n os.chdir(\"..\")\n", "metadata": "{\"size\": 2537, \"lang\": \"Python\", \"max_stars_repo_path\": \"webreader/tran.py\", \"max_stars_repo_name\": \"water-law/waterlawblog\", \"avg_line_length\": 31.3209876543, \"max_line_length\": 97, \"alphanum_fraction\": 0.3563263697, \"__id__\": 9124369, \"nl_ratio\": 0.06813706183536826}", "reason": "", "annotator": "", "campaign": "", "timestamp": "2023-04-08 07:03:22.136399"} | |