<commit_before># -*- coding: utf-8 -*-
import visa


from pprint import pprint

import Instrument.PowerMeter
import Instrument.SignalGenerator
import Instrument.WaveformGenerator
import Instrument.SpectrumAnalyser
import Instrument.NetworkAnalyser
import Instrument.ElectronicAttenuator
import Instrument.DigitalMultimeter
import Instrument.EnviromentalChamber
import Instrument.PowerAnalyser
import Instrument.SurgeGenerator
import Instrument.AudioAnalyser
import Instrument.Osciliscope
import Instrument.FieldStrength
import Instrument.Positioner
import Instrument.FrequencyCounter
import Instrument.SourceDC
import Instrument.SourceAC
import Instrument.SwitchMatrix
import Instrument.ModulationMeter

Drivers = [
    Instrument.SignalGenerator,
    Instrument.PowerMeter,
    Instrument.SpectrumAnalyser,
    Instrument.NetworkAnalyser,
    Instrument.AudioAnalyser,
    Instrument.PowerAnalyser,
    Instrument.SurgeGenerator,
    Instrument.FrequencyCounter,
    Instrument.ElectronicAttenuator,
    Instrument.DigitalMultimeter,
    Instrument.Osciliscope,
    Instrument.FieldStrength,
    Instrument.EnviromentalChamber,
    Instrument.Positioner,
    Instrument.SourceDC,
    Instrument.SourceAC,
    Instrument.SwitchMatrix,
    Instrument.ModulationMeter,
    Instrument.WaveformGenerator,
]

# counter = 0
for each in Drivers:
    pass
    pprint(each)
    pprint(each.REGISTER)
    print()
    # counter += len(each.REGISTER)

Drivernames = []
for driver in Drivers:
    # print(driver)
    Drivernames.append(repr(driver).split("'")[1].split('.')[1])  # Extract class name from repr

# print(counter)

# print(REGISTER)


class ResourceManager(object):
    """ResourceManager as a context manager."""

    def __init__(self, rm):
        self.rm = visa.ResourceManager(rm)

    def __enter__(self):
        return(self.rm)

    def __exit__(self, exc_type, exc, exc_tb):
        del(self.rm)


class Instrument(object):
    """ResourceManager.open_resource as a context manager for instrument."""

    def __init__(self, rm, resource, *, read_termination=False, write_termination=False, **kwargs):

        if read_termination is False:
            read_termination = '\n'
        if write_termination is False:
            write_termination = '\r\n' if resource.startswith('ASRL') else '\n'

        self.inst = rm.open_resource(resource, read_termination=read_termination, write_termination=write_termination, **kwargs)
        # return self.inst

    def __enter__(self):
        # print(repr(self.inst))
        return(self.inst)

    def __exit__(self, *args):
        self.inst.close()


'''
print()
print()

with ResourceManager('@sim') as rm:
    print(rm.list_resources())
    print(rm)

    with Instrument(rm, 'USB::0x1111::0x2222::0x4444::INSTR') as inst:
        # print(inst)
        print(inst.query("*IDN?"))
        # print(inst.session)
    # print(inst.session)
# print(rm.session)
'''
<commit_msg>Move driver initialization to Instrument.__init__<commit_after># -*- coding: utf-8 -*-
import visa


from pprint import pprint

import Instrument.PowerMeter
import Instrument.SignalGenerator
import Instrument.WaveformGenerator
import Instrument.SpectrumAnalyser
import Instrument.NetworkAnalyser
import Instrument.ElectronicAttenuator
import Instrument.DigitalMultimeter
import Instrument.EnviromentalChamber
import Instrument.PowerAnalyser
import Instrument.SurgeGenerator
import Instrument.AudioAnalyser
import Instrument.Osciliscope
import Instrument.FieldStrength
import Instrument.Positioner
import Instrument.FrequencyCounter
import Instrument.SourceDC
import Instrument.SourceAC
import Instrument.SwitchMatrix
import Instrument.ModulationMeter
import Instrument.ElectronicLoad

Drivers = [
    Instrument.SignalGenerator,
    Instrument.PowerMeter,
    Instrument.SpectrumAnalyser,
    Instrument.NetworkAnalyser,
    Instrument.AudioAnalyser,
    Instrument.PowerAnalyser,
    Instrument.SurgeGenerator,
    Instrument.FrequencyCounter,
    Instrument.ElectronicAttenuator,
    Instrument.DigitalMultimeter,
    Instrument.Osciliscope,
    Instrument.FieldStrength,
    Instrument.EnviromentalChamber,
    Instrument.Positioner,
    Instrument.SourceDC,
    Instrument.SourceAC,
    Instrument.SwitchMatrix,
    Instrument.ModulationMeter,
    Instrument.WaveformGenerator,
    Instrument.ElectronicLoad,

]

# counter = 0
for each in Drivers:
    pass
    pprint(each)
    pprint(each.REGISTER)
    print()
    # counter += len(each.REGISTER)

Drivernames = []
for driver in Drivers:
    # print(driver)
    Drivernames.append(repr(driver).split("'")[1].split('.')[1])  # Extract class name from repr

# print(counter)

# print(REGISTER)


class ResourceManager(object):
    """ResourceManager as a context manager."""

    def __init__(self, rm):
        self.rm = visa.ResourceManager(rm)

    def __enter__(self):
        return(self.rm)

    def __exit__(self, exc_type, exc, exc_tb):
        del(self.rm)


class Instrument(object):
    """ResourceManager.open_resource as a context manager for instrument."""

    def __init__(self, rm, resource, *, read_termination=False, write_termination=False, **kwargs):

        if read_termination is False:
            read_termination = '\n'
        if write_termination is False:
            write_termination = '\r\n' if resource.startswith('ASRL') else '\n'

        self.inst = rm.open_resource(resource, read_termination=read_termination, write_termination=write_termination, **kwargs)
        # return self.inst

    def __enter__(self):
        # print(repr(self.inst))
        return(self.inst)

    def __exit__(self, *args):
        self.inst.close()


'''
print()
print()

with ResourceManager('@sim') as rm:
    print(rm.list_resources())
    print(rm)

    with Instrument(rm, 'USB::0x1111::0x2222::0x4444::INSTR') as inst:
        # print(inst)
        print(inst.query("*IDN?"))
        # print(inst.session)
    # print(inst.session)
# print(rm.session)
'''

{'fraction_non_alphanumeric': 0.10028509139694784, 'fraction_numerical': 0.006372631225893007, 'mean_word_length': 4.028667790893761, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3421963', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1738, 'n_words': 365}