text:
class QMenu;
class QVBoxLayout;

class SharedPoolWidget;
class SharedServerWidget;
class CustomServerWidget;
class ApplicationSettings;
struct RemoteServerParams;
struct SharedPool;

namespace Ui
{
class AssetCacheServerWidget;
}

class AssetCacheServerWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit AssetCacheServerWindow(ServerCore& core, QWidget* parent = nullptr);
    ~AssetCacheServerWindow() override;
    void OnFirstLaunch();

private slots:
    void OnTrayIconActivated(QSystemTrayIcon::ActivationReason reason);
    void ChangeSettingsPage(int newRow);

    void OnEditAction();
    void OnStartAction();
    void OnStopAction();

    void OnFolderSelection();
    void OnFolderTextChanged();
    void OnCacheSizeChanged(double);
    void OnNumberOfFilesChanged(int);
    void OnAutoSaveTimeoutChanged(int);
    void OnPortChanged(int);
    void OnHttpPortChanged(int);
    void OnAutoStartToggled(bool);
    void OnSystemStartupToggled(bool);
    void OnRestartToggled(bool);

    void OnServersAreaRangeChanged(int, int);
    void OnCustomServerToAdd();
    void OnCustomServerToRemove();
    void OnCustomServerEdited();
    void OnCustomServerChecked(bool);
    void OnSharedPoolChecked(bool);
    void OnSharedServerChecked(bool);

    void OnShareButtonClicked();
    void OnUnshareButtonClicked();
    void OnGenerateAssertButtonClicked();
    void OnGenerateCrashButtonClicked();
    void OnClearButtonClicked();
    void OnApplyButtonClicked();
    void OnCloseButtonClicked();

    void OnServerStateChanged(const ServerCore*);
    void UpdateUsageProgressbar(DAVA::uint64, DAVA::uint64);

private:
    struct CheckedRemote
    {
        enum Type
        {
            POOL,
            POOL_SERVER,
            CUSTOM_SERVER,
            NONE
        } type = NONE;
        PoolID poolID = NullPoolID;
        ServerID serverID = NullServerID;

        union CheckedWidget
        {
            SharedPoolWidget* poolWidget = nullptr;
            SharedServerWidget* serverWidget;
            CustomServerWidget* customServerWidget;
        } widget;

        CheckedRemote() = default;
        CheckedRemote(SharedPoolWidget* w);
        CheckedRemote(SharedServerWidget* w);
        CheckedRemote(CustomServerWidget* w);
    };

    struct SharedServerContainer
    {
        bool existsInUpdate = false;
        QVBoxLayout* poolLayout = nullptr;
        SharedServerWidget* serverWidget = nullptr;
    };

    using ServerContainersMap = DAVA::Map<ServerID, SharedServerContainer>;

    struct SharedPoolContainer
    {
        bool existsInUpdate = false;
        SharedPoolWidget* poolWidget = nullptr;
        ServerContainersMap serverContainers;
        QVBoxLayout* poolLayout = nullptr;
    };

    using PoolContainersMap = DAVA::Map<PoolID, SharedPoolContainer>;

private:
    void CreateTrayMenu();

    void closeEvent(QCloseEvent* e) override; // QWidget

    void LoadSettings();
    void SaveSettings();

    void ConstructCustomServersList();
    void ClearCustomServersList();

    void ConstructSharedPoolsList();
    void UpdateSharedPoolsList();

    void ConstructSharedPoolsCombo();
    void UpdateSharedPoolsCombo();

    void ClearVBoxLayout(QVBoxLayout* layout);

    void SetVisibilityForShareControls();

    SharedPoolContainer& AddPoolContainer(const SharedPool& pool);
    SharedServerContainer& AddServerContainer(SharedPoolContainer& poolContainer, const SharedServer& server);
    void RemovePoolContainer(PoolContainersMap::iterator iter);
    void RemoveServerContainer(SharedPoolContainer& poolContainer, ServerContainersMap::iterator iter);

    void AddCustomServer(const RemoteServerParams& newServer);

    void ClearPreviousCheck();
    CheckedRemote GetCheckedRemote();

    void DisplayCurrentRemoteServer();

    void SetupLaunchOnStartup(bool toLaunchOnStartup, bool toRestartOnCrash);

    void VerifyChanges();

    enum SettingsState
    {
        NOT_EDITED,
        EDITED,
        EDITED_NOT_CORRECT
    };

    void ChangeSettingsState(SettingsState newState);

private:
    std::unique_ptr<Ui::AssetCacheServerWidget> ui;
    QAction* startAction = nullptr;
    QAction* stopAction = nullptr;
    QSystemTrayIcon* trayIcon = nullptr;
    std::unique_ptr<QIcon> greenGreenTrayIcon;
    std::unique_ptr<QIcon> greenGrayTrayIcon;
    std::unique_ptr<QIcon> greenRedTrayIcon;
    std::unique_ptr<QIcon> redGrayTrayIcon;

    QVBoxLayout* customServersLayout = nullptr;
    QVBoxLayout* sharedPoolsLayout = nullptr;
    QVBoxLayout* serversLayout = nullptr;

    bool customServerManuallyAdded = false;

    DAVA::List<CustomServerWidget*> customServersWidgets;
    PoolContainersMap poolContainers;
    DAVA::Vector<PoolID> comboBoxIDs;

    ServerCore& serverCore;

    SettingsState settingsState = NOT_EDITED;
    bool showAdvanced = false;
};


text:
package com.frequentflyer.cms.repositories;

import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import com.frequentflyer.cms.models.Crew;

/**
 *
 * Repository for handing crew operations
 *
 * @author Sasa Radovanovic
 *
 */
public interface CrewRepository extends MongoRepository<Crew, String> {

	List<Crew> findByFirstNameLike(String name);

	List<Crew> findByLastNameLike(String lastName);

	List<Crew> findByCrewType(String crewType);

	List<Crew> findByCountryOfOriginLike(String countryOfOrigin);

	Crew findByUsername(String username);

	Page<Crew> findAllByCrewType(String type, Pageable pageable);

	Page<Crew> findAllByCrewTypeAndFirstNameLike(String type, String firstName, Pageable pageable);

	Page<Crew> findAllByCrewTypeAndLastNameLike(String type, String lastName, Pageable pageable);

	Page<Crew> findAllByCrewTypeAndCompanyIdLike(String type, String companyId, Pageable pageable);

	long countByCrewType(String type);

	Crew findById (String id);

	@Query(value = "{'_id' : { $in : ?0 }}")
	List<Crew> findAllById (ArrayList<ObjectId> ids);
}

redpajama_set_name: RedPajamaGithub


text:
Pattern: Line if too long

Issue: -

## Description

Your source code should not contain very long lines. The default wrapping in most tools disrupts the visual structure of the code, making it more difficult to understand.

## Configuration

```powershell
    Rules = @{
        PSAvoidLongLines  = @{
            Enable     = $true
            LineLength = 120
        }
    }
```

### Parameters

#### Enable: bool (Default value is `$false`)

Enable or disable the rule during ScriptAnalyzer invocation.

#### MaximumLineLength: int (Default value is 120)

Optional parameter to override the default maximum line length.

## Further Reading

* [PSScriptAnalyzer - AvoidLongLines](https://github.com/PowerShell/PSScriptAnalyzer/tree/master/docs/Rules/AvoidLongLines.md)


text:
package com.hhcache

import com.typesafe.config.{ConfigFactory, Config}

/**
 * Created by huhong on 15/4/30.
 */
package object core {
  private[hhcache] val rootConfig = ConfigFactory.load()

  private[hhcache] val cacheConfig = rootConfig.getConfig("cache_system")
}


text:
package org.axonframework.common.property;

/**
 * Interface describing a mechanism that can read a predefined property from a given instance.
 *
 * @param <T> The type of object defining this property
 * @author Maxim Fedorov
 * @author Allard Buijze
 * @since 2.0
 */
public interface Property<T> {

    /**
     * Returns the value of the property on given target.
     *
     * @param target The instance to get the property value from
     * @param <V>    The type of value expected
     * @return the property value on target
     */
    <V> V getValue(T target);
}


text:
var MainRouter = AwsDeploy.Router.extend({
    initialize: function () {
        this.session = new SessionManager();
        this.listenTo(this.session, 'signin', this.onSignin);

        this.navbar = new NavBarView({
            session: this.session
        });
        $('#header').html(this.navbar.render().el);

        this.footer = new FooterView();
        $('#footer').html(this.footer.render().el);
    },

    routes: {
        "": "showDeployments",
        "deployments/:deployment_id": "showDeploymentOverview",
        "deployments/:deployment_id/edit": "showDeploymentEdit",
        "deployments/:deployment_id/repository": "showDeploymentRepository",
        "deployments/:deployment_id/application": "showDeploymentApplication",
        "deployments/:deployment_id/health": "showDeploymentHealth",
        "deployments/:deployment_id/log": "showDeploymentLog"
    },

    showDeployments: function () {
        this.showView("#content", new DeploymentsListView());
    },

    showDeploymentOverview: function (deployment_id) {
        if (!this.session.isAuthorized()) {
            this.navbar.showLogin();
            return;
        }

        this.before({
            deployment_id: deployment_id
        }, function (deployment) {
            this.showView("#content", new DeploymentView({
                model: deployment, type: 'overview'
            }));
        });
    },

    showDeploymentEdit: function (deployment_id) {
        if (!this.session.isAuthorized()) {
            this.navbar.showLogin();
            return;
        }

        this.before({
            deployment_id: deployment_id
        }, function (deployment) {
            this.showView("#content", new DeploymentView({
                model: deployment, type: 'edit'
            }));
        });
    },

    showDeploymentRepository: function (deployment_id) {
        if (!this.session.isAuthorized()) {
            this.navbar.showLogin();
            return;
        }

        this.before({
            deployment_id: deployment_id
        }, function (deployment) {
            this.showView("#content", new DeploymentView({
                model: deployment, type: 'repository'
            }));
        });
    },

    showDeploymentApplication: function (deployment_id) {
        if (!this.session.isAuthorized()) {
            this.navbar.showLogin();
            return;
        }

        this.before({
            deployment_id: deployment_id
        }, function (deployment) {
            this.showView("#content", new DeploymentView({
                model: deployment, type: 'application'
            }));
        });
    },

    showDeploymentHealth: function (deployment_id) {
        if (!this.session.isAuthorized()) {
            this.navbar.showLogin();
            return;
        }

        this.before({
            deployment_id: deployment_id
        }, function (deployment) {
            this.showView("#content", new DeploymentView({
                model: deployment, type: 'health'
            }));
        });
    },

    showDeploymentLog: function (deployment_id) {
        if (!this.session.isAuthorized()) {
            this.navbar.showLogin();
            return;
        }

        this.before({
            deployment_id: deployment_id
        }, function (deployment) {
            this.showView("#content", new DeploymentView({
                model: deployment, type: 'log'
            }));
        });
    },

    before: function (options, callback) {
        if (_.isUndefined(callback) && _.isFunction(options)) {
            callback = options;
            options = {};
        }

        var args = [];
        var actions = [];

        if (options.deployment_id) {
            actions.push(_.bind(function (callback) {
                if (options.deployment_id) {
                    this._deployments = new DeploymentCollection();
                    this._deployments.fetch({
                        reset: true,
                        success: function (collection) {
                            args.push(collection.get(options.deployment_id));
                            callback();
                        },
                        error: callback
                    })
                } else {
                    async.nextTick(callback);
                }
            }, this));
        }

        async.series(actions, _.bind(function (err) {
            if (err) {
                return;
            }
            callback.apply(this, args);
        }, this));
    }
});

var app;
new (MainApp = AppMain.extend({
    initialize: function () {
        this.create(function (err) {
            if (err) {
                return;
            }
            app = new MainRouter();
        })
    }
}));


text:
package com.batmgr.filesystem;

import java.awt.Toolkit;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.nio.file.FileVisitResult.CONTINUE;

/**
 * Synchronize a source directory to a target directory
 */
@SuppressWarnings("nls")
public class Synchronize {

    private static final Logger LOG = LoggerFactory.getLogger(Synchronize.class);

    public static void main(String[] args) {
        try {
            boolean scrape = args.length > 2 && args[2].equals("scrape");
            LOG.info("scrape : " + scrape);
            synchronize(Paths.get(args[0]), Paths.get(args[1]), scrape);
        } catch (Throwable t) {
            LOG.error("program aborted", t);
        }
        Toolkit.getDefaultToolkit().beep();
    }

    /**
     * Recursively copy the contents of src to dst.
     *

     * This method is safe: it checks the contents of copied files and issues a warning if there is a difference.
     *

     * This method is efficient: existing files are not copied.
     *

     * This method is not optimal: identical files can be copied several times.
     */
    public static void synchronize(Path src, Path dst, boolean scrape) throws NoSuchAlgorithmException, IOException, InvalidIndexException
    {
        LOG.info(String.format("Synchronize %s", src.toString()));
        DirChecker checker = new DirChecker();
        checker.indexFolder(src);
        checker.sweepFolder(src);
        Files.createDirectories(dst);
        checker.indexFolder(dst);
        checker.sweepFolder(dst);
        // TODO faut-il un délai avant d'avoir un index stable ?
        DirInfo srcInfo = new DirInfo(src);
        Map<String, FileInfo> srcNames = srcInfo.getNameIndex();
        DirInfo dstInfo = new DirInfo(dst);
        Map<String, FileInfo> dstNames = dstInfo.getNameIndex();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(src)) {
            for (Path p : stream) { // cannot use stream.forEach because of IOException
                if (Files.isDirectory(p)) {
                    if (checker.isSpecialDir(p)) {
                        continue;
                    }
                    synchronize(p, dst.resolve(p.getFileName().toString()), scrape);
                } else {
                    if (!Files.isRegularFile(p)) {
                        continue;
                    }
                    String name = p.getFileName().toString();
                    FileInfo srcFileInfo = srcNames.get(name);
                    if (srcFileInfo == null) { // file not indexed (special file, index file...)
                        continue;
                    }
                    Path dstPath = dst.resolve(name);
                    FileInfo dstFileInfo = dstNames.get(name);
                    if (dstFileInfo != null && !srcFileInfo.getHash().equals(dstFileInfo.getHash())) {
                        // file is present but different, remove it
                        Files.delete(dstPath);
                        dstInfo.removeFromIndex(dstFileInfo);
                        dstFileInfo = null;
                    }
                    if (dstFileInfo != null) {
                        // file is present and has same contents, skip it
                        continue;
                    }
                    LOG.info(String.format("Copy %s", name));
                    Files.copy(p, dstPath);
                    dstInfo.addIfNeeded(dstPath);
                    dstNames = dstInfo.getNameIndex();
                    dstFileInfo = dstNames.get(name);
                    if (dstFileInfo == null) {
                        LOG.warn(String.format("%s not present in destination", dstPath));
                        continue;
                    }
                    if (!dstFileInfo.getHash().equals(srcFileInfo.getHash())) {
                        LOG.warn(String.format("%s has different content", dstPath));
                    }
                }
            }
        }
        if (scrape) { // scrub ?? // timing issues ??
            try (DirectoryStream<Path> stream = Files.newDirectoryStream(dst)) {
                for (Path p : stream) { // cannot use stream.forEach because of IOException
                    if (Files.isDirectory(p)) {
                        if (checker.isSpecialDir(p)) {
                            continue;
                        }
                        if (!Files.exists(src.resolve(p.getFileName().toString()))) {
                            LOG.info(String.format("directory %s present only in dest, trying to delete",
                                p.getFileName()));
                            try {
                                Files.walkFileTree(p, new FileVisitor<Path>() {
                                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                                        throws IOException {
                                        return CONTINUE;
                                    }
                                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                                        throws IOException {
                                        LOG.info(String.format("deleting file %s", file));
                                        Files.delete(file);
                                        return CONTINUE;
                                    }
                                    public FileVisitResult visitFileFailed(Path file, IOException exc)
                                        throws IOException {
                                        throw exc;
                                    }
                                    public FileVisitResult postVisitDirectory(Path dir, IOException exc)
                                        throws IOException {
                                        if (exc != null) {
                                            throw exc;
                                        }
                                        LOG.info(String.format("deleting directory %s", dir));
                                        Files.delete(dir);
                                        return CONTINUE;
                                    }
                                });
                            } catch (Exception e) {
                                LOG.warn(String.format("cannot delete %s", p), e);
                            }
                        }
                    } else {
                        if (!Files.isRegularFile(p)) {
                            continue;
                        }
                        String name = p.getFileName().toString();
                        FileInfo dstFileInfo = dstNames.get(name);
                        if (dstFileInfo == null) { // file not indexed (special file, index file...)
                            continue;
                        }
                        FileInfo srcFileInfo = srcNames.get(name);
                        if (srcFileInfo == null) { // not present in src
                            LOG.info(String.format("deleting file %s", p));
                            Files.delete(p);
                        }
                    }
                }
            }
        }
    }
}


text:
layout:     post
author:     Julia Bartlewski
author_lnk: https://github.com/jbartlewski
title:      The National Library of Sweden provides APC data for 3 institutions
date:       2022-07-04 12:00:00
summary:
categories: [general, openAPC]
comments: true
---

The [National Library of Sweden](https://www.kb.se/in-english.html) collects data on APC expenditures from universities and other research institutions in Sweden.

APC expenditures for 3 different institutions have now been made available to OpenAPC.

Contact person for the National Library is [Camilla Lindelöw](mailto:Camilla.Lindelow@kb.se).

## Cost data

The new data set provided by the National Library covers publication fees for 37 articles. Total expenditure amounts to 65 914€ and the average fee is 1 781€.

The data originates from the following institutions:

- Stockholm School of Economics
- Swedish School of Sport and Health Sciences
- University of Borås

## Overview

A detailed analysis of the contributed data sets provides the following overview:

### Breakdown by publisher

|                              | Articles| Fees paid in EURO| Mean Fee paid|
|:-----------------------------|--------:|-----------------:|-------------:|
|MDPI AG                       |       22|             34906|          1587|
|Springer Nature               |        4|              8708|          2177|
|Elsevier BV                   |        2|              4841|          2421|
|Informa UK Limited            |        2|              3080|          1540|
|Oxford University Press (OUP) |        2|              5810|          2905|
|BioResources                  |        1|              1174|          1174|
|BMJ                           |        1|              2115|          2115|
|Informing Science Institute   |        1|                64|            64|
|MIS Quarterly                 |        1|              4866|          4866|
|Walter de Gruyter GmbH        |        1|               350|           350|

### Fees paid per publisher (in EURO)

![plot of chunk tree_natlibswe_2022_07_04_full](/figure/tree_natlibswe_2022_07_04_full-1.png)

###  Average costs per publisher (in EURO)

![plot of chunk box_natlibswe_2022_07_04_publisher_full](/figure/box_natlibswe_2022_07_04_publisher_full-1.png)


text:
#ifndef __SYSTEM_SAM4E_H__
#define __SYSTEM_SAM4E_H__

#include <stdint.h>

#include "coredev/system_core_clock.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Setup the microcontroller system.
 * Initialize the System and update the SystemCoreClock variable.
 */
void SystemInit(void);

/**
 * @brief Updates the SystemCoreClock with current core Clock
 * retrieved from cpu registers.
 */
void SystemCoreClockUpdate(void);

#ifdef __cplusplus
}
#endif

extern uint32_t SystemCoreClock; // System Clock Frequency (Core Clock) in Hz

#endif // __SYSTEM_SAM4E_H__


text:
.sheet-section {
    padding: 1rem!important;
    box-shadow: 0 .5rem 1rem rgba(0,0,0,.15)!important;
}

.sheet-sub-section {
    padding: 1rem!important;
    display: none;
}

.sheet-inline {
    display: inline-block;
}

.sheet-block {
    display: block;
}

.sheet-full {
    width: 100%;
}

.sheet-med-wide {
    width: 30%;
}

.sheet-wide {
    width: 45%;
}

.sheet-extra-wide {
    width: 60%;
}

.sheet-column-label {
    font-weight: bold;
    font-size: 1.2em;
    margin-bottom: 10px;
}

input.sheet-arrow:checked ~ div.sheet-sub-section {
    display: block;
}

/* Checkboxes and checkbox labels*/
input[type="checkbox"] {
    display: inline-block;
    border-style: solid black;
    border-radius: 0px;
}

.sheet-checkbox-label {
    display: inline;
}

.sheet-checkbox-label-small {
    display: inline;
    font-size: 1rem;
}


text:
layout: post
title: torch nn.Module
categories: "AI"
tags: AI torch pytorch nn.Module
author: Jasper
---

* content
{:toc}

从代码的角度熟悉nn.Module,跳过苦涩难懂的文档。Module的代码非常简单,比网上各种文档简单多了。

# 内置方法

python重载:

`__dict__`:获取对象属性
`__setattr__`: 设置对象属性,响应python setattr(object)函数, 同时,obj.my\_attr = "value" 也同理,保存到`__dict__`
`__dir__`:获取对象属性和方法,响应python dir(object)函数
`__getattr__`:响应python getattr(object)函数
`__repr__`:响应print()函数,输出"类名+object at+内存地址"信息
`__iadd__`:响应算术运算符 `+`

Module自定义:

`__setstate__`:设置Module的状态,已知在JIT.trace时有用到

# 构建Module

```python
import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))
```

`__init__`: 将conv1和conv2设置为属性,见上方`__setattr__`的说明

除此之外,super中构建:

```python
    def __init__(self):
        """
        Initializes internal Module state, shared by both nn.Module and ScriptModule.
        """
        torch._C._log_api_usage_once("python.nn_module")

        self.training = True
        self._parameters = OrderedDict()
        self._buffers = OrderedDict()
        self._non_persistent_buffers_set = set()
        self._backward_hooks = OrderedDict()
        self._is_full_backward_hook = None
        self._forward_hooks = OrderedDict()
        self._forward_pre_hooks = OrderedDict()
        self._state_dict_hooks = OrderedDict()
        self._load_state_dict_pre_hooks = OrderedDict()
        self._modules = OrderedDict()
```

先记住这些属性,在创建Module对象时,它们仅仅是初始化。

# 容器

`modules/container.py`

容器是用来存放算子的东西,容器也是一个Module对象。
算子也是一个Module对象,所以,容器可以称为:
1. 算子的容器;
2. Module的容器;
3. Module的Module

容器有几种:
1. Sequential
2. ModuleList
3. ......

## Sequential的构建

```python
class Sequential(Module):
    def __init__(self, *args):
        super(Sequential, self).__init__()
        if len(args) == 1 and isinstance(args[0], OrderedDict):
            for key, module in args[0].items():
                self.add_module(key, module)
        else:
            for idx, module in enumerate(args):
                self.add_module(str(idx), module)

    @_copy_to_script_wrapper
    def __getitem__(self, idx) -> Union['Sequential', T]:
        if isinstance(idx, slice):
            return self.__class__(OrderedDict(list(self._modules.items())[idx]))
        else:
            return self._get_item_by_idx(self._modules.values(), idx)

    @_copy_to_script_wrapper
    def __len__(self) -> int:
        return len(self._modules)

    @_copy_to_script_wrapper
    def __dir__(self):
        keys = super(Sequential, self).__dir__()
        keys = [key for key in keys if not key.isdigit()]
        return keys

    @_copy_to_script_wrapper
    def __iter__(self) -> Iterator[Module]:
        return iter(self._modules.values())

    def forward(self, input):
        for module in self:
            input = module(input)
        return input
```

```python
# Example of using Sequential
model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )

# Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))
```

见`__init__`,第一个Example,add_module时,module的名称使用str(idx),第二Example,使用dict->key。

同时,nn.Conv2d, nn.ReLU等也是一个Module。

```python
class _ConvNd(Module):
class Conv2d(_ConvNd):
```

对象创建时,将所有的算子通过add\_module()加入到容器的属性`_modules`中。`_modules`属于父类,即Module类的属性,。

## ModuleList的构建

ModuleList实际上也是一个Module对象,只是它实现了list的操作接口,表现为一个List。

```python
class ModuleList(Module):
    def __init__(self, modules: Optional[Iterable[Module]] = None) -> None:
        super(ModuleList, self).__init__()
        if modules is not None:
            self += modules

    def __iadd__(self, modules: Iterable[Module]) -> 'ModuleList':
        return self.extend(modules)

    def extend(self, modules: Iterable[Module]) -> 'ModuleList':
        offset = len(self)
        for i, module in enumerate(modules):
            self.add_module(str(offset + i), module)
        return self

    def forward(self):
        raise NotImplementedError()
```

```python
Example::

    class MyModule(nn.Module):
        def __init__(self):
            super(MyModule, self).__init__()
            self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])

        def forward(self, x):
            # ModuleList can act as an iterable, or be indexed using ints
            for i, l in enumerate(self.linears):
                x = self.linears[i // 2](x) + l(x)
            return x
```
ModuleList本身不支持forward操作。
为了方便,Example中使用一个Module来保存ModuleList,提供forward接口,在forward中遍历ModuleList。

# load_state_dict

Copies parameters and buffers from state_dict into this module and its descendants.

`local_name_params = itertools.chain(self._parameters.items(), self._buffers.items())`

列出Module的\_parameter 和 \_buffers, 将state\_dict copy过来,通过key进行匹配。

如果strict=true,返回值中,missing\_keys表示本Module没有state\_dict中的key(miss),unexpected\_keys表示state\_dict的key不包含在Module中(多余的)。
missing\_keys和unexpected\_keys都是站在static\_dict的角度而言的。

# 遍历Module

包括,遍历module,parameter,children,buffer。

module:当前module和子module(\_modules)
parameter:参数,当前module和\_modules中的parameter
children:仅仅是遍历\_modules
buffer: 当前module和子module中的buffer

# 总结

Module相关接口并不难理解,看代码比看doc更好。

# 参考

Pytorch Source


text:
import {
  ACTION_CROP,
  ACTION_ZOOM,
  CLASS_CROP,
  CLASS_MODAL,
  DATA_ACTION,
  DRAG_MODE_CROP,
  DRAG_MODE_MOVE,
  DRAG_MODE_NONE,
  EVENT_CROP_END,
  EVENT_CROP_MOVE,
  EVENT_CROP_START,
  REGEXP_ACTIONS,
} from './constants';
import {
  addClass,
  assign,
  dispatchEvent,
  forEach,
  getData,
  getPointer,
  hasClass,
  isNumber,
  toggleClass,
} from './utilities';

export default {
  resize() {
    if (this.disabled) {
      return;
    }

    const { options, container, containerData } = this;
    const ratioX = container.offsetWidth / containerData.width;
    const ratioY = container.offsetHeight / containerData.height;
    const ratio = Math.abs(ratioX - 1) > Math.abs(ratioY - 1) ? ratioX : ratioY;

    // Resize when width changed or height changed
    if (ratio !== 1) {
      let canvasData;
      let cropBoxData;

      if (options.restore) {
        canvasData = this.getCanvasData();
        cropBoxData = this.getCropBoxData();
      }

      this.render();

      if (options.restore) {
        this.setCanvasData(forEach(canvasData, (n, i) => {
          canvasData[i] = n * ratio;
        }));
        this.setCropBoxData(forEach(cropBoxData, (n, i) => {
          cropBoxData[i] = n * ratio;
        }));
      }
    }
  },

  dblclick() {
    if (this.disabled || this.options.dragMode === DRAG_MODE_NONE) {
      return;
    }

    this.setDragMode(hasClass(this.dragBox, CLASS_CROP) ? DRAG_MODE_MOVE : DRAG_MODE_CROP);
  },

  wheel(event) {
    const ratio = Number(this.options.wheelZoomRatio) || 0.1;
    let delta = 1;

    if (this.disabled) {
      return;
    }

    event.preventDefault();

    // Limit wheel speed to prevent zoom too fast (#21)
    if (this.wheeling) {
      return;
    }

    this.wheeling = true;

    setTimeout(() => {
      this.wheeling = false;
    }, 50);

    if (event.deltaY) {
      delta = event.deltaY > 0 ? 1 : -1;
    } else if (event.wheelDelta) {
      delta = -event.wheelDelta / 120;
    } else if (event.detail) {
      delta = event.detail > 0 ? 1 : -1;
    }

    this.zoom(-delta * ratio, event);
  },

  cropStart(event) {
    const { buttons, button } = event;

    if (
      this.disabled

      // Handle mouse event and pointer event and ignore touch event
      || ((
        event.type === 'mousedown'
        || (event.type === 'pointerdown' && event.pointerType === 'mouse')
      ) && (
        // No primary button (Usually the left button)
        (isNumber(buttons) && buttons !== 1)
        || (isNumber(button) && button !== 0)

        // Open context menu
        || event.ctrlKey
      ))
    ) {
      return;
    }

    const { options, pointers } = this;
    let action;

    if (event.changedTouches) {
      // Handle touch event
      forEach(event.changedTouches, (touch) => {
        pointers[touch.identifier] = getPointer(touch);
      });
    } else {
      // Handle mouse event and pointer event
      pointers[event.pointerId || 0] = getPointer(event);
    }

    if (Object.keys(pointers).length > 1 && options.zoomable && options.zoomOnTouch) {
      action = ACTION_ZOOM;
    } else {
      action = getData(event.target, DATA_ACTION);
    }

    if (!REGEXP_ACTIONS.test(action)) {
      return;
    }

    if (dispatchEvent(this.element, EVENT_CROP_START, {
      originalEvent: event,
      action,
    }) === false) {
      return;
    }

    // This line is required for preventing page zooming in iOS browsers
    event.preventDefault();

    this.action = action;
    this.cropping = false;

    if (action === ACTION_CROP) {
      this.cropping = true;
      addClass(this.dragBox, CLASS_MODAL);
    }
  },

  cropMove(event) {
    const { action } = this;

    if (this.disabled || !action) {
      return;
    }

    const { pointers } = this;

    event.preventDefault();

    if (dispatchEvent(this.element, EVENT_CROP_MOVE, {
      originalEvent: event,
      action,
    }) === false) {
      return;
    }

    if (event.changedTouches) {
      forEach(event.changedTouches, (touch) => {
        // The first parameter should not be undefined (#432)
        assign(pointers[touch.identifier] || {}, getPointer(touch, true));
      });
    } else {
      assign(pointers[event.pointerId || 0] || {}, getPointer(event, true));
    }

    this.change(event);
  },

  cropEnd(event) {
    if (this.disabled) {
      return;
    }

    const { action, pointers } = this;

    if (event.changedTouches) {
      forEach(event.changedTouches, (touch) => {
        delete pointers[touch.identifier];
      });
    } else {
      delete pointers[event.pointerId || 0];
    }

    if (!action) {
      return;
    }

    event.preventDefault();

    if (!Object.keys(pointers).length) {
      this.action = '';
    }

    if (this.cropping) {
      this.cropping = false;
      toggleClass(this.dragBox, CLASS_MODAL, this.cropped && this.options.modal);
    }

    dispatchEvent(this.element, EVENT_CROP_END, {
      originalEvent: event,
      action,
    });
  },
};


text:
package com.dmdirc.plugins;

import com.dmdirc.config.ConfigFileBackedConfigProvider;
import com.dmdirc.config.InvalidIdentityFileException;
import com.dmdirc.events.PluginLoadedEvent;
import com.dmdirc.events.PluginUnloadedEvent;
import com.dmdirc.events.eventbus.EventBus;
import com.dmdirc.config.provider.ConfigProvider;
import com.dmdirc.interfaces.config.IdentityController;
import com.dmdirc.util.validators.ValidationResponse;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dagger.ObjectGraph;

import static com.dmdirc.util.LogUtils.APP_ERROR;
import static com.dmdirc.util.LogUtils.USER_ERROR;

/**
 * Stores plugin metadata and handles loading of plugin resources.
 */
public class PluginInfo implements ServiceProvider {

    private static final Logger LOG = LoggerFactory.getLogger(PluginInfo.class);
    /** The metadata for this plugin. */
    private final PluginMetaData metaData;
    /** The manager to use to look up other plugins. */
    private final PluginManager pluginManager;
    /** The manager to register services with. */
    private final ServiceManager serviceManager;
    /** The object graph to pass to plugins for DI purposes. */
    private final ObjectGraph objectGraph;
    /** The controller to add and remove settings from. */
    private final IdentityController identityController;
    /** Filename for this plugin (taken from URL). */
    private final String filename;
    /** The actual Plugin from this jar. */
    private Plugin plugin;
    /** The classloader used for this Plugin. */
    private PluginClassLoader pluginClassLoader;
    /** List of classes this plugin has. */
    private final List<String> myClasses = new ArrayList<>();
    /** Last Error Message. */
    private String lastError = "No Error";
    /** Are we trying to load? */
    private boolean isLoading;
    /** List of services we provide. */
    private final List<Service> provides = new ArrayList<>();
    /** List of children of this plugin. */
    private final List<PluginInfo> children = new ArrayList<>();
    /** Map of exports. */
    private final Map<String, ExportInfo> exports = new HashMap<>();
    /** List of configuration providers. */
    private final Collection<ConfigFileBackedConfigProvider> configProviders = new ArrayList<>();
    /** Event bus to post plugin loaded events to. */
    private final EventBus eventBus;
    /** File system for the plugin's jar. */
    private final FileSystem pluginFilesystem;

    /**
     * Create a new PluginInfo.
     *
     * @param metadata            The plugin's metadata information
     * @param eventBus            Event bus to post event loaded events on.
     * @param identityController  The identity controller to add and remove settings from.
     * @param objectGraph         The object graph to give to plugins for DI purposes.
     *
     * @throws PluginException if there is an error loading the Plugin
     */
    public PluginInfo(
            final PluginManager pluginManager,
            final ServiceManager serviceManager,
            final PluginMetaData metadata,
            final EventBus eventBus,
            final IdentityController identityController,
            final ObjectGraph objectGraph) throws PluginException {
        this.pluginManager = pluginManager;
        this.serviceManager = serviceManager;
        this.objectGraph = objectGraph;
        this.eventBus = eventBus;
        this.identityController = identityController;
        this.filename = metadata.getPluginPath().getFileName().toString();
        this.metaData = metadata;

        try {
            pluginFilesystem = FileSystems.newFileSystem(metadata.getPluginPath(), null);
        } catch (IOException ex) {
            lastError = "Error loading filesystem: " + ex.getMessage();
            throw new PluginException("Plugin " + filename + " failed to load. " + lastError, ex);
        }
        updateClassList();

        if (!myClasses.contains(metadata.getMainClass())) {
            lastError = "main class file (" + metadata.getMainClass() + ") not found in jar.";
            throw new PluginException("Plugin " + filename + " failed to load. " + lastError);
        }

        updateProvides();
        getDefaults();
    }

    public PluginMetaData getMetaData() {
        return metaData;
    }

    public String getFilename() {
        return filename;
    }

    public Plugin getPlugin() {
        return plugin;
    }

    public PluginClassLoader getPluginClassLoader() {
        return pluginClassLoader;
    }

    public String getLastError() {
        return lastError;
    }

    /**
     * Updates the list of known classes within this plugin from the specified resource manager.
     */
    private void updateClassList() throws PluginException {
        myClasses.clear();
        try {
            Files.walkFileTree(pluginFilesystem.getPath("/"), new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
                    if (file.getFileName().toString().endsWith(".class")) {
                        final String classname = file.toAbsolutePath().toString().replace('/',
                                '.');
                        myClasses.add(classname.substring(1, classname.length() - 6));
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException ex) {
            lastError = "Error loading classes: " + ex.getMessage();
            throw new PluginException("Plugin " + filename + " failed to load. " + lastError, ex);
        }
    }

    /**
     * Gets the {@link ObjectGraph} that should be used when loading this plugin.
     *
     * <p>
     * Where this plugin has a parent which returns a non-null graph from
     * {@link Plugin#getObjectGraph()} that object graph will be used unmodified. Otherwise, the
     * global object graph will be used.
     *
     * @return An {@link ObjectGraph} to be used.
     */
    protected ObjectGraph getObjectGraph() {
        if (metaData.getParent() != null) {
            final PluginInfo parentInfo = pluginManager.getPluginInfoByName(metaData.getParent());
            final Plugin parent = parentInfo.getPlugin();
            final ObjectGraph parentGraph = parent.getObjectGraph();
            if (parentGraph != null) {
                return parentGraph;
            }
        }

        return objectGraph;
    }

    /**
     * Get the defaults, formatters and icons for this plugin.
     */
    private void getDefaults() {
        final ConfigProvider defaults = identityController.getAddonSettings();

        LOG.trace("{}: Using domain '{}'", new Object[]{metaData.getName(), getDomain()});

        for (Map.Entry<String, String> entry : metaData.getDefaultSettings().entrySet()) {
            final String key = entry.getKey();
            final String value = entry.getValue();

            defaults.setOption(getDomain(), key, value);
        }

        for (Map.Entry<String, String> entry : metaData.getFormatters().entrySet()) {
            final String key = entry.getKey();
            final String value = entry.getValue();

            defaults.setOption("formatter", key, value);
        }

        for (Map.Entry<String, String> entry : metaData.getIcons().entrySet()) {
            final String key = entry.getKey();
            final String value = entry.getValue();

            defaults.setOption("icon", key, value);
        }
    }

    /**
     * Try get the identities for this plugin. This will unload any identities previously loaded by
     * this plugin.
     */
    private void loadIdentities() {
        if (!Files.exists(pluginFilesystem.getPath("/META-INF/identities/"))) {
            return;
        }
        try {
            unloadIdentities();
            try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(
                    pluginFilesystem.getPath("/META-INF/identities/"))) {
                directoryStream.forEach(this::loadIdentity);
            }
        } catch (final IOException ioe) {
            LOG.warn(USER_ERROR, "Error finding identities in plugin '{}'", metaData.getName(),
                    ioe);
        }
    }

    private void loadIdentity(final Path path) {
        if (!Files.isRegularFile(path)) {
            // Don't try to load folders etc
            return;
        }

        try (final InputStream stream = Files.newInputStream(path)) {
            synchronized (configProviders) {
                final ConfigFileBackedConfigProvider configProvider =
                        new ConfigFileBackedConfigProvider(stream, false);
                identityController.addConfigProvider(configProvider);
                configProviders.add(configProvider);
            }
        } catch (InvalidIdentityFileException ex) {
            LOG.warn(USER_ERROR, "Error with identity file '{}' in plugin '{}'",
                    path.getFileName(), metaData.getName(), ex);
        } catch (IOException ex) {
            LOG.warn(USER_ERROR, "Unable to load identity file '{}' in plugin '{}'",
                    path.getFileName(), metaData.getName(), ex);
        }
    }

    /**
     * Unload any identities loaded by this plugin.
     */
    private void unloadIdentities() {
        synchronized (configProviders) {
            configProviders.forEach(identityController::removeConfigProvider);
            configProviders.clear();
        }
    }

    /**
     * Update provides list.
     */
    private void updateProvides() {
        // Remove us from any existing provides lists.
        synchronized (provides) {
            for (Service service : provides) {
                service.delProvider(this);
            }
            provides.clear();
        }

        // Get services provided by this plugin
        final Collection<String> providesList = metaData.getServices();
        if (providesList != null) {
            for (String item : providesList) {
                final String[] bits = item.split(" ");
                final String name = bits[0];
                final String type = bits.length > 1 ? bits[1] : "misc";

                if (!"any".equalsIgnoreCase(name) && !"export".equalsIgnoreCase(type)) {
                    final Service service = serviceManager.getService(type, name, true);
                    synchronized (provides) {
                        service.addProvider(this);
                        provides.add(service);
                    }
                }
            }
        }

        updateExports();
    }

    /**
     * Called when the plugin is updated using the updater. Reloads metaData and updates the list of
     * files.
     */
    public void pluginUpdated() throws PluginException {
        updateClassList();
        updateMetaData();
        updateProvides();
        getDefaults();
    }

    /**
     * Try to reload the metaData from the plugin.config file. If this fails, the old data will be
     * used still.
     *
     * @return true if metaData was reloaded ok, else false.
     */
    private boolean updateMetaData() {
        metaData.load();

        return !metaData.hasErrors();
    }

    /**
     * Returns a path inside the plugin's jar for the given name.
     *
     * @param   first The path string or initial part of the path string
     * @param   more Additional strings to be joined to form the full path
     *
     * @return The resulting path inside the plugin's jar
     */
    public Path getPath(final String first, final String... more) {
        return pluginFilesystem.getPath(first, more);
    }

    @Override
    public final boolean isActive() {
        return isLoaded();
    }

    @Override
    public void activateServices() {
        loadPlugin();
    }

    @Override
    public String getProviderName() {
        return "Plugin: " + metaData.getFriendlyName() + " ("
                + metaData.getName() + " / " + getFilename() + ')';
    }

    @Override
    public List<Service> getServices() {
        synchronized (provides) {
            return new ArrayList<>(provides);
        }
    }

    /**
     * Is this plugin loaded?
     *
     * @return True if the plugin is currently loaded, false otherwise
     */
    public boolean isLoaded() {
        return plugin != null;
    }

    /**
     * Load any required plugins or services.
     *
     * @return True if all requirements have been satisfied, false otherwise
     */
    protected boolean loadRequirements() {
        return loadRequiredPlugins() && loadRequiredServices();
    }

    /**
     * Attempts to load all services that are required by this plugin.
     *
     * @return True iff all required services were found and satisfied
     */
    protected boolean loadRequiredServices() {
        for (String serviceInfo : metaData.getRequiredServices()) {
            final String[] parts = serviceInfo.split(" ", 2);

            if ("any".equals(parts[0])) {
                Service best = null;
                boolean found = false;

                for (Service service : serviceManager.getServicesByType(parts[1])) {
                    if (service.isActive()) {
                        found = true;
                        break;
                    }

                    best = service;
                }

                if (!found && best != null) {
                    found = best.activate();
                }

                if (!found) {
                    return false;
                }
            } else {
                final Service service = serviceManager.getService(parts[1], parts[0]);

                if (service == null || !service.activate()) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * Attempts to load all plugins that are required by this plugin.
     *
     * @return True if all required plugins were found and loaded
     */
    protected boolean loadRequiredPlugins() {
        final String required = metaData.getRequirements().get("plugins");

        if (required != null) {
            for (String pluginName : required.split(",")) {
                final String[] data = pluginName.split(":");
                if (!data[0].trim().isEmpty() && !loadRequiredPlugin(data[0])) {
                    return false;
                }
            }
        }

        return metaData.getParent() == null || loadRequiredPlugin(metaData.getParent());
    }

    /**
     * Attempts to load the specified required plugin.
     *
     * @param name The name of the plugin to be loaded
     *
     * @return True if the plugin was found and loaded, false otherwise
     */
    protected boolean loadRequiredPlugin(final String name) {
        LOG.info("Loading required plugin '{}' for plugin {}",
                new Object[]{name, metaData.getName()});

        final PluginInfo pi = pluginManager.getPluginInfoByName(name);

        if (pi == null) {
            return false;
        }

        pi.loadPlugin();
        return true;
    }

    /**
     * Load the plugin files.
     */
    public void loadPlugin() {
        if (isLoaded() || isLoading) {
            lastError = "Not Loading: (" + isLoaded() + "||" + isLoading + ')';
            return;
        }

        updateProvides();
        isLoading = true;

        if (!loadRequirements()) {
            isLoading = false;
            lastError = "Unable to satisfy dependencies for " + metaData.getName();
            return;
        }

        loadIdentities();
        loadMainClass();

        if (isLoaded()) {
            //TODO plugin loading shouldn't be done from here, event bus shouldn't be here.
            eventBus.publishAsync(new PluginLoadedEvent(this));
        }

        isLoading = false;
    }

    /**
     * Add the given Plugin as a child of this plugin.
     *
     * @param child Child to add
     */
    public void addChild(final PluginInfo child) {
        children.add(child);
    }

    /**
     * Remove the given Plugin as a child of this plugin.
     *
     * @param child Child to remove
     */
    public void delChild(final PluginInfo child) {
        children.remove(child);
    }

    /**
     * Returns a list of this plugin's children.
     *
     * @return List of child plugins
     */
    public List<PluginInfo> getChildren() {
        return Collections.unmodifiableList(children);
    }

    /**
     * Initialises this plugin's classloader.
     */
    private void initialiseClassLoader() {
        if (pluginClassLoader == null) {
            final PluginClassLoader[] loaders = new PluginClassLoader[metaData.getParent() == null
                    ? 0 : 1];

            if (metaData.getParent() != null) {
                final String parentName = metaData.getParent();
                final PluginInfo parent = pluginManager.getPluginInfoByName(parentName);
                parent.addChild(this);
                loaders[0] = parent.getPluginClassLoader();

                if (loaders[0] == null) {
                    // Not loaded? Try again...

                    parent.loadPlugin();
                    loaders[0] = parent.getPluginClassLoader();

                    if (loaders[0] == null) {
                        lastError = "Unable to get classloader from required parent '" + parentName
                                + "' for " + metaData.getName();
                        return;
                    }
                }
            }

            pluginClassLoader = new PluginClassLoader(this, pluginManager.getGlobalClassLoader(),
                    loaders);
        }
    }

    /**
     * Load the main class
     */
    private void loadMainClass() {
        final String classname = metaData.getMainClass();
        try {
            initialiseClassLoader();

            // Don't reload a class if its already loaded.
            if (pluginClassLoader.isClassLoaded(classname, true)) {
                lastError = "Classloader says we are already loaded.";
                return;
            }

            final Class<?> clazz = pluginClassLoader.loadClass(classname);
            if (clazz == null) {
                lastError = "Class '" + classname + "' was not able to load.";
                return;
            }

            createMainClass(clazz);
        } catch (ClassNotFoundException cnfe) {
            lastError = "Class not found ('" + filename + ':' + classname + ':'
                    + classname.equals(metaData.getMainClass()) + "') - "
                    + cnfe.getMessage();
            LOG.info(USER_ERROR, lastError, cnfe);
        } catch (NoClassDefFoundError ncdf) {
            lastError = "Unable to instantiate plugin ('" + filename + ':'
                    + classname + ':'
                    + classname.equals(metaData.getMainClass())
                    + "') - Unable to find class: " + ncdf.getMessage();
            LOG.info(USER_ERROR, lastError, ncdf);
        } catch (VerifyError ve) {
            lastError = "Unable to instantiate plugin ('" + filename + ':'
                    + classname + ':'
                    + classname.equals(metaData.getMainClass())
                    + "') - Incompatible: " + ve.getMessage();
            LOG.info(USER_ERROR, lastError, ve);
        } catch (IllegalArgumentException | ReflectiveOperationException ex) {
            lastError = "Unable to instantiate class for plugin " + metaData.getName()
                    + ": " + classname;
            LOG.info(APP_ERROR, lastError, ex);
        }
    }

    private void createMainClass(final Class<?> clazz) throws InstantiationException,
            IllegalAccessException {
        final Object temp = clazz.newInstance();
        if (temp instanceof Plugin) {
            final ValidationResponse prerequisites = ((Plugin) temp).checkPrerequisites();
            if (prerequisites.isFailure()) {
                lastError = "Prerequisites for plugin not met. ('"
                        + filename + ':' + metaData.getMainClass()
                        + "' -> '" + prerequisites.getFailureReason() + "') ";
                LOG.info(USER_ERROR, lastError);
            } else {
                initialisePlugin((Plugin) temp);
            }
        }
    }

    private void initialisePlugin(final Plugin temp) {
        plugin = temp;
        try {
            plugin.load(this, getObjectGraph());
            plugin.onLoad();
        } catch (LinkageError | Exception e) {
            lastError = "Error in onLoad for " + metaData.getName() + ':' + e.getMessage();
            LOG.warn(APP_ERROR, lastError, e);
            unloadPlugin();
        }
    }

    /**
     * Gets the configuration domain that should be used by this plugin.
     *
     * @return The configuration domain to use.
     */
    public String getDomain() {
        return "plugin-" + metaData.getName();
    }

    /**
     * Unload the plugin if possible.
     */
    public void unloadPlugin() {
        unloadPlugin(false);
    }

    /**
     * Can this plugin be unloaded? Will return false if: - The plugin is persistent (all its
     * classes are loaded into the global class loader) - The plugin isn't currently loaded - The
     * metadata key "unloadable" is set to false, no or 0
     *
     * @return true if plugin can be unloaded
     */
    public boolean isUnloadable() {
        return !isPersistent() && isLoaded() && metaData.isUnloadable();
    }

    /**
     * Unload the plugin if possible.
     *
     * @param parentUnloading is our parent already unloading? (if so, don't call delChild)
     */
    private void unloadPlugin(final boolean parentUnloading) {
        if (isUnloadable()) {
            // Unload all children
            for (PluginInfo child : children) {
                child.unloadPlugin(true);
            }

            // Delete ourself as a child of our parent.
            if (!parentUnloading && metaData.getParent() != null) {
                final PluginInfo pi = pluginManager.getPluginInfoByName(metaData.getParent());
                if (pi != null) {
                    pi.delChild(this);
                }
            }

            // Now unload ourself
            try {
                plugin.onUnload();
            } catch (Exception | LinkageError e) {
                lastError = "Error in onUnload for " + metaData.getName()
                        + ':' + e + " - " + e.getMessage();
                LOG.warn(APP_ERROR, lastError, e);
            }

            //TODO plugin unloading shouldn't be done from here, event bus shouldn't be here.
            eventBus.publishAsync(new PluginUnloadedEvent(this));
            synchronized (provides) {
                for (Service service : provides) {
                    service.delProvider(this);
                }
                provides.clear();
            }
        }
        unloadIdentities();
        plugin = null;
        pluginClassLoader = null;
    }

    /**
     * Get the list of Classes
     *
     * @return Classes this plugin has
     */
    public List<String> getClassList() {
        return Collections.unmodifiableList(myClasses);
    }

    /**
     * Is this a persistent plugin?
     *
     * @return true if persistent, else false
     */
    public boolean isPersistent() {
        return metaData.getPersistentClasses().contains("*");
    }

    /**
     * Get a list of all persistent classes in this plugin
     *
     * @return List of all persistent classes in this plugin
     */
    public Collection<String> getPersistentClasses() {
        if (isPersistent()) {
            return getClassList();
        } else {
            return metaData.getPersistentClasses();
        }
    }

    /**
     * Is this a persistent class?
     *
     * @param classname class to check persistence of
     *
     * @return true if file (or whole plugin) is persistent, else false
     */
    public boolean isPersistent(final String classname) {
        return isPersistent() || metaData.getPersistentClasses().contains(classname);
    }

    /**
     * String Representation of this plugin
     *
     * @return String Representation of this plugin
     */
    @Override
    public String toString() {
        return metaData.getFriendlyName() + " - " + filename;
    }

    /**
     * Update exports list.
     */
    private void updateExports() {
        exports.clear();

        // Get exports provided by this plugin
        final Collection<String> exportsList = metaData.getExports();
        for (String item : exportsList) {
            final String[] bits = item.split(" ");
            if (bits.length > 2) {
                final String methodName = bits[0];
                final String methodClass = bits[2];
                final String serviceName = bits.length > 4 ? bits[4] : bits[0];

                // Add a provides for this
                final Service service = serviceManager.getService("export", serviceName, true);
                synchronized (provides) {
                    service.addProvider(this);
                    provides.add(service);
                }
                // Add is as an export
                exports.put(serviceName, new ExportInfo(methodName, methodClass, this));
            }
        }
    }

    @Override
    public ExportedService getExportedService(final String name) {
        if (exports.containsKey(name)) {
            return exports.get(name).getExportedService();
        } else {
            return null;
        }
    }

    /**
     * Does this plugin export the specified service?
     *
     * @param name Name of the service to check
     *
     * @return true iff the plugin exports the service
     */
    public boolean hasExportedService(final String name) {
        return exports.containsKey(name);
    }

}


text:
import Ember from 'ember';

export default Ember.Mixin.create({
  validations: null,

  validateInclusion: function() {
    var validations = this.get('validations');

    if (!validations) {
      var msg = 'You must set `validations` property to use '+
                '`InclusionValidation` mixin';

      throw new Error(msg);
    }

    for (var k in validations) {
      if (!validations.hasOwnProperty(k)) {
        continue;
      }

      var v = this.get(k);

      if (!validations[k].in.contains(v)) {
        throw new Error('Error: %@ is not a valid value for %@.'.fmt(v, k));
      }
    }
  }.on('init')
});


text:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="ubic.gemma.model.analysis.expression.diff.ContrastResult" table="CONTRAST_RESULT"
    >
        <cache usage="read-write"/>
        <id name="id" type="java.lang.Long" unsaved-value="null">
            <column name="ID" sql-type="BIGINT"/>
            <generator class="native">
            </generator>
        </id>
        <property name="pvalue" type="java.lang.Double">
            <column name="PVALUE" not-null="false" unique="false" sql-type="DOUBLE"/>
        </property>
        <property name="tstat" type="java.lang.Double">
            <column name="TSTAT" not-null="false" unique="false" sql-type="DOUBLE"/>
        </property>
        <property name="coefficient" type="java.lang.Double">
            <column name="COEFFICIENT" not-null="false" unique="false" sql-type="DOUBLE"/>
        </property>
        <property name="logFoldChange" type="java.lang.Double">
            <column name="LOG_FOLD_CHANGE" not-null="false" unique="false" sql-type="DOUBLE"/>
        </property>
        <many-to-one name="factorValue" class="ubic.gemma.model.expression.experiment.FactorValue" lazy="proxy"
                     fetch="select">
            <column name="FACTOR_VALUE_FK" not-null="false" sql-type="BIGINT"/>
        </many-to-one>
        <many-to-one name="secondFactorValue" class="ubic.gemma.model.expression.experiment.FactorValue" lazy="proxy"
                     fetch="select">
            <column name="SECOND_FACTOR_VALUE_FK" not-null="false" sql-type="BIGINT"/>
        </many-to-one>
    </class>
</hibernate-mapping>


text:
const winston = require('winston');
const logzioTransport = require('winston-logzio');
const { logzIOToken, logzIOHost, environment, logLevel } = require('./constants');
const { combine, timestamp, label, printf } = winston.format;

const consoleFormat = printf(info => {
  const {timestamp, label, level, message, ...rest} = info;
  const extraInformation = Object.keys(rest).length > 0 ? `\n${JSON.stringify(rest, null, 2)}` : '';
  return `${timestamp} [${label}] ${level}: ${message} ${extraInformation}`;
})

const getLogger = theLabel => {
  if (!label) {
    throw new Error('No label defined for the Logger');
  }

  const logger = winston.createLogger({
    level: logLevel || (environment === 'production' ? 'error' : 'debug'),
    format: winston.format.json(),
  });

  if (environment === 'production') {
    logger.add(new logzioTransport({
      token: logzIOToken,
      host: logzIOHost,
    }));
  } else {
    logger.add(new winston.transports.Console({
      format: combine(
        label({ label: theLabel }),
        timestamp(),
        consoleFormat,
      ),
    }));
  }

  return logger;
}

module.exports = getLogger;


text:
CREATE TABLE IF NOT EXISTS guilds (guildid text, PRIMARY KEY (guildid));

CREATE TABLE IF NOT EXISTS tags (mid text,
  name varchar(36),
  content text,
  guildid text,
  authorid text,
  PRIMARY KEY (name, guildid));

CREATE TABLE IF NOT EXISTS colors (name varchar(36),
  color int,
  roleid text,
  guildid text,
  PRIMARY KEY (name, guildid));

CREATE TABLE IF NOT EXISTS creacts (name varchar(36),
  resp text,
  guildid text,
  PRIMARY KEY (name, guildid));

CREATE TABLE IF NOT EXISTS donators (roleid text UNIQUE, userid text, guildid text, PRIMARY KEY (userid, guildid));

CREATE TABLE IF NOT EXISTS highlights (name text, userid text, guildid text, PRIMARY KEY (name, userid, guildid));

CREATE TABLE IF NOT EXISTS reminders (userid text, content text, time timestamp);

CREATE TABLE IF NOT EXISTS configs (key text, value text, PRIMARY KEY (key));

CREATE TABLE IF NOT EXISTS starboard (mid TEXT NOT NULL,
  stars INT NOT NULL DEFAULT 1,
  posted TEXT NOT NULL,
  guildid TEXT NOT NULL,
  PRIMARY KEY (mid));

CREATE TABLE IF NOT EXISTS roles (roleid text, rolename text, guildid text, PRIMARY KEY (roleid));

CREATE TABLE IF NOT EXISTS perms (guildid text,
  command text,
  targettype INT,
  target text,
  staet bool,
  description text,
  PRIMARY KEY (guildid, command, target));

CREATE TABLE IF NOT EXISTS logchannels (guildid text, channelid text, PRIMARY KEY (guildid));

CREATE TABLE IF NOT EXISTS currency (userid text, amount numeric NOT NULL CHECK (amount >= 0), PRIMARY KEY (userid));


text:
DROP TABLE IF EXISTS model_factory.model_varimp;
DROP TABLE IF EXISTS model_factory.model_summary;
DROP TABLE IF EXISTS model_factory.model_scores;
DROP TABLE IF EXISTS model_factory.model_backtesting;
DROP TABLE IF EXISTS model_factory.model_test_results;
DROP TABLE IF EXISTS model_factory.metadata_table;
DROP TABLE IF EXISTS model_factory.run_history;
DROP TABLE IF EXISTS model_factory.model_overview;

DROP SCHEMA IF EXISTS model_factory;

CREATE SCHEMA model_factory;

/*Stores Variable Importance in a run of:
 * Random foest models
 * Genereal Liniur Models
 * Logistic regression models
*/
DROP TABLE IF EXISTS model_factory.model_varimp;

CREATE TABLE model_factory.model_varimp
(
	session_id		VARCHAR(50),
	variable		VARCHAR(255),
	importance		NUMERIC(28,6),
	std				NUMERIC(28,6),
	coefficients	NUMERIC(28,6)
)
--DISTRIBUTE BY HASH (variable)
;

/*Stores that summary od input data of the model for every run
 *
*/
DROP TABLE IF EXISTS model_factory.model_summary;

CREATE TABLE model_factory.model_summary
(
	session_id		VARCHAR(50),
	variable		VARCHAR(255),
	mean			NUMERIC(28,6),
	sd				NUMERIC(28,6),
	median			NUMERIC(28,6),
	min				NUMERIC(28,6),
	max				NUMERIC(28,6),
	n				INTEGER,
	n_na			INTEGER
)
--DISTRIBUTE BY HASH (variable)
;

/*Stores the output scores of regression models
 *
*/
DROP TABLE IF EXISTS model_factory.model_scores;

CREATE TABLE model_factory.model_scores
(
	session_id		VARCHAR(50),
	id				VARCHAR(255),
	scores			NUMERIC(28,6),
	scores_class	VARCHAR(255)
)
--DISTRIBUTE BY HASH (id)
;

/*Stores results from back testing time series models
 *
*/

DROP TABLE IF EXISTS model_factory.model_backtesting;

CREATE TABLE model_factory.model_backtesting
(
	session_id		VARCHAR(50),
	predicted_value	NUMERIC(28,6),
	actual_value	NUMERIC(28,6),
	period			TIMESTAMP
)
--DISTRIBUTE BY HASH (predicted_value)
;

/*Stores results from back testing non-time series models
 * The data should be interprated as a ranked list order by scores in desending order
 * and the row represents a threshold , or cut off point.
 *
 * true_positives is the number of 1 lables are above the current row
 * false_positives is the number of 0 lables are above the current row
 * true_negatives is the number of 0 labeles below this row
 * false_negatives is teh number of 1 lables are below this row
*/

DROP TABLE IF EXISTS model_factory.model_test_results;

CREATE TABLE model_factory.model_test_results
(
	session_id		VARCHAR(50),
	score			NUMERIC(28,6),
	"label"			NUMERIC(28,6),
	population		NUMERIC(28,6),
	target_population NUMERIC(28,6),
	true_positives	INTEGER,
	false_positives	INTEGER,
	true_negatives	INTEGER,
	false_negatives	INTEGER
)
--DISTRIBUTE BY HASH (score)
;

/*Stores key value data assosiated with a run
 * The group column is to allow data to be separated into multple groups when displying
*/

DROP TABLE IF EXISTS model_factory.metadata_table;

CREATE TABLE model_factory.metadata_table
(
	session_id		VARCHAR(50),
	session_key		VARCHAR(255),
	session_value	VARCHAR(255),
	session_group	VARCHAR(50)
)
--DISTRIBUTE BY HASH (session_key)
;

/*Stores a record for every run
 * end_time is only filled if the run was complited
 * last_exported is only fill if and when the model's scores where exported to TD
*/
DROP TABLE IF EXISTS model_factory.run_history;

CREATE TABLE model_factory.run_history
(
	session_id		VARCHAR(50),
	user_id			VARCHAR(50),
	model_id		VARCHAR(50),
	start_time		TIMESTAMP,
	end_time		TIMESTAMP,
	last_exported	TIMESTAMP
)
--DISTRIBUTE BY HASH (session_id)
;

/*Stores a description data about every model
 * if experimental = 1 model scores should be exported to TD under teh experimental model type
 * if producton = 1 model scores should be exported to TD under teh producton model type, this over rules the experimental flag
 * if both experimental <> 1 AND producton <> 1 the model scores should not be exported.
*/
DROP TABLE IF EXISTS model_factory.model_overview;

CREATE TABLE model_factory.model_overview
(
	model_id		VARCHAR(50),
	model_description TEXT,
	score_id_type	VARCHAR(255),
	threshold_value		VARCHAR(255),
	threshold_type	VARCHAR(255),
	experimental NUMERIC(1,0),
	production NUMERIC(1,0)
)
--DISTRIBUTE BY HASH (model_id)
;

/*Stores the status os a column in the input data tables
* Is used to update multiple models at ones when introducing a new column
*/
DROP TABLE IF EXISTS model_factory.input_data_valid_columns;

CREATE TABLE model_factory.input_data_valid_columns
(
  schema_name	VARCHAR(63),
  table_name  VARCHAR(63),
  column_name VARCHAR(63),
  valid_column  INT,
  test_column   INT,
  column_info   TEXT
)
--DISTRIBUTE BY HASH (column_name)
;

/*Stores RDS binary data of models
*/
DROP TABLE IF EXISTS model_factory.model_store;

CREATE  TABLE model_factory.model_store (
  session_id		VARCHAR(50),
  model			    BYTEA
);


text:
<?php

        session_start();
        $formatos = array('.jpg' , '.png' , '.docx' , '.xls' , '.pdf','.pptx');
        $directorio='archivos';
        $contArchivos=0;

        if (isset($_POST['boton'])){
            $nombrearchivo = $_FILES['archivo']['name'];
            $nombreaTmpArchivo = $_FILES['archivo']['tmp_name'];
            $ext = substr($nombrearchivo, strrpos($nombrearchivo, '.'));
            $nombreoriginal =  $_SESSION["var_sesion"]."/".mysql_real_escape_string($_POST['nombre']) . $ext;
            if(in_array($ext,$formatos)){
                if(move_uploaded_file($nombreaTmpArchivo,"archivos/$nombreoriginal" )){

                    echo "Felicitaciones, $nombreoriginal subido exitosamente" ;

                }else{
                    echo "ocurrio un error";
                }
            }else{
                    echo "archivo no permitido";

                }
            }
           header('location:cargar.php');
?>


text:
package quasar.physical.sparkcore.fs

import slamdata.Predef.{Eq => _, _}
import quasar.{datetime, Data, DateTimeInterval, TemporalPart}
import quasar.Planner._
import quasar.common.PrimaryType
import quasar.fp.ski._
import quasar.qscript._, MapFuncsCore._
import quasar.std.{DateLib, StringLib}

import java.time.{LocalTime=>JLocalTime}, java.time.ZoneOffset.UTC
import java.time.{OffsetDateTime=>JOffsetDateTime, Instant}
import scala.math
import scala.util.matching.Regex

import matryoshka._
import matryoshka.data.free._
import matryoshka.implicits._
import matryoshka.patterns._
import scalaz.{Divide => _,Split => _, _}, Scalaz._
import quasar.DataDateTimeExtractors._

object CoreMap extends Serializable {

  private val undefined = Data.NA

  def changeFreeMap[T[_[_]]: BirecursiveT](f: FreeMap[T])
      : PlannerError \/ (Data => Data) =
    f.cataM(interpretM(κ(ι[Data].right[PlannerError]), change[T, Data]))

  def changeJoinFunc[T[_[_]]: BirecursiveT](f: JoinFunc[T])
      : PlannerError \/ ((Data, Data) => Data) =
    f.cataM(interpretM[PlannerError \/ ?, MapFunc[T, ?], JoinSide, ((Data, Data)) => Data](
      (js: JoinSide) => (js match {
        case LeftSide  => (_: (Data, Data))._1
        case RightSide => (_: (Data, Data))._2
      }).right,
      change[T, (Data, Data)])).map(f => (l: Data, r: Data) => f((l, r)))

  def changeReduceFunc[T[_[_]]: BirecursiveT](f: Free[MapFunc[T, ?], ReduceIndex])
      : PlannerError \/ ((List[Data], List[Data]) => Data) =
    f.cataM(interpretM(
      _.idx.fold[((List[Data], List[Data])) => Data](i => _._1(i), i => _._2(i)).right,
      change[T, (List[Data], List[Data])])).map(f => (l: List[Data], r: List[Data]) => f((l, r)))

  def change[T[_[_]]: BirecursiveT, A]
      : AlgebraM[PlannerError \/ ?, MapFunc[T, ?], A => Data] =
    _.run.fold(changeCore, changeDerived)

  def changeDerived[T[_[_]]: BirecursiveT, A]
      : AlgebraM[PlannerError \/ ?, MapFuncDerived[T, ?], A => Data] =
    ExpandMapFunc.expand(changeCore, κ(None))

  def changeCore[T[_[_]]: RecursiveT, A]
      : AlgebraM[PlannerError \/ ?, MapFuncCore[T, ?], A => Data] = {
    case Constant(f) => κ(f.cata(Data.fromEJson)).right
    case Undefined() => κ(undefined).right
    case JoinSideName(n) => UnexpectedJoinSide(n).left

    case Length(f) => (f >>> {
      case Data.Str(v) => Data.Int(v.length)
      case Data.Arr(v) => Data.Int(v.size)
      case _ => undefined
    }).right

    case OffsetDate(f) => (f >>> {
      case Data.Str(v) => DateLib.parseOffsetDate(v).getOrElse(undefined)
      case _ => undefined
    }).right
    case OffsetTime(f) => (f >>> {
      case Data.Str(v) => DateLib.parseOffsetTime(v).getOrElse(undefined)
      case _ => undefined
    }).right
    case OffsetDateTime(f) => (f >>> {
      case Data.Str(v) => DateLib.parseOffsetDateTime(v).getOrElse(undefined)
      case _ => undefined
    }).right
    case LocalDate(f) => (f >>> {
      case Data.Str(v) => DateLib.parseLocalDate(v).getOrElse(undefined)
      case _ => undefined
    }).right
    case LocalTime(f) => (f >>> {
      case Data.Str(v) => DateLib.parseLocalTime(v).getOrElse(undefined)
      case _ => undefined
    }).right
    case LocalDateTime(f) => (f >>> {
      case Data.Str(v) => DateLib.parseLocalDateTime(v).getOrElse(undefined)
      case _ => undefined
    }).right
    case Interval(f) => (f >>> {
      case Data.Str(v) => DateLib.parseInterval(v).getOrElse(undefined)
      case _ => undefined
    }).right
    case StartOfDay(f) => (f >>> {
      case CanAddTime(g) => g(JLocalTime.MIN)
      case _ => undefined
    }).right
    case TemporalTrunc(p, f) => (f >>> (temporalTrunc(p, _))).right
    case TimeOfDay(f) => (f >>> {
      case Data.LocalDateTime(v) => Data.LocalTime(v.toLocalTime)
      case Data.OffsetDateTime(v) => Data.OffsetTime(v.toOffsetTime)
      case _ => undefined
    }).right
    case ToTimestamp(f) => (f >>> {
      case Data.Int(epoch) => Data.OffsetDateTime(Instant.ofEpochMilli(epoch.toLong).atOffset(UTC))
      case _ => undefined
    }).right
    case ExtractCentury(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractCentury(i.pos))
      case _ => undefined
    }).right
    case ExtractDayOfMonth(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractDayOfMonth(i.pos))
      case _ => undefined
    }).right
    case ExtractDecade(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractDecade(i.pos))
      case _ => undefined
    }).right
    case ExtractDayOfWeek(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractDayOfWeek(i.pos))
      case _ => undefined
    }).right
    case ExtractDayOfYear(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractDayOfYear(i.pos))
      case _ => undefined
    }).right
    case ExtractEpoch(f) => (f >>> {
      case Data.OffsetDateTime(v) => Data.Dec(v.toEpochSecond())
      case _ => undefined
    }).right
    case ExtractHour(f) => (f >>> {
      case CanLensTime(i) => Data.Int(datetime.extractHour(i.pos))
      case _ => undefined
    }).right
    case ExtractIsoDayOfWeek(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractIsoDayOfWeek(i.pos))
      case _ => undefined
    }).right
    case ExtractIsoYear(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractIsoYear(i.pos))
      case _ => undefined
    }).right
    case ExtractMicrosecond(f) => (f >>> {
      case CanLensTime(i) => Data.Int(datetime.extractMicrosecond(i.pos))
      case _ => undefined
    }).right
    case ExtractMillennium(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractMillennium(i.pos))
      case _ => undefined
    }).right
    case ExtractMillisecond(f) => (f >>> {
      case CanLensTime(i) => Data.Int(datetime.extractMillisecond(i.pos))
      case _ => undefined
    }).right
    case ExtractMinute(f) => (f >>> {
      case CanLensTime(i) => Data.Int(datetime.extractMinute(i.pos))
      case _ => undefined
    }).right
    case ExtractMonth(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractMonth(i.pos))
      case _ => undefined
    }).right
    case ExtractQuarter(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractQuarter(i.pos))
      case _ => undefined
    }).right
    case ExtractSecond(f) => (f >>> {
      case CanLensTime(i) =>
        Data.Dec(datetime.extractSecond(i.pos))
      case _ => undefined
    }).right
    case ExtractWeek(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractWeek(i.pos))
      case _ => undefined
    }).right
    case ExtractYear(f) => (f >>> {
      case CanLensDate(i) => Data.Int(datetime.extractYear(i.pos))
      case _ => undefined
    }).right
    case ExtractTimeZone(f) => (f >>> {
      case CanLensTimeZone(i) => Data.Int(datetime.extractTimeZone(i.pos))
      case _ => undefined
    }).right
    case ExtractTimeZoneMinute(f) => (f >>> {
      case CanLensTimeZone(i) => Data.Int(datetime.extractTimeZoneMinute(i.pos))
      case _ => undefined
    }).right
    case ExtractTimeZoneHour(f) => (f >>> {
      case CanLensTimeZone(i) => Data.Int(datetime.extractTimeZoneHour(i.pos))
      case _ => undefined
    }).right

    case Now() => κ(Data.OffsetDateTime(JOffsetDateTime.now())).right

    case Negate(f) => (f >>> {
      case Data.Int(v) => Data.Int(-v)
      case Data.Dec(v) => Data.Dec(-v)
      case Data.Interval(v) => Data.Interval(v.multiply(-1))
      case _ => undefined
    }).right
    case Add(f1, f2) => ((x: A) => add(f1(x), f2(x))).right
    case Multiply(f1, f2) => ((x: A) => multiply(f1(x), f2(x))).right
    case Subtract(f1, f2) => ((x: A) => subtract(f1(x), f2(x))).right
    case Divide(f1, f2) => ((x: A) => divide(f1(x), f2(x))).right
    case Modulo(f1, f2) => ((x: A) => modulo(f1(x), f2(x))).right
    case Power(f1, f2) => ((x: A) => power(f1(x), f2(x))).right

    case Not(f) => (f >>> {
      case Data.Bool(b) => Data.Bool(!b)
      case _ => undefined
    }).right
    case Eq(f1, f2) => ((x: A) => Data.Bool(f1(x) === f2(x))).right
    case Neq(f1, f2) => ((x: A) => Data.Bool(f1(x) =/= f2(x))).right
    case Lt(f1, f2) => ((x: A) => lt(f1(x), f2(x))).right
    case Lte(f1, f2) => ((x: A) => lte(f1(x), f2(x))).right
    case Gt(f1, f2) => ((x: A) => gt(f1(x), f2(x))).right
    case Gte(f1, f2) => ((x: A) => gte(f1(x), f2(x))).right
    case IfUndefined(f1, f2) => ((x: A) => f1(x) match {
      case Data.NA => f2(x)
      case d => d
    }).right
    case And(f1, f2) => ((x: A) => (f1(x), f2(x)) match {
      case (Data.Bool(a), Data.Bool(b)) => Data.Bool(a && b)
      case _ => undefined
    }).right
    case Or(f1, f2) => ((x: A) => (f1(x), f2(x)) match {
      case (Data.Bool(a), Data.Bool(b)) => Data.Bool(a || b)
      case (Data.NA, b) => b
      case (a, Data.NA) => a
      case _ => undefined
    }).right
    case Between(f1, f2, f3) => ((x: A) => between(f1(x), f2(x), f3(x))).right
    case Cond(fCond, fThen, fElse) => ((x: A) => fCond(x) match {
      case Data.Bool(true) => fThen(x)
      case Data.Bool(false) => fElse(x)
      case _ => undefined
    }).right

    case Within(f1, f2) => ((x: A) => (f1(x), f2(x)) match {
      case (d, Data.Arr(list)) => Data.Bool(list.contains(d))
      case _ => undefined
    }).right

    case Lower(f) => (f >>> {
      case Data.Str(a) => Data.Str(a.toLowerCase())
      case _ => undefined
    }).right
    case Upper(f) => (f >>> {
      case Data.Str(a) => Data.Str(a.toUpperCase())
      case _ => undefined
    }).right
    case Bool(f) => (f >>> {
      case Data.Str("true") => Data.Bool(true)
      case Data.Str("false") => Data.Bool(false)
      case _ => undefined
    }).right
    case Integer(f) => (f >>> {
      case Data.Str(a) => \/.fromTryCatchNonFatal(Data.Int(BigInt(a))).fold(κ(Data.NA), ι)
      case _ => undefined
    }).right
    case Decimal(f) => (f >>> {
      case Data.Str(a) => \/.fromTryCatchNonFatal(Data.Dec(BigDecimal(a))).fold(κ(Data.NA), ι)
      case _ => undefined
    }).right
    case Null(f) => (f >>> {
      case Data.Str("null") => Data.Null
      case _ => undefined
    }).right
    case ToString(f) => (f >>> toStringFunc).right
    case Search(fStr, fPattern, fInsen) =>
      ((x: A) => search(fStr(x), fPattern(x), fInsen(x))).right
    case Substring(fStr, fFrom, fCount) =>
      ((x: A) => substring(fStr(x), fFrom(x), fCount(x))).right
     case Split(fStr, fDelim) =>
       ((x: A) => split(fStr(x), fDelim(x))).right
    case MakeArray(f) => (f >>> ((x: Data) => Data.Arr(List(x)))).right
    case MakeMap(fK, fV) => ((x: A) => (fK(x), fV(x)) match {
      case (Data.Str(k), v) => Data.Obj(ListMap(k -> v))
      case _ => undefined
    }).right
    case ConcatArrays(f1, f2) => ((x: A) => (f1(x), f2(x)) match {
      case (Data.Arr(l1), Data.Arr(l2)) => Data.Arr(l1 ++ l2)
      case (Data.Str(s1), Data.Str(s2)) => Data.Str(s1 ++ s2)
      case _ => undefined
    }).right
    case ConcatMaps(f1, f2) => ((x: A) => (f1(x), f2(x)) match {
      case (Data.Obj(m1), Data.Obj(m2)) => Data.Obj(m1 ++ m2)
      case _ => undefined
    }).right
    case ProjectIndex(f1, f2) => ((x: A) => (f1(x), f2(x)) match {
      case (Data.Arr(list), Data.Int(index)) =>
        if(index >= 0 && index < list.size) list(index.toInt) else undefined
      case _ => undefined
    }).right
    case ProjectKey(fSrc, fKey) => ((x: A) => (fSrc(x), fKey(x)) match {
      case (Data.Obj(m), Data.Str(key)) if m.isDefinedAt(key) => m(key)
      case _ => undefined
    }).right
    case DeleteKey(fSrc, fField) =>  ((x: A) => (fSrc(x), fField(x)) match {
      case (Data.Obj(m), Data.Str(key)) if m.isDefinedAt(key) => Data.Obj(m - key)
      case (obj @ Data.Obj(_), _) => obj
      case _ => undefined
    }).right
    case Range(fFrom, fTo) => ((x: A) => (fFrom(x), fTo(x)) match {
      case (Data.Int(a), Data.Int(b)) if(a <= b) => Data.Arr((a to b).map(Data.Int(_)).toList)
    }).right
    case Guard(f1, fPattern, f2, ff3) => f2.right
    case TypeOf(f) =>
      (f >>> ((d: Data) => d.dataType.toPrimaryType.fold(Data.NA : Data)(p => Data.Str(PrimaryType.name(p))))).right[PlannerError]
    case _ => InternalError.fromMsg("not implemented").left
  }

  private def add(d1: Data, d2: Data): Data = (d1, d2) match {
    case (Data.Int(a), Data.Int(b)) => Data.Int(a + b)
    case (Data.Int(a), Data.Dec(b)) => Data.Dec(BigDecimal(a) + b)
    case (Data.Dec(a), Data.Int(b)) => Data.Dec(a + BigDecimal(b))
    case (Data.Dec(a), Data.Dec(b)) => Data.Dec(a + b)
    case (Data.Interval(a), Data.Interval(b)) => Data.Interval(a.plus(b))
    case (Data.Interval(b), CanLensDateTime(a)) => a.peeks(b.addTo)
    case (Data.Interval(DateTimeInterval.DateLike(b)), CanLensDate(a)) => a.peeks(_.plus(b))
    case (Data.Interval(DateTimeInterval.TimeLike(b)), CanLensTime(a)) => a.peeks(_.plus(b))
    case (CanLensDateTime(a), Data.Interval(b)) => a.peeks(b.addTo)
    case (CanLensDate(a), Data.Interval(DateTimeInterval.DateLike(b))) => a.peeks(_.plus(b))
    case (CanLensTime(a), Data.Interval(DateTimeInterval.TimeLike(b))) => a.peeks(_.plus(b))
    case _ => undefined
  }

  private def subtract(d1: Data, d2: Data): Data = (d1, d2) match {
    case (Data.Dec(a), Data.Dec(b)) => Data.Dec(a - b)
    case (Data.Int(a), Data.Dec(b)) => Data.Dec(BigDecimal(a) - b)
    case (Data.Dec(a), Data.Int(b)) => Data.Dec(a - BigDecimal(b))
    case (Data.Int(a), Data.Int(b)) => Data.Int(a - b)
    case (Data.Interval(a), Data.Interval(b)) => Data.Interval(a.minus(b))
    case (CanLensDateTime(a), Data.Interval(b)) => a.peeks(b.subtractFrom)
    case (CanLensDate(a), Data.Interval(DateTimeInterval.DateLike(b))) => a.peeks(_.minus(b))
    case (CanLensTime(a), Data.Interval(DateTimeInterval.TimeLike(b))) => a.peeks(_.minus(b))
    case _ => undefined
  }

  private def multiply(d1: Data, d2: Data): Data = (d1, d2)  match {
    case (Data.Dec(a), Data.Dec(b)) => Data.Dec(a * b)
    case (Data.Int(a), Data.Dec(b)) => Data.Dec(BigDecimal(a) * b)
    case (Data.Dec(a), Data.Int(b)) => Data.Dec(a * BigDecimal(b))
    case (Data.Int(a), Data.Int(b)) => Data.Int(a * b)
    case (Data.Interval(a), Data.Dec(b)) => Data.Interval(a.multiply(b.toInt))
    case (Data.Interval(a), Data.Int(b)) => Data.Interval(a.multiply(b.toInt))
    case (Data.Dec(a), Data.Interval(b)) => Data.Interval(b.multiply(a.toInt))
    case (Data.Int(a), Data.Interval(b)) => Data.Interval(b.multiply(a.toInt))
    case _ => undefined
  }

  private def divide(d1: Data, d2: Data): Data = (d1, d2) match {
    case (_, Data.Dec(b)) if b === BigDecimal(0) => Data.NA
    case (_, Data.Int(b)) if b === 0 => Data.NA
    case (Data.Dec(a), Data.Dec(b)) => Data.Dec(a(BigDecimal.defaultMathContext) / b)
    case (Data.Int(a), Data.Dec(b)) => Data.Dec(BigDecimal(a) / b)
    case (Data.Dec(a), Data.Int(b)) => Data.Dec(a(BigDecimal.defaultMathContext) / BigDecimal(b))
    case (Data.Int(a), Data.Int(b)) => Data.Dec(BigDecimal(a) / BigDecimal(b))
    case _ => undefined
  }

  private def modulo(d1: Data, d2: Data) = (d1, d2) match {
    case (Data.Int(a), Data.Int(b)) => Data.Int(a % b)
    case (Data.Int(a), Data.Dec(b)) => Data.Dec(BigDecimal(a).remainder(b))
    case (Data.Dec(a), Data.Int(b)) => Data.Dec(a.remainder(BigDecimal(b)))
    case (Data.Dec(a), Data.Dec(b)) => Data.Dec(a.remainder(b))
    case _ => undefined
  }

  // TODO we loose precision here, consider using https://github.com/non/spire/
  private def power(d1: Data, d2: Data): Data = (d1, d2) match {
    case (Data.Int(a), Data.Int(b)) => Data.Dec(math.pow(a.toDouble, b.toDouble))
    case (Data.Int(a), Data.Dec(b)) => Data.Dec(math.pow(a.toDouble, b.toDouble))
    case (Data.Dec(a), Data.Int(b)) => Data.Dec(math.pow(a.toDouble, b.toDouble))
    case (Data.Dec(a), Data.Dec(b)) => Data.Dec(math.pow(a.toDouble, b.toDouble))
    case _ => undefined
  }

  private def lt(d1: Data, d2: Data): Data = (d1, d2) match {
    case (Data.Int(a), Data.Int(b)) => Data.Bool(a < b)
    case (Data.Dec(a), Data.Dec(b)) => Data.Bool(a < b)
    case (Data.Int(a), Data.Dec(b)) => Data.Bool(BigDecimal(a) < b)
    case (Data.Dec(a), Data.Int(b)) => Data.Bool(a < BigDecimal(b))
    case (Data.Interval(a), Data.Interval(b)) => Data.Bool(a.compareTo(b) < 0)
    case (Data.Str(a), Data.Str(b)) => Data.Bool(a.compareTo(b) < 0)
    case (Data.OffsetDateTime(a), Data.OffsetDateTime(b)) => Data.Bool(a.compareTo(b) < 0)
    case (Data.OffsetDate(a), Data.OffsetDate(b)) => Data.Bool(a.compareTo(b) < 0)
    case (Data.OffsetTime(a), Data.OffsetTime(b)) => Data.Bool(a.compareTo(b) < 0)
    case (Data.LocalDateTime(a), Data.LocalDateTime(b)) => Data.Bool(a.compareTo(b) < 0)
    case (Data.LocalDate(a), Data.LocalDate(b)) => Data.Bool(a.compareTo(b) < 0)
    case (Data.LocalTime(a), Data.LocalTime(b)) => Data.Bool(a.compareTo(b) < 0)
    case (Data.Bool(a), Data.Bool(b)) => (a, b) match {
      case (false, true) => Data.Bool(true)
      case _ => Data.Bool(false)
    }
    case _ => undefined
  }

  private def lte(d1: Data, d2: Data): Data = (d1, d2) match {
    case (Data.Int(a), Data.Int(b)) => Data.Bool(a <= b)
    case (Data.Dec(a), Data.Dec(b)) => Data.Bool(a <= b)
    case (Data.Int(a), Data.Dec(b)) => Data.Bool(BigDecimal(a) <= b)
    case (Data.Dec(a), Data.Int(b)) => Data.Bool(a <= BigDecimal(b))
    case (Data.Interval(a), Data.Interval(b)) => Data.Bool(a.compareTo(b) <= 0)
    case (Data.Str(a), Data.Str(b)) => Data.Bool(a.compareTo(b) <= 0)
    case (Data.OffsetDateTime(a), Data.OffsetDateTime(b)) => Data.Bool(a.compareTo(b) <= 0)
    case (Data.OffsetDate(a), Data.OffsetDate(b)) => Data.Bool(a.compareTo(b) <= 0)
    case (Data.OffsetTime(a), Data.OffsetTime(b)) => Data.Bool(a.compareTo(b) <= 0)
    case (Data.LocalDateTime(a), Data.LocalDateTime(b)) => Data.Bool(a.compareTo(b) <= 0)
    case (Data.LocalDate(a), Data.LocalDate(b)) => Data.Bool(a.compareTo(b) <= 0)
    case (Data.LocalTime(a), Data.LocalTime(b)) => Data.Bool(a.compareTo(b) <= 0)
    case (Data.Bool(a), Data.Bool(b)) => (a, b) match {
      case (true, false) => Data.Bool(false)
      case _ => Data.Bool(true)
    }
    case _ => undefined
  }

  private def gt(d1: Data, d2: Data): Data = (d1, d2) match {
    case (Data.Int(a), Data.Int(b)) => Data.Bool(a > b)
    case (Data.Dec(a), Data.Dec(b)) => Data.Bool(a > b)
    case (Data.Int(a), Data.Dec(b)) => Data.Bool(BigDecimal(a) > b)
    case (Data.Dec(a), Data.Int(b)) => Data.Bool(a > BigDecimal(b))
    case (Data.Interval(a), Data.Interval(b)) => Data.Bool(a.compareTo(b) > 0)
    case (Data.Str(a), Data.Str(b)) => Data.Bool(a.compareTo(b) > 0)
    case (Data.OffsetDateTime(a), Data.OffsetDateTime(b)) => Data.Bool(a.compareTo(b) > 0)
    case (Data.OffsetDate(a), Data.OffsetDate(b)) => Data.Bool(a.compareTo(b) > 0)
    case (Data.OffsetTime(a), Data.OffsetTime(b)) => Data.Bool(a.compareTo(b) > 0)
    case (Data.LocalDateTime(a), Data.LocalDateTime(b)) => Data.Bool(a.compareTo(b) > 0)
    case (Data.LocalDate(a), Data.LocalDate(b)) => Data.Bool(a.compareTo(b) > 0)
    case (Data.LocalTime(a), Data.LocalTime(b)) => Data.Bool(a.compareTo(b) > 0)
    case (Data.Bool(a), Data.Bool(b)) => (a, b) match {
      case (true, false) => Data.Bool(true)
      case _ => Data.Bool(false)
    }
    case _ => undefined
  }

  private def gte(d1: Data, d2: Data): Data = (d1, d2) match {
    case (Data.Int(a), Data.Int(b)) => Data.Bool(a >= b)
    case (Data.Dec(a), Data.Dec(b)) => Data.Bool(a >= b)
    case (Data.Int(a), Data.Dec(b)) => Data.Bool(BigDecimal(a) >= b)
    case (Data.Dec(a), Data.Int(b)) => Data.Bool(a >= BigDecimal(b))
    case (Data.Interval(a), Data.Interval(b)) => Data.Bool(a.compareTo(b) >= 0)
    case (Data.Str(a), Data.Str(b)) => Data.Bool(a.compareTo(b) >= 0)
    case (Data.OffsetDateTime(a), Data.OffsetDateTime(b)) => Data.Bool(a.compareTo(b) >= 0)
    case (Data.OffsetDate(a), Data.OffsetDate(b)) => Data.Bool(a.compareTo(b) >= 0)
    case (Data.OffsetTime(a), Data.OffsetTime(b)) => Data.Bool(a.compareTo(b) >= 0)
    case (Data.LocalDateTime(a), Data.LocalDateTime(b)) => Data.Bool(a.compareTo(b) >= 0)
    case (Data.LocalDate(a), Data.LocalDate(b)) => Data.Bool(a.compareTo(b) >= 0)
    case (Data.LocalTime(a), Data.LocalTime(b)) => Data.Bool(a.compareTo(b) >= 0)
    case (Data.Bool(a), Data.Bool(b)) => (a, b) match {
      case (false, true) => Data.Bool(false)
      case _ => Data.Bool(true)
    }
    case _ => undefined
  }

  private def between(d1: Data, d2: Data, d3: Data): Data = (d1, d2, d3) match {
    case (Data.Int(a), Data.Int(b), Data.Int(c)) =>
      Data.Bool(b <= a && a <= c)
    case (Data.Dec(a), Data.Dec(b), Data.Dec(c)) =>
      Data.Bool(b <= a && a <= c)
    case (Data.Interval(a), Data.Interval(b), Data.Interval(c)) =>
      Data.Bool(b.compareTo(a) <= 0 && a.compareTo(c) <= 0)
    case (Data.Str(a), Data.Str(b), Data.Str(c)) =>
      Data.Bool(b.compareTo(a) <= 0 && a.compareTo(c) <= 0)
    case (Data.OffsetDateTime(a), Data.OffsetDateTime(b), Data.OffsetDateTime(c)) =>
      Data.Bool(b.compareTo(a) <= 0 && a.compareTo(c) <= 0)
    case (Data.OffsetDate(a), Data.OffsetDate(b), Data.OffsetDate(c)) =>
      Data.Bool(b.compareTo(a) <= 0 && a.compareTo(c) <= 0)
    case (Data.OffsetTime(a), Data.OffsetTime(b), Data.OffsetTime(c)) =>
      Data.Bool(b.compareTo(a) <= 0 && a.compareTo(c) <= 0)
    case (Data.LocalDateTime(a), Data.LocalDateTime(b), Data.LocalDateTime(c)) =>
      Data.Bool(b.compareTo(a) <= 0 && a.compareTo(c) <= 0)
    case (Data.LocalDate(a), Data.LocalDate(b), Data.LocalDate(c)) =>
      Data.Bool(b.compareTo(a) <= 0 && a.compareTo(c) <= 0)
    case (Data.LocalTime(a), Data.LocalTime(b), Data.LocalTime(c)) =>
      Data.Bool(b.compareTo(a) <= 0 && a.compareTo(c) <= 0)
    case (Data.Bool(a), Data.Bool(b), Data.Bool(c)) => (b,a,c) match {
      case (false, false, true) => Data.Bool(true)
      case (false, true, true) => Data.Bool(true)
      case (true, true, true) => Data.Bool(true)
      case (false, false, false) => Data.Bool(true)
      case _ => Data.Bool(false)
    }
    case _ => undefined
  }

  private def toStringFunc: Data => Data = {
    case Data.Null => Data.Str("null")
    case d: Data.Str => d
    case Data.Bool(v) => Data.Str(v.toString)
    case Data.Dec(v) => Data.Str(v.toString)
    case Data.Int(v) => Data.Str(v.toString)
    case Data.OffsetDateTime(v) => Data.Str(v.toString)
    case Data.OffsetDate(v) => Data.Str(v.toString)
    case Data.OffsetTime(v) => Data.Str(v.toString)
    case Data.LocalDateTime(v) => Data.Str(v.toString)
    case Data.LocalDate(v) => Data.Str(v.toString)
    case Data.LocalTime(v) => Data.Str(v.toString)
    case Data.Interval(v) => Data.Str(v.toString)
    case Data.Binary(v) => Data.Str(v.toList.mkString(""))
    case Data.Id(s) => Data.Str(s)
    case _ => undefined
  }

  private def century(year: Int): Data = Data.Int(((year - 1) / 100) + 1)

  private def temporalTrunc(part: TemporalPart, src: Data): Data =
    src match {
      case CanLensDateTime(i) => i.peeks(datetime.truncDateTime(part, _))
      case CanLensDate(i)     => i.peeks(datetime.truncDate(part, _))
      case CanLensTime(i)     => i.peeks(datetime.truncTime(part, _))
      case _                  => undefined
    }

  private def search(dStr: Data, dPattern: Data, dInsen: Data): Data =
    (dStr, dPattern, dInsen) match {
      case (Data.Str(str), Data.Str(pattern), Data.Bool(insen)) =>
        Data.Bool(StringLib.matchAnywhere(str, pattern, insen))
      case _ => undefined
    }

  private def substring(dStr: Data, dFrom: Data, dCount: Data): Data =
    (dStr, dFrom, dCount) match {
      case (Data.Str(str), Data.Int(from), Data.Int(count)) =>
        \/.fromTryCatchNonFatal(Data.Str(StringLib.safeSubstring(str, from.toInt, count.toInt))).fold(κ(Data.NA), ι)
      case _ => undefined
    }

  private def split(dStr: Data, dDelim: Data): Data =
    (dStr, dDelim) match {
      case (Data.Str(str), Data.Str(delim)) =>
        \/.fromTryCatchNonFatal(Data.Arr(str.split(Regex.quote(delim), -1).toList.map(Data.Str(_)))).fold(κ(Data.NA), ι)
      case _ => undefined
    }

}


text:
/**
 * Define a Rectangle class
 */
class Rect{
    constructor(width,height){
        this.width = width;
        this.height = height;
    }
    area() {
        return this.width * this.height;
    }

    circumference() {
        return (this.width + this.height) * 2;
    }
}
var r = new Rect(100,30);
r.area();
r.circumference();


text:
Login-AzureRMAccount

$spParams = @{
    DisplayName = exampleapp
    Password    = "{provide-password}"
}

$sp = New-AzureRmADServicePrincipal @spParams

$SleepParams = @{
    Seconds = 20
}

Start-Sleep @SleepParams

$AzureRoleAssignmentParams = @{
    RoleDefinitionName   = Contributor
    ServicePrincipalName = $sp.ApplicationId
}

New-AzureRmRoleAssignment @AzureRoleAssignmentParams

$secpasswdParams = @{
    String      = "mmy8V6nOcOyGX0B"
    AsPlainText = $true
    Force       = $true
}

$secpasswd = ConvertTo-SecureString @secpasswdParams

$mycredsParams = @{
    TypeName     = System.Management.Automation.PSCredential
    ArgumentList = ("768835e9-73f7-4a04-a5c4-b68c43b11477", $secpasswd)
}

$mycreds = New-Object @mycredsParams

$AzureRMAccountParams = @{
    Credential       = $mycreds
    ServicePrincipal = $true
    TenantId         = 72f988bf-86f1-41af-91ab-2d7cd011db47
}

Login-AzureRmAccount @AzureRMAccountParams


text:
<!DOCTYPE html>

	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title><%= htmlWebpackPlugin.options.title %></title>