Journey from static to dynamic (Java to Python)

Via this blog, I want to inspiring developers who have plan to learn. It also covers all pros and cons of Python in many aspects, from philosophy to OOP design, from library to handy shortcut.

Because Python go from one person project, it philosophy quite effective. The main target is make things fast, keep it simple, and do it good enough. Like sentences below:
Borrow ideas from elsewhere whenever it makes sense.
“Things should be as simple as possible, but no simpler.” (Einstein)
Do one thing well (The "UNIX philosophy").
Don’t fret too much about performance--plan to optimize later when needed.
Don’t fight the environment and go with the flow.
Don’t try for perfection because “good enough” is often just that.

The beauty of Python
"The Zen of Python" - Wrote by Tim Peter, main contributor of Python.
  “Beautiful is better than ugly.
   Explicit is better than implicit.
   Simple is better than complex.
   Complex is better than complicated.

   Flat is better than nested.
   Sparse is better than dense.
   Readability counts.
   Special cases aren't special enough to break the rules.

   Although practicality beats purity.
   Errors should never pass silently.
   Unless explicitly silenced.
   In the face of ambiguity, refuse the temptation to guess.
   There should be one-- and preferably only one --obvious way to do it.
   Although that way may not be obvious at first unless you're Dutch.

   Now is better than never.
   Although never is often better than *right* now.
   If the implementation is hard to explain, it's a bad idea.
   If the implementation is easy to explain, it may be a good idea.
   Namespaces are one honking great idea -- let's do more of those!”

Why python is beautiful
Easy to learn and understand
I cannot stress enough how beautiful it feels to code in Python because of its neat flawless syntax. There are no braces for you to keep on continually balancing, white spaces and tabs automatically do it for you , also are no semicolons to remember.

``` JAVA
public class HelloWorld {
   public static void main (String[] args) {
       System.out.println("Hello, world!");
   }
}
```

``` PYTHON
print("Hello, world!")
```

Lean project structure
Multiple public classes can be defined in a single file. If your application has 15 classes, the entire code base could be stored in a single file. Same thing in Java forces you to create all 15 files.

Dynamic typing
In Python, you never declare anything. An assignment statement binds a name to an object, and the object can be of any type. If a name is assigned to an object of one type, it may later be assigned to an object of a different type. Python container objects also (e.g. lists and dictionaries) can hold objects of any type. When you retrieve an object from a container, it remembers its type, so no casting is required.

OOP with multiple inheritances
Python has been an object-oriented language since it existed. Because of this, creating and using classes and objects are downright easy. Java single inheritance is a pain, you must know and use Abstract Class and Interface for a good design structure. Python is object-oriented language to core, cause pretty much everything is an object, whether it is a number, a function… or even None, True or False.

``` JAVA
interface Moveable {
   default void move(){
       System.out.println("I am moving fast, buddy !!");
   }
}
 
interface Crawlable {
   default void crawl(){
       System.out.println("I am crawling !!");
   }
}
 
public class Animal implements Moveable, Crawlable {
   public static void main(String[] args) {
       Animal self = new Animal();
       self.move();
       self.crawl();
   }
}
```

``` PYTHON
class Moveable:
   def move(self):
       print("I am moving fast, buddy !!")

class Crawlable:
   def crawl(self):
       print("I am crawling !!")

class Animal(Moveable, Crawlable):
   def do(self):
       self.move()
       self.crawl()

animal = Animal()
animal.do()
```

Easy to create objects
No more creating an ocean of constructor functions before coding the real important thing. With list argument, key-word argument, default value in Python avoid function overloading problem. List argument is used to send a non-keyworded variable length argument list to the function. Key-word argument allows you to pass keyworded variable length of arguments to a function. Python also provides support for default argument values, that is function arguments that can either be specified by the caller or left blank to automatically receive a predefined value.

``` JAVA
public class Employee {
   private String myEmployeeName;
   private int    myTaxDeductions = 1;
   private String myMaritalStatus = "single";

   public Employee(String EmployeName) {
       this(employeeName, 1);
   }

   public Employee(String EmployeName, int taxDeductions) {
      this(employeeName, taxDeductions, "single");
   }

   public Employee(String EmployeName, int taxDeductions, String maritalStatus) {
      this.employeeName    = employeeName;
      this.taxDeductions   = taxDeductions;
      this.maritalStatus   = maritalStatus;
   }
...
```

``` PYTHON
class Employee():
   def __init__(self, employee_name, tax_deductions=1, marital_status="single"):
       self.employee_name    = employee_name
       self.tax_deductions   = tax_deductions
       self.marital_status   = marital_status
...
```

Awesome set of Libraries
Python’s standard library is very extensive, library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming.
You also have rich set of libraries to choose from image processing, or natural language processing, or web scraping. These libraries come with excellent documentation.

Syntactic sugar
Swapping Variables
```
x = 6
y = 5
x, y = y, x
```

Inline if Statement
```
a =  "Hello" if True else "World"
```

Numerical Comparison
```
if 3 > x > 1:
   print x
```

Iterate Through Two Lists at the Same Time
```
nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
for teama, teamb in zip(nfc, afc):
   print teama + " vs. " + teamb
```

List Comprehension
``` # print element which is even
numbers = [1,2,3,4,5,6]
even = [number for number in numbers if number%2 == 0]
```

Dictionary Comprehension
```
teams = ["Packers", "49ers", "Ravens", "Patriots"]
print {key: value for value, key in enumerate(teams)}
```

Converting a List to a String
```
teams = ["Packers", "49ers", "Ravens", "Patriots"]
print ", ".join(teams)
```

Start a http web server
```
python -m SimpleHTTPServer
```

Great productivity
So programs don’t need to compile before run, code don’t need braces, variable type much more flexible, … With all of these advantages above, most of us will be more much more productive and happier.

The hard & pain
With static programming language, everything so easy to understand, to learn without magic. But with dynamic:
  • Lack of static typing gives programmers headaches. Actually is hard to know which type will return from a complex function. Or which line will raise TypeError in run time.
  • Lack of performance for performance-critical applications or feel bad with Global Interpreter Lock? “In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython's memory management is not thread-safe so that it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations”, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once.” - From Python Wiki


Our rules to live with dynamic life
  • Features separated into, core library, data access service, business logic, API.
  • Code coverage must be greater than 95 percent, or you feature branch will stay at your local forever! Automation test tool will mark these branch as build fail.
  • Every feature branch must be reviewed with at least two round, one from team members and final from our CTO.

Latency Numbers Every Programmer Should Know

Latency Comparison Numbers

L1 cache reference 0.5 ns Branch mispredict 5 ns L2 cache reference 7 ns 14x L1 cache Mutex lock/unlock 25 ns Main memory reference 100 ns 20x L2 cache, 200x L1 cache Compress 1K bytes with Zippy 3,000 ns 3 us Send 1K bytes over 1 Gbps network 10,000 ns 10 us Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD Read 1 MB sequentially from memory 250,000 ns 250 us Round trip within same datacenter 500,000 ns 500 us Read 1 MB sequentially from SSD* 1,000,000 ns 1,000 us 1 ms ~1GB/sec SSD, 4X memory Disk seek 10,000,000 ns 10,000 us 10 ms 20x datacenter roundtrip Read 1 MB sequentially from disk 20,000,000 ns 20,000 us 20 ms 80x memory, 20X SSD Send packet CA->Netherlands->CA 150,000,000 ns 150,000 us 150 ms Notes ----- 1 ns = 10^-9 seconds 1 us = 10^-6 seconds = 1,000 ns 1 ms = 10^-3 seconds = 1,000 us = 1,000,000 ns Credit ------ By Jeff Dean: http://research.google.com/people/jeff/ Originally by Peter Norvig: http://norvig.com/21-days.html#answers Contributions ------------- Some updates from: https://gist.github.com/2843375 'Humanized' comparison: https://gist.github.com/2843375 Visual comparison chart: http://i.imgur.com/k0t1e.png Animated presentation: http://prezi.com/pdkvgys-r0y6/latency-numbers-for-programmers-web-development/latency.txt

6 free way to monitor your Docker containers

Docker is so popular, build, ship and run anywhere. But how to monitor your containers ?
Here is six solutions to choose: 
  1. Docker stat: https://docs.docker.com/reference/commandline/stats/
  2. cAdvisor: https://github.com/google/cadvisor
  3. Telegraf + InfluxDB + Grafana
    1. https://github.com/influxdb/telegraf
    2. https://influxdb.com/
    3. http://grafana.org/
  4. Logstash + Elasticsearch + Kibana: https://www.elastic.co
  5. Newrelic (limited free): http://newrelic.com/docker
  6. Datadog (limited free): https://www.datadoghq.com/blog/monitor-docker-datadog/

A complete Java play framework skeleton

This is skeleton of a play framework project [2.3.7] with Java 8 include a lot of "must have" dependecies and useful classes (Exception logging, Sql util,Datatime Convert, Uuid, MD5, SH1, ...), also already integrated with:

- Guice [Dependencies Injection]
- Jooq [Orm]
- Akka [Concurrency framework]
- RabbitMq [Message queue]
- Redis [In mem Nosql]

Source code: https://github.com/phamptu/java-play-skeleton

How to classify junior, intermediate, senior software developer


  • JUNIOR
    • A good junior developer can be given a known task, and be expected to execute it quickly, and well.



  • INTERMEDIATE
    • A good intermediate developer needs less supervision. They can be trusted to raise issues of code design, and play a valuable role in design discussions. They are also the “workhorses” of the dev team. However, further mentoring and higher level supervision is still vital.



  • SENIOR
    • A senior developer thinks in terms of “context” when applying theory. They understand the only way to build good software is by adapting theory to fit the needs of the client, codebase, team, tools, and organization.
    • A senior developer is the only person fully qualified to choose technology and platforms.
    • A senior developer understands that everything and will look for what that is for design patterns, libraries, frameworks, and processes.