Skip to content

Instantly share code, notes, and snippets.

@gh640
Last active December 8, 2023 05:59
Show Gist options
  • Select an option

  • Save gh640/d2ae2363e9a062adb880d9fc6bfc4a1b to your computer and use it in GitHub Desktop.

Select an option

Save gh640/d2ae2363e9a062adb880d9fc6bfc4a1b to your computer and use it in GitHub Desktop.
Prefix lines with natural numbers.
#!/usr/bin/env python3
"""Prefix lines with natural numbers."""
import sys
from typing import Literal
NUMBER_SUFFIX = "."
def main() -> str | Literal[0]:
if sys.stdin.isatty():
return "Stdin is a tty."
for i, line in enumerate(sys.stdin.readlines(), start=1):
print(f"{i}{NUMBER_SUFFIX} {line}", end="")
return 0
if __name__ == "__main__":
sys.exit(main())
@gh640

gh640 commented Dec 8, 2023

Copy link
Copy Markdown
Author

Sample output:

❯ echo "祇園精舍の鐘の声、
諸行無常の響きあり。
娑羅双樹の花の色、
盛者必衰の理をあらはす。
驕れる人も久しからず、
ただ春の夜の夢のごとし。
猛き者もつひにはほろびぬ、
ひとへに風の前の塵に同じ。" | python enumerate.py
1. 祇園精舍の鐘の声、
2. 諸行無常の響きあり。
3. 娑羅双樹の花の色、
4. 盛者必衰の理をあらはす。
5. 驕れる人も久しからず、
6. ただ春の夜の夢のごとし。
7. 猛き者もつひにはほろびぬ、
8. ひとへに風の前の塵に同じ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment