Merging Dictionaries

There are many ways to merge dictionaries in Python. Python 3.9 introduced a new syntax for merging dictionaries. You can now merge dictionaries by using the merge operator |. From all the possible options, using the merge operator is probably the fastest and cleanest way to achieve this goal. 1 2 3 4 5 6 7 d1 = {'name': 'Tom', 'age': 23} d2 = {'country': 'France', 'city': 'Paris'} details = d1 | d2 print(details) # {'name': 'Tom', 'age': 23, 'country': 'France', 'city': 'Paris'} If you don’t want to reassign the dictionaries, you can use |= operator to get the second dictionary to merge into the first one. ...

December 27, 2022 · 2 min · 234 words

Using Redis for better performance

Sometimes your application runs multiple queries that can get very expensive in terms of performance. There are many tools and options when it comes to performance-optimization, and one of them is called caching. Caching refers to the process of storing data in a temporary location. When a request is made, system will first check this cached file to check if the data is available. It is very useful when the data is frequently accessed and/or the database is under high load. ...

December 27, 2022 · 2 min · 350 words

Django Best Practices - Meta class

In Django, properly defining database models is arguably the most important part of a new project. It is however very flexible in how we structure our models. Official Django coding style recommends following order: choices database fields custom manager attributes Meta def __str__() def save() def get_absolute_url() custom methods Today I want to look at some good practices I have learned so far regarding the Meta class. Explicitly name your model, not just your fields. For this, you would use verbose_name and verbose_name_plural (Django would just add an s to make it plural, citys, which is not correct) 1 2 3 class Meta: verbose_name = 'city' verbose_name_plural = 'cities' ordering is also very common - it defines the default order of a list of objects - however, ordering can be a performance hit, so as Django’s documentation says - “Don’t order results if you don’t care” If you do care, and want to use ordering, it is also a good idea to use indexing The Meta class is very powerful and there is of course a lot more, but these are just some options I have used recently. ...

December 22, 2022 · 1 min · 188 words

Setting up Index in your Models

When setting up models in Django, it is recommended to set an index for the specific fields of that Model. Index is a performance-enhancing feature, that allows a database to quickly locate and retrieve specific data from a table. It makes searching/sorting data in the database easier and more efficient. Here are some reasons why you might want to set an index on a field in a Django model: To improve the performance of queries that filter or sort data based on the indexed field. To enforce uniqueness constraints on the field. To improve the performance of foreign key relationships by creating a link between the primary key of one table and the indexed field of another table. Index class is located in the Meta inner class of your model. ...

December 21, 2022 · 2 min · 249 words

Better way to handle Strings

Python 3.6 introduced a new way of formatting Strings called f-Strings. It’s very practical and I use it all the time. Before f-Strings it was necessary to concatenate the strings which lead to longer and harder to read code. 1 2 def sayHi(name): print(f'Hi {name}!') What’s even better is that we can write expressions in the braces {} which are evaluated at run-time. ...

December 20, 2022 · 1 min · 174 words

Complex database queries

Django comes with excellent ORM - Object Relational Mapper for querying the database. It’s very good for most cases, but sometimes you need to handle more complex queries. When you need to run complex queries, Django offers the Q Object. When you need to retrieve something from the database you would use .filter() method. 1 posts = Post.objects.filter(title__startswith="How to") This gives you all the posts whose title starts with “How to..”. What if you want to further filter the posts by another condition? That’s when Q Object comes in. ...

December 19, 2022 · 1 min · 195 words

Automating my Anki workflow

You can find the app and code here on GitHub. It’s still far from perfect and I am working on improvements and more features. What is Anki Effective learning is a huge topic in itself. There are many methods and approaches. When it comes to memorizing things, spaced repetition is a very popular and effective method. Memorization of some kind is always necessary, but it really depends on what exactly are you learning. ...

December 18, 2022 · 5 min · 998 words

Get count of each item in the List

Sometimes you need the count of each item in the List. Python comes with a handy tool in the collections module exactly for this task. First you have to import the Counter. 1 from collections import Counter And then create the Counter object with the list as argument. 1 2 numbers = [8, 8, 8, 1, 1, 9, 4, 4, 4, 4, 4, 4, 4] counter = Counter(numbers) You can use counter in various ways. ...

December 18, 2022 · 1 min · 197 words

Writing maintainable code

Programming language can be either statically typed or dynamically typed. Languages that use static typing require programmer to explicitly define a data type of a variable. Python is a dynamically typed language, meaning data type doesn’t need to be explicitly defined and it can even change during runtime. Just like with most of things, there are negatives and positives to both. Dynamic typing might be easier and faster, but it’s a lot easier to cause bugs in your program. ...

December 17, 2022 · 2 min · 288 words

Useful methods when working with dictionary

A Dictionary is a very useful and practical data structure. Dictionaries store data values in key-value pairs. Dictionary is a collection which is ordered (as of Python 3.7), changeable and does not allow duplicates. Python’s dictionary comes with many methods that make working with them easier, however as data gets more complex using them can also become challenging. Let’s say you want to get a key that is not part of your dictionary. ...

December 16, 2022 · 2 min · 242 words