<commit_before>import os
# import qt
import h5py
from pycqed.analysis import analysis_toolbox as a_tools
import errno

import sys
import glob
from os.path import join, dirname, exists
from os import makedirs



def get_git_revision_hash():
    import logging
    import subprocess
    try:
        # Refers to the global qc_config
        PycQEDdir = qc_config['PycQEDdir']
        hash = subprocess.check_output(['git', 'rev-parse',
                                        '--short=7', 'HEAD'], cwd=PycQEDdir)
    except:
        logging.warning('Failed to get Git revision hash, using 00000 instead')
        hash = '00000'

    return hash


def str_to_bool(s):
    valid = {'true': True, 't': True, '1': True,
             'false': False, 'f': False, '0': False, }
    if s.lower() not in valid:
        raise KeyError('{} not a valid boolean string'.format(s))
    b = valid[s.lower()]
    return b


def bool_to_int_str(b):
    if b:
        return '1'
    else:
        return '0'


def mopen(filename, mode='w'):
    if not exists(dirname(filename)):
        try:
            makedirs(dirname(filename))
        except OSError as exc:  # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise
    file = open(filename, mode='w')
    return file


def dict_to_ordered_tuples(dic):
    '''Convert a dictionary to a list of tuples, sorted by key.'''
    if dic is None:
        return []
    keys = dic.keys()
    # keys.sort()
    ret = [(key, dic[key]) for key in keys]
    return ret

def to_hex_string(byteval):
    '''
    Returns a hex representation of bytes for printing purposes
    '''
    return "b'" + ''.join('\\x{:02x}'.format(x) for x in byteval) + "'"


def load_settings_onto_instrument(instrument, load_from_instr=None, folder=None,
                                  label=None,
                                  timestamp=None, **kw):
        '''
        Loads settings from an hdf5 file onto the instrument handed to the
        function.
        By default uses the last hdf5 file in the datadirectory.
        By giving a label or timestamp another file can be chosen as the
        settings file.
        '''

        older_than = None
        instrument_name = instrument.name
        success = False
        count = 0
        while success is False and count < 10:
            try:
                if folder is None:
                    folder = a_tools.get_folder(timestamp=timestamp,
                                                older_than=older_than, **kw)
                else:
                    folder = folder
                filepath = a_tools.measurement_filename(folder)
                f = h5py.File(filepath, 'r')
                sets_group = f['Instrument settings']
                if load_from_instr is None:
                    ins_group = sets_group[instrument_name]
                else:
                    ins_group = sets_group[load_from_instr]
                print('Loaded Settings Successfully')
                success = True
            except:
                older_than = os.path.split(folder)[0][-8:] \
                             + '_' + os.path.split(folder)[1][:6]
                folder = None
                success = False
            count += 1

        if not success:
            print('Could not open settings for instrument "%s"' % (
                instrument_name))
            return False

        for parameter, value in ins_group.attrs.items():
            if value != 'None':  # None is saved as string in hdf5
                if type(value) == str:
                    if value == 'False':
                        try:
                            instrument.set(parameter, False)
                        except:
                            print('Could not set parameter: "%s" to "%s" for instrument "%s"' % (
                                parameter, value, instrument_name))
                    else:
                        try:
                            instrument.set(parameter, float(value))
                        except Exception:
                            try:
                                instrument.set(parameter, value)
                            except:
                                print('Could not set parameter: "%s" to "%s" for instrument "%s"' % (
                                    parameter, value, instrument_name))
                        except Exception:
                            try:
                                instrument.set(parameter, int(value))
                            except:
                                print('Could not set parameter: "%s" to "%s" for instrument "%s"' % (
                                    parameter, value, instrument_name))
                else:
                    instrument.set(parameter, value)
        f.close()
        return True


def send_email(subject='PycQED needs your attention!',
               body = '', email=None):
    # Import smtplib for the actual sending function
    import smtplib
    # Here are the email package modules we'll need
    from email.mime.image import MIMEImage
    from email.mime.multipart import MIMEMultipart<commit_msg>Fixed bug that prevented ints from loading correctly<commit_after>import os
# import qt
import h5py
from pycqed.analysis import analysis_toolbox as a_tools
import errno

import sys
import glob
from os.path import join, dirname, exists
from os import makedirs



def get_git_revision_hash():
    import logging
    import subprocess
    try:
        # Refers to the global qc_config
        PycQEDdir = qc_config['PycQEDdir']
        hash = subprocess.check_output(['git', 'rev-parse',
                                        '--short=7', 'HEAD'], cwd=PycQEDdir)
    except:
        logging.warning('Failed to get Git revision hash, using 00000 instead')
        hash = '00000'

    return hash


def str_to_bool(s):
    valid = {'true': True, 't': True, '1': True,
             'false': False, 'f': False, '0': False, }
    if s.lower() not in valid:
        raise KeyError('{} not a valid boolean string'.format(s))
    b = valid[s.lower()]
    return b


def bool_to_int_str(b):
    if b:
        return '1'
    else:
        return '0'


def mopen(filename, mode='w'):
    if not exists(dirname(filename)):
        try:
            makedirs(dirname(filename))
        except OSError as exc:  # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise
    file = open(filename, mode='w')
    return file


def dict_to_ordered_tuples(dic):
    '''Convert a dictionary to a list of tuples, sorted by key.'''
    if dic is None:
        return []
    keys = dic.keys()
    # keys.sort()
    ret = [(key, dic[key]) for key in keys]
    return ret

def to_hex_string(byteval):
    '''
    Returns a hex representation of bytes for printing purposes
    '''
    return "b'" + ''.join('\\x{:02x}'.format(x) for x in byteval) + "'"


def load_settings_onto_instrument(instrument, load_from_instr=None, folder=None,
                                  label=None,
                                  timestamp=None, **kw):
        '''
        Loads settings from an hdf5 file onto the instrument handed to the
        function.
        By default uses the last hdf5 file in the datadirectory.
        By giving a label or timestamp another file can be chosen as the
        settings file.
        '''

        older_than = None
        instrument_name = instrument.name
        success = False
        count = 0
        while success is False and count < 10:
            try:
                if folder is None:
                    folder = a_tools.get_folder(timestamp=timestamp,
                                                older_than=older_than, **kw)
                else:
                    folder = folder
                filepath = a_tools.measurement_filename(folder)
                f = h5py.File(filepath, 'r')
                sets_group = f['Instrument settings']
                if load_from_instr is None:
                    ins_group = sets_group[instrument_name]
                else:
                    ins_group = sets_group[load_from_instr]
                print('Loaded Settings Successfully')
                success = True
            except:
                older_than = os.path.split(folder)[0][-8:] \
                             + '_' + os.path.split(folder)[1][:6]
                folder = None
                success = False
            count += 1

        if not success:
            print('Could not open settings for instrument "%s"' % (
                instrument_name))
            return False

        for parameter, value in ins_group.attrs.items():
            if value != 'None':  # None is saved as string in hdf5
                if type(value) == str:
                    if value == 'False':
                        try:
                            instrument.set(parameter, False)
                        except:
                            print('Could not set parameter: "%s" to "%s" for instrument "%s"' % (
                                parameter, value, instrument_name))
                    else:
                        try:
                            instrument.set(parameter, float(value))
                        except Exception:
                            try:
                                instrument.set(parameter, value)
                            except:
                                try:
                                    instrument.set(parameter, int(value))
                                except:
                                    print('Could not set parameter: "%s" to "%s" for instrument "%s"' % (
                                        parameter, value, instrument_name))
                else:
                    instrument.set(parameter, value)
        f.close()
        return True


def send_email(subject='PycQED needs your attention!',
               body = '', email=None):
    # Import smtplib for the actual sending function
    import smtplib
    # Here are the email package modules we'll need
    from email.mime.image import MIMEImage
    from email.mime.multipart import MIMEMultipart
{'fraction_non_alphanumeric': 0.08707616707616708, 'fraction_numerical': 0.005896805896805897, 'mean_word_length': 1.446742005289733, 'pattern_counts': {'":': 0, '<': 5, '<?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': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5366783', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2556, 'n_words': 811}