阅读下面的文章,我设法将一个传奇放在外面.
码:
import matplotlib.pyplot as pyplot
x = [0, 1, 2, 3, 4]
y = [xx*xx for xx in x]
fig = pyplot.figure()
ax = fig.add_subplot(111)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width*0.8, box.height])
ax.plot(x, y)
leg = ax.legend(['abc'], loc = 'center left', bbox_to_anchor = (1.0, 0.5))
#pyplot.show()
fig.savefig('aaa.png', bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)
pyplot.show()显示正确的情节,外面有一个图例.但是当我将其保存为文件时fig.savefig(),图例会被截断.
一些谷歌搜索显示我作为变通方法添加这样bbox_extra_artists=[leg.legendPatch]或bbox_extra_artists=[leg]至savefig(),但既不工作.
这样做的正确方法是什么?Matplotlib版本为0.99.3.
谢谢.
说我有一个bash脚本文件config.sh.它意味着由其他脚本提供源代码,定义的变量用作上层脚本的自定义.
问题是,如果config.sh有一个临时变量并且它的名称与高级脚本的变量冲突,它会破坏上层脚本的变量.
config.sh:
TMP1=abc
CONFIG_INPUT_DIR="$TMP1"/in
CONFIG_OUTPUT_DIR="$TMP1"/out
Run Code Online (Sandbox Code Playgroud)
上层脚本:
TMP1=def
source config.sh
echo $TMP1
Run Code Online (Sandbox Code Playgroud)
最后的echo印刷品abc,而不是def.
我目前的解决方案是将一个随机字符串附加到临时变量名称,使其几乎不可能发生冲突.例如:
TMP1_vFc9Uiew=abc
CONFIG_INPUT_DIR="$TMP1_vFc9Uiew"/in
CONFIG_OUTPUT_DIR="$TMP1_vFc9Uiew"/out
unset TMP1_vFc9Uiew
Run Code Online (Sandbox Code Playgroud)
这是痛苦的,使代码难以阅读,此外不是完美的.
local关键字经过一番搜索,我才知道local关键字.但是,当我简单地声明TMP1的local,bash的抱怨config.sh: line 1: local: can only be used in a function.
所以我的另一个解决方案是将整个配置脚本作为函数包含在内:
function config_func_rZ0Yqkpm() {
local TMP1=abc
CONFIG_INPUT_DIR="$TMP1"/in
CONFIG_OUTPUT_DIR="$TMP1"/out
}
config_func_rZ0Yqkpm
unset config_func_rZ0Yqkpm
Run Code Online (Sandbox Code Playgroud)
这在可维护性和可读性方面比以前的解决方案更好,但是有一些冲突的可能性以及解决方案1.
我想知道更强大和智能的解决方案,没有任何冲突的可能性.
谢谢.
可以通过它来专门化Iterator模板参数value_type吗?
我有一个功能与以下原型.
template<typename InputIterator>
void f(InputIterator first, InputIterator last);
Run Code Online (Sandbox Code Playgroud)
如果InputIterator::value_type是,我想特别处理SomeSpecificType.
系统中安装了Python 2.6.
现在我想使用Python 2.7中引入的模块.因为我没有root权限,所以我在我的主目录下构建并安装了2.7($ HOME/local /)
我在$ HOME/.bashrc中添加了以下内容:
export PATH=$HOME/local/bin:$PATH
export PYTHONPATH=$HOME/local/lib/python2.7:$PYTHONPATH
Run Code Online (Sandbox Code Playgroud)
现在我遇到了我想要解决的两个问题.
新安装的Python 2.7在系统的库路径(/usr/lib/python2.6/site-packages/)中找不到2.6模块.
我应该手动将它添加到PYTHONPATH吗?有没有更好的解决方案?
Python 2.6在启动时抱怨:
'import site' failed; use -v for traceback
Run Code Online (Sandbox Code Playgroud)
我猜它正在尝试加载2.7个模块(在$ HOME/local/lib/python2.7中).调用Python 2.6时是否可以仅加载2.6个模块?
谢谢.
我发现将抽象方法分成两个方法似乎很有用,一个用于公共接口,另一个用于子类重写.
通过这种方式,您可以为输入和输出添加前置条件/后置条件检查,从而可以抵御人为错误.
但我在这里关心的是它是否是蟒蛇可接受的,因为在我的小经验中我从来没有见过像这样的代码.
正常多态性
import abc
class Shape:
"""Abstract base class for shapes"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get_area(self, scale):
"""Calculates the area of the shape, scaled by a factor.
Do not blame for a silly example.
"""
pass
class Rectangle(Shape):
def __init__(self, left, top, width, height):
self.left = left
self.top = top
self.width = width
self.height = height
def get_area(self, scale):
return scale * self.width * self.height
print(Rectangle(10, 10, 40, 40).get_area(3))
# Gosh!... gets tons of 3's
print(Rectangle(10, 10, …Run Code Online (Sandbox Code Playgroud) python ×3
bash ×1
c++ ×1
iterator ×1
legend ×1
matplotlib ×1
oop ×1
polymorphism ×1
python-2.x ×1
pythonpath ×1
templates ×1