#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    42
    Rep Power
    2

    Interpreting this def


    Hi there, I am fairly new to python and am not used at all with reading and interpreting it.. I've gotten an assignment at my new job to just document what happens in the script, I stumbled across this and can't really interpret it!

    Anyone that feel that they can explain to me whats going on here?

    Code:
    def getPortolioProficentreMapping(uploadDate): #get portfolio and profit center
    
        sql_q = "SELECT child_name FROM pf_tree_mb WHERE name = 'ATCM_#x#_STO' AND "
        sql_q += "upload_date = '%s' " % uploadDate.string('%Y-%m-%d')
    
        db = database.connection("DSN=armsrc.sebank.se")
        tbl = db.select(sql_q)
        portfolioList = []
    
        for row in tbl:
            portfolioList.append(row['child_name'])
    
        sql_q = "SELECT name,child_name FROM pf_tree_mb WHERE upload_date = '%s' AND " % uploadDate.string('%Y-%m-%d')
        sql_q += "child_name IN('%s') AND " % "','".join(portfolioList)
        sql_q += "type = 'profit_centre' AND name != 'MB'"
    
        tbl = db.select(sql_q)
        
        db.close()
    
        portfolioProfitcentreDict = {}
        profitCentres = []
    
        for row in tbl:
            portfolioProfitcentreDict[row['child_name']] = row['name']
            if row['name'] not in profitCentres:
                profitCentres.append(row['name'])
        
        return sorted(portfolioProfitcentreDict.keys()),profitCentres,portfolioProfitcentreDict
        
    def getPositions(uploadDate,portfolios):
    
        sql_q  = "SELECT id,portfolio_name,system,trader,pos_type,pos_xml,user_def_xml FROM dbo.arms_position WHERE "
        sql_q += "portfolio_name IN('%s' )" % "','".join(portfolios)
        sql_q += "AND upload_date = '%s' "  % uploadDate.string('%Y-%m-%d')
        sql_q += "AND archived IS NULL AND cust_id = 1"
    
        db = database.connection("DSN=armsrc.sebank.se")        
        tbl = db.select(sql_q)
        db.close()
        
        return tbl
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0
    Code:
    def getPortolioProficentreMapping(uploadDate): #get portfolio and profit center
    
        sql_q = "SELECT child_name FROM pf_tree_mb WHERE name = 'ATCM_#x#_STO' AND "  # A string that will be
        sql_q += "upload_date = '%s' " % uploadDate.string('%Y-%m-%d')               # updated in this line, uploadDate is the function parameter.
    
        db = database.connection("DSN=armsrc.sebank.se")     # sets database.connection to var db
        tbl = db.select(sql_q)             # sets tb1 to a variable corresponding to sql_q, note that the function parameter will play a role here
        portfolioList = []    # Creates an empty list
    
        for row in tbl:
            portfolioList.append(row['child_name'])   # I Guess db.select(sql_q) is a dict. 'child_name' is a key, and this line will append it's value to the list we created portfolioList = []
    
    
    
    
        sql_q = "SELECT name,child_name FROM pf_tree_mb WHERE upload_date = '%s' AND " % uploadDate.string('%Y-%m-%d') 
        sql_q += "child_name IN('%s') AND " % "','".join(portfolioList)
        sql_q += "type = 'profit_centre' AND name != 'MB'"
    
    #Well, this is just a string reasignment. Where in each line he sums the strings and the final result would be:
    #"SELECT name, child_name  FROM pf_tree_mb WHERE upload_date = 'some date' AND chil_name in 'the value(s) appended to portfolioList' AND type = 'profit_centre' AND name != 'MB'
    
    
        tbl = db.select(sql_q)   # updates tb1 with the new value of sql_q
        
        db.close()      # saves the db
    
        portfolioProfitcentreDict = {}       # empty dict
        profitCentres = []      # empty list
    
        for row in tbl:
            portfolioProfitcentreDict[row['child_name']] = row['name']  # It does this: portfolioProfitcentreDict = {'child_name':'name'}
            if row['name'] not in profitCentres:   # checks if the dict's key value isn't in the profitCentres list
                profitCentres.append(row['name']) # And if is not, he appends it's value to it
        
        return sorted(portfolioProfitcentreDict.keys()),profitCentres,portfolioProfitcentreDict 
    
    #Returns:
    #A list with portfolioProfitcentreDict's keys
    #The profitCentres list
    #The full portfolioProfitcentreDict
    #Notice the sorted function
        
    def getPositions(uploadDate,portfolios):   # Think you can understand this?
    
        sql_q  = "SELECT id,portfolio_name,system,trader,pos_type,pos_xml,user_def_xml FROM dbo.arms_position WHERE "
        sql_q += "portfolio_name IN('%s' )" % "','".join(portfolios)                   
        sql_q += "AND upload_date = '%s' "  % uploadDate.string('%Y-%m-%d')
        sql_q += "AND archived IS NULL AND cust_id = 1"
    
        db = database.connection("DSN=armsrc.sebank.se")        
        tbl = db.select(sql_q)  
        db.close()
        
        return tbl
    I did it kinda fast and i am too a Python newb. Lemme know if helped....

    Comments on this post

    • ChristopherL agrees : Great way of answering! :)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    42
    Rep Power
    2
    Originally Posted by J Dihzy
    Code:
    def getPortolioProficentreMapping(uploadDate): #get portfolio and profit center
    
        sql_q = "SELECT child_name FROM pf_tree_mb WHERE name = 'ATCM_#x#_STO' AND "  # A string that will be
        sql_q += "upload_date = '%s' " % uploadDate.string('%Y-%m-%d')               # updated in this line, uploadDate is the function parameter.
    
        db = database.connection("DSN=armsrc.sebank.se")     # sets database.connection to var db
        tbl = db.select(sql_q)             # sets tb1 to a variable corresponding to sql_q, note that the function parameter will play a role here
        portfolioList = []    # Creates an empty list
    
        for row in tbl:
            portfolioList.append(row['child_name'])   # I Guess db.select(sql_q) is a dict. 'child_name' is a key, and this line will append it's value to the list we created portfolioList = []
    
    
    
    
        sql_q = "SELECT name,child_name FROM pf_tree_mb WHERE upload_date = '%s' AND " % uploadDate.string('%Y-%m-%d') 
        sql_q += "child_name IN('%s') AND " % "','".join(portfolioList)
        sql_q += "type = 'profit_centre' AND name != 'MB'"
    
    #Well, this is just a string reasignment. Where in each line he sums the strings and the final result would be:
    #"SELECT name, child_name  FROM pf_tree_mb WHERE upload_date = 'some date' AND chil_name in 'the value(s) appended to portfolioList' AND type = 'profit_centre' AND name != 'MB'
    
    
        tbl = db.select(sql_q)   # updates tb1 with the new value of sql_q
        
        db.close()      # saves the db
    
        portfolioProfitcentreDict = {}       # empty dict
        profitCentres = []      # empty list
    
        for row in tbl:
            portfolioProfitcentreDict[row['child_name']] = row['name']  # It does this: portfolioProfitcentreDict = {'child_name':'name'}
            if row['name'] not in profitCentres:   # checks if the dict's key value isn't in the profitCentres list
                profitCentres.append(row['name']) # And if is not, he appends it's value to it
        
        return sorted(portfolioProfitcentreDict.keys()),profitCentres,portfolioProfitcentreDict 
    
    #Returns:
    #A list with portfolioProfitcentreDict's keys
    #The profitCentres list
    #The full portfolioProfitcentreDict
    #Notice the sorted function
        
    def getPositions(uploadDate,portfolios):   # Think you can understand this?
    
        sql_q  = "SELECT id,portfolio_name,system,trader,pos_type,pos_xml,user_def_xml FROM dbo.arms_position WHERE "
        sql_q += "portfolio_name IN('%s' )" % "','".join(portfolios)                   
        sql_q += "AND upload_date = '%s' "  % uploadDate.string('%Y-%m-%d')
        sql_q += "AND archived IS NULL AND cust_id = 1"
    
        db = database.connection("DSN=armsrc.sebank.se")        
        tbl = db.select(sql_q)  
        db.close()
        
        return tbl
    I did it kinda fast and i am too a Python newb. Lemme know if helped....
    This is more then enough my friend, truly thankful, great way of commenting like that for me!

    Many thanks and I hope to be able to help you in the future!

IMN logo majestic logo threadwatch logo seochat tools logo