8000 Using Python 3 compatible print statements in snippets (#157) (#159) · viktorasl/firebase-admin-python@9444c95 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 9444c95

Browse files
Nicolas Couturehiranya911
authored andcommitted
Using Python 3 compatible print statements in snippets (firebase#157) (firebase#159)
* Uses python 3 compatible print statements. * Fixed Python 3 incompatible print syntax used in snippets. * adjusting snippets to lint happily on Python <3 - added "from __future__ import print_function" in snippets using print to make pylint "print-function-aware" and lint Python 3.x print-syntax without errors when running under Python 2.x. * remove unwanted parens from print statements in snippets - delete duplicate parens added in previous commits * Uses python 3 compatible print statements. * Fixed Python 3 incompatible print syntax used in snippets. - added "from __future__ import print_function" in snippets using print to make pylint "print-function-aware" and lint Python 3.x print-syntax without errors when running under Python 2.x. - delete duplicate parens used in print statements
1 parent 2e75177 commit 9444c95

File tree

4 files changed

+48
-40
lines changed

4 files changed

+48
-40
lines changed

snippets/auth/get_service_account_tokens.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import print_function
16+
1517
# [START get_service_account_tokens]
1618
from firebase_admin import credentials
1719

@@ -24,4 +26,4 @@
2426
# After expiration_time, you must generate a new access token
2527
# [END get_service_account_tokens]
2628

27-
print 'The access token {} expires at {}'.format(access_token, expiration_time)
29+
print('The access token {} expires at {}'.format(access_token, expiration_time))

snippets/auth/index.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import print_function
16+
1517
import datetime
1618
import sys
1719
import time
@@ -55,7 +57,7 @@ def access_services_default():
5557

5658
# Initialize the default app
5759
default_app = firebase_admin.initialize_app(cred)
58-
print default_app.name # "[DEFAULT]"
60+
print(default_app.name) # "[DEFAULT]"
5961

6062
# Retrieve services via the auth package...
6163
# auth.create_custom_token(...)
@@ -73,8 +75,8 @@ def access_services_nondefault():
7375
# Initialize another app with a different config
7476
other_app = firebase_admin.initialize_app(cred, name='other')
7577

76-
print default_app.name # "[DEFAULT]"
77-
print other_app.name # "other"
78+
print(default_app.name) # "[DEFAULT]"
79+
print(other_app.name) # "other"
7880

7981
# Retrieve default services via the auth package...
8082
# auth.create_custom_token(...)
@@ -119,7 +121,7 @@ def verify_token_uid(id_token):
119121
decoded_token = auth.verify_id_token(id_token)
120122
uid = decoded_token['uid']
121123
# [END verify_token_uid]
122-
print uid
124+
print(uid)
123125
firebase_admin.delete_app(default_app)
124126

125127
def verify_token_uid_check_revoke(id_token):
@@ -152,21 +154,21 @@ def revoke_refresh_token_uid():
152154
user = auth.get_user(uid)
153155
# Convert to seconds as the auth_time in the token claims is in seconds.
154156
revocation_second = user.tokens_valid_after_timestamp / 1000
155-
print 'Tokens revoked at: {0}'.format(revocation_second)
157+
print('Tokens revoked at: {0}'.format(revocation_second))
156158
# [END revoke_tokens]
157159
# [START save_revocation_in_db]
158160
metadata_ref = firebase_admin.db.reference("metadata/" + uid)
159161
metadata_ref.set({'revokeTime': revocation_second})
160162
# [END save_revocation_in_db]
161-
print uid
163+
print(uid)
162164
firebase_admin.delete_app(default_app)
163165

164166
def get_user(uid):
165167
# [START get_user]
166168
from firebase_admin import auth
167169

168170
user = auth.get_user(uid)
169-
print 'Successfully fetched user data: {0}'.format(user.uid)
171+
print('Successfully fetched user data: {0}'.format(user.uid))
170172
# [END get_user]
171173

172174
def get_user_by_email():
@@ -175,7 +177,7 @@ def get_user_by_email():
175177
from firebase_admin import auth
176178

177179
user = auth.get_user_by_email(email)
178-
print 'Successfully fetched user data: {0}'.format(user.uid)
180+
print('Successfully fetched user data: {0}'.format(user.uid))
179181
# [END get_user_by_email]
180182

181183
def get_user_by_phone_number():
@@ -184,7 +186,7 @@ def get_user_by_phone_number():
184186
from firebase_admin import auth
185187

186188
user = auth.get_user_by_phone_number(phone)
187-
print 'Successfully fetched user data: {0}'.format(user.uid)
189+
print('Successfully fetched user data: {0}'.format(user.uid))
188190
# [END get_user_by_phone]
189191

190192
def create_user():
@@ -197,15 +199,15 @@ def create_user():
197199
display_name='John Doe',
198200
photo_url='http://www.example.com/12345678/photo.png',
199201
disabled=False)
200-
print 'Sucessfully created new user: {0}'.format(user.uid)
202+
print('Sucessfully created new user: {0}'.format(user.uid))
201203
# [END create_user]
202204
return user.uid
203205

204206
def create_user_with_id():
205207
# [START create_user_with_id]
206208
user = auth.create_user(
207209
uid='some-uid', email='user@example.com', phone_number='+15555550100')
208-
print 'Sucessfully created new user: {0}'.format(user.uid)
210+
print('Sucessfully created new user: {0}'.format(user.uid))
209211
# [END create_user_with_id]
210212

211213
def update_user(uid):
@@ -219,13 +221,13 @@ def update_user(uid):
219221
display_name='John Doe',
220222
photo_url='http://www.example.com/12345678/photo.png',
221223
disabled=True)
222-
print 'Sucessfully updated user: {0}'.format(user.uid)
224+
print('Sucessfully updated user: {0}'.format(user.uid))
223225
# [END update_user]
224226

225227
def delete_user(uid):
226228
# [START delete_user]
227229
auth.delete_user(uid)
228-
print 'Successfully deleted user'
230+
print('Successfully deleted user')
229231
# [END delete_user]
230232

231233
def set_custom_user_claims(uid):
@@ -249,7 +251,7 @@ def set_custom_user_claims(uid):
249251
# Lookup the user associated with the specified uid.
250252
user = auth.get_user(uid)
251253
# The claims can be accessed on the user record.
252-
print user.custom_claims.get('admin')
254+
print(user.custom_claims.get('admin'))
253255
# [END read_custom_user_claims]
254256

255257
def set_custom_user_claims_script():
@@ -282,14 +284,14 @@ def list_all_users():
282284
page = auth.list_users()
283285
while page:
284286
for user in page.users:
285-
print 'User: ' + user.uid
287+
print('User: ' + user.uid)
286288
# Get next batch of users.
287289
page = page.get_next_page()
288290

289291
# Iterate through all users. This will still retrieve users in batches,
290292
# buffering no more than 1000 users in memory at a time.
291293
for user in auth.list_users().iterate_all():
292-
print 'User: ' + user.uid
294+
print('User: ' + user.uid)
293295
# [END list_all_users]
294296

295297
def create_session_cookie(flask, app):
@@ -340,7 +342,7 @@ def check_auth_time(id_token, flask):
340342

341343
def verfy_session_cookie(app, flask):
342344
def serve_content_for_user(decoded_claims):
343-
print 'Serving content with claims:', decoded_claims
345+
print('Serving content with claims:', decoded_claims)
344346
return flask.jsonify({'status': 'success'})
345347

346348
# [START session_verify]
@@ -362,7 +364,7 @@ def access_restricted_content():
362364

363365
def check_permissions(session_cookie, flask):
364366
def serve_content_for_admin(decoded_claims):
365-
print 'Serving content with claims:', decoded_claims
367+
print('Serving content with claims:', decoded_claims)
366368
return flask.jsonify({'status': 'success'})
367369

368370
# [START session_verify_with_permission_check]

snippets/database/index.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import print_function
16+
1517
import firebase_admin
1618
from firebase_admin import credentials
1719
from firebase_admin import db
@@ -32,7 +34,7 @@ def authenticate_with_admin_privileges():
3234

3335
# As an admin, the app has access to read and write all data, regradless of Security Rules
3436
ref = db.reference('restricted_access/secret_document')
35-
print ref.get()
37+
print(ref.get())
3638
# [END authenticate_with_admin_privileges]
3739
firebase_admin.delete_app(firebase_admin.get_app())
3840

@@ -55,7 +57,7 @@ def authenticate_with_limited_privileges():
5557

5658
# The app only has access as defined in the Security Rules
5759
ref = db.reference('/some_resource')
58-
print ref.get()
60+
print(ref.get())
5961
# [END authenticate_with_limited_privileges]
6062
firebase_admin.delete_app(firebase_admin.get_app())
6163

@@ -76,7 +78,7 @@ def authenticate_with_guest_privileges():
7678

7779
# The app only has access to public data as defined in the Security Rules
7880
ref = db.reference('/public_resource')
79-
print ref.get()
81+
print(ref.get())
8082
# [END authenticate_with_guest_privileges]
8183
firebase_admin.delete_app(firebase_admin.get_app())
8284

@@ -88,7 +90,7 @@ def get_reference():
8890
# Get a database reference to our blog.
8991
ref = db.reference('server/saving-data/fireblog')
9092
# [END get_reference]
91-
print ref.key
93+
print(ref.key)
9294

9395
def set_value():
9496
ref = db.reference('server/saving-data/fireblog')
@@ -201,7 +203,7 @@ def get_push_key():
201203
# Get the unique key generated by push()
202204
post_id = new_post_ref.key
203205
# [END push_key]
204-
print post_id
206+
print(post_id)
205207

206208
def run_transaction():
207209
# [START transaction]
@@ -211,9 +213,9 @@ def increment_votes(current_value):
211213
upvotes_ref = db.reference('server/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes')
212214
try:
213215
new_vote_count = upvotes_ref.transaction(increment_votes)
214-
print 'Transaction completed'
216+
print('Transaction completed')
215217
except db.TransactionError:
216-
print 'Transaction failed to commit'
218+
print('Transaction failed to commit')
217219
# [END transaction]
218220

219221
def read_value():
@@ -225,89 +227,89 @@ def read_value():
225227
ref = db.reference('server/saving-data/fireblog/posts')
226228

227229
# Read the data at the posts reference (this is a blocking operation)
228-
print ref.get()
230+
print(ref.get())
229231
# [END read_value]
230232

231233
def order_by_child():
232234
# [START order_by_child]
233235
ref = db.reference('dinosaurs')
234236
snapshot = ref.order_by_child('height').get()
235237
for key, val in snapshot.items():
236-
print '{0} was {1} meters tall'.format(key, val)
238+
print('{0} was {1} meters tall'.format(key, val))
237239
# [END order_by_child]
238240

239241
def order_by_nested_child():
240242
# [START order_by_nested_child]
241243
ref = db.reference('dinosaurs')
242244
snapshot = ref.order_by_child('dimensions/height').get()
243245
for key, val in snapshot.items():
244-
print '{0} was {1} meters tall'.format(key, val)
246+
print('{0} was {1} meters tall'.format(key, val))
245247
# [END order_by_nested_child]
246248

247249
def order_by_key():
248250
# [START order_by_key]
249251
ref = db.reference('dinosaurs')
250252
snapshot = ref.order_by_key().get()
251-
print snapshot
253+
print(snapshot)
252254
# [END order_by_key]
253255

254256
def order_by_value():
255257
# [START order_by_value]
256258
ref = db.reference('scores')
257259
snapshot = ref.order_by_value().get()
258260
for key, val in snapshot.items():
259-
print 'The {0} dinosaur\'s score is {1}'.format(key, val)
261+
print('The {0} dinosaur\'s score is {1}'.format(key, val))
260262
# [END order_by_value]
261263

262264
def limit_query():
263265
# [START limit_query_1]
264266
ref = db.reference('dinosaurs')
265267
snapshot = ref.order_by_child('weight').limit_to_last(2).get()
266268
for key in snapshot:
267-
print key
269+
print(key)
268270
# [END limit_query_1]
269271

270272
# [START limit_query_2]
271273
ref = db.reference('dinosaurs')
272274
snapshot = ref.order_by_child('height').limit_to_first(2).get()
273275
for key in snapshot:
274-
print key
276+
print(key)
275277
# [END limit_query_2]
276278

277279
# [START limit_query_3]
278280
scores_ref = db.reference('scores')
279281
snapshot = scores_ref.order_by_value().limit_to_last(3).get()
280282
for key, val in snapshot.items():
281-
print 'The {0} dinosaur\'s score is {1}'.format(key, val)
283+
print('The {0} dinosaur\'s score is {1}'.format(key, val))
282284
# [END limit_query_3]
283285

284286
def range_query():
285287
# [START range_query_1]
286288
ref = db.reference('dinosaurs')
287289
snapshot = ref.order_by_child('height').start_at(3).get()
288290
for key in snapshot:
289-
print key
291+
print(key)
290292
# [END range_query_1]
291293

292294
# [START range_query_2]
293295
ref = db.reference('dinosaurs')
294296
snapshot = ref.order_by_key().end_at('pterodactyl').get()
295297
for key in snapshot:
296-
print key
298+
print(key)
297299
# [END range_query_2]
298300

299301
# [START range_query_3]
300302
ref = db.reference('dinosaurs')
301303
snapshot = ref.order_by_key().start_at('b').end_at(u'b\uf8ff').get()
302304
for key in snapshot:
303-
print key
305+
print(key)
304306
# [END range_query_3]
305307

306308
# [START range_query_4]
307309
ref = db.reference('dinosaurs')
308310
snapshot = ref.order_by_child('height').equal_to(25).get()
309311
for key in snapshot:
310-
print key
312+
print(key)
311313
# [END range_query_4]
312314

313315
def complex_query():
@@ -320,10 +322,10 @@ def complex_query():
320322
# Data is ordered by increasing height, so we want the first entry.
321323
# Second entry is stegosarus.
322324
for key in snapshot:
323-
print 'The dinosaur just shorter than the stegosaurus is {0}'.format(key)
325+
print('The dinosaur just shorter than the stegosaurus is {0}'.format(key))
324326
return
325327
else:
326-
print 'The stegosaurus is the shortest dino'
328+
print('The stegosaurus is the shortest dino')
327329
# [END complex_query]
328330

329331

snippets/messaging/cloud_messaging.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import print_function
16+
1517
import datetime
1618

1719
from firebase_admin import messaging

0 commit comments

Comments
 (0)
0