Skip to main content
 首页 » 编程设计

Python 类之存储和引用实例

2024年11月24日9傻小

我正在尝试创建一个类,其中包含:命名空间、文件路径和选择集数据(列表)数据。

我有一个 UI,可以让我为输入的每个字符添加一条新的“记录”。

到目前为止我得到了这个:

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' 然后找到 namespace 、文件路径和选择集数据?

抱歉,如果我的术语在某些方面有误!

干杯。

请您参考如下方法:

虽然它通常用作成熟的网站框架,但 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")