<commit_before>"""
Created on 17 Apr 2017

@author: <NAME> (<EMAIL>)

http://pythoncentral.io/hashing-strings-with-python/

example document:
{"tag": "scs-be2-3",
"rec": "2017-08-29T10:12:32.056+00:00",
"cmd": {"cmd": "?", "params": [], "stdout": ["[\"afe_baseline\", \"afe_calib\""], "stderr": [], "ret": 0},
"omd": "6e81c77aa20562ea06e0e32158d2c7c9431ed251cd5790917f6cb385f6cf62c0",
"digest": "59cb897c308050f6d07f400cc3b784bfa81938d4e8a36896bec2126d24c8fe00"}
"""

import hashlib

from collections import OrderedDict

from scs_core.control.command import Command

from scs_core.data.json import JSONable
from scs_core.data.json import JSONify
from scs_core.data.localized_datetime import LocalizedDatetime

from scs_core.sample.sample import Sample


# --------------------------------------------------------------------------------------------------------------------

class ControlReceipt(JSONable):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def construct_from_jdict(cls, jdict):
        if not jdict:
            return None

        tag = jdict.get('tag')

        rec = LocalizedDatetime.construct_from_iso8601(jdict.get('rec'))
        command = Command.construct_from_jdict(jdict.get('cmd'))
        omd = jdict.get('omd')
        digest = jdict.get('digest')

        datum = ControlReceipt(tag, rec, command, omd, digest)

        return datum


    @classmethod
    def construct_from_datum(cls, datum, rec, command, key):
        digest = ControlReceipt.__hash(datum.attn, rec, command, datum.digest, key)

        return ControlReceipt(datum.attn, rec, command, datum.digest, digest)


    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def __hash(cls, tag, rec, command, omd, subscriber_sn):
        rec_iso8601 = rec.as_iso8601(Sample.INCLUDE_MILLIS)

        text = str(tag) + JSONify.dumps(rec_iso8601) + JSONify.dumps(command) + str(omd) + str(subscriber_sn)
        hash_object = hashlib.sha256(text.encode())
<commit_msg>Added milliseconds control to LocalizedDatetime class.<commit_after>"""
Created on 17 Apr 2017

@author: <NAME> (<EMAIL>)

http://pythoncentral.io/hashing-strings-with-python/

example document:
{"tag": "scs-be2-3",
"rec": "2017-08-29T10:12:32.056+00:00",
"cmd": {"cmd": "?", "params": [], "stdout": ["[\"afe_baseline\", \"afe_calib\""], "stderr": [], "ret": 0},
"omd": "6e81c77aa20562ea06e0e32158d2c7c9431ed251cd5790917f6cb385f6cf62c0",
"digest": "59cb897c308050f6d07f400cc3b784bfa81938d4e8a36896bec2126d24c8fe00"}
"""

import hashlib

from collections import OrderedDict

from scs_core.control.command import Command

from scs_core.data.json import JSONable
from scs_core.data.json import JSONify
from scs_core.data.localized_datetime import LocalizedDatetime

from scs_core.sample.sample import Sample


# --------------------------------------------------------------------------------------------------------------------

class ControlReceipt(JSONable):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def construct_from_jdict(cls, jdict):
        if not jdict:
            return None

        tag = jdict.get('tag')

        rec = LocalizedDatetime.construct_from_iso8601(jdict.get('rec'))
        command = Command.construct_from_jdict(jdict.get('cmd'))
        omd = jdict.get('omd')
        digest = jdict.get('digest')

        datum = ControlReceipt(tag, rec, command, omd, digest)

        return datum


    @classmethod
    def construct_from_datum(cls, datum, rec, command, key):
        digest = ControlReceipt.__hash(datum.attn, rec, command, datum.digest, key)

        return ControlReceipt(datum.attn, rec, command, datum.digest, digest)


    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def __hash(cls, tag, rec, command, omd, subscriber_sn):
        text = str(tag) + rec.as_iso8601(Sample.INCLUDE_MILLIS) + JSONify.dumps(command) + str(omd) + str(subscriber_sn)
        hash_object = hashlib.sha256(text.encode())

{'fraction_non_alphanumeric': 0.2688122250520954, 'fraction_numerical': 0.05973605001157675, 'mean_word_length': 2.0252100840336134, 'pattern_counts': {'":': 20, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5987643', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1300, 'n_words': 247}