Skip to content

Commit 991f83d

Browse files
committed
Refactoring. Using f-strings
1 parent 3f40f80 commit 991f83d

13 files changed

+25
-29
lines changed

CONTACT__examples/connect_to_ng_server_via_socket_.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@
1616
PORT = int(PORT)
1717

1818

19-
post_data = """
19+
post_data = f"""
2020
<?xml version="1.0"?>
2121
<REQUEST OBJECT_CLASS="TAbonentObject" ACTION="GET_CHANGES" VERSION="0" TYPE_VERSION="I" PACK="ZLIB"
2222
INT_SOFT_ID="{INT_SOFT_ID}"
2323
POINT_CODE="{POINT_CODE}"
2424
SignOut="No"
2525
ExpectSigned="No"
2626
/>
27-
""".format(
28-
INT_SOFT_ID=INT_SOFT_ID, POINT_CODE=POINT_CODE
29-
)
27+
"""
3028

3129

3230
http_request = (
@@ -56,7 +54,7 @@
5654
sock.connect((HOST, PORT))
5755
sock.send(http_request.encode())
5856

59-
print("Socket name: {}".format(sock.getsockname()))
57+
print(f"Socket name: {sock.getsockname()}")
6058
print("\nResponse:")
6159

6260
while True:

CONTACT__examples/create_and_fill_database_from_dictionary.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def build_field_table(format_field: dict, indent=" " * 4) -> str:
6868
if field_name.upper() == "ID":
6969
field_type += " PRIMARY KEY"
7070

71-
return indent + "{} {}".format(field_name, field_type)
71+
return f"{indent}{field_name} {field_type}"
7272

7373
fields_list = [
7474
build_field_table(format_field) for format_field in format_fields_of_table
@@ -140,7 +140,7 @@ def create_table(
140140
for file_name in glob.glob("contact_dicts/*.xml"):
141141
table_name = os.path.splitext(os.path.basename(file_name))[0]
142142

143-
print("Append {} from {}".format(table_name, file_name))
143+
print(f"Append {table_name} from {file_name}")
144144

145145
root = BeautifulSoup(open(file_name, "rb"), "lxml")
146146

@@ -153,7 +153,7 @@ def create_table(
153153
sql_table_data_rows = build_sql_rows_data_table(table_name, rows_of_dict)
154154
# print(sql_table + "\n\n" + sql_table_data_rows)
155155

156-
print(" Append {} rows\n".format(len(rows_of_dict)))
156+
print(f" Append {len(rows_of_dict)} rows\n")
157157
create_table(table_name, sql_table, sql_table_data_rows)
158158

159159
# print()

CONTACT__examples/create_full_dict__GET_CHANGES_from_exported_dicts.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def bytes_to_compress_to_base64(bytes_text: bytes) -> str:
4646
bytes_xml = open(file_name, "rb").read()
4747
base64_str = bytes_to_compress_to_base64(bytes_xml)
4848

49-
child_list.append("<{0}>{1}</{0}>".format(dict_name, base64_str))
49+
child_list.append(f"<{dict_name}>{base64_str}</{dict_name}>")
5050

5151
with open(FILE_NAME_FULL_DICT, "w", encoding="utf-8") as f:
5252
f.write(HTML_PATTERN_RESPONSE__GET_CHANGES.format("".join(child_list)))
5353

5454
print()
55-
print("Write to {}".format(FILE_NAME_FULL_DICT))
55+
print(f"Write to {FILE_NAME_FULL_DICT}")

CONTACT__examples/export_dicts_from__full_dict__GET_CHANGES.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_bytes_from_base64_zlib(text_or_tag_or_bytes: Union[str, bytes, Tag]) ->
4444

4545
# Если ошибка
4646
if response["re"] != "0":
47-
print('Error text: "{}"'.format(response["err_text"]))
47+
print(f'Error text: "{response["err_text"]}"')
4848
sys.exit()
4949

5050
print("Справочник полный?:", response["full"] == "1")

CONTACT__examples/get_full_dictionary__method_GET_CHANGES.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919

2020

2121
if __name__ == "__main__":
22-
post_data = """
22+
post_data = f"""
2323
<?xml version="1.0"?>
2424
<REQUEST OBJECT_CLASS="TAbonentObject" ACTION="GET_CHANGES" VERSION="0" TYPE_VERSION="I" PACK="ZLIB"
2525
INT_SOFT_ID="{INT_SOFT_ID}"
2626
POINT_CODE="{POINT_CODE}"
2727
SignOut="No"
2828
ExpectSigned="No"
2929
/>
30-
""".format(
31-
INT_SOFT_ID=INT_SOFT_ID, POINT_CODE=POINT_CODE
32-
)
30+
"""
3331

3432
rs = requests.post(URL_NG_SERVER, data=post_data)
3533
print(rs)
@@ -38,4 +36,4 @@
3836
with open(FILE_NAME_FULL_DICT, "wb") as f:
3937
f.write(rs.content)
4038

41-
print("Write to: {}".format(FILE_NAME_FULL_DICT))
39+
print(f"Write to: {FILE_NAME_FULL_DICT}")

CONTACT__examples/reducing number row in rows dicts.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
row.decompose()
3333

3434
rows = root.select("row")
35-
print(" len {} -> {}".format(old_len, len(rows)))
35+
print(f" len {old_len} -> {len(rows)}")
3636

3737
file_name = "mini_contact_dicts/" + os.path.basename(file_name)
3838
with open(file_name, "w", encoding="cp1251") as f:
3939
f.write(str(root))
4040

41-
print(" Write to {}".format(file_name))
41+
print(f" Write to {file_name}")
4242
print()

Crypto Git Repository/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# How use without input login and password:
2020
# git clone https://username:[email protected]/username/repository.git
21-
URL_GIT = "https://{0}:{1}@github.com/{0}/{2}.git".format(LOGIN, PASSWORD, NEW_REPO)
21+
URL_GIT = f"https://{LOGIN}:{PASSWORD}@github.com/{LOGIN}/{NEW_REPO}.git"
2222

2323

2424
if PROXY:

concurrency_in_python__threading_processing/multithreading__threading__examples/example multiprocessing.dummy/urls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
for url in urls:
3333
result = urlopen(url)
3434
results.append(result)
35-
print("Single thread: {:.3f} seconds".format(time.clock() - t))
35+
print(f"Single thread: {time.clock() - t:.3f} seconds")
3636

3737
# ------- VERSUS ------- #
3838

@@ -43,7 +43,7 @@ def go(count=1):
4343
results = pool.map(urlopen, urls)
4444
# pool.close()
4545
# pool.join()
46-
print("{} Pool: {:.3f} seconds".format(count, time.clock() - t))
46+
print(f"{count} Pool: {time.clock() - t:.3f} seconds")
4747

4848

4949
# ------- 1 Pool ------- #

concurrency_in_python__threading_processing/multithreading__threading__examples/flask__threaded/threaded__with.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@app.route("/")
1515
def index():
16-
return "Current thread: {}".format(threading.current_thread())
16+
return f"Current thread: {threading.current_thread()}"
1717

1818

1919
if __name__ == "__main__":

concurrency_in_python__threading_processing/multithreading__threading__examples/sync_vs_async__with_requests_and_bs4.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def go(url):
2828
t = time.time()
2929
result = [go(url) for url in urls]
3030
print(result)
31-
print("Elapsed {:.3f} secs".format(time.time() - t))
31+
print(f"Elapsed {time.time() - t:.3f} secs")
3232
# ['Streletz', 'Kromster', 'Stepan Kasyanenko', 'Kromster', 'JamesJGoodwin']
3333
# Elapsed 6.030 secs
3434

@@ -38,6 +38,6 @@ def go(url):
3838
pool = ThreadPool()
3939
result = pool.map(go, urls)
4040
print(result)
41-
print("Elapsed {:.3f} secs".format(time.time() - t))
41+
print(f"Elapsed {time.time() - t:.3f} secs")
4242
# ['Streletz', 'Kromster', 'Stepan Kasyanenko', 'Kromster', 'JamesJGoodwin']
4343
# Elapsed 3.203 secs

concurrency_in_python__threading_processing/multithreading__threading__examples/threading_with_webservers__flask.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run(port: int = 80):
1616

1717
@app.route("/")
1818
def index():
19-
html = "Hello World! (port={})".format(port)
19+
html = f"Hello World! (port={port})"
2020
print(html)
2121

2222
return html

concurrency_in_python__threading_processing/multithreading__threading__examples/timeout_run_function.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def post_msg(self):
1616
time.sleep(3)
1717

1818
# Написать пользователю
19-
print("Hi, {}! current_thread: {}".format(self.name, current_thread()))
19+
print(f"Hi, {self.name}! current_thread: {current_thread()}")
2020

2121

2222
user_1 = User("Vasya")
@@ -30,7 +30,7 @@ def foo(name):
3030
time.sleep(5)
3131

3232
# Написать пользователю
33-
print("Hi, {}! current_thread: {}".format(name, current_thread()))
33+
print(f"Hi, {name}! current_thread: {current_thread()}")
3434

3535

3636
Thread(target=lambda: foo("Thread")).start()

converter mils - mm/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ def mm2mils(mm: float) -> float:
1818
if __name__ == "__main__":
1919
mm = 50.0
2020
mils = 1968.0
21-
print("{} mm -> {} mils".format(mm, mm2mils(mm)))
22-
print("{} mils -> {} mm".format(mils, mils2mm(mils)))
21+
print(f"{mm} mm -> {mm2mils(mm)} mils")
22+
print(f"{mils} mils -> {mils2mm(mils)} mm")

0 commit comments

Comments
 (0)