Re:Please Help
np.uint8 is unsigned int, range 0~255 >>> np.uint8(256) 0 At 2018-01-27 12:33:22, "" wrote: >import numpy as np >x=np.unit8([250) >print(x) >y=np.unit8([10]) >print(y) >z=x+y >print(z) > > >output > >[250] >[10] >[4] > >My question how is z [4] >-- >https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
name 'aLOCK' is not defined When I add aLOCK = threading.RLock() behind if __name__ == "__main__"
Hi team Good day The problem I meet when I add "aLOCK = threading.RLock()" to PositionB, the program will report error "name 'aLOCK' is not defined ", but when I change this code to PositionA, it can run normally, is there any difference for the code between 'if __name__ == "__main__:"', can you help me, thanks! The file I was attached, please change the extend ".pyx" to ".py", thanks, code as below, tried python2.7 and python3.4: import threading import time from multiprocessing import Process #PositionA aLOCK = threading.RLock() def Save_runnedCMD(filename, strings): aLOCK.acquire() with open(filename, "at") as f: f.write(str(strings) + "\n\r") aLOCK.release() def runCMD(filename): time.sleep(1) cmd = "testtest" Save_runnedCMD(filename, cmd) def Thr_run(filename): t = [] for i in range(2): tt = threading.Thread(target = runCMD, args=(filename,)) tt.start() t.append(tt) for tt in t: tt.join() if __name__ == "__main__": filename = "./testa.log" #PositionB #aLOCK = threading.RLock() while 1: t1 = Process(target=Thr_run, args=(filename, )) t1.start() t1.join() Error info as below: D:\Ap>python testa.py Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner self.run() File "C:\Python34\lib\threading.py", line 869, in run self._target(*self._args, **self._kwargs) File "D:\Ap\testa.py", line 15, in runCMD Save_runnedCMD(filename, cmd) File "D:\Ap\testa.py", line 7, in Save_runnedCMD aLOCK.acquire() NameError: name 'aLOCK' is not defined Exception in thread Thread-2: Traceback (most recent call last): File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner self.run() File "C:\Python34\lib\threading.py", line 869, in run self._target(*self._args, **self._kwargs) File "D:\Ap\testa.py", line 15, in runCMD Save_runnedCMD(filename, cmd) File "D:\Ap\testa.py", line 7, in Save_runnedCMD aLOCK.acquire() NameError: name 'aLOCK' is not defined -- https://mail.python.org/mailman/listinfo/python-list
Re:Re: name 'aLOCK' is not defined When I add aLOCK = threading.RLock() behind if __name__ == "__main__"
Thanks for your reply! Yes, move the code from positionA(can run normally) to positionB(exception with name undefined) I find this content "https://docs.python.org/3.3/tutorial/classes.html#python-scopes-and-namespaces"; But I still don't undewrstand the differenct of scopes-and-namespaces between positionA and positionB, I think the variable "aLock" at these positionA or positionB are all global. The function can find threading.RLock() by name ""aLock". At 2018-08-09 23:51:45, "Karsten Hilbert" wrote: >On Thu, Aug 09, 2018 at 11:16:37PM +0800, xuanwu348 wrote: > >> The problem I meet when I add "aLOCK = threading.RLock()" to PositionB, the >> program will report error "name 'aLOCK' is not defined ", > >You mean to say: *move*, not *add*. > >That is why aLock is out of scope. > > /python3-doc/html/tutorial/classes.html#python-scopes-and-namespaces > >> but when I change this code to PositionA, it can run normally, is there any >> difference for the code between 'if __name__ == "__main__:"', can you help >> me, thanks! >> >> The file I was attached, please change the extend ".pyx" to ".py", thanks, >> code as below, tried python2.7 and python3.4: >> >> import threading >> import time >> from multiprocessing import Process >> >> #PositionA >> aLOCK = threading.RLock() >> >> def Save_runnedCMD(filename, strings): >> aLOCK.acquire() >> with open(filename, "at") as f: >> f.write(str(strings) + "\n\r") >> aLOCK.release() >> >> def runCMD(filename): >> time.sleep(1) >> cmd = "testtest" >> Save_runnedCMD(filename, cmd) >> >> >> def Thr_run(filename): >> t = [] >> for i in range(2): >> tt = threading.Thread(target = runCMD, args=(filename,)) >> tt.start() >> t.append(tt) >> for tt in t: >> tt.join() >> >> if __name__ == "__main__": >> filename = "./testa.log" >> >> #PositionB >> #aLOCK = threading.RLock() >> >> while 1: >> t1 = Process(target=Thr_run, args=(filename, )) >> t1.start() >> t1.join() >> >> Error info as below: >> D:\Ap>python testa.py >> Exception in thread Thread-1: >> Traceback (most recent call last): >> File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner >> self.run() >> File "C:\Python34\lib\threading.py", line 869, in run >> self._target(*self._args, **self._kwargs) >> File "D:\Ap\testa.py", line 15, in runCMD >> Save_runnedCMD(filename, cmd) >> File "D:\Ap\testa.py", line 7, in Save_runnedCMD >> aLOCK.acquire() >> NameError: name 'aLOCK' is not defined >> >> Exception in thread Thread-2: >> Traceback (most recent call last): >> File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner >> self.run() >> File "C:\Python34\lib\threading.py", line 869, in run >> self._target(*self._args, **self._kwargs) >> File "D:\Ap\testa.py", line 15, in runCMD >> Save_runnedCMD(filename, cmd) >> File "D:\Ap\testa.py", line 7, in Save_runnedCMD >> aLOCK.acquire() >> NameError: name 'aLOCK' is not defined >> -- >> https://mail.python.org/mailman/listinfo/python-list > >-- >GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B >-- >https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
syntax for ellipsis like as "net.blobs['data'].data[...] = transformed_image"
Hi buddies Have a good weekend! I have read some code in caffe, but I confused at "net.blobs['data'].data[...] = transformed_image". The code can be find in this link:https://github.com/BVLC/caffe/blob/master/examples/00-classification.ipynb import caffe model_def = os.path.join(caffe_root, "models/bvlc_alexnet/deploy.prototxt") model_weights = os.path.join(caffe_root, "models/bvlc_alexnet/bvlc_alexnet.caffemodel") net = caffe.Net(model_def, model_weights, caffe.TEST) net.blobs['data'].reshape(50, 3, 227,227) net.blobs['data'].data[...] = transformed_image 1. For ellipsis, is there syntax in python like this " net.blobs['data'].data[...]"? I have checked it which the type is "" 2. Could you provide some samples in which need to use ellipsis. But in python, ellipsis define as below, I did not find the relationship to "net.blobs['data'].data[...]": Help on ellipsis object: class ellipsis(object) | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __reduce__(...) | helper for pickle | | __repr__(self, /) | Return repr(self). Thanks for your help! Best Regards -- https://mail.python.org/mailman/listinfo/python-list
Re:syntax for ellipsis like as "net.blobs['data'].data[...] = transformed_image"
Thanks too, I have find the answer from "https://stackoverflow.com/questions/772124/what-does-the-python-ellipsis-object-do"; This came up in another question recently. I'll elaborate on my answer from there: Ellipsis is an object that can appear in slice notation. For example: myList[1:2, ..., 0] Its interpretation is purely up to whatever implements the __getitem__ function and sees Ellipsis objects there, but its main (and intended) use is in the numeric python extension, which adds a multidimensional array type. Since there are more than one dimensions, slicing becomes more complex than just a start and stop index; it is useful to be able to slice in multiple dimensions as well. E.g., given a 4x4 array, the top left area would be defined by the slice [:2,:2]: >>> a array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12], [13, 14, 15, 16]]) >>> a[:2,:2] # top left array([[1, 2], [5, 6]]) Extending this further, Ellipsis is used here to indicate a placeholder for the rest of the array dimensions not specified. Think of it as indicating the full slice [:] for all the dimensions in the gap it is placed, so for a 3d array, a[...,0] is the same as a[:,:,0] and for 4d, a[:,:,:,0], similarly, a[0,...,0] is a[0,:,:,0] (with however many colons in the middle make up the full number of dimensions in the array). Interestingly, in python3, the Ellipsis literal (...) is usable outside the slice syntax, so you can actually write: >>> ... Ellipsis Other than the various numeric types, no, I don't think it's used. As far as I'm aware, it was added purely for numpy use and has no core support other than providing the object and corresponding syntax. The object being there didn't require this, but the literal "..." support for slices did. 在 2019-10-26 22:51:31,"xuanwu348" 写道: Hi buddies Have a good weekend! I have read some code in caffe, but I confused at "net.blobs['data'].data[...] = transformed_image". The code can be find in this link:https://github.com/BVLC/caffe/blob/master/examples/00-classification.ipynb import caffe model_def = os.path.join(caffe_root, "models/bvlc_alexnet/deploy.prototxt") model_weights = os.path.join(caffe_root, "models/bvlc_alexnet/bvlc_alexnet.caffemodel") net = caffe.Net(model_def, model_weights, caffe.TEST) net.blobs['data'].reshape(50, 3, 227,227) net.blobs['data'].data[...] = transformed_image 1. For ellipsis, is there syntax in python like this " net.blobs['data'].data[...]"? I have checked it which the type is "" 2. Could you provide some samples in which need to use ellipsis. But in python, ellipsis define as below, I did not find the relationship to "net.blobs['data'].data[...]": Help on ellipsis object: class ellipsis(object) | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __reduce__(...) | helper for pickle | | __repr__(self, /) | Return repr(self). Thanks for your help! Best Regards -- https://mail.python.org/mailman/listinfo/python-list
about the function in class
Hi, all There are some methods to define functions in a class, include member functions, static method, class method. My question is that which type of function for "decorate(f),diff_con(f)" I think these functions all were belong to member function, (f <-->self) f can be replace by self. but how to explain "decorate(f)", "@decorate(f)" if f == self; then in wrapper function: res = self(*args, **kwargs) and self means Diff_methods("somgthing") <==> Diff_methods("somgthing")(*args, **kwargs) But it's not correct. thanks lass Diff_methods: def __init__(self, something): self.something = something def decorate(f): @wraps(f) def wrapper(*args, **kwargs): start_time = time.time() res = f(*args, **kwargs) elapse = time.time() - start_time return res, elapse return wrapper def diff_con(f): print(f.something) @decorate def member_function(self, n): return 2 << n @staticmethod def static_function(): pass @classmethod def class_method(cls): pass if __name__ == "__main__": s = Diff_methods("something") print(s.member_function(10)) s.diff_con() -- https://mail.python.org/mailman/listinfo/python-list
why the connection set with “keep live” in urllib.request always set to be“closed, thanks
hi everyone Good day, the code as below, I try to make a long connection by set the connection to "keep-alive", but actually it's not as expected. Why? Thanks import urllib.request as req headers = {"authorization": "Bearer {}".format(self.token), "Content-Type": "multipart/form-data; boundary={}".format(self.boundary), "Connection": "keep-alive"} request = req.Request(event_uri, headers=headers, data=data.encode("utf-8")) Accept-Encoding: identity Content-Length: 705 Host: 10.10.1.114:9443 User-Agent: Python-urllib/3.8 Authorization: Bearer a1.BUiPZxxCQdH2c9uegml Content-Type: multipart/form-data; boundary=--testtest-- Connection: close Best Regards -- https://mail.python.org/mailman/listinfo/python-list
Re:Re: why the connection set with “keep live” in urllib.request always set to be“closed, thanks
This means I want to structure my request header with "Connection: keep alive" for the server which support connect alive, But I checked the requeset header send by myself, the connection still closed, no affect by set And I also found this in python document: == urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None) Open the URL url, which can be either a string or a Request object. data must be an object specifying additional data to be sent to the server, or None if no such data is needed. See Request for details. urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests. === Does it mean urllib.request don't support 'keep alive'? Thanks 在 2020-08-18 04:51:03,"Barry" 写道: > > >> On 17 Aug 2020, at 18:23, xuanwu348 wrote: >> >> hi everyone >> >> >> Good day, the code as below, I try to make a long connection by set the >> connection to "keep-alive", but actually it's not as expected. Why? Thanks > >What this means is. Please web server keep the connect alive if you can. >The web server is allowed to close the connection if it wants too. > >Barry > >> >> >> import urllib.request as req >> headers = {"authorization": "Bearer {}".format(self.token), >> "Content-Type": "multipart/form-data; >> boundary={}".format(self.boundary), >> "Connection": "keep-alive"} >> request = req.Request(event_uri, headers=headers, data=data.encode("utf-8")) >> >> >> >> >> >> Accept-Encoding: identity >> >> Content-Length: 705 >> >> Host: 10.10.1.114:9443 >> >> User-Agent: Python-urllib/3.8 >> >> Authorization: Bearer a1.BUiPZxxCQdH2c9uegml >> >> Content-Type: multipart/form-data; boundary=--testtest-- >> >> Connection: close >> >> >> >> >> >> >> >> Best Regards >> -- >> https://mail.python.org/mailman/listinfo/python-list >> -- https://mail.python.org/mailman/listinfo/python-list
Re:Re: why the connection set with “keep live” in urllib.request always set to be“closed, thanks
Thanks all, I had met this problem was when I did restapi test. I send a request by python script and the time processed by server very short, and another request send by postman, the time seems normal, I compare the diff between these two request, obvious difference is the connection, which in postman request was keep-alive and in python script's was closed. So I try to found out if it was the cause. And at last we found that not the reason triggered this problem, and the really reason lead the problem is the parameter I transfered in json body. and thanks for the links you provid, I will read into them. Best Regards At 2020-08-19 02:35:42, "Barry Scott" wrote: On 18 Aug 2020, at 02:16, xuanwu348 wrote: This means I want to structure my request header with "Connection: keep alive" for the server which support connect alive, But I checked the requeset header send by myself, the connection still closed, no affect by set And I also found this in python document: == urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None) Open the URL url, which can be either a string or a Request object. data must be an object specifying additional data to be sent to the server, or None if no such data is needed. See Request for details. urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests. === Does it mean urllib.request don't support 'keep alive'? The term in HTTP 1.1 is persistent connections. See https://tools.ietf.org/html/rfc2616#section-8.1 The server will assume persistent connections for the reasons documented. Since you have seen requests sending "Connection: close" that is saying to the server that persistent connection is not required. The server will send the response and close its end. Sounds like you need to use another library to allow you to use the persistent connection. If you know your way around TCP sockets and protocols this is not that hard to write the code to do persistent connections for a client. Maybe look at a library like twisted? Its got all the building blocks you need to handle persistent connections. Barry Thanks 在 2020-08-18 04:51:03,"Barry" 写道: > > >> On 17 Aug 2020, at 18:23, xuanwu348 wrote: >> >> hi everyone >> >> >> Good day, the code as below, I try to make a long connection by set the >> connection to "keep-alive", but actually it's not as expected. Why? Thanks > >What this means is. Please web server keep the connect alive if you can. >The web server is allowed to close the connection if it wants too. > >Barry > >> >> >> import urllib.request as req >> headers = {"authorization": "Bearer {}".format(self.token), >> "Content-Type": "multipart/form-data; >> boundary={}".format(self.boundary), >> "Connection": "keep-alive"} >> request = req.Request(event_uri, headers=headers, data=data.encode("utf-8")) >> >> >> >> >> >> Accept-Encoding: identity >> >> Content-Length: 705 >> >> Host: 10.10.1.114:9443 >> >> User-Agent: Python-urllib/3.8 >> >> Authorization: Bearer a1.BUiPZxxCQdH2c9uegml >> >> Content-Type: multipart/form-data; boundary=--testtest-- >> >> Connection: close >> >> >> >> >> >> >> >> Best Regards >> -- >> https://mail.python.org/mailman/listinfo/python-list >> -- https://mail.python.org/mailman/listinfo/python-list