My Python Notes


Everything is object, including class
Everything has namespace
Everything can be printed / retrospected
Functions are first class object
They can be passed as arguments to other functions,
returned as values from other functions, and
- assigned to variables and stored in data structures.

In Python values carries types
In Python variables do not carry types

Variables means reference to object
Always object reference is passed to function

PyPy : Run time environment

Python supports
* Multiple inheritence
* mixins
* extensive introspection of type and class
* operator overloading with special member function
* complex numbers
* limited support to lambda

Modules

Module = Statements + Definitions
Few important modules are: 
OS : System calls. enviorn for env variables like $HOME. it is dictionary.
sys : to get argv. e.g. sys.argv[1:] because argv[0] is programe name
string, regex, collections, heapq, array, enum, etc. 
numbers, decimals and  fractions : For extra numeric types

Package
__init__.py is default constructor for the package. 


from package import module

Package is a folder, that contains (1) __init__.py file (2) 1+ .py files as modules. (3) optionally contains another folder as package. 

Type conversion:
Moderatly type checked language
Implicit conversion for numeric types
No implicit conversion between string and number

Switch = if ... elif ... elif ... else




* Base Types
String is immutable. So replacing a char, returns new String object. 

* Collection (container) Types

Objects can be easily refernced or indexed

A. Sequence : ordered type

1. String = immutable 

String Format

print("I just printed %s pages to the printer %s" % (num, printer))
print("I just printed {0} pages to the printer {1}".format(num, printer))
print("I just printed {num} pages to the printer {printer}".format(num=num, printer=printer))
print(f"I just printed {num} pages to the printer {printer}")

""" OR ''' are used for multiline string

space, coma and newline are default delimiter used in split
x = input("Prompt message"). here x will be always string. 

2. Lists = mutable, dynamic array. 

List has in-build functions like : sort, len, append(one member), remove (value is argument), To delete entry from list at specific index, use : "del list[index]". insert, extend (list), index, pop = pop(-1) (index is argument), no push function.   
List declaration: a_list = [1, 2, "asdf"] OR a_list = list()
comprehension is like lembda function.
primes = [2] 
e.g. list2 = [x ** 2 for x in list1]
Here primes is list with member 2

By Reference
L1 = [1, 2, 3]
L2 = L1

To make duplicate copy of list
L2 = L1[:]

3. Tuples = immutable.

Tuple declaration: a_tuple = 1, 2, "asdf" OR a_tuple = (1, 2, "asdf")
Tuples are also good for storing closely related data. For example, (x, y, z) coordinates or (r, g, b) colour components. Tuples are records without field name. So order of values and number of fields are very important. It is like ASN.1 encoded byte stream. The actual variable name is defined somewhere in RFC.  So number of field and sequence of values are very important. 

B. Mapping : un-ordered type

1. Dictionaries = Associative Array. Keys are immutable. So String and Tuples can be keys.
Dictionary declaration: dict = {k1:v1, k2:v2}
Useful for JSON objects. 
Dictionary has built-in functions like : has_key(key), get(key, 'optional unknown'), keys, values, del dict['key'], update function will merge the value part: if key found then change value else add the pair.  Two dictionary can be merged with update, for duplicate key


>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 3, 'c': 4}

>>> z = {**x, **y}

>>> z
{'c': 4, 'a': 1, 'b': 3}

one can get list containing keys from dictionary. a_list = list(a_dict)

.set_default() is also important function
defaultdict is function at collections module. It will set default value, when key is added for first time. 


>>> student_grades = defaultdict(list)
>>> for name, grade in grades:
...     student_grades[name].append(grade)

Here, list create an empty list. 
if name key is added to first time, then it will be added with value as empty list []


2. Set : no duplicate. no order, 
Set declaration : a_set = {0, (), "asdf", False}
Set has built-in functions like  : union |, intersection &, difference, symmetric difference -, union - intersection = ^, and subset testing

x = {} This is empty dic not empty set. 

3. Frozenset : immutable set

To iterate through all collection, one can use: 
for item in
The built-in function type() allows us to obtain the type of any object.


Python code compilation to : 

1. CPython : code -> Python byte code
2. Jython : code -> Java Byte Code
3. IronPython : code -> IL(.NET) Byte code

Variable scope

1. local scope
2. global scope
3. built-in scope


Comparison
* string, number, sequence, map all can be compared
* a < b < c = (a < b) && (b < c)


The core built-in types for manipulating binary data are bytes and bytearray. They are supported by memoryview, which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.

To activate any virtual enviornment
source venv/bin/activate

It can be created with below steps

pip install virtualenv
sudo /usr/bin/easy_install virtualenv
cd robot-tests
virtualenv venv
source ./venv/bin/activate
pip install
deactivate

Reference:
https://devopedia.org/python-data-structures



inside class, function is called "class method"
inside class, variable is called "member variable"

"this" in Java, is "self" in Python. "self" can be replaced with another name. it should be first argument. Inside method, object variables can be accessed using self

One more method type, where we pass cls. Here with cls, we can access class member. 



__init__ is constructor
__del__ is destructor
If we have two __init__ function in the class. Then last one will override all earlier __init__ functions.

For inheritance, parent class is passed as input. Obejct is parent class of all. 

At abstract class add @abstractmethod decorator,
Abstract class should be child of ABC

private member start with __


To build iterator class, we need two methods: 
1. __iter__
2. next
in addition to __init__

Generator is just a function. Not Class. It should have yield statement. It returns the iterator object. 

lambda is a one line nameless/anonymous function in python

It is used in map, filter, list comprehension etc. 

map (function, iterable)
map is a special function. 
it returns iterable
The first argument 'function' can be lambda or any other built-in function name like 'str'

filter function. input is predicate. predicate is a condition check. 

itertools module has map, filter, accumulation, count, repeat, permutation, products etc. 

decorator
@class it replace the constructor. 
@property

we can have custom decorator as method 

list comprehension

If 2 functions calls each other recursively then also it is recursive function

raise is to stop programme, when error happens

print("%r") can be used, when we are not sure about integer, or string. 

Recommendations
  • instead of "if condition" use dictionary
  • instead of "loop" use "try catch"
  • for re-usability, write code in class. 
  • add documentation 
  • Use _ for variables, you don’t care about. It is just a convention.
  • The * operator allow us to capture remaining items after an unpacking into a list. For example
head, middle, tail = number[0], number[1:-1], number[-1]
head, *middle, tail = number 
  • Python 2 variables can be swapped with this code
a, b = b, a

  • Debugging in Python

1.
# Some complicated code with bugs
    breakpoint()
      2.
        import pdb; pdb.set_trace()

        • Counter from collections module is useful for word count. input is string. it crates dictionary of word as key and count as value. 
        • This common string group is also useful

          • string.ascii_letters
          • string.ascii_uppercase
          • string.ascii_lowercase
          • string.digits
          • string.hexdigits
          • string.octdigits
          • string.punctuation
          • string.printable
          • string.whitespace

        Singleton design pattern

        https://www.tutorialspoint.com/python_design_patterns/python_design_patterns_singleton.htm
        https://www.xoolive.org/2016/06/29/python-private-constructor.html
        https://stackoverflow.com/questions/8212053/private-constructor-in-python
        https://stackoverflow.com/questions/25040834/prevent-other-classes-methods-from-calling-my-constructor/25041285
        https://treyhunner.com/2018/03/tuple-unpacking-improves-python-code-readability/
        https://github.com/satwikkansal/wtfpython

        Cloud Native Landscape



        5G NR : Part 1


        5G is about IoT. In fact in 4G also, IoT related standardization was started with NB-IoT. 

        Use Cases

        1. AR/VR
        2. Autonomous transportation (car)
        3. Reliable access to remote health-care
        4. Public safety
        5. Smarter Agriculture
        6. Efficient use of energy/utilities
        7. Autonomous manufacturing
        8. Sustainable cities and infrastructure 
        9. Digitized logistics and retails
        Verticals

        Avalanche of traffic volumeMassive connected devicesDiversified use cases
        Autonomous car
        Connectivity Req
        Peak data rate 10Gbps
        Min data rate 50 Mbps
        High user mobility
        Brodband access in dense area
        Connectivity Req
        Low cost
        Low energy
        Low packet size
        Connectivity Req
        Ultra high reliability
        Ultra low latency
        Use cases
        Ultra large volume transfer
        Always connected in crowd
        AR / VR
        Use Cases
        IoT
        IIoT
        Use cases
        V2V communication
        Driver-less car
        Remote surgery
        Smart grid
        Manufacturing Robot

        Market Segments

        1. Enhanced Mobile Broadband (eMBB)
        2. Massive Machine Type Communications (eMTC)
        3. Ultra Reliable and Low Latency Communications (URLLC)

        Key KPIs

        1. Peak data rate
        2. Spectrum efficiency
        3. Mobility
        4. Latency
        5. Connection diversity
        6. Network energy efficiency
        7. Area traffic capacity

        5G standard bodies

        1. 3GPP (ITU-R) : (IMT 2020)
        2. EU - (METIS - 2020) 
        3.1 Japan 2020 and beyond
        3.2 Korea 5G Forum
        3.3 MOST - China

        5G Evolution

        1. IMT-Advanced
        2. Enhanced IMT-Advanced
        3. 5G RAN

        Peak data rate
        Mobility
        Capacity (/km square)
        Number of connected devices / cell
        User plane latency
        Energy Saving (energy / bit)

        5G Standards

        3GPP 5G NR Specification
        Verizon 5G Specification
        Phy channels and modulation38.211 : NRTS V5G.211
        Multiplexing and channel coding38.212 : NRTS V5G.212
        Physical layer procedures38.213 : NRTS V5G.213
        URLhttp://www.3gpp.org/DynaReport/38-series.htmhttp://www.5gtf.net/


        pre 5G standard - https://m.corp.kt.com/eng/html/biz/services/sig.html


        3GPP Important Standards

        TS 38.211 NR; Physical channels and modulation  
        TS 38.212 NR; Multiplexing and channel coding  
        TS 38.213 NR; Physical layer procedures for control  
        TS 38.214 NR; Physical layer procedures for data  
        TS 38.215 NR; Physical layer measurements  
        TS 38.300 NR; Overall description; Stage-2  
        TS 38.321 NR; Medium Access Control (MAC) protocol specification  
        TS 38.322 NR; Radio Link Control (RLC) protocol specification  
        TS 38.323 NR; Packet Data Convergence Protocol (PDCP) specification  
        TS 38.331 NR; Radio Resource Control (RRC); Protocol specification
        TR 38.801 Study on new radio access technology: Radio access architecture and interfaces
        TR 38.912 Study on new radio access technology  

        TR 38.913 Study on scenarios and requirements for next generation access technologies
        TS 23.501 System Architecture for the 5G System

        NSA

        gNB to EPC

        SA

        gNB to 5G CN
        For greenfield deployment

        4G and 5G comparison 

        4G
        5G
        eNBgNB
        Key Functions:
        1. Intercell Radio Resource Management
        2. Resouce Block Control 
        3. Radio Admission Control
        4. Connection Mobility Control
        5. Dynamic Resource Allocation (Scheduler)
        6. Measurement Configuration and Provisioning
        X2 InterfaceXn Interface
        MMEAMF : Access & Mobility Management F
        Key Functions:
        1. NAS Security 
        2. Idle State Mobility Handling
        S-GWUPF : User Plane F
        Key Functions:
        1. Mobility Anchroing 
        2. PDU Handling
        P-GWSMF : Session Management F
        Key Functions:
        1. UE IP Address Allocation 
        2. PDU Session Control.
        S1-CNG-C
        S1-UNG-U
        EPC5G CN = NGC

        U-Plane

        New protocol SDAP over existing PDCP

        Deployment Models


        ModelFyBW
        Indoor Hotspot30 GHzUpto 1 GHz
        Rural700 MHzUpto 20 MHz
        High Speed4 GHzUpto 200 MHz
        Urban + Massive Connections700 MHz OR
        Optionally 2100 MHz

        Reference : TR 38.913 Study on scenarios and requirements for next generation access technologies

        mmWave frequency is > 30 GHz

        5G New Technology

        1. mmWave frequency is > 30 GHz
        2. Massive MIMO > 8 x 8 MIMO
        3. Beam Management
        4. LDPC coding (for U-Plane) and Polar coding (for C-Plane) 
        5. AS Layer
        6. UL Waveform
        7. Subframe structure
        8. HARQ
        9. SDN
        10. NFV
        11. Grant-free UL for IoT

        Numerologies

        1 frame = 10 subframe
        1 subframe's slot = f (n)
        1 slot = 14 symbols

        So 1 frame's slot = 10 x f(n)
        So 1 subframe's symbols = 14 x f(n)
        So 1 frame's symbol = 10 x 14 x f(n) = 140 x f(n)


        Numerology
        Sub carrier BW (kHz)
        Delta F = 2 ** n x 15
        12 x Delta F
        Remark
        Slot / subframe
        Slot / frame
        Symbol / subframe
        Symbol / frame
        0
        15
        180 kHz
        Below 1GHz
        1 GHz to 6 GHz
        1
        10
        14
        140
        1
        30
        360 kHz
        Below 1GHz
        1 GHz to 6 GHz
        2
        20
        28
        280
        2
        60
        729 kHz
        1 GHz to 6 GHz
        24 GHz to 52.6 GHz
        4
        40
        56
        560
        3
        120
        1.44 MHz
        24 GHz to 52.6 GHz
        8
        80
        112
        1120
        4
        240
        2.88 MHz
        16
        160
        224
        2240
        5
        380
        5.76 MHz
        32
        320
        448
        4480


        Slot Format

        TDD or FDD depends upon

        0 : All 14 Symbols are D
        1 : All 14 Symbols are U
        2 : X
        3 : 13 D + 1 X
        4:  12 D + 2 X
        5 : 11 D + 3 X

        D = Downlink
        U = Uplink
        X = Flexible

        To be continued...

        AWS


        As per Wikipedia Amazon has about 90+ cloud services. Here is list of all major services

        90 services 
        Computing
        Amazon Elastic Compute Cloud (EC2) : IaaS
        Amazon Elastic Beanstalk (ESB) : PaaS
        Amazon Lambda : FaaS
        Networking
        Amazon Route 53
        Amazon Virtual Private Cloud (VPC)
        AWS Direct Connect
        Amazon Elastic Load Balancing (ELB)
        AWS Elastic Network Adapter (ENA)
        Content Delivery
        Amazon CloudFront : CDN
        Contact Center
        Amazon Connect
        Storage and content delivery
        Amazon Simple Storage Service (S3) : Object storage
        Amazon Glacier : For archiving data
        AWS Storage Gateway : iSCSI
        Amazon Elastic Block Store (EBS) : block level storage
        AWS Import/Export
        Amazon Elastic File System (EFS)
        Database
        Amazon DynamoDB : NoSQL
        Amazon Elastic Cache : Like Memcached and Redis
        Amazon Relational Database Service (RDS) : MySQL, Oracle, SQL Server, PostgreSQL
        Amazon Redshift : column based
        Amazon SimpleDB
        AWS Data Pipeline
        Amazon Aurora : MySQL
        Mobile services 
        AWS Mobile Hub : Add/Configure features for mobile apps
        Amazon Cognito : Singup / signin
        AWS Device Farm : Testing
        Amazon Pinpoint : email/SMS/push notofication and tracking customer activity
        Deployment
        AWS CloudFormation : IaaC to configure cloud
        Amazon Elastic Beanstalk (ESB) : PaaS
        AWS OpsWorks : Configure EC2 using Chef
        AWS CodeDeploy
        Management
        AWS System Manager 
        Amazon Indentity and Access Management (IAM) : Implicit Service
        AWS Directory Service
        Amazon CloudWatch
        AWS Management Console (AWS Console)
        Amazon CloudHSM (Hardware Security Module)
        AWS Key Management Service (KMS)
        Amazon EC2 Container Service (ECS)
        Application services
        Amazon API Gateway
        Amazon CloudSearch
        Amazon DevPay
        Amazon Elastic Transcoder (ETS) : S3 hosted videos transcoding for mobile
        Amazon Simple Email Service (SES)
        Amazon Simple Queue Service (SQS)
        Amazon Simple Notification Service (SNS)
        Amazon Simple Workflow (SWF)
        Amazon Cognito
        Amazon AppStream 
        Analytics 
        Amazon Athena : Serverless query to S3 content
        Amazon Elastic MapReduce (EMR) : PaaS
        Amazon Machine Learning
        Amazon Kinesis : like Apache Kafka
        Amazon Elastic Search : elastic search and Kibana
        Amazon QuickSight
        Amazon Sagemaker : Deep Learning. Build, train and deploy Machine Learning models
        Miscellaneous
        Amazon Marketplace Web Service (MWS)
        Amazon Mechanical Turk (Mturk)
        Amazon Product Advertising API : old names (1) Amazon Associate Web Service (A2S) + (2) Amazon E-commerce Service (ECS)
        Amazon Gift Code on Demand (AGCOD) : For corporate customer
        AWS Partner Network (APN)
        Amazon Lumberyard : Game engine 
        Amazon Chime : Colloborative service for video, conf. and IM. 
        Amazon Autoscaling
        Machine Learning
        AWS DeepLens : Computer vision and deep learning
        Amazon Comprehand : NLP
        Amazon Rekognition Video
        Amazon Sagemaker : Deep Learning. Build, train and deploy Machine Learning models
        Amazon translate
        Amazon Transcribe
        Developer tools

        AWS Cloud9 : cloud IDE