Selesai

Python string wrapper class with token replacement

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 [url removed, login to view] '$[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 [url removed, login to view](foo) throws a "coercing to Unicode" exception.

If I use "[url removed, login to view]('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.

Keahlian: Pemasangan kode program, Shell Script

Lihat lebih lanjut: python token replacement, python token, write a bit or bit, what does novice, two string problem, true hire, to string, the string problem, text to string, string the, string problems, string problem, string i, string find c, python programmer for project hire, python programmer for hire, python hire, python find, python expert hire, programmer dictionary

Tentang Pemberi kerja:
( 27 ulasan ) United States

ID Proyek: #2790457

Diberikan kepada:

vesselin

See private message.

$135 USD dalam 14 hari
(6 Ulasan)
4.6

2 freelancer menawar dengan rata-rata $68 untuk pekerjaan ini

r4z

See private message.

$0 USD dalam 14 hari
(0 Ulasan)
0.0