Reorganize some methods
This commit is contained in:
		
							parent
							
								
									98156bddde
								
							
						
					
					
						commit
						324a0b2f70
					
				
					 1 changed files with 21 additions and 16 deletions
				
			
		| 
						 | 
					@ -81,6 +81,14 @@ def strip_operator(text, operator, is_prefix):
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        return text[:-len_op].rstrip()
 | 
					        return text[:-len_op].rstrip()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def has_operator(text, operators):
 | 
				
			||||||
 | 
					    for op in operators:
 | 
				
			||||||
 | 
					        if text.startswith(op):
 | 
				
			||||||
 | 
					            return op, True
 | 
				
			||||||
 | 
					        elif text.endswith(op):
 | 
				
			||||||
 | 
					            return op, False
 | 
				
			||||||
 | 
					    return None, None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def top5():
 | 
					def top5():
 | 
				
			||||||
    data = read_data()
 | 
					    data = read_data()
 | 
				
			||||||
    items = [(score, name) for name, score in data.items()]
 | 
					    items = [(score, name) for name, score in data.items()]
 | 
				
			||||||
| 
						 | 
					@ -94,15 +102,9 @@ def top5():
 | 
				
			||||||
            break
 | 
					            break
 | 
				
			||||||
    return out
 | 
					    return out
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def update_item(name, increment):
 | 
					#
 | 
				
			||||||
    data = read_data()
 | 
					# Persistence
 | 
				
			||||||
    score = data.get(name)
 | 
					#
 | 
				
			||||||
    if not score:
 | 
					 | 
				
			||||||
        score = 0
 | 
					 | 
				
			||||||
    score += increment
 | 
					 | 
				
			||||||
    data[name] = score
 | 
					 | 
				
			||||||
    write_data(data)
 | 
					 | 
				
			||||||
    return score
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def erase_score(name):
 | 
					def erase_score(name):
 | 
				
			||||||
    data = read_data()
 | 
					    data = read_data()
 | 
				
			||||||
| 
						 | 
					@ -125,10 +127,13 @@ def write_data(obj):
 | 
				
			||||||
    with open(HEARTS_FILE, 'w') as f:
 | 
					    with open(HEARTS_FILE, 'w') as f:
 | 
				
			||||||
        json.dump(obj, f, sort_keys=True, indent=4)
 | 
					        json.dump(obj, f, sort_keys=True, indent=4)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def has_operator(text, operators):
 | 
					def update_item(name, increment):
 | 
				
			||||||
    for op in operators:
 | 
					    data = read_data()
 | 
				
			||||||
        if text.startswith(op):
 | 
					    score = data.get(name)
 | 
				
			||||||
            return op, True
 | 
					    if not score:
 | 
				
			||||||
        elif text.endswith(op):
 | 
					        score = 0
 | 
				
			||||||
            return op, False
 | 
					    score += increment
 | 
				
			||||||
    return None, None
 | 
					    data[name] = score
 | 
				
			||||||
 | 
					    write_data(data)
 | 
				
			||||||
 | 
					    return score
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue