Jump to content
新域网络技术论坛

Python简单Class使用


Jamers
 Share

Recommended Posts

一个简单的封装,自行安装hashlib,hashlib中有一些函数定义的,但实际使用起来比较麻烦,特意用这个来尝试使用Class封装一下。

 

封装文件:tools.py

# -*- Coding: UTF-8 -*-

import hashlib

class tools:
    def md5(self, str):
        return hashlib.md5(str).hexdigest()

    def sha1(self, str):
        return hashlib.sha1(str).hexdigest()

    def sha512(self, str):
        return hashlib.sha512(str).hexdigest()

if __name__ == '__main__':
    t = tools()
    print t.md5('admin')

正常情况,我们可以在命令行测试一下:python tools.py

会显示'admin'的MD5值,简单封装完成了,调用吧

 

调用文件demo.py

#!/usr/local/bin/python
# -*- Coding: UTF-8 -*-
import sys
from tools import *

str = 'admin'
if len(sys.argv)>1:
    str = ""
    for i in range(1, len(sys.argv)):
        str += sys.argv[i]+' '
t = tools()

print('Str: %s,Md5: %s' % (str, t.md5(str)))
print('Str: %s,Sha1: %s' % (str, t.sha1(str)))
print('Str: %s,sha512: %s' % (str, t.sha512(str)))
Link to comment
Share on other sites

数组快速旋转代码:

    #顺时针旋转二维数组
    def r_rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        matrix[:] = map(list, zip(*matrix[::-1]))

    # 逆时针旋转二维数组
    def l_rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        matrix[:] = map(list, zip(*matrix))[::-1]

    def disp(self, matrix):
        for i in matrix:
            print(i)
        print('='*10)
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...