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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import urllib, urllib2, json | |
RPC_URL = 'http://192.168.1.2/jsonrpc' | |
def pushStream(stream, rpc=RPC_URL): | |
''' | |
Push stream URL to XBMC jsonrpc api, XBMC play the streaming. |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import urllib2, cookielib, hashlib, urllib, json | |
EMAIL = '' | |
PWD = '' | |
URL = 'http://bozpy.sinaapp.com' | |
def post(filename): |
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
# coding: utf-8 | |
import MySQLdb | |
connect = MySQLdb.connect(user='root', passwd='', db='blog', host='127.0.0.1', port=3306) | |
cursor = connect.cursor() | |
cursor.execute('SELECT title, content, tags, created FROM blogs') | |
result = cursor.fetchall() |
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
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
""" | |
一个基于thread和queue的线程池,以任务为队列元素,动态创建线程,重复利用线程, | |
通过close和terminate方法关闭线程池。 | |
""" | |
import queue | |
import threading | |
import contextlib |
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
from django.db.models import Aggregate | |
from django.db.models.sql import aggregates | |
def custom_aggregation(select_query): | |
class SqlAggregate(aggregates.Aggregate): | |
sql_function = '' | |
sql_template = select_query | |
class VisitorRate(Aggregate): |
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
from django.contrib.auth.models import Permission, User | |
def get_user_permissions(user): | |
if user.is_superuser: | |
return Permission.objects.all() | |
return Permission.objects.filter(Q(group__user=user)|Q(user=user)).distinct() |
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
class DynamicFieldsModelSerializer(serializers.ModelSerializer): | |
""" | |
A ModelSerializer that takes an additional `fields` argument that | |
controls which fields should be displayed. | |
""" | |
def __init__(self, *args, **kwargs): | |
# Don't pass the 'fields' arg up to the superclass | |
fields = kwargs.pop('fields', None) |
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
# 关联字段用select_related不能用正常使用only选取关联对象的某些字段,可以变通一下 | |
a = Articles.objects.only('title').annotate(blog_name=F('blog__name')).first() | |
a.title | |
a.blog_name | |
# Django group by | |
from django.db.models import Count | |
Members.objects.values('designation').annotate(dcount=Count('designation')) |
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
class ClassPropertyDescriptor(property): | |
u""" | |
class property descriptor | |
""" | |
def __get__(self, obj, objtype=None): | |
if self.fget is None: | |
raise AttributeError("unreadable attribute") | |
return self.fget.__get__(obj, objtype)() |
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
# coding: utf-8 | |
u""" | |
线性数据结构, 栈, 队列, deques, 容器结构, 数据项之间存在相对的位置 | |
""" | |
class Stack(object): | |
u""" | |
栈 先进后出 |
OlderNewer