def backup(path): backup_path = path + ".backup" try: curr_bytes = stat(path).st_size except OSError: RESULT("No file to backup...") return 0 try: back_bytes = stat(backup_path).st_size except OSError: RESULT("No backup file, creating one") b = open(backup_path, 'w') b.write("") b.close() back_bytes = stat(backup_path).st_size if curr_bytes > back_bytes: back = open(backup_path, 'w') curr = open(path, 'r') # Copy current into backup if gt # print "Backing", path, "into", backup_path for line in curr: print(line, file=back) back.close() curr.close()
def getIndexInput(max, multiple=False): ind_array = "" isNum = False inRange = False while not (isNum and inRange): try: if multiple: ind_array = [ int(x) - 1 for x in input( 'Please pick numbers (with spaces)(0 to cancel): '). split() ] else: ind_array = [ int(input('Please pick a number (0 to cancel): ')) - 1 ] isNum = True for ind in ind_array: if ind < max: inRange = True else: inRange = False RESULT("Out of range, please try again") break except ValueError: isNum = False RESULT("Not a number, please try again") if len(ind_array) == 1: return ind_array[0] return ind_array
def removeprompt(self): name = input('Food Name: ').strip() if name in self.foodmap: del self.foodmap[name] RESULT("[Removed]", file=sys.stderr) else: RESULT("[Does not exist!]", file=sys.stderr) self.write()
def display(self, date=None, lastSeven=False): # Dates in order availdates = sorted(self.weightlogmap.keys()) if len(availdates) == 0: RESULT("Nothing logged.") return if date == None: date = availdates[0] #Start point index = availdates.index(date) #Last7 if lastSeven: index -= 7 if index < 0: index = 0 print(Weight.printheader(), file=sys.stderr) #print header # Print all dates from that day forward # For today it is a single date for dated in availdates[index:]: w = self.weightlogmap[dated] print("%s\t%s %s" % (dated, w.printout(), (" <--" if date == dated else " ")), file=sys.stderr)
def insertprompt(self): name = input('Food Name: ').strip() if name in self.foodmap: RESULT("[Food already exists!]", file=sys.stderr) print(self.foodmap[name].printout(header=True), file=sys.stderr) exit(-1) self.insert(name)
def checkLastNight(self): if self.yesterday in self.weightlogmap: w = self.weightlogmap[self.yesterday] if w.night == -1: INFO("Last night not set") if (ynprompt('Set last night? ')): self.logprompt(self.yesterday, isDay=False) return True RESULT("[Ignoring last night]") if ynprompt('Ignore permanently? '): self.log(self.yesterday, 0, False) RESULT("[Ignoring last night permanently]") return True RESULT("[Temporarily ignored last night, moving on..]\n") return False #nothing changed
def choice(array, compare_to=0, multiple=False): # Encaps. '''Takes a list of objects and returns one''' def def_choice(array, compare_to, isTuple=False, isYem=True): choose = 1 if isYem: print(Yemek.printFullHeader()) for x in array: scale = 1 if isTuple: scale = x[1] x = x[0] choose_s = "%2d: " % choose sobj = x if isYem: sobj = x.scaled(scale) print(sobj.printout(pre=choose_s)) else: print("%s%s" % (choose_s, (sobj if not isTuple else sobj + ' ' + str(scale)))) if compare_to != 0: # Whatever is compared MUST have equality method overloaded, else standard type if sobj == compare_to: result("Found definite match!") return x choose += 1 ind = getIndexInput(len(array), multiple) if ind == -1: return -1 res = array[ind] if isTuple: res = array[ind][0] # dont want scale print("") print(("Chose: %s" % res) if not isYem else res.printout(pre="Chose: ")) return res # Main print("") if len(array) == 0: RESULT("No matches") return -1 isTuple = isinstance(array[0], tuple) isYem = isinstance(array[0][0] if isTuple else array[0], Yemek) return def_choice(array, compare_to, isTuple, isYem)
def updateprompt(self): # Print details if exists, else insert, else return close match name = self.info(input('Food: ').strip()) # Name exists by now, or prog exited if name in self.foodmap: edit = input('\nEdit? ') if edit[0].lower() != 'y': exit(-1) else: RESULT("\n[New Food: \"%s\"]" % name, file=sys.stderr) self.insert(name)
def userSingleListPrompt(pretext, message_array, str_array): opts = None lowr_str_array = [x.lower() for x in str_array] max_tries = 3 while opts == None and max_tries > 0: inp = input("%s - %s : [%s]? " % (pretext, ' / '.join(message_array), '/'.join(str_array))) inp += ' ' inp = inp[0].lower() if inp not in lowr_str_array: RESULT("Invalid input.", end="") max_tries -= 1 else: opts = inp return inp
def callProgs(self): Config.resolveAllPaths() #import pdb #pdb.set_trace() if self.weight: wl = WeightLog() if self.insert: wl.checkGaps() return if self.remove: RESULT("TODO: Not implemented") return if self.list: wl.display() # default first return if self.plot: xy = XYGraph(False) startdate="" for date in sorted(wl.weightlogmap.keys()): if startdate=="": startdate=date continue days_since = daysSince(startdate,date) total = days_since # print date,total w = wl.weightlogmap[date] if w.morn>0: xy.addPoint(total,w.morn,"x") if w.night>0: xy.addPoint(total+.5,w.night,"x") Printer(xy) return RESULT("Nothing to do") return if self.food: fl = FoodLogger(testmode=self.test) if self.insert: fl.log(self.opts) return if self.remove: RESULT("Not implemented") return if self.list: date = fl.date if self.opts!="": count = int(self.opts) while count >0: date = previousDay(date) count -= 1 fl.showTotals(date, showPie=True) return if self.plot: RESULT("Not implemented") return if self.suggest: fl.makeTotals(fl.date) p = fl.pie lowcal=False tag="" try: p.allow_kc = int(self.opts) except ValueError: opts = self.opts.lower().split() if len(opts)!=1: tag= ' '.join(opts[:-1]) p.allow_kc = int(opts[-1]) else: tag=self.opts if tag=="lowcal": lowcal=True pass s = Suggest(fl.foodlist, p.allow_kc, p.allow_carb, p.allow_prot, p.allow_fat, tag) if lowcal:s.lowCalHighPF() else:s.suggestSomething() return
p.allow_kc = int(self.opts) except ValueError: opts = self.opts.lower().split() if len(opts)!=1: tag= ' '.join(opts[:-1]) p.allow_kc = int(opts[-1]) else: tag=self.opts if tag=="lowcal": lowcal=True pass s = Suggest(fl.foodlist, p.allow_kc, p.allow_carb, p.allow_prot, p.allow_fat, tag) if lowcal:s.lowCalHighPF() else:s.suggestSomething() return try: Args(sys.argv) except KeyboardInterrupt: RESULT("\nQuit") exit(-1)