Skip to content

pane.io

pane.io

FileOrPath = t.Union[str, Path, TextIOBase, t.TextIO] module-attribute

from_json(f, ty, *, custom=None)

Load an object of type ty from a JSON file f

Parameters:

Name Type Description Default
f FileOrPath

File-like or path-like to load from

required
custom Optional[IntoConverterHandlers]

Custom converters to use

None
Source code in pane/io.py
def from_json(f: FileOrPath, ty: t.Type[T], *,
              custom: t.Optional[IntoConverterHandlers] = None) -> T:
    """
    Load an object of type `ty` from a JSON file `f`

    Parameters:
        f: File-like or path-like to load from
        custom: Custom converters to use
    """
    import json
    with open_file(f) as f:
        obj = json.load(f)
    return from_data(obj, ty, custom=custom)

from_yaml(f, ty, *, custom=None)

Load an object of type ty from a YAML file f

Parameters:

Name Type Description Default
f FileOrPath

File-like or path-like to load from

required
custom Optional[IntoConverterHandlers]

Custom converters to use

None
Source code in pane/io.py
def from_yaml(f: FileOrPath, ty: t.Type[T], *,
              custom: t.Optional[IntoConverterHandlers] = None) -> T:
    """
    Load an object of type `ty` from a YAML file `f`

    Parameters:
        f: File-like or path-like to load from
        custom: Custom converters to use
    """
    import yaml
    try:
        from yaml import CSafeLoader as Loader
    except ImportError:
        from yaml import SafeLoader as Loader

    with open_file(f) as f:
        obj = t.cast(t.Any, yaml.load(f, Loader))  # type: ignore

    return from_data(obj, ty, custom=custom)

from_yaml_all(f, ty, *, custom=None)

Load an object of type ty from a YAML file f

Parameters:

Name Type Description Default
f FileOrPath

File-like or path-like to load from

required
custom Optional[IntoConverterHandlers]

Custom converters to use

None
Source code in pane/io.py
def from_yaml_all(f: FileOrPath, ty: t.Type[T], *,
                  custom: t.Optional[IntoConverterHandlers] = None) -> t.List[T]:
    """
    Load an object of type `ty` from a YAML file `f`

    Parameters:
        f: File-like or path-like to load from
        custom: Custom converters to use
    """
    import yaml
    try:
        from yaml import CSafeLoader as Loader
    except ImportError:
        from yaml import SafeLoader as Loader

    with open_file(f) as f:
        obj = t.cast(t.List[t.Any], list(yaml.load_all(f, Loader)))  # type: ignore

    return from_data(obj, t.List[ty], custom=custom)

write_json(obj, f, *, ty=None, indent=None, sort_keys=False, custom=None)

Write data to a JSON file f

Parameters:

Name Type Description Default
obj Convertible

Object to write

required
ty Optional[IntoConverter]

Type of object

None
f FileOrPath

File-like or path-like to write to

required
indent Union[str, int, None]

Indent to format JSON with. Defaults to None (no indentation)

None
sort_keys bool

Whether to sort keys prior to serialization.

False
custom Optional[IntoConverterHandlers]

Custom converters to use

None
Source code in pane/io.py
def write_json(obj: Convertible, f: FileOrPath, *,
               ty: t.Optional[IntoConverter] = None,
               indent: t.Union[str, int, None] = None,
               sort_keys: bool = False,
               custom: t.Optional[IntoConverterHandlers] = None):
    """
    Write data to a JSON file `f`

    Parameters:
      obj: Object to write
      ty: Type of object
      f: File-like or path-like to write to
      indent: Indent to format JSON with. Defaults to None (no indentation)
      sort_keys: Whether to sort keys prior to serialization.
      custom: Custom converters to use
    """
    import json

    with open_file(f, 'w') as f:
        json.dump(
            into_data(obj, ty, custom=custom),
            f, indent=indent, sort_keys=sort_keys
        )

write_yaml(obj, f, *, ty=None, indent=None, width=None, allow_unicode=True, explicit_start=True, explicit_end=False, default_style=None, default_flow_style=None, sort_keys=False, custom=None)

Write data to a YAML file f

Parameters:

Name Type Description Default
obj Convertible

Object to write

required
ty Optional[IntoConverter]

Type of object

None
f FileOrPath

File-like or path-like to write to

required
indent Optional[int]

Number of spaces to indent blocks with

None
width Optional[int]

Maximum width of file created

None
allow_unicode bool

Whether to output unicode characters or escape them

True
explicit_start bool

Whether to include a YAML document start "---"

True
explicit_end bool

Whether to include a YAML document end "..."

False
default_style Optional[Literal['"', '|', '>']]

Default style to use for scalar nodes. See YAML documentation for more information.

None
default_flow_style Optional[bool]

Whether to default to flow style or block style for collections. See YAML documentation for more information.

None
sort_keys bool

Whether to sort keys prior to serialization.

False
custom Optional[IntoConverterHandlers]

Custom converters to use

None
Source code in pane/io.py
def write_yaml(obj: Convertible, f: FileOrPath, *,
               ty: t.Optional[IntoConverter] = None,
               indent: t.Optional[int] = None,
               width: t.Optional[int] = None,
               allow_unicode: bool = True,
               explicit_start: bool = True, explicit_end: bool = False,
               default_style: t.Optional[t.Literal['"', '|', '>']] = None,
               default_flow_style: t.Optional[bool] = None,
               sort_keys: bool = False,
               custom: t.Optional[IntoConverterHandlers] = None):
    """
    Write data to a YAML file `f`

    Parameters:
      obj: Object to write
      ty: Type of object
      f: File-like or path-like to write to
      indent: Number of spaces to indent blocks with
      width: Maximum width of file created
      allow_unicode: Whether to output unicode characters or escape them
      explicit_start: Whether to include a YAML document start "---"
      explicit_end: Whether to include a YAML document end "..."
      default_style: Default style to use for scalar nodes.
          See YAML documentation for more information.
      default_flow_style: Whether to default to flow style or block style for collections.
          See YAML documentation for more information.
      sort_keys: Whether to sort keys prior to serialization.
      custom: Custom converters to use
    """
    import yaml
    try:
        from yaml import CSafeDumper as Dumper
    except ImportError:
        from yaml import SafeDumper as Dumper

    with open_file(f, 'w') as f:
        yaml.dump(  # type: ignore
            into_data(obj, ty, custom=custom), f, Dumper=Dumper,
            indent=indent, width=width, allow_unicode=allow_unicode,
            explicit_start=explicit_start, explicit_end=explicit_end,
            default_style=default_style, default_flow_style=default_flow_style,
            sort_keys=sort_keys
        )

open_file(f, mode='r', newline=None, encoding='utf-8')

Open the given file for text I/O.

If given a path-like, opens it with the specified settings. Otherwise, make an effort to reconfigure the encoding, and check that it is readable/writable as specified.

Parameters:

Name Type Description Default
f FileOrPath

File to open/reconfigure

required
mode Literal['r', 'w']

Mode file should be opened in

'r'
newline Optional[str]

Newline mode file should be opened in

None
encoding Optional[str]

Encoding file should be opened in

'utf-8'
Source code in pane/io.py
def open_file(f: FileOrPath,
              mode: t.Literal['r', 'w'] = 'r',
              newline: t.Optional[str] = None,
              encoding: t.Optional[str] = 'utf-8') -> AbstractContextManager[TextIOBase]:
    """
    Open the given file for text I/O.

    If given a path-like, opens it with the specified settings.
    Otherwise, make an effort to reconfigure the encoding, and
    check that it is readable/writable as specified.

    Parameters:
      f: File to open/reconfigure
      mode: Mode file should be opened in
      newline: Newline mode file should be opened in
      encoding: Encoding file should be opened in
    """
    if not isinstance(f, (IOBase, t.BinaryIO, t.TextIO)):
        return open(f, mode, newline=newline, encoding=encoding)

    if isinstance(f, TextIOWrapper):
        f.reconfigure(newline=newline, encoding=encoding)
    elif isinstance(f, t.TextIO):
        f = TextIOWrapper(f.buffer, newline=newline, encoding=encoding)
    elif isinstance(f, (BufferedIOBase, t.BinaryIO)):
        f = TextIOWrapper(t.cast(t.BinaryIO, f), newline=newline, encoding=encoding)

    _validate_file(t.cast(TextIOBase, f), mode)
    return nullcontext(t.cast(TextIOBase, f))  # don't close a f we didn't open