[Tutor] Dynamic change of dictionaries

Marc marcolinux@linuxbr.com.br
Wed, 19 Jun 2002 17:47:51 -0300


--uAKRQypu60I7Lcqm
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Sean 'Shaleh' Perry (shalehperry@attbi.com) wrote:

> why not simply ignore 0 count items?
> 
> for item in dict:
>   if dict[item] == 0: continue
> 
>   # print status bars
> 
> if you want to preen items, the del dict[item] method is reasonable.

Thanks for reply.

(And thanks for Lloyd Kvam <pythontutor@venix.com>.The tools you cited
are overkill right now. Besides, I want to play with python a little
bit. I didn't received your mail, read it in tutor mail archive.
I wonder how much fun I have been missing lately :)

Hehe, no need to "preen" right now, but if u consider it have to be seem
from across the room, the bars are more than welcome :)
Your method of ignore item when 0 is nice and have a good side effect: I
know that an IP acessed the proxy, thus it is alive. However, it makes
a "graph" with too many lines. That's why I want to delete zeroed items, to
avoid clutter.

I've made a new version of the simulator. And the more I play with it,
the more I want to changeit.For example, do you guys know how to change
the color of terminal with codes?
I've tried (from /etc/rc.d/functions):
(SETCOLOR_FAILURE="echo -en \\033[1;31m")

$ python
>>> red='\\033[1;31m'
>>> print red,'supposed 2 b red'
\033[1;31m supposed 2 b red

:(. Im sure it's trivial to do , but I dont know how.
Sugestions? Thanks in advance.



--uAKRQypu60I7Lcqm
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="traffic.py"

"""
Show a network traffic gauges
"""
import random,time,os,sys

dict={}
cnt=100

#talkin'bout preen :)
lft='\n'*7+'\t\t\t\t'
splashscreen=lft+'  Traffic Monitor\n'+lft+'Python Powered (TM)'

def increment(item):	
	try:
		 dict[item] += 1
	except KeyError:
		 dict[item] = 1


def decrement():
	if not dict.__len__: return
	try:
		for item in dict:
	        	if dict[item] == 0: continue #del (dict[item])
	        	else: dict[item] -= 1
	except RuntimeError:
			pass


os.system('clear')
print splashscreen
time.sleep(2)
os.system('clear')

while cnt:
	os.system('clear')
	#print dict
	for item in dict:
		print item,'\t'+'#'*dict[item]
	ip ='10.15.50.%d' % random.randrange(1,12)
	increment(ip)
	if not (cnt % 10): 
		decrement()
		print cnt
		cnt=100
	cnt-=1
	time.sleep(1)

--uAKRQypu60I7Lcqm--