Python Forum
Storing version of the downloaded libs using json file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Storing version of the downloaded libs using json file
#1
Hi, I am working on a requirement where I download packages from a repository using Python. To improvise the script further, we want to have a setup where we store the versions of the libraries/packages we have downloaded, and do not repeat the process if the packages are already present. Additionally, if there is a newer version available, only then go ahead with the download. I am using a json file to have information regarding the packages that are downloaded.
I am facing an issue with regards to updating/appending the existing json file:

import json
import os


def writefunc(name_value, location_value):
    with open("my_json.json", 'a+') as f: ##creating a new file during the start
        if os.stat("my_json.json").st_size == 0:
            data = {
                "libraries": {
                    "name": name_value,
                    "version": location_value
                }
            }
            json.dump(data, f, indent=4)
        else:
            dump = json.loads(f.read())  ## errror here as I cannot read the data on the file
            print(dump)

if __name__ == "__main__":
    writefunc('package1', '11.0')
    print('added package1')
    writefunc('package2', '12.0')
    print('added package2')
    writefunc('package1', '12.0')
     print('added package1')
Expected output :
{
"libraries":[ {
"name": "package1",
"version": "12.0"
}, {
"name": "package2",
"version": "12.0"
}
]
}

Any suggestions on my error and how can I achieve this?
Reply
#2
I have strong doubts that this is some sort of homework assignment. There was virtually the same question on StackOverflow today
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi Buran,
Yes I had asked the question on SO yesterday. I still have not achieved the clarity that I wouldve liked, and was hoping it would be easier with the json file.
Working with Json is new for me, and was optimistic that I would get some suggestions/feedback here so that I could proceed on the task.

If you have any suggestions, please share it across so that I could improve myself.

BR,
Rakshan
Reply
#4
IMHO, the best for this task would be using a database. Python comes with native support for sqlite. As of version 3.24.0 (2018-06-04) INSERT/UPDATE (a.k.a UPSERT) support was added, making it even easier to acomplish the task (as I understand it).

The problem with your current approach is that you have your data in list of dicts/dict of dicts. With this current setup, writing to JSON file is just slightly better (if at all) than writing to regular text file. The format of JSON would greatly affect any potential benefits. In any case you have to iterate over at least one of the file and/or new data.

If you change the format of both to {package1:version, package2:version} then it will be easier to update JSON (after reading the whole file), without iterating.
ALSO, As mentioned by someone on SO - it may be better to actually keep history of versions and date of install(?), instead of just overwriting the most up-to-date version number
Rakshan likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug [for a h/w project] How to save and get back dictionary in a .json file in TinyDB. adithya_like_py 4 3,495 Feb-05-2021, 10:49 AM
Last Post: buran
  Extracting link list to json file naor 5 2,557 Sep-17-2020, 04:16 PM
Last Post: micseydel
  Saving to Json file Shambob1874 1 2,434 May-30-2018, 10:00 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020