Skip to content

Commit

Permalink
files parsers docs updated
Browse files Browse the repository at this point in the history
  • Loading branch information
stsouko committed Mar 19, 2019
1 parent 321be59 commit 5feecef
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
10 changes: 8 additions & 2 deletions CGRtools/files/INCHIrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

class INCHIread(CGRread, WithMixin):
"""
accept file wich consist INCHI per lines
INCHI separated per lines files reader. works similar to opened file object. support `with` context manager.
on initialization accept opened in text mode file, string path to file,
pathlib.Path object or another buffered reader object.
line should be start with INCHI string and
optionally continues with space/tab separated list of key:value [or key=value] data.
AuxInfo also can be stored in data.
Expand All @@ -45,6 +46,11 @@ def __init__(self, file, *args, **kwargs):
self.__data = self.__reader()

def read(self):
"""
parse whole file
:return: list of parsed molecules
"""
return list(self.__data)

def __iter__(self):
Expand Down
23 changes: 23 additions & 0 deletions CGRtools/files/MRVrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,22 @@ def xml_dict(parent_element, stop_list=None):


class MRVread(CGRread, WithMixin):
"""
ChemAxon MRV files reader. works similar to opened file object. support `with` context manager.
on initialization accept opened in binary mode file, string path to file,
pathlib.Path object or another binary buffered reader object
"""
def __init__(self, file, *args, **kwargs):
super().__init__(*args, **kwargs)
super(CGRread, self).__init__(file, 'rb')
self.__data = self.__reader()

def read(self):
"""
parse whole file
:return: list of parsed molecules or reactions
"""
return list(self.__data)

def __iter__(self):
Expand Down Expand Up @@ -260,17 +270,30 @@ def __parse_molecule(self, data):


class MRVwrite(CGRwrite, WithMixin):
"""
ChemAxon MRV files writer. works similar to opened for writing file object. support `with` context manager.
on initialization accept opened for writing in text mode file, string path to file,
pathlib.Path object or another buffered writer object
"""
def __init__(self, file, *args, **kwargs):
super().__init__(*args, **kwargs)
super(CGRwrite, self).__init__(file, 'w')

def close(self, *args, **kwargs):
"""
write close tag of MRV file and close opened file
:param force: force closing of externally opened file or buffer
"""
if not self.__finalized:
self._file.write('</cml>')
self.__finalized = True
super().close(*args, **kwargs)

def write(self, data):
"""
write single molecule or reaction into file
"""
self._file.write('<cml>')
self.__write(data)
self.write = self.__write
Expand Down
18 changes: 18 additions & 0 deletions CGRtools/files/RDFrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@


class RDFread(CGRread, WithMixin):
"""
MDL RDF files reader. works similar to opened file object. support `with` context manager.
on initialization accept opened in text mode file, string path to file,
pathlib.Path object or another buffered reader object
"""
def __init__(self, file, *args, **kwargs):
super().__init__(*args, **kwargs)
super(CGRread, self).__init__(file)
self.__data = self.__reader()

def read(self):
"""
parse whole file
:return: list of parsed molecules or reactions
"""
return list(self.__data)

def __iter__(self):
Expand Down Expand Up @@ -127,11 +137,19 @@ def __reader(self):


class RDFwrite(MOLwrite, WithMixin):
"""
MDL RDF files writer. works similar to opened for writing file object. support `with` context manager.
on initialization accept opened for writing in text mode file, string path to file,
pathlib.Path object or another buffered writer object
"""
def __init__(self, file, *args, **kwargs):
super().__init__(*args, **kwargs)
super(CGRwrite, self).__init__(file, 'w')

def write(self, data):
"""
write single molecule or reaction into file
"""
self._file.write(strftime('$RDFILE 1\n$DATM %m/%d/%y %H:%M\n'))
self.__write(data)
self.write = self.__write
Expand Down
18 changes: 18 additions & 0 deletions CGRtools/files/SDFrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@


class SDFread(CGRread, WithMixin):
"""
MDL SDF files reader. works similar to opened file object. support `with` context manager.
on initialization accept opened in text mode file, string path to file,
pathlib.Path object or another buffered reader object
"""
def __init__(self, file, *args, **kwargs):
super().__init__(*args, **kwargs)
super(CGRread, self).__init__(file)
self.__data = self.__reader()

def read(self):
"""
parse whole file
:return: list of parsed molecules
"""
return list(self.__data)

def __iter__(self):
Expand Down Expand Up @@ -101,11 +111,19 @@ def __reader(self):


class SDFwrite(MOLwrite, WithMixin):
"""
MDL SDF files writer. works similar to opened for writing file object. support `with` context manager.
on initialization accept opened for writing in text mode file, string path to file,
pathlib.Path object or another buffered writer object
"""
def __init__(self, file, *args, **kwargs):
super().__init__(*args, **kwargs)
super(CGRwrite, self).__init__(file, 'w')

def write(self, data):
"""
write single molecule into file
"""
m = self._convert_structure(data)
self._file.write(self._format_mol(*m))
self._file.write('M END\n')
Expand Down
12 changes: 9 additions & 3 deletions CGRtools/files/SMILESrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@

class SMILESread(CGRread, WithMixin):
"""
accept file wich consist smiles/smirks per lines
line should be start with smiles/smirks string and
SMILES separated per lines files reader. works similar to opened file object. support `with` context manager.
on initialization accept opened in text mode file, string path to file,
pathlib.Path object or another buffered reader object.
line should be start with SMILES string and
optionally continues with space/tab separated list of key:value [or key=value] data.
for reactions . [dot] in bonds should be used only for molecules separation.
Expand All @@ -42,6 +43,11 @@ def __init__(self, file, *args, **kwargs):
self.__data = self.__reader()

def read(self):
"""
parse whole file
:return: list of parsed molecules or reactions
"""
return list(self.__data)

def __iter__(self):
Expand Down
5 changes: 5 additions & 0 deletions CGRtools/files/_CGRrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def __init__(self, file, mode='r'):
self.__write = mode == 'w'

def close(self, force=False):
"""
close opened file
:param force: force closing of externally opened file or buffer
"""
if self.__write:
self.write = self.__write_adhoc
self.__write = False
Expand Down

0 comments on commit 5feecef

Please sign in to comment.