Category: Code
-
How to assert 2 simple functions return the same value in Python?
To test that two functions return the same value, you can use the assert statement in Python. Here’s an example of how to do it: def function1(): return 5 def function2(): return 5 assert function1() == function2() In this example, we have two functions function1 and function2 that both return the integer value 5. We…
-
Python dict.get() vs dict[‘brackets’], which approach is better?
Using the .get() method on a dictionary is generally considered a better approach than using the direct dictionary indexing dictionary[‘key’] method because the .get() method provides a default value if the key is not found in the dictionary, whereas direct dictionary indexing raises a KeyError exception. Here is an example: my_dict = {‘a’: 1, ‘b’:…
-
Why its better to use .exists() method on query rather then do try-except block?
Both of the provided code samples achieve the same result, but they use different approaches to handle the case when the instance with the given primary key does not exist. class Balance(models.Model): current_balance = models.DecimalField(_(“Current Balance”), max_digits=12, decimal_places=2, default=0.00) last_balance = models.DecimalField(_(“Last Balance”), max_digits=12, decimal_places=2, null=True) def save(self, *args, **kwargs): if self.pk: try: prev_state =…
-
Where should I store database queries in Django?
The Django business logic dilemma? In general, it’s a good practice to keep your code organized and modular. If your view code includes complex database queries or large amounts of database-related logic, it might be a good idea to move those queries to a separate file, such as a Django model or manager. Here are…
-
How to check if django-tables2 table is empty or not in templates code?
In Django-Tables2, you can check if a table is empty or not by checking the length of the table’s queryset. If the length is zero, then the table is empty. You can do this in your template code using the if statement and the table.queryset attribute. Here’s an example of how to do this in…
-
Django Rest Framework Tips and Tricks
In this Django rest framework tips article, I will collect interesting and useful tips that can be used while you code your API with Django. I do also have a Django framework tips and tricks article, don’t forget to check. How to restrict access to Django Rest Framework API for specific users only? I will…
-
How to route internal IP to external IP traffic in Google Virtual Machine?
As you may know, the google App engine Standard doesn’t have the possibility to use a static IP address, but it’s possible to set the static IP address by attaching the proxy/VPN to it. The easiest and most realistic way would be to create a separate Google Cloud Virtual Machine and force it to work…
-
Django tips and tricks
When to use gettext() and gettext_lazy() Functions for TranslationAll of these snippets, Django tips and tricks have been tested on real-world Django applications by me. I’m using each of them in my SaaS projects. I wrote this article in a specific manner, less text – more code – more solutions! If you have any issue/error…
-
Automatic WordPress analytics reports to the Twitter bot
Do you want to share your WordPress blog analytics with Twitter followers automatically and get users engaged? Now you can do it with “Automatic WordPress analytics reports to the Twitter bot”. I will show you how easily it can be done with the powers of Python programming language, Selenium library, GeckoDriver, BeautifulSoup parser and Tweepy.…
-
Create a GitHub bot for automatic contributions
Have you ever wondered how non-tech guys have such impressive contribution stats on their Github profiles? Yes, they have set up a GitHub bot that uploads the same file over and over again to the GitHub repo. This piece of content is a part of my “automation” manifesto, which I declare here: I would automate everything…