forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonFunctions.py
40 lines (34 loc) · 1.17 KB
/
CommonFunctions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
import sys
# See https://www.python.org/dev/peps/pep-0479/#abstract and
# https://stackoverflow.com/a/51701040
if sys.version_info >= (3,7):
NEW_STYLE_GENERATOR = True
else:
NEW_STYLE_GENERATOR = False
class switch(object):
""" Helper class providing nice switch statement"""
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
if NEW_STYLE_GENERATOR:
return
else:
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False