Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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 sys.argv[2] in storage:
with open(storage_path, 'w') as f:
storage.get(key[sys.argv[2]])
def key_and_value(dictionary, key, value):
dictionary.update(key, value)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question