Work hard party harder

I thought working hard all the time will help you to achieve your goals faster, recently I discovered that resting and relaxing are complementary to hard work and is essential for productivity.

Here is the blog post of my mentor Sandeep Karode

who made me realize the 10/20/70 rule:

0% of your time do Intense Activity

20% of your time do Skills Training

70% of your time, Rest

https://sandeepkarode.medium.com/train-fight-rest-b3842005a2e9

If you want to reach your goal faster, learn to relax ūüėČ

Leave your thoughts in the comments if you agree or disagree?

Effective way to learn is repetitive and consistent learning

What you learn, you forget 50% of it within 60 minutes and only retain 20% after a month.

The best way to learn and retain longer is reviewing/revising the content regularly and consistently

Brain is also a muscle, the more regularly you use it on some topic with reputation, the better you become at it. Instead of studying once a week for a longer time, try to study in small slots of time regularly and review it over and over again, so that it will penetrate deeper in your brain.

Difference between AWS Network ACL and Security Group

If you have many instances, managing the firewalls using Network ACL can be very useful. Otherwise, with Security group, you have to manually assign a security group to the instances.

State: Stateful or Stateless
Security groups are stateful: This means any changes applied to an incoming rule will be automatically applied to the outgoing rule. e.g. If you allow an incoming port 80, the outgoing port 80 will be automatically opened.
Network ACLs are stateless: This means any changes applied to an incoming rule will not be applied to the outgoing rule. e.g. If you allow an incoming port 80, you would also need to apply the rule for outgoing traffic.

Rules: Allow or Deny
Security group support allow rules only (by default all rules are denied). e.g. You cannot deny a certain IP address from establishing a connection.
Network ACL support allow and deny rules. By deny rules, you could explicitly deny a certain IP address to establish a connection example: Block IP address 123.201.57.39 from establishing a connection to an EC2 Instance.

Rule process order
All rules in a security group are applied whereas rules are applied in their order (the rule with the lower number gets processed first) in Network ACL.
i.e. Security groups evaluate all the rules in them before allowing a traffic whereas NACLs do it in the number order, from top to bottom.

Defense order
Network ACL first layer of defense, whereas Security group is second layer of the defense for inbound/ingress traffic.
Security group first layer of defense, whereas Network ACL is second layer of the defense for outbound/egress traffic.

Occurrence
Subnet can have only one NACL, whereas Instance can have multiple Security groups.

credit: https://medium.com/awesome-cloud/aws-difference-between-security-groups-and-network-acls-adc632ea29ae

How to Sort a Dictionary by Value in Python

I have implemented the sorting of dictionary using the for loop and lambda function using the Test Driven Data / TDD approach.
Here is the simple code.



"""
@Author: Aseem Jain
@Linkedin: https://www.linkedin.com/in/premaseem/
@Github: https://github.com/premaseem/pythonLab/tree/master/challenge

Sort map/dictionary by keys and values

"""

def sol(m):
    s_k ={}
    s_v ={}
    # On 1
    for k in sorted(m.keys()):
        s_k[k] = m.get(k)

    for v in sorted(m.values()):
        for k in m:
            if v == m.get(k):
                s_v[k] = v

    return (s_k,s_v)


def sol1(m):
    s_k_t = sorted(m.items(),key=lambda i:i[0])
    s_v_t = sorted(m.items(),key=lambda i:i[1])

    s_k_m = {}
    for k,v in s_k_t: s_k_m[k]=v

    s_v_m = {}
    for k,v in s_v_t: s_v_m[k]=v

    return (s_k_m,s_v_m)

def sol2(m):
    s_k_t = sorted(m.items(),key=lambda i:i[0])
    s_v_t = sorted(m.items(),key=lambda i:i[1])
    def tuple_to_map(t):
        new_m = {}
        for k,v in t:
            new_m[k]=v
        return new_m
    return (tuple_to_map(s_k_t),tuple_to_map(s_v_t))


# Test with given map and expected sorted values 
map = { 3:15, 0:10, 5:50, 7:25 }
sorted_key = { 0:10, 3:15, 5:50, 7:25 }
sorted_values = { 5:50, 7:25, 0:10, 3:15}

test_data = [
    (map, (sorted_key,sorted_values) )
]

for given, expected in test_data:
    assert sol(given) == expected
    assert sol1(given) == expected
    print(f"Test passed for: given {given} and expected = {expected}")

Python: Data driven testing

I love doing TDD – Test driven development.

Sharing a small code construct to include variety of data set in a concise manner without depending on any library

def double(n):
    return n*2

test_data = [
    (2 ,4),
    (4 ,8),
]

for given, expected in test_data:
    assert expected == double(given)
    print(f"Test passed for: given {given} and expected = {expected}")

Output on console
Test passed for: given 2 and expectation = 4
Test passed for: given 4 and expectation = 8

How to sort custom objects in Type Script

Imagine you have a student class and you need to sort it reverse order of marks field to calculate ranks of students in the class.

class Student{
    name:String
    marks:Number
    constructor(name:string, marks:number) {
        this.name = name
        this.marks = marks
    }
}

Student objects as part of the array. 

var students:Array<Student> = [
    new Student("aseem",47),
    new Student("prem",97),
    new Student("john",100)

]

sort students by comparing them based on marks field. 

console.log(students.sort( (a,b)=> a.marks > b.marks ? -1:1 ))

here, we used the arrow function which takes a and b objects as params, then we used the ternary operator to compare marks and return the number as -1 or 1 as the result of comparison.