"""Authors: Ben Dichter, Cody Baker."""
import os
from glob import glob
import numpy as np
import pandas as pd
from lxml import etree as et
from pynwb import NWBFile
from pynwb.behavior import SpatialSeries
from pynwb.ecephys import ElectricalSeries, LFP, SpikeEventSeries
from hdmf.backends.hdf5.h5_utils import H5DataIO
from hdmf.data_utils import DataChunkIterator
from pynwb.misc import AnnotationSeries
from tqdm import tqdm
from .utils import check_module
from typing import Optional, List, Iterable
import sys
if sys.version >= '3.8':
    from typing import ArrayLike
else:
    from numpy import ndarray
    from typing import Union, Sequence
    # adapted from numpy typing
    ArrayLike = Union[bool, int, float, complex, list, ndarray, Sequence]

def load_xml(xml_filepath: str):
    """Fetch the xml data from the filepath.

    Parameters
    ----------
    xml_filepath : str
        Absolute filepath for the xml document.

    Returns
    -------
    TYPE
        lxml.etree root object of the xml document.

    """
    return et.parse(xml_filepath).getroot()

def get_channel_groups(session_path: str, xml_filepath: Optional[str] = None):
    """Retrieve all channel ids and their group structure in the Neuroscope xml.

    Parameters
    ----------
    session_path: str
    xml_filepath: None | str (optional)

    Returns
    -------
    list(list(int))

    """
    if xml_filepath is None:
        fpath_base, fname = os.path.split(session_path)
        xml_filepath = os.path.join(session_path, fname + '.xml')

    assert os.path.isfile(xml_filepath), "No .xml file found at the path location!" \
                                         "Unable to retrieve channel_groups."

    root = load_xml(xml_filepath)
    channel_groups = [[int(channel.text)
                      for channel in group.findall('channel')]
                      for group in root.find('anatomicalDescription').find('channelGroups').findall('group')]

    return channel_groups

def get_shank_channels(session_path: str, xml_filepath: Optional[str] = None):
    """Retrieve the channel ids belonging to the shanks in Neuroscope xml.

    Same as first 'nshanks' elements of get_channel_groups(...).

    Parameters
    ----------
    session_path: str
    xml_filepath: None | str (optional)

    Returns
    -------
    list(list(int))

    """
    if xml_filepath is None:
        fpath_base, fname = os.path.split(session_path)
        xml_filepath = os.path.join(session_path, fname + '.xml')

    assert os.path.isfile(xml_filepath), "No .xml file found at the path location!" \
                                         "Unable to retrieve shank_channels."

    root = load_xml(xml_filepath)
    shank_channels = [[int(channel.text)
                      for channel in group.find('channels')]
                      for group in root.find('spikeDetection').find('channelGroups').findall('group')]
    shank_channels = None

    return shank_channels

def get_lfp_sampling_rate(session_path: str, xml_filepath: Optional[str] = None):
    """Read the LFP Sampling Rate from the xml parameter file of the Neuroscope format.

    Parameters
    ----------
    session_path: str
    xml_filepath: None | str (optional)

    Returns
    -------
    fs: float

    """
    if xml_filepath is None:
        session_name = os.path.split(session_path)[1]
        xml_filepath = os.path.join(session_path, session_name + '.xml')

    assert os.path.isfile(xml_filepath), "No .xml file found at the path location!" \
                                         "Unable to retrieve lfp_sampling_rate."

    root = load_xml(xml_filepath)
    lfp_sampling_rate = float(root.find('fieldPotentials').find('lfpSamplingRate').text)

    return lfp_sampling_rate

def add_position_data(nwbfile: NWBFile, session_path: str, fs: float = 1250./32.,
                      names=('x0', 'y0', 'x1', 'y1')):
    """Read raw position sensor data from .whl file.

    Parameters
    ----------
    nwbfile: pynwb.NWBFile
    session_path: str
    fs: float
        sampling rate
    names: iterable
        names of column headings

    """
    session_name = os.path.split(session_path)[1]
    whl_path = os.path.join(session_path, session_name + '.whl')
    if not os.path.isfile(whl_path):
        print(whl_path + ' file not found!')
        return
    df = pd.read_csv(whl_path, sep='\t', names=names)

    nwbfile.add_acquisition(
        SpatialSeries('position_sensor0',
                      H5DataIO(df[['x0', 'y0']].values, compression='gzip'),
                      'unknown', description='raw sensor data from sensor 0',
                      starting_time=0., rate=fs,
                      resolution=np.nan))

    nwbfile.add_acquisition(
        SpatialSeries('position_sensor1',
                      H5DataIO(df[['x1', 'y1']].values, compression='gzip'),
                      'unknown', description='raw sensor data from sensor 1',
                      starting_time=0., rate=fs,
                      resolution=np.nan))

def read_spike_times(session_path: str, shankn: int, fs: float = 20000.):
    """Read .res files to get spike times.

    Parameters
    ----------
    session_path: str | path
    shankn: int
        shank number (1-indexed)
    fs: float
        sampling rate. default = 20000.

    Returns
    -------
    list(float)
    """
    _, session_name = os.path.split(session_path)
    timing_file = os.path.join(session_path, session_name + '.res.' + str(shankn))
    timing_df = pd.read_csv(timing_file, names=('time',))

    return timing_df.values.ravel() / fs

def read_spike_clustering(session_path: str, shankn: int):
    """Read .clu files to get spike cluster assignments for a single shank.

    Parameters
    ----------
    session_path: str | path
    shankn: int
        shank number (1-indexed)

    Returns
    -------
    np.ndarray

    """
    session_name = os.path.split(session_path)[1]
    id_file = os.path.join(session_path, session_name + '.clu.' + str(shankn))
    id_df = pd.read_csv(id_file, names=('id',))
    # The first number is the number of unique ids,
    # including 0 as an unsorted cluster and 1 as mult-unit activity
    id_df = id_df[1:]

    return id_df.values.ravel()

def get_clusters_single_shank(session_path: str, shankn: int, fs: float = 20000.):
    """Read the spike time data for a from the .res and .clu files for a single shank.

    Automatically removes noise and multi-unit.

    Parameters
    ----------
    session_path: str | path
        session path
    shankn: int
        shank number (1-indexed)
    fs: float

    Returns
    -------
    df: pd.DataFrame
        has column named 'id' which indicates cluster id and 'time' which
        indicates spike time.

    """
    spike_times = read_spike_times(session_path, shankn, fs=fs)
    spike_ids = read_spike_clustering(session_path, shankn)
    df = pd.DataFrame({'id': spike_ids, 'time': spike_times})
    # id 0 is unsorted noise and 1 as mult-unit activity
    noise_inds = ((df.iloc[:, 0] == 0) | (df.iloc[:, 0] == 1)).values.ravel()
    df = df.loc[np.logical_not(noise_inds)].reset_index(drop=True)
    df['id'] -= 2

    return df

# TODO: pending nwb changes to waveforms
def write_unit_series(nwbfile, session_path, shankn, fs=20000.):
    """Not yet implemented.

    Parameters
    ----------
    nwbfile: pynwb.NWBFile
    session_path: str | path
    shankn: int
        shank number (1-indexed)
    fs: float

    """
    """
    # find first row where units are from this shank
    start = np.argmax(nwbfile.units.electrode_group == nwbfile.electrode_groups['shank' + str(shankn)])

    df = get_clusters_single_shank(session_path, shankn, fs=fs)

    # TO DO: link timestamps to SpikeEventSeries

    unit_series = UnitSeries(
        name='UnitSeries' + str(shankn),
        description='shank' + str(shankn),
        num=df['id'].values + start,
        timestamps=df['time'].values)

    ecephys_module = check_module(nwbfile, 'ecephys')
    if 'SpikeEventSeries' + str(shankn) in ecephys_module:
        ecephys_module['SpikeEventSeries' + str(shankn)].unit_series = unit_series
    else:
        print('UnitSeries' + str(shankn) + ' not linked with a SpikeEventSeries object')

    ecephys_module.add_data_interface(unit_series)
    """
    raise NotImplementedError

def write_electrode_table(nwbfile: NWBFile, session_path: str,
                          electrode_positions: Optional[ArrayLike] = None,
                          impedances: Optional[ArrayLike] = None,
                          locations: Optional[ArrayLike] = None,
                          filterings: Optional[ArrayLike] = None,
                          custom_columns: Optional[List[dict]] = None,
                          max_shanks: Optional[int] = 8):
    """Write the electrode table to the NWBFile object.

    Parameters
    ----------
    nwbfile: pynwb.NWBFile
    session_path: str
    electrode_positions: Iterable(Iterable(float))
    impedances: array-like(dtype=float) (optional)
    locations: array-like(dtype=str) (optional)
    filterings: array-like(dtype=str) (optional)
    custom_columns: list(dict) (optional)
        {name, description, data} for any custom columns
    max_shanks: int | None

    """
    fpath_base, fname = os.path.split(session_path)

    shank_channels = get_shank_channels(session_path)
    if max_shanks:
        shank_channels = shank_channels[:max_shanks]
    nwbfile.add_electrode_column('shank_electrode_number', '1-indexed channel within a shank')
    nwbfile.add_electrode_column('amp_channel', 'order in which the channels were plugged into amp')
    for custom_column in custom_columns:
        nwbfile.add_electrode_column(custom_column['name'],
                                     custom_column['description'])

    device = nwbfile.create_device('implant', fname + '.xml')
    for shankn, channels in enumerate(shank_channels):
        shankn += 1
        electrode_group = nwbfile.create_electrode_group(
            name='shank{}'.format(shankn),
            description='shank{} electrodes'.format(shankn),
            device=device, location='unknown')
        for shank_electrode_number, amp_channel in enumerate(channels):
            if electrode_positions is not None:
                pos = electrode_positions[amp_channel]
            else:
                pos = (np.nan, np.nan, np.nan)

            if impedances is None:
                imp = np.nan
            else:
                imp = impedances[amp_channel]

            if locations is None:
                location = 'unknown'
            else:
                location = locations[amp_channel]

            if filterings is None:
                filtering = 'unknown'
            else:
                filtering = filterings[amp_channel]

            custom_data = {custom_col['name']: custom_col['data'][amp_channel]
                           for custom_col in custom_columns}

            nwbfile.add_electrode(
                float(pos[0]), float(pos[1]), float(pos[2]),
                imp=imp, location=location, filtering=filtering,
                group=electrode_group, amp_channel=amp_channel,
                shank_electrode_number=shank_electrode_number, **custom_data)

def read_lfp(session_path: str, stub: bool = False):
    """Read LFP data from Neuroscope eeg file.

    Parameters
    ----------
    session_path: str
    stub: bool, optional
        Default is False. If True, don't read full LFP, but instead a
        truncated version of at most size (50, n_channels)

    Returns
    -------
    lfp_fs, all_channels_data
    """
    fpath_base, fname = os.path.split(session_path)
    lfp_filepath = os.path.join(session_path, fname + '.eeg')
    lfp_fs = get_lfp_sampling_rate(session_path)
    n_channels = sum(len(x) for x in get_channel_groups(session_path))

    assert os.path.isfile(lfp_filepath), "No .eeg file found at the path location!" \
                                         "Unable to retrieve all_channels_data."

    if stub:
        max_size = 50
        all_channels_data = np.fromfile(lfp_filepath,
                                        dtype=np.int16,
                                        count=max_size*n_channels).reshape(-1, n_channels)
    else:
        all_channels_data = np.fromfile(lfp_filepath,
                                        dtype=np.int16).reshape(-1, n_channels)

    return lfp_fs, all_channels_data

def write_lfp(nwbfile: NWBFile, data: ArrayLike, fs: float,
              electrode_inds: Optional[List[int]] = None,
              name: Optional[str] = 'LFP',
              description: Optional[str] = 'local field potential signal'):
    """
    Add LFP from neuroscope to a "ecephys" processing module of an NWBFile.

    Parameters
    ----------
    nwbfile: pynwb.NWBFile
    data: array-like
    fs: float
    electrode_inds: list(int), optional
    name: str, optional
    description: str, optional

    Returns
    -------
    LFP pynwb.ecephys.ElectricalSeries

    """
    if electrode_inds is None:
        if nwbfile.electrodes is not None and data.shape[1] <= len(nwbfile.electrodes.id.data[:]):
            electrode_inds = list(range(data.shape[1]))
        else:
            electrode_inds = list(range(len(nwbfile.electrodes.id.data[:])))

    table_region = nwbfile.create_electrode_table_region(
        electrode_inds, 'electrode table reference')

    data = H5DataIO(
        DataChunkIterator(
            tqdm(data, desc='writing lfp data'),
            buffer_size=int(fs * 3600)), compression='gzip')

    lfp_electrical_series = ElectricalSeries(
        name=name, description=description,
        data=data, electrodes=table_region, conversion=np.nan,
        rate=fs, resolution=np.nan)

    ecephys_mod = check_module(
        nwbfile, 'ecephys', 'intermediate data from extracellular electrophysiology recordings, e.g., LFP')

    if 'LFP' not in ecephys_mod.data_interfaces:
        ecephys_mod.add_data_interface(LFP(name='LFP'))

    ecephys_mod.data_interfaces['LFP'].add_electrical_series(lfp_electrical_series)

    return lfp_electrical_series

def add_lfp(nwbfile: NWBFile, session_path: str,
            name: Optional[str] = 'LFP',
            description: Optional[str] = 'local field potential signal',
            stub: bool = False):
    """Read and write LFP data to the NWBFile.

    Parameters
    ----------
    nwbfile: pynwb.NWBFile
    session_path: str
    name: str, optional
    description: str, optional
    stub: bool, optional
        Default is False. If True, don't read LFP, but instead add a small
        amount of placeholder data. This is useful for rapidly checking new
        features without the time-intensive data read step.

    """
    fs, data = read_lfp(session_path, stub=stub)
    shank_channels = get_shank_channels(session_path)
    all_shank_channels = np.concatenate(shank_channels)
    write_lfp(nwbfile, data[:, all_shank_channels], fs, name, description)

def get_events(session_path: str, suffixes: Iterable[int] = None):
    """Retrieve event information from Neuroscope evt files.

    Parameters
    ----------
    session_path: str
    suffixes: Iterable(str), optional
        The 3-letter names for the events to write. If None, detect all in session_path

    """
    session_name = os.path.split(session_path)[1]

    if suffixes is None:
        evt_files = glob(os.path.join(session_path, session_name) + '.evt.*') + \
                    glob(os.path.join(session_path, session_name) + '.*.evt')
    else:
        evt_files = [os.path.join(session_path, session_name + s)
                     for s in suffixes]

    out = []
    for evt_file in evt_files:
        parts = os.path.split(evt_file)[1].split('.')
        if parts[-1] == 'evt':
            name = '.'.join(parts[1:-1])
        else:
            name = parts[-1]
        if os.path.isfile(evt_file):
            df = pd.read_csv(evt_file, sep='\t', names=('time', 'desc'))
            if len(df):
                timestamps = df.values[:, 0].astype(float) / 1000
                data = df['desc'].values
                annotation_series = AnnotationSeries(name=name, data=data, timestamps=timestamps)
                out.append(annotation_series)
        else:
            print("Warning: No .evt file found at the path location!"
                  "Unable to retrieve annotation_series.")
            out = None

    return out

def write_events(nwbfile: NWBFile, session_path: str, suffixes: Iterable[str], module=None):
    """Write the event information from Neurscope into the NWBFile.

    Parameters
    ----------
    nwbfile: pynwb.NWBFile
    session_path: str
    suffixes: Iterable(str), optional
        The 3-letter names for the events to write. If None, detect all in session_path
    module: pynwb.processing_module

    """
    session_name = os.path.split(session_path)[1]

    if suffixes is None:
        evt_files = glob(os.path.join(session_path, session_name) + '.evt.*') + \
                    glob(os.path.join(session_path, session_name) + '.*.evt')
    else:
        evt_files = [os.path.join(session_path, session_name + s)
                     for s in suffixes]
    if module is None:
        module = check_module(nwbfile, 'events')
    for evt_file in evt_files:
        parts = os.path.split(evt_file)[1].split('.')
        if parts[-1] == 'evt':
            name = '.'.join(parts[1:-1])
        else:
            name = parts[-1]
        if os.path.isfile(evt_file):
            df = pd.read_csv(evt_file, sep='\t', names=('time', 'desc'))
            if len(df):
                timestamps = df.values[:, 0].astype(float) / 1000
                data = df['desc'].values
                annotation_series = AnnotationSeries(
                    name=name, data=data, timestamps=timestamps)
                module.add_data_interface(annotation_series)
        else:
            print("Warning: No .evt file found at the path location!"
                  "Unable to write annotation_series.")

def write_spike_waveforms(nwbfile: NWBFile, session_path: str, shankn: int,
                          stub: bool = False,
                          compression: Optional[str] = 'gzip'):
    """Write spike waveforms to NWBFile.

    Parameters
    ----------
    nwbfile: pynwb.NWBFiles
    session_path: str
    shankn: int
    stub: bool, optional
        default: False
    compression: str (optional)
    """
    session_name = os.path.split(session_path)[1]
    xml_filepath = os.path.join(session_path, session_name + '.xml')
    group = nwbfile.electrode_groups['shank' + str(shankn)]
    elec_idx = list(np.where(np.array(nwbfile.ec_electrodes['group']) == group)[0])
    table_region = nwbfile.create_electrode_table_region(elec_idx, group.name + ' region')
    nchan = len(elec_idx)
    root = load_xml(xml_filepath)
    nsamps = int(root.find('neuroscope').find('spikes').find('nSamples').text)

    if stub:
        spks = np.random.randn(10, nsamps, nchan)
        spike_times = np.arange(10)
    else:
        spk_file = os.path.join(session_path, session_name + '.spk.' + str(shankn))
        if not os.path.isfile(spk_file):
            print('spike waveforms for shank{} not found'.format(shankn))
            return
        spks = np.fromfile(spk_file, dtype=np.int16).reshape(-1, nsamps, nchan)
        spike_times = read_spike_times(session_path, shankn)
    if compression:
        data = H5DataIO(spks, compression=compression)
    else:
        data = spks

    spike_event_series = SpikeEventSeries(name='SpikeEventSeries' + str(shankn),
                                          data=data,
                                          timestamps=spike_times,
                                          electrodes=table_region)

    # if 'shank' + str(shankn) in nwbfile.electrode_groups:
    #     nwbfile.electrode_groups['shank' + str(shankn)].event_waveform = EventWaveform(
    #         spike_event_series=spike_event_series)

    check_module(nwbfile, 'ecephys').add_data_interface(spike_event_series)

def add_units(nwbfile: NWBFile, session_path: str,
              custom_cols: Optional[List[dict]] = None,
              max_shanks: Optional[int] = 8):
    """Add the spiking unit information to the NWBFile.

    Parameters
    ----------
    nwbfile: pynwb.NWBFile
    session_path: str
    custom_cols: list(dict), optional
        [{name, description, data, kwargs}]
    max_shanks: int, optional
        only take the first <max_shanks> channel groups

    Returns
    -------
    nwbfile
    """
    nwbfile.add_unit_column('shank_id', '0-indexed id of cluster of shank')
    nshanks = len(get_shank_channels(session_path))
    nshanks = min((max_shanks, nshanks))

    for shankn in range(1, nshanks + 1):
        df = get_clusters_single_shank(session_path, shankn)
        electrode_group = nwbfile.electrode_groups['shank' + str(shankn)]
        for shank_id, idf in df.groupby('id'):
            nwbfile.add_unit(spike_times=idf['time'].values, shank_id=shank_id, electrode_group=electrode_group)

    if custom_cols:
        [nwbfile.add_unit_column(**x) for x in custom_cols]

    return nwbfile


# #!/usr/bin/env.python
# # -*- coding:utf-8 -*-
import requests, time
import pandas as pd

headers = {
    'authority': 'www.zhihu.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    'x-udid': 'AACoDn_Wcw6PTtckbdUX8X7JAG5F1AWh0fE='
}

def get_followees(num: int):
    followees = []
    for i in range(num):
        url = r'https://www.zhihu.com/api/v4/members/jiang-zhan-95-72/followees?include=data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics&offset={}&limit=20'.format(i * 20)
        try:
            response = requests.get(url, headers=headers)
            r = response.json()['data']
            print('爬取史gay关注的人第{}页，状态码{}'.format(i + 1, response.status_code))
            followees.extend(r)
            time.sleep(1)
        except:
            break
    return followees

def get_follow_questions(num: int):
    follow_questions = []
    for i in range(num):
        url = r'https://www.zhihu.com/api/v4/members/jiang-zhan-95-72/following-questions?include=data%5B*%5D.created%2Canswer_count%2Cfollower_count%2Cauthor&offset={}&limit=20'.format(i * 20)
        try:
            response = requests.get(url, headers=headers)
            r = response.json()['data']
            print('爬取史gay关注的问题第{}页，状态码{}'.format(i + 1, response.status_code))
            follow_questions.extend(r)
            time.sleep(1)
        except:
            break
    return follow_questions

if __name__ == '__main__':
    num = 20
    followees = get_followees(num)
    follow_questions = get_follow_questions(num)
    df1 = pd.DataFrame.from_dict(followees)
    df2 = pd.DataFrame.from_dict(follow_questions)
    df1.to_csv('C:\\Users\\jjff1\\Desktop\\followees.csv', encoding='utf-8-sig')
    df2.to_csv('C:\\Users\\jjff1\\Desktop\\follow_questions.csv', encoding='utf-8-sig')

基础爬虫/知乎数据爬取.py


########   SENTIMENT ANALYSIS   #############

#libraries to create and work on data frames
import pandas as pd
import numpy as np
import re
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
#%matplotlib inline
#libraries to scrape data from twitter
import tweepy
import webbrowser
#language processing libraries
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from spellchecker import SpellChecker
from textblob import TextBlob

consumer_key = '--' #insert the key
consumer_secret = '--' #insert the key
callback_uri = 'oob'

#to authorize as a valid user
auth = tweepy.OAuthHandler(consumer_key, consumer_secret, callback_uri)
redirect_url = auth.get_authorization_url()
webbrowser.open(redirect_url)
user_pin = input("Pin: ")
auth.get_access_token(user_pin)
api = tweepy.API(auth)

#loading the timelines
import datetime as dt
start = dt.datetime(2020,10,10)
end = dt.datetime(2020,10,11)
t1 = api.user_timeline(screen_name = 'ndtv', tweet_mode = "extended", count= 500, lang = "en")
t2 = api.user_timeline(screen_name = 'timesofindia', tweet_mode = "extended", count= 500, lang = "en")
t3 = api.user_timeline(screen_name = 'IndiaToday', tweet_mode = "extended", count= 500, lang = "en")

#retrieving the tweets from respective timelines
texts1 = [status1.full_text for status1 in t1]
texts2 = [status2.full_text for status2 in t2]
texts3 = [status3.full_text for status3 in t3]

#creating dataframe
df1 = pd.DataFrame(texts1, columns = ['text'])
df2 = pd.DataFrame(texts2, columns = ['text'])
df3 = pd.DataFrame(texts3, columns = ['text'])

clean_text=[]
for i in df1['text']:
    message = re.sub(r'''(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))''', " ", i)
    message = re.sub(r"[,@\'?\:.$%_]", "",message)
    message = re.sub('[^a-zA-Z/:.\d]',' ',message)
    message = re.sub(r"\s+"," ", message)
    message = message.lower()
    message = message.split()
    ps = PorterStemmer()
    all_words = stopwords.words('english')
    message = [ps.stem(word) for word in message if not word in set(all_words)]
    message = ' '.join(message)
    clean_text.append(message)
df1['text']= clean_text

clean_text = []
for i in df2['text']:
    message = re.sub(r'''(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))''', " ", i)
    message = re.sub(r"[,@\'?\:.$%_]", "",message)
    message = re.sub('[^a-zA-Z/:.\d]',' ',message)
    message = re.sub(r"\s+"," ", message)
    message = message.lower()
    message = message.split()
    ps = PorterStemmer()
    all_words = stopwords.words('english')
    message = [ps.stem(word) for word in message if not word in set(all_words)]
    message = ' '.join(message)
    clean_text.append(message)
df2['text']= clean_text

clean_text = []
for i in df3['text']:
    message = re.sub(r'''(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))''', " ", i)
    message = re.sub(r"[,@\'?\:.$%_]", "",message)
    message = re.sub('[^a-zA-Z/:.\d]',' ',message)
    message = re.sub(r"\s+"," ", message)
    message = message.lower()
    message = message.split()
    ps = PorterStemmer()
    all_words = stopwords.words('english')
    message = [ps.stem(word) for word in message if not word in set(all_words)]
    message = ' '.join(message)
    clean_text.append(message)
df3['text']= clean_text

pol1, pol2, pol3 = [], [], []
for i in range(len(df1)):
    pol1.append(TextBlob(df1['text'][i]).sentiment.polarity)
    pol2.append(TextBlob(df2['text'][i]).sentiment.polarity)
    pol3.append(TextBlob(df3['text'][i]).sentiment.polarity)

df1["Polarity"] = pol1
df2["Polarity"] = pol2
df3["Polarity"] = pol3

senti1, senti2, senti3 = [], [], []

for i in range(len(df1['Polarity'])):
    if df1['Polarity'][i]>0: senti1.append('Positive')
    elif df1['Polarity'][i]<0: senti1.append('Negative')
    else: senti1.append('Neutral')

for i in range(len(df1['Polarity'])):
    if df2['Polarity'][i]>0: senti2.append('Positive')
    elif df2['Polarity'][i]<0: senti2.append('Negative')
    else: senti2.append('Neutral')

for i in range(len(df1['Polarity'])):
    if df3['Polarity'][i]>0: senti3.append('Positive')
    elif df3['Polarity'][i]<0: senti3.append('Negative')
    else: senti3.append('Neutral')

df1["Sentiment"] = senti1
df2["Sentiment"] = senti2
df3["Sentiment"] = senti3

#getting the number of positve and negative tweets
ptext1 = sum([1 for i in range(len(df1)) if df1['Sentiment'][i]=='Positive'])
ptext2 = sum([1 for i in range(len(df2)) if df2['Sentiment'][i]=='Positive'])
ptext3 = sum([1 for i in range(len(df3)) if df3['Sentiment'][i]=='Positive'])

ntext1 = sum([1 for i in range(len(df1)) if df1['Sentiment'][i]=='Negative'])
ntext2 = sum([1 for i in range(len(df2)) if df2['Sentiment'][i]=='Negative'])
ntext3 = sum([1 for i in range(len(df3)) if df3['Sentiment'][i]=='Negative'])

#getting the positive and negative tweets percentage
panalysis1 = round(ptext1 / len(df1) *100)
panalysis2 = round(ptext2 / len(df2) *100)
panalysis3 = round(ptext3 / len(df3) *100)

nanalysis1 = round(ntext1 / len(df1) *100)
nanalysis2 = round(ntext2 / len(df2) *100)
nanalysis3 = round(ntext3 / len(df3) *100)

#plotting graphs for tweets percentage
y = [panalysis1, panalysis2, panalysis3]
x = ["NDTV", "AAJTAK", "INDIATODAY"]
plt.bar(x,y)

y = [nanalysis1, nanalysis2, nanalysis3]
x = ["NDTV", "AAJTAK", "INDIATODAY"]
plt.bar(x,y)

#plotting graphs for negative tweets percentage
y = [nanalysis1, nanalysis2, nanalysis3]
x = ["NDTV", "AAJTAK", "INDIATODAY"]
plt.bar(x,y)


import scrapy
import json
import re
import pandas as pd

class CharacterSpider(scrapy.Spider):
    name = 'char'

    start_urls = ["https://order-nn.ru/kmo/catalog/"]
    headers = {'User-Agent': 'Mozilla/5.0'}
    desired_categories = ["Краски и материалы специального назначения", "Краски для наружных работ", "Лаки"]

    custom_settings = {
        'ROBOTSTXT_OBEY': False,
        'DOWNLOAD_DELAY': 2,
        'CONCURRENT_REQUESTS': 2,
    }

    def remove_special_characters(self, text):
        return re.sub(r'[^\w\sа-яА-Я]+', '', text)

    def parse(self, response):
        find_categories_links = response.xpath("//div[@class='sections-block-level-2']")
        category_dict = {}

        for category in self.desired_categories:
            elements = response.xpath(f'//a[contains(text(), "{category}")]')
            link = elements.xpath('@href').extract_first()

            if category not in category_dict:
                category_dict[category] = []

            if link and link not in category_dict[category]:
                self.log(f"Найдена ссылка на категорию '{category}': {link}")
                category_dict[category].append(link)
            else:
                self.log(f"Ссылка на категорию '{category}' уже существует: {link}")

        for category, links in category_dict.items():
            for link in links:
                yield response.follow(link, self.parse_item_links, meta={'category': category})

    def parse_item_links(self, response):
        item_links = response.xpath('//div/div[@class="horizontal-product-item-block_3_2"]/a/@href').extract()
        for link in item_links:
            yield response.follow(link, self.parse_item, meta={'category': response.meta['category']})

        # Extract and follow the pagination link from the current page
        next_page = response.xpath('//ul[@class="ul-pagination"]/li/a[@rel="canonical"]/@href').extract()[-1]
        if next_page:
            yield response.follow(next_page, self.parse_item_links, meta={'category': response.meta['category']})

    def parse_item(self, response):
        item = {}
        item['Категория'] = response.meta['category']
        item["наименование"] = response.xpath('//div[@class="block-1"]/div/h1/text()').get()
        item["цена"] = response.xpath('//div[@class="block-2-3"]/div/div/span/text()').get()
        description = response.xpath('//div[@id="block-description"]//text()').extract()

        item["описание"] = self.remove_special_characters(''.join(description)).strip()

        ajax_url = f"https://order-nn.ru/local/ajax/kmo/getCharacterItems.php?type=character&style=element&items={response.url.split('/')[-1]}"
        yield scrapy.Request(ajax_url, callback=self.parse_ajax_data, meta={'item': item})

    def parse_ajax_data(self, response):
        item = response.meta['item']
        item["характеристики"] = {}
        data = response.xpath('//table[@class="table table-striped table-character"]/tr')

        for row in data:
            text = row.xpath('td[@class="table-character-text"]/text()').get()
            value = row.xpath('td[@class="table-character-value"]/text()').get()

            if text and value:
                item["характеристики"][text] = value
        self.log(f'Количество спарсенных ячеек: {len(item)}')
        yield item

    def __init__(self, *args, **kwargs):
        super(CharacterSpider, self).__init__(*args, **kwargs)
        self.items = []

    def closed(self, reason):
        df = pd.DataFrame(self.items)

        # Save the DataFrame to a CSV file
        df.to_csv("scraped_data.csv", index=False)


# -*- coding: utf-8 -*-
"""
Created on Sun Mar 12 15:59:21 2017

@author: Thautwarm
"""

from sklearn.cross_validation import KFold
from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestClassifier as rf
from sklearn.naive_bayes import GaussianNB as nb
from sklearn.ensemble import GradientBoostingClassifier as gbdt
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier as rfc
from sklearn.neighbors import KNeighborsClassifier as knn
from sklearn.lda import LDA
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import VotingClassifier as vot
import warnings
warnings.filterwarnings("ignore")
#from keras.models import Sequential
#from keras.layers import Dense,Dropout
#from keras.utils.np_utils import to_categorical
from sklearn.grid_search import GridSearchCV
import numpy as np
from logicMain import X_test,X_train,y_train,TestDatas
import pandas as pd
from functools import reduce
def stats(y_true,y_pred):
    TP=(y_true==1)&(y_pred==1)
    TN=(y_true==0)&(y_pred==0)
    FP=(y_true==0)&(y_pred==1)
    FN=(y_true==1)&(y_pred==0)
    tp=sum(TP)
    fp=sum(FP)
    fn=sum(FN)
    acc=(sum(TP)+sum(TN))/len(y_true)
    pre=0 if fp+tp==0 else tp/(tp+fp)
    rec=0 if fn+tp==0 else tp/(tp+fn)
    f1= 0 if pre==0 or  rec==0 else 2/(1/pre +1/rec)
    select={'TP':TP,'TN':TN,'FP':FP,'FN':FN,'acc':acc}
    return  pre,rec,f1,select
#net=Sequential()
#net.add(Dense(40,input_dim=X_train.shape[1],init='uniform',activation='linear'))
#net.add(Dense(40,input_dim=40,activation='relu'))
#net.add(Dense(40,input_dim=40,activation='relu'))
#net.add(Dropout(0.3))
#net.add(Dense(30,input_dim=40,activation='tanh'))
#net.add(Dense(1,input_dim=30,activation='relu'))
#net.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
#net = Sequential()
#net.add(Dense(64, input_dim=X.shape[1], init='uniform', activation='relu'))
#net.add(Dropout(0.5))
#net.add(Dense(64, activation='relu'))
#net.add(Dropout(0.5))
#net.add(Dense(1, activation='sigmoid'))
#net.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])

Predictors=[]
N=len(X_train)
while len(Predictors)<=100:
    index=np.random.permutation(N)
    X_train=X_train[index]
    y_train=y_train[index]
    kf=KFold(len(X_train),n_folds=6)
    for train_index,test_index in kf:
        x1,x2=X_train[train_index],X_train[test_index]
        y1,y2=y_train[train_index],y_train[test_index]
        clf=vot(estimators=[
                    ('rfc',rfc(n_estimators=500)),
                    ('gbdt',gbdt(n_estimators=500,learning_rate=0.02))
                    ] ,weights=[5,5])
        clf.fit(x1,y1)
        y_pred=clf.predict(x2)
        pre,rec,f1,select=stats(y2,y_pred)
        print()
        print(" precision :%.4f recall : %.4f f1-score :%.4f "%(pre,rec,f1))
        if pre>0.83 and rec>0.72:
            Predictors.append(clf)
y=(np.mean([clf_i.predict(X_test) for clf_i in Predictors],0)>0.5)
y=pd.Series(y,name='Survived')
df=pd.DataFrame([TestDatas.PassengerId,y]).T
df.to_csv('res.csv',index=False)
    #{'gamma': 0.26722222222222219, 'C': 0.55555555555555558}


import sys
from flask import Flask, make_response, request
app = Flask(__name__)

@app.route('/')
def index():
    links = ["ours","prototype", "custom"]

    html = ""
    for link in links:
        html += "
"+link+"
"
    return html

@app.route('/ours')
def new():

    # url = "http://localhost:8007/?REQUEST=GetMap&source_url=http://localhost:8001/ocean_latest.nc&layers=SSTA&time_index=0&colorscalerange=-10,10&width=800&height=600&bbox=0,-90,360,90&styles=contour&time=Default&palette=jet&color_range=-10,10&n_color=10&format=png"
    url = "http://localhost:8007/?"

    params = {
        "request":"GetMap",
        "bbox":"0,-90,360,90",
        "width": "300",
        "height": "400",
        # "layers": "hr24_prcp",
        "layers" : "SSTA",
        "styles" : "contour",
        "crs" : "EPSG:4283",
        "format" : "image/png" ,
        "time" : "Default" ,
        "time_index": "Default" ,
        # "source_url": "http://localhost:8001/atmos_latest.nc",
        "source_url" : "http://localhost:8001/ocean_latest.nc",
        "color_range" : "-10,10" ,
        "n_color" : "10" ,
        "palette" : "jet"
    }

    url += "&".join("=".join(i) for i in params.items())
    return ""+url+""

@app.route('/custom')
def custom():

    url = "http://localhost:8007/?"

    params = {
        "request":"GetMap",
        "bbox":"-180,-90,1800,90",
        "width": "694",
        "height": "573",
        "layers": "hr24_prcp",
        #"layers" : "SSTA",
        "styles" : "contour",
        "crs" : "EPSG:4283",
        "format" : "png" ,
        "time" : "Default" ,
        "time_index": "Default" ,
        "source_url": "http://localhost:8001/atmos_latest.nc",
        #"source_url" : "http://localhost:8001/ocean_latest.nc",
        "palette" : "jet",
        # Cust colors here
        "custom_colors" : "%23aabbcc,%23abc123,b,%23123123,m",
        "custom_levels" : "0,2.5,3.0,5,10,15",
        "custom_min" : "cyan",
        "custom_max" : "yellow"
    }

    url += "&".join("=".join(i) for i in params.items())
    return ""+url+""

@app.route('/prototype')
def prototype():
    url = "http://localhost:8006/?REQUEST=GetFullFigure&DAP_URL=http://localhost:8001/ocean_latest.nc&LAYERS=SSTA&TIMEINDEX=0&COLORSCALERANGE=-10,10&WIDTH=800&HEIGHT=600&BBOX=0,-90,360,90&STYLE=contour"
    return ""+url+""

# can pass the port number as argument
if __name__ == '__main__':
    port = 8008
    if len(sys.argv) > 1:
        port = int(sys.argv[1])
    app.debug = True
    app.run(host='127.0.0.1', port=port)


import logging
import os
import sys
import textwrap
import traceback

import click
import rich

LOGGER = logging.getLogger(__name__)

def entry_points(name: str):
    import importlib.metadata

    try:
        x = importlib.metadata.entry_points()[name]
        return {ep for ep in x}  # use of set here to remove duplicates
    except KeyError:
        return []

def load_plugins(entry_point: str) -> dict:

    eps = {}
    for ep in entry_points(entry_point):
        try:
            eps[ep.name] = ep.load()
        except Exception as exc:
            eps[ep.name] = None
            LOGGER.error(f"Couldn't load entry point: {exc}")

    return eps

# The following code was adapted from the inspiring package click-plugins
# at https://github.com/click-contrib/click-plugins/

def handle_click_plugins(plugins):
    def decorator(group):
        if not isinstance(group, click.Group):
            raise TypeError("Plugins can only be attached to an instance of click.Group()")

        for entry_point in plugins or ():
            try:
                group.add_command(entry_point.load())
            except Exception:
                # Catch this so a busted plugin doesn't take down the CLI.
                # Handled by registering a dummy command that does nothing
                # other than explain the error.
                group.add_command(BrokenCommand(entry_point.name))

        return group

    return decorator

class BrokenCommand(click.Command):
    """
    Rather than completely crash the CLI when a broken plugin is loaded, this
    class provides a modified help message informing the user that the plugin is
    broken, and they should contact the owner.  If the user executes the plugin
    or specifies `--help` a traceback is reported showing the exception the
    plugin loader encountered.
    """

    def __init__(self, name):
        """
        Define the special help messages after instantiating a `click.Command()`.
        """

        click.Command.__init__(self, name)

        util_name = os.path.basename(sys.argv and sys.argv[0] or __file__)
        icon = u'\u2020'

        self.help = textwrap.dedent(
            f"""\
            Warning: entry point could not be loaded. Contact its author for help.

            {traceback.format_exc()}
            """
        )

        self.short_help = f"{icon} Warning: could not load plugin. See `{util_name} {self.name} --help`."

    def invoke(self, ctx):
        """
        Print the traceback instead of doing nothing.
        """

        rich.print()
        rich.print(self.help)
        ctx.exit(1)

    def parse_args(self, ctx, args):
        return args


import requests
import threading
import time
from frostcop.utils import discord_utils
from bs4 import BeautifulSoup as bsoup
from requests_futures.sessions import FuturesSession
class monitor:
	needs_soup = False
	ctime = 0
	def __init__(self):
		pass

	def refresh(self):
		"""
		If a monitor has a list of links it must check,
		it overrides the refresh function. This is not
		required, so it is implemented here as an empty
		method to prevent errors.
		"""
		pass

	def check(self):
		"""
		All monitors must have a method to check the
		stock of their items. If a monitor class does
		not override this method, an error is thrown.
		"""
		raise NotImplementedError("check must be defined in child monitor class.")

	def scrape(self):
		n = len(self.links)
		if n < 0:
			n = 1
		session = FuturesSession(max_workers=n)
		reqs = ()
		for link in self.links:
			reqs += (session.get(link),)
		self.responses = [r.result() for r in reqs]
		print(self.responses)
		self.soups = []
		if self.needs_soup:
			for resp in self.responses:
				self.soups.append(self.soupify(resp))

	def update(self, product):
		if not hasattr(self, 'stock'):
			self.stock = {}
			self.initialized = False
		if self.initialized:
			if product.uid not in self.stock:
				self.notify(product, 'new')
			elif product.stock != self.stock[product.uid].stock:
				self.notify(product, 'restock')
		self.stock[product.uid] = product

	def notify(self, product, notification_type):
		print(self.webhook, repr(product), notification_type)

	def request(self, link):
		"""
		Returns an HTTP GET request for the specified
		web address, and throws an error after a
		timeout of 5 seconds.
		"""
		return requests.get(link, timeout=5)

	def soupify(self, response):
		"""
		Uses BeautifulSoup4 to return the HTML content
		of the specified HTTP response, as obtained
		using the requests module. "response" is not
		a web address, but an already pinged response
		object generated from an address.
		"""
		print('souping')
		return bsoup(response.content, 'html.parser')

	def run(self):
		self.ctime = time.time()
		self.refresh()
		quit()
		self.scrape()
		threads = []
		for i in range(len(self.links)):
			t = threading.Thread(target=self.check, args=(i,))
			t.daemon = True
			t.start()
			threads.append(t)
		for t in threads:
			t.join()
		if not self.initialized:
			self.initialized = True
		print(self.__class__.__name__, len(threads), round(time.time()-self.ctime, 7))

class product:
	def __init__(self, uid, name, price, stock, **kwargs):
		self.uid = uid
		self.name = name
		self.stock = stock
		kw_names = ['variant', 'sizes']
		for name in kw_names:
			if name in kwargs:
				setattr(self, name, kwargs.get(name))
	def __str__(self):
		return str(self.__dict__)


import pyblish.api
from openpype.pipeline import PublishValidationError
from pymxs import runtime as rt

class ValidateTyFlowData(pyblish.api.InstancePlugin):
    """Validate TyFlow plugins or relevant operators are set correctly."""

    order = pyblish.api.ValidatorOrder
    families = ["pointcloud", "tycache"]
    hosts = ["max"]
    label = "TyFlow Data"

    def process(self, instance):
        """
        Notes:
            1. Validate the container only include tyFlow objects
            2. Validate if tyFlow operator Export Particle exists

        """

        invalid_object = self.get_tyflow_object(instance)
        if invalid_object:
            self.log.error(f"Non tyFlow object found: {invalid_object}")

        invalid_operator = self.get_tyflow_operator(instance)
        if invalid_operator:
            self.log.error(
                "Operator 'Export Particles' not found in tyFlow editor.")
        if invalid_object or invalid_operator:
            raise PublishValidationError(
                "issues occurred",
                description="Container should only include tyFlow object "
                "and tyflow operator 'Export Particle' should be in "
                "the tyFlow editor.")

    def get_tyflow_object(self, instance):
        """Get the nodes which are not tyFlow object(s)
        and editable mesh(es)

        Args:
            instance (pyblish.api.Instance): instance

        Returns:
            list: invalid nodes which are not tyFlow
                object(s) and editable mesh(es).
        """
        container = instance.data["instance_node"]
        self.log.debug(f"Validating tyFlow container for {container}")

        allowed_classes = [rt.tyFlow, rt.Editable_Mesh]
        return [
            member for member in instance.data["members"]
            if rt.ClassOf(member) not in allowed_classes
        ]

    def get_tyflow_operator(self, instance):
        """Check if the Export Particle Operators in the node
        connections.

        Args:
            instance (str): instance node

        Returns:
            invalid(list): list of invalid nodes which do
            not consist of Export Particle Operators as parts
            of the node connections
        """
        invalid = []
        members = instance.data["members"]
        for member in members:
            obj = member.baseobject

            # There must be at least one animation with export
            # particles enabled
            has_export_particles = False
            anim_names = rt.GetSubAnimNames(obj)
            for anim_name in anim_names:
                # get name of the related tyFlow node
                sub_anim = rt.GetSubAnim(obj, anim_name)
                # check if there is export particle operator
                if rt.IsProperty(sub_anim, "Export_Particles"):
                    has_export_particles = True
                    break

            if not has_export_particles:
                invalid.append(member)
        return invalid


from tkinter import *

class Bill_App:
    def __init__(self,root):
        self.root = root
        self.root.title("Billing Software")
        self.root.iconbitmap("Lgp85-Magic-Christmas-My-Documents.ico")
        self.root.geometry("1920x1080+0+0")
        bg_color = "#FB3597"
        font_color = "#FBFBFB"
        font_style = "Times New Roman"
        label1 = Label(self.root, text="White House Drycleaners", bd=5, relief = GROOVE, bg=bg_color, fg=font_color, font=(font_style,30,"bold"), pady = 2)
        label1.pack(fill=X)

# variables
        self.cname = StringVar()
        self.c_ph_no = StringVar()
        self.c_email = StringVar()
        self.c_bill_no = StringVar()
        self.searchBill = StringVar()

        self.d_blanket = IntVar()
        self.d_saree = IntVar()
        self.d_shirt = IntVar()

        self.s_blanket = IntVar()
        self.s_saree = IntVar()
        self.s_shirt = IntVar()

        self.c_saree = IntVar()

        self.DryClean_price = StringVar()
        self.SteamPress_price = StringVar()
        self.Charak_price = StringVar()

        # costumer details
        f1 = LabelFrame(self.root, text="Costumer Details: ",bd=5, pady=2, font=(font_style, 15, "bold"), fg=font_color, bg=bg_color)
        f1.place(x=0, y=60, relwidth = 1)

        lbl_name = Label(f1, text="Name: ", font=(font_style, 15, "bold"))
        lbl_name.grid(row=0, column=0, padx=10)
        e1 = Entry(f1,width=20,borderwidth=5, textvariable = self.cname)
        e1.grid(row=0,column=1, padx=10)

        lbl_ph_no = Label(f1, text="Phone No: ", font=(font_style, 15, "bold"))
        lbl_ph_no.grid(row=0, column=2, padx=10)
        e2 = Entry(f1, width=20, borderwidth=5, textvariable = self.c_ph_no)
        e2.grid(row=0, column=3, padx=10)

        lbl_email = Label(f1, text="E-mail", font=(font_style,15,"bold"))
        lbl_email.grid(row=0, column=4, padx=10)
        e4 = Entry(f1, width=40, borderwidth=5, textvariable = self.c_email)
        e4.grid(row=0,column=5, padx=10)

        lbl_bill_no = Label(f1, text="Bill No: ", font=(font_style, 15, "bold"))
        lbl_bill_no.grid(row=0, column=6, padx=10)
        e3 = Entry(f1, width=20, borderwidth=5, textvariable = self.searchBill)
        e3.grid(row=0, column=7, padx=10)

        btn1 = Button(f1, text="Search",bd=5, bg="White", fg="Black", relief="raised", font=(font_style,15,"bold"))
        btn1.grid(row=0, column=8,padx=20,pady=10)

# items : drycleaning
        f2=LabelFrame(self.root,text="DryCleaning", bd=5, font=(font_style,15,"bold"), bg=bg_color, fg=font_color)
        f2.place(x=2,y=160)

        lbl_blanket = Label(f2, text="Blanket", font=(font_style,15,"bold"))
        lbl_blanket.grid(row=0, column=0,padx=5, pady=4, sticky="w")
        blanket_e = Entry(f2, width=5, borderwidth = 5, textvariable = self.d_blanket)
        blanket_e.grid(row=0, column=1, padx=10)

        lbl_saree = Label(f2, text="Saree", font=(font_style, 15, "bold"))
        lbl_saree.grid(row=1, column=0, padx=5, pady=4, sticky="w")
        saree_e = Entry(f2, width=5, borderwidth=5, textvariable = self.d_saree)
        saree_e.grid(row=1, column=1, padx=10)

        lbl_shirt = Label(f2, text="Shirt", font=(font_style, 15, "bold"))
        lbl_shirt.grid(row=2, column=0, padx=5, pady=4, sticky="w")
        shirt_e = Entry(f2, width=5, borderwidth=5, textvariable = self.d_shirt)
        shirt_e.grid(row=2, column=1, padx=10)

#items : steampress

        f3 = LabelFrame(self.root, text="Steam-Press",font=(font_style,15,"bold"),bg=bg_color,fg=font_color,bd=5)
        f3.place(x=250, y=160)

        lbl_blanket_sp = Label(f3, text="Blanket", font=(font_style, 15, "bold"))
        lbl_blanket_sp.grid(row=0, column=0, padx=5, pady=4, sticky="w")
        blanket_sp_e = Entry(f3, width=5, borderwidth=5, textvariable = self.s_blanket)
        blanket_sp_e.grid(row=0, column=1, padx=10)

        lbl_saree_sp = Label(f3, text="Saree", font=(font_style, 15, "bold"))
        lbl_saree_sp.grid(row=1, column=0, padx=5, pady=4, sticky="w")
        saree_sp_e = Entry(f3, width=5, borderwidth=5, textvariable = self.s_saree)
        saree_sp_e.grid(row=1, column=1, padx=10)

        lbl_shirt_sp = Label(f3, text="Shirt", font=(font_style, 15, "bold"))
        lbl_shirt_sp.grid(row=2, column=0, padx=5, pady=4, sticky="w")
        shirt_sp_e = Entry(f3, width=5, borderwidth=5, textvariable = self.s_shirt)
        shirt_sp_e.grid(row=2, column=1, padx=10)

#items : charak

        f4=LabelFrame(self.root,text="Charak",font=(font_style,15,"bold"),fg=font_color,bg=bg_color, bd=5)
        f4.place(x=250,y=305)

        lbl_saree_c = Label(f4, text="Saree", font=(font_style,15,"bold"))
        lbl_saree_c.grid(row=0, column=0)
        saree_c_e = Entry(f4, width=5, borderwidth=5, textvariable = self.c_saree)
        saree_c_e.grid(row=0, column=1, padx=10)

# bill display:

        f5=Frame(self.root, bd=5, relief="groove")
        f5.place(x=870, y=160, width=400, height=400)
        bill_label = Label(f5, text="Bill Area", font=("arial",15,"bold"),bd=5,relief=GROOVE)
        bill_label.pack(fill=X)
        scroll = Scrollbar(f5,orient=VERTICAL)
        self.textarea = Text(f5, yscrollcommand=scroll.set)
        scroll.pack(side=RIGHT,fill=Y)
        scroll.config(command=self.textarea.yview)
        self.textarea.pack(fill=BOTH, expand=1)

#button frame

        f6=LabelFrame(self.root, text="Bill Menu", relief=GROOVE, bg=bg_color,fg=font_color,font=(font_style,15,"bold"),bd=5)
        f6.place(x=870,y=560)

        total_btn = Button(f6,text="TOTAL",bd=5, bg="White", fg="Black", relief="raised", font=(font_style,15,"bold"))
        total_btn.grid(row=0,column=0,padx=9,pady=5)

        bill_btn = Button(f6, text="BILL", bd=5, bg="White", fg="Black", relief="raised", font=(font_style, 15, "bold"))
        bill_btn.grid(row=0, column=1,padx=9,pady=5)

        clear_btn = Button(f6, text="CLEAR", bd=5, bg="White", fg="Black", relief="raised", font=(font_style, 15, "bold"))
        clear_btn.grid(row=0, column=2, padx=9,pady=5)

        exit_btn = Button(f6, text="EXIT", bd=5, bg="White", fg="Black", relief="raised", font=(font_style, 15, "bold"))
        exit_btn.grid(row=0, column=3, padx=9,pady=5)

        self.bill_layout()

    def total_bill(self):
        self.blanket_dry = 160
        self.saree_dry = 120
        self.shirt_dry = 60

        self.blanket_sp = 0
        self.saree_sp = 30
        self.shirt_sp = 20

        self.saree_c = 15

        self.total_dry_clean_cost = float(
            (self.d_blanket.get()*self.blanket_dry)+
            (self.d_saree.get()*self.saree_dry)+
            (self.d_shirt.get()*self.shirt_dry)
        )
        self.total_steam_press_cost = float(
                (self.s_blanket.get() * self.blanket_sp) +
                (self.s_saree.get() * self.saree_sp) +
                (self.s_shirt.get() * self.shirt_sp)
        )
        self.total_charak_cost = float(
                (self.c_saree.get() * self.saree_c)
        )

        self.DryClean_price.set(str(self.total_dry_clean_cost))
        self.SteamPress_price.set(str(self.total_steam_press_cost))
        self.Charak_price.set(str(self.total_charak_cost))

    def bill_layout(self):
        self.textarea.insert(END, "            WHITE HOUSE DRYCLEANERS           ")
        self.textarea.insert(END, "            Kataria Market  Link Road          ")

    def bill(self):
        pass

root = Tk()

obj = Bill_App(root)
root.mainloop()


# -*- coding: utf-8 -*-
import twitter

# XXX: Go to http://dev.twitter.com/apps/new to create an app and get values
# for these credentials, which you'll need to provide in place of these
# empty string values that are defined as placeholders.
# See https://dev.twitter.com/docs/auth/oauth for more information
# on Twitter's OAuth implementation.

# AUTHENTICATION (OAuth)
# It needs an auth file in the following format:
# Your CONSUMER_KEY
# Your CONSUMER_SECRET
# Your OAUTH_TOKEN
# Your OAUTH_TOKEN_SECRET

def tw_oauth(authfile):
    with open(authfile, "r") as f:
        ak = f.readlines() #read the keys and secrets
    f.close()
    CONSUMER_KEY = ak[0].replace("\n","")
    CONSUMER_SECRET = ak[1].replace("\n","")
    OAUTH_TOKEN = ak[2].replace("\n","")
    OAUTH_TOKEN_SECRET = ak[3].replace("\n","")
    auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
    return twitter.Twitter(auth=auth)

twitter_api = tw_oauth("auth.txt") #Get an twitter_api object

# Nothing to see by displaying twitter_api except that it's now a
# defined variable

print (twitter_api)

###############################################
#Get followers and friends(whom you followed)
#This is a simpler version
#response = twitter_api.followers.ids(screen_name=user_name, count=100) #call first time
user_name= "a232152000"
followers = []
cursor = -1  #start page
while cursor != 0:
    response = twitter_api.followers.ids(screen_name=user_name, cursor=cursor)
    if response is not None:
        followers += response['ids']
        cursor = response['next_cursor']
    if len(followers) > 100 or response is None:
        break

for id in followers:
    f = twitter_api.users.lookup(user_id=id)
    if(f[0]["name"]!="樂仲珉"):
        print (f[0]["name"])

friends = twitter_api.friends.ids(screen_name=user_name, count=100)
for id in friends['ids']:
    f = twitter_api.users.lookup(user_id=id)
    if(f[0]["name"]!="樂仲珉"):
        print (f[0]["name"])

twitter_列出自己的追隨者和朋友/列出自己的追隨者和朋友.py

列出自己的追隨者和朋友.py


import pandas as pd
import tensorflow as tf
import numpy as np

#configuracoes
vocab_size = 10000
embedding_dim = 16
max_length = 100
trunc_type='post'
padding_type='post'
oov_tok = "<OOV>"

print("### Etapa 1 - Carregar dados e organizar")

# carrega arquivo com as avaliacoes
sentimento_df = pd.read_csv("reviews.csv")

print(sentimento_df.info())
print(sentimento_df.head())
print(sentimento_df['content'].head())
print(sentimento_df['score'].head())

# Converte notas de 1 a 5 em 0 e 1
def to_sentiment(rating):
  rating = int(rating)
  if rating <= 2:
    return 0
  else:
    return 1

sentimento_df['sentiment'] = sentimento_df.score.apply(to_sentiment)

# Organiza os dados em Treino e Teste
training_size = 7000
training_sentences = sentimento_df['content'].iloc[0:training_size].copy()
testing_sentences = sentimento_df['content'].iloc[training_size:].copy()
print(training_sentences.info())
print(training_sentences.head())

training_labels = sentimento_df['sentiment'].iloc[0:training_size].copy()
testing_labels = sentimento_df['sentiment'].iloc[training_size:].copy()

print("### Etapa 2 - Converter palavras em numeros")
from tensorflow.keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=vocab_size, oov_token=oov_tok)
tokenizer.fit_on_texts(training_sentences)
word_index = tokenizer.word_index

print("### Etapa 3 - Criar as frases/sequencias")
from tensorflow.keras.preprocessing.sequence import pad_sequences
sequencias_treinamento = tokenizer.texts_to_sequences(training_sentences)
padded_treinamento = pad_sequences(sequencias_treinamento, maxlen=max_length, padding=padding_type, truncating=trunc_type)
sequencias_teste = tokenizer.texts_to_sequences(testing_sentences)
padded_teste = pad_sequences(sequencias_teste, maxlen=max_length, padding=padding_type, truncating=trunc_type)

print("### Etapa 4 - Criando a rede neural")
model = tf.keras.Sequential([
  tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
  tf.keras.layers.GlobalAveragePooling1D(),
  tf.keras.layers.Dense(24, activation="relu"),
  tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

print("### Etapa 5 - Treinando a rede")
num_epochs = 30
history = model.fit(padded_treinamento, training_labels, epochs=num_epochs,
                    validation_data=(padded_teste, testing_labels), verbose=2)

print("### Etapa 6 - Final - Testando com uma nova frase")

frases_avaliacao = ["Eu não gostei de nada disso", "Estava excelente, principalmente o hamburguer", "Nem tão bom, nem tão ruim"]

sequencia = tokenizer.texts_to_sequences(frases_avaliacao)
padded = pad_sequences(sequencia, maxlen=max_length, padding=padding_type, truncating=trunc_type)

probs = model.predict(padded)

class_names = ['Negativo:', 'Positivo:']
for index, frase in enumerate(frases_avaliacao):
  print(class_names[round(probs[index][0])], probs[index], frase)


from django.urls import path, include
from .views import ExpenseDetailView, ExpenseList, DeleteMultipleExpensesView

# /api/expense/
urlpatterns = [
    path("", ExpenseList.as_view(), name="expenses"),
    path("<uuid:pk>", ExpenseDetailView.as_view(), name="expense"),
    path(
        "delete_multiple", DeleteMultipleExpensesView.as_view(), name="delete_multiple"
    ),
]


"""
2. Ленивое объединение
Дано: 2 списка произвольной длины.
Задание: написать функцию, которая возвращает результат объединения этих списков. Используйте функцию itertools.chain.
Пример:
list(f([1, 2], [3, 4])), результат: [1, 2, 3, 4]
"""
from itertools import chain

def union_num(num1, num2):
    result = list(chain(num1, num2)) #Создаёт итератор представляющий единой цепочкой элементы указанных объектов.
    return result

num1 = input("Enter number1 ").split()
int_num1 = list(map(int, num1))
num2 = input("Enter number2 ").split()
int_num2 = list(map(int, num2))
res = union_num(int_num1, int_num2)

print(res)


from singly import *

def partition(ll: Singly, part: int):
    right, left = [], []

    for x in ll.llist:
        if x >= part:
            right.append(x)

        else:
            left.append(x)

    newll = Singly(left+right)
    return newll

if __name__ == '__main__':
    l = map(int, input().split())
    part = int(input())
    ll = Singly(l)

    print(partition(ll, part))


from flask import render_template, url_for, redirect, flash, flash

from lennonwall import db, app
from lennonwall.forms import MemoForm
from lennonwall.models import Message

@app.route('/', methods=['GET', 'POST'])
def index():
	form = MemoForm()
	if form.validate_on_submit():
		name=form.name.data
		body=form.body.data
		message=Message(name=name,body=body)
		db.session.add(message)
		db.session.commit()
		flash('Memo Posted')
		return redirect(url_for('index'))
	messages = Message.query.order_by(Message.time.desc()).all()
	return render_template('index.html', form=form, messages=messages)


"""Add date created field to comments

Revision ID: 70ae970d2b67
Revises: 5ab29d80ef60
Create Date: 2022-02-13 17:13:08.529843

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '70ae970d2b67'
down_revision = '5ab29d80ef60'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('comments', sa.Column('date_created', sa.DateTime(), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('comments', 'date_created')
    # ### end Alembic commands ###


import sys
# Remove current dir from sys.path, otherwise distutils will peek up our
# module instead of system one.
sys.path.pop(0)
sys.path.insert(0, '..')
from setuptools import setup
import metadata

NAME = 'collections'

setup(name='micropython-' + NAME,
      version='0.0.2',
      description='Top-level collection package for MicroPython',
      url=metadata.url,
      author=metadata.author_upy_devels,
      author_email=metadata.author_upy_devels_email,
      license='MIT',
      packages=[NAME])


# https://leetcode.com/problems/is-graph-bipartite/
from collections import defaultdict
class Solution:
    def __init__(self):
        self.res = True

    def isBipartite(self, graph: List[List[int]]) -> bool:
        # our final graph is 'g'
        # we need to re-form the graph since the v to u edges are not given in the input
        g = defaultdict(list)
        maxnode = 0

        for u, listv in enumerate(graph):
            g[u] = listv

            maxnode = max(maxnode, u)
            for v in listv:
                g[v].append(u)
                maxnode = max(maxnode, u, v)

        # for bipartite, we do graph coloring algorithm with 2 colors
        # ie, is it possible to assign each vertex a color (blue or red) such that
        # blue vertex is never next to red vertex and vice versa
        # if this is possible, graph is bipartite
        # https://www.youtube.com/watch?v=0ACfAqs8mm0
        blue = 1
        red = -blue
        unassigned = 0
        color = dict()

        for k in range(0, maxnode + 1):
            color[k] = unassigned

        def dfs(n, c):
            # if graph node is already colored with right color
            # then no action needed
            if color[n] == c:
                return

            # if node is colored with a color you're not intending it be colored with
            # then graph is not bipartite
            if color[n] == -c:
                self.res = False
                return

            # color node n with color c
            color[n] = c

            # go into neighbors of n, and attempt to color them with opposite color
            for nbor in graph[n]:
                dfs(nbor, -c)

        # do dfs on all unvisited (unassigned color nodes)
        # why? cos graph could be disconnected
        # so if we only did dfs(node=0), then we may only partially do dfs without exploring
        # other disconnected sub-graphs
        for i in range(maxnode+1):
            if color[i] == unassigned:
                dfs(i, blue)

        return self.res


# the starting zoom level
starting_zoom=2.5
# font size of the application
font_size=12
# use 1 to set custom paper colour, otherwise 0
USE_ALPHA=0
# background colour in the form #xxxxxx or #xxx or x colour (example: gray90)
PAGE_BG="gray90"
# right pad for the horizontal scrollbar
hsb_pad=16


'''
문3) 정규표현식을 적용하여 person을 대상으로 주민번호 양식이 올바른
     사람을 대상으로 다음과 같은 출력 예시와 같이 주민등록번호를 출력하시오.

   <출력 예시>
유관순 750905-*******
홍길동 850905-*******
강감찬 770210-*******
'''

import re # 정규표현식 패키지 임포트

person = """유관순 750905-2049118
홍길동 850905-1059119
강호동 790101-5142142
강감찬 770210-1542001"""

for line in person.split("\n"):
    name=line[:3]
    jumin=re.findall('[가-힣]{3} [0-9]{6}-[1234]\\d{6}', line)

    if jumin:
        str_jumin=jumin[0]
        result=str_jumin[:10]+'-*******'
        print(result)

for line in person.split("\n"):
    name = line[:3]
    jumin = re.findall('[가-힣]{3} \\d{6}-[1234]\\d{6}', line)
    #print(jumin)

    if jumin :
        str_jumin = jumin[0]
        result = str_jumin[:10] + '-*******'
        print(result)

text1=re.sub('[0-9]{7}','*******',person)
print(text1)

result=[]
for line in person.split('\n'):
    name=line[:3]
    jumin=re.findall('[가-힝]{3}\\d{6}-[1234]\\{6}',line)

    if jumin:
        str_jumin=jumin[0]
        result=str_jumin[:10]+'*******'
        print(result)

print(result)


import os
import sys
import tkinter.filedialog
import tkinter.messagebox

# Defining all formats possible
audio_format = (".aif", ".cda", ".flac", ".mid", ".mid", ".mp3", ".mpa", ".ogg", ".wav", ".wma", ".wpl")
video_format = (
    ".3g2", ".3gp", ".asf", ".avi", ".flv", ".m4v", ".mov", ".mkv", ".mp4", ".mpg", ".rm", ".srt", ".swf", ".vob",
    ".webm", ".wmv")
image_format = (
    ".bmp", ".dds", ".gif", ".heic", ".jpg", ".png", ".psd", ".pspimage", ".tga", ".thm", ".tif", ".tiff",
    ".yuv")
document_format = (".pdf", ".txt", ".doc", ".docx", ".log", ".msg", ".odt", ".pages", ".rtf", ".tex", ".wpd", ".wps")
installer_format = (".exe", ".msi")

def file_mover(file, destfoldername):
    # Moving file function
    try:
        os.mkdir(dir_path + '/' + destfoldername)
        os.rename(dir_path + "/" + file, dir_path + "/" + destfoldername + "/" + file)
    except FileExistsError:
        os.rename(dir_path + "/" + file, dir_path + "/" + destfoldername + "/" + file)

def clean():
    # Parse all file and apply file_mover() depending on the extension
    for element in dir_content:
        try:
            if element.endswith(audio_format):
                file_mover(element, "Audio")
            elif element.endswith(video_format):
                file_mover(element, "Videos")
            elif element.endswith(image_format):
                file_mover(element, "Images")
            elif element.endswith(document_format):
                file_mover(element, "Documents")
            elif element.endswith(installer_format):
                file_mover(element, "Installers")
        except FileNotFoundError:
            print("File not found: " + element)
            continue

root = tkinter.Tk()
root.withdraw()

dir_path = tkinter.filedialog.askdirectory()
dir_content = os.listdir(dir_path)

msgbox = tkinter.messagebox.askquestion('Python Cleaner', "Are you sure you want to clean " + dir_path + " ?", icon = 'warning')
if msgbox == 'yes':
    clean()
else:
    sys.exit()


#!/usr/bin/env python3
import os
import sys
import argparse
from collections import Counter
from collections import defaultdict
PARSER = argparse.ArgumentParser(
    prog="Alleles_collectors_curve.py",
    description="Prepare summary for collector's curve")
PARSER.add_argument(
    "-l",
    "--list",
    type=str,
    help="Files List to process in a specific date order",
)
PARSER.add_argument(
    "directory", type=str, help="Directory having the multiple GNUVID reports (time organized) files to analyze"
)
if len(sys.argv) == 1:
    PARSER.print_help()
    sys.exit(0)
ARGS = PARSER.parse_args()
############################
QUERY = ARGS.directory
QUERY_LIST = []
for file in os.listdir(QUERY):
    if file.endswith(".txt"):
        QUERY_LIST.append(QUERY + file)
print("You provided folder of {} GNUVID report files".format(len(QUERY_LIST)))
input_file_object = open(QUERY_LIST[0], 'r')
Genes = input_file_object.readline().split('\t')[4:15]
indices = [4,5,6,7,8,9,10,11,12,13,14]
input_file_object.close()
if ARGS.list:
    QUERY_LIST = []
    list_file_object = open(ARGS.list, 'r')
    for line in list_file_object:
        line = line.rstrip()
        QUERY_LIST.append(QUERY + line)
#Times = ['mid_Jan','end_Jan','mid_Feb','end_Feb','mid_Mar','end_Mar','mid_Apr',
#'end_Apr','mid_May','end_May','mid_June','end_June','mid_July']
Times = ['end_jan','Feb','mid_Mar','end_Mar','mid_Apr',
'end_Apr','mid_May','end_May_mid_June','end_June']
#####################
time_dict_raw = defaultdict(list)
time_dict_percent = defaultdict(list)
Output_obj = open('Allelic_diversity_California_results.txt', "w")
Output_obj2 = open('Novel_STs_California_results.txt', "w")
Output_obj2.write('Time_period\tNumber_of_Novel_STs\tNumber of circulating STs\tNovel_STs_percentage\tSTs\n')
All_STs = set()
for QUERYFILE in QUERY_LIST:
    counter = 0
    QUERYFILE_OBJECT = open(QUERYFILE, "r")
    QUERYFILE_OBJECT.readline()
    time_period = QUERYFILE.rsplit('/',1)[-1].split('California_')[-1].split('.txt')[0]
    Alleles_ST_numbers_dict = defaultdict(list)
    for Gene in Genes:
        Alleles_ST_numbers_dict[Gene]
    for line in QUERYFILE_OBJECT:
        counter += 1
        line = line.rstrip()
        line_list = line.split('\t')
        for Gene,Index in zip(Genes,indices):
            Alleles_ST_numbers_dict[Gene].append(int(line_list[Index]))
    #########Reporting############
    #print(time_period, '\t', counter)
    for record in Genes:
        period_alleles = Alleles_ST_numbers_dict[record]
        #time_dict_set[time_period].extend(period_alleles)
        time_dict_raw[time_period].append(str(len(set(period_alleles))))
        time_dict_percent[time_period].append('{:.2f}'.format(len(set(period_alleles))/counter))
    time_dict_raw[time_period].append(str(counter))
    time_dict_percent[time_period].append(str(counter))
    #capturing novel STs in each period
    period_STs = set(Alleles_ST_numbers_dict['ST'])
    Novel_STs = period_STs - All_STs
    Novel_STs_list = [('ST'+str(x)) for x in Novel_STs]
    All_STs = All_STs.union(period_STs)
    Novel_STs_percent = '{:.2f}'.format(len(Novel_STs)/len(period_STs))
    Output_obj2.write('{}\t{}\t{}\t{}\t{}\n'.format(time_period,len(Novel_STs),len(period_STs),Novel_STs_percent,'\t'.join(Novel_STs_list)))
print('\t','\t'.join(Genes),'\tTotal')
Output_obj.write('\t'+'\t'.join(Genes)+'\tTotal\n')
for period in Times:
    print(period,'\t','\t'.join(time_dict_raw[period]))
    Output_obj.write(period+'\t'+'\t'.join(time_dict_raw[period])+'\n')
print('########################')
Output_obj.write('########################\n')
print('\t','\t'.join(Genes),'\tTotal')
Output_obj.write('\t'+'\t'.join(Genes)+'\tTotal\n')
for period in Times:
    print(period,'\t','\t'.join(time_dict_percent[period]))
    Output_obj.write(period+'\t'+'\t'.join(time_dict_percent[period])+'\n')
#print(record,'\t',len(set(Alleles_ST_numbers_dict[record])))


# © 2020 Comunitea - Javier Colmenero <javier@comunitea.com>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import models, fields, api, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, float_compare, float_round
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_round
from dateutil.relativedelta import relativedelta

class SaleOrder(models.Model):

    _inherit = "sale.order"

    def _compute_available_deliveries(self):
        for order in self:
            domain = [('ps_sync', '=', False)]
            if order.partner_shipping_id:
                country_id = order.partner_shipping_id.country_id.id
                state_id = order.partner_shipping_id.state_id.id

                if state_id:
                    domain += [
                        '|',
                        ('state_ids', '=', False),
                        ('state_ids', '=', state_id)]

                if country_id:
                    domain += [
                        '|',
                        ('country_ids', '=', False),
                        ('country_ids', '=', country_id)]

            carrier_ids = self.env['delivery.carrier'].search(domain).ids
            return carrier_ids

    ship_cost = fields.Monetary(string='Ship Cost', compute="_compute_ship_cost")
    ship_price = fields.Monetary(string='Ship Price', compute="_compute_ship_price")
    num_line = fields.Char(string='Nº Line')
    rejected = fields.Boolean('Rejected')
    rejected_reason = fields.Text('Rejected Reason')
    delivered = fields.Selection([('delivered', 'Delivered'),
                                    ('not_delivered', 'Not Delivered'),
                                    ('partially', 'Partially Delivered')],
                                string='Delivered',
                                default='not_delivered',
                                compute="_compute_delivered",
                                store=True)
    delivery_blocked = fields.Boolean("Entrega bloqueada", default=False)
    admin_fact = fields.Float("Factor admin.", compute="_compute_fa",
                                readonly=True,
                                store = True)
    with_reserves = fields.Boolean("With Reserves", compute="_compute_reserves")
    available_deliveries_ids = fields.Many2many('delivery.carrier')

    @api.onchange('partner_shipping_id')
    def onchange_delivery(self):
        if self.partner_shipping_id:
            self.available_deliveries_ids = self._compute_available_deliveries()

    # Se usa en una acción de servidor
    def get_activity_deadline(self, activity_type_id):

        # Date.context_today is correct because date_deadline is a Date and is meant to be
        # expressed in user TZ
        base = fields.Date.context_today(self)

        return base + relativedelta(**{activity_type_id.delay_unit: activity_type_id.delay_count})

    def _compute_reserves(self):
        for order in self:
            order.with_reserves = any(line.reserved > 0 for line in order.order_line)

    @api.depends('partner_id', 'partner_invoice_id')
    def _compute_fa(self):
        for order in self:
            admin_fact = 0
            if order.partner_invoice_id and \
                    order.partner_invoice_id.admin_fact:
                admin_fact = order.partner_invoice_id._get_admin_fact()
            else:
                admin_fact = order.partner_id._get_admin_fact()
            order.admin_fact = admin_fact

    @api.multi
    def print_quotation(self):
        self.filtered(lambda s: s.state == 'draft').write({'state': 'sent'})
        if self.company_id.cost_sheet_sale:
            return self.env.ref('sale.action_report_pro_forma_invoice')\
                .with_context(discard_logo_check=True).report_action(self)
        else:
            return self.env.ref('custom_documents_lupeon.action_report_saleorder_lupeon')\
                .with_context(discard_logo_check=True).report_action(self)

    def _compute_ship_cost(self):
        for order in self:
            order.ship_cost = sum(order.picking_ids.mapped('ship_cost'))

    def _compute_ship_price(self):
        for order in self:

            ship_line =  order.order_line.filtered(lambda line: line.product_id.default_code=='SHIP')
            if ship_line:
                order.ship_price = ship_line[0].price_subtotal
            else:
                order.ship_price = 0

    @api.depends('picking_ids.delivered')
    def _compute_delivered(self):
        for order in self:
            delivery_pickings = order.picking_ids.filtered(lambda pick: pick.picking_type_id.code=='outgoing' and pick.state != 'cancel')
            order.delivered = 'not_delivered'
            if delivery_pickings:
                if all(picking.delivered for picking in delivery_pickings):
                    order.delivered = 'delivered'
                elif any(picking.delivered for picking in delivery_pickings):
                    order.delivered = 'partially'
                else:
                    order.delivered = 'not_delivered'

    @api.multi
    def _prepare_invoice(self):
        vals = super()._prepare_invoice()
        if self.num_line:
            vals['num_line'] = self.num_line
        return vals

    @api.multi
    def action_confirm(self):
        """
        Check if order requires client_order_ref.
        Creation of product sheet ids
        """
        for order in self:
            if (order.partner_id.require_num_order and not
                    order.client_order_ref):
                order.client_order_ref = 'PENDIENTE'

            # Solo lupeon
            if (order.company_id.id == 1 and not order.commitment_date):
                raise UserError(_('You need to set commitment date.'))

        res = super().action_confirm()
        return res

    @api.multi
    def write(self, vals):
        """
        Propagar cambio de referencia a la factura
        """
        res = super().write(vals)
        if vals.get('client_order_ref'):
            invoices = self.mapped('invoice_ids')
            if invoices:
                invoices.write({'name': vals['client_order_ref']})
        return res

    @api.multi
    def create_procurements_all(self):
        for order in self:
            order.order_line.create_procurements()

    @api.multi
    def action_barcode_delivery(self):
        '''
        This function returns an action that display existing delivery orders
        of given sales order ids. It can either be a in a list or in a form
        view, if there is only one delivery order to show.
        '''
        pickings = self.mapped('picking_ids').filtered(lambda x: x.state == 'assigned')
        if pickings:
            if len(pickings) == 1:
                return pickings[0].open_picking_client_action()
            else:
                pt = pickings.mapped('picking_type_id')
                if pt:
                    res = pt[0].get_action_picking_tree_ready_kanban()
                    res['context'] = ('{\n'
                        "            'form_view_initial_mode': 'edit',\n"
                        "            'search_default_picking_type_id': [%s],\n"
                        "            'default_picking_type_id': %s,\n"
                        "            'contact_display': 'partner_address',\n"
                        "            'search_default_available': 1,\n"
                        "            'force_detailed_view': True,\n"
                        '        }' % (pt.id, pt.id)
                    )
                    res['domain'] = [('id', 'in', pickings.ids)]

                return res
        raise UserError(_('No pickings in assigned state to open'))
        return

    # @api.depends('margin', 'amount_untaxed', 'ship_cost', 'payment_mode_id')
    # def _compute_percent(self):
    #     for order in self:
    #         if order.margin and order.amount_untaxed:
    #             order.percent = ((order.margin - order.ship_cost - order.payment_mode_id.payment_cost) / order.amount_untaxed) * 100

    @api.depends('order_line.margin', 'ship_cost', 'payment_mode_id')
    def _product_margin(self):
        if self.env.in_onchange:
            for order in self:
                payment_cost = order.payment_mode_id and order.payment_mode_id.payment_cost or 0.0
                order.margin = sum(order.order_line.filtered(lambda r: r.state != 'cancel').mapped('margin')) - \
                    order.ship_cost - payment_cost
        else:
            # On batch records recomputation (e.g. at install), compute the margins
            # with a single read_group query for better performance.
            # This isn't done in an onchange environment because (part of) the data
            # may not be stored in database (new records or unsaved modifications).
            grouped_order_lines_data = self.env['sale.order.line'].read_group(
                [
                    ('order_id', 'in', self.ids),
                    ('state', '!=', 'cancel'),
                ], ['margin', 'order_id'], ['order_id'])
            for data in grouped_order_lines_data:
                order = self.browse(data['order_id'][0])
                payment_cost = order.payment_mode_id and order.payment_mode_id.payment_cost or 0.0
                order.margin = data['margin'] - \
                    order.ship_cost - payment_cost

class SaleOrderLine(models.Model):

    _inherit = "sale.order.line"

    reserved = fields.Boolean('Reserved', compute="_is_reserve")
    qty_reserved = fields.Float('Qty Reserved', compute="_is_reserve")
    real_stock = fields.Float('Real stock', related="product_id.qty_available")

    @api.depends('product_id', 'product_uom_qty', 'qty_delivered', 'state')
    def _compute_qty_to_deliver(self):
        """ Based on _compute_qty_to_deliver method of sale.order.line
            model in Odoo v13 'sale_stock' module.
            This method is overwrited ofr Customer especification
        """
        for line in self:
            #line.qty_to_deliver = line.product_uom_qty - line.qty_delivered
            line.display_qty_widget = (line.product_type == 'product')

    @api.multi
    def _is_reserve(self):
        for line in self:
            if line.move_ids.filtered(lambda x: x.state in ('assigned', 'partially_available')):
                line.reserved = True
                line.qty_reserved = sum(move.reserved_availability for move in line.move_ids)

    @api.multi
    def _action_launch_stock_rule_anticiped(self):
        """
        Copia del método pero sin que se salte la logica por no estart en
        estado sale
        """
        precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
        errors = []
        for line in self:
            if not line.product_id.type in ('consu','product'):
                continue
            qty = line._get_qty_procurement()
            if float_compare(qty, line.product_uom_qty, precision_digits=precision) >= 0:
                continue

            group_id = line.order_id.procurement_group_id
            if not group_id:
                group_id = self.env['procurement.group'].create({
                    'name': line.order_id.name, 'move_type': line.order_id.picking_policy,
                    'sale_id': line.order_id.id,
                    'partner_id': line.order_id.partner_shipping_id.id,
                })
                line.order_id.procurement_group_id = group_id
            else:
                # In case the procurement group is already created and the order was
                # cancelled, we need to update certain values of the group.
                updated_vals = {}
                if group_id.partner_id != line.order_id.partner_shipping_id:
                    updated_vals.update({'partner_id': line.order_id.partner_shipping_id.id})
                if group_id.move_type != line.order_id.picking_policy:
                    updated_vals.update({'move_type': line.order_id.picking_policy})
                if updated_vals:
                    group_id.write(updated_vals)

            values = line._prepare_procurement_values(group_id=group_id)
            product_qty = line.product_uom_qty - qty

            procurement_uom = line.product_uom
            quant_uom = line.product_id.uom_id
            get_param = self.env['ir.config_parameter'].sudo().get_param
            if procurement_uom.id != quant_uom.id and get_param('stock.propagate_uom') != '1':
                product_qty = line.product_uom._compute_quantity(product_qty, quant_uom, rounding_method='HALF-UP')
                procurement_uom = quant_uom

            try:
                self.env['procurement.group'].run(line.product_id, product_qty, procurement_uom, line.order_id.partner_shipping_id.property_stock_customer, line.name, line.order_id.name, values)
            except UserError as error:
                errors.append(error.name)
        if errors:
            raise UserError('\n'.join(errors))
        return True

    @api.multi
    def create_procurements(self):
        for line in self:
            # Se necesita la fecha de confirmación para que no falle
            line.order_id.confirmation_date = fields.Datetime.now()
            line._action_launch_stock_rule_anticiped()
            line.move_ids.filtered(lambda x: x.state == 'confirmed')._action_assign()

        return {'type': 'ir.actions.client', 'tag': 'reload'}

    @api.multi
    def _get_display_price(self, product):
        """
        Actualizar solo en compañía dativic
        """
        price = super()._get_display_price(product)
        admin_fact = self.order_id.admin_fact

        if  admin_fact and self.order_id.company_id.id != 1:
            price_precision = self.env['decimal.precision'].precision_get(
            'Product Price')
            price = float_round(price * (1 + admin_fact/100), price_precision)
        return price

    @api.depends('product_id', 'customer_lead', 'product_uom_qty',
                 'order_id.warehouse_id', 'order_id.commitment_date')
    def _compute_qty_at_date(self):
        """
        Add reserved qty to compute calc
        """
        super()._compute_qty_at_date()
        for line in self:
            new_qty = line.virtual_available_at_date + line.qty_reserved
            line.virtual_available_at_date = new_qty

    def unlink(self):
        for line in self:
            if line.move_ids:
                line.move_ids._action_cancel()
                # line.move_ids._do_unreserve()
                line.move_ids.unlink()
        super().unlink()

class SaleOrderState(models.Model):
    _inherit = "sale.order.state"

    trigger_delivered = fields.Boolean('Trigger Delivered')
    pending_payment = fields.Boolean('Pending Payment')


from utils import CountingChannels
from db import db
from utils import utils
from discord import guild as guildClass
from events import EventHelpers
import discord
from utils import DiscordUtils
from logs.log import print
#This file contains all the code for how the bot should handle different events
async def on_ready(bot):

    print(f"{bot.user} has connected to Discord!")
    for guild in bot.guilds:
        print(f"Performing startup for guild {guild.name}")
        print(f"Fetching channels from db...")
        channels = db.getChannels(guild.id)
        for channelId in channels:
            print(f"Checking if channel with id {channelId} still exists")
            channel = DiscordUtils.find(lambda channel, channelId: channel.id == channelId, guild.voice_channels, channelId)
            if channel is None:
                print(f"Channel with id {channelId} doesn't exist. Removing from DB")
                db.deleteChannel(channelId)
            else:
                print(f"Channel with id {channelId} still exists")
        await CountingChannels.calculateChannels(None, "startup", None, guild)
    print("Standing by")

async def on_guild_join(guild: guildClass):
    utils.addPrefixToGuildIfNone(guild)
    utils.removeDeletedChannelsFromDB
    print(f"Bot joined guild {guild.name} (ID:) {guild.id}")
    logs = await guild.audit_logs(limit=1, action=discord.AuditLogAction.bot_add).flatten()
    inviter = logs[0].user
    await inviter.send(embed=EventHelpers.guildJoinMessage(inviter, guild))

async def on_guild_leave(guild):
    print(f"Bot left guild {guild.name} (ID:) {guild.id}")

async def on_message(bot, message):
    if not message.mentions:
        return
    for mention in message.mentions:
        if mention == bot.user:
            prefix = db.getPrefix(message.guild.id)
            await message.channel.send(f"My prefix for this server is: {prefix}")

async def on_member_update(before, after):
    if before.roles != after.roles:
        await CountingChannels.calculateChannels(after, "role changed", None, after.guild)

async def on_member_join(member):
    await CountingChannels.calculateChannels(member, "member joined", None, member.guild)

async def on_member_remove(member):
    await CountingChannels.calculateChannels(member, "member left", None, member.guild)

async def on_guild_channel_delete(channel):
    type = db.getType(channel)
    if (type != None):
        check = True
    else:
        check = False
    db.deleteChannel(channel.id)
    if (check):
        print(f"Counting Channel {channel} deleted in guild {channel.guild}")


import sys
import collections

dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

T = int(sys.stdin.readline())

def BFS(heightDot, widthDot):
    visited[heightDot][widthDot] = True
    q = collections.deque([(heightDot, widthDot)])
    while q:
        y, x = q.popleft()
        for i in range(4):
            newX = x + dx[i]
            newY = y + dy[i]
            if(newX >= 0 and newX < M and newY >= 0 and newY < N):
                if(arr[newY][newX] == 1 and visited[newY][newX] == False):
                    visited[newY][newX] = True
                    q.append((newY, newX))

for i in range(T):
    M, N, K = map(int, sys.stdin.readline().split(" "))
    arr = [[0 for i in range(M)] for j in range(N)]

    for i in range(K):
        x, y = map(int, sys.stdin.readline().split(" "))
        arr[y][x] = 1

    visited = [[False for i in range(M)] for i in range(N)]
    count = 0 # 흰지렁이 개수

    for i in range(N):
        for j in range(M):
            if(arr[i][j] == 1 and visited[i][j] == False):
                BFS(i, j)
                count += 1
    print(count)


import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(12).reshape(3, 4),
                  columns=['A', 'B', 'C', 'D'])

df
df_tmp = df.drop(['B', 'C'], axis=1)
df_tmp

df_tmp2 = df.drop([0,1]) # es lo mismo que df.drop([0,1], axis=0) o df.drop([0,1],0)
df_tmp2


import sqlite3 as sl
import default_values

class DB:

    def __init__(self, col='', tbl='', cons=None):
        if cons is None:
            cons = []
        self.col = col
        self.tbl = tbl
        self.cons = cons

    # Experimental - if conds (conditions) param in helper is present, attempts to split and add to function
    def __craft(self, lst):
        print(len(lst))
        if len(lst) > 1:
            x = 'WHERE ' + ' AND '.join(lst)
            print(x)
            return x
        else:
            x = 'WHERE ' + lst[0]
            return x

    # Experimental - Attempts to get parameters from user input to create queries
    def helper(self, sport, col, tbl, conds):
        if sport == 'NBA':
            con = sl.connect(default_values.nba_db_connection)
        elif sport == 'NHL':
            con = sl.connect(default_values.nhl_db_connection)

        if conds is None:
            with con:
                data = con.execute(f"SELECT {col} FROM {tbl}")
                for row in data:
                    print(row)
        else:
            with con:
                data = con.execute(f"SELECT {col} FROM {tbl} {self.__craft(conds)}")
                for row in data:
                    print(row)

    # Allows user to enter queries manually
    def manual(self, sport, statement):
        if sport == 'NBA':
            con = sl.connect(default_values.nba_db_connection)
        elif sport == 'NHL':
            con = sl.connect(default_values.nhl_db_connection)

        with con:
            data = con.execute(statement)
            for row in data:
                print(row)

    # Experimental - Lists unique days entered into DB. Helps identify missing values or where user last ran
    def list_days(self, sport, tbl):
        if sport == 'NBA':
            con = sl.connect(default_values.nba_db_connection)
        elif sport == 'NHL':
            con = sl.connect(default_values.nhl_db_connection)

        with con:
            data = con.execute(f'SELECT DISTINCT day FROM {tbl}')
            for row in data:
                print(row[0])


input = [*map(str.split, open("input.txt").read().splitlines())]

end = len(input) - 1

def run(target, replacement):
    acc, ptr = 0, 0
    history = set()
    while ptr not in history:
        op, value = input[ptr]
        history.add(ptr)
        if ptr == target:
            # print(target, op, replacement)
            op = replacement
        if ptr == end:
            print(f"part2: acc:{acc}; target:{target},{input[target][0]}->{replacement}")
            return(True)
        if op == "acc":
            acc += int(value)
        if op == "nop":
            pass
        if op == "jmp":
            ptr += int(value)
        else:
            ptr += 1
    return(False)

for i, (op, acc) in enumerate(input):
    if op=="nop":
        if run(i, "jmp"):
            break
    elif op=="jmp":
        if run(i, "nop"):
            break


class Solution:
    def maxSubArray(nums) -> int:
        if nums is None:
            return 0
        ret = 0
        maxsum = nums[0]
        for i in range(len(nums)):
            if ret <= 0:
                ret = nums[i]
            else:
                ret += nums[i]

            if ret > maxsum:
                maxsum = ret
        return maxsum

print(Solution.maxSubArray( [-2,1,-3,4,-1,2,1,-5,4]))

42_ 连续子数组的最大和.py


#!/usr/bin/env python3

import pygame, math, sys, os, random
from colours import *

def drawPixel(surface, color, pos):
    pygame.draw.line(surface, color, pos, pos)

def draw4Pixels(surface, color, pos):
	#points = [pos, (pos[0]+1, pos[1]), (pos[0]+1, pos[1]+1), (pos[0], pos[1]+1)]
	#pygame.draw.polygon(surface, color, points)
    pygame.draw.line(surface, color, pos, (pos[0]+1, pos[1]), 2)

def main():
	pygame.init()
	SCREEN_WIDTH = 800
	SCREEN_HEIGHT = 600

	DISPLAY = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
	CLOCK = pygame.time.Clock()
	totalTime = 0

	maxFire = len(FIRE)-1
	minFire = 0

	pixelMap = []

	fireWidth = 150
	fireHeight = 80

	doGenerate = True

	wind = -5	# -5 to 5
	decaySpeed = 1

	for i in range(fireHeight):
		pixelMap.append([minFire for j in range(fireWidth)])

	fireBoxStart = (SCREEN_WIDTH//2-fireWidth, SCREEN_HEIGHT-fireHeight*2)

	while True:
		DISPLAY.fill(BLACK)
		deltaT = CLOCK.get_time()/1000
		totalTime += deltaT

		heldKeys = pygame.key.get_pressed()
		for e in pygame.event.get():
			if e.type == pygame.QUIT:
				pygame.quit()

			elif e.type == pygame.KEYDOWN:
				heldKeys = pygame.key.get_pressed()
				if (heldKeys[pygame.K_RCTRL] or heldKeys[pygame.K_LCTRL]) and\
					(heldKeys[pygame.K_w] or heldKeys[pygame.K_q]):
					pygame.quit()

				if e.key == pygame.K_SPACE:
					doGenerate = not doGenerate
					if not doGenerate:
						pixelMap[-1] = [0 for j in range(fireWidth)]
				elif e.key == pygame.K_a:
					wind -= 1
					if wind < -5:
						wind = -5
				elif e.key == pygame.K_d:
					wind += 1
					if wind > 5:
						wind = 5

		if doGenerate:
			pixelMap[-1] = [maxFire for j in range(fireWidth)]

		for row in range(len(pixelMap)):
			for p in range(len(pixelMap[row])):
				windRand = random.randint(1,10)
				hor = 0
				if windRand <= wind+3:
					hor = 1
				elif windRand > wind+6:
					hor = -1

				if row > 0 and p+hor < len(pixelMap[row]) and p+hor >= 0:
					pixelMap[row-1][p+hor] = pixelMap[row][p] - (random.randint(2, 9)//3)
					if pixelMap[row-1][p+hor] < 0:
						pixelMap[row-1][p+hor] = 0

		for row in range(len(pixelMap)):
			for p in range(len(pixelMap[row])):
				val = pixelMap[row][p]
				draw4Pixels(DISPLAY, FIRE[val], (fireBoxStart[0]+p*2, fireBoxStart[1]+row*2))

		pygame.display.update()
		CLOCK.tick(24)

if __name__ == "__main__":
	main()


from zrb.config.config import logging_level
from zrb.helper.accessories.color import colored
import logging

# create logger
logger = logging.getLogger('zrb')
logger.setLevel(logging_level)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging_level)

# create formatter
formatter = logging.Formatter(
    colored('%(levelname)-6s %(asctime)s', attrs=['dark']) + ' %(message)s'
)

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)