分类 python 下的文章

[笔记]自己笔记本上最快的python补全方案设置

个人笔记,如有转载,请注明出处。

——by realasking

2020-03-26 增加关于(jedi:setup)的说明

2020-03-21 最初版本

最近一两年,因为使用python比较多,而其它ide总有让人不满意的地方,以及一些其它原因,就将主要使用的编辑器从原先使用的Linux下的vimWindows下的notepad++统一替换为了Emacs。经过较长时间的磨合和多次重构配置文件,Emacs环境已经基本符合自己的习惯。然而,python补全始终是个比较大的问题:在Linux下使用elpycompany虽然有可以感知的延迟,但是还能忍受,在Windows 10下延迟就很大了,特别是补全np.array之类的内容的时候,能等待数秒,甚至十几秒。

这一两年来,翻遍了网页也没有找到解决办法,于是就开始尝试其它方案,最近两天终于折腾出一个反应明显更快的配置。我使用了Emacs-jediauto-completeformat-all,以及Emacs自带的python-mode,在LinuxWindows下虽然仍然有延迟,但比起elpylsp来说,补全还是快了不少,经过简化的配置如下:

(defvar myPackages
  '(format-all
    jedi
   ))
(mapc #'(lambda (package)
      (unless (package-installed-p package)
        (package-install package)))
      myPackages)
(setq python-shell-interpreter "ipython"
      python-shell-interpreter-args "--simple-prompt -i")
(global-set-key [f6] 'run-python)
(defun python-mode-modify()
  (hs-minor-mode t)
  )
(add-hook 'python-mode-hook (lambda ()
                  (jedi:ac-setup)
                  (setq ac-use-quick-help nil)
                  (setq jedi:tooltip-method nil)
                  (setq jedi:complete-on-dot t)
                  (auto-complete-mode 1)))
(add-hook 'python-mode-hook 'python-mode-modify)

补充:有人问我为什么不写(jedi:setup),这个在jedi的官网上是有说明的,如果只需要补全,用(jedi:ac-setup)就够了,如果还需要签名文档提示、函数参数表之类的,就用(jedi:setup)。用(jedi:ac-setup)的时候的确不需要设置(setq jedi:tooltip-method nil)这一句,这一句的意思是在minibuffer中显示签名文档提示,简化配置贴上来的时候我忘了删了。

【笔记】在联想小新潮7000-15ARR+Manjaro Linux使用pyopencl和amd开源显卡驱动的安装配置

个人笔记,如有转载,请注明出处。
——by realasking


众所周知,amdgpu开源驱动的opengl硬件加速性能比amdgpu-pro闭源显卡更好,但其通过mesa实现的opencl只能支持到1.1版,而且并不总能正常的工作。如果能在开源显卡驱动的基础上安装闭源驱动中的opencl支持,显然是非常好的事情。好在AUR中就提供有这样的方案。

安装

yaourt -S opencl-amd ocl-icd opencl-headers clinfo pyopencl-headers python-pyopencl

检查


[[email protected] ~]$ clinfo 
Number of platforms                               2
  Platform Name                                   AMD Accelerated Parallel Processing
  Platform Vendor                                 Advanced Micro Devices, Inc.
  Platform Version                                OpenCL 2.1 AMD-APP (2906.7)
  Platform Profile                                FULL_PROFILE
  Platform Extensions                             cl_khr_icd cl_amd_event_callback cl_amd_offline_devices 
  Platform Host timer resolution                  1ns
  Platform Extensions function suffix             AMD

Platform Name                                   AMD Accelerated Parallel Processing
  Platform Vendor                                 Advanced Micro Devices, Inc.
  Platform Version                                OpenCL 2.1 AMD-APP (2906.7)
  Platform Profile                                FULL_PROFILE
  Platform Extensions                             cl_khr_icd cl_amd_event_callback cl_amd_offline_devices 
  Platform Host timer resolution                  1ns
  Platform Extensions function suffix             AMD

Platform Name                                   AMD Accelerated Parallel Processing
Number of devices                                 1
  Device Name                                     gfx804
  Device Vendor                                   Advanced Micro Devices, Inc.
  Device Vendor ID                                0x1002
  Device Version                                  OpenCL 1.2 AMD-APP (2906.7)
  Driver Version                                  2906.7
  Device OpenCL C Version                         OpenCL C 1.2 
  Device Type                                     GPU
  Device Board Name (AMD)                         Radeon 500 Series
  Device Topology (AMD)                           PCI-E, 01:00.0
  Device Profile                                  FULL_PROFILE
  Device Available                                Yes
  Compiler Available                              Yes
  Linker Available                                Yes
  Max compute units                               8
  SIMD per compute unit (AMD)                     4
  SIMD width (AMD)                                16
  SIMD instruction width (AMD)                    1
  Max clock frequency                             1124MHz
  Graphics IP (AMD)                               8.0
  Device Partition                                (core)
    Max number of sub-devices                     8
    Supported partition types                     None
    Supported affinity domains                    (n/a)
  Max work item dimensions                        3
  Max work item sizes                             1024x1024x1024
  Max work group size                             256
  Preferred work group size (AMD)                 256
  Max work group size (AMD)                       1024
  Preferred work group size multiple              64
  Wavefront width (AMD)                           64
  Preferred / native vector sizes
char 4 / 4
short 2 / 2
int 1 / 1
long 1 / 1
half 1 / 1 (cl_khr_fp16) float 1 / 1
double 1 / 1 (cl_khr_fp64) Half-precision Floating-point support (cl_khr_fp16) Denormals No Infinity and NANs No Round to nearest No Round to zero No Round to infinity No IEEE754-2008 fused multiply-add No Support is emulated in software No Single-precision Floating-point support (core) Denormals No Infinity and NANs Yes Round to nearest Yes Round to zero Yes Round to infinity Yes IEEE754-2008 fused multiply-add Yes Support is emulated in software No Correctly-rounded divide and sqrt operations Yes Double-precision Floating-point support (cl_khr_fp64) Denormals Yes Infinity and NANs Yes Round to nearest Yes Round to zero Yes Round to infinity Yes IEEE754-2008 fused multiply-add Yes Support is emulated in software No Address bits 64, Little-Endian Global memory size 2141634560 (1.995GiB) Global free memory (AMD) 2071608 (1.976GiB) Global memory channels (AMD) 2 Global memory banks per channel (AMD) 16 Global memory bank width (AMD) 256 bytes Error Correction support No Max memory allocation 1596849766 (1.487GiB) Unified memory for Host and Device No Minimum alignment for any data type 128 bytes Alignment of base address 2048 bits (256 bytes) Global Memory cache type Read/Write Global Memory cache size 16384 (16KiB) Global Memory cache line size 64 bytes Image support Yes Max number of samplers per kernel 16 Max size for 1D images from buffer 134217728 pixels Max 1D or 2D image array size 2048 images Base address alignment for 2D image buffers 256 bytes Pitch alignment for 2D image buffers 256 pixels Max 2D image size 16384x16384 pixels Max 3D image size 2048x2048x2048 pixels Max number of read image args 128 Max number of write image args 8 Local memory type Local Local memory size 32768 (32KiB) Local memory syze per CU (AMD) 65536 (64KiB) Local memory banks (AMD) 32 Max number of constant args 8 Max constant buffer size 1596849766 (1.487GiB) Preferred constant buffer size (AMD) 16384 (16KiB) Max size of kernel argument 1024 Queue properties
Out-of-order execution No Profiling Yes Prefer user sync for interop Yes Profiling timer resolution 1ns Profiling timer offset since Epoch (AMD) 1580776646713521172ns (Tue Feb 4 08:37:26 2020) Execution capabilities
Run OpenCL kernels Yes Run native kernels No Thread trace supported (AMD) Yes Number of async queues (AMD) 2 Max real-time compute queues (AMD) 0 Max real-time compute units (AMD) 0 SPIR versions 1.2 printf() buffer size 4194304 (4MiB) Built-in kernels (n/a) Device Extensions cl_khr_fp64 cl_amd_fp64 cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_fp16 cl_khr_gl_sharing cl_amd_device_attribute_query cl_amd_vec3 cl_amd_printf cl_amd_media_ops cl_amd_media_ops2 cl_amd_popcnt cl_khr_image2d_from_buffer cl_amd_bus_addressable_memory cl_khr_spir cl_khr_gl_event Platform Name AMD Accelerated Parallel Processing Number of devices 1 Device Name gfx902 Device Vendor Advanced Micro Devices, Inc. Device Vendor ID 0x1002 Device Version OpenCL 2.0 AMD-APP (2906.7) Driver Version 2906.7 (PAL,HSAIL) Device OpenCL C Version OpenCL C 2.0 Device Type GPU Device Board Name (AMD) Unknown AMD GPU Device Topology (AMD) PCI-E, 05:00.0 Device Profile FULL_PROFILE Device Available Yes Compiler Available Yes Linker Available Yes Max compute units 8 SIMD per compute unit (AMD) 4 SIMD width (AMD) 16 SIMD instruction width (AMD) 1 Max clock frequency 1100MHz Graphics IP (AMD) 9.2 Device Partition (core) Max number of sub-devices 8 Supported partition types None Supported affinity domains (n/a) Max work item dimensions 3 Max work item sizes 1024x1024x1024 Max work group size 256 Preferred work group size (AMD) 256 Max work group size (AMD) 1024 Preferred work group size multiple 64 Wavefront width (AMD) 64 Preferred / native vector sizes
char 4 / 4
short 2 / 2
int 1 / 1
long 1 / 1
half 1 / 1 (cl_khr_fp16) float 1 / 1
double 1 / 1 (cl_khr_fp64) Half-precision Floating-point support (cl_khr_fp16) Denormals No Infinity and NANs No Round to nearest No Round to zero No Round to infinity No IEEE754-2008 fused multiply-add No Support is emulated in software No Single-precision Floating-point support (core) Denormals No Infinity and NANs Yes Round to nearest Yes Round to zero Yes Round to infinity Yes IEEE754-2008 fused multiply-add Yes Support is emulated in software No Correctly-rounded divide and sqrt operations Yes Double-precision Floating-point support (cl_khr_fp64) Denormals Yes Infinity and NANs Yes Round to nearest Yes Round to zero Yes Round to infinity Yes IEEE754-2008 fused multiply-add Yes Support is emulated in software No Address bits 64, Little-Endian Global memory size 2684354560 (2.5GiB) Global free memory (AMD) 2553760 (2.435GiB) Global memory channels (AMD) 4 Global memory banks per channel (AMD) 4 Global memory bank width (AMD) 256 bytes Error Correction support No Max memory allocation 912680550 (870.4MiB) Unified memory for Host and Device Yes Shared Virtual Memory (SVM) capabilities (core) Coarse-grained buffer sharing Yes Fine-grained buffer sharing Yes Fine-grained system sharing No Atomics No Minimum alignment for any data type 128 bytes Alignment of base address 2048 bits (256 bytes) Preferred alignment for atomics
SVM 0 bytes Global 0 bytes Local 0 bytes Max size for global variable 821412352 (783.4MiB) Preferred total size of global vars 2684354560 (2.5GiB) Global Memory cache type Read/Write Global Memory cache size 16384 (16KiB) Global Memory cache line size 64 bytes Image support Yes Max number of samplers per kernel 16 Max size for 1D images from buffer 134217728 pixels Max 1D or 2D image array size 2048 images Base address alignment for 2D image buffers 256 bytes Pitch alignment for 2D image buffers 256 pixels Max 2D image size 16384x16384 pixels Max 3D image size 2048x2048x2048 pixels Max number of read image args 128 Max number of write image args 64 Max number of read/write image args 64 Max number of pipe args 16 Max active pipe reservations 16 Max pipe packet size 912680550 (870.4MiB) Local memory type Local Local memory size 65536 (64KiB) Local memory syze per CU (AMD) 65536 (64KiB) Local memory banks (AMD) 32 Max number of constant args 8 Max constant buffer size 912680550 (870.4MiB) Preferred constant buffer size (AMD) 16384 (16KiB) Max size of kernel argument 1024 Queue properties (on host)
Out-of-order execution No Profiling Yes Queue properties (on device)
Out-of-order execution Yes Profiling Yes Preferred size 262144 (256KiB) Max size 8388608 (8MiB) Max queues on device 1 Max events on device 1024 Prefer user sync for interop Yes Number of P2P devices (AMD) 0 P2P devices (AMD) (n/a) Profiling timer resolution 1ns Profiling timer offset since Epoch (AMD) 1580776646713519545ns (Tue Feb 4 08:37:26 2020) Execution capabilities
Run OpenCL kernels Yes Run native kernels No Thread trace supported (AMD) Yes Number of async queues (AMD) 4 Max real-time compute queues (AMD) 0 Max real-time compute units (AMD) 0 SPIR versions 1.2 printf() buffer size 4194304 (4MiB) Built-in kernels (n/a) Device Extensions cl_khr_fp64 cl_amd_fp64 cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_3d_image_writes cl_khr_byte_addressable_store cl_khr_fp16 cl_khr_gl_sharing cl_khr_gl_depth_images cl_amd_device_attribute_query cl_amd_vec3 cl_amd_printf cl_amd_media_ops cl_amd_media_ops2 cl_amd_popcnt cl_khr_image2d_from_buffer cl_khr_spir cl_khr_subgroups cl_khr_gl_event cl_khr_depth_images cl_khr_mipmap_image cl_khr_mipmap_image_writes cl_amd_copy_buffer_p2p NULL platform behavior clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...) AMD Accelerated Parallel Processing clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...) Success [AMD] clCreateContext(NULL, ...) [default] Success [AMD] clCreateContext(NULL, ...) [other] Success [AMD] clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT) Success (1) Platform Name AMD Accelerated Parallel Processing Device Name gfx804 clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU) No devices found in platform clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU) Success (1) Platform Name AMD Accelerated Parallel Processing Device Name gfx804 clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR) No devices found in platform clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM) No devices found in platform clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL) Success (1) Platform Name AMD Accelerated Parallel Processing Device Name gfx804 ICD loader properties ICD loader Name OpenCL ICD Loader ICD loader Vendor OCL Icd free software ICD loader Version 2.2.12 ICD loader Profile OpenCL 2.2

切换显卡

使用环境变量PYOPENCL_CTX='0''1'

【笔记】对python的最佳优化:换用C++

之前写了个处理计算数据的脚本,图方便,直接用python做的,结果哪想到后来要反复使用这个脚本,就觉得慢了,于是进行了一系列优化。
最初这个脚本运行时间是270秒左右;
使用numpy替换部分列表之后,运行时间缩短到220~240秒;
numpy对部分矩阵赋值改成整行赋值,运行时间缩短到202秒;
将部分内容函数化,然后把外层循环用迭代器替换,运行时间缩短到184秒;
嗯,成效显著。突发奇想,要是换成c++来实现一下怎么样?
于是花了两个小时coding,运行之后......
耗时5秒。
嗯,看到这个结果......
今后需要多次使用的脚本,不太复杂的还是用c++吧......

win10上python环境的重装

个人笔记,如有转载,请注明出处。

——by realasking

之前,在windows上配置和使用python环境一直采用的anaconda,没有觉得什么 不方便,而且由于有清华和中科大的源,更新和安装软件包也没有遇到什么麻烦。但是最近,不知是否是受美国对中国科技方面限制的影响,国内很多源都停止了服务,现在更新和安装软件包变得非常缓慢。不得已,只能将其全部删除,然后重装更干净的python。

安装python 3.7

以管理员启动PowerShell, 然后执行:


choco install python

安装完成后环境变量即已设置,不用自己更改

安装优化后的软件包

先安装wheel


pip install wheel

访问非官方python二进制扩展包,下载需要的优化编译的包,该网址的软件包有多个不同版本,很好用。

下载之后,进入相应目录,用pip install 软件包名进行安装即可。

安装其它软件包

安装IDE

Thonny是一个轻量级的python IDE,简洁好用,下载地址:thonny

安装后需要在Tools->Options->Interpreter中将默认python改为我们安装的,配色方案在themes & fonts中改。窗口显示在View中选。

Python学习笔记

以前所写python学习笔记,python初学时期的,两篇合而为一,原始出处:python学习笔记1python学习笔记2

备忘,如有转载,请注明出处。

——by realasking

Linux下Python程序名应取作:xxx.py,然后程序第一行写成:

#!/usr/bin/env python

未加语言相关的环境变量设置时,不应在python程序中加入非ASCII的字符,程序文件中以#引起的行为注释行。

Python程序写好后可按如下方法运行:

$python ./xxx.py 参数1 参数2 ...

其中python这一命令由系统环境变量$PYTHON的值或alias指定的python来决定,而参数的引入,是通过sys.argv来实现的,
应这样来使用参数:

import os
import sys
input_Args=sys.argv
Arg1=input_Args[1]
Arg2=input_Args[2]

由于参数是字符串,所以如果要传递数值型参数,需要用相应的函数转换,比如:

Arg1=float(Arg1)

python最方便的一点是它是胶水语言,较头痛的则是它的数据类型、文件读写等和其它语言有较大差别,第一个容易忘记的
地方是,它没有double型,它的float型即double精度,类型转换也应用float(A)这样的形式。另外,python的数据类型一般
不需要专门声明,不过可以定义空数组,在数值计算中,这个做法还是有些用的:

tmp=[]

python的循环和条件判断,采用缩进来区分,没有其它定界符,相同缩进表示相同层级的语句,至于循环和条件语句的开
始行的行末,则需要添加:作为标志,比如条件判断:

a=11
b=10
if b > a :
   print "b>a"
   elif b < a :
      print "b

在python中,感觉使用while循环比for循环更方便些,比如:

AA=11
a=0
while a < AA :
      执行语句
      a=a+1

用python读取格式化文本不是太方便,不过还是有办法的,比如读取一个数据矩阵中的一行数据:

f=open(input_file_name,"r")
......
data=[]
line=f.readline()
line=line.strip()
data.append(map(float, line.split(' ')))
.....
f.close()

如果是要读取整个矩阵,在data=[]和最后的append之间加上循环就是了。

矩阵或向量运算,应该采用numpyscipy,做法是:

import math
import numpy as np
import scipy as sp

然后就可以进行运算了,比如加法、除以一个数:

npdata=np.array(data)
data_sum=np.add(npdata,npdata)
data_frac=np.true_divide(npdata,2)

也可以按列合并矩阵(要求两矩阵行数一样):

new_matrix=np.hstack([mat1,mat2])

或按行合并矩阵(要求两矩阵列数一样):

new_matrix=np.vstack([mat1,mat2])

合并矩阵的命令同样可以用于合并向量,但是合并向量的时候有时会提示行列数不对,那可能是因为一个的维度是
(n个),而另一个的维度是(n列,1行),这种情况下,可用reshape来进行转换:

array2=array2.reshape(n)
new_array=np.hstack([array1,array2])

查看矩阵或向量维度的办法是:

xxx.shape

对于一个矩阵,若按其第一列元素大小顺序来对整个矩阵进行行排序,则执行:

mat1=mat1[mat1[:,0].argsort()]

在屏幕上输出数组:

print mat1

保存一个矩阵到文件:

np.savetxt(输出文件名,矩阵名)

Linux下python使用和学习笔记,面向对象部分,包括函数、多个返回值、传递对象、模块、面向对象等内容,

共分7个文件,其中test.py是主文件,mod.py是一个简单的函数文件,modclass.py是一个类,也是modinheritance.py的基类,

而以xxinheritancexx为名的都是派生和继承的练习。

test.py的内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#python函数、类、模块学习脚本
#作者:realasking
#主页:http://realasking.github.io
#2013-10-09

import mod as md
import numpy as np
import modclass as mc
import modinheritance as mi
import modinheritance2 as mi2
import modmultiinheritance as mmi

def firstfunc() :
    print '第一个函数'

firstfunc()

a=0.0
b=''
c=''

def testnumber(a,c) :
    global b
    a=a+1.0
    b='你好'
    c=b
    return [a,b,c]

#python可用列表或元组返回多个参数,放到不同变量中
[a,b,c]=testnumber(a,b)

print a,b
c=''
[a,b,c]=testnumber(a,c)
print a,b,c


#测试不同数据类型的传递
d=np.array([[1,2,3],[4,5,6],[7,8,9]])

def nptest(x) :
    y=np.sin(x)
    return [x,y]

[xx,y]=nptest(d)
print d
print xx
print y

cc=xx*y
print cc

#测试module文件中的函数
[xxx,yy]=md.nptest(d)
print xxx 
print yy

#类的测试
zs=mc.modclass()
zs.nptest(d)
[xxxx,yyy]=zs.getxy()
print xxxx
print yyy 

#测试列表 
xxtmp=[]
i=0
while i < 10:
   zss=mc.modclass()
   zss.nptest(xxxx)
   [xxxx,yyy]=zss.getxy()
   xxtmp.append(zss)
   print xxtmp[i].x
   i+=1

#派生类测试
mis=mi.modinheritance()
mis.nptest(xxxx)
[xxxxx,yyyy,cc]=mis.getxy()

#派生类测试2
mis2=mi2.modinheritance2()
mis2.nptest(xxxx)
[xxxxx,yyyy,cc,dd]=mis2.getxy()
print xxxxx,yyyy,cc,dd

#多重继承测试
mis3=mmi.modmultiinheritance()
mis3.nptest(xxxx)
cccccc=mis3.getxy()
print cccccc

mod.py文件内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#函数的使用
#作者:realasking
#主页:http://realasking.github.io
#2013-10-09

import numpy as np

def nptest(x) :
       y=np.sin(x)
       return [x,y]

modclass.py文件内容,也是类的构建:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#类的使用
#作者:realasking
#主页:http://realasking.github.io
#2013-10-09

import numpy as np

class modclass:
    #初始化,定义内部变量
    def __init__(self):
        self.a=0
        self.x=np.array([])
        self.y=np.array([])
    #从外部传值进来进行计算
    def nptest(self,x):
       self.x=x
       self.y=np.sin(self.x)
    #返回值
    def getxy(self):
       return [self.x*self.y,self.y]

modinheritance.py文件内容,也是类的继承:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#类的继承和派生
#作者:realasking
#主页:http://realasking.github.io
#2013-10-09

import numpy as np
import modclass as mc

class modinheritance(mc.modclass):
    #初始化,定义内部变量
    def __init__(self):
        mc.modclass.__init__(self)
        self.b=0
        self.aa=np.array([])
        self.bb=np.array([])
    #从外部传值进来进行计算
    def nptest(self,x):
       #调用父类同名函数,内部变量仍然共享
       mc.modclass.nptest(self,x)
       #self.a是父类中提供的内部变量,继承而来
       self.b=self.a+2
       #调用getxy也是父类中的
       [self.aa,self.bb]=mc.modclass.getxy(self)
    #返回值
    def getxy(self):
       return [self.aa,self.bb,self.b]

modinheritance2.py文件的内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#类的继承和派生2
#作者:realasking
#主页:http://realasking.github.io
#2013-10-09

import numpy as np
import modclass as mc
import modinheritance as mi

class modinheritance2(mi.modinheritance):
    #初始化,定义内部变量
    def __init__(self):
        mi.modinheritance.__init__(self)
        self.c=0
        self.cc=np.array([])
    #从外部传值进来进行计算
    def nptest(self,x):
       #调用父类同名函数,内部变量仍然共享
       mi.modinheritance.nptest(self,x)
       #self.a是父类中提供的内部变量,继承而来
       self.c=self.b*2
    #返回值
    def getxy(self):
       return [self.aa,self.bb,self.b,self.c]

modinheritance3.py文件的内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#类的继承和派生3
#作者:realasking
#主页:http://realasking.github.io
#2013-10-09

import numpy as np
import modclass as mc
import modinheritance as mi

class modinheritance3(mi.modinheritance):
    #初始化,定义内部变量
    def __init__(self):
        mi.modinheritance.__init__(self)
        self.c=0
        self.cc=np.array([])
    #从外部传值进来进行计算
    def nptest(self,x):
       #调用父类同名函数,内部变量仍然共享
       mi.modinheritance.nptest(self,x)
       #self.a是父类中提供的内部变量,继承而来
       self.c=self.b*2-1
    #返回值
    def getxy(self):
       return [self.aa,self.bb,self.b,self.c]

modmultiinheritance.py文件的内容:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#类的多继承和派生
#作者:realasking
#主页:http://realasking.github.io
#2013-10-09

import numpy as np
import modclass as mc
import modinheritance2 as mi2
import modinheritance3 as mi3

class modmultiinheritance(mi2.modinheritance2,mi3.modinheritance3):
    #初始化,定义内部变量
    def __init__(self):
        mi2.modinheritance2.__init__(self)
        mi3.modinheritance3.__init__(self)
        self.cc=np.array([])
    #从外部传值进来进行计算
    def nptest(self,x):
       #调用父类同名函数,内部变量仍然共享
       mi2.modinheritance2.nptest(self,x)
       mi3.modinheritance3.nptest(self,x)
       [a,b,c,d]=mi2.modinheritance2.getxy(self)
       [e,f,g,h]=mi3.modinheritance3.getxy(self)
       self.cc=h*b+f/g
    #返回值
    def getxy(self):
       return [self.cc]

执行结果:

[[email protected] mod_test]$python ./test.py 
第一个函数
1.0 你好
2.0 你好 你好
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[ 0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155 ]
 [ 0.6569866   0.98935825  0.41211849]]
[[ 0.84147098  1.81859485  0.42336002]
 [-3.02720998 -4.79462137 -1.67649299]
 [ 4.59890619  7.91486597  3.70906637]]
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[ 0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155 ]
 [ 0.6569866   0.98935825  0.41211849]]
[[ 0.84147098  1.81859485  0.42336002]
 [-3.02720998 -4.79462137 -1.67649299]
 [ 4.59890619  7.91486597  3.70906637]]
[[ 0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155 ]
 [ 0.6569866   0.98935825  0.41211849]]
[[ 0.84147098  1.81859485  0.42336002]
 [-3.02720998 -4.79462137 -1.67649299]
 [ 4.59890619  7.91486597  3.70906637]]
[[ 0.62742108  1.76304538  0.17392737]
 [ 0.34550582 -4.77841949  1.667137  ]
 [-4.56932481  7.90020068 -1.99363595]]
[[ 0.36833317  1.73056479  0.03009844]
 [ 0.11701337 -4.76800625  1.65940619]
 [-4.52264351  7.89176398  1.8180512 ]]
[[  1.32622372e-01   1.70852457e+00   9.05779427e-04]
 [  1.36609043e-02  -4.76063376e+00   1.65289587e+00]
 [ -4.44147235e+00   7.88613188e+00   1.76276049e+00]]
[[  1.75371785e-02   1.69234561e+00   8.20436258e-07]
 [  1.86614503e-04  -4.75509451e+00   1.64732846e+00]
 [ -4.27947407e+00   7.88205653e+00   1.73038103e+00]]
[[  3.07536865e-04   1.67985944e+00   6.73115654e-13]
 [  3.48249724e-08  -4.75075909e+00   1.64250647e+00]
 [ -3.88467867e+00   7.87895041e+00   1.70839371e+00]]
[[  9.45789219e-08   1.66987858e+00   4.53084684e-25]
 [  1.21277870e-15  -4.74726233e+00   1.63828512e+00]
 [ -2.62823228e+00   7.87649452e+00   1.69224667e+00]]
[[  8.94517247e-15   1.66168844e+00   2.05285731e-49]
 [  1.47083218e-30  -4.74437593e+00   1.63455555e+00]
 [  1.29074388e+00   7.87449858e+00   1.67978152e+00]]
[[  8.00161105e-29   1.65482924e+00   4.21422313e-98]
 [  2.16334731e-60  -4.74194900e+00   1.63123425e+00]
 [  1.24045771e+00   7.87284127e+00   1.66981536e+00]]
[[  6.40257794e-057   1.64898987e+000   1.77596766e-195]
 [  4.68007157e-120  -4.73987740e+000   1.62825592e+000]
 [  1.17338934e+000   7.87144118e+000   1.66163597e+000]]
[[  1.68042640e-225   1.63955432e+000   0.00000000e+000]
 [  0.00000000e+000  -4.73652238e+000   1.62313097e+000]
 [  9.55219008e-001   7.86920111e+000   1.64895184e+000]] [[  4.09930043e-113   9.97325369e-001   0.00000000e+000]
 [  2.19030699e-239   9.99669830e-001   9.98500367e-001]
 [  8.82872574e-001   9.99867812e-001   9.96475032e-001]] 2
[[  1.68042640e-225   1.63955432e+000   0.00000000e+000]
 [  0.00000000e+000  -4.73652238e+000   1.62313097e+000]
 [  9.55219008e-001   7.86920111e+000   1.64895184e+000]] [[  4.09930043e-113   9.97325369e-001   0.00000000e+000]
 [  2.19030699e-239   9.99669830e-001   9.98500367e-001]
 [  8.82872574e-001   9.99867812e-001   9.96475032e-001]] 2 4
[array([[  1.43475515e-112,   3.49063879e+000,   0.00000000e+000],
       [  7.66607448e-239,   3.49884441e+000,   3.49475128e+000],
       [  3.09005401e+000,   3.49953734e+000,   3.48766261e+000]])]