我正在尝试动态加载我创建的模块.
现在这可以正常工作:
import structures.index
Run Code Online (Sandbox Code Playgroud)
但如果我通过动态导入它来尝试同样的事情,它就会失败.
struct = __import__("structures.index")
Run Code Online (Sandbox Code Playgroud)
提供的错误是:
Error ('No module named structures.index',)
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
编辑:使用完整范围时(它有用吗?):
struct = __import__("neoform.structures.index")
Run Code Online (Sandbox Code Playgroud)
这不会引发任何错误,但是,它没有加载索引模块,而是加载"neoform"模块.
"struct"的结果是:
<module 'neoform' from '/neoform/__init__.py'>
Run Code Online (Sandbox Code Playgroud)
另外,作为一个附带问题,我如何在动态加载的模块中实例化一个类?(假设所有模块都包含一个公共类名).
编辑:解决方案:(感谢coonj和Rick)这最终成功了.不知道为什么(还),但fromlist必须是"任何东西显然,因为当我把字母"a"作为一个值时它起作用(奇怪的是,假设文件中只有1个类).
def get_struct_module(self, name):
try:
return = __import__("neoform.structures." + name, fromlist='*')
except ImportError, e:
self.out.add("Could not load struct: neoform.structure." + name + "\n\n" + "Error " + str(e.args))
Run Code Online (Sandbox Code Playgroud) 创建mysqli对象后,处理$ db_conn变量的最佳方法是什么?
我一直在用
$GLOBALS['_sql'] = new mysqli(...);
Run Code Online (Sandbox Code Playgroud)
但这看起来很脏.我见过的另一种选择是不断地将连接var传递给调用它的每个函数,但这是一个巨大的痛苦.
我很困惑为什么在alert()运行这段代码时全局函数被替换...我不是prototype在这里使用.
Moo = (function(){
this.alert = function(s){
console.log("Replaced Alert! " + s);
};
return this;
})();
alert("poit");
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我没有得到警告弹出窗口,而是运行上面的代码,我看到文本出现在我的控制台中.谁能解释一下?
当尝试通过作用域和类型枚举访问元组的值时,我得到一个错误,指出没有匹配的类型std::get.
enum class User: std::size_t {
FirstName = 1,
LastName = 2,
};
std::tuple<std::string, std::string> user("Bobby", "Bean");
// Error
std::cout << std::get<User::FirstName>(user) << std::endl;
Run Code Online (Sandbox Code Playgroud)
鉴于std::get期望类型std::size_t和枚举的基础类型也是std::size_t,为什么这会失败?
我知道我可以转换枚举的值,但我不确定为什么这两个基本类型是相同的.有了无范围的枚举,这很好用.
我正在尝试用这种方式构建一个有两个键的HashMap:首先我创建了一个只是数据结构的类.
public class Tarta {
public String nome;
public String data;
public Tarta(String nome, String data) {
this.nome = nome;
this.data = data;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我通过在另一个类中写这个来填充我的地图:
mappaTarta.put(new Tarta(nome, data), peso);
Run Code Online (Sandbox Code Playgroud)
在编译期间我没有错误,但是在测试时我得到了null,例如:
System.out.println(lr.leggiRecord().get(new Tarta("R", "15/11/2015")));
Run Code Online (Sandbox Code Playgroud)
你能解释一下为什么吗?谢谢
我正在开发一个mod_wsgi脚本..开头是:
sys.path.extend(map(os.path.abspath, ['/media/server/www/webroot/']))
Run Code Online (Sandbox Code Playgroud)
但我注意到,每次更新脚本时,sys.path var都会随着此扩展的重复项而不断增长:
['/usr/lib64/python25.zip'
'/usr/lib64/python2.5'
'/usr/lib64/python2.5/plat-linux2'
'/usr/lib64/python2.5/lib-tk'
'/usr/lib64/python2.5/lib-dynload'
'/usr/lib64/python2.5/site-packages'
'/usr/lib64/python2.5/site-packages/Numeric'
'/usr/lib64/python2.5/site-packages/gtk-2.0'
'/usr/lib64/python2.5/site-packages/scim-0.1'
'/usr/lib/python2.5/site-packages'
'/media/server/www/webroot'
'/media/server/www/webroot'
'/media/server/www/webroot'
'/media/server/www/webroot']
Run Code Online (Sandbox Code Playgroud)
每次重启apache都会重置..是否有任何方法可以确保不会发生这种情况?我希望模块路径只加载一次..
我有一个创建锚对象的类.当用户点击锚时,我希望它从父类运行一个函数.
function n()
{
var make = function()
{
...
var a = document.createElement('a');
a.innerHTML = 'Add';
//this next line does not work, it returns the error:
//"this.add_button is not a function"
a.onclick = function() { this.add_button(); }
...
}
var add_button = function()
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
这是我的./locale/fr/LC_MESSAGES/messages.po档案:
msgid "NO GOOD"
msgstr "IT WORKED!"
Run Code Online (Sandbox Code Playgroud)
用于生成.mo文件的命令:
$ msgfmt -o locale/fr/LC_MESSAGES/messages.mo locale/fr/LC_MESSAGES/messages.po
Run Code Online (Sandbox Code Playgroud)
我的PHP文件(通过CLI运行,用于测试目的,以root身份).
<?php
bindtextdomain('messages','./locale');
textdomain('messages');
setlocale(LC_MESSAGES, 'fr');
echo gettext("NO GOOD");
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
$ NO GOOD
Run Code Online (Sandbox Code Playgroud)
PHPINFO:
GetText Support => enabled
Run Code Online (Sandbox Code Playgroud)
任何想法为什么这可能不起作用?
好吧,我花了一点时间来缩小这个问题,但看来python正在做这个目的.有人可以解释为什么会发生这种情况以及我可以做些什么来解决这个问题?
文件:library/testModule.py
class testClass:
myvars = dict()
def __getattr__(self, k):
if self.myvars.has_key(k):
return self.myvars[k]
def __setattr__(self, k, v):
self.myvars[k] = v
def __str__(self):
l = []
for k, v in self.myvars.iteritems():
l.append(str(k) + ":" + str(v))
return " - ".join(l)
Run Code Online (Sandbox Code Playgroud)
test.py
from library import testModule
#I get the same result if I instantiate both classes one after another
c1 = testClass()
c1.foo = "hello"
c2 = testClass()
print("c1: " + str(c1) + "\n")
print("c2: " + str(c2) + "\n")
Run Code Online (Sandbox Code Playgroud)
输出: …