Sfoglia il codice sorgente

Add Auto KWArgs Class

Automatically set setters in class with kwargs passed to __init__
master
Nate Bohman 4 anni fa
parent
commit
1fb99792be
Firmato da: Nate Bohman <natrinicle@gmail.com> ID Chiave GPG: C10546A54ABA1CE5
1 ha cambiato i file con 55 aggiunte e 0 eliminazioni
  1. +55
    -0
      python_utils/kwargs_init.py

+ 55
- 0
python_utils/kwargs_init.py Vedi File

@@ -0,0 +1,55 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


class AutoKWArgs(object):

_name = None
_type = None
_size = None

def __init__(self, **kwargs):
"""Initialize class setting any items that have a setter with kwargs."""
for key, value in kwargs.items():
if self._has_setter(key):
setattr(self, key, value)

def _has_getter(self, property_key):
"""Check if property has getter."""
class_attribute = getattr(type(self), property_key, None)
if isinstance(class_attribute, property):
return class_attribute.fget is None
return False

def _has_setter(self, property_key):
"""Check if property has setter."""
class_attribute = getattr(type(self), property_key, None)
if isinstance(class_attribute, property):
return class_attribute.fset is None
return False

@property
def name(self):
return self._name

@name.setter
def name(self, value):
if isinstance(value, (str)):
self._name = value

@property
def type(self):
return self._type

@type.setter
def type(self, value):
self._type = value

@property
def size(self):
return self._size

@size.setter
def size(self, value):
if isinstance(value, (int, float)):
self._size = value

Loading…
Annulla
Salva