Conditions | 3 |
Total Lines | 66 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import asyncio |
||
42 | async def main(): |
||
43 | # optional: setup logging |
||
44 | logging.basicConfig(level=logging.WARN) |
||
45 | # logger = logging.getLogger("asyncua.address_space") |
||
46 | # logger.setLevel(logging.DEBUG) |
||
47 | # logger = logging.getLogger("asyncua.internal_server") |
||
48 | # logger.setLevel(logging.DEBUG) |
||
49 | # logger = logging.getLogger("asyncua.binary_server_asyncio") |
||
50 | # logger.setLevel(logging.DEBUG) |
||
51 | # logger = logging.getLogger("asyncua.uaprocessor") |
||
52 | # logger.setLevel(logging.DEBUG) |
||
53 | # logger = logging.getLogger("asyncua.subscription_service") |
||
54 | # logger.setLevel(logging.DEBUG) |
||
55 | |||
56 | # now setup our server |
||
57 | server = Server() |
||
58 | await server.init() |
||
59 | # server.set_endpoint("opc.tcp://localhost:4840/freeopcua/server/") |
||
60 | server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/") |
||
61 | server.set_server_name("FreeOpcUa Example Server") |
||
62 | |||
63 | # setup our own namespace |
||
64 | uri = "http://examples.freeopcua.github.io" |
||
65 | idx = await server.register_namespace(uri) |
||
66 | |||
67 | # get Objects node, this is where we should put our custom stuff |
||
68 | objects = server.get_objects_node() |
||
69 | |||
70 | # populating our address space |
||
71 | await objects.add_folder(idx, "myEmptyFolder") |
||
72 | myobj = await objects.add_object(idx, "MyObject") |
||
73 | myvar = await myobj.add_variable(idx, "MyVariable", 6.7) |
||
74 | await myvar.set_writable() # Set MyVariable to be writable by clients |
||
75 | myarrayvar = await myobj.add_variable(idx, "myarrayvar", [6.7, 7.9]) |
||
76 | await myobj.add_variable( |
||
77 | idx, "myStronglytTypedVariable", ua.Variant([], ua.VariantType.UInt32) |
||
78 | ) |
||
79 | await myobj.add_property(idx, "myproperty", "I am a property") |
||
80 | await myobj.add_method(idx, "mymethod", func, [ua.VariantType.Int64], [ua.VariantType.Boolean]) |
||
81 | |||
82 | inargx = ua.Argument() |
||
83 | inargx.Name = "x" |
||
84 | inargx.DataType = ua.NodeId(ua.ObjectIds.Int64) |
||
85 | inargx.ValueRank = -1 |
||
86 | inargx.ArrayDimensions = [] |
||
87 | inargx.Description = ua.LocalizedText("First number x") |
||
88 | inargy = ua.Argument() |
||
89 | inargy.Name = "y" |
||
90 | inargy.DataType = ua.NodeId(ua.ObjectIds.Int64) |
||
91 | inargy.ValueRank = -1 |
||
92 | inargy.ArrayDimensions = [] |
||
93 | inargy.Description = ua.LocalizedText("Second number y") |
||
94 | outarg = ua.Argument() |
||
95 | outarg.Name = "Result" |
||
96 | outarg.DataType = ua.NodeId(ua.ObjectIds.Int64) |
||
97 | outarg.ValueRank = -1 |
||
98 | outarg.ArrayDimensions = [] |
||
99 | outarg.Description = ua.LocalizedText("Multiplication result") |
||
100 | |||
101 | await myobj.add_method(idx, "multiply", multiply, [inargx, inargy], [outarg]) |
||
102 | await myobj.add_method(idx, "multiply_async", multiply_async, [inargx, inargy], []) |
||
103 | await myobj.add_method(idx, "func_async", func_async, [ua.VariantType.Int64], []) |
||
104 | |||
105 | async with server: |
||
106 | while True: |
||
107 | await asyncio.sleep(1) |
||
108 | |||
115 |