[Python-announce] PyCA cryptography 41.0.7 released
PyCA cryptography 41.0.7 has been released to PyPI. cryptography includes both high level recipes and low level interfaces to common cryptographic algorithms such as symmetric ciphers, asymmetric algorithms, message digests, X509, key derivation functions, and much more. We support Python 3.7+, and PyPy3 7.3.10+. Changelog (https://cryptography.io/en/latest/changelog/#v41-0-7): * Fixed compilation when using LibreSSL 3.8.2. Alex -- All that is necessary for evil to succeed is for good people to do nothing. ___ Python-announce-list mailing list -- python-announce-list@python.org To unsubscribe send an email to python-announce-list-le...@python.org https://mail.python.org/mailman3/lists/python-announce-list.python.org/ Member address: arch...@mail-archive.com
Re: argparse argument post-processing
Yeah, I have been hearing that people are having troubles converting, but I have only used argparse - got lucky there I guess. I am thinking just making the function which spits the class out. Maybe not very optimised solution, but simple. Argument parsing in my case is very far from being a bottleneck. > On 27 Nov 2023, at 22:36, Mats Wichmann wrote: > > On 11/27/23 13:21, Dom Grigonis wrote: >> Thank you, exactly what I was looking for! >> One more question following this. Is there a way to have a customisable >> action? I.e. What if I want to join with space in one case and with coma in >> another. Is there a way to reuse the same action class? > > I've worked more with optparse (the project I work on that uses it has > reasons why it's not feasible to convert to argparse); in optparse you use a > callback function, rather than an action class, and the change to a callable > class is somewhat significant :-; so I'm not really an expert. > > The question is how you determine which you want to do - then there's no > problem for the action class's call method to implement it. I presume you can > write an initializer class that takes an extra argument, collect that and > stuff it into an instance variable, then use super to call the base Action > class's initializer with the rest of the args > > super().__init__(option_strings=option_strings, *args, **kwargs) > > Hopefully someone else has done this kind of thing because now I'm just > guessing! > > -- https://mail.python.org/mailman/listinfo/python-list
Re: argparse argument post-processing
On 11/27/23 13:21, Dom Grigonis wrote: Thank you, exactly what I was looking for! One more question following this. Is there a way to have a customisable action? I.e. What if I want to join with space in one case and with coma in another. Is there a way to reuse the same action class? I've worked more with optparse (the project I work on that uses it has reasons why it's not feasible to convert to argparse); in optparse you use a callback function, rather than an action class, and the change to a callable class is somewhat significant :-; so I'm not really an expert. The question is how you determine which you want to do - then there's no problem for the action class's call method to implement it. I presume you can write an initializer class that takes an extra argument, collect that and stuff it into an instance variable, then use super to call the base Action class's initializer with the rest of the args super().__init__(option_strings=option_strings, *args, **kwargs) Hopefully someone else has done this kind of thing because now I'm just guessing! -- https://mail.python.org/mailman/listinfo/python-list
[Python-announce] PyCA cryptography 41.0.6 released
PyCA cryptography 41.0.6 has been released to PyPI. cryptography includes both high level recipes and low level interfaces to common cryptographic algorithms such as symmetric ciphers, asymmetric algorithms, message digests, X509, key derivation functions, and much more. We support Python 3.7+, and PyPy3 7.3.10+. Changelog (https://cryptography.io/en/latest/changelog/#v41-0-6): * Fixed a null-pointer-dereference and segfault that could occur when loading certificates from a PKCS#7 bundle. Credit to pkuzco for reporting the issue. CVE-2023-49083 -Paul Kehrer (reaperhulk) ___ Python-announce-list mailing list -- python-announce-list@python.org To unsubscribe send an email to python-announce-list-le...@python.org https://mail.python.org/mailman3/lists/python-announce-list.python.org/ Member address: arch...@mail-archive.com
Re: argparse argument post-processing
Thank you, exactly what I was looking for! One more question following this. Is there a way to have a customisable action? I.e. What if I want to join with space in one case and with coma in another. Is there a way to reuse the same action class? Regards, DG > On 27 Nov 2023, at 21:55, Mats Wichmann via Python-list > wrote: > > On 11/27/23 04:29, Dom Grigonis via Python-list wrote: >> Hi all, >> I have a situation, maybe someone can give some insight. >> Say I want to have input which is comma separated array (e.g. >> paths='path1,path2,path3') and convert it to the desired output - list: >> import argparse >> parser = argparse.ArgumentParser() >> parser.add_argument('paths', type=lambda x: list(filter(str.strip, >> x.split(',' >> So far so good. But this is just an example of what sort of solution I am >> after. > > Maybe use "action" rather than "type" here? the conversion of a csv argument > into words seems more like an action. > >> Now the second case. I want input to be space separated array - bash array. >> And I want space-separated string returned. My current approach is: >> import argparse >> parser = argparse.ArgumentParser() >> parser.add_argument('paths', nargs='+') >> args = parser.parse_args() >> paths = ' '.join(args.paths) >> But what I am looking for is a way to do this, which is intrinsic to >> `argparse` module. Reason being I have a fair amount of such cases and I >> don’t want to do post-processing, where post-post-processing happens (after >> `parser.parse_args()`). >> I have tried overloading `parse_args` with post-processor arguments, and >> that seemed fine, but it stopped working when I had sub-parsers, which are >> defined in different modules and do not call `parse_args` themselves. > > Depending on what *else* you need to handle it may or not may work here to > just collect these from the remainders, and then use an action to join them, > like: > > import argparse > > class JoinAction(argparse.Action): >def __call__(self, parser, namespace, values, option_string=None): >setattr(namespace, self.dest, ' '.join(values)) > > parser = argparse.ArgumentParser() > parser.add_argument('paths', nargs=argparse.REMAINDER, action=JoinAction) > args = parser.parse_args() > > print(f"{args.paths!r}") > > > > > > > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: argparse argument post-processing
On 11/27/23 04:29, Dom Grigonis via Python-list wrote: Hi all, I have a situation, maybe someone can give some insight. Say I want to have input which is comma separated array (e.g. paths='path1,path2,path3') and convert it to the desired output - list: import argparse parser = argparse.ArgumentParser() parser.add_argument('paths', type=lambda x: list(filter(str.strip, x.split(',' So far so good. But this is just an example of what sort of solution I am after. Maybe use "action" rather than "type" here? the conversion of a csv argument into words seems more like an action. Now the second case. I want input to be space separated array - bash array. And I want space-separated string returned. My current approach is: import argparse parser = argparse.ArgumentParser() parser.add_argument('paths', nargs='+') args = parser.parse_args() paths = ' '.join(args.paths) But what I am looking for is a way to do this, which is intrinsic to `argparse` module. Reason being I have a fair amount of such cases and I don’t want to do post-processing, where post-post-processing happens (after `parser.parse_args()`). I have tried overloading `parse_args` with post-processor arguments, and that seemed fine, but it stopped working when I had sub-parsers, which are defined in different modules and do not call `parse_args` themselves. Depending on what *else* you need to handle it may or not may work here to just collect these from the remainders, and then use an action to join them, like: import argparse class JoinAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, ' '.join(values)) parser = argparse.ArgumentParser() parser.add_argument('paths', nargs=argparse.REMAINDER, action=JoinAction) args = parser.parse_args() print(f"{args.paths!r}") -- https://mail.python.org/mailman/listinfo/python-list
RE: Context without manager
> I *must* do: > > with device_open() as device: >device.do_something() > > Nevertheless, I _need_ to have a class > where the device is opened in the __init__() > and used in some methods. > > Any ideas? Perhaps take a look at contextlib.ExitStack and see if you can do something with it. (Below is not tested) import contextlib class myClass: def __init__(self): self._exitStack = contextlib.ExitStack() device = self._exitStack.enter_context(device_open(...)) def __del__(self): self._exitStack.close() -- https://mail.python.org/mailman/listinfo/python-list
Context without manager
Read the Fine context manager documentation. What “with with_expression as var” does is effectively: ob = with_expression var = ob.__enter__() And then at the end of the with, does a ob.__exit__() (With some parameters to __exit__, that could just be None, None, None for the simplest case). Note, YOUR program must now make sure that the __exit__ function is called, and handle any exceptions that got thrown, and that ob and var are put somewhere you can access them at that later time. > On Nov 27, 2023, at 12:24 PM, Piergiorgio Sartor via Python-list > wrote: > > On 26/11/2023 18.50, Dieter Maurer wrote: >> Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100: >>> ... >>> Apparently, the "with" context manager is not usable >>> in classes, at least not with __init__() & co. >> You can use `with` in classes -- with any context manager. >> However, you would usually not use `with` with a file you have opened >> in `__init__`. >> If a class defines `__enter__` and `__exit__` (i.e. >> the "cntext manager protocol"), then its instances >> can be used with the `with` statement. >> The important use case for a context manager is the >> situation: >> set up a context (--> method `__enter__`) >> perform some operations in this context (--> body of `with` statement) >> tear down the context (--> method `__exit__`). >> If you do not have this case (e.g. usually if you open the file >> in a class's `__init__`), you do not use a context manager. > > Very clear, but what if the class is *not* "open()", > but something else _requiring_ using "with"? > How to do this in a "__init__()" of a class? > > In other words, what if "open()" could *only* be used > with "with" and not just by assigning "fp = open()"? > > The problem is I've some SDK of some device which > provides context manager *only* classes. > > I *cannot* do: > > device = device_open(...) > device.do_something() > device.close() > > I *must* do: > > with device_open() as device: > device.do_something() > > Nevertheless, I _need_ to have a class > where the device is opened in the __init__() > and used in some methods. > > Any ideas? > > bye, > > -- > > piergiorgio > > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Context without manager
Read the Fine context manager documentation. What “with with_expression as var” does is effectively: ob = with_expression var = ob.__enter__() And then at the end of the with, does a ob.__exit__() (With some parameters to __exit__, that could just be None, None, None for the simplest case). Note, YOUR program must now make sure that the __exit__ function is called, and handle any exceptions that got thrown, and that ob and var are put somewhere you can access them at that later time. > On Nov 27, 2023, at 12:24 PM, Piergiorgio Sartor via Python-list > wrote: > > On 26/11/2023 18.50, Dieter Maurer wrote: >> Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100: >>> ... >>> Apparently, the "with" context manager is not usable >>> in classes, at least not with __init__() & co. >> You can use `with` in classes -- with any context manager. >> However, you would usually not use `with` with a file you have opened >> in `__init__`. >> If a class defines `__enter__` and `__exit__` (i.e. >> the "cntext manager protocol"), then its instances >> can be used with the `with` statement. >> The important use case for a context manager is the >> situation: >> set up a context (--> method `__enter__`) >> perform some operations in this context (--> body of `with` statement) >> tear down the context (--> method `__exit__`). >> If you do not have this case (e.g. usually if you open the file >> in a class's `__init__`), you do not use a context manager. > > Very clear, but what if the class is *not* "open()", > but something else _requiring_ using "with"? > How to do this in a "__init__()" of a class? > > In other words, what if "open()" could *only* be used > with "with" and not just by assigning "fp = open()"? > > The problem is I've some SDK of some device which > provides context manager *only* classes. > > I *cannot* do: > > device = device_open(...) > device.do_something() > device.close() > > I *must* do: > > with device_open() as device: > device.do_something() > > Nevertheless, I _need_ to have a class > where the device is opened in the __init__() > and used in some methods. > > Any ideas? > > bye, > > -- > > piergiorgio > > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
how to connect linux aws ec2 instance to windows local machine at my home using paramiko
I have a north viriginia ec2 linux instance and a windows machine at my home, how do I connec tthem? import paramiko import time def run_scripts(): # Set your local machine's SSH details local_machine_ip = ' ' username = 'justk' private_key_path = 'C:/Users/justk/.ssh/kashish' print("Connected 1", private_key_path) # Create an SSH client ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) print("Connected 2.2") try: # Connect to the local machine ssh.connect(local_machine_ip, username=username, key_filename=private_key_path,password='abc') print("Connected 2") # Stop the first script: check_messages.py stop_check_messages_command = 'pkill -f python C:/Project/pipeline-deployment/check_messages.py' ssh.exec_command(stop_check_messages_command) print("Connected 3") # Stop the second script: manage.py runserver stop_runserver_command = 'pkill -f "python C:/Project/pipeline-deployment/manage.py runserver' ssh.exec_command(stop_runserver_command) print("Waiting for 5 seconds before starting scripts...") time.sleep(60) # Run the first script: check_messages.py check_messages_command = 'python C:/Project/pipeline-deployment/check_messages.py' stdin, stdout, stderr = ssh.exec_command(check_messages_command) print(f"Output of check_messages.py:\n{stdout.read().decode('utf-8')}") # Run the second script: manage.py runserver runserver_command = 'python C:/Project/pipeline-deployment/manage.py runserver' stdin, stdout, stderr = ssh.exec_command(runserver_command) print(f"Output of manage.py runserver:\n{stdout.read().decode('utf-8')}") # Wait for 60 seconds print("Waiting for 60 seconds...") time.sleep(60) # Run the third script: restart.py restart_command = 'python C:/Project/pipeline-deployment/restartworkersbutton.py' stdin, stdout, stderr = ssh.exec_command(restart_command) print(f"Output of restart.py:\n{stdout.read().decode('utf-8')}") except Exception as e: print(f"Error: {e}") finally: # Close the SSH connection ssh.close() if __name__ == "__main__": run_scripts() i used this but i am unable to know what ip address to use? -- https://mail.python.org/mailman/listinfo/python-list
Re: Context without manager
On 26/11/2023 18.50, Dieter Maurer wrote: Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100: ... Apparently, the "with" context manager is not usable in classes, at least not with __init__() & co. You can use `with` in classes -- with any context manager. However, you would usually not use `with` with a file you have opened in `__init__`. If a class defines `__enter__` and `__exit__` (i.e. the "cntext manager protocol"), then its instances can be used with the `with` statement. The important use case for a context manager is the situation: set up a context (--> method `__enter__`) perform some operations in this context (--> body of `with` statement) tear down the context (--> method `__exit__`). If you do not have this case (e.g. usually if you open the file in a class's `__init__`), you do not use a context manager. Very clear, but what if the class is *not* "open()", but something else _requiring_ using "with"? How to do this in a "__init__()" of a class? In other words, what if "open()" could *only* be used with "with" and not just by assigning "fp = open()"? The problem is I've some SDK of some device which provides context manager *only* classes. I *cannot* do: device = device_open(...) device.do_something() device.close() I *must* do: with device_open() as device: device.do_something() Nevertheless, I _need_ to have a class where the device is opened in the __init__() and used in some methods. Any ideas? bye, -- piergiorgio -- https://mail.python.org/mailman/listinfo/python-list
RE: Newline (NuBe Question)
Dave, I gave an example, again, and make no deep claims so your comments may be valid, without any argument. I mentioned CSV and a related family such as TSV as they were a common and simple data format that has long been used. There are oodles of others and yes, these days many people can read directly from formats like some from EXCEL. But for data that can be shared to almost anyone using anything, something like Comma Separated Values is often used. And some programs that generate such data simply keep appending a line at a time to a file and do not have any header line. There are even some programs that may not tolerate a file with a header line, or comments or other optional things, and some where header lines you can create would cause problems such as using an extended character set or escaped characters. I have worked with these files in many languages and environments and my thought process here focused on recent work in R, albeit much applies everywhere. My point was really not about CSV but the convenience and advantages of data structures you can access by name when you want and sometimes also by position when you want. Too many errors can happen when humans doing programming are not able to concentrate. It is similar to arguments about file names. In the old UNIX days, and the same for other systems like VMS, a filename tended to have a format where relatively few characters were allowed and it might have two parts with the latter being an extension of up to 3 characters, or whatever. So file names like A321G12.dat were common and also next to it similar unpronounceable other file names. It was easy to confuse them and even people who worked with them regularly would forget what it might mean or use the wrong one. Well, if I load in a CSV in a language like R and there is no header line, as with some other data structures, it may make up a placeholder set of names like V1, V2 and so on. Yes, there are ways to specify the names as they are read in or afterward and they can be changed. But I have seen lots of CSV files offered with way too many columns and no names as well as documentation suggesting what names can be added if you wish. This may be a bit off topic, but I want to add a bit in this context about additional concepts regarding name. As mentioned, there is a whole set of add-ons people sometimes use and in R, I like the tidyverse family and it allows some fairly sophisticated things to be done using names. There are ways to specify you want a subset of a data.frame (sometimes a version called a tibble) and you can ask for say all columns starting with "xyz" or containing it or ending with it. That can be very helpful if say we wave columns containing the height and weight and other metrics of say people in three clinics and your column names embed the name of the clinic, or other such examples, and you want to select one grouping for processing. You cannot easily do that without external info is it is just positional. An extension of this is how compactly you can do fairly complex things such as asking to create lots of new columns using calculations. You can specify, as above, which sets of columns to do this too and that you want the results for each XYY in XYZ.mean and XYZ.std and so on. You can skip oodles of carefully crafted and nested loops because of the ability to manipulate using column names at a high and often abstract level. And, just FYI, many other structures such as lists in R also support names for components. It can be very useful. But the overall paradigm compared to Python has major differences and I see strengths and weaknesses and tradeoffs. Your dictionary example is one of them as numpy/pandas often make good use of them as part of dealing with similar data.frame type structures that are often simpler or easier to code with. There is lots of AI discussion these days and some of what you say is applicable in that additional info besides names might be useful in the storage format to make processing it more useful. That is available in formats related to XML where fairly arbitrary markup can be made available. Have to head out as this is already long enough. -Original Message- From: 'DL Neil' Sent: Monday, November 27, 2023 2:49 AM To: avi.e.gr...@gmail.com; python-list@python.org Subject: Re: Newline (NuBe Question) Avi, On 11/27/2023 4:15 PM, avi.e.gr...@gmail.com wrote: > Dave, > > Back on a hopefully more serious note, I want to make a bit of an analogy > with what happens when you save data in a format like a .CSV file. > > Often you have a choice of including a header line giving names to the > resulting columns, or not. > > If you read in the data to some structure, often to some variation I would > loosely call a data.frame or perhaps something like a matrix, then without > headers you have to specify what you want positionally or create your own > names for columns to use.
Re: argparse argument post-processing
On Mon, 27 Nov 2023 at 22:31, Dom Grigonis via Python-list wrote: > > Hi all, > > I have a situation, maybe someone can give some insight. > > Say I want to have input which is comma separated array (e.g. > paths='path1,path2,path3') and convert it to the desired output - list: This is a single argument. > Now the second case. I want input to be space separated array - bash array. > And I want space-separated string returned. My current approach is: > import argparse > parser = argparse.ArgumentParser() > parser.add_argument('paths', nargs='+') > args = parser.parse_args() > paths = ' '.join(args.paths) > But what I am looking for is a way to do this, which is intrinsic to > `argparse` module. Reason being I have a fair amount of such cases and I > don’t want to do post-processing, where post-post-processing happens (after > `parser.parse_args()`). > This is parsing multiple arguments. If you want it space-separated, you can do that, just as a single argument. Otherwise, what you're doing is taking multiple arguments and joining them. I'm not sure what you expect to see, but your examples here pretty clearly show that you are taking multiple arguments, and joining them with spaces. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
argparse argument post-processing
Hi all, I have a situation, maybe someone can give some insight. Say I want to have input which is comma separated array (e.g. paths='path1,path2,path3') and convert it to the desired output - list: import argparse parser = argparse.ArgumentParser() parser.add_argument('paths', type=lambda x: list(filter(str.strip, x.split(',' So far so good. But this is just an example of what sort of solution I am after. - Now the second case. I want input to be space separated array - bash array. And I want space-separated string returned. My current approach is: import argparse parser = argparse.ArgumentParser() parser.add_argument('paths', nargs='+') args = parser.parse_args() paths = ' '.join(args.paths) But what I am looking for is a way to do this, which is intrinsic to `argparse` module. Reason being I have a fair amount of such cases and I don’t want to do post-processing, where post-post-processing happens (after `parser.parse_args()`). I have tried overloading `parse_args` with post-processor arguments, and that seemed fine, but it stopped working when I had sub-parsers, which are defined in different modules and do not call `parse_args` themselves. Any ideas appreciated, Regards, DG -- https://mail.python.org/mailman/listinfo/python-list