gattu marrudu
gattu marrudu

Reputation: 257

Using a conditional 'with' statement in Python

I am trying to process a large byte stream in Python. As far as I have read, using the 'with' statement prevents loading temporary data into memory which would be an advantage for me.

My problem is that I have two options to choose my source data stream from: a raw data stream or a source path.

if sourceRef:
    with open(sourceRef, 'rb') as ds:
        dstreams['master'] = self._generateMasterFile(ds)
else:
    with self._validate(source) as ds:
        dstreams['master'] = self._generateMasterFile(ds)

This works all right, but I have more complex scenarios where the operations following the 'with' satements are more complex and I don't want to duplicate them.

Is there a way to compact these two options?

Thank you,

gm

Edit: I'm using Python 3.

Upvotes: 2

Views: 1991

Answers (2)

Simeon Visser
Simeon Visser

Reputation: 122446

The cleanest solution is probably to define ds beforehand:

if sourceRef:
    ds = open(sourceRef, 'rb')
else:
    ds = self._validate(source)

with ds:
    dstreams['master'] = self._generateMasterFile(ds)

This approach also works in a clean way when you have more than two possible ds values (you can simply extend the checks beforehand to determine the value for ds).

Upvotes: 5

hlt
hlt

Reputation: 6317

As long as both things work with with individually, you can inline the if statement as follows:

with (open(sourceRef, 'rb') if sourceRef else self._validate(source)) as ds:
    dstreams['master'] = self._generateMasterFile(ds)

Upvotes: 6

Related Questions