The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Make a string by repeating specified characters/st
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

AKazak

Russia
1228 Posts

Posted - 02/17/2021 :  11:39:02 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
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

YimingChen

1684 Posts

Posted - 02/17/2021 :  3:32:44 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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

Edited by - YimingChen on 02/17/2021 3:32:57 PM
Go to Top of Page

AKazak

Russia
1228 Posts

Posted - 02/17/2021 :  3:51:49 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

YimingChen

1684 Posts

Posted - 02/17/2021 :  5:02:56 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

AKazak

Russia
1228 Posts

Posted - 02/18/2021 :  08:44:26 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

YimingChen

1684 Posts

Posted - 02/18/2021 :  09:22:38 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

AKazak

Russia
1228 Posts

Posted - 02/18/2021 :  12:44:01 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

AKazak

Russia
1228 Posts

Posted - 07/04/2022 :  05:39:34 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

YimingChen

1684 Posts

Posted - 07/05/2022 :  09:10:06 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Use this link:
https://www.originlab.com/doc/en/python/tools/Set-Column-Values#Specifying_Input_and_Output_Types

Go to Top of Page

AKazak

Russia
1228 Posts

Posted - 07/05/2022 :  09:46:28 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page

aplotnikov

Germany
170 Posts

Posted - 07/05/2022 :  11:31:29 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
#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. :)

Edited by - aplotnikov on 07/05/2022 5:38:14 PM
Go to Top of Page

AKazak

Russia
1228 Posts

Posted - 07/06/2022 :  01:06:55 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000