test if an input string starts with a python expression

T

Torsten Mohr

Hi,

i'd like to test if an input string starts with a python expression
and also where that expression ends. An example:

a_func(3*7, '''abc''') +5 pls some more

The first part until (inclusive) the 5 should be found as an expression
and the length of that string should also be detected.


Background is that i want to embed some python expressions in a text
and i want to evaluate them later.
It is possible that the embedded expressions span several lines.

Alternatively, is it possible to define a start- and an end-marker
that define the embedded expression and find the expression using
a regular expression?
If the expression contains strings, these strings could contain
the end-marker which should not be found inside strings.

Example:

start: <
end: >

< some_func(r">")>


Can anybody tell me how to do this best?

Can i do this using just modules from the python library?


Thanks for any hints,
Torsten.
 
R

r0g

Torsten said:
Hi,

i'd like to test if an input string starts with a python expression
and also where that expression ends. An example:

a_func(3*7, '''abc''') +5 pls some more

The first part until (inclusive) the 5 should be found as an expression
and the length of that string should also be detected.


Background is that i want to embed some python expressions in a text
and i want to evaluate them later.
It is possible that the embedded expressions span several lines.

Alternatively, is it possible to define a start- and an end-marker
that define the embedded expression and find the expression using
a regular expression?


That's the easy way and will work for most cases if you use uncommon
delimiters. I tend to use '<<<' and '>>>' for things like this but you
can make them as obscure as you like.


If the expression contains strings, these strings could contain
the end-marker which should not be found inside strings.


You could build in escaping but that complicates things quite quickly,
assuming this is for your own private use and you won't be dealing with
huge rafts of data from elsewhere or using this to control radiotherapy
machines etc, that's probably overkill.


The re module should do everything you need and is part of the standard lib.

['sat', 'mat']

Hope this helps,

Roger.
 
R

Richard Thomas

Torsten said:
i'd like to test if an input string starts with a python expression
and also where that expression ends.  An example:
a_func(3*7, '''abc''') +5 pls some more
The first part until (inclusive) the 5 should be found as an expression
and the length of that string should also be detected.
Background is that i want to embed some python expressions in a text
and i want to evaluate them later.
It is possible that the embedded expressions span several lines.
Alternatively, is it possible to define a start- and an end-marker
that define the embedded expression and find the expression using
a regular expression?

That's the easy way and will work for most cases if you use uncommon
If the expression contains strings, these strings could contain
the end-marker which should not be found inside strings.

You could build in escaping but that complicates things quite quickly,
assuming this is for your own private use and you won't be dealing with
huge rafts of data from elsewhere or using this to control radiotherapy
machines etc, that's probably overkill.

The re module should do everything you need and is part of the standard lib.

['sat', 'mat']

Hope this helps,

Roger.

Use the parser module.
....
SyntaxError: invalid syntax

Quite intensive as you have to compile every initial substring to find
the longest one but at least you use Python's own definition of an
expression.

Chard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top