9 Bpython
10 bpython 발견: IDE와 유사한 기능을 갖춘 Python REPL
Real Python의 bpython 튜토리얼을 보완하는 파일 및 스크립트입니다.
10.1 파일: adder-v1.py
try:
x = int(input("x = "))
y = int(input("y = "))
z = x / y
except (ValueError, ZeroDivisionError) as ex: # noqa: F841
import bpython
bpython.embed(locals(), banner="Post-Mortem Debugging:")
else:
print(z)10.2 파일: adder-v2.py
try:
x = int(input("x = "))
y = int(input("y = "))
import bpdb
bpdb.set_trace()
z = x / y
except (ValueError, ZeroDivisionError) as ex: # noqa: F841
import bpython
bpython.embed(locals(), banner="Post-Mortem Debugging:")
else:
print(z)10.3 파일: github_gist.py
#!/usr/bin/env python
import json
import os
import sys
from urllib.request import Request, urlopen
def main() -> None:
"""Print the URL of a GitHub gist created from the standard input."""
print(create_gist(sys.stdin.read()))
def create_gist(content: str) -> str:
"""Return the URL of the created GitHub gist."""
response = post_json(
url="https://api.github.com/gists",
data={
"description": "bpython REPL",
"public": False,
"files": {"repl.py": {"content": content}},
},
headers={
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {os.getenv('GITHUB_TOKEN')}",
"Content-Type": "application/json",
},
)
return response["html_url"]
def post_json(url: str, data: dict, headers: dict = None) -> dict:
"""Return the JSON response from the server."""
payload = json.dumps(data).encode("utf-8")
with urlopen(Request(url, payload, headers or {})) as response:
return json.loads(response.read().decode("utf-8"))
if __name__ == "__main__":
try:
main()
except Exception as ex:
print(ex)