Find Jobs
Hire Freelancers

Python string wrapper class with token replacement

$100-150 USD

En curso
Publicado hace más de 11 años

$100-150 USD

Pagado a la entrega
I'm sort of a novice Python programmer so I'm struggling to write a pretty simple wrapper class. The goal is a string-like class with automatic macro replacements. It maintains a dictionary of variables and does the replacement at evaluation time. Variables are evaluated using $[NAME] syntax. Thus, given pseudocode like this: text = 'The quick brown fox jumped over the $[ADJECTIVE] dogs' variables = { 'ADJECTIVE': 'lazy' } sentence = vstring(text, variables) print sentence The result would be 'The quick brown fox jumped over the lazy dogs' Note an important point - the macro replacement must be deferred because the dictionary of variables is dynamic. So replacement is not done at construction time but at evaluation time. I think this would be a very quick job for a true Python expert. I may have more and bigger work for such a person if I can find one. I provide a sample program ready to go which needs only the wrapper class filled in. See the attached file and detailed description. ## Deliverables The goal here is that these objects can be used just like strings. There are two approaches I can think of: one would involve subclassing the actual string class and the other would just "have" the source string and find a way to behave like a string in all ways, maybe by implementing __eq__() and __add__() and so on. I don't care what method is used as long it's elegant and correct. I've left in some code from my own attempt but feel free to rewrite the class entirely. The interpolate method is known to work; possibly it could be improved but that's not the priority. For simplicity of testing the default set of variables is taken from the environment. So if the environment contains XYZ=123, we should get: % python [login to view URL] '$[XYZ]' $[XYZ]='123' **_PLEASE NOTE:_** I've gotten a number of responses saying "All you need to do is implement the __str__() method". I should have made it clearer that it's not quite that simple. I'd already tried it before posting this contest and found a number of problems. Remember, the project description says the goal is a "vstring" class which behaves "just like a string" but with macro replacements. Yes, __str__ gets a good bit of the way there but not all the way. For example: Addition (foo + "bar") will fail if foo is a vstring and vstring does not inherit from str. That particular problem can be easily fixed by implementing __add__() etc but then I find that using a vstring as a keyword parameter eg def func(bar=foo): Still throws an exception. And if foo refers to a pathname then [login to view URL](foo) throws a "coercing to Unicode" exception. If I use "[login to view URL]('bar')" I get, predictably, "vstring object has no attribute 'endswith'". And so on. I felt like I could chase this stuff forever or I could hire someone who knows Python really well to knock out a class that allows a vstring to be used wherever a string could be. I suspect this will involve delegation using __getattr__ or a direct subclass of str or basestring.
ID del proyecto: 2790457

Información sobre el proyecto

2 propuestas
Proyecto remoto
Activo hace 11 años

¿Buscas ganar dinero?

Beneficios de presentar ofertas en Freelancer

Fija tu plazo y presupuesto
Cobra por tu trabajo
Describe tu propuesta
Es gratis registrarse y presentar ofertas en los trabajos
Adjudicado a:
Avatar del usuario
See private message.
$135 USD en 14 días
5,0 (6 comentarios)
4,6
4,6
2 freelancers están ofertando un promedio de $68 USD por este trabajo
Avatar del usuario
See private message.
$0 USD en 14 días
0,0 (0 comentarios)
0,0
0,0

Sobre este cliente

Bandera de UNITED STATES
United States
5,0
27
Miembro desde ene 28, 2006

Verificación del cliente

¡Gracias! Te hemos enviado un enlace para reclamar tu crédito gratuito.
Algo salió mal al enviar tu correo electrónico. Por favor, intenta de nuevo.
Usuarios registrados Total de empleos publicados
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Cargando visualización previa
Permiso concedido para Geolocalización.
Tu sesión de acceso ha expirado y has sido desconectado. Por favor, inica sesión nuevamente.