API

Ulib

ulib is a library for processing the unicode character or string more pythonic.

Warning

This module not finished, don’t use it on product environment.

Support:

  • Chinese
toolkit.ulib.cnlen(us)[source]

Calculate the length of unicode string. length of chinese character is 2.

Parameters:us – Unicode string.
Returns:Bool Value
toolkit.ulib.f2h(u)[source]
Parameters:us – Unicode character or string.
Returns:
toolkit.ulib.has_cn(us)[source]

Test if the unicode string contain an unicode chinese character.

Parameters:us – Unicode string.
Returns:Bool Value
toolkit.ulib.is_cn(u)[source]

Test if the unicode character is a chinese character.

Parameters:u – Unicode character.
Returns:Bool Value
toolkit.ulib.is_digital(u)[source]

Test if the unicode character is a digital.

Parameters:u – Unicode character.
Returns:Bool Value

Tpl

The tpl module has some helper function implements base on jinja2 for human.

toolkit.tpl.create_env_by_folder(folder)[source]

Create jinja2 environment with jinja2.FileSystemLoader()

Parameters:folder – folder path.
Returns:jinja2 environment object.
toolkit.tpl.get_template(template_path)[source]

Get template object from absolute path.

For example, the template has:

{% macro add(a, b) -%}
    {{a + b}}
{%- endmacro %}

And call the macro as template object method.

tpl = get_template('template.tpl')
print(tpl.add(1, 2))

>>> 3
Parameters:template_path – template absolute path.
Returns:template object
toolkit.tpl.proxy_register(register_funtion)[source]

Proxy a function to a register function.

Parameters:register_funtion – a function need to proxy.
Returns:a proxy wrapper
toolkit.tpl.register_filter(excepted, filter_function=None)[source]

Add default filter function to template rendered environment. Register provide 3-way to add a function to environment and use on template.

register_filter('lower', str.lower)

@register_filter
def lower(s):
    return s.lower()

@register_filter('lower')
def xxx_lower():
    return s.lower()
toolkit.tpl.register_test(excepted, filter_function=None)[source]

Add default test function to template rendered environment. Register provide 3-way to add a function to environment and use on template.

register_test('digital', lambda v: type(v) in (int, float))

@register_test
def digital(v):
    return type(v) in (int, float)

@register_test('digital')
def test_if_it_is_digital():
    return type(v) in (int, float)
toolkit.tpl.render(template_path, output_file=None, **kwargs)[source]

Render jinja2 template file use absolute path.

Usage

A simple template as follow:

This is a test template

{{ title }}

Then write render code by single line.

render('template.tpl', name='toolkit')
Parameters:
  • template_path – absolute file path of template file.
  • output_file – the path of output file.
  • kwargs – keyword arguments for template.