Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrote pretty filepointers #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions prettyprinter/pretty_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,26 @@ def pretty_nodes(value, ctx):
@register_pretty('pathlib.PurePath')
def pretty_pathlib(value, ctx):
return pretty_call_alt(ctx, type(value), args=(value.as_posix(),))


@register_pretty('_io.TextIOWrapper')
def pretty_fp(value, ctx):
cls = 'FP'
fields = [('mode', 'r'), ('encoding', None),
('buffer', None), ('closed', None)]
mode_definition = {
'r': 'open for reading(default)',
'w': 'open for writing, truncating the file first',
'x': 'open for exclusive creation, failing if the file already exists',
'a': 'open for writing, appending to the end of the file if it exists',
'b': 'binary mode',
't': 'text mode(default)',
'+': 'open a disk file for updating(reading and writing)',
'U': 'universal newlines mode(deprecated) ',
}
kwargs = dict([(k, getattr(value, k, default)) for k, default in fields])
kwargs['path'] = kwargs['buffer'].name
kwargs['modes'] = '\n'.join([mode_definition[k] for k in kwargs['mode']])
del kwargs['buffer']
del kwargs['mode']
return pretty_call_alt(ctx, cls, kwargs=kwargs)
14 changes: 13 additions & 1 deletion tests/test_stdlib_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)
from enum import Enum
from functools import partial
from pathlib import PosixPath, PurePosixPath, PureWindowsPath, WindowsPath
from pathlib import PosixPath, PurePosixPath, PureWindowsPath, WindowsPath, Path
from types import MappingProxyType
from uuid import UUID

Expand Down Expand Up @@ -121,3 +121,15 @@ def test_purepath(typ, name, args, pathstr):
pathlib.{}(
{}
)""".format(name, pathstr)


def test_fp():
temp_path = Path('./_____test')
value = open(temp_path, 'w')
assert pformat(
value) == "FP"\
"(\n encoding='UTF-8',"\
"\n closed=False,"""\
"\n path='_____test',"\
"\n modes='open for writing, truncating the file first'\n)"
temp_path.unlink()