From 2f9e202adce567c548483bdcc66e4a8dc19444a8 Mon Sep 17 00:00:00 2001 From: beacon Date: Wed, 29 Jun 2022 23:17:16 +0300 Subject: [PATCH] some changing --- test.py | 38 ++++++++++++++++---------------------- test2.py | 41 ++++++++++------------------------------- test3.py | 23 +++++++++++++++++++++++ test4.py | 19 +++++++++++++++++++ 4 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 test3.py create mode 100644 test4.py diff --git a/test.py b/test.py index 1ace354..4a7564a 100644 --- a/test.py +++ b/test.py @@ -167,30 +167,24 @@ class Commands: @staticmethod async def write_json(guild: int, param_name: str, param: str or int): - with open('prefix.json', 'r', encoding='utf-8') as _f: - _jsonObject = json.load(_f) + with open('prefix.json', 'r') as _f: try: - _guild_conf = _jsonObject[f"{guild}"] - except KeyError: - _guild_conf = {} - try: - _param = _guild_conf[f"{param_name}"] - except KeyError: - _param = {} - print(f'json is {_jsonObject}\n type {type(_jsonObject)}' - f' Guild is {guild}\n type {type(_param)}' - f' Param name is {param_name}\n type {type(_param)}' - f' param is {param} type {type(_param)}') - _new_param = {f"{param_name}": f'{param}'} - _new_guild = {f"{guild}": {}} - _jsonObject.update(_new_guild) - _guild_conf = _jsonObject[f"{guild}"] - print(f"Guild conf is: {_guild_conf}") - _param.update(_new_param) - print(f"Param is: {_param}") - _guild_conf.update(_param) + _jsonObject = json.load(_f) + except json.JSONDecodeError: + _jsonObject = {} + try: + _guild_conf = _jsonObject[f'{guild}'] + except KeyError as error: + _guild_conf = {f'{guild}': {}} + try: + _param = _guild_conf[f"{param_name}"] - with open("prefix.json", "w") as _f: + except KeyError: + _param = {f'{param_name}': f'{param}'} + + _jsonObject.update(_guild_conf) + print(_jsonObject) + with open('prefix.json', 'w') as _f: json.dump(_jsonObject, _f) _f.close() diff --git a/test2.py b/test2.py index 390c628..78b3a0b 100644 --- a/test2.py +++ b/test2.py @@ -2,37 +2,16 @@ import json def write_json(guild: int, param_name: str, param: str or int): - with open('prefix.json', 'r+', encoding='utf-8') as _f: + with open('prefix.json', 'r', 'utf-8') as _f: try: - _jsonObject = json.load(_f) + _json = json.load(_f) except json.decoder.JSONDecodeError: - _jsonObject = {} + _json = {} + _json.update({f'{guild}': {}}) + print(_json) + _guild = _json[f'{guild}'] + print(_guild) + _guild.update({f'{param_name}': f'{param}'}) + print(_json) - try: - _guild_conf = _jsonObject[f"{guild}"] - except KeyError: - _guild_conf = {f"{guild}": {}} - - print(_jsonObject) - print(_jsonObject) - - try: - _param = {f"{param_name}": f"{param}"} - except KeyError: - _param = {f"{param_name}": f"{param}"} - print(_param) - - _guild_conf.update(_param) - print(_jsonObject) - _jsonObject.update(_guild_conf) - _guild_conf = _jsonObject[f"{guild}"] - print(_jsonObject) - print(_guild_conf) - print(_param) - - with open("prefix.json", "w") as _f: - json.dump(_jsonObject, _f) - _f.close() - - -write_json(929446191270330452, 'rr', '34') +write_json(929446191270330452, 'test1', '34') diff --git a/test3.py b/test3.py new file mode 100644 index 0000000..b70dde9 --- /dev/null +++ b/test3.py @@ -0,0 +1,23 @@ +my_dict = {'key': 'value'} +print(my_dict) + +my_dict.update({'another_key': 'another_value'}) # Дополняем. +print(my_dict) +my_dict.update({'another_key': 'yet_another_value'}) # Обновляем. +print(my_dict) +my_dict.update({"subdict": {}}) +subdict = my_dict['subdict'] +print(my_dict) +print(subdict) +subdict.update({'subdictkey': 'subdictvalue'}) +print(subdict) + +print(my_dict) +subdict.update({'subdictkey2': 'subdictvalue2'}) +print(my_dict) +subdict.update({'subdictkey': '33'}) + +print(my_dict) +my_dict.update({'testKey': 'testValue'}) +print(my_dict) +print(my_dict) \ No newline at end of file diff --git a/test4.py b/test4.py new file mode 100644 index 0000000..3c1c19b --- /dev/null +++ b/test4.py @@ -0,0 +1,19 @@ +from os import walk + +f = [] +print(f'{f}\n----------------') +for filenames in walk('audio'): + f.extend(filenames) + break +print(f'{f}\n----------------') + + +f = f[2] + +print(f) +print(type(f)) + +i = 100 +print(i) +i = i * 4 +print(i)