def build_gun_list(): gunlist = [] for gun in Handguns.select(): newgun = handgun(model=gun.model, manufacturer=gun.manufacturer, caliber=gun.caliber, size=gun.size, guntype=gun.guntype, action=gun.action, capacity=gun.capacity, minprice=gun.minprice, maxprice=gun.maxprice, comments=gun.comments, photo=gun.photo) gunlist.append(newgun) return gunlist
def pickup(clstype): if clstype == carbine.carbine: return carbine.carbine() elif clstype == handgun.handgun: return handgun.handgun()
def produce(self): print "type =%s" %self.guntype if self.guntype == carbine.carbine: return carbine.carbine() elif self.guntype == handgun.handgun: return handgun.handgun()
class gunFactory(object): def __init__(self, guntype): self.guntype = guntype def produce(self): print "type =%s" %self.guntype if self.guntype == carbine.carbine: return carbine.carbine() elif self.guntype == handgun.handgun: return handgun.handgun() def client(gun): gun.fire() if __name__ == "__main__": # style - 1 client(carbine.carbine()) #client(handgun()) client(handgun.handgun()) # style - 2 retObj = pickup(carbine.carbine) client(retObj) retObj = pickup(handgun.handgun) client(retObj) # style - 3 # add the double quote or not , it's two different types # one is string # one is class # factory = gunFactory("handgun.handgun") factory = gunFactory(handgun.handgun) gun = factory.produce() client(gun)
def parse_wiki_page(): gunslist = [] handguns = [] with open("gunlist.txt") as f: gunlist_txt = f.read().replace("*", "") gunlist = gunlist_txt.split('\n---\n') for item in gunlist: if "###" in item or "Manufacturer" not in item: continue #skip headings semiline = "" manufacturer, model, price, caliber, size, action, semi, capacity, comments = "", "", "", "", "", "", False, "", "" lines = item.split('\n') for line in lines: if 'Manufacturer:' in line: manufacturer = line.partition(':')[2] elif 'Model:' in line: model = line.partition(': ')[2] elif 'Price:' in line or 'MSRP:' in line or 'Street:' in line: price = line.partition(': ')[2] elif 'Caliber:' in line: caliber = line.partition(': ')[2] elif 'Size:' in line: size = line.partition(': ')[2] elif 'Action:' in line: action = line.partition(': ')[2] elif 'Capacity:' in line: capacity = line.partition(': ')[2] elif 'Type:' in line: semiline = line.partition(': ')[2] elif 'Comments:' in line: comments = str(line.partition(': ')[2]) maxprice = get_max_price(price) minprice = get_min_price(price) if "full" in size.lower(): size = "Fullsize" elif "sub" in size.lower(): size = "Subcompact" elif "compact" in size.lower(): size = "Compact" else: size = "Other" if "semi" in semiline.lower(): guntype = "Semiauto" elif "revolver" in semiline.lower(): guntype = "Revolver" else: continue if "striker" in action.lower(): action = "Striker" elif "sa/da" in action.lower() or "da/sa" in action.lower(): action = "SA/DA" elif "dao" in action.lower(): action = "DAO" elif "sa" in action.lower() or "single" in action.lower(): action = "Single" elif "da" in action.lower() or "double" in action.lower(): action = "Double" else: action = "Other" if not comments: comments = "None" newgun = handgun(model=model, manufacturer=manufacturer, caliber=caliber, size=size, guntype=guntype, action=action, capacity=capacity, minprice=minprice, maxprice=maxprice, comments=comments, photo="") handguns.append(newgun) if get_gun(model) is None: add_gun_to_db(newgun) else: update_gun(model, newgun) return handguns