Users Online

· Guests Online: 107

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Practical Python Programming for Non-Engineers

Practical Python Programming for Non-Engineers

 

A detailed explanation of Practical Python Programming for Non-Engineers:

Tedious Tasks

Everyone in our life is not a software Engineer. But still, everyone has the stuff that they need to deal with. But programming is one thing that can make a lot of things easier. We all have a lot of to-do things in our daily schedules. And instead of keeping track of that, we can let the machine take care of these things. We can automate our tasks at hand using simple programming. Although there are many of commercial software of there, we cannot spend money on everything. Also, they won’t be as per our own customized needs. Besides, creating our own program is a totally different level. The best language I can think of as of now is Python programming. Let me tell you how.

Python v/s The world

If you have some knowledge of programming previously, especially C or C++, you may know how tedious it is to create a program and then debug it to make it a clean program. But that isn’t the case with Python. And unlike Java, Assembly, JavaScript, or any other language, Python doesn’t have those hard-to-remember syntaxes.

Python is extremely user-friendly and has a defined way to do things. Unlike Ruby, which is another competition for Python programming, where Ruby is too user-friendly and has a lot of ways to do a single Python, python doesn’t offer that. Python programming has indentations and user-readable code.

One of the best examples I can think of is this. Every once in a while, or for some people, we have a habit of downloading videos from YouTube occasionally. So, we can surely download these videos by downloading some random software from the web and using it. But sometimes, it isn’t that easy. Most software on the web is filled with Trojans and viruses.

So, you cannot simply depend on them. God knows what kind of logic bomb is actually penetrated into that software. So, instead of taking mankind through all this trouble, we have Python programming to save us from all these hassles. I found this on the web long ago and have been using it since then. Through this, you can simply download youtube videos with a simple command.

So here is how it goes:

Step 1: Install Python and pip, and select environment variables when installing so you can call Python from the command line from anywhere.

Step 2: Use this command to download the youtube module

pip install youtube-dl

Done. That’s it.

Step 3: Now, whenever you need to download any video, just simply use this command on the command line and download your video:

youtube-dl [OPTIONS] URL [URL...]

Now, you may know exactly what I was talking about. Isn’t this extremely easy rather than downloading any Trojaned software from the internet?

Just note that youtube-dl is a small command-line program to download videos from YouTube.com and a few more sites. It requires the Python interpreter, version 2.6, 2.7, or 3.2+, and it is not platforming specific. It should work on your Unix box, on Windows, or Mac OS X. You can also try >>>youtube-dl –help and check the necessary steps, for example, if you want to download it from HTTPSor HTTP or another port or something like that.

Working on Practical Python Programming for Non-Engineers

Wishing Happy Birthday, GEEK Mode ON

If you are not bored with this blog and are still reading this, I will go to a higher level. Let us assume you are a person who is a geek in your group of friends, and they keep telling you how boring you are, and you are investing 16 hours out of your 24 hours a day on Computer. Then this is one way to show how fabulous you can be. Now I will assume that you already know bits and bytes of Python programming, and I will proceed forward.

Let us say you have one of your friend’s birthday this week, and you need to do something in a geeky way that will impress all of your friends. Then, first, let us make a list of what things are there when you go to a birthday party. There will be Cakes, Candles, and Birthday songs wishing Happy birthday. So let us create a virtual cake whose shade has an equalizer effect corresponding to the “happy birthday” song played in the background. Here, the cake will have candles with flames fluttering randomly.

Also, we will have a fancy display of happy birthday messages. The first thing that we need to do is to import some modules to make them work correctly. So, here it goes (and just remember that this is all one piece of code that needs to be placed in one xyz.py file)

import scipy.io.wavfile as wavfile
import numpy as np
import pylab as pl
import time
import os
import sys
import subprocess
from scipy import mean
from random import randint

Now let us write a code for the wave/mp3 file to run when the code is executed.

FILE = "Song.mp3"
rate, data = wavfile.read(FILE)
t_total = len(data[:,0])/rate
display_rate = 1500 #number of frames processed in one iteration
sample_size = 120
max_display = 90
data_length = len(data) #total number of frames
_min = min([abs(x) for x in data[:,0]]) #max amplitude in the wav
_max = max([abs(x) for x in data[:,0]]) #min amplitude in the wav
correction = 0.645
Note: Everything written in red or starting with the # tag are comments. The values, such as correction value and length of song, will differ from person to person depending upon the chosen song and other stuff. You may need to tweak these things as per your choice of song.

Now comes the Cake part. Here is the piece of code that I wrote for the cake. You can change the cake’s display char and size depending upon how the cake should look like.

cols = int(subprocess.Popen("tput cols",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).stdout.readlines()[0]) #columns in terminal
display_char = "8"
cake_size = 50

Now we need to set the cake on flames so that it looks like a candle is burning.

flame_flutter_rate = 50
FLAMES = [ " . ", ". ", " ." ]
current_flame = ""
os.system("tput civis") #hide cursor

Now, if you are on a Mac, you may need to open iTunes or another player to run the song. So, if you are on a Mac, then uncomment the below os. system command code. (Just simply remove the hash# tag) 

#os.system("open "+FILE)
for _f in range(data_length/display_rate):
# fluttering effect to candle flames
if _f%flame_flutter_rate == 0:
current_flame = (" "*(cols/2 - cake_size/2))+((" "+FLAMES[randint(0,2)]+" ")*(cake_size/5))
print current_flame
# candles
print (" "*(cols/2 - cake_size/2))+(" | "*(cake_size/5))
# cake top layer
print (" "*(cols/2 - cake_size/2))+("-"*cake_size)
bucket = []
mug = []
# mug contains the current frame samples (absolute values) of given sample_size
# average of mugs are put into bucket
for value in data[:,0][_f*display_rate+1:(_f+1)*display_rate]:
mug.append(abs(value))
if len(mug) == sample_size:
bucket.append(mean(mug))
mug = []
bucket = [ (float)((x - _min) * max_display)/(_max - _min) for x in bucket ]
# print the equalizer from the bucket
for value in bucket:
print (" "*(cols/2 - cake_size/2))+"| "+("8"*(value%(cake_size-2)))+(" "*(cake_size-value-2))+"|"
# bottom crust of the cake
print (" "*(cols/2 - cake_size/2))+("-"*cake_size)
# print happy birthday message
os.system("figlet -c -f small Happy Birthday Chetan!")
# sleep to match with the audio

Note: A correction has to be multiplied to sleep time. This is because of several factors like time taken to wake from sleep, type of terminal used..etc.

CHANGE THE VALUE OF correction TO FIT YOUR NEED

time.sleep(((float)(display_rate * t_total) / data_length)*correction)
# clear sreen
if _f != data_length/display_rate-1:
os.system("clear")
raw_input()
Note: This code was written in Python 2.7.9. You may need to make changes if you use Python v3.4/earlier or later.

These are a few examples of what I do when I get bored. Just change the name, and make a few changes for every other person. So, this not only makes Python programming interesting, but it can also do your day-to-day tasks.

You can even create your own to-do list or a notepad or something like that. Now you may think there are ‘n’ a number of programs out there that do the same, so why write your own? The reason for that is customization. When you buy or download software, you don’t know the source code, or if you need some of your own customizations, you may even need to purchase the software. But Python programming is so easy to understand and write that you don’t need to buy anything.

Besides, to tell you the truth, I started to automate my daily tasks using Python programming, but later I got so interested that I started learning everything related to that. Now my job profile is the same; to create programs in Python.

That would be the end of this blog, but before I end this, I will give you some points so that you can go and check some cool automation stuff that people have done with Python, and I am sure that you will be surprised to see what people have done with simple programming. There is also a book named “Automating boring stuff with Python”. Make sure to check that.

So, here goes my list:

  1. Renaming multiple files with a simple code (I am talking about files of more than 100,200 or even 1000)
  2. Creating a Password protected Phonebook (extremely simple)
  3. Automating scripts at work (If you are a System Engineer)
  4. Creating an RSS reader (Cause using a built-in one is too mainstream)
  5. Creating a Password Protected Notepad to save passwords and personal docs(including images and docs)
  6. Downloading mp3 files by using smart search (Downloading mp3 files with similar names directly and randomly from the web)

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.01 seconds
10,260,049 unique visits