python-user defined module


In python we can import user defined modules, modules are programs written by the users, here is a simple module and using that module in the program.


define a module(user defined name it as anil.py)

 def add(s,t):
    u = s + t
    return [s,t,u]

def sub(s=0,t=0):
    u = s - t
    return [s,t,u]

importing the user defined module

import anil

anil.add(2,3)

python - List and Dictionary comprehension

python comprehension is a good construct with that code is compressed to a greater extent. here is a sample example given for that.





#print even numbers from 1 to 10; 2 4 6 8
print([i for i in range(1,11) if i % 2 == 0])


#print dictionary; key as key and value as length of key
print({i:len(i) for i in ["python","ruby","c"]  })

python-passing variable length of arguments



#variable length arguments in function
def add(*x):
    sum1 = 0
    for i in x:
        sum1 += i
    return sum1

print(sum1)  #sum1 is out of its scope
print(add(2,3,4))
print(add(2,3,4,5,6,7))

python - passing one function to another function




#passing function to a function

def do(x,y):
    # this function is to check if first argument is greater than second
 
    if x > y:
     
        return [True,x,y]
    else:
        return [False]

def sub(func):
    lis = func
    #lis = [False]
    #lis = [True,3,2]
    if lis[0] == True:
        return lis[1] - lis[2]
    else:
        return "sub not possible"



print((sub(do(2,3))))  # none
print((sub(do(3,2))))  # 1

Python- Read from csv file without csv package



The following function is used to read an excel(csv) and return a dictionary which contains all the lines of the csv file


#the function takes a csv file as input and returns a dictionary
def read_annual(file_name,report):
    # The function is defined to read data from the csv file
 
    try:      # to handle exception while opening file
        f = open(file_name, 'r') #to open the file
    except:
        return "File Not Fount"
 
    for line in f:  # to read line by line from the csv file
        mod_line = line.replace('","',';') #to replace "," by ;
        mod_line = mod_line.replace(',','') #to remove 1000 seperator
        mod_line = mod_line.replace(mod_line[0],'') #to remove the first and last double quote
        mod_line = mod_line.replace(mod_line[0],'') #to remove the dot
     
        line_list=mod_line.split(";")  # to split the line into strings(words) based on ;
        key=line_list[0] # key for dictionary
        value = line_list[3].replace('\n','') #to replace new line by empty char
        report.setdefault(key, []).append(value)
        #report.append(value) #to push data into the report dictionary
    f.close()
 
    return report

Cigarette Smokers problem

this is a description of CSP.















/*
Cigarette smokers problem implemented using threads
Thread synchronization was done using semaphore
To treminate the program Each smoker is given two rounds of smoking

*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<semaphore.h>
#include<pthread.h>
int table_used=1,generated_item[2],generated=0;
char *item[]={"tubaco","paper","matches"};
sem_t table;
void *agent(void *arg)
{
// agent thread function that has infinite number of ingredians
int i,j,k=0;
int count=0;
while(1)
{
sleep(1);
sem_wait(&table);
if(count==6) exit(0);
if(table_used==1)
{
i=k;
j=i+1;
if(j==3)
j=0;
k=j;
generated_item[0]=i;
generated_item[1]=j;
printf("agent produces %s,%s\n",item[i],item[j]);
generated=1;
table_used=0;
count++;
}
sem_post(&table);

}
}
void *smokeri(void *i)
{
//int count=0;
while(1)
{
sleep(1);
sem_wait(&table);
if(table_used==0)
{
if(generated && generated_item[0]!=(int)i &&
generated_item[1]!=(int)i)
{
printf("SMOKER%d completed his smoking\n",(int)i);
printf("\n");
//count++;
table_used=1;
generated=0;
}
}
sem_post(&table);
//if(count==5) exit(0);
}
}
main()
{
pthread_t smoker1,smoker2,smoker0,agnt;
sem_init(&table,0,1);
printf("SMOKER0 has tobacco\n");
printf("SMOKER1 has paper\n");
printf("SMOKER2 has matches\n");
pthread_create(&agnt,0,agent,0);
pthread_create(&smoker0,0,smokeri,(void*)0);
pthread_create(&smoker1,0,smokeri,(void*)1);
pthread_create(&smoker2,0,smokeri,(void*)2);
while(1);
}

- Copyright © Anil Kumar Mula - Skyblue - Powered by Blogger - Designed by Johanes Djogan -