Re: pyserial: wait for execute
On Saturday, October 15, 2016 at 10:14:18 PM UTC-6, Michael Okuntsov wrote: > Hello, > is there a way, other than time.sleep(), to be sure that the command > sent through a serial port has been fully executed? I'm interested > specifically in SCPI commands in VA-meters such as Keithley and Tektronix. > Thanks. Looks like the out_waiting method on serial.Serial class is what you need. Or perhaps use the flush method, the doc seems to say it returns when the write completes. -- https://mail.python.org/mailman/listinfo/python-list
How make the judge with for loop?
c="abcdefghijk" len=len(c) n is a int sb=[[] for i in range(n)] while (i < len) { for (int j = 0; j < n && i < len; j++) sb[j].append(c[i++]); for (int j = n-2; j >= 1 && i < len; j--) // sb[j].append(c[i++]); } How to translate to python? I tried but my python code is really stupid -- https://mail.python.org/mailman/listinfo/python-list
pyserial: wait for execute
Hello, is there a way, other than time.sleep(), to be sure that the command sent through a serial port has been fully executed? I'm interested specifically in SCPI commands in VA-meters such as Keithley and Tektronix. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: No registration confirmation at https://bugs.python.org/
On Sun, 16 Oct 2016 07:09 am, Al Schapira wrote: > I have tried to register at https://bugs.python.org/ over a period > of many months, and I never receive the confirmation email to complete > the process. Who can help with this? Thanks. > --Al Have you checked your Junk Mail folder? Unfortunately there are at least four open issues relating to email from the bug tracker being marked as spam: http://psf.upfronthosting.co.za/roundup/meta/ -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
No registration confirmation at https://bugs.python.org/
I have tried to register at https://bugs.python.org/ over a period of many months, and I never receive the confirmation email to complete the process. Who can help with this? Thanks. --Al -- https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3?
在 2016年10月14日星期五 UTC-4下午7:35:08,38016...@gmail.com写道: > nums=['3','30','34','32','9','5'] > I need to sort the list in order to get the largest number string: '953433230' > > nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True) > > But how to do this in python 3? > > Thank you !I learn more new tricks in Python. Thank you all of you guys. You are all very kind helpful and knowledgeable. -- https://mail.python.org/mailman/listinfo/python-list
Re: Appending to a list, which is value of a dictionary
On 15.10.2016 18:16, Steve D'Aprano wrote: # Python 3 only: use a dict comprehension py> d = {x:[] for x in (1, 2, 3)} py> d {1: [], 2: [], 3: []} dict (and set) comprehensions got backported so this works just as well in Python 2.7 Wolfgang -- https://mail.python.org/mailman/listinfo/python-list
Re: Appending to a list, which is value of a dictionary
On Sat, 15 Oct 2016 11:35 pm, Uday J wrote: > Hi, > > Here is the code, which I would like to understand. > l=['a','b','c'] bm=dict.fromkeys(l,['-1','-1']) fromkeys() doesn't make a copy of the list each time it is used. It uses the exact same list each time. Watch: py> L = [] py> d = dict.fromkeys((1, 2, 3), L) py> print(id(d[1]), id(d[2]), id(d[3]), id(L)) 3081897484 3081897484 3081897484 3081897484 You get the same ID number four times: there's only one dict here, in four places: L, d[1], d[2] and d[3] all refer to the same dict. So what what happens: py> print(d) {1: [], 2: [], 3: []} py> L.append('abc') py> print(d) {1: ['abc'], 2: ['abc'], 3: ['abc']} That's because this is NOT four copies of the same list, but just the same list, in four places. Here are two options: # Python 3 only: use a dict comprehension py> d = {x:[] for x in (1, 2, 3)} py> d {1: [], 2: [], 3: []} py> d[1].append('abc') py> d {1: ['abc'], 2: [], 3: []} # any version of Python d = {} for x in (1, 2, 3): d[x] = [] -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Appending to a list, which is value of a dictionary
On Sun, Oct 16, 2016 at 3:12 AM, Jussi Piitulainen wrote: > Chris Angelico writes: > >> On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote: >> bm=dict.fromkeys(l,['-1','-1']) >> >> When you call dict.fromkeys, it uses the same object as the key every >> time. If you don't want that, try a dict comprehension instead: > > s/key/value/ What he said. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Appending to a list, which is value of a dictionary
Chris Angelico writes: > On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote: > bm=dict.fromkeys(l,['-1','-1']) > > When you call dict.fromkeys, it uses the same object as the key every > time. If you don't want that, try a dict comprehension instead: s/key/value/ -- https://mail.python.org/mailman/listinfo/python-list
Re: Appending to a list, which is value of a dictionary
Uday J writes: > Hi, > > Here is the code, which I would like to understand. > l=['a','b','c'] bm=dict.fromkeys(l,['-1','-1']) u={'a':['Q','P']} bm.update(u) bm > {'a': ['Q', 'P'], 'c': ['-1', '-1'], 'b': ['-1', '-1']} for k in bm.keys(): > bm[k].append('DDD') > bm > {'a': ['Q', 'P', 'DDD'], 'c': ['-1', '-1', 'DDD', 'DDD'], 'b': ['-1', '-1', > 'DDD', 'DDD']} > > I was expecting appending DDD to happen once for 'c' and 'b'. > {'a': ['Q', 'P', 'DDD'], 'c': ['-1', '-1', 'DDD'], 'b': ['-1', '-1', 'DDD']} It happened once for 'c' and once for 'd' but bm['c'] and bm['d'] are the same list so it happened twice for that list. -- https://mail.python.org/mailman/listinfo/python-list
Re: Appending to a list, which is value of a dictionary
On Sat, Oct 15, 2016 at 11:35 PM, Uday J wrote: bm=dict.fromkeys(l,['-1','-1']) When you call dict.fromkeys, it uses the same object as the key every time. If you don't want that, try a dict comprehension instead: bm = {x: ['-1', '-1'] for x in l} This will construct a new list for every key, giving you the behaviour you expect. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Appending to a list, which is value of a dictionary
Hi, Here is the code, which I would like to understand. >>> l=['a','b','c'] >>> bm=dict.fromkeys(l,['-1','-1']) >>> u={'a':['Q','P']} >>> bm.update(u) >>> bm {'a': ['Q', 'P'], 'c': ['-1', '-1'], 'b': ['-1', '-1']} >>> for k in bm.keys(): bm[k].append('DDD') >>> bm {'a': ['Q', 'P', 'DDD'], 'c': ['-1', '-1', 'DDD', 'DDD'], 'b': ['-1', '-1', 'DDD', 'DDD']} I was expecting appending DDD to happen once for 'c' and 'b'. {'a': ['Q', 'P', 'DDD'], 'c': ['-1', '-1', 'DDD'], 'b': ['-1', '-1', 'DDD']} -- https://mail.python.org/mailman/listinfo/python-list
Re: try-except with no exceptions
On Thu, 13 Oct 2016 15:06:25 +0100, Daiyue Weng wrote: > I know that such try-catch usage is generally a bad practice, since it > can't locate the root of the exceptions. > > I am wondering how to correct the code above Either identify the specific exceptions you're expecting, or if you're interested in "any error", use "except StandardError". That will catch "errors" but won't catch e.g. KeyboardInterrupt or SystemExit. Trying to further narrow the set of possible exceptions is often impossible considering the use of duck typing. -- https://mail.python.org/mailman/listinfo/python-list
Re: Different behaviour of regexp in 3.6.0b2
Serhiy Storchaka writes: > Seems the documentation is not accurate. Could you file a report on > https://bugs.python.org/ ? Thank you everybody answered! Here it is: http://bugs.python.org/issue28450 ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. l...@metapensiero.it | -- Fortunato Depero, 1929. -- https://mail.python.org/mailman/listinfo/python-list
Re: Different behaviour of regexp in 3.6.0b2
Serhiy Storchaka wrote: > On 14.10.16 20:01, Peter Otten wrote: > def double_bs(s): return "".join(s.split("\\")) >> ... > Just use s.replace('\\', r'\\'). D'oh! -- https://mail.python.org/mailman/listinfo/python-list
Re: Different behaviour of regexp in 3.6.0b2
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 is "my_user" or "the_other_user". Definite improvement. The re module emitted deprecation warnings in 3.5. In 3.6 warnings become errors. The idea is that this allows to add new special sequences (like \p{...} or \R) in future. -- https://mail.python.org/mailman/listinfo/python-list
Re: Different behaviour of regexp in 3.6.0b2
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+'), 'foo bar') 'foo\\s\\+bar' OK, that probably escaped too much. Second attempt: re.sub(r'\s+', lambda m: r'\s+', 'foo bar') 'foo\\s+bar' Better? If that's too much work at runtime: def double_bs(s): return "".join(s.split("\\")) ... re.sub(r'\s+', double_bs(r'\s+'), 'foo bar') 'foo\\s+bar' Just use s.replace('\\', r'\\'). -- https://mail.python.org/mailman/listinfo/python-list
Re: Different behaviour of regexp in 3.6.0b2
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/pgcli/blob/master/pgcli/packages/prioritization.py#L11, ignore the fact that the re.sub() call seem underoptimal). With Python 3.5.2 is straightforward: $ python3.5 Python 3.5.2+ (default, Sep 22 2016, 12:18:14) [GCC 6.2.0 20160927] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> re.sub(r'\s+', r'\s+', 'foo bar') 'foo\\s+bar' While Python 3.6.0b2 gives: $ python3.6 Python 3.6.0b2+ (default, Oct 11 2016, 08:30:05) [GCC 6.2.0 20160927] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> re.sub(r'\s+', r'\s+', 'foo bar') Traceback (most recent call last): File "/usr/local/python3.6/lib/python3.6/sre_parse.py", line 945, in parse_template this = chr(ESCAPES[this][1]) KeyError: '\\s' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/usr/local/python3.6/lib/python3.6/re.py", line 191, in sub return _compile(pattern, flags).sub(repl, string, count) File "/usr/local/python3.6/lib/python3.6/re.py", line 326, in _subx template = _compile_repl(template, pattern) File "/usr/local/python3.6/lib/python3.6/re.py", line 317, in _compile_repl return sre_parse.parse_template(repl, pattern) File "/usr/local/python3.6/lib/python3.6/sre_parse.py", line 948, in parse_template raise s.error('bad escape %s' % this, len(this)) sre_constants.error: bad escape \s at position 0 Accordingly to the documentation (https://docs.python.org/3.6/library/re.html#re.sub) “unknown escapes [in the repl argument] such as \& are left alone”. Am I missing something, or is this a regression? Unknown escapes consisting of "\" following by ASCII letter are errors in 3.6 (and warnings in 3.5). Seems the documentation is not accurate. Could you file a report on https://bugs.python.org/ ? -- https://mail.python.org/mailman/listinfo/python-list
Re: How to sort this without 'cmp=' in python 3?
38016226...@gmail.com wrote: > nums=['3','30','34','32','9','5'] > I need to sort the list in order to get the largest number string: > '953433230' > > nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True) > > But how to do this in python 3? > > Thank you While cmp_to_key is neat doing it by hand should also be instructive. Essentially you move the comparison into a method of the key: $ cat translate_cmp.py class Key(str): def __lt__(a, b): return a + b < b + a nums = ['3','30','34','32','9','5'] print(nums) nums.sort(key=Key, reverse=True) print(nums) print("".join(nums)) $ python3 translate_cmp.py ['3', '30', '34', '32', '9', '5'] ['9', '5', '34', '3', '32', '30'] 953433230 The above works because in CPython list.sort() currently uses only the < operator; adding __gt__() and __eq__() to make this portable is straightforward even if you do not use the functools.total_ordering class decorator. -- https://mail.python.org/mailman/listinfo/python-list