我的函数foo接受一个things在内部变成列表的参数。
def foo(things):
things = list(things)
# more code
Run Code Online (Sandbox Code Playgroud)
该list构造函数接受任何可迭代。
但是,注释thingswithtyping.Iterable并没有给用户一个线索,即迭代必须是有限的,而不是像itertools.count().
在这种情况下使用的正确类型提示是什么?
请考虑脚本 foo.bash
#!/bin/env bash
echo "$@"
echo
for x in "${@:0:$# + 1}" ; do
echo "$x"
done
Run Code Online (Sandbox Code Playgroud)
然后./foo.bash a b c给出输出
a b c
./foo.bash
a
b
c
Run Code Online (Sandbox Code Playgroud)
据我了解文档,@保存位置参数,所以输出echo "$@"对我来说很有意义。
但是为什么for循环会打印脚本名称?
编辑:
我在 Windows 上使用 Git BASH,echo $BASH_VERSION产生 4.4.23(1)-release
假设我们有一个复杂的 API 函数,它是从某个库中导入的。
def complex_api_function(
number, <lots of positional arguments>,
<lots of keyword arguments>):
'''really long docstring'''
# lots of code
Run Code Online (Sandbox Code Playgroud)
我想围绕该函数编写一个简单的包装器以进行微小的更改。例如,应该可以将第一个参数作为字符串传递。如何记录这个?我考虑了以下选项:
def my_complex_api_function(number_or_str, *args, **kwargs):
'''
Do something complex.
Like `complex_api_function`, but first argument can be a string.
Parameters
----------
number_or_str : int or float or str
Can be a number or a string that can be interpreted as a float.
<copy paste description from complex_api_function docstring>
*args
Positional arguments passed to `complex_api_function`.
**kwargs
Keyword arguments …Run Code Online (Sandbox Code Playgroud) 是否有typing在文档字符串中使用类型别名或对象的最佳实践?
这个问题可能会吸引基于意见的答案。但也可能是有一个被广泛接受的约定或对特定解决方案的外部工具支持。
示例:函数返回一个包含字符串键和值的字典。您会将什么类型放入“退货”部分下的文档字符串中?(我使用的是熊猫风格的文档字符串。)
选项1:只是说它是一个字典。
import typing
strstrdict = typing.Dict[str, str]
def foo() -> strstrdict:
'''
bla bla
Returns
-------
dict
A dictionary with string keys and values that represents ...
'''
# code
Run Code Online (Sandbox Code Playgroud)
选项 2:使用类型别名。
import typing
strstrdict = typing.Dict[str, str]
def foo() -> strstrdict:
'''
bla bla
Returns
-------
strstrdict
A dictionary with string keys and values that represents ...
'''
# code
Run Code Online (Sandbox Code Playgroud)
选项 3:放入"typing.Dict[str, str]"文档字符串。
import typing
strstrdict …Run Code Online (Sandbox Code Playgroud) 题
我有一个数据帧 untidy
attribute value
0 age 49
1 sex M
2 height 176
3 age 27
4 sex F
5 height 172
Run Code Online (Sandbox Code Playgroud)
其中'attribute'列中的值定期重复.期望的输出是tidy
age sex height
0 49 M 176
1 27 F 172
Run Code Online (Sandbox Code Playgroud)
(行和列顺序或其他标签无关紧要,我可以自己清理它.)
实例化代码:
untidy = pd.DataFrame([['age', 49],['sex', 'M'],['height', 176],['age', 27],['sex', 'F'],['height', 172]], columns=['attribute', 'value'])
tidy = pd.DataFrame([[49, 'M', 176], [27, 'F', 172]], columns=['age', 'sex', 'height'])
Run Code Online (Sandbox Code Playgroud)
尝试
这看起来像一个简单的数据透视操作,但我的初始方法引入了NaN值:
>>> untidy.pivot(columns='attribute', values='value')
attribute age height sex
0 49 NaN NaN
1 NaN NaN …Run Code Online (Sandbox Code Playgroud) 问题
考虑以下布局:
package/
main.py
math_helpers/
mymath.py
__init__.py
Run Code Online (Sandbox Code Playgroud)
mymath.py包含:
import math
def foo():
pass
Run Code Online (Sandbox Code Playgroud)
我希望main.py能够使用mymath.py这样的代码:
import math_helpers
math_helpers.foo()
Run Code Online (Sandbox Code Playgroud)
为此,__init__.py包含:
from .mymath import *
Run Code Online (Sandbox Code Playgroud)
然而,导入的模块mymath.py现在位于math_helpers命名空间中,例如math_helpers.math可以访问。
目前的方法
我在 的末尾添加以下内容mymath.py。
import types
__all__ = [name for name, thing in globals().items()
if not (name.startswith('_') or isinstance(thing, types.ModuleType))]
Run Code Online (Sandbox Code Playgroud)
这似乎可行,但这是正确的方法吗?
我正在尝试构建封装过程的回归器
理想情况下,最终用户应该能够在不知道目标转换的内部结构的情况下使用回归器。开发人员应提供实现变换和逆变换逻辑的函数。
在sklearn.compose.TransformedTargetRegressor我的帮助下,我能够构建一个线性回归模型,该模型接受时间戳作为目标,并在内部将它们转换为自 1970-01-01 00:00:00(Unix 纪元)以来进化的秒数。该fit和predict预期方法已经工作。
import pandas as pd
from sklearn.compose import TransformedTargetRegressor
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import FunctionTransformer
_check_inverse = False
# helper to convert a 2D numpy array of timestamps to a 2D array of seconds
def _to_float(timestamps):
deltas = pd.DataFrame(timestamps).sub(pd.Timestamp(0))
return deltas.apply(lambda s: s.dt.total_seconds()).values
# helper to convert a 2D numpy array of seconds to a 2D array of timestamps
def _to_timestamp(seconds):
return …Run Code Online (Sandbox Code Playgroud) Github文档说
使用快进选项合并具有压缩提交的拉取请求。
这与我对正在发生的事情的心理模型(如下图所示)相冲突,如果能得到一些澄清,我将不胜感激。
初始情况,功能应该被“压缩并合并”到主版本中:
挤压后合并前的情况:
现在将功能合并到主版本中时如何执行快进合并?
编辑:所以从评论来看,似乎发生的实际上是“挤压和变基”而不是“挤压和合并”。它是否正确?
我在默认命名空间中有一个 nginx pod,并且有一个公开该 pod 的 ClusterIP 服务。
\n$ kubectl run nginx-pod --image=nginx\n$ kubectl expose po nginx-pod --name=nginx-service --port=8080 --target-port=80 --type=ClusterIP\nRun Code Online (Sandbox Code Playgroud)\n我可以从集群内部通过其内部 IP 访问该服务。
\n$ kubectl get svc nginx-service\nNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE\nnginx-service ClusterIP 10.100.5.218 <none> 8080/TCP 2m49s\n$ wget -O- 10.100.5.218:8080\nRun Code Online (Sandbox Code Playgroud)\n--> 200 好
\n我可以从 Pod 内部按名称访问该服务。
\n$ kubectl run tmp -it --rm --image=busybox --command -- /bin/sh -c 'wget -O- nginx-service:8080'\nRun Code Online (Sandbox Code Playgroud)\n--> 200 好
\n但是,为什么我无法从 Pod 外部按名称访问服务?
\n$ wget -O- nginx-service:8080\n …Run Code Online (Sandbox Code Playgroud) python ×6
pandas ×2
bash ×1
docstring ×1
git ×1
github ×1
iterable ×1
kubernetes ×1
networking ×1
scikit-learn ×1
type-hinting ×1
typing ×1