我意识到Go没有类,而是推动了结构的概念.
结构体是否具有任何类型的初始化函数,可以类似于类的__construct()函数调用?
例:
type Console struct {
X int
Y int
}
func (c *Console) init() {
c.X = "5"
}
// Here I want my init function to run
var console Console
// or here if I used
var console Console = new(Console)
Run Code Online (Sandbox Code Playgroud) 在java中使这种代码有效的原因是什么?它是出于某种特定原因而存在,还是仅仅是其他Java语言设计决策的副产品?难道你不能只使用consructor来达到同样的效果吗?
class Student
{
{
System.out.println("Called when Student class is instantiated.");
}
}
Run Code Online (Sandbox Code Playgroud) 我不确定这个结构意味着什么,但我已经看过几次了.下面的示例来自另一个Stack Overflow问题.我不确定如何解释最初的"或"构造本身:
Object.keys = Object.keys || (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !{toString:null}.propertyIsEnumerable("toString"),
DontEnums = [
'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty',
'isPrototypeOf', 'propertyIsEnumerable', 'constructor'
],
DontEnumsLength = DontEnums.length;
//etc...
});
Run Code Online (Sandbox Code Playgroud) 我一直在使用解析模块构造,并发现自己真的迷恋它的数据结构的声明性质.对于那些不熟悉它的人,您可以编写Python代码,这些代码基本上就像您在实例化时通过嵌套对象尝试解析的内容.
ethernet = Struct("ethernet_header",
Bytes("destination", 6),
Bytes("source", 6),
Enum(UBInt16("type"),
IPv4 = 0x0800,
ARP = 0x0806,
RARP = 0x8035,
X25 = 0x0805,
IPX = 0x8137,
IPv6 = 0x86DD,
),
)
Run Code Online (Sandbox Code Playgroud)
虽然构造实际上不支持在此结构中存储值(您可以将抽象Container解析为字节流或将字节流解析为抽象容器),但我希望扩展框架,以便解析器也可以在解析时存储值可以用点符号ethernet.type访问它们.
但是在这样做时,认为这里最好的解决方案是编写编码/解码机制的通用方法,这样您就可以注册编码/解码机制,并能够从抽象数据结构中生成各种输出(解析器本身) ),以及解析器的输出.
举个例子,默认情况下,当您运行通过解析器打包的以太网时,最终会得到类似dict的内容:
Container(name='ethernet_header',
destination='\x01\x02\x03\x04\x05\x06',
source='\x01\x02\x03\x04\x05\x06',
type=IPX
)
Run Code Online (Sandbox Code Playgroud)
我不想两次解析东西 - 理想情况下我希望解析器以可配置的方式生成'target'对象/字符串/字节.
这个想法的根源在于你可以为消费或处理结构注册各种"插件",这样你就可以以编程方式生成XML或Graphviz图,以及能够从字节转换为Python dicts.任务的关键是,走一个节点树并基于编码器/解码器,转换并返回转换后的对象.
所以问题基本上是 - 哪种模式最适合这个目的?
我查看了编解码器模块,它相当优雅,因为您创建了编码机制,注册您的类可以编码的东西,您可以指定您想要的特定编码.
'blah blah'.encode('utf8')
Run Code Online (Sandbox Code Playgroud)
有一些现有的用于Python的serdes模块的例子,我想起了JSON - 但它的问题在于它非常具体,并且不容易支持任意格式.你可以编码或解码JSON,基本上就是这样.有像这样构造的各种serdes,有些使用load,*dumps*方法,有些则没有.这是一个废话.
objects = json.loads('{'a': 1, 'b': 2})
Run Code Online (Sandbox Code Playgroud)
我对访问者模式并不十分熟悉,但似乎确实有一些可能适用的机制 - 这个想法(如果我理解正确的话),你设置了一个节点访问者,它就是树并应用一些转换(并返回新的对象?)..我在这里很朦胧.
是否有其他机制可能更加pythonic或已经写入?我考虑过使用ElementTree和子类化Elements - 但是我想在做一些愚蠢的事情之前咨询stackoverflow.
有没有什么办法可以在java中使用字符串的哈希码,并重新创建该字符串?
例如:
String myNewstring = StringUtils.createFromHashCode("Hello World".hashCode());
if (!myNewstring.equals("Hello World"))
System.out.println("Hmm, something went wrong: " + myNewstring);
Run Code Online (Sandbox Code Playgroud)
我这样说,因为我必须将字符串转换为整数值,并从该整数值重构该字符串.
我有这两个实体
Class A {
@OneToMany(mappedBy="a")
private List<B> bs;
}
Class B {
@ManyToOne
private A a;
private String name;
}
Run Code Online (Sandbox Code Playgroud)
1)我想构建一个查询,说明所有A至少有一个名字="mohamede1945"的B
2)我想构建一个查询,说明所有没有任何B的名字="mohamede1945"
谁能帮助我?
我试图序列化使用Construct2.9库解析一些二进制数据的输出.我想将结果序列化为JSON.
packet是Construct类的实例Container.
显然它包含一个隐藏_io的类型BytesIO- 请参阅dict(packet)下面的输出:
{
'packet_length': 76, 'uart_sent_time': 1, 'frame_number': 42958,
'subframe_number': 0, 'checksum': 33157, '_io': <_io.BytesIO object at 0x7f81c3153728>,
'platform':661058, 'sync': 506660481457717506, 'frame_margin': 20642,
'num_tlvs': 1, 'track_process_time': 593, 'chirp_margin': 78,
'timestamp': 2586231182, 'version': 16908293
}
Run Code Online (Sandbox Code Playgroud)
现在,调用json.dumps(packet)显然会导致TypeError:
...
File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.5/json/encoder.py", line 179, in …Run Code Online (Sandbox Code Playgroud) 我想创建一个"哈希"类,它在哈希和树之间起作用.它只是用于存储可以具有上下文的全局值.
这是我如何使用它:
Config.get("root.parent.child_b") #=> "value"
Run Code Online (Sandbox Code Playgroud)
以下是该类的外观:
class Construct
def get(path)
# split path by "."
# search tree for nodes
end
def set(key, value)
# split path by "."
# create tree node if necessary
# set tree value
end
def tree
{
:root => {
:parent => {
:child_a => "value",
:child_b => "another value"
},
:another_parent => {
:something => {
:nesting => "goes on and on"
}
}
}
}
end
end
Run Code Online (Sandbox Code Playgroud)
在Hash和Tree(不是计算机科学专业)之间有某种名称吗?基本上是树的类似哈希的接口.
像这样输出的东西:
t = TreeHash.new …Run Code Online (Sandbox Code Playgroud) 我想Maybe从D中的Haskell 实现,只是为了它的地狱.这是我到目前为止所得到的,但并不是那么好.任何想法如何改进它?
class Maybe(a = int){ } //problem 1: works only with ints
class Just(alias a) : Maybe!(typeof(a)){ }
class Nothing : Maybe!(){ }
Maybe!int doSomething(in int k){
if(k < 10)
return new Just!3; //problem 2: can't say 'Just!k'
else
return new Nothing;
}
Run Code Online (Sandbox Code Playgroud)
Haskell也许定义:
data Maybe a = Nothing | Just a
Run Code Online (Sandbox Code Playgroud) 我写了这个正则表达式:
(.+?)\s*{?\s*(.+?;)\s*}?\s*
Run Code Online (Sandbox Code Playgroud)
哪个测试正常:https : //regex101.com/r/gD2eN7/1
但是当我尝试用 C++ 构建它时,我得到了一个运行时错误。
temp2.exe 中 0x7714C52F 处的未处理异常:Microsoft C++ 异常:
内存位置 0x003BF2EC 处的 std::regex_error。
const auto input = "if (KnR)\n\tfoo();\nif (spaces) {\n foo();\n}\nif (allman)\n{\n\tfoo();\n}\nif (horstmann)\n{\tfoo();\n}\nif (pico)\n{\tfoo(); }\nif (whitesmiths)\n\t{\n\tfoo();\n\t}"s;
cout << input << endl;
cout << regex_replace(input, regex("(.+?)\\s*{?\\s*(.+?;)\\s*}?\\s*"), "$1 {\n\t$2\n}\n") << endl;
Run Code Online (Sandbox Code Playgroud)
我是否使用了 C++ 不支持的功能?