def get(self, interface: type): if interface in self.literals.keys(): return self.literals[interface] if typing_meta_helper.is_typing_meta_collection(interface): return self.get_all(interface.__args__[0]) stored_component = Stream.of(self.components, self.providers) \ .firstMatch(lambda x: interface in x) \ .map(lambda x: x[interface]) \ .orElseThrow(InstantiationException("Could not instantiate {}.".format(interface))) scope = self.__get_scope(stored_component) if scope is not None and scope.is_stored(stored_component): return scope.get(stored_component) instance = None if interface in self.providers.keys(): instance = self.__inject_function(self.providers[interface]) else: constructor = stored_component.__init__ instance = self.__inject_constructor(stored_component, constructor) if scope: scope.store(stored_component, instance) return instance
def create_route_handlers(self, request: Request) -> Iterable[RouteHandler]: route_registrations = Stream(self.routing_rules) \ .map(lambda rule: rule.get_route_registrations(request.path)) \ .flat().toList() routable_http_methods = Stream(route_registrations).map( lambda route: route.http_method).toSet() if len(route_registrations) == 0: raise UnknownPathException(request.path) if self.is_cors_request( request) and OPTIONS not in routable_http_methods: return Stream.of( self.cors_handler_factory.create_cors_preflight_handler( request.path)) if request.method_annotation not in routable_http_methods: raise MethodNotAllowedException() return Stream(self.routing_rules) \ .map(lambda rule: rule.create_route_handlers(request, self.service_locator, self.deserializer)) \ .flat() \ .map(lambda route_handler: self.cors_handler_factory.apply_cors_rules(request.path, route_handler))
def test_givenMethodWhichRequiresAParameter_whenMapping_thenInvokeMethodWithParameter( self): calculator_object = AClassWithAMethod(0) result = Stream.of(1, 2, 3, 4).map(calculator_object.increment).toList() self.assertEqual([1, 2, 3, 4], result)
def __init__(self, application_properties: ApplicationProperties, env: SystemEnvironmentProperties): self.folders = Stream.of( "media_library", "transcoded_media_folder").map( lambda x: env.get(x) or application_properties[x])
def test_givenBuiltinType_whenMapping_thenCallConstructorWithASingleParameter( self): result = Stream.of("1", "2", "3").map(int).toList() self.assertEqual([1, 2, 3], result)
def test_givenBuiltinFunction_whenMapping_thenCorrectlyExpandParametersAndDoNotCrash( self): expected = Stream(range(0, 4)).map(lambda x: ord(str(x))).toList() result = Stream.of("0", "1", "2", "3").map(ord).toList() self.assertEqual(expected, result)
def test_givenClassReference_whenMapping_thenCallClassConstructor(self): squares = Stream.of(1, 2, 3, 4).map(AClassWithAMethod).map( AClassWithAMethod.get_square).toList() self.assertEqual([1, 4, 9, 16], squares)
def test_whenCreatingFromNonIterableElements_thenCreateACollectionContainingAllParameters( self): result = Stream.of(1, 2, 3, 4).toList() self.assertEqual([1, 2, 3, 4], result)
from jivago.lang.stream import Stream first_ten_square_numbers = Stream.range() \ .map(lambda x: x ** 2) \ .take(10) # [1, 4, 9, 16, 25, ...] Stream.zip(['a', 'b', 'c'], [1, 2, 3]) \ .forEach(lambda letter, number: print(f"{letter} is the {number}th letter of the alphabet.")) Stream.of(1, 2, 3, 4).allMatch(lambda x: x < 10) # True def square(x: int) -> int: return x**2 squares = Stream.of(1, 2, 3, 4).map(square).toList() # [1, 4, 9, 16] alphabet = Stream([(1, 'a'), (2, 'b'), (3, 'c')]).toDict() # { 1 : 'a', ...}