<commit_before># -*- coding: utf-8 -*-
"""
Created on Thu Jun 18 10:38:40 2015

@author: ktritz
"""
import xml.etree.ElementTree as ET
import os
from fdf_globals import *
from fdf_signal import Signal
import numpy as np
import modules
from collections import MutableMapping, OrderedDict
import MDSplus as mds
import types
import inspect
import pymssql

# changed nstx server to skylark.pppl.gov - ds 9/30/2015
mds_servers = {
    'nstx': 'skylark.pppl.gov:8501'
}

logbook_parameters = {
    'nstx': {
        'server': 'sql2008.pppl.gov\sql2008',
        'username': os.getenv('USER'),
        'password': '<PASSWORD>',
        'database': 'nstxlogs',
        'port': '62917',
        'table': 'entries'
    }
}

class Machine(MutableMapping):
    '''Factory root class that contains shot objects and MDS access methods.

    Basic class initialization is performed as follows:
    >>>nstx = Machine(name='nstx')

    the Machine class contains a model shot object: nstx.s0

    shot data can be accessed directly through the Machine class:
    >>>nstx.s141398
    >>>nstx.s141399

    alternatively, a list of shot #'s may be provided during initialization:
    >>>nstx = Machine(name='nstx', shotlist=[141398, 141399])

    or added later using the addshot method:
    >>>nstx.addshot([141398, 141399])
    '''

    # Maintain a dictionary of cached MDS server connections to speed up
    # access for multiple shots and trees. This is a static class variable
    # to avoid proliferation of MDS server connections
    _connections = OrderedDict()
    _parent = None
    _logbook_connection = None

    def __init__(self, name='nstx', shotlist=None, xp=None, date=None):
        self._shots = {}
        self._classlist = {}
        self._name = name.lower()
        self.s0 = Shot(0, root=self, parent=self)

        if len(self._connections) is 0:
            print('Precaching MDS server connections...')
            for _ in range(2):
                self._connections[mds.Connection(mds_servers[name])] = None
            print('Finished.')
        
        # logbook connection 9/30/15 DRS
        self._lbparams = logbook_parameters[self._name]
        if self._logbook_connection is None:
            self._make_logbook_connection()
        self._shotlist_query_prefix = (
            'SELECT DISTINCT rundate, shot, xp, voided '
            'FROM %s '
            'WHERE voided IS null' %
            self._lbparams['table'])
        
        # add shots
        if shotlist is not None:
            self.addshot(shotlist=shotlist)
        if xp is not None:
            self.addshot(xp=xp)
        if date is not None:
            self.addshot(date=date)

    def __getattr__(self, name):
        try:
            shot = int(name.split('s')[1])
        except:
            raise AttributeError("'{}' object has no attribute '{}'".format(
                                 type(self), name))
        if (shot not in self._shots):
            self._shots[shot] = Shot(shot, root=self, parent=self)
        return self._shots[shot]

    def __repr__(self):
        return '<machine {}>'.format(self._name)

    def __iter__(self):
        # return iter(self._shots.values())
        return iter(self._shots)

    def __contains__(self, value):
        return value in self._shots

    def __len__(self):<commit_msg>added xp and date functionality to Machine.addshot in factory.py<commit_after># -*- coding: utf-8 -*-
"""
Created on Thu Jun 18 10:38:40 2015

@author: ktritz
"""
import xml.etree.ElementTree as ET
import os
from fdf_globals import *
from fdf_signal import Signal
import numpy as np
import modules
from collections import MutableMapping, OrderedDict
import MDSplus as mds
import types
import inspect
import pymssql

# changed nstx server to skylark.pppl.gov - ds 9/30/2015
mds_servers = {
    'nstx': 'skylark.pppl.gov:8501'
}

logbook_parameters = {
    'nstx': {
        'server': 'sql2008.pppl.gov\sql2008',
        'username': os.getenv('USER'),
        'password': '<PASSWORD>',
        'database': 'nstxlogs',
        'port': '62917',
        'table': 'entries'
    }
}

class Machine(MutableMapping):
    '''Factory root class that contains shot objects and MDS access methods.

    Basic class initialization is performed as follows:
    >>>nstx = Machine(name='nstx')

    the Machine class contains a model shot object: nstx.s0

    shot data can be accessed directly through the Machine class:
    >>>nstx.s141398
    >>>nstx.s141399

    alternatively, a list of shot #'s may be provided during initialization:
    >>>nstx = Machine(name='nstx', shotlist=[141398, 141399])

    or added later using the addshot method:
    >>>nstx.addshot([141398, 141399])
    '''

    # Maintain a dictionary of cached MDS server connections to speed up
    # access for multiple shots and trees. This is a static class variable
    # to avoid proliferation of MDS server connections
    _connections = OrderedDict()
    _parent = None
    _logbook_connection = None

    def __init__(self, name='nstx', shotlist=None, xp=None, date=None):
        self._shots = {}
        self._classlist = {}
        self._name = name.lower()
        self.s0 = Shot(0, root=self, parent=self)

        if len(self._connections) is 0:
            print('Precaching MDS server connections...')
            for _ in range(2):
                self._connections[mds.Connection(mds_servers[name])] = None
            print('Finished.')
        
        # logbook connection 9/30/15 DRS
        self._lbparams = logbook_parameters[self._name]
        if self._logbook_connection is None:
            self._make_logbook_connection()
        self._shotlist_query_prefix = (
            'SELECT DISTINCT rundate, shot, xp, voided '
            'FROM %s '
            'WHERE voided IS null' %
            self._lbparams['table']) # used in get_shotlist*()
        
        # add shots
        if shotlist is not None:
            self.addshot(shotlist=shotlist)
        if xp is not None:
            self.addshot(xp=xp)
        if date is not None:
            self.addshot(date=date)

    def __getattr__(self, name):
        try:
            shot = int(name.split('s')[1])
        except:
            raise AttributeError("'{}' object has no attribute '{}'".format(
                                 type(self), name))
        if (shot not in self._shots):
            self._shots[shot] = Shot(shot, root=self, parent=self)
        return self._shots[shot]

    def __repr__(self):
        return '<machine {}>'.format(self._name)

    def __iter__(self):
        # return iter(self._shots.values())
        return iter(self._shots)

    def __contains__(self, value):
        return value in self._shots

    def __len__(self):
{'fraction_non_alphanumeric': 0.10736059479553903, 'fraction_numerical': 0.024981412639405204, 'mean_word_length': 2.4439324116743473, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1918300', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2007, 'n_words': 596}