proofit404 / primitives Goto Github PK
View Code? Open in Web Editor NEWFake objects designed with OOP in mind.
Home Page: https://proofit404.github.io/primitives/
License: BSD 2-Clause "Simplified" License
Fake objects designed with OOP in mind.
Home Page: https://proofit404.github.io/primitives/
License: BSD 2-Clause "Simplified" License
from primitives import Generator, Argument, Return, Callable
gen = Callable(Generator(1, 2, 3, Return(4)), a=Argument('a'), b=Argument('b'))
it = gen(a='a', b='b')
next(it) # 1
next(it) # 2
next(it) # 3
next(it) # StopIteration: 4
from primitives import Awaitable, Callable, Throw, Argument
coro = Callable(Awaitable(1, Throw(ValueError, Argument(TypeError))))
it = coro()
await it # 1
it.throw(TypeError()) # ValueError()
from primitives import AsyncContext, Exit
resource = AsyncContext(1, Exit(True))
async with resource as r:
r # 1
raise ValueError()
# Execution continues here without exception
from primitives import Awaitable, Send, Argument
a = Awaitable(1, Send(3, Argument(2)))
it = gen()
await it # 1
it.send(2) # 3
from primitives import AsyncContext
resource = AsyncContext(1)
async with resource as r:
r # 1
from primitives import Callable, Instance
user = Instance(greet=Callable('Hello, John'))
user.greet() # Hello, John
Add comparison with mock library to the index page. Writing mock for use case below would be a nightmare:
def greet_many(repo):
for user in repo.users():
print(user.greet('Hello'))
You could try to check that third user received congratulations in French. Mock version would a homework to exercise.
All fake objects instantiated within particular scope should be used before scope will be finalized.
from primitives import Scope
with Scope() as scope:
func1 = scope.Callable(1, a=scope.Argument('x'))
func2 = scope.Callable(2, a=scope.Argument('y'))
func1(a='x') # 1
# Error (func2 was never called)
from primitives import Callable, Instance
user = Instance(User, greet=Callable())
isinstance(user, User) # True
from primitives import Callable
func = Callable('Hello, John')
func() # 'Hello, John'
from primitives import Callable
func = Callable()
func() # None
from primitives import Callable, Argument, Ignore
func = Callable('Hello, John', Argument(Ignore()))
func('John') # Hello, John
func('Kate') # Hello, John
from primitives import Context
resource = Context(1)
with resource as r:
r # 1
from primitives import Class, Instance, Callable
FkUser = Class(Instance(greet=Callable('Hello, John')))
user = FkUser()
user.greet() # Hello, John
from primitives import Class, Instance, Callable
FkUser = Class(User, Instance(greet=Callable('Hello, John')))
user = FkUser()
user.greet() # Hello, John
from primitives import Never
never = Never()
never() # Exception
never.foo # Exception
from primitives import AsyncContext
resource = AsyncContext()
async with resource as r:
r # None
from primitives import Context
resource = Context()
with resource as r:
r # None
from primitives import AsyncContext, Exit, Argument
resource = AsyncContext(1, Exit(Argument(ValueError)))
async with resource as r:
r # 1
raise ValueError() # Ok
async with resource as r:
r # 1
raise TypeError() # Error
from primitives import Context, Exit
resource = Context(1, Exit(True))
with resource as r:
r # 1
raise ValueError()
# Execution continues here without exception
from primitives import Callable, Instance, Argument
user = Instance(User, greet=Callable(foo=Argument('bar')))
# ---
# `User.greet` does not have `foo` argument.
from primitives import Class
FkUser = Class(User)
inspect.isclass(FkUser) # True
issubclass(FkUser, User) # True
fk_user = FkUser()
isinstance(fk_user, User) # True
from primitives import Callable, Throw
func = Callable(Throw(Exception()))
func() # Exception
from primitives import Callable, Argument
func = Callable(1, a=Argument('x'), b=Argument('y'))
func('x', 'y') # 1
func(b='y', a='x') # 1
from primitives import Callable, Argument
func = Callable(a=Argument('x'), b=Argument('y'))
func('x', 'y') # None
func(b='y', a='x') # None
from primitives import Generator
gen = Generator()
it = gen()
next(it) # StopIteration
from primitives import Argument, Callable
func = Callable('Hello, John', Argument('John'))
func('John') # Hello, John
func('Kate') # Exception
from primitives import Argument, Callable
func = Callable(Argument('John'))
func('John') # None
func('Kate') # Exception
from primitives import Argument, Callable
func = Callable(Argument('John'))
func(a='John') # Exception
from primitives import Generator, Callable, Throw, Argument
gen = Callable(Generator(1, Throw(ValueError, Argument(TypeError))))
it = gen()
next(it) # 1
it.throw(TypeError()) # ValueError()
from primitives import Attribute, Instance
user = Instance(name=Attribute('John'))
user.name # 'John'
from primitives import Attribute, Instance
user = Instance(User, name=Attribute('John'))
user.greet() # Hello, John
It should be possible to expect exception occurred within the context manager.
from primitives import Context, Exit, Argument
resource = Context(1, Exit(Argument(ValueError)))
with resource as r:
r # 1
raise ValueError() # Ok
with resource as r:
r # 1
raise TypeError() # Error
from primitives import Generator, Callable, Argument
genfunc = Callable(Generator(1, 2, 3), Argument('a'), Argument('b'))
it = genfunc('a', 'b')
next(it) # 1
next(it) # 2
next(it) # 3
next(it) # StopIteration
from primitives import Class, Instance, Callable, Variant
FkUser = Class(
User,
Variant(
Callable(
Instance(greet=Callable('Hello, John')),
Argument('John'),
),
Callable(
Instance(greet=Callable(Exception())),
Argument('Kate'),
),
),
)
j_user = FkUser('John')
user.greet() # Hello, John
k_user = FkUser('Kate')
user.greet() # Exception
from primitives import Callable, Instance
user = Instance(User, foo=Callable())
# ---
# `User` does not have `foo` method.
from primitives import Generator, Send, Argument
gen = Generator(1, Send(3, Argument(2)))
it = gen()
next(it) # 1
it.send(2) # 3
from primitives import Callable, Instance, Awaitable
user = Instance(greet=Callable(Awaitable('Hello, John')))
await user.greet() # Hello, John
from primitives import Class
FkUser = Class()
inspect.isclass(FkUser) # True
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.