V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  wwqgtxx  ›  全部回复第 41 页 / 共 192 页
回复总数  3836
1 ... 37  38  39  40  41  42  43  44  45  46 ... 192  
@skinny 基本上除了我在#16 提供的使用 exec 的方法以外,想要动态重构一个模块还真的很难。你提供的方法只要模块能正常的被 import 就能把其他模块比如 sys 注入,但是如果在 import 的时候就直接报错 ImportError 之类的就无能为力了。而我提供的方法其实是完全自己手动模拟的 import 的过程,但是无法解决出现一个插件内部 import 另一个插件的情况(当然也可以在 read 之后把文件内部的 import 再 hook 一下,不过这样就非常的臃肿了)。
看楼主的描述,其实是那些个插件本身写的就有语法问题
至于楼主说的“插件居然不能继承入口文件里面引入的库文件”,其实这是个很合理的行为,每个.py 文件都有一个自己独立的作用域,换句话说,除非手动 import 否则各个模块之间应该互相不干扰,贝莱就没有所谓的入口文件的概念,自然也就不存在继承的问题
应该是 6#刚才打错了
@skinny 说实话,我觉得你的实现方法并不能解决题主在 3#描述的问题,你的方案只是解决了批量导入模块的问题,却没办法在导入模块前动态给模块的开头加上 import
但是上述方法在出现 plugin 之间的相互引用的时候还是容易导致错误,这个需要用 import_hook 进一步优化了
还有个比较暴力的办法
import sys,imp

f = open("plugin/xxx.py",'r')
code = "import A \n import B \n" + f.read()
f.close()
module = imp.new_module('plugin.xxx')
exec code in mymodule.__dict__
sys.module['plugin.xxx'] = module

如果是 python3 的话应该这样
from types import ModuleType
import sys

f = open("plugin/xxx.py",'r')
code = "import A \n import B \n" + f.read()
f.close()
mod = ModuleType('plugin.xxx', '')
exec(code, mod.__dict__)
sys.module['plugin.xxx'] = module
2018-08-28 16:34:43 +08:00
回复了 codechaser 创建的主题 Android Android 不支持 lambda 吗?
@PhxNirvana 试了一下,在 AS3.1.2 上,List.sort()会提示警告的
Call requires API level 24 (current min is 15): java.util.List#sort less... (Ctrl+F1)
This check scans through all the Android API calls in the application and warns about any calls that are not available on all versions targeted by this application (according to its minimum SDK attribute in the manifest). If you really want to use this API and don't need to support older devices just set the minSdkVersion in your build.gradle or AndroidManifest.xml files. If your code is deliberately accessing newer APIs, and you have ensured (e.g. with conditional execution) that this code will only ever be called on a supported platform, then you can annotate your class or method with the @TargetApi annotation specifying the local minimum SDK to apply, such as @TargetApi(11), such that this check considers 11 rather than your manifest file's minimum SDK as the required API level. If you are deliberately setting android: attributes in style definitions, make sure you place this in a values-vNN folder in order to avoid running into runtime conflicts on certain devices where manufacturers have added custom attributes whose ids conflict with the new ones on later platforms. Similarly, you can use tools:targetApi="11" in an XML file to indicate that the element will only be inflated in an adequate context.
有个不是很优雅的实现方法,假设你的插件在 /plugin 目录下,你在程序启动时创建(如果已经存在则删除后重建)一个 /_plugin 目录,然后把源 plugin 目录下的文件一个个的拷贝过去,在拷贝的时候,只要是.py 文件就在开头追加一段你需要的"import xxx;import xxx",最后再用__import__("._plugin/xxxxx")
2018-08-28 16:20:57 +08:00
回复了 codechaser 创建的主题 Android Android 不支持 lambda 吗?
@PhxNirvana 一般来说只要你的 android studio 版本足够新,发现了调用当前 Min Sdk Version 不支持的 api 都会有提示呀,如果你不看选择继续使用,那就没办法了
2018-08-28 13:18:25 +08:00
回复了 codechaser 创建的主题 Android Android 不支持 lambda 吗?
@PhxNirvana 你的知识库该更新了,根据 android 官方文档,lambda 特性在任意 android sdk 目标版本上均可用
https://developer.android.com/studio/write/java8-support?hl=zh-cn
2018-08-28 11:17:47 +08:00
回复了 bxqlsyxn 创建的主题 Python 新人学 Python ,编了一个小函数,出了点问题
@dumungweii 如果看系统库的话,python 官方还是很推荐用 try-except-else 的写法的,除非你在 except 中直接就 return 了,否则后面的代码在前面 open 出错了之后执行就会找不到 contents 这个变量
说到底,99%的消费者对这些功能不感冒,硬件厂商当然懒得推动。硬件厂家不动,ms 怎么动
2018-08-25 14:40:11 +08:00
回复了 frmongo 创建的主题 Python Python 的正则表达式的两种写法的区别是什么?
@glacer
@wocanmei
其实 python3 的 re.py 中_compile()函数内部是有个_cache 的
https://github.com/python/cpython/blob/3.7/Lib/re.py#L268
所以并不会每次调用都会编译一遍
2018-08-25 11:45:48 +08:00
回复了 frmongo 创建的主题 Python Python 的正则表达式的两种写法的区别是什么?
def match(pattern, string, flags=0):
"""Try to apply the pattern at the start of the string, returning
a Match object, or None if no match was found."""
return _compile(pattern, flags).match(string)

这是标准库的定义,所以说你的两种写法本质上是一样的
2018-08-24 13:53:49 +08:00
回复了 tedeyang 创建的主题 Python Python 这语言真是混乱和原始
@tlday 说真的,asyncio 的官方文档看着真的是各种鬼畜
既然有意替换各种默认的阻塞库,那就仿照原来的库比如把 socket 换成 asyncio.socket 不就得了,非要用什么 loop.create_connection()
也就对 queue 和 lock 的替换稍微做的业界良心了
然后各种同步异步代码搅在一起,一不留神就把整个程序写飞掉了
而且为啥要手动调用.__str__不应该用 str()的么
2018-08-15 17:15:16 +08:00
回复了 magicalxxzzcc 创建的主题 iPad 这一波 Costco 美行 iPad Pro 9.7 寸 32G 插卡版又来了
32g 感觉没啥用
2018-08-15 13:09:56 +08:00
回复了 xiaoyong 创建的主题 程序员 你有多久没用现金了?
有些菜市场在负一楼没信号的时候还是要用的。。。
2018-08-15 12:13:07 +08:00
回复了 cc959798 创建的主题 Java 关于 Java 中 JIT 的问题
@LukeChien 我记得并不可以完全编译,AOT 仅限于标准库
2018-08-14 17:48:03 +08:00
回复了 cc959798 创建的主题 Java 关于 Java 中 JIT 的问题
全部编译那就是 aot 了,主要还是编译消耗 cpu 资源,你看看 android5.0 那慢到窒息的 app 安装速度,到 android6.0 就改回 jit 了
另外一方面是 jit 可以分层优化,特别是可以边运行边优化,在代码运行到一定次数的时候,能动态把不必要的循环,递归都优化掉

解析执行的意思是一行一行的翻译执行,编译成机器码就是不在需要翻译了,当然最后执行的都是机器码
1 ... 37  38  39  40  41  42  43  44  45  46 ... 192  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5253 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 42ms · UTC 09:00 · PVG 17:00 · LAX 02:00 · JFK 05:00
Developed with CodeLauncher
♥ Do have faith in what you're doing.