Category Archives: Programming & Python

requirements.txt

Any application typically has a set of dependencies that are required for that application to work. The requirements file is a way to specify and install specific set of package dependencies at once.

Use pip’s freeze command to generate a requirements.txt file for your project:

In the terminal type:

pip freeze

If you save this in requirements.txt,

pip freeze > requirements.txt

then you can install the dependencies using.

pip install -r requirements.txt

Shell commands

I learnt about shell commands. A Shell is an environment which is used to run our commands and programs. It is an user interface with the operating system. There are many different types of shell. I learnt some commands in bash and C shell. Bash is one of the original shell which developed for UNIX computers. C shell was developed based on model of C programming syntax. To know which shell you are using type echo $0.
I learnt how to define variables both in bash and C shell. Bash is denoted by $. The syntax to define a variable is $a = 5
To print the value of the variable use the command echo $a. In C shell we need to set the environment variable. So the syntax goes like this. setenv a 10. An environmental variable has the name and the value.
To go to the home directory ~/.cshrc and ~/.bashrc for C shell and Bash respectively.

scratch programming for teaching physics concepts

I started to learn a scratch program software to create interactive model for learning. Then I got a task to create a model for: How pressure of gas change with respect to temperature and volume to container: I chose to represent it using Scratch. Initially, I had a problem on bouncing of gas molecules with same angle when it hit the wall of container. Then sanjeev helped me to solve it. In the model, I scaled the pressure in range of 0 to 150 and temperate in range of 0 to 300. But it didn’t give exact value of pressure for given temperature ( I will figure out it soon), because main goal of this model is show how pressure (no. of hit in wall by gas molecules) is affect by temperature and volume. I shared the model in scratch.mit.edu website. click this link to see the model https://scratch.mit.edu/projects/205711792/

Engfmt(Engineering Format) Documentation

Link : https://pypi.python.org/pypi/engfmt/1.1.0

engfmt – It is a package used to read and write numbers in engineering format.

In engineering format a number generally includes the units if available and uses SI scale factors to indicate the magnitude of the number.

For example:

1ns

1.4204GHz

This package is designed to convert quantities between the various ways in which they are represented. Those ways are:

As a tuple:

For Eg: “1ns” – would be represented as (1e-9, ‘s’).

As a string in conventional formats:

For Eg: “1ns” – would be represented as ‘1e-9 s’ or as ‘0.000000001s’.

– This form is difficult to read for people.

– engfmt treats it more as a format meant for machines rather than people.

As a string in engineering format:

For Eg: “1ns” – would be represented as ‘1ns’.

– This form is difficult to read for machines

– engfmt treats it more as a human readable format.

The Quantity class is provided for converting between these various forms.

  • A quantity is the pairing of a real number and units, though the units may be empty.

1.It takes one or two arguments.

Eg: Quantity(1e-9) or Quantity(1e-9, ‘s’)

  • The first is taken to be the value, and the second, if given, is taken to be the units.
  • The value may be given as a float or as a string.
  • The string may be in floating point notation, in scientific notation, or in engineering format and may include the units.

Some examples of the conversions are given below:

For example, any of the following ways can be used to specify 1ns:

As a tuple:

>>> from engfmt import Quantity

>>> period = Quantity(1e-9, ‘s’)

>>> print(period)

1ns

As a string in conventional formats:

>>> period = Quantity(‘0.000000001 s’)

>>> print(period)

1ns

>>> period = Quantity(‘1e-9s’)

>>> print(period)

1ns

As a string in engineering format:

>>> period = Quantity(‘1ns’)

>>> print(period)

       1ns

Note: In all cases, the giving the units is optional.

Conversions – Using quantity object you are able to convert it to any representations:

>>> h_line = Quantity(‘1420.405751786 MHz’)

# to converting to tuple with units

>>> h_line.to_tuple()

       (1420405751.786, ‘Hz’)

# to converting to string with engineering format

>>> h_line.to_eng()

       ‘1.4204GHz’

# to converting to string with conventional format

>>> h_line.to_str()

        ‘1420.405751786e6Hz’

You can also access the value without the units:

# convertig to float

>>> h_line.to_float()

       1420405751.786

# converting to a string with conventional format – without units

>>> h_line.to_unitless_eng()

       ‘1.4204G’

# converting to string with engineering format – without units

>>> h_line.to_unitless_str()

       ‘1420.405751786e6’

# Get the units only

>>> h_line.units

        ‘Hz’

There are many other functions also in this package, which is available in the link given.

Some of the functions are given below:

  • Quantities As Reals – You can use a quantity in the same way that you can use a real number, meaning that you can use it in expressions and it will evaluate to its real value.

For Example:

     >>> period = Quantity(‘1us’)

     >> print(period) 

         1us

  • Quantity Class – Quantity class can be used directly, like shortcut functions.

      For Example:

      >>> from engfmt import Quantity

      >>> h_line = Quantity(‘1420.405751786 MHz’)

      >>> str(h_line)

           ‘1.4204GHz’

      >>> float(h_line)

            1420405751.786

  • Physical Constants – The Quantity class also supports a some physical constants.

      For Example:

     Plank’s constant:

      >>> plank = Quantity(‘h’)

      >>> print(plank)

            662.61e-36J-s

  • Exceptions:

The best thing in this is it also gives Exceptions:

Eg: A ValueError is raised if engfmt cannot convert a string it into a number:

For Example:
			>>> try:
			...     value, units = quant_to_tuple('xxx')
			... except ValueError as err:
			...     print(err)
			xxx: not a valid number.

Installation / Usage :

Requires Python2.7 or Python3.3 or better.

1. Use ‘pip install engfmt’ in the cmd prompt to install.

2. By importing the .py file:

  • Download the package from the link.
  • Copy the engfmt.py from the package and put in the working directory of the python.
  • Import the file by from engfmt import* / from engfmt import Quantity.

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
Host: w3schools.com
name1=value1&name2=value2
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(){
$(this).addClass(‘testing’);
});

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.

-Prathap

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.