Friday, 29 January 2021

Python Beginners Resource

 Regardless of the topic, There is much information on the internet. If you’ve just started or are just about to begin your learning in python, you will find a lot of articles, courses, books, and YouTube channels.

But “what is the best for you” is a good question you should ask yourself. Because if you started following what the internet is saying, you will end up nowhere. Spending time figuring out “Functional programming vs Parallel vs Async programming” is also a waste of time.

There are many resources available for learning Python here I am putting out some of them for the absolute beginner. No prior experience in programming required a little bit of familiarity with the computer required.

1. Codecademy's "Learn Python" course: This interactive course is a great way to get started with the basics of Python. It includes exercises and quizzes to help you practice and reinforce what you've learned. Nothing to install on system. Everything on browser.

2. LearnPython.org: This website offers a variety of tutorials and exercises for learning Python, including a beginner-friendly tutorial that covers the basics of the language.

 3. https://www.python.org/'s "BeginnersGuide/Programmers" page: This page, provided by the creators of Python, provides a comprehensive guide to the basics of Python programming, as well as links to more advanced resources.

4. "Automate the Boring Stuff with Python" by Al Sweigart: This popular book is aimed at beginners who want to learn how to use Python to automate repetitive tasks. Get the book or read it free online on their website automatetheboringstuff.com/.

I would suggest to start with Codecademy's "Learn Python" course and then check out other.

Codecademy’s course give you a very basic understanding of Python. With 2 and 3 courses you will learn more about Python. In the “Automate the Boring Stuff” you will learn some of the possible things you can do with python. Checkout the 4th is must.

Along with learning python, In parallel, you can give a try to “Data Structure and Algorithm(DSA)”. It’s not like that if you don’t know the DSA, you won’t able to learn python. But trust me it will add a lot of value to your programming career. Read more about DSA's resources data-structures-and-algorithms-learning-resource.

Let us know how you starting your python journey.

Friday, 1 January 2021

Data Structures and Algorithms: Learning Resources

 There are many resources available to learn Data Structures and Algorithms (DSA), including:

  1. "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein: This is a classic textbook that covers a wide range of DSA topics, including sorting algorithms, data structures, and algorithm design techniques.
  2. "Algorithms, Part I" and "Algorithms, Part II" on Coursera: These online courses, taught by Robert Sedgewick and Kevin Wayne of Princeton University, cover a wide range of DSA topics and include video lectures, quizzes, and programming assignments.
  3. "Data Structures and Algorithms" on Udemy: This online course covers a wide range of DSA topics and includes video lectures, quizzes, and coding exercises.
    1.  Master the coding interview data structures Algorithms 
    2. Data structure and Algorithm
  1. "Data Structures and Algorithms" on HackerRank/LeetCode: This website provides a wide range of coding challenges and tutorials on DSA topics, with a focus on practical implementation.
  2. "The Algorithm Design Manual" by Steven Skiena: This book provides an in-depth look at algorithm design techniques and strategies.
  3. "Algorithms" by Robert Sedgewick and Kevin Wayne, on the book website or on the publisher's website, it includes the full content of the book, as well as additional materials, such as lecture slides and exercises.
  4. "Algorithms and Data structures" from OpenCourseWare from MIT: This resource provides free online access to a wide range of DSA materials, including lecture notes, assignments, and exams.

Some of the courses are not in the language you want to learn, but I would suggest giving it a try in what language the instructor is saying to get out the most from the course.

These resources are widely used and recommended, but you should pick the one that suits you best, based on your learning style, schedule, and budget.

Do Solve DSA's Problems on HackerRank/LeetCode. 

Let us know in comment section about your favorite course/book or even any question. 

Monday, 3 February 2020

Running a Python or Django Application with Supervisor

Running a Python or Django Application with Supervisor

Making use of Supervisor

I have lurking around to find a example how can I get my python application work with Supervisor and not able to find some good examples so decided to write one. it will be brief.

Install supervisor

Go with

pip install superviosr

and it’s done. If not please check the official documentation.

Configure the Supervisor

Supervisor have 3 utility.

  1. supervisord
  2. supervisorctl
  3. Web server

supervisord (supervisor daemon) run in background and process all your configuration.
supervisorctl a CLI that can be used to to access some information regarding running process, configuration change, re-start the supervisor daemon etc.
Web server can check the stack/log for the running services.

After installing the supervisor run

echo_supervisord_conf

It will show a sample of supervisor config. Read through it to get some insight.

Let’s create a configuration file that can be used to start the supervisor. (you can copy-past echo_supervisord_conf content and play around that).

In the Installation folder (/etc/supervisor in Ubuntu), do following:–>

create a supervisor.conf file (vim supervisor.conf) with following content

let’s get it one by one :–>

  1. unix_http_server

    Start of configuration

    ; supervisor config file
    [unix_http_server]
    file=/var/run/supervisor.sock
    chmod=0700
    
  2. supervisord

    This section content the functioning configuration of supervisor daemon. Here we have configured below
    a. logfile --> path of file to record log of supervisor daemon
    b. pidfile --> file in which the process id will be logged.
    c. childlogdir --> path of file to record the child process that are running through supervisor daemon.

    [supervisord]
    logfile=/var/log/supervisor/supervisord.log
    pidfile=/var/run/supervisord.pid 
    childlogdir=/var/log/supervisor 
    
  3. inet_http_server and rpcinterface:supervisor:

    This section is used to define the web interface of the supervisor. Here we are defining the port and rpinterface

     [inet_http_server]
     port = 127.0.0.1:9001
     
     [rpcinterface:supervisor]
     supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface
    
  4. supervisorctl

    Define configuration related to supervisor. Let’s define the server URL for now.

    [supervisorctl]
    serverurl = http://127.0.0.1:9001
    
  5. Include section

    Include section to include the addition programs/process related configuration that we are going to run using the supervisor.

     [include]
     files = /etc/supervisor/conf.d/*.ini
    

Running a Python Script using Supervisor

Create a simple python script (sampleapp.py)

import logging
from random import randint
from time import sleep

def main():
    while True:
        i = randint(1,100)
        # If get a digit in multiple of 10 then crash
        if i in [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]:
            logging.error('Generated {}. Application Crashing'.format(i))
            raise Exception('Application Crashing')
        else:
            print('Generated {}. Sleeping for {}'.format(i, str(i//10)))
        sleep(i//10)

if __name__ == "__main__":
    print('Starting the sample app')
    main()

Now define a sampleapp.ini file

  • define a program using [program:program_name]

  • directory - define the directory where your script is ( Program will change to mentioned directory while running the program.

  • command- What to run

    [program:sample_app]
    directory=/path_to_script_containing_folder/
    command=python3 sample_app.py

In Include section we have mentioned that /etc/supervisor/conf.d/*ini will be considered. So either put this sampleapp.ini file in /etc/supervisor/conf.d or create a symlink.

Now run

supervisord

It will start and run in background.

Now Run

supervisorctl

If everything is alright then you will see a response similar to this and we will enter inside the CLI utility of supervisor.

sajjan# supervisorctl
sample_app   RUNNING   pid 25366, uptime 0:01:34
supervisor> 
supervisor> help
default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

Play around it. Now to check the web interface of the supervisor, visit http://127.0.0.1:9001

Running a Django Application using Supervisor

Similarly, we create a django.ini file for our Django application.

[program:application_name]
directory=/path_to_your_project/
command=/path_to_your_virtual_enviroment/python3 manage.py runserver

Restart the daemon and it’s done. Play around it’s different parameter in configuration file.