上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 18 下一页
摘要: 在 views.py 中添加函数 向数据库中添加数据 def add_persons(request): for i in range(15): person = Person() flag = random.randrange(100) person.p_name = "Hany_ %d"%(i) 阅读全文
posted @ 2020-05-09 17:52 Hany47315 阅读(606) 评论(0) 推荐(0)
摘要: 在 models.py 中添加 from django.db import models # Create your models here. class Person(models.Model): # 伪装成 models p_name = models.CharField(max_length= 阅读全文
posted @ 2020-05-09 14:41 Hany47315 阅读(429) 评论(0) 推荐(1)
摘要: 单例模式的懒汉式 在导入模块的时候,可能会无意中创建一个对象,但当时根本用不到 懒汉式实例化能够确保在实际需要时才创建对象 懒汉式实例化是一种节约资源并仅在需要时才创建它们的方式 class Singleton: _instance = None def __init__(self): if not 阅读全文
posted @ 2020-05-09 13:09 Hany47315 阅读(334) 评论(0) 推荐(0)
摘要: 设计模式分类: 结构型 行为型 创建型 单例模式属于创建型设计模式 单例模式主要使用在 日志记录 ->将多项服务的日志信息按照顺序保存到一个特定日志文件 数据库操作 -> 使用一个数据库对象进行操作,保证数据的一致性 打印机后台处理程序 以及其他程序 该程序运行过程中 只能生成一个实例 避免对同一资 阅读全文
posted @ 2020-05-09 12:15 Hany47315 阅读(160) 评论(0) 推荐(0)
摘要: 以下图示为 学习过程中,在千锋教育视频上截图的 视频链接: https://www.bilibili.com/video/BV1rx411X717?p=11 2020-05-09 阅读全文
posted @ 2020-05-09 11:56 Hany47315 阅读(117) 评论(0) 推荐(0)
摘要: 在创建好的 app3 目录下的 models.py 中 创建对 数据表(学生和班级)的描述 分析: 学生和班级是一对多的关系 班级表的设计 class Grade(models.Model): # 设置班级名字 g_name = models.CharField(max_length=32) 学生表 阅读全文
posted @ 2020-05-08 18:16 Hany47315 阅读(783) 评论(0) 推荐(0)
摘要: 在根目录下创建一个 app3 app3 是新 app 的名字 创建一个 urls.py 在 urls.py 中添加 urlpatterns 列表 容纳需要显示在页面上的函数 from django.conf.urls import url from app3 import views urlpatt 阅读全文
posted @ 2020-05-08 11:27 Hany47315 阅读(241) 评论(0) 推荐(0)
摘要: 示例1 from time import ctime,sleep #导包就不需要使用包名.函数了 def timefun(func): def wrappedfunc(): print("%s called at %s"%(func.__name__,ctime())) func()#内部函数需要使 阅读全文
posted @ 2020-05-08 10:00 Hany47315 阅读(164) 评论(0) 推荐(0)
摘要: def foo(): print("foo") print(foo) # 输出foo的地址 foo()#对foo函数的调用 def foo(): print("foo2") foo = lambda x : x+1 # 使用foo对象接收函数 print(foo(2)) def w(func): d 阅读全文
posted @ 2020-05-08 09:44 Hany47315 阅读(115) 评论(0) 推荐(0)
摘要: """ 输入 graph 输入的图 src 原点 返回 dis 记录源点到其他点的最短距离 path 路径 """ import json def dijkstra(graph, src): if graph == None: return None # 定点集合 nodes = [i for i 阅读全文
posted @ 2020-05-08 09:42 Hany47315 阅读(193) 评论(0) 推荐(0)
摘要: #coding=utf-8 class UpperAttrMetaClass(type): # __new__ 是在__init__之前被调用的特殊方法 # __new__是用来创建对象并返回之的方法 # 而__init__只是用来将传入的参数初始化给对象 # __new__能够控制对象的创建 # 阅读全文
posted @ 2020-05-08 09:40 Hany47315 阅读(173) 评论(0) 推荐(0)
摘要: def test(number): #在函数内部再定义一个函数,并且这个函数用到了外边函数的变量,那么将这个函数以及用到的一些变量称之为闭包 def test_in(number_in): print("in test_in 函数, number_in is %d"%number_in) retur 阅读全文
posted @ 2020-05-08 09:39 Hany47315 阅读(132) 评论(0) 推荐(0)
摘要: #方法一 import random range_lst = [random.randint(0,100) for i in range(100)] # 创建一个包含有 100 个数据的随机数 range_set = set(range_lst) # 创建集合,不包含重复元素 for num in 阅读全文
posted @ 2020-05-08 09:38 Hany47315 阅读(347) 评论(0) 推荐(0)
摘要: def func_class(string): if string == 'class_one': class class_one: pass return class_one else: class class_two: pass return class_two MyClass = func_c 阅读全文
posted @ 2020-05-08 00:59 Hany47315 阅读(487) 评论(0) 推荐(0)
摘要: class ObjectCreator(object): pass print(ObjectCreator) # 打印 ObjectCreator.name = 'XiaoLiu' # 对ObjectCreator类增加属性,以后使用ObjectCreator类时,都具有name属性 g = lam 阅读全文
posted @ 2020-05-08 00:58 Hany47315 阅读(190) 评论(0) 推荐(0)
摘要: dic = {'a':123,'b':456} print("{0}:{1}".format(*dic)) a:b 2020-05-08 阅读全文
posted @ 2020-05-08 00:57 Hany47315 阅读(221) 评论(0) 推荐(0)
摘要: a = [1,2,3,4] print(id(a)) b = a print(id(b)) # 地址相同 a.append('a') print(a) print(b)#b和a的值一致,a改变,b就跟着改变 ''' 2342960103104 2342960103104 [1, 2, 3, 4, ' 阅读全文
posted @ 2020-05-08 00:55 Hany47315 阅读(124) 评论(0) 推荐(0)
摘要: 查看一个对象的引用计数 a = "Hello World " import sys print("a的引用计数为:",sys.getrefcount(a)) '''a的引用计数为: 4''' a = 23 b = a def func(a): print(a) lst = [a] 2020-05-0 阅读全文
posted @ 2020-05-08 00:54 Hany47315 阅读(358) 评论(0) 推荐(0)
摘要: class Person(object): def __init__(self,name = None,age = None): self.name = name#类中拥有的属性 self.age = age def eat (self): print("%s在吃东西"%(self.name)) p 阅读全文
posted @ 2020-05-08 00:53 Hany47315 阅读(608) 评论(0) 推荐(0)
摘要: 查看complex的__class__属性 a = 5+2j print(a.__class__) print(a.__class__.__class__) ''' <class 'complex'> <class 'type'> ''' 查看int的__class__属性 a = 123 prin 阅读全文
posted @ 2020-05-08 00:51 Hany47315 阅读(326) 评论(0) 推荐(0)
摘要: a = "abc" b = a[:] print(a,b)#值相同 print(id(a),id(b))#地址相同(字符串是不可变类型) d = dict(name = "Xiaoming",age = 22) d_copy = d.copy() print( d ,id(d)) print(d_c 阅读全文
posted @ 2020-05-08 00:50 Hany47315 阅读(101) 评论(0) 推荐(0)
摘要: class Person(object): def __init__(self,name,age,taste): self.name = name self._age = age self.__taste = taste def showPerson(self): print(self.name) 阅读全文
posted @ 2020-05-08 00:48 Hany47315 阅读(181) 评论(0) 推荐(0)
摘要: class MyList(object): """自定义的一个可迭代对象""" def __init__(self): self.items = [] def add(self, val): self.items.append(val) def __iter__(self): myiterator 阅读全文
posted @ 2020-05-08 00:47 Hany47315 阅读(159) 评论(0) 推荐(0)
摘要: import time # yield配合next使用 def work1(): while True: print(" work1 ") yield time.sleep(0.3) def work2(): while True: print(" work2 ") yield time.sleep 阅读全文
posted @ 2020-05-08 00:46 Hany47315 阅读(183) 评论(0) 推荐(0)
摘要: #大整数对象池 b = 1500 a = 1254 print(id(a)) print(id(b)) b = a print(id(b)) a1 = "Hello 垃圾机制" a2 = "Hello 垃圾机制" print(id(a1),id(a2)) del a1 del a2 a3 = "He 阅读全文
posted @ 2020-05-08 00:45 Hany47315 阅读(98) 评论(0) 推荐(0)
摘要: ''' 创建mutex = threading.Lock( ) 锁定mutex.acquire([blocking]) 释放mutex.release( ) 创建->锁定->释放 ''' from threading import Thread,Lock from time import sleep 阅读全文
posted @ 2020-05-08 00:44 Hany47315 阅读(101) 评论(0) 推荐(0)
摘要: class Person(object): def __init__(self,name = None,age = None): self.name = name self.age = age def __str__(self): return "%s 的年龄为 %d 岁 %s性"%(self.na 阅读全文
posted @ 2020-05-08 00:43 Hany47315 阅读(294) 评论(0) 推荐(0)
摘要: G = (x*2 for x in range(4)) print(G) print(G.__next__()) print(next(G))#两种方法等价 # G每一次读取,指针都会下移 for x in G: print(x,end = " ") 2020-05-08 阅读全文
posted @ 2020-05-08 00:42 Hany47315 阅读(115) 评论(0) 推荐(0)
摘要: class FibIterator(object): """斐波那契数列迭代器""" def __init__(self, n): """ :param n: int, 指明生成数列的前n个数 """ self.n = n # current用来保存当前生成到数列中的第几个数了 self.curre 阅读全文
posted @ 2020-05-08 00:40 Hany47315 阅读(314) 评论(0) 推荐(0)
摘要: 类方法 class ObjectCreator(object): pass @classmethod def testClass(cls): cls.temp = 666 print(cls.temp) test = type("Test",(ObjectCreator,),{'testClass' 阅读全文
posted @ 2020-05-08 00:39 Hany47315 阅读(163) 评论(0) 推荐(0)
摘要: import types class ppp: pass p = ppp()#p为ppp类实例对象 def run(self): print("run函数") r = types.MethodType(run,p) #函数名,类实例对象 r() ''' run函数 ''' 2020-05-08 阅读全文
posted @ 2020-05-08 00:38 Hany47315 阅读(178) 评论(0) 推荐(0)
摘要: class Days(object): def __init__(self): self.__days = 0 @property def days(self):#获取函数,名字是days days 是get方法 return self.__days @days.setter #get方法的set方 阅读全文
posted @ 2020-05-08 00:36 Hany47315 阅读(199) 评论(0) 推荐(0)
摘要: import pdb a = 'aaa' pdb.set_trace( ) b = 'bbb' c = 'ccc' final = a+b+c print(final) import pdb a = 'aaa' pdb.set_trace( ) b = 'bbb' c = 'ccc' pdb.set 阅读全文
posted @ 2020-05-08 00:35 Hany47315 阅读(195) 评论(0) 推荐(0)
摘要: def counter(start = 0): def incr(): nonlocal start #分别保存每一个变量的临时值、类似yield start += 1 return start return incr c1 = counter(5) print(c1()) c2 = counter 阅读全文
posted @ 2020-05-08 00:34 Hany47315 阅读(152) 评论(0) 推荐(0)
摘要: ''' timeit库Timer函数 ''' from timeit import Timer def test1(): l = list(range(1000)) def test2(): l = [] for i in range(1000): l.append(i) def test3(): 阅读全文
posted @ 2020-05-08 00:33 Hany47315 阅读(201) 评论(0) 推荐(0)
摘要: def upper_attr(future_class_name, future_class_parents, future_class_attr): #遍历属性字典,把不是__开头的属性名字变为大写 newAttr = {} for name,value in future_class_attr. 阅读全文
posted @ 2020-05-08 00:32 Hany47315 阅读(140) 评论(0) 推荐(0)
摘要: from collections import Iterable print(isinstance([],Iterable)) print(isinstance( {}, Iterable)) print(isinstance( (), Iterable)) print(isinstance( 'a 阅读全文
posted @ 2020-05-08 00:30 Hany47315 阅读(999) 评论(0) 推荐(0)
摘要: 使用__slots__时,子类不受影响 class Person(object): __slots__ = ("name","age") def __str__(self): return "姓名:%s,年龄:%d"%(self.name,self.age) p = Person() class m 阅读全文
posted @ 2020-05-08 00:29 Hany47315 阅读(124) 评论(0) 推荐(0)
摘要: # import hashlib # mima = hashlib.md5()#创建hash对象,md5是信息摘要算法,生成128位密文 # print(mima) # # mima.update('参数')使用参数更新哈希对象 # print(mima.hexdigest())#返回16进制的数字 阅读全文
posted @ 2020-05-08 00:27 Hany47315 阅读(173) 评论(0) 推荐(0)
摘要: """ 1、gc.set_debug(flags) 设置gc的debug日志,一般设置为gc.DEBUG_LEAK 2、gc.collect([generation]) 显式进行垃圾回收,可以输入参数,0代表只检查第一代的对象, 1代表检查一,二代的对象,2代表检查一,二,三代的对象,如果不传参数, 阅读全文
posted @ 2020-05-08 00:26 Hany47315 阅读(296) 评论(0) 推荐(0)
上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 18 下一页