Re: How to make Python run as fast (or faster) than Julia
Am 27.02.18 um 03:27 schrieb Chris Angelico: On Tue, Feb 27, 2018 at 12:57 PM, bartc wrote: Anyway, even this pure Python version can deliver pseudo random numbers at some 200,000 per second, while the built-in generator does 450,000 per second, so it's not bad going. The built-in generator is using a completely different algorithm though, so rate of generation isn't the entire story. How long is the period of the one you're using? (How long before it loops?) If you churn the generator to an arbitrary number and then take the next 100 values it generates, are they randomly distributed? George Marsaglia is a researcher who invented a couple of PRNGs which are both simple (one of the first was called KISS) yet surprisingly good. See here: https://en.wikipedia.org/wiki/KISS_(algorithm) They possess all these desirable properties you mention above and pass the DIEHARD random number generator tests (also invented by Marsaglia). Can you reconstruct the RNG's internal state by watching it generate numbers for a short while? https://eprint.iacr.org/2011/007.pdf Obviously no PRNG is going to be perfect at this, but there are certainly degrees of quality to be compared. It is a field of research and the generator shown by Bart is one of the better generators constructed by an expert in the field. Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: Questions about `locals` builtin
On Mon, 26 Feb 2018 17:05:46 -0800, Dan Stromberg wrote: [...] > I don't have IronPython handy, but according my (quite possibly flawed) > test program, locals() is a copy on CPython 3, CPython 2, Pypy3, Pypy, > Jython and MicroPython. > > I didn't see any interpreters that returned the namespace itself. > > What am I missing? Interesting... it seems that Jython is less consistent than I remembered. Here is my test function: def test(): y = 2 ns = locals() ns['x'] = 1 ns['y'] = 1 print(x, y) return # Fool the compiler into treating x as a local. if False: x = 999 In Jython 2.5, it prints (1, 2). So assigning to locals creates a new local variable (x) but doesn't update an existing one (y). In CPython, I get: UnboundLocalError: local variable 'x' referenced before assignment in versions 3.5, 2.7, 2,5, 2,4 and even 1.5 (it's just a regular NameError in 1.5). I broke my IronPython installation, but my recollection is that it would print (1, 1). (That's what I thought Jython did too, but I was mistaken.) So correction is noted: in Jython, at least, locals() is not the namespace itself, but some sort of weird proxy to it. -- Steve -- https://mail.python.org/mailman/listinfo/python-list
In Python2, does it need to wrap imp.find/load_module with imp_acquire/release_lock?
Just like the title. It seems to me it is needed from the source code but codes in stdlib all doesn't do that. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there are good DRY fix for this painful design pattern?
On 26.02.2018 15:41, Steven D'Aprano wrote: I have a class with a large number of parameters (about ten) assigned in `__init__`. The class then has a number of methods which accept *optional* arguments with the same names as the constructor/initialiser parameters. If those arguments are None, the defaults are taken from the instance attributes. Others have pointed out good solutions already, in particular, combining inspect and decorators or encapsulating the parameters in an object. Alternatively, you could reconsider your class design. Although I can't tell from your example whether this idea would be acceptable for your use case, consider removing your parameters from the class methods (except from __init__) altogether so the values passed during instantiation cannot be changed later. In exchange, add module-level functions corresponding to each of your class methods that accept **kwargs and that generate new instances of your class passing **kwargs on to __init__, then call the corresponding instance method. The stdlib textwrap module, for example, uses this approach. Best, Wolfgang -- https://mail.python.org/mailman/listinfo/python-list
Re: Questions about `locals` builtin
2018-02-27 2:57 GMT+03:00 Terry Reedy : > The point of point 3 is that terminology and details would likely be > different if Python were freshly designed more or less as it is today, and > some things only make more or less sense in historical context. Learn what > you need to know to write code that works. > Thank you, I'm fairly familiar with the scope and namespace concepts in Python 3, and they are also very well described in the "Execution model" https://docs.python.org/3/reference/executionmodel.html#execution-model, for this special thanks to the person who wrote it ;-) I started using Python with СPython 3.5 and I'm not familiar with the Python 2 features. But since Python 2 got into our discussion, I still have a question: a. Is this restriction for locals desirable in the implementation of CPython in Python 3? b. Or is it the result of temporary fixes for Python 2? Personally, I find the convenient functionality to update the local symbol table inside a function, similar to `globals`. Of course, I do not have the full picture and the difficulties with the flaws in implementing such functionality in CPython3. On the other hand, I understand that this additional dynamism as a whole may not be beneficial, and that local and global variables are determined at function compilation time. At this point, I only know one way to achieve this: `exec ('% s =% s'% (var_name, var_val))`, which I find clumsy enough. Funny, contradictory thoughts in my head :) Calling surrounding function local names collectively 'nonlocals' is the > result of months of bikeshedding. > Yes `nonlocals` is a cool fix in Python3. 2018-02-27 3:25 GMT+03:00 Steven D'Aprano : > Mostly because locals() predates MappingProxyType by many years, and also > because that restriction doesn't apply to other implementations of Python > such as Jython and IronPython. > > In CPython, the dict returned by locals() is a copy of the local > namespace, so modifying the dict doesn't modify the real local variables. > > (Well, sometimes it does, but not always. The story in Python 2 is really > complex.) Yes, I understand all the more the documentation specifies *New in version 3.3: class.MappingProxyType.* I'm not saying that this should be a standard for the Python language, especially in the context of what I wrote above. But the Python documentation already contains links to the features of the CPython implementation, (`id` for example). If the answer to the previous question is "Yes, such a restriction is desirable in CPython because, because, because ... and thereafter it is not planned to be changed." Then I do not see why it can not be made more explicit by changing `dict` to` types.MappingProxyType`. With the following change in the documentation: "Note The contents of this dictionary should be perceived as read-only mapping and should not be modified; The returned mapping type is implementation-dependent: changes may not affect the values of local variables used by the interpreter or the returned object may not support item assignment. CPython implementation detail: The returned object is `types.MappingProxyType` a read-only proxy of the current local symbol table." p.s.: Steven, this question was somewhat inspired by yours "Is there are good DRY fix for this painful design pattern?" With kind regards, -gdg -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make Python run as fast (or faster) than Julia
Christian Gollwitzer writes: > George Marsaglia is a researcher who invented a couple of PRNGs which > are both simple (one of the first was called KISS) yet surprisingly > good. s/is/was/ Sadly, he died a few years ago (2011). -- Ben. -- https://mail.python.org/mailman/listinfo/python-list
[newbie] how to remove empty lines from webpage/file
Dear all, I try to get the numerical data from the following webpage: http://www.astro.oma.be/GENERAL/INFO/nzon/zon_2018.html With the following code-fragment I was already able to get a partial result: #!/usr/bin/env python #memo: install bs4 as follows: sudo easy_install bs4 # -*- coding: utf-8 -*- #3 lines below necessary to avoid encoding problem import sys reload(sys) sys.setdefaultencoding('utf8') import urllib2 file = open("testfile.txt","w") source = "http://www.astro.oma.be/GENERAL/INFO/nzon/zon_2018.html"; page = urllib2.urlopen(source) from bs4 import BeautifulSoup soup = BeautifulSoup(page,'lxml') lines=soup.get_text() file.write(lines) file.close() I tried to delete the empty lines but I am totally stuck at this moment, can anyone help me further? thanks in advance jens -- https://mail.python.org/mailman/listinfo/python-list
Re: matrix multiplication
Seb wrote: > On Tue, 27 Feb 2018 12:25:30 +1300, > Gregory Ewing wrote: > >> Seb wrote: >>> I was wondering is whether there's a faster way of multiplying each >>> row (1x3) of a matrix by another matrix (3x3), compared to looping >>> through the matrix row by row as shown in the code. > >> Just multiply the two matrices together. > >> If A is an nx3 matrix and B is a 3x3 matrix, then C = A @ B is an nx3 >> matrix where C[i] = A[i] @ B. > >> (This is a property of matrix multiplication in general, nothing >> special about numpy.) > > I think that's only true if B is the same for every row in A. In the > code I posted, B varies by row of A. Yeah, you would have to substitute the N 3x3 matrices with an Nx3x3 tensor, though I don't know if numpy provides an op such that Nx3 op Nx3x3 --> desired result or op(Nx3, Nx3x3) --> desired result -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make Python run as fast (or faster) than Julia
On 27/02/2018 02:27, Chris Angelico wrote: On Tue, Feb 27, 2018 at 12:57 PM, bartc wrote: On 27/02/2018 00:35, Chris Angelico wrote: Anyway, even this pure Python version can deliver pseudo random numbers at some 200,000 per second, while the built-in generator does 450,000 per second, so it's not bad going. The built-in generator is using a completely different algorithm though, so rate of generation isn't the entire story. How long is the period of the one you're using? (How long before it loops?) I believe it's 5*2**1320480*(2**64-1) according to the author's comment. I haven't tested that. (By looping I understand that to mean, before the same sequence starts again. Because as the numbers are 64 bits, individual numbers will inevitably be repeated from time to time.) -- Bartc -- https://mail.python.org/mailman/listinfo/python-list
Re: Questions about `locals` builtin
On 2/27/18 3:52 AM, Kirill Balunov wrote: a. Is this restriction for locals desirable in the implementation of CPython in Python 3? b. Or is it the result of temporary fixes for Python 2? My understanding is that the behavior of locals() is determined mostly by what is convenient for the implementors, so that they can keep regular code running as quickly as possible. The answer to the question, "why can't we make locals() work more like I expect?" is, "because that would make things slower." Personally, I find the convenient functionality to update the local symbol table inside a function, similar to `globals`. Can you show us an example of why you would want to update locals through locals()? There might be more natural ways to solve your problem. --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to make Python run as fast (or faster) than Julia
On Tue, 27 Feb 2018 11:27:21 +, bartc wrote: [...] >> The built-in generator is using a completely different algorithm >> though, so rate of generation isn't the entire story. How long is the >> period of the one you're using? (How long before it loops?) > > I believe it's 5*2**1320480*(2**64-1) according to the author's comment. > > I haven't tested that. What, you haven't run a full period of the generator? I thought your code was fast! *wink* Sorry, I couldn't resist... :-) But for the record, if we could generate a million million numbers per second, that is one every picosecond, the total time would be of the order of 10**397504 years. That's a one followed by three hundred ninety- seven thousand zeroes. > (By looping I understand that to mean, before the same sequence starts > again. Because as the numbers are 64 bits, individual numbers will > inevitably be repeated from time to time.) Yes, that's what the period means: how long it takes for the entire sequence to repeat, not just a single number. -- Steve -- https://mail.python.org/mailman/listinfo/python-list
Re: matrix multiplication
On Tue, Feb 27, 2018 at 4:08 AM, Peter Otten <__pete...@web.de> wrote: > Seb wrote: > >> On Tue, 27 Feb 2018 12:25:30 +1300, >> Gregory Ewing wrote: >> >>> Seb wrote: I was wondering is whether there's a faster way of multiplying each row (1x3) of a matrix by another matrix (3x3), compared to looping through the matrix row by row as shown in the code. >> >>> Just multiply the two matrices together. >> >>> If A is an nx3 matrix and B is a 3x3 matrix, then C = A @ B is an nx3 >>> matrix where C[i] = A[i] @ B. >> >>> (This is a property of matrix multiplication in general, nothing >>> special about numpy.) >> >> I think that's only true if B is the same for every row in A. In the >> code I posted, B varies by row of A. > > Yeah, you would have to substitute the N 3x3 matrices with an Nx3x3 tensor, > though I don't know if numpy provides an op such that > > Nx3 op Nx3x3 --> desired result > > or > > op(Nx3, Nx3x3) --> desired result Nx1x3 @ Nx3x3 ought to do it, with the result being Nx1x3. -- https://mail.python.org/mailman/listinfo/python-list
Re: Questions about `locals` builtin
2018-02-27 14:59 GMT+03:00 Ned Batchelder : > On 2/27/18 3:52 AM, Kirill Balunov wrote: > >> a. Is this restriction for locals desirable in the implementation of >> CPython in Python 3? >> b. Or is it the result of temporary fixes for Python 2? >> > > My understanding is that the behavior of locals() is determined mostly by > what is convenient for the implementors, so that they can keep regular code > running as quickly as possible. The answer to the question, "why can't we > make locals() work more like I expect?" is, "because that would make things > slower." > Ok, but I in this case what is the benefit in Python 3.3+ in returning a copy of dict instead of MappingProxy? Personally, I find the convenient functionality to update the local symbol >> table inside a function, similar to `globals`. >> > > Can you show us an example of why you would want to update locals through > locals()? There might be more natural ways to solve your problem. > > The example from "Is there are good DRY fix for this painful design pattern?" https://mail.python.org/pipermail/python-list/2018- February/731218.html class Foo: def __init__(self, bashful, doc, dopey, grumpy, happy, sleepy, sneezy): self.bashful = bashful # etc def spam(self, bashful=None, doc=None, dopey=None, grumpy=None, happy=None, sleepy=None, sneezy=None): if bashful is None: bashful = self.bashful if doc is None: doc = self.doc if dopey is None: dopey = self.dopey if grumpy is None: grumpy = self.grumpy if happy is None: happy = self.happy if sleepy is None: sleepy = self.sleepy if sneezy is None: sneezy = self.sneezy # now do the real work... def eggs(self, bashful=None, # etc... ): if bashful is None: bashful = self.bashful # and so on and with the possibility to update `locals` the `spam` can be rewritten with: def spam(self, bashful=None, doc=None, dopey=None, grumpy=None, happy=None, sleepy=None, sneezy=None): loc = locals() for key, val in loc.items(): if val is None: loc[key] = getattr(self, key) In fact, I do not have a strict opinion on this matter. And I'd rather be glad that Pнthon was less dynamic in some moments in favor of some optimizations. With kind regards, -gdg -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there are good DRY fix for this painful design pattern?
This validation can be also done with the use of annotations, while I find it super awful, I put this for one more example: from functools import wraps def validate(func): @wraps(func) def _wrap(self, *args, **kwargs): variables = func.__annotations__.keys() kwargs.update(zip(variables, args)) for var in variables - kwargs.keys(): kwargs[var] = getattr(self, var) return func(self, **kwargs) return _wrap class Foo: def __init__(self, bashful, doc, dopey, grumpy, happy, sleepy, sneezy): self.bashful = bashful self.doc = doc self.dopey = dopey self.grumpy = grumpy self.happy = happy self.sleepy = sleepy self.sneezy = sneezy @validate def spam(self, bashful:'Any'=None, doc:'Any'=None, dopey:'Any'=None, grumpy:'Any'=None, happy:'Any'=None, sleepy:'Any'=None, sneezy:'Any'=None): return bashful, doc, dopey, grumpy, happy, sleepy, sneezy a = Foo(1,2,3,4,5,6,7) a.spam(grumpy='Hello') With kind regards, -gdg -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] how to remove empty lines from webpage/file
Perhaps replace: lines=soup.get_text() file.write(lines) ...with something like: text = soup.get_text() lines = text.split('\n') for line in lines: if line.strip(): file.write('%s\n' % (line, )) (untested) On Tue, Feb 27, 2018 at 2:50 AM, wrote: > Dear all, > I try to get the numerical data from the following webpage: > http://www.astro.oma.be/GENERAL/INFO/nzon/zon_2018.html > > With the following code-fragment I was already able to get a partial result: > > #!/usr/bin/env python > #memo: install bs4 as follows: sudo easy_install bs4 > # -*- coding: utf-8 -*- > #3 lines below necessary to avoid encoding problem > import sys > reload(sys) > sys.setdefaultencoding('utf8') > import urllib2 > file = open("testfile.txt","w") > source = "http://www.astro.oma.be/GENERAL/INFO/nzon/zon_2018.html"; > page = urllib2.urlopen(source) > from bs4 import BeautifulSoup > soup = BeautifulSoup(page,'lxml') > lines=soup.get_text() > file.write(lines) > file.close() > > I tried to delete the empty lines but I am totally stuck at this moment, can > anyone help me further? > > thanks in advance > jens > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there are good DRY fix for this painful design pattern?
Of course you can do the same without annotations, but with the introduction of private attribute while your API it is under active development: from functools import wraps def validate(func): @wraps(func) def _wrap(self, *args, **kwargs): variables = self._vars # Here kwargs.update(zip(variables, args)) for var in variables - kwargs.keys(): kwargs[var] = getattr(self, var) return func(self, **kwargs) return _wrap class Foo: def __init__(self, bashful, doc, dopey, grumpy, happy, sleepy, sneezy): self.bashful = bashful self.doc = doc self.dopey = dopey self.grumpy = grumpy self.happy = happy self.sleepy = sleepy self.sneezy = sneezy self._vars = set(v for v in self.__dict__ if not v.startswith('_')) # should be deleted when finish @validate def spam(self, bashful=None, doc=None, dopey=None, grumpy=None, happy=None, sleepy=None, sneezy=None): return bashful, doc, dopey, grumpy, happy, sleepy, sneezy With kind regards, -gdg -- https://mail.python.org/mailman/listinfo/python-list
Re: matrix multiplication
On Tue, 27 Feb 2018 07:36:31 -0700, Ian Kelly wrote: > On Tue, Feb 27, 2018 at 4:08 AM, Peter Otten <__pete...@web.de> wrote: >> Seb wrote: >>> On Tue, 27 Feb 2018 12:25:30 +1300, >>> Gregory Ewing wrote: Seb wrote: > I was wondering is whether there's a faster way of multiplying > each row (1x3) of a matrix by another matrix (3x3), compared to > looping through the matrix row by row as shown in the code. Just multiply the two matrices together. If A is an nx3 matrix and B is a 3x3 matrix, then C = A @ B is an nx3 matrix where C[i] = A[i] @ B. (This is a property of matrix multiplication in general, nothing special about numpy.) >>> I think that's only true if B is the same for every row in A. In >>> the code I posted, B varies by row of A. >> Yeah, you would have to substitute the N 3x3 matrices with an Nx3x3 >> tensor, though I don't know if numpy provides an op such that >> Nx3 op Nx3x3 --> desired result >> or >> op(Nx3, Nx3x3) --> desired result > Nx1x3 @ Nx3x3 ought to do it, with the result being Nx1x3. That's right. I just tried this manipulation by replacing the last block of code in my example, from the line above `for` loop with: ------ # Alternative using `np.matmul` uvw_alt = uvw.reshape((uvw.shape[0], 1, uvw.shape[1])) bmats = np.asarray(map(randint_mat, maxint)) uvw_rots_alt = np.matmul(uvw_alt, bmats).squeeze() --- --- Interestingly, the time savings from IPython are not spectacular: %run -t -N100 loop_approach.py IPython CPU timings (estimated): Total runs performed: 100 Times : Total Per run User : 0.28 s, 0.00 s. System : 0.00 s, 0.00 s. Wall time: 0.28 s. %run -t -N100 matmul_approach.py IPython CPU timings (estimated): Total runs performed: 100 Times : Total Per run User : 0.17 s, 0.00 s. System : 0.00 s, 0.00 s. Wall time: 0.18 s. -- Seb -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On Mon, 26 Feb 2018 01:40:16 -0800, sotaro237 wrote: > Define 2 lists. The first one must contain the integer values 1, 2 and 3 > and the second one the string values a, b and c. Iterate through both > lists to create another list that contains all the combinations of the A > and B elements. The final list should look like one of the 2 lists: > 1. [1a, 1b, 1c, 2a, 2b, 2c, 3a, 3b, 3c] > 2. [a1, a2. a3, b1, b2, b3, c1, c2, c3] > BONUS: Make the final list contain all possible combinations : [1a, a1, > 1b, b1, 1c, c1, 2a, a2, 2b, b2, 2c, c2, 3a, a3, 3b, b3, 3c, c3] > > Help me ! Try staying awake in class where I am sure what you need would have been covered before the assignment was set failing that try reading any of the python tutorials available on the internet -- A career is great, but you can't run your fingers through its hair. -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On Mon, 26 Feb 2018 01:40:16 -0800 (PST), sotaro...@gmail.com wrote: >Define 2 lists. The first one must contain the integer values 1, 2 and 3 and >the second one the string values a, b and c. Iterate through both lists to >create another list that contains all the combinations of the A and B >elements. The final list should look like one of the 2 lists: >1. [1a, 1b, 1c, 2a, 2b, 2c, 3a, 3b, 3c] >2. [a1, a2. a3, b1, b2, b3, c1, c2, c3] >BONUS: Make the final list contain all possible combinations : [1a, a1, 1b, >b1, 1c, c1, 2a, a2, 2b, b2, 2c, c2, 3a, a3, 3b, b3, 3c, c3] > >Help me ! #a list of integers nums = [1, 2, 3] #a list of letters ltrs = ['a', 'b', 'c'] #a list to hold the result rslt = [] for i in nums: for j in ltrs: rslt.append(str(i) + j) #for bonus points uncomment the following line #rslt.append(j + str(i)) print(rslt) '''RESULT ['1a', '1b', '1c', '2a', '2b', '2c', '3a', '3b', '3c'] BONUS RESULT ['1a', 'a1', '1b', 'b1', '1c', 'c1', '2a', 'a2', '2b', 'b2', '2c', 'c2', '3a', 'a3', '3b', 'b3', '3c', 'c3'] ''' -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
Hi, On Tue, Feb 27, 2018 at 10:54 AM, Sir Real wrote: > On Mon, 26 Feb 2018 01:40:16 -0800 (PST), sotaro...@gmail.com wrote: > >>Define 2 lists. The first one must contain the integer values 1, 2 and 3 and >>the second one the string values a, b and c. Iterate through both lists to >>create another list that contains all the combinations of the A and B >>elements. The final list should look like one of the 2 lists: >>1. [1a, 1b, 1c, 2a, 2b, 2c, 3a, 3b, 3c] >>2. [a1, a2. a3, b1, b2, b3, c1, c2, c3] >>BONUS: Make the final list contain all possible combinations : [1a, a1, 1b, >>b1, 1c, c1, 2a, a2, 2b, b2, 2c, c2, 3a, a3, 3b, b3, 3c, c3] >> >>Help me ! > > #a list of integers > nums = [1, 2, 3] > > #a list of letters > ltrs = ['a', 'b', 'c'] > > #a list to hold the result > rslt = [] > > for i in nums: > for j in ltrs: > rslt.append(str(i) + j) > #for bonus points uncomment the following line > #rslt.append(j + str(i)) > > print(rslt) > > > '''RESULT > ['1a', '1b', '1c', > '2a', '2b', '2c', > '3a', '3b', '3c'] > >BONUS RESULT > ['1a', 'a1', '1b', 'b1', '1c', 'c1', > '2a', 'a2', '2b', 'b2', '2c', 'c2', > '3a', 'a3', '3b', 'b3', '3c', 'c3'] Congratulations! You have an "A" for solving the problem and "F" for helping the guy cheat. You should be expelled from the course. ;-) Thank you. > ''' > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: matrix multiplication
On Tue, Feb 27, 2018 at 9:02 AM, Seb wrote: > That's right. I just tried this manipulation by replacing the last > block of code in my example, from the line above `for` loop with: > > ------ > # Alternative using `np.matmul` > uvw_alt = uvw.reshape((uvw.shape[0], 1, uvw.shape[1])) > bmats = np.asarray(map(randint_mat, maxint)) > uvw_rots_alt = np.matmul(uvw_alt, bmats).squeeze() > --- --- > > Interestingly, the time savings from IPython are not spectacular: The second line is still calling the Python function randint_mat 1000 times. That should be possible to do without the Python function using broadcasting, if https://github.com/numpy/numpy/issues/6745 were fixed. Otherwise, I don't know of a way to vectorize that part efficiently. -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On Tue, Feb 27, 2018 at 10:16 AM, Igor Korot wrote: > Congratulations! > You have an "A" for solving the problem and "F" for helping the guy cheat. > You should be expelled from the course. In my experience, this is what happens pretty much every time. Somebody posts a homework question asking for the answer, a few people say something to the effect of, "This looks like homework. What have you tried so far?" Then some other bozo comes along who just likes solving easy problems and hands up the answer on a silver platter. Cheaters are gonna cheat. In the unlikely event they don't get the answer here, they'll probably just manage to convince somebody to do the work for them somewhere else. Honestly, I don't know if it's even worth the bother to engage. -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On Tue, Feb 27, 2018 at 12:56 PM, Ian Kelly wrote: > On Tue, Feb 27, 2018 at 10:16 AM, Igor Korot wrote: >> Congratulations! >> You have an "A" for solving the problem and "F" for helping the guy cheat. >> You should be expelled from the course. > > In my experience, this is what happens pretty much every time. > Somebody posts a homework question asking for the answer, a few people > say something to the effect of, "This looks like homework. What have > you tried so far?" Then some other bozo comes along who just likes > solving easy problems and hands up the answer on a silver platter. > > Cheaters are gonna cheat. In the unlikely event they don't get the > answer here, they'll probably just manage to convince somebody to do > the work for them somewhere else. Honestly, I don't know if it's even > worth the bother to engage. When I was in college back in the 70's they made people in majors like printing or chemistry, for example, take 1 programming course. They were always clueless and I often wrote programs for them - my typical fee was a case of beer. -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On 2018-02-26, sotaro...@gmail.com wrote: > > Help me ! a=[1,2,3,] b=["a","b","c"] x=[] z=[] bonus=[] for digits in a: for letters in b: x.append(str(digits) + letters) bonus.append(letters + str(digits)) for letter in b: for number in a: z.append(letter + str(number)) bonus.append(str(number) + letter) print ("1:",x) print ("2:",z) print("Bonus:",bonus) -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On 2018-02-27, Ian Kelly wrote: > On Tue, Feb 27, 2018 at 10:16 AM, Igor Korot wrote: >> Congratulations! >> You have an "A" for solving the problem and "F" for helping the guy cheat. >> You should be expelled from the course. > > In my experience, this is what happens pretty much every time. > Somebody posts a homework question asking for the answer, a few people > say something to the effect of, "This looks like homework. What have > you tried so far?" Then some other bozo comes along who just likes > solving easy problems and hands up the answer on a silver platter. If the student is actively trying to avoid learning something, there's nothing you can do to help them. They're just wasting their own time and money. The fun part is giving them a solution that's so obscure and "clever" that it technically meets the stated requirement but is so far from what the instructor wanted that they don't get credit for it (and there's no way the student will be able explain how it works to the instructor). -- Grant Edwards grant.b.edwardsYow! We just joined the at civil hair patrol! gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there are good DRY fix for this painful design pattern?
On 02/26/2018 03:41 PM, Steven D'Aprano wrote: I have a class with a large number of parameters (about ten) assigned in `__init__`. The class then has a number of methods which accept *optional* arguments with the same names as the constructor/initialiser parameters. If those arguments are None, the defaults are taken from the instance attributes. An example might be something like this: class Foo: def __init__(self, bashful, doc, dopey, grumpy, happy, sleepy, sneezy): self.bashful = bashful # etc def spam(self, bashful=None, doc=None, dopey=None, grumpy=None, happy=None, sleepy=None, sneezy=None): if bashful is None: bashful = self.bashful if doc is None: doc = self.doc if dopey is None: dopey = self.dopey if grumpy is None: grumpy = self.grumpy if happy is None: happy = self.happy if sleepy is None: sleepy = self.sleepy if sneezy is None: sneezy = self.sneezy # now do the real work... def eggs(self, bashful=None, # etc... ): if bashful is None: bashful = self.bashful # and so on There's a lot of tedious boilerplate repetition in this, and to add insult to injury the class is still under active development with an unstable API, so every time I change one of the parameters, or add a new one, I have to change it in over a dozen places. Is there a good fix for this to reduce the amount of boilerplate? Thanks, def snatchit(func): def snatched(self, *args, **kwargs): frame = inspect.signature(func) for name in frame.parameters: if name != 'self': kwargs.setdefault(name, getattr(self, name)) return func(self, *args, **kwargs) snatched.__name__ = func.__name__ snatched.__doc__ = func.__doc__ return snatched class Foo(object): def __init__(self, sleepy, groggy): self.sleepy = sleepy self.groggy = groggy @snatchit def spam(self, sleepy=None): print("I am spam. sleepy=", sleepy) @snatchit def ham(self, sleepy=None, groggy=None): print("I am ham. sleepy=", sleepy, " and groggy=", groggy) >>> f = Foo(1, 19) >>> f.spam() I am spam. sleepy= 1 >>> f.spam(sleepy=8) I am spam. sleepy= 8 >>> f.ham(sleepy=8) I am ham. sleepy= 8 and groggy= 19 >>> f.ham(sleepy=17, groggy=888) I am ham. sleepy= 17 and groggy= 888 -- ~ Jugurtha Hadjar, -- https://mail.python.org/mailman/listinfo/python-list
Cheetah 3.0.1
Hello! I'm pleased to announce version 3.0.1, the first bugfix release of branch 3.0 of CheetahTemplate3. What's new in CheetahTemplate3 == Bug fixes: - Fix a minor bug in Compiler. What is CheetahTemplate3 Cheetah3 is a free and open source template engine. It's a fork of the original CheetahTemplate library. Python 2.7 or 3.3+ is required. Where is CheetahTemplate3 = Site: http://cheetahtemplate.org/ Development: https://github.com/CheetahTemplate3 Download: https://pypi.python.org/pypi/Cheetah3/3.0.1 News and changes: http://cheetahtemplate.org/news.html StackOverflow: https://stackoverflow.com/questions/tagged/cheetah Example === Below is a simple example of some Cheetah code, as you can see it's practically Python. You can import, inherit and define methods just like in a regular Python module, since that's what your Cheetah templates are compiled to :) :: #from Cheetah.Template import Template #extends Template #set $people = [{'name' : 'Tom', 'mood' : 'Happy'}, {'name' : 'Dick', 'mood' : 'Sad'}, {'name' : 'Harry', 'mood' : 'Hairy' How are you feeling? #for $person in $people $person['name'] is $person['mood'] #end for Oleg. -- Oleg Broytmanhttp://phdru.name/p...@phdru.name Programmers don't die, they just GOSUB without RETURN. -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
Hello, it's a duplicate: https://python-forum.io/Thread-Working-with-lists-homework-2 I have seen this more than one time. We don't like it. You keep people busy with one question at different places. You need two lists and one empty list. One outer loop iterating over the first list and one inner loop iterating over the second list. In the inner loop you concatenate the two elements from the outer-loop and inner-loop. Then you append them to the empty list. This text is 10 times longer as the source code... Complicated solution: from string import ascii_lowercase as letter list1 = [str(i) + c for i in range(1,4) for c in letter[:3]] list2 = [c[::-1] for c in list1] But this won't help you. Before you understand the code above, you have to understand for-loops and nested for-loops. Then you can proceed with list comprehensions. But I don't see that your intention is to learn and understand Python. You just want to finish your homework, which bother you. Greetings Andre -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On Tue, Feb 27, 2018 at 2:50 PM, Andre Müller wrote: > Hello, > > it's a duplicate: > https://python-forum.io/Thread-Working-with-lists-homework-2 > > I have seen this more than one time. We don't like it. You keep people busy > with one question at different places. You assume that it was posted by the same person in both places, and not by two different students in the same class. -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there are good DRY fix for this painful design pattern?
Something like the following might do the trick. As an added benefit, it's easy to set all the defaults automatically in __init__ as well without hand-adding "self.dopey = dopey". On the down side, in the non-__init__ functions, you have to use kwargs["dopey"] and the like. It also involves tacking an "__init_args" onto your object so that the decorator knows what was passed to the __init__ function. -tkc from functools import wraps from inspect import getargspec def template(original_init_fn): args, varargs, keywords, defaults = getargspec(original_init_fn) assert varargs is keywords is None arg_dict = dict(zip(args[-len(defaults):], defaults)) @wraps(original_init_fn) def new_init_fn(self, *args, **kwargs): self.__init_args = arg_dict.copy() self.__init_args.update(kwargs) # if you don't want to automatically set attributes # remove these next two lines for k, v in self.__init_args.items(): setattr(self, k, v) return original_init_fn(self, *args, **kwargs) def templatify(fn): @wraps(fn) def new_templated_fn(self, *args, **kwargs): for k, v in self.__init_args.items(): if k not in kwargs: kwargs[k] = v return fn(self, *args, **kwargs) return new_templated_fn new_init_fn.templatify = templatify return new_init_fn class Foo: @template def __init__(self, bashful=None, dopey=None, doc="On definition", ): pass # look, ma, no manual assignment! @__init__.templatify def myfunc(self, **kwargs): print(kwargs) f1 = Foo() f2 = Foo(bashful="on init", dopey="on init") for fn in (f1, f2): fn.myfunc() fn.myfunc(bashful="on myfunc") On 2018-02-26 14:41, Steven D'Aprano wrote: > I have a class with a large number of parameters (about ten) > assigned in `__init__`. The class then has a number of methods > which accept *optional* arguments with the same names as the > constructor/initialiser parameters. If those arguments are None, > the defaults are taken from the instance attributes. > > An example might be something like this: > > > class Foo: > def __init__(self, bashful, doc, dopey, grumpy, >happy, sleepy, sneezy): > self.bashful = bashful # etc > > def spam(self, bashful=None, doc=None, dopey=None, >grumpy=None, happy=None, sleepy=None, >sneezy=None): > if bashful is None: > bashful = self.bashful > if doc is None: > doc = self.doc > if dopey is None: > dopey = self.dopey > if grumpy is None: > grumpy = self.grumpy > if happy is None: > happy = self.happy > if sleepy is None: > sleepy = self.sleepy > if sneezy is None: > sneezy = self.sneezy > # now do the real work... > > def eggs(self, bashful=None, # etc... >): > if bashful is None: > bashful = self.bashful > # and so on > > > There's a lot of tedious boilerplate repetition in this, and to add > insult to injury the class is still under active development with > an unstable API, so every time I change one of the parameters, or > add a new one, I have to change it in over a dozen places. > > Is there a good fix for this to reduce the amount of boilerplate? > > > Thanks, > > > > -- > Steve > > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On Tue, 27 Feb 2018 10:56:18 -0700, Ian Kelly wrote: > Cheaters are gonna cheat. In the unlikely event they don't get the > answer here, they'll probably just manage to convince somebody to do the > work for them somewhere else. Honestly, I don't know if it's even worth > the bother to engage. Its worth the bother for the 5% who aren't *intentionally* cheating, but don't realise it, and just need a reminder that they need to do the work themselves. (Or at least give co-credit to those they got help from.) Or those who aren't actually doing homework, but are engaged in self- learning. There has to be some penalty for cheating, even if its only the minuscule amount of social disapproval that comes from random strangers on the internet telling you off, or we'll be even more overloaded by cheaters than we already are. -- Steve -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On 2018-02-26 07:17, Stefan Ram wrote: Percival John Hackworth quoted: Define 2 lists. The first one must contain the integer values 1, 2 and 3 a =[ 1, 2, 3 ] and the second one the string values a, b and c. b =[ 'a', 'b', 'c'] Iterate through both lists to create another list that contains all the combinations of the A and B for i in a: result = [ '1a', '1b', '1c', '2a', '2b', '2c', '3a', '3b', '3c' ] for j in b: result = [ '1a', '1b', '1c', '2a', '2b', '2c', '3a', '3b', '3c' ] That's absolutely wonderful! However, I'd like to suggest one change, which would allow greater CPU utilization: for i in a: for j in b: # Sorry, I'm not PEP-8 compliant result = [ '1a', '1b', '1c', '2a', '2b', '2c', '3a', '3b', '3c' ] -- Michael F. Stemper Always remember that you are unique. Just like everyone else. -- https://mail.python.org/mailman/listinfo/python-list
psutil
Trying to install psutil (with pip install psutil) on Red Hat EL 7. It's failing with: Python.h: No such file or directory Typically that means the python devel libs are not installed, but they are: [root@liszt ~]# yum install python-devel Package python-devel-2.7.5-58.el7.x86_64 already installed and latest version Nothing to do Anyone have any idea what to try next? -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil
On Wed, Feb 28, 2018 at 11:29 AM, Larry Martell wrote: > Trying to install psutil (with pip install psutil) on Red Hat EL 7. > It's failing with: > > Python.h: No such file or directory > > Typically that means the python devel libs are not installed, but they are: > > [root@liszt ~]# yum install python-devel > Package python-devel-2.7.5-58.el7.x86_64 already installed and latest version > Nothing to do > > Anyone have any idea what to try next? Are you trying to install that into Python 3 or Python 2? Might need to grab python3-devel. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil
On Tue, Feb 27, 2018 at 07:29:50PM -0500, Larry Martell wrote: > Trying to install psutil (with pip install psutil) on Red Hat EL 7. > It's failing with: > > Python.h: No such file or directory Two questions come to my mind: - Does it work if you try to install some other package? - Is `pip` by any change trying to install a Python 3 package, but you only have the libraries for Python 2 installed? Cheers, -- José María Mateos https://rinzewind.org/blog-es || https://rinzewind.org/blog-en -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil
On Tue, Feb 27, 2018 at 7:37 PM, Chris Angelico wrote: > On Wed, Feb 28, 2018 at 11:29 AM, Larry Martell > wrote: >> Trying to install psutil (with pip install psutil) on Red Hat EL 7. >> It's failing with: >> >> Python.h: No such file or directory >> >> Typically that means the python devel libs are not installed, but they are: >> >> [root@liszt ~]# yum install python-devel >> Package python-devel-2.7.5-58.el7.x86_64 already installed and latest version >> Nothing to do >> >> Anyone have any idea what to try next? > > Are you trying to install that into Python 3 or Python 2? Might need > to grab python3-devel. Python 2 -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil
On Tue, Feb 27, 2018 at 7:36 PM, José María Mateos wrote: > On Tue, Feb 27, 2018 at 07:29:50PM -0500, Larry Martell wrote: >> Trying to install psutil (with pip install psutil) on Red Hat EL 7. >> It's failing with: >> >> Python.h: No such file or directory > > Two questions come to my mind: > > - Does it work if you try to install some other package? Yes, I have installed other packages (e.g. numpy) > - Is `pip` by any change trying to install a Python 3 package, but you > only have the libraries for Python 2 installed? No, here is the gcc command line that failed: gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSUTIL_POSIX=1 -DPSUTIL_VERSION=543 -DPSUTIL_LINUX=1 -I/opt/rh/python27/root/usr/include/python2.7 -c psutil/_psutil_common.c -o build/temp.linux-x86_64-2.7/psutil/_psutil_common.o -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil
On Wed, 28 Feb 2018, 00:49 Larry Martell, wrote: > On Tue, Feb 27, 2018 at 7:36 PM, José María Mateos > wrote: > > On Tue, Feb 27, 2018 at 07:29:50PM -0500, Larry Martell wrote: > >> Trying to install psutil (with pip install psutil) on Red Hat EL 7. > >> It's failing with: > >> > >> Python.h: No such file or directory > > > > Two questions come to my mind: > > > > - Does it work if you try to install some other package? > > Yes, I have installed other packages (e.g. numpy) > Did that install definitely involve a build from source? > - Is `pip` by any change trying to install a Python 3 package, but you > > only have the libraries for Python 2 installed? > > No, here is the gcc command line that failed: > > gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall > -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong > --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic > -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall > -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong > --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic > -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DPSUTIL_POSIX=1 > -DPSUTIL_VERSION=543 -DPSUTIL_LINUX=1 > -I/opt/rh/python27/root/usr/include/python2.7 -c > psutil/_psutil_common.c -o > build/temp.linux-x86_64-2.7/psutil/_psutil_common.o > Based on the include path you have here this looks like you have a non standard python package for EL7 (in addition to the system one, or you'd be having a worse day), which is probably the root of your problem. Are you using python packages from scl perhaps? If so you probably need to install python27-python-devel (found at http://mirror.centos.org/centos/7/sclo/x86_64/rh/python27/) > -- -- Matt Wheeler http://funkyh.at -- https://mail.python.org/mailman/listinfo/python-list
Re: psutil
On Tue, 27 Feb 2018 19:29:50 -0500, Larry Martell wrote: > Trying to install psutil (with pip install psutil) on Red Hat EL 7. > It's failing with: > > Python.h: No such file or directory > > Typically that means the python devel libs are not installed, but they are: > > [root@liszt ~]# yum install python-devel > Package python-devel-2.7.5-58.el7.x86_64 already installed and latest version > Nothing to do > > Anyone have any idea what to try next? I am not familiar with Red Hat and the RPM package system but on my Debian based system it is in the repository as an installable package. The package names are python-psutil and python3-psutil. -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On Tuesday, February 27, 2018 at 10:56:52 AM UTC-8, Grant Edwards wrote: > If the student is actively trying to avoid learning something, there's > nothing you can do to help them. They're just wasting their own time > and money. This is part of the reason why interviews for software developer jobs have gotten so crazy. We have to weed out the impostors after they graduate. > The fun part is giving them a solution that's so obscure and "clever" > that it technically meets the stated requirement but is so far from > what the instructor wanted that they don't get credit for it (and > there's no way the student will be able explain how it works to the > instructor). It doesn't even need to be an obscure solution, it just needs to make use of things you don't expect a novice programmer to use, like lambdas. A recent example: https://groups.google.com/forum/#!original/comp.lang.python/0gYm2g3BA2A/s9xbBG1GAwAJ -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On 02/27/2018 06:54 PM, Grant Edwards wrote: > The fun part is giving them a solution that's so obscure and "clever" > that it technically meets the stated requirement but is so far from > what the instructor wanted that they don't get credit for it (and > there's no way the student will be able explain how it works to the > instructor). Isn't that the point of doing their homework. Amber -- https://mail.python.org/mailman/listinfo/python-list
Re: help me ?
On Tuesday, February 27, 2018 at 12:56:52 PM UTC-6, Grant Edwards wrote: [...] > The fun part is giving them a solution that's so obscure and "clever" > that it technically meets the stated requirement but is so far from > what the instructor wanted that they don't get credit for it (and > there's no way the student will be able explain how it works to the > instructor). Even for the unobscure and unclever solutions (like the one Sir Real offered), i doubt the cheaters could explain them to any level of expertise that would be acceptable to a wise instructor. @OP: If after submitting your copy/paste solution the instructor calls you over for a chat, it's okay to be nervous, because typically this means you're about to be outed and made a complete fool of in front of the whole class. Yep, teachers are sadistic like that. Have a nice day! -- https://mail.python.org/mailman/listinfo/python-list
[RELEASE] Python 3.7.0b2 is now available for testing
On behalf of the Python development community and the Python 3.7 release team, I'm happy to announce the availability of Python 3.7.0b2. b2 is the second of four planned beta releases of Python 3.7, the next major release of Python, and marks the end of the feature development phase for 3.7. You can find Python 3.7.0b2 here: https://www.python.org/downloads/release/python-370b2/ Among the new major new features in Python 3.7 are: * PEP 538, Coercing the legacy C locale to a UTF-8 based locale * PEP 539, A New C-API for Thread-Local Storage in CPython * PEP 540, UTF-8 mode * PEP 552, Deterministic pyc * PEP 553, Built-in breakpoint() * PEP 557, Data Classes * PEP 560, Core support for typing module and generic types * PEP 562, Module __getattr__ and __dir__ * PEP 563, Postponed Evaluation of Annotations * PEP 564, Time functions with nanosecond resolution * PEP 565, Show DeprecationWarning in __main__ * PEP 567, Context Variables Please see "What’s New In Python 3.7" for more information. Additional documentation for these features and for other changes will be provided during the beta phase. https://docs.python.org/3.7/whatsnew/3.7.html Beta releases are intended to give you the opportunity to test new features and bug fixes and to prepare their projects to support the new feature release. We strongly encourage you to test your projects with 3.7 during the beta phase and report issues found to https://bugs.python.org as soon as possible. While the release is feature complete entering the beta phase, it is possible that features may be modified or, in rare cases, deleted up until the start of the release candidate phase (2018-05-21). Our goal is have no ABI changes after beta 3 and no code changes after rc1. To achieve that, it will be extremely important to get as much exposure for 3.7 as possible during the beta phase. Attention macOS users: as of b1, there is a new installer variant for macOS 10.9+ that includes a built-in version of Tcl/Tk 8.6. This variant is expected to become the default version when 3.7.0 releases. Check it out! We welcome your feedback. Please keep in mind that this is a preview release and its use is not recommended for production environments. The next planned release of Python 3.7 will be 3.7.0b3, currently scheduled for 2018-03-26. More information about the release schedule can be found here: https://www.python.org/dev/peps/pep-0537/ -- Ned Deily n...@python.org -- [] -- https://mail.python.org/mailman/listinfo/python-list
Re: Questions about `locals` builtin
Kirill Balunov writes: > 2018-02-27 2:57 GMT+03:00 Terry Reedy : > >> The point of point 3 is that terminology and details would likely be >> different if Python were freshly designed more or less as it is today, and >> some things only make more or less sense in historical context. Learn what >> you need to know to write code that works. >> > > Thank you, I'm fairly familiar with the scope and namespace concepts in > Python 3, and they are also very well described in the "Execution model" > https://docs.python.org/3/reference/executionmodel.html#execution-model, > for this special thanks to the person who wrote it ;-) > > I started using Python with СPython 3.5 and I'm not familiar with the > Python 2 features. But since Python 2 got into our discussion, I still have > a question: > > a. Is this restriction for locals desirable in the implementation of > CPython in Python 3? > b. Or is it the result of temporary fixes for Python 2? I think it is an implementation artefact: for efficiency reasons, local variables in a function are handled differently than "local" variables elsewhere. A side effect of the concrete implementation causes that truely local variables cannot be changed by updating the result of "locals()". The "locals" documentation tries to document the restrictions. -- https://mail.python.org/mailman/listinfo/python-list
Re: Questions about `locals` builtin
Ned Batchelder writes: > On 2/27/18 3:52 AM, Kirill Balunov wrote: >> a. Is this restriction for locals desirable in the implementation of >> CPython in Python 3? >> b. Or is it the result of temporary fixes for Python 2? > > My understanding is that the behavior of locals() is determined mostly > by what is convenient for the implementors, so that they can keep > regular code running as quickly as possible. The answer to the > question, "why can't we make locals() work more like I expect?" is, > "because that would make things slower." >> >> Personally, I find the convenient functionality to update the local symbol >> table inside a function, similar to `globals`. > > Can you show us an example of why you would want to update locals > through locals()? There might be more natural ways to solve your > problem. I am still working with Python 2 (Python 3 may behave differently). There, during debugging, I would sometimes like to change the value of variables (I know that the variable has got a wrong value and would like to fix it to continue senseful debugging without a restart). This works for variables not yet known inside the function but does not work for true local variables. I assume that this is one effect of the "locals()" restriction. -- https://mail.python.org/mailman/listinfo/python-list
Re: Questions about `locals` builtin
On Tue, Feb 27, 2018 at 5:55 AM, Kirill Balunov wrote: > 2. The documentation has a note that "The contents of this dictionary > should not be modified". Which implies that it is a read only mapping. So > the question why it is `dict` instead of `types.MappingProxyType`? A dict is smaller and faster. In some Python implementations, locals() returns an actual dictionary of the actual locals; to construct a proxy would be a waste of effort, and to have the internal locals use a proxy is also a complete waste. Python tends not to enforce rules that won't actually cause a crash, so it's simpler and more efficient to give you a real dictionary and say "changing this might not do what you think it does". > 3. There is one more moment: local variables had been determined when > function was compiled. But `locals` returns _some_ current runtime copy. I > find this also confusing: > > def func1(): > > loc = locals() > > b = 12 > > return loc > > def func2(): > > b = 12 > > loc = locals() > > return loc > > func1() > { } func2() > {'b': 12} The values of those locals is calculated at run-time, but the set of names is not. If a name has not been bound, it's still flagged as "local", but it doesn't appear in locals(), because there's no corresponding value. So if "b" is not in locals(), attempting to access the variable named b will raise UnboundLocalError. Dictionaries don't have a way to say "this thing exists but has no value"; so this says "this thing doesn't exist", which is close enough. If you really want a list of ALL the local names in a function, you can look at its __code__ object, which has a tuple of variable names: print(func1.__code__.co_varnames) That information is static to the function, as it is indeed determined when the function is compiled. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: In Python2, does it need to wrap imp.find/load_module with imp_acquire/release_lock?
Xiang Zhang writes: > Just like the title. It seems to me it is needed from the source code but > codes in stdlib all doesn't do that. The "import" machinery uses locks of its own (to protect "sys.modules"). I assume that "load_module" will use those locks internally. "find" might be considered to usually see quite a static situation; should your file system be modified by concurrent threads, it might be necessary that you use your own locks to protect against this. -- https://mail.python.org/mailman/listinfo/python-list
Re: Questions about `locals` builtin
On Wed, Feb 28, 2018 at 5:54 PM, dieter wrote: > Ned Batchelder writes: >> On 2/27/18 3:52 AM, Kirill Balunov wrote: >>> a. Is this restriction for locals desirable in the implementation of >>> CPython in Python 3? >>> b. Or is it the result of temporary fixes for Python 2? >> >> My understanding is that the behavior of locals() is determined mostly >> by what is convenient for the implementors, so that they can keep >> regular code running as quickly as possible. The answer to the >> question, "why can't we make locals() work more like I expect?" is, >> "because that would make things slower." >>> >>> Personally, I find the convenient functionality to update the local symbol >>> table inside a function, similar to `globals`. >> >> Can you show us an example of why you would want to update locals >> through locals()? There might be more natural ways to solve your >> problem. > > I am still working with Python 2 (Python 3 may behave differently). > There, during debugging, I would sometimes like to change the value > of variables (I know that the variable has got a wrong value > and would like to fix it to continue senseful debugging without a restart). > This works for variables not yet known inside the function but does > not work for true local variables. > I assume that this is one effect of the "locals()" restriction. > That seems like a hairy thing to do, honestly. But if you know that there's only a handful of variables that you'd actually want to do that to, you can simply put those into an object of some form, and then mutate that object. That's guaranteed to work in any Python. Personally, I'd be inclined to break things up into separate functions, and then if you want to change state and continue, it would be by interactively calling one of those functions with slightly different parameters. ChrisA -- https://mail.python.org/mailman/listinfo/python-list