Skip to content

Instantly share code, notes, and snippets.

@st4lk
st4lk / django_log_settings.py
Last active December 2, 2022 07:14
Django logging settings
# Logging settings for django projects, works with django 1.5+
# If DEBUG=True, all logs (including django logs) will be
# written to console and to debug_file.
# If DEBUG=False, logs with level INFO or higher will be
# saved to production_file.
# Logging usage:
# import logging
# logger = logging.getLogger(__name__)
# logger.info("Log this message")
@st4lk
st4lk / root_logger_settings.py
Last active May 2, 2024 15:25
Python logging settings for root logger
"""
Settings for root logger.
Log messages will be printed to console and also to log file (rotated, with
specified size). All log messages from used libraries will be also handled.
Three approaches for defining logging settings are used:
1. using logging classes directly (py25+, py30+)
2. using fileConfig (py26+, py30+)
3. using dictConfig (py27+, py32+)
Choose any variant as you like, but keep in mind python versions, that
@st4lk
st4lk / vkcom_audio_download.py
Last active October 24, 2023 13:13
Python: vkontakte.ru (vk.com) audio music downloader
# -*- coding: utf-8 -*-
"""
Скрипт для скачивания музыки с сайта vkontakte.ru (vk.com)
Запуск:
python vkcom_audio_download.py
Принцип работы:
Скрипт проверяет сохраненный access_token. Если его нет или срок истек,
то открывается страница в браузере с запросом на доступ к аккаунту.
@st4lk
st4lk / gist:4442771
Created January 3, 2013 11:22
Postgres: dump and restore
- pg_dump -U postgres -W -h localhost DATABASE_NAME > outfile
- psql -U postgres -W -h localhost DATABASE_NAME < infile
@st4lk
st4lk / gist:4131438
Created November 22, 2012 14:29
Bash: ssh add passphrase
ssh-add ~/.ssh/id_rsa
@st4lk
st4lk / gist:4011771
Last active October 12, 2015 10:18
Python: django south migrations
python manage.py convert_to_south appname
python manage.py schemamigration appname --auto
python manage.py migrate appname
python manage.py migrate appname --fake 0003
@st4lk
st4lk / gist:4000793
Created November 2, 2012 12:06
Python: django create fixtures
python manage.py dumpdata --indent=4 > initial_data.json
@st4lk
st4lk / gist:3958188
Created October 26, 2012 10:58
MySQL: dump database
1)Создание базы для Django:
CREATE DATABASE database_name CHARACTER SET utf8;
CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'user_password';
GRANT ALL ON database_name.* TO 'user_name'@'localhost';
2) Дамп базы
Сохранение: mysqldump -u имя_пользователя -p -h имя_сервера_БД имя_базы > dump.sql
Восстановление: mysql -u имя_пользователя -p -h имя_сервера_БД имя_базы < dump.sql
@st4lk
st4lk / gist:3957114
Created October 26, 2012 05:52
Python: OrderedSet
class OrderedSet(collections.MutableSet):
def __init__(self, iterable=None):
self.end = end = []
end += [None, end, end] # sentinel node for doubly linked list
self.map = {} # key --> [key, prev, next]
if iterable is not None:
self |= iterable
def __len__(self):
@st4lk
st4lk / gist:3954996
Created October 25, 2012 19:49
Xpath: useful exp
# xpath contains
u'//div[@class="rules"]/div[@class="inner"][contains(./div/text(), "Фотографии не найдены")]'
# xpath 'and' end 'exact'
u'//div[@id="accordion"]/table//tr[./td[1]/text()="SHAPE" and ./td[2]/text()="CARATS"]'
# xpath subling
root_elem.xpath('./following-sibling::*')
# xpath parent