Пример #1
0
def run(
	input: compose(csv.DictReader, open) = csv.DictReader(sys.stdin),
	output: compose(DictWriter, write) = DictWriter(sys.stdout),
	skip: int = 3,
):
	"""
	Resolve sales from transactions using LIFO strategy.
	"""
	output.writer.writerows(itertools.islice(input.reader, skip))
	output.fieldnames = ['Lot'] + input.fieldnames + [Lots.basis_field]
	output.writeheader()
	consume(map(output.writerow, Lots(input)))
Пример #2
0
def multi_substitution(*substitutions):
	"""
	Take a sequence of pairs specifying substitutions, and create
	a function that performs those substitutions.

	>>> multi_substitution(('foo', 'bar'), ('bar', 'baz'))('foo')
	'baz'
	"""
	substitutions = itertools.starmap(substitution, substitutions)
	# compose function applies last function first, so reverse the
	#  substitutions to get the expected order.
	substitutions = reversed(tuple(substitutions))
	return compose(*substitutions)
Пример #3
0
def multi_substitution(*substitutions):
    """
	Take a sequence of pairs specifying substitutions, and create
	a function that performs those substitutions.

	>>> multi_substitution(('foo', 'bar'), ('bar', 'baz'))('foo')
	'baz'
	"""
    substitutions = itertools.starmap(substitution, substitutions)
    # compose function applies last function first, so reverse the
    #  substitutions to get the expected order.
    substitutions = reversed(tuple(substitutions))
    return compose(*substitutions)
Пример #4
0
>>> ob = decode('{"$gte": {"$date": "2019-01-01"}}')
>>> ob['$gte']
datetime.datetime(2019, 1, 1, 0, 0)

This function is useful in particular if you're accepting JSON queries
over an HTTP connection and you don't have the luxury of Javascript
expressions like you see in the Mongo shell or Compass.
"""

import json
import functools
import collections

import dateutil.parser
from jaraco.functools import compose


def maybe_date(obj):
    """
    >>> maybe_date({"$date": "2019-01-01"})
    datetime.datetime(2019, 1, 1, 0, 0)
    """
    return dateutil.parser.parse(obj['$date']) if list(obj) == ['$date'
                                                                ] else obj


smart_hook = compose(maybe_date, collections.OrderedDict)

decode = functools.partial(json.loads, object_pairs_hook=smart_hook)
Пример #5
0
from django.contrib.auth import authenticate

import yaml

from vr.server import models
from .utils import validate_xmlrpc


def yaml_load(yaml_str):
    try:
        return yaml.safe_load(yaml_str)
    except Exception:
        raise forms.ValidationError("Invalid YAML")


try_load = pass_none(compose(validate_xmlrpc, yaml_load))


class ConfigIngredientForm(forms.ModelForm):
    class Meta:
        model = models.ConfigIngredient
        exclude = []

    class Media:
        js = ('js/jquery.textarea.min.js', )

    def clean_config_yaml(self):
        return try_load(self.cleaned_data.get('config_yaml', None))

    def clean_env_yaml(self):
        return try_load(self.cleaned_data.get('env_yaml', None))
Пример #6
0
def parse_args(*args, **kwargs):
    """
    Parse the args for the command.

    It should be possible for one to specify '--ns', '-x', and '--rename'
    multiple times:

    >>> args = parse_args(['--ns', 'foo', 'bar', '--ns', 'baz'])
    >>> args.ns
    ['foo', 'bar', 'baz']

    >>> parse_args(['-x', '--exclude']).exclude
    []

    >>> renames = parse_args(['--rename', 'a=b', '--rename', 'b=c']).rename
    >>> len(renames)
    2
    >>> type(renames)
    <class 'jaraco.mongodb.oplog.Renamer'>

    >>> parse_args(['--seconds', '86402']).start_ts
    Timestamp(..., 0)
    """
    parser = argparse.ArgumentParser(add_help=False)

    parser.add_argument("--help",
        help="show usage information",
        action="help")

    parser.add_argument("--source", metavar="host[:port]",
        help="host to pull from")

    parser.add_argument('--oplogns', default='local.oplog.rs',
        help="source namespace for oplog")

    parser.add_argument("--dest", metavar="host[:port]",
        help="host to push to (<set name>/s1,s2 for sets)")

    parser.add_argument("-s", "--seconds",
        dest="start_ts",
        metavar="SECONDS",
        type=compose(Timestamp.for_window, delta_from_seconds),
        help="""Seconds in the past to query. Overrides any value
            indicated by a resume file. Deprecated, use window instead.""",
        )

    parser.add_argument("-w", "--window",
        dest="start_ts",
        metavar="WINDOW",
        type=compose(
            Timestamp.for_window,
            delta_from_seconds,
            pytimeparse.parse,
        ),
        help='Time window to query, like "3 days" or "24:00".',
    )

    parser.add_argument("-f", "--follow", action="store_true",
        help="wait for new data in oplog, run forever.")

    parser.add_argument("--ns", nargs="*", default=[],
        action=Extend,
        help="this namespace(s) only ('dbname' or 'dbname.coll')")

    parser.add_argument("-x", "--exclude", nargs="*", default=[],
        action=Extend,
        help="exclude namespaces ('dbname' or 'dbname.coll')")

    parser.add_argument("--rename", nargs="*", default=[],
        metavar="ns_old=ns_new",
        type=RenameSpec.from_spec,
        action=Extend,
        help="rename namespaces before processing on dest")

    parser.add_argument("--dry-run", default=False,
        action="store_true",
        help="suppress application of ops")

    help = textwrap.dedent("""
        Read from and write to this file the last processed timestamp.
        """)
    parser.add_argument("--resume-file",
        metavar="FILENAME",
        type=ResumeFile,
        default=NullResumeFile(),
        help=help,
    )
    jaraco.logging.add_arguments(parser)

    args = parser.parse_args(*args, **kwargs)
    args.rename = Renamer(args.rename)

    args.start_ts = args.start_ts or args.resume_file.read()

    return args
Пример #7
0
def parse_args(*args, **kwargs):
    """
    Parse the args for the command.

    It should be possible for one to specify '--ns', '-x', and '--rename'
    multiple times:

    >>> args = parse_args(['--ns', 'foo', 'bar', '--ns', 'baz'])
    >>> args.ns
    ['foo', 'bar', 'baz']

    >>> parse_args(['-x', '--exclude']).exclude
    []

    >>> renames = parse_args(['--rename', 'a=b', '--rename', 'b=c']).rename
    >>> len(renames)
    2

    "..." below should be "jaraco." but for pytest-dev/pytest#3396
    >>> type(renames)
    <class '...mongodb.oplog.Renamer'>
    """
    parser = argparse.ArgumentParser(add_help=False)

    parser.add_argument(
        "--help",
        help="show usage information",
        action="help",
    )

    parser.add_argument(
        "--source",
        metavar="host[:port]",
        help="""Hostname of the mongod server from which oplog
        operations are going to be pulled. Called "--from"
        in mongooplog.""",
    )

    parser.add_argument(
        '--oplogns',
        default='local.oplog.rs',
        help="Source namespace for oplog",
    )

    parser.add_argument(
        "--dest",
        metavar="host[:port]",
        help="""
        Hostname of the mongod server (or replica set as
        <set name>/s1,s2) to which oplog operations
        are going to be applied. Default is "localhost".
        Called "--host" in mongooplog.
        """,
    )

    parser.add_argument(
        "-w",
        "--window",
        dest="start_ts",
        metavar="WINDOW",
        type=compose(
            Timestamp.for_window,
            delta_from_seconds,
            pytimeparse.parse,
        ),
        help="""Time window to query, like "3 days" or "24:00"
        (24 hours, 0 minutes).""",
    )

    parser.add_argument(
        "-f",
        "--follow",
        action="store_true",
        help="""Wait for new data in oplog. Makes the utility
        polling oplog forever (until interrupted). New data
        is going to be applied immediately with at most one
        second delay.""",
    )

    parser.add_argument(
        "--ns",
        nargs="*",
        default=[],
        action=Extend,
        help="""Process only these namespaces, ignoring all others.
        Space separated list of strings in form of ``dname``
        or ``dbname.collection``. May be specified multiple times.
        """,
    )

    parser.add_argument(
        "-x",
        "--exclude",
        nargs="*",
        default=[],
        action=Extend,
        help="""List of space separated namespaces which should be
        ignored. Can be in form of ``dname`` or ``dbname.collection``.
        May be specified multiple times.
        """,
    )

    parser.add_argument(
        "--rename",
        nargs="*",
        default=[],
        metavar="ns_old=ns_new",
        type=RenameSpec.from_spec,
        action=Extend,
        help="""
        Rename database(s) and/or collection(s). Operations on
        namespace ``ns_old`` from the source server will be
        applied to namespace ``ns_new`` on the destination server.
        May be specified multiple times.
        """,
    )

    parser.add_argument(
        "--dry-run",
        default=False,
        action="store_true",
        help="Suppress application of ops.",
    )

    parser.add_argument(
        "--resume-file",
        metavar="FILENAME",
        type=ResumeFile,
        default=NullResumeFile(),
        help="""Read from and write to this file the last processed
        timestamp.""",
    )

    jaraco.logging.add_arguments(parser)

    args = parser.parse_args(*args, **kwargs)
    args.rename = Renamer(args.rename)

    args.start_ts = args.start_ts or args.resume_file.read()

    return args