86 lines
3.4 KiB
Python
Executable File
86 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# fortune - random fortune with categories (built-in list)
|
|
# Usage: fortune [-c CATEGORY] [--seed N] | fortune --list
|
|
import argparse, random, sys
|
|
|
|
FORTUNES = {
|
|
"wisdom": [
|
|
"The best time to plant a tree was twenty years ago. The second best time is now.",
|
|
"A journey of a thousand miles begins with a single step.",
|
|
"Knowing others is intelligence; knowing yourself is true wisdom.",
|
|
"The obstacle is the way.",
|
|
"Slow is smooth, and smooth is fast.",
|
|
"You cannot step into the same river twice.",
|
|
"What we think, we become.",
|
|
"The only true wisdom is in knowing you know nothing.",
|
|
],
|
|
"humor": [
|
|
"I told my computer I needed a break, and now it won't stop sending me KitKat ads.",
|
|
"There are only 10 kinds of people: those who understand binary and those who don't.",
|
|
"Why do programmers prefer dark mode? Because light attracts bugs.",
|
|
"My code doesn't have bugs, it has undocumented features.",
|
|
"I would tell you a UDP joke, but you might not get it.",
|
|
"Debugging: being the detective in a crime movie where you are also the murderer.",
|
|
"There is no cloud, just someone else's computer.",
|
|
"Real programmers count from 0.",
|
|
],
|
|
"tech": [
|
|
"First, solve the problem. Then, write the code.",
|
|
"Simplicity is the soul of efficiency.",
|
|
"The best error message is the one that never shows up.",
|
|
"Measure twice, cut once — then measure the binary too.",
|
|
"Cache invalidation and naming things: the two hard problems.",
|
|
"A backup you have never restored is not a backup.",
|
|
"Premature optimization is the root of all evil.",
|
|
"Ship it, then make it better.",
|
|
],
|
|
"motivation": [
|
|
"Done is better than perfect.",
|
|
"Small steps every day add up to a long way.",
|
|
"You don't have to be great to start, but you have to start to be great.",
|
|
"Focus on the next line, not the whole program.",
|
|
"Every expert was once a beginner.",
|
|
"The harder you work for something, the greater you'll feel when you achieve it.",
|
|
"Discipline is choosing between what you want now and what you want most.",
|
|
"One more commit. One more page. One more rep.",
|
|
],
|
|
}
|
|
|
|
def pick(category, seed=None):
|
|
rng = random.Random(seed)
|
|
cats = [category] if category else list(FORTUNES)
|
|
cat = rng.choice(cats)
|
|
return cat, rng.choice(FORTUNES[cat])
|
|
|
|
def selftest():
|
|
for cat in FORTUNES:
|
|
c, f = pick(cat, seed=1)
|
|
assert c == cat and f, cat
|
|
print(f"[{c}] {f}")
|
|
c, f = pick(None, seed=42)
|
|
assert c in FORTUNES and f
|
|
print("fortune selftest OK")
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(description="Random fortune with categories (built-in list).")
|
|
p.add_argument("-c", "--category", choices=sorted(FORTUNES), help="fortune category")
|
|
p.add_argument("--seed", type=int, default=None, help="seed for deterministic output")
|
|
p.add_argument("--list", action="store_true", help="list categories")
|
|
p.add_argument("--all", action="store_true", help="print one fortune from every category")
|
|
p.add_argument("--selftest", action="store_true", help="run a built-in self-test and exit")
|
|
a = p.parse_args()
|
|
if a.selftest:
|
|
selftest(); return 0
|
|
if a.list:
|
|
print("\n".join(sorted(FORTUNES))); return 0
|
|
if a.all:
|
|
for cat in sorted(FORTUNES):
|
|
print(f"[{cat}] {pick(cat, a.seed)[1]}")
|
|
return 0
|
|
cat, text = pick(a.category, a.seed)
|
|
print(f"[{cat}] {text}")
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|