with-statement

How to combine files in python 3 – with open() as: NOT working

How to combine files in python 3 – with open() as: NOT working Question: I am trying to combine the content of all .txt files in a directory one level above the directory where the .py file below is stored. import os def main(): # Get list of files stored in a dir above this …

Total answers: 3

Should a function that returns a context-managed object be decorated by `@contextmanager`?

Should a function that returns a context-managed object be decorated by `@contextmanager`? Question: Say I have a function that returns a context-managed object, here a tempfile.TemporaryFile: import tempfile def make_temp_file(): """Create a temporary file. Best used with a context manager""" tmpfile = tempfile.NamedTemporaryFile() return tmpfile Is this safe as is or should this be wrapped …

Total answers: 1

How do i mock an external libraries' classes/functions such as yaml.load() or Box() in python

How do i mock an external libraries' classes/functions such as yaml.load() or Box() in python Question: How would i go about testing the following class and its functions? import yaml from box import Box from yaml import SafeLoader class Config: def set_config_path(self): self.path = r"./config/datasets.yaml" return self.path def create_config(self): with open(r"./config/datasets.yaml") as f: self.config = …

Total answers: 1

Using a context manager to read excel files in python

Using a context manager to read excel files in python Question: I’m currently using the following line to read Excel files df = pd.read_excel(f"myfile.xlsx") The problem is the enormous slow down which occurs when I implement data from this Excel file, for example in function commands. I think this occurs because I’m not reading the …

Total answers: 2

How to use the command "with" conditionally without duplication of code

How to use the command "with" conditionally without duplication of code Question: I am trying to use with before executing a block of code, but only if a condition is met, but (at least the common) usage of with doesn’t appear to support that unless I duplicate the block of code. More concretely, I know …

Total answers: 2

Pylint R1732 ("Consider using 'with'") for one-liner: is it really good advice?

Pylint R1732 ("Consider using 'with'") for one-liner: is it really good advice? Question: On a line such as r = open(path, encoding="utf-8").read() (actual line here), Pylint 2.14.5 provides the following advise: submodules-dedup.py:71:32: R1732: Consider using ‘with’ for resource-allocating operations (consider-using-with) If I understand correctly, the suggestion is to change it to with open(path, encoding="utf-8") as …

Total answers: 2

Function failing to update spacing after comma

Function failing to update spacing after comma Question: I have a csv file that has inconsistent spacing after the comma, like this: 534323, 93495443,34234234, 3523423423, 2342342,236555, 6564354344 I have written a function that tries to read in the file and makes the spacing consistent, but it doesn’t appear to update anything. After opening the new …

Total answers: 2

What are the python builtin __exit__ argument types?

What are the python builtin __exit__ argument types? Question: Classes have a defineable function __exit__ that allows implementation of a context manager. It takes the required arguments: def __exit__(self, exc_type, exc_val, exc_tb): but I cannot find a definitive definition of what those arguments are and their types. Here’s my best guess of what they are …

Total answers: 2

Scope of variable within "with" statement?

Scope of variable within "with" statement? Question: I am reading only firstline from python using : with open(file_path, ‘r’) as f: my_count = f.readline() print(my_count) I am bit confused over scope of variable my_count. Although prints work fine, would it be better to do something like my_count = 0 outside with statement first (for eg …

Total answers: 2