<commit_before>#!/usr/bin/env python

import os       # file operations
import pickle   # serializing data structures
#import shelf    # automated persistence of dictionary-like structures

FILEN_EXT__LOCK = ".lock"

db = None

OPTIONS = ('p', 'g', 'r', 'pf', 'gf', 'q')
EXP_OPTIONS = ('Put: p key value',
               'Get: g key',
               'Remove: r key',
               'Put from file: pf key file',
               'Get to file: gf key file',
               'Quit')

class store(object):
    
    def __init__(self, filenDB="./wpi.db"):
        '''Opens the database file, creating it if it doesn't exist, and initializes and 
        options and settings for this session, should those exist later'''
        self.dbopen(filenDB)
        
        
    def dbopen(self, filenDB):
        ''''''
        self.filenDB = filenDB
        if os.path.exists(filenDB):
            pass # open DB
        else:
            pass # create DB
        return True

    def put(self, key, data):
        '''stores data under the given key.'''
        self.db[key] = data
        #TODO check that not in use?
        with open(self.filenDB, 'wb') as handle:
            pickle.dump(self.db, handle)
        return True

    def get(self, key):
        '''retrieves the data.'''

        #TODO check that not in use?
        with open(self.filenDB, 'rb') as handle:
            self.db = pickle.load(handle)
        return self.db[key]

    def remove(self, key):
        '''deletes the key.'''

        present = self.db.pop(key, None)
        with open(self.filenDB, 'wb') as handle:
            pickle.dump(self.db, handle)
        
        if present == None:
            return "Key not found"
        else:
            return True
        
    
def Put(key, data):
    '''stores data under the given key.'''
    return db.put(kay, data)
def Get(key):
    '''retrieves the data.'''
    return db.get(key)
def Remove(key):
    '''deletes the key.'''
    return db.remove(key)


# Running the value store
if __name__ == "__main__":
    db = store()
    # print the options
    for opt, explanation in zip(OPTIONS, EXP_OPTIONS):
        print "%s - %s\n" %(opt, explanation)
    while True:
        # read an option form the commandline
        lnInput = raw_input()
        choice = lnInput.split(" ")[0]
        args = lnInput.split(" ")[1:]

        # place a value into the store
        if choice == 'p':
            Put(args[0], args[1])
        # get
        elif choice == 'g':
            Get(args[0])<commit_msg>fixed spelling errors and added key-not-found handling<commit_after>#!/usr/bin/env python

import os       # file operations
import pickle   # serializing data structures
#import shelf    # automated persistence of dictionary-like structures

FILEN_EXT__LOCK = ".lock"

db = None

OPTIONS = ('p', 'g', 'r', 'pf', 'gf', 'q')
EXP_OPTIONS = ('Put: p key value',
               'Get: g key',
               'Remove: r key',
               'Put from file: pf key file',
               'Get to file: gf key file',
               'Quit')

class store(object):
    
    def __init__(self, filenDB="./wpi.db"):
        '''Opens the database file, creating it if it doesn't exist, and initializes and 
        options and settings for this session, should those exist later'''
        self.db = {}
        self.dbopen(filenDB)
        try:
            with open(self.filenDB, 'r+b') as handle:
                self.db = pickle.load(handle)
        except EOFError: print "no db yet."
        
    def dbopen(self, filenDB):
        ''''''
        self.filenDB = filenDB
        if os.path.exists(filenDB):
            pass # open DB
        else:
            f = open(self.filenDB, "w")
            f.close()
        return True

    def put(self, key, data):
        '''stores data under the given key.'''
        self.db[key] = data
        #TODO check that not in use?
        with open(self.filenDB, 'wb') as handle:
            pickle.dump(self.db, handle)
        return True

    def get(self, key):
        '''retrieves the data.'''

        #TODO check that not in use?
        try:
            with open(self.filenDB, 'rb') as handle:
                self.db = pickle.load(handle)
            return self.db[key]
        except KeyError: return "ERROR: key not found"

    def remove(self, key):
        '''deletes the key.'''

        present = self.db.pop(key, None)
        with open(self.filenDB, 'wb') as handle:
            pickle.dump(self.db, handle)
        
        if present == None:
            return "ERROR: key not found"
        else:
            return True
        
    
def Put(key, data):
    '''stores data under the given key.'''
    return db.put(key, data)
def Get(key):
    '''retrieves the data.'''
    return db.get(key)
def Remove(key):
    '''deletes the key.'''
    return db.remove(key)


# Running the value store
if __name__ == "__main__":
    db = store()
    # print the options
    for opt, explanation in zip(OPTIONS, EXP_OPTIONS):
        print "%s - %s" %(opt, explanation)
    while True:
        # read an option form the commandline
        lnInput = raw_input("--: ")
        choice = lnInput.split(" ")[0]
        args = lnInput.split(" ")[1:]

        # place a value into the store
        if choice == 'p':
            Put(args[0], args[1])
        # get
        elif choice == 'g':
            print Get(args[0])
{'fraction_non_alphanumeric': 0.12430323299888517, 'fraction_numerical': 0.0018580453363062058, 'mean_word_length': 1.848148148148148, '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': '2577608', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1635, 'n_words': 537}