-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add analytics utility functions #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||
| def parse_csv_line(line, delimiter=','): | ||||||||||||
| parts = line.strip().split(delimiter) | ||||||||||||
| return [p.strip().strip('"') for p in parts] | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def compute_moving_average(values, window): | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test coverage:
A parametrized unit test over these cases would catch regressions here.
|
||||||||||||
| if window <= 0 or window > len(values): | ||||||||||||
| raise ValueError("window must be between 1 and len(values)") | ||||||||||||
| result = [] | ||||||||||||
| for i in range(len(values) - window + 1): | ||||||||||||
| result.append(sum(values[i:i + window]) / window) | ||||||||||||
| return result | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def group_by(items, key_fn): | ||||||||||||
| groups = {} | ||||||||||||
| for item in items: | ||||||||||||
| k = key_fn(item) | ||||||||||||
| groups.setdefault(k, []).append(item) | ||||||||||||
| return groups | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def clamp(value, minimum, maximum): | ||||||||||||
| return max(minimum, min(maximum, value)) | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: When
Suggested change
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Splitting on the raw delimiter without respecting quoted fields breaks on any CSV where a field value contains the delimiter — e.g.
'a,"b,c",d'splits into four parts instead of three, and the quote-stripping on line 3 doesn't recover because the split already happened.Use
csv.readerfrom the stdlib — it handles quoted fields, embedded delimiters, and escaped quotes correctly.