Commit a3853ae
authored
Here is an optimized version of your Python program. Major optimizations.
- Caches the result of `guess_type` per unique URL using `functools.lru_cache`, which reduces repeated MIME type computations (especially on large scale repeated calls).
- Since the class is supposed to inherit from `FileUrl`, it is best to avoid repeating the dataclass and repr decorators if already present in the parent (maintaining runtime correctness and consistency).
- Removed imports that are not used in this file to reduce module loading time.
- The code preserves all functionality and the original function signatures.
#### Notes.
- The `_guess_type_cached` helper is a staticmethod, so it's shared across all instances and efficiently caches guess_type results.
- If your usage pattern always has unique URLs, set `maxsize=None` to cache unlimited.
- This optimization especially benefits use-cases where the same URL may have its media-type inferred more than once.
- The `dataclass` and `repr` decorators are *not required* here because `FileUrl` already establishes the base data model and behaviors for you.
1 parent 7dc9ff0 commit a3853ae
1 file changed
+7
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
312 | 313 | | |
313 | 314 | | |
314 | 315 | | |
315 | | - | |
| 316 | + | |
316 | 317 | | |
317 | 318 | | |
318 | 319 | | |
| |||
329 | 330 | | |
330 | 331 | | |
331 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
332 | 338 | | |
333 | 339 | | |
334 | 340 | | |
| |||
0 commit comments