V
V
Vlad Khorenko2021-09-19 20:14:37
Python
Vlad Khorenko, 2021-09-19 20:14:37

How to solve a problem in Python?

There is a task:
An example of how the utility works:
Saving the value value by the key key_name:
$ storage.py --key key_name --val value
Getting a value by the key key_name:
$ storage.py --key key_name
Task: write an implementation of the storage.py utility.
The utility can be called with the following parameters:
--key <key name> , where <key name> is the key by which values ​​are saved/retrieved
--val <value>, where <value> is the value to be saved.
If both keys are passed when starting the utility, the passed value is added by the key and the data is saved in the file. If only the name of the key is passed, the storage file is read and the values ​​stored by the given key are printed. Please note that the values ​​for one key are not overwritten, but added to those already saved. In other words, one key can store several values. When printed, the values ​​are printed in the order they were added to the storage. Multi-value printout format:
value_1, value_2
Note the space after the comma. If no values ​​were found for the key, print an empty string or None.
To work with command line arguments, use the argparse module. We recommend storing data in a file in JSON format by using the json standard library module. Before submitting your solution for review, test your utility by adding multiple keys and different values.
The file must be created using the tempfile module.
import os
import tempfile

storage_path = os.path.join(tempfile.gettempdir(), 'storage.data')
with open(storage_path, 'w') as f:
  ...
Example:
$ python storage.py -- key key_name --val value
$ python storage.py --key key_name
value

$ python storage.py --key multi_key --val value1
$ python storage.py --key multi_key --val value2
$ python storage.py --key multi_key
value1, value2

I solved it like this:

import os
import sys
import tempfile
import json

storage_path = os.path.join(tempfile.gettempdir(), 'storage.data')
storage = {}
def key(dictionary, key):
    dictionary.update(key, None)
def key_and_value(dictionary, key, value):
    dictionary.update(key, value)
with open(storage_path, 'w') as f:
    if len(sys.argv) <= 3 and sys.argv[1] == '--key':
        if sys.argv[2] not in storage:
            key(storage, sys.argv[2])
            json.dump(storage, f)
        if sys.argv[2] in storage:
            with open(storage_path, 'w') as f:
                storage.get(key[sys.argv[2]])
    if len(sys.argv) > 3 and (sys.argv[1] == '--key' and sys.argv[3] == '--val'):
        key_and_value(storage, sys.argv[2], sys.argv[4])
        json.dump(storage, f)

But I have errors while executing this program.
Please, I will be very grateful if you explain to me how to solve this problem, and show me what was wrong with me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Demin, 2021-09-19
@vlad_horenko

You are parsing the arguments incorrectly. For example,

if len(sys.argv) <= 3 and sys.argv[1] == '--key':
        if sys.argv[2] not in storage:

If there are fewer or three arguments, you access the second and third arguments. Zero arguments fits this condition, so your program crashes.
Another example
if sys.argv[2] in storage:
    with open(storage_path, 'w') as f:
        storage.get(key[sys.argv[2]])

If there is a key in the storage, open the file and get the value from the storage. Firstly, at the moment, this file is already open, secondly, you do not read values ​​from it into the storage variable, thirdly, you get the value from storage, but do not display it in the console.
Another wrong piece:
def key_and_value(dictionary, key, value):
    dictionary.update(key, value)

You conditionally need to collect a list of all values, not just the last one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question