Skip to content

Commit fa659a8

Browse files
authored
Merge pull request #333 from realpython/bpython
bpython: Initial commit (materials)
2 parents 78cd84b + f2f42c5 commit fa659a8

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

bpython/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Discover bpython: A Python REPL With IDE-Like Features
2+
3+
Files and scripts supplementing the [bpython](https://realpython.com/bpython-alternative-python-repl/) tutorial on Real Python.

bpython/adder-v1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
try:
2+
x = int(input("x = "))
3+
y = int(input("y = "))
4+
z = x / y
5+
except (ValueError, ZeroDivisionError) as ex: # noqa: F841
6+
import bpython
7+
8+
bpython.embed(locals(), banner="Post-Mortem Debugging:")
9+
else:
10+
print(z)

bpython/adder-v2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
try:
2+
x = int(input("x = "))
3+
y = int(input("y = "))
4+
import bpdb
5+
6+
bpdb.set_trace()
7+
z = x / y
8+
except (ValueError, ZeroDivisionError) as ex: # noqa: F841
9+
import bpython
10+
11+
bpython.embed(locals(), banner="Post-Mortem Debugging:")
12+
else:
13+
print(z)

bpython/custom.theme

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[syntax]
2+
keyword = M
3+
name = r
4+
comment = b
5+
string = g
6+
error = r
7+
number = B
8+
operator = c
9+
paren = b
10+
punctuation = b
11+
token = g
12+
13+
[interface]
14+
background = d
15+
output = b
16+
main = b
17+
prompt = r
18+
prompt_more = g
19+
right_arrow_suggestion = K

bpython/github_gist.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
3+
import json
4+
import os
5+
import sys
6+
from urllib.request import Request, urlopen
7+
8+
9+
def main() -> None:
10+
"""Print the URL of a GitHub gist created from the standard input."""
11+
print(create_gist(sys.stdin.read()))
12+
13+
14+
def create_gist(content: str) -> str:
15+
"""Return the URL of the created GitHub gist."""
16+
response = post_json(
17+
url="https://api.github.com/gists",
18+
data={
19+
"description": "bpython REPL",
20+
"public": False,
21+
"files": {"repl.py": {"content": content}},
22+
},
23+
headers={
24+
"Accept": "application/vnd.github+json",
25+
"Authorization": f"Bearer {os.getenv('GITHUB_TOKEN')}",
26+
"Content-Type": "application/json",
27+
},
28+
)
29+
return response["html_url"]
30+
31+
32+
def post_json(url: str, data: dict, headers: dict = None) -> dict:
33+
"""Return the JSON response from the server."""
34+
payload = json.dumps(data).encode("utf-8")
35+
with urlopen(Request(url, payload, headers or {})) as response:
36+
return json.loads(response.read().decode("utf-8"))
37+
38+
39+
if __name__ == "__main__":
40+
try:
41+
main()
42+
except Exception as ex:
43+
print(ex)

0 commit comments

Comments
 (0)