Skip to content

Instantly share code, notes, and snippets.

@belyaev-pa
belyaev-pa / main.py
Last active August 10, 2018 08:34
redis client/generator task
from redis import Redis
import string
import random
from time import sleep
def random_message_generate(length=100):
message = ''.join([random.choice(
string.ascii_letters + string.digits) for n in range(length)])
return message
@belyaev-pa
belyaev-pa / time.py
Created June 8, 2018 08:47
time decorator
def timer(func):
def tmp(*args, **kwargs):
t = time.time()
res = func(*args, **kwargs)
print("f time - {} {}".format(str(time.time()-t), f.__name__, ))
return res
return tmp
def kangaroo(input_str):
""" для решения задачи необходимо решить уравнение:
x1 + y * v1 = x2 + y * v2, так же проверим условия,
и проверим крайние уникальные условия, при которых
возникает деление на 0: v2 == v1"""
input_list = input_str.split()
x1, v1, x2, v2 = map(int, input_list)
if x1 < 0 or x1 > x2 or x2 > 10000 or\
v1 < 1 or v1 > 10000 or\
v2 < 1 or v2 > 10000:
import itertools
def get_data(file_name):
"""generator to yield file row by row
for not upload it to memory"""
with open(file_name, encoding='Windows-1251') as f:
rows = f.readlines()
for row in rows:
yield (row)
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@belyaev-pa
belyaev-pa / filter.py
Created January 10, 2019 15:01
email filter
# !/usr/bin/python
# -*- coding: utf-8 -*-
from email.parser import Parser
import syslog
import email
import base64
import ldap
import sys
import argparse
import os
def decorator(arg1, arg2):
def real_decorator(function):
def wrapper(*args, **kwargs):
print("Congratulations. You decorated a "
"function that does something with %s and %s" % (arg1, arg2))
for obj in args: # это список
print('hello: {}'.format(obj))
for key, value in kwargs.items(): # это словарь
print('key={}, value={}'.format(key, value))
function(*args, **kwargs)
static private int NumberSum(int number)
{
int sum = 0;
while(number != 0)
{
sum += number % 10;
number /= 10;
}
return sum;
}
# -*- coding: utf-8 -*-
from collections import defaultdict, OrderedDict
PINGDROP = OrderedDict([
('- первичный пинг локальных сокетов', ['ping', '-i', '0.2', '-w', '1', '127.0.0.1']),
(
'установка правила для блокировки icmp пакетов идущих на локальные сокеты',
['iptables', '-I', 'INPUT', '-s', '127.0.0.1', '-p', 'icmp', '-j', 'DROP']
),
@belyaev-pa
belyaev-pa / socket_server.py
Created April 29, 2019 16:20
socket_server
import socket
import sys
import os
import json
import time
server_address = './uds_socket.s'
# Make sure the socket does not already exist