Sam  Son

Sam Son

1575514926

Clean Code in Python

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.!

#Python #tutorial #code #developer

What is GEEK

Buddha Community

Clean Code in Python
Ray  Patel

Ray Patel

1619518440

top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel

1619510796

Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Shardul Bhatt

Shardul Bhatt

1626775355

Why use Python for Software Development

No programming language is pretty much as diverse as Python. It enables building cutting edge applications effortlessly. Developers are as yet investigating the full capability of end-to-end Python development services in various areas. 

By areas, we mean FinTech, HealthTech, InsureTech, Cybersecurity, and that's just the beginning. These are New Economy areas, and Python has the ability to serve every one of them. The vast majority of them require massive computational abilities. Python's code is dynamic and powerful - equipped for taking care of the heavy traffic and substantial algorithmic capacities. 

Programming advancement is multidimensional today. Endeavor programming requires an intelligent application with AI and ML capacities. Shopper based applications require information examination to convey a superior client experience. Netflix, Trello, and Amazon are genuine instances of such applications. Python assists with building them effortlessly. 

5 Reasons to Utilize Python for Programming Web Apps 

Python can do such numerous things that developers can't discover enough reasons to admire it. Python application development isn't restricted to web and enterprise applications. It is exceptionally adaptable and superb for a wide range of uses.

Robust frameworks 

Python is known for its tools and frameworks. There's a structure for everything. Django is helpful for building web applications, venture applications, logical applications, and mathematical processing. Flask is another web improvement framework with no conditions. 

Web2Py, CherryPy, and Falcon offer incredible capabilities to customize Python development services. A large portion of them are open-source frameworks that allow quick turn of events. 

Simple to read and compose 

Python has an improved sentence structure - one that is like the English language. New engineers for Python can undoubtedly understand where they stand in the development process. The simplicity of composing allows quick application building. 

The motivation behind building Python, as said by its maker Guido Van Rossum, was to empower even beginner engineers to comprehend the programming language. The simple coding likewise permits developers to roll out speedy improvements without getting confused by pointless subtleties. 

Utilized by the best 

Alright - Python isn't simply one more programming language. It should have something, which is the reason the business giants use it. Furthermore, that too for different purposes. Developers at Google use Python to assemble framework organization systems, parallel information pusher, code audit, testing and QA, and substantially more. Netflix utilizes Python web development services for its recommendation algorithm and media player. 

Massive community support 

Python has a steadily developing community that offers enormous help. From amateurs to specialists, there's everybody. There are a lot of instructional exercises, documentation, and guides accessible for Python web development solutions. 

Today, numerous universities start with Python, adding to the quantity of individuals in the community. Frequently, Python designers team up on various tasks and help each other with algorithmic, utilitarian, and application critical thinking. 

Progressive applications 

Python is the greatest supporter of data science, Machine Learning, and Artificial Intelligence at any enterprise software development company. Its utilization cases in cutting edge applications are the most compelling motivation for its prosperity. Python is the second most well known tool after R for data analytics.

The simplicity of getting sorted out, overseeing, and visualizing information through unique libraries makes it ideal for data based applications. TensorFlow for neural networks and OpenCV for computer vision are two of Python's most well known use cases for Machine learning applications.

Summary

Thinking about the advances in programming and innovation, Python is a YES for an assorted scope of utilizations. Game development, web application development services, GUI advancement, ML and AI improvement, Enterprise and customer applications - every one of them uses Python to its full potential. 

The disadvantages of Python web improvement arrangements are regularly disregarded by developers and organizations because of the advantages it gives. They focus on quality over speed and performance over blunders. That is the reason it's a good idea to utilize Python for building the applications of the future.

#python development services #python development company #python app development #python development #python in web development #python software development

Ray  Patel

Ray Patel

1619533440

Using Black To Auto Format Your Python

Writing python code is one thing and writing the code in a good format is another thing. Most of the time especially junior coders/programmers focus on making sure the codes are working and forget to format their codes.

If you write a small program (with 1000 lines of codes) you could get away without formatting your code, but as programs get more and more complex, they get harder and harder to understand and at some point (around 15,000 lines of code), it becomes harder to understand the code that you yourself wrote.

The difference between working on well-formatted code and working on badly formatted code is like the difference between living in a palace and living in a dirty house.

Why formatting your python code is important?

#programming #python #python-programming #python-tutorials #python-tips #python-developers #code-quality #clean-code

Ray  Patel

Ray Patel

1623077340

50+ Basic Python Code Examples

List, strings, score calculation and more…

1. How to print “Hello World” on Python?

2. How to print “Hello + Username” with the user’s name on Python?

3. How to add 2 numbers entered on Python?

4. How to find the Average of 2 Entered Numbers on Python?

5. How to calculate the Entered Visa and Final Grade Average on Python?

6. How to find the Average of 3 Written Grades entered on Python?

7. How to show the Class Pass Status (PASSED — FAILED) of the Student whose Written Average Has Been Entered on Python?

8. How to find out if the entered number is odd or even on Python?

9. How to find out if the entered number is Positive, Negative, or 0 on Python?

#programming #python #coding #50+ basic python code examples #python programming examples #python code