mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-16 19:40:59 +03:00
Reverting of removing callable provider example
This commit is contained in:
parent
54eb60698b
commit
67fb4181f4
40
examples/providers/callable.py
Normal file
40
examples/providers/callable.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
"""`Callable` providers examples."""
|
||||||
|
|
||||||
|
from objects.providers import Callable
|
||||||
|
from objects.injections import KwArg
|
||||||
|
|
||||||
|
|
||||||
|
class SomeCrypt(object):
|
||||||
|
|
||||||
|
"""Example class SomeCrypt."""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def encrypt(data, password):
|
||||||
|
"""Encypt data using password."""
|
||||||
|
return ''.join((password, data, password))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def decrypt(data, password):
|
||||||
|
"""Decrypt data using password."""
|
||||||
|
return data[len(password):-len(password)]
|
||||||
|
|
||||||
|
|
||||||
|
# Encrypt and decrypt function providers:
|
||||||
|
encrypt = Callable(SomeCrypt.encrypt,
|
||||||
|
KwArg('password', 'secret123'))
|
||||||
|
decrypt = Callable(SomeCrypt.decrypt,
|
||||||
|
KwArg('password', 'secret123'))
|
||||||
|
|
||||||
|
# Making some asserts:
|
||||||
|
initial_data = 'some_data'
|
||||||
|
|
||||||
|
encrypted1 = encrypt(initial_data)
|
||||||
|
decrypted1 = decrypt(encrypted1)
|
||||||
|
|
||||||
|
assert decrypted1 == initial_data
|
||||||
|
|
||||||
|
# Context keyword arguments priority example:
|
||||||
|
encrypted2 = encrypt(initial_data, password='another_secret')
|
||||||
|
decrypted2 = decrypt(encrypted2)
|
||||||
|
|
||||||
|
assert decrypted2 != initial_data
|
Loading…
Reference in New Issue
Block a user