Python operator.concat() Examples

The following are 24 code examples of operator.concat(). 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 operator , or try the search function .
Example #1
Source File: process_solver.py    From Learning-Path-Learn-Web-Development-with-Python with MIT License 6 votes vote down vote up
def parallel_batch_solver(puzzles, workers=4):
    # Parallel batch solve - Puzzles are chunked into `workers`
    # chunks. A process for each chunk.
    assert len(puzzles) >= workers

    dim = ceil(len(puzzles) / workers)
    chunks = (
        puzzles[k: k + dim] for k in range(0, len(puzzles), dim)
    )

    with ProcessPoolExecutor(max_workers=workers) as executor:
        futures = (
            executor.submit(batch_solve, chunk) for chunk in chunks
        )

        results = (
            future.result() for future in as_completed(futures)
        )

        return reduce(concat, results) 
Example #2
Source File: summary_lossplot.py    From deep500 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def after_training(self, runner, training_stats: TrainingStatistics):
        import matplotlib.pyplot as plt

        losses = map(lambda s: s.losses, training_stats.train_summaries)
        losses = functools.reduce(operator.concat, losses)
        plt.figure()
        plt.plot(np.arange(0, len(losses)), losses)
        plt.ylabel('Loss')
        plt.xlabel('Iteration')

        batch_indices = []
        for summary in training_stats.train_summaries:
            if len(batch_indices) == 0:
                batch_indices.append(summary.n_batches)
            else:
                batch_indices.append(batch_indices[-1] + summary.n_batches)

        for epoch_index in batch_indices:
            plt.axvline(epoch_index, linestyle='dashed', color='black')
        plt.savefig(self.path)
        print('Loss plot written to: {}.png'.format(self.path))
        print('Average inference time: {}'.format(training_stats.current_summary.avg_time_inference)) 
Example #3
Source File: filters.py    From drf-haystack with MIT License 5 votes vote down vote up
def get_valid_fields(self, queryset, view, context={}):
        valid_fields = getattr(view, "ordering_fields", self.ordering_fields)

        if valid_fields is None:
            return self.get_default_valid_fields(queryset, view, context)

        elif valid_fields == "__all__":
            # View explicitly allows filtering on all model fields.
            if not queryset.query.models:
                raise ImproperlyConfigured(
                    "Cannot use %s with '__all__' as 'ordering_fields' attribute on a view "
                    "which has no 'index_models' set. Either specify some 'ordering_fields', "
                    "set the 'index_models' attribute or override the 'get_queryset' "
                    "method and pass some 'index_models'."
                    % self.__class__.__name__)

            model_fields = map(lambda model: [(field.name, field.verbose_name) for field in model._meta.fields],
                               queryset.query.models)
            valid_fields = list(set(reduce(operator.concat, model_fields)))
        else:
            valid_fields = [
                (item, item) if isinstance(item, six.string_types) else item
                for item in valid_fields
            ]

        return valid_fields 
Example #4
Source File: apps.py    From django-river with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_all_workflow_fields(cls):
        from river.core.workflowregistry import workflow_registry
        return reduce(operator.concat, map(list, workflow_registry.workflows.values()), []) 
Example #5
Source File: test_operator.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_concat(self):
        self.failUnlessRaises(TypeError, operator.concat)
        self.failUnlessRaises(TypeError, operator.concat, None, None)
        self.failUnless(operator.concat('py', 'thon') == 'python')
        self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
        self.failUnless(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
        self.failUnless(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
        if not test_support.is_jython:
            # Jython concat is add
            self.failUnlessRaises(TypeError, operator.concat, 13, 29) 
Example #6
Source File: toolchain_makefile.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def concat_deps(self, bn):
        # read source
        src = open(os.path.join(self.buildpath, bn), "r").read()
        # update direct dependencies
        deps = []
        for l in src.splitlines():
            res = includes_re.match(l)
            if res is not None:
                depfn = res.groups()[0]
                if os.path.exists(os.path.join(self.buildpath, depfn)):
                    # print bn + " depends on "+depfn
                    deps.append(depfn)
        # recurse through deps
        # TODO detect cicular deps.
        return reduce(operator.concat, map(self.concat_deps, deps), src) 
Example #7
Source File: toolchain_gcc.py    From OpenPLC_Editor with GNU General Public License v3.0 5 votes vote down vote up
def concat_deps(self, bn):
        # read source
        src = open(os.path.join(self.buildpath, bn), "r").read()
        # update direct dependencies
        deps = []
        self.append_cfile_deps(src, deps)
        # recurse through deps
        # TODO detect cicular deps.
        return reduce(operator.concat, map(self.concat_deps, deps), src) 
Example #8
Source File: builtins.py    From mochi with MIT License 5 votes vote down vote up
def mapcat(func, iterable):
    return reduce(concat, map(func, iterable)) 
Example #9
Source File: test_operator.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_concat(self):
        self.failUnlessRaises(TypeError, operator.concat)
        self.failUnlessRaises(TypeError, operator.concat, None, None)
        self.failUnless(operator.concat('py', 'thon') == 'python')
        self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
        self.failUnless(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
        self.failUnless(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
        if not test_support.is_jython:
            # Jython concat is add
            self.failUnlessRaises(TypeError, operator.concat, 13, 29) 
Example #10
Source File: __init__.py    From antismash with GNU Affero General Public License v3.0 5 votes vote down vote up
def filter_subregions(subregions: List[SubRegion]) -> List[SubRegion]:
    """ Strips any subregion that is fully contained by another for the same anchor

        Arguments:
            subregions: the subregions to filter

        Returns:
            a sorted list of SubRegions
    """
    if not subregions:
        return subregions

    by_anchor = defaultdict(list)  # type: Dict[str, List[SubRegion]]
    # sort from largest to smallest to avoid complicated replacement logic
    # any sharing an anchor will overlap on that gene anyway
    for sub in sorted(subregions, key=lambda x: x.location.end - x.location.start, reverse=True):
        contained = False
        for other in by_anchor[sub.label]:
            if sub.is_contained_by(other):
                contained = True
                break
        if not contained:
            by_anchor[sub.label].append(sub)
    # flatten the lists and sort back into location order, then anchor
    # mypy doesn't handle this reduce well
    flattened = reduce(operator.concat, by_anchor.values())  # type: ignore
    return sorted(flattened, key=lambda x: (x.location.start, x.location.end, x.label)) 
Example #11
Source File: test_operator.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_concat(self):
        self.failUnlessRaises(TypeError, operator.concat)
        self.failUnlessRaises(TypeError, operator.concat, None, None)
        self.failUnless(operator.concat('py', 'thon') == 'python')
        self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
        self.failUnless(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
        self.failUnless(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
        if not test_support.is_jython:
            # Jython concat is add
            self.failUnlessRaises(TypeError, operator.concat, 13, 29) 
Example #12
Source File: expr.py    From owasp-pysec with Apache License 2.0 5 votes vote down vote up
def __iconcat__(self, other):
        return Expression((self, other), operator.concat) 
Example #13
Source File: expr.py    From owasp-pysec with Apache License 2.0 5 votes vote down vote up
def __concat__(self, other):
        return Expression((self, other), operator.concat) 
Example #14
Source File: test_operator.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_concat(self):
        self.assertRaises(TypeError, operator.concat)
        self.assertRaises(TypeError, operator.concat, None, None)
        self.assertTrue(operator.concat('py', 'thon') == 'python')
        self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
        self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
        self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
        self.assertRaises(TypeError, operator.concat, 13, 29) 
Example #15
Source File: shell_backend.py    From TensorNetwork with Apache License 2.0 5 votes vote down vote up
def concat_shape(self, values) -> Sequence:
    tuple_values = (tuple(v) for v in values)
    return functools.reduce(operator.concat, tuple_values) 
Example #16
Source File: Write.py    From pseudonetcdf with GNU Lesser General Public License v3.0 5 votes vote down vote up
def write_emissions_ncf(infile, outfile):
    from operator import concat
    # initialize hdr_fmts with species count
    hdr_fmts = ["10i60i3ifif", "ffiffffiiiiifff",
                "iiii", "10i" * len(infile.variables.keys())]
    hdrlines = []

    hdrlines.append(
        reduce(concat, [Asc2Int(s) for s in [infile.name, infile.note]]) +
        [infile.ione, len(infile.variables.keys()), infile.start_date,
         infile.start_time, infile.end_date, infile.end_time])

    hdrlines.append(
        [infile.rdum, infile.rdum, infile.iutm, infile.xorg, infile.yorg,
         infile.delx, infile.dely, len(infile.dimensions['COL']),
         len(infile.dimensions['ROW']), len(infile.dimensions['LAY']),
         infile.idum, infile.idum, infile.rdum, infile.rdum, infile.rdum])

    hdrlines.append([infile.ione, infile.ione, len(
        infile.dimensions['COL']), len(infile.dimensions['ROW'])])
    hdrlines.append(reduce(concat, [Asc2Int(s.ljust(10))
                                    for s in infile.variables.keys()]))

    for d, h in zip(hdrlines, hdr_fmts):
        outfile.write(writeline(d, h))

    for ti, (d, t) in enumerate(infile.timerange()):
        ed, et = timeadd((d, t), (0, infile.time_step))
        outfile.write(writeline((d, t, ed, et), 'ifif'))
        for spc in infile.variables.keys():
            var = infile.variables[spc]
            for k in range(len(infile.dimensions['LAY'])):
                outfile.write(writeline(
                    [infile.ione] +
                    Asc2Int(spc.ljust(10)) +
                    var[ti, :, :, k].transpose().ravel().tolist(),
                    '11i' + infile.cell_count * 'f')) 
Example #17
Source File: AnalysisReport.py    From irwin with GNU Affero General Public License v3.0 5 votes vote down vote up
def binnedMoveActivations(self, top=False):
        gameReports = self.topGames() if top else self.gameReports
        moveActivations = reduce(operator.concat, [gameReport.activations() for gameReport in gameReports])
        return json.dumps([sum([int(moveActivation in range(i,i+10)) for moveActivation in moveActivations]) for i in range(0, 100, 10)][::-1]) 
Example #18
Source File: plotting.py    From scqubits with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _process_options(figure, axes, opts=None, **kwargs):
    """
    Processes plotting options.

    Parameters
    ----------
    figure: matplotlib.Figure
    axes: matplotlib.Axes
    opts: dict
        keyword dictionary with custom options
    **kwargs: dict
        standard plotting option (see separate documentation)
    """
    opts = opts or {}

    # Only process items in kwargs that would not have been
    # processed through _extract_kwargs_options()
    filtered_kwargs = {key: value for key, value in kwargs.items()
                       if key not in functools.reduce(operator.concat, _direct_plot_options.values())}

    option_dict = {**opts, **filtered_kwargs}

    for key, value in option_dict.items():
        if key in defaults.SPECIAL_PLOT_OPTIONS:
            _process_special_option(figure, axes, key, value)
        else:
            set_method = getattr(axes, 'set_' + key)
            set_method(value)

    filename = kwargs.get('filename')
    if filename:
        figure.savefig(os.path.splitext(filename)[0] + '.pdf')

    if settings.DESPINE and not axes.name == '3d':
        # Hide the right and top spines
        axes.spines['right'].set_visible(False)
        axes.spines['top'].set_visible(False)

        # Only show ticks on the left and bottom spines
        axes.yaxis.set_ticks_position('left')
        axes.xaxis.set_ticks_position('bottom') 
Example #19
Source File: misc.py    From pyshtrih with MIT License 5 votes vote down vote up
def bytearray_concat(*args):
    """
    Функция конкатенирования нескольких bytearray в один.
    """

    return bytearray_cast(reduce(operator.concat, args)) 
Example #20
Source File: test_operator.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_concat(self):
        self.assertRaises(TypeError, operator.concat)
        self.assertRaises(TypeError, operator.concat, None, None)
        self.assertTrue(operator.concat('py', 'thon') == 'python')
        self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
        self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
        self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
        self.assertRaises(TypeError, operator.concat, 13, 29) 
Example #21
Source File: test_operator.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_concat(self):
        self.assertRaises(TypeError, operator.concat)
        self.assertRaises(TypeError, operator.concat, None, None)
        self.assertTrue(operator.concat('py', 'thon') == 'python')
        self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
        self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
        self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
        self.assertRaises(TypeError, operator.concat, 13, 29) 
Example #22
Source File: test_operator.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_concat(self):
        self.assertRaises(TypeError, operator.concat)
        self.assertRaises(TypeError, operator.concat, None, None)
        self.assertTrue(operator.concat('py', 'thon') == 'python')
        self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
        self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
        self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
        self.assertRaises(TypeError, operator.concat, 13, 29) 
Example #23
Source File: Write.py    From pseudonetcdf with GNU Lesser General Public License v3.0 4 votes vote down vote up
def write_point(start_date, start_time, time_step, hdr, vals):
    # Internalize header
    hdr = [h for h in hdr]
    species = hdr[-4]
    nstk = hdr[-3][1]
    timeprops = hdr.pop()

    # initialize hdr_fmts with species count
    hdr_fmts = ["10i60i3ifif", "ffiffffiiiiifff",
                "iiii", "10i" * len(species), "ii", "ffffff" * nstk]

    # initialize output variable
    pt_string = ''

    # Change name and note
    hdr[0] = list(hdr[0][0]) + list(hdr[0][1]) + list(hdr[0][2:])

    # Reducing stk props
    stkprops = hdr[-1]
    hdr[-1] = []
    for stk in stkprops:
        hdr[-1].extend(stk)
    stk_time_prop_fmt = "iiiff" * nstk

    stk_time_props = []
    for time in timeprops:
        stk_time_props.append([])
        for stk in time:
            stk_time_props[-1].extend(stk)

    # Change species names to array of characters
    hdr[-3] = reduce(operator.concat, [Asc2Int(s) for s in hdr[-3]])

    # for each item in the header, write it to output
    for i, (h, f) in enumerate(zip(hdr, hdr_fmts)):
        pt_string += writeline(h, f, False)

    # create value format
    valfmt = 'i10i' + ('f' * nstk)

    # Get end date
    (end_date, end_time) = timeadd(
        (start_date, start_time), (0, time_step * vals.shape[0]))

    # Write out values
    for ti, (d, t) in enumerate(timerange((start_date, start_time),
                                          (end_date, end_time),
                                          time_step)):
        ed, et = timeadd((d, t), (0, time_step))
        pt_string += writeline((d, t, ed, et), 'ifif', False)
        pt_string += writeline((1, nstk), 'ii', False)
        pt_string += writeline(stk_time_props[ti], stk_time_prop_fmt, False)
        for si, spc in enumerate(species):
            # Dummy variable,spc characters and values flattened
            temp = [1]
            temp.extend(Asc2Int(spc))
            temp.extend(vals[ti, si, ...])
            pt_string += writeline(temp, valfmt, False)

    return pt_string 
Example #24
Source File: Write.py    From pseudonetcdf with GNU Lesser General Public License v3.0 4 votes vote down vote up
def write_emissions(start_date, start_time, time_step, hdr, vals):
    # initialize hdr_fmts with species count
    hdr_fmts = ["10i60i3ifif", "ffiffffiiiiifff", "iiii", "10i" * len(hdr[-1])]

    # initialize output variable
    emis_string = ''

    # Internalize header
    hdr = [h for h in hdr]
    species = hdr[-1]

    # Check start_date and start_time
    if tuple(hdr[0][4:6]) != (start_date, start_time):
        print("Header doesn't match start date/time", file=sys.stderr)

    # Change name and note
    hdr[0] = list(hdr[0][0]) + list(hdr[0][1]) + list(hdr[0][2:])

    # Change species names to array of characters
    hdr[-1] = reduce(operator.concat, [Asc2Int(s) for s in hdr[-1]])

    # for each item in the header, write it to output
    for h, f in zip(hdr, hdr_fmts):
        emis_string += writeline(h, f)

    # create value format
    cells = vals.shape[2] * vals.shape[3]
    valfmt = 'i10i' + ('f' * cells)

    # Get end date
    (end_date, end_time) = timeadd(
        (start_date, start_time), (0, time_step * vals.shape[0]))

    if tuple(hdr[0][6:]) != (end_date, end_time):
        print("Header doesn't match end date/time", file=sys.stderr)

    # Write out values
    for ti, (d, t) in enumerate(timerange((start_date, start_time),
                                          (end_date, end_time),
                                          time_step)):
        ed, et = timeadd((d, t), (0, time_step))
        emis_string += writeline((d, t, ed, et), 'ifif')
        for si, spc in enumerate(species):
            for k in range(vals.shape[-1]):
                # Dummy variable,spc characters and values flattened
                temp = [1]
                temp.extend(Asc2Int(spc))
                spcvals = vals[ti, si, ..., ..., k]
                spcvals = spcvals.transpose().ravel()
                temp.extend(spcvals)
                emis_string += writeline(temp, valfmt)

    return emis_string