Same script, but without external packages:
#!/usr/bin/env python3
import urllib.request
# https://docs.python.org/3/library/dataclasses.html
from dataclasses import dataclass
from http.client import HTTPResponse
import json
@dataclass
class DocTags:
id: str
tags: [str]
def parse_line(values: [str]) -> [DocTags]:
assert len(values) == 2
doc_id: str = values[0]
tags_str: str = values[1]
tags = list(map(str.strip, tags_str.split(",")))
return DocTags(doc_id, tags)
def read_file(filename: str) -> [DocTags]:
result = []
with open(filename, 'r') as file:
while True:
line = file.readline()
if not line:
break
fields = line.split("\t")
result.append(parse_line(fields))
return result
def json_request(method: str, url: str, json_obj) -> tuple[HTTPResponse, str]:
json_str = json.dumps(json_obj)
body = bytearray(json_str, 'utf8')
req = urllib.request.Request(
url, data=body, method=method,
headers={'Content-Type': 'application/json'}
)
response = urllib.request.urlopen(req)
response_body = str(response.read(), 'utf8')
return response, response_body
def main():
docs = read_file("example.tsv")
for doc in docs:
with open(f"out/{doc.id}.txt", 'w') as file:
file.write(f"{doc}\n")
resp, body = json_request(
"PUT",
f"https://httpbin.org/anything/{doc.id}",
doc.tags,
)
file.write(f"status_code={resp.status} json={body}\n")
if __name__ == '__main__':
main()
In principle, it’s fine. Of course, technological code has appeared, but not too much.