Python — Fibonacci (dataclass, generator, type hints)
A realistic Python snippet exercising type hints, an lru_cache-memoised recursive function, a generator, and a frozen dataclass — for testing syntax highlighters, linters (ruff/flake8), and formatters (black).
#!/usr/bin/env python3
"""Fibonacci utilities: a memoised recursive form and an iterative generator."""
from __future__ import annotations
from dataclasses import dataclass
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n: int) -> int:
"""Return the n-th Fibonacci number (0-indexed)."""
if n < 0:
raise ValueError("n must be non-negative")
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
def fib_sequence(count: int):
"""Yield the first `count` Fibonacci numbers, iteratively."""
a, b = 0, 1
for _ in range(count):
yield a
a, b = b, a + b
@dataclass(frozen=True)
class Report:
count: int
total: int
def average(self) -> float:
return self.total / self.count if self.count else 0.0
def main() -> None:
seq = list(fib_sequence(10))
report = Report(count=len(seq), total=sum(seq))
print(f"sequence: {seq}")
print(f"fib(20) = {fib(20)}")
print(f"average of first {report.count}: {report.average():.1f}")
if __name__ == "__main__":
main()
Specifications
- Language
- Python
- Kind
- realistic snippet
- Lines
- 45
- Encoding
- UTF-8
- Line Endings
- LF
What is a .py file?
Python (.py) is a plain-text source file for the Python programming language — a dynamically typed, indentation-structured language widely used for scripting, data science, web back-ends, and automation. A .py file is compiled to bytecode and run by the Python interpreter.
How to use this file
Use an example .py file to test syntax highlighters, linters (like flake8 or ruff), formatters (black), tree-sitter grammars, and language-detection or code-editor tooling against known-correct source.
Related files
- pyPython — Hello WorldThe classic hello-world in idiomatic Python 3, with a shebang, a module docstring, and the standard main guard — the minimal correct program for testing highlighters and interpreters.

- cC — Hello WorldThe classic hello-world in C — an include, main, and printf — for testing highlighters, C compilers, and parsers against the canonical first program.

- cC — Linked list (structs, malloc, pointers)A realistic C program: a singly linked list with a typedef struct, malloc/free memory management, pointer walking, and error handling — for testing highlighters, compilers, and static analysers.

- cppC++ — Hello WorldThe classic hello-world in C++ — iostream and std::cout — for testing highlighters, C++ compilers, and parsers.

- cppC++ — Series (class template, STL)A realistic C++ snippet with a class template, STL algorithms (max_element, accumulate), std::move, and range handling — for testing highlighters, g++/clang++, and parsers against real templates.

- cssCSS — Component styles (variables, flexbox, keyframes)A realistic component stylesheet using custom properties, flexbox, a fluid clamp() font size, color-mix(), a keyframe animation, and a media query — for testing CSS parsers, linters (stylelint), and highlighters.

Generated by generation/code_samples.py. Free for any use, no attribution required — license.