From 05d5987897ee9e2d516bf893906e20eccd3fa3d8 Mon Sep 17 00:00:00 2001 From: Mansard <nmansard@laas.fr> Date: Thu, 3 Feb 2011 16:29:16 +0100 Subject: [PATCH] New working version for shortcuts and matlab style display. --- python/matlab.py | 38 ++++++++++---------- python/script_shortcuts.py | 74 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 python/script_shortcuts.py diff --git a/python/matlab.py b/python/matlab.py index 6f8f62e..b44442c 100644 --- a/python/matlab.py +++ b/python/matlab.py @@ -1,11 +1,4 @@ -def prettyDisp(self): - print matlab(self.value) - -from dynamic_graph.signal_base import * -setattr(SignalBase,'m',property(prettyDisp)) - -print('Pretty matlab print set') def pseudozero(prec): @@ -20,7 +13,7 @@ def pseudozero(prec): class matlab: prec=12 space=2 - fullPrec=0 + fullPrec=1e-5 def __init__( self,obj ): try: @@ -31,6 +24,7 @@ class matlab: return self.matlabFromVector(obj) except: pass + self.resstr = str(obj) def __str__(self): return self.resstr @@ -44,17 +38,20 @@ class matlab: for v in A: lnstr=() for x in v: - if (abs(x)<self.fullPrec): - curr=pseudozero(self.prec) + if (abs(x)<self.fullPrec*self.fullPrec): + curr='0' else: - curr= ' '+(fm % x) + if (abs(x)<self.fullPrec): + curr=pseudozero(self.prec) + else: + curr= ' '+(fm % x) if( maxstr<len(curr)): maxstr=len(curr) lnstr+=(curr,) mstr+=(lnstr,) maxstr+=self.space - resstr='...[\n' + resstr='[...\n' first=True for v in mstr: if first: @@ -80,13 +77,16 @@ class matlab: maxstr=0 vstr=(()) for x in v: - if (abs(x)<self.fullPrec): - curr=pseudozero(prec) - else: - curr= ' '+(fm % x) - if( maxstr<len(curr)): - maxstr=len(curr) - vstr+=(curr,) + if (abs(x)<self.fullPrec*self.fullPrec): + curr='0' + else: + if (abs(x)<self.fullPrec): + curr=pseudozero(self.prec) + else: + curr= ' '+(fm % x) + if( maxstr<len(curr)): + maxstr=len(curr) + vstr+=(curr,) maxstr+=self.space resstr='[ ' diff --git a/python/script_shortcuts.py b/python/script_shortcuts.py new file mode 100644 index 0000000..0c7ab25 --- /dev/null +++ b/python/script_shortcuts.py @@ -0,0 +1,74 @@ +from dynamic_graph.signal_base import * +from dynamic_graph.entity import * +from matlab import matlab + +# Enables shortcut "name" +def sig_short_name(self): + return self.getName().split(':')[-1] + +setattr(SignalBase,'name',property(sig_short_name)) + +# 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 + def __init__(self,sig): + self.sig = sig + def __repr__(self): + return str(matlab(self.sig.value)) + def __call__(self,iter): + self.sig.recompute(iter) + return self + def __add__(self,iter): + self.sig.recompute( self.sig.time+iter ) + return self + +def sigMatPrint(sig): + return PrettySignalPrint(sig) + +setattr(SignalBase,'m',property(PrettySignalPrint)) +print('Pretty matlab print set') + +# Enable the same as 'm', but directly on the signal object. +def sigRepr( self ): + return self.name+' = '+str(matlab(self.value)) + +def sigCall( sig,iter ): + sig.recompute(iter) + print sigRepr(sig) + +def sigTimeIncr( sig,iter ): + sig.recompute(sig.time+iter) + print sigRepr(sig) + +setattr(SignalBase,'__repr__',sigRepr) +setattr(SignalBase,'__call__',sigCall) +setattr(SignalBase,'__add__',sigTimeIncr) + +# 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 + def __init__(self,sig): + self.sig=sig + def __repr__(self): + return self.sig.displayDependencies(self.defaultDepth) + def __call__(self,depth): + self.defaultDepth = depth + return self + +setattr(SignalBase,'deps',property(SignalDepPrint)) + + +setattr(Entity,'__repr__',Entity.__str__) + +# Changing prompt +import sys +sys.ps1 = '% ' + -- GitLab