コード例 #1
0
    def runyamlfile(self, fn):
        """Read a YAML test case file and process each test case it defines.
        """

        print("Process YAML file: %s" % os.path.basename(fn))

        with _open(fn, encoding="utf-8") as fp:
            testcases = yaml.load(fp)
            for testcase in testcases:
                self.runtestcase(testcase)
コード例 #2
0
def open(file, mode="r"):
    try:
        if os.path.exists(file):
            encoding = get_file_encoding(file)
        else:
            encoding = "utf-8"
        fd = _open(file, mode, encoding=encoding)
        yield fd
    finally:
        fd.close()
コード例 #3
0
ファイル: utils.py プロジェクト: shuiziliu/django-garage
def open_file(path, mode=None, encoding=None, **kwargs):
    """
    Helper function to open a file for reading/writing.

    :param path: path of file to read.
    :param mode: "b" for bytes or "t" for text (default is "t")
    :param encoding: file encoding for text (default is `utf-8`).
    :returns: stream object for reading/writing
    """
    try:
        from io import open as _open
    except ImportError:
        def _open(path, mode=None, encoding=None, **kwargs):
            return codecs.open(path, mode, encoding=encoding, **kwargs)
    return _open(path, mode=mode, encoding=encoding, **kwargs)
コード例 #4
0
ファイル: memfd.py プロジェクト: tiran/pymemfd
def open(name, *, flags=MFD_CLOEXEC, mode='wb+', buffering=0, closefd=True,
         **kwargs):
    """Open a new memfd

    By default the new memfd is CLOEXEC, binary, open for unbuffered reading
    and writing.
    """
    fd = memfd_create(name, flags)
    try:
        f = _open(fd, mode=mode, buffering=buffering, closefd=True, **kwargs)
    except Exception:
        if not closefd:
            _close(fd)
        raise
    f.name = 'memfd:{}'.format(name)
    return f
コード例 #5
0
def get_file_encoding(file, default_encoding="utf-8"):
    try:
        import chardet
    except:
        return default_encoding

    try:
        with _open(file, "rb") as fd:
            cur_encoding = chardet.detect(fd.read())["encoding"]
            if cur_encoding == "GB2312":
                cur_encoding = "GBK"
    except:
        print("Problem detecting encoding of " + file + ".  Defaulting to " +
              default_encoding)
        cur_encoding = default_encoding

    return cur_encoding
コード例 #6
0
def open_file(path, mode=None, encoding=None, **kwargs):
    """
    Helper function to open a file for reading/writing.

    :param path: path of file to read.
    :param mode: "b" for bytes or "t" for text (default is "t")
    :param encoding: file encoding for text (default is `utf-8`).
    :returns: stream object for reading/writing
    """
    try:
        from io import open as _open
    except ImportError:

        def _open(path, mode=None, encoding=None, **kwargs):
            return codecs.open(path, mode, encoding=encoding, **kwargs)

    return _open(path, mode=mode, encoding=encoding, **kwargs)
コード例 #7
0
ファイル: molecule_data.py プロジェクト: JameMaxwell/DiAtomic
    def edit_coupling_parameters(cls, ypar, couplings):

        cpl_data = cls._get_couplings_data()
        cpar = Channel.tot_npts

        for cp in couplings:
            new_item = []
            if cp.model == cls.models[1] or cp.model == cls.models[2]:
                units = cp.yunits
                for item in cpl_data[cp.label]:
                    sitem = item.split()
                    if len(sitem) >= 3:
                        new_item.append(
                            f'{float(sitem[0]):10.12f}'
                            f'{float(ypar[cpar]/units):24.14f}'
                            f'{int(sitem[2]):7d}'
                            f'{float(sitem[3]):14.2e}'
                            f'{float(sitem[4]):14.1e}'.lstrip()
                        )
                    else:
                        new_item.append(
                            f'{float(sitem[0]):10.12f}'
                            f'{float(ypar[cpar]/units):24.14f}'
                            f'{int(sitem[2]):7d}'
                        )
                    cpar += 1
            else:
                for item in cpl_data[cp.label]:
                    sitem = item.split()
                    if len(sitem) >= 2:
                        new_item.append(
                            f'{float(ypar[cpar]):18.12f}'
                            f'{int(sitem[1]):3d}'
                            f'{float(sitem[2]):10.1}'.lstrip()
                        )
                    else:
                        new_item.append(
                            f'{float(ypar[cpar]):18.12f}'
                            f'{int(sitem[1]):3d}'.lstrip()
                        )
                    cpar += 1
            cpl_data[cp.label] = new_item

        with _open(cls.cpl_file, 'w', encoding='utf8') as stream:
            yaml.dump(cpl_data, stream=stream)
コード例 #8
0
def open(name,
         *,
         flags=MFD_CLOEXEC,
         mode='wb+',
         buffering=0,
         closefd=True,
         **kwargs):
    """Open a new memfd

    By default the new memfd is CLOEXEC, binary, open for unbuffered reading
    and writing.
    """
    fd = memfd_create(name, flags)
    try:
        f = _open(fd, mode=mode, buffering=buffering, closefd=True, **kwargs)
    except Exception:
        if not closefd:
            _close(fd)
        raise
    f.name = 'memfd:{}'.format(name)
    return f
コード例 #9
0
ファイル: __init__.py プロジェクト: kocokolo/plyplus
def open(grammar_name):
    return _open(os.path.join(os.path.split(__file__)[0], grammar_name))
コード例 #10
0
ファイル: FileManager.py プロジェクト: raggnar/PyWeb
def open(path):
    if path is None: return
    return _open(path, "rb")
コード例 #11
0
def open(grammar_name):
    return _open( os.path.join(os.path.dirname(__file__), grammar_name) )
コード例 #12
0
ファイル: test_recorder.py プロジェクト: j053ph4/pywbem
 def loadYamlFile(self):
     """Load any created yaml file"""
     self.closeYamlFile()
     with _open(self.testyamlfile, encoding="utf-8") as fp:
         testyaml = yaml.load(fp)
     return testyaml
コード例 #13
0
def read(path):
    with _open(path, encoding='utf-8', mode='rt') as fp:
        return loads(fp.read())
コード例 #14
0
def open(name, mode='r'):
    return _open(name, mode, encoding='utf-8', newline=linesep)