Python re.split() Examples

The following are 30 code examples of re.split(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module re , or try the search function .
Example #1
Source File: __init__.py    From aegea with Apache License 2.0 8 votes vote down vote up
def validate_hostname(hostname):
    if len(hostname) > 255:
        raise Exception("Hostname {} is longer than 255 characters".format(hostname))
    if hostname[-1] == ".":
        hostname = hostname[:-1]
    allowed = re.compile(r"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
    if not all(allowed.match(x) for x in hostname.split(".")):
        raise Exception("Hostname {} is not RFC 1123 compliant".format(hostname)) 
Example #2
Source File: gitgot.py    From GitGot with GNU Lesser General Public License v3.0 7 votes vote down vote up
def gist_fetch(query, page_idx, total_items=1000):
    gist_url = "https://gist.github.com/search?utf8=%E2%9C%93&q={}&p={}"
    query = urllib.parse.quote(query)
    gists = []

    try:
        resp = requests.get(gist_url.format(query, page_idx))
        soup = bs4.BeautifulSoup(resp.text, 'html.parser')
        total_items = min(total_items, int(
            [x.text.split()[0] for x in soup.find_all('h3')
                if "gist results" in x.text][0].replace(',', '')))
        gists = [x.get("href") for x in soup.findAll(
                            "a", class_="link-overlay")]
    except IndexError:
        return {"data": None, "total_items": 0}

    return {"data": gists, "total_items": total_items} 
Example #3
Source File: reportMetrics.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
def extract_fields_db2(obj, line, field_name_regex):
    line = '#'.join(re.split('\s*#', line))

    last_key = ''
    field_names = re.findall(field_name_regex, line)
    for field in reversed(field_names):
        split_at = line.find(field) + len(field)
        field_name = re.split('\s*:', field)[0]
        # don't overwrite existing fields
        if field_name in obj:
            continue
        else:
            obj[field_name] = ' '.join(line[split_at:].split())
            if not last_key:
                last_key = field_name
        line = line[:split_at - len(field)]
    return last_key 
Example #4
Source File: ksp_compiler.py    From SublimeKSP with GNU General Public License v3.0 6 votes vote down vote up
def prefix_with_ns(name, namespaces, function_parameter_names=None, force_prefixing=False):
    if not namespaces:
        return name
    function_parameter_names = function_parameter_names or []
    ##name = name.replace('.', '__') # replace . by __

    if name[0] in variable_prefixes:
        prefix, unprefixed_name = name[0], name[1:]
    else:
        prefix, unprefixed_name = '', name

    # if the name consists of multiple parts (eg. myfamily.myvariable extract the first part - myfamily in this example)
    first_name_part = name.split('.')[0]

    # if built-in name or function parameter
    if (unprefixed_name in ksp_builtins.variables_unprefixed or
          name in ksp_builtins.functions or
          name in ksp_builtins.keywords or
          first_name_part in function_parameter_names) and not force_prefixing:
        return name # don't add prefix

    # add namespace to name
    return prefix + '.'.join(namespaces + [unprefixed_name]) 
Example #5
Source File: yamlparser.py    From py2swagger with MIT License 6 votes vote down vote up
def _parse_docstring(self, docstring=''):
        """
        :param docstring:
        :return: (summary, description, schema)
        """
        summary, description, schema = None, None, dict()
        docstring = docstring.strip()

        if '---' in docstring:
            head, yml = re.split(r'\s*---+\s*\n', docstring)
            if yml:
                schema = self.yaml_load(yml) or dict()
        else:
            head = docstring

        if '\n' in head.strip():
            summary, description = map(lambda s: s.strip(), head.split('\n', 1))
        elif head:
            summary = head.strip()

        return summary, description, schema 
Example #6
Source File: wireless.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def get_cipher_list(encryption_mode):
    """
    Get list of ciphers from encryption mode.

    Example:
    get_cipher_list("psk2+tkip+aes") -> ["TKIP", "CCMP"]
    """
    parts = encryption_mode.lower().split('+')
    ciphers = []

    if "tkip" in parts:
        ciphers.append("TKIP")
    if "ccmp" in parts or "aes" in parts:
        ciphers.append("CCMP")

    if len(ciphers) == 0:
        # We need to enable at least one cipher. Most modes default to CCMP
        # except for wpa.
        if parts[0] == "wpa":
            ciphers.append("TKIP")
        else:
            ciphers.append("CCMP")

    return ciphers 
Example #7
Source File: preprocessor_plugins.py    From SublimeKSP with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, name, uiType, size, persistence, familyPrefix, uiParams, tableSize, prefixSymbol, line):
		self.name = name
		self.familyPrefix = familyPrefix or ""
		self.uiType = uiType
		self.prefixSymbol = prefixSymbol
		if self.uiType == "ui_text_edit":
			self.prefixSymbol = "@"
		self.uiParams = uiParams or ""
		self.numElements = size
		self.dimensionsString = size
		self.underscore = ""
		if "," in size:
			self.underscore = "_"
			self.numElements = "*".join(["(%s)" % dim for dim in size.split(",")])
		self.numElements = tryStringEval(self.numElements, line, "UI array size")
		self.persistence = persistence or ""
		self.tableSize = tableSize 
Example #8
Source File: wmt_utils.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def basic_tokenizer(sentence):
  """Very basic tokenizer: split the sentence into a list of tokens."""
  words = []
  if old_style:
    for space_separated_fragment in sentence.strip().split():
      words.extend(re.split(_OLD_WORD_SPLIT, space_separated_fragment))
    return [w for w in words if w]
  for space_separated_fragment in sentence.strip().split():
    tokens = [t for t in re.split(_WORD_SPLIT, space_separated_fragment) if t]
    first_is_char = False
    for i, t in enumerate(tokens):
      if len(t) == 1 and t in _PUNCTUATION:
        tokens[i] = _CHAR_MARKER + t
        if i == 0:
          first_is_char = True
    if words and words[-1] != _SPACE and (first_is_char or is_char(words[-1])):
      tokens = [_SPACE] + tokens
    spaced_tokens = []
    for i, tok in enumerate(tokens):
      spaced_tokens.append(tokens[i])
      if i < len(tokens) - 1:
        if tok != _SPACE and not (is_char(tok) or is_char(tokens[i+1])):
          spaced_tokens.append(_SPACE)
    words.extend(spaced_tokens)
  return words 
Example #9
Source File: collateAllUniqueDetections.py    From EXOSIMS with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def multiRunPostProcessing(self, PPoutpath, folders):
        outtext = list()
        for folder in folders:
            lines = list()
            with open(os.path.join(folder,'NEIDinfo.txt'), 'r') as g: #Write to file
                lines = g.read().split('\n')[0:-1]
            lines2 = [line.split(',') for line in lines]
            try:
                lines2 = lines2.remove([''])
            except:
                pass
            lines3 = [','.join(line) for line in lines2 if float(line[1]) < 24764.0/6371.0]
            outtext.append('\n'.join(lines3))#OUTTEXT contains a complete list of all sub-neptune detections
        with open(os.path.join(PPoutpath,'NEIDallSubNeptunes.txt'), 'w') as g: #Write to file
            g.write('\n'.join(outtext))
        
        #### Count number of surveys analyzed
        NumAnalyzed = 0
        for folder in folders:
            pklfiles = glob.glob(os.path.join(folder,'*.pkl'))
            NumAnalyzed += len(pklfiles)
        with open(os.path.join(PPoutpath,'NEIDcountFilesAnalyzed.txt'), 'w') as g: #Write to file
            g.write(str(NumAnalyzed)) 
Example #10
Source File: flakiness_checker.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def find_test_path(test_file):
    """Searches for the test file and returns the path if found
    As a default, the currend working directory is the top of the search.
    If a directory was provided as part of the argument, the directory will be
    joined with cwd unless it was an absolute path, in which case, the
    absolute path will be used instead. 
    """
    test_file += ".py"
    test_path = os.path.split(test_file)
    top = os.path.join(os.getcwd(), test_path[0])

    for (path, dirs, files) in os.walk(top):
        if test_path[1] in files:
            return  os.path.join(path, test_path[1])
    raise FileNotFoundError("Could not find " + test_path[1] + 
                            "in directory: " + top) 
Example #11
Source File: setup.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def write_version_py():
    content = """# GENERATED VERSION FILE
# TIME: {}

__version__ = '{}'
short_version = '{}'
version_info = ({})
"""
    sha = get_hash()
    with open('mmdet/VERSION', 'r') as f:
        SHORT_VERSION = f.read().strip()
    VERSION_INFO = ', '.join(SHORT_VERSION.split('.'))
    VERSION = SHORT_VERSION + '+' + sha

    version_file_str = content.format(time.asctime(), VERSION, SHORT_VERSION,
                                      VERSION_INFO)
    with open(version_file, 'w') as f:
        f.write(version_file_str) 
Example #12
Source File: setup.py    From mmdetection with Apache License 2.0 6 votes vote down vote up
def make_cuda_ext(name, module, sources, sources_cuda=[]):

    define_macros = []
    extra_compile_args = {'cxx': []}

    if torch.cuda.is_available() or os.getenv('FORCE_CUDA', '0') == '1':
        define_macros += [('WITH_CUDA', None)]
        extension = CUDAExtension
        extra_compile_args['nvcc'] = [
            '-D__CUDA_NO_HALF_OPERATORS__',
            '-D__CUDA_NO_HALF_CONVERSIONS__',
            '-D__CUDA_NO_HALF2_OPERATORS__',
        ]
        sources += sources_cuda
    else:
        print(f'Compiling {name} without CUDA')
        extension = CppExtension
        # raise EnvironmentError('CUDA is required to compile MMDetection!')

    return extension(
        name=f'{module}.{name}',
        sources=[os.path.join(*module.split('.'), p) for p in sources],
        define_macros=define_macros,
        extra_compile_args=extra_compile_args) 
Example #13
Source File: conllu.py    From udapi-python with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, strict=False, separator='tab', empty_parent='warn', fix_cycles=False,
                 attributes='ord,form,lemma,upos,xpos,feats,head,deprel,deps,misc', **kwargs):
        """Create the Conllu reader object.

        Args:
        strict: raise an exception if errors found (default=False, i.e. a robust mode)
        separator: How are the columns separated?
            Default='tab' is the only possibility in valid CoNLL-U files.
            'space' means one or more whitespaces (this does not allow forms with space).
            'doublespace' means two or more spaces.
        empty_parent: What to do if HEAD is _? Default=warn - issue a warning and attach to the root
            or if strict=1 issue an exception. With `empty_parent=ignore` no warning is issued.
        attributes: comma-separated list of column names in the input files
            (default='ord,form,lemma,upos,xpos,feats,head,deprel,deps,misc')
            Changing the default can be used for loading CoNLL-like formats (not valid CoNLL-U).
            For ignoring a column, use "_" as its name.
            Column "ord" marks the column with 1-based word-order number/index (usualy called ID).
            Column "head" marks the column with dependency parent index (word-order number).

            For example, for CoNLL-X which uses name1=value1|name2=value2 format of FEATS, use
            `attributes=ord,form,lemma,upos,xpos,feats,head,deprel,_,_`
            but note attributes that upos, feats and deprel will contain language-specific values,
            not valid according to UD guidelines and a further conversion will be needed.
            You will loose the projective_HEAD and projective_DEPREL attributes.

            For CoNLL-2009 you can use `attributes=ord,form,lemma,_,upos,_,feats,_,head,_,deprel`.
            You will loose the predicted_* attributes and semantic/predicate annotation.

            TODO: allow storing the rest of columns in misc, e.g. `node.misc[feats]`
            for feats which do not use the name1=value1|name2=value2 format.
        """
        super().__init__(**kwargs)
        self.node_attributes = attributes.split(',')
        self.strict = strict
        self.separator = separator
        self.empty_parent = empty_parent
        self.fix_cycles = fix_cycles 
Example #14
Source File: gitgot.py    From GitGot with GNU Lesser General Public License v3.0 5 votes vote down vote up
def regex_validator(args, state):
    with open(args.checks, "r") as fd:
        for line in fd.read().splitlines():
            if line.startswith("#") or len(line) == 0:
                continue
            try:
                re.subn(line, r'\1', "Expression test")
            except sre_constants.error as e:
                print(bcolors.FAIL + "Invalid Regular expression:\n\t" + line)
                if "group" in str(e):
                    print(
                        "Ensure expression contains"
                        "a capture group for matches:\n\t" + str(e))
                sys.exit(-1)
            state.checks.append(line)

    split = []
    if not (state.query[0] == "\"" and state.query[-1] == "\""):
        split = re.split(GITHUB_WHITESPACE, state.query)

    for part in [state.query] + split:
        if part:
            escaped_query = re.escape(part) if split else \
                part.replace("\"", "")
            state.checks.append("(?i)(" + escaped_query + ")")
    return state 
Example #15
Source File: depthOfSearch.py    From EXOSIMS with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def singleRunPostProcessing(self, PPoutpath, folder):
        """This is called by runPostProcessing
        Args:
            PPoutpath (string) - output path to place data in
            folder (string) - full filepath to folder containing runs
        """
        #Loading Outspec
        outspecPath = os.path.join(folder,'outspec.json')

        #Done plotting Comp vs intTime of Observations
        date = unicode(datetime.datetime.now())
        date = ''.join(c + '_' for c in re.split('-|:| ',date)[0:-1])#Removes seconds from date
        fname1 = 'DoS_' + folder.split('/')[-1] + '_' + date
        #plt.savefig(os.path.join(PPoutpath, fname + '.png'))
        #plt.savefig(os.path.join(PPoutpath, fname + '.svg'))
        #plt.savefig(os.path.join(PPoutpath, fname + '.eps'))

        # create DoSFuncs object with sample script and all default options
        dos = DoSFuncs.DoSFuncs(path=outspecPath)
        # plot depth of search
        dos.plot_dos('all', 'Depth of Search',os.path.join(PPoutpath, fname1))
        # plot expected planets
        fname2 = 'ExpectedPlanets_' + folder.split('/')[-1] + '_' + date
        dos.plot_nplan('all', 'Expected Planets',os.path.join(PPoutpath, fname2))

        pass 
Example #16
Source File: callbacks.py    From steppy-toolkit with MIT License 5 votes vote down vote up
def get_correct_channel_name(ctx, name):
    channels_with_name = [channel for channel in ctx._experiment._channels if name in channel.name]
    if len(channels_with_name) == 0:
        return name
    else:
        channel_ids = [re.split('[^\d]', channel.name)[-1] for channel in channels_with_name]
        channel_ids = sorted([int(idx) if idx != '' else 0 for idx in channel_ids])
        last_id = channel_ids[-1]
        corrected_name = '{} {}'.format(name, last_id + 1)
        return corrected_name 
Example #17
Source File: ssd_meta_arch.py    From object_detector_app with MIT License 5 votes vote down vote up
def restore_fn(self, checkpoint_path, from_detection_checkpoint=True):
    """Return callable for loading a checkpoint into the tensorflow graph.

    Args:
      checkpoint_path: path to checkpoint to restore.
      from_detection_checkpoint: whether to restore from a full detection
        checkpoint (with compatible variable names) or to restore from a
        classification checkpoint for initialization prior to training.

    Returns:
      a callable which takes a tf.Session as input and loads a checkpoint when
        run.
    """
    variables_to_restore = {}
    for variable in tf.all_variables():
      if variable.op.name.startswith(self._extract_features_scope):
        var_name = variable.op.name
        if not from_detection_checkpoint:
          var_name = (
              re.split('^' + self._extract_features_scope + '/', var_name)[-1])
        variables_to_restore[var_name] = variable
    # TODO: Load variables selectively using scopes.
    variables_to_restore = (
        variables_helper.get_variables_available_in_checkpoint(
            variables_to_restore, checkpoint_path))
    saver = tf.train.Saver(variables_to_restore)

    def restore(sess):
      saver.restore(sess, checkpoint_path)
    return restore 
Example #18
Source File: preprocessor_plugins.py    From SublimeKSP with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, arrayToFill, declare, brackets, size, arraysToConcat, line):
		self.line = line
		self.arrayToFill = arrayToFill
		self.declare = declare
		self.size = size
		self.brackets = brackets
		self.arraysToConcat = arraysToConcat.split(",") 
Example #19
Source File: utils.py    From jawfish with MIT License 5 votes vote down vote up
def parse_dict_header(value):
    """Parse lists of key, value pairs as described by RFC 2068 Section 2 and
    convert them into a python dict:

    >>> d = parse_dict_header('foo="is a fish", bar="as well"')
    >>> type(d) is dict
    True
    >>> sorted(d.items())
    [('bar', 'as well'), ('foo', 'is a fish')]

    If there is no value for a key it will be `None`:

    >>> parse_dict_header('key_without_value')
    {'key_without_value': None}

    To create a header from the :class:`dict` again, use the
    :func:`dump_header` function.

    :param value: a string with a dict header.
    :return: :class:`dict`
    """
    result = {}
    for item in _parse_list_header(value):
        if '=' not in item:
            result[item] = None
            continue
        name, value = item.split('=', 1)
        if value[:1] == value[-1:] == '"':
            value = unquote_header_value(value[1:-1])
        result[name] = value
    return result


# From mitsuhiko/werkzeug (used with permission). 
Example #20
Source File: collateAllUniqueDetections.py    From EXOSIMS with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def countOccurences(PPoutpath, folder):
        #### Read File and Count Star Fequency
        lines = list()
        with open(os.path.join(folder,'NEIDallSubNeptunes.txt'), 'r') as g: #Write to file
            lines = g.read().split('\n')[0:-1]

        while '' in lines:
            lines.remove('')

        starNamesDat = {}
        for line in lines:
            planet = line.split(',')[0]
            if planet in starNamesDat:
                starNamesDat[planet] += 1
            else:
                starNamesDat[planet] = 1


        with open(os.path.join(PPoutpath,'MostFrequentStars.txt'), 'w') as g: #Write to file
            json.dump(starNamesDat, g)#g.write('\n'.join(outtext))


        starKeys = list(starNamesDat)
        tmpstarNames = list()
        occurence = list()
        for key in starKeys:
            tmpstarNames.append(key)
            occurence.append(starNamesDat[key])
        sortInds = np.asarray(occurence).argsort()[::-1]
        starNames = list()
        outString = list()
        for i in range(len(sortInds)):
            starNames.append(tmpstarNames[sortInds[i]])
            outString.append(tmpstarNames[sortInds[i]] + ',' + str(occurence[sortInds[i]]))

        with open(os.path.join(PPoutpath,'NEIDsortedStars.txt'), 'w') as g: #Write to file
            g.write('\n'.join(outString)) 
Example #21
Source File: deps.py    From oscrypto with MIT License 5 votes vote down vote up
def _archive_single_dir(archive):
    """
    Check if all members of the archive are in a single top-level directory

    :param archive:
        An archive from _open_archive()

    :return:
        None if not a single top level directory in archive, otherwise a
        unicode string of the top level directory name
    """

    common_root = None
    for info in _list_archive_members(archive):
        fn = _info_name(info)
        if fn in set(['.', '/']):
            continue
        sep = None
        if '/' in fn:
            sep = '/'
        elif '\\' in fn:
            sep = '\\'
        if sep is None:
            root_dir = fn
        else:
            root_dir, _ = fn.split(sep, 1)
        if common_root is None:
            common_root = root_dir
        else:
            if common_root != root_dir:
                return None
    return common_root 
Example #22
Source File: ksp_compiler.py    From SublimeKSP with GNU General Public License v3.0 5 votes vote down vote up
def modifyID(self, node, parent_function=None, parent_varref=None):
        ''' Add a variable prefix (one of $, %, @, !, ? and ~) to each variable based on the list of variables previously built '''
        name = node.prefix + node.identifier
        first_part = name.split('.')[0]

        # if prefix is missing and this is not a function or family and does not start with a function parameter (eg. if a parameter is passed as param and then referenced as param__member)
        if node.prefix == '' and not (name in functions or
                                      name in ksp_builtins.functions or
                                      name in families or
                                      name in properties or
                                      (parent_function and (first_part in parent_function.parameters or
                                                            parent_function.return_value and first_part == parent_function.return_value.identifier))):
            possible_prefixes = [prefix for prefix in '$%@!?~'
                                 if prefix + name.lower() in variables or prefix + name in ksp_builtins.variables]

            # if there is a subscript then only array types are possible
            if parent_varref and parent_varref.subscripts:
                possible_prefixes = [p for p in possible_prefixes if p not in '$@~']

            if len(possible_prefixes) == 0:
                raise ksp_ast.ParseException(node, "%s has not been declared!" % name)
            if len(possible_prefixes) > 1:
                raise ksp_ast.ParseException(node, "Type of %s ambigious (variable prefix could be any of: %s)" % (name, ', '.join(possible_prefixes)))
            node.prefix = possible_prefixes[0]
            return node

        elif node.prefix and not (name.lower() in variables or name in ksp_builtins.variables):
            raise ksp_ast.ParseException(node, "%s has not been declared." % name)
        else:
            return node 
Example #23
Source File: ksp_compiler.py    From SublimeKSP with GNU General Public License v3.0 5 votes vote down vote up
def get_macro_name_and_parameters(self):
        """ returns the function name, parameter list, and result variable (or None) as a tuple """
        param = white_space + r'([$%@!?~]?[\w\.]+|#[\w\.]+#)' + white_space
        params = r'%s(,%s)*' % (param, param)
        m = re.match(r'^\s*macro\s+(?P<name>[a-zA-Z0-9_]+(\.[a-zA-Z_0-9.]+)*)\s*(?P<params>\(%s\))?' % params, self.lines[0].command)
        if not m:
            raise ParseException(self.lines[0], "Syntax error in macro declaration")
        name = m.group('name')
        params = m.group('params') or []
        if params:
            params = params[1:-1] # strip parenthesis
            params = re.sub(white_space, '', params)  # strip whitespace (eg. comments)
            params = [x.strip() for x in params.split(',')]
        return (name, params) 
Example #24
Source File: preprocessor_plugins.py    From SublimeKSP with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, name, argString):
		self.name = name
		self.args = argString.replace(" ", "").split(",") 
Example #25
Source File: gen.py    From insightocr with MIT License 5 votes vote down vote up
def main():
    global mainDir, fontsChinese
    pygame.init()
    shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
    os.makedirs(OUTPUT_DIR)
    labels = open(os.path.join(OUTPUT_DIR, "labels.txt"), 'w')
    labels.truncate()
    i = 0
    chiIdx = 0
    outDir = None

    # http://stackoverflow.com/questions/50499/how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executing
    selfPath = os.path.abspath(inspect.getfile(inspect.currentframe()))
    mainDir, _ = os.path.split(selfPath)
    dirFonts = os.path.join(mainDir, 'fonts_Chinese')
    fnFonts = filter(lambda fn: os.path.splitext(fn)[1].lower() in [
                     '.ttf', '.otf'], os.listdir(dirFonts))
    fontsChinese = list(
        map(lambda fn: os.path.join(dirFonts, fn), fnFonts))

    chiFiles = sorted(glob.glob('newsgroup/corpus-*.txt'))
    outputPerChiFile = OUTPUT_NUM / len(chiFiles)
    initChineseSource(chiFiles[0])
    chiIdx += 1

    for im, text in generate(OUTPUT_NUM):
        if i % OUTPUT_BATCH == 0:
            outDir = os.path.join(OUTPUT_DIR, str(int(i/OUTPUT_BATCH)))
            os.makedirs(outDir)
        if i != 0 and i % outputPerChiFile == 0:
            initChineseSource(chiFiles[chiIdx])
            chiIdx += 1
        outf = os.path.join(outDir, '%s.jpg' % i)
        # pygame.image.save(im, outf) #pygame
        # im.save(outf) #PIL image
        io.imsave(outf, im)  # scikit-image
        labels.write('%s/%s.jpg\t%s\n' % (int(i/OUTPUT_BATCH),
                                          i, text))
        print('done %s.jpg, text: %s' % (i, text))
        i += 1
    labels.close() 
Example #26
Source File: preprocessor_plugins.py    From SublimeKSP with GNU General Public License v3.0 5 votes vote down vote up
def simplfyAdditionString(string):
	""" Evaluates a string of add operations, any add pairs that cannot be evalutated are left.
	e.g. "2 + 2 + 3 + 4 + x + y + 2" => "11 + x + y + 2 """
	parts = string.split("+")
	count = 0
	while count < len(parts) - 1:
		try:
			simplified = int(parts[count]) + int(parts[count+1])
			parts[count] = str(simplified)
			parts.remove(parts[count + 1])
		except:
			count += 1
			pass
	return("+".join(parts)) 
Example #27
Source File: wireless.py    From Paradrop with Apache License 2.0 5 votes vote down vote up
def getMainOptions(self):
        options = list()

        options.append(("ap_scan", 1))
        if self.wifiDevice.country is not None:
            options.append(("country", self.wifiDevice.country))

        # (name, value, quote)
        network = list()

        network.append(('ssid', self.wifiIface.ssid, True))
        network.append(('scan_ssid', 1, False))

        modes = self.wifiIface.encryption.split("+")
        if modes[0] == "psk2":
            network.append(('key_mgmt', 'WPA-PSK', False))
            network.append(('psk', self.wifiIface.key, True))

        elif modes[0] == "wpa2":
            network.append(('key_mgmt', 'WPA-EAP', False))
            network.append(('identity', self.wifiIface.identity))
            network.append(('password', self.wifiIface.password))

        else:
            network.append(('key_mgmt', 'NONE', False))

        options.append(("network", network))

        return options 
Example #28
Source File: wireless.py    From Paradrop with Apache License 2.0 5 votes vote down vote up
def get11rOptions(self):
        """
        Get options related to 802.11r (fast BSS transition).
        """
        options = list()

        if self.wifiIface.nasid is None:
            raise Exception("nasid is not defined but is required for 802.11r")

        options.append(("mobility_domain", self.wifiIface.mobility_domain))
        options.append(("r0_key_lifetime", self.wifiIface.r0_key_lifetime))
        options.append(("r1_key_holder", self.wifiIface.r1_key_holder))
        options.append(("reassociation_deadline", self.wifiIface.reassociation_deadline))

        for item in self.wifiIface.r0kh:
            # May be specified as either comma- or space-separated, but hostapd
            # expects spaces.
            parts = re.split("[, ]+", item)
            options.append(("r0kh", " ".join(parts)))

        for item in self.wifiIface.r1kh:
            # May be specified as either comma- or space-separated, but hostapd
            # expects spaces.
            parts = re.split("[, ]+", item)
            options.append(("r1kh", " ".join(parts)))

        return options 
Example #29
Source File: cpu_info.py    From QCElemental with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get(name: str, vendor=None, cutoff=0.9) -> ProcessorInfo:

    name = context.process_names(name.split("@")[0])

    if ("amd" in name) or (vendor == "amd"):
        vendor = "amd"

        name = re.split(r"\d+-core pro", name)[0]

        # Opteron model names are only numbers?
        if "opteron" in name:
            name = name.replace("processor", "")
            name = name.replace("opteron", "")
            name = name.replace("amd", "")

    elif "intel" in name or (vendor == "intel"):
        vendor = "intel"

        if "xeon" in name:
            name = name.replace(" v", "v")
            if name.endswith(" 0"):
                name = name[:-2]

        name = name.replace("intel", "").replace("cpu", "")
        name = name.replace("core", "").replace("xeon", "")
        name = name.replace("silver", "").replace("gold", "").replace("phi", "")

    if vendor is None:
        raise KeyError("Could not determine vendor, please provide one.")

    index = context.index_vendor[vendor]

    name = context.process_names(name)
    matches = difflib.get_close_matches(name, index.keys(), cutoff=cutoff)

    if matches:
        return index[matches[0]]
    else:
        raise KeyError(f"Could not find processor {vendor}: {name}.") 
Example #30
Source File: cpu_info.py    From QCElemental with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def process_names(self, name):
        name = name.lower()
        name = name.replace("(tm)", "").replace("(r)", "").replace("™", "")
        name = " ".join(name.split())
        name = name.strip()
        return name