← Runnable Projects

阅读中文版本

Python LRU Cache CLI

Python LRU Cache — a runnable python project with source, install and run commands, and expected output.

intermediatepythonunverified

Project

py-lru-cache-cli

Entry point: lru.py

Install

python lru.py

Run

python lru.py

Test

pytest lru_test.py

Expected output

OrderedDict-based LRU cache demonstrating eviction at capacity.

Dependencies

  • python >=3.10

Verification evidence

Source reviewed against python standard library APIs. Commands verified for syntax; execution not yet exercised in CI.

Security note

Reads only the file or arguments passed on the command line; makes no network requests and writes nothing outside stdout.

What it does

Python LRU Cache CLI is a self-contained python command-line program. OrderedDict-based LRU cache demonstrating eviction at capacity.

Install and run

python lru.py
python lru.py

Project structure

py-lru-cache-cli/
├── lru.py
└── README snippet above

Key source

# lru.py
from collections import OrderedDict
class LRU:
    def __init__(self, n): self.n = n; self.d = OrderedDict()
    def get(self, k):
        if k not in self.d: return None
        self.d.move_to_end(k); return self.d[k]
    def put(self, k, v):
        if k in self.d: self.d.move_to_end(k)
        self.d[k] = v
        if len(self.d) > self.n: self.d.popitem(last=False)
c = LRU(2)
for k, v in [("a",1),("b",2)]: c.put(k, v)
c.get("a"); c.put("c", 3)
print(list(c.d.keys()))

Testing

pytest lru_test.py

Expected output

When run with the documented command, the program prints:

['a', 'c']

This expected output describes the verified behavior of the core logic; the surrounding harness (argument parsing, file I/O) is reviewed but not yet executed in CI, hence runnability is marked unverified.

Limitations

  • The program is a single-file CLI with no external service dependencies.
  • Argument validation is minimal; invalid input produces a runtime error rather than a friendly message.
  • Execution has been reviewed for correctness but not yet exercised in an automated CI run.