Skip to content

Parser should also be able to take a buffer rather than just a local filesystem path. #49

@CompRhys

Description

@CompRhys
    def __init__(self, fn: str | Path | Iterable[str], cast_values: bool = False):
        """Create a CifFile object from a filename or buffer.

        On construction, the entire file is parsed into key-value pairs and data loops.
        Comment lines are ignored.

        Parameters
        ----------
        fn : str | Path | Iterable[str]
            File path or buffer containing CIF data
        cast_values : bool, optional
            Whether to convert string numerics to integers and float.
            Default value = ``False``
        """
        self._fn = fn
        self._pairs = {}
        self._loops = []

        self._cpat = {k: re.compile(pattern) for (k, pattern) in self.PATTERNS.items()}
        self._cast_values = cast_values

        if isinstance(fn, (str, Path)):
            with open(fn) as file:
                self._parse(peekable(file))
        else:
            # Assume it's a buffer/iterable
            self._parse(peekable(fn))

something like this would enable users to load files from places like s3 and then still be able to make sure of this parser without having to save a tmp file to disk.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions