Category Archives: Programming & Python

Dynamic image changing and JavaScript debugging

I and my  colleague are creating a software to track the students plan regarding their progress in their academic . In that we wanted to upload the students pictures when they select their name. I learnt to deal with dynamic images and to debug the code in JavaScript using

<div class=”animptext” id=”animptext”>This is just a text I want to change </div>

document.getElementById(“animptext”).innerHTML = standard;

Two HTTP Request Methods: GET and POST

GET – Requests data from a specified resource
POST – Submits data to be processed to a specified resource
In computing, POST is a request method supported by the HTTP protocol used by the World Wide Web. By design, the POST request method requests that a web server accept the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form.

Note that the query string (name/value pairs) is sent in the HTTP message body of a POST request:
POST /test/demo_form.php HTTP/1.1
POST requests are never cached
POST requests do not remain in the browser history
POST requests cannot be bookmarked
POST requests have no restrictions on data length

jQuery Syntax

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s).
Basic syntax is: $(selector).action()
1) A $ sign to define/access jQuery
2) A (selector) to “query (or find)” HTML elements
3) A jQuery action() to be performed on the element(s)

$(this).hide() – hides the current element.
$(“p”).hide() – hides all <p> elements.
$(“.test”).hide() – hides all elements with class=”test”.
$(“#test”).hide() – hides the element with id=”test”.

$ is just a shortcut for jQuery. The idea is that everything is done with the one global symbol (since the global namespaces is ridiculously crowded), jQuery, but you can use $ (because it’s shorter) if you like.

// These are the same barring your using noConflict (more below)
var divs = $(“div”); // Find all divs
var divs = jQuery(“div”); // Also find all divs, because
console.log($ === jQuery); // “true”

Javascript # and . Symbols – CSS selectors

Code 1:
$(‘#row DIV’).mouseover(function(){
$(‘#row DIV’).addClass(‘testing’);
Code 2:
$(‘.row div’).mouseover(function(){

The hash (#) specifies to select elements by their ID’s
The dot (.) specifies to select elements by their classname
$(‘.row’) will select any element with class=”row”
$(‘#row’) will select the element with id=row

Asteroid game

I developed an Asteroid game from the python course . I did this game since two weeks .I had some difficulties while writing the codes. My team members helped me to over come errors in the program. Here’s a video on the game .I really enjoyed developing this.


Trigonometry Puzzle Game

I have been working on a project for the 9th graders. They had a small puzzle game where they were supposed to find trigonometric ratios and built the puzzle piece by piece by colouring it. Children liked it and as a team we decided to build it in scratch. I took up the responsibility and build a scratch program similar to the game they had in their text book. Here’s the program.

Children were excited and they started building the program themselves.

Python Learning

This week I had a good time with python. Yes, I have learnt few things which I feel worth to share here.
It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. One can find out  the number of occurrence of the list items.
>>>from collections import Counter
# list1 is a list/array which contains numbers
>>>list1 = [1,2,4,5,7,3,5,0,8,5,4,5,4,5,6,7,3,1,2,0,8,3,4,5,2,1,2,3,1,2]
# Counter will convert list1 into dictionary
>>>count = Counter(list1)
>>># most_common function will give the series of all the elements and its corresponding occurrences.
[(5, 6), (2, 5), (1, 4), (3, 4), (4, 4), (0, 2), (7, 2), (8, 2), (6, 1)]
>>># get the most common element by sending 1 to the function
[(5, 6)]
>>>count.most_common(1)[0] # get element & no. of occurrence as dict
(5, 6)
>>>most_common = count.most_common(1)[0][0] # Get the most common element

Strip() :
In the past I have used strip to strip out some letters from a string. Example
But I strip() is also used to remove all whitespace at the start and end, including spaces, tabs, newlines and carriage returns.
I was looking for a module in python which used to rename all the specific type (eg. .docs, .txt, .py) files in a particular directory.
# Go to a directory get all the files of specific type ( lets say “.xls”)
# and rename it with ‘os.rename’
>>> import glob  # used to get all the dir / file from a specified path
>>>import os
>>>path = C:\\Users\\Vaidegi\\Desktop\\*.xls
>>>file_names_with_path = glob.glob(path+“*.xlsx) # to get all the excel files name
>>>for i in range(len(file_names_with_path )):
. . .          os.rename(file_names_with_path[i], student+i+.xls)

Linear, Log and exponent plots – Sratch

I was always confused with log. Sometimes its difficult for me  to handle log based problems . So, I decided to do project in log in scratch to visualize the curve and also to get a clear picture of that.  I started with comparing all the basic curves. First I created Y = X and Y = X^2 as follows,

Then, I went for log(x) and log (x^2). There is no much difference in these two. The plots are same in shape.

At last I plotted the exponent curve (e^x) as follows,

Matplotlib Animation – calling animation from a function

Creating Animation in Matplotlib:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.figure import Figure
import time
def animated_plot():
def data_gen(t=0):
Generator – generating data to plot the graph
cnt = 0
while cnt < 1000:
cnt += 1
t += 1
a = time.localtime() # used to get seconds
yield t, a.tm_sec  # this ‘yield’ will generate two point continuously.
def init():
Initiation – create the base frame upon which the animation takes place.
ax1.set_xlim(0, 10)
del xdata1[:]  # to clear
del ydata1[:] # to clear
line1.set_data(xdata1, ydata1)
return line1,
def run1(data):
To run the animation(draw function)
t1, y1 = data
xmin, xmax = ax1.get_xlim()
if t1 >= xmax:
ax1.set_xlim(xmin, 2*xmax)
line1.set_data(xdata1, ydata1)
return line1,
fig = plt.figure(figsize =(4,1.5))
ax1 = fig.add_subplot(1, 1, 1) #  1×2 mmatrix, 1st subplot
ax1.tick_params(axis=’both’, which=’major’, labelsize=8)
line1, = ax1.plot([], [], lw=2)
xdata1, ydata1 = [], []
animatn = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=1000,repeat=False, init_func=init)
return animatn # must to put return when the animation is calling from a function
Animat=animated_plot() # calling the function. Here ‘Animat’ is must (it just a variable). Otherwise it will return null

Important points to note:

  • Use ‘return’ to return all the values to continue the animation (when you need to call it from a function). Otherwise animation will stop at its first iteration.
  • Call the function with name. ex. ‘Animat=animated_plot()’ not simply ‘animated_plot()’. In the latter case, all the values are collected as garbage values. So it won’t draw anything in the canvas.


Passing ‘Global Variable’ across the files in Python

Passing global variable – Attachment

I have encounter a situation where i need to pass a variable from one file to another file. This is like passing global variable across the files. I’ve created three files to demonstrate this as follows,   # name of the file
import subfile1
import subfile2
print(settings.x)     # name of the file
x = 0
def globle_pas():
global x
x = 1 # Default value of the ‘x’ configuration setting     # name of the file
import settings
def change_glob_var():
settings.x = 9