Posts marked with "Python" in categories

Finding The Value Of Pi

Everyone must have heard of Pi, the irrational number that never ends. It goes by this :

3.14159265...

I have been learning about Pi since I was in 8th grade. All the time in school, I was curious about to which Pi extends.

Now, I know the extend – Infinity :P. I dedicated my precious Christmas vacation for finding the useless value of Pi.

Why ? Curiosity. Simply, the curiosity made me want to do this. Besides, it’s always fun to do something interesting.

... [READ MORE]

Replace Strings in Files With PHP & Python

There are softwares already that helps to replace a string in a file. Suppose, if you’re making a program that requires saving some data in to a file. By “adding”, the program is actually replacing a word. Let’s see how it’s done in PHPPython.

This is the file where we are going to replace the word :

thisWord
I got thisWord for making it thatWord
replace thisWord already my friend.

We replace the word “thisWord” to “thatWord” containing in this file.

... [READ MORE]

Folder Recursion in PHP & Python

A folder has files and sometimes sub directories. If we use the normal function for obtaining contents of a folder, we won’t get the details of the files in the sub directories. In this case, we have to look over into the sub folders and into other sub folders within this sub folder. This looking up of files deep down is called Recursive Folder Searching.

By doing this, we can search for a file or do various actions with each files thus recursed. An example case of this is the replacing softwares like regexxer. It uses recursion to replace a string to another in multiple files in the same directory even under any sub folders.

... [READ MORE]

Word Palindrome Check in PHP & Python

Palindromes are unique words that are read the same forward and backward. We can identify them using out eyes and brain. How about we identify using a simple program in PHPPython ?

PHP

<?php
$word = strtolower("Malayalam");
$splitted = str_split($word);
$reversedWord = "";
$length = strlen($word);
for($i = 0; $i < $length; $i++)
	$reversedWord .= $splitted[$length - $i - 1];
echo $word == $reversedWord ? "It's a palindrome !n" : "It's not a palindromen";
?>

Instead of the loop to reverse the word, you can also use :

... [READ MORE]

Executing Terminal Commands in Python & PHP

Python & PHP are great languages and there are many similarities between them. In this short post, you will learn how to execute terminal commands in PHP & Python.

PHP

As I said before many times in other posts, there areexec andsystem command to execute commands :

exec("firefox ‘http://subinsb.com’");

Or in the other way :

system("firefox ‘http://subinsb.com’");

Python

In Python, there is no exec or system function. But there is a system function in the os nodule. So, you have to include the os module first and then execute the command :

... [READ MORE]

Infinite loop in PHP & Python

Infinite loops are loops that doesn’t have a defined ending. It will continue to run until the program it’s running in is terminated. Normally an infinite loop is created using while loop and is used for continuity of the same program.

In this short tutorial, you will learn how to make an infinite loop in both PHPPython.

PHP

There is no need of defining any variables.

while(1){
  print "Hello"
}

 

... [READ MORE]

Goodbye AppFog, Hello OpenShift

AppFog and Heroku were one of the most popular PaaS services. But due to the limitations of service many moved out. At first when AppFog came, there were a lot of cool awesome features more than Heroku. For the Free plan AppFog had SSL, Custom Domain and other features. But by the end of February 2013, AppFog removed Custom Domain from free plan. Many customers (including me) got sad, angry and were trying to find another PaaS service. And Here it is, the next Awesome PaaS service – OpenShift. Here is the best part : OpenShift is created by the Open-Source legend Red Hat. How awesome is that ? OpenShift is easy to use, fast and reliable. I used it for like 5 hours now and I already liked it. I have moved the Demo site from 3owl to OpenShift.

... [READ MORE]

Create a Simple Browser Using PyGTK, GLADE And MozEmbed

Let’s start creating softwares using Python GTK (PyGTK). Python is the easiest programming language ever and If you are coming from other Programming Languages such as PHP, Here is something that you should understand :

  1. Python doesn’t have semi-colons for closing a code.
  2. function in PHP is def in Python.
  3. Spaces from the starting of a line decides the wrapper of the code.
  4. { in PHP is : in Python.
  5. There is no closing } in Python.
  6. Since there is no closing attribute (";"), 2 or more code can’t be placed in a single line.
  7. Python doesn’t need variables starting with $.
The Browser we are going to create is built using GTK 2. The latest version of GTK is GTK 3.
We are going to use GLADE for designing the interface. If you don’t have GLADE installed, install it using the following command :

... [READ MORE]

Loading A Window Created Using GLADE in Python

If you have created a window using Glade and want to display this window on your Python program containing the event handlers of the Window, then you should follow this tutorial.
This small code will load the Glade file and displays the window.
Note : The Glade Project file format should be GtkBuilder.
Now, let’s get down to the code. Create a Python file named window.py in the folder containing the glade file and put in the following contents :

... [READ MORE]

Program to Replace localhost links with website url

If you are working on a project and wants to upload the project to your site, then you have to manually replace all the localhost links with the site url. I created a small Python program that will replace localhost links with your WWW site url.
Here is the Python code :

#!/usr/bin/python
import os
indir = ‘/var/www/mysite‘ #!Folder of your localhost site.
for root, dirs, filenames in os.walk(indir):
 for f in filenames:
  log = open(os.path.join(root, f), ‘r+’)
  f=log.read()  
  n=f.replace("http://localhost", "http://mysite.com")
  log.seek(0)
  log.truncate()
  log.write(n)
  log.close()
print "Successfully Replaced."

... [READ MORE]

Follow/Subscribe

Telegram 

Mastodon  Twitter

GitHub GitLab

Subdomains

Demos  Lab

Past

This blog was once on WordPress. Now a static site. See source code on

GitLab