- [Solved] Python: Lazy Function Evaluation in any() / all()
- Recent Threads
- Why is it okay for my .bashrc or .zshrc to be writable by my normal user?
- SFTP user login details real-time filtering
- get nat port forwarding IP address
- Python: Lazy Function Evaluation in any() / all()
- jonathan.scholbach
- People also ask
- 1 Answers
[Solved] Python: Lazy Function Evaluation in any() / all()
Jonathan Scholbach Asks: Python: Lazy Function Evaluation in any() / all()
Logical operators in Python are lazy. With the following definition:
def func(s): print(s) return True
only evaluates the first function call, because or recognizes that the expression evaluates to True , irregardless of the return value of the second function call. and does behave analogously.
However, when using any() (analogously: all() ) in the following way:
all function calls are evaluated, because the inner list is constructed first, before any starts to iterate over the boolean values of its items. The same happens when we omit the list construction and just write
That way we lose the power of any being short-circuit, which means that it breaks as soon as the first element of the iterable is truish. If the function calls are expensive, evaluating all the functions up front is a big loss and is a waste of this ability of any . In some sense, one could call this a Python gotcha, because it might be unexpected for users trying to leverage this feature of any , and because any is often thought as being just another syntactic way of chaining a sequence of or statements. But any is just short-circuit, not lazy, and that is a difference here.
any is accepting an iterable. So, there should be a way of creating an iterator which does not evaluate its elements up front but pass them unevaluated to any and lets them evaluate inside of any only, in order to achieve a fully lazy evaluation.
So, the question is: How can we use any with truly lazy function evaluation? That means: How can we make an iterator of function calls which any can consume, without evaluating all the function calls in advance?
Recent Threads
Why is it okay for my .bashrc or .zshrc to be writable by my normal user?
Zach Huxford Asks: Why is it okay for my .bashrc or .zshrc to be writable by my normal user?
My user ~/.zshrc file has the following default privileges
My understanding of user permissions is that any process spawned by my user will then have read/write permissions to this file.
In malicious hands this could probably be used to edit aliases or append a directory of the attackers choosing to the beginning of the $PATH. I’m concerned that a malicious program that I install on the user level could then trick me into somehow giving up my sudo password through this method.
Obviously I do trust most of the programs that I install to not be malicious, however, I do use npm as a package manager for my own projects which is commonly accepted to be a vector for malware due to the sheer number of dependencies each module and it’s dependencies can have.
I know that running sudo npm install -g is really bad practice but is using npm as a user which has write access to your main shell configuration file almost as bad just with a few extra steps in between, or am I lacking an understanding of how user permissions/shell configuration/npm works?
If this is insecure, then have I somehow missed security good practice for handling node js projects?
SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.
SFTP user login details real-time filtering
Amal P Ramesh Asks: SFTP user login details real-time filtering
I have enabled the SFTP login log into the default logfile /var/log/syslog and tried to filter the login time of each user and insert it into the database.
But the filtering is not worked as I expected.
Jun 23 15:47:03 ip-172-16-0-62 systemd[24938]: Reached target Shutdown. Jun 23 15:47:03 ip-172-16-0-62 systemd[24938]: Starting Exit the Session..c. Jun 23 15:47:03 ip-172-16-0-62 systemd[24938]: Received SIGRTMIN+24 from PID 24980 (kill). Jun 23 15:47:03 ip-172-16-0-62 systemd[1]: Stopped User Manager for UID 1051. Jun 23 15:47:03 ip-172-16-0-62 systemd[1]: Removed slice User Slice of nidasu. Jun 23 15:47:13 ip-172-16-0-62 systemd[1]: Created slice User Slice of ftpuser1. Jun 23 15:47:13 ip-172-16-0-62 systemd[1]: Starting User Manager for UID 1069. Jun 23 15:47:13 ip-172-16-0-62 systemd[1]: Started Session 11907571 of user ftpuser1. Jun 23 15:47:13 ip-172-16-0-62 systemd[24987]: Listening on REST API socket for snapd user session agent. Jun 23 15:47:13 ip-172-16-0-62 systemd[24987]: Reached target Paths. Jun 23 15:47:13 ip-172-16-0-62 systemd[24987]: Reached target Timers. Jun 23 15:47:13 ip-172-16-0-62 systemd[24987]: Reached target Sockets. Jun 23 15:47:13 ip-172-16-0-62 systemd[24987]: Reached target Basic System. Jun 23 15:47:13 ip-172-16-0-62 systemd[24987]: Reached target Default. Jun 23 15:47:13 ip-172-16-0-62 systemd[24987]: Startup finished in 15ms.
Needs to filter user login messages, like:
Jun 23 15:47:13 ip-172-16-0-62 systemd[1]: Started Session 11907571 of user ftpuser1.
I need to grep it out by matching the string «Started Session 11907571 of user ftpuser1 «
The session number 11907571 is a random number and usernames also differ so grepping can ignore the numbers and usernames, only need to check the string like: **»Started Session *** of user ***»
And need to parse the line and grep the date + time , and username then insert it into the MySQL database.
If there is any option to create a daemon process to run and insert the details into DB, it will help me to do the task.
SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.
get nat port forwarding IP address
gyandoo Asks: get nat port forwarding IP address
I am using an android phone that is connected to an openwrt router via usb tether
The android phone has a dynamic wan gateway on each reboot
To make things easy for me to connect to the webui of some of the apps on the android phone via the openwrt router, I created a port forwarding rule in openwrt and entered the wan ip of the android phone manually. port forwarding rule
On each reboot of the android phone, i will have to check the routes in openwrt, get the new wan ip and update the port forwarding rule, which is fine
to make things easier on my linux machine, id like to be able to use CLI to get that wan ip that i set in port forwarding i.e 192.168.1.1:32399
not that it matters, but curlftpfs ftp mounting isn’t playing well with nat, all other android app webui’s are working fine with the port redirect, curlftpfs requires the wan ip, it finds the wan ip in debug but skips it
SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.
Python: Lazy Function Evaluation in any() / all()
Logical operators in Python are lazy. With the following definition:
def func(s): print(s) return True
only evaluates the first function call, because or recognizes that the expression evaluates to True , irregardless of the return value of the second function call. and does behave analogously.
However, when using any() (analogously: all() ) in the following way:
all function calls are evaluated, because the inner list is constructed first, before any starts to iterate over the boolean values of its items. The same happens when we omit the list construction and just write
That way we lose the power of any being short-circuit, which means that it breaks as soon as the first element of the iterable is truish. If the function calls are expensive, evaluating all the functions up front is a big loss and is a waste of this ability of any . In some sense, one could call this a Python gotcha, because it might be unexpected for users trying to leverage this feature of any , and because any is often thought as being just another syntactic way of chaining a sequence of or statements. But any is just short-circuit, not lazy, and that is a difference here.
any is accepting an iterable. So, there should be a way of creating an iterator which does not evaluate its elements up front but pass them unevaluated to any and lets them evaluate inside of any only, in order to achieve a fully lazy evaluation.
So, the question is: How can we use any with truly lazy function evaluation? That means: How can we make an iterator of function calls which any can consume, without evaluating all the function calls in advance?
asked Sep 27 ’20 23:09
jonathan.scholbach
People also ask
If you’ve never heard of Lazy Evaluation before, Lazy Evaluation is an evaluation strategy which delays the evaluation of an expression until its value is needed and which also avoids repeated evaluations (From Wikipedia). It’s usually being considered as a strategy to optimize your code.
Yes, Python evaluates boolean conditions lazily. The docs say, The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The benefits of lazy evaluation include: The ability to define control flow (structures) as abstractions instead of primitives. The ability to define potentially infinite data structures. This allows for more straightforward implementation of some algorithms.
In the last two examples, the left operand is false (an empty object). The Python or operator evaluates both operands and returns the object on the right, which may evaluate to either true or false.
1 Answers
We can use a generator expression, passing the functions and their arguments separately and evaluating only in the generator like so:
>>> any(func(arg) for arg in ('s', 't')) 's'
For different functions with different signatures, this could look like the following:
any( f(*args) for f, args in [(func1, ('s',)), (func2, (1, 't'))] )
That way, any will stop calling the next() element in the generator as soon as one function call in the generator evaluates to True , and that means that the function evaluation is fully lazy.
Another neat way to postpone the function evaluation was mentioned by wjandrea in a comment: We can also to use lambda expressions, like so:
>>> any(f() for f in [lambda: func('s'), lambda: func('t')] 's'
answered Sep 18 ’22 20:09