python – Replace string_concat() with equivalent

Question:

I'm new to Python and I accepted to participate in a more or less old project, which was using Python 2.7 until now. Earlier this year, this version was deprecated and now I can't install anything with pip anymore, so I need to update the entire project to Python 3.x. The first string_concat() to this is the string_concat() functions, which used to belong in the django.utils.translation module until Django 1.9, and have since been deprecated. Here is a code example in which it appears:

'ESPESSURAF' : {
    u'NOME':u'ESPESSURAF',
    u'NOME_HTML': string_concat(NOMES_HTML['ESPESSURAF'], u' <em>t&nbsp;<sub> f</sub> </em> [m]'),
    u'UNIDADE':u' <em>t&nbsp;<sub> f</sub>[m] </em>',
    u'COMENTARIO':u'',
    u'TOOLTIP' : u''
},

All other occurrences are very similar to this one, so a solution to one should apply to all. I read somewhere that format_lazy() can be a good replacement, but I don't have enough experience to know how to do this adaptation. I need to get this fixed ASAP, would be enormously grateful for any help.

Answer:

According to the string_concat deprecation topic on Django's issue tracker, all occurrences of string_concat can be replaced with format_lazy('{}'*len(params), *params) .

Example of polyfill, if you didn't want to rename the function:

def string_concat(params):
    return format_lazy('{}'*len(params), *params)

References:

Scroll to Top