## Wednesday, March 24, 2021

### Looping over an iterable (array/list) in JavaScript Vs Python

Lets see what it takes to loop over an iterable using for-loop in both JavaScript Vs Python. By the way, an iterable is an object capable of returning its members one at a time, permitting it to be iterated over in a for-loop.

Assuming we have this iterable : m = [3.23, 4.56, 5.3, 2.44, 6.7, 12.4, 566] and we want to perform some math operation on each element (in this case: 2 to the power of element divided by 2).

The math formulae is as follow:-

For JavaScript

`Math.pow(2, element) / 2`

For Python

`(2**element)/2`

The solutions

JavaScript Solution

```m = [3.23, 4.56, 5.3, 2.44, 6.7, 12.4, 566]

for (let i=0; i<m.length; ++i){
console.log(Math.pow(2, m[i]) / 2);
}```

```m = [3.23, 4.56, 5.3, 2.44, 6.7, 12.4, 566]

for (let i in m){
console.log(Math.pow(2, m[i]) / 2);
}
```

Python Solution

```m = [3.23, 4.56, 5.3, 2.44, 6.7, 12.4, 566]

for i in m:
print((2**i)/2)```

That is it!

## Thursday, March 18, 2021

### Python script - Merge PDF files into a single file

This script make use of the PyPDF2 library to merge list of pdf files into one big file.

```from PyPDF2 import PdfFileMerger
from os import listdir

input_dir = r"C:\Users\Yusuf_08039508010\ND 2 WAEC Result Check\Result"

merge_list = []

for x in listdir(input_dir):
if not x.endswith('.pdf'):
continue
merge_list.append(input_dir +'\\'+ x)

merger = PdfFileMerger()

for pdf in merge_list:
merger.append(pdf)

merger.write(input_dir + "\pdf_file_name.pdf") #your output directory and pdf_file name
merger.close()

print('Finished...')```

Enjoy!

## Tuesday, March 9, 2021

### Spread column-wise data to row-wise

On the web, it is very common to find dataset displayed in column-wise manner as seen below.

As you will already noticed, each new record is separated by the company name in bold capital letters. So, lets add a sign to separate one record from the other. I used this "------------------------" sign, but you can use anything as long as it is unique and not part of the records itself.

So, the working data copied from the website is like this:-

```ACCESS CREDIT MANAGEMENT, INC.
Tim Cullen, Attorney
11225 Huron Ln Ste 222
Little Rock, AR 72211-1861
United States
Phone: (501) 664-2922
Fax: (501) 664-3207
MAP Attorney
------------------------
CREDIT CONTROL CO., INC.
Bill Caldwell, President
Bill Caldwell, Ethics Contact
10201 W Markham St Ste 104
Little Rock, AR 72205-2180
United States
Phone: (501) 225-2050
Fax: (501) 225-2135
ACA Member since 1982
Line of Business: Third Party Collections
------------------------
THE MCHUGHES LAW FIRM, PLLC
Becky A. McHughes Esq., Attorney at Law
10810 Executive Center Dr
Danville Bldg Ste 312
Little Rock, AR 72211
United States
Phone: (501) 376-9131
Fax: (501) 374-9332
http://www.mchugheslaw.com
ACA Member since 2013
Line of Business: Third Party Collections
------------------------
THE MCHUGHES LAW FIRM, PLLC
Becky A. McHughes Esq., Attorney at Law
10809 Executive Center Dr
Danville Bldg Ste 312
Little Rock, AR 72204
United States
Phone: (501) 376-9131
Fax: (501) 374-9332
MAP Attorney
Lowell
------------------------
CENTRAL RESEARCH, INC.
Karena Holt, Vice President of Operations
Karena Holt, Ethics Contact
122 N. Bloominton Ste 1
Lowell, AR 72745
United States
Phone: (479) 419-5456
Fax: (479) 419-5460
http://www.central-research.com
ACA Member since 2016
Line of Business: Third Party Collections
------------------------
CENTRAL RESEARCH, INC.
Shane Taylor
106 N Bloomington
Ste S
Lowell, AR 72745-8988
United States
Phone: (479) 419-5456
MAP Attorney
Mabelvale
------------------------
FIRST COLLECTION SERVICES
Chris Dunkum, President
Chris Dunkum, Ethics Contact
10925 Otter Creek East Blvd
Mabelvale, AR 72103-1661
United States
Phone: (501) 455-1658
http://www.FCScollects.com
ACA Member since 1983
Line of Business: Outsourced First Party or Billing Company
Line of Business: Third Party Collections```

What we really want is something like this:-

```ACCESS CREDIT MANAGEMENT, INC. :: Tim Cullen, Attorney :: 11225 Huron Ln Ste 222 :: Little Rock, AR 72211-1861 :: United States :: Phone: (501) 664-2922 :: Fax: (501) 664-3207 :: MAP Attorney
------------------------
CREDIT CONTROL CO., INC. :: Bill Caldwell, President :: Bill Caldwell, Ethics Contact :: 10201 W Markham St Ste 104 :: Little Rock, AR 72205-2180 :: United States :: Phone: (501) 225-2050 :: Fax: (501) 225-2135 :: ACA Member since 1982 :: Line of Business: Third Party Collections
------------------------
THE MCHUGHES LAW FIRM, PLLC
Becky A. McHughes Esq., Attorney at Law :: 10810 Executive Center Dr :: Danville Bldg Ste 312 :: Little Rock, AR 72211 :: United States :: Phone: (501) 376-9131 :: Fax: (501) 374-9332 :: http://www.mchugheslaw.com :: ACA Member since 2013 :: Line of Business: Law Firm :: Line of Business: Third Party Collections
------------------------
THE MCHUGHES LAW FIRM, PLLC :: Becky A. McHughes Esq., Attorney at Law :: 10809 Executive Center Dr :: Danville Bldg Ste 312 :: Little Rock, AR 72204 :: United States :: Phone: (501) 376-9131 :: Fax: (501) 374-9332 :: MAP Attorney :: Lowell
------------------------
CENTRAL RESEARCH, INC. :: Karena Holt, Vice President of Operations :: Karena Holt, Ethics Contact :: 122 N. Bloominton Ste 1 :: Lowell, AR 72745 :: United States :: Phone: (479) 419-5456 :: Fax: (479) 419-5460 :: http://www.central-research.com :: ACA Member since 2016 :: Line of Business: Third Party Collections
```
From vertical arrangement to horizontal arrangement. The horizontal (row-wise) arrangement, works best in spreadsheet. We will have common column for the same records.

What we have (vertical/column-wise arrangement)

What we want (horizontal/row-wise arrangement)

## Monday, March 8, 2021

### RegexOne.com alternative solution

RegexOne.com has an interactive lessons for Regular Expression and in this post, I want to solve all the lessons with a solution different from the one the provided.

For example: \w matches any word character (equal to [a-zA-Z0-9_]), so if the solution on RegexOne.com is \w then I have to look for another way like [a-zA-Z0-9_] to solve to lesson.

Lets get started...

Exercise 1: Matching Characters

Exercise 1½: Matching Digits

Exercise 2: Matching With Wildcards

Exercise 3: Matching Characters

Exercise 4: Excluding Characters

Exercise 5: Matching Character Ranges

## Tuesday, March 2, 2021

### PyQGIS - Add multiple shapefile vector layers to the QGIS project instance

Sometimes, I need to load many shpafiles which are located in various folders into the QGIS project. A handy way to overcome this repetitive boring task is to use the PyQGIS script below.

```import glob

# Use glob to recursively search all folders for .shp files...
shp_files = glob.glob(r'C:\Users\Yusuf_08039508010\Desktop\GIS Data\NGR\**\*.shp', recursive=True)
# print(shp_files)

layer_count = 0
for shp in shp_files:
layer_name = shp.split('\\')[-1].split('.')[0]
vlayer = QgsVectorLayer(shp, layer_name, "ogr")

if not vlayer.isValid():
else:
layer_count += 1