[Python-ideas] Re: Awaiting until a condition is met

2022-05-30 Thread Kyle Lahnakoski
On 2022-05-14 10:10 p.m., Aaron Fink wrote: start_moving_to(dest) await lambda: distance_to(dest) <= tolerance Instead of evaluating an expression you could build a notification chain that you await upon. x = Variable(value=2) y = Variable(value=2) # we create a Future that watches x and

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-25 Thread Kyle Lahnakoski
On 2021-10-25 3:31 p.m., Mike Miller wrote: > "defer" please. > > This construct did not happen in the past, and it's shorter of course. > > -Mike > I also like `defer`: > def range(a, min=0, max = defer len(a)): > return a[min:max] `default` is also nice: > def range(a, min=

[Python-ideas] Re: What about having a .get(index, default) method for arrays like we have for dicts?

2020-07-03 Thread Kyle Lahnakoski
On 2020-06-27 09:34, Daniel. wrote: When I need to traverse nested dicts, is a common pattern to do somedict.get('foo', {}).get('bar', {}) But there is no such equivalent for arrays, wouldn't be nice if we can follow somedict.get('foo', {}).get('bar', []).get(10) ... ? What do you think?

[Python-ideas] Re: Keyword arguments self-assignment

2020-04-23 Thread Kyle Lahnakoski
On 2020-04-19 07:23, Richard Damon wrote: There is also the issue that if we are building a function that might be used with another function, we will have an incentive to name our keyword parameters that there is a reasonable chance would also be passed to that other function with the same keyw

[Python-ideas] Re: Copying a multiple values of a dict to another with a lot easier syntax

2019-10-21 Thread Kyle Lahnakoski
On 2019-10-21 10:44, gedizgu...@gmail.com wrote: m and n are lists or dicts or enumerates or classes or anything it can be assigned like following: instead of : m.a=n.a; m.b=n.b; m.c=n.c; ... I suggest: a,b,c of m to n ; Interesting.  I also saw this type of redundancy in my code. Instead

[Python-ideas] Re: Allow kwargs in __{get|set|del|}item__

2019-10-08 Thread Kyle Lahnakoski
On 2019-10-07 20:35, Steven D'Aprano wrote: As per Caleb's initial post, this is how Pandas currently does it: db[db['x'] == 1] Replacing that with db[x=1] seems like a HUGE win to me. Even db[{'x': 1}] is pretty clunky. For Pandas, db['x'] which creates an expression involving `x`, a

[Python-ideas] Re: Set operations with Lists

2019-09-25 Thread Kyle Lahnakoski
On 2019-09-20 9:52 p.m., Richard Higginbotham wrote: Andrew Barnert wrote: set(b).intersection(a) or set(a) & set(b) or sb = set(b) then [x for x in a if x in sb] and you’re done. They can easily understand why it works. If they want to know why it’s faster, you can easily explain it, and they

[Python-ideas] Re: Exceptions with Message Templates

2019-08-12 Thread Kyle Lahnakoski
On 2019-08-08 11:52, Ryan Fox wrote: My proposal is a new exception class as the preferred base for user-defined exceptions: >>> class MyException(ExceptionTemplate): ...    message = 'Bad thing happened during {action} in {context}' >>> raise MyException(action=current_action, context=current

[Python-ideas] Re: Operator as first class citizens -- like in scala -- or yet another new operator?

2019-06-13 Thread Kyle Lahnakoski
Correct me if I am wrong: Yanghao would like an elegant way to build graphs. Simply using << to declare the connections in the graph [1] is not an option because << is already needed for legitimate left-shift operation.  The problem is not assignment; rather, Yanghao's HDL requires more operators

Re: [Python-ideas] Trigonometry in degrees

2019-05-08 Thread Kyle Lahnakoski
Maybe a library of trig functions that include an Angle type? This is related to the timespan units discussion we just had def sin(angle): On 2018-06-08 01:44, Yuval Greenfield wrote: > On Thu, Jun 7, 2018 at 10:38 PM Stephen J. Turnbull > > wrote:

Re: [Python-ideas] Type hints for functions with side-effects and for functions raising exceptions

2019-02-25 Thread Kyle Lahnakoski
On 2019-02-22 17:20, Chris Angelico wrote: > On Sat, Feb 23, 2019 at 9:14 AM Kyle Lahnakoski > wrote: >> Can Python provide better support for the CNCR pattern? If it is >> lightweight enough, maybe people will use it, and then we can say >> something useful about t

Re: [Python-ideas] Type hints for functions with side-effects and for functions raising exceptions

2019-02-22 Thread Kyle Lahnakoski
On 2019-02-21 03:09, Christopher Barker wrote: > > But yes, there is no (easy) way to distinguish an Exception raised by the function you called, and one raised somewhere deeper that. > > And I have been bitten by that more than once. It makes "Easier to ask forgiveness than permission" kind of tr

Re: [Python-ideas] add fluent operator to everything

2019-02-21 Thread Kyle Lahnakoski
On 2019-02-20 10:10, Steven D'Aprano wrote: > Or if you're worried about the line length: > result = function(mystr.strip() >.expandtabs() >.lower() >.replace('ham', 'spam') > I think It seems

Re: [Python-ideas] Vectorization [was Re: Add list.join() please]

2019-02-11 Thread Kyle Lahnakoski
CHB, Thank you! I had forgotten that discussion at the beginning of July [1].  Googling the list [2] also shows mention of PythonQL [3], which may point to use cases that can guide a Vectorization idea. [1] groupby discussion - https://mail.python.org/pipermail/python-ideas/2018-July/051786.htm

Re: [Python-ideas] Vectorization [was Re: Add list.join() please]

2019-02-10 Thread Kyle Lahnakoski
On 2019-02-10 18:30, Steven D'Aprano wrote: > > Can you post a simplified example of how you would do it in SQL, > compared to what you would have to do in standard Python? Can I do the same in standard Python? If I did, then I would use Pandas: it has groupby, and some primitive joining, and wi

Re: [Python-ideas] Vectorization [was Re: Add list.join() please]

2019-02-10 Thread Kyle Lahnakoski
On 2019-02-02 18:11, Steven D'Aprano wrote: > We can improve that comprehension a tiny bit by splitting it into > multiple steps: > > temp1 = [d+e for d, e in zip(vector, sequence)] > temp2 = [process(c) for x in temp1] > result = [a*b for a, b in zip(temp2, items)] > > but none of

Re: [Python-ideas] Implementing a set of operation (+, /, - *) on dict consistent with linearAlgebrae

2018-10-31 Thread Kyle Lahnakoski
Julien, Personally, I would be able to use the module you are proposing to accumulate arbitrarily-named measures. I can not think of a use case for division, but it would be nice for completion.  I have made my own library that implements a small part of what you propose [1]. I was looking throu

Re: [Python-ideas] TypeHinting: From variable name to type

2018-10-21 Thread Kyle Lahnakoski
On 2018-10-21 10:44, David Mertz wrote: > On Fri, Oct 19, 2018 at 3:18 AM Thomas Güttler > mailto:guettl...@thomas-guettler.de>> > wrote: > > Now my idea: Per module and/or per file type hinting from variable > name. > Maybe a magic docstring in the __init__.py file: > variable-na

Re: [Python-ideas] Suggestion: Extend integers to include iNaN

2018-10-01 Thread Kyle Lahnakoski
On 2018-09-30 10:45, Anders Hovmöller wrote: > >>> I am roughing out such a class and some test cases which will hopefully >>> include some cases where the hoped for advantages can be realised. >>> >>> My thinking on bitwise operations is to do the same as arithmetic >>> operations, i.e. (anyth

Re: [Python-ideas] Suggestion: Extend integers to include iNaN

2018-09-30 Thread Kyle Lahnakoski
On 2018-09-30 10:15, David Mertz wrote: > For similar reasons, I'd like an iInf too, FWIW.  It's good for an > overflow value, although it's hard to get there in Python ints (would > 'NaNAwareInt(1)/0' be an exception or iInf?).  Bonus points for anyone > who knows the actual maximum size of Pyt

Re: [Python-ideas] Suggestion: Extend integers to include iNaN

2018-09-30 Thread Kyle Lahnakoski
On 2018-09-30 09:41, Steve Barnes wrote: > I am roughing out such a class and some test cases which will hopefully > include some cases where the hoped for advantages can be realised. > > My thinking on bitwise operations is to do the same as arithmetic > operations, i.e. (anything op iNaN) = i

Re: [Python-ideas] Suggestion: Extend integers to include iNaN

2018-09-30 Thread Kyle Lahnakoski
On 2018-09-30 09:41, Steve Barnes wrote: > I am roughing out such a class and some test cases which will hopefully > include some cases where the hoped for advantages can be realised. > > My thinking on bitwise operations is to do the same as arithmetic > operations, i.e. (anything op iNaN) = i

Re: [Python-ideas] Why is design-by-contracts not widely adopted?

2018-09-25 Thread Kyle Lahnakoski
I use DbC occasionally to clarify my thoughts during a refactoring, and then only in the places that continue to make mistakes. In general, I am not in a domain that benefits from DbC. Contracts are code: More code means more bugs. Declarative contracts are succinct, but difficult to debug when w

Re: [Python-ideas] Asynchronous exception handling around with/try statement borders

2018-09-20 Thread Kyle Lahnakoski
On 2017-06-28 07:40, Erik Bray wrote: > Hi folks, Since the java.lang.Thread.stop() "debacle", it has been obvious that stopping code to run other code has been dangerous.  KeyboardInterrupt (any interrupt really) is dangerous. Now, we can probably code a solution, but how about we remove the dan

Re: [Python-ideas] Keyword only argument on function call

2018-09-08 Thread Kyle Lahnakoski
I agree that this is a familiar pattern, but I long since forgot the specifics of the domain it happens in.  I borrowed your code, and added filename tracking to see what source files had high `could_have_been_a_matched_kwarg`.  Here is the top one: https://github.com/django/django/blob/master/te

Re: [Python-ideas] Is this PEP-able? "with" statement inside genexps / list comprehensions

2018-07-30 Thread Kyle Lahnakoski
Rudy, I think your proposal may be very specific to iterable context managers; in which case, make a method that makes that assumption: > def iter_with(obj): > with obj as context: > yield from context and use it > g = ( > f.read() > for fn in filenames > for f in iter_w

Re: [Python-ideas] Redefining method

2018-07-30 Thread Kyle Lahnakoski
I think C# calls methods that are added to a class "extension methods". You can make a decorator that will do that for you: > def extend(cls): >     """ >     DECORATOR TO ADD METHODS TO CLASSES >     :param cls: THE CLASS TO ADD THE METHOD TO >     :return: >     """ >     def extender(func): >

Re: [Python-ideas] PEP 505: None-aware operators

2018-07-26 Thread Kyle Lahnakoski
On 2018-07-25 23:53, David Mertz wrote: > On Wed, Jul 25, 2018, 11:27 PM Chris Angelico > wrote: > > > means that you cannot do this: > > >>> NullCoalesce(spam).nil is None > > This IS fundamentally unfixable in a library. > > > Right now, you can still alway

Re: [Python-ideas] Idea: Deferred Default Arguments?

2018-07-24 Thread Kyle Lahnakoski
I agree this is a problem, which I have seen solved by removing the method signature, which is unfortunate: > def flexible_method(**kwargs): >     # Read the code to find out the expected parameters    I have an @override decorator to handle this type of pattern. It will perform the null-coalesc

Re: [Python-ideas] A better (simpler) approach to PEP 505

2018-07-23 Thread Kyle Lahnakoski
I agree a class can provide a very good alternative to PEP505.  I built one a while ago, and still use it https://github.com/klahnakoski/mo-dots for most my data transformation code. The library only boxes dicts and lists, which removes the need for .unbox() in many of my use cases. My point is,

Re: [Python-ideas] Secure string disposal (maybe other inmutable seq types too?)

2018-06-24 Thread Kyle Lahnakoski
Ezequiel (Ezekiel) Brizuela, How is the secret "password" getting into a Python variable?  It is coming from disk, or network? Do the buffers of those systems have a copy?  How about methods that operate on the secrets?  Do they internally decrypt secrets to perform the necessary operations? I h

Re: [Python-ideas] Trigonometry in degrees

2018-06-08 Thread Kyle Lahnakoski
On 2018-06-08 01:44, Yuval Greenfield wrote: > On Thu, Jun 7, 2018 at 10:38 PM Stephen J. Turnbull > > wrote: > > > 6.123233995736766e-17 > >>> > > is good enough for government work, including at the local public high > school. > > > T

Re: [Python-ideas] Runtime assertion with no overhead when not active

2018-06-05 Thread Kyle Lahnakoski
I currently use the form     and log_function( ) where is some module variable, usually "DEBUG".  I do this because it is one line, and it ensures the log_function parameters are not evaluated. *IF* runtime assertions had a switch so they have no overhead when not active, how much faster can

Re: [Python-ideas] datetime.timedelta literals

2018-06-04 Thread Kyle Lahnakoski
Pål Grønås Drange, I do like the idea of literals typed with scientific units, but I often get short variable names mixed up, so I am not sure if I could use them without a cheat sheet. Formatting datetime is a good example of how confusing a collection of short names can get: Is month %m or %M? 

Re: [Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin

2018-04-08 Thread Kyle Lahnakoski
On 2018-04-05 21:18, Steven D'Aprano wrote: > (I don't understand why so many people have such an aversion to writing > functions and seek to eliminate them from their code.) > I think I am one of those people that have an aversion to writing functions! I hope you do not mind that I attempt to

Re: [Python-ideas] PEP 572: Statement-Local Name Bindings, take three!

2018-03-27 Thread Kyle Lahnakoski
On 2018-03-23 06:01, Chris Angelico wrote: > https://www.python.org/dev/peps/pep-0572/ > A suggestion: Under the rejected "Special-casing comprehensions", you show "prefix-local-name-bindings": Name bindings that appear before the loop, like: > stuff = [(y, x/y) where y = f(x) for x in range(5

Re: [Python-ideas] Descouraging the implicit string concatenation

2018-03-14 Thread Kyle Lahnakoski
On 2018-03-14 08:18, Facundo Batista wrote: > Hello! > > What would you think about formally descouraging the following idiom? > > long_string = ( > "some part of the string " > "with more words, actually is the same " > "string that the compiler puts together") > Tha

Re: [Python-ideas] PEP 572: Statement-Local Name Bindings

2018-02-28 Thread Kyle Lahnakoski
On 2018-02-28 02:46, Matt Arcidy wrote: > From readability, the examples put forth have been to explain the > advantage, with which I agree.  However, i do not believe this scales > well. > > [(foo(x,y) as g)*(bar(y) as i) + g*foo(x,a) +baz(g,i) for x... for y...] > > That's 3 functions, 2 iterat

Re: [Python-ideas] Temporary variables in comprehensions

2018-02-23 Thread Kyle Lahnakoski
On 2018-02-23 12:44, Neil Girdhar wrote: > > On Fri, Feb 23, 2018 at 12:35 PM Kyle Lahnakoski > mailto:klahnako...@mozilla.com>> wrote: > > > > [ > > (w, w**2) > > for x in (1, 2, 3, 4) > > let y = x+1 >

Re: [Python-ideas] Temporary variables in comprehensions

2018-02-23 Thread Kyle Lahnakoski
I believe list comprehensions are difficult to read because they are not formatted properly. For me, list comprehension clauses are an expression, followed by clauses executed in the order. Any list comprehension with more than one clause should be one-line-per clause. Examples inline: On 2018-02

Re: [Python-ideas] Temporary variables in comprehensions

2018-02-23 Thread Kyle Lahnakoski
On 2018-02-17 11:23, fhsxfhsx wrote: > [ >   { >     'id': goods.id, >     'name': goods.name, >     'category': gc.name, >     'category_type': gc.type, >   } >   for goods_id in goods_id_list >   for goods is Goods.get_by_id(goods_id) >   for gc is GoodsCategory.get_by_id(goods.category_id) > ]

Re: [Python-ideas] Arguments to exceptions

2017-07-05 Thread Kyle Lahnakoski
I agree with Ken for the need to make rich exceptions easy to write, but I do not know enough to say if changing BaseException to support this is a good idea; I made my own error reporting library to do this: For example, to define a new exception type with a `name` attribute: raise Log.error("na

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Kyle Lahnakoski
On 2017-03-30 05:18, Markus Meskanen wrote: > Hi Pythonistas, > > yet again today I ended up writing: > > d = [[0] * 5 for _ in range(10)] > > > Thoughts? It looks like you are initializing matrices. Can you make a helper function? d = matrix(shape=(5, 10), default=0) or maybe use NumPy?

Re: [Python-ideas] Proposal: Query language extension to Python (PythonQL)

2017-03-25 Thread Kyle Lahnakoski
Pavel, I like PythonQL. I perform a lot of data transformation, and often find Python's list comprehensions too limiting; leaving me wishing for LINQ-like language features. As an alternative to extending Python with PythonQL, Terry Reedy suggested interpreting a DSL string, and Pavel Velikhov a

Re: [Python-ideas] get() method for list and tuples

2017-03-03 Thread Kyle Lahnakoski
I must mention a get() method for lists and tuples would be very useful for me too. It is so useful, that I spent too much time making my own module to handle this case, plus many of the other dealing-with-None subjects found on this list. Michel is correct to point out that this is domain specif

Re: [Python-ideas] Efficient debug logging

2017-02-16 Thread Kyle Lahnakoski
On 2017-02-15 16:06, Abe Dillon wrote: > On 15.02.2017, 20:39 Kyle Lahnakoski wrote: > > Log "levels" never made sense to me; how can a single dimension be > useful substitute for a number of binary switches? With log > "levels", you either don

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Kyle Lahnakoski
On 2017-02-14 19:51, Abe Dillon wrote: >The point is that the cost of creating the msg argument can be very > high. > > At the point that logging decides to skip output it is to late to > save the cost of creating the arg tuple. > > This sounds like an optimization that's sufficiently

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Kyle Lahnakoski
Can you wrap the expensive functions in lambdas? And have your logger evaluate it, only if required? > debugLog( ‘info is %r’ % (lambda: expensiveFunction(),) ) On 2017-02-14 10:51, Barry Scott wrote: > A common pattern I use is to have logging calls for debug and information > with my applica

Re: [Python-ideas] Fwd: Define a method or function attributeoutsideof a class with the dot operator

2017-02-13 Thread Kyle Lahnakoski
On 2017-02-12 14:01, Joao S. O. Bueno wrote: > On 12 February 2017 at 14:51, Markus Meskanen > wrote: >> 1. Allowing the class to be used in the method's header, f.e. for typing and >> decorators: >> >> @decorate(MyClass) >> def MyClass.method(self, other: MyClass) -> List[MyClass]: >>

Re: [Python-ideas] Define a method or function attribute outside of a class with the dot operator

2017-02-10 Thread Kyle Lahnakoski
On 2017-02-10 05:44, Markus Meskanen wrote: > > > On Fri, Feb 10, 2017 at 12:29 PM, Stephan Houben > wrote: > > What about using a simple decorator instead? > > def monkey_patch(cls): > return lambda func: setattr(cls, func.__name__, func) > I suggest

Re: [Python-ideas] PEP 532: A circuit breaking operator and protocol

2016-11-14 Thread Kyle Lahnakoski
It would be nice if coalesce(EXPR1, EXPR2, EXPR3) Evaluated the N+1 argument only if the Nth evaluated to None. Of course this may break the general patterns in the Python language. Maybe we can fake it by wrapping the expressions in lambdas: coalesce(lambda: EXPR1(), lambda EXPR2(

Re: [Python-ideas] Null coalescing operator

2016-11-02 Thread Kyle Lahnakoski
On 11/2/2016 2:30 PM, Zero Piraeus wrote: If I write something like obj.attr, the failure mode I care about is that obj has no attribute attr, rather than that obj is specifically None (or one of a defined group of somewhat Nonelike objects). I agree with this understanding. The problem wit