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
| -- Define a function that increment one value | |
| inc x = x + 1 | |
| -- Call function | |
| inc |
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
| # Define TV class, notice class is an object too | |
| class TV(object): | |
| def __init__(self, size) | |
| self.size = size | |
| # This is an instance method and it always bound to specific instance of TV class | |
| def get_size(self): | |
| return self.size | |
| # Try to call class method | |
| TV.get_size(TV(40)) |
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
| import functools | |
| import inspect | |
| # decorator | |
| def check_role(f): | |
| # Use this decorator to keep update some attributes of f such as doc string, name ... | |
| @functools.wraps(f) | |
| def wrapper(*args, **kwargs): | |
| # make arguments as a dict, this avoid to check argument which is position or keyword argument | |
| func_args = inspect.getcallargs(f, *args, **kwargs) |
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
| # define a decorator simplest as possible | |
| def identify(f): | |
| return f | |
| # Use decorator | |
| def foo(): | |
| return 'fuzz' | |
| # the same as | |
| foo = identify(foo) |
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
| # Send GET request to github api to get OAuth token. | |
| curl --user "<username>" --data '{"scopes": ["gist"], "note": "Demo Oauth token"}' https://api.github.com/authorizations | |
| # Use OAuth token to access resource | |
| curl https://api.github.com/gitsts/starred?access_token=<token_string> | |
| # or use OAuth token on request header | |
| curl --header "Authorization: token <token_string>" https://api.github.com/gists/starred | |
| # List all authorizations | |
| curl --user "<username>" https://api.github.com/authorizations |
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
| # Send POST request with customize header | |
| curl --header "Content-Type: application/json" --header "authToken: <token_string>" --data @file.txt https://api.abc.com/endpoint |
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
| # Send POST request with data to github api to create a gist | |
| curl --user "<username>" --request POST --data '{"description": "Test create a github gists via api", "public": "true", "files": {"test.txt": {"content": "Hello"}}}' https://api.github.com/gists | |
| # or in short | |
| curl -u "<username>" -X POST -d '{"description": "Test create a github gists via api", "public": "true", "files": {"test.txt": {"content": "Hello"}}}' https://api.github.com/gists | |
| # or more short, if supplied -d (data) POST method can ommited as below | |
| curl -u "<username>" -d '{"description": "Test create a github gists via api", "public": "true", "files": {"test.txt": {"content": "Hello"}}}' https://api.github.com/gists | |
| # Send POST request with multiple data | |
| curl --data "login=<username>" --data "token=<token_string>" https://github.com/api/v2/json/user/show/<username> | |
| # or combine into single --data |
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
| # Send GET request to github api to retrieve info of an github user without header | |
| curl https://api.github.com/users/<username> | |
| # Send GET request to github api to retrieve info of an github user with header | |
| curl --include https://api.github.com/users/<username> | |
| # or | |
| curl -i https://api.github.com/users/<username> | |
| # Send GET request with basic auth use user credential to github api to retrieve protected resources | |
| curl --user "<username>:<password>" https://api.github.com/users/<username> |
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
| # zipping: python 3 return a zip object, python 2 return a list of tuples | |
| a = [1, 2, 3] | |
| b = ['a', 'b', 'c'] | |
| zipped = zip(a, b) # This result will only live for once used then zipped will become empty | |
| # to print value of zip | |
| # 1 use for loop as a iterator | |
| for x in zipped: | |
| print(x) # (1, 'a') ... | |
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
| # list | |
| a = ['Hello', "World", 'Python'] | |
| for i, v in enumerate(a): | |
| print(i, v) | |
| # Dictionary | |
| a = {'name': 'Peter', 'age': 25} | |
| for k, v in a.items(): # .iteritems() with python 2 | |
| print(k, v) |