Skip to content

Commit 0d5650d

Browse files
authored
Merge pull request #172 from alecgrieser/docs-python31-compatible
Make the class scheduling tutorial compatible with Python 2 and 3
2 parents 08d21b2 + 6940538 commit 0d5650d

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

documentation/sphinx/source/class-scheduling.rst

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ We're going to rely on the powerful guarantees of transactions to help keep all
110110

111111
@fdb.transactional
112112
def add_class(tr, c):
113-
tr[course.pack((c,))] = bytes(100)
113+
tr[course.pack((c,))] = fdb.tuple.pack((100,))
114114

115115
:py:func:`@fdb.transactional <fdb.transactional>` is a Python decorator that makes a normal function a transactional function. All functions decorated this way *need to have a parameter named* ``tr``. This parameter is passed the transaction that the function should use to do reads and writes.
116116

@@ -180,7 +180,7 @@ We finally get to the crucial function. A student has decided on a class (by nam
180180
@fdb.transactional
181181
def signup(tr, s, c):
182182
rec = attends.pack((s, c))
183-
tr[rec] = ''
183+
tr[rec] = b''
184184

185185
We simply insert the appropriate record (with a blank value).
186186

@@ -212,7 +212,7 @@ Let's go back to the data model. Remember that we stored the number of seats in
212212
@fdb.transactional
213213
def available_classes(tr):
214214
return [course.unpack(k)[0] for k, v in tr[course.range(())]
215-
if int(v)]
215+
if fdb.tuple.unpack(v)[0]]
216216
217217
This is easy -- we simply add a condition to check that the value is non-zero. Let's check out ``signup`` next:
218218

@@ -224,11 +224,11 @@ This is easy -- we simply add a condition to check that the value is non-zero. L
224224
rec = attends.pack((s, c))
225225
if tr[rec].present(): return # already signed up
226226
227-
seats_left = int(tr[course.pack((c,))])
227+
seats_left = fdb.tuple.unpack(tr[course.pack((c,))])[0]
228228
if not seats_left: raise Exception('No remaining seats')
229229
230-
tr[course.pack((c,))] = bytes(seats_left - 1)
231-
tr[rec] = ''
230+
tr[course.pack((c,))] = fdb.tuple.pack((seats_left - 1,))
231+
tr[rec] = b''
232232
233233
We now have to check that we aren't already signed up, since we don't want a double sign up to decrease the number of seats twice. Then we look up how many seats are left to make sure there is a seat remaining so we don't push the counter into the negative. If there is a seat remaining, we decrement the counter.
234234

@@ -259,7 +259,7 @@ Let's finish up the limited seats feature by modifying the drop function:
259259
def drop(tr, s, c):
260260
rec = attends.pack((s, c))
261261
if not tr[rec].present(): return # not taking this class
262-
tr[course.pack((c,))] = bytes(int(tr[course.pack((c,))]) + 1)
262+
tr[course.pack((c,))] = fdb.tuple.pack((fdb.tuple.unpack(tr[course.pack((c,))])[0] + 1,))
263263
del tr[rec]
264264
265265
This case is easier than signup because there are no constraints we can hit. We just need to make sure the student is in the class and to "give back" one seat when the student drops.
@@ -277,14 +277,14 @@ Of course, as soon as our new version of the system goes live, we hear of a tric
277277
rec = attends.pack((s, c))
278278
if tr[rec].present(): return # already signed up
279279
280-
seats_left = int(tr[course.pack((c,))])
280+
seats_left = fdb.tuple.unpack(tr[course.pack((c,))])[0]
281281
if not seats_left: raise Exception('No remaining seats')
282282
283283
classes = tr[attends.range((s,))]
284284
if len(list(classes)) == 5: raise Exception('Too many classes')
285285
286-
tr[course.pack((c,))] = bytes(seats_left - 1)
287-
tr[rec] = ''
286+
tr[course.pack((c,))] = fdb.tuple.pack((seats_left - 1,))
287+
tr[rec] = b''
288288
289289
Fortunately, we decided on a data model that keeps all of the attending records for a single student together. With this approach, we can use a single range read in the ``attends`` subspace to retrieve all the classes that a student is signed up for. We simply throw an exception if the number of classes has reached the limit of five.
290290

@@ -331,8 +331,10 @@ Appendix: SchedulingTutorial.py
331331
Here's the code for the scheduling tutorial::
332332

333333
import itertools
334+
import traceback
334335

335336
import fdb
337+
import fdb.tuple
336338

337339
fdb.api_version(510)
338340

@@ -352,7 +354,7 @@ Here's the code for the scheduling tutorial::
352354

353355
@fdb.transactional
354356
def add_class(tr, c):
355-
tr[course.pack((c,))] = bytes(100)
357+
tr[course.pack((c,))] = fdb.tuple.pack((100,))
356358

357359
# Generate 1,620 classes like '9:00 chem for dummies'
358360
levels = ['intro', 'for dummies', 'remedial', '101',
@@ -378,29 +380,29 @@ Here's the code for the scheduling tutorial::
378380
@fdb.transactional
379381
def available_classes(tr):
380382
return [course.unpack(k)[0] for k, v in tr[course.range(())]
381-
if int(v)]
383+
if fdb.tuple.unpack(v)[0]]
382384

383385

384386
@fdb.transactional
385387
def signup(tr, s, c):
386388
rec = attends.pack((s, c))
387389
if tr[rec].present(): return # already signed up
388390

389-
seats_left = int(tr[course.pack((c,))])
391+
seats_left = fdb.tuple.unpack(tr[course.pack((c,))])[0]
390392
if not seats_left: raise Exception('No remaining seats')
391393

392394
classes = tr[attends.range((s,))]
393395
if len(list(classes)) == 5: raise Exception('Too many classes')
394396

395-
tr[course.pack((c,))] = bytes(seats_left - 1)
396-
tr[rec] = ''
397+
tr[course.pack((c,))] = fdb.tuple.pack((seats_left - 1,))
398+
tr[rec] = b''
397399

398400

399401
@fdb.transactional
400402
def drop(tr, s, c):
401403
rec = attends.pack((s, c))
402404
if not tr[rec].present(): return # not taking this class
403-
tr[course.pack((c,))] = bytes(int(tr[course.pack((c,))]) + 1)
405+
tr[course.pack((c,))] = fdb.tuple.pack((fdb.tuple.unpack(tr[course.pack((c,))])[0] + 1,))
404406
del tr[rec]
405407

406408

@@ -446,7 +448,8 @@ Here's the code for the scheduling tutorial::
446448
my_classes.remove(old_c)
447449
my_classes.append(new_c)
448450
except Exception as e:
449-
print e, "Need to recheck available classes."
451+
traceback.print_exc()
452+
print("Need to recheck available classes.")
450453
all_classes = []
451454

452455
def run(students, ops_per_student):
@@ -455,9 +458,9 @@ Here's the code for the scheduling tutorial::
455458
for i in range(students)]
456459
for thr in threads: thr.start()
457460
for thr in threads: thr.join()
458-
print "Ran", students * ops_per_student, "transactions"
461+
print("Ran {} transactions".format(students * ops_per_student))
459462

460463
if __name__ == "__main__":
461464
init(db)
462-
print "initialized"
465+
print("initialized")
463466
run(10, 10)

0 commit comments

Comments
 (0)