Tell-me-more

I am pretty sure that you are wondering why such a title!

Tell-me-more is a command line tool built using Python3 that provides information about the linux user and the system. The tool displays the users of the system, the current user who is logged in the system, and the memory information.

To run the tool follow README .

I have used regex-regular expression in Python3 for pattern searching. I have taken help of re module for pattern searching, argparse module for taking command line argument.

Check the source code in github to stay updated.

Finds the users


def bash_users():
    with open('/etc/passwd', 'r') as fobj:
        f = fobj.readlines()
        pattern = '/bash'

        for i in f:
            m = re.search(pattern, i) #searches if the given pattern is in i
            if m:
                print("%s" % i.split(':')[4])
            else:
                pass


Finds details of the memory of your system



def memory_details():
    with open('/proc/meminfo', 'r') as fobj:
        f = fobj.readlines()
        pattern = '^Mem' #All the strings starting with Mem

        for i in f:
            m = re.search(pattern, i) #searches if the pattern is in i
            if m:
                print("%s: %d MB" % (i.split(':')[0], int(i.split()[1])/1024))  #Displays the memory in unit MB
            else:
                pass

See howitworks for a demo of Tell-me-more. Fork me on github to contribute for this project. Thank you 🙂

Making of an index page

Continuation of static site generator….

Each and every blog has an index page that contains 5-6 posts of your blog posts, like wordpress has 10 posts in the index page/ Home page by default. I used to wonder how wordpress shows only the last 10 of my posts in its index page. And yes finally I’ve figured out the way 😉 . We are working on static site generator/ static blog generator in dgplug for the last 10 days and on the last Wednesday kushal shoot an assignment of making an index page to us. His question was a bit confusing, all the folks of dgplug were really confused. Because we were not thinking what kushal was thinking, so he decided to elaborate more on this Monday.

But I couldn’t wait for 3-4 days to solve the assignment, thus guessing what kushal was thinking I finally did something and kushal finally said that it matched with at least what he thought. kushal guided me with what else I have to include in the script and I followed him. Hence I was almost done with the index page of static site generator. Let me share with you what I have done 🙂

from markdown2 import Markdown
import os

def index_page(source_path, destination, N):
    files = []
    for f in os.listdir(source_path):
        if f.endswith('.md'):
            files.append(f)
    return files
    with open(destination, 'w') as d:
        for source in files[:N]:
            with open(source, 'r') as s:
                smd = s.read()
            markdowner = Markdown()
            dest = markdowner.convert(smd)
            d.write(dest)

index_page('source_dir_path', 'index.html', 2)

This script searches for all the .md / markdown files in the source directory and makes a list of it. Then converts it to html as needed. According to this script the index page is having 2 posts.

Thanks for your time :). Can’t wait to contribute a project on static site generator in my github.

Finally the irc bot can save the channel’s logs

Finally batuli can save the channel’s logs. I can still remember the very first day when I and sanketdg joined the irc channel ##testbot to work for irc bot batuli and I asked sanketdg “Won’t batuli save the logs of the channel as batul does for dgplug?” , sanketdg replied to me “Make it save the logs if you can!”.

I couldn’t ever imagined that making batuli save the logs of the channel could be done in so simple way. Ah yes, I spent my days studying about logging module and twisted log module. And i took help of twisted log module to make batuli save the channel’s log and sent a pull request. But sanketdg told me to make it simple and he suggested me to use logging module, yes he was pretty correct. The script was looking pretty complex. Hence I started working with logging module. On the next morning suddenly an idea came to my mind. I asked myself that why I am using these logging modules to save the channel’s log, I also have other options in Python . Because, Python is such a programming language with what you can do just anything. Coding in Python is nothing but playing with it 😉 . And what I did is just append the message of user to a file. There couldn’t be any simpler way to make your bot save the logs. Let me share it with you 🙂

with open(args.filename,'a') as fobj:
    fobj.write('\n' + time.strftime('[%d-%m-%Y %H:%M:%S]') + \
            ' <' + user + '> ' + msg)

To take command line argument, I have used argparse module instead to sys module to make the command line interface more interactive with user and it was an idea of sanketdg  🙂

So I am really happy that batuli now saves the logs of ##testbot. Join our channel ##testbot on irc.freenode . Fork us on github . Follow README section to know how to use batuli , just give it a try and I am sure that you will love it 😉

Calci

I have built Calci, a command line calculator for linux that performs basic operations like addition, subtraction, division and multiplication. I have used Python3 to build this command line tool. You will find that I had already made a blog post on Command line calculator earlier. Now this new post comes with some edits of the earlier one and another important thing is that I have made this open source .

To run Calci follow the steps given below:

  • git clone https://github.com/trishnaguha/Calci.git
  • python3 main.py followed by arguments

Since it is a command line tool you’ll need to provide arguments after python3 main.py . Type python3 main.py -h or python3 main.py –help to know this in details.

you can also fork Calci on github and send pull requests to add extra features or edit the existing features. You are also invited to open issue if you find any.

I am really excited about this project, Looking forward to turn this into a scientific one. 🙂

A Command-line Calculator using Python 3

Kushal introduced us to command-line parsing module argparse in Python3 on Wednesday in dgplug. Well now I am having the real fun with Python.

I have made a simple calculator using argparse that calculates addition, subtraction, division and multiplication. Run the code in Linux terminal as given below in the image.

The code:

import argparse

#Addition function
def add(a,b):
    val = a+b
    return val

#Subtraction function
def sub(a,b):
    val = a-b
    return val

#Division function
def div(a,b):
    val = a/b
    return val

#Multiplication function
def multi(a,b):
    val = a*b
    return val

#Main function
def Main():
    parser = argparse.ArgumentParser()

    group = parser.add_mutually_exclusive_group()
    group.add_argument("-fa","--fadd",help="Performs addition",action="store_true")
    group.add_argument("-fs","--fsub",help="Performs subtraction",action="store_true")
    group.add_argument("-fd","--fdiv",help="Performs division",action="store_true")
    group.add_argument("-fm","--fmulti",help="Performs multiplication",action="store_true")

    parser.add_argument("num1",help="Number1 to calculate",type=int)
    parser.add_argument("num2",help="Number2 to calculate",type=int)

    args = parser.parse_args()
    
#Optional arguments 
    if args.fadd:
        print("The addition result of {} and {} is {}".format(args.num1,args.num2,(add(args.num1,args.num2))))
    elif args.fsub:
        print("The subtraction result of {} and {} is {}".format(args.num1,args.num2,(sub(args.num1,args.num2))))
    elif args.fdiv:
        print("The division result of {} and {} is {}".format(args.num1,args.num2,(div(args.num1,args.num2))))
    elif args.fmulti:
        print("The multiplication result of {} and {} is {}".format(args.num1,args.num2,(multi(args.num1,args.num2))))
    else:
        print("Error:Requires an argument to perform an action")

if __name__ == '__main__':
    Main()

This works like :

Screenshot from 2015-08-07 02:00:12

Leave comments for modification of the code 🙂