Skip to content

dori-dev/python-refactoring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Refactoring

Refactoring Python Applications for Simplicity.

You can open and read project files or use this summary 👇

Concatenate String

my_string = " ".join(["Hi", "Mohammad", "Dori"])

Define Values in Dictionaries

my_dict = {
    "item": "footbal",
    "price": 10
}
count = my_dict.get("count", 1)
count = my_dict.setdefault("count", 1)

Iterate with Enumerate

data = [1, -5, 8, -3]
for index, number in enumerate(data):
    if number < 0:
        data[index] = 0

Format Strings

name = "Mohammad"
my_string = f"Hello {name}"

Generators

my_generator = (n**3 for n in range(10000))
sum_numbers = sum(my_generator))

List Comprehension

squares = [n * n for n in range(10)]

Merge Dictionaries

d1 = {
    "name": "Mohammad Dori",
    "age": 25
}

d2 = {
    "name": "Mohammad Dori",
    "city": "Tehran",
    "job": "Developer"
}
merged_dict = {**d1, **d2}

Unique Values

my_list = [1, 3, 7, 7, 8, 5, 3, 2, 9, 7, 3]
unique_values = set(my_list)

Simplify IF Statement

color = "red"
main_colors = ["red", "blue", "green"]
if color in main_colors:
    print("its main color!")

Sort

data = [
    {
        "name": "ali",
        "age": 20,
    },
    {
        "name": "mohammad",
        "age": 17,
    },
    {
        "name": "sina",
        "age": 50
    }
]

sorted_data = sorted(data, key=lambda x: x["age"])

Counter

from collections import Counter

my_list = [4, 4, 4, 4, 8, 9, 7, 7, 7, 5, 5, 5, 5, 5, 5]
counter = Counter(my_list)

print(counter)
most_common = counter.most_common(2)

Less IF

if a and b:
    return c

Do Not Repeat

if condition:
    result = number * 10
else:
    result = number * 20
label = f"Total {result}"

Less FOR

found = any(conditions)

Project Tree

python-refactoring
├── condition
│   ├── do_not_repeat.py
│   ├── less_if.py
│   └── simplify_if.py
├── loop
│   ├── enumerate_iterate.py
│   ├── list_comprehension.py
│   └── use_fewer_loops.py
├── other
│   ├── merge_dict.py
│   └── set_unique_values.py
├── string
│   ├── concatenate_strings.py
│   └── format_strings.py
└── tool
    ├── define_dict_values.py
    ├── generators.py
    ├── sort_complex_iteration.py
    └── value_counter.py

Links

Download Source Code: Click Here

My Github Acount: Click Here

About

Refactoring Python Applications for Simplicity

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages