View Single Post
Old 10-11-2022, 12:55 PM   #46
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,509
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
Can be useful for also defining convenience methods in the future, that simplifies repetitive tasks, and can be called using the ctx.convenience_method()
True. For example I frequently write the same code to deal with metadata that is None vs a non-existent attribute. For example, mi.get('#custcol', 'foo') will never return 'foo' if '#custcol' exists. If you want a default for an empty column then you must do something like
Code:
    'foo' if mi.get('#custcol') is None else mi.get('#custcol')
One way is a helper method:
Code:
def get_default_if_none(book, field, default):
	v = book.get(field, None)
	return default if v is None else v

Last edited by chaley; 10-11-2022 at 02:27 PM.
chaley is offline   Reply With Quote