How to comment in JSON

Link to source video

  • Comments usually break JSON parsing
  • JSMin is one workaround;

    • Put comments in dev version
    • Remove in minified version
  • Add as a property

    {
      "comment": "This is a person object",
      "firstName": "John",
      "lastName": Doe
    }
    
  • Add underscores to denote property

    {
      "__comment__": "This is a person object",
      "__comment__firstName__":"This is the first name",
      "firstName": "John",
      "lastName": Doe
    }
    

Removing an element from an array in Javascript

Link to source video

> var myArray = [0,1,2,3,4];
> delete myArray[4]
> len(myArray)
//still returns 5
//delete sets myArray[4] to undefined

> myArray.pop();
//this removes the lat one

> myArray.length = 2
//keeps the firs ttwo

> myArray.shift();
//shifts by one

> var myArray = [0,1,2,3,4];
> var newArray = myArray.slice(1,4);
//slice only extracts up to and not including the end

> var myArray = [0,1,2,3,4];
> var myArray.splice(2,1);
//splice takes (index, how many items to remove)
//here, we remove one item starting at position 2

> var myArray = [0,1,2,3,4];
> var myArray.splice(2,1, 98, 97, 96, ,3, 4);

//we can also pass further arguments into the array that adds items to the array.

// ways to delete elements: delete, pop, length, shift, slice, and splice

How to remove a property from a JS object?

Link to short video

//let's define an object
> var myObject = {
    propertyA: 1,
    propertyB: 'B',
    propertyC: [1,2,3],
    propertyD: {sub: 'object}
    }

// to delete propertyD
> delete myObject.propertyD;

//will return true or false 
//usually true for objects of our own definition

> delete myObject['propertyC']

//set it to undefined, which is faster
> myObject['propertyB']= undefined;
> myObject.propertyB = undefined;

//use either delete statement, or set it to undefined.

Calling an external command from Python

Source videos from O'Reily

#calls are made using subprocess
import subprocess
# for a simple command, with no args, supply the command as a string
subprocess.call('ls')
# by default, the output goes to STDOUT
#calls are made using subprocess
import subprocess

#this is how to take the output out of STDOUT
output = subprocess.check_output('ls')
print('the output is: ')
print(output)
import subprocess
# what if we wanted to call other args? call it using call/checkoutput with a tuple or list passed through
output = subprocess.check_output(['ls', '-l'])
# what if we wanted to command directly to the UNIX command line?
import subprocess

#in order to use special characters like the wildcard, we need to talk to the shell directly
# instead of a list of arguments, we put in a list of commands that directly talk to the shell
output = subprocess.check_output('ls *.py', shell=True)

#shell = True can be a security risk if you're taking any input from other users
In [ ]: