Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python deep setter

from operator import attrgetter

def deep_setter(obj, aliases: list, value):
    """
    >>> class C():
    >>>    pass
 
    >>> class B():
    >>>    c = C()

    >>> class A():
    >>>    b = B()
    >>> a = A()
    >>> deep_setter(a, ["b", "c"], "test")
    >>> a.b.c
    'test'
    """
    if not isinstance(aliases, list):
        raise TypeError("Alias must be a list")
    if len(aliases) < 1:
        raise ValueError("Alias list must be longer then 0")
    if any(["." in el for el in aliases]):
        raise ValueError("Elements can not contain dots")
    if len(aliases) > 1:
        last = aliases.pop()
        dot_view = '.'.join(aliases)
        get_penult = attrgetter(dot_view)
        setattr(get_penult(obj), last, value)
    else:
        setattr(obj, aliases[0], value)
Comment

PREVIOUS NEXT
Code Example
Python :: check package without importing it python 
Python :: how to add extra str in python?phython,add,append,insert 
Python :: adding if statements in pyhton with tuple 
Python :: if statement in python with sets 
Python :: snap python api 
Python :: django Mixed Content: The page at ' was loaded over HTTPS, but requested an insecure resource swagger 
Python :: databases not showing in odoo 13 
Python :: labelling row in python 
Python :: print poo 
Python :: What is StringIndexer , VectorIndexer, and how to use them? 
Python :: multiple channel creating command in discord.py 
Python :: What is the expected value of print to this program X = 2 Y = 3 Z = X + Y print(Y) #Z 
Python :: Finding best model using GridSearchCV 
Python :: matplotlib share colorbar 
Python :: find low and high in string 
Python :: 10.4.1.3. return Terminates Function Execution 
Python :: python scrapy 
Python :: How to count a consecutive series of positive or negative values in a column in python 
Python :: python dataset createdimension unlimited 
Python :: pylint no name in module opencv 
Python :: resizing django embed player 
Python :: py3 dict values 
Python :: url namespaces for django rest router urls 
Python :: equivalent of case_when in r in pandas 
Python :: fibonacci series python using function 
Python :: django on_delete rules 
Python :: how to execute queries with cxoracle python 
Python :: get element tag name beautfulsoup 
Python :: python seperate int into digit array 
Python :: how to visualize pytorch model filters 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =