Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions .glotter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,94 @@ projects:
- " *"
transformations:
- "splitlines"
base64_encode_decode:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am so thankful we moved to a config file format for the tests. I will say: I deleted my twitter account for good, and part of the reason was that tech bros constantly post rage bait. I recently saw multiple people claiming YAML is a terrible config file format, and I just don't really get it. There really was one guy going on this huge crusade about it not being typesafe, and that a turing-complete language is better for configurations. In my mind, I was thinking: wasn't setup.py one of the most painful files to setup for a project? I'll stick to my simple YAML, JSON, TOML files.

I will say the folks I've seen complaining about YAML use it in K8s, and I guess that can be annoying. I also find the GitHub Actions use of YAML to be a bit finicky. Anyway, thanks for reading my late night thoughts 😆

words:
- "base64"
- "encode"
- "decode"
requires_parameters: true
tests:
base64_encode_valid:
params:
- name: "lowercase string"
input: '"encode" "hello world"'
expected: "aGVsbG8gd29ybGQ="
- name: "long string"
input: '"encode" "They swam along the boat at incredible speeds."'
expected: "VGhleSBzd2FtIGFsb25nIHRoZSBib2F0IGF0IGluY3JlZGlibGUgc3BlZWRzLg=="
- name: "numbers"
input: '"encode" "1234567890"'
expected: "MTIzNDU2Nzg5MA=="
- name: "symbols"
input: |
"encode" "x!\\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~"
expected: "eCEiIyQlJicoKSorLC0uLzo7PD0+P0BbXF1eX2B7fH1+"
- name: "all base64 characters"
input: '"encode" "! }gggIIT55;qqs!!Gjjb??=~~2$$+;;i::x..4kk,ppnoo"'
expected: "ISAgfWdnZ0lJVDU1O3FxcyEhR2pqYj8/PX5+MiQkKzs7aTo6eC4uNGtrLHBwbm9v"
transformations:
- "strip"
base64_decode_valid:
params:
- name: "lowercase string"
input: '"decode" "aGVsbG8gd29ybGQ="'
expected: "hello world"
- name: "long string"
input: '"decode" "VGhleSBzd2FtIGFsb25nIHRoZSBib2F0IGF0IGluY3JlZGlibGUgc3BlZWRzLg=="'
expected: "They swam along the boat at incredible speeds."
- name: "numbers"
input: '"decode" "MTIzNDU2Nzg5MA=="'
expected: "1234567890"
- name: "symbols"
input: '"decode" "eCEiIyQlJicoKSorLC0uLzo7PD0+P0BbXF1eX2B7fH1+"'
expected: "x!\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~"
- name: "all base64 characters"
input: '"decode" "ISAgfWdnZ0lJVDU1O3FxcyEhR2pqYj8/PX5+MiQkKzs7aTo6eC4uNGtrLHBwbm9v"'
expected: "! }gggIIT55;qqs!!Gjjb??=~~2$$+;;i::x..4kk,ppnoo"
transformations:
- "strip"
base64_invalid_inputs:
params:
- name: "no input"
input: null
expected: "Usage: please provide a mode and a string to encode/decode"
- name: "invalid mode"
input: '"blue" "Oh look a Pascal triangle"'
expected: "Usage: please provide a mode and a string to encode/decode"
transformations:
- "strip"
base64_invalid_encode:
params:
- name: "missing string"
input: '"encode"'
expected: "Usage: please provide a mode and a string to encode/decode"
- name: "empty string"
input: '"encode" ""'
expected: "Usage: please provide a mode and a string to encode/decode"
transformations:
- "strip"
base64_invalid_decode:
params:
- name: "missing string"
input: '"decode"'
expected: "Usage: please provide a mode and a string to encode/decode"
- name: "empty string"
input: '"decode" ""'
expected: "Usage: please provide a mode and a string to encode/decode"
- name: "length number not multiple of 4"
input: '"decode" "hello+world"'
expected: "Usage: please provide a mode and a string to encode/decode"
- name: "invalid characters"
input: '"decode" "hello world="'
expected: "Usage: please provide a mode and a string to encode/decode"
- name: "too many pad characters at end"
input: '"decode" "MTIzNDU2Nzg5M==="'
expected: "Usage: please provide a mode and a string to encode/decode"
- name: "pad characters in middle"
input: '"decode" "MTIzNDU2=Nzg5M=="'
expected: "Usage: please provide a mode and a string to encode/decode"
transformations:
- "strip"
binarysearch:
words:
- "binary"
Expand Down
40 changes: 40 additions & 0 deletions archive/p/python/base64_encode_decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import base64
import binascii
import sys
from typing import NoReturn


def usage() -> NoReturn:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never actually seen this before. Is this because the function exits no matter what, so it has no chance to return None?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, NoReturn means what it says: the function never returns. The program just exits.

print("Usage: please provide a mode and a string to encode/decode")
sys.exit(1)


def base64_encode(s: str) -> str:
return base64.b64encode(s.encode("ascii")).decode("ascii")


def base64_decode(s: str) -> str:
return base64.b64decode(s.encode("ascii")).decode("ascii")


def main() -> None | NoReturn:
if len(sys.argv) < 3 or not sys.argv[2]:
usage()

mode = sys.argv[1]
s = sys.argv[2]
if mode == "encode":
result = base64_encode(s)
elif mode == "decode":
try:
result = base64_decode(s)
except binascii.Error:
usage()
else:
usage()

print(result)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion archive/p/python/prime_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def is_prime(x):
if (x % 2 == 0 and x is not 2) or (x == 1):
if (x % 2 == 0 and x != 2) or (x == 1):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

return False
return not bool([n for n in range(3, int(ceil(sqrt(x))+1)) if x % n == 0])

Expand Down
2 changes: 1 addition & 1 deletion archive/p/python/testinfo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ folder:

container:
image: "python"
tag: "3.7-alpine"
tag: "3.12-alpine"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a wonderfully good jump!

cmd: "python {{ source.name }}{{ source.extension }}"
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"

dependencies = [
"ronbun (>=0.9.0,<0.10.0)",
"glotter2 (>=0.10.1,<0.11.0)"
"glotter2 (>=0.10.2,<0.11.0)"
]

# Initiator of the collection
Expand All @@ -32,5 +32,7 @@ maintainers = [
[tool.poetry]
package-mode = false

[tool.poetry.dependencies]
glotter2 = {develop = true}
[tool.pytest.ini_options]
console_output_style = "count"