Explain Python code
Paste Python, from a one-line comprehension to a decorator, and Vibe explains what it does in plain English.
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
@timedabovetotal_by_cityis shorthand fortotal_by_city = timed(total_by_city). Every call now goes throughwrapper, which runs the real function, prints how long it took and returns its result.- generators
read_rowsusesyield, 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 ontowrapper. 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 raisesValueError.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 usedecimal.Decimalwhen 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).