# -*- coding: utf-8 -*-


import collections
import locale


LOCALE = locale.getpreferredencoding()


def unimapping(arg):
    """
    Mapping object to unicode string.

    :type arg: collections.Mapping
    :param arg: mapping object

    :rtype: unicode
    :return: mapping object as unicode string
    """

    if not isinstance(arg, collections.Mapping):
        raise TypeError(
            'expected collections.Mapping, {} received'.format(type(arg).__name__)
        )

    result = []
    for k, v in arg.items():
        k = convert(k)

        result.append(u'{}: {}'.format(
            k,
            convert(v))
        )

    return u'{{{}}}'.format(u', '.join(result))


def uniiterable(arg):
    """
    Iterable object to unicode string.

    :type arg: collections.Iterable
    :param arg: iterable object

    :rtype: unicode
    :return: iterable object as unicode string
    """

    if not isinstance(arg, collections.Iterable):
        raise TypeError(
            'expected collections.Iterable, {} received'.format(type(arg).__name__)
        )

    templates = {
        list: u'[{}]',
        tuple: u'({})'
    }

    result = []
    for i in arg:
        result.append(convert(i))

    return templates.get(type(arg), templates[tuple]).format(u', '.join(result))


def convert(obj, encoding=LOCALE):
    """
    Covert any object to unicode string.

    :param obj: any object
    :type encoding: str
    :param encoding: codec for encoding unicode strings
                     (locale.getpreferredencoding() by default)

    :rtype: unicode
    :return: any object as unicode string
    """

    if isinstance(obj, unicode):
        func = lambda x: u"u'{}'".format(x)
    elif isinstance(obj, str):
        func = lambda x: u"'{}'".format(x.decode(encoding))
    elif isinstance(obj, (int, float)):
        func = lambda x: unicode(x)
    elif isinstance(obj, collections.Mapping):
        func = unimapping
    elif isinstance(obj, collections.Iterable):
        func = uniiterable
    else:
        func = lambda x: x

    return func(obj)

@@ -501,12 +501,9 @@
 for 
-k, v
+i
  in 
@@ -527,111 +527,49 @@
     
-k = convert(k)%0A%0A        result.append(u'%7B%7D: %7B%7D'.format(%0A            k,%0A            convert(v))%0A        
+result.append(u': '.join(map(convert, i))
 )%0A%0A 
@@ -1561,80 +1561,154 @@
     
-if isinstance(obj, unicode):%0A        func = lambda x: u%22u'%7B%7D'%22.format(x)
+func = lambda x: u%22u'%7B%7D'%22.format(x)%0A%0A    if isinstance(obj, unicode):%0A        # skip if condition, because unicode is a iterable type%0A        pass
 %0A   
@@ -1821,16 +1821,28 @@
 e(obj, (
+type(None), 
 int, flo
@@ -1996,32 +1996,32 @@
 ions.Iterable):%0A
+
         func = u
@@ -2034,45 +2034,8 @@
 able
-%0A    else:%0A        func = lambda x: x
 %0A%0A  

Fix bug with date and datetime types.