Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/numpy/compat/py3k.py
7763 views
"""1Python 3.X compatibility tools.23While this file was originally intended for Python 2 -> 3 transition,4it is now used to create a compatibility layer between different5minor versions of Python 3.67While the active version of numpy may not support a given version of python, we8allow downstream libraries to continue to use these shims for forward9compatibility with numpy while they transition their code to newer versions of10Python.11"""12__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',13'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested',14'asstr', 'open_latin1', 'long', 'basestring', 'sixu',15'integer_types', 'is_pathlib_path', 'npy_load_module', 'Path',16'pickle', 'contextlib_nullcontext', 'os_fspath', 'os_PathLike']1718import sys19import os20from pathlib import Path21import io22try:23import pickle5 as pickle24except ImportError:25import pickle2627long = int28integer_types = (int,)29basestring = str30unicode = str31bytes = bytes3233def asunicode(s):34if isinstance(s, bytes):35return s.decode('latin1')36return str(s)3738def asbytes(s):39if isinstance(s, bytes):40return s41return str(s).encode('latin1')4243def asstr(s):44if isinstance(s, bytes):45return s.decode('latin1')46return str(s)4748def isfileobj(f):49return isinstance(f, (io.FileIO, io.BufferedReader, io.BufferedWriter))5051def open_latin1(filename, mode='r'):52return open(filename, mode=mode, encoding='iso-8859-1')5354def sixu(s):55return s5657strchar = 'U'5859def getexception():60return sys.exc_info()[1]6162def asbytes_nested(x):63if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):64return [asbytes_nested(y) for y in x]65else:66return asbytes(x)6768def asunicode_nested(x):69if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)):70return [asunicode_nested(y) for y in x]71else:72return asunicode(x)7374def is_pathlib_path(obj):75"""76Check whether obj is a `pathlib.Path` object.7778Prefer using ``isinstance(obj, os.PathLike)`` instead of this function.79"""80return isinstance(obj, Path)8182# from Python 3.783class contextlib_nullcontext:84"""Context manager that does no additional processing.8586Used as a stand-in for a normal context manager, when a particular87block of code is only sometimes used with a normal context manager:8889cm = optional_cm if condition else nullcontext()90with cm:91# Perform operation, using optional_cm if condition is True9293.. note::94Prefer using `contextlib.nullcontext` instead of this context manager.95"""9697def __init__(self, enter_result=None):98self.enter_result = enter_result99100def __enter__(self):101return self.enter_result102103def __exit__(self, *excinfo):104pass105106107def npy_load_module(name, fn, info=None):108"""109Load a module. Uses ``load_module`` which will be deprecated in python1103.12. An alternative that uses ``exec_module`` is in111numpy.distutils.misc_util.exec_mod_from_location112113.. versionadded:: 1.11.2114115Parameters116----------117name : str118Full module name.119fn : str120Path to module file.121info : tuple, optional122Only here for backward compatibility with Python 2.*.123124Returns125-------126mod : module127128"""129# Explicitly lazy import this to avoid paying the cost130# of importing importlib at startup131from importlib.machinery import SourceFileLoader132return SourceFileLoader(name, fn).load_module()133134135os_fspath = os.fspath136os_PathLike = os.PathLike137138139