So the problem I am having in python is that every time I make a connection to my jira account, I can't seem to keep my previous python dictionary values.
The logic seems to fail in the for loop and when I assign empty dictionaries. Other than that, I did try to work around this issue by using a couple conditions, but those methods did not work out.
Here is what I mean.
Current Code:
import os
import pandas as pd
from jira.client import JIRA
from datetime import datetime
jira_token = os.getenv(<jira_personal_tokne>)
jira_link = os.getenv(<jira_url>)
jira_server = {"server": jira_link}
auth_jira = JIRA(options=jira_server, token_auth=jira_token)
proj = auth_jira.search_issues("project=<my_project> and 'epic link'='<epic_link_of_interest>'")
plmpgm_dict= {}
for i in proj:
formatted_date = datetime.strptime(i.fields.updated, '%y-%m-%dT%H:%M:%S.%f%z').strftime("%Y-%m-%dT%H:%M:%S")
inner_dict = {}
inner_dict["summary"]=i.fields.summary
inner_dict["description"] = i.fields.description
inner_dict["last_retrieved"] = formatted_date
plmpgm_dict[i.key] inner_dict
if i.key == "<jira issue>":
print(plmpgm_dict)
Output I get:
{'<jira issue>':{
'summary':'summary_values',
'description: 'description values',
'last_retrieved':'2022-03-11T19:44:15'
}
}
Output I want/expected:
{'<jira issue>':{
'summary':'summary_values',
'description: 'description values',
'last_retrieved':'2022-03-11T18:44:15'
},{
'summary':'old summary_values',
'description: ' old description values',
'last_retrieved':'2022-03-11T18:50:15'
}
}
Now, I am wondering if there is a way to possibly store my previous python dictionary key-value pairs instead of being replaced with the new dictionary key-value pairs when I make a connection to jira using python?
Actually, your problem is about neither Jira nor Python.
When you run a program, all variables start from zero. It is programming logic. You need to save previous values, so you just need to save the data at some destination (File system, database, etc.)
The Idea
So, we need to save the data. It is a Python dict
so we can save it as a JSON
string. For now, let's say we're saving it inside a JSON file in system. And when you run the program, you should read this JSON file into plmpgm_dict
variable.
You can use the following code for that operation:
import json
def dict_to_json_file(some_dict, file_name):
fp = open(file_name, 'w+)
fp.write(json.dumps(some_dict))
fp.close()
def json_file_to_dict(file_name):
fp = open(file_name, 'r+)
some_dict = json.loads(fp.read())
fp.close()
return some_dict
Algorithm
Before the for
loop, you need to read from the file (if exists) into plmpgm_dict
with something like plmpgm_dict = json_file_to_dict('dump.json')
.
After the for
loop, at the end of your code, you need to dump the dict to the JSON file with something like dict_to_json_file(plmpgm_dict, 'dump.json')