plynth / anticipate Goto Github PK
View Code? Open in Web Editor NEWExpect the unexpected, but get what you want
Home Page: http://pypi.python.org/pypi/anticipate
License: BSD 3-Clause "New" or "Revised" License
Expect the unexpected, but get what you want
Home Page: http://pypi.python.org/pypi/anticipate
License: BSD 3-Clause "New" or "Revised" License
anticipate_wrapper
does:
def __get__(self, instance, owner):
new_func = self.__call__.__get__(instance, owner)
update_wrapper(new_func, self)
return new_func
So when you @anticpate()
a method, the method will now be a partial
. inspect.argspec
does not work on partial
s so partial
should not be used. The following can be used instead:
def __get__(self, instance, owner):
if instance is None:
return self
def wrap(*args, **kwargs):
return self.__call__(instance, *args, **kwargs)
update_wrapper(wrap, self)
return wrap
class BaseClass(object):
@anticipate(int)
def foo(self):
print '%s' % self
class SubClass(BaseClass):
pass
a = BaseClass()
a.foo() # prints <BaseClass>
b = SubClass()
b.foo() # prints <BaseClass>
/pyroot/lib/python3.8/site-packages/anticipate/decorators.py:35: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()
args, _, kwargs, _ = inspect.getargspec(func)
-- Docs: https://docs.pytest.org/en/latest/warnings.html
future
library3.10-dev
to .travis.yml
to verify future compatibility@anticipate(strings=[StringField()])
def get_strings(self, strings):
Results in error:
def _each(self, func, iterable):
"""
Runs `func` over each item in the iterable and returns a list.
Returns an empty list if iterable is `None`.
"""
> if value is None:
E NameError: global name 'value' is not defined
This is because the following code uses the wrong variable:
def _each(self, func, iterable):
"""
Runs `func` over each item in the iterable and returns a list.
Returns an empty list if iterable is `None`.
"""
if value is None:
return []
return [func(obj) for obj in iterable]
When using anticipate for a list of values, it actually provides a generator, not a list. After the generator is iterated a single time, it appears to be empty.
def test_list():
@anticipate(stringz=[basestring])
def t(stringz=None):
assert list(stringz) == list(stringz)
t([])
t(['a', 'b', 'c'])
This test fails because list() can only consume stringz a single time.
Based on the notation, one would naturally expect to receive a list rather than a generator. If a generator were desired, some other syntax should be used.
This version of adapt_all resolves the issue:
def adapt_all(iter, to_cls):
"""
Returns a generator that will adapt all objects in an iterable to `cls`
"""
return [adapt(obj, to_cls) for obj in iter]
You should be able to specify required parameters so that @anticipate
will fail if there is no value for them.
@anticipate(foo=int, required=['foo'])
def something(foo):
print foo
Or maybe:
@anticipate(foo=required(int))
def something(foo):
print foo
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.