<commit_before>#!python
# -*- coding: utf-8 -*-
"""
point.py: Implementation of a datapoint in Project Haystack
"""

import hszinc
import time
import logging
import datetime

from ..history.HisRecord import HisRecord

class HaystackPoint(object):
    """
    The point class acts as a container for entity metadata and provides an
    interface for conveniently accessing the point's real-time and historical
    data.
    """

    def __init__(self, session, point_id, meta=None):
        """
        Create a point that references some Haystack entity.
        """
        self._session   = session
        self._point_id  = point_id
        self._log       = session._log.getChild('pt.%s' % point_id)

        # We do lazy evaluation on the point metadata.
        self._meta      = None
        self._meta_expiry = 0

        if meta is not None:
            self._load_meta(meta)

    def __repr__(self):
        """
        Dump a rough representation of the point.
        """
        return '%s(%r, %r)' % (self.__class__.__name__,
                self._session, self._point_id)

    def _refresh_meta(self, force=False):
        """
        Retrieve the metadata from the server if out of date.
        """
        if (not force) and (self._meta_expiry > time.time()):
            # We're not being forced and the refresh isn't due yet.
            return

        meta = self._session._get_grid('read', \
                id=hszinc.dump_scalar(hszinc.Ref(self._point_id)))
        self._load_meta(meta[0])

    def _load_meta(self, meta):
        """
        Store the metadata loaded from elsewhere and re-set the expiry timer.
        """
        meta_id = meta.pop('id',None)
        if meta_id is None:
            raise ValueError('Point %s no longer exists' % self._point_id)
        elif meta_id.name != self._point_id:
            raise ValueError('Metadata for incorrect point (%s vs %s)' \
                    % (meta_id.name, self._point_id))

        self._meta = meta
        self._meta_expiry = time.time() + 300.0 # TODO: define this

    def _localise_dt(self, dt):
        """
        Localise a datetime object for this timezone.
        """
        if not isinstance(dt, datetime.datetime):
            raise TypeError('%r is not a datetime.datetime' % dt)

        if dt.tzinfo is None:<commit_msg>client.point: Add _refresh_due property<commit_after>#!python
# -*- coding: utf-8 -*-
"""
point.py: Implementation of a datapoint in Project Haystack
"""

import hszinc
import time
import logging
import datetime

from ..history.HisRecord import HisRecord

class HaystackPoint(object):
    """
    The point class acts as a container for entity metadata and provides an
    interface for conveniently accessing the point's real-time and historical
    data.
    """

    def __init__(self, session, point_id, meta=None):
        """
        Create a point that references some Haystack entity.
        """
        self._session   = session
        self._point_id  = point_id
        self._log       = session._log.getChild('pt.%s' % point_id)

        # We do lazy evaluation on the point metadata.
        self._meta      = None
        self._meta_expiry = 0

        if meta is not None:
            self._load_meta(meta)

    def __repr__(self):
        """
        Dump a rough representation of the point.
        """
        return '%s(%r, %r)' % (self.__class__.__name__,
                self._session, self._point_id)

    @property
    def _refresh_due(self):
        """
        Return True if a refresh is due.
        """
        return (self._meta_expiry <= time.time())

    def _refresh_meta(self, force=False):
        """
        Retrieve the metadata from the server if out of date.
        """
        if not (force or self._refresh_due):
            # We're not being forced and the refresh isn't due yet.
            return

        meta = self._session._get_grid('read', \
                id=hszinc.dump_scalar(hszinc.Ref(self._point_id)))
        self._load_meta(meta[0])

    def _load_meta(self, meta):
        """
        Store the metadata loaded from elsewhere and re-set the expiry timer.
        """
        meta_id = meta.pop('id',None)
        if meta_id is None:
            raise ValueError('Point %s no longer exists' % self._point_id)
        elif meta_id.name != self._point_id:
            raise ValueError('Metadata for incorrect point (%s vs %s)' \
                    % (meta_id.name, self._point_id))

        self._meta = meta
        self._meta_expiry = time.time() + 300.0 # TODO: define this

    def _localise_dt(self, dt):
        """
        Localise a datetime object for this timezone.
        """
        if not isinstance(dt, datetime.datetime):
            raise TypeError('%r is not a datetime.datetime' % dt)

        if dt.tzinfo is None:
{'fraction_non_alphanumeric': 0.1001866832607343, 'fraction_numerical': 0.002903961833644472, 'mean_word_length': 2.063532401524778, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '4090937', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1344, 'n_words': 430}