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

Batch Extract Win10 Lock Screen Wallpapers Using Python

Windows 10 users might notice that every time they boot up, the lock screen displays a different beautiful image. These images are often selected from outstanding photography works and are quite exquisite. However, since the system automatically changes these images, even the most beautiful ones might be replaced the next time you start your computer. With the help of Python, we can use a few simple lines of code to batch extract these beautiful lock screen images. By setting your favorite images as desktop backgrounds, you no longer have to worry about them being replaced. ...

March 26, 2018 · 2 min · Zhiya