Skip to content

Instantly share code, notes, and snippets.

@theresaanna
Forked from danlurie/gist:9079415
Last active August 29, 2015 13:56
Show Gist options
  • Save theresaanna/9079551 to your computer and use it in GitHub Desktop.
Save theresaanna/9079551 to your computer and use it in GitHub Desktop.
def ttestconv( oldfile, gen_code ):
newfile = os.path.basename(oldfile)
#Read in Variables (Case Sensitive)
print gen_code
#gen_code = raw_input("Please Enter Gender Code (Case Sensitive):")
#age_code = raw_input("Please Enter Age Code (Case Sensitive):")
ttest_code = raw_input("Please Enter T Test Code (Case Sensitive):")
#Declare Open Files as Global Variables
global ins
global nf
# Open Input and Output Files
ins =open(oldfile, "r" )
nf = open("NG_"+newfile, "wb")
# Declare Local Variables
match=[]
match1=[]
match2=[]
match3=[]
match4=[]
match5=[]
prevline=[]
# Declare ELSE IF variable, i.e. once IF has been declared for the age statements, each subsequent if statement is ELSEIF
elseifvar=0;
# Iterate through Text
for line in ins:
#Declare Line as String
codestring = "".join(line)
# Match Variables to Text and Print Accordingly
# Gender
if gen_code in line and " 1" in line:
nf.write("IF @Gender = 'M' \n")
#Age
if age_code in line and ">=" in line:
codePattern_rawscore = re.compile(r'(\d{1,3})')
match = re.findall(codePattern_rawscore, codestring)
if len(match)==2:
m_ind1=0
m_ind2=1
if len(match)==3:
m_ind1=1
m_ind2=2
if age_code in line and "<=" in line and elseifvar == 0:
codePattern_rawscore = re.compile(r'(\d{1,3})')
match1 = re.findall(codePattern_rawscore, codestring)
if len(match1)==2:
m1_ind1=0
m1_ind2=1
if len(match1)==3:
m1_ind1=1
m1_ind2=2
nf.write("\tIF @Age >="+match[m_ind1]+'.'+match[m_ind2]+" AND @Age<="+match1[m1_ind1]+'.'+match1[m1_ind2]+"\n\t\tselect @t_score =\n\t\tcase\n")
elseifvar=1;
elif age_code in line and "<=" in line and elseifvar==1 :
codePattern_rawscore = re.compile(r'(\d{1,3})')
match2 = re.findall(codePattern_rawscore, codestring)
if len(match2)==2:
m2_ind1=0
m2_ind2=1
if len(match2)==3:
m2_ind1=1
m2_ind2=2
nf.write("\tELSE IF @Age >="+match[m_ind1]+'.'+match[m_ind2]+" AND @Age<="+match2[m2_ind1]+'.'+match2[m2_ind2]+"\n\t\tselect @t_score =\n\t\tcase\n")
#TTest Variable
if ttest_code in line and '>=' in line:
codePattern_rawscore = re.compile(r'( \d{1,3})')
match3 = re.findall(codePattern_rawscore, codestring)
if len(match3)==3:
m3_ind1=0
m3_ind2=1
if len(match3)==4:
m3_ind1=1
m3_ind2=2
nf.write("\t\twhen @Raw_Score>"+match3[m3_ind1]+" then"+match3[m3_ind2]+"\n")
if ttest_code in line and '==' in line:
codePattern_rawscore = re.compile(r'( \d{1,3})')
match4 = re.findall(codePattern_rawscore, codestring)
if len(match4)==3:
m4_ind1=0
m4_ind2=1
if len(match4)==4:
m4_ind1=1
m4_ind2=2
nf.write("\t\twhen @Raw_Score="+match4[m4_ind1]+" then"+match4[m4_ind2]+"\n")
#if int(float(match4[0])) == 0:
# nf.write("\t\telse 0\n\t\tend\n\n")
if ttest_code in line and '<' in line:
codePattern_rawscore = re.compile(r'\.*(\d{1,3})')
match5 = re.findall(codePattern_rawscore, codestring)
if len(match5)==3:
m5_ind1=0
m5_ind2=1
if len(match5)==4:
m5_ind1=1
m5_ind2=2
nf.write("\t\twhen @Raw_Score<"+match5[m5_ind1]+" then"+match5[m5_ind2]+"\n")
#nf.write("\t\telse 0\n\t\tend\n\n")
if ttest_code not in line and ttest_code in prevline:
nf.write("\t\telse 0\n\t\tend\n\n")
#Gender
if gen_code in line and " 2" in line:
nf.write("IF @Gender = 'F' \n")
elseifvar=0
prevline = line;
#Close Files
nf.close()
ins.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment