小编hir*_*lau的帖子

安装旧版R包

我试图使用Rpy2和ggplot2,但我收到一个错误.在网上搜索了一些错误后,我发现错误的发生是因为ggplot2包中的更改尚未反映在Rpy2中(例如,请参阅此帖子(编辑:链接现已死)).

所以我现在需要安装旧版本的ggplot2.这是我想要的伪代码:

install.packages("ggplot2", version='0.9.1')
Run Code Online (Sandbox Code Playgroud)

install.packages没有version争论.我该怎么做?

installation r version ggplot2 r-faq

117
推荐指数
8
解决办法
7万
查看次数

熊猫的T检验

如果我想计算Pandas中两个类别的平均值,我可以这样做:

data = {'Category': ['cat2','cat1','cat2','cat1','cat2','cat1','cat2','cat1','cat1','cat1','cat2'],
        'values': [1,2,3,1,2,3,1,2,3,5,1]}
my_data = DataFrame(data)
my_data.groupby('Category').mean()

Category:     values:   
cat1     2.666667
cat2     1.600000
Run Code Online (Sandbox Code Playgroud)

我有很多以这种方式格式化的数据,现在我需要做一个T -test,看看cat1cat2的平均值是否有统计学差异.我怎样才能做到这一点?

python statistics scipy pandas hypothesis-test

55
推荐指数
3
解决办法
7万
查看次数

Ruby - 在内存中创建文件

无论如何在Ruby中编写以下代码而不将文件写入磁盘?

temp_file = 'path/to/file.csv'
users = [a@b.c, c@b.a]

CSV.open(temp_file, "w") do |csv|
  csv << data_for_report
end

Reports.sendreport users temp_file

File.delete(temp_file)
Run Code Online (Sandbox Code Playgroud)

Reports.sendreport附加一个文件并发送一封电子邮件,因此它需要是一个文件......

ruby csv io

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

使用 FastAPI 和 Swagger 刷新令牌

我正在尝试使用 FastAPI 为我们的组织创建 API。它有一个用于所有身份验证的 KeyCloak 服务器,以及被认为是最佳实践的 OpenID Connect 和 JWT。

在最简单的情况下,其他人负责获取有效的 JWT 令牌,以便 FastAPI 可以简单地解码和读取用户和权限。

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

async def get_current_user(token: str = Depends(oauth2_scheme)):

    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": "Bearer"},
    )

    try:
        jwt_token = jwt.decode(token, key=env.keycloak_server_public_key, audience='myorg')
        return jwt_token['preferred_username']
    except jwt.exceptions.ExpiredSignatureError:
        raise credentials_exception
Run Code Online (Sandbox Code Playgroud)

生活是简单的!

但是,我确实想尝试让用户使用 Swagger 页面探索 API。我创建了这个函数,让用户使用 UI 登录:

@app.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    login_request = requests.post(
        "https://mygreatorg.com/auth/realms/master/protocol/openid-connect/token",
        data={
            "grant_type": "password",
            "username": form_data.username,
            "password": form_data.password,
            "client_id": "fastapi-application",
        },
    )
    raw_response = json.loads(login_request.content.decode('utf-8'))
    raw_response['acquire_time'] …
Run Code Online (Sandbox Code Playgroud)

python jwt swagger swagger-ui fastapi

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

ORACLE中的CTE和表更新

在我最终通过更新表中的列来存储结果之前,我想要运行许多复杂的逻辑.我收到一个错误,并且能够将其归结为:

with my_cte as
(
  select x,ix from y
)
update z
set mycol = (select x from my_cte where z.ix = my_cte.ix)
Run Code Online (Sandbox Code Playgroud)

然而,这给出了错误:

Error at line 4:
ORA-00928: missing SELECT keyword
set mycol = (select x from my_cte where z.ix = my_cte.ix)
Run Code Online (Sandbox Code Playgroud)

这是否意味着CTE不能与更新一起使用,因为以下查询工作正常:

update z
set mycol = (select x from y where y.ix = my_cte.ix)
Run Code Online (Sandbox Code Playgroud)

使用12c企业版12.1.0.2.0版

编辑:

解决这个问题一段时间后,获得合理性能的唯一方法是使用MERGE子句(仍然使用CTE,如下面的答案).

merge into z using (
  with my_cte as (
    select x,ix from y
  )
)
on (
  my_cte.ix = z.ix …
Run Code Online (Sandbox Code Playgroud)

sql oracle common-table-expression

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

在Oracle中使用单个语句在表之间移动行

由于旧系统的愚蠢限制,我试图使用一个语句编写以下查询:

insert into dbo.mytable_archive 
  select * 
    from dbo.mytable 
   where date < trunc(sysdate) - 14;

delete from dbo.mytable 
 where date < trunc(sysdate) - 14;
Run Code Online (Sandbox Code Playgroud)

利用Google的强大功能,我发现使用RETURNINGPostgres OUTPUT子句或SQLServer中的子句在许多其他数据库中似乎可行,但是我找不到Oracle(V12)的等效解决方案。

有解决方法的主意吗?

sql oracle

4
推荐指数
1
解决办法
1785
查看次数

Pytest 固定装置:在每个测试之间设置、拆卸和运行代码

我正在尝试使用 pytest 来测试我正在编写的模块。该模块是一个具有长启动时间的进程的包装器。因此,我想确保我有一个正确的设置/拆卸逻辑,以确保初始化不会发生多次。

我当前的代码如下所示:

import pytest
import leelaZeroWrapper

@pytest.fixture(scope='module')
def leela(request):
    leela = leelaZeroWrapper.LeelaWrapper()
    def quit():
        leela.quit()
    request.addfinalizer(quit)
    return leela

def test_single_play(leela):
    leela.reset()
    result = leela.play('b', 'a11')
    assert result == [{'color': 'black', 'loc': 'a11'}]

def test_single_play_uppercase(leela):
    leela.reset()
    result = leela.play('WHITE', 'A11')
    assert result == [{'color': 'white', 'loc': 'a11'}]

def test_reset(leela):
    leela.reset()
    leela.play('b', 'a11')
    leela.play('w', 'a13')
    leela.reset()
    assert leela.current_board == []
    assert leela.current_move == 0
Run Code Online (Sandbox Code Playgroud)

我注意到我的所有测试都将从调用重置我的模块开始!有没有办法将它添加到夹具中,或者您将如何解决它?

我想要的伪代码:

@pytest.fixture(scope='module')
def leela(request):
    leela = leelaZeroWrapper.LeelaWrapper()
    def quit():
        leela.quit()
    def reset():
        leela.reset()
    request.addfinalizer(quit)
    request.add_between_test_resetter(reset) …
Run Code Online (Sandbox Code Playgroud)

python fixture pytest

3
推荐指数
1
解决办法
7181
查看次数

内部工作的Integer('string')调用?

如果我想查看字符串是否是有效整数,我可以这样做:

puts Integer('1') #=> 1
Run Code Online (Sandbox Code Playgroud)

因为任何非整数都会引发错误:

puts Integer('a') #=> invalid value for Integer(): "a" (ArgumentError)
Run Code Online (Sandbox Code Playgroud)

如果我想确保我的变量是Float和Integer,我不想重复自己,所以我尝试将这些类放在一个列表中:

x = '1'
[Integer, Float].each{|c| puts c(x) } #=> undefined method `c' for main:Object (NoMethodError)
Run Code Online (Sandbox Code Playgroud)

有人会解释为什么这不起作用,如果有办法实现我想要的东西?什么样的方法是Integer(var)什么?

注意,我没有现实世界的问题我想在这里解决,我只是好奇.

ruby inheritance class

0
推荐指数
1
解决办法
60
查看次数

动态调用模块中的方法?

如何动态调用模块中的方法?

module Notification
  def self.send_notification(title, message)
    puts title + message
  end
end

def test(string)
  p string
end

if __FILE__ == $0
  send("test", 'hello from test')
  send("Notification.send_notification", 'hello', 'there') # Error: undefined method `Notification.send' for main:Object (NoMethodError)
end
Run Code Online (Sandbox Code Playgroud)

编辑:我的库中有多个模块,我确实需要能够将字符串转换为模块名称。假设我还有一个名为电子邮件的模块。也许 Eval 是唯一的方法?Edit2:重命名方法以免与内置发送方法冲突。

ruby methods module

0
推荐指数
1
解决办法
952
查看次数