15AH, San Francisco

California, United States.

Send Your Mail At:

tianyingkejishe@sina.cn

Working Hours

Mon-Sat: 9.30am To 7.00pm

分类标题

Autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et dolore feugait.

分类归档 Python

【python】crcmod.predefined实现CRC校验

一、CRC简介

在数据传输过程中,无论传输系统的设计再怎么完美,差错总会存在,这种差错可能会导致在链路上传输的一个或者多个帧被破坏(出现比特差错,0变为1,或者1变为0),从而接受方接收到错误的数据。为尽量提高接受方收到数据的正确率,在接收方接收数据之前需要对数据进行差错检测,当且仅当检测的结果为正确时接收方才真正收下数据。检测的方式有多种,常见的有奇偶校验、因特网校验和循环冗余校验等。循环冗余校验是一种用于校验通信链路上数字传输准确性的计算方法(通过某种数学运算来建立数据位和校验位的约定关系的 [1] )。发送方计算机使用某公式计算出被传送数据所含信息的一个值,并将此值 附在被传送数据后,接收方计算机则对同一数据进行 相同的计算,应该得到相同的结果。如果这两个 CRC结果不一致,则说明发送中出现了差错,接收方计算机可要求发送方计算机重新发送该数据。在计算机网络通信中运用CRC校验时相对于其他校验方法就有一定的优势。CRC可以高比例的纠正信息传输过程中的错误,可以在极短的时间内完成数据校验码的计算,并迅速完成纠错过程,通过数据包自动重发的方式使得计算机的通信速度大幅提高,对通信效率和安全提供了保障。由于 CRC 算法检验的检错能力极强,且检测成本较低,因此在对于编码器和电路的检测中使用较为广泛。从检错的正确率与速度、成本等方面,都比奇偶校验等校验方式具有优势。因而,CRC 成为计算机信息通信领域最为普遍的校验方式。

二、python实现方法

python通过调用crcmod.predefined模块完成各种CRC校验机制。

三、参考例程

def crcFunc(self,mode,InputData):
        print(InputData)
        crcF= crcmod.predefined.Crc(mode)
        crcF.update(bytes(InputData))
        print(hex(crcF.crcValue))
        return hex(crcF.crcValue)

【Python】Tkinter GUI Toplevel多窗口模态处理

一、Python tkinter多窗口实现

此实例涉及到Tkinter的Toplevel组件应用,通过toplevel可以实现多窗口,从字面意思理解为顶层窗口。

from tkinter import *
root = Tk()
def create():
#创建一个顶级弹窗
    top = Toplevel()
    top.title('我的弹窗')
   # top.attributes('-alpha',0.5)这个可以设置弹出窗口的透明度
    msg = Message(top,text = '类似于弹出窗口,具有独立的窗口属性。',width = 150)
    msg.pack()
Button(root,text = '创建一个顶级窗口',command = create).pack(padx = 20,pady = 50)
mainloop()

二、Python窗口的模态与非模态

大家都很清楚对于多窗口应用的场景中,对主窗口与子窗口之间的关系应用不同,大多数都是希望在处理子窗口的时候,不希望使用者同时可操作主窗口的逻辑,所以模态的概念由此产生,那对于上面的实例如何实现呢,看下面改进方案:

from tkinter import *
root = Tk()
def create():
#创建一个顶级弹窗
    top = Toplevel()
    top.title('我的弹窗')
    top.grab_set()#使当前页面活跃,模态实现
    top.focus_set()#使鼠标键盘聚焦在当前页面
   # top.attributes('-alpha',0.5)这个可以设置弹出窗口的透明度
    msg = Message(top,text = '类似于弹出窗口,具有独立的窗口属性。',width = 150)
    msg.pack()
Button(root,text = '创建一个顶级窗口',command = create).pack(padx = 20,pady = 50)
mainloop()

Python=Lable和LableFrame应用

import os
import tkinter as tk
from tkinter import *

master = tk.Tk()
entry1_str = tk.StringVar()

def helloCallBack():
    entry1_str.set("好的回购好的回购好的回购好的回购好的回购好的回购")
    print("hello word")
    
def gui():
    master.title("Lable家族")
    master.geometry("800x480")
    master.update()

    theLableFrame = tk.LabelFrame(master,text="文件列表",padx=5, pady=5,bd=5)
    theLableFrame.pack(fill=BOTH, expand=YES, padx=10, pady=10)
    
    theLable = tk.Label(theLableFrame,width = master.winfo_width(),wraplength = 80,anchor=NW,textvariable=entry1_str)
    theLable.pack(fill=BOTH, expand=YES, padx=10, pady=10)
    
    theButton = tk.Button(master, text ="点我", command = helloCallBack)
    theButton.pack(side='bottom')
    
    master.mainloop()


if __name__ == '__main__':
    gui()
    

实现效果图如下:

python import引入不同路径下的模块

python 包含子目录中的模块方法比较简单,关键是能够在sys.path里面找到通向模块文件的路径。
下面将具体介绍几种常用情况:
(1)主程序与模块程序在同一目录下:
如下面程序结构:
`– src
|– mod1.py
`– test1.py
若在程序test1.py中导入模块mod1, 则直接使用 import mod1或from mod1 import *;

(2)主程序所在目录是模块所在目录的父(或祖辈)目录
如下面程序结构:
`– src
|– mod1.py
|– mod2
| `– mod2.py
`– test1.py
若在程序test1.py中导入模块mod2, 需要在mod2文件夹中建立空文件__init__.py文件(也可以在该文件中自定义输出模块接口); 然后使用 from mod2.mod2 import * 或import mod2.mod2.

(3)主程序导入上层目录中模块或其他目录(平级)下的模块
如下面程序结构:
`– src
|– mod1.py
|– mod2
| `– mod2.py
|– sub
| `– test2.py
`– test1.py
若在程序test2.py中导入模块mod1和mod2。首先需要在mod2下建立__init__.py文件(同(2)),src下不必建立该文件。然后调用方式如下:
下面程序执行方式均在程序文件所在目录下执行,如test2.py是在cd sub;之后执行python test2.py
而test1.py是在cd src;之后执行python test1.py; 不保证在src目录下执行python sub/test2.py成功。
import sys
sys.path.append(“..”)
import mod1
import mod2.mod2

(4)从(3)可以看出,导入模块关键是能够根据sys.path环境变量的值,找到具体模块的路径。这里仅介绍上面三种简单情况。

Python3异常-AttributeError: module ‘sys’ has no attribute ‘setdefaultencoding

基于python3.6.1版本,在一个.py文件中,加入这3行:
import requests, re, sys
reload(sys)
sys.setdefaultencoding(“utf-8”)
出现这样的错误:
sys.setdefaultencoding(“utf-8”)
AttributeError: module ‘sys’ has no attribute ‘setdefaultencoding’
原因分析:
Python3字符串默认编码unicode, 所以sys.setdefaultencoding也不存在了
解决:
去掉,sys.setdefaultencoding

PyQt5 AttributeError: ‘QGridLayout’ object has no attribute ‘setMargin’

PyQt4和PyQt5版本差异。

PyQt4采用:QVBoxLayout的setMargin方法

PyQt5采用:QVBoxLayout的setContentsMargins方法

 

错误提示: return QtGui.QApplication.translate(context, text, disambig)
AttributeError: module ‘PyQt5.QtGui’ has no attribute ‘QApplication’

修改:return QApplication.translate(context, text, disambig)

原因:PyQt4和PyQt5版本差异。

 

错误提示:NameError: name ‘unicode’ is not defined

修改:Python2 的unicode 函数在 Python3 中被命名为 str。在 Python3 中使用 ·str 来代替 Python2 中的 unicode.

原因:There is no such name in Python 3, no. You are trying to run Python 2 code in Python 3. In Python 3, unicode has been renamed to str.