Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168703
Image: ubuntu2004

This worksheet parses the login data to find the number of simultaneous worksheets and plot the results.

# Get login data from the "last" program from subprocess import Popen, PIPE p = Popen("last", stdout=PIPE,shell=True) output = p.communicate()[0] from datetime import datetime, timedelta # define an "interesting" user name # this definition gets any users with the same name # as the currently executing worksheet user. def interesting_user(s): return s[:8]==os.environ['USER'][:8] # parse the data print "Interesting user: ",os.environ['USER'][:8] data=[] i=0 for line in output.split('\n'): i+=1 tokens=line.split() if len(tokens)==0 or not interesting_user(tokens[0]): continue d=dict(user=tokens[0]) d['action']='log in' d['time']=datetime.strptime('%s %s 2011 %s'%(tokens[4],tokens[5],tokens[6]), '%b %d %Y %H:%M') logindate=d['time'] d['log id']=i data.append(d) if tokens[7]=='-': d=dict(user=tokens[0]) d['action']='log out' try: d['time']=datetime.strptime('%s %s 2011 %s'%(tokens[4],tokens[5],tokens[8]), '%b %d %Y %H:%M') if d['time']<logindate: d['time']+=timedelta(1r) # add one day except ValueError: continue d['log id']=i data.append(d) # sort the data by time data.sort(key=lambda x: x['time']) # now create two corresponding lists of dates and number of logged in users logged_in=0 starting_date=data[0]['time'] dates=[] users=[] for d in data: if d['action']=='log in': logged_in+=1 elif d['action']=='log out': logged_in-=1 dates.append(d['time']) users.append(logged_in)
Interesting user: sagenbws
# here is a static graph import matplotlib from pylab import figure, savefig fig = figure(figsize=[15,5]) ax = fig.add_subplot(111) ax.plot_date(dates, users, '-',drawstyle='steps') ax.grid(True) fig.autofmt_xdate() savefig('test.png',bbox_inches='tight')
# now plot using matplotlib import matplotlib from pylab import figure, savefig @interact def _(view=range_slider(0,len(dates),1,default=(0,len(dates)))): fig = figure(figsize=[15,5]) ax = fig.add_subplot(111) ax.plot_date(dates[view[0]:view[1]], users[view[0]:view[1]], '-',drawstyle='steps') ax.grid(True) fig.autofmt_xdate() savefig('test.png',bbox_inches='tight')