说我有这样一个python Enum
类:
from enum import Enum
class Mood(Enum):
red = 0
green = 1
blue = 2
Run Code Online (Sandbox Code Playgroud)
是否有一种自然的方式来获得物品的总数Mood
?(像,而无需遍历它,或添加一个额外的n
项,或额外的n
classproperty
,等等).
enum
模块是否提供这样的功能?
似乎不可能在R中得到因子矩阵.这是真的吗?如果是,为什么?如果没有,我该怎么办?
f <- factor(sample(letters[1:5], 20, rep=TRUE), letters[1:5])
m <- matrix(f,4,5)
is.factor(m) # fail.
m <- factor(m,letters[1:5])
is.factor(m) # oh, yes?
is.matrix(m) # nope. fail.
dim(f) <- c(4,5) # aha?
is.factor(f) # yes..
is.matrix(f) # yes!
# but then I get a strange behavior
cbind(f,f) # is not a factor anymore
head(f,2) # doesn't give the first 2 rows but the first 2 elements of f
# should I worry about it?
Run Code Online (Sandbox Code Playgroud) 谁能帮我理解这种行为?简而言之:
以下是我将其归结为:
#include <iostream>
#include <vector>
// A base class
struct Base {
// A polymorphic method
virtual void describe() const {
std::cout << "Base" << std::endl;
};
virtual ~Base(){
std::cout << " Base destroyed" << std::endl;
};
};
// A specific interface
struct Interface {
virtual ~Interface(){
std::cout << " Interface Destroyed" << std::endl;
};
virtual void specific() = 0;
};
// A derived class..
struct Derived : public Base, public Interface {
virtual void describe() …
Run Code Online (Sandbox Code Playgroud) 我喜欢使用np.fromiter
,numpy
因为它是一种构建np.array
对象的资源惰性方式.但是,它似乎不支持多维数组,这些数组也非常有用.
import numpy as np
def fun(i):
""" A function returning 4 values of the same type.
"""
return tuple(4*i + j for j in range(4))
# Trying to create a 2-dimensional array from it:
a = np.fromiter((fun(i) for i in range(5)), '4i', 5) # fails
# This function only seems to work for 1D array, trying then:
a = np.fromiter((fun(i) for i in range(5)),
[('', 'i'), ('', 'i'), ('', 'i'), ('', 'i')], 5) …
Run Code Online (Sandbox Code Playgroud) 简而言之:
有没有办法可以为General
模板化的类提供仅代表enum
类型的东西?就像是:
template <typename T> struct General {};
struct EnumSpecific : General<any_enum_type> {};
Run Code Online (Sandbox Code Playgroud)
<int>
在我的情况下太多/不起作用.
我的具体案例:
Holder
类以通用方式处理任何类型的数据.General
类实现依赖于Holder
s'行为的特定算法.General
(如IntSpecific
,DoubleSpecific
,StringSpecific
,MoreSophisticatedTypeSpecific
.)定义如何处理一些具体的Holder
类型.EnumSpecific
规范?以下代码解决了我的问题:
// A templated value holder:
template <typename T>
class Holder {
public:
Holder(T const& t) : _value(t) {};
// generic methods
void generics() {};
// methods concerning the value:
void set(T const& t /*, setInfo …
Run Code Online (Sandbox Code Playgroud) 我从PEP 3131中了解到Python中支持非ASCII标识符,尽管它不被认为是最佳实践.
但是,我得到了这种奇怪的行为,我的 identifier (U+1D70F) seems to be automatically converted to
?
(U + 03C4).
class Base(object):
def __init__(self):
self. = 5 # defined with U+1D70F
a = Base()
print(a.) # 5 # (U+1D70F)
print(a.?) # 5 as well # (U+03C4) ? another way to access it?
d = a.__dict__ # {'?': 5} # (U+03C4) ? seems converted
print(d['?']) # 5 # (U+03C4) ? consistent with the conversion
print(d['']) # KeyError: '' # (U+1D70F) ?! unexpected!
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?为什么会发生这种静默转换?NFKC …
我希望保存一个随机的Vim词典,让我们说:
let dico = {'a' : [[1,2], [3]], 'b' : {'in': "str", 'out' : 51}}
Run Code Online (Sandbox Code Playgroud)
到一个文件.有一个聪明的方法来做到这一点?我可以使用的东西像:
call SaveVariable(dico, "safe.vimData")
let recover = ReadVariable("safe.vimData")
Run Code Online (Sandbox Code Playgroud)
或者我应该用自己的文本文件自己构建一些东西?
根据Rust 结尾的.try_iter()方法的文档,我知道这个迭代器要么产生“None”:Receiver
std::mpsc::channel
就我而言,我想peek
进入通道,而不阻塞,以便确定是否:
问题是如果我..
match my_receiver.try_iter().peekable().peek() {
Some(data) => {/* there is data */}
None => {/* there is no data, but there may be later.. or maybe not, because the channel has maybe hung up, I can't tell! */}
}
Run Code Online (Sandbox Code Playgroud)
..我只能区分两种情况。
receiver
当存在数据时,如何才能查看通道的末尾并区分这三种可能的结果,而不阻塞或消耗数据?
给定unicode 表的这个区域,例如:
...
U+1D44E Dec:119886 MATHEMATICAL ITALIC SMALL A 𝑎
U+1D44F Dec:119887 MATHEMATICAL ITALIC SMALL B 𝑏
U+1D450 Dec:119888 MATHEMATICAL ITALIC SMALL C 𝑐
U+1D451 Dec:119889 MATHEMATICAL ITALIC SMALL D 𝑑
U+1D452 Dec:119890 MATHEMATICAL ITALIC SMALL E 𝑒
U+1D453 Dec:119891 MATHEMATICAL ITALIC SMALL F 𝑓
U+1D454 Dec:119892 MATHEMATICAL ITALIC SMALL G 𝑔
U+1D456 Dec:119894 MATHEMATICAL ITALIC SMALL I 𝑖 # what?!
U+1D457 Dec:119895 MATHEMATICAL ITALIC SMALL J 𝑗
U+1D458 Dec:119896 MATHEMATICAL ITALIC SMALL K 𝑘
U+1D459 Dec:119897 MATHEMATICAL …
Run Code Online (Sandbox Code Playgroud) 我正在关注官方文档,复制并粘贴那里提供的代码:
https://facebook.github.io/react-native/docs/0.56/native-components-ios
但我仍然遇到这个错误
有谁知道文档缺少什么才能使其正常工作?
python ×3
c++ ×2
enums ×2
unicode ×2
arrays ×1
binary ×1
channel ×1
concept ×1
file ×1
generics ×1
identifier ×1
integer ×1
interface ×1
matrix ×1
nonblocking ×1
numpy ×1
peek ×1
polymorphism ×1
python-3.x ×1
r ×1
r-factor ×1
react-native ×1
reflection ×1
rust ×1
save ×1
standards ×1
templates ×1
utf-8 ×1
vector ×1
vim ×1