Python 技巧

This is a series blogs used to record my daily thinking

Use python to format print json content

  1. Json demo file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    $ cat demo.json
    {
    "filepath_completion_use_working_dir": 0,
    "auto_trigger": 1,
    "min_num_of_chars_for_completion": 2,
    "min_num_identifier_candidate_chars": 0,
    "semantic_triggers": {},
    "filetype_specific_completion_to_disable": {
    "gitcommit": 1

    },
    "seed_identifiers_with_syntax": 0,
    "collect_identifiers_from_comments_and_strings": 0,
    "collect_identifiers_from_tags_files": 0,
    "max_num_identifier_candidates": 10,
    "extra_conf_globlist": [],
    "global_ycm_extra_conf": "",
    "confirm_extra_conf": 1,
    "complete_in_comments": 0,
    "complete_in_strings": 1,
    "max_diagnostics_to_display": 30,
    "filetype_whitelist": {
    "*": 1

    },
    "filetype_blacklist": {
    "tagbar": 1,
    "qf": 1,
    "notes": 1,
    "markdown": 1,
    "netrw": 1,
    "unite": 1,
    "text": 1,
    "vimwiki": 1,
    "pandoc": 1,
    "infolog": 1,
    "mail": 1

    },
    "auto_start_csharp_server": 1,
    "auto_stop_csharp_server": 1,
    "use_ultisnips_completer": 1,
    "csharp_server_port": 0,
    "hmac_secret": "",
    "server_keep_logfiles": 0,
    "gocode_binary_path": "",
    "godef_binary_path": "",
    "rust_src_path": "",
    "racerd_binary_path": "",
    "python_binary_path": ""

    }
  2. Use python to format print json file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    $ python -m json.tool demo.json                                                                                                                yarving@yarving-ThinkPad-T400
    {
    "auto_start_csharp_server": 1,
    "auto_stop_csharp_server": 1,
    "auto_trigger": 1,
    "collect_identifiers_from_comments_and_strings": 0,
    "collect_identifiers_from_tags_files": 0,
    "complete_in_comments": 0,
    "complete_in_strings": 1,
    "confirm_extra_conf": 1,
    "csharp_server_port": 0,
    "extra_conf_globlist": [],
    "filepath_completion_use_working_dir": 0,
    "filetype_blacklist": {
    "infolog": 1,
    "mail": 1,
    "markdown": 1,
    "netrw": 1,
    "notes": 1,
    "pandoc": 1,
    "qf": 1,
    "tagbar": 1,
    "text": 1,
    "unite": 1,
    "vimwiki": 1
    },
    "filetype_specific_completion_to_disable": {
    "gitcommit": 1
    },
    "filetype_whitelist": {
    "*": 1
    },
    "global_ycm_extra_conf": "",
    "gocode_binary_path": "",
    "godef_binary_path": "",
    "hmac_secret": "",
    "max_diagnostics_to_display": 30,
    "max_num_identifier_candidates": 10,
    "min_num_identifier_candidate_chars": 0,
    "min_num_of_chars_for_completion": 2,
    "python_binary_path": "",
    "racerd_binary_path": "",
    "rust_src_path": "",
    "seed_identifiers_with_syntax": 0,
    "semantic_triggers": {},
    "server_keep_logfiles": 0,
    "use_ultisnips_completer": 1
    }

python built-in method strip

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。
strip()方法语法:
str.strip([chars]);

Demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
s = '  00ww00  '

s1 = s.strip()
print s1
# expected: '00ww00'

s2 = s1.strip('0')
print s2
# expected: 'ww'

s3 = s1.lstrip('0')
print s3
# expected: 'ww00'

s4 = s1.rstrip('0')
print s4
# expected: '00ww'

python str expand functions

include: center, ljust, rjust, zfill

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
# encoding: utf-8


s = "hello"
print s.center(1)
print s.center(10)
print s.center(10, '-')
# print s.center(10, '--') error
print s.center(1, '-')

print s.ljust(1)
print s.ljust(10)
print s.ljust(1, '-')
print s.ljust(10, '-')

print s.rjust(1)
print s.rjust(10)
print s.rjust(1, '-')
print s.rjust(10, '-')

print s.zfill(10)

python find/index difference

The big difference is when not found:

  1. find() will return -1;
  2. index() will raise error: ValueError: substring not found

python str.format

format() function support str/int/list/tuple/dict/object
verfiy below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# encoding: utf-8

s = "test string success"
print "OK test: {}".format(s)

s = 1
print "OK test: {}".format(s)

s = [1, 2, 3]
print "OK test: {}".format(s)

s = ['a', 'b', 'c']
print "OK test: {}".format(s)

s = (1, 2, 3)
print "OK test: {}".format(s)

s = ('a', 'b', 'c')
print "OK test: {}".format(s)

s = {'a': 1, 'b': 2}
print "OK test: {}".format(s)

s = set([1, 2, 3, 4])
print "OK test: {}".format(s)

s = set(['a', 'b', 'c'])
print "OK test: {}".format(s)

class TestObj():
pass

s = TestObj()
print "OK test: {}".format(s)

python enumerate

enumberate will offer another index, index starts from 0. Demo:

1
2
3
4
5
6
7
8
9
10
11
a = [1, 2, 3, 4, 5]

for index, v in enumerate(a):
print index, v

# output below
# 0 1
# 1 2
# 2 3
# 3 4
# 4 5

argparse

There’re 2 blogs for reference:

  1. https://blog.ixxoo.me/argparse.html
  2. http://www.jianshu.com/p/fef2d215b91d

flask sent mail code segment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python
# encoding: utf-8

from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)
app.config['MAIL_SERVER'] = 'mail-server-address'

mail = Mail(app)
msg = Message('test', sender='sender@historytale.com', recipients=['yarving@historytale.com'])

with app.app_context():
mail.send(msg)
0%