Python os.path.commonprefix() Examples

The following are code examples for showing how to use os.path.commonprefix(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: NiujiaoDebugger   Author: MrSrc   File: util.py    GNU General Public License v3.0 6 votes vote down vote up
def _common_shorten_repr(*args):
    args = tuple(map(safe_repr, args))
    maxlen = max(map(len, args))
    if maxlen <= _MAX_LENGTH:
        return args

    prefix = commonprefix(args)
    prefixlen = len(prefix)

    common_len = _MAX_LENGTH - \
                 (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
    if common_len > _MIN_COMMON_LEN:
        assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
               (maxlen - prefixlen) < _MAX_LENGTH
        prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
        return tuple(prefix + s[prefixlen:] for s in args)

    prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
    return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
                 for s in args) 
Example 2
Project: Blockly-rduino-communication   Author: technologiescollege   File: util.py    GNU General Public License v3.0 6 votes vote down vote up
def _common_shorten_repr(*args):
    args = tuple(map(safe_repr, args))
    maxlen = max(map(len, args))
    if maxlen <= _MAX_LENGTH:
        return args

    prefix = commonprefix(args)
    prefixlen = len(prefix)

    common_len = _MAX_LENGTH - \
                 (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
    if common_len > _MIN_COMMON_LEN:
        assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
               (maxlen - prefixlen) < _MAX_LENGTH
        prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
        return tuple(prefix + s[prefixlen:] for s in args)

    prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
    return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
                 for s in args) 
Example 3
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: util.py    GNU General Public License v2.0 6 votes vote down vote up
def _common_shorten_repr(*args):
    args = tuple(map(safe_repr, args))
    maxlen = max(map(len, args))
    if maxlen <= _MAX_LENGTH:
        return args

    prefix = commonprefix(args)
    prefixlen = len(prefix)

    common_len = _MAX_LENGTH - \
                 (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
    if common_len > _MIN_COMMON_LEN:
        assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
               (maxlen - prefixlen) < _MAX_LENGTH
        prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
        return tuple(prefix + s[prefixlen:] for s in args)

    prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
    return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
                 for s in args) 
Example 4
Project: aws-lambda-runtime-pypy   Author: uscheller   File: util.py    Apache License 2.0 6 votes vote down vote up
def _common_shorten_repr(*args):
    args = tuple(map(safe_repr, args))
    maxlen = max(map(len, args))
    if maxlen <= _MAX_LENGTH:
        return args

    prefix = commonprefix(args)
    prefixlen = len(prefix)

    common_len = _MAX_LENGTH - \
                 (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
    if common_len > _MIN_COMMON_LEN:
        assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
               (maxlen - prefixlen) < _MAX_LENGTH
        prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
        return tuple(prefix + s[prefixlen:] for s in args)

    prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
    return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
                 for s in args) 
Example 5
Project: setup   Author: mindbender-studio   File: util.py    MIT License 6 votes vote down vote up
def _common_shorten_repr(*args):
    args = tuple(map(safe_repr, args))
    maxlen = max(map(len, args))
    if maxlen <= _MAX_LENGTH:
        return args

    prefix = commonprefix(args)
    prefixlen = len(prefix)

    common_len = _MAX_LENGTH - \
                 (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
    if common_len > _MIN_COMMON_LEN:
        assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
               (maxlen - prefixlen) < _MAX_LENGTH
        prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
        return tuple(prefix + s[prefixlen:] for s in args)

    prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
    return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
                 for s in args) 
Example 6
Project: nototools   Author: googlefonts   File: tool_utils.py    Apache License 2.0 6 votes vote down vote up
def commonpathprefix(paths):
  """Return the common path prefix and a tuple of the relative subpaths for the
  provided paths.  Uses resolve_path to convert to absolute paths and returns
  the common absolute path.  Some subpaths might be the empty string and joining
  these will produce paths ending in '/', use normpath if you don't want this.

  Python 2.7 only has path.commonprefix, which returns a common string prefix,
  not a common path prefix.
  """
  norm_paths = [resolve_path(p) + path.sep for p in paths]
  prefix = path.dirname(path.commonprefix(norm_paths))
  prefix_len = len(prefix)
  if prefix_len > 1:
    prefix_len += 1 # not '/' so does not end in '/', strip from subpaths
  subpaths = tuple(p[prefix_len:-1] for p in norm_paths)
  return prefix, subpaths 
Example 7
Project: download-manager   Author: thispc   File: pyLoadGui.py    GNU General Public License v3.0 6 votes vote down vote up
def slotClipboardChange(self):
        """
            called if clipboard changes
        """
        if self.checkClipboard:
            text = self.clipboard.text()
            pattern = re.compile(r"(http|https|ftp)://[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?/.*)?")
            matches = pattern.finditer(text)
            
            # thanks to: jmansour //#139
            links = [str(match.group(0)) for match in matches]
            if len(links) == 0:
                return
                
            filenames = [link.rpartition("/")[2] for link in links]
            
            packagename = commonprefix(filenames)
            if len(packagename) == 0:
                packagename = filenames[0]

            self.slotAddPackage(packagename, links) 
Example 8
Project: ngraph-neon   Author: NervanaSystems   File: pybindtransform.py    Apache License 2.0 6 votes vote down vote up
def search_cpp_op(self, op):
        if isinstance(op, SequentialOp):
            op = op.ops[-1]
        if isinstance(op, TensorValueOp):
            tensor_op = op.tensor
            if not tensor_op.is_constant:
                if tensor_op in self.variables_cpp_op:
                    scopeprefix = commonprefix([self.scopemark[op], self.variables_cpp_op[tensor_op][0]])
                    lastprefix = basename(scopeprefix)
                    if not lastprefix.startswith('par'):
                        if self.op_rank[op] > self.op_rank[self.variables_cpp_op[tensor_op][1]]:
                            """
                            print("Forwarding " + tensor_op.name + " to " + op.name +
                                  " forward value " + self.variables_cpp_op[tensor_op][1].name)
                            """
                            return self.ngraph_cpp_ops[self.variables_cpp_op[tensor_op][1].tensor]
        else:
            tensor_op = op.tensor
        if tensor_op in self.ngraph_cpp_ops:
            return self.ngraph_cpp_ops[tensor_op]
        return None 
Example 9
Project: bioprocs   Author: pwwang   File: pFastqSim.py    MIT License 6 votes vote down vote up
def run_art_illumina():
	global fq1, fq2
	if gz:
		fq1, fq2 = fq1[:-3], fq2[:-3]
	prefix = path.commonprefix([fq1, fq2]).rstrip('._[]')

	params.c = num
	params.i = ref
	params.l = len1
	params.o = prefix
	params.p = True
	params.rndSeed = get_seed(seed)

	shell.fg.art_illumina(**params)

	shell.mv(prefix + '1.fq', fq1)
	shell.mv(prefix + '2.fq', fq2)

	if gz:
		shell.gzip(fq1)
		shell.gzip(fq2) 
Example 10
Project: docker   Author: getavalon   File: util.py    MIT License 6 votes vote down vote up
def _common_shorten_repr(*args):
    args = tuple(map(safe_repr, args))
    maxlen = max(map(len, args))
    if maxlen <= _MAX_LENGTH:
        return args

    prefix = commonprefix(args)
    prefixlen = len(prefix)

    common_len = _MAX_LENGTH - \
                 (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
    if common_len > _MIN_COMMON_LEN:
        assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
               (maxlen - prefixlen) < _MAX_LENGTH
        prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
        return tuple(prefix + s[prefixlen:] for s in args)

    prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
    return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
                 for s in args) 
Example 11
Project: potranslator   Author: SekouD   File: pycompat.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def relpath(path, start=curdir):
        """Return a relative version of a path"""
        from os.path import sep, abspath, commonprefix, join, pardir

        if not path:
            raise ValueError("no path specified")

        start_list = abspath(start).split(sep)
        path_list = abspath(path).split(sep)

        # Work out how much of the filepath is shared by start and path.
        i = len(commonprefix([start_list, path_list]))

        rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
        if not rel_list:
            return start
        return join(*rel_list) 
Example 12
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: util.py    GNU General Public License v3.0 6 votes vote down vote up
def _common_shorten_repr(*args):
    args = tuple(map(safe_repr, args))
    maxlen = max(map(len, args))
    if maxlen <= _MAX_LENGTH:
        return args

    prefix = commonprefix(args)
    prefixlen = len(prefix)

    common_len = _MAX_LENGTH - \
                 (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
    if common_len > _MIN_COMMON_LEN:
        assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
               (maxlen - prefixlen) < _MAX_LENGTH
        prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
        return tuple(prefix + s[prefixlen:] for s in args)

    prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
    return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
                 for s in args) 
Example 13
Project: cells   Author: AlesTsurko   File: util.py    MIT License 6 votes vote down vote up
def _common_shorten_repr(*args):
    args = tuple(map(safe_repr, args))
    maxlen = max(map(len, args))
    if maxlen <= _MAX_LENGTH:
        return args

    prefix = commonprefix(args)
    prefixlen = len(prefix)

    common_len = _MAX_LENGTH - \
                 (maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
    if common_len > _MIN_COMMON_LEN:
        assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
               (maxlen - prefixlen) < _MAX_LENGTH
        prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
        return tuple(prefix + s[prefixlen:] for s in args)

    prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
    return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
                 for s in args) 
Example 14
Project: kay-template   Author: yosukesuzuki   File: extract_messages.py    MIT License 5 votes vote down vote up
def strip_path(filename, base):
  filename = path.normpath(path.join(base, filename))
  return filename[len(path.commonprefix([
    filename, path.dirname(base)])):].lstrip(path.sep) 
Example 15
Project: airflow   Author: apache   File: exampleinclude.py    Apache License 2.0 5 votes vote down vote up
def doctree_read(app, doctree):
    """
    Reads documentation tree for the application and register sources in the generated documentation.

    :param app: application
    :param doctree: documentation tree

    :return None

    """
    env = app.builder.env
    if not hasattr(env, "_viewcode_modules"):
        env._viewcode_modules = {}  # type: ignore

    if app.builder.name == "singlehtml":
        return

    for objnode in doctree.traverse(ExampleHeader):
        filepath = objnode.get("filename")
        relative_path = path.relpath(
            filepath, path.commonprefix([app.config.exampleinclude_sourceroot, filepath])
        )
        modname = relative_path.replace("/", ".")[:-3]
        show_button = register_source(app, env, modname)
        onlynode = create_node(env, relative_path, show_button)

        objnode.replace_self(onlynode)
# pylint: enable=protected-access 
Example 16
Project: bratiaa   Author: kldtz   File: common.py    MIT License 5 votes vote down vote up
def relpath(path, start):
    """Return a relative version of a path."""
    from os.path import abspath, sep, pardir, commonprefix
    from os.path import join as path_join
    if not path:
        raise ValueError("no path specified")
    start_list = abspath(start).split(sep)
    path_list = abspath(path).split(sep)
    # Work out how much of the filepath is shared by start and path.
    i = len(commonprefix([start_list, path_list]))
    rel_list = [pardir] * (len(start_list) - i) + path_list[i:]
    if not rel_list:
        return path
    return path_join(*rel_list) 
Example 17
Project: beansoup   Author: fxtlabs   File: transactions.py    GNU General Public License v2.0 5 votes vote down vote up
def score_model(self, model_txn, txn):
        """Score an existing transaction for its ability to provide a model
        for an incomplete transaction.

        Args:
          model_txn: The transaction to be scored.
          txn: The incomplete transaction.
        Returns:
          A float number representing the score, normalized in [0,1].
        """
        def get_description(txn):
            return ('{} {}'.format(txn.payee or '', txn.narration or '')).strip()

        # If the target transaction does not have a description, there is
        # nothing we can do
        txn_description = get_description(txn)
        n_max = len(txn_description)
        if n_max > 1:
            # Only consider model transactions whose posting to the target
            # account has the same sign as the transaction to be completed
            posting = [p for p in model_txn.postings if p.account == self.account][0]
            if number.same_sign(posting.units.number, txn.postings[0].units.number):
                model_txn_description = get_description(model_txn)
                n_match = len(path.commonprefix(
                    [model_txn_description, txn_description]))
                score = float(n_match) / float(n_max)
                return score
        return 0 
Example 18
Project: nidaba   Author: OpenPhilology   File: util.py    GNU General Public License v2.0 5 votes vote down vote up
def _group_by_prefix(data, prefixes):
    """
    Groups a list of input files by longest common prefix over a given list of prefixes
    """
    ret = [[] for _ in prefixes]
    for doc in data:
        ret[sorted(enumerate(commonprefix([doc[1], pre[1]]) for pre in prefixes),
                   key=lambda x: len(x[1]))[-1][0]].append(doc)
    return ret 
Example 19
Project: download-manager   Author: thispc   File: filters.py    GNU General Public License v3.0 5 votes vote down vote up
def relpath(path, start=curdir):
        """Return a relative version of a path"""
        if not path:
            raise ValueError("no path specified")
        start_list = abspath(start).split(sep)
        path_list = abspath(path).split(sep)
        # Work out how much of the filepath is shared by start and path.
        i = len(commonprefix([start_list, path_list]))
        rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
        if not rel_list:
            return curdir
        return join(*rel_list) 
Example 20
Project: course-advanced_python   Author: mipt-cs   File: thumbnailer.py    GNU General Public License v3.0 5 votes vote down vote up
def get_thumbnail_name(self, in_path):
        # Find the partial path + filename beyond the input image directory.
        prefix = path.commonprefix([in_path, self._root])
        new_filename = in_path[len(prefix) + 1:]

        # Generate the new filename.
        (basename, ext) = path.splitext(new_filename)
        return "{0}_{1}{2}".format(basename, self._name, ext) 
Example 21
Project: data   Author: khaeru   File: ceic.py    GNU General Public License v3.0 5 votes vote down vote up
def _make_attrs(df, key):
    """Construct attributes for a xr.DataArray from *df* and *key*."""
    attrs = {
        'name': ': '.join(key),
        'source': ', '.join(df['source'].unique()),
        'first obs': str(df['first obs. date'].min()),
        'last obs': str(df['last obs. date'].max()),
        'updated': df['last update time'].max(),
        'series codes': ' '.join(df['series code alpha']),
        }

    attrs['unit'] = df['unit'].iloc[0].to_base_units()

    # It's an error to have non-unique values for any of these
    for a in ['country', 'frequency', 'status']:
        unique_value = df[a].unique()
        if len(unique_value) == 1:
            attrs[a] = unique_value[0]
        else:
            raise VariableImportError('Skip (multiple values for %s): %s'
                                      % (a, unique_value))

    # Get the alias for this variable
    name = META['rename variables'].get(key, None)

    if name is None:
        # No alias.
        codes = list(df['series code alpha'])

        # Use _get_name to construct a name
        name = _get_name(commonprefix(codes))

    return attrs, name


# For disambiguating 
Example 22
Project: PyFlow   Author: wonderworks-software   File: PathLib.py    Apache License 2.0 5 votes vote down vote up
def commonprefix(path=("StringPin", [])):
        '''Return the longest path prefix (taken character-by-character) that is a prefix of all paths in list. If list is empty, return the empty string (''). Note that this may return invalid paths because it works a character at a time'''
        return osPath.commonprefix(path) 
Example 23
Project: libSigNetSim   Author: vincent-noel   File: Source.py    GNU General Public License v3.0 5 votes vote down vote up
def makeRelativePath(self, path):

		if self.__sourceType == self.LOCAL and (isabs(self.__source) or commonprefix([self.__source, path]) != ""):
			self.__source = relpath(self.__source, path) 
Example 24
Project: bioprocs   Author: pwwang   File: fastx.py    MIT License 5 votes vote down vote up
def _getCommonName(f1, f2):
	ret = path.basename(path.commonprefix([f1, f2]))
	while not ret[-1].isalnum(): ret = ret[:-1]
	return ret 
Example 25
Project: bioprocs   Author: pwwang   File: fastx.py    MIT License 5 votes vote down vote up
def _pFastq2Expr():
	"""
	@input:
		fqfile1: The fastq file1.
		fqfile2: The fastq file2.
	@output:
		outfile: The expression file
		outdir : Output direcotry with expression and other output files
	@args:
		params   (Diot) : Other parameters for `kallisto quant`.
		idxfile  (path): The kallisto index file.
		kallisto (path): The path to `kallisto`.
		nthread  (int) : # threads to use.
	"""
	return Diot(
		desc   = 'Call gene expression from pair-end fastq files using kallisto.',
		lang   = params.python.value,
		input  = 'fqfile1:file, fqfile2:file',
		output = [
			'outfile:file:{{	i.fqfile1, i.fqfile2 | \
							__import__("os").path.commonprefix | \
							.rstrip("._[]") | bn }}.kallisto/{{i.fqfile1, i.fqfile2 | \
							__import__("os").path.commonprefix | \
							.rstrip("._[]") | bn }}.expr.txt',
			'outdir:dir:{{	i.fqfile1, i.fqfile2 | \
							__import__("os").path.commonprefix | \
							.rstrip("._[]") | bn }}.kallisto'
		],
		preCmd = """
			{{"reference.bash" | bashimport}}
			reference kallisto {{args.idxfile | quote}}
		""",
		args = Diot(
			kallisto = params.kallisto.value,
			params   = Diot(),
			nthread  = 1,
			idxfile  = params.kallistoIdx.value,
		)
	) 
Example 26
Project: bioprocs   Author: pwwang   File: __init__.py    MIT License 5 votes vote down vote up
def fs2name(files):
	if not files: return 'nothing.etc'
	bnames  = [path.basename(f).split('.')[0] for f in files]
	compfix = path.commonprefix(bnames)
	compfix = re.sub(r'[^a-zA-Z0-9]$', '', compfix)
	if len(compfix) < 3:
		return bnames[0] + '.etc'
	else:
		return compfix + '.etc' 
Example 27
Project: bioprocs   Author: pwwang   File: sampleinfo.py    MIT License 5 votes vote down vote up
def toChannel(self, datadir, paired = False, raiseExc = True):
		if not paired:
			samples = self.select(get = 'Sample')
			return [path.join(datadir, r.Sample) for r in samples]
		else:
			data     = self.select(get = ['Sample', 'Patient'])
			patients = [r.Patient for r in data]
			patients = sorted(set(patients), key = patients.index)
			ret      = []
			for patient in patients:
				records = self.select(patient = patient, get = ['Sample', 'Group'])
				if len(records) != 2:
					if raiseExc:
						raise SampleInfoException('Expect 2 samples for paired channel for patient: %s' % patient)
					else:
						continue
				record1, record2 = records
				# make sure record1 is tumor and record2 is normal
				if  record1.Group.upper() in SampleInfo.TUMOR_GROUP + SampleInfo.NORMAL_GROUP and \
					record2.Group.upper() in SampleInfo.TUMOR_GROUP + SampleInfo.NORMAL_GROUP:
					if record1.Group.upper() in SampleInfo.NORMAL_GROUP:
						record1, record2 = record2, record1
				else:
					common = path.commonprefix([record1.Sample, record2.Sample])
					if common and record1.Sample < record2.Sample:
						record1, record2 = record2, record1
				ret.append((path.join(datadir, record1.Sample), path.join(datadir, record2.Sample)))
			return ret 
Example 28
Project: toolbox   Author: EnvGen   File: taxonomy_blast_parser.py    MIT License 5 votes vote down vote up
def lca(scores1, scores2, percent, tax):
	classdict = dict()
	for query, hit in scores1.iteritems():
		scr1 = set(hit.keys())
		scr2 = set(scores2[query].keys())
		#find the common hits of both dictionaries
		common = scr1.intersection(scr2)
		commonscores=dict()
		for goodhit in common:
			score = hit[goodhit] + scores2[query][goodhit]
			commonscores[goodhit] = score
		#get the top percent scores of this intersection
		topcommon = toppercent(commonscores, percent)
		#get the LCA for these
		classify = ''
		for query, score in topcommon.iteritems():
			if classify == '':
				classify = tax[query]
			else:
				classify = commonprefix([classify, tax[query]])
		if classify == '':
			classify = 'Unclassified;'
		#print classify
		#take longest substr ending in ;
		meaningful = re.match(".+;", classify)
		classify = meaningful.group()
		classdict[query] = classify
		#print query + "\t" + classify
	return classdict 
Example 29
Project: parallelio   Author: SignalN   File: parallelio.py    MIT License 5 votes vote down vote up
def pio(fn, *ipaths, path=None, insert_fn=None, filter_fn=None, **kwargs):
    if path.startswith('.'):
        path = commonprefix(ipaths) + path
    p = pread(*ipaths)
    p = pinsert(insert_fn, p, **kwargs)
    p = papply(fn, p, **kwargs)
    p = pfilter(filter_fn, p, **kwargs)
    return write(p, path) 
Example 30
Project: warbend   Author: int19h   File: enum.py    MIT License 5 votes vote down vote up
def simplify_names(names):
    if not names:
        return {}
    cp = commonprefix(set(names.itervalues()) - {None})
    if not cp:
        return names
    res = {}
    for index, name in names.iteritems():
        res[index] = name[len(cp):]
    return res 
Example 31
Project: Repobot   Author: Desgard   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 32
Project: sublime-text-3-packages   Author: nickjj   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return make_charset(oneletter)
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 33
Project: Blockly-rduino-communication   Author: technologiescollege   File: regexopt.py    GNU General Public License v3.0 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 34
Project: DroidPot   Author: WhySoGeeky   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return make_charset(oneletter)
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 35
Project: sketch-wakatime   Author: wakatime   File: regexopt.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 36
Project: pigaios   Author: joxeankoret   File: regexopt.py    GNU General Public License v3.0 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return make_charset(oneletter)
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 37
Project: Rovers   Author: RIT-Space-Exploration   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 38
Project: Rovers   Author: RIT-Space-Exploration   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 39
Project: pySINDy   Author: luckystarufo   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 40
Project: SUMO_dy_public   Author: DayuanTan   File: status.py    GNU General Public License v3.0 4 votes vote down vote up
def printStatus(makeLog, makeAllLog, smtpServer="localhost", out=sys.stdout, toAddr="[email protected]"):
    failed = ""
    build = commonprefix([basename(makeLog), basename(makeAllLog)])
    print(build, end=' ', file=out)
    print(datetime.now().ctime(), file=out)
    print("--", file=out)
    print(basename(makeLog), file=out)
    warnings = 0
    errors = 0
    svnLocked = False
    for l in open(makeLog):
        if ("svn: Working copy" in l and "locked" in l) or "svn: Failed" in l:
            svnLocked = True
            failed += l
        warnings, errors, failed = findErrors(l, warnings, errors, failed)
    if svnLocked:
        failed += "svn up failed\n\n"
    print(warnings, "warnings", file=out)
    if errors:
        print(errors, "errors", file=out)
        failed += "make failed\n\n"
    print("--\nbatchreport\n--", file=out)
    print(basename(makeAllLog), file=out)
    warnings = 0
    errors = 0
    for l in open(makeAllLog):
        warnings, errors, failed = findErrors(l, warnings, errors, failed)
    print(warnings, "warnings", file=out)
    if errors:
        print(errors, "errors", file=out)
        failed += "make debug failed\n\n"
    print("--", file=out)
    if failed:
        fromAddr = "[email protected]"
        message = """From: "%s" <%s>
To: %s
Subject: Error occurred while building

%s""" % (build, fromAddr, toAddr, failed)
        try:
            server = smtplib.SMTP(smtpServer)
            server.sendmail(fromAddr, toAddr, message)
            server.quit()
        except:
            print("Could not send mail.") 
Example 41
Project: ephypype   Author: neuropycon   File: neuropycon.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def infosrc(fif_files):
    r"""Specify input.

    Use wildcards to run computations on multiple files;
    To check yourself it's a good idea to run ls command first like this:


    $ ls ./\*/\*.fif

    $ neuropycon <...> input ./\*/\*.fif

    """

    from os.path import abspath, split
    from os.path import commonprefix as cprfx
    from nipype.interfaces.utility import IdentityInterface, Function

    fif_files = [abspath(f) for f in fif_files]

    common_prefix = split(cprfx(fif_files))[0] + '/'
    iter_mapping = dict()
    for fif_file in fif_files:
        new_base = fif_file.replace(common_prefix, '')
        new_base = new_base.replace('/', '__')
        new_base = new_base.replace('.', '-')
        iter_mapping[new_base] = fif_file

    infosource = pe.Node(interface=IdentityInterface(fields=['keys']),
                         name='input')

    path_node = pe.Node(interface=Function(input_names=['key', 'iter_mapping'],
                                           output_names=['path'],
                                           function=map_path),
                        name='path_node')

    infosource.iterables = [('keys', list(iter_mapping.keys()))]
    path_node.inputs.iter_mapping = iter_mapping
    return infosource, path_node
# -------------------------------------------------------------------------- #


# ---------------------- power spectral density node ---------------------- # 
Example 42
Project: Loop   Author: JasdipChauhan   File: regexopt.py    Apache License 2.0 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return make_charset(oneletter)
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 43
Project: kubeface   Author: hammerlab   File: stringable.py    Apache License 2.0 4 votes vote down vote up
def prefixes(self, max_prefixes=1, **fields_dict):
        for (key, value) in fields_dict.items():
            assert key in self.field_names, key
            assert value is None or isinstance(value, list), type(value)

        def make_prefixes(
                template_pieces,
                max_prefixes=max_prefixes,
                fields_dict=fields_dict):
            result = [[]]
            if not template_pieces:
                return result

            (literal, field_name) = template_pieces[0]
            if literal:
                for piece in result:
                    piece.append(literal)

            values = fields_dict.get(field_name)
            if values is None:
                values = self.valid_values.get(field_name)
            if values is not None:
                if len(result) * len(values) > max_prefixes:
                    common_prefix = commonprefix(values)
                    for piece in result:
                        piece.append(common_prefix)
                else:
                    new_result = []
                    for value in values:
                        new_fields_dict = dict(fields_dict)
                        new_fields_dict[field_name] = [value]
                        rest = make_prefixes(
                            template_pieces[1:],
                            max_prefixes=max_prefixes / (
                                len(result) * len(values)),
                            fields_dict=new_fields_dict)
                        for some_rest in rest:
                            new_result.extend(
                                [x + [value] + some_rest for x in result])
                    result = new_result
            return result

        prefix_components = make_prefixes(self.template_pieces)
        assert len(prefix_components) <= max_prefixes
        return [''.join(x) for x in prefix_components] 
Example 44
Project: pygments   Author: dagwieers   File: regexopt.py    BSD 2-Clause "Simplified" License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 45
Project: Deribit_funding_rate_indicator   Author: Dimasik007   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 46
Project: torchetl   Author: amajidsinar   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return open_paren + make_charset(oneletter) + close_paren
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 47
Project: WRGameVideos-API   Author: thundernet8   File: regexopt.py    GNU General Public License v2.0 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return make_charset(oneletter)
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 48
Project: old-dotfiles   Author: hawkw   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return make_charset(oneletter)
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 49
Project: old-dotfiles   Author: hawkw   File: regexopt.py    MIT License 4 votes vote down vote up
def regex_opt_inner(strings, open_paren):
    """Return a regex that matches any string in the sorted list of strings."""
    close_paren = open_paren and ')' or ''
    # print strings, repr(open_paren)
    if not strings:
        # print '-> nothing left'
        return ''
    first = strings[0]
    if len(strings) == 1:
        # print '-> only 1 string'
        return open_paren + escape(first) + close_paren
    if not first:
        # print '-> first string empty'
        return open_paren + regex_opt_inner(strings[1:], '(?:') \
            + '?' + close_paren
    if len(first) == 1:
        # multiple one-char strings? make a charset
        oneletter = []
        rest = []
        for s in strings:
            if len(s) == 1:
                oneletter.append(s)
            else:
                rest.append(s)
        if len(oneletter) > 1:  # do we have more than one oneletter string?
            if rest:
                # print '-> 1-character + rest'
                return open_paren + regex_opt_inner(rest, '') + '|' \
                    + make_charset(oneletter) + close_paren
            # print '-> only 1-character'
            return make_charset(oneletter)
    prefix = commonprefix(strings)
    if prefix:
        plen = len(prefix)
        # we have a prefix for all strings
        # print '-> prefix:', prefix
        return open_paren + escape(prefix) \
            + regex_opt_inner([s[plen:] for s in strings], '(?:') \
            + close_paren
    # is there a suffix?
    strings_rev = [s[::-1] for s in strings]
    suffix = commonprefix(strings_rev)
    if suffix:
        slen = len(suffix)
        # print '-> suffix:', suffix[::-1]
        return open_paren \
            + regex_opt_inner(sorted(s[:-slen] for s in strings), '(?:') \
            + escape(suffix[::-1]) + close_paren
    # recurse on common 1-string prefixes
    # print '-> last resort'
    return open_paren + \
        '|'.join(regex_opt_inner(list(group[1]), '')
                 for group in groupby(strings, lambda s: s[0] == first[0])) \
        + close_paren 
Example 50
Project: bioprocs   Author: pwwang   File: fastx.py    MIT License 4 votes vote down vote up
def _pFastq2Sam():
	"""
	@description:
		Cleaned paired fastq (.fq, .fq.gz, .fastq, .fastq.gz file to mapped sam/bam file
	@args:
		`tool`   : The tool used for alignment. Available: bowtie2, ngm or star.
		`bwa`    : Path of bwa
		`ngm`    : Path of ngm
		`star`   : Path of STAR
			- The genomeDir should be at `/path/to/hg19.star` if `args.ref` is `/path/to/hg19.fa`
		`bowtie2`: Path of bowtie2
		`rg`:     The read group.
			- `id` will be parsed from filename with "_LX_" in it if not given
			- `sm` will be parsed from filename
		`ref`    : Path of reference file
		`refexon`: The GTF file for STAR to build index. It's not neccessary if index is already been built.
		`params` : Other params for tool
	"""
	return Diot(
		desc   = 'Map cleaned paired fastq file to reference genome.',
		lang   = params.python.value,
		input  = "fq1:file, fq2:file",
		output = "outfile:file:{{i.fq1, i.fq2 | path.commonprefix | bn | .rstrip: '_. ,[]' }}.{{args.outfmt}}",
		envs   = Diot(path = path),
		preCmd = """
			{{"reference.bash" | bashimport}}
			export bwa={{args.bwa | squote}}
			export ngm={{args.ngm | squote}}
			export star={{args.star | squote}}
			export samtools={{args.samtools | squote}}
			export bowtie2={{args.bowtie2 | squote}}
			export nthread={{args.nthread}}
			export refexon={{args.refexon | squote}}
			reference {{args.tool | squote}} {{args.ref | squote}}""",
		args = Diot(
			tool     = 'bwa',
			outfmt   = 'sam',
			bwa      = params.bwa.value,
			ngm      = params.ngm.value,
			star     = params.star.value,
			samtools = params.samtools.value,
			bowtie2  = params.bowtie2.value,
			rg       = Diot(id = '', pl = 'Illumina', pu = 'unit1', lb = 'lib1', sm = ''),
			ref      = params.ref.value,
			refexon  = params.refexon.value,
			nthread  = 1,
			params   = Diot()
		)
	)