понедельник, 21 ноября 2016 г.

latex math to png

import getopt
import os
import subprocess
import sys
import tempfile


def __build_preamble():
    preamble = '\documentclass[a4paper, 12pt]{article}\n'
    preamble += \
"""
\usepackage[unicode]{hyperref}
\usepackage{mathtext}
\usepackage[T1, T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\\vskip 0.5 cm
\pagestyle{empty}
"""
    preamble += "\\begin{document}\n"
    return preamble

def math2png(eqs, outdir, prefix = '', size = 1):
    # Set the working directory
    workdir = tempfile.gettempdir()
    # Get a temporary file
    fd, texfile = tempfile.mkstemp('.tex', 'eq', workdir, True)
    # Create the TeX document
    with os.fdopen(fd, 'w+') as f:
        f.write(__build_preamble())
        for eq in eqs:
            f.write("$%s$\n" % eq.strip())
        f.write('\end{document}')

    f.close()

    # Generate the DVI file
    latexcmd = 'latex -halt-on-error -output-directory %s %s' % (workdir, texfile)
    _run(latexcmd.split())
    # Convert the DVI file to PNG's
    dvifile = texfile.replace('.tex', '.dvi')
    outprefix = os.path.join(outdir, prefix)
    dvicmd = "dvipng -T tight -x %i -z 9 -bg Transparent "\
            "-o %s%%d.png %s" % (size * 1000, outprefix, dvifile)
    _run(dvicmd.split())

def _run(cmd):
    env = os.environ.copy()
    env.update(dict(
        LANG='ru_RU.UTF-8',
        LC_CTYPE='ru_RU.UTF-8',
        HOME='/root',
    ))
    subprocess.Popen(
        cmd,
        env=env,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    ).communicate()

hideme proxy python

#encoding:utf-8
import httplib
import mechanize
import random
import time
import urllib
import urllib2


def initial_hideme():
    url = 'http://hideme.ru/login'
    params = urllib.urlencode({'c': '193477955'})
    urllib.urlopen(url, params)

class IpProxyDNotExist(Exception):

    pass


def get_good_proxy():

    goods = []

    test_url = 'http://example.com'
    count = 4

    def check(ip):
        br = mechanize.Browser()
        br.set_proxies({'http': ip})
        br.set_handle_robots(False)
        try:
            br.open(test_url, timeout=2)
        except (urllib2.URLError, httplib.BadStatusLine):
            pass
        else:
            goods.append(ip)

    proxy_list = get_proxy_list()
    random.shuffle(proxy_list)
    proxy_list = proxy_list[:count]
    for num, ip in enumerate(proxy_list):
        if goods:
            continue
        check(ip)

    try:
        ret = goods[0]
    except IndexError:
        raise IpProxyDNotExist

    return ret


def get_proxy_list():
    response = urllib2.urlopen('http://hideme.ru/api/proxylist.php?anon=4&out=plain')
    response_text = response.read().strip()
    ret = response_text.split()
    return ret

Код сайта kontrolnaya-rabota.ru

equations/detailed.py:
# -*- coding:  utf-8 -*-
from hideme_proxy import get_good_proxy
from mechanize._response import response_seek_wrapper
from wolfram.utils import get_appid, get_user_agent
import mechanize
import time
import urllib
import random
import re
import xml.etree.ElementTree as ET


def get_inequal_detailed(list_data):
    expressions = []
    for data in list_data:
        solve1 = data['solve1']
        solve2 = data['solve2']
        oper = data['oper']
        expression = '%s%s%s' % (solve1, oper, solve2)
        expressions.append(expression)
    text = ' and '.join(expressions)
    expression_quote = urllib.quote(text)
    appid = get_appid()
    url_pattern = 'http://api.wolframalpha.com/v2/query?input=Solve+{0}&appid={1}&format=plaintext'
    url = url_pattern.format(expression_quote, appid)
    br = mechanize.Browser()
    br.set_handle_robots(False)
    br.set_handle_equiv(False)
    ip = get_good_proxy()
    br.set_proxies({'http': ip})
    ua = get_user_agent()
    br.addheaders = [('User-agent', ua)]
    br.open(url)
    response = br.response()
    response_text = response.read()
    root = ET.fromstring(response_text)
    if root.get('success') == 'true':
        elems = root.findall("./pod[@id='Result']/subpod/plaintext")
        ret = [el.text for el in elems]
        return ret
    raise NotImplementedError(response_text)


def get_equal_detailed(data):
    solve1 = data['solve1']
    solve2 = data['solve2']
    expression = '%s=%s' % (solve1, solve2)
    expression_quote = urllib.quote(expression)
    appid = get_appid()
    url_pattern = 'http://api.wolframalpha.com/v2/query?input=Solve+{0}&appid={1}&format=plaintext&podstate=Result__Step-by-step+solution'
    url = url_pattern.format(expression_quote, appid)
    ip = get_good_proxy()
    br = mechanize.Browser()
    br.set_proxies({'http': ip})
    ua = get_user_agent()
    br.addheaders = [('User-agent', ua)]
    br.set_handle_robots(False)
    br.set_handle_equiv(False)
    br.open(url)
    response = br.response()
    response_text = response.read()
    root = ET.fromstring(response_text)
    if root.get('success') == 'true':
        text = root.find("./pod//subpod[@title='Possible intermediate steps']/plaintext").text
        return text
    raise NotImplementedError(response_text)


def test_appid(appid):
    url = 'http://api.wolframalpha.com/v2/query?input=Solve+x^2-1&appid={0}&format=plaintext&podstate=Result__Step-by-step+solution'.format(appid)
    ip = get_good_proxy()
    br = mechanize.Browser()
    br.set_proxies({'http': ip})
    ua = get_user_agent()
    br.addheaders = [('User-agent', ua)]
    br.set_handle_robots(False)
    br.set_handle_equiv(False)
    br.open(url)
    response = br.response()
    response_text = response.read()
    root = ET.fromstring(response_text)
    if root.get('success') == 'true':
        return 'OK'
    return response_text
buy/views.py:
# encoding:utf-8
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.shortcuts import redirect, render
from django.utils.translation import ugettext as _
from django.views.decorators.cache import cache_page
from latexmath2png import math2png
from s.buy.forms import FreeForm, FreeFormWithoutCaptha
from s.equal_one.detailed import get_equal_detailed
from settings import at_project_root
from wolfram.base import get_solution_class
import datetime
import hashlib
import re
import time


def free_old(request):
    return redirect('/s/', permanent=True)


def free(request):
    form = FreeForm(request.POST or None)
    redirect_to = None
    if form.is_valid():
        cleaned_data = form.cleaned_data
        key = generate_key()
        base_url = reverse('buy-complete', kwargs={'key': key})
        query_string = form.get_request_string(cleaned_data)
        redirect_to = '%s?%s' % (base_url, query_string)
    context = {'free_form': form, 'redirect_to': redirect_to}
    template_name = 's/buy/free.html'
    return render(request, template_name, context)


@cache_page(60 * 60)
def buy_complete(request, key):

    user_agent = request.META.get('HTTP_USER_AGENT')

    if 'Mediapartners-Google' in user_agent or \
        'YandexDirect' in user_agent:
        return free(request)

    if key != generate_key():
        return redirect('/s/')
    form = FreeFormWithoutCaptha(request.GET or None)
    if not form.is_valid():
        return redirect(reverse('s-main'))
    module = form.cleaned_data['module']
    data = form.cleaned_data['data']
    if module in ['diff', 'limit']:
        SolutionClass = get_solution_class(module)
        text = SolutionClass(data, with_proxy=False).get_text()
    elif module in ('indefinite', 'definite'):
        SolutionClass = get_solution_class('indefinite')
        text = SolutionClass(data).get_text()
    elif module == 'equal':
        text = get_equal_detailed(data)
    for trans in BLOCK_I18N:
        text = text.replace(trans, _(trans))
    text = text.replace('\\n', '\n')
    text = text.replace('\\/', '/')
    if 'wolfram' in text.lower():
        raise ValidationError(u'wolfram text on solution')
    tex_body = text_to_tex(text)
    prefix = hashlib.md5(tex_body.encode('utf-8')).hexdigest()
    try:
        math2png(
            [tex_body.encode('utf-8')],
            'media/buy',
            prefix=prefix,
        )
    except:
        with_simple_text = True
    else:
        with_simple_text = False

    time.sleep(3)
    template_name = 's/buy/complete.html'
    context = {
        'tex_body': tex_body,
        'prefix': prefix,
        'with_simple_text': with_simple_text,
        'text': text,
    }
    return render(request, template_name, context)


def i18n_blocks():
    fn = at_project_root('kr/wolfram.trans')
    ret = open(fn).read().strip().decode('utf-8').split('\n')
    ret = sorted(ret, key=lambda s: len(s), reverse=True)
    return ret

BLOCK_I18N = i18n_blocks()


def generate_key():
    today_text = datetime.date.today().strftime('%F')
    open_key = '%s-%s' % (settings.SECRET_KEY, today_text)
    key = hashlib.md5(open_key).hexdigest()
    return key


def text_to_tex(text):
    fp = '(?:cos|sin|tan|atan|csc|cot|sec|log|ln|sqrt|sinh|cosh|arccos|arcsin|arccosh|arcsinh|arctg|arcctg|atan)'

    ac = '[_a-zA-Z@^\d.]'

    ac_p = '[_a-zA-Z@^{}\d.]'

    text = text.replace('d/dx', '@@@')
    text = text.replace('tan^(-1)', 'arctg')
    text = text.replace('cos^(-1)', 'arccos')
    text = text.replace('sin^(-1)', 'arcsin')
    text = text.replace('cot^(-1)', 'arcctg')
    text = re.sub(r'\b(sec)\b', '1/sin', text)
    text = re.sub(r'd(sec)\b', 'd 1/sin', text)
    text = re.sub(r'\b(csc)\b', '1/cos', text)
    text = re.sub(r'd(csc)\b', 'd 1/cos', text)
    text = re.sub(r'\b(log)\b', 'ln', text)
    text = re.sub(r'd(log)\b', 'd log', text)
    text = re.sub(r'\b(cot)\b', 'ctg', text)
    text = re.sub(r'd(cot)\b', 'd ctg', text)
    text = re.sub(r'\b(tan)\b', 'tg', text)
    text = re.sub(r'd(tan)\b', 'd tg', text)

    text = re.sub(r'lim_\(([^\)]+)\)', r'lim_{\1}', text)

    text = re.sub(r'(%(fp)s)\^%(b)s\(' % {
        'fp': fp, 'b': get_bucket_pattern_by_level(4)[2:-2]
    }, r'\1^{\2}(', text)

    A = []
    j = 0
    for i in range(5):
        list_expr = set(re.findall(r'(%(fp)s*\([^\(\)]+\))' % {'fp': fp}, text))
        list_expr = sorted(list_expr, key=lambda expr: len(expr))
        list_expr.reverse()
        for bracket in list_expr:
            text = text.replace(bracket, '__III%s__' % j)
            A.append(bracket)
            j += 1

    for j, bucket in enumerate(A):
        in_bucket = bucket
        if bucket.startswith('('):
            in_bucket = in_bucket[1:-1]
        in_bucket = re.sub(r'(%(fp)s*%(ac_p)s+)/(%(fp)s*%(ac_p)s+)' % {
            'fp': fp, 'ac_p': ac_p
        }, r'\\frac{\1}{\2}', in_bucket)

        in_bucket = re.sub(r'(%(ac)s+)\^(%(ac)s+)' % {
            'ac': ac
        }, r'\1^{\2}', in_bucket)
        if bucket.startswith('('):
            A[j] = '(%s)' % in_bucket
        else:
            A[j] = in_bucket

    text = re.sub(r'(%(fp)s*%(ac_p)s+)/(%(fp)s*%(ac_p)s+)' % {
        'fp': fp, 'ac_p': ac_p
    }, r'\\frac{\1}{\2}', text)
    text = re.sub(r'(%(ac)s+)\^(%(ac)s+)' % {'ac': ac}, r'\1^{\2}', text)

    for i in range(5):
        for j, backet in enumerate(A):
            text = text.replace('__III%s__' % j, backet)

    text = text.replace('= | ', '\\\\\\\\ = ').replace(' | ', '\\\\\\\\').replace(' ', '\;').replace('\n', '\\\\\\\\')
    text = text.replace("\\'", "'")

    text = re.sub(r'{\(([^{}]+)\)}', r'{\1}', text)

    text = text.replace('@@@', '\\frac{d}{dx}')
    text = text.replace('integral', '\\int')
    text = text.replace('·', u'*')
    text = text.replace('>infinity', u'>oo')
    text = text.replace('>-infinity', u'>-oo')
    return text


def get_bucket_pattern_by_level(level):
    list_res = []
    b0 = '[^\(\)]'
    c = '%s+' % b0
    list_res.append(c)
    for i in range(level):
        b = '\((%s)\)' % '|'.join(list_res)
        c = '(?:(?:\((?:%s)\))|%s)+' % (b[3:-3], b0)
        list_res.append(c)
    return b
buy/urls.py:
from django.conf.urls.defaults import patterns, url

urlpatterns = patterns('s.buy.views',
    url(r'^free/$',  'free_old' , name='free-old'),
    url(r'^besplatno/$',  'free' , name='free'),
    url(r'^complete/(?P[0-9a-f]+)/$',  'buy_complete', name='buy-complete'),
)
buy/forms.py
# -*- coding: utf8 -*-
from s.models import Ads, ExcludeUrl
from captcha.fields import CaptchaField, CaptchaTextInput
from django import forms
import json
import urllib


BUY_VERSION = 2


class FreeFormWithoutCaptha(forms.Form):

    data = forms.CharField(widget=forms.HiddenInput)
    module = forms.CharField(widget=forms.HiddenInput)

    def clean_data(self):
        data = self.cleaned_data['data']
        try:
            data = json.loads(data)
        except:
            raise forms.ValidationError('No load json')
        return data

    def get_request_string(self, cleaned_data):
        data = cleaned_data['data']
        data = json.dumps(data)
        data = urllib.quote(data)
        module = cleaned_data['module']
        req_string = 'module=%(module)s&data=%(data)s' % {
            'module': module, 'data': data,
        }
        return req_string


if BUY_VERSION == 1:


    class FreeForm(FreeFormWithoutCaptha):

        url = forms.URLField(
            help_text=(
                u'URL страницы, где содержится активная ссылка на мой сайт'
            ), label=u'Url страницы',
        )
        captcha = CaptchaField()

        def __init__(self, *args, **kwargs):
            super(FreeForm, self).__init__(*args, **kwargs)
            self.fields['captcha'].widget.attrs = {'autocomplete': "off"}

        def clean_url(self):
            url = self.cleaned_data['url'].lower()
            exclude_urls = ExcludeUrl.objects.all().values_list('url', flat=True)
            for exclude_url in exclude_urls:
                if url.startswith('http://' + exclude_url) or \
                    url.startswith('http://www.' + exclude_url):
                    raise forms.ValidationError(
                        (u'Котэ следит за тобой. Пожалуйста, добавьте ссылку себе на '
                         u'сайт, форум или блог!')
                    )
            return url

        def get_ads(self):
            ads = Ads.objects.random()
            return ads

elif BUY_VERSION == 2:

    class FreeForm(FreeFormWithoutCaptha):

        captcha = CaptchaField()

        def __init__(self, *args, **kwargs):
            super(FreeForm, self).__init__(*args, **kwargs)
            self.fields['captcha'].widget.attrs = {'autocomplete': "off"}

        def get_ads(self):
            ads = Ads.objects.random()
            return ads
wolfram/utils.py
# coding:utf-8
from settings import at_project_root
import random


def get_appid():
    filename = at_project_root('kr/wolfram.keys')
    appid_list = open(filename).read().strip().split()
    random.shuffle(appid_list)
    appid = appid_list[0]
    return appid


def get_user_agent():
    filename = at_project_root('kr/wolfram.ua')
    ua_list = open(filename).read().strip().split('\n')
    assert len(ua_list) > 1
    random.shuffle(ua_list)
    ua = ua_list[0]
    return ua
wolfram/base.py
#encoding:utf-8
from hideme_proxy import get_good_proxy, IpProxyDNotExist
from wolfram.utils import get_user_agent, get_appid
import httplib
import mechanize
import urllib
import urllib2
import xml.etree.ElementTree as ET
import zlib


_open_url_error_classes = (
    urllib2.HTTPError,
    urllib2.URLError,
    IOError,
    httplib.BadStatusLine,
    httplib.IncompleteRead,
    zlib.error,  # for gzip compressed responses
    IpProxyDNotExist,
)


class InternetError(Exception):

    pass


def get_solution_class(class_name):
    return SOLUTION_CLASSES[class_name]


SOLUTION_CLASSES = {}
def register_solution_class(class_name):
    def wrapper(cls):
        SOLUTION_CLASSES[class_name] = cls
        return cls
    return wrapper


class _InternetObject(object):

    def _initial_browser(self):
        br = mechanize.Browser()
        ua = get_user_agent()
        br.addheaders = [('User-agent', ua)]
        br.set_handle_robots(False)
        br.set_handle_equiv(False)
        return br

    def _get_rawurlencode(self, params):
        items = []
        for key, val in params.items():
            new_val = urllib.quote(val)
            item = '{0}={1}'.format(key, new_val)
            items.append(item)
        ret = '&'.join(items)
        return ret

    def get_params(self):
        raise NotImplementedError


class DetailedSolution(_InternetObject):

    base_url = 'http://api.wolframalpha.com/v2/query'

    def __init__(self, data, with_proxy=True):
        self.data = data
        self.with_proxy = with_proxy
        self.br = self._initial_browser()
        self.appid = get_appid()

    def __str__(self):
        super_str = super(DetailedSolution, self).__str__()
        ret = '{0} with appid={1}'.format(super_str, self.appid)
        return ret

    def get_text(self):
        response_text = self.get_response_text()
        root = ET.fromstring(response_text)
        if root.get('success') == 'true':
            xpath = "./pod//subpod[@title='Possible intermediate steps']/plaintext"
            text = root.find(xpath).text
            return text
        raise NotImplementedError(response_text)

    def get_response_text(self):
        if self.with_proxy:
            try:
                ip = get_good_proxy()
            except _open_url_error_classes, exc:
                raise InternetError(exc)
            self.br.set_proxies({'http': ip})
        params = self.get_params()
        params.update({
            'format': 'plaintext',
            'appid': self.appid,
        })
        url = self.base_url + '?' + \
            self._get_rawurlencode(params)
        try:
            self.br.open(url)
            response = self.br.response()
            response_text = response.read()
        except _open_url_error_classes, exc:
            raise InternetError(exc)
        return response_text


@register_solution_class('limit')
class LimitSolution(DetailedSolution):

    def get_params(self):
        data = self.data
        x0 = data['x0']
        function = data['function']
        expression = 'lim %s x->%s' % (function, x0)
        ret = {
            'input': expression,
            'podstate': 'Limit__Step-by-step solution',
        }
        return ret


@register_solution_class('indefinite')
class IndefiniteSolution(DetailedSolution):

    def get_params(self):
        data = self.data
        function = data['function']
        expression = 'integrate %s' % function
        ret = {
            'input': expression,
            'podstate': 'IndefiniteIntegral__Step-by-step solution',
        }
        return ret


@register_solution_class('diff')
class DiffSolution(DetailedSolution):

    def get_params(self):
        data = self.data
        function = data['function']
        expression = 'derivative of %s' % function
        ret = {
            'input': expression,
            'podstate': 'Input__Step-by-step solution',
        }
        return ret


@register_solution_class('series-number')
class SeriesNumberSolution(DetailedSolution):

    def get_result(self):
        ret = {}
        response_text = self.get_response_text()
        root = ET.fromstring(response_text)
        if root.get('success') == 'true':
            nods = root.findall('./pod[@id="ConvergenceTests"]/subpod/plaintext')
            tests = map(lambda n: n.text, nods)
            ret['tests'] = tests
            return ret
        raise NotImplementedError(response_text)

    def get_params(self):
        data = self.data
        function = data['function']
        n = data['n']
        n0 = data['n0']
        nm = data['nm']
        infinity = data['infinity']
        if infinity:
            nm = 'oo'
        expression = 'Sum[{0}, {1}, {2}, {3}]'.format(function, n, n0, nm)
        ret = {
            'input': expression,
        }
        return ret


@register_solution_class('power-series')
class PowerSeriesSolution(DetailedSolution):

    def get_result(self):
        ret = {}
        response_text = self.get_response_text()
        root = ET.fromstring(response_text)
        if root.get('success') == 'true':
            nods = root.findall('./pod[@id="ConvergenceTests"]/subpod/plaintext')
            tests = map(lambda n: n.text, nods)
            ret['tests'] = tests
            return ret
        raise NotImplementedError(response_text)

    def get_params(self):
        expression = 'Sum of {function}'.format(**self.data)
        ret = {
            'input': expression,
        }
        return ret


@register_solution_class('system-diff')
class SystemDiffSolution(DetailedSolution):

    def get_result(self):
        ret = {}
        response_text = self.get_response_text()
        root = ET.fromstring(response_text)
        if root.get('success') == 'true':
            nods = root.findall('./pod[@id="DifferentialEquationSolution"]/subpod/plaintext')
            solves = map(lambda n: n.text, nods)
            solves = map(lambda s: s.replace('_', ''), solves)
            ret['solves'] = solves
            return ret
        raise NotImplementedError(response_text)

    def get_params(self):
        expression = ' and '.join('='.join(i) for i in self.data['equations'])
        ret = {
            'input': expression,
        }
        return ret
widgetes/jscalendar.py
from django import forms
from django.template.loader import render_to_string

class JSCalendarWidget(forms.DateTimeInput):
    class Media:
        css = {
            'all': ('javascript/jscalendar-1.0/calendar-system.css',)
        }
        js = ('javascript/jscalendar-1.0/calendar.js',
              'javascript/jscalendar-1.0/calendar-setup.js',
              'javascript/jscalendar-1.0/lang/calendar-ru-UTF.js',)

    def render(self, name, value, lang='ru', attrs=None):
        value = value or ''
        return render_to_string("widgets/jscalendar.html", locals())
buy/complete.html
{% extends "s/sbase.html" %}
{% block title %}Подробное решение бесплатно!{% endblock %}
{% block head %}
    
{% endblock %}
{% block crumbs %}Больше решений!{% endblock %}
{% block base_table %}
    

Подробное решение: ответ

{% include "partner_block.html" %} {% if with_simple_text %}
{{ text }}
{% else %} {{ text }} {% endif %} {% include "other-social.html" %} {% include "partner_block.html" %}
{% endblock %}
buy/definite.tex
\documentclass[a4paper, 12pt]{article}
\usepackage[unicode]{hyperref}
\usepackage{mathtext}
\usepackage[T1, T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\vskip 0.5 cm

\begin{document}

${{ tex_body }}$

\end{document}
buy/diff.tex
\documentclass[a4paper, 12pt]{article}
\usepackage[unicode]{hyperref}
\usepackage{mathtext}
\usepackage[T1, T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\vskip 0.5 cm

\begin{document}

${{ tex_body }}$

\end{document}
buy/free_form.html

Для того, чтобы получить подробное решение бесплатно, пожалуйста, проделайте следующие действия:

    {% if not request.COOKIES.ilikeit %}
  1. Скопируйте ссылку и
    разместите вконтакте или др. сайте:
  2. {% endif %}
  3. Введите цифры на рисунке
{% csrf_token %} {{ free_form }}
buy/free.html
{% extends "s/sbase.html" %}
{% load i18n %}
{% block head %}
 {% if redirect_to %}
 
 
 {% endif %}
{% endblock %}
{% block title %}Подробное решение бесплатно!{% endblock %}

{% block content %}
  

Подробное решение

{% include "partner_block.html" %} {% if redirect_to %}

Производится проверка размещения ссылки, пожалуйста подождите чуть больше 1 мин...

Если ссылка на наш сайт будет найдена, то Вам будет предложено решение.
Если программа не найдет ссылку вида http://kontrolnaya-rabota.ru, то Вы не получите файла, но можно попробовать разместить ссылку на сайт http://kontrolnaya-rabota.ru/s/ снова.
Назад.
{% else %}

Хотите получить подробное решение с описанием действий?

{% include "s/buy/free_form.html" %} {% endif %} {% include "partner_block.html" %} {% endblock %}
buy/indefinite.tex
\documentclass[a4paper, 12pt]{article}
\usepackage[unicode]{hyperref}
\usepackage{mathtext}
\usepackage[T1, T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\vskip 0.5 cm

\begin{document}

${{ tex_body }}$

\end{document}
buy/limit.html
\documentclass[a4paper, 12pt]{article}
\usepackage[unicode]{hyperref}
\usepackage{mathtext}
\usepackage[T1, T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\vskip 0.5 cm

\begin{document}

${{ tex_body|safe }}$

\end{document}

среда, 9 ноября 2016 г.

python regex lazy re.compile

import re


class _Lazy(object):

    __slots__ = ['pattern', 'kwargs', 'compiled']

    def __init__(self, pattern, **kwargs):
        self.pattern = pattern
        self.kwargs = kwargs
        self.compiled = None

    def __getattr__(self, name):
        if self.compiled is None:
            self.compiled = re.compile(self.pattern, **self.kwargs)
        return self.compiled.__getattribute__(name)


def lazy_compile(pattern, **kwargs):
    return _Lazy(pattern, **kwargs)
Use:
In [2]:

In [2]: lazy_compile(r'[a-z]')
Out[2]: <__main__._Lazy at 0x11095c088>

In [3]: c = lazy_compile(r'[a-z]')

In [4]: c.findall('sdsd sdsds aq, $A fddf')
Out[4]: ['s', 'd', 's', 'd', 's', 'd', 's', 'd', 's', 'a', 'q', 'f', 'd', 'd', 'f']

понедельник, 23 ноября 2015 г.

Upload a folder to s3

from boto3.session import Session
import os

session = Session(
    aws_access_key_id='xxx',
    aws_secret_access_key='yyy',
    region_name='eu-central-1',  # Put your region name
)

resource = session.resource('s3')

bucket = resource.Bucket('your_bucket_name')

from_dir = 'your_folder_name'

for fn in os.listdir(from_dir):
    file_obj = open(os.path.join(from_dir, fn), 'rb')

    to_folder = ''

    bucket.put_object(
        Key=os.path.join(to_folder, os.path.basename(file_obj.name)),
        Body=file_obj.read(),
    )
    print '%s loaded' % fn

воскресенье, 19 января 2014 г.

Integral Step-by-Step Calculator on Android!

I write my first android application.

I used.. python!

You can write android application on python - use python framework kivy
(write a application supported on many operation systems: mac, windows, linux, android, ios, and sony play station and other)

So, my first android application - Integral Step-by-Step Calculator (link to google play)

This app calculate a integrals:

Offline Integral Step-By-Step Calculator (without online and internet):
* A indefinite integral
* A definite integral
* A improper integral

понедельник, 6 января 2014 г.

Large Scale Machine Learning and Other Animals: Installing BLAS/Lapack/ITPP on Amazon EC2/Ubuntu L...

 Installing BLAS/Lapack/ITPP on Amazon EC2/Ubuntu Linux
BLAS/Lapack are efficient matrix math libraries. The following instructions explains how to install them for Amazon EC2 (Ubuntu maverick version, and Amazon Linux). It++ (itpp) is a popular c++ wrapper for blas/lapack.

DISLAIMER: The below instructions are for 64 bit machines. For 32 bit machines follow other instructions: http://bickson.blogspot.com/2011/06/graphlab-pmf-on-32-bit-linux.html

FOR LAZY READERS:
Just use Amazon EC2 public image ami-c21eedab (Ubuntu)

INSTALLATION VIA YUM/APT-GET
Try to install itpp using the following command:

sudo yum install libitpp-dev

Or

sudo apt-get install libitpp-dev

TIP: You may also want to install libitpp7-dbg using the yum/apt-get command.
It is not mandatory, but it helps debugging when you link against libitpp_debug.so
(instead of libitpp.so).

If the above worked then we are done. If not, you will need to follow
instructions below. Thanks to Udi Weinsberg for this tip.

FOR ADVANCED USERS:


0) Start with an Ubuntu image like ami-641eed0d, or an Amazon AMI image like:

1) Install required packages. For Ubuntu:

sudo apt-get install --yes --force-yes automake autoconf libtool* gfortran

For Amazon Linux:

sudo yum install -y automake autoconf libtool* gcc-gfortran


2) Install lapack.
Here again their are two options:

The easy way is to simply (On Ububtu)

sudo apt-get install --yes --force-yes liblapack-dev

On Amazon Linux:

sudo yum install -y lapack-devel blas-devel


Thanks Akshay Bhat from Cornell for this tip!
If the liblapack setup was successful, go to step 3.

If the above command DOES NOT work for you (depends on your OS and setup) you will need to install lapack manually. The procedure is explained in steps a-c below.

a) Download and prepare the code

wget http://www.netlib.org/lapack/lapack.tgz
tar xvzf lapack.tgz
cd lapack-3.3.0  //if version number changes, change here to the right directory
mv make.inc.example make.inc


b) edit make.inc and add -m64 -fPIC flag to fortran compiler options:
# FORTRAN, OPTS, NOOPT, LOADER

c) compile

make blaslib
make

If everthing went OK, test will be run for a couple of minutes
and the files blas_LINUX.a and lapack_LINUX.a will be created at the main directory

3) setup LDFLAGS

export LDFLAGS="-L/usr/lib -lgfortran"


4) Download and install itpp from

wget http://sourceforge.net/projects/itpp/files/itpp/4.2.0/itpp-4.2.tar.gz
tar xvzf itpp-4.2.tar.gz
cd itpp-4.2
./autogen.sh

If you installed Lapack from yum/apt-get, you should use the following command:

./configure --without-fft --with-blas=/usr/lib64/libblas.so --with-lapack=/usr/lib64/liblapack.so --enable-debug CFLAGS=-fPIC CXXFLAGS=-fPIC CPPFLAGS=-fPIC

Where /usr/lib64/ is the place where lapack was installed.

If you installed lapack from source, use the following command

./configure --without-fft --with-blas=/home/ubuntu/lapack-3.3.0/blas_LINUX.a --with-lapack=/home/ubuntu/lapack-3.3.0/lapack_LINUX.a CFLAGS=-fPIC CXXFLAGS=-fPIC CPPFLAGS=-fPIC


make
sudo make install

Note: If you installed lapack from yum/apt-get, don't forget to add the -lblas -llapack linker flag when you compile against lapack/blas.

Verifying installation
To verify that installation went Ok, run the following commands:

itpp-config --cflags
itpp-config --libs

1) The command itpp-config should be available from shell.
2) The right installation path should appear as output.

Known issues you may encounter:
Problem:

*** Warning: Linking the shared library libitpp.la against the^M
*** static library /usr/lib64/libblas.a is not portable!^M
libtool: link: g++ -shared -nostdlib /usr/lib/gcc/x86_64-amazon-linux/4.4.4/../../../../lib64/crti.o /usr/lib/gcc/x86_64-amazon-linux/4.4.4/crtbeginS.o  -Wl,--whole-archive ../itpp/base/.libs/libbase.a ../itpp/stat/.libs/libstat.a ../itpp/comm/.libs/libcomm.a ../itpp/fixed/.libs/libfixed.a ../itpp/optim/.libs/liboptim.a ../itpp/protocol/.libs/libprotocol.a ../itpp/signal/.libs/libsignal.a ../itpp/srccode/.libs/libsrccode.a -Wl,--no-whole-archive  -L/usr/lib64/ /usr/lib64/liblapack.a /usr/lib64/libblas.a -lgfortranbegin -lgfortran -L/usr/lib/gcc/x86_64-amazon-linux/4.4.4 -L/usr/lib/gcc/x86_64-amazon-linux/4.4.4/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-amazon-linux/4.4.4/../../.. -lstdc++ -lm -lc -lgcc_s /usr/lib/gcc/x86_64-amazon-linux/4.4.4/crtendS.o /usr/lib/gcc/x86_64-amazon-linux/4.4.4/../../../../lib64/crtn.o    -Wl,-soname -Wl,libitpp.so.7 -o .libs/libitpp.so.7.0.0^M
/usr/bin/ld: /usr/lib64/liblapack.a(dgees.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC^M
/usr/lib64/liblapack.a: could not read symbols: Bad value^M
collect2: ld returned 1 exit status^M
make[2]: *** [libitpp.la] Error 1^M

Solution: it seems that lapack was statically compiled without the -fPIC option and thus itpp refuses to link against it. Follow step 2a to install lapack manaually with the -fPIC option.

Problem:

make[1]: *** Waiting for unfinished jobs....
[ 83%] Building CXX object src/graphlab/CMakeFiles/
graphlab_pic.dir/distributed2/distributed_scheduler_list.o /usr/local/lib/libitpp.so: undefined reference to `zgesv_' /usr/local/lib/libitpp.so: undefined reference to `dorgqr_' /usr/local/lib/libitpp.so: undefined reference to `dswap_' /usr/local/lib/libitpp.so: undefined reference to `dgeqp3_' /usr/local/lib/libitpp.so: undefined reference to `dpotrf_' /usr/local/lib/libitpp.so: undefined reference to `dgemm_' /usr/local/lib/libitpp.so: undefined reference to `zungqr_' /usr/local/lib/libitpp.so: undefined reference to `zscal_' /usr/local/lib/libitpp.so: undefined reference to `dscal_' /usr/local/lib/libitpp.so: undefined reference to `dgesv_' /usr/local/lib/libitpp.so: undefined reference to `dgetri_' /usr/local/lib/libitpp.so: undefined reference to `zgemm_' /usr/local/lib/libitpp.so: undefined reference to `zposv_' /usr/local/lib/libitpp.so: undefined reference to `zgetri_' /usr/local/lib/libitpp.so: undefined reference to `dgeev_' /usr/local/lib/libitpp.so: undefined reference to `zgemv_' /usr/local/lib/libitpp.so: undefined reference to `zgeqrf_' /usr/local/lib/libitpp.so: undefined reference to `zgerc_' /usr/local/lib/libitpp.so: undefined reference to `zswap_' /usr/local/lib/libitpp.so: undefined reference to `zgeev_' /usr/local/lib/libitpp.so: undefined reference to `daxpy_' /usr/local/lib/libitpp.so: undefined reference to `dgetrf_' /usr/local/lib/libitpp.so: undefined reference to `zgels_' /usr/local/lib/libitpp.so: undefined reference to `zgetrf_' /usr/local/lib/libitpp.so: undefined reference to `dgees_' /usr/local/lib/libitpp.so: undefined reference to `dcopy_' /usr/local/lib/libitpp.so: undefined reference to `dger_' /usr/local/lib/libitpp.so: undefined reference to `dgels_' /usr/local/lib/libitpp.so: undefined reference to `dgeqrf_' /usr/local/lib/libitpp.so: undefined reference to `zpotrf_' /usr/local/lib/libitpp.so: undefined reference to `zgees_' /usr/local/lib/libitpp.so: undefined reference to `dgesvd_' /usr/local/lib/libitpp.so: undefined reference to `zgeru_' /usr/local/lib/libitpp.so: undefined reference to `dsyev_' /usr/local/lib/libitpp.so: undefined reference to `zaxpy_' /usr/local/lib/libitpp.so: undefined reference to `ddot_' /usr/local/lib/libitpp.so: undefined reference to `zgesvd_' /usr/local/lib/libitpp.so: undefined reference to `zgeqp3_' /usr/local/lib/libitpp.so: undefined reference to `zcopy_' /usr/local/lib/libitpp.so: undefined reference to `dgemv_' /usr/local/lib/libitpp.so: undefined reference to `dposv_' /usr/local/lib/libitpp.so: undefined reference to `zheev_' collect2: ld returned 1 exit status make[2]: *** [tests/anytests] Error 1 make[1]: *** [tests/CMakeFiles/anytests.dir/all] Error 2

Solution: itpp was compiled using dynamic libraries, but your application did not include the -lblas and -llapack link flags.

Problem:

*** Error: You must have "autoconf" installed to compile IT++ SVN sources
*** Error: You must have "automake" installed to compile IT++ SVN sources
*** Error: You must have "libtoolize" installed to compile IT++ SVN sources

Solution:
Need to install the packages autoconf, automake and libtoolize. See yum/apt-get documentation.

Problem:

/usr/bin/ld: /home/bickson/lapack-3.3.1/lapack_LINUX.a(dgees.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/home/bickson/lapack-3.3.1/lapack_LINUX.a: could not read symbols: Bad value
collect2: ld returned 1 exit status

Solution:
It seem you forgot to follow section 2b.

TIP: It is useful to enable also itpp_debug library which is very useful when debugging your code. This is done by adding the flag --enable-debug to the configure script.

Problem:

*** Error in ../../../itpp/base/algebra/ls_solve.cpp on line 271:
LAPACK library is needed to use ls_solve() function

Solution:
It seems that itpp is not installed properly -it did not link to lapack.