python-3.5

Neat way of popping key, value PAIR from dictionary?

拥有回忆 提交于 2019-12-30 18:10:31
问题 pop is a great little function that, when used on dictionaries (given a known key) removes the item with that key from the dictionary and also returns the corresponding value. But what if I want the key as well? Obviously, in simple cases I could probably just do something like this: pair = (key, some_dict.pop(key)) But if, say, I wanted to pop the key-value pair with the lowest value, following the above idea I would have to do this... pair = (min(some_dict, key=some.get), some_dict.pop(min

Neat way of popping key, value PAIR from dictionary?

末鹿安然 提交于 2019-12-30 18:09:08
问题 pop is a great little function that, when used on dictionaries (given a known key) removes the item with that key from the dictionary and also returns the corresponding value. But what if I want the key as well? Obviously, in simple cases I could probably just do something like this: pair = (key, some_dict.pop(key)) But if, say, I wanted to pop the key-value pair with the lowest value, following the above idea I would have to do this... pair = (min(some_dict, key=some.get), some_dict.pop(min

python 3.5 type hints: can i check if function arguments match type hints?

筅森魡賤 提交于 2019-12-29 06:22:26
问题 does python 3.5 provide functions that allow to test whether a given argument would fit the type hints given in the function declaration? if i have e.g. this function: def f(name: List[str]): pass is there a python method that can check whether name = ['a', 'b'] name = [0, 1] name = [] name = None ... fit the type hints? i know that 'no type checking happens at runtime' but can i still check the validity of these arguments by hand in python? or if python does not provide that functionality

Unpacking generalizations

♀尐吖头ヾ 提交于 2019-12-29 04:30:06
问题 >>> LOL = [[1, 2], ['three']] >>> [*LOL[0], *LOL[1]] [1, 2, 'three'] Alright! Goodbye itertools.chain. Never liked you much anyway. >>> [*L for L in LOL] File "<ipython-input-21-e86d2c09c33f>", line 1 [*L for L in LOL] ^ SyntaxError: iterable unpacking cannot be used in comprehension Oh . Why can't we have nice things? Unpacking in a comprehension seems to be obvious/pythonic, but since they've bothered to add that special error message there was a reason for disabling it. So, what's the

Dict order in python 3.6 vs older

随声附和 提交于 2019-12-29 01:45:09
问题 I have this bit of code that I need printed out in this exact order (Visitor Team, Visitor Rating, Home Team, Home Rating, Expected Winner, Margin) when I run it through tabulate . final_dict = {'Visitor Team': visitor_team, 'Visitor Rating': visitor_rating, 'Home Team': home_team, 'Home Rating': home_rating, 'Expected Winner': expected_winner, 'Margin': expected_winner_diff} print(tabulate(final_dict, headers="keys", floatfmt=".2f", tablefmt="fancy_grid")) I've been learning and using Python

Centralize text in image outputs error

一笑奈何 提交于 2019-12-25 17:13:51
问题 Below code is not centralizing text no error in code, but i want to centralize text. import os unicode_text = u"\u0627\u0628\u067E" list_of_letters = list (unicode_text) char = u''.join(word) t1 = arabic_reshaper.reshape(char) W,H= (32, 32) img= PIL.Image.new('RGBA', (W, H), (255, 255, 255),) draw = PIL.ImageDraw.Draw(img) font = PIL.ImageFont.truetype( r"C:\Downloads\arabic.ttf", 15) t2 = get_display(t1) w, h = draw.textsize(t2.encode('utf-8')) draw.text(((W-w)/2,(H-h)/2), t2, fill="#000000"

Centralize text in image outputs error

时光总嘲笑我的痴心妄想 提交于 2019-12-25 17:13:25
问题 Below code is not centralizing text no error in code, but i want to centralize text. import os unicode_text = u"\u0627\u0628\u067E" list_of_letters = list (unicode_text) char = u''.join(word) t1 = arabic_reshaper.reshape(char) W,H= (32, 32) img= PIL.Image.new('RGBA', (W, H), (255, 255, 255),) draw = PIL.ImageDraw.Draw(img) font = PIL.ImageFont.truetype( r"C:\Downloads\arabic.ttf", 15) t2 = get_display(t1) w, h = draw.textsize(t2.encode('utf-8')) draw.text(((W-w)/2,(H-h)/2), t2, fill="#000000"

'str' object has no attribute 'p' using beautifulsoup

霸气de小男生 提交于 2019-12-25 08:14:41
问题 I have been following a tutorial on using BeautifulSoup, however when I try to read the title or even paragraphs (using soup.p) I get an error saying, "Traceback (most recent call last): File "*****/Tutorial1.py", line 9, in pTag = soup.p AttributeError: 'str' object has no attribute 'p'" I am still very new to Python, sorry to bother if these is too much of an easy issue but I will greatly appreciate any help. Code given below: import urllib.request from bs4 import BeautifulSoup with urllib

Is it possible to use concurrent.futures to execute a function/method inside a tkinter class following an event? If yes, how?

老子叫甜甜 提交于 2019-12-25 07:47:45
问题 I am trying to use the pool of workers provided by concurrent.futures.ProcessPoolExecutor to speed up the performance of a method inside a tkinter class. This is because executing the method is cpu intensive and "parallelizing" it should shorten the time to complete it. I hope to benchmark it's performance against a control - a serial execution of the same method. I have written a tkinter GUI test code to perform this benchmark. The serial execution of the method works but the concurrent part