How to Check if a Value Exists in an Enum in TypeScript?

Check if a Value Exists in an Enum in TypeScript

While building an e-commerce application in TypeScript, you might define a ProductCategory enum for valid categories like ‘electronics’, ‘clothing’, and ‘food’. When receiving data from a user or an API, it’s important to check if the provided category actually exists in the enum. This prevents invalid values from being processed or stored. This ensures your … Read more >>

How to Iterate Over Enums in TypeScript?

Iterate Over Enum Keys in TypeScript

When working on TypeScript applications, especially those involving fixed sets of related values like states, cities, or user roles, enums become an essential tool for writing maintainable code. Recently, while working on a project for a US-based company, I encountered a requirement to loop through enum values to display options in a UI dynamically. For … Read more >>

How to Convert an Enum to an Array in TypeScript

TypeScript enum to array: Command Prompt output of a generic helper converting numeric, string and mixed enums

The quickest TypeScript enum to array conversion is a single call: pass the enum to Object.values() for its values or Object.keys() for its names. A string enum gives you a clean array right away; a numeric enum needs one filter() first: My setup for this page was TypeScript 7.0.2 and Node.js v22.22.2 with strict mode … Read more >>

How to Remove Specific Words from a String in Python

Remove specific words from a string in Python: Command Prompt output of re.sub removing whole words

To remove specific words from a string in Python, use re.sub() with word boundaries so only whole words go, then tidy the spaces. Plain replace() works too, but it also cuts the word out of longer words: In this tutorial, we’ll compare four ways to do it, and I ran every script on Python 3.12.5 … Read more >>

How to Read Large CSV Files in Python

Read large CSV files in Python: Command Prompt output of pandas reading a 2 million row sales CSV in chunks

Streaming the file in pieces with pandas chunksize is the reliable way to read large CSV files in Python: only one chunk sits in memory at a time. Loading just the columns and types you need helps too. Here’s the core pattern: In this tutorial, we’ll test every approach on one generated sales file, timed … Read more >>

How to Overwrite a File in Python

Overwrite a file in Python: Command Prompt output of open() in w mode before and after

To overwrite a file in Python, open it in “w” mode and write the new content. Python empties the file the moment it opens it, then saves only what you write, and it creates the file if it’s missing: I tested each script below on Python 3.12.5 on Windows 11, and every output block is … Read more >>

Check If a File Exists and Create It If Not in Python

Python check if a file exists and create it if not: Command Prompt output of open() in x mode

The safest way to check if a file exists and create it if not in Python is one call: open it in “x” mode inside a try block. Python creates a missing file and raises FileExistsError for an existing one, so old data stays safe: Every run you see below was done on Python 3.12.5 … Read more >>

How to Use Python in Excel (=PY Function Guide)

How to use Python in Excel: Microsoft's introduction to Python in Excel support page

Here’s how to use Python in Excel: select a cell, go to Formulas > Insert Python (or type =PY(), write your Python code, and press Ctrl+Enter. Use xl() to pull worksheet data into your code: I don’t have Excel’s cloud Python in my test setup, so the Excel steps follow Microsoft’s support docs as of … Read more >>

How to Move a File in Python (shutil and pathlib)

Move a file in Python: Command Prompt output of shutil.move moving an invoice into an archive folder

The easiest way to move a file in Python is shutil.move(source, destination). Give it a folder and the file goes inside it; give it a full path and the file is moved and renamed in one step: These examples created, moved and checked real files with Python 3.12.5 on Windows; the screenshots show each script’s … Read more >>

How to Close a File in Python

Close a file in Python: Command Prompt output of close() setting the closed attribute to True

To close a file in Python, call close() on the file object, or use a with block, which closes it for you even after an error: My machine for these runs is Python 3.12.5 on Windows 11, which matters for the Windows lock section. In this tutorial, we’ll look at close(), the closed attribute, flush … Read more >>