def fetch_bigtable_rows(big_table, keys, other_silly_variable=None): """Fetches rows from a Bigtable.
Retrieves rows pertaining to the given keys from the Table instance represented by big_table. Silly things may happen if other_silly_variable is not None.
Args: big_table: An open Bigtable Table instance. keys: A sequence of strings representing the key of each table row to fetch. other_silly_variable: Another optional variable, that has a much longer name than the other args, and which does nothing.
Returns: A dict mapping keys to the corresponding table row data fetched. Each row is represented as a tuple of strings. For example:
# We use a weighted dictionary search to find out where i is in # the array. We extrapolate position based on the largest num # in the array and the array size and then do binary search to # get the exact number.
if i & (i-1) == 0: # true iff i is a power of 2
类
如果一个类不继承自其它类, 就显式的从object继承. 嵌套类也一样.
1 2 3 4 5 6 7 8 9 10 11 12
Yes: class SampleClass(object): pass
class OuterClass(object):
class InnerClass(object): pass
class ChildClass(ParentClass): """Explicitly inherits from another class already."""
Yes: x = a + b x = '%s, %s!' % (imperative, expletive) x = '{}, {}!'.format(imperative, expletive) x = 'name: %s; score: %d' % (name, n) x = 'name: {}; score: {}'.format(name, n)
1 2 3 4
No: x = '%s%s' % (a, b) # use + in this case x = '{}{}'.format(a, b) # use + in this case x = imperative + ', ' + expletive + '!' x = 'name: ' + name + '; score: ' + str(n)
>>> L = [x * x for x in range(10)] >>> L [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] >>> g = (x * x for x in range(10)) >>> g <generator object <genexpr> at 0x1022ef630>
import hashlib md5 = hashlib.md5() md5.update('how to use md5 in python hashlib?'.encode('utf-8')) print(md5.hexdigest()) d26a53750bc40b38b65a520292f69306
内建模块contextlib
使用try...finally关闭资源
1 2 3 4 5 6
try: f = open('/path/to/file', 'r') f.read() finally: if f: f.close()
from contextlib import closing from urllib.request import urlopen
with closing(urlopen('https://www.python.org')) as page: for line in page: print(line)
内建模块urllib
1 2 3 4 5 6 7
from urllib import request with request.urlopen('https://api.douban.com/v2/book/2129650') as f: data = f.read() print('Status:', f.status, f.reason) for k, v in f.getheaders(): print('%s: %s' % (k, v)) print('Data:', data.decode('utf-8'))
req = request.Request('https://passport.weibo.cn/sso/login') req.add_header('Origin', 'https://passport.weibo.cn') req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25') req.add_header('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F')
with request.urlopen(req, data=login_data.encode('utf-8')) as f: print('Status:', f.status, f.reason) for k, v in f.getheaders(): print('%s: %s' % (k, v)) print('Data:', f.read().decode('utf-8'))
# 导入MySQL驱动: >>> import mysql.connector # 注意把password设为你的root口令: >>> conn = mysql.connector.connect(user='root', password='password', database='test') >>> cursor = conn.cursor() # 创建user表: >>> cursor.execute('create table user (id varchar(20) primary key, name varchar(20))') # 插入一行记录,注意MySQL的占位符是%s: >>> cursor.execute('insert into user (id, name) values (%s, %s)', ['1', 'Michael']) >>> cursor.rowcount 1 # 提交事务: >>> conn.commit() >>> cursor.close() # 运行查询: >>> cursor = conn.cursor() >>> cursor.execute('select * from user where id = %s', ('1',)) >>> values = cursor.fetchall() >>> values [('1', 'Michael')] # 关闭Cursor和Connection: >>> cursor.close() True >>> conn.close()
多进程
1 2 3 4 5 6 7 8 9
import os
print('Process (%s) start...' % os.getpid()) # Only works on Unix/Linux/Mac: pid = os.fork() if pid == 0: print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid())) else: print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
if __name__=='__main__': print('Parent process %s.' % os.getpid()) p = Process(target=run_proc, args=('test',)) print('Child process will start.') p.start() p.join() print('Child process end.')
if __name__=='__main__': print('Parent process %s.' % os.getpid()) p = Pool(4) for i in range(5): p.apply_async(long_time_task, args=(i,)) print('Waiting for all subprocesses done...') p.close() p.join() print('All subprocesses done.')
Enter current password for root (enter for none): Set root password? [Y/n] y Remove anonymous users? [Y/n] y Disallow root login remotely? [Y/n] n Remove test database and access to it? [Y/n] y Reload privilege tables now? [Y/n] y
新建MySQL数据库及用户
直接在本机登录mysql,新建一个数据库及用户
1 2 3 4
mysql -uroot -p > create database redmine_db character set utf8; > create user 'redmine_admin'@'localhost' identified by 'your_new_password'; > grant all privileges on redmine_db.* to 'redmine_admin'@'localhost';
关闭SELinux
1 2 3 4
vi /etc/selinux/config SELINUX=disabled
/usr/sbin/setenforce 0 立刻关闭
配置iptables
配置iptables防火墙,将3000端口打开
1 2 3
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3000 -j ACCEPT
Deployment方式,因为有文件乱码,选择第3项 I will make the deployment accessible from the following location。但正式部署时建议选择第二项Copy this application onto every target for me!方便版本发布。
注意:最后一步一般选择第3项 I will make the deployment accessible from the following location,需分别上传代码至每台主机,但会避免因乱码文件引起的server无法启动问题。 第2项:Copy this application onto every target for me,可以只在Admin主机上放置代码,Sever启动时自动上传部署。但如果有乱码文件则无法正常启动。 注意:根据application中UploadPath,建立相应上传目录
如果StageMode=stage,server启动时会报Failed to initialize the application ‘examples’ due to error weblogic.management.DeploymentException: Exception occured while downloading files,原因不明,此时需要再手工update一下。
$ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track)
出现如上的提示,说明现在的工作目录相当干净,所有已跟踪文件在上次提交后都未被更改过。
现在我们做一些改动,添加一个readme.txt进去,然后再看一下状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ cat>readme.txt hello git ^C
git status On branch master
Initial commit
Untracked files: (use "git add <file>..." to include in what will be committed) readme.txt
nothing added to commit but untracked files present (use "git add" to track)
Untracked files显示了这个新创建的readme.txt处于未跟跟踪状态
跟踪新文件
使用git add命令开始跟踪一个新文件
1 2 3 4 5 6 7 8 9
$ git status On branch master
Initial commit
Changes to be committed: (use "git rm --cached <file>..." to unstage)
new file: readme.txt
readme.txt已 被跟踪 ,并处于 暂存状态
将本次修改暂存
现在我们再对readme.txt进行修改,添加一行,再执行git status查看状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ git status On branch master
Initial commit
Changes to be committed: (use "git rm --cached <file>..." to unstage)
new file: readme.txt
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: readme.txt
可以看到readme.txt 不仅出现在了Changes to be committed,还出现在了Changes not staged for commit 由此可见,Git关心的是 Changes ,而不是文件本身。 再次执行git add,可以将 本次修改 提交到暂存区,Changes not staged for commit提示消失
提交更新
使用git commit命令将暂存区中的内容提交至版本库,工作区又是干净的了
1 2 3 4 5 6 7 8 9 10
$ git commit -m "my first commit" [master (root-commit) 6c8912a] my first commit 1 file changed, 2 insertions(+) create mode 100644 readme.txt
$ git status On branch master Your branch is based on 'origin/master', but the upstream is gone. (use "git branch --unset-upstream" to fixup) nothing to commit, working tree clean
$ git remote show origin * remote origin Fetch URL: mobgit@134.32.51.60:learngit.git Push URL: mobgit@134.32.51.60:learngit.git HEAD branch (remote HEAD is ambiguous, may be one of the following): dev master serverfix serverfix2 Remote branches: dev tracked master tracked serverfix tracked serverfix2 tracked Local branches configured for 'git pull': dev merges with remote dev master merges with remote master serverfix merges with remote serverfix serverfix2 merges with remote serverfix2 Local refs configured for 'git push': dev pushes to dev (up to date) master pushes to master (up to date) serverfix pushes to serverfix (up to date) serverfix2 pushes to serverfix2 (up to date)
$ vi index.html <<<<<<< HEAD <div id="footer">contact : email.support@github.com</div> ======= <div id="footer"> please contact us at support@github.com </div> >>>>>>> iss53
$ git remote show origin #查询某一个远程服务器的详情 * remote origin Fetch URL: mobgit@134.32.51.60:learngit.git Push URL: mobgit@134.32.51.60:learngit.git HEAD branch (remote HEAD is ambiguous, may be one of the following): dev master Remote branches: dev tracked master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (up to date)
GitLab是一个用Ruby on Rails写的开源的版本管理系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。它拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。 可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。 GitLab是目前搭建内部Git服务器的首选,当然如果要求不高的话,我们也可以直接使用SSH协议来快速搭建Git服务端。