Saturday, March 25, 2017

Optimists, rationalists and pessimists

Thing change. It's the only constant.

And as things change we like to guess outcomes.

Optimists like to guess things will be fine.

Pessimists guess at bad outcomes.

Rationalists recognise that of the outcomes some are better than others.

On this continuum I sit between rational and optimistic. What steers me from pure rationalism?

1. Faith in human ingenuity to steer the outcomes towards the good.

2. The fact that from a historical perspective optimists are more often proved right. Unless you believe a nasty Brutus and short life is better than we have now, overall things improve.

Tell me I'm wrong.


Tuesday, March 14, 2017

ANSI formatter for python

ANSI formatter

I have been playing around with python and needed something that formatted the terminal output using the ansi codes as described here:

http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html

Except I can't remember all the escape sequences any more. The last time I used them was about 35 years ago when I was working with serial terminals at Philips in Holland.

I thought it might be useful so here it is. I've put a listing at the end of the post. Feel free do do with it what you will within the terms of the GPL. I'll update this when I get round to putting it on GitHub.

The code has been tested on LXTerminal and xcfe4 on the pi and xterminal on ubuntu and gives expected results on all of them. It won't work if your terminal doesn't recognise ansi codes.

Here's the sample output:



Update: It's also on GitHub here : https://github.com/davidsinfield/ansilib

And here's the function and a bit of sample usage:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  ansidemo.py
#  
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  https://www.gnu.org/licenses/gpl-3.0.en.html
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  


dictForeBack={
'fore' : 3,
'back' : 4
}
dictColour={
'black' : 0,
'red' : 1,
'green' : 2, 
'yellow' : 3,
'blue' : 4,
'magenta' : 5,
'cyan' : 6,
'white' : 7
}
dictStyle={
'normal' : 0,
'bold': 1,
'italic' : 3,
'underline' :4,
'inverse': 7,
'strikethough' : 9
}
#========================================================================
#GetANSI(text,foreback,colour,style)
#text - text to print
#forecolour - [black|red|green|yellow|blue|magenta|cyan|white]
#backcolour - [black|red|green|yellow|blue|magenta|cyan|white]
#style - [normal|bold|italic|underline|inverse|strikethough]
#
#Returns a string with ansi precursors to set foreground and background
#colours and style. Set to normal after printing.
#
#Out of range parameters throw a dictionary error
#
#Example: set foreground to red
# print GetANSI('this is red text','red','none','none')
#Example: set foreground bold red, background cyan
# print GetANSI('set properties','red','cyan','bold')
#
#Update
# empty string in a parameter is now the same as none.
# Example:
# GetANSI('text','','','bold')
#=========================================================================
def GetANSI(text,forecolour,backcolour,style):
pre="\033["
post="m"
tonormal=pre+'0m'
retval=""
if forecolour != 'none' and forecolour != '':
retval=retval+pre+str(dictForeBack['fore'])+str(dictColour[forecolour])+post
if backcolour != 'none' and backcolour != '':
retval=retval+pre+str(dictForeBack['back'])+str(dictColour[backcolour])+post
if style != 'none' and style != '':
retval=retval+pre+str(dictStyle[style])+post
return (retval+text+tonormal)

def main():
print GetANSI('red text','red','none','none')
print GetANSI('red text cyan bg bold','red','cyan','bold')
print GetANSI('inverse of above','red','cyan','inverse')
print GetANSI('black on white','none','none','inverse')
print GetANSI('black on green','green','none','inverse')
print GetANSI('non explicit green','green','','')
print 'normal text'
return 0

if __name__ == '__main__':
main()