2019 Reading List

Continuing last year’s tradition, here’s an evolving list of books that I am reading in 2019.

Currently Reading

  • How Not To Be Wrong, Jordan Ellenberg

Finished Reading

  • Beat The Bank, Larry Bates
  • The Big Picture, Sean Carroll
  • The Book of Why, Judea Pearl
  • In a Dark, Dark Wood, Ruth Ware
  • The Fault In Our Stars, John Green
  • The Rosie Project, Graeme Simsion
  • Thieves of Bay Street, Bruce Livesey
  • A Noise Downstairs, Linwood Barclay
  • For the Love of Physics, Walter Lewin
  • Economics in One Lesson, Henry Hazlitt
  • The Wealthy Barber Returns, David Chilton
  • The Old Man and the Sea, Ernest Hemingway
  • The Girl with the Dragon Tattoo, Stieg Larsson
  • Fear: Trump in the White House, Bob Woodward
  • The Knowledge Illusion, Steven Sloman, Philip Fernbach
  • Fire and Fury: Inside the Trump White House, Michael Wolff
  • If I Understood You, Would I Have This Look on My Face, Alan Alda
  • Naked Statistics: Stripping the Dread from the DataCharles Wheelan
  • Why We Sleep: Unlocking the Power of Sleep and Dreams, Matthew Walker
  • Game Changers: Stories of the Revolutionary Minds Behind Game Theory, Rudolf Taschner
  • The 3 Simple Rules of Investing, Michael Edesess, Kwok L. Tsui, Carol Fabbri, George Peacock
  • Chancing It: The Laws of Chance and How They Can Work For You, Robert Matthews, Larry Gonick
  • Millionaire Teacher: The Nine Rules of Wealth You Should Have Learned in School, Andrew Hallam

Feel free to drop your own recommendations in the comments!

I’m not saying that you have to be a reader to save your soul in the modern world. I’m saying it helps.

Walter Mosley

2018 Reading List

One of my personal resolutions in 2018 was to read at least one book each month. To track my progress, I started an evolving list of books  that I was reading or had finished reading.

With 2018 wrapped up, here’s the final list:

  • The Girl On The Train, Paula Hawkins
  • Barking Up The Wrong Tree, Ric Barker
  • A Thousand Splendid Suns, Khaled Hosseini
  • Seven Brief Lessons on Physics, Carlo Rovelli
  • Outliers: The Story of Success, Malcolm Gladwell
  • Mr. Penumbra’s 24-Hour Bookstore, Robert Sloan
  • 21 Lessons for the 21st Century, Yuval Noah Harari
  • Brief Answers to the Big Questions, Stephen Hawking
  • Mindset: The New Psychology of Success, Carol S. Dweck, Ph.D.
  • What Do You Care What Other People Think?, Richard P. Feynman
  • How To Lie with Statistics, Darrell Huff (author), Irving Geis (illustrator)
  • Naked Statistics: Stripping the Dread from the Data, Charles Wheelan
  • Bad Blood: Secrets and Lies in a Silicon Valley Startup, John Carreyrou
  • Superforecasting: The Art and Science of Prediction, Philip E. Tetlock, Dan Gardner

The only unfinished book from 2018, The Capital Markets: Evolution of the Financial Ecosystem by Gary Strumeyer and Sarah Swammy, Ph.D., will go onto next year’s list. Honestly, I expect to keep referencing this book for a long time to build domain knowledge rather than reading just once. Maybe, textbooks and reference books should also get a special mention on the future reading lists!

That said, with 2018 coming to a wrap, I want to say that I thoroughly enjoyed and benefited greatly from reading this assortment of books. Happy new year to all of you. I already have a few good books lined up for the next year but your recommendations are welcome

Stay tuned for the 2019 reading list!

CAS Registry Number Validation using PL SQL

CAS Registry Numbers are universally used to provide a unique, unmistakable identifier for chemical substances. CAS stands for Chemical Abstracts Service, a division of the American Chemical Society, that assigns a unique numerical identifier to every chemical substance described in the open scientific literature.

The CAS Registry database contains more than 113 million unique organic and inorganic chemical substances, such as alloys, coordination compounds, minerals, mixtures, polymers and salts, and more than 66 million sequences. CAS Registry contains substances reported in the literature back to the early 1800s and over 15,000 substances are added daily. [1]

A CAS Registry Number itself has no inherent chemical significance but provides an unambiguous way to identify a chemical substance or molecular structure when there are many possible systematic, generic, proprietary or trivial names. [2] These numbers are assigned in a sequential, increasing order when the substance is identified by CAS scientists for inclusion in the CAS REGISTRY database,

A CAS number is separated by hyphens into three parts, the first consisting from two up to seven digits, the second consisting of two digits, and the third consisting of a single digit serving as a check digit. The check digit is calculated by taking the sum of last digit times 1, the previous digit times 2 and the still previous digit times 3 and so on, and finally computing the remainder upon dividing the sum by 10.

For example, the CAS number of water is 7732-18-5: the checksum 5 is calculated as (8×1 + 1×2 + 2×3 + 3×4 + 7×5 + 7×6) = 105; 105 mod 10 = 5.

I wrote a PL SQL function to validate CAS Registry Numbers for use in one of the database applications for a semiconductors company for managing the chemicals used in their chip manufacturing facilities.

The function f_is_valid_cas_number takes a CAS number as input and returns 1 if it is valid and 0 otherwise. The following examples show how the function can be used:

-- Valid CAS Number for Water, returns 1
SELECT f_is_valid_cas_number('7732-18-5') FROM dual;

-- Invalid syntax, returns 0
SELECT f_is_valid_cas_number('7A2-181-522') FROM dual;

-- Valid syntax but incorrect checksum, returns 0
SELECT f_is_valid_cas_number('1-11-5') FROM dual;

In an Oracle PL SQL program, the function can be used to set a flag as follows:

-- Valid CAS Number for Water, sets is_valid_cas_number flag to 1
is_valid_cas_number NUMBER(1) := f_is_valid_cas_number('7732-18-5');

The code for the function is available in the CAS Number Validator repository on Github. I hope you find it useful.

Subsetting a list with named components in R using dollar notation

The more I work with R, the more it amazes me. This particular post is about accessing a named component of a list using the component name with the dollar notation

> name$component_name

So if you have a list named assets with two named components, rate_depreciation and rate_exchange, then you can select them by using assets$rate_depreciation and assets$rate_exchange respectively.

What is amazing, is that the component names can also be selected by using the first few letters of the component name – with the minimum number of letters needed to identify them uniquely. So, rate_depreciation component can be selected as:

> assets$rate_d

Similarly, rate_exchange component can be selected as:

> assets$rate_e

This partial-match behavior results from the exact attribute being implicitly set to FALSE in R. If you want R to select only those components that match exactly with the specified component name, you can explicitly set exact=TRUE to alter this behavior:

> name[[component_name, exact=TRUE]]

Now, if you searched for rate_exchange with a shortened name as the selector and exact match flag set, you would get a NULL instead of contents of your named component.

> assets[[rate_e, exact=TRUE]]

Interesting and even nifty, maybe, when you are in a hurry. But, handle with care!

ORA-03113: end-of-file on communication channel

One of the lesser used features in an application using Oracle Database installed on a UNIX server stopped working recently throwing the ORA-03113 error. The oerr utility provided the following information:

$ oerr ora 3113

ORA-03113: end-of-file on communication channel

Cause: The connection between Client and Server process was broken.

Action: There was a communication error that requires further investigation.

This meant that one of these most probable things had happened:

  • The network connection was lost
  • The server process had crashed

(Note that ORA-03113 is a generic error and there are over 1000 bugs related to it on Oracle Support!)

First possibility was quickly eliminated as the error wasn’t encountered just once or twice which might happen due to that rare, momentary glitch in network connectivity. But it did happen every single time that particular feature was used.

This meant that something in the function invoked, when the feature in question was run, was causing the server process to die. Running through the code using a test script led to the point of failure.

A simple dynamic SQL query was being executed without event.

-- The following query is being used multiple times later on
v_sqlstr := 'SELECT column1, column2, SUM( column_qty ) AS proposed_qty
             FROM a_global_temporary_table	-- or any table
             WHERE column1 = ' || column1_value ||
             ' GROUP BY column1, column2' ||
             ' ORDER BY MOD( SUM( column_qty ) )';

-- Count the number of rows returned by the "ordered" query above
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM (' || v_sqlstr || ')' INTO v_count;

But why?

Playing around with bits and pieces of the query stored in v_sqlstr led to the finding that when trying to get COUNT(*) using v_sqlstr as the source, including the MOD() in the ORDER BY clause of the query was causing the server process to crash. Weird!

To fix the issue, and because you don’t need an ordered result set to take a count, I removed the ORDER BY clause from the original query and appended it back only after taking the count, as shown below:

-- The following query is being used multiple times
v_sqlstr := 'SELECT column1, column2, SUM( column_qty ) AS proposed_qty
             FROM a_global_temporary_table	-- or any table
             WHERE column1 = ' || column1_value ||
             ' GROUP BY column1, column2';

-- Count the number of rows returned by the query above
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM (' || v_sqlstr || ')' INTO v_count;

-- Append the ORDER BY clause to the original query
v_sqlstr := v_sqlstr || ' ORDER BY MOD( SUM( column_qty ) )';

And things were back on track!

This is just one of the many possible causes and fixes for this error. I hope you find it useful in case you get stuck in a similar situation. Cheers!

Custom post type archives with WordPress Cuztom Helper

WordPress Cuztom Helper now enables custom post type archives out of the box.  In the same way that posts are shown on their own archive with archive.php, custom post types will use archive-{post_type}.php .

I decided to add this feature because one, generally, wants to be able to view all the posts of a custom post type. So if you have a custom post type named ‘Acme Products’, you can view all the products of this type by visiting example.com/acme-products/ in the browser.

In WordPress, custom post type archive pages are not enabled by default when you register a post type. If we left it like this in WordPress Cuztom Helper, one would either have to create a page template manually

/* Template Name: Custom Post Type Archive */

//somewhere in the template file 
global $query_string; 
query_posts($query_string . "post_type=YOUR-CUSTOM-POST-TYPE&post_status=publish&posts_per_page=10"); 
if ( have_posts() ) : while ( have_posts() ) : the_post();

and then create a blank page, choose the template and publish it, which is a lot of unnecessary work just to view posts of a custom post type. Or one would set

'has_archive' => true

while registering a custom post type. When has_archive is set to true, the custom post type slug is used for the archive page by default, which is a little unintuitive because acme_products/ makes more sense than acme_product/ when you want to view more than one product of Acme Product post type (it’s an archive page and not a single page).

There is an additional benefit to this i.e. saving the trouble of having to deal with slugs manually. Instead of trying to figure out slug_to_view_all_acme_products, you simply register your Acme Product post type and this will be done for you. In exceptional cases, such as for a custom post type named Music or Country Music where it makes sense to use the post type slug as archive slug, you simply set

'has_archive' => true

and your archive will be accessible at example.com/music/ or example.com/country_music

And when you want to disable the archive, simply set

'has_archive' => false

One just doesn’t have to deal with slugs unless one wants to use something drastically different. Another advantage of using archive over writing a Page Template is that one doesn’t have to create an archive-{post_type}.php page unless one is using custom taxonomies or metaboxes. Simply, the existing archive.php file be used. One also doesn’t have to use query_posts unless absolutely required.

So, from now on, custom post type archives are turned on when registering Custom Post Types with WordPress Cuztom Helper, as the default behavior. There you go. Download WordPress-Cuztom-Helper and take your custom post types to the next level with archives.

Download online course videos from Stanford Center for Professional Development

Stanford Center for Professional Development

It is particularly difficult to watch online videos on SCPD using a ‘mystanfordconnection’ account when you want to watch them at a faster speed, or when you want to watch them offline.

To address these issues, I have written a Python tool that lets you download all the videos from the Stanford courses that you’re enrolled in, while they are still available to you. All the videos can be downloaded in parallel.

Get download-scpd-videos. The instructions are for Mac OS X. For Linux or Windows, simply install the dependencies (mechanize, BeautifulSoup and mimms) using your package manager of choice, packaged binaries or building them from source.

Note: Only for educational purposes. Use with explicit permission from Stanford Connection for Professional Development and for personal and non-commercial purposes only. Use this script with good judgment, common sense, discretion, and assume full responsibility for misuse. I accept NO responsibility for your use of this tool.

Install CakePHP in a subdirectory of WordPress

Let’s say you have a WordPress installation which is accessible in the browser at example.com and you want to install CakePHP in a sub-directory (or a directory inside a sub-directory) within WordPress so that it is accessible at something like example.com/cake/ or example.com/project/cake/ in the browser.

To achieve this, you need to modify the .htaccess files used by WordPress as well as CakePHP. I’ll explain how to modify .htaccess files in the following steps.

Step 1: Modifying .htaccess file in WordPress

In the directory where WordPress is installed ( in the same folder as wp-config.php file ), edit the .htaccess file and add the following line, assuming that your CakePHP subdirectory will be called ‘cake’

RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA]

so that your .htaccess file looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA]
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
# END WordPress

If your ‘cake’ is going to be inside another sub-directory of WordPress installation called ‘project’, then this line should look like:

RewriteRule ^cake/(.*)$ /project/cake/$1 [L,QSA]

If you don’t do this step, visiting example.com/cake/ or example.com/project/cake/ will try to open as a WordPress page which, obviously, isn’t right and will fail.

Step 2: Modifying .htaccess files in CakePHP

After completing Step 1, you will need to add the following RewriteBase statement in the .htaccess files used by CakePHP. If you don’t complete this step, your CakePHP app will show up but fail to load CSS / JS or any other assets in /app/webroot.

RewriteBase /cake

So, first of all, navigate to /cake/ or /project/cake/ , whichever is your CakePHP folder and edit the .htaccess so that it looks like this:

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /cake
 RewriteRule ^$ app/webroot/ [L]
 RewriteRule (.*) app/webroot/$1 [L]

Then navigate to the ‘app’ folder inside ‘cake’ and edit the .htaccess file so that it looks like this:

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /cake
 RewriteRule ^$ webroot/ [L]
 RewriteRule (.*) webroot/$1 [L]

Finally, navigate to webroot folder inside this ‘app’ folder so and edit the .htaccess file so that it looks like this:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /cake
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ index.php [QSA,L]

That’s it. You’re good to go.

Visiting example.com in the browser will open your WordPress website, while example.com/cake/ or example.com/project/cake/ , depending on how you configured it, will open your CakePHP app.

Fix HTTP 500 Internal Server Error on renaming CakePHP root directory

If you have to rename your CakePHP root folder for some reason, you might come across HTTP 500 Internal Server Error. This generally happens because of PHP opcode caching done by PHP accelerators for performance boosts.

The problem can usually be fixed by restarting your server. If not, you might have to clear the PHP opcode cache manually.

Clearing PHP opcode cache in XCache
Clearing PHP opcode cache in XCache

If you’re using MAMP (or MAMP Pro) on a Mac, which comes bundled with XCache, you can easily fix it by visiting XCache tab from server start page and using the clear button against the PHP cache. If you have a different PHP dev setup, check for other PHP accelerators like APC or eAccelerator.

This will fix your problem and your CakePHP app will continue to function normally after renaming the CakePHP root directory.

Bug fix for undefined variable notice in CPT-onomies 1.1.1

CPT-onomies WordPress Plugin by Rachel Carden enables you to use your custom post types as taxonomies and create relationships between your posts by using custom post type post titles as taxonomy terms, in a similar fashion as you would create taxonomy relationships.

Since it is not yet possible to create relationships between custom post types like that with WordPress, out of the box, I am using CPT-onomies v1.1.1 for a project that I’m presently working on.

While playing around, I stumbled across a bug which introduced a debug notice in the WordPress dashboard and didn’t let me publish or update posts for those custom post types which used a cpt-onomy (custom post type as a taxonomy). I reported it to Rachel and she responded quickly with a fix.

CPT-onomies v1.1.1 bug

Until a new version of the plugin comes out with the fix, to get rid of the debug ‘Notice’, you can change line 931 of manager.php in the plugin root directory from:

'cpt_onomy_archive_slug' => ( isset( $cpt_settings[ 'cpt_onomy_archive_slug' ] ) && !empty( $cpt_settings[ 'cpt_onomy_archive_slug' ] ) ) ? $cpt[ 'cpt_onomy_archive_slug' ] : NULL );


'cpt_onomy_archive_slug' => ( isset( $cpt_settings[ 'cpt_onomy_archive_slug' ] ) && !empty( $cpt_settings[ 'cpt_onomy_archive_slug' ] ) ) ? $cpt_settings[ 'cpt_onomy_archive_slug' ] : NULL );

This will remove the notice and you’ll be able to publish or update posts of custom post types which use a cpt-onomy without a hassle.