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)

    • @Heavy_Lvy
      @Heavy_Lvy หลายเดือนก่อน

      ehehehe

  • @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