64798a399a79ed8d34fa83ba0e61c1ac

I bet there is some other, clever way to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import inspect

# tests
def test(foobar, mamibar='ola', tatibar='olee', *args, **kw):
    pass

def test2(test, *taest, **mest):
    pass

def test3(mami, tati, dedi, stric, teta='ok'):
    pass

def test4(test='ok'):
    pass

def test5(*arg):
    pass

def test6():
    pass

# function
def getattrs(func):
    """

    Print function's original arguments (including defaults)

    >>> getattrs(test)
    Computed arguments: test(foobar, mamibar='ola', tatibar='olee', *args, **kw)
    
    >>> getattrs(test2)
    Computed arguments: test2(test, *taest, **mest)
    
    >>> getattrs(test3)
    Computed arguments: test3(mami, tati, dedi, stric, teta='ok')
    
    >>> getattrs(test4)
    Computed arguments: test4(test='ok')
    
    >>> getattrs(test5)
    Computed arguments: test5(*arg)
    
    >>> getattrs(test6)
    Computed arguments: test6()
    

    """

    args = inspect.getargspec(func)
    newargs = []

    if args[3]:
        razlika = len(args[0]) - len(args[3])
        for i, arg in enumerate(args[0]):
            if i < razlika:
                newargs.append(arg)
            else:
                newargs.append('%s=\'%s\'' % (arg, args[3][i - razlika]))

    if args[0] and not args[3]:
        newargs.extend([arg for arg in args[0]])



    if args[1]:
        newargs.append('*' + args[1])
    if args[2]:
        newargs.append('**' + args[2])

    if not newargs:
        newargs = ['']


    print "Computed arguments: %s(%s)" % (func.__name__, ', '.join(newargs))


if __name__ == '__main__':
    import doctest
    doctest.testmod()

Refactorings

No refactoring yet !

Avatar

gsson

August 20, 2008, August 20, 2008 11:46, permalink

1 rating. Login to rate!

Something like this, perhaps?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import inspect

# tests
def test(foobar, mamibar='ola', tatibar='olee', *args, **kw):
    pass

def test2(test, *taest, **mest):
    pass

def test3(mami, tati, dedi, stric, teta='ok'):
    pass

def test4(test='ok'):
    pass

def test5(*arg):
    pass

def test6():
    pass

# function
def getattrs(func):
    """

    Print function's original arguments (including defaults)

    >>> getattrs(test)
    Computed arguments: test(foobar, mamibar='ola', tatibar='olee', *args, **kw)
    
    >>> getattrs(test2)
    Computed arguments: test2(test, *taest, **mest)
    
    >>> getattrs(test3)
    Computed arguments: test3(mami, tati, dedi, stric, teta='ok')
    
    >>> getattrs(test4)
    Computed arguments: test4(test='ok')
    
    >>> getattrs(test5)
    Computed arguments: test5(*arg)
    
    >>> getattrs(test6)
    Computed arguments: test6()
    

    """

    print "Computed arguments: %s%s" % (
                    func.__name__, 
                    inspect.formatargspec(*inspect.getargspec(func))) 


if __name__ == '__main__':
    import doctest
    doctest.testmod()

Your refactoring





Format Copy from initial code

or Cancel