LevelDB.jl: Julia interface to Google's LevelDB Key Value Database

LevelDB 

LevelDB is Google's open source on-disk key-value storage library that provides an ordered mapping from string keys to binary values. In many applications where only key based accesses are needed, it tends to be a faster alternative than databases. LevelDB was written in C++ with a C calling API included. This module provides a Julia interface to LevelDB using Julia's ccall mechanism.

Install LevelDB

You can build LevelDB from its source code at https://github.com/google/leveldb. Please install the final dynamic library into a system directory such as /usr/lib or make sure libleveldb.so is in one of your LD_LIBRARY_PATH directories. If libleveldb.so is not installed, Julia will try to download and build it automatically.

Run Testing Code

(v1.1) pkg> test LevelDB

This will exercise batched and non-batched writes and reads for string and float array values.

Create/Open/Close a LevelDB database

julia> db = LevelDB.DB(file_path; create_if_missing = false, error_if_exists = false)

Here file_path is the full path to a directory that hosts a LevelDB database. create_if_missing is a boolean flag when true the database will be created if it does not exist. error_if_exists is a boolean flag when true an error will be thrown if the database already exists. The return value is a database object for passing to read/write calls.

julia> close(db)

Close a database, db is the object returned from a LevelDB.DB call. A directory can only be opened by a single LevelDB.DB at a time.

Read and Write Operations

julia> db[key] = value

key and value are Array{UInt8}.

julia> db[key]

Return value is an Array{UInt8}, one can use the reinterpret function to cast it into the right array type (see test code).

julia> delete!(db, key)

Delete a key from db.

Batched Write

LevelDB supports grouping a number of put operations into a write batch, the batch will either succeed as a whole or fail altogether, behaving like an atomic update.

julia> db[keys] = values

keys and values must behave like iterators returning Array{UInt8}. Creates a write batch internally which is then commited to db.

Iterate

julia> for (key, value) in db
           #do something with the key value pair
       end

Iterate over all key => value pairs in a LevelDB.DB.

julia> for (key, value) in LevelDB.RangeView(db, key1, key2)
           #do something with the key value pair
       end

Iterate over a range between key1 and key2 (inclusive)

Authors

  • Jerry Zhenlei Cai ( jpenguin at gmail dot com )
  • Guido Kraemer

additional contributions by

  • @huwenshuo
  • @tmlbl

Download Details:

Author: jerryzhenleicai
Source Code: https://github.com/jerryzhenleicai/LevelDB.jl 
License: View license

#julia #leveldb #interface #database 

LevelDB.jl: Julia interface to Google's LevelDB Key Value Database

DBInterface.jl: Database interface definitions for Julia

DBInterface.jl  

Purpose

DBInterface.jl provides interface definitions to allow common database operations to be implemented consistently across various database packages.

For Users

To use DBInterface.jl, select an implementing database package, then utilize the consistent DBInterface.jl interface methods:

conn = DBInterface.connect(T, args...; kw...) # create a connection to a specific database T; required parameters are database-specific

stmt = DBInterface.prepare(conn, sql) # prepare a sql statement against the connection; returns a statement object

results = DBInterface.execute(stmt) # execute a prepared statement; returns an iterator of rows (property-accessible & indexable)

rowid = DBInterface.lastrowid(results) # get the last row id of an INSERT statement, as supported by the database

# example of using a query resultset
for row in results
    @show propertynames(row) # see possible column names of row results
    row.col1 # access the value of a column named `col1`
    row[1] # access the first column in the row results
end

# results also implicitly satisfy the Tables.jl `Tables.rows` inteface, so any compatible sink can ingest results
df = DataFrame(results)
CSV.write("results.csv", results)

results = DBInterface.execute(conn, sql) # convenience method if statement preparation/re-use isn't needed

stmt = DBInterface.prepare(conn, "INSERT INTO test_table VALUES(?, ?)") # prepare a statement with positional parameters

DBInterface.execute(stmt, [1, 3.14]) # execute the prepared INSERT statement, passing 1 and 3.14 as positional parameters

stmt = DBInterface.prepare(conn, "INSERT INTO test_table VALUES(:col1, :col2)") # prepare a statement with named parameters

DBInterface.execute(stmt, (col1=1, col2=3.14)) # execute the prepared INSERT statement, with 1 and 3.14 as named parameters

DBInterface.executemany(stmt, (col1=[1,2,3,4,5], col2=[3.14, 1.23, 2.34 3.45, 4.56])) # execute the prepared statement multiple times for each set of named parameters; each named parameter must be an indexable collection

results = DBInterface.executemultiple(conn, sql) # where sql is a query that returns multiple resultsets

# first iterate through resultsets
for result in results
    # for each resultset, we can iterate through resultset rows
    for row in result
        @show propertynames(row)
        row.col1
        row[1]
    end
end

DBInterface.close!(stmt) # close the prepared statement
DBInterface.close!(conn) # close connection

For Database Package Developers

See the documentation for expanded details on required interface methods.

Download Details:

Author: JuliaDatabases 
Source Code: https://github.com/JuliaDatabases/DBInterface.jl 
License: View license

#julia #database #interface 

DBInterface.jl: Database interface definitions for Julia

Lightweight Key-value interface to A Bunch Of Storage Engines

Chainstore

Chainstore is simple key-value interface to a variety of storage engines organized as a chain of operations. A store adapter is just an engine interface to Open, Close, Put, Get, and Del . Each store has their own inherent properties and so when chained together, it makes for a useful combinations of data caching, flow and persistence depending on your application.

Here is an example of Boltdb and S3 stores chained together to provide fast read/writes to a local working dataset of 500MB and async S3 access for long-term persistence / retrieval. Check out the LRUManager below too, its wrapped around Boltdb to make sure only the least-recently-used key/values are persisted -- the manager can be used with any of the stores and with the chain, which is pretty nifty. This example is also here: example/main.go.

package main

import (
    "fmt"
    "os"
    "time"
    "log"

    "github.com/pressly/chainstore"
    "github.com/pressly/chainstore/boltstore"
    "github.com/pressly/chainstore/lrumgr"
    "github.com/pressly/chainstore/metricsmgr"
    "github.com/pressly/chainstore/s3store"
    "golang.org/x/net/context"
)

var (
    bucketID  string
    accessKey string
    secretKey string
)

func init() {
    bucketID = os.Getenv("S3_BUCKET")
    accessKey = os.Getenv("S3_ACCESS_KEY")
    secretKey = os.Getenv("S3_SECRET_KEY")
}

func main() {
    ctx := context.Background()

    diskStore := lrumgr.New(500*1024*1024, // 500MB of working data
        metricsmgr.New("chainstore.ex.bolt",
            boltstore.New("/tmp/store.db", "myBucket"),
        ),
    )

    remoteStore := metricsmgr.New("chainstore.ex.s3",
        // NOTE: you'll have to supply your own keys in order for this example to work properly
        s3store.New(bucketID, accessKey, secretKey),
    )

    dataStore := chainstore.New(diskStore, remoteStore)

    // OR.. define inline. Except, I wanted to show store independence & state.
    /*
        dataStore := chainstore.New(
            lrumgr.New(500*1024*1024, // 500MB of working data
                metricsmgr.New("chainstore.ex.bolt",
                    boltstore.New("/tmp/store.db", "myBucket"),
                ),
            ),
            metricsmgr.New("chainstore.ex.s3",
                // NOTE: you'll have to supply your own keys in order for this example to work properly
                s3store.New("myBucket", "access-key", "secret-key"),
            ),
        )
    */

    var err error

    err = dataStore.Open()
    if err != nil {
        log.Fatalf("Open: %q", err)
    }

    // Since we've used the metricsManager above (metricsmgr), any calls to the boltstore
    // and s3store will be measured. Next is to send metrics to librato, graphite, influxdb,
    // whatever.. via github.com/goware/go-metrics
    // go librato.Librato(metrics.DefaultRegistry, 10e9, ...)

    //--

    // Save the object in the chain. It will be Put() synchronously into diskStore,
    // the boltdb engine, and then immediately dispatch background Put()'s to the
    // other stores down the chain, in this case S3.
    fmt.Println("Example 1...")
    obj := []byte{1, 2, 3}
    err = dataStore.Put(ctx, "k", obj)
    if err != nil {
        log.Fatalf("Put: %q", err)
    }
    fmt.Println("Put 'k':", obj, "in the chain")

    v, err := dataStore.Get(ctx, "k")
    if err != nil {
        log.Fatalf("Put: %q", err)
    }
    fmt.Println("Grabbing 'k' from the chain:", v) // => [1 2 3]

    // For demonstration, let's grab the key directly from the store instead of
    // through the chain. This is pretty much the same as above, as the chain's Get()
    // stops once it finds the object.
    v, err = diskStore.Get(ctx, "k")
    if err != nil {
        log.Fatalf("Put: %q", err)
    }
    fmt.Println("Grabbing 'k' directly from boltdb:", v) // => [1 2 3]

    // lets pause for a moment and then try to retrieve the value from the s3 store
    time.Sleep(1e9)

    // Grab the object from s3
    v, err = remoteStore.Get(ctx, "k")
    if err != nil {
        log.Fatalf("Put: %q", err)
    }
    fmt.Println("Grabbing 'k' directly from s3:", v) // => [1 2 3]

    // Delete the object from everywhere
    dataStore.Del(ctx, "k")
    time.Sleep(1e9) // pause for s3 demo
    v, _ = dataStore.Get(ctx, "k")
    fmt.Println("Deleted 'k' from the chain (all stores). Get(k) returns:", v)

    //--

    // Another interesting behavior of the chain is when doing a Get(), it goes down
    // the entire chain looking for the value, and when found, it will Put() that
    // object back up the chain for subsequent retrievals. Lets see..
    fmt.Println("Example 2...")
    obj = []byte("hope you enjoy")
    err = dataStore.Put(ctx, "hi", obj)
    if err != nil {
        log.Fatalf("Put: %q", err)
    }
    fmt.Println("Put 'hi':", obj, "in the chain")
    time.Sleep(1e9) // lets wait for s3 again with more then enough time

    err = diskStore.Del(ctx, "hi")
    if err != nil {
        log.Fatalf("Get: %q", err)
    }

    v, _ = diskStore.Get(ctx, "hi")
    fmt.Println("Delete 'hi' from boltdb. diskStore.Get(k) returns:", v)

    v, err = dataStore.Get(ctx, "hi")
    if err != nil {
        log.Fatalf("Get: %q", err)
    }
    fmt.Println("Let's ask the chain for 'hi':", v)
    time.Sleep(1e9) // pause for bg routine to fill our local cache

    // The diskStore now has the value again from remoteStore lower down the chain.
    v, err = diskStore.Get(ctx, "hi")
    if err != nil {
        log.Fatalf("Get: %q", err)
    }
    fmt.Println("Now, let's ask our diskStore again! diskStore.Get(k) returns:", v)

    // Also.. even though it hasn't been demonstrated here, the diskStore will only
    // store a max of 500MB (as defined with diskLru) worth of objects. Give it a shot.
}

/* OUTPUT:

Example 1...
Put 'k': [1 2 3] in the chain
Grabbing 'k' from the chain: [1 2 3]
Grabbing 'k' directly from boltdb: [1 2 3]
Grabbing 'k' directly from s3: [1 2 3]
Deleted 'k' from the chain (all stores). Get(k) returns: []
Example 2...
Put 'hi': [104 111 112 101 32 121 111 117 32 101 110 106 111 121] in the chain
Delete 'hi' from boltdb. diskStore.Get(k) returns: []
Let's ask the chain for 'hi': [104 111 112 101 32 121 111 117 32 101 110 106 111 121]
Now, let's ask our diskStore again! diskStore.Get(k) returns: [104 111 112 101 32 121 111 117 32 101 110 106 111 121]

*/

Currently supported stores: memory, filesystem, boltdb, leveldb, s3, a lru manager, and a metrics manager that can be layered ontop. You can chain these together for different behaviours, for example the memstore implementation is just a simple map[string][]byte with the LRU cache manager (lrumgr).

Thx to other great projects:

Changelog

TODO / Ideas

Error channel where bad puts are communicated so they can be properly handled further down the chain

Idea: provide option to hash the input keys which would make each key fixed-length and smaller footprint everywhere

Timeout (with error notification) when adding an item to a store (ie. 60 seconds max to confirm)

Consider a 'config' structure to pass to stores that can configure things like: * For s3 store, add ACL with options: private, public_read, public_read_write, authenticated_read

Download Details:

Author: Pressly
Source Code: https://github.com/pressly/chainstore 
License: MIT License

#go #golang #interface 

Lightweight Key-value interface to A Bunch Of Storage Engines
Dexter  Goodwin

Dexter Goodwin

1661223840

Lightweight, Beautiful and User-friendly interactive Prompts

❯ Prompts

Lightweight, beautiful and user-friendly interactive prompts
>_ Easy to use CLI prompts to enquire users for information▌

  • Simple: prompts has no big dependencies nor is it broken into a dozen tiny modules that only work well together.
  • User friendly: prompt uses layout and colors to create beautiful cli interfaces.
  • Promised: uses promises and async/await. No callback hell.
  • Flexible: all prompts are independent and can be used on their own.
  • Testable: provides a way to submit answers programmatically.
  • Unified: consistent experience across all prompts.

❯ Install

$ npm install --save prompts

This package supports Node 6 and above

split

❯ Usage

example prompt

const prompts = require('prompts');

(async () => {
  const response = await prompts({
    type: 'number',
    name: 'value',
    message: 'How old are you?',
    validate: value => value < 18 ? `Nightclub is 18+ only` : true
  });

  console.log(response); // => { value: 24 }
})();

See example.js for more options.

split

❯ Examples

Single Prompt

Prompt with a single prompt object. Returns an object with the response.

const prompts = require('prompts');

(async () => {
  const response = await prompts({
    type: 'text',
    name: 'meaning',
    message: 'What is the meaning of life?'
  });

  console.log(response.meaning);
})();

Prompt Chain

Prompt with a list of prompt objects. Returns an object with the responses. Make sure to give each prompt a unique name property to prevent overwriting values.

const prompts = require('prompts');

const questions = [
  {
    type: 'text',
    name: 'username',
    message: 'What is your GitHub username?'
  },
  {
    type: 'number',
    name: 'age',
    message: 'How old are you?'
  },
  {
    type: 'text',
    name: 'about',
    message: 'Tell something about yourself',
    initial: 'Why should I?'
  }
];

(async () => {
  const response = await prompts(questions);

  // => response => { username, age, about }
})();

Dynamic Prompts

Prompt properties can be functions too. Prompt Objects with type set to falsy values are skipped.

const prompts = require('prompts');

const questions = [
  {
    type: 'text',
    name: 'dish',
    message: 'Do you like pizza?'
  },
  {
    type: prev => prev == 'pizza' ? 'text' : null,
    name: 'topping',
    message: 'Name a topping'
  }
];

(async () => {
  const response = await prompts(questions);
})();

split

❯ API

prompts(prompts, options)

Type: Function
Returns: Object

Prompter function which takes your prompt objects and returns an object with responses.

prompts

Type: Array|Object

Array of prompt objects. These are the questions the user will be prompted. You can see the list of supported prompt types here.

Prompts can be submitted (return, enter) or canceled (esc, abort, ctrl+c, ctrl+d). No property is being defined on the returned response object when a prompt is canceled.

options.onSubmit

Type: Function
Default: () => {}

Callback that's invoked after each prompt submission. Its signature is (prompt, answer, answers) where prompt is the current prompt object, answer the user answer to the current question and answers the user answers so far. Async functions are supported.

Return true to quit the prompt chain and return all collected responses so far, otherwise continue to iterate prompt objects.

Example:

(async () => {
  const questions = [{ ... }];
  const onSubmit = (prompt, answer) => console.log(`Thanks I got ${answer} from ${prompt.name}`);
  const response = await prompts(questions, { onSubmit });
})();

options.onCancel

Type: Function
Default: () => {}

Callback that's invoked when the user cancels/exits the prompt. Its signature is (prompt, answers) where prompt is the current prompt object and answers the user answers so far. Async functions are supported.

Return true to continue and prevent the prompt loop from aborting. On cancel responses collected so far are returned.

Example:

(async () => {
  const questions = [{ ... }];
  const onCancel = prompt => {
    console.log('Never stop prompting!');
    return true;
  }
  const response = await prompts(questions, { onCancel });
})();

override

Type: Function

Preanswer questions by passing an object with answers to prompts.override. Powerful when combined with arguments of process.

Example

const prompts = require('prompts');
prompts.override(require('yargs').argv);

(async () => {
  const response = await prompts([
    {
      type: 'text',
      name: 'twitter',
      message: `What's your twitter handle?`
    },
    {
      type: 'multiselect',
      name: 'color',
      message: 'Pick colors',
      choices: [
        { title: 'Red', value: '#ff0000' },
        { title: 'Green', value: '#00ff00' },
        { title: 'Blue', value: '#0000ff' }
      ],
    }
  ]);

  console.log(response);
})();

inject(values)

Type: Function

Programmatically inject responses. This enables you to prepare the responses ahead of time. If any injected value is found the prompt is immediately resolved with the injected value. This feature is intended for testing only.

values

Type: Array

Array with values to inject. Resolved values are removed from the internal inject array. Each value can be an array of values in order to provide answers for a question asked multiple times. If a value is an instance of Error it will simulate the user cancelling/exiting the prompt.

Example:

const prompts = require('prompts');

prompts.inject([ '@terkelg', ['#ff0000', '#0000ff'] ]);

(async () => {
  const response = await prompts([
    {
      type: 'text',
      name: 'twitter',
      message: `What's your twitter handle?`
    },
    {
      type: 'multiselect',
      name: 'color',
      message: 'Pick colors',
      choices: [
        { title: 'Red', value: '#ff0000' },
        { title: 'Green', value: '#00ff00' },
        { title: 'Blue', value: '#0000ff' }
      ],
    }
  ]);

  // => { twitter: 'terkelg', color: [ '#ff0000', '#0000ff' ] }
})();

split

❯ Prompt Objects

Prompts Objects are JavaScript objects that define the "questions" and the type of prompt. Almost all prompt objects have the following properties:

{  type: String | Function,  name: String | Function,  message: String | Function,  initial: String | Function | Async Function  format: Function | Async Function,  onRender: Function  onState: Function  stdin: Readable  stdout: Writeable }

Each property be of type function and will be invoked right before prompting the user.

The function signature is (prev, values, prompt), where prev is the value from the previous prompt, values is the response object with all values collected so far and prompt is the previous prompt object.

Function example:

{
  type: prev => prev > 3 ? 'confirm' : null,
  name: 'confirm',
  message: (prev, values) => `Please confirm that you eat ${values.dish} times ${prev} a day?`
}

The above prompt will be skipped if the value of the previous prompt is less than 3.

type

Type: String|Function

Defines the type of prompt to display. See the list of prompt types for valid values.

If type is a falsy value the prompter will skip that question.

{
  type: null,
  name: 'forgetme',
  message: `I'll never be shown anyway`,
}

name

Type: String|Function

The response will be saved under this key/property in the returned response object. In case you have multiple prompts with the same name only the latest response will be stored.

Make sure to give prompts unique names if you don't want to overwrite previous values.

message

Type: String|Function

The message to be displayed to the user.

initial

Type: String|Function

Optional default prompt value. Async functions are supported too.

format

Type: Function

Receive the user input and return the formatted value to be used inside the program. The value returned will be added to the response object.

The function signature is (val, values), where val is the value from the current prompt and values is the current response object in case you need to format based on previous responses.

Example:

{
  type: 'number',
  name: 'price',
  message: 'Enter price',
  format: val => Intl.NumberFormat(undefined, { style: 'currency', currency: 'USD' }).format(val);
}

onRender

Type: Function

Callback for when the prompt is rendered. The function receives kleur as its first argument and this refers to the current prompt.

Example:

{
  type: 'number',
  message: 'This message will be overridden',
  onRender(kleur) {
    this.msg = kleur.cyan('Enter a number');
  }
}

onState

Type: Function

Callback for when the state of the current prompt changes. The function signature is (state) where state is an object with a snapshot of the current state. The state object has two properties value and aborted. E.g { value: 'This is ', aborted: false }

stdin and stdout

Type: Stream

By default, prompts uses process.stdin for receiving input and process.stdout for writing output. If you need to use different streams, for instance process.stderr, you can set these with the stdin and stdout properties.

split

❯ Types


text(message, [initial], [style])

Text prompt for free text input.

Hit tab to autocomplete to initial value when provided.

Example

text prompt

{
  type: 'text',
  name: 'value',
  message: `What's your twitter handle?`
}

Options

ParamTypeDescription
messagestringPrompt message to display
initialstringDefault string value
stylestringRender style (default, password, invisible, emoji). Defaults to default
formatfunctionReceive user input. The returned value will be added to the response object
validatefunctionReceive user input. Should return true if the value is valid, and an error message String otherwise. If false is returned, a default error message is shown
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

password(message, [initial])

Password prompt with masked input.

This prompt is a similar to a prompt of type 'text' with style set to 'password'.

Example

password prompt

{
  type: 'password',
  name: 'value',
  message: 'Tell me a secret'
}

Options

ParamTypeDescription
messagestringPrompt message to display
initialstringDefault string value
formatfunctionReceive user input. The returned value will be added to the response object
validatefunctionReceive user input. Should return true if the value is valid, and an error message String otherwise. If false is returned, a default error message is shown
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

invisible(message, [initial])

Prompts user for invisible text input.

This prompt is working like sudo where the input is invisible. This prompt is a similar to a prompt of type 'text' with style set to 'invisible'.

Example

invisible prompt

{
  type: 'invisible',
  name: 'value',
  message: 'Enter password'
}

Options

ParamTypeDescription
messagestringPrompt message to display
initialstringDefault string value
formatfunctionReceive user input. The returned value will be added to the response object
validatefunctionReceive user input. Should return true if the value is valid, and an error message String otherwise. If false is returned, a default error message is shown
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

number(message, initial, [max], [min], [style])

Prompts user for number input.

You can type in numbers and use up/down to increase/decrease the value. Only numbers are allowed as input. Hit tab to autocomplete to initial value when provided.

Example

number prompt

{
  type: 'number',
  name: 'value',
  message: 'How old are you?',
  initial: 0,
  style: 'default',
  min: 2,
  max: 10
}

Options

ParamTypeDescription
messagestringPrompt message to display
initialnumberDefault number value
formatfunctionReceive user input. The returned value will be added to the response object
validatefunctionReceive user input. Should return true if the value is valid, and an error message String otherwise. If false is returned, a default error message is shown
maxnumberMax value. Defaults to Infinity
minnumberMin value. Defaults to -infinity
floatbooleanAllow floating point inputs. Defaults to false
roundnumberRound float values to x decimals. Defaults to 2
incrementnumberIncrement step when using arrow keys. Defaults to 1
stylestringRender style (default, password, invisible, emoji). Defaults to default
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

confirm(message, [initial])

Classic yes/no prompt.

Hit y or n to confirm/reject.

Example

confirm prompt

{
  type: 'confirm',
  name: 'value',
  message: 'Can you confirm?',
  initial: true
}

Options

ParamTypeDescription
messagestringPrompt message to display
initialbooleanDefault value. Default is false
formatfunctionReceive user input. The returned value will be added to the response object
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

list(message, [initial])

List prompt that return an array.

Similar to the text prompt, but the output is an Array containing the string separated by separator.

{
  type: 'list',
  name: 'value',
  message: 'Enter keywords',
  initial: '',
  separator: ','
}

list prompt

ParamTypeDescription
messagestringPrompt message to display
initialbooleanDefault value
formatfunctionReceive user input. The returned value will be added to the response object
separatorstringString separator. Will trim all white-spaces from start and end of string. Defaults to ','
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

toggle(message, [initial], [active], [inactive])

Interactive toggle/switch prompt.

Use tab or arrow keys/tab/space to switch between options.

Example

toggle prompt

{
  type: 'toggle',
  name: 'value',
  message: 'Can you confirm?',
  initial: true,
  active: 'yes',
  inactive: 'no'
}

Options

ParamTypeDescription
messagestringPrompt message to display
initialbooleanDefault value. Defaults to false
formatfunctionReceive user input. The returned value will be added to the response object
activestringText for active state. Defaults to 'on'
inactivestringText for inactive state. Defaults to 'off'
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

select(message, choices, [initial], [hint], [warn])

Interactive select prompt.

Use up/down to navigate. Use tab to cycle the list.

Example

select prompt

{
  type: 'select',
  name: 'value',
  message: 'Pick a color',
  choices: [
    { title: 'Red', description: 'This option has a description', value: '#ff0000' },
    { title: 'Green', value: '#00ff00', disabled: true },
    { title: 'Blue', value: '#0000ff' }
  ],
  initial: 1
}

Options

ParamTypeDescription
messagestringPrompt message to display
initialnumberIndex of default value
formatfunctionReceive user input. The returned value will be added to the response object
hintstringHint to display to the user
warnstringMessage to display when selecting a disabled option
choicesArrayArray of strings or choices objects [{ title, description, value, disabled }, ...]. The choice's index in the array will be used as its value if it is not specified.
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

multiselect(message, choices, [initial], [max], [hint], [warn])

autocompleteMultiselect(same)

Interactive multi-select prompt.
Autocomplete is a searchable multiselect prompt with the same options. Useful for long lists.

Use space to toggle select/unselect and up/down to navigate. Use tab to cycle the list. You can also use right to select and left to deselect. By default this prompt returns an array containing the values of the selected items - not their display title.

Example

multiselect prompt

{
  type: 'multiselect',
  name: 'value',
  message: 'Pick colors',
  choices: [
    { title: 'Red', value: '#ff0000' },
    { title: 'Green', value: '#00ff00', disabled: true },
    { title: 'Blue', value: '#0000ff', selected: true }
  ],
  max: 2,
  hint: '- Space to select. Return to submit'
}

Options

ParamTypeDescription
messagestringPrompt message to display
formatfunctionReceive user input. The returned value will be added to the response object
instructionsstring or booleanPrompt instructions to display
choicesArrayArray of strings or choices objects [{ title, value, disabled }, ...]. The choice's index in the array will be used as its value if it is not specified.
optionsPerPagenumberNumber of options displayed per page (default: 10)
minnumberMin select - will display error
maxnumberMax select
hintstringHint to display to the user
warnstringMessage to display when selecting a disabled option
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

This is one of the few prompts that don't take a initial value. If you want to predefine selected values, give the choice object an selected property of true.


autocomplete(message, choices, [initial], [suggest], [limit], [style])

Interactive auto complete prompt.

The prompt will list options based on user input. Type to filter the list. Use ⇧/⇩ to navigate. Use tab to cycle the result. Use Page Up/Page Down (on Mac: fn + ⇧ / ⇩) to change page. Hit enter to select the highlighted item below the prompt.

The default suggests function is sorting based on the title property of the choices. You can overwrite how choices are being filtered by passing your own suggest function.

Example

auto complete prompt

{
  type: 'autocomplete',
  name: 'value',
  message: 'Pick your favorite actor',
  choices: [
    { title: 'Cage' },
    { title: 'Clooney', value: 'silver-fox' },
    { title: 'Gyllenhaal' },
    { title: 'Gibson' },
    { title: 'Grant' }
  ]
}

Options

ParamTypeDescription
messagestringPrompt message to display
formatfunctionReceive user input. The returned value will be added to the response object
choicesArrayArray of auto-complete choices objects [{ title, value }, ...]
suggestfunctionFilter function. Defaults to sort by title property. suggest should always return a promise. Filters using title by default
limitnumberMax number of results to show. Defaults to 10
stylestringRender style (default, password, invisible, emoji). Defaults to 'default'
initialstring | numberDefault initial value
clearFirstbooleanThe first ESCAPE keypress will clear the input
fallbackstringFallback message when no match is found. Defaults to initial value if provided
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with three properties: value, aborted and exited

Example on what a suggest function might look like:

const suggestByTitle = (input, choices) =>
    Promise.resolve(choices.filter(i => i.title.slice(0, input.length) === input))

date(message, [initial], [warn])

Interactive date prompt.

Use left/right/tab to navigate. Use up/down to change date.

Example

date prompt

{
  type: 'date',
  name: 'value',
  message: 'Pick a date',
  initial: new Date(1997, 09, 12),
  validate: date => date > Date.now() ? 'Not in the future' : true
}

Options

ParamTypeDescription
messagestringPrompt message to display
initialdateDefault date
localesobjectUse to define custom locales. See below for an example.
maskstringThe format mask of the date. See below for more information.
Default: YYYY-MM-DD HH:mm:ss
validatefunctionReceive user input. Should return true if the value is valid, and an error message String otherwise. If false is returned, a default error message is shown
onRenderfunctionOn render callback. Keyword this refers to the current prompt
onStatefunctionOn state change callback. Function signature is an object with two properties: value and aborted

Default locales:

{
  months: [
    'January', 'February', 'March', 'April',
    'May', 'June', 'July', 'August',
    'September', 'October', 'November', 'December'
  ],
  monthsShort: [
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  ],
  weekdays: [
    'Sunday', 'Monday', 'Tuesday', 'Wednesday',
    'Thursday', 'Friday', 'Saturday'
  ],
  weekdaysShort: [
    'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  ]
}

Formatting: See full list of formatting options in the wiki

split

❯ Credit

Many of the prompts are based on the work of derhuerst.

Download Details:

Author: Terkelg
Source Code: https://github.com/terkelg/prompts 
License: MIT license

#javascript #nodejs #cli #interface 

Lightweight, Beautiful and User-friendly interactive Prompts

JGUI.jl: Simplified interface for Making GUIs in Julia

JGUI

[This package may not work! Using Gtk.jl might just be best...]

A package to simplify the creation of GUIs within Julia

The JGUI package provides a few different means to ease the creation of GUIs within Julia. These include a simplified implementation of Mathematica's Manipulate function, and a simplified interface for using the tcl/tk, Gtk, or Qt toolkits within Julia.

Installation

The JGUI package installs with Pkg.clone("https://github.com/jverzani/JGUI.jl"). For it to work one needs to have installed the Tk package, the Gtk package, or the PySide package. The latter requires an installation of the Qt libraries (http://qt-project.org/downloads), Python (http://www.python.org/download/), the PySide (http://qt-project.org/wiki/Get-PySide) interface between Python and Qt, and the PyCall package to connect Python with Julia (installed with the PySide package). The Anaconda (http://docs.continuum.io/anaconda/) packaging of Python should be a one-stop installation, though the Qt part can be buggy.

Manipulate

The easiest way to create a GUI with this package is to use the manipulate function, which can be used to evaluate an expression parameterized by values coming from easily specified controls within a GUI.

The first line below allows one to specify the toolkit, currently Tk, Gtk (default) or Qt. Mixing and matching within a Julia session will likely lead to crashes. The toolkit is specified before JGUI is loaded, so that toolkit-specific code can be loaded.

ENV["toolkit"] = "Gtk"        # default, can be skipped
using JGUI

Now, consider the following expression which when evaluated produces a Winston plot object:

expr = quote
     using Winston
     plot(x -> sin(u*x), 0, 2pi)
end

We can use the manipulate function to fill in values for the unbound variable u, when the expression is evaluated. The plot command above returns a Winston plot object, which is then plotted. To create a control to specify values for u, we simply need to specify a range, as follows:

a = manipulate(expr, (:u, 1:10))

This call will pop up a simple GUI with a slider that allows one to adjust the value of u from 1 to 10, updating the graphic as this is done.

Here is a how one can add a title to the plot. First we modify the plot call to include a title:

expr = quote
     using Winston
     xs = linspace(0, 2pi)
     ys = [sin(u*x) for x in xs]
     plot(x, y, title=title)
end

Now title is also unbound. To specify a control to set a title, we use a string:

a = manipulate(expr, (:u, 1:10), (:title, "A sine plot"))

Now when the plot is updated, the title is also taken from a text box.

Manipulate has other simple-to-specify controls:

  • (:symbol, Bool) - checkbox. Use {:label=>"some label"} to label it.
  • (:symbol, Range) - slider
  • (:symbol, Range, Range) - 2d slider
  • (:symbol, Vector) - radio or combobox (depends on size)
  • (:symbol, String) - text edit
  • (:symbol, Real) - text edit with conversion to float via parsefloat
  • (:symbol, Int) - text edit with conversion to integer via parseint

The expression can evaluate to a Winston plot object or any other object. Plot objects are rendered in a graphics device, text objects in a text box.

When using Qt (ENV["toolkit"] = "Qt") one can plot PyPlot calls, not Winston calls. The modules argument should include PyPlot.

A simplified GUI interface

The Gtk, Tk, and PySide packages for Julia provide relatively easy means to produce GUIs with the Gtk, Tcl/Tk and Qt toolkits, respectively. In addition to the manipulate function, this package provides a small API for creating GUIs that makes it even easier than the other packages, though sacrificing a huge amount of their power and flexibility. (The JGUI interface is primarily concerned with simpler things like controls, and not more involved interfaces like those with a canvas widget.)

Here is a simple example where a window has a button which when clicked will destroy the window.

w = window(size=[200, 200])
w[:title] = "hello world"

b = button(w, "Close")
push!(w, b)

connect(b, "clicked", w, destroy)
raise(w)

The first line creates a window object with an optional size specified.

The second line shows how a property of the window object may be set, using indexing notation with an appropriate symbol, in this case :title. There are relatively few properties for any given object. For a control, the most important is :value. (The method properties will list all of a widget's properties.)

The third line creates a button object. All constructors except window use a parent container for the first argument. (This is similar to Tk and so the widget hierarchy is determined, but not the actual layout). The button constructor has label value for the second positional argument.

The fourth line is specific to JGUI and not the underlying toolkit. (Though, the same idea is present in Gtk.) Rather than use separate layout managers, as is done with Qt or Tk, each container object is conceptualized as a queue of some sort. For the window object, the push! method adds the button to the window queue, laying it out as it does so.

The fifth line is how one connects a callback to an event. In this case the receiver of a click event, b, will emit a signal clicked. The w object is passed to its method destroy to destroy the window. This pattern follows Qt's signal-and-slots style. One can also just pass in a function to call instead of the last two arguments, something like connect(b, "clicked", () -> destroy(w)).

Finally, the window is raised.

Though simple, the above example demonstrates most all the procedures when creating a GUI: creating GUI objects, accessing their properties, laying out the objects, and creating interactivity by assigning callbacks to user-initiated events.

Basics

constructors

Let's look at another example, this one mimics, the first manipulate example.

## needs Gtk or Tk
ENV["toolkit"] = "Gtk"
using JGUI, Winston

w = window()
f = hbox(w); push!(f)

sl = slider(f, 1:10)
sl[:size] = [100, 20]
cnv = cairographic(f)

append!(f, [sl, cnv])

connect(sl, "valueChanged") do u
  p = plot(x -> sin(u * x), 0, 2pi)
  display(cnv, p)
end

notify(sl, "valueChanged", 1) # draw initial graphic

In the above we have several constructors: window, hbox, slider, and cairographic. Each produces a widget. The window creates a toplevel window, and slider a slider. The hbox constructor creates a horizontal box container, which is used above to hold two children, the slider and a cairo graphic device produced by cairographic. (The pyplotgraphic widget produces a device for graphics drawn via PyPlot and can be use with the Qt toolkit.)

As mentioned, constructors in JGUI, except for window, have a parent container passed as the first argument. Additional arguments are used to customize the constructor. For hbox and cairographic, there is no needed customization, though the latter may have a width and height argument specified. For a slider, one needs to specify the range that is stepped over. Unlike most slider implementations, such as the one in Tk, a slider is used to select amongst the specified range or sorted vector. This reduces the need to specify a step size and is more in line with how julia produces sequences of values.

For a slider, the valueChanged signal passes the new value to the callback. This value is then used within the callback that produces the graphic. One could also access this value within the callback with sl[:value].

The cairographic widget is not integrated into the multimedia display system (out of ignorance). Following Winston, the display method above takes as a first argument the cairographic widget and the second object a Winston object to plot. If doing this at the command line, use a semicolon after the plot call to avoid displaying automatically.

The last line is one hacky way to get the initial graphic drawn. The notify method of the underlying model notifies any observers of a "valueChanged" event for a specified value.

Containers

Constructors produce basically two types of objects: controls and containers. The containers available in this package are few:

hbox and vbox produce horizontal and vertical box containers.

grid makes a container for arranging its children in a grid.

notebook provides a tabbed notebook for organizing its children.

formlayout provides a simple way to lay out label/controls in a grid

labelframe provides a simple container for holding a single child (like window), in this case with a label and decorative frame.

Containers are coupled with a layout manager which are utilized in a "julian" manner:

The hbox and vbox containers have methods push!, insert!, and append! for adding children to the layout; pop! and splice! for removing children. In the above example, we use append! to add two children at a time.

The formlayout and notebook containers also implement the above for adding a child at a time, with an additional label.

The single-child containers, labelframe and window, use push! to add their child.

Children of a grid container are managed via matrix notation. There are two styles. One can add a matrix of widgets:

w = window(title="Matrix of widgets")
g = grid(w); push!(g)        # push!(g) is same as push!(w, g)
b1 = button(g, "one")
b2 = button(g, "two")
b3 = button(g, "three")

g[:,:] = [b1 b2; nothing b3]
raise(w)

Or one can add a single child using [row,column] notation. These may be specified through a range to span multiple rows or columns.

The expanding and alignment properties of how a child is placed into a parent are specified for the child, not the container. These are done through the properties :sizepolicy and :alignment. Padding is done through the :spacing properties of the containers.

Properties

Widgets have properties that can be queried and set through index notation where a symbol is used for indexing. For example, to set the size policy of a widget, we have:

w = window(size=[300, 300])
f = hbox(w); push!(f)
b = button(f, "expanding")

#b[:sizepolicy] = (:expand, :fixed)  # expand in x direction
b[:sizepolicy] = (:fixed, :expand)   # expand in y direction
#b[:sizepolicy] = (:expand, :expand) # expand in both

push!(b)
raise(w)

Some properties are dynamic, this one is not. It should be set before packing into a layout.

The main value of a widget is assigned the value property. For a button this is its label:

w = window(title="change label")
b = button(w, "old label"); push!(b)
b[:value]            # "old label"
b[:value] = "new label"        # updates button
b[:value]            # "new label"

When a property, say :prop, is looked up a search for either a getProp or setProp method is made. Though not exported, save for getValue and setValue, these functions can be conveniently employed when using the property in a callback.

Signals

The basic connect method is used to connect a callback to an event. The syntax follows Qt's signals and slots usage. It can take two forms: connect(receiver, signal, obj, slot) or connect(receiver, signal, slot), where slot is a function. In the first instance, the call is slot(obj, vals...) and the second, just slot(vals...) where vals... depends on the signal: the basic valueChanged signal passes in the value; whereas, a button's clicked signal has no value passed. One can also use the do syntax to specify the slot as a function.

Widgets have different signals defined. Mostly the names follow a small subset of those for the corresponding Qt widget (hence the names in camelCase format).

The connect method returns an id. This can be used with disconnect to remove an observer of an object. At present there is no way to temporarily suspend a callback.

As an example, This is how one connects a slider value to a label:

w = window(title="label and slider")
f = hbox(w); push!(f)
sl = slider(f, 1:20)
sl[:size] = [100, 20]
l = label(f, sl[:value])
append!(f, [sl, l])

connect(sl, "valueChanged", l, setValue)
raise(w)

Some alternatives would be connect(rb, "valueChanged", l, (l, value) -> l[:value] = value) or connect(rb, "valueChanged", value -> l[:value] = value).

As an aside, this can also be done just by sharing the underlying model, as with:

w = window(title="label and slider")
f = hbox(w); push!(f)
sl = slider(f, 1:20)
sl[:size] = [100, 20]
l = label(f, sl.model)
append!(f, [sl, l])

React

The React.jl package is a signaling package. It will be integrated more fully, but for the output of one widget can be used as the input of another. The above example can be done via:

w =  window(title="label and slider")
f = hbox(w); push!(f)
sl = slider(f, 1:20)
sl[:size] = [100, 20]
l = label(f, "")
l[:value] = sl            # connect value of slider to value of label.
append!(f, [sl, l])
raise(w)

Widgets

The basic widgets are:

label a standard text label

separator used to place a horizontal or vertical line in a layout

button a push button

lineedit a single line text edit

textedit multi-line text edit

checkbox a simple true/false toggle

radiogroup exclusive set of buttons

buttongroup exclusive (or not) set of buttons

combobox a popup selection widget

slider select from numeric range

slider2d select two variables from numeric range. (Not Gtk.)

listview Show a vector of values allowing selection of one or more. (Not Gtk.)

storeview used to display store of records.

treeview used to display tree structured records. (Not Gtk.)

cairographic used with Winston graphics (Gtk and Tk only)

pyplotgraphic used with PyPlot graphics (Qt only)

imageview used to display png or gif image files.

XXX example

Cairo graphic example

The cairographic widget is a light wrapper around a Cairo canvas provided by the Tk and Gtk packages.

To use the canvas, access the :widget property of the cairographic object:

## update two graphics windows...
ENV["toolkit"] = "Gtk"
using JGUI, Winston
w = window()
f = grid(w); push!(f)
cnv1 = cairographic(f, width=480, height=400)
cnv2 = cairographic(f, width=480, height=400)
b = button(f, "update"); b[:alignment] = (:right, :center)
f[:,:] = [cnv1 cnv2; nothing b]
connect(b, "clicked") do
   p = FramedPlot(); add(p, Curve(rand(10), rand(10))); display(cnv1, p)
   p = FramedPlot(); add(p, Curve(rand(10), rand(10))); display(cnv2, p)
end
notify(b, "clicked")    # roundabout way to draw initial graphic, ...

Storeview example

A common widget is a tabular display of data, where each row represents a case, and column some measurement associate to that case. Basically a spreadsheet with some consistency in the usage. The storeview widget can display such data.

A Store is a vector of records, or cases. Each record is an array of type Any or a tuple. The records are displayed in a grid , one row per record. The storeview widget is used to display values in an underlying Store object.

Here is an example. First a store is defined. We specify the type of variables for each column, as this information is needed in some toolkits (such as Gtk, where this design is used for @GtkListStore).


store = Store(Int, Float64, String)
push!(store, (1, 1.0, "one"))
push!(store, (2, 2.0, "two"))
push!(store, (3, 3.0, "three"))

Here is how we lay it out:

w = window(size=[300, 300])
sv = storeview(w, store)
push!(sv)            
sv[:names] = ["Int", "Float64", "String"]
sv[:widths] = [100, 100, 100]    # column widths
sv[:selectmode] = :multiple    # :single or :multiple
id = connect(sv, "clicked", (row, col) -> println((row, col))) # sample handler

raise(w)

One can add and remove items through insert!, splice!; one can modify existing items through indexing:

t4 = (4, 4.0, "four")
push!(store, t4)
splice!(store, 1)
item = store[1]
item[3] = uppercase(item[3])
store[1] = item

The valueChanged signal passes the index (or indices) that are selected. These are also given by the :index property. The :value property returns the items. One uses :index to set the selection, not :value.

In addition to rowClicked, there are rowDoubleClicked, headerClicked, and selectionChanged signals.

Treeview example

A treeview uses a treestore to hold the data. Here is a simple example:

ENV["toolkit"] = "Tk"        # not Gtk!!!

tstore = treestore(Int, Float64, String)

t1 = (1, 1.0, "one")
t11 = (11, 11.0, "one-one")
t2  = (2, 2.0, "two")


w = window(size=[300, 300])
tv = treeview(w, tstore)
tv[:names] = ["Int", "Float", "String"]
tv[:widths] = [50,50,-1]

tv[:keyname] = "key"
tv[:keywidth] = 100

push!(w, tv)  

raise(w)

To manage child items, we have insert! (with signature (store, parent, sibling position, label, [data])):

node = insert!(tstore, nothing, 1, "label1", t1)
insert!(tstore, node, 1, "label11", t11)
node = insert!(tstore, nothing, 2, "label2", t2)

Nodes are related to a path, which specifies the ancestry. The path [3,2,1] would be the first child of the second child of the third child of the root. We use the path to find a node to open via:

node = path_to_node(tstore, [1])
expand_node(tv, node)

We can remove nodes via a two-argument form of pop!

node = path_to_node(tstore, [1,1])
pop!(tstore, node)

Dialogs

There are some standard modal dialogs

filedialog

messagebox

confirmbox

In addition, the dialog constructor can be used to generate dialogs, somewhat similar to Qt's base Dialog class:

ENV["toolkit"] = "Tk"        # not Gtk!!!
using JGUI
w = window()            # Some parent to position the dialog near

dlg = dialog(w, buttons=[:cancel, :ok]) # default is just `:ok`
f = vbox(dlg); push!(f)

l = label(f, "More complicated controls go here"); push!(l)

connect(dlg, "finished", state -> println(state))
dlg.exec()

Download Details:

Author: jverzani
Source Code: https://github.com/jverzani/JGUI.jl 
License: View license

#julia #gui #interface 

JGUI.jl: Simplified interface for Making GUIs in Julia
Nat  Grady

Nat Grady

1660293004

A Database interface Definition for Communication Between R & RDBMSs

DBI

The DBI package helps connecting R to database management systems (DBMS). DBI separates the connectivity to the DBMS into a “front-end” and a “back-end”. The package defines an interface that is implemented by DBI backends such as:

and many more, see the list of backends. R scripts and packages use DBI to access various databases through their DBI backends.

The interface defines a small set of classes and methods similar in spirit to Perl’s DBI, Java’s JDBC, Python’s DB-API, and Microsoft’s ODBC. It supports the following operations:

  • connect/disconnect to the DBMS
  • create and execute statements in the DBMS
  • extract results/output from statements
  • error/exception handling
  • information (meta-data) from database objects
  • transaction management (optional)

Installation

Most users who want to access a database do not need to install DBI directly. It will be installed automatically when you install one of the database backends:

You can install the released version of DBI from CRAN with:

install.packages("DBI")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("r-dbi/DBI")

Example

The following example illustrates some of the DBI capabilities:

library(DBI)
# Create an ephemeral in-memory RSQLite database
con <- dbConnect(RSQLite::SQLite(), dbname = ":memory:")

dbListTables(con)
#> character(0)
dbWriteTable(con, "mtcars", mtcars)
dbListTables(con)
#> [1] "mtcars"

dbListFields(con, "mtcars")
#>  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
#> [11] "carb"
dbReadTable(con, "mtcars")
#>    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> 2 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> 3 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> 4 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> 5 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> 6 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> 7 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> 8 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#> 9 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#>  [ reached 'max' / getOption("max.print") -- omitted 23 rows ]

# You can fetch all results:
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
dbFetch(res)
#>    mpg cyl  disp hp drat    wt  qsec vs am gear carb
#> 1 22.8   4 108.0 93 3.85 2.320 18.61  1  1    4    1
#> 2 24.4   4 146.7 62 3.69 3.190 20.00  1  0    4    2
#> 3 22.8   4 140.8 95 3.92 3.150 22.90  1  0    4    2
#> 4 32.4   4  78.7 66 4.08 2.200 19.47  1  1    4    1
#> 5 30.4   4  75.7 52 4.93 1.615 18.52  1  1    4    2
#> 6 33.9   4  71.1 65 4.22 1.835 19.90  1  1    4    1
#> 7 21.5   4 120.1 97 3.70 2.465 20.01  1  0    3    1
#> 8 27.3   4  79.0 66 4.08 1.935 18.90  1  1    4    1
#> 9 26.0   4 120.3 91 4.43 2.140 16.70  0  1    5    2
#>  [ reached 'max' / getOption("max.print") -- omitted 2 rows ]
dbClearResult(res)

# Or a chunk at a time
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
while(!dbHasCompleted(res)){
  chunk <- dbFetch(res, n = 5)
  print(nrow(chunk))
}
#> [1] 5
#> [1] 5
#> [1] 1
dbClearResult(res)

dbDisconnect(con)

Class structure

There are four main DBI classes. Three which are each extended by individual database backends:

DBIObject: a common base class for all DBI.

DBIDriver: a base class representing overall DBMS properties. Typically generator functions instantiate the driver objects like RSQLite(), RPostgreSQL(), RMySQL() etc.

DBIConnection: represents a connection to a specific database

DBIResult: the result of a DBMS query or statement.

All classes are virtual: they cannot be instantiated directly and instead must be subclassed.

Further Reading

Databases using R describes the tools and best practices in this ecosystem.

The DBI project site hosts a blog where recent developments are presented.

A history of DBI by David James, the driving force behind the development of DBI, and many of the packages that implement it.


Please note that the DBI project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.


Download Details:

Author: r-dbi
Source Code: https://github.com/r-dbi/DBI 
License: LGPL-2.1 license

#r #database #interface 

A Database interface Definition for Communication Between R & RDBMSs
Monty  Boehm

Monty Boehm

1660078920

Iceberg.jl: Ice-seawater interface Calculations using Level Set Method

Iceberg.jl

Performs ice-seawater interface calculations using level set methods. The level set scheme is similar to that described by Chen, et al. (1997). Model state is passed around using a subtype of the ModelState abstract type. Physical constants and parameters are contained in a PhysicalParameters type. See initialize1d() for an example.

Iceberg is new and under development. It requires a recent build of Julia (i.e. 0.3).

Model dimension

Models in one, two, and three dimensions will be supported. Dimension is indicated based on the concrete type of the ModelState type passed to functions. Currently, one-dimensional models are implemented.

Field evolution

Methods to solve the heat equation are contained in heat.jl.

Interface fluxes can be calculated using the front_velocity() function. Level set reinitialization uses a global method described by Chen et al. (1997) and Osher and Fedkiw (2003). In the future, there will be functions to handle level set function evolution with a hyperbolic equation solver.

Validation

The function initialize1d_hill() recreates a scenario easily compared to the analytical solution of Hill (1987), Section 1.3. An instance of this scenario with n=200 is a part of the test suite.

To do

  • extend to higher dimensions
  • add simple Navier-Stokes field solver for fluid phase

References

Chen, S., B. Merriman, S. Osher and P. Smereka. A simple level set method for solving Stefan problems, 1997. Journal of Computational Physics, 135, 8-29.

Hill, J. M. One-dimensional Stefan Problems: an introduction, 1987. Pitman Monographs and Surveys in Pure and Applied Mathematics, 31. John Wiley and Sons, New York.

Osher, S. and R. Fedkiw. Level Set Methods and Dynamic Implicit Surfaces, 2003. Applied Mathematical Sciences, 153. Springer-Verlag, New York.

Download Details:

Author: njwilson23
Source Code: https://github.com/njwilson23/Iceberg.jl 

#julia #methods #interface 

Iceberg.jl: Ice-seawater interface Calculations using Level Set Method
Nat  Grady

Nat Grady

1660014360

Dygraphs: R interface to Dygraphs

dygraphs for R  

The dygraphs package is an R interface to the dygraphs JavaScript charting library. It provides rich facilites for charting time-series data in R, including:

  • Automatically plots xts time series objects (or any object convertible to xts).
  • Highly configurable axis and series display (including optional second Y-axis).
  • Rich interactive features including zoom/pan and series/point highlighting.
  • Display upper/lower bars (e.g. prediction intervals) around series.
  • Various graph overlays including shaded regions, event lines, and point annotations.
  • Use at the R console just like conventional R plots (via RStudio Viewer).
  • Seamless embedding within R Markdown documents and Shiny web applications.

Installation

The dygraphs package depends on the development version of the htmlwidgets package so you need to install both packages. You can do this using the devtools package as follows:

devtools::install_github(c("ramnathv/htmlwidgets", "rstudio/dygraphs"))

Usage

If you have an xts-compatible time-series object creating an interactive plot of it is as simple as this:

dygraph(nhtemp, main = "New Haven Temperatures")

You can also further customize axes and series display as well as add interacitve features like a range selector:

dygraph(nhtemp, main = "New Haven Temperatures") %>%
  dyAxis("y", label = "Temp (F)", valueRange = c(40, 60)) %>%
  dyOptions(fillGraph = TRUE, drawGrid = FALSE) %>%
  dyRangeSelector()

See the online documentation for the dygraphs package for additional details and examples.

Download Details:

Author: rstudio
Source Code: https://github.com/rstudio/dygraphs 
License: View license

#r #interface 

Dygraphs: R interface to Dygraphs
Hunter  Krajcik

Hunter Krajcik

1659664560

Webrtc_interface: WebRTC interface for Dart-Web/Flutter

webrtc-interface

WebRTC interface for Dart-Web/Futter.

Installing

Use this package as a library

Depend on it

Run this command:

With Dart:

 $ dart pub add webrtc_interface

With Flutter:

 $ flutter pub add webrtc_interface

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):

dependencies:
  webrtc_interface: ^1.0.5

Alternatively, your editor might support dart pub get or flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:webrtc_interface/webrtc_interface.dart';

Original article source at: https://pub.dev/packages/webrtc_interface 

#flutter #dart #webrtc #interface 

Webrtc_interface: WebRTC interface for Dart-Web/Flutter
Dexter  Goodwin

Dexter Goodwin

1659497580

RstWiki: Simple reST/wiki system

RstWiki - simple wiki/rst interface

RstWiki provides a simple standalone Wiki interface to a directory of .rst (reStructuredText) files. These .rst files are the sources to a Sphinx documentation instance. The interface is a simple rst-rendering application with basic authentication and VCS integration.

Dependencies

  • Cheetah >= 2.4
  • CherryPy >= 3.1.2
  • Docutils >= 0.5
  • Python >= 2.6
  • Pygments >= 1.4

Optional dependencies

  • GitDB == 0.5.2 (required for VCS integration)
  • GitPython >= 0.3.1 (required for VCS integration)
  • python-ldap >= 2.4 (required for LDAP authentication)
  • Sphinx >= 1.0
  • Dojo, CodeGlass

Installation

  1. Run easy_install cheetah cherrypy docutils pygments gitdb==0.5.2 gitpython to install dependencies
  2. Copy wiki.sample.conf to wiki.conf and configure appropriately
  3. Ensure the session storage directory exists (by default, this is /tmp/rstwiki_sessions)
  4. Run wiki.py

Configuration

There are two major configuration files. Options that should be modified for normal operation are listed below.

global.conf

[global]
server.socket_port - Listen for requests on this port.
server.socket_host - Listen for requests on this IP address. Use 0.0.0.0 to
                     bind to all interfaces.
tools.sessions.storage_type - Type of session storage to use. One of "ram",
                              "file", "postgresql".
tools.sessions.storage_path - The directory to store session data when using
                              "file" storage type.
tools.sessions.timeout - Session timeout, in seconds.

wiki.conf

[/_static]
tools.staticdir.dir - The absolute path to the ``_static`` directory.

[wiki]
root - The absolute path to the directory in which wiki data will be stored.
enable_vcs - Whether or not to use a VCS when managing wiki entries. If this
             is True, GitDB and GitPython must be installed.
editable - Whether or not wiki contents should be editable.

[auth]
type - Type of authentication to use. One of "ldap", "bypass".

[vcs]
type - The type of version control system to use. Currently, only "git" is
       supported.
repo - The address of the repository used to store and retrieve wiki data.
push.enabled - Whether or not to push to upstream when a commit is made to
               the local repository.
[api]
base_url - The root URL for looking up api-doc directives.  *Note* that
  there is an issue where when building the docs, this option is ignored
  and you currently have to manually set it in ``src/dojo.py``.

Download Details: 

Author: phiggins42
Source Code: https://github.com/phiggins42/rstwiki 
License: BSD

#javascript #system #interface 

RstWiki: Simple reST/wiki system
Royce  Reinger

Royce Reinger

1658725696

Liblinear-ruby-swig: This is The Ruby interface to LIBLINEAR

liblinear-ruby-swig

Ruby interface to LIBLINEAR (using SWIG)

DESCRIPTION:

This is the Ruby LIBLINEAR SWIG (Simplified Wrapper and Interface Generator) interface. LIBLINEAR is a high performance machine learning library for large scale text mining(www.csie.ntu.edu.tw/~cjlin/liblinear).

A slightly modified version of LIBLINEAR 1.8 is included which allows turning on/off the default debuging/logging messages. You don't need your own copy of SWIG to use this library - all needed files are generated using SWIG already.

LIBLINEAR is in use at tweetsentiments.com - A Twitter / Tweet sentiment analysis application

INSTALL:

Currently the gem is available on linux and OS X, and you will need g++ installed to compile the native code.

sudo gem sources -a http://gems.github.com   (you only have to do this once)
sudo gem install tomz-liblinear-ruby-swig

SYNOPSIS:

Try the following multiclass problem in irb:

irb(main):001:0> require 'rubygems'
irb(main):002:0> require 'linear'
irb(main):003:0> pa = LParameter.new
irb(main):004:0> pa.solver_type = MCSVM_CS 
irb(main):005:0> pa.eps = 0.1
irb(main):006:0> bias = 1
irb(main):007:0> labels = [1, 2, 1, 2, 3]
irb(main):008:0> samples = [
irb(main):009:1*            {1=>0,2=>0.1,3=>0.2,4=>0,5=>0},
irb(main):010:1*            {1=>0,2=>0.1,3=>0.3,4=>-1.2,5=>0},
irb(main):011:1*            {1=>0.4,2=>0,3=>0,4=>0,5=>0},
irb(main):012:1*            {1=>0,2=>0.1,3=>0,4=>1.4,5=>0.5},
irb(main):013:1*            {1=>-0.1,2=>-0.2,3=>0.1,4=>1.1,5=>0.1}
irb(main):014:1>           ]
irb(main):016:0> sp = LProblem.new(labels,samples,bias)
irb(main):017:0> m = LModel.new(sp, pa)
irb(main):018:0>  pred = m.predict({1=>1,2=>0.1,3=>0.2,4=>0,5=>0})
=> 1
irb(main):019:0>  pred = m.predict({1=>0,2=>0.1,3=>0.2,4=>0,5=>0})
=> 2
irb(main):020:0>  pred = m.predict({1=>0,2=>0.1,3=>0.2,4=>0,5=>0})
=> 2
irb(main):025:0>  pred = m.predict({1=>0.4,2=>0,3=>0,4=>0,5=>0})
=> 1
irb(main):021:0>  pred = m.predict({1=>-0.1,2=>-0.2,3=>0.1,4=>1.1,5=>0.1})
=> 3
irb(main):022:0> m.save("test.model")

For more examples see test*.rb in the liblinear-ruby-swig/liblinear-1.8/ruby directory


www.tomzconsulting.com

tweetsentiments.com


Author: Tomz
Source Code: https://github.com/tomz/liblinear-ruby-swig 
License: View license

#ruby #interface 

Liblinear-ruby-swig: This is The Ruby interface to LIBLINEAR
Monty  Boehm

Monty Boehm

1658637360

ScikitLearnBase.jl: Abstract interface of ScikitLearn.jl

ScikitLearnBase.jl

This package exposes the scikit-learn interface. Packages that implement this interface can be used in conjunction with ScikitLearn.jl (pipelines, cross-validation, hyperparameter tuning, ...)

This is an intentionally slim package (~100 LOC, no dependencies). That way, ML libraries can import ScikitLearnBase without dragging along all of ScikitLearn's dependencies.

Overview

The docs contain an overview of the API and a more thorough specification.

There are two implementation strategies for an existing machine learning package:

  • Create a new type that wraps the existing type. The new type can usually be written entirely on top of the existing codebase (i.e. without modifying it). This gives more implementation freedom, and a more consistent interface amongst the various ScikitLearn.jl models. Here's an example from DecisionTree.jl
  • Use the existing type. This requires less code, and is usually better when the model type already contains the hyperparameters / fitting arguments.

Example

For models with simple hyperparameters, it boils down to this:

import ScikitLearnBase

mutable struct NaiveBayes
    # The model hyperparameters (not learned from data)
    bias::Float64

    # The parameters learned from data
    counts::Matrix{Int}
    
    # A constructor that accepts the hyperparameters as keyword arguments
    # with sensible defaults
    NaiveBayes(; bias=0.0f0) = new(bias)
end

# This will define `clone`, `set_params!` and `get_params` for the model
ScikitLearnBase.@declare_hyperparameters(NaiveBayes, [:bias])

# NaiveBayes is a classifier
ScikitLearnBase.is_classifier(::NaiveBayes) = true   # not required for transformers

function ScikitLearnBase.fit!(model::NaiveBayes, X, y)
    # X should be of size (n_sample, n_feature)
    .... # modify model.counts here
    return model
end

function ScikitLearnBase.predict(model::NaiveBayes, X)
    .... # returns a vector of predicted classes here
end

Models with more complex hyperparameter specifications should implement clone, get_params and set_params! explicitly instead of using @declare_hyperparameters.

More examples of PRs that implement the interface: GaussianMixtures.jl, GaussianProcesses.jl, DecisionTree.jl, LowRankModels.jl

Note: if the model performs unsupervised learning, implement transform instead of predict.

Once your library implements the API, file an issue/PR to add it to the list of models.

Author: cstjean 
Source Code: https://github.com/cstjean/ScikitLearnBase.jl 
License: View license

#julia #interface 

ScikitLearnBase.jl: Abstract interface of ScikitLearn.jl
Hunter  Krajcik

Hunter Krajcik

1657892940

Amplify analytics Plugin interface

amplify_analytics_plugin_interface

The platform interface for the analytics module of Amplify Flutter.

Installing

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add amplify_analytics_plugin_interface

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  amplify_analytics_plugin_interface: ^0.5.1

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:amplify_analytics_plugin_interface/amplify_analytics_plugin_interface.dart';

Getting Started

Visit our Web Site to learn more about AWS Amplify.

Original article source at: https://pub.dev/packages/amplify_analytics_plugin_interface 

#flutter #dart #plugin #interface 

Amplify analytics Plugin interface
Monty  Boehm

Monty Boehm

1656408660

Word2Vec.jl: Julia interface to Word2vec

Word2Vec 

Julia interface to word2vec

Word2Vec takes a text corpus as input and produces the word vectors as output. Training is done using the original C code, other functionalities are pure Julia. 

Installation

Pkg.add("Word2Vec")

Note: Only linux and OS X are supported.

Functions

All exported functions are documented, i.e., we can type ? functionname to get help. For a list of functions, see here.

Examples

We first download some text corpus, for example http://mattmahoney.net/dc/text8.zip.

Suppose the file text8 is stored in the current working directory. We can train the model with the function word2vec.

julia> word2vec("text8", "text8-vec.txt", verbose = true)
Starting training using file text8
Vocab size: 71291
Words in train file: 16718843
Alpha: 0.000002  Progress: 100.04%  Words/thread/sec: 350.44k  

Now we can import the word vectors text8-vec.txt to Julia.

julia> model = wordvectors("./text8-vec")
WordVectors 71291 words, 100-element Float64 vectors

The vector representation of a word can be obtained using get_vector.

julia> get_vector(model, "book")'
100-element Array{Float64,1}:
 -0.05446138539336186
  0.001090934639284009
  0.06498087707990222
  ⋮
 -0.0024113040415322516
  0.04755140828570571
  0.039764719065723826

The cosine similarity of book, for example, can be computed using cosine_similar_words.

julia> cosine_similar_words(model, "book")
10-element Array{String,1}:
 "book"
 "books"
 "diary"
 "story"
 "chapter"
 "novel"
 "preface"
 "poem"
 "tale"
 "bible"

Word vectors have many interesting properties. For example, vector("king") - vector("man") + vector("woman") is close to vector("queen").

5-element Array{String,1}:
 "queen"
 "empress"
 "prince"
 "princess"
 "throne"

References

Tomas Mikolov, Kai Chen, Greg Corrado, and Jeffrey Dean, "Efficient Estimation of Word Representations in Vector Space", In Proceedings of Workshop at ICLR, 2013. [pdf]

Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado, and Jeffrey Dean. "Distributed Representations of Words and Phrases and their Compositionality", In Proceedings of NIPS, 2013. [pdf]

Tomas Mikolov, Wen-tau Yih, and Geoffrey Zweig, "Linguistic Regularities in Continuous Space Word Representations", In Proceedings of NAACL HLT, 2013. [pdf]

Acknowledgements

The design of the package is inspired by Daniel Rodriguez (@danielfrg)'s Python word2vec interface.

Reporting Bugs

Please file an issue to report a bug or request a feature.

See demo for more details.

Author: JuliaText
Source Code: https://github.com/JuliaText/Word2Vec.jl 
License: View license

#julia #text #interface 

Word2Vec.jl: Julia interface to Word2vec

Gami: GO - Asterisk AMI interface

GAMI

GO - Asterisk AMI Interface

communicate with the Asterisk AMI, Actions and Events.

Example connecting to Asterisk and Send Action get Events.

package main
import (
    "log"
    "github.com/bit4bit/gami"
    "github.com/bit4bit/gami/event"
    "time"
)

func main() {
    ami, err := gami.Dial("127.0.0.1:5038")
    if err != nil {
        log.Fatal(err)
    }
    ami.Run()
    defer ami.Close()
    
    //install manager
    go func() {
        for {
            select {
            //handle network errors
            case err := <-ami.NetError:
                log.Println("Network Error:", err)
                //try new connection every second
                <-time.After(time.Second)
                if err := ami.Reconnect(); err == nil {
                    //call start actions
                    ami.Action("Events", gami.Params{"EventMask": "on"})
                }
                
            case err := <-ami.Error:
                log.Println("error:", err)
            //wait events and process
            case ev := <-ami.Events:
                log.Println("Event Detect: %v", *ev)
                //if want type of events
                log.Println("EventType:", event.New(ev))
            }
        }
    }()
    
    if err := ami.Login("admin", "root"); err != nil {
        log.Fatal(err)
    }
    
    
    if _, errPing := ami.Action("Ping", nil); errPing != nil {
        log.Fatal(errPing)
    }
    
    //async actions
    rsPing, rsErr := ami.AsyncAction("Ping", gami.Params{"ActionID": "pingo"})
    if rsErr != nil {
        log.Fatal(rsErr)
    }
                        
    if _, err := ami.Action("Events", gami.Params{"EventMask":"on"}); err != nil {
        log.Fatal(err)
    }
    
    log.Println("ping:", <-rsPing)
    

}

###TLS SUPPORT In order to use TLS connection to manager interface you could Dial with additional parameters

//without TLS
ami, err := gami.Dial("127.0.0.1:5038")

//if certificate is trusted
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLS)

//if self signed certificate
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLS, gami.UnsecureTLS)

//if custom tls configuration
ami, err := gami.Dial("127.0.0.1:5039", gami.UseTLSConfig(&tls.Config{}))

WARNING: Only Asterisk >=1.6 supports TLS connection to AMI and it needs additional configuration(follow the Asterisk AMI configuration documentation)

CURRENT EVENT TYPES

The events use documentation and struct from PAMI.

use bit4bit/gami/event.New() for get this struct from raw event

EVENT IDTYPE TEST
NewchannelYES
NewextenYES
NewstateYES
DialYES
ExtensionStatusYES
HangupYES
PeerStatusYES
PeerEntryYES
VarSetYES
AgentLoginYES
AgentsYES
AgentLogoffYES
AgentConnectYES
RTPReceiverStatsYES
RTPSenderStatsYES
BridgeYES

Author: Bit4bit
Source Code: https://github.com/bit4bit/gami 
License: MIT license

#go #golang #interface 

Gami: GO - Asterisk AMI interface