[python]实现Simple Database

闲来自己写了一个小的简单数据库(Simple Database),要求最好用python写。因为很久没写python了,语法都忘了很多,写的过程中温故知新。

首先这个数据库实现了如下功能:

  数据命令:

  • SET name value – Set the variable name to the value value. Neither variable names nor values will contain spaces.
  • GET name – Print out the value of the variable name, or NULL if that variable is not set.
  • UNSET name – Unset the variable name, making it just like that variable was never set.
  • NUMEQUALTO value – Print out the number of variables that are currently set tovalue. If no variables equal that value, print 0.
  • END – Exit the program. Your program will always receive this as its last command.

事务命令:

  • BEGIN – Open a new transaction block. Transaction blocks can be nested; a BEGINcan be issued inside of an existing block.
  • ROLLBACK – Undo all of the commands issued in the most recent transaction block, and close the block. Print nothing if successful, or print NO TRANSACTION if no transaction is in progress.
  • COMMIT – Close all open transaction blocks, permanently applying the changes made in them. Print nothing if successful, or print NO TRANSACTION if no transaction is in progress.

我的程序的时间复杂度
  SET O(1)
  GET O(1)
  UNSET O(1)) 
  NUMEQUALTO O(1)
  BEGIN O(1)
  ROLLBACK O(k) where k is the number of set and unset command within a transaction.
  COMMIT O(1)

#!/usr/bin/python

class SimpleDatabase:
    keysDict = {}
    numsDict = {}
    blocksNum = 0
    isBegin = False

    def setFun(self, name, value):
        if name in self.keysDict and len(self.keysDict[name]) > 0:
            preValue = self.keysDict[name][-1]
            self.numsDict[preValue].remove(name)
        if value not in self.numsDict:
            self.numsDict.setdefault(value, []).append(name)
        else:
            self.numsDict[value].append(name)

        if self.isBegin == True:
            self.keysDict.setdefault(name, []).append(value)
        else:
             if name not in self.keysDict:
                 self.keysDict.setdefault(name, []).append(value)
             else:
                 self.keysDict[name][-1] = value

    def getFun (self, name):
         if name not in self.keysDict or len(self.keysDict[name]) == 0 or self.keysDict[name][-1] == None:
            print "NULL"
        else:
            print self.keysDict[name][-1]

    def unsetFun(self, name):
        setFun(name, None)

    def numEqualToFun(self, value):
        if value in self.numsDict:
            print len(self.numsDict[value])
        else:
            print 0

    def rollbackFun(self):
        if self.blocksNum == 0:
            print "NO TRANSACTION"
        else:
            self.blocksNum -= 1
            self.isBegin = False
            for item in self.keysDict:
                if self.keysDict[item][-1] != None:
                    self.numsDict[self.keysDict[item][-1]].remove(item)
                self.keysDict[item].pop()
                if len(self.keysDict[item]) > 0:
                     self.numsDict[self.keysDict[item][-1]].append(item)
    def commitFun (self):
        if  self.blocksNum == 0:
            print "NO TRANSACTION"
        else:
            self.blocksNum = 0
            self.isBegin = False

    def beginFun (self):
        self.blocksNum += 1
        self.isBegin = True
    def run(self):
        while True:
            raw_command = raw_input()
            if raw_command == "END":
                break
            commands = raw_command.split()
            if commands[0] == "SET":
                assert (len(commands) >= 3)
                self.setFun (commands[1], commands[2])
            elif commands[0] == "GET":
                assert (len(commands) >= 2)
                self.getFun (commands[1])
            elif commands[0] == "UNSET":
                assert (len(commands) >= 2)
                self.unsetFun (commands[1])
            elif commands[0] == "NUMEQUALTO":
                assert (len(commands) >= 2)
                self.numEqualToFun (commands[1])
            elif commands[0] == "BEGIN":
                self.beginFun()
            elif commands[0] == "COMMIT":
                self.commitFun()
            elif commands[0] == "ROLLBACK":
                self.rollbackFun()        

sd = SimpleDatabase()
sd.run()

先说我的思路:

1.先介绍这里的事务命令(begin,rollback和commit),在这里采用栈的方式实现,即进入一个新的事务就将其中的操作加到栈中,如果还是在同一个事务中就覆盖之前的结果

2. 两个dict分别存储key的values(列表)和num的values(列表),values为列表类似于栈的功能,因为python中没有stack这个数据结构.

3. blocksNum控制blocks的数量,isBegin控制是否进入了一个block的操作.所以这两个变量是控制逻辑的关键.

总结学习心得:

1. python中global变量:dict可以省去global,其它类型变量不能省。global A, B

2. python中的类:__init__是构造函数,不定义就是默认构造函数,实例方法的第一个参数self我理解为C++的this; python类名之后可以加括号

3. python的split默认以空格换行;

4. 尽量用in不用has_key

5. 区分len() == 0, () == None, not in等

6. vim中的添加注释和取消注释命令!

7. raw_input和input的区别

8. list的remove, [-1], append, extend等使用

9.dict的value为list的情况:dict = {}; dict.setdefault(name, []).append(1)

时间: 2024-10-04 05:56:35

[python]实现Simple Database的相关文章

使用国内镜像通过pip安装python的一些包 Cannot fetch index base URL http://pypi.python.org/simple/

原文地址:http://www.xuebuyuan.com/1157602.html 学习flask,安装virtualenv环境,这些带都ok,但是一安装包总是出错无法安装, 比如这样超时的问题: (env)[email protected]:~/flask_study/venv-test/test$ easy_install Flask-SQLAlchemy Searching for Flask-SQLAlchemy Reading http://pypi.python.org/simpl

pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/

pipinstall***安装python包,出现 Cannot fetch index base URL  http://pypi.python.org/simple /错误提示或者直接安装不成功. 解决办法1.windows下创建/%user%/pip/pop.ini,并添加以下内容.        [global]          index-url=http://pypi.douban.com/simple/ 2.linux创建文件~/.pip/pip.conf,并添加一下内容.   

mac 下pip安装python三方库的时候提示 Could not fetch URL https://pypi.python.org/simple/virtualenv/: There was a problem confirming the ssl certificate:......

有什么问题记得留言,大家一起分享遇到过的大坑 我这边是换了一个镜像就好了,具体操作步骤如下(我是MAC): ~:mkdir .pip ~:cd .pip ~:vi pip.conf 将以下内容放入文件pip.conf中 [global] timeout = 6000 index-url = http://pypi.douban.com/simple/ [install] use-mirrors = true mirrors = http://pypi.douban.com/simple/ tru

python dbhelper(simple orm)

# coding:utf-8 import pymysql class Field(object): pass class Expr(object): def __init__(self, model, kwargs): self.model = model # How to deal with a non-dict parameter? self.params = kwargs.values() equations = [key + ' = %s' for key in kwargs.keys

Java vs. Python (1): Simple Code Examples

Some developers have claimed that Python is more productive than Java. It is dangerous to make such a claim, because it may take several days to prove that thoroughly. From a high level view, Java is statically typed, which means all variable names h

解决pip安装Cannot fetch index base URL http://pypi.python.org/simple/

产生这个问题的原因呢和github一样,因为他们用的cdn被墙.经小伙伴反馈,解决办法如下. 通过指定国内镜像源来安装: pip --trusted-host 镜像源 install 模块名 -i 镜像源路径 例如:pip --trusted-host pypi.doubanio.com install paramiko -i http://pypi.doubanio.com/simple 注意后面要有/simple目录!!! 命令解释: --trusted-host 指定可信源(忽略https

Cannot fetch index base URL http://pypi.python.org/simple/

解决方法: 在~/.pip/pip.conf中添加如下内容,没有该文件时创建即可 1 [global] 2 index-url=http://pypi.douban.com/simple/ 原文地址:https://www.cnblogs.com/live-program/p/11174827.html

46 Simple Python Exercises-Very simple exercises

4.Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise. def if_vowel(a): a=a.lower() if a in('a','e','i','o','u'): return True else: return Falseprint(if_vowel('A')) 原文地址:https://www.c

Python著名的lib和开发框架(均为转载)

第一,https://github.com/vinta/awesome-python Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Admin Panels Algorithms and Design Patterns Anti-spam Asset Management A