Reversed() – Python built-in Function

The reversed() function takes any sequence as input, and returns a copy of that sequence in reverse order. It is normally used in for loops when we want to loop over items from back to front. Similar to len , reversed calls the __reversed__() function on the class for the parameter. If that method does ...

Len() – Python built-in Function

Len() function counts the number of items in some kind of container object such as a dictionary or list. For example: Python >>> len([3,4,5,6,7,8,9] 7 12 >>> len([3,4,5,6,7,8,9]7 Why don’t these objects have a length property instead of having to call a function on them? Technically, they do. Most objects that len() will apply to ...

Defining our own exceptions Python

Often, when we want to raise an exception, we find that none of the built-in exceptions are quite what we need. Luckily, it’s very easy to define new exceptions of our own. The name of the class is usually designed to communicate what went wrong, and we can provide arbitrary arguments in the initializer to ...

Raising an Exception in Python

What do we do if we’re writing a program that needs to inform the user or a calling function that the inputs are somehow invalid? It would be nice if we could use the same mechanism that Python uses… and we can! Want to see how? Here’s a simple class that adds items to a ...

Main Features of the Flask Framework

The Flask framework is actually a glue, a very nice one, that sticks together the amazing Werkzeug and Jinja2 frameworks, responsible for answering requests and presenting the output (HTML, maybe). In the MVC architecture, also known as Model-View-Controller, Flask covers C and V. But where is M? Flask does not provide you with an integrated ...

Creating a SQLAlchemy DB Instance in Flask

SQLAlchemy is a Python SQL toolkit and provides an ORM that gives the flexibility and power of SQL with the feel of Python’s object-oriented nature. Flask-SQLAlchemy is the extension that provides the SQLAlchemy interface for Flask. This extension can be simply installed using pip as follows: Python $ pip install flask-sqlalchemy 1 $ pip install ...

Bootstrap Layout in Flask

Most of the applications in Flask follow a specific pattern to lay out templates. In this recipe, we will talk about the recommended way of structuring the layout of templates in a Flask application. By default, Flask expects the templates to be placed inside a folder named templates at the application root level. If this ...

Organization of Static Files in Flask

Organizing static files such as JavaScript, stylesheets, images, and so on efficiently is always a matter of concern for all web frameworks. Flask recommends a specific way to organize static files in our application: Python my_app/ - app.py - config.py - __init__.py - static/ - css/ - js/ - images/ - logo.png 123456789 my_app/    - app.py    - ...

Handling Flask Basic Configurations

The first thing that comes to mind is configuring a Flask application as per the need. In this recipe, we will try to understand the different ways in which Flask configurations can be done. In Flask, a configuration is done on an attribute named config of the Flask object. The config attribute is a subclass ...

Getting Started with Flask Framework

Flask is a microframework for Python based on Werkzeug, Jinja2 and good intentions. Why micro? Does it mean that Flask is lacking in functionality or that your complete web application has to mandatorily go inside one file? Not really! It simply refers to the fact that Flask aims at keeping the core of the framework ...