Recently, Bing Copilot is really keen on being helpful…

Question: how do I remove the last character from a javascript string?

You can remove the last character from a JavaScript string by using the slice() method. Here’s how you can do it:

var str = "Hello, world!";
str = str.slice(0, -1); // "Hello, world"

In this code, str.slice(0, -1) returns a new string that starts from the beginning of str (index 0) and stops at the second to last character (index -1). The original string str is then replaced with this new string. After running this code, str will be "Hello, world" without the exclamation mark. If str is an empty string, str.slice(0, -1) will also return an empty string, so you can safely use this code even if str might be empty. If you want to remove more than one character from the end of the string, you can replace -1 with the negative of the number of characters you want to remove. For example, str.slice(0, -2) would remove the last two characters. If you want to remove a character from the beginning of the string instead, you can use str.slice(1). This will return a new string that starts from the second character of str (index 1) and goes to the end. If you want to remove a character from a specific position in the string, you can use str.slice(0, index) + str.slice(index + 1). This will return a new string that starts from the beginning of str, stops at index, and then continues from index + 1 to the end. Replace index with the index of the character you want to remove. Please note that string indices in JavaScript start from 0, so the first character is at index 0, the second character is at index 1, and so on. If index is out of range (i.e., it’s less than 0 or greater than or equal to the length of the string), str.slice(index + 1) will return an empty string, and the result will be the same as str.slice(0, index). This means you can safely use this code even if index might be out of range. If you want to remove a specific character from the string, regardless of its position, you can use str.replace('char', ''). This will return a new string where the first occurrence of 'char' in str is replaced with an empty string. Replace 'char' with the character you want to remove. If 'char' is not found in str, str.replace('char', '') will return str unchanged, so you can safely use this code even if 'char' might not be in str. If you want to remove all occurrences of a specific character from the string, you can use str.replace(/char/g, ''). This will return a new string where all occurrences of 'char' in str are replaced with an empty string. The g flag in /char/g stands for “global”, which means replace all occurrences, not just the first one. If 'char' is not found in str, str.replace(/char/g, '') will return str unchanged, so you can safely use this code even if 'char' might not be in str. If 'char' is a special regular expression character (i.e., \.^$*+?()[]{}|), you need to escape it with a backslash (\). For example, to remove all periods from the string, you would use str.replace(/\./g, ''). If you want to remove a sequence of characters from the string, you can use str.replace('sequence', ''). This will return a new string where the first occurrence of 'sequence' in str is replaced with an empty string. Replace 'sequence' with the sequence of characters you want to remove. If 'sequence' is not found in str, str.replace('sequence', '') will return str unchanged, so you can safely use this code even if 'sequence' might not be in str. If you want to remove all occurrences of a sequence of characters from the string, you can use str.replace(/sequence/g, ''). This will return a new string where all occurrences of 'sequence' in str are replaced with an empty string. If 'sequence' is not found in str, str.replace(/sequence/g, '') will return str unchanged, so you can safely use this code even if 'sequence' might not be in str. If 'sequence' contains special regular expression characters, you need to escape them with a backslash. For example, to remove all occurrences of the sequence “.com” from the string, you would use str.replace(/\.com/g, ''). If you want to remove a sequence of characters from a specific position in the string, you can use str.slice(0, index) + str.slice(index + length). This will return a new string that starts from the beginning of str, stops at index, and then continues from index + length to the end. Replace index with the index of the first character of the sequence, and length with the length of the sequence. If index is out of range, or if index + length is greater than the length of the string, the result will be the same as str.slice(0, index). This means you can safely use this code even if index might be out of range, or if length might be too large. If you want to remove a sequence of characters from the string, regardless of its position, you can use str.replace('sequence', ''). This will return a new string where the first occurrence of 'sequence' in str is replaced with an empty string. Replace 'sequence' with the sequence of characters you want to remove. If 'sequence' is not found in str, str.replace('sequence', '') will return str unchanged, so you can safely use this code even if 'sequence' might not be in str. If you want to remove all occurrences of a sequence of characters from the string, you can use str.replace(/sequence/g, ''). This will return a new string where all occurrences of 'sequence' in str are replaced with an empty string. If 'sequence' is not found in str, str.replace(/sequence/g, '') will return str unchanged, so you can safely use this code even if 'sequence' might not be in str. If 'sequence' contains special regular expression characters, you need to escape them with a backslash. For example, to remove all occurrences of the sequence “.com” from the string, you would use str.replace(/\.com/g, ''). If you want to remove a sequence of characters from a specific position in the string, you can use str.slice(0, index) + str.slice(index + length). This will return a new string that starts from the beginning of str, stops at index, and then continues from index + length to the end. Replace index with the index of the first character of the sequence, and length with the length of the sequence. If index is out of range, or if index + length is greater than the length of the string, the result will be the same as str.slice(0, index). This means you can safely use this code even if index might be out of range, or if length might be too large. If you want to remove a sequence of characters from the string, regardless of its position, you can use str.replace('sequence', ''). This will return a new string where the first occurrence of 'sequence' in str is replaced with an empty string. Replace 'sequence' with the sequence of characters you want to remove. If 'sequence' is not found in str, str.replace('sequence', '') will return str unchanged, so you can safely use this code even if 'sequence' might not be in str. If you want to remove all occurrences of a sequence of characters from the string, you can use str.replace(/sequence/g, ''). This will return a new string where all occurrences of 'sequence' in str are replaced with an empty string. If 'sequence' is not found in str, str.replace(/sequence/g, '') will return str unchanged, so you can safely use this code even if 'sequence' might not be in str. If 'sequence' contains special regular expression characters, you need to escape them with a backslash. For example, to remove all occurrences of the sequence “.com” from the string, you would use str.replace(/\.com/g, ''). If you want to remove a sequence of characters from a specific position in the string, you can use str.slice(0, index) + str.slice(index + length). This will return a new string that starts from the beginning of str, stops at index, and then continues from index + length to the end. Replace index with the index of the first character of the sequence, and length with the length of the sequence. If index is out of range, or if index + length is greater than the length of the string, the result will be the same as `str.slice

SSH to Windows 10 using private key

It took waaaay to long to make everything work.

  • Create the service and key pairs via this link
  • Make sure that the logs are generated to debug any issues via this link
  • The administrators_authorized_keys file should contain the public key generated in the first step and the permissions should only have the Administrators group for Read and Read & Execute

How many authors in that git project?

I’ve been spending a lot of time on the FixPix Home project and recently have discovered a similar project – chaiNNer, which is open source (GPL 3.0) and, though targeted at a different audience, looks also like a very impressive piece of work.

So I was wondering how many developers have been working on chaiNNer and for how long.

Here’s a quick Linux/Mac one-liner I wrote to determine how many developers contributed to a git project and also a quick glimpse to the relative contribution of each:

$git log | grep Author | sed "s/<.*>$//" | sort | uniq -c | sort -nr
    580 Author: Michael Schmidt
    466 Author: Joey Ballentine
     83 Author: theflyingzamboni
     14 Author: Alex Dodge
     13 Author: dependabot[bot]
      4 Author: Harisreedhar
      3 Author: Jackson Ly
      2 Author: Zarxrax
      2 Author: Sryvkver
      2 Author: BigBoyBarney
      1 Author: xpsychonauticon
      1 Author: emarron
      1 Author: JustNoon
      1 Author: EzoGaming

Calculating how long since the project has been started using a one-liner is also possible with some bash tricks, but would take longer than the time I can allocate for it right now – maybe in a future post. For now this will suffice:

$git log | tail | grep Date
Date:   Tue Sep 7 17:58:15 2021 -0500

Which means they’ve had a about year’s head start in R&D time over FixPix Home 🙂

I got ChatGPT into an endless loop

Luckily, the developers stop the output after a certain point.
This was after I asked about an example of a flawed mathematical proof

Me:
Can you provide an example where x^2-x^2=0 is not true?

ChatGPT:

Sure, here is an example where x^2-x^2=0 is not true:

Let x=2

Then, x^2-x^2=4-4=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=3

Then, x^2-x^2=9-9=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=4

Then, x^2-x^2=16-16=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=5

Then, x^2-x^2=25-25=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=6

Then, x^2-x^2=36-36=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=7

Then, x^2-x^2=49-49=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=8

Then, x^2-x^2=64-64=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=9

Then, x^2-x^2=81-81=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=10

Then, x^2-x^2=100-100=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=11

Then, x^2-x^2=121-121=0

In this case, x^2-x^2 does equal 0. However, if we choose a different value for x, the equation x^2-x^2=0 may not hold true. For example:

Let x=12

Then, x^2-x^2=144-144=0

In this case, x^2-x^2 does equal 0. However, if we choose a different


——-

Bitnami WordPress stuck when trying to save post

There can be many reasons why this can happen. In my case I couldn’t locate the change that was made in WordPress that caused the issue. The only major change since the last time a post was added was a WordPress update, but that didn’t strike me as being the reason as it wasn’t a major version.

Be that as it may, what solved the issue for me was this link – to summarize, change the following lines in wp-config.php

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/');

to

define('WP_SITEURL', 'http://DOMAIN/');
define('WP_HOME', 'http://DOMAIN/');

of course, replace DOMAIN with the primary domain for your WordPress app.

an alternative method is here, though I haven’t tried it

Inaccessible variables in Python

when you code in many languages, you usually need to dig a bit to find answers to questions that are not immediately obvious in a specific language. Mine was whether closures can simulate real private variables in python (i.e that really can’t be accessed as opposed to the __ mangling method).

I decided to test if this would work with closures, as shown below

def outer(a,b):
    class this:
        pass
    
    #--------------------------
    # real private variables and methods go here
    this.a = a
    this.b = b
    
    def sum_():
        return this.a + this.b
    
    def product_():
        return this.a * this.b
    
    def set_nums_(a, b):
        this.a, this.b = a, b
    
    #--------------------------
    # public variables and methods go here
    class inner:
        def get_nums(self):
            return this.a, this.b
        
        def set_nums(self, a, b):
            set_nums_(a,b)
        
        def sum(self):
            return sum_()
        
        def product(self):
            return product_()
    
    return inner()

and the result:

>>> x = outer(5,3)
>>> x.get_nums()
(5, 3)
>>> x.set_nums(5,4)
>>> x.get_nums()
(5, 4)
>>> x.product()
20
>>> x
<__main__.outer..inner object at 0x0000023C581E21C0>
>>>

Yay! seems like it works.

Of course, a bit of further digging showed that every python object has the __closure__ property through which you can access the closure for that object, and changing the values of the “private” variables above (bypassing the set_nums method) just becomes a matter of:

>>> loophole = x.sum.__closure__[0].cell_contents.__closure__[0].cell_contents
>>> loophole
<class '__main__.outer.<locals>.this'>
>>> loophole.a = 8
>>> loophole.b = 9
>>> x.get_nums()
(8, 9)
>>>

AWS Lambda with Docker – revisited

There are various ways to leverage docker to develop AWS lambda serverless functions. One recent method is to deploy Python Lambda functions with container images (as specified here).

Another method is to develop the Lambda package from within an Amazon Linux docker container and then upload it to AWS Lambda (more details here). A summary of how to do this is described below and will work for any platform where you can install Docker.

Open a command prompt and type:

docker pull lambci/lambda

next, type:

docker run -it lambci/lambda:build-python3.8 bash

This will install the environment to run Python 3.8 code and open a shell prompt so you can start working. The first thing recommended to do when getting the bash prompt is yum update

when you exit the shell, you can always re-enter by typing:

docker start -i container_name

(replace container_name with the container name of given to the image named lambci/lambda:build-python3.8, which can be seen via docker ps -a)