Category: python

Python replace a string from Dictionary/Hash mapping

In a situation where we have a dictionary mapping and want to use the mapping to replace a particular string we can use the below code to do the same.

Have a look at below code:

import re

my_dict = {
    "\u0c1C" : "ja",
    "\u0c15" : "ka"
}

string = "కరజ"


for unicode, roman in my_dict.items():
    string = string.replace(unicode, roman)

print(string)

The above code will replace the string from the dictionary. Note that the dictionary has unicode code points that are mapped to specific values. This code will be useful for natural language processing when we deal with unicode range points.

Perl and Python program syntactic differences

If you know Perl Programming then learning Python is not that hard. But many times we would be confusing Perl syntax for Python and might get into trouble while writing Python code. So here I am listing some differences that I know and will keep adding as I knew it.

Variable names

In Perl we would save a variable like $my_variable/@my_varibale etc, but in Python we just can write the names nothing else needed.

Regex Substituion

In Perl we write $in=~s/to/too/g; In Python it is in = re.sub(r'to', 'too', in). Note the difference? In Perl substituion happens in same variable in Python we explicitly need to specify in which variable we are saving the substitution. To replicate the Perl code we have saved in same variable.

Modules

In Perl to import modules we write as use 'modulename' in Python we write as import re. To install module in Perl we have cpan and in Python we use Pip to install modules.

Hash/Dictionary usage

In Perl to assign value to a key we write as $hash{$key} = $value in Python we write as hash[key] = value

Code Indendataion

Perhaps this is the major difference between Python and many other major programming languages. In Perl we have to use curly braces to enclose a block of code into a function/loop/if-else-block in Python we need to indent properly. Also no semicolons are needed to tell the compiler that it is the end of the line we need to just press enter after a line of code. This allows code to look very readable.

Linking a Python script to a web interface with file upload.

In this article I am going to explain how can we link a python script with a web interface. In case you need the GIT hub link for the entire project you can find it here.

Requirements:

  • PHP
  • Web Server(apache)
  • Javascript
  • A Python script that you want to link with the web interface.

The Python Script

#convert text to lowercase 

import sys
#open file using open file mode
fp1 = open(sys.argv[1]) # Open file on read mode -- input file
lines = fp1.read().split("\n") # Create a list containing all lines
fp1.close() # Close file

#write output to a file name it out.txt
fp2 = open("out.txt", "w")

#do something with the text and write it to the file
for line in lines:
	out_line = line.lower()
	fp2.write(out_line + "\n")


fp2.close()

This is our Python script that we want to use it from a Wen Interface where we give input text and the lower case text is shown in the web page itself. Remember I am here just taking a very small example this python script could be doing anything but the basic point is that it takes an input file with some text and writes the output to another file.

To make sure this works, run this python script independently and see if it works as expected.

python3 case_convert.py in.txt
  • Replace your python script with your code.
  • If you have any other script like in PERL, Shell script even then it works fine. You just need to change it in PHP script where it calls the system command.

This script takes a input file in.txt that contains some text and it will be converted to lower case when we execute the script and save it to a output file out.txt

Now lets build our Web page we will call it as index.html It will look like below.

<html>
	<head>
		<title>
			A Web Interface
		</title>

		<script src="js/jquery.js"></script>
		<script src="js/jquery-ui.js"></script>
		<script type="text/javascript" src="config.js"></script>
		<script type="text/javascript" src="js/FileSaver.js"></script>
		<script type="text/javascript" src="js/validation.js"></script>

		<link rel="stylesheet" type="text/css" href="bootstrap-3.3.5-dist/css/bootstrap.css" />
		<link rel="stylesheet" type="text/css" href="css/styles.css" />
		<link rel="stylesheet" type="text/css" href="css/responsive.css" />

		<script type="text/javascript" src="bootstrap-3.3.5-dist/js/bootstrap.js" ></script>
		<script type="text/javascript" src="js/convert.js" ></script>

		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
		 <meta charset='utf-8'>
	</head>
	<body>
		<div>

				<h4 align="center">Convert Upper case to LowerCase </h4>
			<!--common for file and text output-->
			<!--upload form-->
			<form class="form-inline" id="upload" method="post" enctype="multipart/form-data">

				<div class="form-group col-12 col-m-12">
					
					<label class="btn btn-primary" for="my-file-selector">

						<!--<input accept=".doc,.rtf,.docx,.pdf,text/plain" type="file" id="file" name="inputfile" multiple/>-->
						<input accept=".pdf,text/plain,.doc" type="file" id="file" name="inputfile" onchange="startRead(this)" multiple  multiple/>
					</label>

					<!--<button type="button" class="form-control btn btn-default dropdown-toggle" data-toggle="dropdown">
						Select Business type <span class="caret"></span>
					</button>-->
				<!--	<span id="mandatory">*</span> <select name="srclanguage"  id="srclanguage" class="form-control"><option value="" >Choose Type</option>
						<option disabled="true"></option>
				</select>-->

					<button id="submit" type="button" class="btn btn-default navbar-btn" onclick="submittext();">
						Submit
					</button>
					<img style="margin-left:2%;height:4%;position:relative;" id="loading" src="images/processing.gif"></img>
					<br>
			<textarea id="input"></textarea> 
			<textarea id="result"></textarea> 
					<button id="edit" type="button" class="btn btn-default navbar-btn" onclick="edittext();">
						Edit
					</button>
					<br>
					<button id="sample1" type="button" class="btn btn-default navbar-btn sample" onclick="sample(this.id);">
						Sample1
					</button>
					<button id="sample2" type="button" class="btn btn-default navbar-btn sample" onclick="sample(this.id);">
						Sample2
					</button>
					<button id="sample3" type="button" class="btn btn-default navbar-btn sample" onclick="sample(this.id)">
						Sample3
					</button>
					<button id="clear" type="button" class="btn btn-default navbar-btn" onclick="empty();">
						clear
					</button>
					<select style="background-color:#7ba733;color:#fff;" name="savetype"  id="savetype" class="form-control"><option value="" >Export to</option>
						<option disabled="true"></option>
						<option value="txt">.txt </option>
						<option value="csv">.csv </option>
						<option value="doc">.doc</option>
						<option value="rtf">.rtf</option>
						<option value="pdf">.pdf</option>
					</select>

					<button id="download" type="button" disabled class="btn btn-default navbar-btn" onclick="saveasfile();">
						Download
					</button>

				</div> <!-- end of form group div -->

			</form>


			<div class="progressbar">
				<div class="bar">
				</div>

			</div> <!-- end of progressbar div-->

			<!--<div contenteditable="true" id="result"></div>--> <!-- end of result div-->
			<!--			<textarea onkeypress="txtAreaId(this.id);" id="result-text"></textarea> -->

		</div>	<!-- end of container div -->
	</body>
</html>

Looks like a lot but it simply does hold two textareas, button for submitting input text, downloading the output text and linked Javascript and CSS files.

The main Javascript file convert.js which is in js folder will call a PHP script that will execute our python program. I will just list the main function here that is activated when we click submit after inputting some text.

//called when submit button button is clicked
function submittext(){
	$("#savetype").hide();
	$("#download").hide();
	//$("#result").hide();

	//retrieve values from fields
	var srctext = $("#input").val();
	//alert(fromto+" "+srctext);
	
	
	if(typeof srctext =="undefined" || srctext =="") {
		alert("Provide some text...");
		return false;
	}


	$("#loading").show();
	$("#result").empty();
	//Ajax call to upload and submit for conversion
	$.ajax({
		type: 'POST',
		url: "scripts/convert.php",
		//data: "&from=" + from + "&to=" + to + "&text=" + srctext,
		data: "&text=" + srctext,
		header:"application/x-www-form-urlencoded",
		async:false,
		success: function (data) {
			$("#loading").hide();
			//alert(data);
			var tgttext = data;
			$("#result").val(tgttext);
			$('#download').prop('disabled', false);
			$('#language').prop('disabled', false);
			$("#savetype").show();
			$("#download").show();
			$("#edit").show();
		
		},
		error:function  (jqXHR, exception) {
			$("#loading").hide();
			var msg = '';
			if (jqXHR.status === 0) {
				msg = 'Not connect.\n Verify Network.';
			} else if (jqXHR.status == 404) {
				msg = 'Requested page not found. [404]';
			} else if (jqXHR.status == 500) {
				msg = 'Internal Server Error [500].';
			} else if (exception === 'parsererror') {
				msg = 'Requested JSON parse failed.';
			} else if (exception === 'timeout') {
				msg = 'Time out error.';
			} else if (exception === 'abort') {
				msg = 'Ajax request aborted.';
			} else {
				msg = 'Uncaught Error.\n' + jqXHR.responseText;
			}
			alert(msg+" Please try afer sometime");
		}
	});
	return false;
}

So we can see that we are using AJAX which explains why we need jQuery. In the AJax look at the URL it is calling a PHP script convert.php located in scripts directory.

convert.php will look like below. I recommend PHP 7 although it works in lower versions too.

<?php
$text = $_POST["text"];

$fp = fopen("in.txt","w");
fwrite($fp,$text);
fclose($fp);

#call your python script here and make sure your python script is also at same place as this script
$status = system("python3 case_convert.py in.txt");

$fp_out = fopen("out.txt","r");
if ($fp_out) {
    while (($line = fgets($fp_out)) !== false) {
        // process the line read.
        echo "$line";
    }

    fclose($fp_out);
} else {
    // error opening the file.
}

?>

After doing all these. You need to give proper permission the scripts directory or just the two files in.txt and out.txt. Below is the command to do the same.

chown www-data:www-data scripts 
chown www-data:www-data in.txt out.txt

After doing this load your web page in the browser. For example I named the directory as basic-web-interface, so in my browser I open it as http://localhost/basic-web-interface/.

If you have PHP/ APACHE and all other javascript and Bootstrap files installed it should load your web page without any hiccups.

Then provide some input text and see that it gets convert to lowercase and the output text is displayed in the other textarea.

Debugging
  • Check apache error..log file for errors. For ubuntu it is /var/log/apache2/error.log
  • Open Network console(F12) from Browser Developer options and view the API request that is being called. Its request, response, parameters etc.
  • For this Open Developer options, click on network tab and then click submit and view the request/reponse.

Python – print Fibonacci series

This tutorial of python will deal with printing fibonacci series in python. For this we will take user input to print the number of elements in the series.

So in the first step we take input from user and convert into integer as the input function will return string type.

Next we will define 3 variables two to hold the current and next element and other one to hold the total of first two.

Then the actual logic where we calculate total by adding a + b and change values of a and b to total and a respectively after each calculation.

Combining all the above pieces together will look something like below

#print fibonacci series upto a limit given by user input


#take input from user
num = input("How many elements do you want to print in the fibonacci series: ")

#convert it to integer type as the return type of input() is string
num = int(num)

#initally fibonacci series contains 0 and 1
a = 0 
b = 1
print(a)
print(b)

for i in range(1, num+1):
    total = a + b #actual fibonacci series calculation
    print(total)
    a = b
    b = total

        

Comment how do you feel

Python – Print prime numbers up to a given limit

This article of Python deals with printing all prime numbers until a given limit is reached. That is if user gives input as 10, then all prime numbers from 1 to 10 are printed from our program/script.

So the algorithm goes as follows:

First take the upper limit input from user and save it in a variable. Then convert this into int type as the input entered by the user is stored as type string.

#take input from user
a = input("Enter any number: ")
#print(type(a))  #just for understanding

#convert str a into int a

a = int(a)  #since input functon stores always as type string
#print(type(a))  #just for understanding

Then start a loop from 1 to that upper limit number and check if each number is prime or not. So here checking prime for each number is same task or repetitive task. So to do this repetitive task we use a function to check if a number is prime or not.

for i in range(1, a+1): #means start with 1 until given user input, here a+1 because end element is left out in for loop
    prime = checkPrime(i)   #we are passing each element of for loop into a function that will check if it is prime or not
    if(prime == "y"):
        print("%d is a prime number"%(i))
    else:
        print("%d is not a prime number"%(i))

Now its upto the function what we write their. If you are familiar with prime numbers that should be easy. But I will still explain it here. A prime number is defined as a number that is divisible by 1 and itself. So in other words a number to be prime, when it is divided by any other number starting from 1 to that number will give a remainder other than zero. If we get remainder as zero when divided by any number in that range then its not a prime and we need not continue the process of checking prime.

So in programming languages to check remainder we use modulus(%) operator to get remainder of a number. When we use division(/) operator we get quotient. So the entire function will look like something below.

def checkPrime(num):    #function or method syntax use "def func_name(parameters seperated by comma)"
    for i in range(2, num):
        #print(i,num)   #to see what is happening in the loop
        if(num%i == 0):
            return 'n'
    return 'y'

Now when we join all the above pieces together. The code will look like something below.

#this program will print prime numbers upto a given limit

def checkPrime(num):    #function or method syntax use "def func_name(parameters seperated by comma)"
    for i in range(2, num):
        #print(i,num)   #to see what is happening in the loop
        if(num%i == 0):
            return 'n'
    return 'y'

#print(checkPrime(5))
#exit(0)
#take input from user
a = input("Enter any number: ")
#print(type(a))  #just for understanding

#convert str a into int a

a = int(a)  #since input functon stores always as type string
#print(type(a))  #just for understanding


#now let us print all primes until the number given by the user

for i in range(1, a+1): #means start with 1 until given user input, here a+1 because end element is left out in for loop
    prime = checkPrime(i)   #we are passing each element of for loop into a function that will check if it is prime or not
    if(prime == "y"):
        print("%d is a prime number"%(i))
    else:
        print("%d is not a prime number"%(i))

Notice that I had commented some lines that you can uncomment for your understanding purposes. If you have any doubts/suggestions/feedback comment below and let us know.

Using For loop in Python

A for loop is used to perform repititve tasks such as iterating over a list and doing some operation on each item in list.

The above example iterates over each item in fruits list.

break keyword will stop the for loop iteration at that point and skip rest of the iterations.

continue keyword will skip the current iteration and go on with remaining iterations.

python if else condition and indentation – python code for if else

In this article we will see how to use if else condition in python(python3). In python everything is indeneted esepcially conditions and loops with spaces or tabs. And these spaces and tabs are uniform accross the program. If the number of spaces/tabs is violated it is a syntax error.

So this is a simple example of if else block. Note the indentation and colon. Below you can see more examples.

If Elif Else:

Now if else statement with many examples:

You can find the all the above examples in the below code:

# this program is for indentation and if else


# here i am declaring two variables

a = 5
b = 6

print("Value of a is %d, b is %d" %(a, b))

#change the above variables with many possibulites and see what it will print

#now lets test if a is greater or b is greater

#example1
print("Example1")

if( a > b):
    print("a is greater than b")
    #here you can write as many statements as you want
else:
    print("b is greater than a")
    #here you can write as many statements as you want


#in the above code block if a is greater than b then if condition will be true and the statement underneath will execute
#else statemnets under else will be executed        


#now what if there are many if conditions for ex: a>b, a<b, a==b etc


#example2
print("Example2")

if ( a > b):
    print("a is greater than b")
    #here you can write as many statements as you want
elif (a < b):
    print("b is greater than a")
    #here you can write as many statements as you want
else:
    print(" both are equal")
    #here you can write as many statements as you want

#example3
print("Example3")
    
if ( a > b):
    print("a is greater than b")
    #here you can write as many statements as you want
elif (a < b):
    print("b is greater than a")
elif(a == b):
    print("a and b are equal")
    #here you can write as many statements as you want
else:
    print("all the conditions failed")
    #here you can write as many statements as you want

#example4 more complex if else statements

name = 'Manisha'
salary = 10000    #change this value and play with it

print("Example4")
if(salary > 50000):
    print("Salary of %s is %d" %(name,salary))
    print("So she will buy a car")
    print("She will buy a house")
    print("She will not give parties")
else:
    print("Salary of %s is %d" %(name,salary))
    print("She will try for new job")
    print("She will not give parties")

Finding word frequency using Collections module in Python

In this post I am going to discuss how to use Collections module in Python. In simple words collections are nothing but containers that has list, tuple, dict, set etc.

From collections I am going to explain how to use defaultdict and counter subclasses.

Let’s take a basic example:
Suppose we want to get the frequency of words from a text. We do the following.

word_frequency = {}
for words in sent.split(" "):
    if words in word_frequency:
        word_frequency[w]+=1
    else:
        word_frequency[w]=1

But using Collections module our task is more simplified.

from collections import defaultdict
word_frequency = defaultdict(int)
for words in sent.split(" "):
    word_frequency[words]+=1

Using Counter from Collections

from collections import Counter
word_frequency = Counter()
for words in sent.split(" "):
    word_frequency[words]+=1

For more understanding of how this module works visit /https://stackabuse.com/introduction-to-pythons-collections-module/.
Also if you have any doubts or questions please comment on this post so that I can get back to you.