数据类 - 基本方法链接
Dataclasses - Basic method chaining
我正在尝试创建一个 class,它有 2 种方法:
查询数据(作为生成器)
另存为json
@dataclass
class Data_Query:
hierarchic: str
sku: bool
pred_lenght: int
def query(self, db):
if (self.hierarchic == 'store' and self.sku == True):
x = db.aggregate([{...}]);
self.export_json(x)
def export_json(self, x, file):
with open(f'/home/Documents/dataset/{file}', 'w') as fp:
for i in x:
json.dump(i, fp)
fp.write('\n')
当我执行查询方法时,两个方法都被执行了。
data = Data_Query('store', True, 56)
data.query(db)
我需要修改什么才能分开调用这些方法?
我的预期输出:
data.query(db).export_json('abc.json')
不是直接从 query
调用 export_json
,而是将结果保存在实例属性中,return self
以启用链接。然后 export_json
在实例上查找保存的查询,而不是将其作为参数。
@dataclass
class Data_Query:
hierarchic: str
sku: bool
pred_lenght: int
def query(self, db):
if (self.hierarchic == 'store' and self.sku == True):
<b>self.</b>x = db.aggregate([{...}]);
<b># </b>self.export_json(x)
<b>return self</b>
def export_json(self<b>, file</b>):
<b>try:
x = self.x
except AttributeError:
return
</b>
with open(f'/home/Documents/dataset/{file}', 'w') as fp:
for i in x:
json.dump(i, fp)
fp.write('\n')
<b>del self.x</b>
现在您可以编写 data.query(db).export_json('abc.json')
,以及 JSON 文件
只有在实际发生查询时才会写入。
然而,这并不是最好的设计。 export_json
的任何内容都不是您的 class 特有的;它应该是一个带有结果和文件名的常规函数
并且您在 进行查询后调用 ,if 查询 return 任何结果。更像是
@dataclass
class Data_Query:
hierarchic: str
sku: bool
pred_lenght: int
def query(self, db):
if (self.hierarchic == 'store' and self.sku == True):
return db.aggregate([{...}])
def export_json(self, x, file):
with open(f'/home/Documents/dataset/{file}', 'w') as fp:
for i in x:
json.dump(i, fp)
fp.write('\n')
result = data.query(db)
if result is not None:
export_json(result, 'abc.json')
你可能争论"Of course export_json
is related to my class; it assumes that x
is an iterable of objects, which is something defined by the query
method."在那种情况下,你可能会考虑定义一个QueryResult
class,并使export_json
that class 的一种方法。然后 DataQuery.query
return 是 QueryResult
的实例,链接感觉不那么随意:您正在导出 结果 ,而不是 查询.
# By the way, I am assuming there is more to this class than a query
# method; otherwise, there should probably just be a regular function
# that takes the db, hierarchic, and sku as arguments.
@dataclass
class DataQuery:
hierarchic: str
sku: bool
pred_length: int
def query(self, db):
result = None
if self.hierarchic == 'store' and self.sku:
result = db.aggregate(...)
return QueryResult(result)
class QueryResult:
def __init__(self, result):
self.result = result
def export_json(self, file):
if self.result is None:
return
with open(f'/home/Documents/dataset/{file}', 'w') as fp:
for i in x:
json.dump(i, fp)
fp.write('\n')
data.query(db).export_json('abc.json')
我正在尝试创建一个 class,它有 2 种方法:
查询数据(作为生成器)
另存为json
@dataclass class Data_Query: hierarchic: str sku: bool pred_lenght: int def query(self, db): if (self.hierarchic == 'store' and self.sku == True): x = db.aggregate([{...}]); self.export_json(x) def export_json(self, x, file): with open(f'/home/Documents/dataset/{file}', 'w') as fp: for i in x: json.dump(i, fp) fp.write('\n')
当我执行查询方法时,两个方法都被执行了。
data = Data_Query('store', True, 56)
data.query(db)
我需要修改什么才能分开调用这些方法?
我的预期输出:
data.query(db).export_json('abc.json')
不是直接从 query
调用 export_json
,而是将结果保存在实例属性中,return self
以启用链接。然后 export_json
在实例上查找保存的查询,而不是将其作为参数。
@dataclass
class Data_Query:
hierarchic: str
sku: bool
pred_lenght: int
def query(self, db):
if (self.hierarchic == 'store' and self.sku == True):
<b>self.</b>x = db.aggregate([{...}]);
<b># </b>self.export_json(x)
<b>return self</b>
def export_json(self<b>, file</b>):
<b>try:
x = self.x
except AttributeError:
return
</b>
with open(f'/home/Documents/dataset/{file}', 'w') as fp:
for i in x:
json.dump(i, fp)
fp.write('\n')
<b>del self.x</b>
现在您可以编写 data.query(db).export_json('abc.json')
,以及 JSON 文件
只有在实际发生查询时才会写入。
然而,这并不是最好的设计。 export_json
的任何内容都不是您的 class 特有的;它应该是一个带有结果和文件名的常规函数
并且您在 进行查询后调用 ,if 查询 return 任何结果。更像是
@dataclass
class Data_Query:
hierarchic: str
sku: bool
pred_lenght: int
def query(self, db):
if (self.hierarchic == 'store' and self.sku == True):
return db.aggregate([{...}])
def export_json(self, x, file):
with open(f'/home/Documents/dataset/{file}', 'w') as fp:
for i in x:
json.dump(i, fp)
fp.write('\n')
result = data.query(db)
if result is not None:
export_json(result, 'abc.json')
你可能争论"Of course export_json
is related to my class; it assumes that x
is an iterable of objects, which is something defined by the query
method."在那种情况下,你可能会考虑定义一个QueryResult
class,并使export_json
that class 的一种方法。然后 DataQuery.query
return 是 QueryResult
的实例,链接感觉不那么随意:您正在导出 结果 ,而不是 查询.
# By the way, I am assuming there is more to this class than a query
# method; otherwise, there should probably just be a regular function
# that takes the db, hierarchic, and sku as arguments.
@dataclass
class DataQuery:
hierarchic: str
sku: bool
pred_length: int
def query(self, db):
result = None
if self.hierarchic == 'store' and self.sku:
result = db.aggregate(...)
return QueryResult(result)
class QueryResult:
def __init__(self, result):
self.result = result
def export_json(self, file):
if self.result is None:
return
with open(f'/home/Documents/dataset/{file}', 'w') as fp:
for i in x:
json.dump(i, fp)
fp.write('\n')
data.query(db).export_json('abc.json')