hikari.dataframes.cif

Attributes

T

cif_core_validator

Classes

CifBlock

CifBlock object handles all data inside an individual block of Cif file.

CifFrame

A master object which manages cif files. It utilises other Cif* classes

CifValidator

This object is used to validate individual cif keys when parsing cif files.

CifIOBuffer

An abstract base class for Cif reader and writer buffers.

CifIO

A base class for CifRead and CifWrite. This class and its inheritors

CifReaderBuffer

Buffer for reading data from cif file into CifReader

CifReader

A helper class managing reading cif files into

CifWriterBuffer

Buffer for writing data from CifReader into cif file

CifWriter

A helper class managing writing CifFrame or CifBlock

Module Contents

hikari.dataframes.cif.T
class hikari.dataframes.cif.CifBlock(dict=None, /, **kwargs)[source]

Bases: collections.UserDict

CifBlock object handles all data inside an individual block of Cif file. As a subclass of an UserDict, in python3.7+ it is ordered by design. Individual Cif items can be accessed or assigned using a dict-like syntax.

get_as_type(key, typ, default=None)[source]

Get value of self[key] converted to typ. If value is a list, convert its contents element-wise.

Parameters:
  • key (str) – key associated with accessed element

  • typ (Callable[[Any], T]) – type/function applied to a value or its every element

  • default (Any) – if given, return it on KeyError

Returns:

value of self[key] or default converted to typ

Return type:

T

read(path, block)[source]

Read the contents of .cif file specified by the path parameter, but access and store only the block data block in self.

Parameters:
  • path (str) – Absolute or relative path to the .cif file.

  • block (str) – Name of the cif data block to be accessed

Return type:

None

write(path)[source]

Write the contents of CifBlock to the .cif file specified by the path parameter, using ‘hikari’ as block name.

Parameters:

path (str) – Absolute or relative path to the .cif file.

Return type:

None

class hikari.dataframes.cif.CifFrame(dict=None, /, **kwargs)[source]

Bases: collections.UserDict

A master object which manages cif files. It utilises other Cif* classes to manage multiple CifBlock`s with crystallographic information. As a subclass of an `UserDict, in python3.7+ it is ordered by design. Individual Cif blocks and items within them can be accessed or assigned using a single- or nested- dict-like syntax.

Similarly to other Frame`s, `CifFrame is designed to work in-place, meaning it should be first created, and only then accessed using methods such as read() or write(), but not chain assignments.

Unlike dict, CifBlock always initiates empty and does not accept any parameters at creation.

read(path)[source]

Read the contents of .cif file specified by the path parameter. Store each found block as a {block_name: CifBlock} pair.

Parameters:

path (str) – Absolute or relative path to the .cif file.

Return type:

None

write(path)[source]

Write the contents of CifFrame to the .cif file specified by the path parameter.

Parameters:

path (str) – Absolute or relative path to the .cif file.

Return type:

None

class hikari.dataframes.cif.CifValidator[source]

Bases: collections.UserDict

This object is used to validate individual cif keys when parsing cif files. It knows the metadata about each key based on its entry in the cif core dictionary v.2.4.5 packaged with the project. Since the specification itself is written in cif format, it is also read using the same CifReader (but without CifValidator).

Upon initialization, CifValidator becomes a dictionary whose keys are all valid cif keys, according to the cif specification used. Individual values are themselves dictionaries that store information about key’s contents, _category, _type, whether they are a _list etc.

contains all keys from core cif dictionary. In order to access individual values, use .get() instead of bracket notation.

__contains__(item)[source]
Return type:

bool

get(key, default=None)[source]

Get the dictionary containing information about input cif key.

Parameters:
  • key (str)

  • default (collections.UserDict)

Return type:

collections.UserDict

get__category(key, default=None)[source]

Close equivalent to self.get(key).get(‘_category’, default)

Parameters:
  • key (str)

  • default (str)

Return type:

str

get__list(key, default=None)[source]

Close equivalent to self.get(key).get(‘_list’, default) == ‘yes’

Parameters:
  • key (str)

  • default (bool)

Return type:

bool

class hikari.dataframes.cif.CifIOBuffer(target)[source]

Bases: abc.ABC

An abstract base class for Cif reader and writer buffers. Specifies that data can be added and flushed, names and values are stored in a list, output is stored in target (dict if reading, file if writing).

Parameters:

target (Any)

names = []
values = []
abstract add(data)[source]
abstract flush()[source]
class hikari.dataframes.cif.CifIO(cif_file_path, validate=True)[source]

Bases: abc.ABC

A base class for CifRead and CifWrite. This class and its inheritors base on the IUCr File Syntax version 1.1 Working specification available [here](https://www.iucr.org/resources/cif/spec/version1.1/cifsyntax)

COMMENT_REGEX
MATCHING_QUOTES_REGEX
MATCHING_OUTER_DELIMITERS_REGEX
MULTILINE_QUOTE_REGEX
WHITESPACE_SUBSTITUTES
file_contents = ''
file_path = ''
file_lines = []
validate = True
class hikari.dataframes.cif.CifReaderBuffer(target)[source]

Bases: CifIOBuffer

Buffer for reading data from cif file into CifReader

Parameters:

target (dict)

target: dict
add(word)[source]

Append the word to names or values based on its first char

Parameters:

word (str)

Return type:

None

flush()[source]

Update the target dict with names and values stored hitherto

Return type:

None

class hikari.dataframes.cif.CifReader(cif_file_path, validate=True)[source]

Bases: CifIO

A helper class managing reading cif files into CifFrame or CifBlock.

property blocks: dict[str, int]

line numbers where they start in cif file.

Type:

A dict of block names

Return type:

dict[str, int]

_blocks(lines)[source]
Parameters:

lines (Sequence[str])

Return type:

dict[str, int]

class State[source]

Bases: enum.Enum

This class stores current cif reading state (e.g. inside loop etc.)

default = 0
loop_keys = 1
loop_values = 2
format_dictionary(parsed_dict_)[source]

Reformat a dictionary of parsed data so that the format of every name and value agrees with the cif core dictionary stored in CifValidator.

Parameters:
  • parsed_dict – Dictionary with data pairs

  • parsed_dict_ (dict[str, list[str]])

Returns:

Data dictionary with correctly formatted data names and values

Return type:

dict[str, Union[str, list[str]]]

parse_lines(start, end)[source]

Read the data from lines numbered start to end, interpret it, and return it as an instance of a dict.

Parameters:
  • start (int) – number of the first line which data should be read from

  • end (int) – number of the first line which should not be read anymore

Returns:

ordered dictionary with name: value pairs for all parsed lines

Return type:

dict

read()[source]

Read the contents of cif currently pointed by file_path and block data_block_header and return them to a dict.

Returns:

A dictionary containing information read from .cif file.

Return type:

dict

protect_multilines()[source]

Replace whitespace between every pair of “n;” sequences with substitutes and remove the outer semicolons in self.file_contents.

Return type:

None

protect_quotes()[source]

Replace whitespace between every pair of matching quotation marks (single or double) with substitutes and remove the outer quotation marks in self.contents. See stack overflow /q/46967465/ for details.

Return type:

None

classmethod _protect_split(split_string)[source]
Parameters:

split_string (list[str])

Return type:

str

remove_comments()[source]

Replace all comment blocks (whitespace or start-of-file followed by “#”) within self.file_contents with empty strings.

Return type:

None

classmethod revert_delimiters_and_whitespace(string)[source]

If present, remove outer delimiters (matching quotes or semicolons) from supplied string, remove self.WHITESPACE_SUBSTITUTES and return string.

Returns:

string without outer delimiters nor whitespace substitutes.

Parameters:

string (str)

Return type:

str

class hikari.dataframes.cif.CifWriterBuffer(target)[source]

Bases: CifIOBuffer

Buffer for writing data from CifReader into cif file

Parameters:

target (TextIO)

MAX_NAME_LENGTH = 33
MAX_LINE_LENGTH = 80
MIN_STEP_LENGTH = 2
WHITESPACE
target: TextIO
current__category = ''
current__list = False
current_len = 0
add(data)[source]
Parameters:

data (tuple)

Return type:

None

flush()[source]
Return type:

None

format_line(k, v)[source]
Return type:

str

format_table()[source]
Return type:

str

enquote(text, force=False)[source]
Parameters:
  • text (str)

  • force (bool)

Return type:

str

class hikari.dataframes.cif.CifWriter(cif_file_path, validate=True)[source]

Bases: CifIO

A helper class managing writing CifFrame or CifBlock into cif files

write(cif_frame)[source]
Parameters:

cif_frame (CifFrame)

Return type:

None

hikari.dataframes.cif.cif_core_validator