我创建了一个MappingsBean类,其中指定了CSV文件的所有列.接下来,我解析XML文件并创建mappingbeans列表.然后我将该数据写入CSV文件作为报告.
我正在使用以下注释:
public class MappingsBean {
@CsvBindByName(column = "TradeID")
@CsvBindByPosition(position = 0)
private String tradeId;
@CsvBindByName(column = "GWML GUID", required = true)
@CsvBindByPosition(position = 1)
private String gwmlGUID;
@CsvBindByName(column = "MXML GUID", required = true)
@CsvBindByPosition(position = 2)
private String mxmlGUID;
@CsvBindByName(column = "GWML File")
@CsvBindByPosition(position = 3)
private String gwmlFile;
@CsvBindByName(column = "MxML File")
@CsvBindByPosition(position = 4)
private String mxmlFile;
@CsvBindByName(column = "MxML Counterparty")
@CsvBindByPosition(position = 5)
private String mxmlCounterParty;
@CsvBindByName(column = "GWML Counterparty")
@CsvBindByPosition(position = 6)
private String …Run Code Online (Sandbox Code Playgroud) 我对Optional.ofNullable方法感到惊讶.有一天我写了一个应该返回Optional的函数:
private Optional<Integer> extractFirstValueFrom(InsightsResponse insight) {
return Optional.ofNullable(insight.getValues().get(0).getValue());
}
Run Code Online (Sandbox Code Playgroud)
我错误地认为Optional.ofNullable会阻止任何NullPointerExceptions内部的参数表达.
现在我想我知道这是非常愚蠢的想法.Java必须首先解析参数以将其传递给Optional.ofNullable调用.
但我有一个问题.有没有一个很好的方法来实现我的目标?我想从表达式获取insight.getValues().get(0).getValue()一些Integer值或null.Null可以是表达式中的每一个:insight.getValues()或insight.getValues().get(0).
我知道我可以把它放在try/catch块中,但我想知道是否有更优雅的解决方案.
我有一个关于如何为我的程序设计好的问题.我的程序非常简单,但我希望拥有良好的架构,并使我的程序在未来易于扩展.
我的程序需要从外部数据源(XML)获取数据,从这些数据中提取信息,最后需要准备SQL语句以将信息导入数据库.因此,对于现在存在的所有外部数据源,将来会有我的应用程序的简单"流程":获取,提取和加载.
我正在考虑创建名为DataFetcher,DataExtractor和DataLoader的泛型类,然后编写将继承它们的特定类.我想我需要一些工厂设计模式,但是哪个?FactoryMethod还是抽象工厂?
我也想不要使用这样的代码:
if data_source == 'X':
fetcher = XDataFetcher()
elif data_source == 'Y':
fetcher = YDataFetcher()
....
Run Code Online (Sandbox Code Playgroud)
理想情况下(我不确定这是否容易实现),我想编写新的"数据源处理器",在现有代码中添加一行或两行,我的程序将从新数据源加载数据.
如何利用设计模式来实现目标?如果您可以在python中提供一些示例,那就太棒了.
我在Python 3中生成具有超时的异步子进程时遇到问题.
我想要实现的目标:我希望异步生成多个进程而不等待结果,但我还要确保每个生成的进程都会在给定的超时内结束.
我在这里发现了类似的问题:在Python中使用模块'subprocess'和超时以及异步后台进程?但他们没有解决我的问题.
我的代码看起来像这样.我有使用模块'subprocess'和超时建议的Command类:
class Command(object):
def __init__(self, cmd):
self.cmd = cmd
self.process = None
def run(self, timeout):
def target():
print('Thread started')
args = shlex.split(self.cmd)
self.process = subprocess.Popen(args, shell=True)
self.process.communicate()
print('Thread finished')
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout)
if thread.is_alive():
print('Terminating process')
self.process.terminate()
thread.join()
Run Code Online (Sandbox Code Playgroud)
然后当我想要产生子进程时:
for system in systems:
for service in to_spawn_system_info:
command_str = "cd {0} && python proc_ip.py {1} {2} 0 2>>{3}".format(home_dir,
service, system, service_log_dir)
command = Command(command_str)
command.run(timeout=60)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,输出似乎等待每个命令生成并结束.我明白了
Thread …Run Code Online (Sandbox Code Playgroud) 我在使用Python 3.3 32位的Windows上工作.我已经安装了peewee并想尝试它的一些功能.我已经开始使用Peewee Quickstart(http://peewee.readthedocs.org/en/latest/peewee/quickstart.html).
我的代码看起来像这样:
from peewee import *
db = SqliteDatabase('people.db')
class Person(Model):
name = CharField()
birthday = DateField()
is_relative = BooleanField()
class Meta:
database = db
class Pet(Model):
owner = ForeignKeyField(Person, related_name = "pets")
name = CharField()
animal_type = CharField()
class Meta:
database = db
Person.create_table()
Pet.create_table()
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
File "<stdin>", line 1, in <module>
File "<string>", line 21, in <module>
File "C:\Python33\lib\site-packages\peewee.py", line 2094, in create_table
db = cls._meta.database
AttributeError: type object 'Person' has no attribute …Run Code Online (Sandbox Code Playgroud) 我有一个关于使用 XPath 时的性能问题的问题。
哪一个更好,为什么?(当然,如果是性能的话):
//A/B/C[@id="x"]/../..
Run Code Online (Sandbox Code Playgroud)
与
//A[B/C[@id="x"]]
Run Code Online (Sandbox Code Playgroud) python ×3
java ×2
python-3.x ×2
architecture ×1
asynchronous ×1
csv ×1
java-8 ×1
opencsv ×1
optional ×1
peewee ×1
performance ×1
subprocess ×1
xml ×1
xpath ×1