473,782 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Init style output with python?

Hi list,

I'm working on writing sanity check script, and for aesthetic reasons
I would like the output be in the formatted like the gentoo init
script output, that is:
"""
Check for something ............... ............... .... [OK]
Check for something else ............... ...........[FAIL]
"""

Is there are frame work or something in python that would allow me to
do this (quickly) ?
If not, ideas how I should I be getting this boring task of:
1. get screen width
2. get output string length
3. get out status length
4. calculate space
5. print string, print space, print status, print newline

what happens if user changes textual terminal "resolution " ?

p.s.
I would also like to "OK" and "FAIL" output to be colored. I haven't
found anything for python to would allow to to output to ansi (linux,
rxvt, xterm). Here's a quick class I've written (in the hope it proves
to be useful to the next guy).

"""
#!/usr/bin/env python
""" This stuff is under GPL, as always"""

class ColorTerm:
def __init__(self, Mono = False):
pass

def __get_tput_colo r_value__(color code):
from commands import getoutput
return getoutput('tput setaf ' + colorcode)

BLACK_FG = __get_tput_colo r_value__('0')
RED_FG = __get_tput_colo r_value__('1')
GREEN_FG = __get_tput_colo r_value__('2')
YELLOW_FG = __get_tput_colo r_value__('3')
BLUE_FG = __get_tput_colo r_value__('4')
MAGENTA_FG = __get_tput_colo r_value__('5')
CYAN_FG = __get_tput_colo r_value__('6')
WHITE_FG = __get_tput_colo r_value__('7')

def black(self, msg):
return self.BLACK_FG + msg + self.BLACK_FG

def red(self, msg):
return self.RED_FG + msg + self.BLACK_FG

def green(self, msg):
return self.GREEN_FG + msg + self.BLACK_FG

def yellow(self, msg):
return self.YELLOW_FG + msg + self.BLACK_FG

def blue(self, msg):
return self.BLUE_FG + msg + self.BLACK_FG

def magenta(self, msg):
return self.MAGENTA_FG + msg + self.BLACK_FG

def cyan(self, msg):
return self.CYAN_FG + msg + self.BLACK_FG

def white(self, msg):
return self.WHITE_FG + msg + self.BLACK_FG
cc = ColorTerm()
print cc.red('Cool!') + cc.yellow('?'), cc.green('Sure is!!!')
print "Now setting your terminal text color to blue" + cc.BLUE_FG
print "well don't be blue about this, here let me set it back for you"
print cc.BLACK_FG + "see, nothing to worry about"
"""

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
May 5 '07 #1
3 1545
On May 6, 9:27 am, "Maxim Veksler" <hq4e...@gmail. comwrote:
Hi list,

I'm working on writing sanity check script, and for aesthetic reasons
I would like the output be in the formatted like the gentoo init
script output, that is:
"""
Check for something ............... ............... .... [OK]
Check for something else ............... ...........[FAIL]
"""

Is there are frame work or something in python that would allow me to
do this (quickly) ?
If not, ideas how I should I be getting this boring task of:
1. get screen width
Is it not (a) safe (b) sensible to assume a minimum width (say 79) and
avoid the whole question of determining the terminal width?
2. get output string length
3. get out status length
4. calculate space
5. print string, print space, print status, print newline
Surely you don't need assistance with steps 2 - 5 ...
what happens if user changes textual terminal "resolution " ?
Something rather unaesthetic, I imagine.

May 6 '07 #2
Maxim Veksler wrote:
Is there are frame work or something in python that would allow me to
do this (quickly) ?
If not, ideas how I should I be getting this boring task of:
1. get screen width
You can look into the 'curses' module and do something like:

screen = curses.initscre en()
maxheight, maxwith = screen.getmaxyx ()

In my experience curses can be a bit tricky to work with but the online
tutorials have some nice examples that help you avoid some of the
pitfalls (like messing up your terminal)

Tina
May 6 '07 #3
On 5/6/07, Tina I <ti*****@bestem selv.comwrote:
Maxim Veksler wrote:
Is there are frame work or something in python that would allow me to
do this (quickly) ?
If not, ideas how I should I be getting this boring task of:
1. get screen width

You can look into the 'curses' module and do something like:

screen = curses.initscre en()
maxheight, maxwith = screen.getmaxyx ()

In my experience curses can be a bit tricky to work with but the online
tutorials have some nice examples that help you avoid some of the
pitfalls (like messing up your terminal)

Tina
--
http://mail.python.org/mailman/listinfo/python-list
Fine! Thank you.

curses is very helpful, I'm attaching the code.
I see it has support for colors as well, but I haven't found any
tutorial that would explain how to use them.

Please note that this is just a draft, I'm not catching any
KeyboardInterru pt nor nothing.

"""#!/usr/bin/env python

class ColorTerm:
def __init__(self, Mono = False):
pass

def __get_tput_colo r_value__(color code):
from commands import getoutput
return getoutput('tput setaf ' + colorcode)

BLACK_FG = __get_tput_colo r_value__('0')
RED_FG = __get_tput_colo r_value__('1')
GREEN_FG = __get_tput_colo r_value__('2')
YELLOW_FG = __get_tput_colo r_value__('3')
BLUE_FG = __get_tput_colo r_value__('4')
MAGENTA_FG = __get_tput_colo r_value__('5')
CYAN_FG = __get_tput_colo r_value__('6')
WHITE_FG = __get_tput_colo r_value__('7')

def black(self, msg):
return self.BLACK_FG + msg + self.BLACK_FG

def red(self, msg):
return self.RED_FG + msg + self.BLACK_FG

def green(self, msg):
return self.GREEN_FG + msg + self.BLACK_FG

def yellow(self, msg):
return self.YELLOW_FG + msg + self.BLACK_FG

def blue(self, msg):
return self.BLUE_FG + msg + self.BLACK_FG

def magenta(self, msg):
return self.MAGENTA_FG + msg + self.BLACK_FG

def cyan(self, msg):
return self.CYAN_FG + msg + self.BLACK_FG

def white(self, msg):
return self.WHITE_FG + msg + self.BLACK_FG

class StatusWriter(Co lorTerm):
import curses

def __init__(self, report_type = None):
pass

def initstyle_messa ge(self, msg, status = True):
screen = self.curses.ini tscr(); self.curses.end win()
if status:
status_msg = '[' + self.green('OK' ) + ']'
else:
status_msg = '[' + self.red('FAIL' ) + ']'

spaces_count = ( screen.getmaxyx ()[1] - (len(msg)+len(s tatus_msg)) )
return msg + ' '*spaces_count + status_msg

cc = StatusWriter()
while 1:
print cc.initstyle_me ssage('The end is at hand')
print cc.initstyle_me ssage('Lets party', False)
print cc.initstyle_me ssage('Why like this?', True)
"""

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
May 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1771
by: John Hunter | last post by:
matplotlib is a 2D plotting package for python with a matlab compatible syntax and output tested under linux and windows platforms. matplotlib-0.30 is available for download at http://matplotlib.sourceforge.net, and has many new features since the last major release. Multiple outputs matplotlib now supports postscript and GD output, as well as the
0
2727
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24 to obtain SHIP_wrap.cpp and SHIP.py; the latter contains the line "import _SHIP". I compile SHIP_wrap.cpp and a bunch of files into a DLL which I have the
10
3878
by: Wylbur via DotNetMonster.com | last post by:
Hello to all of you geniuses, I'm having a problem trying to get an Init handler to fire for a Placeholder control at the initialization phase. I’ve posted this problem to 3 other ASP.NET forums, and noone wants to touch it. I tried to attach a literal control to a placeholder: <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>
1
2144
by: Alexandre Lahure | last post by:
Hi all, The facts : a rich text editing applet, a HTML/Javascript toolbar and Liveconnect to make them communicate alltogether. - Java to JS communication (for updating the state of the toolbar - text align, style, color) is OK - JS to Java communication (for changing text align, style, color) doesn't work as expected : * Under Firefox, the first call of a Java method is DAMN SLOW (near 20
0
2509
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
5
1511
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create an instance of an Old-Style class without defining a __contains__ but instead define a __getitem__ method in which we raise KeyError. Next we repeatedly use the 'in' operator to test to see whether something, a string, an int, etc is an attribute...
4
3714
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
13
2503
by: stephenpas | last post by:
We are trying to monkey-patch a third-party library that mixes new and old-style classes with multiple inheritance. In so doing we have uncovered some unexpected behaviour: <quote> class Foo: pass class Bar(object): pass
10
1720
by: Terrence Brannon | last post by:
Hello, The most common way of dynamically producing HTML is via template engines like genshi, cheetah, makotemplates, etc. These engines are 'inline' --- they intersperse programming constructs with the HTML document itself. An opposite approach to this form of dynamic HTML production is called push-style templating, as coined by Terence Parr:
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.