| T O P I C R E V I E W |
| AKazak |
Posted - 02/17/2021 : 11:39:02 AM OriginPro 2021b (64-bit) Beta 5 9.8.5.105 Windows 7 Pro x64 SP1
Greetings!
Does LT have a neat command to generate a string by repeating specified characters/stings?
For example, I want to repeat "=" character 13 times and save it to a string variable.
Thank you.
--- Andrey |
| 11 L A T E S T R E P L I E S (Newest First) |
| AKazak |
Posted - 07/06/2022 : 01:06:55 AM quote: Originally posted by aplotnikov
#include <origin.h>
string repnchar(string strCh, int n) {
char *src=strCh.GetBuffer(1);
int val=(int) src[0];
strCh.ReleaseBuffer();
string strRes;
char *buf=strRes.GetBuffer(n);
memset(buf, val, n);
strRes.ReleaseBuffer();
return strRes;
}
An improved version:
#include <origin.h>
string repnchar(string strCh, int n) {
string strTmp(strCh.GetAt(0), n);
return strTmp;
}
PS. A prophecy: after a while Origin users will be experienced enough with Python to find out that Origin is just an optional environment for Python. :)
Great optimization :)
--- Andrey |
| aplotnikov |
Posted - 07/05/2022 : 11:31:29 AM #include <origin.h>
string repnchar(string strCh, int n) {
char *src=strCh.GetBuffer(1);
int val=(int) src[0];
strCh.ReleaseBuffer();
string strRes;
char *buf=strRes.GetBuffer(n);
memset(buf, val, n);
strRes.ReleaseBuffer();
return strRes;
}
An improved version:
#include <origin.h>
string repnchar(string strCh, int n) {
string strTmp(strCh.GetAt(0), n);
return strTmp;
}
PS. A prophecy: after a while Origin users will be experienced enough with Python to find out that Origin is just an optional environment for Python. :) |
| AKazak |
Posted - 07/05/2022 : 09:46:28 AM quote: Originally posted by YimingChen
Use this link: https://www.originlab.com/doc/en/python/tools/Set-Column-Values#Specifying_Input_and_Output_Types
Got it! Thanks.
--- Andrey |
| YimingChen |
Posted - 07/05/2022 : 09:10:06 AM Use this link: https://www.originlab.com/doc/en/python/tools/Set-Column-Values#Specifying_Input_and_Output_Types
|
| AKazak |
Posted - 07/04/2022 : 05:39:34 AM quote: Originally posted by YimingChen
This is necessary for embedded Python in Origin especially when you use it to set column values. It needs to know whether it's a scalar function or a vector function. See the explanation in the page: https://www.originlab.com/doc/python/Calling-Python-Functions-from-Column-Formula-and-LabTalk#Specifying_Input_and_Output_Types
If you select menu Connectivity->Open Examples.py..., you can see the full list of docstring.
James
The hyperlink above is broken. Please update he hyperlink.
Thank you.
--- Andrey |
| AKazak |
Posted - 02/18/2021 : 12:44:01 PM quote: Originally posted by YimingChen
This is necessary for embedded Python in Origin especially when you use it to set column values. It needs to know whether it's a scalar function or a vector function. See the explanation in the page: https://www.originlab.com/doc/python/Calling-Python-Functions-from-Column-Formula-and-LabTalk#Specifying_Input_and_Output_Types
If you select menu Connectivity->Open Examples.py..., you can see the full list of docstring.
James
Got it! Thanks.
--- Andrey |
| YimingChen |
Posted - 02/18/2021 : 09:22:38 AM This is necessary for embedded Python in Origin especially when you use it to set column values. It needs to know whether it's a scalar function or a vector function. See the explanation in the page: https://www.originlab.com/doc/python/Calling-Python-Functions-from-Column-Formula-and-LabTalk#Specifying_Input_and_Output_Types
If you select menu Connectivity->Open Examples.py..., you can see the full list of docstring.
James |
| AKazak |
Posted - 02/18/2021 : 08:44:26 AM quote: Originally posted by YimingChen
Docstring is used to specify the input/output variable type, check below: https://www.originlab.com/doc/python/Calling-Python-Functions-from-Column-Formula-and-LabTalk
Yes, you can use string as the first argument.
James
Dear James,
Is this a strict requirement to specify input and output types? I noticed that https://www.originlab.com/doc/python/Calling-Python-Functions-from-Column-Formula-and-LabTalk provides many examples with unspecified input and output types.
Besides, where do I find a complete list of docstring data type identifiers, for example, F, S, i, etc.?
Thank you.
--- Andrey |
| YimingChen |
Posted - 02/17/2021 : 5:02:56 PM Docstring is used to specify the input/output variable type, check below: https://www.originlab.com/doc/python/Calling-Python-Functions-from-Column-Formula-and-LabTalk
Yes, you can use string as the first argument.
James |
| AKazak |
Posted - 02/17/2021 : 3:51:49 PM quote: Originally posted by YimingChen
You can simply define such function with Python and call from LT. Open menu Connectivity->Open Default Python Functions... then put in the code below and save.
def repeat(str, n):
'''s:si'''
return str*n
Then you can try LT script:
py.repeat(=,13)$=;
James
Dear James,
Great solution! What is '''s:si''' ? Also, can I get the same with multiple character core, for example, "a,b,c"?
--- Andrey |
| YimingChen |
Posted - 02/17/2021 : 3:32:44 PM You can simply define such function with Python and call from LT. Open menu Connectivity->Open Default Python Functions... then put in the code below and save.
def repeat(str, n):
'''s:si'''
return str*n
Then you can try LT script:
py.repeat(=,13)$=;
James |