script_shortcuts.py 3.02 KB
Newer Older
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
1
2
3
4
5
6
7
8
9
10
# Defines the following shortcuts:
# signal.name -> return the shortname
# signal      -> display nicely the content of the signal
# signal(3)   -> recompute the signal at time 3, and display nicely
# signal +1   -> increment the signal time of 1, recompute, and display.
# signal.deps -> display the graph dependancy up to the default value (3)
# signal.deps(6) -> same, but with depth = 6.
# entity      -> same as print(entity)
# change the prompt to be '%'

11
12
from __future__ import print_function

Guilhem Saurel's avatar
Guilhem Saurel committed
13
14
# Changing prompt
import sys
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
15

Guilhem Saurel's avatar
Guilhem Saurel committed
16
17
from .entity import Entity
from .signal_base import SignalBase
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
18

Guilhem Saurel's avatar
Guilhem Saurel committed
19

Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
20
21
22
23
# Enables shortcut "name"
def sig_short_name(self):
    return self.getName().split(':')[-1]

Guilhem Saurel's avatar
Guilhem Saurel committed
24
25
26

setattr(SignalBase, 'name', property(sig_short_name))

Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
27
28
29
30
31
32
33
34
35

# Enables shortcuts "m"
# This code implements a pseudo function 'm' in the class signal_base,
# with no args, or optional args. Three calls can be made:
#    - sig.m : print the current value.
#    - sig.m(time): recompute at given <time>, and display the current value
#    - sig.m +time: recompute at <time> after current time, and display.
class PrettySignalPrint:
    sig = None
Guilhem Saurel's avatar
Guilhem Saurel committed
36
37

    def __init__(self, sig):
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
38
        self.sig = sig
Guilhem Saurel's avatar
Guilhem Saurel committed
39

40
    def __str__(self):
41
        return self.sig.name + " = " + str(self.sig.value)
Guilhem Saurel's avatar
Guilhem Saurel committed
42

Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
43
    def __repr__(self):
44
        return str(self)
Guilhem Saurel's avatar
Guilhem Saurel committed
45
46

    def __call__(self, iter):
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
47
48
        self.sig.recompute(iter)
        return self
Guilhem Saurel's avatar
Guilhem Saurel committed
49
50
51

    def __add__(self, iter):
        self.sig.recompute(self.sig.time + iter)
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
52
53
        return self

Guilhem Saurel's avatar
Guilhem Saurel committed
54

Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
55
56
57
def sigMatPrint(sig):
    return PrettySignalPrint(sig)

Guilhem Saurel's avatar
Guilhem Saurel committed
58
59
60

setattr(SignalBase, 'm', property(PrettySignalPrint))

Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
61
62

# Enable the same as 'm', but directly on the signal object.
Guilhem Saurel's avatar
Guilhem Saurel committed
63
def sigRepr(self):
64
    return self.name + ' = ' + str(self.value)
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
65

Guilhem Saurel's avatar
Guilhem Saurel committed
66
67

def sigCall(sig, iter):
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
68
    sig.recompute(iter)
69
    print(sigRepr(sig))
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
70

Guilhem Saurel's avatar
Guilhem Saurel committed
71
72
73

def sigTimeIncr(sig, iter):
    sig.recompute(sig.time + iter)
74
    print(sigRepr(sig))
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
75

Guilhem Saurel's avatar
Guilhem Saurel committed
76
77
78
79
80

setattr(SignalBase, '__repr__', sigRepr)
setattr(SignalBase, '__call__', sigCall)
setattr(SignalBase, '__add__', sigTimeIncr)

Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
81
82
83
84
85
86
87

# Enables shortcut "deps"
# Implements the peudo function 'deps', that can be called without arg,
# or specifying a specific depth to be printed.
class SignalDepPrint:
    defaultDepth = 2
    sig = None
Guilhem Saurel's avatar
Guilhem Saurel committed
88
89
90
91

    def __init__(self, sig):
        self.sig = sig

Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
92
93
    def __repr__(self):
        return self.sig.displayDependencies(self.defaultDepth)
Guilhem Saurel's avatar
Guilhem Saurel committed
94
95

    def __call__(self, depth):
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
96
97
98
99
        self.defaultDepth = depth
        return self


Guilhem Saurel's avatar
Guilhem Saurel committed
100
101
102
103
setattr(SignalBase, 'deps', property(SignalDepPrint))

setattr(Entity, 'sigs', property(Entity.displaySignals))
setattr(Entity, '__repr__', Entity.__str__)
Nicolas Mansard's avatar
IVIGIT.  
Nicolas Mansard committed
104
105
106

sys.ps1 = '% '

Guilhem Saurel's avatar
Guilhem Saurel committed
107

108
109
110
# Enable function that can be call without()def optionalparentheses(f):
def optionalparentheses(f):
    class decoclass:
Guilhem Saurel's avatar
Guilhem Saurel committed
111
112
113
        def __init__(self, f):
            self.functor = f

114
        def __repr__(self):
Guilhem Saurel's avatar
Guilhem Saurel committed
115
116
117
118
119
120
121
            res = self.functor()
            if isinstance(res, str):
                return res
            else:
                return ''

        def __call__(self, *arg):
122
            return self.functor(*arg)
Guilhem Saurel's avatar
Guilhem Saurel committed
123

124
    return decoclass(f)