defhello(name, age, sex, *args): print("Hello, My name is {name}.".format(name=name)) print("I'm {age} years old.".format(age=age)) print("I'm a {sex}".format(sex=sex))
if __name__ == "__main__": file_name = sys.argv[0] name = sys.argv[1] age = sys.argv[2] sex = sys.argv[3] other = sys.argv[4:] hello(name, age, sex, *other)
+
调用脚本:
+
1
python test_sysargv.py zhangsan 13 man nibi ss
+
脚本输出:
+
1 2 3 4 5 6
Hello, My name is zhangsan. I'm 13 years old. I'm a man Other word: nibi ss
defhello(name, age, sex, *args): print("Hello, My name is {name}.".format(name=name)) print("I'm {age} years old.".format(age=age)) print("I'm a {sex}".format(sex=sex))
if __name__ == "__main__": print("Format of transfer file: {type}".format(type=args.type)) if args.name and args.age and args.sex: hello(args.name, args.age, args.sex)
+
执行脚本:
+
1
python3 test_argparse.py -t json -n zhangsan -a 13 -s man
+
脚本成功输出:
+
1 2 3 4 5 6
Format of transfer file: json Hello, My name is zhangsan. I'm 13 years old. I'm a man Other word:
optional arguments: -h, --help show this help message and exit --name NAME, -n NAME name attribute: 非必要属性 --age AGE, -a AGE age attribute: 非必要属性 --sex SEX, -s SEX sex attribute: 非必要属性 --type TYPE, -t TYPE type attribute: 非必要属性
from flask import Flask, Blueprint, request from apscheduler.executors.pool import ThreadPoolExecutor from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.jobstores.redis import RedisJobStore import time
if __name__ == "__main__": server = TCPServer(("", 5000), EchoHandler) server.serve_forever() ```
```python from socketserver import StreamRequestHandler, TCPServer, ThreadingTCPServer import time
classEchoHandler(StreamRequestHandler): defhandle(self): print("Got Connection Address: %s" % str(self.client_address)) for line in self.rfile: print(line) self.wfile.write(bytes("hello {}".format(line.decode('utf-8')).encode('utf-8')))
if __name__ == "__main__": serv = ThreadingTCPServer(("", 5000), EchoHandler) serv.serve_forever()
type APIUser struct { ID string`gorm:"primaryKey,column:id"` UserName string`gorm:"column:username"` FirstName string`gorm:"first_name"` LastName string`gorm:"last_name"` }
funcadvancedQueryRow(db *gorm.DB){
// 智能选择字段,如果经常只需要查询某些字段,可以重新定义小结构体 var apiUser []APIUser result := db.Model(&User{}).Find(&apiUser) for _, user := range apiUser{ fmt.Println(user) } fmt.Println(result.Error, result.RowsAffected)
// 扫描结果绑定值map[string]interface{} 或者 []map[string]interface{} var users []map[string]interface{} result = db.Model(&User{}).Find(&users) for _, user := range users{ fmt.Println(user) } fmt.Println(result.Error, result.RowsAffected)
// Pluck查询单个列,并将结果扫描到切片 var emails []string result = db.Model(&User{}).Pluck("email",&emails) fmt.Println(emails) fmt.Println(result.Error, result.RowsAffected)
// Count查询 var count int64 result = db.Model(&User{}).Where("date > ?", "2012-10-22").Count(&count) fmt.Println(count) fmt.Println(result.Error, result.RowsAffected) }
// 将查询SQL的结果映射到指定的单个变量中 var oneUser User result := db.Raw("SELECT * FROM user LIMIT 1").Scan(&oneUser) fmt.Println(oneUser) fmt.Println(result.Error, result.RowsAffected)
// 将查询SQL的批量结果映射到列表中 var users []User result = db.Raw("SELECT * FROM user").Scan(&users) for _, user := range users { fmt.Println(user) } fmt.Println(result.Error, result.RowsAffected)
var updateUser User result = db.Raw("UPDATE users SET username = ? where id = ?", "toms jobs", "ab6f089b-3272-49b5-858f-a93ed5a43b4f").Scan(&updateUser) fmt.Println(updateUser) fmt.Println(result.Error, result.RowsAffected)
// 直接通过Exec函数执行Update操作,不返回任何查询结果? result = db.Exec("UPDATE user SET username = ? where id = ?", "toms jobs", "ab6f089b-3272-49b5-858f-a93ed5a43b4f") fmt.Println(result.Error, result.RowsAffected)
def__call__(self, instance: LocalProxy, *args: t.Any, **kwargs: t.Any) -> t.Any: """Support calling unbound methods from the class. For example, this happens with ``copy.copy``, which does ``type(x).__copy__(x)``. ``type(x)`` can't be proxied, so it returns the proxy type and descriptor. """ return self.__get__(instance, type(instance))(*args, **kwargs)
classAppContext: """The app context contains application-specific information. An app context is created and pushed at the beginning of each request if one is not already active. An app context is also pushed when running CLI commands. """
defpush(self) -> None: """Binds the app context to the current context.""" self._cv_tokens.append(_cv_app.set(self)) appcontext_pushed.send(self.app, _async_wrapper=self.app.ensure_sync)