Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion readMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,36 @@ Python DeMo

### venv2

#### 1. venv2 for mac

```
virtualenv -p /usr/local/bin/python2 --no-site-packages venv2
```

## venv3
#### 2. venv2 for windows

```
python2 -m virtualenv -p C:\Python\Python27\python2.exe venv2


python2 -m virtualenv -p C:\Python\Python27\python2.exe venv2 > venv2.log
```

### venv3

#### 1. venv3 for mac

```
virtualenv -p /usr/local/bin/python3 --no-site-packages venv
```

#### 2. venv3 for windows

```
python3 -m virtualenv -p C:\Python\Python38\python3.exe venv


python3 -m virtualenv -p C:\Python\Python38\python3.exe venv > venv.log
```
34 changes: 34 additions & 0 deletions src/argparse/test_argparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding:utf-8 -*-

import argparse

#
# python -m test_argparse.py --help
parser = argparse.ArgumentParser(description='Command-line example.')

# Add optional switches
parser.add_argument('-v',
action='store_true',
dest='is_verbose',
help='produce verbose output')
parser.add_argument('-o',
action='store',
dest='output',
metavar='FILE',
help='direct output to FILE instead of stdout')
parser.add_argument('-C',
action='store',
type=int,
dest='context',
metavar='NUM',
default=0,
help='display NUM lines of added context')

# Allow any number of additional arguments.
parser.add_argument(nargs='*',
action='store',
dest='inputs',
help='input filenames (default is stdin)')

args = parser.parse_args()
print args.__dict__
26 changes: 26 additions & 0 deletions src/class/test_class_2.7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -- coding:utf-8 --
# python 2.7.16 version


class A:
def foo(self):
print('深度优先 -> called A.foo()')


class B(A):
pass


class C(A):
def foo(self):
print('广度优先 -> called C.foo()')


class D(B, C):
pass


if __name__ == '__main__':
''' 深度优先: D -> B -> A '''
d = D()
d.foo()
26 changes: 26 additions & 0 deletions src/class/test_class_2.x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -- coding:utf-8 --
# python 2.7.16 version


class A(object):
def foo(self):
print('深度优先 -> called A.foo()')


class B(A, object):
pass


class C(A, object):
def foo(self):
print('广度优先 -> called C.foo()')


class D(B, C, object):
pass


if __name__ == '__main__':
''' 广度优先: D -> C -> A '''
d = D()
d.foo()
31 changes: 31 additions & 0 deletions src/class/test_class_3.7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -- coding:utf-8 --
# python 3.7.4 version


class A:
def foo(self):
print('深度优先 -> called A.foo()')


class B(A):
pass


class C(A):
def foo(self):
print('广度优先 -> called C.foo()')


class D(B, C):
pass


if __name__ == '__main__':
''' 3.x 广度优先: D -> C -> A '''
# 此处有点反直觉, 3.x 的应该是做了兼容为新式类型,
# D -> C -> A
# 我在我得机器上是:
# D -> B -> A
# 深度优先 -> called A.foo()
d = D()
d.foo()
26 changes: 26 additions & 0 deletions src/class/test_class_3.x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -- coding:utf-8 --
# python 3.7.4 version


class A(object):
def foo(self):
print('深度优先 -> called A.foo()')


class B(A, object):
pass


class C(A, object):
def foo(self):
print('广度优先 -> called C.foo()')


class D(B, C, object):
pass


if __name__ == '__main__':
''' 3.x 广度优先: D -> C -> A '''
d = D()
d.foo()
29 changes: 29 additions & 0 deletions src/file/test_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding:utf-8 -*-

import os

# the define global cwd variable
cwd = os.getcwd()


def test_os_path():
''' the test os path '''
absPath = os.path.abspath(cwd)
print('the current abs path is %s' % absPath)
baseName = os.path.basename(cwd)
print('the current base name is %s' % baseName)
# os.path.commonprefix(cwd)
dirName = os.path.dirname(cwd)
print('the current dir name is %s' % dirName)
exists = os.path.exists(cwd)
print('the current directory or file is exists that is %s ' % exists)
userProfile = os.path.expanduser("~")
print('the current user profile that is %s ' % userProfile)
supportUnicodeFileNames = os.path.supports_unicode_filenames
print('the current weather support unicode file names that is %s ' %
supportUnicodeFileNames)


if __name__ == "__main__":
'''主函数入口'''
test_os_path()
48 changes: 48 additions & 0 deletions src/logging/test_loggins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding:utf-8 -*-

import logging


def loggingConfig():
''' the default logging config

### Python 日志格式

%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID。可能没有
%(threadName)s 线程名。可能没有
%(process)d 进程ID。可能没有
%(message)s 用户输出的消息
'''
logging.basicConfig(
filename='./out/test_logging_recorder.log',
# format='%(asctime)s %(levelname)s:%(lineno)d:%(message)s',
format='%(asctime)s %(levelname)s:%(message)s',
# datefmt='%Y-%m-%d %H:%M:%S',
filemode='a',
level=logging.DEBUG)


def testLogging():
''' the test logging '''
logging.debug('this is debug message.')
logging.info('this is info message.')
logging.warning('this is warning message.')
logging.error('this is error message.')
logging.critical('this is critical message.')


if __name__ == "__main__":
'''主函数入口'''
loggingConfig()
testLogging()
33 changes: 33 additions & 0 deletions src/logging/test_thread_loggins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding:utf-8 -*-

import logging
import threading
import time


def worker(arg):
while not arg['stop']:
logging.debug('我来自于工作线程...')
time.sleep(5)


def main():
logging.basicConfig(
level=logging.DEBUG,
format='%(relativeCreated)6d %(threadName)s %(message)s')
info = {'stop': False}
thread = threading.Thread(target=worker, args=(info, ))
thread.start()
while True:
try:
logging.debug('我来自于主线程...')
time.sleep(5)
except KeyboardInterrupt:
info['stop'] = True
break
thread.join()


if __name__ == "__main__":
'''主函数入口'''
main()
21 changes: 21 additions & 0 deletions src/standard_library/test_argv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -- coding:utf-8 --


import sys


def test_argv():
argv = sys.argv[1:]
# 输出格式:
# ['start_date=2019', 'end_date=2020']
# argv = sys.argv[:]
# 输出格式:
# ['test_argv.py', 'start_date=2019', 'end_date=2020']
print(argv)


if __name__ == "__main__":
'''主函数入口'''
# the python command
# python test_argv.py start_date=2019 end_date=2020
test_argv()
13 changes: 13 additions & 0 deletions src/standard_library/test_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: UTF-8 -*-

import json


def test_dict():
dict1 = {}
print json.dumps(dict1)


if __name__ == "__main__":
'''主函数入口'''
test_dict()
49 changes: 49 additions & 0 deletions src/standard_library/test_getopts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -- coding:utf-8 --

import getopt
import sys
# reload(sys)
# sys.setdefaultencoding('utf8')


def test_getopts():
'''
getopt函数的格式是getopt.getopt ( [命令行参数列表], "短选项", [长选项列表] )
短选项名后的冒号(:)表示该选项必须有附加的参数。
长选项名后的等号(=)表示该选项必须有附加的参数。
'''
short_opt = 'c:d' # 短链选项
long_args = ['info', 'debug', 'time='] # 长链选项
print
sys_argv = sys.argv[1:]
print 'the current sys_argv: %s' % sys_argv
print
opts, args = getopt.getopt(sys_argv, short_opt, long_args)
print('the current opts type: %s' % type(opts))
print('the current opts = %s' % opts)
print ('the current opts[0] type: %s' % type(opts[0]))
print
print('the current args type: %s' % type(args))
print ('the current args = %s' % args)
print


def debug_print_command():
'''
命令输入,测试数据:
1. python test_getopts.py -c c_args:c -d d start_date=2019 end_date=2020
2. python test_getopts.py -c c_args:c -d start_date=2019 end_date=2020
3. python test_getopts.py -c c_args:c -d --info start_date=2019 end_date=2020
4. python test_getopts.py -c c_args:c -d --debug --time=the_current_system_time start_date=2019 end_date=2020
'''
argv = [
'python', 'test_getopts.py', '-c', 'c_args:c', '-d', 'd', 'start_date=2019',
'end_date=2020'
]
print " ".join(argv)


if __name__ == "__main__":
'''主函数入口'''
test_getopts()
# debug_print_command()
Loading