Re: How to support annotations for a custom type in a C extension?

2021-09-19 Thread Serhiy Storchaka
19.09.21 05:59, MRAB пише: > On 2021-09-18 16:09, Serhiy Storchaka wrote: >> "(PyCFunction)" is redundant, Py_GenericAlias already has the right >> type. Overuse of casting to PyCFunction can hide actual bugs. >> > I borrowed that from listobject.c, which

Re: How to support annotations for a custom type in a C extension?

2021-09-18 Thread Serhiy Storchaka
18.09.21 09:40, Marco Sulla пише: > Ooook. I have a question. Why is this code not present in > dictobject.c? Where are the dict annotations implemented? In dictobject.c. -- https://mail.python.org/mailman/listinfo/python-list

Re: How to support annotations for a custom type in a C extension?

2021-09-18 Thread Serhiy Storchaka
18.09.21 03:54, MRAB пише: > static PyMethodDef customdict_methods[] = { > ... >     {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_CLASS | > METH_O | METH_COEXIST, PyDoc_STR("See PEP 585")}, > ... > }; > > > Note the flags: METH_CLASS says that it's a class method and > METH_COEXIST sa

Re: Defining a Python enum in a C extension - am I doing this right?

2021-08-06 Thread Serhiy Storchaka
03.08.21 13:03, Bartosz Golaszewski пише: > Just a follow-up: this is how I did it eventually: I think it can be simpler. 1. No need to create the __main__ module. You can just create a dict. If some attributes are required (e.g. __name__) it is easy to set them in the Python code (__name__ = 'py

Re: Defining a Python enum in a C extension - am I doing this right?

2021-07-30 Thread Serhiy Storchaka
23.07.21 11:20, Bartosz Golaszewski пише: > I'm working on a Python C extension and I would like to expose a > custom enum (as in: a class inheriting from enum.Enum) that would be > entirely defined in C. I think that it would be much easier to define it in Python, and then either import a Python

Re: Immutable view classes - inherit from dict or from Mapping?

2021-04-13 Thread Serhiy Storchaka
13.04.21 15:47, Peter Otten пише: > As to the | operator, maybe it could be added to Mapping? The | operator returns a new instance, but constructor is not a part of the Mapping interface. You cannot make a general implementation, and making __or__ an abstract method will break all existing Mappin

Re: Immutable view classes - inherit from dict or from Mapping?

2021-04-13 Thread Serhiy Storchaka
12.04.21 19:01, Andreas R Maier пише: > Hi, > I have written some classes that represent immutable views on collections > (see "immutable-views" package on Pypi). > > Currently, these view classes inherit from the abstract collection classes > such as Mapping, Sequence, Set. However, they implem

Re: Why assert is not a function?

2021-03-11 Thread Serhiy Storchaka
11.03.21 20:31, Chris Angelico пише: >> assert(expensive_computation()) > > Do you have any asserts like that, or is that a purely theoretical > complaint? I have never once seen anything that costly - usually it'll > be something like checking a length (and this isn't C's strlen, since > Python c

Re: yield from () Was: Re: weirdness with list()

2021-03-11 Thread Serhiy Storchaka
01.03.21 23:59, Cameron Simpson пише: > On 28Feb2021 23:47, Alan Gauld wrote: >> On 28/02/2021 00:17, Cameron Simpson wrote: >>> BUT... It also has a __iter__ value, which like any Box iterates over >>> the subboxes. For MDAT that is implemented like this: >>> >>> def __iter__(self): >>>

Re: Why assert is not a function?

2021-03-11 Thread Serhiy Storchaka
03.03.21 01:24, Chris Angelico пише: > On Wed, Mar 3, 2021 at 10:22 AM Mirko via Python-list > wrote: >> >> Am 02.03.2021 um 23:09 schrieb Stestagg: >>> Ignoring the question about this feature being particularly useful, it >> >> It is useful because "assert" is primarily (if not purely and >> exc

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Serhiy Storchaka
15.12.20 19:07, Mark Polesky via Python-list пише: > # Running this script > > D = {'a':1} > def get_default(): >     print('Nobody expects this') >     return 0 > print(D.get('a', get_default())) > > # ...generates this output: > > Nobody expects this > 1 > > ### > > Since I'm brand new t

Re: Changing strings in files

2020-11-11 Thread Serhiy Storchaka
10.11.20 11:07, Manfred Lotz пише: > Perhaps better. I like to use os.scandir this way > > def scantree(path: str) -> Iterator[os.DirEntry[str]]: > """Recursively yield DirEntry objects (no directories) > for a given directory. > """ > for entry in os.scandir(path): >

Re: Changing strings in files

2020-11-11 Thread Serhiy Storchaka
10.11.20 22:40, Dennis Lee Bieber пише: > Testing for extension in a list of exclusions would be much faster than > scanning the contents of a file, and the few that do get through would have > to be scanned anyway. Then the simplest method should work: read the first 512 bytes and check if

Re: constant folding - why not more

2020-11-11 Thread Serhiy Storchaka
11.11.20 02:46, Skip Montanaro пише: > I can think of two reasons. One, this kind of comparison will almost never > appear in production code (maybe in unit tests?). Unlike the C family of > languages, Python doesn't have a macro processor which would give symbolic > names to numeric constants or s

Re: Best way to determine user's screensize?

2020-10-31 Thread Serhiy Storchaka
30.10.20 17:56, flaskee via Python-list пише: > Perhaps a more tactical approach would best to figure > out how to do cross-platform python apps. > > What is the best approach to determining the user's available screensize, > when they open your python application? > > Note that the "desktop" app

Re: Pickle in C does not work

2020-10-20 Thread Serhiy Storchaka
20.10.20 17:12, Marco Sulla пише: > On Tue, 20 Oct 2020 at 16:07, Serhiy Storchaka wrote: >> You can use PyDict_New() + PyDict_Merge() to create a dict from your >> mapping. > Well, yes, I know. I just wrote it for simplicity now. Do you think this is > the problem? I thin

Re: Py_BuildValue vs PyTuple_Pack vs PyTuple_New

2020-10-20 Thread Serhiy Storchaka
20.10.20 18:05, Marco Sulla пише: > I read these three C api functions, They are similar, because they all > construct a tuple[1]. > > IMHO, Py_BuildValue is more simple to use than PyTuple_Pack that is more > simple to use than PyTuple_New. > > Where to use one or the other? What's the relative

Re: Pickle in C does not work

2020-10-20 Thread Serhiy Storchaka
20.10.20 01:28, Marco Sulla пише: > PyObject *d = PyObject_Call((PyObject *)&PyDict_Type, args, NULL); You can use PyDict_New() + PyDict_Merge() to create a dict from your mapping. > but I get: > _pickle.PicklingError: Can't pickle : attribute lookup > frozendict on builtins failed tp_name o

Re: Simple question - end a raw string with a single backslash ?

2020-10-15 Thread Serhiy Storchaka
15.10.20 22:16, Roland Müller via Python-list пише: > I used the triple single quotes as delimiter: > s = r'''a single quote ', a double quote "''' s > > 'a single quote \', a double quote "' It does not help if the string contains both kinds of triple quotes You have to use triple

Re: Simple question - end a raw string with a single backslash ?

2020-10-13 Thread Serhiy Storchaka
13.10.20 11:52, Tony Flury via Python-list пише: > I am trying to write a simple expression to build a raw string that ends > in a single backslash. My understanding is that a raw string should > ignore attempts at escaping characters but I get this : > >     >>> a = r'end\' >       File "", line

Re: Puzzling difference between lists and tuples

2020-09-21 Thread Serhiy Storchaka
18.09.20 03:55, Chris Angelico пише: > On Fri, Sep 18, 2020 at 10:53 AM Grant Edwards > wrote: >> Yea, the syntax for tuple literals has always been a bit of an ugly >> spot in Python. If ASCII had only had one more set of open/close >> brackets... > > ... then I'd prefer them to be used for set

Re: How to remove "" from starting of a string if provided by the user

2020-08-12 Thread Serhiy Storchaka
12.08.20 18:53, MRAB пише: > I would suggest: > while s[ : 1] in {'"', "'"} and s[ : 1] == s[-1 : ]: > ... s = s[1 : -1] And the condition can be written as s[ : 1] == s[-1 : ] in {'"', "'"} or more efficiently as s and s[0] == s[-1] in '\'"' -- https://mail.python.org/mailm

Re: Change in behaviour Python 3.7 > 3.8

2020-02-07 Thread Serhiy Storchaka
07.02.20 07:27, Frank Millman пише: @Serhiy I import the common object *from* Module A. Sorry, I incorrectly described the change made in issue1. Even if you import it from module A, you can see that it was set to None at the time of executing __del__. But you could see this even before

Re: Change in behaviour Python 3.7 > 3.8

2020-02-06 Thread Serhiy Storchaka
06.02.20 14:58, Frank Millman пише: I have noticed a change in behaviour in Python 3.8 compared with previous versions of Python going back to at least 2.7. I am pretty sure that it is not a problem, and is caused by my relying on a certain sequence of events at shutdown, which of course is not

Re: decorator needs access to variables where it is used.

2019-10-12 Thread Serhiy Storchaka
09.10.19 14:02, Chris Angelico пише: The decorator has full access to the function object, including a reference to that function's module globals. def trace(func): log = func.__globals__["log"] ... proceed as before As long as you can depend on "log" always being a module-level (glob

Re: python3, regular expression and bytes text

2019-10-12 Thread Serhiy Storchaka
12.10.19 21:08, Eko palypse пише: So how can I make it work with utf8 encoded text? You cannot. First, \w in re.LOCALE works only when the text is encoded with the locale encoding (cp1252 in your case). Second, re.LOCALE supports only 8-bit charsets. So even if you set the utf-8 locale, it w

Re: How do I give a decorator acces to the class of a decorated function

2019-09-05 Thread Serhiy Storchaka
04.09.19 17:21, Antoon Pardon пише: What I am trying to do is the following. class MyClass (...) : @register def MyFunction(...) ... What I would want is for the register decorator to somehow create/mutate class variable(s) of MyClass. Is that possible or do I have to rethin

Re: a,b = 2,3 and [a,b] = [2,3]

2019-09-03 Thread Serhiy Storchaka
03.09.19 11:02, Chris Angelico пише: On Tue, Sep 3, 2019 at 5:53 PM Serhiy Storchaka wrote: 02.09.19 12:24, Chris Angelico пише: But the curious difference happens in 3.7. I don't know what changed to cause this, but from there on, the list gets built and then unpacked. This was a

Re: a,b = 2,3 and [a,b] = [2,3]

2019-09-03 Thread Serhiy Storchaka
02.09.19 12:24, Chris Angelico пише: But the curious difference happens in 3.7. I don't know what changed to cause this, but from there on, the list gets built and then unpacked. This was a side effect of moving the optimization for `x in [a, b]` from the peepholer to the AST optimizer. -- h

Re: Are all items in list the same?

2019-01-08 Thread Serhiy Storchaka
08.01.19 11:07, Peter Otten пише: Bob van der Poel wrote: I need to see if all the items in my list are the same. I was using set() for this, but that doesn't work if items are themselves lists. So, assuming that a is a list of some things, the best I've been able to come up with it: if a

Re: Why Python don't accept 03 as a number?

2018-12-08 Thread Serhiy Storchaka
08.12.18 08:53, Henrik Bengtsson пише: A comment from the sideline: one could imagine extending the Python syntax with a (optional) 0d prefix that allows for explicit specification of decimal values. They would "complete" the family: * 0b: binary number * 0o: octal number * 0d: decimal number *

Re: Why Python don't accept 03 as a number?

2018-12-07 Thread Serhiy Storchaka
08.12.18 03:17, jf...@ms4.hinet.net пише: 00 0 03 File "", line 1 03 ^ SyntaxError: invalid token In Python 3.8 the error message will be more informative: 03 File "", line 1 SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for o

Re: Odd truth result with in and ==

2018-11-21 Thread Serhiy Storchaka
21.11.18 22:17, Cameron Simpson пише: Can someone show me a real world, or failing that - sane looking, chained comparison using "in"? s[0] == s[-1] in '\'"' Tests that string s starts and ends with a single or double quote. It can be also used with sets: elem in set1 <= set2 -- htt

Re: Python indentation (3 spaces)

2018-10-05 Thread Serhiy Storchaka
05.10.18 23:53, Chris Angelico пише: I don't understand how three spaces would prevent errors in a way that four wouldn't. In many editors and on terminal for a in x: if a: b() <-tab-->c() looks indistinguishable from for a in x: if a: b() c() but the former i

Re: Calling an unbound method in C using the Public API

2018-08-30 Thread Serhiy Storchaka
29.08.18 17:33, Matthieu Dartiailh пише: I tried to look at the public C API for a way to call an unbound method with a minimal cost (in term of speed and memory). It seems to me, but please correct me if I am wrong, that one cannot call a MethodDef using only the public API. To use the public

Re: Are dicts supposed to raise comparison errors

2018-08-01 Thread Serhiy Storchaka
01.08.18 21:03, Chris Angelico пише: And in any code that does not and cannot run on Python 2, the warning about bytes and text comparing unequal is nothing more than a false positive. Not always. If your code supported Python 2 in the past, or third-party dependencies supports or supported Py

Re: Dealing with dicts in doctest

2018-07-06 Thread Serhiy Storchaka
05.07.18 20:57, Steven D'Aprano пише: I think that's really clever. Is it too clever? How do you deal with dicts in doctests? There was a proposition for adding a doctest option for order-insensitive matching (like NORMALIZE_WHITESPACE and ELLIPSIS). But it was rejected because there is a sim

Re: Why exception from os.path.exists()?

2018-06-01 Thread Serhiy Storchaka
02.06.18 03:05, Chris Angelico пише: The permissions error is reported differently by stat, but then exists() just says "oh that's an OS error so we're going to say it doesn't exist". If you truly want the reliable form of os.path.exists, it would be this: try: os.stat(path) return Tru

Re: Why exception from os.path.exists()?

2018-06-01 Thread Serhiy Storchaka
01.06.18 16:58, Chris Angelico пише: Possibly more confusing, though, is this: os.path.exists(1) True os.path.exists(2) True os.path.exists(3) False I think it's testing that the file descriptors exist, because os.path.exists is defined in terms of os.stat, which can stat a path or an FD.

Re: Enums: making a single enum

2018-05-26 Thread Serhiy Storchaka
26.05.18 08:07, Ian Kelly пише: On Fri, May 25, 2018 at 11:00 PM, Chris Angelico wrote: On Sat, May 26, 2018 at 2:46 PM, Steven D'Aprano wrote: Am I silly for wanting to make a single enum? I have a three-state flag, True, False or Maybe. Is is confusing or bad practice to make a single enum

Re: List replication operator

2018-05-25 Thread Serhiy Storchaka
25.05.18 10:28, Peter Otten пише: Steven D'Aprano wrote: But what do people think about proposing a new list replication with copy operator? [[]]**5 would return a new list consisting of five shallow copies of the inner list. Yet another arcanum to learn for beginners with little retur

Re: what does := means simply?

2018-05-17 Thread Serhiy Storchaka
17.05.18 15:07, Paul Moore пише: It's a good example, because it makes it clear that the benefits of := are at least in some cases, somewhat dependent on the fact that Python evaluates arguments left to right :-) Unless it evaluates them in other order. -- https://mail.python.org/mailman/listi

Re: syntax oddities

2018-05-15 Thread Serhiy Storchaka
15.05.18 22:10, Tobiah пише: Why is it len(object) instead of object.len? Because the first form looked better to Guido van Rossum. Why is it getattr(object, item) rather then object.getattr(item)? How do you get the 'getattr' attribute then? -- https://mail.python.org/mailman/listinfo/pyt

Re: What's the rationale for b"..." in this example?

2018-05-15 Thread Serhiy Storchaka
15.05.18 18:14, Skip Montanaro пише: Consider this: bytes("abc", encoding="utf-8") b'abc' Looks reasonable. Then consider this: str(bytes("abc", encoding="utf-8")) "b'abc'" Why is the b'...' bit still there? I suppose it's because I didn't tell it explicitly how to decode the bytes object

Re: Tracebacks for exceptions in interactively entered code.

2018-05-07 Thread Serhiy Storchaka
07.05.18 22:59, Terry Reedy пише: I intend to improve the IDLE doc section on IDLE-console differences. Don't haste to document it. The behavior of the standard interactive mode can be changed in 3.8. -- https://mail.python.org/mailman/listinfo/python-list

Re: Why is calling eval so slow?

2018-05-05 Thread Serhiy Storchaka
06.05.18 05:29, Steven D'Aprano пише: On Sat, 05 May 2018 20:11:28 +0300, Serhiy Storchaka wrote: Why so slow? 1. Add an overhead of calling eval(), not just looking up its name. It is comparable with a time of calling a simple lambda. 2. Add an overhead of getting the globals dic

Re: Why is calling eval so slow?

2018-05-05 Thread Serhiy Storchaka
05.05.18 19:10, Steven D'Aprano пише: # calling a regular function python3.5 -m timeit -s "f = lambda: 99" "f()" # evaluating a function code object python3.5 -m timeit -s "f = (lambda: 99).__code__" "eval(f)" # estimate the overhead of the eval name lookup python3.5 -m timeit "eval" # evaluating

Re: itemgetter with default arguments

2018-05-05 Thread Serhiy Storchaka
05.05.18 12:59, Steven D'Aprano пише: On Sat, 05 May 2018 10:31:17 +0300, Serhiy Storchaka wrote: Consider a concrete example. You need to sort a list of 2- or 3- element tuples by the first and third items (third items are strings if presented). itemgetter(0, 2) doesn't work be

Re: itemgetter with default arguments

2018-05-05 Thread Serhiy Storchaka
04.05.18 20:04, Steven D'Aprano пише: On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote: On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano wrote: Here are the specifications: * you must use lambda, not def; Why? This seems like an arbitrary constraint. You'll have to ask the two core devs

Re: Tk covering the entire screen

2018-05-05 Thread Serhiy Storchaka
04.05.18 23:15, Skip Montanaro пише: I forgot to mention that when running on Linux (displaying back on Windows), the Python 3 version (3.6.4, Tk 8.6) does cover all three screens. The Windows Python 2.7.14 version with Tk 8.5 has problems. Try to upgrade to 2.7.15. It should be shipped with Tk

Re: Tk covering the entire screen

2018-05-05 Thread Serhiy Storchaka
04.05.18 22:54, Skip Montanaro пише: I suspect this is a Tk issue as much as anything, but this is the only place I know to ask Tk questions. Any ideas? There is more specific place for Tkinter questions: https://mail.python.org/mailman/listinfo/tkinter-discuss -- https://mail.python.org/mail

Improving syntax error messages

2018-04-26 Thread Serhiy Storchaka
What syntax errors did you see most often? Which of them looks the most weird? How would you like to improve their messages. -- https://mail.python.org/mailman/listinfo/python-list

Re: Pep8 for long pattern

2018-03-27 Thread Serhiy Storchaka
27.03.18 17:17, Ganesh Pal пише: How do I split the below regex , so that it fits within the character limit of 79 words pattern = [ r'(?P([0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+::HEAD))', r'(?P(owner:\s+[0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]

Re: Since when builtin dict preserve key order?

2018-03-24 Thread Serhiy Storchaka
24.03.18 10:48, Marko Rauhamaa пише: Chris Angelico : On Sat, Mar 24, 2018 at 3:34 PM, Arkadiusz Bulski wrote: I already asked on PYPY and they confirmed that any version of pypy, including 2.7, has dict preserving insertion order. I am familiar with ordered **kw which was introduced in 3.6 bu

Re: RFC: Proposal: Deterministic Object Destruction

2018-03-01 Thread Serhiy Storchaka
02.03.18 02:49, Rick Johnson пише: This is true, but it does not answer the challenge directly because function CRs are a consequence of Python _internals_ *NOT* consequences of a custom class written by a programmer which references itself _explicitly_. This doesn't matter. You question was "C

Re: RFC: Proposal: Deterministic Object Destruction

2018-02-28 Thread Serhiy Storchaka
01.03.18 04:46, Rick Johnson пише: On Wednesday, February 28, 2018 at 5:02:17 PM UTC-6, Chris Angelico wrote: Here's one example: reference cycles. When do they get detected? Taking a really simple situation: class Foo: def __init__(self): self.self = self *shudders* Can you p

Re: How to make Python run as fast (or faster) than Julia

2018-02-22 Thread Serhiy Storchaka
22.02.18 14:29, Chris Angelico пише: Not overly misleading; the point of it is to show how trivially easy it is to memoize a function in Python. For a fair comparison, I'd like to see the equivalent Julia code: the function, unchanged, with something around the outside of it to manage caching and

Re: [RELEASED] Python 3.4.8 and Python 3.5.5 are now available

2018-02-10 Thread Serhiy Storchaka
05.02.18 02:35, Larry Hastings пише: On behalf of the Python development community, I'm happy to announce the availability of Python 3.4.8 and Python 3.5.5. Both Python 3.4 and 3.5 are in "security fixes only" mode.  Both versions only accept security fixes, not conventional bug fixes, and bo

Re: Why no '|' operator for dict?

2018-02-05 Thread Serhiy Storchaka
05.02.18 10:14, Ian Kelly пише: On Mon, Feb 5, 2018 at 12:35 AM, Frank Millman wrote: So I have 2 questions - 1. Is there any particular reason why '|' is not supported? '|' is the set union operation, roughly equivalent to the set.union method. Dicts don't have a union operation. If they di

Re: Is this the right way to write a codec error handler?

2018-01-20 Thread Serhiy Storchaka
20.01.18 10:32, Steven D'Aprano пише: I want an error handler that falls back on Latin-1 for anything which cannot be decoded. Is this the right way to write it? def latin1_fallback(exception): assert isinstance(exception, UnicodeError) start, end = exception.start, exception.end

Re: Generating SVG from turtle graphics

2018-01-14 Thread Serhiy Storchaka
11.01.18 13:03, Steven D'Aprano пише: I'd like to draw something with turtle, then generate a SVG file from it. Is this possible? If not, is there something I can do which lets me plot lines, shapes and curves and output to SVG? You can translate the following Tcl/Tk recipe to Python/Tkinter:

Re: Why does __ne__ exist?

2018-01-07 Thread Serhiy Storchaka
07.01.18 22:33, Chris Angelico пише: On Mon, Jan 8, 2018 at 7:13 AM, Thomas Jollans wrote: On 07/01/18 20:55, Chris Angelico wrote: Under what circumstances would you want "x != y" to be different from "not (x == y)" ? In numpy, __eq__ and __ne__ do not, in general, return bools. a = np.ar

Re: Compile Python 3 interpreter to force 2-byte unicode

2017-11-29 Thread Serhiy Storchaka
26.11.17 21:46, wojtek.m...@gmail.com пише: On Sunday, November 26, 2017 at 1:00:19 AM UTC+1, Terry Reedy wrote: You must be trying to compile 2.7. There may be Linux distributions that compile this way. You're right, I need 2.7. Any hint which distro has got these settings? UCS-2 is used b

Re: Compile Python 3 interpreter to force 2-byte unicode

2017-11-26 Thread nospam . Serhiy Storchaka
26.11.17 01:59, Terry Reedy D¿D,ÑêDµ: > On 11/25/2017 5:12 PM, Chris Angelico wrote: >> On Sun, Nov 26, 2017 at 9:05 AM,â wrote: >>> Hi, my goal is to obtain an interpreter that internally >>> uses UCS-2. Such a simple code should print 65535: >>> >>> â â import sys >>> â â print sys.maxunicode

Re: Compile Python 3 interpreter to force 2-byte unicode

2017-11-25 Thread Serhiy Storchaka
26.11.17 01:59, Terry Reedy пише: On 11/25/2017 5:12 PM, Chris Angelico wrote: On Sun, Nov 26, 2017 at 9:05 AM,  wrote: Hi, my goal is to obtain an interpreter that internally uses UCS-2. Such a simple code should print 65535:    import sys    print sys.maxunicode This is enabled in Windows,

Re: A use-case for for...else with no break

2017-11-03 Thread Serhiy Storchaka
02.11.17 12:10, Steve D'Aprano пише: Occasionally it is useful to loop over a bunch of stuff in the interactive interpreter, printing them as you go on a single line: for x in something(): print(x, end='') If you do that, the prompt overwrites your output, and you get a mess: py> for x i

Re: Performance of map vs starmap.

2017-11-01 Thread Serhiy Storchaka
30.10.17 12:10, Kirill Balunov пише: Sometime ago I asked this question at SO [1], and among the responses received was paragraph: - `zip` re-uses the returned `tuple` if it has a reference count of 1 when the `__next__` call is made. - `map` build a new `tuple` that is passed to the mapped

Re: Standard for dict-contants with duplicate keys?

2017-09-15 Thread Serhiy Storchaka
16.09.17 00:45, Terry Reedy пише: On 9/15/2017 3:36 PM, Tim Chase wrote: Looking through docs, I was unable to tease out whether there's a prescribed behavior for the results of defining a dictionary with the same keys multiple times d = { "a": 0, "a": 1, "a": 2, }

Re: Python programming language vulnerabilities

2017-09-10 Thread Serhiy Storchaka
08.09.17 20:34, Stephen Michell пише: I chair ISO/IEC/JTC1/SC22/WG23 Programming Language Vulnerabilities. We publish an international technical report, ISO IEC TR 24772 Guide to avoiding programming language vulnerabilities through language selection use. Annex D in this document addresses vu

Re: Case-insensitive string equality

2017-08-31 Thread Serhiy Storchaka
31.08.17 17:38, Steve D'Aprano пише: On Thu, 31 Aug 2017 11:45 pm, Serhiy Storchaka wrote: It is not clear what is your problem exactly. That is fair. This is why I am discussing it here first, before taking it to Python-Ideas. At the moment my ideas on the matter are still half-f

Re: Case-insensitive string equality

2017-08-31 Thread Serhiy Storchaka
31.08.17 15:49, Steve D'Aprano пише: On Thu, 31 Aug 2017 05:51 pm, Serhiy Storchaka wrote: 31.08.17 10:10, Steven D'Aprano пише: (iii) Not every two line function needs to be in the standard library. Just add this to the top of every module: def equal(s, t): return

Re: If you are running 32-bit 3.6 on Windows, please test this

2017-08-31 Thread Serhiy Storchaka
31.08.17 08:13, Steven D'Aprano пише: As far as I can see, apart from tests for NAN and ±INF, there are no tests of math.sqrt on floats at all. See test_testfile in test_math.py. -- https://mail.python.org/mailman/listinfo/python-list

Re: Case-insensitive string equality

2017-08-31 Thread Serhiy Storchaka
31.08.17 10:10, Steven D'Aprano пише: (iii) Not every two line function needs to be in the standard library. Just add this to the top of every module: def equal(s, t): return s.casefold() == t.casefold() This is my answer. Unsolved problems: This proposal doesn't help with sets and dic

Re: Proposed new syntax

2017-08-11 Thread Serhiy Storchaka
10.08.17 23:28, Ian Kelly пише: So, perhaps a better syntax could be: [x + 1 for x in (0, 1, 2, 999, 3, 4) if x < 5 else break] (0, 1, 2, 999, 3, 4) if x < 5 else break looks too similar to the ternary operator. -- https://mail.python.org/mailman/listinfo/python-list

Re: Proposed new syntax

2017-08-11 Thread Serhiy Storchaka
10.08.17 17:28, Steve D'Aprano пише: Every few years, the following syntax comes up for discussion, with some people saying it isn't obvious what it would do, and others disagreeing and saying that it is obvious. So I thought I'd do an informal survey. What would you expect this syntax to return

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Serhiy Storchaka
03.08.17 19:08, Ian Kelly пише: This turns out to be because I was running 3.4 which doesn't have the C implementation to begin with. In 3.6 this trick doesn't seem to work as expected for disabling it: It didn't work because the functools module already was imported at startup. $ ./python -m

Re: @lru_cache on functions with no arguments

2017-08-03 Thread Serhiy Storchaka
03.08.17 18:36, Ian Kelly пише: The single variable is only a dict lookup if it's a global. Locals and closures are faster. def simple_cache(function): sentinel = object() cached = sentinel @functools.wraps(function) def wrapper(*args, **kwargs): nonlocal cached

Re: Progress on the Gilectomy

2017-06-10 Thread Serhiy Storchaka
10.06.17 15:54, Steve D'Aprano пише: Larry Hastings is working on removing the GIL from CPython: https://lwn.net/Articles/723949/ For those who don't know the background: - The GIL (Global Interpreter Lock) is used to ensure that only one piece of code can update references to an object at a

Re: Scala considering significant indentation like Python

2017-05-22 Thread Serhiy Storchaka
22.05.17 17:24, Skip Montanaro пише: On Mon, May 22, 2017 at 9:14 AM, bartc wrote: I think 'end' can be used in Python too: if (cond): stmts end But: - You need to define a dummy variable 'end' Interesting idea. You can avoid the dummy variable part by just using '#end', however:

Re: Ten awesome things you are missing out on if you're still using Python 2

2017-05-09 Thread Serhiy Storchaka
On 09.05.17 09:01, Rustom Mody wrote: On Monday, May 8, 2017 at 12:48:03 PM UTC+5:30, Steven D'Aprano wrote: http://www.asmeurer.com/python3-presentation/slides.html#1 Nice list thanks! Do you have a similar list of 10 awesome features of Python that you can't use because you refuse to upgrad

Re: Calling dunder methods manually

2017-04-13 Thread Serhiy Storchaka
On 13.04.17 08:29, Steven D'Aprano wrote: Should you call dunder methods (Double leading and trailing UNDERscores) manually? For example: my_number.__add__(another_number) The short answer is: NO! In general, you shouldn't do it. Guido recently commented: I agree that one shouldn't ca

Re: Manual parameter substitution in sqlite3

2017-03-01 Thread Serhiy Storchaka
On 28.02.17 19:28, Skip Montanaro wrote: Most of the time (well, all the time if you're smart), you let the database adapter do parameter substitution for you to avoid SQL injection attacks (or stupid users). So: curs.execute("select * from mumble where key = ?", (key,)) If you want to sele

Re: Is shutil.get_terminal_size useless?

2017-01-29 Thread Serhiy Storchaka
On 28.01.17 10:03, Steve D'Aprano wrote: Is shutil.get_terminal_size useless? When, if ever, should I use it in preference to the os version? If the shutil version is broken, can it be fixed? Read the history of shutil.get_terminal_size(). All this was discussed. -- https://mail.python.org/mai

Re: Temporary variables in list comprehensions

2017-01-09 Thread Serhiy Storchaka
On 09.01.17 12:46, Paul Rubin wrote: Serhiy Storchaka writes: gen = (expensive_calculation(x) for x in data) result = [(tmp, tmp + 1) for tmp in gen] result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)] Yes, of course, but only in the case of one-argument function

Re: Temporary variables in list comprehensions

2017-01-09 Thread Serhiy Storchaka
On 09.01.17 05:53, Steven D'Aprano wrote: Suppose you have an expensive calculation that gets used two or more times in a loop. The obvious way to avoid calculating it twice in an ordinary loop is with a temporary variable: result = [] for x in data: tmp = expensive_calculation(x) result

Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Serhiy Storchaka
On 31.12.16 02:30, Steve D'Aprano wrote: Are you saying that the best way of doing this is this? (1) convert signed Python ints to unsigned; (2) perform operation and bitmask; (3) convert unsigned back to signed. Here's an example. I want to multiply 7*3 using a signed 4-bit int, getting 5 a

Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Serhiy Storchaka
On 30.12.16 16:47, Steve D'Aprano wrote: Again, assume both operands are in range for an N-bit signed integer. What's a good way to efficiently, or at least not too inefficiently, do the calculations in Python? def to_unsigned(bits, x): return x & ((1

Re: if iter(iterator) is iterator

2016-11-13 Thread Serhiy Storchaka
On 14.11.16 02:40, Steve D'Aprano wrote: I'm surprised that the inspect module doesn't appear to have isiterable and isiterator functions. Here's my first attempt at both: Just use isinstance() with collections ABC classes. -- https://mail.python.org/mailman/listinfo/python-list

Re: Reversing \N{...} notation?

2016-10-27 Thread Serhiy Storchaka
On 26.10.16 23:47, Peter Otten wrote: def mynamereplace(exc): return u"".join( "\\N{%s}" % unicodedata.name(c) for c in exc.object[exc.start:exc.end] ), exc.end codecs.register_error("namereplace", mynamereplace) Not all characters has standard na

Re: Different behaviour of regexp in 3.6.0b2

2016-10-15 Thread Serhiy Storchaka
On 14.10.16 19:15, Chris Angelico wrote: I wasn't specifically aware that the re module was doing the same thing, but it'll be from the same purpose and goal. The idea is that, for instance, Windows path names in non-raw string literals will no longer behave differently based on whether the path

Re: Different behaviour of regexp in 3.6.0b2

2016-10-15 Thread Serhiy Storchaka
On 14.10.16 20:01, Peter Otten wrote: Lele Gaifax wrote: So, how am I supposed to achieve the mentioned intent? By doubling the escape in the replacement? If there are no escape sequences aimed to be handled by re.sub() you can escape the replacement wholesale: re.sub(r'\s+', re.escape(r'\s+

Re: Different behaviour of regexp in 3.6.0b2

2016-10-15 Thread Serhiy Storchaka
On 14.10.16 18:40, Lele Gaifax wrote: Hi all, trying out pgcli with Python 3.6.0b2 I got an error related to what seem a different behaviour, or even a bug, of re.sub(). The original intent is to replace spaces within a string with the regular expression \s+ (see https://github.com/dbcli/pgcl

Re: repr/str diff between Python 2 and 3

2016-10-11 Thread Serhiy Storchaka
On 12.10.16 04:30, Skip Montanaro wrote: https://docs.python.org/3/whatsnew/3.1.html It's the third hit when searching for 'float'. Assuming I understand what it's saying. ;) Thanks. Is that the "David Gay's algorithm"? That seems to apply only to repr(), while the change I observed was in

Re: [RELEASE] Python 3.6.0b1 is now available

2016-09-14 Thread Serhiy Storchaka
On 14.09.16 17:36, Guido van Rossum wrote: Fortunately that page isn't linked from anywhere on the home page AFAIK. If it is, could someone file an issue in the pydotorg tracker? The url is at the bottom of every page. This is on of the first results (actually the first besides manually edited

Re: Expression can be simplified on list

2016-09-14 Thread Serhiy Storchaka
On 14.09.16 11:28, Steven D'Aprano wrote: I'm not sure if it makes sense to talk about an "empty regular expression", or if that is the same as re.compile(''). Presumably if such a thing makes sense, it would match nothing, from any input at all. Actually, it matches anything. re.compile('').ma

Re: Detecting repeated subsequences of identical items

2016-04-21 Thread Serhiy Storchaka
On 21.04.16 06:07, Steven D'Aprano wrote: Now I want to group subsequences. For example, I have: "ABCABCABCDEABCDEFABCABCABCB" and I want to group it into repeating subsequences. [...] How can I do this? Does this problem have a standard name and/or solution? This is a part of lossless da

Re: How to parameterize unittests

2016-04-15 Thread Serhiy Storchaka
On 15.04.16 11:20, Antoon Pardon wrote: But the tests, at this moment, are not written to instantiate self.tree but to call avltree directly. So I have to rewrite these tests. That will IMO involve a lot of cut and paste. There is yet one approach. Import your original test file, patch it by s

Re: How to parameterize unittests

2016-04-15 Thread Serhiy Storchaka
On 14.04.16 18:05, Steven D'Aprano wrote: On Fri, 15 Apr 2016 12:08 am, Antoon Pardon wrote: I have a unittest for my avltree module. Now I want this unittest to also run on a subclass of avltree. How can I organise this, so that I can largely reuse the original TestCase? class Test_AVLTree(

Re: Adding Icon To Tkinter Window - Followup

2016-03-09 Thread Serhiy Storchaka
On 06.03.16 18:30, Wildman via Python-list wrote: That does not work... $ ./makexface.py Traceback (most recent call last): File "./makexface.py", line 236, in root.wm_iconphoto(True, img) File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1897, in __getattr__ return getattr(self

Re: Pyhon 2.x or 3.x, which is faster?

2016-03-08 Thread Serhiy Storchaka
On 08.03.16 03:34, Steven D'Aprano wrote: Chris, I think that's exactly what BartC has done: he has a program that he actually uses, one which processes JPG files. It's written in pure Python, so he's not comparing the quality of C libraries, he's comparing Python versus Python. I haven't looked

  1   2   3   >