Python class - 存储和引用实例
Python class - storing and referring back to the instances
我正在尝试创建一个 class,其中包含:名称空间、文件路径和选择集数据(列表)数据。
我有一个 UI 可以让我为输入的每个字符添加一个新的 'record'。
到目前为止我得到了这个:
mainlist = []
class chRecord:
def __init__(self, namespace, filepath, selSets =[]):
self.namespace = namespace
self.filepath = filepath
self.selSets = selSets
aName = "John:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk 0-10,
animation:bob_jeff 10-30"
characterRecord = chRecord(aName,aAge,aSelSets)
mainlist.append(characterRecord)
aName = "John2:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk2 0-10,
animation:bob_jeff2 10-30"
characterRecord = chRecord(aName,aAge,aSelSets)
mainlist.append(characterRecord)
我的问题是如何搜索 mainList
以找到我要查找的记录。 IE。 'John'然后找到命名空间、文件路径和选择集数据?
抱歉,如果我的术语在某些方面有误!
干杯。
您需要创建一个函数,将记录列表作为参数并按名称进行搜索。然后它将 return 匹配的记录,您将能够从中获得附加信息。
示例:
mainlist = []
def find_record_by_name(records, name):
for record in records:
if record.namespace == name:
return record
class chRecord:
def __init__(self, namespace, filepath, selSets=[]):
self.namespace = namespace
self.filepath = filepath
self.selSets = selSets
aName = "John:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk 0-10, animation:bob_jeff 10-30"
characterRecord = chRecord(aName, aAge, aSelSets)
mainlist.append(characterRecord)
aName = "John2:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk2 0-10,animation:bob_jeff2 10-30"
characterRecord = chRecord(aName, aAge, aSelSets)
mainlist.append(characterRecord)
selected_record = find_record_by_name(mainlist, "John:")
if selected_record:
print(selected_record.namespace)
print(selected_record.filepath)
print(selected_record.selSets)
浏览列表并匹配 namespace
属性。
for record in mainlist:
if record.namespace == 'John':
# the record is found, now you can access the attributes with `record.filepath`
print(record)
虽然它通常用作成熟的网站框架,但 Django model 与您在此处尝试执行的有关过滤的操作非常接近:
from django.db import models
class CHRecord(models.Model):
namespace = models.CharField(max_length=32)
filepath = models.FileField()
# Not sure what this represents in your question, but if you use
# PostgreSQL as your backend, might want an Array
sel_sets = models.CharField(max_length=512)
# Saves a row to a database (local SQLite by default)
CHRecord.objects.create(namespace="John:", filepath="C:/temp/", sel_sets="xyz")
# Get matching rows translating to a SQL query
CHRecord.objects.filter(namespace__contains="John")
如果您需要强制使用唯一值,您可以使用字典。只需将键设置为唯一值或应该唯一的值。
这应该 运行 比循环更快,但如果键不是唯一的,则会覆盖数据。
mainlist = {}
class chRecord:
def __init__(self, namespace, filepath, selSets =[]):
self.namespace = namespace
self.filepath = filepath
self.selSets = selSets
aName = "John:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk 0-10, animation:bob_jeff 10-30"
mainlist[aName] = chRecord(aName,aAge,aSelSets)
print(mainlist[aName].selSets)
aName = "John2:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk2 0-10, animation:bob_jeff2 10-30"
mainlist[aName] = chRecord(aName,aAge,aSelSets)
print(mainlist.get('John2:').namespace)
print(mainlist.get('John2:').filepath)
print(mainlist.get('John2:').selSets)
我正在尝试创建一个 class,其中包含:名称空间、文件路径和选择集数据(列表)数据。
我有一个 UI 可以让我为输入的每个字符添加一个新的 'record'。
到目前为止我得到了这个:
mainlist = []
class chRecord:
def __init__(self, namespace, filepath, selSets =[]):
self.namespace = namespace
self.filepath = filepath
self.selSets = selSets
aName = "John:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk 0-10,
animation:bob_jeff 10-30"
characterRecord = chRecord(aName,aAge,aSelSets)
mainlist.append(characterRecord)
aName = "John2:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk2 0-10,
animation:bob_jeff2 10-30"
characterRecord = chRecord(aName,aAge,aSelSets)
mainlist.append(characterRecord)
我的问题是如何搜索 mainList
以找到我要查找的记录。 IE。 'John'然后找到命名空间、文件路径和选择集数据?
抱歉,如果我的术语在某些方面有误!
干杯。
您需要创建一个函数,将记录列表作为参数并按名称进行搜索。然后它将 return 匹配的记录,您将能够从中获得附加信息。
示例:
mainlist = []
def find_record_by_name(records, name):
for record in records:
if record.namespace == name:
return record
class chRecord:
def __init__(self, namespace, filepath, selSets=[]):
self.namespace = namespace
self.filepath = filepath
self.selSets = selSets
aName = "John:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk 0-10, animation:bob_jeff 10-30"
characterRecord = chRecord(aName, aAge, aSelSets)
mainlist.append(characterRecord)
aName = "John2:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk2 0-10,animation:bob_jeff2 10-30"
characterRecord = chRecord(aName, aAge, aSelSets)
mainlist.append(characterRecord)
selected_record = find_record_by_name(mainlist, "John:")
if selected_record:
print(selected_record.namespace)
print(selected_record.filepath)
print(selected_record.selSets)
浏览列表并匹配 namespace
属性。
for record in mainlist:
if record.namespace == 'John':
# the record is found, now you can access the attributes with `record.filepath`
print(record)
虽然它通常用作成熟的网站框架,但 Django model 与您在此处尝试执行的有关过滤的操作非常接近:
from django.db import models
class CHRecord(models.Model):
namespace = models.CharField(max_length=32)
filepath = models.FileField()
# Not sure what this represents in your question, but if you use
# PostgreSQL as your backend, might want an Array
sel_sets = models.CharField(max_length=512)
# Saves a row to a database (local SQLite by default)
CHRecord.objects.create(namespace="John:", filepath="C:/temp/", sel_sets="xyz")
# Get matching rows translating to a SQL query
CHRecord.objects.filter(namespace__contains="John")
如果您需要强制使用唯一值,您可以使用字典。只需将键设置为唯一值或应该唯一的值。
这应该 运行 比循环更快,但如果键不是唯一的,则会覆盖数据。
mainlist = {}
class chRecord:
def __init__(self, namespace, filepath, selSets =[]):
self.namespace = namespace
self.filepath = filepath
self.selSets = selSets
aName = "John:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk 0-10, animation:bob_jeff 10-30"
mainlist[aName] = chRecord(aName,aAge,aSelSets)
print(mainlist[aName].selSets)
aName = "John2:"
aAge = "C:/temp/"
aSelSets = "Animation:stuff_Junk2 0-10, animation:bob_jeff2 10-30"
mainlist[aName] = chRecord(aName,aAge,aSelSets)
print(mainlist.get('John2:').namespace)
print(mainlist.get('John2:').filepath)
print(mainlist.get('John2:').selSets)