我正在用Python编写一些J绑定(https://gist.github.com/Synthetica9/73def2ec09d6ac491c98).但是,我遇到了处理任意精度整数的问题:输出没有任何意义.每次都会有所不同(但总体规模相同).相关的一段代码:
def JTypes(desc, master):
newdesc = [item.contents.value for item in desc]
type = newdesc[0]
if debug: print type
rank = newdesc[1]
shape = ct.c_int.from_address(newdesc[2]).value
adress = newdesc[3]
#string
if type == 2:
charlist = (ct.c_char.from_address(adress+i) for i in range(shape))
return "".join((i.value for i in charlist))
#integer
if type == 4:
return ct.c_int.from_address(adress).value
#arb-price int
if type == 64:
return ct.c_int.from_address(adress).value
Run Code Online (Sandbox Code Playgroud)
和
class J(object):
def __init__(self):
self.JDll = ct.cdll.LoadLibrary(os.path.join(jDir, "j.dll"))
self.JProc = self.JDll.JInit()
def __call__(self, code):
#Exec code, I …Run Code Online (Sandbox Code Playgroud) 任何人都可以向我解释为什么A()+A()会出错,但B()+B()按预期工作?当我编写更大的代码时,我遇到了这个,但这似乎是重现它所需的最小代码.
from types import MethodType
D = {'__add__': lambda x, y: "".join((repr(x), repr(y)))}
class A(object):
def __getattr__(self, item):
if item == '__coerce__':
raise AttributeError()
return MethodType(D[item], self)
def __repr__(self):
return "A"
class B():
def __getattr__(self, item):
if item == '__coerce__':
raise AttributeError()
return MethodType(D[item], self)
def __repr__(self):
return "B"
try:
A()+A()
except Exception as e:
print e
B()+B()
Run Code Online (Sandbox Code Playgroud)
有人有解释吗?
我正在尝试使用该IPython.display模块将对象转换为 Markdown。但是,似乎没有什么好方法可以将这个 Markdown 导出为字符串。
行为:
>>> from IPython.display import *
>>> from numpy import *
>>> display_markdown(eye(3))
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Run Code Online (Sandbox Code Playgroud)
通缉行为:
>>> display_markdown_string(eye(3))
"$$\left( ... \right)$$"
Run Code Online (Sandbox Code Playgroud)
有没有好的方法来实现这一目标?在我看来,这个功能必须在 IPython 中的某个地方出现,因为它可以在 Notebooks 中完成。
我试图组合一个类似stringIO的函数,我想知道是否有可能构建一个捕获所有可能方法的类,以便以下方法可行:
a = magicclass("Hello World!") #Hello world would be the return
print a() #Would print Hello world
print a.read() #should also print hello world
print a.adsf.asdf.xyz.random() #should also print hello world
Run Code Online (Sandbox Code Playgroud)
我不知道如何通过这个,我可以定义我想要调用的所有可能的方法,但如果我想将它传递给黑盒函数,这将是有问题的.
#This works, but only for the main method.
#Every submethod has to have its own class defined, if you know what I mean.
def emptyreturnfunc(returnval): lambda: returnval
b = emptyreturnfunc("Hello World")
print b() #Does work
print b.asdf() #Doesn't work.
Run Code Online (Sandbox Code Playgroud)
当然,我知道为什么会这样,但我怎样才能让它发挥作用呢?任何提示?
我过去几周一直在研究J,而且真正让我烦恼的是#操作员的二元情况:我使用它的唯一方法是类似于以下内容:
(1 p: a) # a
Run Code Online (Sandbox Code Playgroud)
如果相反,则可以省略括号:
a #~ 1 p: a
Run Code Online (Sandbox Code Playgroud)
为什么选择不采取与当前论点相反的方式?向后熟悉APL,或者我完全忽视的东西?
为什么2(*i.)5评估0 2 4 6 8?
很明显2*i.5,但是(),从右到左创建一个钩子并进行评估似乎我们得到了
(*i.)5 == 0 5 10 15 20
Run Code Online (Sandbox Code Playgroud)
并且2不会对该列表采取行动 - 所以我哪里错了?