There is no way to explicitly know that your node is on a minority-hashrate fork because to do so would mean awareness of all the new rules that makes the majority-hashrate fork valid, and then determining that more proof-of-work is accumulated on that chain. For that to be possible, you must be running an upgraded node already.
There are a few warning ...
What does getbalance give me?
getbalance returns the total available balance according to your configuration. If you have -spendzeroconfchange set to 1 (the default), this is your total confirmed + unconfirmed balance.
Whichever it gives me, I can then add or subtract the pending value to get the other value.
By default getbalance - getunconfirmedbalance =...
There are two cases here:
Block with the tx became orphaned and the transaction is still valid. In this case it is returned to the mempool. gettransaction will return unconfirmed transaction detail
Another conflicting transaction got confirmed ( having same inputs). In this case existing txid becomes invalid and it is discarded
I had named my script bitcoinrpc.py and had the import bitcoinrpc. Naturally it couldn't find the package. Renamed the script and it works.
Lesson: DO NOT have a file of the same name as the package you are trying to import.