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
import heroku | |
HEROKU_TOKEN = "xx-xx-xx" #used for instance restart | |
APP_NAME = "xxx" | |
def restart_heroku(auto=False): | |
'''restart heroku instance to re-allocate IP''' | |
cloud = heroku.from_key(HEROKU_TOKEN) | |
app = cloud.apps[APP_NAME] | |
for p in app.processes: | |
p.restart() |
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
db.stud.aggregate([ | |
{$match:{"scores.type":{$in:["exam","homework"]}}}, | |
{$unwind:"$scores"}, | |
{$group:{_id:"$class_id",csall:{$avg:"$scores.score"}}}, | |
{$sort:{csall:-1}}, | |
{$limit:5} | |
] | |
) |
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
db.zips.aggregate([ | |
{$project: | |
{ | |
fc: {$substr : ["$city",0,1]}, | |
city:1,pop:1 | |
} | |
}, | |
{$match:{fc:{$lte:"9",$gte:"0"}}} , | |
{ | |
$group: { |
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
db.posts.aggregate([ | |
{$project:{comments:1, author:1}}, | |
{$unwind:"$comments"}, | |
{$group:{_id:"$comments.author",commentnum:{$sum:1}}}, | |
{$sort:{commentnum:-1}}, | |
{$limit:2} | |
]); |
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
db.pop.aggregate([ | |
{$group:{_id:{state:"$state",city:"$city"}, allpop:{$sum:"$pop"}}}, | |
{$match:{allpop:{$gt:25000},"_id.state":{$in:["CA","NY"]}}}, | |
{$group:{_id:null, avgpop:{$avg:"$allpop"}}} | |
] | |
) |
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
a = set([hashabledict({1:11}), hashabledict({2:22})]) | |
b = set([hashabledict({1:11}), hashabledict({3:33})]) | |
for i in range(3): | |
print "set a:", a, id(a) | |
copyofa = set(a) | |
print "shallow copy ofset a:", copyofa, id(copyofa) | |
delta = copyofa-b | |
print "delta", delta | |
for item in delta: | |
print id(item) |
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
a = set([hashabledict({1:11}), hashabledict({2:22})]) | |
b = set([hashabledict({1:11}), hashabledict({3:33})]) | |
for i in range(3): | |
print "set a:", a | |
delta = a-b | |
print "delta", delta, id(delta) | |
for item in delta: | |
print id(item) #always same | |
item[1] = "xx" | |
print "*"*10 |
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
import copy | |
a = set([hashabledict({1:11}), hashabledict({2:22})]) | |
b = set([hashabledict({1:11}), hashabledict({3:33})]) | |
for i in range(3): | |
print "set a:", a | |
delta = a-b | |
print "delta", delta | |
for item in copy.deepcopy(delta): | |
print id(item) | |
item[1] = "xx" |
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
### in a seperated file define the function | |
from django.core.exceptions import SuspiciousOperation | |
def skip_suspicious_exception(record): | |
if record.exc_info: | |
exc_type, exc_value = record.exc_info[:2] | |
if isinstance(exc_value, SuspiciousOperation): | |
return False | |
return True | |
### in settings.py ### |
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
def get_free_space(folder): | |
""" Return folder/drive free space | |
""" | |
if platform.system() == 'Windows': | |
free_bytes = ctypes.c_ulonglong(0) | |
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes)) | |
return free_bytes.value | |
else: | |
st = os.statvfs(folder) | |
return st.f_bavail * st.f_frsize |
NewerOlder