Skip to content

Instantly share code, notes, and snippets.

@yuuniverse4444
Created January 8, 2014 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuuniverse4444/8313233 to your computer and use it in GitHub Desktop.
Save yuuniverse4444/8313233 to your computer and use it in GitHub Desktop.
This is my sample python code. It contains two files. The first file automatically generated a .config file for running program based on user's input parameters. the second file used a dictionary to run a vlookup and find those rows that cannot be matched.
#This file automatically output a .config file based on user's input parameters.
OutputFileName = "zhou1.txt" # This is where the output file will be saved
Total_budget = 200000
Longest_Delay_Week = 5 # Expected longest weeks of delay; For example if you expect the delay will not be more than 35 days then the value would be 5 weeks
NumberofGroup = 4 # Can switch betwee 2 and 4, indicating if 1/2 or 1/4 test groups
### Only one of the two below boolean variable can be True, the other has to be false since it now only support two variations: either budget chang or Number of Groups change
AdjustNumberofGroup = True # If true, we will have senarios for 4 Geo groups and 2 Geo groups. If Set as false, we will double the budget and fix Number of Group as either 2 or 4
AdjustTotalBudget = not AdjustNumberofGroup # if AdjustNumberofGroup = False, then we will have two different budget senarios, one given budget and one given budget times budget_ratio )
Budget_Ratio = 1.5 #(If AdjustTotalBudget is True, then the other variation budget will be given budget times Budget Ratio)
def base_write(file_name,scenario=1,total_spend = 1000, family_num=1,num_gma_groups=1,impact_delay=7,period_length=[28,35,42]):
myfile=open(file_name,"a")
myscenario="scenario"+str(scenario)
myfile.write("section = "+myscenario+ "\n")
myfile.write("family.num: " +str(family_num) + "\n")
myfile.write("num.gma.groups: " + str(num_gma_groups)+ "\n")
myfile.write("impact.delay: " + str(impact_delay) + "\n")
myfile.write("ad.spend.multiplier: " + "1" + "\n")
myfile.write("period.length: ")
loop = len(period_length)
for i in range(loop):
if(i<loop-1):
myfile.write(str(period_length[i])+", ")
else:
myfile.write(str(period_length[i])+"\n")
myfile.write("ad.spend.diff: ")
import pdb
### pdb.set_trace() was used for debugging
numerator=period_length[1]-impact_delay
denominator = period_length[1]
spend =total_spend*denominator/numerator
for i in range(loop):
if(i==0):
myfile.write("0, ")
if(i==1):
myfile.write(str(spend))
if(i==2):
myfile.write(", " +str(spend))
myfile.write("\n")
myfile.write("ad.spend.diff.type: ")
for i in range(loop):
if(i==0):
myfile.write("0, ")
if(i==1):
myfile.write("+") # By default, we assume first is increase budget
if(i==2):# If there is an observational period,
myfile.write(", -")
myfile.write("\n")
myfile.write("\n")
myfile.close()
## End of base_write function defined
## Specify senarios
if(AdjustNumberofGroup):#if you want adjust
if(NumberofGroup>3): #It equals to 4
NumberofGroup1 = NumberofGroup
NumberofGroup2 = NumberofGroup/2
else:# It equals to 2
NumberofGroup1 = NumberofGroup
NumberofGroup2 = NumberofGroup*2
else:#If you don't want to adjust
NumberofGroup1 = NumberofGroup2 =NumberofGroup
if(AdjustTotalBudget):#if you want adjust budget
Total_budget1 = Total_budget
Total_budget2 = Total_budget * Budget_Ratio
else:
Total_budget1 = Total_budget2 = Total_budget
## Output the First section
if(AdjustNumberofGroup): # Change numberofGroup
Label = "1/"+str(NumberofGroup1) + " - OBS, " + "1/" + str(NumberofGroup2)+" - OBS, " + "1/" + str(NumberofGroup1) + " - NoOBS, " + "1/"+ str(NumberofGroup2)+" - NoOBS"
else:
partition = "1/"+ str(NumberofGroup) # A fix number, will not change, change only Budget label
#print partition + str(Total_budget1/1000) + "k -OBS", k indicating thousand here
Label =partition + "-"+ str(Total_budget1/1000) + "k-OBS, " +partition + "-"+ str(Total_budget2/1000) + "k-OBS , " +partition + "-"+ str(Total_budget1/1000) + "k-NoOBS, " +partition + "-"+ str(Total_budget2/1000) + "k -NoOBS"
myfile = open(OutputFileName,"a")
myfile.write("section = global.vars \n")
myfile.write("metric.field.name: sales \n" +
"metric.label: Off-line Sales \n" +
"family.nums: 1, 2,3, 4 \n" +
"legend.title: Test Fraction - Observation Period\n" +
"family.labels: " + Label + "\n" +
"num.assignment.iterations: 200 \n\n\n" )
myfile.close()
## Output all different senarios
for j in range(1,Longest_Delay_Week*4+1):
delay =(j % Longest_Delay_Week)*7
if(delay ==0):
delay = Longest_Delay_Week*7
if(j<Longest_Delay_Week*2+1):# first half casse, with Observation ( two test periods with one observational period)
period = [28,21+delay,21+delay]
if( j < Longest_Delay_Week+1):
Current_Total_budget = Total_budget1
numofGmaGroup = NumberofGroup1#1-7 is 1/4
else:
numofGmaGroup = NumberofGroup2
Current_Total_budget = Total_budget2
else:
period = [28,21+delay]# second half case, without Observation
if( j < Longest_Delay_Week*3+1):#After three period
Current_Total_budget = Total_budget1
numofGmaGroup = NumberofGroup1
else:
numofGmaGroup = NumberofGroup2
Current_Total_budget = Total_budget2
familynum = int((j-0.1)/Longest_Delay_Week)+1
base_write(OutputFileName,scenario=j,total_spend=Current_Total_budget,family_num = familynum,num_gma_groups=numofGmaGroup,impact_delay = delay, period_length=period)
## This file use a dictionary to map City name to Metro code and find out those that cannot be matched.
File=open("C:/Users/zhouyu/Desktop/Geo Work/City spot.txt","r")
File_City=open("C:/Users/zhouyu/Desktop/Geo Work/Metro City List.txt","r")
File_output=open("C:/Users/zhouyu/Desktop/Geo Work/output.txt","w")
cityName=[]
for line in File_City:
a=line.rstrip("\n")
cityName.append(a)
dic={}
## Define a dictionary for matching
for line in File:
a=line.rstrip("\n")
b=a.split("\t")
key=b[0].lower()# make everything to be lower case
value=int(b[1])
dic[key]=value
File.close()
unmatchCity=[]
for i in range(len(cityName)):
currentCity=cityName[i]
lower_case_city=currentCity.lower()
matchUP=0
for k,v in dic.iteritems():
if lower_case_city.find(k)>-1:
outputStr=currentCity+'\t'+k+"\t"+str(dic[k])
File_output.write(outputStr)
File_output.write("\n")
matchUP=1
if matchUP==0:## if it is unmatched
unmatchCity.append(currentCity)
File_output.close()
File_City.close()
########################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment