这是一件小事,真的:我有这个函数将dict对象转换为xml.
这是功能:
def dictToXml(d):
from xml.sax.saxutils import escape
def unicodify(o):
if o is None:
return u'';
return unicode(o)
lines = []
def addDict(node, offset):
for name, value in node.iteritems():
if isinstance(value, dict):
lines.append(offset + u"<%s>" % name)
addDict(value, offset + u" " * 4)
lines.append(offset + u"</%s>" % name)
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
lines.append(offset + u"<%s>" % name)
addDict(item, offset + u" " * 4)
lines.append(offset + u"</%s>" % name)
else:
lines.append(offset + u"<%s>%s</%s>" …Run Code Online (Sandbox Code Playgroud) 我想将这个线性循环转换为并发循环:
for(Item item : ItemList) {
processItem(item);
}
Run Code Online (Sandbox Code Playgroud)
这真的是最短的方式吗?
class Worker implements Runnable {
Item item;
Worker(Item item) {
this.item = item;
}
public void Run() {
processItem(item);
}
}
ExecutorService exec = Executors.newFixedThreadPool(THREADPOOL_SIZE);
for(Item item : ItemList) {
exec.execute(new Worker(item));
}
exec.shutdown();
boolean properFinish = false;
try {
properFinish = exec.awaitTermination(50, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Run Code Online (Sandbox Code Playgroud)
具体来说,我想要一种使用匿名类的方法,但实际上,任何使这种更短和更易读的方法都会受到赞赏.
更新:刚刚意识到我有点愚蠢,因为在这个例子中使用匿名类很容易:
for(final Item item : ItemList) {
exec.execute(new Runnable() {
public void run() {
processItem(item);
}
});
} …Run Code Online (Sandbox Code Playgroud) 我在服务器上有这个bash脚本,每小时运行一次,通过cron.我非常高兴,但现在用户希望能够通过Web界面配置频率.
我不习惯以编程方式操作cron配置,但我不确定其他选项是否更好.
我看到它的方式,我可以:
我该怎么办?
编辑:澄清一下,我担心操纵cron的主要原因是因为它基本上是文本操作而没有验证,如果我搞砸了,我的其他任何cron作业都不会运行.
这是我最终做的事情:
根据stefanw的建议,我在bash脚本的顶部添加了以下行:
if [ ! `cat /home/username/settings/run.freq` = $1 ]; then
exit 0
fi
Run Code Online (Sandbox Code Playgroud)
我设置了以下cron作业:
0 */2 * * * /home/username/scripts/publish.sh 2_hours
@hourly /home/username/scripts/publish.sh 1_hour
*/30 * * * * /home/username/scripts/publish.sh 30_minutes
*/10 * * * * /home/username/scripts/publish.sh 10_minutes
Run Code Online (Sandbox Code Playgroud)
从Web界面,我让用户在这四个选项之间进行选择,并根据用户选择的内容,将字符串2_hours/1_hour/30_minutes/10_minutes写入文件中/home/username/settings/run.freq.
我不喜欢它,但它似乎是最好的选择.
我现在有一个问题,这个是关于 lucene 的。我试图制作一个 lucene 源代码,它可以进行索引并首先使用 RAMDirectory 将它们存储在内存中,然后使用 FSDirectory 将内存中的索引刷新到磁盘中。我对这段代码做了一些修改,但没有效果。也许你们中的一些人可以帮助我一点。
那么,在将 RAMDirectory 放入 FSDirectory 之前,将 RAMDirectory 集成到此源代码中的最佳方法是什么?尽管这里是源代码,但任何帮助将不胜感激。
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class SimpleFileIndexer {
public static void main(String[] args) throws Exception {
File indexDir = new File("C:/Users/Raden/Documents/lucene/LuceneHibernate/adi");
File dataDir = new File("C:/Users/Raden/Documents/lucene/LuceneHibernate/adi");
String suffix = "txt";
SimpleFileIndexer indexer = new SimpleFileIndexer();
int numIndex = indexer.index(indexDir, dataDir, suffix);
System.out.println("Total files indexed " + numIndex);
}
private int index(File indexDir, …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test")
EntityManager entityManager = emf.createEntityManager()
User user = entityManager.find(User.class, 0);
entityManager.getTransaction().begin();
entityManager.getTransaction().rollback();
entityManager.refresh(user);
Run Code Online (Sandbox Code Playgroud)
这会在第四行抛出IllegalArgumentException,说"实体未被管理".如果我改变第三行.commit()而不是.rollback(),一切似乎都正常.
这里发生了什么?我可以防止这种情况发生吗?
更新: @DataNucleus将我引向PersistenceContext.如何更改代码中的持久性上下文?
如果将函数应用于所有元素,您会将一个函数命名为列表和函数,并返回True,会得到相同的结果吗?
def identical_results(l, func):
if len(l) <= 1: return True
result = func(l[0])
for el in l[1:]:
if func(el) != result:
return False
return True
Run Code Online (Sandbox Code Playgroud)
这个东西有一个很好的普遍接受的名字吗?如果你能以一种不那么笨重的方式实施,就可以获得奖励.
naming functional-programming terminology higher-order-functions
我正在将一个文件读入数组并尝试取出数字并将它们作为双精度放入自己的数组中.显然我的中间名必须是"错误".从我可以看出代码是好的....至少没有什么东西跳出来对我.这就是它的荣耀.
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.lang.Object.*;
public class ReadFromFile {
public static void main (String[] args){
File file = new File("vector10.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
StringBuffer sb = new StringBuffer();
String string = new String();
try{
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while((string=dis.readLine()) != null){
sb.append(string+"\n");
}
fis.close();
bis.close();
dis.close();
System.out.println(sb);
String newString = sb.toString(); …Run Code Online (Sandbox Code Playgroud) 我使用(一部分)挂架写了一个小的内部网络应用程序.事实证明,我现在需要允许用户从网络访问它.这不是一个面向Web的应用程序,它有一堆巨大的安全漏洞.
什么是最简单的方法,我可以确保该网站安全地可供该用户使用,但没有其他人?
我正在考虑类似apache的简单HTTP身份验证,但更安全.(OpenID是一个很好的匹配吗?)
只有一个用户.无需任何用户管理,甚至无需更改密码.此外,我相信用户不会损坏服务器(实际上是他的).
如果它适合我,我会将它保留在防火墙后面并使用ssh端口转发,但我想为这个用户提供更简单的东西.
编辑:嗯...从答案判断,这应该是在服务器故障.如果主持人正在阅读此内容,请考虑迁移它.