我正在尝试将一些全局变量传递给 vim 脚本中的函数。但是,一旦将它们传递给函数,我最终会收到变量名称,而不是实际的变量值。这是一个简单的案例:
let g:my_var_a = "melon"
let g:my_var_b = "apple"
" Define the main function
function! MyFunc(myarg1, myarg2)
echom "Arguments: " . a:myarg1 . ", " . a:myarg2
endfunction
" Link the function to a command
command! -nargs=* HookMyFunc call MyFunc(<f-args>)
" Link the command to a plug
nnoremap <unique> <Plug>MyHook :HookMyFunc g:my_var_a g:my_var_b<CR>
" Assign a key to the plug
nmap <silent> <leader>z <Plug>MyHook
Run Code Online (Sandbox Code Playgroud)
所以,如果我这样做: nnoremap <unique> <Plug>MyHook :HookMyFunc melon apple<CR>
我得到以下输出: Arguments: melon apple
当我这样做时: nnoremap <unique> …
我有以下numpy数组:
foo = np.array([[0.0, 10.0], [0.13216, 12.11837], [0.25379, 42.05027], [0.30874, 13.11784]])
Run Code Online (Sandbox Code Playgroud)
产生:
[[ 0. 10. ]
[ 0.13216 12.11837]
[ 0.25379 42.05027]
[ 0.30874 13.11784]]
Run Code Online (Sandbox Code Playgroud)
如何标准化此数组的Y分量。所以它给了我类似的东西:
[[ 0. 0. ]
[ 0.13216 0.06 ]
[ 0.25379 1 ]
[ 0.30874 0.097]]
Run Code Online (Sandbox Code Playgroud) 我一直在尝试绘制resampled来自 Pandas 数据框的简单数据。这是我的初始代码:
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
# Extra plotly bits
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(56), freq='D')
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'date': days, 'value': data})
Run Code Online (Sandbox Code Playgroud)
当我这样做时,print df我得到这个:
date value
0 2017-10-28 17:13:23.867396 29
1 2017-10-29 17:13:23.867396 56
2 2017-10-30 17:13:23.867396 82
3 2017-10-31 17:13:23.867396 13
4 2017-11-01 17:13:23.867396 35 …Run Code Online (Sandbox Code Playgroud) 实现以下代码段中演示的结果的优雅方式是什么。
我通过将第一个数组的每个元素分配到第二个数组来将两个数组合并为一个新数组。
arr1 = ['XYZ', 'ABC']
arr2 = ['EUR', 'USD']
result = ['XYZ/EUR', 'XYZ/USD', 'ABC/EUR', 'ABC/USD']
Run Code Online (Sandbox Code Playgroud)
arr1 = ['XYZ', 'ABC']
arr2 = ['EUR', 'USD']
result = ['XYZ/EUR', 'XYZ/USD', 'ABC/EUR', 'ABC/USD']
Run Code Online (Sandbox Code Playgroud)
这是我的基础对象:
let resources = {
TEST_FLAG: false,
FRUIT: 'banana',
ID: 11
};
Run Code Online (Sandbox Code Playgroud)
我想通过 asetter和 a访问该对象的每个属性getter。我尝试在下面这样做:
let dynamicResources = resources
for (let key in resources) {
Object.defineProperty(dynamicResources, key, {
get() {
console.log(`[debug]: GET <${key}>, VALUE <${this[key]}>`);
return `${this[key]}`;
},
set(value) {
console.log(`[debug]: SET <${key}>, VALUE <${this[key]}>`);
this[key] = value;
}
});
}
Run Code Online (Sandbox Code Playgroud)
这个想法是getter和setter可以从具有任意数量属性的基础对象生成。
当我console.log()得到结果对象时,我得到这个:
{
TEST_FLAG: [Getter/Setter],
FRUIT: [Getter/Setter],
ID: [Getter/Setter]
}
Run Code Online (Sandbox Code Playgroud)
这表明工厂循环已经起作用。但是,当我这样做时:
dynamicResources.FRUIT = 'berry';
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
set: function set(value) { …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像列表的字符串:
activeStateString = "['11', '20', '0']"
Run Code Online (Sandbox Code Playgroud)
我想将其定义为Python中的列表.我知道我可以开始过滤并拆分它并重建一个新列表但是我必须进入循环等.在Python中是否有办法将字符串从"字符串"直接提升到列表?所以一旦转换:
activeStateString -> activeStateList
Run Code Online (Sandbox Code Playgroud)
我明白了:
11
Run Code Online (Sandbox Code Playgroud)
对于:
print activeStateList[0]
Run Code Online (Sandbox Code Playgroud)
谢谢
(Python 2.6)
我一直在这里查看与上下文日志记录相关的示例: Logging Cookbook
但是,我无法使以下示例正常工作。该示例应演示自定义适配器的使用,并继续如下操作:
# Here is a simple example:
class CustomAdapter(logging.LoggerAdapter):
"""
This example adapter expects the passed in dict-like object to have a
'connid' key, whose value in brackets is prepended to the log message.
"""
def process(self, msg, kwargs):
return '[%s] %s' % (self.extra['connid'], msg), kwargs
# which you can use like this:
logger = logging.getLogger(__name__)
adapter = CustomAdapter(logger, {'connid': some_conn_id})
# Then any events that you log to the adapter will have the value of some_conn_id …Run Code Online (Sandbox Code Playgroud)