RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

全部问题

Martin Hope
QuL1ux
Asked: 2025-01-15 01:50:53 +0000 UTC

aiogram 中的个人临时处理程序

  • 5

我正在尝试使用课堂上的按钮制作一个持续时间选择功能,例如 1 分钟、2 小时等,事实是机器人可以同时由几个人使用,或者 1 个人使用此功能两次,然后我在课程开始时注册的所有处理程序,他们只是开始感到困惑。由于处理程序在第一次使用后已经注册,因此在第二次使用期间不会注册它们,因此,机器人将编辑第一条消息,而不是第二条消息。我尝试依靠处理程序中的user_id指向callback_data,但是当一个人使用两次时也毫无意义,我id什至制作了自己的生成器,这也失败了,我不知道为什么。完成持续时间的选择后删除处理程序是没有意义的,因为它们也将从第二个处理程序中删除。

这是我的代码:

def generate_unique_id(length=10):
    return ''.join(choices(ascii_letters + digits, k=length))

class DurationSelector:
    def __init__(self, msg: types.Message, user_id: int = None):
        self.user_id = user_id
        self.uniq_id = generate_unique_id()
        self.message = msg
        self.num = 1
        self.ind_init = 0
        self.max_nums = {0: 60, 1: 24, 2: 30, 3: 4, 4: 12, 5: 1}
        self.max_ind = 5
        self.forms = [["минута", "минуты", "минут"], ["час", "часа", "часов"], ["день", "дня", "дней"], ["неделя", "недели", "недель"], ["месяц", "месяца", "месяцев"], ["год", "года", "лет"]]
        self.max = self.max_nums[self.ind_init]

    def format_number(self, n, forms):
        n = abs(n) % 100
        if 11 <= n <= 19:
            form = forms[2]
        else:
            n = n % 10
            if n == 1:
                form = forms[0]
            elif 2 <= n <= 4:
                form = forms[1]
            else:
                form = forms[2]
        return form

    def get_uniq(self, action: str):
        print(f"{action}_{self.uniq_id}")
        return f"{action}_{self.uniq_id}"

    async def config_nums(self):
        self.max = self.max_nums[self.ind_init]
        if self.num > self.max:
            self.num = self.max
        if self.num < 1:
            self.num = 1
        await self.next()

    def get_keyboard(self) -> InlineKeyboardMarkup:
        keyboard = [
            [get_inline_button('dur-5', id=self.uniq_id) if self.num > 1 else get_inline_button('none_button'),
             get_inline_button('dur-1', id=self.uniq_id) if self.num > 1 else get_inline_button('none_button'),
             InlineKeyboardButton(text=str(self.num), callback_data='none'),
             get_inline_button('dur+1', id=self.uniq_id) if self.num < self.max else get_inline_button('none_button'),
             get_inline_button('dur+5', id=self.uniq_id) if self.num < self.max else get_inline_button('none_button')],
            [get_inline_button('unit-1', id=self.uniq_id) if self.ind_init > 0 else get_inline_button('none_button'),
             InlineKeyboardButton(text=self.format_number(self.num, self.forms[self.ind_init]), callback_data='none'),
             get_inline_button('unit+1', id=self.uniq_id) if self.ind_init < self.max_ind else get_inline_button('none_button')]]
        return InlineKeyboardMarkup(inline_keyboard=keyboard)

    async def start(self):
        private.callback_query.register(self.inc, F.data == self.get_uniq("dur+1"), AddProduct.duration)
        private.callback_query.register(self.dec, F.data == self.get_uniq("dur-1"), AddProduct.duration)
        private.callback_query.register(self.inc5, F.data == self.get_uniq("dur+5"), AddProduct.duration)
        private.callback_query.register(self.dec5, F.data == self.get_uniq("dur-5"), AddProduct.duration)
        private.callback_query.register(self.inc_unit, F.data == self.get_uniq("unit+1"), AddProduct.duration)
        private.callback_query.register(self.dec_unit, F.data == self.get_uniq("unit-1"), AddProduct.duration)
        private.callback_query.register(self.other, AddProduct.duration)
        await self.next()

    async def end(self):
        ...

    async def next(self):
        await self.message.edit_text(**get_text('duration_select', num=self.num, form=self.format_number(self.num, self.forms[self.ind_init])).as_kwargs(), reply_markup=self.get_keyboard())

    async def dec(self, clbck: types.CallbackQuery, state: FSMContext):
        self.num -= 1
        await self.config_nums()
    
    async def inc(self, clbck: types.CallbackQuery, state: FSMContext):
        self.num += 1
        await self.config_nums()
    
    async def dec5(self, clbck: types.CallbackQuery, state: FSMContext):
        self.num -= 5
        await self.config_nums()
    
    async def inc5(self, clbck: types.CallbackQuery, state: FSMContext):
        self.num += 5
        await self.config_nums()

    async def dec_unit(self, clbck: types.CallbackQuery, state: FSMContext):
        self.ind_init -= 1
        await self.config_nums()

    async def inc_unit(self, clbck: types.CallbackQuery, state: FSMContext):
        self.ind_init += 1
        await self.config_nums()

    async def other(self, clbck: types.CallbackQuery, state: FSMContext):
        print('other', clbck.data)

目标:纠正错误,以便几个人可以同时进行选择,或者一个人两次。

python
  • 1 个回答
  • 36 Views
Martin Hope
Steven Kirke
Asked: 2025-01-14 23:09:56 +0000 UTC

SQL 中的自定义方法

  • 6

告诉我如何创建自己的方法将全名组装成姓氏和首字母缩写?只是一个返回计算属性和初始化的示例。我感兴趣的是编写函数的语法。

方法示例:

CREATE FUNCTION assemblerFullname (@Fullname VARCHAR(MAX))
    DECLARE @first_name VARCAR(50) = TRIM(SUBSTRING(@Fullname, 1, CHARINDEX(' ', @Fullname)))
RETURNS @first_name

一个请求,其中有两个带有全名的字段(Assigner.fullname,Assigner.fullname),假设在字段输出中的 SELECT 中,可以添加一个将全名处理到结果中的方法 I需要。

SELECT 
        ts.id,
        ts.name,
        ts.assigner_id,
        assigner.fullname,
        ts.executor_id,
        assigner.fullname,
        ts.status,
        ts.value,
        ts.date_plan,
        ts.end_date_plan,
        role.value('(/task/plan)[1]', 'VARCHAR(MAX)') AS description
    FROM tasks AS ts
    LEFT JOIN task AS t ON t.id = ts.id
    LEFT JOIN collaborators AS assigner ON assigner.id = ts.assigner_id
    LEFT JOIN collaborators AS executor ON executor.id = ts.executor_id
    CROSS APPLY t.data.nodes('/task/role_id') AS field(role)
    WHERE role.value('(/task/role_id)[1]', 'BIGINT') = 7459373706232529779
sql
  • 1 个回答
  • 37 Views
Martin Hope
Юрий Дядык
Asked: 2025-01-14 22:16:11 +0000 UTC

Google 表格中的条件格式

  • 5

如果另一个选项卡上存在电话号码,如何以颜色突出显示带有电话号码的单元格?

我将其应用于范围E2:E10775。

由于某种原因,这个公式不起作用:

=ЕСЛИ(СЧЁТЕСЛИ('Лист1'!$A$1:$A$10; E2) > 0; ИСТИНА; ЛОЖЬ)

google-spreadsheet
  • 1 个回答
  • 41 Views
Martin Hope
Стас Палыч
Asked: 2025-01-14 21:43:50 +0000 UTC

如何将堆上的一个变量绑定到另一个变量

  • 3

有必要HWND以HMODULE这样的方式进行绑定:有了模块,我们就可以接收窗口句柄。大约有100对这样的人,cpp中有这样的机会吗?带有数组的选项将不起作用。

例如,在 AutoIt 中,我可以简单地创建一个全局变量,其名称将由HWND-的字符串格式形成"0x0000FFFFF",并通过其名称检索该变量的值。

cpp中有类型:链接、指针……,这里还有更多的可能性。我对积极的一面知之甚少。请举个例子。

c++
  • 1 个回答
  • 57 Views
Martin Hope
Andrey Altukhov
Asked: 2025-01-14 21:28:30 +0000 UTC

当端口中有数据时从 COM 端口读取数据

  • 6

大家下午好。您需要从一个设备接收数据。设备仅在向其发送命令后才产生数据。我们发送命令并接收几行响应(根据设备设置,接收的行数可能会有所不同)。从程序发送命令后,设备生成行,程序读取它们并冻结。那些。无法进一步处理接收到的数据并正常释放COM口。未经编辑的草稿代码:

import serial.tools.list_ports
#
#ports = serial.tools.list_ports.comports()
#
#for port in ports:
#    print(port.device)

try:
    # Find and open the COM port
    ports = serial.tools.list_ports.comports()
    port = next((p.device for p in ports), None)
    print("Select port",port)
    if port is None:
        raise ValueError("No COM port found.")

    ser = serial.Serial(port, baudrate=19200)
    ser.flushInput()
    ser.flushOutput()
    serial_command=b"T LIST\r\n" #отправляем команду на получение данных
    ser.write(serial_command)
    data=""
    while str(data)!="": # Читаем данные их порта
        data=ser.readline()
        print(data)
    ser.flushInput()
    ser.flushOutput()

    # Perform operations on the COM port

    ser.close()  # Close the connection when done
    print("Finish!")

except ValueError as ve:
    print("Error:", str(ve))

except serial.SerialException as se:
    print("Serial port error:", str(se))

except Exception as e:
    print("An error occurred:", str(e))

结果是一个流:

b'T  014:18:15  0002  RANGE=1000.0 UGM\r\n'
b'T  014:18:15  0002  STABIL=0.0 UGM\r\n'
b'T  014:18:15  0002  PRES=30.5 IN-HG-A\r\n'
b'T  014:18:15  0002  SAMP FL=0 CC/M\r\n'
b'T  014:18:15  0002  PMT=13.7 MV\r\n'
b'T  014:18:15  0002  NORM PMT=16.0 MV\r\n'
b'T  014:18:15  0002  UV LAMP=3364.4 MV\r\n'
b'T  014:18:15  0002  LAMP RATIO=95.5 %\r\n'
b'T  014:18:15  0002  STR. LGT=22.1 UGM\r\n'
b'T  014:18:15  0002  DRK PMT=40.9 MV\r\n'
b'T  014:18:15  0002  DRK LMP=-1.1 MV\r\n'
b'T  014:18:15  0002  SLOPE=1.000\r\n'
b'T  014:18:15  0002  OFFSET=15.5 MV\r\n'
b'T  014:18:15  0002  HVPS=670 VOLTS\r\n'
b'T  014:18:15  0002  RCELL TEMP=50.0 C\r\n'
b'T  014:18:15  0002  BOX TEMP=31.6 C\r\n'
b'T  014:18:15  0002  PMT TEMP=8.2 C\r\n'
b'T  014:18:15  0002  TIME=18:15:13\r\n'

如果serial.Serial(port, baudrate=19200)将 timeout=1 添加到该行,程序将开始无休止地读取数据。那些。在输出中我们有:

b''
b''
...

接收数据后必须释放端口,以便可以处理数据。我就是不知道插头在哪里。

python
  • 1 个回答
  • 33 Views
上一页
下一页

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5