sitelocal_network = IPv6Network('<STR_LIT>')<EOL>return self in sitelocal_network<EOL>

Test if the address is reserved for site-local.

        Note that the site-local address space has been deprecated by RFC 3879.
        Use is_private to test if this address is in the space of unique local
        addresses as defined by RFC 4193.

        Returns:
            A boolean, True if the address is reserved per RFC 3513 2.5.6.


private_network = IPv6Network('<STR_LIT>')<EOL>return self in private_network<EOL>

Test if this address is allocated for private networks.

        Returns:
            A boolean, True if the address is reserved per RFC 4193.


return self._ip == <NUM_LIT:0><EOL>

Test if the address is unspecified.

        Returns:
            A boolean, True if this is the unspecified address as defined in
            RFC 2373 2.5.2.


return self._ip == <NUM_LIT:1><EOL>

Test if the address is a loopback address.

        Returns:
            A boolean, True if the address is a loopback address as defined in
            RFC 2373 2.5.3.


if (self._ip >> <NUM_LIT:32>) != <NUM_LIT>:<EOL><INDENT>return None<EOL><DEDENT>return IPv4Address(self._ip & <NUM_LIT>)<EOL>

Return the IPv4 mapped address.

        Returns:
            If the IPv6 address is a v4 mapped address, return the
            IPv4 mapped address. Return None otherwise.


if (self._ip >> <NUM_LIT>) != <NUM_LIT>:<EOL><INDENT>return None<EOL><DEDENT>return (IPv4Address((self._ip >> <NUM_LIT:64>) & <NUM_LIT>),<EOL>IPv4Address(~self._ip & <NUM_LIT>))<EOL>

Tuple of embedded teredo IPs.

        Returns:
            Tuple of the (server, client) IPs or None if the address
            doesn't appear to be a teredo address (doesn't start with
            2001::/32)


if (self._ip >> <NUM_LIT>) != <NUM_LIT>:<EOL><INDENT>return None<EOL><DEDENT>return IPv4Address((self._ip >> <NUM_LIT>) & <NUM_LIT>)<EOL>

Return the IPv4 6to4 embedded address.

        Returns:
            The IPv4 6to4-embedded address if present or None if the
            address doesn't appear to contain a 6to4 embedded address.


def __init__(self, address, strict=True):

_BaseV6.__init__(self, address)<EOL>_BaseNetwork.__init__(self, address)<EOL>if isinstance(address, _compat_int_types):<EOL><INDENT>self.network_address = IPv6Address(address)<EOL>self._prefixlen = self._max_prefixlen<EOL>self.netmask = IPv6Address(self._ALL_ONES)<EOL>return<EOL><DEDENT>if isinstance(address, bytes):<EOL><INDENT>self.network_address = IPv6Address(address)<EOL>self._prefixlen = self._max_prefixlen<EOL>self.netmask = IPv6Address(self._ALL_ONES)<EOL>return<EOL><DEDENT>addr = _split_optional_netmask(address)<EOL>self.network_address = IPv6Address(self._ip_int_from_string(addr[<NUM_LIT:0>]))<EOL>if len(addr) == <NUM_LIT:2>:<EOL><INDENT>if self._is_valid_netmask(addr[<NUM_LIT:1>]):<EOL><INDENT>self._prefixlen = int(addr[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>raise NetmaskValueError('<STR_LIT>'<EOL>% addr[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self._prefixlen = self._max_prefixlen<EOL><DEDENT>self.netmask = IPv6Address(self._ip_int_from_prefix(self._prefixlen))<EOL>if strict:<EOL><INDENT>if (IPv6Address(int(self.network_address) & int(self.netmask)) !=<EOL>self.network_address):<EOL><INDENT>raise ValueError('<STR_LIT>' % self)<EOL><DEDENT><DEDENT>self.network_address = IPv6Address(int(self.network_address) &<EOL>int(self.netmask))<EOL>if self._prefixlen == (self._max_prefixlen - <NUM_LIT:1>):<EOL><INDENT>self.hosts = self.__iter__<EOL><DEDENT>

Instantiate a new IPv6 Network object.

        Args:
            address: A string or integer representing the IPv6 network or the
              IP and prefix/netmask.
              '2001:db8::/128'
              '2001:db8:0000:0000:0000:0000:0000:0000/128'
              '2001:db8::'
              are all functionally the same in IPv6.  That is to say,
              failing to provide a subnetmask will create an object with
              a mask of /128.

              Additionally, an integer can be passed, so
              IPv6Network('2001:db8::') ==
                IPv6Network(42540766411282592856903984951653826560)
              or, more generally
              IPv6Network(int(IPv6Network('2001:db8::'))) ==
                IPv6Network('2001:db8::')

            strict: A boolean. If true, ensure that we have been passed
              A true network address, eg, 2001:db8::1000/124 and not an
              IP address on a network, eg, 2001:db8::1/124.

        Raises:
            AddressValueError: If address isn't a valid IPv6 address.
            NetmaskValueError: If the netmask isn't valid for
              an IPv6 address.
            ValueError: If strict was True and a network address was not
              supplied.


try:<EOL><INDENT>prefixlen = int(prefixlen)<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>return <NUM_LIT:0> <= prefixlen <= self._max_prefixlen<EOL>

Verify that the netmask/prefixlen is valid.

        Args:
            prefixlen: A string, the netmask in prefix length format.

        Returns:
            A boolean, True if the prefix represents a valid IPv6
            netmask.


return (self.network_address.is_site_local and<EOL>self.broadcast_address.is_site_local)<EOL>


option_group = OptionGroup(parser, group['<STR_LIT:name>'])<EOL>for option in group['<STR_LIT>']:<EOL><INDENT>option_group.add_option(option.make())<EOL><DEDENT>return option_group<EOL>

Return an OptionGroup object
group  -- assumed to be dict with 'name' and 'options' keys
parser -- an optparse Parser


<EOL>if '<STR_LIT>' not in os.environ:<EOL><INDENT>return<EOL><DEDENT>cwords = os.environ['<STR_LIT>'].split()[<NUM_LIT:1>:]<EOL>cword = int(os.environ['<STR_LIT>'])<EOL>try:<EOL><INDENT>current = cwords[cword - <NUM_LIT:1>]<EOL><DEDENT>except IndexError:<EOL><INDENT>current = '<STR_LIT>'<EOL><DEDENT>subcommands = [cmd for cmd, summary in get_summaries()]<EOL>options = []<EOL>try:<EOL><INDENT>subcommand_name = [w for w in cwords if w in subcommands][<NUM_LIT:0>]<EOL><DEDENT>except IndexError:<EOL><INDENT>subcommand_name = None<EOL><DEDENT>parser = create_main_parser()<EOL>if subcommand_name:<EOL><INDENT>if subcommand_name == '<STR_LIT>':<EOL><INDENT>sys.exit(<NUM_LIT:1>)<EOL><DEDENT>if subcommand_name == '<STR_LIT>' and not current.startswith('<STR_LIT:->'):<EOL><INDENT>installed = []<EOL>lc = current.lower()<EOL>for dist in get_installed_distributions(local_only=True):<EOL><INDENT>if dist.key.startswith(lc) and dist.key not in cwords[<NUM_LIT:1>:]:<EOL><INDENT>installed.append(dist.key)<EOL><DEDENT><DEDENT>if installed:<EOL><INDENT>for dist in installed:<EOL><INDENT>print(dist)<EOL><DEDENT>sys.exit(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>subcommand = commands_dict[subcommand_name]()<EOL>options += [(opt.get_opt_string(), opt.nargs)<EOL>for opt in subcommand.parser.option_list_all<EOL>if opt.help != optparse.SUPPRESS_HELP]<EOL>prev_opts = [x.split('<STR_LIT:=>')[<NUM_LIT:0>] for x in cwords[<NUM_LIT:1>:cword - <NUM_LIT:1>]]<EOL>options = [(x, v) for (x, v) in options if x not in prev_opts]<EOL>options = [(k, v) for k, v in options if k.startswith(current)]<EOL>for option in options:<EOL><INDENT>opt_label = option[<NUM_LIT:0>]<EOL>if option[<NUM_LIT:1>]:<EOL><INDENT>opt_label += '<STR_LIT:=>'<EOL><DEDENT>print(opt_label)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if current.startswith('<STR_LIT:->') or current.startswith('<STR_LIT>'):<EOL><INDENT>opts = [i.option_list for i in parser.option_groups]<EOL>opts.append(parser.option_list)<EOL>opts = (o for it in opts for o in it)<EOL>subcommands += [i.get_opt_string() for i in opts<EOL>if i.help != optparse.SUPPRESS_HELP]<EOL><DEDENT>print('<STR_LIT:U+0020>'.join([x for x in subcommands if x.startswith(current)]))<EOL><DEDENT>sys.exit(<NUM_LIT:1>)<EOL>

Command and option completion for the main option parser (and options)
    and its subcommands (and options).

    Enable by sourcing one of the completion shell scripts (bash or zsh).


def _format_option_strings(self, option, mvarfmt='<STR_LIT>', optsep='<STR_LIT:U+002CU+0020>'):

opts = []<EOL>if option._short_opts:<EOL><INDENT>opts.append(option._short_opts[<NUM_LIT:0>])<EOL><DEDENT>if option._long_opts:<EOL><INDENT>opts.append(option._long_opts[<NUM_LIT:0>])<EOL><DEDENT>if len(opts) > <NUM_LIT:1>:<EOL><INDENT>opts.insert(<NUM_LIT:1>, optsep)<EOL><DEDENT>if option.takes_value():<EOL><INDENT>metavar = option.metavar or option.dest.lower()<EOL>opts.append(mvarfmt % metavar.lower())<EOL><DEDENT>return '<STR_LIT>'.join(opts)<EOL>

Return a comma-separated list of option strings and metavars.

:param option:  tuple of (short opt, long opt), e.g: ('-f', '--format')
:param mvarfmt: metavar format string - evaluated as mvarfmt % metavar
:param optsep:  separator


msg = '<STR_LIT>' % self.indent_lines(textwrap.dedent(usage), "<STR_LIT:U+0020>")<EOL>return msg<EOL>

Ensure there is only one newline between usage and the first heading
if there is no description.


def insert_option_group(self, idx, *args, **kwargs):

group = self.add_option_group(*args, **kwargs)<EOL>self.option_groups.pop()<EOL>self.option_groups.insert(idx, group)<EOL>return group<EOL>

Insert an OptionGroup at a given position.


res = self.option_list[:]<EOL>for i in self.option_groups:<EOL><INDENT>res.extend(i.option_list)<EOL><DEDENT>return res<EOL>

Get a list of all options, including those in option groups.


<EOL>config = {}<EOL>for section in ('<STR_LIT>', self.name):<EOL><INDENT>config.update(<EOL>self.normalize_keys(self.get_config_section(section))<EOL>)<EOL><DEDENT>if not self.isolated:<EOL><INDENT>config.update(self.normalize_keys(self.get_environ_vars()))<EOL><DEDENT>for key, val in config.items():<EOL><INDENT>option = self.get_option(key)<EOL>if option is not None:<EOL><INDENT>if not val:<EOL><INDENT>continue<EOL><DEDENT>if option.action in ('<STR_LIT:store_true>', '<STR_LIT>', '<STR_LIT:count>'):<EOL><INDENT>val = strtobool(val)<EOL><DEDENT>if option.action == '<STR_LIT>':<EOL><INDENT>val = val.split()<EOL>val = [self.check_default(option, key, v) for v in val]<EOL><DEDENT>else:<EOL><INDENT>val = self.check_default(option, key, val)<EOL><DEDENT>defaults[option.dest] = val<EOL><DEDENT><DEDENT>return defaults<EOL>

Updates the given defaults with values from the config files and
        the environ. Does a little special handling for certain types of
        options (lists).


normalized = {}<EOL>for key, val in items:<EOL><INDENT>key = key.replace('<STR_LIT:_>', '<STR_LIT:->')<EOL>if not key.startswith('<STR_LIT>'):<EOL><INDENT>key = '<STR_LIT>' % key  <EOL><DEDENT>normalized[key] = val<EOL><DEDENT>return normalized<EOL>

Return a config dictionary with normalized keys regardless of
        whether the keys were specified in environment variables or in config
        files


if self.config.has_section(name):<EOL><INDENT>return self.config.items(name)<EOL><DEDENT>return []<EOL>


for key, val in os.environ.items():<EOL><INDENT>if _environ_prefix_re.search(key):<EOL><INDENT>yield (_environ_prefix_re.sub("<STR_LIT>", key).lower(), val)<EOL><DEDENT><DEDENT>

Returns a generator with all environmental vars with prefix PIP_


if not self.process_default_values:<EOL><INDENT>return optparse.Values(self.defaults)<EOL><DEDENT>defaults = self.update_defaults(self.defaults.copy())  <EOL>for option in self._get_all_options():<EOL><INDENT>default = defaults.get(option.dest)<EOL>if isinstance(default, string_types):<EOL><INDENT>opt_str = option.get_opt_string()<EOL>defaults[option.dest] = option.check_value(opt_str, default)<EOL><DEDENT><DEDENT>return optparse.Values(defaults)<EOL>

Overridding to make updating the defaults after instantiation of
        the option parser possible, update_defaults() does the dirty work.


if hasattr(sys, '<STR_LIT>'):<EOL><INDENT>pyimpl = '<STR_LIT>'<EOL><DEDENT>elif sys.platform.startswith('<STR_LIT>'):<EOL><INDENT>pyimpl = '<STR_LIT>'<EOL><DEDENT>elif sys.platform == '<STR_LIT>':<EOL><INDENT>pyimpl = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>pyimpl = '<STR_LIT>'<EOL><DEDENT>return pyimpl<EOL>


return '<STR_LIT>'.join(map(str, sys.version_info[:<NUM_LIT:2>]))<EOL>


<EOL>return distutils.util.get_platform().replace('<STR_LIT:.>', '<STR_LIT:_>').replace('<STR_LIT:->', '<STR_LIT:_>')<EOL>

Return our platform name 'win32', 'linux_x86_64


supported = []<EOL>if versions is None:<EOL><INDENT>versions = []<EOL>major = sys.version_info[<NUM_LIT:0>]<EOL>for minor in range(sys.version_info[<NUM_LIT:1>], -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>versions.append('<STR_LIT>'.join(map(str, (major, minor))))<EOL><DEDENT><DEDENT>impl = get_abbr_impl()<EOL>abis = []<EOL>try:<EOL><INDENT>soabi = sysconfig.get_config_var('<STR_LIT>')<EOL><DEDENT>except IOError as e:  <EOL><INDENT>warnings.warn("<STR_LIT>".format(e), RuntimeWarning)<EOL>soabi = None<EOL><DEDENT>if soabi and soabi.startswith('<STR_LIT>'):<EOL><INDENT>abis[<NUM_LIT:0>:<NUM_LIT:0>] = ['<STR_LIT>' + soabi.split('<STR_LIT:->', <NUM_LIT:1>)[-<NUM_LIT:1>]]<EOL><DEDENT>abi3s = set()<EOL>import imp<EOL>for suffix in imp.get_suffixes():<EOL><INDENT>if suffix[<NUM_LIT:0>].startswith('<STR_LIT>'):<EOL><INDENT>abi3s.add(suffix[<NUM_LIT:0>].split('<STR_LIT:.>', <NUM_LIT:2>)[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>abis.extend(sorted(list(abi3s)))<EOL>abis.append('<STR_LIT:none>')<EOL>if not noarch:<EOL><INDENT>arch = get_platform()<EOL>if sys.platform == '<STR_LIT>':<EOL><INDENT>match = _osx_arch_pat.match(arch)<EOL>if match:<EOL><INDENT>name, major, minor, actual_arch = match.groups()<EOL>actual_arches = [actual_arch]<EOL>if actual_arch in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>actual_arches.append('<STR_LIT>')<EOL><DEDENT>if actual_arch in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>actual_arches.append('<STR_LIT>')<EOL><DEDENT>if actual_arch in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>actual_arches.append('<STR_LIT>')<EOL><DEDENT>if actual_arch in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>actual_arches.append('<STR_LIT>')<EOL><DEDENT>if actual_arch in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>actual_arches.append('<STR_LIT>')<EOL><DEDENT>tpl = '<STR_LIT>'.format(name, major)<EOL>arches = []<EOL>for m in range(int(minor) + <NUM_LIT:1>):<EOL><INDENT>for a in actual_arches:<EOL><INDENT>arches.append(tpl % (m, a))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>arches = [arch]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>arches = [arch]<EOL><DEDENT>for abi in abis:<EOL><INDENT>for arch in arches:<EOL><INDENT>supported.append(('<STR_LIT>' % (impl, versions[<NUM_LIT:0>]), abi, arch))<EOL><DEDENT><DEDENT><DEDENT>for i, version in enumerate(versions):<EOL><INDENT>supported.append(('<STR_LIT>' % (impl, version), '<STR_LIT:none>', '<STR_LIT>'))<EOL>if i == <NUM_LIT:0>:<EOL><INDENT>supported.append(('<STR_LIT>' % (impl, versions[<NUM_LIT:0>][<NUM_LIT:0>]), '<STR_LIT:none>', '<STR_LIT>'))<EOL><DEDENT><DEDENT>for i, version in enumerate(versions):<EOL><INDENT>supported.append(('<STR_LIT>' % (version,), '<STR_LIT:none>', '<STR_LIT>'))<EOL>if i == <NUM_LIT:0>:<EOL><INDENT>supported.append(('<STR_LIT>' % (version[<NUM_LIT:0>]), '<STR_LIT:none>', '<STR_LIT>'))<EOL><DEDENT><DEDENT>return supported<EOL>

Return a list of supported tags for each version specified in
    `versions`.

    :param versions: a list of string versions, of the form ["33", "32"],
        or None. The first version will be assumed to support our ABI.


funcobj.__isabstractmethod__ = True<EOL>return funcobj<EOL>

A decorator indicating abstract methods.

    Requires that the metaclass is ABCMeta or derived from it.  A
    class that has a metaclass derived from ABCMeta cannot be
    instantiated unless all of its abstract methods are overridden.
    The abstract methods can be called using any of the normal
    'super' call mechanisms.

    Usage:

        class C:
            __metaclass__ = ABCMeta
            @abstractmethod
            def my_abstract_method(self, ...):
                ...


if not isinstance(subclass, (type, types.ClassType)):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if issubclass(subclass, cls):<EOL><INDENT>return  <EOL><DEDENT>if issubclass(cls, subclass):<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>cls._abc_registry.add(subclass)<EOL>ABCMeta._abc_invalidation_counter += <NUM_LIT:1><EOL>


print >> file, "<STR_LIT>" % (cls.__module__, cls.__name__)<EOL>print >> file, "<STR_LIT>" % ABCMeta._abc_invalidation_counter<EOL>for name in sorted(cls.__dict__.keys()):<EOL><INDENT>if name.startswith("<STR_LIT>"):<EOL><INDENT>value = getattr(cls, name)<EOL>print >> file, "<STR_LIT>" % (name, value)<EOL><DEDENT><DEDENT>


<EOL>subclass = getattr(instance, '<STR_LIT>', None)<EOL>if subclass is not None and subclass in cls._abc_cache:<EOL><INDENT>return True<EOL><DEDENT>subtype = type(instance)<EOL>if subtype is _InstanceType:<EOL><INDENT>subtype = subclass<EOL><DEDENT>if subtype is subclass or subclass is None:<EOL><INDENT>if (cls._abc_negative_cache_version ==<EOL>ABCMeta._abc_invalidation_counter and<EOL>subtype in cls._abc_negative_cache):<EOL><INDENT>return False<EOL><DEDENT>return cls.__subclasscheck__(subtype)<EOL><DEDENT>return (cls.__subclasscheck__(subclass) or<EOL>cls.__subclasscheck__(subtype))<EOL>


<EOL>if subclass in cls._abc_cache:<EOL><INDENT>return True<EOL><DEDENT>if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:<EOL><INDENT>cls._abc_negative_cache = WeakSet()<EOL>cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter<EOL><DEDENT>elif subclass in cls._abc_negative_cache:<EOL><INDENT>return False<EOL><DEDENT>ok = cls.__subclasshook__(subclass)<EOL>if ok is not NotImplemented:<EOL><INDENT>assert isinstance(ok, bool)<EOL>if ok:<EOL><INDENT>cls._abc_cache.add(subclass)<EOL><DEDENT>else:<EOL><INDENT>cls._abc_negative_cache.add(subclass)<EOL><DEDENT>return ok<EOL><DEDENT>if cls in getattr(subclass, '<STR_LIT>', ()):<EOL><INDENT>cls._abc_cache.add(subclass)<EOL>return True<EOL><DEDENT>for rcls in cls._abc_registry:<EOL><INDENT>if issubclass(subclass, rcls):<EOL><INDENT>cls._abc_cache.add(subclass)<EOL>return True<EOL><DEDENT><DEDENT>for scls in cls.__subclasses__():<EOL><INDENT>if issubclass(subclass, scls):<EOL><INDENT>cls._abc_cache.add(subclass)<EOL>return True<EOL><DEDENT><DEDENT>cls._abc_negative_cache.add(subclass)<EOL>return False<EOL>


return s.replace("<STR_LIT:/>", "<STR_LIT:\\>").lower()<EOL>

Normalize case of pathname.

    Makes all characters lowercase and all slashes into backslashes.


s = splitdrive(s)[<NUM_LIT:1>]<EOL>return s != '<STR_LIT>' and s[:<NUM_LIT:1>] in '<STR_LIT>'<EOL>


result_drive, result_path = splitdrive(path)<EOL>for p in paths:<EOL><INDENT>p_drive, p_path = splitdrive(p)<EOL>if p_path and p_path[<NUM_LIT:0>] in '<STR_LIT>':<EOL><INDENT>if p_drive or not result_drive:<EOL><INDENT>result_drive = p_drive<EOL><DEDENT>result_path = p_path<EOL>continue<EOL><DEDENT>elif p_drive and p_drive != result_drive:<EOL><INDENT>if p_drive.lower() != result_drive.lower():<EOL><INDENT>result_drive = p_drive<EOL>result_path = p_path<EOL>continue<EOL><DEDENT>result_drive = p_drive<EOL><DEDENT>if result_path and result_path[-<NUM_LIT:1>] not in '<STR_LIT>':<EOL><INDENT>result_path = result_path + '<STR_LIT:\\>'<EOL><DEDENT>result_path = result_path + p_path<EOL><DEDENT>if (result_path and result_path[<NUM_LIT:0>] not in '<STR_LIT>' and<EOL>result_drive and result_drive[-<NUM_LIT:1>:] != '<STR_LIT::>'):<EOL><INDENT>return result_drive + sep + result_path<EOL><DEDENT>return result_drive + result_path<EOL>

Join two or more pathname components, inserting "\\" as needed.


if len(p) > <NUM_LIT:1>:<EOL><INDENT>normp = p.replace(altsep, sep)<EOL>if (normp[<NUM_LIT:0>:<NUM_LIT:2>] == sep*<NUM_LIT:2>) and (normp[<NUM_LIT:2>:<NUM_LIT:3>] != sep):<EOL><INDENT>index = normp.find(sep, <NUM_LIT:2>)<EOL>if index == -<NUM_LIT:1>:<EOL><INDENT>return '<STR_LIT>', p<EOL><DEDENT>index2 = normp.find(sep, index + <NUM_LIT:1>)<EOL>if index2 == index + <NUM_LIT:1>:<EOL><INDENT>return '<STR_LIT>', p<EOL><DEDENT>if index2 == -<NUM_LIT:1>:<EOL><INDENT>index2 = len(p)<EOL><DEDENT>return p[:index2], p[index2:]<EOL><DEDENT>if normp[<NUM_LIT:1>] == '<STR_LIT::>':<EOL><INDENT>return p[:<NUM_LIT:2>], p[<NUM_LIT:2>:]<EOL><DEDENT><DEDENT>return '<STR_LIT>', p<EOL>

Split a pathname into drive/UNC sharepoint and relative path specifiers.
    Returns a 2-tuple (drive_or_unc, path); either part may be empty.

    If you assign
        result = splitdrive(p)
    It is always true that:
        result[0] + result[1] == p

    If the path contained a drive letter, drive_or_unc will contain everything
    up to and including the colon.  e.g. splitdrive("c:/dir") returns ("c:", "/dir")

    If the path contained a UNC path, the drive_or_unc will contain the host name
    and share up to but not including the fourth directory separator character.
    e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir")

    Paths cannot contain both a drive letter and a UNC path.


if p[<NUM_LIT:1>:<NUM_LIT:2>] == '<STR_LIT::>':<EOL><INDENT>return '<STR_LIT>', p <EOL><DEDENT>firstTwo = p[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>if firstTwo == '<STR_LIT>' or firstTwo == '<STR_LIT>':<EOL><INDENT>normp = p.replace('<STR_LIT:\\>', '<STR_LIT:/>')<EOL>index = normp.find('<STR_LIT:/>', <NUM_LIT:2>)<EOL>if index <= <NUM_LIT:2>:<EOL><INDENT>return '<STR_LIT>', p<EOL><DEDENT>index2 = normp.find('<STR_LIT:/>', index + <NUM_LIT:1>)<EOL>if index2 == index + <NUM_LIT:1>:<EOL><INDENT>return '<STR_LIT>', p<EOL><DEDENT>if index2 == -<NUM_LIT:1>:<EOL><INDENT>index2 = len(p)<EOL><DEDENT>return p[:index2], p[index2:]<EOL><DEDENT>return '<STR_LIT>', p<EOL>

Split a pathname into UNC mount point and relative path specifiers.

    Return a 2-tuple (unc, rest); either part may be empty.
    If unc is not empty, it has the form '//host/mount' (or similar
    using backslashes).  unc+rest is always the input path.
    Paths containing drive letters never have a UNC part.


d, p = splitdrive(p)<EOL>i = len(p)<EOL>while i and p[i-<NUM_LIT:1>] not in '<STR_LIT>':<EOL><INDENT>i = i - <NUM_LIT:1><EOL><DEDENT>head, tail = p[:i], p[i:]  <EOL>head2 = head<EOL>while head2 and head2[-<NUM_LIT:1>] in '<STR_LIT>':<EOL><INDENT>head2 = head2[:-<NUM_LIT:1>]<EOL><DEDENT>head = head2 or head<EOL>return d + head, tail<EOL>

Split a pathname.

    Return tuple (head, tail) where tail is everything after the final slash.
    Either part may be empty.


return split(p)[<NUM_LIT:1>]<EOL>

Returns the final component of a pathname


return split(p)[<NUM_LIT:0>]<EOL>

Returns the directory component of a pathname


return False<EOL>

Test for symbolic link.
    On WindowsNT/95 and OS/2 always returns false


unc, rest = splitunc(path)<EOL>if unc:<EOL><INDENT>return rest in ("<STR_LIT>", "<STR_LIT:/>", "<STR_LIT:\\>")<EOL><DEDENT>p = splitdrive(path)[<NUM_LIT:1>]<EOL>return len(p) == <NUM_LIT:1> and p[<NUM_LIT:0>] in '<STR_LIT>'<EOL>

Test whether a path is a mount point (defined as root of drive)


warnings.warnpy3k("<STR_LIT>",<EOL>stacklevel=<NUM_LIT:2>)<EOL>try:<EOL><INDENT>names = os.listdir(top)<EOL><DEDENT>except os.error:<EOL><INDENT>return<EOL><DEDENT>func(arg, top, names)<EOL>for name in names:<EOL><INDENT>name = join(top, name)<EOL>if isdir(name):<EOL><INDENT>walk(name, func, arg)<EOL><DEDENT><DEDENT>

Directory tree walk with callback function.

    For each directory in the directory tree rooted at top (including top
    itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
    dirname is the name of the directory, and fnames a list of the names of
    the files and subdirectories in dirname (excluding '.' and '..').  func
    may modify the fnames list in-place (e.g. via del or slice assignment),
    and walk will only recurse into the subdirectories whose names remain in
    fnames; this can be used to implement a filter, or to impose a specific
    order of visiting.  No semantics are defined for, or required of, arg,
    beyond that arg is always passed to func.  It can be used, e.g., to pass
    a filename pattern, or a mutable object designed to accumulate
    statistics.  Passing None for arg is common.


if path[:<NUM_LIT:1>] != '<STR_LIT>':<EOL><INDENT>return path<EOL><DEDENT>i, n = <NUM_LIT:1>, len(path)<EOL>while i < n and path[i] not in '<STR_LIT>':<EOL><INDENT>i = i + <NUM_LIT:1><EOL><DEDENT>if '<STR_LIT>' in os.environ:<EOL><INDENT>userhome = os.environ['<STR_LIT>']<EOL><DEDENT>elif '<STR_LIT>' in os.environ:<EOL><INDENT>userhome = os.environ['<STR_LIT>']<EOL><DEDENT>elif not '<STR_LIT>' in os.environ:<EOL><INDENT>return path<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>drive = os.environ['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>drive = '<STR_LIT>'<EOL><DEDENT>userhome = join(drive, os.environ['<STR_LIT>'])<EOL><DEDENT>if i != <NUM_LIT:1>: <EOL><INDENT>userhome = join(dirname(userhome), path[<NUM_LIT:1>:i])<EOL><DEDENT>return userhome + path[i:]<EOL>

Expand ~ and ~user constructs.

    If user or $HOME is unknown, do nothing.


if '<STR_LIT:$>' not in path and '<STR_LIT:%>' not in path:<EOL><INDENT>return path<EOL><DEDENT>import string<EOL>varchars = string.ascii_letters + string.digits + '<STR_LIT>'<EOL>if isinstance(path, _unicode):<EOL><INDENT>encoding = sys.getfilesystemencoding()<EOL>def getenv(var):<EOL><INDENT>return os.environ[var.encode(encoding)].decode(encoding)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>def getenv(var):<EOL><INDENT>return os.environ[var]<EOL><DEDENT><DEDENT>res = '<STR_LIT>'<EOL>index = <NUM_LIT:0><EOL>pathlen = len(path)<EOL>while index < pathlen:<EOL><INDENT>c = path[index]<EOL>if c == '<STR_LIT>':   <EOL><INDENT>path = path[index + <NUM_LIT:1>:]<EOL>pathlen = len(path)<EOL>try:<EOL><INDENT>index = path.index('<STR_LIT>')<EOL>res = res + '<STR_LIT>' + path[:index + <NUM_LIT:1>]<EOL><DEDENT>except ValueError:<EOL><INDENT>res = res + c + path<EOL>index = pathlen - <NUM_LIT:1><EOL><DEDENT><DEDENT>elif c == '<STR_LIT:%>':  <EOL><INDENT>if path[index + <NUM_LIT:1>:index + <NUM_LIT:2>] == '<STR_LIT:%>':<EOL><INDENT>res = res + c<EOL>index = index + <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>path = path[index+<NUM_LIT:1>:]<EOL>pathlen = len(path)<EOL>try:<EOL><INDENT>index = path.index('<STR_LIT:%>')<EOL><DEDENT>except ValueError:<EOL><INDENT>res = res + '<STR_LIT:%>' + path<EOL>index = pathlen - <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>var = path[:index]<EOL>try:<EOL><INDENT>res = res + getenv(var)<EOL><DEDENT>except KeyError:<EOL><INDENT>res = res + '<STR_LIT:%>' + var + '<STR_LIT:%>'<EOL><DEDENT><DEDENT><DEDENT><DEDENT>elif c == '<STR_LIT:$>':  <EOL><INDENT>if path[index + <NUM_LIT:1>:index + <NUM_LIT:2>] == '<STR_LIT:$>':<EOL><INDENT>res = res + c<EOL>index = index + <NUM_LIT:1><EOL><DEDENT>elif path[index + <NUM_LIT:1>:index + <NUM_LIT:2>] == '<STR_LIT:{>':<EOL><INDENT>path = path[index+<NUM_LIT:2>:]<EOL>pathlen = len(path)<EOL>try:<EOL><INDENT>index = path.index('<STR_LIT:}>')<EOL>var = path[:index]<EOL>try:<EOL><INDENT>res = res + getenv(var)<EOL><DEDENT>except KeyError:<EOL><INDENT>res = res + '<STR_LIT>' + var + '<STR_LIT:}>'<EOL><DEDENT><DEDENT>except ValueError:<EOL><INDENT>res = res + '<STR_LIT>' + path<EOL>index = pathlen - <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>var = '<STR_LIT>'<EOL>index = index + <NUM_LIT:1><EOL>c = path[index:index + <NUM_LIT:1>]<EOL>while c != '<STR_LIT>' and c in varchars:<EOL><INDENT>var = var + c<EOL>index = index + <NUM_LIT:1><EOL>c = path[index:index + <NUM_LIT:1>]<EOL><DEDENT>try:<EOL><INDENT>res = res + getenv(var)<EOL><DEDENT>except KeyError:<EOL><INDENT>res = res + '<STR_LIT:$>' + var<EOL><DEDENT>if c != '<STR_LIT>':<EOL><INDENT>index = index - <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>res = res + c<EOL><DEDENT>index = index + <NUM_LIT:1><EOL><DEDENT>return res<EOL>

Expand shell variables of the forms $var, ${var} and %var%.

    Unknown variables are left unchanged.


<EOL>backslash, dot = (u'<STR_LIT:\\>', u'<STR_LIT:.>') if isinstance(path, _unicode) else ('<STR_LIT:\\>', '<STR_LIT:.>')<EOL>if path.startswith(('<STR_LIT>', '<STR_LIT>')):<EOL><INDENT>return path<EOL><DEDENT>path = path.replace("<STR_LIT:/>", "<STR_LIT:\\>")<EOL>prefix, path = splitdrive(path)<EOL>if prefix == '<STR_LIT>':<EOL><INDENT>while path[:<NUM_LIT:1>] == "<STR_LIT:\\>":<EOL><INDENT>prefix = prefix + backslash<EOL>path = path[<NUM_LIT:1>:]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if path.startswith("<STR_LIT:\\>"):<EOL><INDENT>prefix = prefix + backslash<EOL>path = path.lstrip("<STR_LIT:\\>")<EOL><DEDENT><DEDENT>comps = path.split("<STR_LIT:\\>")<EOL>i = <NUM_LIT:0><EOL>while i < len(comps):<EOL><INDENT>if comps[i] in ('<STR_LIT:.>', '<STR_LIT>'):<EOL><INDENT>del comps[i]<EOL><DEDENT>elif comps[i] == '<STR_LIT:..>':<EOL><INDENT>if i > <NUM_LIT:0> and comps[i-<NUM_LIT:1>] != '<STR_LIT:..>':<EOL><INDENT>del comps[i-<NUM_LIT:1>:i+<NUM_LIT:1>]<EOL>i -= <NUM_LIT:1><EOL><DEDENT>elif i == <NUM_LIT:0> and prefix.endswith("<STR_LIT:\\>"):<EOL><INDENT>del comps[i]<EOL><DEDENT>else:<EOL><INDENT>i += <NUM_LIT:1><EOL><DEDENT><DEDENT>else:<EOL><INDENT>i += <NUM_LIT:1><EOL><DEDENT><DEDENT>if not prefix and not comps:<EOL><INDENT>comps.append(dot)<EOL><DEDENT>return prefix + backslash.join(comps)<EOL>

Normalize path, eliminating double slashes, etc.


if not path:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>start_is_unc, start_prefix, start_list = _abspath_split(start)<EOL>path_is_unc, path_prefix, path_list = _abspath_split(path)<EOL>if path_is_unc ^ start_is_unc:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>% (path, start))<EOL><DEDENT>if path_prefix.lower() != start_prefix.lower():<EOL><INDENT>if path_is_unc:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>% (path_prefix, start_prefix))<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>% (path_prefix, start_prefix))<EOL><DEDENT><DEDENT>i = <NUM_LIT:0><EOL>for e1, e2 in zip(start_list, path_list):<EOL><INDENT>if e1.lower() != e2.lower():<EOL><INDENT>break<EOL><DEDENT>i += <NUM_LIT:1><EOL><DEDENT>rel_list = [pardir] * (len(start_list)-i) + path_list[i:]<EOL>if not rel_list:<EOL><INDENT>return curdir<EOL><DEDENT>return join(*rel_list)<EOL>


try:<EOL><INDENT>os.stat(path)<EOL><DEDENT>except os.error:<EOL><INDENT>return False<EOL><DEDENT>return True<EOL>

Test whether a path exists.  Returns False for broken symbolic links


try:<EOL><INDENT>st = os.stat(path)<EOL><DEDENT>except os.error:<EOL><INDENT>return False<EOL><DEDENT>return stat.S_ISREG(st.st_mode)<EOL>


try:<EOL><INDENT>st = os.stat(s)<EOL><DEDENT>except os.error:<EOL><INDENT>return False<EOL><DEDENT>return stat.S_ISDIR(st.st_mode)<EOL>

Return true if the pathname refers to an existing directory.


return os.stat(filename).st_size<EOL>

Return the size of a file, reported by os.stat().


return os.stat(filename).st_mtime<EOL>

Return the last modification time of a file, reported by os.stat().


return os.stat(filename).st_atime<EOL>

Return the last access time of a file, reported by os.stat().


return os.stat(filename).st_ctime<EOL>

Return the metadata change time of a file, reported by os.stat().


if not m: return '<STR_LIT>'<EOL>s1 = min(m)<EOL>s2 = max(m)<EOL>for i, c in enumerate(s1):<EOL><INDENT>if c != s2[i]:<EOL><INDENT>return s1[:i]<EOL><DEDENT><DEDENT>return s1<EOL>

Given a list of pathnames, returns the longest common leading component


sepIndex = p.rfind(sep)<EOL>if altsep:<EOL><INDENT>altsepIndex = p.rfind(altsep)<EOL>sepIndex = max(sepIndex, altsepIndex)<EOL><DEDENT>dotIndex = p.rfind(extsep)<EOL>if dotIndex > sepIndex:<EOL><INDENT>filenameIndex = sepIndex + <NUM_LIT:1><EOL>while filenameIndex < dotIndex:<EOL><INDENT>if p[filenameIndex] != extsep:<EOL><INDENT>return p[:dotIndex], p[dotIndex:]<EOL><DEDENT>filenameIndex += <NUM_LIT:1><EOL><DEDENT><DEDENT>return p, '<STR_LIT>'<EOL>

Split the extension from a pathname.

    Extension is everything from the last dot to the end, ignoring
    leading dots.  Returns "(root, ext)"; ext may be empty.


raise StopIteration<EOL>

Return the next item from the iterator. When exhausted, raise StopIteration


return cls(it)<EOL>

Construct an instance of the class from any iterable input.

        Must override this method if the class constructor signature
        does not accept an iterable for an input.


for value in other:<EOL><INDENT>if value in self:<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>

Return True if two sets have a null intersection.


MAX = sys.maxint<EOL>MASK = <NUM_LIT:2> * MAX + <NUM_LIT:1><EOL>n = len(self)<EOL>h = <NUM_LIT> * (n + <NUM_LIT:1>)<EOL>h &= MASK<EOL>for x in self:<EOL><INDENT>hx = hash(x)<EOL>h ^= (hx ^ (hx << <NUM_LIT:16>) ^ <NUM_LIT>)  * <NUM_LIT><EOL>h &= MASK<EOL><DEDENT>h = h * <NUM_LIT> + <NUM_LIT><EOL>h &= MASK<EOL>if h > MAX:<EOL><INDENT>h -= MASK + <NUM_LIT:1><EOL><DEDENT>if h == -<NUM_LIT:1>:<EOL><INDENT>h = <NUM_LIT><EOL><DEDENT>return h<EOL>

Compute the hash value of a set.

        Note that we don't define __hash__: not all sets are hashable.
        But if you define a hashable set type, its __hash__ should
        call this function.

        This must be compatible __eq__.

        All sets ought to compare equal if they contain the same
        elements, regardless of how they are implemented, and
        regardless of the order of the elements; so there's not much
        freedom for __eq__ or __hash__.  We match the algorithm used
        by the built-in frozenset type.


raise NotImplementedError<EOL>


raise NotImplementedError<EOL>

Remove an element.  Do not raise an exception if absent.


if value not in self:<EOL><INDENT>raise KeyError(value)<EOL><DEDENT>self.discard(value)<EOL>

Remove an element. If not a member, raise a KeyError.


it = iter(self)<EOL>try:<EOL><INDENT>value = next(it)<EOL><DEDENT>except StopIteration:<EOL><INDENT>raise KeyError<EOL><DEDENT>self.discard(value)<EOL>return value<EOL>

Return the popped value.  Raise KeyError if empty.


try:<EOL><INDENT>while True:<EOL><INDENT>self.pop()<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>

This is slow (creates N new iterators!) but effective.


try:<EOL><INDENT>return self[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>return default<EOL><DEDENT>

D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.


return iter(self)<EOL>

D.iterkeys() -> an iterator over the keys of D


for key in self:<EOL><INDENT>yield self[key]<EOL><DEDENT>

D.itervalues() -> an iterator over the values of D


for key in self:<EOL><INDENT>yield (key, self[key])<EOL><DEDENT>

D.iteritems() -> an iterator over the (key, value) items of D


return list(self)<EOL>


return [(key, self[key]) for key in self]<EOL>

D.items() -> list of D's (key, value) pairs, as 2-tuples


return [self[key] for key in self]<EOL>


try:<EOL><INDENT>value = self[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>if default is self.__marker:<EOL><INDENT>raise<EOL><DEDENT>return default<EOL><DEDENT>else:<EOL><INDENT>del self[key]<EOL>return value<EOL><DEDENT>

D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
          If key is not found, d is returned if given, otherwise KeyError is raised.


try:<EOL><INDENT>key = next(iter(self))<EOL><DEDENT>except StopIteration:<EOL><INDENT>raise KeyError<EOL><DEDENT>value = self[key]<EOL>del self[key]<EOL>return key, value<EOL>

D.popitem() -> (k, v), remove and return some (key, value) pair
           as a 2-tuple; but raise KeyError if D is empty.


try:<EOL><INDENT>while True:<EOL><INDENT>self.popitem()<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>

D.clear() -> None.  Remove all items from D.


if not args:<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>self = args[<NUM_LIT:0>]<EOL>args = args[<NUM_LIT:1>:]<EOL>if len(args) > <NUM_LIT:1>:<EOL><INDENT>raise TypeError('<STR_LIT>' %<EOL>len(args))<EOL><DEDENT>if args:<EOL><INDENT>other = args[<NUM_LIT:0>]<EOL>if isinstance(other, Mapping):<EOL><INDENT>for key in other:<EOL><INDENT>self[key] = other[key]<EOL><DEDENT><DEDENT>elif hasattr(other, "<STR_LIT>"):<EOL><INDENT>for key in other.keys():<EOL><INDENT>self[key] = other[key]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for key, value in other:<EOL><INDENT>self[key] = value<EOL><DEDENT><DEDENT><DEDENT>for key, value in kwds.items():<EOL><INDENT>self[key] = value<EOL><DEDENT>

D.update([E, ]**F) -> None.  Update D from mapping/iterable E and F.
            If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
            If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
            In either case, this is followed by: for k, v in F.items(): D[k] = v


def setdefault(self, key, default=None):

try:<EOL><INDENT>return self[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>self[key] = default<EOL><DEDENT>return default<EOL>

D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D


for i, v in enumerate(self):<EOL><INDENT>if v == value:<EOL><INDENT>return i<EOL><DEDENT><DEDENT>raise ValueError<EOL>

S.index(value) -> integer -- return first index of value.
           Raises ValueError if the value is not present.


return sum(<NUM_LIT:1> for v in self if v == value)<EOL>

S.count(value) -> integer -- return number of occurrences of value


@abstractmethod<EOL><INDENT>def insert(self, index, value):<DEDENT>

raise IndexError<EOL>

S.insert(index, object) -- insert object before index


self.insert(len(self), value)<EOL>

S.append(object) -- append object to the end of the sequence


n = len(self)<EOL>for i in range(n//<NUM_LIT:2>):<EOL><INDENT>self[i], self[n-i-<NUM_LIT:1>] = self[n-i-<NUM_LIT:1>], self[i]<EOL><DEDENT>


for v in values:<EOL><INDENT>self.append(v)<EOL><DEDENT>

S.extend(iterable) -- extend sequence by appending elements from the iterable


v = self[index]<EOL>del self[index]<EOL>return v<EOL>

S.pop([index]) -> item -- remove and return item at index (default last).
           Raise IndexError if list is empty or index is out of range.


del self[self.index(value)]<EOL>

S.remove(value) -- remove first occurrence of value.
           Raise ValueError if the value is not present.


def match(pattern, string, flags=<NUM_LIT:0>):

return _compile(pattern, flags).match(string)<EOL>

Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found.


def search(pattern, string, flags=<NUM_LIT:0>):

return _compile(pattern, flags).search(string)<EOL>

Scan through string looking for a match to the pattern, returning
    a match object, or None if no match was found.


def sub(pattern, repl, string, count=<NUM_LIT:0>, flags=<NUM_LIT:0>):

return _compile(pattern, flags).sub(repl, string, count)<EOL>

Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the match object and must return
    a replacement string to be used.


def subn(pattern, repl, string, count=<NUM_LIT:0>, flags=<NUM_LIT:0>):

return _compile(pattern, flags).subn(repl, string, count)<EOL>

Return a 2-tuple containing (new_string, number).
    new_string is the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in the source
    string by the replacement repl.  number is the number of
    substitutions that were made. repl can be either a string or a
    callable; if a string, backslash escapes in it are processed.
    If it is a callable, it's passed the match object and must
    return a replacement string to be used.


def split(pattern, string, maxsplit=<NUM_LIT:0>, flags=<NUM_LIT:0>):

return _compile(pattern, flags).split(string, maxsplit)<EOL>

Split the source string by the occurrences of the pattern,
    returning a list containing the resulting substrings.


def findall(pattern, string, flags=<NUM_LIT:0>):

return _compile(pattern, flags).findall(string)<EOL>

Return a list of all non-overlapping matches in the string.

    If one or more groups are present in the pattern, return a
    list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result.


return _compile(pattern, flags)<EOL>

Compile a regular expression pattern, returning a pattern object.


_cache.clear()<EOL>_cache_repl.clear()<EOL>


return _compile(pattern, flags|T)<EOL>

Compile a template pattern, returning a pattern object


s = list(pattern)<EOL>alphanum = _alphanum<EOL>for i, c in enumerate(pattern):<EOL><INDENT>if c not in alphanum:<EOL><INDENT>if c == "<STR_LIT>":<EOL><INDENT>s[i] = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>s[i] = "<STR_LIT:\\>" + c<EOL><DEDENT><DEDENT><DEDENT>return pattern[:<NUM_LIT:0>].join(s)<EOL>

Escape all non-alphanumeric characters in pattern.


def warnpy3k(message, category=None, stacklevel=<NUM_LIT:1>):

if sys.py3kwarning:<EOL><INDENT>if category is None:<EOL><INDENT>category = DeprecationWarning<EOL><DEDENT>warn(message, category, stacklevel+<NUM_LIT:1>)<EOL><DEDENT>

Issue a deprecation warning for Python 3.x related changes.

    Warnings are omitted unless Python is started with the -3 option.


def _show_warning(message, category, filename, lineno, file=None, line=None):

if file is None:<EOL><INDENT>file = sys.stderr<EOL>if file is None:<EOL><INDENT>return<EOL><DEDENT><DEDENT>try:<EOL><INDENT>file.write(formatwarning(message, category, filename, lineno, line))<EOL><DEDENT>except (IOError, UnicodeError):<EOL><INDENT>pass <EOL><DEDENT>

Hook to write a warning to a file; replace if you like.


def formatwarning(message, category, filename, lineno, line=None):

try:<EOL><INDENT>unicodetype = str<EOL><DEDENT>except NameError:<EOL><INDENT>unicodetype = ()<EOL><DEDENT>try:<EOL><INDENT>message = str(message)<EOL><DEDENT>except UnicodeEncodeError:<EOL><INDENT>pass<EOL><DEDENT>s =  "<STR_LIT>" % (lineno, category.__name__, message)<EOL>line = linecache.getline(filename, lineno) if line is None else line<EOL>if line:<EOL><INDENT>line = line.strip()<EOL>if isinstance(s, unicodetype) and isinstance(line, str):<EOL><INDENT>line = str(line, '<STR_LIT>')<EOL><DEDENT>s += "<STR_LIT>" % line<EOL><DEDENT>if isinstance(s, unicodetype) and isinstance(filename, str):<EOL><INDENT>enc = sys.getfilesystemencoding()<EOL>if enc:<EOL><INDENT>try:<EOL><INDENT>filename = str(filename, enc)<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>s = "<STR_LIT>" % (filename, s)<EOL>return s<EOL>

Function to format a warning the standard way.


def filterwarnings(action, message="<STR_LIT>", category=Warning, module="<STR_LIT>", lineno=<NUM_LIT:0>,<EOL>append=<NUM_LIT:0>):

import re<EOL>assert action in ("<STR_LIT:error>", "<STR_LIT:ignore>", "<STR_LIT>", "<STR_LIT:default>", "<STR_LIT>",<EOL>"<STR_LIT>"), "<STR_LIT>" % (action,)<EOL>assert isinstance(message, str), "<STR_LIT>"<EOL>assert isinstance(category, type),"<STR_LIT>"<EOL>assert issubclass(category, Warning), "<STR_LIT>"<EOL>assert isinstance(module, str), "<STR_LIT>"<EOL>assert isinstance(lineno, int) and lineno >= <NUM_LIT:0>,"<STR_LIT>"<EOL>item = (action, re.compile(message, re.I), category,<EOL>re.compile(module), int(lineno))<EOL>if append:<EOL><INDENT>filters.append(item)<EOL><DEDENT>else:<EOL><INDENT>filters.insert(<NUM_LIT:0>, item)<EOL><DEDENT>

Insert an entry into the list of warnings filters (at the front).

    'action' -- one of "error", "ignore", "always", "default", "module",
                or "once"
    'message' -- a regex that the warning message must match
    'category' -- a class that the warning must be a subclass of
    'module' -- a regex that the module name must match
    'lineno' -- an integer line number, 0 matches all warnings
    'append' -- if true, append to the list of filters


def simplefilter(action, category=Warning, lineno=<NUM_LIT:0>, append=<NUM_LIT:0>):

assert action in ("<STR_LIT:error>", "<STR_LIT:ignore>", "<STR_LIT>", "<STR_LIT:default>", "<STR_LIT>",<EOL>"<STR_LIT>"), "<STR_LIT>" % (action,)<EOL>assert isinstance(lineno, int) and lineno >= <NUM_LIT:0>,"<STR_LIT>"<EOL>item = (action, None, category, None, int(lineno))<EOL>if append:<EOL><INDENT>filters.append(item)<EOL><DEDENT>else:<EOL><INDENT>filters.insert(<NUM_LIT:0>, item)<EOL><DEDENT>

Insert a simple entry into the list of warnings filters (at the front).

    A simple filter matches all modules and messages.
    'action' -- one of "error", "ignore", "always", "default", "module",
                or "once"
    'category' -- a class that the warning must be a subclass of
    'lineno' -- an integer line number, 0 matches all warnings
    'append' -- if true, append to the list of filters


filters[:] = []<EOL>

Clear the list of warning filters, so that no filters are active.


def warn(message, category=None, stacklevel=<NUM_LIT:1>):

<EOL>if isinstance(message, Warning):<EOL><INDENT>category = message.__class__<EOL><DEDENT>if category is None:<EOL><INDENT>category = UserWarning<EOL><DEDENT>assert issubclass(category, Warning)<EOL>try:<EOL><INDENT>caller = sys._getframe(stacklevel)<EOL><DEDENT>except ValueError:<EOL><INDENT>globals = sys.__dict__<EOL>lineno = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>globals = caller.f_globals<EOL>lineno = caller.f_lineno<EOL><DEDENT>if '<STR_LIT>' in globals:<EOL><INDENT>module = globals['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>module = "<STR_LIT>"<EOL><DEDENT>filename = globals.get('<STR_LIT>')<EOL>if filename:<EOL><INDENT>fnl = filename.lower()<EOL>if fnl.endswith(("<STR_LIT>", "<STR_LIT>")):<EOL><INDENT>filename = filename[:-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if module == "<STR_LIT:__main__>":<EOL><INDENT>try:<EOL><INDENT>filename = sys.argv[<NUM_LIT:0>]<EOL><DEDENT>except AttributeError:<EOL><INDENT>filename = '<STR_LIT:__main__>'<EOL><DEDENT><DEDENT>if not filename:<EOL><INDENT>filename = module<EOL><DEDENT><DEDENT>registry = globals.setdefault("<STR_LIT>", {})<EOL>warn_explicit(message, category, filename, lineno, module, registry,<EOL>globals)<EOL>

Issue a warning, or maybe ignore it or raise an exception.


def __init__(self, record=False, module=None):

self._record = record<EOL>self._module = sys.modules['<STR_LIT>'] if module is None else module<EOL>self._entered = False<EOL>

Specify whether to record warnings and if an alternative module
        should be used other than sys.modules['warnings'].

        For compatibility with Python 3.0, please consider all arguments to be
        keyword-only.


import os<EOL>name = os.path.normcase(name)<EOL>pat = os.path.normcase(pat)<EOL>return fnmatchcase(name, pat)<EOL>

Test whether FILENAME matches PATTERN.

    Patterns are Unix shell style:

    *       matches everything
    ?       matches any single character
    [seq]   matches any character in seq
    [!seq]  matches any char not in seq

    An initial period in FILENAME is not special.
    Both FILENAME and PATTERN are first case-normalized
    if the operating system requires it.
    If you don't want this, use fnmatchcase(FILENAME, PATTERN).


import os,posixpath<EOL>result=[]<EOL>pat=os.path.normcase(pat)<EOL>try:<EOL><INDENT>re_pat = _cache[pat]<EOL><DEDENT>except KeyError:<EOL><INDENT>res = translate(pat)<EOL>if len(_cache) >= _MAXCACHE:<EOL><INDENT>_cache.clear()<EOL><DEDENT>_cache[pat] = re_pat = re.compile(res)<EOL><DEDENT>match = re_pat.match<EOL>if os.path is posixpath:<EOL><INDENT>for name in names:<EOL><INDENT>if match(name):<EOL><INDENT>result.append(name)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>for name in names:<EOL><INDENT>if match(os.path.normcase(name)):<EOL><INDENT>result.append(name)<EOL><DEDENT><DEDENT><DEDENT>return result<EOL>

Return the subset of the list NAMES that match PAT


try:<EOL><INDENT>re_pat = _cache[pat]<EOL><DEDENT>except KeyError:<EOL><INDENT>res = translate(pat)<EOL>if len(_cache) >= _MAXCACHE:<EOL><INDENT>_cache.clear()<EOL><DEDENT>_cache[pat] = re_pat = re.compile(res)<EOL><DEDENT>return re_pat.match(name) is not None<EOL>

Test whether FILENAME matches PATTERN, including case.

    This is a version of fnmatch() which doesn't case-normalize
    its arguments.


i, n = <NUM_LIT:0>, len(pat)<EOL>res = '<STR_LIT>'<EOL>while i < n:<EOL><INDENT>c = pat[i]<EOL>i = i+<NUM_LIT:1><EOL>if c == '<STR_LIT:*>':<EOL><INDENT>res = res + '<STR_LIT>'<EOL><DEDENT>elif c == '<STR_LIT:?>':<EOL><INDENT>res = res + '<STR_LIT:.>'<EOL><DEDENT>elif c == '<STR_LIT:[>':<EOL><INDENT>j = i<EOL>if j < n and pat[j] == '<STR_LIT:!>':<EOL><INDENT>j = j+<NUM_LIT:1><EOL><DEDENT>if j < n and pat[j] == '<STR_LIT:]>':<EOL><INDENT>j = j+<NUM_LIT:1><EOL><DEDENT>while j < n and pat[j] != '<STR_LIT:]>':<EOL><INDENT>j = j+<NUM_LIT:1><EOL><DEDENT>if j >= n:<EOL><INDENT>res = res + '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>stuff = pat[i:j].replace('<STR_LIT:\\>','<STR_LIT>')<EOL>i = j+<NUM_LIT:1><EOL>if stuff[<NUM_LIT:0>] == '<STR_LIT:!>':<EOL><INDENT>stuff = '<STR_LIT>' + stuff[<NUM_LIT:1>:]<EOL><DEDENT>elif stuff[<NUM_LIT:0>] == '<STR_LIT>':<EOL><INDENT>stuff = '<STR_LIT:\\>' + stuff<EOL><DEDENT>res = '<STR_LIT>' % (res, stuff)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>res = res + re.escape(c)<EOL><DEDENT><DEDENT>return res + '<STR_LIT>'<EOL>

Translate a shell PATTERN to a regular expression.

    There is no way to quote meta-characters.


global CONNECTION<EOL>CONNECTION = redis.Redis(*args, **kwargs)<EOL>

Update the global connection settings for models that don't have
model-specific connections.


return CONNECTION<EOL>

Override me for one of the ways to change the way I connect to Redis.


from .columns import MODELS<EOL>if isinstance(obj, MODELS['<STR_LIT>']):<EOL><INDENT>obj = obj.__class__<EOL><DEDENT>if hasattr(obj, '<STR_LIT>'):<EOL><INDENT>return obj._conn<EOL><DEDENT>if hasattr(obj, '<STR_LIT>'):<EOL><INDENT>return obj.CONN<EOL><DEDENT>return get_connection()<EOL>

Tries to get the _conn attribute from a model. Barring that, gets the
global default connection using other methods.


if isinstance(val, float):<EOL><INDENT>val = repr(val)<EOL><DEDENT>elif val in (None, '<STR_LIT>'):<EOL><INDENT>return None<EOL><DEDENT>elif not isinstance(val, six.string_types):<EOL><INDENT>if six.PY3 and isinstance(val, bytes):<EOL><INDENT>val = val.decode('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>val = str(val)<EOL><DEDENT><DEDENT>r = sorted(set([x for x in [s.lower().strip(string.punctuation) for s in val.split()] if x]))<EOL>if not isinstance(val, str):  <EOL><INDENT>return [s.encode('<STR_LIT:utf-8>') for s in r]<EOL><DEDENT>return r<EOL>

This is a basic full-text index keygen function. Words are lowercased, split
by whitespace, and stripped of punctuation from both ends before an inverted
index is created for term searching.


if not val:<EOL><INDENT>return None<EOL><DEDENT>if not isinstance(val, six.string_types):<EOL><INDENT>if six.PY3 and isinstance(val, bytes):<EOL><INDENT>val = val.decode('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>val = str(val)<EOL><DEDENT><DEDENT>return {'<STR_LIT>': _prefix_score(val)}<EOL>

This is a basic case-sensitive "sorted order" index keygen function for
strings. This will return a value that is suitable to be used for ordering
by a 7-byte prefix of a string (that is 7 characters from a byte-string, and
1.75-7 characters from a unicode string, depending on character -> encoding
length).

.. warning:: Case sensitivity is based on the (encoded) byte prefixes of the
    strings/text being indexed, so ordering *may be different* than a native
    comparison ordering (especially if an order is different based on
    characters past the 7th encoded byte).


return SIMPLE(val.lower())<EOL>

The same as SIMPLE, only case-insensitive.


return SIMPLE_CI(val)<EOL>


if not val:<EOL><INDENT>return None<EOL><DEDENT>if not isinstance(val, six.string_types_ex):<EOL><INDENT>val = str(val)<EOL><DEDENT>return [val]<EOL>

This is a basic "equality" index keygen, primarily meant to be used for
things like::

    Model.query.filter(col='value')

Where ``FULL_TEXT`` would transform a sentence like "A Simple Sentence" into
an inverted index searchable by the words "a", "simple", and/or "sentence",
``IDENTITY`` will only be searchable by the orginal full sentence with the
same capitalization - "A Simple Sentence". See ``IDENTITY_CI`` for the
same function, only case-insensitive.


return IDENTITY(val.lower())<EOL>


global NULL_SESSION<EOL>NULL_SESSION = True<EOL>

If you call ``use_null_session()``, you will change the default session for
all threads to *not cache*. You can override the default on a per-thread
basis by manipulating ``session.null_session`` (set to ``True``, ``False``,
or delete the attribute to not cache, cache, or use the global default,
respectively).


global NULL_SESSION<EOL>NULL_SESSION = False<EOL>

If you call ``use_rom_session()``, you will change the default session for
all threads to *cache*. You can override the default on a per-thread basis
by manipulating ``session.null_session`` (set to ``True``, ``False``, or
delete the attribute to not cache, cache, or use the global default,
respectively).


conn = _connect(model)<EOL>max_id = int(conn.get('<STR_LIT>'%(model._namespace, model._pkey)) or '<STR_LIT:0>')<EOL>block_size = max(block_size, <NUM_LIT:10>)<EOL>for i in range(<NUM_LIT:1>, max_id+<NUM_LIT:1>, block_size):<EOL><INDENT>models = model.get(list(range(i, i+block_size)))<EOL>models <EOL>session.commit(all=True)<EOL>yield min(i+block_size, max_id), max_id<EOL><DEDENT>

This utility function will iterate over all entities of a provided model,
refreshing their indices. This is primarily useful after adding an index
on a column.

Arguments:

    * *model* - the model whose entities you want to reindex
    * *block_size* - the maximum number of entities you want to fetch from
      Redis at a time, defaulting to 100

This function will yield its progression through re-indexing all of your
entities.

Example use::

    for progress, total in refresh_indices(MyModel, block_size=200):
        print "%s of %s"%(progress, total)

.. note:: This uses the session object to handle index refresh via calls to
  ``.commit()``. If you have any outstanding entities known in the
  session, they will be committed.


def clean_old_index(model, block_size=<NUM_LIT:100>, **kwargs):

conn = _connect(model)<EOL>version = list(map(int, conn.info()['<STR_LIT>'].split('<STR_LIT:.>')[:<NUM_LIT:2>]))<EOL>has_hscan = version >= [<NUM_LIT:2>, <NUM_LIT:8>]<EOL>pipe = conn.pipeline(True)<EOL>prefix = '<STR_LIT>'%model._namespace<EOL>index = prefix + '<STR_LIT::>'<EOL>block_size = max(block_size, <NUM_LIT:10>)<EOL>force_hscan = kwargs.get('<STR_LIT>', False)<EOL>if (has_hscan or force_hscan) and force_hscan is not None:<EOL><INDENT>max_id = conn.hlen(index)<EOL>cursor = None<EOL>scanned = <NUM_LIT:0><EOL>while cursor != b'<STR_LIT:0>':<EOL><INDENT>cursor, remove = _scan_index_lua(conn, [index, prefix], [cursor or '<STR_LIT:0>', block_size, <NUM_LIT:0>, <NUM_LIT:0>])<EOL>if remove:<EOL><INDENT>_clean_index_lua(conn, [model._namespace], remove)<EOL><DEDENT>scanned += block_size<EOL>if scanned > max_id:<EOL><INDENT>max_id = scanned + <NUM_LIT:1><EOL><DEDENT>yield scanned, max_id<EOL><DEDENT>for uniq in chain(model._unique, model._cunique):<EOL><INDENT>name = uniq if isinstance(uniq, six.string_types) else '<STR_LIT::>'.join(uniq)<EOL>idx = prefix + name + '<STR_LIT>'<EOL>cursor = None<EOL>while cursor != b'<STR_LIT:0>':<EOL><INDENT>cursor, remove = _scan_index_lua(conn, [idx, prefix], [cursor or '<STR_LIT:0>', block_size, <NUM_LIT:1>, <NUM_LIT:0>])<EOL>if remove:<EOL><INDENT>conn.hdel(idx, *remove)<EOL><DEDENT>scanned += block_size<EOL>if scanned > max_id:<EOL><INDENT>max_id = scanned + <NUM_LIT:1><EOL><DEDENT>yield scanned, max_id<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if model._unique or model._cunique:<EOL><INDENT>if has_hscan:<EOL><INDENT>warnings.warn("<STR_LIT>", stacklevel=<NUM_LIT:2>)<EOL><DEDENT>else:<EOL><INDENT>warnings.warn("<STR_LIT>", stacklevel=<NUM_LIT:2>)<EOL><DEDENT><DEDENT>max_id = int(conn.get('<STR_LIT>'%(prefix, model._pkey)) or '<STR_LIT:0>')<EOL>for i in range(<NUM_LIT:1>, max_id+<NUM_LIT:1>, block_size):<EOL><INDENT>ids = list(range(i, min(i+block_size, max_id+<NUM_LIT:1>)))<EOL>for id in ids:<EOL><INDENT>pipe.exists(prefix + str(id))<EOL>pipe.hexists(index, id)<EOL><DEDENT>result = iter(pipe.execute())<EOL>remove = [id for id, ent, ind in zip(ids, result, result) if ind and not ent]<EOL>if remove:<EOL><INDENT>_clean_index_lua(conn, [model._namespace], remove)<EOL><DEDENT>yield min(i+block_size, max_id-<NUM_LIT:1>), max_id<EOL><DEDENT><DEDENT>yield max_id, max_id<EOL>

This utility function will clean out old index data that was accidentally
left during item deletion in rom versions <= 0.27.0 . You should run this
after you have upgraded all of your clients to version 0.28.0 or later.

Arguments:

    * *model* - the model whose entities you want to reindex
    * *block_size* - the maximum number of items to check at a time
      defaulting to 100

This function will yield its progression through re-checking all of the
data that could be left over.

Example use::

    for progress, total in clean_old_index(MyModel, block_size=200):
        print "%s of %s"%(progress, total)


start = time.time()<EOL>last_print = <NUM_LIT:0><EOL>last_line = <NUM_LIT:0><EOL>for prog, total in chain(job, [(<NUM_LIT:1>, <NUM_LIT:1>)]):<EOL><INDENT>if (time.time() - last_print) > <NUM_LIT> or prog >= total:<EOL><INDENT>delta = (time.time() - start) or <NUM_LIT><EOL>line = "<STR_LIT>"%(<EOL><NUM_LIT> * prog / (total or <NUM_LIT:1>), delta, total * delta / (prog or <NUM_LIT:1>) - delta)<EOL>length = len(line)<EOL>line += max(last_line - length, <NUM_LIT:0>) * '<STR_LIT:U+0020>'<EOL>print(line, end="<STR_LIT:\r>")<EOL>last_line = length<EOL>last_print = time.time()<EOL><DEDENT><DEDENT>print()<EOL>

This utility function will print the progress of a passed iterator job as
started by ``refresh_indices()`` and ``clean_old_index()``.

Usage example::

    class RomTest(Model):
        pass

    for i in xrange(1000):
        RomTest().save()

    util.show_progress(util.clean_old_index(RomTest))


script = script.encode('<STR_LIT:utf-8>') if isinstance(script, six.text_type) else script<EOL>sha = [None, sha1(script).hexdigest()]<EOL>def call(conn, keys=[], args=[], force_eval=False):<EOL><INDENT>keys = tuple(keys)<EOL>args = tuple(args)<EOL>if not force_eval:<EOL><INDENT>if not sha[<NUM_LIT:0>]:<EOL><INDENT>try:<EOL><INDENT>return conn.execute_command(<EOL>'<STR_LIT>', script, len(keys), *(keys + args))<EOL><DEDENT>finally:<EOL><INDENT>del sha[:-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>try:<EOL><INDENT>return conn.execute_command(<EOL>"<STR_LIT>", sha[<NUM_LIT:0>], len(keys), *(keys+args))<EOL><DEDENT>except redis.exceptions.ResponseError as msg:<EOL><INDENT>if not any(msg.args[<NUM_LIT:0>].startswith(nsm) for nsm in NO_SCRIPT_MESSAGES):<EOL><INDENT>raise<EOL><DEDENT><DEDENT><DEDENT>return conn.execute_command(<EOL>"<STR_LIT>", script, len(keys), *(keys+args))<EOL><DEDENT>return call<EOL>

Borrowed/modified from my book, Redis in Action:
https://github.com/josiahcarlson/redis-in-action/blob/master/python/ch11_listing_source.py

Used for Lua scripting support when writing against Redis 2.6+ to allow
for multiple unique columns per model.


def EntityLock(entity, acquire_timeout, lock_timeout):

return Lock(entity._connection, entity._pk, acquire_timeout, lock_timeout)<EOL>

Useful when you want exclusive access to an entity across all writers.::

    # example
    import rom

    class Document(rom.Model):
        owner = rom.ManyToOne('User', on_delete='restrict')
        ...

    def change_owner(document, new_owner):
        with rom.util.EntityLock(document, 5, 90):
            document.owner = new_owner
            document.save()


if self.null_session:<EOL><INDENT>return<EOL><DEDENT>self._init()<EOL>pk = obj._pk<EOL>if not pk.endswith('<STR_LIT>'):<EOL><INDENT>self.known[pk] = obj<EOL>self.wknown[pk] = obj<EOL><DEDENT>


self._init()<EOL>self.known.pop(obj._pk, None)<EOL>self.wknown.pop(obj._pk, None)<EOL>

Forgets about an entity (automatically called when an entity is
deleted). Call this to ensure that an entity that you've modified is
not automatically saved on ``session.commit()`` .


self._init()<EOL>return self.known.get(pk) or self.wknown.get(pk)<EOL>

Fetches an entity from the session based on primary key.


self.wknown = weakref.WeakValueDictionary()<EOL>self.known = {}<EOL>

Forget about all entities in the session (``.commit()`` will do
nothing).


def flush(self, full=False, all=False, force=False):

self._init()<EOL>return self.save(*self.known.values(), full=full, all=all, force=force)<EOL>

Call ``.save()`` on all modified entities in the session. Use when you
want to flush changes to Redis, but don't want to lose your local
session cache.

See the ``.commit()`` method for arguments and their meanings.


def commit(self, full=False, all=False, force=False):

changes = self.flush(full, all, force)<EOL>self.known = {}<EOL>return changes<EOL>

Call ``.save()`` on all modified entities in the session. Also forgets
all known entities in the session, so this should only be called at
the end of a request.

Arguments:

    * *full* - pass ``True`` to force save full entities, not only
      changes
    * *all* - pass ``True`` to save all entities known, not only those
      entities that have been modified.
    * *full* - pass ``True`` to force-save all entities known, ignoring
      DataRaceError and EntityDeletedError exceptions


from rom import Model<EOL>full = kwargs.get('<STR_LIT>')<EOL>all = kwargs.get('<STR_LIT:all>')<EOL>force = kwargs.get('<STR_LIT>')<EOL>changes = <NUM_LIT:0><EOL>items = deque()<EOL>items.extend(objects)<EOL>while items:<EOL><INDENT>o = items.popleft()<EOL>if isinstance(o, (list, tuple)):<EOL><INDENT>items.extendleft(reversed(o))<EOL><DEDENT>elif isinstance(o, Model):<EOL><INDENT>if not o._deleted and (all or o._modified):<EOL><INDENT>changes += o.save(full, force)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ORMError(<EOL>"<STR_LIT>"%(<EOL>o,))<EOL><DEDENT><DEDENT>return changes<EOL>

This method is an alternate API for saving many entities (possibly not
tracked by the session). You can call::

    session.save(obj)
    session.save(obj1, obj2, ...)
    session.save([obj1, obj2, ...])

And the entities will be flushed to Redis.

You can pass the keyword arguments ``full``, ``all``, and ``force`` with
the same meaning and semantics as the ``.commit()`` method.


force = kwargs.get('<STR_LIT>')<EOL>from .model import Model, SKIP_ON_DELETE<EOL>flat = []<EOL>items = deque()<EOL>items.extend(objects)<EOL>types = set()<EOL>while items:<EOL><INDENT>o = items.popleft()<EOL>if isinstance(o, (list, tuple)):<EOL><INDENT>items.extendleft(reversed(o))<EOL><DEDENT>elif isinstance(o, Model):<EOL><INDENT>if force or not o._deleted:<EOL><INDENT>flat.append(o)<EOL>types.add(type(o))<EOL><DEDENT><DEDENT><DEDENT>from .columns import MODELS_REFERENCED<EOL>for t in types:<EOL><INDENT>if not t._no_fk or t._namespace in MODELS_REFERENCED:<EOL><INDENT>raise ORMError("<STR_LIT>")<EOL><DEDENT><DEDENT>c2p = {}<EOL>for o in flat:<EOL><INDENT>if not o._deleted:<EOL><INDENT>o._before_delete()<EOL><DEDENT>c = o._connection<EOL>if c not in c2p:<EOL><INDENT>c2p[c] = c.pipeline()<EOL><DEDENT>o.delete(_conn=c2p[c],<EOL>skip_on_delete_i_really_mean_it=SKIP_ON_DELETE)<EOL><DEDENT>for p in c2p.values():<EOL><INDENT>p.execute()<EOL><DEDENT>forget = self.forget<EOL>for o in flat:<EOL><INDENT>if o._deleted == <NUM_LIT:1>:<EOL><INDENT>o._after_delete()<EOL>o._deleted = <NUM_LIT:2><EOL><DEDENT>forget(o)<EOL><DEDENT>

This method offers the ability to delete multiple entities in a single
round trip to Redis (assuming your models are all stored on the same
server). You can call::

    session.delete(obj)
    session.delete(obj1, obj2, ...)
    session.delete([obj1, obj2, ...])

The keyword argument ``force=True`` can be provided, which can force
the deletion of an entitiy again, even if we believe it to already be
deleted.

If ``force=True``, we won't re-call the object's ``_before_delete()``
method, but we will re-call ``_after_delete()``.

.. note:: Objects are automatically dropped from the session after
    delete for the sake of cache coherency.


self._init()<EOL>from rom import Model<EOL>force = kwargs.get('<STR_LIT>')<EOL>for o in objects:<EOL><INDENT>if isinstance(o, (list, tuple)):<EOL><INDENT>self.refresh(*o, force=force)<EOL><DEDENT>elif isinstance(o, Model):<EOL><INDENT>if not o._new:<EOL><INDENT>o.refresh(force=force)<EOL><DEDENT>else:<EOL><INDENT>self.add(o)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ORMError(<EOL>"<STR_LIT>"%(<EOL>o,))<EOL><DEDENT><DEDENT>

This method is an alternate API for refreshing many entities (possibly
not tracked by the session). You can call::

    session.refresh(obj)
    session.refresh(obj1, obj2, ...)
    session.refresh([obj1, obj2, ...])

And all provided entities will be reloaded from Redis.

To force reloading for modified entities, you can pass ``force=True``.


def refresh_all(self, *objects, **kwargs):

self.refresh(*self.known.values(), force=kwargs.get('<STR_LIT>'))<EOL>

This method is an alternate API for refreshing all entities tracked
by the session. You can call::

    session.refresh_all()
    session.refresh_all(force=True)

And all entities known by the session will be reloaded from Redis.

To force reloading for modified entities, you can pass ``force=True``.


def redis_writer_lua(conn, pkey, namespace, id, unique, udelete, delete,<EOL>data, keys, scored, prefix, suffix, geo, old_data, is_delete):

ldata = []<EOL>for pair in data.items():<EOL><INDENT>ldata.extend(pair)<EOL><DEDENT>for item in prefix:<EOL><INDENT>item.append(_prefix_score(item[-<NUM_LIT:1>]))<EOL><DEDENT>for item in suffix:<EOL><INDENT>item.append(_prefix_score(item[-<NUM_LIT:1>]))<EOL><DEDENT>data = [json.dumps(x, default=_fix_bytes) for x in<EOL>(unique, udelete, delete, ldata, keys, scored, prefix, suffix, geo, is_delete, old_data)]<EOL>result = _redis_writer_lua(conn, [], [namespace, id] + data)<EOL>if isinstance(conn, _Pipeline):<EOL><INDENT>return<EOL><DEDENT>if six.PY3:<EOL><INDENT>result = result.decode()<EOL><DEDENT>result = json.loads(result)<EOL>if '<STR_LIT>' in result:<EOL><INDENT>result = result['<STR_LIT>']<EOL>raise UniqueKeyViolation(<EOL>"<STR_LIT>"%(<EOL>unique[result], namespace, result, id),<EOL>namespace, id)<EOL><DEDENT>if '<STR_LIT>' in result:<EOL><INDENT>result = result['<STR_LIT>']<EOL>if pkey in result:<EOL><INDENT>raise EntityDeletedError(<EOL>"<STR_LIT>"%(<EOL>namespace, id),<EOL>namespace, id)<EOL><DEDENT>raise DataRaceError(<EOL>"<STR_LIT>"%(<EOL>namespace, id, result),<EOL>namespace, id)<EOL><DEDENT>

... Actually write data to Redis. This is an internal detail. Please don't
call me directly.


Called before a new entity is saved to Redis


Called before an entity is saved to Redis


Called after a new entity has been saved to Redis


Called after a previously-saved entity has been saved to Redis


Called after a previously-deleted entity has been deleted from Redis


return dict(self._data)<EOL>

Returns a copy of all data assigned to columns in this entity. Useful
for returning items to JSON-enabled APIs. If you want to copy an
entity, you should look at the ``.copy()`` method.


def save(self, full=False, force=False):

<EOL>was_new = self._new<EOL>if was_new:<EOL><INDENT>self._before_insert()<EOL><DEDENT>else:<EOL><INDENT>self._before_update()<EOL><DEDENT>new = self.to_dict()<EOL>ret, data = self._apply_changes(<EOL>self._last, new, full or self._new or force, is_new=self._new or force)<EOL>self._last = data<EOL>self._new = False<EOL>self._modified = False<EOL>self._deleted = False<EOL>if was_new:<EOL><INDENT>self._after_insert()<EOL><DEDENT>else:<EOL><INDENT>self._after_update()<EOL><DEDENT>return ret<EOL>

Saves the current entity to Redis. Will only save changed data by
default, but you can force a full save by passing ``full=True``.

If the underlying entity was deleted and you want to re-save the entity,
you can pass ``force=True`` to force a full re-save of the entity.


if kwargs.get('<STR_LIT>') is not SKIP_ON_DELETE:<EOL><INDENT>self._before_delete()<EOL>_on_delete(self)<EOL><DEDENT>session.forget(self)<EOL>self._apply_changes(self._last, {}, delete=True, _conn=kwargs.get('<STR_LIT>'))<EOL>self._modified = True<EOL>self._deleted = True<EOL>if kwargs.get('<STR_LIT>') is not SKIP_ON_DELETE:<EOL><INDENT>self._after_delete()<EOL><DEDENT>

Deletes the entity immediately. Also performs any on_delete operations
specified as part of column definitions.


x = self.to_dict()<EOL>x.pop(self._pkey)<EOL>return self.__class__(**x)<EOL>

Creates a shallow copy of the given entity (any entities that can be
retrieved from a OneToMany relationship will not be copied).


conn = _connect(cls)<EOL>single = not isinstance(ids, (list, tuple, set, frozenset))<EOL>if single:<EOL><INDENT>ids = [ids]<EOL><DEDENT>pks = ['<STR_LIT>'%(cls._namespace, id) for id in map(int, ids)]<EOL>out = list(map(session.get, pks))<EOL>if None in out:<EOL><INDENT>pipe = conn.pipeline(True)<EOL>idxs = []<EOL>for i, data in enumerate(out):<EOL><INDENT>if data is None:<EOL><INDENT>idxs.append(i)<EOL>pipe.hgetall(pks[i])<EOL><DEDENT><DEDENT>for i, data in zip(idxs, pipe.execute()):<EOL><INDENT>if data:<EOL><INDENT>if six.PY3:<EOL><INDENT>data = dict((k.decode(), v.decode()) for k, v in data.items())<EOL><DEDENT>out[i] = cls(_loading=True, **data)<EOL><DEDENT><DEDENT>out = [x for x in out if x]<EOL><DEDENT>if single:<EOL><INDENT>return out[<NUM_LIT:0>] if out else None<EOL><DEDENT>return out<EOL>

Will fetch one or more entities of this type from the session or
Redis.

Used like::

    MyModel.get(5)
    MyModel.get([1, 6, 2, 4])

Passing a list or a tuple will return multiple entities, in the same
order that the ids were passed.


conn = _connect(cls)<EOL>model = cls._namespace<EOL>_limit = kwargs.pop('<STR_LIT>', ())<EOL>if _limit and len(_limit) != <NUM_LIT:2>:<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>elif _limit and not all(isinstance(x, six.integer_types) for x in _limit):<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>if len(kwargs) != <NUM_LIT:1>:<EOL><INDENT>raise QueryError("<STR_LIT>"%(len(kwargs),))<EOL><DEDENT>_numeric = bool(kwargs.pop('<STR_LIT>', None))<EOL>for attr, value in kwargs.items():<EOL><INDENT>plain_attr = attr.partition('<STR_LIT::>')[<NUM_LIT:0>]<EOL>if isinstance(value, tuple) and len(value) != <NUM_LIT:2>:<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>if attr in cls._unique and (plain_attr not in cls._index or not _numeric):<EOL><INDENT>if isinstance(value, tuple):<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>single = not isinstance(value, list)<EOL>if single:<EOL><INDENT>value = [value]<EOL><DEDENT>qvalues = list(map(cls._columns[attr]._to_redis, value))<EOL>ids = [x for x in conn.hmget('<STR_LIT>'%(model, attr), qvalues) if x]<EOL>if not ids:<EOL><INDENT>return None if single else []<EOL><DEDENT>return cls.get(ids[<NUM_LIT:0>] if single else ids)<EOL><DEDENT>if plain_attr not in cls._index:<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>if isinstance(value, NUMERIC_TYPES) and not isinstance(value, bool):<EOL><INDENT>value = (value, value)<EOL><DEDENT>if isinstance(value, tuple):<EOL><INDENT>args = list(value)<EOL>for i, a in enumerate(args):<EOL><INDENT>args[i] = ('<STR_LIT>', '<STR_LIT>')[i] if a is None else cls._columns[attr]._to_redis(a)<EOL><DEDENT>if _limit:<EOL><INDENT>args.extend(_limit)<EOL><DEDENT>ids = conn.zrangebyscore('<STR_LIT>'%(model, attr), *args)<EOL>if not ids:<EOL><INDENT>return []<EOL><DEDENT>return cls.get(ids)<EOL><DEDENT>query = cls.query.filter(**{attr: value})<EOL>if _limit:<EOL><INDENT>query = query.limit(*_limit)<EOL><DEDENT>return query.all()<EOL><DEDENT>

This method offers a simple query method for fetching entities of this
type via attribute numeric ranges (such columns must be ``indexed``),
or via ``unique`` columns.

Some examples::

    user = User.get_by(email_address='user@domain.com')
    # gets up to 25 users created in the last 24 hours
    users = User.get_by(
        created_at=(time.time()-86400, time.time()),
        _limit=(0, 25))

Optional keyword-only arguments:

    * *_limit* - A 2-tuple of (offset, count) that can be used to
      paginate or otherwise limit results returned by a numeric range
      query
    * *_numeric* - An optional boolean defaulting to False that forces
      the use of a numeric index for ``.get_by(col=val)`` queries even
      when ``col`` has an existing unique index

If you would like to make queries against multiple columns or with
multiple criteria, look into the Model.query class property.

.. note:: rom will attempt to use a unique index first, then a numeric
    index if there was no unique index. You can explicitly tell rom to
    only use the numeric index by using ``.get_by(..., _numeric=True)``.
.. note:: Ranged queries with `get_by(col=(start, end))` will only work
    with columns that use a numeric index.


return Query(cls)<EOL>

Returns a ``Query`` object that refers to this model to handle
subsequent filtering.


sa = setattr<EOL>for a, v in zip(self._columns, args):<EOL><INDENT>sa(self, a, v)<EOL><DEDENT>for a, v in kwargs.items():<EOL><INDENT>sa(self, a, v)<EOL><DEDENT>return self<EOL>

Updates multiple attributes in a model. If ``args`` are provided, this
method will assign attributes in the order returned by
``list(self._columns)`` until one or both are exhausted.

If ``kwargs`` are provided, this method will assign attributes to the
names provided, after ``args`` have been processed.


def redis_prefix_lua(conn, dest, index, prefix, is_first, pattern=None):

tkey = '<STR_LIT>'%(index.partition('<STR_LIT::>')[<NUM_LIT:0>], uuid.uuid4())<EOL>start, end = _start_end(prefix)<EOL>return _redis_prefix_lua(conn,<EOL>[dest, tkey, index],<EOL>[start, end, pattern or prefix, int(pattern is not None), int(bool(is_first))]<EOL>)<EOL>

Performs the actual prefix, suffix, and pattern match operations.


def estimate_work_lua(conn, index, prefix):

if index.endswith('<STR_LIT>'):<EOL><INDENT>args = [] if not prefix else list(prefix)<EOL>if args:<EOL><INDENT>args[<NUM_LIT:0>] = '<STR_LIT>' if args[<NUM_LIT:0>] is None else repr(float(args[<NUM_LIT:0>]))<EOL>args[<NUM_LIT:1>] = '<STR_LIT>' if args[<NUM_LIT:1>] is None else repr(float(args[<NUM_LIT:1>]))<EOL><DEDENT>return _estimate_work_lua(conn, [index], args, force_eval=True)<EOL><DEDENT>elif index.endswith('<STR_LIT>'):<EOL><INDENT>return _estimate_work_lua(conn, [index], filter(None, [prefix]), force_eval=True)<EOL><DEDENT>start, end = _start_end(prefix)<EOL>return _estimate_work_lua(conn, [index], [start, '<STR_LIT:(>' + end], force_eval=True)<EOL>

Estimates the total work necessary to calculate the prefix match over the
given index with the provided prefix.


def search(self, conn, filters, order_by, offset=None, count=None, timeout=None):

<EOL>pipe, intersect, temp_id = self._prepare(conn, filters)<EOL>if order_by:<EOL><INDENT>reverse = order_by and order_by.startswith('<STR_LIT:->')<EOL>order_clause = '<STR_LIT>'%(self.namespace, order_by.lstrip('<STR_LIT:->'))<EOL>intersect(temp_id, {temp_id:<NUM_LIT:0>, order_clause: -<NUM_LIT:1> if reverse else <NUM_LIT:1>})<EOL><DEDENT>if timeout is not None:<EOL><INDENT>pipe.expire(temp_id, timeout)<EOL>pipe.execute()<EOL>return temp_id<EOL><DEDENT>offset = offset if offset is not None else <NUM_LIT:0><EOL>end = (offset + count - <NUM_LIT:1>) if count and count > <NUM_LIT:0> else -<NUM_LIT:1><EOL>pipe.zrange(temp_id, offset, end)<EOL>pipe.delete(temp_id)<EOL>return pipe.execute()[-<NUM_LIT:2>]<EOL>

Search for model ids that match the provided filters.

Arguments:

    * *filters* - A list of filters that apply to the search of one of
      the following two forms:

        1. ``'column:string'`` - a plain string will match a word in a
           text search on the column

        .. note:: Read the documentation about the ``Query`` object
          for what is actually passed during text search

        2. ``('column', min, max)`` - a numeric column range search,
           between min and max (inclusive by default)

        .. note:: Read the documentation about the ``Query`` object
          for information about open-ended ranges

        3. ``['column:string1', 'column:string2']`` - will match any
           of the provided words in a text search on the column

        4. ``Prefix('column', 'prefix')`` - will match prefixes of
           words in a text search on the column

        5. ``Suffix('column', 'suffix')`` - will match suffixes of
           words in a text search on the column

        6. ``Pattern('column', 'pattern')`` - will match patterns over
           words in a text search on the column

    * *order_by* - A string that names the numeric column by which to
      sort the results by. Prefixing with '-' will return results in
      descending order

    .. note:: While you can technically pass a non-numeric index as an
      *order_by* clause, the results will basically be to order the
      results by string comparison of the ids (10 will come before 2).

    .. note:: If you omit the ``order_by`` argument, results will be
      ordered by the last filter. If the last filter was a text
      filter, see the previous note. If the last filter was numeric,
      then results will be ordered by that result.

    * *offset* - A numeric starting offset for results
    * *count* - The maximum number of results to return from the query


pipe, intersect, temp_id = self._prepare(conn, filters)<EOL>pipe.zcard(temp_id)<EOL>pipe.delete(temp_id)<EOL>return pipe.execute()[-<NUM_LIT:2>]<EOL>

Returns the count of the items that match the provided filters.

For the meaning of what the ``filters`` argument means, see the
``.search()`` method docs.


def select(self, *column_names, **kwargs):

include_pk = kwargs.pop('<STR_LIT>', False)<EOL>decode = kwargs.pop('<STR_LIT>', True)<EOL>ff = kwargs.pop('<STR_LIT>', _dict_data_factory)<EOL>if isinstance(column_names[<NUM_LIT:0>], (list, tuple)):<EOL><INDENT>column_names = column_names[<NUM_LIT:0>]<EOL><DEDENT>if not column_names:<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>if len(set(column_names)) != len(column_names):<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>missing = [c for c in column_names if c not in self._model._columns]<EOL>if missing:<EOL><INDENT>raise QueryError("<STR_LIT>"%(missing,))<EOL><DEDENT>remove_last = False<EOL>if self._model._pkey not in column_names:<EOL><INDENT>column_names += (self._model._pkey,)<EOL>remove_last = not include_pk<EOL><DEDENT>return self.replace(select=(column_names, decode, remove_last, ff))<EOL>

Select the provided column names from the model, do not return an entity,
do not involve the rom session, just get the raw and/or processed column
data from Redis.

Keyword-only arguments:

    * *include_pk=False* - whether to include the primary key in the
        returned data (we need to get this in some cases, so we fetch
        it anyway; if you want it, we can return it to you - just be
        careful with the namedtuple option - see the warning below)
    * *decode=True* - whether to take a pass through normal data
        decoding in the model (will not return an entity/model)
    * *ff=_dict_data_factory* - the type of data to return from the
        select after all filters/limits/order_by are applied

.. warning:: If ``include_pk = True`` and if you don't provide
  the primary key column, it will be appended to your list of columns.

.. note:: if you want to provide a new factory function for the returned
  data, it must be of the form (below is the actual dict factory
  function)

::

    def _dict_data_factory(columns):
        _dict = dict
        _zip = zip
        def make(data):
            # do whatever you need to turn your tuple of columns plus
            # your list of data into whatever you want:
            return _dict(_zip(columns, data))
        return make

Available factory functions:

    * *``rom.query._dict_data_factory``* - default
    * *``rom.query._list_data_factory``* - lowest overhead, as the
      ``data`` passed in above is a list that you can do anything to
    * *``rom.query._tuple_data_factory``* - when you want tuples instead
    * *``rom.query._namedtuple_data_factory``* - get namedtuples, see
      see warning below

.. warning:: If you use the ``_namedtuple_data_factory``, and your
  columns include underscore prefixes, they will be stripped. If this
  results in a name collision, you *will* get an exception. If you want
  differerent behavior, write your own 20 line factory function that
  does exactly what you want, and pass it; they are really easy!


data = {<EOL>'<STR_LIT>': self._model,<EOL>'<STR_LIT>': self._filters,<EOL>'<STR_LIT>': self._order_by,<EOL>'<STR_LIT>': self._limit,<EOL>'<STR_LIT>': self._select,<EOL>}<EOL>data.update(**kwargs)<EOL>return Query(**data)<EOL>

Copy the Query object, optionally replacing the filters, order_by, or
limit information on the copy. This is mostly an internal detail that
you can ignore.


cur_filters = list(self._filters)<EOL>for attr, value in kwargs.items():<EOL><INDENT>value = self._check(attr, value, which='<STR_LIT>')<EOL>if isinstance(value, NUMERIC_TYPES):<EOL><INDENT>value = (value, value)<EOL><DEDENT>if isinstance(value, six.string_types):<EOL><INDENT>cur_filters.append('<STR_LIT>'%(attr, value))<EOL><DEDENT>elif six.PY3 and isinstance(value, bytes):<EOL><INDENT>cur_filters.append('<STR_LIT>'%(attr, value.decode('<STR_LIT>')))<EOL><DEDENT>elif isinstance(value, tuple):<EOL><INDENT>if value is NOT_NULL:<EOL><INDENT>from .columns import OneToOne, ManyToOne<EOL>ctype = type(self._model._columns[attr])<EOL>if not issubclass(ctype, (OneToOne, ManyToOne)):<EOL><INDENT>raise QueryError("<STR_LIT>""<STR_LIT>"%(attr, ctype))<EOL><DEDENT><DEDENT>if len(value) != <NUM_LIT:2>:<EOL><INDENT>raise QueryError("<STR_LIT>"%(len(value), value))<EOL><DEDENT>tt = []<EOL>for v in value:<EOL><INDENT>if isinstance(v, date):<EOL><INDENT>v = dt2ts(v)<EOL><DEDENT>if isinstance(v, dtime):<EOL><INDENT>v = t2ts(v)<EOL><DEDENT>tt.append(v)<EOL><DEDENT>value = tt<EOL>cur_filters.append((attr, value[<NUM_LIT:0>], value[<NUM_LIT:1>]))<EOL><DEDENT>elif isinstance(value, list) and value:<EOL><INDENT>cur_filters.append(['<STR_LIT>'%(attr, _ts(v)) for v in value])<EOL><DEDENT>else:<EOL><INDENT>raise QueryError("<STR_LIT>"%(attr, value))<EOL><DEDENT><DEDENT>return self.replace(filters=tuple(cur_filters))<EOL>

Only columns/attributes that have been specified as having an index with
the ``index=True`` option on the column definition can be filtered with
this method. Prefix, suffix, and pattern match filters must be provided
using the ``.startswith()``, ``.endswith()``, and the ``.like()``
methods on the query object, respectively. Geo location queries should
be performed using the ``.near()`` method.

Filters should be of the form::

    # for numeric ranges, use None for open-ended ranges
    attribute=(min, max)

    # you can also query for equality by passing a single number
    attribute=value

    # for string searches, passing a plain string will require that
    # string to be in the index as a literal
    attribute=string

    # to perform an 'or' query on strings, you can pass a list of
    # strings
    attribute=[string1, string2]

As an example, the following will return entities that have both
``hello`` and ``world`` in the ``String`` column ``scol`` and has a
``Numeric`` column ``ncol`` with value between 2 and 10 (including the
endpoints)::

    results = MyModel.query \\
        .filter(scol='hello') \\
        .filter(scol='world') \\
        .filter(ncol=(2, 10)) \\
        .all()

If you only want to match a single value as part of your range query,
you can pass an integer, float, or Decimal object by itself, similar
to the ``Model.get_by()`` method::

    results = MyModel.query \\
        .filter(ncol=5) \\
        .execute()

.. note:: Trying to use a range query `attribute=(min, max)` on indexed
    string columns won't return any results.
.. note:: This method only filters columns that have been defined with
    ``index=True``.


new = []<EOL>for k, v in kwargs.items():<EOL><INDENT>v = self._check(k, v, '<STR_LIT>')<EOL>new.append(Prefix(k, v))<EOL><DEDENT>return self.replace(filters=self._filters+tuple(new))<EOL>

When provided with keyword arguments of the form ``col=prefix``, this
will limit the entities returned to those that have a word with the
provided prefix in the specified column(s). This requires that the
``prefix=True`` option was provided during column definition.

Usage::

    User.query.startswith(email='user@').execute()


new = []<EOL>for k, v in kwargs.items():<EOL><INDENT>v = self._check(k, v, '<STR_LIT>')<EOL>new.append(Suffix(k, v[::-<NUM_LIT:1>]))<EOL><DEDENT>return self.replace(filters=self._filters+tuple(new))<EOL>

When provided with keyword arguments of the form ``col=suffix``, this
will limit the entities returned to those that have a word with the
provided suffix in the specified column(s). This requires that the
``suffix=True`` option was provided during column definition.

Usage::

    User.query.endswith(email='@gmail.com').execute()


new = []<EOL>for k, v in kwargs.items():<EOL><INDENT>v = self._check(k, v, '<STR_LIT>')<EOL>new.append(Pattern(k, v))<EOL><DEDENT>return self.replace(filters=self._filters+tuple(new))<EOL>

When provided with keyword arguments of the form ``col=pattern``, this
will limit the entities returned to those that include the provided
pattern. Note that 'like' queries require that the ``prefix=True``
option must have been provided as part of the column definition.

Patterns allow for 4 wildcard characters, whose semantics are as
follows:

    * *?* - will match 0 or 1 of any character
    * *\** - will match 0 or more of any character
    * *+* - will match 1 or more of any character
    * *!* - will match exactly 1 of any character

As an example, imagine that you have enabled the required prefix
matching on your ``User.email`` column. And lets say that you want to
find everyone with an email address that contains the name 'frank'
before the ``@`` sign. You can use either of the following patterns
to discover those users.

    * *\*frank\*@*
    * *\*frank\*@*

.. note:: Like queries implicitly start at the beginning of strings
  checked, so if you want to match a pattern that doesn't start at
  the beginning of a string, you should prefix it with one of the
  wildcard characters (like ``*`` as we did with the 'frank' pattern).


cname = column.lstrip('<STR_LIT:->')<EOL>col = self._check(cname)<EOL>if type(col).__name__ in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>') and col._keygen.__name__ not in _STRING_SORT_KEYGENS:<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"%(cname, STRING_SORT_KEYGENS_STR),<EOL>stacklevel=<NUM_LIT:2>)<EOL><DEDENT>return self.replace(order_by=column)<EOL>

When provided with a column name, will sort the results of your query::

    # returns all users, ordered by the created_at column in
    # descending order
    User.query.order_by('-created_at').execute()


return self.replace(limit=(offset, count))<EOL>

Will limit the number of results returned from a query::

    # returns the most recent 25 users
    User.query.order_by('-created_at').limit(0, 25).execute()


filters = self._filters<EOL>if self._order_by:<EOL><INDENT>filters += (self._order_by.lstrip('<STR_LIT:->'),)<EOL><DEDENT>if not filters:<EOL><INDENT>size = _connect(self._model).hlen(self._model._namespace + '<STR_LIT>')<EOL>limit = self._limit or (<NUM_LIT:0>, <NUM_LIT:2>**<NUM_LIT:64>)<EOL>size = max(size - max(limit[<NUM_LIT:0>], <NUM_LIT:0>), <NUM_LIT:0>)<EOL>return min(size, limit[<NUM_LIT:1>])<EOL><DEDENT>return self._model._gindex.count(_connect(self._model), filters)<EOL>

Will return the total count of the objects that match the specified
filters.::

    # counts the number of users created in the last 24 hours
    User.query.filter(created_at=(time.time()-86400, time.time())).count()


def iter_result(self, timeout=<NUM_LIT:30>, pagesize=<NUM_LIT:100>, no_hscan=False):

if not self._filters and not self._order_by:<EOL><INDENT>if self._model._columns[self._model._pkey]._index:<EOL><INDENT>return self._iter_all_pkey()<EOL><DEDENT>conn = _connect(self._model)<EOL>version = list(map(int, conn.info()['<STR_LIT>'].split('<STR_LIT:.>')[:<NUM_LIT:2>]))<EOL>if version >= [<NUM_LIT:2>,<NUM_LIT:8>] and not no_hscan:<EOL><INDENT>return self._iter_all_hscan()<EOL><DEDENT>return self._iter_all()<EOL><DEDENT>return self._iter_results(timeout, pagesize)<EOL>

Iterate over the results of your query instead of getting them all with
`.all()`. Will only perform a single query. If you expect that your
processing will take more than 30 seconds to process 100 items, you
should pass `timeout` and `pagesize` to reflect an appropriate timeout
and page size to fetch at once.

Usage::

    for user in User.query.endswith(email='@gmail.com').iter_result():
        # do something with user
        ...


if not (self._filters or self._order_by):<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>timeout = int(timeout)<EOL>if timeout < <NUM_LIT:1>:<EOL><INDENT>raise QueryError("<STR_LIT>"%timeout)<EOL><DEDENT>return self._model._gindex.search(<EOL>_connect(self._model), self._filters, self._order_by, timeout=timeout)<EOL>

This will execute the query, returning the key where a ZSET of your
results will be stored for pagination, further operations, etc.

The timeout must be a positive integer number of seconds for which to
set the expiration time on the key (this is to ensure that any cached
query results are eventually deleted, unless you make the explicit
step to use the PERSIST command).

.. note:: Limit clauses are ignored and not passed.

Usage::

    ukey = User.query.endswith(email='@gmail.com').cached_result(30)
    for i in xrange(0, conn.zcard(ukey), 100):
        # refresh the expiration
        conn.expire(ukey, 30)
        users = User.get(conn.zrange(ukey, i, i+99))
        ...


return list(self)<EOL>

Actually executes the query, returning any entities that match the
filters, ordered by the specified ordering (if any), limited by any
earlier limit calls.


return self.execute()<EOL>


lim = [<NUM_LIT:0>, <NUM_LIT:1>]<EOL>if self._limit:<EOL><INDENT>lim[<NUM_LIT:0>] = self._limit[<NUM_LIT:0>]<EOL><DEDENT>if not self._filters and not self._order_by:<EOL><INDENT>for ent in self:<EOL><INDENT>return ent<EOL><DEDENT>return None<EOL><DEDENT>ids = self.limit(*lim)._search()<EOL>if ids:<EOL><INDENT>return self._model.get(ids[<NUM_LIT:0>])<EOL><DEDENT>return None<EOL>

Returns only the first result from the query, if any.


from .columns import MODELS_REFERENCED<EOL>if not self._model._no_fk or self._model._namespace in MODELS_REFERENCED:<EOL><INDENT>raise QueryError("<STR_LIT>")<EOL><DEDENT>de = []<EOL>i = <NUM_LIT:0><EOL>for result in self.iter_result(pagesize=blocksize):<EOL><INDENT>de.append(result)<EOL>i += <NUM_LIT:1><EOL>if i >= blocksize:<EOL><INDENT>session.delete(de) <EOL>del de[:]<EOL>i = <NUM_LIT:0><EOL><DEDENT><DEDENT>if de:<EOL><INDENT>session.delete(de)<EOL><DEDENT>

Will delete the entities that match at the time the query is executed.

Used like::

    MyModel.query.filter(email=...).delete()
    MyModel.query.endswith(email='@host.com').delete()

.. warning:: can't be used on models on either side of a ``OneToMany``,
    ``ManyToOne``, or ``OneToOne`` relationship.


seen_d = set([ent._pk])<EOL>to_delete = [ent]<EOL>seen_s = set()<EOL>to_save = []<EOL>def _set_default(ent, attr, de=NULL):<EOL><INDENT>pk = ent._pk<EOL>if pk in seen_d:<EOL><INDENT>return<EOL><DEDENT>col = ent.__class__._columns[attr]<EOL>de = de if de is not NULL else col._default<EOL>if de in (None, NULL):<EOL><INDENT>setattr(ent, attr, None)<EOL><DEDENT>elif callable(col._default):<EOL><INDENT>setattr(ent, attr, col._default())<EOL><DEDENT>else:<EOL><INDENT>setattr(ent, attr, col._default)<EOL><DEDENT>if pk not in seen_s:<EOL><INDENT>seen_s.add(pk)<EOL>to_save.append(ent)<EOL><DEDENT><DEDENT>for self in to_delete:<EOL><INDENT>for tbl, attr, action in MODELS_REFERENCED.get(self._namespace, ()):<EOL><INDENT>if action == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>refs = MODELS[tbl].get_by(**{attr: self.id})<EOL>if not refs:<EOL><INDENT>continue<EOL><DEDENT>if action == '<STR_LIT>':<EOL><INDENT>raise _restrict(self, attr, refs)<EOL><DEDENT>elif action == '<STR_LIT>':<EOL><INDENT>for ref in refs:<EOL><INDENT>_set_default(ref, attr, None)<EOL><DEDENT>continue<EOL><DEDENT>elif action == '<STR_LIT>':<EOL><INDENT>for ref in refs:<EOL><INDENT>_set_default(ref, attr)<EOL><DEDENT>continue<EOL><DEDENT>for ent in (refs if isinstance(refs, list) else [refs]):<EOL><INDENT>if ent._pk not in seen_d:<EOL><INDENT>seen_d.add(ent._pk)<EOL>to_delete.append(ent)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>for self in to_delete:<EOL><INDENT>self.delete(skip_on_delete_i_really_mean_it=SKIP_ON_DELETE)<EOL><DEDENT>for self in to_save:<EOL><INDENT>if self._pk not in seen_d:<EOL><INDENT>self.save()<EOL><DEDENT><DEDENT>

This function handles all on_delete semantics defined on OneToMany columns.

This function only exists because 'cascade' is *very* hard to get right.


def __init__(self, column=None, keygen=None, prefix=False, suffix=False, keygen2=None):

if column and keygen:<EOL><INDENT>keygen2 = lambda _,dct: keygen(dct.get(column))<EOL><DEDENT>elif keygen2:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>Column.__init__(self, index=True, keygen2=keygen2, prefix=prefix, suffix=suffix)<EOL>

Two ways to use:

    * With ``column`` AND ``keygen`` - automatically index an external column
    a second way using standard keygens
    * With ``keygen2`` - generate an arbitrary index on arbitrary model data

Standard Arguments:

    * *column* - column with the data you want to index
    * *keygen* - passed through, see ``Column`` docs for more information
    * *prefix* - passed through, see ``Column`` docs for more information
    * *suffix* - passed through, see ``Column`` docs for more information
    * *keygen2* - passed through, see ``Column`` docs for more information

.. note:: `unique` is not available or passed through, as it relies on
    data in the column (IndexOnly columns only store data in index(es)).


if filename == '<STR_LIT>':<EOL><INDENT>return None<EOL><DEDENT>name, ext = os.path.splitext(filename)<EOL>ext = ext[<NUM_LIT:1>:].upper()<EOL>if ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_BMP<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_GIF<EOL><DEDENT>elif ext == '<STR_LIT>' or ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_JPEG<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_PCX<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_PICT<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_PNG<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_PNM<EOL><DEDENT>elif ext == '<STR_LIT>' or ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_TIF<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_XBM<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_XPM<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeErro('<STR_LIT>')    <EOL><DEDENT>

Get the type of an image from the file's extension ( .jpg, etc. )


<EOL>if filename is None or filename == '<STR_LIT>':<EOL><INDENT>return None<EOL><DEDENT>name, ext = os.path.splitext(filename)<EOL>ext = ext[<NUM_LIT:1>:].upper()<EOL>if ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_BMP<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_GIF<EOL><DEDENT>elif ext == '<STR_LIT>' or ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_JPEG<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_PCX<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_PNG<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_PNM<EOL><DEDENT>elif ext == '<STR_LIT>' or ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_TIF<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_XBM<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>return wx.BITMAP_TYPE_XPM<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>


data = self.build_data_set()<EOL>if btn and btn.name:<EOL><INDENT>data[btn.name] = btn.name<EOL><DEDENT>evt = FormSubmitEvent(self, data)<EOL>self.container.ProcessEvent(evt)<EOL>


data = {}<EOL>for field in self.fields:<EOL><INDENT>if field.name:<EOL><INDENT>val = field.get_value()<EOL>if val is None:<EOL><INDENT>continue<EOL><DEDENT>elif isinstance(val, unicode):<EOL><INDENT>val = val.encode("<STR_LIT:utf-8>")                    <EOL><DEDENT>data[field.name] = val<EOL><DEDENT><DEDENT>return data<EOL>

Construct a sequence of name/value pairs from controls


object._attributes = {}<EOL>object._name = tag.GetName().lower()<EOL>for name in self.attributes:<EOL><INDENT>object._attributes["<STR_LIT>" % name] = tag.GetParam(name)<EOL>if object._attributes["<STR_LIT>" % name] == "<STR_LIT>":<EOL><INDENT>object._attributes["<STR_LIT>" % name] = None<EOL><DEDENT><DEDENT>


def metaclass(name, bases, dict):<EOL><INDENT>klass = type(name, bases, dict)<EOL>form.FormTagHandler.register_type(type_name.upper(), klass)<EOL>return klass<EOL><DEDENT>return metaclass<EOL>

A metaclass generator. Returns a metaclass which
    will register it's class as the class that handles input type=typeName


def GetParam(tag, param, default=__SENTINEL):

if tag.HasParam(param):<EOL><INDENT>return tag.GetParam(param)<EOL><DEDENT>else:<EOL><INDENT>if default == __SENTINEL:<EOL><INDENT>raise KeyError<EOL><DEDENT>else:<EOL><INDENT>return default<EOL><DEDENT><DEDENT>

Convenience function for accessing tag parameters


def set_parent(self, new_parent, init=False):

Component.set_parent(self, new_parent, init)<EOL>if not init:<EOL><INDENT>wx_obj = new_parent and new_parent.wx_obj<EOL>self.wx_obj.Reparent(wx_obj)<EOL><DEDENT>

Re-parent a child control with the new wx_obj parent


<EOL>for name in (name, "<STR_LIT:label>", "<STR_LIT:value>", "<STR_LIT:text>", "<STR_LIT:title>", "<STR_LIT:filename>", <EOL>"<STR_LIT:name>"):<EOL><INDENT>prop = self.pg.GetPropertyByName(name)<EOL>if prop is not None:<EOL><INDENT>break<EOL><DEDENT><DEDENT>self.Parent.SetFocus()<EOL>self.Parent.Raise()<EOL>self.pg.SetFocus()<EOL>wx.CallLater(<NUM_LIT>, self.select, prop.GetName())<EOL>

Programatically select a (default) property to start editing it


def select(self, name, flags=<NUM_LIT:0>):

<EOL>wxPG_SEL_FOCUS=<NUM_LIT>  <EOL>wxPG_SEL_FORCE=<NUM_LIT>  <EOL>flags |= wxPG_SEL_FOCUS <EOL>prop = self.pg.GetPropertyByName(name)<EOL>self.pg.SelectProperty(prop, flags)<EOL>if DEBUG: print("<STR_LIT>", prop)<EOL>

Select a property (and start the editor)


ret = {}<EOL>for k, v in bg.items():<EOL><INDENT>if k == '<STR_LIT:type>':<EOL><INDENT>v = WIN_MAP[v]._meta.name<EOL><DEDENT>elif k == '<STR_LIT>':<EOL><INDENT>menus = v['<STR_LIT>']<EOL>v = [migrate_control(menu) for menu in menus]<EOL><DEDENT>elif k == '<STR_LIT>':<EOL><INDENT>v = [migrate_control(comp) for comp in v]<EOL><DEDENT>else:<EOL><INDENT>k = SPEC_MAP['<STR_LIT>'].get(k, k)<EOL><DEDENT>ret[k] = v<EOL><DEDENT>return ret<EOL>

Take a pythoncard background resource and convert to a gui2py window