Customising object creation with __new__
Mathspp Blog
by
11h ago
The dunder method __new__ is used to customise object creation and is a core stepping stone in understanding metaprogramming in Python. Customising object creation with __new__ The dunder method __new__ is a static method that creates new instances of your class and, therefore, can be used to customise that process. The dunder methods __init__ and __new__ can look quite similar at first sight. The dunder method __init__ will initialise your instance of your class, but that instance must be created before it can be initialised, and that's the job of the dunder method __new__. Arguments of the ..read more
Visit website
Case-insensitive string class
Mathspp Blog
by
11h ago
This article shows how you can create a case-insensitive string class using some basic meta programming with the dunder method __new__. Case-insensitive string class In this article we want to implement a case-insensitive string class, that we will call CIStr, such that the comparisons between an instance of CIStr and a regular string, or between two CIStr instances, are done in a case-insensitive way. Here are two examples of what we're looking for: >>> CIStr("Hello") == "heLLO" True >>> "heLLO" == CIStr("Hello") True Case-insensitive equality comparison To compare two st ..read more
Visit website
Module itertools overview
Mathspp Blog
by
11h ago
This article briefly describes the iterators available in the Python module itertools and how to use them. Module itertools overview The Python module itertools contains 20 tools that every Python developer should be aware of. We divide the iterators from the module itertools in 5 categories to make it easier to learn them and we also present a short list of the generally most useful ones. All the iterators from itertools Category Iterators Reshaping iterators batched, chain*, groupby, islice, pairwise* Filtering iterators compress, dropwhile, filterfalse, takewhile Combinatorial i ..read more
Visit website
TIL #103 – debugging the new Python REPL with trace and PYREPL_TRACE.
Mathspp Blog
by
1w ago
Today I learned how to debug the new Python REPL with _pyrepl.trace and the environment variable PYREPL_TRACE. Debugging the new Python REPL with trace and PYREPL_TRACE As of Python 3.13, the Python REPL is written in Python. This means that if you are debugging the REPL and add a call to print in the code for the REPL, and then run the REPL, the debugging prints will show up in the REPL, in the middle of the thing you are trying to debug. This can get quite confusing. To help alleviate this issue, the REPL includes a short submodule trace that implements the function trace, which can be use ..read more
Visit website
TIL #102 – ctrl+left and ctrl+right not working in the Python REPL on MacOS
Mathspp Blog
by
1w ago
Today I learned how to fix an issue with Ctrl+left and Ctrl+right not working in the new Python REPL on MacOS. Ctrl+left and Ctrl+right not working in the Python REPL on MacOS The (new) Python REPL (3.13+) has many useful keyboard shortcuts and two of them are Ctrl+left and Ctrl+right, which are used to navigate the cursor by skipping to the beginning/end of words, as the GIT below shows: GIF showing the keybindings in action. On MacOS, the keybindings were not working at all. With the help of the new REPL trace functionality, we found out they weren't even making it to Python! After lots of ..read more
Visit website
TIL #100 – Making an iterator out of a function
Mathspp Blog
by
2w 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
by
1M 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
by
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
by
2M 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
by
2M 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

Follow Mathspp Blog on FeedSpot

Continue with Google
Continue with Apple
OR