Two Important Reason for Testing are …

Back in 2020 during the start of the first wave in the UK, before the lockdown, I gave a quick talk on testing with Pytest to a few developer’s colleagues in the office.

Few slides were titled why test and elaborated on various (more like 8) reasons for why we should write tests?

Two reasons that I believe are most important are….

Testing enables better design of the software.

Testing is not about writing tests. It is about writing a testable code.

In order to test a single unit, we need to be able to instantiate that unit standalone – without the rest of the system.
This leads to a loosely coupled code with explicit dependencies.
This leads to a better separation of concerns, and also follows the single responsibility principle


Testing enables quick feedback

Testing provides quick feedback and makes development more productive leading to more confidence when making any changes and eventually makes refactoring a codebase easier.

Whats your reason for writing or not writing tests?

Advertisement

Get Outlook Entries With Python 3

This was an old post, and was written in python 2, but that refused to work in python3 as pointed by win

Today found some time to look at this and fixed the code. So here’s new improved code that works both in python 2 and python3. The new code gives user the ability to change the date time format as suggested in the first comment.

import win32com.client
import datetime
from collections import namedtuple


event = namedtuple("event", "Start Subject Duration")


def get_date(datestr):
    try:  # py3
        adate = datetime.datetime.fromtimestamp(datestr.Start.timestamp())
    except Exception:
        adate = datetime.datetime.fromtimestamp(int(datestr.Start))
    return adate


def getCalendarEntries(days=1, dateformat="%d/%m/%Y"):
    """
    Returns calender entries for days default is 1
    Returns list of events
    """
    Outlook = win32com.client.Dispatch("Outlook.Application")
    ns = Outlook.GetNamespace("MAPI")
    appointments = ns.GetDefaultFolder(9).Items
    appointments.Sort("[Start]")
    appointments.IncludeRecurrences = "True"
    today = datetime.datetime.today()
    begin = today.date().strftime(dateformat)
    tomorrow = datetime.timedelta(days=days) + today
    end = tomorrow.date().strftime(dateformat)
    appointments = appointments.Restrict(
        "[Start] >= '" + begin + "' AND [END] <= '" + end + "'")
    events = []
    for a in appointments:
        adate = get_date(a)
        events.append(event(adate, a.Subject, a.Duration))
    return events


if __name__ == "__main__":
    events = getCalendarEntries()

Sample result

[event(Start=datetime.datetime(2020, 4, 7, 8, 0), Subject='Quick Project Review (30 mins to save future work)', Duration=30),
 event(Start=datetime.datetime(2020, 4, 7, 9, 0), Subject='Billing detail', Duration=15),
 event(Start=datetime.datetime(2020, 4, 7, 9, 0), Subject='DF DW', Duration=60),
 event(Start=datetime.datetime(2020, 4, 7, 10, 0), Subject='hw', Duration=1),
 event(Start=datetime.datetime(2020, 4, 7, 10, 50), Subject='Canceled: Daily Standups are back and they are better than ever..!', Duration=10),
 event(Start=datetime.datetime(2020, 4, 7, 11, 0), Subject='Canceled: Sprint Planning / Refinement (Alternating Weeks)', Duration=120),
 event(Start=datetime.datetime(2020, 4, 7, 12, 0), Subject='Daily Cafe. / FIKA', Duration=30),
 event(Start=datetime.datetime(2020, 4, 7, 12, 0), Subject='CABI COP Weekly Meeting', Duration=30),
 event(Start=datetime.datetime(2020, 4, 7, 12, 0), Subject='Design System Engagement', Duration=30),
 event(Start=datetime.datetime(2020, 4, 7, 16, 0), Subject='rasise invoices', Duration=90),
 event(Start=datetime.datetime(2020, 4, 7, 16, 30), Subject='shutdown', Duration=15)]

Related

Exploring MFEM

mfem c++ fem library

With little time I have after office and playing with kids , I am devoting some to this MFEM library.

Really good piece of software. Exploration continues.

So What is MFEM?

No, it’s not Ministry of Finance and Economics Management, it’s …….

MFEM is a lightweight, general, scalable C++ library for finite element methods and available at http://mfem.googlecode.com

The goal of MFEM is to enable research and development of scalable
finite element discretization and solver algorithms through general
finite element abstractions, accurate and flexible visualization, and
tight integration with the hypre linear solvers library. Its features
include:
– 2D and 3D, arbitrary high-order H1, H(curl), H(div), L2 and NURBS
elements.
– Parallel version scalable to hundreds of thousands of MPI cores.
– Conforming or nonconforming adaptive mesh refinement (AMR),
including anisotropic refinement.
– Galerkin, mixed, isogeometric, DG and DPG discretizations.
– Support for triangular, quadrilateral, tetrahedral and hexahedral
elements with curved boundaries.
– Lightweight interactive OpenGL visualization with GLVis,
http://glvis.googlecode.com.

An interactive documentation of MFEM’s serial and parallel example
codes can be found here
MFEM is freely available under LGPL 2.1.

 

What can Software Teams Learn From Aviation?

This pycon talk combines my two passions, aviation and software development. Grab a cold coffee and watch this very interesting talk. Just a little over half an hour, if you have any interest in any of this two stream, you will like the talk.

What can Python-based software teams learn from aviation? Why should software always fail hard? What’s wrong with too many error logs? And why are ops people already like pilots? Learn all this, and about planes, too.

If in hurry, you can download the PDF of the presentation.

Government and software.

Read this sometime in Jan. But unfortunately lost the link to where I read it.

Regulating a society is like writing software. It sounds easy to amateurs, but it can be amazingly difficult.

Software programmers typically cannot prove that their software will work. At best, in many practical cases, they can test it for typical cases. Unfortunately, government regulators don’t follow any of the good practices software programmers use. Worse: they never accept bug reports.

Any programmer will tell you what to do when testing is hard and failure is not an option: keep the software as simple as possible.

That’s not at all what government do. And, unsurprisingly, they are full of bugs

How to Read HDF5 with Python – a simple introduction

HDF keeps coming into my world from one place or the other. A colleague is working on it, then it showed up in a side project i am working and now I stumbled on a video praising hdf5. So finally decided to dig into it and find more about it.

  1. HDF stands for Hierarchical Data Format. This (HDF, HDF4, or HDF5) is a set of file formats and libraries designed to store and organize large amounts of numerical data. Originally developed at the National Center for Supercomputing Applications, it is supported by the non-profit HDF Group.
  2. HDF5 format supports any kind of data for digital storage regardless of their origin and some.
  3. HDF5 stores data in a highly organised and hierarchical format
  4. HDF5 is highly efficient chunked input output operations.
  5. HDF5 allows inclusion of metadata and attribute.
  6. The format is platform independent and is widely used in scientific codes.
  7. HDF5 data model consists of two major objects, Datasets and Groups.

Enough theory? Here’s a super simple introduction to reading hdf5 data via python.

pytables_intro_how_to_read_hdf5_with_python

enjoy!

Read more about HDF5 here [pdf] and here

Simple Code to Find Which Excel Row is Being Modified

Excel’s inbuilt functions are powerful. They can achieve a lot. Almost anything can be conquered with these functions.

Well almost……everything!!

Recently needed a function to know which row is being modified. As far as i know, there’s no way this can be achieved by using excel’s function. This is where little bit of vba helps.

Here’s the code that helped.

Private Sub Worksheet_Change(ByVal Target As Range)
    If (Target.Column = 3 Or Target.Column = 4) Then
        If Target.Row > 1 Then Cells(1, 6) = Target.Row
    End If
End Sub

In the above sheet change method, the code checks if any row has changed in the columns 3 and 4 and if something has changed beyond row 1 in these columns, the F1 cell is updated with the row number.

Simple and small code but very useful when one wants to know which row is getting modified in excel.

The information about the row being modified can be used to do lot of things using excel in built functions.

Now think where can one use this. Stay tuned I have a follow up post on the use.

Few Toy Programs to Mesh Excel and Fortran together

Fortran DLL and Excel
Fortran 90 with Excel – gfortran example is one of the post that constantly gets lot of attention as measured by the top posts sidebar widget on this blog.

So thought about revisiting it and this post is to show some toy programs that shows how to link fortran program as a dll with excel.

I am using gfortran and the following simple fortran programs to create a DLL which is called from excel.

Continue reading

Sieve of Eratosthenes

In mathematics, the sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. Here’s a Python Implementation of sieve of Eratosthenes.

"""
Created on Wed Sep 25 14:07:59 2013
 
@author: sukhbinder
 
Python Implementation of sieve of Eratosthenes
 
In mathematics, the sieve of Eratosthenes , one of a number of prime number sieves,
is a simple, ancient algorithm for finding all prime numbers up to any given limit.
It does so by iteratively marking as composite (i.e. not prime) the multiples of 
each prime, starting with the multiples of 2.
 
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
 
"""
 
import numpy as np
 
def SOE(n):
    is_p = np.ones((n,),dtype=bool)
    is_p[:2]=0
    nmax = np.int(np.sqrt(n))
    #print nmax,range(2,nmax)
    for j in range(2,nmax+1):
        is_p[2*j::j]=False
        #print is_p.nonzero()
    #print is_p
    return is_p.nonzero()

Prime Seive

So what are the prime weeks of the year?
print SOE(53)

[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

What are the prime days in the month?
print SOE(32)

[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

What are the prime days in the year?
print SOE(366)

[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
317, 331, 337, 347, 349, 353, 359]

Plants Become Weeds when they…..

FortranDLL in visual studio

How to Create Fortran DLL in Visual Studio? – The simple way


Plants become weeds when they obstruct our plans or our tidy maps of the world. Fortran is also perceived in the same way. Although still a formidable programming language, many now treat it as weeds.

They encounter it and they go back to fortran 66 mindset. Fortran’s new capabilities are discounted and people look past the new features of the language to talk about the horrible “go to” mess!! They might want to look at this Myths Of Fortran post!

There’s so much legacy code in fortran, but no one wants to touch it. The easiest solution commonly used is to convert it into a dll or some library and use with new languages. This explains the popularity of the post how to create fortran dll in visual studio on this blog.

This brings me to today’s post. Here’s an even more convenient animated gif to explain the process of converting a fortran program to a dll in visual studio with intel fortran compiler!

Pick-up Sticks

pick-up-sticksAbout a year ago, I was brushing my java programming skills. Mr. K, my colleague from the next cubicle, a seasoned java professional was helping me.

At this very time, my mom came to visit us and she brought this game for my daughter.

So getting bored of creating dummy classes for fictitious banks and its clients to learn java oops programming, I decided to make the game my daughter was playing with physical skicks to something that can be played on the browser.

This is the code that I developed. Not complete game but a decent effort that taught me more than I ever learnt from those dry tutorial examples.

And if you would like to play then, visit this page.

Here’s the full code of the game for anyone to play and improve upon.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;


import javax.swing.JApplet;


public class StickFree extends JApplet implements MouseListener {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	Image img;
	Graphics off;
	int w,h,nsticks,stickdetected;
    Stick sticks[];



	public void init(){
		int x1,y1,x2,y2,tmp;
		w=400;
		h=320;
		nsticks=8;
		stickdetected = 0;
		setBackground(Color.black);
        setSize(w,h);
	    img = createImage(w,h);
	    off = img.getGraphics();
        addMouseListener(this);
        sticks = new Stick[nsticks];
		for (int i=0;i<nsticks;i++){  //for (int i=nsticks-1;i>=0;i--){
		  x1 = (int) (Math.random()*w/2-1);
		  y1 = (int) (Math.random()*h-1);
		  x2 = (int) x1+w/2 ;//(Math.random()*w-1);
		  y2 = (int) (Math.random()*h-1);
		  tmp = (int) (Math.random()*5+1);


		  switch(tmp) {
          case 1:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.red, off);break;
          case 2:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.green, off);break;
          case 3:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.white, off);break;
          case 4:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.yellow, off);break;
          default:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.pink, off);break;
          }


         sticks[i].paint();
		}


	}

//	public boolean isontop(int k){
//		boolean ans =true;
//		int i=0;
//
//		for (i=nsticks-1;i>=0;i--){
//			if(i == k) continue;
//			if(sticks[i].isVisible()){
//
//			}
//		}
//
//
//		return ans;
//
//	}

	
	public boolean isontop(Stick sk1){
		int index = sk1.getId();
		
		System.out.println("Index ...from isontop ....."+index);
		
		//boolean ans;
		Stick sk2 = null;
		
		double x ;
		double y ;
		
		for (int i= index; i<nsticks-2;i++){
			sk2 = sticks[i+1];
			
			if(sk2.isVisible()){
				x = (sk2.intercept - sk1.intercept) / (sk1.slope - sk2.slope);
				y = (sk2.slope * x) + sk2.intercept;
				
				
				////
				double a,b,c;

		        a = sk1.slope*x+sk1.intercept-y;
				b = Math.pow(Math.pow(sk1.slope,2.0)+1.0,0.5);

				c = Math.abs(a/b);
//				System.out.println("c = "+c);
				if(c <= 2.0) {
					return false;
				}

//		        System.out.println("cccc"+c+" "+b+" "+a+"slope "+slope+"in "+intercept);
				//return ans;
				////
			}
		}


		return true;

	}

	public void paint(Graphics g){
		 g.drawImage(img, 0, 0, this);
	}

	public void mouseClicked(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {


	}

	public void mousePressed(MouseEvent e) {


	}

	public void mouseReleased(MouseEvent e) {
        int xx,yy;
		xx=e.getX();
		yy=e.getY();
 	    System.out.println(xx+ " "+ yy+"  n "+stickdetected);

       if(stickdetected >=nsticks) return;

    	   for(int k=0;k<nsticks;k++){
    		   System.out.println(k+"  ....  order ...  ....."+sticks[k].getId());
    		            if(sticks[k].isVisible()){
    		            	
    		            	
    	                 if(sticks[k].isinside(xx,yy)){
    	                		System.out.println("Index ...  ....."+k);
    	                	if(isontop(sticks[k])){
    	                		sticks[k].setVisible(false);
    	                    	   stickdetected++;
    	                    	  break;	
    	                	}
    	                 }
    		          }
    	   }


                       repaint();
                       paintall();
      }

	public void paintall(){
      for(int i=0;i<nsticks;i++){
    	  sticks[i].paint();
      }
	}




}


class Stick {

	private int x0,y0,x1,y1,id,top,down;
	private Color col;
	private Graphics g;
	private boolean visible;
	double slope,intercept;
	Line2D lineShape = null;
	
	public Stick(int id_,int x0_,int y0_,int x1_,int y1_,Color  col_,Graphics g_){
		id = id_;
		x0=x0_;
		y0=y0_;
		x1=x1_;
		y1=y1_;
		col =col_;
		g=g_;
		visible = true;

		slope = ((double)(y1-y0)/(double)(x1-x0));
		intercept = y0-slope*x0;
		top=-1;
		down=-1;
//		System.out.println("id "+id+" x0 "+x0+" x1 "+x1+" y0 "+y0+" y1 "+y1+" slop "+slope+ " in "+ intercept);

		lineShape = new Line2D.Float((float)x0,(float)y0,(float)x1,(float)y1);
	}

	public void paint(){
     if(visible){
    	 
    	
    	 //((Graphics2D)g).
	   g.setColor(col);
	   ((Graphics2D)g).setStroke(new BasicStroke(3));
	   for(int i=0;i<2;i++){
		      g.drawLine(x0+i, y0+i, x1+i, y1+i);
		    }
	    g.drawString(""+this.id, x0, y0);
     }else{
    	 g.setColor(Color.black);

     }



	}

	public void clear(){
		  g.setColor(Color.black);
		    for(int i=0;i<2;i++){
		      g.drawLine(x0+i, y0+i, x1+i, y1+i);
		    }

	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.clear();
		this.visible = visible;
	}

	public boolean isinside(int x,int y){
		boolean ans=false;
		double a,b,c;

        a = slope*x+intercept-y;
		b = Math.pow(Math.pow(slope,2.0)+1.0,0.5);

		c = Math.abs(a/b);
//		System.out.println("c = "+c);
		if(c <= 2.0) ans=true;

//        System.out.println("cccc"+c+" "+b+" "+a+"slope "+slope+"in "+intercept);
		return ans;
		
//		double yyyy =  slope * (x0-x)  + y;
//		
//		System.out.println("x0  "+x0+" y0 "+y0+"  & x1 "+x1+" y1 "+y1+"    ------------ x "+x+"  y "+y+"   ???  "+ yyyy);
//return (y0==yyyy);
	}

	public Line2D getLineShape() {
		return lineShape;
	}

	public int getId() {
		return id;
	}

}



And if you would like to play then, visit this page.

I wish WordPress.com allowed embedding java applets. ;(

The Feast

Innovative, fast, stable and efficient is how the author describe this high performance numerical library named FEAST!

The FEAST solver package is a free high-performance numerical library for solving the standard or generalized eigenvalue problem, and obtaining all the eigenvalues and eigenvectors within a given search interval. It is based on an innovative fast and stable numerical algorithm named the FEAST algorithm which deviates fundamentally from traditional eigenvalue algorithms.

The FEAST algorithm combines simplicity and efficiency and offers many important capabilities for achieving high performance, robustness, accuracy, and scalability on parallel architectures.

The code can be downloaded from the following website: the feast fortran library

Will give it a spin in couple of weeks time?

Rubber Duck Problem Solving Method

Another of my fav non aviation blog Coding horror had this post today.

I love this particular story because it makes it crystal clear how the critical part of rubber duck problem solving is to totally commit to asking a thorough, detailed question of this imaginary person or inanimate object. Yes, even if you end up throwing the question away because you eventually realize that you made some dumb mistake. The effort of walking an imaginary someone through your problem, step by step and in some detail, is what will often lead you to your answer. But if you aren’t willing to put the effort into fully explaining the problem and how you’ve attacked it, you can’t reap the benefits of thinking deeply about your own problem before you ask others to.

Do read the full story here.

It has happened to me many times. As I explain my problem to a colleague, something clicks and I see the solution or the flaw in my approach.

Holi and How i was not able to resist myself?

I was not able to resist myself.

A week back I thought I won’t do anything like this on Holi. Decided to just wish like a normal person with a simple Internet copied picture and abstain from doing any coding.

The abstain worked only till the day of Holi. Once I began getting those static greetings, I fired eclipse and here’s the result.

A java applet to play Holi on your screen


image

Click on the picture and enjoy. Happy Holi!!