One Star

tSystem component to execute python script

Hi all,
I have to execute python script by tSystem component.  Can Talend Studio store the script file .py or only the python code in Metadata or Documentation? I want that both job and script file are stored on Studio and not in a external folder. So, when I export the Job I will not worry about the script file .py
thanks all
9 REPLIES
Fifteen Stars

Re: tSystem component to execute python script

Maybe you could store the code for the file inside the Job and then during the Job write the code out to a script file, which you then run using the tSystem component and then delete using the tFileDelete. This way you can parameterise the script file and do not need to worry about having files set up wherever you want to run the Job.
Rilhia Solutions
One Star

Re: tSystem component to execute python script

How can I store the code in Studio? which components should I use?
thanks very much
Fifteen Stars

Re: tSystem component to execute python script

You can store the script in a context variable if you like. The String context variables should be able to hold your script. Alternatively you can a tJava component to store the script text in several globalMap key/value pairs.
You can use tSetGlobalVar component to interact with the globalMap variable, or interact with it using a tJava component. The code below is how you can store and retrieve String values....
To store....
globalMap.put("MyVarKey", "My text");
To retrieve....
((String)globalMap.get("MyVarKey"));
You can save ANY variable in the globalMap HashMap, hence you need to cast the value on the way out.
You can use the retrieval code in tMap components or tFile components for when you want to use it.
Rilhia Solutions
Seventeen Stars

Re: tSystem component to execute python script

hi all,
try & add py file with documentation (add documentation) in metatdata.
hope it helps
regards
laurent
One Star

Re: tSystem component to execute python script

@rhall_2.0 - It's good strategy, but in this way I must to fix all " and ' characters in the python code.
@kzone - Hi,
 I have added the file py in Documentation, but how can I call it from tSystem?
Seventeen Stars

Re: tSystem component to execute python script

it had been stored in your repository (local ans svn repo) under documentation folder.
So you can call it from that repo
but it just a trick , not an optimize solution Smiley Happy
regards
Fifteen Stars

Re: tSystem component to execute python script

I'm not entirely sure you can refer to the Documentation objects at runtime. If you can great, I have learnt something :-)
If you can't and don't want to have to manipulate " and ' chars, you could read the file in as a byte array using a tFileInputRaw and output it as a byte array using a tFileOutputRaw component. This is a bit trickier and will require Java convert your types. It will also be meaningless to look at. But won't require any manual manipulation of " and ' chars. 
Rilhia Solutions
Seventeen Stars

Re: tSystem component to execute python script

unfortunatly it's just to keep python script beside other development.
but once compile , it will not be accessible as it. keep it in a cloud like drpbox and call it from that point.
Fifteen Stars

Re: tSystem component to execute python script

I was being very literal and looking to store the file inside the job. As kzone suggests, it is probably better to store it in a place you can get from anywhere like the cloud.
In my previous post I recommended reading the file in as a byte array and writing it out as a byte array. I should have explained that I meant at design time, read it in as byte array. Then store the byte array in a context variable. Then write it out from the context variable after having processed it back into a byte array. This will work as I have done something very similar with image files.
But kzone's suggestion is a lot easier and means you can edit your script file without touching the job.
Rilhia Solutions