Stdlib Recipes¶
Read and write a text file¶
fs.write("notes.txt", "hello")
text = fs.read("notes.txt")
print(text)
Work with JSON¶
payload = {"name": "aja", "score": 10}
raw = json.encode(payload)
parsed = json.decode(raw)
print(parsed["name"])
HTTP GET¶
body = http.get("https://example.com")
print(length(body))
HTTP request with status/header/body¶
res = http.requestEx("GET", "https://example.com")
print(res.status)
print(res.body)
Environment variable with fallback¶
mode = os.getenv("APP_MODE")
if mode == "":
mode = "dev"
print(mode)
Path operations¶
full = path.join("logs", "app.txt")
print(path.dirname(full))
print(path.basename(full))
Random number in range¶
rand.seed(123)
n = rand.int(1, 10)
print(n)