Skip to content

Instantly share code, notes, and snippets.

View pointofpresence's full-sized avatar
🏠
Working from home

ReSampled pointofpresence

🏠
Working from home
View GitHub Profile
@pointofpresence
pointofpresence / caller.py
Created April 9, 2022 12:42
Python caller name
import inspect
def f1(): f2()
def f2():
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
print('caller name:', calframe[1][3])
@pointofpresence
pointofpresence / remove_elements_present_in_other_list.py
Created April 9, 2022 12:45
Python remove elements present in other list
# 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)
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
@pointofpresence
pointofpresence / os_path_getsize.py
Last active March 31, 2024 12:01
Этот код использует модуль os.path из стандартной библиотеки языка Python для получения размера файла.
os.path.getsize("/path/isa_005.mp3")
@pointofpresence
pointofpresence / python_map_example.py
Last active March 31, 2024 12:46
Python: Использование map() для преобразования списка
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]
@pointofpresence
pointofpresence / Binary request and save.py
Last active March 31, 2024 20:08
Python: Скачать и сохранить бинарный файл
# 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)
@pointofpresence
pointofpresence / underscore.py
Last active March 31, 2024 13:22
Python: Одноразовая переменная
# Игнорирование значений
for _ in range(10):
print("Hello")
# Игнорирование индексов
my_list = [1, 2, 3, 4, 5]
for _, value in enumerate(my_list):
print(value)
# Использование как временной переменной
@pointofpresence
pointofpresence / enum_with_strings.py
Last active June 14, 2022 13:41
Python: Enum with strings
# 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
@pointofpresence
pointofpresence / cairo_text_centered.py
Last active March 31, 2024 14:00
Python: Текст по центру холста (Cairo)
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" по центру холста, выравнивая его горизонтально и вертикально
@pointofpresence
pointofpresence / saveFromConsole2file.js
Last active June 13, 2022 15:53
console.save(data, filename) - save object to file from console
(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)
}