Chinaunix首页 | 论坛 | 博客
  • 博客访问: 235681
  • 博文数量: 57
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-01 18:05
文章分类

全部博文(57)

文章存档

2017年(57)

我的朋友

分类: Python/Ruby

2017-12-04 16:48:10

#类的公有属性

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-12-04 16:41
  5. # @file :c1.py


  6. class MyClass(object):
  7.     var1 = '类属性,类的公有属性 var1'
  8.     _var2 = '类的私有属性 __var2'

  9.     def func1(self):
  10.         self.var3 = '对象的公有属性 var3'
  11.         self.__var4 = '对象的私有属性 __var4'
  12.         var5 = '函数的局部变量'


  13. mc = MyClass()
  14. mc.func1()
  15. print mc.var3
执行结果:
对象的公有属性 var3

#类的公有方法,私有方法,类方法,静态方法的调用

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-12-04 16:51
  5. # @file :c2.py


  6. class MyClass(object):
  7.     name = 'Test'

  8.     def func1(self):
  9.         print self.name,
  10.         print "我是公有方法"
  11.         self.__func2()

  12.     def __func2(self):
  13.         print self.name,
  14.         print "我是私有方法"

  15.     @classmethod
  16.     def classFun(self):
  17.         print self.name,
  18.         print "我是类方法"

  19.     @staticmethod
  20.     def staticFun():
  21.         print MyClass.name,
  22.         print "我是静态方法"


  23. mc = MyClass()
  24. mc.func1()
  25. MyClass.classFun()
  26. MyClass.staticFun()
执行结果:
Test 我是公有方法
Test 我是私有方法
Test 我是类方法
Test 我是静态方法

#编写memcache 启动脚本

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-12-04 17:04
  5. # @file :memcache.py
  6. import sys

  7. import os
  8. from subprocess import Popen , PIPE


  9. class Process(object):
  10.     '''memcached rc script'''
  11.     def __init__(self, name, program, args, workdir):
  12.         self.name =name
  13.         self.program = program
  14.         self.args = args
  15.         self.workdir = workdir

  16.     def _init(self):
  17.         '''/var/tmp/memcached'''
  18.         if not os.path.exists(self.workdir):
  19.             os.mkdir(self.workdir)
  20.             os.chdir(self.workdir)

  21.     def _pidFile(self):
  22.         '''/var/tmp/memcached/memcached.pid'''
  23.         return os.path.join(self.workdir, "%s.pid" % self.name)

  24.     def _writePid(self):
  25.         if self.pid:
  26.             with open(self._pidFile(), 'w') as fd:
  27.                 fd.write(str(self.pid))

  28.     def start(self):
  29.         self._init()
  30.         cmd = self.program + '' + self.args
  31.         P = Popen(cmd, stdout=PIPE, shell =True)
  32.         self.pid = p.pid
  33.         self._writePid()
  34.         print "%s start Sucessful" % self.name

  35.     def _getPid(self):
  36.         p = Popen(['pidof', self.name], stdout=PIPE)
  37.         pid = p.stdin.read().strip()
  38.         return pid

  39.     def stop(self):
  40.         pid = self._getPid()
  41.         if pid:
  42.             os.kill(int(pid), 15)
  43.             if os.path.exists(self._pidFile()):
  44.                 os.remove(self._pidFile())
  45.             print "%s is stopped" % self.name


  46.     def restart(self):
  47.         self.stop()
  48.         self.start()

  49.     def status(self):
  50.         pid = self._getPid()
  51.         if pid:
  52.             print "%s is already running" % self.name
  53.         else:
  54.             print "%s is not running" % self.name


  55.     def help(self):
  56.         print "Usage: %s {start|stop|status|restart}" % __file__


  57. def main():
  58.     name = 'memcached'
  59.     prog = '/usr/bin/memcached'
  60.     args = '-u nobody -p 11211 -c 1024 -m 64'
  61.     wd = '/var/tmp/memcached'
  62.     pm = Process(name = name,
  63.                  program = prog,
  64.                  args = args,
  65.                  workdir = wd)
  66.     try:
  67.         cmd = sys.argv[1]
  68.     except IndexError, e:
  69.         print "Option error"
  70.         sys.exit()
  71.     if cmd == 'start':
  72.         pm.start()
  73.     elif cmd == 'stop':
  74.         pm.stop()
  75.     elif cmd == 'restart':
  76.         pm.restart()
  77.     elif cmd == 'status':
  78.         pm.status()
  79.     else:
  80.         pm.help()


  81. if __name__ == '__main__':
  82.     main()
执行结果:







































阅读(1402) | 评论(0) | 转发(0) |
0

上一篇:python 基础进阶

下一篇:redis 数据操作1

给主人留下些什么吧!~~