From 2015143cd8f80b79190bee34293b4de682148f93 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Fri, 27 May 2022 15:13:39 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- test/image/make_baseline.py | 35 +++++++++++++++-------------------- test/image/make_exports.py | 21 ++++++++++----------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/test/image/make_baseline.py b/test/image/make_baseline.py index 58c9d40614a..dd4e0e5dc30 100644 --- a/test/image/make_baseline.py +++ b/test/image/make_baseline.py @@ -36,18 +36,12 @@ pio.templates.default = 'none' pio.kaleido.scope.plotlyjs = os.path.join(root, 'build', 'plotly.js') -_credentials = open(os.path.join(root, 'build', 'credentials.json'), 'r') -pio.kaleido.scope.mapbox_access_token = json.load(_credentials)['MAPBOX_ACCESS_TOKEN'] -_credentials.close() - +with open(os.path.join(root, 'build', 'credentials.json'), 'r') as _credentials: + pio.kaleido.scope.mapbox_access_token = json.load(_credentials)['MAPBOX_ACCESS_TOKEN'] ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith('.json')] ALL_MOCKS.sort() -if len(args) > 0 : - allNames = [a for a in args if a in ALL_MOCKS] -else : - allNames = ALL_MOCKS - +allNames = [a for a in args if a in ALL_MOCKS] if len(args) > 0 else ALL_MOCKS # gl2d pointcloud and other non-regl gl2d mock(s) # must be tested in certain order to work on CircleCI; # @@ -81,23 +75,23 @@ ] allNames = [a for a in allNames if a not in blacklist] -if len(allNames) == 0 : +if not allNames: print('error: Nothing to create!') sys.exit(1) failed = [] -for name in allNames : +MAX_RETRY = 2 # 1 means retry once +for name in allNames: outName = name - if mathjax_version == 3 : - outName = 'mathjax3___' + name + if mathjax_version == 3: + outName = f'mathjax3___{name}' print(outName) created = False - MAX_RETRY = 2 # 1 means retry once - for attempt in range(0, MAX_RETRY + 1) : - with open(os.path.join(dirIn, name + '.json'), 'r') as _in : + for attempt in range(MAX_RETRY + 1): + with open(os.path.join(dirIn, f'{name}.json'), 'r') as _in: fig = json.load(_in) width = 700 @@ -110,14 +104,15 @@ if 'height' in layout : height = layout['height'] - try : + try: pio.write_image( fig=fig, - file=os.path.join(dirOut, outName + '.png'), + file=os.path.join(dirOut, f'{outName}.png'), width=width, height=height, - validate=False + validate=False, ) + created = True except Exception as e : print(e) @@ -128,7 +123,7 @@ if(created) : break -if len(failed) > 0 : +if failed: print('Failed at :') print(failed) sys.exit(1) diff --git a/test/image/make_exports.py b/test/image/make_exports.py index 0e7ccd604db..00fb028695b 100644 --- a/test/image/make_exports.py +++ b/test/image/make_exports.py @@ -10,10 +10,8 @@ pio.templates.default = 'none' pio.kaleido.scope.plotlyjs = os.path.join(root, 'build', 'plotly.js') -_credentials = open(os.path.join(root, 'build', 'credentials.json'), 'r') -pio.kaleido.scope.mapbox_access_token = json.load(_credentials)['MAPBOX_ACCESS_TOKEN'] -_credentials.close() - +with open(os.path.join(root, 'build', 'credentials.json'), 'r') as _credentials: + pio.kaleido.scope.mapbox_access_token = json.load(_credentials)['MAPBOX_ACCESS_TOKEN'] allFormats = ['svg', 'jpg', 'jpeg', 'webp', 'eps', 'pdf'] # 'png' is tested by image-test @@ -36,11 +34,11 @@ ] failed = 0 -for name in allNames : - for fmt in allFormats : - print(name + ' --> ' + fmt) +for name in allNames: + for fmt in allFormats: + print(f'{name} --> {fmt}') - with open(os.path.join(dirIn, name + '.json'), 'r') as _in : + with open(os.path.join(dirIn, f'{name}.json'), 'r') as _in: fig = json.load(_in) width = 700 @@ -53,15 +51,16 @@ if 'height' in layout : height = layout['height'] - try : + try: pio.write_image( fig=fig, - file=os.path.join(dirOut, name + '.' + fmt), + file=os.path.join(dirOut, f'{name}.{fmt}'), width=width, height=height, - validate=False + validate=False, ) + except Exception as e : print(e) failed += 1