# Path: pybar/utils/utils.py
# def string_is_binary(string):
#     try:
#         int(string, 2)
#         return True
#     except (TypeError, ValueError):
#         return False
# 
# def flatten_iterable(iterable):
#     """flatten iterable, but leaves out strings
# 
#     [[[1, 2, 3], [4, 5]], 6] -> [1, 2, 3, 4, 5, 6]
# 
#     """
#     for item in iterable:
#         if isinstance(item, collections.Iterable) and not isinstance(item, basestring):
#             for sub in flatten_iterable(item):
#                 yield sub
#         else:
#             yield item
# 
# def iterable(item):
#     """generate iterable from item, but leaves out strings
# 
#     """
#     if isinstance(item, collections.Iterable) and not isinstance(item, basestring):
#         return item
#     else:
#         return [item]


import logging
import re
import os
import struct
import copy
import datetime
import numpy as np
import tables as tb
from ast import literal_eval
from collections import OrderedDict
from contextlib import contextmanager
from importlib import import_module
from operator import itemgetter
from bitarray import bitarray
from pybar.utils.utils import string_is_binary, flatten_iterable, iterable

                    register_littleendian = True
                if (16 * register_object['address'] + register_object['offset'] < 16 * (register_address + 1) and 16 * register_object['address'] + register_object['offset'] + register_object['bitlength'] > 16 * register_address):
                    reg = bitarray_from_value(value=register_object['value'], size=register_object['bitlength'])
                    if register_object['littleendian']:
                        reg.reverse()
# register_bitset[max(0, 16 * (register_object['address'] - register_address) + register_object['offset']):min(16, 16 * (register_object['address'] - register_address) + register_object['offset'] + register_object['bitlength'])] |= reg[max(0, 16 * (register_address - register_object['address']) - register_object['offset']):min(register_object['bitlength'], 16 * (register_address - register_object['address'] + 1) - register_object['offset'])]  # [ bit(n) bit(n-1)... bit(0) ]
                    register_bitset[max(0, 16 - 16 * (register_object['address'] - register_address) - register_object['offset'] - register_object['bitlength']):min(16, 16 - 16 * (register_object['address'] - register_address) - register_object['offset'])] |= reg[max(0, register_object['bitlength'] - 16 - 16 * (register_address - register_object['address']) + register_object['offset']):min(register_object['bitlength'], register_object['bitlength'] + 16 - 16 * (register_address - register_object['address'] + 1) + register_object['offset'])]  # [ bit(0)... bit(n-1) bit(n) ]
                else:
                    raise Exception("wrong register object")
            if register_littleendian:
                register_bitset.reverse()
            register_bitsets.append(register_bitset)
        return register_bitsets

    def get_pixel_register_objects(self, do_sort=None, reverse=False, **kwargs):
        """Generate register objects (list) from register name list

        Usage: get_pixel_register_objects(name = ["TDAC", "FDAC"])
        Receives: keyword lists of register names, addresses,...
        Returns: list of register objects

        """
        # speed up of the most often used keyword name
        try:
            names = iterable(kwargs.pop('name'))
        except KeyError:
            register_objects = []
        else:
            register_objects = [self.pixel_registers[reg] for reg in names]
        for keyword in kwargs.iterkeys():

# Path: pybar/utils/utils.py
# def string_is_binary(string):
#     try:
#         int(string, 2)
#         return True
#     except (TypeError, ValueError):
#         return False
# 
# def flatten_iterable(iterable):
#     """flatten iterable, but leaves out strings
# 
#     [[[1, 2, 3], [4, 5]], 6] -> [1, 2, 3, 4, 5, 6]
# 
#     """
#     for item in iterable:
#         if isinstance(item, collections.Iterable) and not isinstance(item, basestring):
#             for sub in flatten_iterable(item):
#                 yield sub
#         else:
#             yield item
# 
# def iterable(item):
#     """generate iterable from item, but leaves out strings
# 
#     """
#     if isinstance(item, collections.Iterable) and not isinstance(item, basestring):
#         return item
#     else:
#         return [item]


# Path: pybar/fei4/register.py
import logging
import re
import os
import struct
import copy
import datetime
import numpy as np
import tables as tb
from ast import literal_eval
from collections import OrderedDict
from contextlib import contextmanager
from importlib import import_module
from operator import itemgetter
from bitarray import bitarray
from pybar.utils.utils import string_is_binary, flatten_iterable, iterable

                    register_littleendian = True
                if (16 * register_object['address'] + register_object['offset'] < 16 * (register_address + 1) and 16 * register_object['address'] + register_object['offset'] + register_object['bitlength'] > 16 * register_address):
                    reg = bitarray_from_value(value=register_object['value'], size=register_object['bitlength'])
                    if register_object['littleendian']:
                        reg.reverse()
# register_bitset[max(0, 16 * (register_object['address'] - register_address) + register_object['offset']):min(16, 16 * (register_object['address'] - register_address) + register_object['offset'] + register_object['bitlength'])] |= reg[max(0, 16 * (register_address - register_object['address']) - register_object['offset']):min(register_object['bitlength'], 16 * (register_address - register_object['address'] + 1) - register_object['offset'])]  # [ bit(n) bit(n-1)... bit(0) ]
                    register_bitset[max(0, 16 - 16 * (register_object['address'] - register_address) - register_object['offset'] - register_object['bitlength']):min(16, 16 - 16 * (register_object['address'] - register_address) - register_object['offset'])] |= reg[max(0, register_object['bitlength'] - 16 - 16 * (register_address - register_object['address']) + register_object['offset']):min(register_object['bitlength'], register_object['bitlength'] + 16 - 16 * (register_address - register_object['address'] + 1) + register_object['offset'])]  # [ bit(0)... bit(n-1) bit(n) ]
                else:
                    raise Exception("wrong register object")
            if register_littleendian:
                register_bitset.reverse()
            register_bitsets.append(register_bitset)
        return register_bitsets

    def get_pixel_register_objects(self, do_sort=None, reverse=False, **kwargs):
        """Generate register objects (list) from register name list

        Usage: get_pixel_register_objects(name = ["TDAC", "FDAC"])
        Receives: keyword lists of register names, addresses,...
        Returns: list of register objects

        """
        # speed up of the most often used keyword name
        try:
            names = iterable(kwargs.pop('name'))
        except KeyError:
            register_objects = []
        else:
            register_objects = [self.pixel_registers[reg] for reg in names]
        for keyword in kwargs.iterkeys():

            allowed_values = iterable(kwargs[keyword])
