我是 sqlalchemy 的新手,只了解基本的 sqlalchemy 知识。
现在我正在编写一些Python代码,我需要做的事情如下:
有一个用户表、一个组表和一个组用户表。为了简化问题,假设我知道用户 id 是 100。现在我想将一个新组插入到 Group 表中,并获取组 id,然后将 (group_id, user_id) 元组插入 GroupUser 表中。
我可以写的代码如下:
# Insert the group first.
session = self.DBSession()
new_group = Group(name = 'gname')
session.add(new_group)
session.commit()
# Then query back the new group id
gid = session.query(Group).filter(Group.name == 'gname').first().id
# At last, insert group-user
gu = GroupUser(gid=gid, uid=100)
session.add(gu)
session.commit()
Run Code Online (Sandbox Code Playgroud)
顺便说一句,Group表中的id是自增的。
我想知道这个程序是否可以简化?我可以在一次交易中完成此操作吗?
我正在尝试排序git tag。通过Google搜索,我发现很多帖子都在谈论taggerdate,但在我的仓库中,taggerdate总是空的:
$ /usr/bin/git for-each-ref --format "%(refname) | %(subject)| %(taggerdate) | %(authordate)" refs/tags
refs/tags/hello | Merge branch 'featureY' into 'master'| | Sun Jan 19 23:36:30 2020 -0800
refs/tags/v3.0 | Revert "aaaaaaa"| | Wed Jul 3 15:09:20 2019 +0800
refs/tags/v4.0 | feat: a big enhancement| | Wed Jul 3 15:12:49 2019 +0800
refs/tags/v5.0 | feat: hello world. Closed [RNWY-56].| | Wed Jul 3 15:29:13 2019 +0800
refs/tags/v6.0 | feat: hello world. Closes [RNWY-56].| | Wed …Run Code Online (Sandbox Code Playgroud) 我需要模拟一个异常的提交历史来编写一个测试用例,我想要一个像这样的提交历史:
sha4
sha3 // i want this to be a root commit
sha2
sha1 // this is the first commit of the repo, so it's a root commit
Run Code Online (Sandbox Code Playgroud)
我希望git rev-list显示:
$ git rev-list --max-parents=0 HEAD
sha3
sha1
Run Code Online (Sandbox Code Playgroud)
我的最终目标是让git rev-list --max-parents=0 HEAD返回多行。
Linux服务器启动时,会发出一些组播数据包。目的MAC地址是33:33:00:00:00:02。
我想知道多播地址是否是众所周知的?是不是有什么特殊的意义呢?是否有众所周知的组播MAC地址列表?
当我们在Ruby中定义dict/hash时,我们这样做:
{:a => 'b'}
Run Code Online (Sandbox Code Playgroud)
但我读了一些像这样的Ruby代码:
{:a : 'b'}
Run Code Online (Sandbox Code Playgroud)
这应该是类似Python的.任何Ruby版本都支持吗?我没有读过任何提到的Ruby书.
更新:
我在Linux机器上运行以下命令:
$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
$ ruby -e 'puts {a: "b"}'
-e:1: syntax error, unexpected ':', expecting '}'
puts {a: "b"}
^
Run Code Online (Sandbox Code Playgroud)
并在我的Macbook上运行以下内容:
$ /Users/chaol/.rvm/wrappers/ruby-2.0.0-p247/ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]
$ /Users/chaol/.rvm/wrappers/ruby-2.0.0-p247/ruby -e 'puts {a: "b"}'
-e:1: syntax error, unexpected ':', expecting '}'
puts {a: "b"}
^
Run Code Online (Sandbox Code Playgroud)
两个Ruby版本都是1.9+,为什么我仍然会收到错误?
我正在从openstack复制一些代码片段,但是当它运行到:
import six.moves.xmlrpc_client as xmlrpclib
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
import six.moves.xmlrpc_client as xmlrpclib
ImportError: No module named xmlrpc_client
Run Code Online (Sandbox Code Playgroud)
我已经安装了six包.如何解决这个问题呢?
我正在使用python 2.7处理MacOS.
我尝试安装但失败了:
lichaos-MacBook-Pro:common lichao$ sudo pip install --allow-unverified xmlrpclib xmlrpclib
Collecting xmlrpclib
xmlrpclib is potentially insecure and unverifiable.
Downloading http://effbot.org/media/downloads/xmlrpclib-1.0.1.zip
Installing collected packages: xmlrpclib
Running setup.py install for xmlrpclib
changing mode of build/scripts-2.7/xmlrpc_handler.py from 644 to 755
changing mode of build/scripts-2.7/xmlrpcserver.py from 644 to 755
changing mode of build/scripts-2.7/echotest.py from 644 to 755
changing mode of /usr/local/bin/echotest.py to 755
changing mode …Run Code Online (Sandbox Code Playgroud) 我昨天在Macbook上安装了带有Goclipse插件的Eclipse(Mars).他们最初工作得很好.但今天我发现我不能做交叉引用(这意味着cmd +点击关键字无法跳转定义).控制台显示:
Run Code Online (Sandbox Code Playgroud)Running: oracle -pos=/Users/myname/coreos/naas/server/src/mycompany.com/hh/naas-server/main.go:#389,#389 -format=json describe mycompany.com/hh/naas-server FAILED: Could not start process: Reason: Cannot run program "oracle": error=2, No such file or directory
在Ruby脚本中,我想获取操作系统信息,不仅是Linux,还包括32位或64位。
那是因为我的程序将在多个Linux平台上运行。它调用第三方工具,该工具有子文件夹:lin32,lin64,我需要根据操作系统信息调用正确的版本。
我正在编写一个需要验证文件存在的 Python 脚本。该文件可以是完整路径,例如/home/xxx/file.txtURL http://company.com/xxx/file.txt。
是否有一种python方法可以验证各种路径模式的存在?
如果在函数中定义类实例,则在函数退出时,实例将因范围超出而自动销毁.这可以通过一个小程序简单地验证:
class A(object):
def __del__(self):
print 'deleting A ', id(self)
class B(A):
def __init__(self):
self.a = A()
def __del__(self):
print 'deleting B ', id(self)
super(B, self).__del__()
def test():
b = B()
print 'this is ', b
test()
Run Code Online (Sandbox Code Playgroud)
输出是:
this is <__main__.B object at 0x01BC6150>
deleting B 29122896
deleting A 29122896
deleting A 29122960
Run Code Online (Sandbox Code Playgroud)
但我遇到了一个奇怪的问题.当我从novaclient继承一个类时,该实例永远不会被自动销毁.
from novaclient.v1_1.client import Client as NovaClient
class ViviNovaClient(NovaClient):
def __init__(self, auth_token, url, tenant_id):
super(ViviNovaClient, self).__init__(None, None, tenant_id, auth_url = 'http')
self.client.management_url = url
self.client.auth_token = auth_token …Run Code Online (Sandbox Code Playgroud)