我的程序中需要一个临时表.我已经看到这可以通过这种方式使用"mapper"语法实现:
t = Table(
't', metadata,
Column('id', Integer, primary_key=True),
# ...
prefixes=['TEMPORARY'],
)
Run Code Online (Sandbox Code Playgroud)
看到这里
但是,我的整个代码都使用了声明性基础,这是我所理解的,我想坚持下去.有可能使用混合方法, 但如果可能的话我会避免它.
这是我的声明性类的简化版本:
import SQLAlchemy as alc
class Tempo(Base):
"""
Class for temporary table used to process data coming from xlsx
@param Base Declarative Base
"""
# TODO: make it completely temporary
__tablename__ = 'tempo'
drw = alc.Column(alc.String)
date = alc.Column(alc.Date)
check_number = alc.Column(alc.Integer)
Run Code Online (Sandbox Code Playgroud)
提前致谢!
编辑新问题:
现在这个类看起来像这样:
import SQLAlchemy as alc
class Tempo(Base):
"""
Class for temporary table used to process data coming from …Run Code Online (Sandbox Code Playgroud) 我的问题如下。来自网络背景,我没有问题可以做到这一点,但在 Python 桌面应用程序中,我真的看不出什么是根据 MVC 模式组织代码的最佳方式。
我想根据用户输入创建一个窗口,当按下按钮时,它会显示数据库中可用的类似条目。窗户是我的看法。
所以基本上这些是关系:

1)通讯控制器 --> 查看
控制器有一个视图实例,可以使用它公开的方法,例如 view.show_data()。我认为这是要走的路。
# Controller
my_view = View()
...
my_view.show_data(whatever_data)
Run Code Online (Sandbox Code Playgroud)
2)通讯视图--> 控制器
当用户插入一些文本时,必须触发控制器中的一个方法,以便它可以向模型询问数据库中的必要数据。问题是我不知道视图告诉控制器它必须触发那个方法的最佳方式是什么。
我的第一个想法是将控制器的引用传递给视图,并在视图上绑定事件,如下所示:
# Controller
my_view = View(self)
my_model = Model()
...
def on_user_input(self):
# process the input
user_input = ...
self.my_model.method_to_get_info(user_input)
Run Code Online (Sandbox Code Playgroud)
和观点:
# View
def __init__(self, controller):
self.controller_reference = controller
self.launch_gui()
self.config_binds()
def launch_gui(self):
# ... configure all the GUI itself
self.button = ...
def config_binds(self):
self.button.Bind(wx.EVT_BUTTON, self.controller_reference.on_user_input())
Run Code Online (Sandbox Code Playgroud)
但我认为这种“闭环”关系并不是一个非常干净的解决方案。视图在控制器中被引用,控制器在视图中被引用。我认为它在视图和控制器之间建立了紧密的关系。
有什么方法可以做到这一点?
我正试图在字符串中加入两对多对话的关联.在这个例子中,每个团队都有不确定数量的颜色和一个未确定的数字赢得奖项.
这是架构:

这是我正在使用的查询:
SELECT
teams.name AS name,
GROUP_CONCAT(colours.name) AS colours,
GROUP_CONCAT(awards.name) AS awards
FROM
teams
-- join colours
INNER JOIN teams_to_colours
ON teams.id = teams_to_colours.team_id
INNER JOIN colours
ON teams_to_colours.colour_id = colours.id
-- join awards
INNER JOIN teams_to_awards
ON teams.id = teams_to_awards.team_id
INNER JOIN awards
ON teams_to_awards.award_id = awards.id
WHERE
teams.name="A-Team"
GROUP BY
teams.id
Run Code Online (Sandbox Code Playgroud)
问题在于颜色和奖励是重复的.让我们说A-Team有红色和蓝色作为颜色,作为奖项TrollAward和DarwinAward ......我从SQL得到的结果如下:
name: "A-Team"
colours: "red,blue,red,blue"
awards: "TrollAward,DarwinAward,TrollAward,DarwinAward"
Run Code Online (Sandbox Code Playgroud)
我试图只加入一个多对多,并且完美地工作,所以我想我正在监督多个连接的东西......
我有以下问题:我正在设计一个游戏并总结让我们说我有三个类:
一些"用例"
我能让这个工作的唯一方法是,玩家知道PowerPlants和Units,但每个PowerPlant和Unit都知道他的玩家/所有者,这样他们就能以两种方式进行交流.
我不知何故认为这是一种代码味道......当我遇到类似的情况时,我总是遇到麻烦.
提前致谢.
我无法在IPython中加载python模块,该模块在普通解释器中工作正常.我已经分析了问题,并且不知何故IPython没有找到模块,而标准控制台则:
这适用于普通的解释器:
>>> import sys
>>> sys.path.append(r'c:\development\...\ns.package-10.1.0.3-py2.7.egg')
>>> from ns import package
>>>
Run Code Online (Sandbox Code Playgroud)
但是在IPython上它没有:
In [2]: import sys
In [3]: sys.path.append(r'c:\development\...\ns.package-10.1.0.3-py2.7.egg')
In [4]: from ns import package
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-c019e2988e33> in <module>()
----> 1 from ns import package
ImportError: cannot import name package
Run Code Online (Sandbox Code Playgroud)
我发现这很令人困惑.我是IPython的新手,我不知道从哪里开始.提前致谢.
我正在cygwin终端玩鱼和我的鱼.它工作正常,直到我试图打开VI键绑定通过设置fish_vi_key_bindings我的config.fish.
那没起效.显然这只能从版本2.2.x开始,而在cygwin上我正在运行该版本2.1.1.好的,没问题......让我们通过删除该行来停用它config.fish.目前这个文件看起来像这样:
# Path to your oh-my-fish.
set fish_path $HOME/.oh-my-fish
# Path to your custom folder (default path is ~/.oh-my-fish/custom)
#set fish_custom $HOME/dotfiles/oh-my-fish
# Load oh-my-fish configuration.
. $fish_path/oh-my-fish.fish
# Custom plugins and themes may be added to ~/.oh-my-fish/custom
# Plugins and themes can be found at https://github.com/oh-my-fish/
Theme 'robbyrussell'
Plugin 'theme'
Run Code Online (Sandbox Code Playgroud)
但无论如何,当我启动鱼控制台时,我收到以下消息:
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
Standard …Run Code Online (Sandbox Code Playgroud) 我正在看内置Python异常的层次,我注意到StopIteration并GeneratorExit有不同的基类:
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StandardError
+-- Warning
Run Code Online (Sandbox Code Playgroud)
或者在代码中:
>>> GeneratorExit.__bases__
(<type 'exceptions.BaseException'>,)
>>> StopIteration.__bases__
(<type 'exceptions.Exception'>,)
Run Code Online (Sandbox Code Playgroud)
当我转到每个例外的具体描述时,我可以阅读以下内容:
https://docs.python.org/2/library/exceptions.html#exceptions.GeneratorExit
例外GeneratorExit
调用生成器的close()方法时引发.它直接继承自BaseException而不是StandardError,因为它在技术上不是错误.
https://docs.python.org/2/library/exceptions.html#exceptions.StopIteration
例外StopIteration
由迭代器的next()方法引发,表示没有其他值.这是从Exception而不是StandardError派生的,因为这在其正常应用程序中不被视为错误.
这对我来说不是很清楚.两者在它们不通知错误的意义上是相似的,而是用于改变代码流的"事件".因此,它们在技术上不是错误,我理解它们应该与其余的异常分开......但为什么一个是子类,BaseException另一个是Exception?的子类.
一般来说,我一直认为Exception子类是错误的,当我写盲人try: except:(例如调用第三方代码)时,我总是试图捕捉Exception,但也许这是错误的,我应该抓住StandardError.
我正在编写一个通用类装饰器,它需要将装饰器应用于每个方法。我的第一个方法是这样的:
def class_decorator(cls):
for name, member in vars(cls).items():
# Ignore anything that is not a method
if not isinstance(member, (types.FunctionType, types.BuiltinFunctionType, classmethod, staticmethod)):
continue
setattr(cls, name, method_decorator(member))
return cls
Run Code Online (Sandbox Code Playgroud)
装饰器本身并不是很重要。看起来像这样:
def method_decorator(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
# do something
return fn(*args, **kwargs):
return wrapper
Run Code Online (Sandbox Code Playgroud)
一旦我测试了它,我遇到了一个问题,这不适用于静态或类方法,并且引发以下错误functools.wraps:
AttributeError: 'classmethod' object has no attribute '__module__'
Run Code Online (Sandbox Code Playgroud)
是的,classmethod或者staticmethods不是普通函数,甚至不是可调用函数。一般来说,如果你需要装饰一个classmethod,你首先应用你的装饰器,然后应用classmethod装饰器,但由于这是一个类装饰器,我无法影响装饰器的顺序。
对此有什么好的解决办法吗?
我试图更好地理解依赖注入模式,我遇到了是否应该注入记录器的问题。
我使用的语言是 Python,但这个概念是不可知的。我现在所做的只是将单例记录器模块导入到作用域中,并在必要时调用它。例如:
import logger
class Television(object):
def __init__(self, colour, inches):
self._colour = colour
self._inches = inches
def turn_on(self):
# do something
logger.message('TV has been turned on')
Run Code Online (Sandbox Code Playgroud)
如果我使用依赖注入,我会做这样的事情:
import logger
class Television(object):
def __init__(self, colour, inches, logger):
self._colour = colour
self._inches = inches
self._logger = logger
def turn_on(self):
# do something
self._logger.message('TV has been turned on')
Run Code Online (Sandbox Code Playgroud)
这使得代码更易于测试(尽管 python 是如此动态,以至于在测试时您可以实时模拟模块中的记录器)。
缺点是我必须通过使用替代构造函数、一些注入框架或服务定位器来处理注入。我不太确定这种努力是否有回报。
我看过其他类似的问题,但我无法真正理解它们,或者答案没有让我满意:
我有一个混合WithAutoNumbering类,用于需要给定属性的特殊编号的类。除此之外,我有一个很好的类混合调用,WithIndexing用于需要索引功能的那些类......需要WithAutoNumbering.
有些类需要编号但不需要索引,因此将它们混合在一起不是一个好主意。
困境是,应该WithIndexing继承自WithAutoNumbering?或者每个需要的类也WithIndexing应该继承自WithAutoNumbering?
即这个,CoolClass必须实现索引:
class WithAutoNumbering(object):
...
class WithIndexing(WithAutoNumbering):
...
class CoolClass(WithIndexing):
...
Run Code Online (Sandbox Code Playgroud)
或这个
class WithAutoNumbering(object):
...
class WithIndexing(object):
...
class CoolClass(WithIndexing, WithAutoNumbering):
...
Run Code Online (Sandbox Code Playgroud)
一方面,第一种方法更简洁,确保您不能尝试使用WithIndexingwithouth WithAutoNumbering。另一方面,我一直读到(并且发现它很受欢迎)mix-ins 不应该有层次结构,即只继承 from object,以避免整个类层次结构的意大利面化与无法理解的__mro__s
python ×6
class-method ×1
controller ×1
decorator ×1
exception ×1
fish ×1
inheritance ×1
ipython ×1
logging ×1
many-to-many ×1
mixins ×1
oop ×1
orm ×1
shell ×1
sql ×1
sqlalchemy ×1
sqlite ×1
temporary ×1
view ×1