Getting access to class's variables inside another class's def - python

I'm trying override a str method in Person() class:
'''class Person(object):
def __init__(self, Nose = None, Neck = None, RShoulder = None, RElbow = None, RWrist = None, LShoulder = None, LElbow = None, LWrist = None, MidHip = None, RHip = None, RKnee = None, RAnkle = None, LHip = None, LKnee = None, LAnkle = None, REye = None, LEye = None, REar = None, LEar = None, LBigToe = None, LSmallToe = None, LHeel = None, RBigToe = None, RSmallToe = None, RHeel = None):
self.Nose = Nose
self.Neck = Neck
self.RShoulder = RShoulder
self.RElbow = RElbow
self.RWrist = RWrist
self.LShoulder = LShoulder
self.LElbow = LElbow
self.LWrist = LWrist
self.MidHip = MidHip
self.RHip = RHip
self.RKnee = RKnee
self.RAnkle = RAnkle
self.LHip = LHip
self.LKnee = LKnee
self.LAnkle = LAnkle
self.REye = REye
self.LEye = LEye
self.REar = REar
self.LEar = LEar
self.LBigToe = LBigToe
self.LSmallToe = LSmallToe
self.LHeel = LHeel
self.RBigToe = RBigToe
self.RSmallToe = RSmallToe
self.RHeel = RHeel
def __str__(self):
return 'Nose = %s\nNeck = \n%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s'%(self.Nose,self.Neck,self.RShoulder,self.RElbow,self.RWrist,self.LShoulder,self.LElbow,self.LWrist,self.MidHip,self.RHip,self.RKnee,self.RAnkle,self.LHip,self.LKnee,self.LAnkle,self.REye,self.LEye,self.REar,self.LEar,self.LBigToe,self.LSmallToe,self.LHeel,self.RBigToe,self.RSmallToe,self.RHeel)'''
And I want to find more elegant way to return a string which will look like that:
Nose = something
Neck = something
...
...
...

Question: elegant way to return a string which will look like ...
You can use the built-in vars function to get the __dict__ of the class variable and format it using .format(... and .join(....
Reference:
vars([object])
Return the __dict__ attribute for a module, class, instance, or any other object with a __dict__ attribute.
.format(value[, format_spec])
Convert a value to a “formatted” representation, as controlled by a standard formatting syntax that is used by most built-in types: Format Specification Mini-Language.
<str>.join(iterable)
Return a string which is the concatenation of the strings in iterable.
class Person:
def __init__(self, **kwargs):
self.Nose = kwargs.get('Nose', None)
self.Neck = kwargs.get('Neck', None)
self.RShoulder = kwargs.get('RShoulder', None)
def __str__(self):
return '\n'.join(('{} = {}'
.format(k, v) for k, v in vars(self).items()))
p = Person(Nose=1, Neck=1)
print(p)
Output:
Nose = 1
Neck = 1
RShoulder = None
Tested with Python: 3.6

Related

Problem whith SQLAlquemy using SQLServer - Column, parameter, or variable #1: Cannot find data type 52. (2715) (SQLExecDirectW)')

I need a little help!
I'm developing an API (Python) and using SQLAlchemy with SqlServer.When I send the PUT request to my database application return an error:
Column, parameter, or variable #1: Cannot find data type 52. (2715) (SQLExecDirectW)')
entities
class Adress():
def __init__(self,
int_codEndLoja,
str_codigoLoja,
str_enderecoLoja,
str_numeroLoja,
str_complementoLoja,
str_bairroLoja,
str_cidadeLoja,
str_estadoLoja,
str_ufLoja,
str_cepLoja,
str_telefoneUmLoja,
str_telefoneDoisLoja,
str_telefoneTresLoja):
self.__int_codEndLoja = int_codEndLoja
self.__str_codigoLoja = str_codigoLoja
self.__str_enderecoLoja = str_enderecoLoja
self.__str_numeroLoja = str_numeroLoja
self.__str_complementoLoja = str_complementoLoja
self.__str_bairroLoja = str_bairroLoja
self.__str_cidadeLoja = str_cidadeLoja
self.__str_estadoLoja = str_estadoLoja
self.__str_ufLoja = str_ufLoja
self.__str_cepLoja = str_cepLoja
self.__str_telefoneUmLoja = str_telefoneUmLoja
self.__str_telefoneDoisLoja = str_telefoneDoisLoja
self.__str_telefoneTresLoja = str_telefoneTresLoja
#property
def int_codEndLoja(self):
return self.__int_codEndLoja
#int_codEndLoja.setter
def int_codEndLoja(self, int_codEndLoja):
self.__int_codEndLoja = int_codEndLoja
#property
def str_codigoLoja(self):
return self.__str_codigoLoja
#str_codigoLoja.setter
def str_codigoLoja(self, str_codigoLoja):
self.__str_codigoLoja = str_codigoLoja
#property
def str_enderecoLoja(self):
return self.__str_enderecoLoja
#str_enderecoLoja.setter
def str_enderecoLoja(self, str_enderecoLoja):
self.__str_enderecoLoja = str_enderecoLoja
#property
def str_numeroLoja(self):
return self.__str_numeroLoja
#str_numeroLoja.setter
def str_numeroLoja(self, str_numeroLoja):
self.__str_numeroLoja = str_numeroLoja
#property
def str_complementoLoja(self):
return self.__str_complementoLoja
#str_complementoLoja.setter
def str_complementoLoja(self, str_complementoLoja):
self.__str_complementoLoja = str_complementoLoja
#property
def str_bairroLoja(self):
return self.__str_bairroLoja
#str_bairroLoja.setter
def str_bairroLoja(self, str_bairroLoja):
self.__str_bairroLoja = str_bairroLoja
#property
def str_cidadeLoja(self):
return self.__str_cidadeLoja
#str_cidadeLoja.setter
def str_cidadeLoja(self, str_cidadeLoja):
self.__str_cidadeLoja = str_cidadeLoja
#property
def str_estadoLoja(self):
return self.__str_estadoLoja
#str_estadoLoja.setter
def str_estadoLoja(self, str_estadoLoja):
self.__str_estadoLoja = str_estadoLoja
#property
def str_ufLoja(self):
return self.__str_ufLoja
#str_ufLoja.setter
def str_ufLoja(self, str_ufLoja):
self.__str_ufLoja = str_ufLoja
#property
def str_cepLoja(self):
return self.__str_cepLoja
#str_cepLoja.setter
def str_cepLoja(self, str_cepLoja):
self.__str_cepLoja = str_cepLoja
#property
def str_telefoneUmLoja(self):
return self.__str_telefoneUmLoja
#str_telefoneUmLoja.setter
def str_telefoneUmLoja(self, str_telefoneUmLoja):
self.__str_telefoneUmLoja = str_telefoneUmLoja
#property
def str_telefoneDoisLoja(self):
return self.__str_telefoneDoisLoja
#str_telefoneDoisLoja.setter
def str_telefoneDoisLoja(self, str_telefoneDoisLoja):
self.__str_telefoneDoisLoja = str_telefoneDoisLoja
#property
def str_telefoneTresLoja(self):
return self.__str_telefoneTresLoja
#str_telefoneTresLoja.setter
def str_telefoneTresLoja(self, str_telefoneTresLoja):
self.__str_telefoneTresLoja = str_telefoneTresLoja
model.py
class Adress(db.Model):
__tablename__ = "tbl_afeet_dim_endereco_loja"
int_codEndLoja = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=True)
str_codigoLoja = db.Column((db.String(5)), db.ForeignKey("tbl_afeet_dim_loja.str_codigoLoja"))
str_enderecoLoja = db.Column(db.String(255))
str_numeroLoja = db.Column(db.String(10))
str_complementoLoja = db.Column(db.String(80))
str_bairroLoja = db.Column(db.String(30))
str_cidadeLoja = db.Column(db.String(30))
str_estadoLoja = db.Column(db.String(30))
str_ufLoja = db.Column(db.String(30))
str_cepLoja = db.Column(db.String(9))
str_telefoneUmLoja = db.Column(db.String(14))
str_telefoneDoisLoja = db.Column(db.String(14))
str_telefoneTresLoja = db.Column(db.String(14))
service.py
def update_adress(old_adress, new_adress):
old_adress.int_codEndLoja = new_adress.int_codEndLoja,
old_adress.str_codigoLoja = new_adress.str_codigoLoja,
old_adress.str_enderecoLoja = new_adress.str_enderecoLoja,
old_adress.str_numeroLoja = new_adress.str_numeroLoja,
old_adress.str_complementoLoja = new_adress.str_complementoLoja,
old_adress.str_bairroLoja = new_adress.str_bairroLoja,
old_adress.str_cidadeLoja = new_adress.str_cidadeLoja,
old_adress.str_estadoLoja = new_adress.str_estadoLoja,
old_adress.str_ufLoja = new_adress.str_ufLoja,
old_adress.str_cepLoja = new_adress.str_cepLoja,
old_adress.str_telefoneUmLoja = new_adress.str_telefoneUmLoja,
old_adress.str_telefoneDoisLoja = new_adress.str_telefoneDoisLoja,
old_adress.str_telefoneTresLoja = new_adress.str_telefoneTresLoja
db.session.commit()
views
def put(self, int_codEndLoja):
adress_id = adress_service.list_adress_id(int_codEndLoja)
if adress_id is None:
return make_response(jsonify("STORE NOT FOUND"), 400)
ad = adress_schema.AdressSchema()
validate = ad.validate(request.json)
if validate:
return make_response(jsonify(validate), 400)
else:
str_codigoLoja = request.json["str_codigoLoja"]
str_enderecoLoja = request.json["str_enderecoLoja"]
str_numeroLoja = request.json["str_numeroLoja"]
str_complementoLoja = request.json["str_complementoLoja"]
str_bairroLoja = request.json["str_bairroLoja"]
str_cidadeLoja = request.json["str_cidadeLoja"]
str_estadoLoja = request.json["str_estadoLoja"]
str_ufLoja = request.json["str_ufLoja"]
str_cepLoja = request.json["str_cepLoja"]
str_telefoneUmLoja = request.json["str_telefoneUmLoja"]
str_telefoneDoisLoja = request.json["str_telefoneDoisLoja"]
str_telefoneTresLoja = request.json["str_telefoneTresLoja"]
update_adress = adress.Adress(
str_codigoLoja=str_codigoLoja,
str_enderecoLoja=str_enderecoLoja,
str_numeroLoja=str_numeroLoja,
str_complementoLoja=str_complementoLoja,
str_bairroLoja=str_bairroLoja,
str_cidadeLoja=str_cidadeLoja,
str_estadoLoja=str_estadoLoja,
str_ufLoja=str_ufLoja,
str_cepLoja=str_cepLoja,
str_telefoneUmLoja=str_telefoneUmLoja,
str_telefoneDoisLoja=str_telefoneDoisLoja,
str_telefoneTresLoja=str_telefoneTresLoja)
adress_service.update_adress(adress_id, update_adress)
updated = adress_service.list_adress_id(adress_id)
return make_response(ad.jsonify(updated), 200)
Error
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for
SQL Server][SQL Server]Column, parameter, or variable #1: Cannot find data type 52. (2715) (SQLExecDirectW)')
[SQL: UPDATE tbl_afeet_dim_endereco_loja SET [int_codEndLoja]=?, [str_codigoLoja]=?, [str_enderecoLoja]=?,
[str_numeroLoja]=?, [str_complementoLoja]=?, [str_bairroLoja]=?, [str_cidadeLoja]=?, [str_estadoLoja]=?,
[str_ufLoja]=?, [str_cepLoja]=?, [str_telefoneUmLoja]=?, [str_telefoneDoisLoja]=?, [str_telefoneTresLoja]=?
WHERE tbl_afeet_dim_endereco_loja.[int_codEndLoja] = ?]
[parameters: (('XX',), ('XX',), ('XXX',), ('XX',), ('TESTE',), ('XXXXX',), ('XX
XX',), ('XX',), ('XX',), ('XXXXX',), ('XXXX',), ('XX',), 'XX', XX)]
Honestly, I don't know to find the problem, can someone help me?
Thank you 👊🏻

__slots__ does not reduce memory

I have the following class:
class DataGenerator(keras.utils.Sequence):
__slots__ = 'path','batch_size','dim','mode','split_proportion','indices','processes'
def __init__(self, path: str,
batch_size: int,
dim: Tuple[int] = (12,86,98),
mode: str = 'train',
split_proportion: float = None,
indices: List[List[int]] = None,
processes: Optional[int] = None):
self._path = path
self._mode = mode
self._split_proportion = split_proportion
self._k_indices = indices
self._dim = dim
self._batch_size = batch_size
self._processes = processes
# Change mode to retrieve from folders
if mode == 'validation':
mode = 'train'
self._im_path = os.path.join(self._path, mode,'image')
self._msk_path = os.path.join(self._path, mode,'mask')
If I instantiate it, it should not have the attribute dict since it contains slots. However:
training_data = DataGenerator(path = '/path', batch_size = 1,
mode = 'train', split_proportion = 0.1)
training_data.__dict__
{'_path': '/path',
'_mode': 'train',
'_split_proportion': 0.1,
'_k_indices': None,
'_dim': (12, 86, 98),
'_batch_size': 1,
'_processes': None,
'_im_path': '/path/train/image',
'_msk_path': '/path/train/mask'}
Additionally, if I check memory requirements, they seem to be higher than without the slots.
# with __slots__
sys.getsizeof(training_data)
112
sys.getsizeof(training_data.__dict__)
152
# without __slots__
sys.getsizeof(training_data)
56
sys.getsizeof(training_data.__dict__)
152

Error when referencing a class within its __init__ method

I am putting the (NMT Tensorflow code) inside a main class. The code base has two classes - 'Encoder' and 'Decoder'. They are referenced in their respective 'init' methods. However it raises an error - 'Undefined named Encoder'.
class TranslationModel(ModelBase):
pathToZip = tf.keras.utils.get_file('spa-eng.zip', origin='http://download.tensorflow.org/data/spa-eng.zip', extract=True)
pathToFile = os.path.dirname(pathToZip)+"/spa-eng/spa.txt"
def __init__(self,
batchSize = 64,
bufferSize = None,
numberOfBatches = None,
units = 1024,
vocabInputSize = None,
vocabTargetSize = None,
optimizer = tf.train.AdamOptimizer(),
dataSetPath = None,
inputTensor = None,
targetTensor = None,
inputLanguage = None,
targetLanguage = None,
maxLengthInput = None,
maxLengthTarget = None,
embeddingDimension = 256, *arg, **kwargs):
self.batchSize = 64
self.bufferSize = None
self.numberOfBatches = None
self.units = units
self.vocabInputSize = None
self.vocabTargetSize = None
self.optimizer = optimizer
self.dataSetPath = dataSetPath
self.targetTensor = targetTensor
self.inputTensor = inputTensor
self.inputLanguage = inputLanguage
self.targetLanguage = targetLanguage
self.maxLengthInput = maxLengthInput
self.maxLengthTarget = maxLengthTarget
self.embeddingDimension = embeddingDimension
super().__init__(*arg, **kwargs)
#OTHER FUNCTIONS HERE
class Encoder(tf.keras.Model):
def __init__(self, vocabSize, embeddingDimension, encoderUnits, batchSize):
super(Encoder, self).__init__() # Raises error - 'Undefined named Encoder'
#Other code here
class Decoder(tf.keras.Model):
def __init__(self, vocabSize, embeddingDimension, dec_units, batchSize):
super('Decoder', self).__init__() # Raises error - 'Undefined named Decoder'
## Other code
It's because when you have a class inside of another and you want to identify it, you should do it this way : OutterClass.InnerClass
it won't work if you just use : InnerClass
for your case it's TranslationModel.Encoder

Tensorflow reuse variables in different name scope

I've got the problem of reuse variable in different name scope. The code below separate source embedding and target embedding in two different spaces, What I want to do is to put source and target in the same space, reusing the variables in lookup table.
''' Applying bidirectional encoding for source-side inputs and first-word decoding.
'''
def decode_first_word(self, source_vocab_id_tensor, source_mask_tensor, scope, reuse):
with tf.name_scope('Word_Embedding_Layer'):
with tf.variable_scope('Source_Side'):
source_embedding_tensor = self._src_lookup_table(source_vocab_id_tensor)
with tf.name_scope('Encoding_Layer'):
source_concated_hidden_tensor = self._encoder.get_biencoded_tensor(\
source_embedding_tensor, source_mask_tensor)
with tf.name_scope('Decoding_Layer_First'):
rvals = self.decode_next_word(source_concated_hidden_tensor, source_mask_tensor, \
None, None, None, scope, reuse)
return rvals + [source_concated_hidden_tensor]
''' Applying one-step decoding.
'''
def decode_next_word(self, enc_concat_hidden, src_mask, cur_dec_hidden, \
cur_trg_wid, trg_mask=None, scope=None, reuse=False, \
src_side_pre_act=None):
with tf.name_scope('Word_Embedding_Layer'):
with tf.variable_scope('Target_Side'):
cur_trg_wemb = None
if None == cur_trg_wid:
pass
else:
cur_trg_wemb = self._trg_lookup_table(cur_trg_wid)
I want to make them as follows ,so there will only be one embedding node in the whole graph:
def decode_first_word_shared_embedding(self, source_vocab_id_tensor, source_mask_tensor, scope, reuse):
with tf.name_scope('Word_Embedding_Layer'):
with tf.variable_scope('Bi_Side'):
source_embedding_tensor = self._bi_lookup_table(source_vocab_id_tensor)
with tf.name_scope('Encoding_Layer'):
source_concated_hidden_tensor = self._encoder.get_biencoded_tensor(\
source_embedding_tensor, source_mask_tensor)
with tf.name_scope('Decoding_Layer_First'):
rvals = self.decode_next_word_shared_embedding(source_concated_hidden_tensor, source_mask_tensor, \
None, None, None, scope, reuse)
return rvals + [source_concated_hidden_tensor]
def decode_next_word_shared_embedding(self, enc_concat_hidden, src_mask, cur_dec_hidden, \
cur_trg_wid, trg_mask=None, scope=None, reuse=False, \
src_side_pre_act=None):
with tf.name_scope('Word_Embedding_Layer'):
cur_trg_wemb = None
if None == cur_trg_wid:
pass
else:
with tf.variable_scope('Bi_Side'):
cur_trg_wemb = self._bi_lookup_table(cur_trg_wid)
How to achieve this?
I solved it by using a dictionary to save the the weight matrix of embedding. A hint from https://www.tensorflow.org/versions/r0.12/how_tos/variable_scope/
One of the solutions is to save the variable_scope instance and reuse it.
def decode_first_word_shared_embedding(self, source_vocab_id_tensor, source_mask_tensor, scope, reuse):
with tf.name_scope('Word_Embedding_Layer'):
with tf.variable_scope('Bi_Side'):
source_embedding_tensor = self._bi_lookup_table(source_vocab_id_tensor)
shared_variable_scope = tf.get_variable_scope()
with tf.name_scope('Encoding_Layer'):
source_concated_hidden_tensor = self._encoder.get_biencoded_tensor(\
source_embedding_tensor, source_mask_tensor)
with tf.name_scope('Decoding_Layer_First'):
rvals = self.decode_next_word_shared_embedding(source_concated_hidden_tensor, source_mask_tensor, \
None, None, None, scope, reuse)
return rvals + [source_concated_hidden_tensor],
def decode_next_word_shared_embedding(self, enc_concat_hidden, src_mask, cur_dec_hidden, shared_variable_scope, \
cur_trg_wid, trg_mask=None, scope=None, reuse=False, \
src_side_pre_act=None):
with tf.variable_scope('Target_Side'):
cur_trg_wemb = None
if None == cur_trg_wid:
pass
else:
with tf.variable_scope(shared_variable_scope, reuse=True):
cur_trg_wemb = self._bi_lookup_table(cur_trg_wid)
And this is my demo code:
with tf.variable_scope('Word_Embedding_Layer'):
with tf.variable_scope('Bi_Side'):
v = tf.get_variable('bi_var', [1], dtype=tf.float32)
reuse_scope = tf.get_variable_scope()
with tf.variable_scope('Target_side'):
# some other codes.
with tf.variable_scope(reuse_scope, reuse=True):
w = tf.get_variable('bi_var', [1], dtype=tf.float32)
print(v.name)
print(w.name)
assert v==w
Output:
Word_Embedding_Layer/Bi_Side/bi_var:0
Word_Embedding_Layer/Bi_Side/bi_var:0

id type in mongoengine objects

I use mongoengine and when I call a class mongoengine give me error
for example when I use : Site.objects()
I get this
TypeError: id must be an instance of (str, unicode, ObjectId), not <type 'dict'>
and it's my class definition :
class Site(db.Document):
siteURL = db.URLField('''required = True''')
name = db.StringField(required = True)
pages = db.ListField(ReferenceField(Page))
siteId = db.IntField(required = True , unique = True )
views = db.IntField(required = True , default = 0)
visitors = db.IntField(required = True , default = 0)
stat_by_hour = LimitedListField(EmbeddedDocumentField(HourStat))
weekday_stat = db.ListField(EmbeddedDocumentField(WeekdayStat))
os = db.ListField(ReferenceField(OS))
countries = db.ListField(ReferenceField(Country))
cities = db.ListField(ReferenceField(City))
browsers = db.ListField(ReferenceField(Browser))
resolutions = db.ListField(ReferenceField(Resolution))
mostVisitedDays = db.SortedListField(EmbeddedDocumentField(MostVisitedDay) , ordering="visitors")

Categories

Resources