B
    3Rc                 @   sN   d dl mZ G dd dejZdd ZedkrJd dlZe \ZZe	e dS )    )Chemc               @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )PropertyMola)   allows rdkit molecules to be pickled with their properties saved.

     >>> import os
     >>> import pickle
     >>> from rdkit import RDConfig
     >>> m = Chem.MolFromMolFile(os.path.join(RDConfig.RDCodeDir, 'Chem', 'test_data/benzene.mol'))
     >>> m.GetProp('_Name')
     'benzene.mol'

     by default pickling removes properties:

     >>> m2 = pickle.loads(pickle.dumps(m))
     >>> m2.HasProp('_Name')
     0

     Property mols solve this:

     >>> pm = PropertyMol(m)
     >>> pm.GetProp('_Name')
     'benzene.mol'
     >>> pm.SetProp('MyProp','foo')
     >>> pm.HasProp('MyProp')
     1

     >>> pm2 = pickle.loads(pickle.dumps(pm))
     >>> Chem.MolToSmiles(pm2)
     'c1ccccc1'
     >>> pm2.GetProp('_Name')
     'benzene.mol'
     >>> pm2.HasProp('MyProp')
     1
     >>> pm2.GetProp('MyProp')
     'foo'
     >>> pm2.HasProp('MissingProp')
     0

     Property mols are a bit more permissive about the types
     of property values:

     >>> pm.SetProp('IntVal',1)

     That wouldn't work with a standard mol

     but the Property mols still convert all values to strings before storing:

     >>> pm.GetProp('IntVal')
     '1'

     This is a test for sf.net issue 2880943: make sure properties end up in SD files:

     >>> import tempfile, os
     >>> fn = tempfile.NamedTemporaryFile(suffix='.sdf', delete=False).name
     >>> w = Chem.SDWriter(fn)
     >>> w.write(pm)
     >>> w=None
     >>> with open(fn,'r') as inf:
     ...   txt = inf.read()
     >>> '<IntVal>' in txt
     True
     >>> try:
     ...   os.unlink(fn)
     ... except Exception:
     ...   pass

     The next level of that bug: does writing a *depickled* propertymol
     to an SD file include properties:

     >>> fn = tempfile.NamedTemporaryFile(suffix='.sdf', delete=False).name
     >>> w = Chem.SDWriter(fn)
     >>> pm = pickle.loads(pickle.dumps(pm))
     >>> w.write(pm)
     >>> w=None
     >>> with open(fn,'r') as inf:
     ...   txt = inf.read()
     >>> '<IntVal>' in txt
     True
     >>> try:
     ...   os.unlink(fn)
     ... except Exception:
     ...   pass



    Tc             C   sJ   t |tjsd S tj| | x&|jddD ]}| ||| q,W d S )NT)includePrivate)
isinstancer   Mol__init__GetPropNamesSetPropGetProp)selfZmolpn r   5lib/python3.7/site-packages/rdkit/Chem/PropertyMol.pyr   _   s
    zPropertyMol.__init__c             C   s   t j| |t| d S )N)r   r   r	   str)r   Znmvalr   r   r   r	   f   s    zPropertyMol.SetPropc             C   s6   i }x"| j ddD ]}| |||< qW |  |dS )NT)r   )pklpropD)r   r
   ZToBinary)r   ZpDictr   r   r   r   __getstate__i   s    zPropertyMol.__getstate__c             C   s<   t j| |d  x$|d  D ]\}}| || q W d S )Nr   r   )r   r   r   itemsr	   )r   ZstateDZpropr   r   r   r   __setstate__o   s    zPropertyMol.__setstate__N)	__name__
__module____qualname____doc__Z__getstate_manages_dict__r   r	   r   r   r   r   r   r   r      s   Tr   c              C   s&   dd l } dd l}| j|jd | jdS )Nr   __main__)Zoptionflags)doctestsysZtestmodmodulesELLIPSIS)r   r   r   r   r   _testz   s    r   r   N)
Zrdkitr   r   r   r   r   r   ZfailedZtriedexitr   r   r   r   <module>   s   r
