In [29]:
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 = 10
print(f"when resistance = {r2.ohms} and voltage = {r2.voltage}, current is :{r2.current}")
if __name__ == '__main__':
test_resistor()
In [47]:
import json
from effective_python.code import ToDict
from effective_python.code import JsonMixin
from effective_python.code import BinaryTree
from effective_python.code import BinaryTreeWithParent
# 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)
Out[47]:
In [86]:
def kwargs_t(a, b):
print(a, b)
pass
f = kwargs_t()
In [2]:
set1 = set({'a':1,'b':2}.items())
def kwargs_t(mess, **kwargs): for k, v in kwargs: print(mess,k,v)
kwargs_t('hello', {'a':1, 'b':2})