Categories
PHP programming

Read .env files with PHP

The following code is written for Yii2 framework, but except for the first line of the function that fetches the path to the root directory of project, it works with other PHP frameworks or without a framework.

function read($param){
    $path = Yii::getAlias('@app'."/../");
    $envData = [];
    if (!is_readable($path . "/.env" )) {
        //
    } else {
        $lines = file($path."/.env", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        
        foreach ($lines as $key => $value) {
            $explode = explode("=", $value);
            if(array_key_exists(1, $explode)){
                $envData[$explode[0]] = $explode[1];
            }
        }
        
    }

    

    if(array_key_exists($param, $envData)){
        return $envData[$param];
    } else {
        return NULL;
    }
}
Categories
programming

Fetch Information About A Stock From Google Finance And Show It In Google Sheets

Google Sheets offers a Google Finance function to fetch real time data about a stock you are watching.

The syntax is as follows.

=GOOGLEFINANCE(ticker, [attribute], [start_date], [end_date|num_days], [interval])

Ticker is where you specify the Scrip ID of the stock , be it IRCTC, JSL, TATAMOTORS etc. You should use both the exchange symbol and scrip id seperated by a colon to avoid ambiquity. For example, if you are fetching data of IRCTC stock from NSE, ticker parameter should be NSE:IRCTC

Attribute is an optional parameter. This is where you specify the type of information you need about the selected stock. If you leave it blank, it will return the price by default. As this is a string, you need to enclose the value in quotes.

Supported attributes include

  • price – The current market price (CMP) of the stock
  • pe – The price to earnings ratio
  • eps – Earnings per share
  • high – Highest price on current date
  • low – Lowest price on current date
  • high52 – Highest price in 52 weeks
  • low52 – Lowest price in 52 weeks

More attributes can be found here.

Start_date is also optional. The start date when you are fetching historical data.

End_date|num_days is another optional parameter. You can either specify an end_date or number of days from start_date when you are fetching historical data.

Interval, The final parameter is also optional and is used to specify the frequency of returned data. The supported values are DAILY and WEEKLY.

Example 1: Fetch current market price of IRCTC (NSE)

=GOOGLEFINANCE( "NSE:IRCTC", "price")

Example 2: Fetch Price to Earnings (P/E) ratio of IRCTC (NSE)

=GOOGLEFINANCE( "NSE:IRCTC", "pe")

To fetch the Scrip ID from a cell , so that you can apply the same function to other rows as well, as seen in the screenshot, change the function to

=GOOGLEFINANCE( TEXTJOIN("", TRUE, "NSE:",A2), "price")

where A2 is the cell that holds the scrib ID.

Categories
programming

Compile Nodejs Code Using Bytenode

Bytenode is a library you can use to compile your Nodejs source code into V8 Bytecode. The steps are fairly simple.

Step 1: Install Bytenode globally

sudo npm install bytenode -g

Step 2: compile your js file

bytenode --compile app.js

It’ll produce an output file app.jsc

Step 3: Install bytenode inside your project

npm install bytenode --save

Step 4: create a launcher file launcher.js and require bytenode inside it.

require('bytenode')

Step 5: Require the app.jsc file inside launcher.js

require('./app.jsc');

That’s it, now run launcher.js and it will work just like before. Deploy it wherever you want without worrying about the source code.

Categories
Linux

Batch rename files in Nautilus

Nautilus offers an easy way to batch rename files. I used to do this using some bash script, until I’ve recently discovered this. It’s pretty straightforward.

Go to the folder which contains the files you want to rename, and select  those files. Then right click and select rename, as you would do for a single file.

‘Rename using a template’ is the default option. Add an optional prefix, and then click Add button next to the text box. Select a pattern here, for example 1,2,3,4.

Now click rename. Your files are now renamed to n1, n2, etc.

Now, in this example, the extensions are ‘JPG’. I want to change it to ‘jpg’ for all the files. For this, use the ‘Find and replace  text’ option.