Delphi Key Value Pairs

We get so used to the data structures in more modern languages that its frustrating using old Delphi 7 but I will show you how you can at least create a list map like structure using TStringList.

 var
  KeyValuePairsSample: TStringList;
  iIndex : integer;
  sname, svalue, svalueindex: string;
begin
  KeyValuePairsSample := TStringList.Create;
  KeyValuePairsSample.Clear;
  KeyValuePairsSample.NameValueSeparator := '=';
    KeyValuePairsSample.Add('ZERO=0'); // name=value pair //
  KeyValuePairsSample.Add('ONE=1');
  KeyValuePairsSample.Add('TWO=2');
  KeyValuePairsSample.Add('THREE=3');
  KeyValuePairsSample.Add('FOUR=4');
  KeyValuePairsSample.Add('FIVE=5');
  Memo1.Clear;
  Memo1.Lines.Add(KeyValuePairsSample.Names[1]);
  Memo1.Lines.Add(KeyValuePairsSample.Values['THREE']);
  Memo1.Lines.Add(KeyValuePairsSample.Names[0]);

  Memo1.Lines.Add(inttostr(KeyValuePairsSample.IndexOfName('THREE')));
 Memo1.Lines.Add(KeyValuePairsSample.ValueFromIndex[KeyValuePairsSample.IndexOfName('THREE')]);
  KeyValuePairsSample.free;
end;


Output:
ONE
3
ZERO
3
3

Best way to start a process in background and logout on Linux

I very often have to start a process on Linux and logout leaving it running. I used to do a nohub.

nohub /path/to/some/pogram &

This does work but seeing the output of it you probably have to pipe the output to file like

nohub /path/to/some/pogram > /path/to/output &

But i will show you a better way SCREEN to the rescue to install:

sudo apt-get install screen

then simply run screen before you run the process and to disconnect the terminal and leave it running do a CRTL-A D which will take you back to the calling shell and you can log out leaving it running. You can do this with multiple processes.

to access it again type

screen -r

There are several suitable screens on:
	6078.your-server-detail	(SOMEDATE)	(Detached)
	4249.your-server-detail	(SOMEDATE)	(Detached)

and to reconnect do

screen -r 4249

This is ideal if you have to start some docker processes on remote servers.

Docker Short Cheat Sheet

To stop all docker containers:

$ docker stop $(docker ps -a -q)

Then to remove all docker containers:

$ docker rm $(docker ps -a -q)

To remove all docker images:

$ docker rmi $(docker images -q)

Stop specific docker container:

docker ps -a
then copy container is ie: c38ba5c03ab7
docker stop c38ba5c03ab7

Run bash shell in docker image:

$ docker exec -it RUNNING_CONTAINER_NAME /bin/bash

Javascript Object to Array and visa versa

const obj = {"1":"5","2":7,"3":9,"4":155,"6":"189","7":587,"8":"455","9":"89","10":"80","12":"887.6","13":89,"14":9}
const objToArr = Object.keys(obj).map(function(key) {
  return [key, Number(obj[key]) ];
});
console.log(objToArr)
/*
[ [ '1', 5 ],
  [ '2', 7 ],
  [ '3', 9 ],
  [ '4', 155 ],
  [ '6', 189 ],
  [ '7', 587 ],
  [ '8', 455 ],
  [ '9', 89 ],
  [ '10', 80 ],
  [ '12', 887.6 ],
  [ '13', 89 ],
  [ '14', 9 ] ]
*/


let resultObj = objToArr.reduce(function(result, item, index, array) {
   Object.assign(result, { [ item [ 0 ] ]:item[1]} )
  return result;
}, {}) //watch out the empty {}, which is passed as "result"
console.log(resultObj)
/*
{ '1': 5,
  '2': 7,
  '3': 9,
  '4': 155,
  '6': 189,
  '7': 587,
  '8': 455,
  '9': 89,
  '10': 80,
  '12': 887.6,
  '13': 89,
  '14': 9 }
*/

resultObj2 = {...objToArr};
console.log(resultObj2)
/*
{ '0': [ '1', 5 ],
  '1': [ '2', 7 ],
  '2': [ '3', 9 ],
  '3': [ '4', 155 ],
  '4': [ '6', 189 ],
  '5': [ '7', 587 ],
  '6': [ '8', 455 ],
  '7': [ '9', 89 ],
  '8': [ '10', 80 ],
  '9': [ '12', 887.6 ],
  '10': [ '13', 89 ],
  '11': [ '14', 9 ] }
*/





Linux File Permissions

Let’s take a look at our ls -l output and look the first column of the listing:

touch myfile 
ls -l myfile
-rw-rw-r-- 1 hano hano 0 Apr 16 08:57 myfile
sudo chown hano.root myfile
ls -l myfile 
-rw-rw-r-- 1 hano root 0 Apr 16 08:57 myfile

The you have 3 access value bits in this case rw- for the owner in this case hano then rw- for the group which corresponds to the root group and then all other users r–.

rRead Access
wWrite Access
xExecute Access

To change the access use chmod (Change Mod)

chmod 754 myfile
ls -l myfile 
-rwxr-xr-- 1 hano root 0 Apr 16 08:57 myfile
ModeDecimal
rwx7
rw-6
r-w5
r–4
-wx3
-w-2
–x1
0

If you know binary you will notice that it is the decimal value of binary digits 001 = 1 010 = 2 100 = 4 so combinations will then be 101 = 4 + 1 = 5 corresponding to r-w

ES6 Arrow functions and correct using of bind(this)

function Desciples(firstname, attributes) {
  this.firstname = firstname;
  this.attributes = attributes;
  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    });
  };
}

let desciple1 = new Desciples("John", [
  "Author of Gospel of John ",
  "Author of Reveleations",
  "Desicple that Jesus loved"
]);

desciple1.listAttributes();

retuns

"undefined did Author of Gospel of John "
"undefined did Author of Reveleations"
"undefined did Desicple that Jesus loved"

But if we change it to

this.listAttributes = function() {
    return this.attributes.map(attrib => {
      console.log(this.firstname + " did " + attrib);
    });


"John did Author of Gospel of John "
"John did Author of Reveleations"
"John did Desicple that Jesus loved"

We can also create local variable to the function and assign this to it

 this.listAttributes = function() {
    self = this;
    return this.attributes.map(function (attrib)  {
      console.log(self.firstname + " did " + attrib);
    });
  };

We can also use the BIND on the function like:

  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    }.bind(this));
  };

Or the MAP has a second paramenter and we can pass this there that will also work

  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    },this);
  };

Build pdo_oci.so (or oci8.so) on SLES 11.4 and Oracle 12.1

I recently had to build pdo_oci.so and I could not find a proper guide to do it. Hopefully this one will help someone.

create a file on document root called test.php

vi /srv/www/htdocs/test.php
<?php
phpinfo();
?>

Browse to http://yourserverip/test.php, on top is the version mine is 5.3.17
Download the correct source http://php.net/releases/ I used this link http://museum.php.net/php5/php-5.3.17.tar.gz
Now copy the downloaded source to ~/php-build

tar -zxvf php-5.3.17.tar.gz
cd php-5.3.17

Oracle Server installed


I have Oracle 12.1 Server installed on the box so I have ensured the my ORACLE_HOME is correct. Read the docs if you want to use the instant client.

./configure  --with-apxs2=/usr/sbin/apxs2 --with-pdo-oci=shared --with-oci8=shared --with-config-file-path=/etc/php5/apache2/php.ini --with-config-file-scan-dir=/etc/php5/apache2  --with-ldap
make 
cd ext/pdo_oci
;edit the config.m4 file
vi config.m4

search for 11
and change

case $PDO_OCI_VERSION in
9.0|10.1|10.2|11.1|11.2)
PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
;;

to
case $PDO_OCI_VERSION in
9.0|10.1|10.2|11.1|11.2|12.1|12.2)
PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
;;

do a pwd make sure you are in ext/pdo_oci directory

phpize
./configure --enable-shared
make

Instant Client

For the instant client you might get error like:
checking Oracle Instant Client library version compatibility… configure: error: Link from /usr/lib/oracle/libclntsh.so to /usr/lib/oracle/libclntsh.so.*

cd /usr/lib/oracle
sudo ln libclntsh.so.12.1 libclntsh.so

Then configure like

./configure  --with-apxs2=/usr/bin/apxs2 --with-pdo-oci=shared,instantclient,/usr/lib/oracle --with-oci8=shared,instantclient,/usr/lib/oracle

To Build the shared object

cd ext/oci8/
phpize
./configure --enable-shared -with-oci8=instantclient,/usr/lib/oracle
make

OR PDO_OCI

cd ext/pdo_oci
phpize
./configure --enable-shared -with-pdo-oci=instantclient,/usr/lib/oracle
make
now there should be a folder modules cd modules ls -l ;should show a pdo_oci.so sudo cp pdo_oci.so /usr/lib64/php5/extensions cd /etc/php5/conf.d/ sudo vi pdo_oci.ini

Add the following to the file :

; comment out next line to disable pdo_oci extension in php
extension=pdo_oci.so

tail -f /var/log/apache2/error.log
if you get
Unable to load dynamic library ‘pdo_oci.so’ (tried: /usr/lib/php/[SOMENUMBER]/pdo_oci.so (libmql1.so: cannot open shared object file: No such file or directory)

Add Oracle libs to the LD_LIBRARY_PATH

cd /etc/ld.so.conf.d
#Create file oracle and add /usr/lib/oracle
sudo vi oracle

and if that does not work then

cd /etc/apache2/
sudo vi envvars 
#and add 
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle
#to the end of the file

Restart the apache server or you can do
php -i | grep pdo
to see if its loaded.

Python update to fix Kodi on linux (Ubuntu and mint)

I had issues with my Kodi 17.6 on linux. I have noticed errors involving cryptography and ffi. Here is the procedure that I followed to fix it.
Open a Terminal and do the following:

 sudo apt-get install build-essential libssl-dev libffi-dev python-dev python-pip

After its done then install cryptography for python

pip install cryptography

It will prompt to install update pip BUT DONT DO THAT IT WILL BREAK THINGS.