Attend to Your Mistakes

My daughter is an avid book reader now. She reads more books than I do, but most all her books are fiction. She likes them. This year I challenged her to read at least 5 non fiction books of her choice. She has done three so far and I suggested the book The Little Book of Talent by Daniel Coyle next.

While she was starting with it, I wanted to re-read the book. The book is basically collection of short tips from his research for auther’s other similar books on talent. The best thing about this book is you can read this book in a non linear way.

So I started with this chapter. Here’s excerpt that did resonate with me.

Most of us are allergic to mistakes. When we make one, our every instinct urges us to look away, ignore it, and pretend it didn’t happen. This is not good, because as we’ve seen, mistakes are our guideposts for improvement. Brain-scan studies reveal a vital instant, 0.25 seconds after a mistake is made, in which people do one of two things they look hard at the mistake or they ignore it.

People who pay deeper attention to an error learn significantly more than those who ignore it.

Develop the habit of attending to your errors right away. Don’t wince, don’t close your eyes; look straight at them and see what really happened, and ask yourself what you can do next to improve. Take mistakes seriously, but never personally.

Looking forward to what Anmol will like?

Get All Info About a Python Environment

Conda makes creating environments easy and if you are anything like me, over the course of time you end up having many enviroements and it becomes difficult to knwo what is what.

Basic hygine is to make environemnet names unique and descriptive. But even then knowing what an environment has becomes difficult.

Here’s a general script that I use to get all python related information inside an environemnt

Get all python info:

import sys
import os
import pkg_resources
from pprint import pprint


pprint({
    'sys.version_info': sys.version_info,
    'sys.prefix': sys.prefix,
    'sys.path': sys.path,
    'pkg_resources.working_set': list(pkg_resources.working_set),
    'PATH': os.environ['PATH'].split(os.pathsep),
})

Simple and it works.

No One is Bored When They’re Asleep

Some quotes and sayings collected over the last few months.

Taking responsibility (without authority) and creating a positive cycle of generous action. Leading by example. Finding a small corner where you can make a difference–and then making a difference. – Seth Godin

The same is true with all of the initiatives in our culture. Design, movements and ideas are all trapped, waiting to be opened, and then the rest of us will happily pile on – Seth Godin 

A year from now…  Will today’s emergency even be remembered? Will that thing you’re particularly anxious about have been hardly worth the time you put into it? Better question: What could you do today that would matter a year from now? – Seth Godin

When in doubt, go outside. Especially when it’s inconvenient. – Seth Godin 

Your boat, your compass.  – Seth Godin 

A day trader would never take this bet. But a day trader rarely makes an impact. – Seth Godin

Begin with a simple question: What’s it for? – Seth Godin 

Remarkability lies in the edges. The biggest, fastest, slowest, richest, easiest, most difficult. It doesn’t always matter which edge, more that you’re at (or beyond) the edge. – Seth Godin 

Consider surrounding yourself with totems that invite generous action. They’re souvenirs of your best self.  – Seth Godin

You’re only granted so much energy to expend in a lifetime. You’re almost certainly best off focusing it as intensely as you can on the targets that seem to really move the needle.  – Cal Newport 

Next time, take the lead. Not because you have to, but because you can – Seth Godin 

When we have alternatives, we compromise instead of commit. – Seth Godin 

Find someone who has already optimized for the reality you’re about to enter and learn from them. –Seth Godin 

Bored means that you’re paying attention (no one is bored when they’re asleep.)  -Seth Godin 

After you do the reading, then what are you going to do? Good judgment and a thoughtful point of view are now scarce assets worth seeking out. What have you done with what you’ve learned?  – Seth Godin

Looking back is an essential part of moving forward. – Azure Devops docs

People almost always want a smile, a kind word or a hand up sooner than we think and for longer than we imagine. – Seth Godin 

Your customers don’t care what it took for you to make something. They care about what it does for them. – Seth Godin 

Starter Argparse and Setup.py templates

I tend to develop my python scripts as small apps, this way I can call and use them from every where in command line without relying on the calling the python scripts

Here’s a template of the setup.py and argparse template that I always start with.

Setup.py example is for making the application as a package and installable.

Install_requires is a list of packages required by the app.
Entry_points is optional if given, app can be called using the ‘app_name’ from command line.

import os
from setuptools import find_packages, setup
 
setup(
    name= 'name',
    version="3.0",
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    license="Private",
    description= "This is the description",
    author= "author_name",
    author_email= "author_contact_email",
 
    install_requires=["psutil", "pywin32"],
 
    entry_points={
        'console_scripts': ['app_name = app:main']
    }
)
 

Argparse example template

Following a starter template for using argparse.

def main():
    parser = argparse.ArgumentParser(
        description="Pro Thermals: Uses Iges from NX Update to perform thermal analysis on scenery Model")
    parser.add_argument("indir", type=str, help="Input dir ")
    parser.add_argument("outdir", type=str, help="Output dir ")
    parser.add_argument("-s", "--scpath", type=str, help="simulation executable path ",
                        default="W:\\simulation_app\\MSWindows\\bin\\x64")
    parser.add_argument("-o", "--omp-threads", type=int, help="Specifies how many OMP_NUM_THREADS app uses while running ",
                        default=1)
 
    args = parser.parse_args()
 
    input_dir = args.indir or os.getcwd()
    output_dir = args.outdir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
 
    run_ok = db_thermals(input_dir,
                         output_dir,
                         args.scpath, args.omp_threads)
 
if __name__ == "__main__":
    main()

In the above example, indir and outdir are required positional arguments, while scpath and omp-threads are optional arguments with some defaults.

.

Few Useful Conda Commands To Get Started.

Conda is a tool for managing and deploying applications, environments and packages.

I use conda a lot. It has lot of functionalities and there is lot to learn in it but for regular usage, there are just few things that they need to know to use it productively.

so here’s a list of commands that I had shared with my colleagues to get them started in conda.

This commands will just be enough to get the ground running and do most of the stuff.

Few useful conda commands

  • conda env export –f env.yml -> Exports the environment in env.yml file
  • conda env remove -n env_name -> Remove a environment
  • conda create –yes -n env_name python=3.5 -> Create an environment with python 3.5
  • conda info –envs -> List all environments
  • activate env_name -> activates environment
  • deactivate -> deactivates the current environment
  • conda install package_name -> install a package in the current environment
  • conda uninstall package_name -> uninstall a package in the current environment

Subtraction the real engine

Have been thinking of these three quotes, over the last few weeks….

Blaise Pascal famously ended a letter with, “I would have written a shorter letter, but I did not have the time.”

Another quote comes from Antoine de Saint-Exupery: “Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.”

“So knowledge grows by subtraction much more than by addition given that what we know today might turn out to be wrong but what we know to be wrong cannot turn out to be right, at least not easily.” – Nassim Nicholas Taleb in Antifragile

Few common things to look out for while converting python 2 to python 3

If you are converting your python2 code to python 3 manually.

apart from the print statement, here are few things that i encountered.

  1. xrange is not available in python 3, simply used range

2. dict_keys in python 2 was a list and were iterable, but in python 3 you get the following error

this_blade_face = str(initial_node_nums.keys()[-1])

TypeError: 'dict_keys' object does not support indexing

If you need to use the keys, then use list(dict.key())

this_blade_face = str(list(initial_node_nums.keys())[-1])

3. Iterations

for loop in range(start, stop + 1):
TypeError: 'float' object cannot be interpreted as an integer

if start and stop are float, they will not work in python 3

so use integers in your range loop.

4. Map in python 2 returned a list but in python 3 it returns a map object.

in python 3 this code spits teh following error.

times = map(float, timeInput)
ramp1T1 = ((times[count] - times[count-1]) - timeoffset)/5

TypeError: 'map' object is not subscriptable

solution:

times = list(map(float, timeInput))

Well thats it for this post, but do check out the 2to3 tool available in standard python, which will do most of this convertion automatically for you.

What’s not going to change in the next 10 years?

Jeff Bezos once explained why this was critical:

I very frequently get the question: “What’s going to change in the next 10 years?” That’s a very interesting question.

I almost never get the question: “What’s not going to change in the next 10 years?” And I submit to you that that second question is actually the more important of the two.

You can build a business strategy around the things that are stable in time. In our retail business, we know that customers want low prices, and I know that’s going to be true 10 years from now. They want fast delivery; they want vast selection. It’s impossible to imagine a future 10 years from now where a customer comes up and says, “Jeff I love Amazon, I just wish the prices were a little higher.” Or, “I love Amazon, I just wish you’d deliver a little slower.” Impossible.

So we know the energy we put into these things today will still be paying off dividends for our customers 10 years from now. When you have something that you know is true, even over the long term, you can afford to put a lot of energy into it.

Python 2 to Python 3

Python 2 has officially retired, you should be using python 3 now.

2to3

If you have few code and scripts in python2, there’s a tool available with standard python installation, which can help you convert your scripts to python 3 without much effort.

2to3 is the tool available your in command line.

Suppose my python2 code is in the folder python2-version/mycode, the following line will create a python3 version of the script.

2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode

And as the docs mention, you can use some flags for more customization:

  • the -w flag to enable writeback, which applies the changes to the file
  • the -n to disable backups

There are a few more flags; see the docs for more information

If you don’t have 2to3 on your path, you can directly invoke lib2to3:

python -m lib2to3 -W -n directory\file.py --output-dir=python3-version/mycode

Bacteria and Virus

Everyone I talk to sometime in the conversation talks about this.

Why don’t we know how to cure/tame this virus when we do have cure for so many diseases?

I can’t explain this better than the below note.

There are two reasons for this failure to have anything on the shelf that can be used to treat viruses: one biological, the other economic. The biological problem, as Amesh Adalja of Johns Hopkins University argued in a prescient call to arms just before the pandemic struck, is that viruses do not have their own biochemistry, because they borrow ours.

So unlike, say, tuberculosis, there is not much to attack. As any doctor will tell you, antibiotics are no use in fighting a virus. They interfere with machinery found only in bacteria, but there is no equivalent machinery in viruses — which are just a bunch of genes (15 of them in the case of Sars-CoV-2) that borrow our body’s machinery to replicate themselves.

The problem is that viruses differ from each other, so treatments that work for one seldom work for another. The drugs that work against HIV-1, the main cause of Aids, sometimes do not even work against HIV-2,a milder version of the virus. Those that work against herpes don’t kill the very similar cytomegalovirus. One influenza drug works only against influenza A and not B. One antiviral kills just one genotype of hepatitis C. It is no coincidence that the antiviral treatments capable of attacking more kinds of virus, such as ribavirin, are also the most toxic to the patient, because they tend to attack the machinery of the host as well.

Do invest some time to read to this? Also if you decide to read more you will discover how Fujifilms (not Kodak) can help fight this pandemic.

http://www.rationaloptimist.com/blog/race-to-cure-covid

Split text after nth occurrence of character

python never fails to amaze me. You have to keep using and and you find these little gems hidden in it.

Suppose you have a string like this

t = "3,5,2019,9.99, Argos,facial sauna ,(10 Argos gift voucher, card)(19.99)"

you want to split the numbers and the tags, meaning at the fourth occurance of comma, how will you do it?

simple.

t.split(",",4)

this returns a list like this

['3',
'5',
'2019',
'9.99',
' Argos,facial sauna ,(10 Argos gift voucher, card)(19.99)']

docstring of the split

S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.

What was surprising for me was thay i have used split for so long and yet haven’t seen this untill last year.

My Most Commonly used Powershell Commands

Lately I have moved away from using vanila command line of windows to using powershell

For may tasks I find it useful. Some are still better in the old cmd.

Here are few commands that I have regularly used in powershell.

Create a zip file with the contents of C:\Stuff\

Compress-Archive -Path C:\Stuff -DestinationPath archive.zip

Add more files to the zip file (Existing files in the zip file with the same name are replaced)

Compress-Archive -Path C:\OtherStuff*.txt -Update -DestinationPath archive.zip

Extract the zip file to C:\Destination\

Expand-Archive -Path archive.zip -DestinationPath C:\Destination

Copy files folders

Copy-Item -Path C:\PointA\1.txt -Destination C:\PointB\

A real example, something that is part of a backup script

powershell.exe -NoP -NonI -Command "Copy-Item -Path '%root%\apps_installer' -Recurse -Destination 'path\to\directory' -Container -force"

To explore more, type powershell in your windows command prompt and then type help.

One of the best thing about powershell is it extensive help.

When something bad happens….

From Amazon’s 2019 letter shareholders.


“When something bad happens you have three choices. You can either let it define you, let it destroy you, or you can let it strengthen you.” – Thttps://ir.aboutamazon.com/files/doc_financials/2020/ar/2019-Shareholder-Letter.pdfheodor Seuss Geisel

No
As Jeff Bezos says in the letter “I am very optimistic about which of these civilization is going to choose.” I agree.

Some Common Schedule Tasks Command

I have been using schedule tasks in windows quite often and this has become a tool of choice for running many automation scripts on my work and home computer.

Here are few of the common scheduling tasks create commands that I most commonly use.

Below is how to schedule something to run at 7:00 every day every 1 hour for a duration of 1 day.

schtasks /create /tn "monitor" /tr "C:\Users\singh\Desktop\daytracker\activate_tracker.bat" /sc DAILY /st 09:45 /f /RI 60

Open tasks at certain time

SCHTASKS /Create /SC DAILY /TN checkout /TR "C:\Users\singh\Desktop\open_tasks.bat" /ST 16:15 /ET 17:15 /K

Run a particular task

schtasks /run /tn train

Use schtasks /create /? to see many other examples

Inactivity matters

For me investing is like gardening, it doesn’t need to always active. Work the soil, sow the seeds and then later let nature/time take its course.

Of course, you need to regularly water the plants, prune out the weeds and sometime, move the plants in shed, but most of the work is in the beginning and for the rest of the time inactivity matters. Staying away is better.

Was looking at this old post and this prompted me to analyze my activity in investing

Green is buying activity and orange is the selling.

Get Outlook Entries With Python 3

This was an old post, and was written in python 2, but that refused to work in python3 as pointed by win

Today found some time to look at this and fixed the code. So here’s new improved code that works both in python 2 and python3. The new code gives user the ability to change the date time format as suggested in the first comment.

import win32com.client
import datetime
from collections import namedtuple


event = namedtuple("event", "Start Subject Duration")


def get_date(datestr):
    try:  # py3
        adate = datetime.datetime.fromtimestamp(datestr.Start.timestamp())
    except Exception:
        adate = datetime.datetime.fromtimestamp(int(datestr.Start))
    return adate


def getCalendarEntries(days=1, dateformat="%d/%m/%Y"):
    """
    Returns calender entries for days default is 1
    Returns list of events
    """
    Outlook = win32com.client.Dispatch("Outlook.Application")
    ns = Outlook.GetNamespace("MAPI")
    appointments = ns.GetDefaultFolder(9).Items
    appointments.Sort("[Start]")
    appointments.IncludeRecurrences = "True"
    today = datetime.datetime.today()
    begin = today.date().strftime(dateformat)
    tomorrow = datetime.timedelta(days=days) + today
    end = tomorrow.date().strftime(dateformat)
    appointments = appointments.Restrict(
        "[Start] >= '" + begin + "' AND [END] <= '" + end + "'")
    events = []
    for a in appointments:
        adate = get_date(a)
        events.append(event(adate, a.Subject, a.Duration))
    return events


if __name__ == "__main__":
    events = getCalendarEntries()

Sample result

[event(Start=datetime.datetime(2020, 4, 7, 8, 0), Subject='Quick Project Review (30 mins to save future work)', Duration=30),
 event(Start=datetime.datetime(2020, 4, 7, 9, 0), Subject='Billing detail', Duration=15),
 event(Start=datetime.datetime(2020, 4, 7, 9, 0), Subject='DF DW', Duration=60),
 event(Start=datetime.datetime(2020, 4, 7, 10, 0), Subject='hw', Duration=1),
 event(Start=datetime.datetime(2020, 4, 7, 10, 50), Subject='Canceled: Daily Standups are back and they are better than ever..!', Duration=10),
 event(Start=datetime.datetime(2020, 4, 7, 11, 0), Subject='Canceled: Sprint Planning / Refinement (Alternating Weeks)', Duration=120),
 event(Start=datetime.datetime(2020, 4, 7, 12, 0), Subject='Daily Cafe. / FIKA', Duration=30),
 event(Start=datetime.datetime(2020, 4, 7, 12, 0), Subject='CABI COP Weekly Meeting', Duration=30),
 event(Start=datetime.datetime(2020, 4, 7, 12, 0), Subject='Design System Engagement', Duration=30),
 event(Start=datetime.datetime(2020, 4, 7, 16, 0), Subject='rasise invoices', Duration=90),
 event(Start=datetime.datetime(2020, 4, 7, 16, 30), Subject='shutdown', Duration=15)]