File tree Expand file tree Collapse file tree 5 files changed +84
-0
lines changed
Expand file tree Collapse file tree 5 files changed +84
-0
lines changed Original file line number Diff line number Diff line change 1+ # Discover bpython: Python REPL With IDE-Like Features
2+
3+ Files and scripts supplementing the [ bpython] ( https://realpython.com/binary-search-python/ ) tutorial on Real Python.
Original file line number Diff line number Diff line change 1+ try :
2+ x = int (input ("x = " ))
3+ y = int (input ("y = " ))
4+ z = x / y
5+ except (ValueError , ZeroDivisionError ) as ex :
6+ import bpython
7+ bpython .embed (locals (), banner = "Post-Mortem Debugging:" )
8+ else :
9+ print (z )
Original file line number Diff line number Diff line change 1+ try :
2+ x = int (input ("x = " ))
3+ y = int (input ("y = " ))
4+ import bpdb ; bpdb .set_trace ()
5+ z = x / y
6+ except (ValueError , ZeroDivisionError ) as ex :
7+ import bpython
8+ bpython .embed (locals (), banner = "Post-Mortem Debugging:" )
9+ else :
10+ print (z )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments