Example #1
0
def main(argv):
    if not (4 <= len(argv) <= 5):
        usage()
        return BAD_ARGS

    dbname, verb, key, value = (argv[1:] + [None])[:4]
    if verb not in ['get', 'set', 'delete']:
        usage()
        return BAD_VERBS

    db = dbdb.connect(dbname)  # CONNECT
    try:
        if verb == "get":
            sys.stdout.write(db[key])  # Get Value
        elif verb == "set":
            db[key] = value
            db.commit()
        else:
            del db[key]
            db.commit()
    except KeyError:
        print("Key not found", file=sys.stderr)
        return BAD_KEY

    return OK
Example #2
0
 def test_persistence(self):
     db = dbdb.connect(self.tempfile_name)
     db["b"] = "bye"
     db["a"] = "aye"
     db["c"] = "cye"
     db.commit()
     db["d"] = "dye"
     eq_(len(db), 4)
     db.close()
     db = dbdb.connect(self.tempfile_name)
     eq_(db["a"], "aye")
     eq_(db["b"], "bye")
     eq_(db["c"], "cye")
     with assert_raises(KeyError):
         db["d"]
     eq_(len(db), 3)
     db.close()
Example #3
0
 def test_persistence(self):
     db = dbdb.connect(self.tempfile_name)
     db['b'] = 'bee'
     db['a'] = 'aye'
     db['c'] = 'see'
     db.commit()
     db['d'] = 'dee'
     eq_(len(db), 4)
     db.close()
     db = dbdb.connect(self.tempfile_name)
     eq_(db['a'], 'aye')
     eq_(db['b'], 'bee')
     eq_(db['c'], 'see')
     with assert_raises(KeyError):
         db['d']
     eq_(len(db), 3)
     db.close()
Example #4
0
def use(dbname, verb, key, value=None):
    if verb not in {'get', 'set', 'delete'}:
        usage()
        return BAD_VERB
    db = dbdb.connect(dbname)
    try:
        if verb == 'get':
            sys.stdout.write(db[key])
        elif verb == 'set':
            db[key] = value
            db.commit()
        else:
            del db[key]
            db.commit()
    except KeyError:
        print("Key not found", file=sys.stderr)
        return BAD_KEY
    return OK
Example #5
0
def main():
    if not (4 <= len(sys.argv) <= 5):
        usage()
        return BAD_ARGS
    dbname, verb, key, value = (sys.argv[1:] + [None])[:4]
    if verb not in ('get', 'set'):
        usage()
        return BAD_VERB
    db = dbdb.connect(dbname)
    if verb == 'get':
        try:
            sys.stdout.write(db[key])
        except KeyError:
            print("Key not found", file=sys.stderr)
            return BAD_KEY
    else:
        db[key] = value
        db.commit()
    return OK
Example #6
0
def main(argv):
    if not (4 <= len(argv) <= 5):
        usage()
        return BAD_ARGS
    dbname, verb, key, value = (argv[1:] + [None])[:4]
    if verb not in {'get', 'set', 'delete'}:
        usage()
        return BAD_VERB
    db = dbdb.connect(dbname)
    try:
        if verb == 'get':
            sys.stdout.write(db[key])  #等价于print,但不换行
        elif verb == 'set':
            db[key] = value
            db.commit()
        else:
            del db[key]
            db.commit()
    except KeyError:
        print("Key not found", file=sys.stderr)
        return BAD_KEY
    return OK
Example #7
0
def main(argv):
    if not ( 4 <= len(argv) <= 5):
        usage()
        return BAD_ARGS
    dbname, verb, key, value = (argv[1:] + [None])[:4]
    if verb not in [ "get", "set", "delete" ]:
        usage()
        return BAD_VERB
    db = dbdb.connect(dbname)
    try:
        if verb == "get":
            sys.stdout.write(db[key])
        elif verb == "set":
            db[key] = value
            db.commit
        else:
            del db[key]
            db.commit
    except KeyError:
        print("Key not found", file=sys.stderr)
        return BAD_KEY
    return OK
Example #8
0
def main(argv):
    if not (4 <= len(argv) <= 5):
        usage()
        return BAD_ARGS
    dbname, verb, key, value = (
        argv[1:] +
        [None])[:4]  # (DBNAME, get, Key, VALUE) None是有可能用get,delete时无value
    if verb not in {'get', 'set', 'delete'}:
        usage()
        return BAD_VERB
    db = dbdb.connect(dbname)  # import dbdb from dbdb.__init__.py
    try:
        if verb == 'get':
            sys.stdout.write(db[key])
        elif verb == 'set':
            db[key] = value
            db.commit()
        else:
            del db[key]
            db.commit()
    except KeyError:
        print("Key not found", file=sys.stderr)
        return BAD_KEY
    return OK
Example #9
0
 def test_new_database_file(self):
     db = dbdb.connect(self.new_tempfile_name)
     db['a'] = 'aye'
     db.commit()
     db.close()
Example #10
0
 def test_new_database_file(self):
     db = dbdb.connect(self.new_tempfile_name)
     db["a"] = "aye"
     db.commit()
     db.close()
Example #11
0
def init_db(app):
    db = dbdb.connect(app.config["DATABASE"])
    return db