I am implementing a Javascript class in Python 3, and I would like to verify if my code is correct, and how I could refactor it further.
This is the Javascript code:
class DbResult {
constructor(data) {
let self = this;
data = data || {};
self.status = data.status || 'info';
self.message = data.message || null;
}
static map(row) {
let dbResult = new DbResult();
if(!row) { return dbResult; }
for(let property in row) {
if(row.hasOwnProperty(property)) {
const name = property.replace(/_[a-zA-Z]/g, (match) => {
return match[1].toUpperCase();
});
dbResult[name] = row[property];
}
}
return dbResult;
}
module.exports = DbResult;
This is my Python implementation. I'm not certain if I used the lambda expression correctly.
import re
class DbResult:
def __init__(self, data):
if data is None:
data = {}
if data.status is None:
self.status = 'info'
else:
self.status = data.status
if data.message is not None:
self.message = data.message
@staticmethod
def map(row):
dbResult = DbResult()
if row is None:
return dbResult
for prop in row:
if row.has_key(prop):
name = re.sub("_[a-zA-Z]", lambda match: match[1].upper(), prop)
dbResult[name] = row[name]
return dbResult