小编sun*_*ess的帖子

更改目录时自动激活 conda env

我在不同的项目中使用了一些 conda 环境,例如:

  • ml37(用于机器学习)
  • etl37(用于数据管道)

我在自己的目录中组织了本地项目:

  • 应用程序/some_app
  • 应用程序/other_app
  • ...

每次我cd到一个特定的项目时,我已经知道我想使用哪个环境。conda activate [some env]所以我每次更改目录时都会这样做。我觉得一定有更好的方法。

自动化这个过程的干净方法是什么?

或者我使用 conda 环境是错误的?

python environment anaconda conda

8
推荐指数
1
解决办法
2145
查看次数

Ruby Enumerable:块的第一个真值

在 ruby​​ 中我们可以做这样的事情:

stuff_in_trash.detect(&:eatable?)
=> :pack_of_peanuts

stuff_in_trash.detect(&:drinkable?)
=> nil
Run Code Online (Sandbox Code Playgroud)

但是,如果我们对块第一次为真时的值感兴趣,而不是块取真值的第一个项目,该怎么办?

也就是转换下面的代码:

def try_to_make_artwork_from(enumerable)
  enumerable.each do |item|
    result = make_artwork_from item
    return result if result
 end
   nil
end
Run Code Online (Sandbox Code Playgroud)

对于类似的事情:

def try_to_make_artwork_from(enumerable)
  enumerable.try_with { |item| make_artwork_from item }
end
Run Code Online (Sandbox Code Playgroud)

初始代码中需要的内容是:

  1. nil如果该块从未采用真值,则返回;
  2. 它在第一次为真时返回块的值;
  3. 找到第一个匹配后停止;
  4. 它不会make_artwork_from再次调用(假设它不能保证下次调用它时返回相同的结果)。

不太理想的是,使用了result三次,但与故事无关。

编辑:抱歉,最初的实现是不正确的,它需要在nil块值从来不为真的情况下返回。

enumerable.lazy.map(&:block).detect(&:itself)
Run Code Online (Sandbox Code Playgroud)

可以完成这项工作,但这是最简单的方法吗?each与简单地使用并缓存值相比,它是否有效?

ruby enumerable

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

Arel:来自Arel :: SelectManager的活动关系,具有联接

让我们拥有一个Rails 4.2.x应用程序,并且拥有两个表的帖子和作者,并且我们想使用Arel来获取由作者=='Karl'创作的帖子。(在这种情况下,我们可能对Active Record联接感到满意,但这只是为了使示例简单。)

posts = Arel::Table.new :posts
authors = Arel::Table.new :authors

my_query = posts.project(Arel.star)
                .join(authors)
                .on(posts[:author_id].eq(authors[:id]))
                .where(authors[:name].eq('Karl'))

> my_query.class
=> Arel::SelectManager
Run Code Online (Sandbox Code Playgroud)

现在,我们可以通过执行以下操作来获取帖子数组(类Array):

> Post.find_by_sql my_query
[master]  Post Load (3.1ms)  SELECT * FROM "posts" INNER JOIN "authors"
                             ON "posts"."author_id" = "authors"."id"
                             WHERE "authors"."name" = 'Karl'

=> [#<Post:0x005612815ebdf8
    id: 7474,
    ...
   ]
Run Code Online (Sandbox Code Playgroud)

因此,我们确实获得了一系列帖子,而不是活动记录关系:

 > Post.find_by_sql(my_query).class
 => Array
Run Code Online (Sandbox Code Playgroud)

同时将经理注入Post.where无法正常工作

> Post.where my_query
=> #<Post::ActiveRecord_Relation:0x2b13cdc957bc>
> Post.where(my_query).first
ActiveRecord::StatementInvalid: PG::SyntaxError:
ERROR:  subquery must return only one column
SELECT  "posts".* FROM "posts"
WHERE ((SELECT * FROM …
Run Code Online (Sandbox Code Playgroud)

activerecord arel ruby-on-rails-4

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

Redshift:毫秒到时间戳

假设我们有两个表:

CREATE TABLE IF NOT EXISTS tech_time(
    ms_since_epoch BIGINT
);

CREATE TABLE IF NOT EXISTS readable_time(
    ts timestamp without time zone,
);
Run Code Online (Sandbox Code Playgroud)

假设tech_time有数据并且我们想要填充readable_time

所以在 Postgres 中你可以使用to_timestamp(double precision)并做类似的事情

INSERT INTO readable_time(ts)
SELECT DISTINCT to_timestamp(ms_since_epoch::float / 1000) AS ts,
FROM tech_time;
Run Code Online (Sandbox Code Playgroud)

Amazon Redshift 中似乎不存在这样的功能:

函数 to_timestamp(双精度) 不存在

我的问题是:如何正确填充readable_time,同时损失最少的精度?

timestamp type-conversion amazon-redshift

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