This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import inspect | |
| def f1(): f2() | |
| def f2(): | |
| curframe = inspect.currentframe() | |
| calframe = inspect.getouterframes(curframe, 2) | |
| print('caller name:', calframe[1][3]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Python 3 code to demonstrate | |
| # to remove elements present in other list | |
| # using set() | |
| set1 = set(['a', 'b']) | |
| set2 = set(['c', 'b']) | |
| res = list(set1 - set2) | |
| print(res) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def cool_glob(dir_list, patterns_list, recursive=False): | |
| output = [] | |
| template = '**/{pattern}' if recursive else '{pattern}' | |
| for dir_name in list(set(dir_list)): | |
| for pattern in list(set(patterns_list)): | |
| output.extend(list(pathlib.Path(dir_name).glob(template.format(pattern=pattern)))) | |
| return output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| os.path.getsize("/path/isa_005.mp3") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| mile_distances = [1.0, 6.5, 17.4, 2.4, 9] | |
| kilometer_distances = list(map(lambda x: x * 1.6, mile_distances)) | |
| print (kilometer_distances) | |
| # [1.6, 10.4, 27.84, 3.84, 14.4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Binary request and save | |
| # Request the profile picture of the OP: | |
| response = requests.get("https://i.stack.imgur.com/iysmF.jpg?s=32&g=1") | |
| with open("response.jpg", "wb") as f: | |
| f.write(response.content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Игнорирование значений | |
| for _ in range(10): | |
| print("Hello") | |
| # Игнорирование индексов | |
| my_list = [1, 2, 3, 4, 5] | |
| for _, value in enumerate(my_list): | |
| print(value) | |
| # Использование как временной переменной |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # A better practice is to inherit Signal from str: | |
| class Signal(str, Enum): | |
| red = 'red' | |
| green = 'green' | |
| orange = 'orange' | |
| brain_detected_colour = 'red' | |
| brain_detected_colour == Signal.red # direct comparison |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) | |
| context.set_font_size(52.0) | |
| (x, y, width, height, dx, dy) = context.text_extents("Hello") | |
| context.move_to(WIDTH/2. - width/2., HEIGHT/2. + height/2.) | |
| context.show_text("Hello") | |
| """ | |
| Указанный код рисует текст "Hello" по центру холста, выравнивая его горизонтально и вертикально |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(console) { | |
| console.save = function(data, filename){ | |
| if(!data) { | |
| console.error('Console.save: No data') | |
| return; | |
| } | |
| if(!filename) filename = 'console.html' | |
| if(typeof data === "object"){ | |
| data = JSON.stringify(data, undefined, 4) | |
| } |