• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python py3compat.open_with_encoding函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中yapf.yapflib.py3compat.open_with_encoding函数的典型用法代码示例。如果您正苦于以下问题:Python open_with_encoding函数的具体用法?Python open_with_encoding怎么用?Python open_with_encoding使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了open_with_encoding函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: IsPythonFile

def IsPythonFile(filename):
  """Return True if filename is a Python file."""
  if os.path.splitext(filename)[1] == '.py':
    return True

  try:
    with open(filename, 'rb') as fd:
      encoding = tokenize.detect_encoding(fd.readline)[0]

    # Check for correctness of encoding.
    with py3compat.open_with_encoding(filename, encoding=encoding) as fd:
      fd.read()
  except UnicodeDecodeError:
    encoding = 'latin-1'
  except (IOError, SyntaxError):
    # If we fail to detect encoding (or the encoding cookie is incorrect - which
    # will make detect_encoding raise SyntaxError), assume it's not a Python
    # file.
    return False

  try:
    with py3compat.open_with_encoding(filename,
                                      mode='r',
                                      encoding=encoding) as fd:
      first_line = fd.readlines()[0]
  except (IOError, IndexError):
    return False

  return re.match(r'^#!.*\bpython[23]?\b', first_line)
开发者ID:Erguotou,项目名称:pythonVSCode,代码行数:29,代码来源:file_resources.py


示例2: ReadFile

def ReadFile(filename, logger=None):
  """Read the contents of the file.

  An optional logger can be specified to emit messages to your favorite logging
  stream. If specified, then no exception is raised. This is external so that it
  can be used by third-party applications.

  Arguments:
    filename: (unicode) The name of the file.
    logger: (function) A function or lambda that takes a string and emits it.

  Returns:
    The contents of filename.

  Raises:
    IOError: raised if there was an error reading the file.
  """
  try:
    with open(filename, 'rb') as fd:
      encoding = tokenize.detect_encoding(fd.readline)[0]
  except IOError as err:
    if logger:
      logger(err)
    raise

  try:
    with py3compat.open_with_encoding(filename, mode='r',
                                      encoding=encoding) as fd:
      source = fd.read()
    return source, encoding
  except IOError as err:
    if logger:
      logger(err)
    raise
开发者ID:hayd,项目名称:yapf,代码行数:34,代码来源:yapf_api.py


示例3: ReadFile

def ReadFile(filename, logger=None):
  """Read the contents of the file.

  An optional logger can be specified to emit messages to your favorite logging
  stream. If specified, then no exception is raised. This is external so that it
  can be used by third-party applications.

  Arguments:
    filename: (unicode) The name of the file.
    logger: (function) A function or lambda that takes a string and emits it.

  Returns:
    The contents of filename.

  Raises:
    IOError: raised if there was an error reading the file.
  """
  try:
    encoding = file_resources.FileEncoding(filename)

    # Preserves line endings.
    with py3compat.open_with_encoding(
        filename, mode='r', encoding=encoding, newline='') as fd:
      lines = fd.readlines()

    line_ending = file_resources.LineEnding(lines)
    source = '\n'.join(line.rstrip('\r\n') for line in lines) + '\n'
    return source, line_ending, encoding
  except IOError as err:  # pragma: no cover
    if logger:
      logger(err)
    raise
开发者ID:boada,项目名称:yapf,代码行数:32,代码来源:yapf_api.py


示例4: testInPlaceReformattingEmpty

  def testInPlaceReformattingEmpty(self):
    unformatted_code = u''
    expected_formatted_code = u''

    with tempfile.NamedTemporaryFile(
        suffix='.py', dir=self.test_tmpdir) as testfile:
      testfile.write(unformatted_code.encode('UTF-8'))
      testfile.seek(0)

      p = subprocess.Popen(YAPF_BINARY + ['--in-place', testfile.name])
      p.wait()

      with py3compat.open_with_encoding(
          testfile.name, mode='r', encoding='utf-8') as fd:
        reformatted_code = fd.read()

    self.assertEqual(reformatted_code, expected_formatted_code)
开发者ID:b5y,项目名称:yapf,代码行数:17,代码来源:yapf_test.py


示例5: FormatFile

def FormatFile(filename,
               style_config=None,
               lines=None,
               print_diff=False,
               verify=True,
               in_place=False,
               logger=None):
  """Format a single Python file and return the formatted code.

  Arguments:
    filename: (unicode) The file to reformat.
    in_place: (bool) If True, write the reformatted code back to the file.
    logger: (io streamer) A stream to output logging.
    remaining arguments: see comment at the top of this module.

  Returns:
    Tuple of (reformatted_code, encoding, changed). reformatted_code is None if
    the file is sucessfully written to (having used in_place). reformatted_code
    is a diff if print_diff is True.

  Raises:
    IOError: raised if there was an error reading the file.
    ValueError: raised if in_place and print_diff are both specified.
  """
  _CheckPythonVersion()

  if in_place and print_diff:
    raise ValueError('Cannot pass both in_place and print_diff.')

  original_source, encoding = ReadFile(filename, logger)

  reformatted_source, changed = FormatCode(original_source,
                                           style_config=style_config,
                                           filename=filename,
                                           lines=lines,
                                           print_diff=print_diff,
                                           verify=verify)
  if in_place:
    with py3compat.open_with_encoding(filename,
                                      mode='w',
                                      encoding=encoding) as fd:
      fd.write(reformatted_source)
      return None, encoding, changed

  return reformatted_source, encoding, changed
开发者ID:hayd,项目名称:yapf,代码行数:45,代码来源:yapf_api.py


示例6: WriteReformattedCode

def WriteReformattedCode(filename, reformatted_code, in_place, encoding):
  """Emit the reformatted code.

  Write the reformatted code into the file, if in_place is True. Otherwise,
  write to stdout.

  Arguments:
    filename: (unicode) The name of the unformatted file.
    reformatted_code: (unicode) The reformatted code.
    in_place: (bool) If True, then write the reformatted code to the file.
    encoding: (unicode) The encoding of the file.
  """
  if in_place:
    with py3compat.open_with_encoding(filename, mode='w',
                                      encoding=encoding) as fd:
      fd.write(reformatted_code)
  else:
    py3compat.EncodeAndWriteToStdout(reformatted_code, encoding)
开发者ID:pkdevbox,项目名称:yapf,代码行数:18,代码来源:file_resources.py


示例7: test_with_latin_encoding

 def test_with_latin_encoding(self):
   file1 = os.path.join(self.test_tmpdir, 'testfile1')
   with py3compat.open_with_encoding(file1, mode='w', encoding='latin-1') as f:
     f.write(u'#! /bin/python2\n')
   self.assertTrue(file_resources.IsPythonFile(file1))
开发者ID:joachimmetz,项目名称:yapf,代码行数:5,代码来源:file_resources_test.py


示例8: _MakeTempFileWithContents

 def _MakeTempFileWithContents(self, filename, contents):
   path = os.path.join(self.test_tmpdir, filename)
   with py3compat.open_with_encoding(path, mode='w', encoding='utf-8') as f:
     f.write(py3compat.unicode(contents))
   return path
开发者ID:b5y,项目名称:yapf,代码行数:5,代码来源:yapf_test.py



注:本文中的yapf.yapflib.py3compat.open_with_encoding函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python yappi.clear_stats函数代码示例发布时间:2022-05-26
下一篇:
Python output.dbg函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap