我有一个dataclass对象,其中包含嵌套的数据类对象.但是,当我创建主对象时,嵌套对象变成了字典:
@dataclass
class One:
f_one: int
@dataclass
class One:
f_one: int
f_two: str
@dataclass
class Two:
f_three: str
f_four: One
data = {'f_three': 'three', 'f_four': {'f_one': 1, 'f_two': 'two'}}
two = Two(**data)
two
Two(f_three='three', f_four={'f_one': 1, 'f_two': 'two'})
obj = {'f_three': 'three', 'f_four': One(**{'f_one': 1, 'f_two': 'two'})}
two_2 = Two(**data)
two_2
Two(f_three='three', f_four={'f_one': 1, 'f_two': 'two'})
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我试图将所有数据作为字典传递,但我没有得到预期的结果.然后我尝试首先构造嵌套对象并将其传递给对象构造函数,但我得到了相同的结果.
理想情况下,我想构建我的对象来得到这样的东西:
Two(f_three='three', f_four=One(f_one=1, f_two='two'))
Run Code Online (Sandbox Code Playgroud)
除了手动将嵌套字典转换为相应的数据类对象,每当访问对象属性时,有没有办法实现其他目的?
提前致谢.
我正在使用下面的正则表达式来验证电子邮件地址。
/^\w+([\.-]?\w+)*@\w+([\.-]?w+)*(\.\w{2,3})+$/
Run Code Online (Sandbox Code Playgroud)
JavaScript代码:
var email = 'myname@company.com';
var pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?w+)*(\.\w{2,3})+$/;
if(pattern.test(email)){
return true;
}
Run Code Online (Sandbox Code Playgroud)
当我提供以下无效电子邮件时,正则表达式会很快进行评估:
aseflj#$kajsdfklasjdfklasjdfklasdfjklasdjfaklsdfjaklsdjfaklsfaksdjfkasdasdklfjaskldfjjdkfaklsdfjlak@company.com
Run Code Online (Sandbox Code Playgroud)
(我#$在名称中间添加了)
但是,当我尝试评估此电子邮件时,它花费了太多时间,并且浏览器挂起。
asefljkajsdfklasjdfklasjdfklasdfjklasdjfaklsdfjaklsdjfaklsfaksdjfkasdasdklfjaskldfjjdkfaklsdfjlak@company.com1
Run Code Online (Sandbox Code Playgroud)
(我最后补充com1)
我确定正则表达式是正确的,但不确定为什么要花这么多时间来评估第二个示例。如果我提供较短的电子邮件,它将很快得到评估。请参阅以下示例
dfjjdkfaklsdfjlak@company.com1
Run Code Online (Sandbox Code Playgroud)
请帮助我解决性能问题
如何使命令不区分大小写,而不为不同的大小写添加许多别名,例如:
@bot.command(pass_context = True, name = 'test', aliases=['Test', 'tEst', 'teSt', 'tesT', 'TEst', 'TeSt', 'TesT', 'tESt', 'tEsT'])
async def test(self, ctx):
#do stuff
Run Code Online (Sandbox Code Playgroud) 我试图导出id_from的默认值name,反之亦然。
@dataclass
class Item:
id_ = NAME_TO_ID[name]
name = ID_TO_NAME[id_]
Run Code Online (Sandbox Code Playgroud)
我应该能够像这样调用该类:
Item(id_=123)
Item(name='foo')
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我还希望类在提供id_和时引发错误。name
Item(id_=123, name='foo') # ValueError: id_ and name cannot be provided together
Run Code Online (Sandbox Code Playgroud)
关于我应该如何去做这件事有什么建议吗?
我试图在重写时获取我的 Discord 机器人中所有命令的列表。我正在使用 Python 3.6 来编写这个
我试图通过这样做来打印命令列表
print(bot.commands)
这只为我提供了以下返回:
{<discord.ext.commands.core.Command object at 0x00000209EE6AD4E0>, <discord.ext.commands.core.Command object at 0x00000209EE6AD470>}
我希望通常的输出是clear(),因为这是迄今为止我在机器人中编程的唯一命令,该命令按预期工作。但它只打印上面的
我正在尝试使用 Python 创建一个 Discord 机器人,但是每当我在这里运行示例代码时:
import discord
client = discord.Client()
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run('tokenhere')
Run Code Online (Sandbox Code Playgroud)
它返回错误:
Traceback (most recent call last):
File "<ipython-input-6-ea5a13e5703d>", line 1, in <module>
runfile('C:/Users/User/Pictures/rito_bot.py', wdir='C:/Users/User/Pictures')
File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 703, in runfile
execfile(filename, namespace)
File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, …Run Code Online (Sandbox Code Playgroud) 基本上,一切似乎都可以正常运行并启动,但是由于某些原因,我无法调用任何命令。我已经很轻松地环顾了一个小时,然后浏览示例/观看视频,但我终生无法找出问题所在。代码如下:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
@bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
@bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
Run Code Online (Sandbox Code Playgroud)
我在on_message中拥有的调试输出实际上可以正常工作并做出响应,并且整个bot都可以运行,没有任何异常,但是它不会调用命令。
我试图让我的不和谐机器人向我的不和谐服务器发送一个 jpg 文件,但我不断收到一个错误,这似乎很不常见,因为我在互联网上找不到任何解决方案......
错误是... discord.ext.commands.errors.CommandInvokeError:命令引发异常:ClientRequestError:无法为https://discordapp.com/api/v6/channels/454374995758678029/messages编写请求正文
我的进口是
import time
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
Run Code Online (Sandbox Code Playgroud)
我将提取的与错误相关的代码是
@bot.command(pass_context = True)
async def image(ctx):
await bot.send_file(ctx.message.channel, open('halogen.jpg'))
Run Code Online (Sandbox Code Playgroud)
我只是缺少导入还是我的代码存在实际问题?
谢谢你们
这是我在python 3.6中的代码
class A(object)
def __init__(self, a: str):
self._int_a: int = int(a) # desired composition
def get_int_a(self) -> int:
return self._int_a
Run Code Online (Sandbox Code Playgroud)
我想重写这段代码python 3.7,我如何self._int_a: int = int(a)用dataclasses模块初始化?
我知道我可以做类似的事情,但我不知道如何初始化_a: int = int(a)或类似。
from dataclasses import dataclass
@dataclass
class A(object):
_a: int = int(a) # i want to get initialized `int` object here
def get_int_a(self) -> int:
return self._a
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的想法和建议。
我正在寻找最美丽和简短的方法来增加这些类型的列表:
a = [True, False, True, False, False]
b = [100, 200]
Run Code Online (Sandbox Code Playgroud)
的长度b 等于TRUE元素的数目在一个
我需要的答案就[100, 0, 200, 0, 0]在这里
有没有简单的方法来得到答案?
它看起来像元素乘法,但事实是第二个列表的大小较小,因此常见的numpy方法在没有错误代码的情况下不起作用.
希望,你会找到很好的解决方案
python ×9
discord.py ×5
discord ×4
python-3.7 ×2
python-3.x ×2
bots ×1
email-client ×1
javascript ×1
jquery ×1
nested ×1
numpy ×1
object ×1
regex ×1
spyder ×1