# 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

        command_bitvector = bitarray(0, endian='little')
        if command_name not in self.commands:
            raise ValueError('Unknown command %s' % command_name)
        command_object = self.commands[command_name]
        command_parts = re.split(r'\s*[+]\s*', command_object['bitstream'])
        # for index, part in enumerate(command_parts, start = 1): # loop over command parts
        for part in command_parts:  # loop over command parts
            try:
                command_part_object = self.commands[part]
            except KeyError:
                command_part_object = None
            if command_part_object and 'bitstream'in command_part_object:  # command parts of defined content and length, e.g. Slow, ...
                if string_is_binary(command_part_object['bitstream']):
                    command_bitvector += bitarray(command_part_object['bitstream'], endian='little')
                else:
                    command_bitvector += self.build_command(part, **kwargs)
            elif command_part_object:  # Command parts with any content of defined length, e.g. ChipID, Address, ...
                if part in kwargs:
                    value = kwargs[part]
                else:
                    raise ValueError('Value of command part %s not given' % part)
                try:
                    command_bitvector += value
                except TypeError:  # value is no bitarray
                    if string_is_binary(value):
                        value = int(value, 2)
                    try:
                        command_bitvector += bitarray_from_value(value=int(value), size=command_part_object['bitlength'], fmt='I')
                    except Exception:
                        raise TypeError("Type of value not supported")

# 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

        command_bitvector = bitarray(0, endian='little')
        if command_name not in self.commands:
            raise ValueError('Unknown command %s' % command_name)
        command_object = self.commands[command_name]
        command_parts = re.split(r'\s*[+]\s*', command_object['bitstream'])
        # for index, part in enumerate(command_parts, start = 1): # loop over command parts
        for part in command_parts:  # loop over command parts
            try:
                command_part_object = self.commands[part]
            except KeyError:
                command_part_object = None
            if command_part_object and 'bitstream'in command_part_object:  # command parts of defined content and length, e.g. Slow, ...
                if string_is_binary(command_part_object['bitstream']):
                    command_bitvector += bitarray(command_part_object['bitstream'], endian='little')
                else:
                    command_bitvector += self.build_command(part, **kwargs)
            elif command_part_object:  # Command parts with any content of defined length, e.g. ChipID, Address, ...
                if part in kwargs:
                    value = kwargs[part]
                else:
                    raise ValueError('Value of command part %s not given' % part)
                try:
                    command_bitvector += value
                except TypeError:  # value is no bitarray
                    if string_is_binary(value):
                        value = int(value, 2)
                    try:
                        command_bitvector += bitarray_from_value(value=int(value), size=command_part_object['bitlength'], fmt='I')
                    except Exception:
                        raise TypeError("Type of value not supported")

            elif string_is_binary(part):
