In [1]:
import pandas as pd
import numpy as nu
from effective_python.code import VoltageResistance
from effective_python.code import BoundedResistance


def test_resistor():
    r1 = VoltageResistance(5e3)
    print(f"when voltage = {r1.voltage, r1._voltage}, current is :{r1.current}")
    r1.voltage = 10
    print(f"when voltage = {r1.voltage, r1._voltage}, current is :{r1.current}")
    
    r2 = BoundedResistance(1e3)
    r2.ohms = 5e3
    r2.voltage = 100
    print(f"when resistance = {r2.ohms} and voltage = {r2.voltage}, current is :{r2.current}")
    

if __name__ == '__main__':
    test_resistor()


when voltage = (0, 0), current is :0
when voltage = (10, 10), current is :0
when resistance = 5000.0 and voltage = 100, current is :0.02

In [25]:
import json
import logging
from effective_python.code import ToDict
from effective_python.code import JsonMixin
from effective_python.code import BinaryTree
from effective_python.code import BinaryTreeWithParent
       
logging.basicConfig(level=logging.INFO)
# tree_with_parent = BinaryTreeWithParent(10)
# tree_with_parent.left = BinaryTreeWithParent(7, parent=tree_with_parent)
# tree_with_parent.right = BinaryTreeWithParent(9, parent=tree_with_parent)
# tree_with_parent.left.right = BinaryTreeWithParent(9, parent=tree_with_parent.left)
# print(json.dumps(tree_with_parent.to_dict(), indent=True))

class DatacenterRack(ToDict, JsonMixin):
    def __init__(self, switch=None, machines=None):
        self.switch = Switch(**switch)
        self.machines = [
            Machine(**kwargs) for kwargs in machines]

        
class Switch(ToDict, JsonMixin):
    """"""
    def __init__(self, ports, speed):
        """"""
        self.ports = ports
        self.speed = speed
    
    
class Machine(ToDict, JsonMixin):
    """"""
    def __init__(self, cores, ram, disk):
        self.cores = cores
        self.ram = ram
        self.disk = disk
    pass
    
    
serialized = """{
"switch": {"ports": 5, "speed": 1e9},
"machines": [
    {"cores": 8, "ram": 32e9, "disk": 5e12},
    {"cores": 4, "ram": 16e9, "disk": 1e12},
    {"cores": 2, "ram": 8e9, "disk": 5e9}
]
}"""
deserialized = DatacenterRack.from_json(serialized)
roundtrip = deserialized.to_json()
switch_dic = {"ports": 5, "speed": 1000000000.0}
sw = Switch(**switch_dic)
# logging.info('out: %s, %s' % (sw.speed, sw.ports))
print(roundtrip)


{"switch": {"ports": 5, "speed": 1000000000.0}, "machines": [{"cores": 8, "ram": 32000000000.0, "disk": 5000000000000.0}, {"cores": 4, "ram": 16000000000.0, "disk": 1000000000000.0}, {"cores": 2, "ram": 8000000000.0, "disk": 5000000000.0}]}

In [15]:
def test_args_kwargs(arg1, arg2, arg3):
    print("arg1:", arg1)
    print("arg2:", arg2)
    print("arg3:", arg3)
args = [5, 2, 3]
kwargs = { "arg2": "two", "arg1": 5, "arg3": 3, 'arg5': 7}
test_args_kwargs(**kwargs)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-59c71b78d6e9> in <module>()
      5 args = [5, 2, 3]
      6 kwargs = { "arg2": "two", "arg1": 5, "arg3": 3, 'arg5': 7}
----> 7 test_args_kwargs(**kwargs)

TypeError: test_args_kwargs() got an unexpected keyword argument 'arg5'

In [40]:
set1 = set({'a':1,'b':2}.items())

In [73]:
class Ccls(object):
    """"""
    pass
C = Ccls()
C.P = 'P'
C.P


Out[73]:
'P'

In [57]:
def kwargs_t(a, *, b,**c):
    values = [lambda v: v.values() for v in c.values()]
    for v in values:
        print(v)
arg = {'a':1, 'b':2}
kwargs_t(1, b='b', c={"b":5, "c": 6})


<function kwargs_t.<locals>.<listcomp>.<lambda> at 0x000001D7A01F68C8>