当前位置: 首页>>代码示例>>Python>>正文


Python Unpickler.has_key方法代码示例

本文整理汇总了Python中cPickle.Unpickler.has_key方法的典型用法代码示例。如果您正苦于以下问题:Python Unpickler.has_key方法的具体用法?Python Unpickler.has_key怎么用?Python Unpickler.has_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cPickle.Unpickler的用法示例。


在下文中一共展示了Unpickler.has_key方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: IDRequestHandler

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import has_key [as 别名]
class IDRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def run(self):
        try:
            self.counter_file = os.environ.get('counter')
            self.get_id()
        except:
            self.send_response(400)
            self.send_header('Content-Type', 'text/html')
            self.end_headers()
            raise
    do_GET = run
    # do_POST = run

    def getCounter(self, key):
        # initialise counter
        try:
            f = open(self.counter_file, 'r+')
            self._counters = Unpickler(f).load()
        except:
            f = open(self.counter_file, 'w+')
            self._counters = {}
        f.close()
        if not self._counters.has_key(key):
            self._counters[key] = 0
        return self._counters[key]

    def setCounter(self, key, count):
        self._counters[key] = count
        f = open(self.counter_file, 'r+')
        p = Pickler(f)
        p.dump(self._counters)
        f.close()

    def get_id(self):
        batch_size = None
        command = self.command.lower() 
        if command == 'get' and self.path.find('?') != -1:
            key, qs = self.path.split('?', 1)
            data = cgi.parse_qs(qs)
            try:
                batch_size = int(data['batch_size'][0])
            except:
                batch_size = None
        else:
            key = self.path

        # POST (also works .. let's GET for now .. )
        # elif command == 'post':
        #     key = self.path
        #     length = self.headers.getheader('content-length')
        #     if length:
        #         length = int(length)
        #         qs = self.rfile.read(length)
        #         data = cgi.parse_qs(qs)

        prev_count = self.getCounter(key)
        next_count = prev_count + 1
        if batch_size:
            last_count = prev_count + batch_size
        else:
            last_count = next_count 
        self.setCounter(key, last_count)
        self.send_response(200)
        self.send_header('Content-Type', 'text/plain')
        self.end_headers()
        self.wfile.write(str(next_count))
开发者ID:xispa,项目名称:bika.lims,代码行数:69,代码来源:id-server.py

示例2: freetimeConfig

# 需要导入模块: from cPickle import Unpickler [as 别名]
# 或者: from cPickle.Unpickler import has_key [as 别名]

#.........这里部分代码省略.........
        pkey.write( userkey.get('UserKey','rsa').lstrip() )
        pkey.seek(0)
        self.pkey = RSAKey.from_private_key( pkey )
        self.server = userkey.get('UserKey', 'server')
        self.server_account = userkey.get('UserKey','account')

        groups_file = os.path.join( config_path, 'groups.txt' )
        if os.path.exists( groups_file ):
            text = open( groups_file ).read()
            try:
                text = text.decode('utf8')
            except:
                text = text.decode('shift_jis')
                print 'Barfed on groups file, using shift-jis encoding'
            self.groups = [x[1:-1] for x in re.findall('\[[^]]+\]'.decode('utf8'), text )]
        else:
            self.groups = []
        if self.groups:
            self.groups.insert(0, 'Personal')
        ##print self.groups
        #
        # Reconcile old group data and current groups configuration
        self.groups_data_file = os.path.join( config_path, 'groups.pkl' )
        if os.path.exists( self.groups_data_file ):
            ph = open( self.groups_data_file )
            self.groups_data = Unpickler( ph ).load()
            ph.close()
        else:
            self.groups_data = {}
        for key in self.groups_data.keys():
            if not key in self.groups:
                self.groups_data.pop(key)
        for group in self.groups:
            if not self.groups_data.has_key( group ):
                self.groups_data[ group ] = []

        self.build_user_info()

        self.known_hosts_file = os.path.join( defaults_dir, 'known_hosts')
        self.user_data_file = os.path.join( data_path, self.category, self.userkey )
        self.freetime = freeTime()


    def build_user_info(self):
        self.users = {'Staff': [], 'Student': []}
        today = date.today()
        self.user_index = {}
        for category in ['Staff','Student']:
            ids = os.listdir( self.category_path[ category ] )
            for userid in ids:
                userfile = os.path.join( self.category_path[ category ], userid )
                ph = open( userfile )
                try:
                    userdata = Unpickler( ph ).load()
                    doit = True
                except EOFError:
                    ph.close()
                    os.unlink( userfile )
                    doit = False
                if doit:
                    ph.close()
                    fullname = userdata[1].strip()
                    email = userdata[2].strip()
                    telephone = userdata[3].strip()
                    mtime = os.stat( userfile ).st_mtime
                    filedate = date.fromtimestamp( mtime )
开发者ID:Ismatov,项目名称:gsl-en,代码行数:70,代码来源:Config.py


注:本文中的cPickle.Unpickler.has_key方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。