Exemplo n.º 1
0
    def get_element_ending(self, value=None):
        position = self.ctx.position
        tail = u''
        if position & C._AS_VALUE_:
            tail = u''
        elif (position & C._AS_LIST_ELEMENT_ or
                position & C._AS_DICT_ELEMENT_ or
                position & C._AS_CLASS_ELEMENT_ or
                position & C._AS_TUPLE_ELEMENT_):
            tail = u','

            if False:
                pass
            elif position & C._AS_CLASS_ELEMENT_:
                'last element of class has no ending'
                elements = self.ctx.parent.get_elements()
                if elements.index(self.ctx.obj) == len(elements)-1:
                    tail = u''
            elif is_class_instance(value) or utils.is_class(value):
                if self.ctx.indent_cnt >= self.config.max_depth:
                    # if reach max depth, the value would be repr as one-line string
                    # so comma is need
                    pass
                else:
                    tail = u''
            # print('%s or %s: %s' % (is_class_instance(value), utils.is_class(value), value))

        return tail
Exemplo n.º 2
0
    def build_block(self):
        indent_cnt = self.ctx.indent_cnt
        o = self.ctx.obj
        position = self.ctx.position

        debug(self.config, C._DL_FUNC_, indent_cnt,
              ('obj:{} indent_cnt:{} position:{:b}'.format(
                  o, indent_cnt, position)))

        tail = self.ctx.element_ending
        block_ending = self.get_block_ending()
        debug(self.config, C._DL_STATEMENT, 
              indent_cnt, 'tail, block_ending: ' + str([tail, block_ending]))

        # {
        _leading = ustr('')
        if position & C._AS_ELEMENT_:
            _leading += self.config.indent_char * indent_cnt
        elif position & C._AS_VALUE_:
            _leading += ustr('')

        name = get_name(o)
        label = get_type(o)

        ret = _leading
        if (self.config.instance_repr_enable and 
                is_class_instance(self.ctx.obj) and
                has_custom_repr(self.ctx.obj)):
            ret = self.ctn.write(ret + ustr(repr(self.ctx.obj)))
        else:
            ret += '%s(%s):' % (label, name) + '\n'

            # body
            ele_ctnr = self.get_elements()
            props_cnt = len(ele_ctnr)

            if props_cnt == 0:
                # right strip ':\n'
                ret = ret[:-2]

            ret = self.ctn.write(ret)

            for idx, key, val in ele_ctnr:
                # '忽略掉 以__开头的成员、自引用成员、函数成员'
                ctx = self.ctx.clone()
                ctx.obj = (key, val)
                ctx.parent = self
                ctx.position = C._AS_CLASS_ELEMENT_
                ctx.indent_cnt = self.ctx.indent_cnt + 1
                ret += str(PairBlock(self.config, ctx))

        # }
        ret += self.ctn.write(tail + block_ending)
        return ret
Exemplo n.º 3
0
    def test_is_class_instance(self):
        self.assertEqual(is_class_instance(None), False)
        self.assertEqual(is_class_instance(1), False)
        self.assertEqual(is_class_instance((1,)), False)
        self.assertEqual(is_class_instance([]), False)
        self.assertEqual(is_class_instance({}), False)
        self.assertEqual(is_class_instance(tuple), False)
        self.assertEqual(is_class_instance(list), False)
        self.assertEqual(is_class_instance(dict), False)
        self.assertEqual(is_class_instance(df.EmptyFunc), False)
        self.assertEqual(is_class_instance(df.EmptyClassOldStyle), False)
        self.assertEqual(is_class_instance(df.EmptyClassNewStyle), False)
        self.assertEqual(is_class_instance(df.NormalClassOldStyle.mth), False)
        self.assertEqual(is_class_instance(df.NormalClassOldStyle.mth), False)

        self.assertEqual(is_class_instance(df.EmptyClassOldStyle()), True)
        self.assertEqual(is_class_instance(df.EmptyClassNewStyle()), True)
        self.assertEqual(is_class_instance(df.NormalClassOldStyle().mth), False)
        self.assertEqual(is_class_instance(df.NormalClassOldStyle().mth), False)