Python:Create your own CLI

PyLadies October Meetup was held at Red Hat India Pvt. Ltd., Pune on 20th October,2016. This was my first python meetup. Many people from different colleges attended the meetup. The topic for the meetup was “Create your own shell using Python”.

The meetup started with the talk by one of the founders of FOSSASIA Dang Hong Phuc on how “FOSSASIA meets Pyladies Pune”. She briefed us about FOSSASIA by saying that FOSSASIA is a non-profit organization supporting developers and makers of Free and Open Source technologies. She encouraged the belief of improving  women participation in open source. Her talk was very encouraging and helpful.

Then there were few more talks from the pyladies Samriddhi and Pooja explaining there journey with python. Samriddhi talked about her projects in python. And Pooja explained the difference between python2 and python3.

At the end of this meetup there was workshop on “Create your own shell using Python” by Kushal Das. In this workshop Khushal taught us how to use the cmd2 module of python to build our own command line interpreter(CLI).

Code to create shell using python:

screenshot-from-2016-11-10-210728

Output:

screenshot-from-2016-11-10-193757

—————————————————————————-

Some of the Important point from  Code:

  • from cmd2 import Cmd :  cmd2 is a python package for building powerful command-line interpreter (CLI) programs. Extends the Python Standard Library’s cmd package.
  • We created a subclass of cmd2 REPL. Define  do_* methods to control its behavior.
  • prompt:We created our own prompt i.e.pyladies.Here prompt is a inbuilt variable of cmd2 issued to solicit input.
  • __init__ is the constructor for a class. The self parameter refers to the instance of the object.The __init__ method gets called when memory for the object is allocated.
  • def do_hello(self, line):If we  use hello command with one argument (as shown in ouput ) I used hello pyladies is giving output as pyladies in red color.The command method always takes the argument, but sometimes the value is an empty string. It is left up to the command processor to determine if an empty argument is valid, or do any further parsing and processing of the command. Text output can be colored by wrapping it in the colorize method.
  • def do_uid(self,line):On typing the command uid on terminal we get output  current process’s real user id.and User home directory.In this function we used various inbuild functions in python like getuid(),getpwuid(uid) .In order to use these function we have to import os and pwd modules of python.
  • os.getuid():Return the current process’s real user id.
  • pwd.getpwuid(uid):Return the password database entry for the given numeric user ID.
  • .pw_dir:User home directory.
  • if __name__==’__main__’:__name__ is global variable in Python.Every module in python has a special attribute called __name__ . The value of __name__ attribute is set to ‘__main__’ when module run as main program.

————————————————————————–

Lastly, I want to say that it was worth and fun attending the meetup.Thanks pyladies pune.:)

Looking forward to attend the next meetup.

PyLadies Pune

Pune, IN
295 PyLadies

The Pune chapter of PyLadies, an international mentorship group for helping more women become active participants in the Python open-source community.

Next Meetup

PyLadies Pune November meetup

Sunday, Nov 13, 2016, 11:00 AM
11 Attending

Check out this Meetup Group →

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s