Saturday 28 September 2019

Haskell on the Raspberry Pi

Haskell is one of the Functional Programming languages and follows a rather different paradigm to the more commonly taught languages.

There are browser based options, such as https://www.haskellmooc.co.uk/

Installer

There is information about installing the Haskell Platform here.

For a Raspberry Pi, it is in the repository:

sudo apt-get install haskell-platform

References

https://www.haskellmooc.co.uk/

Monday 23 September 2019

Javascript

JavaScript is a high-level scripting language that has become one of the core components of the World Wide Web.
Originally used client side in a user’s browser, it has migrated back to the server and is also used for general scripting purposes.

Windows

JavaScript is one of many languages supported by the Windows Scripting Host (WSH).

Hello World

The following is a JavaScript program using the Windows Scripting Host.
The file is called helloworld.js.

WSH.Echo("Hello world");
WSH.Quit();

Create the file with your favourite plain text editor (Notepad or better, but not a word processor, they tend to add lots of tat).

Save the file.

Bring up the command line interface (search for cmd).

Use the change directory command to move to where you have saved the file (if you have spaces in the folder names, enclose the path in double quotes).

To execute the file type:
cscript helloworld.js
This is what you will get (Windows 10 machine - YMMV)

C:\some\folder\tree>cscript helloworld.js
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

Hello world

C:\some\folder\tree >
An alternative is to use a WSF file (.wsf). This requires the <job> or <package> tags around the script. Because WSF files could contain any supported language (or indeed multiple languages) you need to define the language within a script tag.
<job>
    <script language="JScript">
        WSH.Echo("Hello world");
        WSH.Quit();
    </script>
</job>

C:\some\folder\tree >cscript helloworld.wsf
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

Hello world

C:\some\folder\tree >

HTML

The following is extremely simple.
The script is executed when the page is loaded but before it is displayed. It is possible to execute the JavaScript after the page has loaded or in response to some event.
<!DOCTYPE html>
<html>
    <head>
        <title>Javascript in HTML</title>
    </head>
    <body>
        <p>Testing Javascript</p>
        <script language="JScript">
            alert("Hello world!");
        </script>
    </body>
</html>
Save the file as helloworld.html.
Because this is displayed in your browser, the display will vary.

Linux

Raspberry Pi

From Raspbian Stretch onwards, most standard installs of Raspbian come with node.js installed as standard.
You can check if it is installed by running:
node -v
On the command line. If it is installed, it will return the version number.
If your installation does not have node.js, install using:
sudo apt-get install node
sudo apt-get install npm
The program is just one line:
console.log("Hello world!");
Using terminal, change the directory to where you have saved the file, you can run it by typing:
node helloworld.js
The result will be similar to:
pi@raspberrypi:~/Documents/JavaScript $ node helloworld.js
Hello world!
pi@ raspberrypi:~/Documents/JavaScript $

Useful functions

Splitting Strings

The following is a Windows specific example.
WSH.Echo("Test code");

WSH.Echo(splitString("-2MKptcmt1Q,W3XlnfNPk6I,dRYRcM-HHvY"));

 function splitString(aString){
    WSH.Echo(aString);
    result = "";
    anArray=[];
    anArray = aString.split(",");
    for(ix = 0;ix<anArray.length;ix++){
        result = result +"*" +(anArray[ix])+"*";
    }
    return result;
}


References

https://en.wikipedia.org/wiki/JavaScript
https://en.wikipedia.org/wiki/ECMAScript
https://www.w3schools.com/js/default.asp
https://www.w3schools.com/jsref/default.asp

https://en.wikipedia.org/wiki/Windows_Script_Host
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cscript
https://docs.microsoft.com/en-us/previous-versions//98591fh7(v=vs.85)

https://www.instructables.com/id/Javascripting-Your-RaspberryPi/


Sunday 15 September 2019

Traverse a website with Python

One thing that is important when building a website is that the links on the site connect up.

One way of doing that is to find all the links and then use them.

The following code will find all the links on the first page, and then will visit all the links that point to the same site.

Do not point this at Wikipedia!
# Functions to traverse a website
from lxml import html
import requests
def web_traverse(aURL):
    siteURLs=[]
    
# Get the page from the URL
    page = requests.get(aURL)
# Make an HTML tree from the text
    tree = html.fromstring(page.content)
    tree.make_links_absolute(aURL, resolve_base_href=True)
# Extract urls from the HTML tree
    for  alink in tree.xpath("//a"):
        siteURLs.append(alink.get("href"))
    return siteURLs

def recursive_traverse(aBaseURL,aURL,aLinks):
    print("Traversing " +aURL)
    siteURLs=web_traverse(aURL)
    if siteURLs is not None:
        #print(siteURLs)
        for aLink in siteURLs:
            if aLink is not None:
                if aBaseURL in aLink :
                    if aLink not in aLinks:
                        print("found link:"+aLink)
                        aLinks.append(aLink)
                        aLinks=recursive_traverse(aBaseURL,aLink,aLinks)
    return aLinks
    

    
if __name__ == "__main__":
    # execute only if run as a script
    baseURL="https://technologyisnotdull.blogspot.com"
    #c=web_traverse(baseURL)
    c=recursive_traverse(baseURL,baseURL,[])
    if True:
        print("Links found:")
        for thing in c:
            print (thing )
            #print ((len(thing)))