Page 1 of 2

Undocumented string processing in expressions : shall we document ?

Posted: Sat May 15, 2021 9:23 am
by openBrain
Hi all,

There is a cool feature in expressions about string processing that is, AFAIK, undocumented.
Actually is appears that expressions are able to process strings in the old fashion Python way (% formatting) using the '<< >>' delimiters.
A typical example would be

Code: Select all

<<The length of the cube is : %s>> % Box.Length
that would return (with a default cube)

Code: Select all

The length of the cube is : 10.0 mm
Only one specifier is possible inside delimiters, but string concatenating is also possible using '+', and all Python specifiers are available.
So with a default cube and 3 decimals as a precision

Code: Select all

<<Box (%s) : >> % Box.Label + <<L = %g mm : >> % Box.Length + <<W = %.2f mm>> % Box.Width
would expand in

Code: Select all

Box (Cube) : L = 10 mm : W = 10.00 mm
This to me opens a bunch of new possibilities. Obvious one is for part engraving as demonstrated below:
ExprString.gif
ExprString.gif (108.13 KiB) Viewed 4569 times
I also attach the file, so you can play with (I used this possibility both in conditional expressions in spreadsheet, and in the text of the shapestring) -- you may have to set the font path in the shapestring so file works --.

Now the question is : can it be considered as a really consciously coded feature that we will maintain, and so we can document ? Or is this just an unexpected side effect of the expression engine parser that we are absolutely not sure it will work again in the future (so better not document) ?

Anyway, great kudos to @Forthman that bring this possibility into the light :
Forthman wrote: Fri Apr 30, 2021 9:49 am

Re: Undocumented string processing in expressions : shall we document ?

Posted: Mon May 17, 2021 7:44 am
by onekk
I think that spekaing of Spreadsheet is an "expected feature", as it is a "standard" behaviour that you could refer to a value with column and row "numbers".

The side effect that you could refer also with "Object properties" in a CAD environment is in my opinion a "must have" feature.

Having also the ability to use "Python string expressions" is a good thing, are all the "fomratting" options implemented?

i.e if I write:

Code: Select all

<<value %.3f>> % Box.Length 
it will work?


Regards

Carlo D.

Re: Undocumented string processing in expressions : shall we document ?

Posted: Mon May 17, 2021 2:40 pm
by openBrain
onekk wrote: Mon May 17, 2021 7:44 am I think that spekaing of Spreadsheet is an "expected feature", as it is a "standard" behaviour that you could refer to a value with column and row "numbers".

The point is only about ability to use %-style string formatting in Expressions. ;)

i.e if I write:

Code: Select all

<<value %.3f>> % Box.Length 
it will work?
That's it. The whole Python string formatting is available.

Re: Undocumented string processing in expressions : shall we document ?

Posted: Mon May 17, 2021 3:43 pm
by onekk
Sorry for having not explained better, in my precendet post.

I intended that referring to something other than text in a spreadsheet cell, is a "standard behaviour", but that it is very interesting that you could refer other than the usual way (column, row reference), also with "Object properties".

Thanks for the clarification, it is something to note as it will be useful in some cases.

Regards

Carlo D.

Re: Undocumented string processing in expressions : shall we document ?

Posted: Mon May 17, 2021 4:39 pm
by openBrain
onekk wrote: Mon May 17, 2021 3:43 pm I intended that referring to something other than text in a spreadsheet cell, is a "standard behaviour", but that it is very interesting that you could refer other than the usual way (column, row reference), also with "Object properties".
The topic is neither about Spreadsheet specifically (a spreadsheet cell is basically an expression line) nor about the current capability of Expression engine to reference other objects properties. It's purely about the (AFAIK undocumented) ability to use string formatting in expressions. ;)

Re: Undocumented string processing in expressions : shall we document ?

Posted: Mon May 17, 2021 8:10 pm
by onekk
I have to see how spreadsheet and expression engine is implemented, thanks for clarification.

I have totally misunderstood the topics sorry.

Regards

Carlo D.

Re: Undocumented string processing in expressions : shall we document ?

Posted: Thu Jun 03, 2021 12:45 pm
by openBrain
I made a draft documentation proposal in the wiki : https://wiki.freecadweb.org/Expressions ... nipulation

Re: Undocumented string processing in expressions : shall we document ?

Posted: Sat Jan 22, 2022 9:39 pm
by paullee
Re-directed from a discussion in Help, this is very useful, thanks for the wiki ! :D

Re: Undocumented string processing in expressions : shall we document ?

Posted: Sat Jan 22, 2022 10:03 pm
by TheMarkster
I say document it. If it breaks later and for whatever reason does not get fixed, then it can always be undocumented again.

Re: Undocumented string processing in expressions : shall we document ?

Posted: Sun Jan 23, 2022 7:14 am
by openBrain
TheMarkster wrote: Sat Jan 22, 2022 10:03 pm I say document it.
It's documented for about 6 months now. ;)