From ff361819f93a98d9d07e2ae99b556890cb5a6968 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Thu, 11 Mar 2021 12:30:23 -0500 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/FileWith.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/FileWith.py b/tests/FileWith.py index 221189c..93e7efb 100644 --- a/tests/FileWith.py +++ b/tests/FileWith.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # Python script to Read file from JSON and convert to YAML # Author - HoneyMoose (https://www.ossez.com) From ac0a2ce5d67538357b3c189ec79341e3ed355dbd Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 12 Mar 2021 10:21:18 -0500 Subject: [PATCH 2/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 3 +++ tests/Json2Yaml.py | 26 ++++++++++++++++++++------ tests/resources/black_rock_test.json | 15 +++++---------- tests/resources/honeymoose_test.json | 15 +++++---------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/requirements.txt b/requirements.txt index df5ae38..53a6226 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,4 @@ yfinance==0.1.54 + +python-dateutil~=2.8.1 +setuptools~=54.0.0 \ No newline at end of file diff --git a/tests/Json2Yaml.py b/tests/Json2Yaml.py index b13090a..0862022 100644 --- a/tests/Json2Yaml.py +++ b/tests/Json2Yaml.py @@ -1,22 +1,36 @@ # Python script to Read file from JSON and convert to YAML -# Author - HoneyMoose (https://www.ossez.com) +# Author - yucheng.hu@insight.com +import datetime import json import ruamel.yaml as yaml +from dateutil.relativedelta import relativedelta json_filename = 'resources/black_rock_test.json' yaml_filename = 'resources/black_rock_test.yaml' +ELIGIBLE_FOR_RETIREMENT = 'eligible_for_retirement' + + +# Get Difference Years +def get_age(data_input): + date_user = datetime.datetime.strptime(data_input, '%m/%d/%Y') + date_current = datetime.datetime.now() + + time_difference = relativedelta(date_current, date_user) + return time_difference.years + + # Read and process JSON with open(json_filename) as json_file: data = json.load(json_file) - print(type(data)) + data_bod = data['date_of_birth'] - for data_dict in data: - label_id = data_dict['label_id'] - print(str(label_id)) -json_file.close() + if get_age(data_bod) >= 65: + data[ELIGIBLE_FOR_RETIREMENT] = True + else: + data[ELIGIBLE_FOR_RETIREMENT] = False # Write to YAML with open(yaml_filename, 'w') as yaml_file: diff --git a/tests/resources/black_rock_test.json b/tests/resources/black_rock_test.json index b608e34..e26a7fc 100644 --- a/tests/resources/black_rock_test.json +++ b/tests/resources/black_rock_test.json @@ -1,10 +1,5 @@ -[ - { - "label_id": 603, - "user_name": "YUCHENG-L1" - }, - { - "label_id": 604, - "user_name": "YUCHENG-L2" - } -] \ No newline at end of file +{ + "first_name": "John", + "last_name": "Doe", + "date_of_birth": "1/1/2021" +} \ No newline at end of file diff --git a/tests/resources/honeymoose_test.json b/tests/resources/honeymoose_test.json index b608e34..e26a7fc 100644 --- a/tests/resources/honeymoose_test.json +++ b/tests/resources/honeymoose_test.json @@ -1,10 +1,5 @@ -[ - { - "label_id": 603, - "user_name": "YUCHENG-L1" - }, - { - "label_id": 604, - "user_name": "YUCHENG-L2" - } -] \ No newline at end of file +{ + "first_name": "John", + "last_name": "Doe", + "date_of_birth": "1/1/2021" +} \ No newline at end of file From 46a2ec185a91e064d9e14643f53e2f1410465e60 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 12 Mar 2021 10:21:43 -0500 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/DateTime.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/DateTime.py diff --git a/tests/DateTime.py b/tests/DateTime.py new file mode 100644 index 0000000..fb58d6c --- /dev/null +++ b/tests/DateTime.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- + +# Python script to Process DateTime +# Author - yucheng.hu@ingith.com + +import datetime +import json + +import ruamel.yaml as yaml +from dateutil.relativedelta import relativedelta + +json_filename = 'resources/black_rock_test.json' +yaml_filename = 'resources/black_rock_test.yaml' + +ELIGIBLE_FOR_RETIREMENT = 'eligible_for_retirement' + + +# Get Difference Years +def get_age(data_input): + date_user = datetime.datetime.strptime(data_input, '%m/%d/%Y') + date_current = datetime.datetime.now() + + time_difference = relativedelta(date_current, date_user) + return time_difference.years + + +# Read and process JSON +with open(json_filename) as json_file: + data = json.load(json_file) + data_bod = data['date_of_birth'] + + if get_age(data_bod) >= 65: + data[ELIGIBLE_FOR_RETIREMENT] = True + else: + data[ELIGIBLE_FOR_RETIREMENT] = False + +# Write to YAML +with open(yaml_filename, 'w') as yaml_file: + yaml.dump(data, yaml_file, allow_unicode=True) From d8ab9980e2c3e167a8d523566c4b57840931dd80 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 12 Mar 2021 11:04:05 -0500 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E7=9A=84=E8=AE=A8=E8=AE=BA=E5=92=8C=E5=BB=89=E4=BB=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/appendix/01_use_cases.md | 4 +++- tests/DateTime.py | 43 ++++++++++++----------------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/docs/appendix/01_use_cases.md b/docs/appendix/01_use_cases.md index c117f5b..ff0f0ac 100644 --- a/docs/appendix/01_use_cases.md +++ b/docs/appendix/01_use_cases.md @@ -13,4 +13,6 @@ | 文章标题 | 简要介绍 | |---|---| -| [Python With 关键字和语句](https://www.ossez.com/t/python-with/13387) | 针对 with 关键字的使用进行了一些解答。 | +| [Python With 关键字和语句](https://www.ossez.com/t/python-with/13387) | 针对 with 关键字的使用进行了一些解答 | +| [Python DataTime 日期处理](https://www.ossez.com/t/python-datatime/13388) | 关于日期函数处理的一些小的介绍和讨论 | +| [Python 日期格式和时间以及当前时间和时间戳](https://www.ossez.com/t/python/13389) | 关于时间和当前时间的时间戳的获取 | diff --git a/tests/DateTime.py b/tests/DateTime.py index fb58d6c..0622a15 100644 --- a/tests/DateTime.py +++ b/tests/DateTime.py @@ -1,39 +1,24 @@ # -*- coding: utf-8 -*- - # Python script to Process DateTime -# Author - yucheng.hu@ingith.com +# Author - HoneyMoose import datetime -import json - -import ruamel.yaml as yaml -from dateutil.relativedelta import relativedelta json_filename = 'resources/black_rock_test.json' -yaml_filename = 'resources/black_rock_test.yaml' -ELIGIBLE_FOR_RETIREMENT = 'eligible_for_retirement' +today = datetime.date.today() +print("Today's date:", today) +# 从字符串创建日期对象 +date_user = datetime.datetime.strptime('1/1/2021', '%m/%d/%Y') +print("格式化后的日期对象:", date_user) -# Get Difference Years -def get_age(data_input): - date_user = datetime.datetime.strptime(data_input, '%m/%d/%Y') - date_current = datetime.datetime.now() +# 日期对象通过对格式化输出 +print("日期对象格式化输出 1 =", today.strftime("%d/%m/%Y")) +print("日期对象格式化输出 2 =", today.strftime("%B %d, %Y")) +print("日期对象格式化输出 3 =", today.strftime("%m/%d/%y")) +print("日期对象格式化输出 4 =", today.strftime("%b-%d-%Y")) - time_difference = relativedelta(date_current, date_user) - return time_difference.years - - -# Read and process JSON -with open(json_filename) as json_file: - data = json.load(json_file) - data_bod = data['date_of_birth'] - - if get_age(data_bod) >= 65: - data[ELIGIBLE_FOR_RETIREMENT] = True - else: - data[ELIGIBLE_FOR_RETIREMENT] = False - -# Write to YAML -with open(yaml_filename, 'w') as yaml_file: - yaml.dump(data, yaml_file, allow_unicode=True) +# 当前时间戳 +now = datetime.datetime.now() +print("当前时间戳:", now)