Comments (12)
Hmm; I'm not quite sure what you mean? What ideas do you have in mind?
Do you mean a decorator to make writing custom decoraotrs that create event handlers a bit easier?
from circuits.
The problem is that if you want to write a decorator which is applied after @handler
one need to
-
- copy the function metadata of the original function to the decorated function
-
- copy the code which does the arginspect to know if one have to pass the 'event' to the next function.
from circuits.
Ahh I see; so what are you ideas then?
Turn @handler
into a class that can be subclasses?
How else can decorators be extended in this way?
from circuits.
If I'm not mistaken I think you can do something like this:
def mydecorator(*args, **kwargS):
@handler(...)
def wrapper(f):
return f(...)
return wrapper
I think :)
from circuits.
Ì think a class is not necessary?!
I would add a function handler_decorator(func)
which does the necessary arginspect and use this instead of doing the arginspect in manager.py
from circuits.
e.g: circuits.web.controller.expose
from circuits.
circuits.web.controller.expose is the best example for the redundant things i want to get rid of:
if not getattr(f, "event", False):
return f(self, *args, **kwargs)
else:
return f(self, event, *args, **kwargs)
from circuits.
Yeah I agree; this is less to do with function parameter inspection and more to do with:
Do we need to send the event
object?
I'm not sure how to solve this per se (yet)
from circuits.
Got any ideas around this? I haven't really looked much further into this (yet).
from circuits.
My description skills weren't that good in 2015.
I explain again with a sample implementation:
class Foo(BaseComponent):
@handler('foo')
@my_decorator
def on_foo(self, event):
pass
@handler()
must always be the outside decorator, as it adds sets attributes at the function which makes them a handler.
on_foo()
can in circuits either take event
or not.
So my_decorator
has to implement the same logic as handler
and circuits.core.manager.Manager._dispatcher()
- knowing implementation details of circuits (unlikely: and watching on every release if something changed):
from circuits.tools import getargspec
def my_decorator(func):
@functool.wraps(func)
def decorated(self, event, *args, **kwargs):
args = getargspec(func)[0]
if args and args[0] == "self":
del args[0]
with_event = args and args[0] == "event"
if with_event:
return func(self, event, *args, **kwargs)
return func(self, *args, **kwargs)
return decorated
And my implementation must always take the event
argument (OK, probably cannot be prevented).
from circuits.
If I'm not mistaken I think you can do something like this:
def mydecorator(*args, **kwargS): @handler(...) def wrapper(f): return f(...) return wrapper
I think :)
- I want to chain multiple own decorators
- I don't want to turn the function into an handler already - because I don't know the other arguments passed to
@handler
- If I do this, I still have to evaluate the
event.event
flag and call the function differently.
from circuits.
Ahh I see; so what are you ideas then?
Turn
@handler
into a class that can be subclasses?How else can decorators be extended in this way?
No, turning it into a class is optional. Making a subclass is not my need, see the last comment.
But of course creating a class or extra functions is okay.
in PR #311 I changed the handler decorator into a class, providing .call()
and .decorate()
.
This would simplify my example to:
def my_decorator(func):
handler.decorate(func)
@functool.wraps(func)
def decorated(self, event, *args, **kwargs):
return handler.call(func, self, event, *args, **kwargs)
return decorated
from circuits.
Related Issues (20)
- Speed up more tests
- re-raise KeyboardInterrupt
- Drop Python 2 support HOT 9
- remove landscape.io tooling and references HOT 1
- SSL based tests fail on Ubuntu 20.04 HOT 3
- chunked multi-part web posts HOT 10
- 📣 Migrating off of Github 📣 HOT 5
- Migrate circuits.web HTTP parser to httoop HOT 7
- circuits.web.tools.validate_etags(autotags=True) is broken
- Migrate from Travis-CI to github actions HOT 2
- Fix Python 3.10 support HOT 2
- reduce possible XSS attack vector
- Integrate tornado
- allow coroutines with Python 3 syntax as async def functions HOT 4
- circuits.web error handling broken HOT 1
- circuits.web.tools.validate_etags(autotags=True) is broken
- Missing A record for domain circuitsframework.com HOT 9
- failing IRC byte representation
- Couple of questions the documentation seems to be lacking HOT 4
- TravisCI blocks Pull Requests HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from circuits.