TIL #100 – Making an iterator out of a function
Mathspp Blog » Json
by Rodrigo Girão Serrão
1w ago
You can use the Python built-in function iter with two arguments to create an iterator from a function. Making an iterator out of a function Today I (re)learned that the built-in function iter has a variant with two arguments that lets you turn any 0-argument function into an iterator. The version iter(function, value) will create an iterator that returns the successive return values of calling function until one of them matches value, at which point the iterator is done. For example, using input, we can create a loop that runs while the user is typing input. When the user types “exit”, the l ..read more
Visit website
Breaking out of nested loops with generators
Mathspp Blog » Json
by Rodrigo Girão Serrão
2w ago
You can use generators to simplify nested loops and make it easier to break out of them. Breaking out of nested loops with generators The keyword break is used to break out of the enclosing loop. So, if you have a nested loop like the one shown below, how can you break out of the two loops as soon as you find the number 3? def this_is_the_one(x): return x == 3 my_list = [[1, 2], [3, 4], [5, 6]] for sublist in my_list: for element in sublist: print(f"Checking {element}") if this_is_the_one(element): # ...? Again, break won't be of much help because it wil ..read more
Visit website
TIL #098 – Git alias for quick commit & push
Mathspp Blog » Json
by Rodrigo Girão Serrão
1M ago
Today I learned how to create git aliases in my .gitconfig file. Git alias for quick commit & push Today I learned (or was reminded, really) that you can create aliases for git commands. For example, for this blog I often run these two commands in sequence: git commit -m "Update" git push So, I realised I could set an alias, like git cp, to do this for me! I first learned how to create git aliases from Adam Johnson's “Boost Your Git DX”, but the very short version of one way in which this can work is by modifying the section [alias] of your .gitconfig file. You should place the file .gi ..read more
Visit website
ASCII rain scroll art
Mathspp Blog » Json
by Rodrigo Girão Serrão
1M ago
Using ASCII characters we can create a simple rain animation in the terminal. ASCII rain scroll art “Scroll art” is a term I've heard used to refer to images or animations made out of ASCII characters. In this short article we'll go over the code necessary to create this raining animation: Rain scroll art animation. This animation runs on the terminal and only requires using the built-in print. Coding the animation An animation is a series of images (frames) shown in quick succession. In this case, each frame is composed of a series of printed lines in a loop, so it's the way in which we mana ..read more
Visit website
TIL #097 – get terminal size
Mathspp Blog » Json
by Rodrigo Girão Serrão
1M ago
Today I learned how to get the size of the terminal your code is running on. Get terminal size The module shutil has a method .get_terminal_size that you can use to get the terminal size you're running on. For example, I stretched out my terminal, made it very short, and ran this code: >>> import shutil >>> shutil.get_terminal_size() os.terminal_size(columns=160, lines=9) The output that we see is a named tuple, so we can use it in multiple ways: >>> size = shutil.get_terminal_size() >>> size.columns 160 >>> width, height = size >>> wid ..read more
Visit website
TIL #096 – automatically push code changes during live coding
Mathspp Blog » Json
by Rodrigo Girão Serrão
2M ago
Today I learned how to automatically push code changes while I'm doing live coding, for example while teaching. Automatically push code changes during live coding When I'm doing live coding during tutorials / workshops / webinars, I often want to push the code I'm writing to GitHub so that participants have a way to fetch my work in case they fall behind. I don't want to have to do this manually every time I write some code, so I wrote a short script that does this automatically for me. I can never find the script that automates this process and every time I do some live coding during a webin ..read more
Visit website
TIL #095 – better test parametrisation in pytest
Mathspp Blog » Json
by Rodrigo Girão Serrão
2M ago
Today I learned how to use named tuples to improve readability and flexibility of test parametrisations in pytest. Better test parametrisation in pytest Today I attended a pytest tutorial at PyCon DE 2024 and I learned a couple of really neat tricks to improve parametrised tests. Let's take a look at them! Stacking the parametrize mark One quick tip is that you can stack parametrize to run all permutations of different parameters. For example, if calc is a function that does basic arithmetic operations, the test below will test 9 different additions: import pytest @pytest.mark.parametrize ..read more
Visit website
A tutorial is not a long talk
Mathspp Blog » Json
by Rodrigo Girão Serrão
3M ago
In this opinion piece the author expresses his views on what makes a good (Python) tutorial and how that differs from a long talk. A tutorial is not a long talk Bart Simpson internalising the message of this opinion piece. The title of the article should say it all but I'll elaborate a bit on what I mean by “a tutorial is not a long talk” and I'll share what I think makes a good tutorial. I've been to talks and tutorials at different conferences (mostly Python conferences) and I've attended great talks, great tutorials, terrible talks, and terrible tutorials. I've seen it all. Why do tal ..read more
Visit website
503 days working full-time on FOSS: lessons learned
Mathspp Blog » Json
by Rodrigo Girão Serrão
3M ago
This article shares some of the lessons I learned from working full-time on a FOSS project for 503 days. 503 days working full-time on FOSS: lessons learned From the 14th of November of 2022 to the 31st of March of 2024 I worked full-time on a FOSS project. This article is an account of some of the things that I learned during those 503 days. I have much more time to devote to you now that I don't have this full-time position. If you're looking for Python training, reach out to me or browse for yourself. I'll also be investing my time in this blog and in my books. I joined Textualize, the c ..read more
Visit website
Longest unique substring
Mathspp Blog » Json
by Rodrigo Girão Serrão
4M ago
How can you find the longest substring that contains only unique characters? Longest unique substring In this article I'll share a coding challenge with you. Then, I'll analyse different approaches to solving this problem, talking about the advantages and disadvantages of each one in terms of efficiency and code quality. Problem statement Given a string, find the longest substring that contains only unique characters. In case of a length tie, return the first one. Here are some examples to test your code with: def longest_unique_substring(string): ... lus = longest_unique_substring a ..read more
Visit website

Follow Mathspp Blog » Json on FeedSpot

Continue with Google
Continue with Apple
OR