5 Custom Python Decorators For Your Projects

Поділитися
Вставка
  • Опубліковано 27 січ 2025

КОМЕНТАРІ • 41

  • @djl3009
    @djl3009 Місяць тому +9

    Start (00:00:17)
    @time_logger - simple decorator (00:02:52)
    @retry - parameterised decorator (00:07:39)
    @type_check - type enforcing decorator (00:13:29)
    @debug - simple decorator (00:17:39)
    @rate_limiter (00:20:02)

  • @KeithFlint350
    @KeithFlint350 Місяць тому +29

    from neuralnine import decorators

  • @nathanedison8692
    @nathanedison8692 Місяць тому +5

    Not just useful, really helps make sense of what decorators are and how they work. Thanks!

  • @michaelthomas3235
    @michaelthomas3235 Місяць тому +7

    For the type decorator, I suggest accessing the __annotations__ property of the function and use the hints as the determinate instead of inputing the types in the decorator per function. This makes the decorator more reusable and less coupled.

  • @barmalini
    @barmalini Місяць тому +4

    thisl is wild, I never thought decorators were so easy

  • @thomasgoodwin2648
    @thomasgoodwin2648 Місяць тому +1

    Sorry it took so long to get back on this one. Been busy putting some of this to use. Thank you for once again taking the nightmare jungle of 'I don't have a clue' to 'Wow. that is So easy!'.
    I rewrote the type checker slightly so that it checks the arguments against the list of acceptable types (also checking for subclasses as well) so that the accepted_args acts more like a white list of ok arg types, rather than a strict 1 to 1 correlation.
    I've been paying more attention to type checking (and documentation, and error messaging) as my projects get bigger. It's time well spent not pulling out what little remains of my hair at some later point.
    Another thanks for including the informative error messages. It's so simple, and yet so effective at future stress reduction.
    Even after the years I've been here, you are STILL a freight-train of knowledge in it's simplest forms.
    🖖😎👍

  • @youngzproduction7498
    @youngzproduction7498 Місяць тому +1

    This is gold! 🎉 Thanks for your great effort.

  • @abeyroy007
    @abeyroy007 Місяць тому +5

    Broo you are too OP 🔥
    Keep churning out more Python content...
    *PYTHON FOREVAAAAAA !!!*

  • @davewest6788
    @davewest6788 Місяць тому +2

    Thanks for explaining decorators.

  • @MartinBlaha
    @MartinBlaha Місяць тому +1

    Very useful and well explained. Thank you 👋

  • @lowkeygaming4716
    @lowkeygaming4716 Місяць тому +3

    Thanks again for another quality content

  • @optiprime5336
    @optiprime5336 Місяць тому

    Thanks for the nice presentation of the idiomatic use of python decorators. Hint to all: If you want your original function name to appear in stacktraces instead of the name of the wrapper, just add a line with @wraps(func) before the definition of the wrapper function. For this you also need an import statement like: from functools import wraps

  • @Deadlious
    @Deadlious Місяць тому

    two comments on my side:
    1. consider usage of wraps (from functools)
    2. 16:14 consider checking kwargs, too.

  • @majidissa742
    @majidissa742 Місяць тому

    Such a good video and a perfect explanation of decorations 👍
    I just have a not about the type checking decorator, it's not really practical to defin expected types in the decorator itself, instead use the pythonic type hinting and python typing and parsing for matching types.

  • @mdata.youtube
    @mdata.youtube Місяць тому

    Very usefull video. Thank you!

  • @iSJ9y217
    @iSJ9y217 Місяць тому

    Thank you for something really useful!

  • @Mistery28543
    @Mistery28543 Місяць тому +3

    Really useful

  • @sigmata0
    @sigmata0 Місяць тому +1

    Awesome, thank you.

  • @halilibrahimkisakesen546
    @halilibrahimkisakesen546 Місяць тому +1

    Great,thanks neurol nine. I've already find a solution to some minor bugs

  • @xealit
    @xealit Місяць тому

    it's probably better to capture exceptions at the function call, not at the definition. It's a runtime thing, so should stay with the runtime. But it makes sense for scripting and API calls. I.e. it is indeed a part of the interface then: a network interface that needs retries.

  • @stanTrX
    @stanTrX Місяць тому

    pretty advanced stuff

  • @Atimska
    @Atimska Місяць тому +1

    Should the rate limiter clean up old calls.. otherwise does the calls list keep growing?

    • @Atimska
      @Atimska Місяць тому +1

      oh nevermind. I see how it gets re-assigned with only the calls within the interval. that's nice.

  • @jjolla6391
    @jjolla6391 Місяць тому

    what plugin to vim are you using?

  • @ulfrottger9171
    @ulfrottger9171 Місяць тому

    Thanks a lot!

  • @jftuga
    @jftuga Місяць тому

    Can you explain when you need to use "def decorator(func)" & "return decorator" and when you do not?

    • @peterholzer4481
      @peterholzer4481 Місяць тому +3

      It depends on whether you want to supply arguments or not. The language reference states "the result [of the decorator expression] must be a callable, which is invoked with the function object".
      So when you write
      @foo
      (note: no parentheses) then foo is just the name of the decorator function which needs to return the wrapper when called.
      But when you write
      @foo(bar=5, baz="hello")
      then foo(bar=5, baz="hello") will be called and must return something which in turn returns the wrapper when called. So in this case you need two nested functions.
      (This also explains why @foo() is not the same as @foo. Just in case you ever fell into that trap)

  • @rahulkmail
    @rahulkmail Місяць тому

    What will happen if I change value when I use, "@cache" decorator like print(process_input(6)) in any step of calling the method process_input() ?

    • @barmalini
      @barmalini Місяць тому

      it's for you to decide, because you will have to implement that derorator and there you specify what is going to happen

  • @ruvasqm
    @ruvasqm Місяць тому

    awesom!

  • @somebody___06-j3g
    @somebody___06-j3g Місяць тому

    How to Create a simple tool with UI that leverages
    AI to analyze logs of a Windows 10/11 computer to identify warnings, log off, and login time of users, the tool should automatically email a summary to
    a particular email ID.

  • @murphygreen8484
    @murphygreen8484 Місяць тому

    Is there an easy way to globally turn decorators on and off?

    • @enty-3035
      @enty-3035 Місяць тому +1

      Add an if statement to every decorators code to a veriable.

  • @Da_phuc
    @Da_phuc Місяць тому

    Nah this is wild.

  • @kopytko998
    @kopytko998 Місяць тому

    cool

  • @skewty
    @skewty Місяць тому

    Is old school synchronous code still being widely used outside of the learning sector and data science / AI? I have been seeing / writing async for over 5 years and seldom see benefit in pseudo threaded code.. Maybe when python and the GIL are removed things will change.. This video seems to be your learning journey in python from newb to intermediate.

    • @strivold4922
      @strivold4922 Місяць тому +1

      Yeah it is used, especially when Python is used inside other softwares where there is a main thread and you can't go out of it