我有两个型号.一个是Subscriber在节省时间我根据已经分配给该池的订户数量来分配池:
class Subscriber(models.Model):
pool = models.ForeignKey(Pool)
interface = models.CharField(max_length=30)
class Pool(models.Model):
name = models.CharField(max_length=50)
Run Code Online (Sandbox Code Playgroud)
假设只有4个Subscribers可以分配给一个池.这就是为什么我要覆盖Subscriber的save()方法:
def save(self, *args, **kwargs):
if not self.pk:
#Look for a free Pool among the available ones
for pool in Pool.objects.all():
if pool.subscriber_set.count() < 4:
self.pool = pool
print "Assigned pool: %s" % pool.name
super(Subscriber, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
它运行良好,直到我用完池(所有这些都分配了4个用户).我应该如何从Django管理员处理这个?理想情况下,我想向用户显示一些错误消息,以便他可以创建更多池.
我宁愿不将池分配代码移动到窗体的clean()方法,因为可能用户也将从不同的界面创建,而不是管理GUI.
有任何想法吗?
非常感谢!
我有一个Python dict,它来自于通常读取YAML文件
yaml.load(stream)
Run Code Online (Sandbox Code Playgroud)
我想以编程方式更新YAML文件给定要更新的路径,如:
1组,选项1,option11,值
并将生成的dict再次保存为yaml文件.考虑到路径是动态的(假设用户能够通过我使用Cmd创建的简单CLI进入路径),我面临更新二项决定的问题.
有任何想法吗?
谢谢!
更新 让我在这个问题上更加具体:问题是更新字典的一部分,我事先并不知道结构.我正在开发一个项目,其中所有配置都存储在YAML文件中,我想添加一个CLI以避免必须手动编辑它们.这是一个示例YAML文件,使用PyYaml加载到字典(config-dict):
config:
a-function: enable
b-function: disable
firewall:
NET:
A:
uplink: enable
downlink: enable
B:
uplink: enable
downlink: enable
subscriber-filter:
cancellation-timer: 180
service:
copy:
DS: enable
remark:
header-remark:
DSC: enable
remark-table:
port:
linkup-debounce: 300
p0:
mode: amode
p1:
mode: bmode
p2:
mode: amode
p3:
mode: bmode
Run Code Online (Sandbox Code Playgroud)
我用Cmd创建了CLI,即使使用自动完成它也能很好地工作.用户可以提供如下行:
config port p1 mode amode
Run Code Online (Sandbox Code Playgroud)
所以,我需要编辑:
config-dict ['config'] ['port'] ['p1'] ['mode']并将其设置为'amode'.然后,使用yaml.dump()再次创建该文件.另一条可能的路线是:
config a-function enable
Run Code Online (Sandbox Code Playgroud)
所以config-dict ['config'] ['a-function']必须设置为'enable'.
我的问题是更新字典时.如果Python传递值作为引用将很容易:只需遍历dict直到找到正确的值并保存它.实际上这就是我为Cmd自动完成所做的事情.但我不知道如何进行更新.
希望我现在更好地解释自己!
提前致谢.
我在停止正在执行阻塞操作的线程时遇到问题。我正在编写一个使用gpsd及其python绑定的程序,该Thread的run方法如下所示:
def run(self):
print "inside run. outside while"
global gpsd
while self.running:
print "inside while"
try:
print "before next()"
gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
print "after next()"
self.file_descriptor.write(str(int(time.time())) + ',' + str(gpsd.fix.latitude) + ',' + str(gpsd.fix.longitude) + ',' + str(gpsd.fix.altitude) + ',' + str(gpsd.fix.speed) + '\n')
print "after write"
#self.file_descriptor.write("self.running" + str(self.running) + '\n')
self.file_descriptor.flush()
print "after flush"
time.sleep(5)
print "after sleep"
except:
print "inside thread except"
raise …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Ansible设置我的sshd_config文件.出于设计原因(我们有虚拟机和物理机,有不同的NIC)管理界面没有修复,所以在我的模板中我不能放
{{ ansible_eth0.ipv4.address }}
Run Code Online (Sandbox Code Playgroud)
因为我事先不知道哪个接口是管理接口所以我需要发现它.我知道IP总是在一个范围内(例如192.168.1.0/24).
如何发现哪个接口具有该IP,然后在模板中使用它?