Completed
Push — master ( 2343e4...9a0979 )
by
unknown
07:32
created

this_str()   A

Complexity

Conditions 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
1
2
import collections
3
4
5
def StarTuple(name, named_fields, elements):
6
    named_tuple = collections.namedtuple(name, named_fields)
7
8
    def this_pack(self):
9
        packed = bytes()
10
        for key, value in self.__elements.items():
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like __elements was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
Unused Code introduced by
The variable key seems to be unused.
Loading history...
11
            packed += value.pack(self._asdict())
12
13
        return packed
14
15
    def this_str(self):
16
        import pprint
17
        fmt = 'StarTuple: <{0}>\n'.format(str(name))
18
19
        len_of_keys = 0
20
        for key in self._asdict().keys():
21
            if len(key) > len_of_keys:
22
                len_of_keys = len(key)
23
24
        for key, value in self._asdict().items():
25
            fmt += ('  {key:%d}: {value}\n' % len_of_keys).format(
26
                key=key,
27
                value=pprint.pformat(value, width=150),
28
            )
29
30
        return fmt
31
32
    named_tuple.pack = this_pack
33
    named_tuple.__str__ = this_str
34
    named_tuple.__elements = elements
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like __elements was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
35
36
    return named_tuple
37