thepythonbook 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- thepythonbook/__init__.py +95 -0
- thepythonbook/__main__.py +20 -0
- thepythonbook-0.1.0.dist-info/METADATA +33 -0
- thepythonbook-0.1.0.dist-info/RECORD +7 -0
- thepythonbook-0.1.0.dist-info/WHEEL +4 -0
- thepythonbook-0.1.0.dist-info/entry_points.txt +2 -0
- thepythonbook-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Turn Python code into a link that opens and runs it at pythoncompiler.io."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.0"
|
|
4
|
+
__all__ = ["link", "link_file"]
|
|
5
|
+
|
|
6
|
+
SITE = "https://pythoncompiler.io/"
|
|
7
|
+
MAX_LINK = 8000
|
|
8
|
+
_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _compress(text):
|
|
12
|
+
"""LZ-String's compressToEncodedURIComponent, so the site's editor can read it."""
|
|
13
|
+
if not text:
|
|
14
|
+
return ""
|
|
15
|
+
dictionary, pending = {}, set()
|
|
16
|
+
word, enlarge_in, next_code, num_bits = "", 2, 3, 2
|
|
17
|
+
out, value, position = [], 0, 0
|
|
18
|
+
|
|
19
|
+
def bit(b):
|
|
20
|
+
nonlocal value, position
|
|
21
|
+
value = (value << 1) | b
|
|
22
|
+
if position == 5:
|
|
23
|
+
out.append(_SAFE[value])
|
|
24
|
+
value, position = 0, 0
|
|
25
|
+
else:
|
|
26
|
+
position += 1
|
|
27
|
+
|
|
28
|
+
def bits(number, count):
|
|
29
|
+
for _ in range(count):
|
|
30
|
+
bit(number & 1)
|
|
31
|
+
number >>= 1
|
|
32
|
+
|
|
33
|
+
def write_word():
|
|
34
|
+
nonlocal enlarge_in, num_bits
|
|
35
|
+
if word in pending:
|
|
36
|
+
code = ord(word[0])
|
|
37
|
+
wide = code >= 256
|
|
38
|
+
bits(1 if wide else 0, num_bits)
|
|
39
|
+
bits(code, 16 if wide else 8)
|
|
40
|
+
enlarge_in -= 1
|
|
41
|
+
if enlarge_in == 0:
|
|
42
|
+
enlarge_in, num_bits = 2 ** num_bits, num_bits + 1
|
|
43
|
+
pending.discard(word)
|
|
44
|
+
else:
|
|
45
|
+
bits(dictionary[word], num_bits)
|
|
46
|
+
enlarge_in -= 1
|
|
47
|
+
if enlarge_in == 0:
|
|
48
|
+
enlarge_in, num_bits = 2 ** num_bits, num_bits + 1
|
|
49
|
+
|
|
50
|
+
# JavaScript strings are UTF-16, so work in the same units.
|
|
51
|
+
raw = text.encode("utf-16-le", "surrogatepass")
|
|
52
|
+
for i in range(0, len(raw), 2):
|
|
53
|
+
char = chr(raw[i] | (raw[i + 1] << 8))
|
|
54
|
+
if char not in dictionary:
|
|
55
|
+
dictionary[char] = next_code
|
|
56
|
+
next_code += 1
|
|
57
|
+
pending.add(char)
|
|
58
|
+
joined = word + char
|
|
59
|
+
if joined in dictionary:
|
|
60
|
+
word = joined
|
|
61
|
+
else:
|
|
62
|
+
write_word()
|
|
63
|
+
dictionary[joined] = next_code
|
|
64
|
+
next_code += 1
|
|
65
|
+
word = char
|
|
66
|
+
if word:
|
|
67
|
+
write_word()
|
|
68
|
+
bits(2, num_bits)
|
|
69
|
+
while True:
|
|
70
|
+
value <<= 1
|
|
71
|
+
if position == 5:
|
|
72
|
+
out.append(_SAFE[value])
|
|
73
|
+
break
|
|
74
|
+
position += 1
|
|
75
|
+
return "".join(out)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def link(code):
|
|
79
|
+
"""Return a link that opens this Python code in the online editor."""
|
|
80
|
+
if not isinstance(code, str):
|
|
81
|
+
raise TypeError("link() takes the code as a string; use link_file() for a file")
|
|
82
|
+
if not code.strip():
|
|
83
|
+
raise ValueError("there is no code to share")
|
|
84
|
+
url = SITE + "?code=" + _compress(code)
|
|
85
|
+
if len(url) > MAX_LINK:
|
|
86
|
+
raise ValueError(
|
|
87
|
+
"this code makes a link of %d characters, over the limit of %d" % (len(url), MAX_LINK)
|
|
88
|
+
)
|
|
89
|
+
return url
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def link_file(path):
|
|
93
|
+
"""Return a link that opens this Python file in the online editor."""
|
|
94
|
+
with open(path, encoding="utf-8") as f:
|
|
95
|
+
return link(f.read())
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
from . import link_file
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def main(argv=None):
|
|
7
|
+
args = sys.argv[1:] if argv is None else argv
|
|
8
|
+
if len(args) != 1 or args[0] in ("-h", "--help"):
|
|
9
|
+
print("Usage: python -m thepythonbook hello.py")
|
|
10
|
+
return 0 if args and args[0] in ("-h", "--help") else 2
|
|
11
|
+
try:
|
|
12
|
+
print(link_file(args[0]))
|
|
13
|
+
except (OSError, ValueError, UnicodeDecodeError) as err:
|
|
14
|
+
print("Could not make a link: %s" % err, file=sys.stderr)
|
|
15
|
+
return 1
|
|
16
|
+
return 0
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
if __name__ == "__main__":
|
|
20
|
+
sys.exit(main())
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: thepythonbook
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Turn a Python file into a link that opens and runs it in the browser at pythoncompiler.io.
|
|
5
|
+
Project-URL: Homepage, https://pythoncompiler.io/
|
|
6
|
+
Author: Selva Prabhakaran
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: education,online compiler,python,share code
|
|
10
|
+
Classifier: Intended Audience :: Education
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Education
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# thepythonbook
|
|
17
|
+
|
|
18
|
+
Turn a Python file into a link. Anyone who opens the link gets the code in the online editor at [pythoncompiler.io](https://pythoncompiler.io/), where it runs in their browser with nothing to install.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
pip install thepythonbook
|
|
22
|
+
python -m thepythonbook hello.py
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
From Python, including a notebook:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import thepythonbook as tb
|
|
29
|
+
|
|
30
|
+
print(tb.link("print('Hello')"))
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The code travels inside the link, so nothing is uploaded. Very long programs make links too long to open: over 8,000 characters raises an error.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
thepythonbook/__init__.py,sha256=hRdIxIMFc-49M6V1RPNE2KQhll6TSZzQojjF3ZSB7eA,2906
|
|
2
|
+
thepythonbook/__main__.py,sha256=2u0Q6EGAsWaaLr-CSjAAIqjoFTVag-kR729qTNTWG-I,534
|
|
3
|
+
thepythonbook-0.1.0.dist-info/METADATA,sha256=l-jWeznrDRrgzEAFHCm7lqiUu2aiZ-jgVGwmV9h0Eh0,1063
|
|
4
|
+
thepythonbook-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
5
|
+
thepythonbook-0.1.0.dist-info/entry_points.txt,sha256=nG73PePeSD2SdGdCU99XZ5daWb_brwT21JjY-img4q0,62
|
|
6
|
+
thepythonbook-0.1.0.dist-info/licenses/LICENSE,sha256=TrI6PLwAol4xx6JszSaIj29cYbINIjg4JbnMsTaW5qs,1074
|
|
7
|
+
thepythonbook-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Selva Prabhakaran
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|