Despite the temptation of buying new servers and adding CPU and RAM – you need to do what you can with the code first. I am exploring and cataloguing these methods to speed up Django QuerySets before server scaling.
The general idea is simple – use more QuerySet methods instead of Python methods to avoid evaluation.
I needed to schedule 10 tasks that have these conditions
A time gap between tasks is fixed (but varies)
No need to pass results of one task to another
Need to read a database in every task (important: at the moment of the execution)
I tried a few options, and this one worked
ret = chain(task1.si(args).set(countdown=30*1), ..., task10.si(args).set(countdown=30*10)).apply_sync()
Important note – the signature here is not .s() but .si() this way you don’t pass the result of task1 to task2. You set the countdown with set() and not inside the apply_sync().
It is simple but it took me some time to figure out.
I covered the basics in one of my articles on Video generation with Python, and today I want to write about different ways to assemble text on the image so it does not look too bland.
The world is crazy, but I don’t let it deter me. I am a great believer in doing things during challenging times. So my brother and I are spending our spare (from work) time at Codewars and Hackerrank.
HackerRank
First, I returned to HackerRank to do some SQL, and I completed everything I found relevant to my current studies. It seems like from the lack of use I lost some of my SQL knowledge and I want to fix that.
My modest achievements at HackerRank
I plan to do much more with Problem Solving (there are a few data structures and algorithms I am not yet very familiar with). Maybe I will return and do more of Python, but at this point I just feel bored and uninspired.
SQL Academy
Then I jumped to SQL Academy, and I loved it, but it has too many bugs to my taste
UPD. 24.03.22 – I am a moron. I don’t see bugs, I just missed some details and therefore couldn’t successfully submit a few tasks. Now I bought a Premium account for $10 and realized my mistakes. I use it after I complete the tasks to see how a DB expert would solve it. Can’t wait to finish the task list by end of the week (wish me luck hehe).
One downside for me – it requires MySQL syntax, and I am not really interested in it. I personally find PostgreSQL more accomodating to Python users and those who don’t specialize solely in databases too.
The interface is fantastic and I love how you can see queries and tables and an ERM. I will be completing everything that bugs will let me – it has been very useful to me.
Codewars
Meanwhile my brother has been devoured by Codewars, so after all these websites I returned to it. I have a rather old account there (since 2018), but I haven’t touched it in a while. I have had 5 kyu since forever, now I really want to get 4 kyu, even though it is not just in Python hehe.
My modest achievements at Codewars
In February I upgraded my DS/Algorithms skills with Udemy courses, Yandex Youtube lectures and personal studies here and there. I am excited to return to Problem Solving katas at Codewars with all this acquired knowledge.
Summing Up
All in all, right now I am very much into coding simulators. They bring me a sense of accomplishment that I desperately need right now. Badges and certificates – small achievements you can touch. That brings comfort if anything. So I encourage everybody I know who struggles with reality right now – to do the same. At very least, you came out of it more educated and with brighter prospects.
import os
from pathlib import Path
from git import Repo
# path to repo folder
path = Path(os.path.dirname(os.path.abspath(__file__)))
# repo variable
repo = Repo(path)
# Your last commit of the current branch
commit_feature = repo.head.commit.tree
# Your last commit of the dev branch
commit_origin_dev = repo.commit("origin/dev")
new_files = []
deleted_files = []
modified_files = []
# Comparing
diff_index = commit_origin_dev.diff(commit_feature)
# Collection all new files
for file in diff_index.iter_change_type('A'):
new_files.append(file)
# Collection all deleted files
for file in diff_index.iter_change_type('D'):
deleted_files.append(file)
# Collection all modified files
for file in diff_index.iter_change_type('M'):
modified_files.append(file)
Analyze:
Let’s imagine the situation:
You are before code review and you need to get all changes between feature and develop with GitPython. First you need to get last commits from two branches:
# Your last commit of the current branch
commit_dev = repo.head.commit.tree
# Your last commit of the dev branch
commit_origin_dev = repo.commit("origin/dev")
My previous version has flaws. It works well for opaque GIFs, but OpenCV is famous for not being good with transparent images. So instead of OpenCV I use imageio for GIF iteration and PIL for adding GIF frames to the static background.
Why do more?
The original algorithm left these artifacts, look at the cyan block on the intersection of GIF frame and a darker abstract part:
Initially, I aimed to make one big post, but I am splitting it up. This Part 1 is about how I made the frames to generate videos from.
Intro
I have been on and off doing this project for a few months. Many things did not work for me – from ffmpeg to sheer OpenCV/ffmpeg installation on Apple M1 systems – and I am not 100% sure what made it work to this day. I plan to put myself in a situation where I have to do it again, and I will make a guide then. However, right now, I can only say that this video tutorial on running native ffmpeg and this guide from OpenCV themselves are good places to start.
The last time I used OpenCV was in 2017 when I worked on my graduate work. Back then, I processed prerecorded videos with no need to worry about audio. So I had some background prior to this project, but I was in no way ready for the ride.
Following Channels’ docs I encountered yet another error the solution to what was not obvious right off the bat.
Ditch Channels.
The End.
This was my original post in October 2020 and I stand by it.
You can make your project work with Google Firebase - it handles signals well enough. Yes it is primarily used by mobile developers, but overall it is fine for web too.
I spent trying to install Django Channels on my local Linux 18.04, and I kind of made it work, but then I had to deploy it... And as a professional backender you don't want to let people down.
I decided to try Channels because my Python-related friends talked about it. But days into the trouble I asked my friend about a certain step and he asked me in return "oh so [you use it because] they fixed Channels since two years ago?". I as like ??????????????????. So that moment I ditched it. Too much time wasted, but I like the result I achieved with Firebase.
What I am saying - Firebase is fast and universal. In a matter of days I managed to make and deploy an internal Customer Support chat service that connected mobile app users and a support from the web.</p>
I will be writing a new series on Django + Firebase, and this post is pretty much a short introduction.