Skip to content

Instantly share code, notes, and snippets.

View anand2312's full-sized avatar

Anand anand2312

View GitHub Profile
@anand2312
anand2312 / pymongo-to-motor.md
Last active May 24, 2024 09:15
pymongo vs Motor

pymongo vs Motor

Motor is an async Python driver for MongoDB.

When should I use Motor?

You should use Motor when you're trying to interact with a MongoDB database in an asynchronous context. When you're making something that needs to be asynchronous (like a web server, or most commonly from what I've seen here, Discord bots), you also want all the database calls to be done asynchronously. But pymongo is synchronous, i.e it is blocking, and will block the execution of your asynchronous program for the time that it is talking to the database.

Okay, How do I switch now?!

Thankfully for us, switching from pymongo to Motor isn't too hard, and won't need you to change much code. This process can be roughly summarized as:

Step 1: Install Motor, and import it

Installing can be done with pip - pip install motor

@anand2312
anand2312 / keyerror.md
Created February 5, 2021 14:04
Understanding KeyErrors

Often while using dictionaries in Python, you may come across an error which looks something like this:

001 | Traceback (most recent call last):
002 |   File "<string>", line 2, in <module>
003 | KeyError: 3

This error is raised when you try to access a key that isn't present in your dictionary.

How to handle the KeyError.

Option 1: dict.get