本文基于 Frost Ming “friendly python” 标签下的文章,整理出偏向工程实践的开发范式、规范与典型好/坏代码范式示例。重点是“对使用者友好 + 对维护者友好”,并强调在 Python 中利用语言特性与生态扩展点进行合理抽象。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 随机字符串 | |
| */ | |
| private static final char[] CHARS = new char[] {'F', 'L', 'G', 'W', '5', 'X', 'C', '3', | |
| '9', 'Z', 'M', '6', '7', 'Y', 'R', 'T', '2', 'H', 'S', '8', 'D', 'V', 'E', 'J', '4', 'K', | |
| 'Q', 'P', 'U', 'A', 'N', 'B'}; | |
| private final static int CHARS_LENGTH = 32; | |
| /** | |
| * 邀请码长度 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| module.exports = { | |
| types: [ | |
| { | |
| value: 'WIP', | |
| name : '💪 WIP: Work in progress' | |
| }, | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var bigint = require( 'big-integer' ) | |
| var lower = 'abcdefghijklmnopqrstuvwxyz'; | |
| var upper = lower.toUpperCase(); | |
| var numbers = '0123456789' | |
| var ig_alphabet = upper + lower + numbers + '-_' | |
| var bigint_alphabet = numbers + lower | |
| function toShortcode( longid ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding:utf-8 | |
| from elasticsearch import Elasticsearch | |
| import json | |
| # Define config | |
| host = "127.0.0.1" | |
| port = 9200 | |
| timeout = 1000 | |
| index = "index" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """ | |
| Twitter's API doesn't allow you to get replies to a particular tweet. Strange | |
| but true. But you can use Twitter's Search API to search for tweets that are | |
| directed at a particular user, and then search through the results to see if | |
| any are replies to a given tweet. You probably are also interested in the | |
| replies to any replies as well, so the process is recursive. The big caveat | |
| here is that the search API only returns results for the last 7 days. So |
type below:
brew update
brew install redis
To have launchd start redis now and restart at login:
brew services start redis