This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jaraco
Recipients barry, jaraco, rhettinger
Date 2018-02-09.17:39:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1518197982.45.0.467229070634.issue32158@psf.upfronthosting.co.za>
In-reply-to
Content
In [this question](https://stackoverflow.com/a/48710609/70170), I encounter another case where a decorator would be useful. Without the decorator:

def is_docker():
	path = '/proc/self/cgroup'
	return (
		os.path.exists('/.dockerenv')
		or os.path.isfile(path)
		and any('docker' in line for line in open(path))
	)

With the decorator:

@suppress(FileNotFoundError)
def is_docker():
	return (
		os.path.exists('/.dockerenv')
		or any('docker' in line for line in open('/proc/self/cgroup'))
	)

The decorator enables several improvements:

- The boolean expression is now two expressions joined by 'or', which is semantically easier to parse and thus less prone to error than the three joined by and/or.
- There's no longer a need to create a path variable and reference it twice, allowing the value to appear inline where it's most relevant.
- The code is one line shorter.
- The body of the function is two lines shorter.
- The key behaviors the function is seeking to achieve are prominently presented.

Acknowledged there are two caveats:

- It's unclear the exception really is only expected in the 'open' call.
- In the case where the exception is suppressed, the function will return None, which while resolving to boolean False, isn't False.

Those caveats could be addressed, but will sacrifice readability or conciseness.

I don't think this use-case warrants re-opening the ticket or revisiting the issue, but I wanted to share for consideration.
History
Date User Action Args
2018-02-09 17:39:42jaracosetrecipients: + jaraco, barry, rhettinger
2018-02-09 17:39:42jaracosetmessageid: <1518197982.45.0.467229070634.issue32158@psf.upfronthosting.co.za>
2018-02-09 17:39:42jaracolinkissue32158 messages
2018-02-09 17:39:42jaracocreate