我在不同的项目中使用了一些 conda 环境,例如:
我在自己的目录中组织了本地项目:
每次我cd到一个特定的项目时,我已经知道我想使用哪个环境。conda activate [some env]所以我每次更改目录时都会这样做。我觉得一定有更好的方法。
自动化这个过程的干净方法是什么?
或者我使用 conda 环境是错误的?
在 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)
初始代码中需要的内容是:
nil如果该块从未采用真值,则返回;make_artwork_from再次调用(假设它不能保证下次调用它时返回相同的结果)。不太理想的是,使用了result三次,但与故事无关。
编辑:抱歉,最初的实现是不正确的,它需要在nil块值从来不为真的情况下返回。
enumerable.lazy.map(&:block).detect(&:itself)
Run Code Online (Sandbox Code Playgroud)
可以完成这项工作,但这是最简单的方法吗?each与简单地使用并缓存值相比,它是否有效?
让我们拥有一个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) 假设我们有两个表:
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,同时损失最少的精度?
activerecord ×1
anaconda ×1
arel ×1
conda ×1
enumerable ×1
environment ×1
python ×1
ruby ×1
timestamp ×1