Categories
svelte

Svelte: the missing manual

  • By default, the css specified in one file does not leak to other pages or files, even when that component is nested
  • By default, it is cross site script protected, but you can undo with the @html tag
  • $: at the beginning of a line signifies you are subscribing the variables in that line to react when the values change, and to update the UI accordingly
  • Statements like {#await promise}<p>…waiting</p>{:then number}<p>The number is {number}</p>{:catch error}<p>{error.message}</p>{/await} show you how you can have async promises embeded in the display code itself.
  • Directives allow you to listen to events:
    • <div on:pointermove={handleMove}>The pointer is at {m.x} x {m.y}</div>
    • <script> let m = {x:0, y:0} function handleMove(event){m.x=event.clientX; m.y=event.clientY}</script>
Categories
Vue

Vue: the missing manual

Vue offers the “ref” function to wrap variables:

const message = ref('I am a variable')

You can reach the value of that variable via .value

message.value

Another way to access state is via directives. Example here:

<h1 v-bind:class="dynamicClassHere"></h1>

And in your script section:

const dynamicClassHere = ref('blue')

<style> .blue { color: blue } </style>

When the “dynamicClassHere” variable change, that is bind to the element, and it gets updated.

The above can also be short cutter to:

<h1 :class="dynamicClassHere"></h1>

Event listeners

<button @click="increment">Count is {{ count }}</button>

function increment() { count.value ++ }

Form binding

<input :value="text" @input="onInput">

function onInput(e) { // do something here };

You can accomplish the same using the v-model directive:

<input v-model="text">

Basically, the above skips the need of an “onInput” function

Conditional rendering

This is done via the v-if and v-else directive:

<h1 v-if="conditionalVar">Yay!</h1>

<h1 v-else>Nope</h1>

In this case, the one or the other H1 will show, depending on the boolean conditionalVar

Categories
AWS Sagemaker

AWS Sagemaker: the missing manual

Linear learner algorithm: simple supervised data, usually with just one feature, x, y dimensions, regression kind of

The lastest Sagemaker studio does not have tensorflow by default, to install it via the console:

conda install tensorflow

Categories
Uncategorized

Dynamodb: the basics

Gotcha

Error: The provided key element does not match the schema

Fix: for an AWS endpoint, when trying to do an UpdateItem action via api-gateway, you need to set the method to POST instead of PUT, since the AWS endpoint only takes POST calls

Categories
Solidity

Solidity: the basics

Online IDE: remix.ethereum.org/

Particulars of remix:

  • javascript VM: simulates the network in your browser memory, so you can speed up the development. It doesn’t depend on testing networks being down or slow
  • ganache is another alternative: it runs a network locally, you can just pick a web3 provider in the deploy menu

Solidity:

  • variable initialization defaults
    • strings are empty, ints are zero, booleans are false, so there are no null pointer errors
    • public variables automatically generate a getter function
  • Example of throwing a validation exception
    • require(msg.sender == owner, "You are not the owner");
      • msg is a special object that encapsulates the address that calls the smart contract
      • in this case, we return the specific error, and the whole transaction rolls back
    • transactions are atomic: if an exception happens, they will revert. There is no catch mechanism, so they will cascade thru methods (like Address.send, Address.call, etc)
    • difference between assert vs require:
      • require when revert returns reminding gas
      • require is used to validate user’s input
      • require lets you return an error
      • require triggers: function calls that do not return, receive ether without payable type, transfer errors
      • you can also user revert(“your error message here”) instead of require, to throw explicitly
      • assert doesn’t let you customize the error message
      • assert consumes the gas
      • assert validates things that are unexpected, invariants (out of bounds array, division by zero, non initiated value of internal functions type, or assert(false)
  • Mappings are like hash maps, except they don’t have a length, if you need it, it needs to be stored separately
    • their key type can be pretty much any elementary value
    • their value type can be anything
  • Strucs allow you to create your own data type. But they can’t contain other structs internally
  • Arrays can have both: fixed or dynamic size. The length of arrays can actually result in higher gas consumption
  • Enums are available
  • Functions
    • View function: reading from state (“constant” functions)
    • Pure function: Not reading or modifying state
    • Constructor: only called once during deployment, it is either public or external
    • Fallback function: it is called when you send ether to a contract without specifying the function to call (kind of the default)
      • it can only be external
    • Function visibility:
      • public: can be called from outside the contract
      • private: can only be called from within the contract
      • external: can be called from other contracts or externally
      • internal: only from the contract itself, or derived contracts, can’t be invoked by transactions
  • Inheritance, Modifiers, Importing of files:
    • It is similar to currying functions in javascript, where you can composite a modifier function and include it on others, and it will just run automagically.
    • Usually used to do precondition checks, where you need to do the same ones in multiple functions
  • ABI: application binary interface
    • it contains all the functions / arguments / return values, everything needed to interact with an smart contract
    • the more instructions resulting there from the compilation of your programs, the more gas you will have to pay for transactions against that contract
    • Gas also depends on how congested the network is, not only on the number of complex operations stored in the ABI
    • In the network, the more Gas you pay, the faster your transaction gets processed in the queue of waiting transactions
  • Libraries: they are executed in the context of your smart contract. They can’t receive Ether,

Categories
AWS

AWS: SAM gotchas

If your lambda is just returning before expected, check the default Timeout in template.yaml, most likely you will need a bigger number there

Commands:

sam init

In case you don’t know where to start, that will walk you thru the process and download a sample app for ya

sam build

(pack the latest changes, it will rerun anything new you put in requirements.txt as well

sam local invoke “YourFunctionName” -e events/yourevent.json

run your function locally, with your own event

sam deploy

put it out there

Categories
redshift Uncategorized

Redshift: unable to connect newly created instance

Problem: you just created a new instance, and even though you told it to be publicly available, you can’t connect to it using the provided endpoint….

Solution: you need to explicitly add your current ip address to the security group you are using. Their default security group is miss-leading: even though it says it will accept all traffic from everywhere, it doesn’t (sad panda). Once you add a new security group and attach it to the redshift instance, you will be fine fine fine.

Source of the solution: the infamous stackoverflow:

https://stackoverflow.com/questions/19842720/cant-connect-to-redshift-database

Categories
node.js

puppeteer: a node.js package to simulate browsers

It can be used for web scrapping as well.

Setting up one:

npm init --yes
npm install puppeteer

Create an index.js page, with the following:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://yoururlhere.com/');
  await page.screenshot({ path: 'takingasnapshotofyourpagehere.png' });

  await browser.close();
})();
Categories
express node.js

Express: the basics

On returning content

# automagically detects content and set the right headers:

res.send(returnhere)

# explicitly return json objects:

res.json(JSON.stringify(returnhere))

# you are more interested in sending http codes than actual return objects:

res.status(404).end();

Categories
mongodb

mongodb: the basics

# check existing collections:

show collections

# drop a collection:

db.collectionnamehere.drop()