编辑了 _ Return 个来自 class 的对象
Edited _ Return an object from a class
我重新写了题,因为现在我明白了。
是否可以通过调用 class 来 return 一个对象?举个例子,我正在尝试连接到客户端服务器,只需调用 class,我就可以连接了。
如果我想在该服务器上启动一个新项目,我只需要初始化项目并自动调用连接。
class LabellingProject():
def __init__(self,
web_app=False,
port=8888,
data_dir="/xxxx/xxx/",
local_files_root="/xxxx/datasets",
disable_signup_without_link=True,
username= "xxxx@xxxx"):
self.web_app = web_app
self.port = port
self.data_dir = data_dir
self.local_files_root = local_files_root
self.disable_signup_without_link = disable_signup_without_link
self.username = username
def connection(cls):
"""
"""
config = Config(web_app=cls.web_app,
port = cls.port,
data_dir = cls.data_dir,
local_files_root=cls.local_files_root,
disable_signup_without_link=cls.disable_signup_without_link,
username=cls.username)
server = LabelStudioServer()
server.start(config)
client = Client(url="http://localhost:"+str(config.port),
api_key=config.token)
return client
def create_new(self,client):
"""
"""
project = client.start_project()
return project
我想这样调用函数。
labelling = LabellingProject() => it will return the connection
project = labelling.create_new() => it will return the project and the client it should take it from the __init__.
是不是更懂了?
createFromA
需要是 staticmethod
,因为它不在 class Constructor
的实例上运行
class A:
def __init__(self):
pass
def create_project(self,argA):
newA = self.Constructor.createFromA(argA)
return newA
class Constructor:
def __init__(self):
pass
@staticmethod
def createFromA(argA):
A = 2*argA
return A
cons = A()
cons.create_project(2)
我重新写了题,因为现在我明白了。
是否可以通过调用 class 来 return 一个对象?举个例子,我正在尝试连接到客户端服务器,只需调用 class,我就可以连接了。
如果我想在该服务器上启动一个新项目,我只需要初始化项目并自动调用连接。
class LabellingProject():
def __init__(self,
web_app=False,
port=8888,
data_dir="/xxxx/xxx/",
local_files_root="/xxxx/datasets",
disable_signup_without_link=True,
username= "xxxx@xxxx"):
self.web_app = web_app
self.port = port
self.data_dir = data_dir
self.local_files_root = local_files_root
self.disable_signup_without_link = disable_signup_without_link
self.username = username
def connection(cls):
"""
"""
config = Config(web_app=cls.web_app,
port = cls.port,
data_dir = cls.data_dir,
local_files_root=cls.local_files_root,
disable_signup_without_link=cls.disable_signup_without_link,
username=cls.username)
server = LabelStudioServer()
server.start(config)
client = Client(url="http://localhost:"+str(config.port),
api_key=config.token)
return client
def create_new(self,client):
"""
"""
project = client.start_project()
return project
我想这样调用函数。
labelling = LabellingProject() => it will return the connection
project = labelling.create_new() => it will return the project and the client it should take it from the __init__.
是不是更懂了?
createFromA
需要是 staticmethod
,因为它不在 class Constructor
class A:
def __init__(self):
pass
def create_project(self,argA):
newA = self.Constructor.createFromA(argA)
return newA
class Constructor:
def __init__(self):
pass
@staticmethod
def createFromA(argA):
A = 2*argA
return A
cons = A()
cons.create_project(2)