[ python ] collections.defaultdict

>>> import collections
>>> row={}; row = collections.defaultdict(str, row)
>>> row['x']
''

Usually, a Python dictionary throws a KeyError if you try to get an item with a key that is not currently in the dictionary.

ref : https://stackoverflow.com/questions/5900578/how-does-collections-defaultdict-work

[ jinja2 ] render template

Example

import jinja2

 

path_to_template = '/path/to/templates'

env = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(path_to_template)), extensions=['jinja2.ext.i18n'])

env.install_gettext_callables(_gettext, _ngettext) # for i18n extensions, Installs the given gettext and ngettext callables into the environment as globals. They are supposed to behave exactly like the standard library’s gettext.ugettext() and gettext.ungettext() functions.

t = env.from_string(open(path_to_template).read()) # Load a template from a string. This parses the source given and returns a Template object

row = {'foo': 'bar'}

body = t.render(**row)

 

_gettext and _ngettext are declared below

def _gettext(text):

return text

def _ngettext(singular, plural, n):

pass

• gettext: translate a single string
• ngettext: translate a pluralizable string

 

Actually above example works, but the below example shows better picture of how FileSystemLoader works.

template_env = jinja2.Environment(loader=jinja2.FileSystemLoader('mydir'))
template_env.get_template('foo/bar.html')

 

ref : http://jinja.pocoo.org/docs/2.9/api/,

http://jinja.pocoo.org/docs/2.9/extensions/#jinja-extensions,

https://stackoverflow.com/questions/9641317/jinja2-filesystemloader-load-all-subdirectories

[ python ] dictionary update() method

update()

dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female', 'Name': 'Changed'}

dict.update(dict2)
print "Value : %s" % dict

# Value : {'Age': 7, 'Name': 'Changed', 'Sex': 'female'}

setdefault()

dict = {'Name': 'Zara', 'Age': 7}

print "Value : %s" % dict.setdefault('Age', None) # Value : 7
print "Value : %s" % dict.setdefault('Sex', None) # Value : None

ref : https://www.tutorialspoint.com/python/dictionary_update.htm,
https://www.tutorialspoint.com/python/dictionary_setdefault.htm

boto : A Python interface to Amazon Web Services

Installation

pip install boto

Example :

sending email to `Amazon Simple Email Service` (SES) using boto.ses

import boto

boto.ses.SESConnection(aws_access_key_id=AWS_ACCESS_KEY_ID,boto.ses.SESConnection(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, debug=0)

ses.send_email(sender, subject, body, [recipient], format='html')

http://boto.cloudhackers.com/en/latest/ref/ses.html