Google Ads

February 20, 2018

Use Python objects to process and store csv file data

In Python data from csv files can be easily loaded and processed for analysis or automating other jobs. In this tutorial, we will see how data from a csv file can be stored easily in Python objects and spit out the processed data from an object as necessary. For this exercise, we assume we have a csv file containing temperature data for 28 days where the first column contains days records and the second column contains temperatures records. First we will load the csv file's temperatures data into an object and then show the data and temperature average from the object. After that, we find and store the differences in days temperature from the object to another object. Finally, we save the differences in days temperature data from the object to a csv file. A sample of the csv file contents is shown below:

1 27
2 25
3 23
4 30
5 19
6 20
7 13
8 30

Now, in Python first we will have to create an object for holding these data from the csv file. For this, in the Python object we need to have two lists, one for days records and the other one for temperatures records. Now, we define the Python object as below:

# define object for holding a set of temperature data
class TempData():
    def __init__(self): = [] # list of days
        self.temp = [] # list of temperature

Now, we create two object instance variables of this type where one will hold temperatures data as in the csv file and the other one will hold temperature differences data.

csv_imported_data = TempData() # create object for holding csv file temperature data
processed_data = TempData() # create object for holding processed data

Now, we can open the csv file and load the csv columns data into the object lists for days and temperatures data.

csv_file = open('data.csv', 'r') # open the csv file for reading
reader = csv.reader(csv_file) # pass the file to csv reader object for extracting data

for row in reader: # get the row data from the csv file[0])) # get the first column data
    csv_imported_data.temp.append(float(row[1])) # get the second column data

csv_file.close() # close the csv file

Now, lets show the temperature data from the object:

# print temperature data from the temperature object
for i in range(len(
    print([i], csv_imported_data.temp[i])

Now, lets show the average temperature from the object's temperature data:

# show average temperature from the temperature object
print('Average temperature for', len(, 'days was', sum(csv_imported_data.temp)/len(csv_imported_data.temp))

Now, lets find the temperature difference from the object's temperature data and store difference data into another object of same type:

# find difference between temperatures and store in temperature data object
for i in range(len(
    if (i == 0): # no difference for first data[i])
    else:[i] -[i-1]))
        processed_data.temp.append(abs(csv_imported_data.temp[i] - csv_imported_data.temp[i-1]))

Now, we show the temperature difference data from the object:

# print temperature difference data from the temperature object
for i in range(len(
    print([i], processed_data.temp[i])

Finally, we save the temperature difference data from the object to a new csv file so that we can retrieve the temperature difference data without running the Python script again:

csv_file = open('processed_temp_data.csv', 'w', newline='') # create a new csv file for writing
csv_writer = csv.writer(csv_file) # pass the file to csv writer object for writing data

for row in zip(, processed_data.temp): # create the row data from the the temperature object


Download the full example code and CSV files:


December 16, 2017

Ubuntu improve WIFI performance and reliability

If you need a stable internet connection through WIFI then it is important that WIFI performance remains consistent and WIFI connection is reliable at any given time. This is vital if you are running any kind of server or using torrents. Unfortunately, the power management feature exists for different devices within Ubuntu promotes unstable and slow WIFI connection which is enabled by default for WIFI devices. This creates problems such as automatic turning of WIFI and loosing of WIFI connectivity randomly while the system stays idle for a while or there is no inbound packets coming to your system. To check whether you have power management active for your WIFI device execute the following command into the terminal:

iwconfig | grep -w "Power Management"

Now if it is active then you can easily turn it off by executing following command:

sudo iwconfig wlan0 power off

where you have to replace wlan0 word with your WIFI device name which can be found by executing following command:

ifconfig -a

The above command will list all the network devices present in the system including WIFI devices. If the terminal says that command not found then install the program by installing the package net-tools. To do that, execute the following in the terminal:

sudo apt-get install net-tools

Now to make this setting persistent between the boots you can do several things. First, make changes to the /etc/pm/config.d/blacklist file. To do that, in the terminal execute the following to open the file:

sudo gedit /etc/pm/config.d/blacklist

Add the following line in the /etc/pm/config.d/blacklist file:


Second, make changes to the /etc/pm/power.d/wifi_pwr_off file. To do that, in the terminal execute the following to open the file:

sudo gedit /etc/pm/power.d/wifi_pwr_off

Now add the following lines in the /etc/pm/power.d/wifi_pwr_off file:

/sbin/iwconfig wlan0 power off

Do not forget to replace the word wlan0 with your WIFI device name and save the file. Now, execute the following command it make the file executable upon every boot:

sudo chmod +x /etc/pm/power.d/wifi_pwr_off

Third, make changes to the /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf file. To do that, in the terminal execute the following to open the file:

sudo gedit /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf

Now in the /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf file, replace wifi.powersave = 3 with wifi.powersave = 2 and save the file.

That is it!


December 3, 2017

Yet another HTML cache busting technique

If you are coding your web application or web site from the ground up then you may be already facing problems where changing individual files does not get reflected immediately. This is due to browsers caching some of the files to speed up loading performance and also to save bandwidths. The process of avoiding caching in the browsers is called cache busting.  There are already a ton of solutions out there which can be applied to your HTML code to avoid caching where some are tedious to maintain and others are a breeze. Although not very well known technique here I am presenting how to use hashing technique to do HTML cache busting. This technique can be well applied to both client side or server side scripting languages as well as using any cryptographic hash functions. The method I have selected for my web development is client side PHP scripting language and SHA-1 hash function. Using this method the following HTML code shows how to import external CSS file with cache busting feature:

<link rel="stylesheet" <?php echo "href=\"style1.css?v=" . sha1_file('style1.css') . "\"" ?>>

In the above code, PHP script will inject HREF attribute into the link tag. The ?v=" . sha1_file('style1.css') code fragment will change each time the CSS file is modified as well as add a argument to the CSS file name which will force browsers to load the CSS file. As the SHA-1 hash function is unique each time for a particular CSS file data set the above code works well as intended.

The following code snippet shows that the same technique can be applied to import JavaScript files with cache busting feature:

<script <?php echo "src=\"load.js?v=" . sha1_file('load.js') . "\"" ?>></script>