Wednesday 20 March 2019

Migrating your wordpress from localhost to server or from one domain to other domain


Migrating your wordpress from localhost to server

To move the wordpress site from one host to the other host or server, you can directly make the changes in the MySQL database itself.
Login to your phpmyadmin or MySQL command line and use the following commands on your wordpress database:

1. 
  UPDATE wp_options
     SET option_value = 'http://new_domain_name.com'
     WHERE option_name = 'home';
2.
 UPDATE wp_options
    SET option_value = 'http://new_domain_name.com'
    WHERE option_name = 'siteurl';
3.
UPDATE wp_posts
    SET post_content = REPLACE(post_content,
    'http://old_domain_name.com', 'http://new_domain_name.com');
4.
UPDATE wp_posts
    SET guid = REPLACE(guid,'http://old_domain_name.com', 
     'http://new_domain_name.com');


To make sure there are no broken links, reset your permalinks by following the below steps:
  1. Log in to your WordPress admin panel.
  2. Click Settings.
  3. From the Settings menu, click Permalinks.
  4. Note which kind of Permalink you currently use.(for custom permalink, copy the current permalink structure and save it in a notepad for later use)
  5. Select any Permalink setting other than the one you currently use, and then click Save Changes.
  6. Select your original Permalink setting(paste the above copied permalink structure in custom permalink), and then click Save Changes.

Monday 18 March 2019

Finding last element in array - Check if current element is last element in Python arrays

When we are using loops in Python we might fumble onto a situation where we have to check if we reached the end of the array or simply say that we are iterating through each element in array and checking if the current element exists in a hash/dictionary/database and upon a match we still want to continue until the end of the array is reached.

So there are multiple ways of achieving this but I am going to give a simple solution that is easier to understand.

for suffix in suffixes:
        if(re.search(suffix+'$', my_string)):
            #do something
        elif(suffix == suffixes[-1]):
            print("reached end of array")

So the line that checks for the last element of array is

        elif(suffix == suffixes[-1]):

Similarily, to access for last but second element use suffixes[-2] and so on.

Hope its useful.

Thursday 14 March 2019

python capture regex groups in variables



#python script to capture regex matched groups into variables
import re

suffix = 'en'
word = 'children'

#print(word,suffix)

m = re.search(r'(.*)'+suffix + '$', word)

print(m.group(1))

Monday 11 March 2019

Split multi-paged PDF file in to individual PDF files

You might come across a situation where you have a PDF file with multiple pages and you'd like to split each page in to each PDF file. Sound pretty tedious task right?

It's just as simple as executing a single command. All you gotta do is, make sure you have pdftk (PDF Tool Kit) installed on your Linux machine.

If you do not have the pdftk installed, follow the below steps:

For(Ubuntu/Debian)


sudo apt update

sudo apt install pdftk 

 For(CentOS/Fedora), follow the instructions in the link below to install it:

https://www.pdflabs.com/docs/install-pdftk-on-redhat-or-centos/

Once installed, use the below command to split a single multi-paged PDF file to individual PDF files with each page

$pdftk your_multi-page_file.pdf burst

This will split each page from multi-page PDF file to each individual PDF file.


Similarly, to merge few selected PDF files in to one PDF file, use the following command:

$pdftk file1.pdf file2.pdf cat output mergedfile.pdf

If you want to merge all the PDF files in a specified directory, you may use the wild card character(*) as follows:

$pdftk *.pdf cat output mergedfile.pdf

'*' implies all the files with .pdf extension

You may find few more tips on dealing with PDF files here

Friday 8 March 2019

What is a hash or a dictionary in Python? Understanding hashes.

Hash tables or Dictionaries(as referred to in Python) are associative arrays. From Wikipedia associative arrays are defined as a collection of key, value pairs such that each key appears at most once in the hash table.

Question: Why can't we use arrays?

Answer: Because, when we use arrays its difficult to find an element in the array since the searching will loop through all the elements in the array until the element is found. This will compromise efficiency if the array is large in size. This problem is solved in hash table as elements can be accessed quickly without looping through the entire array.

Okay lets dig deeper...

I am going to explain this one with an example that we see daily. In this example we are going to store all members of a family relations and their names in a hash.

So the hash name is family_dict = {}

I am listing out all the elements I am going to store in it. It is going to contain wife, son, daughter, friend, father, mother....

All these relations have a name that we can call with. Now to build a hash we need keys and values. Identifying keys and values is the important thing because ultimately it will satisfy our need to use hash data structure.

In our hash we are going to store relations and their names. Before that one thing we all need to keep in mind while we build  a hash is that keys should be unique and values we will not care about them until there is really a need.

So our family hash needs unique things as keys. Names can't be unique as many people can be named with same name. This is as simple as that. Therefore, our hash is going to contain relations as keys and names as its corresponding values.

Now there will be a question what if our relations can also be same like when we have many brothers/sisters. Simply we are going to manipulate our keys are brother1, sister2 to make them unique. Enough of theory now and we will start our implementation.

family_dict = {
  "me": "Mr.x",
  "father": "Mr.y",
  "mother": "Mrs.z",
  "son":"kid1",
  "daughter1":"d1"
  "daughter2":"d2",
  "wife":"w1"
}

This seems simple. Each element mapped to its corresponding value. Now think of a situation where we might need to point same key to many values. For it lets assume Mr.x is a bit cheeky and has another wife w2. One just can't put two wives in same house. So in computers what happens is when you add same key with different value like "wife":"w2".  Our hash would store only one key that would be the one added last. That is previous keys are forgotten or overridden when same keys are added.

To solve this issue MR.x would compromise with his family and come to an agreement to put two of them in the same hut. But how do we do it here? No delay just scroll down.

family_dict = {
  "me": "Mr.x",
  "father": "Mr.y",
  "mother": "Mrs.z",
  "son":"kid1",
  "daughter1":"d1"
  "daughter2":"d2",
  "wife":"w1, w2"
}

If you observe clearly we just updated our hash key such that its value holds the previous one too. So in real time programming we should always check for existence of a key in hash if it already exists so as to make sure all our values being taken care of and none are overridden because of duplicate entry.

And this is how is is done. Now think of a hash which is going to store how many times a word occurs in the given text. For this we will store each word as a hash and its count as value. So everytime a word is revisited we will check for the value of the word and increment it by one.

    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1

And finally we will print our hash:

#print the dictionary with sorted keys(words) and count as values
for k in sorted(wordcount):
    print (k, wordcount[k])

This has been a bit long post but thanks for coming here.
Bonus: Finding word frequency using Python

Wednesday 6 March 2019

How to open horizontal and vertical split mode to work with multiple files in VIM

Horizontal split mode

vim -o file1.txt file2.txt

Vertical split mode

vim -O file1.txt file2.txt

Now to switch between these files use Ctrl + W + W to toggle between these files.

By using these commands we can open n number of files.

PDF Splitter using PyPDF2 module of Python - Split PDF into multiple pages

Often when working with a large PDF we fumble upon of a need where we need each page of the PDF in one separate  PDF file.

So in this article we are exactly going to do this but not using any Linux command but using Python.

For that lets get our dependencies get installed. Just run the below command and you are all ready.

pip3 install pypdf2

Now open a editor and save the following code in to your editor.

from PyPDF2 import PdfFileWriter, PdfFileReader

inputpdf = PdfFileReader(open("largefile.pdf", "rb"))

for i in range(inputpdf.numPages):
    output = PdfFileWriter()
    output.addPage(inputpdf.getPage(i))
    with open("largefile-page%s.pdf" % i, "wb") as outputStream:
        output.write(outputStream)

Thanks to StackOverflow for this part. Thats it now you are ready to split a PDF file into multiple files with each file containing one page.