Clean Code in Python

Clean Code in Python

In this post, I'm to put together a clean code checklist which covers my personal recommendations for writing clean and production-ready Python code

Traits of clean code

Our code is the most detailed representation of the design.

  • Our ulitmate goal is to make the code as robust as possible
  • And, to write it in a way that minimizes defects or makes them utterly evident, should they occur.

Maintainability of code, reducting technical debt, working effectivley in agile development, and managing a successful project.

Code Structure Principles and Error Handling

Code Structure Principles

Dividing Code by Responsibilities

Some parts of software a meant to be called directly by users and some by other parts of the code. We divide the responsibilities of the applciation into different components or layers, and we have to think about the interaction between them.

We will have to encapsulate some functionality behind each components, and expose an interface to clients who are going to use that functionality, name an Application Programmable Interface (API). When designing an API we document expected input, output, and side-effects should be documented.

Each component is enforicing its own contraints and maintaining some invariants, and the program can be proven correct as long as these invariants are preserved.

Defensive Programming

The main idea here is how are we going to handle errors for scenarios that we might expect to occur, and how to deal with errors that should never occur.

Separation of concerns

This is relevant to low-level design and also higher level of abstraction. The goal of separating concerns in software is to enhance maintainability by minimizing ripple effects. A riple effect means the propogation of a change in the software from a starting point. This could be case of an error or exception triggering a chain of other exceptions, causing failures that will result in a defect on a remote part of the application.

Achieve High Cohesion and Low Coupling

  • Cohesion: objects should have small and well defined purpose, and they should do as little as possible. It should do only on e thing and do it well.
  • Coupling: How two or more objects depends on each other. If two parts of the code are too dependent on each other, they bring with them some some undesired consequences.

EAFP/LBYL

  • EAFP: Easier to Ask Forgiveness than Permission.
  • LBYL: Look Before You Leap

Error handling

The idea behind error handling is to gracefully respond to these expected errors in an attempt to either continue our program execution or decide to fail if the error turns out to be insurmountable.

There are different apporaches by which we can handle errors in our programs. Some of the approaches are

Value Substitution: Replacing erroneous result for a value that is to be considered non-disruptive (e.g. default value). Exception Handling: Stop the program from continuing to run with wrong data, or failing and notifying the caller that something is wrong and this is the case for a precondition that was violoated.

Tips for Error Handling

`Do not use exceptions as a go-to* mechanism for business logic. Raise exceptions when there is actually something wrong with the code that caller need to be aware of**.

Principle 1

Exceptions should be used carefully because they weaken encapsulation. The more exceptions a function has, the more the caller function will have to anticipate, therefore knowning about the function it is calling. And if a function raises too many exceptions, this means that is not so context-free, because every time we want to invoke it, we will have to keep all of its possible side-effects in mind.

Principle 2

For security considerations, do not expose tracebacks to users.

Principle 3

Avoid empty except blocks.

try:
  process_data()
except: # do not do this
  pass

Instead,

  1. Catch a more specific exception. Pylint warns if broad Exceptions are used
  2. Do some actual error handling on the except block

Include the origin exceptions

As part of error logic, we might decide to raise a different one. In such a case include the original exception.

class SomeDataError(Exception):
  """Some data error exception as part of your domain"""

def process(dictionary, row_id):
  try:
    return dictionary[row_id]
  except KeyError as e:
    raise SomeDataError("Record read error") from e

Use assertions

Assertions are to be used for situations that should never happen or “should this condition happen, it means there is a defect in the software”.

result = 11
assert result == 10, "Excepting 10 items, instead got {0}".format(result)

Creating consistency in code

Documenting Code

Docstrings

Docstrings are not comments; they are documentation. If we are explaining why or how we are doing something, then code is probably not good enough. Some exceptions where we cannot avoid comments is when a third-party libarary has an error we are having to circument them. In those cases, placing a small but descriptive comment might be acceptable.

Good documentation provides crucial information for someone who has to learn and understand how a new function works, and how they can take advantage of it.

Practices

Indexes and Slices

Using a negative index number, will start counting from the last

>>> mylist = ["Apple", "Bannana", "Carrots", "Dragon Fruit"]
>>> mylist [-1]
'Dragon Fruit'
>>> mylist[-2]
'Carrots'

Example for obtaining many by using a slice

>>> mylist = ["Apple", "Bannana", "Carrots", "Dragon Fruit"]
>>> mylist [1:3]
['Bannana', 'Carrots']

Creating your own Sequence

A sequence is an object that implments the getitem and _len** menthods. This enables the iteration. Lists, tuples, and strings are examples of sequence objects in the standard libarary.

In case your class is wrapping standard python library object, delegate the behaviour as much as possible to the underlying object. This means that if your class is a wrapper on the list, call all the same methods on that list to make sure that it remains compatbile.

An example of encapsulation

class Items:

  def __init__(self, *values*):
    self._values = list(values)_

  def __len__(self):
    return len(self._values)

  def __getitem__(self, item):
    return self.__values.__getitem__(item)

When indexing by a range, the result should be an instance of the same type of the class

When to inherit?

///TODO

When to encapsule? ///TODO

Context Managers

Context Managers provide a pattern where we want to run some code, and has preconditions and postconditions, meaning that we want to run things before and after a certain main action.

For examples

fd=open(filename)
try:
  process_file(rd)
finally: 
  fd.close()

An elegant alternative is

with open(filename) as fd:
  process_file(fd)

The context managers consists of two methods: enter and exit. On the first line of the context manager, the with statement will call the first method, enter, and whater this method returns will be assigned to the variable after as*.

Writing your own Context Manager examples

def stop_db_service():
  print("systemctrl stop service cassandra")

def start_db_service():
  print("systemctrl start service cassandra")

class DbHandler:
  def __exit__(self):
    stop_database()
    return self

  def __enter__(self):
    start_database()

def db_print_keyspaces():
  print_desc_keyspace("localhost:6042")

def main():
  with DbHander():
    db_print_keyspaces

As a general rule, always return something on the enter. The return value of exit is something to consider. Normally, we would want to leave the method as it is, without returning anything in particular.

Another alternative is to use the contextlib module. An equivalent code to the above example is

import contextlib

@contextlib.contextmanager
def db_handler():
  stop_database()
  yield
  start_database()

with db_hander():
  db_backup()

Everything before the yield statement will be run as if it were part of the enter method.

Underscores in Python

In Ptyhon all default attributes of an object are public. Objects should only expose those attributes and methods that are relevant to an external caller object. Everything that is not strictly part of an object’s interfact should be kept prefixed with a single underscope.

Double underscores are non-Pythonic approach. If you need to define attributes as private, use a single underscope, and respect the Pythonic convention that it is a private attribute.

Creating iterable objects

When we try to iterate an object, Python will call the iter() function over it.

class DateRangeContainerIterable:
  def __init__(self, start_date, end_date):
    self.start_date = start_date
    self.end_date = end_date

  def __iter__(self):
    current_date = self.start_date
    while current_date < self.end_date:
      yield current_date
      current_date += timedelta(days=1)

Then this can be used assigned

r1 = DateRangeContainerIterable(date(2019, 1, 1), date(2019, 1, 20))
print(", ".join(map(str, r1)))

This iterable object will use less memory, however it takes up to O(n) to get an element.

Callable Objects

It is possbile to define objects that can act as functions. One of the most common applications for this is to create better decorators. The magic method call will be called when we try to execute our object as if it were a regular function. Every argument passed to it will be passed along to the call method. This method is useful when we want to create callable objects that will work as parameterized functions, or in some case functions with memory.

from collections import defaultdict

class CallCount:
  def __init__(self):
    self._counts = defaultdict(int)

  def __call__(self):
    self._counts[argument] += 1
    return self._counts[argument]_

Now

>>> cc = callCount()
>>> cc(1)
1
>>> cc(2)
1
>>> cc(1)
2

Good Tools
  • MyPy is a tool for optional static type checking in Python. It analyzes all of the files in your project, checking for inconsistencies on the use of the types. This aids in detecting actual bugs early, but sometimes it can give false positives.

    $ pip install mypy
    
    

To ignore a false positive, use the following marker as a comment:

some_result = "this is a result" **#type: ignore**

  • Pylint Tool for checking the structure of code (essentially, PEP-8 compliance).

    $ pip install pylint
    
    

Reference Makefile

typehint:
mypy src/ tests/

test:
pytest tests/

lint:
pylint src/ tests/

checklist: lint typehint test

.PHONY: typehint test lint checklist

With this we can run the following command

make checklist

Thank for reading ! If you enjoyed this post, please share it with others who may enjoy it as well.!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Top Python Development Companies | Hire Python Developers

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more:

Is Low-code or no-code development is future of mobile app development

Is Low-code or no-code development is future of mobile app development

Mobile app development has skyrocketed over these years with the increasing demand of mobile apps for a variety of purposes like entertainment, banking, weather update, health, booking movie tickets, booking a taxi etc. With the latest...

Mobile app development has skyrocketed over these years with the increasing demand of mobile apps for a variety of purposes like entertainment, banking, weather update, health, booking movie tickets, booking a taxi etc. With the latest technologies adopted by mobile app development services, there are different app development approaches which are being practiced. Among them is low-code or no-code development. But will it be the future of mobile app development? Will any mobile application development company start taking this approach as the primary one. Let’s try to find a detailed answer to this question.

But first, let’s understand what this approach exactly is? Well, it is a streamlined approach which involves swift design as well as development with minimal coding, generally relying on different third-party APIs.

Even though there isn’t any single definition of no-code or low-code development because it is actually more of a mindset rather than something which can be directly measured, this mindset has certainly led to a vast community mushrooming up with this mentality. Android app development services are rapidly adopted by this approach. Low-code app innovators are rapidly disrupting all types of various industries. There are a plethora of benefits to these low code platforms and let’s look at this.

1. Less Number of Bugs

It is pretty evident that less code actually means fewer bugs. As simple as that. The entire bug testing phase is actually a major part of modern and latest application development. It is quite inevitable that various issues will come up if there is actually enough code present there. But the best thing regarding low code platforms is that there’s certainly less to test. Also, when they actually tap into APIs, those particular APIs are actually tested by other people.

Lesser number of bugs is better for both users, as well as developers since less amount of time, will be taken up with bug-fixes and troubleshooting. Also, the fast pace of this development approach actually means that if in any case a bug is found, it is generally better just to develop a new iteration rather than fixing it.

2. Significant Lower Costs

Among the most obvious reasons for why you would actually opt for any low code platform is that in reality, low code results in lower cost. Low code development leads to faster app development which saves a lot of time and as a result, lower cost.

It's not only good for companies but also for developers. It certainly cut out the intermediaries, and while they charge less, they use fewer resources and finally come out on top. It is fun for developers because it stops them from actually finding themselves stuck on one particular project which seems to last forever. This is why most of the companies hire app developer who is a well-versed with low-code development.

3. Better Accessibility

The lesser amount of code an application uses, the lesser bandwidth is needed to download it as well as run it. This is quite good news for the people who are based in rural areas or in different developing countries where access to the internet isn’t as prevalent as Western countries. Also, as low code applications can easily be created quite easily than a traditional app, they can easily be released much more swiftly and at a much lower price, and sometimes for free. iPhone app development services follow this approach because it will help in increasing the uptake of their apps as it reduces the entry barrier for every person from lower-income families.

Innovative Development Approach

Among the most promising instances of a low-code or no-code platform is Uber. The apps tap into Google for Maps, Dropbox for storage, Braintree for payments and much more. The most interesting thing about this is that app programming interfaces of APIs which Uber actually relies upon are easily available to anyone who wishes to use them. Uber took those APIs and then used them to create, which was new without requiring to develop each of those particular individual elements, all by themselves. They developed their own brand on top of it by means of looking at how they could actually differentiate themselves from the rest of the others. Mobile app development services should follow this example to create their own low code mobile app which disrupts the market.

The best thing about this is that it inspires innovation. At present, the marketplace actually decides, and only the best applications rise to the top. Also, low code development easily allows developers to iterate much more quickly and can aim for higher more.

The Role of Artificial Intelligence (AI)

Artificial Intelligence is certainly making big waves in different businesses, and as this technology improves further, it will find its way in different other uncharted areas. Among those areas is the low code app development, where it comes in quite useful for a wide range of tasks and actions including the integration of various data sources or just making sense of an entire unstructured or semi-structured data.

Artificial Intelligence is quite great at analysing and conducting trial and error. Hence, it won’t be quite long until the usage of AI becomes quite a standard part of the low code app development. A mobile application development company can find ways to reduce the total amount of code that it is using through AI and flagging potential and possible improvements.

Conclusion

It is quite certain that low-code or no-code app development is the future of app development for one of the simplest reasons that it is quite easier as well as faster and efficiently uses time. It actually doesn’t matter whether Android app development services or iPhone app development services are on-board with this particular change or not. This is quite inevitable as this approach is the path of least resistance. Also, as the inherent demand for low code platforms keeps in growing, developers will certainly find themselves to adopt it.

It is quite a great news as it will push the developers of mobile application development services to become the best. Also, there won’t be any time to redoing it to create the same thing, or any room for sloppy code as well as lengthy development processes which makes the mobile apps redundant even before they are actually finished. Hence, low-code or no-code development will certainly lead the future of mobile app development, and mobile app developers certainly have to get on the bandwagon, one way or the other.