mCoding
mCoding
  • Видео 118
  • Просмотров 16 544 109
Python itertools - The key to mastering iteration
The key to iteration in Python
A key feature of Python is it's incredible support for lazy iteration. Defining and consuming lazy sequences in Python are easy. You can even define an infinite sequence like powers of 2 or prime numbers! To make working with iterables in Python even easier, Python provides some basic building blocks to help you compose iteration primitives in different ways, which is what the itertools module is for. This module comes with 21 different primitives (and many more recipes for how to combine them), which we go over in this video. Will you use them?
― mCoding with James Murphy (mcoding.io)
Docs: docs.python.org/3/library/itertools.html
Source code: github.com/mCodin...
Просмотров: 7 139

Видео

All 71 built-in Python functions
Просмотров 43 тыс.21 день назад
How many did you know? A quick rundown of EVERY single one of the 71 builtin Python functions. Technically, these are not all functions, but these are the 71 callables that are listed in the Python docs as "Builtin functions". These are the global names that are available to call that you don't need to install or even import anything to use. ― mCoding with James Murphy (mcoding.io) Docs: docs.p...
Top 5 IDE Productivity Hacks That Will Save You Time Programming
Просмотров 25 тыс.Месяц назад
Save time programming With all the tools available to us in modern IDEs, being a more productive programmer isn't about typing faster. Instead, knowing and taking advantage of powerful IDE features can automate a lot of the mundane and error prone tasks that waste your time, allowing you to get on with the good stuff. In this video we take a look at my top 5 IDE productivity hacks that save you...
What you need to know about Startup and Shutdown Events in FastAPI and ASGI Applications
Просмотров 9 тыс.Месяц назад
Get started in FastAPI, Starlette, and ASGI! How do startup and shutdown events work in FastAPI, Starlette, and other ASGI applications? ASGI defines startup and shutdown events through the "lifespan" of an application, which it models as a connection between the server and the app. In this video we see the right way to do it in FastAPI, Starlette, and then understand the lifespan connection ty...
Async application classes vs. functions
Просмотров 10 тыс.Месяц назад
To function or to class? According to the spec, an ASGI application is an async callable, but when we use FastAPI, Starlette, or other ASGI frameworks, that callable is hidden in plain sight. In this video, we see how it's possible to use an application class instead of a literal async def function, and we go over reasons for preferring the class approach. ― mCoding with James Murphy (mcoding.i...
Asynchronous Web Apps in Python
Просмотров 28 тыс.Месяц назад
Building your future web app. Stop copy-pasting snippets to piece together a FastAPI app you don't really understand. Instead, buckle down and take the time to learn the fundamental building blocks that async web app frameworks in Python are built on top of. That way, you won't be stuck when there's no tutorial to copy from. ― mCoding with James Murphy (mcoding.io) Source code: github.com/mCodi...
Windows Command Escape Vulnerability - Critical CVE ... or is it?
Просмотров 25 тыс.2 месяца назад
Critical! ... or is it? CVE-2024-24576 is a freshly reported 10/10 critical CVE affecting Rust, Python, and many other programming languages on Windows that, if exploited, can allow a malicious user to execute arbitrary code as the current user. The 10/10 rating is the worst severity that can be given to a CVE. However, of 9 affected programming languages, 5 have chosen to either not fix the CV...
Async for loops in Python
Просмотров 58 тыс.2 месяца назад
Await for it! A deep dive into the purpose of the async for loop in Python. How to use the async for loop, why to use the async for loop, and how to write your own async iterators using both async generators as well as by learning the async iterator protocols and writing your own async iterator. We'll see an example ASGI app that computes the SHA256 hash of an uploaded file using Starlette and ...
The ins and outs of context managers and try-finally in Python
Просмотров 29 тыс.3 месяца назад
You can't forget! Context managers (used with the "with" statement) and try-finally are both constructs in Python that help you ensure certain cleanup code runs even in the presence of exceptions. Becoming familiar with these constructs is important for developing good coding habits in Python. Don't put the burden of remembering how to do cleanup on yourself, use a context manager to let the ob...
Python Debugging (PyCharm + VS Code)
Просмотров 35 тыс.4 месяца назад
Step by step through your program. Debugging is an essential and unavoidable part of programming. Everyone writes bugs, and we'd like to get rid of them. Print statements are fine to a certain point, but live debugging is usually a better option. With live debugging, you can step through your program one statement at a time, and set breakpoints at interesting places to stop, view, and interact ...
Modern Python logging
Просмотров 155 тыс.5 месяцев назад
A logging tutorial. At some point, print statements aren't enough. When that time comes in Python, you should reach for the builtin logging package. It may be old (committed in 2002!), but it is the standard in Python. Unfortunately though, it being so old means that it's full of stuff you shouldn't use, it doesn't follow modern conventions like PEP8 coding style, and many tutorials are vastly ...
How mathematicians prove something completely obvious.
Просмотров 16 тыс.5 месяцев назад
Zero, destroyer of things. We take for granted that 0 times anything equals 0. But why? You might think of counting numbers, but we use 0 for a lot of different systems like naturals, reals, modular arithmetic, function spaces, and in all these cases we still have 0 times anything equals 0. Something about 0 makes this happen in many different spaces. In this video we see the proof and discover...
Cloud Imports in Python
Просмотров 45 тыс.6 месяцев назад
Imagine and it is possible! Installing dependencies is such a pain! What if we could just point to a GitHub repo and just use whatever code is there? Python is a dynamic language after all, why do I need to install before running my code? Well with cloud imports you can eliminate the need for installing! Create modules on the fly without ever installing a thing! No need to mess with pesky virtu...
Bloom Filters
Просмотров 53 тыс.7 месяцев назад
Thanks to Hostinger: hostinger.com/mcoding Use coupon code MCODING at checkout to get an additional 10% off! Bloom filters are a simple probabilistic kind of set that exemplify how using probability to your advantage can result in huge performance wins at scale. By giving up some of the flexibility of a full set interface and even getting the wrong answer to "is this element in the set?" on a s...
Actually, you CAN divide by zero.
Просмотров 254 тыс.7 месяцев назад
Yes, it's possible! You've probably heard that you "can't" divide by zero, but why not? As it turns out, adding in the inverse of a number is a well-defined process in math, similar to how you can add in the solution to x^2 = -1. The result is a new number system. In this video, we find out what happens when you apply this process to add division by zero. The result is a pleasant mix of surpris...
Python 3.12 is HERE!
Просмотров 156 тыс.8 месяцев назад
Python 3.12 is HERE!
Lambda in a Loop is a Code Smell
Просмотров 61 тыс.8 месяцев назад
Lambda in a Loop is a Code Smell
A forbidden Python technique to put ANYTHING in a dict or set.
Просмотров 54 тыс.10 месяцев назад
A forbidden Python technique to put ANYTHING in a dict or set.
Don't make this big O mistake!
Просмотров 40 тыс.11 месяцев назад
Don't make this big O mistake!
Why I prefer attrs over dataclasses
Просмотров 62 тыс.11 месяцев назад
Why I prefer attrs over dataclasses
type(obj) vs. obj.__class__ in Python, and changing an object's class.
Просмотров 23 тыс.Год назад
type(obj) vs. obj. class in Python, and changing an object's class.
Python's collections.abc | InvertibleDict
Просмотров 45 тыс.Год назад
Python's collections.abc | InvertibleDict
unique_ptr is NOT just for heap allocations | custom deleters
Просмотров 12 тыс.Год назад
unique_ptr is NOT just for heap allocations | custom deleters
How to check whether a file exists in Python
Просмотров 28 тыс.Год назад
How to check whether a file exists in Python
unique_ptr: C++'s simplest smart pointer
Просмотров 41 тыс.Год назад
unique_ptr: C 's simplest smart pointer
Python's ternary operator
Просмотров 39 тыс.Год назад
Python's ternary operator
21 MORE nooby Python habits
Просмотров 113 тыс.Год назад
21 MORE nooby Python habits
str vs bytes in Python
Просмотров 77 тыс.Год назад
str vs bytes in Python
Intro to async Python | Writing a Web Crawler
Просмотров 75 тыс.Год назад
Intro to async Python | Writing a Web Crawler
Extending Python's Number hierarchy
Просмотров 29 тыс.Год назад
Extending Python's Number hierarchy

Комментарии

  • @jacanchaplais8083
    @jacanchaplais8083 Час назад

    I think there's a bug in your multi_accumulate example. You'd want the first argument of itertools.accumulate to be iterator, not iterable, right? For non exhaustable iterables, like range or sequences, you will end up counting the first element twice. Not an issue for min or max, but you'll definitely see an issue if you use the running sum example. iterator will have that initial value removed, though, so using that instead should solve the problem.

  • @LambdaCreates
    @LambdaCreates Час назад

    Python 3.14 will be the Pi update.

  • @jacanchaplais8083
    @jacanchaplais8083 Час назад

    The combinatorics things are useful to me, but my use case is somewhat mathematical. In physics, you may want to solve an N body problem. That generally requires calculating N² interaction terms, for each pair of bodies. If eagerly calculated, this would correspond to a N×N matrix. However, there is usually some redundancy. First of all, self-interaction terms often can be omitted. So you can skip computing the diagonals. Also, the pairwise interactions are often symmetric. For example, the Earth pulls the moon with the same force as the moon pulls the Earth. So to avoid repeated computations, you can compute just the upper or lower triangles of the matrix. If you want to do the calculation lazily, you can just use itertools.combinations(bodies, 2), and it gives you exactly that triangle - very efficient. If you care about self-interactions, you can use itertools.permutations(bodies, 2), and the triangle including the diagonal will be iterated over. If the interaction is not symmetric, you do the most general case of computing everything in a nested for loop, or like you showed, itertools.product(bodies, bodies).

  • @southernsunreviews5932
    @southernsunreviews5932 Час назад

    On types or dictionary question you asked about. In c# we have enums that you r able to cast the value or str. And values could change depending on what the enum is used for. Then you want to use kwargs because you don't know how the structure changed. Like in reflection scenarios. Thanks great update And you explanation about square and override is a nice explanation of polytechnic methods if they were wondering how polymorphism relate back to real life scenarios instead of foo

  • @DrGreenGiant
    @DrGreenGiant 2 часа назад

    What is the calling syntax with the colon in it?? I've never seen this before in an argument at the call site except for a lambda. It's almost like type hinting or creating an inline dict without braces. Cheers Eg at video time 16:13 you have itertools.collections(data, r:2)

  • @blitzarsun
    @blitzarsun 2 часа назад

    I was hoping you would do itertools. Please do functools too!

  • @johnlabonte-ch5ul
    @johnlabonte-ch5ul 3 часа назад

    The problem in Calculus is the limit. Words like approaching do not prove something will ever equal. "As close as desired" might mean a number "next to". Then the Archimedean property takes over. It is difficult, using base 10 digits, to show that there are numbers between ".99..." and 1. Each number base has this problem. (".(b-1)(b-1)..." approaches 1). You could then find many numbers between ".99..." and 1, one in each number base higher than 10. (1/16^n, 1/20^n or 1/60^n are closer to 0 than 1/10^n) [What about "at infinity"? What is infinity? Infinity is incomplete, inconsistent and indefinite]

  • @resatcanerbas3541
    @resatcanerbas3541 3 часа назад

    Some of them are quite useful, but as a developer, I need to break my habits first to remember the use case and instead of loops go to the itertools.

  • @MLGJuggernautgaming
    @MLGJuggernautgaming 3 часа назад

    In the thumbnail you’re missing a )

  • @MilChamp1
    @MilChamp1 3 часа назад

    I have used itertools.combinations a lot for fuzzy matching of names. If I have a list of cities input by a human along with a lookup dictionary, I can fuzzy-match all the combinations and then lazily pick the top match for each city.

  • @BlackDroid003
    @BlackDroid003 3 часа назад

    If you need a list with a given length (eg for some sort of buffer/storage), instead of list(itertools.repeat("X", 4)) You could also use ["X"] * 4 Not sure if there are any notable performance differences, but you need one less import, and its shorter

    • @IceArdor
      @IceArdor 2 часа назад

      Everything in itertools was written to be lazy because iterables may be have infinite items or may have a finite number of items that exceeds the amount of RAM. repeat("X", 1_000_000_000) versus ["X"] * 1_000_000_000 Additionally, you may not know ahead of time how many times you need to repeat that element, and iterators don't specify a length (and shouldn't be eagerly consumed). This starts to matter once you've adopted fully lazy iterators in your code: excel_column_A_cells = zip(repeat("A"), count(1)) yields A1, A2, A3, ... You couldn't implement this with an explicit list.

  • @joshix833
    @joshix833 3 часа назад

    I've created a typed_stream library to use the most important lazy functions from itertools as methods on a Stream class. That's far more readable imho than the functions

  • @guidodinello1369
    @guidodinello1369 4 часа назад

    Ive found those functions useful while doing simple grid searching where you test combinations of hyperparameters to tune ml models.

  • @luketurner314
    @luketurner314 5 часов назад

    The combinatoric ones are useful for Sudoku variant helper tools

  • @treelight1707
    @treelight1707 6 часов назад

    I think I used to use minmax from more_itertools to get both in a single pass. combinations is one of the most useful itertools for graphs, and pairwise comparisons, albeit I tend to use numpy hacks more because of performance issues.

  • @zeropointer125
    @zeropointer125 7 часов назад

    I would have used those combinatorics functions (and even zip longest) for advent of code challenges. Probably not any real world code

  • @dcmayo
    @dcmayo 7 часов назад

    I use the combinatorial ones a lot, often for testing things. I use all four of them. I have 5 versions of networking device firmware, and I want to make sure they're all compatible with each other, so I iterate over all the pairs of combinations_with_replacement. I have 10 devices networked together, and I want to test throughput for all possible paths. That's combinations, or permutations if I want to test both directions. I have 5 versions of firmware and 3 models of devices, and I want to make sure all firmwares work on all devices, so I iterate over the product of firmwares and models

  • @chair547
    @chair547 8 часов назад

    When she iterate on my object till I throw

  • @jevandezande
    @jevandezande 8 часов назад

    If you want more itertools, I recommend the more-itertools library.

  • @jevandezande
    @jevandezande 8 часов назад

    filterfalse makes sense when you have a function defined elsewhere that you are using (e.g. filter(my_func, it)). To reverse the conditional would require writing a new function or adding a lambda (e.g. filterfalse(lambda x: not my_func(x), it)), which is ugly.

    • @syrupthesaiyanturtle
      @syrupthesaiyanturtle 6 часов назад

      the lambda isn't ugly

    • @IceArdor
      @IceArdor 3 часа назад

      ⁠@@syrupthesaiyanturtleit's an extra function call and requires giving a name to the temporary arguments. Python's functools doesn't have a nice compose function, otherwise we'd have: filter(compose(operator.not, my_func), iterable) But none of that matters because a good portion of the Python itertools library relies on filterfalse being defined, so why not make that function public if they had to implement it anyways.

    • @syrupthesaiyanturtle
      @syrupthesaiyanturtle Час назад

      @@IceArdor why not just add a parameter to the filter function instead of creating a new one entirely?

    • @crayyy_zee
      @crayyy_zee 8 минут назад

      ​@@syrupthesaiyanturtlea new parameter, presumably something called "presume_false" would be uglier

  • @DrDeuteron
    @DrDeuteron 8 часов назад

    filterfalse is necessary b/c sometimes your predicate is "bool", and lambda x: not x is blech.

  • @Filup
    @Filup 8 часов назад

    I would love a comprehensive video that goes over the more_itertools library. It has become a massive go-to for me

  • @DrDeuteron
    @DrDeuteron 8 часов назад

    2:25 a "real" dot product returns this: (abs(u + v)**2 - abs(u - v)**2) / 4

  • @replicaacliper
    @replicaacliper 9 часов назад

    been waiting on this one

  • @Talon_24
    @Talon_24 9 часов назад

    5:05 There's now batched and 11:24 pairwise in the standard itertools? That's fantastic, i had to write these so many times🎉 14:40 I feel i'm using zip_longest less often than zip, but still regularily

  • @BenGroebe
    @BenGroebe 10 часов назад

    I used zip_longest recently. I needed to vertically display two lists side by side in a GUI, and there was actually very little chance they'd be the same length. zip_longest with fill="", then ' '.join

  • @Liam_The_Great
    @Liam_The_Great 10 часов назад

    "iterable" doesn't sound like a word anymore

    • @zanus5591
      @zanus5591 2 часа назад

      "irrbl"

    • @kilianvounckx9904
      @kilianvounckx9904 Час назад

      It's called semantic satiation. Pretty interesting concept

    • @crayyy_zee
      @crayyy_zee 10 минут назад

      ​@@kilianvounckx9904 TIL

  • @NotAUtubeCeleb
    @NotAUtubeCeleb 10 часов назад

    Itertools group by is reminiscent of how old fashion Hadoop works with parallel key, value operations

  • @QuantumHistorian
    @QuantumHistorian 10 часов назад

    The combinatorics functions are pretty damn useful IMO. Sure, it might "just" be for maths stuff, but the range of computational maths problems that involves them is vast! It's essentially the answer to the question "what are all the ways to put these inputs together?" which is super generic.

  • @knut-olaihelgesen3608
    @knut-olaihelgesen3608 10 часов назад

    Do you use the Rye package manager for Python? I found it very awesome to use. Much better than pip (uses uv under the hood), handles .venv gracefully, and managing dependencies is not a pain anymore. If you haven't checked it out, I strongly recommend it! Love your videos so much

  • @jamesarthurkimbell
    @jamesarthurkimbell 10 часов назад

    These are mega useful for Leetcode and CodeWars type problems, second only to the king collections.Counter

  • @314Labs
    @314Labs 10 часов назад

    at 4:41 I'm assuming "min" and "max" are passed to "accumulate" and not the "list" constructor

  • @GanerRL
    @GanerRL 11 часов назад

    for the dot product, using my transpiler: dot = 𝚲𝔸⍉ᴍΠ´Σ (which ends up as: (lambda*_1d538,**_1d542:(_03a3(_1d0d(_2349(_1d538),_03a0),NULL)))) lol

  • @TotalTimoTime
    @TotalTimoTime 11 часов назад

    11:45 For anyone wanting to use the recipes: They are all implemented in more-itertools so you can import that and access those recipes as functions directly

  • @jesavius
    @jesavius 11 часов назад

    Nice! But I want MORE! I SAID MORE!!! As in more-itertools, of course.☺

    • @johnnyq4260
      @johnnyq4260 10 часов назад

      If you could make this comment recursive it'd be really cool.

    • @IceArdor
      @IceArdor 3 часа назад

      The itertools recipes are available in the more-itertools package. Rather than copy-pasting the recipes, I just pip install more-itertools and I'm done.

  • @QuantumHistorian
    @QuantumHistorian 11 часов назад

    How come all the comments are porn bots? And who's this Scott they're all referring to?

    • @avasam06
      @avasam06 11 часов назад

      That's just modern day RUclips. Dislike and report.

    • @trustytrojan
      @trustytrojan 10 часов назад

      luckily they all got deleted, or maybe youtube is filtering them from my view

    • @johnnyq4260
      @johnnyq4260 10 часов назад

      I don't see them. Maybe RUclips is showing them only to frequent visitors to porn sites?😂

    • @felixfourcolor
      @felixfourcolor 9 часов назад

      You would see them if you're early. I reported all of them, and I suppose many other early viewers did, so I guess YT has removed them.

    • @squishy-tomato
      @squishy-tomato 8 часов назад

      Yeah - stupid bots responses are only removed after many users report them. A technical response I take 3 minutes writing gets banned automatically within seconds. I think I should submit my CV to youtube, because it doesn't seem like they know what they're doing.

  • @supermonkeyqwerty
    @supermonkeyqwerty 11 часов назад

    i've been writing `batched` myself or using `chunked` from more_itertools for much too long...

  • @Veptis
    @Veptis 11 часов назад

    I am working on a graphics applicaiton, and while I only need to render a frame 165 times per second, there is five render passes. So each pass can at most be like 1ms - so I need some better profiling tools

  • @ragacola
    @ragacola 14 часов назад

    when i use multi processes and build an .exe file from the python file using pyinstaller, I get an infinite loop of proceses and eventually my PC crashes. Do you know why this is? Ive tried Process.pool, ProcessPoolExecutor, ect.. When i use threads instead of processes, the exe works fine.

  • @peterjurak6738
    @peterjurak6738 15 часов назад

    Pydantic is better in my opinion

    • @mCoding
      @mCoding 13 часов назад

      Check out my video comparing pydantic as well!

  • @user-kf5vm8ok7p
    @user-kf5vm8ok7p 16 часов назад

    0.99999999999999999...... = 1 - 1/infinity

  • @RSLT
    @RSLT День назад

    ANYWAY good Job and excellent content. liked and Subscribed ❤

  • @RSLT
    @RSLT День назад

    Ok, good and 💪 strong strart. Then 1/10^n smaller than 1/n and smaller than epsilon. hence proof!?😮

  • @DJStompZone
    @DJStompZone День назад

    r"^\s+?(?!#)(import\s+\w+|from\s+\w+\s+import\s+\w+)" I get what you're saying, but finding local imports with regex might not have been the best example to use, since that one would actually be pretty easy. You would just need to match "(from ...) import ...", preceded by at least one whitespace character. EZPZ

    • @mCoding
      @mCoding 18 часов назад

      Your regex matches a multiline string that contains a local import! Like """ def f(): import x """ Try again!

  • @marcgentner1322
    @marcgentner1322 День назад

    love the full and simple explanaition. i use staticmethods when appling MVC structure to my api's from the user_model UserModel class i need only sertain methods from the UserModel class. so i can call whathever query method i need in my controllers.

  • @trueplexx
    @trueplexx 2 дня назад

    Howly cow, 3h of debugging because I wrote "handler" in my json instead of "handlers"

    • @mCoding
      @mCoding 2 дня назад

      Dang! Maybe a TypedDict type hint for the logging config would be a good idea to prevent those kinds of typos.

  • @DeepFriedOreoOffline
    @DeepFriedOreoOffline 3 дня назад

    I am not sure why it's a nooby mistake to write your own code when there's an existing algorithm. Sure, it can show inexperience in not knowing that there is an existing algorithm. But right before seeing this video I was watching a video reviewing a Ray Tracing engine. That engine used accumulate. The person doing the review was confused why the debugger showed that ~40% of the compute time of this extremely slow Ray Tracer was happening inside of accumulate, when the math itself was under 1%. So he replaced the function with a for loop and brought the render time from over 7 minutes, down to ~20 seconds.

    • @mCoding
      @mCoding День назад

      I think I know the video you're referring to, by the Cherno, right? I'm not sure why this has become a popular reference against the use of the stl, but that was very much an instance of the author writing code that the compiler was not able to optimize (with the optimization settings he used), not a problem with the performance of the stl. Basically he used accumulate where it does not make sense at all to use accumulate. The error is akin to sorting a vector and grabbing the first element to get the min and then complaining that std::sort is slow, when in reality sort is just not the right tool for finding the min. An important part of using tools is knowing which tool to use even when many of them *can* be used.

    • @DeepFriedOreoOffline
      @DeepFriedOreoOffline День назад

      @@mCoding That's the one, yea. I didn't realize it was a popular reference, I was just genuinely curious as someone who enjoys taking time to re-invent the wheel to learn from things I would otherwise not properly understand. I do understand what you're saying though, a toolbox is only as useful as it's artisan. I would definitely rather use vector, or deque, than make my own in most cases. Thank you for the explanation!

  • @Megaloblocks
    @Megaloblocks 3 дня назад

    1. wrong they are as readable or better readable?????

  • @its_lucky252
    @its_lucky252 4 дня назад

    x = 10 10x = 100 10x + 9 = x 9x = -9 x = -1 any number = -1

  • @SummerFrost23
    @SummerFrost23 5 дней назад

    1/ε → + ∞ Claiming n > 1/ε is saying we have number larger than infinity. Is the reasoning logical? You also mentioned " x approaches 1" , but not showing any proof " x equal one".

    • @Chris-5318
      @Chris-5318 5 дней назад

      That was gibberish. 0.999... := lim n->oo 0.999...9 (n 9s) = lim n->oo 1 - 1/10^n = 1. He did prove that final limit starting at 6:47