import logging
import math

from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport

_LOGGER = logging.getLogger(__name__)

class OctopusEnergyGraphQLClient:

  def __init__(self, api_key: str):
    if (api_key == None):
      raise Exception('API KEY is not set')

    self._api_key = api_key
    self._base_url = "https://api.octopus.energy/v1/graphql/"
    self._login_attempt = 0
    self._session = None

  async def async_get_accounts(self):
    """Gets the accounts for the given API key"""
    return await self.__async_execute_with_session(self.__async_get_accounts)

  async def async_get_combined_state(self, account_id: str):
    """Gets the state for the given account"""
    return await self.__async_execute_with_session(lambda session: self.__async_get_combined_state(session, account_id))

  async def async_get_charge_preferences(self, account_id: str):
    """Gets the charging preferences for the given account"""
    return await self.__async_execute_with_session(lambda session: self.__async_get_charge_preferences(session, account_id))

  async def async_set_charge_preferences(self, account_id: str, readyByHoursAfterMidnight: float, targetSocPercent: int):
    """Sets the charging preferences for the given account"""
    return await self.__async_execute_with_session(
      lambda session: self.__async_set_charge_preferences(session,
        account_id,
        readyByHoursAfterMidnight,
        targetSocPercent))

  async def async_trigger_boost_charge(self, account_id: str):
    """Triggers a boost charge for the given account"""
    return await self.__async_execute_with_session(lambda session: self.__async_trigger_boost_charge(session, account_id))

  async def async_cancel_boost_charge(self, account_id: str):
    """Cancels the boost charge for the given account"""
    return await self.__async_execute_with_session(lambda session: self.__async_cancel_boost_charge(session, account_id))

  async def async_suspend_smart_charging(self, account_id: str):
    """Suspends smart charging for the given account"""
    return await self.__async_execute_with_session(lambda session: self.__async_suspend_smart_charging(session, account_id))

  async def async_resume_smart_charging(self, account_id: str):
    """Resumes smart charging for the given account"""
    return await self.__async_execute_with_session(lambda session: self.__async_resume_smart_charging(session, account_id))

  async def async_get_device_info(self, account_id: str):
    """Gets the device info for the given account"""
    return await self.__async_execute_with_session(lambda session: self.__async_get_device_info(session, account_id))

  async def __async_get_token(self):
    """Gets a new token from the API"""
    transport = AIOHTTPTransport(url=self._base_url)

    # Using `async with` on the client will start a connection on the transport
    # and provide a `session` variable to execute queries on this connection
    async with Client(
      transport=transport,
      fetch_schema_from_transport=True,
    ) as session:

      # Execute single query
      query = gql(
      '''
        mutation krakenTokenAuthentication($apiKey: String!) {
          obtainKrakenToken(input: { APIKey: $apiKey })
          {
            token
          }
        }
      ''')

      params = {"apiKey": self._api_key}

      result = await session.execute(query, variable_values=params, operation_name="krakenTokenAuthentication")
      token = result['obtainKrakenToken']['token']
      return token

  async def __async_get_session(self, reset = False):
    """Creates a new session, use with `async with`"""
    if (reset):
      self._session = None

    if (self._session != None):
      return self._session

    token = await self.__async_get_token()
    headers = {"Authorization": token}
    transport = AIOHTTPTransport(url=self._base_url, headers=headers)

    # Using `async with` on the client will start a connection on the transport
    # and provide a `session` variable to execute queries on this connection
    session = Client(
      transport=transport,
      fetch_schema_from_transport=True,
    )
    self._session = session
    return session

  async def __async_execute_with_session(self, func):
    """Executes the given function with a session, auto retrying with a new session if it fails."""
    try:
      async with await self.__async_get_session() as session:
        return await func(session)
    except Exception as e:
      try:
        async with await self.__async_get_session(reset = True) as session:
          return await func(session)
      except Exception as e:
        raise e

  async def __async_set_charge_preferences(self, session, account_id: str, readyByHoursAfterMidnight: float, targetSocPercent: int):
    """Sets the charging preferences for the given account"""
    # round up to nearest 5
    targetSocPercent = 5 * math.ceil(round(targetSocPercent) / 5)
    # round up to nearest 0.5
    readyByHoursAfterMidnight = 0.5 * round(readyByHoursAfterMidnight / 0.5)

    if readyByHoursAfterMidnight < 4 or readyByHoursAfterMidnight > 11:
        raise ValueError("Target time must be between 4AM and 11AM")
    if targetSocPercent < 10 or targetSocPercent > 100:
        raise ValueError("Target SOC percent must be between 10 and 100")

    # get hours
    readyByHoursAfterMidnightHours = int(readyByHoursAfterMidnight)
    # get mins - rounded to nearest 30 mins
    readyByHoursAfterMidnightMinutes = round(60 * (readyByHoursAfterMidnight % 1))

    targetTime = f"{readyByHoursAfterMidnightHours:02}:{readyByHoursAfterMidnightMinutes:02}"

    # Execute single query
    query = gql(
    '''
      mutation setVehicleChargePreferences($accountNumber: String!, $targetTime: String!, $targetSocPercent: Int!) {
        setVehicleChargePreferences(input: { accountNumber: $accountNumber, weekdayTargetTime: $targetTime, weekendTargetTime: $targetTime, weekdayTargetSoc: $targetSocPercent, weekendTargetSoc: $targetSocPercent }) {
          krakenflexDevice {
            krakenflexDeviceId
          }
        }
      }
    ''')

    params = {"accountNumber": account_id, "targetTime": targetTime, "targetSocPercent": targetSocPercent}
    result = await session.execute(query, variable_values=params, operation_name="setVehicleChargePreferences")
    return result['setVehicleChargePreferences']

  async def __async_trigger_boost_charge(self, session, account_id: str):
    """Triggers a boost charge for the given account"""
    # Execute single query
    query = gql(
    '''
      mutation triggerBoostCharge($accountNumber: String!) {
        triggerBoostCharge(input: { accountNumber: $accountNumber }) {
          krakenflexDevice {
            krakenflexDeviceId
          }
        }
      }
    ''')

    params = {"accountNumber": account_id}
    result = await session.execute(query, variable_values=params, operation_name="triggerBoostCharge")
    return result['triggerBoostCharge']

  async def __async_cancel_boost_charge(self, session, account_id: str):
    """Cancels any boost charge currently in progress for the given account"""
    # Execute single query
    query = gql(
    '''
      mutation deleteBoostCharge($accountNumber: String!) {
        deleteBoostCharge(input: { accountNumber: $accountNumber }) {
          krakenflexDevice {
            krakenflexDeviceId
          }
        }
      }
    ''')

    params = {"accountNumber": account_id}
    result = await session.execute(query, variable_values=params, operation_name="deleteBoostCharge")
    return result['deleteBoostCharge']

  async def __async_get_accounts(session):
    # Execute single query
    query = gql(
    '''
        query viewer {
          viewer {
            fullName
            accounts {
              number
            }
          }
        }
    ''')

    params = {}
    result = await session.execute(query, variable_values=params, operation_name="viewer")

    return list(map(lambda x: x['number'], result['viewer']['accounts']))

  async def __async_get_charge_preferences(self, session, account_id: str):
    """Gets the charging preferences for the given account"""
    # Execute single query
    query = gql(
    '''
      query vehicleChargingPreferences($accountNumber: String!) {
        vehicleChargingPreferences(accountNumber: $accountNumber) {
          weekdayTargetTime,
          weekdayTargetSoc,
          weekendTargetTime,
          weekendTargetSoc
        }
      }
    ''')

    params = {"accountNumber": account_id}
    result = await session.execute(query, variable_values=params, operation_name="vehicleChargingPreferences")
    return result['vehicleChargingPreferences']

  async def __async_get_device_info(self, session, account_id: str):
    """Get the user's device info (e.g. vehicle make, battery size, state etc.)"""
    # Execute single query
    query = gql(
    '''
      query registeredKrakenflexDevice($accountNumber: String!) {
        registeredKrakenflexDevice(accountNumber: $accountNumber) {
          krakenflexDeviceId
          provider
          vehicleMake
          vehicleModel
          vehicleBatterySizeInKwh
          chargePointMake
          chargePointModel
          chargePointPowerInKw
          status
          suspended
          hasToken
          createdAt
        }
      }
    ''')

    params = {"accountNumber": account_id}
    result = await session.execute(query, variable_values=params, operation_name="registeredKrakenflexDevice")
    return result['registeredKrakenflexDevice']

  async def __async_get_combined_state(self, session, account_id: str):
    """Get the user's account state"""
    query = gql(
    '''
        query getCombinedData($accountNumber: String!) {
            vehicleChargingPreferences(accountNumber: $accountNumber) {
                weekdayTargetTime,
                weekdayTargetSoc,
                weekendTargetTime,
                weekendTargetSoc
            }
            registeredKrakenflexDevice(accountNumber: $accountNumber) {
                krakenflexDeviceId
                provider
                vehicleMake
                vehicleModel
                vehicleBatterySizeInKwh
                chargePointMake
                chargePointModel
                chargePointPowerInKw
                status
                suspended
                hasToken
                createdAt
            }
            plannedDispatches(accountNumber: $accountNumber) {
                startDtUtc: startDt
                endDtUtc: endDt
                chargeKwh: delta
                meta {
                    source
                    location
                }
            }
            completedDispatches(accountNumber: $accountNumber) {
                startDtUtc: startDt
                endDtUtc: endDt
                chargeKwh: delta
                meta {
                    source
                    location
                }
            }
        }
    ''')

    params = {"accountNumber": account_id}
    result = await session.execute(query, variable_values=params, operation_name="getCombinedData")
    return result

  async def __async_suspend_smart_charging(self, session, account_id: str):
    """Suspends smart charging for the given account"""
    # Execute single query
    query = gql(
    '''
      mutation suspendControl($accountNumber: String!) {
        suspendControl(input: { accountNumber: $accountNumber }) {
          krakenflexDevice {
            krakenflexDeviceId
          }
        }
      }
    ''')

    params = {"accountNumber": account_id}
    result = await session.execute(query, variable_values=params, operation_name="suspendControl")
    return result['suspendControl']

  async def __async_resume_smart_charging(self, session, account_id: str):
    """Resumes smart charging for the given account"""
    # Execute single query
    query = gql(
    '''
      mutation resumeControl($accountNumber: String!) {
        resumeControl(input: { accountNumber: $accountNumber }) {
          krakenflexDevice {
            krakenflexDeviceId
          }
        }
      }
    ''')

    params = {"accountNumber": account_id}
    result = await session.execute(query, variable_values=params, operation_name="resumeControl")
    return result['resumeControl']


#Ask the user to enter a number. Then find all the primes up to that number.

def isPrime(num):
    for i in range(2,num):
        if (num % i) == 0:
          return False
    return True

def allPrimes(num):
  primes = []
  for n in range(2,num+1):
    if isPrime(n) is True:
        primes.append(n)
  return primes

num = int(input("Upper Limit: "))
primes = allPrimes(num)
print(primes)


# Definition for a Node.
class Node:
    def __init__(
        self,
        val: int = 0,
        left: "Node" = None,
        right: "Node" = None,
        next: "Node" = None,
    ):
        self.val = val
        self.left = left
        self.right = right
        self.next = next

class Solution:
    def __init__(self):
        self.queue = []

    def connect(self, root: "Node") -> "Node":
        if root is None:
            return None
        current_idx = 0
        prev_node_idx = None
        previous_level = 0
        root.level = previous_level
        self.queue.append(root)
        while not len(self.queue) == current_idx:
            node = self.queue[current_idx]
            self.current_level = node.level
            if self.current_level > previous_level:
                self.queue[prev_node_idx].next = None
            elif prev_node_idx is not None:
                self.queue[prev_node_idx].next = node

            self.add_node_to_queue(node.left)
            self.add_node_to_queue(node.right)

            previous_level = self.current_level
            prev_node_idx = current_idx
            current_idx += 1
        self.queue[prev_node_idx].next = None
        return root

    def add_node_to_queue(self, children: "Node"):
        if children is not None:
            children.level = self.current_level + 1
            self.queue.append(children)


from zdppy_api  import Api

app = Api()

@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str | None = None):
    """
    q: str | None = None
    q是可选参数，字符串类型或者None，默认是None
    """
    if q:
        return {"item_id": item_id, "q": q}
    return {"item_id": item_id}

if __name__ == '__main__':
    import uvicorn

    uvicorn.run("z06_optional_parameter:app", reload=True, host="0.0.0.0", port=8888)


# -*- coding: utf-8 -*-
import json
import random
import time

import scrapy

from ljh.items import *

class DoubanSpider(scrapy.Spider):
    name = 'douban'
    allowed_domains = ['movie.douban.com']
    mapping = {'姓名': 'name', '性别': 'gender', '星座': 'constellation', '出生日期': 'birthday', '去世日期': 'date_of_death',
               '出生地': 'birthplace',
               '职业': 'job', '更多外文名': 'other_foreign_names', '更多中文名': 'other_chinese_names', '生卒日期': 'birthday',
               '家庭成员': 'families'}

    # start_urls = ['https://movie.douban.com/top250']
    # start_urls = ['https://movie.douban.com/subject/30304469/?tag=%E7%83%AD%E9%97%A8&from=gaia']
    count = 0
    maxcount = 2500

    def start_requests(self):
        # yield scrapy.Request(url='https://movie.douban.com/subject/1292052/', callback=self.parse_film_page)
        # yield scrapy.Request(url='https://movie.douban.com/top250', callback=self.parse)
        tags = [
            '%e7%83%ad%e9%97%a8',  # 热门
            '%e6%9c%80%e6%96%b0',  # 最新
            '%e7%bb%8f%e5%85%b8',  # 经典
            '%e8%b1%86%e7%93%a3%e9%ab%98%e5%88%86'  # 豆瓣高分
        ]
        for tag in tags:
            yield scrapy.Request(
                url='https://movie.douban.com/j/search_subjects?type=movie&tag=%s&sort=recommend&page_limit=2000&page_start=0' % tag,
                callback=self.parse_json)

    def parse_json(self, response):
        res = json.loads(response.body_as_unicode())['subjects']
        for i in res:
            yield response.follow(i['url'], callback=self.parse_film_page)

    # def start_requests(self):
    #     # yield scrapy.Request(url='https://movie.douban.com/subject/1292052/', callback=self.parse_film_page)
    #     yield scrapy.Request(url='https://movie.douban.com/top250', callback=self.parse)
    #
    # def parse(self, response):
    #     res = response.css('ol.grid_view li div.item div.pic a::attr(href)').getall()
    #     for url in res:
    #         yield response.follow(url, callback=self.parse_film_page)
    #         # print(self.count, picture, main_page, title, description, star, quote)
    #     next = response.css('div.paginator span.next a::attr(href)').extract_first()
    #     if next is not None:
    #         yield response.follow(next, callback=self.parse)

    def parse_film_page(self, response):
        self.count = self.count + 1
        item = FilmItem()
        item['film_id'] = response.request.url.split('subject/')[1].split('/')[0]
        item['title'] = response.css('#content > h1 > span:nth-child(1)::text').extract_first()
        item['year'] = response.css('#content > h1 > span.year::text').extract_first().replace('(', '').replace(')', '')
        item['directors'] = response.css('#info > span:nth-child(1) > span.attrs > a::text').getall()
        director_hrefs = response.css('#info > span:nth-child(1) > span.attrs > a::attr(href)').getall()
        item['writers'] = response.css('#info > span:nth-child(3) > span.attrs > a::text').getall()
        writer_hrefs = response.css('#info > span:nth-child(3) > span.attrs > a::attr(href)').getall()
        item['actors'] = response.css('#info > span.actor > span.attrs > a::text').getall()[:3]
        actor_hrefs = response.css('#info > span.actor > span.attrs > a::attr(href)').getall()[:3]
        item['types'] = response.css('#info > span[property="v:genre"]::text').getall()
        item['region'] = \
            response.css('#info').re(r'制片国家/地区:.*
')[0].split('')[1].split('
')[0]
        item['description'] = response.css('#link-report > span::text').extract_first().strip()
        try:
            item['language'] = response \
                .css('#info') \
                .re(r'语言:.*
')[0] \
                .split('')[1] \
                .split('
')[0] \
                .strip()
        except IndexError:
            item['language'] = ''

        try:
            item['other_name'] = response \
                .css('#info') \
                .re(r'又名:.*
')[0] \
                .split('')[1] \
                .split('
')[0] \
                .strip()
        except IndexError:
            item['other_name'] = ''
        item['release_date'] = response.css('#info > span[property="v:initialReleaseDate"]::text').extract_first()
        item['size'] = response.css('#info > span[property="v:runtime"]::attr(content)').extract_first()
        item['star'] = response.css(
            '#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong::text').extract_first()
        item['star_percent'] = response.css(
            '#interest_sectl > div.rating_wrap.clearbox > div.ratings-on-weight > div > span.rating_per::text').getall()
        item['comment_num'] = response.css(
            '#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > div > div.rating_sum > a > span::text').extract_first()
        item['src'] = response.css(
            '#mainpic > a > img::attr(src)').extract_first()
        recommendations = response.css('#recommendations > div > dl > dt > a::attr(href)').getall()
        # print(title, year, directors, writers, actors, types, region, release_date, size, star, star_percent, sep='\n')
        try:
            for href in director_hrefs:
                d_item = DirectorItem()
                d_item['film_id'] = item['film_id']
                d_item['celebrity_id'] = href.split('celebrity/')[1].split('/')[0]
                yield d_item
            for href in writer_hrefs:
                w_item = WriterItem()
                w_item['film_id'] = item['film_id']
                w_item['celebrity_id'] = href.split('celebrity/')[1].split('/')[0]
                yield w_item
            for href in actor_hrefs:
                a_item = ActorItem()
                a_item['film_id'] = item['film_id']
                a_item['celebrity_id'] = href.split('celebrity/')[1].split('/')[0]
                yield a_item
        except IndexError:
            pass
        else:
            yield item
        for i in list(director_hrefs) + writer_hrefs + actor_hrefs:
            yield response.follow(i, callback=self.parse_celebrity_page)

        if self.count < self.maxcount:
            for i in recommendations:
                yield response.follow(i, callback=self.parse_film_page)

    def parse_celebrity_page(self, response):
        name = response.css('#content > h1::text').extract_first()
        ul = response.css('#headline > div.info > ul > li::text').getall()
        info_names = ['姓名'] + response.css('#headline > div.info > ul > li > span::text').getall()
        del info_names[-1]
        info_list = [name]
        for s in ul:
            s = s.replace(':', '').strip()
            if len(s) > 0:
                info_list.append(s)
        # result = {k: v for k, v in zip(info_names, info_list)}
        # print(result)
        item = CelebrityItem()
        item['celebrity_id'] = response.request.url.split('celebrity/')[1].split('/')[0]
        item['human_src'] = response.css('#headline > div.pic > a > img::attr(src)').extract_first()
        for k in self.mapping:
            item[self.mapping[k]] = ''
        for k, v in zip(info_names, info_list):
            if k == '生卒日期':
                birthday_and_date_of_death = v.split('至')
                item['birthday'] = birthday_and_date_of_death[0]
                item['date_of_death'] = birthday_and_date_of_death[1]
            else:
                item[self.mapping[k]] = v
        yield item


import pandas as pd

def bed_has_header(path:str)->bool:
    """Return if bed file at given path has header.

    Parameters
    ---------------------
    path: str,
        the candidate bed to validate.

    Returns
    ---------------------
    Return boolean representing if given candidate bed
    has a header.
    """
    # Checking the first row of bed file
    header = pd.read_csv(path, sep="\t", nrows=1)
    return any(
        column in header.columns
        for column in ("chrom", "chromStart", "chromEnd")
    )


#   Более удобный вариант программы
# result_day_first = int(input('Введите результат спортсмена в первый день '))
# result_check_exceed = int(input('Введите результат для поиска дня '))
# step_increase = 1.1  # 10% - шаг с которым увеличиваем каждый день результат спортсмена
# number_day = 1  # отправной номер дня по умолчанию
#
# while result_day_first < result_check_exceed:
#     result_day_first = result_day_first * step_increase
#     number_day = number_day + 1
# else:
#     print('Номер дня, когда спортсмен превысил', number_day)

#  Вариант со статичными данными
result_day_first = str(2)  # фейковые статичные данные результата первого дня, как будто из input
result_check_exceed = str(3)  # фейковые статичные данные для поиска превышения результата
step_increase = 1.1  # 10% - шаг с которым увеличиваем каждый день результат спортсмена
number_day = 1  # отправной номер дня по умолчанию

while float(result_day_first) < float(result_check_exceed):
    result_day_first = float(result_day_first) * step_increase
    number_day = number_day + 1
else:
    print(number_day)

# test commit 1


"""6.009 Lab 3 -- Circuit Solver."""

# NO IMPORTS ALLOWED!

# Uncomment below and comment/rename the solveLinear defined in this file to
# use the sample solveLinear function.
# Remember to comment it out before submitting!

#from solve_linear_sample import solveLinear
# global asst
# asst = {}

def substituteEquation(equation, substitutedVariable, substitutionEquation):
    """
        Note that implementing this function is optional. You might want to
        consider implementing it to break up your code into more managable
        chunks.

        Given:
            equation: An equation represented by a dictionary that maps the
                      variables or 1 to its coefficient in the equation.
                      E.g. {1: 2, 'x': 2, 'y': 3} represents 2 + 2x + 3y = 0.
            substitutedVariable: The variable to be substituted out of the
                                 equation.
            substitutionEquation: The substitution equation represented as a
                                  dictionary.
        Return:
            finalEquation: A dictionary representing the resulting equation
                           after the substitution is performed.
    """
    coeff = equation[substitutedVariable]
    d = {}
    for var in {**substitutionEquation, **equation}:
        if var in equation:
            if var in substitutionEquation:
                x = equation[var] - coeff*substitutionEquation[var]/substitutionEquation[substitutedVariable]
            else:
                x = equation[var]
        else:
            x = -coeff*substitutionEquation[var]/substitutionEquation[substitutedVariable]
        if var == 1:
            d[var] = x
        if abs(x) > 10**-7:
            d[var] = x
    return d

def down_gauss(equations, upper=[]):
    '''
    find equation with most variables and pop from equations
    find variable with largest coefficient
    for all equations in eq_d[max_var], substitute longest equation and add
    resulting eq to new equations set.
    recurse on new equations and updated equation dict
    return equation dict when there are no more equations, order that variables
    were popped in list
    '''
    if equations == {}:
        return upper
    sub_eq = min(equations, key=len)
    vars = set(sub_eq)
    try:
        vars.remove(1)
    except:
        pass
    maxVar = max(vars, key=lambda x: abs(sub_eq[x]))
    for eq in varDict[maxVar]:
        if maxVar in eq:
            new_eqs.append(substituteEquation(eq, maxVar, sub_eq))
        else:
            new_eqs.append(eq)
    upper.append((maxVar,sub_eq))
    return down_gauss(new_eqs, upper)

def up_gauss(upper, asst={1:1}):
    '''
    for all equations in eq_d[var_order.pop()], substitute shortest equations
    and add assignment
    '''
    if upper == []:
        del asst[1]
        return asst
    var, eq = upper.pop()
    asst[var] = -sum([asst[key]*eq[key] for key in eq if key != var])/eq[var]
    return up_gauss(upper, asst)

def get_vardict(equations):
    for variable in variables:
        for eq in equations:
            if var in eq:
                try:
                    varDict[var].add[eq]
                except:
                    varDict[var] = {eq}
    return varDict

def solveLinear(variables, equations):
    """
        Given:
            variables: A set of strings or tuples representing the independent
                       variables. E.g. {'x', 'y', 'z'}
            equations: A list of linear equations where each equation is
                       represented as a dictionary. Each dictionary maps the
                       variables or 1 to its coefficient in the equation.
                       E.g. {1: 2, 'x': 2, 'y': 3} represents 2 + 2x + 3y = 0.
                       Note that all variables may not appear in all of the
                       equations. Moreover, you may assume that the equations
                       are independent.
        Return:
            result: A dictionary mapping each variable to the numerical value
            that solves the system of equations. Assume that there is exactly
            one solution. Some inaccuracy as typical from floating point
            computations will be acceptable.
    """
    varDict = get_vardict(equations)
    upper = down_gauss(set([frozenset(eq) for eq in equations]), varDict)
    return up_gauss(upper)

def solveCircuit(junctions, wires, resistances, voltages):
    """
        Given:
            junctions:  A set of junctions. Each junction is labeled by a string
                        or a tuple.
            wires:      A dictionary mapping a unique wire ID (a string or tuple)
                        to a tuple of two elements representing the starting and
                        ending junctions of the wire, respectively. The set of
                        wire IDs is disjoint from the set of junction labels.
                        Note that although electricity can flow in either
                        directions, each wire between a pair of junctions will
                        appear exactly once in the list. Moreover, the starting
                        and ending junctions are distinct.
            resistances:A dictionary mapping each unique wire ID to a numeric
                        value representing the magnitude of the resistance of
                        the wire in Ohms. This dictionary has the same set of
                        keys as the wires dictionary.
            voltages:   A dictionary mapping each unique wire ID to a numeric
                        value representing the voltage (EMF or potential
                        difference) of the battery connected along the wire in
                        Volts. The positive terminal of the battery is next to
                        the ending junction (as defined in the wires dictionary)
                        if the voltage is positive whereas it is next to the
                        starting junction otherwise. This dictionary also has
                        the same set of keys as the wires dictionary.
        Return:
            result: A dictionary mapping the label of each wire to the current
                    it carries. The labels must be the keys in the wires
                    dictionary and the current should be considered positive if
                    it is flowing from the starting junction to the ending
                    junction as specified in the wires dictionary.
    """
    ### make I Equations
    equations = {}
    for wire in wires:
        start, stop = wires[wire]
        if start in equations:
            equations[start][wire] = 1
        else:
            equations[start] = {wire: 1, 1:0}
        if stop in equations:
            equations[stop][wire] = -1
        else:
            equations[stop] = {wire: -1, 1:0}
        r = resistances[wire]
        v = voltages[wire]
        equations[wire] = {start:1, stop:-1, wire:r, 1:-v}
    equations[0] = {start:1, 1:0}
    del equations[start]
    solved = solveLinear(set(equations.keys()).add(start), list(equations.values()))
    d = {}
    for wire in wires:
        d[wire] = solved[wire]
    return d

def findMaximumDeviationJunction(junctions, wires, resistances, voltages, currents):
    """
        Note that this part is completely optional and would not contribute to your grade.

        Given:
            junctions:  A set of junctions. Each junction is labeled by a
                        string or a tuple.
            wires:      A dictionary mapping a unique wire ID (a string or tuple)
                        to a tuple of two elements representing the starting and
                        ending junctions of the wire respectively. The set of
                        wire IDs is disjoint from the set of junction labels.
                        Note that although electricity can flow in either
                        direction, each wire between a pair of junctions will
                        appear exactly once in the list. Moreover, the starting
                        and ending junctions are distinct.
            resistances:A dictionary mapping each unique wire ID to a numeric
                        value representing the magnitude of the resistance of
                        the wire in Ohms. This dictionary has the same set of
                        keys as the wires dictionary.
            voltages:   A dictionary mapping each unique wire ID to a numeric
                        value representing the voltage (EMF or potential
                        difference) of the battery connected along the wire in
                        Volts. The positive terminal of the battery is next to
                        the ending junction (as defined in the wires dictionary)
                        if the voltage is positive whereas it is next to the
                        starting junction otherwise. This dictionary also has
                        the same set of keys as the wires dictionary.
            currents:   A dictionary mapping each unique wire ID to a numeric
                        value representing the indicated current flowing along
                        the wire. The format is identical to that of the output
                        of the previous function. Note that the values will not
                        necessarily be correct.
        Return:
            result: A junction with the maximum deviation from current
                    conservation. Note that any junction with maximal deviation
                    will be accepted.
    """
    raise NotImplementedError

def findMaximumDeviationLoop(junctions, wires, resistances, voltages, currents):
    """
        Note that this part is completely optional and would not contribute to your grade.

        Given:
            junctions:  A set of junctions. Each junction is labeled by a string
                        or a tuple.
            wires:      A dictionary mapping a unique wire ID (a string or tuple)
                        to a tuple of two elements representing the starting and
                        ending junctions of the wire respectively. The set of
                        wire IDs is disjoint from the set of junction labels.
                        Note that although electricity can flow in either
                        directions, each wire between a pair of junctions will
                        appear exactly once in the list. Moreover, the starting
                        and ending junctions are distinct.
            resistances:A dictionary mapping each unique wire ID to a numeric
                        value representing the magnitude of the resistance of
                        the wire in Ohms. This dictionary has the same set of
                        keys as the wires dictionary.
            voltages:   A dictionary mapping each unique wire ID to a numeric
                        value representing the voltage (EMF or potential
                        difference) of the battery connected along the wire in
                        Volts. The positive terminal of the battery is next to
                        the ending junction (as defined in the wires dictionary)
                        if the voltage is positive whereas it is next to the
                        starting junction otherwise. This dictionary also has
                        the same set of keys as the wires dictionary.
            currents:   A dictionary mapping each unique wire ID to a numeric
                        value representing the indicated current flowing along
                        the wire. The format is identical to that of the output
                        of the previous function. Note that the values will not
                        necessarily be correct.
        Return:
            result: A list of wires IDs representing the edges along a loop with
                    maximal (additive) deviation from Kirchoff's loop law.
                    The wires should be in order along the cycle but the
                    starting node and the direction may be arbitrary.
    """
    raise NotImplementedError

if __name__ == '__main__':
    # additional code here will be run only when lab.py is invoked directly
    # (not when imported from test.py), so this is a good place to put code
    # used for testing.
    variables = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
    equations = [{'a': 1, 'b': 2, 'e': 3, 1: -2},
                 {'b': 2, 'e': 4, 'd': 6, 'h': 4, 1: -8},
                 {'a': -1, 'c': 5, 'd': -4, 1: 2},
                 {'b': 2, 'd': -2, 'f': 3, 1: 10},
                 {'e': -1, 'g': 3, 1: 8},
                 {'a': 2, 'd': 3, 'h': -1, 1: -18},
                 {'b': -3, 'c': 2, 'f': 1, 1: 2},
                 {'c': 1, 'd': 2, 'e': -1, 1: -12}]
    print(solveLinear(variables, equations))


import logging

# This will be a demonstration of passing variables to your log file
# This assumes you have looked at example 1 and example 2 first

# Creating logger
l1 = logging.getLogger('example3')
l1.setLevel(logging.DEBUG)

# Creating handler

fh = logging.FileHandler('example3.log')
fh.setLevel(logging.DEBUG)

# Creating formatter

form = logging.Formatter('%(asctime)s, %(message)s')

# Add formatter to handler

fh.setFormatter(form)

# Add handler to logger

l1.addHandler(fh)

# Test logs
v1=5
v2='I\'m a single pringle'
v3=10.5
v4=False
v5='C'

l1.debug(v1)
l1.info(v2)
l1.warn(v3)
l1.error(v4)
l1.critical(v5)


from opencog.type_constructors import *
from opencog.utilities import initialize_opencog
from opencog.bindlink import execute_atom

# Initialize AtomSpace
atomspace = AtomSpace()
initialize_opencog(atomspace)

# Class to be stored in GroundedObjectNode
class Point:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def get_x(self):
        return self.x

    def get_y(self):
        return self.x

    def move(self, x, y):
        self.x += x
        self.y += y

    def __str__(self):
        return "Point(%d, %d)" % (self.x, self.y)

point = Point(2, 3)
print("point:", point)

GroundedObjectNode("point", point, unwrap_args=True)

apply_link = ApplyLink(
    MethodOfLink(
        GroundedObjectNode("point"),
        ConceptNode("move")
    ),
    ListLink(
        GroundedObjectNode("x", 3),
        GroundedObjectNode("y", 4)
    )
)

execute_atom(atomspace, apply_link)

print("point:", point)


import json
import os
import os.path as osp
import pickle
from collections import Counter

import numpy as np
import torch
import torch.utils.data as data
from PIL import Image

from utils import text
from utils.image import coco_name_format

def collate_fn(batch):
    """Sort batch (list) on question lengths for use in RNN pack_padded_sequence later."""
    batch.sort(key=lambda x: x['question_len'], reverse=True)
    return data.dataloader.default_collate(batch)

def get_dataloader(annotations, questions, images, args,
                   split="train", maps=None, vocab=None,
                   raw_images=False, transforms=None, shuffle=True):
    return data.DataLoader(VQADataset(annotations, questions, images, split, args,
                                      raw_images=raw_images, vocab=vocab,
                                      transforms=transforms, maps=maps),
                           batch_size=args.batch_size,
                           num_workers=args.num_workers,
                           shuffle=shuffle,
                           collate_fn=collate_fn)

class VQADataset(data.Dataset):
    def __init__(self, annotations, questions, images_dataset, split, args,
                 raw_images=False, transforms=None,
                 vocab=None, normalize_img=True, maps=None, year=2014):

        # the data is saved as a dict where the key is the image_id
        # and the value is the VGG feature vector
        if not raw_images:
            self.images_dataset = torch.load(images_dataset)
            print("Loaded {0} image embeddings dataset".format(split))

        self.split = split
        self.year = year

        self._process_dataset(annotations, questions, args, split, maps=maps)

        if vocab:
            self.vocab = vocab

        self.embed_question = args.embed_question

        self.raw_images = raw_images
        self.normalize_img = normalize_img
        self.transforms = transforms
        self.root = args.image_root

    def _process_dataset(self, annotations, questions, args,  split="train", maps=None):
        """
        Process the dataset and load it up.
        We should only do this for the training set.
        """
        self.data, self.vocab, self.word_to_wid, self.wid_to_word, \
            self.ans_to_aid, self.aid_to_ans = \
            process_vqa_dataset(questions, annotations, split,
                                maps, args.top_answer_limit, args.max_length)

    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        d = self.data[index]

        item = dict()

        if self.raw_images:
            item["image_id"] = d["image_id"]
            img = Image.open(osp.join(osp.expanduser(self.root),
                                      "{0}{1}".format(self.split, self.year),
                                      d["image_name"]))
            img = img.convert(mode='RGB')

            if self.transforms is not None:
                img = self.transforms(img)

        else:
            # Process Visual (image or features)
            # the preprocess script should have already saved these as Torch tensors
            item["image_id"] = d["image_id"]
            img = self.images_dataset[d["image_id"]].squeeze()

        item['image'] = img

        # Process Question (word token)
        item['question_id'] = d['question_id']
        item['question_wids'] = d['question_wids'].astype(np.int64)

        # if self.embed_question:
        #     item['question'] = torch.from_numpy(d['question_wids'])
        # else:
        #     # one_hot_vec = np.zeros((len(d["question_wids"]), len(self.vocab)))
        #     # for k in range(len(d["question_wids"])):
        #     #     one_hot_vec[k, d['question_wids'][k]] = 1
        #     # item['question'] = torch.from_numpy(one_hot_vec).float()
        #
        #     one_hot_vec = torch.zeros(
        #         (len(d["question_wids"]), len(self.vocab)))
        #     one_hot_vec[torch.arange(one_hot_vec.size(0)).long(
        #     ), d['question_wids'].astype(np.int64)] = 1
        #     item['question'] = one_hot_vec.float()
        item['question'] = torch.from_numpy(d['question_wids']).long()

        item['question_len'] = d['question_length']
        item['answer_type'] = d['answer_type']

        if self.split == "train":
            item['answer_id'] = d['answer_id']

        return item

def process_vqa_dataset(questions_file, annotations_file, split, maps=None,
                        top_answer_limit=1000, max_length=26, year=2014):
    """
    Process the questions and annotations into a consolidated dataset.
    This is done only for the training set.
    :param questions_file:
    :param annotations_file:
    :param split: The dataset split.
    :param maps: Dict containing various mappings such as \
        word_to_wid, wid_to_word, ans_to_aid and aid_to_ans.
    :param top_answer_limit:
    :param max_length: The maximum quetsion length. Taken from the VQA sample code.
    :param year: COCO Dataset release year.
    :return: The processed dataset ready to be used

    """
    cache_file = "vqa_{0}_dataset_cache.pickle".format(split)

    # Check if preprocessed cache exists. If yes, load it up, else preprocess the data
    if os.path.exists(cache_file):
        print("Found {0} set cache! Loading...".format(split))
        dataset, vocab, word_to_wid, wid_to_word, ans_to_aid, aid_to_ans = pickle.load(
            open(cache_file, 'rb'))

    else:
        # load the annotations and questions files
        print("Loading {0} annotations".format(split))
        with open(annotations_file) as ann:
            j = json.load(ann)
            annotations = j["annotations"]

        print("Loading {0} questions".format(split))
        with open(questions_file) as q:
            j = json.load(q)
            questions = j["questions"]

        # load up the dataset
        dataset = []
        for idx, q in enumerate(questions):
            d = dict()
            d["question_id"] = q["question_id"]
            d["question"] = q["question"]
            d["image_id"] = q["image_id"]
            d["image_name"] = coco_name_format(q["image_id"], split, year)

            d["answer"] = annotations[idx]["multiple_choice_answer"]
            answers = []
            for ans in annotations[idx]['answers']:
                answers.append(ans['answer'])
            d['answers_occurence'] = Counter(answers).most_common()

            d["question_type"] = annotations[idx]["question_type"]
            d["answer_type"] = annotations[idx]["answer_type"]

            dataset.append(d)

        if split == "train":
            # Get the top N answers so we can filter the dataset
            # to only questions with these answers
            top_answers = text.get_top_answers(dataset, top_answer_limit)
            dataset = text.filter_dataset(dataset, top_answers)

            # Process the questions
            dataset = text.preprocess_questions(dataset)

            vocab = text.get_vocabulary(dataset)

            # 0 is used for padding
            word_to_wid = {w: i+1 for i, w in enumerate(vocab)}
            wid_to_word = {i+1: w for i, w in enumerate(vocab)}
            ans_to_aid = {a: i for i, a in enumerate(top_answers)}
            aid_to_ans = {i: a for i, a in enumerate(top_answers)}

            dataset = text.encode_answers(dataset, ans_to_aid)

        else:  # split == "val":
            # Process the questions
            dataset = text.preprocess_questions(dataset)

            vocab = maps["vocab"]

            word_to_wid = maps["word_to_wid"]
            wid_to_word = maps["wid_to_word"]
            ans_to_aid = maps["ans_to_aid"]
            aid_to_ans = maps["aid_to_ans"]

            dataset = text.remove_tail_words(dataset, vocab)

        dataset = text.encode_questions(dataset, word_to_wid, max_length)

        print("Caching the processed data")
        pickle.dump([dataset, vocab, word_to_wid, wid_to_word, ans_to_aid, aid_to_ans],
                    open(cache_file, 'wb+'))

    return dataset, vocab, word_to_wid, wid_to_word, ans_to_aid, aid_to_ans


#!/user/bin/python
# -*- coding:utf-8 -*-
# Scopus-test.py
# 获取大学排名信息

"""
http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html

功能描述：
    输入：大学排名URL
    输出：大学排名信息的输出（排名，大学名称，地区，总分）
技术路径：requests-bs4
定向爬虫：仅对输入URL进爬取，不扩展爬取其他URL。

步骤1：从网络上获取大学排名网页内容
    getHTMLText()
步骤2：提取网页内容中信息到合适的数据结构
    fillUnivList()
步骤3：利用数据结构展示并输出结果
    printUnivList()

format 方法回顾：【:<><><>,<>,<>】

"""

import requests
import bs4

# 步骤1：从网络上获取大学排名网页内容
def getHTMLText(url):
    try:
        r = requests.get(url,timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""

# 步骤2：提取网页内容中信息到合适的数据结构
def fillUnivList(ulist,html):
    soup = bs4.BeautifulSoup(html, "html.parser")
    for tr in soup.find("tbody").children:
        if isinstance(tr, bs4.element.Tag):
            td_trs = []
            for td in tr:
                td_trs.append(td)
            ulist.append([td_trs[0].string, td_trs[1].div.string, td_trs[2].string, td_trs[3].string])

# 步骤3：利用数据结构展示并输出结果
def printUnivList(ulist,num):
    '''
    中英文混合输出时，采用中文字符的空格填充 chr(12288)
    :param ulist:
    :param num:
    :return:
    '''

    #生成输出模板
    tplt = "{0:^5}\t{1:{4}^10}\t{2:<6}\t{3:<10}"
    print(tplt.format("排名", "学校名称", "地区", "总分", chr(12288)))
    for i in range(num):
        u = ulist[i]
        print(tplt.format(u[0], u[1], u[2], u[3], chr(12288)))

def main():
    uinfo = []
    url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html"
    html = getHTMLText(url)
    fillUnivList(uinfo, html)
    printUnivList(uinfo, 50)    # 打印20个

# 执行
main()


import ducc0
import numpy as np

__all__ = ["MuellerConvolver"]

def nalm(lmax, mmax):
    return ((mmax+1)*(mmax+2))//2 + (mmax+1)*(lmax-mmax)

# Adri 2020 A25/A35
def mueller_to_C(mueller):
    T = np.zeros((4, 4), dtype=np.complex128)
    T[0, 0] = T[3, 3] = 1.0
    T[1, 1] = T[2, 1] = 1.0 / np.sqrt(2.0)
    T[1, 2] =  1j / np.sqrt(2.0)
    T[2, 2] = -1j / np.sqrt(2.0)
    return T.dot(mueller.dot(np.conj(T.T)))

def truncate_blm(inp, lmax, kmax, epsilon=1e-10):
    limit = epsilon * np.max(np.abs(inp))
    out = []
    for i in range(len(inp)):
        maxk = -1
        idx = 0
        for k in range(kmax+1):
            if np.max(np.abs(inp[i, :, idx:idx+lmax+1-k])) > limit:
                maxk = k
            idx += lmax+1-k
#        print("component",i,"maxk=",maxk)
        if maxk == -1:
            out.append(None)
        else:
            out.append((inp[i, :, : nalm(lmax, maxk)].copy(), maxk))
    return out

class MuellerConvolver:
    """Class for computing convolutions between arbitrary beams and skies in the
    presence of an optical element with arbitrary Mueller matrix in front of the
    detector.

    Most of the expressions in this code are derived from
    Duivenvoorden et al. 2021, MNRAS 502, 4526
    (https://arxiv.org/abs/2012.10437)

    Parameters
    ----------
    lmax : int
        maximum l moment of the provided sky and beam a_lm
    kmax : int
        maximum m moment of the provided beam a_lm
    slm : numpy.ndarray((n_comp, n_slm), dtype=complex)
        input sky a_lm
        ncomp can be 1, 3, or 4, for T, TEB, TEBV components, respectively.
        The components have the a_lm format used by healpy
    blm : numpy.ndarray((n_comp, n_blm), dtype=complex)
        input beam a_lm
        ncomp can be 1, 3, or 4, for T, TEB, TEBV components, respectively.
        The components have the a_lm format used by healpy
    mueller : np.ndarray((4,4), dtype=np.float64)
        Mueller matrix of the optical element in front of the detector
    single_precision : bool
        if True, store internal data in single precision, else double precision
    epsilon : float
        desired accuracy for the interpolation; a typical value is 1e-4
    npoints : int
        total number of irregularly spaced points you want to use this object for
        (only used for performance fine-tuning)
    sigma_min, sigma_max: float
        minimum and maximum allowed oversampling factors
        1.2 <= sigma_min < sigma_max <= 2.5
    nthreads : int
        the number of threads to use for computation
    """

    # Very simple class to store a_lm that allow negative m values
    class AlmPM:
        def __init__(self, lmax, mmax):
            if lmax < 0 or mmax < 0 or lmax < mmax:
                raise ValueError("bad parameters")
            self._lmax, self._mmax = lmax, mmax
            self._data = np.zeros((2*mmax+1, lmax+1), dtype=np.complex128)

        def __getitem__(self, lm):
            l, m = lm

            if isinstance(l, slice):
                if l.step is not None or l.start < 0 or l.stop-1 > self._lmax:
                    print(l, m)
                    raise ValueError("out of bounds read access")
            else:
                if l < 0 or l > self._lmax:  # or abs(m) > l:
                    print(l, m)
                    raise ValueError("out of bounds read access")
            # if we are asked for elements outside our m range, return 0
            if m < -self._mmax or m > self._mmax:
                return 0.0 + 0j
            return self._data[m+self._mmax, l]

        def __setitem__(self, lm, val):
            l, m = lm
            if isinstance(l, slice):
                if l.step is not None or l.start < 0 or l.stop-1 > self._lmax:
                    print(l, m)
                    raise ValueError("out of bounds write access")
            else:
                if l < 0 or l > self._lmax or abs(m) > l or abs(m) > self._mmax:
                    print(l, m)
                    raise ValueError("out of bounds write access")
            self._data[m+self._mmax, l] = val

    def mueller_tc_prep(self, blm, mueller, lmax, mmax):
        ncomp = blm.shape[0]

        # convert input blm to T/P/P*/V blm
        blm2 = [self.AlmPM(lmax, mmax+4) for _ in range(4)]
        idx = 0
        for m in range(mmax+1):
            sign = (-1)**m
            lrange = slice(m, lmax+1)
            idxrange = slice(idx, idx+lmax+1-m)
            # T component
            blm2[0][lrange, m] = blm[0, idxrange]
            blm2[0][lrange, -m] = np.conj(blm[0, idxrange]) * sign
            # V component
            if ncomp > 3:
                blm2[3][lrange, m] = blm[3, idxrange]
                blm2[3][lrange, -m] = np.conj(blm[3, idxrange]) * sign
            # E/B components
            if ncomp > 2:
                # Adri's notes [10], spin +2
                blm2[1][lrange, m] = -blm[1, idxrange] - 1j*blm[2, idxrange]
                # Adri's notes [9], spin -2
                blm2[2][lrange, m] = -blm[1, idxrange] + 1j*blm[2, idxrange]
                # negative m
                # Adri's notes [2]
                blm2[1][lrange, -m] = np.conj(blm2[2][lrange, m]) * sign
                blm2[2][lrange, -m] = np.conj(blm2[1][lrange, m]) * sign
            idx += lmax+1-m

        C = mueller_to_C(mueller)

        # compute the blm for the full beam+Mueller matrix system at angles
        # n*pi/5 for n in [0; 5[
        sqrt2 = np.sqrt(2.0)
        nbeam = 5
        inc = 4
        res = np.zeros((nbeam, ncomp, nalm(lmax, mmax+inc)), dtype=self._ctype)
        blm_eff = [self.AlmPM(lmax, mmax+4) for _ in range(4)]

        for ibeam in range(nbeam):
            alpha = ibeam*np.pi/nbeam
            e2ia = np.exp(2j*alpha)
            e2iac = np.exp(-2j*alpha)
            e4ia = np.exp(4j*alpha)
            e4iac = np.exp(-4j*alpha)
# FIXME: do I need to calculate anything for negative m?
            for m in range(-mmax-4, mmax+4+1):
                lrange = slice(abs(m), lmax+1)
                # T component, Marta notes [4a]
                blm_eff[0][lrange, m] = (
                      C[0, 0] * blm2[0][lrange, m]
                    + C[3, 0] * blm2[3][lrange, m]
                    + 1.0/sqrt2 * ((C[1, 0]*e2ia ) * blm2[2][lrange, m+2]
                                  +(C[2, 0]*e2iac) * blm2[1][lrange, m-2]))
                # E/B components, Marta notes [4b,c]
                blm_eff[1][lrange, m] = (
                      (sqrt2*e2iac) * (C[0, 1] * blm2[0][lrange, m+2]
                                     + C[3, 1] * blm2[3][lrange, m+2])
                    + (C[2, 1]*e4iac) * blm2[2][lrange, m+4]
                    +  C[1, 1]        * blm2[1][lrange, m])
                blm_eff[2][lrange, m] = (
                      (sqrt2*e2ia) * (C[0, 2] * blm2[0][lrange, m-2]
                                    + C[3, 2] * blm2[3][lrange, m-2])
                    + (C[1, 2]*e4ia) * blm2[1][lrange, m-4]
                    + C[2, 2]        * blm2[2][lrange, m])
                # V component, Marta notes [4d]
                blm_eff[3][lrange, m] = (
                      C[0, 3] * blm2[0][lrange, m]
                    + C[3, 3] * blm2[3][lrange, m]
                    + 1.0/sqrt2 * ((C[1, 3]*e2ia ) * blm2[2][lrange, m+2]
                                  +(C[2, 3]*e2iac) * blm2[1][lrange, m-2]))

            # TEMPORARY sanity check ...
            def check(xp, xm, lrange, m):
                sign = (-1)**m
                diff = xp[lrange, m] - sign*np.conj(xm[lrange, -m])
                return np.max(np.abs(diff)) <= 1e-4

            for m in range(0, mmax+4+1):
                sign = (-1)**m
                lrange = slice(abs(m), lmax+1)
                if not check(blm_eff[0], blm_eff[0], lrange, m):
                    raise RuntimeError("error T")
                if not check(blm_eff[1], blm_eff[2], lrange, m):
                    raise RuntimeError("error 12")
                if not check(blm_eff[2], blm_eff[1], lrange, m):
                    raise RuntimeError("error 21")
                if not check(blm_eff[3], blm_eff[3], lrange, m):
                    raise RuntimeError("error V")
            # ... up to here

            # back to original TEBV b_lm format
            idx = 0
            for m in range(mmax+inc+1):
                lrange = slice(m, lmax+1)
                idxrange = slice(idx, idx+lmax+1-m)
                # T component
                res[ibeam, 0, idxrange] = blm_eff[0][lrange, m]
                # P/P* components
                if ncomp > 2:
                    # Adri's notes [10]
                    res[ibeam, 1, idxrange] = -0.5 * (
                        blm_eff[1][lrange, m] + blm_eff[2][lrange, m]
                    )
                    res[ibeam, 2, idxrange] = 0.5j * (
                        blm_eff[1][lrange, m] - blm_eff[2][lrange, m]
                    )
                # V component
                if ncomp > 3:
                    res[ibeam, 3, idxrange] = blm_eff[3][lrange, m]
                idx += lmax+1-m

        return res

    # "Fourier transform" the blm at different alpha to obtain
    # blm(alpha) = out[0] + cos(2*alpha)*out[1] + sin(2*alpha)*out[2]
    #                     + cos(4*alpha)*out[3] + sin(4*alpha)*out[4]
    def pseudo_fft(self, inp):
        out = np.zeros((5, inp.shape[1], inp.shape[2]), dtype=self._ctype)
        out[0] = 0.2 * (inp[0]+inp[1]+inp[2]+inp[3]+inp[4])
        # FIXME: I'm not absolutely sure about the sign of the angles yet
        c1, s1 = np.cos(2*np.pi/5), np.sin(2*np.pi/5)
        c2, s2 = np.cos(4*np.pi/5), np.sin(4*np.pi/5)
        out[1] = 0.4 * (inp[0] + c1*(inp[1]+inp[4]) + c2*(inp[2]+inp[3]))
        out[2] = 0.4 * (         s1*(inp[1]-inp[4]) + s2*(inp[2]-inp[3]))
        out[3] = 0.4 * (inp[0] + c2*(inp[1]+inp[4]) + c1*(inp[2]+inp[3]))
        out[4] = 0.4 * (         s2*(inp[1]-inp[4]) - s1*(inp[2]-inp[3]))
        # Alternative way via real FFT
        # out2 = inp.copy()
        # out2 = out2.view(np.float64)
        # out2 = ducc0.fft.r2r_fftpack(
        #     out2, real2hermitian=True, forward=False, axes=(0,), out=out2
        # )
        # out2[0] *=0.2
        # out2[1:] *=0.4
        # out2 = out2.view(np.complex128)
        # print(np.max(np.abs(out-out2)))
        return out

    def __init__(self, *, lmax, kmax, slm, blm, mueller, single_precision=True,
                 epsilon=1e-4, npoints=1000000000,sigma_min=1.2, sigma_max=2.5,
                 nthreads=1):
        self._ftype = np.float32 if single_precision else np.float64
        self._ctype = np.complex64 if single_precision else np.complex128
        self._slm = slm.astype(self._ctype)
        self._lmax = lmax
        self._kmax = kmax
        tmp = self.mueller_tc_prep(blm, mueller, lmax, kmax)
        tmp = self.pseudo_fft(tmp)

        # construct the five interpolators for the individual components
        # All sets of blm are checked up to which kmax they contain significant
        # coefficients, and the interpolator is chosen accordingly
        tmp = truncate_blm(tmp, self._lmax, self._kmax + 4)

        self._inter = []
        intertype = (ducc0.totalconvolve.Interpolator_f
            if self._ctype == np.complex64
            else ducc0.totalconvolve.Interpolator)
        for i in range(5):
            if tmp[i] is not None:  # component is not zero
                self._inter.append(
                    intertype(self._slm, tmp[i][0], False, self._lmax,
                              tmp[i][1], epsilon=epsilon, npoints=npoints,
                              sigma_min=sigma_min, sigma_max=sigma_max,
                              nthreads=nthreads))
            else:  # we can ignore this component entirely
                self._inter.append(None)

    def signal(self, *, ptg, alpha):
        """Computes the convolved signal for a set of pointings and HWP angles.

        Parameters
        ----------
        ptg : numpy.ndarray((nptg, 3), dtype=float)
            the input pointings in radians, in (theta, phi, psi) order
        alpha : numpy.ndarray((nptg,), dtype=np.float)
            the HWP angles in radians

        Returns
        -------
        signal : numpy.ndarray((nptg,), dtype=np.float)
            the signal measured by the detector
        """
        ptg = ptg.astype(self._ftype)
        alpha = alpha.astype(self._ftype)
        if self._inter[0] is not None:
            res = self._inter[0].interpol(ptg)[0]
        else:
            res = np.zeros(ptg.shape[0], dtype=self._ftype)
        if self._inter[1] is not None:
            res += np.cos(2*alpha) * self._inter[1].interpol(ptg)[0]
        if self._inter[2] is not None:
            res += np.sin(2*alpha) * self._inter[2].interpol(ptg)[0]
        if self._inter[3] is not None:
            res += np.cos(4*alpha) * self._inter[3].interpol(ptg)[0]
        if self._inter[4] is not None:
            res += np.sin(4*alpha) * self._inter[4].interpol(ptg)[0]
        return res


import streamlit as st
from multipage import MultiApp
from apps import pandas_profiling_uk_houseprice, web_app_uk_houseprice

app = MultiApp()

st.set_page_config(
    page_title="UK House Price",
    layout="wide",
    initial_sidebar_state="expanded",
)

st.markdown("""
# UK House Price App
With the launch of Streamlit, developing a dashboard for your machine learning solution has been made incredibly easy.

Streamlit is an open source app framework specifically designed for ML engineers working with Python.
It allows you to create a stunning looking application with only a few lines of code.

I want to take this opportunity to demonstrate the apps you can build using Streamlit.
""")

# Add all your application here
app.add_app("Pandas Profiling UK Houseprice", pandas_profiling_uk_houseprice.app)
app.add_app("Trend UK Houseprice", web_app_uk_houseprice.app)
# app.add_app("Statistical Model", model.app)
# The main app
app.run()


list=[]
no_of_ele=int(input("enter number of elements:"))
for i in range(0,no_of_ele):
    ele=int(input())
    list.append(ele)
print(list)
sum=0
sum_from=int(input("enter the index to calculate sum from:"))
sum_to=int(input("enter the index to calculate sum to:"))
for i in range(sum_from,sum_to+1):
    sum+=list[i];
print(sum)


#!/usr/bin/env python2.5
import hildon
import gtk, gobject
import os
from album_art_spec import getCoverArtThumbFileName
from download_dialog import MussorgskyAlbumArtDownloadDialog
from utils import escape_html
from aa_selection_dialog import AlbumArtSelectionDialog, RESPONSE_CLICK

import time

EMPTY_PIXBUF = gtk.gdk.Pixbuf (gtk.gdk.COLORSPACE_RGB, False, 8, 64, 64)

import i18n
_ = i18n.language.gettext

class MussorgskyAlbumArtPanel (hildon.StackableWindow):

    def __init__ (self, album_artists):
        hildon.StackableWindow.__init__ (self)
        self.set_title (_("Album art selection"))
        self.set_border_width (12)
        self.__create_view ()
        self.downloader = None
        # Visible string, image, artist, album, painted!
        self.model = gtk.ListStore (str, gtk.gdk.Pixbuf, str, str, bool)
        for p in album_artists:
            if (not p[0]):
                continue
            t = (None, None, p[1], p[0], False)
            self.model.append (t)

        self.treeview.set_model (self.model)

    def __create_view (self):
        self.treeview = gtk.TreeView ()
        self.treeview.connect ("row-activated", self.row_activated_cb)

        artist_column = gtk.TreeViewColumn ("Artist", gtk.CellRendererText (), markup=0)
        artist_column.set_expand (True)
        self.treeview.append_column (artist_column)

        renderer = gtk.CellRendererPixbuf ()
        album_art = gtk.TreeViewColumn ("Album art", renderer, pixbuf=1)
        # This doesn't have real effect:
        album_art.set_sizing (gtk.TREE_VIEW_COLUMN_FIXED)
        album_art.set_fixed_width (64)

        album_art.set_cell_data_func (renderer, self.album_art_cell_data_cb)
        self.treeview.append_column (album_art)

        pannable_area = hildon.PannableArea ()
        pannable_area.add (self.treeview)
        self.add (pannable_area)

        # Menu
        menu = hildon.AppMenu ()
        automatic_retrieval = hildon.Button (hildon.BUTTON_STYLE_NORMAL,
                                             hildon.BUTTON_ARRANGEMENT_HORIZONTAL)
        automatic_retrieval.set_title (_("Automatic download"))
        automatic_retrieval.connect ("clicked", self.get_all_album_art)
        menu.append (automatic_retrieval)
        menu.show_all ()
        self.set_app_menu (menu)

    def album_art_cell_data_cb (self, column, cell, model, iter):
        text, pixbuf, artist, album, not_first_time = model.get (iter, 0, 1, 2, 3, 4)
        if (not_first_time):
            if (text == None):
                text = "".join (["", escape_html (album),"\n",
                                 escape_html(artist), ""])
                model.set (iter, 0, text)

            if (pixbuf == None):
                #print "Calling album art cell data cb", model.get (iter, 3)
                album_art_path = getCoverArtThumbFileName (album)
                if (os.path.exists (album_art_path)):
                    pxb = gtk.gdk.pixbuf_new_from_file_at_size (album_art_path, 64, 64)
                    model.set (iter, 1, pxb)
                else:
                    #print "Cannot find thumbnail in '%s'" % (album_art_path)
                    model.set (iter, 1, EMPTY_PIXBUF)

        else:
            model.set (iter, 4, True)

    def get_all_album_art (self, user_data):
        dialog = MussorgskyAlbumArtDownloadDialog (self)
        dialog.show_all ()
        dialog.do_the_job (self.model)

    def row_activated_cb (self, treeview, path, view_colum):
        it = treeview.get_model ().get_iter (path)
        artist = treeview.get_model ().get_value (it, 2)
        album = treeview.get_model ().get_value (it, 3)
        if (artist.find ('|') != -1):
            artist = "Various artists"

        dialog = AlbumArtSelectionDialog (self, artist, album, 5)
        dialog.show_all ()

        response = dialog.run ()
        if (response == RESPONSE_CLICK):
            (img, thumb) = dialog.get_selection ()
            if img and thumb:
                pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (thumb, 64, 64)
                treeview.get_model ().set (it, 1, pixbuf)
            else:
                treeview.get_model ().set (it, 1, EMPTY_PIXBUF)
        dialog.destroy ()

if __name__ == "__main__":
    import random

    artists_albums = [("Artist %d|artist Y" % i, "Album <%d>" % i) for i in range (0, 10)]

    # Overwrite the get thumb path for testing
    def local_file (path):
        return "../thumb%d.124.jpeg" % (random.randint (0, 3))

    global getCoverArtThumbFileName
    getCoverArtThumbFileName = local_file

    window = MussorgskyAlbumArtPanel (artists_albums)
    window.connect ("destroy", gtk.main_quit )
    window.show_all ()
    gtk.main ()


'🍟==============================『🍗 ʏօʊȶʊɮɛʟɨ 🍰』==============================🍟'
from IDLER.Trial import *
'🍟==============================『🍗 ʏօʊȶʊɮɛʟɨ 🍰』==============================🍟'
def extractYt(yturl):
    with ydl:
        resolutiontree = []
        r = ydl.extract_info(yturl,
                             download=False)
        for format in r['formats']:
            if not "dash" in str(format['format']).lower():
                resolutiontree.append(
                {
                    "format": format['format'],
                    "filesize": format['filesize'],
                    "format_id": format['format_id'],
                    "yturl": yturl
                    }
                )
        return r['title'], r['thumbnail'], resolutiontree
'🍟==============================『🍗 ʏօʊȶʊɮɛʟɨ 🍰』==============================🍟'


import matplotlib.pyplot as plt
import numpy as np

n_list = [1, 2, 3, 4, 5, 6, 7, 8]
a_list = [2804, 2530, 6313, 1682, 3310, 3608, 2647, 2278]
m_list = [2158, 939, 3512, 1179, 1685, 2298, 1559, 1310]

plt.plot(n_list, a_list, color='b', marker='.', label='std::map')
plt.plot(n_list, m_list, color='r', marker='.', label='RBtree')

plt.legend(loc=2, borderaxespad=1)

plt.xlabel('Test num')
plt.ylabel('Time(μs)')
plt.title('%%time')
# plt.grid(True)
plt.savefig("time.png")
plt.show()


import os.path
import shutil
from datetime import datetime
from pathlib import Path
import time
import hydra
import wandb
import numpy as np
import torch
from omegaconf import DictConfig, OmegaConf
from torch.cuda import amp
import torch.multiprocessing as mp
import torch.distributed as dist
from tqdm import tqdm
from torch.optim.lr_scheduler import LambdaLR

from dataload.data_load import load_data
from dataload.data_preprocess import prepare_preprocessed_data
from utils.utils import evaluate
from utils.metrics import *
from utils.common import *
from transformers import BertTokenizer
from transformers import AdamW
from torch.nn.parallel import DistributedDataParallel as DDP

### custom your wandb setting here ###
# os.environ["WANDB_API_KEY"] = ""
os.environ["WANDB_MODE"] = "offline"

def setup(rank, world_size):
    os.environ['MASTER_ADDR'] = 'localhost'
    os.environ['MASTER_PORT'] = '23342'
    dist.init_process_group("nccl", rank=rank, world_size=world_size)

def cleanup():
    dist.destroy_process_group()

def train(model, optimizer, dataloader, local_rank, world_size, cfg):
    model.train()
    torch.set_grad_enabled(True)

    mean_loss = torch.zeros(2).to(local_rank)
    acc_cnt = torch.zeros(2).to(local_rank)
    acc_cnt_pos = torch.zeros(2).to(local_rank)
    for cnt, (
            subgraph, mapping_idx, candidate_news, candidate_entity, entity_mask, batch_enc, batch_token, batch_attn,
            target,
            imp) \
            in enumerate(tqdm(dataloader,
                              total=int(cfg.num_epochs * (cfg.dataset.pos_count // cfg.batch_size + 1)),
                              desc=f"[{local_rank}] Training"), start=1):
        subgraph = subgraph.to(local_rank, non_blocking=True)
        mapping_idx = mapping_idx.to(local_rank, non_blocking=True)
        candidate_news = candidate_news.to(local_rank, non_blocking=True)
        imp = imp.to(local_rank, non_blocking=True)
        candidate_entity = candidate_entity.to(local_rank, non_blocking=True)
        entity_mask = entity_mask.to(local_rank, non_blocking=True)

        batch_enc = batch_enc.to(local_rank, non_blocking=True)
        batch_token = batch_token.to(local_rank, non_blocking=True)
        batch_attn = batch_attn.to(local_rank, non_blocking=True)
        target = target.to(local_rank, non_blocking=True)

        # batch_attn = batch_attn.to(local_rank, non_blocking=True)
        # batch_labs = batch_labs.to(local_rank, non_blocking=True)
        # batch_token = batch_token.to(local_rank, non_blocking=True)
        # '[P1][P2][P3][nsep] 0 [nsep] 1 [nsep] 2 [nsep] 3 [nsep] 4 [nsep] 5[SEP][Q1][Q2][Q3]0[SEP]Does the user click the news
        # sentence = [[d["sentence"] for d in sublist] for sublist in batch_enc]

        loss, scores = model(subgraph, mapping_idx, candidate_news, candidate_entity, entity_mask, batch_enc,
                             batch_token, batch_attn, target, imp)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        print("loss: ", loss.item())
        print("score: ", scores[10])
        mean_loss[0] += loss.item()
        mean_loss[1] += 1

        predict = torch.argmax(scores.detach(), dim=1)
        num_correct = (predict == target).sum()
        acc_cnt[0] += num_correct
        acc_cnt[1] += predict.size(0)

        positive_idx = torch.where(target == 1)[0]
        num_correct_pos = (predict[positive_idx] == target[positive_idx]).sum()
        acc_cnt_pos[0] += num_correct_pos
        acc_cnt_pos[1] += positive_idx.size(0)

    dist.all_reduce(mean_loss, op=dist.ReduceOp.SUM)
    dist.all_reduce(acc_cnt, op=dist.ReduceOp.SUM)
    dist.all_reduce(acc_cnt_pos, op=dist.ReduceOp.SUM)

    loss_epoch = mean_loss[0] / mean_loss[1]
    acc = acc_cnt[0] / acc_cnt[1]
    acc_pos = acc_cnt_pos[0] / acc_cnt_pos[1]
    pos_ratio = acc_cnt_pos[1] / acc_cnt[1]

    return loss_epoch.item(), acc.item(), acc_pos.item(), pos_ratio.item()

def eval(model, local_rank, dataloader, world_size, cfg):
    model.eval()
    # data_loader = tqdm(data_loader)
    val_scores = []
    acc_cnt = torch.zeros(2).to(local_rank)
    acc_cnt_pos = torch.zeros(2).to(local_rank)
    imp_ids = []
    labels = []
    for cnt, (
            subgraph, mapping_idx, clicked_entity, candidate_news, candidate_entity, entity_mask, batch_enc,
            batch_token, batch_attn, target,
            imp) \
            in enumerate(tqdm(dataloader,
                              total=int(cfg.num_epochs * (cfg.dataset.pos_count // cfg.batch_size + 1)),
                              desc=f"[{local_rank}] Training"), start=1):
        imp_ids = imp_ids + imp
        labels = labels + target.cpu().numpy().tolist()

        subgraph = subgraph.to(local_rank, non_blocking=True)
        mapping_idx = mapping_idx.to(local_rank, non_blocking=True)
        candidate_news = candidate_news.to(local_rank, non_blocking=True)
        imp = imp.to(local_rank, non_blocking=True)
        candidate_entity = candidate_entity.to(local_rank, non_blocking=True)
        entity_mask = entity_mask.to(local_rank, non_blocking=True)

        batch_enc = batch_enc.to(local_rank, non_blocking=True)
        batch_token = batch_token.to(local_rank, non_blocking=True)
        batch_attn = batch_attn.to(local_rank, non_blocking=True)
        target = target.to(local_rank, non_blocking=True)

        # batch_attn = batch_attn.to(local_rank, non_blocking=True)
        # batch_labs = batch_labs.to(local_rank, non_blocking=True)
        # batch_token = batch_token.to(local_rank, non_blocking=True)
        # '[P1][P2][P3][nsep] 0 [nsep] 1 [nsep] 2 [nsep] 3 [nsep] 4 [nsep] 5[SEP][Q1][Q2][Q3]0[SEP]Does the user click the news
        # sentence = [[d["sentence"] for d in sublist] for sublist in batch_enc]

        loss, scores = model.module.validation_process(subgraph, mapping_idx, clicked_entity, candidate_news,
                                                       candidate_entity, entity_mask, batch_enc,
                                                       batch_token, batch_attn, target, imp)
        ranking_scores = scores[:, 1].detach()
        val_scores.append(ranking_scores)

        predict = torch.argmax(scores.detach(), dim=1)
        num_correct = (predict == target).sum()
        acc_cnt[0] += num_correct
        acc_cnt[1] += predict.size(0)

        positive_idx = torch.where(target == 1)[0]
        num_correct_pos = (predict[positive_idx] == target[positive_idx]).sum()
        acc_cnt_pos[0] += num_correct_pos
        acc_cnt_pos[1] += positive_idx.size(0)

    dist.all_reduce(acc_cnt, op=dist.ReduceOp.SUM)
    dist.all_reduce(acc_cnt_pos, op=dist.ReduceOp.SUM)

    acc = acc_cnt[0] / acc_cnt[1]
    acc_pos = acc_cnt_pos[0] / acc_cnt_pos[1]
    pos_ratio = acc_cnt_pos[1] / acc_cnt[1]

    val_scores = torch.cat(val_scores, dim=0)
    val_impids = torch.IntTensor(imp_ids).to(local_rank)
    val_labels = torch.IntTensor(labels).to(local_rank)

    val_scores_list = [torch.zeros_like(val_scores).to(local_rank) for _ in range(world_size)]
    val_impids_list = [torch.zeros_like(val_impids).to(local_rank) for _ in range(world_size)]
    val_labels_list = [torch.zeros_like(val_labels).to(local_rank) for _ in range(world_size)]

    dist.all_gather(val_scores_list, val_scores)
    dist.all_gather(val_impids_list, val_impids)
    dist.all_gather(val_labels_list, val_labels)

    return val_scores_list, acc.item(), acc_pos.item(), pos_ratio.item(), val_impids_list, val_labels_list

def load_tokenizer(cfg):
    tokenizer = BertTokenizer.from_pretrained(cfg.token.bertmodel)
    conti_tokens1 = []
    for i in range(cfg.token.num_conti1):
        conti_tokens1.append('[P' + str(i + 1) + ']')
    conti_tokens2 = []
    for i in range(cfg.token.num_conti2):
        conti_tokens2.append('[Q' + str(i + 1) + ']')

    new_tokens = ['[NSEP]']
    tokenizer.add_tokens(new_tokens)

    conti_tokens = conti_tokens1 + conti_tokens2
    tokenizer.add_tokens(conti_tokens)

    new_vocab_size = len(tokenizer)
    cfg.token.vocab_size = new_vocab_size
    # cfg.token.tokenizer = tokenizer
    return tokenizer, conti_tokens1, conti_tokens2

def main_worker(local_rank, cfg):
    # -----------------------------------------Environment Initial
    seed_everything(cfg.seed)  # 设置随机数种子
    dist.init_process_group(backend='nccl',
                            init_method='tcp://127.0.0.1:23456',
                            world_size=cfg.gpu_num,
                            rank=local_rank)  # 表明其在整个分布式环境中的排名和标识
    world_size = cfg.gpu_num

    # -----------------------------------------Dataset & Model Load
    num_training_steps = int(cfg.num_epochs * cfg.dataset.pos_count / (cfg.batch_size * cfg.accumulation_steps))
    num_warmup_steps = int(num_training_steps * cfg.warmup_ratio + 1)

    tokenizer, conti_tokens1, conti_tokens2 = load_tokenizer(cfg)
    conti_tokens = [conti_tokens1, conti_tokens2]

    # val_dataloader = load_data(cfg, mode='val',local_rank=local_rank, tokenizer=tokenizer, conti_tokens=conti_tokens)
    model = load_model(cfg, tokenizer).to(local_rank)
    net = DDP(model, device_ids=[local_rank])
    # optimizer = torch.optim.Adam(model.parameters(), lr=cfg.optimizer.lr)

    # AdamW
    no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
    optimizer_grouped_parameters = [
        {'params': [p for n, p in net.named_parameters() if not any(nd in n for nd in no_decay)],
         'weight_decay': cfg.train.wd},
        {'params': [p for n, p in net.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
    ]
    optimizer = AdamW(params=optimizer_grouped_parameters, lr=cfg.train.lr)

    # lr_lambda = lambda step: 1.0 if step > num_warmup_steps else step / num_warmup_steps
    # scheduler = LambdaLR(optimizer, lr_lambda)

    # ------------------------------------------Load Checkpoint & optimizer

    # model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[local_rank])

    train_dataloader = load_data(cfg, mode='train', model=net, local_rank=local_rank, tokenizer=tokenizer,
                                 conti_tokens=conti_tokens)  # 加载训练数据
    # val_dataloader = load_data(cfg, mode='val', model=net, local_rank=local_rank, tokenizer=tokenizer,
    #                              conti_tokens=conti_tokens)
    metrics = ['auc', 'mrr', 'ndcg5', 'ndcg10']
    best_val_result = {}
    best_val_epoch = {}
    for m in metrics:
        best_val_result[m] = 0.0
        best_val_epoch[m] = 0

    for epoch in range(cfg.num_epochs):
        # #################################  train  ###################################
        st_tra = time.time()
        if local_rank == 0:
            print('--------------------------------------------------------------------')
            print('start training: ', datetime.now())
            print('Epoch: ', epoch)
            print('lr:', optimizer.state_dict()['param_groups'][0]['lr'])
        loss, acc_tra, acc_pos_tra, pos_ratio_tra = \
            train(net, optimizer, train_dataloader, local_rank, world_size, cfg)
        # scheduler.step()

        end_tra = time.time()
        train_spend = (end_tra - st_tra) / 3600
        if local_rank == 0:
            print("Train Loss: %0.4f" % loss)
            print("Train ACC: %0.4f\tACC-Positive: %0.4f\tPositiveRatio: %0.4f\t[%0.2f]" %
                  (acc_tra, acc_pos_tra, pos_ratio_tra, train_spend))
            if cfg.train.model_save:
                file = cfg.path.ckp_dir + '/Epoch-' + str(epoch) + '.pt'
                print('save file', file)
                torch.save(net.module.state_dict(), file)

        # #################################  val  ###################################
        st_val = time.time()
        val_scores, acc_val, acc_pos_val, pos_ratio_val, val_impids, val_labels = \
            eval(net, local_rank, val_dataloader, world_size, cfg)
        impressions = {}  # {1: {'score': [], 'lab': []}}
        for i in range(world_size):
            scores, imp_id, labs = val_scores[i], val_impids[i], val_labels[i]
            assert scores.size() == imp_id.size() == labs.size()
            scores = scores.cpu().numpy().tolist()
            imp_id = imp_id.cpu().numpy().tolist()
            labs = labs.cpu().numpy().tolist()
            for j in range(len(scores)):
                sco, imp, lab = scores[j], imp_id[j], labs[j]
                if imp not in impressions:
                    impressions[imp] = {'score': [], 'lab': []}
                    impressions[imp]['score'].append(sco)
                    impressions[imp]['lab'].append(lab)
                else:
                    impressions[imp]['score'].append(sco)
                    impressions[imp]['lab'].append(lab)

        predicts, truths = [], []
        for imp in impressions:
            sims, labs = impressions[imp]['score'], impressions[imp]['lab']
            sl_zip = sorted(zip(sims, labs), key=lambda x: x[0], reverse=True)
            sort_sims, sort_labs = zip(*sl_zip)
            predicts.append(list(range(1, len(sort_labs) + 1, 1)))
            truths.append(sort_labs)

        auc_val, mrr_val, ndcg5_val, ndcg10_val = evaluate(predicts, truths)
        if auc_val > best_val_result['auc']:
            best_val_result['auc'] = auc_val
            best_val_epoch['auc'] = epoch
        if mrr_val > best_val_result['mrr']:
            best_val_result['mrr'] = mrr_val
            best_val_epoch['mrr'] = epoch
        if ndcg5_val > best_val_result['ndcg5']:
            best_val_result['ndcg5'] = ndcg5_val
            best_val_epoch['ndcg5'] = epoch
        if ndcg10_val > best_val_result['ndcg10']:
            best_val_result['ndcg10'] = ndcg10_val
            best_val_epoch['ndcg10'] = epoch
        end_val = time.time()
        val_spend = (end_val - st_val) / 3600

        if local_rank == 0:
            print("Validate: AUC: %0.4f\tMRR: %0.4f\tnDCG@5: %0.4f\tnDCG@10: %0.4f\t[Val-Time: %0.2f]" %
                  (auc_val, mrr_val, ndcg5_val, ndcg10_val, val_spend))
            print('Best Result: AUC: %0.4f \tMRR: %0.4f \tNDCG@5: %0.4f \t NDCG@10: %0.4f' %
                  (best_val_result['auc'], best_val_result['mrr'], best_val_result['ndcg5'], best_val_result['ndcg10']))
            print('Best Epoch: AUC: %d \tMRR: %d \tNDCG@5: %d \t NDCG@10: %d' %
                  (best_val_epoch['auc'], best_val_epoch['mrr'], best_val_epoch['ndcg5'], best_val_epoch['ndcg10']))
        dist.barrier()
    if local_rank == 0:
        best_epochs = [best_val_epoch['auc'], best_val_epoch['mrr'], best_val_epoch['ndcg5'], best_val_epoch['ndcg10']]
        best_epoch = max(set(best_epochs), key=best_epochs.count)
        if cfg.train.model_save:
            old_file = cfg.path.ckp_dir + '/Epoch-' + str(best_epoch) + '.pt'
            # old_file = args.save_dir + '/Epoch-2.pt'
            if not os.path.exists('./temp'):
                os.makedirs('./temp')
            copy_file = './temp' + '/BestModel.pt'
            shutil.copy(old_file, copy_file)
            print('Copy ' + old_file + ' >>> ' + copy_file)

    if local_rank == 0:
        wandb.init(config=OmegaConf.to_container(cfg, resolve=True),
                   project=cfg.logger.exp_name, name=cfg.logger.run_name)
        print(model)

    if local_rank == 0:
        wandb.finish()
    cleanup()

@hydra.main(version_base="1.2", config_path=os.path.join(get_root(), "configs"), config_name="small")
def main(cfg: DictConfig):
    seed_everything(cfg.seed)
    # cfg.model='GLORY'
    # cfg.dataset='MINDSmall.yaml'
    # cfg.reprocess=True
    cfg.gpu_num = torch.cuda.device_count()
    prepare_preprocessed_data(cfg)
    mp.spawn(main_worker, nprocs=cfg.gpu_num, args=(cfg,))  # 这行代码的目的是使用 PyTorch 的分布式训练功能，以并行方式执行名为 main_worker 的函数。
    # 每个工作进程都会执行相同的 main_worker 函数，但可以根据配置中的参数来控制不同的行为

if __name__ == "__main__":
    main()


# Create text file which contains all your friends names, display the count
# of each alphabet present in file
#
#                ex:        chaitanya
#
#                               c-1, h-1,a-3,i-1,t-1,n-1,y-1
f=open("lab_internal_2.txt","w")
f.write("Marepally Saiteja\nKumar teja\nNikhil Krishna")
f.close()
def count_letters(text):
    result={}
    for letter in text:

        if letter not in result:
            result[letter]=0
        result[letter]+=1
    return result

f=open("lab_internal_2.txt",'r')
for i in f:
    s=i[:len(i)+1]
    print(s,':',count_letters(s))
f.close()


from functools import partial
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from rlpyt.models.mlp import MlpModel
from rlpyt.utils.tensor import infer_leading_dims, restore_leading_dims
from rlpyt.utils.collections import namedarraytuple
from rlpyt.utils.quick_args import save__init__args

from sfgen.babyai.modules import initialize_parameters, ObservationLSTM
from sfgen.babyai.visual_goal_generator import VisualGoalGenerator
from sfgen.babyai.babyai_model import BabyAIModel
from sfgen.tools.ops import duplicate_vector
from sfgen.tools.ops import check_for_nan_inf
RnnState = namedarraytuple("RnnState", ["h_obs", "c_obs", "h_goal", "c_goal"])

class SFGenModel(BabyAIModel):
    """
    """
    def __init__(
        self,
        output_size,
        pre_mod_layer=False, # whether to apply layer to goal/task before obtaining cnn weights
        obs_in_state=True,
        goal_in_state=True,
        independent_compression=False,
        goal_use_history=False,
        normalize_history=False,
        normalize_goal=False,
        mod_function='sigmoid',
        mod_compression='maxpool',
        goal_tracking='lstm',
        goal_size=256,
        history_size=256,
        goal_hist_depth=0,
        lstm_size=256,
        head_size=256,
        gvf_size=256,
        nheads=1,
        default_size=None,
        obs_fc_size=256,
        dueling=False,
        rlhead='dqn',
        **kwargs
        ):
        """
        """
        super(SFGenModel, self).__init__(**kwargs)
        # optionally keep everything same dimension and just scale
        if default_size is not None:
            goal_size = default_size
            history_size = default_size
            lstm_size = default_size
            head_size = default_size
            gvf_size = default_size
            obs_fc_size = default_size

        save__init__args(locals())
        assert dueling == False, "Successor doesn't support dueling currently"

        self.observation_memory = ObservationLSTM(
            conv_feature_dims=self.conv.output_dims,
            lstm_size=lstm_size,
            fc_size=obs_fc_size,
            # action dim + reward + direction
            extra_input_dim=output_size+1+self.direction_embed_size,
        )

        # goal_dim = self.conv.output_dims[0] # number of channels
        task_dim = self.text_embed_size
        self.goal_generator = VisualGoalGenerator(
            conv_feature_dims=self.conv.output_dims,
            task_dim=task_dim,
            goal_dim=goal_size,
            history_dim=history_size,
            pre_mod_layer=pre_mod_layer,
            mod_function=mod_function,
            mod_compression=mod_compression,
            independent_compression=independent_compression,
            goal_tracking=goal_tracking,
            use_history=goal_use_history,
            nonlinearity=self.nonlinearity_fn,
            nheads=nheads,
            normalize_goal=normalize_goal,
        )

        goal_hist_dim = self.goal_generator.hist_dim
        if goal_hist_depth == 0:
            self.goal_history_embedder = lambda x:x
        elif goal_hist_depth == 1:
            self.goal_history_embedder = nn.Linear(goal_hist_dim, goal_hist_dim)
        else:
            self.goal_history_embedder = MlpModel(
                        input_size=goal_hist_dim,
                        hidden_sizes=[goal_hist_dim]*(goal_hist_depth-1),
                        output_size=goal_hist_dim,
                        nonlinearity=self.nonlinearity_fn,
                        )

        gvf_input_dim = int(nheads*self.goal_generator.hist_dim + task_dim)
        if goal_in_state:
            gvf_input_dim += int(nheads*self.goal_generator.goal_dim)

        self.goal_gvf = MlpModel(input_size=gvf_input_dim,
            hidden_sizes=[gvf_size] if gvf_size else [],
            output_size=output_size*head_size,
            nonlinearity=self.nonlinearity_fn,
            )

        # self.goal_prediction_head = MlpModel(input_size, head_size, output_size=head_size*output_size)
        # successor_features = self.successor_head(state.view(T*B, -1)).view(T*B, self.output_size, self.head_size)

        if obs_in_state:
            input_size = head_size + lstm_size
        else:
            input_size = head_size

        if rlhead == 'dqn':
            self.rl_head = DqnGvfHead(
                input_size=input_size,
                head_size=head_size,
                output_size=output_size,
                task_dim=self.text_embed_size,
                nonlinearity=self.nonlinearity_fn,
                )
        elif rlhead == 'ppo':
            raise NotImplementedError("PPO")

        else:
            raise RuntimeError(f"Unsupported:'{rlhead}'")

    def forward(self, observation, prev_action, prev_reward, init_rnn_state, all_variables=False):
        """Feedforward layers process as [T*B,H]. Return same leading dims as
        input, can be [T,B], [B], or []."""

        variables=dict()
        lead_dim, T, B, img_shape = infer_leading_dims(observation.image, 3)

        image_embedding, mission_embedding, direction_embedding = self.process_observation(observation)

        variables['image_embedding'] = image_embedding
        variables['mission_embedding'] = mission_embedding
        variables['direction_embedding'] = direction_embedding
        # ======================================================
        # pass CNN output through Observation LSTM
        # ======================================================

        non_obs_inputs = [e for e in [direction_embedding] if e is not None]
        non_obs_inputs.extend([prev_action, prev_reward])
        obs_mem_outputs, (h_obs, c_obs) = self.observation_memory(
            obs_emb=image_embedding,
            init_lstm_inputs=non_obs_inputs,
            init_rnn_state=(init_rnn_state.h_obs, init_rnn_state.c_obs) if init_rnn_state is not None else None
            )

        # ======================================================
        # pass CNN output + task embedding output throught goal generator
        # ======================================================
        goal, goal_history, (h_goal, c_goal) = self.goal_generator(
            obs_emb=image_embedding,
            task_emb=mission_embedding,
            init_goal_state=(init_rnn_state.h_goal, init_rnn_state.c_goal) if init_rnn_state is not None else None
            )
        # Model should always leave B-dimension in rnn state: [N,B,H].
        # will reuse "RNN" state for sum/lstm goal trackers
        next_rnn_state = RnnState(h_obs=h_obs, c_obs=c_obs, h_goal=h_goal, c_goal=c_goal)

        # -----------------------
        # put into variables dictionary
        # -----------------------
        variables['goal'] = goal
        goal_history = self.goal_history_embedder(goal_history)
        variables['normalized_history'] = self.normalize_history
        if self.normalize_history:
            goal_history = F.normalize(goal_history + 1e-12, p=2, dim=-1)
        variables['goal_history'] = goal_history
        check_for_nan_inf(goal_history)

        # -----------------------
        # flatten
        # -----------------------
        # T X B x N x D --> # T X B x ND
        goal_history = goal_history.view(T, B, -1)
        goal = goal.view(T, B, -1)

        # ======================================================
        # Compute Predictive Features using history of goals
        # + task
        # ======================================================
        if self.goal_in_state:
            goal_pred_input = torch.cat((goal, goal_history, mission_embedding), dim=-1)
        else:
            goal_pred_input = torch.cat((goal_history, mission_embedding), dim=-1)
        goal_predictions = self.goal_gvf(goal_pred_input)
        # TB x |A| x D
        goal_predictions = goal_predictions.view(T, B, self.output_size, self.head_size)

        variables['goal_predictions'] = goal_predictions

        # ======================================================
        # get output of RL head
        # ======================================================
        if self.obs_in_state:
            state_action = torch.cat((goal_predictions, duplicate_vector(obs_mem_outputs, n=self.output_size, dim=2)), dim=-1)

        else:
            state_action = goal_predictions

        if all_variables:
            self.rl_head(state_action, mission_embedding,
                final_fn=partial(restore_leading_dims, lead_dim=lead_dim, T=T, B=B),
                variables=variables)
            return variables
        else:
            rl_out = self.rl_head(state_action, mission_embedding)
            # Restore leading dimensions: [T,B], [B], or [], as input.
            rl_out = restore_leading_dims(rl_out, lead_dim, T, B)
            return list(rl_out) + [next_rnn_state]

class DqnGvfHead(torch.nn.Module):
    """docstring for DQNHead"""
    def __init__(self, input_size, head_size, output_size, task_dim, nonlinearity=torch.nn.ReLU, **kwargs):
        super(DqnGvfHead, self).__init__()
        self.head_size = head_size
        self.output_size = output_size
        self.successor_head = MlpModel(input_size, head_size, nonlinearity=nonlinearity, output_size=head_size)
        self.task_weights = nn.Linear(task_dim, head_size)

    def forward(self, state_action, task, final_fn=lambda x:x, variables=None):
        """
        """
        T, B, A = state_action.shape[:3]

        # TBA x H
        successor_features = self.successor_head(state_action.view(T*B*A, -1))
        # T X B X A X D
        successor_features = successor_features.view(T, B, A, self.head_size)

        # T X B X D
        weights = self.task_weights(task)
        # T X B X 1 X D
        weights = weights.unsqueeze(2)
        # T X B X D X 1 (for dot-product)
        weights = weights.transpose(-2, -1)

        # dot product
        q_values = torch.matmul(successor_features, weights).squeeze(-1)

        q_values = q_values.view(T*B, -1)
        q_values = final_fn(q_values)
        check_for_nan_inf(q_values)

        if variables is not None:
            variables['q'] = q_values
        else:
            return [q_values]


from sys import stdin

def solve():
    n, m = map(int, stdin.readline().split())
    seq = stdin.readline().strip()
    m = min(n * 2, m)
    seq = list(seq)
    next = seq[:]

    for cnt in range(m):
        for idx in range(n):
            if idx == 0 and seq[1] == '1':
                next[idx] = '1'
            elif idx == n - 1 and seq[n - 2] == '1':
                next[idx] = '1'
            elif 0 < idx < n - 1 and seq[idx] == '0':
                if seq[idx - 1] == '1' or seq[idx + 1] == '1':
                    if not (seq[idx - 1] == '1' and seq[idx + 1] == '1'):
                        next[idx] = '1'
        seq = next[:]
    return ''.join(seq)

t = int(stdin.readline().strip())
for _ in range(t):
    print(solve())

Codeforces/210530 Deltix/A. Game of Life.py


from time import sleep

print('gamburg ' * 3)
i = 0
while i < 10:
    sleep(0.5)
    print(i)
    i += 1
    if i > 10:
        break

j = 0
print('second try; ' * 3)
for j in range(20):
   sleep(0.1)
   print(j)
   continue

c = 0
print('third try; ' * 3)
for c in 1,2,3:
    print(c)

print('if/else ' * 3)
if i == 9:
    print('NS')
elif i == 10:
    print('NSS')
elif i != 9:
        print('MN')
else:
    print("NO")

    #range(10) - povtor 10 raz
    #continue - cnova, break - vihod


##!/usr/bin/env python3

import argparse
import csv
import os
import time

from asnake.aspace import ASpace
from asnake.utils import walk_tree

FILENAME = "out.csv"
NOTE_TYPE_CHOICES = ["bioghist", "accessrestrict", "odd", "abstract", "arrangement", "userestrict", "fileplan", "acqinfo", "langmaterial", "physdesc", "phystech", "prefercite", "processinfo", "relatedmaterial", "scopecontent", "separatedmaterial"]

def process_tree(args, resource_id):
    """Iterates through a collection with note type and resource ID provided by user input. Writes Collection Title, Finding Aid Number, URI, Object Title, Date, Note Content, Container ID/Box Number, Folder, and Location to a csv file."""
    for record in walk_tree(resource_id, client):
        for note in record['notes']:
            if note['type'] == args.note_type:
                    for subnote in note['subnotes']:
                        content = subnote['content']
                        try:
                            expression = record['dates'][0]['expression']
                        except (KeyError, IndexError):
                            expression = "undated"
                        for instance in record['instances']:
                            sub_container = instance['sub_container']
                            top_container = client.get(instance['sub_container']['top_container']['ref']).json()
                            try:
                                container = "{} {}".format(
                                top_container['type'].capitalize(), top_container['indicator'])
                            except KeyError:
                                container = "None"
                            try:
                                container_2 = "{} {}".format(sub_container['type_2'].capitalize(), sub_container['indicator_2'])
                            except KeyError:
                                container_2 = "None"
                            location = ""
                            for loc in top_container['container_locations']:
                                try:
                                    l = client.get(loc['ref']).json()
                                    location = l['title']
                                except KeyError:
                                    location = "None"
                            writer.writerow([resource_id['title'], resource_id['id_0'], record['uri'], record['title'], record['level'], expression, content, container, container_2, location])

def get_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("note_type", choices=NOTE_TYPE_CHOICES, help="The type of note within a finding aid. For example: accessrestrict.")
    parser.add_argument("resource_id", type=int, help="The identifier of the resource record in which you want to write info to a csv. Found in the URL.")
    return parser

def main(client, writer):
     #"""Main function, which is run when this script is executed"""
    start_time = time.time()
    parser = get_parser()
    args = parser.parse_args()
    writer.writerow(["Collection Title", "Finding Aid Number", "URI", "Object Title", "Level", "Date", "Note Content", "Container ID/Box Number", "Folder", "Location"])
    process_tree(args, client.get(f'/repositories/2/resources/{(args.resource_id)}').json())
    elapsed_time = time.time() - start_time
    print("Time Elapsed: " + time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))

if __name__ == "__main__":
    client = ASpace().client
    spreadsheet_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), FILENAME)
    writer = csv.writer(open(spreadsheet_path, "w"))
    main(client, writer)