Python デザインパターン サンプルコード Interpreter
Mark Summerfield『実践 Python 3』デザインパターンのサンプルコード
Python3(3.11)で動くソースコード(.pyファイル .ipynbファイル)あります
「anaconda3」on .py「PyCharm」.ipynb「Jupyter Notebook」
(2023-11-17)Python3.11で動作確認済み
コードを修正:エラーメッセージの指示で次のようにする
sys.exit()→sys.exit
出力は,コメントアウトされているプログラムをマクロとして動作させたそのマクロの出力である。ただし,そのマクロをどのように利用できるかは不明である。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#!/usr/bin/env python3 # Copyright c 2012-13 Qtrac Ltd. All rights reserved. # This program or module is free software: you can redistribute it # and/or modify it under the terms of the GNU General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. It is provided for # educational purposes and is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. import collections import sys TRANSFORM, SUMMARIZE = ("TRANSFORM", "SUMMARIZE") Code = collections.namedtuple("Code", "name code kind") def main(): genome = 3 * GENOME for code in CODE: context = dict(genome=genome, target="G[AC]{2}TT", replace="TCGA") execute(code, context) def execute(code, context): try: exec(code.code, globals(), context) result = context.get("result") error = context.get("error") handle_result(code, result, error) except Exception as err: print("'{}' raised an exception: {}\n".format(code.name, err)) def handle_result(code, result, error): if error is not None: print("'{}' error: {}".format(code.name, error)) elif result is None: print("'{}' produced no result".format(code.name)) elif code.kind == TRANSFORM: genome = result try: print("'{}' produced a genome of length {}".format(code.name, len(genome))) except TypeError as err: print("'{}' error: expected a sequence result: {}".format( code.name, err)) elif code.kind == SUMMARIZE: print("'{}' produced a result of {}".format(code.name, result)) print() CODE = ( Code("Count", """ import re matches = re.findall(target, genome) if matches: result = len(matches) else: error = "'{}' not found".format(target) """, SUMMARIZE) , Code("Replace", """ import re result, count = re.subn(target, replace, genome) if not count: error = "no '{}' replacements made".format(target) """, TRANSFORM) , Code("Exception Test", """ result = 0 for i in range(len(genome)): if genome[i] = "A": result += 1 """, SUMMARIZE) , Code("Error Test", """ import re matches = re.findall(target * 5, genome) if matches: result = len(matches) else: error = "'{}' not found".format(target) """, TRANSFORM) , Code("No Result Test", """ # No result """, TRANSFORM) , Code("Wrong Kind Test", """ result = len(genome) """, TRANSFORM) , Code("Termination Test", """ import sys result = "terminating" sys.exit() """, SUMMARIZE) , Code("Length", """ result = len(genome) """, SUMMARIZE) ) GENOME = """TGTTAGTCGCTCCTCGGTCTAAGACATCAAAGTCGGTCTGCGCGGCTGCTCCCTTAGCGCTG CATAAGAGCGGGGCAGAGAGAGATAGGCGTTTTGACCGTGGCGAGCAAGGCGCGTCATAGTGTCGCCGTGACTG ATCCTACTGGGTTCTTGCTACTGCCCGGGTCGCAATCCAAAATCTCCACGCGCTGCCACCCCGAAGAAGATATA TGTCACTGAATTGTATTGGTAACATAGTCGAATTGGGTTCAGGTAAGTTAGTCGTTTAGCCGCTGCGACAGTGG TGGAAGGGCGAATAGTGTAAAATTTCGCCTGTTAGTGAACATTATCAGGCTGCCATCGTTGATCGCCCCTCTTA AACTCAGTCTTAAATGAGTTCCCGCCTAAGGTCATTCGTGCCTTGATGATTGATAGCTCGATTGGTCCCTTATG AAACCGGACCAGAAATGTACCCGCTGAACCGGTGTCATAAGTGTCGCCGTCCCTACGATCGACACTTCCTGAGC ACGAACGATTTGCGACGCTGTAATGCCACGAGGACTGCATTGAAGATTTTTTGTCCTAGGTGTATGTGCTTCTC AGGAAGATGCACTACGCACTCCCCTTATCACGGGTGTGACCATCAGGTAGCGTAGGAAGATTAAGACCGCGTAA CTATCCCTTTCCGTCGCACTCCGACGTCTCAGCACATGTGCGGGGGCCCCTAATTGAGAAACAGTCCATGGTTG TCCGTAAGTTTCGGAAATCAACTTCACTGCTAGATGGTTGGACGCCAAGGCTCAATAGGTTGGACTCTAAGAAG """.replace("\n", "") if __name__ == "__main__": main()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#!/usr/bin/env python3 # Copyright c 2012-13 Qtrac Ltd. All rights reserved. # This program or module is free software: you can redistribute it # and/or modify it under the terms of the GNU General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. It is provided for # educational purposes and is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. import collections import json import os import re import subprocess import sys UTF8 = "utf-8" TRANSFORM, SUMMARIZE = ("TRANSFORM", "SUMMARIZE") Code = collections.namedtuple("Code", "name code kind") def main(): genome = 3 * GENOME for i, code in enumerate(CODE): context = dict(genome=genome, target="G[AC]{2}TT", replace="TCGA") execute(code, context) if sys.version_info[:2] > (3, 1): def execute(code, context): module, offset = create_module(code.code, context) with subprocess.Popen([sys.executable, "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process: communicate(process, code, module, offset) else: def execute(code, context): module, offset = create_module(code.code, context) process = subprocess.Popen([sys.executable, "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) communicate(process, code, module, offset) def create_module(code, context): lines = ["import json", "result = error = None"] for key, value in context.items(): lines.append("{} = {!r}".format(key, value)) offset = len(lines) + 1 outputLine = "\nprint(json.dumps((result, error)))" return "\n".join(lines) + "\n" + code + outputLine, offset def communicate(process, code, module, offset): stdout, stderr = process.communicate(module.encode(UTF8)) if stderr: stderr = stderr.decode(UTF8).lstrip().replace(", in <module>", ":") stderr = re.sub(", line (\d+)", lambda match: str(int(match.group(1)) - offset), stderr) print(re.sub(r'File."[^"]+?"', "'{}' has an error on line " .format(code.name), stderr)) return if stdout: result, error = json.loads(stdout.decode(UTF8)) handle_result(code, result, error) return print("'{}' produced no result\n".format(code.name)) def handle_result(code, result, error): if error is not None: print("'{}' error: {}".format(code.name, error)) elif result is None: print("'{}' produced no result".format(code.name)) elif code.kind == TRANSFORM: genome = result try: print("'{}' produced a genome of length {}".format(code.name, len(genome))) except TypeError as err: print("'{}' error: expected a sequence result: {}".format( code.name, err)) elif code.kind == SUMMARIZE: print("'{}' produced a result of {}".format(code.name, result)) print() CODE = ( Code("Count", """ import re matches = re.findall(target, genome) if matches: result = len(matches) else: error = "'{}' not found".format(target) """, SUMMARIZE) , Code("Replace", """ import re result, count = re.subn(target, replace, genome) if not count: error = "no '{}' replacements made".format(target) """, TRANSFORM) , Code("Exception Test", """ result = 0 for i in range(len(genome)): if genome[i] = "A": result += 1 """, SUMMARIZE) , Code("Error Test", """ import re matches = re.findall(target * 5, genome) if matches: result = len(matches) else: error = "'{}' not found".format(target) """, TRANSFORM) , Code("No Result Test", """ # No result """, TRANSFORM) , Code("Wrong Kind Test", """ result = len(genome) """, TRANSFORM) , Code("Termination Test", """ import sys result = "terminating" sys.exit() """, SUMMARIZE) , Code("Length", """ result = len(genome) """, SUMMARIZE) ) GENOME = """TGTTAGTCGCTCCTCGGTCTAAGACATCAAAGTCGGTCTGCGCGGCTGCTCCCTTAGCGCTG CATAAGAGCGGGGCAGAGAGAGATAGGCGTTTTGACCGTGGCGAGCAAGGCGCGTCATAGTGTCGCCGTGACTG ATCCTACTGGGTTCTTGCTACTGCCCGGGTCGCAATCCAAAATCTCCACGCGCTGCCACCCCGAAGAAGATATA TGTCACTGAATTGTATTGGTAACATAGTCGAATTGGGTTCAGGTAAGTTAGTCGTTTAGCCGCTGCGACAGTGG TGGAAGGGCGAATAGTGTAAAATTTCGCCTGTTAGTGAACATTATCAGGCTGCCATCGTTGATCGCCCCTCTTA AACTCAGTCTTAAATGAGTTCCCGCCTAAGGTCATTCGTGCCTTGATGATTGATAGCTCGATTGGTCCCTTATG AAACCGGACCAGAAATGTACCCGCTGAACCGGTGTCATAAGTGTCGCCGTCCCTACGATCGACACTTCCTGAGC ACGAACGATTTGCGACGCTGTAATGCCACGAGGACTGCATTGAAGATTTTTTGTCCTAGGTGTATGTGCTTCTC AGGAAGATGCACTACGCACTCCCCTTATCACGGGTGTGACCATCAGGTAGCGTAGGAAGATTAAGACCGCGTAA CTATCCCTTTCCGTCGCACTCCGACGTCTCAGCACATGTGCGGGGGCCCCTAATTGAGAAACAGTCCATGGTTG TCCGTAAGTTTCGGAAATCAACTTCACTGCTAGATGGTTGGACGCCAAGGCTCAATAGGTTGGACTCTAAGAAG """.replace("\n", "") if __name__ == "__main__": main()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#!/usr/bin/env python3 # Copyright c 2012-13 Qtrac Ltd. All rights reserved. # This program or module is free software: you can redistribute it # and/or modify it under the terms of the GNU General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. It is provided for # educational purposes and is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. import collections import pickle import os import re import subprocess import sys UTF8 = "utf-8" TRANSFORM, SUMMARIZE = ("TRANSFORM", "SUMMARIZE") Code = collections.namedtuple("Code", "name code kind") def main(): genome = 3 * GENOME for i, code in enumerate(CODE): context = dict(genome=genome, target="G[AC]{2}TT", replace="TCGA") execute(code, context) if sys.version_info[:2] > (3, 1): def execute(code, context): module, offset = create_module(code.code, context) with subprocess.Popen([sys.executable, "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as process: communicate(process, code, module, offset) else: def execute(code, context): module, offset = create_module(code.code, context) process = subprocess.Popen([sys.executable, "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) communicate(process, code, module, offset) def create_module(code, context): lines = ["import pickle", "import sys", "result = error = None"] for key, value in context.items(): lines.append("{} = {!r}".format(key, value)) offset = len(lines) + 1 outputLine = "\nsys.stdout.buffer.write(pickle.dumps((result, error)))" return "\n".join(lines) + "\n" + code + outputLine, offset def communicate(process, code, module, offset): stdout, stderr = process.communicate(module.encode(UTF8)) if stderr: stderr = stderr.decode(UTF8).lstrip().replace(", in <module>", ":") stderr = re.sub(", line (\d+)", lambda match: str(int(match.group(1)) - offset), stderr) print(re.sub(r'File."[^"]+?"', "'{}' has an error on line " .format(code.name), stderr)) return if stdout: result, error = pickle.loads(stdout) handle_result(code, result, error) return print("'{}' produced no result\n".format(code.name)) def handle_result(code, result, error): if error is not None: print("'{}' error: {}".format(code.name, error)) elif result is None: print("'{}' produced no result".format(code.name)) elif code.kind == TRANSFORM: genome = result try: print("'{}' produced a genome of length {}".format(code.name, len(genome))) except TypeError as err: print("'{}' error: expected a sequence result: {}".format( code.name, err)) elif code.kind == SUMMARIZE: print("'{}' produced a result of {}".format(code.name, result)) print() CODE = ( Code("Count", """ import re matches = re.findall(target, genome) if matches: result = len(matches) else: error = "'{}' not found".format(target) """, SUMMARIZE) , Code("Replace", """ import re result, count = re.subn(target, replace, genome) if not count: error = "no '{}' replacements made".format(target) """, TRANSFORM) , Code("Exception Test", """ result = 0 for i in range(len(genome)): if genome[i] = "A": result += 1 """, SUMMARIZE) , Code("Error Test", """ import re matches = re.findall(target * 5, genome) if matches: result = len(matches) else: error = "'{}' not found".format(target) """, TRANSFORM) , Code("No Result Test", """ # No result """, TRANSFORM) , Code("Wrong Kind Test", """ result = len(genome) """, TRANSFORM) , Code("Termination Test", """ import sys result = "terminating" sys.exit() """, SUMMARIZE) , Code("Length", """ result = len(genome) """, SUMMARIZE) ) GENOME = """TGTTAGTCGCTCCTCGGTCTAAGACATCAAAGTCGGTCTGCGCGGCTGCTCCCTTAGCGCTG CATAAGAGCGGGGCAGAGAGAGATAGGCGTTTTGACCGTGGCGAGCAAGGCGCGTCATAGTGTCGCCGTGACTG ATCCTACTGGGTTCTTGCTACTGCCCGGGTCGCAATCCAAAATCTCCACGCGCTGCCACCCCGAAGAAGATATA TGTCACTGAATTGTATTGGTAACATAGTCGAATTGGGTTCAGGTAAGTTAGTCGTTTAGCCGCTGCGACAGTGG TGGAAGGGCGAATAGTGTAAAATTTCGCCTGTTAGTGAACATTATCAGGCTGCCATCGTTGATCGCCCCTCTTA AACTCAGTCTTAAATGAGTTCCCGCCTAAGGTCATTCGTGCCTTGATGATTGATAGCTCGATTGGTCCCTTATG AAACCGGACCAGAAATGTACCCGCTGAACCGGTGTCATAAGTGTCGCCGTCCCTACGATCGACACTTCCTGAGC ACGAACGATTTGCGACGCTGTAATGCCACGAGGACTGCATTGAAGATTTTTTGTCCTAGGTGTATGTGCTTCTC AGGAAGATGCACTACGCACTCCCCTTATCACGGGTGTGACCATCAGGTAGCGTAGGAAGATTAAGACCGCGTAA CTATCCCTTTCCGTCGCACTCCGACGTCTCAGCACATGTGCGGGGGCCCCTAATTGAGAAACAGTCCATGGTTG TCCGTAAGTTTCGGAAATCAACTTCACTGCTAGATGGTTGGACGCCAAGGCTCAATAGGTTGGACTCTAAGAAG """.replace("\n", "") if __name__ == "__main__": main()