Do You Really Know How to Use Assertions Correctly?

What is an Assertion Assertions were invented as a debugging tool to check conditions that “should always be true if the code is correct.” For example, if we want to assert that a variable a must be greater than 2, we can write: assert a > 2 When the condition is not met, an AssertionError exception is raised, equivalent to the following code: if not assert_condition: raise AssertionError Since assertions are a debugging tool, Python’s implementation aligns with this philosophy. In Python, the execution of the assert statement depends on the __debug__ variable. The assert statement will only be executed if __debug__ is true. ...

May 7, 2018 · 3 min · Zhiya

Encapsulating Flask-WTF Form Validation Logic with Decorators

Don’t repeat yourself When using Flask-WTF, you often use the following code to validate the legitimacy of form data: from flask import Flask app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def index(): form = TestForm() # Check if valid if not form.validate_on_submit(): return 'err', 400 # Main logic For projects with many submission interfaces, you need to write the same logic under each route, resulting in a lot of code duplication. In Flask-Login, to set a route to be accessible only after logging in, you just need to add a @login_required decorator to the route without any extra code. Can we encapsulate the form validation logic with a decorator like Flask-Login? ...

April 26, 2018 · 3 min · Zhiya

Python Parameter Passing: Neither Pass-by-Value Nor Pass-by-Reference

Have you ever been asked in an interview whether Python passes parameters by reference or by value? Have you heard the notion that Python parameter passing is neither pass-by-value nor pass-by-reference? Even a small default parameter value can sometimes cause bugs that are hard to trace? If you’ve encountered any of these issues, let’s delve into the various aspects of Python function parameter passing. Everything is an Object In Python, there’s a crucial concept: everything is an object. Whether it’s a number, a string, an array, or a dictionary, in Python, they all exist as objects. ...

April 22, 2018 · 4 min · Zhiya

Practical Guide | Implementing a High-Performance Crawler with aiohttp and uvloop

asyncio was introduced in Python 3.4 as part of the standard library, adding support for asynchronous I/O. Based on an event loop, asyncio allows for easy implementation of asynchronous I/O operations. Next, we’ll use libraries based on asyncio to create a high-performance crawler. Preparation Earth View from Google Earth is a Chrome extension that automatically loads a background image from Google Earth when a new tab is opened. ...

April 10, 2018 · 3 min · Zhiya

Chatting with the Interviewer | Python Object-Oriented Access Control

Python was designed as an object-oriented language from the start, and the first element of object-oriented thinking is encapsulation. Simply put, encapsulation means that the attributes and methods within a class are divided into public and private; public ones can be accessed externally, while private ones cannot. This is the most crucial concept in encapsulation—access control. There are three levels of access control: Private, Protected, and Public. Private: Accessible only within the class itself. Protected: Accessible within the class itself and its subclasses. Public: Accessible by any class. ...

March 30, 2018 · 4 min · Zhiya