Python3 with MySQL database

If you don't already have MySQL installed, install it with the following commands (mac only):

$ brew info mysql
$ brew install mysql
$ brew tap homebrew/services

Run the following commands to start or stop mysql. Start MySQL before proceeding:

$ brew services stop mysql
$ brew services start mysql

To see available sevices:

$ brew services list

To check MySQL version:

$ mysql -V

Set root password

Python3 with Oracle database

Installation

These instructions are for Mac OS:

First install homebrew. Never use sudo for brew.

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ which brew
/usr/local/bin/brew

Install Python3. The installation comes with pip3.

$ brew install python3
$ which pip3
/usr/local/bin/pip3

Install cx_Oracle, the library that allows python3 to communicate with Oracle database.

File Management

Node uses the fs library to provide rich and powerful file management functionality. First let's a directory and some files:

$ mkdir data; cd data
$ touch provinces.txt capitals.txt

Add the following text to provinces.txt

Alberta
British Columbia
Manitoba
New Brunswick
Newfoundland and Labrador
Nova Scotia
Ontario
Prince Edward Island
Quebec
Saskatchewan

Add the following to capitals.txt

Child Process

The child_process module allows you to communicate with other applications on your system by executing external processes in your environment. There are four kinds of child processes; exec(), spawn(), execFile(), fork(). I will be covering exec() and spawn() here. If a child process will execute once, use exec() function. For example the "cal" command on terminal prints the current month's calendar in the terminal. The command is executed once, prints a result and that's it. If a child process would be running for a long time and printing output several times, use spawn() function.

Events

Node.js is an event-driven system. When you are performing different tasks, different events are emitted. You need to understand those events and how to use them. Events are defined by different modules. For example process.on() is an event. You can also create and use your own events. See example:

var events = require('events');
var eventEmitter = new events.EventEmitter();

eventEmitter.on('bankrupt', function() {
    console.log('You are bankrupt');
});

var networth = -20000;
if (networth < 0) {
    eventEmitter.emit('bankrupt');
}

output:

Built-in Modules

A module encapsulates relate code, such as code for handling files, into a single unit of code. Node ships with many useful modules. These are called built-in modules. Programmers can create new modules. These are called custom modules.

Following is a list of some built-in modules:

Create server

The following code creates a server and assign a port.

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Your node server is running');
}).listen(8282);

Simply run the code and open http://localhost:8282 in your browser.

Streams

We have already seen how to read stdin (input from the terminal) as follows:

console.log(process.stdin);

A program can take input from many different sources of streaming data. Readline module provides an interface to read data from any Readable stream including process.stdin. See https://nodejs.org/api/readline.html. Following is a simple example, I picked up from Readline documentation.

Process Object

The process object is available globally meaning that it can be accessed from anywhere in the code. It allows us to fetch information regarding the current process instance; information such as environmental variables, path, stdin, and stdout.

console.log(process.argv);

Save this code as processdemo.js. To run:

$ node processdemo.js

This code outputs the parameters used when the process starts. It's output is as follows:

Pages