"""A store for Event objects."""
from DesignationCache import DesignationCache
from Event import Event, EventFileFlags, EventSource
from FileStore import FileStore
from FileFactory import FileFactory
from math import floor
from constants import FD_OPEN, FD_CLOSE
from utils import time2Str, debugEnabled
import sys

class EventStore(object):
    """A store for Event objects.

    EventStore is a store for Events. It sorts all Events by timestamp to
    allow their simulation and the building of a filesystem and information
    flow models.
    """
    __event_store = None

    @staticmethod
    def get():
        """Return the EventStore for the entire application."""
        if not EventStore.__event_store:
            EventStore.__event_store = EventStore()
        return EventStore.__event_store

    @staticmethod
    def reset():
        EventStore.__event_store = None

    def __init__(self):
        """Construct an EventStore."""
        super(EventStore, self).__init__()
        self.clear()

    def clear(self):
        """Empty the EventStore."""
        self.store = list()                   # type: list
        self._sorted = True                   # type: bool
        self.desigcache = DesignationCache()  # type: DesignationCache

    def append(self, event: Event):
        """Append an Event to the store. The store will no longer be sorted."""
        if event.time == 0:
            raise ValueError("Events must have a timestamp.")
        self.store.append(event)
        self._sorted = False

    def insert(self, event: Event):
        """Insert an Event. Maintains the store sorted, if it was sorted."""
        if event.time == 0:
            raise ValueError("Events must have a timestamp.")

        # Binary search, first part: find a good index where to insert
        targetval = event.time
        targetb = 0
        minb = 0
        maxb = len(self.store)
        while maxb > minb + 1:
            currentb = minb + floor((maxb-minb)/2)
            currentval = self.store[currentb].time

            # Equal timestamps: put event after events with the same timestamp
            if currentval == targetval:
                targetb = currentb
                break
                pass
            # Event must go before the current value
            elif currentval > targetval:
                maxb = currentb
            # Event must go after the current value
            else:  # store[nextb] < target:
                minb = currentb
        else:
            # Reached if minb == maxb, aka event goes between minb and maxb
            targetb = minb

        # This serves two purposes: firstly, it inserts the new event after
        # those with a strictly equal timestamp. secondly, it 'finishes' the
        # previous loop by ensuring that we place our targetb cursor on the
        # nearest larger value. This is necessary because insert will insert
        # before the index it's given, rather than after, so we must target
        # the nearest larger value. We could have used ceiling instead of
        # floor in the previous loop to avoid that but we still needed to
        # iterate when timestamps are equal.
        maxb = len(self.store)
        while targetb < maxb and self.store[targetb].time <= targetval:
            targetb += 1
        self.store.insert(targetb, event)

    def sort(self):
        """Sort all the inserted Events by timestamp."""
        self.store = sorted(self.store, key=lambda x: x.time)
        self._sorted = True

    def getAllEvents(self):
        """Return all the events in the store. Very memory-intensive."""
        return self.store

    def getEventCount(self):
        """Return the number of events currently in the store."""
        return len(self.store)

    def simulateAccess(self,
                       event: Event,
                       fileFactory: FileFactory,
                       fileStore: FileStore):
        """Simulate a file access Event."""
        files = []

        # Get each File
        for subj in event.getData():
            file = fileFactory.getFile(name=subj.path,
                                       time=event.time,
                                       ftype=subj.ftype)
            files.append(file)

        # Check acts of designation
        res = self.desigcache.checkForDesignation(event, files)
        del files

        # Then, for each File, log the access
        for (file, flags) in res:
            file.addAccess(actor=event.actor,
                           flags=flags,
                           time=event.time)
            fileStore.updateFile(file)

    def simulateDestroy(self,
                        event: Event,
                        fileFactory: FileFactory,
                        fileStore: FileStore):
        """Simulate a file deletion Event."""
        filesDeleted = []
        files = []

        # Get each File
        for subj in event.getData():
            file = fileFactory.getFile(name=subj.path,
                                       time=event.time,
                                       ftype=subj.ftype)
            files.append(file)

        # Check acts of designation
        res = self.desigcache.checkForDesignation(event, files)
        del files

        # Then, for each File, set its end time, and update the store
        for (file, flags) in res:
            fileFactory.deleteFile(file,
                                   event.actor,
                                   event.time,
                                   flags)
            filesDeleted.append(file)

        return filesDeleted

    def __doCreateFile(self,
                       name: str,
                       ftype: str,
                       event: Event,
                       fileFactory: FileFactory,
                       fileStore: FileStore):
        """Create a File, and return it."""

        file = fileFactory.getFile(name=name,
                                   time=event.time,
                                   ftype=ftype)
        file.setTimeOfStart(event.time)

        res = self.desigcache.checkForDesignation(event, [file])
        file.addAccess(actor=event.actor,
                       time=event.time,
                       flags=res[0][1])
        fileStore.updateFile(file)

        return file

    def simulateCreate(self,
                       event: Event,
                       fileFactory: FileFactory,
                       fileStore: FileStore):
        """Simulate a file creation Event."""
        filesCreated = []

        # Get each File
        for subj in event.getData():

            # Remove old File if it exists and we overwrite.
            if event.evflags & EventFileFlags.overwrite:
                oldFile = fileFactory.getFileIfExists(subj.path, event.time)
                if oldFile:
                    deleting = True

                    # Check if the File was created by us, recently.
                    # TODO

                    # Check if we accessed the File, recently (some overwrites
                    # can be false positives). This means this Event is
                    # actually a simulated write to File, so we must correct
                    # that.
                    # TODO

                    deleting = False  # FIXME temporary till above code written
                    # If we decide to delete the File, this is executed.
                    if deleting:
                        baseFlags = event.evflags
                        event.evflags = (event.evflags |
                                         EventFileFlags.write |
                                         EventFileFlags.destroy &
                                         ~EventFileFlags.create &
                                         ~EventFileFlags.overwrite &
                                         ~EventFileFlags.read &
                                         ~EventFileFlags.copy &
                                         ~EventFileFlags.move)
                        res = self.desigcache.checkForDesignation(event, [f])
                        fileFactory.deleteFile(oldFile,
                                               event.actor,
                                               event.time,
                                               res[0][1])
                        event.evflags = baseFlags

            f = self.__doCreateFile(subj.path,
                                    subj.ftype,
                                    event,
                                    fileFactory,
                                    fileStore)
            filesCreated.append(f)

        return filesCreated

    def simulateCopy(self,
                     event: Event,
                     fileFactory: FileFactory,
                     fileStore: FileStore,
                     keepOld: bool=True):
        """Simulate a file copy or move Event, based on :keepOld:."""
        newFiles = []
        ctype = 'copy' if keepOld else 'move'
        baseFlags = event.evflags

        def _delFile(event: Event, f, read: bool=False):
            event.evflags = (baseFlags |
                             EventFileFlags.write |
                             EventFileFlags.destroy)
            if read:
                event.evflags |= EventFileFlags.read
            res = self.desigcache.checkForDesignation(event, [f])
            fileFactory.deleteFile(f, event.actor, event.time, res[0][1])
            event.evflags = baseFlags

        def _addRead(event: Event, f):
            event.evflags = (baseFlags | EventFileFlags.read)
            res = self.desigcache.checkForDesignation(event, [f])
            f.addAccess(actor=event.actor,
                        flags=res[0][1],
                        time=event.time)
            event.evflags = baseFlags

        def _createCopy(event: Event, oldFile, newPath):
            # Create a file on the new path which is identical to the old File.
            event.evflags = (baseFlags |
                             EventFileFlags.write |
                             EventFileFlags.create |
                             EventFileFlags.overwrite)
            newFile = self.__doCreateFile(newPath,
                                          oldFile.ftype,
                                          event,
                                          fileFactory,
                                          fileStore)
            event.evflags = baseFlags

            # Update the files' links
            oldFile.addFollower(newFile.inode, event.time, ctype)
            newFile.setPredecessor(oldFile.inode, event.time, ctype)
            fileStore.updateFile(oldFile)
            fileStore.updateFile(newFile)

            return newFile

        # Get each file, set its starting time and type, and update the store
        subjects = list((old.path, new.path) for (old, new) in event.getData())
        for (old, new) in subjects:

            # Not legal. 'a' and 'a' are the same file.
            if old == new:
                # TODO DBG?
                continue

            # Get the old file. It must exist, or the simulation is invalid.
            oldFile = fileFactory.getFile(old, event.time)
            if not oldFile:
                raise ValueError("Attempting to move/copy from a file that "
                                 "does not exist: %s at time %d" % (
                                  old,
                                  event.time))

            if debugEnabled():
                print("Info: %s '%s' to '%s' at time %s, by actor %s." % (
                      ctype,
                      old,
                      new,
                      time2Str(event.time),
                      event.actor.uid()))

            # Check if the target is a directory, or a regular file. If it does
            # not exist, it's a regular file.
            newFile = fileFactory.getFileIfExists(new, event.time)
            newIsFolder = newFile and newFile.isFolder()

            # If the target is a directory, we will copy/move inside it.
            sourceIsFolder = oldFile.isFolder()
            targetPath = new if not newIsFolder else \
                new + "/" + oldFile.getFileName()
            # If mv/cp'ing a folder to an existing path, restrictions apply.
            if sourceIsFolder:
                # oldFile is a/, newFile is b/, targetFile is b/a/
                targetFile = fileFactory.getFileIfExists(targetPath,
                                                         event.time)
                targetIsFolder = targetFile and targetFile.isFolder()

                # cannot overwrite non-directory 'b' with directory 'a'
                # cannot overwrite non-directory 'b/a' with directory 'a'
                if targetFile and not targetIsFolder:
                    # TODO DBG?
                    continue

                # mv: cannot move 'a' to 'b/a': Directory not empty
                elif targetIsFolder and ctype == "move":
                    children = fileStore.getChildren(targetFile, event.time)
                    if len(children) == 0:
                        _delFile(event, targetFile)
                    else:
                        # TODO DBG?
                        continue

                # mv or cp would make the target directory here. Our code later
                # on in this function will create a copy of the old file, which
                # means the new folder will be made, with a creation access
                # from the actor that performs the copy event we are analysing.
                elif not targetFile and ctype == "copy":
                    pass

            # When the source is a file, just delete the new target path.
            else:
                targetFile = fileFactory.getFileIfExists(targetPath,
                                                         event.time)
                if targetFile:
                    _delFile(event, targetFile)

            # Collect the children of the source folder.
            children = fileStore.getChildren(oldFile, event.time) if \
                sourceIsFolder else []

            # Make the target file, and link the old and target files.
            _createCopy(event, oldFile, targetPath)
            if ctype == "move":
                _delFile(event, oldFile, read=True)
            else:
                _addRead(event, oldFile)

            # Move or copy the children.
            for child in children:
                childRelPath = child.path[len(oldFile.path)+1:]
                childTargetPath = targetPath + "/" + childRelPath
                childNewFile = None

                # Let the Python purists hang me for that. Iterators will catch
                # appended elements on a mutable list and this is easier to
                # read than other solutions that don't modify the list while it
                # is iterated over.
                subjects.append((child.path, childTargetPath))

            newFiles.append(targetFile)

        return newFiles

    def simulateAllEvents(self):
        """Simulate all events to instantiate Files in the FileStore."""
        if not self._sorted:
            self.sort()

        fileStore = FileStore.get()
        fileFactory = FileFactory.get()

        # First, parse for Zeitgeist designation events in order to instantiate
        # the designation cache.
        if debugEnabled():
            print("Instantiating Zeitgeist acts of designation...")
        for event in self.store:
            if event.isInvalid():
                continue

            if event.getSource() == EventSource.zeitgeist:
                # The event grants 5 minutes of designation both ways.
                self.desigcache.addItem(event,
                                        start=event.time - 5*60*1000,
                                        duration=10*60*1000)
            # The current Event is an act of designation for future Events
            # related to the same Application and Files. Save it.
            elif event.getFileFlags() & EventFileFlags.designationcache:
                self.desigcache.addItem(event)

        if debugEnabled():
            print("Done. Starting simulation...")
        # Then, dispatch each event to the appropriate handler
        for event in self.store:
            if event.isInvalid():
                continue

            # Designation events are already processed.
            if event.getFileFlags() & EventFileFlags.designationcache:
                continue

            if debugEnabled():
                print("Simulating Event %s from %s at time %s." % (
                    event.evflags, event.actor.uid(), time2Str(event.time)))

            for data in event.data_app:
                if data[2] == FD_OPEN:
                    event.actor.openFD(data[0], data[1], event.time)
                elif data[2] == FD_CLOSE:
                    event.actor.closeFD(data[0], event.time)

            if event.getFileFlags() & EventFileFlags.destroy:
                res = self.simulateDestroy(event, fileFactory, fileStore)

            elif event.getFileFlags() & EventFileFlags.create:
                res = self.simulateCreate(event, fileFactory, fileStore)

            elif event.getFileFlags() & EventFileFlags.overwrite:
                res = self.simulateCreate(event, fileFactory, fileStore)

                # We received a list of files that were created
                if isinstance(res, list):
                    pass
                # We received instructions to hot-patch the event list
                else:
                    raise NotImplementedError  # TODO

            elif event.getFileFlags() & (EventFileFlags.read |
                                         EventFileFlags.write):
                self.simulateAccess(event, fileFactory, fileStore)

            # Keep me last, or use elif guards: I WILL change your event flags!
            elif event.getFileFlags() & EventFileFlags.move or \
                    event.getFileFlags() & EventFileFlags.copy:
                res = self.simulateCopy(event,
                                        fileFactory,
                                        fileStore,
                                        keepOld=event.getFileFlags() &
                                        EventFileFlags.copy)

        # Filter out invalid file descriptor references before computing stats.
        fileStore.purgeFDReferences()


import requests as req
from multiprocessing import Pool
from lxml import  etree
import re

def geturl():
    url='http://www.sharenice.net/t-6Z+z5LmQ'
    headers={}
    page_text=req.get(url=url,headers=headers).text
    tree=etree.HTML(page_text)
    li_list=tree.xpath('//div[@class="cate-block first-block"]//ul/li')
    #print(li_list)
    video_urls=[]
    for li in li_list:
        try:
            #print(li)
            detial_url=''+li.xpath('./span[@class="cover"]/a/@href')[0]
            #print(detial_url)
            detial_title=li.xpath('./span[@class="cover"]/a/@title')[0]+'.mp4'
            #print(detial_title,detial_url)
            #print(detial_url)
            detial_page_text=req.get(url=detial_url,headers=headers).text
            #print(detial_page_text)
            '''src = "https://video.pearvideo.com/mp4/adshort/20210125/cont-1717851-15586820_adpkg-ad_hd.mp4"'''
            regex='src="(https.*?\.mp4)'
            video_url=re.findall(regex,detial_page_text)[0]
            if video_url=='':
                continue
            #print(video_url)
            dict1= {'name':detial_title,'url':video_url}
            video_urls.append(dict1)
        except BaseException as BE:
            continue
    #print(video_urls)
    return video_urls

def save_video(reponse,name):
    with open(name,'wb') as fw:
        fw.write(reponse)
    fw.close()
    print('%s /保存成功！！' %name)

def getvideo(video_urls):

    #name=li['name']
    url1=video_urls['url']
    #print(url)
    context=req.get(url=url1).content
    print('正在保存：%s' %video_urls['name'])
    #save_video(context,name)
    with open(video_urls['name'], 'wb') as fw:
        fw.write(context)
    fw.close()
    print('%s /保存成功！！' % video_urls['name'])

if __name__ == '__main__':
    videourls=geturl()
    #print(videourls)
    #开启8线程
    pool=Pool(8)
    pool.map(getvideo,videourls)  #传入一个方法和一个可迭代的对象
    pool.close()
    pool.join()

爬虫_多线程_分享短视频.py

爬虫_多线程_分享短视频.py


from collections import deque

def solution(n, wires):
    #연결된 송전탑 수 계산을 위한 bfs
    def bfs(start):
        q = deque()
        q.append(start)
        visited[start] = True
        count = 1
        while q:
            next_node = q.popleft()
            for g in graph[next_node]:
                if visited[g] == False:
                    visited[g] = True
                    q.append(g)
                    count += 1
        return count

    answer = 101
    for i in range(len(wires)):
        graph = [[] for _ in range(n+1)]
        for j in range(len(wires)):
            if i == j: #선택한 부분 연결 끊기
                continue
            graph[wires[j][0]].append(wires[j][1])
            graph[wires[j][1]].append(wires[j][0])

        visited = [False] * (n+1)
        count_network = []
        for j in range(1,n+1):
            if visited[j] == False:
                count_network.append(bfs(j))
        #송전탑 개수 차이 최소값
        answer = min(answer,abs(count_network[0]-count_network[1]))
    return answer

프로그래머스/lv2/86971. 전력망을 둘로 나누기/전력망을 둘로 나누기.py

전력망을 둘로 나누기.py


import ast
import os
import sys

from ovn_k8s.common.util import ovs_vsctl
from ovn_k8s.common.util import ovn_nbctl
from ovn_k8s.common import variables

if sys.version_info < (3, 0):
    # python 2
    import ConfigParser
else:
    # python 3
    import configparser as ConfigParser

raw_config_parser = ConfigParser.RawConfigParser()

def init_config():
    config_filename = 'ovn_k8s.conf'
    if sys.platform != 'win32':
        config_path = "/etc/openvswitch"
    else:
        config_path = "C:\\etc"
    config_file = '%s/%s' % (config_path, config_filename)
    # Convert the path to use platform specific path delimitators
    config_file = os.path.abspath(config_file)
    # Return True if the file could be read, otherwise False
    return len(raw_config_parser.read(config_file)) == 1

config_successfully_read = init_config()

def get_default_value(option_name, section_name):
    section_default = {
        "mtu": 1400,
        "conntrack_zone": 64000,
        "ovn_mode": "overlay",
        "log_path": "/var/log/openvswitch/ovn-k8s-cni-overlay.log",
        "unix_socket": "/var/run/openvswitch/ovnnb_db.sock",
        "cni_conf_path": "/etc/cni/net.d",
        "cni_link_path": "/opt/cni/bin/",
        "cni_plugin": "ovn-k8s-cni-overlay",
        "private_key": "/etc/openvswitch/ovncontroller-privkey.pem",
        "certificate": "/etc/openvswitch/ovncontroller-cert.pem",
        "ca_cert": "/etc/openvswitch/ovnnb-ca.cert",
        "k8s_ca_certificate": "/etc/openvswitch/k8s-ca.crt",
        "rundir": "",
        "logdir": ""
    }
    default_config = {'default': section_default}

    section_dict = default_config.get(section_name, {})
    return section_dict.get(option_name, None)

def get_option(option_name, section_name='default'):
    if not config_successfully_read:
        return get_default_value(option_name, section_name)

    try:
        config_string = raw_config_parser.get(section_name, option_name)
    except Exception:
        # Config value could not be found in the file, retrieve the
        # default value
        config_string = get_default_value(option_name, section_name)

    try:
        # Try to evaluate the string which may contain a Python expression
        expr = ast.literal_eval(config_string)
        return expr
    except Exception:
        return config_string

UNIX_SOCKET = get_option('unix_socket')

def ovn_init_overlay():
    if os.path.exists(UNIX_SOCKET):
        OVN_NB = "unix:%s" % UNIX_SOCKET
    else:
        sys.exit("OVN NB database does not have unix socket")
    variables.OVN_NB = OVN_NB

    K8S_API_SERVER = ovs_vsctl("--if-exists", "get", "Open_vSwitch", ".",
                               "external_ids:k8s-api-server").strip('"')
    if not K8S_API_SERVER:
        sys.exit("K8S_API_SERVER not set")
    if not K8S_API_SERVER.startswith("http"):
        variables.K8S_API_SERVER = "http://%s" % K8S_API_SERVER
    else:
        variables.K8S_API_SERVER = K8S_API_SERVER

    K8S_CLUSTER_ROUTER = ovn_nbctl("--data=bare", "--no-heading",
                                   "--columns=_uuid", "find", "logical_router",
                                   "external_ids:k8s-cluster-router=yes")
    if not K8S_CLUSTER_ROUTER:
        sys.exit("K8S_CLUSTER_ROUTER not set")
    variables.K8S_CLUSTER_ROUTER = K8S_CLUSTER_ROUTER

    K8S_CLUSTER_LB_TCP = ovn_nbctl("--data=bare", "--no-heading",
                                   "--columns=_uuid", "find", "load_balancer",
                                   "external_ids:k8s-cluster-lb-tcp=yes")
    if not K8S_CLUSTER_LB_TCP:
        sys.exit("K8S_CLUSTER_LB_TCP not set")
    variables.K8S_CLUSTER_LB_TCP = K8S_CLUSTER_LB_TCP

    K8S_CLUSTER_LB_UDP = ovn_nbctl("--data=bare", "--no-heading",
                                   "--columns=_uuid", "find", "load_balancer",
                                   "external_ids:k8s-cluster-lb-udp=yes")
    if not K8S_CLUSTER_LB_UDP:
        sys.exit("K8S_CLUSTER_LB_UDP not set")
    variables.K8S_CLUSTER_LB_UDP = K8S_CLUSTER_LB_UDP


import torch
import torch.nn as nn
import numpy as np
import os
import argparse
from unidecode import unidecode

from fastspeech2.model_fs2 import FastSpeech2
from text_fs2 import text_to_sequence, sequence_to_text
import hparams as hp
import utils
from string import punctuation
from G2p import G2p
from time import time
import re
from evaluating_sentence import EvalMeanFrequencyScore

ev = EvalMeanFrequencyScore()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)

def preprocess(text, g2p):
    text = text.rstrip(punctuation).lower()
    print(text)
    phone = g2p.g2p(text)
    # phone = list(filter(lambda p: p != ' ', phone))
    # print(phone)

    phone = '{' + '}{'.join(phone.split()) + '}'
    phone = re.sub(r'\{[^\w\s]?\}', '{sp}', phone)
    phone = phone.replace('}{', ' ')
    # print('|' + phone + '|')
    sequence = np.array(text_to_sequence(phone, hp.text_cleaners))
    # print(sequence_to_text(sequence))
    # print(sequence)
    sequence = np.stack([sequence])
    return torch.from_numpy(sequence).long().to(device)

def get_FastSpeech2():
    checkpoint_path = os.path.join(
        hp.restore_path, "checkpoint_{}.pth.tar".format('120000'))
    model = nn.DataParallel(FastSpeech2())
    model.load_state_dict(torch.load(checkpoint_path, map_location=device)['model'])
    model.requires_grad = False
    model.eval()
    return model

def synthesize(model, waveglow, text, idx, prefix='', duration_control=1.0, pitch_control=1.0, energy_control=1.0):
    t = time()
    src_len = torch.from_numpy(np.array([text.shape[1]])).to(device)
    mel, mel_postnet, log_duration_output, f0_output, energy_output, _, _, mel_len = model(
        text, src_len, d_control=duration_control, p_control=pitch_control, e_control=energy_control)

    if device == 'cuda':
        mel_postnet = mel_postnet.transpose(1, 2).detach()
    else:
        mel_postnet = mel_postnet.cpu().transpose(1, 2).detach()

    if not os.path.exists(args.test_path):
        os.makedirs(args.test_path)
    # Audio.tools.inv_mel_spec(mel_postnet, os.path.join(
    #     hp.test_path, '{}_griffin_lim_{}.wav'.format(prefix, name)))
    t1 = time() - t
    if waveglow is not None:
        utils.waveglow_infer(mel_postnet, waveglow, os.path.join(
            args.test_path, '{}_{}_{}.wav'.format(prefix, hp.vocoder, idx)))
    t2 = time() - t
    print('{}: time FS: {} (s) time {}: {}'.format(idx, t1, hp.vocoder, t2 - t1))

def gen_mel(model, text, idx, duration_control=1.0, pitch_control=1.0, energy_control=1.0):
    src_len = torch.from_numpy(np.array([text.shape[1]])).to(device)
    mel, mel_postnet, log_duration_output, f0_output, energy_output, _, _, mel_len = model(
        text, src_len, d_control=duration_control, p_control=pitch_control, e_control=energy_control)
    mel_postnet = mel_postnet[0].cpu().transpose(0, 1).detach()
    mel = mel[0].cpu().transpose(0, 1).detach()
    mel_filename = '{}-mel.npy'.format(idx)
    np.save(os.path.join(args.test_path, mel_filename), mel, allow_pickle=False)
    mel_filename = '{}-mel-post.npy'.format(idx)
    np.save(os.path.join(args.test_path, mel_filename), mel_postnet, allow_pickle=False)

if __name__ == "__main__":
    # Test
    parser = argparse.ArgumentParser()
    parser.add_argument('--test_path', type=str, default='./test/')
    parser.add_argument('--path', type=str, default='test.txt')
    parser.add_argument('--dict_path', type=str, default='syllable_g2p.txt')
    parser.add_argument('--sent', type=str, default='?')
    parser.add_argument('--duration_control', type=float, default=1.0)
    parser.add_argument('--pitch_control', type=float, default=1.0)
    parser.add_argument('--energy_control', type=float, default=1.0)

    args = parser.parse_args()

    g2p = G2p(args.dict_path)

    sentences = []
    if args.sent != '?':
        sentences.append(args.sent)
    else:
        with open(args.path, 'r', encoding='utf-8') as rf:
            lines = rf.read().split('\n')
            for i, line in enumerate(lines):
                sentences.append(line)

    model = get_FastSpeech2().to(device)
    waveglow = utils.get_waveglow()
    # waveglow = None
    with torch.no_grad():
        for idx, sentence in enumerate(sentences):
            score = ev.cal(sentence)
        text = preprocess(sentence, g2p)
        name = str(idx) + '_' + str(score)
        print(text.shape)
        synthesize(model, waveglow, text, name, 'step_{}'.format(
            '120000'), args.duration_control, args.pitch_control, args.energy_control)
        # gen_mel(model, text_fs2, name, args.duration_control, args.pitch_control, args.energy_control)
        print('DONE', idx)

# print(text_to_sequence('{o2_T1 l e2_T1 sp k o1_T3 t ie2_T3 ng ng uoi3_T2 sp n oi_T3}', ['basic_cleaners']))


import os
import subprocess
from distutils import dir_util

from testtools.matchers import Equals

from snapcraft.tests import integration
from snapcraft.tests import fixture_setup

class GoTestCase(integration.SnapdIntegrationTestCase):

    def test_install_and_execution(self):
        with fixture_setup.WithoutSnapInstalled('go-hello'):
            # If we just put the source in the temp dir, go will generate
            # the binary with the name of the temp dir, instead of go-hello.
            dir_util.copy_tree(
                os.path.join(self.snaps_dir, 'go-hello'),
                os.path.join(self.path, 'go-hello'),
                preserve_symlinks=True)
            os.chdir('go-hello')

            self.run_snapcraft()
            self.install_snap()
            self.assertThat(
                subprocess.check_output(
                    ['go-hello'], universal_newlines=True),
                Equals('Hello snapcrafter\n'))


from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path

from . import views

app_name = 'node'
urlpatterns = [
    # path('node_list/<int:file_Code>', views.CreateTree, name="node_list"),
    path('node_list/<str:proj_Code>', views.node_list, name="node_list"),
    path('node_detail/<str:node_Code>', views.node_detail, name="node_detail"),
    path('create_node/',views.create_node, name='create_node'),
    path('changeNodeInfo/', views.changeNodeInfo, name="changeNodeInfo"),
    path('download/<str:pk>',views.download_view,name='download'),
    path('node_comment_create/<node_Code>',
         views.node_comment_create, name="node_comment_create"),
    path('comment_mention/',views.mentionable_member_json, name='mentionable_member_json'),
    path('submit/', views.comment_submit, name="comment_submit"),
    path('load_comment/', views.load_comment, name="load_comment"),
    path('load_node_data/', views.load_node_data, name="load_node_data"),

]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)


from pxr import Gf
import carb
import omni.usd
import omni.ext
import omni.ui as ui
from omni.kit.menu.utils import add_menu_items, remove_menu_items, MenuItemDescription

import asyncio
import gc
import weakref

from omni.isaac.dynamic_control import _dynamic_control
from .utils.simple_robot_controller import RobotController
from omni.isaac.utils.scripts.scene_utils import set_up_z_axis, setup_physics, create_background
from omni.isaac.utils.scripts.nucleus_utils import find_nucleus_server

EXTENSION_NAME = "Robot Navigation"

def create_xyz(init={"X": 100, "Y": 100, "Z": 0}):
    all_axis = ["X", "Y", "Z"]
    colors = {"X": 0xFF5555AA, "Y": 0xFF76A371, "Z": 0xFFA07D4F}
    float_drags = {}
    for axis in all_axis:
        with ui.HStack():
            with ui.ZStack(width=15):
                ui.Rectangle(
                    width=15,
                    height=20,
                    style={"background_color": colors[axis], "border_radius": 3, "corner_flag": ui.CornerFlag.LEFT},
                )
                ui.Label(axis, name="transform_label", alignment=ui.Alignment.CENTER)
            float_drags[axis] = ui.FloatDrag(name="transform", min=-1000000, max=1000000, step=1, width=100)
            float_drags[axis].model.set_value(init[axis])
    return float_drags

class Extension(omni.ext.IExt):
    def on_startup(self):
        self._timeline = omni.timeline.get_timeline_interface()
        self._viewport = omni.kit.viewport.get_default_viewport_window()
        self._usd_context = omni.usd.get_context()
        self._stage = self._usd_context.get_stage()
        self._window = ui.Window(EXTENSION_NAME, width=500, height=175, visible=False)
        self._window.set_visibility_changed_fn(self._on_window)

        menu_items = [
            MenuItemDescription(name=EXTENSION_NAME, onclick_fn=lambda a=weakref.proxy(self): a._menu_callback())
        ]
        self._menu_items = [
            MenuItemDescription(
                name="Controlling", sub_menu=[MenuItemDescription(name="Navigation", sub_menu=menu_items)]
            )
        ]
        add_menu_items(self._menu_items, "Isaac Examples")
        self._dc = _dynamic_control.acquire_dynamic_control_interface()
        self._create_ui()

        self._setup_done = False
        self._rc = None

    def _menu_callback(self):
        self._window.visible = not self._window.visible

    def _on_window(self, visible):
        if self._window.visible:
            self._sub_stage_event = self._usd_context.get_stage_event_stream().create_subscription_to_pop(
                self._on_stage_event
            )
        else:
            self._sub_stage_event = None

    def _create_ui(self):
        with self._window.frame:
            with omni.ui.VStack():
                with ui.HStack(height=5):
                    ui.Spacer(width=7)
                    self._robot_option = ui.ComboBox(0, "Transporter", "Carter", width=125)
                with ui.HStack(height=5):
                    ui.Spacer(width=5)
                    self._load_btn = ui.Button("Load Environment", width=125)
                    self._load_btn.set_clicked_fn(self._on_environment_setup)
                with ui.HStack(height=5):
                    ui.Spacer(width=9)
                    self._motion_label = ui.Label("Primitive Tasks", width=100)
                    self._motion_label.set_tooltip(
                        "Perform simple tasks like moving robot forward or rotating in-place"
                    )
                    self._move_btn = ui.Button("Move Robot", width=100, enabled=False)
                    self._move_btn.set_clicked_fn(self._on_move_fn)
                    self._rotate_btn = ui.Button("Rotate Robot", width=100, enabled=False)
                    self._rotate_btn.set_clicked_fn(self._on_rotate_fn)
                with ui.HStack(height=5):
                    ui.Spacer(width=9)
                    self._goal_label = ui.Label("Set Robot Goal", width=100)
                    self._goal_label.set_tooltip("Set robot target specified as (X, Y, theta)")
                    self.goal_coord = create_xyz(init={"X": -200, "Y": -400, "Z": 0})
                with ui.HStack(height=5):
                    ui.Spacer(width=5)
                    self._navigate_btn = ui.Button("Navigate Robot", width=100, enabled=False)
                    self._navigate_btn.set_clicked_fn(self._on_navigate_fn)
                    self._stop_btn = ui.Button("Stop Robot", width=100, enabled=False)
                    self._stop_btn.set_clicked_fn(self._on_navigate_stop_fn)

    async def _create_robot(self, task):
        done, pending = await asyncio.wait({task})
        if task in done:
            print("Loading Robot Enviornment")
            self._viewport.set_camera_position("/OmniverseKit_Persp", 300, 300, 100, True)
            self._viewport.set_camera_target("/OmniverseKit_Persp", 0, 0, 0, True)
            self._stage = self._usd_context.get_stage()
            result, nucleus_server = find_nucleus_server()
            if result is False:
                carb.log_error("Could not find nucleus server with /Isaac folder")
                return
            self._asset_path = nucleus_server + "/Isaac"

            current_robot_index = self._robot_option.model.get_item_value_model().as_int
            self._robot_prim_path = "/robot"
            if current_robot_index == 0:
                asset_path = self._asset_path + "/Robots/Transporter"
                robot_usd = asset_path + "/transporter.usd"
                self._robot_chassis = self._robot_prim_path + "/chassis"
                self._robot_wheels = ["left_wheel_joint", "right_wheel_joint"]
                self._robot_wheels_speed = [3, 3]
            elif current_robot_index == 1:
                asset_path = self._asset_path + "/Robots/Carter"
                robot_usd = asset_path + "/carter_sphere_wheels_lidar.usd"
                self._robot_chassis = self._robot_prim_path + "/chassis_link"
                self._robot_wheels = ["left_wheel", "right_wheel"]
                self._robot_wheels_speed = [2, 2]

            set_up_z_axis(self._stage)
            setup_physics(self._stage)

            create_background(
                self._stage,
                self._asset_path + "/Environments/Grid/gridroom_curved.usd",
                background_path="/background",
                offset=Gf.Vec3d(0, 0, -9),
            )

            # setup high-level robot prim
            self.prim = self._stage.DefinePrim(self._robot_prim_path, "Xform")
            self.prim.GetReferences().AddReference(robot_usd)

    def _on_stage_event(self, event):
        self.stage = self._usd_context.get_stage()
        if event.type == int(omni.usd.StageEventType.OPENED):
            self._move_btn.enabled = self._setup_done
            self._rotate_btn.enabled = self._setup_done
            self._navigate_btn.enabled = self._setup_done
            self._stop_btn.enabled = self._setup_done
            if self._rc:
                self._rc.enable_navigation(False)
            self._setup_done = False

    async def _play(self, task):
        done, pending = await asyncio.wait({task})
        if task in done:
            self._timeline.play()
            await asyncio.sleep(1)

    async def _on_setup_fn(self, task):
        done, pending = await asyncio.wait({task})
        if task in done:
            self._stage = self._usd_context.get_stage()
            # setup robot controller
            self._rc = RobotController(
                self._stage,
                self._timeline,
                self._dc,
                self._robot_prim_path,
                self._robot_chassis,
                self._robot_wheels,
                self._robot_wheels_speed,
                [1, 0.05],
            )
            self._rc.control_setup()
            # start stepping
            self._editor_event_subscription = (
                omni.kit.app.get_app().get_update_event_stream().create_subscription_to_pop(self._rc.update)
            )

    def _on_environment_setup(self):
        # wait for new stage before creating robot
        task = asyncio.ensure_future(omni.usd.get_context().new_stage_async())
        task1 = asyncio.ensure_future(self._create_robot(task))
        # set editor to play before setting up robot controller
        task2 = asyncio.ensure_future(self._play(task1))
        asyncio.ensure_future(self._on_setup_fn(task2))

        # self._load_btn.enabled=False
        self._move_btn.enabled = True
        self._rotate_btn.enabled = True
        self._navigate_btn.enabled = True
        self._stop_btn.enabled = True
        self._setup_done = True

    def _on_move_fn(self):
        print("Moving forward")
        self._rc.control_command(3, 3)

    def _on_rotate_fn(self):
        print("Rotating in-place")
        self._rc.control_command(3, -3)

    def _on_navigate_fn(self):
        goal_x = self.goal_coord["X"].model.get_value_as_float()
        goal_y = self.goal_coord["Y"].model.get_value_as_float()
        goal_z = self.goal_coord["Z"].model.get_value_as_float()
        print("Navigating to goal ({}, {}, {})".format(goal_x, goal_y, goal_z))
        self._rc.set_goal(goal_x, goal_y, goal_z)
        self._rc.enable_navigation(True)

    def _on_navigate_stop_fn(self):
        print("Navigation Stopped")
        self._rc.enable_navigation(False)
        self._rc.control_command(0, 0)

    def on_shutdown(self):
        self._rc = None
        self._timeline.stop()
        self._editor_event_subscription = None
        remove_menu_items(self._menu_items, "Isaac Examples")
        self._window = None
        gc.collect()


"""Unit tests for djblets.privacy.templatetags.djblets_privacy."""

from django.template import Context, Template
from django.test.client import RequestFactory
from django.urls import ResolverMatch

from djblets.testing.testcases import TestCase

class PIISafePageURLTemplateTagTests(TestCase):
    """Unit tests for the {% pii_safe_page_url %} template tag."""

    def test_with_pii(self):
        """Testing {% pii_safe_page_url %} with PII in URL"""
        url = self._render_url('/users/test/', kwargs={
            'username': 'test',
        })

        self.assertEqual(url, '/users/<REDACTED>/')

    def test_without_pii(self):
        """Testing {% pii_safe_page_url %} without PII in URL"""
        self.assertEqual(self._render_url('/groups/test/'),
                         '/groups/test/')

    def _render_url(self, path, kwargs={}):
        request = RequestFactory().get(path)
        request.resolver_match = ResolverMatch(func=lambda: None,
                                               args=(),
                                               kwargs=kwargs)

        t = Template('{% load djblets_privacy %}'
                     '{% pii_safe_page_url %}')

        return t.render(Context({
            'request': request,
        }))


# 1
#%matplotlib notebook
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 2
# Orion
x = [-0.41, 0.57, 0.07, 0.00, -0.29, -0.32,-0.50,-0.23, -0.23]
y = [4.12, 7.71, 2.36, 9.10, 13.35, 8.13, 7.19, 13.25,13.43]
z = [2.06, 0.84, 1.56, 2.07, 2.36, 1.72, 0.66, 1.25,1.38]

# 3
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x,y, marker = '*')
ax.set_title('2D Constellation View')
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
plt.show()

# 4
fig_3d = plt.figure()
ax = fig_3d.add_subplot(1,1,1, projection='3d')
constellation3d = ax.scatter(x,y,z, marker='*')
ax.set_title('Constellation of Orion', size=('small'), weight=('bold'))
ax.set_xlabel('X_Axis', size=('x-small'), weight=('bold'))
ax.set_ylabel('Y_Axis', size=('x-small'), weight=('bold'))
ax.set_zlabel('Z_Axis', size=('x-small'), weight=('bold'))

plt.show()

# 5
x2 = [22.4, 33.7, 18.4, 24.3]
y2 = [58.3, 55.2, 48.4, 44.4]
z2 = [18.5, 19.3, 26.6, 20.4]

some_stars = plt.figure()
ax = some_stars.add_subplot(1,1,1, projection='3d')
new_constellation3D = ax.scatter(x2,y2,z2, marker='*')
ax.set_title('Aldebaran, Bonner Stars, 39 Tauri', size=('small'), weight=('bold'))
ax.set_xlabel('Celestial X Coordinate', size=('x-small'), fontstyle=('italic'))
ax.set_ylabel('Celestial Y Coordinate', size=('x-small'), fontstyle=('italic'))
ax.set_zlabel('Celestial Z Coordinate', size=('x-small'), fontstyle=('italic'))

plt.show()


import pytest

from pcapi.core.offers import factories as offers_factories
from pcapi.core.testing import assert_num_queries
from pcapi.core.users import factories as users_factories
from pcapi.models import ApiErrors
from pcapi.models import Venue
from pcapi.utils.human_ids import humanize
from pcapi.utils.rest import check_user_has_access_to_offerer
from pcapi.utils.rest import load_or_raise_error

pytestmark = pytest.mark.usefixtures("db_session")

class CheckUserHasAccessToOffererTest:
    def test_check_user_has_access_to_offerer(self, app):
        pro = users_factories.UserFactory()
        offerer = offers_factories.OffererFactory()
        offers_factories.UserOffererFactory(user=pro, offerer=offerer)
        # fmt: off
        n_queries = (
            1  # select Offerer
            + 1  # select User
            + 1  # select UserOfferer
        )
        # fmt: on
        with assert_num_queries(n_queries):
            check_user_has_access_to_offerer(pro, offerer.id)

    def test_raises_if_user_cannot_access_offerer(self, app):
        user = users_factories.UserFactory()
        offerer = offers_factories.OffererFactory()
        with pytest.raises(ApiErrors) as error:
            check_user_has_access_to_offerer(user, offerer.id)

        assert error.value.errors["global"] == [
            "Vous n'avez pas les droits d'accès suffisant pour accéder à cette information."
        ]
        assert error.value.status_code == 403

class TestLoadOrRaiseErrorTest:
    def test_returns_object_if_found(self, app):
        with pytest.raises(ApiErrors) as error:
            load_or_raise_error(Venue, humanize(1))

        assert error.value.errors["global"] == [
            "Aucun objet ne correspond à cet identifiant dans notre base de données"
        ]
        assert error.value.status_code == 404

    def test_raises_api_error_if_no_object(self, app):
        venue = offers_factories.VenueFactory()

        load_or_raise_error(Venue, humanize(venue.id))  # should not raise


from typing import Any
import pytest

from blockwork.config.scheduler import Scheduler, CyclicError

class StateRecord:
    def __init__(self):
        self.states = []
        self.keys = {"leaves","schedulable","blocked","unscheduled","scheduled","incomplete","complete"}

    def record(self, scheduler):
        self.states.append({key: getattr(scheduler, key) for key in self.keys})

    def __getattr__(self, __name: str) -> Any:
        if __name in self.keys:
            return tuple(state.get(__name) for state in self.states)
        return super().__getattribute__(__name)

class TestScheduler:

    def test_basic(self):
        'Tests a basic chain'
        dependency_map = {
            "y": {"x"},
            "z": {"y"}
        }
        scheduler = Scheduler(dependency_map)

        states = StateRecord()

        while scheduler.incomplete:
            states.record(scheduler)
            for item in scheduler.schedulable:
                scheduler.schedule(item)
                scheduler.finish(item)

        assert states.schedulable == ({"x"},{"y"},{"z"})
        assert states.blocked == ({"y","z"},{"z"}, set())
        assert states.incomplete == ({"x", "y", "z"}, {"y", "z"}, {"z"})
        assert states.complete == (set(), {"x"}, {"x", "y"})

    def test_cycle(self):
        'Tests that cycles are detected'
        dependency_map = {
            "y": {"x"},
            "z": {"y"},
            "x": {"z"}
        }
        scheduler = Scheduler(dependency_map)

        with pytest.raises(CyclicError):
            scheduler.schedulable

    def test_complex(self) -> None:
        'Tests a more complex tree is scheduled correctly'
        dependency_map = {
            "b": {"a"},
            "c": {"b"},
            "d": {"b"},
            "e": {"c"},
            "f": {"d", "e", "g"}
        }
        scheduler = Scheduler(dependency_map)

        states = StateRecord()
        while scheduler.incomplete:
            states.record(scheduler)
            for item in scheduler.schedulable:
                scheduler.schedule(item)
                scheduler.finish(item)

        assert states.schedulable == ({"a", "g"}, {"b"}, {"c", "d"}, {"e"}, {"f"})

        # Do it again, but this time with a specific target
        scheduler = Scheduler(dependency_map, targets=['e'])
        states = StateRecord()
        while scheduler.incomplete:
            states.record(scheduler)
            for item in scheduler.schedulable:
                scheduler.schedule(item)
                scheduler.finish(item)
        assert states.schedulable == ({"a"}, {"b"}, {"c"}, {"e"})


import torch

def per_face_normals(
    V : torch.Tensor,
    F : torch.Tensor):
    """Compute normals per face.

    Args:
        V (torch.FloatTensor): Vertices of shape [V, 3]
        F (torch.LongTensor): Faces of shape [F, 3]

    Returns:
        (torch.FloatTensor): Normals of shape [F, 3]
    """
    mesh = V[F]

    vec_a = mesh[:, 0] - mesh[:, 1]
    vec_b = mesh[:, 1] - mesh[:, 2]
    normals = torch.cross(vec_a, vec_b)
    return normals


# An example of using the compress function inside itertools
# If you do not understand you can check this link as well:
# https://www.novixys.com/blog/python-itertools-compress-dropwhile-takewhile-groupby/

import itertools
import random

names = ["Subhayan", "Shaayan", "Dimpu", "Poulomi", "Rakesh"]

choices = [random.choice([0, 1]) for _ in itertools.repeat(None, 10)]
print(choices)

for item in itertools.compress(names, choices):
    print(item)


from flask import request, current_app
from flask_restful import Resource
from Model import db, Category, CategorySchema
from flask_jwt_extended import (create_access_token, create_refresh_token,
                                jwt_required, jwt_refresh_token_required, get_jwt_identity)
from templates import admin_required

categories_schema = CategorySchema(many=True)
category_schema = CategorySchema()

class CategoryResource(Resource):
    @jwt_required
    def get(self):
        current_app.logger.info('Processing Account GET')
        categories = Category.query.all()
        categories = categories_schema.dump(categories).data
        return {'status': 'success', 'data': categories}, 200

    #make an api for specific user ID to restrict user access to their own entries
    @admin_required
    def post(self):
        current_app.logger.info('Processing Category POST')
        json_data = request.get_json(force=True)
        if not json_data:
            current_app.logger.error('No input data given to Category POST')
            return {'message': 'No input data provided', 'error': 'true'}, 400
        # Validate and deserialize input
        data, errors = category_schema.load(json_data)
        if errors:
            current_app.logger.error('Bad data given to Category POST')
            return errors, 422
        category = Category.query.filter_by(name=data['name']).first()
        if category:
            current_app.logger.error('Category already exists in Category POST')
            return {'message': 'Category already exists', 'error': 'true'}, 400
        category = Category(
            name=json_data['name']
            )

        db.session.add(category)
        db.session.commit()

        result = category_schema.dump(category).data

        current_app.logger.info('Successful Category POST')
        return { "status": 'success', 'data': result }, 201

    #make an api for specific user ID to restrict user access to their own entries
    @admin_required
    def put(self):
        current_app.logger.info('Processing Category PUT')
        json_data = request.get_json(force=True)
        if not json_data:
            current_app.logger.error('No input data given to Category PUT')
            return {'message': 'No input data provided', 'error': 'true'}, 400
        # Validate and deserialize input
        data, errors = category_schema.load(json_data)
        if errors:
            current_app.logger.error('Bad data given to Category PUT')
            return errors, 422
        category = Category.query.filter_by(id=data['id']).first()
        if not category:
            current_app.logger.error('Selection not found in Category PUT')
            return {'message': 'Category does not exist', 'error': 'true'}, 400
        category.name = data['name']
        db.session.commit()

        result = category_schema.dump(category).data

        current_app.logger.info('Successful Category PUT')
        return { "status": 'success', 'data': result }, 204

    #make an api for specific user ID to restrict user access to their own entries
    @admin_required
    def delete(self):
        current_app.logger.info('Processing Category DELETE')
        json_data = request.get_json(force=True)
        if not json_data:
            current_app.logger.error('No input data given to Category DELETE')
            return {'message': 'No input data provided', 'error': 'true'}, 400
        # Validate and deserialize input
        data, errors = category_schema.load(json_data)
        if errors:
            current_app.logger.error('Bad data given to Category DELETE')
            return errors, 422
        category = Category.query.filter_by(id=data['id']).delete()
        db.session.commit()

        result = category_schema.dump(category).data

        current_app.logger.info('Successful Category DELETE')
        return { "status": 'success', 'data': result}, 204


import os
from typing import Dict
from flask import Flask

def create_app(config_dict: Dict = {}) -> Flask:
    app = Flask("main")

    DATABASE_URI = os.environ.get('DATABASE_URI')
    BUCKET_NAME = os.environ.get('BUCKET_NAME')
    SQS_URL = os.environ.get('SQS_URL')
    SECRET = os.environ.get('SECRET')
    AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    app.config['PROPAGATE_EXCEPTIONS'] = True
    app.config['JWT_SECRET_KEY'] = SECRET
    app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI
    app.config['BUCKET_NAME'] = BUCKET_NAME
    app.config['SQS_URL'] = SQS_URL
    app.config['AWS_ACCESS_KEY_ID'] = AWS_ACCESS_KEY_ID
    app.config['AWS_SECRET_ACCESS_KEY'] = AWS_SECRET_ACCESS_KEY

    return app


import os
import allure
import pytest

@pytest.fixture(scope='session')
def login_fixture():
    print("===前置登录===")

@allure.step('步骤一')
def step_1():
    print("操作步骤----------1")

@allure.step('步骤二')
def step_2():
    print("操作步骤----------2")

@allure.step('步骤三')
def step_3():
    print("操作步骤----------3")

@allure.epic('epic 相当于总体描述')
@allure.feature('测试模块')
class TestAllureALL:
    @allure.testcase("https://www.cnblogs.com/poloyy/", '测试用例,点我一下')
    @allure.issue("https://www.cnblogs.com/poloyy/p/12219145.html", 'Bug 链接,点我一下')
    @allure.title("用例的标题")
    @allure.story("story one")
    @allure.severity("critical")
    def test_case_1(self,login_fixture):
        """
        学习-小菠萝测试笔记地址：https://www.cnblogs.com/poloyy/
        """
        print("测试用例1")
        step_1()
        step_2()

    @allure.severity("critical")
    @allure.story("story two")
    def test_case_2(self,login_fixture):
        print("测试用例2")
        step_1()
        step_3()

@allure.epic("另一个 epic")
@allure.feature("查找模块")
class TestAllureALL2:
    @allure.story("story three")
    def test_case_3(self, login_fixture):
        print("测试用例3")
        step_1()

    # @allure.story("story four")
    # def test_case_4(self,login_fixture):
    #     print("测试用例4")
    #     step_3()


import sys
import json
import argparse

def check_overlap(set1_path, set2_path, l1):
    """ Checks challenge set overlap. """

    # Read-in sets
    print('Reading-in adversarial samples table ...')
    with open(set1_path, 'r', encoding='utf8') as s1p:
        set1 = json.load(s1p)
    with open(set2_path, 'r', encoding='utf8') as s2p:
        set2 = json.load(s2p)

    overlap = 0

    for term in set1.keys():
        if term not in set2.keys():
            continue
        s1_sents = list()
        s2_sents = list()

        for seed_cluster in set1[term].keys():
            for adv_cluster in set1[term][seed_cluster].keys():
                for seed_sentence in set1[term][seed_cluster][adv_cluster].keys():
                    s1_sents.append(seed_sentence)

        for seed_cluster in set2[term].keys():
            for adv_cluster in set2[term][seed_cluster].keys():
                for seed_sentence in set2[term][seed_cluster][adv_cluster].keys():
                    s2_sents.append(seed_sentence)

        overlap += len(list(set(s1_sents) & set(s2_sents)))

    print('Overlap: {:d} sentences ({:.2f}%)'.format(overlap, (overlap / l1) * 100))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--corpus_path1', type=str, help='path to the first challenge set',
                        required=True)
    parser.add_argument('--corpus_path2', type=str, help='path to the second challenge set',
                        required=True)
    parser.add_argument('--corpus_size', type=int, help='challenge set size (has to be equal for both)',
                        required=True)
    args = parser.parse_args()

    check_overlap(sys.argv[1], sys.argv[2], int(sys.argv[3]))


from rest_framework import mixins, viewsets

from core.api.permissions import IsAuthenticatedOrPostOnly

from data.wbs_user.models import WbsUser

from api.users.filters import WbsUserFilter
from api.users.serializers import WbsUserSerializer, WbsUserUpdateSerializer, WbsUserProjectSerializer

class WbsUserModelViewSet(
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
    mixins.CreateModelMixin,
    mixins.UpdateModelMixin,
    viewsets.GenericViewSet,
):
    """
    ### Endpoints:

    ```[GET]		/api/users/(?(username))```

        lists all users

        PARAMS:
        - username: <String>
            filter by the users username. Note that usernames are unique.

    ```[POST]		/api/users/```

        creates a new user

        POST DATA:
        {
            /** new users username */
            username: <String>,
            /** new users password */
            password: <String>
        }

    ```[GET]		/api/users/<user_id>/```

        the user with the user id `<user_id>`.

    ```[PATCH]		/api/users/<user_id>/```

        change password of the user with the user id `<user_id>`.

        POST DATA:
        {
            /** the users new password */
            password: <String>
        }

    ```[GET]		/api/users/<user_id>/projects/```

        get all projects ot the user with the user id `<user_id>`.

    ```[POST]		/api/users/<user_id>/projects/```

        add the user to an already existing project

        POST DATA:
        {
            /** URL of the project you want the user to be added to */
            project: <URL>
        }
    """
    filter_class = WbsUserFilter
    permission_classes = (IsAuthenticatedOrPostOnly,)
    queryset = WbsUser.objects.all()

    def get_serializer_class(self):
        if self.action == 'update' or self.action == 'partial_update':
            return WbsUserUpdateSerializer
        else:
            return WbsUserSerializer

class WbsUserProjectsModelViewSet(
    mixins.ListModelMixin,
    mixins.CreateModelMixin,
    viewsets.GenericViewSet,
):
    """
    ### Endpoints:

    ```[GET]		/api/users/(?(username))```

        lists all users

        PARAMS:
        - username: <String>
            filter by the users username. Note that usernames are unique.

    ```[POST]		/api/users/```

        creates a new user

        POST DATA:
        {
            /** new users username */
            username: <String>,
            /** new users password */
            password: <String>
        }

    ```[GET]		/api/users/<user_id>/```

        the user with the user id `<user_id>`.

    ```[PATCH]		/api/users/<user_id>/```

        change password of the user with the user id `<user_id>`.

        POST DATA:
        {
            /** the users new password */
            password: <String>
        }

    ```[GET]		/api/users/<user_id>/projects/```

        get all projects ot the user with the user id `<user_id>`.

    ```[POST]		/api/users/<user_id>/projects/```

        add the user to an already existing project

        POST DATA:
        {
            /** URL of the project you want the user to be added to */
            project: <URL>
        }
    """
    serializer_class = WbsUserProjectSerializer

    def get_user_from_url(self):
        """
        retrieves the WbsUser from the url

        :return: the user
        :rtype: WbsUser
        """
        return WbsUser.objects.get(pk=self.kwargs.get('user_id'))

    def get_queryset(self):
        return self.get_user_from_url().projects.all()


from datetime import datetime
from typing import Optional, TYPE_CHECKING, Union

from propsd.enums import SourceEvents, SourceState
from propsd.sourcemanager import SourceManager
from propsd.util import dispatch

if TYPE_CHECKING:
    from apscheduler.job import Job

class Schedulable:
    updated: Optional[datetime] = None
    _state: Optional[SourceState] = None

    events: dict = {
        SourceEvents.CREATED: dispatch.Signal(providing_args=['source']),
        SourceEvents.INITIALIZED: dispatch.Signal(providing_args=['source']),
        SourceEvents.NO_UPDATE: dispatch.Signal(providing_args=['source']),
        SourceEvents.ERROR: dispatch.Signal(providing_args=['source', 'error']),
        SourceEvents.UPDATE: dispatch.Signal(providing_args=['source', 'data']),
        SourceEvents.SHUTDOWN: dispatch.Signal(providing_args=['source'])
    }

    def __init__(self):
        self._state = SourceState.CREATED
        self._send_event(SourceEvents.CREATED, source=self)

    def __repr__(self):
        return '{}.{}'.format(self.__class__.__name__, self._state)

    def _pre_get(self):
        if self._state == SourceState.CREATED:
            # First pass
            self._state = SourceState.INITIALIZING
        elif self._state == SourceState.WAITING:
            # Set running state
            self._state = SourceState.RUNNING

    def _post_get(self):
        self.updated = datetime.now()

        if self._state == SourceState.INITIALIZING:
            self._state = SourceState.WAITING
            self._send_event(SourceEvents.INITIALIZED, source=self)
        elif self._state == SourceState.RUNNING:
            # Reset to waiting state on success
            self._state = SourceState.WAITING

    @property
    def job(self) -> 'Job':
        return SourceManager.get_job(str(self))

    @property
    def available(self):
        return self._state in [SourceState.WAITING, SourceState.RUNNING]

    def shutdown(self):
        self._state = SourceState.SHUTDOWN
        self._send_event(SourceEvents.SHUTDOWN, source=self)

    def _send_event(self, event: SourceEvents, **kwargs: Optional[Union['Schedulable', dict]]) -> None:
        self.events[event].send_robust(sender=self.__class__, **kwargs)

def get_event(event: SourceEvents) -> dispatch.Signal:
    return Schedulable.events[event]


#!/usr/bin/python
import sys
from suds.client import Client
from suds import WebFault
import test
import psycopg2
import sys
from datetime import datetime
from datetime import timedelta
import traceback

# List of environment variables, client side
# ICS_BALTIC_DB_HOST = 'localhost'
# ICS_BALTIC_DB_DBNAME = 'ivs'
# ICS_BALTIC_DB_DBUSER = 'ivs'
# ICS_BALTIC_DB_PASSWD = ***

# ICS_BALTIC_SERVER_IP = ***
# ICS_BALTIC_FTP_IPADDR = ***
# ICS_BALTIC_FTP_LOGIN = 'ftpuser'
# ICS_BALTIC_FTP_PASSWD = ***
import os

def connect_db():
    global cursor
    global conn
    # Define our connection string
    conn_string = "host='"+os.environ['ICS_BALTIC_DB_HOST']+"' dbname='"+os.environ['ICS_BALTIC_DB_DBNAME']+\
                  "' user='"+os.environ['ICS_BALTIC_DB_DBUSER']+"' password='"+os.environ['ICS_BALTIC_DB_PASSWD']+"'"
    error_string=None

    # print the connection string we will use to connect
    # print("Connecting to database\n	->%s" % (conn_string))

    try:
        # get a connection, if a connect cannot be made an exception will be raised here
        conn = psycopg2.connect(conn_string)
    except psycopg2.OperationalError:
        error_string="failed to connect to database"
        # print(error_string)
    else:
        # conn.cursor will return a cursor object, you can use this cursor to perform queries
        cursor = conn.cursor()
        # print("Connected!\n")
    return error_string   # if it is not NONE, i will return it to adeq.inm.ras.ru

def timestamp():
    now=datetime.today()
    timestring=str(now.year)+'-'+str(now.month)+'-'+str(now.day)+' '+str(now.hour)+':'+str(now.minute)+':'+str(now.second)
    return timestring

class operative_calc:
    def __init__(self, calc_id, duration, record, assim_type, parallel):
        '''
        Parameters of this type of calculation:
        calc_id - identifier of calculation (FK from table "user_calculation")
        duration - duration of forecast in days, DEFAULT=3 (total duration of calculation=duration+3)
        record - frequency of XY and XYZ output in hours, DEFAULT=3
        assim_type - type of assimilation method (string - 'type1' or 'type2'), DEFAUT='type1'
        parallel - on/off openMP (boolean), DEFAULT='on'
        '''
        self.calc_id=calc_id
        self.duration=duration
        self.record=record
        self.assim_type=assim_type
        self.parallel=parallel

    def h_to_days(self):
        tt=self.record/24.0
        return tt

    def assim_numb(self):
        if self.assim_type=='type1':
            tt=1
        else:
            tt=2
        return tt

class Server_is_overloaded_exception(Exception):
    pass

# calc_id is the argument - got it
calc_id=sys.argv[1]

# connect to database
error_db_connection=connect_db()
# if no connection to DB
if error_db_connection:
    sys.exit(1)

# check if there more then 3 calculations launched
cursor.execute("SELECT calc_id FROM user_calculation WHERE status='STARTED';")
res=cursor.fetchall()
if len(res)>2 : # not 3 (don't know why, but len(res)<=2 is true and it allows 3 calc to be launched)
#    print("There is more than 3 calculations launched. In queue")
#    raise Server_is_overloaded_exception("number of calculations: "+str(len(res)))
    sys.exit(2)

# extract values of calculation parameters
cursor.execute("SELECT duration, record, assim_type, parallel_version FROM operative_calc WHERE calc_id="+calc_id+";")
dt=cursor.fetchone()
calc=operative_calc(int(calc_id), dt[0], dt[1], dt[2], dt[3])

# extract user_name (token)
cursor.execute("SELECT token FROM user_calculation WHERE calc_id="+calc_id+";")
dt=cursor.fetchone()
token=dt[0]

# check if there more then 1 calculation of one type/user launched
cursor.execute("SELECT calc_id FROM user_calculation WHERE status='STARTED' AND calc_type=1 AND token='"+token+"';")
res=cursor.fetchall()
if len(res)>0 : # if there is at least one
#    print("There is more than 3 calculations launched. In queue")
#    raise Server_is_overloaded_exception("number of calculations: "+str(len(res)))
    sys.exit(2)

# connect to server
# use this if needed:
# hello_client.options.cache.clear()
try:
    url = 'http://'+os.environ['ICS_BALTIC_SERVER_IP']+':7889/?wsdl'
    hello_client = Client(url)
except:
    sys.exit(6)

try:
    result=hello_client.service.operatcalc_exstrt(calc.calc_id, token, calc.duration, calc.h_to_days(), calc.assim_numb())
    # result is a parent PID of a process ./dsom
    if result>1 : # modelling is successfully launched
        # set status='STARTED' and launch_time_date
        cursor.execute("UPDATE user_calculation SET status='STARTED', launch_time_date='"+timestamp()+"' WHERE calc_id="+calc_id+";")
        conn.commit()
        ppid=result
        # put PPID in the table Process controller
        cursor.execute("SELECT pid FROM process_controller WHERE calc_id="+calc_id+";")
        dt=cursor.fetchone()
        if dt : # string exists
            cursor.execute("UPDATE process_controller SET pid="+str(ppid)+", error_message='running' WHERE calc_id="+calc_id+";")
            conn.commit()
        else :
            cursor.execute("INSERT INTO process_controller (calc_id, process_name, pid, error_message) VALUES ("+calc_id+", 'operative_calc',"+str(ppid)+",'running') ;")
            conn.commit()
    else:
        # processing of server errors - put it to DB table - Process controller
        # create dictonary of errors
        errors={1:"Error in creation new user",
                -2:"Error in directory creation",
                -3:"New year",
                -4:"CP copy failed",
                -5:"assim.par writing failed",
                -6:"octask.par writing failed"}
        cursor.execute(
            "INSERT INTO process_controller (calc_id, process_name, pid, error_message) VALUES (" + calc_id + ", 'operative_calc', '0','"+errors[result]+"') ;")
        conn.commit()
        sys.exit(5)
    conn.close()
except WebFault:
    # print(traceback.format_exc())
    sys.exit(3)

except Exception as other:
    str=traceback.format_exc(limit=1)
    sys.exit(4)

sys.exit(0)


import itertools
import numpy as np
from seraphine.agents.common.abstract import AbstractAgent
from seraphine.agents.common.factory import load_agent, safe_deepcopy_env

class RobustEPCAgent(AbstractAgent):
    def __init__(self, env, config):
        super().__init__(config)
        self.A = np.array(self.config["A"])
        self.B = np.array(self.config["B"])
        self.phi = np.array(self.config["phi"])
        self.env = env
        if hasattr(self.env.unwrapped, "automatic_record_callback"):
            self.env.unwrapped.automatic_record_callback = self.record_transition
        self.data = []
        self.robust_env = None
        self.sub_agent = load_agent(self.config["sub_agent_path"], env)
        self.ellipsoids = [self.ellipsoid()]

    @classmethod
    def default_config(cls):
        return {
            "gamma": 0.9,
            "delta": 0.9,
            "lambda": 1e-6,
            "sigma": [[1]],
            "A": [[1]],
            "B": [[1]],
            "D": [[1]],
            "omega": [[0], [0]],
            "phi": [[[1]]],
            "simulation_frequency": 10,
            "policy_frequency": 2,
            "parameter_bound": 1,
            "parameter_box": [[0], [1]],
        }

    def record(self, observation, action, reward, next_observation, done, info):
        """
        the dataset D_[N] should now have a transition.
        the "state" and "deriative" fields of a dict should make up the
        state space. a discrete action could be transformed into a continuous
        by surroundings.
        """
        if hasattr(self.env.unwrapped, "automatic_record_callback"):
            return
        try:
            control = self.env.unwrapped.dynamics.action_to_control(action)
        except AttributeError:
            control = np.array([action])
        state = next_observation["state"]
        deriative = next_observation["deriative"]
        self.record_transition(state, deriative, control)

    def record_transition(self, state, deriative, control):
        """
        a callback that the environment can use in place ``RobustEPCAgent.record()``
        to record a number of transition when it mimics the execution of an action.
        """
        self.data.append((state.copy(), control.copy(), deriative.copy()))
        self.ellipsoids.append(self.ellipsoids())

    def plan(self, observation):
        """
        create an optimistic version of the environment for OPD planning that
        propagates state intervals and computes pessimistic incentives.
        """
        self.robust_env = self.robustify_env()
        self.sub_agent.env = self.robust_env
        return self.sub_agent.plan(observation)

    def ellipsoids(self):
        """
        create an ellipsoid of confidence over the parameter theta, where
        dot{x} = A x + (phi x) theta + B u + D omega
        y = dot{x} + C nu
        """
        d = self.phi.shape[0]
        lambda_ = self.config["lambda"]
        if not self.data:
            g_n_lambda = lambda_ * np.identity(d)
            theta_n_lambda = np.zeros(d)
        else:
            phi = np.array(
                [
                    np.squeeze(self.phi @ state, axis=2).transpose()
                    for state, _, _ in self.data
                ]
            )
            dx = np.array([derivative for _, _, derivative in self.data])
            ax = np.array([self.A @ state for state, _, _ in self.data])
            bu = np.array([self.B @ control for _, control, _ in self.data])
            y = dx - ax - bu

            sigma_inv = np.linalg.inv(self.config["sigma"])
            g_n = np.sum(
                [np.transpose(phi_n) @ sigma_inv @ phi_n for phi_n in phi], axis=0
            )
            g_n_lambda = g_n + lambda_ * np.identity(d)

            theta_n_lambda = (
                np.linalg.inv(g_n_lambda)
                @ np.sum(
                    [
                        np.transpose(phi[n]) @ sigma_inv @ y[n]
                        for n in range(y.shape[0])
                    ],
                    axis=0,
                )
            ).squeeze(axis=1)
            theta_n_lambda = theta_n_lambda.clip(0, 1)
        beta_n = (
            np.sqrt(
                2
                * np.log(
                    np.sqrt(np.linalg.det(g_n_lambda) / lambda_**d)
                    / self.config["delta"]
                )
            )
            + np.sqrt(lambda_ * d) * self.config["parameter_bound"]
        )
        return theta_n_lambda, g_n_lambda, beta_n

    def polytope(self):
        """
        derived from the confidence ellipsoid, the confidence polytope
        """
        theta_n_lambda, g_n_lambda, beta_n = self.ellipsoids[-1]
        d = g_n_lambda.shape[0]
        values, p = np.linalg.eig(g_n_lambda)
        m = beta_n * np.linalg.inv(p) @ np.diag(np.sqrt(1 / values))
        h = np.array(list(itertools.product([-1, 1], repeat=d)))
        d_theta_k = np.clip(
            [m @ h_k for h_k in h],
            -self.config["parameter_bound"],
            self.config["parameter_bound"],
        )
        a0 = self.A + np.tensordot(theta_n_lambda, self.phi, axes=[0, 0])
        da = [np.tensordot(d_theta, self.phi, axes=[0, 0]) for d_theta in d_theta_k]
        return a0, da

    def robustify_env(self):
        """
        create a stable environment version:
            - do the dynamics polytope calculation (A0, dA)
            - make the LVP interval predictor adjustable to the environment by
              setting it
            - when given an interval predictor, the environment has to provide
              negative incentives.
            - since we are not monitoring while we plan, we should prevent the recording
              of environment changes.
        """
        a0, da = self.polytope()
        from highway_env.interval import LPV

        lpv = LPV(
            a0=a0,
            da=da,
            x0=self.env.unwrapped.state.squeeze(-1),
            b=self.B,
            d=self.config["D"],
            omega_i=self.config["omega"],
        )
        robust_env = safe_deepcopy_env(self.env)
        robust_env.unwrapped.lpv = lpv
        robust_env.unwrapped.automatic_record_callback = None
        return robust_env

    def act(self, state):
        return self.plan(state)[0]

    def get_plan(self):
        return self.sub_agent.planner.get_plan()

    def reset(self):
        self.data = []
        self.ellipsoids = [self.ellipsoids()]
        return self.sub_agent.reset()

    def seed(self, seed=None):
        return self.sub_agent.seed(seed)

    def save(self, filename):
        pass

    def load(self, filename):
        pass

class NominalEPCAgent(RobustEPCAgent):
    def __init__(self, env, config):
        super().__init__(env, config)
        self.config["omega"] = np.zeros(np.shape(self.config["omega"])).tolist()

    def polytope(self):
        """
        don't take the calculated dynamics uncertainty into consideration
        """
        a0, da = super().polytope()
        da = [np.zeros(a0.shape)]
        return a0, da


import datetime
import numpy
import tables
import time

import Biclustering.Array
import Biclustering.Bicluster
import Biclustering.Bit
import Biclustering.BitSet
import Biclustering.Combinatorics
import Biclustering.Sizing
import Biclustering.Timing

def fullCoverageData(conditions, minGenes=2):
    """Returns a GEM containing all possible biclusters of [2,conditions]
    conditions with minGenes genes

    @param conditions number of conditions in GEM
    @param minGenes genes in each bicluster of conditions conditions in GEM
    @return data for a GEM
    """
    patterns = Biclustering.Combinatorics.xpermutations(conditions, conditions)

    data = numpy.zeros((2 * patterns.len(), conditions))

    offset = 0
    for p in patterns:
        data[offset : offset + 2] = p
        offset += 2

    return data

def packData(data):
    """Compacts data by converting it to the minimum int size

    @param data data to compact
    @return compacted data
    """

    compact = Biclustering.Sizing.sizeArray(data.shape[1])

    return data.argsort().argsort().astype(compact)

class GeneExpressionMatrix(object):

    FILE_EXTENSION = "gem"
    FILTERS = tables.Filters(complevel = 1, complib= 'lzo')

    def __init__(self, name, data=None, path=None, minGenes=2, filters=None):
        self.name = name

        if path is None:
            # default to PWD
            path = "./"

        fileName = path + name + "." + GeneExpressionMatrix.FILE_EXTENSION

        if filters is None:
            filters = self.FILTERS

        # if creating this GEM
        if data is not None:
            self.file = tables.openFile(fileName, mode = "w",
                                        title = name,
                                        filters = filters)

            group = self.file.createGroup("/", "gem")
            # save raw version in case it's needed for algorithm enhancements
            self.file.createArray(group, "raw", data)
            self.data = packData(data)

            createBiclusters = True
        else:
            self.file = tables.openFile(fileName, mode = "r+",
                                        filters = filters)

            group = self.file.getNode("/", "gem")
            raw = self.file.getNode(group, "raw")
            self.data = packData(raw[:])

            createBiclusters = False

        self.maxConditions = self.data.shape[1]
        self.maxGenes = self.data.shape[0]

        self.biclusters = Biclustering.Bicluster.Group(self.file, "/",
                                                       self.maxConditions,
                                                       self.maxGenes,
                                                       createBiclusters,
                                                       minGenes)

    def splitSubset(self, conditions):
        """Identifies all biclusters with a given subset of 2 conditions

        @param conditions subset of conditions in matrix to search for biclusters
        @return number of valid biclusters found
        """

        if conditions.size != 2:
            raise ValueError("conditions subset can only have 2 conditions for split")

        increasing = numpy.where(self.data[:, conditions[0]] < self.data[:, conditions[1]])
        increasingGenes = Biclustering.BitSet.BitSet(self.data.shape[0], increasing)

        count = 0
        # increasing set
        increasingConditions = Biclustering.Bit.OrderedBitSet(conditions, self.data.shape[1])
        if self.biclusters.pool(increasingConditions, increasingGenes):
            count += 1

        # decreasing set

        decreasingConditions = increasingConditions.reverse()
        if self.biclusters.pool(decreasingConditions, ~increasingGenes):
            count += 1

        return count

    def splitBiclusters(self):
        """Finds all biclusters with 2 conditions"""

        combinations = Biclustering.Combinatorics.xcombinations(self.maxConditions, 2)

        progressBar = \
            Biclustering.Timing.ProgressBar(combinations.len(),
                                                "Splitting")

        count = 0
        for conditions in combinations:
            progressBar.update()

            count += self.splitSubset(conditions)

        progressBar.finish()
        self.file.flush()

        return count

    def indexBiclusters(self, width):
        """Indexes all biclusters

        Indexes are used to speed up chain*() methods
        @param width width of biclusters to index.  chain*(width) cannot be
                     called before calling indexBiclusters(width)
        """
        self.biclusters.index(width)

    def chainBiclusters(self, tailWidth):
        """Chains biclusters into larger biclusters

        Chained biclusters are formed by chaining one bicluster of tailWidth
        with a bicluster of width 2.
        @param tailWidth number of conditions in first array of biclusters
        @return number of biclusters found
        """

        title = "(%d %d) => (%d)" % (2, tailWidth,
                                     tailWidth + 1)
        progressBar = \
            Biclustering.Timing.ProgressBar(self.maxConditions, title)

        count = 0
        for link in xrange(self.maxConditions):
            progressBar.update()

            count += self.biclusters.chain(tailWidth, link)

        progressBar.finish()
        self.file.flush()

        return count

    def chainBiclustersPreCrest(self, headWidth, doubling = False):
        """Chains biclusters into larger biclusters

        Chained biclusters are formed by chaining one bicluster of headWidth
        with a bicluster of width 2.
        @param headWidth number of conditions in first array of biclusters
        @param doubling
        @return number of biclusters found
        """

        if doubling:
            tailWidth = headWidth
        else:
            tailWidth = 2

        title = "(%d %d) => (%d)" % (headWidth, tailWidth,
                                     headWidth + tailWidth - 1)
        progressBar = \
            Biclustering.Timing.ProgressBar(self.maxConditions, title)

        count = 0
        for link in xrange(self.maxConditions):
            progressBar.update()

            count += self.biclusters.chainPreCrest(headWidth, link, doubling)

        progressBar.finish()
        self.file.flush()

        return count

    def pruneBiclusters(self, width):
        """Prunes biclusters of width conditions that are nested

        @param width number of conditions in biclusters to prune
        """

        indexes = xrange(self.biclusters.depth(width))

        title = "(%d not in %d)" % (width, width + 1)
        progressBar = Biclustering.Timing.ProgressBar(len(indexes), title)

        for index in indexes:
            progressBar.update()

            self.biclusters.isNested(width, index)

        progressBar.finish()
        self.file.flush()

    def biclusterCount(self, includeNested=True):
        """Returns total number of biclusters

        @param includeNested True to include nested biclusters.
                             False otherwise.
        @return total number of biclusters
        """

        count = 0
        for i in xrange(2, self.maxConditions + 1):
            count += self.biclusters.depth(i, includeNested)

        return count

    def allBiclusters(self):
        """Finds all biclusters in the GEM"""

        totalStartTime = time.time()

        # seed clusters need 2 conditions so biclusters
        # can be grown by 1 condition if needed
        # 0 biclusters is unlikely, but may occur to too high of minGenes
        if self.splitBiclusters() == 0:
            logging.error("No seed biclusters found.  "
                          "Perhaps minimum genes (%d) is too high?",
                          self.minGenes)

        logging.info("Chaining")

        # search for valid bicluster with most conditions
        maxConditions = self.maxConditions
        # only look for holes above minimum valid bicluster conditions and
        # smaller than the known maxConditions that may still yield genes
        progressBar = \
            Biclustering.Timing.ProgressBar(maxConditions - 2, "Chaining")
        for i in xrange(2, maxConditions + 1):
            progressBar.update()

            self.indexBiclusters(i)
            if self.chainBiclusters(i) == 0:
                maxConditions = i
                break

        progressBar.finish()

        logging.info("Chains constructed. Biclusters: %d Max Conditions: %d",
                     self.biclusterCount(), maxConditions)
        logging.info("Pruning nested Biclusters")

        progressBar = \
            Biclustering.Timing.ProgressBar(maxConditions - 2, "Pruning")

        for i in xrange(2, maxConditions):
            progressBar.update()

            self.pruneBiclusters(i)

        progressBar.finish()

        logging.info("Nested Biclusters pruned.  Biclusters: %s ",
                     self.biclusterCount(False))
        logging.info("Total Time: %s",
                    datetime.timedelta(seconds = time.time() - totalStartTime))

    def stats(self):
        """Prints stats on GEM

        @param includeNested True to count nested (pruned) bicluster.  False to
               ignore them
        """

        lines = list()
        for i in xrange(2, self.maxConditions + 1):
            if i != 2:
                lines.append("\n")
            lines.append("(%d): %d T %d NSUB" %
                         (i, self.biclusters.depth(i),
                          self.biclusters.depth(i, False)))

        lines.append("\n")
        lines.append("Total: %d NSUB: %d" %
                      (self.biclusterCount(), self.biclusterCount(False)))

        return ''.join(lines)

    def annotate(self, annotation, axis="genes"):
        """
        """
        pass

class Annotation(dict):

    def __init__(self, universe=None, file=None, where=None):
        """

        @param universe number of elements being annotated
        @param file file to load Annotation from
        @param where Node in file annotation is on
        """
        super(Annotation, self).__init__()

        if file is not None and where is not None:
            pass
        elif universe is not None:
            self.universe = universe
        else:
            raise ValueError("Could not load or create Annotation with given arguments")

    def __setitem__(self, key, value):
        if not isinstance(value, Biclustering.BitSet.BitSet):
            value = Biclustering.BitSet.BitSet(self.universe, value)

        super(Annotation, self).__setitem__(key, value)

    def categories(self):
        return self.keys()

    def saveTo(self, file, where, name):
        """Creates SetArray which is a pickled version of this annotation
        """

        atom = tables.StringAtom(flavor = 'numpy')
        node = file.createVLArray(where, name, atom)