Example #1
0
    def __repr__(self):
        '''
            Describes current function wrapper.

            Examples:

            >>> function_decorator = FunctionDecorator(
            ...     FunctionDecorator.__repr__)
            >>> function_decorator.class_object = FunctionDecorator
            >>> repr(function_decorator) # doctest: +ELLIPSIS
            'Object of "FunctionDecorator" ... "__repr__" ... (NoneType).'

            >>> repr(FunctionDecorator(FunctionDecorator)) # doctest: +ELLIPSIS
            'Object of "FunctionDecorator" with wrapped function "None" and...'
        '''
        function_name = 'None'
        if self.__func__ is not None:
            function_name = self.__func__.__name__
        if self.class_object:
# # python3.5
# #             return (
# #                 'Object of "{class_name}" with class object '
# #                 '"{class_object}", object "{object}", wrapped function '
# #                 '"{wrapped_function}" and return value "{value}" '
# #                 '({type}).'.format(
# #                     class_name=self.__class__.__name__,
# #                     class_object=self.class_object.__name__,
# #                     object=builtins.repr(self.object),
# #                     wrapped_function=function_name,
# #                     value=builtins.str(self.return_value),
# #                     type=builtins.type(self.return_value).__name__))
# #         return (
# #             'Object of "{class_name}" with wrapped function '
# #             '"{wrapped_function}" and return value "{value}" '
# #             '({type}).'.format(
# #                 class_name=self.__class__.__name__,
# #                 wrapped_function=function_name,
# #                 value=builtins.str(self.return_value),
# #                 type=builtins.type(self.return_value).__name__))
            return (
                'Object of "{class_name}" with class object '
                '"{class_object}", object "{object}", wrapped function '
                '"{wrapped_function}" and return value "{value}" '
                '({type}).'.format(
                    class_name=self.__class__.__name__,
                    class_object=self.class_object.__name__,
                    object=builtins.repr(self.object),
                    wrapped_function=function_name,
                    value=convert_to_unicode(self.return_value),
                    type=builtins.type(self.return_value).__name__))
        return (
            'Object of "{class_name}" with wrapped function '
            '"{wrapped_function}" and return value "{value}" '
            '({type}).'.format(
                class_name=self.__class__.__name__,
                wrapped_function=function_name,
                value=convert_to_unicode(self.return_value),
                type=builtins.type(self.return_value).__name__))
Example #2
0
    def get_content(self):
        '''
            Getter for the current content.

            Examples:

            >>> Buffer().write('test').content
            'test'

            >>> Buffer(queue=True).write('test').content
            'test'
        '''
        with self._lock:
            if self.file is not None:
                self._content = self.file.content
            elif self.queue:
                self._content = ''
                temp_buffer = []
                while not self.queue.empty():
# # python3.5
# #                     temp_buffer.append(self.queue.get())
                    temp_buffer.append(convert_to_unicode(
                        self.queue.get()))
# #
                    self._content += temp_buffer[-1]
                for content in temp_buffer:
                    self.queue.put(content)
# # python3.5
# #         pass
        if self.force_string and builtins.isinstance(
            self._content, builtins.unicode
        ):
            self._content = convert_to_string(self._content)
# #
        return self._content
Example #3
0
    def __repr__(self):
        '''
            Represents the current handled function call.

            Examples:

            >>> class A:
            ...     def a(self): pass
            >>> return_aspect = ReturnAspect()
            >>> return_aspect.class_object = A
            >>> return_aspect.object = A()
            >>> return_aspect.__func__ = A().a
            >>> return_aspect.arguments = ()
            >>> return_aspect.keywords = {}

            >>> repr(return_aspect) # doctest: +ELLIPSIS
            'Object of "ReturnAspect" with class object "A", object "...'
        '''
# # python3.5
# #         return (
# #             'Object of "{class_name}" with class object "{class_object}", '
# #             'object "{object}", function "{function}", arguments '
# #             '"{arguments}", keywords "{keywords}" and return value '
# #             '"{value}" ({type}).'.format(
# #                 class_name=self.__class__.__name__,
# #                 class_object=self.class_object.__name__,
# #                 object=builtins.repr(self.object),
# #                 function=self.__func__.__name__,
# #                 arguments='", "'.join(self.arguments),
# #                 keywords=builtins.str(self.keywords),
# #                 value=builtins.str(self.return_value),
# #                 type=builtins.str(builtins.type(self.return_value))))
        return (
            'Object of "{class_name}" with class object "{class_object}", '
            'object "{object}", function "{function}", arguments '
            '"{arguments}", keywords "{keywords}" and return value '
            '"{value}" ({type}).'.format(
                class_name=self.__class__.__name__,
                class_object=self.class_object.__name__,
                object=builtins.repr(self.object),
                function=self.__func__.__name__,
                arguments='", "'.join(self.arguments),
                keywords=builtins.str(self.keywords),
                value=convert_to_unicode(self.return_value),
                type=builtins.str(builtins.type(self.return_value))))
Example #4
0
    def __str__(self):
        '''
            Is triggered if this object should be converted to string.

            Examples:

            >>> str(Print('peter', buffer=Buffer()))
            'peter\\n'

            >>> print = Print('', buffer=Buffer())
            >>> print.buffer = None
            >>> str(print)
            ''
        '''
        if builtins.isinstance(self.buffer, Buffer):
# # python3.5             return builtins.str(self.buffer)
            return convert_to_unicode(self.buffer)
        return ''
Example #5
0
    def __repr__(cls):
        '''
            Invokes if this object should describe itself by a string.

            Examples:

            >>> repr(Logger()) # doctest: +ELLIPSIS
            'Object of "Logger" with logger "...

            >>> logger1 = Logger.get()
            >>> repr(Logger()) # doctest: +ELLIPSIS
            'Object of "Logger" with logger "...

            >>> logger1 = Logger.get()
            >>> logger2 = Logger.get('hans')
            >>> repr(Logger()) # doctest: +ELLIPSIS
            'Object of "Logger" with logger "... and ...
        '''
        handler_string = formatter_string = ''
        for index, logger in builtins.enumerate(cls.instances):
            start = ', "'
            end = '"'
            if index + 1 == builtins.len(cls.instances):
                start = ' and "'
                end = ''
            if index == 0:
                start = ''
            handler_string += start + builtins.repr(logger.handlers[0]) + end
            formatter_string += start + builtins.repr(
                logger.handlers[0].formatter
            ) + end
# # python3.5
# #         return (
# #             'Object of "{class_name}" with logger "{handler}", formatter '
# #             '"{formatter}" and buffer "{buffer}".'.format(
# #                 class_name=cls.__name__, handler=handler_string,
# #                 formatter=formatter_string,
# #                 buffer=builtins.str(cls.buffer)))
        return (
            'Object of "{class_name}" with logger "{handler}", formatter '
            '"{formatter}" and buffer "{buffer}".'.format(
                class_name=cls.__name__, handler=handler_string,
                formatter=formatter_string,
                buffer=convert_to_unicode(cls.buffer)))
Example #6
0
    def __repr__(self):
        '''
            Invokes if this object should describe itself by a string.

            Examples:

            >>> repr(Buffer())
            'Object of "Buffer" (memory buffered) with content "".'

            >>> buffer = Buffer(file=__test_folder__.path + '__repr__')
            >>> buffer.write('hans') # doctest: +ELLIPSIS
            Object of "Buffer" (file buffered with "...__repr__" (type: file...

            >>> repr(Buffer(queue=True))
            'Object of "Buffer" (queue buffered) with content "".'

            >>> repr(Buffer(queue=native_queue.Queue()))
            'Object of "Buffer" (queue buffered) with content "".'
        '''
        buffer_type = 'memory'
        type_addition = ''
        if self.file:
            buffer_type = 'file'
            type_addition = ' with "%s"' % builtins.repr(self.file)
        elif self.queue:
            buffer_type = 'queue'
# # python3.5
# #         pass
        if self.force_string:
            return (
                'Object of "{class_name}" ({type} buffered{type_addition})'
                ' with content "{content}".'.format(
                    class_name=self.__class__.__name__,
                    type=buffer_type, type_addition=type_addition,
                    content=convert_to_unicode(self.content)))
# #
        return (
            'Object of "{class_name}" ({type} buffered{type_addition}) '
            'with content "{content}".'.format(
                class_name=self.__class__.__name__, type=buffer_type,
                type_addition=type_addition, content=self.content))
Example #7
0
    def __str__(cls):
        '''
            Is triggered if a "Logger" object should be converted to string.

            Examples:

            >>> str(Logger())
            ''

            >>> logger_backup = Logger.buffer
            >>> Logger.buffer = Buffer(),
            >>> str(Logger()) # doctest: +ELLIPSIS
            ''
            >>> Logger.buffer = logger_backup
        '''
        result = ''
        for buffer in cls.buffer:
            if builtins.isinstance(buffer, Buffer):
# # python3.5                 result += builtins.str(buffer)
                result += convert_to_unicode(buffer)
        return result
Example #8
0
    def _join_distinct_types(self, types, meta_type):
# #
        '''Joins a given list of objects to a single human readable string.'''
        result = ''
        for type in types:
            if type is Self:
# # python3.5
# #                 result += '"%s (self)"' % builtins.str(self.object)
                result += '"%s (self)"' % convert_to_unicode(self.object)
# #
            elif type is SelfClass:
                result += '"%s (self class)"' % self.class_object.__name__
            elif type is SelfClassObject:
                result += '"%s (self class object)"' % \
                    self.class_object.__name__
            elif meta_type:
                result += '"%s"' % type.__name__
            else:
                result += '"%s"' % builtins.str(type)
            if type is types[-2]:
                result += ' or '
            elif type is not types[-1]:
                result += ', '
        return result
Example #9
0
    def __init__(self, *output, **codewords):
# #
        '''
            Writes something to the output buffer or prints to standard \
            output.

            Examples:

            >>> buffer = Buffer()

            >>> Print(native_queue.Queue(), buffer=buffer) # doctest: +ELLIPSIS
            Object of "Print" with "...

            >>> queue1 = native_queue.Queue()
            >>> queue2 = native_queue.Queue()
            >>> queue1.put('hans')
            >>> queue2.put('hans')
            >>> Print(
            ...     queue1, queue2, buffer=buffer, flush=True
            ... ) # doctest: +ELLIPSIS
            Object of "Print" with "...

            >>> Print.default_buffer = Buffer()
            >>> Print('hans', 'hans again') # doctest: +ELLIPSIS
            Object of "Print" with "Object of "Buffer" (mem... "hans hans again
            ".".

            >>> buffer = Buffer()
            >>> Print(
            ...     'hans,', 'peter', end=' and klaus', sep=' ', buffer=buffer
            ... ) # doctest: +ELLIPSIS
            Object of "Print" with "Object of "Buffer" (memory buffered...".".

            >>> buffer # doctest: +ELLIPSIS
            Object ... (memory buffered) with content "hans, peter and klaus".
        '''
        keywords = {
            'replace': self.__class__.replace,
            'start': self.__class__.start,
            'separator': self.__class__.separator,
            'end': self.__class__.end,
            'buffer': self.__class__.default_buffer,
            'flush': codewords.get('replace', False)}
        keywords.update(codewords)
        '''Redirect print output to this buffer.'''
        self.buffer = keywords['buffer']
        output = builtins.list(output)
        for index, out in builtins.enumerate(output):
            if builtins.isinstance(out, native_queue.Queue):
                result = ''
                while not out.empty():
                    if index != 0 and keywords['separator']:
# # python3.5
# #                         result += builtins.str(keywords['separator'])
# #                     result += out.get()
                        result += convert_to_unicode(
                            keywords['separator'])
                    result += convert_to_unicode(out.get())
# #
                output[index] = result
            elif index == 0:
# # python3.5                 output[index] = builtins.str(out)
                output[index] = convert_to_unicode(out)
            else:
# # python3.5
# #                 output[index] = '%s%s' % (builtins.str(
# #                     keywords['separator']
# #                 ), builtins.str(out))
                output[index] = '%s%s' % (convert_to_unicode(
                    keywords['separator']
                ), convert_to_unicode(out))
# #
        line_replace = '\033[1A\r\033[2K' if keywords['replace'] else ''
        output = [keywords['start'], line_replace] + output + [keywords['end']]
# # python3.5
# #         builtins.print(
# #             *output, sep='', end='', file=keywords['buffer'],
# #             flush=keywords['flush'])
        builtins.print(*filter(lambda content: convert_to_string(
            content
        ), output), sep='', end='', file=keywords['buffer'])
        if keywords['flush']:
            sys.stdout.flush()