design-patterns

How to avoid nested "with" statements when working with multiple files in Python

How to avoid nested "with" statements when working with multiple files in Python Question: When working with multiple files in Python code can get ugly when using the recommended style: with open(“foo.txt”) as foo: with open(“bar.txt”, “w”) as bar: with open(“baz.txt”, “w”) as baz: # Read from foo, write different output to bar an baz …

Total answers: 1

Method overloading for different argument type in python

Method overloading for different argument type in python Question: I’m writing a preprocessor in python, part of which works with an AST. There is a render() method that takes care of converting various statements to source code. Now, I have it like this (shortened): def render(self, s): “”” Render a statement by type. “”” # …

Total answers: 5

python – should I use static methods or top-level functions

python – should I use static methods or top-level functions Question: I come from a Java background and I’m new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing files. Some functions associated with reading, some with writing. While searching for the correct …

Total answers: 5

Builder pattern equivalent in Python

Builder pattern equivalent in Python Question: In Java, you can use the builder pattern to provide a more readable means to instantiating a class with many parameters. In the builder pattern, one constructs a configuration object with methods to set named attributes, and then uses it to construct another object. What is the equivalent in …

Total answers: 6

elegant find sub-list in list

elegant find sub-list in list Question: Given a list containing a known pattern surrounded by noise, is there an elegant way to get all items that equal the pattern. See below for my crude code. list_with_noise = [7,2,1,2,3,4,2,1,2,3,4,9,9,1,2,3,4,7,4,3,1,2,3,5] known_pattern = [1,2,3,4] res = [] for i in list_with_noise: for j in known_pattern: if i == …

Total answers: 7

What is the difference between Python decorators and the decorator pattern?

What is the difference between Python decorators and the decorator pattern? Question: What is the difference between “Python decorators” and the “decorator pattern”? When should I use Python decorators, and when should I use the decorator pattern? I’m looking for examples of Python decorators and the decorator pattern accomplishing same. @AcceptedAnswer I know that Jakob …

Total answers: 2

Python dynamic inheritance: How to choose base class upon instance creation?

Python dynamic inheritance: How to choose base class upon instance creation? Question: Introduction I have encountered an interesting case in my programming job that requires me to implement a mechanism of dynamic class inheritance in python. What I mean when using the term “dynamic inheritance” is a class that doesn’t inherit from any base class …

Total answers: 4

Python logging exception

Python logging exception Question: I’m currently writing a wrapper class. I want to be able to log exceptions properly but allow calling methods to be aware of exceptions which occur. My class looks like this: import logging log = logging.getLogger(‘module’) class MyAPIWrapper(library.APIClass): def __init__(self): self.log = logging.getLogger(‘module.myapiwrapper’) def my_wrapper_method(self): try: response = self.call_api_method() return response.someData …

Total answers: 2

Using a class' __new__ method as a Factory: __init__ gets called twice

Using a class' __new__ method as a Factory: __init__ gets called twice Question: I encountered a strange bug in python where using the __new__ method of a class as a factory would lead to the __init__ method of the instantiated class to be called twice. The idea was originally to use the __new__ method of …

Total answers: 3

Python pattern for sharing configuration throughout application

Python pattern for sharing configuration throughout application Question: I have an application consisting of a base app that brings in several modules. The base app reads a parameter file into a configuration hash, and I want to share it across all my modules. Currently, I am passing a ‘parent’ object down to modules, and then …

Total answers: 4