就像 subprocess.Popen( target=, cwd=) 一样,它可以指定自己的本地工作目录。我不想每次都指定绝对路径,因为简单总比复杂好。os.chdir() 根本不起作用,因为它设置了一个全局变量(对吧?)。只要有多个线程, os.chdir() 就会失败。有什么建议?谢谢!
我只是尝试了 jorgenkg 的代码并稍作修改,您可能会明白我为什么要问这个问题。这是代码。
import os
import threading
import time
class child(threading.Thread):
def run(self ):
for i in range(1,3):
print "I am " + str(threading.current_thread())[7:15] + " in " + os.getcwd() + '\r\n'
time.sleep(2)
child().start() # prints "/username/path"
os.chdir('C://') # The process is changing directory
child().start() # prints "/"
Run Code Online (Sandbox Code Playgroud)
这是结果。
I am Thread-1 in C:\Python27\Projects
I am Thread-2 in C:\
I am Thread-1 in C:\
I am Thread-2 in C:\
Run Code Online (Sandbox Code Playgroud)
您可以看到,在调用 …
. 大家好。
我最近尝试在我的情节中添加文本对象。但是当我放大文本时,文本大小保持不变。我想要的是放大时文本大小会增加,缩小时会减小。
import matplotlib as mpl
fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.text('','', '',position=[0.5,0.5], text='Y', fontsize='xx-small' )
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。谢谢~
补充-UTC+8 30/04/2013 9:40 AM
感谢 tcaswell 的建议。TextPath 确实实现了我的部分目的。
我发现 matplotlib 官方网站上没有关于 textpath 的文档,所以我查看源代码以了解它的作用。最后,我得到了一个不出色但令人满意的结果,如下所示。
from matplotlib.textpath import TextPath
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.path import Path
fig=plt.figure()
ax1=fig.add_subplot(111)
tp1=TextPath((0.5,0.5), r'How do you turn this on?', size=1)
polygon=tp1.to_polygons()
for a in polygon:
p1=patches.Polygon(a)
ax1.add_patch(p1)
Run Code Online (Sandbox Code Playgroud)
这段代码不太好的部分是它不支持旋转并将文本导出为填充多边形。有没有简单的方法来旋转文本?我可以将文本导出为非填充多边形吗?
"大家好.
我发现子类化ndarray时有一种奇怪的行为.
import numpy as np
class fooarray(np.ndarray):
def __new__(cls, input_array, *args, **kwargs):
obj = np.asarray(input_array).view(cls)
return obj
def __init__(self, *args, **kwargs):
return
def __array_finalize__(self, obj):
return
a=fooarray(np.random.randn(3,5))
b=np.random.randn(3,5)
a_sum=np.sum(a,axis=0,keepdims=True)
b_sum=np.sum(b,axis=0, keepdims=True)
print a_sum.ndim #1
print b_sum.ndim #2
Run Code Online (Sandbox Code Playgroud)
如您所见,该keepdims参数对我的子类不起作用fooarray.它失去了一个轴.我怎能不避免这个问题?或者更一般地说,我怎样才能正确地将numpy ndarray子类化?
我只是不明白为什么我们需要使用@staticmethod.让我们从一个例子开始吧.
class test1:
def __init__(self,value):
self.value=value
@staticmethod
def static_add_one(value):
return value+1
@property
def new_val(self):
self.value=self.static_add_one(self.value)
return self.value
a=test1(3)
print(a.new_val) ## >>> 4
class test2:
def __init__(self,value):
self.value=value
def static_add_one(self,value):
return value+1
@property
def new_val(self):
self.value=self.static_add_one(self.value)
return self.value
b=test2(3)
print(b.new_val) ## >>> 4
Run Code Online (Sandbox Code Playgroud)
在上面的示例static_add_one中,两个类中的方法在计算中不需要类(self)的实例.
该方法static_add_one在类test1是由装饰@staticmethod和正常工作.
但与此同时,static_add_one类test2中没有@staticmethod装饰的方法也可以通过使用self在参数中提供但完全不使用它的技巧来正常工作.
那么使用的好处是@staticmethod什么?它会改善性能吗?或者仅仅是因为python的禅宗指出" 明确比隐含更好 "?
如果我尝试按如下方式创建一个字符串数组,我将得到一个连接的字符串(或1x6数组?).
>> [ 'A1' 'A2' 'A3']
ans = A1A2A3
Run Code Online (Sandbox Code Playgroud)
我期望获得的是一个包含3个元素的字符串数组['A1' 'A2' 'A3'].
如果我尝试首先创建一个垂直字符串数组并转置它,我会得到:
>> transpose([ 'A1'; 'A2'; 'A3'])
ans =
AAA
123
Run Code Online (Sandbox Code Playgroud)
这与创建字符串列表时的python有很大不同.我不知道为什么'A1','A2'和'A3'不是分开而是连在一起.似乎背后有一些原则我不太明白.希望有人能解释.谢谢!
当我将下面的代码与明显错误的 http 代理一起使用时,该requests模块仍然可以从 url 获取。怎么可能?这是否意味着requests不会使用http代理?如果是这样,它是否也不会使用 https 代理?我需要确认我的发布和获取是通过代理完成的。
import requests
url=r'https://stackoverflow.com/questions'
proxies={'http':'http://asdasdasd:80'}
with requests.session() as s:
resp = s.get(url, proxies=proxies)
print resp
print resp.text
Run Code Online (Sandbox Code Playgroud)