python

How to get COUNT query in django

我只是一个虾纸丫 提交于 2021-02-20 08:30:46
问题 To get a query in django I can do: >>> print User.objects.all().query SELECT `auth_user`.`id`, `auth_user`.`username`, `auth_user`.`first_name`, `auth_user`.`last_name`, `auth_user`.`email`, `auth_user`.`password`, `auth_user`.`is_staff`, `auth_user`.`is_active`, `auth_user`.`is_superuser`, `auth_user`.`last_login`, `auth_user`.`date_joined` FROM `auth_user` However, how would I get the query it builds when doing a COUNT? >>> User.objects.all().count().query Traceback (most recent call last):

Python数据结构———队列

痞子三分冷 提交于 2021-02-20 08:25:23
队列(Queue) 队列也是一系列有顺序的元素的集合,新元素的加入在队列的一端,叫做“队尾”(rear),已有元素的移除发生在队列的另一端,叫做“队首”(front),和栈不同的是,队列只能在队尾插入元素,在队首删除元素。最新加入的元素必须处于队尾,在队列停留时间最长的元素处于队首。可以把队列想象成在银行前排队的人群,排在最前面的人第一个办理业务,新来的人只能在后面排队,直到轮到他们为止。这是一种先进先出(FIFO,First-In-First-Out)的数据结构。 队列有两种主要操作:向队列中插入新元素和删除队列中的元素。插入操作也叫做入队,删除操作也叫做出队。入队操作在队尾插入新元素,出队操作删除队头的元素。 队列的另外一项重要操作是读取队头的元素。这个操作叫做peek()。该操作返回队头元素,但不把它从队列中删除。除了读取队头元素,我们还想知道队列中存储了多少元素,可以使用size()满足该需求。 队列Queue的操作: Queue ( ) 定义一个空队列,无参数,返回值是空队列。 enqueue ( item ) 在队列尾部加入一个数据项,参数是数据项,无返回值。 dequeue ( ) 删除队列头部的数据项,不需要参数,返回值是被删除的数据,队列本身有变化。 isEmpty ( ) 检测队列是否为空。无参数,返回布尔值。 size ( ) 返回队列数据项的数量。无参数

23、Python之加密&解密以及加签&验签

笑着哭i 提交于 2021-02-20 08:07:13
一、rsa基本原理 关于加密与加签其目的就是为了保证数据传输的安全性,加签的作用就像签名,告诉别人这个就是我发的数据,别人无法模仿,加密的作用是传输的报文是一串密文,他人无法看懂什么意思,下图描述了使用rsa进行加签,加密,验签,解密的过程。 AB双方生成2对秘钥,A用自己的私钥进行签名(表示是自己发的数据),然后用B的公钥进行加密(这个数据是密文,第三方压根看不懂),B在接收到A发过来的数据时,先用B的私钥进行解密(嘿嘿,只有我能解密),随后用A的公钥进行验签(嗯,就是A发过来的数据),最终使用A传过来的明文进行后续业务处理(上图中B端数据描述不太准确,忽略,懒的画图)。后续通信亦是如此! 二、python中rsa操作 python中使用rsa模块,首先我们要先生成2对秘钥值,在python中使用下面代码生成秘钥值(记得生成2对)。 1 import rsa 2 3 import base64 4 # 生成密钥 5 (pubkey, privkey) = rsa.newkeys(1024 ) 6 # 保存密钥 7 with open( ' public.pem ' , ' w+ ' ) as f: 8 f.write(pubkey.save_pkcs1().decode()) 9 10 with open( ' private.pem ' , ' w+ ' ) as f: 11

Python数据结构与算法——双端队列Dequeue

老子叫甜甜 提交于 2021-02-20 08:06:51
点击上方 蓝字 关注我们 双端队列Dequeue 双端队列是一种有序的数据集,与队列相似,但双端队列的两端都可以作为队首和队尾( 即数据在两端都可以删除和插入 ) 某种意义上来说,双端队列Dequeue 集合了栈和队列的功能 ,Dequeue既可以实现栈也可以实现队列 双端队列的操作: Dequeue() 创建一个双端队列 addFront() 队首加入数据 addFear() 队尾加入数据 removeFront() 队首删除数据 removeFear() 队尾删除数据 size() 双端队列元素个数 isEmpty() 是否为空 双端队列使用实例: 双端队列代码: 双端队列应用—— “回文词”判定 “回文词”: 正读和反读都一样的词 例:radar(雷达),madam,foot,“上海自来水来自海上”,“山东落花生花落” 算法: 利用双端队列Dequeue,先将字符串加入双端队列,再从两端开始移除判断是否相同,最后剩一个字符 代码: 所有代码: class Dequeue () : """双端队列""" def __init__ (self) : self.items = [] def addFront (self, item) : self.items.append(item) def addFear (self, item) : self.items.insert( 0 ,

concatenation of two or more base64 strings in python

元气小坏坏 提交于 2021-02-20 06:50:58
问题 I'm tring to concatenate two strings encoded to base64 but it doesn't really work, just prints the first string in concatanation: q = base64.b64encode("StringA") print q # prints an encoded string q = q+base64.b64encode("StringB") print q # prints an encoded string print base64.b64decode(q) # just prints "StringA" 回答1: You are decoding a string that is concatenation of two base64 strings. This is not correct. You should do something like this - base64.b64decode(base64.b64encode("StringA" +

concatenation of two or more base64 strings in python

这一生的挚爱 提交于 2021-02-20 06:50:50
问题 I'm tring to concatenate two strings encoded to base64 but it doesn't really work, just prints the first string in concatanation: q = base64.b64encode("StringA") print q # prints an encoded string q = q+base64.b64encode("StringB") print q # prints an encoded string print base64.b64decode(q) # just prints "StringA" 回答1: You are decoding a string that is concatenation of two base64 strings. This is not correct. You should do something like this - base64.b64decode(base64.b64encode("StringA" +

Displaying image without waitKey

放肆的年华 提交于 2021-02-20 06:50:30
问题 This Python code displayed an image in full screen: blank_image = cv2.imread('blank.jpg') cv2.namedWindow("bw", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("bw", cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN) cv2.imshow("bw", blank_image) cv2.waitKey(0) The problem is the code is going to run on a Linux machine without keyboard. Calling waitKey means the UI processing will not be done until a key event occurs, and thus the contradiction. Is there any way beside the waitKey, then?

Displaying image without waitKey

一世执手 提交于 2021-02-20 06:50:29
问题 This Python code displayed an image in full screen: blank_image = cv2.imread('blank.jpg') cv2.namedWindow("bw", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("bw", cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN) cv2.imshow("bw", blank_image) cv2.waitKey(0) The problem is the code is going to run on a Linux machine without keyboard. Calling waitKey means the UI processing will not be done until a key event occurs, and thus the contradiction. Is there any way beside the waitKey, then?

tkinter resize frame and contents with main window

£可爱£侵袭症+ 提交于 2021-02-20 06:45:09
问题 I am trying to work out how to control resizing of the window containing a frame. In the code I have three buttons across the top that should to stay exactly where they are. When I drag the window to expand it, I want the frame and the text box it contains to expand with the master window. I have read that columnconfigure and rowconfigure can be used but not sure how to implement it. from tkinter import * from tkinter import scrolledtext master_window = Tk() # Create the buttons btn_Image =

Why is ‘==‘ coming before ‘in’ in Python?

时间秒杀一切 提交于 2021-02-20 06:42:32
问题 The following code outputs False, when according to the Python Order of Operations it should output True (the order should be in -> ==, not the other way around). Why is == coming before in? y = "33" "3" in y == True Output False 回答1: The existing answers give helpful advice that you shouldn't compare booleans to True because it's redundant. However, none of the answers actually answer the root question: "why does "3" in y == True evaluate to False ?". That question was answered in a comment