小编But*_*840的帖子

如何在不重复的情况下重用大型查询?

如果我有两个查询,我将调用horrible_query_1ugly_query_2,并且我想对它们执行以下两个减号操作:

(horrible_query_1) minus (ugly_query_2)
(ugly_query_2) minus (horrible_query_1)
Run Code Online (Sandbox Code Playgroud)

或者也许我有一个terribly_large_and_useful_query,它产生的结果集我想用作未来几个查询的一部分.

如何避免在多个位置复制和粘贴相同的查询?我怎么能"不重复自己",并遵循DRY原则.这在SQL中可行吗?

我正在使用Oracle SQL.便携式SQL解决方案是首选,但如果我必须使用Oracle特定功能(包括PL/SQL),那就没问题.

sql plsql dry

13
推荐指数
2
解决办法
1万
查看次数

当我尝试去除它时,为什么我的Haskell符号会中断?

我有来自99个Haskell问题的问题26的以下代码:

combinations :: Int -> [a] -> [[a]]
combinations 0 _  = return []
combinations n xs = do y:xs' <- tails xs
                       ys <- combinations (n-1) xs'
                       return (y:ys)
Run Code Online (Sandbox Code Playgroud)

上面的代码按预期工作.以下是我的主要功能和打印结果:

main = print $ combinations 2 "abcd"
-- Prints: ["ab","ac","ad","bc","bd","cd"]
Run Code Online (Sandbox Code Playgroud)

作为一个学习练习,我试图"desugar"这样的记号:

combinations :: Int -> [a] -> [[a]]
combinations 0 _  = return []
combinations n xs = tails xs >>= \(y:xs') ->
                    do
                        ys <- combinations (n-1) xs'
                        return (y:ys)
Run Code Online (Sandbox Code Playgroud)

这会编译,但在运行时会出现以下错误:

PatternMatchFail: /home/.../test.hs:(46,34)-(49,37): Non-exhaustive …
Run Code Online (Sandbox Code Playgroud)

haskell

13
推荐指数
1
解决办法
560
查看次数

我是否需要明确处理SWT Shell?

我被告知并且已经读过必须通过调用他们的dispose方法明确处理SWT对象.但是,在我自己使用以下代码进行的测试中,我注意到,Shell即使在我的代码中的任何地方都没有调用(也不会出现)dispose方法,至少s会将自己报告为已处理.

import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


    public class Test {

        private static int numDisposals = 0;
        private static List<Shell> shells = new ArrayList<Shell>();

    public static void main(String[] args) {
        Display d = Display.getDefault();
        for (int i = 0; i < 3; i++) {
            Shell s = new Shell(d);
            shells.add(s);
            s.setText(String.valueOf(i));
            s.open();
            s.addDisposeListener(new DisposeListener() {

                @Override
                public void widgetDisposed(DisposeEvent notUsed) {
                    numDisposals++;
                    printShellStatus();
                }
            });
        }
        while (numDisposals …
Run Code Online (Sandbox Code Playgroud)

java swt

12
推荐指数
1
解决办法
6353
查看次数

如何存储和计算版本控制历史记录?

考虑这个简单的python代码,它演示了一个非常简单的版本控制设计:

def build_current(history):
    current = {}
    for action, key, value in history:
        assert action in ('set', 'del')
        if action == 'set':
            current[key] = value
        elif action == 'del':
            del current[key]
    return current

history = []
history.append(('set', '1', 'one'))
history.append(('set', '2', 'two'))
history.append(('set', '3', 'three'))
print build_current(history)
history.append(('del', '2', None))
history.append(('set', '1', 'uno'))
history.append(('set', '4', 'four'))
print build_current(history)
for action, key, value in history:
    if key == '2':
        print '(%s, %s, %s)' % (action, key, value)
Run Code Online (Sandbox Code Playgroud)

请注意,通过使用历史列表,您可以在曾经存在的任何状态下重建当前字典.我认为这是一个"前向构建"(缺少一个更好的术语)因为要构建当前字典,必须从头开始并处理整个历史列表.我认为这是最明显和最直接的方法.

正如我所听到的,早期版本控制系统使用了这种"前向构建"过程,但它们并不是最佳的,因为大多数用户更关心构建的最新版本.此外,当用户只关心查看最新版本时,他们不想下载整个历史记录.

那么我的问题是,在版本控制系统中存储历史记录还有哪些其他方法?也许可以使用"向后构建"?这可能允许用户仅下载最近的修订版而不需要整个历史记录.我还看到了一些用于存储历史记录的不同格式,即:变更集,快照和补丁.变更集,快照和补丁之间有什么区别? …

python svn git version-control mercurial

11
推荐指数
3
解决办法
1512
查看次数

在Python中,if条件中的变量是否隐藏全局范围,即使它们未被执行?

def do_something():
    print 'doing something...'

def maybe_do_it(hesitant=False):
    if hesitant:
        do_something = lambda: 'did nothing'
    result = do_something()
    print result

maybe_do_it()
Run Code Online (Sandbox Code Playgroud)

这段代码的结果是:

  File "scope_test.py", line 10, in <module>
    maybe_do_it()
  File "scope_test.py", line 7, in maybe_do_it
    result = do_something()
UnboundLocalError: local variable 'do_something' referenced before assignment
Run Code Online (Sandbox Code Playgroud)

但是这段代码按照预期印刷了"做了些什么......":

def do_something():
    print 'doing something...'

def maybe_do_it(hesitant=False):
    result = do_something()
    print result

maybe_do_it()
Run Code Online (Sandbox Code Playgroud)

即使if语句中的条件从未执行过,该函数是如何被覆盖的?这种情况发生在Python 2.7中 - 在Python 3中是否相同?

python

11
推荐指数
2
解决办法
3131
查看次数

Python的SQLAlchemy不会清除二级(多对多)表吗?

UserTasks 之间有多对多的关系.当我删除Task或删除时,我希望清除"辅助表"(意思是,促进多对多关系的表)User.如何为此配置SQLAlchemy?

这是一些示例python代码,它演示了我遇到的问题.注意:此代码完全独立,只需要sqlalchemy模块.如果您复制并粘贴此代码,您应该能够在没有任何副作用的情况下运行它并自己查看相同的行为.脚本的最后一行显示删除相应任务时未删除"辅助表"中的相关行.在这个例子中,所有的断言都通过了.

from sqlalchemy import create_engine, Column, Integer, Text, Table, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, relationship

Model = declarative_base()

class User(Model):
    __tablename__ = 'users'
    id = Column('user_id', Integer, primary_key=True)
    email = Column('email', Text, unique=True)

    def __init__(self, email):
        self.email = email

user_tasks = Table('user_tasks', Model.metadata,
    Column('user_id', Integer, ForeignKey('users.user_id')),
    Column('task_id', Integer, ForeignKey('tasks.task_id')))

class Task(Model):
    __tablename__ = 'tasks'
    id = Column('task_id', Integer, primary_key=True)
    description = Column('description', Text)
    assigned_to …
Run Code Online (Sandbox Code Playgroud)

python many-to-many sqlalchemy

9
推荐指数
1
解决办法
3861
查看次数

如何在Python和Networkx中找到图形中的循环关系?

考虑我有以下图表:

A -> B
B -> C
C -> D
C -> A
Run Code Online (Sandbox Code Playgroud)

找到A - > B - > C - > A是一个循环关系的最简单方法是什么?是否已经在NetworkX或其他易于使用的Python库中内置了这样的功能?

python algorithm graph-theory networkx

7
推荐指数
2
解决办法
1748
查看次数

Class.getResource()在我的Eclipse Application中返回null?无法配置classpath?

我试图用来Class.getResource("rsc/my_resource_file.txt")在Eclipse应用程序中加载文件.但是,无论我在Eclipse中做什么,类路径总是只包含Eclipse Launcher的一个条目:

... /日蚀/插件/ org.eclipse.equinox.launcher_1.2.0.v20110502.pkc

如何配置类路径?

注意:在运行时,我使用以下代码确定类路径:

URLClassLoader cl = (URLClassLoader) ClassLoader.getSystemClassLoader();
for (URL classpathURL : cl.getURLs()) {
    System.out.println(classpathURL);
}
Run Code Online (Sandbox Code Playgroud)

编辑:更多信息.

问题的根源Class.getResource("rsc/my_resource_file.txt")是返回null.在一个简单的5行"Java应用程序"中做了一些小实验,我以为我已经弄明白了,问题与类路径有关.显然,类路径与"Eclipse应用程序"的行为略有不同.我通过Class.getResource("/rsc/my_resource_file.txt")感谢BalusC 解决了这个问题.

java eclipse classpath

6
推荐指数
1
解决办法
2万
查看次数

在Haskell中汇总1到1,000,000会导致堆栈溢出.引擎盖下发生了什么?

我有以下代码.

main = print $ sum [1..1000000]
Run Code Online (Sandbox Code Playgroud)

当我运行时,我得到一个堆栈溢出:

Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize -RTS' to increase it.
Run Code Online (Sandbox Code Playgroud)

我习惯于像Python那样的命令式语言,这些算法似乎没有问题:

sum(range(100000000))  # I'm not even using a generator.
4999999950000000
Run Code Online (Sandbox Code Playgroud)

Haskell显然是不同的,但我不太明白是什么导致堆栈溢出?在引擎盖下发生什么导致Haskell中的堆栈溢出?

stack-overflow haskell sum

6
推荐指数
1
解决办法
487
查看次数

我如何知道我要为 sci-kit learn 提供哪些先验知识?(朴素贝叶斯分类器。)

在 sci-kit learn 的朴素贝叶斯分类器中,您可以指定先验概率,分类器将在计算中使用这些提供的概率。但我不知道先验概率应该如何排序。

from sklearn.naive_bayes import BernoulliNB
data = [[0], [1]]
classes = ['light bulb', 'door mat']
classes.shuffle()  # This simulates getting classes from a complex source.
classifier = BernoulliNB(class_prior=[0, 1])  # Here we provide prior probabilities.
classifier.fit(data, classes)
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我如何知道哪个类被假定为 100% 先验?在指定先验概率之前,我是否需要考虑数据中类别的顺序?

我也有兴趣知道这个记录在哪里。

python scikit-learn

5
推荐指数
1
解决办法
2499
查看次数