Skip to content

Commit

Permalink
feat: Us e with when opening file to ensure closure ✨ (#604)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #604

Reviewed By: mengxuanzhangz

Differential Revision: D49881089

Pulled By: stcheng

fbshipit-source-id: c43169f1924b9336352e029e952da91311f019de
  • Loading branch information
yezz123 authored and facebook-github-bot committed Oct 3, 2023
1 parent 22d7355 commit 438b9b0
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 43 deletions.
6 changes: 2 additions & 4 deletions examples/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@

this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')
config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
api = FacebookAdsApi.init(access_token=config['access_token'])
account_id = config['act_id']

Expand Down
7 changes: 3 additions & 4 deletions examples/batch_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Creates several ads using batch calls.
"""


from facebook_business import FacebookSession
from facebook_business import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
Expand All @@ -35,10 +36,8 @@
this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')

config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
### Setup session and api objects
session = FacebookSession(
config['app_id'],
Expand Down
7 changes: 3 additions & 4 deletions examples/batch_pause.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Pauses all active ad campaigns using batch calls.
"""


from facebook_business import FacebookSession
from facebook_business import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
Expand All @@ -35,10 +36,8 @@
this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')

config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
### Setup session and api objects
session = FacebookSession(
config['app_id'],
Expand Down
8 changes: 3 additions & 5 deletions examples/custom_audience_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
https://github.com/facebook/facebook-python-ads-sdk
"""


from facebook_business import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.customaudience import CustomAudience
Expand All @@ -38,11 +39,8 @@
this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')

### Setup session and api objects
config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
auth_info = (
config['app_id'],
config['app_secret'],
Expand Down
6 changes: 2 additions & 4 deletions examples/dpa-update/dpa_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
import sys
sys.path.insert(1, os.path.join(this_dir, os.pardir, os.pardir))

config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.productcatalog import ProductCatalog
from facebook_business.adobjects.productitem import ProductItem
Expand Down
6 changes: 2 additions & 4 deletions examples/dpa-update/dpa_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@
import sys
sys.path.insert(1, os.path.join(this_dir, os.pardir, os.pardir))

config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.productcatalog import ProductCatalog
from facebook_business.adobjects.productitem import ProductItem
Expand Down
7 changes: 3 additions & 4 deletions examples/read_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Prints account permissions and campaign statistics.
"""


from facebook_business import FacebookSession
from facebook_business import FacebookAdsApi
from facebook_business.adobjects.campaign import Campaign as AdCampaign
Expand All @@ -35,10 +36,8 @@
this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')

config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
### Setup session and api objects
session = FacebookSession(
config['app_id'],
Expand Down
18 changes: 8 additions & 10 deletions examples/simple_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Creates an ad through a utility function.
"""


from facebook_business import FacebookSession
from facebook_business import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
Expand All @@ -36,10 +37,8 @@
this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')

config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
### Setup session and api objects
session = FacebookSession(
config['app_id'],
Expand Down Expand Up @@ -88,10 +87,9 @@
'ad_format': AdPreview.AdFormat.right_column_standard
})
preview_filename = os.path.join(this_dir, 'preview_ad.html')
preview_file = open(preview_filename, 'w')
preview_file.write(
"<html><head><title>Facebook Ad Preview</title><body>%s</body></html>"
% preview.get_html()
)
preview_file.close()
with open(preview_filename, 'w') as preview_file:
preview_file.write(
"<html><head><title>Facebook Ad Preview</title><body>%s</body></html>"
% preview.get_html()
)
print('**** %s has been created!' % preview_filename)
7 changes: 3 additions & 4 deletions examples/upload_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Upload a video to adaccount
"""


import sys
import os

Expand All @@ -34,10 +35,8 @@

config_filename = os.path.join(sdk_path, './config.json')

config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

with open(config_filename) as config_file:
config = json.load(config_file)
### Setup session and api objects
session = FacebookSession(
config['app_id'],
Expand Down

0 comments on commit 438b9b0

Please sign in to comment.