我想编写一个带有基于T ext的U ser I接口(TUI)的程序,它由几种形式组成.
这是我的尝试,它使用库npyscreen但不返回第一个表单.代码也不包含更改列表项的逻辑.
#! /usr/bin/env python3
# coding:utf8
import npyscreen
# content
headers = ["column 1", "column 2", "column 3", "column 4"]
entries = [["a1", "a2", "a3", "a4"],
["b1", "b2", "b3", "b4"],
["c1", "c2", "c3", "c4"],
["d1", "d2", "d3", "d4"],
["e1", "e2", "e3", "e4"]]
# returns a string in which the segments are padded with spaces.
def format_entry(entry):
return "{:10} | {:10} | {:10} | {:10}".format(entry[0], entry[1] , …Run Code Online (Sandbox Code Playgroud) 如何使用 Python 动态指向 MongoDB 中的特定集合名称?
我从网络中的几十个传感器中的任意一个接收数据,并且我想将原始数据存储到以传感器命名的数据库集合中。
# Server Parameters
host = '1.2.3.4'
port = 27017
client = MongoClient(host, port)
db = client.myDB # use database myDB
# receive data from sensors
# {"sensorName":"...", "x":"...", "y":"...", "z":"...", "time":"..."}
db.SensorA.insert_one({...}) # record raw data in the collection for SensorA
db.SensorB.insert_one({...})
db.SensorC.insert_one({...})
Run Code Online (Sandbox Code Playgroud)
我不想明确地写db.SensorName.insert_one({...}),而是想以某种方式引用给定的传感器/集合名称。
谢谢
我正在尝试npyscreen在 python 中创建一个简单的curses应用程序,该应用程序请求用户在一个屏幕上输入,然后在另一个屏幕上为用户验证它。
大多数情况下,这是为了理解如何在 内部存储和检索值npyscreen。我确信我错过了一些简单的东西,但我无法在文档中找到(或理解?)答案。
下面的示例代码将无法正确传递值:
#!/usr/bin/env python3.5
import npyscreen as np
class EmployeeForm(np.Form):
def afterEditing(self):
self.parentApp.switchForm('CONFIRMFM')
def create(self):
self.myName = self.add(np.TitleText, name='Name')
self.myDepartment = self.add(np.TitleSelectOne, scroll_exit=True, max_height=3, name='Department', values = ['Department 1', 'Department 2', 'Department 3'])
self.myDate = self.add(np.TitleDateCombo, name='Date Employed')
class EmployeeConfirmForm(np.Form):
def afterEditing(self):
self.parentApp.setNextForm(None)
def create(self):
self.value = None
self.wgName = self.add(np.TitleText, name = "Name:",)
self.wgDept = self.add(np.TitleText, name = "Dept:")
self.wgEmp = self.add(np.TitleText, name = "Employed:")
def beforeEditing(self):
if self.value:
self.name = …Run Code Online (Sandbox Code Playgroud) 我试图基本上清除控制台行,但不是在不使用空格的情况下清除整个控制台窗口,这样我就不会从上次打印的内容中获得额外的字符。例如:
# This causes characters from the last thing printed:
print("I don't know you.", end="\r")
print("Hello Jim!", end="\r")
# Yields the following (without the quotations) -->
# "Hello Jim!ow you."
Run Code Online (Sandbox Code Playgroud)
现在要解决这个问题可以这样做:
# This causes characters from the last thing printed:
print("I don't know you.", end="\r")
print("Hello Jim!", end="\r")
# Yields the following (without the quotations) -->
# "Hello Jim!ow you."
Run Code Online (Sandbox Code Playgroud)
我如何
"Hello Jim!"而不是"Hello Jim! "(两者显然都没有引号)具体来说,当改变尺寸时(例如控制台宽度从 17 到 30),控制台中会发生类似的情况,在我的情况下,这种情况经常发生:
Hello Jim! …Run Code Online (Sandbox Code Playgroud)