-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiary3.txt
853 lines (705 loc) · 25.8 KB
/
Diary3.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
【2018-03-06】【1】C#数据
对于结构赋值操作是拷贝内容
对于类则是增加引用技术
但是对于String却也是拷贝内容
struct ValueContain
{
public int value;
};
或者
class ValueContain
{
public int value;
};
// 对于结构可以省略 = new ValueContain();
ValueContain val = new ValueContain();
val.value = 10;
ValueContain val2 = val;
val2.value = 20;
Console.WriteLine("{0}", val.value);
结构输出的是10 类输出的是20
String str = "A";
String str2 = str;
str2 = "B";
Console.WriteLine("{0}", str);
输出 A
【2018-03-06】【2】C# Region
仅仅用于编辑器折叠代码使用
#region MyRegion
#endregion
【2018-03-06】【3】Python argparse
# https://www.jianshu.com/p/fef2d215b91d
import argparse
# 1.新建一个解析器
parser = argparse.ArgumentParser(
# description 可选
description="test case"
)
# 2.添加一个名字为arg0的定位参数
parser.add_argument("arg0")
# 3.添加一个可选参数
一种是通过一个-来指定的短参数 如-h
一种是通过--来指定的长参数 如--help
parser.add_argument("-v", "--verbosity")
# 注意如果长短参数同时存在则
# 通过 args = parser.parse_args() 访问 args 的参数成员只能用长参数访问 args.verbosity
# 4.避开设定值
parser.add_argument("-v", "--verbose", action="store_true")
# 这样指定参数时就不能设定参数值并且参数值为true 如果action="store_false"则参数值为false
# 5.设定值类型
parser.add_argument("x", type=int)
# 这样值类型不为int则报错
# 6.设定值范围
parser.add_argument("x", type=int, choices=[1,2,3])
# 这样值不为 1 or 2 or 3 则报错
# 7.添加help信息
parser.add_argument("x", type=int, help="args help")
# 8.设定默认参数
parser.add_argument("-v", "--verbose", default=1)
# 这样值不被指定时将会用默认值1 注意只能对可选参数使用
# *9.设置互斥参数
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")
# 这样 -v 和 -q 不能同时出现
【2018-03-07】SimplePref使用
// http://www.jishux.com/plus/view-672770-1.html
// https://zhuanlan.zhihu.com/p/25277481
// http://www.dpull.com/blog/2018-02-22-simpleperf
// https://developer.android.com/ndk/guides/simpleperf.html
下载最新的NDK 目录里有个simpleperf文件夹 运行里面的app_profiler.py进行剖析
如: python app_profiler.py -p com.xxx.yyy.zzz -nc -r "--duration 10 -g --call-graph dwarf" -nb
生成perf.data文件 运行report.py进行分析
如: python report.py --full-callgraph -g caller --gui --children
【2018-04-02】【1】Python面向对象
# http://www.runoob.com/python/python-object.html
# 类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称 按照惯例它的名称是 self
例子:
#coding=utf-8
class Test:
# 类文档
'Test Class'
# 类变量 相当于C++类静态变量
count = 0
# 通常传入的第一个参数叫self 这里为了突出self不是关键字
# 起名为address
def __init__(address, name):
print address
# 新增一个对象属性
address._name = name
Test.count += 1
def PrintName(self):
print self._name
def ModifyName(self, name):
# 修改一个对象属性
self._name = name
def DeleteName(self):
# 删除一个对象属性
del self._name
# 实例化对象 隐式传入对象地址作为__init__的第一个参数
t0 = Test("Test")
t0.ModifyName("ModifyName")
t0.PrintName()
t0.DeleteName()
# t0.PrintName()
# 输出类变量
print Test.count
【2018-04-02】【2】Python面向对象
# http://www.runoob.com/python/python-object.html
# 你也可以使用以下函数的方式来访问属性:
# getattr(obj, name[, default]): 访问对象的属性
# hasattr(obj,name): 检查是否存在一个属性
# setattr(obj,name,value): 设置一个属性 如果属性不存在 会创建一个新属性
# delattr(obj, name): 删除属性
例子:
#coding=utf-8
class Test:
# 类文档
'Test Class'
# 类成员 相当于C++类静态变量
count = 0
# 通常传入的第一个参数叫self 这里为了突出self不是关键字
# 起名为address
def __init__(address, name):
print address
# 新增一个对象属性
address._name = name
Test.count += 1
t0 = Test("Name")
# 输出True
print hasattr(t0, "_name")
# 输出False
print hasattr(t0, "_age")
# 输出Name
print getattr(t0, "_name")
# 报错不存在属性_age
# print getattr(t0, "_age")
# 删除属性_name
delattr(t0, "_name")
# 报错不存在属性_age)
# delattr(t0, "_age"
setattr(t0, "_name", "NewName")
# 不存在属性_age 输出Name 避免抛出异常
print getattr(t0, "_age", "Name")
【2018-04-03】【1】Python继承
# http://www.runoob.com/python/python-object.html
1.在继承中基类的构造(__init__()方法)不会被自动调用 它需要在其派生类的构造中亲自专门调用
2.在调用基类的方法时 需要加上基类的类名前缀 且需要带上self参数变量
3.Python总是首先查找对应类型的方法(或成员) 如果它不能在派生类中找到对应的方法(或成员) 它才开始到基类中逐个查找
例子:
class Base:
def __init__(self):
print "Base __init__ call"
def __del__(self):
print "Base __del__ call"
def Func(self):
print "Base Func"
class Derived(Base):
def __init__(self):
print "Derived __init__ call"
def __del__(self):
print "Derived __del__ call"
# 重写基类函数
def Func(self):
print "Derived Func"
# 只调用到派生类的构造函数
# derived = Derived()
class Derived_2(Base):
count = 0
def __init__(self):
Base.__init__(self)
Derived_2.count += 1
print "Derived_2 __init__ call"
def __del__(self):
# 函数可能无法被调用 垃圾回收时 类被销毁了
# Base.__del__(self)
# 类成员可能无法使用 垃圾回收时 类被销毁了
# print Base.count
# Derived_2.count -= 1
print "Derived_2 __del__ call"
# 运算符重载
def __str__(self):
return "Derived_2 toStr"
# 重写基类函数
def Func(self):
print "Derived_2 Func"
derived2 = Derived_2()
derived2.Func()
【2018-04-03】【2】Python面向对象
# http://www.runoob.com/python/python-object.html
1.对象变量
单下划线、双下划线、头尾双下划线说明:
__foo__: 定义的是特殊方法,一般是系统定义名字 类似 __init__() 之类的
_foo: 以单下划线开头的表示的是 protected 类型的变量 即保护类型只能允许其本身与子类进行访问 不能用于 from module import *
__foo: 双下划线的表示的是私有类型(private)的变量 只能是允许这个类本身进行访问了
2.类属性与方法
类的私有属性
__private_attrs: 两个下划线开头 声明该属性为私有 不能在类的外部被使用或直接访问 在类内部的方法中使用时 self.__private_attrs
类的方法
在类的内部 使用 def 关键字可以为类定义一个方法 与一般函数定义不同 类方法必须包含参数 self 且为第一个参数
类的私有方法
__private_method: 两个下划线开头 声明该方法为私有方法 不能在类地外部调用 在类的内部调用 self.__private_methods
【2018-04-03】【3】Python模块与包
# http://www.runoob.com/python/python-modules.html
# https://www.cnblogs.com/jiaxin359/p/7580375.html
# https://blog.csdn.net/luo123n/article/details/49849649
模块(Module): 是一个 Python 文件以 .py 结尾 包含了Python对象定义和Python语句
包(Package): 是文件夹 但该文件夹下必须存在 __init__.py 文件用于标识当前文件夹是一个包
例子:
目录结构
package/
subpackage/
__init__.py
__main__.py
subfile.py
subfile2.py
__init__.py
__main.py
main.py
在与package同级目录下package是一个包 package.subpackage也是一个包
package有模块main.py
package.subpackage有模块 subfile.py subfile2.py
python有两种加载文件的方法 一种是作为顶层的脚本
另一种时当做模块 如果你直接执行这个程序 那么这个文件就被当做是顶层脚本来执行了
__name__的值为模块名 顶层脚本的__name__为 __main__
模块内引用其他模块用import
如果你输入python -m myfile.py或者在其他的文件当中使用import来导入这个文件的时候 它就被当做模块来导入
相对import
subfile.py :
#coding=utf-8
# 绝对引用subfile2
import subfile2 [as newspace]
# 相对引用 一个.表示同级路径 ..表示更上一级
from . import subfile2 [as newspace]
相对引用不能用于顶层脚本 只能用于模块
如果使用相对引用 执行python package/subpackage/subfile.py报错
ValueError: Attempted relative import in non-package
可以使用python -m package.subpackage.subfile -m参数告诉python把它当做一个模块来加载 而不是顶层的脚本
如果被执行的不是模块而是一个包 则需要包内有命名为__main__.py的文件
【2018-04-03】【4】Sublime Text Project
// http://www.cnblogs.com/easy-blue/p/7840885.html
1. 创建工程 Project > Add Folder to Project
2. 保存工程 Project > Save Project As
菜单栏如果隐藏了点击Alt重新弹出
【2018-04-04】【1】Python异常
1.
try:
可能抛出异常代码
except: #抛出异常进入此分支
异常处理代码
else: #无抛出异常进入分支
非异常执行代码
2.
try:
可能抛出异常代码
finally:
无论是否异常都会进入分支
3.自定义异常
class MyException(Exception):
def __init__(self, param):
self.agrs = str(param)
例子:
1.
try:
if len(profit) != len(rate):
# 手动促发异常
raise Exception ("length wrong", "ERROR")
# arg 为异常参数
# 才此为 ("length wrong", "ERROR")
except Exception, arg:
print "Excetion catch", arg
else:
pass
2.
class MyException(Exception):
def __init__(self, param):
self.agrs = str(param)
try:
if len(profit) == len(rate):
raise MyException("ERROR")
# 捕获自定义异常 instance为异常实例
except Exception as instance:
print "Excetion catch", instance.args
3.
try:
if len(profit) != len(rate):
raise Exception('length wrong', "ERROR")
else:
print "no error"
finally:
print "finally call"
【2018-04-04】【2】Python序列化对象
# https://www.cnblogs.com/shixisheng/p/7089930.html
根据python官方文档的定义 一个序列对象不必要保存所有的元素
一般来说 一个序列对象至少需要实现如下两个方法:
1. __len__方法 该方法返回序列长度 也即序列中元素个数。
2. __getitem__方法 该方法有一个整型参数(index) 它需要返回序列中下标为index的元素的值
例子:
class MyRange(object):
def __init__(self, beg, end, step):
self._beg = beg
self._end = end
self._step = step
self._count = end - beg
# 序列化对象必须实现__len__方法
def __len__(self):
return self._count
# 序列化对象必须实现__getitem__方法
def __getitem__(self, index):
index = index if index >= 0 else self._count + index
return self._beg + index * self._step
myRange = MyRange(0, 10, 1)
# 输出 3
print myRange[3]
# 输出 10
print len(myRange)
range方法返回的是一个list对象 它需要开辟专门的空间保存序列中所有的元素
xrange方法返回的是xrange对象 它是一个序列对象 但并不保存序列中的元素
其实现方法与MyRange类型类似
在python3.x中 xrange函数已经不复存在了
【2018-04-04】【3】Python for if
Python里面有一种for if 组合的用法比较特别
例如:
1.判断字符串里有无空格
str = 'i am ok'
# 进入else分支输出 no space in string
# str = "hello"
for i in xrange(0, len(str)):
ch = str[i]
if ch.isspace():
break;
# 如果循环正常执行完毕将会进入else分支
else:
print "no space in string"
2.判断100以内的素数
for n in range(2, 101):
m = int(math.sqrt(n)) + 1
for k in range(2, m):
if n % k == 0:
break;
else:
print n
【2018-04-04】【4】Python 函数变量
1.Python函数内部的变量为局部变量 如果想要引用到全局变量需要使用global声明
例:
def func():
global callCount # = 0 编译失败 Python允许在声明全局变量同时赋值
callCount += 1
callCount = 0
func()
func()
func()
# 输出 3
print callCount
2.Python函数内部变量不具有记忆性 相当于C++的栈变量
例:
def func():
callCount = 0
callCount += 1
print callCount
# 三次都是输出1
func()
func()
func()
【2018-04-04】【5】Python 类变量与实例变量
Python里不对实例化对象的变量初始化将会使用类同名变量的值初始化
例:
class MyClass:
count = 10
def __init__(self):
# 不对实例化对象的count初始化
# self.count的值会用类同名变量的值初始化
# self.count = 0
pass
def call(self):
self.count += 1
print "self.count: ", self.count
myClass = MyClass()
# 输出 10
print "myClass.count", myClass.count
# 输出 11 如果在__init__对count赋值为0 输出为 1
myClass.call()
# 输出 12 如果在__init__对count赋值为0 输出为 2
myClass.call()
# 输出 13 如果在__init__对count赋值为0 输出为 3
myClass.call()
# 输出 10
print "MyClass.count", MyClass.count
【2018-04-04】【6】Python三元表达式
格式: valForTrue if 条件为真 else valForFalse
max = lambda a, b: a if a > b else b
min = lambda a, b: b if a > b else b
【2018-04-04】【7】Python函数传参
# https://www.cnblogs.com/loleina/p/5276918.html
Python不允许程序员选择采用传值还是传引用
如果函数收到的是一个可变对象(比如字典或者列表) 就能修改对象的原始值 相当于通过"传引用"来传递对象
如果函数收到的是一个不可变对象(比如数字 字符串或者元组) 就不能直接修改原始对象 相当于通过"传值"来传递对象。
例:
def func(num):
num *= 2
class NumContainer:
def __init__(self, num):
self.num = num
def funContainer(container):
container.num *= 2
num = 10
func(num)
# 输出10
print num
container = NumContainer(10)
# 输出20
funContainer(container)
print container.num
又如:
def func(str):
str = "newString"
str = "oldString"
# 输出oldString
func(str)
print str
【2018-04-08】【1】Python闭包
Python支持在函数内定义函数 这样就有语言上的闭包支持
# https://www.cnblogs.com/Sabre/p/8182107.html
闭包定义:
如果在一个内部函数里 对在外部作用域(但不是在全局作用域)的变量进行引用
那么内部函数就被认为是闭包(closure)
定义在外部函数内的但由内部函数引用或者使用的变量被称为自由变量
简言之就是子函数对父函数作用域内的变量进行引用就形成闭包
例:
def funcGenerator(func):
print "funcGenerator: ", func
def funcRes():
print "funcRes: ", func
return func()
return funcRes
【2018-04-08】【2】Python装饰器
# https://www.cnblogs.com/lianyingteng/p/7743876.html
# https://www.zhihu.com/question/26930016
装饰器函数的外部函数传入要装饰的函数名字 返回经过修饰后函数的名字
@a
@b
def c():
...
相当于
c = a(b(c()))
例:
def funcGenerator(func):
print "funcGenerator: ", func
def funcRes():
print "funcRes: ", func
return func()
return funcRes
不使用装饰器
# def myFunc():
# print "myFunc"
# myFunc = funcGenerator(myFunc)
使用装饰器
@funcGenerator
def myFunc():
print "myFunc"
【2018-04-08】【3】PyQt
PyQt是一个GUI控件工具箱 是Qt的Python接口
下载PyQt: https://sourceforge.net/projects/pyqt/files/PyQt4/
或者 pip install python-qt4
文档: http://pyqt.sourceforge.net/Docs/PyQt4/index.html
PyQt4 HelloWorld:
面向过程:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(250, 150)
widget.setWindowTitle('Icon')
widget.setWindowIcon(QtGui.QIcon('icon.png'))
widget.show()
sys.exit(app.exec_())
面向对象:
class Icon(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
self.setWindowIcon(QtGui.QIcon('icons/web.png'))
app = QtGui.QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())
【2018-04-08】【4】Qt信号槽
# https://www.cnblogs.com/QG-whz/p/4995938.html
# https://www.cnblogs.com/liushui-sky/p/5732931.html
信号与插槽机制是Qt编程的基础 它可以绑定对象而不需要对象之间彼此了解
不同点式槽可以链接到信号 通过这种方式可以在每次信号发射的的时候做到调用槽函数
connect(sender, SIGNAL(signal), receiver, SLOT(slot));
每次调用sender.singal()时候将会调用receiver.slot()
【2018-04-08】【5】Python类静态函数
Python可以在类函数上加上修饰符@staticmethod
使函数变为静态函数
例:
class MyClass:
@staticmethod
def MyStaticFunc():
print "MyStaticFunc"
# 通过类调用类静态函数
MyClass.Func()
# 通过对象调用类静态函数
myClass = MyClass()
myClass.Func()
【2018-04-09】【1】Pyqt自定义信号与槽
# http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html
自定义信号:
发射对象.emit(QtCore.SIGNAL(信号原型) [,信号参数])
自定义槽:
@pyqtSlot(参数类型表)
槽函数体
例:
# -*- coding: UTF-8 -*-
import sys
import inspect
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSlot
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
# self.connect(self, QtCore.SIGNAL("closeEmit(int, int)"),
# # 这里槽函数不加@pyqtSlot(int, int)也不会报错
# self.onMouseClicked)
QtCore.QObject.connect(self,
QtCore.SIGNAL("closeEmit(int, int)"),
self,
# 这里如果槽函数不加@pyqtSlot(int, int)将会报错
# Although PyQt4 allows any Python callable to be used as a slot when connecting signals,
# it is sometimes necessary to explicitly mark a Python method as being a Qt slot and to
# provide a C++ signature for it. PyQt4 provides the pyqtSlot() function decorator to do this.
QtCore.SLOT("onMouseClicked(int, int)"))
@pyqtSlot(int, int)
def onMouseClicked(self, x, y):
print "Clicked Pos: (%d %d)" % (x, y)
def mousePressEvent(self, event):
pos = event.globalPos()
# 发射信号和参数
self.emit(QtCore.SIGNAL("closeEmit(int, int)"), pos.x(), pos.y())
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
【2018-04-09】【2】Pyqt(Qt)常用函数
// http://doc.qt.io/archives/qt-4.8/index.html
1.QWidget::setWindowTitle(const QString &) 设置标题
2.QWidget::setWindowIcon(const QIcon & icon) 设置图标
3.QWidget::setGeometry(int x, int y, int w, int h) 设置位置与大小
4.bool QObject::connect(const QObject * sender, const char * signal,
const QObject * receiver, const char * method,
Qt::ConnectionType type = Qt::AutoConnection) 信号槽连接
5.QAction::setShortcut(const QKeySequence & shortcut) 设置快捷键
6.QAction::setStatusTip(const QString & statusTip) 设置状态Tip
7.QWidget::setFocusPolicy(Qt::FocusPolicy policy) 设置焦点策略
8.QWidget::move(int x, int y) 移动控件
9.void QWidget::setLayout(QLayout * layout) 设置布局
10.void QBoxLayout::addLayout(QLayout * layout, int stretch = 0) 和
void QGridLayout::addLayout(QLayout * layout, int row, int column, Qt::Alignment alignment = 0) 添加布局
11.void QLayout::addWidget(QWidget * w) 在布局里添加控件
12.QMenuBar * QMainWindow::menuBar() const 获取菜单栏
13.QMenu * QMenuBar::addMenu(const QString & title) 往菜单栏添加菜单项
14.void QMenu::addAction(QAction * action) 往菜单项添加 动作事件(Action)即子项
15.QToolBar * QMainWindow::addToolBar(const QString & title) 添加工具栏项目
【2018-04-09】【3】Pyqt(Qt)常用函数
1.void QToolBar::addAction(QAction * action) 往工具栏项目添加 动作事件(Action)即子项
2.QWidget::resize(int w, int h) 重新设置控件大小
3.QWidget::setAcceptDrops(bool on) 是否接受拖放
4.QLineEdit::setDragEnabled(bool b) 是否启用拖放
5.void QWidget::dragEnterEvent(QDragEnterEvent * event) 拖动正在进行鼠标进入控件时调用 用于促发拖放事件
6.void QWidget::dropEvent(QDropEvent * event) 处理拖放事件
7.void QDrag::setHotSpot(const QPoint & hotspot) 设置拖放位置
8.void QDrag::setMimeData(QMimeData * data) 设置拖动Mime数据
9.Qt::DropAction QDrag::exec(Qt::DropActions supportedActions = Qt::MoveAction) 启动拖放
【2018-04-09】【4】Python super关键字
# http://www.runoob.com/python/python-func-super.html
对于继承自object的基类 可以使用super关键字查找
例:
# 必须继承object
class Base(object):
def __init__(self):
print "BaseInit"
def Func(self):
print "Base"
class Derived(Base):
def __init__(self):
# Base.__init__(self)
super(Derived, self).__init__()
print "DerivedInit"
def Func(self):
# Base.Func(self)
super(Derived, self).Func()
print "Derived"
# 输出 BaseInit DerivedInit
test = Derived()
# 输出 Base Derived
test.Func()
【2018-04-10】【1】QtDesigner
1.使用QtDesigner生成.ui文件
2.执行命令 pyuic4 -o 目标文件 源文件(如 pyuic4 -o myUI.py myUI.ui)
3.使用生成的目标文件
例:
myUI.py:
......
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(479, 331)
self.centralwidget = QtGui.QWidget(MainWindow)
......
self.textEdit = QtGui.QTextEdit(self.centralwidget)
......
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
......
MainWindow.setStatusBar(self.statusbar)
self.menuBar = QtGui.QMenuBar(MainWindow)
......
MainWindow.setMenuBar(self.menuBar)
self.actionOpen = QtGui.QAction(MainWindow)
......
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.actionOpen, QtCore.SIGNAL(_fromUtf8("triggered()")), MainWindow.openDialog)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.menuFile.setTitle(_translate("MainWindow", "File", None))
self.actionOpen.setText(_translate("MainWindow", "Open", None))
self.actionOpen.setShortcut(_translate("MainWindow", "Ctrl+O", None))
main.py:
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSlot
from myUI import *
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.main = Ui_MainWindow()
self.main.setupUi(self)
self.show()
def openDialog(self):
filename = QtGui.QFileDialog.getOpenFileName(self, "open file",
"/home")
if filename:
file = open(filename)
data = file.read()
file.close()
self.main.textEdit.setText(data)
app = QtGui.QApplication(sys.argv)
myWindow = MyWindow()
sys.exit(app.exec_())
【2018-04-10】【2】Python常见文档格式
# http://forum.digitser.cn/thread-1758-1-1.html
# https://www.zhihu.com/question/30296617
# https://www.cnblogs.com/zhangqunshi/p/6657208.html
1. .py 文件
以 .py 作扩展名的文件是 Python 源代码文件 由 python.exe 解释,可在控制台下运行
2. .pyc 文件
以 .pyc 作扩展名的文件是 python 编译文件 .pyc 文件是不能直接用文本编辑器进行编辑 其优点是 .pyc 文件的执行速度要远快于 .py 文件
对于py文件 执行命令来生成pyc文件 python -m foo.py
3. .pyw 文件
.pyw 文件与 .pyc 文件的执行 本质上并没什么区别 只是 .pyw 文件执行的时候不会出现类似 CMD 命令的黑色 shell 窗口
.pyw 文件格式主要是设计用来运行 Python 纯 GUI (图形用户界面) 程序的
4. .pyo 文件
.pyo 文件是相对 .pyc 而言的 优化编译后的 Python 文件
5. .pyd 文件
.pyd 文件是非 Python,由其它编程语言 "编写-编译" 生成的 Python 扩展模块
【2018-04-11】【1】安装Python setuptools
1.下载 setuptools https://pypi.python.org/pypi/setuptools#downloads
2.若有 pip 用 pip list 检查是否已存在旧版本的 setuptools 若有 用命令 pip uninstall setuptools 卸载掉
3.解压下载好的setuptools 用命令 python setup.py install 安装
4.设置好setuptools.exe所在路径到环境变量Path里
5.用命令 easy_install 验证是否安装成功 若输出类似 error: No urls, filenames, or requirements specified (see --help) 则成功
【2018-04-11】【2】PyCharm Python远程调试
# https://www.cnblogs.com/jinjiangongzuoshi/p/5638706.html
1.在PyCharm安装路径里找到pycharm-debug.egg文件(若远程计算机运行的是Python3则需要pycharm-debug-py3k.egg)
2.设法把pycharm-debug.egg添加到远程计算机引用路径中
可用以下方式
2.1 用 easy_install pycharm-debug.egg 进行安装
2.2 将pycharm-debug.egg添加至PYTHONPATH或sys.path
2.3 用pip install pydevd
直到import pydevd成功
3.在本地开发环境的PyCharm中进行监听配置
3.1【Run】->【Edit Configurations】
3.2【Add New Configuration】->【Python Remote Debug】
填写 Local host name 和 Port 其中Local host name指的是本机开发环境的IP地址 而Port则随便填写一个10000以上的即可
需要注意的是 由于远程计算机需要连接至本地开发环境 因此本地IP地址应该保证远程可以访问得到
3.3【Apply】and【OK】
4.在远程计算机的应用程序中插入代码
例如:
pydevd.settrace('localhost', port=10088, stdoutToServer=True, stderrToServer=True)
其中 IP地址和端口号要与PyCharm中的监听配置保持一致
5.在PyCharm中启动Debug Server
【Run】->【Debug…】,选择刚创建的远程调试配置项,在Debug Console中会显示如下信息:
Starting debug server at port 10088
Use the following code to connect to the debugger:
import pydevd
pydevd.settrace('localhost', port=10088, stdoutToServer=True, stderrToServer=True)
Waiting for process connection...
Connected to pydev debugger (build 181.4445.76)
Waiting for process connection...
6.在远程计算机中启动应用程序 当执行到pydevd.settrace语句时
便会与本地开发环境中的PyCharm建立通讯连接 接下来便可以在本地IDE中进行单步调试了