Filter a list of objects by a property in Python

To filter a list of objects by a property in Python, you can use a list comprehension. Here is an example demonstrating how to filter a list of objects by a specific property:

# Define a sample class
class Employee:
    def __init__(self, name, department, salary):
        self.name = name
        self.department = department
        self.salary = salary

# Create a list of Employee objects
employees = [
    Employee("Alice", "HR", 60000),
    Employee("Bob", "IT", 75000),
    Employee("Charlie", "HR", 55000),
    Employee("David", "IT", 80000),
    Employee("Eve", "Finance", 65000)
]

# Filter the list by department
filtered_employees = [employee for employee in employees if employee.department == "HR"]

# Print the filtered list
for employee in filtered_employees:
    print(f"Name: {employee.name}, Department: {employee.department}, Salary: {employee.salary}")

In this example, we define a class Employee and create a list of Employee objects. We then use a list comprehension to filter the list by the department property, keeping only the employees who work in the “HR” department. Finally, we print the details of the filtered employees.

You can modify the filter condition inside the list comprehension to filter by any other property or criteria.

Migrating a Git repository from HTTPS to SSH

Step 1: Verify SSH Keys

First, ensure you have SSH keys set up on your machine and added to your GitHub account.

  1. Check for existing SSH keys:
    ls -al ~/.ssh

    Look for files named id_rsa and id_rsa.pub or similar.

  2. Generate a new SSH key (if necessary):
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    

    Follow the prompts to save the key (default location is fine).

  3. Add your SSH key to the SSH agent:
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
    
  4. Add the SSH key to your GitHub account: Copy the contents of your public key file to the clipboard:
    cat ~/.ssh/id_rsa.pub
    

    Then, add it to your GitHub account by going to Settings > SSH and GPG keys > New SSH key.

Step 2: Update Remote URL

  1. Navigate to your local repository:
    cd /path/to/your/repo
    
  2. Get the current remote URL:
    git remote -v
    
  3. Update the remote URL to use SSH: Replace origin with the name of your remote if it’s different.
    git remote set-url origin [email protected]:username/repo.git
    

    Replace username with your GitHub username and repo with the name of your repository.

  4. Verify the change:
    git remote -v
    

    You should see the SSH URL listed.

Step 3: Test the Connection

  1. Test the SSH connection:

    You should see a success message like “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”

  2. Fetch from the remote to ensure everything is working:
    git fetch
    

If everything is set up correctly, your repository should now be using SSH instead of HTTPS.

Install Android ADB on Fedora Linux

sudo dnf install android-tools

Create a plugdev group (if it doesn’t already exist) and add the user to it:

sudo groupadd plugdev
sudo usermod -aG plugdev $LOGNAME

Log out and log back in for the group change to take effect. Use the id command to verify that you are in the plugdev group:

id

Identify the Vendor ID:

Connect your Android device to your computer and run the following command to identify the vendor ID:

lsusb

Look for the line that corresponds to your Android device. The vendor ID is the first part of the ID after ID, for example, 18d1 for Google.

Create a udev Rule:

Create a file for the udev rule:

sudo nano /etc/udev/rules.d/51-android.rules

Add the Following Content:

In the file, add a rule to grant the necessary permissions. Replace YOUR-VENDOR-ID with your device’s vendor ID. Here’s an example rule:

SUBSYSTEM=="usb", ATTR{idVendor}=="YOUR-VENDOR-ID", MODE="0666", GROUP="plugdev"

For example, if the vendor ID is 18d1 (Google):

SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0666", GROUP="plugdev"

Change File Permissions:

Change the permissions of the file to make it readable:

sudo chmod a+r /etc/udev/rules.d/51-android.rules

Reload udev rules:

sudo udevadm control --reload-rules
sudo udevadm trigger

Restart your machine.

Check ADB Devices:

Verify that your device is recognized correctly:

adb devices

References
https://developer.android.com/studio/run/device

Using Data Recovery Tools in Linux

Stop Using the Drive

Immediately stop using the drive to prevent further data overwriting.

Identify the Overwritten Part

Determine how much data the ISO has overwritten. Typically, writing an ISO will overwrite the beginning of the drive, including the partition table and some initial sectors.

Use Data Recovery Tools

You can try using data recovery tools that work well on Linux:

  1. TestDisk: This tool can help recover lost partitions and make non-booting disks bootable again. It can also recover deleted files from FAT, NTFS, and ext2 filesystems.
  2. PhotoRec: This companion tool to TestDisk specializes in recovering lost files, including videos, documents, and archives from hard disks, CD-ROMs, and lost pictures from memory cards.
  3. ddrescue: A data recovery tool specifically designed to recover data from damaged disks. It can create a disk image and then work on the image to recover data.

Steps to Recover Data

  1. Install Recovery Tools:
    sudo apt-get install testdisk photorec gddrescue
    
  2. Create a Disk Image: It’s safer to work on a disk image rather than directly on the hard drive.
    sudo ddrescue /dev/sdX /path/to/image.img /path/to/logfile.log
    

    Replace /dev/sdX with your external hard drive’s identifier.

  3. Run TestDisk:
    sudo testdisk /path/to/image.img
    

    Follow the on-screen instructions to analyze and recover lost partitions.

  4. Run PhotoRec:
    sudo photorec /path/to/image.img
    

    This will guide you through recovering individual files.

upsert (insert or update) operation using SQLAlchemy

To achieve an “upsert” (insert or update) operation using SQLAlchemy, you can use the merge method. This method will check if the record exists and update it if it does, or insert it if it does not. Here’s how you can do it:

  1. Define your SQLAlchemy model: Ensure that you have your model defined. For example, let’s assume you have a model called MyModel.
    from sqlalchemy import Column, Integer, String, create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import sessionmaker
    
    Base = declarative_base()
    
    class MyModel(Base):
        __tablename__ = 'my_model'
        id = Column(Integer, primary_key=True)
        name = Column(String)
        value = Column(String)
    
    engine = create_engine('sqlite:///mydatabase.db')
    Session = sessionmaker(bind=engine)
    session = Session()
    
  2. Use the merge method: The merge method will ensure that if the object with the primary key already exists in the database, it will be updated with the new values. If it does not exist, a new record will be created.
    new_data = MyModel(id=1, name='example', value='updated_value')
    session.merge(new_data)
    session.commit()
    

    In this example, if a record with id=1 exists, it will be updated with the new values. If it doesn’t exist, a new record will be created.

many-to-many relationship in SQLAlchemy

In SQLAlchemy, a many-to-many relationship is represented using an association table. This table contains foreign keys that reference the primary keys of the two tables that are involved in the many-to-many relationship. Here’s a step-by-step guide to setting up a many-to-many relationship in SQLAlchemy:

  1. Define the association table: This table contains only foreign keys and optionally other columns.
  2. Define the models: Define the two tables involved in the many-to-many relationship.
  3. Set up the relationship: Use SQLAlchemy’s relationship function to establish the many-to-many relationship.

Here is an example with two models: Student and Course, which have a many-to-many relationship through an enrollment association table.

Step 1: Define the Association Table

from sqlalchemy import Table, Column, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

enrollment = Table('enrollment', Base.metadata,
    Column('student_id', Integer, ForeignKey('students.id'), primary_key=True),
    Column('course_id', Integer, ForeignKey('courses.id'), primary_key=True)
)

Step 2: Define the Models

from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship

class Student(Base):
    __tablename__ = 'students'
    
    id = Column(Integer, primary_key=True)
    name = Column(String)
    
    courses = relationship('Course', secondary=enrollment, back_populates='students')

class Course(Base):
    __tablename__ = 'courses'
    
    id = Column(Integer, primary_key=True)
    title = Column(String)
    
    students = relationship('Student', secondary=enrollment, back_populates='courses')

Step 3: Create the Database and Tables

from sqlalchemy import create_engine

engine = create_engine('sqlite:///school.db')
Base.metadata.create_all(engine)

Step 4: Establishing the Session

from sqlalchemy.orm import sessionmaker

Session = sessionmaker(bind=engine)
session = Session()

Step 5: Adding Data

# Create new students and courses
student1 = Student(name='John Doe')
student2 = Student(name='Jane Smith')
course1 = Course(title='Math 101')
course2 = Course(title='History 101')

# Establish many-to-many relationships
student1.courses.append(course1)
student2.courses.append(course1)
student2.courses.append(course2)

# Add to session and commit
session.add(student1)
session.add(student2)
session.commit()

Step 6: Querying Data

# Querying students enrolled in Math 101
math_students = session.query(Student).join(enrollment).join(Course).filter(Course.title == 'Math 101').all()

for student in math_students:
    print(student.name)

# Querying courses taken by Jane Smith
jane_courses = session.query(Course).join(enrollment).join(Student).filter(Student.name == 'Jane Smith').all()

for course in jane_courses:
    print(course.title)

In this example, enrollment is the association table that links students and courses. The relationship function with the secondary parameter is used to define the many-to-many relationships in both Student and Course classes. The back_populates parameter ensures that the relationship is bidirectional.

This setup allows you to easily query and manage the many-to-many relationship between students and courses.

Change Display gamma on Linux Mint

To change the display gamma on Linux Mint, you can use the xgamma utility. Follow these steps:

Step 1: Install xgamma (if not already installed)

Open a terminal and install xgamma if it is not already installed:

sudo apt update
sudo apt install x11-xserver-utils

Step 2: Adjust the Gamma Value

You can adjust the gamma value using the xgamma command. The gamma value can be set for red, green, and blue channels individually or together. The default gamma value is usually 1.0. Adjusting this value will change the brightness of the display.

To set gamma for all channels equally:

xgamma -gamma 2.2

Step 3: Make the Changes Permanent

The changes made with xgamma will be reset after a reboot. To make them permanent, you can add the xgamma command to your startup applications or add it to your ~/.xprofile file.

Adding to Startup Applications:

  1. Open the Menu and search for “Startup Applications”.
  2. Click “Add” to create a new startup program.
  3. Enter a name (e.g., “Set Gamma”), and in the command field, enter the xgamma command you used (e.g., xgamma -gamma 1.2).
  4. Click “Add” to save the entry.

Adding to ~/.xprofile:

  1. Open a terminal.
  2. Edit the ~/.xprofile file (create it if it does not exist):
    nano ~/.xprofile
    
  3. Add the xgamma command you used (e.g., xgamma -gamma 1.2).
  4. Save the file and exit the editor (in nano, you can do this by pressing Ctrl+X, then Y, and Enter).

Now, the gamma settings should be applied every time you log in.

Step 4: Verify the Changes

To verify the current gamma settings, simply run xgamma without any arguments:

xgamma

This will display the current gamma settings for red, green, and blue channels.

By following these steps, you should be able to adjust and maintain your desired display gamma settings on Linux Mint.

Disable CPU mitigations on Linux Mint

To disable mitigations on Linux Mint, you need to edit the GRUB configuration file and add a kernel parameter. Here are the steps to do that:

  1. Open Terminal: You can open the terminal by searching for it in the application menu or by pressing Ctrl + Alt + T.
  2. Edit GRUB Configuration:
    sudo nano /etc/default/grub
    
  3. Modify the GRUB_CMDLINE_LINUX_DEFAULT Line: Find the line that starts with GRUB_CMDLINE_LINUX_DEFAULT. It usually looks something like this:
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
    

    Add mitigations=off to this line. After editing, it should look like this:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash mitigations=off"
    
  4. Update GRUB: Save the file and exit the editor (in nano, you can do this by pressing Ctrl + X, then Y, and Enter). Then update GRUB with the following command:
    sudo update-grub
    
  5. Reboot: Reboot your system for the changes to take effect:
    sudo reboot
    

After rebooting, the mitigations should be disabled. You can verify this by checking the kernel command line:

cat /proc/cmdline

You should see mitigations=off in the output.

Transform SVGs into React components using svgr as vite plugin

npm install --save-dev vite-plugin-svgr
// vite.config.js
import svgr from "vite-plugin-svgr";

export default {
  // ...
  plugins: [svgr()],
};

Then SVG files can be imported as React components:

import Logo from "./logo.svg?react";

If you are using TypeScript, there is also a declaration helper for better type inference:

/// <reference types="vite-plugin-svgr/client" />

 

References
https://www.npmjs.com/package/vite-plugin-svgr

Save userId using React Redux Toolkit

To save userId using React Redux Toolkit in TypeScript, you can follow these steps:

  1. Set up your Redux store with Redux Toolkit:

    First, you’ll need to install the required dependencies if you haven’t already:

    npm install @reduxjs/toolkit react-redux
    
  2. Create a slice for the user state:

    Create a new file userSlice.ts for the user state slice.

    // src/features/user/userSlice.ts
    import { createSlice, PayloadAction } from '@reduxjs/toolkit';
    
    interface UserState {
      userId: string | null;
    }
    
    const initialState: UserState = {
      userId: null,
    };
    
    const userSlice = createSlice({
      name: 'user',
      initialState,
      reducers: {
        setUserId(state, action: PayloadAction<string>) {
          state.userId = action.payload;
        },
        clearUserId(state) {
          state.userId = null;
        },
      },
    });
    
    export const { setUserId, clearUserId } = userSlice.actions;
    export default userSlice.reducer;
    
  3. Configure the Redux store:

    Configure your Redux store by combining the user slice reducer.

    // src/app/store.ts
    import { configureStore } from '@reduxjs/toolkit';
    import userReducer from '../features/user/userSlice';
    
    const store = configureStore({
      reducer: {
        user: userReducer,
      },
    });
    
    export type RootState = ReturnType<typeof store.getState>;
    export type AppDispatch = typeof store.dispatch;
    export default store;
    
  4. Set up the provider in your main application file:

    Wrap your application with the Redux provider.

    // src/index.tsx
    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Provider } from 'react-redux';
    import store from './app/store';
    import App from './App';
    
    ReactDOM.render(
      <Provider store={store}>
        <App />
      </Provider>,
      document.getElementById('root')
    );
    
  5. Create a component to use the user state and dispatch actions:

    Here’s an example component that uses the user state and dispatches actions to set and clear the userId.

    // src/components/UserComponent.tsx
    import React, { useState } from 'react';
    import { useDispatch, useSelector } from 'react-redux';
    import { RootState } from '../app/store';
    import { setUserId, clearUserId } from '../features/user/userSlice';
    
    const UserComponent: React.FC = () => {
      const dispatch = useDispatch();
      const userId = useSelector((state: RootState) => state.user.userId);
      const [inputUserId, setInputUserId] = useState('');
    
      const handleSetUserId = () => {
        dispatch(setUserId(inputUserId));
      };
    
      const handleClearUserId = () => {
        dispatch(clearUserId());
      };
    
      return (
        <div>
          <h1>User ID: {userId}</h1>
          <input
            type="text"
            value={inputUserId}
            onChange={(e) => setInputUserId(e.target.value)}
          />
          <button onClick={handleSetUserId}>Set User ID</button>
          <button onClick={handleClearUserId}>Clear User ID</button>
        </div>
      );
    };
    
    export default UserComponent;
    
  6. Use the component in your application:

    Finally, use the UserComponent in your application.

    // src/App.tsx
    import React from 'react';
    import UserComponent from './components/UserComponent';
    
    const App: React.FC = () => {
      return (
        <div>
          <UserComponent />
        </div>
      );
    };
    
    export default App;
    

This setup allows you to manage the userId state using Redux Toolkit in a TypeScript React application.