Re: on writing a while loop for rolling two dice
On 11/09/2021 10:09, dn via Python-list wrote: The stated requirement is: "I'd like to get the number of times I tried". Given such: why bother with returning any of the pairs of values? Indeed, if that's the requirement, then you can do even better, noting that the probability of getting a matched pair is 1/6 (6 matches out of 6*6 possibilities). So the answer to the problem is exactly the same as rolling a single die until you get any particular number (e.g., 1). This is somewhat easier to simulate than the two-dice problem (and the number of throws until a match is also a known, analytic distribution that you could sample from, but this is probably easier). -- https://mail.python.org/mailman/listinfo/python-list
Re: better handling of "pinned" modules?
On 08/01/2021 18:21, Chris Angelico wrote: On Sat, Jan 9, 2021 at 5:18 AM Andrew Jaffe wrote: Hi, I don't know if this makes more sense here or on "python-ideas" (or elsewhere?) but I'll try this first: I am starting to encounter more and more instances of packages requiring older, pinned, versions of modules, and this is occasionally actually starting to cause conflicts. The first thing to do is to see if those packages ACTUALLY need older versions of those dependencies. There's a good chance they don't. To avoid creating this sort of problem, don't depend on a highly specific version of things; just depend on a minimum version, and if there's a problem (ONLY if there's a problem), a maximum version. Well, sure. But there's still the "aesthetic" problem that `pip[3] check` reports a problem in such a case, and the real (albeit correctable) problem that `pip[3] install --upgrade` will occasionally automatically downgrade required packages. So perhaps my original query about whether there could be a way to actually solve this problem is still potentially interesting/useful. AndrewJ ChrisA -- https://mail.python.org/mailman/listinfo/python-list
better handling of "pinned" modules?
Hi, I don't know if this makes more sense here or on "python-ideas" (or elsewhere?) but I'll try this first: I am starting to encounter more and more instances of packages requiring older, pinned, versions of modules, and this is occasionally actually starting to cause conflicts. It seems that the "official" way to handle this is through virtual environments, but this is not ideal for my workflow, which usually involves a lot of exploratory analysis -- I don't know which packages I'll need before I start (and, in any event, there's no guarantee I won't need conflicting packages). Are there any good workarounds, or proposals for actual solutions so that multiple versions of a package can be installed? I understand that this is not trivial. Where would the "pinning" happen? It could be in the source code, so that it would somehow happen in the `import` statement (e.g., by explicitly renaming the package in question to include a version number, or less likely, by a new `import` syntax)? Or it could happen in the structure of the way modules are stored so that, for example, any imports that happen within a specific package egg would be directed to a specific version stored within the egg or in some other known location? Is this an issue worth tackling? Or do we just have to rely on package developers to keep up with their dependencies? Yours, Andrew -- https://mail.python.org/mailman/listinfo/python-list
Re: [Beginner] Spliting input
Hi, On 25/06/2020 12:50, Bischoop wrote: I try to split input numbers, for example: 12 so I cant add them, I tried separated split(' ') but it's not working. Any ideas how to do this? * numb1,numb2=input("enter 1st and 2nd no ").split() Avg=(int(numb1) + int(numb2)) / 2 print(Avg) So, this is an opportunity to do (and learn) some debugging! One problem is that the first line does two things at once -- it reads the input and tries to split it into numb1 and numb2. Similarly for the "average" line. You could instead try: input_string = input("enter 1st and 2nd no ") print(input_string) numb1_as_string, numb2_as_string = input_string.split() print(numb1_as_string, numb2_as_string) numb1 = int(numb1) numb2 = int(numb2) print(numb1, numb2) avg = (numb1 + numb2) / 2 print(avg) Now, when if fails on input_string.split() (which I think is what happens) you can then be sure that you know what the input is. Let's say it's "17,3". Since you know that, you can experiment: "17, 3".split() -> "17,", "3" (note extra space) "17 3".split() -> "17" "3" (aha, getting somewhere) ... and same results for both of these with split(' '), which should indicate that it's splitting on the space, and you didn't supply a space. So now maybe try "17,3".split(',') -> "17", "3" (bingo!) Bonus question: what happens with "17, 3".split(',')? * -- Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Behaviour of os.path.join
Dear all, \On 26/05/2020 15:56, BlindAnagram wrote: I came across an issue that I am wondering whether I should report as an issue. If I have a directory, say: base='C:\\Documents' and I use os.path.join() as follows: join(base, '..\\..\\', 'build', '') I obtain as expected from the documentation: 'C:\\Documents\\..\\..\\build\\' But if I try to make the directory myself (as I tried first): join(base, '..\\..\\', 'build', '\\') I obtain: 'C:\\' The documentation says that an absolute path in the parameter list for join will discard all previous parameters but '\\' is not an absoute path! Moreover, if I use join(base, '..\\..\\', 'build', os.sep) I get the same result. This seems to me to be a bug that I should report but to avoid wasting developer time I wanted to hear what others feel about this. Maybe I am being obtuse (and apologies for not quoting any of the subsequent voluminous messages in this thread), but I think perhaps there is confusion at a somewhat more basic level. It seems that there is never (rarely?) any reason to explicitly pass a string which already contains an explicit separator to `os.path.join` -- the whole point of the function is to be os-agnostic. If you already know what the separator is, and you also don't like the behaviour of restarting the path if any of the items are (by the function's definition) absolute, then is there any reason to prefer `os.path.join` to `string.join`? A -- https://mail.python.org/mailman/listinfo/python-list
[issue30392] default webbrowser fails on macOS Sierra 10.12.5
Andrew Jaffe added the comment: 10.12.6 is out and the bug appears to be fixed... -- resolution: out of date -> third party status: pending -> closed ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser fails on macOS Sierra 10.12.5
Andrew Jaffe added the comment: I'll also note that my bug report (radar) has been marked as "DUPLICATE OF 31898264 (OPEN)". So Apple is aware of the bug, and possibly not completely ignoring it. However, the opacity of the system is such that there is no way to get any further information (even though we know the bug number). -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser fails on macOS Sierra 10.12.5
Andrew Jaffe added the comment: Yes, it's a weird bug. see http://www.andrewjaffe.net/blog/2017/05/python-bug-hunt.html for more of the story so far. I have already filed a radar, and I hope this will get fixed at Apple, but it's a bug we need to live with for a while one way or the other. (For what it's worth, among other things the current implementation relies on the officially deprecated os.popen() which has nothing to do with this problem but I guess this could be taken as an opportunity to change...) -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser fails on macOS Sierra 10.12.5
Andrew Jaffe added the comment: This seems to be a bug in the `osascript` application in the latest macOS 10.12.5: $ osascript < open location "http://python.org; > EOF 0:33: execution error: "http://python.org; doesn’t understand the “open location” message. (-1708) -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser fails on macOS Sierra 10.12.5
Andrew Jaffe added the comment: The same behaviour also happens under Apple's system Python 2.7.10. Perhaps this implies a macOS bug or deliberate behaviour change? (I couldn't find anything obviously appropriate in the list of security fixes for 10.12.5.) -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser fails on macOS Sierra 10.12.5
Changes by Andrew Jaffe <a.h.ja...@gmail.com>: -- title: default webbrowser not used on macOS Sierra 10.12.5 -> default webbrowser fails on macOS Sierra 10.12.5 ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser not used on macOS Sierra 10.12.5
Andrew Jaffe added the comment: A few more details: - I believe this worked correctly under previous macOS versions (but I don't currently have access to any such machines). - This behaviour is identical under 3.6.1 and 2.7.13 (untested elsewhere) - Behaviour first noticed under Jupyter notebook -- see https://github.com/jupyter/notebook/issues/2438 -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser not used on macOS Sierra 10.12.5
New submission from Andrew Jaffe: On the newly-released macOS Sierra 10.12.5, the default web browser which is meant to returned by webbrowser.get() gives an error. Specifically: >>> import webbrowser >>> br = webbrowser.get() >>> br.open("http://python.org;) 0:33: execution error: "http://python.org; doesn’t understand the “open location” message. (-1708) False >>> ### but this works >>> br = webbrowser.get("safari") >>> br.open("http://python.org;) True -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser not used on macOS Sierra 10.12.5
Changes by Andrew Jaffe <a.h.ja...@gmail.com>: -- title: default webbrowser macOS Sierra 10.12.5 -> default webbrowser not used on macOS Sierra 10.12.5 ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue30392] default webbrowser macOS Sierra 10.12.5
Changes by Andrew Jaffe <a.h.ja...@gmail.com>: -- components: Library (Lib), macOS nosy: Andrew.Jaffe, ned.deily, ronaldoussoren priority: normal severity: normal status: open title: default webbrowser macOS Sierra 10.12.5 type: behavior versions: Python 2.7, Python 3.6 ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30392> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Pyhon 2.x or 3.x, which is faster?
On 06/03/2016 14:41, Steven D'Aprano wrote: On Sun, 6 Mar 2016 10:34 pm, Tony van der Hoff wrote: Hi, I've been experimenting with a short test program under python 2.7 and python 3.4.2. It's a simple read from file, and locate a word therein. I get the (subjective) impression that python2 is slightly faster than python3. Is that correct? Is there any documentation to support this? I believe that, overall, Python 3 is still slightly slower than Python 2, but it's a near thing. Have a look at the latest performance benchmarks: https://speed.python.org/comparison/ Eyeballing the graph, I estimate that the latest 3.x version is probably about 10% slower overall, although different benchmarks show different speeds. Dumb question, and this probably isn't the place for it, but the three Pythons being tested are: 64-bit CPython on Li... latest in branch '2.7' 64-bit CPython on Li... latest in branch '3.5' 64-bit CPython on Li... latest I understand that the first two are the released 2.7 and 3.5 versions; is the third the most recent 3.x build? Andrew -- https://mail.python.org/mailman/listinfo/python-list
OS X Python: can I explicitly set MACOSX_DEPLOYMENT_TARGET for extensions?
I use the python.org framework build of Python under recent versions of OS X (i.e., 10.11 El Capitan). I need to build some extensions that rely on recent versions of compilers (e.g., C++-11 features). However the python.org python is built to work on older systems as well, for backward compatibility. Hence, it has the environment variable MACOSX_DEPLOYMENT_TARGET=10.6. This means that extensions are built by default with a toolchain that, I think, mimics gcc-4.2, in particular in terms of what stdlib it searches. In the past, I have fixed this by installing more recent compilers with homebrew and explicitly setting CC, CXX, etc before installation. However, I have tried just setting MACOSX_DEPLOYMENT_TARGET=10.11, and that seems to work. Is this safe? Are there any downsides? (I don't need to distribute these builds, just use them locally?) Conversely, are there any upsides? Does a newer deployment target allow more recent compilers and/or higher optimizations? -Andrew -- https://mail.python.org/mailman/listinfo/python-list
Re: Question About Running Python code
On 15/10/2014 23:50, ryguy7272 wrote: The error that I get is this. 'invalid syntax' The second single quote in this line is highlighted pink. print 'Downloading data from Yahoo for %s sector' % sector This is a script written for Python 2.*, but you say you are using Python 3.4. In Python 3, print is a function, not a statement, so you need to translate this to print('Downloading data from Yahoo for %s sector' % sector) -- https://mail.python.org/mailman/listinfo/python-list
Re: Log base 2 of large integers
On 13/08/2014 14:46, Mok-Kong Shen wrote: Am 13.08.2014 15:32, schrieb Steven D'Aprano: Mok-Kong Shen wrote: I like to compute log base 2 of a fairly large integer n but with math.log(n,2) I got: OverflowError: long int too large to convert to float. Is there any feasible work-around for that? If you want the integer log2, that is, the floor of log2, the simplest way is calculate it like this: removed... see below Does that help? That is too inaccurate (e.g. for 513 above) for me, I would like to get accuracy around 0.01 and that for very large n. M. K. Shen Well, we can use Steven d'A's idea as a starting point: import math def log2_floor(n): Return the floor of log2(n). if n = 0: raise ValueError i = -1 while n: n //= 2 i += 1 return i def log2(n): return log_2(n) by splitting the problem into the integer and fractional parts l2f = log2_floor(n) if n == 2**l2f: return l2f else: return l2f + math.log(n*2**-l2f, 2) Andrew -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 3 on Mac OS X 10.8.4
On 19/06/2014 08:02, Une Bévue wrote: On my mac i do have : $ python --version Python 2.7.2 I want to install Python 3 such as python-3.4.0-macosx10.6.dmg avoiding disturbing the built-in version. Is that possible ? The python.org packages are explicitly created in order to have no conflict with the system installed python. There is no problem with using them. Yours, Andrew -- https://mail.python.org/mailman/listinfo/python-list
[issue3588] sysconfig variable LINKFORSHARED has wrong value for MacOS X framework build
Andrew Jaffe added the comment: Was this actually fixed? As per http://bugs.python.org/issue16848 it affects python-config --ldflags which is used by various build systems. -- nosy: +Andrew.Jaffe ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue3588 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16848] Mac OS X: python-config --ldflags and location of Python.framework
Andrew Jaffe added the comment: Will this be fixed? I note that the related LINKFORSHARED bug (which causes this, I think) is marked as resolved. -- nosy: +Andrew.Jaffe ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16848 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: classmethods, class variables and subclassing
Andrew Jaffe wrote: Hi, I have a class with various class-level variables which are used to store global state information for all instances of a class. These are set by a classmethod as in the following class sup(object): cvar1 = None cvar2 = None @classmethod def setcvar1(cls, val): cls.cvar1 = val @classmethod def setcvar2(cls, val): cls.cvar2 = val @classmethod def printcvars(cls): print cls.cvar1, cls.cvar2 Now, the problem comes when I want to subclass this class. If I override the setcvar1 method to do some new things special to this class, and then call the sup.setcvar1() method, it all works fine: class sub(sup): cvar1a = None @classmethod def setcvar1(cls, val, vala): cls.cvar1a = vala sup.setcvar1(val) @classmethod def printcvars(cls): print cls.cvar1a sup.printcvars() This works fine, and sets cvar and cvar2 for both classes. However, if I *don't* override the setcvar2 method, but I call sub.setcvar2(val) directly, then only sub.cvar2 gets set; it is no longer identical to sup.cvar1! In particular, sub.setcvar1(1,10) sub.setcvar2(2) sub.printcvars() prints 10 1 None i.e. sub.cvar1, sub.cvar1a, sub.cvar2= 1 10 2 but sup.cvar1, cvar2= 1 None This behavior is expected, but is it desirable? You are experiencing this problem because you are using hard-wired class names. Try using (for example) self.__class__. That way, even if your method is inheroted by a subclass it will use the class of the object it finds itself a method of. No need to use classmethods. The problem is that I actually do want to call these methods on the class itself, before I've made any instances. A -- http://mail.python.org/mailman/listinfo/python-list
Re: classmethods, class variables and subclassing
Steve Holden wrote: Andrew Jaffe wrote: The problem is that I actually do want to call these methods on the class itself, before I've made any instances. Except you could use staticmethods with an explicit class argument ... Steve, Yep, that would work! Thanks. But it does seem like a bit of a kludge: classmethods seems to be almost exactly what you 'ought' to use here (i.e., I really do want to apply these methods to the class as an object in its own right). A -- http://mail.python.org/mailman/listinfo/python-list
Re: classmethods, class variables and subclassing
Steve Holden wrote: Andrew Jaffe wrote: Steve Holden wrote: Andrew Jaffe wrote: The problem is that I actually do want to call these methods on the class itself, before I've made any instances. Except you could use staticmethods with an explicit class argument ... Yep, that would work! Thanks. But it does seem like a bit of a kludge: classmethods seems to be almost exactly what you 'ought' to use here (i.e., I really do want to apply these methods to the class as an object in its own right). What's the use case? Fair question. I hope the following isn't too technical. I have a class which describes the model for fitting some data, encapsulating among other things a bunch of parameters whose values I'd like to determine for a given dataset. The base class is a simple model, the derived class a slightly more complicated one with an extra parameter. At present, I instantiate the class with a particular set of values for the parameters. One of the methods in this class is called 'prior', which returns the prior probability for the instance's paramters. However, it turns out there are some 'meta-parameters' which don't change between instances, in particular the allowed limits on the parameters, beyond which the prior should return 0. Currently these are stored as class variables -- so both the base and derived class want to be able to act as if these class variables are 'native' to their own class -- since in fact the base/derived relationship is in this case actually something of an implementation detail. (Currently I've solved the problem by explicitly using the base class methods for setting the class variables.) Does this make sense? Andrew -- http://mail.python.org/mailman/listinfo/python-list
Re: classmethods, class variables and subclassing
Steven Bethard wrote: Andrew Jaffe wrote: I'm not sure if I understand your goal here, but you can get different behavior using super(). py class sup(object): ... cvar1 = None ... cvar2 = None ... @classmethod ... def setcvar1(cls, val): ... cls.cvar1 = val ... @classmethod ... def setcvar2(cls, val): ... cls.cvar2 = val ... @classmethod ... def printcvars(cls): ... print cls.cvar1, cls.cvar2 ... py class sub(sup): ... cvar1a = None ... @classmethod ... def setcvar1(cls, val, vala): ... cls.cvar1a = vala ... super(sub, cls).setcvar1(val) ... @classmethod ... def printcvars(cls): ... print cls.cvar1a ... super(sub, cls).printcvars() ... py sub.setcvar1(1, 10); sub.setcvar2(2); sub.printcvars() 10 1 2 py sup.printcvars() None None Aha! This is the behavior I want -- the variables get set correctly when the methods are called on the subclass. I thought super() might be the right idea, but I didn't realize you could call it as super(sub, cls) rather than with an instance in the second slot. I don't think my use case ever needs the superclass to have the variables accessible as in your next ideas, not reproduced here. Thanks! Andrew -- http://mail.python.org/mailman/listinfo/python-list
classmethods, class variables and subclassing
Hi, I have a class with various class-level variables which are used to store global state information for all instances of a class. These are set by a classmethod as in the following (in reality the setcvar method is more complicated than this!): class sup(object): cvar1 = None cvar2 = None @classmethod def setcvar1(cls, val): cls.cvar1 = val @classmethod def setcvar2(cls, val): cls.cvar2 = val @classmethod def printcvars(cls): print cls.cvar1, cls.cvar2 I can then call setcvar on either instances of the class or the class itself. Now, the problem comes when I want to subclass this class. If I override the setcvar1 method to do some new things special to this class, and then call the sup.setcvar1() method, it all works fine: class sub(sup): cvar1a = None @classmethod def setcvar1(cls, val, vala): cls.cvar1a = vala sup.setcvar1(val) @classmethod def printcvars(cls): print cls.cvar1a sup.printcvars() This works fine, and sets cvar and cvar2 for both classes. However, if I *don't* override the setcvar2 method, but I call sub.setcvar2(val) directly, then only sub.cvar2 gets set; it is no longer identical to sup.cvar1! In particular, sub.setcvar1(1,10) sub.setcvar2(2) sub.printcvars() prints 10 1 None i.e. sub.cvar1, sub.cvar1a, sub.cvar2= 1 10 2 but sup.cvar1, cvar2= 1 None since sup.cvar2 has never been set, and this is what sup.printcvars() looks for. This behavior is expected, but is it desirable? For my application, at least, I think the problem really comes in the printcvars method: is there any way to call the overridden sup.printcvars() but with, effectively, cls=sub? Thanks for reading this far! Andrew Andrew This seems like a bug Is this expected behavior, or a bug (or both -- it is expected but probably not what is wanted!)? -- http://mail.python.org/mailman/listinfo/python-list