# -*- coding: utf-8 -*-
import scrapy
import re
import json
import pymysql

from mysql_test import connect_config
connection=connect_config('lianjia')

class LianjiaSpider(scrapy.Spider):

    name = 'lianjia'
    def start_requests(self):
        urllist=['https://sz.lianjia.com/ershoufang/rs/','https://gz.lianjia.com/ershoufang/rs/','https://fs.lianjia.com/ershoufang/rs/','https://zh.lianjia.com/ershoufang/rs/','https://zs.lianjia.com/ershoufang/rs/','https://hui.lianjia.com/ershoufang/rs/']
        for url in urllist:
            yield scrapy.Request(url)

    def parse(self, response):
        city=re.findall("https://(.*?)\.",response.url)[0]

        rl = response.xpath('//div[@data-role="ershoufang"]/div/a/@href').extract()
        urls = ["https://{}.lianjia.com".format(city) + i for i in rl]
        for url in urls:
            yield scrapy.Request(url, callback=self.get_region)

    def get_region(self,response):
        rl = response.xpath('//div[@data-role="ershoufang"]/div[2]/a/@href').extract()
        first_url = re.findall('((.*?)com)', response.url)[0][0]
        urllist = [first_url + i for i in rl]
        for url in urllist:
            yield scrapy.Request(url, callback=self.parse_url)

    def parse_url(self, response):
        urllist = []
        if response.xpath('//div[@class="page-box house-lst-page-box"]/@page-data') != []:
            next_page = json.loads(
                response.xpath('//div[@class="page-box house-lst-page-box"]/@page-data').extract()[0])
            totalPage = next_page["totalPage"]
            if totalPage != None:
                for i in range(1, totalPage + 1):
                    urllist.append(response.url + "pg{}".format(i))
            for url in urllist:
                yield scrapy.Request(url, callback=self.get_item)

    def get_item(self, response):

        item = {}
        detail_urls = response.xpath('//div[@class="info clear"]//div[@class="title"]/a/@href').extract()
        titles = response.xpath('//div[@class="info clear"]//div[@class="title"]/a/text()').extract()
        times = response.xpath('//div[@class="followInfo"]//text()').extract()
        addresses = response.xpath('//div[@class="address"]//a/text()').extract()
        total_prices = response.xpath('//div[@class="totalPrice"]/span/text()').extract()
        unit_Prices = response.xpath('//div[@class="unitPrice"]/span/text()').extract()

        for title, time, detail_url, address, total_price, unit_Price in zip(titles, times, detail_urls,
                                                                                addresses, total_prices,
                                                                                unit_Prices):
            unit_Price = re.findall('(\d+)', unit_Price)[0]
            total_price = re.findall('(\d+)', total_price)[0]

            item["title"] = title
            item["address"] = address
            item['followinfo']=time
            item["url"] = detail_url
            item["unit_Price"] = unit_Price
            item["total_price"] = total_price
            yield scrapy.Request(item["url"], callback=self.parse_detail, meta={'item': item})

    def parse_detail(self, response):
        item = response.meta['item']

        area=response.xpath('//div[@class="area"]/div[@class="mainInfo"]/text()').extract()[0] if len(response.xpath('//div[@class="area"]/div[@class="mainInfo"]').extract())!=0 else None
        area1 = response.xpath('//div[@class="areaName"]/span/a[1]/text()').extract()[0] if len(
            response.xpath('//div[@class="areaName"]/span/a[1]/text()').extract()) != 0 else None
        area2 = response.xpath('//div[@class="areaName"]/span/a[2]/text()').extract()[0] if len(
            response.xpath('//div[@class="areaName"]/span/a[2]/text()').extract()) != 0 else None
        room=response.xpath('//div[@class="base"]//li[1]/text()').extract()[0]  if len(response.xpath('//div[@class="base"]//li[1]/text()').extract())!= 0 else None
        floor=response.xpath('//div[@class="base"]//li[2]/text()').extract()[0] if len(response.xpath('//div[@class="base"]//li[2]/text()').extract())!= 0 else None
        direction=response.xpath('//div[@class="type"]/div[@class="mainInfo"]/text()').extract()[0] if len(response.xpath('//div[@class="type"]/div[@class="mainInfo"]/text()').extract())!= 0 else None
        type=response.xpath('//div[@class="transaction"]//li[2]/span[2]/text()').extract()[0] if len(response.xpath('//div[@class="transaction"]//li[2]/span[2]/text()').extract())!= 0 else None
        recommend=response.xpath('//div[@class="title"]//div[@class="sub"]/text()').extract()[0] if len(response.xpath('//div[@class="title"]//div[@class="sub"]/text()').extract())!= 0 else None

        item["area"]=area
        item["area1"] = area1
        item["area2"] = area2
        item['room']=room
        item['floor']=floor
        item['direction']=direction
        item["type"]=type
        item['recommend']=recommend
        city = re.findall("https://(.*?)\.", item["url"])[0]

        insert_table_sql = """\
        INSERT INTO %s(title,address,followinfo,url,unit_Price,total_price,area,area1,area2,room,floor,direction,type,recommend)
         VALUES('{title}','{address}', '{followinfo}', '{url}', '{unit_Price}', '{total_price}', '{area}', '{area1}', '{area2}', '{room}', '{floor}', '{direction}', '{type}', '{recommend}')
        """ % city
        with connection.cursor() as cursor:
            cursor.execute(insert_table_sql.format(title=item['title'],address=item['address'],followinfo=item['followinfo'],url=item['url'],unit_Price=item['unit_Price'],total_price=item['total_price'],area=item['area'],area1=item['area1'],area2=item['area2'],room=item['room'],floor=item['floor'],direction=item['direction'],type=item['type'],recommend=item['recommend']))
            connection.commit()

        ###linux后台执行程序的语句:
            #nohup python -u test.py > test.log 2>&1 &


import networkx as nx
import matplotlib.pyplot as plt

m, n = 3, 4
G = nx.grid_2d_graph(m, n)

pos = {(i, j): (i, j) for (i, j) in G.nodes()}

plt.figure()
plt.title("Problem")
nx.draw(G, pos=pos, node_size=100)
plt.show()


import copy
import itertools
import logging
import sys
import builtins

from pyomo.core.expr.expr_errors import TemplateExpressionError
from pyomo.core.expr.numvalue import (
    NumericValue, native_types, nonpyomo_leaf_types,
    as_numeric, value,
)
from pyomo.core.expr.numeric_expr import ExpressionBase, SumExpression
from pyomo.core.expr.visitor import (
    ExpressionReplacementVisitor, StreamBasedExpressionVisitor
)

logger = logging.getLogger(__name__)

class _NotSpecified(object): pass

class GetItemExpression(ExpressionBase):
    """
    Expression to call :func:`__getitem__` on the base object.
    """
    PRECEDENCE = 1

    def _precedence(self):
        return GetItemExpression.PRECEDENCE

    def __init__(self, args):
        """Construct an expression with an operation and a set of arguments"""
        self._args_ = args

    def nargs(self):
        return len(self._args_)

    def __getattr__(self, attr):
        if attr.startswith('__') and attr.endswith('__'):
            raise AttributeError()
        return GetAttrExpression((self, attr))

    def __iter__(self):
        return iter(value(self))

    def __len__(self):
        return len(value(self))

    def getname(self, *args, **kwds):
        return self._args_[0].getname(*args, **kwds)

    def is_potentially_variable(self):
        _false = lambda: False
        if any( getattr(arg, 'is_potentially_variable', _false)()
                for arg in self._args_ ):
            return True
        base = self._args_[0]
        if base.is_expression_type():
            base = value(base)
        # TODO: fix value iteration when generating templates
        #
        # There is a nasty problem here: we want to iterate over all the
        # members of the base and see if *any* of them are potentially
        # variable.  Unfortunately, this method is called during
        # expression generation, and we *could* be generating a
        # template.  When that occurs, iterating over the base will
        # yield a new IndexTemplate (which will in turn raise an
        # exception because IndexTemplates are not constant).  The real
        # solution is probably to re-think how we define
        # is_potentially_variable, but for now we will only handle
        # members that are explicitly stored in the _data dict.  Not
        # general (because a Component could implement a non-standard
        # storage scheme), but as of now [30 Apr 20], there are no known
        # Components where this assumption will cause problems.
        return any( getattr(x, 'is_potentially_variable', _false)()
                    for x in getattr(base, '_data', {}).values() )

    def _is_fixed(self, values):
        if not all(values[1:]):
            return False
        _true = lambda: True
        return all( getattr(x, 'is_fixed', _true)()
                    for x in values[0].values() )

    def _compute_polynomial_degree(self, result):
        if any(x != 0 for x in result[1:]):
            return None
        ans = 0
        for x in result[0].values():
            if x.__class__ in nonpyomo_leaf_types \
               or not hasattr(x, 'polynomial_degree'):
                continue
            tmp = x.polynomial_degree()
            if tmp is None:
                return None
            elif tmp > ans:
                ans = tmp
        return ans

    def _apply_operation(self, result):
        obj = result[0].__getitem__( tuple(result[1:]) )
        if obj.__class__ in nonpyomo_leaf_types:
            return obj
        # Note that because it is possible (likely) that the result
        # could be an IndexedComponent_slice object, must test "is
        # True", as the slice will return a list of values.
        if obj.is_numeric_type() is True:
            obj = value(obj)
        return obj

    def _to_string(self, values, verbose, smap, compute_values):
        values = tuple(_[1:-1] if _[0]=='(' and _[-1]==')' else _
                       for _ in values)
        if verbose:
            return "getitem(%s, %s)" % (values[0], ', '.join(values[1:]))
        return "%s[%s]" % (values[0], ','.join(values[1:]))

    def _resolve_template(self, args):
        return args[0].__getitem__(tuple(args[1:]))

class GetAttrExpression(ExpressionBase):
    """
    Expression to call :func:`__getattr__` on the base object.
    """
    __slots__ = ()
    PRECEDENCE = 1

    def _precedence(self):
        return GetAttrExpression.PRECEDENCE

    def nargs(self):
        return len(self._args_)

    def __getattr__(self, attr):
        if attr.startswith('__') and attr.endswith('__'):
            raise AttributeError()
        return GetAttrExpression((self, attr))

    def __getitem__(self, *idx):
        return GetItemExpression((self,) + idx)

    def __iter__(self):
        return iter(value(self))

    def __len__(self):
        return len(value(self))

    def getname(self, *args, **kwds):
        return 'getattr'

    def _compute_polynomial_degree(self, result):
        if result[1] != 0:
            return None
        return result[0]

    def _apply_operation(self, result):
        assert len(result) == 2
        obj = getattr(result[0], result[1])
        if obj.__class__ in nonpyomo_leaf_types:
            return obj
        # Note that because it is possible (likely) that the result
        # could be an IndexedComponent_slice object, must test "is
        # True", as the slice will return a list of values.
        if obj.is_numeric_type() is True:
            obj = value(obj)
        return obj

    def _to_string(self, values, verbose, smap, compute_values):
        assert len(values) == 2
        if verbose:
            return "getattr(%s, %s)" % tuple(values)
        # Note that the string argument for getattr comes quoted, so we
        # need to remove the quotes.
        attr = values[1]
        if attr[0] in '\"\'' and attr[0] == attr[-1]:
            attr = attr[1:-1]
        return "%s.%s" % (values[0], attr)

    def _resolve_template(self, args):
        return getattr(*tuple(args))

class _TemplateSumExpression_argList(object):
    """A virtual list to represent the expanded SumExpression args

    This class implements a "virtual args list" for
    TemplateSumExpressions without actually generating the expanded
    expression.  It can be accessed either in "one-pass" without
    generating a list of template argument values (more efficient), or
    as a random-access list (where it will have to create the full list
    of argument values (less efficient).

    The instance can be used as a context manager to both lock the
    IndexTemplate values within this context and to restore their original
    values upon exit.

    It is (intentionally) not iterable.

    """
    def __init__(self, TSE):
        self._tse = TSE
        self._i = 0
        self._init_vals = None
        self._iter = self._get_iter()
        self._lock = None

    def __len__(self):
        return self._tse.nargs()

    def __getitem__(self, i):
        if self._i == i:
            self._set_iter_vals(next(self._iter))
            self._i += 1
        elif self._i is not None:
            # Switch to random-access mode.  If we have already
            # retrieved one of the indices, then we need to regenerate
            # the iterator from scratch.
            self._iter = list(self._get_iter() if self._i else self._iter)
            self._set_iter_vals(self._iter[i])
        else:
            self._set_iter_vals(self._iter[i])
        return self._tse._local_args_[0]

    def __enter__(self):
        self._lock = self
        self._lock_iters()

    def __exit__(self, exc_type, exc_value, tb):
        self._unlock_iters()
        self._lock = None

    def _get_iter(self):
        # Note: by definition, all _set pointers within an itergroup
        # point to the same Set
        _sets = tuple(iterGroup[0]._set for iterGroup in self._tse._iters)
        return itertools.product(*_sets)

    def _lock_iters(self):
        self._init_vals = tuple(
            tuple(
                it.lock(self._lock) for it in iterGroup
            ) for iterGroup in self._tse._iters )

    def _unlock_iters(self):
        self._set_iter_vals(self._init_vals)
        for iterGroup in self._tse._iters:
            for it in iterGroup:
                it.unlock(self._lock)

    def _set_iter_vals(self, val):
        for i, iterGroup in enumerate(self._tse._iters):
            if len(iterGroup) == 1:
                iterGroup[0].set_value(val[i], self._lock)
            else:
                for j, v in enumerate(val[i]):
                    iterGroup[j].set_value(v, self._lock)

class TemplateSumExpression(ExpressionBase):
    """
    Expression to represent an unexpanded sum over one or more sets.
    """
    __slots__ = ('_iters', '_local_args_')
    PRECEDENCE = 1

    def _precedence(self):
        return TemplateSumExpression.PRECEDENCE

    def __init__(self, args, _iters):
        assert len(args) == 1
        self._args_ = args
        self._iters = _iters

    def nargs(self):
        # Note: by definition, all _set pointers within an itergroup
        # point to the same Set
        ans = 1
        for iterGroup in self._iters:
            ans *= len(iterGroup[0]._set)
        return ans

    @property
    def args(self):
        return _TemplateSumExpression_argList(self)

    @property
    def _args_(self):
        return _TemplateSumExpression_argList(self)

    @_args_.setter
    def _args_(self, args):
        self._local_args_ = args

    def create_node_with_local_data(self, args):
        return self.__class__(args, self._iters)

    def __getstate__(self):
        state = super(TemplateSumExpression, self).__getstate__()
        for i in TemplateSumExpression.__slots__:
            state[i] = getattr(self, i)
        return state

    def getname(self, *args, **kwds):
        return "SUM"

    def is_potentially_variable(self):
        if any(arg.is_potentially_variable() for arg in self._local_args_
               if arg.__class__ not in nonpyomo_leaf_types):
            return True
        return False

    def _is_fixed(self, values):
        return all(values)

    def _compute_polynomial_degree(self, result):
        if None in result:
            return None
        return result[0]

    def _apply_operation(self, result):
        return sum(result)

    def _to_string(self, values, verbose, smap, compute_values):
        ans = ''
        val = values[0]
        if val[0]=='(' and val[-1]==')' and _balanced_parens(val[1:-1]):
            val = val[1:-1]
        iterStrGenerator = (
            ( ', '.join(str(i) for i in iterGroup),
              ( iterGroup[0]._set.to_string(verbose=verbose)
                if hasattr(iterGroup[0]._set, 'to_string')
                else str(iterGroup[0]._set) ) )
            for iterGroup in self._iters
        )
        if verbose:
            iterStr = ', '.join('iter(%s, %s)' % x for x in iterStrGenerator)
            return 'templatesum(%s, %s)' % (val, iterStr)
        else:
            iterStr = ' '.join('for %s in %s' % x for x in iterStrGenerator)
            return 'SUM(%s %s)' % (val, iterStr)

    def _resolve_template(self, args):
        return SumExpression(args)

class IndexTemplate(NumericValue):
    """A "placeholder" for an index value in template expressions.

    This class is a placeholder for an index value within a template
    expression.  That is, given the expression template for "m.x[i]",
    where `m.z` is indexed by `m.I`, the expression tree becomes:

    _GetItem:
       - m.x
       - IndexTemplate(_set=m.I, _value=None)

    Constructor Arguments:
       _set: the Set from which this IndexTemplate can take values
    """

    __slots__ = ('_set', '_value', '_index', '_id', '_lock')

    def __init__(self, _set, index=0, _id=None):
        self._set = _set
        self._value = _NotSpecified
        self._index = index
        self._id = _id
        self._lock = None

    def __getstate__(self):
        """
        This method must be defined because this class uses slots.
        """
        state = super(IndexTemplate, self).__getstate__()
        for i in IndexTemplate.__slots__:
            state[i] = getattr(self, i)
        return state

    def __deepcopy__(self, memo):
        # Because we leverage deepcopy for expression cloning, we need
        # to see if this is a clone operation and *not* copy the
        # template.
        #
        # TODO: JDS: We should consider converting the IndexTemplate to
        # a proper Component: that way it could leverage the normal
        # logic of using the parent_block scope to dictate the behavior
        # of deepcopy.
        if '__block_scope__' in memo:
            memo[id(self)] = self
            return self
        #
        # "Normal" deepcopying outside the context of pyomo.
        #
        ans = memo[id(self)] = self.__class__.__new__(self.__class__)
        ans.__setstate__(copy.deepcopy(self.__getstate__(), memo))
        return ans

    # Note: because NONE of the slots on this class need to be edited,
    # we don't need to implement a specialized __setstate__ method.

    def __call__(self, exception=True):
        """
        Return the value of this object.
        """
        if self._value is _NotSpecified:
            if exception:
                raise TemplateExpressionError(
                    self, "Evaluating uninitialized IndexTemplate (%s)"
                    % (self,))
            return None
        else:
            return self._value

    def _resolve_template(self, args):
        assert not args
        return self()

    def is_fixed(self):
        """
        Returns True because this value is fixed.
        """
        return True

    def is_constant(self):
        """
        Returns False because this cannot immediately be simplified.
        """
        return False

    def is_potentially_variable(self):
        """Returns False because index values cannot be variables.

        The IndexTemplate represents a placeholder for an index value
        for an IndexedComponent, and at the moment, Pyomo does not
        support variable indirection.
        """
        return False

    def __str__(self):
        return self.getname()

    def getname(self, fully_qualified=False, name_buffer=None, relative_to=None):
        if self._id is not None:
            return "_%s" % (self._id,)

        _set_name = self._set.getname(fully_qualified, name_buffer, relative_to)
        if self._index is not None and self._set.dimen != 1:
            _set_name += "(%s)" % (self._index,)
        return "{"+_set_name+"}"

    def set_value(self, values=_NotSpecified, lock=None):
        # It might be nice to check if the value is valid for the base
        # set, but things are tricky when the base set is not dimention
        # 1.  So, for the time being, we will just "trust" the user.
        # After all, the actual Set will raise exceptions if the value
        # is not present.
        if lock is not self._lock:
            raise RuntimeError(
                "The TemplateIndex %s is currently locked by %s and "
                "cannot be set through lock %s" % (self, self._lock, lock))
        if values is _NotSpecified:
            self._value = _NotSpecified
            return
        if type(values) is not tuple:
            values = (values,)
        if self._index is not None:
            if len(values) == 1:
                self._value = values[0]
            else:
                raise ValueError("Passed multiple values %s to a scalar "
                                 "IndexTemplate %s" % (values, self))
        else:
            self._value = values

    def lock(self, lock):
        assert self._lock is None
        self._lock = lock
        return self._value

    def unlock(self, lock):
        assert self._lock is lock
        self._lock = None

def resolve_template(expr):
    """Resolve a template into a concrete expression

    This takes a template expression and returns the concrete equivalent
    by substituting the current values of all IndexTemplate objects and
    resolving (evaluating and removing) all GetItemExpression,
    GetAttrExpression, and TemplateSumExpression expression nodes.

    """
    def beforeChild(node, child, child_idx):
        # Efficiency: do not decend into leaf nodes.
        if type(child) in native_types or not child.is_expression_type():
            if hasattr(child, '_resolve_template'):
                return False, child._resolve_template(())
            return False, child
        else:
            return True, None

    def exitNode(node, args):
        if hasattr(node, '_resolve_template'):
            return node._resolve_template(args)
        if len(args) == node.nargs() and all(
                a is b for a,b in zip(node.args, args)):
            return node
        return node.create_node_with_local_data(args)

    return StreamBasedExpressionVisitor(
        initializeWalker=lambda x: beforeChild(None, x, None),
        beforeChild=beforeChild,
        exitNode=exitNode,
    ).walk_expression(expr)

class ReplaceTemplateExpression(ExpressionReplacementVisitor):
    template_types = {GetItemExpression, IndexTemplate}

    def __init__(self, substituter, *args, **kwargs):
        kwargs.setdefault('remove_named_expressions', True)
        super().__init__(**kwargs)
        self.substituter = substituter
        self.substituter_args = args

    def beforeChild(self, node, child, child_idx):
        if type(child) in ReplaceTemplateExpression.template_types:
            return False, self.substituter(child, *self.substituter_args)
        return super().beforeChild(node, child, child_idx)

def substitute_template_expression(expr, substituter, *args, **kwargs):
    """Substitute IndexTemplates in an expression tree.

    This is a general utility function for walking the expression tree
    and subtituting all occurances of IndexTemplate and
    _GetItemExpression nodes.

    Args:
        substituter: method taking (expression, *args) and returning
           the new object
        *args: these are passed directly to the substituter

    Returns:
        a new expression tree with all substitutions done
    """
    visitor = ReplaceTemplateExpression(substituter, *args, **kwargs)
    return visitor.walk_expression(expr)

class _GetItemIndexer(object):
    # Note that this class makes the assumption that only one template
    # ever appears in an expression for a single index

    def __init__(self, expr):
        self._base = expr.arg(0)
        self._args = []
        _hash = [ id(self._base) ]
        for x in expr.args[1:]:
            try:
                logging.disable(logging.CRITICAL)
                val = value(x)
                self._args.append(val)
                _hash.append(val)
            except TemplateExpressionError as e:
                if x is not e.template:
                    raise TypeError(
                        "Cannot use the param substituter with expression "
                        "templates\nwhere the component index has the "
                        "IndexTemplate in an expression.\n\tFound in %s"
                        % ( expr, ))
                self._args.append(e.template)
                _hash.append(id(e.template._set))
            finally:
                logging.disable(logging.NOTSET)

        self._hash = tuple(_hash)

    def nargs(self):
        return len(self._args)

    def arg(self, i):
        return self._args[i]

    @property
    def base(self):
        return self._base

    @property
    def args(self):
        return self._args

    def __hash__(self):
        return hash(self._hash)

    def __eq__(self, other):
        if type(other) is _GetItemIndexer:
            return self._hash == other._hash
        else:
            return False

    def __str__(self):
        return "%s[%s]" % (
            self._base.name, ','.join(str(x) for x in self._args) )

def substitute_getitem_with_param(expr, _map):
    """A simple substituter to replace _GetItem nodes with mutable Params.

    This substituter will replace all _GetItemExpression nodes with a
    new Param.  For example, this method will create expressions
    suitable for passing to DAE integrators
    """
    import pyomo.core.base.param
    if type(expr) is IndexTemplate:
        return expr

    _id = _GetItemIndexer(expr)
    if _id not in _map:
        _map[_id] = pyomo.core.base.param.Param(mutable=True)
        _map[_id].construct()
        _map[_id]._name = "%s[%s]" % (
            _id.base.name, ','.join(str(x) for x in _id.args) )
    return _map[_id]

def substitute_template_with_value(expr):
    """A simple substituter to expand expression for current template

    This substituter will replace all _GetItemExpression / IndexTemplate
    nodes with the actual _ComponentData based on the current value of
    the IndexTemplate(s)

    """

    if type(expr) is IndexTemplate:
        return as_numeric(expr())
    else:
        return resolve_template(expr)

class _set_iterator_template_generator(object):
    """Replacement iterator that returns IndexTemplates

    In order to generate template expressions, we hijack the normal Set
    iteration mechanisms so that this iterator is returned instead of
    the usual iterator.  This iterator will return IndexTemplate
    object(s) instead of the actual Set items the first time next() is
    called.
    """
    def __init__(self, _set, context):
        self._set = _set
        self.context = context

    def __iter__(self):
        return self

    def __next__(self):
        # Prevent context from ever being called more than once
        if self.context is None:
            raise StopIteration()
        context, self.context = self.context, None

        _set = self._set
        d = _set.dimen
        if d is None or type(d) is not int:
            idx = (IndexTemplate(_set, None, context.next_id()),)
        else:
            idx = tuple(
                IndexTemplate(_set, i, context.next_id()) for i in range(d)
            )
        context.cache.append(idx)
        if len(idx) == 1:
            return idx[0]
        else:
            return idx

    next = __next__

class _template_iter_context(object):
    """Manage the iteration context when generating templatized rules

    This class manages the context tracking when generating templatized
    rules.  It has two methods (`sum_template` and `get_iter`) that
    replace standard functions / methods (`sum` and
    :py:meth:`_FiniteSetMixin.__iter__`, respectively).  It also tracks
    unique identifiers for IndexTemplate objects and their groupings
    within `sum()` generators.
    """
    def __init__(self):
        self.cache = []
        self._id = 0

    def get_iter(self, _set):
        return _set_iterator_template_generator(_set, self)

    def npop_cache(self, n):
        result = self.cache[-n:]
        self.cache[-n:] = []
        return result

    def next_id(self):
        self._id += 1
        return self._id

    def sum_template(self, generator):
        init_cache = len(self.cache)
        expr = next(generator)
        final_cache = len(self.cache)
        return TemplateSumExpression(
            (expr,), self.npop_cache(final_cache-init_cache)
        )

def templatize_rule(block, rule, index_set):
    import pyomo.core.base.set
    context = _template_iter_context()
    internal_error = None
    _old_iters = (
            pyomo.core.base.set._FiniteSetMixin.__iter__,
            GetItemExpression.__iter__,
            GetAttrExpression.__iter__,
        )
    _old_sum = builtins.sum
    try:
        # Override Set iteration to return IndexTemplates
        pyomo.core.base.set._FiniteSetMixin.__iter__ \
            = GetItemExpression.__iter__ \
            = GetAttrExpression.__iter__ \
            = lambda x: context.get_iter(x).__iter__()
        # Override sum with our sum
        builtins.sum = context.sum_template
        # Get the index templates needed for calling the rule
        if index_set is not None:
            # Note, do not rely on the __iter__ overload, as non-finite
            # Sets don't have an __iter__.
            indices = next(iter(context.get_iter(index_set)))
            try:
                context.cache.pop()
            except IndexError:
                assert indices is None
                indices = ()
        else:
            indices = ()
        if type(indices) is not tuple:
            indices = (indices,)
        # Call the rule, returning the template expression and the
        # top-level IndexTemplate(s) generated when calling the rule.
        #
        # TBD: Should this just return a "FORALL()" expression node that
        # behaves similarly to the GetItemExpression node?
        return rule(block, indices), indices
    except:
        internal_error = sys.exc_info()
        raise
    finally:
        pyomo.core.base.set._FiniteSetMixin.__iter__, \
            GetItemExpression.__iter__, \
            GetAttrExpression.__iter__ = _old_iters
        builtins.sum = _old_sum
        if len(context.cache):
            if internal_error is not None:
                logger.error("The following exception was raised when "
                             "templatizing the rule '%s':\n\t%s"
                             % (rule.__name__, internal_error[1]))
            raise TemplateExpressionError(
                None,
                "Explicit iteration (for loops) over Sets is not supported "
                "by template expressions.  Encountered loop over %s"
                % (context.cache[-1][0]._set,))
    return None, indices

def templatize_constraint(con):
    return templatize_rule(con.parent_block(), con.rule, con.index_set())


alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

cache_names = {}

def get_multi_alphabet_sum(path: str) -> int:
    """Прочитывает информацию из файла. Преобраует информацию в список и сортирует её.
    Затем находит все алфавитные суммы. Суммирует полученные значения из списка"""
    text = __get_content(path)
    names = text.replace('"', '').split(',')
    names.sort()
    multi_alphabet_sums = [(i + 1) * __calculate_alphabet_sum(name) for i, name in enumerate(names)]
    sum_multi_sums = sum(multi_alphabet_sums)
    return sum_multi_sums

def __calculate_alphabet_sum(name: str) -> int:
    """Проверяет в словаре cache_names наличие данных по ключу name,
     если нет, то вычисляет алфавитную сумму и записывает её в cache_names."""
    letter_places = []
    if name in cache_names.keys():
        alphabet_sum = cache_names[name]
    else:
        for letter in name:
            place = alphabet.index(letter) + 1
            letter_places.append(place)
        alphabet_sum = sum(letter_places)
        cache_names[name] = alphabet_sum
    return alphabet_sum

def __get_content(path) -> str:
    """Читает информацию из локального файла"""
    with open(path, 'r') as file:
        text = file.read()
    return text


import time

import torch, torch.nn as nn

from vae import MultVAE
from dae import MultDAE

class ADV(nn.Module):
    def __init__(self, n_item, arg, device):
        super(ADV, self).__init__()

        self.n_item = n_item
        self.arg = arg
        self.device = device

        # self.G_add = MacridVAE(arg, n_item, device, aug=True).to(device)
        # self.G_del = MacridVAE(arg, n_item, device, aug=True).to(device)
        if arg.model == 'multvae':
            self.G = MultVAE(arg, n_item, device, aug=True).to(device)
            self.D = MultVAE(arg, n_item, device).to(device)
        elif arg.model == 'multdae':
            self.G = MultDAE(arg, n_item, device, aug=True).to(device)
            self.D = MultDAE(arg, n_item, device).to(device)

    def forward(self, x, anneal, opt, opt_aug, is_train=False):
        # Optimize augmenter
        self.G.train()
        self.G.zero_grad()
        self.D.eval()
        mi_aug = torch.zeros(1).to(self.device)
        aug = self.G(x, x, is_train=is_train)
        gumbel = self.GumbelMax(aug)
        # gumbel = aug

        aug_graph = torch.zeros_like(x).to(self.device)
        aug_graph[x == 0] = (1 - (1 - x) * gumbel)[x == 0]
        aug_graph[x == 1] = (x * gumbel)[x == 1]

        if self.arg.model == 'multvae':
            _, recon, kl, z_aug = self.D(aug_graph, x, is_train=is_train)
        elif self.arg.model == 'multdae':
            _, recon, z_aug = self.D(aug_graph, x, is_train=is_train)

        add = torch.norm(aug_graph[x == 0], p=1) / (x == 0).sum()
        drop = torch.norm(1 - aug_graph[x == 1], p=1) / (x == 1).sum()
        reg_aug = add + drop
        aug_loss = -recon + self.arg.rg_aug * reg_aug

        aug_loss.backward()
        opt_aug.step()

        self.D.train()
        self.D.zero_grad()
        self.G.eval()
        mi_rec = torch.zeros(1).to(self.device)
        aug = self.G(x, x, is_train=is_train)
        gumbel = self.GumbelMax(aug)
        # gumbel = aug

        aug_graph = torch.zeros_like(x).to(self.device)
        aug_graph[x == 0] = (1 - (1 - x) * gumbel)[x == 0]
        aug_graph[x == 1] = (x * gumbel)[x == 1]

        if self.arg.model == 'multvae':
            _, recon, kl, z_aug = self.D(aug_graph, x, is_train=is_train)
            rec_loss = recon + anneal * kl
        elif self.arg.model == 'multdae':
            _, recon, z_aug = self.D(aug_graph, x, is_train=is_train)
            rec_loss = recon
            kl = torch.zeros(1).to(self.device)

        rec_loss.backward()
        opt.step()

        return rec_loss, aug_loss, mi_rec, mi_aug, recon, kl, reg_aug, drop, add# torch.Tensor([0.]), reg_aug

    def GumbelMax(self, prob):
        bias = 0.0001
        delta = ((bias - (1 - bias)) * torch.rand(prob.size()) + (1 - bias)).to(self.device)
        p_k = torch.log(delta) - torch.log(1 - delta) + prob
        p_k = torch.sigmoid(p_k / self.arg.tau_aug)
        return p_k


with open('day13_data.txt') as f:
	data = [line.strip('\n') for line in f.readlines()]

buses = data[1]

def checkNum(t, buses):

	for increase, bus in enumerate(buses):
		if bus == "x":
			continue
		else:
			if (t + increase) % int(bus) != 0:
				return False
	return True

def findIncrement(index, buses):
	inc = 1
	for bus in buses:
		if bus != "x":
			inc *= int(bus)
	return inc

t = 1
increment = 1
for index, bus in enumerate(buses.split(",")):
	while not checkNum(t, buses.split(",")[0:index + 1]):
		t += increment
	increment = findIncrement(index, buses.split(",")[0:index + 1])
print(t)


"""
This file is a thin wrapper around the C++ library
For documentation of inner workings goto https://github.com/ChrisFoster4/mem-grep

"""
import mgpybind
from dataclasses import dataclass
import ctypes
import logging
import sys

RemoteHeapPointer = mgpybind.RemoteHeapPointer;

class SystemConfigurationError(Exception):
    pass

class MustContain:
    SUPPORTED_DATA_TYPES = [ctypes.c_uint32, ctypes.c_uint64, ctypes.c_double, ctypes.c_float, str]
    def __init__(self, data_type, value):
        if type(data_type) not in self.SUPPORTED_DATA_TYPES:
            raise ValueError(f"Unsupported data type. Must be one of: {self.SUPPORTED_DATA_TYPES}")

        self.value=value
        self.data_type = data_type

class FilterCriteria:
    def __init__(self, minimum_children=None, maximum_children=None, minimum_size=0, maximum_size=100000, maximum_decendants=None, minimum_decendants=None, must_contain:[MustContain]=None):
        pass

# Check that the system is configured to allow us access to other processes memory (or we are root)
def can_run() -> bool:
    return mgpybind._CanRun()

def search(pid: int, search_bss:bool = True, search_stack: bool = True):
    if can_run() is False:
        raise SystemConfigurationError()

    if pid < 1:
        raise ValueError(f"pid must be a positive integer")

    if search_bss is False and search_stack is False:
        raise ValueError(f"Both search_bss and search_stack cannot be False")

    if can_run() is False:
        raise SystemConfigurationError()

    return mgpybind._search(pid,search_bss,search_stack)

def traverse(pid: int, base_pointers:[RemoteHeapPointer], filter=None)->[[list]]:
    if can_run() is False:
        raise SystemConfigurationError()

    if not base_pointers:
        logging.error(f"{__name__} asked to traverse an empty list")
        return []

    return mgpybind._traverse(pid, base_pointers);

def filter(pid: int, pointers:[RemoteHeapPointer], filter: FilterCriteria):
    raise NotImplementedError

# Fetch a copy of an object from the process
def fetch_object(pid: int, pointer:RemoteHeapPointer) -> bytes:

    if can_run() is False:
        raise SystemConfigurationError()

    if pid < 1:
        raise ValueError(f"pid must be a positive integer")

    view: memoryview = mgpybind._FetchObject(pid, pointer)
    return view.tobytes()

class Substitution:
    def __init__(self, data_type, current_value, new_value):
        raise NotImplementedError

# Change values specified by the substitutions in a provided object
def substitute(pid: int, target_object:RemoteHeapPointer, substitutions=[Substitution]):
    raise NotImplementedError


class FeistelNetwork():
    '''
    Class that implements Feistel network.
    Note: It doesn't add any whitening or pre/post processing, this is just pure feistel.
    '''

    def __init__(self, keys, f_func, rounds=16, block_size=8):
        self.keys = keys
        self.round_count = rounds
        self.block_size = block_size
        self.f_func = f_func

    def _xor(self, a, b):
        a_i = int.from_bytes(a, "little")
        b_i = int.from_bytes(b, "little")
        return (a_i ^ b_i).to_bytes(len(a), "little")

    def _reverse(self, block):
        L = block[:self.block_size//2]
        R = block[self.block_size//2:]
        return R + L

    def round(self, block, round_key):
        L = block[:self.block_size//2]
        R = block[self.block_size//2:]

        R_prime = self.f_func(R, round_key)
        new_L = self._xor(L, R_prime)
        return R + new_L

    def feistel_encrypt_block(self, block):
        for i in range(self.round_count):
            block = self.round(block, self.keys[i])

        block = self._reverse(block)
        return block

    def feistel_decrypt_block(self, block):
        for i in range(self.round_count-1, -1, -1):
            block = self.round(block, self.keys[i])

        block = self._reverse(block)
        return block


import sys
import os

def Pow(li: list, power):
    re = []

    for item in li:
        re.append(pow(item, power))

    return re

def Half(li: list, right = False):
    if right: return [li[i] for i in range(len(li)) if i >= int(len(li) / 2)]
    else: return [li[i] for i in range(len(li)) if i <= int(len(li) / 2)]


import glob
import os

def kitti_parser(root_path: str):
    """
    Returns a list of absolute image paths contained in the KITTI dataset.

    Ref: http://www.cvlibs.net/datasets/kitti/raw_data.php

    Reads the first of each RGB stereo image pair.

    Dataset structure (top level corrspond to 'root directory'):
    kitti/
        2011_09_26/
            2011_09_26_drive_0002_sync/
                image_00/ <-- Grayscale stereo 1/2
                    data/
                        .png
                image_01/ <-- Grayscale stereo 2/2
                    data/
                        .png
                image_02/ <-- RGB stereo 1/2
                    data/
                        .png
                image_03/ <-- RGB stereo 2/2
                    data/
                        .png
                oxts/
                velodyne_points/
            2011_09_26_drive_0005_sync/
            ...
        2011_09_28/
            ...
        2011_09_29/
            ...
        2011_09_30/
            ...
        2011_10_03/
            ...
        object_2d/
            testing/
                image_2/
                    .png
                prev_2/
                    .png
            training/
                image_2/
                    .png
                prev_2/
                    .png

    Args:
        root_path (str): Root path of dataset.

    Returns:
        List of strings corresponding to absolute image paths.
    """
    dataset_img_paths = []

    # Parse 'raw data' directories
    if os.path.isdir(os.path.join(root_path, '2011_09_26')):
        img_paths = glob.glob(f"{root_path}/2011_09_26/*/image_02/data/*.png")
        dataset_img_paths += img_paths
    if os.path.isdir(os.path.join(root_path, '2011_09_28')):
        img_paths = glob.glob(f"{root_path}/2011_09_28/*/image_02/data/*.png")
        dataset_img_paths += img_paths
    if os.path.isdir(os.path.join(root_path, '2011_09_29')):
        img_paths = glob.glob(f"{root_path}/2011_09_29/*/image_02/data/*.png")
        dataset_img_paths += img_paths
    if os.path.isdir(os.path.join(root_path, '2011_09_30')):
        img_paths = glob.glob(f"{root_path}/2011_09_30/*/image_02/data/*.png")
        dataset_img_paths += img_paths
    if os.path.isdir(os.path.join(root_path, '2011_10_03')):
        img_paths = glob.glob(f"{root_path}/2011_10_03/*/image_02/data/*.png")
        dataset_img_paths += img_paths

    # Parse '2D object detection' directories
    if os.path.isdir(os.path.join(root_path, 'object_2d')):
        img_paths = glob.glob(f"{root_path}/object_2d/*/*/*.png")
        dataset_img_paths += img_paths

    return dataset_img_paths


# Crops the given deck.gl HeatmapLayer JSON file to the given
# geographic zone as specified by latitude & longitude bounds

import json
import os
import sys

# ------------------------------------------------------------------------------

def crop(in_path, min_lon, max_lon, min_lat, max_lat, out_path=None):

  if out_path is None:
    base, ext = os.path.splitext(in_path)
    out_path = base + "_crop.json"

  print("Cropping {} ...".format(in_path))
  fin = open(in_path)
  fout = open(out_path, "w")
  fout.write("[")
  lines_count = 0
  out_count = 0
  for line in fin:
    line = line.strip()
    lines_count += 1
    if "COORDINATES" in line:
      line = line.strip(',')
      data = json.loads(line)
      lon = float(data["COORDINATES"][0])
      lat = float(data["COORDINATES"][1])
      if min_lon <= lon <= max_lon and min_lat <= lat <= max_lat:
        if out_count > 0:
          fout.write(",")
        fout.write("\n" + line)
        out_count += 1
  fout.write("\n]")

  fin.close()
  fout.close()

  print("Wrote {:,} points to {}".format(out_count, out_path))

# ------------------------------------------------------------------------------

# Some bounding boxes:
# United States: 119.531111 73.300833 25.403611 49.818055
# Mexico: -117.12776 -86.811982388 14.5388286402 32.72083
# Australia: 113.338953078 153.569469029 -43.6345972634 -10.6681857235

if __name__ == "__main__":

  if len(sys.argv) != 6:
    print("Usage:")
    print("  python3 crop_HeatmapLayer_JSON.py <JSON file> <min longitude> <max longitude> <min latitude> <max latitude>")
    print("\nLatitude and longitude in decimal degrees, negative for west longitudes and south latitudes.")
    sys.exit()

  in_path = sys.argv[1]
  min_lon = float(sys.argv[2])
  max_lon = float(sys.argv[3])
  min_lat = float(sys.argv[4])
  max_lat = float(sys.argv[5])

  crop(in_path, min_lon, max_lon, min_lat, max_lat, out_path=None)


# Вывод таблицы умножения как на школьной тетради

def make_table():
    table = []
    for i in range(2, 10):
        temp = []
        for j in range(2, 11):
            temp.append(f'{i} x {j} = {i * j}')
        table.append(temp)
    return table

def print_table(array):
    for row in range(len(array[0])):
        for column in array:
            if column != array[-1]:
                print(column[row], end="\t")
            else:
                print(column[row])
    print("\n")

def split_table(array):
    part_1 = array[0: len(array)//2]
    part_2 = array[len(array)//2: len(array)]
    return part_1, part_2

multitable = make_table()
tabulation_size = 20
print("ТАБЛИЦА УМНОЖЕНИЯ".center(len(multitable[0])*len(multitable[0][0])//2 + tabulation_size))

part_1, part_2 = split_table(multitable)
print_table(part_1)
print_table(part_2)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 16 09:35:55 2020
You are asked to ensure that the first and last names of people begin with a capital letter in their passports.
@author: devyanshitiwari
"""

# Complete the solve function below.
def solve(s):
    s=s.capitalize()
    for i in s[:].split():
        s=s.replace(i,i.capitalize())
    return s

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = raw_input()

    result = solve(s)

    fptr.write(result + '\n')

    fptr.close()


# urlimport.py

import sys
import importlib.abc
import imp
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
from html.parser import HTMLParser

# Debugowanie
import logging
log = logging.getLogger(__name__)

# Pobieranie odnośników z danego adresu URL
def _get_links(url):
    class LinkParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            if tag == 'a':
                attrs = dict(attrs)
                links.add(attrs.get('href').rstrip('/'))

    links = set()
    try:
        log.debug('Pobieranie odnośników z %s' % url)
        u = urlopen(url)
        parser = LinkParser()
        parser.feed(u.read().decode('utf-8'))
    except Exception as e:
        log.debug('Nie można pobrać odnośników. %s', e)
    log.debug('Odnośniki: %r', links)
    return links

class UrlMetaFinder(importlib.abc.MetaPathFinder):
    def __init__(self, baseurl):
        self._baseurl = baseurl
        self._links   = { }
        self._loaders = { baseurl : UrlModuleLoader(baseurl) }

    def find_module(self, fullname, path=None):
        log.debug('find_module: fullname=%r, path=%r', fullname, path)
        if path is None:
            baseurl = self._baseurl
        else:
            if not path[0].startswith(self._baseurl):
                return None
            baseurl = path[0]

        parts = fullname.split('.')
        basename = parts[-1]
        log.debug('find_module: baseurl=%r, basename=%r', baseurl, basename)

        # Sprawdzanie pamięci podręcznej z odnośnikami
        if basename not in self._links:
            self._links[baseurl] = _get_links(baseurl)

        # Sprawdzanie, czy odnośnik prowadzi do pakietu
        if basename in self._links[baseurl]:
            log.debug('find_module: sprawdzanie pakietu %r', fullname)
            fullurl = self._baseurl + '/' + basename
            # Próba wczytania pakietu (powoduje dostęp do __init__.py)
            loader = UrlPackageLoader(fullurl)
            try:
                loader.load_module(fullname)
                self._links[fullurl] = _get_links(fullurl)
                self._loaders[fullurl] = UrlModuleLoader(fullurl)
                log.debug('find_module: wczytano pakiet %r', fullname)
            except ImportError as e:
                log.debug('find_module: nieudany import pakietu. %s', e)
                loader = None
            return loader

        # Zwykły moduł
        filename = basename + '.py'
        if filename in self._links[baseurl]:
            log.debug('find_module: znaleziono moduł %r', fullname)
            return self._loaders[baseurl]
        else:
            log.debug('find_module: nie znaleziono modułu %r', fullname)
            return None

    def invalidate_caches(self):
        log.debug('Unieważnianie pamięci podręcznej z odnośnikami')
        self._links.clear()

# Mechanizm wczytywania modułów na podstawie adresów URL
class UrlModuleLoader(importlib.abc.SourceLoader):
    def __init__(self, baseurl):
        self._baseurl = baseurl
        self._source_cache = {}

    def module_repr(self, module):
        return '<urlmodule %r from %r>' % (module.__name__, module.__file__)

    # Wymagana metoda
    def load_module(self, fullname):
        code = self.get_code(fullname)
        mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
        mod.__file__ = self.get_filename(fullname)
        mod.__loader__ = self
        mod.__package__ = fullname.rpartition('.')[0]
        exec(code, mod.__dict__)
        return mod

    # Opcjonalne rozszerzenia
    def get_code(self, fullname):
        src = self.get_source(fullname)
        return compile(src, self.get_filename(fullname), 'exec')

    def get_data(self, path):
        pass

    def get_filename(self, fullname):
        return self._baseurl + '/' + fullname.split('.')[-1] + '.py'

    def get_source(self, fullname):
        filename = self.get_filename(fullname)
        log.debug('loader: odczyt %r', filename)
        if filename in self._source_cache:
            log.debug('loader: zapisano %r w pamięci podręcznej', filename)
            return self._source_cache[filename]
        try:
            u = urlopen(filename)
            source = u.read().decode('utf-8')
            log.debug('loader: wczytano %r', filename)
            self._source_cache[filename] = source
            return source
        except (HTTPError, URLError) as e:
            log.debug('loader: nieudane wczytywanie %r.  %s', filename, e)
            raise ImportError("Nie można wczytać %s" % filename)

    def is_package(self, fullname):
        return False

# Mechanizm wczytywania pakietów na podstawie adresów URL
class UrlPackageLoader(UrlModuleLoader):
    def load_module(self, fullname):
        mod = super().load_module(fullname)
        mod.__path__ = [ self._baseurl ]
        mod.__package__ = fullname

    def get_filename(self, fullname):
        return self._baseurl + '/' + '__init__.py'

    def is_package(self, fullname):
        return True

# Funkcje narzędziowe do instalowania i usuwania mechanizmu wczytywania
_installed_meta_cache = { }
def install_meta(address):
    if address not in _installed_meta_cache:
        finder = UrlMetaFinder(address)
        _installed_meta_cache[address] = finder
        sys.meta_path.append(finder)
        log.debug('%r zainstalowano w sys.meta_path', finder)

def remove_meta(address):
    if address in _installed_meta_cache:
        finder = _installed_meta_cache.pop(address)
        sys.meta_path.remove(finder)
        log.debug('%r usunięto z sys.meta_path', finder)

# Klasa do znajdowania ścieżek na podstawie adresu URL
class UrlPathFinder(importlib.abc.PathEntryFinder):
    def __init__(self, baseurl):
        self._links = None
        self._loader = UrlModuleLoader(baseurl)
        self._baseurl = baseurl

    def find_loader(self, fullname):
        log.debug('find_loader: %r', fullname)
        parts = fullname.split('.')
        basename = parts[-1]
        # Sprawdzanie pamięci podręcznej z odnośnikami
        if self._links is None:
            self._links = []     # Zobacz omówienie
            self._links = _get_links(self._baseurl)

        # Sprawdzanie, czy odnośnik prowadzi do pakietu
        if basename in self._links:
            log.debug('find_loader: sprawdzanie pakietu %r', fullname)
            fullurl = self._baseurl + '/' + basename
            # Próba wczytania pakietu (prowadzi do dostępu do __init__.py)
            loader = UrlPackageLoader(fullurl)
            try:
                loader.load_module(fullname)
                log.debug('find_loader: wczytano pakiet %r', fullname)
            except ImportError as e:
                log.debug('find_loader: %r to pakiet oparty na przestrzeni nazw', fullname)
                loader = None
            return (loader, [fullurl])

        # Zwykły moduł
        filename = basename + '.py'
        if filename in self._links:
            log.debug('find_loader: znaleziono moduł %r', fullname)
            return (self._loader, [])
        else:
            log.debug('find_loader: nie znaleziono modułu %r', fullname)
            return (None, [])

    def invalidate_caches(self):
        log.debug('Unieważnianie pamięci podręcznej z odnośnikami')
        self._links = None

# Sprawdzanie, czy ścieżka wygląda jak adres URL
_url_path_cache = {}
def handle_url(path):
    if path.startswith(('http://', 'https://')):
        log.debug('Obsługa ścieżki? %s. [Tak]', path)
        if path in _url_path_cache:
            finder = _url_path_cache[path]
        else:
            finder = UrlPathFinder(path)
            _url_path_cache[path] = finder
        return finder
    else:
        log.debug('Obsługa ścieżki? %s. [Nie]', path)

def install_path_hook():
    sys.path_hooks.append(handle_url)
    sys.path_importer_cache.clear()
    log.debug('Instalowanie handle_url')

def remove_path_hook():
    sys.path_hooks.remove(handle_url)
    sys.path_importer_cache.clear()
    log.debug('Usuwanie handle_url')


import os
import subprocess as sp
import multiprocessing
import re

omitlist = []

def flake8_file(fpath):
    '''
    see all messages in 1 file
    '''
    file = sp.getoutput('flake8 ' + fpath)
    filelist = file.strip().split('\n')
    filelist = list(filter(None, filelist))
    filelist = [x for x in filelist if x not in omitlist]
    return filelist

def extract_details(entry):
    '''
    fetch information from flake8 message string
    '''
    entrysplit = entry.split(':')
    path = entrysplit[0]
    row = int(entrysplit[1])-1
    col = int(entrysplit[2])-1
    message = ''.join(entrysplit[3])
    return (path, row, col, message, entry)

def get_all_files():
    '''
    get all files that need linting
    '''
    flake8 = sp.getoutput('flake8 ' + os.getcwd())
    flakelist = flake8.strip().split('\n')
    return {x.split(':')[0] for x in flakelist}

def find_fix(message):
    '''
    look through func_fix dict, find key
    '''
    keys = list(func_fix.keys())
    for key in keys:
        if key in message:
            return key
    return None

def delete_line(bundle):
    '''
    delete line of mentioned row
    '''
    print('deleting line')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            if index != row:
                f.write(line)

def delete_blank_line(bundle):
    '''
    delete previous line of mentioned row
    '''
    print('deleting blank line')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            if index != row-1:
                f.write(line)

def insert_line(bundle):
    '''
    insert an new line at row
    '''
    print('inserting line')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            if index == row:
                f.write('\n')
            f.write(line)

def newline_EOF(bundle):
    '''
    insert new line at EOF
    '''
    print('inserting line at end')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            f.write(line)
        f.write('\n')

def insert_space_before(bundle):
    '''
    insert space at mentioned col
    '''
    print('inserting space')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            if index == row:
                newline = line[:col] + ' ' + line[col:]
                f.write(newline)
            else:
                f.write(line)

def insert_space_after(bundle):
    '''
    insert space after mentioned col
    '''
    print('inserting space')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            if index == row:
                newline = line[:col+1] + ' ' + line[col+1:]
                f.write(newline)
            else:
                f.write(line)

def convert_tabs_to_spaces(bundle):
    '''
    convert all tabs to 4 spaces
    '''
    print('converting tabs to spaces')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            line = re.sub('\t', '    ', line)
            f.write(line)

def remove_semicolon(bundle):
    '''
    remove semicolons
    '''
    print('deleting semicolon')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            if index == row:
                line = line.replace(';', '')
            f.write(line)

def delete_character(bundle):
    '''
    delete a character at row, col
    '''
    print('deleting character')
    lines, details = bundle
    path, row, col, message, entry = details
    with open(path, 'w') as f:
        for index, line in enumerate(lines):
            if index == row:
                line = line[:col] + line[col+1:]
            f.write(line)

def delete_unused_import(bundle):
    '''
    delete a character at row, col
    '''
    print('deleting unused import')
    global omitlist
    lines, details = bundle
    path, row, col, message, entry = details
    string_to_remove = message.split("'")[1].split('.')[-1]
    if string_to_remove in lines[row]:
        with open(path, 'w') as f:
            for index, line in enumerate(lines):
                if index == row:
                    spl = line.split('import')
                    imports = spl[-1].split(',')
                    imports = [x.strip() for x in imports]
                    imports.remove(string_to_remove)
                    if len(imports):
                        new_line = spl[0] + ' import ' + ' ,'.join(imports) + '\n'
                        f.write(new_line)
                else:
                    f.write(line)
    else:
        print("Manually fix", entry)
        omitlist.append(entry)

func_fix = {
    'E201': delete_character,
    'E202': delete_character,
    'E203': delete_character,
    'E211': delete_character,
    'E221': delete_character,
    'E222': delete_character,
    'E225': insert_space_before,
    'E231': insert_space_after,
    'E251': delete_character,
    'E252': insert_space_before,
    'E261': insert_space_before,
    'E262': insert_space_after,
    'E265': insert_space_after,
    'E266': delete_character,
    'E272': delete_character,
    'E302': insert_line,
    'E303': delete_blank_line,
    'E305': insert_line,
    'E703': remove_semicolon,
    'F401': delete_unused_import,
    'W191': convert_tabs_to_spaces,
    'W291': delete_character,
    'W292': newline_EOF,
    'W293': delete_character,
    'W391': delete_line,
}

def solution_selector(full_details):
    '''
    selects the solution to deal out
    '''
    global omitlist
    path, row, col, message, entry = full_details
    # details = (path, row, col, message)
    with open(path, 'r') as f:
        lines = f.readlines()
        key = find_fix(message)
        if key is None:
            print("Manually fix", entry)
            omitlist.append(entry)
        else:
            print(key, entry)
            bundle = (lines, full_details)
            func_fix[key](bundle)

def fix_a_file(file):
    resolved = False
    while not resolved:
        file_errors = flake8_file(file)
        if(len(file_errors) == 0):
            resolved = True
        else:
            details = extract_details(file_errors[0])
            solution_selector(details)

files = get_all_files()
p = multiprocessing.Pool(multiprocessing.cpu_count())
p.map(fix_a_file, files)


import numpy as np
import matplotlib.pyplot as plt

wavelet_data = np.load("wavelet_data_beta.npy",allow_pickle=True)

all_rmd=list()
# only for maximum participant, maxiumum intensity
#calculate RMD
# maximum element for alpha is element 34 which is particpant 4 intensity 0.4
# maximum element for beta is element 16 which is particpant 2 intensity 0.7

#specific_file = wavelet_data[34]
p_coffs = list()
for window in range(500):
    for i in range(wavelet_data[16].shape[1]-1):
        for j in range(i+1,wavelet_data[16].shape[1]):
            l = np.linalg.norm(wavelet_data[16][window:window+15,i]-wavelet_data[16][window:window+15,j])
            #forget about the comparison
            #maybe we keep the values and normalize
            p_coffs.append(l)
    all_rmd.append(np.average(p_coffs))

print(len(all_rmd))

x = np.linspace(0,500,500)
plt.plot(x,all_rmd,'r')
plt.xlabel('numbet of windows')
plt.ylabel('values')
plt.show()


import os
import keras
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
import sis_utils
import uabRepoPaths
import uabCrossValMaker
import uab_collectionFunctions
import uab_DataHandlerFunctions
import uabUtilreader
import util_functions
from bohaoCustom import uabDataReader

class ImageLabelReaderBuilding(uabDataReader.ImageLabelReader):
    def __init__(self, gtInds, dataInds, parentDir, chipFiles, chip_size, batchSize, center_crop, patch_prob,
                 nChannels=1, padding=np.array((0, 0)), block_mean=None, dataAug=''):
        self.patch_prob = patch_prob
        self.center_crop = center_crop
        super(ImageLabelReaderBuilding, self).__init__(gtInds, dataInds, parentDir,
                                                       chipFiles,
                                                       chip_size,
                                                       batchSize, nChannels, padding,
                                                       block_mean, dataAug)

    def readFromDiskIteratorTrain(self, image_dir, chipFiles, batch_size, patch_size, padding=(0, 0), dataAug=''):
        # this is a iterator for training
        nDims = len(chipFiles[0])
        image_batch = np.zeros((batch_size, self.center_crop[0], self.center_crop[1], nDims))
        building_truth = np.zeros((batch_size, 2))
        # select number to sample
        idx_batch = np.random.permutation(len(chipFiles))
        for cnt, randInd in enumerate(idx_batch):
            row = chipFiles[randInd]

            blockList = []
            nDims = 0
            for file in row:
                img = util_functions.uabUtilAllTypeLoad(os.path.join(image_dir, file))
                if len(img.shape) == 2:
                    img = np.expand_dims(img, axis=2)
                nDims += img.shape[2]
                blockList.append(img)
            block = np.dstack(blockList).astype(np.float32)

            if self.block_mean is not None:
                block -= self.block_mean

            if dataAug != '':
                augDat = uabUtilreader.doDataAug(block, nDims, dataAug, is_np=True,
                                                 img_mean=self.block_mean)
            else:
                augDat = block

            if (np.array(padding) > 0).any():
                augDat = uabUtilreader.pad_block(augDat, padding)

            augDat = util_functions.crop_center(augDat, self.center_crop[0], self.center_crop[1])

            store_idx = cnt % batch_size
            image_batch[store_idx, :, :, :] = augDat
            percent = np.sum(augDat[:, :, 0]) / (self.center_crop[0] * self.center_crop[1])

            if percent > self.patch_prob:
                building_truth[store_idx, :] = [0, 1]
            else:
                building_truth[store_idx, :] = [1, 0]

            if (cnt + 1) % batch_size == 0:
                yield (image_batch[:, :, :, 1:], building_truth)

if __name__ == '__main__':
    city_list = ['austin', 'chicago', 'kitsap', 'tyrol-w', 'vienna']
    model_name = 'unet'
    batch_size = 100
    prescr_name = 'res50'
    blCol = uab_collectionFunctions.uabCollection('inria')
    img_mean = blCol.getChannelMeans([0, 1, 2])
    img_dir, task_dir = sis_utils.get_task_img_folder()
    force_run = False

    if force_run:
        if prescr_name == 'incep':
            center_crop = (299, 299)
        elif prescr_name == 'res50':
            center_crop = (224, 224)
        else:
            raise KeyError

        truth_building = []
        pred_building = []
        pred_building_binary = []
        for city_num in range(5):
            model_save_dir = os.path.join(task_dir, '{}_building_loo_{}.hdf5'.format(prescr_name, city_num))
            model = keras.models.load_model(model_save_dir)

            if model_name == 'unet':
                patch_size = (572, 572)
                overlap = 184
                pad = 92
            else:
                patch_size = (321, 321)
                overlap = 0
                pad = 0

            # extract patches
            extrObj = uab_DataHandlerFunctions.uabPatchExtr([0, 1, 2, 4],
                                                            cSize=patch_size,
                                                            numPixOverlap=overlap,
                                                            extSave=['jpg', 'jpg', 'jpg', 'png'],
                                                            isTrain=True,
                                                            gtInd=3,
                                                            pad=pad)
            patchDir = extrObj.run(blCol)
            chipFiles = os.path.join(patchDir, 'fileList.txt')
            idx, file_list = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt', 'city')
            idx2, _ = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt', 'force_tile')
            idx3 = [j * 10 + i for i, j in zip(idx, idx2)]
            filter_train = []
            filter_valid = []
            for i in range(5):
                for j in range(1, 37):
                    if i == city_num and j <= 5:
                        filter_valid.append(j * 10 + i)
                    elif i != city_num:
                        filter_train.append(j * 10 + i)
            file_list_valid = uabCrossValMaker.make_file_list_by_key(idx3, file_list, filter_valid)

            dataReader_valid = ImageLabelReaderBuilding(
                [3], [0, 1, 2], patchDir, file_list_valid, patch_size, batch_size, center_crop, 0.1,
                block_mean=np.append([0], img_mean)).readManager

            for img, gt in dataReader_valid:
                pred = model.predict_on_batch(img)
                truth_building.append(np.argmax(gt, axis=1))
                pred_building.append(pred[:, 1])
                pred_building_binary.append(np.argmax(pred, axis=1))
        truth_building = [item for sublist in truth_building for item in sublist]
        pred_building = [item for sublist in pred_building for item in sublist]
        pred_building_binary = [item for sublist in pred_building_binary for item in sublist]
        np.save(os.path.join(task_dir, '{}_truth_building.npy'.format(prescr_name)), truth_building)
        np.save(os.path.join(task_dir, '{}_pred_building.npy'.format(prescr_name)), pred_building)
        np.save(os.path.join(task_dir, '{}_pred_building_binary.npy'.format(prescr_name)), pred_building_binary)
    else:
        res50_truth_building = np.load(os.path.join(task_dir, 'res50_truth_building.npy'))
        res50_pred_building = np.load(os.path.join(task_dir, 'res50_pred_building.npy'))
        res50_pred_building_binary = np.load(os.path.join(task_dir, 'res50_pred_building_binary.npy'))

        incep_truth_building = np.load(os.path.join(task_dir, 'incep_truth_building.npy'))
        incep_pred_building = np.load(os.path.join(task_dir, 'incep_pred_building.npy'))
        incep_pred_building_binary = np.load(os.path.join(task_dir, 'incep_pred_building_binary.npy'))

        plt.figure()
        colors = util_functions.get_default_colors()

        fpr_rf, tpr_rf, _ = roc_curve(res50_truth_building, res50_pred_building)
        plt.plot(fpr_rf, tpr_rf, '--', label='Res50 AUC = {:.2f}'.format(auc(fpr_rf, tpr_rf)), color=colors[0])
        fpr_rf, tpr_rf, _ = roc_curve(incep_truth_building, incep_pred_building)
        plt.plot(fpr_rf, tpr_rf, '--', label='Incep AUC = {:.2f}'.format(auc(fpr_rf, tpr_rf)), color=colors[1])
        plt.plot([0, 1], [0, 1], color='grey', lw=2, linestyle='--')

        plt.plot([0.028234771009337484], [0.8338590956887487], color=colors[2], marker='o', label='LOO')
        fpr_rf, tpr_rf, _ = roc_curve(res50_truth_building, res50_pred_building_binary)
        plt.plot(fpr_rf[1], tpr_rf[1], label='Res50', marker='o', color=colors[0])
        fpr_rf, tpr_rf, _ = roc_curve(incep_truth_building, incep_pred_building_binary)
        plt.plot(fpr_rf[1], tpr_rf[1], label='Incep', marker='o', color=colors[1])

        plt.xlim([0.0, 1.0])
        plt.ylim([0.0, 1.05])
        plt.xlabel('False Positive Rate')
        plt.ylabel('True Positive Rate')
        plt.title('ROC Building LOO')
        plt.legend(loc="lower right")
        plt.tight_layout()
        plt.savefig(os.path.join(img_dir, 'prescr_cmp.png'))
        plt.show()


from flask import Flask, render_template, request, jsonify
import re
import pickle
import json
import numpy as np

app = Flask(__name__)

# Load the rf model using pickle
with open("final_rf_model.pkl", "rb") as file:
    loaded_rf_model = pickle.load(file)

# Load the specialized_dict from JSON
with open("disease_specialist_dict.json", "r") as file:
    loaded_specialized_dict = json.load(file)

# Load the prediction_encoder classes from JSON
with open("encoder_data.json", "r") as file:
    encoder_data = json.load(file)

with open("X.pkl", "rb") as file:
    X = pickle.load(file)

symptoms = X.columns.values

# Creating a symptom index dictionary to encode the
# input symptoms into numerical form
symptom_index = {}
for index, value in enumerate(symptoms):
    symptom = " ".join([i.capitalize() for i in value.split("_")])
    symptom_index[symptom] = index

data_dict = {
    "symptom_index": symptom_index,
    "prediction_classes": encoder_data,
}

# Function to convert string to camel case format
def to_camel_case(string):
    return re.sub(r"(?:^|_)(\w)", lambda x: x.group(1).capitalize(), string)

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        symptom1 = request.form.get("symptom1")
        symptom2 = request.form.get("symptom2")
        symptom3 = request.form.get("symptom3")

        symptoms = [to_camel_case(symptom1), to_camel_case(symptom2), to_camel_case(symptom3)]
        input_data = [0] * len(data_dict["symptom_index"])
        for symptom in symptoms:
            index = data_dict["symptom_index"].get(symptom)
            if index is not None:
                input_data[index] = 1

        input_data = np.array(input_data).reshape(1, -1)
        final_prediction = data_dict["prediction_classes"][loaded_rf_model.predict(input_data)[0]]
        specialist_info = loaded_specialized_dict.get(final_prediction)
        if specialist_info is None:
            specialist_department = "Unknown"
            severity = "Unknown"
            observed_symptoms = []
        else:
            specialist_department = specialist_info.get("department", "Unknown")
            severity = specialist_info.get("severity", "Unknown")
            observed_symptoms = specialist_info.get("observed_symptoms", [])

        response = {
            "suspected_disease": final_prediction,
            "specialist_department": specialist_department,
            "severity": severity,
            "observed_symptoms": observed_symptoms
        }

        return render_template("result.html", **response)

    return render_template("recommender_html.html")

if __name__ == "__main__":
    app.run()


# 파이썬
# 연산자 문법
# 증감 연산자 : 기존에 사용하던 증가/감소 기능을 간단하게 이용
# 파이썬은 ++, --  사용하지 않음

#한글 사용
import sys
import io
sys.stdout  = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr  = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

a = 10
a += 10 # a = a + 10
print(a)

# 관계연산자 : 두 값을 비교
# true, false로 return
# A와 B가 같은지 : A == B
# A와 B가 다른지 : A != B
# A가 B보다 큰지 : A > B
# A가 B보다 작은지 : A < B
a = 10
b = 20
print("a와 b의 값이 같음은 " ,a == b, "입니다.")
print("a가 b보다 크다는 말은 " ,a > b, "입니다.")

#문자열도 즉시 비교가 가능
a = 'banana'
b = 'apple'
print("a와 b의 값이 같음은 " ,a == b, "입니다.")
print("a가 b보다 크다는 말은 " ,a > b, "입니다.") #사전순으로 비교

#논리 연산자 : 여러개의 수식을 논리적으로 연산
# A and B : A와 B모두 참인지 판별
# A or B : A나 B 둘 중 하나라도 참이면 참
# not A : A가 true이면 false, false이면 true
a = True
b = False
print(a and b)
print(a or b)
print(not a)


class Solution:
    """
    You are given an array of words where each word consists of lowercase
    English letters.

    wordA is a predecessor of wordB if and only if we can insert exactly one
    letter anywhere in wordA without changing the order of the other characters
    to make it equal to wordB.

    - For example, "abc" is a predecessor of "abac", while "cba" is not a
      predecessor of "bcad".

    A word chain is a sequence of words [word1, word2, ..., wordk] with k >= 1,
    where word1 is a predecessor of word2, word2 is a predecessor of word3, and
    so on. A single word is trivially a word chain with k == 1.

    Return the length of the longest possible word chain with words chosen from
    the given list of words.

    Constraints:
    - 1 <= words.length <= 1000
    - 1 <= words[i].length <= 16
    - words[i] only consists of lowercase English letters.
    """

    def longestStrChain(self, words: list[str]) -> int:
        """
        O(n * n * word_length) / O(n)   time / space complexity
        """
        # maximum length of a word
        LEN_CONSTRAINT = 16

        # sort words to make iterating easier
        words.sort(key=lambda x: len(x))

        # longest_chain_len[i] = longest chain possible ending with word i
        longest_chain_len = [1] * len(words)

        # words in separate buckets by length
        words_by_len_with_idx: list[list[tuple[int, str]]] = [
            [] for _ in range(LEN_CONSTRAINT + 1)
        ]

        # put words into buckets with their index
        for i, word in enumerate(words):
            words_by_len_with_idx[len(word)].append((i, word))

        for i, word in enumerate(words):
            l = len(word)
            # get all possible predecessors of word
            for j, child_word in words_by_len_with_idx[l - 1]:
                # count character difference
                diff = 0
                for k in range(l):
                    # check how many characters are different, if more than one, break
                    if (k - diff < l - 1) and (child_word[k - diff] != word[k]):
                        diff += 1
                        if diff == 2:
                            break
                else:
                    # if 0 or 1 characters differ, update longest chain for successor word
                    chain_from_predecessor = longest_chain_len[j] + 1
                    if chain_from_predecessor > longest_chain_len[i]:
                        longest_chain_len[i] = chain_from_predecessor

        # return longest chain
        return max(longest_chain_len)


# coding=utf-8
from __future__ import absolute_import, division, print_function, \
  unicode_literals

from unittest import TestCase

import filters as f
from filters.test import BaseFilterTestCase
from six import text_type

from iota import Address, Iota, Tag, TransactionHash, TryteString
from iota.adapter import MockAdapter
from iota.commands.core.find_transactions import FindTransactionsCommand, \
  FindTransactionsRequestFilter
from iota.filters import Trytes
from pack001 import *

class FindTransactionsRequestFilterTestCase(BaseFilterTestCase):
  filter_type = FindTransactionsCommand(MockAdapter()).get_request_filter
  print_var_type_n_val(var001 = filter_type, pointer = "#ZERTsdfgrd1234jh")#ZERTsdfgrd1234jh
# Value:
# # <bound method FindTransactionsCommand.get_request_filter of <iota.commands.core.find_transactions.FindTransactionsCommand object at 0x0000021C318C8A58>>

# Type: <class 'method'>
  skip_value_check = True

  # noinspection SpellCheckingInspection
  def setUp(self):
    super(FindTransactionsRequestFilterTestCase, self).setUp()

    # Define a few valid values that we can reuse across tests.
    self.trytes1 = 'RBTC9D9DCDQAEASBYBCCKBFA'
    self.trytes2 =\
      'CCPCBDVC9DTCEAKDXC9D9DEARCWCPCBDVCTCEAHDWCTCEAKDCDFD9DSCSA'
    self.trytes3 = '999999999999999999999999999'

  def test_pass_all_parameters(self):
    """
    The request contains valid values for all parameters.
    """
    # Raw trytes are extracted to match the IRI's JSON protocol.
    request = {
      'bundles':
        [
          text_type(
            TransactionHash(self.trytes1)
            # # self.trytes1 = 'RBTC9D9DCDQAEASBYBCCKBFA'
          ),
          text_type(
            TransactionHash(self.trytes2)
          ),
        ],
      'addresses':
        [
          text_type(
            Address(self.trytes1)
          ),
          text_type(
            Address(self.trytes2)
          ), ],
      'tags':
        [
          text_type(
            Tag(self.trytes1)
          ),
          text_type(
            Tag(self.trytes3)
            # # self.trytes3 = '999999999999999999999999999'
          ), ],
      'approvees': [
        text_type(
          TransactionHash(self.trytes1)
        ),
        text_type(
          TransactionHash(
            self.trytes3)
          ),
      ],
    }
    print_var_type_n_val(var001 = request, pointer = "#XXXXCVBVCZZZzzzer12345")#XXXXCVBVCZZZzzzer12345
# Value:
# # {'bundles': ['RBTC9D9DCDQAEASBYBCCKBFA999999999999999999999999999999999999999999999999999999999', 'CCPCBDVC9DTCEAKDXC9D9DEARCWCPCBDVCTCEAHDWCTCEAKDCDFD9DSCSA99999999999999999999999'], 'addresses': ['RBTC9D9DCDQAEASBYBCCKBFA999999999999999999999999999999999999999999999999999999999', 'CCPCBDVC9DTCEAKDXC9D9DEARCWCPCBDVCTCEAHDWCTCEAKDCDFD9DSCSA99999999999999999999999'], 'tags': ['RBTC9D9DCDQAEASBYBCCKBFA999', '999999999999999999999999999'], 'approvees': ['RBTC9D9DCDQAEASBYBCCKBFA999999999999999999999999999999999999999999999999999999999', '999999999999999999999999999999999999999999999999999999999999999999999999999999999']}

# Type: <class 'dict'>
    filter_ = self._filter(request) #here001
    print_var_type_n_val(var001 = filter_, pointer = "#WXCVRDFGTFGHV")#WXCVRDFGTFGHV
# Value:
# # FindTransactionsRequestFilter(FilterChain(Type(Mapping, allow_subclass=True) | FilterMapper(addresses=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | AddressNoChecksum() | Unicode(encoding='ascii')))), approvees=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))), bundles=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))), tags=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))))))

# Type: <class 'filters.handlers.FilterRunner'>
    self.assertFilterPasses(filter_)
    self.assertDictEqual(filter_.cleaned_data, request)
      # print('filter_.cleaned_data: ', filter_.cleaned_data)
      # # {'tags': ['RBTC9D9DCDQAEASBYBCCKBFA999', '999999999999999999999999999']}

  def test_pass_compatible_types(self):
    """
    The request contains values that can be converted to the expected
    types.
    """
    filter_ = self._filter({'bundles': [self.trytes1.encode('ascii'), TransactionHash(self.trytes2), ], 'addresses': [self.trytes1.encode('ascii'), Address(self.trytes2), ], 'tags': [self.trytes1.encode('ascii'), Tag(self.trytes3), ], 'approvees': [self.trytes1.encode('ascii'), TransactionHash(self.trytes3), ], })
    print_var_type_n_val(var001 = filter_, pointer = "#WXCVBFGrtrer12349999")#WXCVBFGrtrer12349999
# Value:
# # FindTransactionsRequestFilter(FilterChain(Type(Mapping, allow_subclass=True) | FilterMapper(addresses=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | AddressNoChecksum() | Unicode(encoding='ascii')))), approvees=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))), bundles=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))), tags=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))))))

# Type: <class 'filters.handlers.FilterRunner'>
    self.assertFilterPasses(filter_)
    # to_find: where is the command above is being created
    self.assertDictEqual(
      filter_.cleaned_data,

      {
        # Raw trytes are extracted to match the IRI's JSON protocol.
        'bundles': [
          text_type(TransactionHash(self.trytes1)),
          text_type(TransactionHash(self.trytes2)),
        ],

        'addresses': [
          text_type(Address(self.trytes1)),
          text_type(Address(self.trytes2)),
        ],

        'tags': [
          text_type(Tag(self.trytes1)),
          text_type(Tag(self.trytes3)),
        ],

        'approvees': [
          text_type(TransactionHash(self.trytes1)),
          text_type(TransactionHash(self.trytes3)),
        ],
      },
    )

  def test_pass_bundles_only(self):
    """
    The request only includes bundles.
    """
    request = {'bundles': [
      TransactionHash(self.trytes1),
      TransactionHash(self.trytes2),
    ], }

    filter_ = self._filter(request)
    print_var_type_n_val(var001 = filter_, pointer = "#5555ttyhgffbbvcvbaazezZZZSSS")#5555ttyhgffbbvcvbaazezZZZSSS
# Value:
# # FindTransactionsRequestFilter(FilterChain(Type(Mapping, allow_subclass=True) | FilterMapper(addresses=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | AddressNoChecksum() | Unicode(encoding='ascii')))), approvees=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))), bundles=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))), tags=FilterChain(Array(Sequence, allow_subclass=True) | FilterRepeater(FilterChain(Required(allow_none=False) | Trytes() | Unicode(encoding='ascii')))))))

# Type: <class 'filters.handlers.FilterRunner'>

    self.assertFilterPasses(filter_)

    print_var_type_n_val(var001 = text_type(TransactionHash(self.trytes1)), pointer = "#4543ERTGF555666aaaqsCE")#4543ERTGF555666aaaqsCE
# Value: RBTC9D9DCDQAEASBYBCCKBFA999999999999999999999999999999999999999999999999999999999
# Type: <class 'str'>
    print_var_type_n_val(var001 = TransactionHash(self.trytes1), pointer = "#XCEDFGtrefg654")#XCEDFGtrefg654
# Value: RBTC9D9DCDQAEASBYBCCKBFA999999999999999999999999999999999999999999999999999999999
# Type: <class 'iota.transaction.types.TransactionHash'>
    print_var_type_n_val(var001 = self.trytes1, pointer = "#XCVDFFERRRTdd4544")#XCVDFFERRRTdd4544
# Value: RBTC9D9DCDQAEASBYBCCKBFA
# Type: <class 'str'>

    self.assertDictEqual(
      filter_.cleaned_data,

      {
        'bundles': [
          text_type(TransactionHash(self.trytes1)),
          text_type(TransactionHash(self.trytes2)),
        ],

        # Null criteria are not included in the request.
        # https://github.com/iotaledger/iota.lib.py/issues/96
        # 'addresses':  [],
        # 'approvees':  [],
        # 'tags':       [],
      },
    )

  def test_pass_addresses_only(self):
    """
    The request only includes addresses.
    """

    request = {
      'addresses': [
        Address(self.trytes1),
          # print("type(Address(self.trytes1)): ", type(Address(self.trytes1)))
          # # <class 'iota.types.Address'>
          # print('Address(self.trytes1): ', Address(self.trytes1))
          # # RBTC9D9DCDQAEASBYBCCKBFA999999999999999999999999999999999999999999999999999999999
        Address(self.trytes2),
      ],
    }

    filter_ = self._filter(request)

    self.assertFilterPasses(filter_)
    self.assertDictEqual(
      filter_.cleaned_data,

      {
        'addresses': [
          text_type(Address(self.trytes1)),
            # print("type(Address(self.trytes1)): ", type(Address(self.trytes1)))
            # # <class 'iota.types.Address'>
            # print('Address(self.trytes1): ', Address(self.trytes1))
            # # RBTC9D9DCDQAEASBYBCCKBFA999999999999999999999999999999999999999999999999999999999
          text_type(Address(self.trytes2)),
        ],

        # Null criteria are not included in the request.
        # https://github.com/iotaledger/iota.lib.py/issues/96
        # 'approvees':  [],
        # 'bundles':    [],
        # 'tags':       [],
      },
    )

  def test_pass_tags_only(self):
    """
    The request only includes tags.
    """
    # print("type(Tag(self.trytes1)): ", type(Tag(self.trytes1)))
    # # <class 'iota.types.Tag'>
    # print('Tag(self.trytes1): ', Tag(self.trytes1))
    # # RBTC9D9DCDQAEASBYBCCKBFA999
    request = {
      'tags': [
        Tag(self.trytes1),
        Tag(self.trytes3),
      ],
    }

    filter_ = self._filter(request)
      # print('filter_.cleaned_data: ', filter_.cleaned_data)
      # # {'tags': ['RBTC9D9DCDQAEASBYBCCKBFA999', '999999999999999999999999999']}

    self.assertFilterPasses(filter_)
    self.assertDictEqual(
      filter_.cleaned_data,

      {
        'tags': [
          text_type(Tag(self.trytes1)),
          text_type(Tag(self.trytes3)),
        ],

        # Null criteria are not included in the request.
        # https://github.com/iotaledger/iota.lib.py/issues/96
        # 'addresses':  [],
        # 'approvees':  [],
        # 'bundles':    [],
      },
    )

  def test_pass_approvees_only(self):
    """
    The request only includes approvees.
    """
    request = {
      'approvees': [
        TransactionHash(self.trytes1),
        TransactionHash(self.trytes3),
      ],
    }

    filter_ = self._filter(request)

    self.assertFilterPasses(filter_)
    self.assertDictEqual(
      filter_.cleaned_data,

      {
        'approvees': [
          text_type(TransactionHash(self.trytes1)),
          text_type(TransactionHash(self.trytes3)),
        ],

        # Null criteria are not included in the request.
        # https://github.com/iotaledger/iota.lib.py/issues/96
        # 'addresses':  [],
        # 'bundles':    [],
        # 'tags':       [],
      },
    )

  def test_fail_empty(self):
    """
    The request does not contain any parameters.
    """
    self.assertFilterErrors(
      {},

      {
        '': [FindTransactionsRequestFilter.CODE_NO_SEARCH_VALUES],
      },
    )

  def test_fail_all_parameters_null(self):
    """
    The request contains all parameters, but every one is null.
    """
    self.assertFilterErrors(
      {
        'addresses':  None,
        'approvees':  None,
        'bundles':    None,
        'tags':       None,
      },

      {
        '': [FindTransactionsRequestFilter.CODE_NO_SEARCH_VALUES],
      },
    )

  def test_success_all_parameters_empty(self):
    """
    All of the parameters are empty lists.

    This is technically valid, though probably not very useful.
    """
    self.assertFilterPasses(
      {
        'addresses':  [],
        'approvees':  [],
        'bundles':    [],
        'tags':       [],
      },
    )

  def test_fail_unexpected_parameters(self):
    """
    The request contains unexpected parameters.
    """
    self.assertFilterErrors(
      {
        'addresses':  [Address(self.trytes1)],
        'approvees':  [TransactionHash(self.trytes1)],
        'bundles':    [TransactionHash(self.trytes1)],
        'tags':       [Tag(self.trytes1)],

        # Hey, you're not allowed in he-argh!
        'foo': 'bar',
      },

      {
        'foo': [f.FilterMapper.CODE_EXTRA_KEY],
      },
    )

  def test_fail_bundles_wrong_type(self):
    """
    ``bundles`` is not an array.
    """
    self.assertFilterErrors(
      {
        'bundles': TransactionHash(self.trytes1),
      },

      {
        'bundles': [f.Type.CODE_WRONG_TYPE],
      },
    )

  def test_fail_bundles_contents_invalid(self):
    """
    ``bundles`` is an array, but it contains invalid values.
    """
    self.assertFilterErrors(
      {
        'bundles': [
          b'',
          True,
          None,
          b'not valid trytes',

          # This is actually valid; I just added it to make sure the
          # filter isn't cheating!
          TryteString(self.trytes2),

          2130706433,
          b'9' * 82,
        ],
      },

      {
        'bundles.0':  [f.Required.CODE_EMPTY],
        'bundles.1':  [f.Type.CODE_WRONG_TYPE],
        'bundles.2':  [f.Required.CODE_EMPTY],
        'bundles.3':  [Trytes.CODE_NOT_TRYTES],
        'bundles.5':  [f.Type.CODE_WRONG_TYPE],
        'bundles.6':  [Trytes.CODE_WRONG_FORMAT],
      },
    )

  def test_fail_addresses_wrong_type(self):
    """
    ``addresses`` is not an array.
    """
    self.assertFilterErrors(
      {
        'addresses':  Address(self.trytes1),
      },

      {
        'addresses': [f.Type.CODE_WRONG_TYPE],
      },
    )

  def test_fail_addresses_contents_invalid(self):
    """
    ``addresses`` is an array, but it contains invalid values.
    """
    self.assertFilterErrors(
      {
        'addresses': [
          b'',
          True,
          None,
          b'not valid trytes',

          # This is actually valid; I just added it to make sure the
          # filter isn't cheating!
          TryteString(self.trytes2),

          2130706433,
          b'9' * 82,
        ],
      },

      {
        'addresses.0':  [f.Required.CODE_EMPTY],
        'addresses.1':  [f.Type.CODE_WRONG_TYPE],
        'addresses.2':  [f.Required.CODE_EMPTY],
        'addresses.3':  [Trytes.CODE_NOT_TRYTES],
        'addresses.5':  [f.Type.CODE_WRONG_TYPE],
        'addresses.6':  [Trytes.CODE_WRONG_FORMAT],
      },
    )

  def test_fail_tags_wrong_type(self):
    """
    ``tags`` is not an array.
    """
    self.assertFilterErrors(
      {
        'tags':  Tag(self.trytes1),
      },

      {
        'tags': [f.Type.CODE_WRONG_TYPE],
      },
    )

  def test_fail_tags_contents_invalid(self):
    """
    ``tags`` is an array, but it contains invalid values.
    """
    self.assertFilterErrors(
      {
        'tags': [
          b'',
          True,
          None,
          b'not valid trytes',

          # This is actually valid; I just added it to make sure the
          # filter isn't cheating!
          TryteString(self.trytes1),

          2130706433,
          b'9' * 28,
        ],
      },

      {
        'tags.0':  [f.Required.CODE_EMPTY],
        'tags.1':  [f.Type.CODE_WRONG_TYPE],
        'tags.2':  [f.Required.CODE_EMPTY],
        'tags.3':  [Trytes.CODE_NOT_TRYTES],
        'tags.5':  [f.Type.CODE_WRONG_TYPE],
        'tags.6':  [Trytes.CODE_WRONG_FORMAT],
      },
    )

  def test_fail_approvees_wrong_type(self):
    """
    ``approvees`` is not an array.
    """
    self.assertFilterErrors(
      {
        'approvees': TransactionHash(self.trytes1),
      },

      {
        'approvees': [f.Type.CODE_WRONG_TYPE],
      },
    )

  def test_fail_approvees_contents_invalid(self):
    """
    ``approvees`` is an array, but it contains invalid values.
    """
    self.assertFilterErrors(
      {
        'approvees': [
          b'',
          True,
          None,
          b'not valid trytes',

          # This is actually valid; I just added it to make sure the
          # filter isn't cheating!
          TryteString(self.trytes2),

          2130706433,
          b'9' * 82,
        ],
      },

      {
        'approvees.0':  [f.Required.CODE_EMPTY],
        'approvees.1':  [f.Type.CODE_WRONG_TYPE],
        'approvees.2':  [f.Required.CODE_EMPTY],
        'approvees.3':  [Trytes.CODE_NOT_TRYTES],
        'approvees.5':  [f.Type.CODE_WRONG_TYPE],
        'approvees.6':  [Trytes.CODE_WRONG_FORMAT],
      },
    )

class FindTransactionsResponseFilterTestCase(BaseFilterTestCase):
  filter_type = FindTransactionsCommand(MockAdapter()).get_response_filter
  skip_value_check = True

  # noinspection SpellCheckingInspection
  def setUp(self):
    super(FindTransactionsResponseFilterTestCase, self).setUp()

    # Define a few valid values here that we can reuse across multiple
    # tests.
    self.trytes1 = b'RBTC9D9DCDQAEASBYBCCKBFA'
    self.trytes2 =\
      b'CCPCBDVC9DTCEAKDXC9D9DEARCWCPCBDVCTCEAHDWCTCEAKDCDFD9DSCSA'

  def test_no_results(self):
    """
    The incoming response contains no hashes.
    """
    response = {
      'hashes':   [],
      'duration': 42,
    }

    filter_ = self._filter(response)

    self.assertFilterPasses(filter_)
    self.assertDictEqual(filter_.cleaned_data, response)

  # noinspection SpellCheckingInspection
  def test_search_results(self):
    """
    The incoming response contains lots of hashes.
    """
    filter_ = self._filter({
      'hashes': [
        'RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFW'
        'YWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVA',

        'ZJVYUGTDRPDYFGFXMKOTV9ZWSGFK9CFPXTITQLQN'
        'LPPG9YNAARMKNKYQO9GSCSBIOTGMLJUFLZWSY9999',
      ],

      'duration': 42,
    })

    self.assertFilterPasses(filter_)
    self.assertDictEqual(
      filter_.cleaned_data,

      {
        'hashes': [
          TransactionHash(
            b'RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFW'
            b'YWZRE9JQKG9REPKIASHUUECPSQO9JT9XNMVKWYGVA',
          ),

          TransactionHash(
            b'ZJVYUGTDRPDYFGFXMKOTV9ZWSGFK9CFPXTITQLQN'
            b'LPPG9YNAARMKNKYQO9GSCSBIOTGMLJUFLZWSY9999',
          ),
        ],

        'duration': 42,
      },
    )

class FindTransactionsCommandTestCase(TestCase):
  def setUp(self):
    super(FindTransactionsCommandTestCase, self).setUp()

    self.adapter = MockAdapter()

  def test_wireup(self):
    """
    Verify that the command is wired up correctly.
    """
    self.assertIsInstance(
      Iota(self.adapter).findTransactions,
      FindTransactionsCommand,
    )


from typing import Union

from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI()

@app.get("/health")
async def health_check():
    return JSONResponse({"status":"ok"})

@app.get("/")
async def read_root():
    return {"Hello": "World"}


#!../../../env/bin/python
'''
Script to test threading functions on archive.
'''
# Standalone broilerplate -------------------------------------------------------------
from django_setup import do_setup
do_setup()
# -------------------------------------------------------------------------------------

import argparse
import time

from mlarchive.archive.models import Message, EmailList
from mlarchive.archive.thread import process

def timeit(method):
    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()

        print('%r (%r, %r) %2.2f sec' % (method.__name__, args, kw, te - ts))
        return result

    return timed

@timeit
def do_thread(elist, args):
    queryset = Message.objects.filter(email_list=elist).order_by('date')
    # DEBUG
    # ids = ['55ADF8D7.1000608@meinberg.de', '613F85B8-20E2-45AB-A1D9-1CACC5B82F64@noao.edu']
    # queryset = Message.objects.filter(email_list__name='ntp',subject__contains='Proposed REFID changes').order_by('date')
    # queryset = Message.objects.filter(email_list__name='ntp',msgid__in=ids).order_by('date')
    if not queryset:
        return

    root = process(queryset, debug=args.verbose)

    # check walk
    count = 0
    empty = 0
    for c in root.walk():
        count = count + 1
        if c.is_empty():
            empty = empty + 1

    print("Messages: {}, Containers: {}, Empty {}".format(
        queryset.count(),
        count,
        empty))

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', action='store_true', help='verbose mode')
    parser.add_argument('-s', '--start', help='the list to start with')
    parser.add_argument('-l', '--list', help='the list to process')
    args = parser.parse_args()
    kwargs = {}
    if args.list:
        kwargs['name'] = args.list
    if args.start:
        kwargs['name__gte'] = args.start

    elists = EmailList.objects.filter(**kwargs).order_by('name')
    for elist in elists:
        do_thread(elist, args)

if __name__ == "__main__":
    main()