我正在构建一个使用ActiveRecord的非rails纯红宝石应用程序.我想写一个rake文件,为它创建一个数据库和表.我尝试以下代码
namespace :db do
task :create do
conn = ActiveRecord::Base.connection
create_db = "CREATE DATABASE foo_dev"
conn.execute(create_db)
end
end
Run Code Online (Sandbox Code Playgroud)
但这给了我
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
Run Code Online (Sandbox Code Playgroud)
错误.嗯,这很明显,因为我没有将ActiveRecord连接到任何数据库.
我该怎么办?
编辑:我想创建一个MySQL数据库.
我想从熊猫系列中删除前导和尾随零,即输入如
my_series = pandas.Series([0,0,1,2,0,3,4,0,0])
Run Code Online (Sandbox Code Playgroud)
应该屈服
pandas.Series([1,2,0,3,4])
Run Code Online (Sandbox Code Playgroud)
作为输出.
我可以通过删除第一个(和最后一个)零然后再次调用该方法来递归地执行此操作.有更多的pythonic方式吗?
我试图在Golang解组数据,我发现一个奇怪的现象时JSON对象中的一些关键有下划线(_在里面).
举个例子:
package main
import (
"encoding/json"
"fmt"
)
func main() {
var jsonBlob = []byte(`{"name": "Quoll", "order": "Dasyuromorphia"}`)
type Animal struct {
Name string `json: "name"`
Order string `json: "order"`
}
var animal Animal
err := json.Unmarshal(jsonBlob, &animal)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", animal)
}
Run Code Online (Sandbox Code Playgroud)
这运行得非常好.但是,如果我更改某个键以包含下划线:
var jsonBlob = []byte(`{"name": "Quoll", "order_": "Dasyuromorphia"}`)
Run Code Online (Sandbox Code Playgroud)
我希望将其纳入Animal.Order,我正在尝试:
type Animal struct {
Name string `json: "name"`
Order string `json: "order_"`
}
Run Code Online (Sandbox Code Playgroud)
我很难读到数据.如何将arbirary键映射到我想要的结构元素?这是一个带有示例的操场的链接.
perl --version 打印所有这些:
This is perl 5, version 26, subversion 1 (v5.26.1) built for darwin-thread-multi-2level
Copyright 1987-2017, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the …Run Code Online (Sandbox Code Playgroud) 我有以下形式的 Rails 路线
get '/:collection/*files' => 'player#index'
Run Code Online (Sandbox Code Playgroud)
其中files旨在是以分号分隔的媒体文件列表,例如/my-collection/some-video.mp4%3Bsome-audio.mp3
这些由以下形式的控制器操作处理:
class PlayerController < ApplicationController
def index
@collection = params[:collection]
@files = params[:files].split(';')
end
end
Run Code Online (Sandbox Code Playgroud)
<audio>并使用显示每个文件的 HTML5或元素的模板进行渲染<video>。
只要文件没有扩展名,例如
/my-collection/file1%3Bfile2.
但是,如果我添加文件扩展名 ,
/my-collection/foo.mp3%3Bbar.mp4我会得到:
没有路线匹配 [GET]“/my-collection/foo.mp3%3Bbar.mp4”
如果我尝试使用单个文件,例如/my-collection/foo.mp3,我得到:
PlayerController#index 缺少此请求格式和变体的模板。request.formats: ["audio/mpeg"] request.variant: []
基于这个答案,我向路线添加了正则表达式约束:
class PlayerController < ApplicationController
def index
@collection = params[:collection]
@files = params[:files].split(';')
end
end
Run Code Online (Sandbox Code Playgroud)
这解决了No Route matches 的问题,但现在多个分离的版本也会因缺少模板而失败。(无论如何,这并不理想,因为我仍然宁愿允许/文件值。但/.*/效果并没有更好。)
我尝试过format: false …
我试图让我的Scala代码更加惯用.现在它看起来像Java代码.
我正在尝试在Scala中做一个简单的布尔正则表达式匹配函数,因为我似乎无法在标准库中找到它(?)
我不认为try-catch和所有的结果都特别好.另外,一个前提条件是'patt'只有一个组,我实际上并没有用它.有什么输入?
def doesMatchRegEx(subj:String, patt:scala.util.matching.Regex) = {
try{
val Match = patt
val Match(x) = subj
true
} catch {
// we didnt match and therefore got an error
case e:MatchError => false
}
}
Run Code Online (Sandbox Code Playgroud)
使用:
scala> doesMatchRegEx("foo",".*(foo).*".r)
res36: Boolean = true
scala> doesMatchRegEx("bar",".*(foo).*".r)
res37: Boolean = false
Run Code Online (Sandbox Code Playgroud) 我有一个git存储库,其中包含大约30个我想要导入现有SVN存储库的修订版.不幸的是,SVN存储库有一堆预提交挂钩,需要提交消息中的某些信息,某些文件类型中的某些SVN关键字等等.这些都与我正在检查的东西无关,或者无论如何,它们都不像保留现有的修订历史那么重要.
在一个完美的世界里,我可能会做一些事情:
在一个不那么完美的世界里,我可以得到git-svn以某种方式预先设置skip-pre-commit-checks(? - 我之前从未使用过它),然后我至少拥有所有的修订历史记录.
思考?
更新以添加: skip-pre-commit-checks实际上并不是一件事; 我被某个特定项目的特定黑客误导了.
给定异构序列,我如何只提取某种类型的成员,并以类型安全的方式对这些成员进行操作?
如果我有:
trait Foo
case class Bar(baz: String)
case class Qux(bar: Bar, quux: String) extends Foo
case class Corge(grault: Int) extends Foo
Run Code Online (Sandbox Code Playgroud)
如果我想采取这种混合Corges和Quxes...的序列
val s = Seq[Foo](Corge(1), Qux(Bar("2"), "3"), Qux(Bar("4"), "5"), Corge(6), Qux(Bar("2"), "7"))
Run Code Online (Sandbox Code Playgroud)
......然后拉出来Quxes,分组Bar:
Map(
Bar(2) -> List(Qux(Bar(2),3), Qux(Bar(2),7)),
Bar(4) -> List(Qux(Bar(4),5))
)
Run Code Online (Sandbox Code Playgroud)
我可以做这个:
s filter { f => f.isInstanceOf[Qux] } groupBy {
f => f.asInstanceOf[Qux].bar }
Run Code Online (Sandbox Code Playgroud)
或者我可以这样做:
(s.filter({ f => f.isInstanceOf[Qux] }).asInstanceOf[Seq[Qux]]) groupBy {
q => q.bar }
Run Code Online (Sandbox Code Playgroud)
但无论哪种方式,我需要两个instanceOf …
给定一个树结构的 TinkerPop 图,其顶点由标记的父子关系 ( [parent-PARENT_CHILD->child]) 连接,遍历和查找所有这些节点的惯用方法是什么?
我是图形遍历的新手,因此使用递归函数遍历它们似乎或多或少很简单:
Stream<Vertex> depthFirst(Vertex v) {
Stream<Vertex> selfStream = Stream.of(v);
Iterator<Vertex> childIterator = v.vertices(Direction.OUT, PARENT_CHILD);
if (childIterator.hasNext()) {
return selfStream.appendAll(
Stream.ofAll(() -> childIterator)
.flatMap(this::depthFirst)
);
}
return selfStream;
}
Run Code Online (Sandbox Code Playgroud)
(注意本示例使用Vavr流,但 Java 流版本类似,只是稍微冗长一些。)
我认为图原生实现的性能会更高,尤其是在内存中 TinkerGraph 以外的数据库上。
但是,当我查看 TinkerPop tree recipes 时,不清楚repeat()/until()等的哪种组合适合做我想做的事。
如果我只想找到那些具有特定标签的顶点(叶子或分支),同样,我可以看到如何使用上面的函数来做到这一点:
Stream<Vertex> nodesWithMyLabel = depthFirst(root)
.filter(v -> "myLabel".equals(v.label()));
Run Code Online (Sandbox Code Playgroud)
但这远不是很明显,这是有效的,我认为必须有更好的图形原生方法。
该类TrainingData有一个属性intent,可以是类的任何子类型Intent.我正在尝试使用Java Generics来强制泛型类型T只能是子类Intent.我怎样才能做到这一点?
public class TrainingData<T> extends Data {
private T intent;
private String id;
public TrainingData (UserCommand userCommand, T intent) {
super(userCommand);
this.intent = intent;
}
public Klass getKlass() {
return intent.getKlass(); // <-- THIS WORKS ONLY IF T extends from Intent
}
public Intent getIntent() {
return intent;
}
public void setIntent(Intent intent) {
this.intent = intent;
}
@Override
public String toString() {
return "TrainingData [intent=" + intent + …Run Code Online (Sandbox Code Playgroud) java ×2
scala ×2
activerecord ×1
collections ×1
database ×1
generics ×1
git ×1
git-svn ×1
go ×1
gremlin ×1
json ×1
pandas ×1
perl ×1
python ×1
regex ×1
ruby ×1
svn ×1
tinkerpop3 ×1
url-routing ×1