需要一些帮助来理解如何做到这一点; 我将在文件系统上运行递归"查找",并且我希望将信息保存在单个数据库表中 - 具有自引用的层次结构:
这是我要填充的数据库表结构.
目录表:
id int NOT NULL,
name varchar(255) NOT NULL,
parentid int NOT NULL);
Run Code Online (Sandbox Code Playgroud)
这是我想要映射的Java类(仅显示字段):
public DirObject {
int id;
String name;
DirObject parent;
...
Run Code Online (Sandbox Code Playgroud)
对于'root'目录,将使用parentid = 0; real id将从1开始,理想情况下我希望hibernate自动生成id.
有人可以为此提供建议的映射文件; 作为第二个问题,我考虑过像这样做Java类:
public DirObject {
int id;
String name;
List<DirObject> subdirs;
Run Code Online (Sandbox Code Playgroud)
我可以对这两种方法中的任何一种使用相同的数据模型吗?(当然使用不同的映射文件).
---更新:所以我尝试了下面建议的映射文件(谢谢!),在此重复以供参考:
<hibernate-mapping>
<class name="my.proj.DirObject" table="category">
...
<set name="subDirs" lazy="true" inverse="true">
<key column="parentId"/>
<one-to-many class="my.proj.DirObject"/>
</set>
<many-to-one name="parent"
class="my.proj.DirObject"
column="parentId" cascade="all" />
</class>
Run Code Online (Sandbox Code Playgroud)
...并且改变了我的Java类以使'parentid'和'getSubDirs'[返回'HashSet'].
这似乎有用 - 谢谢,但这是我用来驱动它的测试代码 - 我想我在这里没做什么,因为我认为Hibernate将负责保存Set中的从属对象而不必我做这明确吗?
DirObject dirobject=new DirObject();
dirobject.setName("/files");
dirobject.setParent(dirobject);
DirObject …
Run Code Online (Sandbox Code Playgroud) 我目前正在使用XmlTextWriter类将包含大量数据(100000条记录)的数据库表导出到xml文件中,并且我直接写入物理驱动器上的文件.
_XmlTextWriterObject = new XmlTextWriter(_xmlFilePath, null);
Run Code Online (Sandbox Code Playgroud)
虽然我的代码运行正常但我的问题是它是最好的方法吗?我应该首先在内存流中写入整个xml,然后从内存流中将xml文档写入物理文件中吗?在这两种情况下,对内存/性能的影响是什么?
编辑
对不起,我实际上无法表达我的意思.谢谢Ash指出.我确实会使用XmlTextWriter,但我想说是否将物理文件路径字符串传递给XmlTextWriter构造函数(或者,如John建议的那样,传递给XmlTextWriter.Create()
方法)或使用基于流的api.我当前的代码如下所示:
XmlWriter objXmlWriter = XmlTextWriter.Create(new BufferedStream(new FileStream(@"C:\test.xml", FileMode.Create, System.Security.AccessControl.FileSystemRights.Write, FileShare.None, 1024, FileOptions.SequentialScan)), new XmlWriterSettings { Encoding = Encoding.Unicode, Indent = true, CloseOutput = true });
using (objXmlWriter)
{
//writing xml contents here
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Hibernate在数据库中存储和检索Java实体.
我需要建立一个独特字符串列表(为了创建网页'下拉'/'查找').
注意:我真的不想在这里检索实体 - 我想运行等效的SQL'SELECT DISCTINCT(列)FROM表;' 并返回一个字符串列表.
是否有标准的Hibernate成语用于此 - 或者我应该使用其他机制?
你好Pythoneers:下面的代码只是模仿我正在尝试做的事情,但它应该说明我的问题.
我想知道这是否是我从Java编程中获取的肮脏技巧,或者是有效的Pythonic做事方式:基本上我是在创建一堆实例,但我需要跟踪所有实例的"静态"数据因为他们被创造了.
class Myclass:
counter=0
last_value=None
def __init__(self,name):
self.name=name
Myclass.counter+=1
Myclass.last_value=name
Run Code Online (Sandbox Code Playgroud)
还有一些使用这个简单类的输出,表明一切都按预期工作:
>>> x=Myclass("hello")
>>> print x.name
hello
>>> print Myclass.last_value
hello
>>> y=Myclass("goodbye")
>>> print y.name
goodbye
>>> print x.name
hello
>>> print Myclass.last_value
goodbye
Run Code Online (Sandbox Code Playgroud)
这是一种普遍接受的做这种事情的方式,还是一种反模式?
[例如,我不太高兴我显然可以在班级(好)和外面(坏)中设置反击; 也不热衷于在类代码本身中使用完整的命名空间'Myclass' - 只是看起来很笨重; 最后我最初将值设置为'None' - 可能我是通过这样做来打扰静态类型的语言?]
我使用的是Python 2.6.2,程序是单线程的.
给定一个输入XML文档,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<title> This contains an 'embedded' HTML document </title>
<document>
<html>
<head><title>HTML DOC</title></head>
<body>
Hello World
</body>
</html>
</document>
</root>
Run Code Online (Sandbox Code Playgroud)
如何提取"内部"HTML文档; 将其渲染为CDATA并包含在我的输出文档中?
因此输出文档将是一个HTML文档; 其中包含一个文本框,将元素显示为文本(因此它将显示内部文档的"源视图").
我试过这个:
<xsl:template match="document">
<xsl:value-of select="*"/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
但这只会呈现文本节点.
我试过这个:
<xsl:template match="document">
<![CDATA[
<xsl:value-of select="*"/>
]]>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
但这逃脱了实际的XSLT,我得到:
<xsl:value-of select="*"/>
Run Code Online (Sandbox Code Playgroud)
我试过这个:
<xsl:output method="xml" indent="yes" cdata-section-elements="document"/>
[...]
<xsl:template match="document">
<document>
<xsl:value-of select="*"/>
</document>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
这确实插入了CDATA部分,但输出仍然只包含文本(剥离元素):
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<title>My doc</title>
</head>
<body>
<h1>Title: This contains an 'embedded' HTML document </h1>
<document><![CDATA[
HTML DOC
Hello …
Run Code Online (Sandbox Code Playgroud) 从REPL(Cloure 1.4.0)我试图使用source
宏来显示我的函数的定义 - 但它回复'源未找到'
我可以用source
在source
自己喜欢这个(可以看到它使用了source-fn
) -但不知道为什么它不喜欢我的defn x[] "hello"
函数的定义?
user=> (source source)
(defmacro source
"Prints the source code for the given symbol, if it can find it.
This requires that the symbol resolve to a Var defined in a
namespace for which the .clj is in the classpath.
Example: (source filter)"
[n]
`(println (or (source-fn '~n) (str "Source not found"))))
nil
user=> (defn x[] "hello")
#'user/x
user=> (source x)
Source not …
Run Code Online (Sandbox Code Playgroud) 在python中有一个名为range()
list 的方法,例如:
>>> a = range(1,10)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> type(a)
<type 'list'>
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的配置文件:
[ports]
scan_range = 1,10
Run Code Online (Sandbox Code Playgroud)
输出:
1,10
<type 'str'>
Run Code Online (Sandbox Code Playgroud)
我想读取此配置文件并使用它来生成range()方法的参数 - 我该怎么做?
我正在这样设置变量“ a”:
$a=dir -recurse c:\temp
Run Code Online (Sandbox Code Playgroud)
如果现在使用“ get-member”检查该对象,如下所示:
$a|get-member
Run Code Online (Sandbox Code Playgroud)
我得到类型,还有所有方法和其他属性,如下所示:
TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Mode CodeProperty System.String Mode{get=Mode;}
AppendText Method System.IO.StreamWriter AppendText()
CopyTo Method System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(string destFileName, bool...
[...]
Run Code Online (Sandbox Code Playgroud)
哪个好 但有时我只是想掌握这种类型(此后我将查找它所做的任何事情)。
所以我尝试了这个:
$a|get-member|select-object -Property typename
Run Code Online (Sandbox Code Playgroud)
最初的输出使我感到惊讶:因为您得到的是集合中每个单独项目的类型名称-并且类型(尽管明显相关)并不相同:
TypeName
--------
System.IO.DirectoryInfo
System.IO.DirectoryInfo
System.IO.DirectoryInfo
[...]
System.IO.FileInfo
[...]
Run Code Online (Sandbox Code Playgroud)
然后我想到了这一点。从某种意义上讲-这是我通过对象管道传递的一组对象;但是后来让我想到:
以前“ Get-Member”实际上是在告诉我什么?当它说类型是'System.IO.FileInfo'时-但是实际上集合包含对象类型的混合吗?
不管它是“获取会员”显示的是-我怎么在那得到确切的事情吗?
我几乎可以(有点,但实际上是错误的)得到了我最初的想法:
$a|get-member|select-object -Property typename -first 1
Run Code Online (Sandbox Code Playgroud)
但这只是偷看“第一个”对象。实际上,对于我获得的“ Get-Member”输出,我给出了不同的答案。
那么,“ Get-Member”显示的“ TypeName”是什么?它存储在哪里?
是'dir'的值(针对文件路径的Get-ChildItem)仅仅是对象的集合,还是它的父对象(具有自己的“标量”属性集)和引用关联对象集合的单个属性?
我只是想读一个标准的linux'/ etc/passwd'文件:拆分成记录.这似乎工作(所有行都回显到终端)但最后抛出一个异常?(见下文)
这项计划有什么用?
(use 'clojure.java.io)
(use 'clojure.string)
(defn process_file[infile] (
(defstruct user :username
:password
:uid
:gid
:comment
:home_dir
:shell)
(def record_separator #":")
(with-open [rdr (reader infile)]
(doseq [line (line-seq rdr)]
(def fields (split line record_separator) )
(def user_record (apply struct user fields) )
(println (user_record :username) )
)
)
)
)
; main
(process_file "/etc/passwd")
[ after all the lines read have been output ]
Exception in thread "main" java.lang.ClassCastException: clojure.lang.PersistentStructMap$Def cannot be cast to clojure.lang.IFn
at clojure.lang.Var.fn(Var.java:392)
at clojure.lang.Var.invoke(Var.java:419) …
Run Code Online (Sandbox Code Playgroud) 参考这篇文章,我已经成功地获得了一个Linux设备(Raspberry Pi A +型)来将我的Bluetooth 4.0 USB软件狗切换到“广告”状态:
sudo hciconfig hci0 leadv 3
Run Code Online (Sandbox Code Playgroud)
要么
sudo hciconfig hci0 leadv 0
Run Code Online (Sandbox Code Playgroud)
我使用运行“ LightBlue Explorer”应用程序的iPod进行了验证。
到目前为止一切都很好。
从另一个具有蓝牙4.0加密狗的Linux盒(另一个Pi)中获取;我还可以使用以下命令查看设备:
sudo hcitool lescan --passive
Run Code Online (Sandbox Code Playgroud)
返回如下内容:
LE Scan ...
xx:xx:xx:xx:xx:xx (unknown)
yy:yy:yy:yy:yy:yy (unknown)
xx:xx:xx;xx:xx:xx (unknown)
Run Code Online (Sandbox Code Playgroud)
但是该命令会不断循环刷新设备列表。
所以我的主要问题是:是否可以运行该命令的变体,侦听(说)5秒;返回发现要播发的设备列表并退出?
我想构建一个简单的脚本(理想情况下为Python程序),该脚本将定期唤醒,(被动地)监听流量几秒钟,然后返回源设备列表。
另外:我不确定为什么该命令为找到的设备显示“未知”。(而LightBlue确实会标识名称)。