Aug-18-2020, 08:38 AM
Hi
I've come up with a use of decorators which I haven't seen anyone else use as of yet, and I'm wondering if there is a reason not to do this. Basically the methods using the decorator are there purely for the method name. I've done this as it allows me to have a general function, but control which columns are available outside the class.
Here is the code for the class with the decorator (__set_field__). The sending record is kept in a table in an sqlite DB, hence the conn parameter.
What I'm after is whether this is good or bad as far as python goes, I'm an experienced scripter, but this is only the 2nd python script I've worked on.
I've come up with a use of decorators which I haven't seen anyone else use as of yet, and I'm wondering if there is a reason not to do this. Basically the methods using the decorator are there purely for the method name. I've done this as it allows me to have a general function, but control which columns are available outside the class.
Here is the code for the class with the decorator (__set_field__). The sending record is kept in a table in an sqlite DB, hence the conn parameter.
What I'm after is whether this is good or bad as far as python goes, I'm an experienced scripter, but this is only the 2nd python script I've worked on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class SendingRecord: def __init__( self , conn, processingid, file , destination): """ Constructor method """ self . id = conn.add_sending_record(processingid, file , destination) self .processingid = processingid self . file = file self .destination = destination self .send_attempts = 0 self .last_result = None def __set_field__(func): @wraps (func) def decorator( self , conn, value): col_name = func.__name__[ 4 :] conn.execute( 'UPDATE sending SET {0} = ? WHERE id = ?' . format (col_name), (value, self . id )) conn.commit() setattr ( self , col_name, value) return decorator @__set_field__ def set_status( self , conn, value): conn.set_record_status( 'sending' , self . id , status) @__set_field__ def set_send_attempts( self , conn, value): pass |