Error handling (#151)

* fix : load_config function updated

* fix : load_data function updated

* fix : tests updated

* fix : tests updated

* fix : new tests added

* doc : CHANGELOG updated
pull/154/head
Sepand Haghighi 2022-09-15 13:52:24 +04:30 zatwierdzone przez GitHub
rodzic 2fadfea62f
commit a7a1ae56ee
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
5 zmienionych plików z 40 dodań i 17 usunięć

Wyświetl plik

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Test system modified
- `generate` method optimized
- `samila_help` function updated
- `load_data` and `load_config` functions error handling updated
## [0.8] - 2022-06-01
### Added
- `INVALID_COLOR_TYPE_ERROR` error

Wyświetl plik

@ -14,7 +14,7 @@ from .params import DEFAULT_START, DEFAULT_STOP, DEFAULT_STEP, DEFAULT_COLOR, DE
from .params import DEFAULT_CMAP, DEFAULT_CMAP_RANGE
from .params import DEFAULT_BACKGROUND_COLOR, DEFAULT_SPOT_SIZE, DEFAULT_PROJECTION, DEFAULT_ALPHA, DEFAULT_LINEWIDTH
from .params import Projection, VALID_COLORS, HEX_COLOR_PATTERN, NFT_STORAGE_API, NFT_STORAGE_LINK, OVERVIEW
from .params import DATA_TYPE_ERROR, CONFIG_TYPE_ERROR, PLOT_DATA_ERROR, CONFIG_NO_STR_FUNCTION_ERROR
from .params import DATA_TYPE_ERROR, DATA_FORMAT_ERROR, CONFIG_TYPE_ERROR, CONFIG_FORMAT_ERROR, PLOT_DATA_ERROR, CONFIG_NO_STR_FUNCTION_ERROR
from .params import NO_FIG_ERROR_MESSAGE, FIG_SAVE_SUCCESS_MESSAGE, NFT_STORAGE_SUCCESS_MESSAGE, SAVE_NO_DATA_ERROR
from .params import INVALID_COLOR_TYPE_ERROR, COLOR_SIZE_ERROR
from .params import BOTH_COLOR_COMPLEMENT_WARNING, COLOR_NOT_FOUND_WARNING
@ -712,6 +712,8 @@ def load_data(g, data):
data = json.load(data)
g.data1 = data.get('data1')
g.data2 = data.get('data2')
if g.data1 is None or g.data2 is None:
raise samilaDataError(DATA_FORMAT_ERROR)
if 'matplotlib_version' in data:
g.matplotlib_version = data['matplotlib_version']
plot_config = data.get("plot")
@ -742,6 +744,8 @@ def load_config(g, config):
data = json.load(config)
g.function1_str = data.get("f1")
g.function2_str = data.get("f2")
if g.function1_str is None or g.function2_str is None:
raise samilaConfigError(CONFIG_FORMAT_ERROR)
if 'matplotlib_version' in data:
g.matplotlib_version = data['matplotlib_version']
generate_config = data.get("generate")

Wyświetl plik

@ -38,7 +38,9 @@ FIG_SAVE_SUCCESS_MESSAGE = "Everything seems good."
DATA_SAVE_SUCCESS_MESSAGE = "Everything seems good."
NO_FIG_ERROR_MESSAGE = "No figure was found. First run `generate` and `plot` methods."
DATA_TYPE_ERROR = "Provided data file is not supported. It should be either file or io.IOBase."
DATA_FORMAT_ERROR = "Provided data file is not supported. It should include data1 and data2."
CONFIG_TYPE_ERROR = "Provided config file is not supported. It should be either file or io.IOBase."
CONFIG_FORMAT_ERROR = "Provided config file is not supported. It should include f1 and f2."
CONFIG_NO_STR_FUNCTION_ERROR = "Config file can't be saved. At least one of the function1_str or function2_str is None."
PLOT_DATA_ERROR = "Plotting process can't be Done because data{0} is empty. Use generate method first."
COLOR_SIZE_ERROR = "Color list size is not equal to the data size."

Wyświetl plik

@ -24,8 +24,9 @@ Traceback (most recent call last):
...
samila.errors.samilaPlotError: Plotting process can't be Done because data1 is empty. Use generate method first.
>>> with open("data.json", 'w') as fp:
... json.dump({'data1': [0]}, fp)
... json.dump({'data1': [0], 'data2': [0]}, fp)
>>> g = GenerativeImage(data=open('data.json', 'r'))
>>> g.data2 = None
>>> g.save_data()
Traceback (most recent call last):
...
@ -61,5 +62,30 @@ samila.errors.samilaPlotError: Given color/bgcolor type is not supported.
Traceback (most recent call last):
...
samila.errors.samilaPlotError: Given color/bgcolor type is not supported.
>>> with open("data.json", 'w') as fp:
... json.dump({'data1': [0]}, fp)
>>> g = GenerativeImage(data=open('data.json', 'r'))
Traceback (most recent call last):
...
samila.errors.samilaDataError: Provided data file is not supported. It should include data1 and data2.
>>> with open("data.json", 'w') as fp:
... json.dump({'data2': [0]}, fp)
>>> g = GenerativeImage(data=open('data.json', 'r'))
Traceback (most recent call last):
...
samila.errors.samilaDataError: Provided data file is not supported. It should include data1 and data2.
>>> with open("config.json", 'w') as fp:
... json.dump({'f1': "x"}, fp)
>>> g = GenerativeImage(config=open('config.json', 'r'))
Traceback (most recent call last):
...
samila.errors.samilaConfigError: Provided config file is not supported. It should include f1 and f2.
>>> with open("config.json", 'w') as fp:
... json.dump({'f2': "x"}, fp)
>>> g = GenerativeImage(config=open('config.json', 'r'))
Traceback (most recent call last):
...
samila.errors.samilaConfigError: Provided config file is not supported. It should include f1 and f2.
>>> os.remove('data.json')
>>> os.remove('config.json')
"""

Wyświetl plik

@ -197,16 +197,6 @@ True
>>> g.spot_size == g_.spot_size
True
>>> with open("config.json", 'w') as fp:
... json.dump({'f1': 'x'}, fp)
>>> g = GenerativeImage(config=open("config.json", 'r'))
>>> g.function1_str
'x'
>>> with open("config.json", 'w') as fp:
... json.dump({'f2': 'x'}, fp)
>>> g = GenerativeImage(config=open("config.json", 'r'))
>>> g.function2_str
'x'
>>> with open("config.json", 'w') as fp:
... json.dump({'f1': 'y', 'f2': 'x'}, fp)
>>> g = GenerativeImage(config=open("config.json", 'r'))
>>> g.function1_str
@ -228,7 +218,7 @@ False
>>> g = GenerativeImage(data=open("data.json", 'r'))
>>> with open("config.json", 'w') as fp:
... json.dump({'f1': "x", 'f2': "y", 'plot':{}}, fp)
>>> g = GenerativeImage(data=open("config.json", 'r'))
>>> g = GenerativeImage(config=open("config.json", 'r'))
>>> g = GenerativeImage()
>>> g.generate()
>>> cm = Colormap(name="Purples")
@ -236,7 +226,7 @@ False
>>> result = g.save_config("config.json")
>>> result["status"]
True
>>> g_ = GenerativeImage(data=open("config.json", 'r'))
>>> g_ = GenerativeImage(config=open("config.json", 'r'))
>>> (g_.cmap.colors == g.cmap.colors).all()
True
>>> cm = ["black", [0.6, 0.2, 0.2, 1], [0.5, 0.2, 0.2, 1], [0.4, 0.2, 0.2, 1], "yellow", [0.2, 0.2, 0.2, 1],]
@ -244,7 +234,7 @@ True
>>> result = g.save_config("config.json")
>>> result["status"]
True
>>> g_ = GenerativeImage(data=open("config.json", 'r'))
>>> g_ = GenerativeImage(config=open("config.json", 'r'))
>>> g_.cmap.colors == g.cmap.colors
True
>>> g.plot(cmap="Purples")
@ -255,10 +245,10 @@ True
>>> g = GenerativeImage()
>>> g.generate()
>>> g.plot(cmap=cmap, color=g.data1)
>>> result = g.save_data("config.json")
>>> result = g.save_data("data.json")
>>> result["status"]
True
>>> g_ = GenerativeImage(data=open("config.json", "r"))
>>> g_ = GenerativeImage(data=open("data.json", "r"))
>>> g_.plot()
>>> g_.cmap.colors == g.cmap.colors
True