Generators in Python!

Python generators code tutorial: import string import inspect # Informal tutorial on the generators. We will cover very briefly: ## 1) What are the generators (PEP 255). ## 2) What is yield (PEP268). # Generator in Python is a fancy object that maintain its state between calls. # In simple words generator knows what to […]

UUID3 cross compatible in Java and Python

Sometimes, in the world of microservices you want to generate consistent hashes across several languages. I’ve faced a problem of getting the same UUID in Java and Python, and wanted to share how it can be done. Java UUID.nameUUIDFromBytes(“name”.getBytes()) # b068931c-c450-342b-a3f5-b3d276ea4297 Python class NULL_NAMESPACE: bytes = b” uid.uuid3(NULL_NAMESPACE, “name”) # b068931c-c450-342b-a3f5-b3d276ea4297 Enjoy!

PyMongo insert_many with overwrite

I’ve recently needed to insert lots of objects into the Mongo collection. The only problem was that some of the objects would have an ‘_id’ key pre-set and would conflict with existing objects in the database. PyMongo inser_many operation doesn’t support it out of the box, so here is a work-around using bulk api: try: […]