snippet Archives - ISbyR https://isbyr.com/tag/snippet/ Infrequent Smarts by Reshetnikov Thu, 29 May 2025 23:31:24 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.4 Stop pandas truncating output width … https://isbyr.com/stop-pandas-truncating-output-width/ https://isbyr.com/stop-pandas-truncating-output-width/#respond Fri, 02 Feb 2024 13:15:16 +0000 https://isbyr.com/?p=1158 I’m new to pandas (the first time touched it was 45 minutes ago), but I was wondering how can I stop pandas from truncating output width. You know that annoying ... at the end of a field?! So there is a magic display.max_colwidth option (and many other wonderful options). From the official docs: display.max_colwidth : … Continue reading Stop pandas truncating output width … →

The post Stop pandas truncating output width … appeared first on ISbyR.

]]>
I’m new to pandas (the first time touched it was 45 minutes ago), but I was wondering how can I stop pandas from truncating output width.

You know that annoying ... at the end of a field?!

So there is a magic display.max_colwidth option (and many other wonderful options).

From the official docs:

display.max_colwidth : int or None
    The maximum width in characters of a column in the repr of
    a pandas data structure. When the column overflows, a "..."
    placeholder is embedded in the output. A 'None' value means unlimited.
    [default: 50] [currently: 50]
Here are a couple of examples on how to use it:
Pandas display.max_colwidth option that prevents truncation of the fields

`

More posts related to my AI journey:

The post Stop pandas truncating output width … appeared first on ISbyR.

]]>
https://isbyr.com/stop-pandas-truncating-output-width/feed/ 0
Create a free pod index in Pinecone using Python https://isbyr.com/create-a-free-pod-index-in-pinecone-using-python/ https://isbyr.com/create-a-free-pod-index-in-pinecone-using-python/#respond Wed, 31 Jan 2024 12:24:43 +0000 https://isbyr.com/?p=1154 Pinecone documentation is quite good, but when I wanted to create a free pod index in Pinecone using Python, I didn’t know what parameters I should supply. Specifically, I couldn’t understand what values would be or environment and pod_type attributes After a bit of digging (looking at the WebUI), here is how to do it … Continue reading Create a free pod index in Pinecone using Python →

The post Create a free pod index in Pinecone using Python appeared first on ISbyR.

]]>
Pinecone documentation is quite good, but when I wanted to create a free pod index in Pinecone using Python, I didn’t know what parameters I should supply.

Specifically, I couldn’t understand what values would be or environment and pod_type attributes

After a bit of digging (looking at the WebUI), here is how to do it

from pinecone import Pinecone, PodSpec

pc = Pinecone(api_key='<<PINECONE_API_KEY>>')
pc.create_index(
    name="example-index", 
    dimension=1536, 
    metric="cosine", 
    spec=PodSpec(
        environment='gcp-starter', 
        pod_type='s1.x1'
    )
)

More posts related to my AI journey:

The post Create a free pod index in Pinecone using Python appeared first on ISbyR.

]]>
https://isbyr.com/create-a-free-pod-index-in-pinecone-using-python/feed/ 0
How to use an SSH key stored in Azure Key Vault while building Azure Linux VMs using Terraform https://isbyr.com/how-to-use-an-ssh-key-stored-in-azure-key-vault-while-building-azure-linux-vms-using-terraform/ https://isbyr.com/how-to-use-an-ssh-key-stored-in-azure-key-vault-while-building-azure-linux-vms-using-terraform/#comments Mon, 03 Oct 2022 20:14:01 +0000 http://isbyr.com/?p=962 So I want to use the same SSH Public key to be able to authenticate across multiple Linux VMs that I’m building in Azure in Terraform. While I did find a lot of examples (including among Terraform example repo) of how to do it if you have the key stored on your local machine I … Continue reading How to use an SSH key stored in Azure Key Vault while building Azure Linux VMs using Terraform →

The post How to use an SSH key stored in Azure Key Vault while building Azure Linux VMs using Terraform appeared first on ISbyR.

]]>
So I want to use the same SSH Public key to be able to authenticate across multiple Linux VMs that I’m building in Azure in Terraform. While I did find a lot of examples (including among Terraform example repo) of how to do it if you have the key stored on your local machine I couldn’t find (or didn’t search long enough) how to use an SSH key stored in Azure Key Vault while building Azure Linux VMs using Terraform.

So to reiterate what I have and what I want.

I have:

  • a private key stored on my machine (that will be used in the future)
  • a corresponding public key dev-mgmt-ssh-key stored in an existing Azure Key Vault kv-dev-mgmt (which I don’t want to be managed by Terraform, but only used by it)

I want:

  • Terraform to read the public key that is stored in the Azure Key Vault
  • Terraform to use that key while provisioning new VM(s)

Using Terraform to read a key that is stored in Azure Key Vault

We will be using the data functions to read an existing key,

# Get existing Key Vault
data "azurerm_key_vault" "kv" {
  name                = "kv-dev-mgmt"
  resource_group_name = "rg-master"
}

# Get existing Key
data "azurerm_key_vault_key" "ssh_key" {
  name         = "dev-mgmt-ssh-key"
  key_vault_id = data.azurerm_key_vault.kv.id
}

Step 1: we used azurerm_key_vault to access an Azure Key Vault resource by specifying the Resource Group and Key Vault names

Step 2: we used azurerm_key_vault_key to access our key by providing a Key Vault Id and the Key name

Now we have the key stored in ssh_key for future reference.

Providing an ssh public key to Azure Linux VM in Terraform

# Create a VM
resource "azurerm_linux_virtual_machine" "main" {
  name                            = .....
  resource_group_name             = .....
  location                        = .....
  size                            = .....
  admin_username                  = "adminuser"
  admin_ssh_key {
    username = "adminuser"
    public_key = data.azurerm_key_vault_key.ssh_key.public_key_openssh
  }
  disable_password_authentication = true

Note: I have reducted all the configuration lines that are irrelevant to the SSH section (like image type, networking, disk, etc.)

We are passing the public_key_openssh attribute of our ssh_key data source to the public_key property of the admin_ssh_key.

We also disable password authentication by setting the disable_password_authentication to true.

Error: decoding … for public key data

As a bonus, I initially tried to use the public_key_pem attribute of the ssh_key key data source, but that, while being able to pass Terraform validate step didn’t work when running apply and was failing with ‘Error: decoding “admin_ssh_key.0.public_key” for public key data” message.

The post How to use an SSH key stored in Azure Key Vault while building Azure Linux VMs using Terraform appeared first on ISbyR.

]]>
https://isbyr.com/how-to-use-an-ssh-key-stored-in-azure-key-vault-while-building-azure-linux-vms-using-terraform/feed/ 3
Python – Test Network Connection https://isbyr.com/python-test-network-connection/ https://isbyr.com/python-test-network-connection/#respond Wed, 19 Jun 2019 21:35:38 +0000 http://isbyr.com/?p=508 The below will return True/False import socket def test_connection(host="8.8.8.8", port=53, timeout=3): try: socket.setdefaulttimeout(timeout) socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port)) return True except Exception as ex: #print ex.message return False destination_host = "mymachine.company" destination_port = 9997 timeout = 2 test_result = test_connection(destination_host, destination_port, timeout) Or you can use this version to return the Exception in case connection has failed … Continue reading Python – Test Network Connection →

The post Python – Test Network Connection appeared first on ISbyR.

]]>
The below will return True/False

import socket

def test_connection(host="8.8.8.8", port=53, timeout=3):
  try:
    socket.setdefaulttimeout(timeout)
    socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
    return True
  except Exception as ex:
    #print ex.message
    return False


destination_host = "mymachine.company"
destination_port = 9997
timeout = 2
test_result = test_connection(destination_host, destination_port, timeout)

Or you can use this version to return the Exception in case connection has failed

import socket

def test_connection(host="8.8.8.8", port=53, timeout=3):
  try:
    socket.setdefaulttimeout(timeout)
    socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
    return True, None
  except Exception as ex:
    #print ex.message
    return False, ex.message


destination_host = "mymachine.company"
destination_port = 9997
timeout = 2
test_result, ex = test_connection(destination_host, destination_port, timeout)

The post Python – Test Network Connection appeared first on ISbyR.

]]>
https://isbyr.com/python-test-network-connection/feed/ 0
Python – Get local machine IP https://isbyr.com/python-get-local-machine-ip/ https://isbyr.com/python-get-local-machine-ip/#respond Wed, 19 Jun 2019 21:27:44 +0000 http://isbyr.com/?p=506 Here is how to use Python – to Get local machine IP import socket def get_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: # doesn't even have to be reachable s.connect(('10.255.255.255', 1)) IP = s.getsockname()[0] except: IP = '127.0.0.1' finally: s.close() return IP  

The post Python – Get local machine IP appeared first on ISbyR.

]]>
Here is how to use Python – to Get local machine IP

import socket

def get_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        s.connect(('10.255.255.255', 1))
        IP = s.getsockname()[0]
    except:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP

 

The post Python – Get local machine IP appeared first on ISbyR.

]]>
https://isbyr.com/python-get-local-machine-ip/feed/ 0