Python Best Practices: Abstract Syntax Trees

Modifying Abstract Syntax Trees I would like to be able to build and modify an ast and then optionally write it out as python byte code for execution later without overhead. I have been hacking around with the ast docs for python3.0 and python2.6 , but I can't seem to find any good sources on best practices for this type of code. Question What are some best practices and guidelines fo

Python最佳实践:抽象语法树

修改抽象语法树 我希望能够构建和修改一个ast ,然后可以将其作为python字节代码写出来,以便稍后执行而不会产生开销。 我一直在python3.0和python2.6的ast文档,但我似乎无法找到有关此类代码的最佳实践的任何良好资源。 题 在Python中修改抽象语法树的最佳实践和指导是什么? [编辑] 未知状态,字节播放是这样一个库的一个很好的例子。 此外,Benford引用了GeniuSQL,它使用抽象语法树将python代码转换为SQL。

Curses Difference between newwin and subwin

i don't seem to be able to find any informations about the difference between curses.newwin and curses.subwin do you know any? i'd like to have a screen divided in 3 different sections with different updates times (not everything must be updated every keypress). is this the correct direction to go? thank you Docu not for Python, but could help you. Calling newwin() creates and

诅咒newwin和subwin之间的区别

我似乎无法找到有关curses.newwin和curses.subwin之间区别的任何信息 你知道任何? 我想有一个屏幕分为3个不同的部分,不同的更新时间(不是每个按键都必须更新)。 这是正确的方向吗? 谢谢 记录不是Python,但可以帮助你。 调用newwin()创建并返回一个指向具有给定行数和列数的新窗口的指针。 窗口的左上角位于begin_y行,begin_x列。 如果nlines或ncols为零,则默认为LINES-begin_y和COLS-begin_x。 通过调用

Reshape 3D numpy array with heterogeneous dimensions to 2D numpy array

My data is 3D but only the third axis have a knowed dimension and I need to convert it to a 2D array. Example: input = [[[1, 2, 3]], [[4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]] input_reshaped = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]] So I have a 3D data in which the first and second dimensions are variable along my dataset and the th

重新塑造具有异质维度的3D numpy阵列到二维numpy阵列

我的数据是三维的,但只有第三轴有一个已知维度,我需要将其转换为二维数组。 例: input = [[[1, 2, 3]], [[4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]] input_reshaped = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]] 所以我有一个3D数据,其中第一维和第二维在我的数据集中是可变的,而第三维是恒定的并且先前已知。 输入中的每个块也是numpy数组。 我知道

Slice 2d array into smaller 2d arrays

Is there a way to slice a 2d array in numpy into smaller 2d arrays? Example [[1,2,3,4], -> [[1,2] [3,4] [5,6,7,8]] [5,6] [7,8]] So I basically want to cut down a 2x4 array into 2 2x2 arrays. Looking for a generic solution to be used on images. It seems to me that this is a task for numpy.split or some variant. eg a = np.arange(30).reshape([5,6]) #a.shape = (5,6) a1

将二维数组切片成较小的二维数组

有没有办法将nd数组中的2d数组切分成更小的2d数组? 例 [[1,2,3,4], -> [[1,2] [3,4] [5,6,7,8]] [5,6] [7,8]] 所以我基本上想要将2x4阵列分割成2个2x2阵列。 寻找在图像上使用的通用解决方案。 在我看来,这是numpy.split或其他变体的一项任务。 例如 a = np.arange(30).reshape([5,6]) #a.shape = (5,6) a1 = np.split(a,3,axis=1) #'a1' is a list of 3 arrays of shape (5,2) a2 = np.split

Broadcasting a 1d numpy array with a 2d numpy array

This is probably a really simple question, but I am not figuring this out. I have a 2D numpy array, which is of shape (3,2) and a 1D array of shape (3,): A = [[2,4],[6,8][10,12]] B = [1,2,4] I would like to divide array A by array B, resulting in: [[2,4],[3,4][2.5,3]] But numpy will not let me do this, I think because the shape is not right. I get the familiar 'operands coul

用2d numpy数组广播1d numpy数组

这可能是一个非常简单的问题,但我没有想到这一点。 我有一个二维numpy数组,形状(3,2)和一维形状(3,)的一维数组: A = [[2,4],[6,8][10,12]] B = [1,2,4] 我想通过数组B分割数组A,结果是: [[2,4],[3,4][2.5,3]] 但numpy不会让我这样做,我认为是因为形状不对。 我得到熟悉的'操作数不能与形状(10,2)(10,)'错误一起播出。 我尝试重塑和swapaxis,但它不起作用。 我更希望能够在没有for

averaging over subsets of array in numpy

I have a numpy array of the shape (10, 10, 10, 60). The dimensions could be arbitrary but this just an example. I want to reduce this to an array of (10, 10, 10, 20) by taking the mean over some subsets I have two scenarios: 1 : Take the mean of every (10, 10, 10, 20) block ie have three (10, 10, 10, 20) block and take the mean between the three. This can be done with: m = np.mean((x[..., :

在numpy中对数组的子集进行平均

我有一个形状不规则的阵列(10,10,10,60)。 尺寸可以是任意的,但这只是一个例子。 我想通过在一些子集上取平均值来减少这个数组(10, 10, 10, 20) ,我有两种情况: 1 :取每个(10, 10, 10, 20)块的平均值,即具有三个(10, 10, 10, 20)块,并取三者之间的平均值。 这可以通过以下方式完成: m = np.mean((x[..., :20], x[..., 20:40], x[...,40:60]), axis=3) 。 我的问题是,如果没有编写一些显式循环,最后一个维度是

Reshaping a numpy array

I would like to reshape the following numpy array in iPython: array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]]) # Array A to: array([[1, 5, 9],[2, 6, 10],[3, 7, 11],[4, 8, 12]]) # Array B The main task is to calculate the mean of the first elements of Array A (meaning the mean of 1,5,9), then the second elements, etc. I thought the easiest way to do this is to reshape the array and afterwa

重塑一个numpy数组

我想在iPython中重塑下面的numpy数组: array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]]) # Array A 至: array([[1, 5, 9],[2, 6, 10],[3, 7, 11],[4, 8, 12]]) # Array B 主要任务是计算数组A的第一个元素的均值(意思是1,5,9的平均数),然后计算第二个元素等。 我认为最简单的方法是重塑阵列,然后计算它的平均值。 有没有办法做到这一点,而不通过for循环循环数组? 要做这种计算,你应该使用numpy。 假设a

3d Numpy array to 2d

I have a 3d matrix like this arange(16).reshape((4,2,2)) array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]], [[12, 13], [14, 15]]]) and would like to stack them in grid format, ending up with array([[ 0, 1, 4, 5], [ 2, 3, 6, 7], [ 8, 9, 12, 13], [10, 11, 14, 15]]) Is there a way of doin

3d numpy数组到2d

我有这样的3d矩阵 arange(16).reshape((4,2,2)) array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]], [[12, 13], [14, 15]]]) 并希望以网格格式堆叠它们,最终以 array([[ 0, 1, 4, 5], [ 2, 3, 6, 7], [ 8, 9, 12, 13], [10, 11, 14, 15]]) 有没有一种方式没有明确的堆栈(和/或vstacking)他们或添加额外

Changing data in a dataframe with hierarchical indexing

How can I change every element in a DataFrame with hierarchical indexing? For example, maybe I want to convert strings into floats: from pandas import DataFrame f = DataFrame({'a': ['1,000','2,000','3,000'], 'b': ['2,000','3,000','4,000']}) f.columns = [['level1', 'level1'],['item1', 'item2']] f Out[152]: level1 item1 item2 0 1,000 2,000 1 2,000 3,000 2 3,000 4,000

使用分层索引更改数据框中的数据

如何使用分层索引来更改DataFrame中的每个元素? 例如,也许我想将字符串转换为浮点数: from pandas import DataFrame f = DataFrame({'a': ['1,000','2,000','3,000'], 'b': ['2,000','3,000','4,000']}) f.columns = [['level1', 'level1'],['item1', 'item2']] f Out[152]: level1 item1 item2 0 1,000 2,000 1 2,000 3,000 2 3,000 4,000 我试过这个: def clean(group): group = gro

Scons copy header files to build directory

I'm trying to copy a number of headers files from my source directories to an 'includes' directory inside my build directory using scons. My target is a static library and I want to distribute it together with its relevant headers. The expected end result: build |-- objects -> .o output files for constructing libmclib.a |-- includes | |-- foo.h | `-- bar.h `-- libmclib.a My

Scons复制头文件以构建目录

我正在尝试使用scons将我的源目录中的许多头文件复制到构建目录中的'includes'目录。 我的目标是一个静态库,我想把它和它的相关头文件一起分发。 预期的最终结果: build |-- objects -> .o output files for constructing libmclib.a |-- includes | |-- foo.h | `-- bar.h `-- libmclib.a 我的SConstruct: #!python target = 'mock' env = Environment(LINKCOM = "$LINK -o $TARGET $SOURCES $LINKFLAGS