Pagename: programming - Programming
Category:Array
Post Count: 5Category:Git & github
Pagename: programming - Programming

Git reference – commands

Leave a Reply

Your email address will not be published. Required fields are marked *

Category:Programming
Pagename: programming - Programming

jQuery – test jQuery is running

1. Check for the jQuery or $ object:
  • Open your browser’s developer tools (usually by pressing F12 or Ctrl+Shift+I/Cmd+Option+I).
  • Navigate to the “Console” tab.
  • Type jQuery and press Enter.
  • Type $ and press Enter.
     

If jQuery is loaded, these commands will return the jQuery function or object. If jQuery is not loaded, you will likely receive an “Uncaught ReferenceError: jQuery is not defined” or similar error.

 
2. Check the jQuery version:
  • In the console, type jQuery.fn.jquery and press Enter.
  • Alternatively, type $.fn.jquery and press Enter.
     
 
This will return the version number of the loaded jQuery library (e.g., “3.6.0”). If jQuery is not loaded, you will encounter an error.
3. Use a conditional check:
  • In the console, type if (typeof jQuery != 'undefined') { console.log("jQuery is loaded!"); } else { console.log("jQuery is NOT loaded."); } and press Enter. 
     
 
This snippet will explicitly tell you whether jQuery is present on the page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Category:PowerShell
Pagename: programming - Programming

Powershell – Bulk check of image files

When dealing with collections of thousands of photos, it becomes necessary to use scripts to do things in an efficient manner. 

This script checks each image within the specified folder for errors. It writes the result to the console and to a log file which is saved to the same specified folder location.

This script was largely produced by Cline (Anthropic) AI. It adds a header that includes a time-stamp and ends with a summary of the results.

# Load System.Drawing assembly for image processing
Add-Type -AssemblyName System.Drawing

function Test-ImageFile {
    param (
        [Parameter(Mandatory=$true)]
        [string]$ImagePath
    )
    
    try {
        $image = [System.Drawing.Image]::FromFile($ImagePath)
        $image.Dispose()
        return $true
    }
    catch {
        return $false
    }
}

function Check-ImagesInFolder {
    param (
        [Parameter(Mandatory=$true)]
        [string]$FolderPath
    )

    # Create log file in the target folder
    $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
    $logFile = Join-Path $FolderPath "ImageCheck_$timestamp.log"
    
    $header = @"
Image Check Report
Timestamp: $(Get-Date)
Target Folder: $FolderPath
----------------------------------------
"@
    
    $header | Out-File -FilePath $logFile
    Write-Host "Checking images in folder: $FolderPath" -ForegroundColor Cyan
    Write-Host "----------------------------------------" -ForegroundColor Cyan
    
    # Function to write both to console and log file
    function Write-OutputAndLog {
        param(
            [string]$Message,
            [string]$Color = "White"
        )
        Write-Host $Message -ForegroundColor $Color
        Add-Content -Path $logFile -Value $Message
    }

    # Supported image formats
    $imageExtensions = @("*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp")
    $errorCount = 0
    $totalCount = 0

    foreach ($ext in $imageExtensions) {
        $images = Get-ChildItem -Path $FolderPath -Filter $ext -Recurse -File
        
        foreach ($image in $images) {
            $totalCount++
            $status = "Checking: $($image.Name)..."
            Write-Host $status -NoNewline
            Add-Content -Path $logFile -Value $status -NoNewline

            if (Test-ImageFile -ImagePath $image.FullName) {
                Write-Host " OK" -ForegroundColor Green
                Add-Content -Path $logFile -Value " OK"
            }
            else {
                Write-Host " ERROR" -ForegroundColor Red
                Write-Host "  - Path: $($image.FullName)" -ForegroundColor Yellow
                Add-Content -Path $logFile -Value " ERROR"
                Add-Content -Path $logFile -Value "  - Path: $($image.FullName)"
                $errorCount++
            }
        }
    }

    $summary = @"

Scan Complete
----------------------------------------
Total images scanned: $totalCount
Images with errors: $errorCount
Status: $(if ($errorCount -gt 0) { "Issues Found" } else { "All Images OK" })
"@

    Write-OutputAndLog "`nScan Complete" "Cyan"
    Write-OutputAndLog "----------------------------------------" "Cyan"
    Write-OutputAndLog "Total images scanned: $totalCount"
    Write-OutputAndLog "Images with errors: $errorCount"
    Write-OutputAndLog "Status: $(if ($errorCount -gt 0) { 'Issues Found' } else { 'All Images OK' })" $(if ($errorCount -gt 0) { "Red" } else { "Green" })
    Write-OutputAndLog "Log file created at: $logFile" "Cyan"
}

# Example usage:
# Check-ImagesInFolder -FolderPath "C:\Path\To\Your\Images"

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Category:Programming
Pagename: programming - Programming

R – Import historic ASX data for selected companies

On the Yahoo.com finance site,  you can select the company of interest over a selected period of time. For example, this link shows daily data for ASX over a 12 month period: https://au.finance.yahoo.com/quote/ASX.AX/history/?period1=1700345906&period2=1731968156

To import the data directly into R, I had the best results with the ‘yfR’ package: https://cran.r-project.org/web/packages/yfR/yfR.pdf

install.packages('yfR')
library(yfR)

It has various functionality but to import the stock market data for specific companies, I use ‘yf_get’.

Companies listed in the Australian stock exchange (ASX) get ‘.AX’ postpended to the company code. So BHP becomes BHP.AX.

An individual company can be specified or a list can be created as in this example below.

 

tickers <- c("BHP.AX", "AGL.AX") 
first_date <- Sys.Date() - 30 
last_date <- Sys.Date()

df_yf <- yf_get( tickers = TicketList, first_date = first_date, last_date = last_date)
print(df_yf)

Dates need to be entered in this format “YYY-MM-DD” (ie first_date <- "2001-01-01")

I used this approach to import data for 17 companies over a period of approximately ten years without any difficulty. Other packages like rvest and httr were producing errors.

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Category:Programming
Pagename: programming - Programming

R – Import data from HTML web site

A good reference for this topic was: R for Data Science – Scraping

This post is just my notes while going through that ebook. I post it here for a quick overview when necessary. Examples here come from that source.


Recommended package is ‘rvest’ (as in ‘harvest’ ie scrape). This is included with Tidyverse but is not loaded by default so that needs to be loaded:

library(tidyverse)
library(rvest)

Firstly, import the HTML via ‘read_html’ like so:

html <- read_html("http://rvest.tidyverse.org/")

This returns the HTML document in xml format.

Then it is a matter of looking through the document (ie with browser inspector functions) to find the desired data and the appropriate elements/classes/id’s to target the data. This is done with ‘html_elements’ and ‘html_element’ which returns an array or an element depending on which of these that is used. Class and Id descriptors are used in much the same way as jQuery. (ie “.class” or “#Id”).

html |> html_elements("b")
html |> html_element("b")

Contents of a cell is accessed via ‘html_text2’. ‘Text2’ returns the text as would be seen by the user of the page. ‘html_text’ is a more primitive function. This is an example of how it would be used:

characters |> 
  html_element(".weight") |> 
  html_text2()

HTML can be added directly with ‘minimal_html’ such as: html <- minimal_html("<p><a href='https://link.com'>Random link</a></p>")

html_attr() is used to return the attributes of a selected attribute. html |> html_elements("p") |> html_attr("href")

Table data can be access with html_table(). It returns a tibble with the data found in the specified table.

html |> html_element(".mytable") |> html_table()

 

The rvest package does not handle content managed or manipulated by javascript.

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *