Topic Archive

Tips

  • How do you know if you’re actually growing as a dev? Last week I was chatting with a developer who’d hit a wall. (I talk to a lot of devs now that I think about it!) Like him, you might consider yourself a scrappy coder. You’re an all-rounder, can generally figure things out and write some sort of scrappy script to solve a problem. It’s actually a badge of pride! I myself have a bunch of scrappy scripts running in my home lab to do various things. The thing is, sometimes this confidence we feel being scrappy, may actually just…


  • There are few things in software engineering that induce panic quite like a massive git merge conflict. You pull down the latest code, open your editor, and suddenly your screen is bleeding with <<<<<<< HEAD markers. Your logic is tangled with someone else’s, the CSS is conflicting, and you realise you just wasted hours building on top of outdated architecture. It is easy to think this only happens to juniors, but it happens to us all. Case in point – this week it was the two of us butting… HEADs (get it?). When you code in isolation, you get comfortable.…


  • Python generators provide an elegant mechanism for handling iteration, particularly for large datasets where traditional approaches may be memory-intensive. Unlike standard functions that compute and return all values at once, generators produce values on demand through the yield statement, enabling efficient memory usage and creating new possibilities for data processing workflows. Generator Function Mechanics At their core, generator functions appear similar to regular functions but behave quite differently. The defining characteristic is the yield statement, which fundamentally alters the function’s execution model: When you call this function, it doesn’t execute immediately. Instead, it returns a generator object: This generator object…


  • A few examples that will save the day probably* 95% of time. *I don’t have the actual data but seriously, I bet you’ll find those tips useful more often than not! Introduction This article was originally posted on Medium. I use f-strings every day. The irony is I also every day end up searching the Web to find the correct format to use. Until one day I thought a better use of my time would be to create a cheat sheet of the most common formatting cases — AKA this article. It covers the following: It’s important to note that the f-strings were first…


  • Virtual environments are vital if you’re developing Python apps or just writing some Python scripts. They allow you to isolate different app requirements to prevent conflicts and keep your global OS environment clean. This is super important for many reasons. The most obvious one is requirements isolation. Let’s say that you’re working on two different projects: Although you can install both Python 3.6 and Python 3.10 globally on your OS, you may have to manually set the default version which may end up breaking some packages in your OS that expect the other version. As for other packages, “requests” in…


  • In this article let’s look at the functools Standard Library module and 6 cool things you can do with it (be warned, a lot of decorators are coming your way! 😍) … 1. Cache (“memoize”) things You can use the @cache decorator (formerly called @lru_cache) as a “simple lightweight unbounded function cache”. The classic example is calculating a Fibonacci series where the intermediate results are cached, speeding up the calculation significantly: On my system this code takes 0.02s to complete. 😎 However if I comment the @cache decorator it takes 28.30s because of all the repeated calculations! 😱 Hence caching is…


  • A challenge in software development is to keep things simple 🤯 For your code to not grow overly complex over time 😱 Simple is better than complex.Complex is better than complicated. Zen of Python 🐍 Simplicity in your code means fewer possibilities for bugs to hide and easier debugging when they do arise 📈 It also makes your code more understandable and maintainable, which is crucial in a team setting or when returning to your code after a period of time. Photo by Pablo Arroyo on Unsplash Let’s look at 5 examples: 1. Are all numbers divisible? A good first example is underutilizing…


  • Python allows you to use *args and **kwargs in function definitions to accept an arbitrary number of positional and keyword arguments, respectively. Here is a simple example: Different types of function arguments In the above example, the arbitrary_args function is defined to accept any number of positional and keyword arguments using the *args and **kwargs syntax. When the function is called with arbitrary_args(1, 2, 3, a=”apple”, b=”banana”), the *args collects all the positional arguments (1, 2, 3) into a tuple, and **kwargs gathers the keyword arguments (a and b) into a dictionary. Consequently, the function prints the tuple (1, 2,…


  • In the world of programming, errors are inevitable. But how we choose to handle these errors can make the difference between a system that is robust and user-friendly and one that is fraught with ambiguous issues 😱 The Zen of Python famously states, “Errors should never pass silently.” This principle emphasizes the importance of addressing issues head-on rather than ignoring them 😅 In this article, we’ll delve into the consequences of silent failures, the value of transparent error handling, and practical ways to ensure that errors in our Python code are always brought to light. Requests Let’s look at a…


  • Testing membership and empty strings

    ·

    ·

    2 min read

    I was working on one of the exercises on the Pybites platform (Bite 29) and encountered a situation I didn’t understand. I needed to check a set of inputs to see if they were alphanumeric or not as part of the solution to the exercise. I succeeded in all but one test, but I couldn’t tell why one failed so I researched why that was and would like to share it. Note – if you haven’t completed the Bite, the solution might be partially spoiled below so maybe you want to do the exercise before reading on. Membership testing is…