Skip to content

Commit 19961a1

Browse files
Refactor file handling to use 'with' statement (#9500)
* refactor: use 'with' statement for file handling to ensure proper closure * refactor: replace open() with 'with' statement for better file handling --------- Co-authored-by: Chris Gervang <[email protected]>
1 parent b1e4a9f commit 19961a1

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

bindings/pydeck-carto/setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
with open(join("pydeck_carto", "_version.py")) as f:
66
exec(f.read(), {}, version_ns)
77

8+
with open("README.md") as f:
9+
long_description = f.read()
10+
811
setup(
912
name="pydeck-carto",
1013
version=version_ns["__version__"],
1114
description="Pydeck wrapper for use with CARTO",
12-
long_description=open("README.md").read(),
15+
long_description=long_description,
1316
long_description_content_type="text/markdown",
1417
keywords=["pydeck", "carto", "visualization", "graphics", "GIS", "maps"],
1518
author="CARTO",

bindings/pydeck/bump_version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def rewrite_version_file(semver):
3737

3838
def rewrite_frontend_version_file():
3939
"""Current associated version of NPM modules deck.gl and @deck.gl/jupyter-widget"""
40-
lerna_version = json.loads(open("../../lerna.json").read())["version"]
40+
with open("../../lerna.json") as f:
41+
lerna_version = json.loads(f.read())["version"]
4142
with open("pydeck/frontend_semver.py", "w+") as f:
4243
t = jinja2.Template("DECKGL_SEMVER = '{{semver_str}}'")
4344
contents = t.render(semver_str=str(lerna_version))

bindings/pydeck/docs/scripts/embed_examples.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def create_rst(pydeck_example_file_name):
3131
),
3232
shell=True,
3333
)
34-
python_code = open(pydeck_example_file_name, "r").read()
34+
with open(pydeck_example_file_name, "r") as f:
35+
python_code = f.read()
3536
doc_source = DOC_TEMPLATE.render(
3637
page_title=to_presentation_name(asset_name),
3738
snake_name=asset_name,
@@ -40,10 +41,10 @@ def create_rst(pydeck_example_file_name):
4041
deckgl_doc_url=deckgl_doc_url,
4142
)
4243
rst_path = os.path.join(GALLERY_DIR, asset_name + ".rst")
43-
f = open(rst_path, "w+")
44-
print("* Converted %s to %s" % (pydeck_example_file_name, rst_path))
45-
f.write(doc_source)
46-
f.close()
44+
with open(rst_path, "w") as f:
45+
print("* Converted %s to %s" % (pydeck_example_file_name, rst_path))
46+
f.write(doc_source)
47+
4748

4849

4950
def main():

bindings/pydeck/tests/fixtures.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
fixtures = {}
1212

1313
for fname in glob.glob(json_glob):
14-
fixture_text = open(fname).read()
14+
with open(fname) as file:
15+
fixture_text = file.read()
1516
fixture_name = os.path.basename(fname).replace(".json", "")
1617
fixtures[fixture_name] = fixture_text

0 commit comments

Comments
 (0)