Real-world Python code optimizations
In this video I explain the optimizations for the upcoming version of md-toc (8.2.1). You'll see an in-depth analysis about some design decisions.
I'll talk about list comprehensions, type annotations, dictionaries and hash functions
md-toc is a Python program capable of generating clickable table of contents for markdown files.
Links
md-toc: https://github.com/frnmst/md-toc
PEP 589 – TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys: https://peps.python.org/pep-0589/
PEP 3107 – Function Annotations: https://peps.python.org/pep-3107/
TimeComplexity - Python Wiki: https://wiki.python.org/moin/TimeComplexity
Memory-usage of dictionary in Python? - Stack Overflow: https://stackoverflow.com/questions/6579757/memory-usage-of-dictionary-in-python
CHAPTERS
0:00 Intro
0:26 Transform repeated lines of code into a for loop
1:41 Transform the for loop into a list comprehension using the assignment operator (PEP 572): in this case the list comprehension is not the best solution
3:08 Having a look at some examples of the assignment operator (PEP 572)
3:41 Complex typing annotations: passing from manually checking the variable types to simple type annotations. This saves lots of boliterplate code
4:32 PEP 484: Type Hints. Seeing various examples in the document and in md-toc's code
5:37 The new TypedDict definitions in md-toc and the `typing` Python documentation examples
8:02 Caveats for TypedDict definitions: to be valid TypedDict class attributes, the dictionary keys must have the same properties of normal Python variables
9:17 Overview of PEP 589
9:43 For a for loop to a dict comprehension
10:41 Using a hash function in the key before saving a key-value in a dict: improve memory usage by potentially a lot at the cost of some speed. Example that compares a normal key with a hashed key in terms of memory usage
14:55 Outro
#markdown #md-toc #python #solvecomputerscience
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Real-world Python code optimizations», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.