[英]Python to extract output from attribute
我試圖從我在 python 的幫助下獲得的屬性中提取像authored_date
這樣的特定細節。
我的最終目標是什么:
我想提取名為tobedeleted_branch1
、 tobedeleted_branch2
的特定分支,如果authored_date
超過 7 天,則在我的腳本的幫助下將它們刪除。
我是初學者,目前正在學習。
所以,我想做的是,
從輸出中提取創作日期並檢查它是否早於 7 天。 如果它超過 7 天,我將繼續執行我想在 if 條件下執行的任何操作。
import gitlab, os
#from gitlab.v4.objects import *
# authenticate
TOKEN = "MYTOKEN"
GITLAB_HOST = 'MYINSTANCE' # or your instance
gl = gitlab.Gitlab(GITLAB_HOST, private_token=TOKEN)
# set gitlab group id
group_id = 6
group = gl.groups.get(group_id, lazy=True)
#get all projects
projects = group.projects.list(include_subgroups=True, all=True)
#get all project ids
project_ids = []
for project in projects:
# project_ids.append((project.path_with_namespace, project.id, project.name ))
project_ids.append((project.id))
print(project_ids)
for project in project_ids:
project = gl.projects.get(project)
branches = project.branches.list()
for branch in branches:
if "tobedeleted" in branch.attributes['name']:
print(branch.attributes['name'])
#print(branch)
print(branch.attributes['commit'])
#branch.delete()
我從打印print(branch.attributes['commit'])
得到的輸出是這樣的:
{'id': '1245829930', 'short_id': '124582', 'created_at': '2021-11-15T09:10:26.000+00:00', 'parent_ids': None, 'title': 'branch name commit' into \'master\'"', 'message': 'branch name commit', 'author_name': 'Administrator', 'author_email': 'someemail@gmail.com', 'authored_date': '2021-11-15T09:10:26.000+00:00', 'committer_name': 'Administrator', 'committer_email': 'someemail@gmail.com', 'committed_date': '2021-11-15T09:10:26.000+00:00', 'trailers': None, 'web_url': 'someweburl'}
從上面的輸出中,我想提取“authored_date”並檢查它是否超過 7 天,我將繼續刪除合並的分支。
任何有關這方面的幫助都非常感謝。
import datetime
created_at = '2021-11-15T09:10:26.000+00:00'
t = datetime.datetime.fromisoformat(created_at)
n = datetime.datetime.now(tz=t.tzinfo)
if n - t <= datetime.timedelta(days=7):
... # do someting
使用您的branch.attributes
關於您在循環中的提交,您可以執行以下操作(確保導入日期時間)。 您需要將要刪除的分支名稱附加到您將在之后迭代的列表中,因為您不想修改您當前正在迭代的任何對象(即從您仍在迭代的branches
對象中刪除項目迭代)。
from datetime import datetime
...
branches_to_delete = []
for project in project_ids:
project = gl.projects.get(project)
branches = project.branches.list()
for branch in branches:
if "tobedeleted" in branch.attributes['name']:
print(branch.attributes['name'])
#print(branch)
print(branch.attributes['commit'])
branch_date_object = datetime.strptime((mydict['commit']['authored_date'].split('.')[0]), "%Y-%m-%dT%H:%M:%S")
days_diff = datetime.now() - branch_date_object
if days_diff.days > 7:
branches_to_delete.append(branch.attributes['name'])
for branch in branches_to_delete:
# perform your delete functionality
from datetime import datetime
def get_day_diff(old_date):
old_datetime = datetime.fromisoformat(old_date)
# Check timezone of date
tz_info = old_datetime.tzinfo
current_datetime = datetime.now(tz_info)
datetime_delta = current_datetime - old_datetime
return datetime_delta.days
# test with authored_date
authored_date = '2021-11-15T09:10:26.000+00:00'
if get_day_diff(authored_date) > 7:
# then delete branch
branch.delete()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.