Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Filter by Categories
About Article
Analyze Data
Archive
Best Practices
Better Outputs
Blog
Code Optimization
Code Quality
Command Line
Course
Daily tips
Dashboard
Data Analysis & Manipulation
Data Engineer
Data Visualization
DataFrame
Delta Lake
DevOps
DuckDB
Environment Management
Feature Engineer
Git
Jupyter Notebook
LLM
LLM Tools
Machine Learning
Machine Learning & AI
Machine Learning Tools
Manage Data
MLOps
Natural Language Processing
Newsletter Archive
NumPy
Pandas
Polars
PySpark
Python Helpers
Python Tips
Python Utilities
Scrape Data
SQL
Testing
Time Series
Tools
Visualization
Visualization & Reporting
Workflow & Automation
Workflow Automation

Python Tips

decorator module: Write Shorter Python Decorators without Nested Functions

Have you ever wished to write a Python decorator with only one function instead of nested functions? If so, try decorator.
In the code above, time_func and time_func_with_decorator produce the exact same results except that time_func_with_decorator is easier and short to write.
Check out other things the decorator library can do.
Link to the source code.

decorator module: Write Shorter Python Decorators without Nested Functions Read More »

eval: Turn a Python String into a Variable or Function

If you want to turn a string into a function or variable, use eval. In the code above, I turn the string variable_1 to a variable.
eval is especially handy when you don’t want to hardcode the names of your variables or functions.
For example, you can use eval and list comprehension like above to combine all values of different variables into a list.
Link to the source code.

eval: Turn a Python String into a Variable or Function Read More »

re.sub: Replace One String with Another String Using Regular Expression

If you want to either replace one string with another string or to change the order of characters in a string, use re.sub.
re.sub allows you to use a regular expression to specify the pattern of the string you want to swap.
In the code above, I replace 3/7/2021 with Sunday and replace 3/7/2021 with 2021/3/7.
Link to the source code.

re.sub: Replace One String with Another String Using Regular Expression Read More »

namedtuple: A Lightweight Python Structure to Mange your Data

If you need a small class to manage data in your project, consider using namedtuple.
namedtuple object is like a tuple but can be used as a normal Python class.
In the code above, I use namedtuple to create a Person object with attributes name and gender. Just like Python class,  you can access attributes of namedtuple using obj.attr.

namedtuple: A Lightweight Python Structure to Mange your Data Read More »

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran