Explain Python code

Paste Python, from a one-line comprehension to a decorator, and Vibe explains what it does in plain English.

sales_report.py

Up to 1,500 characters (about 60 lines). Your code is sent to Unvibe and Google Gemini to write the explanation. Don't paste keys or passwords. Common key formats are refused.

Vibe23 lines selected

Press Explain it for the sample, or paste your own code first.

What this Python sample shows

decorators
@timed above total_by_city is shorthand for total_by_city = timed(total_by_city). Every call now goes through wrapper, which runs the real function, prints how long it took and returns its result.
generators
read_rows uses yield, so it hands back one row at a time instead of building a list. A large file never has to fit in memory at once.
comprehensions
The last line is a dict comprehension: it builds a new dictionary from totals, in city order, with each total rounded to 2 decimal places.

Good to know

  • @wraps(func) copies the original name and docstring onto wrapper. Without it, total_by_city.__name__ would be "wrapper", which makes logs and tracebacks harder to read.
  • for city, amount in read_rows(path) unpacks each row into exactly two values. A row with more or fewer commas raises ValueError.
  • totals.get(city, 0) returns 0 the first time a city appears. collections.defaultdict(float) does the same job without the .get.
  • round(total, 2) only changes what is returned. The running totals are plain floats, so use decimal.Decimal when the amounts are money that must add up exactly.

Watch out: A header row such as city,amount crashes the function, because float("amount") raises ValueError. Skip it with next(f) before the loop, or read the file with the csv module.

Explain Python code beside your editor

Unvibe is a free app for Mac and Windows. In the app there is no pasting: select code in any app and press Control+U (Ctrl+Alt+U on Windows).