Monthly Archives: December 2017

TypeScript

What is it?
It’s Typed-JavaScript. Javscript doesn’t have types, remember?
So you can do ridiculous things like:

var a = 2;
if( randomFunctionIsTrue() ) a = "2";
console.log( a+2 );

Sometimes, this might produce 4 and sometimes it produces “22”. Who knows.

So this is TypeScript

let a: number = 2;
a = "2" // throws compile time error

TypeScript prevents you from doing stupid stuff.

Notice I said TypeScript throws compile time error. TypeScript files (which end in .ts) compiles to .js files.

In the end, TypeScript is just another syntax for javascript and it creates javascript and runs anywhere that javascript can (frontend, backend, standalone).

But wait, there’s more
You may know that you can write classes in javascript, but they don’t look like classes. That’s because they’re functions. With TypeScript, you can write classes and interfaces that actually look like classes and interfaces.

interface Animal {
...
}

class Dog implements Animal {
...
}

This is not Java. This is TypeScript.

Why use it?
If you’re familiar with Java, you’ll adapt to TypeScript easily.

Because it’s typed, you catch errors early on in compile time, rather than at some arbitrary time during a production run.

Because it’s typed, IDE’s can leverage the compiler hints to give you heads up when there are potential errors. Think the power of Eclipse on java, applied to javascript.

It does everything javascript can because it compiles to javascript. This is just like scala compiling to java bytecode.

You can download a Starter kit example that shows a few interfaces and classes that performs a http request and mysql insert.

Tagged ,

Setting up your python environment

If you’ve worked with python but never used virtualenv, then you will probably have encountered this problem. You are working on 2 projects. One requires version X of a library; the other requires version Y of the same library.

So you use virtualenv to create isolated python environments so you don’t have messy and conflicting dev environments in your system.

But here’s another tool that’s a bit easier to use. It’s a wrapper for virtualenv called virtualenvwrapper.

Here’s the basics.

Download it

pip install virtualenvwrapper

Now add these 2 lines to your ~/.bash_profile (if you’re on OSX)

export WORKON_HOME=~/virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

(Note, the 2nd line may be different depending on where virtualenvwrapper was installed)

Create a virtual env

mkvirtualenv env1

Exit from the virtual env

deactivate

Next time you want to work on that project again

workon env1

If you forgot what virtual envs you have created, you can list them

lsvirtualenv

Bonus Material: Attaching PyCharm to your VirtualEnv
If you use PyCharm as your python IDE, then you should know that you can attach your project to these virtualenv.

Open Preferences then search for “interpreter” or look under Project > Project Interpreter.
Then click on the gear icon in the top right and select “Add…”
Screen Shot 2019-02-15 at 6.00.48 PM

Then select “Existing environment” and click “…” to find the virtualenv’s python executable you created using virtualenvwrapper above.
Screen Shot 2019-02-15 at 6.02.12 PM
If you set your WORKON_HOME to ~/virtualenvs as instructed above, then you’ll find your virtualenv under ~/virtualenvs.
And if your virtual env is called env1, then you’ll select ~/virtualenvs/env1/bin/python (or ~/virtualenvs/env1/bin/python3 if you want to use python3)

Tagged , ,