<commit_before>import curses
import math
import os
import traceback
import threading
import time
import random
import getpass
import json
import sqlite3
import string
import re
import completer
import datetime

class CursedMenu(object):
    #TODO: name your plant
    '''A class which abstracts the horrors of building a curses-based menu system'''
    def __init__(self, this_plant, this_data):
        '''Initialization'''
        self.initialized = False
        self.screen = curses.initscr()
        curses.noecho()
        curses.raw()
        if curses.has_colors():
            curses.start_color()
        try:
            curses.curs_set(0)
        except curses.error:
            # Not all terminals support this functionality.
            # When the error is ignored the screen will look a little uglier, but that's not terrible
            # So in order to keep botany as accesible as possible to everyone, it should be safe to ignore the error.
            pass
        self.screen.keypad(1)
        self.plant = this_plant
        self.visited_plant = None
        self.user_data = this_data
        self.plant_string = self.plant.parse_plant()
        self.plant_ticks = str(int(self.plant.ticks))
        self.exit = False
        self.infotoggle = 0
        self.maxy, self.maxx = self.screen.getmaxyx()
        # Highlighted and Normal line definitions
        if curses.has_colors():
            self.define_colors()
            self.highlighted = curses.color_pair(1)
        else:
            self.highlighted = curses.A_REVERSE
        self.normal = curses.A_NORMAL
        # Threaded screen update for live changes
        screen_thread = threading.Thread(target=self.update_plant_live, args=())
        screen_thread.daemon = True
        screen_thread.start()
        # Recusive lock to prevent both threads from drawing at the same time
        self.screen_lock = threading.RLock()
        self.screen.clear()
        self.show(["water","look","garden","visit", "instructions"], title=' botany ', subtitle='options')

    def define_colors(self):
        # TODO: implement colors
        # set curses color pairs manually
        curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
        curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
        curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)
        curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
        curses.init_pair(5, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
        curses.init_pair(6, curses.COLOR_YELLOW, curses.COLOR_BLACK)
        curses.init_pair(7, curses.COLOR_RED, curses.COLOR_BLACK)
        curses.init_pair(8, curses.COLOR_CYAN, curses.COLOR_BLACK)

    def show(self, options, title, subtitle):
        # Draws a menu with parameters
        self.set_options(options)
        self.update_options()
        self.title = title
        self.subtitle = subtitle
        self.selected = 0
        self.initialized = True
        self.draw_menu()

    def update_options(self):
        # Makes sure you can get a new plant if it dies
        if self.plant.dead or self.plant.stage == 5:
            if "harvest" not in self.options:
                self.options.insert(-1,"harvest")
        else:
            if "harvest" in self.options:
                self.options.remove("harvest")

    def set_options(self, options):
        # Validates that the last option is "exit"
        if options[-1] is not 'exit':
            options.append('exit')
        self.options = options

    def draw(self):
        # Draw the menu and lines
        self.maxy, self.maxx = self.screen.getmaxyx()
        self.screen_lock.acquire()
        self.screen.refresh()
        try:
            self.draw_default()
            self.screen.refresh()
        except Exception as exception:
            # Makes sure data is saved in event of a crash due to window resizing
            self.screen.clear()
            self.screen.addstr(0, 0, "Enlarge terminal!", curses.A_NORMAL)
            self.screen.refresh()
            self.__exit__()
            traceback.print_exc()
        self.screen_lock.release()

    def draw_menu(self):
        # Actually draws the menu and handles branching
        request = ""
        try:
            while request is not "exit":
                self.draw()
                request = self.get_user_input()
                self.handle_request(request)
            self.__exit__()

        # Also calls __exit__, but adds traceback after
        except Exception as exception:<commit_msg>don't use "is" to compare string literals<commit_after>import curses
import math
import os
import traceback
import threading
import time
import random
import getpass
import json
import sqlite3
import string
import re
import completer
import datetime

class CursedMenu(object):
    #TODO: name your plant
    '''A class which abstracts the horrors of building a curses-based menu system'''
    def __init__(self, this_plant, this_data):
        '''Initialization'''
        self.initialized = False
        self.screen = curses.initscr()
        curses.noecho()
        curses.raw()
        if curses.has_colors():
            curses.start_color()
        try:
            curses.curs_set(0)
        except curses.error:
            # Not all terminals support this functionality.
            # When the error is ignored the screen will look a little uglier, but that's not terrible
            # So in order to keep botany as accesible as possible to everyone, it should be safe to ignore the error.
            pass
        self.screen.keypad(1)
        self.plant = this_plant
        self.visited_plant = None
        self.user_data = this_data
        self.plant_string = self.plant.parse_plant()
        self.plant_ticks = str(int(self.plant.ticks))
        self.exit = False
        self.infotoggle = 0
        self.maxy, self.maxx = self.screen.getmaxyx()
        # Highlighted and Normal line definitions
        if curses.has_colors():
            self.define_colors()
            self.highlighted = curses.color_pair(1)
        else:
            self.highlighted = curses.A_REVERSE
        self.normal = curses.A_NORMAL
        # Threaded screen update for live changes
        screen_thread = threading.Thread(target=self.update_plant_live, args=())
        screen_thread.daemon = True
        screen_thread.start()
        # Recusive lock to prevent both threads from drawing at the same time
        self.screen_lock = threading.RLock()
        self.screen.clear()
        self.show(["water","look","garden","visit", "instructions"], title=' botany ', subtitle='options')

    def define_colors(self):
        # TODO: implement colors
        # set curses color pairs manually
        curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
        curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
        curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)
        curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
        curses.init_pair(5, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
        curses.init_pair(6, curses.COLOR_YELLOW, curses.COLOR_BLACK)
        curses.init_pair(7, curses.COLOR_RED, curses.COLOR_BLACK)
        curses.init_pair(8, curses.COLOR_CYAN, curses.COLOR_BLACK)

    def show(self, options, title, subtitle):
        # Draws a menu with parameters
        self.set_options(options)
        self.update_options()
        self.title = title
        self.subtitle = subtitle
        self.selected = 0
        self.initialized = True
        self.draw_menu()

    def update_options(self):
        # Makes sure you can get a new plant if it dies
        if self.plant.dead or self.plant.stage == 5:
            if "harvest" not in self.options:
                self.options.insert(-1,"harvest")
        else:
            if "harvest" in self.options:
                self.options.remove("harvest")

    def set_options(self, options):
        # Validates that the last option is "exit"
        if options[-1] != 'exit':
            options.append('exit')
        self.options = options

    def draw(self):
        # Draw the menu and lines
        self.maxy, self.maxx = self.screen.getmaxyx()
        self.screen_lock.acquire()
        self.screen.refresh()
        try:
            self.draw_default()
            self.screen.refresh()
        except Exception as exception:
            # Makes sure data is saved in event of a crash due to window resizing
            self.screen.clear()
            self.screen.addstr(0, 0, "Enlarge terminal!", curses.A_NORMAL)
            self.screen.refresh()
            self.__exit__()
            traceback.print_exc()
        self.screen_lock.release()

    def draw_menu(self):
        # Actually draws the menu and handles branching
        request = ""
        try:
            while request != "exit":
                self.draw()
                request = self.get_user_input()
                self.handle_request(request)
            self.__exit__()

        # Also calls __exit__, but adds traceback after
        except Exception as exception:
{'fraction_non_alphanumeric': 0.0875439367311072, 'fraction_numerical': 0.0041739894551845345, 'mean_word_length': 2.2093761015156854, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '567376', 'n_tokens_mistral': 2644, 'n_tokens_neox': 2590, 'n_words': 701}