Re: Discerning "Run Environment"
On 5/18/22, Chris Angelico wrote: > > Real solution? Set the command prompt to codepage 65001. Then it > should be able to handle all characters. (Windows-65001 is its alias > for UTF-8.) I suggest using win_unicode_console for Python versions prior to 3.6: https://pypi.org/project/win_unicode_console This package uses the console's native 16-bit character support with UTF-16 text, as does Python 3.6+. Compared to the console's incomplete and broken support for UTF-8, the console's support for UTF-16 (or just UCS-2 prior to Windows 10) is far more functional and reliable across commonly used versions of Windows 7, 8, and 10. Reading console input as UTF-8 is still limited to ASCII up to and including Windows 11, which for me is a showstopper. Non-ASCII characters are read as null bytes, which is useless. Support for writing UTF-8 to the console screen buffer is implemented correctly in recent builds of Windows 10 and 11, and mostly correct in Windows 8. Prior to Windows 8, writing UTF-8 to the console is badly broken. It returns the number of UTF-16 codes written instead of the number of bytes written, which confuses buffered writers into writing a lot of junk to the screen. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python & nmap
On 2022-05-18, ^Bart wrote: > THE INPUT > - > import nmap > nm.scan(hosts='192.168.205.0/24', arguments='-n -sP -PE -PA21,23,80,3389') > hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()] > for host, status in hosts_list: > print('{0}:{1}'.host) > > THE OUTPUT > - > Traceback (most recent call last): >File "/home/gabriele/Documenti/Python/nmap.py", line 1, in > import nmap >File "/home/gabriele/Documenti/Python/nmap.py", line 2, in > nm.scan(hosts='192.168.205.0/24', arguments='-n -sP -PE > -PA21,23,80,3389') > NameError: name 'nm' is not defined You forgot the second line (after 'import nmap' and before 'nm.scan()'): nm = nmap.PortScanner() -- https://mail.python.org/mailman/listinfo/python-list
Python & nmap
Hi guys, i need to copy some files from a Debian client to all linux embedded clients. I know the linux commands like: # scp "my_file" root@192.168.205.x/my_directory But... I have to upload 100 devices, I have a lan and a dhcp server just for this work and I'd like to make a script by Python which can: 1) To scan the lan 2) To find which ips are "ready" 3) To send files to all of the "ready" clients 4) After I see on the display of these clients the successfully update I remove from the lan them and I put them to the box to send them to our customers. I found https://pypi.org/project/python-nmap/ and I followed the line "To check the network status" but... it doesn't work. THE INPUT - import nmap nm.scan(hosts='192.168.205.0/24', arguments='-n -sP -PE -PA21,23,80,3389') hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()] for host, status in hosts_list: print('{0}:{1}'.host) THE OUTPUT - Traceback (most recent call last): File "/home/gabriele/Documenti/Python/nmap.py", line 1, in import nmap File "/home/gabriele/Documenti/Python/nmap.py", line 2, in nm.scan(hosts='192.168.205.0/24', arguments='-n -sP -PE -PA21,23,80,3389') NameError: name 'nm' is not defined Regards. ^Bart -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
#!/usr/bin/env python3 ''' NewsGroup comp.lang.python Subject .. Convert the decimal numbers expressed in a numpy.ndarray into a matrix representing elements in fractiona Date . 2022-05-16 Post_By .. hongy... Edit_By .. Stanley C. Kitching ''' import numpy as np from fractions import Fraction b = [ [ 0.0 , -1.0 , 0.0 , 0.25 ] , [ 1.0 , 0.0 , 0.0 , 0.25 ] , [ 0.0 , 0.0 , 1.0 , 0.25 ] , [ 0.0 , 0.0 , 0.0 , 1.0 ] ] a = [ ] print( '\n b \n' ) for row in b : arow = [] print( '' , row ) for dec_x in row : frac_x = Fraction( dec_x ) arow.append( frac_x ) a.append( arow ) # using f-string format print( '\n a \n' ) for row in a : for item in row : print( f'{item} ' , end = '' ) print() # -- -- Stanley C. Kitching Human Being Phoenix, Arizona -- https://mail.python.org/mailman/listinfo/python-list
Re: tail
On 17May2022 22:45, Marco Sulla wrote: >Well, I've done a benchmark. timeit.timeit("tail('/home/marco/small.txt')", globals={"tail":tail}, number=10) >1.5963431186974049 timeit.timeit("tail('/home/marco/lorem.txt')", globals={"tail":tail}, number=10) >2.5240604374557734 timeit.timeit("tail('/home/marco/lorem.txt', chunk_size=1000)", globals={"tail":tail}, number=10) >1.8944984432309866 This suggests that the file size does not dominate uour runtime. Ah. _Or_ that there are similar numbers of newlines vs text in the files so reading similar amounts of data from the end. If the "line desnity" of the files were similar you would hope that the runtimes would be similar. >small.txt is a text file of 1.3 KB. lorem.txt is a lorem ipsum of 1.2 >GB. It seems the performance is good, thanks to the chunk suggestion. > >But the time of Linux tail surprise me: > >marco@buzz:~$ time tail lorem.txt >[text] > >real0m0.004s >user0m0.003s >sys0m0.001s > >It's strange that it's so slow. I thought it was because it decodes >and print the result, but I timed You're measuring different things. timeit() tries hard to measure just the code snippet you provide. It doesn't measure the startup cost of the whole python interpreter. Try: time python3 your-tail-prog.py /home/marco/lorem.txt BTW, does your `tail()` print output? If not, again not measuring the same thing. If you have the source of tail(1) to hand, consider getting to the core and measuring `time()` immediately before and immediately after the central tail operation and printing the result. Also: does tail(1) do character set / encoding stuff? Does your Python code do that? Might be apples and oranges. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue sending data from C++ to Python
Am 18.05.22 um 16:08 schrieb Pablo Martinez Ulloa: I have been using your C++ Python API, in order to establish a bridge from C++ to Python. We want to do this, as we have a tactile sensor, which only has a library developed in C++, but we want to obtain the data in real time in Python to perform tests with a robotic arm and gripper. The problem we are facing is with transmitting the data into Python. We are using the function Py_BuildValue, and it seems to be working, but only for sending one value at a time, and we want to transmit 54 numbers The usual way to pass an array of numbers into Python is by means of a Numpy Array. In order to do that, you need to include arrayobject.h and then use PyArray_SimpleNew to create an array of numbers as a Python object which you can return https://numpy.org/devdocs/reference/c-api/array.html#c.PyArray_SimpleNew Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue sending data from C++ to Python
On 2022-05-18 15:08, Pablo Martinez Ulloa wrote: Hello, I have been using your C++ Python API, in order to establish a bridge from C++ to Python. We want to do this, as we have a tactile sensor, which only has a library developed in C++, but we want to obtain the data in real time in Python to perform tests with a robotic arm and gripper. The problem we are facing is with transmitting the data into Python. We are using the function Py_BuildValue, and it seems to be working, but only for sending one value at a time, and we want to transmit 54 numbers. When transmitting a single value retrieved from the sensor into Python in a double or string format, it works, but when we make the string bigger it gives us an error with the utf-8 encoding. Also when sending a tuple of doubles or strings, it works when the tuple is composed of a single element, but not when we send 2 or more values. Is there any way of fixing this issue? Or is it just not possible to transmit such a large amount of data? I find it easier if I have some code in front of me to 'criticise'! Have you thought about building and returning, say, a list instead? /* Error checking omitted. */ PyObject* list; list = PyList_New(0); PyList_Append(list, PyLong_FromSsize_t(1)); PyList_Append(list, PyFloat_FromDouble(2.0)); ... -- https://mail.python.org/mailman/listinfo/python-list
Re: Request for assistance (hopefully not OT)
Chris Angelico writes: > On Wed, 18 May 2022 at 04:05, Loris Bennett > wrote: >> >> [snip (26 lines)] >> >> > I think you had a problem before that. Debian testing is not an >> > operating system you should be using if you have a fairly good >> > understanding of how Debian (or Linux in general) works. >> >> Should be >> >> I think you had a problem before that. Debian testing is not an >> operating system you should be using *unless* you have a fairly good >> understanding of how Debian (or Linux in general) works. >> >> [snip (62 lines)] >> > > Oh! My bad, didn't see this correction, sorry. With this adjustment, > the comment is a bit more reasonable, although I'd still say it's > generally fine to run Debian Testing on a personal desktop machine; > there are a number of distros that base themselves on Testing. > > But yes, "unless" makes much more sense there. It's lucky I never got "if" and "unless" mixed up when I used to program in Perl ;-) Yes, there are a number of distros based on Debian Testing, but those tend to be aimed more at sysadmins (e.g. Kali and Grml) than people just starting out with Linux. However, with plain old Debian Testing you need to be able to deal with things occasionally not working properly. As the Debian people say about Testing: "If it doesn't work for you, then there's a good chance it's broken." And that's even before you delete part of the OS with 'rm'. Cheers, Loris -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
On Tue, 17 May 2022 17:20:54 +0100, MRAB declaimed the following: >As it's just a simple replacement, I would've thought that the 'obvious' >solution would be: > a = a.replace("'", "") Mea culpa... Guess it's time for me to review the library reference for basic data types again. I'm so used to the .join(.split()) (usually for other purposes -- like a quick&dirty TSV/CSV formatting without importing the csv module). The only firm item is that I do not look at any regular expression module if I can come up with something using native data type methods -- and using the overhead of re with just simple text [ie; nothing that might be called an "expression" designed to match /varying/ content) seems really inefficient. -- Wulfraed Dennis Lee Bieber AF6VN wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list
Re: tail
Well, I've done a benchmark. >>> timeit.timeit("tail('/home/marco/small.txt')", globals={"tail":tail}, >>> number=10) 1.5963431186974049 >>> timeit.timeit("tail('/home/marco/lorem.txt')", globals={"tail":tail}, >>> number=10) 2.5240604374557734 >>> timeit.timeit("tail('/home/marco/lorem.txt', chunk_size=1000)", >>> globals={"tail":tail}, number=10) 1.8944984432309866 small.txt is a text file of 1.3 KB. lorem.txt is a lorem ipsum of 1.2 GB. It seems the performance is good, thanks to the chunk suggestion. But the time of Linux tail surprise me: marco@buzz:~$ time tail lorem.txt [text] real0m0.004s user0m0.003s sys0m0.001s It's strange that it's so slow. I thought it was because it decodes and print the result, but I timed timeit.timeit("print(tail('/home/marco/lorem.txt').decode('utf-8'))", globals={"tail":tail}, number=10) and I got ~36 seconds. It seems quite strange to me. Maybe I got the benchmarks wrong at some point? -- https://mail.python.org/mailman/listinfo/python-list
Issue sending data from C++ to Python
Hello, I have been using your C++ Python API, in order to establish a bridge from C++ to Python. We want to do this, as we have a tactile sensor, which only has a library developed in C++, but we want to obtain the data in real time in Python to perform tests with a robotic arm and gripper. The problem we are facing is with transmitting the data into Python. We are using the function Py_BuildValue, and it seems to be working, but only for sending one value at a time, and we want to transmit 54 numbers. When transmitting a single value retrieved from the sensor into Python in a double or string format, it works, but when we make the string bigger it gives us an error with the utf-8 encoding. Also when sending a tuple of doubles or strings, it works when the tuple is composed of a single element, but not when we send 2 or more values. Is there any way of fixing this issue? Or is it just not possible to transmit such a large amount of data? Thank you. Best regards, *Pablo Martinez Ulloa* PhD Candidate, School of Electrical and Electronic Engineering, R324A University College Dublin, Belfield, Dublin 4, Ireland -- https://mail.python.org/mailman/listinfo/python-list
Re: Discerning "Run Environment"
On Wed, 18 May 2022 at 19:40, Stephen Tucker wrote: > > Hi, > > I am a Windows 10 user still using Python 2.x (for good reasons, I assure > you.) > > I have a Python 2.x module that I would like to be able to use in a variety > of Python 2.x programs. The module outputs characters to the user that are > only available in the Unicode character set. > > I have found that the selection of characters in that set that are > available to my software depends on whether, for example, the program is > being run during an IDLE session or at a Command Prompt. Real solution? Set the command prompt to codepage 65001. Then it should be able to handle all characters. (Windows-65001 is its alias for UTF-8.) > I am therefore needing to include logic in this module that (a) enables it > to output appropriate characters depending on whether it is being run > during an IDLE session or at a command prompt, and (b) enables it to > discern which of these two "run environments" it is running in. > > Goal (a) is achieved easily by using string.replace to replace unavailable > characters with available ones where necessary. > > The best way I have found so far to achieve goal (b) is to use sys.modules > and ascertain whether any modules contain the string "idlelib". If they do, > that I assume that the software is being run in an IDLE session. > > I suspect that there is a more Pythonic (and reliable?) way of achieving > goal (b). > > Can anyone please tell me if there is, and, if there is, what it is? Ultimately, it's going to depend on where your text is going: is it going to the console, or to a Tk widget? I don't have a Windows system handy to check, but I would suspect that you can distinguish these by seeing whether sys.stdout is a tty, since Idle pipes stdout into its own handler. That won't be a perfect check, as it would consider "piped into another command" to be UTF-8 compatible, but that's probably more right than wrong anyway. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Discerning "Run Environment"
Hi, I am a Windows 10 user still using Python 2.x (for good reasons, I assure you.) I have a Python 2.x module that I would like to be able to use in a variety of Python 2.x programs. The module outputs characters to the user that are only available in the Unicode character set. I have found that the selection of characters in that set that are available to my software depends on whether, for example, the program is being run during an IDLE session or at a Command Prompt. I am therefore needing to include logic in this module that (a) enables it to output appropriate characters depending on whether it is being run during an IDLE session or at a command prompt, and (b) enables it to discern which of these two "run environments" it is running in. Goal (a) is achieved easily by using string.replace to replace unavailable characters with available ones where necessary. The best way I have found so far to achieve goal (b) is to use sys.modules and ascertain whether any modules contain the string "idlelib". If they do, that I assume that the software is being run in an IDLE session. I suspect that there is a more Pythonic (and reliable?) way of achieving goal (b). Can anyone please tell me if there is, and, if there is, what it is? Thanks. Stephen Tucker. -- https://mail.python.org/mailman/listinfo/python-list