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 / 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 / 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 / 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 / os_path_getsize.py
Last active March 31, 2024 12:01
Этот код использует модуль os.path из стандартной библиотеки языка Python для получения размера файла.
os.path.getsize("/path/isa_005.mp3")
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 / 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)
@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 / ran_random_module.py
Created April 9, 2022 12:09
Python run random module
def run_random_module(modules):
module_name = random.choice(modules)
__import__(module_name)
@pointofpresence
pointofpresence / set_current_working_dir.py
Created April 9, 2022 12:08
Python set current working dir
import os
import sys
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
@pointofpresence
pointofpresence / python enum.py
Last active March 31, 2024 20:11
Python: Использование Enum
from enum import Enum, auto
class Color(Enum):
RED = auto()
BLUE = auto()
GREEN = auto()
list(Color) # [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]