Robot Framework
setter.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 
17 class setter:
18 
19  def __init__(self, method):
20  self.methodmethod = method
21  self.attr_nameattr_name = '_setter__' + method.__name__
22  self.__doc____doc__ = method.__doc__
23 
24  def __get__(self, instance, owner):
25  if instance is None:
26  return self
27  try:
28  return getattr(instance, self.attr_nameattr_name)
29  except AttributeError:
30  raise AttributeError(self.methodmethod.__name__)
31 
32  def __set__(self, instance, value):
33  if instance is None:
34  return
35  setattr(instance, self.attr_nameattr_name, self.methodmethod(instance, value))
36 
37 
39 
40  def __new__(cls, name, bases, dct):
41  if '__slots__' in dct:
42  slots = list(dct['__slots__'])
43  for item in dct.values():
44  if isinstance(item, setter):
45  slots.append(item.attr_name)
46  dct['__slots__'] = slots
47  return type.__new__(cls, name, bases, dct)
def __new__(cls, name, bases, dct)
Definition: setter.py:40
def __init__(self, method)
Definition: setter.py:19
def __get__(self, instance, owner)
Definition: setter.py:24
def __set__(self, instance, value)
Definition: setter.py:32