Piton

What is Pony ORM and How to Get Started?

What is Pony ORM and How to Get Started?
Pony ORM is a Python programming language directory that enables people to work comfortably with objects kept as tuples in a relational database system. It enables you to deal with the information of the databank, in the form of substances/objects. In the database, there are tables having tuples of data. Conversely, when it is possible to view the data obtained from the databank in object form, it is far more useful when writing the code in an advanced-level object-oriented semantic. If you wish to work with Pony ORM, you have to go through the below-appended steps thoroughly.

Install Python

You need to install the newest version of Python on your system to get started with Pony ORM. Visit the official website of Python and Download it quickly by pressing the yellow button.


Run the downloaded file as an Administrator from the folder and the following window will pop up. Checkmark the checkboxes and click on the blue highlighted Install Now text to continue the installation of Python.


Python will be installed quickly. Close the setup window to proceed further.


Open the command prompt from the search bar and run the below-given command:

>> Python


Try to install the Pony directory using the pip command in the command prompt as:

>> Pip install pony

If you find an error showing that the pip package is not recognized, it means that the environment variables have not been set for the pip package.

Set Environment Variables

In the search bar, type Python. A list of Python-related apps and files will pop up. Right Click on the main Python application and select Open File Location. You will be navigated to the folder where your newly installed Python has been located, as shown below. You can double click on it to check the Python installed version on your system.

Select the Python 3.9 main file, right-click on it, and pick Open File Location. The following new folder will be opened. Click on the path, select and copy the full path, as shown at the top of the image.

Go to your Windows Search bar at the below right corner of the desktop, and type Edit the System Environment Variables. Click on the utility to open it.

The below window named System Properties will be shown. Click on the Environment Variable button.

Below the 'User variables' tab, select Path, and tap the Edit button.

A new window will pop. Tap the New button, paste the copied path, and click on the OK button to continue.

Below the System Variables tab, select Path and tap the Edit button.

Tap the New button, paste the copied path, and tap the Ok button to continue.

Now when you run the python command in the command prompt, you will see the newest version of python and the environment variables you have set for Python successfully.

If you want to set variable paths for pip and pony as well, you will have to copy the location of both and paste it into the path of the 'User Variable' tab. Now you can see pip package is working successfully in the command prompt.

Install Pony ORM

In the command prompt, try the following pip command to install Pony on your system:

>> pip install pony

Make sure to upgrade pip to install all the packages in the Pony directory. Use this command to upgrade:

>> pip install -upgrade pip

Again, trying the installation command for Pony:

>> pip install pony

Now, you have to install some extra packages to work on Pony. First of all, try to install any database you need to work along with Pony. You don't have to configure any other database when you are working with the SQLite database. If you want to try a different database, you should install the matching database driver from the official Pony website. I have installed MySQL using the MySQL driver. Here is the command to install the database:

>> pip install mysql

Import ORM package

Open your Python interpreter to start with Pony ORM. We are using the Visual Studio Code App. Import the ORM package using the Pony directory using any of the statements below:

>> from pony.orm import *
>> from pony import orm

Create Database Object

Entities are added to the database of Pony. That's why we have to first construct a database object. In the editor for Python, type:

>>db = Database()

Define Entities

For our convenience, we have to build two classes. There are two traits of the class Person: name and age, and Car have parameters: make and model, as shown below:

Binding of Database

If you want to bind the stated entities with your database, you have to use the below bind function in your code. In the provider attribute, you can provide any database which you are using. If an in-memory database is established, replace database.sqlite with ':memory:'

For SQLite use:

>> db.bind(provided='dbname', filename='database.sqlite', create_db=True)

For MySQL, PostgreSQL, Oracle, and CockroachDB use:

>> db.bind(provided='dbname', user=", password=", host=", database=")

Map Entities to Database Tables

We have to invoke the generate mapping() function using the db object. The argument create_tables=True means that if the table doesn't exist yet, then the CREATE TABLE instruction can be used to create them.

Turn on the Debug Mode

For this purpose, type the below statement:

>> set_sql_debug(True)

Create Entity Objects

We have to create objects for both classes while passing values to arguments and save the changes using the commit() function.

DB Session

Database communication can be performed inside a database session using Pony within the application. The methods that interact long the database must be packed with the db_session() decorator.

You can use db_session() as context manager rather than the decorator.

Write Queries

Try some queries to get information. You can use the slice-operator [:] and for-loop in your query. Try any one of the commands in interactive mode.

Get Objects

You can get objects using the primary indexes.

Conclusion

Hopefully, you have successfully catered to all the problems while using the Pony ORM using this guide.

Motoare de jocuri gratuite și open source pentru dezvoltarea jocurilor Linux
Acest articol va acoperi o listă de motoare de jocuri gratuite și open source care pot fi utilizate pentru dezvoltarea jocurilor 2D și 3D pe Linux. Ex...
Tutorial Shadow of the Tomb Raider pentru Linux
Shadow of the Tomb Raider este a douăsprezecea completare a seriei Tomb Raider - o franciză de jocuri de acțiune-aventură creată de Eidos Montreal. Jo...
Cum se mărește FPS în Linux?
FPS înseamnă Cadre pe secundă. Sarcina FPS este de a măsura rata de cadre în redările video sau în performanțele jocului. În cuvinte simple, numărul d...