-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
378 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,7 @@ crawler/common | |
|
||
# for jetbrain | ||
*/*.iml | ||
*.iml | ||
|
||
# for other | ||
*/winn/* | ||
|
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
39 changes: 39 additions & 0 deletions
39
java/leetcode/src/test/java/com/wxnacy/leetcode/TwoSumTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.wxnacy.leetcode; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
|
||
/** | ||
* 只出现一次的数字 | ||
* 本题分析文章详见 https://wxnacy.com/leetcode/problems/136-single-number/ | ||
*/ | ||
public class TwoSumTest | ||
{ | ||
@Test | ||
public void shouldAnswerWithTrue() | ||
{ | ||
assertArrayEquals(twoSum(new int[]{2, 7, 9}, 9), new int[]{0, 1}); | ||
} | ||
|
||
/** | ||
* 一遍哈希表 | ||
* 执行用时 : 6 ms, 在Two Sum的Java提交中击败了94.56% 的用户 | ||
* 内存消耗 : 37.9 MB, 在Two Sum的Java提交中击败了80.11% 的用户 | ||
*/ | ||
public int[] twoSum(int[] nums, int target) { | ||
HashMap<Integer, Integer> m = new HashMap<>(); | ||
for (int i = 0; i < nums.length; i++) { | ||
if (m.containsKey(target - nums[i])) { | ||
return new int[]{m.get(target - nums[i]), i}; | ||
} | ||
m.put(nums[i], i); | ||
} | ||
return new int[]{0, 0}; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
public class ArrayTest{ | ||
public static void main(String args[]){ | ||
System.out.println("Hello World"); | ||
int[] a = new int[]{1, 2, 3}; | ||
// System.out.println(a); | ||
int[] b = {1, 2, 3}; | ||
int[] c = new int[4]; | ||
c[0] = 3; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
# Author: wxnacy([email protected]) | ||
# Description: 使用文件系统来存储缓存值 | ||
# http://www.pythondoc.com/flask-cache/index.html | ||
''' | ||
FileSystemCache – filesystem | ||
使用文件系统来存储缓存值 | ||
CACHE_DEFAULT_TIMEOUT | ||
CACHE_DIR | ||
CACHE_THRESHOLD | ||
CACHE_ARGS | ||
CACHE_OPTIONS | ||
''' | ||
|
||
from flask import Flask | ||
from flask import jsonify | ||
from flask_caching import Cache | ||
import random | ||
|
||
app = Flask(__name__) | ||
cache = Cache(app, config={ | ||
"CACHE_TYPE": "filesystem", | ||
"CACHE_DIR": "/Users/wxnacy/Downloads/cache" | ||
}) | ||
|
||
@app.route('/filesystem') | ||
@cache.cached(timeout=10) | ||
def filesystem(): | ||
return jsonify({"num": random.randint(1, 100)}) | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
# Author: wxnacy([email protected]) | ||
# Description: 使用 redis 做缓存 | ||
# http://www.pythondoc.com/flask-cache/index.html | ||
''' | ||
RedisCache – redis | ||
相关配置 | ||
CACHE_DEFAULT_TIMEOUT | ||
CACHE_KEY_PREFIX | ||
CACHE_REDIS_HOST | ||
CACHE_REDIS_PORT | ||
CACHE_REDIS_PASSWORD | ||
CACHE_REDIS_DB | ||
CACHE_ARGS | ||
CACHE_OPTIONS | ||
CACHE_REDIS_URL | ||
不配置 CACHE_KEY_PREFIX 和 key_prefix 时,默认 key 为 flask_cache_view/{route} | ||
''' | ||
|
||
from flask import Flask | ||
from flask import jsonify | ||
from flask import request | ||
from flask_caching import Cache | ||
import random | ||
|
||
app = Flask(__name__) | ||
cache = Cache(app, config={ | ||
"CACHE_TYPE": "redis", | ||
"CACHE_REDIS_URL": "redis://localhost:6379/0", # redis 地址 | ||
"CACHE_DEFAULT_TIMEOUT": 10, # 全局过期时间 | ||
# key 前缀,key 为 redis_cache_view/{route} | ||
"CACHE_KEY_PREFIX": "redis_cache_", | ||
}) | ||
|
||
@app.route('/redis1') | ||
@cache.cached() | ||
def redis1(): | ||
''' | ||
key=redis_cache_view//redis1 | ||
''' | ||
return jsonify({"num": random.randint(1, 100)}) | ||
|
||
@app.route('/redis2') | ||
@cache.cached(timeout = 20, key_prefix='customize') | ||
def redis2(): | ||
''' | ||
自定义过期时间 20 | ||
key=redis_cache_customize | ||
''' | ||
return jsonify({"num": random.randint(1, 100)}) | ||
|
||
def make_cache_key(): | ||
path = request.path | ||
key = str(hash(request.args)) | ||
return f'{path}-{key}' | ||
|
||
|
||
@app.route('/redis3') | ||
@cache.cached(timeout = 20, key_prefix=make_cache_key) | ||
def redis3(): | ||
''' | ||
自定义 key | ||
key=redis_cache_/redis3-133156838395276 | ||
''' | ||
return jsonify({"num": random.randint(1, 100)}) | ||
if __name__ == "__main__": | ||
app.run(debug=True) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
# Author: wxnacy([email protected]) | ||
# Description: 使用本地Python字典缓存。这不是真正的线程安全。 | ||
# http://www.pythondoc.com/flask-cache/index.html | ||
''' | ||
SimpleCache – simple | ||
使用本地Python字典缓存。这不是真正的线程安全。 | ||
相关配置 | ||
CACHE_DEFAULT_TIMEOUT | ||
CACHE_THRESHOLD | ||
CACHE_ARGS | ||
CACHE_OPTIONS | ||
''' | ||
|
||
from flask import Flask | ||
from flask import jsonify | ||
from flask_caching import Cache | ||
import random | ||
|
||
app = Flask(__name__) | ||
cache = Cache(app, config={"CACHE_TYPE": "simple"}) | ||
|
||
@app.route('/simple') | ||
@cache.cached(timeout=10) | ||
def simple(): | ||
return jsonify({"num": random.randint(1, 100)}) | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
# Author: wxnacy([email protected]) | ||
# Description: | ||
|
||
import records | ||
|
||
db = records.Database('mysql+pymysql://root:[email protected]:3306/study?charset=utf8mb4') | ||
|
||
rows = db.query('select * from user') | ||
print(rows.dataset) | ||
print(rows.first()) | ||
|
||
# with open('/tmp/report.xls', 'wb') as f: | ||
# f.write(rows.export('xls')) |
Oops, something went wrong.