简体   繁体   中英

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.

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)

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM