Tuesday, December 22, 2015

Python: Pitfalls Filled

# list to string, separated by comma
lis = [1,2,3]
print ",".join(map(str, lis))

# print system time in formated way - logging time
from time import gmtime, strftime
strftime("%Y-%m-%d %H:%M:%S", gmtime())

#appending dict or merging two dicts
b={}
d=b.copy()
d.update({'list':0})

#joining dict with string
yook = {'a':0,'img':['url2','url2']}
print str(yook) + "asd"

#pretty print (json - dict)
a= {u'response': {u'url': u'http://www.faceplusplus.com/static/img/demo/9.jpg', u'img_id': u'2081892e329727f350337509ff939a29', u'img_width': 540, u'session_id': u'd6fdce4b591e43dabbd8d31f68db0a46', u'face': [{u'attribute': {u'gender': {u'confidence': 99.7639, u'value': u'Male'}, u'age': {u'range': 9, u'value': 23}, u'race': {u'confidence': 99.9662, u'value': u'White'}, u'smiling': {u'value': 13.909}}, u'face_id': u'42dd2bdec1f8fae250a8d4c6b5f11837', u'tag': u'', u'position': {u'eye_left': {u'y': 44.727248, u'x': 58.625}, u'center': {u'y': 46.730245, u'x':59.537037}, u'width': 5.0, u'mouth_left': {u'y': 47.659401, u'x': 58.412037}, u'height': 7.356948, u'mouth_right': {u'y': 47.838692, u'x': 60.201481}, u'nose':{u'y': 46.782834, u'x': 59.463519}, u'eye_right': {u'y': 44.922616, u'x': 60.493333}}}], u'img_height': 367}}

from pprint import pprint
pprint(a)

It is prettier, isn't it?

{u'response': {u'face': [{u'attribute': {u'age': {u'range': 9,
                                                  u'value': 23},
                                         u'gender': {u'confidence': 99.7639,
                                                     u'value': u'Male'},
                                         u'race': {u'confidence': 99.9662,
                                                   u'value': u'White'},
                                         u'smiling': {u'value': 13.909}},
                          u'face_id': u'42dd2bdec1f8fae250a8d4c6b5f11837',
                          u'position': {u'center': {u'x': 59.537037,
                                                    u'y': 46.730245},
                                        u'eye_left': {u'x': 58.625,
                                                      u'y': 44.727248},
                                        u'eye_right': {u'x': 60.493333,
                                                       u'y': 44.922616},
                                        u'height': 7.356948,
                                        u'mouth_left': {u'x': 58.412037,
                                                        u'y': 47.659401},
                                        u'mouth_right': {u'x': 60.201481,
                                                         u'y': 47.838692},
                                        u'nose': {u'x': 59.463519,
                                                  u'y': 46.782834},
                                        u'width': 5.0},
                          u'tag': u''}],
               u'img_height': 367,
               u'img_id': u'2081892e329727f350337509ff939a29',
               u'img_width': 540,
               u'session_id': u'd6fdce4b591e43dabbd8d31f68db0a46',
               u'url': u'http://www.faceplusplus.com/static/img/demo/9.jpg'}}

No comments:

Post a Comment