我刚刚开始使用Equinox和Eclipse PDE的OSGI和声明服务(DS).
我有2个Bundle,A和B. Bundle A公开了Bundle B使用的组件.这两个bundle也将这个服务再次公开给OSGI Service注册表.
到目前为止,一切正常,Equinox将组件连接在一起,这意味着Bundle A和Bundle B由Equinox实现(通过调用默认构造函数),然后使用bind/unbind方法进行连线.
现在,由于Equinox正在创建这些组件/服务的实例,我想知道获取此实例的最佳方法是什么?
所以假设有第三个类没有被OSGI实例化:
Class WantsToUseComponentB{ public void doSomethingWithComponentB(){ // how do I get componentB??? Something like this maybe? ComponentB component = (ComponentB)someComponentRegistry.getComponent(ComponentB.class.getName()); }
我现在看到以下选项:
1.使用Activator中的ServiceTracker来获取ComponentBundleA.class.getName()的服务(我已经尝试了它并且它可以工作,但对我来说似乎有很多开销)并通过静态工厂方法使其可用
public class Activator{ private static ServiceTracker componentBServiceTracker; public void start(BundleContext context){ componentBServiceTracker = new ServiceTracker(context, ComponentB.class.getName(),null); } public static ComponentB getComponentB(){ return (ComponentB)componentBServiceTracker.getService(); }; }
2.创建某种注册表,其中每个组件在调用activate()方法后立即注册.
public ComponentB{ public void bind(ComponentA componentA){ someRegistry.registerComponent(this); }
要么
public ComponentB{ public void activate(ComponentContext context){ someRegistry.registerComponent(this); …
我经常面临将一个 API 的参数空间映射到另一个 API 的参数空间的问题。我经常看到这个问题是通过嵌套嵌套的... switch 语句解决的。
我想知道是否会碰巧有一个库或一种技术可以让您“声明”映射而不是“编程”它。
一个简单的例子包括将两个枚举的值合并为一个:
namespace sourceAPI {
struct A { typedef e { A1, A2, A3 } };
struct B { typedef e { B1, B2 } };
}
namespace targetAPI {
struct AB { typedef e { A1B1, A1B2, A2B1, A2B2, A3B1, A3B2 } };
}
Run Code Online (Sandbox Code Playgroud)
其中映射通常是这样完成的
switch( a ){
case( A::A1 ): switch( b ) {
case( B::B1 ): return A1B1;
case( B::B2 ): return A1B2;
....
}
Run Code Online (Sandbox Code Playgroud)
而且这个映射仍然需要一个“反向”开关。
但我更喜欢“密集”的东西
declare( source( A::A1, …
Run Code Online (Sandbox Code Playgroud) 我正在向我的Java应用程序编写XML文件的导入功能.我正在使用XOM来解析XML文件.解析XML的代码不容易理解,它是一些硬编码.getChild(3)
等等.与声明性XML文档相比,很难遵循代码.
是否有更可维护的方法将XML文档解析为Java对象?我希望以更具说明性的方式使用它,我可以在其中指定哪些标记对应于哪些Java类.
这就是我为应用程序设置数据库的方法(在Flask中):
from sqlalchemy.engine import create_engine
from sqlalchemy.orm import scoped_session, create_session
from sqlalchemy.ext.declarative import declarative_base
engine = None
db_session = scoped_session(lambda: create_session(bind=engine,
autoflush=False, autocommit=False, expire_on_commit=True))
Base = declarative_base()
Base.query = db_session.query_property()
def init_engine(uri, **kwargs):
global engine
engine = create_engine(uri, **kwargs)
Base.metadata.create_all(bind=engine)
return engine
Run Code Online (Sandbox Code Playgroud)
如果我连接到已经创建了表的文件数据库,一切正常,但使用sqlite:///:memory:作为目标数据库给我:
OperationalError: (OperationalError) no such table: users u'DELETE FROM users' ()
Run Code Online (Sandbox Code Playgroud)
当你这样查询时:
UsersTable.query.delete()
db_session.commit()
Run Code Online (Sandbox Code Playgroud)
我从单元测试中访问此代码.问题是什么?
谢谢
编辑:
应用程序的工作设置:
app = Flask(__name__)
app.config.from_object(__name__)
app.secret_key = 'XXX'
# presenters
from presenters.users import users
# register modules (presenters)
app.register_module(users) …
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
static if (!is(MyStruct))
{
struct MyStruct
{
}
}
static if (is(MyStruct))
{
static assert(0);
}
Run Code Online (Sandbox Code Playgroud)
我最初的理解是,声明的顺序(在全球范围内)在D中无关紧要.
但是,在这种情况下,static if
s 的顺序决定了程序是否编译.
因此,D的编译时评估阶段是程序性特征(如C/C++),声明性特征还是其他什么?目前是什么,计划是什么(如果两者不同)?
我刚刚意识到,问题甚至还没有结束.一,会发生什么static if
用途.tupleof
枚举当前模块的成员,并创建同一类型的问题?
此示例显示如何将其与"非声明性"一起使用 - http://docs.sqlalchemy.org/en/latest/core/ddl.html#sqlalchemy.schema.DDL
如何将其与ORM声明性语法一起使用?
例如,使用此结构:
Base = declarative_base(bind=engine)
class TableXYZ(Base):
__tablename__ = 'tablexyz'
Run Code Online (Sandbox Code Playgroud) 我一直在研究声明性语言,似乎声明只是逻辑和函数语言的总称.还是我错了?是否有任何通用的声明性编程语言不能归类为功能或逻辑(al),只是"声明"?
我很好奇这是否有可能在余烬中.这是一个很容易做的角度(plunkr:http://plnkr.co/edit/O2e0ukyXdKMs4FcgKGmX?p = preview ):
目标是为api消费者提供易于使用,通用,可重复使用的手风琴api.
我希望调用者能够使用的api就是这个(就像角度api一样):
{{#ember-accordion listOfAccordionPaneObjects=model}}
{{#ember-accordion-heading}}
heading template html {{accordionPaneObject.firstName}}
{{/ember-accordion-heading}}
{{#ember-accordion-body}}
this is the accordion body {{accordionPaneObject.lastName}}
{{/ember-accordion-body}}
{{/ember-accordion}}
Run Code Online (Sandbox Code Playgroud)
这是我用angular写的一个工作示例:
<!doctype html>
<html ng-app="angular-accordion">
<head>
<style>
.angular-accordion-header {
background-color: #999;
color: #ffffff;
padding: 10px;
margin: 0;
line-height: 14px;
-webkit-border-top-left-radius: 5px;
-webkit-border-top-right-radius: 5px;
-moz-border-radius-topleft: 5px;
-moz-border-radius-topright: 5px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
cursor: pointer;
text-decoration: none;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
}
.angular-accordion-container {
height: 100%;
width: 100%;
}
.angular-accordion-pane {
padding: 2px; …
Run Code Online (Sandbox Code Playgroud) 要创建User
表,我必须使用drop_all
然后使用create_all
方法.但是这两个函数重新启动整个数据库.我想知道是否有一种方法来创建User
表而不擦除(或删除)数据库中的任何现有表?
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
name = sqlalchemy.Column(sqlalchemy.String)
def __init__(self, code=None, *args, **kwargs):
self.name = name
url = 'postgresql+psycopg2://user:pass@01.02.03.04/my_db'
engine = sqlalchemy.create_engine(url)
session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker())
session.configure(bind=engine, autoflush=False, expire_on_commit=False)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
Run Code Online (Sandbox Code Playgroud) setuptools 30.3.0引入了声明性包配置,使我们可以将以前直接传递给文件的大多数选项setuptools.setup
放入setup.cfg
文件中。例如,给出以下setup.cfg:
[metadata]
name = hello-world
description = Example of hello world
[options]
zip_safe = False
packages =
hello_world
install_requires =
examples
example1
Run Code Online (Sandbox Code Playgroud)
仅包含一个setup.py
import setuptools
setuptools.setup()
Run Code Online (Sandbox Code Playgroud)
会做所有正确的事情。
但是,我无法找出的正确语法extras_require
。在setup
args中,它是一个字典,例如
setup(extras_require={'test': ['faker', 'pytest']})
Run Code Online (Sandbox Code Playgroud)
但是我找不到在setup.cfg中使用的正确语法。我尝试阅读文档,但是找不到那里的setuptools期望的正确语法。我也尝试了一些猜测
[options]
extras_require =
test=faker,pytest
Run Code Online (Sandbox Code Playgroud)
它失败。
Traceback (most recent call last):
File "./setup.py", line 15, in <module>
'pylint',
File "/lib/site-packages/setuptools/__init__.py", line 128, in setup
_install_setup_requires(attrs)
File "/lib/site-packages/setuptools/__init__.py", line 121, in _install_setup_requires
dist.parse_config_files(ignore_option_errors=True)
File "/lib/python3.6/site-packages/setuptools/dist.py", line 495, in parse_config_files
self._finalize_requires() …
Run Code Online (Sandbox Code Playgroud) declarative ×10
python ×4
sqlalchemy ×3
java ×2
api ×1
c++ ×1
compile-time ×1
components ×1
d ×1
ember.js ×1
equinox ×1
frameworks ×1
osgi ×1
service ×1
setuptools ×1
sql ×1
sqlite ×1
static-if ×1
terminology ×1
transclusion ×1
xml ×1
xml-parsing ×1