Sunday, 23 January 2022

Python Lists and methods

 

List Items

List items are ordered, changeable, and allow duplicate values.

List items are indexed, the first item has index [0], the second item has index [1] etc.


Ordered

When we say that lists are ordered, it means that the items have a defined order, and that order will not change.

If you add new items to a list, the new items will be placed at the end of the list.

Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
  • Dictionary is a collection which is ordered** and changeable. No duplicate members.


Python has a set of built-in methods that you can use on lists.

MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

*Set items are unchangeable, but you can remove and/or add items whenever you like.

**As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.


The list() Constructor

It is also possible to use the list() constructor when creating a new list.

Example

Using the list() constructor to make a List:

thislist = list(("apple""banana""cherry")) # note the double round-brackets
print(thislist)

If you insert more items than you replace, the new items will be inserted where you specified, and the remaining items will move accordingly:

Example

Change the second value by replacing it with two new values:

thislist = ["apple""banana""cherry"]
thislist[1:2] = ["blackcurrant""watermelon"]
print(thislist)

Result: thislist = ["apple", "blackcurrant", "watermelon", "cherry"]

Del

The del keyword also removes the specified index:

Example

Remove the first item:

thislist = ["apple""banana""cherry"]
del thislist[0]
print(thislist)
Try it Yourself »

The del keyword can also delete the list completely.

Example

Delete the entire list:

thislist = ["apple""banana""cherry"]
del thislist 
Try it Yourself »


Looping Using List Comprehension

List Comprehension offers the shortest syntax for looping through lists:

Example

A short hand for loop that will print all items in a list:

thislist = ["apple""banana""cherry"]
[print(x) for x in thislist] 
Try it Yourself »

List Comprehension

List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

Example:

Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.

Without list comprehension you will have to write a for statement with a conditional test inside:

Example

fruits = ["apple""banana""cherry""kiwi""mango"]
newlist = []

for x in fruits:
  if "a" in x:
    newlist.append(x)

print(newlist) 
Try it Yourself »

With list comprehension you can do all that with only one line of code:

Example

fruits = ["apple""banana""cherry""kiwi""mango"]

newlist = [x for x in fruits if "a" in x]

print(newlist) 
Try it Yourself »


The Syntax

newlist = [expression for item in iterable if condition == True]

The return value is a new list, leaving the old list unchanged.


Condition

The condition is like a filter that only accepts the items that valuate to True.

Example

Only accept items that are not "apple":

newlist = [x for x in fruits if x != "apple"
Try it Yourself »

The condition if x != "apple"  will return True for all elements other than "apple", making the new list contain all fruits except "apple".

The condition is optional and can be omitted:

Example

With no if statement:

newlist = [x for x in fruits] 
Try it Yourself »




Tuesday, 28 January 2020

Microservices


For many years now we have been building systems and getting better at it. Several technologies, architectural patterns, and best practices have emerged over those years. Microservices is one of those architectural patterns which has emerged from the world of domain-driven design, continuous delivery, platform and infrastructure automation, scalable systems, polyglot programming, and persistence.

What is Microservices?
It is an architectural style which is autonomous, independently deployable services collaborate together to form an application.

They should be optimally small, but there is no official size limit. Many teams try to keep it as small as possible(like 100 lines of code), but the benefits of microservices still apply even if the application is a bit large.

Monolith: It is a software application that has all of its code in a single codebase. The build artifact is generally a single process that runs on a single server or machine and keeps all of its data in a single database.
The development of an environment for monolith typically uses a single consistent technology such as what programming language or SDK you use.

Benefits of Monolith architecture
SimplicityOne codebase. Easy to find things.
Deployment: One application to replace.

Disadvantage: It is difficult to scale. It is generally meant for small teams. If the project grows larger than the difficulty arises in maintaining the application. Even if you have modularized your application well, these modules will get too entangled that it becomes difficult to update the app easily.
A single line change requires an entire application to be redeployed, which causes additional regression and it's risky. This also requires a lot of downtimes.
Horizontal scaling often not possible and hence you have to go for vertical scaling(to install costly and powerful servers). Also, the whole application has to be scaled together instead of a single component.
It’s a legacy technology. The whole codebase has to be updated to include any new technology. This reduces agility.

Distributed Monoliths
Even if your services which are separated different components and are interacting over the internet, it is still not a microservice, as it is accessing a single shared database and they are all tightly coupled to each other such a way that you have to deploy them all together. Any new changes often require changes to multiple components.

Benefits of microservices
  1. As Microservices are separated into smaller pieces each piece can be owned and maintained by a separate team. In this way, a microservice is easier to understand. for sizing microservice should be small enough so that it can easily be rewritten and the old code can be thrown away.
  2. Microservices allow us the Flexibility to use new technologies without the need to upgrade the complete system in a single go. This also allows us to use the right tool for our needs. Eg. One microservice can use a relational database while the other can use a document database.
  3. Since they are loosely coupled they can be deployed independently. This causes low downtime and high-frequency deployment. The risk of breaking the complete application is low because of loose coupling.
  4. This is cost-effective and easily scalable.
  5. Easy to adapt to change of technology or design. Also, they are reusable.

Disadvantages of microservices
Along with many advantages of microservices there comes some disadvantages too. so before deciding that the microservice architecture is a good fit, you should be aware about some of the challenges which microservices bring along.
  1. Developer Productivity
Developer productivity is a major concern when working with microservices as you have to think about how the developers will work with different components of the service. Do they have to download the complete service and configure each and every project?  Along with this testing of the microservices also has to be managed in context with the whole application as well as individually.
  1. Complex Interactions
The communication between different microservices  also needs to be managed as we do not want the communication between different services to be over-complex and time-consuming.
  1. Deployment
Deploying individual microservice is easier and safer when compared to deploying a monolithic application but this process gets complicated when it comes to many microservices. In this scenario, the process has to be automated so that the chances of error should be less.
  1. Monitoring
Monitoring and logging has to be proper for microservices because in case of an error we do not want to debug and check the logs for each and every individual microservice and hence a centralized logging system has to be defined. 

So this is all about a basic introduction to microservices to get a basic idea about it.
In the next post, I will come with a live example of the same.