Skip to content

Commit b0a0638

Browse files
authored
Fix warnings (#350)
* fix warnings * fix warning #347 * kill mainloop more gently #218 * sort imports * remove print * remove whitespaces
1 parent d26912b commit b0a0638

18 files changed

+125
-175
lines changed

example/boolean.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
So how to change a bool value?
2+
So how to change a bool value?
33
4-
1) Read out the _bytearray your boolean values are in.
5-
2) In this byte_array you need to find the byte (byteindex)
6-
3) Find the bit in this byte (bitindex) so you can
4+
1) Read out the _bytearray your boolean values are in.
5+
2) In this byte_array you need to find the byte (byteindex)
6+
3) Find the bit in this byte (bitindex) so you can
77
4) set the correct value with utility function set_bool which does all the hard work.
88
set_bool(_bytearray = data which you read out before.., byte_index, bool_index, value),
99
5) write back the changed bytearray back to the PLC.
@@ -21,12 +21,11 @@
2121
plc = snap7.client.Client()
2222
plc.connect('192.168.200.24', 0, 3)
2323

24-
# In this example boolean in DB 31 at byte 120 and bit 5 is changed. = 120.5
24+
# In this example boolean in DB 31 at byte 120 and bit 5 is changed. = 120.5
2525

2626
reading = plc.db_read(31, 120, 1) # read 1 byte from db 31 staring from byte 120
2727
snap7.util.set_bool(reading, 0, 5) # set a value of fifth bit
28-
plc.db_write(reading, 31, 120, 1) # write back the bytearray and now the boolean value is changed
29-
# in the PLC.
28+
plc.db_write(reading, 31, 120, 1) # write back the bytearray and now the boolean value is changed in the PLC.
3029

3130
# NOTE you could also use the read_area and write_area functions.
3231
# then you can specify an area to read from:

example/example.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def close_row(row):
133133
row['CloseAut'] = 1
134134
row['OpenAut'] = 0
135135

136-
#show_row(0)
137-
#show_row(1)
136+
# show_row(0)
137+
# show_row(1)
138138

139139

140140
def open_and_close():
@@ -152,7 +152,7 @@ def open_and_close():
152152

153153

154154
def set_part_db(start, size, _bytearray):
155-
data = _bytearray[start:start+size]
155+
data = _bytearray[start:start + size]
156156
set_db_row(1, start, size, data)
157157

158158

@@ -170,7 +170,7 @@ def open_and_close_db1():
170170

171171
for x, (name, row) in enumerate(db1.index.items()):
172172
open_row(row)
173-
#set_part_db(4+x*126, 126, all_data)
173+
# set_part_db(4+x*126, 126, all_data)
174174

175175
t = time.time()
176176
write_data_db(1, all_data, 4 + 126 * 450)
@@ -180,7 +180,7 @@ def open_and_close_db1():
180180
time.sleep(5)
181181
for x, (name, row) in enumerate(db1):
182182
close_row(row)
183-
#set_part_db(4+x*126, 126, all_data)
183+
# set_part_db(4+x*126, 126, all_data)
184184

185185
print(time.time() - t)
186186

@@ -237,12 +237,12 @@ def print_open():
237237
print(row)
238238

239239

240-
#read_tank_db()
241-
#open_and_close()
242-
#open_and_close_db1()
243-
#time.sleep(1)
244-
#show_row(2)
240+
# read_tank_db()
241+
# open_and_close()
242+
# open_and_close_db1()
243+
# time.sleep(1)
244+
# show_row(2)
245245
print_tag()
246-
#print_open()
246+
# print_open()
247247

248248
client.disconnect()

example/logo_7_8.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@
1414
logger = logging.getLogger(__name__)
1515

1616
plc = snap7.logo.Logo()
17-
plc.connect("192.168.0.41",0x1000,0x2000)
17+
plc.connect("192.168.0.41", 0x1000, 0x2000)
1818

1919
if plc.get_connected():
2020
logger.info("connected")
21-
21+
2222
# read I1 from logo
23-
vm_address = ("V923.0" if Logo_7==True else "V1024.0")
24-
print (f"I1: {str(plc.read(vm_address))}")
23+
vm_address = ("V923.0" if Logo_7 else "V1024.0")
24+
print(f"I1: {str(plc.read(vm_address))}")
2525

2626
# write some values in VM addresses between 0 and 100
27-
27+
2828
value_1 = 0b10110001
2929
value_2 = 480
30-
30+
3131
print("write 0b10110001 to V10")
3232
plc.write("V10", value_1)
33-
33+
3434
print(f"read V10.0 must be 1 - check: {str(plc.read('V10.0'))}")
3535
print(f"read V10.3 must be 0 - check: {str(plc.read('V10.3'))}")
3636
print(f"read V10.7 must be 1 - check: {str(plc.read('V10.7'))}")
37-
37+
3838
print("write 480 analog value to VW20")
3939
plc.write("VW20", value_2)
40-
40+
4141
print(f"read VW20 must be 480 - check: {str(plc.read('VW20'))}")
4242

4343
print("trigger V10.2")

example/write_multi.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import ctypes
22
import snap7
3-
from snap7.types import S7WLByte, S7DataItem, S7WLWord, S7WLReal, S7WLTimer
4-
from snap7.types import areas, wordlen_to_ctypes
5-
from snap7.util import set_int, set_real, set_word, get_int, get_real, get_s5time
3+
from snap7.types import Areas, S7DataItem, S7WLWord, S7WLReal, S7WLTimer
4+
from snap7.util import set_int, set_real, get_int, get_real, get_s5time
65

76

87
client = snap7.client.Client()
@@ -34,9 +33,9 @@ def set_data_item(area, word_len, db_number: int, start: int, amount: int, data:
3433

3534
counters = 0x2999.to_bytes(2, 'big') + 0x1111.to_bytes(2, 'big')
3635

37-
item1 = set_data_item(area=areas.DB, word_len=S7WLWord, db_number=1, start=0, amount=4, data=ints)
38-
item2 = set_data_item(area=areas.DB, word_len=S7WLReal, db_number=1, start=8, amount=1, data=real)
39-
item3 = set_data_item(area=areas.TM, word_len=S7WLTimer, db_number=0, start=2, amount=2, data=counters)
36+
item1 = set_data_item(area=Areas.DB, word_len=S7WLWord, db_number=1, start=0, amount=4, data=ints)
37+
item2 = set_data_item(area=Areas.DB, word_len=S7WLReal, db_number=1, start=8, amount=1, data=real)
38+
item3 = set_data_item(area=Areas.TM, word_len=S7WLTimer, db_number=0, start=2, amount=2, data=counters)
4039

4140
items.append(item1)
4241
items.append(item2)

setup.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
ignore = E501,E402,W391,W292,W503
33

44
[mypy]
5-
ignore_missing_imports = True
5+
ignore_missing_imports = True
6+
7+
[tool:pytest]
8+
asyncio_mode = auto

snap7/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import snap7.types as types
1212
import snap7.util as util
1313

14+
__all__ = ['client', 'common', 'error', 'logo', 'server', 'types', 'util']
15+
1416
try:
1517
__version__ = pkg_resources.require("python-snap7")[0].version
1618
except pkg_resources.DistributionNotFound:

snap7/client.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
"""
22
Snap7 client used for connection to a siemens 7 server.
33
"""
4-
import logging
54
import re
6-
from ctypes import c_int, c_char_p, byref, sizeof, c_uint16, c_int32, c_byte, c_ulong, Array
7-
from ctypes import c_void_p, create_string_buffer
5+
import logging
6+
from ctypes import byref, create_string_buffer, sizeof
7+
from ctypes import Array, c_byte, c_char_p, c_int, c_int32, c_uint16, c_ulong, c_void_p
88
from datetime import datetime
9-
from typing import Union, Tuple, Optional, List
9+
from typing import List, Optional, Tuple, Union
1010

1111
import snap7
12-
from snap7.common import check_error, load_library, ipv4
12+
from snap7.common import check_error, ipv4, load_library
1313
from snap7.exceptions import Snap7Exception
14-
from snap7.types import Areas, WordLen, S7Object, buffer_type, buffer_size, BlocksList, S7CpuInfo, S7DataItem, S7SZL, S7OrderCode, \
15-
S7Protection, S7SZLList, S7CpInfo
16-
from snap7.types import TS7BlockInfo, param_types, cpu_statuses
17-
14+
from snap7.types import S7SZL, Areas, BlocksList, S7CpInfo, S7CpuInfo, S7DataItem
15+
from snap7.types import S7OrderCode, S7Protection, S7SZLList, TS7BlockInfo, WordLen
16+
from snap7.types import S7Object, buffer_size, buffer_type, cpu_statuses, param_types
1817
logger = logging.getLogger(__name__)
1918

2019

snap7/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import pathlib
55
import platform
66
from ctypes import c_char
7-
from ctypes.util import find_library
87
from typing import Optional
8+
from ctypes.util import find_library
99

1010
from snap7.exceptions import Snap7Exception
1111

snap7/logo.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
"""
22
Snap7 client used for connection to a siemens LOGO 7/8 server.
33
"""
4-
import logging
54
import re
65
import struct
7-
from ctypes import c_int, byref, c_uint16, c_int32
8-
from ctypes import c_void_p
6+
import logging
7+
from ctypes import byref, c_int, c_int32, c_uint16, c_void_p
98

109
import snap7
1110
from snap7 import types
12-
from snap7.common import check_error, load_library, ipv4
11+
from snap7.types import WordLen, S7Object, param_types
12+
from snap7.common import ipv4, check_error, load_library
1313
from snap7.exceptions import Snap7Exception
14-
from snap7.types import S7Object, WordLen
15-
from snap7.types import param_types
1614

1715
logger = logging.getLogger(__name__)
1816

snap7/partner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
can send data asynchronously. The only difference between them is the one who
88
is requesting the connection.
99
"""
10-
from ctypes import c_int32, c_uint32, byref, c_uint16, c_int, c_void_p
11-
import logging
1210
import re
11+
import logging
12+
from ctypes import byref, c_int, c_int32, c_uint32, c_void_p
1313
from typing import Tuple, Optional
1414

1515
import snap7.types
16-
from snap7.common import load_library, check_error, ipv4
16+
from snap7.common import ipv4, check_error, load_library
1717
from snap7.exceptions import Snap7Exception
1818

1919
logger = logging.getLogger(__name__)

0 commit comments

Comments
 (0)