예제 #1
0
class Pep8Checker(object):

    def __init__(self, cmd, args, cache_limit=10):
        self.cmd = cmd
        self.args = args
        self.cache_limit = cache_limit
        self.cache = OrderedDict()

    def check(self, buffer):
        """
        Return a list to check current buffer on the fly.
        """
        # Caching with a digest.
        data = '\n'.join(buffer) + '\n'
        key = md5(data).digest()
        if key in self.cache:
            return self.cache[key]
        # Dequeue the oldest cache in caching FIFO queue.
        if len(self.cache) > self.cache_limit:
            self.cache.popitem(0)
        self.cache[key] = result = self._check(data)
        return result

    def _check(self, data):
        assert isinstance(data, (unicode, str))

        # dump current data to a temp file to check on the fly.
        temp_file_fd, temp_file_path = tempfile.mkstemp()
        try:
            os.write(temp_file_fd, data)
        except:
            os.unlink(temp_file_path)
            raise
        finally:
            os.close(temp_file_fd)

        cmd = "%s %s %s" % (self.cmd, self.args, temp_file_path)

        try:
            p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
            stdout, _stderr = p.communicate()
            if p.returncode == 0 and p.returncode == 1:
                # we got any pep8 violations.
                pass
            elif p.returncode > 1:
                # TODO: notify error by other reason
                return []
        finally:
            os.unlink(temp_file_path)

        l = list()
        # Return each pep8 report
        for line in stdout.split("\n"):
            if not line.strip():
                continue
            _path, line = line.split(':', 1)
            lineno, line = line.split(':', 1)
            _columnno, description = line.split(':', 1)
            l.append((lineno, description))
        return l
예제 #2
0
    def visit(self, ast_node, compiled_children_by_node, ancestors=None):
        if ancestors is None:
            ancestors = []

        childs_ancestors = [ast_node] + ancestors
        print(dump(ast_node, include_attributes=True))
        compiled_children = OrderedDict()
        for field, value in ast.iter_fields(ast_node):
            # print("iterating:", get_name(ast_node), field)
            if isinstance(value, list):
                compiled_children[field] = [
                    self.visit(item, compiled_children_by_node,
                               childs_ancestors) for item in value
                    if isinstance(item, ast.AST)
                ]
            elif isinstance(value, ast.AST):
                compiled_children[field] = self.visit(
                    value, compiled_children_by_node, childs_ancestors)
        compile_func = getattr(compilers, "compile_" + get_name(ast_node))
        compiled_children_by_node[ast_node] = compiled_children.copy()

        # `ancestors` argument is optional
        try:
            return compile_func(ast_node, compiled_children, ancestors)
        except TypeError as e:
            if str(e).startswith("compile_") and str(e).endswith(
                    "takes 2 positional arguments but 3 were given"):
                return compile_func(ast_node, compiled_children)
            else:
                print("cannot handle error", str(e))
                raise e
        except Exception as e:
            raise e
예제 #3
0
    def show_menu(self, filtered_templates=None):
        #from collections import OrderedDict
        from ordered_dict import OrderedDict
        show_dict = {}
        if isinstance(filtered_templates, list):
            if filtered_templates:
                for temp_key in filtered_templates:
                   show_dict["template%s" % temp_key] = self.templates_info_dict["template%s" % temp_key]
        else:
            show_dict = self.templates_info_dict
        templates_info_ord = OrderedDict(sorted(show_dict.items()))
        for template, template_data in templates_info_ord.iteritems():
            sys.stdout.write("\n")
            template_up_to_date = ""
            try:
                # At some point, images could lack a hash file. Ignore if that happens
                template_hash = template_data["hash"]
                template_up_to_date = template_hash["remote_sync"]
                if template_up_to_date:
                    template_up_to_date = "[ state: %s ]" % template_up_to_date
            except:
                pass
            try:
                template_number = template_data["number"]
                template_file = template_data["template"]
                # File extensions not shown
                template_filename = template_file["filename"].split(".")[0]
                indents = self.get_indents_for_template(template_file["filename"], template_file["size"])
#                sys.stdout.write("[%s] %s%s%s" % (str(template_number), str(template_filename), indent, str(template_file["size"])))
                sys.stdout.write("[%s] %s%s%s %s%s" % (template_number, template_filename, indents[0], template_file["size"], indents[1], template_up_to_date))
            except:
                pass
예제 #4
0
파일: message.py 프로젝트: yamateh/Rammbock
class _StructuredElement(object):

    _type = None

    def __init__(self, name):
        self._name = name
        self._fields = OrderedDict()
        self._parent = None

    def __setitem__(self, name, child):
        self._fields[unicode(name)] = child
        child._parent = self

    def __getitem__(self, name):
        return self._fields[unicode(name)]

    def __getattr__(self, name):
        return self[name]

    def __delitem__(self, name):
        name = unicode(name)
        item = self._fields[name]
        del self._fields[name]
        item._parent = None

    def __str__(self):
        return self._get_name()

    def __repr__(self):
        result = '%s\n' % str(self._get_name())
        for field in self._fields.values():
            result += self._format_indented('%s' % repr(field))
        return result

    def __contains__(self, key):
        return unicode(key) in self._fields

    def _format_indented(self, text):
        return ''.join(['  %s\n' % line for line in text.splitlines()])

    @property
    def _raw(self):
        return self._get_raw_bytes()

    def _get_name(self):
        return '%s %s' % (self._type, self._name)

    def _get_raw_bytes(self):
        return ''.join((field._raw for field in self._fields.values()))

    def __len__(self):
        return sum(len(field) for field in self._fields.values())

    def __nonzero__(self):
        return True

    def _get_recursive_name(self):
        return (self._parent._get_recursive_name()
                if self._parent else '') + self._name + '.'
예제 #5
0
class _StructuredElement(object):

    _type = None

    def __init__(self, name):
        self._name = name
        self._fields = OrderedDict()
        self._parent = None

    def __setitem__(self, name, child):
        self._fields[unicode(name)] = child
        child._parent = self

    def __getitem__(self, name):
        return self._fields[unicode(name)]

    def __getattr__(self, name):
        return self[name]

    def __delitem__(self, name):
        name = unicode(name)
        item = self._fields[name]
        del self._fields[name]
        item._parent = None

    def __str__(self):
        return self._get_name()

    def __repr__(self):
        result = '%s\n' % str(self._get_name())
        for field in self._fields.values():
            result += self._format_indented('%s' % repr(field))
        return result

    def __contains__(self, key):
        return unicode(key) in self._fields

    def _format_indented(self, text):
        return ''.join(['  %s\n' % line for line in text.splitlines()])

    @property
    def _raw(self):
        return self._get_raw_bytes()

    def _get_name(self):
        return '%s %s' % (self._type, self._name)

    def _get_raw_bytes(self):
        return ''.join((field._raw for field in self._fields.values()))

    def __len__(self):
        return sum(len(field) for field in self._fields.values())

    def __nonzero__(self):
        return True

    def _get_recursive_name(self):
        return (self._parent._get_recursive_name() if self._parent else '') + self._name + '.'
예제 #6
0
def test_from_keys_with_sequences():
    d = OrderedDict.from_keys('abc')
    assert d == {'a': None, 'b': None, 'c': None}

    d = OrderedDict.from_keys(['a', 'b', 'c'])
    assert d == {'a': None, 'b': None, 'c': None}

    d = OrderedDict.from_keys(['abc', 'def'])
    assert d == {'abc': None, 'def': None}
예제 #7
0
def test_dictionaries_are_not_equal_order():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['b'] = 2
    d2['a'] = 1

    assert d1 != d2  # this is using the __str__ method.. They are diff order
예제 #8
0
def test_ordered_dict_get_item():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert_eq(1, o['a'])
    assert_eq(1, o.get('a'))
    assert_eq('asdf', o['b'])
    assert_eq('asdf', o.get('b'))
    assert_eq(None, o.get('c'))
예제 #9
0
def test_dictionaries_are_equal():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['a'] = 1
    d2['b'] = 2

    assert d1 == d2
예제 #10
0
def test_dictionaries_are_not_equal_order():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['b'] = 2
    d2['a'] = 1

    assert d1 != d2
예제 #11
0
def test_dictionaries_are_not_equal_elements():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['c'] = 1
    d2['d'] = 2

    assert d1 != d2
예제 #12
0
def test_ordered_dict_popitem():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert_eq(1, o.popitem(True))
    assert_eq(1, len(o))

    o['a'] = 1
    assert_eq(1, o.popitem())
    assert_eq(1, len(o))
예제 #13
0
def test_add_two_dicts_with_unique_keys():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['c'] = 3
    d2['d'] = 4

    d3 = d1 + d2

    assert d3 == {'a': 1, 'b': 2, 'c': 3, 'd': 4}
예제 #14
0
def test_items_method():
    d1 = OrderedDict()

    d1['a'] = 1
    d1['b'] = 2
    d1['c'] = 3
    d1['d'] = 4

    assert d1.items() == [
        ('a', 1),
        ('b', 2),
        ('c', 3),
        ('d', 4),
    ]
예제 #15
0
def test_ordered_dict_del_item():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert_eq(1, o['a'])
    assert_eq('asdf', o['b'])

    del o['a']
    assert_eq(None, o.get('a'))
    try:
        o['a']
        assert False, 'Should throw key error'
    except KeyError:
        pass
예제 #16
0
	def __init__(self, units="J", lattice_name=None, ligand_name=None):
		"""The constructor for the base ITCModel class. Child class constructors should call this parent constructor first and then probably do something else with an argument or two.
		
		Arguments
		---------
		units : string
			The units to use for energetic parameters (J, kcal, cal)
		lattice_name : string
			The name of the binding component (for binary systems)
		ligand_name : string
			The name of the bound component (for binary systems)

		Returns
		-------
		None

		""" 
		self.lattice_name = lattice_name
		self.ligand_name = ligand_name
		self.units = units
		
		self.params = OrderedDict()
		self.components = []
		self._param_meta = {}
		self._component_meta = {}
예제 #17
0
 def __init__(self, *args, **kwds):
     # begin wxGlade: MyFrame.__init__
     kwds["style"] = wx.DEFAULT_FRAME_STYLE
     wx.Frame.__init__(self, *args, **kwds)
     self.buttons = []
     self.panels = OrderedDict()
     
     self.motors_cfg = XMLRPCMotorConfiguration(self)
     self.menubar = wx.MenuBar()
     self.menubar.Append(self.motors_cfg.motormenu, self.motors_cfg.motormenu_title)
     self.SetMenuBar(self.menubar)
     
     self.abort_panel = XMLRPCAbortPanel(self, -1)
     self.panels['motor'] = XMLRPCMotorPanel(self, -1, self.motors_cfg)
     self.panels['rapidscan'] = XMLRPCRapidScanPanel(self, -1, self.motors_cfg)
     self.panels['findpeak'] = XMLRPCFindPeakPanel(self, -1, self.motors_cfg)
     self.panels['count'] = XMLRPCCountPanel(self, -1)
     # buttons to disable when moving:
     for panel_name in self.panels:
         panel = self.panels[panel_name]
         self.buttons.extend(panel.buttons)
         if hasattr(panel, 'menus') and len(panel.menus) > 0:
             for menu, title in panel.menus:
                 self.menubar.Append(menu, title)
         
     # (this specifically excludes the Abort button, which is NOT disabled)
     #self.plot_panel = PlotPanel(self, -1)      
     self.__set_properties()
     self.__do_layout()
def ordered_unbunchify(x):
    """ Recursively converts a OrderedBunch into a dictionary.
        
        >>> b = OrderedBunch(foo=OrderedBunch(lol=True), hello=42, ponies='are pretty!')
        >>> ordered_unbunchify(b)
        {'ponies': 'are pretty!', 'foo': {'lol': True}, 'hello': 42}
        
        ordered_unbunchify will handle intermediary dicts, lists and tuples (as well as
        their subclasses), but ymmv on custom datatypes.
        
        >>> b = OrderedBunch(foo=['bar', OrderedBunch(lol=True)], hello=42, 
        ...         ponies=('are pretty!', OrderedBunch(lies='are trouble!')))
        >>> ordered_unbunchify(b) #doctest: +NORMALIZE_WHITESPACE
        {'ponies': ('are pretty!', {'lies': 'are trouble!'}), 
         'foo': ['bar', {'lol': True}], 'hello': 42}
        
        nb. As dicts are not hashable, they cannot be nested in sets/frozensets.
    """
    if isinstance(x, OrderedDict):
        return OrderedDict(
            (k, ordered_unbunchify(v)) for k, v in x.iteritems())
    elif isinstance(x, dict):
        return dict((k, ordered_unbunchify(v)) for k, v in x.iteritems())
    elif isinstance(x, (list, tuple)):
        return type(x)(ordered_unbunchify(v) for v in x)
    else:
        return x
예제 #19
0
def test_add_two_dicts_with_repeated_keys():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['c'] = 3
    d2['a'] = 4  # repeated

    d3 = d1 + d2

    assert d3 == {
        'a': 4,  # updated
        'b': 2,
        'c': 3
    }
예제 #20
0
def test_get_items_with_existent_items():
    d = OrderedDict()

    d['language'] = 'Python'
    d['other'] = ('a', 'b', 'c')
    assert d['language'] == 'Python'
    assert d['other'] == ('a', 'b', 'c')
예제 #21
0
 def __recursiveUpdate(left, right):
     for key, value in right.iteritems():
         if isinstance(value, dict):
             tmp = __recursiveUpdate(left.get(key, OrderedDict()), value)
             left[key] = tmp
         else:
             left[key] = right[key]
     return left
예제 #22
0
def test_get_items_with_missing_items():
    d = OrderedDict()

    d['language'] = 'Python'

    assert d['language'] == 'Python'
    with pytest.raises(KeyError):
        d['other-key']
예제 #23
0
def test_set_repeated_items():
    d = OrderedDict()
    assert d.keys() == []
    assert d.values() == []

    d['language'] = 'Python'
    assert d.keys() == ['language']
    assert d.values() == ['Python']

    d['other'] = ('a', 'b', 'c')
    assert d.keys() == ['language', 'other']
    assert d.values() == ['Python', ('a', 'b', 'c')]

    # Same key again:
    d['language'] = 'Ruby'
    assert d.keys() == ['language', 'other']
    assert d.values() == ['Ruby', ('a', 'b', 'c')]
예제 #24
0
def test_membership():
    d = OrderedDict()

    d['language'] = 'Python'
    d['other'] = ('a', 'b', 'c')
    assert 'language' in d
    assert 'other' in d

    assert 'not-found' not in d
예제 #25
0
        def get_dict(source, offset, size, original):
            #assert is_md5sum(source)
            assert offset >= 0
            assert size >= 0
            assert type(original) == bool

            return OrderedDict([("source", source), ("offset", offset),
                                ("size", size), ("original", original),
                                ("repeat", 1)])
예제 #26
0
def readDictionary(file):
    """ read csv file and parse it to dictionary """

    # define local function to parse last token in proper Python types
    def __parseToken(token):

        return {
            's': lambda x: x,
            'i': lambda x: int(x),
            'd': lambda x: float(x),
            'f': lambda x: __findFunction(x),
            't': lambda x: tuple(map(__parseToken, x.split("|"))),
            'b': lambda x: bool(x),
            'n': lambda x: None
        }[token[0]](token[1:])

    def __findFunction(string):
        # does not allow for nested functions right now
        # split name into parts
        string = string.split(".")
        # import module
        module = __import__(string[0], globals(), locals(), [], -1)
        # return function object
        return module.__dict__[string[1]]

    # recursively merge dictionary right into dictionary left
    def __recursiveUpdate(left, right):
        for key, value in right.iteritems():
            if isinstance(value, dict):
                tmp = __recursiveUpdate(left.get(key, OrderedDict()), value)
                left[key] = tmp
            else:
                left[key] = right[key]
        return left

    # create dictionary for output
    out_dict = OrderedDict()

    # open file and iterate over all lines expects empty ones
    f = open(file, 'r')
    for line in f:
        if (line != "\n") and (not line.startswith("#")):

            # split line into segments
            items = line.strip().split("||")

            # parse last item
            tmp_dict = __parseToken(items[-1])

            # create hierachy
            for item in items[:-1:][::-1]:
                tmp_dict = {item: tmp_dict}

            # merge temporaly built dictionary into output dictionary
            __recursiveUpdate(out_dict, tmp_dict)
    return out_dict
예제 #27
0
class MessageSequence(object):
    def __init__(self):
        self.operators = OrderedDict()
        self.sequence = []

    def _operator(self, name, ip, port):
        ip_port = ip_name(ip, port)
        if ip_port not in self.operators:
            self.operators[ip_port] = Operator(ip_port, name)
        else:
            self.operators[ip_port].name = name
        return self.operators[ip_port]

    def _get_operator(self, ip_port):
        if ip_port not in self.operators:
            self.operators[ip_port] = Operator(ip_port)
        return self.operators[ip_port]

    def send(self, sender_name, sender, receiver, protocol, message_name, error=""):
        self.sequence.append(
            (
                self._operator(sender_name, *sender),
                self._get_operator(ip_name(*receiver)),
                msg_name(protocol, message_name),
                error,
                "sent",
            )
        )

    def receive(self, receiver_name, receiver, sender, protocol, message_name, error=""):
        sender_ip_name = ip_name(*sender)
        row = (
            self._get_operator(sender_ip_name),
            self._operator(receiver_name, *receiver),
            msg_name(protocol, message_name),
            error,
            "received",
        )
        if self.is_named_operator(sender_ip_name):
            for i in reversed(range(len(self.sequence))):
                if self._matches(self.sequence[i], receiver, sender):
                    self.sequence[i] = row
                    return
        self.sequence.append(row)

    def _matches(self, msg, receiver, sender):
        return msg[0].ip_name == ip_name(*sender) and msg[1].ip_name == ip_name(*receiver) and msg[-1] == "sent"

    def get_operators(self):
        return (operator.name for operator in self.operators.values())

    def get(self):
        return ((str(elem) for elem in row) for row in self.sequence)

    def is_named_operator(self, ip_name):
        return ip_name in self.operators and ip_name != self.operators[ip_name].name
예제 #28
0
def sorted_dicts(): #Sort dictionarys by names
  global states, events, callbacks
  states = OrderedDict(sorted(states.items(), key=lambda t: t[0]))
  events = OrderedDict(sorted(events.items(), key=lambda t: t[0]))
  callbacks = OrderedDict(sorted(callbacks.items(), key=lambda t: t[0]))

  for state in states.keys():
    states[state] = OrderedDict(sorted(states[state].items(), key=lambda t: t[0]))
예제 #29
0
    def analyzeDataSet(self, state, data):
        timestamp = data[state['mainid']][state['subid']]["timestamp"]

        try:
            value = data[state['mainid']][state['subid']][self.field]

            if self.differential_mode:
                if state['last_value'] is None:
                    state['last_value'] = data[state['mainid']][
                        state['subid']][self.field]
                    return
                t = value - state['last_value']
            else:
                t = value

        except KeyError:
            return (self.name, state['mainid'], state['subid'], "KeyError",
                    timestamp, timestamp, "%s not in data" % self.field,
                    str(sys.exc_info()))

        except TypeError:
            return (self.name, state['mainid'], state['subid'], "TypeError",
                    timestamp, timestamp, "%s not in data" % self.field,
                    str(sys.exc_info()))

        state['st'] = self.p_st * t + (1 - self.p_st) * state['st']
        state['ewmv'] = self.p_ewmv * (t - state['st'])**2 + (
            1 - self.p_ewmv) * state['ewmv']

        lower_bound = state['st'] - self.L * math.sqrt(
            state['ewmv'] * self.p_st / (2 - self.p_st))
        upper_bound = state['st'] + self.L * math.sqrt(
            state['ewmv'] * self.p_st / (2 - self.p_st))

        parameterdump = OrderedDict([("mainid", state['mainid']),
                                     ("subid", state['subid']),
                                     ("last_value", state['last_value']),
                                     ("L", self.L), ("st", state['st']),
                                     ("p_st", self.p_st),
                                     ("ewmv", state['ewmv']),
                                     ("p_ewmv", self.p_ewmv), ("value", value),
                                     ("t", t), ("lower_bound", lower_bound),
                                     ("upper_bound", upper_bound)])

        state['last_value'] = data[state['mainid']][state['subid']][self.field]

        # print >> sys.stderr, parameterdump

        if lower_bound - t > 6e-14 and lower_bound - t > self.tolerance:
            return (self.name, state['mainid'], state['subid'], "LowValue",
                    timestamp, timestamp, "%s < %s" % (t, lower_bound),
                    str(parameterdump))
        if t - upper_bound > 6e-14 and t - upper_bound > self.tolerance:
            return (self.name, state['mainid'], state['subid'], "HighValue",
                    timestamp, timestamp, "%s > %s" % (t, upper_bound),
                    str(parameterdump))
예제 #30
0
def test_count_items():
    d = OrderedDict()

    assert len(d) == 0

    d['language'] = 'Python'
    assert len(d) == 1

    d['other'] = ('a', 'b', 'c')
    assert len(d) == 2
예제 #31
0
def test_set_new_items():
    d = OrderedDict()
    assert d.keys() == []
    assert d.values() == []

    d['language'] = 'Python'
    assert d.keys() == ['language']
    assert d.values() == ['Python']

    d['other'] = ('a', 'b', 'c')
    assert d.keys() == ['language', 'other']
    assert d.values() == ['Python', ('a', 'b', 'c')]
예제 #32
0
 def get_recipe(self):
     assert self.closed
     if self.recipe == None:
         self.recipe = OrderedDict([("md5sum", self.md5summer.hexdigest()),
                                    ("size", len(self.tail_buffer)),
                                    ("method", "concat"),
                                    ("pieces", list(self.__seq2rec()))])
         # We now have a complete and useful recipe. But can it be improved?
         self.__polish_recipe_tail()
         self.__polish_recipe_repeats()
     return self.recipe
예제 #33
0
def merge_plain_and_compiled_fields(node, compiled_children):
    result = OrderedDict()
    # print("in merge_plain_and_compiled_fields......")
    # print(compiled_children)
    for name, field in ast.iter_fields(node):
        # print(name)
        if name in compiled_children:
            result[name] = compiled_children[name]
        else:
            result[name] = field
    return result
예제 #34
0
파일: message.py 프로젝트: yamateh/Rammbock
class Bag(_StructuredElement):

    _type = 'Bag'

    def __init__(self, name):
        self._name = name
        self._fields = OrderedDict()
        self._parent = None

    @property
    def len(self):
        return sum(field.len for field in self._fields.values())
예제 #35
0
파일: message.py 프로젝트: yamateh/Rammbock
class Struct(_StructuredElement):

    _type = 'Struct'

    def __init__(self, name, type_name, align=1):
        self._name = name
        self._type = type_name
        self._fields = OrderedDict()
        self._parent = None
        self._align = align

    def __len__(self):
        result = sum(len(field) for field in self._fields.values())
        return self._get_aligned(result)

    def _get_aligned(self, length):
        return length + (self._align - length % self._align) % self._align

    def _get_raw_bytes(self):
        result = ''.join((field._raw for field in self._fields.values()))
        return result.ljust(self._get_aligned(len(result)), '\x00')
예제 #36
0
class Bag(_StructuredElement):

    _type = 'Bag'

    def __init__(self, name):
        self._name = name
        self._fields = OrderedDict()
        self._parent = None

    @property
    def len(self):
        return sum(field.len for field in self._fields.values())
예제 #37
0
class Struct(_StructuredElement):

    _type = 'Struct'

    def __init__(self, name, type_name, align=1):
        self._name = name
        self._type = type_name
        self._fields = OrderedDict()
        self._parent = None
        self._align = align

    def __len__(self):
        result = sum(len(field) for field in self._fields.values())
        return self._get_aligned(result)

    def _get_aligned(self, length):
        return length + (self._align - length % self._align) % self._align

    def _get_raw_bytes(self):
        result = ''.join((field._raw for field in self._fields.values()))
        return result.ljust(self._get_aligned(len(result)), '\x00')
예제 #38
0
 def compile(self):
     print("...in compile() of ", self.name, self.field_name)
     if self.name is not None:
         method = getattr(compilers, "compile_" + self.name)
         if self.is_leaf():
             print("compiling", self.name)
             return method(node=self, compiled_children=OrderedDict())
         return str(method(
             node=self,
             compiled_children=OrderedDict(
                 (child.name, child.compile())
                 for child in self.children()
             )
         ))
     # else:
     # This instance is a pseudo node to increase hierarchy
     # e.g. function_def -> body is a list of nodes
     # so these nodes cannot be direct children of function_def.
     # body itself is no ast node.
     # In this case we return a list instead of a string.
     return compilers.compile_list(self.children())
예제 #39
0
def create_fieldDict(backend, convert_generic, convert_snmp, csv_file):
    field_map = readDictionary(
        os.path.join(os.path.dirname(__file__), '..', 'config', csv_file))
    fieldDict = OrderedDict()

    for key, dictionary in field_map.iteritems():
        name = dictionary['name']
        table = dictionary['table']
        if (not 'only' in dictionary) or (dictionary['only'] == backend):
            if not table in fieldDict:
                fieldDict[table] = OrderedDict()

            if 'use_type' in dictionary:
                if dictionary['use_type'] == 'predef_value':
                    fieldDict[table][name] = dictionary['value']
                else:
                    fieldDict[table][name] = convert_generic(
                        dictionary['use_type'])
            else:
                fieldDict[table][name] = convert_snmp(dictionary['snmp_type'])
    return fieldDict
예제 #40
0
 def _add_header(self, header):
     new = OrderedDict({'_header': header})
     new.update(self._fields)
     self._fields = new
예제 #41
0
 def __init__(self, name):
     self._name = name
     self._fields = OrderedDict()
     self._parent = None
예제 #42
0
class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.buttons = []
        self.panels = OrderedDict()
        
        self.motors_cfg = XMLRPCMotorConfiguration(self)
        self.menubar = wx.MenuBar()
        self.menubar.Append(self.motors_cfg.motormenu, self.motors_cfg.motormenu_title)
        self.SetMenuBar(self.menubar)
        
        self.abort_panel = XMLRPCAbortPanel(self, -1)
        self.panels['motor'] = XMLRPCMotorPanel(self, -1, self.motors_cfg)
        self.panels['rapidscan'] = XMLRPCRapidScanPanel(self, -1, self.motors_cfg)
        self.panels['findpeak'] = XMLRPCFindPeakPanel(self, -1, self.motors_cfg)
        self.panels['count'] = XMLRPCCountPanel(self, -1)
        # buttons to disable when moving:
        for panel_name in self.panels:
            panel = self.panels[panel_name]
            self.buttons.extend(panel.buttons)
            if hasattr(panel, 'menus') and len(panel.menus) > 0:
                for menu, title in panel.menus:
                    self.menubar.Append(menu, title)
            
        # (this specifically excludes the Abort button, which is NOT disabled)
        #self.plot_panel = PlotPanel(self, -1)      
        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("Motor Control Panel")
        # end wxGlade        
    
    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        for panel in self.panels.values():
            sizer_2.Add(wx.StaticLine(self, -1), 0, wx.EXPAND)
            sizer_2.Add(panel, 0, wx.EXPAND, 0)
        #sizer_1.Add(self.panel_1, 0, wx.EXPAND, 0)
        sizer_1.Add(sizer_2)
        sizer_1.Add(self.abort_panel, 0, wx.EXPAND, 0)
        #sizer_2.Add(sizer_1)
        #rs_label = wx.StaticText(self, -1, 'RAPID SCAN:')
        #rs_label.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
        #sizer_2.Add(rs_label, 0,0,0)
        #sizer_2.Add(self.panel_2, 0, wx.EXPAND, 0)
        #sizer_2.Add(self.panel_3, 0, wx.EXPAND, 0)
        #sizer_1.Add(self.plot_panel, 0, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade

    def disable_all_buttons(self):
        """ disables all buttons except the abort button """
        old_button_state = []
        for button in self.buttons:
            old_button_state.append(button.IsEnabled())
            button.Disable()
        self._stored_button_state = old_button_state
        return old_button_state
    
    def restore_button_state(self, button_state=None):
        for panel in self.panels.values():
            panel.update_all_motors()
        if button_state==None: button_state = self._stored_button_state
        for i, state in enumerate(button_state):
            self.buttons[i].Enable(state)
    
    def update_motors(self, motors):
        for panel in self.panels.values():
            panel.motors = motors
            panel.update_all_motors()
            
    def change_enabled_motors(self, motors_enabled):
        for panel in self.panels.values():
            panel.motors_enabled = motors_enabled
            panel.__do_layout()     
예제 #43
0
 def __init__(self, cmd, cache_limit=10):
     self.cmd = cmd
     self.cache_limit = cache_limit
     self.cache = OrderedDict()
예제 #44
0
class Pep8Checker(object):

    def __init__(self, cmd, cache_limit=10):
        self.cmd = cmd
        self.cache_limit = cache_limit
        self.cache = OrderedDict()

    def check(self, buffer):
        """
        Return a list to check current buffer on the fly.
        """
        # Caching with a digest.
        data = '\n'.join(buffer) + '\n'
        key = md5(data).digest()
        if key in self.cache:
            return self.cache[key]
        # Dequeue the oldest cache in caching FIFO queue.
        if len(self.cache) > self.cache_limit:
            self.cache.popitem(0)
        self.cache[key] = result = self._check(data)
        return result

    def _check(self, data):
        assert isinstance(data, (unicode, str))

        # dump current data to a temp file to check on the fly.
        temp_file_fd, temp_file_path = tempfile.mkstemp()
        try:
            os.write(temp_file_fd, data)
        except:
            os.unlink(temp_file_path)
            raise
        finally:
            os.close(temp_file_fd)

        # Because the call function requires real file, we make a temp file.
        # Otherwise we could use StringIO as a file.
        stdout_file_fd, stdout_file_path = tempfile.mkstemp()
        try:
            # os.fdopen may close then re-open the file descriptor.
            stdout = os.fdopen(stdout_file_fd, 'rw')
        except:
            os.unlink(temp_file_path)
            os.unlink(stdout_file_path)
            raise

        cmd = "%s %s" % (self.cmd, temp_file_path)

        try:
            code = subprocess.call(cmd, shell=True, stdout=stdout)
            if code == 0:
                # no pep8 violation
                return []
            elif code == 1:
                stdout.seek(0)
                result = StringIO(stdout.read())
            elif code > 1:
                # TODO: notify error by other reason
                return []
        finally:
            stdout.close()
            os.unlink(temp_file_path)
            os.unlink(stdout_file_path)

        l = list()
        # Return each pep8 report
        for line in result:
            _path, line = line.split(':', 1)
            lineno, line = line.split(':', 1)
            _columnno, description = line.split(':', 1)
            l.append((lineno, description))
        return l
예제 #45
0
 def __init__(self):
     self.operators = OrderedDict()
     self.sequence = []
예제 #46
0
 def __init__(self, name, type_name, align=1):
     self._name = name
     self._type = type_name
     self._fields = OrderedDict()
     self._parent = None
     self._align = align
예제 #47
0
class ITCModel():
	"""A base class that serves as the foundation for generation of per-injection binding enthalpies
	
	Attributes
	----------
	units : string
		The units to report binding enthalpies in. Note that internally, all calculations should be performed in SI units (e.g. Joules)
	params : ordered dict of strings
		The parameters of the model
	components : list of strings
		The names of the components that are involved in the binding model (e.g. the lattice/macromolecule and ligand)
	_param_meta : dict of tuples
		A storage variable that contains the current value and meta information for each model parameter
	_component_meta : dict of tuples
		A storage variable that contains information for each model component
	lattice_name : string
		The name of the binding component (for binary systems)
	ligand_name : string
		The name of the bound component (for binary systems)

	Notes
	-----
		Parameter types can be "n" (stoichiometry), "k" (a kinetic constant, not currently used), "dG" (change in free energy), "dH" (change in enthalpy), "dS" (change in entropy), or "dCp" (change in heat capacity)
		By convention, if lattice_name and ligand_name are not provided, they'll be set to the first and second (respectively) components set by add_component()
	"""
	
	def __init__(self, units="J", lattice_name=None, ligand_name=None):
		"""The constructor for the base ITCModel class. Child class constructors should call this parent constructor first and then probably do something else with an argument or two.
		
		Arguments
		---------
		units : string
			The units to use for energetic parameters (J, kcal, cal)
		lattice_name : string
			The name of the binding component (for binary systems)
		ligand_name : string
			The name of the bound component (for binary systems)

		Returns
		-------
		None

		""" 
		self.lattice_name = lattice_name
		self.ligand_name = ligand_name
		self.units = units
		
		self.params = OrderedDict()
		self.components = []
		self._param_meta = {}
		self._component_meta = {}
		
	def add_parameter(self, name, type, bounds=[None,None], default=0.0, linked='', description=''):
		"""Register a model parameter.
		
		Arguments
		---------
		name : string
			The name of the model parameter. Should be descriptive, like "dG1" (e.g. the delta G(ibbs) energy for binding mode 1).
		type : string
			The type, i.e. energy, stoichiometry, etc. See class notes.
		bound : tuple
			The high and low boundaries for the parameter. Depending on the fitting mode, these may or not be enforced.
		default : float
			The default value for the parameter
		linked : string
			(Not used yet)
		description: string
			A description of the model parameter. Only ever reported to the user for their information.
			
		Returns
		-------
		None
		"""
				
		assert name not in self.params.keys()
		assert type in ('n','k','dG','dH','dS','dCp')
		self.params[name] = default
		self._param_meta[name]	= (default,type,bounds,description,linked,type in ('dG','dH','dS','dCp'))
	
	def add_component(self, name, description=''):
		"""Register a model component.
		
		Arguments
		---------
		name : string
			The name of the model component (e.g. "Lattice", "Macromolecule", or "Ligand")
		description : string
			A description of the model component. Only ever reported to the user for their information.
		
		Returns
		-------
		None
		"""

		if self.lattice_name is None:
			self.lattice_name = name
		elif self.ligand_name is None:
			self.ligand_name = name

		self.components.append(name)
		self._component_meta[name] = (description)
	
	# used if we need any special startup or shutdown procedures
	def start(self):
		"""Class method stub for any necessary setup calls the model needs (scratch space, etc.). Actual models will need to overwrite this only if necessary.
		
		Arguments
		---------
		None
		
		Returns
		-------
		None
		"""
		pass
		
	def stop(self):
		"""Class method stub for any necessary shutdown calls the model needs (clearing scratch space, etc.). Actual models will need to overwrite this only if necessary.
		
		Arguments
		---------
		None
		
		Returns
		-------
		None
		"""
		pass
						
	# setters
	def set_units(self,units):
		"""Set the units the model will A) receive parameter values, and B) return dQ heats.
		
		Arguments
		---------
		units : string
			The units to use for model parameters and dQ heats
		
		Returns
		-------
		None
			
		Notes
		-----
			All internal calculations are still to be done in SI units (Joules)!
		"""
		
		assert units in _UNITS
				
		self.units = units		

	def set_param(self, name, value ):
		"""Set the value of the single named parameter.
		
		Arguments
		---------
		name : string
			The name of the model parameter
		value : float
			The value to set the named model parameter too
		
		Notes
		-----
			If the model parameter is an energy, it will be automatically converted to Joules for storage from whatever units are currently specified
		"""
				
		if self._param_meta[name][5]:
			self.params[name] = convert_to_J(self.units,value)
		else:
			self.params[name] = value

	def set_params(self, *args, **kwargs):
		"""Set the values of multiple parameters.
		
		Arguments
		---------
		*args
			Set the values of the model parameters in the order as returned by get_param_names().
		**kwargs
			Set the values of the named model parameters.
			
		Returns
		-------
		None
			
		Notes
		-----
			To prevent confusion, when setting parameter values positionally, all model values must be specified (in the correct order of course).
		"""
		
		for i in xrange(len(args)):
			assert len(args) == len(self.params)
			self.set_param( self.params.keys()[i], args[i] )
		for k,v in kwargs.iteritems():
			self.set_param( k, v )

	# getters
	def get_params(self,units=None):
		"""Return the value of all model parameters.
		
		Arguments
		---------
		units : string
			Use the specified units. If omitted, use the units already specified by the model (if applicable).
		
		Returns
		-------
		OrderedDict
			Parameter name-keyed dict of values.
		"""
		return OrderedDict( (name,self.get_param(name,units)) for name in self.params.keys() )
		
	def get_param(self,name,units=None):
		"""Return the value of the specified model parameter.
		
		Arguments
		---------
		name : string
			The name of the parameter to return.
		units : string
			Use the specified units. If omitted, use the units already specified by the model (if applicable).
		
		Returns
		-------
		float
			The value of the parameter.
		"""
			
		if not self._param_meta[name][5]:
			return self.params[name]
		if units:
			return convert_from_J(units,self.params[name])
		return convert_from_J(self.units,self.params[name])
		
	def get_param_names(self):
		"""Return a list of the parameter names used by the model.
		
		Arguments
		---------
		None
		
		Returns
		-------
		list of strings
			The names of the model parameters.
		"""
		
		return self.params.keys()
		
	def get_component_names(self):
		"""Return a list of the component names present in the model.
		
		Arguments
		---------
		None
		
		Returns
		-------
		list of strings
			The names of the model components.
		"""
		return self.components.keys()

	def get_param_type(self,name):
		"""Return the type of the named model parameter.
		
		Arguments
		---------
		name : string
			Name of the model parameter.
		
		Returns
		-------
		string
			The type of the requested parameter (e.g. "n", "k", "dG", "dH", or "dCp"). See class notes.
		"""
		return self._param_meta[name][1]

	def get_param_bounds(self,name):
		"""Return the boundaries for the named model parameter.
		
		Arguments
		---------
		name : string
			Name of the model parameter.
		
		Returns
		-------
		tuple of floats
			The high and low boundaries (if specified, otherwise they are None) for the requested model parameter.
		"""
		return self._param_meta[name][2]

	def get_param_description(self,name):
		"""Return the description for the named model parameter.
		
		Arguments
		---------
		name : string
			Name of the model parameter.
		
		Returns
		-------
		string
			The description of the model parameter previously set by add_parameter().
		"""
		return self._param_meta[name][3]
									
	def __str__(self):
		"""Stringifies the model and its current state.
		
		Arguments
		---------
		None
		
		Returns
		-------
		string
			A user-interpretable string that describes the parameters of the model, their values, and other info.
		"""
		
		types = {'n':"Stoichiometry",'k':"Rate constant",'dG':"Free energy",'dH':"Enthalpy",'dS':"Entropy",'dCp':"Heat capacity"}
		units = {'n':"sites",'k':"1/s or 1/s/mol",'dG':"%s/mol"%self.units,'dH':"%s/mol"%self.units,'dS':"%s/mol/K"%self.units,'dCp':"%s/mol/K"%self.units}
		ret = "\nModel: %s.%s\n\nDescription:\n%s\n"%(self.__module__,self.__class__.__name__, self.__doc__)
		ret+= "\nComponents:\n"
		ret+= "Index	Name	Description\n"
		for i,c in enumerate(self.components):
			ret+="%i)\t%s\t%s\n"%(i+1,c,self._component_meta[c])
		ret+= "\nParameters:\n"
		ret+= "Index	Param     Type                Value               Description\n"
		for i,k in enumerate(self.params):
			ret+="%i)\t%s%s%s%s\n" % (
				i+1,
				k.ljust(10),
				types[self.get_param_type(k)].ljust(20),
				(("%.3f"%self.get_param(k))+" "+units[self.get_param_type(k)]).ljust(20),
				self._param_meta[k][3]
				)

		return ret

	def Q(self,T0,T,concentrations): # return the enthalpy at each set of component concentrations
		"""Return the total binding heat at each injection predicted by the model and its current parameter values. This is just a stub, and child classes should implement their own calculations of Q.
		
		Arguments
		---------
		T0 : float
			The reference temperature to be used for the model (used in temperature-dependent dG, dH, dCp).
		T : float
			The temperature the titration was performed at.
		concentrations : list of dicts
			The concentration of components at each injection point.
			
		Returns
		-------
		list of floats
			The total heat in the system at each injection point.
		"""
		raise NotImplementedError("Valid ITC models should implement this!")