Skip to content

Instantly share code, notes, and snippets.

View apalii's full-sized avatar
🐍
focusing

Andrii Palii apalii

🐍
focusing
View GitHub Profile
@apalii
apalii / data_structures.py
Last active December 5, 2018 10:17
Algorithms
# How to find a missing number from a list?
>>> a=[1,2,3,4,5,7,8,9,10]
>>> sum(xrange(a[0],a[-1]+1)) - sum(a)
6
def is_matched(expression):
"""
Finds out how balanced an expression is.
With a string containing only brackets.
@apalii
apalii / generators.py
Last active October 2, 2015 14:23
Iteration protocol
# --- iterators ---
# Если у объекта есть методы __iter__() __next__() значит он является итератором
class MyIter(object):
def __init__(self, start, stop):
self._current = start
self._stop = stop
def __iter__(self):
# _____________________________________________________
def roman_to_arabic(line):
roman_numerals = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
class Phonebook:
def __init__(self):
self.entries = {}
def add(self, name, number):
self.entries[name] = number
def lookup(self, name):
return self.entries[name]
@apalii
apalii / top100_us_cities.ipynb
Last active August 29, 2015 14:21
The largest US cities
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
def output(color, message='empty message'):
colors = {
'blue' : '\033[94m',
'green' : '\033[92m',
import requests
from bs4 import BeautifulSoup as bs
import sys
def get_track_status(track_number):
base_url = 'http://novaposhta.ua/ru/tracking/?cargo_number='
headers = {'Accept-Language':'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3'}
data = requests.get(base_url + track_number, headers=headers)
data.encoding = 'utf-8'
soup = bs(data.text)
@apalii
apalii / mem_usage.py
Last active August 29, 2015 14:10
memory usage of some process
import psutil
for proc in psutil.process_iter():
try:
proc = proc.as_dict(attrs=['pid', 'name'])
except psutil.NoSuchProcess:
pass
else:
if proc['name'] == 'firefox':
>>> def decorate(func):
... print u"Декорируем %s..." % func.__name__,
... def wrapped(*args, **kwargs):
... print "Вызываем обёрнутую функцию с аргументами:", args
... return func(*args, **kwargs)
... print u"выполнено!"
... return wrapped
import logging
@apalii
apalii / networkcheck
Created October 14, 2014 14:15
check ONBOOT/GATEWAY/HWADDR on all servers
for i in `mysql -uroot porta-configurator -sse "select ip from Servers"`
do
h=`mysql -uroot porta-configurator -sse "select Name from Servers where IP='$i'"`
echo -e "\n====== $h $i ======\n"
rsh_porta.sh $i '
red=`echo -en "\e[31m"`
green=`echo -en "\e[32m"`
normal=`echo -en "\e[0m"`
lightyellow=`echo -en "\e[93m"`
if [ $(grep -qi gateway /etc/sysconfig/network; echo "$?") -eq 0 ]