def create_permission_enum(key, member_names): """Create a permission enum.""" name = derive_name(key) permission_enum = Enum(name, list(member_names)) permission_enum.__key__ = key permission_enum.__repr__ = lambda self: '<{}>'.format(self) return permission_enum
def add_enum(self, *args, **kwargs): """ Add an enumeration to the module. See the documentation for L{Enum.__init__} for information on accepted parameters. """ if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], Enum): enum = args[0] warnings.warn("add_enum has changed API; see the API documentation", DeprecationWarning, stacklevel=2) else: enum = Enum(*args, **kwargs) enum.stack_where_defined = traceback.extract_stack() self._add_enum_obj(enum) return enum
def test_shouldReturnDefaultMaxWordsLearningFromSettingsWhenNoEntryExists(self): enum = Enum('enum', 'LEARN_RYTHM_MULTIPLIER LEARN_BASE_RYTHM LEARN_MAX_WORDS') for given_key, expected_value in { 'LEARN_RYTHM_MULTIPLIER': '2', 'LEARN_BASE_RYTHM': '2', 'LEARN_MAX_WORDS': '5', }.items(): # Given self.configuration_objects.filter.return_value = [] # When result = self.conf.get_configuration(enum.__getattr__(given_key), configuration_model=self.configuration_model) # Then self.assertEqual(result, expected_value)
def _PoCEntityTypes_parser(cls, value): if not isinstance(value, str): return Enum.__new__(cls, value) else: # map strings to enum values, default to Unknown return { 'src': EntityTypes.Source, 'tb': EntityTypes.Testbench, 'nl': EntityTypes.NetList }.get(value, EntityTypes.Unknown)
def test_base_enum(): e = Enum(operators=frozenset([PLUS, OR, NOT])) e.leafs = [0, 'x'] result = [] for t in e.base_enum(3): print t result.append(t) expected = sorted([ ('not', ('not', 0)), ('not', ('not', 'x')), ('plus', 0, 0), ('plus', 0, 'x'), ('plus', 'x', 'x'), ('or', 0, 0), ('or', 0, 'x'), ('or', 'x', 'x'), ]) eq_(sorted(result), expected)
def test_with_enum(): e = Enum(operators=frozenset( [NOT, SHL1, SHR4, AND, PLUS, OR, XOR])) all_terms = list(e.base_enum(3)) d = SemanticDict() for t in all_terms: if t not in d: d[t] = t print d.to_str() unique_terms = list(d.itervalues()) for i, t1 in enumerate(unique_terms): for j in range(i): assert not terms_equivalent(t1, unique_terms[j]) for t in all_terms: assert any(terms_equivalent(t, u) for u in unique_terms) print len(all_terms), 'terms total' print len(unique_terms), 'unique terms'
class EnumTests(unittest.TestCase): def setUp(self): self.enum = Enum('Dog','Cat','Fish') def testEmptyNotAllowed(self): try: enum = Enum() except AssertionError: pass else: fail("Expected assertion - empty enums not allowed") def testInclusion(self): assert self.enum.Cat def testInclusion_case(self): try: items = self.enum.cat except: pass else: fail("Expected assertion - enums are case sensitive") def testExclusion(self): try: items = self.enum.Frog except: pass else: fail("Expected assertion - item not in set") def testEquals(self): cat = self.enum.Cat anotherCat = self.enum.Cat assert (cat == anotherCat) def testLength(self): assert(self.enum.__len__() == 3)
def __init__(self, code): Enum.__init__(self) # cached canonical code self._code = self._canonicalize_code(code)
from enum import Enum ACTIONS = Enum('API functions', 'MATCHES PLAYERS TEAMS') BASE_URL = 'https://api.dc01.gamelockerapp.com/shards/global' SUCCESS_CODES = [200]
def __lt__(self, other): return int(self).__lt__(other) if isinstance(other, int) else Enum.__lt__(self, other)
opts.wrap_mode = None opts.prefix = prefix opts.cmd_line_options = {} return opts def get_fake_env(sdir='', bdir=None, prefix='', opts=None): if opts is None: opts = get_fake_options(prefix) env = Environment(sdir, bdir, opts) env.coredata.compiler_options.host['c']['args'] = FakeCompilerOptions() env.machines.host.cpu_family = 'x86_64' # Used on macOS inside find_library return env Backend = Enum('Backend', 'ninja vs xcode') if 'MESON_EXE' in os.environ: meson_exe = mesonlib.split_args(os.environ['MESON_EXE']) else: meson_exe = None if mesonlib.is_windows() or mesonlib.is_cygwin(): exe_suffix = '.exe' else: exe_suffix = '' def get_meson_script(): ''' Guess the meson that corresponds to the `mesonbuild` that has been imported
def test_iteration_order(self): Fruit = Enum('Fruit',[('Apple',2), ('Orange',0), ('Banana',1)]) expected_list = [Fruit.Orange,Fruit.Banana,Fruit.Apple] self.assertEqual(expected_list,Fruit.items())
class Config: """Configuration options. All available options, their types, and their descriptions are defined in :file:`config_default.yaml`. """ def __init__(self, folder: str = None, load_default=True): """Initialize with the default configuration""" if load_default: import kge from kge.misc import filename_in_module with open(filename_in_module(kge, "config-default.yaml"), "r") as file: self.options: Dict[str, Any] = yaml.load(file, Loader=yaml.SafeLoader) else: self.options = {} self.folder = folder # main folder (config file, checkpoints, ...) self.log_folder = ( None # None means use self.folder; used for kge.log, trace.yaml ) self.log_prefix: str = None # -- ACCESS METHODS ---------------------------------------------------------------- def get(self, key: str, remove_plusplusplus=True) -> Any: """Obtain value of specified key. Nested dictionary values can be accessed via "." (e.g., "job.type"). Strips all '+++' keys unless `remove_plusplusplus` is set to `False`. """ result = self.options for name in key.split("."): try: result = result[name] except KeyError: raise KeyError(f"Error accessing {name} for key {key}") if remove_plusplusplus and isinstance(result, collections.Mapping): def do_remove_plusplusplus(option): if isinstance(option, collections.Mapping): option.pop("+++", None) for values in option.values(): do_remove_plusplusplus(values) result = copy.deepcopy(result) do_remove_plusplusplus(result) return result def get_default(self, key: str) -> Any: """Returns the value of the key if present or default if not. The default value is looked up as follows. If the key has form ``parent.field``, see if there is a ``parent.type`` property. If so, try to look up ``field`` under the key specified there (proceeds recursively). If not, go up until a `type` field is found, and then continue from there. """ try: return self.get(key) except KeyError as e: last_dot_index = key.rfind(".") if last_dot_index < 0: raise e parent = key[:last_dot_index] field = key[last_dot_index + 1:] while True: # self.log("Looking up {}/{}".format(parent, field)) try: parent_type = self.get(parent + "." + "type") # found a type -> go to this type and lookup there new_key = parent_type + "." + field last_dot_index = new_key.rfind(".") parent = new_key[:last_dot_index] field = new_key[last_dot_index + 1:] except KeyError: # no type found -> go up hierarchy last_dot_index = parent.rfind(".") if last_dot_index < 0: raise e field = parent[last_dot_index + 1:] + "." + field parent = parent[:last_dot_index] continue try: value = self.get(parent + "." + field) # uncomment this to see where defaults are taken from # self.log( # "Using value of {}={} for key {}".format( # parent + "." + field, value, key # ) # ) return value except KeyError: # try further continue def get_first_present_key(self, *keys: str, use_get_default=False) -> str: "Return the first key for which ``get`` or ``get_default`` finds a value." for key in keys: try: self.get_default(key) if use_get_default else self.get(key) return key except KeyError: pass raise KeyError("None of the following keys found: ".format(keys)) def get_first(self, *keys: str, use_get_default=False) -> Any: "Return value (or default value) of the first valid key present or KeyError." if use_get_default: return self.get_default( self.get_first_present_key(*keys, use_get_default=True)) else: return self.get(self.get_first_present_key(*keys)) Overwrite = Enum("Overwrite", "Yes No Error") def set(self, key: str, value, create=False, overwrite=Overwrite.Yes, log=False) -> Any: """Set value of specified key. Nested dictionary values can be accessed via "." (e.g., "job.type"). If ``create`` is ``False`` , raises :class:`ValueError` when the key does not exist already; otherwise, the new key-value pair is inserted into the configuration. """ from kge.misc import is_number splits = key.split(".") data = self.options # flatten path and see if it is valid to be set path = [] for i in range(len(splits) - 1): if splits[i] in data: create = create or "+++" in data[splits[i]] else: if create: data[splits[i]] = dict() else: raise KeyError(("{} cannot be set because creation of " + "{} is not permitted").format( key, ".".join(splits[:(i + 1)]))) path.append(splits[i]) data = data[splits[i]] # check correctness of value try: current_value = data.get(splits[-1]) except: raise Exception( "These config entries {} {} caused an error.".format( data, splits[-1])) if current_value is None: if not create: raise ValueError("key {} not present".format(key)) if isinstance(value, str) and is_number(value, int): value = int(value) elif isinstance(value, str) and is_number(value, float): value = float(value) else: if (isinstance(value, str) and isinstance(current_value, float) and is_number(value, float)): value = float(value) elif (isinstance(value, str) and isinstance(current_value, int) and is_number(value, int)): value = int(value) if type(value) != type(current_value): raise ValueError( "key {} has incorrect type (expected {}, found {})".format( key, type(current_value), type(value))) if overwrite == Config.Overwrite.No: return current_value if overwrite == Config.Overwrite.Error and value != current_value: raise ValueError("key {} cannot be overwritten".format(key)) # all fine, set value data[splits[-1]] = value if log: self.log("Set {}={}".format(key, value)) return value def _import(self, module_name: str): """Imports the specified module configuration. Adds the configuration options from kge/model/<module_name>.yaml to the configuration. Retains existing module configurations, but verifies that fields and their types are correct. """ import kge.model, kge.model.embedder from kge.misc import filename_in_module # load the module_name module_config = Config(load_default=False) module_config.load( filename_in_module([kge.model, kge.model.embedder], "{}.yaml".format(module_name)), create=True, ) if "import" in module_config.options: del module_config.options["import"] # add/verify current configuration for key in module_config.options.keys(): cur_value = None try: cur_value = {key: self.get(key)} except KeyError: continue module_config.set_all(cur_value, create=False) # now update this configuration self.set_all(module_config.options, create=True) # remember the import imports = self.options.get("import") if imports is None: imports = module_name elif isinstance(imports, str): imports = [imports, module_name] else: imports.append(module_name) imports = list(dict.fromkeys(imports)) self.options["import"] = imports def set_all(self, new_options: Dict[str, Any], create=False, overwrite=Overwrite.Yes): for key, value in Config.flatten(new_options).items(): self.set(key, value, create, overwrite) def load( self, filename: str, create=False, overwrite=Overwrite.Yes, allow_deprecated=True, ): """Update configuration options from the specified YAML file. All options that do not occur in the specified file are retained. If ``create`` is ``False``, raises :class:`ValueError` when the file contains a non-existing options. When ``create`` is ``True``, allows to add options that are not present in this configuration. If the file has an import or model field, the corresponding configuration files are imported. """ with open(filename, "r") as file: new_options = yaml.load(file, Loader=yaml.SafeLoader) self.load_options( new_options, create=create, overwrite=overwrite, allow_deprecated=allow_deprecated, ) def load_options(self, new_options, create=False, overwrite=Overwrite.Yes, allow_deprecated=True): "Like `load`, but loads from an options object obtained from `yaml.load`." # import model configurations if "model" in new_options: model = new_options.get("model") # TODO not sure why this can be empty when resuming an ax # search with model as a search parameter if model: self._import(model) if "import" in new_options: imports = new_options.get("import") if not isinstance(imports, list): imports = [imports] for module_name in imports: self._import(module_name) del new_options["import"] # process deprecated options if allow_deprecated: new_options = _process_deprecated_options( Config.flatten(new_options)) # now set all options self.set_all(new_options, create, overwrite) def save(self, filename): """Save this configuration to the given file""" with open(filename, "w+") as file: file.write(yaml.dump(self.options)) @staticmethod def flatten(options: Dict[str, Any]) -> Dict[str, Any]: """Returns a dictionary of flattened configuration options.""" result = {} Config.__flatten(options, result) return result @staticmethod def __flatten(options: Dict[str, Any], result: Dict[str, Any], prefix=""): for key, value in options.items(): fullkey = key if prefix == "" else prefix + "." + key if type(value) is dict: Config.__flatten(value, result, prefix=fullkey) else: result[fullkey] = value def clone(self, subfolder: str = None) -> "Config": """Return a deep copy""" new_config = Config(folder=copy.deepcopy(self.folder), load_default=False) new_config.options = copy.deepcopy(self.options) if subfolder is not None: new_config.folder = os.path.join(self.folder, subfolder) return new_config # -- LOGGING AND TRACING ----------------------------------------------------------- def log(self, msg: str, echo=True, prefix=""): """Add a message to the default log file. Optionally also print on console. ``prefix`` is used to indent each output line. """ with open(self.logfile(), "a") as file: for line in msg.splitlines(): if prefix: line = prefix + line if self.log_prefix: line = self.log_prefix + line if echo: print(line) file.write(str(datetime.datetime.now()) + " " + line + "\n") def trace(self, echo=False, echo_prefix="", echo_flow=False, log=False, **kwargs) -> Dict[str, Any]: """Write a set of key-value pairs to the trace file. The pairs are written as a single-line YAML record. Optionally, also echo to console and/or write to log file. And id and the current time is automatically added using key ``timestamp``. Returns the written k/v pairs. """ kwargs["timestamp"] = time.time() kwargs["entry_id"] = str(uuid.uuid4()) line = yaml.dump(kwargs, width=float("inf"), default_flow_style=True).strip() if echo or log: msg = yaml.dump(kwargs, default_flow_style=echo_flow) if log: self.log(msg, echo, echo_prefix) else: for line in msg.splitlines(): if echo_prefix: line = echo_prefix + line print(line) with open(self.tracefile(), "a") as file: file.write(line + "\n") return kwargs # -- FOLDERS AND CHECKPOINTS ---------------------------------------------- def init_folder(self): """Initialize the output folder. If the folder does not exists, create it, dump the configuration there and return ``True``. Else do nothing and return ``False``. """ if not os.path.exists(self.folder): os.makedirs(self.folder) os.makedirs(os.path.join(self.folder, "config")) self.save(os.path.join(self.folder, "config.yaml")) return True return False def checkpoint_file(self, cpt_id: Union[str, int]) -> str: "Return path of checkpoint file for given checkpoint id" from kge.misc import is_number if is_number(cpt_id, int): return os.path.join(self.folder, "checkpoint_{:05d}.pt".format(int(cpt_id))) else: return os.path.join(self.folder, "checkpoint_{}.pt".format(cpt_id)) def last_checkpoint(self) -> Optional[int]: "Return epoch number of latest checkpoint" # stupid implementation, but works tried_epoch = 0 found_epoch = 0 while tried_epoch < found_epoch + 500: tried_epoch += 1 if os.path.exists(self.checkpoint_file(tried_epoch)): found_epoch = tried_epoch if found_epoch > 0: return found_epoch else: return None @staticmethod def get_best_or_last_checkpoint(path: str) -> str: """Returns best (if present) or last checkpoint path for a given folder path.""" config = Config(folder=path, load_default=False) checkpoint_file = config.checkpoint_file("best") if os.path.isfile(checkpoint_file): return checkpoint_file cpt_epoch = config.last_checkpoint() if cpt_epoch: return config.checkpoint_file(cpt_epoch) else: raise Exception("Could not find checkpoint in {}".format(path)) # -- CONVENIENCE METHODS -------------------------------------------------- def _check(self, key: str, value, allowed_values) -> Any: if value not in allowed_values: raise ValueError( "Illegal value {} for key {}; allowed values are {}".format( value, key, allowed_values)) return value def check(self, key: str, allowed_values) -> Any: """Raise an error if value of key is not in allowed. If fine, returns value. """ return self._check(key, self.get(key), allowed_values) def check_default(self, key: str, allowed_values) -> Any: """Raise an error if value or default value of key is not in allowed. If fine, returns value. """ return self._check(key, self.get_default(key), allowed_values) def check_range(self, key: str, min_value, max_value, min_inclusive=True, max_inclusive=True) -> Any: value = self.get(key) if (value < min_value or (value == min_value and not min_inclusive) or value > max_value or (value == max_value and not max_inclusive)): raise ValueError( "Illegal value {} for key {}; must be in range {}{},{}{}". format( value, key, "[" if min_inclusive else "(", min_value, max_value, "]" if max_inclusive else ")", )) return value def logfile(self) -> str: folder = self.log_folder if self.log_folder else self.folder return os.path.join(folder, "kge.log") def tracefile(self) -> str: folder = self.log_folder if self.log_folder else self.folder return os.path.join(folder, "trace.yaml")
def setUp(self): self.enum = Enum('Dog','Cat','Fish')
from enum import Enum import re from typing import Dict, List, Optional, TextIO from linesep import read_paragraphs from pydantic import BaseModel ParserState = Enum("ParserState", "BADGES POST_LINKS POST_CONTENTS INTRO SECTIONS") HEADER_LINK_RGX = r"`(?P<label>[^`<>]+) <(?P<url>[^>]+)>`_" IMAGE_START = ".. image:: " class Image(BaseModel): href: str target: Optional[str] alt: Optional[str] @classmethod def parse(cls, s: str) -> "Image": if not s.startswith(IMAGE_START): raise ValueError(f"Not an RST image: {s!r}") lines = s.splitlines(keepends=True) href = lines[0][len(IMAGE_START) :].strip() options: Dict[str, Optional[str]] = { "target": None, "alt": None, } opt_name: Optional[str] = None opt_value: Optional[str] = None for ln in lines[1:]:
def parse_header_type(cfg): header_type = Enum('HeaderType', ' '.join(cfg.HeaderType), module=__name__) return header_type
Contours as a list of numpy.ndarray. """ output = [] for contour in input_contours: x, y, w, h = cv2.boundingRect(contour) if (w < min_width or w > max_width): continue if (h < min_height or h > max_height): continue area = cv2.contourArea(contour) if (area < min_area): continue if (cv2.arcLength(contour, True) < min_perimeter): continue hull = cv2.convexHull(contour) solid = 100 * area / cv2.contourArea(hull) if (solid < solidity[0] or solid > solidity[1]): continue if (len(contour) < min_vertex_count or len(contour) > max_vertex_count): continue ratio = (float)(w) / h if (ratio < min_ratio or ratio > max_ratio): continue output.append(contour) return output BlurType = Enum('BlurType', 'Box_Blur Gaussian_Blur Median_Filter Bilateral_Filter')
class RemapLabels(Transform, CliPlugin): DefaultAction = Enum('DefaultAction', ['keep', 'delete']) @staticmethod def _split_arg(s): parts = s.split(':') if len(parts) != 2: import argparse raise argparse.ArgumentTypeError() return (parts[0], parts[1]) @classmethod def build_cmdline_parser(cls, **kwargs): parser = super().build_cmdline_parser(**kwargs) parser.add_argument('-l', '--label', action='append', type=cls._split_arg, dest='mapping', help="Label in the form of: '<src>:<dst>' (repeatable)") parser.add_argument('--default', choices=[a.name for a in cls.DefaultAction], default=cls.DefaultAction.keep.name, help="Action for unspecified labels") return parser def __init__(self, extractor, mapping, default=None): super().__init__(extractor) assert isinstance(default, (str, self.DefaultAction)) if isinstance(default, str): default = self.DefaultAction[default] assert isinstance(mapping, (dict, list)) if isinstance(mapping, list): mapping = dict(mapping) self._categories = {} src_label_cat = self._extractor.categories().get(AnnotationType.label) if src_label_cat is not None: self._make_label_id_map(src_label_cat, mapping, default) src_mask_cat = self._extractor.categories().get(AnnotationType.mask) if src_mask_cat is not None: assert src_label_cat is not None dst_mask_cat = MaskCategories(attributes=src_mask_cat.attributes) dst_mask_cat.colormap = { id: src_mask_cat.colormap[id] for id, _ in enumerate(src_label_cat.items) if self._map_id(id) or id == 0 } self._categories[AnnotationType.mask] = dst_mask_cat src_points_cat = self._extractor.categories().get(AnnotationType.points) if src_points_cat is not None: assert src_label_cat is not None dst_points_cat = PointsCategories(attributes=src_points_cat.attributes) dst_points_cat.items = { id: src_points_cat.items[id] for id, item in enumerate(src_label_cat.items) if self._map_id(id) or id == 0 } self._categories[AnnotationType.points] = dst_points_cat def _make_label_id_map(self, src_label_cat, label_mapping, default_action): dst_label_cat = LabelCategories(attributes=src_label_cat.attributes) id_mapping = {} for src_index, src_label in enumerate(src_label_cat.items): dst_label = label_mapping.get(src_label.name) if not dst_label and default_action == self.DefaultAction.keep: dst_label = src_label.name # keep unspecified as is if not dst_label: continue dst_index = dst_label_cat.find(dst_label)[0] if dst_index is None: dst_index = dst_label_cat.add(dst_label, src_label.parent, src_label.attributes) id_mapping[src_index] = dst_index if log.getLogger().isEnabledFor(log.DEBUG): log.debug("Label mapping:") for src_id, src_label in enumerate(src_label_cat.items): if id_mapping.get(src_id): log.debug("#%s '%s' -> #%s '%s'", src_id, src_label.name, id_mapping[src_id], dst_label_cat.items[id_mapping[src_id]].name ) else: log.debug("#%s '%s' -> <deleted>", src_id, src_label.name) self._map_id = lambda src_id: id_mapping.get(src_id, None) self._categories[AnnotationType.label] = dst_label_cat def categories(self): return self._categories def transform_item(self, item): # TODO: provide non-inplace version annotations = [] for ann in item.annotations: if ann.type in { AnnotationType.label, AnnotationType.mask, AnnotationType.points, AnnotationType.polygon, AnnotationType.polyline, AnnotationType.bbox } and ann.label is not None: conv_label = self._map_id(ann.label) if conv_label is not None: ann._label = conv_label annotations.append(ann) else: annotations.append(ann) item._annotations = annotations return item
from enum import Enum from collections import namedtuple Type = Enum("Type", ("CURE", "HEALTHY", "SICK", "DYING", "DEAD")) Agent = namedtuple("Agent", ("name", "category")) def meetup(agent_listing: tuple) -> list: """Model the outcome of the meetings of pairs of agents. The pairs of agents are ((a[0], a[1]), (a[2], a[3]), ...). If there's an uneven number of agents, the last agent will remain the same. Notes ----- The rules governing the meetings were described in the question. The outgoing listing may change its internal ordering relative to the incoming one. Parameters ---------- agent_listing : tuple of Agent A listing (tuple in this case) in which each element is of the Agent type, containing a 'name' field and a 'category' field, with 'category' being of the type Type. Returns ------- updated_listing : list A list of Agents with their 'category' field changed according to the result of the meeting. """
import sys from enum import Enum from error import print_error_msg, ERRNUM from loader import DictionaryLoader # ex: python -m "name" # ex: python -m "name" "ox" ARGS = Enum("PY", "WORD", "TYPE", "MAX", AVAILABLE=2) def parse_arguements(args): if len(args) < ARGS.AVAILABLE: print_error_msg(ERRNUM.E_ARGS) word = args[ARGS.WORD] try: module = args[ARGS.TYPE] except IndexError: module = 'ox' # default module return (word, module) if __name__ == "__main__": word, module = parse_arguements(sys.argv) dict_ins = DictionaryLoader.get_module(module) dict_ins.find_word(word).show_word()
name='followup_priorities', validate_strings=True) sqla_enum_types = [ allowed_bandpasses, thumbnail_types, instrument_types, followup_priorities, ] api_classnames = sa.Enum( *[ k for k, v in facility_apis.__dict__.items() if inspect.isclass(v) and issubclass(v, facility_apis.FollowUpAPI) and v is not facility_apis.FollowUpAPI ], name='followup_apis', validate_strings=True, ) listener_classnames = sa.Enum( *LISTENER_CLASSNAMES, name='followup_listeners', validate_strings=True, ) py_allowed_magsystems = Enum('magsystems', ALLOWED_MAGSYSTEMS) py_allowed_bandpasses = Enum('bandpasses', ALLOWED_BANDPASSES) py_thumbnail_types = Enum('thumbnail_types', THUMBNAIL_TYPES) py_followup_priorities = Enum('priority', FOLLOWUP_PRIORITIES)
possible_tokens = ['ARRAY', 'BOOLEAN', 'BREAK', 'CHAR', 'CONTINUE', 'DO', 'ELSE', 'FALSE', 'FUNCTION', 'IF', 'INTEGER', 'OF', 'STRING', 'STRUCT', 'TRUE', 'TYPE', 'VAR', 'WHILE', 'COLON', 'SEMI_COLON', 'COMMA', 'EQUALS', 'LEFT_SQUARE', 'RIGHT_SQUARE', 'LEFT_BRACES', 'RIGHT_BRACES', 'LEFT_PARENTHESIS', 'RIGHT_PARENTHESIS', 'AND', 'OR', 'LESS_THAN', 'GREATER_THAN', 'LESS_OR_EQUAL', 'GREATER_OR_EQUAL', 'NOT_EQUAL', 'EQUAL_EQUAL', 'PLUS', 'PLUS_PLUS', 'MINUS', 'MINUS_MINUS', 'TIMES', 'DIVIDE', 'DOT', 'NOT', 'CHARACTER', 'NUMERAL', 'STRINGVAL', 'ID', 'UNKNOWN'] possible_tokens_ids = ['array', 'boolean', 'break', 'char', 'continue', 'do', 'else', 'false', 'function', 'if', 'integer', 'of', 'string', 'struct', 'true', 'type', 'var', 'while', ':', ';', ',', '=', '[', ']', '"', '"', '(', ')', '&&', '||', '<', '>', '<=', '>=', '!=', '==', '+', '++', '-', '--', '*', '/', '.', '!', 'character', 'numeral', 'stringval', 'id'] Tokens = Enum._create_('Tokens', possible_tokens, None, int) def searchKeyWord(text): if possible_tokens_ids.__contains__(text.lower()): return possible_tokens_ids.index(text.lower())+1 else: return Tokens.ID.value def searchName(text): if possible_tokens.__contains__(text.upper()): return possible_tokens.index(text.upper()) else: return -1
from __future__ import unicode_literals from collections import defaultdict from enum import Enum from tgen.logf import log_debug from tgen.logf import log_warn from tgen.tree import TreeData, TreeNode import numpy as np try: from pytreex.core.node import T except ImportError: log_warn('Pytreex modules not available, will not be able to evaluate trees.') EvalTypes = Enum(b'EvalTypes', b'TOKEN NODE DEP') EvalTypes.__doc__ = """Evaluation flavors (tokens, tree node-only, tree dependency)""" def collect_counts(sent, eval_type=EvalTypes.NODE): """Collects counts of different node/dependency types in the given t-tree. @param sent: the tree/sentence to collect counts from @param eval_type: if set to EvalTypes.NODE (default), count nodes (formemes, lemmas, dependency \ direction), if set to EvalTypes.DEP, count dependencies (including parent's formeme, lemma, \ dependency direction), if set to EvalTypes.TOKEN, count just word forms (in list of tokens). @rtype: defaultdict """ counts = defaultdict(int) nodes = sent if isinstance(sent, list) else sent.get_descendants() for node in nodes:
class GeneticAlgorithm(Generic[C]): SelectionType = Enum("SelectionType", "ROULETTE TOURNAMENT") def __init__( self, initial_population: List[C], threshold: float, max_generations: int = 100, mutation_chance: float = 0.01, crossover_chance: float = 0.7, selection_type: SelectionType = SelectionType.TOURNAMENT) -> None: self._population: List[C] = initial_population self._threshold: float = threshold self._max_generations: int = max_generations self._mutation_chance: float = mutation_chance self._crossover_chance: float = crossover_chance self._selection_type: GeneticAlgorithm.SelectionType = selection_type self._fitness_key: Callable = type(self._population[0]).fitness # Use the probability distribution wheel to pick 2 parents # Note: will not work with negative fitness results def _pick_roulette(self, wheel: List[float]) -> Tuple[C, C]: return tuple(choices(self._population, weights=wheel, k=2)) # Choose num_participants at random and take the best 2 def _pick_tournament(self, num_participants: int) -> Tuple[C, C]: participants: List[C] = choices(self._population, k=num_participants) return tuple(nlargest(2, participants, key=self._fitness_key)) # Replace the population with a new generation of individuals def _reproduce_and_replace(self) -> None: new_population: List[C] = [] # keep going until we've filled the new generation while len(new_population) < len(self._population): # pick the 2 parents if self._selection_type == GeneticAlgorithm.SelectionType.ROULETTE: parents: Tuple[C, C] = self._pick_roulette( [x.fitness() for x in self._population]) else: parents = self._pick_tournament(len(self._population) // 2) # potentially crossover the 2 parents if random() < self._crossover_chance: new_population.extend(parents[0].crossover(parents[1])) else: new_population.extend(parents) # if we had an odd number, we'll have 1 extra, so we remove it if len(new_population) > len(self._population): new_population.pop() self._population = new_population # replace reference # With _mutation_chance probability mutate each individual def _mutate(self) -> None: for individual in self._population: if random() < self._mutation_chance: individual.mutate() # Run the genetic algorithm for max_generations iterations # and return the best individual found def run(self) -> C: best: C = max(self._population, key=self._fitness_key) for generation in range(self._max_generations): if best.fitness( ) >= self._threshold: # early exit if we beat threshold return best print( f"Generation {generation} Best {best.fitness()} Avg {mean(map(self._fitness_key, self._population))}" ) self._reproduce_and_replace() self._mutate() highest: C = max(self._population, key=self._fitness_key) if highest.fitness() > best.fitness(): best = highest # found a new best return best # best we found in max_generations
from enum import Enum import mutablerecords from openhtf.util import logs _LOG = logging.getLogger(__name__) class InvalidMeasurementDimensions(Exception): """Raised when a measurement is taken with the wrong number of dimensions.""" OutcomeDetails = collections.namedtuple('OutcomeDetails', 'code description') Outcome = Enum('Outcome', ['PASS', 'FAIL', 'ERROR', 'TIMEOUT', 'ABORTED']) # pylint: disable=invalid-name # LogRecord is in openhtf.util.logs.LogRecord. class Attachment(collections.namedtuple('Attachment', 'data mimetype')): """Encapsulate attachment data and guessed MIME type.""" @property def sha1(self): return hashlib.sha1(self.data).hexdigest() class TestRecord( # pylint: disable=no-init mutablerecords.Record( 'TestRecord', ['dut_id', 'station_id'], { 'start_time_millis': int, 'end_time_millis': None,
class Uint128Struct(c.Structure): _fields_ = [("hi", c.c_uint64), ("lo", c.c_uint64)] ObjT = Enum( 'ObjT', [ # These are the only conf object types we care about. ('PROCESS', 0x7200000000000001), ('SERVICE', 0x7300000000000001), ('SDEV', 0x6400000000000001), ('DRIVE', 0x6b00000000000001), ('PROFILE', 0x7000000000000001), ('OBJV', 0x6a00000000000001), ('NODE', 0x6e00000000000001), ('SITE', 0x5300000000000001), ('RACK', 0x6100000000000001), ('ENCLOSURE', 0x6500000000000001), ('CONTROLLER', 0x6300000000000001), ('ROOT', 0x7400000000000001), ('POOL', 0x6f00000000000001), ('PVER', 0x7600000000000001) ]) ObjT.__doc__ = 'Motr conf object types and their m0_fid.f_container values' class HaNoteStruct(c.Structure): # Constants for no_state field values as they are described in ha/note.h
class TestEnum(BackendInfraTest): def setUp(self): super(TestEnum,self).setUp() self.Colors = Enum('Colors',['Red','Green','Blue']) def test_get_by_name(self): red = self.Colors.Red self.assertEqual(red.name,'Red') self.assertEqual(red.val,0) self.assertEqual(str(red),'Red') def test_get_by_value(self): red = self.Colors.get_by_value(1) self.assertEqual(red.name,'Green') self.assertEqual(self.Colors.Blue, self.Colors.get_by_value(2)) def test_specific_values(self): Priority = Enum('Priority',[('Critical',500), ('Medium',250), ('Low',0)]) med = Priority.Medium self.assertEqual(med.val,250) def test_lookup_error(self): self.assertRaises(AttributeError,getattr, self.Colors,'Purple') self.assertRaises(KeyError,self.Colors.get_by_value, 5) def test_ctor(self): Enum('SomeEnum',[]) # check empty list (should work although not very useful) self.assertRaises(EnumException, Enum, 'SomeEnum', ['Yes',5,4]) self.assertRaises(EnumException, Enum, 'SomeEnum', ['Yes',('No',4),('Maybe','MaybeNot')]) self.assertRaises(EnumException, Enum, 'SomeEnum', ['Yes','No','Yes']) # duplicate name self.assertRaises(EnumException, Enum, 'SomeEnum', ['Yes','No',('Maybe',0)]) # duplicate value def test_cmp(self): self.assertEqual(self.Colors.Blue,self.Colors.Blue) self.assertNotEqual(self.Colors.Blue,self.Colors.Red) self.assert_(self.Colors.Red < self.Colors.Blue) # comparisons to other types or other enums self.assertNotEqual(self.Colors.Red,None) Genders = Enum('Genders', ['Male','Female']) Genders2 = Enum('Genders2', ['Male','Female','Undecided']) self.assertNotEqual(self.Colors.Red,Genders.Male) self.assertNotEqual(Genders.Male,Genders2.Male) def test_is_legal_value(self): self.assertEqual(self.Colors.is_legal_value(self.Colors.Red), True) self.assertEqual(self.Colors.is_legal_value(None), False) self.assertEqual(self.Colors.is_legal_value(True), False) def test_iteration_order(self): Fruit = Enum('Fruit',[('Apple',2), ('Orange',0), ('Banana',1)]) expected_list = [Fruit.Orange,Fruit.Banana,Fruit.Apple] self.assertEqual(expected_list,Fruit.items()) def test_add_item(self): white = self.Colors.add_item('White') self.assertEqual(white, self.Colors.White) self.assertEqual(self.Colors.White.val,3) self.Colors.add_item('Black',100) self.assertEqual(self.Colors.Black.val,100) self.assertRaises(EnumException, self.Colors.add_item, 'Red') # duplicate name self.assertRaises(EnumException, self.Colors.add_item, 'Purple', 2) # duplicate value
class QueryHistory(models.Model): """ Holds metadata about all queries that have been executed. """ STATE = Enum('submitted', 'running', 'available', 'failed', 'expired') SERVER_TYPE = ((BEESWAX, 'Beeswax'), (HIVE_SERVER2, 'Hive Server 2')) owner = models.ForeignKey(User, db_index=True) query = models.TextField() last_state = models.IntegerField(db_index=True) has_results = models.BooleanField(default=False) # If true, this query will eventually return tabular results. submission_date = models.DateTimeField(auto_now_add=True) # In case of multi statements in a query, these are the id of the currently running statement server_id = models.CharField(max_length=1024, null=True) # Aka secret, only query in the "submitted" state is allowed to have no server_id server_guid = models.CharField(max_length=1024, null=True, default=None) statement_number = models.SmallIntegerField(default=0) # The index of the currently running statement operation_type = models.SmallIntegerField(null=True) modified_row_count = models.FloatField(null=True) log_context = models.CharField(max_length=1024, null=True) server_host = models.CharField(max_length=128, help_text=_('Host of the query server.'), default='') server_port = models.SmallIntegerField(help_text=_('Port of the query server.'), default=0) server_name = models.CharField(max_length=128, help_text=_('Name of the query server.'), default='') server_type = models.CharField(max_length=128, help_text=_('Type of the query server.'), default=BEESWAX, choices=SERVER_TYPE) query_type = models.SmallIntegerField(help_text=_('Type of the query.'), default=HQL, choices=((HQL, 'HQL'), (IMPALA, 'IMPALA'))) design = models.ForeignKey('SavedQuery', to_field='id', null=True) # Some queries (like read/create table) don't have a design notify = models.BooleanField(default=False) # Notify on completion class Meta: ordering = ['-submission_date'] @staticmethod def build(*args, **kwargs): if kwargs['server_type'] == HIVE_SERVER2: return HiveServerQueryHistory(*args, **kwargs) else: return BeeswaxQueryHistory(*args, **kwargs) def get_full_object(self): if self.server_type == HiveServerQueryHistory.node_type: return HiveServerQueryHistory.objects.get(id=self.id) else: return BeeswaxQueryHistory.objects.get(id=self.id) @staticmethod def get(id): if QueryHistory.objects.filter(id=id, server_type=BEESWAX).exists(): return BeeswaxQueryHistory.objects.get(id=id) else: return HiveServerQueryHistory.objects.get(id=id) def get_type_name(self): if self.query_type == 1: return 'impala' else: return 'beeswax' def get_query_server_config(self): from beeswax.server.dbms import get_query_server_config query_server = get_query_server_config(self.get_type_name()) query_server.update({ 'server_name': self.server_name, 'server_host': self.server_host, 'server_port': self.server_port, 'server_type': self.server_type, }) return query_server def get_current_statement(self): if self.design is not None: design = self.design.get_design() return design.get_query_statement(self.statement_number) else: return self.query def is_finished(self): is_statement_finished = not self.is_running() if self.design is not None: design = self.design.get_design() return is_statement_finished and self.statement_number + 1 == design.statement_count # Last statement else: return is_statement_finished def is_running(self): return self.last_state in (QueryHistory.STATE.running.index, QueryHistory.STATE.submitted.index) def is_success(self): return self.last_state in (QueryHistory.STATE.available.index,) def is_failure(self): return self.last_state in (QueryHistory.STATE.expired.index, QueryHistory.STATE.failed.index) def set_to_running(self): self.last_state = QueryHistory.STATE.running.index def set_to_failed(self): self.last_state = QueryHistory.STATE.failed.index def set_to_available(self): self.last_state = QueryHistory.STATE.available.index
Any issues can be reported to https://github.com/CalabreseLab --- """ import numpy as np from enum import Enum from collections import defaultdict from itertools import product from pandas import DataFrame from seekr.my_tqdm import my_tqdm from seekr.fasta_reader import Reader Log2 = Enum('Log2', ('pre', 'post', 'none')) class BasicCounter: """Generates overlapping kmer counts for a fasta file Parameters ---------- infasta: str (default=None) Full path to fasta file to be counted outfile: str (default=None) Full path to the counts file to be saved k: int (default=6) Size of kmer to be counted binary: bool (default=True) Saves as numpy array if True, else saves as csv
class TestState(util.SubscribableStateMixin): """This class handles tracking the state of a running Test. This class encapsulates all the interesting transient bits of a running Test, as opposed to the openhtf.TestDescriptor class, which encapsulates static data associated with a Test (that is, it remains the same across invocations of Test.Execute()). Init Args: test_desc: openhtf.TestDescriptor instance describing the test to run, used to initialize some values here, but it is not modified. Attributes: test_record: TestRecord instance for the currently running test. logger: Logger that logs to test_record's log_records attribute. running_phase_state: PhaseState object for the currently running phase, if any, otherwise None. user_defined_state: Dictionary for users to persist state across phase invokations. It's passed to the user via test_api. test_api: An openhtf.TestApi instance for passing to test phases, providing test authors access to necessary state information, while protecting internal-only structures from being accidentally modified. Note that if there is no running phase, test_api is also None. execution_uid: A UUID that is specific to this execution. """ Status = Enum('Status', ['WAITING_FOR_TEST_START', 'RUNNING', 'COMPLETED']) def __init__(self, test_desc, execution_uid): super(TestState, self).__init__() self._status = self.Status.WAITING_FOR_TEST_START self.test_record = test_record.TestRecord( dut_id=None, station_id=conf.station_id, code_info=test_desc.code_info, # Copy metadata so we don't modify test_desc. metadata=copy.deepcopy(test_desc.metadata)) self.logger = logs.initialize_record_logger(execution_uid, self.test_record, self.notify_update) self.plug_manager = plugs.PlugManager(test_desc.plug_types, self.logger) self.running_phase_state = None self.user_defined_state = {} self.execution_uid = execution_uid @property def test_api(self): """Create a TestApi for access to this TestState. The returned TestApi should be passed as the first argument to test phases. Note that the return value is none if there is no self.running_phase_state set. As such, this attribute should only be accessed within a RunningPhaseContext(). """ running_phase_state = self.running_phase_state return (running_phase_state and openhtf.TestApi( self.logger, self.user_defined_state, self.test_record, measurements.Collection(running_phase_state.measurements), running_phase_state.attachments, running_phase_state.attach, running_phase_state.attach_from_file, self.notify_update)) @contextlib.contextmanager def running_phase_context(self, phase_desc): """Create a context within which a single phase is running. Yields a PhaseState object for tracking transient state during the execution of the phase, including the output PhaseRecord. That PhaseState provides the TestApi to be passed into the test phase. Within this context, the Station API will report the given phase as the currently running phase. """ assert not self.running_phase_state, 'Phase already running!' self.running_phase_state = PhaseState.from_descriptor( phase_desc, self.notify_update) try: with self.running_phase_state.record_timing_context: self.notify_update() # New phase started. yield self.running_phase_state finally: # Clear notification callbacks so we can serialize measurements. for meas in self.running_phase_state.measurements.values(): meas.set_notification_callback(None) self.test_record.phases.append( self.running_phase_state.phase_record) self.running_phase_state = None self.notify_update() # Phase finished. def _asdict(self): """Return a dict representation of the test's state.""" return { 'status': self._status, 'test_record': self.test_record, 'plugs': self.plug_manager._asdict(), 'running_phase_state': self.running_phase_state, } @property def is_finalized(self): return self._status == self.Status.COMPLETED @property def last_run_phase_name(self): """Get the name of the currently running phase, or None. Note that this name is not guaranteed to still be accurate by the time this method returns, so this should only be used for log messages/user display and not for programmatic purposes. """ return self.running_phase_state and self.running_phase_state.name def set_status_from_phase_outcome(self, phase_outcome): """Set our internal state based on the given phase outcome. Args: phase_outcome: An instance of phase_executor.PhaseOutcome Returns: True if the test has finished prematurely (failed). """ assert not self.is_finalized, 'Test already completed!' # Handle a few cases where the test is ending prematurely. if phase_outcome.raised_exception: self.logger.debug('Finishing test execution early due to phase ' 'exception, outcome ERROR.') result = phase_outcome.phase_result if isinstance(result, phase_executor.ExceptionInfo): code = result.exc_type.__name__ description = str(result.exc_val).decode('utf8', 'replace') else: # threads.ThreadTerminationError gets str'd directly. code = str(type(phase_outcome.phase_result).__name__) description = str(phase_outcome.phase_result).decode( 'utf8', 'replace') self.test_record.add_outcome_details(code, description) self.finalize(test_record.Outcome.ERROR) elif phase_outcome.is_timeout: self.logger.debug('Finishing test execution early due to phase ' 'timeout, outcome TIMEOUT.') self.finalize(test_record.Outcome.TIMEOUT) elif phase_outcome.phase_result == openhtf.PhaseResult.STOP: self.logger.debug('Finishing test execution early due to ' 'PhaseResult.STOP, outcome FAIL.') # TODO(madsci): Decouple flow control from pass/fail. self.finalize(test_record.Outcome.ABORTED) if self.is_finalized != phase_outcome.is_terminal: raise openhtf.InvalidTestStateError( 'Unexpected finalized state (%s) after PhaseOutcome %s.', self.is_finalized, phase_outcome) return self.is_finalized def mark_test_started(self): """Set the TestRecord's start_time_millis field.""" # Blow up instead of blowing away a previously set start_time_millis. assert self.test_record.start_time_millis is 0 self.test_record.start_time_millis = util.time_millis() self.notify_update() def set_status_running(self): """Mark the test as actually running, can't be done once finalized.""" assert self._status == self.Status.WAITING_FOR_TEST_START self._status = self.Status.RUNNING self.notify_update() def finalize(self, test_outcome=None): """Mark the state as finished. This method is called with no arguments on normal test completion, or with an argument if the test stopped under some other condition, where the test_outcome argument specifies what the Test's outcome was. When a Test completes normally, the outcome will be either PASS or FAIL, depending on measurements' PASS/FAIL status. Any UNSET measurements will cause the Test to FAIL unless conf.allow_unset_measurements is set True. Args: test_outcome: If specified, use this as the Test outcome. """ assert not self.is_finalized, 'Test already completed!' # Sanity check to make sure we have a DUT ID by the end of the test. if not self.test_record.dut_id: raise BlankDutIdError( 'Blank or missing DUT ID, HTF requires a non-blank ID.') if test_outcome: # Override measurement-based PASS/FAIL with a specific test outcome. self.test_record.outcome = test_outcome else: allowed_outcomes = {measurements.Outcome.PASS} if conf.allow_unset_measurements: allowed_outcomes.add(measurements.Outcome.UNSET) if any(meas.outcome not in allowed_outcomes for phase in self.test_record.phases for meas in phase.measurements.itervalues()): self.test_record.outcome = test_record.Outcome.FAIL else: self.test_record.outcome = test_record.Outcome.PASS # A message has already been logged if we were called with test_outcome # set, but if we're finishing normally, log it here. self.logger.debug( 'Finishing test execution normally with outcome %s.', self.test_record.outcome.name) # The test is done at this point, no further updates to test_record. self.logger.handlers = [] self.test_record.end_time_millis = util.time_millis() self._status = self.Status.COMPLETED self.notify_update() def __str__(self): return '<%s: %s@%s Running Phase: %s>' % ( type(self).__name__, self.test_record.dut_id, self.test_record.station_id, self.last_run_phase_name, )
# along with this program. If not, see <http://www.gnu.org/licenses/>. """ Usage: streamparser.py [FILE] Consumes input from a file (first argument) or stdin, parsing and pretty printing the readings of lexical units found. """ import re, pprint, sys, itertools, fileinput from enum import Enum from collections import namedtuple Knownness = Enum('Knownness', 'known unknown biunknown genunknown') try: Knownness.__doc__ = """Level of knowledge associated with a lexical unit. Values: known unknown: Denoted by '*', analysis not available. biunknown: Denoted by '@', translation not available. genunknown: Denoted by '#', generated form not available. """ except AttributeError: # Python 3.2 users have to read the source pass SReading = namedtuple('SReading', ['baseform', 'tags']) try: SReading.__doc__ = """A single subreading of an analysis of a token.
# coding: utf-8 from enum import Enum import time PizzaProgress = Enum('PizzaProgress', 'queued preparation baking ready') PizzaDough = Enum('PizzaDough', 'thin thick') PizzaSauce = Enum('PizzaSauce', 'tomato creme_fraiche') PizzaTopping = Enum('PizzaTopping', 'mozzarella double_mozzarella bacon ham mushrooms red_onion oregano') STEP_DELAY = 3 # 考虑是示例,单位为秒 # 这里没有一个AbstractBuilder class Pizza: def __init__(self, name): self.name = name self.dough = None self.sauce = None self.topping = [] def __str__(self): return self.name def prepare_dough(self, dough): self.dough = dough print('preparing the {} dough of your {}...'.format(self.dough.name, self)) time.sleep(STEP_DELAY) print('done with the {} dough'.format(self.dough.name)) # ConcreateBuilder
# coding=utf-8 """ Evaluation (t-tree comparison functions). """ from __future__ import unicode_literals from collections import defaultdict from enum import Enum from tgen.logf import log_debug from tgen.tree import TreeData, TreeNode import numpy as np from alex.components.nlg.tectotpl.core.node import T EvalTypes = Enum(b'EvalTypes', b'NODE DEP') EvalTypes.__doc__ = """Evaluation flavors (node-only, dependency)""" def collect_counts(ttree, eval_type=EvalTypes.NODE): """Collects counts of different node/dependency types in the given t-tree. @param ttree: the tree to collect counts from @param eval_type: if set to 'node' (default), count nodes (formemes, lemmas, dependency \ direction), if set to 'dep', count dependencies (including parent's formeme, lemma, \ dependency direction). @rtype: defaultdict """ counts = defaultdict(int) for node in ttree.get_descendants(): if eval_type == EvalTypes.NODE:
"""Returns a function that prepends the supplied prefix and converts underscores to the dashes required by Cloud constants.. """ def inner(name: str) -> str: return "{}-{}".format(prefix, name.replace('_', '-')) return inner US_REGIONS = {"west1", "west2", "central1", "east1", "east4"} EURO_REGIONS = {"west1", "west4", "north1"} ASIA_REGIONS = {"southeast1", "east1", "northeast1"} # Actual enum types. US = Enum('US', u.dict_by(US_REGIONS, _vfn("us"))) Europe = Enum('Europe', u.dict_by(EURO_REGIONS, _vfn("europe"))) Asia = Enum('Asia', u.dict_by(ASIA_REGIONS, _vfn("asia"))) Region = Union[US, Europe, Asia] Zone = str def valid_regions(zone: Optional[str] = None) -> List[Region]: """Returns valid region strings for Cloud, for the globe or for a particular region if specified. """ if zone is None: return valid_regions("americas") \ + valid_regions("europe") \ + valid_regions("asia")
__author__ = 'Kaike' from models import * from enum import Enum ## kind tKinds = ["NO_KIND_DEF", "VAR_", "PARAM_", "FUNCTION_", "FIELD_", "ARRAY_TYPE_", "STRUCT_TYPE_", "ALIAS_TYPE_", "SCALAR_TYPE_" , "UNIVERSAL_"] Kinds = Enum._create_('Kinds', tKinds, None, int) int_ = Object(-1, None, Kinds.SCALAR_TYPE_) char_ = Object(-1, None, Kinds.SCALAR_TYPE_) bool_ = Object(-1, None, Kinds.SCALAR_TYPE_) string_ = Object(-1, None, Kinds.SCALAR_TYPE_) universal_ = Object(-1, None, Kinds.SCALAR_TYPE_) def IS_TYPE_KIND(k): return k == Kinds.ARRAY_TYPE_ or k == Kinds.STRUCT_TYPE_ or k == Kinds.ALIAS_TYPE_ or k == Kinds.SCALAR_TYPE_ def CheckTypes(self, t1, t2): if(t1 == t2): return True elif(t1 == universal_ or t2 == universal_): return True elif(t1.eKind == Kinds.UNIVERSAL_ or t2.eKind == Kinds.UNIVERSAL_): return True elif(t1.eKind == Kinds.ALIAS_TYPE_ and t2.eKind != Kinds.ALIAS_TYPE_):
'Const', 'Gather', 'StridedSlice', 'Slice', 'ReverseV2', 'Stack', 'Pack', 'Unstack', 'Unpack', 'Cast', 'ArgMax', 'Split', 'FakeQuantWithMinMaxVars', ] TFOpType = Enum('TFOpType', [(op, op) for op in TFSupportedOps], type=str) TFSupportedOps = [six.b(op) for op in TFSupportedOps] TFTransformGraphOptions = { base_converter.DeviceType.CPU.value: [ 'strip_unused_nodes', 'remove_nodes(op=Identity, op=CheckNumerics)', 'fold_constants(ignore_errors=true)', 'fold_batch_norms', 'fold_old_batch_norms', 'remove_control_dependencies', 'strip_unused_nodes', 'sort_by_execution_order' ], base_converter.DeviceType.GPU.value: [ 'strip_unused_nodes', 'remove_nodes(op=Identity, op=CheckNumerics)', 'fold_constants(ignore_errors=true)', 'flatten_atrous_conv', 'fold_batch_norms', 'fold_old_batch_norms', 'remove_control_dependencies', 'strip_unused_nodes',
# $Id: core.py 677 2012-01-12 17:46:11Z gnoel $ import datetime import calendar import re from enum import Enum modifiers = Enum() modifiers.addElement('DEFAULT', {'symbol': ''}) modifiers.addElement('CIRCA', {'symbol': 'c. '}) modifiers.addElement('UNCERTAIN', {'symbol': '?'}) # date1 <= date <= date2 # represents an approximate date into an inclusive date range # Please run the regression test after modifying the code to make sure it is bug-free # See tester.py # # Todo: # comparison # support 196* or 12** format # notes # before X # after X # BC # less than 4 digits for the year # class FuzzyDate(object): dates = [datetime.date.today(), datetime.date.today()] modifier = modifiers.DEFAULT lastError = u'' # if False the format is iso-8109 ukFormat = True
'arm64', 'armhf', 'host', ] ModelFormatStrs = [ "file", "code", ] PlatformTypeStrs = [ "tensorflow", "caffe", "onnx", ] PlatformType = Enum('PlatformType', [(ele, ele) for ele in PlatformTypeStrs], type=str) RuntimeTypeStrs = ["cpu", "gpu", "dsp", "hta", "apu", "cpu+gpu"] InOutDataTypeStrs = [ "int32", "float32", ] InOutDataType = Enum('InputDataType', [(ele, ele) for ele in InOutDataTypeStrs], type=str) FPDataTypeStrs = [ "fp16_fp32", "fp32_fp32",
import math import threading import sys from random import choice from enum import Enum # DFS Meta Data CHUNK_SIZE = 2 * 1024 * 1024 BUFSIZE = 4 * 1024 * 1024 NUM_DATA_SERVER = 4 NUM_REPLICATION = 4 # Data Node Addr DATA_NODE_ADDR = [ '121.37.138.6', '124.70.177.49', '124.70.128.29', '124.70.153.253' ] DATA_NODE_PORT = 20000 CHUNK_PATTERN = 'file-%s-%s' TIME_OUT = 3 SYSTEM_Mode = 'DISTRIBUTED' # LOCAL/DISTRIBUTED 本地/分布式 # Name Node Meta Data NAME_NODE_META_PATH = './dfs/namenode/meta.pkl' # Local Data Node DATA_NODE_DIR = './dfs/datanode%s' # Operations operation_names = ('put', 'read', 'fetch', 'quit', 'ls', 'delete', 'll') COMMAND = Enum('COMMAND', operation_names)
def run_mpi_sim(args, numbermodelruns, inputfile, usernamespace, optparams=None): """Run mixed mode MPI/OpenMP simulation - MPI task farm for models with each model parallelised with OpenMP Args: args (dict): Namespace with command line arguments numbermodelruns (int): Total number of model runs. inputfile (str): Name of the input file to open. usernamespace (dict): Namespace that can be accessed by user in any Python code blocks in input file. optparams (dict): Optional argument. For Taguchi optimisation it provides the parameters to optimise and their values. """ from mpi4py import MPI # Define MPI message tags tags = Enum('tags', {'READY': 0, 'DONE': 1, 'EXIT': 2, 'START': 3}) # Initializations and preliminaries comm = MPI.COMM_WORLD # get MPI communicator object size = comm.Get_size() # total number of processes rank = comm.Get_rank() # rank of this process status = MPI.Status() # get MPI status object name = MPI.Get_processor_name() # get name of processor/host tsimstart = perf_counter() # Master process if rank == 0: modelrun = 1 numworkers = size - 1 closedworkers = 0 print('Master: PID {} on {} using {} workers.'.format( os.getpid(), name, numworkers)) while closedworkers < numworkers: data = comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status) source = status.Get_source() tag = status.Get_tag() if tag == tags.READY.value: # Worker is ready, so send it a task if modelrun < numbermodelruns + 1: comm.send(modelrun, dest=source, tag=tags.START.value) print('Master: sending model {} to worker {}.'.format( modelrun, source)) modelrun += 1 else: comm.send(None, dest=source, tag=tags.EXIT.value) elif tag == tags.DONE.value: print('Worker {}: completed.'.format(source)) elif tag == tags.EXIT.value: print('Worker {}: exited.'.format(source)) closedworkers += 1 # Worker process else: print('Worker {}: PID {} on {}.'.format(rank, os.getpid(), name)) while True: comm.send(None, dest=0, tag=tags.READY.value) modelrun = comm.recv( source=0, tag=MPI.ANY_TAG, status=status ) # Receive a model number to run from the master tag = status.Get_tag() # Run a model if tag == tags.START.value: if optparams: # If Taguchi optimistaion, add specific value for each parameter to optimise for each experiment to user accessible namespace tmp = {} tmp.update((key, value[modelrun - 1]) for key, value in optparams.items()) modelusernamespace = usernamespace.copy() modelusernamespace.update({'optparams': tmp}) else: modelusernamespace = usernamespace run_model(args, modelrun, numbermodelruns, inputfile, modelusernamespace) comm.send(None, dest=0, tag=tags.DONE.value) elif tag == tags.EXIT.value: break comm.send(None, dest=0, tag=tags.EXIT.value) tsimend = perf_counter() simcompletestr = '\n=== Simulation completed in [HH:MM:SS]: {}'.format( datetime.timedelta(seconds=int(tsimend - tsimstart))) print('{} {}\n'.format( simcompletestr, '=' * (get_terminal_width() - 1 - len(simcompletestr))))
import trp from typing import List, Optional from tabulate import tabulate from enum import Enum from io import StringIO import csv import logging logger = logging.getLogger(__name__) Textract_Pretty_Print = Enum('Textract_Pretty_Print', ["WORDS", "LINES", "FORMS", "TABLES"], start=0) Pretty_Print_Table_Format = Enum('Pretty_Print_Table_Format', [ "csv", "plain", "simple", "github", "grid", "fancy_grid", "pipe", "orgtbl", "jira", "presto", "pretty", "psql", "rst", "mediawiki", "moinmoin", "youtrack", "html", "unsafehtml", "latex", "latex_raw", "latex_booktabs", "latex_longtable", "textile", "tsv" ]) def get_string( textract_json: dict, output_type: Optional[List[Textract_Pretty_Print]] = None, table_format: Pretty_Print_Table_Format = Pretty_Print_Table_Format.github ): result_value = "" for t in output_type: if t == Textract_Pretty_Print.WORDS: result_value += get_words_string(textract_json=textract_json) if t == Textract_Pretty_Print.LINES:
BG_DIR = os.path.join(GFX_DIR, "bg") BGD_DIR = os.path.join(GFX_DIR, "bgd") CUTIN_DIR = os.path.join(GFX_DIR, "cutin") EVENT_DIR = os.path.join(GFX_DIR, "events") FLASH_DIR = os.path.join(GFX_DIR, "flash") FONT_FOLDER = os.path.join(GFX_DIR, "font") MENU_DIR = os.path.join(GFX_DIR, "menu") MOVIEFRAME_DIR = os.path.join(GFX_DIR, "movieframes") # Full-sized background frames MOVIE_DIR = os.path.join(GFX_DIR, "movies") # Icons for the movie gallery NAMETAG_DIR = os.path.join(GFX_DIR, "nametags") PRESENT_DIR = os.path.join(GFX_DIR, "presents") SPRITE_DIR = os.path.join(GFX_DIR, "sprites") TEXTBOX_DIR = os.path.join(GFX_DIR, "textbox") TRIAL_DIR = os.path.join(GFX_DIR, "trial") TEXT_ALIGN = Enum("left", "right", "center", "offcenter") #TEXT_V_ALIGN = Enum("normal", "nonstop") IMG_FILTERS = Enum("unfiltered", "sepia", "inverted") #SCENE_MODES = Enum("normal", "trial", "rules", "ammo", "ammoname", "present", "presentname", "debate", "mtb", "climax", "anagram", "menu", "map", "report", "report2", "skill", "skill2", "music", "eventname", "moviename", "theatre", "help", "other") TEXT_FORMAT = { common.SCENE_MODES.normal: {"x": 18, "y": 202, "w": 444, "h": 24, "a": TEXT_ALIGN.left, "clt": 0, "killblanks": True}, common.SCENE_MODES.trial: {"x": 18, "y": 202, "w": 444, "h": 24, "a": TEXT_ALIGN.left, "clt": 0, "killblanks": True}, common.SCENE_MODES.rules: {"x": 32, "y": 159, "w": 416, "h": 18, "a": TEXT_ALIGN.center, "clt": 0, "killblanks": True}, common.SCENE_MODES.ammo: {"x": 247, "y": 72, "w": 185, "h": 14, "a": TEXT_ALIGN.left, "clt": 7, "killblanks": False}, common.SCENE_MODES.ammoname: {"x": 32, "y": 199, "w": 200, "h": 14, "a": TEXT_ALIGN.center, "clt": 7, "killblanks": True}, common.SCENE_MODES.ammosummary: {"x": 41, "y": 192, "w": 200, "h": 12, "a": TEXT_ALIGN.left, "clt": 7, "killblanks": False}, common.SCENE_MODES.present: {"x": 247, "y": 72, "w": 185, "h": 14, "a": TEXT_ALIGN.left, "clt": 7, "killblanks": False}, common.SCENE_MODES.presentname: {"x": 32, "y": 199, "w": 200, "h": 14, "a": TEXT_ALIGN.center, "clt": 7, "killblanks": True}, common.SCENE_MODES.debate: {"x": 18, "y": 160, "w": 444, "h": 24, "a": TEXT_ALIGN.center, "clt": 8, "killblanks": True}, common.SCENE_MODES.mtb: {"x": 18, "y": 160, "w": 444, "h": 24, "a": TEXT_ALIGN.center, "clt": 12, "killblanks": True},
, 'uint': 'u' , 'float': '' , 'double': 'd' } __machine_types__ = {'bool': dtype('uint32') ,'int': dtype('int32') ,'uint': dtype('uint32') ,'float': dtype('float32') ,'double': dtype('float64')} def __init__(self, value): self.prefix = self.__prefixes__[self.name] self.machine_type = self.__machine_types__[self.name] self.scalar_type = self self.opaque = False scalar_doc = Scalar.__doc__ Scalar = Enum('Scalar', ((s, s) for s in scalar_types), type=Scalar) Scalar.__doc__ = scalar_doc floating_point_scalars = { Scalar.float, Scalar.double } sampler_dims = range(1, 4) sampler_data_types = {Scalar.float, Scalar.int, Scalar.uint} sampler_types = [ "{}sampler{}D".format(scalar_type.prefix, ndim) for scalar_type, ndim in cartesian(sampler_data_types, sampler_dims) ] class Sampler(str, BasicType, Enum): '''The GLSL sampler types. Scalars difine the following attributes: *opaque* Whether the datatype is an opaque type (:py:obj:`True`)
:License: Modified BSD, see LICENSE for details. """ from enum import Enum from flask import current_app EXTENSION_KEY = 'byceps' KEY_SITE_MODE = 'site_mode' KEY_PARTY_ID = 'party_id' KEY_TICKET_MANAGEMENT_ENABLED = 'ticket_management_enabled' KEY_USER_REGISTRATION_ENABLED = 'user_registration_enabled' SiteMode = Enum('SiteMode', ['public', 'admin']) SiteMode.is_admin = lambda self: self == SiteMode.admin SiteMode.is_public = lambda self: self == SiteMode.public def init_app(app): app.extensions[EXTENSION_KEY] = {} site_mode = determine_site_mode(app) update_extension_value(app, KEY_SITE_MODE, site_mode) if site_mode.is_public(): party_id = determine_party_id(app) update_extension_value(app, KEY_PARTY_ID, party_id) user_registration_enabled = determine_user_registration_enabled(app,
# -*- coding: utf-8 -*- # _author_='HAN' import os import const from enum import Enum, unique @unique class LimitedOutputDirective(Enum): notLimited = 0 limitedOutput = 1 table_type_t = Enum('table_type_t', ('byPtr', 'byU32', 'byU16')) dict_directive = Enum('dict_directive', ('noDict', 'withPrefix64k', 'usingExtDict')) dict_directive.noDict = 0 dictIssue_directive = Enum('dictIssue_directive', ('noDictIssue', 'dictSmall')) dictIssue_directive.noDictIssue = 0 class EndConditionDirective(Enum): endOnOutputSize = 0 endOnInputSize = 1 class EarlyEndDirective(Enum): full = 0 partial = 1 const.memory_usage = 14 const.lz4_hash_log = 12 const.hash_table_size = 2 ^ 14
from enum import Enum import threading import time CaptureMode = Enum('CaptureMode', 'Win32UI mss') CAPTURE_MODE = CaptureMode.Win32UI if CAPTURE_MODE == CaptureMode.Win32UI: from Model.ImageCaptureWin32UI import ImageCapture else: from Model.ImageCaptureMSS import ImageCapture import Networking.StoppableThread as StoppableThread class ThreadedWinCapture(StoppableThread.StoppableThread): def __init__(self, *args): self.captureArgs = None self.currentImage = None self.imageId = 0 self.captureArgLock = threading.Lock() self.imageLock = threading.Lock() super().__init__(*args) def setCaptureArgs(self, captureArgs): self.captureArgLock.acquire() self.captureArgs = captureArgs self.captureArgLock.release() def getCaptureArgs(self): self.captureArgLock.acquire()
@enum.unique class Patterns(Enum): """ Defines some known regex pattern templates for common types of inline syntax. For example, a single group inline element (one capture group framed by something before and after the group). """ escape = '(?<!\\\\)(?:\\\\\\\\)*{0}' single_group = '(?<!\\\\)(?:\\\\\\\\)*\\K{0}(.*?(?<!\\\\)(?:\\\\\\\\)*){1}' link = r'(?<!\\)(?:\\\\)*\K{0}\[(.*?(?<!\\)(?:\\\\)*)\]\((.*?(?<!\\)(?:\\\\)*)\)' double_group = r'(?<!\\)(?:\\\\)*\K\{0}(.*?(?<!\\)(?:\\\\)*){1}(.*?(?<!\\)(?:\\\\)*){2}' Nesting = Enum('Nesting', 'FRAME POST SUB NONE') Nesting.__doc__ = """ FRAME: element is intended to contain/frame the inside text, which means that subscriptions should be inherited from the parent. POST: text in the block should be parsed AFTER this block is parsed. This is the default, and is suitable for most situations. SUB: the inside of the text is parsed for child nodes (inline and block) first, and the corresponding sections are replaced with [|*|] style tags that are meant to be left UNTOUCHED. After this block is parsed, then the tags are replaced with the appropriate parsed sections. This could have also been called 'PRE', since it pre-parses the contents before calling the block's parsing function. NONE: terminal element. The parser's output is taken verbatim, with out any
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.""" __revision__ = " $Id: LingData.py 2 2017-07-25 14:32:00Z damir $ " __docformat__ = 'reStructuredText' __author__ = 'Damir Cavar <*****@*****.**>, Atreyee M.' __version__ = '0.1' from enum import Enum #import PSTree labels = Enum( 'labels', 'id word lemma POS SPOS NER foreign isReferent hasAntecedent RefText RefS RefFrom RefTo RefHead' ) wtypes = Enum('wtypes', 'verb noun pronoun copula unknown') def main(): pass class Document(): """This is the main class that holds documents, which consist of a set of sentences. """ def __init__(self): self.sentences = [] #: list of sentences self.sIDs = { } #: dictionary with sentence IDs as keys and the sentence number in the list as value
def setUp(self): super(TestEnum,self).setUp() self.Colors = Enum('Colors',['Red','Green','Blue'])
except Exception as e: log.warning("Failed to convert attribute '%s'='%s': %s" % \ (name, value, e)) return default def _write_xml_bbox(bbox, parent_elem): x, y, w, h = bbox bbox_elem = ET.SubElement(parent_elem, 'bndbox') ET.SubElement(bbox_elem, 'xmin').text = str(x) ET.SubElement(bbox_elem, 'ymin').text = str(y) ET.SubElement(bbox_elem, 'xmax').text = str(x + w) ET.SubElement(bbox_elem, 'ymax').text = str(y + h) return bbox_elem LabelmapType = Enum('LabelmapType', ['voc', 'source']) class VocConverter(Converter): DEFAULT_IMAGE_EXT = VocPath.IMAGE_EXT BUILTIN_ATTRS = {'difficult', 'pose', 'truncated', 'occluded' } @staticmethod def _split_tasks_string(s): return [VocTask[i.strip()] for i in s.split(',')] @staticmethod def _get_labelmap(s): if osp.isfile(s): return s try: return LabelmapType[s].name
#枚举类型 from enum import Enum Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'oct', 'Nov', 'Dec')) for name, member in Month.__members__.items(): print(name, '=>', member, ',', member.value)
class VW_EGolf(Device, HTTP): pass """ the main class for the Golf charge device /!\ actually unable to work """ BASE = "http://{h}/r?rapi=%24" Cmd = Enum("Cmd", "sleep reset enable disable") __CmdToUrl = { Cmd.sleep: "FS", Cmd.reset: "FR", Cmd.enable: "FE", Cmd.disable: "FD" } def __init__(self, host): """ Construtor of the class VW_EGolf: :param host: the host of the charger :type host: string """ Device.__init__(self) BASE = BASE.format(h=host) self.__charging_rate = self.get_charging_rate() def command(self, cmd): """ command to the charger: :param cmd: the command to pass to the charger :type cmd: Cmd enum :return: if it has been done or not :rtype: bool """ pass url = BASE + __CmdToUrl[cmd] result = self._get(url) def get_charging_rate(self): """charging rate getter from the charger""" pass url = BASE + "GC" result = self._get(url) #sort infos to get the value and return it @property def charging_rate(self): return __charging_rate @charging_rate.setter def set_charging_rate(self, charging_rate): """ change the charging rate in ampere: :param charging_rate: new charging rate :type charging_rate: int :return: if it has been done or not :rtype: bool """ pass url = BASE + "SC+" + str(amp) result = self._get(url)
'IB_EXACT_PREFIX', ]) ib_match_mode_t = c_int # XXX: unsure class ib_col_meta_t(Structure): _fields_ = [ ('type', ib_col_type_t), ('attr', ib_col_attr_t), ('type_len', ib_u32_t), ('client_type', ib_u16_t), ('charset', POINTER(ib_charset_t)), ] ib_trx_state = Enum(globals(), [ 'IB_TRX_NOT_STARTED', 'IB_TRX_ACTIVE', 'IB_TRX_COMMITTED_IN_MEMORY', 'IB_TRX_PREPARED', ]) ib_trx_state_t = c_int # XXX: unsure ib_trx_level = Enum(globals(), [ ('IB_TRX_READ_UNCOMMITTED', 0), ('IB_TRX_READ_COMMITTED', 1), ('IB_TRX_REPEATABLE_READ', 2), ('IB_TRX_SERIALIZABLE', 3), ]) ib_trx_level_t = c_int # XXX: unsure ib_shutdown = Enum(globals(), [ 'IB_SHUTDOWN_NORMAL', 'IB_SHUTDOWN_NO_IBUFMERGE_PURGE',
def pad(self, word_amount: int, char_amount: int) -> None: if word_amount >= 0: self.X_words = np.pad(self.X_words, [(0, 0), (0, word_amount)], "constant") else: self.X_words = self.X_words[:, :word_amount] if char_amount >= 0: self.X_chars = np.pad(self.X_chars, [(0, 0), (0, char_amount)], "constant") else: self.X_chars = self.X_chars[:, :char_amount] ClusterHandling = Enum("ClusterHandling", "CONCAT MEAN CENTER") class GermanDataset(Dataset): def __init__( self, files: List[str], gmm_files: List[str], num_clusterlabels: int, num_gmm_clusters: int, window_before: int, window_after: int, word_vocab: Optional[Vocab] = None, char_vocab: Optional[Vocab] = None, bag_of_words: bool = False, cluster_handling: ClusterHandling = ClusterHandling.CONCAT