一个游戏引擎为我提供了一个Player带有steamid属性的类(来自C++,这只是一个关于它在Python中的样子的基本示例):
# game_engine.py
class Player:
def __init__(self, steamid):
self.__steamid = steamid
@property
def steamid(self):
return self.__steamid
Run Code Online (Sandbox Code Playgroud)
然后我在添加gold属性时继续子类化这个类:
# my_plugin.py
class MyPlayer(game_engine.Player, Base):
gold = Column(Integer)
Run Code Online (Sandbox Code Playgroud)
现在我需要将播放器存储gold到数据库中,并将播放器steamid作为识别播放器的主键.如何告诉SQLAlchemy使用基类的steamid属性作为主键?
这是我试过的傻事:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
import game_engine
Base = declarative_base()
class Player(game_engine.Player, Base):
__tablename__ = 'player'
_steamid = game_engine.Player.steamid
@hybrid_property
def steamid(self):
return type(self)._steamid.__get__(self)
Run Code Online (Sandbox Code Playgroud)
但是,这是一个长镜头......
sqlalchemy.exc.ArgumentError: Mapper Mapper|Player|player could not assemble any primary key columns for mapped table …Run Code Online (Sandbox Code Playgroud) 我正在使用psycopg2通过 Python 3 访问 PostgreSQL 数据库,并且我正在尝试进行查询,如果列表不为空,我想选择名称在列表中的所有用户。如果提供的列表为空,我想忽略该条件,即选择所有用户而不考虑他们的姓名。
我已经尝试了以下三个调用:
# Using list
cursor.execute(
"SELECT age FROM user WHERE %(names) = '{}' OR user.name IN %(names)s",
{'names': []},
)
# Using tuple
cursor.execute(
"SELECT age FROM user WHERE %(names) = () OR user.name IN %(names)s",
{'names': ()},
)
# Using both list and tuple
cursor.execute(
"SELECT age FROM user WHERE %(names_l) = '{}' OR user.name IN %(names_t)s",
{'names_l': [], 'names_t': ()},
)
Run Code Online (Sandbox Code Playgroud)
但是它们都从某一点或另一点引发了无效的语法错误:
# Using list
psycopg2.ProgrammingError: syntax error …Run Code Online (Sandbox Code Playgroud) 有没有办法使用 Python (3) 将字符打印到控制台上的某个点?这是我正在努力实现的理想示例:
def print_char(x, y, char):
# Move console cursor to 'x', 'y'
# Set character under cursor to 'char'
Run Code Online (Sandbox Code Playgroud)
我知道在其他一些语言中是可能的,Python 怎么样?我不介意我是否必须使用外部库。
我在 Windows 7 上。
我正在使用PyCharm Community Edition 4.5.4,我讨厌它如何告诉我即使我有完全打算在30秒内修复它的每一个小"错误".
我的风格是一次写入所有内容(而不是在移动到另一个之前完成一件事),因此我的代码中的每个第二个单词都会突出显示为variable 'x' is not used或者Unresolved reference 'x'因为我已经移动到我的代码的其他部分,意图完成之后的for循环.如果我这样做:
for x in my_list:
pass
Run Code Online (Sandbox Code Playgroud)
然后移动到文件顶部定义my_list,它会立即突出显示Local variable 'x' is not used.我希望自由地编写我的整个代码,然后在点击保存后,我想知道我犯了什么错误.
有没有办法禁用PEP8检查器,所以它只会检查我实际保存文件的时间,而不是在我输入任何内容时?
假设我已经有一个带有类型注释的方法:
class Shape:
def area(self) -> float:
raise NotImplementedError
Run Code Online (Sandbox Code Playgroud)
然后我将多次子类化:
class Circle:
def area(self) -> float:
return math.pi * self.radius ** 2
class Rectangle:
def area(self) -> float:
return self.height * self.width
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我复制-> float了很多。假设我有 10 个不同的形状,有多种方法,其中一些也包含参数。有没有办法从父类中“复制”注释,类似于functools.wraps()文档字符串?
我的代码:
import asyncio
async def test(i):
await asyncio.sleep(i)
print('test')
async def main():
await test(2)
await test(2)
await test(2)
asyncio.get_event_loop().run_forever(main())
Run Code Online (Sandbox Code Playgroud)
我原本希望它休眠三秒钟,然后打印'test'三遍,但是要等到每秒钟'test'分别等待2秒钟(所以最后一次'test'打印才6秒钟)。
我有什么不对的地方,怎么解决?
我有一个抽象基类,Animal:
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod
def move(self):
raise NotImplementedError()
@abc.abstractmethod
def eat(self):
raise NotImplementedError()
Run Code Online (Sandbox Code Playgroud)
现在我有另一个 abc 只实现了这些方法之一:
class Bird(Animal):
def move(self):
print("fly")
Run Code Online (Sandbox Code Playgroud)
另一个实现缺失方法的类:
class Eagle(Bird):
def eat(self):
print("eagle eats")
Run Code Online (Sandbox Code Playgroud)
但是 PyCharm 抱怨Bird它“必须实现所有抽象方法”,而我故意希望它保持抽象。
我错过了什么,还是这是一个错误?如果这只是一个错误,我可以以某种方式忽略警告#noqa吗(类似于)?
Swift 有 nil-coalescing 运算符a ?? b,它是a != nil ? a : b. Swift 是否有相反的运算符,即 的简写,a == nil ? a : b或者换句话说,a == nil ? nil : b?
我会用它来将一个可选值映射到其他东西,就像这样:
let x = dict["key"] != nil ? mapValue(dict["key"]) : nil
// ideally: let x = dict["key"] ¿¿ mapValue(dict["key"])
Run Code Online (Sandbox Code Playgroud) 以下是否有任何区别?
print(x if x else 'no x available')
# compared to:
print(x and x or 'no x available')
Run Code Online (Sandbox Code Playgroud) class Spam(object):
__slots__ = ('__dict__',)
Run Code Online (Sandbox Code Playgroud)
生成小于"普通"类的实例.为什么是这样?
资料来源:David Beazley最近发布的推文.
python ×7
python-3.x ×7
pycharm ×2
abc ×1
async-await ×1
asynchronous ×1
class ×1
console ×1
if-statement ×1
inheritance ×1
list ×1
operators ×1
parameters ×1
pep8 ×1
postgresql ×1
printing ×1
properties ×1
psycopg2 ×1
python-3.6 ×1
sqlalchemy ×1
swift ×1
types ×1
where-in ×1