Skip to content

Instantly share code, notes, and snippets.

@datasciencemonkey
Last active March 18, 2021 15:51
Show Gist options
  • Save datasciencemonkey/7afc0c7c220edf22b328e21c571c770d to your computer and use it in GitHub Desktop.
Save datasciencemonkey/7afc0c7c220edf22b328e21c571c770d to your computer and use it in GitHub Desktop.
open src node run py scripts and make results globally available - CAS/Viya/Model Studio
# import libraries
import os
import swat
import pandas as pd
import toml
with open('/tmp/config.toml','r') as f:
server_config = toml.load(f)
cas_settings = server_config.get('cas35')
#your CASHOST
host = cas_settings.get('host')
#set env variables - change to what works for you
os.environ['CAS_CLIENT_SSL_CA_LIST'] = cas_settings.get('CAS_CLIENT_SSL_CA_LIST')
os.environ['TKESSL_OPENSSL_LIB']= cas_settings.get('TKESSL_OPENSSL_LIB')
# Run your python script
print('starting to exec')
os.system("python /tmp/script.py")
#import your results from your process - just make sure you have a results df that conforms to what Model Studio expects
dm_scoreddf=pd.read_csv('/tmp/result.csv')
print(f'there are a total of {len(dm_scoreddf)} records')
# I'm running the following two lines to respect what the model comparison node expects
# To get more info on this - see below - basically these P_variable(s) are needed for the model comparsion node
# https://go.documentation.sas.com/?cdcId=vdmmlcdc&cdcVersion=8.5&docsetId=vdmmlref&docsetTarget=p0dzscw6302tphn1btd5wv7v6bjp.htm&locale=en
dm_scoreddf['P_CHURN0']= 0
dm_scoreddf['P_CHURN1']= 1
print('complete with df generation')
# Connecting and uploading to CAS
#change this to fit your server. Doesnt matter if your use toml, ini or simple py config files so you dont expose creds.
user = cas_settings.get('user')
pswd = cas_settings.get('password')
conn = swat.CAS(host,5570,user,pswd)
try:
# uploading to CAS and setting the object ref to a var tbl
conn.upload(dm_scoreddf,casout=dict(name='mytbl',caslib='casuser',replace=True))
tbl = conn.CASTable('mytbl')
# if the table exists, drop it - since you now have a better/newer table
if (conn.table.tableExists(caslib="public",name="mytbl")['exists'] ==2):
conn.droptable(caslib="public",table="mytbl");
print('Existing Table dropped')
# promote this table to public - so that this persists
conn.table.promote(name=tbl,drop=True, targetlib='public')
except:
# fill in with your exception handling logic
print("some cas error")
finally:
# ensure you close your session
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment