wtorek, 20 marca 2012

in bigger loop in big loop

# Auto XXX if action == 'XXX': Relation = self.GetAXXXRelation(cid, cType) if AXXXRelation: product_AXXX = self.actionModel.ProductManager.GetProductAttrByName("AXXX") if not self.Company[cid].Customer[cust_ID].PriceSet[product_AXXX.ID].priceType == self.priceInterface.PRICETYPE_GLOBAL: for c_record in AXXXRelation: if c_record[0] == tour_id: action = 'AXXX' break
it is a big part of very complex class.

GetAXXXRelation returns a list of tuples to consider - in most cases this list is big.
It contains basic information about transactions that have to be handled in other way.

This part is located in big loop that iterates and transforms all transactions.

Isn't it more nice to use python built-in functions? it might work in the same ( AFAIR 'in' is optimized ) .

Lets look on this example
XXXTransaction id list [2,4,6,8...] allTransaction id list [1,2,3,4...]
now comes funny part, because lists are sorted.
Checking if first elements are in XXX lists is quite easy but it is more expensive on the end of list :)

Hint for solution - it will help with code understanding and might be faster!


# Auto XXX if action == 'XXX': #that have to be performed on higher level - to not iterate it on each transaction #I've placed it only to show that now it is list of id not tuples AXXXRelation = [x[0] for x in self.GetAXXXRelation(cid, cType)] if AXXXRelation: product_AXXX = self.actionModel.ProductManager.GetProductAttrByName("AXXX") if not self.Company[cid].Customer[cust_ID].PriceSet[product_AXXX.ID].priceType == self.priceInterface.PRICETYPE_GLOBAL: if tour_id in AXXXRelation: # for c_record in AXXXRelation: # if c_record[0] == tour_id: action = 'AXXX' # break

poniedziałek, 16 stycznia 2012

exception - no one knows!

try:
  try:
    self.DBHelper.SubCompany_MappingDeleteStdByCID(cursor, ...)
    if self.Mode == SUBCOMPANYMAPPINGDIALOG_MODE_ADD:
      self.DBHelper.SubCompany_MappingAdd(cursor, ...)
    else:
      self.DBHelper.SubCompany_MappingReplace(cursor, ...)
      DB.commit()
  except:
    DB.rollback()   
finally:
  cursor.close()    

From my point of view I'd love to know that exception occured!

poniedziałek, 24 października 2011

more lines == more profit

  def _SortItemizedDataListById_(self, a, b):
    if a.Transaction_Number < b.Transaction_Number:
      return -1
    
    return int(a.Transaction_Number > b.Transaction_Number)


Isn't easyer to fo it using cmp build-in function??

wtorek, 19 lipca 2011

  1. Make sure that there are no duplicates!!!
    select distinct tc.COMPANY_ID, tc.NAME from 
           TAB_TACC_RECHNUNGEN tr,  
           tab_tacc_company tc 
    where tr.COMPANY_ID = tc.COMPANY_ID and 
           UPPER(tc.NAME) like UPPER('%scc%') 
    group by tc.COMPANY_ID, tc.NAME 
    order by tc.NAME
    
    
    tab_tacc_technungen is needed because we need only companies with invoices - but why join and not exist?
    Why distinct and group by?
    tc.company_id is PK of tab_tacc_company - so after elimination of 'join', distinct is not needed any more.

poniedziałek, 18 lipca 2011

piątek, 15 lipca 2011

List to String in Python

  1. Using build in __str__ and str.strip
     if isinstance(tour_id, list):
                tour_id_str = str(tour_id).strip("[]")
    
    

    The fact is that tour_id contains only ints so 'casting' to string have to be done befor using str.join

  2. Using for loop
    tmpCompanyListStr = ""
            for tmpCompany in cid:
                tmpCompanyListStr += "%s, " %(tmpCompany)
            tmpCompanyListStr = tmpCompanyListStr.rstrip(', ')
    
    It is hard to say if author had knowlage about str.join but he was using str.rstrip.